]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
Do not separately read type units
[thirdparty/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 /* If this CU was inherited by another CU (via specification,
688 abstract_origin, etc), this is the ancestor CU. */
689 dwarf2_cu *ancestor;
690
691 /* Get the buildsym_compunit for this CU. */
692 buildsym_compunit *get_builder ()
693 {
694 /* If this CU has a builder associated with it, use that. */
695 if (m_builder != nullptr)
696 return m_builder.get ();
697
698 /* Otherwise, search ancestors for a valid builder. */
699 if (ancestor != nullptr)
700 return ancestor->get_builder ();
701
702 return nullptr;
703 }
704 };
705
706 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
707 This includes type_unit_group and quick_file_names. */
708
709 struct stmt_list_hash
710 {
711 /* The DWO unit this table is from or NULL if there is none. */
712 struct dwo_unit *dwo_unit;
713
714 /* Offset in .debug_line or .debug_line.dwo. */
715 sect_offset line_sect_off;
716 };
717
718 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
719 an object of this type. This contains elements of type unit groups
720 that can be shared across objfiles. The non-shareable parts are in
721 type_unit_group_unshareable. */
722
723 struct type_unit_group : public dwarf2_per_cu_data
724 {
725 /* The TUs that share this DW_AT_stmt_list entry.
726 This is added to while parsing type units to build partial symtabs,
727 and is deleted afterwards and not used again. */
728 std::vector<signatured_type *> *tus = nullptr;
729
730 /* The data used to construct the hash key. */
731 struct stmt_list_hash hash {};
732 };
733
734 /* These sections are what may appear in a (real or virtual) DWO file. */
735
736 struct dwo_sections
737 {
738 struct dwarf2_section_info abbrev;
739 struct dwarf2_section_info line;
740 struct dwarf2_section_info loc;
741 struct dwarf2_section_info loclists;
742 struct dwarf2_section_info macinfo;
743 struct dwarf2_section_info macro;
744 struct dwarf2_section_info rnglists;
745 struct dwarf2_section_info str;
746 struct dwarf2_section_info str_offsets;
747 /* In the case of a virtual DWO file, these two are unused. */
748 struct dwarf2_section_info info;
749 std::vector<dwarf2_section_info> types;
750 };
751
752 /* CUs/TUs in DWP/DWO files. */
753
754 struct dwo_unit
755 {
756 /* Backlink to the containing struct dwo_file. */
757 struct dwo_file *dwo_file;
758
759 /* The "id" that distinguishes this CU/TU.
760 .debug_info calls this "dwo_id", .debug_types calls this "signature".
761 Since signatures came first, we stick with it for consistency. */
762 ULONGEST signature;
763
764 /* The section this CU/TU lives in, in the DWO file. */
765 struct dwarf2_section_info *section;
766
767 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
768 sect_offset sect_off;
769 unsigned int length;
770
771 /* For types, offset in the type's DIE of the type defined by this TU. */
772 cu_offset type_offset_in_tu;
773 };
774
775 /* include/dwarf2.h defines the DWP section codes.
776 It defines a max value but it doesn't define a min value, which we
777 use for error checking, so provide one. */
778
779 enum dwp_v2_section_ids
780 {
781 DW_SECT_MIN = 1
782 };
783
784 /* Data for one DWO file.
785
786 This includes virtual DWO files (a virtual DWO file is a DWO file as it
787 appears in a DWP file). DWP files don't really have DWO files per se -
788 comdat folding of types "loses" the DWO file they came from, and from
789 a high level view DWP files appear to contain a mass of random types.
790 However, to maintain consistency with the non-DWP case we pretend DWP
791 files contain virtual DWO files, and we assign each TU with one virtual
792 DWO file (generally based on the line and abbrev section offsets -
793 a heuristic that seems to work in practice). */
794
795 struct dwo_file
796 {
797 dwo_file () = default;
798 DISABLE_COPY_AND_ASSIGN (dwo_file);
799
800 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
801 For virtual DWO files the name is constructed from the section offsets
802 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
803 from related CU+TUs. */
804 const char *dwo_name = nullptr;
805
806 /* The DW_AT_comp_dir attribute. */
807 const char *comp_dir = nullptr;
808
809 /* The bfd, when the file is open. Otherwise this is NULL.
810 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
811 gdb_bfd_ref_ptr dbfd;
812
813 /* The sections that make up this DWO file.
814 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
815 sections (for lack of a better name). */
816 struct dwo_sections sections {};
817
818 /* The CUs in the file.
819 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
820 an extension to handle LLVM's Link Time Optimization output (where
821 multiple source files may be compiled into a single object/dwo pair). */
822 htab_up cus;
823
824 /* Table of TUs in the file.
825 Each element is a struct dwo_unit. */
826 htab_up tus;
827 };
828
829 /* These sections are what may appear in a DWP file. */
830
831 struct dwp_sections
832 {
833 /* These are used by all DWP versions (1, 2 and 5). */
834 struct dwarf2_section_info str;
835 struct dwarf2_section_info cu_index;
836 struct dwarf2_section_info tu_index;
837
838 /* These are only used by DWP version 2 and version 5 files.
839 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
840 sections are referenced by section number, and are not recorded here.
841 In DWP version 2 or 5 there is at most one copy of all these sections,
842 each section being (effectively) comprised of the concatenation of all of
843 the individual sections that exist in the version 1 format.
844 To keep the code simple we treat each of these concatenated pieces as a
845 section itself (a virtual section?). */
846 struct dwarf2_section_info abbrev;
847 struct dwarf2_section_info info;
848 struct dwarf2_section_info line;
849 struct dwarf2_section_info loc;
850 struct dwarf2_section_info loclists;
851 struct dwarf2_section_info macinfo;
852 struct dwarf2_section_info macro;
853 struct dwarf2_section_info rnglists;
854 struct dwarf2_section_info str_offsets;
855 struct dwarf2_section_info types;
856 };
857
858 /* These sections are what may appear in a virtual DWO file in DWP version 1.
859 A virtual DWO file is a DWO file as it appears in a DWP file. */
860
861 struct virtual_v1_dwo_sections
862 {
863 struct dwarf2_section_info abbrev;
864 struct dwarf2_section_info line;
865 struct dwarf2_section_info loc;
866 struct dwarf2_section_info macinfo;
867 struct dwarf2_section_info macro;
868 struct dwarf2_section_info str_offsets;
869 /* Each DWP hash table entry records one CU or one TU.
870 That is recorded here, and copied to dwo_unit.section. */
871 struct dwarf2_section_info info_or_types;
872 };
873
874 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
875 In version 2, the sections of the DWO files are concatenated together
876 and stored in one section of that name. Thus each ELF section contains
877 several "virtual" sections. */
878
879 struct virtual_v2_or_v5_dwo_sections
880 {
881 bfd_size_type abbrev_offset;
882 bfd_size_type abbrev_size;
883
884 bfd_size_type line_offset;
885 bfd_size_type line_size;
886
887 bfd_size_type loc_offset;
888 bfd_size_type loc_size;
889
890 bfd_size_type loclists_offset;
891 bfd_size_type loclists_size;
892
893 bfd_size_type macinfo_offset;
894 bfd_size_type macinfo_size;
895
896 bfd_size_type macro_offset;
897 bfd_size_type macro_size;
898
899 bfd_size_type rnglists_offset;
900 bfd_size_type rnglists_size;
901
902 bfd_size_type str_offsets_offset;
903 bfd_size_type str_offsets_size;
904
905 /* Each DWP hash table entry records one CU or one TU.
906 That is recorded here, and copied to dwo_unit.section. */
907 bfd_size_type info_or_types_offset;
908 bfd_size_type info_or_types_size;
909 };
910
911 /* Contents of DWP hash tables. */
912
913 struct dwp_hash_table
914 {
915 uint32_t version, nr_columns;
916 uint32_t nr_units, nr_slots;
917 const gdb_byte *hash_table, *unit_table;
918 union
919 {
920 struct
921 {
922 const gdb_byte *indices;
923 } v1;
924 struct
925 {
926 /* This is indexed by column number and gives the id of the section
927 in that column. */
928 #define MAX_NR_V2_DWO_SECTIONS \
929 (1 /* .debug_info or .debug_types */ \
930 + 1 /* .debug_abbrev */ \
931 + 1 /* .debug_line */ \
932 + 1 /* .debug_loc */ \
933 + 1 /* .debug_str_offsets */ \
934 + 1 /* .debug_macro or .debug_macinfo */)
935 int section_ids[MAX_NR_V2_DWO_SECTIONS];
936 const gdb_byte *offsets;
937 const gdb_byte *sizes;
938 } v2;
939 struct
940 {
941 /* This is indexed by column number and gives the id of the section
942 in that column. */
943 #define MAX_NR_V5_DWO_SECTIONS \
944 (1 /* .debug_info */ \
945 + 1 /* .debug_abbrev */ \
946 + 1 /* .debug_line */ \
947 + 1 /* .debug_loclists */ \
948 + 1 /* .debug_str_offsets */ \
949 + 1 /* .debug_macro */ \
950 + 1 /* .debug_rnglists */)
951 int section_ids[MAX_NR_V5_DWO_SECTIONS];
952 const gdb_byte *offsets;
953 const gdb_byte *sizes;
954 } v5;
955 } section_pool;
956 };
957
958 /* Data for one DWP file. */
959
960 struct dwp_file
961 {
962 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
963 : name (name_),
964 dbfd (std::move (abfd))
965 {
966 }
967
968 /* Name of the file. */
969 const char *name;
970
971 /* File format version. */
972 int version = 0;
973
974 /* The bfd. */
975 gdb_bfd_ref_ptr dbfd;
976
977 /* Section info for this file. */
978 struct dwp_sections sections {};
979
980 /* Table of CUs in the file. */
981 const struct dwp_hash_table *cus = nullptr;
982
983 /* Table of TUs in the file. */
984 const struct dwp_hash_table *tus = nullptr;
985
986 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
987 htab_up loaded_cus;
988 htab_up loaded_tus;
989
990 /* Table to map ELF section numbers to their sections.
991 This is only needed for the DWP V1 file format. */
992 unsigned int num_sections = 0;
993 asection **elf_sections = nullptr;
994 };
995
996 /* Struct used to pass misc. parameters to read_die_and_children, et
997 al. which are used for both .debug_info and .debug_types dies.
998 All parameters here are unchanging for the life of the call. This
999 struct exists to abstract away the constant parameters of die reading. */
1000
1001 struct die_reader_specs
1002 {
1003 /* The bfd of die_section. */
1004 bfd *abfd;
1005
1006 /* The CU of the DIE we are parsing. */
1007 struct dwarf2_cu *cu;
1008
1009 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1010 struct dwo_file *dwo_file;
1011
1012 /* The section the die comes from.
1013 This is either .debug_info or .debug_types, or the .dwo variants. */
1014 struct dwarf2_section_info *die_section;
1015
1016 /* die_section->buffer. */
1017 const gdb_byte *buffer;
1018
1019 /* The end of the buffer. */
1020 const gdb_byte *buffer_end;
1021
1022 /* The abbreviation table to use when reading the DIEs. */
1023 struct abbrev_table *abbrev_table;
1024 };
1025
1026 /* A subclass of die_reader_specs that holds storage and has complex
1027 constructor and destructor behavior. */
1028
1029 class cutu_reader : public die_reader_specs
1030 {
1031 public:
1032
1033 cutu_reader (dwarf2_per_cu_data *this_cu,
1034 dwarf2_per_objfile *per_objfile,
1035 struct abbrev_table *abbrev_table,
1036 dwarf2_cu *existing_cu,
1037 bool skip_partial);
1038
1039 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
1040 dwarf2_per_objfile *per_objfile,
1041 struct dwarf2_cu *parent_cu = nullptr,
1042 struct dwo_file *dwo_file = nullptr);
1043
1044 DISABLE_COPY_AND_ASSIGN (cutu_reader);
1045
1046 const gdb_byte *info_ptr = nullptr;
1047 struct die_info *comp_unit_die = nullptr;
1048 bool dummy_p = false;
1049
1050 /* Release the new CU, putting it on the chain. This cannot be done
1051 for dummy CUs. */
1052 void keep ();
1053
1054 private:
1055 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
1056 dwarf2_per_objfile *per_objfile,
1057 dwarf2_cu *existing_cu);
1058
1059 struct dwarf2_per_cu_data *m_this_cu;
1060 std::unique_ptr<dwarf2_cu> m_new_cu;
1061
1062 /* The ordinary abbreviation table. */
1063 abbrev_table_up m_abbrev_table_holder;
1064
1065 /* The DWO abbreviation table. */
1066 abbrev_table_up m_dwo_abbrev_table;
1067 };
1068
1069 /* When we construct a partial symbol table entry we only
1070 need this much information. */
1071 struct partial_die_info : public allocate_on_obstack
1072 {
1073 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
1074
1075 /* Disable assign but still keep copy ctor, which is needed
1076 load_partial_dies. */
1077 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1078
1079 /* Adjust the partial die before generating a symbol for it. This
1080 function may set the is_external flag or change the DIE's
1081 name. */
1082 void fixup (struct dwarf2_cu *cu);
1083
1084 /* Read a minimal amount of information into the minimal die
1085 structure. */
1086 const gdb_byte *read (const struct die_reader_specs *reader,
1087 const struct abbrev_info &abbrev,
1088 const gdb_byte *info_ptr);
1089
1090 /* Compute the name of this partial DIE. This memoizes the
1091 result, so it is safe to call multiple times. */
1092 const char *name (dwarf2_cu *cu);
1093
1094 /* Offset of this DIE. */
1095 const sect_offset sect_off;
1096
1097 /* DWARF-2 tag for this DIE. */
1098 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1099
1100 /* Assorted flags describing the data found in this DIE. */
1101 const unsigned int has_children : 1;
1102
1103 unsigned int is_external : 1;
1104 unsigned int is_declaration : 1;
1105 unsigned int has_type : 1;
1106 unsigned int has_specification : 1;
1107 unsigned int has_pc_info : 1;
1108 unsigned int may_be_inlined : 1;
1109
1110 /* This DIE has been marked DW_AT_main_subprogram. */
1111 unsigned int main_subprogram : 1;
1112
1113 /* Flag set if the SCOPE field of this structure has been
1114 computed. */
1115 unsigned int scope_set : 1;
1116
1117 /* Flag set if the DIE has a byte_size attribute. */
1118 unsigned int has_byte_size : 1;
1119
1120 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1121 unsigned int has_const_value : 1;
1122
1123 /* Flag set if any of the DIE's children are template arguments. */
1124 unsigned int has_template_arguments : 1;
1125
1126 /* Flag set if fixup has been called on this die. */
1127 unsigned int fixup_called : 1;
1128
1129 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1130 unsigned int is_dwz : 1;
1131
1132 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1133 unsigned int spec_is_dwz : 1;
1134
1135 unsigned int canonical_name : 1;
1136
1137 /* The name of this DIE. Normally the value of DW_AT_name, but
1138 sometimes a default name for unnamed DIEs. */
1139 const char *raw_name = nullptr;
1140
1141 /* The linkage name, if present. */
1142 const char *linkage_name = nullptr;
1143
1144 /* The scope to prepend to our children. This is generally
1145 allocated on the comp_unit_obstack, so will disappear
1146 when this compilation unit leaves the cache. */
1147 const char *scope = nullptr;
1148
1149 /* Some data associated with the partial DIE. The tag determines
1150 which field is live. */
1151 union
1152 {
1153 /* The location description associated with this DIE, if any. */
1154 struct dwarf_block *locdesc;
1155 /* The offset of an import, for DW_TAG_imported_unit. */
1156 sect_offset sect_off;
1157 } d {};
1158
1159 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1160 CORE_ADDR lowpc = 0;
1161 CORE_ADDR highpc = 0;
1162
1163 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1164 DW_AT_sibling, if any. */
1165 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1166 could return DW_AT_sibling values to its caller load_partial_dies. */
1167 const gdb_byte *sibling = nullptr;
1168
1169 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1170 DW_AT_specification (or DW_AT_abstract_origin or
1171 DW_AT_extension). */
1172 sect_offset spec_offset {};
1173
1174 /* Pointers to this DIE's parent, first child, and next sibling,
1175 if any. */
1176 struct partial_die_info *die_parent = nullptr;
1177 struct partial_die_info *die_child = nullptr;
1178 struct partial_die_info *die_sibling = nullptr;
1179
1180 friend struct partial_die_info *
1181 dwarf2_cu::find_partial_die (sect_offset sect_off);
1182
1183 private:
1184 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1185 partial_die_info (sect_offset sect_off)
1186 : partial_die_info (sect_off, DW_TAG_padding, 0)
1187 {
1188 }
1189
1190 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1191 int has_children_)
1192 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1193 {
1194 is_external = 0;
1195 is_declaration = 0;
1196 has_type = 0;
1197 has_specification = 0;
1198 has_pc_info = 0;
1199 may_be_inlined = 0;
1200 main_subprogram = 0;
1201 scope_set = 0;
1202 has_byte_size = 0;
1203 has_const_value = 0;
1204 has_template_arguments = 0;
1205 fixup_called = 0;
1206 is_dwz = 0;
1207 spec_is_dwz = 0;
1208 canonical_name = 0;
1209 }
1210 };
1211
1212 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1213 but this would require a corresponding change in unpack_field_as_long
1214 and friends. */
1215 static int bits_per_byte = 8;
1216
1217 struct variant_part_builder;
1218
1219 /* When reading a variant, we track a bit more information about the
1220 field, and store it in an object of this type. */
1221
1222 struct variant_field
1223 {
1224 int first_field = -1;
1225 int last_field = -1;
1226
1227 /* A variant can contain other variant parts. */
1228 std::vector<variant_part_builder> variant_parts;
1229
1230 /* If we see a DW_TAG_variant, then this will be set if this is the
1231 default branch. */
1232 bool default_branch = false;
1233 /* If we see a DW_AT_discr_value, then this will be the discriminant
1234 value. */
1235 ULONGEST discriminant_value = 0;
1236 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1237 data. */
1238 struct dwarf_block *discr_list_data = nullptr;
1239 };
1240
1241 /* This represents a DW_TAG_variant_part. */
1242
1243 struct variant_part_builder
1244 {
1245 /* The offset of the discriminant field. */
1246 sect_offset discriminant_offset {};
1247
1248 /* Variants that are direct children of this variant part. */
1249 std::vector<variant_field> variants;
1250
1251 /* True if we're currently reading a variant. */
1252 bool processing_variant = false;
1253 };
1254
1255 struct nextfield
1256 {
1257 int accessibility = 0;
1258 int virtuality = 0;
1259 /* Variant parts need to find the discriminant, which is a DIE
1260 reference. We track the section offset of each field to make
1261 this link. */
1262 sect_offset offset;
1263 struct field field {};
1264 };
1265
1266 struct fnfieldlist
1267 {
1268 const char *name = nullptr;
1269 std::vector<struct fn_field> fnfields;
1270 };
1271
1272 /* The routines that read and process dies for a C struct or C++ class
1273 pass lists of data member fields and lists of member function fields
1274 in an instance of a field_info structure, as defined below. */
1275 struct field_info
1276 {
1277 /* List of data member and baseclasses fields. */
1278 std::vector<struct nextfield> fields;
1279 std::vector<struct nextfield> baseclasses;
1280
1281 /* Set if the accessibility of one of the fields is not public. */
1282 bool non_public_fields = false;
1283
1284 /* Member function fieldlist array, contains name of possibly overloaded
1285 member function, number of overloaded member functions and a pointer
1286 to the head of the member function field chain. */
1287 std::vector<struct fnfieldlist> fnfieldlists;
1288
1289 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1290 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1291 std::vector<struct decl_field> typedef_field_list;
1292
1293 /* Nested types defined by this class and the number of elements in this
1294 list. */
1295 std::vector<struct decl_field> nested_types_list;
1296
1297 /* If non-null, this is the variant part we are currently
1298 reading. */
1299 variant_part_builder *current_variant_part = nullptr;
1300 /* This holds all the top-level variant parts attached to the type
1301 we're reading. */
1302 std::vector<variant_part_builder> variant_parts;
1303
1304 /* Return the total number of fields (including baseclasses). */
1305 int nfields () const
1306 {
1307 return fields.size () + baseclasses.size ();
1308 }
1309 };
1310
1311 /* Loaded secondary compilation units are kept in memory until they
1312 have not been referenced for the processing of this many
1313 compilation units. Set this to zero to disable caching. Cache
1314 sizes of up to at least twenty will improve startup time for
1315 typical inter-CU-reference binaries, at an obvious memory cost. */
1316 static int dwarf_max_cache_age = 5;
1317 static void
1318 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1319 struct cmd_list_element *c, const char *value)
1320 {
1321 fprintf_filtered (file, _("The upper bound on the age of cached "
1322 "DWARF compilation units is %s.\n"),
1323 value);
1324 }
1325 \f
1326 /* local function prototypes */
1327
1328 static void dwarf2_find_base_address (struct die_info *die,
1329 struct dwarf2_cu *cu);
1330
1331 static dwarf2_psymtab *create_partial_symtab
1332 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1333 const char *name);
1334
1335 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1336 const gdb_byte *info_ptr,
1337 struct die_info *type_unit_die);
1338
1339 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1340
1341 static void scan_partial_symbols (struct partial_die_info *,
1342 CORE_ADDR *, CORE_ADDR *,
1343 int, struct dwarf2_cu *);
1344
1345 static void add_partial_symbol (struct partial_die_info *,
1346 struct dwarf2_cu *);
1347
1348 static void add_partial_namespace (struct partial_die_info *pdi,
1349 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1350 int set_addrmap, struct dwarf2_cu *cu);
1351
1352 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1353 CORE_ADDR *highpc, int set_addrmap,
1354 struct dwarf2_cu *cu);
1355
1356 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1357 struct dwarf2_cu *cu);
1358
1359 static void add_partial_subprogram (struct partial_die_info *pdi,
1360 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1361 int need_pc, struct dwarf2_cu *cu);
1362
1363 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1364
1365 static struct partial_die_info *load_partial_dies
1366 (const struct die_reader_specs *, const gdb_byte *, int);
1367
1368 /* A pair of partial_die_info and compilation unit. */
1369 struct cu_partial_die_info
1370 {
1371 /* The compilation unit of the partial_die_info. */
1372 struct dwarf2_cu *cu;
1373 /* A partial_die_info. */
1374 struct partial_die_info *pdi;
1375
1376 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1377 : cu (cu),
1378 pdi (pdi)
1379 { /* Nothing. */ }
1380
1381 private:
1382 cu_partial_die_info () = delete;
1383 };
1384
1385 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1386 struct dwarf2_cu *);
1387
1388 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1389 struct attribute *,
1390 const struct attr_abbrev *,
1391 const gdb_byte *);
1392
1393 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1394 struct attribute *attr, dwarf_tag tag);
1395
1396 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1397
1398 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1399 dwarf2_section_info *, sect_offset);
1400
1401 static const char *read_indirect_string
1402 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1403 const struct comp_unit_head *, unsigned int *);
1404
1405 static const char *read_indirect_string_at_offset
1406 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1407
1408 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1409 const gdb_byte *,
1410 unsigned int *);
1411
1412 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1413 ULONGEST str_index);
1414
1415 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1416 ULONGEST str_index);
1417
1418 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1419
1420 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1421 struct dwarf2_cu *);
1422
1423 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1424 struct dwarf2_cu *cu);
1425
1426 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1427
1428 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1429 struct dwarf2_cu *cu);
1430
1431 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1432
1433 static struct die_info *die_specification (struct die_info *die,
1434 struct dwarf2_cu **);
1435
1436 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1437 struct dwarf2_cu *cu);
1438
1439 static void dwarf_decode_lines (struct line_header *, const char *,
1440 struct dwarf2_cu *, dwarf2_psymtab *,
1441 CORE_ADDR, int decode_mapping);
1442
1443 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1444 const char *);
1445
1446 static struct symbol *new_symbol (struct die_info *, struct type *,
1447 struct dwarf2_cu *, struct symbol * = NULL);
1448
1449 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1450 struct dwarf2_cu *);
1451
1452 static void dwarf2_const_value_attr (const struct attribute *attr,
1453 struct type *type,
1454 const char *name,
1455 struct obstack *obstack,
1456 struct dwarf2_cu *cu, LONGEST *value,
1457 const gdb_byte **bytes,
1458 struct dwarf2_locexpr_baton **baton);
1459
1460 static struct type *read_subrange_index_type (struct die_info *die,
1461 struct dwarf2_cu *cu);
1462
1463 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1464
1465 static int need_gnat_info (struct dwarf2_cu *);
1466
1467 static struct type *die_descriptive_type (struct die_info *,
1468 struct dwarf2_cu *);
1469
1470 static void set_descriptive_type (struct type *, struct die_info *,
1471 struct dwarf2_cu *);
1472
1473 static struct type *die_containing_type (struct die_info *,
1474 struct dwarf2_cu *);
1475
1476 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1477 struct dwarf2_cu *);
1478
1479 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1480
1481 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1482
1483 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1484
1485 static char *typename_concat (struct obstack *obs, const char *prefix,
1486 const char *suffix, int physname,
1487 struct dwarf2_cu *cu);
1488
1489 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1490
1491 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1492
1493 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1494
1495 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1496
1497 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1498
1499 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1500
1501 /* Return the .debug_loclists section to use for cu. */
1502 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1503
1504 /* Return the .debug_rnglists section to use for cu. */
1505 static struct dwarf2_section_info *cu_debug_rnglists_section
1506 (struct dwarf2_cu *cu, dwarf_tag tag);
1507
1508 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1509 values. Keep the items ordered with increasing constraints compliance. */
1510 enum pc_bounds_kind
1511 {
1512 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1513 PC_BOUNDS_NOT_PRESENT,
1514
1515 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1516 were present but they do not form a valid range of PC addresses. */
1517 PC_BOUNDS_INVALID,
1518
1519 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1520 PC_BOUNDS_RANGES,
1521
1522 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1523 PC_BOUNDS_HIGH_LOW,
1524 };
1525
1526 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1527 CORE_ADDR *, CORE_ADDR *,
1528 struct dwarf2_cu *,
1529 dwarf2_psymtab *);
1530
1531 static void get_scope_pc_bounds (struct die_info *,
1532 CORE_ADDR *, CORE_ADDR *,
1533 struct dwarf2_cu *);
1534
1535 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1536 CORE_ADDR, struct dwarf2_cu *);
1537
1538 static void dwarf2_add_field (struct field_info *, struct die_info *,
1539 struct dwarf2_cu *);
1540
1541 static void dwarf2_attach_fields_to_type (struct field_info *,
1542 struct type *, struct dwarf2_cu *);
1543
1544 static void dwarf2_add_member_fn (struct field_info *,
1545 struct die_info *, struct type *,
1546 struct dwarf2_cu *);
1547
1548 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1549 struct type *,
1550 struct dwarf2_cu *);
1551
1552 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1553
1554 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1555
1556 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1557
1558 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1559
1560 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1561
1562 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1563
1564 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1565
1566 static struct type *read_module_type (struct die_info *die,
1567 struct dwarf2_cu *cu);
1568
1569 static const char *namespace_name (struct die_info *die,
1570 int *is_anonymous, struct dwarf2_cu *);
1571
1572 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1573
1574 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1575 bool * = nullptr);
1576
1577 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1578 struct dwarf2_cu *);
1579
1580 static struct die_info *read_die_and_siblings_1
1581 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1582 struct die_info *);
1583
1584 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1585 const gdb_byte *info_ptr,
1586 const gdb_byte **new_info_ptr,
1587 struct die_info *parent);
1588
1589 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1590 struct die_info **, const gdb_byte *,
1591 int);
1592
1593 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1594 struct die_info **, const gdb_byte *);
1595
1596 static void process_die (struct die_info *, struct dwarf2_cu *);
1597
1598 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1599 struct objfile *);
1600
1601 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1602
1603 static const char *dwarf2_full_name (const char *name,
1604 struct die_info *die,
1605 struct dwarf2_cu *cu);
1606
1607 static const char *dwarf2_physname (const char *name, struct die_info *die,
1608 struct dwarf2_cu *cu);
1609
1610 static struct die_info *dwarf2_extension (struct die_info *die,
1611 struct dwarf2_cu **);
1612
1613 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1614
1615 static void dump_die_for_error (struct die_info *);
1616
1617 static void dump_die_1 (struct ui_file *, int level, int max_level,
1618 struct die_info *);
1619
1620 /*static*/ void dump_die (struct die_info *, int max_level);
1621
1622 static void store_in_ref_table (struct die_info *,
1623 struct dwarf2_cu *);
1624
1625 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1626 const struct attribute *,
1627 struct dwarf2_cu **);
1628
1629 static struct die_info *follow_die_ref (struct die_info *,
1630 const struct attribute *,
1631 struct dwarf2_cu **);
1632
1633 static struct die_info *follow_die_sig (struct die_info *,
1634 const struct attribute *,
1635 struct dwarf2_cu **);
1636
1637 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1638 struct dwarf2_cu *);
1639
1640 static struct type *get_DW_AT_signature_type (struct die_info *,
1641 const struct attribute *,
1642 struct dwarf2_cu *);
1643
1644 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1645 dwarf2_per_objfile *per_objfile);
1646
1647 static void read_signatured_type (signatured_type *sig_type,
1648 dwarf2_per_objfile *per_objfile);
1649
1650 static int attr_to_dynamic_prop (const struct attribute *attr,
1651 struct die_info *die, struct dwarf2_cu *cu,
1652 struct dynamic_prop *prop, struct type *type);
1653
1654 /* memory allocation interface */
1655
1656 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1657
1658 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1659
1660 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1661
1662 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1663 struct dwarf2_loclist_baton *baton,
1664 const struct attribute *attr);
1665
1666 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1667 struct symbol *sym,
1668 struct dwarf2_cu *cu,
1669 int is_block);
1670
1671 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1672 const gdb_byte *info_ptr,
1673 const struct abbrev_info *abbrev);
1674
1675 static hashval_t partial_die_hash (const void *item);
1676
1677 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1678
1679 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1680 (sect_offset sect_off, unsigned int offset_in_dwz,
1681 dwarf2_per_objfile *per_objfile);
1682
1683 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1684 struct die_info *comp_unit_die,
1685 enum language pretend_language);
1686
1687 static struct type *set_die_type (struct die_info *, struct type *,
1688 struct dwarf2_cu *, bool = false);
1689
1690 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1691
1692 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1693 dwarf2_per_objfile *per_objfile,
1694 dwarf2_cu *existing_cu,
1695 bool skip_partial,
1696 enum language pretend_language);
1697
1698 static void process_full_comp_unit (dwarf2_cu *cu,
1699 enum language pretend_language);
1700
1701 static void process_full_type_unit (dwarf2_cu *cu,
1702 enum language pretend_language);
1703
1704 static void dwarf2_add_dependence (struct dwarf2_cu *,
1705 struct dwarf2_per_cu_data *);
1706
1707 static void dwarf2_mark (struct dwarf2_cu *);
1708
1709 static struct type *get_die_type_at_offset (sect_offset,
1710 dwarf2_per_cu_data *per_cu,
1711 dwarf2_per_objfile *per_objfile);
1712
1713 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1714
1715 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1716 dwarf2_per_objfile *per_objfile,
1717 enum language pretend_language);
1718
1719 static void process_queue (dwarf2_per_objfile *per_objfile);
1720
1721 /* Class, the destructor of which frees all allocated queue entries. This
1722 will only have work to do if an error was thrown while processing the
1723 dwarf. If no error was thrown then the queue entries should have all
1724 been processed, and freed, as we went along. */
1725
1726 class dwarf2_queue_guard
1727 {
1728 public:
1729 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1730 : m_per_objfile (per_objfile)
1731 {
1732 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1733
1734 m_per_objfile->per_bfd->queue.emplace ();
1735 }
1736
1737 /* Free any entries remaining on the queue. There should only be
1738 entries left if we hit an error while processing the dwarf. */
1739 ~dwarf2_queue_guard ()
1740 {
1741 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1742
1743 m_per_objfile->per_bfd->queue.reset ();
1744 }
1745
1746 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1747
1748 private:
1749 dwarf2_per_objfile *m_per_objfile;
1750 };
1751
1752 dwarf2_queue_item::~dwarf2_queue_item ()
1753 {
1754 /* Anything still marked queued is likely to be in an
1755 inconsistent state, so discard it. */
1756 if (per_cu->queued)
1757 {
1758 per_objfile->remove_cu (per_cu);
1759 per_cu->queued = 0;
1760 }
1761 }
1762
1763 /* The return type of find_file_and_directory. Note, the enclosed
1764 string pointers are only valid while this object is valid. */
1765
1766 struct file_and_directory
1767 {
1768 /* The filename. This is never NULL. */
1769 const char *name;
1770
1771 /* The compilation directory. NULL if not known. If we needed to
1772 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1773 points directly to the DW_AT_comp_dir string attribute owned by
1774 the obstack that owns the DIE. */
1775 const char *comp_dir;
1776
1777 /* If we needed to build a new string for comp_dir, this is what
1778 owns the storage. */
1779 std::string comp_dir_storage;
1780 };
1781
1782 static file_and_directory find_file_and_directory (struct die_info *die,
1783 struct dwarf2_cu *cu);
1784
1785 static htab_up allocate_signatured_type_table ();
1786
1787 static htab_up allocate_dwo_unit_table ();
1788
1789 static struct dwo_unit *lookup_dwo_unit_in_dwp
1790 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1791 const char *comp_dir, ULONGEST signature, int is_debug_types);
1792
1793 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1794
1795 static struct dwo_unit *lookup_dwo_comp_unit
1796 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1797 ULONGEST signature);
1798
1799 static struct dwo_unit *lookup_dwo_type_unit
1800 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1801
1802 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1803
1804 /* A unique pointer to a dwo_file. */
1805
1806 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1807
1808 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1809
1810 static void check_producer (struct dwarf2_cu *cu);
1811
1812 static void free_line_header_voidp (void *arg);
1813 \f
1814 /* Various complaints about symbol reading that don't abort the process. */
1815
1816 static void
1817 dwarf2_debug_line_missing_file_complaint (void)
1818 {
1819 complaint (_(".debug_line section has line data without a file"));
1820 }
1821
1822 static void
1823 dwarf2_debug_line_missing_end_sequence_complaint (void)
1824 {
1825 complaint (_(".debug_line section has line "
1826 "program sequence without an end"));
1827 }
1828
1829 static void
1830 dwarf2_complex_location_expr_complaint (void)
1831 {
1832 complaint (_("location expression too complex"));
1833 }
1834
1835 static void
1836 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1837 int arg3)
1838 {
1839 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1840 arg1, arg2, arg3);
1841 }
1842
1843 static void
1844 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1845 {
1846 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1847 arg1, arg2);
1848 }
1849
1850 /* Hash function for line_header_hash. */
1851
1852 static hashval_t
1853 line_header_hash (const struct line_header *ofs)
1854 {
1855 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1856 }
1857
1858 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1859
1860 static hashval_t
1861 line_header_hash_voidp (const void *item)
1862 {
1863 const struct line_header *ofs = (const struct line_header *) item;
1864
1865 return line_header_hash (ofs);
1866 }
1867
1868 /* Equality function for line_header_hash. */
1869
1870 static int
1871 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1872 {
1873 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1874 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1875
1876 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1877 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1878 }
1879
1880 \f
1881
1882 /* See declaration. */
1883
1884 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1885 bool can_copy_)
1886 : obfd (obfd),
1887 can_copy (can_copy_)
1888 {
1889 if (names == NULL)
1890 names = &dwarf2_elf_names;
1891
1892 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1893 locate_sections (obfd, sec, *names);
1894 }
1895
1896 dwarf2_per_bfd::~dwarf2_per_bfd ()
1897 {
1898 for (auto &per_cu : all_comp_units)
1899 per_cu->imported_symtabs_free ();
1900
1901 /* Everything else should be on this->obstack. */
1902 }
1903
1904 /* See read.h. */
1905
1906 void
1907 dwarf2_per_objfile::remove_all_cus ()
1908 {
1909 gdb_assert (!this->per_bfd->queue.has_value ());
1910
1911 for (auto pair : m_dwarf2_cus)
1912 delete pair.second;
1913
1914 m_dwarf2_cus.clear ();
1915 }
1916
1917 /* A helper class that calls free_cached_comp_units on
1918 destruction. */
1919
1920 class free_cached_comp_units
1921 {
1922 public:
1923
1924 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1925 : m_per_objfile (per_objfile)
1926 {
1927 }
1928
1929 ~free_cached_comp_units ()
1930 {
1931 m_per_objfile->remove_all_cus ();
1932 }
1933
1934 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1935
1936 private:
1937
1938 dwarf2_per_objfile *m_per_objfile;
1939 };
1940
1941 /* See read.h. */
1942
1943 bool
1944 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1945 {
1946 gdb_assert (per_cu->index < this->m_symtabs.size ());
1947
1948 return this->m_symtabs[per_cu->index] != nullptr;
1949 }
1950
1951 /* See read.h. */
1952
1953 compunit_symtab *
1954 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1955 {
1956 gdb_assert (per_cu->index < this->m_symtabs.size ());
1957
1958 return this->m_symtabs[per_cu->index];
1959 }
1960
1961 /* See read.h. */
1962
1963 void
1964 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1965 compunit_symtab *symtab)
1966 {
1967 gdb_assert (per_cu->index < this->m_symtabs.size ());
1968 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1969
1970 this->m_symtabs[per_cu->index] = symtab;
1971 }
1972
1973 /* Try to locate the sections we need for DWARF 2 debugging
1974 information and return true if we have enough to do something.
1975 NAMES points to the dwarf2 section names, or is NULL if the standard
1976 ELF names are used. CAN_COPY is true for formats where symbol
1977 interposition is possible and so symbol values must follow copy
1978 relocation rules. */
1979
1980 int
1981 dwarf2_has_info (struct objfile *objfile,
1982 const struct dwarf2_debug_sections *names,
1983 bool can_copy)
1984 {
1985 if (objfile->flags & OBJF_READNEVER)
1986 return 0;
1987
1988 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1989
1990 if (per_objfile == NULL)
1991 {
1992 dwarf2_per_bfd *per_bfd;
1993
1994 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1995 BFD doesn't require relocations.
1996
1997 We don't share with objfiles for which -readnow was requested,
1998 because it would complicate things when loading the same BFD with
1999 -readnow and then without -readnow. */
2000 if (!gdb_bfd_requires_relocations (objfile->obfd)
2001 && (objfile->flags & OBJF_READNOW) == 0)
2002 {
2003 /* See if one has been created for this BFD yet. */
2004 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
2005
2006 if (per_bfd == nullptr)
2007 {
2008 /* No, create it now. */
2009 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
2010 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
2011 }
2012 }
2013 else
2014 {
2015 /* No sharing possible, create one specifically for this objfile. */
2016 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
2017 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
2018 }
2019
2020 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
2021 }
2022
2023 return (!per_objfile->per_bfd->info.is_virtual
2024 && per_objfile->per_bfd->info.s.section != NULL
2025 && !per_objfile->per_bfd->abbrev.is_virtual
2026 && per_objfile->per_bfd->abbrev.s.section != NULL);
2027 }
2028
2029 /* See declaration. */
2030
2031 void
2032 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
2033 const dwarf2_debug_sections &names)
2034 {
2035 flagword aflag = bfd_section_flags (sectp);
2036
2037 if ((aflag & SEC_HAS_CONTENTS) == 0)
2038 {
2039 }
2040 else if (elf_section_data (sectp)->this_hdr.sh_size
2041 > bfd_get_file_size (abfd))
2042 {
2043 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2044 warning (_("Discarding section %s which has a section size (%s"
2045 ") larger than the file size [in module %s]"),
2046 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2047 bfd_get_filename (abfd));
2048 }
2049 else if (names.info.matches (sectp->name))
2050 {
2051 this->info.s.section = sectp;
2052 this->info.size = bfd_section_size (sectp);
2053 }
2054 else if (names.abbrev.matches (sectp->name))
2055 {
2056 this->abbrev.s.section = sectp;
2057 this->abbrev.size = bfd_section_size (sectp);
2058 }
2059 else if (names.line.matches (sectp->name))
2060 {
2061 this->line.s.section = sectp;
2062 this->line.size = bfd_section_size (sectp);
2063 }
2064 else if (names.loc.matches (sectp->name))
2065 {
2066 this->loc.s.section = sectp;
2067 this->loc.size = bfd_section_size (sectp);
2068 }
2069 else if (names.loclists.matches (sectp->name))
2070 {
2071 this->loclists.s.section = sectp;
2072 this->loclists.size = bfd_section_size (sectp);
2073 }
2074 else if (names.macinfo.matches (sectp->name))
2075 {
2076 this->macinfo.s.section = sectp;
2077 this->macinfo.size = bfd_section_size (sectp);
2078 }
2079 else if (names.macro.matches (sectp->name))
2080 {
2081 this->macro.s.section = sectp;
2082 this->macro.size = bfd_section_size (sectp);
2083 }
2084 else if (names.str.matches (sectp->name))
2085 {
2086 this->str.s.section = sectp;
2087 this->str.size = bfd_section_size (sectp);
2088 }
2089 else if (names.str_offsets.matches (sectp->name))
2090 {
2091 this->str_offsets.s.section = sectp;
2092 this->str_offsets.size = bfd_section_size (sectp);
2093 }
2094 else if (names.line_str.matches (sectp->name))
2095 {
2096 this->line_str.s.section = sectp;
2097 this->line_str.size = bfd_section_size (sectp);
2098 }
2099 else if (names.addr.matches (sectp->name))
2100 {
2101 this->addr.s.section = sectp;
2102 this->addr.size = bfd_section_size (sectp);
2103 }
2104 else if (names.frame.matches (sectp->name))
2105 {
2106 this->frame.s.section = sectp;
2107 this->frame.size = bfd_section_size (sectp);
2108 }
2109 else if (names.eh_frame.matches (sectp->name))
2110 {
2111 this->eh_frame.s.section = sectp;
2112 this->eh_frame.size = bfd_section_size (sectp);
2113 }
2114 else if (names.ranges.matches (sectp->name))
2115 {
2116 this->ranges.s.section = sectp;
2117 this->ranges.size = bfd_section_size (sectp);
2118 }
2119 else if (names.rnglists.matches (sectp->name))
2120 {
2121 this->rnglists.s.section = sectp;
2122 this->rnglists.size = bfd_section_size (sectp);
2123 }
2124 else if (names.types.matches (sectp->name))
2125 {
2126 struct dwarf2_section_info type_section;
2127
2128 memset (&type_section, 0, sizeof (type_section));
2129 type_section.s.section = sectp;
2130 type_section.size = bfd_section_size (sectp);
2131
2132 this->types.push_back (type_section);
2133 }
2134 else if (names.gdb_index.matches (sectp->name))
2135 {
2136 this->gdb_index.s.section = sectp;
2137 this->gdb_index.size = bfd_section_size (sectp);
2138 }
2139 else if (names.debug_names.matches (sectp->name))
2140 {
2141 this->debug_names.s.section = sectp;
2142 this->debug_names.size = bfd_section_size (sectp);
2143 }
2144 else if (names.debug_aranges.matches (sectp->name))
2145 {
2146 this->debug_aranges.s.section = sectp;
2147 this->debug_aranges.size = bfd_section_size (sectp);
2148 }
2149
2150 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2151 && bfd_section_vma (sectp) == 0)
2152 this->has_section_at_zero = true;
2153 }
2154
2155 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2156 SECTION_NAME. */
2157
2158 void
2159 dwarf2_get_section_info (struct objfile *objfile,
2160 enum dwarf2_section_enum sect,
2161 asection **sectp, const gdb_byte **bufp,
2162 bfd_size_type *sizep)
2163 {
2164 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2165 struct dwarf2_section_info *info;
2166
2167 /* We may see an objfile without any DWARF, in which case we just
2168 return nothing. */
2169 if (per_objfile == NULL)
2170 {
2171 *sectp = NULL;
2172 *bufp = NULL;
2173 *sizep = 0;
2174 return;
2175 }
2176 switch (sect)
2177 {
2178 case DWARF2_DEBUG_FRAME:
2179 info = &per_objfile->per_bfd->frame;
2180 break;
2181 case DWARF2_EH_FRAME:
2182 info = &per_objfile->per_bfd->eh_frame;
2183 break;
2184 default:
2185 gdb_assert_not_reached ("unexpected section");
2186 }
2187
2188 info->read (objfile);
2189
2190 *sectp = info->get_bfd_section ();
2191 *bufp = info->buffer;
2192 *sizep = info->size;
2193 }
2194
2195 \f
2196 /* DWARF quick_symbol_functions support. */
2197
2198 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2199 unique line tables, so we maintain a separate table of all .debug_line
2200 derived entries to support the sharing.
2201 All the quick functions need is the list of file names. We discard the
2202 line_header when we're done and don't need to record it here. */
2203 struct quick_file_names
2204 {
2205 /* The data used to construct the hash key. */
2206 struct stmt_list_hash hash;
2207
2208 /* The number of entries in file_names, real_names. */
2209 unsigned int num_file_names;
2210
2211 /* The file names from the line table, after being run through
2212 file_full_name. */
2213 const char **file_names;
2214
2215 /* The file names from the line table after being run through
2216 gdb_realpath. These are computed lazily. */
2217 const char **real_names;
2218 };
2219
2220 /* When using the index (and thus not using psymtabs), each CU has an
2221 object of this type. This is used to hold information needed by
2222 the various "quick" methods. */
2223 struct dwarf2_per_cu_quick_data
2224 {
2225 /* The file table. This can be NULL if there was no file table
2226 or it's currently not read in.
2227 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2228 struct quick_file_names *file_names;
2229
2230 /* A temporary mark bit used when iterating over all CUs in
2231 expand_symtabs_matching. */
2232 unsigned int mark : 1;
2233
2234 /* True if we've tried to read the file table and found there isn't one.
2235 There will be no point in trying to read it again next time. */
2236 unsigned int no_file_data : 1;
2237 };
2238
2239 /* A subclass of psymbol_functions that arranges to read the DWARF
2240 partial symbols when needed. */
2241 struct lazy_dwarf_reader : public psymbol_functions
2242 {
2243 using psymbol_functions::psymbol_functions;
2244
2245 bool can_lazily_read_symbols () override
2246 {
2247 return true;
2248 }
2249
2250 void read_partial_symbols (struct objfile *objfile) override
2251 {
2252 if (dwarf2_has_info (objfile, nullptr))
2253 dwarf2_build_psymtabs (objfile, this);
2254 }
2255 };
2256
2257 static quick_symbol_functions_up
2258 make_lazy_dwarf_reader ()
2259 {
2260 return quick_symbol_functions_up (new lazy_dwarf_reader);
2261 }
2262
2263 struct dwarf2_base_index_functions : public quick_symbol_functions
2264 {
2265 bool has_symbols (struct objfile *objfile) override;
2266
2267 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2268
2269 void forget_cached_source_info (struct objfile *objfile) override;
2270
2271 enum language lookup_global_symbol_language (struct objfile *objfile,
2272 const char *name,
2273 domain_enum domain,
2274 bool *symbol_found_p) override
2275 {
2276 *symbol_found_p = false;
2277 return language_unknown;
2278 }
2279
2280 void print_stats (struct objfile *objfile, bool print_bcache) override;
2281
2282 void expand_all_symtabs (struct objfile *objfile) override;
2283
2284 struct compunit_symtab *find_pc_sect_compunit_symtab
2285 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2286 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2287
2288 struct compunit_symtab *find_compunit_symtab_by_address
2289 (struct objfile *objfile, CORE_ADDR address) override
2290 {
2291 return nullptr;
2292 }
2293
2294 void map_symbol_filenames (struct objfile *objfile,
2295 gdb::function_view<symbol_filename_ftype> fun,
2296 bool need_fullname) override;
2297 };
2298
2299 struct dwarf2_gdb_index : public dwarf2_base_index_functions
2300 {
2301 void dump (struct objfile *objfile) override;
2302
2303 void expand_matching_symbols
2304 (struct objfile *,
2305 const lookup_name_info &lookup_name,
2306 domain_enum domain,
2307 int global,
2308 symbol_compare_ftype *ordered_compare) override;
2309
2310 bool expand_symtabs_matching
2311 (struct objfile *objfile,
2312 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2313 const lookup_name_info *lookup_name,
2314 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2315 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2316 block_search_flags search_flags,
2317 domain_enum domain,
2318 enum search_domain kind) override;
2319 };
2320
2321 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2322 {
2323 void dump (struct objfile *objfile) override;
2324
2325 void expand_matching_symbols
2326 (struct objfile *,
2327 const lookup_name_info &lookup_name,
2328 domain_enum domain,
2329 int global,
2330 symbol_compare_ftype *ordered_compare) override;
2331
2332 bool expand_symtabs_matching
2333 (struct objfile *objfile,
2334 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2335 const lookup_name_info *lookup_name,
2336 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2337 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2338 block_search_flags search_flags,
2339 domain_enum domain,
2340 enum search_domain kind) override;
2341 };
2342
2343 static quick_symbol_functions_up
2344 make_dwarf_gdb_index ()
2345 {
2346 return quick_symbol_functions_up (new dwarf2_gdb_index);
2347 }
2348
2349 static quick_symbol_functions_up
2350 make_dwarf_debug_names ()
2351 {
2352 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2353 }
2354
2355 /* Utility hash function for a stmt_list_hash. */
2356
2357 static hashval_t
2358 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2359 {
2360 hashval_t v = 0;
2361
2362 if (stmt_list_hash->dwo_unit != NULL)
2363 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2364 v += to_underlying (stmt_list_hash->line_sect_off);
2365 return v;
2366 }
2367
2368 /* Utility equality function for a stmt_list_hash. */
2369
2370 static int
2371 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2372 const struct stmt_list_hash *rhs)
2373 {
2374 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2375 return 0;
2376 if (lhs->dwo_unit != NULL
2377 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2378 return 0;
2379
2380 return lhs->line_sect_off == rhs->line_sect_off;
2381 }
2382
2383 /* Hash function for a quick_file_names. */
2384
2385 static hashval_t
2386 hash_file_name_entry (const void *e)
2387 {
2388 const struct quick_file_names *file_data
2389 = (const struct quick_file_names *) e;
2390
2391 return hash_stmt_list_entry (&file_data->hash);
2392 }
2393
2394 /* Equality function for a quick_file_names. */
2395
2396 static int
2397 eq_file_name_entry (const void *a, const void *b)
2398 {
2399 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2400 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2401
2402 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2403 }
2404
2405 /* Delete function for a quick_file_names. */
2406
2407 static void
2408 delete_file_name_entry (void *e)
2409 {
2410 struct quick_file_names *file_data = (struct quick_file_names *) e;
2411 int i;
2412
2413 for (i = 0; i < file_data->num_file_names; ++i)
2414 {
2415 xfree ((void*) file_data->file_names[i]);
2416 if (file_data->real_names)
2417 xfree ((void*) file_data->real_names[i]);
2418 }
2419
2420 /* The space for the struct itself lives on the obstack, so we don't
2421 free it here. */
2422 }
2423
2424 /* Create a quick_file_names hash table. */
2425
2426 static htab_up
2427 create_quick_file_names_table (unsigned int nr_initial_entries)
2428 {
2429 return htab_up (htab_create_alloc (nr_initial_entries,
2430 hash_file_name_entry, eq_file_name_entry,
2431 delete_file_name_entry, xcalloc, xfree));
2432 }
2433
2434 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2435 function is unrelated to symtabs, symtab would have to be created afterwards.
2436 You should call age_cached_comp_units after processing the CU. */
2437
2438 static dwarf2_cu *
2439 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2440 bool skip_partial)
2441 {
2442 if (per_cu->is_debug_types)
2443 load_full_type_unit (per_cu, per_objfile);
2444 else
2445 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2446 skip_partial, language_minimal);
2447
2448 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2449 if (cu == nullptr)
2450 return nullptr; /* Dummy CU. */
2451
2452 dwarf2_find_base_address (cu->dies, cu);
2453
2454 return cu;
2455 }
2456
2457 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2458
2459 static void
2460 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2461 dwarf2_per_objfile *per_objfile, bool skip_partial)
2462 {
2463 /* Skip type_unit_groups, reading the type units they contain
2464 is handled elsewhere. */
2465 if (per_cu->type_unit_group_p ())
2466 return;
2467
2468 {
2469 /* The destructor of dwarf2_queue_guard frees any entries left on
2470 the queue. After this point we're guaranteed to leave this function
2471 with the dwarf queue empty. */
2472 dwarf2_queue_guard q_guard (per_objfile);
2473
2474 if (!per_objfile->symtab_set_p (per_cu))
2475 {
2476 queue_comp_unit (per_cu, per_objfile, language_minimal);
2477 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2478
2479 /* If we just loaded a CU from a DWO, and we're working with an index
2480 that may badly handle TUs, load all the TUs in that DWO as well.
2481 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2482 if (!per_cu->is_debug_types
2483 && cu != NULL
2484 && cu->dwo_unit != NULL
2485 && per_objfile->per_bfd->index_table != NULL
2486 && per_objfile->per_bfd->index_table->version <= 7
2487 /* DWP files aren't supported yet. */
2488 && get_dwp_file (per_objfile) == NULL)
2489 queue_and_load_all_dwo_tus (cu);
2490 }
2491
2492 process_queue (per_objfile);
2493 }
2494
2495 /* Age the cache, releasing compilation units that have not
2496 been used recently. */
2497 per_objfile->age_comp_units ();
2498 }
2499
2500 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2501 the per-objfile for which this symtab is instantiated.
2502
2503 Returns the resulting symbol table. */
2504
2505 static struct compunit_symtab *
2506 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2507 dwarf2_per_objfile *per_objfile,
2508 bool skip_partial)
2509 {
2510 gdb_assert (per_objfile->per_bfd->using_index);
2511
2512 if (!per_objfile->symtab_set_p (per_cu))
2513 {
2514 free_cached_comp_units freer (per_objfile);
2515 scoped_restore decrementer = increment_reading_symtab ();
2516 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2517 process_cu_includes (per_objfile);
2518 }
2519
2520 return per_objfile->get_symtab (per_cu);
2521 }
2522
2523 /* See read.h. */
2524
2525 std::unique_ptr<dwarf2_per_cu_data>
2526 dwarf2_per_bfd::allocate_per_cu ()
2527 {
2528 std::unique_ptr<dwarf2_per_cu_data> result (new dwarf2_per_cu_data);
2529 result->per_bfd = this;
2530 result->index = m_num_psymtabs++;
2531 return result;
2532 }
2533
2534 /* See read.h. */
2535
2536 std::unique_ptr<signatured_type>
2537 dwarf2_per_bfd::allocate_signatured_type ()
2538 {
2539 std::unique_ptr<signatured_type> result (new signatured_type);
2540 result->per_bfd = this;
2541 result->index = m_num_psymtabs++;
2542 tu_stats.nr_tus++;
2543 return result;
2544 }
2545
2546 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2547 obstack, and constructed with the specified field values. */
2548
2549 static std::unique_ptr<dwarf2_per_cu_data>
2550 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2551 struct dwarf2_section_info *section,
2552 int is_dwz,
2553 sect_offset sect_off, ULONGEST length)
2554 {
2555 std::unique_ptr<dwarf2_per_cu_data> the_cu = per_bfd->allocate_per_cu ();
2556 the_cu->sect_off = sect_off;
2557 the_cu->length = length;
2558 the_cu->section = section;
2559 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2560 struct dwarf2_per_cu_quick_data);
2561 the_cu->is_dwz = is_dwz;
2562 return the_cu;
2563 }
2564
2565 /* A helper for create_cus_from_index that handles a given list of
2566 CUs. */
2567
2568 static void
2569 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2570 const gdb_byte *cu_list, offset_type n_elements,
2571 struct dwarf2_section_info *section,
2572 int is_dwz)
2573 {
2574 for (offset_type i = 0; i < n_elements; i += 2)
2575 {
2576 gdb_static_assert (sizeof (ULONGEST) >= 8);
2577
2578 sect_offset sect_off
2579 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2580 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2581 cu_list += 2 * 8;
2582
2583 std::unique_ptr<dwarf2_per_cu_data> per_cu
2584 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2585 length);
2586 per_bfd->all_comp_units.push_back (std::move (per_cu));
2587 }
2588 }
2589
2590 /* Read the CU list from the mapped index, and use it to create all
2591 the CU objects for PER_BFD. */
2592
2593 static void
2594 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2595 const gdb_byte *cu_list, offset_type cu_list_elements,
2596 const gdb_byte *dwz_list, offset_type dwz_elements)
2597 {
2598 gdb_assert (per_bfd->all_comp_units.empty ());
2599 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2600
2601 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2602 &per_bfd->info, 0);
2603
2604 if (dwz_elements == 0)
2605 return;
2606
2607 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2608 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2609 &dwz->info, 1);
2610 }
2611
2612 /* Create the signatured type hash table from the index. */
2613
2614 static void
2615 create_signatured_type_table_from_index
2616 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2617 const gdb_byte *bytes, offset_type elements)
2618 {
2619 htab_up sig_types_hash = allocate_signatured_type_table ();
2620
2621 for (offset_type i = 0; i < elements; i += 3)
2622 {
2623 std::unique_ptr<signatured_type> sig_type;
2624 ULONGEST signature;
2625 void **slot;
2626 cu_offset type_offset_in_tu;
2627
2628 gdb_static_assert (sizeof (ULONGEST) >= 8);
2629 sect_offset sect_off
2630 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2631 type_offset_in_tu
2632 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2633 BFD_ENDIAN_LITTLE);
2634 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2635 bytes += 3 * 8;
2636
2637 sig_type = per_bfd->allocate_signatured_type ();
2638 sig_type->signature = signature;
2639 sig_type->type_offset_in_tu = type_offset_in_tu;
2640 sig_type->is_debug_types = 1;
2641 sig_type->section = section;
2642 sig_type->sect_off = sect_off;
2643 sig_type->v.quick
2644 = OBSTACK_ZALLOC (&per_bfd->obstack,
2645 struct dwarf2_per_cu_quick_data);
2646
2647 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2648 *slot = sig_type.get ();
2649
2650 per_bfd->all_comp_units.push_back (std::move (sig_type));
2651 }
2652
2653 per_bfd->signatured_types = std::move (sig_types_hash);
2654 }
2655
2656 /* Create the signatured type hash table from .debug_names. */
2657
2658 static void
2659 create_signatured_type_table_from_debug_names
2660 (dwarf2_per_objfile *per_objfile,
2661 const mapped_debug_names &map,
2662 struct dwarf2_section_info *section,
2663 struct dwarf2_section_info *abbrev_section)
2664 {
2665 struct objfile *objfile = per_objfile->objfile;
2666
2667 section->read (objfile);
2668 abbrev_section->read (objfile);
2669
2670 htab_up sig_types_hash = allocate_signatured_type_table ();
2671
2672 for (uint32_t i = 0; i < map.tu_count; ++i)
2673 {
2674 std::unique_ptr<signatured_type> sig_type;
2675 void **slot;
2676
2677 sect_offset sect_off
2678 = (sect_offset) (extract_unsigned_integer
2679 (map.tu_table_reordered + i * map.offset_size,
2680 map.offset_size,
2681 map.dwarf5_byte_order));
2682
2683 comp_unit_head cu_header;
2684 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2685 abbrev_section,
2686 section->buffer + to_underlying (sect_off),
2687 rcuh_kind::TYPE);
2688
2689 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2690 sig_type->signature = cu_header.signature;
2691 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2692 sig_type->is_debug_types = 1;
2693 sig_type->section = section;
2694 sig_type->sect_off = sect_off;
2695 sig_type->v.quick
2696 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2697 struct dwarf2_per_cu_quick_data);
2698
2699 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2700 *slot = sig_type.get ();
2701
2702 per_objfile->per_bfd->all_comp_units.push_back (std::move (sig_type));
2703 }
2704
2705 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2706 }
2707
2708 /* Read the address map data from the mapped index, and use it to
2709 populate the psymtabs_addrmap. */
2710
2711 static void
2712 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2713 struct mapped_index *index)
2714 {
2715 struct objfile *objfile = per_objfile->objfile;
2716 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2717 struct gdbarch *gdbarch = objfile->arch ();
2718 const gdb_byte *iter, *end;
2719 struct addrmap *mutable_map;
2720 CORE_ADDR baseaddr;
2721
2722 auto_obstack temp_obstack;
2723
2724 mutable_map = addrmap_create_mutable (&temp_obstack);
2725
2726 iter = index->address_table.data ();
2727 end = iter + index->address_table.size ();
2728
2729 baseaddr = objfile->text_section_offset ();
2730
2731 while (iter < end)
2732 {
2733 ULONGEST hi, lo, cu_index;
2734 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2735 iter += 8;
2736 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2737 iter += 8;
2738 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2739 iter += 4;
2740
2741 if (lo > hi)
2742 {
2743 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2744 hex_string (lo), hex_string (hi));
2745 continue;
2746 }
2747
2748 if (cu_index >= per_bfd->all_comp_units.size ())
2749 {
2750 complaint (_(".gdb_index address table has invalid CU number %u"),
2751 (unsigned) cu_index);
2752 continue;
2753 }
2754
2755 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2756 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2757 addrmap_set_empty (mutable_map, lo, hi - 1,
2758 per_bfd->get_cu (cu_index));
2759 }
2760
2761 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2762 &per_bfd->obstack);
2763 }
2764
2765 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2766 populate the psymtabs_addrmap. */
2767
2768 static void
2769 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2770 struct dwarf2_section_info *section)
2771 {
2772 struct objfile *objfile = per_objfile->objfile;
2773 bfd *abfd = objfile->obfd;
2774 struct gdbarch *gdbarch = objfile->arch ();
2775 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2776 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2777
2778 auto_obstack temp_obstack;
2779 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2780
2781 std::unordered_map<sect_offset,
2782 dwarf2_per_cu_data *,
2783 gdb::hash_enum<sect_offset>>
2784 debug_info_offset_to_per_cu;
2785 for (const auto &per_cu : per_bfd->all_comp_units)
2786 {
2787 const auto insertpair
2788 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2789 per_cu.get ());
2790 if (!insertpair.second)
2791 {
2792 warning (_("Section .debug_aranges in %s has duplicate "
2793 "debug_info_offset %s, ignoring .debug_aranges."),
2794 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2795 return;
2796 }
2797 }
2798
2799 section->read (objfile);
2800
2801 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2802
2803 const gdb_byte *addr = section->buffer;
2804
2805 while (addr < section->buffer + section->size)
2806 {
2807 const gdb_byte *const entry_addr = addr;
2808 unsigned int bytes_read;
2809
2810 const LONGEST entry_length = read_initial_length (abfd, addr,
2811 &bytes_read);
2812 addr += bytes_read;
2813
2814 const gdb_byte *const entry_end = addr + entry_length;
2815 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2816 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2817 if (addr + entry_length > section->buffer + section->size)
2818 {
2819 warning (_("Section .debug_aranges in %s entry at offset %s "
2820 "length %s exceeds section length %s, "
2821 "ignoring .debug_aranges."),
2822 objfile_name (objfile),
2823 plongest (entry_addr - section->buffer),
2824 plongest (bytes_read + entry_length),
2825 pulongest (section->size));
2826 return;
2827 }
2828
2829 /* The version number. */
2830 const uint16_t version = read_2_bytes (abfd, addr);
2831 addr += 2;
2832 if (version != 2)
2833 {
2834 warning (_("Section .debug_aranges in %s entry at offset %s "
2835 "has unsupported version %d, ignoring .debug_aranges."),
2836 objfile_name (objfile),
2837 plongest (entry_addr - section->buffer), version);
2838 return;
2839 }
2840
2841 const uint64_t debug_info_offset
2842 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2843 addr += offset_size;
2844 const auto per_cu_it
2845 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2846 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2847 {
2848 warning (_("Section .debug_aranges in %s entry at offset %s "
2849 "debug_info_offset %s does not exists, "
2850 "ignoring .debug_aranges."),
2851 objfile_name (objfile),
2852 plongest (entry_addr - section->buffer),
2853 pulongest (debug_info_offset));
2854 return;
2855 }
2856 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2857
2858 const uint8_t address_size = *addr++;
2859 if (address_size < 1 || address_size > 8)
2860 {
2861 warning (_("Section .debug_aranges in %s entry at offset %s "
2862 "address_size %u is invalid, ignoring .debug_aranges."),
2863 objfile_name (objfile),
2864 plongest (entry_addr - section->buffer), address_size);
2865 return;
2866 }
2867
2868 const uint8_t segment_selector_size = *addr++;
2869 if (segment_selector_size != 0)
2870 {
2871 warning (_("Section .debug_aranges in %s entry at offset %s "
2872 "segment_selector_size %u is not supported, "
2873 "ignoring .debug_aranges."),
2874 objfile_name (objfile),
2875 plongest (entry_addr - section->buffer),
2876 segment_selector_size);
2877 return;
2878 }
2879
2880 /* Must pad to an alignment boundary that is twice the address
2881 size. It is undocumented by the DWARF standard but GCC does
2882 use it. */
2883 for (size_t padding = ((-(addr - section->buffer))
2884 & (2 * address_size - 1));
2885 padding > 0; padding--)
2886 if (*addr++ != 0)
2887 {
2888 warning (_("Section .debug_aranges in %s entry at offset %s "
2889 "padding is not zero, ignoring .debug_aranges."),
2890 objfile_name (objfile),
2891 plongest (entry_addr - section->buffer));
2892 return;
2893 }
2894
2895 for (;;)
2896 {
2897 if (addr + 2 * address_size > entry_end)
2898 {
2899 warning (_("Section .debug_aranges in %s entry at offset %s "
2900 "address list is not properly terminated, "
2901 "ignoring .debug_aranges."),
2902 objfile_name (objfile),
2903 plongest (entry_addr - section->buffer));
2904 return;
2905 }
2906 ULONGEST start = extract_unsigned_integer (addr, address_size,
2907 dwarf5_byte_order);
2908 addr += address_size;
2909 ULONGEST length = extract_unsigned_integer (addr, address_size,
2910 dwarf5_byte_order);
2911 addr += address_size;
2912 if (start == 0 && length == 0)
2913 break;
2914 if (start == 0 && !per_bfd->has_section_at_zero)
2915 {
2916 /* Symbol was eliminated due to a COMDAT group. */
2917 continue;
2918 }
2919 ULONGEST end = start + length;
2920 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2921 - baseaddr);
2922 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2923 - baseaddr);
2924 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2925 }
2926 }
2927
2928 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2929 &per_bfd->obstack);
2930 }
2931
2932 /* A helper function that reads the .gdb_index from BUFFER and fills
2933 in MAP. FILENAME is the name of the file containing the data;
2934 it is used for error reporting. DEPRECATED_OK is true if it is
2935 ok to use deprecated sections.
2936
2937 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2938 out parameters that are filled in with information about the CU and
2939 TU lists in the section.
2940
2941 Returns true if all went well, false otherwise. */
2942
2943 static bool
2944 read_gdb_index_from_buffer (const char *filename,
2945 bool deprecated_ok,
2946 gdb::array_view<const gdb_byte> buffer,
2947 struct mapped_index *map,
2948 const gdb_byte **cu_list,
2949 offset_type *cu_list_elements,
2950 const gdb_byte **types_list,
2951 offset_type *types_list_elements)
2952 {
2953 const gdb_byte *addr = &buffer[0];
2954 offset_view metadata (buffer);
2955
2956 /* Version check. */
2957 offset_type version = metadata[0];
2958 /* Versions earlier than 3 emitted every copy of a psymbol. This
2959 causes the index to behave very poorly for certain requests. Version 3
2960 contained incomplete addrmap. So, it seems better to just ignore such
2961 indices. */
2962 if (version < 4)
2963 {
2964 static int warning_printed = 0;
2965 if (!warning_printed)
2966 {
2967 warning (_("Skipping obsolete .gdb_index section in %s."),
2968 filename);
2969 warning_printed = 1;
2970 }
2971 return 0;
2972 }
2973 /* Index version 4 uses a different hash function than index version
2974 5 and later.
2975
2976 Versions earlier than 6 did not emit psymbols for inlined
2977 functions. Using these files will cause GDB not to be able to
2978 set breakpoints on inlined functions by name, so we ignore these
2979 indices unless the user has done
2980 "set use-deprecated-index-sections on". */
2981 if (version < 6 && !deprecated_ok)
2982 {
2983 static int warning_printed = 0;
2984 if (!warning_printed)
2985 {
2986 warning (_("\
2987 Skipping deprecated .gdb_index section in %s.\n\
2988 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2989 to use the section anyway."),
2990 filename);
2991 warning_printed = 1;
2992 }
2993 return 0;
2994 }
2995 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2996 of the TU (for symbols coming from TUs),
2997 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2998 Plus gold-generated indices can have duplicate entries for global symbols,
2999 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3000 These are just performance bugs, and we can't distinguish gdb-generated
3001 indices from gold-generated ones, so issue no warning here. */
3002
3003 /* Indexes with higher version than the one supported by GDB may be no
3004 longer backward compatible. */
3005 if (version > 8)
3006 return 0;
3007
3008 map->version = version;
3009
3010 int i = 1;
3011 *cu_list = addr + metadata[i];
3012 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
3013 ++i;
3014
3015 *types_list = addr + metadata[i];
3016 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
3017 ++i;
3018
3019 const gdb_byte *address_table = addr + metadata[i];
3020 const gdb_byte *address_table_end = addr + metadata[i + 1];
3021 map->address_table
3022 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3023 ++i;
3024
3025 const gdb_byte *symbol_table = addr + metadata[i];
3026 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
3027 map->symbol_table
3028 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
3029 symbol_table_end));
3030
3031 ++i;
3032 map->constant_pool = buffer.slice (metadata[i]);
3033
3034 return 1;
3035 }
3036
3037 /* Callback types for dwarf2_read_gdb_index. */
3038
3039 typedef gdb::function_view
3040 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3041 get_gdb_index_contents_ftype;
3042 typedef gdb::function_view
3043 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3044 get_gdb_index_contents_dwz_ftype;
3045
3046 /* Read .gdb_index. If everything went ok, initialize the "quick"
3047 elements of all the CUs and return 1. Otherwise, return 0. */
3048
3049 static int
3050 dwarf2_read_gdb_index
3051 (dwarf2_per_objfile *per_objfile,
3052 get_gdb_index_contents_ftype get_gdb_index_contents,
3053 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3054 {
3055 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3056 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3057 struct dwz_file *dwz;
3058 struct objfile *objfile = per_objfile->objfile;
3059 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3060
3061 gdb::array_view<const gdb_byte> main_index_contents
3062 = get_gdb_index_contents (objfile, per_bfd);
3063
3064 if (main_index_contents.empty ())
3065 return 0;
3066
3067 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3068 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3069 use_deprecated_index_sections,
3070 main_index_contents, map.get (), &cu_list,
3071 &cu_list_elements, &types_list,
3072 &types_list_elements))
3073 return 0;
3074
3075 /* Don't use the index if it's empty. */
3076 if (map->symbol_table.empty ())
3077 return 0;
3078
3079 /* If there is a .dwz file, read it so we can get its CU list as
3080 well. */
3081 dwz = dwarf2_get_dwz_file (per_bfd);
3082 if (dwz != NULL)
3083 {
3084 struct mapped_index dwz_map;
3085 const gdb_byte *dwz_types_ignore;
3086 offset_type dwz_types_elements_ignore;
3087
3088 gdb::array_view<const gdb_byte> dwz_index_content
3089 = get_gdb_index_contents_dwz (objfile, dwz);
3090
3091 if (dwz_index_content.empty ())
3092 return 0;
3093
3094 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3095 1, dwz_index_content, &dwz_map,
3096 &dwz_list, &dwz_list_elements,
3097 &dwz_types_ignore,
3098 &dwz_types_elements_ignore))
3099 {
3100 warning (_("could not read '.gdb_index' section from %s; skipping"),
3101 bfd_get_filename (dwz->dwz_bfd.get ()));
3102 return 0;
3103 }
3104 }
3105
3106 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3107 dwz_list_elements);
3108
3109 if (types_list_elements)
3110 {
3111 /* We can only handle a single .debug_types when we have an
3112 index. */
3113 if (per_bfd->types.size () != 1)
3114 return 0;
3115
3116 dwarf2_section_info *section = &per_bfd->types[0];
3117
3118 create_signatured_type_table_from_index (per_bfd, section, types_list,
3119 types_list_elements);
3120 }
3121
3122 create_addrmap_from_index (per_objfile, map.get ());
3123
3124 per_bfd->index_table = std::move (map);
3125 per_bfd->using_index = 1;
3126 per_bfd->quick_file_names_table =
3127 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3128
3129 return 1;
3130 }
3131
3132 /* die_reader_func for dw2_get_file_names. */
3133
3134 static void
3135 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3136 struct die_info *comp_unit_die)
3137 {
3138 struct dwarf2_cu *cu = reader->cu;
3139 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3140 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3141 struct dwarf2_per_cu_data *lh_cu;
3142 struct attribute *attr;
3143 void **slot;
3144 struct quick_file_names *qfn;
3145
3146 gdb_assert (! this_cu->is_debug_types);
3147
3148 /* Our callers never want to match partial units -- instead they
3149 will match the enclosing full CU. */
3150 if (comp_unit_die->tag == DW_TAG_partial_unit)
3151 {
3152 this_cu->v.quick->no_file_data = 1;
3153 return;
3154 }
3155
3156 lh_cu = this_cu;
3157 slot = NULL;
3158
3159 line_header_up lh;
3160 sect_offset line_offset {};
3161
3162 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3163 if (attr != nullptr && attr->form_is_unsigned ())
3164 {
3165 struct quick_file_names find_entry;
3166
3167 line_offset = (sect_offset) attr->as_unsigned ();
3168
3169 /* We may have already read in this line header (TU line header sharing).
3170 If we have we're done. */
3171 find_entry.hash.dwo_unit = cu->dwo_unit;
3172 find_entry.hash.line_sect_off = line_offset;
3173 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3174 &find_entry, INSERT);
3175 if (*slot != NULL)
3176 {
3177 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3178 return;
3179 }
3180
3181 lh = dwarf_decode_line_header (line_offset, cu);
3182 }
3183 if (lh == NULL)
3184 {
3185 lh_cu->v.quick->no_file_data = 1;
3186 return;
3187 }
3188
3189 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3190 qfn->hash.dwo_unit = cu->dwo_unit;
3191 qfn->hash.line_sect_off = line_offset;
3192 gdb_assert (slot != NULL);
3193 *slot = qfn;
3194
3195 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3196
3197 int offset = 0;
3198 if (strcmp (fnd.name, "<unknown>") != 0)
3199 ++offset;
3200
3201 qfn->num_file_names = offset + lh->file_names_size ();
3202 qfn->file_names =
3203 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3204 qfn->num_file_names);
3205 if (offset != 0)
3206 qfn->file_names[0] = xstrdup (fnd.name);
3207 for (int i = 0; i < lh->file_names_size (); ++i)
3208 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3209 fnd.comp_dir).release ();
3210 qfn->real_names = NULL;
3211
3212 lh_cu->v.quick->file_names = qfn;
3213 }
3214
3215 /* A helper for the "quick" functions which attempts to read the line
3216 table for THIS_CU. */
3217
3218 static struct quick_file_names *
3219 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3220 dwarf2_per_objfile *per_objfile)
3221 {
3222 /* This should never be called for TUs. */
3223 gdb_assert (! this_cu->is_debug_types);
3224 /* Nor type unit groups. */
3225 gdb_assert (! this_cu->type_unit_group_p ());
3226
3227 if (this_cu->v.quick->file_names != NULL)
3228 return this_cu->v.quick->file_names;
3229 /* If we know there is no line data, no point in looking again. */
3230 if (this_cu->v.quick->no_file_data)
3231 return NULL;
3232
3233 cutu_reader reader (this_cu, per_objfile);
3234 if (!reader.dummy_p)
3235 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
3236
3237 if (this_cu->v.quick->no_file_data)
3238 return NULL;
3239 return this_cu->v.quick->file_names;
3240 }
3241
3242 /* A helper for the "quick" functions which computes and caches the
3243 real path for a given file name from the line table. */
3244
3245 static const char *
3246 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3247 struct quick_file_names *qfn, int index)
3248 {
3249 if (qfn->real_names == NULL)
3250 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3251 qfn->num_file_names, const char *);
3252
3253 if (qfn->real_names[index] == NULL)
3254 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3255
3256 return qfn->real_names[index];
3257 }
3258
3259 struct symtab *
3260 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3261 {
3262 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3263 dwarf2_per_cu_data *dwarf_cu
3264 = per_objfile->per_bfd->all_comp_units.back ().get ();
3265 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3266
3267 if (cust == NULL)
3268 return NULL;
3269
3270 return compunit_primary_filetab (cust);
3271 }
3272
3273 /* Traversal function for dw2_forget_cached_source_info. */
3274
3275 static int
3276 dw2_free_cached_file_names (void **slot, void *info)
3277 {
3278 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3279
3280 if (file_data->real_names)
3281 {
3282 int i;
3283
3284 for (i = 0; i < file_data->num_file_names; ++i)
3285 {
3286 xfree ((void*) file_data->real_names[i]);
3287 file_data->real_names[i] = NULL;
3288 }
3289 }
3290
3291 return 1;
3292 }
3293
3294 void
3295 dwarf2_base_index_functions::forget_cached_source_info
3296 (struct objfile *objfile)
3297 {
3298 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3299
3300 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3301 dw2_free_cached_file_names, NULL);
3302 }
3303
3304 /* Struct used to manage iterating over all CUs looking for a symbol. */
3305
3306 struct dw2_symtab_iterator
3307 {
3308 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3309 dwarf2_per_objfile *per_objfile;
3310 /* If set, only look for symbols that match that block. Valid values are
3311 GLOBAL_BLOCK and STATIC_BLOCK. */
3312 gdb::optional<block_enum> block_index;
3313 /* The kind of symbol we're looking for. */
3314 domain_enum domain;
3315 /* The list of CUs from the index entry of the symbol,
3316 or NULL if not found. */
3317 offset_view vec;
3318 /* The next element in VEC to look at. */
3319 int next;
3320 /* The number of elements in VEC, or zero if there is no match. */
3321 int length;
3322 /* Have we seen a global version of the symbol?
3323 If so we can ignore all further global instances.
3324 This is to work around gold/15646, inefficient gold-generated
3325 indices. */
3326 int global_seen;
3327 };
3328
3329 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3330
3331 static void
3332 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3333 dwarf2_per_objfile *per_objfile,
3334 gdb::optional<block_enum> block_index,
3335 domain_enum domain, offset_type namei)
3336 {
3337 iter->per_objfile = per_objfile;
3338 iter->block_index = block_index;
3339 iter->domain = domain;
3340 iter->next = 0;
3341 iter->global_seen = 0;
3342 iter->vec = {};
3343 iter->length = 0;
3344
3345 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3346 /* index is NULL if OBJF_READNOW. */
3347 if (index == NULL)
3348 return;
3349
3350 gdb_assert (!index->symbol_name_slot_invalid (namei));
3351 offset_type vec_idx = index->symbol_vec_index (namei);
3352
3353 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3354 iter->length = iter->vec[0];
3355 }
3356
3357 /* Return the next matching CU or NULL if there are no more. */
3358
3359 static struct dwarf2_per_cu_data *
3360 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3361 {
3362 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3363
3364 for ( ; iter->next < iter->length; ++iter->next)
3365 {
3366 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
3367 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3368 gdb_index_symbol_kind symbol_kind =
3369 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3370 /* Only check the symbol attributes if they're present.
3371 Indices prior to version 7 don't record them,
3372 and indices >= 7 may elide them for certain symbols
3373 (gold does this). */
3374 int attrs_valid =
3375 (per_objfile->per_bfd->index_table->version >= 7
3376 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3377
3378 /* Don't crash on bad data. */
3379 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
3380 {
3381 complaint (_(".gdb_index entry has bad CU index"
3382 " [in module %s]"), objfile_name (per_objfile->objfile));
3383 continue;
3384 }
3385
3386 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3387
3388 /* Skip if already read in. */
3389 if (per_objfile->symtab_set_p (per_cu))
3390 continue;
3391
3392 /* Check static vs global. */
3393 if (attrs_valid)
3394 {
3395 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3396
3397 if (iter->block_index.has_value ())
3398 {
3399 bool want_static = *iter->block_index == STATIC_BLOCK;
3400
3401 if (is_static != want_static)
3402 continue;
3403 }
3404
3405 /* Work around gold/15646. */
3406 if (!is_static
3407 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3408 {
3409 if (iter->global_seen)
3410 continue;
3411
3412 iter->global_seen = 1;
3413 }
3414 }
3415
3416 /* Only check the symbol's kind if it has one. */
3417 if (attrs_valid)
3418 {
3419 switch (iter->domain)
3420 {
3421 case VAR_DOMAIN:
3422 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3423 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3424 /* Some types are also in VAR_DOMAIN. */
3425 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3426 continue;
3427 break;
3428 case STRUCT_DOMAIN:
3429 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3430 continue;
3431 break;
3432 case LABEL_DOMAIN:
3433 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3434 continue;
3435 break;
3436 case MODULE_DOMAIN:
3437 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3438 continue;
3439 break;
3440 default:
3441 break;
3442 }
3443 }
3444
3445 ++iter->next;
3446 return per_cu;
3447 }
3448
3449 return NULL;
3450 }
3451
3452 void
3453 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3454 bool print_bcache)
3455 {
3456 if (print_bcache)
3457 return;
3458
3459 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3460 int total = per_objfile->per_bfd->all_comp_units.size ();
3461 int count = 0;
3462
3463 for (int i = 0; i < total; ++i)
3464 {
3465 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3466
3467 if (!per_objfile->symtab_set_p (per_cu))
3468 ++count;
3469 }
3470 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3471 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3472 }
3473
3474 /* This dumps minimal information about the index.
3475 It is called via "mt print objfiles".
3476 One use is to verify .gdb_index has been loaded by the
3477 gdb.dwarf2/gdb-index.exp testcase. */
3478
3479 void
3480 dwarf2_gdb_index::dump (struct objfile *objfile)
3481 {
3482 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3483
3484 gdb_assert (per_objfile->per_bfd->using_index);
3485 printf_filtered (".gdb_index:");
3486 if (per_objfile->per_bfd->index_table != NULL)
3487 {
3488 printf_filtered (" version %d\n",
3489 per_objfile->per_bfd->index_table->version);
3490 }
3491 else
3492 printf_filtered (" faked for \"readnow\"\n");
3493 printf_filtered ("\n");
3494 }
3495
3496 void
3497 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3498 {
3499 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3500 int total_units = per_objfile->per_bfd->all_comp_units.size ();
3501
3502 for (int i = 0; i < total_units; ++i)
3503 {
3504 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3505
3506 /* We don't want to directly expand a partial CU, because if we
3507 read it with the wrong language, then assertion failures can
3508 be triggered later on. See PR symtab/23010. So, tell
3509 dw2_instantiate_symtab to skip partial CUs -- any important
3510 partial CU will be read via DW_TAG_imported_unit anyway. */
3511 dw2_instantiate_symtab (per_cu, per_objfile, true);
3512 }
3513 }
3514
3515 static bool
3516 dw2_expand_symtabs_matching_symbol
3517 (mapped_index_base &index,
3518 const lookup_name_info &lookup_name_in,
3519 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3520 gdb::function_view<bool (offset_type)> match_callback,
3521 dwarf2_per_objfile *per_objfile);
3522
3523 static bool
3524 dw2_expand_symtabs_matching_one
3525 (dwarf2_per_cu_data *per_cu,
3526 dwarf2_per_objfile *per_objfile,
3527 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3528 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3529
3530 void
3531 dwarf2_gdb_index::expand_matching_symbols
3532 (struct objfile *objfile,
3533 const lookup_name_info &name, domain_enum domain,
3534 int global,
3535 symbol_compare_ftype *ordered_compare)
3536 {
3537 /* Used for Ada. */
3538 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3539
3540 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3541
3542 if (per_objfile->per_bfd->index_table != nullptr)
3543 {
3544 mapped_index &index = *per_objfile->per_bfd->index_table;
3545
3546 const char *match_name = name.ada ().lookup_name ().c_str ();
3547 auto matcher = [&] (const char *symname)
3548 {
3549 if (ordered_compare == nullptr)
3550 return true;
3551 return ordered_compare (symname, match_name) == 0;
3552 };
3553
3554 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3555 [&] (offset_type namei)
3556 {
3557 struct dw2_symtab_iterator iter;
3558 struct dwarf2_per_cu_data *per_cu;
3559
3560 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3561 namei);
3562 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3563 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3564 nullptr);
3565 return true;
3566 }, per_objfile);
3567 }
3568 else
3569 {
3570 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3571 proceed assuming all symtabs have been read in. */
3572 }
3573 }
3574
3575 /* Starting from a search name, return the string that finds the upper
3576 bound of all strings that start with SEARCH_NAME in a sorted name
3577 list. Returns the empty string to indicate that the upper bound is
3578 the end of the list. */
3579
3580 static std::string
3581 make_sort_after_prefix_name (const char *search_name)
3582 {
3583 /* When looking to complete "func", we find the upper bound of all
3584 symbols that start with "func" by looking for where we'd insert
3585 the closest string that would follow "func" in lexicographical
3586 order. Usually, that's "func"-with-last-character-incremented,
3587 i.e. "fund". Mind non-ASCII characters, though. Usually those
3588 will be UTF-8 multi-byte sequences, but we can't be certain.
3589 Especially mind the 0xff character, which is a valid character in
3590 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3591 rule out compilers allowing it in identifiers. Note that
3592 conveniently, strcmp/strcasecmp are specified to compare
3593 characters interpreted as unsigned char. So what we do is treat
3594 the whole string as a base 256 number composed of a sequence of
3595 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3596 to 0, and carries 1 to the following more-significant position.
3597 If the very first character in SEARCH_NAME ends up incremented
3598 and carries/overflows, then the upper bound is the end of the
3599 list. The string after the empty string is also the empty
3600 string.
3601
3602 Some examples of this operation:
3603
3604 SEARCH_NAME => "+1" RESULT
3605
3606 "abc" => "abd"
3607 "ab\xff" => "ac"
3608 "\xff" "a" "\xff" => "\xff" "b"
3609 "\xff" => ""
3610 "\xff\xff" => ""
3611 "" => ""
3612
3613 Then, with these symbols for example:
3614
3615 func
3616 func1
3617 fund
3618
3619 completing "func" looks for symbols between "func" and
3620 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3621 which finds "func" and "func1", but not "fund".
3622
3623 And with:
3624
3625 funcÿ (Latin1 'ÿ' [0xff])
3626 funcÿ1
3627 fund
3628
3629 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3630 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3631
3632 And with:
3633
3634 ÿÿ (Latin1 'ÿ' [0xff])
3635 ÿÿ1
3636
3637 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3638 the end of the list.
3639 */
3640 std::string after = search_name;
3641 while (!after.empty () && (unsigned char) after.back () == 0xff)
3642 after.pop_back ();
3643 if (!after.empty ())
3644 after.back () = (unsigned char) after.back () + 1;
3645 return after;
3646 }
3647
3648 /* See declaration. */
3649
3650 std::pair<std::vector<name_component>::const_iterator,
3651 std::vector<name_component>::const_iterator>
3652 mapped_index_base::find_name_components_bounds
3653 (const lookup_name_info &lookup_name_without_params, language lang,
3654 dwarf2_per_objfile *per_objfile) const
3655 {
3656 auto *name_cmp
3657 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3658
3659 const char *lang_name
3660 = lookup_name_without_params.language_lookup_name (lang);
3661
3662 /* Comparison function object for lower_bound that matches against a
3663 given symbol name. */
3664 auto lookup_compare_lower = [&] (const name_component &elem,
3665 const char *name)
3666 {
3667 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3668 const char *elem_name = elem_qualified + elem.name_offset;
3669 return name_cmp (elem_name, name) < 0;
3670 };
3671
3672 /* Comparison function object for upper_bound that matches against a
3673 given symbol name. */
3674 auto lookup_compare_upper = [&] (const char *name,
3675 const name_component &elem)
3676 {
3677 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3678 const char *elem_name = elem_qualified + elem.name_offset;
3679 return name_cmp (name, elem_name) < 0;
3680 };
3681
3682 auto begin = this->name_components.begin ();
3683 auto end = this->name_components.end ();
3684
3685 /* Find the lower bound. */
3686 auto lower = [&] ()
3687 {
3688 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3689 return begin;
3690 else
3691 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3692 } ();
3693
3694 /* Find the upper bound. */
3695 auto upper = [&] ()
3696 {
3697 if (lookup_name_without_params.completion_mode ())
3698 {
3699 /* In completion mode, we want UPPER to point past all
3700 symbols names that have the same prefix. I.e., with
3701 these symbols, and completing "func":
3702
3703 function << lower bound
3704 function1
3705 other_function << upper bound
3706
3707 We find the upper bound by looking for the insertion
3708 point of "func"-with-last-character-incremented,
3709 i.e. "fund". */
3710 std::string after = make_sort_after_prefix_name (lang_name);
3711 if (after.empty ())
3712 return end;
3713 return std::lower_bound (lower, end, after.c_str (),
3714 lookup_compare_lower);
3715 }
3716 else
3717 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3718 } ();
3719
3720 return {lower, upper};
3721 }
3722
3723 /* See declaration. */
3724
3725 void
3726 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3727 {
3728 if (!this->name_components.empty ())
3729 return;
3730
3731 this->name_components_casing = case_sensitivity;
3732 auto *name_cmp
3733 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3734
3735 /* The code below only knows how to break apart components of C++
3736 symbol names (and other languages that use '::' as
3737 namespace/module separator) and Ada symbol names. */
3738 auto count = this->symbol_name_count ();
3739 for (offset_type idx = 0; idx < count; idx++)
3740 {
3741 if (this->symbol_name_slot_invalid (idx))
3742 continue;
3743
3744 const char *name = this->symbol_name_at (idx, per_objfile);
3745
3746 /* Add each name component to the name component table. */
3747 unsigned int previous_len = 0;
3748
3749 if (strstr (name, "::") != nullptr)
3750 {
3751 for (unsigned int current_len = cp_find_first_component (name);
3752 name[current_len] != '\0';
3753 current_len += cp_find_first_component (name + current_len))
3754 {
3755 gdb_assert (name[current_len] == ':');
3756 this->name_components.push_back ({previous_len, idx});
3757 /* Skip the '::'. */
3758 current_len += 2;
3759 previous_len = current_len;
3760 }
3761 }
3762 else
3763 {
3764 /* Handle the Ada encoded (aka mangled) form here. */
3765 for (const char *iter = strstr (name, "__");
3766 iter != nullptr;
3767 iter = strstr (iter, "__"))
3768 {
3769 this->name_components.push_back ({previous_len, idx});
3770 iter += 2;
3771 previous_len = iter - name;
3772 }
3773 }
3774
3775 this->name_components.push_back ({previous_len, idx});
3776 }
3777
3778 /* Sort name_components elements by name. */
3779 auto name_comp_compare = [&] (const name_component &left,
3780 const name_component &right)
3781 {
3782 const char *left_qualified
3783 = this->symbol_name_at (left.idx, per_objfile);
3784 const char *right_qualified
3785 = this->symbol_name_at (right.idx, per_objfile);
3786
3787 const char *left_name = left_qualified + left.name_offset;
3788 const char *right_name = right_qualified + right.name_offset;
3789
3790 return name_cmp (left_name, right_name) < 0;
3791 };
3792
3793 std::sort (this->name_components.begin (),
3794 this->name_components.end (),
3795 name_comp_compare);
3796 }
3797
3798 /* Helper for dw2_expand_symtabs_matching that works with a
3799 mapped_index_base instead of the containing objfile. This is split
3800 to a separate function in order to be able to unit test the
3801 name_components matching using a mock mapped_index_base. For each
3802 symbol name that matches, calls MATCH_CALLBACK, passing it the
3803 symbol's index in the mapped_index_base symbol table. */
3804
3805 static bool
3806 dw2_expand_symtabs_matching_symbol
3807 (mapped_index_base &index,
3808 const lookup_name_info &lookup_name_in,
3809 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3810 gdb::function_view<bool (offset_type)> match_callback,
3811 dwarf2_per_objfile *per_objfile)
3812 {
3813 lookup_name_info lookup_name_without_params
3814 = lookup_name_in.make_ignore_params ();
3815
3816 /* Build the symbol name component sorted vector, if we haven't
3817 yet. */
3818 index.build_name_components (per_objfile);
3819
3820 /* The same symbol may appear more than once in the range though.
3821 E.g., if we're looking for symbols that complete "w", and we have
3822 a symbol named "w1::w2", we'll find the two name components for
3823 that same symbol in the range. To be sure we only call the
3824 callback once per symbol, we first collect the symbol name
3825 indexes that matched in a temporary vector and ignore
3826 duplicates. */
3827 std::vector<offset_type> matches;
3828
3829 struct name_and_matcher
3830 {
3831 symbol_name_matcher_ftype *matcher;
3832 const char *name;
3833
3834 bool operator== (const name_and_matcher &other) const
3835 {
3836 return matcher == other.matcher && strcmp (name, other.name) == 0;
3837 }
3838 };
3839
3840 /* A vector holding all the different symbol name matchers, for all
3841 languages. */
3842 std::vector<name_and_matcher> matchers;
3843
3844 for (int i = 0; i < nr_languages; i++)
3845 {
3846 enum language lang_e = (enum language) i;
3847
3848 const language_defn *lang = language_def (lang_e);
3849 symbol_name_matcher_ftype *name_matcher
3850 = lang->get_symbol_name_matcher (lookup_name_without_params);
3851
3852 name_and_matcher key {
3853 name_matcher,
3854 lookup_name_without_params.language_lookup_name (lang_e)
3855 };
3856
3857 /* Don't insert the same comparison routine more than once.
3858 Note that we do this linear walk. This is not a problem in
3859 practice because the number of supported languages is
3860 low. */
3861 if (std::find (matchers.begin (), matchers.end (), key)
3862 != matchers.end ())
3863 continue;
3864 matchers.push_back (std::move (key));
3865
3866 auto bounds
3867 = index.find_name_components_bounds (lookup_name_without_params,
3868 lang_e, per_objfile);
3869
3870 /* Now for each symbol name in range, check to see if we have a name
3871 match, and if so, call the MATCH_CALLBACK callback. */
3872
3873 for (; bounds.first != bounds.second; ++bounds.first)
3874 {
3875 const char *qualified
3876 = index.symbol_name_at (bounds.first->idx, per_objfile);
3877
3878 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3879 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3880 continue;
3881
3882 matches.push_back (bounds.first->idx);
3883 }
3884 }
3885
3886 std::sort (matches.begin (), matches.end ());
3887
3888 /* Finally call the callback, once per match. */
3889 ULONGEST prev = -1;
3890 bool result = true;
3891 for (offset_type idx : matches)
3892 {
3893 if (prev != idx)
3894 {
3895 if (!match_callback (idx))
3896 {
3897 result = false;
3898 break;
3899 }
3900 prev = idx;
3901 }
3902 }
3903
3904 /* Above we use a type wider than idx's for 'prev', since 0 and
3905 (offset_type)-1 are both possible values. */
3906 static_assert (sizeof (prev) > sizeof (offset_type), "");
3907
3908 return result;
3909 }
3910
3911 #if GDB_SELF_TEST
3912
3913 namespace selftests { namespace dw2_expand_symtabs_matching {
3914
3915 /* A mock .gdb_index/.debug_names-like name index table, enough to
3916 exercise dw2_expand_symtabs_matching_symbol, which works with the
3917 mapped_index_base interface. Builds an index from the symbol list
3918 passed as parameter to the constructor. */
3919 class mock_mapped_index : public mapped_index_base
3920 {
3921 public:
3922 mock_mapped_index (gdb::array_view<const char *> symbols)
3923 : m_symbol_table (symbols)
3924 {}
3925
3926 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3927
3928 /* Return the number of names in the symbol table. */
3929 size_t symbol_name_count () const override
3930 {
3931 return m_symbol_table.size ();
3932 }
3933
3934 /* Get the name of the symbol at IDX in the symbol table. */
3935 const char *symbol_name_at
3936 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3937 {
3938 return m_symbol_table[idx];
3939 }
3940
3941 private:
3942 gdb::array_view<const char *> m_symbol_table;
3943 };
3944
3945 /* Convenience function that converts a NULL pointer to a "<null>"
3946 string, to pass to print routines. */
3947
3948 static const char *
3949 string_or_null (const char *str)
3950 {
3951 return str != NULL ? str : "<null>";
3952 }
3953
3954 /* Check if a lookup_name_info built from
3955 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3956 index. EXPECTED_LIST is the list of expected matches, in expected
3957 matching order. If no match expected, then an empty list is
3958 specified. Returns true on success. On failure prints a warning
3959 indicating the file:line that failed, and returns false. */
3960
3961 static bool
3962 check_match (const char *file, int line,
3963 mock_mapped_index &mock_index,
3964 const char *name, symbol_name_match_type match_type,
3965 bool completion_mode,
3966 std::initializer_list<const char *> expected_list,
3967 dwarf2_per_objfile *per_objfile)
3968 {
3969 lookup_name_info lookup_name (name, match_type, completion_mode);
3970
3971 bool matched = true;
3972
3973 auto mismatch = [&] (const char *expected_str,
3974 const char *got)
3975 {
3976 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3977 "expected=\"%s\", got=\"%s\"\n"),
3978 file, line,
3979 (match_type == symbol_name_match_type::FULL
3980 ? "FULL" : "WILD"),
3981 name, string_or_null (expected_str), string_or_null (got));
3982 matched = false;
3983 };
3984
3985 auto expected_it = expected_list.begin ();
3986 auto expected_end = expected_list.end ();
3987
3988 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
3989 nullptr,
3990 [&] (offset_type idx)
3991 {
3992 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
3993 const char *expected_str
3994 = expected_it == expected_end ? NULL : *expected_it++;
3995
3996 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3997 mismatch (expected_str, matched_name);
3998 return true;
3999 }, per_objfile);
4000
4001 const char *expected_str
4002 = expected_it == expected_end ? NULL : *expected_it++;
4003 if (expected_str != NULL)
4004 mismatch (expected_str, NULL);
4005
4006 return matched;
4007 }
4008
4009 /* The symbols added to the mock mapped_index for testing (in
4010 canonical form). */
4011 static const char *test_symbols[] = {
4012 "function",
4013 "std::bar",
4014 "std::zfunction",
4015 "std::zfunction2",
4016 "w1::w2",
4017 "ns::foo<char*>",
4018 "ns::foo<int>",
4019 "ns::foo<long>",
4020 "ns2::tmpl<int>::foo2",
4021 "(anonymous namespace)::A::B::C",
4022
4023 /* These are used to check that the increment-last-char in the
4024 matching algorithm for completion doesn't match "t1_fund" when
4025 completing "t1_func". */
4026 "t1_func",
4027 "t1_func1",
4028 "t1_fund",
4029 "t1_fund1",
4030
4031 /* A UTF-8 name with multi-byte sequences to make sure that
4032 cp-name-parser understands this as a single identifier ("função"
4033 is "function" in PT). */
4034 u8"u8função",
4035
4036 /* \377 (0xff) is Latin1 'ÿ'. */
4037 "yfunc\377",
4038
4039 /* \377 (0xff) is Latin1 'ÿ'. */
4040 "\377",
4041 "\377\377123",
4042
4043 /* A name with all sorts of complications. Starts with "z" to make
4044 it easier for the completion tests below. */
4045 #define Z_SYM_NAME \
4046 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4047 "::tuple<(anonymous namespace)::ui*, " \
4048 "std::default_delete<(anonymous namespace)::ui>, void>"
4049
4050 Z_SYM_NAME
4051 };
4052
4053 /* Returns true if the mapped_index_base::find_name_component_bounds
4054 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4055 in completion mode. */
4056
4057 static bool
4058 check_find_bounds_finds (mapped_index_base &index,
4059 const char *search_name,
4060 gdb::array_view<const char *> expected_syms,
4061 dwarf2_per_objfile *per_objfile)
4062 {
4063 lookup_name_info lookup_name (search_name,
4064 symbol_name_match_type::FULL, true);
4065
4066 auto bounds = index.find_name_components_bounds (lookup_name,
4067 language_cplus,
4068 per_objfile);
4069
4070 size_t distance = std::distance (bounds.first, bounds.second);
4071 if (distance != expected_syms.size ())
4072 return false;
4073
4074 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4075 {
4076 auto nc_elem = bounds.first + exp_elem;
4077 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4078 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4079 return false;
4080 }
4081
4082 return true;
4083 }
4084
4085 /* Test the lower-level mapped_index::find_name_component_bounds
4086 method. */
4087
4088 static void
4089 test_mapped_index_find_name_component_bounds ()
4090 {
4091 mock_mapped_index mock_index (test_symbols);
4092
4093 mock_index.build_name_components (NULL /* per_objfile */);
4094
4095 /* Test the lower-level mapped_index::find_name_component_bounds
4096 method in completion mode. */
4097 {
4098 static const char *expected_syms[] = {
4099 "t1_func",
4100 "t1_func1",
4101 };
4102
4103 SELF_CHECK (check_find_bounds_finds
4104 (mock_index, "t1_func", expected_syms,
4105 NULL /* per_objfile */));
4106 }
4107
4108 /* Check that the increment-last-char in the name matching algorithm
4109 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4110 {
4111 static const char *expected_syms1[] = {
4112 "\377",
4113 "\377\377123",
4114 };
4115 SELF_CHECK (check_find_bounds_finds
4116 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4117
4118 static const char *expected_syms2[] = {
4119 "\377\377123",
4120 };
4121 SELF_CHECK (check_find_bounds_finds
4122 (mock_index, "\377\377", expected_syms2,
4123 NULL /* per_objfile */));
4124 }
4125 }
4126
4127 /* Test dw2_expand_symtabs_matching_symbol. */
4128
4129 static void
4130 test_dw2_expand_symtabs_matching_symbol ()
4131 {
4132 mock_mapped_index mock_index (test_symbols);
4133
4134 /* We let all tests run until the end even if some fails, for debug
4135 convenience. */
4136 bool any_mismatch = false;
4137
4138 /* Create the expected symbols list (an initializer_list). Needed
4139 because lists have commas, and we need to pass them to CHECK,
4140 which is a macro. */
4141 #define EXPECT(...) { __VA_ARGS__ }
4142
4143 /* Wrapper for check_match that passes down the current
4144 __FILE__/__LINE__. */
4145 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4146 any_mismatch |= !check_match (__FILE__, __LINE__, \
4147 mock_index, \
4148 NAME, MATCH_TYPE, COMPLETION_MODE, \
4149 EXPECTED_LIST, NULL)
4150
4151 /* Identity checks. */
4152 for (const char *sym : test_symbols)
4153 {
4154 /* Should be able to match all existing symbols. */
4155 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4156 EXPECT (sym));
4157
4158 /* Should be able to match all existing symbols with
4159 parameters. */
4160 std::string with_params = std::string (sym) + "(int)";
4161 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4162 EXPECT (sym));
4163
4164 /* Should be able to match all existing symbols with
4165 parameters and qualifiers. */
4166 with_params = std::string (sym) + " ( int ) const";
4167 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4168 EXPECT (sym));
4169
4170 /* This should really find sym, but cp-name-parser.y doesn't
4171 know about lvalue/rvalue qualifiers yet. */
4172 with_params = std::string (sym) + " ( int ) &&";
4173 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4174 {});
4175 }
4176
4177 /* Check that the name matching algorithm for completion doesn't get
4178 confused with Latin1 'ÿ' / 0xff. */
4179 {
4180 static const char str[] = "\377";
4181 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4182 EXPECT ("\377", "\377\377123"));
4183 }
4184
4185 /* Check that the increment-last-char in the matching algorithm for
4186 completion doesn't match "t1_fund" when completing "t1_func". */
4187 {
4188 static const char str[] = "t1_func";
4189 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4190 EXPECT ("t1_func", "t1_func1"));
4191 }
4192
4193 /* Check that completion mode works at each prefix of the expected
4194 symbol name. */
4195 {
4196 static const char str[] = "function(int)";
4197 size_t len = strlen (str);
4198 std::string lookup;
4199
4200 for (size_t i = 1; i < len; i++)
4201 {
4202 lookup.assign (str, i);
4203 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4204 EXPECT ("function"));
4205 }
4206 }
4207
4208 /* While "w" is a prefix of both components, the match function
4209 should still only be called once. */
4210 {
4211 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4212 EXPECT ("w1::w2"));
4213 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4214 EXPECT ("w1::w2"));
4215 }
4216
4217 /* Same, with a "complicated" symbol. */
4218 {
4219 static const char str[] = Z_SYM_NAME;
4220 size_t len = strlen (str);
4221 std::string lookup;
4222
4223 for (size_t i = 1; i < len; i++)
4224 {
4225 lookup.assign (str, i);
4226 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4227 EXPECT (Z_SYM_NAME));
4228 }
4229 }
4230
4231 /* In FULL mode, an incomplete symbol doesn't match. */
4232 {
4233 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4234 {});
4235 }
4236
4237 /* A complete symbol with parameters matches any overload, since the
4238 index has no overload info. */
4239 {
4240 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4241 EXPECT ("std::zfunction", "std::zfunction2"));
4242 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4243 EXPECT ("std::zfunction", "std::zfunction2"));
4244 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4245 EXPECT ("std::zfunction", "std::zfunction2"));
4246 }
4247
4248 /* Check that whitespace is ignored appropriately. A symbol with a
4249 template argument list. */
4250 {
4251 static const char expected[] = "ns::foo<int>";
4252 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4253 EXPECT (expected));
4254 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4255 EXPECT (expected));
4256 }
4257
4258 /* Check that whitespace is ignored appropriately. A symbol with a
4259 template argument list that includes a pointer. */
4260 {
4261 static const char expected[] = "ns::foo<char*>";
4262 /* Try both completion and non-completion modes. */
4263 static const bool completion_mode[2] = {false, true};
4264 for (size_t i = 0; i < 2; i++)
4265 {
4266 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4267 completion_mode[i], EXPECT (expected));
4268 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4269 completion_mode[i], EXPECT (expected));
4270
4271 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4272 completion_mode[i], EXPECT (expected));
4273 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4274 completion_mode[i], EXPECT (expected));
4275 }
4276 }
4277
4278 {
4279 /* Check method qualifiers are ignored. */
4280 static const char expected[] = "ns::foo<char*>";
4281 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4282 symbol_name_match_type::FULL, true, EXPECT (expected));
4283 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4284 symbol_name_match_type::FULL, true, EXPECT (expected));
4285 CHECK_MATCH ("foo < char * > ( int ) const",
4286 symbol_name_match_type::WILD, true, EXPECT (expected));
4287 CHECK_MATCH ("foo < char * > ( int ) &&",
4288 symbol_name_match_type::WILD, true, EXPECT (expected));
4289 }
4290
4291 /* Test lookup names that don't match anything. */
4292 {
4293 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4294 {});
4295
4296 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4297 {});
4298 }
4299
4300 /* Some wild matching tests, exercising "(anonymous namespace)",
4301 which should not be confused with a parameter list. */
4302 {
4303 static const char *syms[] = {
4304 "A::B::C",
4305 "B::C",
4306 "C",
4307 "A :: B :: C ( int )",
4308 "B :: C ( int )",
4309 "C ( int )",
4310 };
4311
4312 for (const char *s : syms)
4313 {
4314 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4315 EXPECT ("(anonymous namespace)::A::B::C"));
4316 }
4317 }
4318
4319 {
4320 static const char expected[] = "ns2::tmpl<int>::foo2";
4321 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4322 EXPECT (expected));
4323 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4324 EXPECT (expected));
4325 }
4326
4327 SELF_CHECK (!any_mismatch);
4328
4329 #undef EXPECT
4330 #undef CHECK_MATCH
4331 }
4332
4333 static void
4334 run_test ()
4335 {
4336 test_mapped_index_find_name_component_bounds ();
4337 test_dw2_expand_symtabs_matching_symbol ();
4338 }
4339
4340 }} // namespace selftests::dw2_expand_symtabs_matching
4341
4342 #endif /* GDB_SELF_TEST */
4343
4344 /* If FILE_MATCHER is NULL or if PER_CU has
4345 dwarf2_per_cu_quick_data::MARK set (see
4346 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4347 EXPANSION_NOTIFY on it. */
4348
4349 static bool
4350 dw2_expand_symtabs_matching_one
4351 (dwarf2_per_cu_data *per_cu,
4352 dwarf2_per_objfile *per_objfile,
4353 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4354 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4355 {
4356 if (file_matcher == NULL || per_cu->v.quick->mark)
4357 {
4358 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4359
4360 compunit_symtab *symtab
4361 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4362 gdb_assert (symtab != nullptr);
4363
4364 if (expansion_notify != NULL && symtab_was_null)
4365 return expansion_notify (symtab);
4366 }
4367 return true;
4368 }
4369
4370 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4371 matched, to expand corresponding CUs that were marked. IDX is the
4372 index of the symbol name that matched. */
4373
4374 static bool
4375 dw2_expand_marked_cus
4376 (dwarf2_per_objfile *per_objfile, offset_type idx,
4377 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4378 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4379 block_search_flags search_flags,
4380 search_domain kind)
4381 {
4382 offset_type vec_len, vec_idx;
4383 bool global_seen = false;
4384 mapped_index &index = *per_objfile->per_bfd->index_table;
4385
4386 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4387 vec_len = vec[0];
4388 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4389 {
4390 offset_type cu_index_and_attrs = vec[vec_idx + 1];
4391 /* This value is only valid for index versions >= 7. */
4392 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4393 gdb_index_symbol_kind symbol_kind =
4394 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4395 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4396 /* Only check the symbol attributes if they're present.
4397 Indices prior to version 7 don't record them,
4398 and indices >= 7 may elide them for certain symbols
4399 (gold does this). */
4400 int attrs_valid =
4401 (index.version >= 7
4402 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4403
4404 /* Work around gold/15646. */
4405 if (attrs_valid
4406 && !is_static
4407 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4408 {
4409 if (global_seen)
4410 continue;
4411
4412 global_seen = true;
4413 }
4414
4415 /* Only check the symbol's kind if it has one. */
4416 if (attrs_valid)
4417 {
4418 if (is_static)
4419 {
4420 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4421 continue;
4422 }
4423 else
4424 {
4425 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4426 continue;
4427 }
4428
4429 switch (kind)
4430 {
4431 case VARIABLES_DOMAIN:
4432 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4433 continue;
4434 break;
4435 case FUNCTIONS_DOMAIN:
4436 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4437 continue;
4438 break;
4439 case TYPES_DOMAIN:
4440 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4441 continue;
4442 break;
4443 case MODULES_DOMAIN:
4444 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4445 continue;
4446 break;
4447 default:
4448 break;
4449 }
4450 }
4451
4452 /* Don't crash on bad data. */
4453 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
4454 {
4455 complaint (_(".gdb_index entry has bad CU index"
4456 " [in module %s]"), objfile_name (per_objfile->objfile));
4457 continue;
4458 }
4459
4460 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
4461 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4462 expansion_notify))
4463 return false;
4464 }
4465
4466 return true;
4467 }
4468
4469 /* If FILE_MATCHER is non-NULL, set all the
4470 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4471 that match FILE_MATCHER. */
4472
4473 static void
4474 dw_expand_symtabs_matching_file_matcher
4475 (dwarf2_per_objfile *per_objfile,
4476 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4477 {
4478 if (file_matcher == NULL)
4479 return;
4480
4481 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4482 htab_eq_pointer,
4483 NULL, xcalloc, xfree));
4484 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4485 htab_eq_pointer,
4486 NULL, xcalloc, xfree));
4487
4488 /* The rule is CUs specify all the files, including those used by
4489 any TU, so there's no need to scan TUs here. */
4490
4491 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4492 {
4493 QUIT;
4494
4495 if (per_cu->is_debug_types)
4496 continue;
4497 per_cu->v.quick->mark = 0;
4498
4499 /* We only need to look at symtabs not already expanded. */
4500 if (per_objfile->symtab_set_p (per_cu.get ()))
4501 continue;
4502
4503 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4504 per_objfile);
4505 if (file_data == NULL)
4506 continue;
4507
4508 if (htab_find (visited_not_found.get (), file_data) != NULL)
4509 continue;
4510 else if (htab_find (visited_found.get (), file_data) != NULL)
4511 {
4512 per_cu->v.quick->mark = 1;
4513 continue;
4514 }
4515
4516 for (int j = 0; j < file_data->num_file_names; ++j)
4517 {
4518 const char *this_real_name;
4519
4520 if (file_matcher (file_data->file_names[j], false))
4521 {
4522 per_cu->v.quick->mark = 1;
4523 break;
4524 }
4525
4526 /* Before we invoke realpath, which can get expensive when many
4527 files are involved, do a quick comparison of the basenames. */
4528 if (!basenames_may_differ
4529 && !file_matcher (lbasename (file_data->file_names[j]),
4530 true))
4531 continue;
4532
4533 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4534 if (file_matcher (this_real_name, false))
4535 {
4536 per_cu->v.quick->mark = 1;
4537 break;
4538 }
4539 }
4540
4541 void **slot = htab_find_slot (per_cu->v.quick->mark
4542 ? visited_found.get ()
4543 : visited_not_found.get (),
4544 file_data, INSERT);
4545 *slot = file_data;
4546 }
4547 }
4548
4549 bool
4550 dwarf2_gdb_index::expand_symtabs_matching
4551 (struct objfile *objfile,
4552 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4553 const lookup_name_info *lookup_name,
4554 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4555 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4556 block_search_flags search_flags,
4557 domain_enum domain,
4558 enum search_domain kind)
4559 {
4560 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4561
4562 /* index_table is NULL if OBJF_READNOW. */
4563 if (!per_objfile->per_bfd->index_table)
4564 return true;
4565
4566 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4567
4568 if (symbol_matcher == NULL && lookup_name == NULL)
4569 {
4570 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4571 {
4572 QUIT;
4573
4574 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
4575 file_matcher,
4576 expansion_notify))
4577 return false;
4578 }
4579 return true;
4580 }
4581
4582 mapped_index &index = *per_objfile->per_bfd->index_table;
4583
4584 bool result
4585 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4586 symbol_matcher,
4587 [&] (offset_type idx)
4588 {
4589 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4590 expansion_notify, search_flags, kind))
4591 return false;
4592 return true;
4593 }, per_objfile);
4594
4595 return result;
4596 }
4597
4598 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4599 symtab. */
4600
4601 static struct compunit_symtab *
4602 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4603 CORE_ADDR pc)
4604 {
4605 int i;
4606
4607 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4608 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4609 return cust;
4610
4611 if (cust->includes == NULL)
4612 return NULL;
4613
4614 for (i = 0; cust->includes[i]; ++i)
4615 {
4616 struct compunit_symtab *s = cust->includes[i];
4617
4618 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4619 if (s != NULL)
4620 return s;
4621 }
4622
4623 return NULL;
4624 }
4625
4626 struct compunit_symtab *
4627 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4628 (struct objfile *objfile,
4629 struct bound_minimal_symbol msymbol,
4630 CORE_ADDR pc,
4631 struct obj_section *section,
4632 int warn_if_readin)
4633 {
4634 struct dwarf2_per_cu_data *data;
4635 struct compunit_symtab *result;
4636
4637 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4638 if (per_objfile->per_bfd->index_addrmap == nullptr)
4639 return NULL;
4640
4641 CORE_ADDR baseaddr = objfile->text_section_offset ();
4642 data = ((struct dwarf2_per_cu_data *)
4643 addrmap_find (per_objfile->per_bfd->index_addrmap,
4644 pc - baseaddr));
4645 if (!data)
4646 return NULL;
4647
4648 if (warn_if_readin && per_objfile->symtab_set_p (data))
4649 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4650 paddress (objfile->arch (), pc));
4651
4652 result = recursively_find_pc_sect_compunit_symtab
4653 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4654
4655 gdb_assert (result != NULL);
4656 return result;
4657 }
4658
4659 void
4660 dwarf2_base_index_functions::map_symbol_filenames
4661 (struct objfile *objfile,
4662 gdb::function_view<symbol_filename_ftype> fun,
4663 bool need_fullname)
4664 {
4665 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4666
4667 /* Use caches to ensure we only call FUN once for each filename. */
4668 filename_seen_cache filenames_cache;
4669 std::unordered_set<quick_file_names *> qfn_cache;
4670
4671 /* The rule is CUs specify all the files, including those used by any TU,
4672 so there's no need to scan TUs here. We can ignore file names coming
4673 from already-expanded CUs. It is possible that an expanded CU might
4674 reuse the file names data from a currently unexpanded CU, in this
4675 case we don't want to report the files from the unexpanded CU. */
4676
4677 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4678 {
4679 if (per_objfile->symtab_set_p (per_cu.get ()))
4680 {
4681 if (per_cu->v.quick->file_names != nullptr)
4682 qfn_cache.insert (per_cu->v.quick->file_names);
4683 }
4684 }
4685
4686 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4687 {
4688 /* We only need to look at symtabs not already expanded. */
4689 if (per_objfile->symtab_set_p (per_cu.get ()))
4690 continue;
4691
4692 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4693 per_objfile);
4694 if (file_data == nullptr
4695 || qfn_cache.find (file_data) != qfn_cache.end ())
4696 continue;
4697
4698 for (int j = 0; j < file_data->num_file_names; ++j)
4699 {
4700 const char *filename = file_data->file_names[j];
4701 filenames_cache.seen (filename);
4702 }
4703 }
4704
4705 filenames_cache.traverse ([&] (const char *filename)
4706 {
4707 gdb::unique_xmalloc_ptr<char> this_real_name;
4708
4709 if (need_fullname)
4710 this_real_name = gdb_realpath (filename);
4711 fun (filename, this_real_name.get ());
4712 });
4713 }
4714
4715 bool
4716 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4717 {
4718 return true;
4719 }
4720
4721 /* DWARF-5 debug_names reader. */
4722
4723 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4724 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4725
4726 /* A helper function that reads the .debug_names section in SECTION
4727 and fills in MAP. FILENAME is the name of the file containing the
4728 section; it is used for error reporting.
4729
4730 Returns true if all went well, false otherwise. */
4731
4732 static bool
4733 read_debug_names_from_section (struct objfile *objfile,
4734 const char *filename,
4735 struct dwarf2_section_info *section,
4736 mapped_debug_names &map)
4737 {
4738 if (section->empty ())
4739 return false;
4740
4741 /* Older elfutils strip versions could keep the section in the main
4742 executable while splitting it for the separate debug info file. */
4743 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4744 return false;
4745
4746 section->read (objfile);
4747
4748 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4749
4750 const gdb_byte *addr = section->buffer;
4751
4752 bfd *const abfd = section->get_bfd_owner ();
4753
4754 unsigned int bytes_read;
4755 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4756 addr += bytes_read;
4757
4758 map.dwarf5_is_dwarf64 = bytes_read != 4;
4759 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4760 if (bytes_read + length != section->size)
4761 {
4762 /* There may be multiple per-CU indices. */
4763 warning (_("Section .debug_names in %s length %s does not match "
4764 "section length %s, ignoring .debug_names."),
4765 filename, plongest (bytes_read + length),
4766 pulongest (section->size));
4767 return false;
4768 }
4769
4770 /* The version number. */
4771 uint16_t version = read_2_bytes (abfd, addr);
4772 addr += 2;
4773 if (version != 5)
4774 {
4775 warning (_("Section .debug_names in %s has unsupported version %d, "
4776 "ignoring .debug_names."),
4777 filename, version);
4778 return false;
4779 }
4780
4781 /* Padding. */
4782 uint16_t padding = read_2_bytes (abfd, addr);
4783 addr += 2;
4784 if (padding != 0)
4785 {
4786 warning (_("Section .debug_names in %s has unsupported padding %d, "
4787 "ignoring .debug_names."),
4788 filename, padding);
4789 return false;
4790 }
4791
4792 /* comp_unit_count - The number of CUs in the CU list. */
4793 map.cu_count = read_4_bytes (abfd, addr);
4794 addr += 4;
4795
4796 /* local_type_unit_count - The number of TUs in the local TU
4797 list. */
4798 map.tu_count = read_4_bytes (abfd, addr);
4799 addr += 4;
4800
4801 /* foreign_type_unit_count - The number of TUs in the foreign TU
4802 list. */
4803 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4804 addr += 4;
4805 if (foreign_tu_count != 0)
4806 {
4807 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4808 "ignoring .debug_names."),
4809 filename, static_cast<unsigned long> (foreign_tu_count));
4810 return false;
4811 }
4812
4813 /* bucket_count - The number of hash buckets in the hash lookup
4814 table. */
4815 map.bucket_count = read_4_bytes (abfd, addr);
4816 addr += 4;
4817
4818 /* name_count - The number of unique names in the index. */
4819 map.name_count = read_4_bytes (abfd, addr);
4820 addr += 4;
4821
4822 /* abbrev_table_size - The size in bytes of the abbreviations
4823 table. */
4824 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4825 addr += 4;
4826
4827 /* augmentation_string_size - The size in bytes of the augmentation
4828 string. This value is rounded up to a multiple of 4. */
4829 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4830 addr += 4;
4831 map.augmentation_is_gdb = ((augmentation_string_size
4832 == sizeof (dwarf5_augmentation))
4833 && memcmp (addr, dwarf5_augmentation,
4834 sizeof (dwarf5_augmentation)) == 0);
4835 augmentation_string_size += (-augmentation_string_size) & 3;
4836 addr += augmentation_string_size;
4837
4838 /* List of CUs */
4839 map.cu_table_reordered = addr;
4840 addr += map.cu_count * map.offset_size;
4841
4842 /* List of Local TUs */
4843 map.tu_table_reordered = addr;
4844 addr += map.tu_count * map.offset_size;
4845
4846 /* Hash Lookup Table */
4847 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4848 addr += map.bucket_count * 4;
4849 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4850 addr += map.name_count * 4;
4851
4852 /* Name Table */
4853 map.name_table_string_offs_reordered = addr;
4854 addr += map.name_count * map.offset_size;
4855 map.name_table_entry_offs_reordered = addr;
4856 addr += map.name_count * map.offset_size;
4857
4858 const gdb_byte *abbrev_table_start = addr;
4859 for (;;)
4860 {
4861 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4862 addr += bytes_read;
4863 if (index_num == 0)
4864 break;
4865
4866 const auto insertpair
4867 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4868 if (!insertpair.second)
4869 {
4870 warning (_("Section .debug_names in %s has duplicate index %s, "
4871 "ignoring .debug_names."),
4872 filename, pulongest (index_num));
4873 return false;
4874 }
4875 mapped_debug_names::index_val &indexval = insertpair.first->second;
4876 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4877 addr += bytes_read;
4878
4879 for (;;)
4880 {
4881 mapped_debug_names::index_val::attr attr;
4882 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4883 addr += bytes_read;
4884 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4885 addr += bytes_read;
4886 if (attr.form == DW_FORM_implicit_const)
4887 {
4888 attr.implicit_const = read_signed_leb128 (abfd, addr,
4889 &bytes_read);
4890 addr += bytes_read;
4891 }
4892 if (attr.dw_idx == 0 && attr.form == 0)
4893 break;
4894 indexval.attr_vec.push_back (std::move (attr));
4895 }
4896 }
4897 if (addr != abbrev_table_start + abbrev_table_size)
4898 {
4899 warning (_("Section .debug_names in %s has abbreviation_table "
4900 "of size %s vs. written as %u, ignoring .debug_names."),
4901 filename, plongest (addr - abbrev_table_start),
4902 abbrev_table_size);
4903 return false;
4904 }
4905 map.entry_pool = addr;
4906
4907 return true;
4908 }
4909
4910 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4911 list. */
4912
4913 static void
4914 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4915 const mapped_debug_names &map,
4916 dwarf2_section_info &section,
4917 bool is_dwz)
4918 {
4919 if (!map.augmentation_is_gdb)
4920 {
4921 for (uint32_t i = 0; i < map.cu_count; ++i)
4922 {
4923 sect_offset sect_off
4924 = (sect_offset) (extract_unsigned_integer
4925 (map.cu_table_reordered + i * map.offset_size,
4926 map.offset_size,
4927 map.dwarf5_byte_order));
4928 /* We don't know the length of the CU, because the CU list in a
4929 .debug_names index can be incomplete, so we can't use the start
4930 of the next CU as end of this CU. We create the CUs here with
4931 length 0, and in cutu_reader::cutu_reader we'll fill in the
4932 actual length. */
4933 std::unique_ptr<dwarf2_per_cu_data> per_cu
4934 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4935 sect_off, 0);
4936 per_bfd->all_comp_units.push_back (std::move (per_cu));
4937 }
4938 return;
4939 }
4940
4941 sect_offset sect_off_prev;
4942 for (uint32_t i = 0; i <= map.cu_count; ++i)
4943 {
4944 sect_offset sect_off_next;
4945 if (i < map.cu_count)
4946 {
4947 sect_off_next
4948 = (sect_offset) (extract_unsigned_integer
4949 (map.cu_table_reordered + i * map.offset_size,
4950 map.offset_size,
4951 map.dwarf5_byte_order));
4952 }
4953 else
4954 sect_off_next = (sect_offset) section.size;
4955 if (i >= 1)
4956 {
4957 const ULONGEST length = sect_off_next - sect_off_prev;
4958 std::unique_ptr<dwarf2_per_cu_data> per_cu
4959 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4960 sect_off_prev, length);
4961 per_bfd->all_comp_units.push_back (std::move (per_cu));
4962 }
4963 sect_off_prev = sect_off_next;
4964 }
4965 }
4966
4967 /* Read the CU list from the mapped index, and use it to create all
4968 the CU objects for this dwarf2_per_objfile. */
4969
4970 static void
4971 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
4972 const mapped_debug_names &map,
4973 const mapped_debug_names &dwz_map)
4974 {
4975 gdb_assert (per_bfd->all_comp_units.empty ());
4976 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4977
4978 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
4979 false /* is_dwz */);
4980
4981 if (dwz_map.cu_count == 0)
4982 return;
4983
4984 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4985 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
4986 true /* is_dwz */);
4987 }
4988
4989 /* Read .debug_names. If everything went ok, initialize the "quick"
4990 elements of all the CUs and return true. Otherwise, return false. */
4991
4992 static bool
4993 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
4994 {
4995 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4996 mapped_debug_names dwz_map;
4997 struct objfile *objfile = per_objfile->objfile;
4998 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4999
5000 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5001 &per_bfd->debug_names, *map))
5002 return false;
5003
5004 /* Don't use the index if it's empty. */
5005 if (map->name_count == 0)
5006 return false;
5007
5008 /* If there is a .dwz file, read it so we can get its CU list as
5009 well. */
5010 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5011 if (dwz != NULL)
5012 {
5013 if (!read_debug_names_from_section (objfile,
5014 bfd_get_filename (dwz->dwz_bfd.get ()),
5015 &dwz->debug_names, dwz_map))
5016 {
5017 warning (_("could not read '.debug_names' section from %s; skipping"),
5018 bfd_get_filename (dwz->dwz_bfd.get ()));
5019 return false;
5020 }
5021 }
5022
5023 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5024
5025 if (map->tu_count != 0)
5026 {
5027 /* We can only handle a single .debug_types when we have an
5028 index. */
5029 if (per_bfd->types.size () != 1)
5030 return false;
5031
5032 dwarf2_section_info *section = &per_bfd->types[0];
5033
5034 create_signatured_type_table_from_debug_names
5035 (per_objfile, *map, section, &per_bfd->abbrev);
5036 }
5037
5038 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5039
5040 per_bfd->debug_names_table = std::move (map);
5041 per_bfd->using_index = 1;
5042 per_bfd->quick_file_names_table =
5043 create_quick_file_names_table (per_bfd->all_comp_units.size ());
5044
5045 return true;
5046 }
5047
5048 /* Type used to manage iterating over all CUs looking for a symbol for
5049 .debug_names. */
5050
5051 class dw2_debug_names_iterator
5052 {
5053 public:
5054 dw2_debug_names_iterator (const mapped_debug_names &map,
5055 block_search_flags block_index,
5056 domain_enum domain,
5057 const char *name, dwarf2_per_objfile *per_objfile)
5058 : m_map (map), m_block_index (block_index), m_domain (domain),
5059 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5060 m_per_objfile (per_objfile)
5061 {}
5062
5063 dw2_debug_names_iterator (const mapped_debug_names &map,
5064 search_domain search, uint32_t namei,
5065 dwarf2_per_objfile *per_objfile,
5066 domain_enum domain = UNDEF_DOMAIN)
5067 : m_map (map),
5068 m_domain (domain),
5069 m_search (search),
5070 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5071 m_per_objfile (per_objfile)
5072 {}
5073
5074 dw2_debug_names_iterator (const mapped_debug_names &map,
5075 block_search_flags block_index, domain_enum domain,
5076 uint32_t namei, dwarf2_per_objfile *per_objfile)
5077 : m_map (map), m_block_index (block_index), m_domain (domain),
5078 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5079 m_per_objfile (per_objfile)
5080 {}
5081
5082 /* Return the next matching CU or NULL if there are no more. */
5083 dwarf2_per_cu_data *next ();
5084
5085 private:
5086 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5087 const char *name,
5088 dwarf2_per_objfile *per_objfile);
5089 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5090 uint32_t namei,
5091 dwarf2_per_objfile *per_objfile);
5092
5093 /* The internalized form of .debug_names. */
5094 const mapped_debug_names &m_map;
5095
5096 /* Restrict the search to these blocks. */
5097 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
5098 | SEARCH_STATIC_BLOCK);
5099
5100 /* The kind of symbol we're looking for. */
5101 const domain_enum m_domain = UNDEF_DOMAIN;
5102 const search_domain m_search = ALL_DOMAIN;
5103
5104 /* The list of CUs from the index entry of the symbol, or NULL if
5105 not found. */
5106 const gdb_byte *m_addr;
5107
5108 dwarf2_per_objfile *m_per_objfile;
5109 };
5110
5111 const char *
5112 mapped_debug_names::namei_to_name
5113 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5114 {
5115 const ULONGEST namei_string_offs
5116 = extract_unsigned_integer ((name_table_string_offs_reordered
5117 + namei * offset_size),
5118 offset_size,
5119 dwarf5_byte_order);
5120 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5121 }
5122
5123 /* Find a slot in .debug_names for the object named NAME. If NAME is
5124 found, return pointer to its pool data. If NAME cannot be found,
5125 return NULL. */
5126
5127 const gdb_byte *
5128 dw2_debug_names_iterator::find_vec_in_debug_names
5129 (const mapped_debug_names &map, const char *name,
5130 dwarf2_per_objfile *per_objfile)
5131 {
5132 int (*cmp) (const char *, const char *);
5133
5134 gdb::unique_xmalloc_ptr<char> without_params;
5135 if (current_language->la_language == language_cplus
5136 || current_language->la_language == language_fortran
5137 || current_language->la_language == language_d)
5138 {
5139 /* NAME is already canonical. Drop any qualifiers as
5140 .debug_names does not contain any. */
5141
5142 if (strchr (name, '(') != NULL)
5143 {
5144 without_params = cp_remove_params (name);
5145 if (without_params != NULL)
5146 name = without_params.get ();
5147 }
5148 }
5149
5150 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5151
5152 const uint32_t full_hash = dwarf5_djb_hash (name);
5153 uint32_t namei
5154 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5155 (map.bucket_table_reordered
5156 + (full_hash % map.bucket_count)), 4,
5157 map.dwarf5_byte_order);
5158 if (namei == 0)
5159 return NULL;
5160 --namei;
5161 if (namei >= map.name_count)
5162 {
5163 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5164 "[in module %s]"),
5165 namei, map.name_count,
5166 objfile_name (per_objfile->objfile));
5167 return NULL;
5168 }
5169
5170 for (;;)
5171 {
5172 const uint32_t namei_full_hash
5173 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5174 (map.hash_table_reordered + namei), 4,
5175 map.dwarf5_byte_order);
5176 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5177 return NULL;
5178
5179 if (full_hash == namei_full_hash)
5180 {
5181 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5182
5183 #if 0 /* An expensive sanity check. */
5184 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5185 {
5186 complaint (_("Wrong .debug_names hash for string at index %u "
5187 "[in module %s]"),
5188 namei, objfile_name (dwarf2_per_objfile->objfile));
5189 return NULL;
5190 }
5191 #endif
5192
5193 if (cmp (namei_string, name) == 0)
5194 {
5195 const ULONGEST namei_entry_offs
5196 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5197 + namei * map.offset_size),
5198 map.offset_size, map.dwarf5_byte_order);
5199 return map.entry_pool + namei_entry_offs;
5200 }
5201 }
5202
5203 ++namei;
5204 if (namei >= map.name_count)
5205 return NULL;
5206 }
5207 }
5208
5209 const gdb_byte *
5210 dw2_debug_names_iterator::find_vec_in_debug_names
5211 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5212 {
5213 if (namei >= map.name_count)
5214 {
5215 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5216 "[in module %s]"),
5217 namei, map.name_count,
5218 objfile_name (per_objfile->objfile));
5219 return NULL;
5220 }
5221
5222 const ULONGEST namei_entry_offs
5223 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5224 + namei * map.offset_size),
5225 map.offset_size, map.dwarf5_byte_order);
5226 return map.entry_pool + namei_entry_offs;
5227 }
5228
5229 /* See dw2_debug_names_iterator. */
5230
5231 dwarf2_per_cu_data *
5232 dw2_debug_names_iterator::next ()
5233 {
5234 if (m_addr == NULL)
5235 return NULL;
5236
5237 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5238 struct objfile *objfile = m_per_objfile->objfile;
5239 bfd *const abfd = objfile->obfd;
5240
5241 again:
5242
5243 unsigned int bytes_read;
5244 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5245 m_addr += bytes_read;
5246 if (abbrev == 0)
5247 return NULL;
5248
5249 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5250 if (indexval_it == m_map.abbrev_map.cend ())
5251 {
5252 complaint (_("Wrong .debug_names undefined abbrev code %s "
5253 "[in module %s]"),
5254 pulongest (abbrev), objfile_name (objfile));
5255 return NULL;
5256 }
5257 const mapped_debug_names::index_val &indexval = indexval_it->second;
5258 enum class symbol_linkage {
5259 unknown,
5260 static_,
5261 extern_,
5262 } symbol_linkage_ = symbol_linkage::unknown;
5263 dwarf2_per_cu_data *per_cu = NULL;
5264 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5265 {
5266 ULONGEST ull;
5267 switch (attr.form)
5268 {
5269 case DW_FORM_implicit_const:
5270 ull = attr.implicit_const;
5271 break;
5272 case DW_FORM_flag_present:
5273 ull = 1;
5274 break;
5275 case DW_FORM_udata:
5276 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5277 m_addr += bytes_read;
5278 break;
5279 case DW_FORM_ref4:
5280 ull = read_4_bytes (abfd, m_addr);
5281 m_addr += 4;
5282 break;
5283 case DW_FORM_ref8:
5284 ull = read_8_bytes (abfd, m_addr);
5285 m_addr += 8;
5286 break;
5287 case DW_FORM_ref_sig8:
5288 ull = read_8_bytes (abfd, m_addr);
5289 m_addr += 8;
5290 break;
5291 default:
5292 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5293 dwarf_form_name (attr.form),
5294 objfile_name (objfile));
5295 return NULL;
5296 }
5297 switch (attr.dw_idx)
5298 {
5299 case DW_IDX_compile_unit:
5300 /* Don't crash on bad data. */
5301 if (ull >= per_bfd->all_comp_units.size ())
5302 {
5303 complaint (_(".debug_names entry has bad CU index %s"
5304 " [in module %s]"),
5305 pulongest (ull),
5306 objfile_name (objfile));
5307 continue;
5308 }
5309 per_cu = per_bfd->get_cu (ull);
5310 break;
5311 case DW_IDX_type_unit:
5312 /* Don't crash on bad data. */
5313 if (ull >= per_bfd->tu_stats.nr_tus)
5314 {
5315 complaint (_(".debug_names entry has bad TU index %s"
5316 " [in module %s]"),
5317 pulongest (ull),
5318 objfile_name (objfile));
5319 continue;
5320 }
5321 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
5322 break;
5323 case DW_IDX_die_offset:
5324 /* In a per-CU index (as opposed to a per-module index), index
5325 entries without CU attribute implicitly refer to the single CU. */
5326 if (per_cu == NULL)
5327 per_cu = per_bfd->get_cu (0);
5328 break;
5329 case DW_IDX_GNU_internal:
5330 if (!m_map.augmentation_is_gdb)
5331 break;
5332 symbol_linkage_ = symbol_linkage::static_;
5333 break;
5334 case DW_IDX_GNU_external:
5335 if (!m_map.augmentation_is_gdb)
5336 break;
5337 symbol_linkage_ = symbol_linkage::extern_;
5338 break;
5339 }
5340 }
5341
5342 /* Skip if already read in. */
5343 if (m_per_objfile->symtab_set_p (per_cu))
5344 goto again;
5345
5346 /* Check static vs global. */
5347 if (symbol_linkage_ != symbol_linkage::unknown)
5348 {
5349 if (symbol_linkage_ == symbol_linkage::static_)
5350 {
5351 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5352 goto again;
5353 }
5354 else
5355 {
5356 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5357 goto again;
5358 }
5359 }
5360
5361 /* Match dw2_symtab_iter_next, symbol_kind
5362 and debug_names::psymbol_tag. */
5363 switch (m_domain)
5364 {
5365 case VAR_DOMAIN:
5366 switch (indexval.dwarf_tag)
5367 {
5368 case DW_TAG_variable:
5369 case DW_TAG_subprogram:
5370 /* Some types are also in VAR_DOMAIN. */
5371 case DW_TAG_typedef:
5372 case DW_TAG_structure_type:
5373 break;
5374 default:
5375 goto again;
5376 }
5377 break;
5378 case STRUCT_DOMAIN:
5379 switch (indexval.dwarf_tag)
5380 {
5381 case DW_TAG_typedef:
5382 case DW_TAG_structure_type:
5383 break;
5384 default:
5385 goto again;
5386 }
5387 break;
5388 case LABEL_DOMAIN:
5389 switch (indexval.dwarf_tag)
5390 {
5391 case 0:
5392 case DW_TAG_variable:
5393 break;
5394 default:
5395 goto again;
5396 }
5397 break;
5398 case MODULE_DOMAIN:
5399 switch (indexval.dwarf_tag)
5400 {
5401 case DW_TAG_module:
5402 break;
5403 default:
5404 goto again;
5405 }
5406 break;
5407 default:
5408 break;
5409 }
5410
5411 /* Match dw2_expand_symtabs_matching, symbol_kind and
5412 debug_names::psymbol_tag. */
5413 switch (m_search)
5414 {
5415 case VARIABLES_DOMAIN:
5416 switch (indexval.dwarf_tag)
5417 {
5418 case DW_TAG_variable:
5419 break;
5420 default:
5421 goto again;
5422 }
5423 break;
5424 case FUNCTIONS_DOMAIN:
5425 switch (indexval.dwarf_tag)
5426 {
5427 case DW_TAG_subprogram:
5428 break;
5429 default:
5430 goto again;
5431 }
5432 break;
5433 case TYPES_DOMAIN:
5434 switch (indexval.dwarf_tag)
5435 {
5436 case DW_TAG_typedef:
5437 case DW_TAG_structure_type:
5438 break;
5439 default:
5440 goto again;
5441 }
5442 break;
5443 case MODULES_DOMAIN:
5444 switch (indexval.dwarf_tag)
5445 {
5446 case DW_TAG_module:
5447 break;
5448 default:
5449 goto again;
5450 }
5451 default:
5452 break;
5453 }
5454
5455 return per_cu;
5456 }
5457
5458 /* This dumps minimal information about .debug_names. It is called
5459 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5460 uses this to verify that .debug_names has been loaded. */
5461
5462 void
5463 dwarf2_debug_names_index::dump (struct objfile *objfile)
5464 {
5465 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5466
5467 gdb_assert (per_objfile->per_bfd->using_index);
5468 printf_filtered (".debug_names:");
5469 if (per_objfile->per_bfd->debug_names_table)
5470 printf_filtered (" exists\n");
5471 else
5472 printf_filtered (" faked for \"readnow\"\n");
5473 printf_filtered ("\n");
5474 }
5475
5476 void
5477 dwarf2_debug_names_index::expand_matching_symbols
5478 (struct objfile *objfile,
5479 const lookup_name_info &name, domain_enum domain,
5480 int global,
5481 symbol_compare_ftype *ordered_compare)
5482 {
5483 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5484
5485 /* debug_names_table is NULL if OBJF_READNOW. */
5486 if (!per_objfile->per_bfd->debug_names_table)
5487 return;
5488
5489 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5490 const block_search_flags block_flags
5491 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5492
5493 const char *match_name = name.ada ().lookup_name ().c_str ();
5494 auto matcher = [&] (const char *symname)
5495 {
5496 if (ordered_compare == nullptr)
5497 return true;
5498 return ordered_compare (symname, match_name) == 0;
5499 };
5500
5501 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5502 [&] (offset_type namei)
5503 {
5504 /* The name was matched, now expand corresponding CUs that were
5505 marked. */
5506 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5507 per_objfile);
5508
5509 struct dwarf2_per_cu_data *per_cu;
5510 while ((per_cu = iter.next ()) != NULL)
5511 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5512 nullptr);
5513 return true;
5514 }, per_objfile);
5515 }
5516
5517 bool
5518 dwarf2_debug_names_index::expand_symtabs_matching
5519 (struct objfile *objfile,
5520 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5521 const lookup_name_info *lookup_name,
5522 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5523 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5524 block_search_flags search_flags,
5525 domain_enum domain,
5526 enum search_domain kind)
5527 {
5528 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5529
5530 /* debug_names_table is NULL if OBJF_READNOW. */
5531 if (!per_objfile->per_bfd->debug_names_table)
5532 return true;
5533
5534 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5535
5536 if (symbol_matcher == NULL && lookup_name == NULL)
5537 {
5538 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
5539 {
5540 QUIT;
5541
5542 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
5543 file_matcher,
5544 expansion_notify))
5545 return false;
5546 }
5547 return true;
5548 }
5549
5550 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5551
5552 bool result
5553 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5554 symbol_matcher,
5555 [&] (offset_type namei)
5556 {
5557 /* The name was matched, now expand corresponding CUs that were
5558 marked. */
5559 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5560
5561 struct dwarf2_per_cu_data *per_cu;
5562 while ((per_cu = iter.next ()) != NULL)
5563 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5564 file_matcher,
5565 expansion_notify))
5566 return false;
5567 return true;
5568 }, per_objfile);
5569
5570 return result;
5571 }
5572
5573 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5574 to either a dwarf2_per_bfd or dwz_file object. */
5575
5576 template <typename T>
5577 static gdb::array_view<const gdb_byte>
5578 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5579 {
5580 dwarf2_section_info *section = &section_owner->gdb_index;
5581
5582 if (section->empty ())
5583 return {};
5584
5585 /* Older elfutils strip versions could keep the section in the main
5586 executable while splitting it for the separate debug info file. */
5587 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5588 return {};
5589
5590 section->read (obj);
5591
5592 /* dwarf2_section_info::size is a bfd_size_type, while
5593 gdb::array_view works with size_t. On 32-bit hosts, with
5594 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5595 is 32-bit. So we need an explicit narrowing conversion here.
5596 This is fine, because it's impossible to allocate or mmap an
5597 array/buffer larger than what size_t can represent. */
5598 return gdb::make_array_view (section->buffer, section->size);
5599 }
5600
5601 /* Lookup the index cache for the contents of the index associated to
5602 DWARF2_OBJ. */
5603
5604 static gdb::array_view<const gdb_byte>
5605 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5606 {
5607 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5608 if (build_id == nullptr)
5609 return {};
5610
5611 return global_index_cache.lookup_gdb_index (build_id,
5612 &dwarf2_per_bfd->index_cache_res);
5613 }
5614
5615 /* Same as the above, but for DWZ. */
5616
5617 static gdb::array_view<const gdb_byte>
5618 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5619 {
5620 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5621 if (build_id == nullptr)
5622 return {};
5623
5624 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5625 }
5626
5627 /* See dwarf2/public.h. */
5628
5629 void
5630 dwarf2_initialize_objfile (struct objfile *objfile)
5631 {
5632 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5633 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5634
5635 dwarf_read_debug_printf ("called");
5636
5637 /* If we're about to read full symbols, don't bother with the
5638 indices. In this case we also don't care if some other debug
5639 format is making psymtabs, because they are all about to be
5640 expanded anyway. */
5641 if ((objfile->flags & OBJF_READNOW))
5642 {
5643 dwarf_read_debug_printf ("readnow requested");
5644
5645 /* When using READNOW, the using_index flag (set below) indicates that
5646 PER_BFD was already initialized, when we loaded some other objfile. */
5647 if (per_bfd->using_index)
5648 {
5649 dwarf_read_debug_printf ("using_index already set");
5650 per_objfile->resize_symtabs ();
5651 objfile->qf.push_front (make_dwarf_gdb_index ());
5652 return;
5653 }
5654
5655 per_bfd->using_index = 1;
5656 create_all_comp_units (per_objfile);
5657 per_bfd->quick_file_names_table
5658 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5659 per_objfile->resize_symtabs ();
5660
5661 for (int i = 0; i < per_bfd->all_comp_units.size (); ++i)
5662 {
5663 dwarf2_per_cu_data *per_cu = per_bfd->get_cu (i);
5664
5665 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
5666 struct dwarf2_per_cu_quick_data);
5667 }
5668
5669 /* Arrange for gdb to see the "quick" functions. However, these
5670 functions will be no-ops because we will have expanded all
5671 symtabs. */
5672 objfile->qf.push_front (make_dwarf_gdb_index ());
5673 return;
5674 }
5675
5676 /* Was a debug names index already read when we processed an objfile sharing
5677 PER_BFD? */
5678 if (per_bfd->debug_names_table != nullptr)
5679 {
5680 dwarf_read_debug_printf ("re-using shared debug names table");
5681 per_objfile->resize_symtabs ();
5682 objfile->qf.push_front (make_dwarf_debug_names ());
5683 return;
5684 }
5685
5686 /* Was a GDB index already read when we processed an objfile sharing
5687 PER_BFD? */
5688 if (per_bfd->index_table != nullptr)
5689 {
5690 dwarf_read_debug_printf ("re-using shared index table");
5691 per_objfile->resize_symtabs ();
5692 objfile->qf.push_front (make_dwarf_gdb_index ());
5693 return;
5694 }
5695
5696 /* There might already be partial symtabs built for this BFD. This happens
5697 when loading the same binary twice with the index-cache enabled. If so,
5698 don't try to read an index. The objfile / per_objfile initialization will
5699 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5700 code path. */
5701 if (per_bfd->partial_symtabs != nullptr)
5702 {
5703 dwarf_read_debug_printf ("re-using shared partial symtabs");
5704 objfile->qf.push_front (make_lazy_dwarf_reader ());
5705 return;
5706 }
5707
5708 if (dwarf2_read_debug_names (per_objfile))
5709 {
5710 dwarf_read_debug_printf ("found debug names");
5711 per_objfile->resize_symtabs ();
5712 objfile->qf.push_front (make_dwarf_debug_names ());
5713 return;
5714 }
5715
5716 if (dwarf2_read_gdb_index (per_objfile,
5717 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5718 get_gdb_index_contents_from_section<dwz_file>))
5719 {
5720 dwarf_read_debug_printf ("found gdb index from file");
5721 per_objfile->resize_symtabs ();
5722 objfile->qf.push_front (make_dwarf_gdb_index ());
5723 return;
5724 }
5725
5726 /* ... otherwise, try to find the index in the index cache. */
5727 if (dwarf2_read_gdb_index (per_objfile,
5728 get_gdb_index_contents_from_cache,
5729 get_gdb_index_contents_from_cache_dwz))
5730 {
5731 dwarf_read_debug_printf ("found gdb index from cache");
5732 global_index_cache.hit ();
5733 per_objfile->resize_symtabs ();
5734 objfile->qf.push_front (make_dwarf_gdb_index ());
5735 return;
5736 }
5737
5738 global_index_cache.miss ();
5739 objfile->qf.push_front (make_lazy_dwarf_reader ());
5740 }
5741
5742 \f
5743
5744 /* Build a partial symbol table. */
5745
5746 void
5747 dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
5748 {
5749 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5750 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5751
5752 if (per_bfd->partial_symtabs != nullptr)
5753 {
5754 /* Partial symbols were already read, so now we can simply
5755 attach them. */
5756 if (psf == nullptr)
5757 {
5758 psf = new psymbol_functions (per_bfd->partial_symtabs);
5759 objfile->qf.emplace_front (psf);
5760 }
5761 else
5762 psf->set_partial_symtabs (per_bfd->partial_symtabs);
5763 per_objfile->resize_symtabs ();
5764 return;
5765 }
5766
5767 if (psf == nullptr)
5768 {
5769 psf = new psymbol_functions;
5770 objfile->qf.emplace_front (psf);
5771 }
5772 const std::shared_ptr<psymtab_storage> &partial_symtabs
5773 = psf->get_partial_symtabs ();
5774
5775 /* Set the local reference to partial symtabs, so that we don't try
5776 to read them again if reading another objfile with the same BFD.
5777 If we can't in fact share, this won't make a difference anyway as
5778 the dwarf2_per_bfd object won't be shared. */
5779 per_bfd->partial_symtabs = partial_symtabs;
5780
5781 try
5782 {
5783 /* This isn't really ideal: all the data we allocate on the
5784 objfile's obstack is still uselessly kept around. However,
5785 freeing it seems unsafe. */
5786 psymtab_discarder psymtabs (partial_symtabs.get ());
5787 dwarf2_build_psymtabs_hard (per_objfile);
5788 psymtabs.keep ();
5789
5790 per_objfile->resize_symtabs ();
5791
5792 /* (maybe) store an index in the cache. */
5793 global_index_cache.store (per_objfile);
5794 }
5795 catch (const gdb_exception_error &except)
5796 {
5797 exception_print (gdb_stderr, except);
5798 }
5799 }
5800
5801 /* Find the base address of the compilation unit for range lists and
5802 location lists. It will normally be specified by DW_AT_low_pc.
5803 In DWARF-3 draft 4, the base address could be overridden by
5804 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5805 compilation units with discontinuous ranges. */
5806
5807 static void
5808 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5809 {
5810 struct attribute *attr;
5811
5812 cu->base_address.reset ();
5813
5814 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5815 if (attr != nullptr)
5816 cu->base_address = attr->as_address ();
5817 else
5818 {
5819 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5820 if (attr != nullptr)
5821 cu->base_address = attr->as_address ();
5822 }
5823 }
5824
5825 /* Helper function that returns the proper abbrev section for
5826 THIS_CU. */
5827
5828 static struct dwarf2_section_info *
5829 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5830 {
5831 struct dwarf2_section_info *abbrev;
5832 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5833
5834 if (this_cu->is_dwz)
5835 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5836 else
5837 abbrev = &per_bfd->abbrev;
5838
5839 return abbrev;
5840 }
5841
5842 /* Fetch the abbreviation table offset from a comp or type unit header. */
5843
5844 static sect_offset
5845 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5846 struct dwarf2_section_info *section,
5847 sect_offset sect_off)
5848 {
5849 bfd *abfd = section->get_bfd_owner ();
5850 const gdb_byte *info_ptr;
5851 unsigned int initial_length_size, offset_size;
5852 uint16_t version;
5853
5854 section->read (per_objfile->objfile);
5855 info_ptr = section->buffer + to_underlying (sect_off);
5856 read_initial_length (abfd, info_ptr, &initial_length_size);
5857 offset_size = initial_length_size == 4 ? 4 : 8;
5858 info_ptr += initial_length_size;
5859
5860 version = read_2_bytes (abfd, info_ptr);
5861 info_ptr += 2;
5862 if (version >= 5)
5863 {
5864 /* Skip unit type and address size. */
5865 info_ptr += 2;
5866 }
5867
5868 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5869 }
5870
5871 /* A partial symtab that is used only for include files. */
5872 struct dwarf2_include_psymtab : public partial_symtab
5873 {
5874 dwarf2_include_psymtab (const char *filename,
5875 psymtab_storage *partial_symtabs,
5876 objfile_per_bfd_storage *objfile_per_bfd)
5877 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
5878 {
5879 }
5880
5881 void read_symtab (struct objfile *objfile) override
5882 {
5883 /* It's an include file, no symbols to read for it.
5884 Everything is in the includer symtab. */
5885
5886 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5887 expansion of the includer psymtab. We use the dependencies[0] field to
5888 model the includer. But if we go the regular route of calling
5889 expand_psymtab here, and having expand_psymtab call expand_dependencies
5890 to expand the includer, we'll only use expand_psymtab on the includer
5891 (making it a non-toplevel psymtab), while if we expand the includer via
5892 another path, we'll use read_symtab (making it a toplevel psymtab).
5893 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5894 psymtab, and trigger read_symtab on the includer here directly. */
5895 includer ()->read_symtab (objfile);
5896 }
5897
5898 void expand_psymtab (struct objfile *objfile) override
5899 {
5900 /* This is not called by read_symtab, and should not be called by any
5901 expand_dependencies. */
5902 gdb_assert (false);
5903 }
5904
5905 bool readin_p (struct objfile *objfile) const override
5906 {
5907 return includer ()->readin_p (objfile);
5908 }
5909
5910 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
5911 {
5912 return nullptr;
5913 }
5914
5915 private:
5916 partial_symtab *includer () const
5917 {
5918 /* An include psymtab has exactly one dependency: the psymtab that
5919 includes it. */
5920 gdb_assert (this->number_of_dependencies == 1);
5921 return this->dependencies[0];
5922 }
5923 };
5924
5925 /* Allocate a new partial symtab for file named NAME and mark this new
5926 partial symtab as being an include of PST. */
5927
5928 static void
5929 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
5930 const char *name,
5931 dwarf2_psymtab *pst,
5932 psymtab_storage *partial_symtabs,
5933 objfile_per_bfd_storage *objfile_per_bfd)
5934 {
5935 dwarf2_include_psymtab *subpst
5936 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
5937
5938 if (!IS_ABSOLUTE_PATH (subpst->filename))
5939 subpst->dirname = pst->dirname;
5940
5941 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
5942 subpst->dependencies[0] = pst;
5943 subpst->number_of_dependencies = 1;
5944 }
5945
5946 /* Read the Line Number Program data and extract the list of files
5947 included by the source file represented by PST. Build an include
5948 partial symtab for each of these included files. */
5949
5950 static void
5951 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5952 struct die_info *die,
5953 dwarf2_psymtab *pst)
5954 {
5955 line_header_up lh;
5956 struct attribute *attr;
5957
5958 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5959 if (attr != nullptr && attr->form_is_unsigned ())
5960 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
5961 if (lh == NULL)
5962 return; /* No linetable, so no includes. */
5963
5964 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5965 that we pass in the raw text_low here; that is ok because we're
5966 only decoding the line table to make include partial symtabs, and
5967 so the addresses aren't really used. */
5968 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5969 pst->raw_text_low (), 1);
5970 }
5971
5972 static hashval_t
5973 hash_signatured_type (const void *item)
5974 {
5975 const struct signatured_type *sig_type
5976 = (const struct signatured_type *) item;
5977
5978 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5979 return sig_type->signature;
5980 }
5981
5982 static int
5983 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5984 {
5985 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5986 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5987
5988 return lhs->signature == rhs->signature;
5989 }
5990
5991 /* Allocate a hash table for signatured types. */
5992
5993 static htab_up
5994 allocate_signatured_type_table ()
5995 {
5996 return htab_up (htab_create_alloc (41,
5997 hash_signatured_type,
5998 eq_signatured_type,
5999 NULL, xcalloc, xfree));
6000 }
6001
6002 /* A helper for create_debug_types_hash_table. Read types from SECTION
6003 and fill them into TYPES_HTAB. It will process only type units,
6004 therefore DW_UT_type. */
6005
6006 static void
6007 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6008 struct dwo_file *dwo_file,
6009 dwarf2_section_info *section, htab_up &types_htab,
6010 rcuh_kind section_kind)
6011 {
6012 struct objfile *objfile = per_objfile->objfile;
6013 struct dwarf2_section_info *abbrev_section;
6014 bfd *abfd;
6015 const gdb_byte *info_ptr, *end_ptr;
6016
6017 abbrev_section = &dwo_file->sections.abbrev;
6018
6019 dwarf_read_debug_printf ("Reading %s for %s",
6020 section->get_name (),
6021 abbrev_section->get_file_name ());
6022
6023 section->read (objfile);
6024 info_ptr = section->buffer;
6025
6026 if (info_ptr == NULL)
6027 return;
6028
6029 /* We can't set abfd until now because the section may be empty or
6030 not present, in which case the bfd is unknown. */
6031 abfd = section->get_bfd_owner ();
6032
6033 /* We don't use cutu_reader here because we don't need to read
6034 any dies: the signature is in the header. */
6035
6036 end_ptr = info_ptr + section->size;
6037 while (info_ptr < end_ptr)
6038 {
6039 std::unique_ptr<signatured_type> sig_type;
6040 struct dwo_unit *dwo_tu;
6041 void **slot;
6042 const gdb_byte *ptr = info_ptr;
6043 struct comp_unit_head header;
6044 unsigned int length;
6045
6046 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6047
6048 /* Initialize it due to a false compiler warning. */
6049 header.signature = -1;
6050 header.type_cu_offset_in_tu = (cu_offset) -1;
6051
6052 /* We need to read the type's signature in order to build the hash
6053 table, but we don't need anything else just yet. */
6054
6055 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6056 abbrev_section, ptr, section_kind);
6057
6058 length = header.get_length ();
6059
6060 /* Skip dummy type units. */
6061 if (ptr >= info_ptr + length
6062 || peek_abbrev_code (abfd, ptr) == 0
6063 || (header.unit_type != DW_UT_type
6064 && header.unit_type != DW_UT_split_type))
6065 {
6066 info_ptr += length;
6067 continue;
6068 }
6069
6070 if (types_htab == NULL)
6071 types_htab = allocate_dwo_unit_table ();
6072
6073 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6074 dwo_tu->dwo_file = dwo_file;
6075 dwo_tu->signature = header.signature;
6076 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6077 dwo_tu->section = section;
6078 dwo_tu->sect_off = sect_off;
6079 dwo_tu->length = length;
6080
6081 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
6082 gdb_assert (slot != NULL);
6083 if (*slot != NULL)
6084 complaint (_("debug type entry at offset %s is duplicate to"
6085 " the entry at offset %s, signature %s"),
6086 sect_offset_str (sect_off),
6087 sect_offset_str (dwo_tu->sect_off),
6088 hex_string (header.signature));
6089 *slot = dwo_tu;
6090
6091 dwarf_read_debug_printf_v (" offset %s, signature %s",
6092 sect_offset_str (sect_off),
6093 hex_string (header.signature));
6094
6095 info_ptr += length;
6096 }
6097 }
6098
6099 /* Create the hash table of all entries in the .debug_types
6100 (or .debug_types.dwo) section(s).
6101 DWO_FILE is a pointer to the DWO file object.
6102
6103 The result is a pointer to the hash table or NULL if there are no types.
6104
6105 Note: This function processes DWO files only, not DWP files. */
6106
6107 static void
6108 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6109 struct dwo_file *dwo_file,
6110 gdb::array_view<dwarf2_section_info> type_sections,
6111 htab_up &types_htab)
6112 {
6113 for (dwarf2_section_info &section : type_sections)
6114 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6115 rcuh_kind::TYPE);
6116 }
6117
6118 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6119 If SLOT is non-NULL, it is the entry to use in the hash table.
6120 Otherwise we find one. */
6121
6122 static struct signatured_type *
6123 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6124 {
6125 if (per_objfile->per_bfd->all_comp_units.size ()
6126 == per_objfile->per_bfd->all_comp_units.capacity ())
6127 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6128
6129 std::unique_ptr<signatured_type> sig_type_holder
6130 = per_objfile->per_bfd->allocate_signatured_type ();
6131 signatured_type *sig_type = sig_type_holder.get ();
6132
6133 per_objfile->resize_symtabs ();
6134
6135 per_objfile->per_bfd->all_comp_units.push_back (std::move (sig_type_holder));
6136 sig_type->signature = sig;
6137 sig_type->is_debug_types = 1;
6138 if (per_objfile->per_bfd->using_index)
6139 {
6140 sig_type->v.quick =
6141 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6142 struct dwarf2_per_cu_quick_data);
6143 }
6144
6145 if (slot == NULL)
6146 {
6147 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6148 sig_type, INSERT);
6149 }
6150 gdb_assert (*slot == NULL);
6151 *slot = sig_type;
6152 /* The rest of sig_type must be filled in by the caller. */
6153 return sig_type;
6154 }
6155
6156 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6157 Fill in SIG_ENTRY with DWO_ENTRY. */
6158
6159 static void
6160 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6161 struct signatured_type *sig_entry,
6162 struct dwo_unit *dwo_entry)
6163 {
6164 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6165
6166 /* Make sure we're not clobbering something we don't expect to. */
6167 gdb_assert (! sig_entry->queued);
6168 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
6169 if (per_bfd->using_index)
6170 {
6171 gdb_assert (sig_entry->v.quick != NULL);
6172 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
6173 }
6174 else
6175 gdb_assert (sig_entry->v.psymtab == NULL);
6176 gdb_assert (sig_entry->signature == dwo_entry->signature);
6177 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6178 gdb_assert (sig_entry->type_unit_group == NULL);
6179 gdb_assert (sig_entry->dwo_unit == NULL);
6180
6181 sig_entry->section = dwo_entry->section;
6182 sig_entry->sect_off = dwo_entry->sect_off;
6183 sig_entry->length = dwo_entry->length;
6184 sig_entry->reading_dwo_directly = 1;
6185 sig_entry->per_bfd = per_bfd;
6186 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6187 sig_entry->dwo_unit = dwo_entry;
6188 }
6189
6190 /* Subroutine of lookup_signatured_type.
6191 If we haven't read the TU yet, create the signatured_type data structure
6192 for a TU to be read in directly from a DWO file, bypassing the stub.
6193 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6194 using .gdb_index, then when reading a CU we want to stay in the DWO file
6195 containing that CU. Otherwise we could end up reading several other DWO
6196 files (due to comdat folding) to process the transitive closure of all the
6197 mentioned TUs, and that can be slow. The current DWO file will have every
6198 type signature that it needs.
6199 We only do this for .gdb_index because in the psymtab case we already have
6200 to read all the DWOs to build the type unit groups. */
6201
6202 static struct signatured_type *
6203 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6204 {
6205 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6206 struct dwo_file *dwo_file;
6207 struct dwo_unit find_dwo_entry, *dwo_entry;
6208 struct signatured_type find_sig_entry, *sig_entry;
6209 void **slot;
6210
6211 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6212
6213 /* If TU skeletons have been removed then we may not have read in any
6214 TUs yet. */
6215 if (per_objfile->per_bfd->signatured_types == NULL)
6216 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6217
6218 /* We only ever need to read in one copy of a signatured type.
6219 Use the global signatured_types array to do our own comdat-folding
6220 of types. If this is the first time we're reading this TU, and
6221 the TU has an entry in .gdb_index, replace the recorded data from
6222 .gdb_index with this TU. */
6223
6224 find_sig_entry.signature = sig;
6225 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6226 &find_sig_entry, INSERT);
6227 sig_entry = (struct signatured_type *) *slot;
6228
6229 /* We can get here with the TU already read, *or* in the process of being
6230 read. Don't reassign the global entry to point to this DWO if that's
6231 the case. Also note that if the TU is already being read, it may not
6232 have come from a DWO, the program may be a mix of Fission-compiled
6233 code and non-Fission-compiled code. */
6234
6235 /* Have we already tried to read this TU?
6236 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6237 needn't exist in the global table yet). */
6238 if (sig_entry != NULL && sig_entry->tu_read)
6239 return sig_entry;
6240
6241 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6242 dwo_unit of the TU itself. */
6243 dwo_file = cu->dwo_unit->dwo_file;
6244
6245 /* Ok, this is the first time we're reading this TU. */
6246 if (dwo_file->tus == NULL)
6247 return NULL;
6248 find_dwo_entry.signature = sig;
6249 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6250 &find_dwo_entry);
6251 if (dwo_entry == NULL)
6252 return NULL;
6253
6254 /* If the global table doesn't have an entry for this TU, add one. */
6255 if (sig_entry == NULL)
6256 sig_entry = add_type_unit (per_objfile, sig, slot);
6257
6258 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6259 sig_entry->tu_read = 1;
6260 return sig_entry;
6261 }
6262
6263 /* Subroutine of lookup_signatured_type.
6264 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6265 then try the DWP file. If the TU stub (skeleton) has been removed then
6266 it won't be in .gdb_index. */
6267
6268 static struct signatured_type *
6269 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6270 {
6271 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6272 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6273 struct dwo_unit *dwo_entry;
6274 struct signatured_type find_sig_entry, *sig_entry;
6275 void **slot;
6276
6277 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6278 gdb_assert (dwp_file != NULL);
6279
6280 /* If TU skeletons have been removed then we may not have read in any
6281 TUs yet. */
6282 if (per_objfile->per_bfd->signatured_types == NULL)
6283 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6284
6285 find_sig_entry.signature = sig;
6286 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6287 &find_sig_entry, INSERT);
6288 sig_entry = (struct signatured_type *) *slot;
6289
6290 /* Have we already tried to read this TU?
6291 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6292 needn't exist in the global table yet). */
6293 if (sig_entry != NULL)
6294 return sig_entry;
6295
6296 if (dwp_file->tus == NULL)
6297 return NULL;
6298 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6299 1 /* is_debug_types */);
6300 if (dwo_entry == NULL)
6301 return NULL;
6302
6303 sig_entry = add_type_unit (per_objfile, sig, slot);
6304 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6305
6306 return sig_entry;
6307 }
6308
6309 /* Lookup a signature based type for DW_FORM_ref_sig8.
6310 Returns NULL if signature SIG is not present in the table.
6311 It is up to the caller to complain about this. */
6312
6313 static struct signatured_type *
6314 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6315 {
6316 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6317
6318 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6319 {
6320 /* We're in a DWO/DWP file, and we're using .gdb_index.
6321 These cases require special processing. */
6322 if (get_dwp_file (per_objfile) == NULL)
6323 return lookup_dwo_signatured_type (cu, sig);
6324 else
6325 return lookup_dwp_signatured_type (cu, sig);
6326 }
6327 else
6328 {
6329 struct signatured_type find_entry, *entry;
6330
6331 if (per_objfile->per_bfd->signatured_types == NULL)
6332 return NULL;
6333 find_entry.signature = sig;
6334 entry = ((struct signatured_type *)
6335 htab_find (per_objfile->per_bfd->signatured_types.get (),
6336 &find_entry));
6337 return entry;
6338 }
6339 }
6340
6341 /* Low level DIE reading support. */
6342
6343 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6344
6345 static void
6346 init_cu_die_reader (struct die_reader_specs *reader,
6347 struct dwarf2_cu *cu,
6348 struct dwarf2_section_info *section,
6349 struct dwo_file *dwo_file,
6350 struct abbrev_table *abbrev_table)
6351 {
6352 gdb_assert (section->readin && section->buffer != NULL);
6353 reader->abfd = section->get_bfd_owner ();
6354 reader->cu = cu;
6355 reader->dwo_file = dwo_file;
6356 reader->die_section = section;
6357 reader->buffer = section->buffer;
6358 reader->buffer_end = section->buffer + section->size;
6359 reader->abbrev_table = abbrev_table;
6360 }
6361
6362 /* Subroutine of cutu_reader to simplify it.
6363 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6364 There's just a lot of work to do, and cutu_reader is big enough
6365 already.
6366
6367 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6368 from it to the DIE in the DWO. If NULL we are skipping the stub.
6369 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6370 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6371 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6372 STUB_COMP_DIR may be non-NULL.
6373 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6374 are filled in with the info of the DIE from the DWO file.
6375 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6376 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6377 kept around for at least as long as *RESULT_READER.
6378
6379 The result is non-zero if a valid (non-dummy) DIE was found. */
6380
6381 static int
6382 read_cutu_die_from_dwo (dwarf2_cu *cu,
6383 struct dwo_unit *dwo_unit,
6384 struct die_info *stub_comp_unit_die,
6385 const char *stub_comp_dir,
6386 struct die_reader_specs *result_reader,
6387 const gdb_byte **result_info_ptr,
6388 struct die_info **result_comp_unit_die,
6389 abbrev_table_up *result_dwo_abbrev_table)
6390 {
6391 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6392 dwarf2_per_cu_data *per_cu = cu->per_cu;
6393 struct objfile *objfile = per_objfile->objfile;
6394 bfd *abfd;
6395 const gdb_byte *begin_info_ptr, *info_ptr;
6396 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6397 int i,num_extra_attrs;
6398 struct dwarf2_section_info *dwo_abbrev_section;
6399 struct die_info *comp_unit_die;
6400
6401 /* At most one of these may be provided. */
6402 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6403
6404 /* These attributes aren't processed until later:
6405 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6406 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6407 referenced later. However, these attributes are found in the stub
6408 which we won't have later. In order to not impose this complication
6409 on the rest of the code, we read them here and copy them to the
6410 DWO CU/TU die. */
6411
6412 stmt_list = NULL;
6413 low_pc = NULL;
6414 high_pc = NULL;
6415 ranges = NULL;
6416 comp_dir = NULL;
6417
6418 if (stub_comp_unit_die != NULL)
6419 {
6420 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6421 DWO file. */
6422 if (!per_cu->is_debug_types)
6423 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6424 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6425 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6426 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6427 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6428
6429 cu->addr_base = stub_comp_unit_die->addr_base ();
6430
6431 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6432 We need the value before we can process DW_AT_ranges values from the
6433 DWO. */
6434 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6435
6436 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6437 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6438 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6439 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6440 section. */
6441 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6442 }
6443 else if (stub_comp_dir != NULL)
6444 {
6445 /* Reconstruct the comp_dir attribute to simplify the code below. */
6446 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6447 comp_dir->name = DW_AT_comp_dir;
6448 comp_dir->form = DW_FORM_string;
6449 comp_dir->set_string_noncanonical (stub_comp_dir);
6450 }
6451
6452 /* Set up for reading the DWO CU/TU. */
6453 cu->dwo_unit = dwo_unit;
6454 dwarf2_section_info *section = dwo_unit->section;
6455 section->read (objfile);
6456 abfd = section->get_bfd_owner ();
6457 begin_info_ptr = info_ptr = (section->buffer
6458 + to_underlying (dwo_unit->sect_off));
6459 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6460
6461 if (per_cu->is_debug_types)
6462 {
6463 signatured_type *sig_type = (struct signatured_type *) per_cu;
6464
6465 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6466 section, dwo_abbrev_section,
6467 info_ptr, rcuh_kind::TYPE);
6468 /* This is not an assert because it can be caused by bad debug info. */
6469 if (sig_type->signature != cu->header.signature)
6470 {
6471 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6472 " TU at offset %s [in module %s]"),
6473 hex_string (sig_type->signature),
6474 hex_string (cu->header.signature),
6475 sect_offset_str (dwo_unit->sect_off),
6476 bfd_get_filename (abfd));
6477 }
6478 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6479 /* For DWOs coming from DWP files, we don't know the CU length
6480 nor the type's offset in the TU until now. */
6481 dwo_unit->length = cu->header.get_length ();
6482 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6483
6484 /* Establish the type offset that can be used to lookup the type.
6485 For DWO files, we don't know it until now. */
6486 sig_type->type_offset_in_section
6487 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6488 }
6489 else
6490 {
6491 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6492 section, dwo_abbrev_section,
6493 info_ptr, rcuh_kind::COMPILE);
6494 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6495 /* For DWOs coming from DWP files, we don't know the CU length
6496 until now. */
6497 dwo_unit->length = cu->header.get_length ();
6498 }
6499
6500 dwo_abbrev_section->read (objfile);
6501 *result_dwo_abbrev_table
6502 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6503 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6504 result_dwo_abbrev_table->get ());
6505
6506 /* Read in the die, but leave space to copy over the attributes
6507 from the stub. This has the benefit of simplifying the rest of
6508 the code - all the work to maintain the illusion of a single
6509 DW_TAG_{compile,type}_unit DIE is done here. */
6510 num_extra_attrs = ((stmt_list != NULL)
6511 + (low_pc != NULL)
6512 + (high_pc != NULL)
6513 + (ranges != NULL)
6514 + (comp_dir != NULL));
6515 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6516 num_extra_attrs);
6517
6518 /* Copy over the attributes from the stub to the DIE we just read in. */
6519 comp_unit_die = *result_comp_unit_die;
6520 i = comp_unit_die->num_attrs;
6521 if (stmt_list != NULL)
6522 comp_unit_die->attrs[i++] = *stmt_list;
6523 if (low_pc != NULL)
6524 comp_unit_die->attrs[i++] = *low_pc;
6525 if (high_pc != NULL)
6526 comp_unit_die->attrs[i++] = *high_pc;
6527 if (ranges != NULL)
6528 comp_unit_die->attrs[i++] = *ranges;
6529 if (comp_dir != NULL)
6530 comp_unit_die->attrs[i++] = *comp_dir;
6531 comp_unit_die->num_attrs += num_extra_attrs;
6532
6533 if (dwarf_die_debug)
6534 {
6535 fprintf_unfiltered (gdb_stdlog,
6536 "Read die from %s@0x%x of %s:\n",
6537 section->get_name (),
6538 (unsigned) (begin_info_ptr - section->buffer),
6539 bfd_get_filename (abfd));
6540 dump_die (comp_unit_die, dwarf_die_debug);
6541 }
6542
6543 /* Skip dummy compilation units. */
6544 if (info_ptr >= begin_info_ptr + dwo_unit->length
6545 || peek_abbrev_code (abfd, info_ptr) == 0)
6546 return 0;
6547
6548 *result_info_ptr = info_ptr;
6549 return 1;
6550 }
6551
6552 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6553 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6554 signature is part of the header. */
6555 static gdb::optional<ULONGEST>
6556 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6557 {
6558 if (cu->header.version >= 5)
6559 return cu->header.signature;
6560 struct attribute *attr;
6561 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6562 if (attr == nullptr || !attr->form_is_unsigned ())
6563 return gdb::optional<ULONGEST> ();
6564 return attr->as_unsigned ();
6565 }
6566
6567 /* Subroutine of cutu_reader to simplify it.
6568 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6569 Returns NULL if the specified DWO unit cannot be found. */
6570
6571 static struct dwo_unit *
6572 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6573 {
6574 dwarf2_per_cu_data *per_cu = cu->per_cu;
6575 struct dwo_unit *dwo_unit;
6576 const char *comp_dir;
6577
6578 gdb_assert (cu != NULL);
6579
6580 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6581 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6582 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6583
6584 if (per_cu->is_debug_types)
6585 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6586 else
6587 {
6588 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6589
6590 if (!signature.has_value ())
6591 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6592 " [in module %s]"),
6593 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6594
6595 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6596 }
6597
6598 return dwo_unit;
6599 }
6600
6601 /* Subroutine of cutu_reader to simplify it.
6602 See it for a description of the parameters.
6603 Read a TU directly from a DWO file, bypassing the stub. */
6604
6605 void
6606 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6607 dwarf2_per_objfile *per_objfile,
6608 dwarf2_cu *existing_cu)
6609 {
6610 struct signatured_type *sig_type;
6611
6612 /* Verify we can do the following downcast, and that we have the
6613 data we need. */
6614 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6615 sig_type = (struct signatured_type *) this_cu;
6616 gdb_assert (sig_type->dwo_unit != NULL);
6617
6618 dwarf2_cu *cu;
6619
6620 if (existing_cu != nullptr)
6621 {
6622 cu = existing_cu;
6623 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6624 /* There's no need to do the rereading_dwo_cu handling that
6625 cutu_reader does since we don't read the stub. */
6626 }
6627 else
6628 {
6629 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6630 in per_objfile yet. */
6631 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6632 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6633 cu = m_new_cu.get ();
6634 }
6635
6636 /* A future optimization, if needed, would be to use an existing
6637 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6638 could share abbrev tables. */
6639
6640 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6641 NULL /* stub_comp_unit_die */,
6642 sig_type->dwo_unit->dwo_file->comp_dir,
6643 this, &info_ptr,
6644 &comp_unit_die,
6645 &m_dwo_abbrev_table) == 0)
6646 {
6647 /* Dummy die. */
6648 dummy_p = true;
6649 }
6650 }
6651
6652 /* Initialize a CU (or TU) and read its DIEs.
6653 If the CU defers to a DWO file, read the DWO file as well.
6654
6655 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6656 Otherwise the table specified in the comp unit header is read in and used.
6657 This is an optimization for when we already have the abbrev table.
6658
6659 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6660 allocated. */
6661
6662 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6663 dwarf2_per_objfile *per_objfile,
6664 struct abbrev_table *abbrev_table,
6665 dwarf2_cu *existing_cu,
6666 bool skip_partial)
6667 : die_reader_specs {},
6668 m_this_cu (this_cu)
6669 {
6670 struct objfile *objfile = per_objfile->objfile;
6671 struct dwarf2_section_info *section = this_cu->section;
6672 bfd *abfd = section->get_bfd_owner ();
6673 const gdb_byte *begin_info_ptr;
6674 struct signatured_type *sig_type = NULL;
6675 struct dwarf2_section_info *abbrev_section;
6676 /* Non-zero if CU currently points to a DWO file and we need to
6677 reread it. When this happens we need to reread the skeleton die
6678 before we can reread the DWO file (this only applies to CUs, not TUs). */
6679 int rereading_dwo_cu = 0;
6680
6681 if (dwarf_die_debug)
6682 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6683 this_cu->is_debug_types ? "type" : "comp",
6684 sect_offset_str (this_cu->sect_off));
6685
6686 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6687 file (instead of going through the stub), short-circuit all of this. */
6688 if (this_cu->reading_dwo_directly)
6689 {
6690 /* Narrow down the scope of possibilities to have to understand. */
6691 gdb_assert (this_cu->is_debug_types);
6692 gdb_assert (abbrev_table == NULL);
6693 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6694 return;
6695 }
6696
6697 /* This is cheap if the section is already read in. */
6698 section->read (objfile);
6699
6700 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6701
6702 abbrev_section = get_abbrev_section_for_cu (this_cu);
6703
6704 dwarf2_cu *cu;
6705
6706 if (existing_cu != nullptr)
6707 {
6708 cu = existing_cu;
6709 /* If this CU is from a DWO file we need to start over, we need to
6710 refetch the attributes from the skeleton CU.
6711 This could be optimized by retrieving those attributes from when we
6712 were here the first time: the previous comp_unit_die was stored in
6713 comp_unit_obstack. But there's no data yet that we need this
6714 optimization. */
6715 if (cu->dwo_unit != NULL)
6716 rereading_dwo_cu = 1;
6717 }
6718 else
6719 {
6720 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6721 in per_objfile yet. */
6722 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6723 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6724 cu = m_new_cu.get ();
6725 }
6726
6727 /* Get the header. */
6728 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6729 {
6730 /* We already have the header, there's no need to read it in again. */
6731 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6732 }
6733 else
6734 {
6735 if (this_cu->is_debug_types)
6736 {
6737 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6738 section, abbrev_section,
6739 info_ptr, rcuh_kind::TYPE);
6740
6741 /* Since per_cu is the first member of struct signatured_type,
6742 we can go from a pointer to one to a pointer to the other. */
6743 sig_type = (struct signatured_type *) this_cu;
6744 gdb_assert (sig_type->signature == cu->header.signature);
6745 gdb_assert (sig_type->type_offset_in_tu
6746 == cu->header.type_cu_offset_in_tu);
6747 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6748
6749 /* LENGTH has not been set yet for type units if we're
6750 using .gdb_index. */
6751 this_cu->length = cu->header.get_length ();
6752
6753 /* Establish the type offset that can be used to lookup the type. */
6754 sig_type->type_offset_in_section =
6755 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6756
6757 this_cu->dwarf_version = cu->header.version;
6758 }
6759 else
6760 {
6761 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6762 section, abbrev_section,
6763 info_ptr,
6764 rcuh_kind::COMPILE);
6765
6766 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6767 if (this_cu->length == 0)
6768 this_cu->length = cu->header.get_length ();
6769 else
6770 gdb_assert (this_cu->length == cu->header.get_length ());
6771 this_cu->dwarf_version = cu->header.version;
6772 }
6773 }
6774
6775 /* Skip dummy compilation units. */
6776 if (info_ptr >= begin_info_ptr + this_cu->length
6777 || peek_abbrev_code (abfd, info_ptr) == 0)
6778 {
6779 dummy_p = true;
6780 return;
6781 }
6782
6783 /* If we don't have them yet, read the abbrevs for this compilation unit.
6784 And if we need to read them now, make sure they're freed when we're
6785 done. */
6786 if (abbrev_table != NULL)
6787 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6788 else
6789 {
6790 abbrev_section->read (objfile);
6791 m_abbrev_table_holder
6792 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6793 abbrev_table = m_abbrev_table_holder.get ();
6794 }
6795
6796 /* Read the top level CU/TU die. */
6797 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6798 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6799
6800 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6801 {
6802 dummy_p = true;
6803 return;
6804 }
6805
6806 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6807 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6808 table from the DWO file and pass the ownership over to us. It will be
6809 referenced from READER, so we must make sure to free it after we're done
6810 with READER.
6811
6812 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6813 DWO CU, that this test will fail (the attribute will not be present). */
6814 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6815 if (dwo_name != nullptr)
6816 {
6817 struct dwo_unit *dwo_unit;
6818 struct die_info *dwo_comp_unit_die;
6819
6820 if (comp_unit_die->has_children)
6821 {
6822 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6823 " has children (offset %s) [in module %s]"),
6824 sect_offset_str (this_cu->sect_off),
6825 bfd_get_filename (abfd));
6826 }
6827 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6828 if (dwo_unit != NULL)
6829 {
6830 if (read_cutu_die_from_dwo (cu, dwo_unit,
6831 comp_unit_die, NULL,
6832 this, &info_ptr,
6833 &dwo_comp_unit_die,
6834 &m_dwo_abbrev_table) == 0)
6835 {
6836 /* Dummy die. */
6837 dummy_p = true;
6838 return;
6839 }
6840 comp_unit_die = dwo_comp_unit_die;
6841 }
6842 else
6843 {
6844 /* Yikes, we couldn't find the rest of the DIE, we only have
6845 the stub. A complaint has already been logged. There's
6846 not much more we can do except pass on the stub DIE to
6847 die_reader_func. We don't want to throw an error on bad
6848 debug info. */
6849 }
6850 }
6851 }
6852
6853 void
6854 cutu_reader::keep ()
6855 {
6856 /* Done, clean up. */
6857 gdb_assert (!dummy_p);
6858 if (m_new_cu != NULL)
6859 {
6860 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6861 now. */
6862 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6863 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
6864 }
6865 }
6866
6867 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6868 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6869 assumed to have already done the lookup to find the DWO file).
6870
6871 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6872 THIS_CU->is_debug_types, but nothing else.
6873
6874 We fill in THIS_CU->length.
6875
6876 THIS_CU->cu is always freed when done.
6877 This is done in order to not leave THIS_CU->cu in a state where we have
6878 to care whether it refers to the "main" CU or the DWO CU.
6879
6880 When parent_cu is passed, it is used to provide a default value for
6881 str_offsets_base and addr_base from the parent. */
6882
6883 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6884 dwarf2_per_objfile *per_objfile,
6885 struct dwarf2_cu *parent_cu,
6886 struct dwo_file *dwo_file)
6887 : die_reader_specs {},
6888 m_this_cu (this_cu)
6889 {
6890 struct objfile *objfile = per_objfile->objfile;
6891 struct dwarf2_section_info *section = this_cu->section;
6892 bfd *abfd = section->get_bfd_owner ();
6893 struct dwarf2_section_info *abbrev_section;
6894 const gdb_byte *begin_info_ptr, *info_ptr;
6895
6896 if (dwarf_die_debug)
6897 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6898 this_cu->is_debug_types ? "type" : "comp",
6899 sect_offset_str (this_cu->sect_off));
6900
6901 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6902
6903 abbrev_section = (dwo_file != NULL
6904 ? &dwo_file->sections.abbrev
6905 : get_abbrev_section_for_cu (this_cu));
6906
6907 /* This is cheap if the section is already read in. */
6908 section->read (objfile);
6909
6910 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6911
6912 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6913 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6914 section, abbrev_section, info_ptr,
6915 (this_cu->is_debug_types
6916 ? rcuh_kind::TYPE
6917 : rcuh_kind::COMPILE));
6918
6919 if (parent_cu != nullptr)
6920 {
6921 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6922 m_new_cu->addr_base = parent_cu->addr_base;
6923 }
6924 this_cu->length = m_new_cu->header.get_length ();
6925
6926 /* Skip dummy compilation units. */
6927 if (info_ptr >= begin_info_ptr + this_cu->length
6928 || peek_abbrev_code (abfd, info_ptr) == 0)
6929 {
6930 dummy_p = true;
6931 return;
6932 }
6933
6934 abbrev_section->read (objfile);
6935 m_abbrev_table_holder
6936 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
6937
6938 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6939 m_abbrev_table_holder.get ());
6940 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6941 }
6942
6943 \f
6944 /* Type Unit Groups.
6945
6946 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6947 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6948 so that all types coming from the same compilation (.o file) are grouped
6949 together. A future step could be to put the types in the same symtab as
6950 the CU the types ultimately came from. */
6951
6952 static hashval_t
6953 hash_type_unit_group (const void *item)
6954 {
6955 const struct type_unit_group *tu_group
6956 = (const struct type_unit_group *) item;
6957
6958 return hash_stmt_list_entry (&tu_group->hash);
6959 }
6960
6961 static int
6962 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6963 {
6964 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6965 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6966
6967 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6968 }
6969
6970 /* Allocate a hash table for type unit groups. */
6971
6972 static htab_up
6973 allocate_type_unit_groups_table ()
6974 {
6975 return htab_up (htab_create_alloc (3,
6976 hash_type_unit_group,
6977 eq_type_unit_group,
6978 [] (void *arg)
6979 {
6980 type_unit_group *grp
6981 = (type_unit_group *) arg;
6982 delete grp;
6983 },
6984 xcalloc, xfree));
6985 }
6986
6987 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6988 partial symtabs. We combine several TUs per psymtab to not let the size
6989 of any one psymtab grow too big. */
6990 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6991 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6992
6993 /* Helper routine for get_type_unit_group.
6994 Create the type_unit_group object used to hold one or more TUs. */
6995
6996 static std::unique_ptr<type_unit_group>
6997 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6998 {
6999 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7000 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7001
7002 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
7003 tu_group->per_bfd = per_bfd;
7004
7005 if (per_bfd->using_index)
7006 {
7007 tu_group->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7008 struct dwarf2_per_cu_quick_data);
7009 }
7010 else
7011 {
7012 unsigned int line_offset = to_underlying (line_offset_struct);
7013 dwarf2_psymtab *pst;
7014 std::string name;
7015
7016 /* Give the symtab a useful name for debug purposes. */
7017 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7018 name = string_printf ("<type_units_%d>",
7019 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7020 else
7021 name = string_printf ("<type_units_at_0x%x>", line_offset);
7022
7023 pst = create_partial_symtab (tu_group.get (), per_objfile,
7024 name.c_str ());
7025 pst->anonymous = true;
7026 }
7027
7028 tu_group->hash.dwo_unit = cu->dwo_unit;
7029 tu_group->hash.line_sect_off = line_offset_struct;
7030
7031 return tu_group;
7032 }
7033
7034 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7035 STMT_LIST is a DW_AT_stmt_list attribute. */
7036
7037 static struct type_unit_group *
7038 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7039 {
7040 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7041 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7042 struct type_unit_group *tu_group;
7043 void **slot;
7044 unsigned int line_offset;
7045 struct type_unit_group type_unit_group_for_lookup;
7046
7047 if (per_objfile->per_bfd->type_unit_groups == NULL)
7048 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7049
7050 /* Do we need to create a new group, or can we use an existing one? */
7051
7052 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
7053 {
7054 line_offset = stmt_list->as_unsigned ();
7055 ++tu_stats->nr_symtab_sharers;
7056 }
7057 else
7058 {
7059 /* Ugh, no stmt_list. Rare, but we have to handle it.
7060 We can do various things here like create one group per TU or
7061 spread them over multiple groups to split up the expansion work.
7062 To avoid worst case scenarios (too many groups or too large groups)
7063 we, umm, group them in bunches. */
7064 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7065 | (tu_stats->nr_stmt_less_type_units
7066 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7067 ++tu_stats->nr_stmt_less_type_units;
7068 }
7069
7070 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7071 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7072 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7073 &type_unit_group_for_lookup, INSERT);
7074 if (*slot == nullptr)
7075 {
7076 sect_offset line_offset_struct = (sect_offset) line_offset;
7077 std::unique_ptr<type_unit_group> grp
7078 = create_type_unit_group (cu, line_offset_struct);
7079 *slot = grp.release ();
7080 ++tu_stats->nr_symtabs;
7081 }
7082
7083 tu_group = (struct type_unit_group *) *slot;
7084 gdb_assert (tu_group != nullptr);
7085 return tu_group;
7086 }
7087 \f
7088 /* Partial symbol tables. */
7089
7090 /* Create a psymtab named NAME and assign it to PER_CU.
7091
7092 The caller must fill in the following details:
7093 dirname, textlow, texthigh. */
7094
7095 static dwarf2_psymtab *
7096 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7097 dwarf2_per_objfile *per_objfile,
7098 const char *name)
7099 {
7100 dwarf2_psymtab *pst
7101 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
7102 per_objfile->objfile->per_bfd, per_cu);
7103
7104 pst->psymtabs_addrmap_supported = true;
7105
7106 /* This is the glue that links PST into GDB's symbol API. */
7107 per_cu->v.psymtab = pst;
7108
7109 return pst;
7110 }
7111
7112 /* DIE reader function for process_psymtab_comp_unit. */
7113
7114 static void
7115 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7116 const gdb_byte *info_ptr,
7117 struct die_info *comp_unit_die,
7118 enum language pretend_language)
7119 {
7120 struct dwarf2_cu *cu = reader->cu;
7121 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7122 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7123 struct objfile *objfile = per_objfile->objfile;
7124 struct gdbarch *gdbarch = objfile->arch ();
7125 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7126 CORE_ADDR baseaddr;
7127 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7128 dwarf2_psymtab *pst;
7129 enum pc_bounds_kind cu_bounds_kind;
7130 const char *filename;
7131
7132 gdb_assert (! per_cu->is_debug_types);
7133
7134 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7135
7136 /* Allocate a new partial symbol table structure. */
7137 gdb::unique_xmalloc_ptr<char> debug_filename;
7138 static const char artificial[] = "<artificial>";
7139 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7140 if (filename == NULL)
7141 filename = "";
7142 else if (strcmp (filename, artificial) == 0)
7143 {
7144 debug_filename.reset (concat (artificial, "@",
7145 sect_offset_str (per_cu->sect_off),
7146 (char *) NULL));
7147 filename = debug_filename.get ();
7148 }
7149
7150 pst = create_partial_symtab (per_cu, per_objfile, filename);
7151
7152 /* This must be done before calling dwarf2_build_include_psymtabs. */
7153 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7154
7155 baseaddr = objfile->text_section_offset ();
7156
7157 dwarf2_find_base_address (comp_unit_die, cu);
7158
7159 /* Possibly set the default values of LOWPC and HIGHPC from
7160 `DW_AT_ranges'. */
7161 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7162 &best_highpc, cu, pst);
7163 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7164 {
7165 CORE_ADDR low
7166 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7167 - baseaddr);
7168 CORE_ADDR high
7169 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7170 - baseaddr - 1);
7171 /* Store the contiguous range if it is not empty; it can be
7172 empty for CUs with no code. */
7173 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
7174 low, high, pst);
7175 }
7176
7177 /* Check if comp unit has_children.
7178 If so, read the rest of the partial symbols from this comp unit.
7179 If not, there's no more debug_info for this comp unit. */
7180 if (comp_unit_die->has_children)
7181 {
7182 struct partial_die_info *first_die;
7183 CORE_ADDR lowpc, highpc;
7184
7185 lowpc = ((CORE_ADDR) -1);
7186 highpc = ((CORE_ADDR) 0);
7187
7188 first_die = load_partial_dies (reader, info_ptr, 1);
7189
7190 scan_partial_symbols (first_die, &lowpc, &highpc,
7191 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7192
7193 /* If we didn't find a lowpc, set it to highpc to avoid
7194 complaints from `maint check'. */
7195 if (lowpc == ((CORE_ADDR) -1))
7196 lowpc = highpc;
7197
7198 /* If the compilation unit didn't have an explicit address range,
7199 then use the information extracted from its child dies. */
7200 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7201 {
7202 best_lowpc = lowpc;
7203 best_highpc = highpc;
7204 }
7205 }
7206 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7207 best_lowpc + baseaddr)
7208 - baseaddr);
7209 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7210 best_highpc + baseaddr)
7211 - baseaddr);
7212
7213 pst->end ();
7214
7215 if (!cu->per_cu->imported_symtabs_empty ())
7216 {
7217 int i;
7218 int len = cu->per_cu->imported_symtabs_size ();
7219
7220 /* Fill in 'dependencies' here; we fill in 'users' in a
7221 post-pass. */
7222 pst->number_of_dependencies = len;
7223 pst->dependencies
7224 = per_bfd->partial_symtabs->allocate_dependencies (len);
7225 for (i = 0; i < len; ++i)
7226 {
7227 pst->dependencies[i]
7228 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7229 }
7230
7231 cu->per_cu->imported_symtabs_free ();
7232 }
7233
7234 /* Get the list of files included in the current compilation unit,
7235 and build a psymtab for each of them. */
7236 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7237
7238 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7239 ", %d global, %d static syms",
7240 per_cu->is_debug_types ? "type" : "comp",
7241 sect_offset_str (per_cu->sect_off),
7242 paddress (gdbarch, pst->text_low (objfile)),
7243 paddress (gdbarch, pst->text_high (objfile)),
7244 (int) pst->global_psymbols.size (),
7245 (int) pst->static_psymbols.size ());
7246 }
7247
7248 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7249 Process compilation unit THIS_CU for a psymtab. */
7250
7251 static void
7252 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7253 dwarf2_per_objfile *per_objfile,
7254 bool want_partial_unit,
7255 enum language pretend_language)
7256 {
7257 /* If this compilation unit was already read in, free the
7258 cached copy in order to read it in again. This is
7259 necessary because we skipped some symbols when we first
7260 read in the compilation unit (see load_partial_dies).
7261 This problem could be avoided, but the benefit is unclear. */
7262 per_objfile->remove_cu (this_cu);
7263
7264 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7265
7266 switch (reader.comp_unit_die->tag)
7267 {
7268 case DW_TAG_compile_unit:
7269 this_cu->unit_type = DW_UT_compile;
7270 break;
7271 case DW_TAG_partial_unit:
7272 this_cu->unit_type = DW_UT_partial;
7273 break;
7274 case DW_TAG_type_unit:
7275 this_cu->unit_type = DW_UT_type;
7276 break;
7277 default:
7278 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7279 dwarf_tag_name (reader.comp_unit_die->tag),
7280 sect_offset_str (reader.cu->per_cu->sect_off),
7281 objfile_name (per_objfile->objfile));
7282 }
7283
7284 if (reader.dummy_p)
7285 {
7286 /* Nothing. */
7287 }
7288 else if (this_cu->is_debug_types)
7289 build_type_psymtabs_reader (&reader, reader.info_ptr,
7290 reader.comp_unit_die);
7291 else if (want_partial_unit
7292 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7293 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7294 reader.comp_unit_die,
7295 pretend_language);
7296
7297 this_cu->lang = reader.cu->language;
7298
7299 /* Age out any secondary CUs. */
7300 per_objfile->age_comp_units ();
7301 }
7302
7303 /* Reader function for build_type_psymtabs. */
7304
7305 static void
7306 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7307 const gdb_byte *info_ptr,
7308 struct die_info *type_unit_die)
7309 {
7310 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7311 struct dwarf2_cu *cu = reader->cu;
7312 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7313 struct signatured_type *sig_type;
7314 struct type_unit_group *tu_group;
7315 struct attribute *attr;
7316 struct partial_die_info *first_die;
7317 CORE_ADDR lowpc, highpc;
7318 dwarf2_psymtab *pst;
7319
7320 gdb_assert (per_cu->is_debug_types);
7321 sig_type = (struct signatured_type *) per_cu;
7322
7323 if (! type_unit_die->has_children)
7324 return;
7325
7326 attr = type_unit_die->attr (DW_AT_stmt_list);
7327 tu_group = get_type_unit_group (cu, attr);
7328
7329 if (tu_group->tus == nullptr)
7330 tu_group->tus = new std::vector<signatured_type *>;
7331 tu_group->tus->push_back (sig_type);
7332
7333 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7334 pst = create_partial_symtab (per_cu, per_objfile, "");
7335 pst->anonymous = true;
7336
7337 first_die = load_partial_dies (reader, info_ptr, 1);
7338
7339 lowpc = (CORE_ADDR) -1;
7340 highpc = (CORE_ADDR) 0;
7341 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7342
7343 pst->end ();
7344 }
7345
7346 /* Struct used to sort TUs by their abbreviation table offset. */
7347
7348 struct tu_abbrev_offset
7349 {
7350 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7351 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7352 {}
7353
7354 signatured_type *sig_type;
7355 sect_offset abbrev_offset;
7356 };
7357
7358 /* Helper routine for build_type_psymtabs, passed to std::sort. */
7359
7360 static bool
7361 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7362 const struct tu_abbrev_offset &b)
7363 {
7364 return a.abbrev_offset < b.abbrev_offset;
7365 }
7366
7367 /* Efficiently read all the type units.
7368
7369 The efficiency is because we sort TUs by the abbrev table they use and
7370 only read each abbrev table once. In one program there are 200K TUs
7371 sharing 8K abbrev tables.
7372
7373 The main purpose of this function is to support building the
7374 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7375 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7376 can collapse the search space by grouping them by stmt_list.
7377 The savings can be significant, in the same program from above the 200K TUs
7378 share 8K stmt_list tables.
7379
7380 FUNC is expected to call get_type_unit_group, which will create the
7381 struct type_unit_group if necessary and add it to
7382 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7383
7384 static void
7385 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7386 {
7387 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7388 abbrev_table_up abbrev_table;
7389 sect_offset abbrev_offset;
7390
7391 /* It's up to the caller to not call us multiple times. */
7392 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7393
7394 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
7395 return;
7396
7397 /* TUs typically share abbrev tables, and there can be way more TUs than
7398 abbrev tables. Sort by abbrev table to reduce the number of times we
7399 read each abbrev table in.
7400 Alternatives are to punt or to maintain a cache of abbrev tables.
7401 This is simpler and efficient enough for now.
7402
7403 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7404 symtab to use). Typically TUs with the same abbrev offset have the same
7405 stmt_list value too so in practice this should work well.
7406
7407 The basic algorithm here is:
7408
7409 sort TUs by abbrev table
7410 for each TU with same abbrev table:
7411 read abbrev table if first user
7412 read TU top level DIE
7413 [IWBN if DWO skeletons had DW_AT_stmt_list]
7414 call FUNC */
7415
7416 dwarf_read_debug_printf ("Building type unit groups ...");
7417
7418 /* Sort in a separate table to maintain the order of all_comp_units
7419 for .gdb_index: TU indices directly index all_type_units. */
7420 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7421 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
7422
7423 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
7424 {
7425 if (cu->is_debug_types)
7426 {
7427 auto sig_type = static_cast<signatured_type *> (cu.get ());
7428 sorted_by_abbrev.emplace_back
7429 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
7430 sig_type->sect_off));
7431 }
7432 }
7433
7434 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7435 sort_tu_by_abbrev_offset);
7436
7437 abbrev_offset = (sect_offset) ~(unsigned) 0;
7438
7439 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7440 {
7441 /* Switch to the next abbrev table if necessary. */
7442 if (abbrev_table == NULL
7443 || tu.abbrev_offset != abbrev_offset)
7444 {
7445 abbrev_offset = tu.abbrev_offset;
7446 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7447 abbrev_table =
7448 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7449 ++tu_stats->nr_uniq_abbrev_tables;
7450 }
7451
7452 cutu_reader reader (tu.sig_type, per_objfile,
7453 abbrev_table.get (), nullptr, false);
7454 if (!reader.dummy_p)
7455 build_type_psymtabs_reader (&reader, reader.info_ptr,
7456 reader.comp_unit_die);
7457 }
7458 }
7459
7460 /* Print collected type unit statistics. */
7461
7462 static void
7463 print_tu_stats (dwarf2_per_objfile *per_objfile)
7464 {
7465 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7466
7467 dwarf_read_debug_printf ("Type unit statistics:");
7468 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
7469 dwarf_read_debug_printf (" %d uniq abbrev tables",
7470 tu_stats->nr_uniq_abbrev_tables);
7471 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7472 tu_stats->nr_symtabs);
7473 dwarf_read_debug_printf (" %d symtab sharers",
7474 tu_stats->nr_symtab_sharers);
7475 dwarf_read_debug_printf (" %d type units without a stmt_list",
7476 tu_stats->nr_stmt_less_type_units);
7477 dwarf_read_debug_printf (" %d all_type_units reallocs",
7478 tu_stats->nr_all_type_units_reallocs);
7479 }
7480
7481 /* Traversal function for build_type_psymtabs. */
7482
7483 static int
7484 build_type_psymtab_dependencies (void **slot, void *info)
7485 {
7486 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7487 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7488 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7489 dwarf2_psymtab *pst = tu_group->v.psymtab;
7490 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7491 int i;
7492
7493 gdb_assert (len > 0);
7494 gdb_assert (tu_group->type_unit_group_p ());
7495
7496 pst->number_of_dependencies = len;
7497 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
7498 for (i = 0; i < len; ++i)
7499 {
7500 struct signatured_type *iter = tu_group->tus->at (i);
7501 gdb_assert (iter->is_debug_types);
7502 pst->dependencies[i] = iter->v.psymtab;
7503 iter->type_unit_group = tu_group;
7504 }
7505
7506 delete tu_group->tus;
7507 tu_group->tus = nullptr;
7508
7509 return 1;
7510 }
7511
7512 /* Traversal function for process_skeletonless_type_unit.
7513 Read a TU in a DWO file and build partial symbols for it. */
7514
7515 static int
7516 process_skeletonless_type_unit (void **slot, void *info)
7517 {
7518 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7519 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7520 struct signatured_type find_entry, *entry;
7521
7522 /* If this TU doesn't exist in the global table, add it and read it in. */
7523
7524 if (per_objfile->per_bfd->signatured_types == NULL)
7525 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7526
7527 find_entry.signature = dwo_unit->signature;
7528 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7529 &find_entry, INSERT);
7530 /* If we've already seen this type there's nothing to do. What's happening
7531 is we're doing our own version of comdat-folding here. */
7532 if (*slot != NULL)
7533 return 1;
7534
7535 /* This does the job that create_all_comp_units would have done for
7536 this TU. */
7537 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7538 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7539 *slot = entry;
7540
7541 /* This does the job that build_type_psymtabs would have done. */
7542 cutu_reader reader (entry, per_objfile, nullptr, nullptr, false);
7543 if (!reader.dummy_p)
7544 build_type_psymtabs_reader (&reader, reader.info_ptr,
7545 reader.comp_unit_die);
7546
7547 return 1;
7548 }
7549
7550 /* Traversal function for process_skeletonless_type_units. */
7551
7552 static int
7553 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7554 {
7555 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7556
7557 if (dwo_file->tus != NULL)
7558 htab_traverse_noresize (dwo_file->tus.get (),
7559 process_skeletonless_type_unit, info);
7560
7561 return 1;
7562 }
7563
7564 /* Scan all TUs of DWO files, verifying we've processed them.
7565 This is needed in case a TU was emitted without its skeleton.
7566 Note: This can't be done until we know what all the DWO files are. */
7567
7568 static void
7569 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
7570 {
7571 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7572 if (get_dwp_file (per_objfile) == NULL
7573 && per_objfile->per_bfd->dwo_files != NULL)
7574 {
7575 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7576 process_dwo_file_for_skeletonless_type_units,
7577 per_objfile);
7578 }
7579 }
7580
7581 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7582
7583 static void
7584 set_partial_user (dwarf2_per_objfile *per_objfile)
7585 {
7586 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
7587 {
7588 dwarf2_psymtab *pst = per_cu->v.psymtab;
7589
7590 if (pst == NULL)
7591 continue;
7592
7593 for (int j = 0; j < pst->number_of_dependencies; ++j)
7594 {
7595 /* Set the 'user' field only if it is not already set. */
7596 if (pst->dependencies[j]->user == NULL)
7597 pst->dependencies[j]->user = pst;
7598 }
7599 }
7600 }
7601
7602 /* Build the partial symbol table by doing a quick pass through the
7603 .debug_info and .debug_abbrev sections. */
7604
7605 static void
7606 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7607 {
7608 struct objfile *objfile = per_objfile->objfile;
7609 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7610
7611 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7612 objfile_name (objfile));
7613
7614 scoped_restore restore_reading_psyms
7615 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
7616
7617 per_bfd->info.read (objfile);
7618
7619 /* Any cached compilation units will be linked by the per-objfile
7620 read_in_chain. Make sure to free them when we're done. */
7621 free_cached_comp_units freer (per_objfile);
7622
7623 create_all_comp_units (per_objfile);
7624 build_type_psymtabs (per_objfile);
7625
7626 /* Create a temporary address map on a temporary obstack. We later
7627 copy this to the final obstack. */
7628 auto_obstack temp_obstack;
7629
7630 scoped_restore save_psymtabs_addrmap
7631 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
7632 addrmap_create_mutable (&temp_obstack));
7633
7634 for (const auto &per_cu : per_bfd->all_comp_units)
7635 {
7636 if (per_cu->v.psymtab != NULL)
7637 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7638 continue;
7639 process_psymtab_comp_unit (per_cu.get (), per_objfile, false,
7640 language_minimal);
7641 }
7642
7643 /* This has to wait until we read the CUs, we need the list of DWOs. */
7644 process_skeletonless_type_units (per_objfile);
7645
7646 /* Now that all TUs have been processed we can fill in the dependencies. */
7647 if (per_bfd->type_unit_groups != NULL)
7648 {
7649 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
7650 build_type_psymtab_dependencies, per_objfile);
7651 }
7652
7653 if (dwarf_read_debug > 0)
7654 print_tu_stats (per_objfile);
7655
7656 set_partial_user (per_objfile);
7657
7658 per_bfd->partial_symtabs->psymtabs_addrmap
7659 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7660 per_bfd->partial_symtabs->obstack ());
7661 /* At this point we want to keep the address map. */
7662 save_psymtabs_addrmap.release ();
7663
7664 dwarf_read_debug_printf ("Done building psymtabs of %s",
7665 objfile_name (objfile));
7666 }
7667
7668 /* Load the partial DIEs for a secondary CU into memory.
7669 This is also used when rereading a primary CU with load_all_dies. */
7670
7671 static void
7672 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7673 dwarf2_per_objfile *per_objfile,
7674 dwarf2_cu *existing_cu)
7675 {
7676 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
7677
7678 if (!reader.dummy_p)
7679 {
7680 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7681 language_minimal);
7682
7683 /* Check if comp unit has_children.
7684 If so, read the rest of the partial symbols from this comp unit.
7685 If not, there's no more debug_info for this comp unit. */
7686 if (reader.comp_unit_die->has_children)
7687 load_partial_dies (&reader, reader.info_ptr, 0);
7688
7689 reader.keep ();
7690 }
7691 }
7692
7693 static void
7694 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
7695 struct dwarf2_section_info *section,
7696 struct dwarf2_section_info *abbrev_section,
7697 unsigned int is_dwz,
7698 htab_up &types_htab,
7699 rcuh_kind section_kind)
7700 {
7701 const gdb_byte *info_ptr;
7702 struct objfile *objfile = per_objfile->objfile;
7703
7704 dwarf_read_debug_printf ("Reading %s for %s",
7705 section->get_name (),
7706 section->get_file_name ());
7707
7708 section->read (objfile);
7709
7710 info_ptr = section->buffer;
7711
7712 while (info_ptr < section->buffer + section->size)
7713 {
7714 std::unique_ptr<dwarf2_per_cu_data> this_cu;
7715
7716 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7717
7718 comp_unit_head cu_header;
7719 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
7720 abbrev_section, info_ptr,
7721 section_kind);
7722
7723 /* Save the compilation unit for later lookup. */
7724 if (cu_header.unit_type != DW_UT_type)
7725 this_cu = per_objfile->per_bfd->allocate_per_cu ();
7726 else
7727 {
7728 if (types_htab == nullptr)
7729 types_htab = allocate_signatured_type_table ();
7730
7731 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
7732 signatured_type *sig_ptr = sig_type.get ();
7733 sig_type->signature = cu_header.signature;
7734 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7735 this_cu = std::move (sig_type);
7736
7737 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7738 gdb_assert (slot != nullptr);
7739 if (*slot != nullptr)
7740 complaint (_("debug type entry at offset %s is duplicate to"
7741 " the entry at offset %s, signature %s"),
7742 sect_offset_str (sect_off),
7743 sect_offset_str (sig_ptr->sect_off),
7744 hex_string (sig_ptr->signature));
7745 *slot = sig_ptr;
7746 }
7747 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7748 this_cu->sect_off = sect_off;
7749 this_cu->length = cu_header.length + cu_header.initial_length_size;
7750 this_cu->is_dwz = is_dwz;
7751 this_cu->section = section;
7752
7753 info_ptr = info_ptr + this_cu->length;
7754 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
7755 }
7756 }
7757
7758 /* Create a list of all compilation units in OBJFILE.
7759 This is only done for -readnow and building partial symtabs. */
7760
7761 static void
7762 create_all_comp_units (dwarf2_per_objfile *per_objfile)
7763 {
7764 htab_up types_htab;
7765
7766 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7767 &per_objfile->per_bfd->abbrev, 0,
7768 types_htab, rcuh_kind::COMPILE);
7769 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7770 read_comp_units_from_section (per_objfile, &section,
7771 &per_objfile->per_bfd->abbrev, 0,
7772 types_htab, rcuh_kind::TYPE);
7773
7774 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
7775 if (dwz != NULL)
7776 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7777 types_htab, rcuh_kind::COMPILE);
7778
7779 per_objfile->per_bfd->signatured_types = std::move (types_htab);
7780 }
7781
7782 /* Process all loaded DIEs for compilation unit CU, starting at
7783 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7784 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7785 DW_AT_ranges). See the comments of add_partial_subprogram on how
7786 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7787
7788 static void
7789 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7790 CORE_ADDR *highpc, int set_addrmap,
7791 struct dwarf2_cu *cu)
7792 {
7793 struct partial_die_info *pdi;
7794
7795 /* Now, march along the PDI's, descending into ones which have
7796 interesting children but skipping the children of the other ones,
7797 until we reach the end of the compilation unit. */
7798
7799 pdi = first_die;
7800
7801 while (pdi != NULL)
7802 {
7803 pdi->fixup (cu);
7804
7805 /* Anonymous namespaces or modules have no name but have interesting
7806 children, so we need to look at them. Ditto for anonymous
7807 enums. */
7808
7809 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
7810 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7811 || pdi->tag == DW_TAG_imported_unit
7812 || pdi->tag == DW_TAG_inlined_subroutine)
7813 {
7814 switch (pdi->tag)
7815 {
7816 case DW_TAG_subprogram:
7817 case DW_TAG_inlined_subroutine:
7818 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7819 if (cu->language == language_cplus)
7820 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7821 set_addrmap, cu);
7822 break;
7823 case DW_TAG_constant:
7824 case DW_TAG_variable:
7825 case DW_TAG_typedef:
7826 case DW_TAG_union_type:
7827 if (!pdi->is_declaration
7828 || (pdi->tag == DW_TAG_variable && pdi->is_external))
7829 {
7830 add_partial_symbol (pdi, cu);
7831 }
7832 break;
7833 case DW_TAG_class_type:
7834 case DW_TAG_interface_type:
7835 case DW_TAG_structure_type:
7836 if (!pdi->is_declaration)
7837 {
7838 add_partial_symbol (pdi, cu);
7839 }
7840 if ((cu->language == language_rust
7841 || cu->language == language_cplus) && pdi->has_children)
7842 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7843 set_addrmap, cu);
7844 break;
7845 case DW_TAG_enumeration_type:
7846 if (!pdi->is_declaration)
7847 add_partial_enumeration (pdi, cu);
7848 break;
7849 case DW_TAG_base_type:
7850 case DW_TAG_subrange_type:
7851 /* File scope base type definitions are added to the partial
7852 symbol table. */
7853 add_partial_symbol (pdi, cu);
7854 break;
7855 case DW_TAG_namespace:
7856 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7857 break;
7858 case DW_TAG_module:
7859 if (!pdi->is_declaration)
7860 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7861 break;
7862 case DW_TAG_imported_unit:
7863 {
7864 struct dwarf2_per_cu_data *per_cu;
7865
7866 /* For now we don't handle imported units in type units. */
7867 if (cu->per_cu->is_debug_types)
7868 {
7869 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7870 " supported in type units [in module %s]"),
7871 objfile_name (cu->per_objfile->objfile));
7872 }
7873
7874 per_cu = dwarf2_find_containing_comp_unit
7875 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
7876
7877 /* Go read the partial unit, if needed. */
7878 if (per_cu->v.psymtab == NULL)
7879 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
7880 cu->language);
7881
7882 cu->per_cu->imported_symtabs_push (per_cu);
7883 }
7884 break;
7885 case DW_TAG_imported_declaration:
7886 add_partial_symbol (pdi, cu);
7887 break;
7888 default:
7889 break;
7890 }
7891 }
7892
7893 /* If the die has a sibling, skip to the sibling. */
7894
7895 pdi = pdi->die_sibling;
7896 }
7897 }
7898
7899 /* Functions used to compute the fully scoped name of a partial DIE.
7900
7901 Normally, this is simple. For C++, the parent DIE's fully scoped
7902 name is concatenated with "::" and the partial DIE's name.
7903 Enumerators are an exception; they use the scope of their parent
7904 enumeration type, i.e. the name of the enumeration type is not
7905 prepended to the enumerator.
7906
7907 There are two complexities. One is DW_AT_specification; in this
7908 case "parent" means the parent of the target of the specification,
7909 instead of the direct parent of the DIE. The other is compilers
7910 which do not emit DW_TAG_namespace; in this case we try to guess
7911 the fully qualified name of structure types from their members'
7912 linkage names. This must be done using the DIE's children rather
7913 than the children of any DW_AT_specification target. We only need
7914 to do this for structures at the top level, i.e. if the target of
7915 any DW_AT_specification (if any; otherwise the DIE itself) does not
7916 have a parent. */
7917
7918 /* Compute the scope prefix associated with PDI's parent, in
7919 compilation unit CU. The result will be allocated on CU's
7920 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7921 field. NULL is returned if no prefix is necessary. */
7922 static const char *
7923 partial_die_parent_scope (struct partial_die_info *pdi,
7924 struct dwarf2_cu *cu)
7925 {
7926 const char *grandparent_scope;
7927 struct partial_die_info *parent, *real_pdi;
7928
7929 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7930 then this means the parent of the specification DIE. */
7931
7932 real_pdi = pdi;
7933 while (real_pdi->has_specification)
7934 {
7935 auto res = find_partial_die (real_pdi->spec_offset,
7936 real_pdi->spec_is_dwz, cu);
7937 real_pdi = res.pdi;
7938 cu = res.cu;
7939 }
7940
7941 parent = real_pdi->die_parent;
7942 if (parent == NULL)
7943 return NULL;
7944
7945 if (parent->scope_set)
7946 return parent->scope;
7947
7948 parent->fixup (cu);
7949
7950 grandparent_scope = partial_die_parent_scope (parent, cu);
7951
7952 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7953 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7954 Work around this problem here. */
7955 if (cu->language == language_cplus
7956 && parent->tag == DW_TAG_namespace
7957 && strcmp (parent->name (cu), "::") == 0
7958 && grandparent_scope == NULL)
7959 {
7960 parent->scope = NULL;
7961 parent->scope_set = 1;
7962 return NULL;
7963 }
7964
7965 /* Nested subroutines in Fortran get a prefix. */
7966 if (pdi->tag == DW_TAG_enumerator)
7967 /* Enumerators should not get the name of the enumeration as a prefix. */
7968 parent->scope = grandparent_scope;
7969 else if (parent->tag == DW_TAG_namespace
7970 || parent->tag == DW_TAG_module
7971 || parent->tag == DW_TAG_structure_type
7972 || parent->tag == DW_TAG_class_type
7973 || parent->tag == DW_TAG_interface_type
7974 || parent->tag == DW_TAG_union_type
7975 || parent->tag == DW_TAG_enumeration_type
7976 || (cu->language == language_fortran
7977 && parent->tag == DW_TAG_subprogram
7978 && pdi->tag == DW_TAG_subprogram))
7979 {
7980 if (grandparent_scope == NULL)
7981 parent->scope = parent->name (cu);
7982 else
7983 parent->scope = typename_concat (&cu->comp_unit_obstack,
7984 grandparent_scope,
7985 parent->name (cu), 0, cu);
7986 }
7987 else
7988 {
7989 /* FIXME drow/2004-04-01: What should we be doing with
7990 function-local names? For partial symbols, we should probably be
7991 ignoring them. */
7992 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
7993 dwarf_tag_name (parent->tag),
7994 sect_offset_str (pdi->sect_off));
7995 parent->scope = grandparent_scope;
7996 }
7997
7998 parent->scope_set = 1;
7999 return parent->scope;
8000 }
8001
8002 /* Return the fully scoped name associated with PDI, from compilation unit
8003 CU. The result will be allocated with malloc. */
8004
8005 static gdb::unique_xmalloc_ptr<char>
8006 partial_die_full_name (struct partial_die_info *pdi,
8007 struct dwarf2_cu *cu)
8008 {
8009 const char *parent_scope;
8010
8011 /* If this is a template instantiation, we can not work out the
8012 template arguments from partial DIEs. So, unfortunately, we have
8013 to go through the full DIEs. At least any work we do building
8014 types here will be reused if full symbols are loaded later. */
8015 if (pdi->has_template_arguments)
8016 {
8017 pdi->fixup (cu);
8018
8019 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8020 {
8021 struct die_info *die;
8022 struct attribute attr;
8023 struct dwarf2_cu *ref_cu = cu;
8024
8025 /* DW_FORM_ref_addr is using section offset. */
8026 attr.name = (enum dwarf_attribute) 0;
8027 attr.form = DW_FORM_ref_addr;
8028 attr.u.unsnd = to_underlying (pdi->sect_off);
8029 die = follow_die_ref (NULL, &attr, &ref_cu);
8030
8031 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8032 }
8033 }
8034
8035 parent_scope = partial_die_parent_scope (pdi, cu);
8036 if (parent_scope == NULL)
8037 return NULL;
8038 else
8039 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8040 pdi->name (cu),
8041 0, cu));
8042 }
8043
8044 static void
8045 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8046 {
8047 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8048 struct objfile *objfile = per_objfile->objfile;
8049 struct gdbarch *gdbarch = objfile->arch ();
8050 CORE_ADDR addr = 0;
8051 const char *actual_name = NULL;
8052 CORE_ADDR baseaddr;
8053
8054 baseaddr = objfile->text_section_offset ();
8055
8056 gdb::unique_xmalloc_ptr<char> built_actual_name
8057 = partial_die_full_name (pdi, cu);
8058 if (built_actual_name != NULL)
8059 actual_name = built_actual_name.get ();
8060
8061 if (actual_name == NULL)
8062 actual_name = pdi->name (cu);
8063
8064 partial_symbol psymbol;
8065 memset (&psymbol, 0, sizeof (psymbol));
8066 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8067 psymbol.ginfo.set_section_index (-1);
8068
8069 /* The code below indicates that the psymbol should be installed by
8070 setting this. */
8071 gdb::optional<psymbol_placement> where;
8072
8073 switch (pdi->tag)
8074 {
8075 case DW_TAG_inlined_subroutine:
8076 case DW_TAG_subprogram:
8077 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8078 - baseaddr);
8079 if (pdi->is_external
8080 || cu->language == language_ada
8081 || (cu->language == language_fortran
8082 && pdi->die_parent != NULL
8083 && pdi->die_parent->tag == DW_TAG_subprogram))
8084 {
8085 /* Normally, only "external" DIEs are part of the global scope.
8086 But in Ada and Fortran, we want to be able to access nested
8087 procedures globally. So all Ada and Fortran subprograms are
8088 stored in the global scope. */
8089 where = psymbol_placement::GLOBAL;
8090 }
8091 else
8092 where = psymbol_placement::STATIC;
8093
8094 psymbol.domain = VAR_DOMAIN;
8095 psymbol.aclass = LOC_BLOCK;
8096 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8097 psymbol.ginfo.value.address = addr;
8098
8099 if (pdi->main_subprogram && actual_name != NULL)
8100 set_objfile_main_name (objfile, actual_name, cu->language);
8101 break;
8102 case DW_TAG_constant:
8103 psymbol.domain = VAR_DOMAIN;
8104 psymbol.aclass = LOC_STATIC;
8105 where = (pdi->is_external
8106 ? psymbol_placement::GLOBAL
8107 : psymbol_placement::STATIC);
8108 break;
8109 case DW_TAG_variable:
8110 if (pdi->d.locdesc)
8111 addr = decode_locdesc (pdi->d.locdesc, cu);
8112
8113 if (pdi->d.locdesc
8114 && addr == 0
8115 && !per_objfile->per_bfd->has_section_at_zero)
8116 {
8117 /* A global or static variable may also have been stripped
8118 out by the linker if unused, in which case its address
8119 will be nullified; do not add such variables into partial
8120 symbol table then. */
8121 }
8122 else if (pdi->is_external)
8123 {
8124 /* Global Variable.
8125 Don't enter into the minimal symbol tables as there is
8126 a minimal symbol table entry from the ELF symbols already.
8127 Enter into partial symbol table if it has a location
8128 descriptor or a type.
8129 If the location descriptor is missing, new_symbol will create
8130 a LOC_UNRESOLVED symbol, the address of the variable will then
8131 be determined from the minimal symbol table whenever the variable
8132 is referenced.
8133 The address for the partial symbol table entry is not
8134 used by GDB, but it comes in handy for debugging partial symbol
8135 table building. */
8136
8137 if (pdi->d.locdesc || pdi->has_type)
8138 {
8139 psymbol.domain = VAR_DOMAIN;
8140 psymbol.aclass = LOC_STATIC;
8141 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8142 psymbol.ginfo.value.address = addr;
8143 where = psymbol_placement::GLOBAL;
8144 }
8145 }
8146 else
8147 {
8148 int has_loc = pdi->d.locdesc != NULL;
8149
8150 /* Static Variable. Skip symbols whose value we cannot know (those
8151 without location descriptors or constant values). */
8152 if (!has_loc && !pdi->has_const_value)
8153 return;
8154
8155 psymbol.domain = VAR_DOMAIN;
8156 psymbol.aclass = LOC_STATIC;
8157 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8158 if (has_loc)
8159 psymbol.ginfo.value.address = addr;
8160 where = psymbol_placement::STATIC;
8161 }
8162 break;
8163 case DW_TAG_array_type:
8164 case DW_TAG_typedef:
8165 case DW_TAG_base_type:
8166 case DW_TAG_subrange_type:
8167 psymbol.domain = VAR_DOMAIN;
8168 psymbol.aclass = LOC_TYPEDEF;
8169 where = psymbol_placement::STATIC;
8170 break;
8171 case DW_TAG_imported_declaration:
8172 case DW_TAG_namespace:
8173 psymbol.domain = VAR_DOMAIN;
8174 psymbol.aclass = LOC_TYPEDEF;
8175 where = psymbol_placement::GLOBAL;
8176 break;
8177 case DW_TAG_module:
8178 /* With Fortran 77 there might be a "BLOCK DATA" module
8179 available without any name. If so, we skip the module as it
8180 doesn't bring any value. */
8181 if (actual_name != nullptr)
8182 {
8183 psymbol.domain = MODULE_DOMAIN;
8184 psymbol.aclass = LOC_TYPEDEF;
8185 where = psymbol_placement::GLOBAL;
8186 }
8187 break;
8188 case DW_TAG_class_type:
8189 case DW_TAG_interface_type:
8190 case DW_TAG_structure_type:
8191 case DW_TAG_union_type:
8192 case DW_TAG_enumeration_type:
8193 /* Skip external references. The DWARF standard says in the section
8194 about "Structure, Union, and Class Type Entries": "An incomplete
8195 structure, union or class type is represented by a structure,
8196 union or class entry that does not have a byte size attribute
8197 and that has a DW_AT_declaration attribute." */
8198 if (!pdi->has_byte_size && pdi->is_declaration)
8199 return;
8200
8201 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8202 static vs. global. */
8203 psymbol.domain = STRUCT_DOMAIN;
8204 psymbol.aclass = LOC_TYPEDEF;
8205 where = (cu->language == language_cplus
8206 ? psymbol_placement::GLOBAL
8207 : psymbol_placement::STATIC);
8208 break;
8209 case DW_TAG_enumerator:
8210 psymbol.domain = VAR_DOMAIN;
8211 psymbol.aclass = LOC_CONST;
8212 where = (cu->language == language_cplus
8213 ? psymbol_placement::GLOBAL
8214 : psymbol_placement::STATIC);
8215 break;
8216 default:
8217 break;
8218 }
8219
8220 if (where.has_value ())
8221 {
8222 if (built_actual_name != nullptr)
8223 actual_name = objfile->intern (actual_name);
8224 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8225 psymbol.ginfo.set_linkage_name (actual_name);
8226 else
8227 {
8228 psymbol.ginfo.set_demangled_name (actual_name,
8229 &objfile->objfile_obstack);
8230 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8231 }
8232 cu->per_cu->v.psymtab->add_psymbol
8233 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8234 objfile);
8235 }
8236 }
8237
8238 /* Read a partial die corresponding to a namespace; also, add a symbol
8239 corresponding to that namespace to the symbol table. NAMESPACE is
8240 the name of the enclosing namespace. */
8241
8242 static void
8243 add_partial_namespace (struct partial_die_info *pdi,
8244 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8245 int set_addrmap, struct dwarf2_cu *cu)
8246 {
8247 /* Add a symbol for the namespace. */
8248
8249 add_partial_symbol (pdi, cu);
8250
8251 /* Now scan partial symbols in that namespace. */
8252
8253 if (pdi->has_children)
8254 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8255 }
8256
8257 /* Read a partial die corresponding to a Fortran module. */
8258
8259 static void
8260 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8261 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8262 {
8263 /* Add a symbol for the namespace. */
8264
8265 add_partial_symbol (pdi, cu);
8266
8267 /* Now scan partial symbols in that module. */
8268
8269 if (pdi->has_children)
8270 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8271 }
8272
8273 /* Read a partial die corresponding to a subprogram or an inlined
8274 subprogram and create a partial symbol for that subprogram.
8275 When the CU language allows it, this routine also defines a partial
8276 symbol for each nested subprogram that this subprogram contains.
8277 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8278 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8279
8280 PDI may also be a lexical block, in which case we simply search
8281 recursively for subprograms defined inside that lexical block.
8282 Again, this is only performed when the CU language allows this
8283 type of definitions. */
8284
8285 static void
8286 add_partial_subprogram (struct partial_die_info *pdi,
8287 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8288 int set_addrmap, struct dwarf2_cu *cu)
8289 {
8290 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8291 {
8292 if (pdi->has_pc_info)
8293 {
8294 if (pdi->lowpc < *lowpc)
8295 *lowpc = pdi->lowpc;
8296 if (pdi->highpc > *highpc)
8297 *highpc = pdi->highpc;
8298 if (set_addrmap)
8299 {
8300 struct objfile *objfile = cu->per_objfile->objfile;
8301 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8302 struct gdbarch *gdbarch = objfile->arch ();
8303 CORE_ADDR baseaddr;
8304 CORE_ADDR this_highpc;
8305 CORE_ADDR this_lowpc;
8306
8307 baseaddr = objfile->text_section_offset ();
8308 this_lowpc
8309 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8310 pdi->lowpc + baseaddr)
8311 - baseaddr);
8312 this_highpc
8313 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8314 pdi->highpc + baseaddr)
8315 - baseaddr);
8316 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8317 this_lowpc, this_highpc - 1,
8318 cu->per_cu->v.psymtab);
8319 }
8320 }
8321
8322 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8323 {
8324 if (!pdi->is_declaration)
8325 /* Ignore subprogram DIEs that do not have a name, they are
8326 illegal. Do not emit a complaint at this point, we will
8327 do so when we convert this psymtab into a symtab. */
8328 if (pdi->name (cu))
8329 add_partial_symbol (pdi, cu);
8330 }
8331 }
8332
8333 if (! pdi->has_children)
8334 return;
8335
8336 if (cu->language == language_ada || cu->language == language_fortran)
8337 {
8338 pdi = pdi->die_child;
8339 while (pdi != NULL)
8340 {
8341 pdi->fixup (cu);
8342 if (pdi->tag == DW_TAG_subprogram
8343 || pdi->tag == DW_TAG_inlined_subroutine
8344 || pdi->tag == DW_TAG_lexical_block)
8345 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8346 pdi = pdi->die_sibling;
8347 }
8348 }
8349 }
8350
8351 /* Read a partial die corresponding to an enumeration type. */
8352
8353 static void
8354 add_partial_enumeration (struct partial_die_info *enum_pdi,
8355 struct dwarf2_cu *cu)
8356 {
8357 struct partial_die_info *pdi;
8358
8359 if (enum_pdi->name (cu) != NULL)
8360 add_partial_symbol (enum_pdi, cu);
8361
8362 pdi = enum_pdi->die_child;
8363 while (pdi)
8364 {
8365 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8366 complaint (_("malformed enumerator DIE ignored"));
8367 else
8368 add_partial_symbol (pdi, cu);
8369 pdi = pdi->die_sibling;
8370 }
8371 }
8372
8373 /* Return the initial uleb128 in the die at INFO_PTR. */
8374
8375 static unsigned int
8376 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8377 {
8378 unsigned int bytes_read;
8379
8380 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8381 }
8382
8383 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8384 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8385
8386 Return the corresponding abbrev, or NULL if the number is zero (indicating
8387 an empty DIE). In either case *BYTES_READ will be set to the length of
8388 the initial number. */
8389
8390 static const struct abbrev_info *
8391 peek_die_abbrev (const die_reader_specs &reader,
8392 const gdb_byte *info_ptr, unsigned int *bytes_read)
8393 {
8394 dwarf2_cu *cu = reader.cu;
8395 bfd *abfd = reader.abfd;
8396 unsigned int abbrev_number
8397 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8398
8399 if (abbrev_number == 0)
8400 return NULL;
8401
8402 const abbrev_info *abbrev
8403 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8404 if (!abbrev)
8405 {
8406 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8407 " at offset %s [in module %s]"),
8408 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8409 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8410 }
8411
8412 return abbrev;
8413 }
8414
8415 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8416 Returns a pointer to the end of a series of DIEs, terminated by an empty
8417 DIE. Any children of the skipped DIEs will also be skipped. */
8418
8419 static const gdb_byte *
8420 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8421 {
8422 while (1)
8423 {
8424 unsigned int bytes_read;
8425 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8426 &bytes_read);
8427
8428 if (abbrev == NULL)
8429 return info_ptr + bytes_read;
8430 else
8431 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8432 }
8433 }
8434
8435 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8436 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8437 abbrev corresponding to that skipped uleb128 should be passed in
8438 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8439 children. */
8440
8441 static const gdb_byte *
8442 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8443 const struct abbrev_info *abbrev)
8444 {
8445 unsigned int bytes_read;
8446 struct attribute attr;
8447 bfd *abfd = reader->abfd;
8448 struct dwarf2_cu *cu = reader->cu;
8449 const gdb_byte *buffer = reader->buffer;
8450 const gdb_byte *buffer_end = reader->buffer_end;
8451 unsigned int form, i;
8452
8453 for (i = 0; i < abbrev->num_attrs; i++)
8454 {
8455 /* The only abbrev we care about is DW_AT_sibling. */
8456 if (abbrev->attrs[i].name == DW_AT_sibling)
8457 {
8458 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8459 if (attr.form == DW_FORM_ref_addr)
8460 complaint (_("ignoring absolute DW_AT_sibling"));
8461 else
8462 {
8463 sect_offset off = attr.get_ref_die_offset ();
8464 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8465
8466 if (sibling_ptr < info_ptr)
8467 complaint (_("DW_AT_sibling points backwards"));
8468 else if (sibling_ptr > reader->buffer_end)
8469 reader->die_section->overflow_complaint ();
8470 else
8471 return sibling_ptr;
8472 }
8473 }
8474
8475 /* If it isn't DW_AT_sibling, skip this attribute. */
8476 form = abbrev->attrs[i].form;
8477 skip_attribute:
8478 switch (form)
8479 {
8480 case DW_FORM_ref_addr:
8481 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8482 and later it is offset sized. */
8483 if (cu->header.version == 2)
8484 info_ptr += cu->header.addr_size;
8485 else
8486 info_ptr += cu->header.offset_size;
8487 break;
8488 case DW_FORM_GNU_ref_alt:
8489 info_ptr += cu->header.offset_size;
8490 break;
8491 case DW_FORM_addr:
8492 info_ptr += cu->header.addr_size;
8493 break;
8494 case DW_FORM_data1:
8495 case DW_FORM_ref1:
8496 case DW_FORM_flag:
8497 case DW_FORM_strx1:
8498 info_ptr += 1;
8499 break;
8500 case DW_FORM_flag_present:
8501 case DW_FORM_implicit_const:
8502 break;
8503 case DW_FORM_data2:
8504 case DW_FORM_ref2:
8505 case DW_FORM_strx2:
8506 info_ptr += 2;
8507 break;
8508 case DW_FORM_strx3:
8509 info_ptr += 3;
8510 break;
8511 case DW_FORM_data4:
8512 case DW_FORM_ref4:
8513 case DW_FORM_strx4:
8514 info_ptr += 4;
8515 break;
8516 case DW_FORM_data8:
8517 case DW_FORM_ref8:
8518 case DW_FORM_ref_sig8:
8519 info_ptr += 8;
8520 break;
8521 case DW_FORM_data16:
8522 info_ptr += 16;
8523 break;
8524 case DW_FORM_string:
8525 read_direct_string (abfd, info_ptr, &bytes_read);
8526 info_ptr += bytes_read;
8527 break;
8528 case DW_FORM_sec_offset:
8529 case DW_FORM_strp:
8530 case DW_FORM_GNU_strp_alt:
8531 info_ptr += cu->header.offset_size;
8532 break;
8533 case DW_FORM_exprloc:
8534 case DW_FORM_block:
8535 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8536 info_ptr += bytes_read;
8537 break;
8538 case DW_FORM_block1:
8539 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8540 break;
8541 case DW_FORM_block2:
8542 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8543 break;
8544 case DW_FORM_block4:
8545 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8546 break;
8547 case DW_FORM_addrx:
8548 case DW_FORM_strx:
8549 case DW_FORM_sdata:
8550 case DW_FORM_udata:
8551 case DW_FORM_ref_udata:
8552 case DW_FORM_GNU_addr_index:
8553 case DW_FORM_GNU_str_index:
8554 case DW_FORM_rnglistx:
8555 case DW_FORM_loclistx:
8556 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8557 break;
8558 case DW_FORM_indirect:
8559 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8560 info_ptr += bytes_read;
8561 /* We need to continue parsing from here, so just go back to
8562 the top. */
8563 goto skip_attribute;
8564
8565 default:
8566 error (_("Dwarf Error: Cannot handle %s "
8567 "in DWARF reader [in module %s]"),
8568 dwarf_form_name (form),
8569 bfd_get_filename (abfd));
8570 }
8571 }
8572
8573 if (abbrev->has_children)
8574 return skip_children (reader, info_ptr);
8575 else
8576 return info_ptr;
8577 }
8578
8579 /* Locate ORIG_PDI's sibling.
8580 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8581
8582 static const gdb_byte *
8583 locate_pdi_sibling (const struct die_reader_specs *reader,
8584 struct partial_die_info *orig_pdi,
8585 const gdb_byte *info_ptr)
8586 {
8587 /* Do we know the sibling already? */
8588
8589 if (orig_pdi->sibling)
8590 return orig_pdi->sibling;
8591
8592 /* Are there any children to deal with? */
8593
8594 if (!orig_pdi->has_children)
8595 return info_ptr;
8596
8597 /* Skip the children the long way. */
8598
8599 return skip_children (reader, info_ptr);
8600 }
8601
8602 /* Expand this partial symbol table into a full symbol table. SELF is
8603 not NULL. */
8604
8605 void
8606 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8607 {
8608 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8609
8610 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
8611
8612 /* If this psymtab is constructed from a debug-only objfile, the
8613 has_section_at_zero flag will not necessarily be correct. We
8614 can get the correct value for this flag by looking at the data
8615 associated with the (presumably stripped) associated objfile. */
8616 if (objfile->separate_debug_objfile_backlink)
8617 {
8618 dwarf2_per_objfile *per_objfile_backlink
8619 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8620
8621 per_objfile->per_bfd->has_section_at_zero
8622 = per_objfile_backlink->per_bfd->has_section_at_zero;
8623 }
8624
8625 expand_psymtab (objfile);
8626
8627 process_cu_includes (per_objfile);
8628 }
8629 \f
8630 /* Reading in full CUs. */
8631
8632 /* Add PER_CU to the queue. */
8633
8634 static void
8635 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8636 dwarf2_per_objfile *per_objfile,
8637 enum language pretend_language)
8638 {
8639 per_cu->queued = 1;
8640
8641 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8642 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
8643 }
8644
8645 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8646
8647 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8648 dependency.
8649
8650 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8651 DIEs, false otherwise.
8652
8653 Explanation: there is an invariant that if a CU is queued for expansion
8654 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8655 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8656 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8657 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8658 invariant is respected.
8659
8660 The caller is therefore not required to load the CU's DIEs (we return false)
8661 if:
8662
8663 - the CU is already expanded, and therefore does not get enqueued
8664 - the CU gets enqueued for expansion, but its DIEs are already loaded
8665
8666 Note that the caller should not use this function's return value as an
8667 indicator of whether the CU's DIEs are loaded right now, it should check
8668 that by calling `dwarf2_per_objfile::get_cu` instead. */
8669
8670 static int
8671 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8672 dwarf2_per_cu_data *per_cu,
8673 dwarf2_per_objfile *per_objfile,
8674 enum language pretend_language)
8675 {
8676 /* We may arrive here during partial symbol reading, if we need full
8677 DIEs to process an unusual case (e.g. template arguments). Do
8678 not queue PER_CU, just tell our caller to load its DIEs. */
8679 if (per_cu->per_bfd->reading_partial_symbols)
8680 {
8681 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8682
8683 if (cu == NULL || cu->dies == NULL)
8684 return 1;
8685 return 0;
8686 }
8687
8688 /* Mark the dependence relation so that we don't flush PER_CU
8689 too early. */
8690 if (dependent_cu != NULL)
8691 dwarf2_add_dependence (dependent_cu, per_cu);
8692
8693 /* If it's already on the queue, we have nothing to do. */
8694 if (per_cu->queued)
8695 {
8696 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8697 loaded. */
8698 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
8699
8700 /* If the CU is queued for expansion, it should not already be
8701 expanded. */
8702 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8703
8704 /* The DIEs are already loaded, the caller doesn't need to do it. */
8705 return 0;
8706 }
8707
8708 bool queued = false;
8709 if (!per_objfile->symtab_set_p (per_cu))
8710 {
8711 /* Add it to the queue. */
8712 queue_comp_unit (per_cu, per_objfile, pretend_language);
8713 queued = true;
8714 }
8715
8716 /* If the compilation unit is already loaded, just mark it as
8717 used. */
8718 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8719 if (cu != nullptr)
8720 cu->last_used = 0;
8721
8722 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8723 and the DIEs are not already loaded. */
8724 return queued && cu == nullptr;
8725 }
8726
8727 /* Process the queue. */
8728
8729 static void
8730 process_queue (dwarf2_per_objfile *per_objfile)
8731 {
8732 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8733 objfile_name (per_objfile->objfile));
8734
8735 /* The queue starts out with one item, but following a DIE reference
8736 may load a new CU, adding it to the end of the queue. */
8737 while (!per_objfile->per_bfd->queue->empty ())
8738 {
8739 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
8740 dwarf2_per_cu_data *per_cu = item.per_cu;
8741
8742 if (!per_objfile->symtab_set_p (per_cu))
8743 {
8744 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8745
8746 /* Skip dummy CUs. */
8747 if (cu != nullptr)
8748 {
8749 unsigned int debug_print_threshold;
8750 char buf[100];
8751
8752 if (per_cu->is_debug_types)
8753 {
8754 struct signatured_type *sig_type =
8755 (struct signatured_type *) per_cu;
8756
8757 sprintf (buf, "TU %s at offset %s",
8758 hex_string (sig_type->signature),
8759 sect_offset_str (per_cu->sect_off));
8760 /* There can be 100s of TUs.
8761 Only print them in verbose mode. */
8762 debug_print_threshold = 2;
8763 }
8764 else
8765 {
8766 sprintf (buf, "CU at offset %s",
8767 sect_offset_str (per_cu->sect_off));
8768 debug_print_threshold = 1;
8769 }
8770
8771 if (dwarf_read_debug >= debug_print_threshold)
8772 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
8773
8774 if (per_cu->is_debug_types)
8775 process_full_type_unit (cu, item.pretend_language);
8776 else
8777 process_full_comp_unit (cu, item.pretend_language);
8778
8779 if (dwarf_read_debug >= debug_print_threshold)
8780 dwarf_read_debug_printf ("Done expanding %s", buf);
8781 }
8782 }
8783
8784 per_cu->queued = 0;
8785 per_objfile->per_bfd->queue->pop ();
8786 }
8787
8788 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8789 objfile_name (per_objfile->objfile));
8790 }
8791
8792 /* Read in full symbols for PST, and anything it depends on. */
8793
8794 void
8795 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8796 {
8797 gdb_assert (!readin_p (objfile));
8798
8799 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8800 free_cached_comp_units freer (per_objfile);
8801 expand_dependencies (objfile);
8802
8803 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
8804 gdb_assert (get_compunit_symtab (objfile) != nullptr);
8805 }
8806
8807 /* See psympriv.h. */
8808
8809 bool
8810 dwarf2_psymtab::readin_p (struct objfile *objfile) const
8811 {
8812 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8813 return per_objfile->symtab_set_p (per_cu_data);
8814 }
8815
8816 /* See psympriv.h. */
8817
8818 compunit_symtab *
8819 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8820 {
8821 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8822 return per_objfile->get_symtab (per_cu_data);
8823 }
8824
8825 /* Trivial hash function for die_info: the hash value of a DIE
8826 is its offset in .debug_info for this objfile. */
8827
8828 static hashval_t
8829 die_hash (const void *item)
8830 {
8831 const struct die_info *die = (const struct die_info *) item;
8832
8833 return to_underlying (die->sect_off);
8834 }
8835
8836 /* Trivial comparison function for die_info structures: two DIEs
8837 are equal if they have the same offset. */
8838
8839 static int
8840 die_eq (const void *item_lhs, const void *item_rhs)
8841 {
8842 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8843 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8844
8845 return die_lhs->sect_off == die_rhs->sect_off;
8846 }
8847
8848 /* Load the DIEs associated with PER_CU into memory.
8849
8850 In some cases, the caller, while reading partial symbols, will need to load
8851 the full symbols for the CU for some reason. It will already have a
8852 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8853 rather than creating a new one. */
8854
8855 static void
8856 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8857 dwarf2_per_objfile *per_objfile,
8858 dwarf2_cu *existing_cu,
8859 bool skip_partial,
8860 enum language pretend_language)
8861 {
8862 gdb_assert (! this_cu->is_debug_types);
8863
8864 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
8865 if (reader.dummy_p)
8866 return;
8867
8868 struct dwarf2_cu *cu = reader.cu;
8869 const gdb_byte *info_ptr = reader.info_ptr;
8870
8871 gdb_assert (cu->die_hash == NULL);
8872 cu->die_hash =
8873 htab_create_alloc_ex (cu->header.length / 12,
8874 die_hash,
8875 die_eq,
8876 NULL,
8877 &cu->comp_unit_obstack,
8878 hashtab_obstack_allocate,
8879 dummy_obstack_deallocate);
8880
8881 if (reader.comp_unit_die->has_children)
8882 reader.comp_unit_die->child
8883 = read_die_and_siblings (&reader, reader.info_ptr,
8884 &info_ptr, reader.comp_unit_die);
8885 cu->dies = reader.comp_unit_die;
8886 /* comp_unit_die is not stored in die_hash, no need. */
8887
8888 /* We try not to read any attributes in this function, because not
8889 all CUs needed for references have been loaded yet, and symbol
8890 table processing isn't initialized. But we have to set the CU language,
8891 or we won't be able to build types correctly.
8892 Similarly, if we do not read the producer, we can not apply
8893 producer-specific interpretation. */
8894 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8895
8896 reader.keep ();
8897 }
8898
8899 /* Add a DIE to the delayed physname list. */
8900
8901 static void
8902 add_to_method_list (struct type *type, int fnfield_index, int index,
8903 const char *name, struct die_info *die,
8904 struct dwarf2_cu *cu)
8905 {
8906 struct delayed_method_info mi;
8907 mi.type = type;
8908 mi.fnfield_index = fnfield_index;
8909 mi.index = index;
8910 mi.name = name;
8911 mi.die = die;
8912 cu->method_list.push_back (mi);
8913 }
8914
8915 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8916 "const" / "volatile". If so, decrements LEN by the length of the
8917 modifier and return true. Otherwise return false. */
8918
8919 template<size_t N>
8920 static bool
8921 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8922 {
8923 size_t mod_len = sizeof (mod) - 1;
8924 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8925 {
8926 len -= mod_len;
8927 return true;
8928 }
8929 return false;
8930 }
8931
8932 /* Compute the physnames of any methods on the CU's method list.
8933
8934 The computation of method physnames is delayed in order to avoid the
8935 (bad) condition that one of the method's formal parameters is of an as yet
8936 incomplete type. */
8937
8938 static void
8939 compute_delayed_physnames (struct dwarf2_cu *cu)
8940 {
8941 /* Only C++ delays computing physnames. */
8942 if (cu->method_list.empty ())
8943 return;
8944 gdb_assert (cu->language == language_cplus);
8945
8946 for (const delayed_method_info &mi : cu->method_list)
8947 {
8948 const char *physname;
8949 struct fn_fieldlist *fn_flp
8950 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8951 physname = dwarf2_physname (mi.name, mi.die, cu);
8952 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8953 = physname ? physname : "";
8954
8955 /* Since there's no tag to indicate whether a method is a
8956 const/volatile overload, extract that information out of the
8957 demangled name. */
8958 if (physname != NULL)
8959 {
8960 size_t len = strlen (physname);
8961
8962 while (1)
8963 {
8964 if (physname[len] == ')') /* shortcut */
8965 break;
8966 else if (check_modifier (physname, len, " const"))
8967 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8968 else if (check_modifier (physname, len, " volatile"))
8969 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8970 else
8971 break;
8972 }
8973 }
8974 }
8975
8976 /* The list is no longer needed. */
8977 cu->method_list.clear ();
8978 }
8979
8980 /* Go objects should be embedded in a DW_TAG_module DIE,
8981 and it's not clear if/how imported objects will appear.
8982 To keep Go support simple until that's worked out,
8983 go back through what we've read and create something usable.
8984 We could do this while processing each DIE, and feels kinda cleaner,
8985 but that way is more invasive.
8986 This is to, for example, allow the user to type "p var" or "b main"
8987 without having to specify the package name, and allow lookups
8988 of module.object to work in contexts that use the expression
8989 parser. */
8990
8991 static void
8992 fixup_go_packaging (struct dwarf2_cu *cu)
8993 {
8994 gdb::unique_xmalloc_ptr<char> package_name;
8995 struct pending *list;
8996 int i;
8997
8998 for (list = *cu->get_builder ()->get_global_symbols ();
8999 list != NULL;
9000 list = list->next)
9001 {
9002 for (i = 0; i < list->nsyms; ++i)
9003 {
9004 struct symbol *sym = list->symbol[i];
9005
9006 if (sym->language () == language_go
9007 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9008 {
9009 gdb::unique_xmalloc_ptr<char> this_package_name
9010 (go_symbol_package_name (sym));
9011
9012 if (this_package_name == NULL)
9013 continue;
9014 if (package_name == NULL)
9015 package_name = std::move (this_package_name);
9016 else
9017 {
9018 struct objfile *objfile = cu->per_objfile->objfile;
9019 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9020 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9021 (symbol_symtab (sym) != NULL
9022 ? symtab_to_filename_for_display
9023 (symbol_symtab (sym))
9024 : objfile_name (objfile)),
9025 this_package_name.get (), package_name.get ());
9026 }
9027 }
9028 }
9029 }
9030
9031 if (package_name != NULL)
9032 {
9033 struct objfile *objfile = cu->per_objfile->objfile;
9034 const char *saved_package_name = objfile->intern (package_name.get ());
9035 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9036 saved_package_name);
9037 struct symbol *sym;
9038
9039 sym = new (&objfile->objfile_obstack) symbol;
9040 sym->set_language (language_go, &objfile->objfile_obstack);
9041 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9042 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9043 e.g., "main" finds the "main" module and not C's main(). */
9044 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9045 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9046 SYMBOL_TYPE (sym) = type;
9047
9048 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9049 }
9050 }
9051
9052 /* Allocate a fully-qualified name consisting of the two parts on the
9053 obstack. */
9054
9055 static const char *
9056 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9057 {
9058 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9059 }
9060
9061 /* A helper that allocates a variant part to attach to a Rust enum
9062 type. OBSTACK is where the results should be allocated. TYPE is
9063 the type we're processing. DISCRIMINANT_INDEX is the index of the
9064 discriminant. It must be the index of one of the fields of TYPE,
9065 or -1 to mean there is no discriminant (univariant enum).
9066 DEFAULT_INDEX is the index of the default field; or -1 if there is
9067 no default. RANGES is indexed by "effective" field number (the
9068 field index, but omitting the discriminant and default fields) and
9069 must hold the discriminant values used by the variants. Note that
9070 RANGES must have a lifetime at least as long as OBSTACK -- either
9071 already allocated on it, or static. */
9072
9073 static void
9074 alloc_rust_variant (struct obstack *obstack, struct type *type,
9075 int discriminant_index, int default_index,
9076 gdb::array_view<discriminant_range> ranges)
9077 {
9078 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9079 gdb_assert (discriminant_index == -1
9080 || (discriminant_index >= 0
9081 && discriminant_index < type->num_fields ()));
9082 gdb_assert (default_index == -1
9083 || (default_index >= 0 && default_index < type->num_fields ()));
9084
9085 /* We have one variant for each non-discriminant field. */
9086 int n_variants = type->num_fields ();
9087 if (discriminant_index != -1)
9088 --n_variants;
9089
9090 variant *variants = new (obstack) variant[n_variants];
9091 int var_idx = 0;
9092 int range_idx = 0;
9093 for (int i = 0; i < type->num_fields (); ++i)
9094 {
9095 if (i == discriminant_index)
9096 continue;
9097
9098 variants[var_idx].first_field = i;
9099 variants[var_idx].last_field = i + 1;
9100
9101 /* The default field does not need a range, but other fields do.
9102 We skipped the discriminant above. */
9103 if (i != default_index)
9104 {
9105 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9106 ++range_idx;
9107 }
9108
9109 ++var_idx;
9110 }
9111
9112 gdb_assert (range_idx == ranges.size ());
9113 gdb_assert (var_idx == n_variants);
9114
9115 variant_part *part = new (obstack) variant_part;
9116 part->discriminant_index = discriminant_index;
9117 /* If there is no discriminant, then whether it is signed is of no
9118 consequence. */
9119 part->is_unsigned
9120 = (discriminant_index == -1
9121 ? false
9122 : type->field (discriminant_index).type ()->is_unsigned ());
9123 part->variants = gdb::array_view<variant> (variants, n_variants);
9124
9125 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9126 gdb::array_view<variant_part> *prop_value
9127 = new (storage) gdb::array_view<variant_part> (part, 1);
9128
9129 struct dynamic_prop prop;
9130 prop.set_variant_parts (prop_value);
9131
9132 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9133 }
9134
9135 /* Some versions of rustc emitted enums in an unusual way.
9136
9137 Ordinary enums were emitted as unions. The first element of each
9138 structure in the union was named "RUST$ENUM$DISR". This element
9139 held the discriminant.
9140
9141 These versions of Rust also implemented the "non-zero"
9142 optimization. When the enum had two values, and one is empty and
9143 the other holds a pointer that cannot be zero, the pointer is used
9144 as the discriminant, with a zero value meaning the empty variant.
9145 Here, the union's first member is of the form
9146 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9147 where the fieldnos are the indices of the fields that should be
9148 traversed in order to find the field (which may be several fields deep)
9149 and the variantname is the name of the variant of the case when the
9150 field is zero.
9151
9152 This function recognizes whether TYPE is of one of these forms,
9153 and, if so, smashes it to be a variant type. */
9154
9155 static void
9156 quirk_rust_enum (struct type *type, struct objfile *objfile)
9157 {
9158 gdb_assert (type->code () == TYPE_CODE_UNION);
9159
9160 /* We don't need to deal with empty enums. */
9161 if (type->num_fields () == 0)
9162 return;
9163
9164 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9165 if (type->num_fields () == 1
9166 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9167 {
9168 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9169
9170 /* Decode the field name to find the offset of the
9171 discriminant. */
9172 ULONGEST bit_offset = 0;
9173 struct type *field_type = type->field (0).type ();
9174 while (name[0] >= '0' && name[0] <= '9')
9175 {
9176 char *tail;
9177 unsigned long index = strtoul (name, &tail, 10);
9178 name = tail;
9179 if (*name != '$'
9180 || index >= field_type->num_fields ()
9181 || (TYPE_FIELD_LOC_KIND (field_type, index)
9182 != FIELD_LOC_KIND_BITPOS))
9183 {
9184 complaint (_("Could not parse Rust enum encoding string \"%s\""
9185 "[in module %s]"),
9186 TYPE_FIELD_NAME (type, 0),
9187 objfile_name (objfile));
9188 return;
9189 }
9190 ++name;
9191
9192 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9193 field_type = field_type->field (index).type ();
9194 }
9195
9196 /* Smash this type to be a structure type. We have to do this
9197 because the type has already been recorded. */
9198 type->set_code (TYPE_CODE_STRUCT);
9199 type->set_num_fields (3);
9200 /* Save the field we care about. */
9201 struct field saved_field = type->field (0);
9202 type->set_fields
9203 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9204
9205 /* Put the discriminant at index 0. */
9206 type->field (0).set_type (field_type);
9207 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9208 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9209 SET_FIELD_BITPOS (type->field (0), bit_offset);
9210
9211 /* The order of fields doesn't really matter, so put the real
9212 field at index 1 and the data-less field at index 2. */
9213 type->field (1) = saved_field;
9214 TYPE_FIELD_NAME (type, 1)
9215 = rust_last_path_segment (type->field (1).type ()->name ());
9216 type->field (1).type ()->set_name
9217 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9218 TYPE_FIELD_NAME (type, 1)));
9219
9220 const char *dataless_name
9221 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9222 name);
9223 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9224 dataless_name);
9225 type->field (2).set_type (dataless_type);
9226 /* NAME points into the original discriminant name, which
9227 already has the correct lifetime. */
9228 TYPE_FIELD_NAME (type, 2) = name;
9229 SET_FIELD_BITPOS (type->field (2), 0);
9230
9231 /* Indicate that this is a variant type. */
9232 static discriminant_range ranges[1] = { { 0, 0 } };
9233 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9234 }
9235 /* A union with a single anonymous field is probably an old-style
9236 univariant enum. */
9237 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9238 {
9239 /* Smash this type to be a structure type. We have to do this
9240 because the type has already been recorded. */
9241 type->set_code (TYPE_CODE_STRUCT);
9242
9243 struct type *field_type = type->field (0).type ();
9244 const char *variant_name
9245 = rust_last_path_segment (field_type->name ());
9246 TYPE_FIELD_NAME (type, 0) = variant_name;
9247 field_type->set_name
9248 (rust_fully_qualify (&objfile->objfile_obstack,
9249 type->name (), variant_name));
9250
9251 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9252 }
9253 else
9254 {
9255 struct type *disr_type = nullptr;
9256 for (int i = 0; i < type->num_fields (); ++i)
9257 {
9258 disr_type = type->field (i).type ();
9259
9260 if (disr_type->code () != TYPE_CODE_STRUCT)
9261 {
9262 /* All fields of a true enum will be structs. */
9263 return;
9264 }
9265 else if (disr_type->num_fields () == 0)
9266 {
9267 /* Could be data-less variant, so keep going. */
9268 disr_type = nullptr;
9269 }
9270 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9271 "RUST$ENUM$DISR") != 0)
9272 {
9273 /* Not a Rust enum. */
9274 return;
9275 }
9276 else
9277 {
9278 /* Found one. */
9279 break;
9280 }
9281 }
9282
9283 /* If we got here without a discriminant, then it's probably
9284 just a union. */
9285 if (disr_type == nullptr)
9286 return;
9287
9288 /* Smash this type to be a structure type. We have to do this
9289 because the type has already been recorded. */
9290 type->set_code (TYPE_CODE_STRUCT);
9291
9292 /* Make space for the discriminant field. */
9293 struct field *disr_field = &disr_type->field (0);
9294 field *new_fields
9295 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9296 * sizeof (struct field)));
9297 memcpy (new_fields + 1, type->fields (),
9298 type->num_fields () * sizeof (struct field));
9299 type->set_fields (new_fields);
9300 type->set_num_fields (type->num_fields () + 1);
9301
9302 /* Install the discriminant at index 0 in the union. */
9303 type->field (0) = *disr_field;
9304 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9305 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9306
9307 /* We need a way to find the correct discriminant given a
9308 variant name. For convenience we build a map here. */
9309 struct type *enum_type = disr_field->type ();
9310 std::unordered_map<std::string, ULONGEST> discriminant_map;
9311 for (int i = 0; i < enum_type->num_fields (); ++i)
9312 {
9313 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9314 {
9315 const char *name
9316 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9317 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9318 }
9319 }
9320
9321 int n_fields = type->num_fields ();
9322 /* We don't need a range entry for the discriminant, but we do
9323 need one for every other field, as there is no default
9324 variant. */
9325 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9326 discriminant_range,
9327 n_fields - 1);
9328 /* Skip the discriminant here. */
9329 for (int i = 1; i < n_fields; ++i)
9330 {
9331 /* Find the final word in the name of this variant's type.
9332 That name can be used to look up the correct
9333 discriminant. */
9334 const char *variant_name
9335 = rust_last_path_segment (type->field (i).type ()->name ());
9336
9337 auto iter = discriminant_map.find (variant_name);
9338 if (iter != discriminant_map.end ())
9339 {
9340 ranges[i - 1].low = iter->second;
9341 ranges[i - 1].high = iter->second;
9342 }
9343
9344 /* In Rust, each element should have the size of the
9345 enclosing enum. */
9346 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9347
9348 /* Remove the discriminant field, if it exists. */
9349 struct type *sub_type = type->field (i).type ();
9350 if (sub_type->num_fields () > 0)
9351 {
9352 sub_type->set_num_fields (sub_type->num_fields () - 1);
9353 sub_type->set_fields (sub_type->fields () + 1);
9354 }
9355 TYPE_FIELD_NAME (type, i) = variant_name;
9356 sub_type->set_name
9357 (rust_fully_qualify (&objfile->objfile_obstack,
9358 type->name (), variant_name));
9359 }
9360
9361 /* Indicate that this is a variant type. */
9362 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9363 gdb::array_view<discriminant_range> (ranges,
9364 n_fields - 1));
9365 }
9366 }
9367
9368 /* Rewrite some Rust unions to be structures with variants parts. */
9369
9370 static void
9371 rust_union_quirks (struct dwarf2_cu *cu)
9372 {
9373 gdb_assert (cu->language == language_rust);
9374 for (type *type_ : cu->rust_unions)
9375 quirk_rust_enum (type_, cu->per_objfile->objfile);
9376 /* We don't need this any more. */
9377 cu->rust_unions.clear ();
9378 }
9379
9380 /* See read.h. */
9381
9382 type_unit_group_unshareable *
9383 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9384 {
9385 auto iter = this->m_type_units.find (tu_group);
9386 if (iter != this->m_type_units.end ())
9387 return iter->second.get ();
9388
9389 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9390 type_unit_group_unshareable *result = uniq.get ();
9391 this->m_type_units[tu_group] = std::move (uniq);
9392 return result;
9393 }
9394
9395 struct type *
9396 dwarf2_per_objfile::get_type_for_signatured_type
9397 (signatured_type *sig_type) const
9398 {
9399 auto iter = this->m_type_map.find (sig_type);
9400 if (iter == this->m_type_map.end ())
9401 return nullptr;
9402
9403 return iter->second;
9404 }
9405
9406 void dwarf2_per_objfile::set_type_for_signatured_type
9407 (signatured_type *sig_type, struct type *type)
9408 {
9409 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9410
9411 this->m_type_map[sig_type] = type;
9412 }
9413
9414 /* A helper function for computing the list of all symbol tables
9415 included by PER_CU. */
9416
9417 static void
9418 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9419 htab_t all_children, htab_t all_type_symtabs,
9420 dwarf2_per_cu_data *per_cu,
9421 dwarf2_per_objfile *per_objfile,
9422 struct compunit_symtab *immediate_parent)
9423 {
9424 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9425 if (*slot != NULL)
9426 {
9427 /* This inclusion and its children have been processed. */
9428 return;
9429 }
9430
9431 *slot = per_cu;
9432
9433 /* Only add a CU if it has a symbol table. */
9434 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9435 if (cust != NULL)
9436 {
9437 /* If this is a type unit only add its symbol table if we haven't
9438 seen it yet (type unit per_cu's can share symtabs). */
9439 if (per_cu->is_debug_types)
9440 {
9441 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9442 if (*slot == NULL)
9443 {
9444 *slot = cust;
9445 result->push_back (cust);
9446 if (cust->user == NULL)
9447 cust->user = immediate_parent;
9448 }
9449 }
9450 else
9451 {
9452 result->push_back (cust);
9453 if (cust->user == NULL)
9454 cust->user = immediate_parent;
9455 }
9456 }
9457
9458 if (!per_cu->imported_symtabs_empty ())
9459 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9460 {
9461 recursively_compute_inclusions (result, all_children,
9462 all_type_symtabs, ptr, per_objfile,
9463 cust);
9464 }
9465 }
9466
9467 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9468 PER_CU. */
9469
9470 static void
9471 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9472 dwarf2_per_objfile *per_objfile)
9473 {
9474 gdb_assert (! per_cu->is_debug_types);
9475
9476 if (!per_cu->imported_symtabs_empty ())
9477 {
9478 int len;
9479 std::vector<compunit_symtab *> result_symtabs;
9480 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9481
9482 /* If we don't have a symtab, we can just skip this case. */
9483 if (cust == NULL)
9484 return;
9485
9486 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9487 htab_eq_pointer,
9488 NULL, xcalloc, xfree));
9489 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9490 htab_eq_pointer,
9491 NULL, xcalloc, xfree));
9492
9493 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9494 {
9495 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9496 all_type_symtabs.get (), ptr,
9497 per_objfile, cust);
9498 }
9499
9500 /* Now we have a transitive closure of all the included symtabs. */
9501 len = result_symtabs.size ();
9502 cust->includes
9503 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9504 struct compunit_symtab *, len + 1);
9505 memcpy (cust->includes, result_symtabs.data (),
9506 len * sizeof (compunit_symtab *));
9507 cust->includes[len] = NULL;
9508 }
9509 }
9510
9511 /* Compute the 'includes' field for the symtabs of all the CUs we just
9512 read. */
9513
9514 static void
9515 process_cu_includes (dwarf2_per_objfile *per_objfile)
9516 {
9517 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9518 {
9519 if (! iter->is_debug_types)
9520 compute_compunit_symtab_includes (iter, per_objfile);
9521 }
9522
9523 per_objfile->per_bfd->just_read_cus.clear ();
9524 }
9525
9526 /* Generate full symbol information for CU, whose DIEs have
9527 already been loaded into memory. */
9528
9529 static void
9530 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9531 {
9532 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9533 struct objfile *objfile = per_objfile->objfile;
9534 struct gdbarch *gdbarch = objfile->arch ();
9535 CORE_ADDR lowpc, highpc;
9536 struct compunit_symtab *cust;
9537 CORE_ADDR baseaddr;
9538 struct block *static_block;
9539 CORE_ADDR addr;
9540
9541 baseaddr = objfile->text_section_offset ();
9542
9543 /* Clear the list here in case something was left over. */
9544 cu->method_list.clear ();
9545
9546 cu->language = pretend_language;
9547 cu->language_defn = language_def (cu->language);
9548
9549 dwarf2_find_base_address (cu->dies, cu);
9550
9551 /* Before we start reading the top-level DIE, ensure it has a valid tag
9552 type. */
9553 switch (cu->dies->tag)
9554 {
9555 case DW_TAG_compile_unit:
9556 case DW_TAG_partial_unit:
9557 case DW_TAG_type_unit:
9558 break;
9559 default:
9560 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9561 dwarf_tag_name (cu->dies->tag),
9562 sect_offset_str (cu->per_cu->sect_off),
9563 objfile_name (per_objfile->objfile));
9564 }
9565
9566 /* Do line number decoding in read_file_scope () */
9567 process_die (cu->dies, cu);
9568
9569 /* For now fudge the Go package. */
9570 if (cu->language == language_go)
9571 fixup_go_packaging (cu);
9572
9573 /* Now that we have processed all the DIEs in the CU, all the types
9574 should be complete, and it should now be safe to compute all of the
9575 physnames. */
9576 compute_delayed_physnames (cu);
9577
9578 if (cu->language == language_rust)
9579 rust_union_quirks (cu);
9580
9581 /* Some compilers don't define a DW_AT_high_pc attribute for the
9582 compilation unit. If the DW_AT_high_pc is missing, synthesize
9583 it, by scanning the DIE's below the compilation unit. */
9584 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9585
9586 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9587 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9588
9589 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9590 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9591 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9592 addrmap to help ensure it has an accurate map of pc values belonging to
9593 this comp unit. */
9594 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9595
9596 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9597 SECT_OFF_TEXT (objfile),
9598 0);
9599
9600 if (cust != NULL)
9601 {
9602 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9603
9604 /* Set symtab language to language from DW_AT_language. If the
9605 compilation is from a C file generated by language preprocessors, do
9606 not set the language if it was already deduced by start_subfile. */
9607 if (!(cu->language == language_c
9608 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9609 COMPUNIT_FILETABS (cust)->language = cu->language;
9610
9611 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9612 produce DW_AT_location with location lists but it can be possibly
9613 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9614 there were bugs in prologue debug info, fixed later in GCC-4.5
9615 by "unwind info for epilogues" patch (which is not directly related).
9616
9617 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9618 needed, it would be wrong due to missing DW_AT_producer there.
9619
9620 Still one can confuse GDB by using non-standard GCC compilation
9621 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9622 */
9623 if (cu->has_loclist && gcc_4_minor >= 5)
9624 cust->locations_valid = 1;
9625
9626 if (gcc_4_minor >= 5)
9627 cust->epilogue_unwind_valid = 1;
9628
9629 cust->call_site_htab = cu->call_site_htab;
9630 }
9631
9632 per_objfile->set_symtab (cu->per_cu, cust);
9633
9634 /* Push it for inclusion processing later. */
9635 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9636
9637 /* Not needed any more. */
9638 cu->reset_builder ();
9639 }
9640
9641 /* Generate full symbol information for type unit CU, whose DIEs have
9642 already been loaded into memory. */
9643
9644 static void
9645 process_full_type_unit (dwarf2_cu *cu,
9646 enum language pretend_language)
9647 {
9648 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9649 struct objfile *objfile = per_objfile->objfile;
9650 struct compunit_symtab *cust;
9651 struct signatured_type *sig_type;
9652
9653 gdb_assert (cu->per_cu->is_debug_types);
9654 sig_type = (struct signatured_type *) cu->per_cu;
9655
9656 /* Clear the list here in case something was left over. */
9657 cu->method_list.clear ();
9658
9659 cu->language = pretend_language;
9660 cu->language_defn = language_def (cu->language);
9661
9662 /* The symbol tables are set up in read_type_unit_scope. */
9663 process_die (cu->dies, cu);
9664
9665 /* For now fudge the Go package. */
9666 if (cu->language == language_go)
9667 fixup_go_packaging (cu);
9668
9669 /* Now that we have processed all the DIEs in the CU, all the types
9670 should be complete, and it should now be safe to compute all of the
9671 physnames. */
9672 compute_delayed_physnames (cu);
9673
9674 if (cu->language == language_rust)
9675 rust_union_quirks (cu);
9676
9677 /* TUs share symbol tables.
9678 If this is the first TU to use this symtab, complete the construction
9679 of it with end_expandable_symtab. Otherwise, complete the addition of
9680 this TU's symbols to the existing symtab. */
9681 type_unit_group_unshareable *tug_unshare =
9682 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9683 if (tug_unshare->compunit_symtab == NULL)
9684 {
9685 buildsym_compunit *builder = cu->get_builder ();
9686 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9687 tug_unshare->compunit_symtab = cust;
9688
9689 if (cust != NULL)
9690 {
9691 /* Set symtab language to language from DW_AT_language. If the
9692 compilation is from a C file generated by language preprocessors,
9693 do not set the language if it was already deduced by
9694 start_subfile. */
9695 if (!(cu->language == language_c
9696 && COMPUNIT_FILETABS (cust)->language != language_c))
9697 COMPUNIT_FILETABS (cust)->language = cu->language;
9698 }
9699 }
9700 else
9701 {
9702 cu->get_builder ()->augment_type_symtab ();
9703 cust = tug_unshare->compunit_symtab;
9704 }
9705
9706 per_objfile->set_symtab (cu->per_cu, cust);
9707
9708 /* Not needed any more. */
9709 cu->reset_builder ();
9710 }
9711
9712 /* Process an imported unit DIE. */
9713
9714 static void
9715 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9716 {
9717 struct attribute *attr;
9718
9719 /* For now we don't handle imported units in type units. */
9720 if (cu->per_cu->is_debug_types)
9721 {
9722 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9723 " supported in type units [in module %s]"),
9724 objfile_name (cu->per_objfile->objfile));
9725 }
9726
9727 attr = dwarf2_attr (die, DW_AT_import, cu);
9728 if (attr != NULL)
9729 {
9730 sect_offset sect_off = attr->get_ref_die_offset ();
9731 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9732 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9733 dwarf2_per_cu_data *per_cu
9734 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
9735
9736 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9737 into another compilation unit, at root level. Regard this as a hint,
9738 and ignore it. */
9739 if (die->parent && die->parent->parent == NULL
9740 && per_cu->unit_type == DW_UT_compile
9741 && per_cu->lang == language_cplus)
9742 return;
9743
9744 /* If necessary, add it to the queue and load its DIEs. */
9745 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
9746 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9747 false, cu->language);
9748
9749 cu->per_cu->imported_symtabs_push (per_cu);
9750 }
9751 }
9752
9753 /* RAII object that represents a process_die scope: i.e.,
9754 starts/finishes processing a DIE. */
9755 class process_die_scope
9756 {
9757 public:
9758 process_die_scope (die_info *die, dwarf2_cu *cu)
9759 : m_die (die), m_cu (cu)
9760 {
9761 /* We should only be processing DIEs not already in process. */
9762 gdb_assert (!m_die->in_process);
9763 m_die->in_process = true;
9764 }
9765
9766 ~process_die_scope ()
9767 {
9768 m_die->in_process = false;
9769
9770 /* If we're done processing the DIE for the CU that owns the line
9771 header, we don't need the line header anymore. */
9772 if (m_cu->line_header_die_owner == m_die)
9773 {
9774 delete m_cu->line_header;
9775 m_cu->line_header = NULL;
9776 m_cu->line_header_die_owner = NULL;
9777 }
9778 }
9779
9780 private:
9781 die_info *m_die;
9782 dwarf2_cu *m_cu;
9783 };
9784
9785 /* Process a die and its children. */
9786
9787 static void
9788 process_die (struct die_info *die, struct dwarf2_cu *cu)
9789 {
9790 process_die_scope scope (die, cu);
9791
9792 switch (die->tag)
9793 {
9794 case DW_TAG_padding:
9795 break;
9796 case DW_TAG_compile_unit:
9797 case DW_TAG_partial_unit:
9798 read_file_scope (die, cu);
9799 break;
9800 case DW_TAG_type_unit:
9801 read_type_unit_scope (die, cu);
9802 break;
9803 case DW_TAG_subprogram:
9804 /* Nested subprograms in Fortran get a prefix. */
9805 if (cu->language == language_fortran
9806 && die->parent != NULL
9807 && die->parent->tag == DW_TAG_subprogram)
9808 cu->processing_has_namespace_info = true;
9809 /* Fall through. */
9810 case DW_TAG_inlined_subroutine:
9811 read_func_scope (die, cu);
9812 break;
9813 case DW_TAG_lexical_block:
9814 case DW_TAG_try_block:
9815 case DW_TAG_catch_block:
9816 read_lexical_block_scope (die, cu);
9817 break;
9818 case DW_TAG_call_site:
9819 case DW_TAG_GNU_call_site:
9820 read_call_site_scope (die, cu);
9821 break;
9822 case DW_TAG_class_type:
9823 case DW_TAG_interface_type:
9824 case DW_TAG_structure_type:
9825 case DW_TAG_union_type:
9826 process_structure_scope (die, cu);
9827 break;
9828 case DW_TAG_enumeration_type:
9829 process_enumeration_scope (die, cu);
9830 break;
9831
9832 /* These dies have a type, but processing them does not create
9833 a symbol or recurse to process the children. Therefore we can
9834 read them on-demand through read_type_die. */
9835 case DW_TAG_subroutine_type:
9836 case DW_TAG_set_type:
9837 case DW_TAG_pointer_type:
9838 case DW_TAG_ptr_to_member_type:
9839 case DW_TAG_reference_type:
9840 case DW_TAG_rvalue_reference_type:
9841 case DW_TAG_string_type:
9842 break;
9843
9844 case DW_TAG_array_type:
9845 /* We only need to handle this case for Ada -- in other
9846 languages, it's normal for the compiler to emit a typedef
9847 instead. */
9848 if (cu->language != language_ada)
9849 break;
9850 /* FALLTHROUGH */
9851 case DW_TAG_base_type:
9852 case DW_TAG_subrange_type:
9853 case DW_TAG_typedef:
9854 /* Add a typedef symbol for the type definition, if it has a
9855 DW_AT_name. */
9856 new_symbol (die, read_type_die (die, cu), cu);
9857 break;
9858 case DW_TAG_common_block:
9859 read_common_block (die, cu);
9860 break;
9861 case DW_TAG_common_inclusion:
9862 break;
9863 case DW_TAG_namespace:
9864 cu->processing_has_namespace_info = true;
9865 read_namespace (die, cu);
9866 break;
9867 case DW_TAG_module:
9868 cu->processing_has_namespace_info = true;
9869 read_module (die, cu);
9870 break;
9871 case DW_TAG_imported_declaration:
9872 cu->processing_has_namespace_info = true;
9873 if (read_namespace_alias (die, cu))
9874 break;
9875 /* The declaration is not a global namespace alias. */
9876 /* Fall through. */
9877 case DW_TAG_imported_module:
9878 cu->processing_has_namespace_info = true;
9879 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9880 || cu->language != language_fortran))
9881 complaint (_("Tag '%s' has unexpected children"),
9882 dwarf_tag_name (die->tag));
9883 read_import_statement (die, cu);
9884 break;
9885
9886 case DW_TAG_imported_unit:
9887 process_imported_unit_die (die, cu);
9888 break;
9889
9890 case DW_TAG_variable:
9891 read_variable (die, cu);
9892 break;
9893
9894 default:
9895 new_symbol (die, NULL, cu);
9896 break;
9897 }
9898 }
9899 \f
9900 /* DWARF name computation. */
9901
9902 /* A helper function for dwarf2_compute_name which determines whether DIE
9903 needs to have the name of the scope prepended to the name listed in the
9904 die. */
9905
9906 static int
9907 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9908 {
9909 struct attribute *attr;
9910
9911 switch (die->tag)
9912 {
9913 case DW_TAG_namespace:
9914 case DW_TAG_typedef:
9915 case DW_TAG_class_type:
9916 case DW_TAG_interface_type:
9917 case DW_TAG_structure_type:
9918 case DW_TAG_union_type:
9919 case DW_TAG_enumeration_type:
9920 case DW_TAG_enumerator:
9921 case DW_TAG_subprogram:
9922 case DW_TAG_inlined_subroutine:
9923 case DW_TAG_member:
9924 case DW_TAG_imported_declaration:
9925 return 1;
9926
9927 case DW_TAG_variable:
9928 case DW_TAG_constant:
9929 /* We only need to prefix "globally" visible variables. These include
9930 any variable marked with DW_AT_external or any variable that
9931 lives in a namespace. [Variables in anonymous namespaces
9932 require prefixing, but they are not DW_AT_external.] */
9933
9934 if (dwarf2_attr (die, DW_AT_specification, cu))
9935 {
9936 struct dwarf2_cu *spec_cu = cu;
9937
9938 return die_needs_namespace (die_specification (die, &spec_cu),
9939 spec_cu);
9940 }
9941
9942 attr = dwarf2_attr (die, DW_AT_external, cu);
9943 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9944 && die->parent->tag != DW_TAG_module)
9945 return 0;
9946 /* A variable in a lexical block of some kind does not need a
9947 namespace, even though in C++ such variables may be external
9948 and have a mangled name. */
9949 if (die->parent->tag == DW_TAG_lexical_block
9950 || die->parent->tag == DW_TAG_try_block
9951 || die->parent->tag == DW_TAG_catch_block
9952 || die->parent->tag == DW_TAG_subprogram)
9953 return 0;
9954 return 1;
9955
9956 default:
9957 return 0;
9958 }
9959 }
9960
9961 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9962 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9963 defined for the given DIE. */
9964
9965 static struct attribute *
9966 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9967 {
9968 struct attribute *attr;
9969
9970 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9971 if (attr == NULL)
9972 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9973
9974 return attr;
9975 }
9976
9977 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9978 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9979 defined for the given DIE. */
9980
9981 static const char *
9982 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9983 {
9984 const char *linkage_name;
9985
9986 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9987 if (linkage_name == NULL)
9988 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9989
9990 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9991 See https://github.com/rust-lang/rust/issues/32925. */
9992 if (cu->language == language_rust && linkage_name != NULL
9993 && strchr (linkage_name, '{') != NULL)
9994 linkage_name = NULL;
9995
9996 return linkage_name;
9997 }
9998
9999 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10000 compute the physname for the object, which include a method's:
10001 - formal parameters (C++),
10002 - receiver type (Go),
10003
10004 The term "physname" is a bit confusing.
10005 For C++, for example, it is the demangled name.
10006 For Go, for example, it's the mangled name.
10007
10008 For Ada, return the DIE's linkage name rather than the fully qualified
10009 name. PHYSNAME is ignored..
10010
10011 The result is allocated on the objfile->per_bfd's obstack and
10012 canonicalized. */
10013
10014 static const char *
10015 dwarf2_compute_name (const char *name,
10016 struct die_info *die, struct dwarf2_cu *cu,
10017 int physname)
10018 {
10019 struct objfile *objfile = cu->per_objfile->objfile;
10020
10021 if (name == NULL)
10022 name = dwarf2_name (die, cu);
10023
10024 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10025 but otherwise compute it by typename_concat inside GDB.
10026 FIXME: Actually this is not really true, or at least not always true.
10027 It's all very confusing. compute_and_set_names doesn't try to demangle
10028 Fortran names because there is no mangling standard. So new_symbol
10029 will set the demangled name to the result of dwarf2_full_name, and it is
10030 the demangled name that GDB uses if it exists. */
10031 if (cu->language == language_ada
10032 || (cu->language == language_fortran && physname))
10033 {
10034 /* For Ada unit, we prefer the linkage name over the name, as
10035 the former contains the exported name, which the user expects
10036 to be able to reference. Ideally, we want the user to be able
10037 to reference this entity using either natural or linkage name,
10038 but we haven't started looking at this enhancement yet. */
10039 const char *linkage_name = dw2_linkage_name (die, cu);
10040
10041 if (linkage_name != NULL)
10042 return linkage_name;
10043 }
10044
10045 /* These are the only languages we know how to qualify names in. */
10046 if (name != NULL
10047 && (cu->language == language_cplus
10048 || cu->language == language_fortran || cu->language == language_d
10049 || cu->language == language_rust))
10050 {
10051 if (die_needs_namespace (die, cu))
10052 {
10053 const char *prefix;
10054 const char *canonical_name = NULL;
10055
10056 string_file buf;
10057
10058 prefix = determine_prefix (die, cu);
10059 if (*prefix != '\0')
10060 {
10061 gdb::unique_xmalloc_ptr<char> prefixed_name
10062 (typename_concat (NULL, prefix, name, physname, cu));
10063
10064 buf.puts (prefixed_name.get ());
10065 }
10066 else
10067 buf.puts (name);
10068
10069 /* Template parameters may be specified in the DIE's DW_AT_name, or
10070 as children with DW_TAG_template_type_param or
10071 DW_TAG_value_type_param. If the latter, add them to the name
10072 here. If the name already has template parameters, then
10073 skip this step; some versions of GCC emit both, and
10074 it is more efficient to use the pre-computed name.
10075
10076 Something to keep in mind about this process: it is very
10077 unlikely, or in some cases downright impossible, to produce
10078 something that will match the mangled name of a function.
10079 If the definition of the function has the same debug info,
10080 we should be able to match up with it anyway. But fallbacks
10081 using the minimal symbol, for instance to find a method
10082 implemented in a stripped copy of libstdc++, will not work.
10083 If we do not have debug info for the definition, we will have to
10084 match them up some other way.
10085
10086 When we do name matching there is a related problem with function
10087 templates; two instantiated function templates are allowed to
10088 differ only by their return types, which we do not add here. */
10089
10090 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10091 {
10092 struct attribute *attr;
10093 struct die_info *child;
10094 int first = 1;
10095 const language_defn *cplus_lang = language_def (cu->language);
10096
10097 die->building_fullname = 1;
10098
10099 for (child = die->child; child != NULL; child = child->sibling)
10100 {
10101 struct type *type;
10102 LONGEST value;
10103 const gdb_byte *bytes;
10104 struct dwarf2_locexpr_baton *baton;
10105 struct value *v;
10106
10107 if (child->tag != DW_TAG_template_type_param
10108 && child->tag != DW_TAG_template_value_param)
10109 continue;
10110
10111 if (first)
10112 {
10113 buf.puts ("<");
10114 first = 0;
10115 }
10116 else
10117 buf.puts (", ");
10118
10119 attr = dwarf2_attr (child, DW_AT_type, cu);
10120 if (attr == NULL)
10121 {
10122 complaint (_("template parameter missing DW_AT_type"));
10123 buf.puts ("UNKNOWN_TYPE");
10124 continue;
10125 }
10126 type = die_type (child, cu);
10127
10128 if (child->tag == DW_TAG_template_type_param)
10129 {
10130 cplus_lang->print_type (type, "", &buf, -1, 0,
10131 &type_print_raw_options);
10132 continue;
10133 }
10134
10135 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10136 if (attr == NULL)
10137 {
10138 complaint (_("template parameter missing "
10139 "DW_AT_const_value"));
10140 buf.puts ("UNKNOWN_VALUE");
10141 continue;
10142 }
10143
10144 dwarf2_const_value_attr (attr, type, name,
10145 &cu->comp_unit_obstack, cu,
10146 &value, &bytes, &baton);
10147
10148 if (type->has_no_signedness ())
10149 /* GDB prints characters as NUMBER 'CHAR'. If that's
10150 changed, this can use value_print instead. */
10151 cplus_lang->printchar (value, type, &buf);
10152 else
10153 {
10154 struct value_print_options opts;
10155
10156 if (baton != NULL)
10157 v = dwarf2_evaluate_loc_desc (type, NULL,
10158 baton->data,
10159 baton->size,
10160 baton->per_cu,
10161 baton->per_objfile);
10162 else if (bytes != NULL)
10163 {
10164 v = allocate_value (type);
10165 memcpy (value_contents_writeable (v), bytes,
10166 TYPE_LENGTH (type));
10167 }
10168 else
10169 v = value_from_longest (type, value);
10170
10171 /* Specify decimal so that we do not depend on
10172 the radix. */
10173 get_formatted_print_options (&opts, 'd');
10174 opts.raw = 1;
10175 value_print (v, &buf, &opts);
10176 release_value (v);
10177 }
10178 }
10179
10180 die->building_fullname = 0;
10181
10182 if (!first)
10183 {
10184 /* Close the argument list, with a space if necessary
10185 (nested templates). */
10186 if (!buf.empty () && buf.string ().back () == '>')
10187 buf.puts (" >");
10188 else
10189 buf.puts (">");
10190 }
10191 }
10192
10193 /* For C++ methods, append formal parameter type
10194 information, if PHYSNAME. */
10195
10196 if (physname && die->tag == DW_TAG_subprogram
10197 && cu->language == language_cplus)
10198 {
10199 struct type *type = read_type_die (die, cu);
10200
10201 c_type_print_args (type, &buf, 1, cu->language,
10202 &type_print_raw_options);
10203
10204 if (cu->language == language_cplus)
10205 {
10206 /* Assume that an artificial first parameter is
10207 "this", but do not crash if it is not. RealView
10208 marks unnamed (and thus unused) parameters as
10209 artificial; there is no way to differentiate
10210 the two cases. */
10211 if (type->num_fields () > 0
10212 && TYPE_FIELD_ARTIFICIAL (type, 0)
10213 && type->field (0).type ()->code () == TYPE_CODE_PTR
10214 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10215 buf.puts (" const");
10216 }
10217 }
10218
10219 const std::string &intermediate_name = buf.string ();
10220
10221 if (cu->language == language_cplus)
10222 canonical_name
10223 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10224 objfile);
10225
10226 /* If we only computed INTERMEDIATE_NAME, or if
10227 INTERMEDIATE_NAME is already canonical, then we need to
10228 intern it. */
10229 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10230 name = objfile->intern (intermediate_name);
10231 else
10232 name = canonical_name;
10233 }
10234 }
10235
10236 return name;
10237 }
10238
10239 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10240 If scope qualifiers are appropriate they will be added. The result
10241 will be allocated on the storage_obstack, or NULL if the DIE does
10242 not have a name. NAME may either be from a previous call to
10243 dwarf2_name or NULL.
10244
10245 The output string will be canonicalized (if C++). */
10246
10247 static const char *
10248 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10249 {
10250 return dwarf2_compute_name (name, die, cu, 0);
10251 }
10252
10253 /* Construct a physname for the given DIE in CU. NAME may either be
10254 from a previous call to dwarf2_name or NULL. The result will be
10255 allocated on the objfile_objstack or NULL if the DIE does not have a
10256 name.
10257
10258 The output string will be canonicalized (if C++). */
10259
10260 static const char *
10261 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10262 {
10263 struct objfile *objfile = cu->per_objfile->objfile;
10264 const char *retval, *mangled = NULL, *canon = NULL;
10265 int need_copy = 1;
10266
10267 /* In this case dwarf2_compute_name is just a shortcut not building anything
10268 on its own. */
10269 if (!die_needs_namespace (die, cu))
10270 return dwarf2_compute_name (name, die, cu, 1);
10271
10272 if (cu->language != language_rust)
10273 mangled = dw2_linkage_name (die, cu);
10274
10275 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10276 has computed. */
10277 gdb::unique_xmalloc_ptr<char> demangled;
10278 if (mangled != NULL)
10279 {
10280
10281 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10282 {
10283 /* Do nothing (do not demangle the symbol name). */
10284 }
10285 else
10286 {
10287 /* Use DMGL_RET_DROP for C++ template functions to suppress
10288 their return type. It is easier for GDB users to search
10289 for such functions as `name(params)' than `long name(params)'.
10290 In such case the minimal symbol names do not match the full
10291 symbol names but for template functions there is never a need
10292 to look up their definition from their declaration so
10293 the only disadvantage remains the minimal symbol variant
10294 `long name(params)' does not have the proper inferior type. */
10295 demangled.reset (gdb_demangle (mangled,
10296 (DMGL_PARAMS | DMGL_ANSI
10297 | DMGL_RET_DROP)));
10298 }
10299 if (demangled)
10300 canon = demangled.get ();
10301 else
10302 {
10303 canon = mangled;
10304 need_copy = 0;
10305 }
10306 }
10307
10308 if (canon == NULL || check_physname)
10309 {
10310 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10311
10312 if (canon != NULL && strcmp (physname, canon) != 0)
10313 {
10314 /* It may not mean a bug in GDB. The compiler could also
10315 compute DW_AT_linkage_name incorrectly. But in such case
10316 GDB would need to be bug-to-bug compatible. */
10317
10318 complaint (_("Computed physname <%s> does not match demangled <%s> "
10319 "(from linkage <%s>) - DIE at %s [in module %s]"),
10320 physname, canon, mangled, sect_offset_str (die->sect_off),
10321 objfile_name (objfile));
10322
10323 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10324 is available here - over computed PHYSNAME. It is safer
10325 against both buggy GDB and buggy compilers. */
10326
10327 retval = canon;
10328 }
10329 else
10330 {
10331 retval = physname;
10332 need_copy = 0;
10333 }
10334 }
10335 else
10336 retval = canon;
10337
10338 if (need_copy)
10339 retval = objfile->intern (retval);
10340
10341 return retval;
10342 }
10343
10344 /* Inspect DIE in CU for a namespace alias. If one exists, record
10345 a new symbol for it.
10346
10347 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10348
10349 static int
10350 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10351 {
10352 struct attribute *attr;
10353
10354 /* If the die does not have a name, this is not a namespace
10355 alias. */
10356 attr = dwarf2_attr (die, DW_AT_name, cu);
10357 if (attr != NULL)
10358 {
10359 int num;
10360 struct die_info *d = die;
10361 struct dwarf2_cu *imported_cu = cu;
10362
10363 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10364 keep inspecting DIEs until we hit the underlying import. */
10365 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10366 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10367 {
10368 attr = dwarf2_attr (d, DW_AT_import, cu);
10369 if (attr == NULL)
10370 break;
10371
10372 d = follow_die_ref (d, attr, &imported_cu);
10373 if (d->tag != DW_TAG_imported_declaration)
10374 break;
10375 }
10376
10377 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10378 {
10379 complaint (_("DIE at %s has too many recursively imported "
10380 "declarations"), sect_offset_str (d->sect_off));
10381 return 0;
10382 }
10383
10384 if (attr != NULL)
10385 {
10386 struct type *type;
10387 sect_offset sect_off = attr->get_ref_die_offset ();
10388
10389 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10390 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10391 {
10392 /* This declaration is a global namespace alias. Add
10393 a symbol for it whose type is the aliased namespace. */
10394 new_symbol (die, type, cu);
10395 return 1;
10396 }
10397 }
10398 }
10399
10400 return 0;
10401 }
10402
10403 /* Return the using directives repository (global or local?) to use in the
10404 current context for CU.
10405
10406 For Ada, imported declarations can materialize renamings, which *may* be
10407 global. However it is impossible (for now?) in DWARF to distinguish
10408 "external" imported declarations and "static" ones. As all imported
10409 declarations seem to be static in all other languages, make them all CU-wide
10410 global only in Ada. */
10411
10412 static struct using_direct **
10413 using_directives (struct dwarf2_cu *cu)
10414 {
10415 if (cu->language == language_ada
10416 && cu->get_builder ()->outermost_context_p ())
10417 return cu->get_builder ()->get_global_using_directives ();
10418 else
10419 return cu->get_builder ()->get_local_using_directives ();
10420 }
10421
10422 /* Read the import statement specified by the given die and record it. */
10423
10424 static void
10425 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10426 {
10427 struct objfile *objfile = cu->per_objfile->objfile;
10428 struct attribute *import_attr;
10429 struct die_info *imported_die, *child_die;
10430 struct dwarf2_cu *imported_cu;
10431 const char *imported_name;
10432 const char *imported_name_prefix;
10433 const char *canonical_name;
10434 const char *import_alias;
10435 const char *imported_declaration = NULL;
10436 const char *import_prefix;
10437 std::vector<const char *> excludes;
10438
10439 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10440 if (import_attr == NULL)
10441 {
10442 complaint (_("Tag '%s' has no DW_AT_import"),
10443 dwarf_tag_name (die->tag));
10444 return;
10445 }
10446
10447 imported_cu = cu;
10448 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10449 imported_name = dwarf2_name (imported_die, imported_cu);
10450 if (imported_name == NULL)
10451 {
10452 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10453
10454 The import in the following code:
10455 namespace A
10456 {
10457 typedef int B;
10458 }
10459
10460 int main ()
10461 {
10462 using A::B;
10463 B b;
10464 return b;
10465 }
10466
10467 ...
10468 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10469 <52> DW_AT_decl_file : 1
10470 <53> DW_AT_decl_line : 6
10471 <54> DW_AT_import : <0x75>
10472 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10473 <59> DW_AT_name : B
10474 <5b> DW_AT_decl_file : 1
10475 <5c> DW_AT_decl_line : 2
10476 <5d> DW_AT_type : <0x6e>
10477 ...
10478 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10479 <76> DW_AT_byte_size : 4
10480 <77> DW_AT_encoding : 5 (signed)
10481
10482 imports the wrong die ( 0x75 instead of 0x58 ).
10483 This case will be ignored until the gcc bug is fixed. */
10484 return;
10485 }
10486
10487 /* Figure out the local name after import. */
10488 import_alias = dwarf2_name (die, cu);
10489
10490 /* Figure out where the statement is being imported to. */
10491 import_prefix = determine_prefix (die, cu);
10492
10493 /* Figure out what the scope of the imported die is and prepend it
10494 to the name of the imported die. */
10495 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10496
10497 if (imported_die->tag != DW_TAG_namespace
10498 && imported_die->tag != DW_TAG_module)
10499 {
10500 imported_declaration = imported_name;
10501 canonical_name = imported_name_prefix;
10502 }
10503 else if (strlen (imported_name_prefix) > 0)
10504 canonical_name = obconcat (&objfile->objfile_obstack,
10505 imported_name_prefix,
10506 (cu->language == language_d ? "." : "::"),
10507 imported_name, (char *) NULL);
10508 else
10509 canonical_name = imported_name;
10510
10511 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10512 for (child_die = die->child; child_die && child_die->tag;
10513 child_die = child_die->sibling)
10514 {
10515 /* DWARF-4: A Fortran use statement with a “rename list” may be
10516 represented by an imported module entry with an import attribute
10517 referring to the module and owned entries corresponding to those
10518 entities that are renamed as part of being imported. */
10519
10520 if (child_die->tag != DW_TAG_imported_declaration)
10521 {
10522 complaint (_("child DW_TAG_imported_declaration expected "
10523 "- DIE at %s [in module %s]"),
10524 sect_offset_str (child_die->sect_off),
10525 objfile_name (objfile));
10526 continue;
10527 }
10528
10529 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10530 if (import_attr == NULL)
10531 {
10532 complaint (_("Tag '%s' has no DW_AT_import"),
10533 dwarf_tag_name (child_die->tag));
10534 continue;
10535 }
10536
10537 imported_cu = cu;
10538 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10539 &imported_cu);
10540 imported_name = dwarf2_name (imported_die, imported_cu);
10541 if (imported_name == NULL)
10542 {
10543 complaint (_("child DW_TAG_imported_declaration has unknown "
10544 "imported name - DIE at %s [in module %s]"),
10545 sect_offset_str (child_die->sect_off),
10546 objfile_name (objfile));
10547 continue;
10548 }
10549
10550 excludes.push_back (imported_name);
10551
10552 process_die (child_die, cu);
10553 }
10554
10555 add_using_directive (using_directives (cu),
10556 import_prefix,
10557 canonical_name,
10558 import_alias,
10559 imported_declaration,
10560 excludes,
10561 0,
10562 &objfile->objfile_obstack);
10563 }
10564
10565 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10566 types, but gives them a size of zero. Starting with version 14,
10567 ICC is compatible with GCC. */
10568
10569 static bool
10570 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10571 {
10572 if (!cu->checked_producer)
10573 check_producer (cu);
10574
10575 return cu->producer_is_icc_lt_14;
10576 }
10577
10578 /* ICC generates a DW_AT_type for C void functions. This was observed on
10579 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10580 which says that void functions should not have a DW_AT_type. */
10581
10582 static bool
10583 producer_is_icc (struct dwarf2_cu *cu)
10584 {
10585 if (!cu->checked_producer)
10586 check_producer (cu);
10587
10588 return cu->producer_is_icc;
10589 }
10590
10591 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10592 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10593 this, it was first present in GCC release 4.3.0. */
10594
10595 static bool
10596 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10597 {
10598 if (!cu->checked_producer)
10599 check_producer (cu);
10600
10601 return cu->producer_is_gcc_lt_4_3;
10602 }
10603
10604 static file_and_directory
10605 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10606 {
10607 file_and_directory res;
10608
10609 /* Find the filename. Do not use dwarf2_name here, since the filename
10610 is not a source language identifier. */
10611 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10612 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10613
10614 if (res.comp_dir == NULL
10615 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10616 && IS_ABSOLUTE_PATH (res.name))
10617 {
10618 res.comp_dir_storage = ldirname (res.name);
10619 if (!res.comp_dir_storage.empty ())
10620 res.comp_dir = res.comp_dir_storage.c_str ();
10621 }
10622 if (res.comp_dir != NULL)
10623 {
10624 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10625 directory, get rid of it. */
10626 const char *cp = strchr (res.comp_dir, ':');
10627
10628 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10629 res.comp_dir = cp + 1;
10630 }
10631
10632 if (res.name == NULL)
10633 res.name = "<unknown>";
10634
10635 return res;
10636 }
10637
10638 /* Handle DW_AT_stmt_list for a compilation unit.
10639 DIE is the DW_TAG_compile_unit die for CU.
10640 COMP_DIR is the compilation directory. LOWPC is passed to
10641 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10642
10643 static void
10644 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10645 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10646 {
10647 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10648 struct attribute *attr;
10649 struct line_header line_header_local;
10650 hashval_t line_header_local_hash;
10651 void **slot;
10652 int decode_mapping;
10653
10654 gdb_assert (! cu->per_cu->is_debug_types);
10655
10656 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10657 if (attr == NULL || !attr->form_is_unsigned ())
10658 return;
10659
10660 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10661
10662 /* The line header hash table is only created if needed (it exists to
10663 prevent redundant reading of the line table for partial_units).
10664 If we're given a partial_unit, we'll need it. If we're given a
10665 compile_unit, then use the line header hash table if it's already
10666 created, but don't create one just yet. */
10667
10668 if (per_objfile->line_header_hash == NULL
10669 && die->tag == DW_TAG_partial_unit)
10670 {
10671 per_objfile->line_header_hash
10672 .reset (htab_create_alloc (127, line_header_hash_voidp,
10673 line_header_eq_voidp,
10674 free_line_header_voidp,
10675 xcalloc, xfree));
10676 }
10677
10678 line_header_local.sect_off = line_offset;
10679 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10680 line_header_local_hash = line_header_hash (&line_header_local);
10681 if (per_objfile->line_header_hash != NULL)
10682 {
10683 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10684 &line_header_local,
10685 line_header_local_hash, NO_INSERT);
10686
10687 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10688 is not present in *SLOT (since if there is something in *SLOT then
10689 it will be for a partial_unit). */
10690 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10691 {
10692 gdb_assert (*slot != NULL);
10693 cu->line_header = (struct line_header *) *slot;
10694 return;
10695 }
10696 }
10697
10698 /* dwarf_decode_line_header does not yet provide sufficient information.
10699 We always have to call also dwarf_decode_lines for it. */
10700 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10701 if (lh == NULL)
10702 return;
10703
10704 cu->line_header = lh.release ();
10705 cu->line_header_die_owner = die;
10706
10707 if (per_objfile->line_header_hash == NULL)
10708 slot = NULL;
10709 else
10710 {
10711 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10712 &line_header_local,
10713 line_header_local_hash, INSERT);
10714 gdb_assert (slot != NULL);
10715 }
10716 if (slot != NULL && *slot == NULL)
10717 {
10718 /* This newly decoded line number information unit will be owned
10719 by line_header_hash hash table. */
10720 *slot = cu->line_header;
10721 cu->line_header_die_owner = NULL;
10722 }
10723 else
10724 {
10725 /* We cannot free any current entry in (*slot) as that struct line_header
10726 may be already used by multiple CUs. Create only temporary decoded
10727 line_header for this CU - it may happen at most once for each line
10728 number information unit. And if we're not using line_header_hash
10729 then this is what we want as well. */
10730 gdb_assert (die->tag != DW_TAG_partial_unit);
10731 }
10732 decode_mapping = (die->tag != DW_TAG_partial_unit);
10733 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10734 decode_mapping);
10735
10736 }
10737
10738 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10739
10740 static void
10741 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10742 {
10743 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10744 struct objfile *objfile = per_objfile->objfile;
10745 struct gdbarch *gdbarch = objfile->arch ();
10746 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10747 CORE_ADDR highpc = ((CORE_ADDR) 0);
10748 struct attribute *attr;
10749 struct die_info *child_die;
10750 CORE_ADDR baseaddr;
10751
10752 prepare_one_comp_unit (cu, die, cu->language);
10753 baseaddr = objfile->text_section_offset ();
10754
10755 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10756
10757 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10758 from finish_block. */
10759 if (lowpc == ((CORE_ADDR) -1))
10760 lowpc = highpc;
10761 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10762
10763 file_and_directory fnd = find_file_and_directory (die, cu);
10764
10765 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10766 standardised yet. As a workaround for the language detection we fall
10767 back to the DW_AT_producer string. */
10768 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10769 cu->language = language_opencl;
10770
10771 /* Similar hack for Go. */
10772 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10773 set_cu_language (DW_LANG_Go, cu);
10774
10775 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10776
10777 /* Decode line number information if present. We do this before
10778 processing child DIEs, so that the line header table is available
10779 for DW_AT_decl_file. */
10780 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10781
10782 /* Process all dies in compilation unit. */
10783 if (die->child != NULL)
10784 {
10785 child_die = die->child;
10786 while (child_die && child_die->tag)
10787 {
10788 process_die (child_die, cu);
10789 child_die = child_die->sibling;
10790 }
10791 }
10792
10793 /* Decode macro information, if present. Dwarf 2 macro information
10794 refers to information in the line number info statement program
10795 header, so we can only read it if we've read the header
10796 successfully. */
10797 attr = dwarf2_attr (die, DW_AT_macros, cu);
10798 if (attr == NULL)
10799 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10800 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10801 {
10802 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10803 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10804
10805 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
10806 }
10807 else
10808 {
10809 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10810 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10811 {
10812 unsigned int macro_offset = attr->as_unsigned ();
10813
10814 dwarf_decode_macros (cu, macro_offset, 0);
10815 }
10816 }
10817 }
10818
10819 void
10820 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10821 {
10822 struct type_unit_group *tu_group;
10823 int first_time;
10824 struct attribute *attr;
10825 unsigned int i;
10826 struct signatured_type *sig_type;
10827
10828 gdb_assert (per_cu->is_debug_types);
10829 sig_type = (struct signatured_type *) per_cu;
10830
10831 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10832
10833 /* If we're using .gdb_index (includes -readnow) then
10834 per_cu->type_unit_group may not have been set up yet. */
10835 if (sig_type->type_unit_group == NULL)
10836 sig_type->type_unit_group = get_type_unit_group (this, attr);
10837 tu_group = sig_type->type_unit_group;
10838
10839 /* If we've already processed this stmt_list there's no real need to
10840 do it again, we could fake it and just recreate the part we need
10841 (file name,index -> symtab mapping). If data shows this optimization
10842 is useful we can do it then. */
10843 type_unit_group_unshareable *tug_unshare
10844 = per_objfile->get_type_unit_group_unshareable (tu_group);
10845 first_time = tug_unshare->compunit_symtab == NULL;
10846
10847 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10848 debug info. */
10849 line_header_up lh;
10850 if (attr != NULL && attr->form_is_unsigned ())
10851 {
10852 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10853 lh = dwarf_decode_line_header (line_offset, this);
10854 }
10855 if (lh == NULL)
10856 {
10857 if (first_time)
10858 start_symtab ("", NULL, 0);
10859 else
10860 {
10861 gdb_assert (tug_unshare->symtabs == NULL);
10862 gdb_assert (m_builder == nullptr);
10863 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10864 m_builder.reset (new struct buildsym_compunit
10865 (COMPUNIT_OBJFILE (cust), "",
10866 COMPUNIT_DIRNAME (cust),
10867 compunit_language (cust),
10868 0, cust));
10869 list_in_scope = get_builder ()->get_file_symbols ();
10870 }
10871 return;
10872 }
10873
10874 line_header = lh.release ();
10875 line_header_die_owner = die;
10876
10877 if (first_time)
10878 {
10879 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10880
10881 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10882 still initializing it, and our caller (a few levels up)
10883 process_full_type_unit still needs to know if this is the first
10884 time. */
10885
10886 tug_unshare->symtabs
10887 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10888 struct symtab *, line_header->file_names_size ());
10889
10890 auto &file_names = line_header->file_names ();
10891 for (i = 0; i < file_names.size (); ++i)
10892 {
10893 file_entry &fe = file_names[i];
10894 dwarf2_start_subfile (this, fe.name,
10895 fe.include_dir (line_header));
10896 buildsym_compunit *b = get_builder ();
10897 if (b->get_current_subfile ()->symtab == NULL)
10898 {
10899 /* NOTE: start_subfile will recognize when it's been
10900 passed a file it has already seen. So we can't
10901 assume there's a simple mapping from
10902 cu->line_header->file_names to subfiles, plus
10903 cu->line_header->file_names may contain dups. */
10904 b->get_current_subfile ()->symtab
10905 = allocate_symtab (cust, b->get_current_subfile ()->name);
10906 }
10907
10908 fe.symtab = b->get_current_subfile ()->symtab;
10909 tug_unshare->symtabs[i] = fe.symtab;
10910 }
10911 }
10912 else
10913 {
10914 gdb_assert (m_builder == nullptr);
10915 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10916 m_builder.reset (new struct buildsym_compunit
10917 (COMPUNIT_OBJFILE (cust), "",
10918 COMPUNIT_DIRNAME (cust),
10919 compunit_language (cust),
10920 0, cust));
10921 list_in_scope = get_builder ()->get_file_symbols ();
10922
10923 auto &file_names = line_header->file_names ();
10924 for (i = 0; i < file_names.size (); ++i)
10925 {
10926 file_entry &fe = file_names[i];
10927 fe.symtab = tug_unshare->symtabs[i];
10928 }
10929 }
10930
10931 /* The main symtab is allocated last. Type units don't have DW_AT_name
10932 so they don't have a "real" (so to speak) symtab anyway.
10933 There is later code that will assign the main symtab to all symbols
10934 that don't have one. We need to handle the case of a symbol with a
10935 missing symtab (DW_AT_decl_file) anyway. */
10936 }
10937
10938 /* Process DW_TAG_type_unit.
10939 For TUs we want to skip the first top level sibling if it's not the
10940 actual type being defined by this TU. In this case the first top
10941 level sibling is there to provide context only. */
10942
10943 static void
10944 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10945 {
10946 struct die_info *child_die;
10947
10948 prepare_one_comp_unit (cu, die, language_minimal);
10949
10950 /* Initialize (or reinitialize) the machinery for building symtabs.
10951 We do this before processing child DIEs, so that the line header table
10952 is available for DW_AT_decl_file. */
10953 cu->setup_type_unit_groups (die);
10954
10955 if (die->child != NULL)
10956 {
10957 child_die = die->child;
10958 while (child_die && child_die->tag)
10959 {
10960 process_die (child_die, cu);
10961 child_die = child_die->sibling;
10962 }
10963 }
10964 }
10965 \f
10966 /* DWO/DWP files.
10967
10968 http://gcc.gnu.org/wiki/DebugFission
10969 http://gcc.gnu.org/wiki/DebugFissionDWP
10970
10971 To simplify handling of both DWO files ("object" files with the DWARF info)
10972 and DWP files (a file with the DWOs packaged up into one file), we treat
10973 DWP files as having a collection of virtual DWO files. */
10974
10975 static hashval_t
10976 hash_dwo_file (const void *item)
10977 {
10978 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10979 hashval_t hash;
10980
10981 hash = htab_hash_string (dwo_file->dwo_name);
10982 if (dwo_file->comp_dir != NULL)
10983 hash += htab_hash_string (dwo_file->comp_dir);
10984 return hash;
10985 }
10986
10987 static int
10988 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10989 {
10990 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10991 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10992
10993 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10994 return 0;
10995 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10996 return lhs->comp_dir == rhs->comp_dir;
10997 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10998 }
10999
11000 /* Allocate a hash table for DWO files. */
11001
11002 static htab_up
11003 allocate_dwo_file_hash_table ()
11004 {
11005 auto delete_dwo_file = [] (void *item)
11006 {
11007 struct dwo_file *dwo_file = (struct dwo_file *) item;
11008
11009 delete dwo_file;
11010 };
11011
11012 return htab_up (htab_create_alloc (41,
11013 hash_dwo_file,
11014 eq_dwo_file,
11015 delete_dwo_file,
11016 xcalloc, xfree));
11017 }
11018
11019 /* Lookup DWO file DWO_NAME. */
11020
11021 static void **
11022 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11023 const char *dwo_name,
11024 const char *comp_dir)
11025 {
11026 struct dwo_file find_entry;
11027 void **slot;
11028
11029 if (per_objfile->per_bfd->dwo_files == NULL)
11030 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11031
11032 find_entry.dwo_name = dwo_name;
11033 find_entry.comp_dir = comp_dir;
11034 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11035 INSERT);
11036
11037 return slot;
11038 }
11039
11040 static hashval_t
11041 hash_dwo_unit (const void *item)
11042 {
11043 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11044
11045 /* This drops the top 32 bits of the id, but is ok for a hash. */
11046 return dwo_unit->signature;
11047 }
11048
11049 static int
11050 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11051 {
11052 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11053 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11054
11055 /* The signature is assumed to be unique within the DWO file.
11056 So while object file CU dwo_id's always have the value zero,
11057 that's OK, assuming each object file DWO file has only one CU,
11058 and that's the rule for now. */
11059 return lhs->signature == rhs->signature;
11060 }
11061
11062 /* Allocate a hash table for DWO CUs,TUs.
11063 There is one of these tables for each of CUs,TUs for each DWO file. */
11064
11065 static htab_up
11066 allocate_dwo_unit_table ()
11067 {
11068 /* Start out with a pretty small number.
11069 Generally DWO files contain only one CU and maybe some TUs. */
11070 return htab_up (htab_create_alloc (3,
11071 hash_dwo_unit,
11072 eq_dwo_unit,
11073 NULL, xcalloc, xfree));
11074 }
11075
11076 /* die_reader_func for create_dwo_cu. */
11077
11078 static void
11079 create_dwo_cu_reader (const struct die_reader_specs *reader,
11080 const gdb_byte *info_ptr,
11081 struct die_info *comp_unit_die,
11082 struct dwo_file *dwo_file,
11083 struct dwo_unit *dwo_unit)
11084 {
11085 struct dwarf2_cu *cu = reader->cu;
11086 sect_offset sect_off = cu->per_cu->sect_off;
11087 struct dwarf2_section_info *section = cu->per_cu->section;
11088
11089 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11090 if (!signature.has_value ())
11091 {
11092 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11093 " its dwo_id [in module %s]"),
11094 sect_offset_str (sect_off), dwo_file->dwo_name);
11095 return;
11096 }
11097
11098 dwo_unit->dwo_file = dwo_file;
11099 dwo_unit->signature = *signature;
11100 dwo_unit->section = section;
11101 dwo_unit->sect_off = sect_off;
11102 dwo_unit->length = cu->per_cu->length;
11103
11104 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11105 sect_offset_str (sect_off),
11106 hex_string (dwo_unit->signature));
11107 }
11108
11109 /* Create the dwo_units for the CUs in a DWO_FILE.
11110 Note: This function processes DWO files only, not DWP files. */
11111
11112 static void
11113 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11114 dwarf2_cu *cu, struct dwo_file &dwo_file,
11115 dwarf2_section_info &section, htab_up &cus_htab)
11116 {
11117 struct objfile *objfile = per_objfile->objfile;
11118 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11119 const gdb_byte *info_ptr, *end_ptr;
11120
11121 section.read (objfile);
11122 info_ptr = section.buffer;
11123
11124 if (info_ptr == NULL)
11125 return;
11126
11127 dwarf_read_debug_printf ("Reading %s for %s:",
11128 section.get_name (),
11129 section.get_file_name ());
11130
11131 end_ptr = info_ptr + section.size;
11132 while (info_ptr < end_ptr)
11133 {
11134 struct dwarf2_per_cu_data per_cu;
11135 struct dwo_unit read_unit {};
11136 struct dwo_unit *dwo_unit;
11137 void **slot;
11138 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11139
11140 per_cu.per_bfd = per_bfd;
11141 per_cu.is_debug_types = 0;
11142 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11143 per_cu.section = &section;
11144
11145 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11146 if (!reader.dummy_p)
11147 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11148 &dwo_file, &read_unit);
11149 info_ptr += per_cu.length;
11150
11151 // If the unit could not be parsed, skip it.
11152 if (read_unit.dwo_file == NULL)
11153 continue;
11154
11155 if (cus_htab == NULL)
11156 cus_htab = allocate_dwo_unit_table ();
11157
11158 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11159 struct dwo_unit);
11160 *dwo_unit = read_unit;
11161 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11162 gdb_assert (slot != NULL);
11163 if (*slot != NULL)
11164 {
11165 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11166 sect_offset dup_sect_off = dup_cu->sect_off;
11167
11168 complaint (_("debug cu entry at offset %s is duplicate to"
11169 " the entry at offset %s, signature %s"),
11170 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11171 hex_string (dwo_unit->signature));
11172 }
11173 *slot = (void *)dwo_unit;
11174 }
11175 }
11176
11177 /* DWP file .debug_{cu,tu}_index section format:
11178 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11179 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11180
11181 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11182 officially standard DWP format was published with DWARF v5 and is called
11183 Version 5. There are no versions 3 or 4.
11184
11185 DWP Version 1:
11186
11187 Both index sections have the same format, and serve to map a 64-bit
11188 signature to a set of section numbers. Each section begins with a header,
11189 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11190 indexes, and a pool of 32-bit section numbers. The index sections will be
11191 aligned at 8-byte boundaries in the file.
11192
11193 The index section header consists of:
11194
11195 V, 32 bit version number
11196 -, 32 bits unused
11197 N, 32 bit number of compilation units or type units in the index
11198 M, 32 bit number of slots in the hash table
11199
11200 Numbers are recorded using the byte order of the application binary.
11201
11202 The hash table begins at offset 16 in the section, and consists of an array
11203 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11204 order of the application binary). Unused slots in the hash table are 0.
11205 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11206
11207 The parallel table begins immediately after the hash table
11208 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11209 array of 32-bit indexes (using the byte order of the application binary),
11210 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11211 table contains a 32-bit index into the pool of section numbers. For unused
11212 hash table slots, the corresponding entry in the parallel table will be 0.
11213
11214 The pool of section numbers begins immediately following the hash table
11215 (at offset 16 + 12 * M from the beginning of the section). The pool of
11216 section numbers consists of an array of 32-bit words (using the byte order
11217 of the application binary). Each item in the array is indexed starting
11218 from 0. The hash table entry provides the index of the first section
11219 number in the set. Additional section numbers in the set follow, and the
11220 set is terminated by a 0 entry (section number 0 is not used in ELF).
11221
11222 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11223 section must be the first entry in the set, and the .debug_abbrev.dwo must
11224 be the second entry. Other members of the set may follow in any order.
11225
11226 ---
11227
11228 DWP Versions 2 and 5:
11229
11230 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11231 and the entries in the index tables are now offsets into these sections.
11232 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11233 section.
11234
11235 Index Section Contents:
11236 Header
11237 Hash Table of Signatures dwp_hash_table.hash_table
11238 Parallel Table of Indices dwp_hash_table.unit_table
11239 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11240 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11241
11242 The index section header consists of:
11243
11244 V, 32 bit version number
11245 L, 32 bit number of columns in the table of section offsets
11246 N, 32 bit number of compilation units or type units in the index
11247 M, 32 bit number of slots in the hash table
11248
11249 Numbers are recorded using the byte order of the application binary.
11250
11251 The hash table has the same format as version 1.
11252 The parallel table of indices has the same format as version 1,
11253 except that the entries are origin-1 indices into the table of sections
11254 offsets and the table of section sizes.
11255
11256 The table of offsets begins immediately following the parallel table
11257 (at offset 16 + 12 * M from the beginning of the section). The table is
11258 a two-dimensional array of 32-bit words (using the byte order of the
11259 application binary), with L columns and N+1 rows, in row-major order.
11260 Each row in the array is indexed starting from 0. The first row provides
11261 a key to the remaining rows: each column in this row provides an identifier
11262 for a debug section, and the offsets in the same column of subsequent rows
11263 refer to that section. The section identifiers for Version 2 are:
11264
11265 DW_SECT_INFO 1 .debug_info.dwo
11266 DW_SECT_TYPES 2 .debug_types.dwo
11267 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11268 DW_SECT_LINE 4 .debug_line.dwo
11269 DW_SECT_LOC 5 .debug_loc.dwo
11270 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11271 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11272 DW_SECT_MACRO 8 .debug_macro.dwo
11273
11274 The section identifiers for Version 5 are:
11275
11276 DW_SECT_INFO_V5 1 .debug_info.dwo
11277 DW_SECT_RESERVED_V5 2 --
11278 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11279 DW_SECT_LINE_V5 4 .debug_line.dwo
11280 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11281 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11282 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11283 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11284
11285 The offsets provided by the CU and TU index sections are the base offsets
11286 for the contributions made by each CU or TU to the corresponding section
11287 in the package file. Each CU and TU header contains an abbrev_offset
11288 field, used to find the abbreviations table for that CU or TU within the
11289 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11290 be interpreted as relative to the base offset given in the index section.
11291 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11292 should be interpreted as relative to the base offset for .debug_line.dwo,
11293 and offsets into other debug sections obtained from DWARF attributes should
11294 also be interpreted as relative to the corresponding base offset.
11295
11296 The table of sizes begins immediately following the table of offsets.
11297 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11298 with L columns and N rows, in row-major order. Each row in the array is
11299 indexed starting from 1 (row 0 is shared by the two tables).
11300
11301 ---
11302
11303 Hash table lookup is handled the same in version 1 and 2:
11304
11305 We assume that N and M will not exceed 2^32 - 1.
11306 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11307
11308 Given a 64-bit compilation unit signature or a type signature S, an entry
11309 in the hash table is located as follows:
11310
11311 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11312 the low-order k bits all set to 1.
11313
11314 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11315
11316 3) If the hash table entry at index H matches the signature, use that
11317 entry. If the hash table entry at index H is unused (all zeroes),
11318 terminate the search: the signature is not present in the table.
11319
11320 4) Let H = (H + H') modulo M. Repeat at Step 3.
11321
11322 Because M > N and H' and M are relatively prime, the search is guaranteed
11323 to stop at an unused slot or find the match. */
11324
11325 /* Create a hash table to map DWO IDs to their CU/TU entry in
11326 .debug_{info,types}.dwo in DWP_FILE.
11327 Returns NULL if there isn't one.
11328 Note: This function processes DWP files only, not DWO files. */
11329
11330 static struct dwp_hash_table *
11331 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11332 struct dwp_file *dwp_file, int is_debug_types)
11333 {
11334 struct objfile *objfile = per_objfile->objfile;
11335 bfd *dbfd = dwp_file->dbfd.get ();
11336 const gdb_byte *index_ptr, *index_end;
11337 struct dwarf2_section_info *index;
11338 uint32_t version, nr_columns, nr_units, nr_slots;
11339 struct dwp_hash_table *htab;
11340
11341 if (is_debug_types)
11342 index = &dwp_file->sections.tu_index;
11343 else
11344 index = &dwp_file->sections.cu_index;
11345
11346 if (index->empty ())
11347 return NULL;
11348 index->read (objfile);
11349
11350 index_ptr = index->buffer;
11351 index_end = index_ptr + index->size;
11352
11353 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11354 For now it's safe to just read 4 bytes (particularly as it's difficult to
11355 tell if you're dealing with Version 5 before you've read the version). */
11356 version = read_4_bytes (dbfd, index_ptr);
11357 index_ptr += 4;
11358 if (version == 2 || version == 5)
11359 nr_columns = read_4_bytes (dbfd, index_ptr);
11360 else
11361 nr_columns = 0;
11362 index_ptr += 4;
11363 nr_units = read_4_bytes (dbfd, index_ptr);
11364 index_ptr += 4;
11365 nr_slots = read_4_bytes (dbfd, index_ptr);
11366 index_ptr += 4;
11367
11368 if (version != 1 && version != 2 && version != 5)
11369 {
11370 error (_("Dwarf Error: unsupported DWP file version (%s)"
11371 " [in module %s]"),
11372 pulongest (version), dwp_file->name);
11373 }
11374 if (nr_slots != (nr_slots & -nr_slots))
11375 {
11376 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11377 " is not power of 2 [in module %s]"),
11378 pulongest (nr_slots), dwp_file->name);
11379 }
11380
11381 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11382 htab->version = version;
11383 htab->nr_columns = nr_columns;
11384 htab->nr_units = nr_units;
11385 htab->nr_slots = nr_slots;
11386 htab->hash_table = index_ptr;
11387 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11388
11389 /* Exit early if the table is empty. */
11390 if (nr_slots == 0 || nr_units == 0
11391 || (version == 2 && nr_columns == 0)
11392 || (version == 5 && nr_columns == 0))
11393 {
11394 /* All must be zero. */
11395 if (nr_slots != 0 || nr_units != 0
11396 || (version == 2 && nr_columns != 0)
11397 || (version == 5 && nr_columns != 0))
11398 {
11399 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11400 " all zero [in modules %s]"),
11401 dwp_file->name);
11402 }
11403 return htab;
11404 }
11405
11406 if (version == 1)
11407 {
11408 htab->section_pool.v1.indices =
11409 htab->unit_table + sizeof (uint32_t) * nr_slots;
11410 /* It's harder to decide whether the section is too small in v1.
11411 V1 is deprecated anyway so we punt. */
11412 }
11413 else if (version == 2)
11414 {
11415 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11416 int *ids = htab->section_pool.v2.section_ids;
11417 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11418 /* Reverse map for error checking. */
11419 int ids_seen[DW_SECT_MAX + 1];
11420 int i;
11421
11422 if (nr_columns < 2)
11423 {
11424 error (_("Dwarf Error: bad DWP hash table, too few columns"
11425 " in section table [in module %s]"),
11426 dwp_file->name);
11427 }
11428 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11429 {
11430 error (_("Dwarf Error: bad DWP hash table, too many columns"
11431 " in section table [in module %s]"),
11432 dwp_file->name);
11433 }
11434 memset (ids, 255, sizeof_ids);
11435 memset (ids_seen, 255, sizeof (ids_seen));
11436 for (i = 0; i < nr_columns; ++i)
11437 {
11438 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11439
11440 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11441 {
11442 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11443 " in section table [in module %s]"),
11444 id, dwp_file->name);
11445 }
11446 if (ids_seen[id] != -1)
11447 {
11448 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11449 " id %d in section table [in module %s]"),
11450 id, dwp_file->name);
11451 }
11452 ids_seen[id] = i;
11453 ids[i] = id;
11454 }
11455 /* Must have exactly one info or types section. */
11456 if (((ids_seen[DW_SECT_INFO] != -1)
11457 + (ids_seen[DW_SECT_TYPES] != -1))
11458 != 1)
11459 {
11460 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11461 " DWO info/types section [in module %s]"),
11462 dwp_file->name);
11463 }
11464 /* Must have an abbrev section. */
11465 if (ids_seen[DW_SECT_ABBREV] == -1)
11466 {
11467 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11468 " section [in module %s]"),
11469 dwp_file->name);
11470 }
11471 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11472 htab->section_pool.v2.sizes =
11473 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11474 * nr_units * nr_columns);
11475 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11476 * nr_units * nr_columns))
11477 > index_end)
11478 {
11479 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11480 " [in module %s]"),
11481 dwp_file->name);
11482 }
11483 }
11484 else /* version == 5 */
11485 {
11486 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11487 int *ids = htab->section_pool.v5.section_ids;
11488 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11489 /* Reverse map for error checking. */
11490 int ids_seen[DW_SECT_MAX_V5 + 1];
11491
11492 if (nr_columns < 2)
11493 {
11494 error (_("Dwarf Error: bad DWP hash table, too few columns"
11495 " in section table [in module %s]"),
11496 dwp_file->name);
11497 }
11498 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11499 {
11500 error (_("Dwarf Error: bad DWP hash table, too many columns"
11501 " in section table [in module %s]"),
11502 dwp_file->name);
11503 }
11504 memset (ids, 255, sizeof_ids);
11505 memset (ids_seen, 255, sizeof (ids_seen));
11506 for (int i = 0; i < nr_columns; ++i)
11507 {
11508 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11509
11510 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11511 {
11512 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11513 " in section table [in module %s]"),
11514 id, dwp_file->name);
11515 }
11516 if (ids_seen[id] != -1)
11517 {
11518 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11519 " id %d in section table [in module %s]"),
11520 id, dwp_file->name);
11521 }
11522 ids_seen[id] = i;
11523 ids[i] = id;
11524 }
11525 /* Must have seen an info section. */
11526 if (ids_seen[DW_SECT_INFO_V5] == -1)
11527 {
11528 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11529 " DWO info/types section [in module %s]"),
11530 dwp_file->name);
11531 }
11532 /* Must have an abbrev section. */
11533 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11534 {
11535 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11536 " section [in module %s]"),
11537 dwp_file->name);
11538 }
11539 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11540 htab->section_pool.v5.sizes
11541 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11542 * nr_units * nr_columns);
11543 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11544 * nr_units * nr_columns))
11545 > index_end)
11546 {
11547 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11548 " [in module %s]"),
11549 dwp_file->name);
11550 }
11551 }
11552
11553 return htab;
11554 }
11555
11556 /* Update SECTIONS with the data from SECTP.
11557
11558 This function is like the other "locate" section routines, but in
11559 this context the sections to read comes from the DWP V1 hash table,
11560 not the full ELF section table.
11561
11562 The result is non-zero for success, or zero if an error was found. */
11563
11564 static int
11565 locate_v1_virtual_dwo_sections (asection *sectp,
11566 struct virtual_v1_dwo_sections *sections)
11567 {
11568 const struct dwop_section_names *names = &dwop_section_names;
11569
11570 if (names->abbrev_dwo.matches (sectp->name))
11571 {
11572 /* There can be only one. */
11573 if (sections->abbrev.s.section != NULL)
11574 return 0;
11575 sections->abbrev.s.section = sectp;
11576 sections->abbrev.size = bfd_section_size (sectp);
11577 }
11578 else if (names->info_dwo.matches (sectp->name)
11579 || names->types_dwo.matches (sectp->name))
11580 {
11581 /* There can be only one. */
11582 if (sections->info_or_types.s.section != NULL)
11583 return 0;
11584 sections->info_or_types.s.section = sectp;
11585 sections->info_or_types.size = bfd_section_size (sectp);
11586 }
11587 else if (names->line_dwo.matches (sectp->name))
11588 {
11589 /* There can be only one. */
11590 if (sections->line.s.section != NULL)
11591 return 0;
11592 sections->line.s.section = sectp;
11593 sections->line.size = bfd_section_size (sectp);
11594 }
11595 else if (names->loc_dwo.matches (sectp->name))
11596 {
11597 /* There can be only one. */
11598 if (sections->loc.s.section != NULL)
11599 return 0;
11600 sections->loc.s.section = sectp;
11601 sections->loc.size = bfd_section_size (sectp);
11602 }
11603 else if (names->macinfo_dwo.matches (sectp->name))
11604 {
11605 /* There can be only one. */
11606 if (sections->macinfo.s.section != NULL)
11607 return 0;
11608 sections->macinfo.s.section = sectp;
11609 sections->macinfo.size = bfd_section_size (sectp);
11610 }
11611 else if (names->macro_dwo.matches (sectp->name))
11612 {
11613 /* There can be only one. */
11614 if (sections->macro.s.section != NULL)
11615 return 0;
11616 sections->macro.s.section = sectp;
11617 sections->macro.size = bfd_section_size (sectp);
11618 }
11619 else if (names->str_offsets_dwo.matches (sectp->name))
11620 {
11621 /* There can be only one. */
11622 if (sections->str_offsets.s.section != NULL)
11623 return 0;
11624 sections->str_offsets.s.section = sectp;
11625 sections->str_offsets.size = bfd_section_size (sectp);
11626 }
11627 else
11628 {
11629 /* No other kind of section is valid. */
11630 return 0;
11631 }
11632
11633 return 1;
11634 }
11635
11636 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11637 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11638 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11639 This is for DWP version 1 files. */
11640
11641 static struct dwo_unit *
11642 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
11643 struct dwp_file *dwp_file,
11644 uint32_t unit_index,
11645 const char *comp_dir,
11646 ULONGEST signature, int is_debug_types)
11647 {
11648 const struct dwp_hash_table *dwp_htab =
11649 is_debug_types ? dwp_file->tus : dwp_file->cus;
11650 bfd *dbfd = dwp_file->dbfd.get ();
11651 const char *kind = is_debug_types ? "TU" : "CU";
11652 struct dwo_file *dwo_file;
11653 struct dwo_unit *dwo_unit;
11654 struct virtual_v1_dwo_sections sections;
11655 void **dwo_file_slot;
11656 int i;
11657
11658 gdb_assert (dwp_file->version == 1);
11659
11660 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11661 kind, pulongest (unit_index), hex_string (signature),
11662 dwp_file->name);
11663
11664 /* Fetch the sections of this DWO unit.
11665 Put a limit on the number of sections we look for so that bad data
11666 doesn't cause us to loop forever. */
11667
11668 #define MAX_NR_V1_DWO_SECTIONS \
11669 (1 /* .debug_info or .debug_types */ \
11670 + 1 /* .debug_abbrev */ \
11671 + 1 /* .debug_line */ \
11672 + 1 /* .debug_loc */ \
11673 + 1 /* .debug_str_offsets */ \
11674 + 1 /* .debug_macro or .debug_macinfo */ \
11675 + 1 /* trailing zero */)
11676
11677 memset (&sections, 0, sizeof (sections));
11678
11679 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11680 {
11681 asection *sectp;
11682 uint32_t section_nr =
11683 read_4_bytes (dbfd,
11684 dwp_htab->section_pool.v1.indices
11685 + (unit_index + i) * sizeof (uint32_t));
11686
11687 if (section_nr == 0)
11688 break;
11689 if (section_nr >= dwp_file->num_sections)
11690 {
11691 error (_("Dwarf Error: bad DWP hash table, section number too large"
11692 " [in module %s]"),
11693 dwp_file->name);
11694 }
11695
11696 sectp = dwp_file->elf_sections[section_nr];
11697 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11698 {
11699 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11700 " [in module %s]"),
11701 dwp_file->name);
11702 }
11703 }
11704
11705 if (i < 2
11706 || sections.info_or_types.empty ()
11707 || sections.abbrev.empty ())
11708 {
11709 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11710 " [in module %s]"),
11711 dwp_file->name);
11712 }
11713 if (i == MAX_NR_V1_DWO_SECTIONS)
11714 {
11715 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11716 " [in module %s]"),
11717 dwp_file->name);
11718 }
11719
11720 /* It's easier for the rest of the code if we fake a struct dwo_file and
11721 have dwo_unit "live" in that. At least for now.
11722
11723 The DWP file can be made up of a random collection of CUs and TUs.
11724 However, for each CU + set of TUs that came from the same original DWO
11725 file, we can combine them back into a virtual DWO file to save space
11726 (fewer struct dwo_file objects to allocate). Remember that for really
11727 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11728
11729 std::string virtual_dwo_name =
11730 string_printf ("virtual-dwo/%d-%d-%d-%d",
11731 sections.abbrev.get_id (),
11732 sections.line.get_id (),
11733 sections.loc.get_id (),
11734 sections.str_offsets.get_id ());
11735 /* Can we use an existing virtual DWO file? */
11736 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11737 comp_dir);
11738 /* Create one if necessary. */
11739 if (*dwo_file_slot == NULL)
11740 {
11741 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11742 virtual_dwo_name.c_str ());
11743
11744 dwo_file = new struct dwo_file;
11745 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11746 dwo_file->comp_dir = comp_dir;
11747 dwo_file->sections.abbrev = sections.abbrev;
11748 dwo_file->sections.line = sections.line;
11749 dwo_file->sections.loc = sections.loc;
11750 dwo_file->sections.macinfo = sections.macinfo;
11751 dwo_file->sections.macro = sections.macro;
11752 dwo_file->sections.str_offsets = sections.str_offsets;
11753 /* The "str" section is global to the entire DWP file. */
11754 dwo_file->sections.str = dwp_file->sections.str;
11755 /* The info or types section is assigned below to dwo_unit,
11756 there's no need to record it in dwo_file.
11757 Also, we can't simply record type sections in dwo_file because
11758 we record a pointer into the vector in dwo_unit. As we collect more
11759 types we'll grow the vector and eventually have to reallocate space
11760 for it, invalidating all copies of pointers into the previous
11761 contents. */
11762 *dwo_file_slot = dwo_file;
11763 }
11764 else
11765 {
11766 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11767 virtual_dwo_name.c_str ());
11768
11769 dwo_file = (struct dwo_file *) *dwo_file_slot;
11770 }
11771
11772 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11773 dwo_unit->dwo_file = dwo_file;
11774 dwo_unit->signature = signature;
11775 dwo_unit->section =
11776 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11777 *dwo_unit->section = sections.info_or_types;
11778 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11779
11780 return dwo_unit;
11781 }
11782
11783 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11784 simplify them. Given a pointer to the containing section SECTION, and
11785 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11786 virtual section of just that piece. */
11787
11788 static struct dwarf2_section_info
11789 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11790 struct dwarf2_section_info *section,
11791 bfd_size_type offset, bfd_size_type size)
11792 {
11793 struct dwarf2_section_info result;
11794 asection *sectp;
11795
11796 gdb_assert (section != NULL);
11797 gdb_assert (!section->is_virtual);
11798
11799 memset (&result, 0, sizeof (result));
11800 result.s.containing_section = section;
11801 result.is_virtual = true;
11802
11803 if (size == 0)
11804 return result;
11805
11806 sectp = section->get_bfd_section ();
11807
11808 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11809 bounds of the real section. This is a pretty-rare event, so just
11810 flag an error (easier) instead of a warning and trying to cope. */
11811 if (sectp == NULL
11812 || offset + size > bfd_section_size (sectp))
11813 {
11814 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
11815 " in section %s [in module %s]"),
11816 sectp ? bfd_section_name (sectp) : "<unknown>",
11817 objfile_name (per_objfile->objfile));
11818 }
11819
11820 result.virtual_offset = offset;
11821 result.size = size;
11822 return result;
11823 }
11824
11825 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11826 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11827 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11828 This is for DWP version 2 files. */
11829
11830 static struct dwo_unit *
11831 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
11832 struct dwp_file *dwp_file,
11833 uint32_t unit_index,
11834 const char *comp_dir,
11835 ULONGEST signature, int is_debug_types)
11836 {
11837 const struct dwp_hash_table *dwp_htab =
11838 is_debug_types ? dwp_file->tus : dwp_file->cus;
11839 bfd *dbfd = dwp_file->dbfd.get ();
11840 const char *kind = is_debug_types ? "TU" : "CU";
11841 struct dwo_file *dwo_file;
11842 struct dwo_unit *dwo_unit;
11843 struct virtual_v2_or_v5_dwo_sections sections;
11844 void **dwo_file_slot;
11845 int i;
11846
11847 gdb_assert (dwp_file->version == 2);
11848
11849 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11850 kind, pulongest (unit_index), hex_string (signature),
11851 dwp_file->name);
11852
11853 /* Fetch the section offsets of this DWO unit. */
11854
11855 memset (&sections, 0, sizeof (sections));
11856
11857 for (i = 0; i < dwp_htab->nr_columns; ++i)
11858 {
11859 uint32_t offset = read_4_bytes (dbfd,
11860 dwp_htab->section_pool.v2.offsets
11861 + (((unit_index - 1) * dwp_htab->nr_columns
11862 + i)
11863 * sizeof (uint32_t)));
11864 uint32_t size = read_4_bytes (dbfd,
11865 dwp_htab->section_pool.v2.sizes
11866 + (((unit_index - 1) * dwp_htab->nr_columns
11867 + i)
11868 * sizeof (uint32_t)));
11869
11870 switch (dwp_htab->section_pool.v2.section_ids[i])
11871 {
11872 case DW_SECT_INFO:
11873 case DW_SECT_TYPES:
11874 sections.info_or_types_offset = offset;
11875 sections.info_or_types_size = size;
11876 break;
11877 case DW_SECT_ABBREV:
11878 sections.abbrev_offset = offset;
11879 sections.abbrev_size = size;
11880 break;
11881 case DW_SECT_LINE:
11882 sections.line_offset = offset;
11883 sections.line_size = size;
11884 break;
11885 case DW_SECT_LOC:
11886 sections.loc_offset = offset;
11887 sections.loc_size = size;
11888 break;
11889 case DW_SECT_STR_OFFSETS:
11890 sections.str_offsets_offset = offset;
11891 sections.str_offsets_size = size;
11892 break;
11893 case DW_SECT_MACINFO:
11894 sections.macinfo_offset = offset;
11895 sections.macinfo_size = size;
11896 break;
11897 case DW_SECT_MACRO:
11898 sections.macro_offset = offset;
11899 sections.macro_size = size;
11900 break;
11901 }
11902 }
11903
11904 /* It's easier for the rest of the code if we fake a struct dwo_file and
11905 have dwo_unit "live" in that. At least for now.
11906
11907 The DWP file can be made up of a random collection of CUs and TUs.
11908 However, for each CU + set of TUs that came from the same original DWO
11909 file, we can combine them back into a virtual DWO file to save space
11910 (fewer struct dwo_file objects to allocate). Remember that for really
11911 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11912
11913 std::string virtual_dwo_name =
11914 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11915 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11916 (long) (sections.line_size ? sections.line_offset : 0),
11917 (long) (sections.loc_size ? sections.loc_offset : 0),
11918 (long) (sections.str_offsets_size
11919 ? sections.str_offsets_offset : 0));
11920 /* Can we use an existing virtual DWO file? */
11921 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11922 comp_dir);
11923 /* Create one if necessary. */
11924 if (*dwo_file_slot == NULL)
11925 {
11926 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11927 virtual_dwo_name.c_str ());
11928
11929 dwo_file = new struct dwo_file;
11930 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11931 dwo_file->comp_dir = comp_dir;
11932 dwo_file->sections.abbrev =
11933 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
11934 sections.abbrev_offset,
11935 sections.abbrev_size);
11936 dwo_file->sections.line =
11937 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
11938 sections.line_offset,
11939 sections.line_size);
11940 dwo_file->sections.loc =
11941 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
11942 sections.loc_offset, sections.loc_size);
11943 dwo_file->sections.macinfo =
11944 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
11945 sections.macinfo_offset,
11946 sections.macinfo_size);
11947 dwo_file->sections.macro =
11948 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
11949 sections.macro_offset,
11950 sections.macro_size);
11951 dwo_file->sections.str_offsets =
11952 create_dwp_v2_or_v5_section (per_objfile,
11953 &dwp_file->sections.str_offsets,
11954 sections.str_offsets_offset,
11955 sections.str_offsets_size);
11956 /* The "str" section is global to the entire DWP file. */
11957 dwo_file->sections.str = dwp_file->sections.str;
11958 /* The info or types section is assigned below to dwo_unit,
11959 there's no need to record it in dwo_file.
11960 Also, we can't simply record type sections in dwo_file because
11961 we record a pointer into the vector in dwo_unit. As we collect more
11962 types we'll grow the vector and eventually have to reallocate space
11963 for it, invalidating all copies of pointers into the previous
11964 contents. */
11965 *dwo_file_slot = dwo_file;
11966 }
11967 else
11968 {
11969 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11970 virtual_dwo_name.c_str ());
11971
11972 dwo_file = (struct dwo_file *) *dwo_file_slot;
11973 }
11974
11975 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11976 dwo_unit->dwo_file = dwo_file;
11977 dwo_unit->signature = signature;
11978 dwo_unit->section =
11979 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11980 *dwo_unit->section = create_dwp_v2_or_v5_section
11981 (per_objfile,
11982 is_debug_types
11983 ? &dwp_file->sections.types
11984 : &dwp_file->sections.info,
11985 sections.info_or_types_offset,
11986 sections.info_or_types_size);
11987 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11988
11989 return dwo_unit;
11990 }
11991
11992 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11993 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11994 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11995 This is for DWP version 5 files. */
11996
11997 static struct dwo_unit *
11998 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
11999 struct dwp_file *dwp_file,
12000 uint32_t unit_index,
12001 const char *comp_dir,
12002 ULONGEST signature, int is_debug_types)
12003 {
12004 const struct dwp_hash_table *dwp_htab
12005 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12006 bfd *dbfd = dwp_file->dbfd.get ();
12007 const char *kind = is_debug_types ? "TU" : "CU";
12008 struct dwo_file *dwo_file;
12009 struct dwo_unit *dwo_unit;
12010 struct virtual_v2_or_v5_dwo_sections sections {};
12011 void **dwo_file_slot;
12012
12013 gdb_assert (dwp_file->version == 5);
12014
12015 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12016 kind, pulongest (unit_index), hex_string (signature),
12017 dwp_file->name);
12018
12019 /* Fetch the section offsets of this DWO unit. */
12020
12021 /* memset (&sections, 0, sizeof (sections)); */
12022
12023 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12024 {
12025 uint32_t offset = read_4_bytes (dbfd,
12026 dwp_htab->section_pool.v5.offsets
12027 + (((unit_index - 1)
12028 * dwp_htab->nr_columns
12029 + i)
12030 * sizeof (uint32_t)));
12031 uint32_t size = read_4_bytes (dbfd,
12032 dwp_htab->section_pool.v5.sizes
12033 + (((unit_index - 1) * dwp_htab->nr_columns
12034 + i)
12035 * sizeof (uint32_t)));
12036
12037 switch (dwp_htab->section_pool.v5.section_ids[i])
12038 {
12039 case DW_SECT_ABBREV_V5:
12040 sections.abbrev_offset = offset;
12041 sections.abbrev_size = size;
12042 break;
12043 case DW_SECT_INFO_V5:
12044 sections.info_or_types_offset = offset;
12045 sections.info_or_types_size = size;
12046 break;
12047 case DW_SECT_LINE_V5:
12048 sections.line_offset = offset;
12049 sections.line_size = size;
12050 break;
12051 case DW_SECT_LOCLISTS_V5:
12052 sections.loclists_offset = offset;
12053 sections.loclists_size = size;
12054 break;
12055 case DW_SECT_MACRO_V5:
12056 sections.macro_offset = offset;
12057 sections.macro_size = size;
12058 break;
12059 case DW_SECT_RNGLISTS_V5:
12060 sections.rnglists_offset = offset;
12061 sections.rnglists_size = size;
12062 break;
12063 case DW_SECT_STR_OFFSETS_V5:
12064 sections.str_offsets_offset = offset;
12065 sections.str_offsets_size = size;
12066 break;
12067 case DW_SECT_RESERVED_V5:
12068 default:
12069 break;
12070 }
12071 }
12072
12073 /* It's easier for the rest of the code if we fake a struct dwo_file and
12074 have dwo_unit "live" in that. At least for now.
12075
12076 The DWP file can be made up of a random collection of CUs and TUs.
12077 However, for each CU + set of TUs that came from the same original DWO
12078 file, we can combine them back into a virtual DWO file to save space
12079 (fewer struct dwo_file objects to allocate). Remember that for really
12080 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12081
12082 std::string virtual_dwo_name =
12083 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12084 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12085 (long) (sections.line_size ? sections.line_offset : 0),
12086 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12087 (long) (sections.str_offsets_size
12088 ? sections.str_offsets_offset : 0),
12089 (long) (sections.macro_size ? sections.macro_offset : 0),
12090 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12091 /* Can we use an existing virtual DWO file? */
12092 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12093 virtual_dwo_name.c_str (),
12094 comp_dir);
12095 /* Create one if necessary. */
12096 if (*dwo_file_slot == NULL)
12097 {
12098 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12099 virtual_dwo_name.c_str ());
12100
12101 dwo_file = new struct dwo_file;
12102 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12103 dwo_file->comp_dir = comp_dir;
12104 dwo_file->sections.abbrev =
12105 create_dwp_v2_or_v5_section (per_objfile,
12106 &dwp_file->sections.abbrev,
12107 sections.abbrev_offset,
12108 sections.abbrev_size);
12109 dwo_file->sections.line =
12110 create_dwp_v2_or_v5_section (per_objfile,
12111 &dwp_file->sections.line,
12112 sections.line_offset, sections.line_size);
12113 dwo_file->sections.macro =
12114 create_dwp_v2_or_v5_section (per_objfile,
12115 &dwp_file->sections.macro,
12116 sections.macro_offset,
12117 sections.macro_size);
12118 dwo_file->sections.loclists =
12119 create_dwp_v2_or_v5_section (per_objfile,
12120 &dwp_file->sections.loclists,
12121 sections.loclists_offset,
12122 sections.loclists_size);
12123 dwo_file->sections.rnglists =
12124 create_dwp_v2_or_v5_section (per_objfile,
12125 &dwp_file->sections.rnglists,
12126 sections.rnglists_offset,
12127 sections.rnglists_size);
12128 dwo_file->sections.str_offsets =
12129 create_dwp_v2_or_v5_section (per_objfile,
12130 &dwp_file->sections.str_offsets,
12131 sections.str_offsets_offset,
12132 sections.str_offsets_size);
12133 /* The "str" section is global to the entire DWP file. */
12134 dwo_file->sections.str = dwp_file->sections.str;
12135 /* The info or types section is assigned below to dwo_unit,
12136 there's no need to record it in dwo_file.
12137 Also, we can't simply record type sections in dwo_file because
12138 we record a pointer into the vector in dwo_unit. As we collect more
12139 types we'll grow the vector and eventually have to reallocate space
12140 for it, invalidating all copies of pointers into the previous
12141 contents. */
12142 *dwo_file_slot = dwo_file;
12143 }
12144 else
12145 {
12146 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12147 virtual_dwo_name.c_str ());
12148
12149 dwo_file = (struct dwo_file *) *dwo_file_slot;
12150 }
12151
12152 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12153 dwo_unit->dwo_file = dwo_file;
12154 dwo_unit->signature = signature;
12155 dwo_unit->section
12156 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12157 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12158 &dwp_file->sections.info,
12159 sections.info_or_types_offset,
12160 sections.info_or_types_size);
12161 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12162
12163 return dwo_unit;
12164 }
12165
12166 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12167 Returns NULL if the signature isn't found. */
12168
12169 static struct dwo_unit *
12170 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12171 struct dwp_file *dwp_file, const char *comp_dir,
12172 ULONGEST signature, int is_debug_types)
12173 {
12174 const struct dwp_hash_table *dwp_htab =
12175 is_debug_types ? dwp_file->tus : dwp_file->cus;
12176 bfd *dbfd = dwp_file->dbfd.get ();
12177 uint32_t mask = dwp_htab->nr_slots - 1;
12178 uint32_t hash = signature & mask;
12179 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12180 unsigned int i;
12181 void **slot;
12182 struct dwo_unit find_dwo_cu;
12183
12184 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12185 find_dwo_cu.signature = signature;
12186 slot = htab_find_slot (is_debug_types
12187 ? dwp_file->loaded_tus.get ()
12188 : dwp_file->loaded_cus.get (),
12189 &find_dwo_cu, INSERT);
12190
12191 if (*slot != NULL)
12192 return (struct dwo_unit *) *slot;
12193
12194 /* Use a for loop so that we don't loop forever on bad debug info. */
12195 for (i = 0; i < dwp_htab->nr_slots; ++i)
12196 {
12197 ULONGEST signature_in_table;
12198
12199 signature_in_table =
12200 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12201 if (signature_in_table == signature)
12202 {
12203 uint32_t unit_index =
12204 read_4_bytes (dbfd,
12205 dwp_htab->unit_table + hash * sizeof (uint32_t));
12206
12207 if (dwp_file->version == 1)
12208 {
12209 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12210 unit_index, comp_dir,
12211 signature, is_debug_types);
12212 }
12213 else if (dwp_file->version == 2)
12214 {
12215 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12216 unit_index, comp_dir,
12217 signature, is_debug_types);
12218 }
12219 else /* version == 5 */
12220 {
12221 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12222 unit_index, comp_dir,
12223 signature, is_debug_types);
12224 }
12225 return (struct dwo_unit *) *slot;
12226 }
12227 if (signature_in_table == 0)
12228 return NULL;
12229 hash = (hash + hash2) & mask;
12230 }
12231
12232 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12233 " [in module %s]"),
12234 dwp_file->name);
12235 }
12236
12237 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12238 Open the file specified by FILE_NAME and hand it off to BFD for
12239 preliminary analysis. Return a newly initialized bfd *, which
12240 includes a canonicalized copy of FILE_NAME.
12241 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12242 SEARCH_CWD is true if the current directory is to be searched.
12243 It will be searched before debug-file-directory.
12244 If successful, the file is added to the bfd include table of the
12245 objfile's bfd (see gdb_bfd_record_inclusion).
12246 If unable to find/open the file, return NULL.
12247 NOTE: This function is derived from symfile_bfd_open. */
12248
12249 static gdb_bfd_ref_ptr
12250 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12251 const char *file_name, int is_dwp, int search_cwd)
12252 {
12253 int desc;
12254 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12255 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12256 to debug_file_directory. */
12257 const char *search_path;
12258 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12259
12260 gdb::unique_xmalloc_ptr<char> search_path_holder;
12261 if (search_cwd)
12262 {
12263 if (*debug_file_directory != '\0')
12264 {
12265 search_path_holder.reset (concat (".", dirname_separator_string,
12266 debug_file_directory,
12267 (char *) NULL));
12268 search_path = search_path_holder.get ();
12269 }
12270 else
12271 search_path = ".";
12272 }
12273 else
12274 search_path = debug_file_directory;
12275
12276 /* Add the path for the executable binary to the list of search paths. */
12277 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12278 search_path_holder.reset (concat (objfile_dir.c_str (),
12279 dirname_separator_string,
12280 search_path, nullptr));
12281 search_path = search_path_holder.get ();
12282
12283 openp_flags flags = OPF_RETURN_REALPATH;
12284 if (is_dwp)
12285 flags |= OPF_SEARCH_IN_PATH;
12286
12287 gdb::unique_xmalloc_ptr<char> absolute_name;
12288 desc = openp (search_path, flags, file_name,
12289 O_RDONLY | O_BINARY, &absolute_name);
12290 if (desc < 0)
12291 return NULL;
12292
12293 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12294 gnutarget, desc));
12295 if (sym_bfd == NULL)
12296 return NULL;
12297 bfd_set_cacheable (sym_bfd.get (), 1);
12298
12299 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12300 return NULL;
12301
12302 /* Success. Record the bfd as having been included by the objfile's bfd.
12303 This is important because things like demangled_names_hash lives in the
12304 objfile's per_bfd space and may have references to things like symbol
12305 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12306 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12307
12308 return sym_bfd;
12309 }
12310
12311 /* Try to open DWO file FILE_NAME.
12312 COMP_DIR is the DW_AT_comp_dir attribute.
12313 The result is the bfd handle of the file.
12314 If there is a problem finding or opening the file, return NULL.
12315 Upon success, the canonicalized path of the file is stored in the bfd,
12316 same as symfile_bfd_open. */
12317
12318 static gdb_bfd_ref_ptr
12319 open_dwo_file (dwarf2_per_objfile *per_objfile,
12320 const char *file_name, const char *comp_dir)
12321 {
12322 if (IS_ABSOLUTE_PATH (file_name))
12323 return try_open_dwop_file (per_objfile, file_name,
12324 0 /*is_dwp*/, 0 /*search_cwd*/);
12325
12326 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12327
12328 if (comp_dir != NULL)
12329 {
12330 gdb::unique_xmalloc_ptr<char> path_to_try
12331 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12332
12333 /* NOTE: If comp_dir is a relative path, this will also try the
12334 search path, which seems useful. */
12335 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12336 0 /*is_dwp*/,
12337 1 /*search_cwd*/));
12338 if (abfd != NULL)
12339 return abfd;
12340 }
12341
12342 /* That didn't work, try debug-file-directory, which, despite its name,
12343 is a list of paths. */
12344
12345 if (*debug_file_directory == '\0')
12346 return NULL;
12347
12348 return try_open_dwop_file (per_objfile, file_name,
12349 0 /*is_dwp*/, 1 /*search_cwd*/);
12350 }
12351
12352 /* This function is mapped across the sections and remembers the offset and
12353 size of each of the DWO debugging sections we are interested in. */
12354
12355 static void
12356 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12357 dwo_sections *dwo_sections)
12358 {
12359 const struct dwop_section_names *names = &dwop_section_names;
12360
12361 if (names->abbrev_dwo.matches (sectp->name))
12362 {
12363 dwo_sections->abbrev.s.section = sectp;
12364 dwo_sections->abbrev.size = bfd_section_size (sectp);
12365 }
12366 else if (names->info_dwo.matches (sectp->name))
12367 {
12368 dwo_sections->info.s.section = sectp;
12369 dwo_sections->info.size = bfd_section_size (sectp);
12370 }
12371 else if (names->line_dwo.matches (sectp->name))
12372 {
12373 dwo_sections->line.s.section = sectp;
12374 dwo_sections->line.size = bfd_section_size (sectp);
12375 }
12376 else if (names->loc_dwo.matches (sectp->name))
12377 {
12378 dwo_sections->loc.s.section = sectp;
12379 dwo_sections->loc.size = bfd_section_size (sectp);
12380 }
12381 else if (names->loclists_dwo.matches (sectp->name))
12382 {
12383 dwo_sections->loclists.s.section = sectp;
12384 dwo_sections->loclists.size = bfd_section_size (sectp);
12385 }
12386 else if (names->macinfo_dwo.matches (sectp->name))
12387 {
12388 dwo_sections->macinfo.s.section = sectp;
12389 dwo_sections->macinfo.size = bfd_section_size (sectp);
12390 }
12391 else if (names->macro_dwo.matches (sectp->name))
12392 {
12393 dwo_sections->macro.s.section = sectp;
12394 dwo_sections->macro.size = bfd_section_size (sectp);
12395 }
12396 else if (names->rnglists_dwo.matches (sectp->name))
12397 {
12398 dwo_sections->rnglists.s.section = sectp;
12399 dwo_sections->rnglists.size = bfd_section_size (sectp);
12400 }
12401 else if (names->str_dwo.matches (sectp->name))
12402 {
12403 dwo_sections->str.s.section = sectp;
12404 dwo_sections->str.size = bfd_section_size (sectp);
12405 }
12406 else if (names->str_offsets_dwo.matches (sectp->name))
12407 {
12408 dwo_sections->str_offsets.s.section = sectp;
12409 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12410 }
12411 else if (names->types_dwo.matches (sectp->name))
12412 {
12413 struct dwarf2_section_info type_section;
12414
12415 memset (&type_section, 0, sizeof (type_section));
12416 type_section.s.section = sectp;
12417 type_section.size = bfd_section_size (sectp);
12418 dwo_sections->types.push_back (type_section);
12419 }
12420 }
12421
12422 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12423 by PER_CU. This is for the non-DWP case.
12424 The result is NULL if DWO_NAME can't be found. */
12425
12426 static struct dwo_file *
12427 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12428 const char *comp_dir)
12429 {
12430 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12431
12432 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12433 if (dbfd == NULL)
12434 {
12435 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12436
12437 return NULL;
12438 }
12439
12440 dwo_file_up dwo_file (new struct dwo_file);
12441 dwo_file->dwo_name = dwo_name;
12442 dwo_file->comp_dir = comp_dir;
12443 dwo_file->dbfd = std::move (dbfd);
12444
12445 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12446 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12447 &dwo_file->sections);
12448
12449 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12450 dwo_file->cus);
12451
12452 if (cu->per_cu->dwarf_version < 5)
12453 {
12454 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12455 dwo_file->sections.types, dwo_file->tus);
12456 }
12457 else
12458 {
12459 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12460 &dwo_file->sections.info, dwo_file->tus,
12461 rcuh_kind::COMPILE);
12462 }
12463
12464 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12465
12466 return dwo_file.release ();
12467 }
12468
12469 /* This function is mapped across the sections and remembers the offset and
12470 size of each of the DWP debugging sections common to version 1 and 2 that
12471 we are interested in. */
12472
12473 static void
12474 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12475 dwp_file *dwp_file)
12476 {
12477 const struct dwop_section_names *names = &dwop_section_names;
12478 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12479
12480 /* Record the ELF section number for later lookup: this is what the
12481 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12482 gdb_assert (elf_section_nr < dwp_file->num_sections);
12483 dwp_file->elf_sections[elf_section_nr] = sectp;
12484
12485 /* Look for specific sections that we need. */
12486 if (names->str_dwo.matches (sectp->name))
12487 {
12488 dwp_file->sections.str.s.section = sectp;
12489 dwp_file->sections.str.size = bfd_section_size (sectp);
12490 }
12491 else if (names->cu_index.matches (sectp->name))
12492 {
12493 dwp_file->sections.cu_index.s.section = sectp;
12494 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12495 }
12496 else if (names->tu_index.matches (sectp->name))
12497 {
12498 dwp_file->sections.tu_index.s.section = sectp;
12499 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12500 }
12501 }
12502
12503 /* This function is mapped across the sections and remembers the offset and
12504 size of each of the DWP version 2 debugging sections that we are interested
12505 in. This is split into a separate function because we don't know if we
12506 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12507
12508 static void
12509 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12510 {
12511 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12512 const struct dwop_section_names *names = &dwop_section_names;
12513 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12514
12515 /* Record the ELF section number for later lookup: this is what the
12516 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12517 gdb_assert (elf_section_nr < dwp_file->num_sections);
12518 dwp_file->elf_sections[elf_section_nr] = sectp;
12519
12520 /* Look for specific sections that we need. */
12521 if (names->abbrev_dwo.matches (sectp->name))
12522 {
12523 dwp_file->sections.abbrev.s.section = sectp;
12524 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12525 }
12526 else if (names->info_dwo.matches (sectp->name))
12527 {
12528 dwp_file->sections.info.s.section = sectp;
12529 dwp_file->sections.info.size = bfd_section_size (sectp);
12530 }
12531 else if (names->line_dwo.matches (sectp->name))
12532 {
12533 dwp_file->sections.line.s.section = sectp;
12534 dwp_file->sections.line.size = bfd_section_size (sectp);
12535 }
12536 else if (names->loc_dwo.matches (sectp->name))
12537 {
12538 dwp_file->sections.loc.s.section = sectp;
12539 dwp_file->sections.loc.size = bfd_section_size (sectp);
12540 }
12541 else if (names->macinfo_dwo.matches (sectp->name))
12542 {
12543 dwp_file->sections.macinfo.s.section = sectp;
12544 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12545 }
12546 else if (names->macro_dwo.matches (sectp->name))
12547 {
12548 dwp_file->sections.macro.s.section = sectp;
12549 dwp_file->sections.macro.size = bfd_section_size (sectp);
12550 }
12551 else if (names->str_offsets_dwo.matches (sectp->name))
12552 {
12553 dwp_file->sections.str_offsets.s.section = sectp;
12554 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12555 }
12556 else if (names->types_dwo.matches (sectp->name))
12557 {
12558 dwp_file->sections.types.s.section = sectp;
12559 dwp_file->sections.types.size = bfd_section_size (sectp);
12560 }
12561 }
12562
12563 /* This function is mapped across the sections and remembers the offset and
12564 size of each of the DWP version 5 debugging sections that we are interested
12565 in. This is split into a separate function because we don't know if we
12566 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12567
12568 static void
12569 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12570 {
12571 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12572 const struct dwop_section_names *names = &dwop_section_names;
12573 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12574
12575 /* Record the ELF section number for later lookup: this is what the
12576 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12577 gdb_assert (elf_section_nr < dwp_file->num_sections);
12578 dwp_file->elf_sections[elf_section_nr] = sectp;
12579
12580 /* Look for specific sections that we need. */
12581 if (names->abbrev_dwo.matches (sectp->name))
12582 {
12583 dwp_file->sections.abbrev.s.section = sectp;
12584 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12585 }
12586 else if (names->info_dwo.matches (sectp->name))
12587 {
12588 dwp_file->sections.info.s.section = sectp;
12589 dwp_file->sections.info.size = bfd_section_size (sectp);
12590 }
12591 else if (names->line_dwo.matches (sectp->name))
12592 {
12593 dwp_file->sections.line.s.section = sectp;
12594 dwp_file->sections.line.size = bfd_section_size (sectp);
12595 }
12596 else if (names->loclists_dwo.matches (sectp->name))
12597 {
12598 dwp_file->sections.loclists.s.section = sectp;
12599 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12600 }
12601 else if (names->macro_dwo.matches (sectp->name))
12602 {
12603 dwp_file->sections.macro.s.section = sectp;
12604 dwp_file->sections.macro.size = bfd_section_size (sectp);
12605 }
12606 else if (names->rnglists_dwo.matches (sectp->name))
12607 {
12608 dwp_file->sections.rnglists.s.section = sectp;
12609 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12610 }
12611 else if (names->str_offsets_dwo.matches (sectp->name))
12612 {
12613 dwp_file->sections.str_offsets.s.section = sectp;
12614 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12615 }
12616 }
12617
12618 /* Hash function for dwp_file loaded CUs/TUs. */
12619
12620 static hashval_t
12621 hash_dwp_loaded_cutus (const void *item)
12622 {
12623 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12624
12625 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12626 return dwo_unit->signature;
12627 }
12628
12629 /* Equality function for dwp_file loaded CUs/TUs. */
12630
12631 static int
12632 eq_dwp_loaded_cutus (const void *a, const void *b)
12633 {
12634 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12635 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12636
12637 return dua->signature == dub->signature;
12638 }
12639
12640 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12641
12642 static htab_up
12643 allocate_dwp_loaded_cutus_table ()
12644 {
12645 return htab_up (htab_create_alloc (3,
12646 hash_dwp_loaded_cutus,
12647 eq_dwp_loaded_cutus,
12648 NULL, xcalloc, xfree));
12649 }
12650
12651 /* Try to open DWP file FILE_NAME.
12652 The result is the bfd handle of the file.
12653 If there is a problem finding or opening the file, return NULL.
12654 Upon success, the canonicalized path of the file is stored in the bfd,
12655 same as symfile_bfd_open. */
12656
12657 static gdb_bfd_ref_ptr
12658 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
12659 {
12660 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
12661 1 /*is_dwp*/,
12662 1 /*search_cwd*/));
12663 if (abfd != NULL)
12664 return abfd;
12665
12666 /* Work around upstream bug 15652.
12667 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12668 [Whether that's a "bug" is debatable, but it is getting in our way.]
12669 We have no real idea where the dwp file is, because gdb's realpath-ing
12670 of the executable's path may have discarded the needed info.
12671 [IWBN if the dwp file name was recorded in the executable, akin to
12672 .gnu_debuglink, but that doesn't exist yet.]
12673 Strip the directory from FILE_NAME and search again. */
12674 if (*debug_file_directory != '\0')
12675 {
12676 /* Don't implicitly search the current directory here.
12677 If the user wants to search "." to handle this case,
12678 it must be added to debug-file-directory. */
12679 return try_open_dwop_file (per_objfile, lbasename (file_name),
12680 1 /*is_dwp*/,
12681 0 /*search_cwd*/);
12682 }
12683
12684 return NULL;
12685 }
12686
12687 /* Initialize the use of the DWP file for the current objfile.
12688 By convention the name of the DWP file is ${objfile}.dwp.
12689 The result is NULL if it can't be found. */
12690
12691 static std::unique_ptr<struct dwp_file>
12692 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
12693 {
12694 struct objfile *objfile = per_objfile->objfile;
12695
12696 /* Try to find first .dwp for the binary file before any symbolic links
12697 resolving. */
12698
12699 /* If the objfile is a debug file, find the name of the real binary
12700 file and get the name of dwp file from there. */
12701 std::string dwp_name;
12702 if (objfile->separate_debug_objfile_backlink != NULL)
12703 {
12704 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12705 const char *backlink_basename = lbasename (backlink->original_name);
12706
12707 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12708 }
12709 else
12710 dwp_name = objfile->original_name;
12711
12712 dwp_name += ".dwp";
12713
12714 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
12715 if (dbfd == NULL
12716 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12717 {
12718 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12719 dwp_name = objfile_name (objfile);
12720 dwp_name += ".dwp";
12721 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
12722 }
12723
12724 if (dbfd == NULL)
12725 {
12726 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12727
12728 return std::unique_ptr<dwp_file> ();
12729 }
12730
12731 const char *name = bfd_get_filename (dbfd.get ());
12732 std::unique_ptr<struct dwp_file> dwp_file
12733 (new struct dwp_file (name, std::move (dbfd)));
12734
12735 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12736 dwp_file->elf_sections =
12737 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
12738 dwp_file->num_sections, asection *);
12739
12740 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12741 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12742 dwp_file.get ());
12743
12744 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
12745
12746 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
12747
12748 /* The DWP file version is stored in the hash table. Oh well. */
12749 if (dwp_file->cus && dwp_file->tus
12750 && dwp_file->cus->version != dwp_file->tus->version)
12751 {
12752 /* Technically speaking, we should try to limp along, but this is
12753 pretty bizarre. We use pulongest here because that's the established
12754 portability solution (e.g, we cannot use %u for uint32_t). */
12755 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12756 " TU version %s [in DWP file %s]"),
12757 pulongest (dwp_file->cus->version),
12758 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12759 }
12760
12761 if (dwp_file->cus)
12762 dwp_file->version = dwp_file->cus->version;
12763 else if (dwp_file->tus)
12764 dwp_file->version = dwp_file->tus->version;
12765 else
12766 dwp_file->version = 2;
12767
12768 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12769 {
12770 if (dwp_file->version == 2)
12771 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12772 dwp_file.get ());
12773 else
12774 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12775 dwp_file.get ());
12776 }
12777
12778 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12779 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12780
12781 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12782 dwarf_read_debug_printf (" %s CUs, %s TUs",
12783 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12784 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12785
12786 return dwp_file;
12787 }
12788
12789 /* Wrapper around open_and_init_dwp_file, only open it once. */
12790
12791 static struct dwp_file *
12792 get_dwp_file (dwarf2_per_objfile *per_objfile)
12793 {
12794 if (!per_objfile->per_bfd->dwp_checked)
12795 {
12796 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12797 per_objfile->per_bfd->dwp_checked = 1;
12798 }
12799 return per_objfile->per_bfd->dwp_file.get ();
12800 }
12801
12802 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12803 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12804 or in the DWP file for the objfile, referenced by THIS_UNIT.
12805 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12806 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12807
12808 This is called, for example, when wanting to read a variable with a
12809 complex location. Therefore we don't want to do file i/o for every call.
12810 Therefore we don't want to look for a DWO file on every call.
12811 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12812 then we check if we've already seen DWO_NAME, and only THEN do we check
12813 for a DWO file.
12814
12815 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12816 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12817
12818 static struct dwo_unit *
12819 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12820 ULONGEST signature, int is_debug_types)
12821 {
12822 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12823 struct objfile *objfile = per_objfile->objfile;
12824 const char *kind = is_debug_types ? "TU" : "CU";
12825 void **dwo_file_slot;
12826 struct dwo_file *dwo_file;
12827 struct dwp_file *dwp_file;
12828
12829 /* First see if there's a DWP file.
12830 If we have a DWP file but didn't find the DWO inside it, don't
12831 look for the original DWO file. It makes gdb behave differently
12832 depending on whether one is debugging in the build tree. */
12833
12834 dwp_file = get_dwp_file (per_objfile);
12835 if (dwp_file != NULL)
12836 {
12837 const struct dwp_hash_table *dwp_htab =
12838 is_debug_types ? dwp_file->tus : dwp_file->cus;
12839
12840 if (dwp_htab != NULL)
12841 {
12842 struct dwo_unit *dwo_cutu =
12843 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12844 is_debug_types);
12845
12846 if (dwo_cutu != NULL)
12847 {
12848 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12849 kind, hex_string (signature),
12850 host_address_to_string (dwo_cutu));
12851
12852 return dwo_cutu;
12853 }
12854 }
12855 }
12856 else
12857 {
12858 /* No DWP file, look for the DWO file. */
12859
12860 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
12861 if (*dwo_file_slot == NULL)
12862 {
12863 /* Read in the file and build a table of the CUs/TUs it contains. */
12864 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12865 }
12866 /* NOTE: This will be NULL if unable to open the file. */
12867 dwo_file = (struct dwo_file *) *dwo_file_slot;
12868
12869 if (dwo_file != NULL)
12870 {
12871 struct dwo_unit *dwo_cutu = NULL;
12872
12873 if (is_debug_types && dwo_file->tus)
12874 {
12875 struct dwo_unit find_dwo_cutu;
12876
12877 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12878 find_dwo_cutu.signature = signature;
12879 dwo_cutu
12880 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12881 &find_dwo_cutu);
12882 }
12883 else if (!is_debug_types && dwo_file->cus)
12884 {
12885 struct dwo_unit find_dwo_cutu;
12886
12887 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12888 find_dwo_cutu.signature = signature;
12889 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12890 &find_dwo_cutu);
12891 }
12892
12893 if (dwo_cutu != NULL)
12894 {
12895 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
12896 kind, dwo_name, hex_string (signature),
12897 host_address_to_string (dwo_cutu));
12898
12899 return dwo_cutu;
12900 }
12901 }
12902 }
12903
12904 /* We didn't find it. This could mean a dwo_id mismatch, or
12905 someone deleted the DWO/DWP file, or the search path isn't set up
12906 correctly to find the file. */
12907
12908 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
12909 kind, dwo_name, hex_string (signature));
12910
12911 /* This is a warning and not a complaint because it can be caused by
12912 pilot error (e.g., user accidentally deleting the DWO). */
12913 {
12914 /* Print the name of the DWP file if we looked there, helps the user
12915 better diagnose the problem. */
12916 std::string dwp_text;
12917
12918 if (dwp_file != NULL)
12919 dwp_text = string_printf (" [in DWP file %s]",
12920 lbasename (dwp_file->name));
12921
12922 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12923 " [in module %s]"),
12924 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12925 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12926 }
12927 return NULL;
12928 }
12929
12930 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12931 See lookup_dwo_cutu_unit for details. */
12932
12933 static struct dwo_unit *
12934 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12935 ULONGEST signature)
12936 {
12937 gdb_assert (!cu->per_cu->is_debug_types);
12938
12939 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12940 }
12941
12942 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12943 See lookup_dwo_cutu_unit for details. */
12944
12945 static struct dwo_unit *
12946 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12947 {
12948 gdb_assert (cu->per_cu->is_debug_types);
12949
12950 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12951
12952 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12953 }
12954
12955 /* Traversal function for queue_and_load_all_dwo_tus. */
12956
12957 static int
12958 queue_and_load_dwo_tu (void **slot, void *info)
12959 {
12960 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12961 dwarf2_cu *cu = (dwarf2_cu *) info;
12962 ULONGEST signature = dwo_unit->signature;
12963 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12964
12965 if (sig_type != NULL)
12966 {
12967 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12968 a real dependency of PER_CU on SIG_TYPE. That is detected later
12969 while processing PER_CU. */
12970 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
12971 cu->language))
12972 load_full_type_unit (sig_type, cu->per_objfile);
12973 cu->per_cu->imported_symtabs_push (sig_type);
12974 }
12975
12976 return 1;
12977 }
12978
12979 /* Queue all TUs contained in the DWO of CU to be read in.
12980 The DWO may have the only definition of the type, though it may not be
12981 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12982 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12983
12984 static void
12985 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12986 {
12987 struct dwo_unit *dwo_unit;
12988 struct dwo_file *dwo_file;
12989
12990 gdb_assert (cu != nullptr);
12991 gdb_assert (!cu->per_cu->is_debug_types);
12992 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12993
12994 dwo_unit = cu->dwo_unit;
12995 gdb_assert (dwo_unit != NULL);
12996
12997 dwo_file = dwo_unit->dwo_file;
12998 if (dwo_file->tus != NULL)
12999 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
13000 }
13001
13002 /* Read in various DIEs. */
13003
13004 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13005 Inherit only the children of the DW_AT_abstract_origin DIE not being
13006 already referenced by DW_AT_abstract_origin from the children of the
13007 current DIE. */
13008
13009 static void
13010 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13011 {
13012 struct die_info *child_die;
13013 sect_offset *offsetp;
13014 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13015 struct die_info *origin_die;
13016 /* Iterator of the ORIGIN_DIE children. */
13017 struct die_info *origin_child_die;
13018 struct attribute *attr;
13019 struct dwarf2_cu *origin_cu;
13020 struct pending **origin_previous_list_in_scope;
13021
13022 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13023 if (!attr)
13024 return;
13025
13026 /* Note that following die references may follow to a die in a
13027 different cu. */
13028
13029 origin_cu = cu;
13030 origin_die = follow_die_ref (die, attr, &origin_cu);
13031
13032 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13033 symbols in. */
13034 origin_previous_list_in_scope = origin_cu->list_in_scope;
13035 origin_cu->list_in_scope = cu->list_in_scope;
13036
13037 if (die->tag != origin_die->tag
13038 && !(die->tag == DW_TAG_inlined_subroutine
13039 && origin_die->tag == DW_TAG_subprogram))
13040 complaint (_("DIE %s and its abstract origin %s have different tags"),
13041 sect_offset_str (die->sect_off),
13042 sect_offset_str (origin_die->sect_off));
13043
13044 /* Find if the concrete and abstract trees are structurally the
13045 same. This is a shallow traversal and it is not bullet-proof;
13046 the compiler can trick the debugger into believing that the trees
13047 are isomorphic, whereas they actually are not. However, the
13048 likelyhood of this happening is pretty low, and a full-fledged
13049 check would be an overkill. */
13050 bool are_isomorphic = true;
13051 die_info *concrete_child = die->child;
13052 die_info *abstract_child = origin_die->child;
13053 while (concrete_child != nullptr || abstract_child != nullptr)
13054 {
13055 if (concrete_child == nullptr
13056 || abstract_child == nullptr
13057 || concrete_child->tag != abstract_child->tag)
13058 {
13059 are_isomorphic = false;
13060 break;
13061 }
13062
13063 concrete_child = concrete_child->sibling;
13064 abstract_child = abstract_child->sibling;
13065 }
13066
13067 /* Walk the origin's children in parallel to the concrete children.
13068 This helps match an origin child in case the debug info misses
13069 DW_AT_abstract_origin attributes. Keep in mind that the abstract
13070 origin tree may not have the same tree structure as the concrete
13071 DIE, though. */
13072 die_info *corresponding_abstract_child
13073 = are_isomorphic ? origin_die->child : nullptr;
13074
13075 std::vector<sect_offset> offsets;
13076
13077 for (child_die = die->child;
13078 child_die && child_die->tag;
13079 child_die = child_die->sibling)
13080 {
13081 struct die_info *child_origin_die;
13082 struct dwarf2_cu *child_origin_cu;
13083
13084 /* We are trying to process concrete instance entries:
13085 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13086 it's not relevant to our analysis here. i.e. detecting DIEs that are
13087 present in the abstract instance but not referenced in the concrete
13088 one. */
13089 if (child_die->tag == DW_TAG_call_site
13090 || child_die->tag == DW_TAG_GNU_call_site)
13091 {
13092 if (are_isomorphic)
13093 corresponding_abstract_child
13094 = corresponding_abstract_child->sibling;
13095 continue;
13096 }
13097
13098 /* For each CHILD_DIE, find the corresponding child of
13099 ORIGIN_DIE. If there is more than one layer of
13100 DW_AT_abstract_origin, follow them all; there shouldn't be,
13101 but GCC versions at least through 4.4 generate this (GCC PR
13102 40573). */
13103 child_origin_die = child_die;
13104 child_origin_cu = cu;
13105 while (1)
13106 {
13107 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13108 child_origin_cu);
13109 if (attr == NULL)
13110 break;
13111 child_origin_die = follow_die_ref (child_origin_die, attr,
13112 &child_origin_cu);
13113 }
13114
13115 /* If missing DW_AT_abstract_origin, try the corresponding child
13116 of the origin. Clang emits such lexical scopes. */
13117 if (child_origin_die == child_die
13118 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
13119 && are_isomorphic
13120 && child_die->tag == DW_TAG_lexical_block)
13121 child_origin_die = corresponding_abstract_child;
13122
13123 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13124 counterpart may exist. */
13125 if (child_origin_die != child_die)
13126 {
13127 if (child_die->tag != child_origin_die->tag
13128 && !(child_die->tag == DW_TAG_inlined_subroutine
13129 && child_origin_die->tag == DW_TAG_subprogram))
13130 complaint (_("Child DIE %s and its abstract origin %s have "
13131 "different tags"),
13132 sect_offset_str (child_die->sect_off),
13133 sect_offset_str (child_origin_die->sect_off));
13134 if (child_origin_die->parent != origin_die)
13135 complaint (_("Child DIE %s and its abstract origin %s have "
13136 "different parents"),
13137 sect_offset_str (child_die->sect_off),
13138 sect_offset_str (child_origin_die->sect_off));
13139 else
13140 offsets.push_back (child_origin_die->sect_off);
13141 }
13142
13143 if (are_isomorphic)
13144 corresponding_abstract_child = corresponding_abstract_child->sibling;
13145 }
13146 std::sort (offsets.begin (), offsets.end ());
13147 sect_offset *offsets_end = offsets.data () + offsets.size ();
13148 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13149 if (offsetp[-1] == *offsetp)
13150 complaint (_("Multiple children of DIE %s refer "
13151 "to DIE %s as their abstract origin"),
13152 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13153
13154 offsetp = offsets.data ();
13155 origin_child_die = origin_die->child;
13156 while (origin_child_die && origin_child_die->tag)
13157 {
13158 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13159 while (offsetp < offsets_end
13160 && *offsetp < origin_child_die->sect_off)
13161 offsetp++;
13162 if (offsetp >= offsets_end
13163 || *offsetp > origin_child_die->sect_off)
13164 {
13165 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13166 Check whether we're already processing ORIGIN_CHILD_DIE.
13167 This can happen with mutually referenced abstract_origins.
13168 PR 16581. */
13169 if (!origin_child_die->in_process)
13170 process_die (origin_child_die, origin_cu);
13171 }
13172 origin_child_die = origin_child_die->sibling;
13173 }
13174 origin_cu->list_in_scope = origin_previous_list_in_scope;
13175
13176 if (cu != origin_cu)
13177 compute_delayed_physnames (origin_cu);
13178 }
13179
13180 static void
13181 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13182 {
13183 struct objfile *objfile = cu->per_objfile->objfile;
13184 struct gdbarch *gdbarch = objfile->arch ();
13185 struct context_stack *newobj;
13186 CORE_ADDR lowpc;
13187 CORE_ADDR highpc;
13188 struct die_info *child_die;
13189 struct attribute *attr, *call_line, *call_file;
13190 const char *name;
13191 CORE_ADDR baseaddr;
13192 struct block *block;
13193 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13194 std::vector<struct symbol *> template_args;
13195 struct template_symbol *templ_func = NULL;
13196
13197 if (inlined_func)
13198 {
13199 /* If we do not have call site information, we can't show the
13200 caller of this inlined function. That's too confusing, so
13201 only use the scope for local variables. */
13202 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13203 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13204 if (call_line == NULL || call_file == NULL)
13205 {
13206 read_lexical_block_scope (die, cu);
13207 return;
13208 }
13209 }
13210
13211 baseaddr = objfile->text_section_offset ();
13212
13213 name = dwarf2_name (die, cu);
13214
13215 /* Ignore functions with missing or empty names. These are actually
13216 illegal according to the DWARF standard. */
13217 if (name == NULL)
13218 {
13219 complaint (_("missing name for subprogram DIE at %s"),
13220 sect_offset_str (die->sect_off));
13221 return;
13222 }
13223
13224 /* Ignore functions with missing or invalid low and high pc attributes. */
13225 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13226 <= PC_BOUNDS_INVALID)
13227 {
13228 attr = dwarf2_attr (die, DW_AT_external, cu);
13229 if (attr == nullptr || !attr->as_boolean ())
13230 complaint (_("cannot get low and high bounds "
13231 "for subprogram DIE at %s"),
13232 sect_offset_str (die->sect_off));
13233 return;
13234 }
13235
13236 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13237 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13238
13239 /* If we have any template arguments, then we must allocate a
13240 different sort of symbol. */
13241 for (child_die = die->child; child_die; child_die = child_die->sibling)
13242 {
13243 if (child_die->tag == DW_TAG_template_type_param
13244 || child_die->tag == DW_TAG_template_value_param)
13245 {
13246 templ_func = new (&objfile->objfile_obstack) template_symbol;
13247 templ_func->subclass = SYMBOL_TEMPLATE;
13248 break;
13249 }
13250 }
13251
13252 gdb_assert (cu->get_builder () != nullptr);
13253 newobj = cu->get_builder ()->push_context (0, lowpc);
13254 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13255 (struct symbol *) templ_func);
13256
13257 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13258 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13259 cu->language);
13260
13261 /* If there is a location expression for DW_AT_frame_base, record
13262 it. */
13263 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13264 if (attr != nullptr)
13265 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13266
13267 /* If there is a location for the static link, record it. */
13268 newobj->static_link = NULL;
13269 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13270 if (attr != nullptr)
13271 {
13272 newobj->static_link
13273 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13274 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13275 cu->addr_type ());
13276 }
13277
13278 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13279
13280 if (die->child != NULL)
13281 {
13282 child_die = die->child;
13283 while (child_die && child_die->tag)
13284 {
13285 if (child_die->tag == DW_TAG_template_type_param
13286 || child_die->tag == DW_TAG_template_value_param)
13287 {
13288 struct symbol *arg = new_symbol (child_die, NULL, cu);
13289
13290 if (arg != NULL)
13291 template_args.push_back (arg);
13292 }
13293 else
13294 process_die (child_die, cu);
13295 child_die = child_die->sibling;
13296 }
13297 }
13298
13299 inherit_abstract_dies (die, cu);
13300
13301 /* If we have a DW_AT_specification, we might need to import using
13302 directives from the context of the specification DIE. See the
13303 comment in determine_prefix. */
13304 if (cu->language == language_cplus
13305 && dwarf2_attr (die, DW_AT_specification, cu))
13306 {
13307 struct dwarf2_cu *spec_cu = cu;
13308 struct die_info *spec_die = die_specification (die, &spec_cu);
13309
13310 while (spec_die)
13311 {
13312 child_die = spec_die->child;
13313 while (child_die && child_die->tag)
13314 {
13315 if (child_die->tag == DW_TAG_imported_module)
13316 process_die (child_die, spec_cu);
13317 child_die = child_die->sibling;
13318 }
13319
13320 /* In some cases, GCC generates specification DIEs that
13321 themselves contain DW_AT_specification attributes. */
13322 spec_die = die_specification (spec_die, &spec_cu);
13323 }
13324 }
13325
13326 struct context_stack cstk = cu->get_builder ()->pop_context ();
13327 /* Make a block for the local symbols within. */
13328 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13329 cstk.static_link, lowpc, highpc);
13330
13331 /* For C++, set the block's scope. */
13332 if ((cu->language == language_cplus
13333 || cu->language == language_fortran
13334 || cu->language == language_d
13335 || cu->language == language_rust)
13336 && cu->processing_has_namespace_info)
13337 block_set_scope (block, determine_prefix (die, cu),
13338 &objfile->objfile_obstack);
13339
13340 /* If we have address ranges, record them. */
13341 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13342
13343 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13344
13345 /* Attach template arguments to function. */
13346 if (!template_args.empty ())
13347 {
13348 gdb_assert (templ_func != NULL);
13349
13350 templ_func->n_template_arguments = template_args.size ();
13351 templ_func->template_arguments
13352 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13353 templ_func->n_template_arguments);
13354 memcpy (templ_func->template_arguments,
13355 template_args.data (),
13356 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13357
13358 /* Make sure that the symtab is set on the new symbols. Even
13359 though they don't appear in this symtab directly, other parts
13360 of gdb assume that symbols do, and this is reasonably
13361 true. */
13362 for (symbol *sym : template_args)
13363 symbol_set_symtab (sym, symbol_symtab (templ_func));
13364 }
13365
13366 /* In C++, we can have functions nested inside functions (e.g., when
13367 a function declares a class that has methods). This means that
13368 when we finish processing a function scope, we may need to go
13369 back to building a containing block's symbol lists. */
13370 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13371 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13372
13373 /* If we've finished processing a top-level function, subsequent
13374 symbols go in the file symbol list. */
13375 if (cu->get_builder ()->outermost_context_p ())
13376 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13377 }
13378
13379 /* Process all the DIES contained within a lexical block scope. Start
13380 a new scope, process the dies, and then close the scope. */
13381
13382 static void
13383 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13384 {
13385 struct objfile *objfile = cu->per_objfile->objfile;
13386 struct gdbarch *gdbarch = objfile->arch ();
13387 CORE_ADDR lowpc, highpc;
13388 struct die_info *child_die;
13389 CORE_ADDR baseaddr;
13390
13391 baseaddr = objfile->text_section_offset ();
13392
13393 /* Ignore blocks with missing or invalid low and high pc attributes. */
13394 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13395 as multiple lexical blocks? Handling children in a sane way would
13396 be nasty. Might be easier to properly extend generic blocks to
13397 describe ranges. */
13398 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13399 {
13400 case PC_BOUNDS_NOT_PRESENT:
13401 /* DW_TAG_lexical_block has no attributes, process its children as if
13402 there was no wrapping by that DW_TAG_lexical_block.
13403 GCC does no longer produces such DWARF since GCC r224161. */
13404 for (child_die = die->child;
13405 child_die != NULL && child_die->tag;
13406 child_die = child_die->sibling)
13407 {
13408 /* We might already be processing this DIE. This can happen
13409 in an unusual circumstance -- where a subroutine A
13410 appears lexically in another subroutine B, but A actually
13411 inlines B. The recursion is broken here, rather than in
13412 inherit_abstract_dies, because it seems better to simply
13413 drop concrete children here. */
13414 if (!child_die->in_process)
13415 process_die (child_die, cu);
13416 }
13417 return;
13418 case PC_BOUNDS_INVALID:
13419 return;
13420 }
13421 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13422 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13423
13424 cu->get_builder ()->push_context (0, lowpc);
13425 if (die->child != NULL)
13426 {
13427 child_die = die->child;
13428 while (child_die && child_die->tag)
13429 {
13430 process_die (child_die, cu);
13431 child_die = child_die->sibling;
13432 }
13433 }
13434 inherit_abstract_dies (die, cu);
13435 struct context_stack cstk = cu->get_builder ()->pop_context ();
13436
13437 if (*cu->get_builder ()->get_local_symbols () != NULL
13438 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13439 {
13440 struct block *block
13441 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13442 cstk.start_addr, highpc);
13443
13444 /* Note that recording ranges after traversing children, as we
13445 do here, means that recording a parent's ranges entails
13446 walking across all its children's ranges as they appear in
13447 the address map, which is quadratic behavior.
13448
13449 It would be nicer to record the parent's ranges before
13450 traversing its children, simply overriding whatever you find
13451 there. But since we don't even decide whether to create a
13452 block until after we've traversed its children, that's hard
13453 to do. */
13454 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13455 }
13456 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13457 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13458 }
13459
13460 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13461
13462 static void
13463 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13464 {
13465 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13466 struct objfile *objfile = per_objfile->objfile;
13467 struct gdbarch *gdbarch = objfile->arch ();
13468 CORE_ADDR pc, baseaddr;
13469 struct attribute *attr;
13470 struct call_site *call_site, call_site_local;
13471 void **slot;
13472 int nparams;
13473 struct die_info *child_die;
13474
13475 baseaddr = objfile->text_section_offset ();
13476
13477 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13478 if (attr == NULL)
13479 {
13480 /* This was a pre-DWARF-5 GNU extension alias
13481 for DW_AT_call_return_pc. */
13482 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13483 }
13484 if (!attr)
13485 {
13486 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13487 "DIE %s [in module %s]"),
13488 sect_offset_str (die->sect_off), objfile_name (objfile));
13489 return;
13490 }
13491 pc = attr->as_address () + baseaddr;
13492 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13493
13494 if (cu->call_site_htab == NULL)
13495 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13496 NULL, &objfile->objfile_obstack,
13497 hashtab_obstack_allocate, NULL);
13498 call_site_local.pc = pc;
13499 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13500 if (*slot != NULL)
13501 {
13502 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13503 "DIE %s [in module %s]"),
13504 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13505 objfile_name (objfile));
13506 return;
13507 }
13508
13509 /* Count parameters at the caller. */
13510
13511 nparams = 0;
13512 for (child_die = die->child; child_die && child_die->tag;
13513 child_die = child_die->sibling)
13514 {
13515 if (child_die->tag != DW_TAG_call_site_parameter
13516 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13517 {
13518 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13519 "DW_TAG_call_site child DIE %s [in module %s]"),
13520 child_die->tag, sect_offset_str (child_die->sect_off),
13521 objfile_name (objfile));
13522 continue;
13523 }
13524
13525 nparams++;
13526 }
13527
13528 call_site
13529 = ((struct call_site *)
13530 obstack_alloc (&objfile->objfile_obstack,
13531 sizeof (*call_site)
13532 + (sizeof (*call_site->parameter) * (nparams - 1))));
13533 *slot = call_site;
13534 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13535 call_site->pc = pc;
13536
13537 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13538 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13539 {
13540 struct die_info *func_die;
13541
13542 /* Skip also over DW_TAG_inlined_subroutine. */
13543 for (func_die = die->parent;
13544 func_die && func_die->tag != DW_TAG_subprogram
13545 && func_die->tag != DW_TAG_subroutine_type;
13546 func_die = func_die->parent);
13547
13548 /* DW_AT_call_all_calls is a superset
13549 of DW_AT_call_all_tail_calls. */
13550 if (func_die
13551 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13552 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13553 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13554 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13555 {
13556 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13557 not complete. But keep CALL_SITE for look ups via call_site_htab,
13558 both the initial caller containing the real return address PC and
13559 the final callee containing the current PC of a chain of tail
13560 calls do not need to have the tail call list complete. But any
13561 function candidate for a virtual tail call frame searched via
13562 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13563 determined unambiguously. */
13564 }
13565 else
13566 {
13567 struct type *func_type = NULL;
13568
13569 if (func_die)
13570 func_type = get_die_type (func_die, cu);
13571 if (func_type != NULL)
13572 {
13573 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13574
13575 /* Enlist this call site to the function. */
13576 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13577 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13578 }
13579 else
13580 complaint (_("Cannot find function owning DW_TAG_call_site "
13581 "DIE %s [in module %s]"),
13582 sect_offset_str (die->sect_off), objfile_name (objfile));
13583 }
13584 }
13585
13586 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13587 if (attr == NULL)
13588 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13589 if (attr == NULL)
13590 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13591 if (attr == NULL)
13592 {
13593 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13594 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13595 }
13596 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13597 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13598 /* Keep NULL DWARF_BLOCK. */;
13599 else if (attr->form_is_block ())
13600 {
13601 struct dwarf2_locexpr_baton *dlbaton;
13602 struct dwarf_block *block = attr->as_block ();
13603
13604 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13605 dlbaton->data = block->data;
13606 dlbaton->size = block->size;
13607 dlbaton->per_objfile = per_objfile;
13608 dlbaton->per_cu = cu->per_cu;
13609
13610 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13611 }
13612 else if (attr->form_is_ref ())
13613 {
13614 struct dwarf2_cu *target_cu = cu;
13615 struct die_info *target_die;
13616
13617 target_die = follow_die_ref (die, attr, &target_cu);
13618 gdb_assert (target_cu->per_objfile->objfile == objfile);
13619 if (die_is_declaration (target_die, target_cu))
13620 {
13621 const char *target_physname;
13622
13623 /* Prefer the mangled name; otherwise compute the demangled one. */
13624 target_physname = dw2_linkage_name (target_die, target_cu);
13625 if (target_physname == NULL)
13626 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13627 if (target_physname == NULL)
13628 complaint (_("DW_AT_call_target target DIE has invalid "
13629 "physname, for referencing DIE %s [in module %s]"),
13630 sect_offset_str (die->sect_off), objfile_name (objfile));
13631 else
13632 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13633 }
13634 else
13635 {
13636 CORE_ADDR lowpc;
13637
13638 /* DW_AT_entry_pc should be preferred. */
13639 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13640 <= PC_BOUNDS_INVALID)
13641 complaint (_("DW_AT_call_target target DIE has invalid "
13642 "low pc, for referencing DIE %s [in module %s]"),
13643 sect_offset_str (die->sect_off), objfile_name (objfile));
13644 else
13645 {
13646 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13647 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13648 }
13649 }
13650 }
13651 else
13652 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13653 "block nor reference, for DIE %s [in module %s]"),
13654 sect_offset_str (die->sect_off), objfile_name (objfile));
13655
13656 call_site->per_cu = cu->per_cu;
13657 call_site->per_objfile = per_objfile;
13658
13659 for (child_die = die->child;
13660 child_die && child_die->tag;
13661 child_die = child_die->sibling)
13662 {
13663 struct call_site_parameter *parameter;
13664 struct attribute *loc, *origin;
13665
13666 if (child_die->tag != DW_TAG_call_site_parameter
13667 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13668 {
13669 /* Already printed the complaint above. */
13670 continue;
13671 }
13672
13673 gdb_assert (call_site->parameter_count < nparams);
13674 parameter = &call_site->parameter[call_site->parameter_count];
13675
13676 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13677 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13678 register is contained in DW_AT_call_value. */
13679
13680 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13681 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13682 if (origin == NULL)
13683 {
13684 /* This was a pre-DWARF-5 GNU extension alias
13685 for DW_AT_call_parameter. */
13686 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13687 }
13688 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13689 {
13690 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13691
13692 sect_offset sect_off = origin->get_ref_die_offset ();
13693 if (!cu->header.offset_in_cu_p (sect_off))
13694 {
13695 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13696 binding can be done only inside one CU. Such referenced DIE
13697 therefore cannot be even moved to DW_TAG_partial_unit. */
13698 complaint (_("DW_AT_call_parameter offset is not in CU for "
13699 "DW_TAG_call_site child DIE %s [in module %s]"),
13700 sect_offset_str (child_die->sect_off),
13701 objfile_name (objfile));
13702 continue;
13703 }
13704 parameter->u.param_cu_off
13705 = (cu_offset) (sect_off - cu->header.sect_off);
13706 }
13707 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13708 {
13709 complaint (_("No DW_FORM_block* DW_AT_location for "
13710 "DW_TAG_call_site child DIE %s [in module %s]"),
13711 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13712 continue;
13713 }
13714 else
13715 {
13716 struct dwarf_block *block = loc->as_block ();
13717
13718 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13719 (block->data, &block->data[block->size]);
13720 if (parameter->u.dwarf_reg != -1)
13721 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13722 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13723 &block->data[block->size],
13724 &parameter->u.fb_offset))
13725 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13726 else
13727 {
13728 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13729 "for DW_FORM_block* DW_AT_location is supported for "
13730 "DW_TAG_call_site child DIE %s "
13731 "[in module %s]"),
13732 sect_offset_str (child_die->sect_off),
13733 objfile_name (objfile));
13734 continue;
13735 }
13736 }
13737
13738 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13739 if (attr == NULL)
13740 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13741 if (attr == NULL || !attr->form_is_block ())
13742 {
13743 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13744 "DW_TAG_call_site child DIE %s [in module %s]"),
13745 sect_offset_str (child_die->sect_off),
13746 objfile_name (objfile));
13747 continue;
13748 }
13749
13750 struct dwarf_block *block = attr->as_block ();
13751 parameter->value = block->data;
13752 parameter->value_size = block->size;
13753
13754 /* Parameters are not pre-cleared by memset above. */
13755 parameter->data_value = NULL;
13756 parameter->data_value_size = 0;
13757 call_site->parameter_count++;
13758
13759 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13760 if (attr == NULL)
13761 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13762 if (attr != nullptr)
13763 {
13764 if (!attr->form_is_block ())
13765 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13766 "DW_TAG_call_site child DIE %s [in module %s]"),
13767 sect_offset_str (child_die->sect_off),
13768 objfile_name (objfile));
13769 else
13770 {
13771 block = attr->as_block ();
13772 parameter->data_value = block->data;
13773 parameter->data_value_size = block->size;
13774 }
13775 }
13776 }
13777 }
13778
13779 /* Helper function for read_variable. If DIE represents a virtual
13780 table, then return the type of the concrete object that is
13781 associated with the virtual table. Otherwise, return NULL. */
13782
13783 static struct type *
13784 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13785 {
13786 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13787 if (attr == NULL)
13788 return NULL;
13789
13790 /* Find the type DIE. */
13791 struct die_info *type_die = NULL;
13792 struct dwarf2_cu *type_cu = cu;
13793
13794 if (attr->form_is_ref ())
13795 type_die = follow_die_ref (die, attr, &type_cu);
13796 if (type_die == NULL)
13797 return NULL;
13798
13799 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13800 return NULL;
13801 return die_containing_type (type_die, type_cu);
13802 }
13803
13804 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13805
13806 static void
13807 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13808 {
13809 struct rust_vtable_symbol *storage = NULL;
13810
13811 if (cu->language == language_rust)
13812 {
13813 struct type *containing_type = rust_containing_type (die, cu);
13814
13815 if (containing_type != NULL)
13816 {
13817 struct objfile *objfile = cu->per_objfile->objfile;
13818
13819 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13820 storage->concrete_type = containing_type;
13821 storage->subclass = SYMBOL_RUST_VTABLE;
13822 }
13823 }
13824
13825 struct symbol *res = new_symbol (die, NULL, cu, storage);
13826 struct attribute *abstract_origin
13827 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13828 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13829 if (res == NULL && loc && abstract_origin)
13830 {
13831 /* We have a variable without a name, but with a location and an abstract
13832 origin. This may be a concrete instance of an abstract variable
13833 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13834 later. */
13835 struct dwarf2_cu *origin_cu = cu;
13836 struct die_info *origin_die
13837 = follow_die_ref (die, abstract_origin, &origin_cu);
13838 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13839 per_objfile->per_bfd->abstract_to_concrete
13840 [origin_die->sect_off].push_back (die->sect_off);
13841 }
13842 }
13843
13844 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13845 reading .debug_rnglists.
13846 Callback's type should be:
13847 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13848 Return true if the attributes are present and valid, otherwise,
13849 return false. */
13850
13851 template <typename Callback>
13852 static bool
13853 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13854 dwarf_tag tag, Callback &&callback)
13855 {
13856 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13857 struct objfile *objfile = per_objfile->objfile;
13858 bfd *obfd = objfile->obfd;
13859 /* Base address selection entry. */
13860 gdb::optional<CORE_ADDR> base;
13861 const gdb_byte *buffer;
13862 CORE_ADDR baseaddr;
13863 bool overflow = false;
13864 ULONGEST addr_index;
13865 struct dwarf2_section_info *rnglists_section;
13866
13867 base = cu->base_address;
13868 rnglists_section = cu_debug_rnglists_section (cu, tag);
13869 rnglists_section->read (objfile);
13870
13871 if (offset >= rnglists_section->size)
13872 {
13873 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13874 offset);
13875 return false;
13876 }
13877 buffer = rnglists_section->buffer + offset;
13878
13879 baseaddr = objfile->text_section_offset ();
13880
13881 while (1)
13882 {
13883 /* Initialize it due to a false compiler warning. */
13884 CORE_ADDR range_beginning = 0, range_end = 0;
13885 const gdb_byte *buf_end = (rnglists_section->buffer
13886 + rnglists_section->size);
13887 unsigned int bytes_read;
13888
13889 if (buffer == buf_end)
13890 {
13891 overflow = true;
13892 break;
13893 }
13894 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13895 switch (rlet)
13896 {
13897 case DW_RLE_end_of_list:
13898 break;
13899 case DW_RLE_base_address:
13900 if (buffer + cu->header.addr_size > buf_end)
13901 {
13902 overflow = true;
13903 break;
13904 }
13905 base = cu->header.read_address (obfd, buffer, &bytes_read);
13906 buffer += bytes_read;
13907 break;
13908 case DW_RLE_base_addressx:
13909 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13910 buffer += bytes_read;
13911 base = read_addr_index (cu, addr_index);
13912 break;
13913 case DW_RLE_start_length:
13914 if (buffer + cu->header.addr_size > buf_end)
13915 {
13916 overflow = true;
13917 break;
13918 }
13919 range_beginning = cu->header.read_address (obfd, buffer,
13920 &bytes_read);
13921 buffer += bytes_read;
13922 range_end = (range_beginning
13923 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13924 buffer += bytes_read;
13925 if (buffer > buf_end)
13926 {
13927 overflow = true;
13928 break;
13929 }
13930 break;
13931 case DW_RLE_startx_length:
13932 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13933 buffer += bytes_read;
13934 range_beginning = read_addr_index (cu, addr_index);
13935 if (buffer > buf_end)
13936 {
13937 overflow = true;
13938 break;
13939 }
13940 range_end = (range_beginning
13941 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13942 buffer += bytes_read;
13943 break;
13944 case DW_RLE_offset_pair:
13945 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13946 buffer += bytes_read;
13947 if (buffer > buf_end)
13948 {
13949 overflow = true;
13950 break;
13951 }
13952 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13953 buffer += bytes_read;
13954 if (buffer > buf_end)
13955 {
13956 overflow = true;
13957 break;
13958 }
13959 break;
13960 case DW_RLE_start_end:
13961 if (buffer + 2 * cu->header.addr_size > buf_end)
13962 {
13963 overflow = true;
13964 break;
13965 }
13966 range_beginning = cu->header.read_address (obfd, buffer,
13967 &bytes_read);
13968 buffer += bytes_read;
13969 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13970 buffer += bytes_read;
13971 break;
13972 case DW_RLE_startx_endx:
13973 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13974 buffer += bytes_read;
13975 range_beginning = read_addr_index (cu, addr_index);
13976 if (buffer > buf_end)
13977 {
13978 overflow = true;
13979 break;
13980 }
13981 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13982 buffer += bytes_read;
13983 range_end = read_addr_index (cu, addr_index);
13984 break;
13985 default:
13986 complaint (_("Invalid .debug_rnglists data (no base address)"));
13987 return false;
13988 }
13989 if (rlet == DW_RLE_end_of_list || overflow)
13990 break;
13991 if (rlet == DW_RLE_base_address)
13992 continue;
13993
13994 if (range_beginning > range_end)
13995 {
13996 /* Inverted range entries are invalid. */
13997 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13998 return false;
13999 }
14000
14001 /* Empty range entries have no effect. */
14002 if (range_beginning == range_end)
14003 continue;
14004
14005 /* Only DW_RLE_offset_pair needs the base address added. */
14006 if (rlet == DW_RLE_offset_pair)
14007 {
14008 if (!base.has_value ())
14009 {
14010 /* We have no valid base address for the DW_RLE_offset_pair. */
14011 complaint (_("Invalid .debug_rnglists data (no base address for "
14012 "DW_RLE_offset_pair)"));
14013 return false;
14014 }
14015
14016 range_beginning += *base;
14017 range_end += *base;
14018 }
14019
14020 /* A not-uncommon case of bad debug info.
14021 Don't pollute the addrmap with bad data. */
14022 if (range_beginning + baseaddr == 0
14023 && !per_objfile->per_bfd->has_section_at_zero)
14024 {
14025 complaint (_(".debug_rnglists entry has start address of zero"
14026 " [in module %s]"), objfile_name (objfile));
14027 continue;
14028 }
14029
14030 callback (range_beginning, range_end);
14031 }
14032
14033 if (overflow)
14034 {
14035 complaint (_("Offset %d is not terminated "
14036 "for DW_AT_ranges attribute"),
14037 offset);
14038 return false;
14039 }
14040
14041 return true;
14042 }
14043
14044 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14045 Callback's type should be:
14046 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14047 Return 1 if the attributes are present and valid, otherwise, return 0. */
14048
14049 template <typename Callback>
14050 static int
14051 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
14052 Callback &&callback)
14053 {
14054 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14055 struct objfile *objfile = per_objfile->objfile;
14056 struct comp_unit_head *cu_header = &cu->header;
14057 bfd *obfd = objfile->obfd;
14058 unsigned int addr_size = cu_header->addr_size;
14059 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14060 /* Base address selection entry. */
14061 gdb::optional<CORE_ADDR> base;
14062 unsigned int dummy;
14063 const gdb_byte *buffer;
14064 CORE_ADDR baseaddr;
14065
14066 if (cu_header->version >= 5)
14067 return dwarf2_rnglists_process (offset, cu, tag, callback);
14068
14069 base = cu->base_address;
14070
14071 per_objfile->per_bfd->ranges.read (objfile);
14072 if (offset >= per_objfile->per_bfd->ranges.size)
14073 {
14074 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14075 offset);
14076 return 0;
14077 }
14078 buffer = per_objfile->per_bfd->ranges.buffer + offset;
14079
14080 baseaddr = objfile->text_section_offset ();
14081
14082 while (1)
14083 {
14084 CORE_ADDR range_beginning, range_end;
14085
14086 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
14087 buffer += addr_size;
14088 range_end = cu->header.read_address (obfd, buffer, &dummy);
14089 buffer += addr_size;
14090 offset += 2 * addr_size;
14091
14092 /* An end of list marker is a pair of zero addresses. */
14093 if (range_beginning == 0 && range_end == 0)
14094 /* Found the end of list entry. */
14095 break;
14096
14097 /* Each base address selection entry is a pair of 2 values.
14098 The first is the largest possible address, the second is
14099 the base address. Check for a base address here. */
14100 if ((range_beginning & mask) == mask)
14101 {
14102 /* If we found the largest possible address, then we already
14103 have the base address in range_end. */
14104 base = range_end;
14105 continue;
14106 }
14107
14108 if (!base.has_value ())
14109 {
14110 /* We have no valid base address for the ranges
14111 data. */
14112 complaint (_("Invalid .debug_ranges data (no base address)"));
14113 return 0;
14114 }
14115
14116 if (range_beginning > range_end)
14117 {
14118 /* Inverted range entries are invalid. */
14119 complaint (_("Invalid .debug_ranges data (inverted range)"));
14120 return 0;
14121 }
14122
14123 /* Empty range entries have no effect. */
14124 if (range_beginning == range_end)
14125 continue;
14126
14127 range_beginning += *base;
14128 range_end += *base;
14129
14130 /* A not-uncommon case of bad debug info.
14131 Don't pollute the addrmap with bad data. */
14132 if (range_beginning + baseaddr == 0
14133 && !per_objfile->per_bfd->has_section_at_zero)
14134 {
14135 complaint (_(".debug_ranges entry has start address of zero"
14136 " [in module %s]"), objfile_name (objfile));
14137 continue;
14138 }
14139
14140 callback (range_beginning, range_end);
14141 }
14142
14143 return 1;
14144 }
14145
14146 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14147 Return 1 if the attributes are present and valid, otherwise, return 0.
14148 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
14149
14150 static int
14151 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14152 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14153 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14154 {
14155 struct objfile *objfile = cu->per_objfile->objfile;
14156 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
14157 struct gdbarch *gdbarch = objfile->arch ();
14158 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14159 int low_set = 0;
14160 CORE_ADDR low = 0;
14161 CORE_ADDR high = 0;
14162 int retval;
14163
14164 retval = dwarf2_ranges_process (offset, cu, tag,
14165 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14166 {
14167 if (ranges_pst != NULL)
14168 {
14169 CORE_ADDR lowpc;
14170 CORE_ADDR highpc;
14171
14172 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14173 range_beginning + baseaddr)
14174 - baseaddr);
14175 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14176 range_end + baseaddr)
14177 - baseaddr);
14178 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
14179 lowpc, highpc - 1, ranges_pst);
14180 }
14181
14182 /* FIXME: This is recording everything as a low-high
14183 segment of consecutive addresses. We should have a
14184 data structure for discontiguous block ranges
14185 instead. */
14186 if (! low_set)
14187 {
14188 low = range_beginning;
14189 high = range_end;
14190 low_set = 1;
14191 }
14192 else
14193 {
14194 if (range_beginning < low)
14195 low = range_beginning;
14196 if (range_end > high)
14197 high = range_end;
14198 }
14199 });
14200 if (!retval)
14201 return 0;
14202
14203 if (! low_set)
14204 /* If the first entry is an end-of-list marker, the range
14205 describes an empty scope, i.e. no instructions. */
14206 return 0;
14207
14208 if (low_return)
14209 *low_return = low;
14210 if (high_return)
14211 *high_return = high;
14212 return 1;
14213 }
14214
14215 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14216 definition for the return value. *LOWPC and *HIGHPC are set iff
14217 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14218
14219 static enum pc_bounds_kind
14220 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14221 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14222 dwarf2_psymtab *pst)
14223 {
14224 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14225 struct attribute *attr;
14226 struct attribute *attr_high;
14227 CORE_ADDR low = 0;
14228 CORE_ADDR high = 0;
14229 enum pc_bounds_kind ret;
14230
14231 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14232 if (attr_high)
14233 {
14234 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14235 if (attr != nullptr)
14236 {
14237 low = attr->as_address ();
14238 high = attr_high->as_address ();
14239 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14240 high += low;
14241 }
14242 else
14243 /* Found high w/o low attribute. */
14244 return PC_BOUNDS_INVALID;
14245
14246 /* Found consecutive range of addresses. */
14247 ret = PC_BOUNDS_HIGH_LOW;
14248 }
14249 else
14250 {
14251 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14252 if (attr != nullptr && attr->form_is_unsigned ())
14253 {
14254 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14255 on DWARF version). */
14256 ULONGEST ranges_offset = attr->as_unsigned ();
14257
14258 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14259 this value. */
14260 if (die->tag != DW_TAG_compile_unit)
14261 ranges_offset += cu->gnu_ranges_base;
14262
14263 /* Value of the DW_AT_ranges attribute is the offset in the
14264 .debug_ranges section. */
14265 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14266 die->tag))
14267 return PC_BOUNDS_INVALID;
14268 /* Found discontinuous range of addresses. */
14269 ret = PC_BOUNDS_RANGES;
14270 }
14271 else
14272 return PC_BOUNDS_NOT_PRESENT;
14273 }
14274
14275 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14276 if (high <= low)
14277 return PC_BOUNDS_INVALID;
14278
14279 /* When using the GNU linker, .gnu.linkonce. sections are used to
14280 eliminate duplicate copies of functions and vtables and such.
14281 The linker will arbitrarily choose one and discard the others.
14282 The AT_*_pc values for such functions refer to local labels in
14283 these sections. If the section from that file was discarded, the
14284 labels are not in the output, so the relocs get a value of 0.
14285 If this is a discarded function, mark the pc bounds as invalid,
14286 so that GDB will ignore it. */
14287 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14288 return PC_BOUNDS_INVALID;
14289
14290 *lowpc = low;
14291 if (highpc)
14292 *highpc = high;
14293 return ret;
14294 }
14295
14296 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14297 its low and high PC addresses. Do nothing if these addresses could not
14298 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14299 and HIGHPC to the high address if greater than HIGHPC. */
14300
14301 static void
14302 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14303 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14304 struct dwarf2_cu *cu)
14305 {
14306 CORE_ADDR low, high;
14307 struct die_info *child = die->child;
14308
14309 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14310 {
14311 *lowpc = std::min (*lowpc, low);
14312 *highpc = std::max (*highpc, high);
14313 }
14314
14315 /* If the language does not allow nested subprograms (either inside
14316 subprograms or lexical blocks), we're done. */
14317 if (cu->language != language_ada)
14318 return;
14319
14320 /* Check all the children of the given DIE. If it contains nested
14321 subprograms, then check their pc bounds. Likewise, we need to
14322 check lexical blocks as well, as they may also contain subprogram
14323 definitions. */
14324 while (child && child->tag)
14325 {
14326 if (child->tag == DW_TAG_subprogram
14327 || child->tag == DW_TAG_lexical_block)
14328 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14329 child = child->sibling;
14330 }
14331 }
14332
14333 /* Get the low and high pc's represented by the scope DIE, and store
14334 them in *LOWPC and *HIGHPC. If the correct values can't be
14335 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14336
14337 static void
14338 get_scope_pc_bounds (struct die_info *die,
14339 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14340 struct dwarf2_cu *cu)
14341 {
14342 CORE_ADDR best_low = (CORE_ADDR) -1;
14343 CORE_ADDR best_high = (CORE_ADDR) 0;
14344 CORE_ADDR current_low, current_high;
14345
14346 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14347 >= PC_BOUNDS_RANGES)
14348 {
14349 best_low = current_low;
14350 best_high = current_high;
14351 }
14352 else
14353 {
14354 struct die_info *child = die->child;
14355
14356 while (child && child->tag)
14357 {
14358 switch (child->tag) {
14359 case DW_TAG_subprogram:
14360 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14361 break;
14362 case DW_TAG_namespace:
14363 case DW_TAG_module:
14364 /* FIXME: carlton/2004-01-16: Should we do this for
14365 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14366 that current GCC's always emit the DIEs corresponding
14367 to definitions of methods of classes as children of a
14368 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14369 the DIEs giving the declarations, which could be
14370 anywhere). But I don't see any reason why the
14371 standards says that they have to be there. */
14372 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14373
14374 if (current_low != ((CORE_ADDR) -1))
14375 {
14376 best_low = std::min (best_low, current_low);
14377 best_high = std::max (best_high, current_high);
14378 }
14379 break;
14380 default:
14381 /* Ignore. */
14382 break;
14383 }
14384
14385 child = child->sibling;
14386 }
14387 }
14388
14389 *lowpc = best_low;
14390 *highpc = best_high;
14391 }
14392
14393 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14394 in DIE. */
14395
14396 static void
14397 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14398 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14399 {
14400 struct objfile *objfile = cu->per_objfile->objfile;
14401 struct gdbarch *gdbarch = objfile->arch ();
14402 struct attribute *attr;
14403 struct attribute *attr_high;
14404
14405 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14406 if (attr_high)
14407 {
14408 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14409 if (attr != nullptr)
14410 {
14411 CORE_ADDR low = attr->as_address ();
14412 CORE_ADDR high = attr_high->as_address ();
14413
14414 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14415 high += low;
14416
14417 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14418 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14419 cu->get_builder ()->record_block_range (block, low, high - 1);
14420 }
14421 }
14422
14423 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14424 if (attr != nullptr && attr->form_is_unsigned ())
14425 {
14426 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14427 on DWARF version). */
14428 ULONGEST ranges_offset = attr->as_unsigned ();
14429
14430 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14431 this value. */
14432 if (die->tag != DW_TAG_compile_unit)
14433 ranges_offset += cu->gnu_ranges_base;
14434
14435 std::vector<blockrange> blockvec;
14436 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14437 [&] (CORE_ADDR start, CORE_ADDR end)
14438 {
14439 start += baseaddr;
14440 end += baseaddr;
14441 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14442 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14443 cu->get_builder ()->record_block_range (block, start, end - 1);
14444 blockvec.emplace_back (start, end);
14445 });
14446
14447 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14448 }
14449 }
14450
14451 /* Check whether the producer field indicates either of GCC < 4.6, or the
14452 Intel C/C++ compiler, and cache the result in CU. */
14453
14454 static void
14455 check_producer (struct dwarf2_cu *cu)
14456 {
14457 int major, minor;
14458
14459 if (cu->producer == NULL)
14460 {
14461 /* For unknown compilers expect their behavior is DWARF version
14462 compliant.
14463
14464 GCC started to support .debug_types sections by -gdwarf-4 since
14465 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14466 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14467 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14468 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14469 }
14470 else if (producer_is_gcc (cu->producer, &major, &minor))
14471 {
14472 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14473 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14474 }
14475 else if (producer_is_icc (cu->producer, &major, &minor))
14476 {
14477 cu->producer_is_icc = true;
14478 cu->producer_is_icc_lt_14 = major < 14;
14479 }
14480 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14481 cu->producer_is_codewarrior = true;
14482 else
14483 {
14484 /* For other non-GCC compilers, expect their behavior is DWARF version
14485 compliant. */
14486 }
14487
14488 cu->checked_producer = true;
14489 }
14490
14491 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14492 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14493 during 4.6.0 experimental. */
14494
14495 static bool
14496 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14497 {
14498 if (!cu->checked_producer)
14499 check_producer (cu);
14500
14501 return cu->producer_is_gxx_lt_4_6;
14502 }
14503
14504
14505 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14506 with incorrect is_stmt attributes. */
14507
14508 static bool
14509 producer_is_codewarrior (struct dwarf2_cu *cu)
14510 {
14511 if (!cu->checked_producer)
14512 check_producer (cu);
14513
14514 return cu->producer_is_codewarrior;
14515 }
14516
14517 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14518 If that attribute is not available, return the appropriate
14519 default. */
14520
14521 static enum dwarf_access_attribute
14522 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14523 {
14524 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14525 if (attr != nullptr)
14526 {
14527 LONGEST value = attr->constant_value (-1);
14528 if (value == DW_ACCESS_public
14529 || value == DW_ACCESS_protected
14530 || value == DW_ACCESS_private)
14531 return (dwarf_access_attribute) value;
14532 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14533 plongest (value));
14534 }
14535
14536 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14537 {
14538 /* The default DWARF 2 accessibility for members is public, the default
14539 accessibility for inheritance is private. */
14540
14541 if (die->tag != DW_TAG_inheritance)
14542 return DW_ACCESS_public;
14543 else
14544 return DW_ACCESS_private;
14545 }
14546 else
14547 {
14548 /* DWARF 3+ defines the default accessibility a different way. The same
14549 rules apply now for DW_TAG_inheritance as for the members and it only
14550 depends on the container kind. */
14551
14552 if (die->parent->tag == DW_TAG_class_type)
14553 return DW_ACCESS_private;
14554 else
14555 return DW_ACCESS_public;
14556 }
14557 }
14558
14559 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14560 offset. If the attribute was not found return 0, otherwise return
14561 1. If it was found but could not properly be handled, set *OFFSET
14562 to 0. */
14563
14564 static int
14565 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14566 LONGEST *offset)
14567 {
14568 struct attribute *attr;
14569
14570 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14571 if (attr != NULL)
14572 {
14573 *offset = 0;
14574
14575 /* Note that we do not check for a section offset first here.
14576 This is because DW_AT_data_member_location is new in DWARF 4,
14577 so if we see it, we can assume that a constant form is really
14578 a constant and not a section offset. */
14579 if (attr->form_is_constant ())
14580 *offset = attr->constant_value (0);
14581 else if (attr->form_is_section_offset ())
14582 dwarf2_complex_location_expr_complaint ();
14583 else if (attr->form_is_block ())
14584 *offset = decode_locdesc (attr->as_block (), cu);
14585 else
14586 dwarf2_complex_location_expr_complaint ();
14587
14588 return 1;
14589 }
14590
14591 return 0;
14592 }
14593
14594 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14595
14596 static void
14597 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14598 struct field *field)
14599 {
14600 struct attribute *attr;
14601
14602 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14603 if (attr != NULL)
14604 {
14605 if (attr->form_is_constant ())
14606 {
14607 LONGEST offset = attr->constant_value (0);
14608 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14609 }
14610 else if (attr->form_is_section_offset ())
14611 dwarf2_complex_location_expr_complaint ();
14612 else if (attr->form_is_block ())
14613 {
14614 bool handled;
14615 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14616 if (handled)
14617 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14618 else
14619 {
14620 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14621 struct objfile *objfile = per_objfile->objfile;
14622 struct dwarf2_locexpr_baton *dlbaton
14623 = XOBNEW (&objfile->objfile_obstack,
14624 struct dwarf2_locexpr_baton);
14625 dlbaton->data = attr->as_block ()->data;
14626 dlbaton->size = attr->as_block ()->size;
14627 /* When using this baton, we want to compute the address
14628 of the field, not the value. This is why
14629 is_reference is set to false here. */
14630 dlbaton->is_reference = false;
14631 dlbaton->per_objfile = per_objfile;
14632 dlbaton->per_cu = cu->per_cu;
14633
14634 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14635 }
14636 }
14637 else
14638 dwarf2_complex_location_expr_complaint ();
14639 }
14640 }
14641
14642 /* Add an aggregate field to the field list. */
14643
14644 static void
14645 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14646 struct dwarf2_cu *cu)
14647 {
14648 struct objfile *objfile = cu->per_objfile->objfile;
14649 struct gdbarch *gdbarch = objfile->arch ();
14650 struct nextfield *new_field;
14651 struct attribute *attr;
14652 struct field *fp;
14653 const char *fieldname = "";
14654
14655 if (die->tag == DW_TAG_inheritance)
14656 {
14657 fip->baseclasses.emplace_back ();
14658 new_field = &fip->baseclasses.back ();
14659 }
14660 else
14661 {
14662 fip->fields.emplace_back ();
14663 new_field = &fip->fields.back ();
14664 }
14665
14666 new_field->offset = die->sect_off;
14667
14668 new_field->accessibility = dwarf2_access_attribute (die, cu);
14669 if (new_field->accessibility != DW_ACCESS_public)
14670 fip->non_public_fields = true;
14671
14672 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14673 if (attr != nullptr)
14674 new_field->virtuality = attr->as_virtuality ();
14675 else
14676 new_field->virtuality = DW_VIRTUALITY_none;
14677
14678 fp = &new_field->field;
14679
14680 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14681 {
14682 /* Data member other than a C++ static data member. */
14683
14684 /* Get type of field. */
14685 fp->set_type (die_type (die, cu));
14686
14687 SET_FIELD_BITPOS (*fp, 0);
14688
14689 /* Get bit size of field (zero if none). */
14690 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14691 if (attr != nullptr)
14692 {
14693 FIELD_BITSIZE (*fp) = attr->constant_value (0);
14694 }
14695 else
14696 {
14697 FIELD_BITSIZE (*fp) = 0;
14698 }
14699
14700 /* Get bit offset of field. */
14701 handle_data_member_location (die, cu, fp);
14702 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14703 if (attr != nullptr && attr->form_is_constant ())
14704 {
14705 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14706 {
14707 /* For big endian bits, the DW_AT_bit_offset gives the
14708 additional bit offset from the MSB of the containing
14709 anonymous object to the MSB of the field. We don't
14710 have to do anything special since we don't need to
14711 know the size of the anonymous object. */
14712 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14713 + attr->constant_value (0)));
14714 }
14715 else
14716 {
14717 /* For little endian bits, compute the bit offset to the
14718 MSB of the anonymous object, subtract off the number of
14719 bits from the MSB of the field to the MSB of the
14720 object, and then subtract off the number of bits of
14721 the field itself. The result is the bit offset of
14722 the LSB of the field. */
14723 int anonymous_size;
14724 int bit_offset = attr->constant_value (0);
14725
14726 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14727 if (attr != nullptr && attr->form_is_constant ())
14728 {
14729 /* The size of the anonymous object containing
14730 the bit field is explicit, so use the
14731 indicated size (in bytes). */
14732 anonymous_size = attr->constant_value (0);
14733 }
14734 else
14735 {
14736 /* The size of the anonymous object containing
14737 the bit field must be inferred from the type
14738 attribute of the data member containing the
14739 bit field. */
14740 anonymous_size = TYPE_LENGTH (fp->type ());
14741 }
14742 SET_FIELD_BITPOS (*fp,
14743 (FIELD_BITPOS (*fp)
14744 + anonymous_size * bits_per_byte
14745 - bit_offset - FIELD_BITSIZE (*fp)));
14746 }
14747 }
14748 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14749 if (attr != NULL)
14750 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14751 + attr->constant_value (0)));
14752
14753 /* Get name of field. */
14754 fieldname = dwarf2_name (die, cu);
14755 if (fieldname == NULL)
14756 fieldname = "";
14757
14758 /* The name is already allocated along with this objfile, so we don't
14759 need to duplicate it for the type. */
14760 fp->name = fieldname;
14761
14762 /* Change accessibility for artificial fields (e.g. virtual table
14763 pointer or virtual base class pointer) to private. */
14764 if (dwarf2_attr (die, DW_AT_artificial, cu))
14765 {
14766 FIELD_ARTIFICIAL (*fp) = 1;
14767 new_field->accessibility = DW_ACCESS_private;
14768 fip->non_public_fields = true;
14769 }
14770 }
14771 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14772 {
14773 /* C++ static member. */
14774
14775 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14776 is a declaration, but all versions of G++ as of this writing
14777 (so through at least 3.2.1) incorrectly generate
14778 DW_TAG_variable tags. */
14779
14780 const char *physname;
14781
14782 /* Get name of field. */
14783 fieldname = dwarf2_name (die, cu);
14784 if (fieldname == NULL)
14785 return;
14786
14787 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14788 if (attr
14789 /* Only create a symbol if this is an external value.
14790 new_symbol checks this and puts the value in the global symbol
14791 table, which we want. If it is not external, new_symbol
14792 will try to put the value in cu->list_in_scope which is wrong. */
14793 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14794 {
14795 /* A static const member, not much different than an enum as far as
14796 we're concerned, except that we can support more types. */
14797 new_symbol (die, NULL, cu);
14798 }
14799
14800 /* Get physical name. */
14801 physname = dwarf2_physname (fieldname, die, cu);
14802
14803 /* The name is already allocated along with this objfile, so we don't
14804 need to duplicate it for the type. */
14805 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14806 fp->set_type (die_type (die, cu));
14807 FIELD_NAME (*fp) = fieldname;
14808 }
14809 else if (die->tag == DW_TAG_inheritance)
14810 {
14811 /* C++ base class field. */
14812 handle_data_member_location (die, cu, fp);
14813 FIELD_BITSIZE (*fp) = 0;
14814 fp->set_type (die_type (die, cu));
14815 FIELD_NAME (*fp) = fp->type ()->name ();
14816 }
14817 else
14818 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14819 }
14820
14821 /* Can the type given by DIE define another type? */
14822
14823 static bool
14824 type_can_define_types (const struct die_info *die)
14825 {
14826 switch (die->tag)
14827 {
14828 case DW_TAG_typedef:
14829 case DW_TAG_class_type:
14830 case DW_TAG_structure_type:
14831 case DW_TAG_union_type:
14832 case DW_TAG_enumeration_type:
14833 return true;
14834
14835 default:
14836 return false;
14837 }
14838 }
14839
14840 /* Add a type definition defined in the scope of the FIP's class. */
14841
14842 static void
14843 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14844 struct dwarf2_cu *cu)
14845 {
14846 struct decl_field fp;
14847 memset (&fp, 0, sizeof (fp));
14848
14849 gdb_assert (type_can_define_types (die));
14850
14851 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14852 fp.name = dwarf2_name (die, cu);
14853 fp.type = read_type_die (die, cu);
14854
14855 /* Save accessibility. */
14856 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
14857 switch (accessibility)
14858 {
14859 case DW_ACCESS_public:
14860 /* The assumed value if neither private nor protected. */
14861 break;
14862 case DW_ACCESS_private:
14863 fp.is_private = 1;
14864 break;
14865 case DW_ACCESS_protected:
14866 fp.is_protected = 1;
14867 break;
14868 }
14869
14870 if (die->tag == DW_TAG_typedef)
14871 fip->typedef_field_list.push_back (fp);
14872 else
14873 fip->nested_types_list.push_back (fp);
14874 }
14875
14876 /* A convenience typedef that's used when finding the discriminant
14877 field for a variant part. */
14878 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14879 offset_map_type;
14880
14881 /* Compute the discriminant range for a given variant. OBSTACK is
14882 where the results will be stored. VARIANT is the variant to
14883 process. IS_UNSIGNED indicates whether the discriminant is signed
14884 or unsigned. */
14885
14886 static const gdb::array_view<discriminant_range>
14887 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14888 bool is_unsigned)
14889 {
14890 std::vector<discriminant_range> ranges;
14891
14892 if (variant.default_branch)
14893 return {};
14894
14895 if (variant.discr_list_data == nullptr)
14896 {
14897 discriminant_range r
14898 = {variant.discriminant_value, variant.discriminant_value};
14899 ranges.push_back (r);
14900 }
14901 else
14902 {
14903 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14904 variant.discr_list_data->size);
14905 while (!data.empty ())
14906 {
14907 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14908 {
14909 complaint (_("invalid discriminant marker: %d"), data[0]);
14910 break;
14911 }
14912 bool is_range = data[0] == DW_DSC_range;
14913 data = data.slice (1);
14914
14915 ULONGEST low, high;
14916 unsigned int bytes_read;
14917
14918 if (data.empty ())
14919 {
14920 complaint (_("DW_AT_discr_list missing low value"));
14921 break;
14922 }
14923 if (is_unsigned)
14924 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14925 else
14926 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14927 &bytes_read);
14928 data = data.slice (bytes_read);
14929
14930 if (is_range)
14931 {
14932 if (data.empty ())
14933 {
14934 complaint (_("DW_AT_discr_list missing high value"));
14935 break;
14936 }
14937 if (is_unsigned)
14938 high = read_unsigned_leb128 (nullptr, data.data (),
14939 &bytes_read);
14940 else
14941 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14942 &bytes_read);
14943 data = data.slice (bytes_read);
14944 }
14945 else
14946 high = low;
14947
14948 ranges.push_back ({ low, high });
14949 }
14950 }
14951
14952 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14953 ranges.size ());
14954 std::copy (ranges.begin (), ranges.end (), result);
14955 return gdb::array_view<discriminant_range> (result, ranges.size ());
14956 }
14957
14958 static const gdb::array_view<variant_part> create_variant_parts
14959 (struct obstack *obstack,
14960 const offset_map_type &offset_map,
14961 struct field_info *fi,
14962 const std::vector<variant_part_builder> &variant_parts);
14963
14964 /* Fill in a "struct variant" for a given variant field. RESULT is
14965 the variant to fill in. OBSTACK is where any needed allocations
14966 will be done. OFFSET_MAP holds the mapping from section offsets to
14967 fields for the type. FI describes the fields of the type we're
14968 processing. FIELD is the variant field we're converting. */
14969
14970 static void
14971 create_one_variant (variant &result, struct obstack *obstack,
14972 const offset_map_type &offset_map,
14973 struct field_info *fi, const variant_field &field)
14974 {
14975 result.discriminants = convert_variant_range (obstack, field, false);
14976 result.first_field = field.first_field + fi->baseclasses.size ();
14977 result.last_field = field.last_field + fi->baseclasses.size ();
14978 result.parts = create_variant_parts (obstack, offset_map, fi,
14979 field.variant_parts);
14980 }
14981
14982 /* Fill in a "struct variant_part" for a given variant part. RESULT
14983 is the variant part to fill in. OBSTACK is where any needed
14984 allocations will be done. OFFSET_MAP holds the mapping from
14985 section offsets to fields for the type. FI describes the fields of
14986 the type we're processing. BUILDER is the variant part to be
14987 converted. */
14988
14989 static void
14990 create_one_variant_part (variant_part &result,
14991 struct obstack *obstack,
14992 const offset_map_type &offset_map,
14993 struct field_info *fi,
14994 const variant_part_builder &builder)
14995 {
14996 auto iter = offset_map.find (builder.discriminant_offset);
14997 if (iter == offset_map.end ())
14998 {
14999 result.discriminant_index = -1;
15000 /* Doesn't matter. */
15001 result.is_unsigned = false;
15002 }
15003 else
15004 {
15005 result.discriminant_index = iter->second;
15006 result.is_unsigned
15007 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
15008 }
15009
15010 size_t n = builder.variants.size ();
15011 variant *output = new (obstack) variant[n];
15012 for (size_t i = 0; i < n; ++i)
15013 create_one_variant (output[i], obstack, offset_map, fi,
15014 builder.variants[i]);
15015
15016 result.variants = gdb::array_view<variant> (output, n);
15017 }
15018
15019 /* Create a vector of variant parts that can be attached to a type.
15020 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15021 holds the mapping from section offsets to fields for the type. FI
15022 describes the fields of the type we're processing. VARIANT_PARTS
15023 is the vector to convert. */
15024
15025 static const gdb::array_view<variant_part>
15026 create_variant_parts (struct obstack *obstack,
15027 const offset_map_type &offset_map,
15028 struct field_info *fi,
15029 const std::vector<variant_part_builder> &variant_parts)
15030 {
15031 if (variant_parts.empty ())
15032 return {};
15033
15034 size_t n = variant_parts.size ();
15035 variant_part *result = new (obstack) variant_part[n];
15036 for (size_t i = 0; i < n; ++i)
15037 create_one_variant_part (result[i], obstack, offset_map, fi,
15038 variant_parts[i]);
15039
15040 return gdb::array_view<variant_part> (result, n);
15041 }
15042
15043 /* Compute the variant part vector for FIP, attaching it to TYPE when
15044 done. */
15045
15046 static void
15047 add_variant_property (struct field_info *fip, struct type *type,
15048 struct dwarf2_cu *cu)
15049 {
15050 /* Map section offsets of fields to their field index. Note the
15051 field index here does not take the number of baseclasses into
15052 account. */
15053 offset_map_type offset_map;
15054 for (int i = 0; i < fip->fields.size (); ++i)
15055 offset_map[fip->fields[i].offset] = i;
15056
15057 struct objfile *objfile = cu->per_objfile->objfile;
15058 gdb::array_view<variant_part> parts
15059 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15060 fip->variant_parts);
15061
15062 struct dynamic_prop prop;
15063 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15064 obstack_copy (&objfile->objfile_obstack, &parts,
15065 sizeof (parts)));
15066
15067 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15068 }
15069
15070 /* Create the vector of fields, and attach it to the type. */
15071
15072 static void
15073 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15074 struct dwarf2_cu *cu)
15075 {
15076 int nfields = fip->nfields ();
15077
15078 /* Record the field count, allocate space for the array of fields,
15079 and create blank accessibility bitfields if necessary. */
15080 type->set_num_fields (nfields);
15081 type->set_fields
15082 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15083
15084 if (fip->non_public_fields && cu->language != language_ada)
15085 {
15086 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15087
15088 TYPE_FIELD_PRIVATE_BITS (type) =
15089 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15090 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15091
15092 TYPE_FIELD_PROTECTED_BITS (type) =
15093 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15094 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15095
15096 TYPE_FIELD_IGNORE_BITS (type) =
15097 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15098 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15099 }
15100
15101 /* If the type has baseclasses, allocate and clear a bit vector for
15102 TYPE_FIELD_VIRTUAL_BITS. */
15103 if (!fip->baseclasses.empty () && cu->language != language_ada)
15104 {
15105 int num_bytes = B_BYTES (fip->baseclasses.size ());
15106 unsigned char *pointer;
15107
15108 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15109 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15110 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15111 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15112 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15113 }
15114
15115 if (!fip->variant_parts.empty ())
15116 add_variant_property (fip, type, cu);
15117
15118 /* Copy the saved-up fields into the field vector. */
15119 for (int i = 0; i < nfields; ++i)
15120 {
15121 struct nextfield &field
15122 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15123 : fip->fields[i - fip->baseclasses.size ()]);
15124
15125 type->field (i) = field.field;
15126 switch (field.accessibility)
15127 {
15128 case DW_ACCESS_private:
15129 if (cu->language != language_ada)
15130 SET_TYPE_FIELD_PRIVATE (type, i);
15131 break;
15132
15133 case DW_ACCESS_protected:
15134 if (cu->language != language_ada)
15135 SET_TYPE_FIELD_PROTECTED (type, i);
15136 break;
15137
15138 case DW_ACCESS_public:
15139 break;
15140
15141 default:
15142 /* Unknown accessibility. Complain and treat it as public. */
15143 {
15144 complaint (_("unsupported accessibility %d"),
15145 field.accessibility);
15146 }
15147 break;
15148 }
15149 if (i < fip->baseclasses.size ())
15150 {
15151 switch (field.virtuality)
15152 {
15153 case DW_VIRTUALITY_virtual:
15154 case DW_VIRTUALITY_pure_virtual:
15155 if (cu->language == language_ada)
15156 error (_("unexpected virtuality in component of Ada type"));
15157 SET_TYPE_FIELD_VIRTUAL (type, i);
15158 break;
15159 }
15160 }
15161 }
15162 }
15163
15164 /* Return true if this member function is a constructor, false
15165 otherwise. */
15166
15167 static int
15168 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15169 {
15170 const char *fieldname;
15171 const char *type_name;
15172 int len;
15173
15174 if (die->parent == NULL)
15175 return 0;
15176
15177 if (die->parent->tag != DW_TAG_structure_type
15178 && die->parent->tag != DW_TAG_union_type
15179 && die->parent->tag != DW_TAG_class_type)
15180 return 0;
15181
15182 fieldname = dwarf2_name (die, cu);
15183 type_name = dwarf2_name (die->parent, cu);
15184 if (fieldname == NULL || type_name == NULL)
15185 return 0;
15186
15187 len = strlen (fieldname);
15188 return (strncmp (fieldname, type_name, len) == 0
15189 && (type_name[len] == '\0' || type_name[len] == '<'));
15190 }
15191
15192 /* Add a member function to the proper fieldlist. */
15193
15194 static void
15195 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15196 struct type *type, struct dwarf2_cu *cu)
15197 {
15198 struct objfile *objfile = cu->per_objfile->objfile;
15199 struct attribute *attr;
15200 int i;
15201 struct fnfieldlist *flp = nullptr;
15202 struct fn_field *fnp;
15203 const char *fieldname;
15204 struct type *this_type;
15205
15206 if (cu->language == language_ada)
15207 error (_("unexpected member function in Ada type"));
15208
15209 /* Get name of member function. */
15210 fieldname = dwarf2_name (die, cu);
15211 if (fieldname == NULL)
15212 return;
15213
15214 /* Look up member function name in fieldlist. */
15215 for (i = 0; i < fip->fnfieldlists.size (); i++)
15216 {
15217 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15218 {
15219 flp = &fip->fnfieldlists[i];
15220 break;
15221 }
15222 }
15223
15224 /* Create a new fnfieldlist if necessary. */
15225 if (flp == nullptr)
15226 {
15227 fip->fnfieldlists.emplace_back ();
15228 flp = &fip->fnfieldlists.back ();
15229 flp->name = fieldname;
15230 i = fip->fnfieldlists.size () - 1;
15231 }
15232
15233 /* Create a new member function field and add it to the vector of
15234 fnfieldlists. */
15235 flp->fnfields.emplace_back ();
15236 fnp = &flp->fnfields.back ();
15237
15238 /* Delay processing of the physname until later. */
15239 if (cu->language == language_cplus)
15240 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15241 die, cu);
15242 else
15243 {
15244 const char *physname = dwarf2_physname (fieldname, die, cu);
15245 fnp->physname = physname ? physname : "";
15246 }
15247
15248 fnp->type = alloc_type (objfile);
15249 this_type = read_type_die (die, cu);
15250 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15251 {
15252 int nparams = this_type->num_fields ();
15253
15254 /* TYPE is the domain of this method, and THIS_TYPE is the type
15255 of the method itself (TYPE_CODE_METHOD). */
15256 smash_to_method_type (fnp->type, type,
15257 TYPE_TARGET_TYPE (this_type),
15258 this_type->fields (),
15259 this_type->num_fields (),
15260 this_type->has_varargs ());
15261
15262 /* Handle static member functions.
15263 Dwarf2 has no clean way to discern C++ static and non-static
15264 member functions. G++ helps GDB by marking the first
15265 parameter for non-static member functions (which is the this
15266 pointer) as artificial. We obtain this information from
15267 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15268 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15269 fnp->voffset = VOFFSET_STATIC;
15270 }
15271 else
15272 complaint (_("member function type missing for '%s'"),
15273 dwarf2_full_name (fieldname, die, cu));
15274
15275 /* Get fcontext from DW_AT_containing_type if present. */
15276 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15277 fnp->fcontext = die_containing_type (die, cu);
15278
15279 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15280 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15281
15282 /* Get accessibility. */
15283 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15284 switch (accessibility)
15285 {
15286 case DW_ACCESS_private:
15287 fnp->is_private = 1;
15288 break;
15289 case DW_ACCESS_protected:
15290 fnp->is_protected = 1;
15291 break;
15292 }
15293
15294 /* Check for artificial methods. */
15295 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15296 if (attr && attr->as_boolean ())
15297 fnp->is_artificial = 1;
15298
15299 /* Check for defaulted methods. */
15300 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15301 if (attr != nullptr)
15302 fnp->defaulted = attr->defaulted ();
15303
15304 /* Check for deleted methods. */
15305 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15306 if (attr != nullptr && attr->as_boolean ())
15307 fnp->is_deleted = 1;
15308
15309 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15310
15311 /* Get index in virtual function table if it is a virtual member
15312 function. For older versions of GCC, this is an offset in the
15313 appropriate virtual table, as specified by DW_AT_containing_type.
15314 For everyone else, it is an expression to be evaluated relative
15315 to the object address. */
15316
15317 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15318 if (attr != nullptr)
15319 {
15320 if (attr->form_is_block () && attr->as_block ()->size > 0)
15321 {
15322 struct dwarf_block *block = attr->as_block ();
15323
15324 if (block->data[0] == DW_OP_constu)
15325 {
15326 /* Old-style GCC. */
15327 fnp->voffset = decode_locdesc (block, cu) + 2;
15328 }
15329 else if (block->data[0] == DW_OP_deref
15330 || (block->size > 1
15331 && block->data[0] == DW_OP_deref_size
15332 && block->data[1] == cu->header.addr_size))
15333 {
15334 fnp->voffset = decode_locdesc (block, cu);
15335 if ((fnp->voffset % cu->header.addr_size) != 0)
15336 dwarf2_complex_location_expr_complaint ();
15337 else
15338 fnp->voffset /= cu->header.addr_size;
15339 fnp->voffset += 2;
15340 }
15341 else
15342 dwarf2_complex_location_expr_complaint ();
15343
15344 if (!fnp->fcontext)
15345 {
15346 /* If there is no `this' field and no DW_AT_containing_type,
15347 we cannot actually find a base class context for the
15348 vtable! */
15349 if (this_type->num_fields () == 0
15350 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15351 {
15352 complaint (_("cannot determine context for virtual member "
15353 "function \"%s\" (offset %s)"),
15354 fieldname, sect_offset_str (die->sect_off));
15355 }
15356 else
15357 {
15358 fnp->fcontext
15359 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15360 }
15361 }
15362 }
15363 else if (attr->form_is_section_offset ())
15364 {
15365 dwarf2_complex_location_expr_complaint ();
15366 }
15367 else
15368 {
15369 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15370 fieldname);
15371 }
15372 }
15373 else
15374 {
15375 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15376 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15377 {
15378 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15379 complaint (_("Member function \"%s\" (offset %s) is virtual "
15380 "but the vtable offset is not specified"),
15381 fieldname, sect_offset_str (die->sect_off));
15382 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15383 TYPE_CPLUS_DYNAMIC (type) = 1;
15384 }
15385 }
15386 }
15387
15388 /* Create the vector of member function fields, and attach it to the type. */
15389
15390 static void
15391 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15392 struct dwarf2_cu *cu)
15393 {
15394 if (cu->language == language_ada)
15395 error (_("unexpected member functions in Ada type"));
15396
15397 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15398 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15399 TYPE_ALLOC (type,
15400 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15401
15402 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15403 {
15404 struct fnfieldlist &nf = fip->fnfieldlists[i];
15405 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15406
15407 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15408 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15409 fn_flp->fn_fields = (struct fn_field *)
15410 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15411
15412 for (int k = 0; k < nf.fnfields.size (); ++k)
15413 fn_flp->fn_fields[k] = nf.fnfields[k];
15414 }
15415
15416 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15417 }
15418
15419 /* Returns non-zero if NAME is the name of a vtable member in CU's
15420 language, zero otherwise. */
15421 static int
15422 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15423 {
15424 static const char vptr[] = "_vptr";
15425
15426 /* Look for the C++ form of the vtable. */
15427 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15428 return 1;
15429
15430 return 0;
15431 }
15432
15433 /* GCC outputs unnamed structures that are really pointers to member
15434 functions, with the ABI-specified layout. If TYPE describes
15435 such a structure, smash it into a member function type.
15436
15437 GCC shouldn't do this; it should just output pointer to member DIEs.
15438 This is GCC PR debug/28767. */
15439
15440 static void
15441 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15442 {
15443 struct type *pfn_type, *self_type, *new_type;
15444
15445 /* Check for a structure with no name and two children. */
15446 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15447 return;
15448
15449 /* Check for __pfn and __delta members. */
15450 if (TYPE_FIELD_NAME (type, 0) == NULL
15451 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15452 || TYPE_FIELD_NAME (type, 1) == NULL
15453 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15454 return;
15455
15456 /* Find the type of the method. */
15457 pfn_type = type->field (0).type ();
15458 if (pfn_type == NULL
15459 || pfn_type->code () != TYPE_CODE_PTR
15460 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15461 return;
15462
15463 /* Look for the "this" argument. */
15464 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15465 if (pfn_type->num_fields () == 0
15466 /* || pfn_type->field (0).type () == NULL */
15467 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15468 return;
15469
15470 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15471 new_type = alloc_type (objfile);
15472 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15473 pfn_type->fields (), pfn_type->num_fields (),
15474 pfn_type->has_varargs ());
15475 smash_to_methodptr_type (type, new_type);
15476 }
15477
15478 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15479 requires rewriting, then copy it and return the updated copy.
15480 Otherwise return nullptr. */
15481
15482 static struct type *
15483 rewrite_array_type (struct type *type)
15484 {
15485 if (type->code () != TYPE_CODE_ARRAY)
15486 return nullptr;
15487
15488 struct type *index_type = type->index_type ();
15489 range_bounds *current_bounds = index_type->bounds ();
15490
15491 /* Handle multi-dimensional arrays. */
15492 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15493 if (new_target == nullptr)
15494 {
15495 /* Maybe we don't need to rewrite this array. */
15496 if (current_bounds->low.kind () == PROP_CONST
15497 && current_bounds->high.kind () == PROP_CONST)
15498 return nullptr;
15499 }
15500
15501 /* Either the target type was rewritten, or the bounds have to be
15502 updated. Either way we want to copy the type and update
15503 everything. */
15504 struct type *copy = copy_type (type);
15505 int nfields = copy->num_fields ();
15506 field *new_fields
15507 = ((struct field *) TYPE_ZALLOC (copy,
15508 nfields * sizeof (struct field)));
15509 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15510 copy->set_fields (new_fields);
15511 if (new_target != nullptr)
15512 TYPE_TARGET_TYPE (copy) = new_target;
15513
15514 struct type *index_copy = copy_type (index_type);
15515 range_bounds *bounds
15516 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15517 sizeof (range_bounds));
15518 *bounds = *current_bounds;
15519 bounds->low.set_const_val (1);
15520 bounds->high.set_const_val (0);
15521 index_copy->set_bounds (bounds);
15522 copy->set_index_type (index_copy);
15523
15524 return copy;
15525 }
15526
15527 /* While some versions of GCC will generate complicated DWARF for an
15528 array (see quirk_ada_thick_pointer), more recent versions were
15529 modified to emit an explicit thick pointer structure. However, in
15530 this case, the array still has DWARF expressions for its ranges,
15531 and these must be ignored. */
15532
15533 static void
15534 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15535 struct type *type)
15536 {
15537 gdb_assert (cu->language == language_ada);
15538
15539 /* Check for a structure with two children. */
15540 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15541 return;
15542
15543 /* Check for P_ARRAY and P_BOUNDS members. */
15544 if (TYPE_FIELD_NAME (type, 0) == NULL
15545 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15546 || TYPE_FIELD_NAME (type, 1) == NULL
15547 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15548 return;
15549
15550 /* Make sure we're looking at a pointer to an array. */
15551 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15552 return;
15553
15554 /* The Ada code already knows how to handle these types, so all that
15555 we need to do is turn the bounds into static bounds. However, we
15556 don't want to rewrite existing array or index types in-place,
15557 because those may be referenced in other contexts where this
15558 rewriting is undesirable. */
15559 struct type *new_ary_type
15560 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15561 if (new_ary_type != nullptr)
15562 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15563 }
15564
15565 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15566 appropriate error checking and issuing complaints if there is a
15567 problem. */
15568
15569 static ULONGEST
15570 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15571 {
15572 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15573
15574 if (attr == nullptr)
15575 return 0;
15576
15577 if (!attr->form_is_constant ())
15578 {
15579 complaint (_("DW_AT_alignment must have constant form"
15580 " - DIE at %s [in module %s]"),
15581 sect_offset_str (die->sect_off),
15582 objfile_name (cu->per_objfile->objfile));
15583 return 0;
15584 }
15585
15586 LONGEST val = attr->constant_value (0);
15587 if (val < 0)
15588 {
15589 complaint (_("DW_AT_alignment value must not be negative"
15590 " - DIE at %s [in module %s]"),
15591 sect_offset_str (die->sect_off),
15592 objfile_name (cu->per_objfile->objfile));
15593 return 0;
15594 }
15595 ULONGEST align = val;
15596
15597 if (align == 0)
15598 {
15599 complaint (_("DW_AT_alignment value must not be zero"
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 if ((align & (align - 1)) != 0)
15606 {
15607 complaint (_("DW_AT_alignment value must be a power of 2"
15608 " - DIE at %s [in module %s]"),
15609 sect_offset_str (die->sect_off),
15610 objfile_name (cu->per_objfile->objfile));
15611 return 0;
15612 }
15613
15614 return align;
15615 }
15616
15617 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15618 the alignment for TYPE. */
15619
15620 static void
15621 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15622 struct type *type)
15623 {
15624 if (!set_type_align (type, get_alignment (cu, die)))
15625 complaint (_("DW_AT_alignment value too large"
15626 " - DIE at %s [in module %s]"),
15627 sect_offset_str (die->sect_off),
15628 objfile_name (cu->per_objfile->objfile));
15629 }
15630
15631 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15632 constant for a type, according to DWARF5 spec, Table 5.5. */
15633
15634 static bool
15635 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15636 {
15637 switch (value)
15638 {
15639 case DW_CC_normal:
15640 case DW_CC_pass_by_reference:
15641 case DW_CC_pass_by_value:
15642 return true;
15643
15644 default:
15645 complaint (_("unrecognized DW_AT_calling_convention value "
15646 "(%s) for a type"), pulongest (value));
15647 return false;
15648 }
15649 }
15650
15651 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15652 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15653 also according to GNU-specific values (see include/dwarf2.h). */
15654
15655 static bool
15656 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15657 {
15658 switch (value)
15659 {
15660 case DW_CC_normal:
15661 case DW_CC_program:
15662 case DW_CC_nocall:
15663 return true;
15664
15665 case DW_CC_GNU_renesas_sh:
15666 case DW_CC_GNU_borland_fastcall_i386:
15667 case DW_CC_GDB_IBM_OpenCL:
15668 return true;
15669
15670 default:
15671 complaint (_("unrecognized DW_AT_calling_convention value "
15672 "(%s) for a subroutine"), pulongest (value));
15673 return false;
15674 }
15675 }
15676
15677 /* Called when we find the DIE that starts a structure or union scope
15678 (definition) to create a type for the structure or union. Fill in
15679 the type's name and general properties; the members will not be
15680 processed until process_structure_scope. A symbol table entry for
15681 the type will also not be done until process_structure_scope (assuming
15682 the type has a name).
15683
15684 NOTE: we need to call these functions regardless of whether or not the
15685 DIE has a DW_AT_name attribute, since it might be an anonymous
15686 structure or union. This gets the type entered into our set of
15687 user defined types. */
15688
15689 static struct type *
15690 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15691 {
15692 struct objfile *objfile = cu->per_objfile->objfile;
15693 struct type *type;
15694 struct attribute *attr;
15695 const char *name;
15696
15697 /* If the definition of this type lives in .debug_types, read that type.
15698 Don't follow DW_AT_specification though, that will take us back up
15699 the chain and we want to go down. */
15700 attr = die->attr (DW_AT_signature);
15701 if (attr != nullptr)
15702 {
15703 type = get_DW_AT_signature_type (die, attr, cu);
15704
15705 /* The type's CU may not be the same as CU.
15706 Ensure TYPE is recorded with CU in die_type_hash. */
15707 return set_die_type (die, type, cu);
15708 }
15709
15710 type = alloc_type (objfile);
15711 INIT_CPLUS_SPECIFIC (type);
15712
15713 name = dwarf2_name (die, cu);
15714 if (name != NULL)
15715 {
15716 if (cu->language == language_cplus
15717 || cu->language == language_d
15718 || cu->language == language_rust)
15719 {
15720 const char *full_name = dwarf2_full_name (name, die, cu);
15721
15722 /* dwarf2_full_name might have already finished building the DIE's
15723 type. If so, there is no need to continue. */
15724 if (get_die_type (die, cu) != NULL)
15725 return get_die_type (die, cu);
15726
15727 type->set_name (full_name);
15728 }
15729 else
15730 {
15731 /* The name is already allocated along with this objfile, so
15732 we don't need to duplicate it for the type. */
15733 type->set_name (name);
15734 }
15735 }
15736
15737 if (die->tag == DW_TAG_structure_type)
15738 {
15739 type->set_code (TYPE_CODE_STRUCT);
15740 }
15741 else if (die->tag == DW_TAG_union_type)
15742 {
15743 type->set_code (TYPE_CODE_UNION);
15744 }
15745 else
15746 {
15747 type->set_code (TYPE_CODE_STRUCT);
15748 }
15749
15750 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15751 type->set_is_declared_class (true);
15752
15753 /* Store the calling convention in the type if it's available in
15754 the die. Otherwise the calling convention remains set to
15755 the default value DW_CC_normal. */
15756 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15757 if (attr != nullptr
15758 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
15759 {
15760 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15761 TYPE_CPLUS_CALLING_CONVENTION (type)
15762 = (enum dwarf_calling_convention) (attr->constant_value (0));
15763 }
15764
15765 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15766 if (attr != nullptr)
15767 {
15768 if (attr->form_is_constant ())
15769 TYPE_LENGTH (type) = attr->constant_value (0);
15770 else
15771 {
15772 struct dynamic_prop prop;
15773 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15774 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15775 TYPE_LENGTH (type) = 0;
15776 }
15777 }
15778 else
15779 {
15780 TYPE_LENGTH (type) = 0;
15781 }
15782
15783 maybe_set_alignment (cu, die, type);
15784
15785 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15786 {
15787 /* ICC<14 does not output the required DW_AT_declaration on
15788 incomplete types, but gives them a size of zero. */
15789 type->set_is_stub (true);
15790 }
15791 else
15792 type->set_stub_is_supported (true);
15793
15794 if (die_is_declaration (die, cu))
15795 type->set_is_stub (true);
15796 else if (attr == NULL && die->child == NULL
15797 && producer_is_realview (cu->producer))
15798 /* RealView does not output the required DW_AT_declaration
15799 on incomplete types. */
15800 type->set_is_stub (true);
15801
15802 /* We need to add the type field to the die immediately so we don't
15803 infinitely recurse when dealing with pointers to the structure
15804 type within the structure itself. */
15805 set_die_type (die, type, cu);
15806
15807 /* set_die_type should be already done. */
15808 set_descriptive_type (type, die, cu);
15809
15810 return type;
15811 }
15812
15813 static void handle_struct_member_die
15814 (struct die_info *child_die,
15815 struct type *type,
15816 struct field_info *fi,
15817 std::vector<struct symbol *> *template_args,
15818 struct dwarf2_cu *cu);
15819
15820 /* A helper for handle_struct_member_die that handles
15821 DW_TAG_variant_part. */
15822
15823 static void
15824 handle_variant_part (struct die_info *die, struct type *type,
15825 struct field_info *fi,
15826 std::vector<struct symbol *> *template_args,
15827 struct dwarf2_cu *cu)
15828 {
15829 variant_part_builder *new_part;
15830 if (fi->current_variant_part == nullptr)
15831 {
15832 fi->variant_parts.emplace_back ();
15833 new_part = &fi->variant_parts.back ();
15834 }
15835 else if (!fi->current_variant_part->processing_variant)
15836 {
15837 complaint (_("nested DW_TAG_variant_part seen "
15838 "- DIE at %s [in module %s]"),
15839 sect_offset_str (die->sect_off),
15840 objfile_name (cu->per_objfile->objfile));
15841 return;
15842 }
15843 else
15844 {
15845 variant_field &current = fi->current_variant_part->variants.back ();
15846 current.variant_parts.emplace_back ();
15847 new_part = &current.variant_parts.back ();
15848 }
15849
15850 /* When we recurse, we want callees to add to this new variant
15851 part. */
15852 scoped_restore save_current_variant_part
15853 = make_scoped_restore (&fi->current_variant_part, new_part);
15854
15855 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15856 if (discr == NULL)
15857 {
15858 /* It's a univariant form, an extension we support. */
15859 }
15860 else if (discr->form_is_ref ())
15861 {
15862 struct dwarf2_cu *target_cu = cu;
15863 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15864
15865 new_part->discriminant_offset = target_die->sect_off;
15866 }
15867 else
15868 {
15869 complaint (_("DW_AT_discr does not have DIE reference form"
15870 " - DIE at %s [in module %s]"),
15871 sect_offset_str (die->sect_off),
15872 objfile_name (cu->per_objfile->objfile));
15873 }
15874
15875 for (die_info *child_die = die->child;
15876 child_die != NULL;
15877 child_die = child_die->sibling)
15878 handle_struct_member_die (child_die, type, fi, template_args, cu);
15879 }
15880
15881 /* A helper for handle_struct_member_die that handles
15882 DW_TAG_variant. */
15883
15884 static void
15885 handle_variant (struct die_info *die, struct type *type,
15886 struct field_info *fi,
15887 std::vector<struct symbol *> *template_args,
15888 struct dwarf2_cu *cu)
15889 {
15890 if (fi->current_variant_part == nullptr)
15891 {
15892 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
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 if (fi->current_variant_part->processing_variant)
15899 {
15900 complaint (_("nested DW_TAG_variant seen "
15901 "- DIE at %s [in module %s]"),
15902 sect_offset_str (die->sect_off),
15903 objfile_name (cu->per_objfile->objfile));
15904 return;
15905 }
15906
15907 scoped_restore save_processing_variant
15908 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15909 true);
15910
15911 fi->current_variant_part->variants.emplace_back ();
15912 variant_field &variant = fi->current_variant_part->variants.back ();
15913 variant.first_field = fi->fields.size ();
15914
15915 /* In a variant we want to get the discriminant and also add a
15916 field for our sole member child. */
15917 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15918 if (discr == nullptr || !discr->form_is_constant ())
15919 {
15920 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15921 if (discr == nullptr || discr->as_block ()->size == 0)
15922 variant.default_branch = true;
15923 else
15924 variant.discr_list_data = discr->as_block ();
15925 }
15926 else
15927 variant.discriminant_value = discr->constant_value (0);
15928
15929 for (die_info *variant_child = die->child;
15930 variant_child != NULL;
15931 variant_child = variant_child->sibling)
15932 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15933
15934 variant.last_field = fi->fields.size ();
15935 }
15936
15937 /* A helper for process_structure_scope that handles a single member
15938 DIE. */
15939
15940 static void
15941 handle_struct_member_die (struct die_info *child_die, struct type *type,
15942 struct field_info *fi,
15943 std::vector<struct symbol *> *template_args,
15944 struct dwarf2_cu *cu)
15945 {
15946 if (child_die->tag == DW_TAG_member
15947 || child_die->tag == DW_TAG_variable)
15948 {
15949 /* NOTE: carlton/2002-11-05: A C++ static data member
15950 should be a DW_TAG_member that is a declaration, but
15951 all versions of G++ as of this writing (so through at
15952 least 3.2.1) incorrectly generate DW_TAG_variable
15953 tags for them instead. */
15954 dwarf2_add_field (fi, child_die, cu);
15955 }
15956 else if (child_die->tag == DW_TAG_subprogram)
15957 {
15958 /* Rust doesn't have member functions in the C++ sense.
15959 However, it does emit ordinary functions as children
15960 of a struct DIE. */
15961 if (cu->language == language_rust)
15962 read_func_scope (child_die, cu);
15963 else
15964 {
15965 /* C++ member function. */
15966 dwarf2_add_member_fn (fi, child_die, type, cu);
15967 }
15968 }
15969 else if (child_die->tag == DW_TAG_inheritance)
15970 {
15971 /* C++ base class field. */
15972 dwarf2_add_field (fi, child_die, cu);
15973 }
15974 else if (type_can_define_types (child_die))
15975 dwarf2_add_type_defn (fi, child_die, cu);
15976 else if (child_die->tag == DW_TAG_template_type_param
15977 || child_die->tag == DW_TAG_template_value_param)
15978 {
15979 struct symbol *arg = new_symbol (child_die, NULL, cu);
15980
15981 if (arg != NULL)
15982 template_args->push_back (arg);
15983 }
15984 else if (child_die->tag == DW_TAG_variant_part)
15985 handle_variant_part (child_die, type, fi, template_args, cu);
15986 else if (child_die->tag == DW_TAG_variant)
15987 handle_variant (child_die, type, fi, template_args, cu);
15988 }
15989
15990 /* Finish creating a structure or union type, including filling in
15991 its members and creating a symbol for it. */
15992
15993 static void
15994 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15995 {
15996 struct objfile *objfile = cu->per_objfile->objfile;
15997 struct die_info *child_die;
15998 struct type *type;
15999
16000 type = get_die_type (die, cu);
16001 if (type == NULL)
16002 type = read_structure_type (die, cu);
16003
16004 bool has_template_parameters = false;
16005 if (die->child != NULL && ! die_is_declaration (die, cu))
16006 {
16007 struct field_info fi;
16008 std::vector<struct symbol *> template_args;
16009
16010 child_die = die->child;
16011
16012 while (child_die && child_die->tag)
16013 {
16014 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16015 child_die = child_die->sibling;
16016 }
16017
16018 /* Attach template arguments to type. */
16019 if (!template_args.empty ())
16020 {
16021 has_template_parameters = true;
16022 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16023 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16024 TYPE_TEMPLATE_ARGUMENTS (type)
16025 = XOBNEWVEC (&objfile->objfile_obstack,
16026 struct symbol *,
16027 TYPE_N_TEMPLATE_ARGUMENTS (type));
16028 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16029 template_args.data (),
16030 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16031 * sizeof (struct symbol *)));
16032 }
16033
16034 /* Attach fields and member functions to the type. */
16035 if (fi.nfields () > 0)
16036 dwarf2_attach_fields_to_type (&fi, type, cu);
16037 if (!fi.fnfieldlists.empty ())
16038 {
16039 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16040
16041 /* Get the type which refers to the base class (possibly this
16042 class itself) which contains the vtable pointer for the current
16043 class from the DW_AT_containing_type attribute. This use of
16044 DW_AT_containing_type is a GNU extension. */
16045
16046 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16047 {
16048 struct type *t = die_containing_type (die, cu);
16049
16050 set_type_vptr_basetype (type, t);
16051 if (type == t)
16052 {
16053 int i;
16054
16055 /* Our own class provides vtbl ptr. */
16056 for (i = t->num_fields () - 1;
16057 i >= TYPE_N_BASECLASSES (t);
16058 --i)
16059 {
16060 const char *fieldname = TYPE_FIELD_NAME (t, i);
16061
16062 if (is_vtable_name (fieldname, cu))
16063 {
16064 set_type_vptr_fieldno (type, i);
16065 break;
16066 }
16067 }
16068
16069 /* Complain if virtual function table field not found. */
16070 if (i < TYPE_N_BASECLASSES (t))
16071 complaint (_("virtual function table pointer "
16072 "not found when defining class '%s'"),
16073 type->name () ? type->name () : "");
16074 }
16075 else
16076 {
16077 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16078 }
16079 }
16080 else if (cu->producer
16081 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16082 {
16083 /* The IBM XLC compiler does not provide direct indication
16084 of the containing type, but the vtable pointer is
16085 always named __vfp. */
16086
16087 int i;
16088
16089 for (i = type->num_fields () - 1;
16090 i >= TYPE_N_BASECLASSES (type);
16091 --i)
16092 {
16093 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16094 {
16095 set_type_vptr_fieldno (type, i);
16096 set_type_vptr_basetype (type, type);
16097 break;
16098 }
16099 }
16100 }
16101 }
16102
16103 /* Copy fi.typedef_field_list linked list elements content into the
16104 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16105 if (!fi.typedef_field_list.empty ())
16106 {
16107 int count = fi.typedef_field_list.size ();
16108
16109 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16110 TYPE_TYPEDEF_FIELD_ARRAY (type)
16111 = ((struct decl_field *)
16112 TYPE_ALLOC (type,
16113 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16114 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16115
16116 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16117 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16118 }
16119
16120 /* Copy fi.nested_types_list linked list elements content into the
16121 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16122 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16123 {
16124 int count = fi.nested_types_list.size ();
16125
16126 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16127 TYPE_NESTED_TYPES_ARRAY (type)
16128 = ((struct decl_field *)
16129 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16130 TYPE_NESTED_TYPES_COUNT (type) = count;
16131
16132 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16133 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16134 }
16135 }
16136
16137 quirk_gcc_member_function_pointer (type, objfile);
16138 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16139 cu->rust_unions.push_back (type);
16140 else if (cu->language == language_ada)
16141 quirk_ada_thick_pointer_struct (die, cu, type);
16142
16143 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16144 snapshots) has been known to create a die giving a declaration
16145 for a class that has, as a child, a die giving a definition for a
16146 nested class. So we have to process our children even if the
16147 current die is a declaration. Normally, of course, a declaration
16148 won't have any children at all. */
16149
16150 child_die = die->child;
16151
16152 while (child_die != NULL && child_die->tag)
16153 {
16154 if (child_die->tag == DW_TAG_member
16155 || child_die->tag == DW_TAG_variable
16156 || child_die->tag == DW_TAG_inheritance
16157 || child_die->tag == DW_TAG_template_value_param
16158 || child_die->tag == DW_TAG_template_type_param)
16159 {
16160 /* Do nothing. */
16161 }
16162 else
16163 process_die (child_die, cu);
16164
16165 child_die = child_die->sibling;
16166 }
16167
16168 /* Do not consider external references. According to the DWARF standard,
16169 these DIEs are identified by the fact that they have no byte_size
16170 attribute, and a declaration attribute. */
16171 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16172 || !die_is_declaration (die, cu)
16173 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16174 {
16175 struct symbol *sym = new_symbol (die, type, cu);
16176
16177 if (has_template_parameters)
16178 {
16179 struct symtab *symtab;
16180 if (sym != nullptr)
16181 symtab = symbol_symtab (sym);
16182 else if (cu->line_header != nullptr)
16183 {
16184 /* Any related symtab will do. */
16185 symtab
16186 = cu->line_header->file_names ()[0].symtab;
16187 }
16188 else
16189 {
16190 symtab = nullptr;
16191 complaint (_("could not find suitable "
16192 "symtab for template parameter"
16193 " - DIE at %s [in module %s]"),
16194 sect_offset_str (die->sect_off),
16195 objfile_name (objfile));
16196 }
16197
16198 if (symtab != nullptr)
16199 {
16200 /* Make sure that the symtab is set on the new symbols.
16201 Even though they don't appear in this symtab directly,
16202 other parts of gdb assume that symbols do, and this is
16203 reasonably true. */
16204 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16205 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16206 }
16207 }
16208 }
16209 }
16210
16211 /* Assuming DIE is an enumeration type, and TYPE is its associated
16212 type, update TYPE using some information only available in DIE's
16213 children. In particular, the fields are computed. */
16214
16215 static void
16216 update_enumeration_type_from_children (struct die_info *die,
16217 struct type *type,
16218 struct dwarf2_cu *cu)
16219 {
16220 struct die_info *child_die;
16221 int unsigned_enum = 1;
16222 int flag_enum = 1;
16223
16224 auto_obstack obstack;
16225 std::vector<struct field> fields;
16226
16227 for (child_die = die->child;
16228 child_die != NULL && child_die->tag;
16229 child_die = child_die->sibling)
16230 {
16231 struct attribute *attr;
16232 LONGEST value;
16233 const gdb_byte *bytes;
16234 struct dwarf2_locexpr_baton *baton;
16235 const char *name;
16236
16237 if (child_die->tag != DW_TAG_enumerator)
16238 continue;
16239
16240 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16241 if (attr == NULL)
16242 continue;
16243
16244 name = dwarf2_name (child_die, cu);
16245 if (name == NULL)
16246 name = "<anonymous enumerator>";
16247
16248 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16249 &value, &bytes, &baton);
16250 if (value < 0)
16251 {
16252 unsigned_enum = 0;
16253 flag_enum = 0;
16254 }
16255 else
16256 {
16257 if (count_one_bits_ll (value) >= 2)
16258 flag_enum = 0;
16259 }
16260
16261 fields.emplace_back ();
16262 struct field &field = fields.back ();
16263 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16264 SET_FIELD_ENUMVAL (field, value);
16265 }
16266
16267 if (!fields.empty ())
16268 {
16269 type->set_num_fields (fields.size ());
16270 type->set_fields
16271 ((struct field *)
16272 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16273 memcpy (type->fields (), fields.data (),
16274 sizeof (struct field) * fields.size ());
16275 }
16276
16277 if (unsigned_enum)
16278 type->set_is_unsigned (true);
16279
16280 if (flag_enum)
16281 type->set_is_flag_enum (true);
16282 }
16283
16284 /* Given a DW_AT_enumeration_type die, set its type. We do not
16285 complete the type's fields yet, or create any symbols. */
16286
16287 static struct type *
16288 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16289 {
16290 struct objfile *objfile = cu->per_objfile->objfile;
16291 struct type *type;
16292 struct attribute *attr;
16293 const char *name;
16294
16295 /* If the definition of this type lives in .debug_types, read that type.
16296 Don't follow DW_AT_specification though, that will take us back up
16297 the chain and we want to go down. */
16298 attr = die->attr (DW_AT_signature);
16299 if (attr != nullptr)
16300 {
16301 type = get_DW_AT_signature_type (die, attr, cu);
16302
16303 /* The type's CU may not be the same as CU.
16304 Ensure TYPE is recorded with CU in die_type_hash. */
16305 return set_die_type (die, type, cu);
16306 }
16307
16308 type = alloc_type (objfile);
16309
16310 type->set_code (TYPE_CODE_ENUM);
16311 name = dwarf2_full_name (NULL, die, cu);
16312 if (name != NULL)
16313 type->set_name (name);
16314
16315 attr = dwarf2_attr (die, DW_AT_type, cu);
16316 if (attr != NULL)
16317 {
16318 struct type *underlying_type = die_type (die, cu);
16319
16320 TYPE_TARGET_TYPE (type) = underlying_type;
16321 }
16322
16323 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16324 if (attr != nullptr)
16325 {
16326 TYPE_LENGTH (type) = attr->constant_value (0);
16327 }
16328 else
16329 {
16330 TYPE_LENGTH (type) = 0;
16331 }
16332
16333 maybe_set_alignment (cu, die, type);
16334
16335 /* The enumeration DIE can be incomplete. In Ada, any type can be
16336 declared as private in the package spec, and then defined only
16337 inside the package body. Such types are known as Taft Amendment
16338 Types. When another package uses such a type, an incomplete DIE
16339 may be generated by the compiler. */
16340 if (die_is_declaration (die, cu))
16341 type->set_is_stub (true);
16342
16343 /* If this type has an underlying type that is not a stub, then we
16344 may use its attributes. We always use the "unsigned" attribute
16345 in this situation, because ordinarily we guess whether the type
16346 is unsigned -- but the guess can be wrong and the underlying type
16347 can tell us the reality. However, we defer to a local size
16348 attribute if one exists, because this lets the compiler override
16349 the underlying type if needed. */
16350 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16351 {
16352 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16353 underlying_type = check_typedef (underlying_type);
16354
16355 type->set_is_unsigned (underlying_type->is_unsigned ());
16356
16357 if (TYPE_LENGTH (type) == 0)
16358 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16359
16360 if (TYPE_RAW_ALIGN (type) == 0
16361 && TYPE_RAW_ALIGN (underlying_type) != 0)
16362 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16363 }
16364
16365 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
16366
16367 set_die_type (die, type, cu);
16368
16369 /* Finish the creation of this type by using the enum's children.
16370 Note that, as usual, this must come after set_die_type to avoid
16371 infinite recursion when trying to compute the names of the
16372 enumerators. */
16373 update_enumeration_type_from_children (die, type, cu);
16374
16375 return type;
16376 }
16377
16378 /* Given a pointer to a die which begins an enumeration, process all
16379 the dies that define the members of the enumeration, and create the
16380 symbol for the enumeration type.
16381
16382 NOTE: We reverse the order of the element list. */
16383
16384 static void
16385 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16386 {
16387 struct type *this_type;
16388
16389 this_type = get_die_type (die, cu);
16390 if (this_type == NULL)
16391 this_type = read_enumeration_type (die, cu);
16392
16393 if (die->child != NULL)
16394 {
16395 struct die_info *child_die;
16396 const char *name;
16397
16398 child_die = die->child;
16399 while (child_die && child_die->tag)
16400 {
16401 if (child_die->tag != DW_TAG_enumerator)
16402 {
16403 process_die (child_die, cu);
16404 }
16405 else
16406 {
16407 name = dwarf2_name (child_die, cu);
16408 if (name)
16409 new_symbol (child_die, this_type, cu);
16410 }
16411
16412 child_die = child_die->sibling;
16413 }
16414 }
16415
16416 /* If we are reading an enum from a .debug_types unit, and the enum
16417 is a declaration, and the enum is not the signatured type in the
16418 unit, then we do not want to add a symbol for it. Adding a
16419 symbol would in some cases obscure the true definition of the
16420 enum, giving users an incomplete type when the definition is
16421 actually available. Note that we do not want to do this for all
16422 enums which are just declarations, because C++0x allows forward
16423 enum declarations. */
16424 if (cu->per_cu->is_debug_types
16425 && die_is_declaration (die, cu))
16426 {
16427 struct signatured_type *sig_type;
16428
16429 sig_type = (struct signatured_type *) cu->per_cu;
16430 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16431 if (sig_type->type_offset_in_section != die->sect_off)
16432 return;
16433 }
16434
16435 new_symbol (die, this_type, cu);
16436 }
16437
16438 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16439 expression for an index type and finds the corresponding field
16440 offset in the hidden "P_BOUNDS" structure. Returns true on success
16441 and updates *FIELD, false if it fails to recognize an
16442 expression. */
16443
16444 static bool
16445 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16446 int *bounds_offset, struct field *field,
16447 struct dwarf2_cu *cu)
16448 {
16449 struct attribute *attr = dwarf2_attr (die, name, cu);
16450 if (attr == nullptr || !attr->form_is_block ())
16451 return false;
16452
16453 const struct dwarf_block *block = attr->as_block ();
16454 const gdb_byte *start = block->data;
16455 const gdb_byte *end = block->data + block->size;
16456
16457 /* The expression to recognize generally looks like:
16458
16459 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16460 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16461
16462 However, the second "plus_uconst" may be missing:
16463
16464 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16465 DW_OP_deref_size: 4)
16466
16467 This happens when the field is at the start of the structure.
16468
16469 Also, the final deref may not be sized:
16470
16471 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16472 DW_OP_deref)
16473
16474 This happens when the size of the index type happens to be the
16475 same as the architecture's word size. This can occur with or
16476 without the second plus_uconst. */
16477
16478 if (end - start < 2)
16479 return false;
16480 if (*start++ != DW_OP_push_object_address)
16481 return false;
16482 if (*start++ != DW_OP_plus_uconst)
16483 return false;
16484
16485 uint64_t this_bound_off;
16486 start = gdb_read_uleb128 (start, end, &this_bound_off);
16487 if (start == nullptr || (int) this_bound_off != this_bound_off)
16488 return false;
16489 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16490 is consistent among all bounds. */
16491 if (*bounds_offset == -1)
16492 *bounds_offset = this_bound_off;
16493 else if (*bounds_offset != this_bound_off)
16494 return false;
16495
16496 if (start == end || *start++ != DW_OP_deref)
16497 return false;
16498
16499 int offset = 0;
16500 if (start ==end)
16501 return false;
16502 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16503 {
16504 /* This means an offset of 0. */
16505 }
16506 else if (*start++ != DW_OP_plus_uconst)
16507 return false;
16508 else
16509 {
16510 /* The size is the parameter to DW_OP_plus_uconst. */
16511 uint64_t val;
16512 start = gdb_read_uleb128 (start, end, &val);
16513 if (start == nullptr)
16514 return false;
16515 if ((int) val != val)
16516 return false;
16517 offset = val;
16518 }
16519
16520 if (start == end)
16521 return false;
16522
16523 uint64_t size;
16524 if (*start == DW_OP_deref_size)
16525 {
16526 start = gdb_read_uleb128 (start + 1, end, &size);
16527 if (start == nullptr)
16528 return false;
16529 }
16530 else if (*start == DW_OP_deref)
16531 {
16532 size = cu->header.addr_size;
16533 ++start;
16534 }
16535 else
16536 return false;
16537
16538 SET_FIELD_BITPOS (*field, 8 * offset);
16539 if (size != TYPE_LENGTH (field->type ()))
16540 FIELD_BITSIZE (*field) = 8 * size;
16541
16542 return true;
16543 }
16544
16545 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16546 some kinds of Ada arrays:
16547
16548 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16549 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16550 <11e0> DW_AT_data_location: 2 byte block: 97 6
16551 (DW_OP_push_object_address; DW_OP_deref)
16552 <11e3> DW_AT_type : <0x1173>
16553 <11e7> DW_AT_sibling : <0x1201>
16554 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16555 <11ec> DW_AT_type : <0x1206>
16556 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16557 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16558 DW_OP_deref_size: 4)
16559 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16560 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16561 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16562
16563 This actually represents a "thick pointer", which is a structure
16564 with two elements: one that is a pointer to the array data, and one
16565 that is a pointer to another structure; this second structure holds
16566 the array bounds.
16567
16568 This returns a new type on success, or nullptr if this didn't
16569 recognize the type. */
16570
16571 static struct type *
16572 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16573 struct type *type)
16574 {
16575 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16576 /* So far we've only seen this with block form. */
16577 if (attr == nullptr || !attr->form_is_block ())
16578 return nullptr;
16579
16580 /* Note that this will fail if the structure layout is changed by
16581 the compiler. However, we have no good way to recognize some
16582 other layout, because we don't know what expression the compiler
16583 might choose to emit should this happen. */
16584 struct dwarf_block *blk = attr->as_block ();
16585 if (blk->size != 2
16586 || blk->data[0] != DW_OP_push_object_address
16587 || blk->data[1] != DW_OP_deref)
16588 return nullptr;
16589
16590 int bounds_offset = -1;
16591 int max_align = -1;
16592 std::vector<struct field> range_fields;
16593 for (struct die_info *child_die = die->child;
16594 child_die;
16595 child_die = child_die->sibling)
16596 {
16597 if (child_die->tag == DW_TAG_subrange_type)
16598 {
16599 struct type *underlying = read_subrange_index_type (child_die, cu);
16600
16601 int this_align = type_align (underlying);
16602 if (this_align > max_align)
16603 max_align = this_align;
16604
16605 range_fields.emplace_back ();
16606 range_fields.emplace_back ();
16607
16608 struct field &lower = range_fields[range_fields.size () - 2];
16609 struct field &upper = range_fields[range_fields.size () - 1];
16610
16611 lower.set_type (underlying);
16612 FIELD_ARTIFICIAL (lower) = 1;
16613
16614 upper.set_type (underlying);
16615 FIELD_ARTIFICIAL (upper) = 1;
16616
16617 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16618 &bounds_offset, &lower, cu)
16619 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16620 &bounds_offset, &upper, cu))
16621 return nullptr;
16622 }
16623 }
16624
16625 /* This shouldn't really happen, but double-check that we found
16626 where the bounds are stored. */
16627 if (bounds_offset == -1)
16628 return nullptr;
16629
16630 struct objfile *objfile = cu->per_objfile->objfile;
16631 for (int i = 0; i < range_fields.size (); i += 2)
16632 {
16633 char name[20];
16634
16635 /* Set the name of each field in the bounds. */
16636 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16637 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16638 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16639 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16640 }
16641
16642 struct type *bounds = alloc_type (objfile);
16643 bounds->set_code (TYPE_CODE_STRUCT);
16644
16645 bounds->set_num_fields (range_fields.size ());
16646 bounds->set_fields
16647 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16648 * sizeof (struct field))));
16649 memcpy (bounds->fields (), range_fields.data (),
16650 bounds->num_fields () * sizeof (struct field));
16651
16652 int last_fieldno = range_fields.size () - 1;
16653 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16654 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16655 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16656
16657 /* Rewrite the existing array type in place. Specifically, we
16658 remove any dynamic properties we might have read, and we replace
16659 the index types. */
16660 struct type *iter = type;
16661 for (int i = 0; i < range_fields.size (); i += 2)
16662 {
16663 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16664 iter->main_type->dyn_prop_list = nullptr;
16665 iter->set_index_type
16666 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16667 iter = TYPE_TARGET_TYPE (iter);
16668 }
16669
16670 struct type *result = alloc_type (objfile);
16671 result->set_code (TYPE_CODE_STRUCT);
16672
16673 result->set_num_fields (2);
16674 result->set_fields
16675 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16676 * sizeof (struct field))));
16677
16678 /* The names are chosen to coincide with what the compiler does with
16679 -fgnat-encodings=all, which the Ada code in gdb already
16680 understands. */
16681 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16682 result->field (0).set_type (lookup_pointer_type (type));
16683
16684 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
16685 result->field (1).set_type (lookup_pointer_type (bounds));
16686 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
16687
16688 result->set_name (type->name ());
16689 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16690 + TYPE_LENGTH (result->field (1).type ()));
16691
16692 return result;
16693 }
16694
16695 /* Extract all information from a DW_TAG_array_type DIE and put it in
16696 the DIE's type field. For now, this only handles one dimensional
16697 arrays. */
16698
16699 static struct type *
16700 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16701 {
16702 struct objfile *objfile = cu->per_objfile->objfile;
16703 struct die_info *child_die;
16704 struct type *type;
16705 struct type *element_type, *range_type, *index_type;
16706 struct attribute *attr;
16707 const char *name;
16708 struct dynamic_prop *byte_stride_prop = NULL;
16709 unsigned int bit_stride = 0;
16710
16711 element_type = die_type (die, cu);
16712
16713 /* The die_type call above may have already set the type for this DIE. */
16714 type = get_die_type (die, cu);
16715 if (type)
16716 return type;
16717
16718 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16719 if (attr != NULL)
16720 {
16721 int stride_ok;
16722 struct type *prop_type = cu->addr_sized_int_type (false);
16723
16724 byte_stride_prop
16725 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16726 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16727 prop_type);
16728 if (!stride_ok)
16729 {
16730 complaint (_("unable to read array DW_AT_byte_stride "
16731 " - DIE at %s [in module %s]"),
16732 sect_offset_str (die->sect_off),
16733 objfile_name (cu->per_objfile->objfile));
16734 /* Ignore this attribute. We will likely not be able to print
16735 arrays of this type correctly, but there is little we can do
16736 to help if we cannot read the attribute's value. */
16737 byte_stride_prop = NULL;
16738 }
16739 }
16740
16741 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16742 if (attr != NULL)
16743 bit_stride = attr->constant_value (0);
16744
16745 /* Irix 6.2 native cc creates array types without children for
16746 arrays with unspecified length. */
16747 if (die->child == NULL)
16748 {
16749 index_type = objfile_type (objfile)->builtin_int;
16750 range_type = create_static_range_type (NULL, index_type, 0, -1);
16751 type = create_array_type_with_stride (NULL, element_type, range_type,
16752 byte_stride_prop, bit_stride);
16753 return set_die_type (die, type, cu);
16754 }
16755
16756 std::vector<struct type *> range_types;
16757 child_die = die->child;
16758 while (child_die && child_die->tag)
16759 {
16760 if (child_die->tag == DW_TAG_subrange_type)
16761 {
16762 struct type *child_type = read_type_die (child_die, cu);
16763
16764 if (child_type != NULL)
16765 {
16766 /* The range type was succesfully read. Save it for the
16767 array type creation. */
16768 range_types.push_back (child_type);
16769 }
16770 }
16771 child_die = child_die->sibling;
16772 }
16773
16774 if (range_types.empty ())
16775 {
16776 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16777 sect_offset_str (die->sect_off),
16778 objfile_name (cu->per_objfile->objfile));
16779 return NULL;
16780 }
16781
16782 /* Dwarf2 dimensions are output from left to right, create the
16783 necessary array types in backwards order. */
16784
16785 type = element_type;
16786
16787 if (read_array_order (die, cu) == DW_ORD_col_major)
16788 {
16789 int i = 0;
16790
16791 while (i < range_types.size ())
16792 {
16793 type = create_array_type_with_stride (NULL, type, range_types[i++],
16794 byte_stride_prop, bit_stride);
16795 bit_stride = 0;
16796 byte_stride_prop = nullptr;
16797 }
16798 }
16799 else
16800 {
16801 size_t ndim = range_types.size ();
16802 while (ndim-- > 0)
16803 {
16804 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16805 byte_stride_prop, bit_stride);
16806 bit_stride = 0;
16807 byte_stride_prop = nullptr;
16808 }
16809 }
16810
16811 gdb_assert (type != element_type);
16812
16813 /* Understand Dwarf2 support for vector types (like they occur on
16814 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16815 array type. This is not part of the Dwarf2/3 standard yet, but a
16816 custom vendor extension. The main difference between a regular
16817 array and the vector variant is that vectors are passed by value
16818 to functions. */
16819 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16820 if (attr != nullptr)
16821 make_vector_type (type);
16822
16823 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16824 implementation may choose to implement triple vectors using this
16825 attribute. */
16826 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16827 if (attr != nullptr && attr->form_is_unsigned ())
16828 {
16829 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16830 TYPE_LENGTH (type) = attr->as_unsigned ();
16831 else
16832 complaint (_("DW_AT_byte_size for array type smaller "
16833 "than the total size of elements"));
16834 }
16835
16836 name = dwarf2_name (die, cu);
16837 if (name)
16838 type->set_name (name);
16839
16840 maybe_set_alignment (cu, die, type);
16841
16842 struct type *replacement_type = nullptr;
16843 if (cu->language == language_ada)
16844 {
16845 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16846 if (replacement_type != nullptr)
16847 type = replacement_type;
16848 }
16849
16850 /* Install the type in the die. */
16851 set_die_type (die, type, cu, replacement_type != nullptr);
16852
16853 /* set_die_type should be already done. */
16854 set_descriptive_type (type, die, cu);
16855
16856 return type;
16857 }
16858
16859 static enum dwarf_array_dim_ordering
16860 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16861 {
16862 struct attribute *attr;
16863
16864 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16865
16866 if (attr != nullptr)
16867 {
16868 LONGEST val = attr->constant_value (-1);
16869 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16870 return (enum dwarf_array_dim_ordering) val;
16871 }
16872
16873 /* GNU F77 is a special case, as at 08/2004 array type info is the
16874 opposite order to the dwarf2 specification, but data is still
16875 laid out as per normal fortran.
16876
16877 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16878 version checking. */
16879
16880 if (cu->language == language_fortran
16881 && cu->producer && strstr (cu->producer, "GNU F77"))
16882 {
16883 return DW_ORD_row_major;
16884 }
16885
16886 switch (cu->language_defn->array_ordering ())
16887 {
16888 case array_column_major:
16889 return DW_ORD_col_major;
16890 case array_row_major:
16891 default:
16892 return DW_ORD_row_major;
16893 };
16894 }
16895
16896 /* Extract all information from a DW_TAG_set_type DIE and put it in
16897 the DIE's type field. */
16898
16899 static struct type *
16900 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16901 {
16902 struct type *domain_type, *set_type;
16903 struct attribute *attr;
16904
16905 domain_type = die_type (die, cu);
16906
16907 /* The die_type call above may have already set the type for this DIE. */
16908 set_type = get_die_type (die, cu);
16909 if (set_type)
16910 return set_type;
16911
16912 set_type = create_set_type (NULL, domain_type);
16913
16914 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16915 if (attr != nullptr && attr->form_is_unsigned ())
16916 TYPE_LENGTH (set_type) = attr->as_unsigned ();
16917
16918 maybe_set_alignment (cu, die, set_type);
16919
16920 return set_die_type (die, set_type, cu);
16921 }
16922
16923 /* A helper for read_common_block that creates a locexpr baton.
16924 SYM is the symbol which we are marking as computed.
16925 COMMON_DIE is the DIE for the common block.
16926 COMMON_LOC is the location expression attribute for the common
16927 block itself.
16928 MEMBER_LOC is the location expression attribute for the particular
16929 member of the common block that we are processing.
16930 CU is the CU from which the above come. */
16931
16932 static void
16933 mark_common_block_symbol_computed (struct symbol *sym,
16934 struct die_info *common_die,
16935 struct attribute *common_loc,
16936 struct attribute *member_loc,
16937 struct dwarf2_cu *cu)
16938 {
16939 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16940 struct objfile *objfile = per_objfile->objfile;
16941 struct dwarf2_locexpr_baton *baton;
16942 gdb_byte *ptr;
16943 unsigned int cu_off;
16944 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16945 LONGEST offset = 0;
16946
16947 gdb_assert (common_loc && member_loc);
16948 gdb_assert (common_loc->form_is_block ());
16949 gdb_assert (member_loc->form_is_block ()
16950 || member_loc->form_is_constant ());
16951
16952 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16953 baton->per_objfile = per_objfile;
16954 baton->per_cu = cu->per_cu;
16955 gdb_assert (baton->per_cu);
16956
16957 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16958
16959 if (member_loc->form_is_constant ())
16960 {
16961 offset = member_loc->constant_value (0);
16962 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16963 }
16964 else
16965 baton->size += member_loc->as_block ()->size;
16966
16967 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16968 baton->data = ptr;
16969
16970 *ptr++ = DW_OP_call4;
16971 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16972 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16973 ptr += 4;
16974
16975 if (member_loc->form_is_constant ())
16976 {
16977 *ptr++ = DW_OP_addr;
16978 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16979 ptr += cu->header.addr_size;
16980 }
16981 else
16982 {
16983 /* We have to copy the data here, because DW_OP_call4 will only
16984 use a DW_AT_location attribute. */
16985 struct dwarf_block *block = member_loc->as_block ();
16986 memcpy (ptr, block->data, block->size);
16987 ptr += block->size;
16988 }
16989
16990 *ptr++ = DW_OP_plus;
16991 gdb_assert (ptr - baton->data == baton->size);
16992
16993 SYMBOL_LOCATION_BATON (sym) = baton;
16994 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16995 }
16996
16997 /* Create appropriate locally-scoped variables for all the
16998 DW_TAG_common_block entries. Also create a struct common_block
16999 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17000 is used to separate the common blocks name namespace from regular
17001 variable names. */
17002
17003 static void
17004 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17005 {
17006 struct attribute *attr;
17007
17008 attr = dwarf2_attr (die, DW_AT_location, cu);
17009 if (attr != nullptr)
17010 {
17011 /* Support the .debug_loc offsets. */
17012 if (attr->form_is_block ())
17013 {
17014 /* Ok. */
17015 }
17016 else if (attr->form_is_section_offset ())
17017 {
17018 dwarf2_complex_location_expr_complaint ();
17019 attr = NULL;
17020 }
17021 else
17022 {
17023 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17024 "common block member");
17025 attr = NULL;
17026 }
17027 }
17028
17029 if (die->child != NULL)
17030 {
17031 struct objfile *objfile = cu->per_objfile->objfile;
17032 struct die_info *child_die;
17033 size_t n_entries = 0, size;
17034 struct common_block *common_block;
17035 struct symbol *sym;
17036
17037 for (child_die = die->child;
17038 child_die && child_die->tag;
17039 child_die = child_die->sibling)
17040 ++n_entries;
17041
17042 size = (sizeof (struct common_block)
17043 + (n_entries - 1) * sizeof (struct symbol *));
17044 common_block
17045 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17046 size);
17047 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17048 common_block->n_entries = 0;
17049
17050 for (child_die = die->child;
17051 child_die && child_die->tag;
17052 child_die = child_die->sibling)
17053 {
17054 /* Create the symbol in the DW_TAG_common_block block in the current
17055 symbol scope. */
17056 sym = new_symbol (child_die, NULL, cu);
17057 if (sym != NULL)
17058 {
17059 struct attribute *member_loc;
17060
17061 common_block->contents[common_block->n_entries++] = sym;
17062
17063 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17064 cu);
17065 if (member_loc)
17066 {
17067 /* GDB has handled this for a long time, but it is
17068 not specified by DWARF. It seems to have been
17069 emitted by gfortran at least as recently as:
17070 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17071 complaint (_("Variable in common block has "
17072 "DW_AT_data_member_location "
17073 "- DIE at %s [in module %s]"),
17074 sect_offset_str (child_die->sect_off),
17075 objfile_name (objfile));
17076
17077 if (member_loc->form_is_section_offset ())
17078 dwarf2_complex_location_expr_complaint ();
17079 else if (member_loc->form_is_constant ()
17080 || member_loc->form_is_block ())
17081 {
17082 if (attr != nullptr)
17083 mark_common_block_symbol_computed (sym, die, attr,
17084 member_loc, cu);
17085 }
17086 else
17087 dwarf2_complex_location_expr_complaint ();
17088 }
17089 }
17090 }
17091
17092 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17093 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17094 }
17095 }
17096
17097 /* Create a type for a C++ namespace. */
17098
17099 static struct type *
17100 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17101 {
17102 struct objfile *objfile = cu->per_objfile->objfile;
17103 const char *previous_prefix, *name;
17104 int is_anonymous;
17105 struct type *type;
17106
17107 /* For extensions, reuse the type of the original namespace. */
17108 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17109 {
17110 struct die_info *ext_die;
17111 struct dwarf2_cu *ext_cu = cu;
17112
17113 ext_die = dwarf2_extension (die, &ext_cu);
17114 type = read_type_die (ext_die, ext_cu);
17115
17116 /* EXT_CU may not be the same as CU.
17117 Ensure TYPE is recorded with CU in die_type_hash. */
17118 return set_die_type (die, type, cu);
17119 }
17120
17121 name = namespace_name (die, &is_anonymous, cu);
17122
17123 /* Now build the name of the current namespace. */
17124
17125 previous_prefix = determine_prefix (die, cu);
17126 if (previous_prefix[0] != '\0')
17127 name = typename_concat (&objfile->objfile_obstack,
17128 previous_prefix, name, 0, cu);
17129
17130 /* Create the type. */
17131 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17132
17133 return set_die_type (die, type, cu);
17134 }
17135
17136 /* Read a namespace scope. */
17137
17138 static void
17139 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17140 {
17141 struct objfile *objfile = cu->per_objfile->objfile;
17142 int is_anonymous;
17143
17144 /* Add a symbol associated to this if we haven't seen the namespace
17145 before. Also, add a using directive if it's an anonymous
17146 namespace. */
17147
17148 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17149 {
17150 struct type *type;
17151
17152 type = read_type_die (die, cu);
17153 new_symbol (die, type, cu);
17154
17155 namespace_name (die, &is_anonymous, cu);
17156 if (is_anonymous)
17157 {
17158 const char *previous_prefix = determine_prefix (die, cu);
17159
17160 std::vector<const char *> excludes;
17161 add_using_directive (using_directives (cu),
17162 previous_prefix, type->name (), NULL,
17163 NULL, excludes, 0, &objfile->objfile_obstack);
17164 }
17165 }
17166
17167 if (die->child != NULL)
17168 {
17169 struct die_info *child_die = die->child;
17170
17171 while (child_die && child_die->tag)
17172 {
17173 process_die (child_die, cu);
17174 child_die = child_die->sibling;
17175 }
17176 }
17177 }
17178
17179 /* Read a Fortran module as type. This DIE can be only a declaration used for
17180 imported module. Still we need that type as local Fortran "use ... only"
17181 declaration imports depend on the created type in determine_prefix. */
17182
17183 static struct type *
17184 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17185 {
17186 struct objfile *objfile = cu->per_objfile->objfile;
17187 const char *module_name;
17188 struct type *type;
17189
17190 module_name = dwarf2_name (die, cu);
17191 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17192
17193 return set_die_type (die, type, cu);
17194 }
17195
17196 /* Read a Fortran module. */
17197
17198 static void
17199 read_module (struct die_info *die, struct dwarf2_cu *cu)
17200 {
17201 struct die_info *child_die = die->child;
17202 struct type *type;
17203
17204 type = read_type_die (die, cu);
17205 new_symbol (die, type, cu);
17206
17207 while (child_die && child_die->tag)
17208 {
17209 process_die (child_die, cu);
17210 child_die = child_die->sibling;
17211 }
17212 }
17213
17214 /* Return the name of the namespace represented by DIE. Set
17215 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17216 namespace. */
17217
17218 static const char *
17219 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17220 {
17221 struct die_info *current_die;
17222 const char *name = NULL;
17223
17224 /* Loop through the extensions until we find a name. */
17225
17226 for (current_die = die;
17227 current_die != NULL;
17228 current_die = dwarf2_extension (die, &cu))
17229 {
17230 /* We don't use dwarf2_name here so that we can detect the absence
17231 of a name -> anonymous namespace. */
17232 name = dwarf2_string_attr (die, DW_AT_name, cu);
17233
17234 if (name != NULL)
17235 break;
17236 }
17237
17238 /* Is it an anonymous namespace? */
17239
17240 *is_anonymous = (name == NULL);
17241 if (*is_anonymous)
17242 name = CP_ANONYMOUS_NAMESPACE_STR;
17243
17244 return name;
17245 }
17246
17247 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17248 the user defined type vector. */
17249
17250 static struct type *
17251 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17252 {
17253 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17254 struct comp_unit_head *cu_header = &cu->header;
17255 struct type *type;
17256 struct attribute *attr_byte_size;
17257 struct attribute *attr_address_class;
17258 int byte_size, addr_class;
17259 struct type *target_type;
17260
17261 target_type = die_type (die, cu);
17262
17263 /* The die_type call above may have already set the type for this DIE. */
17264 type = get_die_type (die, cu);
17265 if (type)
17266 return type;
17267
17268 type = lookup_pointer_type (target_type);
17269
17270 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17271 if (attr_byte_size)
17272 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17273 else
17274 byte_size = cu_header->addr_size;
17275
17276 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17277 if (attr_address_class)
17278 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17279 else
17280 addr_class = DW_ADDR_none;
17281
17282 ULONGEST alignment = get_alignment (cu, die);
17283
17284 /* If the pointer size, alignment, or address class is different
17285 than the default, create a type variant marked as such and set
17286 the length accordingly. */
17287 if (TYPE_LENGTH (type) != byte_size
17288 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17289 && alignment != TYPE_RAW_ALIGN (type))
17290 || addr_class != DW_ADDR_none)
17291 {
17292 if (gdbarch_address_class_type_flags_p (gdbarch))
17293 {
17294 type_instance_flags type_flags
17295 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17296 addr_class);
17297 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17298 == 0);
17299 type = make_type_with_address_space (type, type_flags);
17300 }
17301 else if (TYPE_LENGTH (type) != byte_size)
17302 {
17303 complaint (_("invalid pointer size %d"), byte_size);
17304 }
17305 else if (TYPE_RAW_ALIGN (type) != alignment)
17306 {
17307 complaint (_("Invalid DW_AT_alignment"
17308 " - DIE at %s [in module %s]"),
17309 sect_offset_str (die->sect_off),
17310 objfile_name (cu->per_objfile->objfile));
17311 }
17312 else
17313 {
17314 /* Should we also complain about unhandled address classes? */
17315 }
17316 }
17317
17318 TYPE_LENGTH (type) = byte_size;
17319 set_type_align (type, alignment);
17320 return set_die_type (die, type, cu);
17321 }
17322
17323 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17324 the user defined type vector. */
17325
17326 static struct type *
17327 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17328 {
17329 struct type *type;
17330 struct type *to_type;
17331 struct type *domain;
17332
17333 to_type = die_type (die, cu);
17334 domain = die_containing_type (die, cu);
17335
17336 /* The calls above may have already set the type for this DIE. */
17337 type = get_die_type (die, cu);
17338 if (type)
17339 return type;
17340
17341 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17342 type = lookup_methodptr_type (to_type);
17343 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17344 {
17345 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17346
17347 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17348 to_type->fields (), to_type->num_fields (),
17349 to_type->has_varargs ());
17350 type = lookup_methodptr_type (new_type);
17351 }
17352 else
17353 type = lookup_memberptr_type (to_type, domain);
17354
17355 return set_die_type (die, type, cu);
17356 }
17357
17358 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17359 the user defined type vector. */
17360
17361 static struct type *
17362 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17363 enum type_code refcode)
17364 {
17365 struct comp_unit_head *cu_header = &cu->header;
17366 struct type *type, *target_type;
17367 struct attribute *attr;
17368
17369 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17370
17371 target_type = die_type (die, cu);
17372
17373 /* The die_type call above may have already set the type for this DIE. */
17374 type = get_die_type (die, cu);
17375 if (type)
17376 return type;
17377
17378 type = lookup_reference_type (target_type, refcode);
17379 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17380 if (attr != nullptr)
17381 {
17382 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17383 }
17384 else
17385 {
17386 TYPE_LENGTH (type) = cu_header->addr_size;
17387 }
17388 maybe_set_alignment (cu, die, type);
17389 return set_die_type (die, type, cu);
17390 }
17391
17392 /* Add the given cv-qualifiers to the element type of the array. GCC
17393 outputs DWARF type qualifiers that apply to an array, not the
17394 element type. But GDB relies on the array element type to carry
17395 the cv-qualifiers. This mimics section 6.7.3 of the C99
17396 specification. */
17397
17398 static struct type *
17399 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17400 struct type *base_type, int cnst, int voltl)
17401 {
17402 struct type *el_type, *inner_array;
17403
17404 base_type = copy_type (base_type);
17405 inner_array = base_type;
17406
17407 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17408 {
17409 TYPE_TARGET_TYPE (inner_array) =
17410 copy_type (TYPE_TARGET_TYPE (inner_array));
17411 inner_array = TYPE_TARGET_TYPE (inner_array);
17412 }
17413
17414 el_type = TYPE_TARGET_TYPE (inner_array);
17415 cnst |= TYPE_CONST (el_type);
17416 voltl |= TYPE_VOLATILE (el_type);
17417 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17418
17419 return set_die_type (die, base_type, cu);
17420 }
17421
17422 static struct type *
17423 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17424 {
17425 struct type *base_type, *cv_type;
17426
17427 base_type = die_type (die, cu);
17428
17429 /* The die_type call above may have already set the type for this DIE. */
17430 cv_type = get_die_type (die, cu);
17431 if (cv_type)
17432 return cv_type;
17433
17434 /* In case the const qualifier is applied to an array type, the element type
17435 is so qualified, not the array type (section 6.7.3 of C99). */
17436 if (base_type->code () == TYPE_CODE_ARRAY)
17437 return add_array_cv_type (die, cu, base_type, 1, 0);
17438
17439 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17440 return set_die_type (die, cv_type, cu);
17441 }
17442
17443 static struct type *
17444 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17445 {
17446 struct type *base_type, *cv_type;
17447
17448 base_type = die_type (die, cu);
17449
17450 /* The die_type call above may have already set the type for this DIE. */
17451 cv_type = get_die_type (die, cu);
17452 if (cv_type)
17453 return cv_type;
17454
17455 /* In case the volatile qualifier is applied to an array type, the
17456 element type is so qualified, not the array type (section 6.7.3
17457 of C99). */
17458 if (base_type->code () == TYPE_CODE_ARRAY)
17459 return add_array_cv_type (die, cu, base_type, 0, 1);
17460
17461 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17462 return set_die_type (die, cv_type, cu);
17463 }
17464
17465 /* Handle DW_TAG_restrict_type. */
17466
17467 static struct type *
17468 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17469 {
17470 struct type *base_type, *cv_type;
17471
17472 base_type = die_type (die, cu);
17473
17474 /* The die_type call above may have already set the type for this DIE. */
17475 cv_type = get_die_type (die, cu);
17476 if (cv_type)
17477 return cv_type;
17478
17479 cv_type = make_restrict_type (base_type);
17480 return set_die_type (die, cv_type, cu);
17481 }
17482
17483 /* Handle DW_TAG_atomic_type. */
17484
17485 static struct type *
17486 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17487 {
17488 struct type *base_type, *cv_type;
17489
17490 base_type = die_type (die, cu);
17491
17492 /* The die_type call above may have already set the type for this DIE. */
17493 cv_type = get_die_type (die, cu);
17494 if (cv_type)
17495 return cv_type;
17496
17497 cv_type = make_atomic_type (base_type);
17498 return set_die_type (die, cv_type, cu);
17499 }
17500
17501 /* Extract all information from a DW_TAG_string_type DIE and add to
17502 the user defined type vector. It isn't really a user defined type,
17503 but it behaves like one, with other DIE's using an AT_user_def_type
17504 attribute to reference it. */
17505
17506 static struct type *
17507 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17508 {
17509 struct objfile *objfile = cu->per_objfile->objfile;
17510 struct gdbarch *gdbarch = objfile->arch ();
17511 struct type *type, *range_type, *index_type, *char_type;
17512 struct attribute *attr;
17513 struct dynamic_prop prop;
17514 bool length_is_constant = true;
17515 LONGEST length;
17516
17517 /* There are a couple of places where bit sizes might be made use of
17518 when parsing a DW_TAG_string_type, however, no producer that we know
17519 of make use of these. Handling bit sizes that are a multiple of the
17520 byte size is easy enough, but what about other bit sizes? Lets deal
17521 with that problem when we have to. Warn about these attributes being
17522 unsupported, then parse the type and ignore them like we always
17523 have. */
17524 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17525 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17526 {
17527 static bool warning_printed = false;
17528 if (!warning_printed)
17529 {
17530 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17531 "currently supported on DW_TAG_string_type."));
17532 warning_printed = true;
17533 }
17534 }
17535
17536 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17537 if (attr != nullptr && !attr->form_is_constant ())
17538 {
17539 /* The string length describes the location at which the length of
17540 the string can be found. The size of the length field can be
17541 specified with one of the attributes below. */
17542 struct type *prop_type;
17543 struct attribute *len
17544 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17545 if (len == nullptr)
17546 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17547 if (len != nullptr && len->form_is_constant ())
17548 {
17549 /* Pass 0 as the default as we know this attribute is constant
17550 and the default value will not be returned. */
17551 LONGEST sz = len->constant_value (0);
17552 prop_type = cu->per_objfile->int_type (sz, true);
17553 }
17554 else
17555 {
17556 /* If the size is not specified then we assume it is the size of
17557 an address on this target. */
17558 prop_type = cu->addr_sized_int_type (true);
17559 }
17560
17561 /* Convert the attribute into a dynamic property. */
17562 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17563 length = 1;
17564 else
17565 length_is_constant = false;
17566 }
17567 else if (attr != nullptr)
17568 {
17569 /* This DW_AT_string_length just contains the length with no
17570 indirection. There's no need to create a dynamic property in this
17571 case. Pass 0 for the default value as we know it will not be
17572 returned in this case. */
17573 length = attr->constant_value (0);
17574 }
17575 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17576 {
17577 /* We don't currently support non-constant byte sizes for strings. */
17578 length = attr->constant_value (1);
17579 }
17580 else
17581 {
17582 /* Use 1 as a fallback length if we have nothing else. */
17583 length = 1;
17584 }
17585
17586 index_type = objfile_type (objfile)->builtin_int;
17587 if (length_is_constant)
17588 range_type = create_static_range_type (NULL, index_type, 1, length);
17589 else
17590 {
17591 struct dynamic_prop low_bound;
17592
17593 low_bound.set_const_val (1);
17594 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17595 }
17596 char_type = language_string_char_type (cu->language_defn, gdbarch);
17597 type = create_string_type (NULL, char_type, range_type);
17598
17599 return set_die_type (die, type, cu);
17600 }
17601
17602 /* Assuming that DIE corresponds to a function, returns nonzero
17603 if the function is prototyped. */
17604
17605 static int
17606 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17607 {
17608 struct attribute *attr;
17609
17610 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17611 if (attr && attr->as_boolean ())
17612 return 1;
17613
17614 /* The DWARF standard implies that the DW_AT_prototyped attribute
17615 is only meaningful for C, but the concept also extends to other
17616 languages that allow unprototyped functions (Eg: Objective C).
17617 For all other languages, assume that functions are always
17618 prototyped. */
17619 if (cu->language != language_c
17620 && cu->language != language_objc
17621 && cu->language != language_opencl)
17622 return 1;
17623
17624 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17625 prototyped and unprototyped functions; default to prototyped,
17626 since that is more common in modern code (and RealView warns
17627 about unprototyped functions). */
17628 if (producer_is_realview (cu->producer))
17629 return 1;
17630
17631 return 0;
17632 }
17633
17634 /* Handle DIES due to C code like:
17635
17636 struct foo
17637 {
17638 int (*funcp)(int a, long l);
17639 int b;
17640 };
17641
17642 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17643
17644 static struct type *
17645 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17646 {
17647 struct objfile *objfile = cu->per_objfile->objfile;
17648 struct type *type; /* Type that this function returns. */
17649 struct type *ftype; /* Function that returns above type. */
17650 struct attribute *attr;
17651
17652 type = die_type (die, cu);
17653
17654 /* The die_type call above may have already set the type for this DIE. */
17655 ftype = get_die_type (die, cu);
17656 if (ftype)
17657 return ftype;
17658
17659 ftype = lookup_function_type (type);
17660
17661 if (prototyped_function_p (die, cu))
17662 ftype->set_is_prototyped (true);
17663
17664 /* Store the calling convention in the type if it's available in
17665 the subroutine die. Otherwise set the calling convention to
17666 the default value DW_CC_normal. */
17667 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17668 if (attr != nullptr
17669 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
17670 TYPE_CALLING_CONVENTION (ftype)
17671 = (enum dwarf_calling_convention) attr->constant_value (0);
17672 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17673 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17674 else
17675 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17676
17677 /* Record whether the function returns normally to its caller or not
17678 if the DWARF producer set that information. */
17679 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17680 if (attr && attr->as_boolean ())
17681 TYPE_NO_RETURN (ftype) = 1;
17682
17683 /* We need to add the subroutine type to the die immediately so
17684 we don't infinitely recurse when dealing with parameters
17685 declared as the same subroutine type. */
17686 set_die_type (die, ftype, cu);
17687
17688 if (die->child != NULL)
17689 {
17690 struct type *void_type = objfile_type (objfile)->builtin_void;
17691 struct die_info *child_die;
17692 int nparams, iparams;
17693
17694 /* Count the number of parameters.
17695 FIXME: GDB currently ignores vararg functions, but knows about
17696 vararg member functions. */
17697 nparams = 0;
17698 child_die = die->child;
17699 while (child_die && child_die->tag)
17700 {
17701 if (child_die->tag == DW_TAG_formal_parameter)
17702 nparams++;
17703 else if (child_die->tag == DW_TAG_unspecified_parameters)
17704 ftype->set_has_varargs (true);
17705
17706 child_die = child_die->sibling;
17707 }
17708
17709 /* Allocate storage for parameters and fill them in. */
17710 ftype->set_num_fields (nparams);
17711 ftype->set_fields
17712 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17713
17714 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17715 even if we error out during the parameters reading below. */
17716 for (iparams = 0; iparams < nparams; iparams++)
17717 ftype->field (iparams).set_type (void_type);
17718
17719 iparams = 0;
17720 child_die = die->child;
17721 while (child_die && child_die->tag)
17722 {
17723 if (child_die->tag == DW_TAG_formal_parameter)
17724 {
17725 struct type *arg_type;
17726
17727 /* DWARF version 2 has no clean way to discern C++
17728 static and non-static member functions. G++ helps
17729 GDB by marking the first parameter for non-static
17730 member functions (which is the this pointer) as
17731 artificial. We pass this information to
17732 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17733
17734 DWARF version 3 added DW_AT_object_pointer, which GCC
17735 4.5 does not yet generate. */
17736 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17737 if (attr != nullptr)
17738 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
17739 else
17740 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17741 arg_type = die_type (child_die, cu);
17742
17743 /* RealView does not mark THIS as const, which the testsuite
17744 expects. GCC marks THIS as const in method definitions,
17745 but not in the class specifications (GCC PR 43053). */
17746 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17747 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17748 {
17749 int is_this = 0;
17750 struct dwarf2_cu *arg_cu = cu;
17751 const char *name = dwarf2_name (child_die, cu);
17752
17753 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17754 if (attr != nullptr)
17755 {
17756 /* If the compiler emits this, use it. */
17757 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17758 is_this = 1;
17759 }
17760 else if (name && strcmp (name, "this") == 0)
17761 /* Function definitions will have the argument names. */
17762 is_this = 1;
17763 else if (name == NULL && iparams == 0)
17764 /* Declarations may not have the names, so like
17765 elsewhere in GDB, assume an artificial first
17766 argument is "this". */
17767 is_this = 1;
17768
17769 if (is_this)
17770 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17771 arg_type, 0);
17772 }
17773
17774 ftype->field (iparams).set_type (arg_type);
17775 iparams++;
17776 }
17777 child_die = child_die->sibling;
17778 }
17779 }
17780
17781 return ftype;
17782 }
17783
17784 static struct type *
17785 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17786 {
17787 struct objfile *objfile = cu->per_objfile->objfile;
17788 const char *name = NULL;
17789 struct type *this_type, *target_type;
17790
17791 name = dwarf2_full_name (NULL, die, cu);
17792 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17793 this_type->set_target_is_stub (true);
17794 set_die_type (die, this_type, cu);
17795 target_type = die_type (die, cu);
17796 if (target_type != this_type)
17797 TYPE_TARGET_TYPE (this_type) = target_type;
17798 else
17799 {
17800 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17801 spec and cause infinite loops in GDB. */
17802 complaint (_("Self-referential DW_TAG_typedef "
17803 "- DIE at %s [in module %s]"),
17804 sect_offset_str (die->sect_off), objfile_name (objfile));
17805 TYPE_TARGET_TYPE (this_type) = NULL;
17806 }
17807 if (name == NULL)
17808 {
17809 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17810 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17811 Handle these by just returning the target type, rather than
17812 constructing an anonymous typedef type and trying to handle this
17813 elsewhere. */
17814 set_die_type (die, target_type, cu);
17815 return target_type;
17816 }
17817 return this_type;
17818 }
17819
17820 /* Helper for get_dwarf2_rational_constant that computes the value of
17821 a given gmp_mpz given an attribute. */
17822
17823 static void
17824 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17825 {
17826 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17827 location expression that pushes an implicit value. */
17828 if (attr->form == DW_FORM_exprloc)
17829 {
17830 dwarf_block *blk = attr->as_block ();
17831 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17832 {
17833 uint64_t len;
17834 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17835 blk->data + blk->size,
17836 &len);
17837 if (ptr - blk->data + len <= blk->size)
17838 {
17839 mpz_import (value->val, len,
17840 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17841 1, 0, 0, ptr);
17842 return;
17843 }
17844 }
17845
17846 /* On failure set it to 1. */
17847 *value = gdb_mpz (1);
17848 }
17849 else if (attr->form_is_block ())
17850 {
17851 dwarf_block *blk = attr->as_block ();
17852 mpz_import (value->val, blk->size,
17853 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17854 1, 0, 0, blk->data);
17855 }
17856 else
17857 *value = gdb_mpz (attr->constant_value (1));
17858 }
17859
17860 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
17861 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
17862
17863 If the numerator and/or numerator attribute is missing,
17864 a complaint is filed, and NUMERATOR and DENOMINATOR are left
17865 untouched. */
17866
17867 static void
17868 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
17869 gdb_mpz *numerator, gdb_mpz *denominator)
17870 {
17871 struct attribute *num_attr, *denom_attr;
17872
17873 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
17874 if (num_attr == nullptr)
17875 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
17876 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17877
17878 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
17879 if (denom_attr == nullptr)
17880 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
17881 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17882
17883 if (num_attr == nullptr || denom_attr == nullptr)
17884 return;
17885
17886 get_mpz (cu, numerator, num_attr);
17887 get_mpz (cu, denominator, denom_attr);
17888 }
17889
17890 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
17891 rational constant, rather than a signed one.
17892
17893 If the rational constant has a negative value, a complaint
17894 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
17895
17896 static void
17897 get_dwarf2_unsigned_rational_constant (struct die_info *die,
17898 struct dwarf2_cu *cu,
17899 gdb_mpz *numerator,
17900 gdb_mpz *denominator)
17901 {
17902 gdb_mpz num (1);
17903 gdb_mpz denom (1);
17904
17905 get_dwarf2_rational_constant (die, cu, &num, &denom);
17906 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
17907 {
17908 mpz_neg (num.val, num.val);
17909 mpz_neg (denom.val, denom.val);
17910 }
17911 else if (mpz_sgn (num.val) == -1)
17912 {
17913 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
17914 " in DIE at %s"),
17915 sect_offset_str (die->sect_off));
17916 return;
17917 }
17918 else if (mpz_sgn (denom.val) == -1)
17919 {
17920 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
17921 " in DIE at %s"),
17922 sect_offset_str (die->sect_off));
17923 return;
17924 }
17925
17926 *numerator = std::move (num);
17927 *denominator = std::move (denom);
17928 }
17929
17930 /* Assuming that ENCODING is a string whose contents starting at the
17931 K'th character is "_nn" where "nn" is a decimal number, scan that
17932 number and set RESULT to the value. K is updated to point to the
17933 character immediately following the number.
17934
17935 If the string does not conform to the format described above, false
17936 is returned, and K may or may not be changed. */
17937
17938 static bool
17939 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
17940 {
17941 /* The next character should be an underscore ('_') followed
17942 by a digit. */
17943 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
17944 return false;
17945
17946 /* Skip the underscore. */
17947 k++;
17948 int start = k;
17949
17950 /* Determine the number of digits for our number. */
17951 while (isdigit (encoding[k]))
17952 k++;
17953 if (k == start)
17954 return false;
17955
17956 std::string copy (&encoding[start], k - start);
17957 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
17958 return false;
17959
17960 return true;
17961 }
17962
17963 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
17964 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
17965 DENOM, update OFFSET, and return true on success. Return false on
17966 failure. */
17967
17968 static bool
17969 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
17970 gdb_mpz *num, gdb_mpz *denom)
17971 {
17972 if (!ada_get_gnat_encoded_number (encoding, offset, num))
17973 return false;
17974 return ada_get_gnat_encoded_number (encoding, offset, denom);
17975 }
17976
17977 /* Assuming DIE corresponds to a fixed point type, finish the creation
17978 of the corresponding TYPE by setting its type-specific data. CU is
17979 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
17980 encodings. It is nullptr if the GNAT encoding should be
17981 ignored. */
17982
17983 static void
17984 finish_fixed_point_type (struct type *type, const char *suffix,
17985 struct die_info *die, struct dwarf2_cu *cu)
17986 {
17987 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
17988 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
17989
17990 /* If GNAT encodings are preferred, don't examine the
17991 attributes. */
17992 struct attribute *attr = nullptr;
17993 if (suffix == nullptr)
17994 {
17995 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
17996 if (attr == nullptr)
17997 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
17998 if (attr == nullptr)
17999 attr = dwarf2_attr (die, DW_AT_small, cu);
18000 }
18001
18002 /* Numerator and denominator of our fixed-point type's scaling factor.
18003 The default is a scaling factor of 1, which we use as a fallback
18004 when we are not able to decode it (problem with the debugging info,
18005 unsupported forms, bug in GDB, etc...). Using that as the default
18006 allows us to at least print the unscaled value, which might still
18007 be useful to a user. */
18008 gdb_mpz scale_num (1);
18009 gdb_mpz scale_denom (1);
18010
18011 if (attr == nullptr)
18012 {
18013 int offset = 0;
18014 if (suffix != nullptr
18015 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18016 &scale_denom)
18017 /* The number might be encoded as _nn_dd_nn_dd, where the
18018 second ratio is the 'small value. In this situation, we
18019 want the second value. */
18020 && (suffix[offset] != '_'
18021 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18022 &scale_denom)))
18023 {
18024 /* Found it. */
18025 }
18026 else
18027 {
18028 /* Scaling factor not found. Assume a scaling factor of 1,
18029 and hope for the best. At least the user will be able to
18030 see the encoded value. */
18031 scale_num = 1;
18032 scale_denom = 1;
18033 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18034 sect_offset_str (die->sect_off));
18035 }
18036 }
18037 else if (attr->name == DW_AT_binary_scale)
18038 {
18039 LONGEST scale_exp = attr->constant_value (0);
18040 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18041
18042 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
18043 }
18044 else if (attr->name == DW_AT_decimal_scale)
18045 {
18046 LONGEST scale_exp = attr->constant_value (0);
18047 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18048
18049 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
18050 }
18051 else if (attr->name == DW_AT_small)
18052 {
18053 struct die_info *scale_die;
18054 struct dwarf2_cu *scale_cu = cu;
18055
18056 scale_die = follow_die_ref (die, attr, &scale_cu);
18057 if (scale_die->tag == DW_TAG_constant)
18058 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18059 &scale_num, &scale_denom);
18060 else
18061 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18062 " (DIE at %s)"),
18063 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18064 }
18065 else
18066 {
18067 complaint (_("unsupported scale attribute %s for fixed-point type"
18068 " (DIE at %s)"),
18069 dwarf_attr_name (attr->name),
18070 sect_offset_str (die->sect_off));
18071 }
18072
18073 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
18074 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18075 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
18076 mpq_canonicalize (scaling_factor.val);
18077 }
18078
18079 /* The gnat-encoding suffix for fixed point. */
18080
18081 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
18082
18083 /* If NAME encodes an Ada fixed-point type, return a pointer to the
18084 "XF" suffix of the name. The text after this is what encodes the
18085 'small and 'delta information. Otherwise, return nullptr. */
18086
18087 static const char *
18088 gnat_encoded_fixed_point_type_info (const char *name)
18089 {
18090 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18091 }
18092
18093 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18094 (which may be different from NAME) to the architecture back-end to allow
18095 it to guess the correct format if necessary. */
18096
18097 static struct type *
18098 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18099 const char *name_hint, enum bfd_endian byte_order)
18100 {
18101 struct gdbarch *gdbarch = objfile->arch ();
18102 const struct floatformat **format;
18103 struct type *type;
18104
18105 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18106 if (format)
18107 type = init_float_type (objfile, bits, name, format, byte_order);
18108 else
18109 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18110
18111 return type;
18112 }
18113
18114 /* Allocate an integer type of size BITS and name NAME. */
18115
18116 static struct type *
18117 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18118 int bits, int unsigned_p, const char *name)
18119 {
18120 struct type *type;
18121
18122 /* Versions of Intel's C Compiler generate an integer type called "void"
18123 instead of using DW_TAG_unspecified_type. This has been seen on
18124 at least versions 14, 17, and 18. */
18125 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18126 && strcmp (name, "void") == 0)
18127 type = objfile_type (objfile)->builtin_void;
18128 else
18129 type = init_integer_type (objfile, bits, unsigned_p, name);
18130
18131 return type;
18132 }
18133
18134 /* Return true if DIE has a DW_AT_small attribute whose value is
18135 a constant rational, where both the numerator and denominator
18136 are equal to zero.
18137
18138 CU is the DIE's Compilation Unit. */
18139
18140 static bool
18141 has_zero_over_zero_small_attribute (struct die_info *die,
18142 struct dwarf2_cu *cu)
18143 {
18144 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18145 if (attr == nullptr)
18146 return false;
18147
18148 struct dwarf2_cu *scale_cu = cu;
18149 struct die_info *scale_die
18150 = follow_die_ref (die, attr, &scale_cu);
18151
18152 if (scale_die->tag != DW_TAG_constant)
18153 return false;
18154
18155 gdb_mpz num (1), denom (1);
18156 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18157 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18158 }
18159
18160 /* Initialise and return a floating point type of size BITS suitable for
18161 use as a component of a complex number. The NAME_HINT is passed through
18162 when initialising the floating point type and is the name of the complex
18163 type.
18164
18165 As DWARF doesn't currently provide an explicit name for the components
18166 of a complex number, but it can be helpful to have these components
18167 named, we try to select a suitable name based on the size of the
18168 component. */
18169 static struct type *
18170 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18171 struct objfile *objfile,
18172 int bits, const char *name_hint,
18173 enum bfd_endian byte_order)
18174 {
18175 gdbarch *gdbarch = objfile->arch ();
18176 struct type *tt = nullptr;
18177
18178 /* Try to find a suitable floating point builtin type of size BITS.
18179 We're going to use the name of this type as the name for the complex
18180 target type that we are about to create. */
18181 switch (cu->language)
18182 {
18183 case language_fortran:
18184 switch (bits)
18185 {
18186 case 32:
18187 tt = builtin_f_type (gdbarch)->builtin_real;
18188 break;
18189 case 64:
18190 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18191 break;
18192 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18193 case 128:
18194 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18195 break;
18196 }
18197 break;
18198 default:
18199 switch (bits)
18200 {
18201 case 32:
18202 tt = builtin_type (gdbarch)->builtin_float;
18203 break;
18204 case 64:
18205 tt = builtin_type (gdbarch)->builtin_double;
18206 break;
18207 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18208 case 128:
18209 tt = builtin_type (gdbarch)->builtin_long_double;
18210 break;
18211 }
18212 break;
18213 }
18214
18215 /* If the type we found doesn't match the size we were looking for, then
18216 pretend we didn't find a type at all, the complex target type we
18217 create will then be nameless. */
18218 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18219 tt = nullptr;
18220
18221 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18222 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18223 }
18224
18225 /* Find a representation of a given base type and install
18226 it in the TYPE field of the die. */
18227
18228 static struct type *
18229 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18230 {
18231 struct objfile *objfile = cu->per_objfile->objfile;
18232 struct type *type;
18233 struct attribute *attr;
18234 int encoding = 0, bits = 0;
18235 const char *name;
18236 gdbarch *arch;
18237
18238 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18239 if (attr != nullptr && attr->form_is_constant ())
18240 encoding = attr->constant_value (0);
18241 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18242 if (attr != nullptr)
18243 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18244 name = dwarf2_name (die, cu);
18245 if (!name)
18246 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18247
18248 arch = objfile->arch ();
18249 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18250
18251 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18252 if (attr != nullptr && attr->form_is_constant ())
18253 {
18254 int endianity = attr->constant_value (0);
18255
18256 switch (endianity)
18257 {
18258 case DW_END_big:
18259 byte_order = BFD_ENDIAN_BIG;
18260 break;
18261 case DW_END_little:
18262 byte_order = BFD_ENDIAN_LITTLE;
18263 break;
18264 default:
18265 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18266 break;
18267 }
18268 }
18269
18270 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18271 && cu->language == language_ada
18272 && has_zero_over_zero_small_attribute (die, cu))
18273 {
18274 /* brobecker/2018-02-24: This is a fixed point type for which
18275 the scaling factor is represented as fraction whose value
18276 does not make sense (zero divided by zero), so we should
18277 normally never see these. However, there is a small category
18278 of fixed point types for which GNAT is unable to provide
18279 the scaling factor via the standard DWARF mechanisms, and
18280 for which the info is provided via the GNAT encodings instead.
18281 This is likely what this DIE is about. */
18282 encoding = (encoding == DW_ATE_signed_fixed
18283 ? DW_ATE_signed
18284 : DW_ATE_unsigned);
18285 }
18286
18287 /* With GNAT encodings, fixed-point information will be encoded in
18288 the type name. Note that this can also occur with the above
18289 zero-over-zero case, which is why this is a separate "if" rather
18290 than an "else if". */
18291 const char *gnat_encoding_suffix = nullptr;
18292 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18293 && cu->language == language_ada
18294 && name != nullptr)
18295 {
18296 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18297 if (gnat_encoding_suffix != nullptr)
18298 {
18299 gdb_assert (startswith (gnat_encoding_suffix,
18300 GNAT_FIXED_POINT_SUFFIX));
18301 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18302 name, gnat_encoding_suffix - name);
18303 /* Use -1 here so that SUFFIX points at the "_" after the
18304 "XF". */
18305 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18306
18307 encoding = (encoding == DW_ATE_signed
18308 ? DW_ATE_signed_fixed
18309 : DW_ATE_unsigned_fixed);
18310 }
18311 }
18312
18313 switch (encoding)
18314 {
18315 case DW_ATE_address:
18316 /* Turn DW_ATE_address into a void * pointer. */
18317 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18318 type = init_pointer_type (objfile, bits, name, type);
18319 break;
18320 case DW_ATE_boolean:
18321 type = init_boolean_type (objfile, bits, 1, name);
18322 break;
18323 case DW_ATE_complex_float:
18324 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18325 byte_order);
18326 if (type->code () == TYPE_CODE_ERROR)
18327 {
18328 if (name == nullptr)
18329 {
18330 struct obstack *obstack
18331 = &cu->per_objfile->objfile->objfile_obstack;
18332 name = obconcat (obstack, "_Complex ", type->name (),
18333 nullptr);
18334 }
18335 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18336 }
18337 else
18338 type = init_complex_type (name, type);
18339 break;
18340 case DW_ATE_decimal_float:
18341 type = init_decfloat_type (objfile, bits, name);
18342 break;
18343 case DW_ATE_float:
18344 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18345 break;
18346 case DW_ATE_signed:
18347 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18348 break;
18349 case DW_ATE_unsigned:
18350 if (cu->language == language_fortran
18351 && name
18352 && startswith (name, "character("))
18353 type = init_character_type (objfile, bits, 1, name);
18354 else
18355 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18356 break;
18357 case DW_ATE_signed_char:
18358 if (cu->language == language_ada || cu->language == language_m2
18359 || cu->language == language_pascal
18360 || cu->language == language_fortran)
18361 type = init_character_type (objfile, bits, 0, name);
18362 else
18363 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18364 break;
18365 case DW_ATE_unsigned_char:
18366 if (cu->language == language_ada || cu->language == language_m2
18367 || cu->language == language_pascal
18368 || cu->language == language_fortran
18369 || cu->language == language_rust)
18370 type = init_character_type (objfile, bits, 1, name);
18371 else
18372 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18373 break;
18374 case DW_ATE_UTF:
18375 {
18376 if (bits == 16)
18377 type = builtin_type (arch)->builtin_char16;
18378 else if (bits == 32)
18379 type = builtin_type (arch)->builtin_char32;
18380 else
18381 {
18382 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18383 bits);
18384 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18385 }
18386 return set_die_type (die, type, cu);
18387 }
18388 break;
18389 case DW_ATE_signed_fixed:
18390 type = init_fixed_point_type (objfile, bits, 0, name);
18391 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18392 break;
18393 case DW_ATE_unsigned_fixed:
18394 type = init_fixed_point_type (objfile, bits, 1, name);
18395 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18396 break;
18397
18398 default:
18399 complaint (_("unsupported DW_AT_encoding: '%s'"),
18400 dwarf_type_encoding_name (encoding));
18401 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18402 break;
18403 }
18404
18405 if (name && strcmp (name, "char") == 0)
18406 type->set_has_no_signedness (true);
18407
18408 maybe_set_alignment (cu, die, type);
18409
18410 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18411
18412 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18413 {
18414 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18415 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18416 {
18417 unsigned real_bit_size = attr->as_unsigned ();
18418 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18419 /* Only use the attributes if they make sense together. */
18420 if (attr == nullptr
18421 || (attr->as_unsigned () + real_bit_size
18422 <= 8 * TYPE_LENGTH (type)))
18423 {
18424 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18425 = real_bit_size;
18426 if (attr != nullptr)
18427 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18428 = attr->as_unsigned ();
18429 }
18430 }
18431 }
18432
18433 return set_die_type (die, type, cu);
18434 }
18435
18436 /* Parse dwarf attribute if it's a block, reference or constant and put the
18437 resulting value of the attribute into struct bound_prop.
18438 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18439
18440 static int
18441 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18442 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18443 struct type *default_type)
18444 {
18445 struct dwarf2_property_baton *baton;
18446 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18447 struct objfile *objfile = per_objfile->objfile;
18448 struct obstack *obstack = &objfile->objfile_obstack;
18449
18450 gdb_assert (default_type != NULL);
18451
18452 if (attr == NULL || prop == NULL)
18453 return 0;
18454
18455 if (attr->form_is_block ())
18456 {
18457 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18458 baton->property_type = default_type;
18459 baton->locexpr.per_cu = cu->per_cu;
18460 baton->locexpr.per_objfile = per_objfile;
18461
18462 struct dwarf_block *block = attr->as_block ();
18463 baton->locexpr.size = block->size;
18464 baton->locexpr.data = block->data;
18465 switch (attr->name)
18466 {
18467 case DW_AT_string_length:
18468 baton->locexpr.is_reference = true;
18469 break;
18470 default:
18471 baton->locexpr.is_reference = false;
18472 break;
18473 }
18474
18475 prop->set_locexpr (baton);
18476 gdb_assert (prop->baton () != NULL);
18477 }
18478 else if (attr->form_is_ref ())
18479 {
18480 struct dwarf2_cu *target_cu = cu;
18481 struct die_info *target_die;
18482 struct attribute *target_attr;
18483
18484 target_die = follow_die_ref (die, attr, &target_cu);
18485 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18486 if (target_attr == NULL)
18487 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18488 target_cu);
18489 if (target_attr == NULL)
18490 return 0;
18491
18492 switch (target_attr->name)
18493 {
18494 case DW_AT_location:
18495 if (target_attr->form_is_section_offset ())
18496 {
18497 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18498 baton->property_type = die_type (target_die, target_cu);
18499 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18500 prop->set_loclist (baton);
18501 gdb_assert (prop->baton () != NULL);
18502 }
18503 else if (target_attr->form_is_block ())
18504 {
18505 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18506 baton->property_type = die_type (target_die, target_cu);
18507 baton->locexpr.per_cu = cu->per_cu;
18508 baton->locexpr.per_objfile = per_objfile;
18509 struct dwarf_block *block = target_attr->as_block ();
18510 baton->locexpr.size = block->size;
18511 baton->locexpr.data = block->data;
18512 baton->locexpr.is_reference = true;
18513 prop->set_locexpr (baton);
18514 gdb_assert (prop->baton () != NULL);
18515 }
18516 else
18517 {
18518 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18519 "dynamic property");
18520 return 0;
18521 }
18522 break;
18523 case DW_AT_data_member_location:
18524 {
18525 LONGEST offset;
18526
18527 if (!handle_data_member_location (target_die, target_cu,
18528 &offset))
18529 return 0;
18530
18531 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18532 baton->property_type = read_type_die (target_die->parent,
18533 target_cu);
18534 baton->offset_info.offset = offset;
18535 baton->offset_info.type = die_type (target_die, target_cu);
18536 prop->set_addr_offset (baton);
18537 break;
18538 }
18539 }
18540 }
18541 else if (attr->form_is_constant ())
18542 prop->set_const_val (attr->constant_value (0));
18543 else
18544 {
18545 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18546 dwarf2_name (die, cu));
18547 return 0;
18548 }
18549
18550 return 1;
18551 }
18552
18553 /* See read.h. */
18554
18555 struct type *
18556 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18557 {
18558 struct type *int_type;
18559
18560 /* Helper macro to examine the various builtin types. */
18561 #define TRY_TYPE(F) \
18562 int_type = (unsigned_p \
18563 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18564 : objfile_type (objfile)->builtin_ ## F); \
18565 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18566 return int_type
18567
18568 TRY_TYPE (char);
18569 TRY_TYPE (short);
18570 TRY_TYPE (int);
18571 TRY_TYPE (long);
18572 TRY_TYPE (long_long);
18573
18574 #undef TRY_TYPE
18575
18576 gdb_assert_not_reached ("unable to find suitable integer type");
18577 }
18578
18579 /* See read.h. */
18580
18581 struct type *
18582 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
18583 {
18584 int addr_size = this->per_cu->addr_size ();
18585 return this->per_objfile->int_type (addr_size, unsigned_p);
18586 }
18587
18588 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18589 present (which is valid) then compute the default type based on the
18590 compilation units address size. */
18591
18592 static struct type *
18593 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18594 {
18595 struct type *index_type = die_type (die, cu);
18596
18597 /* Dwarf-2 specifications explicitly allows to create subrange types
18598 without specifying a base type.
18599 In that case, the base type must be set to the type of
18600 the lower bound, upper bound or count, in that order, if any of these
18601 three attributes references an object that has a type.
18602 If no base type is found, the Dwarf-2 specifications say that
18603 a signed integer type of size equal to the size of an address should
18604 be used.
18605 For the following C code: `extern char gdb_int [];'
18606 GCC produces an empty range DIE.
18607 FIXME: muller/2010-05-28: Possible references to object for low bound,
18608 high bound or count are not yet handled by this code. */
18609 if (index_type->code () == TYPE_CODE_VOID)
18610 index_type = cu->addr_sized_int_type (false);
18611
18612 return index_type;
18613 }
18614
18615 /* Read the given DW_AT_subrange DIE. */
18616
18617 static struct type *
18618 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18619 {
18620 struct type *base_type, *orig_base_type;
18621 struct type *range_type;
18622 struct attribute *attr;
18623 struct dynamic_prop low, high;
18624 int low_default_is_valid;
18625 int high_bound_is_count = 0;
18626 const char *name;
18627 ULONGEST negative_mask;
18628
18629 orig_base_type = read_subrange_index_type (die, cu);
18630
18631 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18632 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18633 creating the range type, but we use the result of check_typedef
18634 when examining properties of the type. */
18635 base_type = check_typedef (orig_base_type);
18636
18637 /* The die_type call above may have already set the type for this DIE. */
18638 range_type = get_die_type (die, cu);
18639 if (range_type)
18640 return range_type;
18641
18642 high.set_const_val (0);
18643
18644 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18645 omitting DW_AT_lower_bound. */
18646 switch (cu->language)
18647 {
18648 case language_c:
18649 case language_cplus:
18650 low.set_const_val (0);
18651 low_default_is_valid = 1;
18652 break;
18653 case language_fortran:
18654 low.set_const_val (1);
18655 low_default_is_valid = 1;
18656 break;
18657 case language_d:
18658 case language_objc:
18659 case language_rust:
18660 low.set_const_val (0);
18661 low_default_is_valid = (cu->header.version >= 4);
18662 break;
18663 case language_ada:
18664 case language_m2:
18665 case language_pascal:
18666 low.set_const_val (1);
18667 low_default_is_valid = (cu->header.version >= 4);
18668 break;
18669 default:
18670 low.set_const_val (0);
18671 low_default_is_valid = 0;
18672 break;
18673 }
18674
18675 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18676 if (attr != nullptr)
18677 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18678 else if (!low_default_is_valid)
18679 complaint (_("Missing DW_AT_lower_bound "
18680 "- DIE at %s [in module %s]"),
18681 sect_offset_str (die->sect_off),
18682 objfile_name (cu->per_objfile->objfile));
18683
18684 struct attribute *attr_ub, *attr_count;
18685 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18686 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18687 {
18688 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18689 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18690 {
18691 /* If bounds are constant do the final calculation here. */
18692 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18693 high.set_const_val (low.const_val () + high.const_val () - 1);
18694 else
18695 high_bound_is_count = 1;
18696 }
18697 else
18698 {
18699 if (attr_ub != NULL)
18700 complaint (_("Unresolved DW_AT_upper_bound "
18701 "- DIE at %s [in module %s]"),
18702 sect_offset_str (die->sect_off),
18703 objfile_name (cu->per_objfile->objfile));
18704 if (attr_count != NULL)
18705 complaint (_("Unresolved DW_AT_count "
18706 "- DIE at %s [in module %s]"),
18707 sect_offset_str (die->sect_off),
18708 objfile_name (cu->per_objfile->objfile));
18709 }
18710 }
18711
18712 LONGEST bias = 0;
18713 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18714 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18715 bias = bias_attr->constant_value (0);
18716
18717 /* Normally, the DWARF producers are expected to use a signed
18718 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18719 But this is unfortunately not always the case, as witnessed
18720 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18721 is used instead. To work around that ambiguity, we treat
18722 the bounds as signed, and thus sign-extend their values, when
18723 the base type is signed. */
18724 negative_mask =
18725 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18726 if (low.kind () == PROP_CONST
18727 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
18728 low.set_const_val (low.const_val () | negative_mask);
18729 if (high.kind () == PROP_CONST
18730 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
18731 high.set_const_val (high.const_val () | negative_mask);
18732
18733 /* Check for bit and byte strides. */
18734 struct dynamic_prop byte_stride_prop;
18735 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18736 if (attr_byte_stride != nullptr)
18737 {
18738 struct type *prop_type = cu->addr_sized_int_type (false);
18739 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18740 prop_type);
18741 }
18742
18743 struct dynamic_prop bit_stride_prop;
18744 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18745 if (attr_bit_stride != nullptr)
18746 {
18747 /* It only makes sense to have either a bit or byte stride. */
18748 if (attr_byte_stride != nullptr)
18749 {
18750 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18751 "- DIE at %s [in module %s]"),
18752 sect_offset_str (die->sect_off),
18753 objfile_name (cu->per_objfile->objfile));
18754 attr_bit_stride = nullptr;
18755 }
18756 else
18757 {
18758 struct type *prop_type = cu->addr_sized_int_type (false);
18759 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18760 prop_type);
18761 }
18762 }
18763
18764 if (attr_byte_stride != nullptr
18765 || attr_bit_stride != nullptr)
18766 {
18767 bool byte_stride_p = (attr_byte_stride != nullptr);
18768 struct dynamic_prop *stride
18769 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18770
18771 range_type
18772 = create_range_type_with_stride (NULL, orig_base_type, &low,
18773 &high, bias, stride, byte_stride_p);
18774 }
18775 else
18776 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18777
18778 if (high_bound_is_count)
18779 range_type->bounds ()->flag_upper_bound_is_count = 1;
18780
18781 /* Ada expects an empty array on no boundary attributes. */
18782 if (attr == NULL && cu->language != language_ada)
18783 range_type->bounds ()->high.set_undefined ();
18784
18785 name = dwarf2_name (die, cu);
18786 if (name)
18787 range_type->set_name (name);
18788
18789 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18790 if (attr != nullptr)
18791 TYPE_LENGTH (range_type) = attr->constant_value (0);
18792
18793 maybe_set_alignment (cu, die, range_type);
18794
18795 set_die_type (die, range_type, cu);
18796
18797 /* set_die_type should be already done. */
18798 set_descriptive_type (range_type, die, cu);
18799
18800 return range_type;
18801 }
18802
18803 static struct type *
18804 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18805 {
18806 struct type *type;
18807
18808 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
18809 type->set_name (dwarf2_name (die, cu));
18810
18811 /* In Ada, an unspecified type is typically used when the description
18812 of the type is deferred to a different unit. When encountering
18813 such a type, we treat it as a stub, and try to resolve it later on,
18814 when needed. */
18815 if (cu->language == language_ada)
18816 type->set_is_stub (true);
18817
18818 return set_die_type (die, type, cu);
18819 }
18820
18821 /* Read a single die and all its descendents. Set the die's sibling
18822 field to NULL; set other fields in the die correctly, and set all
18823 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18824 location of the info_ptr after reading all of those dies. PARENT
18825 is the parent of the die in question. */
18826
18827 static struct die_info *
18828 read_die_and_children (const struct die_reader_specs *reader,
18829 const gdb_byte *info_ptr,
18830 const gdb_byte **new_info_ptr,
18831 struct die_info *parent)
18832 {
18833 struct die_info *die;
18834 const gdb_byte *cur_ptr;
18835
18836 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18837 if (die == NULL)
18838 {
18839 *new_info_ptr = cur_ptr;
18840 return NULL;
18841 }
18842 store_in_ref_table (die, reader->cu);
18843
18844 if (die->has_children)
18845 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18846 else
18847 {
18848 die->child = NULL;
18849 *new_info_ptr = cur_ptr;
18850 }
18851
18852 die->sibling = NULL;
18853 die->parent = parent;
18854 return die;
18855 }
18856
18857 /* Read a die, all of its descendents, and all of its siblings; set
18858 all of the fields of all of the dies correctly. Arguments are as
18859 in read_die_and_children. */
18860
18861 static struct die_info *
18862 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18863 const gdb_byte *info_ptr,
18864 const gdb_byte **new_info_ptr,
18865 struct die_info *parent)
18866 {
18867 struct die_info *first_die, *last_sibling;
18868 const gdb_byte *cur_ptr;
18869
18870 cur_ptr = info_ptr;
18871 first_die = last_sibling = NULL;
18872
18873 while (1)
18874 {
18875 struct die_info *die
18876 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18877
18878 if (die == NULL)
18879 {
18880 *new_info_ptr = cur_ptr;
18881 return first_die;
18882 }
18883
18884 if (!first_die)
18885 first_die = die;
18886 else
18887 last_sibling->sibling = die;
18888
18889 last_sibling = die;
18890 }
18891 }
18892
18893 /* Read a die, all of its descendents, and all of its siblings; set
18894 all of the fields of all of the dies correctly. Arguments are as
18895 in read_die_and_children.
18896 This the main entry point for reading a DIE and all its children. */
18897
18898 static struct die_info *
18899 read_die_and_siblings (const struct die_reader_specs *reader,
18900 const gdb_byte *info_ptr,
18901 const gdb_byte **new_info_ptr,
18902 struct die_info *parent)
18903 {
18904 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18905 new_info_ptr, parent);
18906
18907 if (dwarf_die_debug)
18908 {
18909 fprintf_unfiltered (gdb_stdlog,
18910 "Read die from %s@0x%x of %s:\n",
18911 reader->die_section->get_name (),
18912 (unsigned) (info_ptr - reader->die_section->buffer),
18913 bfd_get_filename (reader->abfd));
18914 dump_die (die, dwarf_die_debug);
18915 }
18916
18917 return die;
18918 }
18919
18920 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18921 attributes.
18922 The caller is responsible for filling in the extra attributes
18923 and updating (*DIEP)->num_attrs.
18924 Set DIEP to point to a newly allocated die with its information,
18925 except for its child, sibling, and parent fields. */
18926
18927 static const gdb_byte *
18928 read_full_die_1 (const struct die_reader_specs *reader,
18929 struct die_info **diep, const gdb_byte *info_ptr,
18930 int num_extra_attrs)
18931 {
18932 unsigned int abbrev_number, bytes_read, i;
18933 const struct abbrev_info *abbrev;
18934 struct die_info *die;
18935 struct dwarf2_cu *cu = reader->cu;
18936 bfd *abfd = reader->abfd;
18937
18938 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18939 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18940 info_ptr += bytes_read;
18941 if (!abbrev_number)
18942 {
18943 *diep = NULL;
18944 return info_ptr;
18945 }
18946
18947 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18948 if (!abbrev)
18949 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18950 abbrev_number,
18951 bfd_get_filename (abfd));
18952
18953 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18954 die->sect_off = sect_off;
18955 die->tag = abbrev->tag;
18956 die->abbrev = abbrev_number;
18957 die->has_children = abbrev->has_children;
18958
18959 /* Make the result usable.
18960 The caller needs to update num_attrs after adding the extra
18961 attributes. */
18962 die->num_attrs = abbrev->num_attrs;
18963
18964 bool any_need_reprocess = false;
18965 for (i = 0; i < abbrev->num_attrs; ++i)
18966 {
18967 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18968 info_ptr);
18969 if (die->attrs[i].requires_reprocessing_p ())
18970 any_need_reprocess = true;
18971 }
18972
18973 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18974 if (attr != nullptr && attr->form_is_unsigned ())
18975 cu->str_offsets_base = attr->as_unsigned ();
18976
18977 attr = die->attr (DW_AT_loclists_base);
18978 if (attr != nullptr)
18979 cu->loclist_base = attr->as_unsigned ();
18980
18981 auto maybe_addr_base = die->addr_base ();
18982 if (maybe_addr_base.has_value ())
18983 cu->addr_base = *maybe_addr_base;
18984
18985 attr = die->attr (DW_AT_rnglists_base);
18986 if (attr != nullptr)
18987 cu->rnglists_base = attr->as_unsigned ();
18988
18989 if (any_need_reprocess)
18990 {
18991 for (i = 0; i < abbrev->num_attrs; ++i)
18992 {
18993 if (die->attrs[i].requires_reprocessing_p ())
18994 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18995 }
18996 }
18997 *diep = die;
18998 return info_ptr;
18999 }
19000
19001 /* Read a die and all its attributes.
19002 Set DIEP to point to a newly allocated die with its information,
19003 except for its child, sibling, and parent fields. */
19004
19005 static const gdb_byte *
19006 read_full_die (const struct die_reader_specs *reader,
19007 struct die_info **diep, const gdb_byte *info_ptr)
19008 {
19009 const gdb_byte *result;
19010
19011 result = read_full_die_1 (reader, diep, info_ptr, 0);
19012
19013 if (dwarf_die_debug)
19014 {
19015 fprintf_unfiltered (gdb_stdlog,
19016 "Read die from %s@0x%x of %s:\n",
19017 reader->die_section->get_name (),
19018 (unsigned) (info_ptr - reader->die_section->buffer),
19019 bfd_get_filename (reader->abfd));
19020 dump_die (*diep, dwarf_die_debug);
19021 }
19022
19023 return result;
19024 }
19025 \f
19026
19027 /* Returns nonzero if TAG represents a type that we might generate a partial
19028 symbol for. */
19029
19030 static int
19031 is_type_tag_for_partial (int tag, enum language lang)
19032 {
19033 switch (tag)
19034 {
19035 #if 0
19036 /* Some types that would be reasonable to generate partial symbols for,
19037 that we don't at present. Note that normally this does not
19038 matter, mainly because C compilers don't give names to these
19039 types, but instead emit DW_TAG_typedef. */
19040 case DW_TAG_file_type:
19041 case DW_TAG_ptr_to_member_type:
19042 case DW_TAG_set_type:
19043 case DW_TAG_string_type:
19044 case DW_TAG_subroutine_type:
19045 #endif
19046
19047 /* GNAT may emit an array with a name, but no typedef, so we
19048 need to make a symbol in this case. */
19049 case DW_TAG_array_type:
19050 return lang == language_ada;
19051
19052 case DW_TAG_base_type:
19053 case DW_TAG_class_type:
19054 case DW_TAG_interface_type:
19055 case DW_TAG_enumeration_type:
19056 case DW_TAG_structure_type:
19057 case DW_TAG_subrange_type:
19058 case DW_TAG_typedef:
19059 case DW_TAG_union_type:
19060 return 1;
19061 default:
19062 return 0;
19063 }
19064 }
19065
19066 /* Load all DIEs that are interesting for partial symbols into memory. */
19067
19068 static struct partial_die_info *
19069 load_partial_dies (const struct die_reader_specs *reader,
19070 const gdb_byte *info_ptr, int building_psymtab)
19071 {
19072 struct dwarf2_cu *cu = reader->cu;
19073 struct objfile *objfile = cu->per_objfile->objfile;
19074 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19075 unsigned int bytes_read;
19076 unsigned int load_all = 0;
19077 int nesting_level = 1;
19078
19079 parent_die = NULL;
19080 last_die = NULL;
19081
19082 gdb_assert (cu->per_cu != NULL);
19083 if (cu->per_cu->load_all_dies)
19084 load_all = 1;
19085
19086 cu->partial_dies
19087 = htab_create_alloc_ex (cu->header.length / 12,
19088 partial_die_hash,
19089 partial_die_eq,
19090 NULL,
19091 &cu->comp_unit_obstack,
19092 hashtab_obstack_allocate,
19093 dummy_obstack_deallocate);
19094
19095 while (1)
19096 {
19097 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19098 &bytes_read);
19099
19100 /* A NULL abbrev means the end of a series of children. */
19101 if (abbrev == NULL)
19102 {
19103 if (--nesting_level == 0)
19104 return first_die;
19105
19106 info_ptr += bytes_read;
19107 last_die = parent_die;
19108 parent_die = parent_die->die_parent;
19109 continue;
19110 }
19111
19112 /* Check for template arguments. We never save these; if
19113 they're seen, we just mark the parent, and go on our way. */
19114 if (parent_die != NULL
19115 && cu->language == language_cplus
19116 && (abbrev->tag == DW_TAG_template_type_param
19117 || abbrev->tag == DW_TAG_template_value_param))
19118 {
19119 parent_die->has_template_arguments = 1;
19120
19121 if (!load_all)
19122 {
19123 /* We don't need a partial DIE for the template argument. */
19124 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19125 continue;
19126 }
19127 }
19128
19129 /* We only recurse into c++ subprograms looking for template arguments.
19130 Skip their other children. */
19131 if (!load_all
19132 && cu->language == language_cplus
19133 && parent_die != NULL
19134 && parent_die->tag == DW_TAG_subprogram
19135 && abbrev->tag != DW_TAG_inlined_subroutine)
19136 {
19137 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19138 continue;
19139 }
19140
19141 /* Check whether this DIE is interesting enough to save. Normally
19142 we would not be interested in members here, but there may be
19143 later variables referencing them via DW_AT_specification (for
19144 static members). */
19145 if (!load_all
19146 && !is_type_tag_for_partial (abbrev->tag, cu->language)
19147 && abbrev->tag != DW_TAG_constant
19148 && abbrev->tag != DW_TAG_enumerator
19149 && abbrev->tag != DW_TAG_subprogram
19150 && abbrev->tag != DW_TAG_inlined_subroutine
19151 && abbrev->tag != DW_TAG_lexical_block
19152 && abbrev->tag != DW_TAG_variable
19153 && abbrev->tag != DW_TAG_namespace
19154 && abbrev->tag != DW_TAG_module
19155 && abbrev->tag != DW_TAG_member
19156 && abbrev->tag != DW_TAG_imported_unit
19157 && abbrev->tag != DW_TAG_imported_declaration)
19158 {
19159 /* Otherwise we skip to the next sibling, if any. */
19160 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19161 continue;
19162 }
19163
19164 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19165 abbrev);
19166
19167 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19168
19169 /* This two-pass algorithm for processing partial symbols has a
19170 high cost in cache pressure. Thus, handle some simple cases
19171 here which cover the majority of C partial symbols. DIEs
19172 which neither have specification tags in them, nor could have
19173 specification tags elsewhere pointing at them, can simply be
19174 processed and discarded.
19175
19176 This segment is also optional; scan_partial_symbols and
19177 add_partial_symbol will handle these DIEs if we chain
19178 them in normally. When compilers which do not emit large
19179 quantities of duplicate debug information are more common,
19180 this code can probably be removed. */
19181
19182 /* Any complete simple types at the top level (pretty much all
19183 of them, for a language without namespaces), can be processed
19184 directly. */
19185 if (parent_die == NULL
19186 && pdi.has_specification == 0
19187 && pdi.is_declaration == 0
19188 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19189 || pdi.tag == DW_TAG_base_type
19190 || pdi.tag == DW_TAG_array_type
19191 || pdi.tag == DW_TAG_subrange_type))
19192 {
19193 if (building_psymtab && pdi.raw_name != NULL)
19194 add_partial_symbol (&pdi, cu);
19195
19196 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19197 continue;
19198 }
19199
19200 /* The exception for DW_TAG_typedef with has_children above is
19201 a workaround of GCC PR debug/47510. In the case of this complaint
19202 type_name_or_error will error on such types later.
19203
19204 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19205 it could not find the child DIEs referenced later, this is checked
19206 above. In correct DWARF DW_TAG_typedef should have no children. */
19207
19208 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19209 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19210 "- DIE at %s [in module %s]"),
19211 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19212
19213 /* If we're at the second level, and we're an enumerator, and
19214 our parent has no specification (meaning possibly lives in a
19215 namespace elsewhere), then we can add the partial symbol now
19216 instead of queueing it. */
19217 if (pdi.tag == DW_TAG_enumerator
19218 && parent_die != NULL
19219 && parent_die->die_parent == NULL
19220 && parent_die->tag == DW_TAG_enumeration_type
19221 && parent_die->has_specification == 0)
19222 {
19223 if (pdi.raw_name == NULL)
19224 complaint (_("malformed enumerator DIE ignored"));
19225 else if (building_psymtab)
19226 add_partial_symbol (&pdi, cu);
19227
19228 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19229 continue;
19230 }
19231
19232 struct partial_die_info *part_die
19233 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19234
19235 /* We'll save this DIE so link it in. */
19236 part_die->die_parent = parent_die;
19237 part_die->die_sibling = NULL;
19238 part_die->die_child = NULL;
19239
19240 if (last_die && last_die == parent_die)
19241 last_die->die_child = part_die;
19242 else if (last_die)
19243 last_die->die_sibling = part_die;
19244
19245 last_die = part_die;
19246
19247 if (first_die == NULL)
19248 first_die = part_die;
19249
19250 /* Maybe add the DIE to the hash table. Not all DIEs that we
19251 find interesting need to be in the hash table, because we
19252 also have the parent/sibling/child chains; only those that we
19253 might refer to by offset later during partial symbol reading.
19254
19255 For now this means things that might have be the target of a
19256 DW_AT_specification, DW_AT_abstract_origin, or
19257 DW_AT_extension. DW_AT_extension will refer only to
19258 namespaces; DW_AT_abstract_origin refers to functions (and
19259 many things under the function DIE, but we do not recurse
19260 into function DIEs during partial symbol reading) and
19261 possibly variables as well; DW_AT_specification refers to
19262 declarations. Declarations ought to have the DW_AT_declaration
19263 flag. It happens that GCC forgets to put it in sometimes, but
19264 only for functions, not for types.
19265
19266 Adding more things than necessary to the hash table is harmless
19267 except for the performance cost. Adding too few will result in
19268 wasted time in find_partial_die, when we reread the compilation
19269 unit with load_all_dies set. */
19270
19271 if (load_all
19272 || abbrev->tag == DW_TAG_constant
19273 || abbrev->tag == DW_TAG_subprogram
19274 || abbrev->tag == DW_TAG_variable
19275 || abbrev->tag == DW_TAG_namespace
19276 || part_die->is_declaration)
19277 {
19278 void **slot;
19279
19280 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19281 to_underlying (part_die->sect_off),
19282 INSERT);
19283 *slot = part_die;
19284 }
19285
19286 /* For some DIEs we want to follow their children (if any). For C
19287 we have no reason to follow the children of structures; for other
19288 languages we have to, so that we can get at method physnames
19289 to infer fully qualified class names, for DW_AT_specification,
19290 and for C++ template arguments. For C++, we also look one level
19291 inside functions to find template arguments (if the name of the
19292 function does not already contain the template arguments).
19293
19294 For Ada and Fortran, we need to scan the children of subprograms
19295 and lexical blocks as well because these languages allow the
19296 definition of nested entities that could be interesting for the
19297 debugger, such as nested subprograms for instance. */
19298 if (last_die->has_children
19299 && (load_all
19300 || last_die->tag == DW_TAG_namespace
19301 || last_die->tag == DW_TAG_module
19302 || last_die->tag == DW_TAG_enumeration_type
19303 || (cu->language == language_cplus
19304 && last_die->tag == DW_TAG_subprogram
19305 && (last_die->raw_name == NULL
19306 || strchr (last_die->raw_name, '<') == NULL))
19307 || (cu->language != language_c
19308 && (last_die->tag == DW_TAG_class_type
19309 || last_die->tag == DW_TAG_interface_type
19310 || last_die->tag == DW_TAG_structure_type
19311 || last_die->tag == DW_TAG_union_type))
19312 || ((cu->language == language_ada
19313 || cu->language == language_fortran)
19314 && (last_die->tag == DW_TAG_subprogram
19315 || last_die->tag == DW_TAG_lexical_block))))
19316 {
19317 nesting_level++;
19318 parent_die = last_die;
19319 continue;
19320 }
19321
19322 /* Otherwise we skip to the next sibling, if any. */
19323 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19324
19325 /* Back to the top, do it again. */
19326 }
19327 }
19328
19329 partial_die_info::partial_die_info (sect_offset sect_off_,
19330 const struct abbrev_info *abbrev)
19331 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19332 {
19333 }
19334
19335 /* See class definition. */
19336
19337 const char *
19338 partial_die_info::name (dwarf2_cu *cu)
19339 {
19340 if (!canonical_name && raw_name != nullptr)
19341 {
19342 struct objfile *objfile = cu->per_objfile->objfile;
19343 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19344 canonical_name = 1;
19345 }
19346
19347 return raw_name;
19348 }
19349
19350 /* Read a minimal amount of information into the minimal die structure.
19351 INFO_PTR should point just after the initial uleb128 of a DIE. */
19352
19353 const gdb_byte *
19354 partial_die_info::read (const struct die_reader_specs *reader,
19355 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19356 {
19357 struct dwarf2_cu *cu = reader->cu;
19358 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19359 unsigned int i;
19360 int has_low_pc_attr = 0;
19361 int has_high_pc_attr = 0;
19362 int high_pc_relative = 0;
19363
19364 for (i = 0; i < abbrev.num_attrs; ++i)
19365 {
19366 attribute attr;
19367 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19368 /* String and address offsets that need to do the reprocessing have
19369 already been read at this point, so there is no need to wait until
19370 the loop terminates to do the reprocessing. */
19371 if (attr.requires_reprocessing_p ())
19372 read_attribute_reprocess (reader, &attr, tag);
19373 /* Store the data if it is of an attribute we want to keep in a
19374 partial symbol table. */
19375 switch (attr.name)
19376 {
19377 case DW_AT_name:
19378 switch (tag)
19379 {
19380 case DW_TAG_compile_unit:
19381 case DW_TAG_partial_unit:
19382 case DW_TAG_type_unit:
19383 /* Compilation units have a DW_AT_name that is a filename, not
19384 a source language identifier. */
19385 case DW_TAG_enumeration_type:
19386 case DW_TAG_enumerator:
19387 /* These tags always have simple identifiers already; no need
19388 to canonicalize them. */
19389 canonical_name = 1;
19390 raw_name = attr.as_string ();
19391 break;
19392 default:
19393 canonical_name = 0;
19394 raw_name = attr.as_string ();
19395 break;
19396 }
19397 break;
19398 case DW_AT_linkage_name:
19399 case DW_AT_MIPS_linkage_name:
19400 /* Note that both forms of linkage name might appear. We
19401 assume they will be the same, and we only store the last
19402 one we see. */
19403 linkage_name = attr.as_string ();
19404 break;
19405 case DW_AT_low_pc:
19406 has_low_pc_attr = 1;
19407 lowpc = attr.as_address ();
19408 break;
19409 case DW_AT_high_pc:
19410 has_high_pc_attr = 1;
19411 highpc = attr.as_address ();
19412 if (cu->header.version >= 4 && attr.form_is_constant ())
19413 high_pc_relative = 1;
19414 break;
19415 case DW_AT_location:
19416 /* Support the .debug_loc offsets. */
19417 if (attr.form_is_block ())
19418 {
19419 d.locdesc = attr.as_block ();
19420 }
19421 else if (attr.form_is_section_offset ())
19422 {
19423 dwarf2_complex_location_expr_complaint ();
19424 }
19425 else
19426 {
19427 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19428 "partial symbol information");
19429 }
19430 break;
19431 case DW_AT_external:
19432 is_external = attr.as_boolean ();
19433 break;
19434 case DW_AT_declaration:
19435 is_declaration = attr.as_boolean ();
19436 break;
19437 case DW_AT_type:
19438 has_type = 1;
19439 break;
19440 case DW_AT_abstract_origin:
19441 case DW_AT_specification:
19442 case DW_AT_extension:
19443 has_specification = 1;
19444 spec_offset = attr.get_ref_die_offset ();
19445 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19446 || cu->per_cu->is_dwz);
19447 break;
19448 case DW_AT_sibling:
19449 /* Ignore absolute siblings, they might point outside of
19450 the current compile unit. */
19451 if (attr.form == DW_FORM_ref_addr)
19452 complaint (_("ignoring absolute DW_AT_sibling"));
19453 else
19454 {
19455 const gdb_byte *buffer = reader->buffer;
19456 sect_offset off = attr.get_ref_die_offset ();
19457 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19458
19459 if (sibling_ptr < info_ptr)
19460 complaint (_("DW_AT_sibling points backwards"));
19461 else if (sibling_ptr > reader->buffer_end)
19462 reader->die_section->overflow_complaint ();
19463 else
19464 sibling = sibling_ptr;
19465 }
19466 break;
19467 case DW_AT_byte_size:
19468 has_byte_size = 1;
19469 break;
19470 case DW_AT_const_value:
19471 has_const_value = 1;
19472 break;
19473 case DW_AT_calling_convention:
19474 /* DWARF doesn't provide a way to identify a program's source-level
19475 entry point. DW_AT_calling_convention attributes are only meant
19476 to describe functions' calling conventions.
19477
19478 However, because it's a necessary piece of information in
19479 Fortran, and before DWARF 4 DW_CC_program was the only
19480 piece of debugging information whose definition refers to
19481 a 'main program' at all, several compilers marked Fortran
19482 main programs with DW_CC_program --- even when those
19483 functions use the standard calling conventions.
19484
19485 Although DWARF now specifies a way to provide this
19486 information, we support this practice for backward
19487 compatibility. */
19488 if (attr.constant_value (0) == DW_CC_program
19489 && cu->language == language_fortran)
19490 main_subprogram = 1;
19491 break;
19492 case DW_AT_inline:
19493 {
19494 LONGEST value = attr.constant_value (-1);
19495 if (value == DW_INL_inlined
19496 || value == DW_INL_declared_inlined)
19497 may_be_inlined = 1;
19498 }
19499 break;
19500
19501 case DW_AT_import:
19502 if (tag == DW_TAG_imported_unit)
19503 {
19504 d.sect_off = attr.get_ref_die_offset ();
19505 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19506 || cu->per_cu->is_dwz);
19507 }
19508 break;
19509
19510 case DW_AT_main_subprogram:
19511 main_subprogram = attr.as_boolean ();
19512 break;
19513
19514 case DW_AT_ranges:
19515 {
19516 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19517 on DWARF version). */
19518 ULONGEST ranges_offset = attr.as_unsigned ();
19519
19520 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19521 this value. */
19522 if (tag != DW_TAG_compile_unit)
19523 ranges_offset += cu->gnu_ranges_base;
19524
19525 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19526 nullptr, tag))
19527 has_pc_info = 1;
19528 }
19529 break;
19530
19531 default:
19532 break;
19533 }
19534 }
19535
19536 /* For Ada, if both the name and the linkage name appear, we prefer
19537 the latter. This lets "catch exception" work better, regardless
19538 of the order in which the name and linkage name were emitted.
19539 Really, though, this is just a workaround for the fact that gdb
19540 doesn't store both the name and the linkage name. */
19541 if (cu->language == language_ada && linkage_name != nullptr)
19542 raw_name = linkage_name;
19543
19544 if (high_pc_relative)
19545 highpc += lowpc;
19546
19547 if (has_low_pc_attr && has_high_pc_attr)
19548 {
19549 /* When using the GNU linker, .gnu.linkonce. sections are used to
19550 eliminate duplicate copies of functions and vtables and such.
19551 The linker will arbitrarily choose one and discard the others.
19552 The AT_*_pc values for such functions refer to local labels in
19553 these sections. If the section from that file was discarded, the
19554 labels are not in the output, so the relocs get a value of 0.
19555 If this is a discarded function, mark the pc bounds as invalid,
19556 so that GDB will ignore it. */
19557 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19558 {
19559 struct objfile *objfile = per_objfile->objfile;
19560 struct gdbarch *gdbarch = objfile->arch ();
19561
19562 complaint (_("DW_AT_low_pc %s is zero "
19563 "for DIE at %s [in module %s]"),
19564 paddress (gdbarch, lowpc),
19565 sect_offset_str (sect_off),
19566 objfile_name (objfile));
19567 }
19568 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19569 else if (lowpc >= highpc)
19570 {
19571 struct objfile *objfile = per_objfile->objfile;
19572 struct gdbarch *gdbarch = objfile->arch ();
19573
19574 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19575 "for DIE at %s [in module %s]"),
19576 paddress (gdbarch, lowpc),
19577 paddress (gdbarch, highpc),
19578 sect_offset_str (sect_off),
19579 objfile_name (objfile));
19580 }
19581 else
19582 has_pc_info = 1;
19583 }
19584
19585 return info_ptr;
19586 }
19587
19588 /* Find a cached partial DIE at OFFSET in CU. */
19589
19590 struct partial_die_info *
19591 dwarf2_cu::find_partial_die (sect_offset sect_off)
19592 {
19593 struct partial_die_info *lookup_die = NULL;
19594 struct partial_die_info part_die (sect_off);
19595
19596 lookup_die = ((struct partial_die_info *)
19597 htab_find_with_hash (partial_dies, &part_die,
19598 to_underlying (sect_off)));
19599
19600 return lookup_die;
19601 }
19602
19603 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19604 except in the case of .debug_types DIEs which do not reference
19605 outside their CU (they do however referencing other types via
19606 DW_FORM_ref_sig8). */
19607
19608 static const struct cu_partial_die_info
19609 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19610 {
19611 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19612 struct objfile *objfile = per_objfile->objfile;
19613 struct partial_die_info *pd = NULL;
19614
19615 if (offset_in_dwz == cu->per_cu->is_dwz
19616 && cu->header.offset_in_cu_p (sect_off))
19617 {
19618 pd = cu->find_partial_die (sect_off);
19619 if (pd != NULL)
19620 return { cu, pd };
19621 /* We missed recording what we needed.
19622 Load all dies and try again. */
19623 }
19624 else
19625 {
19626 /* TUs don't reference other CUs/TUs (except via type signatures). */
19627 if (cu->per_cu->is_debug_types)
19628 {
19629 error (_("Dwarf Error: Type Unit at offset %s contains"
19630 " external reference to offset %s [in module %s].\n"),
19631 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19632 bfd_get_filename (objfile->obfd));
19633 }
19634 dwarf2_per_cu_data *per_cu
19635 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19636 per_objfile);
19637
19638 cu = per_objfile->get_cu (per_cu);
19639 if (cu == NULL || cu->partial_dies == NULL)
19640 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19641
19642 cu = per_objfile->get_cu (per_cu);
19643
19644 cu->last_used = 0;
19645 pd = cu->find_partial_die (sect_off);
19646 }
19647
19648 /* If we didn't find it, and not all dies have been loaded,
19649 load them all and try again. */
19650
19651 if (pd == NULL && cu->per_cu->load_all_dies == 0)
19652 {
19653 cu->per_cu->load_all_dies = 1;
19654
19655 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19656 THIS_CU->cu may already be in use. So we can't just free it and
19657 replace its DIEs with the ones we read in. Instead, we leave those
19658 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19659 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19660 set. */
19661 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19662
19663 pd = cu->find_partial_die (sect_off);
19664 }
19665
19666 if (pd == NULL)
19667 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
19668 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19669 return { cu, pd };
19670 }
19671
19672 /* See if we can figure out if the class lives in a namespace. We do
19673 this by looking for a member function; its demangled name will
19674 contain namespace info, if there is any. */
19675
19676 static void
19677 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19678 struct dwarf2_cu *cu)
19679 {
19680 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19681 what template types look like, because the demangler
19682 frequently doesn't give the same name as the debug info. We
19683 could fix this by only using the demangled name to get the
19684 prefix (but see comment in read_structure_type). */
19685
19686 struct partial_die_info *real_pdi;
19687 struct partial_die_info *child_pdi;
19688
19689 /* If this DIE (this DIE's specification, if any) has a parent, then
19690 we should not do this. We'll prepend the parent's fully qualified
19691 name when we create the partial symbol. */
19692
19693 real_pdi = struct_pdi;
19694 while (real_pdi->has_specification)
19695 {
19696 auto res = find_partial_die (real_pdi->spec_offset,
19697 real_pdi->spec_is_dwz, cu);
19698 real_pdi = res.pdi;
19699 cu = res.cu;
19700 }
19701
19702 if (real_pdi->die_parent != NULL)
19703 return;
19704
19705 for (child_pdi = struct_pdi->die_child;
19706 child_pdi != NULL;
19707 child_pdi = child_pdi->die_sibling)
19708 {
19709 if (child_pdi->tag == DW_TAG_subprogram
19710 && child_pdi->linkage_name != NULL)
19711 {
19712 gdb::unique_xmalloc_ptr<char> actual_class_name
19713 (cu->language_defn->class_name_from_physname
19714 (child_pdi->linkage_name));
19715 if (actual_class_name != NULL)
19716 {
19717 struct objfile *objfile = cu->per_objfile->objfile;
19718 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19719 struct_pdi->canonical_name = 1;
19720 }
19721 break;
19722 }
19723 }
19724 }
19725
19726 /* Return true if a DIE with TAG may have the DW_AT_const_value
19727 attribute. */
19728
19729 static bool
19730 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19731 {
19732 switch (tag)
19733 {
19734 case DW_TAG_constant:
19735 case DW_TAG_enumerator:
19736 case DW_TAG_formal_parameter:
19737 case DW_TAG_template_value_param:
19738 case DW_TAG_variable:
19739 return true;
19740 }
19741
19742 return false;
19743 }
19744
19745 void
19746 partial_die_info::fixup (struct dwarf2_cu *cu)
19747 {
19748 /* Once we've fixed up a die, there's no point in doing so again.
19749 This also avoids a memory leak if we were to call
19750 guess_partial_die_structure_name multiple times. */
19751 if (fixup_called)
19752 return;
19753
19754 /* If we found a reference attribute and the DIE has no name, try
19755 to find a name in the referred to DIE. */
19756
19757 if (raw_name == NULL && has_specification)
19758 {
19759 struct partial_die_info *spec_die;
19760
19761 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19762 spec_die = res.pdi;
19763 cu = res.cu;
19764
19765 spec_die->fixup (cu);
19766
19767 if (spec_die->raw_name)
19768 {
19769 raw_name = spec_die->raw_name;
19770 canonical_name = spec_die->canonical_name;
19771
19772 /* Copy DW_AT_external attribute if it is set. */
19773 if (spec_die->is_external)
19774 is_external = spec_die->is_external;
19775 }
19776 }
19777
19778 if (!has_const_value && has_specification
19779 && can_have_DW_AT_const_value_p (tag))
19780 {
19781 struct partial_die_info *spec_die;
19782
19783 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19784 spec_die = res.pdi;
19785 cu = res.cu;
19786
19787 spec_die->fixup (cu);
19788
19789 if (spec_die->has_const_value)
19790 {
19791 /* Copy DW_AT_const_value attribute if it is set. */
19792 has_const_value = spec_die->has_const_value;
19793 }
19794 }
19795
19796 /* Set default names for some unnamed DIEs. */
19797
19798 if (raw_name == NULL && tag == DW_TAG_namespace)
19799 {
19800 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19801 canonical_name = 1;
19802 }
19803
19804 /* If there is no parent die to provide a namespace, and there are
19805 children, see if we can determine the namespace from their linkage
19806 name. */
19807 if (cu->language == language_cplus
19808 && !cu->per_objfile->per_bfd->types.empty ()
19809 && die_parent == NULL
19810 && has_children
19811 && (tag == DW_TAG_class_type
19812 || tag == DW_TAG_structure_type
19813 || tag == DW_TAG_union_type))
19814 guess_partial_die_structure_name (this, cu);
19815
19816 /* GCC might emit a nameless struct or union that has a linkage
19817 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19818 if (raw_name == NULL
19819 && (tag == DW_TAG_class_type
19820 || tag == DW_TAG_interface_type
19821 || tag == DW_TAG_structure_type
19822 || tag == DW_TAG_union_type)
19823 && linkage_name != NULL)
19824 {
19825 gdb::unique_xmalloc_ptr<char> demangled
19826 (gdb_demangle (linkage_name, DMGL_TYPES));
19827 if (demangled != nullptr)
19828 {
19829 const char *base;
19830
19831 /* Strip any leading namespaces/classes, keep only the base name.
19832 DW_AT_name for named DIEs does not contain the prefixes. */
19833 base = strrchr (demangled.get (), ':');
19834 if (base && base > demangled.get () && base[-1] == ':')
19835 base++;
19836 else
19837 base = demangled.get ();
19838
19839 struct objfile *objfile = cu->per_objfile->objfile;
19840 raw_name = objfile->intern (base);
19841 canonical_name = 1;
19842 }
19843 }
19844
19845 fixup_called = 1;
19846 }
19847
19848 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19849 contents from the given SECTION in the HEADER.
19850
19851 HEADER_OFFSET is the offset of the header in the section. */
19852 static void
19853 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19854 struct dwarf2_section_info *section,
19855 sect_offset header_offset)
19856 {
19857 unsigned int bytes_read;
19858 bfd *abfd = section->get_bfd_owner ();
19859 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
19860
19861 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19862 info_ptr += bytes_read;
19863
19864 header->version = read_2_bytes (abfd, info_ptr);
19865 info_ptr += 2;
19866
19867 header->addr_size = read_1_byte (abfd, info_ptr);
19868 info_ptr += 1;
19869
19870 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19871 info_ptr += 1;
19872
19873 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19874 }
19875
19876 /* Return the DW_AT_loclists_base value for the CU. */
19877 static ULONGEST
19878 lookup_loclist_base (struct dwarf2_cu *cu)
19879 {
19880 /* For the .dwo unit, the loclist_base points to the first offset following
19881 the header. The header consists of the following entities-
19882 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19883 bit format)
19884 2. version (2 bytes)
19885 3. address size (1 byte)
19886 4. segment selector size (1 byte)
19887 5. offset entry count (4 bytes)
19888 These sizes are derived as per the DWARFv5 standard. */
19889 if (cu->dwo_unit != nullptr)
19890 {
19891 if (cu->header.initial_length_size == 4)
19892 return LOCLIST_HEADER_SIZE32;
19893 return LOCLIST_HEADER_SIZE64;
19894 }
19895 return cu->loclist_base;
19896 }
19897
19898 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19899 array of offsets in the .debug_loclists section. */
19900
19901 static sect_offset
19902 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19903 {
19904 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19905 struct objfile *objfile = per_objfile->objfile;
19906 bfd *abfd = objfile->obfd;
19907 ULONGEST loclist_header_size =
19908 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
19909 : LOCLIST_HEADER_SIZE64);
19910 ULONGEST loclist_base = lookup_loclist_base (cu);
19911
19912 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
19913 ULONGEST start_offset =
19914 loclist_base + loclist_index * cu->header.offset_size;
19915
19916 /* Get loclists section. */
19917 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19918
19919 /* Read the loclists section content. */
19920 section->read (objfile);
19921 if (section->buffer == NULL)
19922 error (_("DW_FORM_loclistx used without .debug_loclists "
19923 "section [in module %s]"), objfile_name (objfile));
19924
19925 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
19926 so if loclist_base is smaller than the header size, we have a problem. */
19927 if (loclist_base < loclist_header_size)
19928 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
19929 objfile_name (objfile));
19930
19931 /* Read the header of the loclists contribution. */
19932 struct loclists_rnglists_header header;
19933 read_loclists_rnglists_header (&header, section,
19934 (sect_offset) (loclist_base - loclist_header_size));
19935
19936 /* Verify the loclist index is valid. */
19937 if (loclist_index >= header.offset_entry_count)
19938 error (_("DW_FORM_loclistx pointing outside of "
19939 ".debug_loclists offset array [in module %s]"),
19940 objfile_name (objfile));
19941
19942 /* Validate that reading won't go beyond the end of the section. */
19943 if (start_offset + cu->header.offset_size > section->size)
19944 error (_("Reading DW_FORM_loclistx index beyond end of"
19945 ".debug_loclists section [in module %s]"),
19946 objfile_name (objfile));
19947
19948 const gdb_byte *info_ptr = section->buffer + start_offset;
19949
19950 if (cu->header.offset_size == 4)
19951 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
19952 else
19953 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
19954 }
19955
19956 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19957 array of offsets in the .debug_rnglists section. */
19958
19959 static sect_offset
19960 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19961 dwarf_tag tag)
19962 {
19963 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19964 struct objfile *objfile = dwarf2_per_objfile->objfile;
19965 bfd *abfd = objfile->obfd;
19966 ULONGEST rnglist_header_size =
19967 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19968 : RNGLIST_HEADER_SIZE64);
19969
19970 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
19971 .debug_rnglists.dwo section. The rnglists base given in the skeleton
19972 doesn't apply. */
19973 ULONGEST rnglist_base =
19974 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
19975
19976 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
19977 ULONGEST start_offset =
19978 rnglist_base + rnglist_index * cu->header.offset_size;
19979
19980 /* Get rnglists section. */
19981 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19982
19983 /* Read the rnglists section content. */
19984 section->read (objfile);
19985 if (section->buffer == nullptr)
19986 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19987 "[in module %s]"),
19988 objfile_name (objfile));
19989
19990 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
19991 so if rnglist_base is smaller than the header size, we have a problem. */
19992 if (rnglist_base < rnglist_header_size)
19993 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
19994 objfile_name (objfile));
19995
19996 /* Read the header of the rnglists contribution. */
19997 struct loclists_rnglists_header header;
19998 read_loclists_rnglists_header (&header, section,
19999 (sect_offset) (rnglist_base - rnglist_header_size));
20000
20001 /* Verify the rnglist index is valid. */
20002 if (rnglist_index >= header.offset_entry_count)
20003 error (_("DW_FORM_rnglistx index pointing outside of "
20004 ".debug_rnglists offset array [in module %s]"),
20005 objfile_name (objfile));
20006
20007 /* Validate that reading won't go beyond the end of the section. */
20008 if (start_offset + cu->header.offset_size > section->size)
20009 error (_("Reading DW_FORM_rnglistx index beyond end of"
20010 ".debug_rnglists section [in module %s]"),
20011 objfile_name (objfile));
20012
20013 const gdb_byte *info_ptr = section->buffer + start_offset;
20014
20015 if (cu->header.offset_size == 4)
20016 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
20017 else
20018 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
20019 }
20020
20021 /* Process the attributes that had to be skipped in the first round. These
20022 attributes are the ones that need str_offsets_base or addr_base attributes.
20023 They could not have been processed in the first round, because at the time
20024 the values of str_offsets_base or addr_base may not have been known. */
20025 static void
20026 read_attribute_reprocess (const struct die_reader_specs *reader,
20027 struct attribute *attr, dwarf_tag tag)
20028 {
20029 struct dwarf2_cu *cu = reader->cu;
20030 switch (attr->form)
20031 {
20032 case DW_FORM_addrx:
20033 case DW_FORM_GNU_addr_index:
20034 attr->set_address (read_addr_index (cu,
20035 attr->as_unsigned_reprocess ()));
20036 break;
20037 case DW_FORM_loclistx:
20038 {
20039 sect_offset loclists_sect_off
20040 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20041
20042 attr->set_unsigned (to_underlying (loclists_sect_off));
20043 }
20044 break;
20045 case DW_FORM_rnglistx:
20046 {
20047 sect_offset rnglists_sect_off
20048 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20049
20050 attr->set_unsigned (to_underlying (rnglists_sect_off));
20051 }
20052 break;
20053 case DW_FORM_strx:
20054 case DW_FORM_strx1:
20055 case DW_FORM_strx2:
20056 case DW_FORM_strx3:
20057 case DW_FORM_strx4:
20058 case DW_FORM_GNU_str_index:
20059 {
20060 unsigned int str_index = attr->as_unsigned_reprocess ();
20061 gdb_assert (!attr->canonical_string_p ());
20062 if (reader->dwo_file != NULL)
20063 attr->set_string_noncanonical (read_dwo_str_index (reader,
20064 str_index));
20065 else
20066 attr->set_string_noncanonical (read_stub_str_index (cu,
20067 str_index));
20068 break;
20069 }
20070 default:
20071 gdb_assert_not_reached (_("Unexpected DWARF form."));
20072 }
20073 }
20074
20075 /* Read an attribute value described by an attribute form. */
20076
20077 static const gdb_byte *
20078 read_attribute_value (const struct die_reader_specs *reader,
20079 struct attribute *attr, unsigned form,
20080 LONGEST implicit_const, const gdb_byte *info_ptr)
20081 {
20082 struct dwarf2_cu *cu = reader->cu;
20083 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20084 struct objfile *objfile = per_objfile->objfile;
20085 bfd *abfd = reader->abfd;
20086 struct comp_unit_head *cu_header = &cu->header;
20087 unsigned int bytes_read;
20088 struct dwarf_block *blk;
20089
20090 attr->form = (enum dwarf_form) form;
20091 switch (form)
20092 {
20093 case DW_FORM_ref_addr:
20094 if (cu_header->version == 2)
20095 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
20096 &bytes_read));
20097 else
20098 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20099 &bytes_read));
20100 info_ptr += bytes_read;
20101 break;
20102 case DW_FORM_GNU_ref_alt:
20103 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20104 &bytes_read));
20105 info_ptr += bytes_read;
20106 break;
20107 case DW_FORM_addr:
20108 {
20109 struct gdbarch *gdbarch = objfile->arch ();
20110 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
20111 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20112 attr->set_address (addr);
20113 info_ptr += bytes_read;
20114 }
20115 break;
20116 case DW_FORM_block2:
20117 blk = dwarf_alloc_block (cu);
20118 blk->size = read_2_bytes (abfd, info_ptr);
20119 info_ptr += 2;
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_block4:
20125 blk = dwarf_alloc_block (cu);
20126 blk->size = read_4_bytes (abfd, info_ptr);
20127 info_ptr += 4;
20128 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20129 info_ptr += blk->size;
20130 attr->set_block (blk);
20131 break;
20132 case DW_FORM_data2:
20133 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20134 info_ptr += 2;
20135 break;
20136 case DW_FORM_data4:
20137 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20138 info_ptr += 4;
20139 break;
20140 case DW_FORM_data8:
20141 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20142 info_ptr += 8;
20143 break;
20144 case DW_FORM_data16:
20145 blk = dwarf_alloc_block (cu);
20146 blk->size = 16;
20147 blk->data = read_n_bytes (abfd, info_ptr, 16);
20148 info_ptr += 16;
20149 attr->set_block (blk);
20150 break;
20151 case DW_FORM_sec_offset:
20152 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20153 &bytes_read));
20154 info_ptr += bytes_read;
20155 break;
20156 case DW_FORM_loclistx:
20157 {
20158 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20159 &bytes_read));
20160 info_ptr += bytes_read;
20161 }
20162 break;
20163 case DW_FORM_string:
20164 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20165 &bytes_read));
20166 info_ptr += bytes_read;
20167 break;
20168 case DW_FORM_strp:
20169 if (!cu->per_cu->is_dwz)
20170 {
20171 attr->set_string_noncanonical
20172 (read_indirect_string (per_objfile,
20173 abfd, info_ptr, cu_header,
20174 &bytes_read));
20175 info_ptr += bytes_read;
20176 break;
20177 }
20178 /* FALLTHROUGH */
20179 case DW_FORM_line_strp:
20180 if (!cu->per_cu->is_dwz)
20181 {
20182 attr->set_string_noncanonical
20183 (per_objfile->read_line_string (info_ptr, cu_header,
20184 &bytes_read));
20185 info_ptr += bytes_read;
20186 break;
20187 }
20188 /* FALLTHROUGH */
20189 case DW_FORM_GNU_strp_alt:
20190 {
20191 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20192 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20193 &bytes_read);
20194
20195 attr->set_string_noncanonical
20196 (dwz->read_string (objfile, str_offset));
20197 info_ptr += bytes_read;
20198 }
20199 break;
20200 case DW_FORM_exprloc:
20201 case DW_FORM_block:
20202 blk = dwarf_alloc_block (cu);
20203 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20204 info_ptr += bytes_read;
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_block1:
20210 blk = dwarf_alloc_block (cu);
20211 blk->size = read_1_byte (abfd, info_ptr);
20212 info_ptr += 1;
20213 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20214 info_ptr += blk->size;
20215 attr->set_block (blk);
20216 break;
20217 case DW_FORM_data1:
20218 case DW_FORM_flag:
20219 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20220 info_ptr += 1;
20221 break;
20222 case DW_FORM_flag_present:
20223 attr->set_unsigned (1);
20224 break;
20225 case DW_FORM_sdata:
20226 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20227 info_ptr += bytes_read;
20228 break;
20229 case DW_FORM_rnglistx:
20230 {
20231 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20232 &bytes_read));
20233 info_ptr += bytes_read;
20234 }
20235 break;
20236 case DW_FORM_udata:
20237 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20238 info_ptr += bytes_read;
20239 break;
20240 case DW_FORM_ref1:
20241 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20242 + read_1_byte (abfd, info_ptr)));
20243 info_ptr += 1;
20244 break;
20245 case DW_FORM_ref2:
20246 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20247 + read_2_bytes (abfd, info_ptr)));
20248 info_ptr += 2;
20249 break;
20250 case DW_FORM_ref4:
20251 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20252 + read_4_bytes (abfd, info_ptr)));
20253 info_ptr += 4;
20254 break;
20255 case DW_FORM_ref8:
20256 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20257 + read_8_bytes (abfd, info_ptr)));
20258 info_ptr += 8;
20259 break;
20260 case DW_FORM_ref_sig8:
20261 attr->set_signature (read_8_bytes (abfd, info_ptr));
20262 info_ptr += 8;
20263 break;
20264 case DW_FORM_ref_udata:
20265 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20266 + read_unsigned_leb128 (abfd, info_ptr,
20267 &bytes_read)));
20268 info_ptr += bytes_read;
20269 break;
20270 case DW_FORM_indirect:
20271 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20272 info_ptr += bytes_read;
20273 if (form == DW_FORM_implicit_const)
20274 {
20275 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20276 info_ptr += bytes_read;
20277 }
20278 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20279 info_ptr);
20280 break;
20281 case DW_FORM_implicit_const:
20282 attr->set_signed (implicit_const);
20283 break;
20284 case DW_FORM_addrx:
20285 case DW_FORM_GNU_addr_index:
20286 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20287 &bytes_read));
20288 info_ptr += bytes_read;
20289 break;
20290 case DW_FORM_strx:
20291 case DW_FORM_strx1:
20292 case DW_FORM_strx2:
20293 case DW_FORM_strx3:
20294 case DW_FORM_strx4:
20295 case DW_FORM_GNU_str_index:
20296 {
20297 ULONGEST str_index;
20298 if (form == DW_FORM_strx1)
20299 {
20300 str_index = read_1_byte (abfd, info_ptr);
20301 info_ptr += 1;
20302 }
20303 else if (form == DW_FORM_strx2)
20304 {
20305 str_index = read_2_bytes (abfd, info_ptr);
20306 info_ptr += 2;
20307 }
20308 else if (form == DW_FORM_strx3)
20309 {
20310 str_index = read_3_bytes (abfd, info_ptr);
20311 info_ptr += 3;
20312 }
20313 else if (form == DW_FORM_strx4)
20314 {
20315 str_index = read_4_bytes (abfd, info_ptr);
20316 info_ptr += 4;
20317 }
20318 else
20319 {
20320 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20321 info_ptr += bytes_read;
20322 }
20323 attr->set_unsigned_reprocess (str_index);
20324 }
20325 break;
20326 default:
20327 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20328 dwarf_form_name (form),
20329 bfd_get_filename (abfd));
20330 }
20331
20332 /* Super hack. */
20333 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20334 attr->form = DW_FORM_GNU_ref_alt;
20335
20336 /* We have seen instances where the compiler tried to emit a byte
20337 size attribute of -1 which ended up being encoded as an unsigned
20338 0xffffffff. Although 0xffffffff is technically a valid size value,
20339 an object of this size seems pretty unlikely so we can relatively
20340 safely treat these cases as if the size attribute was invalid and
20341 treat them as zero by default. */
20342 if (attr->name == DW_AT_byte_size
20343 && form == DW_FORM_data4
20344 && attr->as_unsigned () >= 0xffffffff)
20345 {
20346 complaint
20347 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20348 hex_string (attr->as_unsigned ()));
20349 attr->set_unsigned (0);
20350 }
20351
20352 return info_ptr;
20353 }
20354
20355 /* Read an attribute described by an abbreviated attribute. */
20356
20357 static const gdb_byte *
20358 read_attribute (const struct die_reader_specs *reader,
20359 struct attribute *attr, const struct attr_abbrev *abbrev,
20360 const gdb_byte *info_ptr)
20361 {
20362 attr->name = abbrev->name;
20363 attr->string_is_canonical = 0;
20364 attr->requires_reprocessing = 0;
20365 return read_attribute_value (reader, attr, abbrev->form,
20366 abbrev->implicit_const, info_ptr);
20367 }
20368
20369 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20370
20371 static const char *
20372 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20373 LONGEST str_offset)
20374 {
20375 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20376 str_offset, "DW_FORM_strp");
20377 }
20378
20379 /* Return pointer to string at .debug_str offset as read from BUF.
20380 BUF is assumed to be in a compilation unit described by CU_HEADER.
20381 Return *BYTES_READ_PTR count of bytes read from BUF. */
20382
20383 static const char *
20384 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20385 const gdb_byte *buf,
20386 const struct comp_unit_head *cu_header,
20387 unsigned int *bytes_read_ptr)
20388 {
20389 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20390
20391 return read_indirect_string_at_offset (per_objfile, str_offset);
20392 }
20393
20394 /* See read.h. */
20395
20396 const char *
20397 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20398 const struct comp_unit_head *cu_header,
20399 unsigned int *bytes_read_ptr)
20400 {
20401 bfd *abfd = objfile->obfd;
20402 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20403
20404 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20405 }
20406
20407 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20408 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20409 ADDR_SIZE is the size of addresses from the CU header. */
20410
20411 static CORE_ADDR
20412 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20413 gdb::optional<ULONGEST> addr_base, int addr_size)
20414 {
20415 struct objfile *objfile = per_objfile->objfile;
20416 bfd *abfd = objfile->obfd;
20417 const gdb_byte *info_ptr;
20418 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20419
20420 per_objfile->per_bfd->addr.read (objfile);
20421 if (per_objfile->per_bfd->addr.buffer == NULL)
20422 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20423 objfile_name (objfile));
20424 if (addr_base_or_zero + addr_index * addr_size
20425 >= per_objfile->per_bfd->addr.size)
20426 error (_("DW_FORM_addr_index pointing outside of "
20427 ".debug_addr section [in module %s]"),
20428 objfile_name (objfile));
20429 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20430 + addr_index * addr_size);
20431 if (addr_size == 4)
20432 return bfd_get_32 (abfd, info_ptr);
20433 else
20434 return bfd_get_64 (abfd, info_ptr);
20435 }
20436
20437 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20438
20439 static CORE_ADDR
20440 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20441 {
20442 return read_addr_index_1 (cu->per_objfile, addr_index,
20443 cu->addr_base, cu->header.addr_size);
20444 }
20445
20446 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20447
20448 static CORE_ADDR
20449 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20450 unsigned int *bytes_read)
20451 {
20452 bfd *abfd = cu->per_objfile->objfile->obfd;
20453 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20454
20455 return read_addr_index (cu, addr_index);
20456 }
20457
20458 /* See read.h. */
20459
20460 CORE_ADDR
20461 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20462 dwarf2_per_objfile *per_objfile,
20463 unsigned int addr_index)
20464 {
20465 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20466 gdb::optional<ULONGEST> addr_base;
20467 int addr_size;
20468
20469 /* We need addr_base and addr_size.
20470 If we don't have PER_CU->cu, we have to get it.
20471 Nasty, but the alternative is storing the needed info in PER_CU,
20472 which at this point doesn't seem justified: it's not clear how frequently
20473 it would get used and it would increase the size of every PER_CU.
20474 Entry points like dwarf2_per_cu_addr_size do a similar thing
20475 so we're not in uncharted territory here.
20476 Alas we need to be a bit more complicated as addr_base is contained
20477 in the DIE.
20478
20479 We don't need to read the entire CU(/TU).
20480 We just need the header and top level die.
20481
20482 IWBN to use the aging mechanism to let us lazily later discard the CU.
20483 For now we skip this optimization. */
20484
20485 if (cu != NULL)
20486 {
20487 addr_base = cu->addr_base;
20488 addr_size = cu->header.addr_size;
20489 }
20490 else
20491 {
20492 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20493 addr_base = reader.cu->addr_base;
20494 addr_size = reader.cu->header.addr_size;
20495 }
20496
20497 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20498 }
20499
20500 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20501 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20502 DWO file. */
20503
20504 static const char *
20505 read_str_index (struct dwarf2_cu *cu,
20506 struct dwarf2_section_info *str_section,
20507 struct dwarf2_section_info *str_offsets_section,
20508 ULONGEST str_offsets_base, ULONGEST str_index)
20509 {
20510 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20511 struct objfile *objfile = per_objfile->objfile;
20512 const char *objf_name = objfile_name (objfile);
20513 bfd *abfd = objfile->obfd;
20514 const gdb_byte *info_ptr;
20515 ULONGEST str_offset;
20516 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20517
20518 str_section->read (objfile);
20519 str_offsets_section->read (objfile);
20520 if (str_section->buffer == NULL)
20521 error (_("%s used without %s section"
20522 " in CU at offset %s [in module %s]"),
20523 form_name, str_section->get_name (),
20524 sect_offset_str (cu->header.sect_off), objf_name);
20525 if (str_offsets_section->buffer == NULL)
20526 error (_("%s used without %s section"
20527 " in CU at offset %s [in module %s]"),
20528 form_name, str_section->get_name (),
20529 sect_offset_str (cu->header.sect_off), objf_name);
20530 info_ptr = (str_offsets_section->buffer
20531 + str_offsets_base
20532 + str_index * cu->header.offset_size);
20533 if (cu->header.offset_size == 4)
20534 str_offset = bfd_get_32 (abfd, info_ptr);
20535 else
20536 str_offset = bfd_get_64 (abfd, info_ptr);
20537 if (str_offset >= str_section->size)
20538 error (_("Offset from %s pointing outside of"
20539 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20540 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20541 return (const char *) (str_section->buffer + str_offset);
20542 }
20543
20544 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20545
20546 static const char *
20547 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20548 {
20549 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20550 ? reader->cu->header.addr_size : 0;
20551 return read_str_index (reader->cu,
20552 &reader->dwo_file->sections.str,
20553 &reader->dwo_file->sections.str_offsets,
20554 str_offsets_base, str_index);
20555 }
20556
20557 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20558
20559 static const char *
20560 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20561 {
20562 struct objfile *objfile = cu->per_objfile->objfile;
20563 const char *objf_name = objfile_name (objfile);
20564 static const char form_name[] = "DW_FORM_GNU_str_index";
20565 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20566
20567 if (!cu->str_offsets_base.has_value ())
20568 error (_("%s used in Fission stub without %s"
20569 " in CU at offset 0x%lx [in module %s]"),
20570 form_name, str_offsets_attr_name,
20571 (long) cu->header.offset_size, objf_name);
20572
20573 return read_str_index (cu,
20574 &cu->per_objfile->per_bfd->str,
20575 &cu->per_objfile->per_bfd->str_offsets,
20576 *cu->str_offsets_base, str_index);
20577 }
20578
20579 /* Return the length of an LEB128 number in BUF. */
20580
20581 static int
20582 leb128_size (const gdb_byte *buf)
20583 {
20584 const gdb_byte *begin = buf;
20585 gdb_byte byte;
20586
20587 while (1)
20588 {
20589 byte = *buf++;
20590 if ((byte & 128) == 0)
20591 return buf - begin;
20592 }
20593 }
20594
20595 static void
20596 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20597 {
20598 switch (lang)
20599 {
20600 case DW_LANG_C89:
20601 case DW_LANG_C99:
20602 case DW_LANG_C11:
20603 case DW_LANG_C:
20604 case DW_LANG_UPC:
20605 cu->language = language_c;
20606 break;
20607 case DW_LANG_Java:
20608 case DW_LANG_C_plus_plus:
20609 case DW_LANG_C_plus_plus_11:
20610 case DW_LANG_C_plus_plus_14:
20611 cu->language = language_cplus;
20612 break;
20613 case DW_LANG_D:
20614 cu->language = language_d;
20615 break;
20616 case DW_LANG_Fortran77:
20617 case DW_LANG_Fortran90:
20618 case DW_LANG_Fortran95:
20619 case DW_LANG_Fortran03:
20620 case DW_LANG_Fortran08:
20621 cu->language = language_fortran;
20622 break;
20623 case DW_LANG_Go:
20624 cu->language = language_go;
20625 break;
20626 case DW_LANG_Mips_Assembler:
20627 cu->language = language_asm;
20628 break;
20629 case DW_LANG_Ada83:
20630 case DW_LANG_Ada95:
20631 cu->language = language_ada;
20632 break;
20633 case DW_LANG_Modula2:
20634 cu->language = language_m2;
20635 break;
20636 case DW_LANG_Pascal83:
20637 cu->language = language_pascal;
20638 break;
20639 case DW_LANG_ObjC:
20640 cu->language = language_objc;
20641 break;
20642 case DW_LANG_Rust:
20643 case DW_LANG_Rust_old:
20644 cu->language = language_rust;
20645 break;
20646 case DW_LANG_Cobol74:
20647 case DW_LANG_Cobol85:
20648 default:
20649 cu->language = language_minimal;
20650 break;
20651 }
20652 cu->language_defn = language_def (cu->language);
20653 }
20654
20655 /* Return the named attribute or NULL if not there. */
20656
20657 static struct attribute *
20658 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20659 {
20660 for (;;)
20661 {
20662 unsigned int i;
20663 struct attribute *spec = NULL;
20664
20665 for (i = 0; i < die->num_attrs; ++i)
20666 {
20667 if (die->attrs[i].name == name)
20668 return &die->attrs[i];
20669 if (die->attrs[i].name == DW_AT_specification
20670 || die->attrs[i].name == DW_AT_abstract_origin)
20671 spec = &die->attrs[i];
20672 }
20673
20674 if (!spec)
20675 break;
20676
20677 die = follow_die_ref (die, spec, &cu);
20678 }
20679
20680 return NULL;
20681 }
20682
20683 /* Return the string associated with a string-typed attribute, or NULL if it
20684 is either not found or is of an incorrect type. */
20685
20686 static const char *
20687 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20688 {
20689 struct attribute *attr;
20690 const char *str = NULL;
20691
20692 attr = dwarf2_attr (die, name, cu);
20693
20694 if (attr != NULL)
20695 {
20696 str = attr->as_string ();
20697 if (str == nullptr)
20698 complaint (_("string type expected for attribute %s for "
20699 "DIE at %s in module %s"),
20700 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20701 objfile_name (cu->per_objfile->objfile));
20702 }
20703
20704 return str;
20705 }
20706
20707 /* Return the dwo name or NULL if not present. If present, it is in either
20708 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20709 static const char *
20710 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20711 {
20712 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20713 if (dwo_name == nullptr)
20714 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20715 return dwo_name;
20716 }
20717
20718 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20719 and holds a non-zero value. This function should only be used for
20720 DW_FORM_flag or DW_FORM_flag_present attributes. */
20721
20722 static int
20723 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20724 {
20725 struct attribute *attr = dwarf2_attr (die, name, cu);
20726
20727 return attr != nullptr && attr->as_boolean ();
20728 }
20729
20730 static int
20731 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20732 {
20733 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20734 which value is non-zero. However, we have to be careful with
20735 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20736 (via dwarf2_flag_true_p) follows this attribute. So we may
20737 end up accidently finding a declaration attribute that belongs
20738 to a different DIE referenced by the specification attribute,
20739 even though the given DIE does not have a declaration attribute. */
20740 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20741 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20742 }
20743
20744 /* Return the die giving the specification for DIE, if there is
20745 one. *SPEC_CU is the CU containing DIE on input, and the CU
20746 containing the return value on output. If there is no
20747 specification, but there is an abstract origin, that is
20748 returned. */
20749
20750 static struct die_info *
20751 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20752 {
20753 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20754 *spec_cu);
20755
20756 if (spec_attr == NULL)
20757 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20758
20759 if (spec_attr == NULL)
20760 return NULL;
20761 else
20762 return follow_die_ref (die, spec_attr, spec_cu);
20763 }
20764
20765 /* Stub for free_line_header to match void * callback types. */
20766
20767 static void
20768 free_line_header_voidp (void *arg)
20769 {
20770 struct line_header *lh = (struct line_header *) arg;
20771
20772 delete lh;
20773 }
20774
20775 /* A convenience function to find the proper .debug_line section for a CU. */
20776
20777 static struct dwarf2_section_info *
20778 get_debug_line_section (struct dwarf2_cu *cu)
20779 {
20780 struct dwarf2_section_info *section;
20781 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20782
20783 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20784 DWO file. */
20785 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20786 section = &cu->dwo_unit->dwo_file->sections.line;
20787 else if (cu->per_cu->is_dwz)
20788 {
20789 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20790
20791 section = &dwz->line;
20792 }
20793 else
20794 section = &per_objfile->per_bfd->line;
20795
20796 return section;
20797 }
20798
20799 /* Read the statement program header starting at OFFSET in
20800 .debug_line, or .debug_line.dwo. Return a pointer
20801 to a struct line_header, allocated using xmalloc.
20802 Returns NULL if there is a problem reading the header, e.g., if it
20803 has a version we don't understand.
20804
20805 NOTE: the strings in the include directory and file name tables of
20806 the returned object point into the dwarf line section buffer,
20807 and must not be freed. */
20808
20809 static line_header_up
20810 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20811 {
20812 struct dwarf2_section_info *section;
20813 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20814
20815 section = get_debug_line_section (cu);
20816 section->read (per_objfile->objfile);
20817 if (section->buffer == NULL)
20818 {
20819 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20820 complaint (_("missing .debug_line.dwo section"));
20821 else
20822 complaint (_("missing .debug_line section"));
20823 return 0;
20824 }
20825
20826 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
20827 per_objfile, section, &cu->header);
20828 }
20829
20830 /* Subroutine of dwarf_decode_lines to simplify it.
20831 Return the file name of the psymtab for the given file_entry.
20832 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20833 If space for the result is malloc'd, *NAME_HOLDER will be set.
20834 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20835
20836 static const char *
20837 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20838 const dwarf2_psymtab *pst,
20839 const char *comp_dir,
20840 gdb::unique_xmalloc_ptr<char> *name_holder)
20841 {
20842 const char *include_name = fe.name;
20843 const char *include_name_to_compare = include_name;
20844 const char *pst_filename;
20845 int file_is_pst;
20846
20847 const char *dir_name = fe.include_dir (lh);
20848
20849 gdb::unique_xmalloc_ptr<char> hold_compare;
20850 if (!IS_ABSOLUTE_PATH (include_name)
20851 && (dir_name != NULL || comp_dir != NULL))
20852 {
20853 /* Avoid creating a duplicate psymtab for PST.
20854 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20855 Before we do the comparison, however, we need to account
20856 for DIR_NAME and COMP_DIR.
20857 First prepend dir_name (if non-NULL). If we still don't
20858 have an absolute path prepend comp_dir (if non-NULL).
20859 However, the directory we record in the include-file's
20860 psymtab does not contain COMP_DIR (to match the
20861 corresponding symtab(s)).
20862
20863 Example:
20864
20865 bash$ cd /tmp
20866 bash$ gcc -g ./hello.c
20867 include_name = "hello.c"
20868 dir_name = "."
20869 DW_AT_comp_dir = comp_dir = "/tmp"
20870 DW_AT_name = "./hello.c"
20871
20872 */
20873
20874 if (dir_name != NULL)
20875 {
20876 name_holder->reset (concat (dir_name, SLASH_STRING,
20877 include_name, (char *) NULL));
20878 include_name = name_holder->get ();
20879 include_name_to_compare = include_name;
20880 }
20881 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20882 {
20883 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20884 include_name, (char *) NULL));
20885 include_name_to_compare = hold_compare.get ();
20886 }
20887 }
20888
20889 pst_filename = pst->filename;
20890 gdb::unique_xmalloc_ptr<char> copied_name;
20891 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20892 {
20893 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20894 pst_filename, (char *) NULL));
20895 pst_filename = copied_name.get ();
20896 }
20897
20898 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20899
20900 if (file_is_pst)
20901 return NULL;
20902 return include_name;
20903 }
20904
20905 /* State machine to track the state of the line number program. */
20906
20907 class lnp_state_machine
20908 {
20909 public:
20910 /* Initialize a machine state for the start of a line number
20911 program. */
20912 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20913 bool record_lines_p);
20914
20915 file_entry *current_file ()
20916 {
20917 /* lh->file_names is 0-based, but the file name numbers in the
20918 statement program are 1-based. */
20919 return m_line_header->file_name_at (m_file);
20920 }
20921
20922 /* Record the line in the state machine. END_SEQUENCE is true if
20923 we're processing the end of a sequence. */
20924 void record_line (bool end_sequence);
20925
20926 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
20927 nop-out rest of the lines in this sequence. */
20928 void check_line_address (struct dwarf2_cu *cu,
20929 const gdb_byte *line_ptr,
20930 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20931
20932 void handle_set_discriminator (unsigned int discriminator)
20933 {
20934 m_discriminator = discriminator;
20935 m_line_has_non_zero_discriminator |= discriminator != 0;
20936 }
20937
20938 /* Handle DW_LNE_set_address. */
20939 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20940 {
20941 m_op_index = 0;
20942 address += baseaddr;
20943 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20944 }
20945
20946 /* Handle DW_LNS_advance_pc. */
20947 void handle_advance_pc (CORE_ADDR adjust);
20948
20949 /* Handle a special opcode. */
20950 void handle_special_opcode (unsigned char op_code);
20951
20952 /* Handle DW_LNS_advance_line. */
20953 void handle_advance_line (int line_delta)
20954 {
20955 advance_line (line_delta);
20956 }
20957
20958 /* Handle DW_LNS_set_file. */
20959 void handle_set_file (file_name_index file);
20960
20961 /* Handle DW_LNS_negate_stmt. */
20962 void handle_negate_stmt ()
20963 {
20964 m_is_stmt = !m_is_stmt;
20965 }
20966
20967 /* Handle DW_LNS_const_add_pc. */
20968 void handle_const_add_pc ();
20969
20970 /* Handle DW_LNS_fixed_advance_pc. */
20971 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20972 {
20973 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20974 m_op_index = 0;
20975 }
20976
20977 /* Handle DW_LNS_copy. */
20978 void handle_copy ()
20979 {
20980 record_line (false);
20981 m_discriminator = 0;
20982 }
20983
20984 /* Handle DW_LNE_end_sequence. */
20985 void handle_end_sequence ()
20986 {
20987 m_currently_recording_lines = true;
20988 }
20989
20990 private:
20991 /* Advance the line by LINE_DELTA. */
20992 void advance_line (int line_delta)
20993 {
20994 m_line += line_delta;
20995
20996 if (line_delta != 0)
20997 m_line_has_non_zero_discriminator = m_discriminator != 0;
20998 }
20999
21000 struct dwarf2_cu *m_cu;
21001
21002 gdbarch *m_gdbarch;
21003
21004 /* True if we're recording lines.
21005 Otherwise we're building partial symtabs and are just interested in
21006 finding include files mentioned by the line number program. */
21007 bool m_record_lines_p;
21008
21009 /* The line number header. */
21010 line_header *m_line_header;
21011
21012 /* These are part of the standard DWARF line number state machine,
21013 and initialized according to the DWARF spec. */
21014
21015 unsigned char m_op_index = 0;
21016 /* The line table index of the current file. */
21017 file_name_index m_file = 1;
21018 unsigned int m_line = 1;
21019
21020 /* These are initialized in the constructor. */
21021
21022 CORE_ADDR m_address;
21023 bool m_is_stmt;
21024 unsigned int m_discriminator;
21025
21026 /* Additional bits of state we need to track. */
21027
21028 /* The last file that we called dwarf2_start_subfile for.
21029 This is only used for TLLs. */
21030 unsigned int m_last_file = 0;
21031 /* The last file a line number was recorded for. */
21032 struct subfile *m_last_subfile = NULL;
21033
21034 /* The address of the last line entry. */
21035 CORE_ADDR m_last_address;
21036
21037 /* Set to true when a previous line at the same address (using
21038 m_last_address) had m_is_stmt true. This is reset to false when a
21039 line entry at a new address (m_address different to m_last_address) is
21040 processed. */
21041 bool m_stmt_at_address = false;
21042
21043 /* When true, record the lines we decode. */
21044 bool m_currently_recording_lines = false;
21045
21046 /* The last line number that was recorded, used to coalesce
21047 consecutive entries for the same line. This can happen, for
21048 example, when discriminators are present. PR 17276. */
21049 unsigned int m_last_line = 0;
21050 bool m_line_has_non_zero_discriminator = false;
21051 };
21052
21053 void
21054 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21055 {
21056 CORE_ADDR addr_adj = (((m_op_index + adjust)
21057 / m_line_header->maximum_ops_per_instruction)
21058 * m_line_header->minimum_instruction_length);
21059 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21060 m_op_index = ((m_op_index + adjust)
21061 % m_line_header->maximum_ops_per_instruction);
21062 }
21063
21064 void
21065 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21066 {
21067 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21068 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21069 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21070 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
21071 / m_line_header->maximum_ops_per_instruction)
21072 * m_line_header->minimum_instruction_length);
21073 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21074 m_op_index = ((m_op_index + adj_opcode_d)
21075 % m_line_header->maximum_ops_per_instruction);
21076
21077 int line_delta = m_line_header->line_base + adj_opcode_r;
21078 advance_line (line_delta);
21079 record_line (false);
21080 m_discriminator = 0;
21081 }
21082
21083 void
21084 lnp_state_machine::handle_set_file (file_name_index file)
21085 {
21086 m_file = file;
21087
21088 const file_entry *fe = current_file ();
21089 if (fe == NULL)
21090 dwarf2_debug_line_missing_file_complaint ();
21091 else if (m_record_lines_p)
21092 {
21093 const char *dir = fe->include_dir (m_line_header);
21094
21095 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21096 m_line_has_non_zero_discriminator = m_discriminator != 0;
21097 dwarf2_start_subfile (m_cu, fe->name, dir);
21098 }
21099 }
21100
21101 void
21102 lnp_state_machine::handle_const_add_pc ()
21103 {
21104 CORE_ADDR adjust
21105 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21106
21107 CORE_ADDR addr_adj
21108 = (((m_op_index + adjust)
21109 / m_line_header->maximum_ops_per_instruction)
21110 * m_line_header->minimum_instruction_length);
21111
21112 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21113 m_op_index = ((m_op_index + adjust)
21114 % m_line_header->maximum_ops_per_instruction);
21115 }
21116
21117 /* Return non-zero if we should add LINE to the line number table.
21118 LINE is the line to add, LAST_LINE is the last line that was added,
21119 LAST_SUBFILE is the subfile for LAST_LINE.
21120 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21121 had a non-zero discriminator.
21122
21123 We have to be careful in the presence of discriminators.
21124 E.g., for this line:
21125
21126 for (i = 0; i < 100000; i++);
21127
21128 clang can emit four line number entries for that one line,
21129 each with a different discriminator.
21130 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21131
21132 However, we want gdb to coalesce all four entries into one.
21133 Otherwise the user could stepi into the middle of the line and
21134 gdb would get confused about whether the pc really was in the
21135 middle of the line.
21136
21137 Things are further complicated by the fact that two consecutive
21138 line number entries for the same line is a heuristic used by gcc
21139 to denote the end of the prologue. So we can't just discard duplicate
21140 entries, we have to be selective about it. The heuristic we use is
21141 that we only collapse consecutive entries for the same line if at least
21142 one of those entries has a non-zero discriminator. PR 17276.
21143
21144 Note: Addresses in the line number state machine can never go backwards
21145 within one sequence, thus this coalescing is ok. */
21146
21147 static int
21148 dwarf_record_line_p (struct dwarf2_cu *cu,
21149 unsigned int line, unsigned int last_line,
21150 int line_has_non_zero_discriminator,
21151 struct subfile *last_subfile)
21152 {
21153 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21154 return 1;
21155 if (line != last_line)
21156 return 1;
21157 /* Same line for the same file that we've seen already.
21158 As a last check, for pr 17276, only record the line if the line
21159 has never had a non-zero discriminator. */
21160 if (!line_has_non_zero_discriminator)
21161 return 1;
21162 return 0;
21163 }
21164
21165 /* Use the CU's builder to record line number LINE beginning at
21166 address ADDRESS in the line table of subfile SUBFILE. */
21167
21168 static void
21169 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21170 unsigned int line, CORE_ADDR address, bool is_stmt,
21171 struct dwarf2_cu *cu)
21172 {
21173 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21174
21175 if (dwarf_line_debug)
21176 {
21177 fprintf_unfiltered (gdb_stdlog,
21178 "Recording line %u, file %s, address %s\n",
21179 line, lbasename (subfile->name),
21180 paddress (gdbarch, address));
21181 }
21182
21183 if (cu != nullptr)
21184 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21185 }
21186
21187 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21188 Mark the end of a set of line number records.
21189 The arguments are the same as for dwarf_record_line_1.
21190 If SUBFILE is NULL the request is ignored. */
21191
21192 static void
21193 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21194 CORE_ADDR address, struct dwarf2_cu *cu)
21195 {
21196 if (subfile == NULL)
21197 return;
21198
21199 if (dwarf_line_debug)
21200 {
21201 fprintf_unfiltered (gdb_stdlog,
21202 "Finishing current line, file %s, address %s\n",
21203 lbasename (subfile->name),
21204 paddress (gdbarch, address));
21205 }
21206
21207 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21208 }
21209
21210 void
21211 lnp_state_machine::record_line (bool end_sequence)
21212 {
21213 if (dwarf_line_debug)
21214 {
21215 fprintf_unfiltered (gdb_stdlog,
21216 "Processing actual line %u: file %u,"
21217 " address %s, is_stmt %u, discrim %u%s\n",
21218 m_line, m_file,
21219 paddress (m_gdbarch, m_address),
21220 m_is_stmt, m_discriminator,
21221 (end_sequence ? "\t(end sequence)" : ""));
21222 }
21223
21224 file_entry *fe = current_file ();
21225
21226 if (fe == NULL)
21227 dwarf2_debug_line_missing_file_complaint ();
21228 /* For now we ignore lines not starting on an instruction boundary.
21229 But not when processing end_sequence for compatibility with the
21230 previous version of the code. */
21231 else if (m_op_index == 0 || end_sequence)
21232 {
21233 fe->included_p = 1;
21234 if (m_record_lines_p)
21235 {
21236 /* When we switch files we insert an end maker in the first file,
21237 switch to the second file and add a new line entry. The
21238 problem is that the end marker inserted in the first file will
21239 discard any previous line entries at the same address. If the
21240 line entries in the first file are marked as is-stmt, while
21241 the new line in the second file is non-stmt, then this means
21242 the end marker will discard is-stmt lines so we can have a
21243 non-stmt line. This means that there are less addresses at
21244 which the user can insert a breakpoint.
21245
21246 To improve this we track the last address in m_last_address,
21247 and whether we have seen an is-stmt at this address. Then
21248 when switching files, if we have seen a stmt at the current
21249 address, and we are switching to create a non-stmt line, then
21250 discard the new line. */
21251 bool file_changed
21252 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21253 bool ignore_this_line
21254 = ((file_changed && !end_sequence && m_last_address == m_address
21255 && !m_is_stmt && m_stmt_at_address)
21256 || (!end_sequence && m_line == 0));
21257
21258 if ((file_changed && !ignore_this_line) || end_sequence)
21259 {
21260 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21261 m_currently_recording_lines ? m_cu : nullptr);
21262 }
21263
21264 if (!end_sequence && !ignore_this_line)
21265 {
21266 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21267
21268 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21269 m_line_has_non_zero_discriminator,
21270 m_last_subfile))
21271 {
21272 buildsym_compunit *builder = m_cu->get_builder ();
21273 dwarf_record_line_1 (m_gdbarch,
21274 builder->get_current_subfile (),
21275 m_line, m_address, is_stmt,
21276 m_currently_recording_lines ? m_cu : nullptr);
21277 }
21278 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21279 m_last_line = m_line;
21280 }
21281 }
21282 }
21283
21284 /* Track whether we have seen any m_is_stmt true at m_address in case we
21285 have multiple line table entries all at m_address. */
21286 if (m_last_address != m_address)
21287 {
21288 m_stmt_at_address = false;
21289 m_last_address = m_address;
21290 }
21291 m_stmt_at_address |= m_is_stmt;
21292 }
21293
21294 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21295 line_header *lh, bool record_lines_p)
21296 {
21297 m_cu = cu;
21298 m_gdbarch = arch;
21299 m_record_lines_p = record_lines_p;
21300 m_line_header = lh;
21301
21302 m_currently_recording_lines = true;
21303
21304 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21305 was a line entry for it so that the backend has a chance to adjust it
21306 and also record it in case it needs it. This is currently used by MIPS
21307 code, cf. `mips_adjust_dwarf2_line'. */
21308 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21309 m_is_stmt = lh->default_is_stmt;
21310 m_discriminator = 0;
21311
21312 m_last_address = m_address;
21313 m_stmt_at_address = false;
21314 }
21315
21316 void
21317 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21318 const gdb_byte *line_ptr,
21319 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21320 {
21321 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21322 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21323 located at 0x0. In this case, additionally check that if
21324 ADDRESS < UNRELOCATED_LOWPC. */
21325
21326 if ((address == 0 && address < unrelocated_lowpc)
21327 || address == (CORE_ADDR) -1)
21328 {
21329 /* This line table is for a function which has been
21330 GCd by the linker. Ignore it. PR gdb/12528 */
21331
21332 struct objfile *objfile = cu->per_objfile->objfile;
21333 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21334
21335 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21336 line_offset, objfile_name (objfile));
21337 m_currently_recording_lines = false;
21338 /* Note: m_currently_recording_lines is left as false until we see
21339 DW_LNE_end_sequence. */
21340 }
21341 }
21342
21343 /* Subroutine of dwarf_decode_lines to simplify it.
21344 Process the line number information in LH.
21345 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21346 program in order to set included_p for every referenced header. */
21347
21348 static void
21349 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21350 const int decode_for_pst_p, CORE_ADDR lowpc)
21351 {
21352 const gdb_byte *line_ptr, *extended_end;
21353 const gdb_byte *line_end;
21354 unsigned int bytes_read, extended_len;
21355 unsigned char op_code, extended_op;
21356 CORE_ADDR baseaddr;
21357 struct objfile *objfile = cu->per_objfile->objfile;
21358 bfd *abfd = objfile->obfd;
21359 struct gdbarch *gdbarch = objfile->arch ();
21360 /* True if we're recording line info (as opposed to building partial
21361 symtabs and just interested in finding include files mentioned by
21362 the line number program). */
21363 bool record_lines_p = !decode_for_pst_p;
21364
21365 baseaddr = objfile->text_section_offset ();
21366
21367 line_ptr = lh->statement_program_start;
21368 line_end = lh->statement_program_end;
21369
21370 /* Read the statement sequences until there's nothing left. */
21371 while (line_ptr < line_end)
21372 {
21373 /* The DWARF line number program state machine. Reset the state
21374 machine at the start of each sequence. */
21375 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21376 bool end_sequence = false;
21377
21378 if (record_lines_p)
21379 {
21380 /* Start a subfile for the current file of the state
21381 machine. */
21382 const file_entry *fe = state_machine.current_file ();
21383
21384 if (fe != NULL)
21385 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21386 }
21387
21388 /* Decode the table. */
21389 while (line_ptr < line_end && !end_sequence)
21390 {
21391 op_code = read_1_byte (abfd, line_ptr);
21392 line_ptr += 1;
21393
21394 if (op_code >= lh->opcode_base)
21395 {
21396 /* Special opcode. */
21397 state_machine.handle_special_opcode (op_code);
21398 }
21399 else switch (op_code)
21400 {
21401 case DW_LNS_extended_op:
21402 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21403 &bytes_read);
21404 line_ptr += bytes_read;
21405 extended_end = line_ptr + extended_len;
21406 extended_op = read_1_byte (abfd, line_ptr);
21407 line_ptr += 1;
21408 if (DW_LNE_lo_user <= extended_op
21409 && extended_op <= DW_LNE_hi_user)
21410 {
21411 /* Vendor extension, ignore. */
21412 line_ptr = extended_end;
21413 break;
21414 }
21415 switch (extended_op)
21416 {
21417 case DW_LNE_end_sequence:
21418 state_machine.handle_end_sequence ();
21419 end_sequence = true;
21420 break;
21421 case DW_LNE_set_address:
21422 {
21423 CORE_ADDR address
21424 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21425 line_ptr += bytes_read;
21426
21427 state_machine.check_line_address (cu, line_ptr,
21428 lowpc - baseaddr, address);
21429 state_machine.handle_set_address (baseaddr, address);
21430 }
21431 break;
21432 case DW_LNE_define_file:
21433 {
21434 const char *cur_file;
21435 unsigned int mod_time, length;
21436 dir_index dindex;
21437
21438 cur_file = read_direct_string (abfd, line_ptr,
21439 &bytes_read);
21440 line_ptr += bytes_read;
21441 dindex = (dir_index)
21442 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21443 line_ptr += bytes_read;
21444 mod_time =
21445 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21446 line_ptr += bytes_read;
21447 length =
21448 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21449 line_ptr += bytes_read;
21450 lh->add_file_name (cur_file, dindex, mod_time, length);
21451 }
21452 break;
21453 case DW_LNE_set_discriminator:
21454 {
21455 /* The discriminator is not interesting to the
21456 debugger; just ignore it. We still need to
21457 check its value though:
21458 if there are consecutive entries for the same
21459 (non-prologue) line we want to coalesce them.
21460 PR 17276. */
21461 unsigned int discr
21462 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21463 line_ptr += bytes_read;
21464
21465 state_machine.handle_set_discriminator (discr);
21466 }
21467 break;
21468 default:
21469 complaint (_("mangled .debug_line section"));
21470 return;
21471 }
21472 /* Make sure that we parsed the extended op correctly. If e.g.
21473 we expected a different address size than the producer used,
21474 we may have read the wrong number of bytes. */
21475 if (line_ptr != extended_end)
21476 {
21477 complaint (_("mangled .debug_line section"));
21478 return;
21479 }
21480 break;
21481 case DW_LNS_copy:
21482 state_machine.handle_copy ();
21483 break;
21484 case DW_LNS_advance_pc:
21485 {
21486 CORE_ADDR adjust
21487 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21488 line_ptr += bytes_read;
21489
21490 state_machine.handle_advance_pc (adjust);
21491 }
21492 break;
21493 case DW_LNS_advance_line:
21494 {
21495 int line_delta
21496 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21497 line_ptr += bytes_read;
21498
21499 state_machine.handle_advance_line (line_delta);
21500 }
21501 break;
21502 case DW_LNS_set_file:
21503 {
21504 file_name_index file
21505 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21506 &bytes_read);
21507 line_ptr += bytes_read;
21508
21509 state_machine.handle_set_file (file);
21510 }
21511 break;
21512 case DW_LNS_set_column:
21513 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21514 line_ptr += bytes_read;
21515 break;
21516 case DW_LNS_negate_stmt:
21517 state_machine.handle_negate_stmt ();
21518 break;
21519 case DW_LNS_set_basic_block:
21520 break;
21521 /* Add to the address register of the state machine the
21522 address increment value corresponding to special opcode
21523 255. I.e., this value is scaled by the minimum
21524 instruction length since special opcode 255 would have
21525 scaled the increment. */
21526 case DW_LNS_const_add_pc:
21527 state_machine.handle_const_add_pc ();
21528 break;
21529 case DW_LNS_fixed_advance_pc:
21530 {
21531 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21532 line_ptr += 2;
21533
21534 state_machine.handle_fixed_advance_pc (addr_adj);
21535 }
21536 break;
21537 default:
21538 {
21539 /* Unknown standard opcode, ignore it. */
21540 int i;
21541
21542 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21543 {
21544 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21545 line_ptr += bytes_read;
21546 }
21547 }
21548 }
21549 }
21550
21551 if (!end_sequence)
21552 dwarf2_debug_line_missing_end_sequence_complaint ();
21553
21554 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21555 in which case we still finish recording the last line). */
21556 state_machine.record_line (true);
21557 }
21558 }
21559
21560 /* Decode the Line Number Program (LNP) for the given line_header
21561 structure and CU. The actual information extracted and the type
21562 of structures created from the LNP depends on the value of PST.
21563
21564 1. If PST is NULL, then this procedure uses the data from the program
21565 to create all necessary symbol tables, and their linetables.
21566
21567 2. If PST is not NULL, this procedure reads the program to determine
21568 the list of files included by the unit represented by PST, and
21569 builds all the associated partial symbol tables.
21570
21571 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21572 It is used for relative paths in the line table.
21573 NOTE: When processing partial symtabs (pst != NULL),
21574 comp_dir == pst->dirname.
21575
21576 NOTE: It is important that psymtabs have the same file name (via strcmp)
21577 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21578 symtab we don't use it in the name of the psymtabs we create.
21579 E.g. expand_line_sal requires this when finding psymtabs to expand.
21580 A good testcase for this is mb-inline.exp.
21581
21582 LOWPC is the lowest address in CU (or 0 if not known).
21583
21584 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21585 for its PC<->lines mapping information. Otherwise only the filename
21586 table is read in. */
21587
21588 static void
21589 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21590 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21591 CORE_ADDR lowpc, int decode_mapping)
21592 {
21593 struct objfile *objfile = cu->per_objfile->objfile;
21594 const int decode_for_pst_p = (pst != NULL);
21595
21596 if (decode_mapping)
21597 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21598
21599 if (decode_for_pst_p)
21600 {
21601 /* Now that we're done scanning the Line Header Program, we can
21602 create the psymtab of each included file. */
21603 for (auto &file_entry : lh->file_names ())
21604 if (file_entry.included_p == 1)
21605 {
21606 gdb::unique_xmalloc_ptr<char> name_holder;
21607 const char *include_name =
21608 psymtab_include_file_name (lh, file_entry, pst,
21609 comp_dir, &name_holder);
21610 if (include_name != NULL)
21611 dwarf2_create_include_psymtab
21612 (cu->per_objfile->per_bfd, include_name, pst,
21613 cu->per_objfile->per_bfd->partial_symtabs.get (),
21614 objfile->per_bfd);
21615 }
21616 }
21617 else
21618 {
21619 /* Make sure a symtab is created for every file, even files
21620 which contain only variables (i.e. no code with associated
21621 line numbers). */
21622 buildsym_compunit *builder = cu->get_builder ();
21623 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21624
21625 for (auto &fe : lh->file_names ())
21626 {
21627 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21628 if (builder->get_current_subfile ()->symtab == NULL)
21629 {
21630 builder->get_current_subfile ()->symtab
21631 = allocate_symtab (cust,
21632 builder->get_current_subfile ()->name);
21633 }
21634 fe.symtab = builder->get_current_subfile ()->symtab;
21635 }
21636 }
21637 }
21638
21639 /* Start a subfile for DWARF. FILENAME is the name of the file and
21640 DIRNAME the name of the source directory which contains FILENAME
21641 or NULL if not known.
21642 This routine tries to keep line numbers from identical absolute and
21643 relative file names in a common subfile.
21644
21645 Using the `list' example from the GDB testsuite, which resides in
21646 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21647 of /srcdir/list0.c yields the following debugging information for list0.c:
21648
21649 DW_AT_name: /srcdir/list0.c
21650 DW_AT_comp_dir: /compdir
21651 files.files[0].name: list0.h
21652 files.files[0].dir: /srcdir
21653 files.files[1].name: list0.c
21654 files.files[1].dir: /srcdir
21655
21656 The line number information for list0.c has to end up in a single
21657 subfile, so that `break /srcdir/list0.c:1' works as expected.
21658 start_subfile will ensure that this happens provided that we pass the
21659 concatenation of files.files[1].dir and files.files[1].name as the
21660 subfile's name. */
21661
21662 static void
21663 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21664 const char *dirname)
21665 {
21666 gdb::unique_xmalloc_ptr<char> copy;
21667
21668 /* In order not to lose the line information directory,
21669 we concatenate it to the filename when it makes sense.
21670 Note that the Dwarf3 standard says (speaking of filenames in line
21671 information): ``The directory index is ignored for file names
21672 that represent full path names''. Thus ignoring dirname in the
21673 `else' branch below isn't an issue. */
21674
21675 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21676 {
21677 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21678 filename = copy.get ();
21679 }
21680
21681 cu->get_builder ()->start_subfile (filename);
21682 }
21683
21684 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21685 buildsym_compunit constructor. */
21686
21687 struct compunit_symtab *
21688 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21689 CORE_ADDR low_pc)
21690 {
21691 gdb_assert (m_builder == nullptr);
21692
21693 m_builder.reset (new struct buildsym_compunit
21694 (this->per_objfile->objfile,
21695 name, comp_dir, language, low_pc));
21696
21697 list_in_scope = get_builder ()->get_file_symbols ();
21698
21699 get_builder ()->record_debugformat ("DWARF 2");
21700 get_builder ()->record_producer (producer);
21701
21702 processing_has_namespace_info = false;
21703
21704 return get_builder ()->get_compunit_symtab ();
21705 }
21706
21707 static void
21708 var_decode_location (struct attribute *attr, struct symbol *sym,
21709 struct dwarf2_cu *cu)
21710 {
21711 struct objfile *objfile = cu->per_objfile->objfile;
21712 struct comp_unit_head *cu_header = &cu->header;
21713
21714 /* NOTE drow/2003-01-30: There used to be a comment and some special
21715 code here to turn a symbol with DW_AT_external and a
21716 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21717 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21718 with some versions of binutils) where shared libraries could have
21719 relocations against symbols in their debug information - the
21720 minimal symbol would have the right address, but the debug info
21721 would not. It's no longer necessary, because we will explicitly
21722 apply relocations when we read in the debug information now. */
21723
21724 /* A DW_AT_location attribute with no contents indicates that a
21725 variable has been optimized away. */
21726 if (attr->form_is_block () && attr->as_block ()->size == 0)
21727 {
21728 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21729 return;
21730 }
21731
21732 /* Handle one degenerate form of location expression specially, to
21733 preserve GDB's previous behavior when section offsets are
21734 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21735 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21736
21737 if (attr->form_is_block ())
21738 {
21739 struct dwarf_block *block = attr->as_block ();
21740
21741 if ((block->data[0] == DW_OP_addr
21742 && block->size == 1 + cu_header->addr_size)
21743 || ((block->data[0] == DW_OP_GNU_addr_index
21744 || block->data[0] == DW_OP_addrx)
21745 && (block->size
21746 == 1 + leb128_size (&block->data[1]))))
21747 {
21748 unsigned int dummy;
21749
21750 if (block->data[0] == DW_OP_addr)
21751 SET_SYMBOL_VALUE_ADDRESS
21752 (sym, cu->header.read_address (objfile->obfd,
21753 block->data + 1,
21754 &dummy));
21755 else
21756 SET_SYMBOL_VALUE_ADDRESS
21757 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21758 &dummy));
21759 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21760 fixup_symbol_section (sym, objfile);
21761 SET_SYMBOL_VALUE_ADDRESS
21762 (sym,
21763 SYMBOL_VALUE_ADDRESS (sym)
21764 + objfile->section_offsets[sym->section_index ()]);
21765 return;
21766 }
21767 }
21768
21769 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21770 expression evaluator, and use LOC_COMPUTED only when necessary
21771 (i.e. when the value of a register or memory location is
21772 referenced, or a thread-local block, etc.). Then again, it might
21773 not be worthwhile. I'm assuming that it isn't unless performance
21774 or memory numbers show me otherwise. */
21775
21776 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21777
21778 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21779 cu->has_loclist = true;
21780 }
21781
21782 /* Given a pointer to a DWARF information entry, figure out if we need
21783 to make a symbol table entry for it, and if so, create a new entry
21784 and return a pointer to it.
21785 If TYPE is NULL, determine symbol type from the die, otherwise
21786 used the passed type.
21787 If SPACE is not NULL, use it to hold the new symbol. If it is
21788 NULL, allocate a new symbol on the objfile's obstack. */
21789
21790 static struct symbol *
21791 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21792 struct symbol *space)
21793 {
21794 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21795 struct objfile *objfile = per_objfile->objfile;
21796 struct gdbarch *gdbarch = objfile->arch ();
21797 struct symbol *sym = NULL;
21798 const char *name;
21799 struct attribute *attr = NULL;
21800 struct attribute *attr2 = NULL;
21801 CORE_ADDR baseaddr;
21802 struct pending **list_to_add = NULL;
21803
21804 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21805
21806 baseaddr = objfile->text_section_offset ();
21807
21808 name = dwarf2_name (die, cu);
21809 if (name)
21810 {
21811 int suppress_add = 0;
21812
21813 if (space)
21814 sym = space;
21815 else
21816 sym = new (&objfile->objfile_obstack) symbol;
21817 OBJSTAT (objfile, n_syms++);
21818
21819 /* Cache this symbol's name and the name's demangled form (if any). */
21820 sym->set_language (cu->language, &objfile->objfile_obstack);
21821 /* Fortran does not have mangling standard and the mangling does differ
21822 between gfortran, iFort etc. */
21823 const char *physname
21824 = (cu->language == language_fortran
21825 ? dwarf2_full_name (name, die, cu)
21826 : dwarf2_physname (name, die, cu));
21827 const char *linkagename = dw2_linkage_name (die, cu);
21828
21829 if (linkagename == nullptr || cu->language == language_ada)
21830 sym->set_linkage_name (physname);
21831 else
21832 {
21833 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21834 sym->set_linkage_name (linkagename);
21835 }
21836
21837 /* Default assumptions.
21838 Use the passed type or decode it from the die. */
21839 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21840 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21841 if (type != NULL)
21842 SYMBOL_TYPE (sym) = type;
21843 else
21844 SYMBOL_TYPE (sym) = die_type (die, cu);
21845 attr = dwarf2_attr (die,
21846 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21847 cu);
21848 if (attr != nullptr)
21849 SYMBOL_LINE (sym) = attr->constant_value (0);
21850
21851 attr = dwarf2_attr (die,
21852 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21853 cu);
21854 if (attr != nullptr && attr->is_nonnegative ())
21855 {
21856 file_name_index file_index
21857 = (file_name_index) attr->as_nonnegative ();
21858 struct file_entry *fe;
21859
21860 if (cu->line_header != NULL)
21861 fe = cu->line_header->file_name_at (file_index);
21862 else
21863 fe = NULL;
21864
21865 if (fe == NULL)
21866 complaint (_("file index out of range"));
21867 else
21868 symbol_set_symtab (sym, fe->symtab);
21869 }
21870
21871 switch (die->tag)
21872 {
21873 case DW_TAG_label:
21874 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21875 if (attr != nullptr)
21876 {
21877 CORE_ADDR addr;
21878
21879 addr = attr->as_address ();
21880 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21881 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21882 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21883 }
21884 else
21885 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21886 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21887 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21888 add_symbol_to_list (sym, cu->list_in_scope);
21889 break;
21890 case DW_TAG_subprogram:
21891 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21892 finish_block. */
21893 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21894 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21895 if ((attr2 != nullptr && attr2->as_boolean ())
21896 || cu->language == language_ada
21897 || cu->language == language_fortran)
21898 {
21899 /* Subprograms marked external are stored as a global symbol.
21900 Ada and Fortran subprograms, whether marked external or
21901 not, are always stored as a global symbol, because we want
21902 to be able to access them globally. For instance, we want
21903 to be able to break on a nested subprogram without having
21904 to specify the context. */
21905 list_to_add = cu->get_builder ()->get_global_symbols ();
21906 }
21907 else
21908 {
21909 list_to_add = cu->list_in_scope;
21910 }
21911 break;
21912 case DW_TAG_inlined_subroutine:
21913 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21914 finish_block. */
21915 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21916 SYMBOL_INLINED (sym) = 1;
21917 list_to_add = cu->list_in_scope;
21918 break;
21919 case DW_TAG_template_value_param:
21920 suppress_add = 1;
21921 /* Fall through. */
21922 case DW_TAG_constant:
21923 case DW_TAG_variable:
21924 case DW_TAG_member:
21925 /* Compilation with minimal debug info may result in
21926 variables with missing type entries. Change the
21927 misleading `void' type to something sensible. */
21928 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
21929 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21930
21931 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21932 /* In the case of DW_TAG_member, we should only be called for
21933 static const members. */
21934 if (die->tag == DW_TAG_member)
21935 {
21936 /* dwarf2_add_field uses die_is_declaration,
21937 so we do the same. */
21938 gdb_assert (die_is_declaration (die, cu));
21939 gdb_assert (attr);
21940 }
21941 if (attr != nullptr)
21942 {
21943 dwarf2_const_value (attr, sym, cu);
21944 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21945 if (!suppress_add)
21946 {
21947 if (attr2 != nullptr && attr2->as_boolean ())
21948 list_to_add = cu->get_builder ()->get_global_symbols ();
21949 else
21950 list_to_add = cu->list_in_scope;
21951 }
21952 break;
21953 }
21954 attr = dwarf2_attr (die, DW_AT_location, cu);
21955 if (attr != nullptr)
21956 {
21957 var_decode_location (attr, sym, cu);
21958 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21959
21960 /* Fortran explicitly imports any global symbols to the local
21961 scope by DW_TAG_common_block. */
21962 if (cu->language == language_fortran && die->parent
21963 && die->parent->tag == DW_TAG_common_block)
21964 attr2 = NULL;
21965
21966 if (SYMBOL_CLASS (sym) == LOC_STATIC
21967 && SYMBOL_VALUE_ADDRESS (sym) == 0
21968 && !per_objfile->per_bfd->has_section_at_zero)
21969 {
21970 /* When a static variable is eliminated by the linker,
21971 the corresponding debug information is not stripped
21972 out, but the variable address is set to null;
21973 do not add such variables into symbol table. */
21974 }
21975 else if (attr2 != nullptr && attr2->as_boolean ())
21976 {
21977 if (SYMBOL_CLASS (sym) == LOC_STATIC
21978 && (objfile->flags & OBJF_MAINLINE) == 0
21979 && per_objfile->per_bfd->can_copy)
21980 {
21981 /* A global static variable might be subject to
21982 copy relocation. We first check for a local
21983 minsym, though, because maybe the symbol was
21984 marked hidden, in which case this would not
21985 apply. */
21986 bound_minimal_symbol found
21987 = (lookup_minimal_symbol_linkage
21988 (sym->linkage_name (), objfile));
21989 if (found.minsym != nullptr)
21990 sym->maybe_copied = 1;
21991 }
21992
21993 /* A variable with DW_AT_external is never static,
21994 but it may be block-scoped. */
21995 list_to_add
21996 = ((cu->list_in_scope
21997 == cu->get_builder ()->get_file_symbols ())
21998 ? cu->get_builder ()->get_global_symbols ()
21999 : cu->list_in_scope);
22000 }
22001 else
22002 list_to_add = cu->list_in_scope;
22003 }
22004 else
22005 {
22006 /* We do not know the address of this symbol.
22007 If it is an external symbol and we have type information
22008 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22009 The address of the variable will then be determined from
22010 the minimal symbol table whenever the variable is
22011 referenced. */
22012 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22013
22014 /* Fortran explicitly imports any global symbols to the local
22015 scope by DW_TAG_common_block. */
22016 if (cu->language == language_fortran && die->parent
22017 && die->parent->tag == DW_TAG_common_block)
22018 {
22019 /* SYMBOL_CLASS doesn't matter here because
22020 read_common_block is going to reset it. */
22021 if (!suppress_add)
22022 list_to_add = cu->list_in_scope;
22023 }
22024 else if (attr2 != nullptr && attr2->as_boolean ()
22025 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22026 {
22027 /* A variable with DW_AT_external is never static, but it
22028 may be block-scoped. */
22029 list_to_add
22030 = ((cu->list_in_scope
22031 == cu->get_builder ()->get_file_symbols ())
22032 ? cu->get_builder ()->get_global_symbols ()
22033 : cu->list_in_scope);
22034
22035 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22036 }
22037 else if (!die_is_declaration (die, cu))
22038 {
22039 /* Use the default LOC_OPTIMIZED_OUT class. */
22040 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22041 if (!suppress_add)
22042 list_to_add = cu->list_in_scope;
22043 }
22044 }
22045 break;
22046 case DW_TAG_formal_parameter:
22047 {
22048 /* If we are inside a function, mark this as an argument. If
22049 not, we might be looking at an argument to an inlined function
22050 when we do not have enough information to show inlined frames;
22051 pretend it's a local variable in that case so that the user can
22052 still see it. */
22053 struct context_stack *curr
22054 = cu->get_builder ()->get_current_context_stack ();
22055 if (curr != nullptr && curr->name != nullptr)
22056 SYMBOL_IS_ARGUMENT (sym) = 1;
22057 attr = dwarf2_attr (die, DW_AT_location, cu);
22058 if (attr != nullptr)
22059 {
22060 var_decode_location (attr, sym, cu);
22061 }
22062 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22063 if (attr != nullptr)
22064 {
22065 dwarf2_const_value (attr, sym, cu);
22066 }
22067
22068 list_to_add = cu->list_in_scope;
22069 }
22070 break;
22071 case DW_TAG_unspecified_parameters:
22072 /* From varargs functions; gdb doesn't seem to have any
22073 interest in this information, so just ignore it for now.
22074 (FIXME?) */
22075 break;
22076 case DW_TAG_template_type_param:
22077 suppress_add = 1;
22078 /* Fall through. */
22079 case DW_TAG_class_type:
22080 case DW_TAG_interface_type:
22081 case DW_TAG_structure_type:
22082 case DW_TAG_union_type:
22083 case DW_TAG_set_type:
22084 case DW_TAG_enumeration_type:
22085 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22086 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22087
22088 {
22089 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22090 really ever be static objects: otherwise, if you try
22091 to, say, break of a class's method and you're in a file
22092 which doesn't mention that class, it won't work unless
22093 the check for all static symbols in lookup_symbol_aux
22094 saves you. See the OtherFileClass tests in
22095 gdb.c++/namespace.exp. */
22096
22097 if (!suppress_add)
22098 {
22099 buildsym_compunit *builder = cu->get_builder ();
22100 list_to_add
22101 = (cu->list_in_scope == builder->get_file_symbols ()
22102 && cu->language == language_cplus
22103 ? builder->get_global_symbols ()
22104 : cu->list_in_scope);
22105
22106 /* The semantics of C++ state that "struct foo {
22107 ... }" also defines a typedef for "foo". */
22108 if (cu->language == language_cplus
22109 || cu->language == language_ada
22110 || cu->language == language_d
22111 || cu->language == language_rust)
22112 {
22113 /* The symbol's name is already allocated along
22114 with this objfile, so we don't need to
22115 duplicate it for the type. */
22116 if (SYMBOL_TYPE (sym)->name () == 0)
22117 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
22118 }
22119 }
22120 }
22121 break;
22122 case DW_TAG_typedef:
22123 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22124 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22125 list_to_add = cu->list_in_scope;
22126 break;
22127 case DW_TAG_array_type:
22128 case DW_TAG_base_type:
22129 case DW_TAG_subrange_type:
22130 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22131 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22132 list_to_add = cu->list_in_scope;
22133 break;
22134 case DW_TAG_enumerator:
22135 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22136 if (attr != nullptr)
22137 {
22138 dwarf2_const_value (attr, sym, cu);
22139 }
22140 {
22141 /* NOTE: carlton/2003-11-10: See comment above in the
22142 DW_TAG_class_type, etc. block. */
22143
22144 list_to_add
22145 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22146 && cu->language == language_cplus
22147 ? cu->get_builder ()->get_global_symbols ()
22148 : cu->list_in_scope);
22149 }
22150 break;
22151 case DW_TAG_imported_declaration:
22152 case DW_TAG_namespace:
22153 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22154 list_to_add = cu->get_builder ()->get_global_symbols ();
22155 break;
22156 case DW_TAG_module:
22157 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22158 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22159 list_to_add = cu->get_builder ()->get_global_symbols ();
22160 break;
22161 case DW_TAG_common_block:
22162 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22163 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22164 add_symbol_to_list (sym, cu->list_in_scope);
22165 break;
22166 default:
22167 /* Not a tag we recognize. Hopefully we aren't processing
22168 trash data, but since we must specifically ignore things
22169 we don't recognize, there is nothing else we should do at
22170 this point. */
22171 complaint (_("unsupported tag: '%s'"),
22172 dwarf_tag_name (die->tag));
22173 break;
22174 }
22175
22176 if (suppress_add)
22177 {
22178 sym->hash_next = objfile->template_symbols;
22179 objfile->template_symbols = sym;
22180 list_to_add = NULL;
22181 }
22182
22183 if (list_to_add != NULL)
22184 add_symbol_to_list (sym, list_to_add);
22185
22186 /* For the benefit of old versions of GCC, check for anonymous
22187 namespaces based on the demangled name. */
22188 if (!cu->processing_has_namespace_info
22189 && cu->language == language_cplus)
22190 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22191 }
22192 return (sym);
22193 }
22194
22195 /* Given an attr with a DW_FORM_dataN value in host byte order,
22196 zero-extend it as appropriate for the symbol's type. The DWARF
22197 standard (v4) is not entirely clear about the meaning of using
22198 DW_FORM_dataN for a constant with a signed type, where the type is
22199 wider than the data. The conclusion of a discussion on the DWARF
22200 list was that this is unspecified. We choose to always zero-extend
22201 because that is the interpretation long in use by GCC. */
22202
22203 static gdb_byte *
22204 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22205 struct dwarf2_cu *cu, LONGEST *value, int bits)
22206 {
22207 struct objfile *objfile = cu->per_objfile->objfile;
22208 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22209 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22210 LONGEST l = attr->constant_value (0);
22211
22212 if (bits < sizeof (*value) * 8)
22213 {
22214 l &= ((LONGEST) 1 << bits) - 1;
22215 *value = l;
22216 }
22217 else if (bits == sizeof (*value) * 8)
22218 *value = l;
22219 else
22220 {
22221 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22222 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22223 return bytes;
22224 }
22225
22226 return NULL;
22227 }
22228
22229 /* Read a constant value from an attribute. Either set *VALUE, or if
22230 the value does not fit in *VALUE, set *BYTES - either already
22231 allocated on the objfile obstack, or newly allocated on OBSTACK,
22232 or, set *BATON, if we translated the constant to a location
22233 expression. */
22234
22235 static void
22236 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22237 const char *name, struct obstack *obstack,
22238 struct dwarf2_cu *cu,
22239 LONGEST *value, const gdb_byte **bytes,
22240 struct dwarf2_locexpr_baton **baton)
22241 {
22242 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22243 struct objfile *objfile = per_objfile->objfile;
22244 struct comp_unit_head *cu_header = &cu->header;
22245 struct dwarf_block *blk;
22246 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22247 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22248
22249 *value = 0;
22250 *bytes = NULL;
22251 *baton = NULL;
22252
22253 switch (attr->form)
22254 {
22255 case DW_FORM_addr:
22256 case DW_FORM_addrx:
22257 case DW_FORM_GNU_addr_index:
22258 {
22259 gdb_byte *data;
22260
22261 if (TYPE_LENGTH (type) != cu_header->addr_size)
22262 dwarf2_const_value_length_mismatch_complaint (name,
22263 cu_header->addr_size,
22264 TYPE_LENGTH (type));
22265 /* Symbols of this form are reasonably rare, so we just
22266 piggyback on the existing location code rather than writing
22267 a new implementation of symbol_computed_ops. */
22268 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22269 (*baton)->per_objfile = per_objfile;
22270 (*baton)->per_cu = cu->per_cu;
22271 gdb_assert ((*baton)->per_cu);
22272
22273 (*baton)->size = 2 + cu_header->addr_size;
22274 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22275 (*baton)->data = data;
22276
22277 data[0] = DW_OP_addr;
22278 store_unsigned_integer (&data[1], cu_header->addr_size,
22279 byte_order, attr->as_address ());
22280 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22281 }
22282 break;
22283 case DW_FORM_string:
22284 case DW_FORM_strp:
22285 case DW_FORM_strx:
22286 case DW_FORM_GNU_str_index:
22287 case DW_FORM_GNU_strp_alt:
22288 /* The string is already allocated on the objfile obstack, point
22289 directly to it. */
22290 *bytes = (const gdb_byte *) attr->as_string ();
22291 break;
22292 case DW_FORM_block1:
22293 case DW_FORM_block2:
22294 case DW_FORM_block4:
22295 case DW_FORM_block:
22296 case DW_FORM_exprloc:
22297 case DW_FORM_data16:
22298 blk = attr->as_block ();
22299 if (TYPE_LENGTH (type) != blk->size)
22300 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22301 TYPE_LENGTH (type));
22302 *bytes = blk->data;
22303 break;
22304
22305 /* The DW_AT_const_value attributes are supposed to carry the
22306 symbol's value "represented as it would be on the target
22307 architecture." By the time we get here, it's already been
22308 converted to host endianness, so we just need to sign- or
22309 zero-extend it as appropriate. */
22310 case DW_FORM_data1:
22311 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22312 break;
22313 case DW_FORM_data2:
22314 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22315 break;
22316 case DW_FORM_data4:
22317 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22318 break;
22319 case DW_FORM_data8:
22320 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22321 break;
22322
22323 case DW_FORM_sdata:
22324 case DW_FORM_implicit_const:
22325 *value = attr->as_signed ();
22326 break;
22327
22328 case DW_FORM_udata:
22329 *value = attr->as_unsigned ();
22330 break;
22331
22332 default:
22333 complaint (_("unsupported const value attribute form: '%s'"),
22334 dwarf_form_name (attr->form));
22335 *value = 0;
22336 break;
22337 }
22338 }
22339
22340
22341 /* Copy constant value from an attribute to a symbol. */
22342
22343 static void
22344 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22345 struct dwarf2_cu *cu)
22346 {
22347 struct objfile *objfile = cu->per_objfile->objfile;
22348 LONGEST value;
22349 const gdb_byte *bytes;
22350 struct dwarf2_locexpr_baton *baton;
22351
22352 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22353 sym->print_name (),
22354 &objfile->objfile_obstack, cu,
22355 &value, &bytes, &baton);
22356
22357 if (baton != NULL)
22358 {
22359 SYMBOL_LOCATION_BATON (sym) = baton;
22360 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22361 }
22362 else if (bytes != NULL)
22363 {
22364 SYMBOL_VALUE_BYTES (sym) = bytes;
22365 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22366 }
22367 else
22368 {
22369 SYMBOL_VALUE (sym) = value;
22370 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22371 }
22372 }
22373
22374 /* Return the type of the die in question using its DW_AT_type attribute. */
22375
22376 static struct type *
22377 die_type (struct die_info *die, struct dwarf2_cu *cu)
22378 {
22379 struct attribute *type_attr;
22380
22381 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22382 if (!type_attr)
22383 {
22384 struct objfile *objfile = cu->per_objfile->objfile;
22385 /* A missing DW_AT_type represents a void type. */
22386 return objfile_type (objfile)->builtin_void;
22387 }
22388
22389 return lookup_die_type (die, type_attr, cu);
22390 }
22391
22392 /* True iff CU's producer generates GNAT Ada auxiliary information
22393 that allows to find parallel types through that information instead
22394 of having to do expensive parallel lookups by type name. */
22395
22396 static int
22397 need_gnat_info (struct dwarf2_cu *cu)
22398 {
22399 /* Assume that the Ada compiler was GNAT, which always produces
22400 the auxiliary information. */
22401 return (cu->language == language_ada);
22402 }
22403
22404 /* Return the auxiliary type of the die in question using its
22405 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22406 attribute is not present. */
22407
22408 static struct type *
22409 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22410 {
22411 struct attribute *type_attr;
22412
22413 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22414 if (!type_attr)
22415 return NULL;
22416
22417 return lookup_die_type (die, type_attr, cu);
22418 }
22419
22420 /* If DIE has a descriptive_type attribute, then set the TYPE's
22421 descriptive type accordingly. */
22422
22423 static void
22424 set_descriptive_type (struct type *type, struct die_info *die,
22425 struct dwarf2_cu *cu)
22426 {
22427 struct type *descriptive_type = die_descriptive_type (die, cu);
22428
22429 if (descriptive_type)
22430 {
22431 ALLOCATE_GNAT_AUX_TYPE (type);
22432 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22433 }
22434 }
22435
22436 /* Return the containing type of the die in question using its
22437 DW_AT_containing_type attribute. */
22438
22439 static struct type *
22440 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22441 {
22442 struct attribute *type_attr;
22443 struct objfile *objfile = cu->per_objfile->objfile;
22444
22445 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22446 if (!type_attr)
22447 error (_("Dwarf Error: Problem turning containing type into gdb type "
22448 "[in module %s]"), objfile_name (objfile));
22449
22450 return lookup_die_type (die, type_attr, cu);
22451 }
22452
22453 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22454
22455 static struct type *
22456 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22457 {
22458 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22459 struct objfile *objfile = per_objfile->objfile;
22460 char *saved;
22461
22462 std::string message
22463 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22464 objfile_name (objfile),
22465 sect_offset_str (cu->header.sect_off),
22466 sect_offset_str (die->sect_off));
22467 saved = obstack_strdup (&objfile->objfile_obstack, message);
22468
22469 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22470 }
22471
22472 /* Look up the type of DIE in CU using its type attribute ATTR.
22473 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22474 DW_AT_containing_type.
22475 If there is no type substitute an error marker. */
22476
22477 static struct type *
22478 lookup_die_type (struct die_info *die, const struct attribute *attr,
22479 struct dwarf2_cu *cu)
22480 {
22481 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22482 struct objfile *objfile = per_objfile->objfile;
22483 struct type *this_type;
22484
22485 gdb_assert (attr->name == DW_AT_type
22486 || attr->name == DW_AT_GNAT_descriptive_type
22487 || attr->name == DW_AT_containing_type);
22488
22489 /* First see if we have it cached. */
22490
22491 if (attr->form == DW_FORM_GNU_ref_alt)
22492 {
22493 struct dwarf2_per_cu_data *per_cu;
22494 sect_offset sect_off = attr->get_ref_die_offset ();
22495
22496 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22497 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22498 }
22499 else if (attr->form_is_ref ())
22500 {
22501 sect_offset sect_off = attr->get_ref_die_offset ();
22502
22503 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22504 }
22505 else if (attr->form == DW_FORM_ref_sig8)
22506 {
22507 ULONGEST signature = attr->as_signature ();
22508
22509 return get_signatured_type (die, signature, cu);
22510 }
22511 else
22512 {
22513 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22514 " at %s [in module %s]"),
22515 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22516 objfile_name (objfile));
22517 return build_error_marker_type (cu, die);
22518 }
22519
22520 /* If not cached we need to read it in. */
22521
22522 if (this_type == NULL)
22523 {
22524 struct die_info *type_die = NULL;
22525 struct dwarf2_cu *type_cu = cu;
22526
22527 if (attr->form_is_ref ())
22528 type_die = follow_die_ref (die, attr, &type_cu);
22529 if (type_die == NULL)
22530 return build_error_marker_type (cu, die);
22531 /* If we find the type now, it's probably because the type came
22532 from an inter-CU reference and the type's CU got expanded before
22533 ours. */
22534 this_type = read_type_die (type_die, type_cu);
22535 }
22536
22537 /* If we still don't have a type use an error marker. */
22538
22539 if (this_type == NULL)
22540 return build_error_marker_type (cu, die);
22541
22542 return this_type;
22543 }
22544
22545 /* Return the type in DIE, CU.
22546 Returns NULL for invalid types.
22547
22548 This first does a lookup in die_type_hash,
22549 and only reads the die in if necessary.
22550
22551 NOTE: This can be called when reading in partial or full symbols. */
22552
22553 static struct type *
22554 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22555 {
22556 struct type *this_type;
22557
22558 this_type = get_die_type (die, cu);
22559 if (this_type)
22560 return this_type;
22561
22562 return read_type_die_1 (die, cu);
22563 }
22564
22565 /* Read the type in DIE, CU.
22566 Returns NULL for invalid types. */
22567
22568 static struct type *
22569 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22570 {
22571 struct type *this_type = NULL;
22572
22573 switch (die->tag)
22574 {
22575 case DW_TAG_class_type:
22576 case DW_TAG_interface_type:
22577 case DW_TAG_structure_type:
22578 case DW_TAG_union_type:
22579 this_type = read_structure_type (die, cu);
22580 break;
22581 case DW_TAG_enumeration_type:
22582 this_type = read_enumeration_type (die, cu);
22583 break;
22584 case DW_TAG_subprogram:
22585 case DW_TAG_subroutine_type:
22586 case DW_TAG_inlined_subroutine:
22587 this_type = read_subroutine_type (die, cu);
22588 break;
22589 case DW_TAG_array_type:
22590 this_type = read_array_type (die, cu);
22591 break;
22592 case DW_TAG_set_type:
22593 this_type = read_set_type (die, cu);
22594 break;
22595 case DW_TAG_pointer_type:
22596 this_type = read_tag_pointer_type (die, cu);
22597 break;
22598 case DW_TAG_ptr_to_member_type:
22599 this_type = read_tag_ptr_to_member_type (die, cu);
22600 break;
22601 case DW_TAG_reference_type:
22602 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22603 break;
22604 case DW_TAG_rvalue_reference_type:
22605 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22606 break;
22607 case DW_TAG_const_type:
22608 this_type = read_tag_const_type (die, cu);
22609 break;
22610 case DW_TAG_volatile_type:
22611 this_type = read_tag_volatile_type (die, cu);
22612 break;
22613 case DW_TAG_restrict_type:
22614 this_type = read_tag_restrict_type (die, cu);
22615 break;
22616 case DW_TAG_string_type:
22617 this_type = read_tag_string_type (die, cu);
22618 break;
22619 case DW_TAG_typedef:
22620 this_type = read_typedef (die, cu);
22621 break;
22622 case DW_TAG_subrange_type:
22623 this_type = read_subrange_type (die, cu);
22624 break;
22625 case DW_TAG_base_type:
22626 this_type = read_base_type (die, cu);
22627 break;
22628 case DW_TAG_unspecified_type:
22629 this_type = read_unspecified_type (die, cu);
22630 break;
22631 case DW_TAG_namespace:
22632 this_type = read_namespace_type (die, cu);
22633 break;
22634 case DW_TAG_module:
22635 this_type = read_module_type (die, cu);
22636 break;
22637 case DW_TAG_atomic_type:
22638 this_type = read_tag_atomic_type (die, cu);
22639 break;
22640 default:
22641 complaint (_("unexpected tag in read_type_die: '%s'"),
22642 dwarf_tag_name (die->tag));
22643 break;
22644 }
22645
22646 return this_type;
22647 }
22648
22649 /* See if we can figure out if the class lives in a namespace. We do
22650 this by looking for a member function; its demangled name will
22651 contain namespace info, if there is any.
22652 Return the computed name or NULL.
22653 Space for the result is allocated on the objfile's obstack.
22654 This is the full-die version of guess_partial_die_structure_name.
22655 In this case we know DIE has no useful parent. */
22656
22657 static const char *
22658 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22659 {
22660 struct die_info *spec_die;
22661 struct dwarf2_cu *spec_cu;
22662 struct die_info *child;
22663 struct objfile *objfile = cu->per_objfile->objfile;
22664
22665 spec_cu = cu;
22666 spec_die = die_specification (die, &spec_cu);
22667 if (spec_die != NULL)
22668 {
22669 die = spec_die;
22670 cu = spec_cu;
22671 }
22672
22673 for (child = die->child;
22674 child != NULL;
22675 child = child->sibling)
22676 {
22677 if (child->tag == DW_TAG_subprogram)
22678 {
22679 const char *linkage_name = dw2_linkage_name (child, cu);
22680
22681 if (linkage_name != NULL)
22682 {
22683 gdb::unique_xmalloc_ptr<char> actual_name
22684 (cu->language_defn->class_name_from_physname (linkage_name));
22685 const char *name = NULL;
22686
22687 if (actual_name != NULL)
22688 {
22689 const char *die_name = dwarf2_name (die, cu);
22690
22691 if (die_name != NULL
22692 && strcmp (die_name, actual_name.get ()) != 0)
22693 {
22694 /* Strip off the class name from the full name.
22695 We want the prefix. */
22696 int die_name_len = strlen (die_name);
22697 int actual_name_len = strlen (actual_name.get ());
22698 const char *ptr = actual_name.get ();
22699
22700 /* Test for '::' as a sanity check. */
22701 if (actual_name_len > die_name_len + 2
22702 && ptr[actual_name_len - die_name_len - 1] == ':')
22703 name = obstack_strndup (
22704 &objfile->per_bfd->storage_obstack,
22705 ptr, actual_name_len - die_name_len - 2);
22706 }
22707 }
22708 return name;
22709 }
22710 }
22711 }
22712
22713 return NULL;
22714 }
22715
22716 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22717 prefix part in such case. See
22718 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22719
22720 static const char *
22721 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22722 {
22723 struct attribute *attr;
22724 const char *base;
22725
22726 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22727 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22728 return NULL;
22729
22730 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22731 return NULL;
22732
22733 attr = dw2_linkage_name_attr (die, cu);
22734 const char *attr_name = attr->as_string ();
22735 if (attr == NULL || attr_name == NULL)
22736 return NULL;
22737
22738 /* dwarf2_name had to be already called. */
22739 gdb_assert (attr->canonical_string_p ());
22740
22741 /* Strip the base name, keep any leading namespaces/classes. */
22742 base = strrchr (attr_name, ':');
22743 if (base == NULL || base == attr_name || base[-1] != ':')
22744 return "";
22745
22746 struct objfile *objfile = cu->per_objfile->objfile;
22747 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22748 attr_name,
22749 &base[-1] - attr_name);
22750 }
22751
22752 /* Return the name of the namespace/class that DIE is defined within,
22753 or "" if we can't tell. The caller should not xfree the result.
22754
22755 For example, if we're within the method foo() in the following
22756 code:
22757
22758 namespace N {
22759 class C {
22760 void foo () {
22761 }
22762 };
22763 }
22764
22765 then determine_prefix on foo's die will return "N::C". */
22766
22767 static const char *
22768 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22769 {
22770 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22771 struct die_info *parent, *spec_die;
22772 struct dwarf2_cu *spec_cu;
22773 struct type *parent_type;
22774 const char *retval;
22775
22776 if (cu->language != language_cplus
22777 && cu->language != language_fortran && cu->language != language_d
22778 && cu->language != language_rust)
22779 return "";
22780
22781 retval = anonymous_struct_prefix (die, cu);
22782 if (retval)
22783 return retval;
22784
22785 /* We have to be careful in the presence of DW_AT_specification.
22786 For example, with GCC 3.4, given the code
22787
22788 namespace N {
22789 void foo() {
22790 // Definition of N::foo.
22791 }
22792 }
22793
22794 then we'll have a tree of DIEs like this:
22795
22796 1: DW_TAG_compile_unit
22797 2: DW_TAG_namespace // N
22798 3: DW_TAG_subprogram // declaration of N::foo
22799 4: DW_TAG_subprogram // definition of N::foo
22800 DW_AT_specification // refers to die #3
22801
22802 Thus, when processing die #4, we have to pretend that we're in
22803 the context of its DW_AT_specification, namely the contex of die
22804 #3. */
22805 spec_cu = cu;
22806 spec_die = die_specification (die, &spec_cu);
22807 if (spec_die == NULL)
22808 parent = die->parent;
22809 else
22810 {
22811 parent = spec_die->parent;
22812 cu = spec_cu;
22813 }
22814
22815 if (parent == NULL)
22816 return "";
22817 else if (parent->building_fullname)
22818 {
22819 const char *name;
22820 const char *parent_name;
22821
22822 /* It has been seen on RealView 2.2 built binaries,
22823 DW_TAG_template_type_param types actually _defined_ as
22824 children of the parent class:
22825
22826 enum E {};
22827 template class <class Enum> Class{};
22828 Class<enum E> class_e;
22829
22830 1: DW_TAG_class_type (Class)
22831 2: DW_TAG_enumeration_type (E)
22832 3: DW_TAG_enumerator (enum1:0)
22833 3: DW_TAG_enumerator (enum2:1)
22834 ...
22835 2: DW_TAG_template_type_param
22836 DW_AT_type DW_FORM_ref_udata (E)
22837
22838 Besides being broken debug info, it can put GDB into an
22839 infinite loop. Consider:
22840
22841 When we're building the full name for Class<E>, we'll start
22842 at Class, and go look over its template type parameters,
22843 finding E. We'll then try to build the full name of E, and
22844 reach here. We're now trying to build the full name of E,
22845 and look over the parent DIE for containing scope. In the
22846 broken case, if we followed the parent DIE of E, we'd again
22847 find Class, and once again go look at its template type
22848 arguments, etc., etc. Simply don't consider such parent die
22849 as source-level parent of this die (it can't be, the language
22850 doesn't allow it), and break the loop here. */
22851 name = dwarf2_name (die, cu);
22852 parent_name = dwarf2_name (parent, cu);
22853 complaint (_("template param type '%s' defined within parent '%s'"),
22854 name ? name : "<unknown>",
22855 parent_name ? parent_name : "<unknown>");
22856 return "";
22857 }
22858 else
22859 switch (parent->tag)
22860 {
22861 case DW_TAG_namespace:
22862 parent_type = read_type_die (parent, cu);
22863 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22864 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22865 Work around this problem here. */
22866 if (cu->language == language_cplus
22867 && strcmp (parent_type->name (), "::") == 0)
22868 return "";
22869 /* We give a name to even anonymous namespaces. */
22870 return parent_type->name ();
22871 case DW_TAG_class_type:
22872 case DW_TAG_interface_type:
22873 case DW_TAG_structure_type:
22874 case DW_TAG_union_type:
22875 case DW_TAG_module:
22876 parent_type = read_type_die (parent, cu);
22877 if (parent_type->name () != NULL)
22878 return parent_type->name ();
22879 else
22880 /* An anonymous structure is only allowed non-static data
22881 members; no typedefs, no member functions, et cetera.
22882 So it does not need a prefix. */
22883 return "";
22884 case DW_TAG_compile_unit:
22885 case DW_TAG_partial_unit:
22886 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22887 if (cu->language == language_cplus
22888 && !per_objfile->per_bfd->types.empty ()
22889 && die->child != NULL
22890 && (die->tag == DW_TAG_class_type
22891 || die->tag == DW_TAG_structure_type
22892 || die->tag == DW_TAG_union_type))
22893 {
22894 const char *name = guess_full_die_structure_name (die, cu);
22895 if (name != NULL)
22896 return name;
22897 }
22898 return "";
22899 case DW_TAG_subprogram:
22900 /* Nested subroutines in Fortran get a prefix with the name
22901 of the parent's subroutine. */
22902 if (cu->language == language_fortran)
22903 {
22904 if ((die->tag == DW_TAG_subprogram)
22905 && (dwarf2_name (parent, cu) != NULL))
22906 return dwarf2_name (parent, cu);
22907 }
22908 return determine_prefix (parent, cu);
22909 case DW_TAG_enumeration_type:
22910 parent_type = read_type_die (parent, cu);
22911 if (parent_type->is_declared_class ())
22912 {
22913 if (parent_type->name () != NULL)
22914 return parent_type->name ();
22915 return "";
22916 }
22917 /* Fall through. */
22918 default:
22919 return determine_prefix (parent, cu);
22920 }
22921 }
22922
22923 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22924 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22925 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22926 an obconcat, otherwise allocate storage for the result. The CU argument is
22927 used to determine the language and hence, the appropriate separator. */
22928
22929 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22930
22931 static char *
22932 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22933 int physname, struct dwarf2_cu *cu)
22934 {
22935 const char *lead = "";
22936 const char *sep;
22937
22938 if (suffix == NULL || suffix[0] == '\0'
22939 || prefix == NULL || prefix[0] == '\0')
22940 sep = "";
22941 else if (cu->language == language_d)
22942 {
22943 /* For D, the 'main' function could be defined in any module, but it
22944 should never be prefixed. */
22945 if (strcmp (suffix, "D main") == 0)
22946 {
22947 prefix = "";
22948 sep = "";
22949 }
22950 else
22951 sep = ".";
22952 }
22953 else if (cu->language == language_fortran && physname)
22954 {
22955 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22956 DW_AT_MIPS_linkage_name is preferred and used instead. */
22957
22958 lead = "__";
22959 sep = "_MOD_";
22960 }
22961 else
22962 sep = "::";
22963
22964 if (prefix == NULL)
22965 prefix = "";
22966 if (suffix == NULL)
22967 suffix = "";
22968
22969 if (obs == NULL)
22970 {
22971 char *retval
22972 = ((char *)
22973 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22974
22975 strcpy (retval, lead);
22976 strcat (retval, prefix);
22977 strcat (retval, sep);
22978 strcat (retval, suffix);
22979 return retval;
22980 }
22981 else
22982 {
22983 /* We have an obstack. */
22984 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22985 }
22986 }
22987
22988 /* Get name of a die, return NULL if not found. */
22989
22990 static const char *
22991 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22992 struct objfile *objfile)
22993 {
22994 if (name && cu->language == language_cplus)
22995 {
22996 gdb::unique_xmalloc_ptr<char> canon_name
22997 = cp_canonicalize_string (name);
22998
22999 if (canon_name != nullptr)
23000 name = objfile->intern (canon_name.get ());
23001 }
23002
23003 return name;
23004 }
23005
23006 /* Get name of a die, return NULL if not found.
23007 Anonymous namespaces are converted to their magic string. */
23008
23009 static const char *
23010 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23011 {
23012 struct attribute *attr;
23013 struct objfile *objfile = cu->per_objfile->objfile;
23014
23015 attr = dwarf2_attr (die, DW_AT_name, cu);
23016 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23017 if (attr_name == nullptr
23018 && die->tag != DW_TAG_namespace
23019 && die->tag != DW_TAG_class_type
23020 && die->tag != DW_TAG_interface_type
23021 && die->tag != DW_TAG_structure_type
23022 && die->tag != DW_TAG_union_type)
23023 return NULL;
23024
23025 switch (die->tag)
23026 {
23027 case DW_TAG_compile_unit:
23028 case DW_TAG_partial_unit:
23029 /* Compilation units have a DW_AT_name that is a filename, not
23030 a source language identifier. */
23031 case DW_TAG_enumeration_type:
23032 case DW_TAG_enumerator:
23033 /* These tags always have simple identifiers already; no need
23034 to canonicalize them. */
23035 return attr_name;
23036
23037 case DW_TAG_namespace:
23038 if (attr_name != nullptr)
23039 return attr_name;
23040 return CP_ANONYMOUS_NAMESPACE_STR;
23041
23042 case DW_TAG_class_type:
23043 case DW_TAG_interface_type:
23044 case DW_TAG_structure_type:
23045 case DW_TAG_union_type:
23046 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23047 structures or unions. These were of the form "._%d" in GCC 4.1,
23048 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23049 and GCC 4.4. We work around this problem by ignoring these. */
23050 if (attr_name != nullptr
23051 && (startswith (attr_name, "._")
23052 || startswith (attr_name, "<anonymous")))
23053 return NULL;
23054
23055 /* GCC might emit a nameless typedef that has a linkage name. See
23056 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23057 if (!attr || attr_name == NULL)
23058 {
23059 attr = dw2_linkage_name_attr (die, cu);
23060 attr_name = attr == nullptr ? nullptr : attr->as_string ();
23061 if (attr == NULL || attr_name == NULL)
23062 return NULL;
23063
23064 /* Avoid demangling attr_name the second time on a second
23065 call for the same DIE. */
23066 if (!attr->canonical_string_p ())
23067 {
23068 gdb::unique_xmalloc_ptr<char> demangled
23069 (gdb_demangle (attr_name, DMGL_TYPES));
23070 if (demangled == nullptr)
23071 return nullptr;
23072
23073 attr->set_string_canonical (objfile->intern (demangled.get ()));
23074 attr_name = attr->as_string ();
23075 }
23076
23077 /* Strip any leading namespaces/classes, keep only the
23078 base name. DW_AT_name for named DIEs does not
23079 contain the prefixes. */
23080 const char *base = strrchr (attr_name, ':');
23081 if (base && base > attr_name && base[-1] == ':')
23082 return &base[1];
23083 else
23084 return attr_name;
23085 }
23086 break;
23087
23088 default:
23089 break;
23090 }
23091
23092 if (!attr->canonical_string_p ())
23093 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23094 objfile));
23095 return attr->as_string ();
23096 }
23097
23098 /* Return the die that this die in an extension of, or NULL if there
23099 is none. *EXT_CU is the CU containing DIE on input, and the CU
23100 containing the return value on output. */
23101
23102 static struct die_info *
23103 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23104 {
23105 struct attribute *attr;
23106
23107 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23108 if (attr == NULL)
23109 return NULL;
23110
23111 return follow_die_ref (die, attr, ext_cu);
23112 }
23113
23114 static void
23115 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23116 {
23117 unsigned int i;
23118
23119 print_spaces (indent, f);
23120 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23121 dwarf_tag_name (die->tag), die->abbrev,
23122 sect_offset_str (die->sect_off));
23123
23124 if (die->parent != NULL)
23125 {
23126 print_spaces (indent, f);
23127 fprintf_unfiltered (f, " parent at offset: %s\n",
23128 sect_offset_str (die->parent->sect_off));
23129 }
23130
23131 print_spaces (indent, f);
23132 fprintf_unfiltered (f, " has children: %s\n",
23133 dwarf_bool_name (die->child != NULL));
23134
23135 print_spaces (indent, f);
23136 fprintf_unfiltered (f, " attributes:\n");
23137
23138 for (i = 0; i < die->num_attrs; ++i)
23139 {
23140 print_spaces (indent, f);
23141 fprintf_unfiltered (f, " %s (%s) ",
23142 dwarf_attr_name (die->attrs[i].name),
23143 dwarf_form_name (die->attrs[i].form));
23144
23145 switch (die->attrs[i].form)
23146 {
23147 case DW_FORM_addr:
23148 case DW_FORM_addrx:
23149 case DW_FORM_GNU_addr_index:
23150 fprintf_unfiltered (f, "address: ");
23151 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23152 break;
23153 case DW_FORM_block2:
23154 case DW_FORM_block4:
23155 case DW_FORM_block:
23156 case DW_FORM_block1:
23157 fprintf_unfiltered (f, "block: size %s",
23158 pulongest (die->attrs[i].as_block ()->size));
23159 break;
23160 case DW_FORM_exprloc:
23161 fprintf_unfiltered (f, "expression: size %s",
23162 pulongest (die->attrs[i].as_block ()->size));
23163 break;
23164 case DW_FORM_data16:
23165 fprintf_unfiltered (f, "constant of 16 bytes");
23166 break;
23167 case DW_FORM_ref_addr:
23168 fprintf_unfiltered (f, "ref address: ");
23169 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23170 break;
23171 case DW_FORM_GNU_ref_alt:
23172 fprintf_unfiltered (f, "alt ref address: ");
23173 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23174 break;
23175 case DW_FORM_ref1:
23176 case DW_FORM_ref2:
23177 case DW_FORM_ref4:
23178 case DW_FORM_ref8:
23179 case DW_FORM_ref_udata:
23180 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23181 (long) (die->attrs[i].as_unsigned ()));
23182 break;
23183 case DW_FORM_data1:
23184 case DW_FORM_data2:
23185 case DW_FORM_data4:
23186 case DW_FORM_data8:
23187 case DW_FORM_udata:
23188 fprintf_unfiltered (f, "constant: %s",
23189 pulongest (die->attrs[i].as_unsigned ()));
23190 break;
23191 case DW_FORM_sec_offset:
23192 fprintf_unfiltered (f, "section offset: %s",
23193 pulongest (die->attrs[i].as_unsigned ()));
23194 break;
23195 case DW_FORM_ref_sig8:
23196 fprintf_unfiltered (f, "signature: %s",
23197 hex_string (die->attrs[i].as_signature ()));
23198 break;
23199 case DW_FORM_string:
23200 case DW_FORM_strp:
23201 case DW_FORM_line_strp:
23202 case DW_FORM_strx:
23203 case DW_FORM_GNU_str_index:
23204 case DW_FORM_GNU_strp_alt:
23205 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23206 die->attrs[i].as_string ()
23207 ? die->attrs[i].as_string () : "",
23208 die->attrs[i].canonical_string_p () ? "is" : "not");
23209 break;
23210 case DW_FORM_flag:
23211 if (die->attrs[i].as_boolean ())
23212 fprintf_unfiltered (f, "flag: TRUE");
23213 else
23214 fprintf_unfiltered (f, "flag: FALSE");
23215 break;
23216 case DW_FORM_flag_present:
23217 fprintf_unfiltered (f, "flag: TRUE");
23218 break;
23219 case DW_FORM_indirect:
23220 /* The reader will have reduced the indirect form to
23221 the "base form" so this form should not occur. */
23222 fprintf_unfiltered (f,
23223 "unexpected attribute form: DW_FORM_indirect");
23224 break;
23225 case DW_FORM_sdata:
23226 case DW_FORM_implicit_const:
23227 fprintf_unfiltered (f, "constant: %s",
23228 plongest (die->attrs[i].as_signed ()));
23229 break;
23230 default:
23231 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23232 die->attrs[i].form);
23233 break;
23234 }
23235 fprintf_unfiltered (f, "\n");
23236 }
23237 }
23238
23239 static void
23240 dump_die_for_error (struct die_info *die)
23241 {
23242 dump_die_shallow (gdb_stderr, 0, die);
23243 }
23244
23245 static void
23246 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23247 {
23248 int indent = level * 4;
23249
23250 gdb_assert (die != NULL);
23251
23252 if (level >= max_level)
23253 return;
23254
23255 dump_die_shallow (f, indent, die);
23256
23257 if (die->child != NULL)
23258 {
23259 print_spaces (indent, f);
23260 fprintf_unfiltered (f, " Children:");
23261 if (level + 1 < max_level)
23262 {
23263 fprintf_unfiltered (f, "\n");
23264 dump_die_1 (f, level + 1, max_level, die->child);
23265 }
23266 else
23267 {
23268 fprintf_unfiltered (f,
23269 " [not printed, max nesting level reached]\n");
23270 }
23271 }
23272
23273 if (die->sibling != NULL && level > 0)
23274 {
23275 dump_die_1 (f, level, max_level, die->sibling);
23276 }
23277 }
23278
23279 /* This is called from the pdie macro in gdbinit.in.
23280 It's not static so gcc will keep a copy callable from gdb. */
23281
23282 void
23283 dump_die (struct die_info *die, int max_level)
23284 {
23285 dump_die_1 (gdb_stdlog, 0, max_level, die);
23286 }
23287
23288 static void
23289 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23290 {
23291 void **slot;
23292
23293 slot = htab_find_slot_with_hash (cu->die_hash, die,
23294 to_underlying (die->sect_off),
23295 INSERT);
23296
23297 *slot = die;
23298 }
23299
23300 /* Follow reference or signature attribute ATTR of SRC_DIE.
23301 On entry *REF_CU is the CU of SRC_DIE.
23302 On exit *REF_CU is the CU of the result. */
23303
23304 static struct die_info *
23305 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23306 struct dwarf2_cu **ref_cu)
23307 {
23308 struct die_info *die;
23309
23310 if (attr->form_is_ref ())
23311 die = follow_die_ref (src_die, attr, ref_cu);
23312 else if (attr->form == DW_FORM_ref_sig8)
23313 die = follow_die_sig (src_die, attr, ref_cu);
23314 else
23315 {
23316 dump_die_for_error (src_die);
23317 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23318 objfile_name ((*ref_cu)->per_objfile->objfile));
23319 }
23320
23321 return die;
23322 }
23323
23324 /* Follow reference OFFSET.
23325 On entry *REF_CU is the CU of the source die referencing OFFSET.
23326 On exit *REF_CU is the CU of the result.
23327 Returns NULL if OFFSET is invalid. */
23328
23329 static struct die_info *
23330 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23331 struct dwarf2_cu **ref_cu)
23332 {
23333 struct die_info temp_die;
23334 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23335 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23336
23337 gdb_assert (cu->per_cu != NULL);
23338
23339 target_cu = cu;
23340
23341 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23342 "source CU contains target offset: %d",
23343 sect_offset_str (cu->per_cu->sect_off),
23344 sect_offset_str (sect_off),
23345 cu->header.offset_in_cu_p (sect_off));
23346
23347 if (cu->per_cu->is_debug_types)
23348 {
23349 /* .debug_types CUs cannot reference anything outside their CU.
23350 If they need to, they have to reference a signatured type via
23351 DW_FORM_ref_sig8. */
23352 if (!cu->header.offset_in_cu_p (sect_off))
23353 return NULL;
23354 }
23355 else if (offset_in_dwz != cu->per_cu->is_dwz
23356 || !cu->header.offset_in_cu_p (sect_off))
23357 {
23358 struct dwarf2_per_cu_data *per_cu;
23359
23360 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23361 per_objfile);
23362
23363 dwarf_read_debug_printf_v ("target CU offset: %s, "
23364 "target CU DIEs loaded: %d",
23365 sect_offset_str (per_cu->sect_off),
23366 per_objfile->get_cu (per_cu) != nullptr);
23367
23368 /* If necessary, add it to the queue and load its DIEs.
23369
23370 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23371 it doesn't mean they are currently loaded. Since we require them
23372 to be loaded, we must check for ourselves. */
23373 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23374 || per_objfile->get_cu (per_cu) == nullptr)
23375 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23376 false, cu->language);
23377
23378 target_cu = per_objfile->get_cu (per_cu);
23379 gdb_assert (target_cu != nullptr);
23380 }
23381 else if (cu->dies == NULL)
23382 {
23383 /* We're loading full DIEs during partial symbol reading. */
23384 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23385 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23386 language_minimal);
23387 }
23388
23389 *ref_cu = target_cu;
23390 temp_die.sect_off = sect_off;
23391
23392 if (target_cu != cu)
23393 target_cu->ancestor = cu;
23394
23395 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23396 &temp_die,
23397 to_underlying (sect_off));
23398 }
23399
23400 /* Follow reference attribute ATTR of SRC_DIE.
23401 On entry *REF_CU is the CU of SRC_DIE.
23402 On exit *REF_CU is the CU of the result. */
23403
23404 static struct die_info *
23405 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23406 struct dwarf2_cu **ref_cu)
23407 {
23408 sect_offset sect_off = attr->get_ref_die_offset ();
23409 struct dwarf2_cu *cu = *ref_cu;
23410 struct die_info *die;
23411
23412 die = follow_die_offset (sect_off,
23413 (attr->form == DW_FORM_GNU_ref_alt
23414 || cu->per_cu->is_dwz),
23415 ref_cu);
23416 if (!die)
23417 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23418 "at %s [in module %s]"),
23419 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23420 objfile_name (cu->per_objfile->objfile));
23421
23422 return die;
23423 }
23424
23425 /* See read.h. */
23426
23427 struct dwarf2_locexpr_baton
23428 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23429 dwarf2_per_cu_data *per_cu,
23430 dwarf2_per_objfile *per_objfile,
23431 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23432 bool resolve_abstract_p)
23433 {
23434 struct die_info *die;
23435 struct attribute *attr;
23436 struct dwarf2_locexpr_baton retval;
23437 struct objfile *objfile = per_objfile->objfile;
23438
23439 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23440 if (cu == nullptr)
23441 cu = load_cu (per_cu, per_objfile, false);
23442
23443 if (cu == nullptr)
23444 {
23445 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23446 Instead just throw an error, not much else we can do. */
23447 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23448 sect_offset_str (sect_off), objfile_name (objfile));
23449 }
23450
23451 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23452 if (!die)
23453 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23454 sect_offset_str (sect_off), objfile_name (objfile));
23455
23456 attr = dwarf2_attr (die, DW_AT_location, cu);
23457 if (!attr && resolve_abstract_p
23458 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23459 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23460 {
23461 CORE_ADDR pc = get_frame_pc ();
23462 CORE_ADDR baseaddr = objfile->text_section_offset ();
23463 struct gdbarch *gdbarch = objfile->arch ();
23464
23465 for (const auto &cand_off
23466 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23467 {
23468 struct dwarf2_cu *cand_cu = cu;
23469 struct die_info *cand
23470 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23471 if (!cand
23472 || !cand->parent
23473 || cand->parent->tag != DW_TAG_subprogram)
23474 continue;
23475
23476 CORE_ADDR pc_low, pc_high;
23477 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23478 if (pc_low == ((CORE_ADDR) -1))
23479 continue;
23480 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23481 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23482 if (!(pc_low <= pc && pc < pc_high))
23483 continue;
23484
23485 die = cand;
23486 attr = dwarf2_attr (die, DW_AT_location, cu);
23487 break;
23488 }
23489 }
23490
23491 if (!attr)
23492 {
23493 /* DWARF: "If there is no such attribute, then there is no effect.".
23494 DATA is ignored if SIZE is 0. */
23495
23496 retval.data = NULL;
23497 retval.size = 0;
23498 }
23499 else if (attr->form_is_section_offset ())
23500 {
23501 struct dwarf2_loclist_baton loclist_baton;
23502 CORE_ADDR pc = get_frame_pc ();
23503 size_t size;
23504
23505 fill_in_loclist_baton (cu, &loclist_baton, attr);
23506
23507 retval.data = dwarf2_find_location_expression (&loclist_baton,
23508 &size, pc);
23509 retval.size = size;
23510 }
23511 else
23512 {
23513 if (!attr->form_is_block ())
23514 error (_("Dwarf Error: DIE at %s referenced in module %s "
23515 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23516 sect_offset_str (sect_off), objfile_name (objfile));
23517
23518 struct dwarf_block *block = attr->as_block ();
23519 retval.data = block->data;
23520 retval.size = block->size;
23521 }
23522 retval.per_objfile = per_objfile;
23523 retval.per_cu = cu->per_cu;
23524
23525 per_objfile->age_comp_units ();
23526
23527 return retval;
23528 }
23529
23530 /* See read.h. */
23531
23532 struct dwarf2_locexpr_baton
23533 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23534 dwarf2_per_cu_data *per_cu,
23535 dwarf2_per_objfile *per_objfile,
23536 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23537 {
23538 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23539
23540 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23541 get_frame_pc);
23542 }
23543
23544 /* Write a constant of a given type as target-ordered bytes into
23545 OBSTACK. */
23546
23547 static const gdb_byte *
23548 write_constant_as_bytes (struct obstack *obstack,
23549 enum bfd_endian byte_order,
23550 struct type *type,
23551 ULONGEST value,
23552 LONGEST *len)
23553 {
23554 gdb_byte *result;
23555
23556 *len = TYPE_LENGTH (type);
23557 result = (gdb_byte *) obstack_alloc (obstack, *len);
23558 store_unsigned_integer (result, *len, byte_order, value);
23559
23560 return result;
23561 }
23562
23563 /* See read.h. */
23564
23565 const gdb_byte *
23566 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23567 dwarf2_per_cu_data *per_cu,
23568 dwarf2_per_objfile *per_objfile,
23569 obstack *obstack,
23570 LONGEST *len)
23571 {
23572 struct die_info *die;
23573 struct attribute *attr;
23574 const gdb_byte *result = NULL;
23575 struct type *type;
23576 LONGEST value;
23577 enum bfd_endian byte_order;
23578 struct objfile *objfile = per_objfile->objfile;
23579
23580 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23581 if (cu == nullptr)
23582 cu = load_cu (per_cu, per_objfile, false);
23583
23584 if (cu == nullptr)
23585 {
23586 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23587 Instead just throw an error, not much else we can do. */
23588 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23589 sect_offset_str (sect_off), objfile_name (objfile));
23590 }
23591
23592 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23593 if (!die)
23594 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23595 sect_offset_str (sect_off), objfile_name (objfile));
23596
23597 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23598 if (attr == NULL)
23599 return NULL;
23600
23601 byte_order = (bfd_big_endian (objfile->obfd)
23602 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23603
23604 switch (attr->form)
23605 {
23606 case DW_FORM_addr:
23607 case DW_FORM_addrx:
23608 case DW_FORM_GNU_addr_index:
23609 {
23610 gdb_byte *tem;
23611
23612 *len = cu->header.addr_size;
23613 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23614 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23615 result = tem;
23616 }
23617 break;
23618 case DW_FORM_string:
23619 case DW_FORM_strp:
23620 case DW_FORM_strx:
23621 case DW_FORM_GNU_str_index:
23622 case DW_FORM_GNU_strp_alt:
23623 /* The string is already allocated on the objfile obstack, point
23624 directly to it. */
23625 {
23626 const char *attr_name = attr->as_string ();
23627 result = (const gdb_byte *) attr_name;
23628 *len = strlen (attr_name);
23629 }
23630 break;
23631 case DW_FORM_block1:
23632 case DW_FORM_block2:
23633 case DW_FORM_block4:
23634 case DW_FORM_block:
23635 case DW_FORM_exprloc:
23636 case DW_FORM_data16:
23637 {
23638 struct dwarf_block *block = attr->as_block ();
23639 result = block->data;
23640 *len = block->size;
23641 }
23642 break;
23643
23644 /* The DW_AT_const_value attributes are supposed to carry the
23645 symbol's value "represented as it would be on the target
23646 architecture." By the time we get here, it's already been
23647 converted to host endianness, so we just need to sign- or
23648 zero-extend it as appropriate. */
23649 case DW_FORM_data1:
23650 type = die_type (die, cu);
23651 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23652 if (result == NULL)
23653 result = write_constant_as_bytes (obstack, byte_order,
23654 type, value, len);
23655 break;
23656 case DW_FORM_data2:
23657 type = die_type (die, cu);
23658 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23659 if (result == NULL)
23660 result = write_constant_as_bytes (obstack, byte_order,
23661 type, value, len);
23662 break;
23663 case DW_FORM_data4:
23664 type = die_type (die, cu);
23665 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23666 if (result == NULL)
23667 result = write_constant_as_bytes (obstack, byte_order,
23668 type, value, len);
23669 break;
23670 case DW_FORM_data8:
23671 type = die_type (die, cu);
23672 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23673 if (result == NULL)
23674 result = write_constant_as_bytes (obstack, byte_order,
23675 type, value, len);
23676 break;
23677
23678 case DW_FORM_sdata:
23679 case DW_FORM_implicit_const:
23680 type = die_type (die, cu);
23681 result = write_constant_as_bytes (obstack, byte_order,
23682 type, attr->as_signed (), len);
23683 break;
23684
23685 case DW_FORM_udata:
23686 type = die_type (die, cu);
23687 result = write_constant_as_bytes (obstack, byte_order,
23688 type, attr->as_unsigned (), len);
23689 break;
23690
23691 default:
23692 complaint (_("unsupported const value attribute form: '%s'"),
23693 dwarf_form_name (attr->form));
23694 break;
23695 }
23696
23697 return result;
23698 }
23699
23700 /* See read.h. */
23701
23702 struct type *
23703 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23704 dwarf2_per_cu_data *per_cu,
23705 dwarf2_per_objfile *per_objfile)
23706 {
23707 struct die_info *die;
23708
23709 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23710 if (cu == nullptr)
23711 cu = load_cu (per_cu, per_objfile, false);
23712
23713 if (cu == nullptr)
23714 return nullptr;
23715
23716 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23717 if (!die)
23718 return NULL;
23719
23720 return die_type (die, cu);
23721 }
23722
23723 /* See read.h. */
23724
23725 struct type *
23726 dwarf2_get_die_type (cu_offset die_offset,
23727 dwarf2_per_cu_data *per_cu,
23728 dwarf2_per_objfile *per_objfile)
23729 {
23730 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23731 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23732 }
23733
23734 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23735 On entry *REF_CU is the CU of SRC_DIE.
23736 On exit *REF_CU is the CU of the result.
23737 Returns NULL if the referenced DIE isn't found. */
23738
23739 static struct die_info *
23740 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23741 struct dwarf2_cu **ref_cu)
23742 {
23743 struct die_info temp_die;
23744 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23745 struct die_info *die;
23746 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23747
23748
23749 /* While it might be nice to assert sig_type->type == NULL here,
23750 we can get here for DW_AT_imported_declaration where we need
23751 the DIE not the type. */
23752
23753 /* If necessary, add it to the queue and load its DIEs.
23754
23755 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23756 it doesn't mean they are currently loaded. Since we require them
23757 to be loaded, we must check for ourselves. */
23758 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
23759 language_minimal)
23760 || per_objfile->get_cu (sig_type) == nullptr)
23761 read_signatured_type (sig_type, per_objfile);
23762
23763 sig_cu = per_objfile->get_cu (sig_type);
23764 gdb_assert (sig_cu != NULL);
23765 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23766 temp_die.sect_off = sig_type->type_offset_in_section;
23767 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23768 to_underlying (temp_die.sect_off));
23769 if (die)
23770 {
23771 /* For .gdb_index version 7 keep track of included TUs.
23772 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23773 if (per_objfile->per_bfd->index_table != NULL
23774 && per_objfile->per_bfd->index_table->version <= 7)
23775 {
23776 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23777 }
23778
23779 *ref_cu = sig_cu;
23780 if (sig_cu != cu)
23781 sig_cu->ancestor = cu;
23782
23783 return die;
23784 }
23785
23786 return NULL;
23787 }
23788
23789 /* Follow signatured type referenced by ATTR in SRC_DIE.
23790 On entry *REF_CU is the CU of SRC_DIE.
23791 On exit *REF_CU is the CU of the result.
23792 The result is the DIE of the type.
23793 If the referenced type cannot be found an error is thrown. */
23794
23795 static struct die_info *
23796 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23797 struct dwarf2_cu **ref_cu)
23798 {
23799 ULONGEST signature = attr->as_signature ();
23800 struct signatured_type *sig_type;
23801 struct die_info *die;
23802
23803 gdb_assert (attr->form == DW_FORM_ref_sig8);
23804
23805 sig_type = lookup_signatured_type (*ref_cu, signature);
23806 /* sig_type will be NULL if the signatured type is missing from
23807 the debug info. */
23808 if (sig_type == NULL)
23809 {
23810 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23811 " from DIE at %s [in module %s]"),
23812 hex_string (signature), sect_offset_str (src_die->sect_off),
23813 objfile_name ((*ref_cu)->per_objfile->objfile));
23814 }
23815
23816 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23817 if (die == NULL)
23818 {
23819 dump_die_for_error (src_die);
23820 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23821 " from DIE at %s [in module %s]"),
23822 hex_string (signature), sect_offset_str (src_die->sect_off),
23823 objfile_name ((*ref_cu)->per_objfile->objfile));
23824 }
23825
23826 return die;
23827 }
23828
23829 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23830 reading in and processing the type unit if necessary. */
23831
23832 static struct type *
23833 get_signatured_type (struct die_info *die, ULONGEST signature,
23834 struct dwarf2_cu *cu)
23835 {
23836 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23837 struct signatured_type *sig_type;
23838 struct dwarf2_cu *type_cu;
23839 struct die_info *type_die;
23840 struct type *type;
23841
23842 sig_type = lookup_signatured_type (cu, signature);
23843 /* sig_type will be NULL if the signatured type is missing from
23844 the debug info. */
23845 if (sig_type == NULL)
23846 {
23847 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23848 " from DIE at %s [in module %s]"),
23849 hex_string (signature), sect_offset_str (die->sect_off),
23850 objfile_name (per_objfile->objfile));
23851 return build_error_marker_type (cu, die);
23852 }
23853
23854 /* If we already know the type we're done. */
23855 type = per_objfile->get_type_for_signatured_type (sig_type);
23856 if (type != nullptr)
23857 return type;
23858
23859 type_cu = cu;
23860 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23861 if (type_die != NULL)
23862 {
23863 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23864 is created. This is important, for example, because for c++ classes
23865 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23866 type = read_type_die (type_die, type_cu);
23867 if (type == NULL)
23868 {
23869 complaint (_("Dwarf Error: Cannot build signatured type %s"
23870 " referenced from DIE at %s [in module %s]"),
23871 hex_string (signature), sect_offset_str (die->sect_off),
23872 objfile_name (per_objfile->objfile));
23873 type = build_error_marker_type (cu, die);
23874 }
23875 }
23876 else
23877 {
23878 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23879 " from DIE at %s [in module %s]"),
23880 hex_string (signature), sect_offset_str (die->sect_off),
23881 objfile_name (per_objfile->objfile));
23882 type = build_error_marker_type (cu, die);
23883 }
23884
23885 per_objfile->set_type_for_signatured_type (sig_type, type);
23886
23887 return type;
23888 }
23889
23890 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23891 reading in and processing the type unit if necessary. */
23892
23893 static struct type *
23894 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23895 struct dwarf2_cu *cu) /* ARI: editCase function */
23896 {
23897 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23898 if (attr->form_is_ref ())
23899 {
23900 struct dwarf2_cu *type_cu = cu;
23901 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23902
23903 return read_type_die (type_die, type_cu);
23904 }
23905 else if (attr->form == DW_FORM_ref_sig8)
23906 {
23907 return get_signatured_type (die, attr->as_signature (), cu);
23908 }
23909 else
23910 {
23911 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23912
23913 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23914 " at %s [in module %s]"),
23915 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23916 objfile_name (per_objfile->objfile));
23917 return build_error_marker_type (cu, die);
23918 }
23919 }
23920
23921 /* Load the DIEs associated with type unit PER_CU into memory. */
23922
23923 static void
23924 load_full_type_unit (dwarf2_per_cu_data *per_cu,
23925 dwarf2_per_objfile *per_objfile)
23926 {
23927 struct signatured_type *sig_type;
23928
23929 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23930 gdb_assert (! per_cu->type_unit_group_p ());
23931
23932 /* We have the per_cu, but we need the signatured_type.
23933 Fortunately this is an easy translation. */
23934 gdb_assert (per_cu->is_debug_types);
23935 sig_type = (struct signatured_type *) per_cu;
23936
23937 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23938
23939 read_signatured_type (sig_type, per_objfile);
23940
23941 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
23942 }
23943
23944 /* Read in a signatured type and build its CU and DIEs.
23945 If the type is a stub for the real type in a DWO file,
23946 read in the real type from the DWO file as well. */
23947
23948 static void
23949 read_signatured_type (signatured_type *sig_type,
23950 dwarf2_per_objfile *per_objfile)
23951 {
23952 gdb_assert (sig_type->is_debug_types);
23953 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
23954
23955 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
23956
23957 if (!reader.dummy_p)
23958 {
23959 struct dwarf2_cu *cu = reader.cu;
23960 const gdb_byte *info_ptr = reader.info_ptr;
23961
23962 gdb_assert (cu->die_hash == NULL);
23963 cu->die_hash =
23964 htab_create_alloc_ex (cu->header.length / 12,
23965 die_hash,
23966 die_eq,
23967 NULL,
23968 &cu->comp_unit_obstack,
23969 hashtab_obstack_allocate,
23970 dummy_obstack_deallocate);
23971
23972 if (reader.comp_unit_die->has_children)
23973 reader.comp_unit_die->child
23974 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23975 reader.comp_unit_die);
23976 cu->dies = reader.comp_unit_die;
23977 /* comp_unit_die is not stored in die_hash, no need. */
23978
23979 /* We try not to read any attributes in this function, because
23980 not all CUs needed for references have been loaded yet, and
23981 symbol table processing isn't initialized. But we have to
23982 set the CU language, or we won't be able to build types
23983 correctly. Similarly, if we do not read the producer, we can
23984 not apply producer-specific interpretation. */
23985 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23986
23987 reader.keep ();
23988 }
23989
23990 sig_type->tu_read = 1;
23991 }
23992
23993 /* Decode simple location descriptions.
23994 Given a pointer to a dwarf block that defines a location, compute
23995 the location and return the value. If COMPUTED is non-null, it is
23996 set to true to indicate that decoding was successful, and false
23997 otherwise. If COMPUTED is null, then this function may emit a
23998 complaint. */
23999
24000 static CORE_ADDR
24001 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
24002 {
24003 struct objfile *objfile = cu->per_objfile->objfile;
24004 size_t i;
24005 size_t size = blk->size;
24006 const gdb_byte *data = blk->data;
24007 CORE_ADDR stack[64];
24008 int stacki;
24009 unsigned int bytes_read, unsnd;
24010 gdb_byte op;
24011
24012 if (computed != nullptr)
24013 *computed = false;
24014
24015 i = 0;
24016 stacki = 0;
24017 stack[stacki] = 0;
24018 stack[++stacki] = 0;
24019
24020 while (i < size)
24021 {
24022 op = data[i++];
24023 switch (op)
24024 {
24025 case DW_OP_lit0:
24026 case DW_OP_lit1:
24027 case DW_OP_lit2:
24028 case DW_OP_lit3:
24029 case DW_OP_lit4:
24030 case DW_OP_lit5:
24031 case DW_OP_lit6:
24032 case DW_OP_lit7:
24033 case DW_OP_lit8:
24034 case DW_OP_lit9:
24035 case DW_OP_lit10:
24036 case DW_OP_lit11:
24037 case DW_OP_lit12:
24038 case DW_OP_lit13:
24039 case DW_OP_lit14:
24040 case DW_OP_lit15:
24041 case DW_OP_lit16:
24042 case DW_OP_lit17:
24043 case DW_OP_lit18:
24044 case DW_OP_lit19:
24045 case DW_OP_lit20:
24046 case DW_OP_lit21:
24047 case DW_OP_lit22:
24048 case DW_OP_lit23:
24049 case DW_OP_lit24:
24050 case DW_OP_lit25:
24051 case DW_OP_lit26:
24052 case DW_OP_lit27:
24053 case DW_OP_lit28:
24054 case DW_OP_lit29:
24055 case DW_OP_lit30:
24056 case DW_OP_lit31:
24057 stack[++stacki] = op - DW_OP_lit0;
24058 break;
24059
24060 case DW_OP_reg0:
24061 case DW_OP_reg1:
24062 case DW_OP_reg2:
24063 case DW_OP_reg3:
24064 case DW_OP_reg4:
24065 case DW_OP_reg5:
24066 case DW_OP_reg6:
24067 case DW_OP_reg7:
24068 case DW_OP_reg8:
24069 case DW_OP_reg9:
24070 case DW_OP_reg10:
24071 case DW_OP_reg11:
24072 case DW_OP_reg12:
24073 case DW_OP_reg13:
24074 case DW_OP_reg14:
24075 case DW_OP_reg15:
24076 case DW_OP_reg16:
24077 case DW_OP_reg17:
24078 case DW_OP_reg18:
24079 case DW_OP_reg19:
24080 case DW_OP_reg20:
24081 case DW_OP_reg21:
24082 case DW_OP_reg22:
24083 case DW_OP_reg23:
24084 case DW_OP_reg24:
24085 case DW_OP_reg25:
24086 case DW_OP_reg26:
24087 case DW_OP_reg27:
24088 case DW_OP_reg28:
24089 case DW_OP_reg29:
24090 case DW_OP_reg30:
24091 case DW_OP_reg31:
24092 stack[++stacki] = op - DW_OP_reg0;
24093 if (i < size)
24094 {
24095 if (computed == nullptr)
24096 dwarf2_complex_location_expr_complaint ();
24097 else
24098 return 0;
24099 }
24100 break;
24101
24102 case DW_OP_regx:
24103 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24104 i += bytes_read;
24105 stack[++stacki] = unsnd;
24106 if (i < size)
24107 {
24108 if (computed == nullptr)
24109 dwarf2_complex_location_expr_complaint ();
24110 else
24111 return 0;
24112 }
24113 break;
24114
24115 case DW_OP_addr:
24116 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24117 &bytes_read);
24118 i += bytes_read;
24119 break;
24120
24121 case DW_OP_const1u:
24122 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24123 i += 1;
24124 break;
24125
24126 case DW_OP_const1s:
24127 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24128 i += 1;
24129 break;
24130
24131 case DW_OP_const2u:
24132 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24133 i += 2;
24134 break;
24135
24136 case DW_OP_const2s:
24137 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24138 i += 2;
24139 break;
24140
24141 case DW_OP_const4u:
24142 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24143 i += 4;
24144 break;
24145
24146 case DW_OP_const4s:
24147 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24148 i += 4;
24149 break;
24150
24151 case DW_OP_const8u:
24152 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24153 i += 8;
24154 break;
24155
24156 case DW_OP_constu:
24157 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24158 &bytes_read);
24159 i += bytes_read;
24160 break;
24161
24162 case DW_OP_consts:
24163 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24164 i += bytes_read;
24165 break;
24166
24167 case DW_OP_dup:
24168 stack[stacki + 1] = stack[stacki];
24169 stacki++;
24170 break;
24171
24172 case DW_OP_plus:
24173 stack[stacki - 1] += stack[stacki];
24174 stacki--;
24175 break;
24176
24177 case DW_OP_plus_uconst:
24178 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24179 &bytes_read);
24180 i += bytes_read;
24181 break;
24182
24183 case DW_OP_minus:
24184 stack[stacki - 1] -= stack[stacki];
24185 stacki--;
24186 break;
24187
24188 case DW_OP_deref:
24189 /* If we're not the last op, then we definitely can't encode
24190 this using GDB's address_class enum. This is valid for partial
24191 global symbols, although the variable's address will be bogus
24192 in the psymtab. */
24193 if (i < size)
24194 {
24195 if (computed == nullptr)
24196 dwarf2_complex_location_expr_complaint ();
24197 else
24198 return 0;
24199 }
24200 break;
24201
24202 case DW_OP_GNU_push_tls_address:
24203 case DW_OP_form_tls_address:
24204 /* The top of the stack has the offset from the beginning
24205 of the thread control block at which the variable is located. */
24206 /* Nothing should follow this operator, so the top of stack would
24207 be returned. */
24208 /* This is valid for partial global symbols, but the variable's
24209 address will be bogus in the psymtab. Make it always at least
24210 non-zero to not look as a variable garbage collected by linker
24211 which have DW_OP_addr 0. */
24212 if (i < size)
24213 {
24214 if (computed == nullptr)
24215 dwarf2_complex_location_expr_complaint ();
24216 else
24217 return 0;
24218 }
24219 stack[stacki]++;
24220 break;
24221
24222 case DW_OP_GNU_uninit:
24223 if (computed != nullptr)
24224 return 0;
24225 break;
24226
24227 case DW_OP_addrx:
24228 case DW_OP_GNU_addr_index:
24229 case DW_OP_GNU_const_index:
24230 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24231 &bytes_read);
24232 i += bytes_read;
24233 break;
24234
24235 default:
24236 if (computed == nullptr)
24237 {
24238 const char *name = get_DW_OP_name (op);
24239
24240 if (name)
24241 complaint (_("unsupported stack op: '%s'"),
24242 name);
24243 else
24244 complaint (_("unsupported stack op: '%02x'"),
24245 op);
24246 }
24247
24248 return (stack[stacki]);
24249 }
24250
24251 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24252 outside of the allocated space. Also enforce minimum>0. */
24253 if (stacki >= ARRAY_SIZE (stack) - 1)
24254 {
24255 if (computed == nullptr)
24256 complaint (_("location description stack overflow"));
24257 return 0;
24258 }
24259
24260 if (stacki <= 0)
24261 {
24262 if (computed == nullptr)
24263 complaint (_("location description stack underflow"));
24264 return 0;
24265 }
24266 }
24267
24268 if (computed != nullptr)
24269 *computed = true;
24270 return (stack[stacki]);
24271 }
24272
24273 /* memory allocation interface */
24274
24275 static struct dwarf_block *
24276 dwarf_alloc_block (struct dwarf2_cu *cu)
24277 {
24278 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24279 }
24280
24281 static struct die_info *
24282 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24283 {
24284 struct die_info *die;
24285 size_t size = sizeof (struct die_info);
24286
24287 if (num_attrs > 1)
24288 size += (num_attrs - 1) * sizeof (struct attribute);
24289
24290 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24291 memset (die, 0, sizeof (struct die_info));
24292 return (die);
24293 }
24294
24295 \f
24296
24297 /* Macro support. */
24298
24299 /* An overload of dwarf_decode_macros that finds the correct section
24300 and ensures it is read in before calling the other overload. */
24301
24302 static void
24303 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24304 int section_is_gnu)
24305 {
24306 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24307 struct objfile *objfile = per_objfile->objfile;
24308 const struct line_header *lh = cu->line_header;
24309 unsigned int offset_size = cu->header.offset_size;
24310 struct dwarf2_section_info *section;
24311 const char *section_name;
24312
24313 if (cu->dwo_unit != nullptr)
24314 {
24315 if (section_is_gnu)
24316 {
24317 section = &cu->dwo_unit->dwo_file->sections.macro;
24318 section_name = ".debug_macro.dwo";
24319 }
24320 else
24321 {
24322 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24323 section_name = ".debug_macinfo.dwo";
24324 }
24325 }
24326 else
24327 {
24328 if (section_is_gnu)
24329 {
24330 section = &per_objfile->per_bfd->macro;
24331 section_name = ".debug_macro";
24332 }
24333 else
24334 {
24335 section = &per_objfile->per_bfd->macinfo;
24336 section_name = ".debug_macinfo";
24337 }
24338 }
24339
24340 section->read (objfile);
24341 if (section->buffer == nullptr)
24342 {
24343 complaint (_("missing %s section"), section_name);
24344 return;
24345 }
24346
24347 buildsym_compunit *builder = cu->get_builder ();
24348
24349 struct dwarf2_section_info *str_offsets_section;
24350 struct dwarf2_section_info *str_section;
24351 ULONGEST str_offsets_base;
24352
24353 if (cu->dwo_unit != nullptr)
24354 {
24355 str_offsets_section = &cu->dwo_unit->dwo_file
24356 ->sections.str_offsets;
24357 str_section = &cu->dwo_unit->dwo_file->sections.str;
24358 str_offsets_base = cu->header.addr_size;
24359 }
24360 else
24361 {
24362 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24363 str_section = &per_objfile->per_bfd->str;
24364 str_offsets_base = *cu->str_offsets_base;
24365 }
24366
24367 dwarf_decode_macros (per_objfile, builder, section, lh,
24368 offset_size, offset, str_section, str_offsets_section,
24369 str_offsets_base, section_is_gnu);
24370 }
24371
24372 /* Return the .debug_loc section to use for CU.
24373 For DWO files use .debug_loc.dwo. */
24374
24375 static struct dwarf2_section_info *
24376 cu_debug_loc_section (struct dwarf2_cu *cu)
24377 {
24378 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24379
24380 if (cu->dwo_unit)
24381 {
24382 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24383
24384 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24385 }
24386 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24387 : &per_objfile->per_bfd->loc);
24388 }
24389
24390 /* Return the .debug_rnglists section to use for CU. */
24391 static struct dwarf2_section_info *
24392 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24393 {
24394 if (cu->header.version < 5)
24395 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24396 cu->header.version);
24397 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24398
24399 /* Make sure we read the .debug_rnglists section from the file that
24400 contains the DW_AT_ranges attribute we are reading. Normally that
24401 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24402 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24403 program. */
24404 if (cu->dwo_unit != nullptr
24405 && tag != DW_TAG_compile_unit
24406 && tag != DW_TAG_skeleton_unit)
24407 {
24408 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24409
24410 if (sections->rnglists.size > 0)
24411 return &sections->rnglists;
24412 else
24413 error (_(".debug_rnglists section is missing from .dwo file."));
24414 }
24415 return &dwarf2_per_objfile->per_bfd->rnglists;
24416 }
24417
24418 /* A helper function that fills in a dwarf2_loclist_baton. */
24419
24420 static void
24421 fill_in_loclist_baton (struct dwarf2_cu *cu,
24422 struct dwarf2_loclist_baton *baton,
24423 const struct attribute *attr)
24424 {
24425 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24426 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24427
24428 section->read (per_objfile->objfile);
24429
24430 baton->per_objfile = per_objfile;
24431 baton->per_cu = cu->per_cu;
24432 gdb_assert (baton->per_cu);
24433 /* We don't know how long the location list is, but make sure we
24434 don't run off the edge of the section. */
24435 baton->size = section->size - attr->as_unsigned ();
24436 baton->data = section->buffer + attr->as_unsigned ();
24437 if (cu->base_address.has_value ())
24438 baton->base_address = *cu->base_address;
24439 else
24440 baton->base_address = 0;
24441 baton->from_dwo = cu->dwo_unit != NULL;
24442 }
24443
24444 static void
24445 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24446 struct dwarf2_cu *cu, int is_block)
24447 {
24448 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24449 struct objfile *objfile = per_objfile->objfile;
24450 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24451
24452 if (attr->form_is_section_offset ()
24453 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24454 the section. If so, fall through to the complaint in the
24455 other branch. */
24456 && attr->as_unsigned () < section->get_size (objfile))
24457 {
24458 struct dwarf2_loclist_baton *baton;
24459
24460 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24461
24462 fill_in_loclist_baton (cu, baton, attr);
24463
24464 if (!cu->base_address.has_value ())
24465 complaint (_("Location list used without "
24466 "specifying the CU base address."));
24467
24468 SYMBOL_ACLASS_INDEX (sym) = (is_block
24469 ? dwarf2_loclist_block_index
24470 : dwarf2_loclist_index);
24471 SYMBOL_LOCATION_BATON (sym) = baton;
24472 }
24473 else
24474 {
24475 struct dwarf2_locexpr_baton *baton;
24476
24477 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24478 baton->per_objfile = per_objfile;
24479 baton->per_cu = cu->per_cu;
24480 gdb_assert (baton->per_cu);
24481
24482 if (attr->form_is_block ())
24483 {
24484 /* Note that we're just copying the block's data pointer
24485 here, not the actual data. We're still pointing into the
24486 info_buffer for SYM's objfile; right now we never release
24487 that buffer, but when we do clean up properly this may
24488 need to change. */
24489 struct dwarf_block *block = attr->as_block ();
24490 baton->size = block->size;
24491 baton->data = block->data;
24492 }
24493 else
24494 {
24495 dwarf2_invalid_attrib_class_complaint ("location description",
24496 sym->natural_name ());
24497 baton->size = 0;
24498 }
24499
24500 SYMBOL_ACLASS_INDEX (sym) = (is_block
24501 ? dwarf2_locexpr_block_index
24502 : dwarf2_locexpr_index);
24503 SYMBOL_LOCATION_BATON (sym) = baton;
24504 }
24505 }
24506
24507 /* See read.h. */
24508
24509 const comp_unit_head *
24510 dwarf2_per_cu_data::get_header () const
24511 {
24512 if (!m_header_read_in)
24513 {
24514 const gdb_byte *info_ptr
24515 = this->section->buffer + to_underlying (this->sect_off);
24516
24517 memset (&m_header, 0, sizeof (m_header));
24518
24519 read_comp_unit_head (&m_header, info_ptr, this->section,
24520 rcuh_kind::COMPILE);
24521
24522 m_header_read_in = true;
24523 }
24524
24525 return &m_header;
24526 }
24527
24528 /* See read.h. */
24529
24530 int
24531 dwarf2_per_cu_data::addr_size () const
24532 {
24533 return this->get_header ()->addr_size;
24534 }
24535
24536 /* See read.h. */
24537
24538 int
24539 dwarf2_per_cu_data::offset_size () const
24540 {
24541 return this->get_header ()->offset_size;
24542 }
24543
24544 /* See read.h. */
24545
24546 int
24547 dwarf2_per_cu_data::ref_addr_size () const
24548 {
24549 const comp_unit_head *header = this->get_header ();
24550
24551 if (header->version == 2)
24552 return header->addr_size;
24553 else
24554 return header->offset_size;
24555 }
24556
24557 /* See read.h. */
24558
24559 struct type *
24560 dwarf2_cu::addr_type () const
24561 {
24562 struct objfile *objfile = this->per_objfile->objfile;
24563 struct type *void_type = objfile_type (objfile)->builtin_void;
24564 struct type *addr_type = lookup_pointer_type (void_type);
24565 int addr_size = this->per_cu->addr_size ();
24566
24567 if (TYPE_LENGTH (addr_type) == addr_size)
24568 return addr_type;
24569
24570 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
24571 return addr_type;
24572 }
24573
24574 /* A helper function for dwarf2_find_containing_comp_unit that returns
24575 the index of the result, and that searches a vector. It will
24576 return a result even if the offset in question does not actually
24577 occur in any CU. This is separate so that it can be unit
24578 tested. */
24579
24580 static int
24581 dwarf2_find_containing_comp_unit
24582 (sect_offset sect_off,
24583 unsigned int offset_in_dwz,
24584 const std::vector<std::unique_ptr<dwarf2_per_cu_data>> &all_comp_units)
24585 {
24586 int low, high;
24587
24588 low = 0;
24589 high = all_comp_units.size () - 1;
24590 while (high > low)
24591 {
24592 struct dwarf2_per_cu_data *mid_cu;
24593 int mid = low + (high - low) / 2;
24594
24595 mid_cu = all_comp_units[mid].get ();
24596 if (mid_cu->is_dwz > offset_in_dwz
24597 || (mid_cu->is_dwz == offset_in_dwz
24598 && mid_cu->sect_off + mid_cu->length > sect_off))
24599 high = mid;
24600 else
24601 low = mid + 1;
24602 }
24603 gdb_assert (low == high);
24604 return low;
24605 }
24606
24607 /* Locate the .debug_info compilation unit from CU's objfile which contains
24608 the DIE at OFFSET. Raises an error on failure. */
24609
24610 static struct dwarf2_per_cu_data *
24611 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24612 unsigned int offset_in_dwz,
24613 dwarf2_per_objfile *per_objfile)
24614 {
24615 int low = dwarf2_find_containing_comp_unit
24616 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24617 dwarf2_per_cu_data *this_cu
24618 = per_objfile->per_bfd->all_comp_units[low].get ();
24619
24620 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24621 {
24622 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24623 error (_("Dwarf Error: could not find partial DIE containing "
24624 "offset %s [in module %s]"),
24625 sect_offset_str (sect_off),
24626 bfd_get_filename (per_objfile->objfile->obfd));
24627
24628 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
24629 <= sect_off);
24630 return per_objfile->per_bfd->all_comp_units[low - 1].get ();
24631 }
24632 else
24633 {
24634 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
24635 && sect_off >= this_cu->sect_off + this_cu->length)
24636 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24637 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24638 return this_cu;
24639 }
24640 }
24641
24642 #if GDB_SELF_TEST
24643
24644 namespace selftests {
24645 namespace find_containing_comp_unit {
24646
24647 static void
24648 run_test ()
24649 {
24650 std::unique_ptr<dwarf2_per_cu_data> one (new dwarf2_per_cu_data);
24651 dwarf2_per_cu_data *one_ptr = one.get ();
24652 std::unique_ptr<dwarf2_per_cu_data> two (new dwarf2_per_cu_data);
24653 dwarf2_per_cu_data *two_ptr = two.get ();
24654 std::unique_ptr<dwarf2_per_cu_data> three (new dwarf2_per_cu_data);
24655 dwarf2_per_cu_data *three_ptr = three.get ();
24656 std::unique_ptr<dwarf2_per_cu_data> four (new dwarf2_per_cu_data);
24657 dwarf2_per_cu_data *four_ptr = four.get ();
24658
24659 one->length = 5;
24660 two->sect_off = sect_offset (one->length);
24661 two->length = 7;
24662
24663 three->length = 5;
24664 three->is_dwz = 1;
24665 four->sect_off = sect_offset (three->length);
24666 four->length = 7;
24667 four->is_dwz = 1;
24668
24669 std::vector<std::unique_ptr<dwarf2_per_cu_data>> units;
24670 units.push_back (std::move (one));
24671 units.push_back (std::move (two));
24672 units.push_back (std::move (three));
24673 units.push_back (std::move (four));
24674
24675 int result;
24676
24677 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24678 SELF_CHECK (units[result].get () == one_ptr);
24679 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24680 SELF_CHECK (units[result].get () == one_ptr);
24681 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24682 SELF_CHECK (units[result].get () == two_ptr);
24683
24684 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24685 SELF_CHECK (units[result].get () == three_ptr);
24686 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24687 SELF_CHECK (units[result].get () == three_ptr);
24688 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24689 SELF_CHECK (units[result].get () == four_ptr);
24690 }
24691
24692 }
24693 }
24694
24695 #endif /* GDB_SELF_TEST */
24696
24697 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
24698
24699 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
24700 dwarf2_per_objfile *per_objfile)
24701 : per_cu (per_cu),
24702 per_objfile (per_objfile),
24703 mark (false),
24704 has_loclist (false),
24705 checked_producer (false),
24706 producer_is_gxx_lt_4_6 (false),
24707 producer_is_gcc_lt_4_3 (false),
24708 producer_is_icc (false),
24709 producer_is_icc_lt_14 (false),
24710 producer_is_codewarrior (false),
24711 processing_has_namespace_info (false)
24712 {
24713 }
24714
24715 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24716
24717 static void
24718 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24719 enum language pretend_language)
24720 {
24721 struct attribute *attr;
24722
24723 /* Set the language we're debugging. */
24724 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24725 if (attr != nullptr)
24726 set_cu_language (attr->constant_value (0), cu);
24727 else
24728 {
24729 cu->language = pretend_language;
24730 cu->language_defn = language_def (cu->language);
24731 }
24732
24733 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24734 }
24735
24736 /* See read.h. */
24737
24738 dwarf2_cu *
24739 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24740 {
24741 auto it = m_dwarf2_cus.find (per_cu);
24742 if (it == m_dwarf2_cus.end ())
24743 return nullptr;
24744
24745 return it->second;
24746 }
24747
24748 /* See read.h. */
24749
24750 void
24751 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24752 {
24753 gdb_assert (this->get_cu (per_cu) == nullptr);
24754
24755 m_dwarf2_cus[per_cu] = cu;
24756 }
24757
24758 /* See read.h. */
24759
24760 void
24761 dwarf2_per_objfile::age_comp_units ()
24762 {
24763 dwarf_read_debug_printf_v ("running");
24764
24765 /* This is not expected to be called in the middle of CU expansion. There is
24766 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24767 loaded in memory. Calling age_comp_units while the queue is in use could
24768 make us free the DIEs for a CU that is in the queue and therefore break
24769 that invariant. */
24770 gdb_assert (!this->per_bfd->queue.has_value ());
24771
24772 /* Start by clearing all marks. */
24773 for (auto pair : m_dwarf2_cus)
24774 pair.second->mark = false;
24775
24776 /* Traverse all CUs, mark them and their dependencies if used recently
24777 enough. */
24778 for (auto pair : m_dwarf2_cus)
24779 {
24780 dwarf2_cu *cu = pair.second;
24781
24782 cu->last_used++;
24783 if (cu->last_used <= dwarf_max_cache_age)
24784 dwarf2_mark (cu);
24785 }
24786
24787 /* Delete all CUs still not marked. */
24788 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
24789 {
24790 dwarf2_cu *cu = it->second;
24791
24792 if (!cu->mark)
24793 {
24794 dwarf_read_debug_printf_v ("deleting old CU %s",
24795 sect_offset_str (cu->per_cu->sect_off));
24796 delete cu;
24797 it = m_dwarf2_cus.erase (it);
24798 }
24799 else
24800 it++;
24801 }
24802 }
24803
24804 /* See read.h. */
24805
24806 void
24807 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
24808 {
24809 auto it = m_dwarf2_cus.find (per_cu);
24810 if (it == m_dwarf2_cus.end ())
24811 return;
24812
24813 delete it->second;
24814
24815 m_dwarf2_cus.erase (it);
24816 }
24817
24818 dwarf2_per_objfile::~dwarf2_per_objfile ()
24819 {
24820 remove_all_cus ();
24821 }
24822
24823 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24824 We store these in a hash table separate from the DIEs, and preserve them
24825 when the DIEs are flushed out of cache.
24826
24827 The CU "per_cu" pointer is needed because offset alone is not enough to
24828 uniquely identify the type. A file may have multiple .debug_types sections,
24829 or the type may come from a DWO file. Furthermore, while it's more logical
24830 to use per_cu->section+offset, with Fission the section with the data is in
24831 the DWO file but we don't know that section at the point we need it.
24832 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24833 because we can enter the lookup routine, get_die_type_at_offset, from
24834 outside this file, and thus won't necessarily have PER_CU->cu.
24835 Fortunately, PER_CU is stable for the life of the objfile. */
24836
24837 struct dwarf2_per_cu_offset_and_type
24838 {
24839 const struct dwarf2_per_cu_data *per_cu;
24840 sect_offset sect_off;
24841 struct type *type;
24842 };
24843
24844 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24845
24846 static hashval_t
24847 per_cu_offset_and_type_hash (const void *item)
24848 {
24849 const struct dwarf2_per_cu_offset_and_type *ofs
24850 = (const struct dwarf2_per_cu_offset_and_type *) item;
24851
24852 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24853 }
24854
24855 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24856
24857 static int
24858 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24859 {
24860 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24861 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24862 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24863 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24864
24865 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24866 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24867 }
24868
24869 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24870 table if necessary. For convenience, return TYPE.
24871
24872 The DIEs reading must have careful ordering to:
24873 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24874 reading current DIE.
24875 * Not trying to dereference contents of still incompletely read in types
24876 while reading in other DIEs.
24877 * Enable referencing still incompletely read in types just by a pointer to
24878 the type without accessing its fields.
24879
24880 Therefore caller should follow these rules:
24881 * Try to fetch any prerequisite types we may need to build this DIE type
24882 before building the type and calling set_die_type.
24883 * After building type call set_die_type for current DIE as soon as
24884 possible before fetching more types to complete the current type.
24885 * Make the type as complete as possible before fetching more types. */
24886
24887 static struct type *
24888 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24889 bool skip_data_location)
24890 {
24891 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24892 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24893 struct objfile *objfile = per_objfile->objfile;
24894 struct attribute *attr;
24895 struct dynamic_prop prop;
24896
24897 /* For Ada types, make sure that the gnat-specific data is always
24898 initialized (if not already set). There are a few types where
24899 we should not be doing so, because the type-specific area is
24900 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24901 where the type-specific area is used to store the floatformat).
24902 But this is not a problem, because the gnat-specific information
24903 is actually not needed for these types. */
24904 if (need_gnat_info (cu)
24905 && type->code () != TYPE_CODE_FUNC
24906 && type->code () != TYPE_CODE_FLT
24907 && type->code () != TYPE_CODE_METHODPTR
24908 && type->code () != TYPE_CODE_MEMBERPTR
24909 && type->code () != TYPE_CODE_METHOD
24910 && type->code () != TYPE_CODE_FIXED_POINT
24911 && !HAVE_GNAT_AUX_INFO (type))
24912 INIT_GNAT_SPECIFIC (type);
24913
24914 /* Read DW_AT_allocated and set in type. */
24915 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24916 if (attr != NULL)
24917 {
24918 struct type *prop_type = cu->addr_sized_int_type (false);
24919 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24920 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
24921 }
24922
24923 /* Read DW_AT_associated and set in type. */
24924 attr = dwarf2_attr (die, DW_AT_associated, cu);
24925 if (attr != NULL)
24926 {
24927 struct type *prop_type = cu->addr_sized_int_type (false);
24928 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24929 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
24930 }
24931
24932 /* Read DW_AT_data_location and set in type. */
24933 if (!skip_data_location)
24934 {
24935 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24936 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24937 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24938 }
24939
24940 if (per_objfile->die_type_hash == NULL)
24941 per_objfile->die_type_hash
24942 = htab_up (htab_create_alloc (127,
24943 per_cu_offset_and_type_hash,
24944 per_cu_offset_and_type_eq,
24945 NULL, xcalloc, xfree));
24946
24947 ofs.per_cu = cu->per_cu;
24948 ofs.sect_off = die->sect_off;
24949 ofs.type = type;
24950 slot = (struct dwarf2_per_cu_offset_and_type **)
24951 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
24952 if (*slot)
24953 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24954 sect_offset_str (die->sect_off));
24955 *slot = XOBNEW (&objfile->objfile_obstack,
24956 struct dwarf2_per_cu_offset_and_type);
24957 **slot = ofs;
24958 return type;
24959 }
24960
24961 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24962 or return NULL if the die does not have a saved type. */
24963
24964 static struct type *
24965 get_die_type_at_offset (sect_offset sect_off,
24966 dwarf2_per_cu_data *per_cu,
24967 dwarf2_per_objfile *per_objfile)
24968 {
24969 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24970
24971 if (per_objfile->die_type_hash == NULL)
24972 return NULL;
24973
24974 ofs.per_cu = per_cu;
24975 ofs.sect_off = sect_off;
24976 slot = ((struct dwarf2_per_cu_offset_and_type *)
24977 htab_find (per_objfile->die_type_hash.get (), &ofs));
24978 if (slot)
24979 return slot->type;
24980 else
24981 return NULL;
24982 }
24983
24984 /* Look up the type for DIE in CU in die_type_hash,
24985 or return NULL if DIE does not have a saved type. */
24986
24987 static struct type *
24988 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24989 {
24990 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
24991 }
24992
24993 /* Add a dependence relationship from CU to REF_PER_CU. */
24994
24995 static void
24996 dwarf2_add_dependence (struct dwarf2_cu *cu,
24997 struct dwarf2_per_cu_data *ref_per_cu)
24998 {
24999 void **slot;
25000
25001 if (cu->dependencies == NULL)
25002 cu->dependencies
25003 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25004 NULL, &cu->comp_unit_obstack,
25005 hashtab_obstack_allocate,
25006 dummy_obstack_deallocate);
25007
25008 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25009 if (*slot == NULL)
25010 *slot = ref_per_cu;
25011 }
25012
25013 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25014 Set the mark field in every compilation unit in the
25015 cache that we must keep because we are keeping CU.
25016
25017 DATA is the dwarf2_per_objfile object in which to look up CUs. */
25018
25019 static int
25020 dwarf2_mark_helper (void **slot, void *data)
25021 {
25022 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25023 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25024 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
25025
25026 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25027 reading of the chain. As such dependencies remain valid it is not much
25028 useful to track and undo them during QUIT cleanups. */
25029 if (cu == nullptr)
25030 return 1;
25031
25032 if (cu->mark)
25033 return 1;
25034
25035 cu->mark = true;
25036
25037 if (cu->dependencies != nullptr)
25038 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
25039
25040 return 1;
25041 }
25042
25043 /* Set the mark field in CU and in every other compilation unit in the
25044 cache that we must keep because we are keeping CU. */
25045
25046 static void
25047 dwarf2_mark (struct dwarf2_cu *cu)
25048 {
25049 if (cu->mark)
25050 return;
25051
25052 cu->mark = true;
25053
25054 if (cu->dependencies != nullptr)
25055 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
25056 }
25057
25058 /* Trivial hash function for partial_die_info: the hash value of a DIE
25059 is its offset in .debug_info for this objfile. */
25060
25061 static hashval_t
25062 partial_die_hash (const void *item)
25063 {
25064 const struct partial_die_info *part_die
25065 = (const struct partial_die_info *) item;
25066
25067 return to_underlying (part_die->sect_off);
25068 }
25069
25070 /* Trivial comparison function for partial_die_info structures: two DIEs
25071 are equal if they have the same offset. */
25072
25073 static int
25074 partial_die_eq (const void *item_lhs, const void *item_rhs)
25075 {
25076 const struct partial_die_info *part_die_lhs
25077 = (const struct partial_die_info *) item_lhs;
25078 const struct partial_die_info *part_die_rhs
25079 = (const struct partial_die_info *) item_rhs;
25080
25081 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25082 }
25083
25084 struct cmd_list_element *set_dwarf_cmdlist;
25085 struct cmd_list_element *show_dwarf_cmdlist;
25086
25087 static void
25088 show_check_physname (struct ui_file *file, int from_tty,
25089 struct cmd_list_element *c, const char *value)
25090 {
25091 fprintf_filtered (file,
25092 _("Whether to check \"physname\" is %s.\n"),
25093 value);
25094 }
25095
25096 void _initialize_dwarf2_read ();
25097 void
25098 _initialize_dwarf2_read ()
25099 {
25100 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
25101 Set DWARF specific variables.\n\
25102 Configure DWARF variables such as the cache size."),
25103 &set_dwarf_cmdlist, "maintenance set dwarf ",
25104 0/*allow-unknown*/, &maintenance_set_cmdlist);
25105
25106 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
25107 Show DWARF specific variables.\n\
25108 Show DWARF variables such as the cache size."),
25109 &show_dwarf_cmdlist, "maintenance show dwarf ",
25110 0/*allow-unknown*/, &maintenance_show_cmdlist);
25111
25112 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25113 &dwarf_max_cache_age, _("\
25114 Set the upper bound on the age of cached DWARF compilation units."), _("\
25115 Show the upper bound on the age of cached DWARF compilation units."), _("\
25116 A higher limit means that cached compilation units will be stored\n\
25117 in memory longer, and more total memory will be used. Zero disables\n\
25118 caching, which can slow down startup."),
25119 NULL,
25120 show_dwarf_max_cache_age,
25121 &set_dwarf_cmdlist,
25122 &show_dwarf_cmdlist);
25123
25124 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25125 Set debugging of the DWARF reader."), _("\
25126 Show debugging of the DWARF reader."), _("\
25127 When enabled (non-zero), debugging messages are printed during DWARF\n\
25128 reading and symtab expansion. A value of 1 (one) provides basic\n\
25129 information. A value greater than 1 provides more verbose information."),
25130 NULL,
25131 NULL,
25132 &setdebuglist, &showdebuglist);
25133
25134 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25135 Set debugging of the DWARF DIE reader."), _("\
25136 Show debugging of the DWARF DIE reader."), _("\
25137 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25138 The value is the maximum depth to print."),
25139 NULL,
25140 NULL,
25141 &setdebuglist, &showdebuglist);
25142
25143 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25144 Set debugging of the dwarf line reader."), _("\
25145 Show debugging of the dwarf line reader."), _("\
25146 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25147 A value of 1 (one) provides basic information.\n\
25148 A value greater than 1 provides more verbose information."),
25149 NULL,
25150 NULL,
25151 &setdebuglist, &showdebuglist);
25152
25153 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25154 Set cross-checking of \"physname\" code against demangler."), _("\
25155 Show cross-checking of \"physname\" code against demangler."), _("\
25156 When enabled, GDB's internal \"physname\" code is checked against\n\
25157 the demangler."),
25158 NULL, show_check_physname,
25159 &setdebuglist, &showdebuglist);
25160
25161 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25162 no_class, &use_deprecated_index_sections, _("\
25163 Set whether to use deprecated gdb_index sections."), _("\
25164 Show whether to use deprecated gdb_index sections."), _("\
25165 When enabled, deprecated .gdb_index sections are used anyway.\n\
25166 Normally they are ignored either because of a missing feature or\n\
25167 performance issue.\n\
25168 Warning: This option must be enabled before gdb reads the file."),
25169 NULL,
25170 NULL,
25171 &setlist, &showlist);
25172
25173 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25174 &dwarf2_locexpr_funcs);
25175 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25176 &dwarf2_loclist_funcs);
25177
25178 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25179 &dwarf2_block_frame_base_locexpr_funcs);
25180 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25181 &dwarf2_block_frame_base_loclist_funcs);
25182
25183 #if GDB_SELF_TEST
25184 selftests::register_test ("dw2_expand_symtabs_matching",
25185 selftests::dw2_expand_symtabs_matching::run_test);
25186 selftests::register_test ("dwarf2_find_containing_comp_unit",
25187 selftests::find_containing_comp_unit::run_test);
25188 #endif
25189 }