c3a502ec44552cb10a90733a183e3537dacff143
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2018 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 "bfd.h"
33 #include "elf-bfd.h"
34 #include "symtab.h"
35 #include "gdbtypes.h"
36 #include "objfiles.h"
37 #include "dwarf2.h"
38 #include "buildsym.h"
39 #include "demangle.h"
40 #include "gdb-demangle.h"
41 #include "expression.h"
42 #include "filenames.h" /* for DOSish file names */
43 #include "macrotab.h"
44 #include "language.h"
45 #include "complaints.h"
46 #include "bcache.h"
47 #include "dwarf2expr.h"
48 #include "dwarf2loc.h"
49 #include "cp-support.h"
50 #include "hashtab.h"
51 #include "command.h"
52 #include "gdbcmd.h"
53 #include "block.h"
54 #include "addrmap.h"
55 #include "typeprint.h"
56 #include "psympriv.h"
57 #include <sys/stat.h>
58 #include "completer.h"
59 #include "vec.h"
60 #include "c-lang.h"
61 #include "go-lang.h"
62 #include "valprint.h"
63 #include "gdbcore.h" /* for gnutarget */
64 #include "gdb/gdb-index.h"
65 #include <ctype.h>
66 #include "gdb_bfd.h"
67 #include "f-lang.h"
68 #include "source.h"
69 #include "filestuff.h"
70 #include "build-id.h"
71 #include "namespace.h"
72 #include "common/gdb_unlinker.h"
73 #include "common/function-view.h"
74 #include "common/gdb_optional.h"
75 #include "common/underlying.h"
76 #include "common/byte-vector.h"
77 #include "common/hash_enum.h"
78 #include "filename-seen-cache.h"
79 #include "producer.h"
80 #include <fcntl.h>
81 #include <sys/types.h>
82 #include <algorithm>
83 #include <unordered_set>
84 #include <unordered_map>
85 #include "selftest.h"
86 #include <cmath>
87 #include <set>
88 #include <forward_list>
89 #include "rust-lang.h"
90 #include "common/pathstuff.h"
91
92 /* When == 1, print basic high level tracing messages.
93 When > 1, be more verbose.
94 This is in contrast to the low level DIE reading of dwarf_die_debug. */
95 static unsigned int dwarf_read_debug = 0;
96
97 /* When non-zero, dump DIEs after they are read in. */
98 static unsigned int dwarf_die_debug = 0;
99
100 /* When non-zero, dump line number entries as they are read in. */
101 static unsigned int dwarf_line_debug = 0;
102
103 /* When non-zero, cross-check physname against demangler. */
104 static int check_physname = 0;
105
106 /* When non-zero, do not reject deprecated .gdb_index sections. */
107 static int use_deprecated_index_sections = 0;
108
109 static const struct objfile_data *dwarf2_objfile_data_key;
110
111 /* The "aclass" indices for various kinds of computed DWARF symbols. */
112
113 static int dwarf2_locexpr_index;
114 static int dwarf2_loclist_index;
115 static int dwarf2_locexpr_block_index;
116 static int dwarf2_loclist_block_index;
117
118 /* A descriptor for dwarf sections.
119
120 S.ASECTION, SIZE are typically initialized when the objfile is first
121 scanned. BUFFER, READIN are filled in later when the section is read.
122 If the section contained compressed data then SIZE is updated to record
123 the uncompressed size of the section.
124
125 DWP file format V2 introduces a wrinkle that is easiest to handle by
126 creating the concept of virtual sections contained within a real section.
127 In DWP V2 the sections of the input DWO files are concatenated together
128 into one section, but section offsets are kept relative to the original
129 input section.
130 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
131 the real section this "virtual" section is contained in, and BUFFER,SIZE
132 describe the virtual section. */
133
134 struct dwarf2_section_info
135 {
136 union
137 {
138 /* If this is a real section, the bfd section. */
139 asection *section;
140 /* If this is a virtual section, pointer to the containing ("real")
141 section. */
142 struct dwarf2_section_info *containing_section;
143 } s;
144 /* Pointer to section data, only valid if readin. */
145 const gdb_byte *buffer;
146 /* The size of the section, real or virtual. */
147 bfd_size_type size;
148 /* If this is a virtual section, the offset in the real section.
149 Only valid if is_virtual. */
150 bfd_size_type virtual_offset;
151 /* True if we have tried to read this section. */
152 char readin;
153 /* True if this is a virtual section, False otherwise.
154 This specifies which of s.section and s.containing_section to use. */
155 char is_virtual;
156 };
157
158 typedef struct dwarf2_section_info dwarf2_section_info_def;
159 DEF_VEC_O (dwarf2_section_info_def);
160
161 /* All offsets in the index are of this type. It must be
162 architecture-independent. */
163 typedef uint32_t offset_type;
164
165 /* Ensure only legit values are used. */
166 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
167 do { \
168 gdb_assert ((unsigned int) (value) <= 1); \
169 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
170 } while (0)
171
172 /* Ensure only legit values are used. */
173 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
174 do { \
175 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
176 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
177 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
178 } while (0)
179
180 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
181 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
182 do { \
183 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
184 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
185 } while (0)
186
187 #if WORDS_BIGENDIAN
188
189 /* Convert VALUE between big- and little-endian. */
190
191 static offset_type
192 byte_swap (offset_type value)
193 {
194 offset_type result;
195
196 result = (value & 0xff) << 24;
197 result |= (value & 0xff00) << 8;
198 result |= (value & 0xff0000) >> 8;
199 result |= (value & 0xff000000) >> 24;
200 return result;
201 }
202
203 #define MAYBE_SWAP(V) byte_swap (V)
204
205 #else
206 #define MAYBE_SWAP(V) static_cast<offset_type> (V)
207 #endif /* WORDS_BIGENDIAN */
208
209 /* An index into a (C++) symbol name component in a symbol name as
210 recorded in the mapped_index's symbol table. For each C++ symbol
211 in the symbol table, we record one entry for the start of each
212 component in the symbol in a table of name components, and then
213 sort the table, in order to be able to binary search symbol names,
214 ignoring leading namespaces, both completion and regular look up.
215 For example, for symbol "A::B::C", we'll have an entry that points
216 to "A::B::C", another that points to "B::C", and another for "C".
217 Note that function symbols in GDB index have no parameter
218 information, just the function/method names. You can convert a
219 name_component to a "const char *" using the
220 'mapped_index::symbol_name_at(offset_type)' method. */
221
222 struct name_component
223 {
224 /* Offset in the symbol name where the component starts. Stored as
225 a (32-bit) offset instead of a pointer to save memory and improve
226 locality on 64-bit architectures. */
227 offset_type name_offset;
228
229 /* The symbol's index in the symbol and constant pool tables of a
230 mapped_index. */
231 offset_type idx;
232 };
233
234 /* Base class containing bits shared by both .gdb_index and
235 .debug_name indexes. */
236
237 struct mapped_index_base
238 {
239 /* The name_component table (a sorted vector). See name_component's
240 description above. */
241 std::vector<name_component> name_components;
242
243 /* How NAME_COMPONENTS is sorted. */
244 enum case_sensitivity name_components_casing;
245
246 /* Return the number of names in the symbol table. */
247 virtual size_t symbol_name_count () const = 0;
248
249 /* Get the name of the symbol at IDX in the symbol table. */
250 virtual const char *symbol_name_at (offset_type idx) const = 0;
251
252 /* Return whether the name at IDX in the symbol table should be
253 ignored. */
254 virtual bool symbol_name_slot_invalid (offset_type idx) const
255 {
256 return false;
257 }
258
259 /* Build the symbol name component sorted vector, if we haven't
260 yet. */
261 void build_name_components ();
262
263 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
264 possible matches for LN_NO_PARAMS in the name component
265 vector. */
266 std::pair<std::vector<name_component>::const_iterator,
267 std::vector<name_component>::const_iterator>
268 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
269
270 /* Prevent deleting/destroying via a base class pointer. */
271 protected:
272 ~mapped_index_base() = default;
273 };
274
275 /* A description of the mapped index. The file format is described in
276 a comment by the code that writes the index. */
277 struct mapped_index final : public mapped_index_base
278 {
279 /* A slot/bucket in the symbol table hash. */
280 struct symbol_table_slot
281 {
282 const offset_type name;
283 const offset_type vec;
284 };
285
286 /* Index data format version. */
287 int version;
288
289 /* The total length of the buffer. */
290 off_t total_size;
291
292 /* The address table data. */
293 gdb::array_view<const gdb_byte> address_table;
294
295 /* The symbol table, implemented as a hash table. */
296 gdb::array_view<symbol_table_slot> symbol_table;
297
298 /* A pointer to the constant pool. */
299 const char *constant_pool;
300
301 bool symbol_name_slot_invalid (offset_type idx) const override
302 {
303 const auto &bucket = this->symbol_table[idx];
304 return bucket.name == 0 && bucket.vec;
305 }
306
307 /* Convenience method to get at the name of the symbol at IDX in the
308 symbol table. */
309 const char *symbol_name_at (offset_type idx) const override
310 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
311
312 size_t symbol_name_count () const override
313 { return this->symbol_table.size (); }
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 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
321 : dwarf2_per_objfile (dwarf2_per_objfile_)
322 {}
323
324 struct dwarf2_per_objfile *dwarf2_per_objfile;
325 bfd_endian dwarf5_byte_order;
326 bool dwarf5_is_dwarf64;
327 bool augmentation_is_gdb;
328 uint8_t offset_size;
329 uint32_t cu_count = 0;
330 uint32_t tu_count, bucket_count, name_count;
331 const gdb_byte *cu_table_reordered, *tu_table_reordered;
332 const uint32_t *bucket_table_reordered, *hash_table_reordered;
333 const gdb_byte *name_table_string_offs_reordered;
334 const gdb_byte *name_table_entry_offs_reordered;
335 const gdb_byte *entry_pool;
336
337 struct index_val
338 {
339 ULONGEST dwarf_tag;
340 struct attr
341 {
342 /* Attribute name DW_IDX_*. */
343 ULONGEST dw_idx;
344
345 /* Attribute form DW_FORM_*. */
346 ULONGEST form;
347
348 /* Value if FORM is DW_FORM_implicit_const. */
349 LONGEST implicit_const;
350 };
351 std::vector<attr> attr_vec;
352 };
353
354 std::unordered_map<ULONGEST, index_val> abbrev_map;
355
356 const char *namei_to_name (uint32_t namei) const;
357
358 /* Implementation of the mapped_index_base virtual interface, for
359 the name_components cache. */
360
361 const char *symbol_name_at (offset_type idx) const override
362 { return namei_to_name (idx); }
363
364 size_t symbol_name_count () const override
365 { return this->name_count; }
366 };
367
368 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
369 DEF_VEC_P (dwarf2_per_cu_ptr);
370
371 struct tu_stats
372 {
373 int nr_uniq_abbrev_tables;
374 int nr_symtabs;
375 int nr_symtab_sharers;
376 int nr_stmt_less_type_units;
377 int nr_all_type_units_reallocs;
378 };
379
380 /* Collection of data recorded per objfile.
381 This hangs off of dwarf2_objfile_data_key. */
382
383 struct dwarf2_per_objfile : public allocate_on_obstack
384 {
385 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
386 dwarf2 section names, or is NULL if the standard ELF names are
387 used. */
388 dwarf2_per_objfile (struct objfile *objfile,
389 const dwarf2_debug_sections *names);
390
391 ~dwarf2_per_objfile ();
392
393 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
394
395 /* Free all cached compilation units. */
396 void free_cached_comp_units ();
397 private:
398 /* This function is mapped across the sections and remembers the
399 offset and size of each of the debugging sections we are
400 interested in. */
401 void locate_sections (bfd *abfd, asection *sectp,
402 const dwarf2_debug_sections &names);
403
404 public:
405 dwarf2_section_info info {};
406 dwarf2_section_info abbrev {};
407 dwarf2_section_info line {};
408 dwarf2_section_info loc {};
409 dwarf2_section_info loclists {};
410 dwarf2_section_info macinfo {};
411 dwarf2_section_info macro {};
412 dwarf2_section_info str {};
413 dwarf2_section_info line_str {};
414 dwarf2_section_info ranges {};
415 dwarf2_section_info rnglists {};
416 dwarf2_section_info addr {};
417 dwarf2_section_info frame {};
418 dwarf2_section_info eh_frame {};
419 dwarf2_section_info gdb_index {};
420 dwarf2_section_info debug_names {};
421 dwarf2_section_info debug_aranges {};
422
423 VEC (dwarf2_section_info_def) *types = NULL;
424
425 /* Back link. */
426 struct objfile *objfile = NULL;
427
428 /* Table of all the compilation units. This is used to locate
429 the target compilation unit of a particular reference. */
430 struct dwarf2_per_cu_data **all_comp_units = NULL;
431
432 /* The number of compilation units in ALL_COMP_UNITS. */
433 int n_comp_units = 0;
434
435 /* The number of .debug_types-related CUs. */
436 int n_type_units = 0;
437
438 /* The number of elements allocated in all_type_units.
439 If there are skeleton-less TUs, we add them to all_type_units lazily. */
440 int n_allocated_type_units = 0;
441
442 /* The .debug_types-related CUs (TUs).
443 This is stored in malloc space because we may realloc it. */
444 struct signatured_type **all_type_units = NULL;
445
446 /* Table of struct type_unit_group objects.
447 The hash key is the DW_AT_stmt_list value. */
448 htab_t type_unit_groups {};
449
450 /* A table mapping .debug_types signatures to its signatured_type entry.
451 This is NULL if the .debug_types section hasn't been read in yet. */
452 htab_t signatured_types {};
453
454 /* Type unit statistics, to see how well the scaling improvements
455 are doing. */
456 struct tu_stats tu_stats {};
457
458 /* A chain of compilation units that are currently read in, so that
459 they can be freed later. */
460 dwarf2_per_cu_data *read_in_chain = NULL;
461
462 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
463 This is NULL if the table hasn't been allocated yet. */
464 htab_t dwo_files {};
465
466 /* True if we've checked for whether there is a DWP file. */
467 bool dwp_checked = false;
468
469 /* The DWP file if there is one, or NULL. */
470 struct dwp_file *dwp_file = NULL;
471
472 /* The shared '.dwz' file, if one exists. This is used when the
473 original data was compressed using 'dwz -m'. */
474 struct dwz_file *dwz_file = NULL;
475
476 /* A flag indicating whether this objfile has a section loaded at a
477 VMA of 0. */
478 bool has_section_at_zero = false;
479
480 /* True if we are using the mapped index,
481 or we are faking it for OBJF_READNOW's sake. */
482 bool using_index = false;
483
484 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
485 mapped_index *index_table = NULL;
486
487 /* The mapped index, or NULL if .debug_names is missing or not being used. */
488 std::unique_ptr<mapped_debug_names> debug_names_table;
489
490 /* When using index_table, this keeps track of all quick_file_names entries.
491 TUs typically share line table entries with a CU, so we maintain a
492 separate table of all line table entries to support the sharing.
493 Note that while there can be way more TUs than CUs, we've already
494 sorted all the TUs into "type unit groups", grouped by their
495 DW_AT_stmt_list value. Therefore the only sharing done here is with a
496 CU and its associated TU group if there is one. */
497 htab_t quick_file_names_table {};
498
499 /* Set during partial symbol reading, to prevent queueing of full
500 symbols. */
501 bool reading_partial_symbols = false;
502
503 /* Table mapping type DIEs to their struct type *.
504 This is NULL if not allocated yet.
505 The mapping is done via (CU/TU + DIE offset) -> type. */
506 htab_t die_type_hash {};
507
508 /* The CUs we recently read. */
509 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
510
511 /* Table containing line_header indexed by offset and offset_in_dwz. */
512 htab_t line_header_hash {};
513
514 /* Table containing all filenames. This is an optional because the
515 table is lazily constructed on first access. */
516 gdb::optional<filename_seen_cache> filenames_cache;
517 };
518
519 /* Get the dwarf2_per_objfile associated to OBJFILE. */
520
521 struct dwarf2_per_objfile *
522 get_dwarf2_per_objfile (struct objfile *objfile)
523 {
524 return ((struct dwarf2_per_objfile *)
525 objfile_data (objfile, dwarf2_objfile_data_key));
526 }
527
528 /* Set the dwarf2_per_objfile associated to OBJFILE. */
529
530 void
531 set_dwarf2_per_objfile (struct objfile *objfile,
532 struct dwarf2_per_objfile *dwarf2_per_objfile)
533 {
534 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
535 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
536 }
537
538 /* Default names of the debugging sections. */
539
540 /* Note that if the debugging section has been compressed, it might
541 have a name like .zdebug_info. */
542
543 static const struct dwarf2_debug_sections dwarf2_elf_names =
544 {
545 { ".debug_info", ".zdebug_info" },
546 { ".debug_abbrev", ".zdebug_abbrev" },
547 { ".debug_line", ".zdebug_line" },
548 { ".debug_loc", ".zdebug_loc" },
549 { ".debug_loclists", ".zdebug_loclists" },
550 { ".debug_macinfo", ".zdebug_macinfo" },
551 { ".debug_macro", ".zdebug_macro" },
552 { ".debug_str", ".zdebug_str" },
553 { ".debug_line_str", ".zdebug_line_str" },
554 { ".debug_ranges", ".zdebug_ranges" },
555 { ".debug_rnglists", ".zdebug_rnglists" },
556 { ".debug_types", ".zdebug_types" },
557 { ".debug_addr", ".zdebug_addr" },
558 { ".debug_frame", ".zdebug_frame" },
559 { ".eh_frame", NULL },
560 { ".gdb_index", ".zgdb_index" },
561 { ".debug_names", ".zdebug_names" },
562 { ".debug_aranges", ".zdebug_aranges" },
563 23
564 };
565
566 /* List of DWO/DWP sections. */
567
568 static const struct dwop_section_names
569 {
570 struct dwarf2_section_names abbrev_dwo;
571 struct dwarf2_section_names info_dwo;
572 struct dwarf2_section_names line_dwo;
573 struct dwarf2_section_names loc_dwo;
574 struct dwarf2_section_names loclists_dwo;
575 struct dwarf2_section_names macinfo_dwo;
576 struct dwarf2_section_names macro_dwo;
577 struct dwarf2_section_names str_dwo;
578 struct dwarf2_section_names str_offsets_dwo;
579 struct dwarf2_section_names types_dwo;
580 struct dwarf2_section_names cu_index;
581 struct dwarf2_section_names tu_index;
582 }
583 dwop_section_names =
584 {
585 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
586 { ".debug_info.dwo", ".zdebug_info.dwo" },
587 { ".debug_line.dwo", ".zdebug_line.dwo" },
588 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
589 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
590 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
591 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
592 { ".debug_str.dwo", ".zdebug_str.dwo" },
593 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
594 { ".debug_types.dwo", ".zdebug_types.dwo" },
595 { ".debug_cu_index", ".zdebug_cu_index" },
596 { ".debug_tu_index", ".zdebug_tu_index" },
597 };
598
599 /* local data types */
600
601 /* The data in a compilation unit header, after target2host
602 translation, looks like this. */
603 struct comp_unit_head
604 {
605 unsigned int length;
606 short version;
607 unsigned char addr_size;
608 unsigned char signed_addr_p;
609 sect_offset abbrev_sect_off;
610
611 /* Size of file offsets; either 4 or 8. */
612 unsigned int offset_size;
613
614 /* Size of the length field; either 4 or 12. */
615 unsigned int initial_length_size;
616
617 enum dwarf_unit_type unit_type;
618
619 /* Offset to the first byte of this compilation unit header in the
620 .debug_info section, for resolving relative reference dies. */
621 sect_offset sect_off;
622
623 /* Offset to first die in this cu from the start of the cu.
624 This will be the first byte following the compilation unit header. */
625 cu_offset first_die_cu_offset;
626
627 /* 64-bit signature of this type unit - it is valid only for
628 UNIT_TYPE DW_UT_type. */
629 ULONGEST signature;
630
631 /* For types, offset in the type's DIE of the type defined by this TU. */
632 cu_offset type_cu_offset_in_tu;
633 };
634
635 /* Type used for delaying computation of method physnames.
636 See comments for compute_delayed_physnames. */
637 struct delayed_method_info
638 {
639 /* The type to which the method is attached, i.e., its parent class. */
640 struct type *type;
641
642 /* The index of the method in the type's function fieldlists. */
643 int fnfield_index;
644
645 /* The index of the method in the fieldlist. */
646 int index;
647
648 /* The name of the DIE. */
649 const char *name;
650
651 /* The DIE associated with this method. */
652 struct die_info *die;
653 };
654
655 /* Internal state when decoding a particular compilation unit. */
656 struct dwarf2_cu
657 {
658 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
659 ~dwarf2_cu ();
660
661 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
662
663 /* The header of the compilation unit. */
664 struct comp_unit_head header {};
665
666 /* Base address of this compilation unit. */
667 CORE_ADDR base_address = 0;
668
669 /* Non-zero if base_address has been set. */
670 int base_known = 0;
671
672 /* The language we are debugging. */
673 enum language language = language_unknown;
674 const struct language_defn *language_defn = nullptr;
675
676 const char *producer = nullptr;
677
678 /* The generic symbol table building routines have separate lists for
679 file scope symbols and all all other scopes (local scopes). So
680 we need to select the right one to pass to add_symbol_to_list().
681 We do it by keeping a pointer to the correct list in list_in_scope.
682
683 FIXME: The original dwarf code just treated the file scope as the
684 first local scope, and all other local scopes as nested local
685 scopes, and worked fine. Check to see if we really need to
686 distinguish these in buildsym.c. */
687 struct pending **list_in_scope = nullptr;
688
689 /* Hash table holding all the loaded partial DIEs
690 with partial_die->offset.SECT_OFF as hash. */
691 htab_t partial_dies = nullptr;
692
693 /* Storage for things with the same lifetime as this read-in compilation
694 unit, including partial DIEs. */
695 auto_obstack comp_unit_obstack;
696
697 /* When multiple dwarf2_cu structures are living in memory, this field
698 chains them all together, so that they can be released efficiently.
699 We will probably also want a generation counter so that most-recently-used
700 compilation units are cached... */
701 struct dwarf2_per_cu_data *read_in_chain = nullptr;
702
703 /* Backlink to our per_cu entry. */
704 struct dwarf2_per_cu_data *per_cu;
705
706 /* How many compilation units ago was this CU last referenced? */
707 int last_used = 0;
708
709 /* A hash table of DIE cu_offset for following references with
710 die_info->offset.sect_off as hash. */
711 htab_t die_hash = nullptr;
712
713 /* Full DIEs if read in. */
714 struct die_info *dies = nullptr;
715
716 /* A set of pointers to dwarf2_per_cu_data objects for compilation
717 units referenced by this one. Only set during full symbol processing;
718 partial symbol tables do not have dependencies. */
719 htab_t dependencies = nullptr;
720
721 /* Header data from the line table, during full symbol processing. */
722 struct line_header *line_header = nullptr;
723 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
724 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
725 this is the DW_TAG_compile_unit die for this CU. We'll hold on
726 to the line header as long as this DIE is being processed. See
727 process_die_scope. */
728 die_info *line_header_die_owner = nullptr;
729
730 /* A list of methods which need to have physnames computed
731 after all type information has been read. */
732 std::vector<delayed_method_info> method_list;
733
734 /* To be copied to symtab->call_site_htab. */
735 htab_t call_site_htab = nullptr;
736
737 /* Non-NULL if this CU came from a DWO file.
738 There is an invariant here that is important to remember:
739 Except for attributes copied from the top level DIE in the "main"
740 (or "stub") file in preparation for reading the DWO file
741 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
742 Either there isn't a DWO file (in which case this is NULL and the point
743 is moot), or there is and either we're not going to read it (in which
744 case this is NULL) or there is and we are reading it (in which case this
745 is non-NULL). */
746 struct dwo_unit *dwo_unit = nullptr;
747
748 /* The DW_AT_addr_base attribute if present, zero otherwise
749 (zero is a valid value though).
750 Note this value comes from the Fission stub CU/TU's DIE. */
751 ULONGEST addr_base = 0;
752
753 /* The DW_AT_ranges_base attribute if present, zero otherwise
754 (zero is a valid value though).
755 Note this value comes from the Fission stub CU/TU's DIE.
756 Also note that the value is zero in the non-DWO case so this value can
757 be used without needing to know whether DWO files are in use or not.
758 N.B. This does not apply to DW_AT_ranges appearing in
759 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
760 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
761 DW_AT_ranges_base *would* have to be applied, and we'd have to care
762 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
763 ULONGEST ranges_base = 0;
764
765 /* When reading debug info generated by older versions of rustc, we
766 have to rewrite some union types to be struct types with a
767 variant part. This rewriting must be done after the CU is fully
768 read in, because otherwise at the point of rewriting some struct
769 type might not have been fully processed. So, we keep a list of
770 all such types here and process them after expansion. */
771 std::vector<struct type *> rust_unions;
772
773 /* Mark used when releasing cached dies. */
774 unsigned int mark : 1;
775
776 /* This CU references .debug_loc. See the symtab->locations_valid field.
777 This test is imperfect as there may exist optimized debug code not using
778 any location list and still facing inlining issues if handled as
779 unoptimized code. For a future better test see GCC PR other/32998. */
780 unsigned int has_loclist : 1;
781
782 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
783 if all the producer_is_* fields are valid. This information is cached
784 because profiling CU expansion showed excessive time spent in
785 producer_is_gxx_lt_4_6. */
786 unsigned int checked_producer : 1;
787 unsigned int producer_is_gxx_lt_4_6 : 1;
788 unsigned int producer_is_gcc_lt_4_3 : 1;
789 unsigned int producer_is_icc_lt_14 : 1;
790
791 /* When set, the file that we're processing is known to have
792 debugging info for C++ namespaces. GCC 3.3.x did not produce
793 this information, but later versions do. */
794
795 unsigned int processing_has_namespace_info : 1;
796
797 struct partial_die_info *find_partial_die (sect_offset sect_off);
798 };
799
800 /* Persistent data held for a compilation unit, even when not
801 processing it. We put a pointer to this structure in the
802 read_symtab_private field of the psymtab. */
803
804 struct dwarf2_per_cu_data
805 {
806 /* The start offset and length of this compilation unit.
807 NOTE: Unlike comp_unit_head.length, this length includes
808 initial_length_size.
809 If the DIE refers to a DWO file, this is always of the original die,
810 not the DWO file. */
811 sect_offset sect_off;
812 unsigned int length;
813
814 /* DWARF standard version this data has been read from (such as 4 or 5). */
815 short dwarf_version;
816
817 /* Flag indicating this compilation unit will be read in before
818 any of the current compilation units are processed. */
819 unsigned int queued : 1;
820
821 /* This flag will be set when reading partial DIEs if we need to load
822 absolutely all DIEs for this compilation unit, instead of just the ones
823 we think are interesting. It gets set if we look for a DIE in the
824 hash table and don't find it. */
825 unsigned int load_all_dies : 1;
826
827 /* Non-zero if this CU is from .debug_types.
828 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
829 this is non-zero. */
830 unsigned int is_debug_types : 1;
831
832 /* Non-zero if this CU is from the .dwz file. */
833 unsigned int is_dwz : 1;
834
835 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
836 This flag is only valid if is_debug_types is true.
837 We can't read a CU directly from a DWO file: There are required
838 attributes in the stub. */
839 unsigned int reading_dwo_directly : 1;
840
841 /* Non-zero if the TU has been read.
842 This is used to assist the "Stay in DWO Optimization" for Fission:
843 When reading a DWO, it's faster to read TUs from the DWO instead of
844 fetching them from random other DWOs (due to comdat folding).
845 If the TU has already been read, the optimization is unnecessary
846 (and unwise - we don't want to change where gdb thinks the TU lives
847 "midflight").
848 This flag is only valid if is_debug_types is true. */
849 unsigned int tu_read : 1;
850
851 /* The section this CU/TU lives in.
852 If the DIE refers to a DWO file, this is always the original die,
853 not the DWO file. */
854 struct dwarf2_section_info *section;
855
856 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
857 of the CU cache it gets reset to NULL again. This is left as NULL for
858 dummy CUs (a CU header, but nothing else). */
859 struct dwarf2_cu *cu;
860
861 /* The corresponding dwarf2_per_objfile. */
862 struct dwarf2_per_objfile *dwarf2_per_objfile;
863
864 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
865 is active. Otherwise, the 'psymtab' field is active. */
866 union
867 {
868 /* The partial symbol table associated with this compilation unit,
869 or NULL for unread partial units. */
870 struct partial_symtab *psymtab;
871
872 /* Data needed by the "quick" functions. */
873 struct dwarf2_per_cu_quick_data *quick;
874 } v;
875
876 /* The CUs we import using DW_TAG_imported_unit. This is filled in
877 while reading psymtabs, used to compute the psymtab dependencies,
878 and then cleared. Then it is filled in again while reading full
879 symbols, and only deleted when the objfile is destroyed.
880
881 This is also used to work around a difference between the way gold
882 generates .gdb_index version <=7 and the way gdb does. Arguably this
883 is a gold bug. For symbols coming from TUs, gold records in the index
884 the CU that includes the TU instead of the TU itself. This breaks
885 dw2_lookup_symbol: It assumes that if the index says symbol X lives
886 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
887 will find X. Alas TUs live in their own symtab, so after expanding CU Y
888 we need to look in TU Z to find X. Fortunately, this is akin to
889 DW_TAG_imported_unit, so we just use the same mechanism: For
890 .gdb_index version <=7 this also records the TUs that the CU referred
891 to. Concurrently with this change gdb was modified to emit version 8
892 indices so we only pay a price for gold generated indices.
893 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
894 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
895 };
896
897 /* Entry in the signatured_types hash table. */
898
899 struct signatured_type
900 {
901 /* The "per_cu" object of this type.
902 This struct is used iff per_cu.is_debug_types.
903 N.B.: This is the first member so that it's easy to convert pointers
904 between them. */
905 struct dwarf2_per_cu_data per_cu;
906
907 /* The type's signature. */
908 ULONGEST signature;
909
910 /* Offset in the TU of the type's DIE, as read from the TU header.
911 If this TU is a DWO stub and the definition lives in a DWO file
912 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
913 cu_offset type_offset_in_tu;
914
915 /* Offset in the section of the type's DIE.
916 If the definition lives in a DWO file, this is the offset in the
917 .debug_types.dwo section.
918 The value is zero until the actual value is known.
919 Zero is otherwise not a valid section offset. */
920 sect_offset type_offset_in_section;
921
922 /* Type units are grouped by their DW_AT_stmt_list entry so that they
923 can share them. This points to the containing symtab. */
924 struct type_unit_group *type_unit_group;
925
926 /* The type.
927 The first time we encounter this type we fully read it in and install it
928 in the symbol tables. Subsequent times we only need the type. */
929 struct type *type;
930
931 /* Containing DWO unit.
932 This field is valid iff per_cu.reading_dwo_directly. */
933 struct dwo_unit *dwo_unit;
934 };
935
936 typedef struct signatured_type *sig_type_ptr;
937 DEF_VEC_P (sig_type_ptr);
938
939 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
940 This includes type_unit_group and quick_file_names. */
941
942 struct stmt_list_hash
943 {
944 /* The DWO unit this table is from or NULL if there is none. */
945 struct dwo_unit *dwo_unit;
946
947 /* Offset in .debug_line or .debug_line.dwo. */
948 sect_offset line_sect_off;
949 };
950
951 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
952 an object of this type. */
953
954 struct type_unit_group
955 {
956 /* dwarf2read.c's main "handle" on a TU symtab.
957 To simplify things we create an artificial CU that "includes" all the
958 type units using this stmt_list so that the rest of the code still has
959 a "per_cu" handle on the symtab.
960 This PER_CU is recognized by having no section. */
961 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
962 struct dwarf2_per_cu_data per_cu;
963
964 /* The TUs that share this DW_AT_stmt_list entry.
965 This is added to while parsing type units to build partial symtabs,
966 and is deleted afterwards and not used again. */
967 VEC (sig_type_ptr) *tus;
968
969 /* The compunit symtab.
970 Type units in a group needn't all be defined in the same source file,
971 so we create an essentially anonymous symtab as the compunit symtab. */
972 struct compunit_symtab *compunit_symtab;
973
974 /* The data used to construct the hash key. */
975 struct stmt_list_hash hash;
976
977 /* The number of symtabs from the line header.
978 The value here must match line_header.num_file_names. */
979 unsigned int num_symtabs;
980
981 /* The symbol tables for this TU (obtained from the files listed in
982 DW_AT_stmt_list).
983 WARNING: The order of entries here must match the order of entries
984 in the line header. After the first TU using this type_unit_group, the
985 line header for the subsequent TUs is recreated from this. This is done
986 because we need to use the same symtabs for each TU using the same
987 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
988 there's no guarantee the line header doesn't have duplicate entries. */
989 struct symtab **symtabs;
990 };
991
992 /* These sections are what may appear in a (real or virtual) DWO file. */
993
994 struct dwo_sections
995 {
996 struct dwarf2_section_info abbrev;
997 struct dwarf2_section_info line;
998 struct dwarf2_section_info loc;
999 struct dwarf2_section_info loclists;
1000 struct dwarf2_section_info macinfo;
1001 struct dwarf2_section_info macro;
1002 struct dwarf2_section_info str;
1003 struct dwarf2_section_info str_offsets;
1004 /* In the case of a virtual DWO file, these two are unused. */
1005 struct dwarf2_section_info info;
1006 VEC (dwarf2_section_info_def) *types;
1007 };
1008
1009 /* CUs/TUs in DWP/DWO files. */
1010
1011 struct dwo_unit
1012 {
1013 /* Backlink to the containing struct dwo_file. */
1014 struct dwo_file *dwo_file;
1015
1016 /* The "id" that distinguishes this CU/TU.
1017 .debug_info calls this "dwo_id", .debug_types calls this "signature".
1018 Since signatures came first, we stick with it for consistency. */
1019 ULONGEST signature;
1020
1021 /* The section this CU/TU lives in, in the DWO file. */
1022 struct dwarf2_section_info *section;
1023
1024 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
1025 sect_offset sect_off;
1026 unsigned int length;
1027
1028 /* For types, offset in the type's DIE of the type defined by this TU. */
1029 cu_offset type_offset_in_tu;
1030 };
1031
1032 /* include/dwarf2.h defines the DWP section codes.
1033 It defines a max value but it doesn't define a min value, which we
1034 use for error checking, so provide one. */
1035
1036 enum dwp_v2_section_ids
1037 {
1038 DW_SECT_MIN = 1
1039 };
1040
1041 /* Data for one DWO file.
1042
1043 This includes virtual DWO files (a virtual DWO file is a DWO file as it
1044 appears in a DWP file). DWP files don't really have DWO files per se -
1045 comdat folding of types "loses" the DWO file they came from, and from
1046 a high level view DWP files appear to contain a mass of random types.
1047 However, to maintain consistency with the non-DWP case we pretend DWP
1048 files contain virtual DWO files, and we assign each TU with one virtual
1049 DWO file (generally based on the line and abbrev section offsets -
1050 a heuristic that seems to work in practice). */
1051
1052 struct dwo_file
1053 {
1054 /* The DW_AT_GNU_dwo_name attribute.
1055 For virtual DWO files the name is constructed from the section offsets
1056 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
1057 from related CU+TUs. */
1058 const char *dwo_name;
1059
1060 /* The DW_AT_comp_dir attribute. */
1061 const char *comp_dir;
1062
1063 /* The bfd, when the file is open. Otherwise this is NULL.
1064 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
1065 bfd *dbfd;
1066
1067 /* The sections that make up this DWO file.
1068 Remember that for virtual DWO files in DWP V2, these are virtual
1069 sections (for lack of a better name). */
1070 struct dwo_sections sections;
1071
1072 /* The CUs in the file.
1073 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
1074 an extension to handle LLVM's Link Time Optimization output (where
1075 multiple source files may be compiled into a single object/dwo pair). */
1076 htab_t cus;
1077
1078 /* Table of TUs in the file.
1079 Each element is a struct dwo_unit. */
1080 htab_t tus;
1081 };
1082
1083 /* These sections are what may appear in a DWP file. */
1084
1085 struct dwp_sections
1086 {
1087 /* These are used by both DWP version 1 and 2. */
1088 struct dwarf2_section_info str;
1089 struct dwarf2_section_info cu_index;
1090 struct dwarf2_section_info tu_index;
1091
1092 /* These are only used by DWP version 2 files.
1093 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
1094 sections are referenced by section number, and are not recorded here.
1095 In DWP version 2 there is at most one copy of all these sections, each
1096 section being (effectively) comprised of the concatenation of all of the
1097 individual sections that exist in the version 1 format.
1098 To keep the code simple we treat each of these concatenated pieces as a
1099 section itself (a virtual section?). */
1100 struct dwarf2_section_info abbrev;
1101 struct dwarf2_section_info info;
1102 struct dwarf2_section_info line;
1103 struct dwarf2_section_info loc;
1104 struct dwarf2_section_info macinfo;
1105 struct dwarf2_section_info macro;
1106 struct dwarf2_section_info str_offsets;
1107 struct dwarf2_section_info types;
1108 };
1109
1110 /* These sections are what may appear in a virtual DWO file in DWP version 1.
1111 A virtual DWO file is a DWO file as it appears in a DWP file. */
1112
1113 struct virtual_v1_dwo_sections
1114 {
1115 struct dwarf2_section_info abbrev;
1116 struct dwarf2_section_info line;
1117 struct dwarf2_section_info loc;
1118 struct dwarf2_section_info macinfo;
1119 struct dwarf2_section_info macro;
1120 struct dwarf2_section_info str_offsets;
1121 /* Each DWP hash table entry records one CU or one TU.
1122 That is recorded here, and copied to dwo_unit.section. */
1123 struct dwarf2_section_info info_or_types;
1124 };
1125
1126 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1127 In version 2, the sections of the DWO files are concatenated together
1128 and stored in one section of that name. Thus each ELF section contains
1129 several "virtual" sections. */
1130
1131 struct virtual_v2_dwo_sections
1132 {
1133 bfd_size_type abbrev_offset;
1134 bfd_size_type abbrev_size;
1135
1136 bfd_size_type line_offset;
1137 bfd_size_type line_size;
1138
1139 bfd_size_type loc_offset;
1140 bfd_size_type loc_size;
1141
1142 bfd_size_type macinfo_offset;
1143 bfd_size_type macinfo_size;
1144
1145 bfd_size_type macro_offset;
1146 bfd_size_type macro_size;
1147
1148 bfd_size_type str_offsets_offset;
1149 bfd_size_type str_offsets_size;
1150
1151 /* Each DWP hash table entry records one CU or one TU.
1152 That is recorded here, and copied to dwo_unit.section. */
1153 bfd_size_type info_or_types_offset;
1154 bfd_size_type info_or_types_size;
1155 };
1156
1157 /* Contents of DWP hash tables. */
1158
1159 struct dwp_hash_table
1160 {
1161 uint32_t version, nr_columns;
1162 uint32_t nr_units, nr_slots;
1163 const gdb_byte *hash_table, *unit_table;
1164 union
1165 {
1166 struct
1167 {
1168 const gdb_byte *indices;
1169 } v1;
1170 struct
1171 {
1172 /* This is indexed by column number and gives the id of the section
1173 in that column. */
1174 #define MAX_NR_V2_DWO_SECTIONS \
1175 (1 /* .debug_info or .debug_types */ \
1176 + 1 /* .debug_abbrev */ \
1177 + 1 /* .debug_line */ \
1178 + 1 /* .debug_loc */ \
1179 + 1 /* .debug_str_offsets */ \
1180 + 1 /* .debug_macro or .debug_macinfo */)
1181 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1182 const gdb_byte *offsets;
1183 const gdb_byte *sizes;
1184 } v2;
1185 } section_pool;
1186 };
1187
1188 /* Data for one DWP file. */
1189
1190 struct dwp_file
1191 {
1192 /* Name of the file. */
1193 const char *name;
1194
1195 /* File format version. */
1196 int version;
1197
1198 /* The bfd. */
1199 bfd *dbfd;
1200
1201 /* Section info for this file. */
1202 struct dwp_sections sections;
1203
1204 /* Table of CUs in the file. */
1205 const struct dwp_hash_table *cus;
1206
1207 /* Table of TUs in the file. */
1208 const struct dwp_hash_table *tus;
1209
1210 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1211 htab_t loaded_cus;
1212 htab_t loaded_tus;
1213
1214 /* Table to map ELF section numbers to their sections.
1215 This is only needed for the DWP V1 file format. */
1216 unsigned int num_sections;
1217 asection **elf_sections;
1218 };
1219
1220 /* This represents a '.dwz' file. */
1221
1222 struct dwz_file
1223 {
1224 /* A dwz file can only contain a few sections. */
1225 struct dwarf2_section_info abbrev;
1226 struct dwarf2_section_info info;
1227 struct dwarf2_section_info str;
1228 struct dwarf2_section_info line;
1229 struct dwarf2_section_info macro;
1230 struct dwarf2_section_info gdb_index;
1231 struct dwarf2_section_info debug_names;
1232
1233 /* The dwz's BFD. */
1234 bfd *dwz_bfd;
1235 };
1236
1237 /* Struct used to pass misc. parameters to read_die_and_children, et
1238 al. which are used for both .debug_info and .debug_types dies.
1239 All parameters here are unchanging for the life of the call. This
1240 struct exists to abstract away the constant parameters of die reading. */
1241
1242 struct die_reader_specs
1243 {
1244 /* The bfd of die_section. */
1245 bfd* abfd;
1246
1247 /* The CU of the DIE we are parsing. */
1248 struct dwarf2_cu *cu;
1249
1250 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1251 struct dwo_file *dwo_file;
1252
1253 /* The section the die comes from.
1254 This is either .debug_info or .debug_types, or the .dwo variants. */
1255 struct dwarf2_section_info *die_section;
1256
1257 /* die_section->buffer. */
1258 const gdb_byte *buffer;
1259
1260 /* The end of the buffer. */
1261 const gdb_byte *buffer_end;
1262
1263 /* The value of the DW_AT_comp_dir attribute. */
1264 const char *comp_dir;
1265
1266 /* The abbreviation table to use when reading the DIEs. */
1267 struct abbrev_table *abbrev_table;
1268 };
1269
1270 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1271 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1272 const gdb_byte *info_ptr,
1273 struct die_info *comp_unit_die,
1274 int has_children,
1275 void *data);
1276
1277 /* A 1-based directory index. This is a strong typedef to prevent
1278 accidentally using a directory index as a 0-based index into an
1279 array/vector. */
1280 enum class dir_index : unsigned int {};
1281
1282 /* Likewise, a 1-based file name index. */
1283 enum class file_name_index : unsigned int {};
1284
1285 struct file_entry
1286 {
1287 file_entry () = default;
1288
1289 file_entry (const char *name_, dir_index d_index_,
1290 unsigned int mod_time_, unsigned int length_)
1291 : name (name_),
1292 d_index (d_index_),
1293 mod_time (mod_time_),
1294 length (length_)
1295 {}
1296
1297 /* Return the include directory at D_INDEX stored in LH. Returns
1298 NULL if D_INDEX is out of bounds. */
1299 const char *include_dir (const line_header *lh) const;
1300
1301 /* The file name. Note this is an observing pointer. The memory is
1302 owned by debug_line_buffer. */
1303 const char *name {};
1304
1305 /* The directory index (1-based). */
1306 dir_index d_index {};
1307
1308 unsigned int mod_time {};
1309
1310 unsigned int length {};
1311
1312 /* True if referenced by the Line Number Program. */
1313 bool included_p {};
1314
1315 /* The associated symbol table, if any. */
1316 struct symtab *symtab {};
1317 };
1318
1319 /* The line number information for a compilation unit (found in the
1320 .debug_line section) begins with a "statement program header",
1321 which contains the following information. */
1322 struct line_header
1323 {
1324 line_header ()
1325 : offset_in_dwz {}
1326 {}
1327
1328 /* Add an entry to the include directory table. */
1329 void add_include_dir (const char *include_dir);
1330
1331 /* Add an entry to the file name table. */
1332 void add_file_name (const char *name, dir_index d_index,
1333 unsigned int mod_time, unsigned int length);
1334
1335 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1336 is out of bounds. */
1337 const char *include_dir_at (dir_index index) const
1338 {
1339 /* Convert directory index number (1-based) to vector index
1340 (0-based). */
1341 size_t vec_index = to_underlying (index) - 1;
1342
1343 if (vec_index >= include_dirs.size ())
1344 return NULL;
1345 return include_dirs[vec_index];
1346 }
1347
1348 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1349 is out of bounds. */
1350 file_entry *file_name_at (file_name_index index)
1351 {
1352 /* Convert file name index number (1-based) to vector index
1353 (0-based). */
1354 size_t vec_index = to_underlying (index) - 1;
1355
1356 if (vec_index >= file_names.size ())
1357 return NULL;
1358 return &file_names[vec_index];
1359 }
1360
1361 /* Const version of the above. */
1362 const file_entry *file_name_at (unsigned int index) const
1363 {
1364 if (index >= file_names.size ())
1365 return NULL;
1366 return &file_names[index];
1367 }
1368
1369 /* Offset of line number information in .debug_line section. */
1370 sect_offset sect_off {};
1371
1372 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1373 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1374
1375 unsigned int total_length {};
1376 unsigned short version {};
1377 unsigned int header_length {};
1378 unsigned char minimum_instruction_length {};
1379 unsigned char maximum_ops_per_instruction {};
1380 unsigned char default_is_stmt {};
1381 int line_base {};
1382 unsigned char line_range {};
1383 unsigned char opcode_base {};
1384
1385 /* standard_opcode_lengths[i] is the number of operands for the
1386 standard opcode whose value is i. This means that
1387 standard_opcode_lengths[0] is unused, and the last meaningful
1388 element is standard_opcode_lengths[opcode_base - 1]. */
1389 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1390
1391 /* The include_directories table. Note these are observing
1392 pointers. The memory is owned by debug_line_buffer. */
1393 std::vector<const char *> include_dirs;
1394
1395 /* The file_names table. */
1396 std::vector<file_entry> file_names;
1397
1398 /* The start and end of the statement program following this
1399 header. These point into dwarf2_per_objfile->line_buffer. */
1400 const gdb_byte *statement_program_start {}, *statement_program_end {};
1401 };
1402
1403 typedef std::unique_ptr<line_header> line_header_up;
1404
1405 const char *
1406 file_entry::include_dir (const line_header *lh) const
1407 {
1408 return lh->include_dir_at (d_index);
1409 }
1410
1411 /* When we construct a partial symbol table entry we only
1412 need this much information. */
1413 struct partial_die_info : public allocate_on_obstack
1414 {
1415 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1416
1417 /* Disable assign but still keep copy ctor, which is needed
1418 load_partial_dies. */
1419 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1420
1421 /* Adjust the partial die before generating a symbol for it. This
1422 function may set the is_external flag or change the DIE's
1423 name. */
1424 void fixup (struct dwarf2_cu *cu);
1425
1426 /* Read a minimal amount of information into the minimal die
1427 structure. */
1428 const gdb_byte *read (const struct die_reader_specs *reader,
1429 const struct abbrev_info &abbrev,
1430 const gdb_byte *info_ptr);
1431
1432 /* Offset of this DIE. */
1433 const sect_offset sect_off;
1434
1435 /* DWARF-2 tag for this DIE. */
1436 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1437
1438 /* Assorted flags describing the data found in this DIE. */
1439 const unsigned int has_children : 1;
1440
1441 unsigned int is_external : 1;
1442 unsigned int is_declaration : 1;
1443 unsigned int has_type : 1;
1444 unsigned int has_specification : 1;
1445 unsigned int has_pc_info : 1;
1446 unsigned int may_be_inlined : 1;
1447
1448 /* This DIE has been marked DW_AT_main_subprogram. */
1449 unsigned int main_subprogram : 1;
1450
1451 /* Flag set if the SCOPE field of this structure has been
1452 computed. */
1453 unsigned int scope_set : 1;
1454
1455 /* Flag set if the DIE has a byte_size attribute. */
1456 unsigned int has_byte_size : 1;
1457
1458 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1459 unsigned int has_const_value : 1;
1460
1461 /* Flag set if any of the DIE's children are template arguments. */
1462 unsigned int has_template_arguments : 1;
1463
1464 /* Flag set if fixup has been called on this die. */
1465 unsigned int fixup_called : 1;
1466
1467 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1468 unsigned int is_dwz : 1;
1469
1470 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1471 unsigned int spec_is_dwz : 1;
1472
1473 /* The name of this DIE. Normally the value of DW_AT_name, but
1474 sometimes a default name for unnamed DIEs. */
1475 const char *name = nullptr;
1476
1477 /* The linkage name, if present. */
1478 const char *linkage_name = nullptr;
1479
1480 /* The scope to prepend to our children. This is generally
1481 allocated on the comp_unit_obstack, so will disappear
1482 when this compilation unit leaves the cache. */
1483 const char *scope = nullptr;
1484
1485 /* Some data associated with the partial DIE. The tag determines
1486 which field is live. */
1487 union
1488 {
1489 /* The location description associated with this DIE, if any. */
1490 struct dwarf_block *locdesc;
1491 /* The offset of an import, for DW_TAG_imported_unit. */
1492 sect_offset sect_off;
1493 } d {};
1494
1495 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1496 CORE_ADDR lowpc = 0;
1497 CORE_ADDR highpc = 0;
1498
1499 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1500 DW_AT_sibling, if any. */
1501 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1502 could return DW_AT_sibling values to its caller load_partial_dies. */
1503 const gdb_byte *sibling = nullptr;
1504
1505 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1506 DW_AT_specification (or DW_AT_abstract_origin or
1507 DW_AT_extension). */
1508 sect_offset spec_offset {};
1509
1510 /* Pointers to this DIE's parent, first child, and next sibling,
1511 if any. */
1512 struct partial_die_info *die_parent = nullptr;
1513 struct partial_die_info *die_child = nullptr;
1514 struct partial_die_info *die_sibling = nullptr;
1515
1516 friend struct partial_die_info *
1517 dwarf2_cu::find_partial_die (sect_offset sect_off);
1518
1519 private:
1520 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1521 partial_die_info (sect_offset sect_off)
1522 : partial_die_info (sect_off, DW_TAG_padding, 0)
1523 {
1524 }
1525
1526 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1527 int has_children_)
1528 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1529 {
1530 is_external = 0;
1531 is_declaration = 0;
1532 has_type = 0;
1533 has_specification = 0;
1534 has_pc_info = 0;
1535 may_be_inlined = 0;
1536 main_subprogram = 0;
1537 scope_set = 0;
1538 has_byte_size = 0;
1539 has_const_value = 0;
1540 has_template_arguments = 0;
1541 fixup_called = 0;
1542 is_dwz = 0;
1543 spec_is_dwz = 0;
1544 }
1545 };
1546
1547 /* This data structure holds the information of an abbrev. */
1548 struct abbrev_info
1549 {
1550 unsigned int number; /* number identifying abbrev */
1551 enum dwarf_tag tag; /* dwarf tag */
1552 unsigned short has_children; /* boolean */
1553 unsigned short num_attrs; /* number of attributes */
1554 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1555 struct abbrev_info *next; /* next in chain */
1556 };
1557
1558 struct attr_abbrev
1559 {
1560 ENUM_BITFIELD(dwarf_attribute) name : 16;
1561 ENUM_BITFIELD(dwarf_form) form : 16;
1562
1563 /* It is valid only if FORM is DW_FORM_implicit_const. */
1564 LONGEST implicit_const;
1565 };
1566
1567 /* Size of abbrev_table.abbrev_hash_table. */
1568 #define ABBREV_HASH_SIZE 121
1569
1570 /* Top level data structure to contain an abbreviation table. */
1571
1572 struct abbrev_table
1573 {
1574 explicit abbrev_table (sect_offset off)
1575 : sect_off (off)
1576 {
1577 m_abbrevs =
1578 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1579 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1580 }
1581
1582 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1583
1584 /* Allocate space for a struct abbrev_info object in
1585 ABBREV_TABLE. */
1586 struct abbrev_info *alloc_abbrev ();
1587
1588 /* Add an abbreviation to the table. */
1589 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1590
1591 /* Look up an abbrev in the table.
1592 Returns NULL if the abbrev is not found. */
1593
1594 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1595
1596
1597 /* Where the abbrev table came from.
1598 This is used as a sanity check when the table is used. */
1599 const sect_offset sect_off;
1600
1601 /* Storage for the abbrev table. */
1602 auto_obstack abbrev_obstack;
1603
1604 private:
1605
1606 /* Hash table of abbrevs.
1607 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1608 It could be statically allocated, but the previous code didn't so we
1609 don't either. */
1610 struct abbrev_info **m_abbrevs;
1611 };
1612
1613 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1614
1615 /* Attributes have a name and a value. */
1616 struct attribute
1617 {
1618 ENUM_BITFIELD(dwarf_attribute) name : 16;
1619 ENUM_BITFIELD(dwarf_form) form : 15;
1620
1621 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1622 field should be in u.str (existing only for DW_STRING) but it is kept
1623 here for better struct attribute alignment. */
1624 unsigned int string_is_canonical : 1;
1625
1626 union
1627 {
1628 const char *str;
1629 struct dwarf_block *blk;
1630 ULONGEST unsnd;
1631 LONGEST snd;
1632 CORE_ADDR addr;
1633 ULONGEST signature;
1634 }
1635 u;
1636 };
1637
1638 /* This data structure holds a complete die structure. */
1639 struct die_info
1640 {
1641 /* DWARF-2 tag for this DIE. */
1642 ENUM_BITFIELD(dwarf_tag) tag : 16;
1643
1644 /* Number of attributes */
1645 unsigned char num_attrs;
1646
1647 /* True if we're presently building the full type name for the
1648 type derived from this DIE. */
1649 unsigned char building_fullname : 1;
1650
1651 /* True if this die is in process. PR 16581. */
1652 unsigned char in_process : 1;
1653
1654 /* Abbrev number */
1655 unsigned int abbrev;
1656
1657 /* Offset in .debug_info or .debug_types section. */
1658 sect_offset sect_off;
1659
1660 /* The dies in a compilation unit form an n-ary tree. PARENT
1661 points to this die's parent; CHILD points to the first child of
1662 this node; and all the children of a given node are chained
1663 together via their SIBLING fields. */
1664 struct die_info *child; /* Its first child, if any. */
1665 struct die_info *sibling; /* Its next sibling, if any. */
1666 struct die_info *parent; /* Its parent, if any. */
1667
1668 /* An array of attributes, with NUM_ATTRS elements. There may be
1669 zero, but it's not common and zero-sized arrays are not
1670 sufficiently portable C. */
1671 struct attribute attrs[1];
1672 };
1673
1674 /* Get at parts of an attribute structure. */
1675
1676 #define DW_STRING(attr) ((attr)->u.str)
1677 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1678 #define DW_UNSND(attr) ((attr)->u.unsnd)
1679 #define DW_BLOCK(attr) ((attr)->u.blk)
1680 #define DW_SND(attr) ((attr)->u.snd)
1681 #define DW_ADDR(attr) ((attr)->u.addr)
1682 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1683
1684 /* Blocks are a bunch of untyped bytes. */
1685 struct dwarf_block
1686 {
1687 size_t size;
1688
1689 /* Valid only if SIZE is not zero. */
1690 const gdb_byte *data;
1691 };
1692
1693 #ifndef ATTR_ALLOC_CHUNK
1694 #define ATTR_ALLOC_CHUNK 4
1695 #endif
1696
1697 /* Allocate fields for structs, unions and enums in this size. */
1698 #ifndef DW_FIELD_ALLOC_CHUNK
1699 #define DW_FIELD_ALLOC_CHUNK 4
1700 #endif
1701
1702 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1703 but this would require a corresponding change in unpack_field_as_long
1704 and friends. */
1705 static int bits_per_byte = 8;
1706
1707 /* When reading a variant or variant part, we track a bit more
1708 information about the field, and store it in an object of this
1709 type. */
1710
1711 struct variant_field
1712 {
1713 /* If we see a DW_TAG_variant, then this will be the discriminant
1714 value. */
1715 ULONGEST discriminant_value;
1716 /* If we see a DW_TAG_variant, then this will be set if this is the
1717 default branch. */
1718 bool default_branch;
1719 /* While reading a DW_TAG_variant_part, this will be set if this
1720 field is the discriminant. */
1721 bool is_discriminant;
1722 };
1723
1724 struct nextfield
1725 {
1726 int accessibility = 0;
1727 int virtuality = 0;
1728 /* Extra information to describe a variant or variant part. */
1729 struct variant_field variant {};
1730 struct field field {};
1731 };
1732
1733 struct fnfieldlist
1734 {
1735 const char *name = nullptr;
1736 std::vector<struct fn_field> fnfields;
1737 };
1738
1739 /* The routines that read and process dies for a C struct or C++ class
1740 pass lists of data member fields and lists of member function fields
1741 in an instance of a field_info structure, as defined below. */
1742 struct field_info
1743 {
1744 /* List of data member and baseclasses fields. */
1745 std::vector<struct nextfield> fields;
1746 std::vector<struct nextfield> baseclasses;
1747
1748 /* Number of fields (including baseclasses). */
1749 int nfields = 0;
1750
1751 /* Set if the accesibility of one of the fields is not public. */
1752 int non_public_fields = 0;
1753
1754 /* Member function fieldlist array, contains name of possibly overloaded
1755 member function, number of overloaded member functions and a pointer
1756 to the head of the member function field chain. */
1757 std::vector<struct fnfieldlist> fnfieldlists;
1758
1759 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1760 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1761 std::vector<struct decl_field> typedef_field_list;
1762
1763 /* Nested types defined by this class and the number of elements in this
1764 list. */
1765 std::vector<struct decl_field> nested_types_list;
1766 };
1767
1768 /* One item on the queue of compilation units to read in full symbols
1769 for. */
1770 struct dwarf2_queue_item
1771 {
1772 struct dwarf2_per_cu_data *per_cu;
1773 enum language pretend_language;
1774 struct dwarf2_queue_item *next;
1775 };
1776
1777 /* The current queue. */
1778 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1779
1780 /* Loaded secondary compilation units are kept in memory until they
1781 have not been referenced for the processing of this many
1782 compilation units. Set this to zero to disable caching. Cache
1783 sizes of up to at least twenty will improve startup time for
1784 typical inter-CU-reference binaries, at an obvious memory cost. */
1785 static int dwarf_max_cache_age = 5;
1786 static void
1787 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1788 struct cmd_list_element *c, const char *value)
1789 {
1790 fprintf_filtered (file, _("The upper bound on the age of cached "
1791 "DWARF compilation units is %s.\n"),
1792 value);
1793 }
1794 \f
1795 /* local function prototypes */
1796
1797 static const char *get_section_name (const struct dwarf2_section_info *);
1798
1799 static const char *get_section_file_name (const struct dwarf2_section_info *);
1800
1801 static void dwarf2_find_base_address (struct die_info *die,
1802 struct dwarf2_cu *cu);
1803
1804 static struct partial_symtab *create_partial_symtab
1805 (struct dwarf2_per_cu_data *per_cu, const char *name);
1806
1807 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1808 const gdb_byte *info_ptr,
1809 struct die_info *type_unit_die,
1810 int has_children, void *data);
1811
1812 static void dwarf2_build_psymtabs_hard
1813 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1814
1815 static void scan_partial_symbols (struct partial_die_info *,
1816 CORE_ADDR *, CORE_ADDR *,
1817 int, struct dwarf2_cu *);
1818
1819 static void add_partial_symbol (struct partial_die_info *,
1820 struct dwarf2_cu *);
1821
1822 static void add_partial_namespace (struct partial_die_info *pdi,
1823 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1824 int set_addrmap, struct dwarf2_cu *cu);
1825
1826 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1827 CORE_ADDR *highpc, int set_addrmap,
1828 struct dwarf2_cu *cu);
1829
1830 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1831 struct dwarf2_cu *cu);
1832
1833 static void add_partial_subprogram (struct partial_die_info *pdi,
1834 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1835 int need_pc, struct dwarf2_cu *cu);
1836
1837 static void dwarf2_read_symtab (struct partial_symtab *,
1838 struct objfile *);
1839
1840 static void psymtab_to_symtab_1 (struct partial_symtab *);
1841
1842 static abbrev_table_up abbrev_table_read_table
1843 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1844 sect_offset);
1845
1846 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1847
1848 static struct partial_die_info *load_partial_dies
1849 (const struct die_reader_specs *, const gdb_byte *, int);
1850
1851 static struct partial_die_info *find_partial_die (sect_offset, int,
1852 struct dwarf2_cu *);
1853
1854 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1855 struct attribute *, struct attr_abbrev *,
1856 const gdb_byte *);
1857
1858 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1859
1860 static int read_1_signed_byte (bfd *, const gdb_byte *);
1861
1862 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1863
1864 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1865
1866 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1867
1868 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1869 unsigned int *);
1870
1871 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1872
1873 static LONGEST read_checked_initial_length_and_offset
1874 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1875 unsigned int *, unsigned int *);
1876
1877 static LONGEST read_offset (bfd *, const gdb_byte *,
1878 const struct comp_unit_head *,
1879 unsigned int *);
1880
1881 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1882
1883 static sect_offset read_abbrev_offset
1884 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1885 struct dwarf2_section_info *, sect_offset);
1886
1887 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1888
1889 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1890
1891 static const char *read_indirect_string
1892 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1893 const struct comp_unit_head *, unsigned int *);
1894
1895 static const char *read_indirect_line_string
1896 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1897 const struct comp_unit_head *, unsigned int *);
1898
1899 static const char *read_indirect_string_at_offset
1900 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1901 LONGEST str_offset);
1902
1903 static const char *read_indirect_string_from_dwz
1904 (struct objfile *objfile, struct dwz_file *, LONGEST);
1905
1906 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1907
1908 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1909 const gdb_byte *,
1910 unsigned int *);
1911
1912 static const char *read_str_index (const struct die_reader_specs *reader,
1913 ULONGEST str_index);
1914
1915 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1916
1917 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1918 struct dwarf2_cu *);
1919
1920 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1921 unsigned int);
1922
1923 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1924 struct dwarf2_cu *cu);
1925
1926 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1927 struct dwarf2_cu *cu);
1928
1929 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1930
1931 static struct die_info *die_specification (struct die_info *die,
1932 struct dwarf2_cu **);
1933
1934 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1935 struct dwarf2_cu *cu);
1936
1937 static void dwarf_decode_lines (struct line_header *, const char *,
1938 struct dwarf2_cu *, struct partial_symtab *,
1939 CORE_ADDR, int decode_mapping);
1940
1941 static void dwarf2_start_subfile (const char *, const char *);
1942
1943 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1944 const char *, const char *,
1945 CORE_ADDR);
1946
1947 static struct symbol *new_symbol (struct die_info *, struct type *,
1948 struct dwarf2_cu *, struct symbol * = NULL);
1949
1950 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1951 struct dwarf2_cu *);
1952
1953 static void dwarf2_const_value_attr (const struct attribute *attr,
1954 struct type *type,
1955 const char *name,
1956 struct obstack *obstack,
1957 struct dwarf2_cu *cu, LONGEST *value,
1958 const gdb_byte **bytes,
1959 struct dwarf2_locexpr_baton **baton);
1960
1961 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1962
1963 static int need_gnat_info (struct dwarf2_cu *);
1964
1965 static struct type *die_descriptive_type (struct die_info *,
1966 struct dwarf2_cu *);
1967
1968 static void set_descriptive_type (struct type *, struct die_info *,
1969 struct dwarf2_cu *);
1970
1971 static struct type *die_containing_type (struct die_info *,
1972 struct dwarf2_cu *);
1973
1974 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1975 struct dwarf2_cu *);
1976
1977 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1978
1979 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1980
1981 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1982
1983 static char *typename_concat (struct obstack *obs, const char *prefix,
1984 const char *suffix, int physname,
1985 struct dwarf2_cu *cu);
1986
1987 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1988
1989 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1990
1991 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1992
1993 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1994
1995 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1996
1997 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1998
1999 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
2000 struct dwarf2_cu *, struct partial_symtab *);
2001
2002 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
2003 values. Keep the items ordered with increasing constraints compliance. */
2004 enum pc_bounds_kind
2005 {
2006 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
2007 PC_BOUNDS_NOT_PRESENT,
2008
2009 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
2010 were present but they do not form a valid range of PC addresses. */
2011 PC_BOUNDS_INVALID,
2012
2013 /* Discontiguous range was found - that is DW_AT_ranges was found. */
2014 PC_BOUNDS_RANGES,
2015
2016 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
2017 PC_BOUNDS_HIGH_LOW,
2018 };
2019
2020 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
2021 CORE_ADDR *, CORE_ADDR *,
2022 struct dwarf2_cu *,
2023 struct partial_symtab *);
2024
2025 static void get_scope_pc_bounds (struct die_info *,
2026 CORE_ADDR *, CORE_ADDR *,
2027 struct dwarf2_cu *);
2028
2029 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
2030 CORE_ADDR, struct dwarf2_cu *);
2031
2032 static void dwarf2_add_field (struct field_info *, struct die_info *,
2033 struct dwarf2_cu *);
2034
2035 static void dwarf2_attach_fields_to_type (struct field_info *,
2036 struct type *, struct dwarf2_cu *);
2037
2038 static void dwarf2_add_member_fn (struct field_info *,
2039 struct die_info *, struct type *,
2040 struct dwarf2_cu *);
2041
2042 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
2043 struct type *,
2044 struct dwarf2_cu *);
2045
2046 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
2047
2048 static void read_common_block (struct die_info *, struct dwarf2_cu *);
2049
2050 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
2051
2052 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
2053
2054 static struct using_direct **using_directives (enum language);
2055
2056 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
2057
2058 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
2059
2060 static struct type *read_module_type (struct die_info *die,
2061 struct dwarf2_cu *cu);
2062
2063 static const char *namespace_name (struct die_info *die,
2064 int *is_anonymous, struct dwarf2_cu *);
2065
2066 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
2067
2068 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
2069
2070 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
2071 struct dwarf2_cu *);
2072
2073 static struct die_info *read_die_and_siblings_1
2074 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
2075 struct die_info *);
2076
2077 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
2078 const gdb_byte *info_ptr,
2079 const gdb_byte **new_info_ptr,
2080 struct die_info *parent);
2081
2082 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
2083 struct die_info **, const gdb_byte *,
2084 int *, int);
2085
2086 static const gdb_byte *read_full_die (const struct die_reader_specs *,
2087 struct die_info **, const gdb_byte *,
2088 int *);
2089
2090 static void process_die (struct die_info *, struct dwarf2_cu *);
2091
2092 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
2093 struct obstack *);
2094
2095 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
2096
2097 static const char *dwarf2_full_name (const char *name,
2098 struct die_info *die,
2099 struct dwarf2_cu *cu);
2100
2101 static const char *dwarf2_physname (const char *name, struct die_info *die,
2102 struct dwarf2_cu *cu);
2103
2104 static struct die_info *dwarf2_extension (struct die_info *die,
2105 struct dwarf2_cu **);
2106
2107 static const char *dwarf_tag_name (unsigned int);
2108
2109 static const char *dwarf_attr_name (unsigned int);
2110
2111 static const char *dwarf_form_name (unsigned int);
2112
2113 static const char *dwarf_bool_name (unsigned int);
2114
2115 static const char *dwarf_type_encoding_name (unsigned int);
2116
2117 static struct die_info *sibling_die (struct die_info *);
2118
2119 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
2120
2121 static void dump_die_for_error (struct die_info *);
2122
2123 static void dump_die_1 (struct ui_file *, int level, int max_level,
2124 struct die_info *);
2125
2126 /*static*/ void dump_die (struct die_info *, int max_level);
2127
2128 static void store_in_ref_table (struct die_info *,
2129 struct dwarf2_cu *);
2130
2131 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
2132
2133 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
2134
2135 static struct die_info *follow_die_ref_or_sig (struct die_info *,
2136 const struct attribute *,
2137 struct dwarf2_cu **);
2138
2139 static struct die_info *follow_die_ref (struct die_info *,
2140 const struct attribute *,
2141 struct dwarf2_cu **);
2142
2143 static struct die_info *follow_die_sig (struct die_info *,
2144 const struct attribute *,
2145 struct dwarf2_cu **);
2146
2147 static struct type *get_signatured_type (struct die_info *, ULONGEST,
2148 struct dwarf2_cu *);
2149
2150 static struct type *get_DW_AT_signature_type (struct die_info *,
2151 const struct attribute *,
2152 struct dwarf2_cu *);
2153
2154 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
2155
2156 static void read_signatured_type (struct signatured_type *);
2157
2158 static int attr_to_dynamic_prop (const struct attribute *attr,
2159 struct die_info *die, struct dwarf2_cu *cu,
2160 struct dynamic_prop *prop);
2161
2162 /* memory allocation interface */
2163
2164 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
2165
2166 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
2167
2168 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2169
2170 static int attr_form_is_block (const struct attribute *);
2171
2172 static int attr_form_is_section_offset (const struct attribute *);
2173
2174 static int attr_form_is_constant (const struct attribute *);
2175
2176 static int attr_form_is_ref (const struct attribute *);
2177
2178 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2179 struct dwarf2_loclist_baton *baton,
2180 const struct attribute *attr);
2181
2182 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
2183 struct symbol *sym,
2184 struct dwarf2_cu *cu,
2185 int is_block);
2186
2187 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2188 const gdb_byte *info_ptr,
2189 struct abbrev_info *abbrev);
2190
2191 static hashval_t partial_die_hash (const void *item);
2192
2193 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2194
2195 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
2196 (sect_offset sect_off, unsigned int offset_in_dwz,
2197 struct dwarf2_per_objfile *dwarf2_per_objfile);
2198
2199 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
2200 struct die_info *comp_unit_die,
2201 enum language pretend_language);
2202
2203 static void free_cached_comp_units (void *);
2204
2205 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2206
2207 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
2208
2209 static struct type *set_die_type (struct die_info *, struct type *,
2210 struct dwarf2_cu *);
2211
2212 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2213
2214 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2215
2216 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2217 enum language);
2218
2219 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2220 enum language);
2221
2222 static void process_full_type_unit (struct dwarf2_per_cu_data *,
2223 enum language);
2224
2225 static void dwarf2_add_dependence (struct dwarf2_cu *,
2226 struct dwarf2_per_cu_data *);
2227
2228 static void dwarf2_mark (struct dwarf2_cu *);
2229
2230 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2231
2232 static struct type *get_die_type_at_offset (sect_offset,
2233 struct dwarf2_per_cu_data *);
2234
2235 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
2236
2237 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2238 enum language pretend_language);
2239
2240 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
2241
2242 /* Class, the destructor of which frees all allocated queue entries. This
2243 will only have work to do if an error was thrown while processing the
2244 dwarf. If no error was thrown then the queue entries should have all
2245 been processed, and freed, as we went along. */
2246
2247 class dwarf2_queue_guard
2248 {
2249 public:
2250 dwarf2_queue_guard () = default;
2251
2252 /* Free any entries remaining on the queue. There should only be
2253 entries left if we hit an error while processing the dwarf. */
2254 ~dwarf2_queue_guard ()
2255 {
2256 struct dwarf2_queue_item *item, *last;
2257
2258 item = dwarf2_queue;
2259 while (item)
2260 {
2261 /* Anything still marked queued is likely to be in an
2262 inconsistent state, so discard it. */
2263 if (item->per_cu->queued)
2264 {
2265 if (item->per_cu->cu != NULL)
2266 free_one_cached_comp_unit (item->per_cu);
2267 item->per_cu->queued = 0;
2268 }
2269
2270 last = item;
2271 item = item->next;
2272 xfree (last);
2273 }
2274
2275 dwarf2_queue = dwarf2_queue_tail = NULL;
2276 }
2277 };
2278
2279 /* The return type of find_file_and_directory. Note, the enclosed
2280 string pointers are only valid while this object is valid. */
2281
2282 struct file_and_directory
2283 {
2284 /* The filename. This is never NULL. */
2285 const char *name;
2286
2287 /* The compilation directory. NULL if not known. If we needed to
2288 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2289 points directly to the DW_AT_comp_dir string attribute owned by
2290 the obstack that owns the DIE. */
2291 const char *comp_dir;
2292
2293 /* If we needed to build a new string for comp_dir, this is what
2294 owns the storage. */
2295 std::string comp_dir_storage;
2296 };
2297
2298 static file_and_directory find_file_and_directory (struct die_info *die,
2299 struct dwarf2_cu *cu);
2300
2301 static char *file_full_name (int file, struct line_header *lh,
2302 const char *comp_dir);
2303
2304 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2305 enum class rcuh_kind { COMPILE, TYPE };
2306
2307 static const gdb_byte *read_and_check_comp_unit_head
2308 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2309 struct comp_unit_head *header,
2310 struct dwarf2_section_info *section,
2311 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2312 rcuh_kind section_kind);
2313
2314 static void init_cutu_and_read_dies
2315 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2316 int use_existing_cu, int keep,
2317 die_reader_func_ftype *die_reader_func, void *data);
2318
2319 static void init_cutu_and_read_dies_simple
2320 (struct dwarf2_per_cu_data *this_cu,
2321 die_reader_func_ftype *die_reader_func, void *data);
2322
2323 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2324
2325 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2326
2327 static struct dwo_unit *lookup_dwo_unit_in_dwp
2328 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2329 struct dwp_file *dwp_file, const char *comp_dir,
2330 ULONGEST signature, int is_debug_types);
2331
2332 static struct dwp_file *get_dwp_file
2333 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2334
2335 static struct dwo_unit *lookup_dwo_comp_unit
2336 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2337
2338 static struct dwo_unit *lookup_dwo_type_unit
2339 (struct signatured_type *, const char *, const char *);
2340
2341 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2342
2343 static void free_dwo_file_cleanup (void *);
2344
2345 struct free_dwo_file_cleanup_data
2346 {
2347 struct dwo_file *dwo_file;
2348 struct dwarf2_per_objfile *dwarf2_per_objfile;
2349 };
2350
2351 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2352
2353 static void check_producer (struct dwarf2_cu *cu);
2354
2355 static void free_line_header_voidp (void *arg);
2356 \f
2357 /* Various complaints about symbol reading that don't abort the process. */
2358
2359 static void
2360 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2361 {
2362 complaint (&symfile_complaints,
2363 _("statement list doesn't fit in .debug_line section"));
2364 }
2365
2366 static void
2367 dwarf2_debug_line_missing_file_complaint (void)
2368 {
2369 complaint (&symfile_complaints,
2370 _(".debug_line section has line data without a file"));
2371 }
2372
2373 static void
2374 dwarf2_debug_line_missing_end_sequence_complaint (void)
2375 {
2376 complaint (&symfile_complaints,
2377 _(".debug_line section has line "
2378 "program sequence without an end"));
2379 }
2380
2381 static void
2382 dwarf2_complex_location_expr_complaint (void)
2383 {
2384 complaint (&symfile_complaints, _("location expression too complex"));
2385 }
2386
2387 static void
2388 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2389 int arg3)
2390 {
2391 complaint (&symfile_complaints,
2392 _("const value length mismatch for '%s', got %d, expected %d"),
2393 arg1, arg2, arg3);
2394 }
2395
2396 static void
2397 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2398 {
2399 complaint (&symfile_complaints,
2400 _("debug info runs off end of %s section"
2401 " [in module %s]"),
2402 get_section_name (section),
2403 get_section_file_name (section));
2404 }
2405
2406 static void
2407 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2408 {
2409 complaint (&symfile_complaints,
2410 _("macro debug info contains a "
2411 "malformed macro definition:\n`%s'"),
2412 arg1);
2413 }
2414
2415 static void
2416 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2417 {
2418 complaint (&symfile_complaints,
2419 _("invalid attribute class or form for '%s' in '%s'"),
2420 arg1, arg2);
2421 }
2422
2423 /* Hash function for line_header_hash. */
2424
2425 static hashval_t
2426 line_header_hash (const struct line_header *ofs)
2427 {
2428 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2429 }
2430
2431 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2432
2433 static hashval_t
2434 line_header_hash_voidp (const void *item)
2435 {
2436 const struct line_header *ofs = (const struct line_header *) item;
2437
2438 return line_header_hash (ofs);
2439 }
2440
2441 /* Equality function for line_header_hash. */
2442
2443 static int
2444 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2445 {
2446 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2447 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2448
2449 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2450 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2451 }
2452
2453 \f
2454
2455 /* Read the given attribute value as an address, taking the attribute's
2456 form into account. */
2457
2458 static CORE_ADDR
2459 attr_value_as_address (struct attribute *attr)
2460 {
2461 CORE_ADDR addr;
2462
2463 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2464 {
2465 /* Aside from a few clearly defined exceptions, attributes that
2466 contain an address must always be in DW_FORM_addr form.
2467 Unfortunately, some compilers happen to be violating this
2468 requirement by encoding addresses using other forms, such
2469 as DW_FORM_data4 for example. For those broken compilers,
2470 we try to do our best, without any guarantee of success,
2471 to interpret the address correctly. It would also be nice
2472 to generate a complaint, but that would require us to maintain
2473 a list of legitimate cases where a non-address form is allowed,
2474 as well as update callers to pass in at least the CU's DWARF
2475 version. This is more overhead than what we're willing to
2476 expand for a pretty rare case. */
2477 addr = DW_UNSND (attr);
2478 }
2479 else
2480 addr = DW_ADDR (attr);
2481
2482 return addr;
2483 }
2484
2485 /* The suffix for an index file. */
2486 #define INDEX4_SUFFIX ".gdb-index"
2487 #define INDEX5_SUFFIX ".debug_names"
2488 #define DEBUG_STR_SUFFIX ".debug_str"
2489
2490 /* See declaration. */
2491
2492 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2493 const dwarf2_debug_sections *names)
2494 : objfile (objfile_)
2495 {
2496 if (names == NULL)
2497 names = &dwarf2_elf_names;
2498
2499 bfd *obfd = objfile->obfd;
2500
2501 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2502 locate_sections (obfd, sec, *names);
2503 }
2504
2505 static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2506
2507 dwarf2_per_objfile::~dwarf2_per_objfile ()
2508 {
2509 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2510 free_cached_comp_units ();
2511
2512 if (quick_file_names_table)
2513 htab_delete (quick_file_names_table);
2514
2515 if (line_header_hash)
2516 htab_delete (line_header_hash);
2517
2518 for (int ix = 0; ix < n_comp_units; ++ix)
2519 VEC_free (dwarf2_per_cu_ptr, all_comp_units[ix]->imported_symtabs);
2520
2521 for (int ix = 0; ix < n_type_units; ++ix)
2522 VEC_free (dwarf2_per_cu_ptr,
2523 all_type_units[ix]->per_cu.imported_symtabs);
2524 xfree (all_type_units);
2525
2526 VEC_free (dwarf2_section_info_def, types);
2527
2528 if (dwo_files != NULL)
2529 free_dwo_files (dwo_files, objfile);
2530 if (dwp_file != NULL)
2531 gdb_bfd_unref (dwp_file->dbfd);
2532
2533 if (dwz_file != NULL && dwz_file->dwz_bfd)
2534 gdb_bfd_unref (dwz_file->dwz_bfd);
2535
2536 if (index_table != NULL)
2537 index_table->~mapped_index ();
2538
2539 /* Everything else should be on the objfile obstack. */
2540 }
2541
2542 /* See declaration. */
2543
2544 void
2545 dwarf2_per_objfile::free_cached_comp_units ()
2546 {
2547 dwarf2_per_cu_data *per_cu = read_in_chain;
2548 dwarf2_per_cu_data **last_chain = &read_in_chain;
2549 while (per_cu != NULL)
2550 {
2551 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2552
2553 delete per_cu->cu;
2554 *last_chain = next_cu;
2555 per_cu = next_cu;
2556 }
2557 }
2558
2559 /* Try to locate the sections we need for DWARF 2 debugging
2560 information and return true if we have enough to do something.
2561 NAMES points to the dwarf2 section names, or is NULL if the standard
2562 ELF names are used. */
2563
2564 int
2565 dwarf2_has_info (struct objfile *objfile,
2566 const struct dwarf2_debug_sections *names)
2567 {
2568 if (objfile->flags & OBJF_READNEVER)
2569 return 0;
2570
2571 struct dwarf2_per_objfile *dwarf2_per_objfile
2572 = get_dwarf2_per_objfile (objfile);
2573
2574 if (dwarf2_per_objfile == NULL)
2575 {
2576 /* Initialize per-objfile state. */
2577 dwarf2_per_objfile
2578 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2579 names);
2580 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
2581 }
2582 return (!dwarf2_per_objfile->info.is_virtual
2583 && dwarf2_per_objfile->info.s.section != NULL
2584 && !dwarf2_per_objfile->abbrev.is_virtual
2585 && dwarf2_per_objfile->abbrev.s.section != NULL);
2586 }
2587
2588 /* Return the containing section of virtual section SECTION. */
2589
2590 static struct dwarf2_section_info *
2591 get_containing_section (const struct dwarf2_section_info *section)
2592 {
2593 gdb_assert (section->is_virtual);
2594 return section->s.containing_section;
2595 }
2596
2597 /* Return the bfd owner of SECTION. */
2598
2599 static struct bfd *
2600 get_section_bfd_owner (const struct dwarf2_section_info *section)
2601 {
2602 if (section->is_virtual)
2603 {
2604 section = get_containing_section (section);
2605 gdb_assert (!section->is_virtual);
2606 }
2607 return section->s.section->owner;
2608 }
2609
2610 /* Return the bfd section of SECTION.
2611 Returns NULL if the section is not present. */
2612
2613 static asection *
2614 get_section_bfd_section (const struct dwarf2_section_info *section)
2615 {
2616 if (section->is_virtual)
2617 {
2618 section = get_containing_section (section);
2619 gdb_assert (!section->is_virtual);
2620 }
2621 return section->s.section;
2622 }
2623
2624 /* Return the name of SECTION. */
2625
2626 static const char *
2627 get_section_name (const struct dwarf2_section_info *section)
2628 {
2629 asection *sectp = get_section_bfd_section (section);
2630
2631 gdb_assert (sectp != NULL);
2632 return bfd_section_name (get_section_bfd_owner (section), sectp);
2633 }
2634
2635 /* Return the name of the file SECTION is in. */
2636
2637 static const char *
2638 get_section_file_name (const struct dwarf2_section_info *section)
2639 {
2640 bfd *abfd = get_section_bfd_owner (section);
2641
2642 return bfd_get_filename (abfd);
2643 }
2644
2645 /* Return the id of SECTION.
2646 Returns 0 if SECTION doesn't exist. */
2647
2648 static int
2649 get_section_id (const struct dwarf2_section_info *section)
2650 {
2651 asection *sectp = get_section_bfd_section (section);
2652
2653 if (sectp == NULL)
2654 return 0;
2655 return sectp->id;
2656 }
2657
2658 /* Return the flags of SECTION.
2659 SECTION (or containing section if this is a virtual section) must exist. */
2660
2661 static int
2662 get_section_flags (const struct dwarf2_section_info *section)
2663 {
2664 asection *sectp = get_section_bfd_section (section);
2665
2666 gdb_assert (sectp != NULL);
2667 return bfd_get_section_flags (sectp->owner, sectp);
2668 }
2669
2670 /* When loading sections, we look either for uncompressed section or for
2671 compressed section names. */
2672
2673 static int
2674 section_is_p (const char *section_name,
2675 const struct dwarf2_section_names *names)
2676 {
2677 if (names->normal != NULL
2678 && strcmp (section_name, names->normal) == 0)
2679 return 1;
2680 if (names->compressed != NULL
2681 && strcmp (section_name, names->compressed) == 0)
2682 return 1;
2683 return 0;
2684 }
2685
2686 /* See declaration. */
2687
2688 void
2689 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2690 const dwarf2_debug_sections &names)
2691 {
2692 flagword aflag = bfd_get_section_flags (abfd, sectp);
2693
2694 if ((aflag & SEC_HAS_CONTENTS) == 0)
2695 {
2696 }
2697 else if (section_is_p (sectp->name, &names.info))
2698 {
2699 this->info.s.section = sectp;
2700 this->info.size = bfd_get_section_size (sectp);
2701 }
2702 else if (section_is_p (sectp->name, &names.abbrev))
2703 {
2704 this->abbrev.s.section = sectp;
2705 this->abbrev.size = bfd_get_section_size (sectp);
2706 }
2707 else if (section_is_p (sectp->name, &names.line))
2708 {
2709 this->line.s.section = sectp;
2710 this->line.size = bfd_get_section_size (sectp);
2711 }
2712 else if (section_is_p (sectp->name, &names.loc))
2713 {
2714 this->loc.s.section = sectp;
2715 this->loc.size = bfd_get_section_size (sectp);
2716 }
2717 else if (section_is_p (sectp->name, &names.loclists))
2718 {
2719 this->loclists.s.section = sectp;
2720 this->loclists.size = bfd_get_section_size (sectp);
2721 }
2722 else if (section_is_p (sectp->name, &names.macinfo))
2723 {
2724 this->macinfo.s.section = sectp;
2725 this->macinfo.size = bfd_get_section_size (sectp);
2726 }
2727 else if (section_is_p (sectp->name, &names.macro))
2728 {
2729 this->macro.s.section = sectp;
2730 this->macro.size = bfd_get_section_size (sectp);
2731 }
2732 else if (section_is_p (sectp->name, &names.str))
2733 {
2734 this->str.s.section = sectp;
2735 this->str.size = bfd_get_section_size (sectp);
2736 }
2737 else if (section_is_p (sectp->name, &names.line_str))
2738 {
2739 this->line_str.s.section = sectp;
2740 this->line_str.size = bfd_get_section_size (sectp);
2741 }
2742 else if (section_is_p (sectp->name, &names.addr))
2743 {
2744 this->addr.s.section = sectp;
2745 this->addr.size = bfd_get_section_size (sectp);
2746 }
2747 else if (section_is_p (sectp->name, &names.frame))
2748 {
2749 this->frame.s.section = sectp;
2750 this->frame.size = bfd_get_section_size (sectp);
2751 }
2752 else if (section_is_p (sectp->name, &names.eh_frame))
2753 {
2754 this->eh_frame.s.section = sectp;
2755 this->eh_frame.size = bfd_get_section_size (sectp);
2756 }
2757 else if (section_is_p (sectp->name, &names.ranges))
2758 {
2759 this->ranges.s.section = sectp;
2760 this->ranges.size = bfd_get_section_size (sectp);
2761 }
2762 else if (section_is_p (sectp->name, &names.rnglists))
2763 {
2764 this->rnglists.s.section = sectp;
2765 this->rnglists.size = bfd_get_section_size (sectp);
2766 }
2767 else if (section_is_p (sectp->name, &names.types))
2768 {
2769 struct dwarf2_section_info type_section;
2770
2771 memset (&type_section, 0, sizeof (type_section));
2772 type_section.s.section = sectp;
2773 type_section.size = bfd_get_section_size (sectp);
2774
2775 VEC_safe_push (dwarf2_section_info_def, this->types,
2776 &type_section);
2777 }
2778 else if (section_is_p (sectp->name, &names.gdb_index))
2779 {
2780 this->gdb_index.s.section = sectp;
2781 this->gdb_index.size = bfd_get_section_size (sectp);
2782 }
2783 else if (section_is_p (sectp->name, &names.debug_names))
2784 {
2785 this->debug_names.s.section = sectp;
2786 this->debug_names.size = bfd_get_section_size (sectp);
2787 }
2788 else if (section_is_p (sectp->name, &names.debug_aranges))
2789 {
2790 this->debug_aranges.s.section = sectp;
2791 this->debug_aranges.size = bfd_get_section_size (sectp);
2792 }
2793
2794 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2795 && bfd_section_vma (abfd, sectp) == 0)
2796 this->has_section_at_zero = true;
2797 }
2798
2799 /* A helper function that decides whether a section is empty,
2800 or not present. */
2801
2802 static int
2803 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2804 {
2805 if (section->is_virtual)
2806 return section->size == 0;
2807 return section->s.section == NULL || section->size == 0;
2808 }
2809
2810 /* Read the contents of the section INFO.
2811 OBJFILE is the main object file, but not necessarily the file where
2812 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2813 of the DWO file.
2814 If the section is compressed, uncompress it before returning. */
2815
2816 static void
2817 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2818 {
2819 asection *sectp;
2820 bfd *abfd;
2821 gdb_byte *buf, *retbuf;
2822
2823 if (info->readin)
2824 return;
2825 info->buffer = NULL;
2826 info->readin = 1;
2827
2828 if (dwarf2_section_empty_p (info))
2829 return;
2830
2831 sectp = get_section_bfd_section (info);
2832
2833 /* If this is a virtual section we need to read in the real one first. */
2834 if (info->is_virtual)
2835 {
2836 struct dwarf2_section_info *containing_section =
2837 get_containing_section (info);
2838
2839 gdb_assert (sectp != NULL);
2840 if ((sectp->flags & SEC_RELOC) != 0)
2841 {
2842 error (_("Dwarf Error: DWP format V2 with relocations is not"
2843 " supported in section %s [in module %s]"),
2844 get_section_name (info), get_section_file_name (info));
2845 }
2846 dwarf2_read_section (objfile, containing_section);
2847 /* Other code should have already caught virtual sections that don't
2848 fit. */
2849 gdb_assert (info->virtual_offset + info->size
2850 <= containing_section->size);
2851 /* If the real section is empty or there was a problem reading the
2852 section we shouldn't get here. */
2853 gdb_assert (containing_section->buffer != NULL);
2854 info->buffer = containing_section->buffer + info->virtual_offset;
2855 return;
2856 }
2857
2858 /* If the section has relocations, we must read it ourselves.
2859 Otherwise we attach it to the BFD. */
2860 if ((sectp->flags & SEC_RELOC) == 0)
2861 {
2862 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2863 return;
2864 }
2865
2866 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2867 info->buffer = buf;
2868
2869 /* When debugging .o files, we may need to apply relocations; see
2870 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2871 We never compress sections in .o files, so we only need to
2872 try this when the section is not compressed. */
2873 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2874 if (retbuf != NULL)
2875 {
2876 info->buffer = retbuf;
2877 return;
2878 }
2879
2880 abfd = get_section_bfd_owner (info);
2881 gdb_assert (abfd != NULL);
2882
2883 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2884 || bfd_bread (buf, info->size, abfd) != info->size)
2885 {
2886 error (_("Dwarf Error: Can't read DWARF data"
2887 " in section %s [in module %s]"),
2888 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2889 }
2890 }
2891
2892 /* A helper function that returns the size of a section in a safe way.
2893 If you are positive that the section has been read before using the
2894 size, then it is safe to refer to the dwarf2_section_info object's
2895 "size" field directly. In other cases, you must call this
2896 function, because for compressed sections the size field is not set
2897 correctly until the section has been read. */
2898
2899 static bfd_size_type
2900 dwarf2_section_size (struct objfile *objfile,
2901 struct dwarf2_section_info *info)
2902 {
2903 if (!info->readin)
2904 dwarf2_read_section (objfile, info);
2905 return info->size;
2906 }
2907
2908 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2909 SECTION_NAME. */
2910
2911 void
2912 dwarf2_get_section_info (struct objfile *objfile,
2913 enum dwarf2_section_enum sect,
2914 asection **sectp, const gdb_byte **bufp,
2915 bfd_size_type *sizep)
2916 {
2917 struct dwarf2_per_objfile *data
2918 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2919 dwarf2_objfile_data_key);
2920 struct dwarf2_section_info *info;
2921
2922 /* We may see an objfile without any DWARF, in which case we just
2923 return nothing. */
2924 if (data == NULL)
2925 {
2926 *sectp = NULL;
2927 *bufp = NULL;
2928 *sizep = 0;
2929 return;
2930 }
2931 switch (sect)
2932 {
2933 case DWARF2_DEBUG_FRAME:
2934 info = &data->frame;
2935 break;
2936 case DWARF2_EH_FRAME:
2937 info = &data->eh_frame;
2938 break;
2939 default:
2940 gdb_assert_not_reached ("unexpected section");
2941 }
2942
2943 dwarf2_read_section (objfile, info);
2944
2945 *sectp = get_section_bfd_section (info);
2946 *bufp = info->buffer;
2947 *sizep = info->size;
2948 }
2949
2950 /* A helper function to find the sections for a .dwz file. */
2951
2952 static void
2953 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2954 {
2955 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2956
2957 /* Note that we only support the standard ELF names, because .dwz
2958 is ELF-only (at the time of writing). */
2959 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2960 {
2961 dwz_file->abbrev.s.section = sectp;
2962 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2963 }
2964 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2965 {
2966 dwz_file->info.s.section = sectp;
2967 dwz_file->info.size = bfd_get_section_size (sectp);
2968 }
2969 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2970 {
2971 dwz_file->str.s.section = sectp;
2972 dwz_file->str.size = bfd_get_section_size (sectp);
2973 }
2974 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2975 {
2976 dwz_file->line.s.section = sectp;
2977 dwz_file->line.size = bfd_get_section_size (sectp);
2978 }
2979 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2980 {
2981 dwz_file->macro.s.section = sectp;
2982 dwz_file->macro.size = bfd_get_section_size (sectp);
2983 }
2984 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2985 {
2986 dwz_file->gdb_index.s.section = sectp;
2987 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2988 }
2989 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2990 {
2991 dwz_file->debug_names.s.section = sectp;
2992 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2993 }
2994 }
2995
2996 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2997 there is no .gnu_debugaltlink section in the file. Error if there
2998 is such a section but the file cannot be found. */
2999
3000 static struct dwz_file *
3001 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
3002 {
3003 const char *filename;
3004 struct dwz_file *result;
3005 bfd_size_type buildid_len_arg;
3006 size_t buildid_len;
3007 bfd_byte *buildid;
3008
3009 if (dwarf2_per_objfile->dwz_file != NULL)
3010 return dwarf2_per_objfile->dwz_file;
3011
3012 bfd_set_error (bfd_error_no_error);
3013 gdb::unique_xmalloc_ptr<char> data
3014 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
3015 &buildid_len_arg, &buildid));
3016 if (data == NULL)
3017 {
3018 if (bfd_get_error () == bfd_error_no_error)
3019 return NULL;
3020 error (_("could not read '.gnu_debugaltlink' section: %s"),
3021 bfd_errmsg (bfd_get_error ()));
3022 }
3023
3024 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
3025
3026 buildid_len = (size_t) buildid_len_arg;
3027
3028 filename = data.get ();
3029
3030 std::string abs_storage;
3031 if (!IS_ABSOLUTE_PATH (filename))
3032 {
3033 gdb::unique_xmalloc_ptr<char> abs
3034 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
3035
3036 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
3037 filename = abs_storage.c_str ();
3038 }
3039
3040 /* First try the file name given in the section. If that doesn't
3041 work, try to use the build-id instead. */
3042 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
3043 if (dwz_bfd != NULL)
3044 {
3045 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
3046 dwz_bfd.release ();
3047 }
3048
3049 if (dwz_bfd == NULL)
3050 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
3051
3052 if (dwz_bfd == NULL)
3053 error (_("could not find '.gnu_debugaltlink' file for %s"),
3054 objfile_name (dwarf2_per_objfile->objfile));
3055
3056 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
3057 struct dwz_file);
3058 result->dwz_bfd = dwz_bfd.release ();
3059
3060 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
3061
3062 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
3063 dwarf2_per_objfile->dwz_file = result;
3064 return result;
3065 }
3066 \f
3067 /* DWARF quick_symbols_functions support. */
3068
3069 /* TUs can share .debug_line entries, and there can be a lot more TUs than
3070 unique line tables, so we maintain a separate table of all .debug_line
3071 derived entries to support the sharing.
3072 All the quick functions need is the list of file names. We discard the
3073 line_header when we're done and don't need to record it here. */
3074 struct quick_file_names
3075 {
3076 /* The data used to construct the hash key. */
3077 struct stmt_list_hash hash;
3078
3079 /* The number of entries in file_names, real_names. */
3080 unsigned int num_file_names;
3081
3082 /* The file names from the line table, after being run through
3083 file_full_name. */
3084 const char **file_names;
3085
3086 /* The file names from the line table after being run through
3087 gdb_realpath. These are computed lazily. */
3088 const char **real_names;
3089 };
3090
3091 /* When using the index (and thus not using psymtabs), each CU has an
3092 object of this type. This is used to hold information needed by
3093 the various "quick" methods. */
3094 struct dwarf2_per_cu_quick_data
3095 {
3096 /* The file table. This can be NULL if there was no file table
3097 or it's currently not read in.
3098 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
3099 struct quick_file_names *file_names;
3100
3101 /* The corresponding symbol table. This is NULL if symbols for this
3102 CU have not yet been read. */
3103 struct compunit_symtab *compunit_symtab;
3104
3105 /* A temporary mark bit used when iterating over all CUs in
3106 expand_symtabs_matching. */
3107 unsigned int mark : 1;
3108
3109 /* True if we've tried to read the file table and found there isn't one.
3110 There will be no point in trying to read it again next time. */
3111 unsigned int no_file_data : 1;
3112 };
3113
3114 /* Utility hash function for a stmt_list_hash. */
3115
3116 static hashval_t
3117 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
3118 {
3119 hashval_t v = 0;
3120
3121 if (stmt_list_hash->dwo_unit != NULL)
3122 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
3123 v += to_underlying (stmt_list_hash->line_sect_off);
3124 return v;
3125 }
3126
3127 /* Utility equality function for a stmt_list_hash. */
3128
3129 static int
3130 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
3131 const struct stmt_list_hash *rhs)
3132 {
3133 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
3134 return 0;
3135 if (lhs->dwo_unit != NULL
3136 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
3137 return 0;
3138
3139 return lhs->line_sect_off == rhs->line_sect_off;
3140 }
3141
3142 /* Hash function for a quick_file_names. */
3143
3144 static hashval_t
3145 hash_file_name_entry (const void *e)
3146 {
3147 const struct quick_file_names *file_data
3148 = (const struct quick_file_names *) e;
3149
3150 return hash_stmt_list_entry (&file_data->hash);
3151 }
3152
3153 /* Equality function for a quick_file_names. */
3154
3155 static int
3156 eq_file_name_entry (const void *a, const void *b)
3157 {
3158 const struct quick_file_names *ea = (const struct quick_file_names *) a;
3159 const struct quick_file_names *eb = (const struct quick_file_names *) b;
3160
3161 return eq_stmt_list_entry (&ea->hash, &eb->hash);
3162 }
3163
3164 /* Delete function for a quick_file_names. */
3165
3166 static void
3167 delete_file_name_entry (void *e)
3168 {
3169 struct quick_file_names *file_data = (struct quick_file_names *) e;
3170 int i;
3171
3172 for (i = 0; i < file_data->num_file_names; ++i)
3173 {
3174 xfree ((void*) file_data->file_names[i]);
3175 if (file_data->real_names)
3176 xfree ((void*) file_data->real_names[i]);
3177 }
3178
3179 /* The space for the struct itself lives on objfile_obstack,
3180 so we don't free it here. */
3181 }
3182
3183 /* Create a quick_file_names hash table. */
3184
3185 static htab_t
3186 create_quick_file_names_table (unsigned int nr_initial_entries)
3187 {
3188 return htab_create_alloc (nr_initial_entries,
3189 hash_file_name_entry, eq_file_name_entry,
3190 delete_file_name_entry, xcalloc, xfree);
3191 }
3192
3193 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3194 have to be created afterwards. You should call age_cached_comp_units after
3195 processing PER_CU->CU. dw2_setup must have been already called. */
3196
3197 static void
3198 load_cu (struct dwarf2_per_cu_data *per_cu)
3199 {
3200 if (per_cu->is_debug_types)
3201 load_full_type_unit (per_cu);
3202 else
3203 load_full_comp_unit (per_cu, language_minimal);
3204
3205 if (per_cu->cu == NULL)
3206 return; /* Dummy CU. */
3207
3208 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
3209 }
3210
3211 /* Read in the symbols for PER_CU. */
3212
3213 static void
3214 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3215 {
3216 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3217
3218 /* Skip type_unit_groups, reading the type units they contain
3219 is handled elsewhere. */
3220 if (IS_TYPE_UNIT_GROUP (per_cu))
3221 return;
3222
3223 /* The destructor of dwarf2_queue_guard frees any entries left on
3224 the queue. After this point we're guaranteed to leave this function
3225 with the dwarf queue empty. */
3226 dwarf2_queue_guard q_guard;
3227
3228 if (dwarf2_per_objfile->using_index
3229 ? per_cu->v.quick->compunit_symtab == NULL
3230 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3231 {
3232 queue_comp_unit (per_cu, language_minimal);
3233 load_cu (per_cu);
3234
3235 /* If we just loaded a CU from a DWO, and we're working with an index
3236 that may badly handle TUs, load all the TUs in that DWO as well.
3237 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3238 if (!per_cu->is_debug_types
3239 && per_cu->cu != NULL
3240 && per_cu->cu->dwo_unit != NULL
3241 && dwarf2_per_objfile->index_table != NULL
3242 && dwarf2_per_objfile->index_table->version <= 7
3243 /* DWP files aren't supported yet. */
3244 && get_dwp_file (dwarf2_per_objfile) == NULL)
3245 queue_and_load_all_dwo_tus (per_cu);
3246 }
3247
3248 process_queue (dwarf2_per_objfile);
3249
3250 /* Age the cache, releasing compilation units that have not
3251 been used recently. */
3252 age_cached_comp_units (dwarf2_per_objfile);
3253 }
3254
3255 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3256 the objfile from which this CU came. Returns the resulting symbol
3257 table. */
3258
3259 static struct compunit_symtab *
3260 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3261 {
3262 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3263
3264 gdb_assert (dwarf2_per_objfile->using_index);
3265 if (!per_cu->v.quick->compunit_symtab)
3266 {
3267 struct cleanup *back_to = make_cleanup (free_cached_comp_units,
3268 dwarf2_per_objfile);
3269 scoped_restore decrementer = increment_reading_symtab ();
3270 dw2_do_instantiate_symtab (per_cu);
3271 process_cu_includes (dwarf2_per_objfile);
3272 do_cleanups (back_to);
3273 }
3274
3275 return per_cu->v.quick->compunit_symtab;
3276 }
3277
3278 /* Return the CU/TU given its index.
3279
3280 This is intended for loops like:
3281
3282 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3283 + dwarf2_per_objfile->n_type_units); ++i)
3284 {
3285 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3286
3287 ...;
3288 }
3289 */
3290
3291 static struct dwarf2_per_cu_data *
3292 dw2_get_cutu (struct dwarf2_per_objfile *dwarf2_per_objfile,
3293 int index)
3294 {
3295 if (index >= dwarf2_per_objfile->n_comp_units)
3296 {
3297 index -= dwarf2_per_objfile->n_comp_units;
3298 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3299 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
3300 }
3301
3302 return dwarf2_per_objfile->all_comp_units[index];
3303 }
3304
3305 /* Return the CU given its index.
3306 This differs from dw2_get_cutu in that it's for when you know INDEX
3307 refers to a CU. */
3308
3309 static struct dwarf2_per_cu_data *
3310 dw2_get_cu (struct dwarf2_per_objfile *dwarf2_per_objfile, int index)
3311 {
3312 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
3313
3314 return dwarf2_per_objfile->all_comp_units[index];
3315 }
3316
3317 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3318 objfile_obstack, and constructed with the specified field
3319 values. */
3320
3321 static dwarf2_per_cu_data *
3322 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3323 struct dwarf2_section_info *section,
3324 int is_dwz,
3325 sect_offset sect_off, ULONGEST length)
3326 {
3327 struct objfile *objfile = dwarf2_per_objfile->objfile;
3328 dwarf2_per_cu_data *the_cu
3329 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3330 struct dwarf2_per_cu_data);
3331 the_cu->sect_off = sect_off;
3332 the_cu->length = length;
3333 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
3334 the_cu->section = section;
3335 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3336 struct dwarf2_per_cu_quick_data);
3337 the_cu->is_dwz = is_dwz;
3338 return the_cu;
3339 }
3340
3341 /* A helper for create_cus_from_index that handles a given list of
3342 CUs. */
3343
3344 static void
3345 create_cus_from_index_list (struct objfile *objfile,
3346 const gdb_byte *cu_list, offset_type n_elements,
3347 struct dwarf2_section_info *section,
3348 int is_dwz,
3349 int base_offset)
3350 {
3351 offset_type i;
3352 struct dwarf2_per_objfile *dwarf2_per_objfile
3353 = get_dwarf2_per_objfile (objfile);
3354
3355 for (i = 0; i < n_elements; i += 2)
3356 {
3357 gdb_static_assert (sizeof (ULONGEST) >= 8);
3358
3359 sect_offset sect_off
3360 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3361 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3362 cu_list += 2 * 8;
3363
3364 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
3365 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3366 sect_off, length);
3367 }
3368 }
3369
3370 /* Read the CU list from the mapped index, and use it to create all
3371 the CU objects for this objfile. */
3372
3373 static void
3374 create_cus_from_index (struct objfile *objfile,
3375 const gdb_byte *cu_list, offset_type cu_list_elements,
3376 const gdb_byte *dwz_list, offset_type dwz_elements)
3377 {
3378 struct dwz_file *dwz;
3379 struct dwarf2_per_objfile *dwarf2_per_objfile
3380 = get_dwarf2_per_objfile (objfile);
3381
3382 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3383 dwarf2_per_objfile->all_comp_units =
3384 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3385 dwarf2_per_objfile->n_comp_units);
3386
3387 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3388 &dwarf2_per_objfile->info, 0, 0);
3389
3390 if (dwz_elements == 0)
3391 return;
3392
3393 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3394 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3395 cu_list_elements / 2);
3396 }
3397
3398 /* Create the signatured type hash table from the index. */
3399
3400 static void
3401 create_signatured_type_table_from_index (struct objfile *objfile,
3402 struct dwarf2_section_info *section,
3403 const gdb_byte *bytes,
3404 offset_type elements)
3405 {
3406 offset_type i;
3407 htab_t sig_types_hash;
3408 struct dwarf2_per_objfile *dwarf2_per_objfile
3409 = get_dwarf2_per_objfile (objfile);
3410
3411 dwarf2_per_objfile->n_type_units
3412 = dwarf2_per_objfile->n_allocated_type_units
3413 = elements / 3;
3414 dwarf2_per_objfile->all_type_units =
3415 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3416
3417 sig_types_hash = allocate_signatured_type_table (objfile);
3418
3419 for (i = 0; i < elements; i += 3)
3420 {
3421 struct signatured_type *sig_type;
3422 ULONGEST signature;
3423 void **slot;
3424 cu_offset type_offset_in_tu;
3425
3426 gdb_static_assert (sizeof (ULONGEST) >= 8);
3427 sect_offset sect_off
3428 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3429 type_offset_in_tu
3430 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3431 BFD_ENDIAN_LITTLE);
3432 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3433 bytes += 3 * 8;
3434
3435 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3436 struct signatured_type);
3437 sig_type->signature = signature;
3438 sig_type->type_offset_in_tu = type_offset_in_tu;
3439 sig_type->per_cu.is_debug_types = 1;
3440 sig_type->per_cu.section = section;
3441 sig_type->per_cu.sect_off = sect_off;
3442 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3443 sig_type->per_cu.v.quick
3444 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3445 struct dwarf2_per_cu_quick_data);
3446
3447 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3448 *slot = sig_type;
3449
3450 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3451 }
3452
3453 dwarf2_per_objfile->signatured_types = sig_types_hash;
3454 }
3455
3456 /* Create the signatured type hash table from .debug_names. */
3457
3458 static void
3459 create_signatured_type_table_from_debug_names
3460 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3461 const mapped_debug_names &map,
3462 struct dwarf2_section_info *section,
3463 struct dwarf2_section_info *abbrev_section)
3464 {
3465 struct objfile *objfile = dwarf2_per_objfile->objfile;
3466
3467 dwarf2_read_section (objfile, section);
3468 dwarf2_read_section (objfile, abbrev_section);
3469
3470 dwarf2_per_objfile->n_type_units
3471 = dwarf2_per_objfile->n_allocated_type_units
3472 = map.tu_count;
3473 dwarf2_per_objfile->all_type_units
3474 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3475
3476 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3477
3478 for (uint32_t i = 0; i < map.tu_count; ++i)
3479 {
3480 struct signatured_type *sig_type;
3481 ULONGEST signature;
3482 void **slot;
3483 cu_offset type_offset_in_tu;
3484
3485 sect_offset sect_off
3486 = (sect_offset) (extract_unsigned_integer
3487 (map.tu_table_reordered + i * map.offset_size,
3488 map.offset_size,
3489 map.dwarf5_byte_order));
3490
3491 comp_unit_head cu_header;
3492 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3493 abbrev_section,
3494 section->buffer + to_underlying (sect_off),
3495 rcuh_kind::TYPE);
3496
3497 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3498 struct signatured_type);
3499 sig_type->signature = cu_header.signature;
3500 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3501 sig_type->per_cu.is_debug_types = 1;
3502 sig_type->per_cu.section = section;
3503 sig_type->per_cu.sect_off = sect_off;
3504 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3505 sig_type->per_cu.v.quick
3506 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3507 struct dwarf2_per_cu_quick_data);
3508
3509 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3510 *slot = sig_type;
3511
3512 dwarf2_per_objfile->all_type_units[i] = sig_type;
3513 }
3514
3515 dwarf2_per_objfile->signatured_types = sig_types_hash;
3516 }
3517
3518 /* Read the address map data from the mapped index, and use it to
3519 populate the objfile's psymtabs_addrmap. */
3520
3521 static void
3522 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3523 struct mapped_index *index)
3524 {
3525 struct objfile *objfile = dwarf2_per_objfile->objfile;
3526 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3527 const gdb_byte *iter, *end;
3528 struct addrmap *mutable_map;
3529 CORE_ADDR baseaddr;
3530
3531 auto_obstack temp_obstack;
3532
3533 mutable_map = addrmap_create_mutable (&temp_obstack);
3534
3535 iter = index->address_table.data ();
3536 end = iter + index->address_table.size ();
3537
3538 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3539
3540 while (iter < end)
3541 {
3542 ULONGEST hi, lo, cu_index;
3543 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3544 iter += 8;
3545 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3546 iter += 8;
3547 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3548 iter += 4;
3549
3550 if (lo > hi)
3551 {
3552 complaint (&symfile_complaints,
3553 _(".gdb_index address table has invalid range (%s - %s)"),
3554 hex_string (lo), hex_string (hi));
3555 continue;
3556 }
3557
3558 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3559 {
3560 complaint (&symfile_complaints,
3561 _(".gdb_index address table has invalid CU number %u"),
3562 (unsigned) cu_index);
3563 continue;
3564 }
3565
3566 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3567 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3568 addrmap_set_empty (mutable_map, lo, hi - 1,
3569 dw2_get_cutu (dwarf2_per_objfile, cu_index));
3570 }
3571
3572 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3573 &objfile->objfile_obstack);
3574 }
3575
3576 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3577 populate the objfile's psymtabs_addrmap. */
3578
3579 static void
3580 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3581 struct dwarf2_section_info *section)
3582 {
3583 struct objfile *objfile = dwarf2_per_objfile->objfile;
3584 bfd *abfd = objfile->obfd;
3585 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3586 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3587 SECT_OFF_TEXT (objfile));
3588
3589 auto_obstack temp_obstack;
3590 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3591
3592 std::unordered_map<sect_offset,
3593 dwarf2_per_cu_data *,
3594 gdb::hash_enum<sect_offset>>
3595 debug_info_offset_to_per_cu;
3596 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3597 {
3598 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, cui);
3599 const auto insertpair
3600 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3601 if (!insertpair.second)
3602 {
3603 warning (_("Section .debug_aranges in %s has duplicate "
3604 "debug_info_offset %s, ignoring .debug_aranges."),
3605 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3606 return;
3607 }
3608 }
3609
3610 dwarf2_read_section (objfile, section);
3611
3612 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3613
3614 const gdb_byte *addr = section->buffer;
3615
3616 while (addr < section->buffer + section->size)
3617 {
3618 const gdb_byte *const entry_addr = addr;
3619 unsigned int bytes_read;
3620
3621 const LONGEST entry_length = read_initial_length (abfd, addr,
3622 &bytes_read);
3623 addr += bytes_read;
3624
3625 const gdb_byte *const entry_end = addr + entry_length;
3626 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3627 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3628 if (addr + entry_length > section->buffer + section->size)
3629 {
3630 warning (_("Section .debug_aranges in %s entry at offset %zu "
3631 "length %s exceeds section length %s, "
3632 "ignoring .debug_aranges."),
3633 objfile_name (objfile), entry_addr - section->buffer,
3634 plongest (bytes_read + entry_length),
3635 pulongest (section->size));
3636 return;
3637 }
3638
3639 /* The version number. */
3640 const uint16_t version = read_2_bytes (abfd, addr);
3641 addr += 2;
3642 if (version != 2)
3643 {
3644 warning (_("Section .debug_aranges in %s entry at offset %zu "
3645 "has unsupported version %d, ignoring .debug_aranges."),
3646 objfile_name (objfile), entry_addr - section->buffer,
3647 version);
3648 return;
3649 }
3650
3651 const uint64_t debug_info_offset
3652 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3653 addr += offset_size;
3654 const auto per_cu_it
3655 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3656 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3657 {
3658 warning (_("Section .debug_aranges in %s entry at offset %zu "
3659 "debug_info_offset %s does not exists, "
3660 "ignoring .debug_aranges."),
3661 objfile_name (objfile), entry_addr - section->buffer,
3662 pulongest (debug_info_offset));
3663 return;
3664 }
3665 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3666
3667 const uint8_t address_size = *addr++;
3668 if (address_size < 1 || address_size > 8)
3669 {
3670 warning (_("Section .debug_aranges in %s entry at offset %zu "
3671 "address_size %u is invalid, ignoring .debug_aranges."),
3672 objfile_name (objfile), entry_addr - section->buffer,
3673 address_size);
3674 return;
3675 }
3676
3677 const uint8_t segment_selector_size = *addr++;
3678 if (segment_selector_size != 0)
3679 {
3680 warning (_("Section .debug_aranges in %s entry at offset %zu "
3681 "segment_selector_size %u is not supported, "
3682 "ignoring .debug_aranges."),
3683 objfile_name (objfile), entry_addr - section->buffer,
3684 segment_selector_size);
3685 return;
3686 }
3687
3688 /* Must pad to an alignment boundary that is twice the address
3689 size. It is undocumented by the DWARF standard but GCC does
3690 use it. */
3691 for (size_t padding = ((-(addr - section->buffer))
3692 & (2 * address_size - 1));
3693 padding > 0; padding--)
3694 if (*addr++ != 0)
3695 {
3696 warning (_("Section .debug_aranges in %s entry at offset %zu "
3697 "padding is not zero, ignoring .debug_aranges."),
3698 objfile_name (objfile), entry_addr - section->buffer);
3699 return;
3700 }
3701
3702 for (;;)
3703 {
3704 if (addr + 2 * address_size > entry_end)
3705 {
3706 warning (_("Section .debug_aranges in %s entry at offset %zu "
3707 "address list is not properly terminated, "
3708 "ignoring .debug_aranges."),
3709 objfile_name (objfile), entry_addr - section->buffer);
3710 return;
3711 }
3712 ULONGEST start = extract_unsigned_integer (addr, address_size,
3713 dwarf5_byte_order);
3714 addr += address_size;
3715 ULONGEST length = extract_unsigned_integer (addr, address_size,
3716 dwarf5_byte_order);
3717 addr += address_size;
3718 if (start == 0 && length == 0)
3719 break;
3720 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3721 {
3722 /* Symbol was eliminated due to a COMDAT group. */
3723 continue;
3724 }
3725 ULONGEST end = start + length;
3726 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3727 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3728 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3729 }
3730 }
3731
3732 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3733 &objfile->objfile_obstack);
3734 }
3735
3736 /* The hash function for strings in the mapped index. This is the same as
3737 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3738 implementation. This is necessary because the hash function is tied to the
3739 format of the mapped index file. The hash values do not have to match with
3740 SYMBOL_HASH_NEXT.
3741
3742 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3743
3744 static hashval_t
3745 mapped_index_string_hash (int index_version, const void *p)
3746 {
3747 const unsigned char *str = (const unsigned char *) p;
3748 hashval_t r = 0;
3749 unsigned char c;
3750
3751 while ((c = *str++) != 0)
3752 {
3753 if (index_version >= 5)
3754 c = tolower (c);
3755 r = r * 67 + c - 113;
3756 }
3757
3758 return r;
3759 }
3760
3761 /* Find a slot in the mapped index INDEX for the object named NAME.
3762 If NAME is found, set *VEC_OUT to point to the CU vector in the
3763 constant pool and return true. If NAME cannot be found, return
3764 false. */
3765
3766 static bool
3767 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3768 offset_type **vec_out)
3769 {
3770 offset_type hash;
3771 offset_type slot, step;
3772 int (*cmp) (const char *, const char *);
3773
3774 gdb::unique_xmalloc_ptr<char> without_params;
3775 if (current_language->la_language == language_cplus
3776 || current_language->la_language == language_fortran
3777 || current_language->la_language == language_d)
3778 {
3779 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3780 not contain any. */
3781
3782 if (strchr (name, '(') != NULL)
3783 {
3784 without_params = cp_remove_params (name);
3785
3786 if (without_params != NULL)
3787 name = without_params.get ();
3788 }
3789 }
3790
3791 /* Index version 4 did not support case insensitive searches. But the
3792 indices for case insensitive languages are built in lowercase, therefore
3793 simulate our NAME being searched is also lowercased. */
3794 hash = mapped_index_string_hash ((index->version == 4
3795 && case_sensitivity == case_sensitive_off
3796 ? 5 : index->version),
3797 name);
3798
3799 slot = hash & (index->symbol_table.size () - 1);
3800 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3801 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3802
3803 for (;;)
3804 {
3805 const char *str;
3806
3807 const auto &bucket = index->symbol_table[slot];
3808 if (bucket.name == 0 && bucket.vec == 0)
3809 return false;
3810
3811 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3812 if (!cmp (name, str))
3813 {
3814 *vec_out = (offset_type *) (index->constant_pool
3815 + MAYBE_SWAP (bucket.vec));
3816 return true;
3817 }
3818
3819 slot = (slot + step) & (index->symbol_table.size () - 1);
3820 }
3821 }
3822
3823 /* A helper function that reads the .gdb_index from SECTION and fills
3824 in MAP. FILENAME is the name of the file containing the section;
3825 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3826 ok to use deprecated sections.
3827
3828 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3829 out parameters that are filled in with information about the CU and
3830 TU lists in the section.
3831
3832 Returns 1 if all went well, 0 otherwise. */
3833
3834 static int
3835 read_index_from_section (struct objfile *objfile,
3836 const char *filename,
3837 int deprecated_ok,
3838 struct dwarf2_section_info *section,
3839 struct mapped_index *map,
3840 const gdb_byte **cu_list,
3841 offset_type *cu_list_elements,
3842 const gdb_byte **types_list,
3843 offset_type *types_list_elements)
3844 {
3845 const gdb_byte *addr;
3846 offset_type version;
3847 offset_type *metadata;
3848 int i;
3849
3850 if (dwarf2_section_empty_p (section))
3851 return 0;
3852
3853 /* Older elfutils strip versions could keep the section in the main
3854 executable while splitting it for the separate debug info file. */
3855 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3856 return 0;
3857
3858 dwarf2_read_section (objfile, section);
3859
3860 addr = section->buffer;
3861 /* Version check. */
3862 version = MAYBE_SWAP (*(offset_type *) addr);
3863 /* Versions earlier than 3 emitted every copy of a psymbol. This
3864 causes the index to behave very poorly for certain requests. Version 3
3865 contained incomplete addrmap. So, it seems better to just ignore such
3866 indices. */
3867 if (version < 4)
3868 {
3869 static int warning_printed = 0;
3870 if (!warning_printed)
3871 {
3872 warning (_("Skipping obsolete .gdb_index section in %s."),
3873 filename);
3874 warning_printed = 1;
3875 }
3876 return 0;
3877 }
3878 /* Index version 4 uses a different hash function than index version
3879 5 and later.
3880
3881 Versions earlier than 6 did not emit psymbols for inlined
3882 functions. Using these files will cause GDB not to be able to
3883 set breakpoints on inlined functions by name, so we ignore these
3884 indices unless the user has done
3885 "set use-deprecated-index-sections on". */
3886 if (version < 6 && !deprecated_ok)
3887 {
3888 static int warning_printed = 0;
3889 if (!warning_printed)
3890 {
3891 warning (_("\
3892 Skipping deprecated .gdb_index section in %s.\n\
3893 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3894 to use the section anyway."),
3895 filename);
3896 warning_printed = 1;
3897 }
3898 return 0;
3899 }
3900 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3901 of the TU (for symbols coming from TUs),
3902 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3903 Plus gold-generated indices can have duplicate entries for global symbols,
3904 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3905 These are just performance bugs, and we can't distinguish gdb-generated
3906 indices from gold-generated ones, so issue no warning here. */
3907
3908 /* Indexes with higher version than the one supported by GDB may be no
3909 longer backward compatible. */
3910 if (version > 8)
3911 return 0;
3912
3913 map->version = version;
3914 map->total_size = section->size;
3915
3916 metadata = (offset_type *) (addr + sizeof (offset_type));
3917
3918 i = 0;
3919 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3920 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3921 / 8);
3922 ++i;
3923
3924 *types_list = addr + MAYBE_SWAP (metadata[i]);
3925 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3926 - MAYBE_SWAP (metadata[i]))
3927 / 8);
3928 ++i;
3929
3930 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3931 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3932 map->address_table
3933 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3934 ++i;
3935
3936 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3937 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3938 map->symbol_table
3939 = gdb::array_view<mapped_index::symbol_table_slot>
3940 ((mapped_index::symbol_table_slot *) symbol_table,
3941 (mapped_index::symbol_table_slot *) symbol_table_end);
3942
3943 ++i;
3944 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3945
3946 return 1;
3947 }
3948
3949 /* Read .gdb_index. If everything went ok, initialize the "quick"
3950 elements of all the CUs and return 1. Otherwise, return 0. */
3951
3952 static int
3953 dwarf2_read_index (struct objfile *objfile)
3954 {
3955 struct mapped_index local_map, *map;
3956 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3957 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3958 struct dwz_file *dwz;
3959 struct dwarf2_per_objfile *dwarf2_per_objfile
3960 = get_dwarf2_per_objfile (objfile);
3961
3962 if (!read_index_from_section (objfile, objfile_name (objfile),
3963 use_deprecated_index_sections,
3964 &dwarf2_per_objfile->gdb_index, &local_map,
3965 &cu_list, &cu_list_elements,
3966 &types_list, &types_list_elements))
3967 return 0;
3968
3969 /* Don't use the index if it's empty. */
3970 if (local_map.symbol_table.empty ())
3971 return 0;
3972
3973 /* If there is a .dwz file, read it so we can get its CU list as
3974 well. */
3975 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3976 if (dwz != NULL)
3977 {
3978 struct mapped_index dwz_map;
3979 const gdb_byte *dwz_types_ignore;
3980 offset_type dwz_types_elements_ignore;
3981
3982 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3983 1,
3984 &dwz->gdb_index, &dwz_map,
3985 &dwz_list, &dwz_list_elements,
3986 &dwz_types_ignore,
3987 &dwz_types_elements_ignore))
3988 {
3989 warning (_("could not read '.gdb_index' section from %s; skipping"),
3990 bfd_get_filename (dwz->dwz_bfd));
3991 return 0;
3992 }
3993 }
3994
3995 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3996 dwz_list_elements);
3997
3998 if (types_list_elements)
3999 {
4000 struct dwarf2_section_info *section;
4001
4002 /* We can only handle a single .debug_types when we have an
4003 index. */
4004 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
4005 return 0;
4006
4007 section = VEC_index (dwarf2_section_info_def,
4008 dwarf2_per_objfile->types, 0);
4009
4010 create_signatured_type_table_from_index (objfile, section, types_list,
4011 types_list_elements);
4012 }
4013
4014 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
4015
4016 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
4017 map = new (map) mapped_index ();
4018 *map = local_map;
4019
4020 dwarf2_per_objfile->index_table = map;
4021 dwarf2_per_objfile->using_index = 1;
4022 dwarf2_per_objfile->quick_file_names_table =
4023 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
4024
4025 return 1;
4026 }
4027
4028 /* die_reader_func for dw2_get_file_names. */
4029
4030 static void
4031 dw2_get_file_names_reader (const struct die_reader_specs *reader,
4032 const gdb_byte *info_ptr,
4033 struct die_info *comp_unit_die,
4034 int has_children,
4035 void *data)
4036 {
4037 struct dwarf2_cu *cu = reader->cu;
4038 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
4039 struct dwarf2_per_objfile *dwarf2_per_objfile
4040 = cu->per_cu->dwarf2_per_objfile;
4041 struct objfile *objfile = dwarf2_per_objfile->objfile;
4042 struct dwarf2_per_cu_data *lh_cu;
4043 struct attribute *attr;
4044 int i;
4045 void **slot;
4046 struct quick_file_names *qfn;
4047
4048 gdb_assert (! this_cu->is_debug_types);
4049
4050 /* Our callers never want to match partial units -- instead they
4051 will match the enclosing full CU. */
4052 if (comp_unit_die->tag == DW_TAG_partial_unit)
4053 {
4054 this_cu->v.quick->no_file_data = 1;
4055 return;
4056 }
4057
4058 lh_cu = this_cu;
4059 slot = NULL;
4060
4061 line_header_up lh;
4062 sect_offset line_offset {};
4063
4064 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4065 if (attr)
4066 {
4067 struct quick_file_names find_entry;
4068
4069 line_offset = (sect_offset) DW_UNSND (attr);
4070
4071 /* We may have already read in this line header (TU line header sharing).
4072 If we have we're done. */
4073 find_entry.hash.dwo_unit = cu->dwo_unit;
4074 find_entry.hash.line_sect_off = line_offset;
4075 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
4076 &find_entry, INSERT);
4077 if (*slot != NULL)
4078 {
4079 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
4080 return;
4081 }
4082
4083 lh = dwarf_decode_line_header (line_offset, cu);
4084 }
4085 if (lh == NULL)
4086 {
4087 lh_cu->v.quick->no_file_data = 1;
4088 return;
4089 }
4090
4091 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
4092 qfn->hash.dwo_unit = cu->dwo_unit;
4093 qfn->hash.line_sect_off = line_offset;
4094 gdb_assert (slot != NULL);
4095 *slot = qfn;
4096
4097 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
4098
4099 qfn->num_file_names = lh->file_names.size ();
4100 qfn->file_names =
4101 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
4102 for (i = 0; i < lh->file_names.size (); ++i)
4103 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
4104 qfn->real_names = NULL;
4105
4106 lh_cu->v.quick->file_names = qfn;
4107 }
4108
4109 /* A helper for the "quick" functions which attempts to read the line
4110 table for THIS_CU. */
4111
4112 static struct quick_file_names *
4113 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
4114 {
4115 /* This should never be called for TUs. */
4116 gdb_assert (! this_cu->is_debug_types);
4117 /* Nor type unit groups. */
4118 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
4119
4120 if (this_cu->v.quick->file_names != NULL)
4121 return this_cu->v.quick->file_names;
4122 /* If we know there is no line data, no point in looking again. */
4123 if (this_cu->v.quick->no_file_data)
4124 return NULL;
4125
4126 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
4127
4128 if (this_cu->v.quick->no_file_data)
4129 return NULL;
4130 return this_cu->v.quick->file_names;
4131 }
4132
4133 /* A helper for the "quick" functions which computes and caches the
4134 real path for a given file name from the line table. */
4135
4136 static const char *
4137 dw2_get_real_path (struct objfile *objfile,
4138 struct quick_file_names *qfn, int index)
4139 {
4140 if (qfn->real_names == NULL)
4141 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
4142 qfn->num_file_names, const char *);
4143
4144 if (qfn->real_names[index] == NULL)
4145 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
4146
4147 return qfn->real_names[index];
4148 }
4149
4150 static struct symtab *
4151 dw2_find_last_source_symtab (struct objfile *objfile)
4152 {
4153 struct dwarf2_per_objfile *dwarf2_per_objfile
4154 = get_dwarf2_per_objfile (objfile);
4155 int index = dwarf2_per_objfile->n_comp_units - 1;
4156 dwarf2_per_cu_data *dwarf_cu = dw2_get_cutu (dwarf2_per_objfile, index);
4157 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
4158
4159 if (cust == NULL)
4160 return NULL;
4161
4162 return compunit_primary_filetab (cust);
4163 }
4164
4165 /* Traversal function for dw2_forget_cached_source_info. */
4166
4167 static int
4168 dw2_free_cached_file_names (void **slot, void *info)
4169 {
4170 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
4171
4172 if (file_data->real_names)
4173 {
4174 int i;
4175
4176 for (i = 0; i < file_data->num_file_names; ++i)
4177 {
4178 xfree ((void*) file_data->real_names[i]);
4179 file_data->real_names[i] = NULL;
4180 }
4181 }
4182
4183 return 1;
4184 }
4185
4186 static void
4187 dw2_forget_cached_source_info (struct objfile *objfile)
4188 {
4189 struct dwarf2_per_objfile *dwarf2_per_objfile
4190 = get_dwarf2_per_objfile (objfile);
4191
4192 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
4193 dw2_free_cached_file_names, NULL);
4194 }
4195
4196 /* Helper function for dw2_map_symtabs_matching_filename that expands
4197 the symtabs and calls the iterator. */
4198
4199 static int
4200 dw2_map_expand_apply (struct objfile *objfile,
4201 struct dwarf2_per_cu_data *per_cu,
4202 const char *name, const char *real_path,
4203 gdb::function_view<bool (symtab *)> callback)
4204 {
4205 struct compunit_symtab *last_made = objfile->compunit_symtabs;
4206
4207 /* Don't visit already-expanded CUs. */
4208 if (per_cu->v.quick->compunit_symtab)
4209 return 0;
4210
4211 /* This may expand more than one symtab, and we want to iterate over
4212 all of them. */
4213 dw2_instantiate_symtab (per_cu);
4214
4215 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4216 last_made, callback);
4217 }
4218
4219 /* Implementation of the map_symtabs_matching_filename method. */
4220
4221 static bool
4222 dw2_map_symtabs_matching_filename
4223 (struct objfile *objfile, const char *name, const char *real_path,
4224 gdb::function_view<bool (symtab *)> callback)
4225 {
4226 int i;
4227 const char *name_basename = lbasename (name);
4228 struct dwarf2_per_objfile *dwarf2_per_objfile
4229 = get_dwarf2_per_objfile (objfile);
4230
4231 /* The rule is CUs specify all the files, including those used by
4232 any TU, so there's no need to scan TUs here. */
4233
4234 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4235 {
4236 int j;
4237 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
4238 struct quick_file_names *file_data;
4239
4240 /* We only need to look at symtabs not already expanded. */
4241 if (per_cu->v.quick->compunit_symtab)
4242 continue;
4243
4244 file_data = dw2_get_file_names (per_cu);
4245 if (file_data == NULL)
4246 continue;
4247
4248 for (j = 0; j < file_data->num_file_names; ++j)
4249 {
4250 const char *this_name = file_data->file_names[j];
4251 const char *this_real_name;
4252
4253 if (compare_filenames_for_search (this_name, name))
4254 {
4255 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4256 callback))
4257 return true;
4258 continue;
4259 }
4260
4261 /* Before we invoke realpath, which can get expensive when many
4262 files are involved, do a quick comparison of the basenames. */
4263 if (! basenames_may_differ
4264 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4265 continue;
4266
4267 this_real_name = dw2_get_real_path (objfile, file_data, j);
4268 if (compare_filenames_for_search (this_real_name, name))
4269 {
4270 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4271 callback))
4272 return true;
4273 continue;
4274 }
4275
4276 if (real_path != NULL)
4277 {
4278 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4279 gdb_assert (IS_ABSOLUTE_PATH (name));
4280 if (this_real_name != NULL
4281 && FILENAME_CMP (real_path, this_real_name) == 0)
4282 {
4283 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4284 callback))
4285 return true;
4286 continue;
4287 }
4288 }
4289 }
4290 }
4291
4292 return false;
4293 }
4294
4295 /* Struct used to manage iterating over all CUs looking for a symbol. */
4296
4297 struct dw2_symtab_iterator
4298 {
4299 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
4300 struct dwarf2_per_objfile *dwarf2_per_objfile;
4301 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4302 int want_specific_block;
4303 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4304 Unused if !WANT_SPECIFIC_BLOCK. */
4305 int block_index;
4306 /* The kind of symbol we're looking for. */
4307 domain_enum domain;
4308 /* The list of CUs from the index entry of the symbol,
4309 or NULL if not found. */
4310 offset_type *vec;
4311 /* The next element in VEC to look at. */
4312 int next;
4313 /* The number of elements in VEC, or zero if there is no match. */
4314 int length;
4315 /* Have we seen a global version of the symbol?
4316 If so we can ignore all further global instances.
4317 This is to work around gold/15646, inefficient gold-generated
4318 indices. */
4319 int global_seen;
4320 };
4321
4322 /* Initialize the index symtab iterator ITER.
4323 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4324 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
4325
4326 static void
4327 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
4328 struct dwarf2_per_objfile *dwarf2_per_objfile,
4329 int want_specific_block,
4330 int block_index,
4331 domain_enum domain,
4332 const char *name)
4333 {
4334 iter->dwarf2_per_objfile = dwarf2_per_objfile;
4335 iter->want_specific_block = want_specific_block;
4336 iter->block_index = block_index;
4337 iter->domain = domain;
4338 iter->next = 0;
4339 iter->global_seen = 0;
4340
4341 mapped_index *index = dwarf2_per_objfile->index_table;
4342
4343 /* index is NULL if OBJF_READNOW. */
4344 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
4345 iter->length = MAYBE_SWAP (*iter->vec);
4346 else
4347 {
4348 iter->vec = NULL;
4349 iter->length = 0;
4350 }
4351 }
4352
4353 /* Return the next matching CU or NULL if there are no more. */
4354
4355 static struct dwarf2_per_cu_data *
4356 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4357 {
4358 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4359
4360 for ( ; iter->next < iter->length; ++iter->next)
4361 {
4362 offset_type cu_index_and_attrs =
4363 MAYBE_SWAP (iter->vec[iter->next + 1]);
4364 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4365 struct dwarf2_per_cu_data *per_cu;
4366 int want_static = iter->block_index != GLOBAL_BLOCK;
4367 /* This value is only valid for index versions >= 7. */
4368 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4369 gdb_index_symbol_kind symbol_kind =
4370 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4371 /* Only check the symbol attributes if they're present.
4372 Indices prior to version 7 don't record them,
4373 and indices >= 7 may elide them for certain symbols
4374 (gold does this). */
4375 int attrs_valid =
4376 (dwarf2_per_objfile->index_table->version >= 7
4377 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4378
4379 /* Don't crash on bad data. */
4380 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4381 + dwarf2_per_objfile->n_type_units))
4382 {
4383 complaint (&symfile_complaints,
4384 _(".gdb_index entry has bad CU index"
4385 " [in module %s]"),
4386 objfile_name (dwarf2_per_objfile->objfile));
4387 continue;
4388 }
4389
4390 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
4391
4392 /* Skip if already read in. */
4393 if (per_cu->v.quick->compunit_symtab)
4394 continue;
4395
4396 /* Check static vs global. */
4397 if (attrs_valid)
4398 {
4399 if (iter->want_specific_block
4400 && want_static != is_static)
4401 continue;
4402 /* Work around gold/15646. */
4403 if (!is_static && iter->global_seen)
4404 continue;
4405 if (!is_static)
4406 iter->global_seen = 1;
4407 }
4408
4409 /* Only check the symbol's kind if it has one. */
4410 if (attrs_valid)
4411 {
4412 switch (iter->domain)
4413 {
4414 case VAR_DOMAIN:
4415 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4416 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4417 /* Some types are also in VAR_DOMAIN. */
4418 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4419 continue;
4420 break;
4421 case STRUCT_DOMAIN:
4422 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4423 continue;
4424 break;
4425 case LABEL_DOMAIN:
4426 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4427 continue;
4428 break;
4429 default:
4430 break;
4431 }
4432 }
4433
4434 ++iter->next;
4435 return per_cu;
4436 }
4437
4438 return NULL;
4439 }
4440
4441 static struct compunit_symtab *
4442 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4443 const char *name, domain_enum domain)
4444 {
4445 struct compunit_symtab *stab_best = NULL;
4446 struct dwarf2_per_objfile *dwarf2_per_objfile
4447 = get_dwarf2_per_objfile (objfile);
4448
4449 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4450
4451 struct dw2_symtab_iterator iter;
4452 struct dwarf2_per_cu_data *per_cu;
4453
4454 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4455
4456 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4457 {
4458 struct symbol *sym, *with_opaque = NULL;
4459 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4460 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4461 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4462
4463 sym = block_find_symbol (block, name, domain,
4464 block_find_non_opaque_type_preferred,
4465 &with_opaque);
4466
4467 /* Some caution must be observed with overloaded functions
4468 and methods, since the index will not contain any overload
4469 information (but NAME might contain it). */
4470
4471 if (sym != NULL
4472 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4473 return stab;
4474 if (with_opaque != NULL
4475 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4476 stab_best = stab;
4477
4478 /* Keep looking through other CUs. */
4479 }
4480
4481 return stab_best;
4482 }
4483
4484 static void
4485 dw2_print_stats (struct objfile *objfile)
4486 {
4487 struct dwarf2_per_objfile *dwarf2_per_objfile
4488 = get_dwarf2_per_objfile (objfile);
4489 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4490 int count = 0;
4491
4492 for (int i = 0; i < total; ++i)
4493 {
4494 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4495
4496 if (!per_cu->v.quick->compunit_symtab)
4497 ++count;
4498 }
4499 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4500 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4501 }
4502
4503 /* This dumps minimal information about the index.
4504 It is called via "mt print objfiles".
4505 One use is to verify .gdb_index has been loaded by the
4506 gdb.dwarf2/gdb-index.exp testcase. */
4507
4508 static void
4509 dw2_dump (struct objfile *objfile)
4510 {
4511 struct dwarf2_per_objfile *dwarf2_per_objfile
4512 = get_dwarf2_per_objfile (objfile);
4513
4514 gdb_assert (dwarf2_per_objfile->using_index);
4515 printf_filtered (".gdb_index:");
4516 if (dwarf2_per_objfile->index_table != NULL)
4517 {
4518 printf_filtered (" version %d\n",
4519 dwarf2_per_objfile->index_table->version);
4520 }
4521 else
4522 printf_filtered (" faked for \"readnow\"\n");
4523 printf_filtered ("\n");
4524 }
4525
4526 static void
4527 dw2_relocate (struct objfile *objfile,
4528 const struct section_offsets *new_offsets,
4529 const struct section_offsets *delta)
4530 {
4531 /* There's nothing to relocate here. */
4532 }
4533
4534 static void
4535 dw2_expand_symtabs_for_function (struct objfile *objfile,
4536 const char *func_name)
4537 {
4538 struct dwarf2_per_objfile *dwarf2_per_objfile
4539 = get_dwarf2_per_objfile (objfile);
4540
4541 struct dw2_symtab_iterator iter;
4542 struct dwarf2_per_cu_data *per_cu;
4543
4544 /* Note: It doesn't matter what we pass for block_index here. */
4545 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4546 func_name);
4547
4548 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4549 dw2_instantiate_symtab (per_cu);
4550
4551 }
4552
4553 static void
4554 dw2_expand_all_symtabs (struct objfile *objfile)
4555 {
4556 struct dwarf2_per_objfile *dwarf2_per_objfile
4557 = get_dwarf2_per_objfile (objfile);
4558 int total_units = (dwarf2_per_objfile->n_comp_units
4559 + dwarf2_per_objfile->n_type_units);
4560
4561 for (int i = 0; i < total_units; ++i)
4562 {
4563 struct dwarf2_per_cu_data *per_cu
4564 = dw2_get_cutu (dwarf2_per_objfile, i);
4565
4566 dw2_instantiate_symtab (per_cu);
4567 }
4568 }
4569
4570 static void
4571 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4572 const char *fullname)
4573 {
4574 struct dwarf2_per_objfile *dwarf2_per_objfile
4575 = get_dwarf2_per_objfile (objfile);
4576
4577 /* We don't need to consider type units here.
4578 This is only called for examining code, e.g. expand_line_sal.
4579 There can be an order of magnitude (or more) more type units
4580 than comp units, and we avoid them if we can. */
4581
4582 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4583 {
4584 int j;
4585 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4586 struct quick_file_names *file_data;
4587
4588 /* We only need to look at symtabs not already expanded. */
4589 if (per_cu->v.quick->compunit_symtab)
4590 continue;
4591
4592 file_data = dw2_get_file_names (per_cu);
4593 if (file_data == NULL)
4594 continue;
4595
4596 for (j = 0; j < file_data->num_file_names; ++j)
4597 {
4598 const char *this_fullname = file_data->file_names[j];
4599
4600 if (filename_cmp (this_fullname, fullname) == 0)
4601 {
4602 dw2_instantiate_symtab (per_cu);
4603 break;
4604 }
4605 }
4606 }
4607 }
4608
4609 static void
4610 dw2_map_matching_symbols (struct objfile *objfile,
4611 const char * name, domain_enum domain,
4612 int global,
4613 int (*callback) (struct block *,
4614 struct symbol *, void *),
4615 void *data, symbol_name_match_type match,
4616 symbol_compare_ftype *ordered_compare)
4617 {
4618 /* Currently unimplemented; used for Ada. The function can be called if the
4619 current language is Ada for a non-Ada objfile using GNU index. As Ada
4620 does not look for non-Ada symbols this function should just return. */
4621 }
4622
4623 /* Symbol name matcher for .gdb_index names.
4624
4625 Symbol names in .gdb_index have a few particularities:
4626
4627 - There's no indication of which is the language of each symbol.
4628
4629 Since each language has its own symbol name matching algorithm,
4630 and we don't know which language is the right one, we must match
4631 each symbol against all languages. This would be a potential
4632 performance problem if it were not mitigated by the
4633 mapped_index::name_components lookup table, which significantly
4634 reduces the number of times we need to call into this matcher,
4635 making it a non-issue.
4636
4637 - Symbol names in the index have no overload (parameter)
4638 information. I.e., in C++, "foo(int)" and "foo(long)" both
4639 appear as "foo" in the index, for example.
4640
4641 This means that the lookup names passed to the symbol name
4642 matcher functions must have no parameter information either
4643 because (e.g.) symbol search name "foo" does not match
4644 lookup-name "foo(int)" [while swapping search name for lookup
4645 name would match].
4646 */
4647 class gdb_index_symbol_name_matcher
4648 {
4649 public:
4650 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4651 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4652
4653 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4654 Returns true if any matcher matches. */
4655 bool matches (const char *symbol_name);
4656
4657 private:
4658 /* A reference to the lookup name we're matching against. */
4659 const lookup_name_info &m_lookup_name;
4660
4661 /* A vector holding all the different symbol name matchers, for all
4662 languages. */
4663 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4664 };
4665
4666 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4667 (const lookup_name_info &lookup_name)
4668 : m_lookup_name (lookup_name)
4669 {
4670 /* Prepare the vector of comparison functions upfront, to avoid
4671 doing the same work for each symbol. Care is taken to avoid
4672 matching with the same matcher more than once if/when multiple
4673 languages use the same matcher function. */
4674 auto &matchers = m_symbol_name_matcher_funcs;
4675 matchers.reserve (nr_languages);
4676
4677 matchers.push_back (default_symbol_name_matcher);
4678
4679 for (int i = 0; i < nr_languages; i++)
4680 {
4681 const language_defn *lang = language_def ((enum language) i);
4682 symbol_name_matcher_ftype *name_matcher
4683 = get_symbol_name_matcher (lang, m_lookup_name);
4684
4685 /* Don't insert the same comparison routine more than once.
4686 Note that we do this linear walk instead of a seemingly
4687 cheaper sorted insert, or use a std::set or something like
4688 that, because relative order of function addresses is not
4689 stable. This is not a problem in practice because the number
4690 of supported languages is low, and the cost here is tiny
4691 compared to the number of searches we'll do afterwards using
4692 this object. */
4693 if (name_matcher != default_symbol_name_matcher
4694 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4695 == matchers.end ()))
4696 matchers.push_back (name_matcher);
4697 }
4698 }
4699
4700 bool
4701 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4702 {
4703 for (auto matches_name : m_symbol_name_matcher_funcs)
4704 if (matches_name (symbol_name, m_lookup_name, NULL))
4705 return true;
4706
4707 return false;
4708 }
4709
4710 /* Starting from a search name, return the string that finds the upper
4711 bound of all strings that start with SEARCH_NAME in a sorted name
4712 list. Returns the empty string to indicate that the upper bound is
4713 the end of the list. */
4714
4715 static std::string
4716 make_sort_after_prefix_name (const char *search_name)
4717 {
4718 /* When looking to complete "func", we find the upper bound of all
4719 symbols that start with "func" by looking for where we'd insert
4720 the closest string that would follow "func" in lexicographical
4721 order. Usually, that's "func"-with-last-character-incremented,
4722 i.e. "fund". Mind non-ASCII characters, though. Usually those
4723 will be UTF-8 multi-byte sequences, but we can't be certain.
4724 Especially mind the 0xff character, which is a valid character in
4725 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4726 rule out compilers allowing it in identifiers. Note that
4727 conveniently, strcmp/strcasecmp are specified to compare
4728 characters interpreted as unsigned char. So what we do is treat
4729 the whole string as a base 256 number composed of a sequence of
4730 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4731 to 0, and carries 1 to the following more-significant position.
4732 If the very first character in SEARCH_NAME ends up incremented
4733 and carries/overflows, then the upper bound is the end of the
4734 list. The string after the empty string is also the empty
4735 string.
4736
4737 Some examples of this operation:
4738
4739 SEARCH_NAME => "+1" RESULT
4740
4741 "abc" => "abd"
4742 "ab\xff" => "ac"
4743 "\xff" "a" "\xff" => "\xff" "b"
4744 "\xff" => ""
4745 "\xff\xff" => ""
4746 "" => ""
4747
4748 Then, with these symbols for example:
4749
4750 func
4751 func1
4752 fund
4753
4754 completing "func" looks for symbols between "func" and
4755 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4756 which finds "func" and "func1", but not "fund".
4757
4758 And with:
4759
4760 funcÿ (Latin1 'ÿ' [0xff])
4761 funcÿ1
4762 fund
4763
4764 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4765 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4766
4767 And with:
4768
4769 ÿÿ (Latin1 'ÿ' [0xff])
4770 ÿÿ1
4771
4772 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4773 the end of the list.
4774 */
4775 std::string after = search_name;
4776 while (!after.empty () && (unsigned char) after.back () == 0xff)
4777 after.pop_back ();
4778 if (!after.empty ())
4779 after.back () = (unsigned char) after.back () + 1;
4780 return after;
4781 }
4782
4783 /* See declaration. */
4784
4785 std::pair<std::vector<name_component>::const_iterator,
4786 std::vector<name_component>::const_iterator>
4787 mapped_index_base::find_name_components_bounds
4788 (const lookup_name_info &lookup_name_without_params) const
4789 {
4790 auto *name_cmp
4791 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4792
4793 const char *cplus
4794 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4795
4796 /* Comparison function object for lower_bound that matches against a
4797 given symbol name. */
4798 auto lookup_compare_lower = [&] (const name_component &elem,
4799 const char *name)
4800 {
4801 const char *elem_qualified = this->symbol_name_at (elem.idx);
4802 const char *elem_name = elem_qualified + elem.name_offset;
4803 return name_cmp (elem_name, name) < 0;
4804 };
4805
4806 /* Comparison function object for upper_bound that matches against a
4807 given symbol name. */
4808 auto lookup_compare_upper = [&] (const char *name,
4809 const name_component &elem)
4810 {
4811 const char *elem_qualified = this->symbol_name_at (elem.idx);
4812 const char *elem_name = elem_qualified + elem.name_offset;
4813 return name_cmp (name, elem_name) < 0;
4814 };
4815
4816 auto begin = this->name_components.begin ();
4817 auto end = this->name_components.end ();
4818
4819 /* Find the lower bound. */
4820 auto lower = [&] ()
4821 {
4822 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4823 return begin;
4824 else
4825 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4826 } ();
4827
4828 /* Find the upper bound. */
4829 auto upper = [&] ()
4830 {
4831 if (lookup_name_without_params.completion_mode ())
4832 {
4833 /* In completion mode, we want UPPER to point past all
4834 symbols names that have the same prefix. I.e., with
4835 these symbols, and completing "func":
4836
4837 function << lower bound
4838 function1
4839 other_function << upper bound
4840
4841 We find the upper bound by looking for the insertion
4842 point of "func"-with-last-character-incremented,
4843 i.e. "fund". */
4844 std::string after = make_sort_after_prefix_name (cplus);
4845 if (after.empty ())
4846 return end;
4847 return std::lower_bound (lower, end, after.c_str (),
4848 lookup_compare_lower);
4849 }
4850 else
4851 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4852 } ();
4853
4854 return {lower, upper};
4855 }
4856
4857 /* See declaration. */
4858
4859 void
4860 mapped_index_base::build_name_components ()
4861 {
4862 if (!this->name_components.empty ())
4863 return;
4864
4865 this->name_components_casing = case_sensitivity;
4866 auto *name_cmp
4867 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4868
4869 /* The code below only knows how to break apart components of C++
4870 symbol names (and other languages that use '::' as
4871 namespace/module separator). If we add support for wild matching
4872 to some language that uses some other operator (E.g., Ada, Go and
4873 D use '.'), then we'll need to try splitting the symbol name
4874 according to that language too. Note that Ada does support wild
4875 matching, but doesn't currently support .gdb_index. */
4876 auto count = this->symbol_name_count ();
4877 for (offset_type idx = 0; idx < count; idx++)
4878 {
4879 if (this->symbol_name_slot_invalid (idx))
4880 continue;
4881
4882 const char *name = this->symbol_name_at (idx);
4883
4884 /* Add each name component to the name component table. */
4885 unsigned int previous_len = 0;
4886 for (unsigned int current_len = cp_find_first_component (name);
4887 name[current_len] != '\0';
4888 current_len += cp_find_first_component (name + current_len))
4889 {
4890 gdb_assert (name[current_len] == ':');
4891 this->name_components.push_back ({previous_len, idx});
4892 /* Skip the '::'. */
4893 current_len += 2;
4894 previous_len = current_len;
4895 }
4896 this->name_components.push_back ({previous_len, idx});
4897 }
4898
4899 /* Sort name_components elements by name. */
4900 auto name_comp_compare = [&] (const name_component &left,
4901 const name_component &right)
4902 {
4903 const char *left_qualified = this->symbol_name_at (left.idx);
4904 const char *right_qualified = this->symbol_name_at (right.idx);
4905
4906 const char *left_name = left_qualified + left.name_offset;
4907 const char *right_name = right_qualified + right.name_offset;
4908
4909 return name_cmp (left_name, right_name) < 0;
4910 };
4911
4912 std::sort (this->name_components.begin (),
4913 this->name_components.end (),
4914 name_comp_compare);
4915 }
4916
4917 /* Helper for dw2_expand_symtabs_matching that works with a
4918 mapped_index_base instead of the containing objfile. This is split
4919 to a separate function in order to be able to unit test the
4920 name_components matching using a mock mapped_index_base. For each
4921 symbol name that matches, calls MATCH_CALLBACK, passing it the
4922 symbol's index in the mapped_index_base symbol table. */
4923
4924 static void
4925 dw2_expand_symtabs_matching_symbol
4926 (mapped_index_base &index,
4927 const lookup_name_info &lookup_name_in,
4928 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4929 enum search_domain kind,
4930 gdb::function_view<void (offset_type)> match_callback)
4931 {
4932 lookup_name_info lookup_name_without_params
4933 = lookup_name_in.make_ignore_params ();
4934 gdb_index_symbol_name_matcher lookup_name_matcher
4935 (lookup_name_without_params);
4936
4937 /* Build the symbol name component sorted vector, if we haven't
4938 yet. */
4939 index.build_name_components ();
4940
4941 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4942
4943 /* Now for each symbol name in range, check to see if we have a name
4944 match, and if so, call the MATCH_CALLBACK callback. */
4945
4946 /* The same symbol may appear more than once in the range though.
4947 E.g., if we're looking for symbols that complete "w", and we have
4948 a symbol named "w1::w2", we'll find the two name components for
4949 that same symbol in the range. To be sure we only call the
4950 callback once per symbol, we first collect the symbol name
4951 indexes that matched in a temporary vector and ignore
4952 duplicates. */
4953 std::vector<offset_type> matches;
4954 matches.reserve (std::distance (bounds.first, bounds.second));
4955
4956 for (; bounds.first != bounds.second; ++bounds.first)
4957 {
4958 const char *qualified = index.symbol_name_at (bounds.first->idx);
4959
4960 if (!lookup_name_matcher.matches (qualified)
4961 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4962 continue;
4963
4964 matches.push_back (bounds.first->idx);
4965 }
4966
4967 std::sort (matches.begin (), matches.end ());
4968
4969 /* Finally call the callback, once per match. */
4970 ULONGEST prev = -1;
4971 for (offset_type idx : matches)
4972 {
4973 if (prev != idx)
4974 {
4975 match_callback (idx);
4976 prev = idx;
4977 }
4978 }
4979
4980 /* Above we use a type wider than idx's for 'prev', since 0 and
4981 (offset_type)-1 are both possible values. */
4982 static_assert (sizeof (prev) > sizeof (offset_type), "");
4983 }
4984
4985 #if GDB_SELF_TEST
4986
4987 namespace selftests { namespace dw2_expand_symtabs_matching {
4988
4989 /* A mock .gdb_index/.debug_names-like name index table, enough to
4990 exercise dw2_expand_symtabs_matching_symbol, which works with the
4991 mapped_index_base interface. Builds an index from the symbol list
4992 passed as parameter to the constructor. */
4993 class mock_mapped_index : public mapped_index_base
4994 {
4995 public:
4996 mock_mapped_index (gdb::array_view<const char *> symbols)
4997 : m_symbol_table (symbols)
4998 {}
4999
5000 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
5001
5002 /* Return the number of names in the symbol table. */
5003 virtual size_t symbol_name_count () const
5004 {
5005 return m_symbol_table.size ();
5006 }
5007
5008 /* Get the name of the symbol at IDX in the symbol table. */
5009 virtual const char *symbol_name_at (offset_type idx) const
5010 {
5011 return m_symbol_table[idx];
5012 }
5013
5014 private:
5015 gdb::array_view<const char *> m_symbol_table;
5016 };
5017
5018 /* Convenience function that converts a NULL pointer to a "<null>"
5019 string, to pass to print routines. */
5020
5021 static const char *
5022 string_or_null (const char *str)
5023 {
5024 return str != NULL ? str : "<null>";
5025 }
5026
5027 /* Check if a lookup_name_info built from
5028 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
5029 index. EXPECTED_LIST is the list of expected matches, in expected
5030 matching order. If no match expected, then an empty list is
5031 specified. Returns true on success. On failure prints a warning
5032 indicating the file:line that failed, and returns false. */
5033
5034 static bool
5035 check_match (const char *file, int line,
5036 mock_mapped_index &mock_index,
5037 const char *name, symbol_name_match_type match_type,
5038 bool completion_mode,
5039 std::initializer_list<const char *> expected_list)
5040 {
5041 lookup_name_info lookup_name (name, match_type, completion_mode);
5042
5043 bool matched = true;
5044
5045 auto mismatch = [&] (const char *expected_str,
5046 const char *got)
5047 {
5048 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
5049 "expected=\"%s\", got=\"%s\"\n"),
5050 file, line,
5051 (match_type == symbol_name_match_type::FULL
5052 ? "FULL" : "WILD"),
5053 name, string_or_null (expected_str), string_or_null (got));
5054 matched = false;
5055 };
5056
5057 auto expected_it = expected_list.begin ();
5058 auto expected_end = expected_list.end ();
5059
5060 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
5061 NULL, ALL_DOMAIN,
5062 [&] (offset_type idx)
5063 {
5064 const char *matched_name = mock_index.symbol_name_at (idx);
5065 const char *expected_str
5066 = expected_it == expected_end ? NULL : *expected_it++;
5067
5068 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
5069 mismatch (expected_str, matched_name);
5070 });
5071
5072 const char *expected_str
5073 = expected_it == expected_end ? NULL : *expected_it++;
5074 if (expected_str != NULL)
5075 mismatch (expected_str, NULL);
5076
5077 return matched;
5078 }
5079
5080 /* The symbols added to the mock mapped_index for testing (in
5081 canonical form). */
5082 static const char *test_symbols[] = {
5083 "function",
5084 "std::bar",
5085 "std::zfunction",
5086 "std::zfunction2",
5087 "w1::w2",
5088 "ns::foo<char*>",
5089 "ns::foo<int>",
5090 "ns::foo<long>",
5091 "ns2::tmpl<int>::foo2",
5092 "(anonymous namespace)::A::B::C",
5093
5094 /* These are used to check that the increment-last-char in the
5095 matching algorithm for completion doesn't match "t1_fund" when
5096 completing "t1_func". */
5097 "t1_func",
5098 "t1_func1",
5099 "t1_fund",
5100 "t1_fund1",
5101
5102 /* A UTF-8 name with multi-byte sequences to make sure that
5103 cp-name-parser understands this as a single identifier ("função"
5104 is "function" in PT). */
5105 u8"u8função",
5106
5107 /* \377 (0xff) is Latin1 'ÿ'. */
5108 "yfunc\377",
5109
5110 /* \377 (0xff) is Latin1 'ÿ'. */
5111 "\377",
5112 "\377\377123",
5113
5114 /* A name with all sorts of complications. Starts with "z" to make
5115 it easier for the completion tests below. */
5116 #define Z_SYM_NAME \
5117 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
5118 "::tuple<(anonymous namespace)::ui*, " \
5119 "std::default_delete<(anonymous namespace)::ui>, void>"
5120
5121 Z_SYM_NAME
5122 };
5123
5124 /* Returns true if the mapped_index_base::find_name_component_bounds
5125 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
5126 in completion mode. */
5127
5128 static bool
5129 check_find_bounds_finds (mapped_index_base &index,
5130 const char *search_name,
5131 gdb::array_view<const char *> expected_syms)
5132 {
5133 lookup_name_info lookup_name (search_name,
5134 symbol_name_match_type::FULL, true);
5135
5136 auto bounds = index.find_name_components_bounds (lookup_name);
5137
5138 size_t distance = std::distance (bounds.first, bounds.second);
5139 if (distance != expected_syms.size ())
5140 return false;
5141
5142 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
5143 {
5144 auto nc_elem = bounds.first + exp_elem;
5145 const char *qualified = index.symbol_name_at (nc_elem->idx);
5146 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
5147 return false;
5148 }
5149
5150 return true;
5151 }
5152
5153 /* Test the lower-level mapped_index::find_name_component_bounds
5154 method. */
5155
5156 static void
5157 test_mapped_index_find_name_component_bounds ()
5158 {
5159 mock_mapped_index mock_index (test_symbols);
5160
5161 mock_index.build_name_components ();
5162
5163 /* Test the lower-level mapped_index::find_name_component_bounds
5164 method in completion mode. */
5165 {
5166 static const char *expected_syms[] = {
5167 "t1_func",
5168 "t1_func1",
5169 };
5170
5171 SELF_CHECK (check_find_bounds_finds (mock_index,
5172 "t1_func", expected_syms));
5173 }
5174
5175 /* Check that the increment-last-char in the name matching algorithm
5176 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5177 {
5178 static const char *expected_syms1[] = {
5179 "\377",
5180 "\377\377123",
5181 };
5182 SELF_CHECK (check_find_bounds_finds (mock_index,
5183 "\377", expected_syms1));
5184
5185 static const char *expected_syms2[] = {
5186 "\377\377123",
5187 };
5188 SELF_CHECK (check_find_bounds_finds (mock_index,
5189 "\377\377", expected_syms2));
5190 }
5191 }
5192
5193 /* Test dw2_expand_symtabs_matching_symbol. */
5194
5195 static void
5196 test_dw2_expand_symtabs_matching_symbol ()
5197 {
5198 mock_mapped_index mock_index (test_symbols);
5199
5200 /* We let all tests run until the end even if some fails, for debug
5201 convenience. */
5202 bool any_mismatch = false;
5203
5204 /* Create the expected symbols list (an initializer_list). Needed
5205 because lists have commas, and we need to pass them to CHECK,
5206 which is a macro. */
5207 #define EXPECT(...) { __VA_ARGS__ }
5208
5209 /* Wrapper for check_match that passes down the current
5210 __FILE__/__LINE__. */
5211 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5212 any_mismatch |= !check_match (__FILE__, __LINE__, \
5213 mock_index, \
5214 NAME, MATCH_TYPE, COMPLETION_MODE, \
5215 EXPECTED_LIST)
5216
5217 /* Identity checks. */
5218 for (const char *sym : test_symbols)
5219 {
5220 /* Should be able to match all existing symbols. */
5221 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5222 EXPECT (sym));
5223
5224 /* Should be able to match all existing symbols with
5225 parameters. */
5226 std::string with_params = std::string (sym) + "(int)";
5227 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5228 EXPECT (sym));
5229
5230 /* Should be able to match all existing symbols with
5231 parameters and qualifiers. */
5232 with_params = std::string (sym) + " ( int ) const";
5233 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5234 EXPECT (sym));
5235
5236 /* This should really find sym, but cp-name-parser.y doesn't
5237 know about lvalue/rvalue qualifiers yet. */
5238 with_params = std::string (sym) + " ( int ) &&";
5239 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5240 {});
5241 }
5242
5243 /* Check that the name matching algorithm for completion doesn't get
5244 confused with Latin1 'ÿ' / 0xff. */
5245 {
5246 static const char str[] = "\377";
5247 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5248 EXPECT ("\377", "\377\377123"));
5249 }
5250
5251 /* Check that the increment-last-char in the matching algorithm for
5252 completion doesn't match "t1_fund" when completing "t1_func". */
5253 {
5254 static const char str[] = "t1_func";
5255 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5256 EXPECT ("t1_func", "t1_func1"));
5257 }
5258
5259 /* Check that completion mode works at each prefix of the expected
5260 symbol name. */
5261 {
5262 static const char str[] = "function(int)";
5263 size_t len = strlen (str);
5264 std::string lookup;
5265
5266 for (size_t i = 1; i < len; i++)
5267 {
5268 lookup.assign (str, i);
5269 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5270 EXPECT ("function"));
5271 }
5272 }
5273
5274 /* While "w" is a prefix of both components, the match function
5275 should still only be called once. */
5276 {
5277 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5278 EXPECT ("w1::w2"));
5279 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5280 EXPECT ("w1::w2"));
5281 }
5282
5283 /* Same, with a "complicated" symbol. */
5284 {
5285 static const char str[] = Z_SYM_NAME;
5286 size_t len = strlen (str);
5287 std::string lookup;
5288
5289 for (size_t i = 1; i < len; i++)
5290 {
5291 lookup.assign (str, i);
5292 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5293 EXPECT (Z_SYM_NAME));
5294 }
5295 }
5296
5297 /* In FULL mode, an incomplete symbol doesn't match. */
5298 {
5299 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5300 {});
5301 }
5302
5303 /* A complete symbol with parameters matches any overload, since the
5304 index has no overload info. */
5305 {
5306 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5307 EXPECT ("std::zfunction", "std::zfunction2"));
5308 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5309 EXPECT ("std::zfunction", "std::zfunction2"));
5310 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5311 EXPECT ("std::zfunction", "std::zfunction2"));
5312 }
5313
5314 /* Check that whitespace is ignored appropriately. A symbol with a
5315 template argument list. */
5316 {
5317 static const char expected[] = "ns::foo<int>";
5318 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5319 EXPECT (expected));
5320 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5321 EXPECT (expected));
5322 }
5323
5324 /* Check that whitespace is ignored appropriately. A symbol with a
5325 template argument list that includes a pointer. */
5326 {
5327 static const char expected[] = "ns::foo<char*>";
5328 /* Try both completion and non-completion modes. */
5329 static const bool completion_mode[2] = {false, true};
5330 for (size_t i = 0; i < 2; i++)
5331 {
5332 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5333 completion_mode[i], EXPECT (expected));
5334 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5335 completion_mode[i], EXPECT (expected));
5336
5337 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5338 completion_mode[i], EXPECT (expected));
5339 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5340 completion_mode[i], EXPECT (expected));
5341 }
5342 }
5343
5344 {
5345 /* Check method qualifiers are ignored. */
5346 static const char expected[] = "ns::foo<char*>";
5347 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5348 symbol_name_match_type::FULL, true, EXPECT (expected));
5349 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5350 symbol_name_match_type::FULL, true, EXPECT (expected));
5351 CHECK_MATCH ("foo < char * > ( int ) const",
5352 symbol_name_match_type::WILD, true, EXPECT (expected));
5353 CHECK_MATCH ("foo < char * > ( int ) &&",
5354 symbol_name_match_type::WILD, true, EXPECT (expected));
5355 }
5356
5357 /* Test lookup names that don't match anything. */
5358 {
5359 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5360 {});
5361
5362 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5363 {});
5364 }
5365
5366 /* Some wild matching tests, exercising "(anonymous namespace)",
5367 which should not be confused with a parameter list. */
5368 {
5369 static const char *syms[] = {
5370 "A::B::C",
5371 "B::C",
5372 "C",
5373 "A :: B :: C ( int )",
5374 "B :: C ( int )",
5375 "C ( int )",
5376 };
5377
5378 for (const char *s : syms)
5379 {
5380 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5381 EXPECT ("(anonymous namespace)::A::B::C"));
5382 }
5383 }
5384
5385 {
5386 static const char expected[] = "ns2::tmpl<int>::foo2";
5387 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5388 EXPECT (expected));
5389 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5390 EXPECT (expected));
5391 }
5392
5393 SELF_CHECK (!any_mismatch);
5394
5395 #undef EXPECT
5396 #undef CHECK_MATCH
5397 }
5398
5399 static void
5400 run_test ()
5401 {
5402 test_mapped_index_find_name_component_bounds ();
5403 test_dw2_expand_symtabs_matching_symbol ();
5404 }
5405
5406 }} // namespace selftests::dw2_expand_symtabs_matching
5407
5408 #endif /* GDB_SELF_TEST */
5409
5410 /* If FILE_MATCHER is NULL or if PER_CU has
5411 dwarf2_per_cu_quick_data::MARK set (see
5412 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5413 EXPANSION_NOTIFY on it. */
5414
5415 static void
5416 dw2_expand_symtabs_matching_one
5417 (struct dwarf2_per_cu_data *per_cu,
5418 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5419 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5420 {
5421 if (file_matcher == NULL || per_cu->v.quick->mark)
5422 {
5423 bool symtab_was_null
5424 = (per_cu->v.quick->compunit_symtab == NULL);
5425
5426 dw2_instantiate_symtab (per_cu);
5427
5428 if (expansion_notify != NULL
5429 && symtab_was_null
5430 && per_cu->v.quick->compunit_symtab != NULL)
5431 expansion_notify (per_cu->v.quick->compunit_symtab);
5432 }
5433 }
5434
5435 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5436 matched, to expand corresponding CUs that were marked. IDX is the
5437 index of the symbol name that matched. */
5438
5439 static void
5440 dw2_expand_marked_cus
5441 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5442 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5443 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5444 search_domain kind)
5445 {
5446 offset_type *vec, vec_len, vec_idx;
5447 bool global_seen = false;
5448 mapped_index &index = *dwarf2_per_objfile->index_table;
5449
5450 vec = (offset_type *) (index.constant_pool
5451 + MAYBE_SWAP (index.symbol_table[idx].vec));
5452 vec_len = MAYBE_SWAP (vec[0]);
5453 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5454 {
5455 struct dwarf2_per_cu_data *per_cu;
5456 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5457 /* This value is only valid for index versions >= 7. */
5458 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5459 gdb_index_symbol_kind symbol_kind =
5460 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5461 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5462 /* Only check the symbol attributes if they're present.
5463 Indices prior to version 7 don't record them,
5464 and indices >= 7 may elide them for certain symbols
5465 (gold does this). */
5466 int attrs_valid =
5467 (index.version >= 7
5468 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5469
5470 /* Work around gold/15646. */
5471 if (attrs_valid)
5472 {
5473 if (!is_static && global_seen)
5474 continue;
5475 if (!is_static)
5476 global_seen = true;
5477 }
5478
5479 /* Only check the symbol's kind if it has one. */
5480 if (attrs_valid)
5481 {
5482 switch (kind)
5483 {
5484 case VARIABLES_DOMAIN:
5485 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5486 continue;
5487 break;
5488 case FUNCTIONS_DOMAIN:
5489 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5490 continue;
5491 break;
5492 case TYPES_DOMAIN:
5493 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5494 continue;
5495 break;
5496 default:
5497 break;
5498 }
5499 }
5500
5501 /* Don't crash on bad data. */
5502 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5503 + dwarf2_per_objfile->n_type_units))
5504 {
5505 complaint (&symfile_complaints,
5506 _(".gdb_index entry has bad CU index"
5507 " [in module %s]"),
5508 objfile_name (dwarf2_per_objfile->objfile));
5509 continue;
5510 }
5511
5512 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
5513 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5514 expansion_notify);
5515 }
5516 }
5517
5518 /* If FILE_MATCHER is non-NULL, set all the
5519 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5520 that match FILE_MATCHER. */
5521
5522 static void
5523 dw_expand_symtabs_matching_file_matcher
5524 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5525 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5526 {
5527 if (file_matcher == NULL)
5528 return;
5529
5530 objfile *const objfile = dwarf2_per_objfile->objfile;
5531
5532 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5533 htab_eq_pointer,
5534 NULL, xcalloc, xfree));
5535 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5536 htab_eq_pointer,
5537 NULL, xcalloc, xfree));
5538
5539 /* The rule is CUs specify all the files, including those used by
5540 any TU, so there's no need to scan TUs here. */
5541
5542 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5543 {
5544 int j;
5545 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5546 struct quick_file_names *file_data;
5547 void **slot;
5548
5549 QUIT;
5550
5551 per_cu->v.quick->mark = 0;
5552
5553 /* We only need to look at symtabs not already expanded. */
5554 if (per_cu->v.quick->compunit_symtab)
5555 continue;
5556
5557 file_data = dw2_get_file_names (per_cu);
5558 if (file_data == NULL)
5559 continue;
5560
5561 if (htab_find (visited_not_found.get (), file_data) != NULL)
5562 continue;
5563 else if (htab_find (visited_found.get (), file_data) != NULL)
5564 {
5565 per_cu->v.quick->mark = 1;
5566 continue;
5567 }
5568
5569 for (j = 0; j < file_data->num_file_names; ++j)
5570 {
5571 const char *this_real_name;
5572
5573 if (file_matcher (file_data->file_names[j], false))
5574 {
5575 per_cu->v.quick->mark = 1;
5576 break;
5577 }
5578
5579 /* Before we invoke realpath, which can get expensive when many
5580 files are involved, do a quick comparison of the basenames. */
5581 if (!basenames_may_differ
5582 && !file_matcher (lbasename (file_data->file_names[j]),
5583 true))
5584 continue;
5585
5586 this_real_name = dw2_get_real_path (objfile, file_data, j);
5587 if (file_matcher (this_real_name, false))
5588 {
5589 per_cu->v.quick->mark = 1;
5590 break;
5591 }
5592 }
5593
5594 slot = htab_find_slot (per_cu->v.quick->mark
5595 ? visited_found.get ()
5596 : visited_not_found.get (),
5597 file_data, INSERT);
5598 *slot = file_data;
5599 }
5600 }
5601
5602 static void
5603 dw2_expand_symtabs_matching
5604 (struct objfile *objfile,
5605 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5606 const lookup_name_info &lookup_name,
5607 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5608 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5609 enum search_domain kind)
5610 {
5611 struct dwarf2_per_objfile *dwarf2_per_objfile
5612 = get_dwarf2_per_objfile (objfile);
5613
5614 /* index_table is NULL if OBJF_READNOW. */
5615 if (!dwarf2_per_objfile->index_table)
5616 return;
5617
5618 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5619
5620 mapped_index &index = *dwarf2_per_objfile->index_table;
5621
5622 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5623 symbol_matcher,
5624 kind, [&] (offset_type idx)
5625 {
5626 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5627 expansion_notify, kind);
5628 });
5629 }
5630
5631 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5632 symtab. */
5633
5634 static struct compunit_symtab *
5635 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5636 CORE_ADDR pc)
5637 {
5638 int i;
5639
5640 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5641 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5642 return cust;
5643
5644 if (cust->includes == NULL)
5645 return NULL;
5646
5647 for (i = 0; cust->includes[i]; ++i)
5648 {
5649 struct compunit_symtab *s = cust->includes[i];
5650
5651 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5652 if (s != NULL)
5653 return s;
5654 }
5655
5656 return NULL;
5657 }
5658
5659 static struct compunit_symtab *
5660 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5661 struct bound_minimal_symbol msymbol,
5662 CORE_ADDR pc,
5663 struct obj_section *section,
5664 int warn_if_readin)
5665 {
5666 struct dwarf2_per_cu_data *data;
5667 struct compunit_symtab *result;
5668
5669 if (!objfile->psymtabs_addrmap)
5670 return NULL;
5671
5672 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5673 pc);
5674 if (!data)
5675 return NULL;
5676
5677 if (warn_if_readin && data->v.quick->compunit_symtab)
5678 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5679 paddress (get_objfile_arch (objfile), pc));
5680
5681 result
5682 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5683 pc);
5684 gdb_assert (result != NULL);
5685 return result;
5686 }
5687
5688 static void
5689 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5690 void *data, int need_fullname)
5691 {
5692 struct dwarf2_per_objfile *dwarf2_per_objfile
5693 = get_dwarf2_per_objfile (objfile);
5694
5695 if (!dwarf2_per_objfile->filenames_cache)
5696 {
5697 dwarf2_per_objfile->filenames_cache.emplace ();
5698
5699 htab_up visited (htab_create_alloc (10,
5700 htab_hash_pointer, htab_eq_pointer,
5701 NULL, xcalloc, xfree));
5702
5703 /* The rule is CUs specify all the files, including those used
5704 by any TU, so there's no need to scan TUs here. We can
5705 ignore file names coming from already-expanded CUs. */
5706
5707 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5708 {
5709 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
5710
5711 if (per_cu->v.quick->compunit_symtab)
5712 {
5713 void **slot = htab_find_slot (visited.get (),
5714 per_cu->v.quick->file_names,
5715 INSERT);
5716
5717 *slot = per_cu->v.quick->file_names;
5718 }
5719 }
5720
5721 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5722 {
5723 dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5724 struct quick_file_names *file_data;
5725 void **slot;
5726
5727 /* We only need to look at symtabs not already expanded. */
5728 if (per_cu->v.quick->compunit_symtab)
5729 continue;
5730
5731 file_data = dw2_get_file_names (per_cu);
5732 if (file_data == NULL)
5733 continue;
5734
5735 slot = htab_find_slot (visited.get (), file_data, INSERT);
5736 if (*slot)
5737 {
5738 /* Already visited. */
5739 continue;
5740 }
5741 *slot = file_data;
5742
5743 for (int j = 0; j < file_data->num_file_names; ++j)
5744 {
5745 const char *filename = file_data->file_names[j];
5746 dwarf2_per_objfile->filenames_cache->seen (filename);
5747 }
5748 }
5749 }
5750
5751 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5752 {
5753 gdb::unique_xmalloc_ptr<char> this_real_name;
5754
5755 if (need_fullname)
5756 this_real_name = gdb_realpath (filename);
5757 (*fun) (filename, this_real_name.get (), data);
5758 });
5759 }
5760
5761 static int
5762 dw2_has_symbols (struct objfile *objfile)
5763 {
5764 return 1;
5765 }
5766
5767 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5768 {
5769 dw2_has_symbols,
5770 dw2_find_last_source_symtab,
5771 dw2_forget_cached_source_info,
5772 dw2_map_symtabs_matching_filename,
5773 dw2_lookup_symbol,
5774 dw2_print_stats,
5775 dw2_dump,
5776 dw2_relocate,
5777 dw2_expand_symtabs_for_function,
5778 dw2_expand_all_symtabs,
5779 dw2_expand_symtabs_with_fullname,
5780 dw2_map_matching_symbols,
5781 dw2_expand_symtabs_matching,
5782 dw2_find_pc_sect_compunit_symtab,
5783 NULL,
5784 dw2_map_symbol_filenames
5785 };
5786
5787 /* DWARF-5 debug_names reader. */
5788
5789 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5790 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5791
5792 /* A helper function that reads the .debug_names section in SECTION
5793 and fills in MAP. FILENAME is the name of the file containing the
5794 section; it is used for error reporting.
5795
5796 Returns true if all went well, false otherwise. */
5797
5798 static bool
5799 read_debug_names_from_section (struct objfile *objfile,
5800 const char *filename,
5801 struct dwarf2_section_info *section,
5802 mapped_debug_names &map)
5803 {
5804 if (dwarf2_section_empty_p (section))
5805 return false;
5806
5807 /* Older elfutils strip versions could keep the section in the main
5808 executable while splitting it for the separate debug info file. */
5809 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5810 return false;
5811
5812 dwarf2_read_section (objfile, section);
5813
5814 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5815
5816 const gdb_byte *addr = section->buffer;
5817
5818 bfd *const abfd = get_section_bfd_owner (section);
5819
5820 unsigned int bytes_read;
5821 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5822 addr += bytes_read;
5823
5824 map.dwarf5_is_dwarf64 = bytes_read != 4;
5825 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5826 if (bytes_read + length != section->size)
5827 {
5828 /* There may be multiple per-CU indices. */
5829 warning (_("Section .debug_names in %s length %s does not match "
5830 "section length %s, ignoring .debug_names."),
5831 filename, plongest (bytes_read + length),
5832 pulongest (section->size));
5833 return false;
5834 }
5835
5836 /* The version number. */
5837 uint16_t version = read_2_bytes (abfd, addr);
5838 addr += 2;
5839 if (version != 5)
5840 {
5841 warning (_("Section .debug_names in %s has unsupported version %d, "
5842 "ignoring .debug_names."),
5843 filename, version);
5844 return false;
5845 }
5846
5847 /* Padding. */
5848 uint16_t padding = read_2_bytes (abfd, addr);
5849 addr += 2;
5850 if (padding != 0)
5851 {
5852 warning (_("Section .debug_names in %s has unsupported padding %d, "
5853 "ignoring .debug_names."),
5854 filename, padding);
5855 return false;
5856 }
5857
5858 /* comp_unit_count - The number of CUs in the CU list. */
5859 map.cu_count = read_4_bytes (abfd, addr);
5860 addr += 4;
5861
5862 /* local_type_unit_count - The number of TUs in the local TU
5863 list. */
5864 map.tu_count = read_4_bytes (abfd, addr);
5865 addr += 4;
5866
5867 /* foreign_type_unit_count - The number of TUs in the foreign TU
5868 list. */
5869 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5870 addr += 4;
5871 if (foreign_tu_count != 0)
5872 {
5873 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5874 "ignoring .debug_names."),
5875 filename, static_cast<unsigned long> (foreign_tu_count));
5876 return false;
5877 }
5878
5879 /* bucket_count - The number of hash buckets in the hash lookup
5880 table. */
5881 map.bucket_count = read_4_bytes (abfd, addr);
5882 addr += 4;
5883
5884 /* name_count - The number of unique names in the index. */
5885 map.name_count = read_4_bytes (abfd, addr);
5886 addr += 4;
5887
5888 /* abbrev_table_size - The size in bytes of the abbreviations
5889 table. */
5890 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5891 addr += 4;
5892
5893 /* augmentation_string_size - The size in bytes of the augmentation
5894 string. This value is rounded up to a multiple of 4. */
5895 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5896 addr += 4;
5897 map.augmentation_is_gdb = ((augmentation_string_size
5898 == sizeof (dwarf5_augmentation))
5899 && memcmp (addr, dwarf5_augmentation,
5900 sizeof (dwarf5_augmentation)) == 0);
5901 augmentation_string_size += (-augmentation_string_size) & 3;
5902 addr += augmentation_string_size;
5903
5904 /* List of CUs */
5905 map.cu_table_reordered = addr;
5906 addr += map.cu_count * map.offset_size;
5907
5908 /* List of Local TUs */
5909 map.tu_table_reordered = addr;
5910 addr += map.tu_count * map.offset_size;
5911
5912 /* Hash Lookup Table */
5913 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5914 addr += map.bucket_count * 4;
5915 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5916 addr += map.name_count * 4;
5917
5918 /* Name Table */
5919 map.name_table_string_offs_reordered = addr;
5920 addr += map.name_count * map.offset_size;
5921 map.name_table_entry_offs_reordered = addr;
5922 addr += map.name_count * map.offset_size;
5923
5924 const gdb_byte *abbrev_table_start = addr;
5925 for (;;)
5926 {
5927 unsigned int bytes_read;
5928 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5929 addr += bytes_read;
5930 if (index_num == 0)
5931 break;
5932
5933 const auto insertpair
5934 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5935 if (!insertpair.second)
5936 {
5937 warning (_("Section .debug_names in %s has duplicate index %s, "
5938 "ignoring .debug_names."),
5939 filename, pulongest (index_num));
5940 return false;
5941 }
5942 mapped_debug_names::index_val &indexval = insertpair.first->second;
5943 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5944 addr += bytes_read;
5945
5946 for (;;)
5947 {
5948 mapped_debug_names::index_val::attr attr;
5949 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5950 addr += bytes_read;
5951 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5952 addr += bytes_read;
5953 if (attr.form == DW_FORM_implicit_const)
5954 {
5955 attr.implicit_const = read_signed_leb128 (abfd, addr,
5956 &bytes_read);
5957 addr += bytes_read;
5958 }
5959 if (attr.dw_idx == 0 && attr.form == 0)
5960 break;
5961 indexval.attr_vec.push_back (std::move (attr));
5962 }
5963 }
5964 if (addr != abbrev_table_start + abbrev_table_size)
5965 {
5966 warning (_("Section .debug_names in %s has abbreviation_table "
5967 "of size %zu vs. written as %u, ignoring .debug_names."),
5968 filename, addr - abbrev_table_start, abbrev_table_size);
5969 return false;
5970 }
5971 map.entry_pool = addr;
5972
5973 return true;
5974 }
5975
5976 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5977 list. */
5978
5979 static void
5980 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5981 const mapped_debug_names &map,
5982 dwarf2_section_info &section,
5983 bool is_dwz, int base_offset)
5984 {
5985 sect_offset sect_off_prev;
5986 for (uint32_t i = 0; i <= map.cu_count; ++i)
5987 {
5988 sect_offset sect_off_next;
5989 if (i < map.cu_count)
5990 {
5991 sect_off_next
5992 = (sect_offset) (extract_unsigned_integer
5993 (map.cu_table_reordered + i * map.offset_size,
5994 map.offset_size,
5995 map.dwarf5_byte_order));
5996 }
5997 else
5998 sect_off_next = (sect_offset) section.size;
5999 if (i >= 1)
6000 {
6001 const ULONGEST length = sect_off_next - sect_off_prev;
6002 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
6003 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
6004 sect_off_prev, length);
6005 }
6006 sect_off_prev = sect_off_next;
6007 }
6008 }
6009
6010 /* Read the CU list from the mapped index, and use it to create all
6011 the CU objects for this dwarf2_per_objfile. */
6012
6013 static void
6014 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
6015 const mapped_debug_names &map,
6016 const mapped_debug_names &dwz_map)
6017 {
6018 struct objfile *objfile = dwarf2_per_objfile->objfile;
6019
6020 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
6021 dwarf2_per_objfile->all_comp_units
6022 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
6023 dwarf2_per_objfile->n_comp_units);
6024
6025 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
6026 dwarf2_per_objfile->info,
6027 false /* is_dwz */,
6028 0 /* base_offset */);
6029
6030 if (dwz_map.cu_count == 0)
6031 return;
6032
6033 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
6034 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
6035 true /* is_dwz */,
6036 map.cu_count /* base_offset */);
6037 }
6038
6039 /* Read .debug_names. If everything went ok, initialize the "quick"
6040 elements of all the CUs and return true. Otherwise, return false. */
6041
6042 static bool
6043 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
6044 {
6045 mapped_debug_names local_map (dwarf2_per_objfile);
6046 mapped_debug_names dwz_map (dwarf2_per_objfile);
6047 struct objfile *objfile = dwarf2_per_objfile->objfile;
6048
6049 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
6050 &dwarf2_per_objfile->debug_names,
6051 local_map))
6052 return false;
6053
6054 /* Don't use the index if it's empty. */
6055 if (local_map.name_count == 0)
6056 return false;
6057
6058 /* If there is a .dwz file, read it so we can get its CU list as
6059 well. */
6060 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
6061 if (dwz != NULL)
6062 {
6063 if (!read_debug_names_from_section (objfile,
6064 bfd_get_filename (dwz->dwz_bfd),
6065 &dwz->debug_names, dwz_map))
6066 {
6067 warning (_("could not read '.debug_names' section from %s; skipping"),
6068 bfd_get_filename (dwz->dwz_bfd));
6069 return false;
6070 }
6071 }
6072
6073 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
6074
6075 if (local_map.tu_count != 0)
6076 {
6077 /* We can only handle a single .debug_types when we have an
6078 index. */
6079 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
6080 return false;
6081
6082 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
6083 dwarf2_per_objfile->types, 0);
6084
6085 create_signatured_type_table_from_debug_names
6086 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
6087 }
6088
6089 create_addrmap_from_aranges (dwarf2_per_objfile,
6090 &dwarf2_per_objfile->debug_aranges);
6091
6092 dwarf2_per_objfile->debug_names_table.reset
6093 (new mapped_debug_names (dwarf2_per_objfile));
6094 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
6095 dwarf2_per_objfile->using_index = 1;
6096 dwarf2_per_objfile->quick_file_names_table =
6097 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6098
6099 return true;
6100 }
6101
6102 /* Symbol name hashing function as specified by DWARF-5. */
6103
6104 static uint32_t
6105 dwarf5_djb_hash (const char *str_)
6106 {
6107 const unsigned char *str = (const unsigned char *) str_;
6108
6109 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
6110 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
6111
6112 uint32_t hash = 5381;
6113 while (int c = *str++)
6114 hash = hash * 33 + tolower (c);
6115 return hash;
6116 }
6117
6118 /* Type used to manage iterating over all CUs looking for a symbol for
6119 .debug_names. */
6120
6121 class dw2_debug_names_iterator
6122 {
6123 public:
6124 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
6125 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
6126 dw2_debug_names_iterator (const mapped_debug_names &map,
6127 bool want_specific_block,
6128 block_enum block_index, domain_enum domain,
6129 const char *name)
6130 : m_map (map), m_want_specific_block (want_specific_block),
6131 m_block_index (block_index), m_domain (domain),
6132 m_addr (find_vec_in_debug_names (map, name))
6133 {}
6134
6135 dw2_debug_names_iterator (const mapped_debug_names &map,
6136 search_domain search, uint32_t namei)
6137 : m_map (map),
6138 m_search (search),
6139 m_addr (find_vec_in_debug_names (map, namei))
6140 {}
6141
6142 /* Return the next matching CU or NULL if there are no more. */
6143 dwarf2_per_cu_data *next ();
6144
6145 private:
6146 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6147 const char *name);
6148 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6149 uint32_t namei);
6150
6151 /* The internalized form of .debug_names. */
6152 const mapped_debug_names &m_map;
6153
6154 /* If true, only look for symbols that match BLOCK_INDEX. */
6155 const bool m_want_specific_block = false;
6156
6157 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
6158 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
6159 value. */
6160 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
6161
6162 /* The kind of symbol we're looking for. */
6163 const domain_enum m_domain = UNDEF_DOMAIN;
6164 const search_domain m_search = ALL_DOMAIN;
6165
6166 /* The list of CUs from the index entry of the symbol, or NULL if
6167 not found. */
6168 const gdb_byte *m_addr;
6169 };
6170
6171 const char *
6172 mapped_debug_names::namei_to_name (uint32_t namei) const
6173 {
6174 const ULONGEST namei_string_offs
6175 = extract_unsigned_integer ((name_table_string_offs_reordered
6176 + namei * offset_size),
6177 offset_size,
6178 dwarf5_byte_order);
6179 return read_indirect_string_at_offset
6180 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
6181 }
6182
6183 /* Find a slot in .debug_names for the object named NAME. If NAME is
6184 found, return pointer to its pool data. If NAME cannot be found,
6185 return NULL. */
6186
6187 const gdb_byte *
6188 dw2_debug_names_iterator::find_vec_in_debug_names
6189 (const mapped_debug_names &map, const char *name)
6190 {
6191 int (*cmp) (const char *, const char *);
6192
6193 if (current_language->la_language == language_cplus
6194 || current_language->la_language == language_fortran
6195 || current_language->la_language == language_d)
6196 {
6197 /* NAME is already canonical. Drop any qualifiers as
6198 .debug_names does not contain any. */
6199
6200 if (strchr (name, '(') != NULL)
6201 {
6202 gdb::unique_xmalloc_ptr<char> without_params
6203 = cp_remove_params (name);
6204
6205 if (without_params != NULL)
6206 {
6207 name = without_params.get();
6208 }
6209 }
6210 }
6211
6212 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6213
6214 const uint32_t full_hash = dwarf5_djb_hash (name);
6215 uint32_t namei
6216 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6217 (map.bucket_table_reordered
6218 + (full_hash % map.bucket_count)), 4,
6219 map.dwarf5_byte_order);
6220 if (namei == 0)
6221 return NULL;
6222 --namei;
6223 if (namei >= map.name_count)
6224 {
6225 complaint (&symfile_complaints,
6226 _("Wrong .debug_names with name index %u but name_count=%u "
6227 "[in module %s]"),
6228 namei, map.name_count,
6229 objfile_name (map.dwarf2_per_objfile->objfile));
6230 return NULL;
6231 }
6232
6233 for (;;)
6234 {
6235 const uint32_t namei_full_hash
6236 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6237 (map.hash_table_reordered + namei), 4,
6238 map.dwarf5_byte_order);
6239 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6240 return NULL;
6241
6242 if (full_hash == namei_full_hash)
6243 {
6244 const char *const namei_string = map.namei_to_name (namei);
6245
6246 #if 0 /* An expensive sanity check. */
6247 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6248 {
6249 complaint (&symfile_complaints,
6250 _("Wrong .debug_names hash for string at index %u "
6251 "[in module %s]"),
6252 namei, objfile_name (dwarf2_per_objfile->objfile));
6253 return NULL;
6254 }
6255 #endif
6256
6257 if (cmp (namei_string, name) == 0)
6258 {
6259 const ULONGEST namei_entry_offs
6260 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6261 + namei * map.offset_size),
6262 map.offset_size, map.dwarf5_byte_order);
6263 return map.entry_pool + namei_entry_offs;
6264 }
6265 }
6266
6267 ++namei;
6268 if (namei >= map.name_count)
6269 return NULL;
6270 }
6271 }
6272
6273 const gdb_byte *
6274 dw2_debug_names_iterator::find_vec_in_debug_names
6275 (const mapped_debug_names &map, uint32_t namei)
6276 {
6277 if (namei >= map.name_count)
6278 {
6279 complaint (&symfile_complaints,
6280 _("Wrong .debug_names with name index %u but name_count=%u "
6281 "[in module %s]"),
6282 namei, map.name_count,
6283 objfile_name (map.dwarf2_per_objfile->objfile));
6284 return NULL;
6285 }
6286
6287 const ULONGEST namei_entry_offs
6288 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6289 + namei * map.offset_size),
6290 map.offset_size, map.dwarf5_byte_order);
6291 return map.entry_pool + namei_entry_offs;
6292 }
6293
6294 /* See dw2_debug_names_iterator. */
6295
6296 dwarf2_per_cu_data *
6297 dw2_debug_names_iterator::next ()
6298 {
6299 if (m_addr == NULL)
6300 return NULL;
6301
6302 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
6303 struct objfile *objfile = dwarf2_per_objfile->objfile;
6304 bfd *const abfd = objfile->obfd;
6305
6306 again:
6307
6308 unsigned int bytes_read;
6309 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6310 m_addr += bytes_read;
6311 if (abbrev == 0)
6312 return NULL;
6313
6314 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6315 if (indexval_it == m_map.abbrev_map.cend ())
6316 {
6317 complaint (&symfile_complaints,
6318 _("Wrong .debug_names undefined abbrev code %s "
6319 "[in module %s]"),
6320 pulongest (abbrev), objfile_name (objfile));
6321 return NULL;
6322 }
6323 const mapped_debug_names::index_val &indexval = indexval_it->second;
6324 bool have_is_static = false;
6325 bool is_static;
6326 dwarf2_per_cu_data *per_cu = NULL;
6327 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6328 {
6329 ULONGEST ull;
6330 switch (attr.form)
6331 {
6332 case DW_FORM_implicit_const:
6333 ull = attr.implicit_const;
6334 break;
6335 case DW_FORM_flag_present:
6336 ull = 1;
6337 break;
6338 case DW_FORM_udata:
6339 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6340 m_addr += bytes_read;
6341 break;
6342 default:
6343 complaint (&symfile_complaints,
6344 _("Unsupported .debug_names form %s [in module %s]"),
6345 dwarf_form_name (attr.form),
6346 objfile_name (objfile));
6347 return NULL;
6348 }
6349 switch (attr.dw_idx)
6350 {
6351 case DW_IDX_compile_unit:
6352 /* Don't crash on bad data. */
6353 if (ull >= dwarf2_per_objfile->n_comp_units)
6354 {
6355 complaint (&symfile_complaints,
6356 _(".debug_names entry has bad CU index %s"
6357 " [in module %s]"),
6358 pulongest (ull),
6359 objfile_name (dwarf2_per_objfile->objfile));
6360 continue;
6361 }
6362 per_cu = dw2_get_cutu (dwarf2_per_objfile, ull);
6363 break;
6364 case DW_IDX_type_unit:
6365 /* Don't crash on bad data. */
6366 if (ull >= dwarf2_per_objfile->n_type_units)
6367 {
6368 complaint (&symfile_complaints,
6369 _(".debug_names entry has bad TU index %s"
6370 " [in module %s]"),
6371 pulongest (ull),
6372 objfile_name (dwarf2_per_objfile->objfile));
6373 continue;
6374 }
6375 per_cu = dw2_get_cutu (dwarf2_per_objfile,
6376 dwarf2_per_objfile->n_comp_units + ull);
6377 break;
6378 case DW_IDX_GNU_internal:
6379 if (!m_map.augmentation_is_gdb)
6380 break;
6381 have_is_static = true;
6382 is_static = true;
6383 break;
6384 case DW_IDX_GNU_external:
6385 if (!m_map.augmentation_is_gdb)
6386 break;
6387 have_is_static = true;
6388 is_static = false;
6389 break;
6390 }
6391 }
6392
6393 /* Skip if already read in. */
6394 if (per_cu->v.quick->compunit_symtab)
6395 goto again;
6396
6397 /* Check static vs global. */
6398 if (have_is_static)
6399 {
6400 const bool want_static = m_block_index != GLOBAL_BLOCK;
6401 if (m_want_specific_block && want_static != is_static)
6402 goto again;
6403 }
6404
6405 /* Match dw2_symtab_iter_next, symbol_kind
6406 and debug_names::psymbol_tag. */
6407 switch (m_domain)
6408 {
6409 case VAR_DOMAIN:
6410 switch (indexval.dwarf_tag)
6411 {
6412 case DW_TAG_variable:
6413 case DW_TAG_subprogram:
6414 /* Some types are also in VAR_DOMAIN. */
6415 case DW_TAG_typedef:
6416 case DW_TAG_structure_type:
6417 break;
6418 default:
6419 goto again;
6420 }
6421 break;
6422 case STRUCT_DOMAIN:
6423 switch (indexval.dwarf_tag)
6424 {
6425 case DW_TAG_typedef:
6426 case DW_TAG_structure_type:
6427 break;
6428 default:
6429 goto again;
6430 }
6431 break;
6432 case LABEL_DOMAIN:
6433 switch (indexval.dwarf_tag)
6434 {
6435 case 0:
6436 case DW_TAG_variable:
6437 break;
6438 default:
6439 goto again;
6440 }
6441 break;
6442 default:
6443 break;
6444 }
6445
6446 /* Match dw2_expand_symtabs_matching, symbol_kind and
6447 debug_names::psymbol_tag. */
6448 switch (m_search)
6449 {
6450 case VARIABLES_DOMAIN:
6451 switch (indexval.dwarf_tag)
6452 {
6453 case DW_TAG_variable:
6454 break;
6455 default:
6456 goto again;
6457 }
6458 break;
6459 case FUNCTIONS_DOMAIN:
6460 switch (indexval.dwarf_tag)
6461 {
6462 case DW_TAG_subprogram:
6463 break;
6464 default:
6465 goto again;
6466 }
6467 break;
6468 case TYPES_DOMAIN:
6469 switch (indexval.dwarf_tag)
6470 {
6471 case DW_TAG_typedef:
6472 case DW_TAG_structure_type:
6473 break;
6474 default:
6475 goto again;
6476 }
6477 break;
6478 default:
6479 break;
6480 }
6481
6482 return per_cu;
6483 }
6484
6485 static struct compunit_symtab *
6486 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6487 const char *name, domain_enum domain)
6488 {
6489 const block_enum block_index = static_cast<block_enum> (block_index_int);
6490 struct dwarf2_per_objfile *dwarf2_per_objfile
6491 = get_dwarf2_per_objfile (objfile);
6492
6493 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6494 if (!mapp)
6495 {
6496 /* index is NULL if OBJF_READNOW. */
6497 return NULL;
6498 }
6499 const auto &map = *mapp;
6500
6501 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6502 block_index, domain, name);
6503
6504 struct compunit_symtab *stab_best = NULL;
6505 struct dwarf2_per_cu_data *per_cu;
6506 while ((per_cu = iter.next ()) != NULL)
6507 {
6508 struct symbol *sym, *with_opaque = NULL;
6509 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6510 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6511 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6512
6513 sym = block_find_symbol (block, name, domain,
6514 block_find_non_opaque_type_preferred,
6515 &with_opaque);
6516
6517 /* Some caution must be observed with overloaded functions and
6518 methods, since the index will not contain any overload
6519 information (but NAME might contain it). */
6520
6521 if (sym != NULL
6522 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6523 return stab;
6524 if (with_opaque != NULL
6525 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6526 stab_best = stab;
6527
6528 /* Keep looking through other CUs. */
6529 }
6530
6531 return stab_best;
6532 }
6533
6534 /* This dumps minimal information about .debug_names. It is called
6535 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6536 uses this to verify that .debug_names has been loaded. */
6537
6538 static void
6539 dw2_debug_names_dump (struct objfile *objfile)
6540 {
6541 struct dwarf2_per_objfile *dwarf2_per_objfile
6542 = get_dwarf2_per_objfile (objfile);
6543
6544 gdb_assert (dwarf2_per_objfile->using_index);
6545 printf_filtered (".debug_names:");
6546 if (dwarf2_per_objfile->debug_names_table)
6547 printf_filtered (" exists\n");
6548 else
6549 printf_filtered (" faked for \"readnow\"\n");
6550 printf_filtered ("\n");
6551 }
6552
6553 static void
6554 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6555 const char *func_name)
6556 {
6557 struct dwarf2_per_objfile *dwarf2_per_objfile
6558 = get_dwarf2_per_objfile (objfile);
6559
6560 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6561 if (dwarf2_per_objfile->debug_names_table)
6562 {
6563 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6564
6565 /* Note: It doesn't matter what we pass for block_index here. */
6566 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6567 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6568
6569 struct dwarf2_per_cu_data *per_cu;
6570 while ((per_cu = iter.next ()) != NULL)
6571 dw2_instantiate_symtab (per_cu);
6572 }
6573 }
6574
6575 static void
6576 dw2_debug_names_expand_symtabs_matching
6577 (struct objfile *objfile,
6578 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6579 const lookup_name_info &lookup_name,
6580 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6581 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6582 enum search_domain kind)
6583 {
6584 struct dwarf2_per_objfile *dwarf2_per_objfile
6585 = get_dwarf2_per_objfile (objfile);
6586
6587 /* debug_names_table is NULL if OBJF_READNOW. */
6588 if (!dwarf2_per_objfile->debug_names_table)
6589 return;
6590
6591 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6592
6593 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6594
6595 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6596 symbol_matcher,
6597 kind, [&] (offset_type namei)
6598 {
6599 /* The name was matched, now expand corresponding CUs that were
6600 marked. */
6601 dw2_debug_names_iterator iter (map, kind, namei);
6602
6603 struct dwarf2_per_cu_data *per_cu;
6604 while ((per_cu = iter.next ()) != NULL)
6605 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6606 expansion_notify);
6607 });
6608 }
6609
6610 const struct quick_symbol_functions dwarf2_debug_names_functions =
6611 {
6612 dw2_has_symbols,
6613 dw2_find_last_source_symtab,
6614 dw2_forget_cached_source_info,
6615 dw2_map_symtabs_matching_filename,
6616 dw2_debug_names_lookup_symbol,
6617 dw2_print_stats,
6618 dw2_debug_names_dump,
6619 dw2_relocate,
6620 dw2_debug_names_expand_symtabs_for_function,
6621 dw2_expand_all_symtabs,
6622 dw2_expand_symtabs_with_fullname,
6623 dw2_map_matching_symbols,
6624 dw2_debug_names_expand_symtabs_matching,
6625 dw2_find_pc_sect_compunit_symtab,
6626 NULL,
6627 dw2_map_symbol_filenames
6628 };
6629
6630 /* See symfile.h. */
6631
6632 bool
6633 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6634 {
6635 struct dwarf2_per_objfile *dwarf2_per_objfile
6636 = get_dwarf2_per_objfile (objfile);
6637
6638 /* If we're about to read full symbols, don't bother with the
6639 indices. In this case we also don't care if some other debug
6640 format is making psymtabs, because they are all about to be
6641 expanded anyway. */
6642 if ((objfile->flags & OBJF_READNOW))
6643 {
6644 int i;
6645
6646 dwarf2_per_objfile->using_index = 1;
6647 create_all_comp_units (dwarf2_per_objfile);
6648 create_all_type_units (dwarf2_per_objfile);
6649 dwarf2_per_objfile->quick_file_names_table =
6650 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6651
6652 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
6653 + dwarf2_per_objfile->n_type_units); ++i)
6654 {
6655 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
6656
6657 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6658 struct dwarf2_per_cu_quick_data);
6659 }
6660
6661 /* Return 1 so that gdb sees the "quick" functions. However,
6662 these functions will be no-ops because we will have expanded
6663 all symtabs. */
6664 *index_kind = dw_index_kind::GDB_INDEX;
6665 return true;
6666 }
6667
6668 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6669 {
6670 *index_kind = dw_index_kind::DEBUG_NAMES;
6671 return true;
6672 }
6673
6674 if (dwarf2_read_index (objfile))
6675 {
6676 *index_kind = dw_index_kind::GDB_INDEX;
6677 return true;
6678 }
6679
6680 return false;
6681 }
6682
6683 \f
6684
6685 /* Build a partial symbol table. */
6686
6687 void
6688 dwarf2_build_psymtabs (struct objfile *objfile)
6689 {
6690 struct dwarf2_per_objfile *dwarf2_per_objfile
6691 = get_dwarf2_per_objfile (objfile);
6692
6693 if (objfile->global_psymbols.capacity () == 0
6694 && objfile->static_psymbols.capacity () == 0)
6695 init_psymbol_list (objfile, 1024);
6696
6697 TRY
6698 {
6699 /* This isn't really ideal: all the data we allocate on the
6700 objfile's obstack is still uselessly kept around. However,
6701 freeing it seems unsafe. */
6702 psymtab_discarder psymtabs (objfile);
6703 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6704 psymtabs.keep ();
6705 }
6706 CATCH (except, RETURN_MASK_ERROR)
6707 {
6708 exception_print (gdb_stderr, except);
6709 }
6710 END_CATCH
6711 }
6712
6713 /* Return the total length of the CU described by HEADER. */
6714
6715 static unsigned int
6716 get_cu_length (const struct comp_unit_head *header)
6717 {
6718 return header->initial_length_size + header->length;
6719 }
6720
6721 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6722
6723 static inline bool
6724 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6725 {
6726 sect_offset bottom = cu_header->sect_off;
6727 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6728
6729 return sect_off >= bottom && sect_off < top;
6730 }
6731
6732 /* Find the base address of the compilation unit for range lists and
6733 location lists. It will normally be specified by DW_AT_low_pc.
6734 In DWARF-3 draft 4, the base address could be overridden by
6735 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6736 compilation units with discontinuous ranges. */
6737
6738 static void
6739 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6740 {
6741 struct attribute *attr;
6742
6743 cu->base_known = 0;
6744 cu->base_address = 0;
6745
6746 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6747 if (attr)
6748 {
6749 cu->base_address = attr_value_as_address (attr);
6750 cu->base_known = 1;
6751 }
6752 else
6753 {
6754 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6755 if (attr)
6756 {
6757 cu->base_address = attr_value_as_address (attr);
6758 cu->base_known = 1;
6759 }
6760 }
6761 }
6762
6763 /* Read in the comp unit header information from the debug_info at info_ptr.
6764 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6765 NOTE: This leaves members offset, first_die_offset to be filled in
6766 by the caller. */
6767
6768 static const gdb_byte *
6769 read_comp_unit_head (struct comp_unit_head *cu_header,
6770 const gdb_byte *info_ptr,
6771 struct dwarf2_section_info *section,
6772 rcuh_kind section_kind)
6773 {
6774 int signed_addr;
6775 unsigned int bytes_read;
6776 const char *filename = get_section_file_name (section);
6777 bfd *abfd = get_section_bfd_owner (section);
6778
6779 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6780 cu_header->initial_length_size = bytes_read;
6781 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6782 info_ptr += bytes_read;
6783 cu_header->version = read_2_bytes (abfd, info_ptr);
6784 info_ptr += 2;
6785 if (cu_header->version < 5)
6786 switch (section_kind)
6787 {
6788 case rcuh_kind::COMPILE:
6789 cu_header->unit_type = DW_UT_compile;
6790 break;
6791 case rcuh_kind::TYPE:
6792 cu_header->unit_type = DW_UT_type;
6793 break;
6794 default:
6795 internal_error (__FILE__, __LINE__,
6796 _("read_comp_unit_head: invalid section_kind"));
6797 }
6798 else
6799 {
6800 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6801 (read_1_byte (abfd, info_ptr));
6802 info_ptr += 1;
6803 switch (cu_header->unit_type)
6804 {
6805 case DW_UT_compile:
6806 if (section_kind != rcuh_kind::COMPILE)
6807 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6808 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6809 filename);
6810 break;
6811 case DW_UT_type:
6812 section_kind = rcuh_kind::TYPE;
6813 break;
6814 default:
6815 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6816 "(is %d, should be %d or %d) [in module %s]"),
6817 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6818 }
6819
6820 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6821 info_ptr += 1;
6822 }
6823 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6824 cu_header,
6825 &bytes_read);
6826 info_ptr += bytes_read;
6827 if (cu_header->version < 5)
6828 {
6829 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6830 info_ptr += 1;
6831 }
6832 signed_addr = bfd_get_sign_extend_vma (abfd);
6833 if (signed_addr < 0)
6834 internal_error (__FILE__, __LINE__,
6835 _("read_comp_unit_head: dwarf from non elf file"));
6836 cu_header->signed_addr_p = signed_addr;
6837
6838 if (section_kind == rcuh_kind::TYPE)
6839 {
6840 LONGEST type_offset;
6841
6842 cu_header->signature = read_8_bytes (abfd, info_ptr);
6843 info_ptr += 8;
6844
6845 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6846 info_ptr += bytes_read;
6847 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6848 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6849 error (_("Dwarf Error: Too big type_offset in compilation unit "
6850 "header (is %s) [in module %s]"), plongest (type_offset),
6851 filename);
6852 }
6853
6854 return info_ptr;
6855 }
6856
6857 /* Helper function that returns the proper abbrev section for
6858 THIS_CU. */
6859
6860 static struct dwarf2_section_info *
6861 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6862 {
6863 struct dwarf2_section_info *abbrev;
6864 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6865
6866 if (this_cu->is_dwz)
6867 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6868 else
6869 abbrev = &dwarf2_per_objfile->abbrev;
6870
6871 return abbrev;
6872 }
6873
6874 /* Subroutine of read_and_check_comp_unit_head and
6875 read_and_check_type_unit_head to simplify them.
6876 Perform various error checking on the header. */
6877
6878 static void
6879 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6880 struct comp_unit_head *header,
6881 struct dwarf2_section_info *section,
6882 struct dwarf2_section_info *abbrev_section)
6883 {
6884 const char *filename = get_section_file_name (section);
6885
6886 if (header->version < 2 || header->version > 5)
6887 error (_("Dwarf Error: wrong version in compilation unit header "
6888 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6889 filename);
6890
6891 if (to_underlying (header->abbrev_sect_off)
6892 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6893 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6894 "(offset %s + 6) [in module %s]"),
6895 sect_offset_str (header->abbrev_sect_off),
6896 sect_offset_str (header->sect_off),
6897 filename);
6898
6899 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6900 avoid potential 32-bit overflow. */
6901 if (((ULONGEST) header->sect_off + get_cu_length (header))
6902 > section->size)
6903 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6904 "(offset %s + 0) [in module %s]"),
6905 header->length, sect_offset_str (header->sect_off),
6906 filename);
6907 }
6908
6909 /* Read in a CU/TU header and perform some basic error checking.
6910 The contents of the header are stored in HEADER.
6911 The result is a pointer to the start of the first DIE. */
6912
6913 static const gdb_byte *
6914 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6915 struct comp_unit_head *header,
6916 struct dwarf2_section_info *section,
6917 struct dwarf2_section_info *abbrev_section,
6918 const gdb_byte *info_ptr,
6919 rcuh_kind section_kind)
6920 {
6921 const gdb_byte *beg_of_comp_unit = info_ptr;
6922
6923 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6924
6925 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6926
6927 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6928
6929 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6930 abbrev_section);
6931
6932 return info_ptr;
6933 }
6934
6935 /* Fetch the abbreviation table offset from a comp or type unit header. */
6936
6937 static sect_offset
6938 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6939 struct dwarf2_section_info *section,
6940 sect_offset sect_off)
6941 {
6942 bfd *abfd = get_section_bfd_owner (section);
6943 const gdb_byte *info_ptr;
6944 unsigned int initial_length_size, offset_size;
6945 uint16_t version;
6946
6947 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6948 info_ptr = section->buffer + to_underlying (sect_off);
6949 read_initial_length (abfd, info_ptr, &initial_length_size);
6950 offset_size = initial_length_size == 4 ? 4 : 8;
6951 info_ptr += initial_length_size;
6952
6953 version = read_2_bytes (abfd, info_ptr);
6954 info_ptr += 2;
6955 if (version >= 5)
6956 {
6957 /* Skip unit type and address size. */
6958 info_ptr += 2;
6959 }
6960
6961 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6962 }
6963
6964 /* Allocate a new partial symtab for file named NAME and mark this new
6965 partial symtab as being an include of PST. */
6966
6967 static void
6968 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6969 struct objfile *objfile)
6970 {
6971 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6972
6973 if (!IS_ABSOLUTE_PATH (subpst->filename))
6974 {
6975 /* It shares objfile->objfile_obstack. */
6976 subpst->dirname = pst->dirname;
6977 }
6978
6979 subpst->textlow = 0;
6980 subpst->texthigh = 0;
6981
6982 subpst->dependencies
6983 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6984 subpst->dependencies[0] = pst;
6985 subpst->number_of_dependencies = 1;
6986
6987 subpst->globals_offset = 0;
6988 subpst->n_global_syms = 0;
6989 subpst->statics_offset = 0;
6990 subpst->n_static_syms = 0;
6991 subpst->compunit_symtab = NULL;
6992 subpst->read_symtab = pst->read_symtab;
6993 subpst->readin = 0;
6994
6995 /* No private part is necessary for include psymtabs. This property
6996 can be used to differentiate between such include psymtabs and
6997 the regular ones. */
6998 subpst->read_symtab_private = NULL;
6999 }
7000
7001 /* Read the Line Number Program data and extract the list of files
7002 included by the source file represented by PST. Build an include
7003 partial symtab for each of these included files. */
7004
7005 static void
7006 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
7007 struct die_info *die,
7008 struct partial_symtab *pst)
7009 {
7010 line_header_up lh;
7011 struct attribute *attr;
7012
7013 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7014 if (attr)
7015 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
7016 if (lh == NULL)
7017 return; /* No linetable, so no includes. */
7018
7019 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
7020 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
7021 }
7022
7023 static hashval_t
7024 hash_signatured_type (const void *item)
7025 {
7026 const struct signatured_type *sig_type
7027 = (const struct signatured_type *) item;
7028
7029 /* This drops the top 32 bits of the signature, but is ok for a hash. */
7030 return sig_type->signature;
7031 }
7032
7033 static int
7034 eq_signatured_type (const void *item_lhs, const void *item_rhs)
7035 {
7036 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
7037 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
7038
7039 return lhs->signature == rhs->signature;
7040 }
7041
7042 /* Allocate a hash table for signatured types. */
7043
7044 static htab_t
7045 allocate_signatured_type_table (struct objfile *objfile)
7046 {
7047 return htab_create_alloc_ex (41,
7048 hash_signatured_type,
7049 eq_signatured_type,
7050 NULL,
7051 &objfile->objfile_obstack,
7052 hashtab_obstack_allocate,
7053 dummy_obstack_deallocate);
7054 }
7055
7056 /* A helper function to add a signatured type CU to a table. */
7057
7058 static int
7059 add_signatured_type_cu_to_table (void **slot, void *datum)
7060 {
7061 struct signatured_type *sigt = (struct signatured_type *) *slot;
7062 struct signatured_type ***datap = (struct signatured_type ***) datum;
7063
7064 **datap = sigt;
7065 ++*datap;
7066
7067 return 1;
7068 }
7069
7070 /* A helper for create_debug_types_hash_table. Read types from SECTION
7071 and fill them into TYPES_HTAB. It will process only type units,
7072 therefore DW_UT_type. */
7073
7074 static void
7075 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7076 struct dwo_file *dwo_file,
7077 dwarf2_section_info *section, htab_t &types_htab,
7078 rcuh_kind section_kind)
7079 {
7080 struct objfile *objfile = dwarf2_per_objfile->objfile;
7081 struct dwarf2_section_info *abbrev_section;
7082 bfd *abfd;
7083 const gdb_byte *info_ptr, *end_ptr;
7084
7085 abbrev_section = (dwo_file != NULL
7086 ? &dwo_file->sections.abbrev
7087 : &dwarf2_per_objfile->abbrev);
7088
7089 if (dwarf_read_debug)
7090 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
7091 get_section_name (section),
7092 get_section_file_name (abbrev_section));
7093
7094 dwarf2_read_section (objfile, section);
7095 info_ptr = section->buffer;
7096
7097 if (info_ptr == NULL)
7098 return;
7099
7100 /* We can't set abfd until now because the section may be empty or
7101 not present, in which case the bfd is unknown. */
7102 abfd = get_section_bfd_owner (section);
7103
7104 /* We don't use init_cutu_and_read_dies_simple, or some such, here
7105 because we don't need to read any dies: the signature is in the
7106 header. */
7107
7108 end_ptr = info_ptr + section->size;
7109 while (info_ptr < end_ptr)
7110 {
7111 struct signatured_type *sig_type;
7112 struct dwo_unit *dwo_tu;
7113 void **slot;
7114 const gdb_byte *ptr = info_ptr;
7115 struct comp_unit_head header;
7116 unsigned int length;
7117
7118 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
7119
7120 /* Initialize it due to a false compiler warning. */
7121 header.signature = -1;
7122 header.type_cu_offset_in_tu = (cu_offset) -1;
7123
7124 /* We need to read the type's signature in order to build the hash
7125 table, but we don't need anything else just yet. */
7126
7127 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
7128 abbrev_section, ptr, section_kind);
7129
7130 length = get_cu_length (&header);
7131
7132 /* Skip dummy type units. */
7133 if (ptr >= info_ptr + length
7134 || peek_abbrev_code (abfd, ptr) == 0
7135 || header.unit_type != DW_UT_type)
7136 {
7137 info_ptr += length;
7138 continue;
7139 }
7140
7141 if (types_htab == NULL)
7142 {
7143 if (dwo_file)
7144 types_htab = allocate_dwo_unit_table (objfile);
7145 else
7146 types_htab = allocate_signatured_type_table (objfile);
7147 }
7148
7149 if (dwo_file)
7150 {
7151 sig_type = NULL;
7152 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7153 struct dwo_unit);
7154 dwo_tu->dwo_file = dwo_file;
7155 dwo_tu->signature = header.signature;
7156 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
7157 dwo_tu->section = section;
7158 dwo_tu->sect_off = sect_off;
7159 dwo_tu->length = length;
7160 }
7161 else
7162 {
7163 /* N.B.: type_offset is not usable if this type uses a DWO file.
7164 The real type_offset is in the DWO file. */
7165 dwo_tu = NULL;
7166 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7167 struct signatured_type);
7168 sig_type->signature = header.signature;
7169 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
7170 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7171 sig_type->per_cu.is_debug_types = 1;
7172 sig_type->per_cu.section = section;
7173 sig_type->per_cu.sect_off = sect_off;
7174 sig_type->per_cu.length = length;
7175 }
7176
7177 slot = htab_find_slot (types_htab,
7178 dwo_file ? (void*) dwo_tu : (void *) sig_type,
7179 INSERT);
7180 gdb_assert (slot != NULL);
7181 if (*slot != NULL)
7182 {
7183 sect_offset dup_sect_off;
7184
7185 if (dwo_file)
7186 {
7187 const struct dwo_unit *dup_tu
7188 = (const struct dwo_unit *) *slot;
7189
7190 dup_sect_off = dup_tu->sect_off;
7191 }
7192 else
7193 {
7194 const struct signatured_type *dup_tu
7195 = (const struct signatured_type *) *slot;
7196
7197 dup_sect_off = dup_tu->per_cu.sect_off;
7198 }
7199
7200 complaint (&symfile_complaints,
7201 _("debug type entry at offset %s is duplicate to"
7202 " the entry at offset %s, signature %s"),
7203 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
7204 hex_string (header.signature));
7205 }
7206 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
7207
7208 if (dwarf_read_debug > 1)
7209 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
7210 sect_offset_str (sect_off),
7211 hex_string (header.signature));
7212
7213 info_ptr += length;
7214 }
7215 }
7216
7217 /* Create the hash table of all entries in the .debug_types
7218 (or .debug_types.dwo) section(s).
7219 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7220 otherwise it is NULL.
7221
7222 The result is a pointer to the hash table or NULL if there are no types.
7223
7224 Note: This function processes DWO files only, not DWP files. */
7225
7226 static void
7227 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7228 struct dwo_file *dwo_file,
7229 VEC (dwarf2_section_info_def) *types,
7230 htab_t &types_htab)
7231 {
7232 int ix;
7233 struct dwarf2_section_info *section;
7234
7235 if (VEC_empty (dwarf2_section_info_def, types))
7236 return;
7237
7238 for (ix = 0;
7239 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7240 ++ix)
7241 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
7242 types_htab, rcuh_kind::TYPE);
7243 }
7244
7245 /* Create the hash table of all entries in the .debug_types section,
7246 and initialize all_type_units.
7247 The result is zero if there is an error (e.g. missing .debug_types section),
7248 otherwise non-zero. */
7249
7250 static int
7251 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7252 {
7253 htab_t types_htab = NULL;
7254 struct signatured_type **iter;
7255
7256 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
7257 &dwarf2_per_objfile->info, types_htab,
7258 rcuh_kind::COMPILE);
7259 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
7260 dwarf2_per_objfile->types, types_htab);
7261 if (types_htab == NULL)
7262 {
7263 dwarf2_per_objfile->signatured_types = NULL;
7264 return 0;
7265 }
7266
7267 dwarf2_per_objfile->signatured_types = types_htab;
7268
7269 dwarf2_per_objfile->n_type_units
7270 = dwarf2_per_objfile->n_allocated_type_units
7271 = htab_elements (types_htab);
7272 dwarf2_per_objfile->all_type_units =
7273 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
7274 iter = &dwarf2_per_objfile->all_type_units[0];
7275 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7276 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7277 == dwarf2_per_objfile->n_type_units);
7278
7279 return 1;
7280 }
7281
7282 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7283 If SLOT is non-NULL, it is the entry to use in the hash table.
7284 Otherwise we find one. */
7285
7286 static struct signatured_type *
7287 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
7288 void **slot)
7289 {
7290 struct objfile *objfile = dwarf2_per_objfile->objfile;
7291 int n_type_units = dwarf2_per_objfile->n_type_units;
7292 struct signatured_type *sig_type;
7293
7294 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7295 ++n_type_units;
7296 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7297 {
7298 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7299 dwarf2_per_objfile->n_allocated_type_units = 1;
7300 dwarf2_per_objfile->n_allocated_type_units *= 2;
7301 dwarf2_per_objfile->all_type_units
7302 = XRESIZEVEC (struct signatured_type *,
7303 dwarf2_per_objfile->all_type_units,
7304 dwarf2_per_objfile->n_allocated_type_units);
7305 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7306 }
7307 dwarf2_per_objfile->n_type_units = n_type_units;
7308
7309 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7310 struct signatured_type);
7311 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7312 sig_type->signature = sig;
7313 sig_type->per_cu.is_debug_types = 1;
7314 if (dwarf2_per_objfile->using_index)
7315 {
7316 sig_type->per_cu.v.quick =
7317 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7318 struct dwarf2_per_cu_quick_data);
7319 }
7320
7321 if (slot == NULL)
7322 {
7323 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7324 sig_type, INSERT);
7325 }
7326 gdb_assert (*slot == NULL);
7327 *slot = sig_type;
7328 /* The rest of sig_type must be filled in by the caller. */
7329 return sig_type;
7330 }
7331
7332 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7333 Fill in SIG_ENTRY with DWO_ENTRY. */
7334
7335 static void
7336 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
7337 struct signatured_type *sig_entry,
7338 struct dwo_unit *dwo_entry)
7339 {
7340 /* Make sure we're not clobbering something we don't expect to. */
7341 gdb_assert (! sig_entry->per_cu.queued);
7342 gdb_assert (sig_entry->per_cu.cu == NULL);
7343 if (dwarf2_per_objfile->using_index)
7344 {
7345 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7346 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7347 }
7348 else
7349 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7350 gdb_assert (sig_entry->signature == dwo_entry->signature);
7351 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7352 gdb_assert (sig_entry->type_unit_group == NULL);
7353 gdb_assert (sig_entry->dwo_unit == NULL);
7354
7355 sig_entry->per_cu.section = dwo_entry->section;
7356 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7357 sig_entry->per_cu.length = dwo_entry->length;
7358 sig_entry->per_cu.reading_dwo_directly = 1;
7359 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7360 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7361 sig_entry->dwo_unit = dwo_entry;
7362 }
7363
7364 /* Subroutine of lookup_signatured_type.
7365 If we haven't read the TU yet, create the signatured_type data structure
7366 for a TU to be read in directly from a DWO file, bypassing the stub.
7367 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7368 using .gdb_index, then when reading a CU we want to stay in the DWO file
7369 containing that CU. Otherwise we could end up reading several other DWO
7370 files (due to comdat folding) to process the transitive closure of all the
7371 mentioned TUs, and that can be slow. The current DWO file will have every
7372 type signature that it needs.
7373 We only do this for .gdb_index because in the psymtab case we already have
7374 to read all the DWOs to build the type unit groups. */
7375
7376 static struct signatured_type *
7377 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7378 {
7379 struct dwarf2_per_objfile *dwarf2_per_objfile
7380 = cu->per_cu->dwarf2_per_objfile;
7381 struct objfile *objfile = dwarf2_per_objfile->objfile;
7382 struct dwo_file *dwo_file;
7383 struct dwo_unit find_dwo_entry, *dwo_entry;
7384 struct signatured_type find_sig_entry, *sig_entry;
7385 void **slot;
7386
7387 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7388
7389 /* If TU skeletons have been removed then we may not have read in any
7390 TUs yet. */
7391 if (dwarf2_per_objfile->signatured_types == NULL)
7392 {
7393 dwarf2_per_objfile->signatured_types
7394 = allocate_signatured_type_table (objfile);
7395 }
7396
7397 /* We only ever need to read in one copy of a signatured type.
7398 Use the global signatured_types array to do our own comdat-folding
7399 of types. If this is the first time we're reading this TU, and
7400 the TU has an entry in .gdb_index, replace the recorded data from
7401 .gdb_index with this TU. */
7402
7403 find_sig_entry.signature = sig;
7404 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7405 &find_sig_entry, INSERT);
7406 sig_entry = (struct signatured_type *) *slot;
7407
7408 /* We can get here with the TU already read, *or* in the process of being
7409 read. Don't reassign the global entry to point to this DWO if that's
7410 the case. Also note that if the TU is already being read, it may not
7411 have come from a DWO, the program may be a mix of Fission-compiled
7412 code and non-Fission-compiled code. */
7413
7414 /* Have we already tried to read this TU?
7415 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7416 needn't exist in the global table yet). */
7417 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7418 return sig_entry;
7419
7420 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7421 dwo_unit of the TU itself. */
7422 dwo_file = cu->dwo_unit->dwo_file;
7423
7424 /* Ok, this is the first time we're reading this TU. */
7425 if (dwo_file->tus == NULL)
7426 return NULL;
7427 find_dwo_entry.signature = sig;
7428 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7429 if (dwo_entry == NULL)
7430 return NULL;
7431
7432 /* If the global table doesn't have an entry for this TU, add one. */
7433 if (sig_entry == NULL)
7434 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7435
7436 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7437 sig_entry->per_cu.tu_read = 1;
7438 return sig_entry;
7439 }
7440
7441 /* Subroutine of lookup_signatured_type.
7442 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7443 then try the DWP file. If the TU stub (skeleton) has been removed then
7444 it won't be in .gdb_index. */
7445
7446 static struct signatured_type *
7447 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7448 {
7449 struct dwarf2_per_objfile *dwarf2_per_objfile
7450 = cu->per_cu->dwarf2_per_objfile;
7451 struct objfile *objfile = dwarf2_per_objfile->objfile;
7452 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7453 struct dwo_unit *dwo_entry;
7454 struct signatured_type find_sig_entry, *sig_entry;
7455 void **slot;
7456
7457 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7458 gdb_assert (dwp_file != NULL);
7459
7460 /* If TU skeletons have been removed then we may not have read in any
7461 TUs yet. */
7462 if (dwarf2_per_objfile->signatured_types == NULL)
7463 {
7464 dwarf2_per_objfile->signatured_types
7465 = allocate_signatured_type_table (objfile);
7466 }
7467
7468 find_sig_entry.signature = sig;
7469 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7470 &find_sig_entry, INSERT);
7471 sig_entry = (struct signatured_type *) *slot;
7472
7473 /* Have we already tried to read this TU?
7474 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7475 needn't exist in the global table yet). */
7476 if (sig_entry != NULL)
7477 return sig_entry;
7478
7479 if (dwp_file->tus == NULL)
7480 return NULL;
7481 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7482 sig, 1 /* is_debug_types */);
7483 if (dwo_entry == NULL)
7484 return NULL;
7485
7486 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7487 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7488
7489 return sig_entry;
7490 }
7491
7492 /* Lookup a signature based type for DW_FORM_ref_sig8.
7493 Returns NULL if signature SIG is not present in the table.
7494 It is up to the caller to complain about this. */
7495
7496 static struct signatured_type *
7497 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7498 {
7499 struct dwarf2_per_objfile *dwarf2_per_objfile
7500 = cu->per_cu->dwarf2_per_objfile;
7501
7502 if (cu->dwo_unit
7503 && dwarf2_per_objfile->using_index)
7504 {
7505 /* We're in a DWO/DWP file, and we're using .gdb_index.
7506 These cases require special processing. */
7507 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7508 return lookup_dwo_signatured_type (cu, sig);
7509 else
7510 return lookup_dwp_signatured_type (cu, sig);
7511 }
7512 else
7513 {
7514 struct signatured_type find_entry, *entry;
7515
7516 if (dwarf2_per_objfile->signatured_types == NULL)
7517 return NULL;
7518 find_entry.signature = sig;
7519 entry = ((struct signatured_type *)
7520 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7521 return entry;
7522 }
7523 }
7524 \f
7525 /* Low level DIE reading support. */
7526
7527 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7528
7529 static void
7530 init_cu_die_reader (struct die_reader_specs *reader,
7531 struct dwarf2_cu *cu,
7532 struct dwarf2_section_info *section,
7533 struct dwo_file *dwo_file,
7534 struct abbrev_table *abbrev_table)
7535 {
7536 gdb_assert (section->readin && section->buffer != NULL);
7537 reader->abfd = get_section_bfd_owner (section);
7538 reader->cu = cu;
7539 reader->dwo_file = dwo_file;
7540 reader->die_section = section;
7541 reader->buffer = section->buffer;
7542 reader->buffer_end = section->buffer + section->size;
7543 reader->comp_dir = NULL;
7544 reader->abbrev_table = abbrev_table;
7545 }
7546
7547 /* Subroutine of init_cutu_and_read_dies to simplify it.
7548 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7549 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7550 already.
7551
7552 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7553 from it to the DIE in the DWO. If NULL we are skipping the stub.
7554 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7555 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7556 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7557 STUB_COMP_DIR may be non-NULL.
7558 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7559 are filled in with the info of the DIE from the DWO file.
7560 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7561 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7562 kept around for at least as long as *RESULT_READER.
7563
7564 The result is non-zero if a valid (non-dummy) DIE was found. */
7565
7566 static int
7567 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7568 struct dwo_unit *dwo_unit,
7569 struct die_info *stub_comp_unit_die,
7570 const char *stub_comp_dir,
7571 struct die_reader_specs *result_reader,
7572 const gdb_byte **result_info_ptr,
7573 struct die_info **result_comp_unit_die,
7574 int *result_has_children,
7575 abbrev_table_up *result_dwo_abbrev_table)
7576 {
7577 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7578 struct objfile *objfile = dwarf2_per_objfile->objfile;
7579 struct dwarf2_cu *cu = this_cu->cu;
7580 bfd *abfd;
7581 const gdb_byte *begin_info_ptr, *info_ptr;
7582 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7583 int i,num_extra_attrs;
7584 struct dwarf2_section_info *dwo_abbrev_section;
7585 struct attribute *attr;
7586 struct die_info *comp_unit_die;
7587
7588 /* At most one of these may be provided. */
7589 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7590
7591 /* These attributes aren't processed until later:
7592 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7593 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7594 referenced later. However, these attributes are found in the stub
7595 which we won't have later. In order to not impose this complication
7596 on the rest of the code, we read them here and copy them to the
7597 DWO CU/TU die. */
7598
7599 stmt_list = NULL;
7600 low_pc = NULL;
7601 high_pc = NULL;
7602 ranges = NULL;
7603 comp_dir = NULL;
7604
7605 if (stub_comp_unit_die != NULL)
7606 {
7607 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7608 DWO file. */
7609 if (! this_cu->is_debug_types)
7610 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7611 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7612 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7613 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7614 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7615
7616 /* There should be a DW_AT_addr_base attribute here (if needed).
7617 We need the value before we can process DW_FORM_GNU_addr_index. */
7618 cu->addr_base = 0;
7619 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7620 if (attr)
7621 cu->addr_base = DW_UNSND (attr);
7622
7623 /* There should be a DW_AT_ranges_base attribute here (if needed).
7624 We need the value before we can process DW_AT_ranges. */
7625 cu->ranges_base = 0;
7626 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7627 if (attr)
7628 cu->ranges_base = DW_UNSND (attr);
7629 }
7630 else if (stub_comp_dir != NULL)
7631 {
7632 /* Reconstruct the comp_dir attribute to simplify the code below. */
7633 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7634 comp_dir->name = DW_AT_comp_dir;
7635 comp_dir->form = DW_FORM_string;
7636 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7637 DW_STRING (comp_dir) = stub_comp_dir;
7638 }
7639
7640 /* Set up for reading the DWO CU/TU. */
7641 cu->dwo_unit = dwo_unit;
7642 dwarf2_section_info *section = dwo_unit->section;
7643 dwarf2_read_section (objfile, section);
7644 abfd = get_section_bfd_owner (section);
7645 begin_info_ptr = info_ptr = (section->buffer
7646 + to_underlying (dwo_unit->sect_off));
7647 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7648
7649 if (this_cu->is_debug_types)
7650 {
7651 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7652
7653 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7654 &cu->header, section,
7655 dwo_abbrev_section,
7656 info_ptr, rcuh_kind::TYPE);
7657 /* This is not an assert because it can be caused by bad debug info. */
7658 if (sig_type->signature != cu->header.signature)
7659 {
7660 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7661 " TU at offset %s [in module %s]"),
7662 hex_string (sig_type->signature),
7663 hex_string (cu->header.signature),
7664 sect_offset_str (dwo_unit->sect_off),
7665 bfd_get_filename (abfd));
7666 }
7667 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7668 /* For DWOs coming from DWP files, we don't know the CU length
7669 nor the type's offset in the TU until now. */
7670 dwo_unit->length = get_cu_length (&cu->header);
7671 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7672
7673 /* Establish the type offset that can be used to lookup the type.
7674 For DWO files, we don't know it until now. */
7675 sig_type->type_offset_in_section
7676 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7677 }
7678 else
7679 {
7680 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7681 &cu->header, section,
7682 dwo_abbrev_section,
7683 info_ptr, rcuh_kind::COMPILE);
7684 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7685 /* For DWOs coming from DWP files, we don't know the CU length
7686 until now. */
7687 dwo_unit->length = get_cu_length (&cu->header);
7688 }
7689
7690 *result_dwo_abbrev_table
7691 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7692 cu->header.abbrev_sect_off);
7693 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7694 result_dwo_abbrev_table->get ());
7695
7696 /* Read in the die, but leave space to copy over the attributes
7697 from the stub. This has the benefit of simplifying the rest of
7698 the code - all the work to maintain the illusion of a single
7699 DW_TAG_{compile,type}_unit DIE is done here. */
7700 num_extra_attrs = ((stmt_list != NULL)
7701 + (low_pc != NULL)
7702 + (high_pc != NULL)
7703 + (ranges != NULL)
7704 + (comp_dir != NULL));
7705 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7706 result_has_children, num_extra_attrs);
7707
7708 /* Copy over the attributes from the stub to the DIE we just read in. */
7709 comp_unit_die = *result_comp_unit_die;
7710 i = comp_unit_die->num_attrs;
7711 if (stmt_list != NULL)
7712 comp_unit_die->attrs[i++] = *stmt_list;
7713 if (low_pc != NULL)
7714 comp_unit_die->attrs[i++] = *low_pc;
7715 if (high_pc != NULL)
7716 comp_unit_die->attrs[i++] = *high_pc;
7717 if (ranges != NULL)
7718 comp_unit_die->attrs[i++] = *ranges;
7719 if (comp_dir != NULL)
7720 comp_unit_die->attrs[i++] = *comp_dir;
7721 comp_unit_die->num_attrs += num_extra_attrs;
7722
7723 if (dwarf_die_debug)
7724 {
7725 fprintf_unfiltered (gdb_stdlog,
7726 "Read die from %s@0x%x of %s:\n",
7727 get_section_name (section),
7728 (unsigned) (begin_info_ptr - section->buffer),
7729 bfd_get_filename (abfd));
7730 dump_die (comp_unit_die, dwarf_die_debug);
7731 }
7732
7733 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7734 TUs by skipping the stub and going directly to the entry in the DWO file.
7735 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7736 to get it via circuitous means. Blech. */
7737 if (comp_dir != NULL)
7738 result_reader->comp_dir = DW_STRING (comp_dir);
7739
7740 /* Skip dummy compilation units. */
7741 if (info_ptr >= begin_info_ptr + dwo_unit->length
7742 || peek_abbrev_code (abfd, info_ptr) == 0)
7743 return 0;
7744
7745 *result_info_ptr = info_ptr;
7746 return 1;
7747 }
7748
7749 /* Subroutine of init_cutu_and_read_dies to simplify it.
7750 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7751 Returns NULL if the specified DWO unit cannot be found. */
7752
7753 static struct dwo_unit *
7754 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7755 struct die_info *comp_unit_die)
7756 {
7757 struct dwarf2_cu *cu = this_cu->cu;
7758 ULONGEST signature;
7759 struct dwo_unit *dwo_unit;
7760 const char *comp_dir, *dwo_name;
7761
7762 gdb_assert (cu != NULL);
7763
7764 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7765 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7766 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7767
7768 if (this_cu->is_debug_types)
7769 {
7770 struct signatured_type *sig_type;
7771
7772 /* Since this_cu is the first member of struct signatured_type,
7773 we can go from a pointer to one to a pointer to the other. */
7774 sig_type = (struct signatured_type *) this_cu;
7775 signature = sig_type->signature;
7776 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7777 }
7778 else
7779 {
7780 struct attribute *attr;
7781
7782 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7783 if (! attr)
7784 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7785 " [in module %s]"),
7786 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7787 signature = DW_UNSND (attr);
7788 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7789 signature);
7790 }
7791
7792 return dwo_unit;
7793 }
7794
7795 /* Subroutine of init_cutu_and_read_dies to simplify it.
7796 See it for a description of the parameters.
7797 Read a TU directly from a DWO file, bypassing the stub. */
7798
7799 static void
7800 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7801 int use_existing_cu, int keep,
7802 die_reader_func_ftype *die_reader_func,
7803 void *data)
7804 {
7805 std::unique_ptr<dwarf2_cu> new_cu;
7806 struct signatured_type *sig_type;
7807 struct die_reader_specs reader;
7808 const gdb_byte *info_ptr;
7809 struct die_info *comp_unit_die;
7810 int has_children;
7811 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7812
7813 /* Verify we can do the following downcast, and that we have the
7814 data we need. */
7815 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7816 sig_type = (struct signatured_type *) this_cu;
7817 gdb_assert (sig_type->dwo_unit != NULL);
7818
7819 if (use_existing_cu && this_cu->cu != NULL)
7820 {
7821 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7822 /* There's no need to do the rereading_dwo_cu handling that
7823 init_cutu_and_read_dies does since we don't read the stub. */
7824 }
7825 else
7826 {
7827 /* If !use_existing_cu, this_cu->cu must be NULL. */
7828 gdb_assert (this_cu->cu == NULL);
7829 new_cu.reset (new dwarf2_cu (this_cu));
7830 }
7831
7832 /* A future optimization, if needed, would be to use an existing
7833 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7834 could share abbrev tables. */
7835
7836 /* The abbreviation table used by READER, this must live at least as long as
7837 READER. */
7838 abbrev_table_up dwo_abbrev_table;
7839
7840 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7841 NULL /* stub_comp_unit_die */,
7842 sig_type->dwo_unit->dwo_file->comp_dir,
7843 &reader, &info_ptr,
7844 &comp_unit_die, &has_children,
7845 &dwo_abbrev_table) == 0)
7846 {
7847 /* Dummy die. */
7848 return;
7849 }
7850
7851 /* All the "real" work is done here. */
7852 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7853
7854 /* This duplicates the code in init_cutu_and_read_dies,
7855 but the alternative is making the latter more complex.
7856 This function is only for the special case of using DWO files directly:
7857 no point in overly complicating the general case just to handle this. */
7858 if (new_cu != NULL && keep)
7859 {
7860 /* Link this CU into read_in_chain. */
7861 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7862 dwarf2_per_objfile->read_in_chain = this_cu;
7863 /* The chain owns it now. */
7864 new_cu.release ();
7865 }
7866 }
7867
7868 /* Initialize a CU (or TU) and read its DIEs.
7869 If the CU defers to a DWO file, read the DWO file as well.
7870
7871 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7872 Otherwise the table specified in the comp unit header is read in and used.
7873 This is an optimization for when we already have the abbrev table.
7874
7875 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7876 Otherwise, a new CU is allocated with xmalloc.
7877
7878 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7879 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7880
7881 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7882 linker) then DIE_READER_FUNC will not get called. */
7883
7884 static void
7885 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7886 struct abbrev_table *abbrev_table,
7887 int use_existing_cu, int keep,
7888 die_reader_func_ftype *die_reader_func,
7889 void *data)
7890 {
7891 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7892 struct objfile *objfile = dwarf2_per_objfile->objfile;
7893 struct dwarf2_section_info *section = this_cu->section;
7894 bfd *abfd = get_section_bfd_owner (section);
7895 struct dwarf2_cu *cu;
7896 const gdb_byte *begin_info_ptr, *info_ptr;
7897 struct die_reader_specs reader;
7898 struct die_info *comp_unit_die;
7899 int has_children;
7900 struct attribute *attr;
7901 struct signatured_type *sig_type = NULL;
7902 struct dwarf2_section_info *abbrev_section;
7903 /* Non-zero if CU currently points to a DWO file and we need to
7904 reread it. When this happens we need to reread the skeleton die
7905 before we can reread the DWO file (this only applies to CUs, not TUs). */
7906 int rereading_dwo_cu = 0;
7907
7908 if (dwarf_die_debug)
7909 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7910 this_cu->is_debug_types ? "type" : "comp",
7911 sect_offset_str (this_cu->sect_off));
7912
7913 if (use_existing_cu)
7914 gdb_assert (keep);
7915
7916 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7917 file (instead of going through the stub), short-circuit all of this. */
7918 if (this_cu->reading_dwo_directly)
7919 {
7920 /* Narrow down the scope of possibilities to have to understand. */
7921 gdb_assert (this_cu->is_debug_types);
7922 gdb_assert (abbrev_table == NULL);
7923 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7924 die_reader_func, data);
7925 return;
7926 }
7927
7928 /* This is cheap if the section is already read in. */
7929 dwarf2_read_section (objfile, section);
7930
7931 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7932
7933 abbrev_section = get_abbrev_section_for_cu (this_cu);
7934
7935 std::unique_ptr<dwarf2_cu> new_cu;
7936 if (use_existing_cu && this_cu->cu != NULL)
7937 {
7938 cu = this_cu->cu;
7939 /* If this CU is from a DWO file we need to start over, we need to
7940 refetch the attributes from the skeleton CU.
7941 This could be optimized by retrieving those attributes from when we
7942 were here the first time: the previous comp_unit_die was stored in
7943 comp_unit_obstack. But there's no data yet that we need this
7944 optimization. */
7945 if (cu->dwo_unit != NULL)
7946 rereading_dwo_cu = 1;
7947 }
7948 else
7949 {
7950 /* If !use_existing_cu, this_cu->cu must be NULL. */
7951 gdb_assert (this_cu->cu == NULL);
7952 new_cu.reset (new dwarf2_cu (this_cu));
7953 cu = new_cu.get ();
7954 }
7955
7956 /* Get the header. */
7957 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7958 {
7959 /* We already have the header, there's no need to read it in again. */
7960 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7961 }
7962 else
7963 {
7964 if (this_cu->is_debug_types)
7965 {
7966 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7967 &cu->header, section,
7968 abbrev_section, info_ptr,
7969 rcuh_kind::TYPE);
7970
7971 /* Since per_cu is the first member of struct signatured_type,
7972 we can go from a pointer to one to a pointer to the other. */
7973 sig_type = (struct signatured_type *) this_cu;
7974 gdb_assert (sig_type->signature == cu->header.signature);
7975 gdb_assert (sig_type->type_offset_in_tu
7976 == cu->header.type_cu_offset_in_tu);
7977 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7978
7979 /* LENGTH has not been set yet for type units if we're
7980 using .gdb_index. */
7981 this_cu->length = get_cu_length (&cu->header);
7982
7983 /* Establish the type offset that can be used to lookup the type. */
7984 sig_type->type_offset_in_section =
7985 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7986
7987 this_cu->dwarf_version = cu->header.version;
7988 }
7989 else
7990 {
7991 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7992 &cu->header, section,
7993 abbrev_section,
7994 info_ptr,
7995 rcuh_kind::COMPILE);
7996
7997 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7998 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7999 this_cu->dwarf_version = cu->header.version;
8000 }
8001 }
8002
8003 /* Skip dummy compilation units. */
8004 if (info_ptr >= begin_info_ptr + this_cu->length
8005 || peek_abbrev_code (abfd, info_ptr) == 0)
8006 return;
8007
8008 /* If we don't have them yet, read the abbrevs for this compilation unit.
8009 And if we need to read them now, make sure they're freed when we're
8010 done (own the table through ABBREV_TABLE_HOLDER). */
8011 abbrev_table_up abbrev_table_holder;
8012 if (abbrev_table != NULL)
8013 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
8014 else
8015 {
8016 abbrev_table_holder
8017 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8018 cu->header.abbrev_sect_off);
8019 abbrev_table = abbrev_table_holder.get ();
8020 }
8021
8022 /* Read the top level CU/TU die. */
8023 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
8024 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8025
8026 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
8027 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
8028 table from the DWO file and pass the ownership over to us. It will be
8029 referenced from READER, so we must make sure to free it after we're done
8030 with READER.
8031
8032 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
8033 DWO CU, that this test will fail (the attribute will not be present). */
8034 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
8035 abbrev_table_up dwo_abbrev_table;
8036 if (attr)
8037 {
8038 struct dwo_unit *dwo_unit;
8039 struct die_info *dwo_comp_unit_die;
8040
8041 if (has_children)
8042 {
8043 complaint (&symfile_complaints,
8044 _("compilation unit with DW_AT_GNU_dwo_name"
8045 " has children (offset %s) [in module %s]"),
8046 sect_offset_str (this_cu->sect_off),
8047 bfd_get_filename (abfd));
8048 }
8049 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
8050 if (dwo_unit != NULL)
8051 {
8052 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
8053 comp_unit_die, NULL,
8054 &reader, &info_ptr,
8055 &dwo_comp_unit_die, &has_children,
8056 &dwo_abbrev_table) == 0)
8057 {
8058 /* Dummy die. */
8059 return;
8060 }
8061 comp_unit_die = dwo_comp_unit_die;
8062 }
8063 else
8064 {
8065 /* Yikes, we couldn't find the rest of the DIE, we only have
8066 the stub. A complaint has already been logged. There's
8067 not much more we can do except pass on the stub DIE to
8068 die_reader_func. We don't want to throw an error on bad
8069 debug info. */
8070 }
8071 }
8072
8073 /* All of the above is setup for this call. Yikes. */
8074 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8075
8076 /* Done, clean up. */
8077 if (new_cu != NULL && keep)
8078 {
8079 /* Link this CU into read_in_chain. */
8080 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
8081 dwarf2_per_objfile->read_in_chain = this_cu;
8082 /* The chain owns it now. */
8083 new_cu.release ();
8084 }
8085 }
8086
8087 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
8088 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
8089 to have already done the lookup to find the DWO file).
8090
8091 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
8092 THIS_CU->is_debug_types, but nothing else.
8093
8094 We fill in THIS_CU->length.
8095
8096 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
8097 linker) then DIE_READER_FUNC will not get called.
8098
8099 THIS_CU->cu is always freed when done.
8100 This is done in order to not leave THIS_CU->cu in a state where we have
8101 to care whether it refers to the "main" CU or the DWO CU. */
8102
8103 static void
8104 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
8105 struct dwo_file *dwo_file,
8106 die_reader_func_ftype *die_reader_func,
8107 void *data)
8108 {
8109 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
8110 struct objfile *objfile = dwarf2_per_objfile->objfile;
8111 struct dwarf2_section_info *section = this_cu->section;
8112 bfd *abfd = get_section_bfd_owner (section);
8113 struct dwarf2_section_info *abbrev_section;
8114 const gdb_byte *begin_info_ptr, *info_ptr;
8115 struct die_reader_specs reader;
8116 struct die_info *comp_unit_die;
8117 int has_children;
8118
8119 if (dwarf_die_debug)
8120 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
8121 this_cu->is_debug_types ? "type" : "comp",
8122 sect_offset_str (this_cu->sect_off));
8123
8124 gdb_assert (this_cu->cu == NULL);
8125
8126 abbrev_section = (dwo_file != NULL
8127 ? &dwo_file->sections.abbrev
8128 : get_abbrev_section_for_cu (this_cu));
8129
8130 /* This is cheap if the section is already read in. */
8131 dwarf2_read_section (objfile, section);
8132
8133 struct dwarf2_cu cu (this_cu);
8134
8135 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
8136 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
8137 &cu.header, section,
8138 abbrev_section, info_ptr,
8139 (this_cu->is_debug_types
8140 ? rcuh_kind::TYPE
8141 : rcuh_kind::COMPILE));
8142
8143 this_cu->length = get_cu_length (&cu.header);
8144
8145 /* Skip dummy compilation units. */
8146 if (info_ptr >= begin_info_ptr + this_cu->length
8147 || peek_abbrev_code (abfd, info_ptr) == 0)
8148 return;
8149
8150 abbrev_table_up abbrev_table
8151 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8152 cu.header.abbrev_sect_off);
8153
8154 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
8155 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8156
8157 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8158 }
8159
8160 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
8161 does not lookup the specified DWO file.
8162 This cannot be used to read DWO files.
8163
8164 THIS_CU->cu is always freed when done.
8165 This is done in order to not leave THIS_CU->cu in a state where we have
8166 to care whether it refers to the "main" CU or the DWO CU.
8167 We can revisit this if the data shows there's a performance issue. */
8168
8169 static void
8170 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8171 die_reader_func_ftype *die_reader_func,
8172 void *data)
8173 {
8174 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
8175 }
8176 \f
8177 /* Type Unit Groups.
8178
8179 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8180 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8181 so that all types coming from the same compilation (.o file) are grouped
8182 together. A future step could be to put the types in the same symtab as
8183 the CU the types ultimately came from. */
8184
8185 static hashval_t
8186 hash_type_unit_group (const void *item)
8187 {
8188 const struct type_unit_group *tu_group
8189 = (const struct type_unit_group *) item;
8190
8191 return hash_stmt_list_entry (&tu_group->hash);
8192 }
8193
8194 static int
8195 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
8196 {
8197 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8198 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
8199
8200 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
8201 }
8202
8203 /* Allocate a hash table for type unit groups. */
8204
8205 static htab_t
8206 allocate_type_unit_groups_table (struct objfile *objfile)
8207 {
8208 return htab_create_alloc_ex (3,
8209 hash_type_unit_group,
8210 eq_type_unit_group,
8211 NULL,
8212 &objfile->objfile_obstack,
8213 hashtab_obstack_allocate,
8214 dummy_obstack_deallocate);
8215 }
8216
8217 /* Type units that don't have DW_AT_stmt_list are grouped into their own
8218 partial symtabs. We combine several TUs per psymtab to not let the size
8219 of any one psymtab grow too big. */
8220 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8221 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
8222
8223 /* Helper routine for get_type_unit_group.
8224 Create the type_unit_group object used to hold one or more TUs. */
8225
8226 static struct type_unit_group *
8227 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
8228 {
8229 struct dwarf2_per_objfile *dwarf2_per_objfile
8230 = cu->per_cu->dwarf2_per_objfile;
8231 struct objfile *objfile = dwarf2_per_objfile->objfile;
8232 struct dwarf2_per_cu_data *per_cu;
8233 struct type_unit_group *tu_group;
8234
8235 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8236 struct type_unit_group);
8237 per_cu = &tu_group->per_cu;
8238 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8239
8240 if (dwarf2_per_objfile->using_index)
8241 {
8242 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8243 struct dwarf2_per_cu_quick_data);
8244 }
8245 else
8246 {
8247 unsigned int line_offset = to_underlying (line_offset_struct);
8248 struct partial_symtab *pst;
8249 char *name;
8250
8251 /* Give the symtab a useful name for debug purposes. */
8252 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8253 name = xstrprintf ("<type_units_%d>",
8254 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8255 else
8256 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8257
8258 pst = create_partial_symtab (per_cu, name);
8259 pst->anonymous = 1;
8260
8261 xfree (name);
8262 }
8263
8264 tu_group->hash.dwo_unit = cu->dwo_unit;
8265 tu_group->hash.line_sect_off = line_offset_struct;
8266
8267 return tu_group;
8268 }
8269
8270 /* Look up the type_unit_group for type unit CU, and create it if necessary.
8271 STMT_LIST is a DW_AT_stmt_list attribute. */
8272
8273 static struct type_unit_group *
8274 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
8275 {
8276 struct dwarf2_per_objfile *dwarf2_per_objfile
8277 = cu->per_cu->dwarf2_per_objfile;
8278 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8279 struct type_unit_group *tu_group;
8280 void **slot;
8281 unsigned int line_offset;
8282 struct type_unit_group type_unit_group_for_lookup;
8283
8284 if (dwarf2_per_objfile->type_unit_groups == NULL)
8285 {
8286 dwarf2_per_objfile->type_unit_groups =
8287 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
8288 }
8289
8290 /* Do we need to create a new group, or can we use an existing one? */
8291
8292 if (stmt_list)
8293 {
8294 line_offset = DW_UNSND (stmt_list);
8295 ++tu_stats->nr_symtab_sharers;
8296 }
8297 else
8298 {
8299 /* Ugh, no stmt_list. Rare, but we have to handle it.
8300 We can do various things here like create one group per TU or
8301 spread them over multiple groups to split up the expansion work.
8302 To avoid worst case scenarios (too many groups or too large groups)
8303 we, umm, group them in bunches. */
8304 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8305 | (tu_stats->nr_stmt_less_type_units
8306 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8307 ++tu_stats->nr_stmt_less_type_units;
8308 }
8309
8310 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
8311 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
8312 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8313 &type_unit_group_for_lookup, INSERT);
8314 if (*slot != NULL)
8315 {
8316 tu_group = (struct type_unit_group *) *slot;
8317 gdb_assert (tu_group != NULL);
8318 }
8319 else
8320 {
8321 sect_offset line_offset_struct = (sect_offset) line_offset;
8322 tu_group = create_type_unit_group (cu, line_offset_struct);
8323 *slot = tu_group;
8324 ++tu_stats->nr_symtabs;
8325 }
8326
8327 return tu_group;
8328 }
8329 \f
8330 /* Partial symbol tables. */
8331
8332 /* Create a psymtab named NAME and assign it to PER_CU.
8333
8334 The caller must fill in the following details:
8335 dirname, textlow, texthigh. */
8336
8337 static struct partial_symtab *
8338 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8339 {
8340 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8341 struct partial_symtab *pst;
8342
8343 pst = start_psymtab_common (objfile, name, 0,
8344 objfile->global_psymbols,
8345 objfile->static_psymbols);
8346
8347 pst->psymtabs_addrmap_supported = 1;
8348
8349 /* This is the glue that links PST into GDB's symbol API. */
8350 pst->read_symtab_private = per_cu;
8351 pst->read_symtab = dwarf2_read_symtab;
8352 per_cu->v.psymtab = pst;
8353
8354 return pst;
8355 }
8356
8357 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8358 type. */
8359
8360 struct process_psymtab_comp_unit_data
8361 {
8362 /* True if we are reading a DW_TAG_partial_unit. */
8363
8364 int want_partial_unit;
8365
8366 /* The "pretend" language that is used if the CU doesn't declare a
8367 language. */
8368
8369 enum language pretend_language;
8370 };
8371
8372 /* die_reader_func for process_psymtab_comp_unit. */
8373
8374 static void
8375 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8376 const gdb_byte *info_ptr,
8377 struct die_info *comp_unit_die,
8378 int has_children,
8379 void *data)
8380 {
8381 struct dwarf2_cu *cu = reader->cu;
8382 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8383 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8384 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8385 CORE_ADDR baseaddr;
8386 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8387 struct partial_symtab *pst;
8388 enum pc_bounds_kind cu_bounds_kind;
8389 const char *filename;
8390 struct process_psymtab_comp_unit_data *info
8391 = (struct process_psymtab_comp_unit_data *) data;
8392
8393 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8394 return;
8395
8396 gdb_assert (! per_cu->is_debug_types);
8397
8398 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8399
8400 cu->list_in_scope = &file_symbols;
8401
8402 /* Allocate a new partial symbol table structure. */
8403 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8404 if (filename == NULL)
8405 filename = "";
8406
8407 pst = create_partial_symtab (per_cu, filename);
8408
8409 /* This must be done before calling dwarf2_build_include_psymtabs. */
8410 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8411
8412 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8413
8414 dwarf2_find_base_address (comp_unit_die, cu);
8415
8416 /* Possibly set the default values of LOWPC and HIGHPC from
8417 `DW_AT_ranges'. */
8418 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8419 &best_highpc, cu, pst);
8420 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8421 /* Store the contiguous range if it is not empty; it can be empty for
8422 CUs with no code. */
8423 addrmap_set_empty (objfile->psymtabs_addrmap,
8424 gdbarch_adjust_dwarf2_addr (gdbarch,
8425 best_lowpc + baseaddr),
8426 gdbarch_adjust_dwarf2_addr (gdbarch,
8427 best_highpc + baseaddr) - 1,
8428 pst);
8429
8430 /* Check if comp unit has_children.
8431 If so, read the rest of the partial symbols from this comp unit.
8432 If not, there's no more debug_info for this comp unit. */
8433 if (has_children)
8434 {
8435 struct partial_die_info *first_die;
8436 CORE_ADDR lowpc, highpc;
8437
8438 lowpc = ((CORE_ADDR) -1);
8439 highpc = ((CORE_ADDR) 0);
8440
8441 first_die = load_partial_dies (reader, info_ptr, 1);
8442
8443 scan_partial_symbols (first_die, &lowpc, &highpc,
8444 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8445
8446 /* If we didn't find a lowpc, set it to highpc to avoid
8447 complaints from `maint check'. */
8448 if (lowpc == ((CORE_ADDR) -1))
8449 lowpc = highpc;
8450
8451 /* If the compilation unit didn't have an explicit address range,
8452 then use the information extracted from its child dies. */
8453 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8454 {
8455 best_lowpc = lowpc;
8456 best_highpc = highpc;
8457 }
8458 }
8459 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8460 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
8461
8462 end_psymtab_common (objfile, pst);
8463
8464 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8465 {
8466 int i;
8467 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8468 struct dwarf2_per_cu_data *iter;
8469
8470 /* Fill in 'dependencies' here; we fill in 'users' in a
8471 post-pass. */
8472 pst->number_of_dependencies = len;
8473 pst->dependencies =
8474 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8475 for (i = 0;
8476 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8477 i, iter);
8478 ++i)
8479 pst->dependencies[i] = iter->v.psymtab;
8480
8481 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8482 }
8483
8484 /* Get the list of files included in the current compilation unit,
8485 and build a psymtab for each of them. */
8486 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8487
8488 if (dwarf_read_debug)
8489 {
8490 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8491
8492 fprintf_unfiltered (gdb_stdlog,
8493 "Psymtab for %s unit @%s: %s - %s"
8494 ", %d global, %d static syms\n",
8495 per_cu->is_debug_types ? "type" : "comp",
8496 sect_offset_str (per_cu->sect_off),
8497 paddress (gdbarch, pst->textlow),
8498 paddress (gdbarch, pst->texthigh),
8499 pst->n_global_syms, pst->n_static_syms);
8500 }
8501 }
8502
8503 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8504 Process compilation unit THIS_CU for a psymtab. */
8505
8506 static void
8507 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8508 int want_partial_unit,
8509 enum language pretend_language)
8510 {
8511 /* If this compilation unit was already read in, free the
8512 cached copy in order to read it in again. This is
8513 necessary because we skipped some symbols when we first
8514 read in the compilation unit (see load_partial_dies).
8515 This problem could be avoided, but the benefit is unclear. */
8516 if (this_cu->cu != NULL)
8517 free_one_cached_comp_unit (this_cu);
8518
8519 if (this_cu->is_debug_types)
8520 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8521 NULL);
8522 else
8523 {
8524 process_psymtab_comp_unit_data info;
8525 info.want_partial_unit = want_partial_unit;
8526 info.pretend_language = pretend_language;
8527 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8528 process_psymtab_comp_unit_reader, &info);
8529 }
8530
8531 /* Age out any secondary CUs. */
8532 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8533 }
8534
8535 /* Reader function for build_type_psymtabs. */
8536
8537 static void
8538 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8539 const gdb_byte *info_ptr,
8540 struct die_info *type_unit_die,
8541 int has_children,
8542 void *data)
8543 {
8544 struct dwarf2_per_objfile *dwarf2_per_objfile
8545 = reader->cu->per_cu->dwarf2_per_objfile;
8546 struct objfile *objfile = dwarf2_per_objfile->objfile;
8547 struct dwarf2_cu *cu = reader->cu;
8548 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8549 struct signatured_type *sig_type;
8550 struct type_unit_group *tu_group;
8551 struct attribute *attr;
8552 struct partial_die_info *first_die;
8553 CORE_ADDR lowpc, highpc;
8554 struct partial_symtab *pst;
8555
8556 gdb_assert (data == NULL);
8557 gdb_assert (per_cu->is_debug_types);
8558 sig_type = (struct signatured_type *) per_cu;
8559
8560 if (! has_children)
8561 return;
8562
8563 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8564 tu_group = get_type_unit_group (cu, attr);
8565
8566 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8567
8568 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8569 cu->list_in_scope = &file_symbols;
8570 pst = create_partial_symtab (per_cu, "");
8571 pst->anonymous = 1;
8572
8573 first_die = load_partial_dies (reader, info_ptr, 1);
8574
8575 lowpc = (CORE_ADDR) -1;
8576 highpc = (CORE_ADDR) 0;
8577 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8578
8579 end_psymtab_common (objfile, pst);
8580 }
8581
8582 /* Struct used to sort TUs by their abbreviation table offset. */
8583
8584 struct tu_abbrev_offset
8585 {
8586 struct signatured_type *sig_type;
8587 sect_offset abbrev_offset;
8588 };
8589
8590 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8591
8592 static bool
8593 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8594 const struct tu_abbrev_offset &b)
8595 {
8596 return a.abbrev_offset < b.abbrev_offset;
8597 }
8598
8599 /* Efficiently read all the type units.
8600 This does the bulk of the work for build_type_psymtabs.
8601
8602 The efficiency is because we sort TUs by the abbrev table they use and
8603 only read each abbrev table once. In one program there are 200K TUs
8604 sharing 8K abbrev tables.
8605
8606 The main purpose of this function is to support building the
8607 dwarf2_per_objfile->type_unit_groups table.
8608 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8609 can collapse the search space by grouping them by stmt_list.
8610 The savings can be significant, in the same program from above the 200K TUs
8611 share 8K stmt_list tables.
8612
8613 FUNC is expected to call get_type_unit_group, which will create the
8614 struct type_unit_group if necessary and add it to
8615 dwarf2_per_objfile->type_unit_groups. */
8616
8617 static void
8618 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8619 {
8620 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8621 abbrev_table_up abbrev_table;
8622 sect_offset abbrev_offset;
8623 int i;
8624
8625 /* It's up to the caller to not call us multiple times. */
8626 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8627
8628 if (dwarf2_per_objfile->n_type_units == 0)
8629 return;
8630
8631 /* TUs typically share abbrev tables, and there can be way more TUs than
8632 abbrev tables. Sort by abbrev table to reduce the number of times we
8633 read each abbrev table in.
8634 Alternatives are to punt or to maintain a cache of abbrev tables.
8635 This is simpler and efficient enough for now.
8636
8637 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8638 symtab to use). Typically TUs with the same abbrev offset have the same
8639 stmt_list value too so in practice this should work well.
8640
8641 The basic algorithm here is:
8642
8643 sort TUs by abbrev table
8644 for each TU with same abbrev table:
8645 read abbrev table if first user
8646 read TU top level DIE
8647 [IWBN if DWO skeletons had DW_AT_stmt_list]
8648 call FUNC */
8649
8650 if (dwarf_read_debug)
8651 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8652
8653 /* Sort in a separate table to maintain the order of all_type_units
8654 for .gdb_index: TU indices directly index all_type_units. */
8655 std::vector<struct tu_abbrev_offset> sorted_by_abbrev
8656 (dwarf2_per_objfile->n_type_units);
8657 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8658 {
8659 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8660
8661 sorted_by_abbrev[i].sig_type = sig_type;
8662 sorted_by_abbrev[i].abbrev_offset =
8663 read_abbrev_offset (dwarf2_per_objfile,
8664 sig_type->per_cu.section,
8665 sig_type->per_cu.sect_off);
8666 }
8667 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8668 sort_tu_by_abbrev_offset);
8669
8670 abbrev_offset = (sect_offset) ~(unsigned) 0;
8671
8672 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8673 {
8674 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8675
8676 /* Switch to the next abbrev table if necessary. */
8677 if (abbrev_table == NULL
8678 || tu->abbrev_offset != abbrev_offset)
8679 {
8680 abbrev_offset = tu->abbrev_offset;
8681 abbrev_table =
8682 abbrev_table_read_table (dwarf2_per_objfile,
8683 &dwarf2_per_objfile->abbrev,
8684 abbrev_offset);
8685 ++tu_stats->nr_uniq_abbrev_tables;
8686 }
8687
8688 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table.get (),
8689 0, 0, build_type_psymtabs_reader, NULL);
8690 }
8691 }
8692
8693 /* Print collected type unit statistics. */
8694
8695 static void
8696 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8697 {
8698 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8699
8700 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8701 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8702 dwarf2_per_objfile->n_type_units);
8703 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8704 tu_stats->nr_uniq_abbrev_tables);
8705 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8706 tu_stats->nr_symtabs);
8707 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8708 tu_stats->nr_symtab_sharers);
8709 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8710 tu_stats->nr_stmt_less_type_units);
8711 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8712 tu_stats->nr_all_type_units_reallocs);
8713 }
8714
8715 /* Traversal function for build_type_psymtabs. */
8716
8717 static int
8718 build_type_psymtab_dependencies (void **slot, void *info)
8719 {
8720 struct dwarf2_per_objfile *dwarf2_per_objfile
8721 = (struct dwarf2_per_objfile *) info;
8722 struct objfile *objfile = dwarf2_per_objfile->objfile;
8723 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8724 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8725 struct partial_symtab *pst = per_cu->v.psymtab;
8726 int len = VEC_length (sig_type_ptr, tu_group->tus);
8727 struct signatured_type *iter;
8728 int i;
8729
8730 gdb_assert (len > 0);
8731 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8732
8733 pst->number_of_dependencies = len;
8734 pst->dependencies =
8735 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8736 for (i = 0;
8737 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8738 ++i)
8739 {
8740 gdb_assert (iter->per_cu.is_debug_types);
8741 pst->dependencies[i] = iter->per_cu.v.psymtab;
8742 iter->type_unit_group = tu_group;
8743 }
8744
8745 VEC_free (sig_type_ptr, tu_group->tus);
8746
8747 return 1;
8748 }
8749
8750 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8751 Build partial symbol tables for the .debug_types comp-units. */
8752
8753 static void
8754 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8755 {
8756 if (! create_all_type_units (dwarf2_per_objfile))
8757 return;
8758
8759 build_type_psymtabs_1 (dwarf2_per_objfile);
8760 }
8761
8762 /* Traversal function for process_skeletonless_type_unit.
8763 Read a TU in a DWO file and build partial symbols for it. */
8764
8765 static int
8766 process_skeletonless_type_unit (void **slot, void *info)
8767 {
8768 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8769 struct dwarf2_per_objfile *dwarf2_per_objfile
8770 = (struct dwarf2_per_objfile *) info;
8771 struct signatured_type find_entry, *entry;
8772
8773 /* If this TU doesn't exist in the global table, add it and read it in. */
8774
8775 if (dwarf2_per_objfile->signatured_types == NULL)
8776 {
8777 dwarf2_per_objfile->signatured_types
8778 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8779 }
8780
8781 find_entry.signature = dwo_unit->signature;
8782 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8783 INSERT);
8784 /* If we've already seen this type there's nothing to do. What's happening
8785 is we're doing our own version of comdat-folding here. */
8786 if (*slot != NULL)
8787 return 1;
8788
8789 /* This does the job that create_all_type_units would have done for
8790 this TU. */
8791 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8792 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8793 *slot = entry;
8794
8795 /* This does the job that build_type_psymtabs_1 would have done. */
8796 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8797 build_type_psymtabs_reader, NULL);
8798
8799 return 1;
8800 }
8801
8802 /* Traversal function for process_skeletonless_type_units. */
8803
8804 static int
8805 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8806 {
8807 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8808
8809 if (dwo_file->tus != NULL)
8810 {
8811 htab_traverse_noresize (dwo_file->tus,
8812 process_skeletonless_type_unit, info);
8813 }
8814
8815 return 1;
8816 }
8817
8818 /* Scan all TUs of DWO files, verifying we've processed them.
8819 This is needed in case a TU was emitted without its skeleton.
8820 Note: This can't be done until we know what all the DWO files are. */
8821
8822 static void
8823 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8824 {
8825 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8826 if (get_dwp_file (dwarf2_per_objfile) == NULL
8827 && dwarf2_per_objfile->dwo_files != NULL)
8828 {
8829 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8830 process_dwo_file_for_skeletonless_type_units,
8831 dwarf2_per_objfile);
8832 }
8833 }
8834
8835 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8836
8837 static void
8838 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8839 {
8840 int i;
8841
8842 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8843 {
8844 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8845 struct partial_symtab *pst = per_cu->v.psymtab;
8846 int j;
8847
8848 if (pst == NULL)
8849 continue;
8850
8851 for (j = 0; j < pst->number_of_dependencies; ++j)
8852 {
8853 /* Set the 'user' field only if it is not already set. */
8854 if (pst->dependencies[j]->user == NULL)
8855 pst->dependencies[j]->user = pst;
8856 }
8857 }
8858 }
8859
8860 /* Build the partial symbol table by doing a quick pass through the
8861 .debug_info and .debug_abbrev sections. */
8862
8863 static void
8864 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8865 {
8866 struct cleanup *back_to;
8867 int i;
8868 struct objfile *objfile = dwarf2_per_objfile->objfile;
8869
8870 if (dwarf_read_debug)
8871 {
8872 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8873 objfile_name (objfile));
8874 }
8875
8876 dwarf2_per_objfile->reading_partial_symbols = 1;
8877
8878 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8879
8880 /* Any cached compilation units will be linked by the per-objfile
8881 read_in_chain. Make sure to free them when we're done. */
8882 back_to = make_cleanup (free_cached_comp_units, dwarf2_per_objfile);
8883
8884 build_type_psymtabs (dwarf2_per_objfile);
8885
8886 create_all_comp_units (dwarf2_per_objfile);
8887
8888 /* Create a temporary address map on a temporary obstack. We later
8889 copy this to the final obstack. */
8890 auto_obstack temp_obstack;
8891
8892 scoped_restore save_psymtabs_addrmap
8893 = make_scoped_restore (&objfile->psymtabs_addrmap,
8894 addrmap_create_mutable (&temp_obstack));
8895
8896 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8897 {
8898 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8899
8900 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8901 }
8902
8903 /* This has to wait until we read the CUs, we need the list of DWOs. */
8904 process_skeletonless_type_units (dwarf2_per_objfile);
8905
8906 /* Now that all TUs have been processed we can fill in the dependencies. */
8907 if (dwarf2_per_objfile->type_unit_groups != NULL)
8908 {
8909 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8910 build_type_psymtab_dependencies, dwarf2_per_objfile);
8911 }
8912
8913 if (dwarf_read_debug)
8914 print_tu_stats (dwarf2_per_objfile);
8915
8916 set_partial_user (dwarf2_per_objfile);
8917
8918 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8919 &objfile->objfile_obstack);
8920 /* At this point we want to keep the address map. */
8921 save_psymtabs_addrmap.release ();
8922
8923 do_cleanups (back_to);
8924
8925 if (dwarf_read_debug)
8926 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8927 objfile_name (objfile));
8928 }
8929
8930 /* die_reader_func for load_partial_comp_unit. */
8931
8932 static void
8933 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8934 const gdb_byte *info_ptr,
8935 struct die_info *comp_unit_die,
8936 int has_children,
8937 void *data)
8938 {
8939 struct dwarf2_cu *cu = reader->cu;
8940
8941 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8942
8943 /* Check if comp unit has_children.
8944 If so, read the rest of the partial symbols from this comp unit.
8945 If not, there's no more debug_info for this comp unit. */
8946 if (has_children)
8947 load_partial_dies (reader, info_ptr, 0);
8948 }
8949
8950 /* Load the partial DIEs for a secondary CU into memory.
8951 This is also used when rereading a primary CU with load_all_dies. */
8952
8953 static void
8954 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8955 {
8956 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8957 load_partial_comp_unit_reader, NULL);
8958 }
8959
8960 static void
8961 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8962 struct dwarf2_section_info *section,
8963 struct dwarf2_section_info *abbrev_section,
8964 unsigned int is_dwz,
8965 int *n_allocated,
8966 int *n_comp_units,
8967 struct dwarf2_per_cu_data ***all_comp_units)
8968 {
8969 const gdb_byte *info_ptr;
8970 struct objfile *objfile = dwarf2_per_objfile->objfile;
8971
8972 if (dwarf_read_debug)
8973 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8974 get_section_name (section),
8975 get_section_file_name (section));
8976
8977 dwarf2_read_section (objfile, section);
8978
8979 info_ptr = section->buffer;
8980
8981 while (info_ptr < section->buffer + section->size)
8982 {
8983 struct dwarf2_per_cu_data *this_cu;
8984
8985 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8986
8987 comp_unit_head cu_header;
8988 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8989 abbrev_section, info_ptr,
8990 rcuh_kind::COMPILE);
8991
8992 /* Save the compilation unit for later lookup. */
8993 if (cu_header.unit_type != DW_UT_type)
8994 {
8995 this_cu = XOBNEW (&objfile->objfile_obstack,
8996 struct dwarf2_per_cu_data);
8997 memset (this_cu, 0, sizeof (*this_cu));
8998 }
8999 else
9000 {
9001 auto sig_type = XOBNEW (&objfile->objfile_obstack,
9002 struct signatured_type);
9003 memset (sig_type, 0, sizeof (*sig_type));
9004 sig_type->signature = cu_header.signature;
9005 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
9006 this_cu = &sig_type->per_cu;
9007 }
9008 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9009 this_cu->sect_off = sect_off;
9010 this_cu->length = cu_header.length + cu_header.initial_length_size;
9011 this_cu->is_dwz = is_dwz;
9012 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
9013 this_cu->section = section;
9014
9015 if (*n_comp_units == *n_allocated)
9016 {
9017 *n_allocated *= 2;
9018 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
9019 *all_comp_units, *n_allocated);
9020 }
9021 (*all_comp_units)[*n_comp_units] = this_cu;
9022 ++*n_comp_units;
9023
9024 info_ptr = info_ptr + this_cu->length;
9025 }
9026 }
9027
9028 /* Create a list of all compilation units in OBJFILE.
9029 This is only done for -readnow and building partial symtabs. */
9030
9031 static void
9032 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
9033 {
9034 int n_allocated;
9035 int n_comp_units;
9036 struct dwarf2_per_cu_data **all_comp_units;
9037 struct dwz_file *dwz;
9038 struct objfile *objfile = dwarf2_per_objfile->objfile;
9039
9040 n_comp_units = 0;
9041 n_allocated = 10;
9042 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
9043
9044 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
9045 &dwarf2_per_objfile->abbrev, 0,
9046 &n_allocated, &n_comp_units, &all_comp_units);
9047
9048 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
9049 if (dwz != NULL)
9050 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
9051 1, &n_allocated, &n_comp_units,
9052 &all_comp_units);
9053
9054 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
9055 struct dwarf2_per_cu_data *,
9056 n_comp_units);
9057 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
9058 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
9059 xfree (all_comp_units);
9060 dwarf2_per_objfile->n_comp_units = n_comp_units;
9061 }
9062
9063 /* Process all loaded DIEs for compilation unit CU, starting at
9064 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
9065 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
9066 DW_AT_ranges). See the comments of add_partial_subprogram on how
9067 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
9068
9069 static void
9070 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
9071 CORE_ADDR *highpc, int set_addrmap,
9072 struct dwarf2_cu *cu)
9073 {
9074 struct partial_die_info *pdi;
9075
9076 /* Now, march along the PDI's, descending into ones which have
9077 interesting children but skipping the children of the other ones,
9078 until we reach the end of the compilation unit. */
9079
9080 pdi = first_die;
9081
9082 while (pdi != NULL)
9083 {
9084 pdi->fixup (cu);
9085
9086 /* Anonymous namespaces or modules have no name but have interesting
9087 children, so we need to look at them. Ditto for anonymous
9088 enums. */
9089
9090 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
9091 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
9092 || pdi->tag == DW_TAG_imported_unit
9093 || pdi->tag == DW_TAG_inlined_subroutine)
9094 {
9095 switch (pdi->tag)
9096 {
9097 case DW_TAG_subprogram:
9098 case DW_TAG_inlined_subroutine:
9099 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9100 break;
9101 case DW_TAG_constant:
9102 case DW_TAG_variable:
9103 case DW_TAG_typedef:
9104 case DW_TAG_union_type:
9105 if (!pdi->is_declaration)
9106 {
9107 add_partial_symbol (pdi, cu);
9108 }
9109 break;
9110 case DW_TAG_class_type:
9111 case DW_TAG_interface_type:
9112 case DW_TAG_structure_type:
9113 if (!pdi->is_declaration)
9114 {
9115 add_partial_symbol (pdi, cu);
9116 }
9117 if ((cu->language == language_rust
9118 || cu->language == language_cplus) && pdi->has_children)
9119 scan_partial_symbols (pdi->die_child, lowpc, highpc,
9120 set_addrmap, cu);
9121 break;
9122 case DW_TAG_enumeration_type:
9123 if (!pdi->is_declaration)
9124 add_partial_enumeration (pdi, cu);
9125 break;
9126 case DW_TAG_base_type:
9127 case DW_TAG_subrange_type:
9128 /* File scope base type definitions are added to the partial
9129 symbol table. */
9130 add_partial_symbol (pdi, cu);
9131 break;
9132 case DW_TAG_namespace:
9133 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
9134 break;
9135 case DW_TAG_module:
9136 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
9137 break;
9138 case DW_TAG_imported_unit:
9139 {
9140 struct dwarf2_per_cu_data *per_cu;
9141
9142 /* For now we don't handle imported units in type units. */
9143 if (cu->per_cu->is_debug_types)
9144 {
9145 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9146 " supported in type units [in module %s]"),
9147 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9148 }
9149
9150 per_cu = dwarf2_find_containing_comp_unit
9151 (pdi->d.sect_off, pdi->is_dwz,
9152 cu->per_cu->dwarf2_per_objfile);
9153
9154 /* Go read the partial unit, if needed. */
9155 if (per_cu->v.psymtab == NULL)
9156 process_psymtab_comp_unit (per_cu, 1, cu->language);
9157
9158 VEC_safe_push (dwarf2_per_cu_ptr,
9159 cu->per_cu->imported_symtabs, per_cu);
9160 }
9161 break;
9162 case DW_TAG_imported_declaration:
9163 add_partial_symbol (pdi, cu);
9164 break;
9165 default:
9166 break;
9167 }
9168 }
9169
9170 /* If the die has a sibling, skip to the sibling. */
9171
9172 pdi = pdi->die_sibling;
9173 }
9174 }
9175
9176 /* Functions used to compute the fully scoped name of a partial DIE.
9177
9178 Normally, this is simple. For C++, the parent DIE's fully scoped
9179 name is concatenated with "::" and the partial DIE's name.
9180 Enumerators are an exception; they use the scope of their parent
9181 enumeration type, i.e. the name of the enumeration type is not
9182 prepended to the enumerator.
9183
9184 There are two complexities. One is DW_AT_specification; in this
9185 case "parent" means the parent of the target of the specification,
9186 instead of the direct parent of the DIE. The other is compilers
9187 which do not emit DW_TAG_namespace; in this case we try to guess
9188 the fully qualified name of structure types from their members'
9189 linkage names. This must be done using the DIE's children rather
9190 than the children of any DW_AT_specification target. We only need
9191 to do this for structures at the top level, i.e. if the target of
9192 any DW_AT_specification (if any; otherwise the DIE itself) does not
9193 have a parent. */
9194
9195 /* Compute the scope prefix associated with PDI's parent, in
9196 compilation unit CU. The result will be allocated on CU's
9197 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9198 field. NULL is returned if no prefix is necessary. */
9199 static const char *
9200 partial_die_parent_scope (struct partial_die_info *pdi,
9201 struct dwarf2_cu *cu)
9202 {
9203 const char *grandparent_scope;
9204 struct partial_die_info *parent, *real_pdi;
9205
9206 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9207 then this means the parent of the specification DIE. */
9208
9209 real_pdi = pdi;
9210 while (real_pdi->has_specification)
9211 real_pdi = find_partial_die (real_pdi->spec_offset,
9212 real_pdi->spec_is_dwz, cu);
9213
9214 parent = real_pdi->die_parent;
9215 if (parent == NULL)
9216 return NULL;
9217
9218 if (parent->scope_set)
9219 return parent->scope;
9220
9221 parent->fixup (cu);
9222
9223 grandparent_scope = partial_die_parent_scope (parent, cu);
9224
9225 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9226 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9227 Work around this problem here. */
9228 if (cu->language == language_cplus
9229 && parent->tag == DW_TAG_namespace
9230 && strcmp (parent->name, "::") == 0
9231 && grandparent_scope == NULL)
9232 {
9233 parent->scope = NULL;
9234 parent->scope_set = 1;
9235 return NULL;
9236 }
9237
9238 if (pdi->tag == DW_TAG_enumerator)
9239 /* Enumerators should not get the name of the enumeration as a prefix. */
9240 parent->scope = grandparent_scope;
9241 else if (parent->tag == DW_TAG_namespace
9242 || parent->tag == DW_TAG_module
9243 || parent->tag == DW_TAG_structure_type
9244 || parent->tag == DW_TAG_class_type
9245 || parent->tag == DW_TAG_interface_type
9246 || parent->tag == DW_TAG_union_type
9247 || parent->tag == DW_TAG_enumeration_type)
9248 {
9249 if (grandparent_scope == NULL)
9250 parent->scope = parent->name;
9251 else
9252 parent->scope = typename_concat (&cu->comp_unit_obstack,
9253 grandparent_scope,
9254 parent->name, 0, cu);
9255 }
9256 else
9257 {
9258 /* FIXME drow/2004-04-01: What should we be doing with
9259 function-local names? For partial symbols, we should probably be
9260 ignoring them. */
9261 complaint (&symfile_complaints,
9262 _("unhandled containing DIE tag %d for DIE at %s"),
9263 parent->tag, sect_offset_str (pdi->sect_off));
9264 parent->scope = grandparent_scope;
9265 }
9266
9267 parent->scope_set = 1;
9268 return parent->scope;
9269 }
9270
9271 /* Return the fully scoped name associated with PDI, from compilation unit
9272 CU. The result will be allocated with malloc. */
9273
9274 static char *
9275 partial_die_full_name (struct partial_die_info *pdi,
9276 struct dwarf2_cu *cu)
9277 {
9278 const char *parent_scope;
9279
9280 /* If this is a template instantiation, we can not work out the
9281 template arguments from partial DIEs. So, unfortunately, we have
9282 to go through the full DIEs. At least any work we do building
9283 types here will be reused if full symbols are loaded later. */
9284 if (pdi->has_template_arguments)
9285 {
9286 pdi->fixup (cu);
9287
9288 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9289 {
9290 struct die_info *die;
9291 struct attribute attr;
9292 struct dwarf2_cu *ref_cu = cu;
9293
9294 /* DW_FORM_ref_addr is using section offset. */
9295 attr.name = (enum dwarf_attribute) 0;
9296 attr.form = DW_FORM_ref_addr;
9297 attr.u.unsnd = to_underlying (pdi->sect_off);
9298 die = follow_die_ref (NULL, &attr, &ref_cu);
9299
9300 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9301 }
9302 }
9303
9304 parent_scope = partial_die_parent_scope (pdi, cu);
9305 if (parent_scope == NULL)
9306 return NULL;
9307 else
9308 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
9309 }
9310
9311 static void
9312 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
9313 {
9314 struct dwarf2_per_objfile *dwarf2_per_objfile
9315 = cu->per_cu->dwarf2_per_objfile;
9316 struct objfile *objfile = dwarf2_per_objfile->objfile;
9317 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9318 CORE_ADDR addr = 0;
9319 const char *actual_name = NULL;
9320 CORE_ADDR baseaddr;
9321 char *built_actual_name;
9322
9323 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9324
9325 built_actual_name = partial_die_full_name (pdi, cu);
9326 if (built_actual_name != NULL)
9327 actual_name = built_actual_name;
9328
9329 if (actual_name == NULL)
9330 actual_name = pdi->name;
9331
9332 switch (pdi->tag)
9333 {
9334 case DW_TAG_inlined_subroutine:
9335 case DW_TAG_subprogram:
9336 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
9337 if (pdi->is_external || cu->language == language_ada)
9338 {
9339 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9340 of the global scope. But in Ada, we want to be able to access
9341 nested procedures globally. So all Ada subprograms are stored
9342 in the global scope. */
9343 add_psymbol_to_list (actual_name, strlen (actual_name),
9344 built_actual_name != NULL,
9345 VAR_DOMAIN, LOC_BLOCK,
9346 &objfile->global_psymbols,
9347 addr, cu->language, objfile);
9348 }
9349 else
9350 {
9351 add_psymbol_to_list (actual_name, strlen (actual_name),
9352 built_actual_name != NULL,
9353 VAR_DOMAIN, LOC_BLOCK,
9354 &objfile->static_psymbols,
9355 addr, cu->language, objfile);
9356 }
9357
9358 if (pdi->main_subprogram && actual_name != NULL)
9359 set_objfile_main_name (objfile, actual_name, cu->language);
9360 break;
9361 case DW_TAG_constant:
9362 {
9363 std::vector<partial_symbol *> *list;
9364
9365 if (pdi->is_external)
9366 list = &objfile->global_psymbols;
9367 else
9368 list = &objfile->static_psymbols;
9369 add_psymbol_to_list (actual_name, strlen (actual_name),
9370 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9371 list, 0, cu->language, objfile);
9372 }
9373 break;
9374 case DW_TAG_variable:
9375 if (pdi->d.locdesc)
9376 addr = decode_locdesc (pdi->d.locdesc, cu);
9377
9378 if (pdi->d.locdesc
9379 && addr == 0
9380 && !dwarf2_per_objfile->has_section_at_zero)
9381 {
9382 /* A global or static variable may also have been stripped
9383 out by the linker if unused, in which case its address
9384 will be nullified; do not add such variables into partial
9385 symbol table then. */
9386 }
9387 else if (pdi->is_external)
9388 {
9389 /* Global Variable.
9390 Don't enter into the minimal symbol tables as there is
9391 a minimal symbol table entry from the ELF symbols already.
9392 Enter into partial symbol table if it has a location
9393 descriptor or a type.
9394 If the location descriptor is missing, new_symbol will create
9395 a LOC_UNRESOLVED symbol, the address of the variable will then
9396 be determined from the minimal symbol table whenever the variable
9397 is referenced.
9398 The address for the partial symbol table entry is not
9399 used by GDB, but it comes in handy for debugging partial symbol
9400 table building. */
9401
9402 if (pdi->d.locdesc || pdi->has_type)
9403 add_psymbol_to_list (actual_name, strlen (actual_name),
9404 built_actual_name != NULL,
9405 VAR_DOMAIN, LOC_STATIC,
9406 &objfile->global_psymbols,
9407 addr + baseaddr,
9408 cu->language, objfile);
9409 }
9410 else
9411 {
9412 int has_loc = pdi->d.locdesc != NULL;
9413
9414 /* Static Variable. Skip symbols whose value we cannot know (those
9415 without location descriptors or constant values). */
9416 if (!has_loc && !pdi->has_const_value)
9417 {
9418 xfree (built_actual_name);
9419 return;
9420 }
9421
9422 add_psymbol_to_list (actual_name, strlen (actual_name),
9423 built_actual_name != NULL,
9424 VAR_DOMAIN, LOC_STATIC,
9425 &objfile->static_psymbols,
9426 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
9427 cu->language, objfile);
9428 }
9429 break;
9430 case DW_TAG_typedef:
9431 case DW_TAG_base_type:
9432 case DW_TAG_subrange_type:
9433 add_psymbol_to_list (actual_name, strlen (actual_name),
9434 built_actual_name != NULL,
9435 VAR_DOMAIN, LOC_TYPEDEF,
9436 &objfile->static_psymbols,
9437 0, cu->language, objfile);
9438 break;
9439 case DW_TAG_imported_declaration:
9440 case DW_TAG_namespace:
9441 add_psymbol_to_list (actual_name, strlen (actual_name),
9442 built_actual_name != NULL,
9443 VAR_DOMAIN, LOC_TYPEDEF,
9444 &objfile->global_psymbols,
9445 0, cu->language, objfile);
9446 break;
9447 case DW_TAG_module:
9448 add_psymbol_to_list (actual_name, strlen (actual_name),
9449 built_actual_name != NULL,
9450 MODULE_DOMAIN, LOC_TYPEDEF,
9451 &objfile->global_psymbols,
9452 0, cu->language, objfile);
9453 break;
9454 case DW_TAG_class_type:
9455 case DW_TAG_interface_type:
9456 case DW_TAG_structure_type:
9457 case DW_TAG_union_type:
9458 case DW_TAG_enumeration_type:
9459 /* Skip external references. The DWARF standard says in the section
9460 about "Structure, Union, and Class Type Entries": "An incomplete
9461 structure, union or class type is represented by a structure,
9462 union or class entry that does not have a byte size attribute
9463 and that has a DW_AT_declaration attribute." */
9464 if (!pdi->has_byte_size && pdi->is_declaration)
9465 {
9466 xfree (built_actual_name);
9467 return;
9468 }
9469
9470 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9471 static vs. global. */
9472 add_psymbol_to_list (actual_name, strlen (actual_name),
9473 built_actual_name != NULL,
9474 STRUCT_DOMAIN, LOC_TYPEDEF,
9475 cu->language == language_cplus
9476 ? &objfile->global_psymbols
9477 : &objfile->static_psymbols,
9478 0, cu->language, objfile);
9479
9480 break;
9481 case DW_TAG_enumerator:
9482 add_psymbol_to_list (actual_name, strlen (actual_name),
9483 built_actual_name != NULL,
9484 VAR_DOMAIN, LOC_CONST,
9485 cu->language == language_cplus
9486 ? &objfile->global_psymbols
9487 : &objfile->static_psymbols,
9488 0, cu->language, objfile);
9489 break;
9490 default:
9491 break;
9492 }
9493
9494 xfree (built_actual_name);
9495 }
9496
9497 /* Read a partial die corresponding to a namespace; also, add a symbol
9498 corresponding to that namespace to the symbol table. NAMESPACE is
9499 the name of the enclosing namespace. */
9500
9501 static void
9502 add_partial_namespace (struct partial_die_info *pdi,
9503 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9504 int set_addrmap, struct dwarf2_cu *cu)
9505 {
9506 /* Add a symbol for the namespace. */
9507
9508 add_partial_symbol (pdi, cu);
9509
9510 /* Now scan partial symbols in that namespace. */
9511
9512 if (pdi->has_children)
9513 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9514 }
9515
9516 /* Read a partial die corresponding to a Fortran module. */
9517
9518 static void
9519 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9520 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9521 {
9522 /* Add a symbol for the namespace. */
9523
9524 add_partial_symbol (pdi, cu);
9525
9526 /* Now scan partial symbols in that module. */
9527
9528 if (pdi->has_children)
9529 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9530 }
9531
9532 /* Read a partial die corresponding to a subprogram or an inlined
9533 subprogram and create a partial symbol for that subprogram.
9534 When the CU language allows it, this routine also defines a partial
9535 symbol for each nested subprogram that this subprogram contains.
9536 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9537 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9538
9539 PDI may also be a lexical block, in which case we simply search
9540 recursively for subprograms defined inside that lexical block.
9541 Again, this is only performed when the CU language allows this
9542 type of definitions. */
9543
9544 static void
9545 add_partial_subprogram (struct partial_die_info *pdi,
9546 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9547 int set_addrmap, struct dwarf2_cu *cu)
9548 {
9549 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9550 {
9551 if (pdi->has_pc_info)
9552 {
9553 if (pdi->lowpc < *lowpc)
9554 *lowpc = pdi->lowpc;
9555 if (pdi->highpc > *highpc)
9556 *highpc = pdi->highpc;
9557 if (set_addrmap)
9558 {
9559 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9560 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9561 CORE_ADDR baseaddr;
9562 CORE_ADDR highpc;
9563 CORE_ADDR lowpc;
9564
9565 baseaddr = ANOFFSET (objfile->section_offsets,
9566 SECT_OFF_TEXT (objfile));
9567 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9568 pdi->lowpc + baseaddr);
9569 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9570 pdi->highpc + baseaddr);
9571 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9572 cu->per_cu->v.psymtab);
9573 }
9574 }
9575
9576 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9577 {
9578 if (!pdi->is_declaration)
9579 /* Ignore subprogram DIEs that do not have a name, they are
9580 illegal. Do not emit a complaint at this point, we will
9581 do so when we convert this psymtab into a symtab. */
9582 if (pdi->name)
9583 add_partial_symbol (pdi, cu);
9584 }
9585 }
9586
9587 if (! pdi->has_children)
9588 return;
9589
9590 if (cu->language == language_ada)
9591 {
9592 pdi = pdi->die_child;
9593 while (pdi != NULL)
9594 {
9595 pdi->fixup (cu);
9596 if (pdi->tag == DW_TAG_subprogram
9597 || pdi->tag == DW_TAG_inlined_subroutine
9598 || pdi->tag == DW_TAG_lexical_block)
9599 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9600 pdi = pdi->die_sibling;
9601 }
9602 }
9603 }
9604
9605 /* Read a partial die corresponding to an enumeration type. */
9606
9607 static void
9608 add_partial_enumeration (struct partial_die_info *enum_pdi,
9609 struct dwarf2_cu *cu)
9610 {
9611 struct partial_die_info *pdi;
9612
9613 if (enum_pdi->name != NULL)
9614 add_partial_symbol (enum_pdi, cu);
9615
9616 pdi = enum_pdi->die_child;
9617 while (pdi)
9618 {
9619 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9620 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9621 else
9622 add_partial_symbol (pdi, cu);
9623 pdi = pdi->die_sibling;
9624 }
9625 }
9626
9627 /* Return the initial uleb128 in the die at INFO_PTR. */
9628
9629 static unsigned int
9630 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9631 {
9632 unsigned int bytes_read;
9633
9634 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9635 }
9636
9637 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9638 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9639
9640 Return the corresponding abbrev, or NULL if the number is zero (indicating
9641 an empty DIE). In either case *BYTES_READ will be set to the length of
9642 the initial number. */
9643
9644 static struct abbrev_info *
9645 peek_die_abbrev (const die_reader_specs &reader,
9646 const gdb_byte *info_ptr, unsigned int *bytes_read)
9647 {
9648 dwarf2_cu *cu = reader.cu;
9649 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9650 unsigned int abbrev_number
9651 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9652
9653 if (abbrev_number == 0)
9654 return NULL;
9655
9656 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9657 if (!abbrev)
9658 {
9659 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9660 " at offset %s [in module %s]"),
9661 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9662 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9663 }
9664
9665 return abbrev;
9666 }
9667
9668 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9669 Returns a pointer to the end of a series of DIEs, terminated by an empty
9670 DIE. Any children of the skipped DIEs will also be skipped. */
9671
9672 static const gdb_byte *
9673 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9674 {
9675 while (1)
9676 {
9677 unsigned int bytes_read;
9678 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9679
9680 if (abbrev == NULL)
9681 return info_ptr + bytes_read;
9682 else
9683 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9684 }
9685 }
9686
9687 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9688 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9689 abbrev corresponding to that skipped uleb128 should be passed in
9690 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9691 children. */
9692
9693 static const gdb_byte *
9694 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9695 struct abbrev_info *abbrev)
9696 {
9697 unsigned int bytes_read;
9698 struct attribute attr;
9699 bfd *abfd = reader->abfd;
9700 struct dwarf2_cu *cu = reader->cu;
9701 const gdb_byte *buffer = reader->buffer;
9702 const gdb_byte *buffer_end = reader->buffer_end;
9703 unsigned int form, i;
9704
9705 for (i = 0; i < abbrev->num_attrs; i++)
9706 {
9707 /* The only abbrev we care about is DW_AT_sibling. */
9708 if (abbrev->attrs[i].name == DW_AT_sibling)
9709 {
9710 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9711 if (attr.form == DW_FORM_ref_addr)
9712 complaint (&symfile_complaints,
9713 _("ignoring absolute DW_AT_sibling"));
9714 else
9715 {
9716 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9717 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9718
9719 if (sibling_ptr < info_ptr)
9720 complaint (&symfile_complaints,
9721 _("DW_AT_sibling points backwards"));
9722 else if (sibling_ptr > reader->buffer_end)
9723 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9724 else
9725 return sibling_ptr;
9726 }
9727 }
9728
9729 /* If it isn't DW_AT_sibling, skip this attribute. */
9730 form = abbrev->attrs[i].form;
9731 skip_attribute:
9732 switch (form)
9733 {
9734 case DW_FORM_ref_addr:
9735 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9736 and later it is offset sized. */
9737 if (cu->header.version == 2)
9738 info_ptr += cu->header.addr_size;
9739 else
9740 info_ptr += cu->header.offset_size;
9741 break;
9742 case DW_FORM_GNU_ref_alt:
9743 info_ptr += cu->header.offset_size;
9744 break;
9745 case DW_FORM_addr:
9746 info_ptr += cu->header.addr_size;
9747 break;
9748 case DW_FORM_data1:
9749 case DW_FORM_ref1:
9750 case DW_FORM_flag:
9751 info_ptr += 1;
9752 break;
9753 case DW_FORM_flag_present:
9754 case DW_FORM_implicit_const:
9755 break;
9756 case DW_FORM_data2:
9757 case DW_FORM_ref2:
9758 info_ptr += 2;
9759 break;
9760 case DW_FORM_data4:
9761 case DW_FORM_ref4:
9762 info_ptr += 4;
9763 break;
9764 case DW_FORM_data8:
9765 case DW_FORM_ref8:
9766 case DW_FORM_ref_sig8:
9767 info_ptr += 8;
9768 break;
9769 case DW_FORM_data16:
9770 info_ptr += 16;
9771 break;
9772 case DW_FORM_string:
9773 read_direct_string (abfd, info_ptr, &bytes_read);
9774 info_ptr += bytes_read;
9775 break;
9776 case DW_FORM_sec_offset:
9777 case DW_FORM_strp:
9778 case DW_FORM_GNU_strp_alt:
9779 info_ptr += cu->header.offset_size;
9780 break;
9781 case DW_FORM_exprloc:
9782 case DW_FORM_block:
9783 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9784 info_ptr += bytes_read;
9785 break;
9786 case DW_FORM_block1:
9787 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9788 break;
9789 case DW_FORM_block2:
9790 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9791 break;
9792 case DW_FORM_block4:
9793 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9794 break;
9795 case DW_FORM_sdata:
9796 case DW_FORM_udata:
9797 case DW_FORM_ref_udata:
9798 case DW_FORM_GNU_addr_index:
9799 case DW_FORM_GNU_str_index:
9800 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9801 break;
9802 case DW_FORM_indirect:
9803 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9804 info_ptr += bytes_read;
9805 /* We need to continue parsing from here, so just go back to
9806 the top. */
9807 goto skip_attribute;
9808
9809 default:
9810 error (_("Dwarf Error: Cannot handle %s "
9811 "in DWARF reader [in module %s]"),
9812 dwarf_form_name (form),
9813 bfd_get_filename (abfd));
9814 }
9815 }
9816
9817 if (abbrev->has_children)
9818 return skip_children (reader, info_ptr);
9819 else
9820 return info_ptr;
9821 }
9822
9823 /* Locate ORIG_PDI's sibling.
9824 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9825
9826 static const gdb_byte *
9827 locate_pdi_sibling (const struct die_reader_specs *reader,
9828 struct partial_die_info *orig_pdi,
9829 const gdb_byte *info_ptr)
9830 {
9831 /* Do we know the sibling already? */
9832
9833 if (orig_pdi->sibling)
9834 return orig_pdi->sibling;
9835
9836 /* Are there any children to deal with? */
9837
9838 if (!orig_pdi->has_children)
9839 return info_ptr;
9840
9841 /* Skip the children the long way. */
9842
9843 return skip_children (reader, info_ptr);
9844 }
9845
9846 /* Expand this partial symbol table into a full symbol table. SELF is
9847 not NULL. */
9848
9849 static void
9850 dwarf2_read_symtab (struct partial_symtab *self,
9851 struct objfile *objfile)
9852 {
9853 struct dwarf2_per_objfile *dwarf2_per_objfile
9854 = get_dwarf2_per_objfile (objfile);
9855
9856 if (self->readin)
9857 {
9858 warning (_("bug: psymtab for %s is already read in."),
9859 self->filename);
9860 }
9861 else
9862 {
9863 if (info_verbose)
9864 {
9865 printf_filtered (_("Reading in symbols for %s..."),
9866 self->filename);
9867 gdb_flush (gdb_stdout);
9868 }
9869
9870 /* If this psymtab is constructed from a debug-only objfile, the
9871 has_section_at_zero flag will not necessarily be correct. We
9872 can get the correct value for this flag by looking at the data
9873 associated with the (presumably stripped) associated objfile. */
9874 if (objfile->separate_debug_objfile_backlink)
9875 {
9876 struct dwarf2_per_objfile *dpo_backlink
9877 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9878
9879 dwarf2_per_objfile->has_section_at_zero
9880 = dpo_backlink->has_section_at_zero;
9881 }
9882
9883 dwarf2_per_objfile->reading_partial_symbols = 0;
9884
9885 psymtab_to_symtab_1 (self);
9886
9887 /* Finish up the debug error message. */
9888 if (info_verbose)
9889 printf_filtered (_("done.\n"));
9890 }
9891
9892 process_cu_includes (dwarf2_per_objfile);
9893 }
9894 \f
9895 /* Reading in full CUs. */
9896
9897 /* Add PER_CU to the queue. */
9898
9899 static void
9900 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9901 enum language pretend_language)
9902 {
9903 struct dwarf2_queue_item *item;
9904
9905 per_cu->queued = 1;
9906 item = XNEW (struct dwarf2_queue_item);
9907 item->per_cu = per_cu;
9908 item->pretend_language = pretend_language;
9909 item->next = NULL;
9910
9911 if (dwarf2_queue == NULL)
9912 dwarf2_queue = item;
9913 else
9914 dwarf2_queue_tail->next = item;
9915
9916 dwarf2_queue_tail = item;
9917 }
9918
9919 /* If PER_CU is not yet queued, add it to the queue.
9920 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9921 dependency.
9922 The result is non-zero if PER_CU was queued, otherwise the result is zero
9923 meaning either PER_CU is already queued or it is already loaded.
9924
9925 N.B. There is an invariant here that if a CU is queued then it is loaded.
9926 The caller is required to load PER_CU if we return non-zero. */
9927
9928 static int
9929 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9930 struct dwarf2_per_cu_data *per_cu,
9931 enum language pretend_language)
9932 {
9933 /* We may arrive here during partial symbol reading, if we need full
9934 DIEs to process an unusual case (e.g. template arguments). Do
9935 not queue PER_CU, just tell our caller to load its DIEs. */
9936 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9937 {
9938 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9939 return 1;
9940 return 0;
9941 }
9942
9943 /* Mark the dependence relation so that we don't flush PER_CU
9944 too early. */
9945 if (dependent_cu != NULL)
9946 dwarf2_add_dependence (dependent_cu, per_cu);
9947
9948 /* If it's already on the queue, we have nothing to do. */
9949 if (per_cu->queued)
9950 return 0;
9951
9952 /* If the compilation unit is already loaded, just mark it as
9953 used. */
9954 if (per_cu->cu != NULL)
9955 {
9956 per_cu->cu->last_used = 0;
9957 return 0;
9958 }
9959
9960 /* Add it to the queue. */
9961 queue_comp_unit (per_cu, pretend_language);
9962
9963 return 1;
9964 }
9965
9966 /* Process the queue. */
9967
9968 static void
9969 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9970 {
9971 struct dwarf2_queue_item *item, *next_item;
9972
9973 if (dwarf_read_debug)
9974 {
9975 fprintf_unfiltered (gdb_stdlog,
9976 "Expanding one or more symtabs of objfile %s ...\n",
9977 objfile_name (dwarf2_per_objfile->objfile));
9978 }
9979
9980 /* The queue starts out with one item, but following a DIE reference
9981 may load a new CU, adding it to the end of the queue. */
9982 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9983 {
9984 if ((dwarf2_per_objfile->using_index
9985 ? !item->per_cu->v.quick->compunit_symtab
9986 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9987 /* Skip dummy CUs. */
9988 && item->per_cu->cu != NULL)
9989 {
9990 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9991 unsigned int debug_print_threshold;
9992 char buf[100];
9993
9994 if (per_cu->is_debug_types)
9995 {
9996 struct signatured_type *sig_type =
9997 (struct signatured_type *) per_cu;
9998
9999 sprintf (buf, "TU %s at offset %s",
10000 hex_string (sig_type->signature),
10001 sect_offset_str (per_cu->sect_off));
10002 /* There can be 100s of TUs.
10003 Only print them in verbose mode. */
10004 debug_print_threshold = 2;
10005 }
10006 else
10007 {
10008 sprintf (buf, "CU at offset %s",
10009 sect_offset_str (per_cu->sect_off));
10010 debug_print_threshold = 1;
10011 }
10012
10013 if (dwarf_read_debug >= debug_print_threshold)
10014 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
10015
10016 if (per_cu->is_debug_types)
10017 process_full_type_unit (per_cu, item->pretend_language);
10018 else
10019 process_full_comp_unit (per_cu, item->pretend_language);
10020
10021 if (dwarf_read_debug >= debug_print_threshold)
10022 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
10023 }
10024
10025 item->per_cu->queued = 0;
10026 next_item = item->next;
10027 xfree (item);
10028 }
10029
10030 dwarf2_queue_tail = NULL;
10031
10032 if (dwarf_read_debug)
10033 {
10034 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
10035 objfile_name (dwarf2_per_objfile->objfile));
10036 }
10037 }
10038
10039 /* Read in full symbols for PST, and anything it depends on. */
10040
10041 static void
10042 psymtab_to_symtab_1 (struct partial_symtab *pst)
10043 {
10044 struct dwarf2_per_cu_data *per_cu;
10045 int i;
10046
10047 if (pst->readin)
10048 return;
10049
10050 for (i = 0; i < pst->number_of_dependencies; i++)
10051 if (!pst->dependencies[i]->readin
10052 && pst->dependencies[i]->user == NULL)
10053 {
10054 /* Inform about additional files that need to be read in. */
10055 if (info_verbose)
10056 {
10057 /* FIXME: i18n: Need to make this a single string. */
10058 fputs_filtered (" ", gdb_stdout);
10059 wrap_here ("");
10060 fputs_filtered ("and ", gdb_stdout);
10061 wrap_here ("");
10062 printf_filtered ("%s...", pst->dependencies[i]->filename);
10063 wrap_here (""); /* Flush output. */
10064 gdb_flush (gdb_stdout);
10065 }
10066 psymtab_to_symtab_1 (pst->dependencies[i]);
10067 }
10068
10069 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10070
10071 if (per_cu == NULL)
10072 {
10073 /* It's an include file, no symbols to read for it.
10074 Everything is in the parent symtab. */
10075 pst->readin = 1;
10076 return;
10077 }
10078
10079 dw2_do_instantiate_symtab (per_cu);
10080 }
10081
10082 /* Trivial hash function for die_info: the hash value of a DIE
10083 is its offset in .debug_info for this objfile. */
10084
10085 static hashval_t
10086 die_hash (const void *item)
10087 {
10088 const struct die_info *die = (const struct die_info *) item;
10089
10090 return to_underlying (die->sect_off);
10091 }
10092
10093 /* Trivial comparison function for die_info structures: two DIEs
10094 are equal if they have the same offset. */
10095
10096 static int
10097 die_eq (const void *item_lhs, const void *item_rhs)
10098 {
10099 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
10100 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
10101
10102 return die_lhs->sect_off == die_rhs->sect_off;
10103 }
10104
10105 /* die_reader_func for load_full_comp_unit.
10106 This is identical to read_signatured_type_reader,
10107 but is kept separate for now. */
10108
10109 static void
10110 load_full_comp_unit_reader (const struct die_reader_specs *reader,
10111 const gdb_byte *info_ptr,
10112 struct die_info *comp_unit_die,
10113 int has_children,
10114 void *data)
10115 {
10116 struct dwarf2_cu *cu = reader->cu;
10117 enum language *language_ptr = (enum language *) data;
10118
10119 gdb_assert (cu->die_hash == NULL);
10120 cu->die_hash =
10121 htab_create_alloc_ex (cu->header.length / 12,
10122 die_hash,
10123 die_eq,
10124 NULL,
10125 &cu->comp_unit_obstack,
10126 hashtab_obstack_allocate,
10127 dummy_obstack_deallocate);
10128
10129 if (has_children)
10130 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10131 &info_ptr, comp_unit_die);
10132 cu->dies = comp_unit_die;
10133 /* comp_unit_die is not stored in die_hash, no need. */
10134
10135 /* We try not to read any attributes in this function, because not
10136 all CUs needed for references have been loaded yet, and symbol
10137 table processing isn't initialized. But we have to set the CU language,
10138 or we won't be able to build types correctly.
10139 Similarly, if we do not read the producer, we can not apply
10140 producer-specific interpretation. */
10141 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
10142 }
10143
10144 /* Load the DIEs associated with PER_CU into memory. */
10145
10146 static void
10147 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10148 enum language pretend_language)
10149 {
10150 gdb_assert (! this_cu->is_debug_types);
10151
10152 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10153 load_full_comp_unit_reader, &pretend_language);
10154 }
10155
10156 /* Add a DIE to the delayed physname list. */
10157
10158 static void
10159 add_to_method_list (struct type *type, int fnfield_index, int index,
10160 const char *name, struct die_info *die,
10161 struct dwarf2_cu *cu)
10162 {
10163 struct delayed_method_info mi;
10164 mi.type = type;
10165 mi.fnfield_index = fnfield_index;
10166 mi.index = index;
10167 mi.name = name;
10168 mi.die = die;
10169 cu->method_list.push_back (mi);
10170 }
10171
10172 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10173 "const" / "volatile". If so, decrements LEN by the length of the
10174 modifier and return true. Otherwise return false. */
10175
10176 template<size_t N>
10177 static bool
10178 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10179 {
10180 size_t mod_len = sizeof (mod) - 1;
10181 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10182 {
10183 len -= mod_len;
10184 return true;
10185 }
10186 return false;
10187 }
10188
10189 /* Compute the physnames of any methods on the CU's method list.
10190
10191 The computation of method physnames is delayed in order to avoid the
10192 (bad) condition that one of the method's formal parameters is of an as yet
10193 incomplete type. */
10194
10195 static void
10196 compute_delayed_physnames (struct dwarf2_cu *cu)
10197 {
10198 /* Only C++ delays computing physnames. */
10199 if (cu->method_list.empty ())
10200 return;
10201 gdb_assert (cu->language == language_cplus);
10202
10203 for (struct delayed_method_info &mi : cu->method_list)
10204 {
10205 const char *physname;
10206 struct fn_fieldlist *fn_flp
10207 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
10208 physname = dwarf2_physname (mi.name, mi.die, cu);
10209 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
10210 = physname ? physname : "";
10211
10212 /* Since there's no tag to indicate whether a method is a
10213 const/volatile overload, extract that information out of the
10214 demangled name. */
10215 if (physname != NULL)
10216 {
10217 size_t len = strlen (physname);
10218
10219 while (1)
10220 {
10221 if (physname[len] == ')') /* shortcut */
10222 break;
10223 else if (check_modifier (physname, len, " const"))
10224 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
10225 else if (check_modifier (physname, len, " volatile"))
10226 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
10227 else
10228 break;
10229 }
10230 }
10231 }
10232
10233 /* The list is no longer needed. */
10234 cu->method_list.clear ();
10235 }
10236
10237 /* Go objects should be embedded in a DW_TAG_module DIE,
10238 and it's not clear if/how imported objects will appear.
10239 To keep Go support simple until that's worked out,
10240 go back through what we've read and create something usable.
10241 We could do this while processing each DIE, and feels kinda cleaner,
10242 but that way is more invasive.
10243 This is to, for example, allow the user to type "p var" or "b main"
10244 without having to specify the package name, and allow lookups
10245 of module.object to work in contexts that use the expression
10246 parser. */
10247
10248 static void
10249 fixup_go_packaging (struct dwarf2_cu *cu)
10250 {
10251 char *package_name = NULL;
10252 struct pending *list;
10253 int i;
10254
10255 for (list = global_symbols; list != NULL; list = list->next)
10256 {
10257 for (i = 0; i < list->nsyms; ++i)
10258 {
10259 struct symbol *sym = list->symbol[i];
10260
10261 if (SYMBOL_LANGUAGE (sym) == language_go
10262 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10263 {
10264 char *this_package_name = go_symbol_package_name (sym);
10265
10266 if (this_package_name == NULL)
10267 continue;
10268 if (package_name == NULL)
10269 package_name = this_package_name;
10270 else
10271 {
10272 struct objfile *objfile
10273 = cu->per_cu->dwarf2_per_objfile->objfile;
10274 if (strcmp (package_name, this_package_name) != 0)
10275 complaint (&symfile_complaints,
10276 _("Symtab %s has objects from two different Go packages: %s and %s"),
10277 (symbol_symtab (sym) != NULL
10278 ? symtab_to_filename_for_display
10279 (symbol_symtab (sym))
10280 : objfile_name (objfile)),
10281 this_package_name, package_name);
10282 xfree (this_package_name);
10283 }
10284 }
10285 }
10286 }
10287
10288 if (package_name != NULL)
10289 {
10290 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10291 const char *saved_package_name
10292 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10293 package_name,
10294 strlen (package_name));
10295 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10296 saved_package_name);
10297 struct symbol *sym;
10298
10299 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10300
10301 sym = allocate_symbol (objfile);
10302 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
10303 SYMBOL_SET_NAMES (sym, saved_package_name,
10304 strlen (saved_package_name), 0, objfile);
10305 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10306 e.g., "main" finds the "main" module and not C's main(). */
10307 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10308 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
10309 SYMBOL_TYPE (sym) = type;
10310
10311 add_symbol_to_list (sym, &global_symbols);
10312
10313 xfree (package_name);
10314 }
10315 }
10316
10317 /* Allocate a fully-qualified name consisting of the two parts on the
10318 obstack. */
10319
10320 static const char *
10321 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
10322 {
10323 return obconcat (obstack, p1, "::", p2, (char *) NULL);
10324 }
10325
10326 /* A helper that allocates a struct discriminant_info to attach to a
10327 union type. */
10328
10329 static struct discriminant_info *
10330 alloc_discriminant_info (struct type *type, int discriminant_index,
10331 int default_index)
10332 {
10333 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10334 gdb_assert (discriminant_index == -1
10335 || (discriminant_index >= 0
10336 && discriminant_index < TYPE_NFIELDS (type)));
10337 gdb_assert (default_index == -1
10338 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
10339
10340 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
10341
10342 struct discriminant_info *disc
10343 = ((struct discriminant_info *)
10344 TYPE_ZALLOC (type,
10345 offsetof (struct discriminant_info, discriminants)
10346 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
10347 disc->default_index = default_index;
10348 disc->discriminant_index = discriminant_index;
10349
10350 struct dynamic_prop prop;
10351 prop.kind = PROP_UNDEFINED;
10352 prop.data.baton = disc;
10353
10354 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
10355
10356 return disc;
10357 }
10358
10359 /* Some versions of rustc emitted enums in an unusual way.
10360
10361 Ordinary enums were emitted as unions. The first element of each
10362 structure in the union was named "RUST$ENUM$DISR". This element
10363 held the discriminant.
10364
10365 These versions of Rust also implemented the "non-zero"
10366 optimization. When the enum had two values, and one is empty and
10367 the other holds a pointer that cannot be zero, the pointer is used
10368 as the discriminant, with a zero value meaning the empty variant.
10369 Here, the union's first member is of the form
10370 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10371 where the fieldnos are the indices of the fields that should be
10372 traversed in order to find the field (which may be several fields deep)
10373 and the variantname is the name of the variant of the case when the
10374 field is zero.
10375
10376 This function recognizes whether TYPE is of one of these forms,
10377 and, if so, smashes it to be a variant type. */
10378
10379 static void
10380 quirk_rust_enum (struct type *type, struct objfile *objfile)
10381 {
10382 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10383
10384 /* We don't need to deal with empty enums. */
10385 if (TYPE_NFIELDS (type) == 0)
10386 return;
10387
10388 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10389 if (TYPE_NFIELDS (type) == 1
10390 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10391 {
10392 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10393
10394 /* Decode the field name to find the offset of the
10395 discriminant. */
10396 ULONGEST bit_offset = 0;
10397 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10398 while (name[0] >= '0' && name[0] <= '9')
10399 {
10400 char *tail;
10401 unsigned long index = strtoul (name, &tail, 10);
10402 name = tail;
10403 if (*name != '$'
10404 || index >= TYPE_NFIELDS (field_type)
10405 || (TYPE_FIELD_LOC_KIND (field_type, index)
10406 != FIELD_LOC_KIND_BITPOS))
10407 {
10408 complaint (&symfile_complaints,
10409 _("Could not parse Rust enum encoding string \"%s\""
10410 "[in module %s]"),
10411 TYPE_FIELD_NAME (type, 0),
10412 objfile_name (objfile));
10413 return;
10414 }
10415 ++name;
10416
10417 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10418 field_type = TYPE_FIELD_TYPE (field_type, index);
10419 }
10420
10421 /* Make a union to hold the variants. */
10422 struct type *union_type = alloc_type (objfile);
10423 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10424 TYPE_NFIELDS (union_type) = 3;
10425 TYPE_FIELDS (union_type)
10426 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10427 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10428
10429 /* Put the discriminant must at index 0. */
10430 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10431 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10432 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10433 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10434
10435 /* The order of fields doesn't really matter, so put the real
10436 field at index 1 and the data-less field at index 2. */
10437 struct discriminant_info *disc
10438 = alloc_discriminant_info (union_type, 0, 1);
10439 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10440 TYPE_FIELD_NAME (union_type, 1)
10441 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10442 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10443 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10444 TYPE_FIELD_NAME (union_type, 1));
10445
10446 const char *dataless_name
10447 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10448 name);
10449 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10450 dataless_name);
10451 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10452 /* NAME points into the original discriminant name, which
10453 already has the correct lifetime. */
10454 TYPE_FIELD_NAME (union_type, 2) = name;
10455 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10456 disc->discriminants[2] = 0;
10457
10458 /* Smash this type to be a structure type. We have to do this
10459 because the type has already been recorded. */
10460 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10461 TYPE_NFIELDS (type) = 1;
10462 TYPE_FIELDS (type)
10463 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10464
10465 /* Install the variant part. */
10466 TYPE_FIELD_TYPE (type, 0) = union_type;
10467 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10468 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10469 }
10470 else if (TYPE_NFIELDS (type) == 1)
10471 {
10472 /* We assume that a union with a single field is a univariant
10473 enum. */
10474 /* Smash this type to be a structure type. We have to do this
10475 because the type has already been recorded. */
10476 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10477
10478 /* Make a union to hold the variants. */
10479 struct type *union_type = alloc_type (objfile);
10480 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10481 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10482 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10483 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10484
10485 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10486 const char *variant_name
10487 = rust_last_path_segment (TYPE_NAME (field_type));
10488 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10489 TYPE_NAME (field_type)
10490 = rust_fully_qualify (&objfile->objfile_obstack,
10491 TYPE_NAME (type), variant_name);
10492
10493 /* Install the union in the outer struct type. */
10494 TYPE_NFIELDS (type) = 1;
10495 TYPE_FIELDS (type)
10496 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10497 TYPE_FIELD_TYPE (type, 0) = union_type;
10498 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10499 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10500
10501 alloc_discriminant_info (union_type, -1, 0);
10502 }
10503 else
10504 {
10505 struct type *disr_type = nullptr;
10506 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10507 {
10508 disr_type = TYPE_FIELD_TYPE (type, i);
10509
10510 if (TYPE_NFIELDS (disr_type) == 0)
10511 {
10512 /* Could be data-less variant, so keep going. */
10513 }
10514 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10515 "RUST$ENUM$DISR") != 0)
10516 {
10517 /* Not a Rust enum. */
10518 return;
10519 }
10520 else
10521 {
10522 /* Found one. */
10523 break;
10524 }
10525 }
10526
10527 /* If we got here without a discriminant, then it's probably
10528 just a union. */
10529 if (disr_type == nullptr)
10530 return;
10531
10532 /* Smash this type to be a structure type. We have to do this
10533 because the type has already been recorded. */
10534 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10535
10536 /* Make a union to hold the variants. */
10537 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10538 struct type *union_type = alloc_type (objfile);
10539 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10540 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10541 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10542 TYPE_FIELDS (union_type)
10543 = (struct field *) TYPE_ZALLOC (union_type,
10544 (TYPE_NFIELDS (union_type)
10545 * sizeof (struct field)));
10546
10547 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10548 TYPE_NFIELDS (type) * sizeof (struct field));
10549
10550 /* Install the discriminant at index 0 in the union. */
10551 TYPE_FIELD (union_type, 0) = *disr_field;
10552 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10553 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10554
10555 /* Install the union in the outer struct type. */
10556 TYPE_FIELD_TYPE (type, 0) = union_type;
10557 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10558 TYPE_NFIELDS (type) = 1;
10559
10560 /* Set the size and offset of the union type. */
10561 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10562
10563 /* We need a way to find the correct discriminant given a
10564 variant name. For convenience we build a map here. */
10565 struct type *enum_type = FIELD_TYPE (*disr_field);
10566 std::unordered_map<std::string, ULONGEST> discriminant_map;
10567 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10568 {
10569 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10570 {
10571 const char *name
10572 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10573 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10574 }
10575 }
10576
10577 int n_fields = TYPE_NFIELDS (union_type);
10578 struct discriminant_info *disc
10579 = alloc_discriminant_info (union_type, 0, -1);
10580 /* Skip the discriminant here. */
10581 for (int i = 1; i < n_fields; ++i)
10582 {
10583 /* Find the final word in the name of this variant's type.
10584 That name can be used to look up the correct
10585 discriminant. */
10586 const char *variant_name
10587 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10588 i)));
10589
10590 auto iter = discriminant_map.find (variant_name);
10591 if (iter != discriminant_map.end ())
10592 disc->discriminants[i] = iter->second;
10593
10594 /* Remove the discriminant field. */
10595 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10596 --TYPE_NFIELDS (sub_type);
10597 ++TYPE_FIELDS (sub_type);
10598 TYPE_FIELD_NAME (union_type, i) = variant_name;
10599 TYPE_NAME (sub_type)
10600 = rust_fully_qualify (&objfile->objfile_obstack,
10601 TYPE_NAME (type), variant_name);
10602 }
10603 }
10604 }
10605
10606 /* Rewrite some Rust unions to be structures with variants parts. */
10607
10608 static void
10609 rust_union_quirks (struct dwarf2_cu *cu)
10610 {
10611 gdb_assert (cu->language == language_rust);
10612 for (struct type *type : cu->rust_unions)
10613 quirk_rust_enum (type, cu->per_cu->dwarf2_per_objfile->objfile);
10614 }
10615
10616 /* Return the symtab for PER_CU. This works properly regardless of
10617 whether we're using the index or psymtabs. */
10618
10619 static struct compunit_symtab *
10620 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10621 {
10622 return (per_cu->dwarf2_per_objfile->using_index
10623 ? per_cu->v.quick->compunit_symtab
10624 : per_cu->v.psymtab->compunit_symtab);
10625 }
10626
10627 /* A helper function for computing the list of all symbol tables
10628 included by PER_CU. */
10629
10630 static void
10631 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10632 htab_t all_children, htab_t all_type_symtabs,
10633 struct dwarf2_per_cu_data *per_cu,
10634 struct compunit_symtab *immediate_parent)
10635 {
10636 void **slot;
10637 int ix;
10638 struct compunit_symtab *cust;
10639 struct dwarf2_per_cu_data *iter;
10640
10641 slot = htab_find_slot (all_children, per_cu, INSERT);
10642 if (*slot != NULL)
10643 {
10644 /* This inclusion and its children have been processed. */
10645 return;
10646 }
10647
10648 *slot = per_cu;
10649 /* Only add a CU if it has a symbol table. */
10650 cust = get_compunit_symtab (per_cu);
10651 if (cust != NULL)
10652 {
10653 /* If this is a type unit only add its symbol table if we haven't
10654 seen it yet (type unit per_cu's can share symtabs). */
10655 if (per_cu->is_debug_types)
10656 {
10657 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10658 if (*slot == NULL)
10659 {
10660 *slot = cust;
10661 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10662 if (cust->user == NULL)
10663 cust->user = immediate_parent;
10664 }
10665 }
10666 else
10667 {
10668 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10669 if (cust->user == NULL)
10670 cust->user = immediate_parent;
10671 }
10672 }
10673
10674 for (ix = 0;
10675 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10676 ++ix)
10677 {
10678 recursively_compute_inclusions (result, all_children,
10679 all_type_symtabs, iter, cust);
10680 }
10681 }
10682
10683 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10684 PER_CU. */
10685
10686 static void
10687 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10688 {
10689 gdb_assert (! per_cu->is_debug_types);
10690
10691 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10692 {
10693 int ix, len;
10694 struct dwarf2_per_cu_data *per_cu_iter;
10695 struct compunit_symtab *compunit_symtab_iter;
10696 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10697 htab_t all_children, all_type_symtabs;
10698 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10699
10700 /* If we don't have a symtab, we can just skip this case. */
10701 if (cust == NULL)
10702 return;
10703
10704 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10705 NULL, xcalloc, xfree);
10706 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10707 NULL, xcalloc, xfree);
10708
10709 for (ix = 0;
10710 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10711 ix, per_cu_iter);
10712 ++ix)
10713 {
10714 recursively_compute_inclusions (&result_symtabs, all_children,
10715 all_type_symtabs, per_cu_iter,
10716 cust);
10717 }
10718
10719 /* Now we have a transitive closure of all the included symtabs. */
10720 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10721 cust->includes
10722 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10723 struct compunit_symtab *, len + 1);
10724 for (ix = 0;
10725 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10726 compunit_symtab_iter);
10727 ++ix)
10728 cust->includes[ix] = compunit_symtab_iter;
10729 cust->includes[len] = NULL;
10730
10731 VEC_free (compunit_symtab_ptr, result_symtabs);
10732 htab_delete (all_children);
10733 htab_delete (all_type_symtabs);
10734 }
10735 }
10736
10737 /* Compute the 'includes' field for the symtabs of all the CUs we just
10738 read. */
10739
10740 static void
10741 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10742 {
10743 int ix;
10744 struct dwarf2_per_cu_data *iter;
10745
10746 for (ix = 0;
10747 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10748 ix, iter);
10749 ++ix)
10750 {
10751 if (! iter->is_debug_types)
10752 compute_compunit_symtab_includes (iter);
10753 }
10754
10755 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10756 }
10757
10758 /* Generate full symbol information for PER_CU, whose DIEs have
10759 already been loaded into memory. */
10760
10761 static void
10762 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10763 enum language pretend_language)
10764 {
10765 struct dwarf2_cu *cu = per_cu->cu;
10766 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10767 struct objfile *objfile = dwarf2_per_objfile->objfile;
10768 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10769 CORE_ADDR lowpc, highpc;
10770 struct compunit_symtab *cust;
10771 CORE_ADDR baseaddr;
10772 struct block *static_block;
10773 CORE_ADDR addr;
10774
10775 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10776
10777 buildsym_init ();
10778 scoped_free_pendings free_pending;
10779
10780 /* Clear the list here in case something was left over. */
10781 cu->method_list.clear ();
10782
10783 cu->list_in_scope = &file_symbols;
10784
10785 cu->language = pretend_language;
10786 cu->language_defn = language_def (cu->language);
10787
10788 /* Do line number decoding in read_file_scope () */
10789 process_die (cu->dies, cu);
10790
10791 /* For now fudge the Go package. */
10792 if (cu->language == language_go)
10793 fixup_go_packaging (cu);
10794
10795 /* Now that we have processed all the DIEs in the CU, all the types
10796 should be complete, and it should now be safe to compute all of the
10797 physnames. */
10798 compute_delayed_physnames (cu);
10799
10800 if (cu->language == language_rust)
10801 rust_union_quirks (cu);
10802
10803 /* Some compilers don't define a DW_AT_high_pc attribute for the
10804 compilation unit. If the DW_AT_high_pc is missing, synthesize
10805 it, by scanning the DIE's below the compilation unit. */
10806 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10807
10808 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10809 static_block = end_symtab_get_static_block (addr, 0, 1);
10810
10811 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10812 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10813 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10814 addrmap to help ensure it has an accurate map of pc values belonging to
10815 this comp unit. */
10816 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10817
10818 cust = end_symtab_from_static_block (static_block,
10819 SECT_OFF_TEXT (objfile), 0);
10820
10821 if (cust != NULL)
10822 {
10823 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10824
10825 /* Set symtab language to language from DW_AT_language. If the
10826 compilation is from a C file generated by language preprocessors, do
10827 not set the language if it was already deduced by start_subfile. */
10828 if (!(cu->language == language_c
10829 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10830 COMPUNIT_FILETABS (cust)->language = cu->language;
10831
10832 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10833 produce DW_AT_location with location lists but it can be possibly
10834 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10835 there were bugs in prologue debug info, fixed later in GCC-4.5
10836 by "unwind info for epilogues" patch (which is not directly related).
10837
10838 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10839 needed, it would be wrong due to missing DW_AT_producer there.
10840
10841 Still one can confuse GDB by using non-standard GCC compilation
10842 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10843 */
10844 if (cu->has_loclist && gcc_4_minor >= 5)
10845 cust->locations_valid = 1;
10846
10847 if (gcc_4_minor >= 5)
10848 cust->epilogue_unwind_valid = 1;
10849
10850 cust->call_site_htab = cu->call_site_htab;
10851 }
10852
10853 if (dwarf2_per_objfile->using_index)
10854 per_cu->v.quick->compunit_symtab = cust;
10855 else
10856 {
10857 struct partial_symtab *pst = per_cu->v.psymtab;
10858 pst->compunit_symtab = cust;
10859 pst->readin = 1;
10860 }
10861
10862 /* Push it for inclusion processing later. */
10863 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10864 }
10865
10866 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10867 already been loaded into memory. */
10868
10869 static void
10870 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10871 enum language pretend_language)
10872 {
10873 struct dwarf2_cu *cu = per_cu->cu;
10874 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10875 struct objfile *objfile = dwarf2_per_objfile->objfile;
10876 struct compunit_symtab *cust;
10877 struct signatured_type *sig_type;
10878
10879 gdb_assert (per_cu->is_debug_types);
10880 sig_type = (struct signatured_type *) per_cu;
10881
10882 buildsym_init ();
10883 scoped_free_pendings free_pending;
10884
10885 /* Clear the list here in case something was left over. */
10886 cu->method_list.clear ();
10887
10888 cu->list_in_scope = &file_symbols;
10889
10890 cu->language = pretend_language;
10891 cu->language_defn = language_def (cu->language);
10892
10893 /* The symbol tables are set up in read_type_unit_scope. */
10894 process_die (cu->dies, cu);
10895
10896 /* For now fudge the Go package. */
10897 if (cu->language == language_go)
10898 fixup_go_packaging (cu);
10899
10900 /* Now that we have processed all the DIEs in the CU, all the types
10901 should be complete, and it should now be safe to compute all of the
10902 physnames. */
10903 compute_delayed_physnames (cu);
10904
10905 if (cu->language == language_rust)
10906 rust_union_quirks (cu);
10907
10908 /* TUs share symbol tables.
10909 If this is the first TU to use this symtab, complete the construction
10910 of it with end_expandable_symtab. Otherwise, complete the addition of
10911 this TU's symbols to the existing symtab. */
10912 if (sig_type->type_unit_group->compunit_symtab == NULL)
10913 {
10914 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10915 sig_type->type_unit_group->compunit_symtab = cust;
10916
10917 if (cust != NULL)
10918 {
10919 /* Set symtab language to language from DW_AT_language. If the
10920 compilation is from a C file generated by language preprocessors,
10921 do not set the language if it was already deduced by
10922 start_subfile. */
10923 if (!(cu->language == language_c
10924 && COMPUNIT_FILETABS (cust)->language != language_c))
10925 COMPUNIT_FILETABS (cust)->language = cu->language;
10926 }
10927 }
10928 else
10929 {
10930 augment_type_symtab ();
10931 cust = sig_type->type_unit_group->compunit_symtab;
10932 }
10933
10934 if (dwarf2_per_objfile->using_index)
10935 per_cu->v.quick->compunit_symtab = cust;
10936 else
10937 {
10938 struct partial_symtab *pst = per_cu->v.psymtab;
10939 pst->compunit_symtab = cust;
10940 pst->readin = 1;
10941 }
10942 }
10943
10944 /* Process an imported unit DIE. */
10945
10946 static void
10947 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10948 {
10949 struct attribute *attr;
10950
10951 /* For now we don't handle imported units in type units. */
10952 if (cu->per_cu->is_debug_types)
10953 {
10954 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10955 " supported in type units [in module %s]"),
10956 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10957 }
10958
10959 attr = dwarf2_attr (die, DW_AT_import, cu);
10960 if (attr != NULL)
10961 {
10962 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10963 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10964 dwarf2_per_cu_data *per_cu
10965 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10966 cu->per_cu->dwarf2_per_objfile);
10967
10968 /* If necessary, add it to the queue and load its DIEs. */
10969 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10970 load_full_comp_unit (per_cu, cu->language);
10971
10972 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10973 per_cu);
10974 }
10975 }
10976
10977 /* RAII object that represents a process_die scope: i.e.,
10978 starts/finishes processing a DIE. */
10979 class process_die_scope
10980 {
10981 public:
10982 process_die_scope (die_info *die, dwarf2_cu *cu)
10983 : m_die (die), m_cu (cu)
10984 {
10985 /* We should only be processing DIEs not already in process. */
10986 gdb_assert (!m_die->in_process);
10987 m_die->in_process = true;
10988 }
10989
10990 ~process_die_scope ()
10991 {
10992 m_die->in_process = false;
10993
10994 /* If we're done processing the DIE for the CU that owns the line
10995 header, we don't need the line header anymore. */
10996 if (m_cu->line_header_die_owner == m_die)
10997 {
10998 delete m_cu->line_header;
10999 m_cu->line_header = NULL;
11000 m_cu->line_header_die_owner = NULL;
11001 }
11002 }
11003
11004 private:
11005 die_info *m_die;
11006 dwarf2_cu *m_cu;
11007 };
11008
11009 /* Process a die and its children. */
11010
11011 static void
11012 process_die (struct die_info *die, struct dwarf2_cu *cu)
11013 {
11014 process_die_scope scope (die, cu);
11015
11016 switch (die->tag)
11017 {
11018 case DW_TAG_padding:
11019 break;
11020 case DW_TAG_compile_unit:
11021 case DW_TAG_partial_unit:
11022 read_file_scope (die, cu);
11023 break;
11024 case DW_TAG_type_unit:
11025 read_type_unit_scope (die, cu);
11026 break;
11027 case DW_TAG_subprogram:
11028 case DW_TAG_inlined_subroutine:
11029 read_func_scope (die, cu);
11030 break;
11031 case DW_TAG_lexical_block:
11032 case DW_TAG_try_block:
11033 case DW_TAG_catch_block:
11034 read_lexical_block_scope (die, cu);
11035 break;
11036 case DW_TAG_call_site:
11037 case DW_TAG_GNU_call_site:
11038 read_call_site_scope (die, cu);
11039 break;
11040 case DW_TAG_class_type:
11041 case DW_TAG_interface_type:
11042 case DW_TAG_structure_type:
11043 case DW_TAG_union_type:
11044 process_structure_scope (die, cu);
11045 break;
11046 case DW_TAG_enumeration_type:
11047 process_enumeration_scope (die, cu);
11048 break;
11049
11050 /* These dies have a type, but processing them does not create
11051 a symbol or recurse to process the children. Therefore we can
11052 read them on-demand through read_type_die. */
11053 case DW_TAG_subroutine_type:
11054 case DW_TAG_set_type:
11055 case DW_TAG_array_type:
11056 case DW_TAG_pointer_type:
11057 case DW_TAG_ptr_to_member_type:
11058 case DW_TAG_reference_type:
11059 case DW_TAG_rvalue_reference_type:
11060 case DW_TAG_string_type:
11061 break;
11062
11063 case DW_TAG_base_type:
11064 case DW_TAG_subrange_type:
11065 case DW_TAG_typedef:
11066 /* Add a typedef symbol for the type definition, if it has a
11067 DW_AT_name. */
11068 new_symbol (die, read_type_die (die, cu), cu);
11069 break;
11070 case DW_TAG_common_block:
11071 read_common_block (die, cu);
11072 break;
11073 case DW_TAG_common_inclusion:
11074 break;
11075 case DW_TAG_namespace:
11076 cu->processing_has_namespace_info = 1;
11077 read_namespace (die, cu);
11078 break;
11079 case DW_TAG_module:
11080 cu->processing_has_namespace_info = 1;
11081 read_module (die, cu);
11082 break;
11083 case DW_TAG_imported_declaration:
11084 cu->processing_has_namespace_info = 1;
11085 if (read_namespace_alias (die, cu))
11086 break;
11087 /* The declaration is not a global namespace alias: fall through. */
11088 case DW_TAG_imported_module:
11089 cu->processing_has_namespace_info = 1;
11090 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
11091 || cu->language != language_fortran))
11092 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
11093 dwarf_tag_name (die->tag));
11094 read_import_statement (die, cu);
11095 break;
11096
11097 case DW_TAG_imported_unit:
11098 process_imported_unit_die (die, cu);
11099 break;
11100
11101 case DW_TAG_variable:
11102 read_variable (die, cu);
11103 break;
11104
11105 default:
11106 new_symbol (die, NULL, cu);
11107 break;
11108 }
11109 }
11110 \f
11111 /* DWARF name computation. */
11112
11113 /* A helper function for dwarf2_compute_name which determines whether DIE
11114 needs to have the name of the scope prepended to the name listed in the
11115 die. */
11116
11117 static int
11118 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
11119 {
11120 struct attribute *attr;
11121
11122 switch (die->tag)
11123 {
11124 case DW_TAG_namespace:
11125 case DW_TAG_typedef:
11126 case DW_TAG_class_type:
11127 case DW_TAG_interface_type:
11128 case DW_TAG_structure_type:
11129 case DW_TAG_union_type:
11130 case DW_TAG_enumeration_type:
11131 case DW_TAG_enumerator:
11132 case DW_TAG_subprogram:
11133 case DW_TAG_inlined_subroutine:
11134 case DW_TAG_member:
11135 case DW_TAG_imported_declaration:
11136 return 1;
11137
11138 case DW_TAG_variable:
11139 case DW_TAG_constant:
11140 /* We only need to prefix "globally" visible variables. These include
11141 any variable marked with DW_AT_external or any variable that
11142 lives in a namespace. [Variables in anonymous namespaces
11143 require prefixing, but they are not DW_AT_external.] */
11144
11145 if (dwarf2_attr (die, DW_AT_specification, cu))
11146 {
11147 struct dwarf2_cu *spec_cu = cu;
11148
11149 return die_needs_namespace (die_specification (die, &spec_cu),
11150 spec_cu);
11151 }
11152
11153 attr = dwarf2_attr (die, DW_AT_external, cu);
11154 if (attr == NULL && die->parent->tag != DW_TAG_namespace
11155 && die->parent->tag != DW_TAG_module)
11156 return 0;
11157 /* A variable in a lexical block of some kind does not need a
11158 namespace, even though in C++ such variables may be external
11159 and have a mangled name. */
11160 if (die->parent->tag == DW_TAG_lexical_block
11161 || die->parent->tag == DW_TAG_try_block
11162 || die->parent->tag == DW_TAG_catch_block
11163 || die->parent->tag == DW_TAG_subprogram)
11164 return 0;
11165 return 1;
11166
11167 default:
11168 return 0;
11169 }
11170 }
11171
11172 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
11173 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
11174 defined for the given DIE. */
11175
11176 static struct attribute *
11177 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
11178 {
11179 struct attribute *attr;
11180
11181 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
11182 if (attr == NULL)
11183 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
11184
11185 return attr;
11186 }
11187
11188 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
11189 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
11190 defined for the given DIE. */
11191
11192 static const char *
11193 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
11194 {
11195 const char *linkage_name;
11196
11197 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
11198 if (linkage_name == NULL)
11199 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
11200
11201 return linkage_name;
11202 }
11203
11204 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
11205 compute the physname for the object, which include a method's:
11206 - formal parameters (C++),
11207 - receiver type (Go),
11208
11209 The term "physname" is a bit confusing.
11210 For C++, for example, it is the demangled name.
11211 For Go, for example, it's the mangled name.
11212
11213 For Ada, return the DIE's linkage name rather than the fully qualified
11214 name. PHYSNAME is ignored..
11215
11216 The result is allocated on the objfile_obstack and canonicalized. */
11217
11218 static const char *
11219 dwarf2_compute_name (const char *name,
11220 struct die_info *die, struct dwarf2_cu *cu,
11221 int physname)
11222 {
11223 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11224
11225 if (name == NULL)
11226 name = dwarf2_name (die, cu);
11227
11228 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
11229 but otherwise compute it by typename_concat inside GDB.
11230 FIXME: Actually this is not really true, or at least not always true.
11231 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
11232 Fortran names because there is no mangling standard. So new_symbol
11233 will set the demangled name to the result of dwarf2_full_name, and it is
11234 the demangled name that GDB uses if it exists. */
11235 if (cu->language == language_ada
11236 || (cu->language == language_fortran && physname))
11237 {
11238 /* For Ada unit, we prefer the linkage name over the name, as
11239 the former contains the exported name, which the user expects
11240 to be able to reference. Ideally, we want the user to be able
11241 to reference this entity using either natural or linkage name,
11242 but we haven't started looking at this enhancement yet. */
11243 const char *linkage_name = dw2_linkage_name (die, cu);
11244
11245 if (linkage_name != NULL)
11246 return linkage_name;
11247 }
11248
11249 /* These are the only languages we know how to qualify names in. */
11250 if (name != NULL
11251 && (cu->language == language_cplus
11252 || cu->language == language_fortran || cu->language == language_d
11253 || cu->language == language_rust))
11254 {
11255 if (die_needs_namespace (die, cu))
11256 {
11257 const char *prefix;
11258 const char *canonical_name = NULL;
11259
11260 string_file buf;
11261
11262 prefix = determine_prefix (die, cu);
11263 if (*prefix != '\0')
11264 {
11265 char *prefixed_name = typename_concat (NULL, prefix, name,
11266 physname, cu);
11267
11268 buf.puts (prefixed_name);
11269 xfree (prefixed_name);
11270 }
11271 else
11272 buf.puts (name);
11273
11274 /* Template parameters may be specified in the DIE's DW_AT_name, or
11275 as children with DW_TAG_template_type_param or
11276 DW_TAG_value_type_param. If the latter, add them to the name
11277 here. If the name already has template parameters, then
11278 skip this step; some versions of GCC emit both, and
11279 it is more efficient to use the pre-computed name.
11280
11281 Something to keep in mind about this process: it is very
11282 unlikely, or in some cases downright impossible, to produce
11283 something that will match the mangled name of a function.
11284 If the definition of the function has the same debug info,
11285 we should be able to match up with it anyway. But fallbacks
11286 using the minimal symbol, for instance to find a method
11287 implemented in a stripped copy of libstdc++, will not work.
11288 If we do not have debug info for the definition, we will have to
11289 match them up some other way.
11290
11291 When we do name matching there is a related problem with function
11292 templates; two instantiated function templates are allowed to
11293 differ only by their return types, which we do not add here. */
11294
11295 if (cu->language == language_cplus && strchr (name, '<') == NULL)
11296 {
11297 struct attribute *attr;
11298 struct die_info *child;
11299 int first = 1;
11300
11301 die->building_fullname = 1;
11302
11303 for (child = die->child; child != NULL; child = child->sibling)
11304 {
11305 struct type *type;
11306 LONGEST value;
11307 const gdb_byte *bytes;
11308 struct dwarf2_locexpr_baton *baton;
11309 struct value *v;
11310
11311 if (child->tag != DW_TAG_template_type_param
11312 && child->tag != DW_TAG_template_value_param)
11313 continue;
11314
11315 if (first)
11316 {
11317 buf.puts ("<");
11318 first = 0;
11319 }
11320 else
11321 buf.puts (", ");
11322
11323 attr = dwarf2_attr (child, DW_AT_type, cu);
11324 if (attr == NULL)
11325 {
11326 complaint (&symfile_complaints,
11327 _("template parameter missing DW_AT_type"));
11328 buf.puts ("UNKNOWN_TYPE");
11329 continue;
11330 }
11331 type = die_type (child, cu);
11332
11333 if (child->tag == DW_TAG_template_type_param)
11334 {
11335 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
11336 continue;
11337 }
11338
11339 attr = dwarf2_attr (child, DW_AT_const_value, cu);
11340 if (attr == NULL)
11341 {
11342 complaint (&symfile_complaints,
11343 _("template parameter missing "
11344 "DW_AT_const_value"));
11345 buf.puts ("UNKNOWN_VALUE");
11346 continue;
11347 }
11348
11349 dwarf2_const_value_attr (attr, type, name,
11350 &cu->comp_unit_obstack, cu,
11351 &value, &bytes, &baton);
11352
11353 if (TYPE_NOSIGN (type))
11354 /* GDB prints characters as NUMBER 'CHAR'. If that's
11355 changed, this can use value_print instead. */
11356 c_printchar (value, type, &buf);
11357 else
11358 {
11359 struct value_print_options opts;
11360
11361 if (baton != NULL)
11362 v = dwarf2_evaluate_loc_desc (type, NULL,
11363 baton->data,
11364 baton->size,
11365 baton->per_cu);
11366 else if (bytes != NULL)
11367 {
11368 v = allocate_value (type);
11369 memcpy (value_contents_writeable (v), bytes,
11370 TYPE_LENGTH (type));
11371 }
11372 else
11373 v = value_from_longest (type, value);
11374
11375 /* Specify decimal so that we do not depend on
11376 the radix. */
11377 get_formatted_print_options (&opts, 'd');
11378 opts.raw = 1;
11379 value_print (v, &buf, &opts);
11380 release_value (v);
11381 value_free (v);
11382 }
11383 }
11384
11385 die->building_fullname = 0;
11386
11387 if (!first)
11388 {
11389 /* Close the argument list, with a space if necessary
11390 (nested templates). */
11391 if (!buf.empty () && buf.string ().back () == '>')
11392 buf.puts (" >");
11393 else
11394 buf.puts (">");
11395 }
11396 }
11397
11398 /* For C++ methods, append formal parameter type
11399 information, if PHYSNAME. */
11400
11401 if (physname && die->tag == DW_TAG_subprogram
11402 && cu->language == language_cplus)
11403 {
11404 struct type *type = read_type_die (die, cu);
11405
11406 c_type_print_args (type, &buf, 1, cu->language,
11407 &type_print_raw_options);
11408
11409 if (cu->language == language_cplus)
11410 {
11411 /* Assume that an artificial first parameter is
11412 "this", but do not crash if it is not. RealView
11413 marks unnamed (and thus unused) parameters as
11414 artificial; there is no way to differentiate
11415 the two cases. */
11416 if (TYPE_NFIELDS (type) > 0
11417 && TYPE_FIELD_ARTIFICIAL (type, 0)
11418 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11419 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11420 0))))
11421 buf.puts (" const");
11422 }
11423 }
11424
11425 const std::string &intermediate_name = buf.string ();
11426
11427 if (cu->language == language_cplus)
11428 canonical_name
11429 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11430 &objfile->per_bfd->storage_obstack);
11431
11432 /* If we only computed INTERMEDIATE_NAME, or if
11433 INTERMEDIATE_NAME is already canonical, then we need to
11434 copy it to the appropriate obstack. */
11435 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11436 name = ((const char *)
11437 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11438 intermediate_name.c_str (),
11439 intermediate_name.length ()));
11440 else
11441 name = canonical_name;
11442 }
11443 }
11444
11445 return name;
11446 }
11447
11448 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11449 If scope qualifiers are appropriate they will be added. The result
11450 will be allocated on the storage_obstack, or NULL if the DIE does
11451 not have a name. NAME may either be from a previous call to
11452 dwarf2_name or NULL.
11453
11454 The output string will be canonicalized (if C++). */
11455
11456 static const char *
11457 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11458 {
11459 return dwarf2_compute_name (name, die, cu, 0);
11460 }
11461
11462 /* Construct a physname for the given DIE in CU. NAME may either be
11463 from a previous call to dwarf2_name or NULL. The result will be
11464 allocated on the objfile_objstack or NULL if the DIE does not have a
11465 name.
11466
11467 The output string will be canonicalized (if C++). */
11468
11469 static const char *
11470 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11471 {
11472 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11473 const char *retval, *mangled = NULL, *canon = NULL;
11474 int need_copy = 1;
11475
11476 /* In this case dwarf2_compute_name is just a shortcut not building anything
11477 on its own. */
11478 if (!die_needs_namespace (die, cu))
11479 return dwarf2_compute_name (name, die, cu, 1);
11480
11481 mangled = dw2_linkage_name (die, cu);
11482
11483 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11484 See https://github.com/rust-lang/rust/issues/32925. */
11485 if (cu->language == language_rust && mangled != NULL
11486 && strchr (mangled, '{') != NULL)
11487 mangled = NULL;
11488
11489 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11490 has computed. */
11491 gdb::unique_xmalloc_ptr<char> demangled;
11492 if (mangled != NULL)
11493 {
11494
11495 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11496 {
11497 /* Do nothing (do not demangle the symbol name). */
11498 }
11499 else if (cu->language == language_go)
11500 {
11501 /* This is a lie, but we already lie to the caller new_symbol.
11502 new_symbol assumes we return the mangled name.
11503 This just undoes that lie until things are cleaned up. */
11504 }
11505 else
11506 {
11507 /* Use DMGL_RET_DROP for C++ template functions to suppress
11508 their return type. It is easier for GDB users to search
11509 for such functions as `name(params)' than `long name(params)'.
11510 In such case the minimal symbol names do not match the full
11511 symbol names but for template functions there is never a need
11512 to look up their definition from their declaration so
11513 the only disadvantage remains the minimal symbol variant
11514 `long name(params)' does not have the proper inferior type. */
11515 demangled.reset (gdb_demangle (mangled,
11516 (DMGL_PARAMS | DMGL_ANSI
11517 | DMGL_RET_DROP)));
11518 }
11519 if (demangled)
11520 canon = demangled.get ();
11521 else
11522 {
11523 canon = mangled;
11524 need_copy = 0;
11525 }
11526 }
11527
11528 if (canon == NULL || check_physname)
11529 {
11530 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11531
11532 if (canon != NULL && strcmp (physname, canon) != 0)
11533 {
11534 /* It may not mean a bug in GDB. The compiler could also
11535 compute DW_AT_linkage_name incorrectly. But in such case
11536 GDB would need to be bug-to-bug compatible. */
11537
11538 complaint (&symfile_complaints,
11539 _("Computed physname <%s> does not match demangled <%s> "
11540 "(from linkage <%s>) - DIE at %s [in module %s]"),
11541 physname, canon, mangled, sect_offset_str (die->sect_off),
11542 objfile_name (objfile));
11543
11544 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11545 is available here - over computed PHYSNAME. It is safer
11546 against both buggy GDB and buggy compilers. */
11547
11548 retval = canon;
11549 }
11550 else
11551 {
11552 retval = physname;
11553 need_copy = 0;
11554 }
11555 }
11556 else
11557 retval = canon;
11558
11559 if (need_copy)
11560 retval = ((const char *)
11561 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11562 retval, strlen (retval)));
11563
11564 return retval;
11565 }
11566
11567 /* Inspect DIE in CU for a namespace alias. If one exists, record
11568 a new symbol for it.
11569
11570 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11571
11572 static int
11573 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11574 {
11575 struct attribute *attr;
11576
11577 /* If the die does not have a name, this is not a namespace
11578 alias. */
11579 attr = dwarf2_attr (die, DW_AT_name, cu);
11580 if (attr != NULL)
11581 {
11582 int num;
11583 struct die_info *d = die;
11584 struct dwarf2_cu *imported_cu = cu;
11585
11586 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11587 keep inspecting DIEs until we hit the underlying import. */
11588 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11589 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11590 {
11591 attr = dwarf2_attr (d, DW_AT_import, cu);
11592 if (attr == NULL)
11593 break;
11594
11595 d = follow_die_ref (d, attr, &imported_cu);
11596 if (d->tag != DW_TAG_imported_declaration)
11597 break;
11598 }
11599
11600 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11601 {
11602 complaint (&symfile_complaints,
11603 _("DIE at %s has too many recursively imported "
11604 "declarations"), sect_offset_str (d->sect_off));
11605 return 0;
11606 }
11607
11608 if (attr != NULL)
11609 {
11610 struct type *type;
11611 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11612
11613 type = get_die_type_at_offset (sect_off, cu->per_cu);
11614 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11615 {
11616 /* This declaration is a global namespace alias. Add
11617 a symbol for it whose type is the aliased namespace. */
11618 new_symbol (die, type, cu);
11619 return 1;
11620 }
11621 }
11622 }
11623
11624 return 0;
11625 }
11626
11627 /* Return the using directives repository (global or local?) to use in the
11628 current context for LANGUAGE.
11629
11630 For Ada, imported declarations can materialize renamings, which *may* be
11631 global. However it is impossible (for now?) in DWARF to distinguish
11632 "external" imported declarations and "static" ones. As all imported
11633 declarations seem to be static in all other languages, make them all CU-wide
11634 global only in Ada. */
11635
11636 static struct using_direct **
11637 using_directives (enum language language)
11638 {
11639 if (language == language_ada && context_stack_depth == 0)
11640 return &global_using_directives;
11641 else
11642 return &local_using_directives;
11643 }
11644
11645 /* Read the import statement specified by the given die and record it. */
11646
11647 static void
11648 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11649 {
11650 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11651 struct attribute *import_attr;
11652 struct die_info *imported_die, *child_die;
11653 struct dwarf2_cu *imported_cu;
11654 const char *imported_name;
11655 const char *imported_name_prefix;
11656 const char *canonical_name;
11657 const char *import_alias;
11658 const char *imported_declaration = NULL;
11659 const char *import_prefix;
11660 std::vector<const char *> excludes;
11661
11662 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11663 if (import_attr == NULL)
11664 {
11665 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11666 dwarf_tag_name (die->tag));
11667 return;
11668 }
11669
11670 imported_cu = cu;
11671 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11672 imported_name = dwarf2_name (imported_die, imported_cu);
11673 if (imported_name == NULL)
11674 {
11675 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11676
11677 The import in the following code:
11678 namespace A
11679 {
11680 typedef int B;
11681 }
11682
11683 int main ()
11684 {
11685 using A::B;
11686 B b;
11687 return b;
11688 }
11689
11690 ...
11691 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11692 <52> DW_AT_decl_file : 1
11693 <53> DW_AT_decl_line : 6
11694 <54> DW_AT_import : <0x75>
11695 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11696 <59> DW_AT_name : B
11697 <5b> DW_AT_decl_file : 1
11698 <5c> DW_AT_decl_line : 2
11699 <5d> DW_AT_type : <0x6e>
11700 ...
11701 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11702 <76> DW_AT_byte_size : 4
11703 <77> DW_AT_encoding : 5 (signed)
11704
11705 imports the wrong die ( 0x75 instead of 0x58 ).
11706 This case will be ignored until the gcc bug is fixed. */
11707 return;
11708 }
11709
11710 /* Figure out the local name after import. */
11711 import_alias = dwarf2_name (die, cu);
11712
11713 /* Figure out where the statement is being imported to. */
11714 import_prefix = determine_prefix (die, cu);
11715
11716 /* Figure out what the scope of the imported die is and prepend it
11717 to the name of the imported die. */
11718 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11719
11720 if (imported_die->tag != DW_TAG_namespace
11721 && imported_die->tag != DW_TAG_module)
11722 {
11723 imported_declaration = imported_name;
11724 canonical_name = imported_name_prefix;
11725 }
11726 else if (strlen (imported_name_prefix) > 0)
11727 canonical_name = obconcat (&objfile->objfile_obstack,
11728 imported_name_prefix,
11729 (cu->language == language_d ? "." : "::"),
11730 imported_name, (char *) NULL);
11731 else
11732 canonical_name = imported_name;
11733
11734 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11735 for (child_die = die->child; child_die && child_die->tag;
11736 child_die = sibling_die (child_die))
11737 {
11738 /* DWARF-4: A Fortran use statement with a “rename list” may be
11739 represented by an imported module entry with an import attribute
11740 referring to the module and owned entries corresponding to those
11741 entities that are renamed as part of being imported. */
11742
11743 if (child_die->tag != DW_TAG_imported_declaration)
11744 {
11745 complaint (&symfile_complaints,
11746 _("child DW_TAG_imported_declaration expected "
11747 "- DIE at %s [in module %s]"),
11748 sect_offset_str (child_die->sect_off),
11749 objfile_name (objfile));
11750 continue;
11751 }
11752
11753 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11754 if (import_attr == NULL)
11755 {
11756 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11757 dwarf_tag_name (child_die->tag));
11758 continue;
11759 }
11760
11761 imported_cu = cu;
11762 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11763 &imported_cu);
11764 imported_name = dwarf2_name (imported_die, imported_cu);
11765 if (imported_name == NULL)
11766 {
11767 complaint (&symfile_complaints,
11768 _("child DW_TAG_imported_declaration has unknown "
11769 "imported name - DIE at %s [in module %s]"),
11770 sect_offset_str (child_die->sect_off),
11771 objfile_name (objfile));
11772 continue;
11773 }
11774
11775 excludes.push_back (imported_name);
11776
11777 process_die (child_die, cu);
11778 }
11779
11780 add_using_directive (using_directives (cu->language),
11781 import_prefix,
11782 canonical_name,
11783 import_alias,
11784 imported_declaration,
11785 excludes,
11786 0,
11787 &objfile->objfile_obstack);
11788 }
11789
11790 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11791 types, but gives them a size of zero. Starting with version 14,
11792 ICC is compatible with GCC. */
11793
11794 static int
11795 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11796 {
11797 if (!cu->checked_producer)
11798 check_producer (cu);
11799
11800 return cu->producer_is_icc_lt_14;
11801 }
11802
11803 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11804 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11805 this, it was first present in GCC release 4.3.0. */
11806
11807 static int
11808 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11809 {
11810 if (!cu->checked_producer)
11811 check_producer (cu);
11812
11813 return cu->producer_is_gcc_lt_4_3;
11814 }
11815
11816 static file_and_directory
11817 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11818 {
11819 file_and_directory res;
11820
11821 /* Find the filename. Do not use dwarf2_name here, since the filename
11822 is not a source language identifier. */
11823 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11824 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11825
11826 if (res.comp_dir == NULL
11827 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11828 && IS_ABSOLUTE_PATH (res.name))
11829 {
11830 res.comp_dir_storage = ldirname (res.name);
11831 if (!res.comp_dir_storage.empty ())
11832 res.comp_dir = res.comp_dir_storage.c_str ();
11833 }
11834 if (res.comp_dir != NULL)
11835 {
11836 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11837 directory, get rid of it. */
11838 const char *cp = strchr (res.comp_dir, ':');
11839
11840 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11841 res.comp_dir = cp + 1;
11842 }
11843
11844 if (res.name == NULL)
11845 res.name = "<unknown>";
11846
11847 return res;
11848 }
11849
11850 /* Handle DW_AT_stmt_list for a compilation unit.
11851 DIE is the DW_TAG_compile_unit die for CU.
11852 COMP_DIR is the compilation directory. LOWPC is passed to
11853 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11854
11855 static void
11856 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11857 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11858 {
11859 struct dwarf2_per_objfile *dwarf2_per_objfile
11860 = cu->per_cu->dwarf2_per_objfile;
11861 struct objfile *objfile = dwarf2_per_objfile->objfile;
11862 struct attribute *attr;
11863 struct line_header line_header_local;
11864 hashval_t line_header_local_hash;
11865 void **slot;
11866 int decode_mapping;
11867
11868 gdb_assert (! cu->per_cu->is_debug_types);
11869
11870 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11871 if (attr == NULL)
11872 return;
11873
11874 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11875
11876 /* The line header hash table is only created if needed (it exists to
11877 prevent redundant reading of the line table for partial_units).
11878 If we're given a partial_unit, we'll need it. If we're given a
11879 compile_unit, then use the line header hash table if it's already
11880 created, but don't create one just yet. */
11881
11882 if (dwarf2_per_objfile->line_header_hash == NULL
11883 && die->tag == DW_TAG_partial_unit)
11884 {
11885 dwarf2_per_objfile->line_header_hash
11886 = htab_create_alloc_ex (127, line_header_hash_voidp,
11887 line_header_eq_voidp,
11888 free_line_header_voidp,
11889 &objfile->objfile_obstack,
11890 hashtab_obstack_allocate,
11891 dummy_obstack_deallocate);
11892 }
11893
11894 line_header_local.sect_off = line_offset;
11895 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11896 line_header_local_hash = line_header_hash (&line_header_local);
11897 if (dwarf2_per_objfile->line_header_hash != NULL)
11898 {
11899 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11900 &line_header_local,
11901 line_header_local_hash, NO_INSERT);
11902
11903 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11904 is not present in *SLOT (since if there is something in *SLOT then
11905 it will be for a partial_unit). */
11906 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11907 {
11908 gdb_assert (*slot != NULL);
11909 cu->line_header = (struct line_header *) *slot;
11910 return;
11911 }
11912 }
11913
11914 /* dwarf_decode_line_header does not yet provide sufficient information.
11915 We always have to call also dwarf_decode_lines for it. */
11916 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11917 if (lh == NULL)
11918 return;
11919
11920 cu->line_header = lh.release ();
11921 cu->line_header_die_owner = die;
11922
11923 if (dwarf2_per_objfile->line_header_hash == NULL)
11924 slot = NULL;
11925 else
11926 {
11927 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11928 &line_header_local,
11929 line_header_local_hash, INSERT);
11930 gdb_assert (slot != NULL);
11931 }
11932 if (slot != NULL && *slot == NULL)
11933 {
11934 /* This newly decoded line number information unit will be owned
11935 by line_header_hash hash table. */
11936 *slot = cu->line_header;
11937 cu->line_header_die_owner = NULL;
11938 }
11939 else
11940 {
11941 /* We cannot free any current entry in (*slot) as that struct line_header
11942 may be already used by multiple CUs. Create only temporary decoded
11943 line_header for this CU - it may happen at most once for each line
11944 number information unit. And if we're not using line_header_hash
11945 then this is what we want as well. */
11946 gdb_assert (die->tag != DW_TAG_partial_unit);
11947 }
11948 decode_mapping = (die->tag != DW_TAG_partial_unit);
11949 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11950 decode_mapping);
11951
11952 }
11953
11954 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11955
11956 static void
11957 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11958 {
11959 struct dwarf2_per_objfile *dwarf2_per_objfile
11960 = cu->per_cu->dwarf2_per_objfile;
11961 struct objfile *objfile = dwarf2_per_objfile->objfile;
11962 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11963 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11964 CORE_ADDR highpc = ((CORE_ADDR) 0);
11965 struct attribute *attr;
11966 struct die_info *child_die;
11967 CORE_ADDR baseaddr;
11968
11969 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11970
11971 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11972
11973 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11974 from finish_block. */
11975 if (lowpc == ((CORE_ADDR) -1))
11976 lowpc = highpc;
11977 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11978
11979 file_and_directory fnd = find_file_and_directory (die, cu);
11980
11981 prepare_one_comp_unit (cu, die, cu->language);
11982
11983 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11984 standardised yet. As a workaround for the language detection we fall
11985 back to the DW_AT_producer string. */
11986 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11987 cu->language = language_opencl;
11988
11989 /* Similar hack for Go. */
11990 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11991 set_cu_language (DW_LANG_Go, cu);
11992
11993 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11994
11995 /* Decode line number information if present. We do this before
11996 processing child DIEs, so that the line header table is available
11997 for DW_AT_decl_file. */
11998 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11999
12000 /* Process all dies in compilation unit. */
12001 if (die->child != NULL)
12002 {
12003 child_die = die->child;
12004 while (child_die && child_die->tag)
12005 {
12006 process_die (child_die, cu);
12007 child_die = sibling_die (child_die);
12008 }
12009 }
12010
12011 /* Decode macro information, if present. Dwarf 2 macro information
12012 refers to information in the line number info statement program
12013 header, so we can only read it if we've read the header
12014 successfully. */
12015 attr = dwarf2_attr (die, DW_AT_macros, cu);
12016 if (attr == NULL)
12017 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
12018 if (attr && cu->line_header)
12019 {
12020 if (dwarf2_attr (die, DW_AT_macro_info, cu))
12021 complaint (&symfile_complaints,
12022 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
12023
12024 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
12025 }
12026 else
12027 {
12028 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
12029 if (attr && cu->line_header)
12030 {
12031 unsigned int macro_offset = DW_UNSND (attr);
12032
12033 dwarf_decode_macros (cu, macro_offset, 0);
12034 }
12035 }
12036 }
12037
12038 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
12039 Create the set of symtabs used by this TU, or if this TU is sharing
12040 symtabs with another TU and the symtabs have already been created
12041 then restore those symtabs in the line header.
12042 We don't need the pc/line-number mapping for type units. */
12043
12044 static void
12045 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
12046 {
12047 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
12048 struct type_unit_group *tu_group;
12049 int first_time;
12050 struct attribute *attr;
12051 unsigned int i;
12052 struct signatured_type *sig_type;
12053
12054 gdb_assert (per_cu->is_debug_types);
12055 sig_type = (struct signatured_type *) per_cu;
12056
12057 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
12058
12059 /* If we're using .gdb_index (includes -readnow) then
12060 per_cu->type_unit_group may not have been set up yet. */
12061 if (sig_type->type_unit_group == NULL)
12062 sig_type->type_unit_group = get_type_unit_group (cu, attr);
12063 tu_group = sig_type->type_unit_group;
12064
12065 /* If we've already processed this stmt_list there's no real need to
12066 do it again, we could fake it and just recreate the part we need
12067 (file name,index -> symtab mapping). If data shows this optimization
12068 is useful we can do it then. */
12069 first_time = tu_group->compunit_symtab == NULL;
12070
12071 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
12072 debug info. */
12073 line_header_up lh;
12074 if (attr != NULL)
12075 {
12076 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
12077 lh = dwarf_decode_line_header (line_offset, cu);
12078 }
12079 if (lh == NULL)
12080 {
12081 if (first_time)
12082 dwarf2_start_symtab (cu, "", NULL, 0);
12083 else
12084 {
12085 gdb_assert (tu_group->symtabs == NULL);
12086 restart_symtab (tu_group->compunit_symtab, "", 0);
12087 }
12088 return;
12089 }
12090
12091 cu->line_header = lh.release ();
12092 cu->line_header_die_owner = die;
12093
12094 if (first_time)
12095 {
12096 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
12097
12098 /* Note: We don't assign tu_group->compunit_symtab yet because we're
12099 still initializing it, and our caller (a few levels up)
12100 process_full_type_unit still needs to know if this is the first
12101 time. */
12102
12103 tu_group->num_symtabs = cu->line_header->file_names.size ();
12104 tu_group->symtabs = XNEWVEC (struct symtab *,
12105 cu->line_header->file_names.size ());
12106
12107 for (i = 0; i < cu->line_header->file_names.size (); ++i)
12108 {
12109 file_entry &fe = cu->line_header->file_names[i];
12110
12111 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
12112
12113 if (current_subfile->symtab == NULL)
12114 {
12115 /* NOTE: start_subfile will recognize when it's been
12116 passed a file it has already seen. So we can't
12117 assume there's a simple mapping from
12118 cu->line_header->file_names to subfiles, plus
12119 cu->line_header->file_names may contain dups. */
12120 current_subfile->symtab
12121 = allocate_symtab (cust, current_subfile->name);
12122 }
12123
12124 fe.symtab = current_subfile->symtab;
12125 tu_group->symtabs[i] = fe.symtab;
12126 }
12127 }
12128 else
12129 {
12130 restart_symtab (tu_group->compunit_symtab, "", 0);
12131
12132 for (i = 0; i < cu->line_header->file_names.size (); ++i)
12133 {
12134 file_entry &fe = cu->line_header->file_names[i];
12135
12136 fe.symtab = tu_group->symtabs[i];
12137 }
12138 }
12139
12140 /* The main symtab is allocated last. Type units don't have DW_AT_name
12141 so they don't have a "real" (so to speak) symtab anyway.
12142 There is later code that will assign the main symtab to all symbols
12143 that don't have one. We need to handle the case of a symbol with a
12144 missing symtab (DW_AT_decl_file) anyway. */
12145 }
12146
12147 /* Process DW_TAG_type_unit.
12148 For TUs we want to skip the first top level sibling if it's not the
12149 actual type being defined by this TU. In this case the first top
12150 level sibling is there to provide context only. */
12151
12152 static void
12153 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
12154 {
12155 struct die_info *child_die;
12156
12157 prepare_one_comp_unit (cu, die, language_minimal);
12158
12159 /* Initialize (or reinitialize) the machinery for building symtabs.
12160 We do this before processing child DIEs, so that the line header table
12161 is available for DW_AT_decl_file. */
12162 setup_type_unit_groups (die, cu);
12163
12164 if (die->child != NULL)
12165 {
12166 child_die = die->child;
12167 while (child_die && child_die->tag)
12168 {
12169 process_die (child_die, cu);
12170 child_die = sibling_die (child_die);
12171 }
12172 }
12173 }
12174 \f
12175 /* DWO/DWP files.
12176
12177 http://gcc.gnu.org/wiki/DebugFission
12178 http://gcc.gnu.org/wiki/DebugFissionDWP
12179
12180 To simplify handling of both DWO files ("object" files with the DWARF info)
12181 and DWP files (a file with the DWOs packaged up into one file), we treat
12182 DWP files as having a collection of virtual DWO files. */
12183
12184 static hashval_t
12185 hash_dwo_file (const void *item)
12186 {
12187 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
12188 hashval_t hash;
12189
12190 hash = htab_hash_string (dwo_file->dwo_name);
12191 if (dwo_file->comp_dir != NULL)
12192 hash += htab_hash_string (dwo_file->comp_dir);
12193 return hash;
12194 }
12195
12196 static int
12197 eq_dwo_file (const void *item_lhs, const void *item_rhs)
12198 {
12199 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
12200 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
12201
12202 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
12203 return 0;
12204 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
12205 return lhs->comp_dir == rhs->comp_dir;
12206 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
12207 }
12208
12209 /* Allocate a hash table for DWO files. */
12210
12211 static htab_t
12212 allocate_dwo_file_hash_table (struct objfile *objfile)
12213 {
12214 return htab_create_alloc_ex (41,
12215 hash_dwo_file,
12216 eq_dwo_file,
12217 NULL,
12218 &objfile->objfile_obstack,
12219 hashtab_obstack_allocate,
12220 dummy_obstack_deallocate);
12221 }
12222
12223 /* Lookup DWO file DWO_NAME. */
12224
12225 static void **
12226 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
12227 const char *dwo_name,
12228 const char *comp_dir)
12229 {
12230 struct dwo_file find_entry;
12231 void **slot;
12232
12233 if (dwarf2_per_objfile->dwo_files == NULL)
12234 dwarf2_per_objfile->dwo_files
12235 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
12236
12237 memset (&find_entry, 0, sizeof (find_entry));
12238 find_entry.dwo_name = dwo_name;
12239 find_entry.comp_dir = comp_dir;
12240 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
12241
12242 return slot;
12243 }
12244
12245 static hashval_t
12246 hash_dwo_unit (const void *item)
12247 {
12248 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12249
12250 /* This drops the top 32 bits of the id, but is ok for a hash. */
12251 return dwo_unit->signature;
12252 }
12253
12254 static int
12255 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
12256 {
12257 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
12258 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
12259
12260 /* The signature is assumed to be unique within the DWO file.
12261 So while object file CU dwo_id's always have the value zero,
12262 that's OK, assuming each object file DWO file has only one CU,
12263 and that's the rule for now. */
12264 return lhs->signature == rhs->signature;
12265 }
12266
12267 /* Allocate a hash table for DWO CUs,TUs.
12268 There is one of these tables for each of CUs,TUs for each DWO file. */
12269
12270 static htab_t
12271 allocate_dwo_unit_table (struct objfile *objfile)
12272 {
12273 /* Start out with a pretty small number.
12274 Generally DWO files contain only one CU and maybe some TUs. */
12275 return htab_create_alloc_ex (3,
12276 hash_dwo_unit,
12277 eq_dwo_unit,
12278 NULL,
12279 &objfile->objfile_obstack,
12280 hashtab_obstack_allocate,
12281 dummy_obstack_deallocate);
12282 }
12283
12284 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
12285
12286 struct create_dwo_cu_data
12287 {
12288 struct dwo_file *dwo_file;
12289 struct dwo_unit dwo_unit;
12290 };
12291
12292 /* die_reader_func for create_dwo_cu. */
12293
12294 static void
12295 create_dwo_cu_reader (const struct die_reader_specs *reader,
12296 const gdb_byte *info_ptr,
12297 struct die_info *comp_unit_die,
12298 int has_children,
12299 void *datap)
12300 {
12301 struct dwarf2_cu *cu = reader->cu;
12302 sect_offset sect_off = cu->per_cu->sect_off;
12303 struct dwarf2_section_info *section = cu->per_cu->section;
12304 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
12305 struct dwo_file *dwo_file = data->dwo_file;
12306 struct dwo_unit *dwo_unit = &data->dwo_unit;
12307 struct attribute *attr;
12308
12309 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
12310 if (attr == NULL)
12311 {
12312 complaint (&symfile_complaints,
12313 _("Dwarf Error: debug entry at offset %s is missing"
12314 " its dwo_id [in module %s]"),
12315 sect_offset_str (sect_off), dwo_file->dwo_name);
12316 return;
12317 }
12318
12319 dwo_unit->dwo_file = dwo_file;
12320 dwo_unit->signature = DW_UNSND (attr);
12321 dwo_unit->section = section;
12322 dwo_unit->sect_off = sect_off;
12323 dwo_unit->length = cu->per_cu->length;
12324
12325 if (dwarf_read_debug)
12326 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
12327 sect_offset_str (sect_off),
12328 hex_string (dwo_unit->signature));
12329 }
12330
12331 /* Create the dwo_units for the CUs in a DWO_FILE.
12332 Note: This function processes DWO files only, not DWP files. */
12333
12334 static void
12335 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12336 struct dwo_file &dwo_file, dwarf2_section_info &section,
12337 htab_t &cus_htab)
12338 {
12339 struct objfile *objfile = dwarf2_per_objfile->objfile;
12340 const gdb_byte *info_ptr, *end_ptr;
12341
12342 dwarf2_read_section (objfile, &section);
12343 info_ptr = section.buffer;
12344
12345 if (info_ptr == NULL)
12346 return;
12347
12348 if (dwarf_read_debug)
12349 {
12350 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
12351 get_section_name (&section),
12352 get_section_file_name (&section));
12353 }
12354
12355 end_ptr = info_ptr + section.size;
12356 while (info_ptr < end_ptr)
12357 {
12358 struct dwarf2_per_cu_data per_cu;
12359 struct create_dwo_cu_data create_dwo_cu_data;
12360 struct dwo_unit *dwo_unit;
12361 void **slot;
12362 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
12363
12364 memset (&create_dwo_cu_data.dwo_unit, 0,
12365 sizeof (create_dwo_cu_data.dwo_unit));
12366 memset (&per_cu, 0, sizeof (per_cu));
12367 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
12368 per_cu.is_debug_types = 0;
12369 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12370 per_cu.section = &section;
12371 create_dwo_cu_data.dwo_file = &dwo_file;
12372
12373 init_cutu_and_read_dies_no_follow (
12374 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12375 info_ptr += per_cu.length;
12376
12377 // If the unit could not be parsed, skip it.
12378 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12379 continue;
12380
12381 if (cus_htab == NULL)
12382 cus_htab = allocate_dwo_unit_table (objfile);
12383
12384 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12385 *dwo_unit = create_dwo_cu_data.dwo_unit;
12386 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12387 gdb_assert (slot != NULL);
12388 if (*slot != NULL)
12389 {
12390 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12391 sect_offset dup_sect_off = dup_cu->sect_off;
12392
12393 complaint (&symfile_complaints,
12394 _("debug cu entry at offset %s is duplicate to"
12395 " the entry at offset %s, signature %s"),
12396 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12397 hex_string (dwo_unit->signature));
12398 }
12399 *slot = (void *)dwo_unit;
12400 }
12401 }
12402
12403 /* DWP file .debug_{cu,tu}_index section format:
12404 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12405
12406 DWP Version 1:
12407
12408 Both index sections have the same format, and serve to map a 64-bit
12409 signature to a set of section numbers. Each section begins with a header,
12410 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12411 indexes, and a pool of 32-bit section numbers. The index sections will be
12412 aligned at 8-byte boundaries in the file.
12413
12414 The index section header consists of:
12415
12416 V, 32 bit version number
12417 -, 32 bits unused
12418 N, 32 bit number of compilation units or type units in the index
12419 M, 32 bit number of slots in the hash table
12420
12421 Numbers are recorded using the byte order of the application binary.
12422
12423 The hash table begins at offset 16 in the section, and consists of an array
12424 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12425 order of the application binary). Unused slots in the hash table are 0.
12426 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12427
12428 The parallel table begins immediately after the hash table
12429 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12430 array of 32-bit indexes (using the byte order of the application binary),
12431 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12432 table contains a 32-bit index into the pool of section numbers. For unused
12433 hash table slots, the corresponding entry in the parallel table will be 0.
12434
12435 The pool of section numbers begins immediately following the hash table
12436 (at offset 16 + 12 * M from the beginning of the section). The pool of
12437 section numbers consists of an array of 32-bit words (using the byte order
12438 of the application binary). Each item in the array is indexed starting
12439 from 0. The hash table entry provides the index of the first section
12440 number in the set. Additional section numbers in the set follow, and the
12441 set is terminated by a 0 entry (section number 0 is not used in ELF).
12442
12443 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12444 section must be the first entry in the set, and the .debug_abbrev.dwo must
12445 be the second entry. Other members of the set may follow in any order.
12446
12447 ---
12448
12449 DWP Version 2:
12450
12451 DWP Version 2 combines all the .debug_info, etc. sections into one,
12452 and the entries in the index tables are now offsets into these sections.
12453 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12454 section.
12455
12456 Index Section Contents:
12457 Header
12458 Hash Table of Signatures dwp_hash_table.hash_table
12459 Parallel Table of Indices dwp_hash_table.unit_table
12460 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12461 Table of Section Sizes dwp_hash_table.v2.sizes
12462
12463 The index section header consists of:
12464
12465 V, 32 bit version number
12466 L, 32 bit number of columns in the table of section offsets
12467 N, 32 bit number of compilation units or type units in the index
12468 M, 32 bit number of slots in the hash table
12469
12470 Numbers are recorded using the byte order of the application binary.
12471
12472 The hash table has the same format as version 1.
12473 The parallel table of indices has the same format as version 1,
12474 except that the entries are origin-1 indices into the table of sections
12475 offsets and the table of section sizes.
12476
12477 The table of offsets begins immediately following the parallel table
12478 (at offset 16 + 12 * M from the beginning of the section). The table is
12479 a two-dimensional array of 32-bit words (using the byte order of the
12480 application binary), with L columns and N+1 rows, in row-major order.
12481 Each row in the array is indexed starting from 0. The first row provides
12482 a key to the remaining rows: each column in this row provides an identifier
12483 for a debug section, and the offsets in the same column of subsequent rows
12484 refer to that section. The section identifiers are:
12485
12486 DW_SECT_INFO 1 .debug_info.dwo
12487 DW_SECT_TYPES 2 .debug_types.dwo
12488 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12489 DW_SECT_LINE 4 .debug_line.dwo
12490 DW_SECT_LOC 5 .debug_loc.dwo
12491 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12492 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12493 DW_SECT_MACRO 8 .debug_macro.dwo
12494
12495 The offsets provided by the CU and TU index sections are the base offsets
12496 for the contributions made by each CU or TU to the corresponding section
12497 in the package file. Each CU and TU header contains an abbrev_offset
12498 field, used to find the abbreviations table for that CU or TU within the
12499 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12500 be interpreted as relative to the base offset given in the index section.
12501 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12502 should be interpreted as relative to the base offset for .debug_line.dwo,
12503 and offsets into other debug sections obtained from DWARF attributes should
12504 also be interpreted as relative to the corresponding base offset.
12505
12506 The table of sizes begins immediately following the table of offsets.
12507 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12508 with L columns and N rows, in row-major order. Each row in the array is
12509 indexed starting from 1 (row 0 is shared by the two tables).
12510
12511 ---
12512
12513 Hash table lookup is handled the same in version 1 and 2:
12514
12515 We assume that N and M will not exceed 2^32 - 1.
12516 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12517
12518 Given a 64-bit compilation unit signature or a type signature S, an entry
12519 in the hash table is located as follows:
12520
12521 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12522 the low-order k bits all set to 1.
12523
12524 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12525
12526 3) If the hash table entry at index H matches the signature, use that
12527 entry. If the hash table entry at index H is unused (all zeroes),
12528 terminate the search: the signature is not present in the table.
12529
12530 4) Let H = (H + H') modulo M. Repeat at Step 3.
12531
12532 Because M > N and H' and M are relatively prime, the search is guaranteed
12533 to stop at an unused slot or find the match. */
12534
12535 /* Create a hash table to map DWO IDs to their CU/TU entry in
12536 .debug_{info,types}.dwo in DWP_FILE.
12537 Returns NULL if there isn't one.
12538 Note: This function processes DWP files only, not DWO files. */
12539
12540 static struct dwp_hash_table *
12541 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12542 struct dwp_file *dwp_file, int is_debug_types)
12543 {
12544 struct objfile *objfile = dwarf2_per_objfile->objfile;
12545 bfd *dbfd = dwp_file->dbfd;
12546 const gdb_byte *index_ptr, *index_end;
12547 struct dwarf2_section_info *index;
12548 uint32_t version, nr_columns, nr_units, nr_slots;
12549 struct dwp_hash_table *htab;
12550
12551 if (is_debug_types)
12552 index = &dwp_file->sections.tu_index;
12553 else
12554 index = &dwp_file->sections.cu_index;
12555
12556 if (dwarf2_section_empty_p (index))
12557 return NULL;
12558 dwarf2_read_section (objfile, index);
12559
12560 index_ptr = index->buffer;
12561 index_end = index_ptr + index->size;
12562
12563 version = read_4_bytes (dbfd, index_ptr);
12564 index_ptr += 4;
12565 if (version == 2)
12566 nr_columns = read_4_bytes (dbfd, index_ptr);
12567 else
12568 nr_columns = 0;
12569 index_ptr += 4;
12570 nr_units = read_4_bytes (dbfd, index_ptr);
12571 index_ptr += 4;
12572 nr_slots = read_4_bytes (dbfd, index_ptr);
12573 index_ptr += 4;
12574
12575 if (version != 1 && version != 2)
12576 {
12577 error (_("Dwarf Error: unsupported DWP file version (%s)"
12578 " [in module %s]"),
12579 pulongest (version), dwp_file->name);
12580 }
12581 if (nr_slots != (nr_slots & -nr_slots))
12582 {
12583 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12584 " is not power of 2 [in module %s]"),
12585 pulongest (nr_slots), dwp_file->name);
12586 }
12587
12588 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12589 htab->version = version;
12590 htab->nr_columns = nr_columns;
12591 htab->nr_units = nr_units;
12592 htab->nr_slots = nr_slots;
12593 htab->hash_table = index_ptr;
12594 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12595
12596 /* Exit early if the table is empty. */
12597 if (nr_slots == 0 || nr_units == 0
12598 || (version == 2 && nr_columns == 0))
12599 {
12600 /* All must be zero. */
12601 if (nr_slots != 0 || nr_units != 0
12602 || (version == 2 && nr_columns != 0))
12603 {
12604 complaint (&symfile_complaints,
12605 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12606 " all zero [in modules %s]"),
12607 dwp_file->name);
12608 }
12609 return htab;
12610 }
12611
12612 if (version == 1)
12613 {
12614 htab->section_pool.v1.indices =
12615 htab->unit_table + sizeof (uint32_t) * nr_slots;
12616 /* It's harder to decide whether the section is too small in v1.
12617 V1 is deprecated anyway so we punt. */
12618 }
12619 else
12620 {
12621 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12622 int *ids = htab->section_pool.v2.section_ids;
12623 /* Reverse map for error checking. */
12624 int ids_seen[DW_SECT_MAX + 1];
12625 int i;
12626
12627 if (nr_columns < 2)
12628 {
12629 error (_("Dwarf Error: bad DWP hash table, too few columns"
12630 " in section table [in module %s]"),
12631 dwp_file->name);
12632 }
12633 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12634 {
12635 error (_("Dwarf Error: bad DWP hash table, too many columns"
12636 " in section table [in module %s]"),
12637 dwp_file->name);
12638 }
12639 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12640 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12641 for (i = 0; i < nr_columns; ++i)
12642 {
12643 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12644
12645 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12646 {
12647 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12648 " in section table [in module %s]"),
12649 id, dwp_file->name);
12650 }
12651 if (ids_seen[id] != -1)
12652 {
12653 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12654 " id %d in section table [in module %s]"),
12655 id, dwp_file->name);
12656 }
12657 ids_seen[id] = i;
12658 ids[i] = id;
12659 }
12660 /* Must have exactly one info or types section. */
12661 if (((ids_seen[DW_SECT_INFO] != -1)
12662 + (ids_seen[DW_SECT_TYPES] != -1))
12663 != 1)
12664 {
12665 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12666 " DWO info/types section [in module %s]"),
12667 dwp_file->name);
12668 }
12669 /* Must have an abbrev section. */
12670 if (ids_seen[DW_SECT_ABBREV] == -1)
12671 {
12672 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12673 " section [in module %s]"),
12674 dwp_file->name);
12675 }
12676 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12677 htab->section_pool.v2.sizes =
12678 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12679 * nr_units * nr_columns);
12680 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12681 * nr_units * nr_columns))
12682 > index_end)
12683 {
12684 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12685 " [in module %s]"),
12686 dwp_file->name);
12687 }
12688 }
12689
12690 return htab;
12691 }
12692
12693 /* Update SECTIONS with the data from SECTP.
12694
12695 This function is like the other "locate" section routines that are
12696 passed to bfd_map_over_sections, but in this context the sections to
12697 read comes from the DWP V1 hash table, not the full ELF section table.
12698
12699 The result is non-zero for success, or zero if an error was found. */
12700
12701 static int
12702 locate_v1_virtual_dwo_sections (asection *sectp,
12703 struct virtual_v1_dwo_sections *sections)
12704 {
12705 const struct dwop_section_names *names = &dwop_section_names;
12706
12707 if (section_is_p (sectp->name, &names->abbrev_dwo))
12708 {
12709 /* There can be only one. */
12710 if (sections->abbrev.s.section != NULL)
12711 return 0;
12712 sections->abbrev.s.section = sectp;
12713 sections->abbrev.size = bfd_get_section_size (sectp);
12714 }
12715 else if (section_is_p (sectp->name, &names->info_dwo)
12716 || section_is_p (sectp->name, &names->types_dwo))
12717 {
12718 /* There can be only one. */
12719 if (sections->info_or_types.s.section != NULL)
12720 return 0;
12721 sections->info_or_types.s.section = sectp;
12722 sections->info_or_types.size = bfd_get_section_size (sectp);
12723 }
12724 else if (section_is_p (sectp->name, &names->line_dwo))
12725 {
12726 /* There can be only one. */
12727 if (sections->line.s.section != NULL)
12728 return 0;
12729 sections->line.s.section = sectp;
12730 sections->line.size = bfd_get_section_size (sectp);
12731 }
12732 else if (section_is_p (sectp->name, &names->loc_dwo))
12733 {
12734 /* There can be only one. */
12735 if (sections->loc.s.section != NULL)
12736 return 0;
12737 sections->loc.s.section = sectp;
12738 sections->loc.size = bfd_get_section_size (sectp);
12739 }
12740 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12741 {
12742 /* There can be only one. */
12743 if (sections->macinfo.s.section != NULL)
12744 return 0;
12745 sections->macinfo.s.section = sectp;
12746 sections->macinfo.size = bfd_get_section_size (sectp);
12747 }
12748 else if (section_is_p (sectp->name, &names->macro_dwo))
12749 {
12750 /* There can be only one. */
12751 if (sections->macro.s.section != NULL)
12752 return 0;
12753 sections->macro.s.section = sectp;
12754 sections->macro.size = bfd_get_section_size (sectp);
12755 }
12756 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12757 {
12758 /* There can be only one. */
12759 if (sections->str_offsets.s.section != NULL)
12760 return 0;
12761 sections->str_offsets.s.section = sectp;
12762 sections->str_offsets.size = bfd_get_section_size (sectp);
12763 }
12764 else
12765 {
12766 /* No other kind of section is valid. */
12767 return 0;
12768 }
12769
12770 return 1;
12771 }
12772
12773 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12774 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12775 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12776 This is for DWP version 1 files. */
12777
12778 static struct dwo_unit *
12779 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12780 struct dwp_file *dwp_file,
12781 uint32_t unit_index,
12782 const char *comp_dir,
12783 ULONGEST signature, int is_debug_types)
12784 {
12785 struct objfile *objfile = dwarf2_per_objfile->objfile;
12786 const struct dwp_hash_table *dwp_htab =
12787 is_debug_types ? dwp_file->tus : dwp_file->cus;
12788 bfd *dbfd = dwp_file->dbfd;
12789 const char *kind = is_debug_types ? "TU" : "CU";
12790 struct dwo_file *dwo_file;
12791 struct dwo_unit *dwo_unit;
12792 struct virtual_v1_dwo_sections sections;
12793 void **dwo_file_slot;
12794 int i;
12795
12796 gdb_assert (dwp_file->version == 1);
12797
12798 if (dwarf_read_debug)
12799 {
12800 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12801 kind,
12802 pulongest (unit_index), hex_string (signature),
12803 dwp_file->name);
12804 }
12805
12806 /* Fetch the sections of this DWO unit.
12807 Put a limit on the number of sections we look for so that bad data
12808 doesn't cause us to loop forever. */
12809
12810 #define MAX_NR_V1_DWO_SECTIONS \
12811 (1 /* .debug_info or .debug_types */ \
12812 + 1 /* .debug_abbrev */ \
12813 + 1 /* .debug_line */ \
12814 + 1 /* .debug_loc */ \
12815 + 1 /* .debug_str_offsets */ \
12816 + 1 /* .debug_macro or .debug_macinfo */ \
12817 + 1 /* trailing zero */)
12818
12819 memset (&sections, 0, sizeof (sections));
12820
12821 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12822 {
12823 asection *sectp;
12824 uint32_t section_nr =
12825 read_4_bytes (dbfd,
12826 dwp_htab->section_pool.v1.indices
12827 + (unit_index + i) * sizeof (uint32_t));
12828
12829 if (section_nr == 0)
12830 break;
12831 if (section_nr >= dwp_file->num_sections)
12832 {
12833 error (_("Dwarf Error: bad DWP hash table, section number too large"
12834 " [in module %s]"),
12835 dwp_file->name);
12836 }
12837
12838 sectp = dwp_file->elf_sections[section_nr];
12839 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12840 {
12841 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12842 " [in module %s]"),
12843 dwp_file->name);
12844 }
12845 }
12846
12847 if (i < 2
12848 || dwarf2_section_empty_p (&sections.info_or_types)
12849 || dwarf2_section_empty_p (&sections.abbrev))
12850 {
12851 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12852 " [in module %s]"),
12853 dwp_file->name);
12854 }
12855 if (i == MAX_NR_V1_DWO_SECTIONS)
12856 {
12857 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12858 " [in module %s]"),
12859 dwp_file->name);
12860 }
12861
12862 /* It's easier for the rest of the code if we fake a struct dwo_file and
12863 have dwo_unit "live" in that. At least for now.
12864
12865 The DWP file can be made up of a random collection of CUs and TUs.
12866 However, for each CU + set of TUs that came from the same original DWO
12867 file, we can combine them back into a virtual DWO file to save space
12868 (fewer struct dwo_file objects to allocate). Remember that for really
12869 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12870
12871 std::string virtual_dwo_name =
12872 string_printf ("virtual-dwo/%d-%d-%d-%d",
12873 get_section_id (&sections.abbrev),
12874 get_section_id (&sections.line),
12875 get_section_id (&sections.loc),
12876 get_section_id (&sections.str_offsets));
12877 /* Can we use an existing virtual DWO file? */
12878 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12879 virtual_dwo_name.c_str (),
12880 comp_dir);
12881 /* Create one if necessary. */
12882 if (*dwo_file_slot == NULL)
12883 {
12884 if (dwarf_read_debug)
12885 {
12886 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12887 virtual_dwo_name.c_str ());
12888 }
12889 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12890 dwo_file->dwo_name
12891 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12892 virtual_dwo_name.c_str (),
12893 virtual_dwo_name.size ());
12894 dwo_file->comp_dir = comp_dir;
12895 dwo_file->sections.abbrev = sections.abbrev;
12896 dwo_file->sections.line = sections.line;
12897 dwo_file->sections.loc = sections.loc;
12898 dwo_file->sections.macinfo = sections.macinfo;
12899 dwo_file->sections.macro = sections.macro;
12900 dwo_file->sections.str_offsets = sections.str_offsets;
12901 /* The "str" section is global to the entire DWP file. */
12902 dwo_file->sections.str = dwp_file->sections.str;
12903 /* The info or types section is assigned below to dwo_unit,
12904 there's no need to record it in dwo_file.
12905 Also, we can't simply record type sections in dwo_file because
12906 we record a pointer into the vector in dwo_unit. As we collect more
12907 types we'll grow the vector and eventually have to reallocate space
12908 for it, invalidating all copies of pointers into the previous
12909 contents. */
12910 *dwo_file_slot = dwo_file;
12911 }
12912 else
12913 {
12914 if (dwarf_read_debug)
12915 {
12916 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12917 virtual_dwo_name.c_str ());
12918 }
12919 dwo_file = (struct dwo_file *) *dwo_file_slot;
12920 }
12921
12922 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12923 dwo_unit->dwo_file = dwo_file;
12924 dwo_unit->signature = signature;
12925 dwo_unit->section =
12926 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12927 *dwo_unit->section = sections.info_or_types;
12928 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12929
12930 return dwo_unit;
12931 }
12932
12933 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12934 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12935 piece within that section used by a TU/CU, return a virtual section
12936 of just that piece. */
12937
12938 static struct dwarf2_section_info
12939 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12940 struct dwarf2_section_info *section,
12941 bfd_size_type offset, bfd_size_type size)
12942 {
12943 struct dwarf2_section_info result;
12944 asection *sectp;
12945
12946 gdb_assert (section != NULL);
12947 gdb_assert (!section->is_virtual);
12948
12949 memset (&result, 0, sizeof (result));
12950 result.s.containing_section = section;
12951 result.is_virtual = 1;
12952
12953 if (size == 0)
12954 return result;
12955
12956 sectp = get_section_bfd_section (section);
12957
12958 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12959 bounds of the real section. This is a pretty-rare event, so just
12960 flag an error (easier) instead of a warning and trying to cope. */
12961 if (sectp == NULL
12962 || offset + size > bfd_get_section_size (sectp))
12963 {
12964 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12965 " in section %s [in module %s]"),
12966 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12967 objfile_name (dwarf2_per_objfile->objfile));
12968 }
12969
12970 result.virtual_offset = offset;
12971 result.size = size;
12972 return result;
12973 }
12974
12975 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12976 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12977 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12978 This is for DWP version 2 files. */
12979
12980 static struct dwo_unit *
12981 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12982 struct dwp_file *dwp_file,
12983 uint32_t unit_index,
12984 const char *comp_dir,
12985 ULONGEST signature, int is_debug_types)
12986 {
12987 struct objfile *objfile = dwarf2_per_objfile->objfile;
12988 const struct dwp_hash_table *dwp_htab =
12989 is_debug_types ? dwp_file->tus : dwp_file->cus;
12990 bfd *dbfd = dwp_file->dbfd;
12991 const char *kind = is_debug_types ? "TU" : "CU";
12992 struct dwo_file *dwo_file;
12993 struct dwo_unit *dwo_unit;
12994 struct virtual_v2_dwo_sections sections;
12995 void **dwo_file_slot;
12996 int i;
12997
12998 gdb_assert (dwp_file->version == 2);
12999
13000 if (dwarf_read_debug)
13001 {
13002 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
13003 kind,
13004 pulongest (unit_index), hex_string (signature),
13005 dwp_file->name);
13006 }
13007
13008 /* Fetch the section offsets of this DWO unit. */
13009
13010 memset (&sections, 0, sizeof (sections));
13011
13012 for (i = 0; i < dwp_htab->nr_columns; ++i)
13013 {
13014 uint32_t offset = read_4_bytes (dbfd,
13015 dwp_htab->section_pool.v2.offsets
13016 + (((unit_index - 1) * dwp_htab->nr_columns
13017 + i)
13018 * sizeof (uint32_t)));
13019 uint32_t size = read_4_bytes (dbfd,
13020 dwp_htab->section_pool.v2.sizes
13021 + (((unit_index - 1) * dwp_htab->nr_columns
13022 + i)
13023 * sizeof (uint32_t)));
13024
13025 switch (dwp_htab->section_pool.v2.section_ids[i])
13026 {
13027 case DW_SECT_INFO:
13028 case DW_SECT_TYPES:
13029 sections.info_or_types_offset = offset;
13030 sections.info_or_types_size = size;
13031 break;
13032 case DW_SECT_ABBREV:
13033 sections.abbrev_offset = offset;
13034 sections.abbrev_size = size;
13035 break;
13036 case DW_SECT_LINE:
13037 sections.line_offset = offset;
13038 sections.line_size = size;
13039 break;
13040 case DW_SECT_LOC:
13041 sections.loc_offset = offset;
13042 sections.loc_size = size;
13043 break;
13044 case DW_SECT_STR_OFFSETS:
13045 sections.str_offsets_offset = offset;
13046 sections.str_offsets_size = size;
13047 break;
13048 case DW_SECT_MACINFO:
13049 sections.macinfo_offset = offset;
13050 sections.macinfo_size = size;
13051 break;
13052 case DW_SECT_MACRO:
13053 sections.macro_offset = offset;
13054 sections.macro_size = size;
13055 break;
13056 }
13057 }
13058
13059 /* It's easier for the rest of the code if we fake a struct dwo_file and
13060 have dwo_unit "live" in that. At least for now.
13061
13062 The DWP file can be made up of a random collection of CUs and TUs.
13063 However, for each CU + set of TUs that came from the same original DWO
13064 file, we can combine them back into a virtual DWO file to save space
13065 (fewer struct dwo_file objects to allocate). Remember that for really
13066 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
13067
13068 std::string virtual_dwo_name =
13069 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
13070 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
13071 (long) (sections.line_size ? sections.line_offset : 0),
13072 (long) (sections.loc_size ? sections.loc_offset : 0),
13073 (long) (sections.str_offsets_size
13074 ? sections.str_offsets_offset : 0));
13075 /* Can we use an existing virtual DWO file? */
13076 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13077 virtual_dwo_name.c_str (),
13078 comp_dir);
13079 /* Create one if necessary. */
13080 if (*dwo_file_slot == NULL)
13081 {
13082 if (dwarf_read_debug)
13083 {
13084 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
13085 virtual_dwo_name.c_str ());
13086 }
13087 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
13088 dwo_file->dwo_name
13089 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
13090 virtual_dwo_name.c_str (),
13091 virtual_dwo_name.size ());
13092 dwo_file->comp_dir = comp_dir;
13093 dwo_file->sections.abbrev =
13094 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
13095 sections.abbrev_offset, sections.abbrev_size);
13096 dwo_file->sections.line =
13097 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
13098 sections.line_offset, sections.line_size);
13099 dwo_file->sections.loc =
13100 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
13101 sections.loc_offset, sections.loc_size);
13102 dwo_file->sections.macinfo =
13103 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
13104 sections.macinfo_offset, sections.macinfo_size);
13105 dwo_file->sections.macro =
13106 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
13107 sections.macro_offset, sections.macro_size);
13108 dwo_file->sections.str_offsets =
13109 create_dwp_v2_section (dwarf2_per_objfile,
13110 &dwp_file->sections.str_offsets,
13111 sections.str_offsets_offset,
13112 sections.str_offsets_size);
13113 /* The "str" section is global to the entire DWP file. */
13114 dwo_file->sections.str = dwp_file->sections.str;
13115 /* The info or types section is assigned below to dwo_unit,
13116 there's no need to record it in dwo_file.
13117 Also, we can't simply record type sections in dwo_file because
13118 we record a pointer into the vector in dwo_unit. As we collect more
13119 types we'll grow the vector and eventually have to reallocate space
13120 for it, invalidating all copies of pointers into the previous
13121 contents. */
13122 *dwo_file_slot = dwo_file;
13123 }
13124 else
13125 {
13126 if (dwarf_read_debug)
13127 {
13128 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
13129 virtual_dwo_name.c_str ());
13130 }
13131 dwo_file = (struct dwo_file *) *dwo_file_slot;
13132 }
13133
13134 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
13135 dwo_unit->dwo_file = dwo_file;
13136 dwo_unit->signature = signature;
13137 dwo_unit->section =
13138 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
13139 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
13140 is_debug_types
13141 ? &dwp_file->sections.types
13142 : &dwp_file->sections.info,
13143 sections.info_or_types_offset,
13144 sections.info_or_types_size);
13145 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
13146
13147 return dwo_unit;
13148 }
13149
13150 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
13151 Returns NULL if the signature isn't found. */
13152
13153 static struct dwo_unit *
13154 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
13155 struct dwp_file *dwp_file, const char *comp_dir,
13156 ULONGEST signature, int is_debug_types)
13157 {
13158 const struct dwp_hash_table *dwp_htab =
13159 is_debug_types ? dwp_file->tus : dwp_file->cus;
13160 bfd *dbfd = dwp_file->dbfd;
13161 uint32_t mask = dwp_htab->nr_slots - 1;
13162 uint32_t hash = signature & mask;
13163 uint32_t hash2 = ((signature >> 32) & mask) | 1;
13164 unsigned int i;
13165 void **slot;
13166 struct dwo_unit find_dwo_cu;
13167
13168 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
13169 find_dwo_cu.signature = signature;
13170 slot = htab_find_slot (is_debug_types
13171 ? dwp_file->loaded_tus
13172 : dwp_file->loaded_cus,
13173 &find_dwo_cu, INSERT);
13174
13175 if (*slot != NULL)
13176 return (struct dwo_unit *) *slot;
13177
13178 /* Use a for loop so that we don't loop forever on bad debug info. */
13179 for (i = 0; i < dwp_htab->nr_slots; ++i)
13180 {
13181 ULONGEST signature_in_table;
13182
13183 signature_in_table =
13184 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
13185 if (signature_in_table == signature)
13186 {
13187 uint32_t unit_index =
13188 read_4_bytes (dbfd,
13189 dwp_htab->unit_table + hash * sizeof (uint32_t));
13190
13191 if (dwp_file->version == 1)
13192 {
13193 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
13194 dwp_file, unit_index,
13195 comp_dir, signature,
13196 is_debug_types);
13197 }
13198 else
13199 {
13200 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
13201 dwp_file, unit_index,
13202 comp_dir, signature,
13203 is_debug_types);
13204 }
13205 return (struct dwo_unit *) *slot;
13206 }
13207 if (signature_in_table == 0)
13208 return NULL;
13209 hash = (hash + hash2) & mask;
13210 }
13211
13212 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
13213 " [in module %s]"),
13214 dwp_file->name);
13215 }
13216
13217 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
13218 Open the file specified by FILE_NAME and hand it off to BFD for
13219 preliminary analysis. Return a newly initialized bfd *, which
13220 includes a canonicalized copy of FILE_NAME.
13221 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
13222 SEARCH_CWD is true if the current directory is to be searched.
13223 It will be searched before debug-file-directory.
13224 If successful, the file is added to the bfd include table of the
13225 objfile's bfd (see gdb_bfd_record_inclusion).
13226 If unable to find/open the file, return NULL.
13227 NOTE: This function is derived from symfile_bfd_open. */
13228
13229 static gdb_bfd_ref_ptr
13230 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13231 const char *file_name, int is_dwp, int search_cwd)
13232 {
13233 int desc;
13234 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
13235 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
13236 to debug_file_directory. */
13237 const char *search_path;
13238 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
13239
13240 gdb::unique_xmalloc_ptr<char> search_path_holder;
13241 if (search_cwd)
13242 {
13243 if (*debug_file_directory != '\0')
13244 {
13245 search_path_holder.reset (concat (".", dirname_separator_string,
13246 debug_file_directory,
13247 (char *) NULL));
13248 search_path = search_path_holder.get ();
13249 }
13250 else
13251 search_path = ".";
13252 }
13253 else
13254 search_path = debug_file_directory;
13255
13256 openp_flags flags = OPF_RETURN_REALPATH;
13257 if (is_dwp)
13258 flags |= OPF_SEARCH_IN_PATH;
13259
13260 gdb::unique_xmalloc_ptr<char> absolute_name;
13261 desc = openp (search_path, flags, file_name,
13262 O_RDONLY | O_BINARY, &absolute_name);
13263 if (desc < 0)
13264 return NULL;
13265
13266 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
13267 gnutarget, desc));
13268 if (sym_bfd == NULL)
13269 return NULL;
13270 bfd_set_cacheable (sym_bfd.get (), 1);
13271
13272 if (!bfd_check_format (sym_bfd.get (), bfd_object))
13273 return NULL;
13274
13275 /* Success. Record the bfd as having been included by the objfile's bfd.
13276 This is important because things like demangled_names_hash lives in the
13277 objfile's per_bfd space and may have references to things like symbol
13278 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
13279 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13280
13281 return sym_bfd;
13282 }
13283
13284 /* Try to open DWO file FILE_NAME.
13285 COMP_DIR is the DW_AT_comp_dir attribute.
13286 The result is the bfd handle of the file.
13287 If there is a problem finding or opening the file, return NULL.
13288 Upon success, the canonicalized path of the file is stored in the bfd,
13289 same as symfile_bfd_open. */
13290
13291 static gdb_bfd_ref_ptr
13292 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13293 const char *file_name, const char *comp_dir)
13294 {
13295 if (IS_ABSOLUTE_PATH (file_name))
13296 return try_open_dwop_file (dwarf2_per_objfile, file_name,
13297 0 /*is_dwp*/, 0 /*search_cwd*/);
13298
13299 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
13300
13301 if (comp_dir != NULL)
13302 {
13303 char *path_to_try = concat (comp_dir, SLASH_STRING,
13304 file_name, (char *) NULL);
13305
13306 /* NOTE: If comp_dir is a relative path, this will also try the
13307 search path, which seems useful. */
13308 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
13309 path_to_try,
13310 0 /*is_dwp*/,
13311 1 /*search_cwd*/));
13312 xfree (path_to_try);
13313 if (abfd != NULL)
13314 return abfd;
13315 }
13316
13317 /* That didn't work, try debug-file-directory, which, despite its name,
13318 is a list of paths. */
13319
13320 if (*debug_file_directory == '\0')
13321 return NULL;
13322
13323 return try_open_dwop_file (dwarf2_per_objfile, file_name,
13324 0 /*is_dwp*/, 1 /*search_cwd*/);
13325 }
13326
13327 /* This function is mapped across the sections and remembers the offset and
13328 size of each of the DWO debugging sections we are interested in. */
13329
13330 static void
13331 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
13332 {
13333 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
13334 const struct dwop_section_names *names = &dwop_section_names;
13335
13336 if (section_is_p (sectp->name, &names->abbrev_dwo))
13337 {
13338 dwo_sections->abbrev.s.section = sectp;
13339 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
13340 }
13341 else if (section_is_p (sectp->name, &names->info_dwo))
13342 {
13343 dwo_sections->info.s.section = sectp;
13344 dwo_sections->info.size = bfd_get_section_size (sectp);
13345 }
13346 else if (section_is_p (sectp->name, &names->line_dwo))
13347 {
13348 dwo_sections->line.s.section = sectp;
13349 dwo_sections->line.size = bfd_get_section_size (sectp);
13350 }
13351 else if (section_is_p (sectp->name, &names->loc_dwo))
13352 {
13353 dwo_sections->loc.s.section = sectp;
13354 dwo_sections->loc.size = bfd_get_section_size (sectp);
13355 }
13356 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13357 {
13358 dwo_sections->macinfo.s.section = sectp;
13359 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
13360 }
13361 else if (section_is_p (sectp->name, &names->macro_dwo))
13362 {
13363 dwo_sections->macro.s.section = sectp;
13364 dwo_sections->macro.size = bfd_get_section_size (sectp);
13365 }
13366 else if (section_is_p (sectp->name, &names->str_dwo))
13367 {
13368 dwo_sections->str.s.section = sectp;
13369 dwo_sections->str.size = bfd_get_section_size (sectp);
13370 }
13371 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13372 {
13373 dwo_sections->str_offsets.s.section = sectp;
13374 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
13375 }
13376 else if (section_is_p (sectp->name, &names->types_dwo))
13377 {
13378 struct dwarf2_section_info type_section;
13379
13380 memset (&type_section, 0, sizeof (type_section));
13381 type_section.s.section = sectp;
13382 type_section.size = bfd_get_section_size (sectp);
13383 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
13384 &type_section);
13385 }
13386 }
13387
13388 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13389 by PER_CU. This is for the non-DWP case.
13390 The result is NULL if DWO_NAME can't be found. */
13391
13392 static struct dwo_file *
13393 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13394 const char *dwo_name, const char *comp_dir)
13395 {
13396 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13397 struct objfile *objfile = dwarf2_per_objfile->objfile;
13398 struct dwo_file *dwo_file;
13399 struct cleanup *cleanups;
13400
13401 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
13402 if (dbfd == NULL)
13403 {
13404 if (dwarf_read_debug)
13405 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13406 return NULL;
13407 }
13408 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
13409 dwo_file->dwo_name = dwo_name;
13410 dwo_file->comp_dir = comp_dir;
13411 dwo_file->dbfd = dbfd.release ();
13412
13413 free_dwo_file_cleanup_data *cleanup_data = XNEW (free_dwo_file_cleanup_data);
13414 cleanup_data->dwo_file = dwo_file;
13415 cleanup_data->dwarf2_per_objfile = dwarf2_per_objfile;
13416
13417 cleanups = make_cleanup (free_dwo_file_cleanup, cleanup_data);
13418
13419 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13420 &dwo_file->sections);
13421
13422 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13423 dwo_file->cus);
13424
13425 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file,
13426 dwo_file->sections.types, dwo_file->tus);
13427
13428 discard_cleanups (cleanups);
13429
13430 if (dwarf_read_debug)
13431 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13432
13433 return dwo_file;
13434 }
13435
13436 /* This function is mapped across the sections and remembers the offset and
13437 size of each of the DWP debugging sections common to version 1 and 2 that
13438 we are interested in. */
13439
13440 static void
13441 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13442 void *dwp_file_ptr)
13443 {
13444 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13445 const struct dwop_section_names *names = &dwop_section_names;
13446 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13447
13448 /* Record the ELF section number for later lookup: this is what the
13449 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13450 gdb_assert (elf_section_nr < dwp_file->num_sections);
13451 dwp_file->elf_sections[elf_section_nr] = sectp;
13452
13453 /* Look for specific sections that we need. */
13454 if (section_is_p (sectp->name, &names->str_dwo))
13455 {
13456 dwp_file->sections.str.s.section = sectp;
13457 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13458 }
13459 else if (section_is_p (sectp->name, &names->cu_index))
13460 {
13461 dwp_file->sections.cu_index.s.section = sectp;
13462 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13463 }
13464 else if (section_is_p (sectp->name, &names->tu_index))
13465 {
13466 dwp_file->sections.tu_index.s.section = sectp;
13467 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13468 }
13469 }
13470
13471 /* This function is mapped across the sections and remembers the offset and
13472 size of each of the DWP version 2 debugging sections that we are interested
13473 in. This is split into a separate function because we don't know if we
13474 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13475
13476 static void
13477 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13478 {
13479 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13480 const struct dwop_section_names *names = &dwop_section_names;
13481 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13482
13483 /* Record the ELF section number for later lookup: this is what the
13484 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13485 gdb_assert (elf_section_nr < dwp_file->num_sections);
13486 dwp_file->elf_sections[elf_section_nr] = sectp;
13487
13488 /* Look for specific sections that we need. */
13489 if (section_is_p (sectp->name, &names->abbrev_dwo))
13490 {
13491 dwp_file->sections.abbrev.s.section = sectp;
13492 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13493 }
13494 else if (section_is_p (sectp->name, &names->info_dwo))
13495 {
13496 dwp_file->sections.info.s.section = sectp;
13497 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13498 }
13499 else if (section_is_p (sectp->name, &names->line_dwo))
13500 {
13501 dwp_file->sections.line.s.section = sectp;
13502 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13503 }
13504 else if (section_is_p (sectp->name, &names->loc_dwo))
13505 {
13506 dwp_file->sections.loc.s.section = sectp;
13507 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13508 }
13509 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13510 {
13511 dwp_file->sections.macinfo.s.section = sectp;
13512 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13513 }
13514 else if (section_is_p (sectp->name, &names->macro_dwo))
13515 {
13516 dwp_file->sections.macro.s.section = sectp;
13517 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13518 }
13519 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13520 {
13521 dwp_file->sections.str_offsets.s.section = sectp;
13522 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13523 }
13524 else if (section_is_p (sectp->name, &names->types_dwo))
13525 {
13526 dwp_file->sections.types.s.section = sectp;
13527 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13528 }
13529 }
13530
13531 /* Hash function for dwp_file loaded CUs/TUs. */
13532
13533 static hashval_t
13534 hash_dwp_loaded_cutus (const void *item)
13535 {
13536 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13537
13538 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13539 return dwo_unit->signature;
13540 }
13541
13542 /* Equality function for dwp_file loaded CUs/TUs. */
13543
13544 static int
13545 eq_dwp_loaded_cutus (const void *a, const void *b)
13546 {
13547 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13548 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13549
13550 return dua->signature == dub->signature;
13551 }
13552
13553 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13554
13555 static htab_t
13556 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13557 {
13558 return htab_create_alloc_ex (3,
13559 hash_dwp_loaded_cutus,
13560 eq_dwp_loaded_cutus,
13561 NULL,
13562 &objfile->objfile_obstack,
13563 hashtab_obstack_allocate,
13564 dummy_obstack_deallocate);
13565 }
13566
13567 /* Try to open DWP file FILE_NAME.
13568 The result is the bfd handle of the file.
13569 If there is a problem finding or opening the file, return NULL.
13570 Upon success, the canonicalized path of the file is stored in the bfd,
13571 same as symfile_bfd_open. */
13572
13573 static gdb_bfd_ref_ptr
13574 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13575 const char *file_name)
13576 {
13577 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13578 1 /*is_dwp*/,
13579 1 /*search_cwd*/));
13580 if (abfd != NULL)
13581 return abfd;
13582
13583 /* Work around upstream bug 15652.
13584 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13585 [Whether that's a "bug" is debatable, but it is getting in our way.]
13586 We have no real idea where the dwp file is, because gdb's realpath-ing
13587 of the executable's path may have discarded the needed info.
13588 [IWBN if the dwp file name was recorded in the executable, akin to
13589 .gnu_debuglink, but that doesn't exist yet.]
13590 Strip the directory from FILE_NAME and search again. */
13591 if (*debug_file_directory != '\0')
13592 {
13593 /* Don't implicitly search the current directory here.
13594 If the user wants to search "." to handle this case,
13595 it must be added to debug-file-directory. */
13596 return try_open_dwop_file (dwarf2_per_objfile,
13597 lbasename (file_name), 1 /*is_dwp*/,
13598 0 /*search_cwd*/);
13599 }
13600
13601 return NULL;
13602 }
13603
13604 /* Initialize the use of the DWP file for the current objfile.
13605 By convention the name of the DWP file is ${objfile}.dwp.
13606 The result is NULL if it can't be found. */
13607
13608 static struct dwp_file *
13609 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13610 {
13611 struct objfile *objfile = dwarf2_per_objfile->objfile;
13612 struct dwp_file *dwp_file;
13613
13614 /* Try to find first .dwp for the binary file before any symbolic links
13615 resolving. */
13616
13617 /* If the objfile is a debug file, find the name of the real binary
13618 file and get the name of dwp file from there. */
13619 std::string dwp_name;
13620 if (objfile->separate_debug_objfile_backlink != NULL)
13621 {
13622 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13623 const char *backlink_basename = lbasename (backlink->original_name);
13624
13625 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13626 }
13627 else
13628 dwp_name = objfile->original_name;
13629
13630 dwp_name += ".dwp";
13631
13632 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13633 if (dbfd == NULL
13634 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13635 {
13636 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13637 dwp_name = objfile_name (objfile);
13638 dwp_name += ".dwp";
13639 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13640 }
13641
13642 if (dbfd == NULL)
13643 {
13644 if (dwarf_read_debug)
13645 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13646 return NULL;
13647 }
13648 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
13649 dwp_file->name = bfd_get_filename (dbfd.get ());
13650 dwp_file->dbfd = dbfd.release ();
13651
13652 /* +1: section 0 is unused */
13653 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13654 dwp_file->elf_sections =
13655 OBSTACK_CALLOC (&objfile->objfile_obstack,
13656 dwp_file->num_sections, asection *);
13657
13658 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13659 dwp_file);
13660
13661 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
13662
13663 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
13664
13665 /* The DWP file version is stored in the hash table. Oh well. */
13666 if (dwp_file->cus && dwp_file->tus
13667 && dwp_file->cus->version != dwp_file->tus->version)
13668 {
13669 /* Technically speaking, we should try to limp along, but this is
13670 pretty bizarre. We use pulongest here because that's the established
13671 portability solution (e.g, we cannot use %u for uint32_t). */
13672 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13673 " TU version %s [in DWP file %s]"),
13674 pulongest (dwp_file->cus->version),
13675 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13676 }
13677
13678 if (dwp_file->cus)
13679 dwp_file->version = dwp_file->cus->version;
13680 else if (dwp_file->tus)
13681 dwp_file->version = dwp_file->tus->version;
13682 else
13683 dwp_file->version = 2;
13684
13685 if (dwp_file->version == 2)
13686 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13687 dwp_file);
13688
13689 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13690 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13691
13692 if (dwarf_read_debug)
13693 {
13694 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13695 fprintf_unfiltered (gdb_stdlog,
13696 " %s CUs, %s TUs\n",
13697 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13698 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13699 }
13700
13701 return dwp_file;
13702 }
13703
13704 /* Wrapper around open_and_init_dwp_file, only open it once. */
13705
13706 static struct dwp_file *
13707 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13708 {
13709 if (! dwarf2_per_objfile->dwp_checked)
13710 {
13711 dwarf2_per_objfile->dwp_file
13712 = open_and_init_dwp_file (dwarf2_per_objfile);
13713 dwarf2_per_objfile->dwp_checked = 1;
13714 }
13715 return dwarf2_per_objfile->dwp_file;
13716 }
13717
13718 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13719 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13720 or in the DWP file for the objfile, referenced by THIS_UNIT.
13721 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13722 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13723
13724 This is called, for example, when wanting to read a variable with a
13725 complex location. Therefore we don't want to do file i/o for every call.
13726 Therefore we don't want to look for a DWO file on every call.
13727 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13728 then we check if we've already seen DWO_NAME, and only THEN do we check
13729 for a DWO file.
13730
13731 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13732 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13733
13734 static struct dwo_unit *
13735 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13736 const char *dwo_name, const char *comp_dir,
13737 ULONGEST signature, int is_debug_types)
13738 {
13739 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13740 struct objfile *objfile = dwarf2_per_objfile->objfile;
13741 const char *kind = is_debug_types ? "TU" : "CU";
13742 void **dwo_file_slot;
13743 struct dwo_file *dwo_file;
13744 struct dwp_file *dwp_file;
13745
13746 /* First see if there's a DWP file.
13747 If we have a DWP file but didn't find the DWO inside it, don't
13748 look for the original DWO file. It makes gdb behave differently
13749 depending on whether one is debugging in the build tree. */
13750
13751 dwp_file = get_dwp_file (dwarf2_per_objfile);
13752 if (dwp_file != NULL)
13753 {
13754 const struct dwp_hash_table *dwp_htab =
13755 is_debug_types ? dwp_file->tus : dwp_file->cus;
13756
13757 if (dwp_htab != NULL)
13758 {
13759 struct dwo_unit *dwo_cutu =
13760 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13761 signature, is_debug_types);
13762
13763 if (dwo_cutu != NULL)
13764 {
13765 if (dwarf_read_debug)
13766 {
13767 fprintf_unfiltered (gdb_stdlog,
13768 "Virtual DWO %s %s found: @%s\n",
13769 kind, hex_string (signature),
13770 host_address_to_string (dwo_cutu));
13771 }
13772 return dwo_cutu;
13773 }
13774 }
13775 }
13776 else
13777 {
13778 /* No DWP file, look for the DWO file. */
13779
13780 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13781 dwo_name, comp_dir);
13782 if (*dwo_file_slot == NULL)
13783 {
13784 /* Read in the file and build a table of the CUs/TUs it contains. */
13785 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13786 }
13787 /* NOTE: This will be NULL if unable to open the file. */
13788 dwo_file = (struct dwo_file *) *dwo_file_slot;
13789
13790 if (dwo_file != NULL)
13791 {
13792 struct dwo_unit *dwo_cutu = NULL;
13793
13794 if (is_debug_types && dwo_file->tus)
13795 {
13796 struct dwo_unit find_dwo_cutu;
13797
13798 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13799 find_dwo_cutu.signature = signature;
13800 dwo_cutu
13801 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13802 }
13803 else if (!is_debug_types && dwo_file->cus)
13804 {
13805 struct dwo_unit find_dwo_cutu;
13806
13807 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13808 find_dwo_cutu.signature = signature;
13809 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13810 &find_dwo_cutu);
13811 }
13812
13813 if (dwo_cutu != NULL)
13814 {
13815 if (dwarf_read_debug)
13816 {
13817 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13818 kind, dwo_name, hex_string (signature),
13819 host_address_to_string (dwo_cutu));
13820 }
13821 return dwo_cutu;
13822 }
13823 }
13824 }
13825
13826 /* We didn't find it. This could mean a dwo_id mismatch, or
13827 someone deleted the DWO/DWP file, or the search path isn't set up
13828 correctly to find the file. */
13829
13830 if (dwarf_read_debug)
13831 {
13832 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13833 kind, dwo_name, hex_string (signature));
13834 }
13835
13836 /* This is a warning and not a complaint because it can be caused by
13837 pilot error (e.g., user accidentally deleting the DWO). */
13838 {
13839 /* Print the name of the DWP file if we looked there, helps the user
13840 better diagnose the problem. */
13841 std::string dwp_text;
13842
13843 if (dwp_file != NULL)
13844 dwp_text = string_printf (" [in DWP file %s]",
13845 lbasename (dwp_file->name));
13846
13847 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13848 " [in module %s]"),
13849 kind, dwo_name, hex_string (signature),
13850 dwp_text.c_str (),
13851 this_unit->is_debug_types ? "TU" : "CU",
13852 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13853 }
13854 return NULL;
13855 }
13856
13857 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13858 See lookup_dwo_cutu_unit for details. */
13859
13860 static struct dwo_unit *
13861 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13862 const char *dwo_name, const char *comp_dir,
13863 ULONGEST signature)
13864 {
13865 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13866 }
13867
13868 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13869 See lookup_dwo_cutu_unit for details. */
13870
13871 static struct dwo_unit *
13872 lookup_dwo_type_unit (struct signatured_type *this_tu,
13873 const char *dwo_name, const char *comp_dir)
13874 {
13875 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13876 }
13877
13878 /* Traversal function for queue_and_load_all_dwo_tus. */
13879
13880 static int
13881 queue_and_load_dwo_tu (void **slot, void *info)
13882 {
13883 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13884 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13885 ULONGEST signature = dwo_unit->signature;
13886 struct signatured_type *sig_type =
13887 lookup_dwo_signatured_type (per_cu->cu, signature);
13888
13889 if (sig_type != NULL)
13890 {
13891 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13892
13893 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13894 a real dependency of PER_CU on SIG_TYPE. That is detected later
13895 while processing PER_CU. */
13896 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13897 load_full_type_unit (sig_cu);
13898 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13899 }
13900
13901 return 1;
13902 }
13903
13904 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13905 The DWO may have the only definition of the type, though it may not be
13906 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13907 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13908
13909 static void
13910 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13911 {
13912 struct dwo_unit *dwo_unit;
13913 struct dwo_file *dwo_file;
13914
13915 gdb_assert (!per_cu->is_debug_types);
13916 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13917 gdb_assert (per_cu->cu != NULL);
13918
13919 dwo_unit = per_cu->cu->dwo_unit;
13920 gdb_assert (dwo_unit != NULL);
13921
13922 dwo_file = dwo_unit->dwo_file;
13923 if (dwo_file->tus != NULL)
13924 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13925 }
13926
13927 /* Free all resources associated with DWO_FILE.
13928 Close the DWO file and munmap the sections.
13929 All memory should be on the objfile obstack. */
13930
13931 static void
13932 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
13933 {
13934
13935 /* Note: dbfd is NULL for virtual DWO files. */
13936 gdb_bfd_unref (dwo_file->dbfd);
13937
13938 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13939 }
13940
13941 /* Wrapper for free_dwo_file for use in cleanups. */
13942
13943 static void
13944 free_dwo_file_cleanup (void *arg)
13945 {
13946 struct free_dwo_file_cleanup_data *data
13947 = (struct free_dwo_file_cleanup_data *) arg;
13948 struct objfile *objfile = data->dwarf2_per_objfile->objfile;
13949
13950 free_dwo_file (data->dwo_file, objfile);
13951
13952 xfree (data);
13953 }
13954
13955 /* Traversal function for free_dwo_files. */
13956
13957 static int
13958 free_dwo_file_from_slot (void **slot, void *info)
13959 {
13960 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13961 struct objfile *objfile = (struct objfile *) info;
13962
13963 free_dwo_file (dwo_file, objfile);
13964
13965 return 1;
13966 }
13967
13968 /* Free all resources associated with DWO_FILES. */
13969
13970 static void
13971 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13972 {
13973 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13974 }
13975 \f
13976 /* Read in various DIEs. */
13977
13978 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13979 Inherit only the children of the DW_AT_abstract_origin DIE not being
13980 already referenced by DW_AT_abstract_origin from the children of the
13981 current DIE. */
13982
13983 static void
13984 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13985 {
13986 struct die_info *child_die;
13987 sect_offset *offsetp;
13988 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13989 struct die_info *origin_die;
13990 /* Iterator of the ORIGIN_DIE children. */
13991 struct die_info *origin_child_die;
13992 struct attribute *attr;
13993 struct dwarf2_cu *origin_cu;
13994 struct pending **origin_previous_list_in_scope;
13995
13996 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13997 if (!attr)
13998 return;
13999
14000 /* Note that following die references may follow to a die in a
14001 different cu. */
14002
14003 origin_cu = cu;
14004 origin_die = follow_die_ref (die, attr, &origin_cu);
14005
14006 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
14007 symbols in. */
14008 origin_previous_list_in_scope = origin_cu->list_in_scope;
14009 origin_cu->list_in_scope = cu->list_in_scope;
14010
14011 if (die->tag != origin_die->tag
14012 && !(die->tag == DW_TAG_inlined_subroutine
14013 && origin_die->tag == DW_TAG_subprogram))
14014 complaint (&symfile_complaints,
14015 _("DIE %s and its abstract origin %s have different tags"),
14016 sect_offset_str (die->sect_off),
14017 sect_offset_str (origin_die->sect_off));
14018
14019 std::vector<sect_offset> offsets;
14020
14021 for (child_die = die->child;
14022 child_die && child_die->tag;
14023 child_die = sibling_die (child_die))
14024 {
14025 struct die_info *child_origin_die;
14026 struct dwarf2_cu *child_origin_cu;
14027
14028 /* We are trying to process concrete instance entries:
14029 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
14030 it's not relevant to our analysis here. i.e. detecting DIEs that are
14031 present in the abstract instance but not referenced in the concrete
14032 one. */
14033 if (child_die->tag == DW_TAG_call_site
14034 || child_die->tag == DW_TAG_GNU_call_site)
14035 continue;
14036
14037 /* For each CHILD_DIE, find the corresponding child of
14038 ORIGIN_DIE. If there is more than one layer of
14039 DW_AT_abstract_origin, follow them all; there shouldn't be,
14040 but GCC versions at least through 4.4 generate this (GCC PR
14041 40573). */
14042 child_origin_die = child_die;
14043 child_origin_cu = cu;
14044 while (1)
14045 {
14046 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
14047 child_origin_cu);
14048 if (attr == NULL)
14049 break;
14050 child_origin_die = follow_die_ref (child_origin_die, attr,
14051 &child_origin_cu);
14052 }
14053
14054 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
14055 counterpart may exist. */
14056 if (child_origin_die != child_die)
14057 {
14058 if (child_die->tag != child_origin_die->tag
14059 && !(child_die->tag == DW_TAG_inlined_subroutine
14060 && child_origin_die->tag == DW_TAG_subprogram))
14061 complaint (&symfile_complaints,
14062 _("Child DIE %s and its abstract origin %s have "
14063 "different tags"),
14064 sect_offset_str (child_die->sect_off),
14065 sect_offset_str (child_origin_die->sect_off));
14066 if (child_origin_die->parent != origin_die)
14067 complaint (&symfile_complaints,
14068 _("Child DIE %s and its abstract origin %s have "
14069 "different parents"),
14070 sect_offset_str (child_die->sect_off),
14071 sect_offset_str (child_origin_die->sect_off));
14072 else
14073 offsets.push_back (child_origin_die->sect_off);
14074 }
14075 }
14076 std::sort (offsets.begin (), offsets.end ());
14077 sect_offset *offsets_end = offsets.data () + offsets.size ();
14078 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
14079 if (offsetp[-1] == *offsetp)
14080 complaint (&symfile_complaints,
14081 _("Multiple children of DIE %s refer "
14082 "to DIE %s as their abstract origin"),
14083 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
14084
14085 offsetp = offsets.data ();
14086 origin_child_die = origin_die->child;
14087 while (origin_child_die && origin_child_die->tag)
14088 {
14089 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
14090 while (offsetp < offsets_end
14091 && *offsetp < origin_child_die->sect_off)
14092 offsetp++;
14093 if (offsetp >= offsets_end
14094 || *offsetp > origin_child_die->sect_off)
14095 {
14096 /* Found that ORIGIN_CHILD_DIE is really not referenced.
14097 Check whether we're already processing ORIGIN_CHILD_DIE.
14098 This can happen with mutually referenced abstract_origins.
14099 PR 16581. */
14100 if (!origin_child_die->in_process)
14101 process_die (origin_child_die, origin_cu);
14102 }
14103 origin_child_die = sibling_die (origin_child_die);
14104 }
14105 origin_cu->list_in_scope = origin_previous_list_in_scope;
14106 }
14107
14108 static void
14109 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
14110 {
14111 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14112 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14113 struct context_stack *newobj;
14114 CORE_ADDR lowpc;
14115 CORE_ADDR highpc;
14116 struct die_info *child_die;
14117 struct attribute *attr, *call_line, *call_file;
14118 const char *name;
14119 CORE_ADDR baseaddr;
14120 struct block *block;
14121 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
14122 std::vector<struct symbol *> template_args;
14123 struct template_symbol *templ_func = NULL;
14124
14125 if (inlined_func)
14126 {
14127 /* If we do not have call site information, we can't show the
14128 caller of this inlined function. That's too confusing, so
14129 only use the scope for local variables. */
14130 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
14131 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
14132 if (call_line == NULL || call_file == NULL)
14133 {
14134 read_lexical_block_scope (die, cu);
14135 return;
14136 }
14137 }
14138
14139 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14140
14141 name = dwarf2_name (die, cu);
14142
14143 /* Ignore functions with missing or empty names. These are actually
14144 illegal according to the DWARF standard. */
14145 if (name == NULL)
14146 {
14147 complaint (&symfile_complaints,
14148 _("missing name for subprogram DIE at %s"),
14149 sect_offset_str (die->sect_off));
14150 return;
14151 }
14152
14153 /* Ignore functions with missing or invalid low and high pc attributes. */
14154 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
14155 <= PC_BOUNDS_INVALID)
14156 {
14157 attr = dwarf2_attr (die, DW_AT_external, cu);
14158 if (!attr || !DW_UNSND (attr))
14159 complaint (&symfile_complaints,
14160 _("cannot get low and high bounds "
14161 "for subprogram DIE at %s"),
14162 sect_offset_str (die->sect_off));
14163 return;
14164 }
14165
14166 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14167 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
14168
14169 /* If we have any template arguments, then we must allocate a
14170 different sort of symbol. */
14171 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
14172 {
14173 if (child_die->tag == DW_TAG_template_type_param
14174 || child_die->tag == DW_TAG_template_value_param)
14175 {
14176 templ_func = allocate_template_symbol (objfile);
14177 templ_func->subclass = SYMBOL_TEMPLATE;
14178 break;
14179 }
14180 }
14181
14182 newobj = push_context (0, lowpc);
14183 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
14184 (struct symbol *) templ_func);
14185
14186 /* If there is a location expression for DW_AT_frame_base, record
14187 it. */
14188 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
14189 if (attr)
14190 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
14191
14192 /* If there is a location for the static link, record it. */
14193 newobj->static_link = NULL;
14194 attr = dwarf2_attr (die, DW_AT_static_link, cu);
14195 if (attr)
14196 {
14197 newobj->static_link
14198 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
14199 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
14200 }
14201
14202 cu->list_in_scope = &local_symbols;
14203
14204 if (die->child != NULL)
14205 {
14206 child_die = die->child;
14207 while (child_die && child_die->tag)
14208 {
14209 if (child_die->tag == DW_TAG_template_type_param
14210 || child_die->tag == DW_TAG_template_value_param)
14211 {
14212 struct symbol *arg = new_symbol (child_die, NULL, cu);
14213
14214 if (arg != NULL)
14215 template_args.push_back (arg);
14216 }
14217 else
14218 process_die (child_die, cu);
14219 child_die = sibling_die (child_die);
14220 }
14221 }
14222
14223 inherit_abstract_dies (die, cu);
14224
14225 /* If we have a DW_AT_specification, we might need to import using
14226 directives from the context of the specification DIE. See the
14227 comment in determine_prefix. */
14228 if (cu->language == language_cplus
14229 && dwarf2_attr (die, DW_AT_specification, cu))
14230 {
14231 struct dwarf2_cu *spec_cu = cu;
14232 struct die_info *spec_die = die_specification (die, &spec_cu);
14233
14234 while (spec_die)
14235 {
14236 child_die = spec_die->child;
14237 while (child_die && child_die->tag)
14238 {
14239 if (child_die->tag == DW_TAG_imported_module)
14240 process_die (child_die, spec_cu);
14241 child_die = sibling_die (child_die);
14242 }
14243
14244 /* In some cases, GCC generates specification DIEs that
14245 themselves contain DW_AT_specification attributes. */
14246 spec_die = die_specification (spec_die, &spec_cu);
14247 }
14248 }
14249
14250 newobj = pop_context ();
14251 /* Make a block for the local symbols within. */
14252 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
14253 newobj->static_link, lowpc, highpc);
14254
14255 /* For C++, set the block's scope. */
14256 if ((cu->language == language_cplus
14257 || cu->language == language_fortran
14258 || cu->language == language_d
14259 || cu->language == language_rust)
14260 && cu->processing_has_namespace_info)
14261 block_set_scope (block, determine_prefix (die, cu),
14262 &objfile->objfile_obstack);
14263
14264 /* If we have address ranges, record them. */
14265 dwarf2_record_block_ranges (die, block, baseaddr, cu);
14266
14267 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
14268
14269 /* Attach template arguments to function. */
14270 if (!template_args.empty ())
14271 {
14272 gdb_assert (templ_func != NULL);
14273
14274 templ_func->n_template_arguments = template_args.size ();
14275 templ_func->template_arguments
14276 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
14277 templ_func->n_template_arguments);
14278 memcpy (templ_func->template_arguments,
14279 template_args.data (),
14280 (templ_func->n_template_arguments * sizeof (struct symbol *)));
14281 }
14282
14283 /* In C++, we can have functions nested inside functions (e.g., when
14284 a function declares a class that has methods). This means that
14285 when we finish processing a function scope, we may need to go
14286 back to building a containing block's symbol lists. */
14287 local_symbols = newobj->locals;
14288 local_using_directives = newobj->local_using_directives;
14289
14290 /* If we've finished processing a top-level function, subsequent
14291 symbols go in the file symbol list. */
14292 if (outermost_context_p ())
14293 cu->list_in_scope = &file_symbols;
14294 }
14295
14296 /* Process all the DIES contained within a lexical block scope. Start
14297 a new scope, process the dies, and then close the scope. */
14298
14299 static void
14300 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
14301 {
14302 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14303 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14304 struct context_stack *newobj;
14305 CORE_ADDR lowpc, highpc;
14306 struct die_info *child_die;
14307 CORE_ADDR baseaddr;
14308
14309 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14310
14311 /* Ignore blocks with missing or invalid low and high pc attributes. */
14312 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
14313 as multiple lexical blocks? Handling children in a sane way would
14314 be nasty. Might be easier to properly extend generic blocks to
14315 describe ranges. */
14316 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
14317 {
14318 case PC_BOUNDS_NOT_PRESENT:
14319 /* DW_TAG_lexical_block has no attributes, process its children as if
14320 there was no wrapping by that DW_TAG_lexical_block.
14321 GCC does no longer produces such DWARF since GCC r224161. */
14322 for (child_die = die->child;
14323 child_die != NULL && child_die->tag;
14324 child_die = sibling_die (child_die))
14325 process_die (child_die, cu);
14326 return;
14327 case PC_BOUNDS_INVALID:
14328 return;
14329 }
14330 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14331 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
14332
14333 push_context (0, lowpc);
14334 if (die->child != NULL)
14335 {
14336 child_die = die->child;
14337 while (child_die && child_die->tag)
14338 {
14339 process_die (child_die, cu);
14340 child_die = sibling_die (child_die);
14341 }
14342 }
14343 inherit_abstract_dies (die, cu);
14344 newobj = pop_context ();
14345
14346 if (local_symbols != NULL || local_using_directives != NULL)
14347 {
14348 struct block *block
14349 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
14350 newobj->start_addr, highpc);
14351
14352 /* Note that recording ranges after traversing children, as we
14353 do here, means that recording a parent's ranges entails
14354 walking across all its children's ranges as they appear in
14355 the address map, which is quadratic behavior.
14356
14357 It would be nicer to record the parent's ranges before
14358 traversing its children, simply overriding whatever you find
14359 there. But since we don't even decide whether to create a
14360 block until after we've traversed its children, that's hard
14361 to do. */
14362 dwarf2_record_block_ranges (die, block, baseaddr, cu);
14363 }
14364 local_symbols = newobj->locals;
14365 local_using_directives = newobj->local_using_directives;
14366 }
14367
14368 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
14369
14370 static void
14371 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
14372 {
14373 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14374 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14375 CORE_ADDR pc, baseaddr;
14376 struct attribute *attr;
14377 struct call_site *call_site, call_site_local;
14378 void **slot;
14379 int nparams;
14380 struct die_info *child_die;
14381
14382 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14383
14384 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
14385 if (attr == NULL)
14386 {
14387 /* This was a pre-DWARF-5 GNU extension alias
14388 for DW_AT_call_return_pc. */
14389 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14390 }
14391 if (!attr)
14392 {
14393 complaint (&symfile_complaints,
14394 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
14395 "DIE %s [in module %s]"),
14396 sect_offset_str (die->sect_off), objfile_name (objfile));
14397 return;
14398 }
14399 pc = attr_value_as_address (attr) + baseaddr;
14400 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
14401
14402 if (cu->call_site_htab == NULL)
14403 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14404 NULL, &objfile->objfile_obstack,
14405 hashtab_obstack_allocate, NULL);
14406 call_site_local.pc = pc;
14407 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14408 if (*slot != NULL)
14409 {
14410 complaint (&symfile_complaints,
14411 _("Duplicate PC %s for DW_TAG_call_site "
14412 "DIE %s [in module %s]"),
14413 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
14414 objfile_name (objfile));
14415 return;
14416 }
14417
14418 /* Count parameters at the caller. */
14419
14420 nparams = 0;
14421 for (child_die = die->child; child_die && child_die->tag;
14422 child_die = sibling_die (child_die))
14423 {
14424 if (child_die->tag != DW_TAG_call_site_parameter
14425 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14426 {
14427 complaint (&symfile_complaints,
14428 _("Tag %d is not DW_TAG_call_site_parameter in "
14429 "DW_TAG_call_site child DIE %s [in module %s]"),
14430 child_die->tag, sect_offset_str (child_die->sect_off),
14431 objfile_name (objfile));
14432 continue;
14433 }
14434
14435 nparams++;
14436 }
14437
14438 call_site
14439 = ((struct call_site *)
14440 obstack_alloc (&objfile->objfile_obstack,
14441 sizeof (*call_site)
14442 + (sizeof (*call_site->parameter) * (nparams - 1))));
14443 *slot = call_site;
14444 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14445 call_site->pc = pc;
14446
14447 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14448 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14449 {
14450 struct die_info *func_die;
14451
14452 /* Skip also over DW_TAG_inlined_subroutine. */
14453 for (func_die = die->parent;
14454 func_die && func_die->tag != DW_TAG_subprogram
14455 && func_die->tag != DW_TAG_subroutine_type;
14456 func_die = func_die->parent);
14457
14458 /* DW_AT_call_all_calls is a superset
14459 of DW_AT_call_all_tail_calls. */
14460 if (func_die
14461 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14462 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14463 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14464 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14465 {
14466 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14467 not complete. But keep CALL_SITE for look ups via call_site_htab,
14468 both the initial caller containing the real return address PC and
14469 the final callee containing the current PC of a chain of tail
14470 calls do not need to have the tail call list complete. But any
14471 function candidate for a virtual tail call frame searched via
14472 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14473 determined unambiguously. */
14474 }
14475 else
14476 {
14477 struct type *func_type = NULL;
14478
14479 if (func_die)
14480 func_type = get_die_type (func_die, cu);
14481 if (func_type != NULL)
14482 {
14483 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14484
14485 /* Enlist this call site to the function. */
14486 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14487 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14488 }
14489 else
14490 complaint (&symfile_complaints,
14491 _("Cannot find function owning DW_TAG_call_site "
14492 "DIE %s [in module %s]"),
14493 sect_offset_str (die->sect_off), objfile_name (objfile));
14494 }
14495 }
14496
14497 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14498 if (attr == NULL)
14499 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14500 if (attr == NULL)
14501 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14502 if (attr == NULL)
14503 {
14504 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14505 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14506 }
14507 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14508 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14509 /* Keep NULL DWARF_BLOCK. */;
14510 else if (attr_form_is_block (attr))
14511 {
14512 struct dwarf2_locexpr_baton *dlbaton;
14513
14514 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14515 dlbaton->data = DW_BLOCK (attr)->data;
14516 dlbaton->size = DW_BLOCK (attr)->size;
14517 dlbaton->per_cu = cu->per_cu;
14518
14519 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14520 }
14521 else if (attr_form_is_ref (attr))
14522 {
14523 struct dwarf2_cu *target_cu = cu;
14524 struct die_info *target_die;
14525
14526 target_die = follow_die_ref (die, attr, &target_cu);
14527 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14528 if (die_is_declaration (target_die, target_cu))
14529 {
14530 const char *target_physname;
14531
14532 /* Prefer the mangled name; otherwise compute the demangled one. */
14533 target_physname = dw2_linkage_name (target_die, target_cu);
14534 if (target_physname == NULL)
14535 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14536 if (target_physname == NULL)
14537 complaint (&symfile_complaints,
14538 _("DW_AT_call_target target DIE has invalid "
14539 "physname, for referencing DIE %s [in module %s]"),
14540 sect_offset_str (die->sect_off), objfile_name (objfile));
14541 else
14542 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14543 }
14544 else
14545 {
14546 CORE_ADDR lowpc;
14547
14548 /* DW_AT_entry_pc should be preferred. */
14549 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14550 <= PC_BOUNDS_INVALID)
14551 complaint (&symfile_complaints,
14552 _("DW_AT_call_target target DIE has invalid "
14553 "low pc, for referencing DIE %s [in module %s]"),
14554 sect_offset_str (die->sect_off), objfile_name (objfile));
14555 else
14556 {
14557 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14558 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14559 }
14560 }
14561 }
14562 else
14563 complaint (&symfile_complaints,
14564 _("DW_TAG_call_site DW_AT_call_target is neither "
14565 "block nor reference, for DIE %s [in module %s]"),
14566 sect_offset_str (die->sect_off), objfile_name (objfile));
14567
14568 call_site->per_cu = cu->per_cu;
14569
14570 for (child_die = die->child;
14571 child_die && child_die->tag;
14572 child_die = sibling_die (child_die))
14573 {
14574 struct call_site_parameter *parameter;
14575 struct attribute *loc, *origin;
14576
14577 if (child_die->tag != DW_TAG_call_site_parameter
14578 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14579 {
14580 /* Already printed the complaint above. */
14581 continue;
14582 }
14583
14584 gdb_assert (call_site->parameter_count < nparams);
14585 parameter = &call_site->parameter[call_site->parameter_count];
14586
14587 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14588 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14589 register is contained in DW_AT_call_value. */
14590
14591 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14592 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14593 if (origin == NULL)
14594 {
14595 /* This was a pre-DWARF-5 GNU extension alias
14596 for DW_AT_call_parameter. */
14597 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14598 }
14599 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14600 {
14601 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14602
14603 sect_offset sect_off
14604 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14605 if (!offset_in_cu_p (&cu->header, sect_off))
14606 {
14607 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14608 binding can be done only inside one CU. Such referenced DIE
14609 therefore cannot be even moved to DW_TAG_partial_unit. */
14610 complaint (&symfile_complaints,
14611 _("DW_AT_call_parameter offset is not in CU for "
14612 "DW_TAG_call_site child DIE %s [in module %s]"),
14613 sect_offset_str (child_die->sect_off),
14614 objfile_name (objfile));
14615 continue;
14616 }
14617 parameter->u.param_cu_off
14618 = (cu_offset) (sect_off - cu->header.sect_off);
14619 }
14620 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14621 {
14622 complaint (&symfile_complaints,
14623 _("No DW_FORM_block* DW_AT_location for "
14624 "DW_TAG_call_site child DIE %s [in module %s]"),
14625 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14626 continue;
14627 }
14628 else
14629 {
14630 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14631 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14632 if (parameter->u.dwarf_reg != -1)
14633 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14634 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14635 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14636 &parameter->u.fb_offset))
14637 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14638 else
14639 {
14640 complaint (&symfile_complaints,
14641 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14642 "for DW_FORM_block* DW_AT_location is supported for "
14643 "DW_TAG_call_site child DIE %s "
14644 "[in module %s]"),
14645 sect_offset_str (child_die->sect_off),
14646 objfile_name (objfile));
14647 continue;
14648 }
14649 }
14650
14651 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14652 if (attr == NULL)
14653 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14654 if (!attr_form_is_block (attr))
14655 {
14656 complaint (&symfile_complaints,
14657 _("No DW_FORM_block* DW_AT_call_value for "
14658 "DW_TAG_call_site child DIE %s [in module %s]"),
14659 sect_offset_str (child_die->sect_off),
14660 objfile_name (objfile));
14661 continue;
14662 }
14663 parameter->value = DW_BLOCK (attr)->data;
14664 parameter->value_size = DW_BLOCK (attr)->size;
14665
14666 /* Parameters are not pre-cleared by memset above. */
14667 parameter->data_value = NULL;
14668 parameter->data_value_size = 0;
14669 call_site->parameter_count++;
14670
14671 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14672 if (attr == NULL)
14673 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14674 if (attr)
14675 {
14676 if (!attr_form_is_block (attr))
14677 complaint (&symfile_complaints,
14678 _("No DW_FORM_block* DW_AT_call_data_value for "
14679 "DW_TAG_call_site child DIE %s [in module %s]"),
14680 sect_offset_str (child_die->sect_off),
14681 objfile_name (objfile));
14682 else
14683 {
14684 parameter->data_value = DW_BLOCK (attr)->data;
14685 parameter->data_value_size = DW_BLOCK (attr)->size;
14686 }
14687 }
14688 }
14689 }
14690
14691 /* Helper function for read_variable. If DIE represents a virtual
14692 table, then return the type of the concrete object that is
14693 associated with the virtual table. Otherwise, return NULL. */
14694
14695 static struct type *
14696 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14697 {
14698 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14699 if (attr == NULL)
14700 return NULL;
14701
14702 /* Find the type DIE. */
14703 struct die_info *type_die = NULL;
14704 struct dwarf2_cu *type_cu = cu;
14705
14706 if (attr_form_is_ref (attr))
14707 type_die = follow_die_ref (die, attr, &type_cu);
14708 if (type_die == NULL)
14709 return NULL;
14710
14711 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14712 return NULL;
14713 return die_containing_type (type_die, type_cu);
14714 }
14715
14716 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14717
14718 static void
14719 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14720 {
14721 struct rust_vtable_symbol *storage = NULL;
14722
14723 if (cu->language == language_rust)
14724 {
14725 struct type *containing_type = rust_containing_type (die, cu);
14726
14727 if (containing_type != NULL)
14728 {
14729 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14730
14731 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14732 struct rust_vtable_symbol);
14733 initialize_objfile_symbol (storage);
14734 storage->concrete_type = containing_type;
14735 storage->subclass = SYMBOL_RUST_VTABLE;
14736 }
14737 }
14738
14739 new_symbol (die, NULL, cu, storage);
14740 }
14741
14742 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14743 reading .debug_rnglists.
14744 Callback's type should be:
14745 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14746 Return true if the attributes are present and valid, otherwise,
14747 return false. */
14748
14749 template <typename Callback>
14750 static bool
14751 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14752 Callback &&callback)
14753 {
14754 struct dwarf2_per_objfile *dwarf2_per_objfile
14755 = cu->per_cu->dwarf2_per_objfile;
14756 struct objfile *objfile = dwarf2_per_objfile->objfile;
14757 bfd *obfd = objfile->obfd;
14758 /* Base address selection entry. */
14759 CORE_ADDR base;
14760 int found_base;
14761 const gdb_byte *buffer;
14762 CORE_ADDR baseaddr;
14763 bool overflow = false;
14764
14765 found_base = cu->base_known;
14766 base = cu->base_address;
14767
14768 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14769 if (offset >= dwarf2_per_objfile->rnglists.size)
14770 {
14771 complaint (&symfile_complaints,
14772 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14773 offset);
14774 return false;
14775 }
14776 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14777
14778 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14779
14780 while (1)
14781 {
14782 /* Initialize it due to a false compiler warning. */
14783 CORE_ADDR range_beginning = 0, range_end = 0;
14784 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14785 + dwarf2_per_objfile->rnglists.size);
14786 unsigned int bytes_read;
14787
14788 if (buffer == buf_end)
14789 {
14790 overflow = true;
14791 break;
14792 }
14793 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14794 switch (rlet)
14795 {
14796 case DW_RLE_end_of_list:
14797 break;
14798 case DW_RLE_base_address:
14799 if (buffer + cu->header.addr_size > buf_end)
14800 {
14801 overflow = true;
14802 break;
14803 }
14804 base = read_address (obfd, buffer, cu, &bytes_read);
14805 found_base = 1;
14806 buffer += bytes_read;
14807 break;
14808 case DW_RLE_start_length:
14809 if (buffer + cu->header.addr_size > buf_end)
14810 {
14811 overflow = true;
14812 break;
14813 }
14814 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14815 buffer += bytes_read;
14816 range_end = (range_beginning
14817 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14818 buffer += bytes_read;
14819 if (buffer > buf_end)
14820 {
14821 overflow = true;
14822 break;
14823 }
14824 break;
14825 case DW_RLE_offset_pair:
14826 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14827 buffer += bytes_read;
14828 if (buffer > buf_end)
14829 {
14830 overflow = true;
14831 break;
14832 }
14833 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14834 buffer += bytes_read;
14835 if (buffer > buf_end)
14836 {
14837 overflow = true;
14838 break;
14839 }
14840 break;
14841 case DW_RLE_start_end:
14842 if (buffer + 2 * cu->header.addr_size > buf_end)
14843 {
14844 overflow = true;
14845 break;
14846 }
14847 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14848 buffer += bytes_read;
14849 range_end = read_address (obfd, buffer, cu, &bytes_read);
14850 buffer += bytes_read;
14851 break;
14852 default:
14853 complaint (&symfile_complaints,
14854 _("Invalid .debug_rnglists data (no base address)"));
14855 return false;
14856 }
14857 if (rlet == DW_RLE_end_of_list || overflow)
14858 break;
14859 if (rlet == DW_RLE_base_address)
14860 continue;
14861
14862 if (!found_base)
14863 {
14864 /* We have no valid base address for the ranges
14865 data. */
14866 complaint (&symfile_complaints,
14867 _("Invalid .debug_rnglists data (no base address)"));
14868 return false;
14869 }
14870
14871 if (range_beginning > range_end)
14872 {
14873 /* Inverted range entries are invalid. */
14874 complaint (&symfile_complaints,
14875 _("Invalid .debug_rnglists data (inverted range)"));
14876 return false;
14877 }
14878
14879 /* Empty range entries have no effect. */
14880 if (range_beginning == range_end)
14881 continue;
14882
14883 range_beginning += base;
14884 range_end += base;
14885
14886 /* A not-uncommon case of bad debug info.
14887 Don't pollute the addrmap with bad data. */
14888 if (range_beginning + baseaddr == 0
14889 && !dwarf2_per_objfile->has_section_at_zero)
14890 {
14891 complaint (&symfile_complaints,
14892 _(".debug_rnglists entry has start address of zero"
14893 " [in module %s]"), objfile_name (objfile));
14894 continue;
14895 }
14896
14897 callback (range_beginning, range_end);
14898 }
14899
14900 if (overflow)
14901 {
14902 complaint (&symfile_complaints,
14903 _("Offset %d is not terminated "
14904 "for DW_AT_ranges attribute"),
14905 offset);
14906 return false;
14907 }
14908
14909 return true;
14910 }
14911
14912 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14913 Callback's type should be:
14914 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14915 Return 1 if the attributes are present and valid, otherwise, return 0. */
14916
14917 template <typename Callback>
14918 static int
14919 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14920 Callback &&callback)
14921 {
14922 struct dwarf2_per_objfile *dwarf2_per_objfile
14923 = cu->per_cu->dwarf2_per_objfile;
14924 struct objfile *objfile = dwarf2_per_objfile->objfile;
14925 struct comp_unit_head *cu_header = &cu->header;
14926 bfd *obfd = objfile->obfd;
14927 unsigned int addr_size = cu_header->addr_size;
14928 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14929 /* Base address selection entry. */
14930 CORE_ADDR base;
14931 int found_base;
14932 unsigned int dummy;
14933 const gdb_byte *buffer;
14934 CORE_ADDR baseaddr;
14935
14936 if (cu_header->version >= 5)
14937 return dwarf2_rnglists_process (offset, cu, callback);
14938
14939 found_base = cu->base_known;
14940 base = cu->base_address;
14941
14942 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14943 if (offset >= dwarf2_per_objfile->ranges.size)
14944 {
14945 complaint (&symfile_complaints,
14946 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14947 offset);
14948 return 0;
14949 }
14950 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14951
14952 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14953
14954 while (1)
14955 {
14956 CORE_ADDR range_beginning, range_end;
14957
14958 range_beginning = read_address (obfd, buffer, cu, &dummy);
14959 buffer += addr_size;
14960 range_end = read_address (obfd, buffer, cu, &dummy);
14961 buffer += addr_size;
14962 offset += 2 * addr_size;
14963
14964 /* An end of list marker is a pair of zero addresses. */
14965 if (range_beginning == 0 && range_end == 0)
14966 /* Found the end of list entry. */
14967 break;
14968
14969 /* Each base address selection entry is a pair of 2 values.
14970 The first is the largest possible address, the second is
14971 the base address. Check for a base address here. */
14972 if ((range_beginning & mask) == mask)
14973 {
14974 /* If we found the largest possible address, then we already
14975 have the base address in range_end. */
14976 base = range_end;
14977 found_base = 1;
14978 continue;
14979 }
14980
14981 if (!found_base)
14982 {
14983 /* We have no valid base address for the ranges
14984 data. */
14985 complaint (&symfile_complaints,
14986 _("Invalid .debug_ranges data (no base address)"));
14987 return 0;
14988 }
14989
14990 if (range_beginning > range_end)
14991 {
14992 /* Inverted range entries are invalid. */
14993 complaint (&symfile_complaints,
14994 _("Invalid .debug_ranges data (inverted range)"));
14995 return 0;
14996 }
14997
14998 /* Empty range entries have no effect. */
14999 if (range_beginning == range_end)
15000 continue;
15001
15002 range_beginning += base;
15003 range_end += base;
15004
15005 /* A not-uncommon case of bad debug info.
15006 Don't pollute the addrmap with bad data. */
15007 if (range_beginning + baseaddr == 0
15008 && !dwarf2_per_objfile->has_section_at_zero)
15009 {
15010 complaint (&symfile_complaints,
15011 _(".debug_ranges entry has start address of zero"
15012 " [in module %s]"), objfile_name (objfile));
15013 continue;
15014 }
15015
15016 callback (range_beginning, range_end);
15017 }
15018
15019 return 1;
15020 }
15021
15022 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
15023 Return 1 if the attributes are present and valid, otherwise, return 0.
15024 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
15025
15026 static int
15027 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
15028 CORE_ADDR *high_return, struct dwarf2_cu *cu,
15029 struct partial_symtab *ranges_pst)
15030 {
15031 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15032 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15033 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
15034 SECT_OFF_TEXT (objfile));
15035 int low_set = 0;
15036 CORE_ADDR low = 0;
15037 CORE_ADDR high = 0;
15038 int retval;
15039
15040 retval = dwarf2_ranges_process (offset, cu,
15041 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
15042 {
15043 if (ranges_pst != NULL)
15044 {
15045 CORE_ADDR lowpc;
15046 CORE_ADDR highpc;
15047
15048 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
15049 range_beginning + baseaddr);
15050 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
15051 range_end + baseaddr);
15052 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
15053 ranges_pst);
15054 }
15055
15056 /* FIXME: This is recording everything as a low-high
15057 segment of consecutive addresses. We should have a
15058 data structure for discontiguous block ranges
15059 instead. */
15060 if (! low_set)
15061 {
15062 low = range_beginning;
15063 high = range_end;
15064 low_set = 1;
15065 }
15066 else
15067 {
15068 if (range_beginning < low)
15069 low = range_beginning;
15070 if (range_end > high)
15071 high = range_end;
15072 }
15073 });
15074 if (!retval)
15075 return 0;
15076
15077 if (! low_set)
15078 /* If the first entry is an end-of-list marker, the range
15079 describes an empty scope, i.e. no instructions. */
15080 return 0;
15081
15082 if (low_return)
15083 *low_return = low;
15084 if (high_return)
15085 *high_return = high;
15086 return 1;
15087 }
15088
15089 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
15090 definition for the return value. *LOWPC and *HIGHPC are set iff
15091 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
15092
15093 static enum pc_bounds_kind
15094 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
15095 CORE_ADDR *highpc, struct dwarf2_cu *cu,
15096 struct partial_symtab *pst)
15097 {
15098 struct dwarf2_per_objfile *dwarf2_per_objfile
15099 = cu->per_cu->dwarf2_per_objfile;
15100 struct attribute *attr;
15101 struct attribute *attr_high;
15102 CORE_ADDR low = 0;
15103 CORE_ADDR high = 0;
15104 enum pc_bounds_kind ret;
15105
15106 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
15107 if (attr_high)
15108 {
15109 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15110 if (attr)
15111 {
15112 low = attr_value_as_address (attr);
15113 high = attr_value_as_address (attr_high);
15114 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
15115 high += low;
15116 }
15117 else
15118 /* Found high w/o low attribute. */
15119 return PC_BOUNDS_INVALID;
15120
15121 /* Found consecutive range of addresses. */
15122 ret = PC_BOUNDS_HIGH_LOW;
15123 }
15124 else
15125 {
15126 attr = dwarf2_attr (die, DW_AT_ranges, cu);
15127 if (attr != NULL)
15128 {
15129 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
15130 We take advantage of the fact that DW_AT_ranges does not appear
15131 in DW_TAG_compile_unit of DWO files. */
15132 int need_ranges_base = die->tag != DW_TAG_compile_unit;
15133 unsigned int ranges_offset = (DW_UNSND (attr)
15134 + (need_ranges_base
15135 ? cu->ranges_base
15136 : 0));
15137
15138 /* Value of the DW_AT_ranges attribute is the offset in the
15139 .debug_ranges section. */
15140 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
15141 return PC_BOUNDS_INVALID;
15142 /* Found discontinuous range of addresses. */
15143 ret = PC_BOUNDS_RANGES;
15144 }
15145 else
15146 return PC_BOUNDS_NOT_PRESENT;
15147 }
15148
15149 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
15150 if (high <= low)
15151 return PC_BOUNDS_INVALID;
15152
15153 /* When using the GNU linker, .gnu.linkonce. sections are used to
15154 eliminate duplicate copies of functions and vtables and such.
15155 The linker will arbitrarily choose one and discard the others.
15156 The AT_*_pc values for such functions refer to local labels in
15157 these sections. If the section from that file was discarded, the
15158 labels are not in the output, so the relocs get a value of 0.
15159 If this is a discarded function, mark the pc bounds as invalid,
15160 so that GDB will ignore it. */
15161 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
15162 return PC_BOUNDS_INVALID;
15163
15164 *lowpc = low;
15165 if (highpc)
15166 *highpc = high;
15167 return ret;
15168 }
15169
15170 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
15171 its low and high PC addresses. Do nothing if these addresses could not
15172 be determined. Otherwise, set LOWPC to the low address if it is smaller,
15173 and HIGHPC to the high address if greater than HIGHPC. */
15174
15175 static void
15176 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
15177 CORE_ADDR *lowpc, CORE_ADDR *highpc,
15178 struct dwarf2_cu *cu)
15179 {
15180 CORE_ADDR low, high;
15181 struct die_info *child = die->child;
15182
15183 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
15184 {
15185 *lowpc = std::min (*lowpc, low);
15186 *highpc = std::max (*highpc, high);
15187 }
15188
15189 /* If the language does not allow nested subprograms (either inside
15190 subprograms or lexical blocks), we're done. */
15191 if (cu->language != language_ada)
15192 return;
15193
15194 /* Check all the children of the given DIE. If it contains nested
15195 subprograms, then check their pc bounds. Likewise, we need to
15196 check lexical blocks as well, as they may also contain subprogram
15197 definitions. */
15198 while (child && child->tag)
15199 {
15200 if (child->tag == DW_TAG_subprogram
15201 || child->tag == DW_TAG_lexical_block)
15202 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
15203 child = sibling_die (child);
15204 }
15205 }
15206
15207 /* Get the low and high pc's represented by the scope DIE, and store
15208 them in *LOWPC and *HIGHPC. If the correct values can't be
15209 determined, set *LOWPC to -1 and *HIGHPC to 0. */
15210
15211 static void
15212 get_scope_pc_bounds (struct die_info *die,
15213 CORE_ADDR *lowpc, CORE_ADDR *highpc,
15214 struct dwarf2_cu *cu)
15215 {
15216 CORE_ADDR best_low = (CORE_ADDR) -1;
15217 CORE_ADDR best_high = (CORE_ADDR) 0;
15218 CORE_ADDR current_low, current_high;
15219
15220 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
15221 >= PC_BOUNDS_RANGES)
15222 {
15223 best_low = current_low;
15224 best_high = current_high;
15225 }
15226 else
15227 {
15228 struct die_info *child = die->child;
15229
15230 while (child && child->tag)
15231 {
15232 switch (child->tag) {
15233 case DW_TAG_subprogram:
15234 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
15235 break;
15236 case DW_TAG_namespace:
15237 case DW_TAG_module:
15238 /* FIXME: carlton/2004-01-16: Should we do this for
15239 DW_TAG_class_type/DW_TAG_structure_type, too? I think
15240 that current GCC's always emit the DIEs corresponding
15241 to definitions of methods of classes as children of a
15242 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
15243 the DIEs giving the declarations, which could be
15244 anywhere). But I don't see any reason why the
15245 standards says that they have to be there. */
15246 get_scope_pc_bounds (child, &current_low, &current_high, cu);
15247
15248 if (current_low != ((CORE_ADDR) -1))
15249 {
15250 best_low = std::min (best_low, current_low);
15251 best_high = std::max (best_high, current_high);
15252 }
15253 break;
15254 default:
15255 /* Ignore. */
15256 break;
15257 }
15258
15259 child = sibling_die (child);
15260 }
15261 }
15262
15263 *lowpc = best_low;
15264 *highpc = best_high;
15265 }
15266
15267 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
15268 in DIE. */
15269
15270 static void
15271 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
15272 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
15273 {
15274 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15275 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15276 struct attribute *attr;
15277 struct attribute *attr_high;
15278
15279 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
15280 if (attr_high)
15281 {
15282 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15283 if (attr)
15284 {
15285 CORE_ADDR low = attr_value_as_address (attr);
15286 CORE_ADDR high = attr_value_as_address (attr_high);
15287
15288 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
15289 high += low;
15290
15291 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
15292 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
15293 record_block_range (block, low, high - 1);
15294 }
15295 }
15296
15297 attr = dwarf2_attr (die, DW_AT_ranges, cu);
15298 if (attr)
15299 {
15300 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
15301 We take advantage of the fact that DW_AT_ranges does not appear
15302 in DW_TAG_compile_unit of DWO files. */
15303 int need_ranges_base = die->tag != DW_TAG_compile_unit;
15304
15305 /* The value of the DW_AT_ranges attribute is the offset of the
15306 address range list in the .debug_ranges section. */
15307 unsigned long offset = (DW_UNSND (attr)
15308 + (need_ranges_base ? cu->ranges_base : 0));
15309 const gdb_byte *buffer;
15310
15311 /* For some target architectures, but not others, the
15312 read_address function sign-extends the addresses it returns.
15313 To recognize base address selection entries, we need a
15314 mask. */
15315 unsigned int addr_size = cu->header.addr_size;
15316 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
15317
15318 /* The base address, to which the next pair is relative. Note
15319 that this 'base' is a DWARF concept: most entries in a range
15320 list are relative, to reduce the number of relocs against the
15321 debugging information. This is separate from this function's
15322 'baseaddr' argument, which GDB uses to relocate debugging
15323 information from a shared library based on the address at
15324 which the library was loaded. */
15325 CORE_ADDR base = cu->base_address;
15326 int base_known = cu->base_known;
15327
15328 dwarf2_ranges_process (offset, cu,
15329 [&] (CORE_ADDR start, CORE_ADDR end)
15330 {
15331 start += baseaddr;
15332 end += baseaddr;
15333 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
15334 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
15335 record_block_range (block, start, end - 1);
15336 });
15337 }
15338 }
15339
15340 /* Check whether the producer field indicates either of GCC < 4.6, or the
15341 Intel C/C++ compiler, and cache the result in CU. */
15342
15343 static void
15344 check_producer (struct dwarf2_cu *cu)
15345 {
15346 int major, minor;
15347
15348 if (cu->producer == NULL)
15349 {
15350 /* For unknown compilers expect their behavior is DWARF version
15351 compliant.
15352
15353 GCC started to support .debug_types sections by -gdwarf-4 since
15354 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
15355 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
15356 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
15357 interpreted incorrectly by GDB now - GCC PR debug/48229. */
15358 }
15359 else if (producer_is_gcc (cu->producer, &major, &minor))
15360 {
15361 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
15362 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
15363 }
15364 else if (producer_is_icc (cu->producer, &major, &minor))
15365 cu->producer_is_icc_lt_14 = major < 14;
15366 else
15367 {
15368 /* For other non-GCC compilers, expect their behavior is DWARF version
15369 compliant. */
15370 }
15371
15372 cu->checked_producer = 1;
15373 }
15374
15375 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
15376 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
15377 during 4.6.0 experimental. */
15378
15379 static int
15380 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
15381 {
15382 if (!cu->checked_producer)
15383 check_producer (cu);
15384
15385 return cu->producer_is_gxx_lt_4_6;
15386 }
15387
15388 /* Return the default accessibility type if it is not overriden by
15389 DW_AT_accessibility. */
15390
15391 static enum dwarf_access_attribute
15392 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15393 {
15394 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15395 {
15396 /* The default DWARF 2 accessibility for members is public, the default
15397 accessibility for inheritance is private. */
15398
15399 if (die->tag != DW_TAG_inheritance)
15400 return DW_ACCESS_public;
15401 else
15402 return DW_ACCESS_private;
15403 }
15404 else
15405 {
15406 /* DWARF 3+ defines the default accessibility a different way. The same
15407 rules apply now for DW_TAG_inheritance as for the members and it only
15408 depends on the container kind. */
15409
15410 if (die->parent->tag == DW_TAG_class_type)
15411 return DW_ACCESS_private;
15412 else
15413 return DW_ACCESS_public;
15414 }
15415 }
15416
15417 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15418 offset. If the attribute was not found return 0, otherwise return
15419 1. If it was found but could not properly be handled, set *OFFSET
15420 to 0. */
15421
15422 static int
15423 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15424 LONGEST *offset)
15425 {
15426 struct attribute *attr;
15427
15428 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15429 if (attr != NULL)
15430 {
15431 *offset = 0;
15432
15433 /* Note that we do not check for a section offset first here.
15434 This is because DW_AT_data_member_location is new in DWARF 4,
15435 so if we see it, we can assume that a constant form is really
15436 a constant and not a section offset. */
15437 if (attr_form_is_constant (attr))
15438 *offset = dwarf2_get_attr_constant_value (attr, 0);
15439 else if (attr_form_is_section_offset (attr))
15440 dwarf2_complex_location_expr_complaint ();
15441 else if (attr_form_is_block (attr))
15442 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15443 else
15444 dwarf2_complex_location_expr_complaint ();
15445
15446 return 1;
15447 }
15448
15449 return 0;
15450 }
15451
15452 /* Add an aggregate field to the field list. */
15453
15454 static void
15455 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15456 struct dwarf2_cu *cu)
15457 {
15458 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15459 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15460 struct nextfield *new_field;
15461 struct attribute *attr;
15462 struct field *fp;
15463 const char *fieldname = "";
15464
15465 if (die->tag == DW_TAG_inheritance)
15466 {
15467 fip->baseclasses.emplace_back ();
15468 new_field = &fip->baseclasses.back ();
15469 }
15470 else
15471 {
15472 fip->fields.emplace_back ();
15473 new_field = &fip->fields.back ();
15474 }
15475
15476 fip->nfields++;
15477
15478 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15479 if (attr)
15480 new_field->accessibility = DW_UNSND (attr);
15481 else
15482 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15483 if (new_field->accessibility != DW_ACCESS_public)
15484 fip->non_public_fields = 1;
15485
15486 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15487 if (attr)
15488 new_field->virtuality = DW_UNSND (attr);
15489 else
15490 new_field->virtuality = DW_VIRTUALITY_none;
15491
15492 fp = &new_field->field;
15493
15494 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15495 {
15496 LONGEST offset;
15497
15498 /* Data member other than a C++ static data member. */
15499
15500 /* Get type of field. */
15501 fp->type = die_type (die, cu);
15502
15503 SET_FIELD_BITPOS (*fp, 0);
15504
15505 /* Get bit size of field (zero if none). */
15506 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15507 if (attr)
15508 {
15509 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15510 }
15511 else
15512 {
15513 FIELD_BITSIZE (*fp) = 0;
15514 }
15515
15516 /* Get bit offset of field. */
15517 if (handle_data_member_location (die, cu, &offset))
15518 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15519 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15520 if (attr)
15521 {
15522 if (gdbarch_bits_big_endian (gdbarch))
15523 {
15524 /* For big endian bits, the DW_AT_bit_offset gives the
15525 additional bit offset from the MSB of the containing
15526 anonymous object to the MSB of the field. We don't
15527 have to do anything special since we don't need to
15528 know the size of the anonymous object. */
15529 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15530 }
15531 else
15532 {
15533 /* For little endian bits, compute the bit offset to the
15534 MSB of the anonymous object, subtract off the number of
15535 bits from the MSB of the field to the MSB of the
15536 object, and then subtract off the number of bits of
15537 the field itself. The result is the bit offset of
15538 the LSB of the field. */
15539 int anonymous_size;
15540 int bit_offset = DW_UNSND (attr);
15541
15542 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15543 if (attr)
15544 {
15545 /* The size of the anonymous object containing
15546 the bit field is explicit, so use the
15547 indicated size (in bytes). */
15548 anonymous_size = DW_UNSND (attr);
15549 }
15550 else
15551 {
15552 /* The size of the anonymous object containing
15553 the bit field must be inferred from the type
15554 attribute of the data member containing the
15555 bit field. */
15556 anonymous_size = TYPE_LENGTH (fp->type);
15557 }
15558 SET_FIELD_BITPOS (*fp,
15559 (FIELD_BITPOS (*fp)
15560 + anonymous_size * bits_per_byte
15561 - bit_offset - FIELD_BITSIZE (*fp)));
15562 }
15563 }
15564 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15565 if (attr != NULL)
15566 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15567 + dwarf2_get_attr_constant_value (attr, 0)));
15568
15569 /* Get name of field. */
15570 fieldname = dwarf2_name (die, cu);
15571 if (fieldname == NULL)
15572 fieldname = "";
15573
15574 /* The name is already allocated along with this objfile, so we don't
15575 need to duplicate it for the type. */
15576 fp->name = fieldname;
15577
15578 /* Change accessibility for artificial fields (e.g. virtual table
15579 pointer or virtual base class pointer) to private. */
15580 if (dwarf2_attr (die, DW_AT_artificial, cu))
15581 {
15582 FIELD_ARTIFICIAL (*fp) = 1;
15583 new_field->accessibility = DW_ACCESS_private;
15584 fip->non_public_fields = 1;
15585 }
15586 }
15587 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15588 {
15589 /* C++ static member. */
15590
15591 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15592 is a declaration, but all versions of G++ as of this writing
15593 (so through at least 3.2.1) incorrectly generate
15594 DW_TAG_variable tags. */
15595
15596 const char *physname;
15597
15598 /* Get name of field. */
15599 fieldname = dwarf2_name (die, cu);
15600 if (fieldname == NULL)
15601 return;
15602
15603 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15604 if (attr
15605 /* Only create a symbol if this is an external value.
15606 new_symbol checks this and puts the value in the global symbol
15607 table, which we want. If it is not external, new_symbol
15608 will try to put the value in cu->list_in_scope which is wrong. */
15609 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15610 {
15611 /* A static const member, not much different than an enum as far as
15612 we're concerned, except that we can support more types. */
15613 new_symbol (die, NULL, cu);
15614 }
15615
15616 /* Get physical name. */
15617 physname = dwarf2_physname (fieldname, die, cu);
15618
15619 /* The name is already allocated along with this objfile, so we don't
15620 need to duplicate it for the type. */
15621 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15622 FIELD_TYPE (*fp) = die_type (die, cu);
15623 FIELD_NAME (*fp) = fieldname;
15624 }
15625 else if (die->tag == DW_TAG_inheritance)
15626 {
15627 LONGEST offset;
15628
15629 /* C++ base class field. */
15630 if (handle_data_member_location (die, cu, &offset))
15631 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15632 FIELD_BITSIZE (*fp) = 0;
15633 FIELD_TYPE (*fp) = die_type (die, cu);
15634 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15635 }
15636 else if (die->tag == DW_TAG_variant_part)
15637 {
15638 /* process_structure_scope will treat this DIE as a union. */
15639 process_structure_scope (die, cu);
15640
15641 /* The variant part is relative to the start of the enclosing
15642 structure. */
15643 SET_FIELD_BITPOS (*fp, 0);
15644 fp->type = get_die_type (die, cu);
15645 fp->artificial = 1;
15646 fp->name = "<<variant>>";
15647 }
15648 else
15649 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15650 }
15651
15652 /* Can the type given by DIE define another type? */
15653
15654 static bool
15655 type_can_define_types (const struct die_info *die)
15656 {
15657 switch (die->tag)
15658 {
15659 case DW_TAG_typedef:
15660 case DW_TAG_class_type:
15661 case DW_TAG_structure_type:
15662 case DW_TAG_union_type:
15663 case DW_TAG_enumeration_type:
15664 return true;
15665
15666 default:
15667 return false;
15668 }
15669 }
15670
15671 /* Add a type definition defined in the scope of the FIP's class. */
15672
15673 static void
15674 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15675 struct dwarf2_cu *cu)
15676 {
15677 struct decl_field fp;
15678 memset (&fp, 0, sizeof (fp));
15679
15680 gdb_assert (type_can_define_types (die));
15681
15682 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15683 fp.name = dwarf2_name (die, cu);
15684 fp.type = read_type_die (die, cu);
15685
15686 /* Save accessibility. */
15687 enum dwarf_access_attribute accessibility;
15688 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15689 if (attr != NULL)
15690 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15691 else
15692 accessibility = dwarf2_default_access_attribute (die, cu);
15693 switch (accessibility)
15694 {
15695 case DW_ACCESS_public:
15696 /* The assumed value if neither private nor protected. */
15697 break;
15698 case DW_ACCESS_private:
15699 fp.is_private = 1;
15700 break;
15701 case DW_ACCESS_protected:
15702 fp.is_protected = 1;
15703 break;
15704 default:
15705 complaint (&symfile_complaints,
15706 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15707 }
15708
15709 if (die->tag == DW_TAG_typedef)
15710 fip->typedef_field_list.push_back (fp);
15711 else
15712 fip->nested_types_list.push_back (fp);
15713 }
15714
15715 /* Create the vector of fields, and attach it to the type. */
15716
15717 static void
15718 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15719 struct dwarf2_cu *cu)
15720 {
15721 int nfields = fip->nfields;
15722
15723 /* Record the field count, allocate space for the array of fields,
15724 and create blank accessibility bitfields if necessary. */
15725 TYPE_NFIELDS (type) = nfields;
15726 TYPE_FIELDS (type) = (struct field *)
15727 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15728
15729 if (fip->non_public_fields && cu->language != language_ada)
15730 {
15731 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15732
15733 TYPE_FIELD_PRIVATE_BITS (type) =
15734 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15735 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15736
15737 TYPE_FIELD_PROTECTED_BITS (type) =
15738 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15739 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15740
15741 TYPE_FIELD_IGNORE_BITS (type) =
15742 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15743 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15744 }
15745
15746 /* If the type has baseclasses, allocate and clear a bit vector for
15747 TYPE_FIELD_VIRTUAL_BITS. */
15748 if (!fip->baseclasses.empty () && cu->language != language_ada)
15749 {
15750 int num_bytes = B_BYTES (fip->baseclasses.size ());
15751 unsigned char *pointer;
15752
15753 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15754 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15755 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15756 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15757 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15758 }
15759
15760 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15761 {
15762 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15763
15764 for (int index = 0; index < nfields; ++index)
15765 {
15766 struct nextfield &field = fip->fields[index];
15767
15768 if (field.variant.is_discriminant)
15769 di->discriminant_index = index;
15770 else if (field.variant.default_branch)
15771 di->default_index = index;
15772 else
15773 di->discriminants[index] = field.variant.discriminant_value;
15774 }
15775 }
15776
15777 /* Copy the saved-up fields into the field vector. */
15778 for (int i = 0; i < nfields; ++i)
15779 {
15780 struct nextfield &field
15781 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15782 : fip->fields[i - fip->baseclasses.size ()]);
15783
15784 TYPE_FIELD (type, i) = field.field;
15785 switch (field.accessibility)
15786 {
15787 case DW_ACCESS_private:
15788 if (cu->language != language_ada)
15789 SET_TYPE_FIELD_PRIVATE (type, i);
15790 break;
15791
15792 case DW_ACCESS_protected:
15793 if (cu->language != language_ada)
15794 SET_TYPE_FIELD_PROTECTED (type, i);
15795 break;
15796
15797 case DW_ACCESS_public:
15798 break;
15799
15800 default:
15801 /* Unknown accessibility. Complain and treat it as public. */
15802 {
15803 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15804 field.accessibility);
15805 }
15806 break;
15807 }
15808 if (i < fip->baseclasses.size ())
15809 {
15810 switch (field.virtuality)
15811 {
15812 case DW_VIRTUALITY_virtual:
15813 case DW_VIRTUALITY_pure_virtual:
15814 if (cu->language == language_ada)
15815 error (_("unexpected virtuality in component of Ada type"));
15816 SET_TYPE_FIELD_VIRTUAL (type, i);
15817 break;
15818 }
15819 }
15820 }
15821 }
15822
15823 /* Return true if this member function is a constructor, false
15824 otherwise. */
15825
15826 static int
15827 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15828 {
15829 const char *fieldname;
15830 const char *type_name;
15831 int len;
15832
15833 if (die->parent == NULL)
15834 return 0;
15835
15836 if (die->parent->tag != DW_TAG_structure_type
15837 && die->parent->tag != DW_TAG_union_type
15838 && die->parent->tag != DW_TAG_class_type)
15839 return 0;
15840
15841 fieldname = dwarf2_name (die, cu);
15842 type_name = dwarf2_name (die->parent, cu);
15843 if (fieldname == NULL || type_name == NULL)
15844 return 0;
15845
15846 len = strlen (fieldname);
15847 return (strncmp (fieldname, type_name, len) == 0
15848 && (type_name[len] == '\0' || type_name[len] == '<'));
15849 }
15850
15851 /* Add a member function to the proper fieldlist. */
15852
15853 static void
15854 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15855 struct type *type, struct dwarf2_cu *cu)
15856 {
15857 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15858 struct attribute *attr;
15859 int i;
15860 struct fnfieldlist *flp = nullptr;
15861 struct fn_field *fnp;
15862 const char *fieldname;
15863 struct type *this_type;
15864 enum dwarf_access_attribute accessibility;
15865
15866 if (cu->language == language_ada)
15867 error (_("unexpected member function in Ada type"));
15868
15869 /* Get name of member function. */
15870 fieldname = dwarf2_name (die, cu);
15871 if (fieldname == NULL)
15872 return;
15873
15874 /* Look up member function name in fieldlist. */
15875 for (i = 0; i < fip->fnfieldlists.size (); i++)
15876 {
15877 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15878 {
15879 flp = &fip->fnfieldlists[i];
15880 break;
15881 }
15882 }
15883
15884 /* Create a new fnfieldlist if necessary. */
15885 if (flp == nullptr)
15886 {
15887 fip->fnfieldlists.emplace_back ();
15888 flp = &fip->fnfieldlists.back ();
15889 flp->name = fieldname;
15890 i = fip->fnfieldlists.size () - 1;
15891 }
15892
15893 /* Create a new member function field and add it to the vector of
15894 fnfieldlists. */
15895 flp->fnfields.emplace_back ();
15896 fnp = &flp->fnfields.back ();
15897
15898 /* Delay processing of the physname until later. */
15899 if (cu->language == language_cplus)
15900 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15901 die, cu);
15902 else
15903 {
15904 const char *physname = dwarf2_physname (fieldname, die, cu);
15905 fnp->physname = physname ? physname : "";
15906 }
15907
15908 fnp->type = alloc_type (objfile);
15909 this_type = read_type_die (die, cu);
15910 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15911 {
15912 int nparams = TYPE_NFIELDS (this_type);
15913
15914 /* TYPE is the domain of this method, and THIS_TYPE is the type
15915 of the method itself (TYPE_CODE_METHOD). */
15916 smash_to_method_type (fnp->type, type,
15917 TYPE_TARGET_TYPE (this_type),
15918 TYPE_FIELDS (this_type),
15919 TYPE_NFIELDS (this_type),
15920 TYPE_VARARGS (this_type));
15921
15922 /* Handle static member functions.
15923 Dwarf2 has no clean way to discern C++ static and non-static
15924 member functions. G++ helps GDB by marking the first
15925 parameter for non-static member functions (which is the this
15926 pointer) as artificial. We obtain this information from
15927 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15928 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15929 fnp->voffset = VOFFSET_STATIC;
15930 }
15931 else
15932 complaint (&symfile_complaints, _("member function type missing for '%s'"),
15933 dwarf2_full_name (fieldname, die, cu));
15934
15935 /* Get fcontext from DW_AT_containing_type if present. */
15936 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15937 fnp->fcontext = die_containing_type (die, cu);
15938
15939 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15940 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15941
15942 /* Get accessibility. */
15943 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15944 if (attr)
15945 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15946 else
15947 accessibility = dwarf2_default_access_attribute (die, cu);
15948 switch (accessibility)
15949 {
15950 case DW_ACCESS_private:
15951 fnp->is_private = 1;
15952 break;
15953 case DW_ACCESS_protected:
15954 fnp->is_protected = 1;
15955 break;
15956 }
15957
15958 /* Check for artificial methods. */
15959 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15960 if (attr && DW_UNSND (attr) != 0)
15961 fnp->is_artificial = 1;
15962
15963 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15964
15965 /* Get index in virtual function table if it is a virtual member
15966 function. For older versions of GCC, this is an offset in the
15967 appropriate virtual table, as specified by DW_AT_containing_type.
15968 For everyone else, it is an expression to be evaluated relative
15969 to the object address. */
15970
15971 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15972 if (attr)
15973 {
15974 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15975 {
15976 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15977 {
15978 /* Old-style GCC. */
15979 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15980 }
15981 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15982 || (DW_BLOCK (attr)->size > 1
15983 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15984 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15985 {
15986 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15987 if ((fnp->voffset % cu->header.addr_size) != 0)
15988 dwarf2_complex_location_expr_complaint ();
15989 else
15990 fnp->voffset /= cu->header.addr_size;
15991 fnp->voffset += 2;
15992 }
15993 else
15994 dwarf2_complex_location_expr_complaint ();
15995
15996 if (!fnp->fcontext)
15997 {
15998 /* If there is no `this' field and no DW_AT_containing_type,
15999 we cannot actually find a base class context for the
16000 vtable! */
16001 if (TYPE_NFIELDS (this_type) == 0
16002 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
16003 {
16004 complaint (&symfile_complaints,
16005 _("cannot determine context for virtual member "
16006 "function \"%s\" (offset %s)"),
16007 fieldname, sect_offset_str (die->sect_off));
16008 }
16009 else
16010 {
16011 fnp->fcontext
16012 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
16013 }
16014 }
16015 }
16016 else if (attr_form_is_section_offset (attr))
16017 {
16018 dwarf2_complex_location_expr_complaint ();
16019 }
16020 else
16021 {
16022 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
16023 fieldname);
16024 }
16025 }
16026 else
16027 {
16028 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
16029 if (attr && DW_UNSND (attr))
16030 {
16031 /* GCC does this, as of 2008-08-25; PR debug/37237. */
16032 complaint (&symfile_complaints,
16033 _("Member function \"%s\" (offset %s) is virtual "
16034 "but the vtable offset is not specified"),
16035 fieldname, sect_offset_str (die->sect_off));
16036 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16037 TYPE_CPLUS_DYNAMIC (type) = 1;
16038 }
16039 }
16040 }
16041
16042 /* Create the vector of member function fields, and attach it to the type. */
16043
16044 static void
16045 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
16046 struct dwarf2_cu *cu)
16047 {
16048 if (cu->language == language_ada)
16049 error (_("unexpected member functions in Ada type"));
16050
16051 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16052 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
16053 TYPE_ALLOC (type,
16054 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
16055
16056 for (int i = 0; i < fip->fnfieldlists.size (); i++)
16057 {
16058 struct fnfieldlist &nf = fip->fnfieldlists[i];
16059 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
16060
16061 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
16062 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
16063 fn_flp->fn_fields = (struct fn_field *)
16064 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
16065
16066 for (int k = 0; k < nf.fnfields.size (); ++k)
16067 fn_flp->fn_fields[k] = nf.fnfields[k];
16068 }
16069
16070 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
16071 }
16072
16073 /* Returns non-zero if NAME is the name of a vtable member in CU's
16074 language, zero otherwise. */
16075 static int
16076 is_vtable_name (const char *name, struct dwarf2_cu *cu)
16077 {
16078 static const char vptr[] = "_vptr";
16079
16080 /* Look for the C++ form of the vtable. */
16081 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
16082 return 1;
16083
16084 return 0;
16085 }
16086
16087 /* GCC outputs unnamed structures that are really pointers to member
16088 functions, with the ABI-specified layout. If TYPE describes
16089 such a structure, smash it into a member function type.
16090
16091 GCC shouldn't do this; it should just output pointer to member DIEs.
16092 This is GCC PR debug/28767. */
16093
16094 static void
16095 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
16096 {
16097 struct type *pfn_type, *self_type, *new_type;
16098
16099 /* Check for a structure with no name and two children. */
16100 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
16101 return;
16102
16103 /* Check for __pfn and __delta members. */
16104 if (TYPE_FIELD_NAME (type, 0) == NULL
16105 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
16106 || TYPE_FIELD_NAME (type, 1) == NULL
16107 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
16108 return;
16109
16110 /* Find the type of the method. */
16111 pfn_type = TYPE_FIELD_TYPE (type, 0);
16112 if (pfn_type == NULL
16113 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
16114 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
16115 return;
16116
16117 /* Look for the "this" argument. */
16118 pfn_type = TYPE_TARGET_TYPE (pfn_type);
16119 if (TYPE_NFIELDS (pfn_type) == 0
16120 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
16121 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
16122 return;
16123
16124 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
16125 new_type = alloc_type (objfile);
16126 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
16127 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
16128 TYPE_VARARGS (pfn_type));
16129 smash_to_methodptr_type (type, new_type);
16130 }
16131
16132
16133 /* Called when we find the DIE that starts a structure or union scope
16134 (definition) to create a type for the structure or union. Fill in
16135 the type's name and general properties; the members will not be
16136 processed until process_structure_scope. A symbol table entry for
16137 the type will also not be done until process_structure_scope (assuming
16138 the type has a name).
16139
16140 NOTE: we need to call these functions regardless of whether or not the
16141 DIE has a DW_AT_name attribute, since it might be an anonymous
16142 structure or union. This gets the type entered into our set of
16143 user defined types. */
16144
16145 static struct type *
16146 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
16147 {
16148 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16149 struct type *type;
16150 struct attribute *attr;
16151 const char *name;
16152
16153 /* If the definition of this type lives in .debug_types, read that type.
16154 Don't follow DW_AT_specification though, that will take us back up
16155 the chain and we want to go down. */
16156 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16157 if (attr)
16158 {
16159 type = get_DW_AT_signature_type (die, attr, cu);
16160
16161 /* The type's CU may not be the same as CU.
16162 Ensure TYPE is recorded with CU in die_type_hash. */
16163 return set_die_type (die, type, cu);
16164 }
16165
16166 type = alloc_type (objfile);
16167 INIT_CPLUS_SPECIFIC (type);
16168
16169 name = dwarf2_name (die, cu);
16170 if (name != NULL)
16171 {
16172 if (cu->language == language_cplus
16173 || cu->language == language_d
16174 || cu->language == language_rust)
16175 {
16176 const char *full_name = dwarf2_full_name (name, die, cu);
16177
16178 /* dwarf2_full_name might have already finished building the DIE's
16179 type. If so, there is no need to continue. */
16180 if (get_die_type (die, cu) != NULL)
16181 return get_die_type (die, cu);
16182
16183 TYPE_TAG_NAME (type) = full_name;
16184 if (die->tag == DW_TAG_structure_type
16185 || die->tag == DW_TAG_class_type)
16186 TYPE_NAME (type) = TYPE_TAG_NAME (type);
16187 }
16188 else
16189 {
16190 /* The name is already allocated along with this objfile, so
16191 we don't need to duplicate it for the type. */
16192 TYPE_TAG_NAME (type) = name;
16193 if (die->tag == DW_TAG_class_type)
16194 TYPE_NAME (type) = TYPE_TAG_NAME (type);
16195 }
16196 }
16197
16198 if (die->tag == DW_TAG_structure_type)
16199 {
16200 TYPE_CODE (type) = TYPE_CODE_STRUCT;
16201 }
16202 else if (die->tag == DW_TAG_union_type)
16203 {
16204 TYPE_CODE (type) = TYPE_CODE_UNION;
16205 }
16206 else if (die->tag == DW_TAG_variant_part)
16207 {
16208 TYPE_CODE (type) = TYPE_CODE_UNION;
16209 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
16210 }
16211 else
16212 {
16213 TYPE_CODE (type) = TYPE_CODE_STRUCT;
16214 }
16215
16216 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16217 TYPE_DECLARED_CLASS (type) = 1;
16218
16219 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16220 if (attr)
16221 {
16222 if (attr_form_is_constant (attr))
16223 TYPE_LENGTH (type) = DW_UNSND (attr);
16224 else
16225 {
16226 /* For the moment, dynamic type sizes are not supported
16227 by GDB's struct type. The actual size is determined
16228 on-demand when resolving the type of a given object,
16229 so set the type's length to zero for now. Otherwise,
16230 we record an expression as the length, and that expression
16231 could lead to a very large value, which could eventually
16232 lead to us trying to allocate that much memory when creating
16233 a value of that type. */
16234 TYPE_LENGTH (type) = 0;
16235 }
16236 }
16237 else
16238 {
16239 TYPE_LENGTH (type) = 0;
16240 }
16241
16242 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16243 {
16244 /* ICC<14 does not output the required DW_AT_declaration on
16245 incomplete types, but gives them a size of zero. */
16246 TYPE_STUB (type) = 1;
16247 }
16248 else
16249 TYPE_STUB_SUPPORTED (type) = 1;
16250
16251 if (die_is_declaration (die, cu))
16252 TYPE_STUB (type) = 1;
16253 else if (attr == NULL && die->child == NULL
16254 && producer_is_realview (cu->producer))
16255 /* RealView does not output the required DW_AT_declaration
16256 on incomplete types. */
16257 TYPE_STUB (type) = 1;
16258
16259 /* We need to add the type field to the die immediately so we don't
16260 infinitely recurse when dealing with pointers to the structure
16261 type within the structure itself. */
16262 set_die_type (die, type, cu);
16263
16264 /* set_die_type should be already done. */
16265 set_descriptive_type (type, die, cu);
16266
16267 return type;
16268 }
16269
16270 /* A helper for process_structure_scope that handles a single member
16271 DIE. */
16272
16273 static void
16274 handle_struct_member_die (struct die_info *child_die, struct type *type,
16275 struct field_info *fi,
16276 std::vector<struct symbol *> *template_args,
16277 struct dwarf2_cu *cu)
16278 {
16279 if (child_die->tag == DW_TAG_member
16280 || child_die->tag == DW_TAG_variable
16281 || child_die->tag == DW_TAG_variant_part)
16282 {
16283 /* NOTE: carlton/2002-11-05: A C++ static data member
16284 should be a DW_TAG_member that is a declaration, but
16285 all versions of G++ as of this writing (so through at
16286 least 3.2.1) incorrectly generate DW_TAG_variable
16287 tags for them instead. */
16288 dwarf2_add_field (fi, child_die, cu);
16289 }
16290 else if (child_die->tag == DW_TAG_subprogram)
16291 {
16292 /* Rust doesn't have member functions in the C++ sense.
16293 However, it does emit ordinary functions as children
16294 of a struct DIE. */
16295 if (cu->language == language_rust)
16296 read_func_scope (child_die, cu);
16297 else
16298 {
16299 /* C++ member function. */
16300 dwarf2_add_member_fn (fi, child_die, type, cu);
16301 }
16302 }
16303 else if (child_die->tag == DW_TAG_inheritance)
16304 {
16305 /* C++ base class field. */
16306 dwarf2_add_field (fi, child_die, cu);
16307 }
16308 else if (type_can_define_types (child_die))
16309 dwarf2_add_type_defn (fi, child_die, cu);
16310 else if (child_die->tag == DW_TAG_template_type_param
16311 || child_die->tag == DW_TAG_template_value_param)
16312 {
16313 struct symbol *arg = new_symbol (child_die, NULL, cu);
16314
16315 if (arg != NULL)
16316 template_args->push_back (arg);
16317 }
16318 else if (child_die->tag == DW_TAG_variant)
16319 {
16320 /* In a variant we want to get the discriminant and also add a
16321 field for our sole member child. */
16322 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16323
16324 for (struct die_info *variant_child = child_die->child;
16325 variant_child != NULL;
16326 variant_child = sibling_die (variant_child))
16327 {
16328 if (variant_child->tag == DW_TAG_member)
16329 {
16330 handle_struct_member_die (variant_child, type, fi,
16331 template_args, cu);
16332 /* Only handle the one. */
16333 break;
16334 }
16335 }
16336
16337 /* We don't handle this but we might as well report it if we see
16338 it. */
16339 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
16340 complaint (&symfile_complaints,
16341 _("DW_AT_discr_list is not supported yet"
16342 " - DIE at %s [in module %s]"),
16343 sect_offset_str (child_die->sect_off),
16344 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16345
16346 /* The first field was just added, so we can stash the
16347 discriminant there. */
16348 gdb_assert (!fi->fields.empty ());
16349 if (discr == NULL)
16350 fi->fields.back ().variant.default_branch = true;
16351 else
16352 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
16353 }
16354 }
16355
16356 /* Finish creating a structure or union type, including filling in
16357 its members and creating a symbol for it. */
16358
16359 static void
16360 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16361 {
16362 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16363 struct die_info *child_die;
16364 struct type *type;
16365
16366 type = get_die_type (die, cu);
16367 if (type == NULL)
16368 type = read_structure_type (die, cu);
16369
16370 /* When reading a DW_TAG_variant_part, we need to notice when we
16371 read the discriminant member, so we can record it later in the
16372 discriminant_info. */
16373 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16374 sect_offset discr_offset;
16375
16376 if (is_variant_part)
16377 {
16378 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16379 if (discr == NULL)
16380 {
16381 /* Maybe it's a univariant form, an extension we support.
16382 In this case arrange not to check the offset. */
16383 is_variant_part = false;
16384 }
16385 else if (attr_form_is_ref (discr))
16386 {
16387 struct dwarf2_cu *target_cu = cu;
16388 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16389
16390 discr_offset = target_die->sect_off;
16391 }
16392 else
16393 {
16394 complaint (&symfile_complaints,
16395 _("DW_AT_discr does not have DIE reference form"
16396 " - DIE at %s [in module %s]"),
16397 sect_offset_str (die->sect_off),
16398 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16399 is_variant_part = false;
16400 }
16401 }
16402
16403 if (die->child != NULL && ! die_is_declaration (die, cu))
16404 {
16405 struct field_info fi;
16406 std::vector<struct symbol *> template_args;
16407
16408 child_die = die->child;
16409
16410 while (child_die && child_die->tag)
16411 {
16412 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16413
16414 if (is_variant_part && discr_offset == child_die->sect_off)
16415 fi.fields.back ().variant.is_discriminant = true;
16416
16417 child_die = sibling_die (child_die);
16418 }
16419
16420 /* Attach template arguments to type. */
16421 if (!template_args.empty ())
16422 {
16423 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16424 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16425 TYPE_TEMPLATE_ARGUMENTS (type)
16426 = XOBNEWVEC (&objfile->objfile_obstack,
16427 struct symbol *,
16428 TYPE_N_TEMPLATE_ARGUMENTS (type));
16429 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16430 template_args.data (),
16431 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16432 * sizeof (struct symbol *)));
16433 }
16434
16435 /* Attach fields and member functions to the type. */
16436 if (fi.nfields)
16437 dwarf2_attach_fields_to_type (&fi, type, cu);
16438 if (!fi.fnfieldlists.empty ())
16439 {
16440 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16441
16442 /* Get the type which refers to the base class (possibly this
16443 class itself) which contains the vtable pointer for the current
16444 class from the DW_AT_containing_type attribute. This use of
16445 DW_AT_containing_type is a GNU extension. */
16446
16447 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16448 {
16449 struct type *t = die_containing_type (die, cu);
16450
16451 set_type_vptr_basetype (type, t);
16452 if (type == t)
16453 {
16454 int i;
16455
16456 /* Our own class provides vtbl ptr. */
16457 for (i = TYPE_NFIELDS (t) - 1;
16458 i >= TYPE_N_BASECLASSES (t);
16459 --i)
16460 {
16461 const char *fieldname = TYPE_FIELD_NAME (t, i);
16462
16463 if (is_vtable_name (fieldname, cu))
16464 {
16465 set_type_vptr_fieldno (type, i);
16466 break;
16467 }
16468 }
16469
16470 /* Complain if virtual function table field not found. */
16471 if (i < TYPE_N_BASECLASSES (t))
16472 complaint (&symfile_complaints,
16473 _("virtual function table pointer "
16474 "not found when defining class '%s'"),
16475 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16476 "");
16477 }
16478 else
16479 {
16480 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16481 }
16482 }
16483 else if (cu->producer
16484 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16485 {
16486 /* The IBM XLC compiler does not provide direct indication
16487 of the containing type, but the vtable pointer is
16488 always named __vfp. */
16489
16490 int i;
16491
16492 for (i = TYPE_NFIELDS (type) - 1;
16493 i >= TYPE_N_BASECLASSES (type);
16494 --i)
16495 {
16496 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16497 {
16498 set_type_vptr_fieldno (type, i);
16499 set_type_vptr_basetype (type, type);
16500 break;
16501 }
16502 }
16503 }
16504 }
16505
16506 /* Copy fi.typedef_field_list linked list elements content into the
16507 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16508 if (!fi.typedef_field_list.empty ())
16509 {
16510 int count = fi.typedef_field_list.size ();
16511
16512 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16513 TYPE_TYPEDEF_FIELD_ARRAY (type)
16514 = ((struct decl_field *)
16515 TYPE_ALLOC (type,
16516 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16517 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16518
16519 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16520 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16521 }
16522
16523 /* Copy fi.nested_types_list linked list elements content into the
16524 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16525 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16526 {
16527 int count = fi.nested_types_list.size ();
16528
16529 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16530 TYPE_NESTED_TYPES_ARRAY (type)
16531 = ((struct decl_field *)
16532 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16533 TYPE_NESTED_TYPES_COUNT (type) = count;
16534
16535 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16536 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16537 }
16538 }
16539
16540 quirk_gcc_member_function_pointer (type, objfile);
16541 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16542 cu->rust_unions.push_back (type);
16543
16544 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16545 snapshots) has been known to create a die giving a declaration
16546 for a class that has, as a child, a die giving a definition for a
16547 nested class. So we have to process our children even if the
16548 current die is a declaration. Normally, of course, a declaration
16549 won't have any children at all. */
16550
16551 child_die = die->child;
16552
16553 while (child_die != NULL && child_die->tag)
16554 {
16555 if (child_die->tag == DW_TAG_member
16556 || child_die->tag == DW_TAG_variable
16557 || child_die->tag == DW_TAG_inheritance
16558 || child_die->tag == DW_TAG_template_value_param
16559 || child_die->tag == DW_TAG_template_type_param)
16560 {
16561 /* Do nothing. */
16562 }
16563 else
16564 process_die (child_die, cu);
16565
16566 child_die = sibling_die (child_die);
16567 }
16568
16569 /* Do not consider external references. According to the DWARF standard,
16570 these DIEs are identified by the fact that they have no byte_size
16571 attribute, and a declaration attribute. */
16572 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16573 || !die_is_declaration (die, cu))
16574 new_symbol (die, type, cu);
16575 }
16576
16577 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16578 update TYPE using some information only available in DIE's children. */
16579
16580 static void
16581 update_enumeration_type_from_children (struct die_info *die,
16582 struct type *type,
16583 struct dwarf2_cu *cu)
16584 {
16585 struct die_info *child_die;
16586 int unsigned_enum = 1;
16587 int flag_enum = 1;
16588 ULONGEST mask = 0;
16589
16590 auto_obstack obstack;
16591
16592 for (child_die = die->child;
16593 child_die != NULL && child_die->tag;
16594 child_die = sibling_die (child_die))
16595 {
16596 struct attribute *attr;
16597 LONGEST value;
16598 const gdb_byte *bytes;
16599 struct dwarf2_locexpr_baton *baton;
16600 const char *name;
16601
16602 if (child_die->tag != DW_TAG_enumerator)
16603 continue;
16604
16605 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16606 if (attr == NULL)
16607 continue;
16608
16609 name = dwarf2_name (child_die, cu);
16610 if (name == NULL)
16611 name = "<anonymous enumerator>";
16612
16613 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16614 &value, &bytes, &baton);
16615 if (value < 0)
16616 {
16617 unsigned_enum = 0;
16618 flag_enum = 0;
16619 }
16620 else if ((mask & value) != 0)
16621 flag_enum = 0;
16622 else
16623 mask |= value;
16624
16625 /* If we already know that the enum type is neither unsigned, nor
16626 a flag type, no need to look at the rest of the enumerates. */
16627 if (!unsigned_enum && !flag_enum)
16628 break;
16629 }
16630
16631 if (unsigned_enum)
16632 TYPE_UNSIGNED (type) = 1;
16633 if (flag_enum)
16634 TYPE_FLAG_ENUM (type) = 1;
16635 }
16636
16637 /* Given a DW_AT_enumeration_type die, set its type. We do not
16638 complete the type's fields yet, or create any symbols. */
16639
16640 static struct type *
16641 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16642 {
16643 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16644 struct type *type;
16645 struct attribute *attr;
16646 const char *name;
16647
16648 /* If the definition of this type lives in .debug_types, read that type.
16649 Don't follow DW_AT_specification though, that will take us back up
16650 the chain and we want to go down. */
16651 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16652 if (attr)
16653 {
16654 type = get_DW_AT_signature_type (die, attr, cu);
16655
16656 /* The type's CU may not be the same as CU.
16657 Ensure TYPE is recorded with CU in die_type_hash. */
16658 return set_die_type (die, type, cu);
16659 }
16660
16661 type = alloc_type (objfile);
16662
16663 TYPE_CODE (type) = TYPE_CODE_ENUM;
16664 name = dwarf2_full_name (NULL, die, cu);
16665 if (name != NULL)
16666 TYPE_TAG_NAME (type) = name;
16667
16668 attr = dwarf2_attr (die, DW_AT_type, cu);
16669 if (attr != NULL)
16670 {
16671 struct type *underlying_type = die_type (die, cu);
16672
16673 TYPE_TARGET_TYPE (type) = underlying_type;
16674 }
16675
16676 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16677 if (attr)
16678 {
16679 TYPE_LENGTH (type) = DW_UNSND (attr);
16680 }
16681 else
16682 {
16683 TYPE_LENGTH (type) = 0;
16684 }
16685
16686 /* The enumeration DIE can be incomplete. In Ada, any type can be
16687 declared as private in the package spec, and then defined only
16688 inside the package body. Such types are known as Taft Amendment
16689 Types. When another package uses such a type, an incomplete DIE
16690 may be generated by the compiler. */
16691 if (die_is_declaration (die, cu))
16692 TYPE_STUB (type) = 1;
16693
16694 /* Finish the creation of this type by using the enum's children.
16695 We must call this even when the underlying type has been provided
16696 so that we can determine if we're looking at a "flag" enum. */
16697 update_enumeration_type_from_children (die, type, cu);
16698
16699 /* If this type has an underlying type that is not a stub, then we
16700 may use its attributes. We always use the "unsigned" attribute
16701 in this situation, because ordinarily we guess whether the type
16702 is unsigned -- but the guess can be wrong and the underlying type
16703 can tell us the reality. However, we defer to a local size
16704 attribute if one exists, because this lets the compiler override
16705 the underlying type if needed. */
16706 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16707 {
16708 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16709 if (TYPE_LENGTH (type) == 0)
16710 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16711 }
16712
16713 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16714
16715 return set_die_type (die, type, cu);
16716 }
16717
16718 /* Given a pointer to a die which begins an enumeration, process all
16719 the dies that define the members of the enumeration, and create the
16720 symbol for the enumeration type.
16721
16722 NOTE: We reverse the order of the element list. */
16723
16724 static void
16725 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16726 {
16727 struct type *this_type;
16728
16729 this_type = get_die_type (die, cu);
16730 if (this_type == NULL)
16731 this_type = read_enumeration_type (die, cu);
16732
16733 if (die->child != NULL)
16734 {
16735 struct die_info *child_die;
16736 struct symbol *sym;
16737 struct field *fields = NULL;
16738 int num_fields = 0;
16739 const char *name;
16740
16741 child_die = die->child;
16742 while (child_die && child_die->tag)
16743 {
16744 if (child_die->tag != DW_TAG_enumerator)
16745 {
16746 process_die (child_die, cu);
16747 }
16748 else
16749 {
16750 name = dwarf2_name (child_die, cu);
16751 if (name)
16752 {
16753 sym = new_symbol (child_die, this_type, cu);
16754
16755 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16756 {
16757 fields = (struct field *)
16758 xrealloc (fields,
16759 (num_fields + DW_FIELD_ALLOC_CHUNK)
16760 * sizeof (struct field));
16761 }
16762
16763 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16764 FIELD_TYPE (fields[num_fields]) = NULL;
16765 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16766 FIELD_BITSIZE (fields[num_fields]) = 0;
16767
16768 num_fields++;
16769 }
16770 }
16771
16772 child_die = sibling_die (child_die);
16773 }
16774
16775 if (num_fields)
16776 {
16777 TYPE_NFIELDS (this_type) = num_fields;
16778 TYPE_FIELDS (this_type) = (struct field *)
16779 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16780 memcpy (TYPE_FIELDS (this_type), fields,
16781 sizeof (struct field) * num_fields);
16782 xfree (fields);
16783 }
16784 }
16785
16786 /* If we are reading an enum from a .debug_types unit, and the enum
16787 is a declaration, and the enum is not the signatured type in the
16788 unit, then we do not want to add a symbol for it. Adding a
16789 symbol would in some cases obscure the true definition of the
16790 enum, giving users an incomplete type when the definition is
16791 actually available. Note that we do not want to do this for all
16792 enums which are just declarations, because C++0x allows forward
16793 enum declarations. */
16794 if (cu->per_cu->is_debug_types
16795 && die_is_declaration (die, cu))
16796 {
16797 struct signatured_type *sig_type;
16798
16799 sig_type = (struct signatured_type *) cu->per_cu;
16800 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16801 if (sig_type->type_offset_in_section != die->sect_off)
16802 return;
16803 }
16804
16805 new_symbol (die, this_type, cu);
16806 }
16807
16808 /* Extract all information from a DW_TAG_array_type DIE and put it in
16809 the DIE's type field. For now, this only handles one dimensional
16810 arrays. */
16811
16812 static struct type *
16813 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16814 {
16815 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16816 struct die_info *child_die;
16817 struct type *type;
16818 struct type *element_type, *range_type, *index_type;
16819 struct attribute *attr;
16820 const char *name;
16821 struct dynamic_prop *byte_stride_prop = NULL;
16822 unsigned int bit_stride = 0;
16823
16824 element_type = die_type (die, cu);
16825
16826 /* The die_type call above may have already set the type for this DIE. */
16827 type = get_die_type (die, cu);
16828 if (type)
16829 return type;
16830
16831 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16832 if (attr != NULL)
16833 {
16834 int stride_ok;
16835
16836 byte_stride_prop
16837 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16838 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16839 if (!stride_ok)
16840 {
16841 complaint (&symfile_complaints,
16842 _("unable to read array DW_AT_byte_stride "
16843 " - DIE at %s [in module %s]"),
16844 sect_offset_str (die->sect_off),
16845 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16846 /* Ignore this attribute. We will likely not be able to print
16847 arrays of this type correctly, but there is little we can do
16848 to help if we cannot read the attribute's value. */
16849 byte_stride_prop = NULL;
16850 }
16851 }
16852
16853 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16854 if (attr != NULL)
16855 bit_stride = DW_UNSND (attr);
16856
16857 /* Irix 6.2 native cc creates array types without children for
16858 arrays with unspecified length. */
16859 if (die->child == NULL)
16860 {
16861 index_type = objfile_type (objfile)->builtin_int;
16862 range_type = create_static_range_type (NULL, index_type, 0, -1);
16863 type = create_array_type_with_stride (NULL, element_type, range_type,
16864 byte_stride_prop, bit_stride);
16865 return set_die_type (die, type, cu);
16866 }
16867
16868 std::vector<struct type *> range_types;
16869 child_die = die->child;
16870 while (child_die && child_die->tag)
16871 {
16872 if (child_die->tag == DW_TAG_subrange_type)
16873 {
16874 struct type *child_type = read_type_die (child_die, cu);
16875
16876 if (child_type != NULL)
16877 {
16878 /* The range type was succesfully read. Save it for the
16879 array type creation. */
16880 range_types.push_back (child_type);
16881 }
16882 }
16883 child_die = sibling_die (child_die);
16884 }
16885
16886 /* Dwarf2 dimensions are output from left to right, create the
16887 necessary array types in backwards order. */
16888
16889 type = element_type;
16890
16891 if (read_array_order (die, cu) == DW_ORD_col_major)
16892 {
16893 int i = 0;
16894
16895 while (i < range_types.size ())
16896 type = create_array_type_with_stride (NULL, type, range_types[i++],
16897 byte_stride_prop, bit_stride);
16898 }
16899 else
16900 {
16901 size_t ndim = range_types.size ();
16902 while (ndim-- > 0)
16903 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16904 byte_stride_prop, bit_stride);
16905 }
16906
16907 /* Understand Dwarf2 support for vector types (like they occur on
16908 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16909 array type. This is not part of the Dwarf2/3 standard yet, but a
16910 custom vendor extension. The main difference between a regular
16911 array and the vector variant is that vectors are passed by value
16912 to functions. */
16913 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16914 if (attr)
16915 make_vector_type (type);
16916
16917 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16918 implementation may choose to implement triple vectors using this
16919 attribute. */
16920 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16921 if (attr)
16922 {
16923 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16924 TYPE_LENGTH (type) = DW_UNSND (attr);
16925 else
16926 complaint (&symfile_complaints,
16927 _("DW_AT_byte_size for array type smaller "
16928 "than the total size of elements"));
16929 }
16930
16931 name = dwarf2_name (die, cu);
16932 if (name)
16933 TYPE_NAME (type) = name;
16934
16935 /* Install the type in the die. */
16936 set_die_type (die, type, cu);
16937
16938 /* set_die_type should be already done. */
16939 set_descriptive_type (type, die, cu);
16940
16941 return type;
16942 }
16943
16944 static enum dwarf_array_dim_ordering
16945 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16946 {
16947 struct attribute *attr;
16948
16949 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16950
16951 if (attr)
16952 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16953
16954 /* GNU F77 is a special case, as at 08/2004 array type info is the
16955 opposite order to the dwarf2 specification, but data is still
16956 laid out as per normal fortran.
16957
16958 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16959 version checking. */
16960
16961 if (cu->language == language_fortran
16962 && cu->producer && strstr (cu->producer, "GNU F77"))
16963 {
16964 return DW_ORD_row_major;
16965 }
16966
16967 switch (cu->language_defn->la_array_ordering)
16968 {
16969 case array_column_major:
16970 return DW_ORD_col_major;
16971 case array_row_major:
16972 default:
16973 return DW_ORD_row_major;
16974 };
16975 }
16976
16977 /* Extract all information from a DW_TAG_set_type DIE and put it in
16978 the DIE's type field. */
16979
16980 static struct type *
16981 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16982 {
16983 struct type *domain_type, *set_type;
16984 struct attribute *attr;
16985
16986 domain_type = die_type (die, cu);
16987
16988 /* The die_type call above may have already set the type for this DIE. */
16989 set_type = get_die_type (die, cu);
16990 if (set_type)
16991 return set_type;
16992
16993 set_type = create_set_type (NULL, domain_type);
16994
16995 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16996 if (attr)
16997 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16998
16999 return set_die_type (die, set_type, cu);
17000 }
17001
17002 /* A helper for read_common_block that creates a locexpr baton.
17003 SYM is the symbol which we are marking as computed.
17004 COMMON_DIE is the DIE for the common block.
17005 COMMON_LOC is the location expression attribute for the common
17006 block itself.
17007 MEMBER_LOC is the location expression attribute for the particular
17008 member of the common block that we are processing.
17009 CU is the CU from which the above come. */
17010
17011 static void
17012 mark_common_block_symbol_computed (struct symbol *sym,
17013 struct die_info *common_die,
17014 struct attribute *common_loc,
17015 struct attribute *member_loc,
17016 struct dwarf2_cu *cu)
17017 {
17018 struct dwarf2_per_objfile *dwarf2_per_objfile
17019 = cu->per_cu->dwarf2_per_objfile;
17020 struct objfile *objfile = dwarf2_per_objfile->objfile;
17021 struct dwarf2_locexpr_baton *baton;
17022 gdb_byte *ptr;
17023 unsigned int cu_off;
17024 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
17025 LONGEST offset = 0;
17026
17027 gdb_assert (common_loc && member_loc);
17028 gdb_assert (attr_form_is_block (common_loc));
17029 gdb_assert (attr_form_is_block (member_loc)
17030 || attr_form_is_constant (member_loc));
17031
17032 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17033 baton->per_cu = cu->per_cu;
17034 gdb_assert (baton->per_cu);
17035
17036 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17037
17038 if (attr_form_is_constant (member_loc))
17039 {
17040 offset = dwarf2_get_attr_constant_value (member_loc, 0);
17041 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17042 }
17043 else
17044 baton->size += DW_BLOCK (member_loc)->size;
17045
17046 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17047 baton->data = ptr;
17048
17049 *ptr++ = DW_OP_call4;
17050 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17051 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17052 ptr += 4;
17053
17054 if (attr_form_is_constant (member_loc))
17055 {
17056 *ptr++ = DW_OP_addr;
17057 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17058 ptr += cu->header.addr_size;
17059 }
17060 else
17061 {
17062 /* We have to copy the data here, because DW_OP_call4 will only
17063 use a DW_AT_location attribute. */
17064 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
17065 ptr += DW_BLOCK (member_loc)->size;
17066 }
17067
17068 *ptr++ = DW_OP_plus;
17069 gdb_assert (ptr - baton->data == baton->size);
17070
17071 SYMBOL_LOCATION_BATON (sym) = baton;
17072 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17073 }
17074
17075 /* Create appropriate locally-scoped variables for all the
17076 DW_TAG_common_block entries. Also create a struct common_block
17077 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17078 is used to sepate the common blocks name namespace from regular
17079 variable names. */
17080
17081 static void
17082 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17083 {
17084 struct attribute *attr;
17085
17086 attr = dwarf2_attr (die, DW_AT_location, cu);
17087 if (attr)
17088 {
17089 /* Support the .debug_loc offsets. */
17090 if (attr_form_is_block (attr))
17091 {
17092 /* Ok. */
17093 }
17094 else if (attr_form_is_section_offset (attr))
17095 {
17096 dwarf2_complex_location_expr_complaint ();
17097 attr = NULL;
17098 }
17099 else
17100 {
17101 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17102 "common block member");
17103 attr = NULL;
17104 }
17105 }
17106
17107 if (die->child != NULL)
17108 {
17109 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17110 struct die_info *child_die;
17111 size_t n_entries = 0, size;
17112 struct common_block *common_block;
17113 struct symbol *sym;
17114
17115 for (child_die = die->child;
17116 child_die && child_die->tag;
17117 child_die = sibling_die (child_die))
17118 ++n_entries;
17119
17120 size = (sizeof (struct common_block)
17121 + (n_entries - 1) * sizeof (struct symbol *));
17122 common_block
17123 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17124 size);
17125 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17126 common_block->n_entries = 0;
17127
17128 for (child_die = die->child;
17129 child_die && child_die->tag;
17130 child_die = sibling_die (child_die))
17131 {
17132 /* Create the symbol in the DW_TAG_common_block block in the current
17133 symbol scope. */
17134 sym = new_symbol (child_die, NULL, cu);
17135 if (sym != NULL)
17136 {
17137 struct attribute *member_loc;
17138
17139 common_block->contents[common_block->n_entries++] = sym;
17140
17141 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17142 cu);
17143 if (member_loc)
17144 {
17145 /* GDB has handled this for a long time, but it is
17146 not specified by DWARF. It seems to have been
17147 emitted by gfortran at least as recently as:
17148 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17149 complaint (&symfile_complaints,
17150 _("Variable in common block has "
17151 "DW_AT_data_member_location "
17152 "- DIE at %s [in module %s]"),
17153 sect_offset_str (child_die->sect_off),
17154 objfile_name (objfile));
17155
17156 if (attr_form_is_section_offset (member_loc))
17157 dwarf2_complex_location_expr_complaint ();
17158 else if (attr_form_is_constant (member_loc)
17159 || attr_form_is_block (member_loc))
17160 {
17161 if (attr)
17162 mark_common_block_symbol_computed (sym, die, attr,
17163 member_loc, cu);
17164 }
17165 else
17166 dwarf2_complex_location_expr_complaint ();
17167 }
17168 }
17169 }
17170
17171 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17172 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17173 }
17174 }
17175
17176 /* Create a type for a C++ namespace. */
17177
17178 static struct type *
17179 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17180 {
17181 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17182 const char *previous_prefix, *name;
17183 int is_anonymous;
17184 struct type *type;
17185
17186 /* For extensions, reuse the type of the original namespace. */
17187 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17188 {
17189 struct die_info *ext_die;
17190 struct dwarf2_cu *ext_cu = cu;
17191
17192 ext_die = dwarf2_extension (die, &ext_cu);
17193 type = read_type_die (ext_die, ext_cu);
17194
17195 /* EXT_CU may not be the same as CU.
17196 Ensure TYPE is recorded with CU in die_type_hash. */
17197 return set_die_type (die, type, cu);
17198 }
17199
17200 name = namespace_name (die, &is_anonymous, cu);
17201
17202 /* Now build the name of the current namespace. */
17203
17204 previous_prefix = determine_prefix (die, cu);
17205 if (previous_prefix[0] != '\0')
17206 name = typename_concat (&objfile->objfile_obstack,
17207 previous_prefix, name, 0, cu);
17208
17209 /* Create the type. */
17210 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17211 TYPE_TAG_NAME (type) = TYPE_NAME (type);
17212
17213 return set_die_type (die, type, cu);
17214 }
17215
17216 /* Read a namespace scope. */
17217
17218 static void
17219 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17220 {
17221 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17222 int is_anonymous;
17223
17224 /* Add a symbol associated to this if we haven't seen the namespace
17225 before. Also, add a using directive if it's an anonymous
17226 namespace. */
17227
17228 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17229 {
17230 struct type *type;
17231
17232 type = read_type_die (die, cu);
17233 new_symbol (die, type, cu);
17234
17235 namespace_name (die, &is_anonymous, cu);
17236 if (is_anonymous)
17237 {
17238 const char *previous_prefix = determine_prefix (die, cu);
17239
17240 std::vector<const char *> excludes;
17241 add_using_directive (using_directives (cu->language),
17242 previous_prefix, TYPE_NAME (type), NULL,
17243 NULL, excludes, 0, &objfile->objfile_obstack);
17244 }
17245 }
17246
17247 if (die->child != NULL)
17248 {
17249 struct die_info *child_die = die->child;
17250
17251 while (child_die && child_die->tag)
17252 {
17253 process_die (child_die, cu);
17254 child_die = sibling_die (child_die);
17255 }
17256 }
17257 }
17258
17259 /* Read a Fortran module as type. This DIE can be only a declaration used for
17260 imported module. Still we need that type as local Fortran "use ... only"
17261 declaration imports depend on the created type in determine_prefix. */
17262
17263 static struct type *
17264 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17265 {
17266 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17267 const char *module_name;
17268 struct type *type;
17269
17270 module_name = dwarf2_name (die, cu);
17271 if (!module_name)
17272 complaint (&symfile_complaints,
17273 _("DW_TAG_module has no name, offset %s"),
17274 sect_offset_str (die->sect_off));
17275 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17276
17277 /* determine_prefix uses TYPE_TAG_NAME. */
17278 TYPE_TAG_NAME (type) = TYPE_NAME (type);
17279
17280 return set_die_type (die, type, cu);
17281 }
17282
17283 /* Read a Fortran module. */
17284
17285 static void
17286 read_module (struct die_info *die, struct dwarf2_cu *cu)
17287 {
17288 struct die_info *child_die = die->child;
17289 struct type *type;
17290
17291 type = read_type_die (die, cu);
17292 new_symbol (die, type, cu);
17293
17294 while (child_die && child_die->tag)
17295 {
17296 process_die (child_die, cu);
17297 child_die = sibling_die (child_die);
17298 }
17299 }
17300
17301 /* Return the name of the namespace represented by DIE. Set
17302 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17303 namespace. */
17304
17305 static const char *
17306 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17307 {
17308 struct die_info *current_die;
17309 const char *name = NULL;
17310
17311 /* Loop through the extensions until we find a name. */
17312
17313 for (current_die = die;
17314 current_die != NULL;
17315 current_die = dwarf2_extension (die, &cu))
17316 {
17317 /* We don't use dwarf2_name here so that we can detect the absence
17318 of a name -> anonymous namespace. */
17319 name = dwarf2_string_attr (die, DW_AT_name, cu);
17320
17321 if (name != NULL)
17322 break;
17323 }
17324
17325 /* Is it an anonymous namespace? */
17326
17327 *is_anonymous = (name == NULL);
17328 if (*is_anonymous)
17329 name = CP_ANONYMOUS_NAMESPACE_STR;
17330
17331 return name;
17332 }
17333
17334 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17335 the user defined type vector. */
17336
17337 static struct type *
17338 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17339 {
17340 struct gdbarch *gdbarch
17341 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17342 struct comp_unit_head *cu_header = &cu->header;
17343 struct type *type;
17344 struct attribute *attr_byte_size;
17345 struct attribute *attr_address_class;
17346 int byte_size, addr_class;
17347 struct type *target_type;
17348
17349 target_type = die_type (die, cu);
17350
17351 /* The die_type call above may have already set the type for this DIE. */
17352 type = get_die_type (die, cu);
17353 if (type)
17354 return type;
17355
17356 type = lookup_pointer_type (target_type);
17357
17358 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17359 if (attr_byte_size)
17360 byte_size = DW_UNSND (attr_byte_size);
17361 else
17362 byte_size = cu_header->addr_size;
17363
17364 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17365 if (attr_address_class)
17366 addr_class = DW_UNSND (attr_address_class);
17367 else
17368 addr_class = DW_ADDR_none;
17369
17370 /* If the pointer size or address class is different than the
17371 default, create a type variant marked as such and set the
17372 length accordingly. */
17373 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
17374 {
17375 if (gdbarch_address_class_type_flags_p (gdbarch))
17376 {
17377 int type_flags;
17378
17379 type_flags = gdbarch_address_class_type_flags
17380 (gdbarch, byte_size, addr_class);
17381 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17382 == 0);
17383 type = make_type_with_address_space (type, type_flags);
17384 }
17385 else if (TYPE_LENGTH (type) != byte_size)
17386 {
17387 complaint (&symfile_complaints,
17388 _("invalid pointer size %d"), byte_size);
17389 }
17390 else
17391 {
17392 /* Should we also complain about unhandled address classes? */
17393 }
17394 }
17395
17396 TYPE_LENGTH (type) = byte_size;
17397 return set_die_type (die, type, cu);
17398 }
17399
17400 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17401 the user defined type vector. */
17402
17403 static struct type *
17404 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17405 {
17406 struct type *type;
17407 struct type *to_type;
17408 struct type *domain;
17409
17410 to_type = die_type (die, cu);
17411 domain = die_containing_type (die, cu);
17412
17413 /* The calls above may have already set the type for this DIE. */
17414 type = get_die_type (die, cu);
17415 if (type)
17416 return type;
17417
17418 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17419 type = lookup_methodptr_type (to_type);
17420 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17421 {
17422 struct type *new_type
17423 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17424
17425 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17426 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17427 TYPE_VARARGS (to_type));
17428 type = lookup_methodptr_type (new_type);
17429 }
17430 else
17431 type = lookup_memberptr_type (to_type, domain);
17432
17433 return set_die_type (die, type, cu);
17434 }
17435
17436 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17437 the user defined type vector. */
17438
17439 static struct type *
17440 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17441 enum type_code refcode)
17442 {
17443 struct comp_unit_head *cu_header = &cu->header;
17444 struct type *type, *target_type;
17445 struct attribute *attr;
17446
17447 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17448
17449 target_type = die_type (die, cu);
17450
17451 /* The die_type call above may have already set the type for this DIE. */
17452 type = get_die_type (die, cu);
17453 if (type)
17454 return type;
17455
17456 type = lookup_reference_type (target_type, refcode);
17457 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17458 if (attr)
17459 {
17460 TYPE_LENGTH (type) = DW_UNSND (attr);
17461 }
17462 else
17463 {
17464 TYPE_LENGTH (type) = cu_header->addr_size;
17465 }
17466 return set_die_type (die, type, cu);
17467 }
17468
17469 /* Add the given cv-qualifiers to the element type of the array. GCC
17470 outputs DWARF type qualifiers that apply to an array, not the
17471 element type. But GDB relies on the array element type to carry
17472 the cv-qualifiers. This mimics section 6.7.3 of the C99
17473 specification. */
17474
17475 static struct type *
17476 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17477 struct type *base_type, int cnst, int voltl)
17478 {
17479 struct type *el_type, *inner_array;
17480
17481 base_type = copy_type (base_type);
17482 inner_array = base_type;
17483
17484 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17485 {
17486 TYPE_TARGET_TYPE (inner_array) =
17487 copy_type (TYPE_TARGET_TYPE (inner_array));
17488 inner_array = TYPE_TARGET_TYPE (inner_array);
17489 }
17490
17491 el_type = TYPE_TARGET_TYPE (inner_array);
17492 cnst |= TYPE_CONST (el_type);
17493 voltl |= TYPE_VOLATILE (el_type);
17494 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17495
17496 return set_die_type (die, base_type, cu);
17497 }
17498
17499 static struct type *
17500 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17501 {
17502 struct type *base_type, *cv_type;
17503
17504 base_type = die_type (die, cu);
17505
17506 /* The die_type call above may have already set the type for this DIE. */
17507 cv_type = get_die_type (die, cu);
17508 if (cv_type)
17509 return cv_type;
17510
17511 /* In case the const qualifier is applied to an array type, the element type
17512 is so qualified, not the array type (section 6.7.3 of C99). */
17513 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17514 return add_array_cv_type (die, cu, base_type, 1, 0);
17515
17516 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17517 return set_die_type (die, cv_type, cu);
17518 }
17519
17520 static struct type *
17521 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17522 {
17523 struct type *base_type, *cv_type;
17524
17525 base_type = die_type (die, cu);
17526
17527 /* The die_type call above may have already set the type for this DIE. */
17528 cv_type = get_die_type (die, cu);
17529 if (cv_type)
17530 return cv_type;
17531
17532 /* In case the volatile qualifier is applied to an array type, the
17533 element type is so qualified, not the array type (section 6.7.3
17534 of C99). */
17535 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17536 return add_array_cv_type (die, cu, base_type, 0, 1);
17537
17538 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17539 return set_die_type (die, cv_type, cu);
17540 }
17541
17542 /* Handle DW_TAG_restrict_type. */
17543
17544 static struct type *
17545 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17546 {
17547 struct type *base_type, *cv_type;
17548
17549 base_type = die_type (die, cu);
17550
17551 /* The die_type call above may have already set the type for this DIE. */
17552 cv_type = get_die_type (die, cu);
17553 if (cv_type)
17554 return cv_type;
17555
17556 cv_type = make_restrict_type (base_type);
17557 return set_die_type (die, cv_type, cu);
17558 }
17559
17560 /* Handle DW_TAG_atomic_type. */
17561
17562 static struct type *
17563 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17564 {
17565 struct type *base_type, *cv_type;
17566
17567 base_type = die_type (die, cu);
17568
17569 /* The die_type call above may have already set the type for this DIE. */
17570 cv_type = get_die_type (die, cu);
17571 if (cv_type)
17572 return cv_type;
17573
17574 cv_type = make_atomic_type (base_type);
17575 return set_die_type (die, cv_type, cu);
17576 }
17577
17578 /* Extract all information from a DW_TAG_string_type DIE and add to
17579 the user defined type vector. It isn't really a user defined type,
17580 but it behaves like one, with other DIE's using an AT_user_def_type
17581 attribute to reference it. */
17582
17583 static struct type *
17584 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17585 {
17586 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17587 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17588 struct type *type, *range_type, *index_type, *char_type;
17589 struct attribute *attr;
17590 unsigned int length;
17591
17592 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17593 if (attr)
17594 {
17595 length = DW_UNSND (attr);
17596 }
17597 else
17598 {
17599 /* Check for the DW_AT_byte_size attribute. */
17600 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17601 if (attr)
17602 {
17603 length = DW_UNSND (attr);
17604 }
17605 else
17606 {
17607 length = 1;
17608 }
17609 }
17610
17611 index_type = objfile_type (objfile)->builtin_int;
17612 range_type = create_static_range_type (NULL, index_type, 1, length);
17613 char_type = language_string_char_type (cu->language_defn, gdbarch);
17614 type = create_string_type (NULL, char_type, range_type);
17615
17616 return set_die_type (die, type, cu);
17617 }
17618
17619 /* Assuming that DIE corresponds to a function, returns nonzero
17620 if the function is prototyped. */
17621
17622 static int
17623 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17624 {
17625 struct attribute *attr;
17626
17627 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17628 if (attr && (DW_UNSND (attr) != 0))
17629 return 1;
17630
17631 /* The DWARF standard implies that the DW_AT_prototyped attribute
17632 is only meaninful for C, but the concept also extends to other
17633 languages that allow unprototyped functions (Eg: Objective C).
17634 For all other languages, assume that functions are always
17635 prototyped. */
17636 if (cu->language != language_c
17637 && cu->language != language_objc
17638 && cu->language != language_opencl)
17639 return 1;
17640
17641 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17642 prototyped and unprototyped functions; default to prototyped,
17643 since that is more common in modern code (and RealView warns
17644 about unprototyped functions). */
17645 if (producer_is_realview (cu->producer))
17646 return 1;
17647
17648 return 0;
17649 }
17650
17651 /* Handle DIES due to C code like:
17652
17653 struct foo
17654 {
17655 int (*funcp)(int a, long l);
17656 int b;
17657 };
17658
17659 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17660
17661 static struct type *
17662 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17663 {
17664 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17665 struct type *type; /* Type that this function returns. */
17666 struct type *ftype; /* Function that returns above type. */
17667 struct attribute *attr;
17668
17669 type = die_type (die, cu);
17670
17671 /* The die_type call above may have already set the type for this DIE. */
17672 ftype = get_die_type (die, cu);
17673 if (ftype)
17674 return ftype;
17675
17676 ftype = lookup_function_type (type);
17677
17678 if (prototyped_function_p (die, cu))
17679 TYPE_PROTOTYPED (ftype) = 1;
17680
17681 /* Store the calling convention in the type if it's available in
17682 the subroutine die. Otherwise set the calling convention to
17683 the default value DW_CC_normal. */
17684 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17685 if (attr)
17686 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17687 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17688 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17689 else
17690 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17691
17692 /* Record whether the function returns normally to its caller or not
17693 if the DWARF producer set that information. */
17694 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17695 if (attr && (DW_UNSND (attr) != 0))
17696 TYPE_NO_RETURN (ftype) = 1;
17697
17698 /* We need to add the subroutine type to the die immediately so
17699 we don't infinitely recurse when dealing with parameters
17700 declared as the same subroutine type. */
17701 set_die_type (die, ftype, cu);
17702
17703 if (die->child != NULL)
17704 {
17705 struct type *void_type = objfile_type (objfile)->builtin_void;
17706 struct die_info *child_die;
17707 int nparams, iparams;
17708
17709 /* Count the number of parameters.
17710 FIXME: GDB currently ignores vararg functions, but knows about
17711 vararg member functions. */
17712 nparams = 0;
17713 child_die = die->child;
17714 while (child_die && child_die->tag)
17715 {
17716 if (child_die->tag == DW_TAG_formal_parameter)
17717 nparams++;
17718 else if (child_die->tag == DW_TAG_unspecified_parameters)
17719 TYPE_VARARGS (ftype) = 1;
17720 child_die = sibling_die (child_die);
17721 }
17722
17723 /* Allocate storage for parameters and fill them in. */
17724 TYPE_NFIELDS (ftype) = nparams;
17725 TYPE_FIELDS (ftype) = (struct field *)
17726 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17727
17728 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17729 even if we error out during the parameters reading below. */
17730 for (iparams = 0; iparams < nparams; iparams++)
17731 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17732
17733 iparams = 0;
17734 child_die = die->child;
17735 while (child_die && child_die->tag)
17736 {
17737 if (child_die->tag == DW_TAG_formal_parameter)
17738 {
17739 struct type *arg_type;
17740
17741 /* DWARF version 2 has no clean way to discern C++
17742 static and non-static member functions. G++ helps
17743 GDB by marking the first parameter for non-static
17744 member functions (which is the this pointer) as
17745 artificial. We pass this information to
17746 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17747
17748 DWARF version 3 added DW_AT_object_pointer, which GCC
17749 4.5 does not yet generate. */
17750 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17751 if (attr)
17752 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17753 else
17754 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17755 arg_type = die_type (child_die, cu);
17756
17757 /* RealView does not mark THIS as const, which the testsuite
17758 expects. GCC marks THIS as const in method definitions,
17759 but not in the class specifications (GCC PR 43053). */
17760 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17761 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17762 {
17763 int is_this = 0;
17764 struct dwarf2_cu *arg_cu = cu;
17765 const char *name = dwarf2_name (child_die, cu);
17766
17767 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17768 if (attr)
17769 {
17770 /* If the compiler emits this, use it. */
17771 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17772 is_this = 1;
17773 }
17774 else if (name && strcmp (name, "this") == 0)
17775 /* Function definitions will have the argument names. */
17776 is_this = 1;
17777 else if (name == NULL && iparams == 0)
17778 /* Declarations may not have the names, so like
17779 elsewhere in GDB, assume an artificial first
17780 argument is "this". */
17781 is_this = 1;
17782
17783 if (is_this)
17784 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17785 arg_type, 0);
17786 }
17787
17788 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17789 iparams++;
17790 }
17791 child_die = sibling_die (child_die);
17792 }
17793 }
17794
17795 return ftype;
17796 }
17797
17798 static struct type *
17799 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17800 {
17801 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17802 const char *name = NULL;
17803 struct type *this_type, *target_type;
17804
17805 name = dwarf2_full_name (NULL, die, cu);
17806 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17807 TYPE_TARGET_STUB (this_type) = 1;
17808 set_die_type (die, this_type, cu);
17809 target_type = die_type (die, cu);
17810 if (target_type != this_type)
17811 TYPE_TARGET_TYPE (this_type) = target_type;
17812 else
17813 {
17814 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17815 spec and cause infinite loops in GDB. */
17816 complaint (&symfile_complaints,
17817 _("Self-referential DW_TAG_typedef "
17818 "- DIE at %s [in module %s]"),
17819 sect_offset_str (die->sect_off), objfile_name (objfile));
17820 TYPE_TARGET_TYPE (this_type) = NULL;
17821 }
17822 return this_type;
17823 }
17824
17825 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17826 (which may be different from NAME) to the architecture back-end to allow
17827 it to guess the correct format if necessary. */
17828
17829 static struct type *
17830 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17831 const char *name_hint)
17832 {
17833 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17834 const struct floatformat **format;
17835 struct type *type;
17836
17837 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17838 if (format)
17839 type = init_float_type (objfile, bits, name, format);
17840 else
17841 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17842
17843 return type;
17844 }
17845
17846 /* Find a representation of a given base type and install
17847 it in the TYPE field of the die. */
17848
17849 static struct type *
17850 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17851 {
17852 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17853 struct type *type;
17854 struct attribute *attr;
17855 int encoding = 0, bits = 0;
17856 const char *name;
17857
17858 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17859 if (attr)
17860 {
17861 encoding = DW_UNSND (attr);
17862 }
17863 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17864 if (attr)
17865 {
17866 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17867 }
17868 name = dwarf2_name (die, cu);
17869 if (!name)
17870 {
17871 complaint (&symfile_complaints,
17872 _("DW_AT_name missing from DW_TAG_base_type"));
17873 }
17874
17875 switch (encoding)
17876 {
17877 case DW_ATE_address:
17878 /* Turn DW_ATE_address into a void * pointer. */
17879 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17880 type = init_pointer_type (objfile, bits, name, type);
17881 break;
17882 case DW_ATE_boolean:
17883 type = init_boolean_type (objfile, bits, 1, name);
17884 break;
17885 case DW_ATE_complex_float:
17886 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17887 type = init_complex_type (objfile, name, type);
17888 break;
17889 case DW_ATE_decimal_float:
17890 type = init_decfloat_type (objfile, bits, name);
17891 break;
17892 case DW_ATE_float:
17893 type = dwarf2_init_float_type (objfile, bits, name, name);
17894 break;
17895 case DW_ATE_signed:
17896 type = init_integer_type (objfile, bits, 0, name);
17897 break;
17898 case DW_ATE_unsigned:
17899 if (cu->language == language_fortran
17900 && name
17901 && startswith (name, "character("))
17902 type = init_character_type (objfile, bits, 1, name);
17903 else
17904 type = init_integer_type (objfile, bits, 1, name);
17905 break;
17906 case DW_ATE_signed_char:
17907 if (cu->language == language_ada || cu->language == language_m2
17908 || cu->language == language_pascal
17909 || cu->language == language_fortran)
17910 type = init_character_type (objfile, bits, 0, name);
17911 else
17912 type = init_integer_type (objfile, bits, 0, name);
17913 break;
17914 case DW_ATE_unsigned_char:
17915 if (cu->language == language_ada || cu->language == language_m2
17916 || cu->language == language_pascal
17917 || cu->language == language_fortran
17918 || cu->language == language_rust)
17919 type = init_character_type (objfile, bits, 1, name);
17920 else
17921 type = init_integer_type (objfile, bits, 1, name);
17922 break;
17923 case DW_ATE_UTF:
17924 {
17925 gdbarch *arch = get_objfile_arch (objfile);
17926
17927 if (bits == 16)
17928 type = builtin_type (arch)->builtin_char16;
17929 else if (bits == 32)
17930 type = builtin_type (arch)->builtin_char32;
17931 else
17932 {
17933 complaint (&symfile_complaints,
17934 _("unsupported DW_ATE_UTF bit size: '%d'"),
17935 bits);
17936 type = init_integer_type (objfile, bits, 1, name);
17937 }
17938 return set_die_type (die, type, cu);
17939 }
17940 break;
17941
17942 default:
17943 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17944 dwarf_type_encoding_name (encoding));
17945 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17946 break;
17947 }
17948
17949 if (name && strcmp (name, "char") == 0)
17950 TYPE_NOSIGN (type) = 1;
17951
17952 return set_die_type (die, type, cu);
17953 }
17954
17955 /* Parse dwarf attribute if it's a block, reference or constant and put the
17956 resulting value of the attribute into struct bound_prop.
17957 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17958
17959 static int
17960 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17961 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17962 {
17963 struct dwarf2_property_baton *baton;
17964 struct obstack *obstack
17965 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17966
17967 if (attr == NULL || prop == NULL)
17968 return 0;
17969
17970 if (attr_form_is_block (attr))
17971 {
17972 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17973 baton->referenced_type = NULL;
17974 baton->locexpr.per_cu = cu->per_cu;
17975 baton->locexpr.size = DW_BLOCK (attr)->size;
17976 baton->locexpr.data = DW_BLOCK (attr)->data;
17977 prop->data.baton = baton;
17978 prop->kind = PROP_LOCEXPR;
17979 gdb_assert (prop->data.baton != NULL);
17980 }
17981 else if (attr_form_is_ref (attr))
17982 {
17983 struct dwarf2_cu *target_cu = cu;
17984 struct die_info *target_die;
17985 struct attribute *target_attr;
17986
17987 target_die = follow_die_ref (die, attr, &target_cu);
17988 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17989 if (target_attr == NULL)
17990 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17991 target_cu);
17992 if (target_attr == NULL)
17993 return 0;
17994
17995 switch (target_attr->name)
17996 {
17997 case DW_AT_location:
17998 if (attr_form_is_section_offset (target_attr))
17999 {
18000 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18001 baton->referenced_type = die_type (target_die, target_cu);
18002 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18003 prop->data.baton = baton;
18004 prop->kind = PROP_LOCLIST;
18005 gdb_assert (prop->data.baton != NULL);
18006 }
18007 else if (attr_form_is_block (target_attr))
18008 {
18009 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18010 baton->referenced_type = die_type (target_die, target_cu);
18011 baton->locexpr.per_cu = cu->per_cu;
18012 baton->locexpr.size = DW_BLOCK (target_attr)->size;
18013 baton->locexpr.data = DW_BLOCK (target_attr)->data;
18014 prop->data.baton = baton;
18015 prop->kind = PROP_LOCEXPR;
18016 gdb_assert (prop->data.baton != NULL);
18017 }
18018 else
18019 {
18020 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18021 "dynamic property");
18022 return 0;
18023 }
18024 break;
18025 case DW_AT_data_member_location:
18026 {
18027 LONGEST offset;
18028
18029 if (!handle_data_member_location (target_die, target_cu,
18030 &offset))
18031 return 0;
18032
18033 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18034 baton->referenced_type = read_type_die (target_die->parent,
18035 target_cu);
18036 baton->offset_info.offset = offset;
18037 baton->offset_info.type = die_type (target_die, target_cu);
18038 prop->data.baton = baton;
18039 prop->kind = PROP_ADDR_OFFSET;
18040 break;
18041 }
18042 }
18043 }
18044 else if (attr_form_is_constant (attr))
18045 {
18046 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
18047 prop->kind = PROP_CONST;
18048 }
18049 else
18050 {
18051 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18052 dwarf2_name (die, cu));
18053 return 0;
18054 }
18055
18056 return 1;
18057 }
18058
18059 /* Read the given DW_AT_subrange DIE. */
18060
18061 static struct type *
18062 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18063 {
18064 struct type *base_type, *orig_base_type;
18065 struct type *range_type;
18066 struct attribute *attr;
18067 struct dynamic_prop low, high;
18068 int low_default_is_valid;
18069 int high_bound_is_count = 0;
18070 const char *name;
18071 LONGEST negative_mask;
18072
18073 orig_base_type = die_type (die, cu);
18074 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18075 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18076 creating the range type, but we use the result of check_typedef
18077 when examining properties of the type. */
18078 base_type = check_typedef (orig_base_type);
18079
18080 /* The die_type call above may have already set the type for this DIE. */
18081 range_type = get_die_type (die, cu);
18082 if (range_type)
18083 return range_type;
18084
18085 low.kind = PROP_CONST;
18086 high.kind = PROP_CONST;
18087 high.data.const_val = 0;
18088
18089 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18090 omitting DW_AT_lower_bound. */
18091 switch (cu->language)
18092 {
18093 case language_c:
18094 case language_cplus:
18095 low.data.const_val = 0;
18096 low_default_is_valid = 1;
18097 break;
18098 case language_fortran:
18099 low.data.const_val = 1;
18100 low_default_is_valid = 1;
18101 break;
18102 case language_d:
18103 case language_objc:
18104 case language_rust:
18105 low.data.const_val = 0;
18106 low_default_is_valid = (cu->header.version >= 4);
18107 break;
18108 case language_ada:
18109 case language_m2:
18110 case language_pascal:
18111 low.data.const_val = 1;
18112 low_default_is_valid = (cu->header.version >= 4);
18113 break;
18114 default:
18115 low.data.const_val = 0;
18116 low_default_is_valid = 0;
18117 break;
18118 }
18119
18120 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18121 if (attr)
18122 attr_to_dynamic_prop (attr, die, cu, &low);
18123 else if (!low_default_is_valid)
18124 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
18125 "- DIE at %s [in module %s]"),
18126 sect_offset_str (die->sect_off),
18127 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18128
18129 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
18130 if (!attr_to_dynamic_prop (attr, die, cu, &high))
18131 {
18132 attr = dwarf2_attr (die, DW_AT_count, cu);
18133 if (attr_to_dynamic_prop (attr, die, cu, &high))
18134 {
18135 /* If bounds are constant do the final calculation here. */
18136 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18137 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18138 else
18139 high_bound_is_count = 1;
18140 }
18141 }
18142
18143 /* Dwarf-2 specifications explicitly allows to create subrange types
18144 without specifying a base type.
18145 In that case, the base type must be set to the type of
18146 the lower bound, upper bound or count, in that order, if any of these
18147 three attributes references an object that has a type.
18148 If no base type is found, the Dwarf-2 specifications say that
18149 a signed integer type of size equal to the size of an address should
18150 be used.
18151 For the following C code: `extern char gdb_int [];'
18152 GCC produces an empty range DIE.
18153 FIXME: muller/2010-05-28: Possible references to object for low bound,
18154 high bound or count are not yet handled by this code. */
18155 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
18156 {
18157 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18158 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18159 int addr_size = gdbarch_addr_bit (gdbarch) /8;
18160 struct type *int_type = objfile_type (objfile)->builtin_int;
18161
18162 /* Test "int", "long int", and "long long int" objfile types,
18163 and select the first one having a size above or equal to the
18164 architecture address size. */
18165 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18166 base_type = int_type;
18167 else
18168 {
18169 int_type = objfile_type (objfile)->builtin_long;
18170 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18171 base_type = int_type;
18172 else
18173 {
18174 int_type = objfile_type (objfile)->builtin_long_long;
18175 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18176 base_type = int_type;
18177 }
18178 }
18179 }
18180
18181 /* Normally, the DWARF producers are expected to use a signed
18182 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18183 But this is unfortunately not always the case, as witnessed
18184 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18185 is used instead. To work around that ambiguity, we treat
18186 the bounds as signed, and thus sign-extend their values, when
18187 the base type is signed. */
18188 negative_mask =
18189 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18190 if (low.kind == PROP_CONST
18191 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18192 low.data.const_val |= negative_mask;
18193 if (high.kind == PROP_CONST
18194 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18195 high.data.const_val |= negative_mask;
18196
18197 range_type = create_range_type (NULL, orig_base_type, &low, &high);
18198
18199 if (high_bound_is_count)
18200 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18201
18202 /* Ada expects an empty array on no boundary attributes. */
18203 if (attr == NULL && cu->language != language_ada)
18204 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
18205
18206 name = dwarf2_name (die, cu);
18207 if (name)
18208 TYPE_NAME (range_type) = name;
18209
18210 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18211 if (attr)
18212 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18213
18214 set_die_type (die, range_type, cu);
18215
18216 /* set_die_type should be already done. */
18217 set_descriptive_type (range_type, die, cu);
18218
18219 return range_type;
18220 }
18221
18222 static struct type *
18223 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18224 {
18225 struct type *type;
18226
18227 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18228 NULL);
18229 TYPE_NAME (type) = dwarf2_name (die, cu);
18230
18231 /* In Ada, an unspecified type is typically used when the description
18232 of the type is defered to a different unit. When encountering
18233 such a type, we treat it as a stub, and try to resolve it later on,
18234 when needed. */
18235 if (cu->language == language_ada)
18236 TYPE_STUB (type) = 1;
18237
18238 return set_die_type (die, type, cu);
18239 }
18240
18241 /* Read a single die and all its descendents. Set the die's sibling
18242 field to NULL; set other fields in the die correctly, and set all
18243 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18244 location of the info_ptr after reading all of those dies. PARENT
18245 is the parent of the die in question. */
18246
18247 static struct die_info *
18248 read_die_and_children (const struct die_reader_specs *reader,
18249 const gdb_byte *info_ptr,
18250 const gdb_byte **new_info_ptr,
18251 struct die_info *parent)
18252 {
18253 struct die_info *die;
18254 const gdb_byte *cur_ptr;
18255 int has_children;
18256
18257 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18258 if (die == NULL)
18259 {
18260 *new_info_ptr = cur_ptr;
18261 return NULL;
18262 }
18263 store_in_ref_table (die, reader->cu);
18264
18265 if (has_children)
18266 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18267 else
18268 {
18269 die->child = NULL;
18270 *new_info_ptr = cur_ptr;
18271 }
18272
18273 die->sibling = NULL;
18274 die->parent = parent;
18275 return die;
18276 }
18277
18278 /* Read a die, all of its descendents, and all of its siblings; set
18279 all of the fields of all of the dies correctly. Arguments are as
18280 in read_die_and_children. */
18281
18282 static struct die_info *
18283 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18284 const gdb_byte *info_ptr,
18285 const gdb_byte **new_info_ptr,
18286 struct die_info *parent)
18287 {
18288 struct die_info *first_die, *last_sibling;
18289 const gdb_byte *cur_ptr;
18290
18291 cur_ptr = info_ptr;
18292 first_die = last_sibling = NULL;
18293
18294 while (1)
18295 {
18296 struct die_info *die
18297 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18298
18299 if (die == NULL)
18300 {
18301 *new_info_ptr = cur_ptr;
18302 return first_die;
18303 }
18304
18305 if (!first_die)
18306 first_die = die;
18307 else
18308 last_sibling->sibling = die;
18309
18310 last_sibling = die;
18311 }
18312 }
18313
18314 /* Read a die, all of its descendents, and all of its siblings; set
18315 all of the fields of all of the dies correctly. Arguments are as
18316 in read_die_and_children.
18317 This the main entry point for reading a DIE and all its children. */
18318
18319 static struct die_info *
18320 read_die_and_siblings (const struct die_reader_specs *reader,
18321 const gdb_byte *info_ptr,
18322 const gdb_byte **new_info_ptr,
18323 struct die_info *parent)
18324 {
18325 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18326 new_info_ptr, parent);
18327
18328 if (dwarf_die_debug)
18329 {
18330 fprintf_unfiltered (gdb_stdlog,
18331 "Read die from %s@0x%x of %s:\n",
18332 get_section_name (reader->die_section),
18333 (unsigned) (info_ptr - reader->die_section->buffer),
18334 bfd_get_filename (reader->abfd));
18335 dump_die (die, dwarf_die_debug);
18336 }
18337
18338 return die;
18339 }
18340
18341 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18342 attributes.
18343 The caller is responsible for filling in the extra attributes
18344 and updating (*DIEP)->num_attrs.
18345 Set DIEP to point to a newly allocated die with its information,
18346 except for its child, sibling, and parent fields.
18347 Set HAS_CHILDREN to tell whether the die has children or not. */
18348
18349 static const gdb_byte *
18350 read_full_die_1 (const struct die_reader_specs *reader,
18351 struct die_info **diep, const gdb_byte *info_ptr,
18352 int *has_children, int num_extra_attrs)
18353 {
18354 unsigned int abbrev_number, bytes_read, i;
18355 struct abbrev_info *abbrev;
18356 struct die_info *die;
18357 struct dwarf2_cu *cu = reader->cu;
18358 bfd *abfd = reader->abfd;
18359
18360 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18361 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18362 info_ptr += bytes_read;
18363 if (!abbrev_number)
18364 {
18365 *diep = NULL;
18366 *has_children = 0;
18367 return info_ptr;
18368 }
18369
18370 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18371 if (!abbrev)
18372 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18373 abbrev_number,
18374 bfd_get_filename (abfd));
18375
18376 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18377 die->sect_off = sect_off;
18378 die->tag = abbrev->tag;
18379 die->abbrev = abbrev_number;
18380
18381 /* Make the result usable.
18382 The caller needs to update num_attrs after adding the extra
18383 attributes. */
18384 die->num_attrs = abbrev->num_attrs;
18385
18386 for (i = 0; i < abbrev->num_attrs; ++i)
18387 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18388 info_ptr);
18389
18390 *diep = die;
18391 *has_children = abbrev->has_children;
18392 return info_ptr;
18393 }
18394
18395 /* Read a die and all its attributes.
18396 Set DIEP to point to a newly allocated die with its information,
18397 except for its child, sibling, and parent fields.
18398 Set HAS_CHILDREN to tell whether the die has children or not. */
18399
18400 static const gdb_byte *
18401 read_full_die (const struct die_reader_specs *reader,
18402 struct die_info **diep, const gdb_byte *info_ptr,
18403 int *has_children)
18404 {
18405 const gdb_byte *result;
18406
18407 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18408
18409 if (dwarf_die_debug)
18410 {
18411 fprintf_unfiltered (gdb_stdlog,
18412 "Read die from %s@0x%x of %s:\n",
18413 get_section_name (reader->die_section),
18414 (unsigned) (info_ptr - reader->die_section->buffer),
18415 bfd_get_filename (reader->abfd));
18416 dump_die (*diep, dwarf_die_debug);
18417 }
18418
18419 return result;
18420 }
18421 \f
18422 /* Abbreviation tables.
18423
18424 In DWARF version 2, the description of the debugging information is
18425 stored in a separate .debug_abbrev section. Before we read any
18426 dies from a section we read in all abbreviations and install them
18427 in a hash table. */
18428
18429 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18430
18431 struct abbrev_info *
18432 abbrev_table::alloc_abbrev ()
18433 {
18434 struct abbrev_info *abbrev;
18435
18436 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18437 memset (abbrev, 0, sizeof (struct abbrev_info));
18438
18439 return abbrev;
18440 }
18441
18442 /* Add an abbreviation to the table. */
18443
18444 void
18445 abbrev_table::add_abbrev (unsigned int abbrev_number,
18446 struct abbrev_info *abbrev)
18447 {
18448 unsigned int hash_number;
18449
18450 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18451 abbrev->next = m_abbrevs[hash_number];
18452 m_abbrevs[hash_number] = abbrev;
18453 }
18454
18455 /* Look up an abbrev in the table.
18456 Returns NULL if the abbrev is not found. */
18457
18458 struct abbrev_info *
18459 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18460 {
18461 unsigned int hash_number;
18462 struct abbrev_info *abbrev;
18463
18464 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18465 abbrev = m_abbrevs[hash_number];
18466
18467 while (abbrev)
18468 {
18469 if (abbrev->number == abbrev_number)
18470 return abbrev;
18471 abbrev = abbrev->next;
18472 }
18473 return NULL;
18474 }
18475
18476 /* Read in an abbrev table. */
18477
18478 static abbrev_table_up
18479 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18480 struct dwarf2_section_info *section,
18481 sect_offset sect_off)
18482 {
18483 struct objfile *objfile = dwarf2_per_objfile->objfile;
18484 bfd *abfd = get_section_bfd_owner (section);
18485 const gdb_byte *abbrev_ptr;
18486 struct abbrev_info *cur_abbrev;
18487 unsigned int abbrev_number, bytes_read, abbrev_name;
18488 unsigned int abbrev_form;
18489 struct attr_abbrev *cur_attrs;
18490 unsigned int allocated_attrs;
18491
18492 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18493
18494 dwarf2_read_section (objfile, section);
18495 abbrev_ptr = section->buffer + to_underlying (sect_off);
18496 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18497 abbrev_ptr += bytes_read;
18498
18499 allocated_attrs = ATTR_ALLOC_CHUNK;
18500 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18501
18502 /* Loop until we reach an abbrev number of 0. */
18503 while (abbrev_number)
18504 {
18505 cur_abbrev = abbrev_table->alloc_abbrev ();
18506
18507 /* read in abbrev header */
18508 cur_abbrev->number = abbrev_number;
18509 cur_abbrev->tag
18510 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18511 abbrev_ptr += bytes_read;
18512 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18513 abbrev_ptr += 1;
18514
18515 /* now read in declarations */
18516 for (;;)
18517 {
18518 LONGEST implicit_const;
18519
18520 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18521 abbrev_ptr += bytes_read;
18522 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18523 abbrev_ptr += bytes_read;
18524 if (abbrev_form == DW_FORM_implicit_const)
18525 {
18526 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18527 &bytes_read);
18528 abbrev_ptr += bytes_read;
18529 }
18530 else
18531 {
18532 /* Initialize it due to a false compiler warning. */
18533 implicit_const = -1;
18534 }
18535
18536 if (abbrev_name == 0)
18537 break;
18538
18539 if (cur_abbrev->num_attrs == allocated_attrs)
18540 {
18541 allocated_attrs += ATTR_ALLOC_CHUNK;
18542 cur_attrs
18543 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18544 }
18545
18546 cur_attrs[cur_abbrev->num_attrs].name
18547 = (enum dwarf_attribute) abbrev_name;
18548 cur_attrs[cur_abbrev->num_attrs].form
18549 = (enum dwarf_form) abbrev_form;
18550 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18551 ++cur_abbrev->num_attrs;
18552 }
18553
18554 cur_abbrev->attrs =
18555 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18556 cur_abbrev->num_attrs);
18557 memcpy (cur_abbrev->attrs, cur_attrs,
18558 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18559
18560 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18561
18562 /* Get next abbreviation.
18563 Under Irix6 the abbreviations for a compilation unit are not
18564 always properly terminated with an abbrev number of 0.
18565 Exit loop if we encounter an abbreviation which we have
18566 already read (which means we are about to read the abbreviations
18567 for the next compile unit) or if the end of the abbreviation
18568 table is reached. */
18569 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18570 break;
18571 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18572 abbrev_ptr += bytes_read;
18573 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18574 break;
18575 }
18576
18577 xfree (cur_attrs);
18578 return abbrev_table;
18579 }
18580
18581 /* Returns nonzero if TAG represents a type that we might generate a partial
18582 symbol for. */
18583
18584 static int
18585 is_type_tag_for_partial (int tag)
18586 {
18587 switch (tag)
18588 {
18589 #if 0
18590 /* Some types that would be reasonable to generate partial symbols for,
18591 that we don't at present. */
18592 case DW_TAG_array_type:
18593 case DW_TAG_file_type:
18594 case DW_TAG_ptr_to_member_type:
18595 case DW_TAG_set_type:
18596 case DW_TAG_string_type:
18597 case DW_TAG_subroutine_type:
18598 #endif
18599 case DW_TAG_base_type:
18600 case DW_TAG_class_type:
18601 case DW_TAG_interface_type:
18602 case DW_TAG_enumeration_type:
18603 case DW_TAG_structure_type:
18604 case DW_TAG_subrange_type:
18605 case DW_TAG_typedef:
18606 case DW_TAG_union_type:
18607 return 1;
18608 default:
18609 return 0;
18610 }
18611 }
18612
18613 /* Load all DIEs that are interesting for partial symbols into memory. */
18614
18615 static struct partial_die_info *
18616 load_partial_dies (const struct die_reader_specs *reader,
18617 const gdb_byte *info_ptr, int building_psymtab)
18618 {
18619 struct dwarf2_cu *cu = reader->cu;
18620 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18621 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18622 unsigned int bytes_read;
18623 unsigned int load_all = 0;
18624 int nesting_level = 1;
18625
18626 parent_die = NULL;
18627 last_die = NULL;
18628
18629 gdb_assert (cu->per_cu != NULL);
18630 if (cu->per_cu->load_all_dies)
18631 load_all = 1;
18632
18633 cu->partial_dies
18634 = htab_create_alloc_ex (cu->header.length / 12,
18635 partial_die_hash,
18636 partial_die_eq,
18637 NULL,
18638 &cu->comp_unit_obstack,
18639 hashtab_obstack_allocate,
18640 dummy_obstack_deallocate);
18641
18642 while (1)
18643 {
18644 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18645
18646 /* A NULL abbrev means the end of a series of children. */
18647 if (abbrev == NULL)
18648 {
18649 if (--nesting_level == 0)
18650 return first_die;
18651
18652 info_ptr += bytes_read;
18653 last_die = parent_die;
18654 parent_die = parent_die->die_parent;
18655 continue;
18656 }
18657
18658 /* Check for template arguments. We never save these; if
18659 they're seen, we just mark the parent, and go on our way. */
18660 if (parent_die != NULL
18661 && cu->language == language_cplus
18662 && (abbrev->tag == DW_TAG_template_type_param
18663 || abbrev->tag == DW_TAG_template_value_param))
18664 {
18665 parent_die->has_template_arguments = 1;
18666
18667 if (!load_all)
18668 {
18669 /* We don't need a partial DIE for the template argument. */
18670 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18671 continue;
18672 }
18673 }
18674
18675 /* We only recurse into c++ subprograms looking for template arguments.
18676 Skip their other children. */
18677 if (!load_all
18678 && cu->language == language_cplus
18679 && parent_die != NULL
18680 && parent_die->tag == DW_TAG_subprogram)
18681 {
18682 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18683 continue;
18684 }
18685
18686 /* Check whether this DIE is interesting enough to save. Normally
18687 we would not be interested in members here, but there may be
18688 later variables referencing them via DW_AT_specification (for
18689 static members). */
18690 if (!load_all
18691 && !is_type_tag_for_partial (abbrev->tag)
18692 && abbrev->tag != DW_TAG_constant
18693 && abbrev->tag != DW_TAG_enumerator
18694 && abbrev->tag != DW_TAG_subprogram
18695 && abbrev->tag != DW_TAG_inlined_subroutine
18696 && abbrev->tag != DW_TAG_lexical_block
18697 && abbrev->tag != DW_TAG_variable
18698 && abbrev->tag != DW_TAG_namespace
18699 && abbrev->tag != DW_TAG_module
18700 && abbrev->tag != DW_TAG_member
18701 && abbrev->tag != DW_TAG_imported_unit
18702 && abbrev->tag != DW_TAG_imported_declaration)
18703 {
18704 /* Otherwise we skip to the next sibling, if any. */
18705 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18706 continue;
18707 }
18708
18709 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18710 abbrev);
18711
18712 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18713
18714 /* This two-pass algorithm for processing partial symbols has a
18715 high cost in cache pressure. Thus, handle some simple cases
18716 here which cover the majority of C partial symbols. DIEs
18717 which neither have specification tags in them, nor could have
18718 specification tags elsewhere pointing at them, can simply be
18719 processed and discarded.
18720
18721 This segment is also optional; scan_partial_symbols and
18722 add_partial_symbol will handle these DIEs if we chain
18723 them in normally. When compilers which do not emit large
18724 quantities of duplicate debug information are more common,
18725 this code can probably be removed. */
18726
18727 /* Any complete simple types at the top level (pretty much all
18728 of them, for a language without namespaces), can be processed
18729 directly. */
18730 if (parent_die == NULL
18731 && pdi.has_specification == 0
18732 && pdi.is_declaration == 0
18733 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18734 || pdi.tag == DW_TAG_base_type
18735 || pdi.tag == DW_TAG_subrange_type))
18736 {
18737 if (building_psymtab && pdi.name != NULL)
18738 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18739 VAR_DOMAIN, LOC_TYPEDEF,
18740 &objfile->static_psymbols,
18741 0, cu->language, objfile);
18742 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18743 continue;
18744 }
18745
18746 /* The exception for DW_TAG_typedef with has_children above is
18747 a workaround of GCC PR debug/47510. In the case of this complaint
18748 type_name_no_tag_or_error will error on such types later.
18749
18750 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18751 it could not find the child DIEs referenced later, this is checked
18752 above. In correct DWARF DW_TAG_typedef should have no children. */
18753
18754 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18755 complaint (&symfile_complaints,
18756 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18757 "- DIE at %s [in module %s]"),
18758 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18759
18760 /* If we're at the second level, and we're an enumerator, and
18761 our parent has no specification (meaning possibly lives in a
18762 namespace elsewhere), then we can add the partial symbol now
18763 instead of queueing it. */
18764 if (pdi.tag == DW_TAG_enumerator
18765 && parent_die != NULL
18766 && parent_die->die_parent == NULL
18767 && parent_die->tag == DW_TAG_enumeration_type
18768 && parent_die->has_specification == 0)
18769 {
18770 if (pdi.name == NULL)
18771 complaint (&symfile_complaints,
18772 _("malformed enumerator DIE ignored"));
18773 else if (building_psymtab)
18774 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18775 VAR_DOMAIN, LOC_CONST,
18776 cu->language == language_cplus
18777 ? &objfile->global_psymbols
18778 : &objfile->static_psymbols,
18779 0, cu->language, objfile);
18780
18781 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18782 continue;
18783 }
18784
18785 struct partial_die_info *part_die
18786 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18787
18788 /* We'll save this DIE so link it in. */
18789 part_die->die_parent = parent_die;
18790 part_die->die_sibling = NULL;
18791 part_die->die_child = NULL;
18792
18793 if (last_die && last_die == parent_die)
18794 last_die->die_child = part_die;
18795 else if (last_die)
18796 last_die->die_sibling = part_die;
18797
18798 last_die = part_die;
18799
18800 if (first_die == NULL)
18801 first_die = part_die;
18802
18803 /* Maybe add the DIE to the hash table. Not all DIEs that we
18804 find interesting need to be in the hash table, because we
18805 also have the parent/sibling/child chains; only those that we
18806 might refer to by offset later during partial symbol reading.
18807
18808 For now this means things that might have be the target of a
18809 DW_AT_specification, DW_AT_abstract_origin, or
18810 DW_AT_extension. DW_AT_extension will refer only to
18811 namespaces; DW_AT_abstract_origin refers to functions (and
18812 many things under the function DIE, but we do not recurse
18813 into function DIEs during partial symbol reading) and
18814 possibly variables as well; DW_AT_specification refers to
18815 declarations. Declarations ought to have the DW_AT_declaration
18816 flag. It happens that GCC forgets to put it in sometimes, but
18817 only for functions, not for types.
18818
18819 Adding more things than necessary to the hash table is harmless
18820 except for the performance cost. Adding too few will result in
18821 wasted time in find_partial_die, when we reread the compilation
18822 unit with load_all_dies set. */
18823
18824 if (load_all
18825 || abbrev->tag == DW_TAG_constant
18826 || abbrev->tag == DW_TAG_subprogram
18827 || abbrev->tag == DW_TAG_variable
18828 || abbrev->tag == DW_TAG_namespace
18829 || part_die->is_declaration)
18830 {
18831 void **slot;
18832
18833 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18834 to_underlying (part_die->sect_off),
18835 INSERT);
18836 *slot = part_die;
18837 }
18838
18839 /* For some DIEs we want to follow their children (if any). For C
18840 we have no reason to follow the children of structures; for other
18841 languages we have to, so that we can get at method physnames
18842 to infer fully qualified class names, for DW_AT_specification,
18843 and for C++ template arguments. For C++, we also look one level
18844 inside functions to find template arguments (if the name of the
18845 function does not already contain the template arguments).
18846
18847 For Ada, we need to scan the children of subprograms and lexical
18848 blocks as well because Ada allows the definition of nested
18849 entities that could be interesting for the debugger, such as
18850 nested subprograms for instance. */
18851 if (last_die->has_children
18852 && (load_all
18853 || last_die->tag == DW_TAG_namespace
18854 || last_die->tag == DW_TAG_module
18855 || last_die->tag == DW_TAG_enumeration_type
18856 || (cu->language == language_cplus
18857 && last_die->tag == DW_TAG_subprogram
18858 && (last_die->name == NULL
18859 || strchr (last_die->name, '<') == NULL))
18860 || (cu->language != language_c
18861 && (last_die->tag == DW_TAG_class_type
18862 || last_die->tag == DW_TAG_interface_type
18863 || last_die->tag == DW_TAG_structure_type
18864 || last_die->tag == DW_TAG_union_type))
18865 || (cu->language == language_ada
18866 && (last_die->tag == DW_TAG_subprogram
18867 || last_die->tag == DW_TAG_lexical_block))))
18868 {
18869 nesting_level++;
18870 parent_die = last_die;
18871 continue;
18872 }
18873
18874 /* Otherwise we skip to the next sibling, if any. */
18875 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18876
18877 /* Back to the top, do it again. */
18878 }
18879 }
18880
18881 partial_die_info::partial_die_info (sect_offset sect_off_,
18882 struct abbrev_info *abbrev)
18883 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18884 {
18885 }
18886
18887 /* Read a minimal amount of information into the minimal die structure.
18888 INFO_PTR should point just after the initial uleb128 of a DIE. */
18889
18890 const gdb_byte *
18891 partial_die_info::read (const struct die_reader_specs *reader,
18892 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18893 {
18894 struct dwarf2_cu *cu = reader->cu;
18895 struct dwarf2_per_objfile *dwarf2_per_objfile
18896 = cu->per_cu->dwarf2_per_objfile;
18897 unsigned int i;
18898 int has_low_pc_attr = 0;
18899 int has_high_pc_attr = 0;
18900 int high_pc_relative = 0;
18901
18902 for (i = 0; i < abbrev.num_attrs; ++i)
18903 {
18904 struct attribute attr;
18905
18906 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18907
18908 /* Store the data if it is of an attribute we want to keep in a
18909 partial symbol table. */
18910 switch (attr.name)
18911 {
18912 case DW_AT_name:
18913 switch (tag)
18914 {
18915 case DW_TAG_compile_unit:
18916 case DW_TAG_partial_unit:
18917 case DW_TAG_type_unit:
18918 /* Compilation units have a DW_AT_name that is a filename, not
18919 a source language identifier. */
18920 case DW_TAG_enumeration_type:
18921 case DW_TAG_enumerator:
18922 /* These tags always have simple identifiers already; no need
18923 to canonicalize them. */
18924 name = DW_STRING (&attr);
18925 break;
18926 default:
18927 {
18928 struct objfile *objfile = dwarf2_per_objfile->objfile;
18929
18930 name
18931 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18932 &objfile->per_bfd->storage_obstack);
18933 }
18934 break;
18935 }
18936 break;
18937 case DW_AT_linkage_name:
18938 case DW_AT_MIPS_linkage_name:
18939 /* Note that both forms of linkage name might appear. We
18940 assume they will be the same, and we only store the last
18941 one we see. */
18942 if (cu->language == language_ada)
18943 name = DW_STRING (&attr);
18944 linkage_name = DW_STRING (&attr);
18945 break;
18946 case DW_AT_low_pc:
18947 has_low_pc_attr = 1;
18948 lowpc = attr_value_as_address (&attr);
18949 break;
18950 case DW_AT_high_pc:
18951 has_high_pc_attr = 1;
18952 highpc = attr_value_as_address (&attr);
18953 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18954 high_pc_relative = 1;
18955 break;
18956 case DW_AT_location:
18957 /* Support the .debug_loc offsets. */
18958 if (attr_form_is_block (&attr))
18959 {
18960 d.locdesc = DW_BLOCK (&attr);
18961 }
18962 else if (attr_form_is_section_offset (&attr))
18963 {
18964 dwarf2_complex_location_expr_complaint ();
18965 }
18966 else
18967 {
18968 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18969 "partial symbol information");
18970 }
18971 break;
18972 case DW_AT_external:
18973 is_external = DW_UNSND (&attr);
18974 break;
18975 case DW_AT_declaration:
18976 is_declaration = DW_UNSND (&attr);
18977 break;
18978 case DW_AT_type:
18979 has_type = 1;
18980 break;
18981 case DW_AT_abstract_origin:
18982 case DW_AT_specification:
18983 case DW_AT_extension:
18984 has_specification = 1;
18985 spec_offset = dwarf2_get_ref_die_offset (&attr);
18986 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18987 || cu->per_cu->is_dwz);
18988 break;
18989 case DW_AT_sibling:
18990 /* Ignore absolute siblings, they might point outside of
18991 the current compile unit. */
18992 if (attr.form == DW_FORM_ref_addr)
18993 complaint (&symfile_complaints,
18994 _("ignoring absolute DW_AT_sibling"));
18995 else
18996 {
18997 const gdb_byte *buffer = reader->buffer;
18998 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18999 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19000
19001 if (sibling_ptr < info_ptr)
19002 complaint (&symfile_complaints,
19003 _("DW_AT_sibling points backwards"));
19004 else if (sibling_ptr > reader->buffer_end)
19005 dwarf2_section_buffer_overflow_complaint (reader->die_section);
19006 else
19007 sibling = sibling_ptr;
19008 }
19009 break;
19010 case DW_AT_byte_size:
19011 has_byte_size = 1;
19012 break;
19013 case DW_AT_const_value:
19014 has_const_value = 1;
19015 break;
19016 case DW_AT_calling_convention:
19017 /* DWARF doesn't provide a way to identify a program's source-level
19018 entry point. DW_AT_calling_convention attributes are only meant
19019 to describe functions' calling conventions.
19020
19021 However, because it's a necessary piece of information in
19022 Fortran, and before DWARF 4 DW_CC_program was the only
19023 piece of debugging information whose definition refers to
19024 a 'main program' at all, several compilers marked Fortran
19025 main programs with DW_CC_program --- even when those
19026 functions use the standard calling conventions.
19027
19028 Although DWARF now specifies a way to provide this
19029 information, we support this practice for backward
19030 compatibility. */
19031 if (DW_UNSND (&attr) == DW_CC_program
19032 && cu->language == language_fortran)
19033 main_subprogram = 1;
19034 break;
19035 case DW_AT_inline:
19036 if (DW_UNSND (&attr) == DW_INL_inlined
19037 || DW_UNSND (&attr) == DW_INL_declared_inlined)
19038 may_be_inlined = 1;
19039 break;
19040
19041 case DW_AT_import:
19042 if (tag == DW_TAG_imported_unit)
19043 {
19044 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19045 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19046 || cu->per_cu->is_dwz);
19047 }
19048 break;
19049
19050 case DW_AT_main_subprogram:
19051 main_subprogram = DW_UNSND (&attr);
19052 break;
19053
19054 default:
19055 break;
19056 }
19057 }
19058
19059 if (high_pc_relative)
19060 highpc += lowpc;
19061
19062 if (has_low_pc_attr && has_high_pc_attr)
19063 {
19064 /* When using the GNU linker, .gnu.linkonce. sections are used to
19065 eliminate duplicate copies of functions and vtables and such.
19066 The linker will arbitrarily choose one and discard the others.
19067 The AT_*_pc values for such functions refer to local labels in
19068 these sections. If the section from that file was discarded, the
19069 labels are not in the output, so the relocs get a value of 0.
19070 If this is a discarded function, mark the pc bounds as invalid,
19071 so that GDB will ignore it. */
19072 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
19073 {
19074 struct objfile *objfile = dwarf2_per_objfile->objfile;
19075 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19076
19077 complaint (&symfile_complaints,
19078 _("DW_AT_low_pc %s is zero "
19079 "for DIE at %s [in module %s]"),
19080 paddress (gdbarch, lowpc),
19081 sect_offset_str (sect_off),
19082 objfile_name (objfile));
19083 }
19084 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19085 else if (lowpc >= highpc)
19086 {
19087 struct objfile *objfile = dwarf2_per_objfile->objfile;
19088 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19089
19090 complaint (&symfile_complaints,
19091 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19092 "for DIE at %s [in module %s]"),
19093 paddress (gdbarch, lowpc),
19094 paddress (gdbarch, highpc),
19095 sect_offset_str (sect_off),
19096 objfile_name (objfile));
19097 }
19098 else
19099 has_pc_info = 1;
19100 }
19101
19102 return info_ptr;
19103 }
19104
19105 /* Find a cached partial DIE at OFFSET in CU. */
19106
19107 struct partial_die_info *
19108 dwarf2_cu::find_partial_die (sect_offset sect_off)
19109 {
19110 struct partial_die_info *lookup_die = NULL;
19111 struct partial_die_info part_die (sect_off);
19112
19113 lookup_die = ((struct partial_die_info *)
19114 htab_find_with_hash (partial_dies, &part_die,
19115 to_underlying (sect_off)));
19116
19117 return lookup_die;
19118 }
19119
19120 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19121 except in the case of .debug_types DIEs which do not reference
19122 outside their CU (they do however referencing other types via
19123 DW_FORM_ref_sig8). */
19124
19125 static struct partial_die_info *
19126 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19127 {
19128 struct dwarf2_per_objfile *dwarf2_per_objfile
19129 = cu->per_cu->dwarf2_per_objfile;
19130 struct objfile *objfile = dwarf2_per_objfile->objfile;
19131 struct dwarf2_per_cu_data *per_cu = NULL;
19132 struct partial_die_info *pd = NULL;
19133
19134 if (offset_in_dwz == cu->per_cu->is_dwz
19135 && offset_in_cu_p (&cu->header, sect_off))
19136 {
19137 pd = cu->find_partial_die (sect_off);
19138 if (pd != NULL)
19139 return pd;
19140 /* We missed recording what we needed.
19141 Load all dies and try again. */
19142 per_cu = cu->per_cu;
19143 }
19144 else
19145 {
19146 /* TUs don't reference other CUs/TUs (except via type signatures). */
19147 if (cu->per_cu->is_debug_types)
19148 {
19149 error (_("Dwarf Error: Type Unit at offset %s contains"
19150 " external reference to offset %s [in module %s].\n"),
19151 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19152 bfd_get_filename (objfile->obfd));
19153 }
19154 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19155 dwarf2_per_objfile);
19156
19157 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19158 load_partial_comp_unit (per_cu);
19159
19160 per_cu->cu->last_used = 0;
19161 pd = per_cu->cu->find_partial_die (sect_off);
19162 }
19163
19164 /* If we didn't find it, and not all dies have been loaded,
19165 load them all and try again. */
19166
19167 if (pd == NULL && per_cu->load_all_dies == 0)
19168 {
19169 per_cu->load_all_dies = 1;
19170
19171 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19172 THIS_CU->cu may already be in use. So we can't just free it and
19173 replace its DIEs with the ones we read in. Instead, we leave those
19174 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19175 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19176 set. */
19177 load_partial_comp_unit (per_cu);
19178
19179 pd = per_cu->cu->find_partial_die (sect_off);
19180 }
19181
19182 if (pd == NULL)
19183 internal_error (__FILE__, __LINE__,
19184 _("could not find partial DIE %s "
19185 "in cache [from module %s]\n"),
19186 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19187 return pd;
19188 }
19189
19190 /* See if we can figure out if the class lives in a namespace. We do
19191 this by looking for a member function; its demangled name will
19192 contain namespace info, if there is any. */
19193
19194 static void
19195 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19196 struct dwarf2_cu *cu)
19197 {
19198 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19199 what template types look like, because the demangler
19200 frequently doesn't give the same name as the debug info. We
19201 could fix this by only using the demangled name to get the
19202 prefix (but see comment in read_structure_type). */
19203
19204 struct partial_die_info *real_pdi;
19205 struct partial_die_info *child_pdi;
19206
19207 /* If this DIE (this DIE's specification, if any) has a parent, then
19208 we should not do this. We'll prepend the parent's fully qualified
19209 name when we create the partial symbol. */
19210
19211 real_pdi = struct_pdi;
19212 while (real_pdi->has_specification)
19213 real_pdi = find_partial_die (real_pdi->spec_offset,
19214 real_pdi->spec_is_dwz, cu);
19215
19216 if (real_pdi->die_parent != NULL)
19217 return;
19218
19219 for (child_pdi = struct_pdi->die_child;
19220 child_pdi != NULL;
19221 child_pdi = child_pdi->die_sibling)
19222 {
19223 if (child_pdi->tag == DW_TAG_subprogram
19224 && child_pdi->linkage_name != NULL)
19225 {
19226 char *actual_class_name
19227 = language_class_name_from_physname (cu->language_defn,
19228 child_pdi->linkage_name);
19229 if (actual_class_name != NULL)
19230 {
19231 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19232 struct_pdi->name
19233 = ((const char *)
19234 obstack_copy0 (&objfile->per_bfd->storage_obstack,
19235 actual_class_name,
19236 strlen (actual_class_name)));
19237 xfree (actual_class_name);
19238 }
19239 break;
19240 }
19241 }
19242 }
19243
19244 void
19245 partial_die_info::fixup (struct dwarf2_cu *cu)
19246 {
19247 /* Once we've fixed up a die, there's no point in doing so again.
19248 This also avoids a memory leak if we were to call
19249 guess_partial_die_structure_name multiple times. */
19250 if (fixup_called)
19251 return;
19252
19253 /* If we found a reference attribute and the DIE has no name, try
19254 to find a name in the referred to DIE. */
19255
19256 if (name == NULL && has_specification)
19257 {
19258 struct partial_die_info *spec_die;
19259
19260 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
19261
19262 spec_die->fixup (cu);
19263
19264 if (spec_die->name)
19265 {
19266 name = spec_die->name;
19267
19268 /* Copy DW_AT_external attribute if it is set. */
19269 if (spec_die->is_external)
19270 is_external = spec_die->is_external;
19271 }
19272 }
19273
19274 /* Set default names for some unnamed DIEs. */
19275
19276 if (name == NULL && tag == DW_TAG_namespace)
19277 name = CP_ANONYMOUS_NAMESPACE_STR;
19278
19279 /* If there is no parent die to provide a namespace, and there are
19280 children, see if we can determine the namespace from their linkage
19281 name. */
19282 if (cu->language == language_cplus
19283 && !VEC_empty (dwarf2_section_info_def,
19284 cu->per_cu->dwarf2_per_objfile->types)
19285 && die_parent == NULL
19286 && has_children
19287 && (tag == DW_TAG_class_type
19288 || tag == DW_TAG_structure_type
19289 || tag == DW_TAG_union_type))
19290 guess_partial_die_structure_name (this, cu);
19291
19292 /* GCC might emit a nameless struct or union that has a linkage
19293 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19294 if (name == NULL
19295 && (tag == DW_TAG_class_type
19296 || tag == DW_TAG_interface_type
19297 || tag == DW_TAG_structure_type
19298 || tag == DW_TAG_union_type)
19299 && linkage_name != NULL)
19300 {
19301 char *demangled;
19302
19303 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
19304 if (demangled)
19305 {
19306 const char *base;
19307
19308 /* Strip any leading namespaces/classes, keep only the base name.
19309 DW_AT_name for named DIEs does not contain the prefixes. */
19310 base = strrchr (demangled, ':');
19311 if (base && base > demangled && base[-1] == ':')
19312 base++;
19313 else
19314 base = demangled;
19315
19316 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19317 name
19318 = ((const char *)
19319 obstack_copy0 (&objfile->per_bfd->storage_obstack,
19320 base, strlen (base)));
19321 xfree (demangled);
19322 }
19323 }
19324
19325 fixup_called = 1;
19326 }
19327
19328 /* Read an attribute value described by an attribute form. */
19329
19330 static const gdb_byte *
19331 read_attribute_value (const struct die_reader_specs *reader,
19332 struct attribute *attr, unsigned form,
19333 LONGEST implicit_const, const gdb_byte *info_ptr)
19334 {
19335 struct dwarf2_cu *cu = reader->cu;
19336 struct dwarf2_per_objfile *dwarf2_per_objfile
19337 = cu->per_cu->dwarf2_per_objfile;
19338 struct objfile *objfile = dwarf2_per_objfile->objfile;
19339 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19340 bfd *abfd = reader->abfd;
19341 struct comp_unit_head *cu_header = &cu->header;
19342 unsigned int bytes_read;
19343 struct dwarf_block *blk;
19344
19345 attr->form = (enum dwarf_form) form;
19346 switch (form)
19347 {
19348 case DW_FORM_ref_addr:
19349 if (cu->header.version == 2)
19350 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19351 else
19352 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19353 &cu->header, &bytes_read);
19354 info_ptr += bytes_read;
19355 break;
19356 case DW_FORM_GNU_ref_alt:
19357 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19358 info_ptr += bytes_read;
19359 break;
19360 case DW_FORM_addr:
19361 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19362 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19363 info_ptr += bytes_read;
19364 break;
19365 case DW_FORM_block2:
19366 blk = dwarf_alloc_block (cu);
19367 blk->size = read_2_bytes (abfd, info_ptr);
19368 info_ptr += 2;
19369 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19370 info_ptr += blk->size;
19371 DW_BLOCK (attr) = blk;
19372 break;
19373 case DW_FORM_block4:
19374 blk = dwarf_alloc_block (cu);
19375 blk->size = read_4_bytes (abfd, info_ptr);
19376 info_ptr += 4;
19377 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19378 info_ptr += blk->size;
19379 DW_BLOCK (attr) = blk;
19380 break;
19381 case DW_FORM_data2:
19382 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19383 info_ptr += 2;
19384 break;
19385 case DW_FORM_data4:
19386 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19387 info_ptr += 4;
19388 break;
19389 case DW_FORM_data8:
19390 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19391 info_ptr += 8;
19392 break;
19393 case DW_FORM_data16:
19394 blk = dwarf_alloc_block (cu);
19395 blk->size = 16;
19396 blk->data = read_n_bytes (abfd, info_ptr, 16);
19397 info_ptr += 16;
19398 DW_BLOCK (attr) = blk;
19399 break;
19400 case DW_FORM_sec_offset:
19401 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19402 info_ptr += bytes_read;
19403 break;
19404 case DW_FORM_string:
19405 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19406 DW_STRING_IS_CANONICAL (attr) = 0;
19407 info_ptr += bytes_read;
19408 break;
19409 case DW_FORM_strp:
19410 if (!cu->per_cu->is_dwz)
19411 {
19412 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19413 abfd, info_ptr, cu_header,
19414 &bytes_read);
19415 DW_STRING_IS_CANONICAL (attr) = 0;
19416 info_ptr += bytes_read;
19417 break;
19418 }
19419 /* FALLTHROUGH */
19420 case DW_FORM_line_strp:
19421 if (!cu->per_cu->is_dwz)
19422 {
19423 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19424 abfd, info_ptr,
19425 cu_header, &bytes_read);
19426 DW_STRING_IS_CANONICAL (attr) = 0;
19427 info_ptr += bytes_read;
19428 break;
19429 }
19430 /* FALLTHROUGH */
19431 case DW_FORM_GNU_strp_alt:
19432 {
19433 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19434 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19435 &bytes_read);
19436
19437 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19438 dwz, str_offset);
19439 DW_STRING_IS_CANONICAL (attr) = 0;
19440 info_ptr += bytes_read;
19441 }
19442 break;
19443 case DW_FORM_exprloc:
19444 case DW_FORM_block:
19445 blk = dwarf_alloc_block (cu);
19446 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19447 info_ptr += bytes_read;
19448 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19449 info_ptr += blk->size;
19450 DW_BLOCK (attr) = blk;
19451 break;
19452 case DW_FORM_block1:
19453 blk = dwarf_alloc_block (cu);
19454 blk->size = read_1_byte (abfd, info_ptr);
19455 info_ptr += 1;
19456 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19457 info_ptr += blk->size;
19458 DW_BLOCK (attr) = blk;
19459 break;
19460 case DW_FORM_data1:
19461 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19462 info_ptr += 1;
19463 break;
19464 case DW_FORM_flag:
19465 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19466 info_ptr += 1;
19467 break;
19468 case DW_FORM_flag_present:
19469 DW_UNSND (attr) = 1;
19470 break;
19471 case DW_FORM_sdata:
19472 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19473 info_ptr += bytes_read;
19474 break;
19475 case DW_FORM_udata:
19476 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19477 info_ptr += bytes_read;
19478 break;
19479 case DW_FORM_ref1:
19480 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19481 + read_1_byte (abfd, info_ptr));
19482 info_ptr += 1;
19483 break;
19484 case DW_FORM_ref2:
19485 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19486 + read_2_bytes (abfd, info_ptr));
19487 info_ptr += 2;
19488 break;
19489 case DW_FORM_ref4:
19490 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19491 + read_4_bytes (abfd, info_ptr));
19492 info_ptr += 4;
19493 break;
19494 case DW_FORM_ref8:
19495 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19496 + read_8_bytes (abfd, info_ptr));
19497 info_ptr += 8;
19498 break;
19499 case DW_FORM_ref_sig8:
19500 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19501 info_ptr += 8;
19502 break;
19503 case DW_FORM_ref_udata:
19504 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19505 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19506 info_ptr += bytes_read;
19507 break;
19508 case DW_FORM_indirect:
19509 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19510 info_ptr += bytes_read;
19511 if (form == DW_FORM_implicit_const)
19512 {
19513 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19514 info_ptr += bytes_read;
19515 }
19516 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19517 info_ptr);
19518 break;
19519 case DW_FORM_implicit_const:
19520 DW_SND (attr) = implicit_const;
19521 break;
19522 case DW_FORM_GNU_addr_index:
19523 if (reader->dwo_file == NULL)
19524 {
19525 /* For now flag a hard error.
19526 Later we can turn this into a complaint. */
19527 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19528 dwarf_form_name (form),
19529 bfd_get_filename (abfd));
19530 }
19531 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19532 info_ptr += bytes_read;
19533 break;
19534 case DW_FORM_GNU_str_index:
19535 if (reader->dwo_file == NULL)
19536 {
19537 /* For now flag a hard error.
19538 Later we can turn this into a complaint if warranted. */
19539 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19540 dwarf_form_name (form),
19541 bfd_get_filename (abfd));
19542 }
19543 {
19544 ULONGEST str_index =
19545 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19546
19547 DW_STRING (attr) = read_str_index (reader, str_index);
19548 DW_STRING_IS_CANONICAL (attr) = 0;
19549 info_ptr += bytes_read;
19550 }
19551 break;
19552 default:
19553 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19554 dwarf_form_name (form),
19555 bfd_get_filename (abfd));
19556 }
19557
19558 /* Super hack. */
19559 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19560 attr->form = DW_FORM_GNU_ref_alt;
19561
19562 /* We have seen instances where the compiler tried to emit a byte
19563 size attribute of -1 which ended up being encoded as an unsigned
19564 0xffffffff. Although 0xffffffff is technically a valid size value,
19565 an object of this size seems pretty unlikely so we can relatively
19566 safely treat these cases as if the size attribute was invalid and
19567 treat them as zero by default. */
19568 if (attr->name == DW_AT_byte_size
19569 && form == DW_FORM_data4
19570 && DW_UNSND (attr) >= 0xffffffff)
19571 {
19572 complaint
19573 (&symfile_complaints,
19574 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19575 hex_string (DW_UNSND (attr)));
19576 DW_UNSND (attr) = 0;
19577 }
19578
19579 return info_ptr;
19580 }
19581
19582 /* Read an attribute described by an abbreviated attribute. */
19583
19584 static const gdb_byte *
19585 read_attribute (const struct die_reader_specs *reader,
19586 struct attribute *attr, struct attr_abbrev *abbrev,
19587 const gdb_byte *info_ptr)
19588 {
19589 attr->name = abbrev->name;
19590 return read_attribute_value (reader, attr, abbrev->form,
19591 abbrev->implicit_const, info_ptr);
19592 }
19593
19594 /* Read dwarf information from a buffer. */
19595
19596 static unsigned int
19597 read_1_byte (bfd *abfd, const gdb_byte *buf)
19598 {
19599 return bfd_get_8 (abfd, buf);
19600 }
19601
19602 static int
19603 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19604 {
19605 return bfd_get_signed_8 (abfd, buf);
19606 }
19607
19608 static unsigned int
19609 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19610 {
19611 return bfd_get_16 (abfd, buf);
19612 }
19613
19614 static int
19615 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19616 {
19617 return bfd_get_signed_16 (abfd, buf);
19618 }
19619
19620 static unsigned int
19621 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19622 {
19623 return bfd_get_32 (abfd, buf);
19624 }
19625
19626 static int
19627 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19628 {
19629 return bfd_get_signed_32 (abfd, buf);
19630 }
19631
19632 static ULONGEST
19633 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19634 {
19635 return bfd_get_64 (abfd, buf);
19636 }
19637
19638 static CORE_ADDR
19639 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19640 unsigned int *bytes_read)
19641 {
19642 struct comp_unit_head *cu_header = &cu->header;
19643 CORE_ADDR retval = 0;
19644
19645 if (cu_header->signed_addr_p)
19646 {
19647 switch (cu_header->addr_size)
19648 {
19649 case 2:
19650 retval = bfd_get_signed_16 (abfd, buf);
19651 break;
19652 case 4:
19653 retval = bfd_get_signed_32 (abfd, buf);
19654 break;
19655 case 8:
19656 retval = bfd_get_signed_64 (abfd, buf);
19657 break;
19658 default:
19659 internal_error (__FILE__, __LINE__,
19660 _("read_address: bad switch, signed [in module %s]"),
19661 bfd_get_filename (abfd));
19662 }
19663 }
19664 else
19665 {
19666 switch (cu_header->addr_size)
19667 {
19668 case 2:
19669 retval = bfd_get_16 (abfd, buf);
19670 break;
19671 case 4:
19672 retval = bfd_get_32 (abfd, buf);
19673 break;
19674 case 8:
19675 retval = bfd_get_64 (abfd, buf);
19676 break;
19677 default:
19678 internal_error (__FILE__, __LINE__,
19679 _("read_address: bad switch, "
19680 "unsigned [in module %s]"),
19681 bfd_get_filename (abfd));
19682 }
19683 }
19684
19685 *bytes_read = cu_header->addr_size;
19686 return retval;
19687 }
19688
19689 /* Read the initial length from a section. The (draft) DWARF 3
19690 specification allows the initial length to take up either 4 bytes
19691 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19692 bytes describe the length and all offsets will be 8 bytes in length
19693 instead of 4.
19694
19695 An older, non-standard 64-bit format is also handled by this
19696 function. The older format in question stores the initial length
19697 as an 8-byte quantity without an escape value. Lengths greater
19698 than 2^32 aren't very common which means that the initial 4 bytes
19699 is almost always zero. Since a length value of zero doesn't make
19700 sense for the 32-bit format, this initial zero can be considered to
19701 be an escape value which indicates the presence of the older 64-bit
19702 format. As written, the code can't detect (old format) lengths
19703 greater than 4GB. If it becomes necessary to handle lengths
19704 somewhat larger than 4GB, we could allow other small values (such
19705 as the non-sensical values of 1, 2, and 3) to also be used as
19706 escape values indicating the presence of the old format.
19707
19708 The value returned via bytes_read should be used to increment the
19709 relevant pointer after calling read_initial_length().
19710
19711 [ Note: read_initial_length() and read_offset() are based on the
19712 document entitled "DWARF Debugging Information Format", revision
19713 3, draft 8, dated November 19, 2001. This document was obtained
19714 from:
19715
19716 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19717
19718 This document is only a draft and is subject to change. (So beware.)
19719
19720 Details regarding the older, non-standard 64-bit format were
19721 determined empirically by examining 64-bit ELF files produced by
19722 the SGI toolchain on an IRIX 6.5 machine.
19723
19724 - Kevin, July 16, 2002
19725 ] */
19726
19727 static LONGEST
19728 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19729 {
19730 LONGEST length = bfd_get_32 (abfd, buf);
19731
19732 if (length == 0xffffffff)
19733 {
19734 length = bfd_get_64 (abfd, buf + 4);
19735 *bytes_read = 12;
19736 }
19737 else if (length == 0)
19738 {
19739 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19740 length = bfd_get_64 (abfd, buf);
19741 *bytes_read = 8;
19742 }
19743 else
19744 {
19745 *bytes_read = 4;
19746 }
19747
19748 return length;
19749 }
19750
19751 /* Cover function for read_initial_length.
19752 Returns the length of the object at BUF, and stores the size of the
19753 initial length in *BYTES_READ and stores the size that offsets will be in
19754 *OFFSET_SIZE.
19755 If the initial length size is not equivalent to that specified in
19756 CU_HEADER then issue a complaint.
19757 This is useful when reading non-comp-unit headers. */
19758
19759 static LONGEST
19760 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19761 const struct comp_unit_head *cu_header,
19762 unsigned int *bytes_read,
19763 unsigned int *offset_size)
19764 {
19765 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19766
19767 gdb_assert (cu_header->initial_length_size == 4
19768 || cu_header->initial_length_size == 8
19769 || cu_header->initial_length_size == 12);
19770
19771 if (cu_header->initial_length_size != *bytes_read)
19772 complaint (&symfile_complaints,
19773 _("intermixed 32-bit and 64-bit DWARF sections"));
19774
19775 *offset_size = (*bytes_read == 4) ? 4 : 8;
19776 return length;
19777 }
19778
19779 /* Read an offset from the data stream. The size of the offset is
19780 given by cu_header->offset_size. */
19781
19782 static LONGEST
19783 read_offset (bfd *abfd, const gdb_byte *buf,
19784 const struct comp_unit_head *cu_header,
19785 unsigned int *bytes_read)
19786 {
19787 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19788
19789 *bytes_read = cu_header->offset_size;
19790 return offset;
19791 }
19792
19793 /* Read an offset from the data stream. */
19794
19795 static LONGEST
19796 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19797 {
19798 LONGEST retval = 0;
19799
19800 switch (offset_size)
19801 {
19802 case 4:
19803 retval = bfd_get_32 (abfd, buf);
19804 break;
19805 case 8:
19806 retval = bfd_get_64 (abfd, buf);
19807 break;
19808 default:
19809 internal_error (__FILE__, __LINE__,
19810 _("read_offset_1: bad switch [in module %s]"),
19811 bfd_get_filename (abfd));
19812 }
19813
19814 return retval;
19815 }
19816
19817 static const gdb_byte *
19818 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19819 {
19820 /* If the size of a host char is 8 bits, we can return a pointer
19821 to the buffer, otherwise we have to copy the data to a buffer
19822 allocated on the temporary obstack. */
19823 gdb_assert (HOST_CHAR_BIT == 8);
19824 return buf;
19825 }
19826
19827 static const char *
19828 read_direct_string (bfd *abfd, const gdb_byte *buf,
19829 unsigned int *bytes_read_ptr)
19830 {
19831 /* If the size of a host char is 8 bits, we can return a pointer
19832 to the string, otherwise we have to copy the string to a buffer
19833 allocated on the temporary obstack. */
19834 gdb_assert (HOST_CHAR_BIT == 8);
19835 if (*buf == '\0')
19836 {
19837 *bytes_read_ptr = 1;
19838 return NULL;
19839 }
19840 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19841 return (const char *) buf;
19842 }
19843
19844 /* Return pointer to string at section SECT offset STR_OFFSET with error
19845 reporting strings FORM_NAME and SECT_NAME. */
19846
19847 static const char *
19848 read_indirect_string_at_offset_from (struct objfile *objfile,
19849 bfd *abfd, LONGEST str_offset,
19850 struct dwarf2_section_info *sect,
19851 const char *form_name,
19852 const char *sect_name)
19853 {
19854 dwarf2_read_section (objfile, sect);
19855 if (sect->buffer == NULL)
19856 error (_("%s used without %s section [in module %s]"),
19857 form_name, sect_name, bfd_get_filename (abfd));
19858 if (str_offset >= sect->size)
19859 error (_("%s pointing outside of %s section [in module %s]"),
19860 form_name, sect_name, bfd_get_filename (abfd));
19861 gdb_assert (HOST_CHAR_BIT == 8);
19862 if (sect->buffer[str_offset] == '\0')
19863 return NULL;
19864 return (const char *) (sect->buffer + str_offset);
19865 }
19866
19867 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19868
19869 static const char *
19870 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19871 bfd *abfd, LONGEST str_offset)
19872 {
19873 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19874 abfd, str_offset,
19875 &dwarf2_per_objfile->str,
19876 "DW_FORM_strp", ".debug_str");
19877 }
19878
19879 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19880
19881 static const char *
19882 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19883 bfd *abfd, LONGEST str_offset)
19884 {
19885 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19886 abfd, str_offset,
19887 &dwarf2_per_objfile->line_str,
19888 "DW_FORM_line_strp",
19889 ".debug_line_str");
19890 }
19891
19892 /* Read a string at offset STR_OFFSET in the .debug_str section from
19893 the .dwz file DWZ. Throw an error if the offset is too large. If
19894 the string consists of a single NUL byte, return NULL; otherwise
19895 return a pointer to the string. */
19896
19897 static const char *
19898 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19899 LONGEST str_offset)
19900 {
19901 dwarf2_read_section (objfile, &dwz->str);
19902
19903 if (dwz->str.buffer == NULL)
19904 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19905 "section [in module %s]"),
19906 bfd_get_filename (dwz->dwz_bfd));
19907 if (str_offset >= dwz->str.size)
19908 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19909 ".debug_str section [in module %s]"),
19910 bfd_get_filename (dwz->dwz_bfd));
19911 gdb_assert (HOST_CHAR_BIT == 8);
19912 if (dwz->str.buffer[str_offset] == '\0')
19913 return NULL;
19914 return (const char *) (dwz->str.buffer + str_offset);
19915 }
19916
19917 /* Return pointer to string at .debug_str offset as read from BUF.
19918 BUF is assumed to be in a compilation unit described by CU_HEADER.
19919 Return *BYTES_READ_PTR count of bytes read from BUF. */
19920
19921 static const char *
19922 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19923 const gdb_byte *buf,
19924 const struct comp_unit_head *cu_header,
19925 unsigned int *bytes_read_ptr)
19926 {
19927 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19928
19929 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19930 }
19931
19932 /* Return pointer to string at .debug_line_str offset as read from BUF.
19933 BUF is assumed to be in a compilation unit described by CU_HEADER.
19934 Return *BYTES_READ_PTR count of bytes read from BUF. */
19935
19936 static const char *
19937 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19938 bfd *abfd, const gdb_byte *buf,
19939 const struct comp_unit_head *cu_header,
19940 unsigned int *bytes_read_ptr)
19941 {
19942 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19943
19944 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19945 str_offset);
19946 }
19947
19948 ULONGEST
19949 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19950 unsigned int *bytes_read_ptr)
19951 {
19952 ULONGEST result;
19953 unsigned int num_read;
19954 int shift;
19955 unsigned char byte;
19956
19957 result = 0;
19958 shift = 0;
19959 num_read = 0;
19960 while (1)
19961 {
19962 byte = bfd_get_8 (abfd, buf);
19963 buf++;
19964 num_read++;
19965 result |= ((ULONGEST) (byte & 127) << shift);
19966 if ((byte & 128) == 0)
19967 {
19968 break;
19969 }
19970 shift += 7;
19971 }
19972 *bytes_read_ptr = num_read;
19973 return result;
19974 }
19975
19976 static LONGEST
19977 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19978 unsigned int *bytes_read_ptr)
19979 {
19980 LONGEST result;
19981 int shift, num_read;
19982 unsigned char byte;
19983
19984 result = 0;
19985 shift = 0;
19986 num_read = 0;
19987 while (1)
19988 {
19989 byte = bfd_get_8 (abfd, buf);
19990 buf++;
19991 num_read++;
19992 result |= ((LONGEST) (byte & 127) << shift);
19993 shift += 7;
19994 if ((byte & 128) == 0)
19995 {
19996 break;
19997 }
19998 }
19999 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
20000 result |= -(((LONGEST) 1) << shift);
20001 *bytes_read_ptr = num_read;
20002 return result;
20003 }
20004
20005 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20006 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
20007 ADDR_SIZE is the size of addresses from the CU header. */
20008
20009 static CORE_ADDR
20010 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20011 unsigned int addr_index, ULONGEST addr_base, int addr_size)
20012 {
20013 struct objfile *objfile = dwarf2_per_objfile->objfile;
20014 bfd *abfd = objfile->obfd;
20015 const gdb_byte *info_ptr;
20016
20017 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20018 if (dwarf2_per_objfile->addr.buffer == NULL)
20019 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20020 objfile_name (objfile));
20021 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
20022 error (_("DW_FORM_addr_index pointing outside of "
20023 ".debug_addr section [in module %s]"),
20024 objfile_name (objfile));
20025 info_ptr = (dwarf2_per_objfile->addr.buffer
20026 + addr_base + addr_index * addr_size);
20027 if (addr_size == 4)
20028 return bfd_get_32 (abfd, info_ptr);
20029 else
20030 return bfd_get_64 (abfd, info_ptr);
20031 }
20032
20033 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20034
20035 static CORE_ADDR
20036 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20037 {
20038 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20039 cu->addr_base, cu->header.addr_size);
20040 }
20041
20042 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20043
20044 static CORE_ADDR
20045 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20046 unsigned int *bytes_read)
20047 {
20048 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
20049 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20050
20051 return read_addr_index (cu, addr_index);
20052 }
20053
20054 /* Data structure to pass results from dwarf2_read_addr_index_reader
20055 back to dwarf2_read_addr_index. */
20056
20057 struct dwarf2_read_addr_index_data
20058 {
20059 ULONGEST addr_base;
20060 int addr_size;
20061 };
20062
20063 /* die_reader_func for dwarf2_read_addr_index. */
20064
20065 static void
20066 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
20067 const gdb_byte *info_ptr,
20068 struct die_info *comp_unit_die,
20069 int has_children,
20070 void *data)
20071 {
20072 struct dwarf2_cu *cu = reader->cu;
20073 struct dwarf2_read_addr_index_data *aidata =
20074 (struct dwarf2_read_addr_index_data *) data;
20075
20076 aidata->addr_base = cu->addr_base;
20077 aidata->addr_size = cu->header.addr_size;
20078 }
20079
20080 /* Given an index in .debug_addr, fetch the value.
20081 NOTE: This can be called during dwarf expression evaluation,
20082 long after the debug information has been read, and thus per_cu->cu
20083 may no longer exist. */
20084
20085 CORE_ADDR
20086 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20087 unsigned int addr_index)
20088 {
20089 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
20090 struct objfile *objfile = dwarf2_per_objfile->objfile;
20091 struct dwarf2_cu *cu = per_cu->cu;
20092 ULONGEST addr_base;
20093 int addr_size;
20094
20095 /* We need addr_base and addr_size.
20096 If we don't have PER_CU->cu, we have to get it.
20097 Nasty, but the alternative is storing the needed info in PER_CU,
20098 which at this point doesn't seem justified: it's not clear how frequently
20099 it would get used and it would increase the size of every PER_CU.
20100 Entry points like dwarf2_per_cu_addr_size do a similar thing
20101 so we're not in uncharted territory here.
20102 Alas we need to be a bit more complicated as addr_base is contained
20103 in the DIE.
20104
20105 We don't need to read the entire CU(/TU).
20106 We just need the header and top level die.
20107
20108 IWBN to use the aging mechanism to let us lazily later discard the CU.
20109 For now we skip this optimization. */
20110
20111 if (cu != NULL)
20112 {
20113 addr_base = cu->addr_base;
20114 addr_size = cu->header.addr_size;
20115 }
20116 else
20117 {
20118 struct dwarf2_read_addr_index_data aidata;
20119
20120 /* Note: We can't use init_cutu_and_read_dies_simple here,
20121 we need addr_base. */
20122 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
20123 dwarf2_read_addr_index_reader, &aidata);
20124 addr_base = aidata.addr_base;
20125 addr_size = aidata.addr_size;
20126 }
20127
20128 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20129 addr_size);
20130 }
20131
20132 /* Given a DW_FORM_GNU_str_index, fetch the string.
20133 This is only used by the Fission support. */
20134
20135 static const char *
20136 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20137 {
20138 struct dwarf2_cu *cu = reader->cu;
20139 struct dwarf2_per_objfile *dwarf2_per_objfile
20140 = cu->per_cu->dwarf2_per_objfile;
20141 struct objfile *objfile = dwarf2_per_objfile->objfile;
20142 const char *objf_name = objfile_name (objfile);
20143 bfd *abfd = objfile->obfd;
20144 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20145 struct dwarf2_section_info *str_offsets_section =
20146 &reader->dwo_file->sections.str_offsets;
20147 const gdb_byte *info_ptr;
20148 ULONGEST str_offset;
20149 static const char form_name[] = "DW_FORM_GNU_str_index";
20150
20151 dwarf2_read_section (objfile, str_section);
20152 dwarf2_read_section (objfile, str_offsets_section);
20153 if (str_section->buffer == NULL)
20154 error (_("%s used without .debug_str.dwo section"
20155 " in CU at offset %s [in module %s]"),
20156 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20157 if (str_offsets_section->buffer == NULL)
20158 error (_("%s used without .debug_str_offsets.dwo section"
20159 " in CU at offset %s [in module %s]"),
20160 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20161 if (str_index * cu->header.offset_size >= str_offsets_section->size)
20162 error (_("%s pointing outside of .debug_str_offsets.dwo"
20163 " section in CU at offset %s [in module %s]"),
20164 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20165 info_ptr = (str_offsets_section->buffer
20166 + str_index * cu->header.offset_size);
20167 if (cu->header.offset_size == 4)
20168 str_offset = bfd_get_32 (abfd, info_ptr);
20169 else
20170 str_offset = bfd_get_64 (abfd, info_ptr);
20171 if (str_offset >= str_section->size)
20172 error (_("Offset from %s pointing outside of"
20173 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20174 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20175 return (const char *) (str_section->buffer + str_offset);
20176 }
20177
20178 /* Return the length of an LEB128 number in BUF. */
20179
20180 static int
20181 leb128_size (const gdb_byte *buf)
20182 {
20183 const gdb_byte *begin = buf;
20184 gdb_byte byte;
20185
20186 while (1)
20187 {
20188 byte = *buf++;
20189 if ((byte & 128) == 0)
20190 return buf - begin;
20191 }
20192 }
20193
20194 static void
20195 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20196 {
20197 switch (lang)
20198 {
20199 case DW_LANG_C89:
20200 case DW_LANG_C99:
20201 case DW_LANG_C11:
20202 case DW_LANG_C:
20203 case DW_LANG_UPC:
20204 cu->language = language_c;
20205 break;
20206 case DW_LANG_Java:
20207 case DW_LANG_C_plus_plus:
20208 case DW_LANG_C_plus_plus_11:
20209 case DW_LANG_C_plus_plus_14:
20210 cu->language = language_cplus;
20211 break;
20212 case DW_LANG_D:
20213 cu->language = language_d;
20214 break;
20215 case DW_LANG_Fortran77:
20216 case DW_LANG_Fortran90:
20217 case DW_LANG_Fortran95:
20218 case DW_LANG_Fortran03:
20219 case DW_LANG_Fortran08:
20220 cu->language = language_fortran;
20221 break;
20222 case DW_LANG_Go:
20223 cu->language = language_go;
20224 break;
20225 case DW_LANG_Mips_Assembler:
20226 cu->language = language_asm;
20227 break;
20228 case DW_LANG_Ada83:
20229 case DW_LANG_Ada95:
20230 cu->language = language_ada;
20231 break;
20232 case DW_LANG_Modula2:
20233 cu->language = language_m2;
20234 break;
20235 case DW_LANG_Pascal83:
20236 cu->language = language_pascal;
20237 break;
20238 case DW_LANG_ObjC:
20239 cu->language = language_objc;
20240 break;
20241 case DW_LANG_Rust:
20242 case DW_LANG_Rust_old:
20243 cu->language = language_rust;
20244 break;
20245 case DW_LANG_Cobol74:
20246 case DW_LANG_Cobol85:
20247 default:
20248 cu->language = language_minimal;
20249 break;
20250 }
20251 cu->language_defn = language_def (cu->language);
20252 }
20253
20254 /* Return the named attribute or NULL if not there. */
20255
20256 static struct attribute *
20257 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20258 {
20259 for (;;)
20260 {
20261 unsigned int i;
20262 struct attribute *spec = NULL;
20263
20264 for (i = 0; i < die->num_attrs; ++i)
20265 {
20266 if (die->attrs[i].name == name)
20267 return &die->attrs[i];
20268 if (die->attrs[i].name == DW_AT_specification
20269 || die->attrs[i].name == DW_AT_abstract_origin)
20270 spec = &die->attrs[i];
20271 }
20272
20273 if (!spec)
20274 break;
20275
20276 die = follow_die_ref (die, spec, &cu);
20277 }
20278
20279 return NULL;
20280 }
20281
20282 /* Return the named attribute or NULL if not there,
20283 but do not follow DW_AT_specification, etc.
20284 This is for use in contexts where we're reading .debug_types dies.
20285 Following DW_AT_specification, DW_AT_abstract_origin will take us
20286 back up the chain, and we want to go down. */
20287
20288 static struct attribute *
20289 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20290 {
20291 unsigned int i;
20292
20293 for (i = 0; i < die->num_attrs; ++i)
20294 if (die->attrs[i].name == name)
20295 return &die->attrs[i];
20296
20297 return NULL;
20298 }
20299
20300 /* Return the string associated with a string-typed attribute, or NULL if it
20301 is either not found or is of an incorrect type. */
20302
20303 static const char *
20304 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20305 {
20306 struct attribute *attr;
20307 const char *str = NULL;
20308
20309 attr = dwarf2_attr (die, name, cu);
20310
20311 if (attr != NULL)
20312 {
20313 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20314 || attr->form == DW_FORM_string
20315 || attr->form == DW_FORM_GNU_str_index
20316 || attr->form == DW_FORM_GNU_strp_alt)
20317 str = DW_STRING (attr);
20318 else
20319 complaint (&symfile_complaints,
20320 _("string type expected for attribute %s for "
20321 "DIE at %s in module %s"),
20322 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20323 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20324 }
20325
20326 return str;
20327 }
20328
20329 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20330 and holds a non-zero value. This function should only be used for
20331 DW_FORM_flag or DW_FORM_flag_present attributes. */
20332
20333 static int
20334 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20335 {
20336 struct attribute *attr = dwarf2_attr (die, name, cu);
20337
20338 return (attr && DW_UNSND (attr));
20339 }
20340
20341 static int
20342 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20343 {
20344 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20345 which value is non-zero. However, we have to be careful with
20346 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20347 (via dwarf2_flag_true_p) follows this attribute. So we may
20348 end up accidently finding a declaration attribute that belongs
20349 to a different DIE referenced by the specification attribute,
20350 even though the given DIE does not have a declaration attribute. */
20351 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20352 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20353 }
20354
20355 /* Return the die giving the specification for DIE, if there is
20356 one. *SPEC_CU is the CU containing DIE on input, and the CU
20357 containing the return value on output. If there is no
20358 specification, but there is an abstract origin, that is
20359 returned. */
20360
20361 static struct die_info *
20362 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20363 {
20364 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20365 *spec_cu);
20366
20367 if (spec_attr == NULL)
20368 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20369
20370 if (spec_attr == NULL)
20371 return NULL;
20372 else
20373 return follow_die_ref (die, spec_attr, spec_cu);
20374 }
20375
20376 /* Stub for free_line_header to match void * callback types. */
20377
20378 static void
20379 free_line_header_voidp (void *arg)
20380 {
20381 struct line_header *lh = (struct line_header *) arg;
20382
20383 delete lh;
20384 }
20385
20386 void
20387 line_header::add_include_dir (const char *include_dir)
20388 {
20389 if (dwarf_line_debug >= 2)
20390 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20391 include_dirs.size () + 1, include_dir);
20392
20393 include_dirs.push_back (include_dir);
20394 }
20395
20396 void
20397 line_header::add_file_name (const char *name,
20398 dir_index d_index,
20399 unsigned int mod_time,
20400 unsigned int length)
20401 {
20402 if (dwarf_line_debug >= 2)
20403 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
20404 (unsigned) file_names.size () + 1, name);
20405
20406 file_names.emplace_back (name, d_index, mod_time, length);
20407 }
20408
20409 /* A convenience function to find the proper .debug_line section for a CU. */
20410
20411 static struct dwarf2_section_info *
20412 get_debug_line_section (struct dwarf2_cu *cu)
20413 {
20414 struct dwarf2_section_info *section;
20415 struct dwarf2_per_objfile *dwarf2_per_objfile
20416 = cu->per_cu->dwarf2_per_objfile;
20417
20418 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20419 DWO file. */
20420 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20421 section = &cu->dwo_unit->dwo_file->sections.line;
20422 else if (cu->per_cu->is_dwz)
20423 {
20424 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20425
20426 section = &dwz->line;
20427 }
20428 else
20429 section = &dwarf2_per_objfile->line;
20430
20431 return section;
20432 }
20433
20434 /* Read directory or file name entry format, starting with byte of
20435 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20436 entries count and the entries themselves in the described entry
20437 format. */
20438
20439 static void
20440 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20441 bfd *abfd, const gdb_byte **bufp,
20442 struct line_header *lh,
20443 const struct comp_unit_head *cu_header,
20444 void (*callback) (struct line_header *lh,
20445 const char *name,
20446 dir_index d_index,
20447 unsigned int mod_time,
20448 unsigned int length))
20449 {
20450 gdb_byte format_count, formati;
20451 ULONGEST data_count, datai;
20452 const gdb_byte *buf = *bufp;
20453 const gdb_byte *format_header_data;
20454 unsigned int bytes_read;
20455
20456 format_count = read_1_byte (abfd, buf);
20457 buf += 1;
20458 format_header_data = buf;
20459 for (formati = 0; formati < format_count; formati++)
20460 {
20461 read_unsigned_leb128 (abfd, buf, &bytes_read);
20462 buf += bytes_read;
20463 read_unsigned_leb128 (abfd, buf, &bytes_read);
20464 buf += bytes_read;
20465 }
20466
20467 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20468 buf += bytes_read;
20469 for (datai = 0; datai < data_count; datai++)
20470 {
20471 const gdb_byte *format = format_header_data;
20472 struct file_entry fe;
20473
20474 for (formati = 0; formati < format_count; formati++)
20475 {
20476 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20477 format += bytes_read;
20478
20479 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20480 format += bytes_read;
20481
20482 gdb::optional<const char *> string;
20483 gdb::optional<unsigned int> uint;
20484
20485 switch (form)
20486 {
20487 case DW_FORM_string:
20488 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20489 buf += bytes_read;
20490 break;
20491
20492 case DW_FORM_line_strp:
20493 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20494 abfd, buf,
20495 cu_header,
20496 &bytes_read));
20497 buf += bytes_read;
20498 break;
20499
20500 case DW_FORM_data1:
20501 uint.emplace (read_1_byte (abfd, buf));
20502 buf += 1;
20503 break;
20504
20505 case DW_FORM_data2:
20506 uint.emplace (read_2_bytes (abfd, buf));
20507 buf += 2;
20508 break;
20509
20510 case DW_FORM_data4:
20511 uint.emplace (read_4_bytes (abfd, buf));
20512 buf += 4;
20513 break;
20514
20515 case DW_FORM_data8:
20516 uint.emplace (read_8_bytes (abfd, buf));
20517 buf += 8;
20518 break;
20519
20520 case DW_FORM_udata:
20521 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20522 buf += bytes_read;
20523 break;
20524
20525 case DW_FORM_block:
20526 /* It is valid only for DW_LNCT_timestamp which is ignored by
20527 current GDB. */
20528 break;
20529 }
20530
20531 switch (content_type)
20532 {
20533 case DW_LNCT_path:
20534 if (string.has_value ())
20535 fe.name = *string;
20536 break;
20537 case DW_LNCT_directory_index:
20538 if (uint.has_value ())
20539 fe.d_index = (dir_index) *uint;
20540 break;
20541 case DW_LNCT_timestamp:
20542 if (uint.has_value ())
20543 fe.mod_time = *uint;
20544 break;
20545 case DW_LNCT_size:
20546 if (uint.has_value ())
20547 fe.length = *uint;
20548 break;
20549 case DW_LNCT_MD5:
20550 break;
20551 default:
20552 complaint (&symfile_complaints,
20553 _("Unknown format content type %s"),
20554 pulongest (content_type));
20555 }
20556 }
20557
20558 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20559 }
20560
20561 *bufp = buf;
20562 }
20563
20564 /* Read the statement program header starting at OFFSET in
20565 .debug_line, or .debug_line.dwo. Return a pointer
20566 to a struct line_header, allocated using xmalloc.
20567 Returns NULL if there is a problem reading the header, e.g., if it
20568 has a version we don't understand.
20569
20570 NOTE: the strings in the include directory and file name tables of
20571 the returned object point into the dwarf line section buffer,
20572 and must not be freed. */
20573
20574 static line_header_up
20575 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20576 {
20577 const gdb_byte *line_ptr;
20578 unsigned int bytes_read, offset_size;
20579 int i;
20580 const char *cur_dir, *cur_file;
20581 struct dwarf2_section_info *section;
20582 bfd *abfd;
20583 struct dwarf2_per_objfile *dwarf2_per_objfile
20584 = cu->per_cu->dwarf2_per_objfile;
20585
20586 section = get_debug_line_section (cu);
20587 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20588 if (section->buffer == NULL)
20589 {
20590 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20591 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20592 else
20593 complaint (&symfile_complaints, _("missing .debug_line section"));
20594 return 0;
20595 }
20596
20597 /* We can't do this until we know the section is non-empty.
20598 Only then do we know we have such a section. */
20599 abfd = get_section_bfd_owner (section);
20600
20601 /* Make sure that at least there's room for the total_length field.
20602 That could be 12 bytes long, but we're just going to fudge that. */
20603 if (to_underlying (sect_off) + 4 >= section->size)
20604 {
20605 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20606 return 0;
20607 }
20608
20609 line_header_up lh (new line_header ());
20610
20611 lh->sect_off = sect_off;
20612 lh->offset_in_dwz = cu->per_cu->is_dwz;
20613
20614 line_ptr = section->buffer + to_underlying (sect_off);
20615
20616 /* Read in the header. */
20617 lh->total_length =
20618 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20619 &bytes_read, &offset_size);
20620 line_ptr += bytes_read;
20621 if (line_ptr + lh->total_length > (section->buffer + section->size))
20622 {
20623 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20624 return 0;
20625 }
20626 lh->statement_program_end = line_ptr + lh->total_length;
20627 lh->version = read_2_bytes (abfd, line_ptr);
20628 line_ptr += 2;
20629 if (lh->version > 5)
20630 {
20631 /* This is a version we don't understand. The format could have
20632 changed in ways we don't handle properly so just punt. */
20633 complaint (&symfile_complaints,
20634 _("unsupported version in .debug_line section"));
20635 return NULL;
20636 }
20637 if (lh->version >= 5)
20638 {
20639 gdb_byte segment_selector_size;
20640
20641 /* Skip address size. */
20642 read_1_byte (abfd, line_ptr);
20643 line_ptr += 1;
20644
20645 segment_selector_size = read_1_byte (abfd, line_ptr);
20646 line_ptr += 1;
20647 if (segment_selector_size != 0)
20648 {
20649 complaint (&symfile_complaints,
20650 _("unsupported segment selector size %u "
20651 "in .debug_line section"),
20652 segment_selector_size);
20653 return NULL;
20654 }
20655 }
20656 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20657 line_ptr += offset_size;
20658 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20659 line_ptr += 1;
20660 if (lh->version >= 4)
20661 {
20662 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20663 line_ptr += 1;
20664 }
20665 else
20666 lh->maximum_ops_per_instruction = 1;
20667
20668 if (lh->maximum_ops_per_instruction == 0)
20669 {
20670 lh->maximum_ops_per_instruction = 1;
20671 complaint (&symfile_complaints,
20672 _("invalid maximum_ops_per_instruction "
20673 "in `.debug_line' section"));
20674 }
20675
20676 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20677 line_ptr += 1;
20678 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20679 line_ptr += 1;
20680 lh->line_range = read_1_byte (abfd, line_ptr);
20681 line_ptr += 1;
20682 lh->opcode_base = read_1_byte (abfd, line_ptr);
20683 line_ptr += 1;
20684 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20685
20686 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20687 for (i = 1; i < lh->opcode_base; ++i)
20688 {
20689 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20690 line_ptr += 1;
20691 }
20692
20693 if (lh->version >= 5)
20694 {
20695 /* Read directory table. */
20696 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20697 &cu->header,
20698 [] (struct line_header *lh, const char *name,
20699 dir_index d_index, unsigned int mod_time,
20700 unsigned int length)
20701 {
20702 lh->add_include_dir (name);
20703 });
20704
20705 /* Read file name table. */
20706 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20707 &cu->header,
20708 [] (struct line_header *lh, const char *name,
20709 dir_index d_index, unsigned int mod_time,
20710 unsigned int length)
20711 {
20712 lh->add_file_name (name, d_index, mod_time, length);
20713 });
20714 }
20715 else
20716 {
20717 /* Read directory table. */
20718 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20719 {
20720 line_ptr += bytes_read;
20721 lh->add_include_dir (cur_dir);
20722 }
20723 line_ptr += bytes_read;
20724
20725 /* Read file name table. */
20726 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20727 {
20728 unsigned int mod_time, length;
20729 dir_index d_index;
20730
20731 line_ptr += bytes_read;
20732 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20733 line_ptr += bytes_read;
20734 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20735 line_ptr += bytes_read;
20736 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20737 line_ptr += bytes_read;
20738
20739 lh->add_file_name (cur_file, d_index, mod_time, length);
20740 }
20741 line_ptr += bytes_read;
20742 }
20743 lh->statement_program_start = line_ptr;
20744
20745 if (line_ptr > (section->buffer + section->size))
20746 complaint (&symfile_complaints,
20747 _("line number info header doesn't "
20748 "fit in `.debug_line' section"));
20749
20750 return lh;
20751 }
20752
20753 /* Subroutine of dwarf_decode_lines to simplify it.
20754 Return the file name of the psymtab for included file FILE_INDEX
20755 in line header LH of PST.
20756 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20757 If space for the result is malloc'd, *NAME_HOLDER will be set.
20758 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20759
20760 static const char *
20761 psymtab_include_file_name (const struct line_header *lh, int file_index,
20762 const struct partial_symtab *pst,
20763 const char *comp_dir,
20764 gdb::unique_xmalloc_ptr<char> *name_holder)
20765 {
20766 const file_entry &fe = lh->file_names[file_index];
20767 const char *include_name = fe.name;
20768 const char *include_name_to_compare = include_name;
20769 const char *pst_filename;
20770 int file_is_pst;
20771
20772 const char *dir_name = fe.include_dir (lh);
20773
20774 gdb::unique_xmalloc_ptr<char> hold_compare;
20775 if (!IS_ABSOLUTE_PATH (include_name)
20776 && (dir_name != NULL || comp_dir != NULL))
20777 {
20778 /* Avoid creating a duplicate psymtab for PST.
20779 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20780 Before we do the comparison, however, we need to account
20781 for DIR_NAME and COMP_DIR.
20782 First prepend dir_name (if non-NULL). If we still don't
20783 have an absolute path prepend comp_dir (if non-NULL).
20784 However, the directory we record in the include-file's
20785 psymtab does not contain COMP_DIR (to match the
20786 corresponding symtab(s)).
20787
20788 Example:
20789
20790 bash$ cd /tmp
20791 bash$ gcc -g ./hello.c
20792 include_name = "hello.c"
20793 dir_name = "."
20794 DW_AT_comp_dir = comp_dir = "/tmp"
20795 DW_AT_name = "./hello.c"
20796
20797 */
20798
20799 if (dir_name != NULL)
20800 {
20801 name_holder->reset (concat (dir_name, SLASH_STRING,
20802 include_name, (char *) NULL));
20803 include_name = name_holder->get ();
20804 include_name_to_compare = include_name;
20805 }
20806 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20807 {
20808 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20809 include_name, (char *) NULL));
20810 include_name_to_compare = hold_compare.get ();
20811 }
20812 }
20813
20814 pst_filename = pst->filename;
20815 gdb::unique_xmalloc_ptr<char> copied_name;
20816 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20817 {
20818 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20819 pst_filename, (char *) NULL));
20820 pst_filename = copied_name.get ();
20821 }
20822
20823 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20824
20825 if (file_is_pst)
20826 return NULL;
20827 return include_name;
20828 }
20829
20830 /* State machine to track the state of the line number program. */
20831
20832 class lnp_state_machine
20833 {
20834 public:
20835 /* Initialize a machine state for the start of a line number
20836 program. */
20837 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20838
20839 file_entry *current_file ()
20840 {
20841 /* lh->file_names is 0-based, but the file name numbers in the
20842 statement program are 1-based. */
20843 return m_line_header->file_name_at (m_file);
20844 }
20845
20846 /* Record the line in the state machine. END_SEQUENCE is true if
20847 we're processing the end of a sequence. */
20848 void record_line (bool end_sequence);
20849
20850 /* Check address and if invalid nop-out the rest of the lines in this
20851 sequence. */
20852 void check_line_address (struct dwarf2_cu *cu,
20853 const gdb_byte *line_ptr,
20854 CORE_ADDR lowpc, CORE_ADDR address);
20855
20856 void handle_set_discriminator (unsigned int discriminator)
20857 {
20858 m_discriminator = discriminator;
20859 m_line_has_non_zero_discriminator |= discriminator != 0;
20860 }
20861
20862 /* Handle DW_LNE_set_address. */
20863 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20864 {
20865 m_op_index = 0;
20866 address += baseaddr;
20867 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20868 }
20869
20870 /* Handle DW_LNS_advance_pc. */
20871 void handle_advance_pc (CORE_ADDR adjust);
20872
20873 /* Handle a special opcode. */
20874 void handle_special_opcode (unsigned char op_code);
20875
20876 /* Handle DW_LNS_advance_line. */
20877 void handle_advance_line (int line_delta)
20878 {
20879 advance_line (line_delta);
20880 }
20881
20882 /* Handle DW_LNS_set_file. */
20883 void handle_set_file (file_name_index file);
20884
20885 /* Handle DW_LNS_negate_stmt. */
20886 void handle_negate_stmt ()
20887 {
20888 m_is_stmt = !m_is_stmt;
20889 }
20890
20891 /* Handle DW_LNS_const_add_pc. */
20892 void handle_const_add_pc ();
20893
20894 /* Handle DW_LNS_fixed_advance_pc. */
20895 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20896 {
20897 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20898 m_op_index = 0;
20899 }
20900
20901 /* Handle DW_LNS_copy. */
20902 void handle_copy ()
20903 {
20904 record_line (false);
20905 m_discriminator = 0;
20906 }
20907
20908 /* Handle DW_LNE_end_sequence. */
20909 void handle_end_sequence ()
20910 {
20911 m_record_line_callback = ::record_line;
20912 }
20913
20914 private:
20915 /* Advance the line by LINE_DELTA. */
20916 void advance_line (int line_delta)
20917 {
20918 m_line += line_delta;
20919
20920 if (line_delta != 0)
20921 m_line_has_non_zero_discriminator = m_discriminator != 0;
20922 }
20923
20924 gdbarch *m_gdbarch;
20925
20926 /* True if we're recording lines.
20927 Otherwise we're building partial symtabs and are just interested in
20928 finding include files mentioned by the line number program. */
20929 bool m_record_lines_p;
20930
20931 /* The line number header. */
20932 line_header *m_line_header;
20933
20934 /* These are part of the standard DWARF line number state machine,
20935 and initialized according to the DWARF spec. */
20936
20937 unsigned char m_op_index = 0;
20938 /* The line table index (1-based) of the current file. */
20939 file_name_index m_file = (file_name_index) 1;
20940 unsigned int m_line = 1;
20941
20942 /* These are initialized in the constructor. */
20943
20944 CORE_ADDR m_address;
20945 bool m_is_stmt;
20946 unsigned int m_discriminator;
20947
20948 /* Additional bits of state we need to track. */
20949
20950 /* The last file that we called dwarf2_start_subfile for.
20951 This is only used for TLLs. */
20952 unsigned int m_last_file = 0;
20953 /* The last file a line number was recorded for. */
20954 struct subfile *m_last_subfile = NULL;
20955
20956 /* The function to call to record a line. */
20957 record_line_ftype *m_record_line_callback = NULL;
20958
20959 /* The last line number that was recorded, used to coalesce
20960 consecutive entries for the same line. This can happen, for
20961 example, when discriminators are present. PR 17276. */
20962 unsigned int m_last_line = 0;
20963 bool m_line_has_non_zero_discriminator = false;
20964 };
20965
20966 void
20967 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20968 {
20969 CORE_ADDR addr_adj = (((m_op_index + adjust)
20970 / m_line_header->maximum_ops_per_instruction)
20971 * m_line_header->minimum_instruction_length);
20972 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20973 m_op_index = ((m_op_index + adjust)
20974 % m_line_header->maximum_ops_per_instruction);
20975 }
20976
20977 void
20978 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20979 {
20980 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20981 CORE_ADDR addr_adj = (((m_op_index
20982 + (adj_opcode / m_line_header->line_range))
20983 / m_line_header->maximum_ops_per_instruction)
20984 * m_line_header->minimum_instruction_length);
20985 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20986 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20987 % m_line_header->maximum_ops_per_instruction);
20988
20989 int line_delta = (m_line_header->line_base
20990 + (adj_opcode % m_line_header->line_range));
20991 advance_line (line_delta);
20992 record_line (false);
20993 m_discriminator = 0;
20994 }
20995
20996 void
20997 lnp_state_machine::handle_set_file (file_name_index file)
20998 {
20999 m_file = file;
21000
21001 const file_entry *fe = current_file ();
21002 if (fe == NULL)
21003 dwarf2_debug_line_missing_file_complaint ();
21004 else if (m_record_lines_p)
21005 {
21006 const char *dir = fe->include_dir (m_line_header);
21007
21008 m_last_subfile = current_subfile;
21009 m_line_has_non_zero_discriminator = m_discriminator != 0;
21010 dwarf2_start_subfile (fe->name, dir);
21011 }
21012 }
21013
21014 void
21015 lnp_state_machine::handle_const_add_pc ()
21016 {
21017 CORE_ADDR adjust
21018 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21019
21020 CORE_ADDR addr_adj
21021 = (((m_op_index + adjust)
21022 / m_line_header->maximum_ops_per_instruction)
21023 * m_line_header->minimum_instruction_length);
21024
21025 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21026 m_op_index = ((m_op_index + adjust)
21027 % m_line_header->maximum_ops_per_instruction);
21028 }
21029
21030 /* Ignore this record_line request. */
21031
21032 static void
21033 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
21034 {
21035 return;
21036 }
21037
21038 /* Return non-zero if we should add LINE to the line number table.
21039 LINE is the line to add, LAST_LINE is the last line that was added,
21040 LAST_SUBFILE is the subfile for LAST_LINE.
21041 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21042 had a non-zero discriminator.
21043
21044 We have to be careful in the presence of discriminators.
21045 E.g., for this line:
21046
21047 for (i = 0; i < 100000; i++);
21048
21049 clang can emit four line number entries for that one line,
21050 each with a different discriminator.
21051 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21052
21053 However, we want gdb to coalesce all four entries into one.
21054 Otherwise the user could stepi into the middle of the line and
21055 gdb would get confused about whether the pc really was in the
21056 middle of the line.
21057
21058 Things are further complicated by the fact that two consecutive
21059 line number entries for the same line is a heuristic used by gcc
21060 to denote the end of the prologue. So we can't just discard duplicate
21061 entries, we have to be selective about it. The heuristic we use is
21062 that we only collapse consecutive entries for the same line if at least
21063 one of those entries has a non-zero discriminator. PR 17276.
21064
21065 Note: Addresses in the line number state machine can never go backwards
21066 within one sequence, thus this coalescing is ok. */
21067
21068 static int
21069 dwarf_record_line_p (unsigned int line, unsigned int last_line,
21070 int line_has_non_zero_discriminator,
21071 struct subfile *last_subfile)
21072 {
21073 if (current_subfile != last_subfile)
21074 return 1;
21075 if (line != last_line)
21076 return 1;
21077 /* Same line for the same file that we've seen already.
21078 As a last check, for pr 17276, only record the line if the line
21079 has never had a non-zero discriminator. */
21080 if (!line_has_non_zero_discriminator)
21081 return 1;
21082 return 0;
21083 }
21084
21085 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
21086 in the line table of subfile SUBFILE. */
21087
21088 static void
21089 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21090 unsigned int line, CORE_ADDR address,
21091 record_line_ftype p_record_line)
21092 {
21093 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21094
21095 if (dwarf_line_debug)
21096 {
21097 fprintf_unfiltered (gdb_stdlog,
21098 "Recording line %u, file %s, address %s\n",
21099 line, lbasename (subfile->name),
21100 paddress (gdbarch, address));
21101 }
21102
21103 (*p_record_line) (subfile, line, addr);
21104 }
21105
21106 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21107 Mark the end of a set of line number records.
21108 The arguments are the same as for dwarf_record_line_1.
21109 If SUBFILE is NULL the request is ignored. */
21110
21111 static void
21112 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21113 CORE_ADDR address, record_line_ftype p_record_line)
21114 {
21115 if (subfile == NULL)
21116 return;
21117
21118 if (dwarf_line_debug)
21119 {
21120 fprintf_unfiltered (gdb_stdlog,
21121 "Finishing current line, file %s, address %s\n",
21122 lbasename (subfile->name),
21123 paddress (gdbarch, address));
21124 }
21125
21126 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
21127 }
21128
21129 void
21130 lnp_state_machine::record_line (bool end_sequence)
21131 {
21132 if (dwarf_line_debug)
21133 {
21134 fprintf_unfiltered (gdb_stdlog,
21135 "Processing actual line %u: file %u,"
21136 " address %s, is_stmt %u, discrim %u\n",
21137 m_line, to_underlying (m_file),
21138 paddress (m_gdbarch, m_address),
21139 m_is_stmt, m_discriminator);
21140 }
21141
21142 file_entry *fe = current_file ();
21143
21144 if (fe == NULL)
21145 dwarf2_debug_line_missing_file_complaint ();
21146 /* For now we ignore lines not starting on an instruction boundary.
21147 But not when processing end_sequence for compatibility with the
21148 previous version of the code. */
21149 else if (m_op_index == 0 || end_sequence)
21150 {
21151 fe->included_p = 1;
21152 if (m_record_lines_p && m_is_stmt)
21153 {
21154 if (m_last_subfile != current_subfile || end_sequence)
21155 {
21156 dwarf_finish_line (m_gdbarch, m_last_subfile,
21157 m_address, m_record_line_callback);
21158 }
21159
21160 if (!end_sequence)
21161 {
21162 if (dwarf_record_line_p (m_line, m_last_line,
21163 m_line_has_non_zero_discriminator,
21164 m_last_subfile))
21165 {
21166 dwarf_record_line_1 (m_gdbarch, current_subfile,
21167 m_line, m_address,
21168 m_record_line_callback);
21169 }
21170 m_last_subfile = current_subfile;
21171 m_last_line = m_line;
21172 }
21173 }
21174 }
21175 }
21176
21177 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
21178 bool record_lines_p)
21179 {
21180 m_gdbarch = arch;
21181 m_record_lines_p = record_lines_p;
21182 m_line_header = lh;
21183
21184 m_record_line_callback = ::record_line;
21185
21186 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21187 was a line entry for it so that the backend has a chance to adjust it
21188 and also record it in case it needs it. This is currently used by MIPS
21189 code, cf. `mips_adjust_dwarf2_line'. */
21190 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21191 m_is_stmt = lh->default_is_stmt;
21192 m_discriminator = 0;
21193 }
21194
21195 void
21196 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21197 const gdb_byte *line_ptr,
21198 CORE_ADDR lowpc, CORE_ADDR address)
21199 {
21200 /* If address < lowpc then it's not a usable value, it's outside the
21201 pc range of the CU. However, we restrict the test to only address
21202 values of zero to preserve GDB's previous behaviour which is to
21203 handle the specific case of a function being GC'd by the linker. */
21204
21205 if (address == 0 && address < lowpc)
21206 {
21207 /* This line table is for a function which has been
21208 GCd by the linker. Ignore it. PR gdb/12528 */
21209
21210 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21211 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21212
21213 complaint (&symfile_complaints,
21214 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21215 line_offset, objfile_name (objfile));
21216 m_record_line_callback = noop_record_line;
21217 /* Note: record_line_callback is left as noop_record_line until
21218 we see DW_LNE_end_sequence. */
21219 }
21220 }
21221
21222 /* Subroutine of dwarf_decode_lines to simplify it.
21223 Process the line number information in LH.
21224 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21225 program in order to set included_p for every referenced header. */
21226
21227 static void
21228 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21229 const int decode_for_pst_p, CORE_ADDR lowpc)
21230 {
21231 const gdb_byte *line_ptr, *extended_end;
21232 const gdb_byte *line_end;
21233 unsigned int bytes_read, extended_len;
21234 unsigned char op_code, extended_op;
21235 CORE_ADDR baseaddr;
21236 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21237 bfd *abfd = objfile->obfd;
21238 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21239 /* True if we're recording line info (as opposed to building partial
21240 symtabs and just interested in finding include files mentioned by
21241 the line number program). */
21242 bool record_lines_p = !decode_for_pst_p;
21243
21244 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21245
21246 line_ptr = lh->statement_program_start;
21247 line_end = lh->statement_program_end;
21248
21249 /* Read the statement sequences until there's nothing left. */
21250 while (line_ptr < line_end)
21251 {
21252 /* The DWARF line number program state machine. Reset the state
21253 machine at the start of each sequence. */
21254 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
21255 bool end_sequence = false;
21256
21257 if (record_lines_p)
21258 {
21259 /* Start a subfile for the current file of the state
21260 machine. */
21261 const file_entry *fe = state_machine.current_file ();
21262
21263 if (fe != NULL)
21264 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
21265 }
21266
21267 /* Decode the table. */
21268 while (line_ptr < line_end && !end_sequence)
21269 {
21270 op_code = read_1_byte (abfd, line_ptr);
21271 line_ptr += 1;
21272
21273 if (op_code >= lh->opcode_base)
21274 {
21275 /* Special opcode. */
21276 state_machine.handle_special_opcode (op_code);
21277 }
21278 else switch (op_code)
21279 {
21280 case DW_LNS_extended_op:
21281 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21282 &bytes_read);
21283 line_ptr += bytes_read;
21284 extended_end = line_ptr + extended_len;
21285 extended_op = read_1_byte (abfd, line_ptr);
21286 line_ptr += 1;
21287 switch (extended_op)
21288 {
21289 case DW_LNE_end_sequence:
21290 state_machine.handle_end_sequence ();
21291 end_sequence = true;
21292 break;
21293 case DW_LNE_set_address:
21294 {
21295 CORE_ADDR address
21296 = read_address (abfd, line_ptr, cu, &bytes_read);
21297 line_ptr += bytes_read;
21298
21299 state_machine.check_line_address (cu, line_ptr,
21300 lowpc, address);
21301 state_machine.handle_set_address (baseaddr, address);
21302 }
21303 break;
21304 case DW_LNE_define_file:
21305 {
21306 const char *cur_file;
21307 unsigned int mod_time, length;
21308 dir_index dindex;
21309
21310 cur_file = read_direct_string (abfd, line_ptr,
21311 &bytes_read);
21312 line_ptr += bytes_read;
21313 dindex = (dir_index)
21314 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21315 line_ptr += bytes_read;
21316 mod_time =
21317 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21318 line_ptr += bytes_read;
21319 length =
21320 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21321 line_ptr += bytes_read;
21322 lh->add_file_name (cur_file, dindex, mod_time, length);
21323 }
21324 break;
21325 case DW_LNE_set_discriminator:
21326 {
21327 /* The discriminator is not interesting to the
21328 debugger; just ignore it. We still need to
21329 check its value though:
21330 if there are consecutive entries for the same
21331 (non-prologue) line we want to coalesce them.
21332 PR 17276. */
21333 unsigned int discr
21334 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21335 line_ptr += bytes_read;
21336
21337 state_machine.handle_set_discriminator (discr);
21338 }
21339 break;
21340 default:
21341 complaint (&symfile_complaints,
21342 _("mangled .debug_line section"));
21343 return;
21344 }
21345 /* Make sure that we parsed the extended op correctly. If e.g.
21346 we expected a different address size than the producer used,
21347 we may have read the wrong number of bytes. */
21348 if (line_ptr != extended_end)
21349 {
21350 complaint (&symfile_complaints,
21351 _("mangled .debug_line section"));
21352 return;
21353 }
21354 break;
21355 case DW_LNS_copy:
21356 state_machine.handle_copy ();
21357 break;
21358 case DW_LNS_advance_pc:
21359 {
21360 CORE_ADDR adjust
21361 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21362 line_ptr += bytes_read;
21363
21364 state_machine.handle_advance_pc (adjust);
21365 }
21366 break;
21367 case DW_LNS_advance_line:
21368 {
21369 int line_delta
21370 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21371 line_ptr += bytes_read;
21372
21373 state_machine.handle_advance_line (line_delta);
21374 }
21375 break;
21376 case DW_LNS_set_file:
21377 {
21378 file_name_index file
21379 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21380 &bytes_read);
21381 line_ptr += bytes_read;
21382
21383 state_machine.handle_set_file (file);
21384 }
21385 break;
21386 case DW_LNS_set_column:
21387 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21388 line_ptr += bytes_read;
21389 break;
21390 case DW_LNS_negate_stmt:
21391 state_machine.handle_negate_stmt ();
21392 break;
21393 case DW_LNS_set_basic_block:
21394 break;
21395 /* Add to the address register of the state machine the
21396 address increment value corresponding to special opcode
21397 255. I.e., this value is scaled by the minimum
21398 instruction length since special opcode 255 would have
21399 scaled the increment. */
21400 case DW_LNS_const_add_pc:
21401 state_machine.handle_const_add_pc ();
21402 break;
21403 case DW_LNS_fixed_advance_pc:
21404 {
21405 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21406 line_ptr += 2;
21407
21408 state_machine.handle_fixed_advance_pc (addr_adj);
21409 }
21410 break;
21411 default:
21412 {
21413 /* Unknown standard opcode, ignore it. */
21414 int i;
21415
21416 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21417 {
21418 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21419 line_ptr += bytes_read;
21420 }
21421 }
21422 }
21423 }
21424
21425 if (!end_sequence)
21426 dwarf2_debug_line_missing_end_sequence_complaint ();
21427
21428 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21429 in which case we still finish recording the last line). */
21430 state_machine.record_line (true);
21431 }
21432 }
21433
21434 /* Decode the Line Number Program (LNP) for the given line_header
21435 structure and CU. The actual information extracted and the type
21436 of structures created from the LNP depends on the value of PST.
21437
21438 1. If PST is NULL, then this procedure uses the data from the program
21439 to create all necessary symbol tables, and their linetables.
21440
21441 2. If PST is not NULL, this procedure reads the program to determine
21442 the list of files included by the unit represented by PST, and
21443 builds all the associated partial symbol tables.
21444
21445 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21446 It is used for relative paths in the line table.
21447 NOTE: When processing partial symtabs (pst != NULL),
21448 comp_dir == pst->dirname.
21449
21450 NOTE: It is important that psymtabs have the same file name (via strcmp)
21451 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21452 symtab we don't use it in the name of the psymtabs we create.
21453 E.g. expand_line_sal requires this when finding psymtabs to expand.
21454 A good testcase for this is mb-inline.exp.
21455
21456 LOWPC is the lowest address in CU (or 0 if not known).
21457
21458 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21459 for its PC<->lines mapping information. Otherwise only the filename
21460 table is read in. */
21461
21462 static void
21463 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21464 struct dwarf2_cu *cu, struct partial_symtab *pst,
21465 CORE_ADDR lowpc, int decode_mapping)
21466 {
21467 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21468 const int decode_for_pst_p = (pst != NULL);
21469
21470 if (decode_mapping)
21471 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21472
21473 if (decode_for_pst_p)
21474 {
21475 int file_index;
21476
21477 /* Now that we're done scanning the Line Header Program, we can
21478 create the psymtab of each included file. */
21479 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21480 if (lh->file_names[file_index].included_p == 1)
21481 {
21482 gdb::unique_xmalloc_ptr<char> name_holder;
21483 const char *include_name =
21484 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21485 &name_holder);
21486 if (include_name != NULL)
21487 dwarf2_create_include_psymtab (include_name, pst, objfile);
21488 }
21489 }
21490 else
21491 {
21492 /* Make sure a symtab is created for every file, even files
21493 which contain only variables (i.e. no code with associated
21494 line numbers). */
21495 struct compunit_symtab *cust = buildsym_compunit_symtab ();
21496 int i;
21497
21498 for (i = 0; i < lh->file_names.size (); i++)
21499 {
21500 file_entry &fe = lh->file_names[i];
21501
21502 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
21503
21504 if (current_subfile->symtab == NULL)
21505 {
21506 current_subfile->symtab
21507 = allocate_symtab (cust, current_subfile->name);
21508 }
21509 fe.symtab = current_subfile->symtab;
21510 }
21511 }
21512 }
21513
21514 /* Start a subfile for DWARF. FILENAME is the name of the file and
21515 DIRNAME the name of the source directory which contains FILENAME
21516 or NULL if not known.
21517 This routine tries to keep line numbers from identical absolute and
21518 relative file names in a common subfile.
21519
21520 Using the `list' example from the GDB testsuite, which resides in
21521 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21522 of /srcdir/list0.c yields the following debugging information for list0.c:
21523
21524 DW_AT_name: /srcdir/list0.c
21525 DW_AT_comp_dir: /compdir
21526 files.files[0].name: list0.h
21527 files.files[0].dir: /srcdir
21528 files.files[1].name: list0.c
21529 files.files[1].dir: /srcdir
21530
21531 The line number information for list0.c has to end up in a single
21532 subfile, so that `break /srcdir/list0.c:1' works as expected.
21533 start_subfile will ensure that this happens provided that we pass the
21534 concatenation of files.files[1].dir and files.files[1].name as the
21535 subfile's name. */
21536
21537 static void
21538 dwarf2_start_subfile (const char *filename, const char *dirname)
21539 {
21540 char *copy = NULL;
21541
21542 /* In order not to lose the line information directory,
21543 we concatenate it to the filename when it makes sense.
21544 Note that the Dwarf3 standard says (speaking of filenames in line
21545 information): ``The directory index is ignored for file names
21546 that represent full path names''. Thus ignoring dirname in the
21547 `else' branch below isn't an issue. */
21548
21549 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21550 {
21551 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21552 filename = copy;
21553 }
21554
21555 start_subfile (filename);
21556
21557 if (copy != NULL)
21558 xfree (copy);
21559 }
21560
21561 /* Start a symtab for DWARF.
21562 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21563
21564 static struct compunit_symtab *
21565 dwarf2_start_symtab (struct dwarf2_cu *cu,
21566 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21567 {
21568 struct compunit_symtab *cust
21569 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21570 low_pc, cu->language);
21571
21572 record_debugformat ("DWARF 2");
21573 record_producer (cu->producer);
21574
21575 /* We assume that we're processing GCC output. */
21576 processing_gcc_compilation = 2;
21577
21578 cu->processing_has_namespace_info = 0;
21579
21580 return cust;
21581 }
21582
21583 static void
21584 var_decode_location (struct attribute *attr, struct symbol *sym,
21585 struct dwarf2_cu *cu)
21586 {
21587 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21588 struct comp_unit_head *cu_header = &cu->header;
21589
21590 /* NOTE drow/2003-01-30: There used to be a comment and some special
21591 code here to turn a symbol with DW_AT_external and a
21592 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21593 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21594 with some versions of binutils) where shared libraries could have
21595 relocations against symbols in their debug information - the
21596 minimal symbol would have the right address, but the debug info
21597 would not. It's no longer necessary, because we will explicitly
21598 apply relocations when we read in the debug information now. */
21599
21600 /* A DW_AT_location attribute with no contents indicates that a
21601 variable has been optimized away. */
21602 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21603 {
21604 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21605 return;
21606 }
21607
21608 /* Handle one degenerate form of location expression specially, to
21609 preserve GDB's previous behavior when section offsets are
21610 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21611 then mark this symbol as LOC_STATIC. */
21612
21613 if (attr_form_is_block (attr)
21614 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21615 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21616 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21617 && (DW_BLOCK (attr)->size
21618 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21619 {
21620 unsigned int dummy;
21621
21622 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21623 SYMBOL_VALUE_ADDRESS (sym) =
21624 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21625 else
21626 SYMBOL_VALUE_ADDRESS (sym) =
21627 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21628 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21629 fixup_symbol_section (sym, objfile);
21630 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21631 SYMBOL_SECTION (sym));
21632 return;
21633 }
21634
21635 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21636 expression evaluator, and use LOC_COMPUTED only when necessary
21637 (i.e. when the value of a register or memory location is
21638 referenced, or a thread-local block, etc.). Then again, it might
21639 not be worthwhile. I'm assuming that it isn't unless performance
21640 or memory numbers show me otherwise. */
21641
21642 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21643
21644 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21645 cu->has_loclist = 1;
21646 }
21647
21648 /* Given a pointer to a DWARF information entry, figure out if we need
21649 to make a symbol table entry for it, and if so, create a new entry
21650 and return a pointer to it.
21651 If TYPE is NULL, determine symbol type from the die, otherwise
21652 used the passed type.
21653 If SPACE is not NULL, use it to hold the new symbol. If it is
21654 NULL, allocate a new symbol on the objfile's obstack. */
21655
21656 static struct symbol *
21657 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21658 struct symbol *space)
21659 {
21660 struct dwarf2_per_objfile *dwarf2_per_objfile
21661 = cu->per_cu->dwarf2_per_objfile;
21662 struct objfile *objfile = dwarf2_per_objfile->objfile;
21663 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21664 struct symbol *sym = NULL;
21665 const char *name;
21666 struct attribute *attr = NULL;
21667 struct attribute *attr2 = NULL;
21668 CORE_ADDR baseaddr;
21669 struct pending **list_to_add = NULL;
21670
21671 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21672
21673 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21674
21675 name = dwarf2_name (die, cu);
21676 if (name)
21677 {
21678 const char *linkagename;
21679 int suppress_add = 0;
21680
21681 if (space)
21682 sym = space;
21683 else
21684 sym = allocate_symbol (objfile);
21685 OBJSTAT (objfile, n_syms++);
21686
21687 /* Cache this symbol's name and the name's demangled form (if any). */
21688 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21689 linkagename = dwarf2_physname (name, die, cu);
21690 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21691
21692 /* Fortran does not have mangling standard and the mangling does differ
21693 between gfortran, iFort etc. */
21694 if (cu->language == language_fortran
21695 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21696 symbol_set_demangled_name (&(sym->ginfo),
21697 dwarf2_full_name (name, die, cu),
21698 NULL);
21699
21700 /* Default assumptions.
21701 Use the passed type or decode it from the die. */
21702 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21703 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21704 if (type != NULL)
21705 SYMBOL_TYPE (sym) = type;
21706 else
21707 SYMBOL_TYPE (sym) = die_type (die, cu);
21708 attr = dwarf2_attr (die,
21709 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21710 cu);
21711 if (attr)
21712 {
21713 SYMBOL_LINE (sym) = DW_UNSND (attr);
21714 }
21715
21716 attr = dwarf2_attr (die,
21717 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21718 cu);
21719 if (attr)
21720 {
21721 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21722 struct file_entry *fe;
21723
21724 if (cu->line_header != NULL)
21725 fe = cu->line_header->file_name_at (file_index);
21726 else
21727 fe = NULL;
21728
21729 if (fe == NULL)
21730 complaint (&symfile_complaints,
21731 _("file index out of range"));
21732 else
21733 symbol_set_symtab (sym, fe->symtab);
21734 }
21735
21736 switch (die->tag)
21737 {
21738 case DW_TAG_label:
21739 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21740 if (attr)
21741 {
21742 CORE_ADDR addr;
21743
21744 addr = attr_value_as_address (attr);
21745 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21746 SYMBOL_VALUE_ADDRESS (sym) = addr;
21747 }
21748 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21749 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21750 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21751 add_symbol_to_list (sym, cu->list_in_scope);
21752 break;
21753 case DW_TAG_subprogram:
21754 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21755 finish_block. */
21756 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21757 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21758 if ((attr2 && (DW_UNSND (attr2) != 0))
21759 || cu->language == language_ada)
21760 {
21761 /* Subprograms marked external are stored as a global symbol.
21762 Ada subprograms, whether marked external or not, are always
21763 stored as a global symbol, because we want to be able to
21764 access them globally. For instance, we want to be able
21765 to break on a nested subprogram without having to
21766 specify the context. */
21767 list_to_add = &global_symbols;
21768 }
21769 else
21770 {
21771 list_to_add = cu->list_in_scope;
21772 }
21773 break;
21774 case DW_TAG_inlined_subroutine:
21775 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21776 finish_block. */
21777 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21778 SYMBOL_INLINED (sym) = 1;
21779 list_to_add = cu->list_in_scope;
21780 break;
21781 case DW_TAG_template_value_param:
21782 suppress_add = 1;
21783 /* Fall through. */
21784 case DW_TAG_constant:
21785 case DW_TAG_variable:
21786 case DW_TAG_member:
21787 /* Compilation with minimal debug info may result in
21788 variables with missing type entries. Change the
21789 misleading `void' type to something sensible. */
21790 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21791 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21792
21793 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21794 /* In the case of DW_TAG_member, we should only be called for
21795 static const members. */
21796 if (die->tag == DW_TAG_member)
21797 {
21798 /* dwarf2_add_field uses die_is_declaration,
21799 so we do the same. */
21800 gdb_assert (die_is_declaration (die, cu));
21801 gdb_assert (attr);
21802 }
21803 if (attr)
21804 {
21805 dwarf2_const_value (attr, sym, cu);
21806 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21807 if (!suppress_add)
21808 {
21809 if (attr2 && (DW_UNSND (attr2) != 0))
21810 list_to_add = &global_symbols;
21811 else
21812 list_to_add = cu->list_in_scope;
21813 }
21814 break;
21815 }
21816 attr = dwarf2_attr (die, DW_AT_location, cu);
21817 if (attr)
21818 {
21819 var_decode_location (attr, sym, cu);
21820 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21821
21822 /* Fortran explicitly imports any global symbols to the local
21823 scope by DW_TAG_common_block. */
21824 if (cu->language == language_fortran && die->parent
21825 && die->parent->tag == DW_TAG_common_block)
21826 attr2 = NULL;
21827
21828 if (SYMBOL_CLASS (sym) == LOC_STATIC
21829 && SYMBOL_VALUE_ADDRESS (sym) == 0
21830 && !dwarf2_per_objfile->has_section_at_zero)
21831 {
21832 /* When a static variable is eliminated by the linker,
21833 the corresponding debug information is not stripped
21834 out, but the variable address is set to null;
21835 do not add such variables into symbol table. */
21836 }
21837 else if (attr2 && (DW_UNSND (attr2) != 0))
21838 {
21839 /* Workaround gfortran PR debug/40040 - it uses
21840 DW_AT_location for variables in -fPIC libraries which may
21841 get overriden by other libraries/executable and get
21842 a different address. Resolve it by the minimal symbol
21843 which may come from inferior's executable using copy
21844 relocation. Make this workaround only for gfortran as for
21845 other compilers GDB cannot guess the minimal symbol
21846 Fortran mangling kind. */
21847 if (cu->language == language_fortran && die->parent
21848 && die->parent->tag == DW_TAG_module
21849 && cu->producer
21850 && startswith (cu->producer, "GNU Fortran"))
21851 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21852
21853 /* A variable with DW_AT_external is never static,
21854 but it may be block-scoped. */
21855 list_to_add = (cu->list_in_scope == &file_symbols
21856 ? &global_symbols : cu->list_in_scope);
21857 }
21858 else
21859 list_to_add = cu->list_in_scope;
21860 }
21861 else
21862 {
21863 /* We do not know the address of this symbol.
21864 If it is an external symbol and we have type information
21865 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21866 The address of the variable will then be determined from
21867 the minimal symbol table whenever the variable is
21868 referenced. */
21869 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21870
21871 /* Fortran explicitly imports any global symbols to the local
21872 scope by DW_TAG_common_block. */
21873 if (cu->language == language_fortran && die->parent
21874 && die->parent->tag == DW_TAG_common_block)
21875 {
21876 /* SYMBOL_CLASS doesn't matter here because
21877 read_common_block is going to reset it. */
21878 if (!suppress_add)
21879 list_to_add = cu->list_in_scope;
21880 }
21881 else if (attr2 && (DW_UNSND (attr2) != 0)
21882 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21883 {
21884 /* A variable with DW_AT_external is never static, but it
21885 may be block-scoped. */
21886 list_to_add = (cu->list_in_scope == &file_symbols
21887 ? &global_symbols : cu->list_in_scope);
21888
21889 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21890 }
21891 else if (!die_is_declaration (die, cu))
21892 {
21893 /* Use the default LOC_OPTIMIZED_OUT class. */
21894 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21895 if (!suppress_add)
21896 list_to_add = cu->list_in_scope;
21897 }
21898 }
21899 break;
21900 case DW_TAG_formal_parameter:
21901 /* If we are inside a function, mark this as an argument. If
21902 not, we might be looking at an argument to an inlined function
21903 when we do not have enough information to show inlined frames;
21904 pretend it's a local variable in that case so that the user can
21905 still see it. */
21906 if (context_stack_depth > 0
21907 && context_stack[context_stack_depth - 1].name != NULL)
21908 SYMBOL_IS_ARGUMENT (sym) = 1;
21909 attr = dwarf2_attr (die, DW_AT_location, cu);
21910 if (attr)
21911 {
21912 var_decode_location (attr, sym, cu);
21913 }
21914 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21915 if (attr)
21916 {
21917 dwarf2_const_value (attr, sym, cu);
21918 }
21919
21920 list_to_add = cu->list_in_scope;
21921 break;
21922 case DW_TAG_unspecified_parameters:
21923 /* From varargs functions; gdb doesn't seem to have any
21924 interest in this information, so just ignore it for now.
21925 (FIXME?) */
21926 break;
21927 case DW_TAG_template_type_param:
21928 suppress_add = 1;
21929 /* Fall through. */
21930 case DW_TAG_class_type:
21931 case DW_TAG_interface_type:
21932 case DW_TAG_structure_type:
21933 case DW_TAG_union_type:
21934 case DW_TAG_set_type:
21935 case DW_TAG_enumeration_type:
21936 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21937 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21938
21939 {
21940 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21941 really ever be static objects: otherwise, if you try
21942 to, say, break of a class's method and you're in a file
21943 which doesn't mention that class, it won't work unless
21944 the check for all static symbols in lookup_symbol_aux
21945 saves you. See the OtherFileClass tests in
21946 gdb.c++/namespace.exp. */
21947
21948 if (!suppress_add)
21949 {
21950 list_to_add = (cu->list_in_scope == &file_symbols
21951 && cu->language == language_cplus
21952 ? &global_symbols : cu->list_in_scope);
21953
21954 /* The semantics of C++ state that "struct foo {
21955 ... }" also defines a typedef for "foo". */
21956 if (cu->language == language_cplus
21957 || cu->language == language_ada
21958 || cu->language == language_d
21959 || cu->language == language_rust)
21960 {
21961 /* The symbol's name is already allocated along
21962 with this objfile, so we don't need to
21963 duplicate it for the type. */
21964 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21965 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21966 }
21967 }
21968 }
21969 break;
21970 case DW_TAG_typedef:
21971 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21972 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21973 list_to_add = cu->list_in_scope;
21974 break;
21975 case DW_TAG_base_type:
21976 case DW_TAG_subrange_type:
21977 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21978 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21979 list_to_add = cu->list_in_scope;
21980 break;
21981 case DW_TAG_enumerator:
21982 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21983 if (attr)
21984 {
21985 dwarf2_const_value (attr, sym, cu);
21986 }
21987 {
21988 /* NOTE: carlton/2003-11-10: See comment above in the
21989 DW_TAG_class_type, etc. block. */
21990
21991 list_to_add = (cu->list_in_scope == &file_symbols
21992 && cu->language == language_cplus
21993 ? &global_symbols : cu->list_in_scope);
21994 }
21995 break;
21996 case DW_TAG_imported_declaration:
21997 case DW_TAG_namespace:
21998 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21999 list_to_add = &global_symbols;
22000 break;
22001 case DW_TAG_module:
22002 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22003 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22004 list_to_add = &global_symbols;
22005 break;
22006 case DW_TAG_common_block:
22007 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22008 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22009 add_symbol_to_list (sym, cu->list_in_scope);
22010 break;
22011 default:
22012 /* Not a tag we recognize. Hopefully we aren't processing
22013 trash data, but since we must specifically ignore things
22014 we don't recognize, there is nothing else we should do at
22015 this point. */
22016 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
22017 dwarf_tag_name (die->tag));
22018 break;
22019 }
22020
22021 if (suppress_add)
22022 {
22023 sym->hash_next = objfile->template_symbols;
22024 objfile->template_symbols = sym;
22025 list_to_add = NULL;
22026 }
22027
22028 if (list_to_add != NULL)
22029 add_symbol_to_list (sym, list_to_add);
22030
22031 /* For the benefit of old versions of GCC, check for anonymous
22032 namespaces based on the demangled name. */
22033 if (!cu->processing_has_namespace_info
22034 && cu->language == language_cplus)
22035 cp_scan_for_anonymous_namespaces (sym, objfile);
22036 }
22037 return (sym);
22038 }
22039
22040 /* Given an attr with a DW_FORM_dataN value in host byte order,
22041 zero-extend it as appropriate for the symbol's type. The DWARF
22042 standard (v4) is not entirely clear about the meaning of using
22043 DW_FORM_dataN for a constant with a signed type, where the type is
22044 wider than the data. The conclusion of a discussion on the DWARF
22045 list was that this is unspecified. We choose to always zero-extend
22046 because that is the interpretation long in use by GCC. */
22047
22048 static gdb_byte *
22049 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22050 struct dwarf2_cu *cu, LONGEST *value, int bits)
22051 {
22052 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22053 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22054 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22055 LONGEST l = DW_UNSND (attr);
22056
22057 if (bits < sizeof (*value) * 8)
22058 {
22059 l &= ((LONGEST) 1 << bits) - 1;
22060 *value = l;
22061 }
22062 else if (bits == sizeof (*value) * 8)
22063 *value = l;
22064 else
22065 {
22066 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22067 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22068 return bytes;
22069 }
22070
22071 return NULL;
22072 }
22073
22074 /* Read a constant value from an attribute. Either set *VALUE, or if
22075 the value does not fit in *VALUE, set *BYTES - either already
22076 allocated on the objfile obstack, or newly allocated on OBSTACK,
22077 or, set *BATON, if we translated the constant to a location
22078 expression. */
22079
22080 static void
22081 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22082 const char *name, struct obstack *obstack,
22083 struct dwarf2_cu *cu,
22084 LONGEST *value, const gdb_byte **bytes,
22085 struct dwarf2_locexpr_baton **baton)
22086 {
22087 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22088 struct comp_unit_head *cu_header = &cu->header;
22089 struct dwarf_block *blk;
22090 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22091 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22092
22093 *value = 0;
22094 *bytes = NULL;
22095 *baton = NULL;
22096
22097 switch (attr->form)
22098 {
22099 case DW_FORM_addr:
22100 case DW_FORM_GNU_addr_index:
22101 {
22102 gdb_byte *data;
22103
22104 if (TYPE_LENGTH (type) != cu_header->addr_size)
22105 dwarf2_const_value_length_mismatch_complaint (name,
22106 cu_header->addr_size,
22107 TYPE_LENGTH (type));
22108 /* Symbols of this form are reasonably rare, so we just
22109 piggyback on the existing location code rather than writing
22110 a new implementation of symbol_computed_ops. */
22111 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22112 (*baton)->per_cu = cu->per_cu;
22113 gdb_assert ((*baton)->per_cu);
22114
22115 (*baton)->size = 2 + cu_header->addr_size;
22116 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22117 (*baton)->data = data;
22118
22119 data[0] = DW_OP_addr;
22120 store_unsigned_integer (&data[1], cu_header->addr_size,
22121 byte_order, DW_ADDR (attr));
22122 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22123 }
22124 break;
22125 case DW_FORM_string:
22126 case DW_FORM_strp:
22127 case DW_FORM_GNU_str_index:
22128 case DW_FORM_GNU_strp_alt:
22129 /* DW_STRING is already allocated on the objfile obstack, point
22130 directly to it. */
22131 *bytes = (const gdb_byte *) DW_STRING (attr);
22132 break;
22133 case DW_FORM_block1:
22134 case DW_FORM_block2:
22135 case DW_FORM_block4:
22136 case DW_FORM_block:
22137 case DW_FORM_exprloc:
22138 case DW_FORM_data16:
22139 blk = DW_BLOCK (attr);
22140 if (TYPE_LENGTH (type) != blk->size)
22141 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22142 TYPE_LENGTH (type));
22143 *bytes = blk->data;
22144 break;
22145
22146 /* The DW_AT_const_value attributes are supposed to carry the
22147 symbol's value "represented as it would be on the target
22148 architecture." By the time we get here, it's already been
22149 converted to host endianness, so we just need to sign- or
22150 zero-extend it as appropriate. */
22151 case DW_FORM_data1:
22152 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22153 break;
22154 case DW_FORM_data2:
22155 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22156 break;
22157 case DW_FORM_data4:
22158 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22159 break;
22160 case DW_FORM_data8:
22161 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22162 break;
22163
22164 case DW_FORM_sdata:
22165 case DW_FORM_implicit_const:
22166 *value = DW_SND (attr);
22167 break;
22168
22169 case DW_FORM_udata:
22170 *value = DW_UNSND (attr);
22171 break;
22172
22173 default:
22174 complaint (&symfile_complaints,
22175 _("unsupported const value attribute form: '%s'"),
22176 dwarf_form_name (attr->form));
22177 *value = 0;
22178 break;
22179 }
22180 }
22181
22182
22183 /* Copy constant value from an attribute to a symbol. */
22184
22185 static void
22186 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22187 struct dwarf2_cu *cu)
22188 {
22189 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22190 LONGEST value;
22191 const gdb_byte *bytes;
22192 struct dwarf2_locexpr_baton *baton;
22193
22194 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22195 SYMBOL_PRINT_NAME (sym),
22196 &objfile->objfile_obstack, cu,
22197 &value, &bytes, &baton);
22198
22199 if (baton != NULL)
22200 {
22201 SYMBOL_LOCATION_BATON (sym) = baton;
22202 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22203 }
22204 else if (bytes != NULL)
22205 {
22206 SYMBOL_VALUE_BYTES (sym) = bytes;
22207 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22208 }
22209 else
22210 {
22211 SYMBOL_VALUE (sym) = value;
22212 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22213 }
22214 }
22215
22216 /* Return the type of the die in question using its DW_AT_type attribute. */
22217
22218 static struct type *
22219 die_type (struct die_info *die, struct dwarf2_cu *cu)
22220 {
22221 struct attribute *type_attr;
22222
22223 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22224 if (!type_attr)
22225 {
22226 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22227 /* A missing DW_AT_type represents a void type. */
22228 return objfile_type (objfile)->builtin_void;
22229 }
22230
22231 return lookup_die_type (die, type_attr, cu);
22232 }
22233
22234 /* True iff CU's producer generates GNAT Ada auxiliary information
22235 that allows to find parallel types through that information instead
22236 of having to do expensive parallel lookups by type name. */
22237
22238 static int
22239 need_gnat_info (struct dwarf2_cu *cu)
22240 {
22241 /* Assume that the Ada compiler was GNAT, which always produces
22242 the auxiliary information. */
22243 return (cu->language == language_ada);
22244 }
22245
22246 /* Return the auxiliary type of the die in question using its
22247 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22248 attribute is not present. */
22249
22250 static struct type *
22251 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22252 {
22253 struct attribute *type_attr;
22254
22255 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22256 if (!type_attr)
22257 return NULL;
22258
22259 return lookup_die_type (die, type_attr, cu);
22260 }
22261
22262 /* If DIE has a descriptive_type attribute, then set the TYPE's
22263 descriptive type accordingly. */
22264
22265 static void
22266 set_descriptive_type (struct type *type, struct die_info *die,
22267 struct dwarf2_cu *cu)
22268 {
22269 struct type *descriptive_type = die_descriptive_type (die, cu);
22270
22271 if (descriptive_type)
22272 {
22273 ALLOCATE_GNAT_AUX_TYPE (type);
22274 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22275 }
22276 }
22277
22278 /* Return the containing type of the die in question using its
22279 DW_AT_containing_type attribute. */
22280
22281 static struct type *
22282 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22283 {
22284 struct attribute *type_attr;
22285 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22286
22287 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22288 if (!type_attr)
22289 error (_("Dwarf Error: Problem turning containing type into gdb type "
22290 "[in module %s]"), objfile_name (objfile));
22291
22292 return lookup_die_type (die, type_attr, cu);
22293 }
22294
22295 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22296
22297 static struct type *
22298 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22299 {
22300 struct dwarf2_per_objfile *dwarf2_per_objfile
22301 = cu->per_cu->dwarf2_per_objfile;
22302 struct objfile *objfile = dwarf2_per_objfile->objfile;
22303 char *message, *saved;
22304
22305 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
22306 objfile_name (objfile),
22307 sect_offset_str (cu->header.sect_off),
22308 sect_offset_str (die->sect_off));
22309 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
22310 message, strlen (message));
22311 xfree (message);
22312
22313 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22314 }
22315
22316 /* Look up the type of DIE in CU using its type attribute ATTR.
22317 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22318 DW_AT_containing_type.
22319 If there is no type substitute an error marker. */
22320
22321 static struct type *
22322 lookup_die_type (struct die_info *die, const struct attribute *attr,
22323 struct dwarf2_cu *cu)
22324 {
22325 struct dwarf2_per_objfile *dwarf2_per_objfile
22326 = cu->per_cu->dwarf2_per_objfile;
22327 struct objfile *objfile = dwarf2_per_objfile->objfile;
22328 struct type *this_type;
22329
22330 gdb_assert (attr->name == DW_AT_type
22331 || attr->name == DW_AT_GNAT_descriptive_type
22332 || attr->name == DW_AT_containing_type);
22333
22334 /* First see if we have it cached. */
22335
22336 if (attr->form == DW_FORM_GNU_ref_alt)
22337 {
22338 struct dwarf2_per_cu_data *per_cu;
22339 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22340
22341 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22342 dwarf2_per_objfile);
22343 this_type = get_die_type_at_offset (sect_off, per_cu);
22344 }
22345 else if (attr_form_is_ref (attr))
22346 {
22347 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22348
22349 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22350 }
22351 else if (attr->form == DW_FORM_ref_sig8)
22352 {
22353 ULONGEST signature = DW_SIGNATURE (attr);
22354
22355 return get_signatured_type (die, signature, cu);
22356 }
22357 else
22358 {
22359 complaint (&symfile_complaints,
22360 _("Dwarf Error: Bad type attribute %s in DIE"
22361 " at %s [in module %s]"),
22362 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22363 objfile_name (objfile));
22364 return build_error_marker_type (cu, die);
22365 }
22366
22367 /* If not cached we need to read it in. */
22368
22369 if (this_type == NULL)
22370 {
22371 struct die_info *type_die = NULL;
22372 struct dwarf2_cu *type_cu = cu;
22373
22374 if (attr_form_is_ref (attr))
22375 type_die = follow_die_ref (die, attr, &type_cu);
22376 if (type_die == NULL)
22377 return build_error_marker_type (cu, die);
22378 /* If we find the type now, it's probably because the type came
22379 from an inter-CU reference and the type's CU got expanded before
22380 ours. */
22381 this_type = read_type_die (type_die, type_cu);
22382 }
22383
22384 /* If we still don't have a type use an error marker. */
22385
22386 if (this_type == NULL)
22387 return build_error_marker_type (cu, die);
22388
22389 return this_type;
22390 }
22391
22392 /* Return the type in DIE, CU.
22393 Returns NULL for invalid types.
22394
22395 This first does a lookup in die_type_hash,
22396 and only reads the die in if necessary.
22397
22398 NOTE: This can be called when reading in partial or full symbols. */
22399
22400 static struct type *
22401 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22402 {
22403 struct type *this_type;
22404
22405 this_type = get_die_type (die, cu);
22406 if (this_type)
22407 return this_type;
22408
22409 return read_type_die_1 (die, cu);
22410 }
22411
22412 /* Read the type in DIE, CU.
22413 Returns NULL for invalid types. */
22414
22415 static struct type *
22416 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22417 {
22418 struct type *this_type = NULL;
22419
22420 switch (die->tag)
22421 {
22422 case DW_TAG_class_type:
22423 case DW_TAG_interface_type:
22424 case DW_TAG_structure_type:
22425 case DW_TAG_union_type:
22426 this_type = read_structure_type (die, cu);
22427 break;
22428 case DW_TAG_enumeration_type:
22429 this_type = read_enumeration_type (die, cu);
22430 break;
22431 case DW_TAG_subprogram:
22432 case DW_TAG_subroutine_type:
22433 case DW_TAG_inlined_subroutine:
22434 this_type = read_subroutine_type (die, cu);
22435 break;
22436 case DW_TAG_array_type:
22437 this_type = read_array_type (die, cu);
22438 break;
22439 case DW_TAG_set_type:
22440 this_type = read_set_type (die, cu);
22441 break;
22442 case DW_TAG_pointer_type:
22443 this_type = read_tag_pointer_type (die, cu);
22444 break;
22445 case DW_TAG_ptr_to_member_type:
22446 this_type = read_tag_ptr_to_member_type (die, cu);
22447 break;
22448 case DW_TAG_reference_type:
22449 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22450 break;
22451 case DW_TAG_rvalue_reference_type:
22452 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22453 break;
22454 case DW_TAG_const_type:
22455 this_type = read_tag_const_type (die, cu);
22456 break;
22457 case DW_TAG_volatile_type:
22458 this_type = read_tag_volatile_type (die, cu);
22459 break;
22460 case DW_TAG_restrict_type:
22461 this_type = read_tag_restrict_type (die, cu);
22462 break;
22463 case DW_TAG_string_type:
22464 this_type = read_tag_string_type (die, cu);
22465 break;
22466 case DW_TAG_typedef:
22467 this_type = read_typedef (die, cu);
22468 break;
22469 case DW_TAG_subrange_type:
22470 this_type = read_subrange_type (die, cu);
22471 break;
22472 case DW_TAG_base_type:
22473 this_type = read_base_type (die, cu);
22474 break;
22475 case DW_TAG_unspecified_type:
22476 this_type = read_unspecified_type (die, cu);
22477 break;
22478 case DW_TAG_namespace:
22479 this_type = read_namespace_type (die, cu);
22480 break;
22481 case DW_TAG_module:
22482 this_type = read_module_type (die, cu);
22483 break;
22484 case DW_TAG_atomic_type:
22485 this_type = read_tag_atomic_type (die, cu);
22486 break;
22487 default:
22488 complaint (&symfile_complaints,
22489 _("unexpected tag in read_type_die: '%s'"),
22490 dwarf_tag_name (die->tag));
22491 break;
22492 }
22493
22494 return this_type;
22495 }
22496
22497 /* See if we can figure out if the class lives in a namespace. We do
22498 this by looking for a member function; its demangled name will
22499 contain namespace info, if there is any.
22500 Return the computed name or NULL.
22501 Space for the result is allocated on the objfile's obstack.
22502 This is the full-die version of guess_partial_die_structure_name.
22503 In this case we know DIE has no useful parent. */
22504
22505 static char *
22506 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22507 {
22508 struct die_info *spec_die;
22509 struct dwarf2_cu *spec_cu;
22510 struct die_info *child;
22511 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22512
22513 spec_cu = cu;
22514 spec_die = die_specification (die, &spec_cu);
22515 if (spec_die != NULL)
22516 {
22517 die = spec_die;
22518 cu = spec_cu;
22519 }
22520
22521 for (child = die->child;
22522 child != NULL;
22523 child = child->sibling)
22524 {
22525 if (child->tag == DW_TAG_subprogram)
22526 {
22527 const char *linkage_name = dw2_linkage_name (child, cu);
22528
22529 if (linkage_name != NULL)
22530 {
22531 char *actual_name
22532 = language_class_name_from_physname (cu->language_defn,
22533 linkage_name);
22534 char *name = NULL;
22535
22536 if (actual_name != NULL)
22537 {
22538 const char *die_name = dwarf2_name (die, cu);
22539
22540 if (die_name != NULL
22541 && strcmp (die_name, actual_name) != 0)
22542 {
22543 /* Strip off the class name from the full name.
22544 We want the prefix. */
22545 int die_name_len = strlen (die_name);
22546 int actual_name_len = strlen (actual_name);
22547
22548 /* Test for '::' as a sanity check. */
22549 if (actual_name_len > die_name_len + 2
22550 && actual_name[actual_name_len
22551 - die_name_len - 1] == ':')
22552 name = (char *) obstack_copy0 (
22553 &objfile->per_bfd->storage_obstack,
22554 actual_name, actual_name_len - die_name_len - 2);
22555 }
22556 }
22557 xfree (actual_name);
22558 return name;
22559 }
22560 }
22561 }
22562
22563 return NULL;
22564 }
22565
22566 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22567 prefix part in such case. See
22568 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22569
22570 static const char *
22571 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22572 {
22573 struct attribute *attr;
22574 const char *base;
22575
22576 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22577 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22578 return NULL;
22579
22580 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22581 return NULL;
22582
22583 attr = dw2_linkage_name_attr (die, cu);
22584 if (attr == NULL || DW_STRING (attr) == NULL)
22585 return NULL;
22586
22587 /* dwarf2_name had to be already called. */
22588 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22589
22590 /* Strip the base name, keep any leading namespaces/classes. */
22591 base = strrchr (DW_STRING (attr), ':');
22592 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22593 return "";
22594
22595 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22596 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22597 DW_STRING (attr),
22598 &base[-1] - DW_STRING (attr));
22599 }
22600
22601 /* Return the name of the namespace/class that DIE is defined within,
22602 or "" if we can't tell. The caller should not xfree the result.
22603
22604 For example, if we're within the method foo() in the following
22605 code:
22606
22607 namespace N {
22608 class C {
22609 void foo () {
22610 }
22611 };
22612 }
22613
22614 then determine_prefix on foo's die will return "N::C". */
22615
22616 static const char *
22617 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22618 {
22619 struct dwarf2_per_objfile *dwarf2_per_objfile
22620 = cu->per_cu->dwarf2_per_objfile;
22621 struct die_info *parent, *spec_die;
22622 struct dwarf2_cu *spec_cu;
22623 struct type *parent_type;
22624 const char *retval;
22625
22626 if (cu->language != language_cplus
22627 && cu->language != language_fortran && cu->language != language_d
22628 && cu->language != language_rust)
22629 return "";
22630
22631 retval = anonymous_struct_prefix (die, cu);
22632 if (retval)
22633 return retval;
22634
22635 /* We have to be careful in the presence of DW_AT_specification.
22636 For example, with GCC 3.4, given the code
22637
22638 namespace N {
22639 void foo() {
22640 // Definition of N::foo.
22641 }
22642 }
22643
22644 then we'll have a tree of DIEs like this:
22645
22646 1: DW_TAG_compile_unit
22647 2: DW_TAG_namespace // N
22648 3: DW_TAG_subprogram // declaration of N::foo
22649 4: DW_TAG_subprogram // definition of N::foo
22650 DW_AT_specification // refers to die #3
22651
22652 Thus, when processing die #4, we have to pretend that we're in
22653 the context of its DW_AT_specification, namely the contex of die
22654 #3. */
22655 spec_cu = cu;
22656 spec_die = die_specification (die, &spec_cu);
22657 if (spec_die == NULL)
22658 parent = die->parent;
22659 else
22660 {
22661 parent = spec_die->parent;
22662 cu = spec_cu;
22663 }
22664
22665 if (parent == NULL)
22666 return "";
22667 else if (parent->building_fullname)
22668 {
22669 const char *name;
22670 const char *parent_name;
22671
22672 /* It has been seen on RealView 2.2 built binaries,
22673 DW_TAG_template_type_param types actually _defined_ as
22674 children of the parent class:
22675
22676 enum E {};
22677 template class <class Enum> Class{};
22678 Class<enum E> class_e;
22679
22680 1: DW_TAG_class_type (Class)
22681 2: DW_TAG_enumeration_type (E)
22682 3: DW_TAG_enumerator (enum1:0)
22683 3: DW_TAG_enumerator (enum2:1)
22684 ...
22685 2: DW_TAG_template_type_param
22686 DW_AT_type DW_FORM_ref_udata (E)
22687
22688 Besides being broken debug info, it can put GDB into an
22689 infinite loop. Consider:
22690
22691 When we're building the full name for Class<E>, we'll start
22692 at Class, and go look over its template type parameters,
22693 finding E. We'll then try to build the full name of E, and
22694 reach here. We're now trying to build the full name of E,
22695 and look over the parent DIE for containing scope. In the
22696 broken case, if we followed the parent DIE of E, we'd again
22697 find Class, and once again go look at its template type
22698 arguments, etc., etc. Simply don't consider such parent die
22699 as source-level parent of this die (it can't be, the language
22700 doesn't allow it), and break the loop here. */
22701 name = dwarf2_name (die, cu);
22702 parent_name = dwarf2_name (parent, cu);
22703 complaint (&symfile_complaints,
22704 _("template param type '%s' defined within parent '%s'"),
22705 name ? name : "<unknown>",
22706 parent_name ? parent_name : "<unknown>");
22707 return "";
22708 }
22709 else
22710 switch (parent->tag)
22711 {
22712 case DW_TAG_namespace:
22713 parent_type = read_type_die (parent, cu);
22714 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22715 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22716 Work around this problem here. */
22717 if (cu->language == language_cplus
22718 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22719 return "";
22720 /* We give a name to even anonymous namespaces. */
22721 return TYPE_TAG_NAME (parent_type);
22722 case DW_TAG_class_type:
22723 case DW_TAG_interface_type:
22724 case DW_TAG_structure_type:
22725 case DW_TAG_union_type:
22726 case DW_TAG_module:
22727 parent_type = read_type_die (parent, cu);
22728 if (TYPE_TAG_NAME (parent_type) != NULL)
22729 return TYPE_TAG_NAME (parent_type);
22730 else
22731 /* An anonymous structure is only allowed non-static data
22732 members; no typedefs, no member functions, et cetera.
22733 So it does not need a prefix. */
22734 return "";
22735 case DW_TAG_compile_unit:
22736 case DW_TAG_partial_unit:
22737 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22738 if (cu->language == language_cplus
22739 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22740 && die->child != NULL
22741 && (die->tag == DW_TAG_class_type
22742 || die->tag == DW_TAG_structure_type
22743 || die->tag == DW_TAG_union_type))
22744 {
22745 char *name = guess_full_die_structure_name (die, cu);
22746 if (name != NULL)
22747 return name;
22748 }
22749 return "";
22750 case DW_TAG_enumeration_type:
22751 parent_type = read_type_die (parent, cu);
22752 if (TYPE_DECLARED_CLASS (parent_type))
22753 {
22754 if (TYPE_TAG_NAME (parent_type) != NULL)
22755 return TYPE_TAG_NAME (parent_type);
22756 return "";
22757 }
22758 /* Fall through. */
22759 default:
22760 return determine_prefix (parent, cu);
22761 }
22762 }
22763
22764 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22765 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22766 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22767 an obconcat, otherwise allocate storage for the result. The CU argument is
22768 used to determine the language and hence, the appropriate separator. */
22769
22770 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22771
22772 static char *
22773 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22774 int physname, struct dwarf2_cu *cu)
22775 {
22776 const char *lead = "";
22777 const char *sep;
22778
22779 if (suffix == NULL || suffix[0] == '\0'
22780 || prefix == NULL || prefix[0] == '\0')
22781 sep = "";
22782 else if (cu->language == language_d)
22783 {
22784 /* For D, the 'main' function could be defined in any module, but it
22785 should never be prefixed. */
22786 if (strcmp (suffix, "D main") == 0)
22787 {
22788 prefix = "";
22789 sep = "";
22790 }
22791 else
22792 sep = ".";
22793 }
22794 else if (cu->language == language_fortran && physname)
22795 {
22796 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22797 DW_AT_MIPS_linkage_name is preferred and used instead. */
22798
22799 lead = "__";
22800 sep = "_MOD_";
22801 }
22802 else
22803 sep = "::";
22804
22805 if (prefix == NULL)
22806 prefix = "";
22807 if (suffix == NULL)
22808 suffix = "";
22809
22810 if (obs == NULL)
22811 {
22812 char *retval
22813 = ((char *)
22814 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22815
22816 strcpy (retval, lead);
22817 strcat (retval, prefix);
22818 strcat (retval, sep);
22819 strcat (retval, suffix);
22820 return retval;
22821 }
22822 else
22823 {
22824 /* We have an obstack. */
22825 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22826 }
22827 }
22828
22829 /* Return sibling of die, NULL if no sibling. */
22830
22831 static struct die_info *
22832 sibling_die (struct die_info *die)
22833 {
22834 return die->sibling;
22835 }
22836
22837 /* Get name of a die, return NULL if not found. */
22838
22839 static const char *
22840 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22841 struct obstack *obstack)
22842 {
22843 if (name && cu->language == language_cplus)
22844 {
22845 std::string canon_name = cp_canonicalize_string (name);
22846
22847 if (!canon_name.empty ())
22848 {
22849 if (canon_name != name)
22850 name = (const char *) obstack_copy0 (obstack,
22851 canon_name.c_str (),
22852 canon_name.length ());
22853 }
22854 }
22855
22856 return name;
22857 }
22858
22859 /* Get name of a die, return NULL if not found.
22860 Anonymous namespaces are converted to their magic string. */
22861
22862 static const char *
22863 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22864 {
22865 struct attribute *attr;
22866 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22867
22868 attr = dwarf2_attr (die, DW_AT_name, cu);
22869 if ((!attr || !DW_STRING (attr))
22870 && die->tag != DW_TAG_namespace
22871 && die->tag != DW_TAG_class_type
22872 && die->tag != DW_TAG_interface_type
22873 && die->tag != DW_TAG_structure_type
22874 && die->tag != DW_TAG_union_type)
22875 return NULL;
22876
22877 switch (die->tag)
22878 {
22879 case DW_TAG_compile_unit:
22880 case DW_TAG_partial_unit:
22881 /* Compilation units have a DW_AT_name that is a filename, not
22882 a source language identifier. */
22883 case DW_TAG_enumeration_type:
22884 case DW_TAG_enumerator:
22885 /* These tags always have simple identifiers already; no need
22886 to canonicalize them. */
22887 return DW_STRING (attr);
22888
22889 case DW_TAG_namespace:
22890 if (attr != NULL && DW_STRING (attr) != NULL)
22891 return DW_STRING (attr);
22892 return CP_ANONYMOUS_NAMESPACE_STR;
22893
22894 case DW_TAG_class_type:
22895 case DW_TAG_interface_type:
22896 case DW_TAG_structure_type:
22897 case DW_TAG_union_type:
22898 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22899 structures or unions. These were of the form "._%d" in GCC 4.1,
22900 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22901 and GCC 4.4. We work around this problem by ignoring these. */
22902 if (attr && DW_STRING (attr)
22903 && (startswith (DW_STRING (attr), "._")
22904 || startswith (DW_STRING (attr), "<anonymous")))
22905 return NULL;
22906
22907 /* GCC might emit a nameless typedef that has a linkage name. See
22908 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22909 if (!attr || DW_STRING (attr) == NULL)
22910 {
22911 char *demangled = NULL;
22912
22913 attr = dw2_linkage_name_attr (die, cu);
22914 if (attr == NULL || DW_STRING (attr) == NULL)
22915 return NULL;
22916
22917 /* Avoid demangling DW_STRING (attr) the second time on a second
22918 call for the same DIE. */
22919 if (!DW_STRING_IS_CANONICAL (attr))
22920 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22921
22922 if (demangled)
22923 {
22924 const char *base;
22925
22926 /* FIXME: we already did this for the partial symbol... */
22927 DW_STRING (attr)
22928 = ((const char *)
22929 obstack_copy0 (&objfile->per_bfd->storage_obstack,
22930 demangled, strlen (demangled)));
22931 DW_STRING_IS_CANONICAL (attr) = 1;
22932 xfree (demangled);
22933
22934 /* Strip any leading namespaces/classes, keep only the base name.
22935 DW_AT_name for named DIEs does not contain the prefixes. */
22936 base = strrchr (DW_STRING (attr), ':');
22937 if (base && base > DW_STRING (attr) && base[-1] == ':')
22938 return &base[1];
22939 else
22940 return DW_STRING (attr);
22941 }
22942 }
22943 break;
22944
22945 default:
22946 break;
22947 }
22948
22949 if (!DW_STRING_IS_CANONICAL (attr))
22950 {
22951 DW_STRING (attr)
22952 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22953 &objfile->per_bfd->storage_obstack);
22954 DW_STRING_IS_CANONICAL (attr) = 1;
22955 }
22956 return DW_STRING (attr);
22957 }
22958
22959 /* Return the die that this die in an extension of, or NULL if there
22960 is none. *EXT_CU is the CU containing DIE on input, and the CU
22961 containing the return value on output. */
22962
22963 static struct die_info *
22964 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22965 {
22966 struct attribute *attr;
22967
22968 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22969 if (attr == NULL)
22970 return NULL;
22971
22972 return follow_die_ref (die, attr, ext_cu);
22973 }
22974
22975 /* Convert a DIE tag into its string name. */
22976
22977 static const char *
22978 dwarf_tag_name (unsigned tag)
22979 {
22980 const char *name = get_DW_TAG_name (tag);
22981
22982 if (name == NULL)
22983 return "DW_TAG_<unknown>";
22984
22985 return name;
22986 }
22987
22988 /* Convert a DWARF attribute code into its string name. */
22989
22990 static const char *
22991 dwarf_attr_name (unsigned attr)
22992 {
22993 const char *name;
22994
22995 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22996 if (attr == DW_AT_MIPS_fde)
22997 return "DW_AT_MIPS_fde";
22998 #else
22999 if (attr == DW_AT_HP_block_index)
23000 return "DW_AT_HP_block_index";
23001 #endif
23002
23003 name = get_DW_AT_name (attr);
23004
23005 if (name == NULL)
23006 return "DW_AT_<unknown>";
23007
23008 return name;
23009 }
23010
23011 /* Convert a DWARF value form code into its string name. */
23012
23013 static const char *
23014 dwarf_form_name (unsigned form)
23015 {
23016 const char *name = get_DW_FORM_name (form);
23017
23018 if (name == NULL)
23019 return "DW_FORM_<unknown>";
23020
23021 return name;
23022 }
23023
23024 static const char *
23025 dwarf_bool_name (unsigned mybool)
23026 {
23027 if (mybool)
23028 return "TRUE";
23029 else
23030 return "FALSE";
23031 }
23032
23033 /* Convert a DWARF type code into its string name. */
23034
23035 static const char *
23036 dwarf_type_encoding_name (unsigned enc)
23037 {
23038 const char *name = get_DW_ATE_name (enc);
23039
23040 if (name == NULL)
23041 return "DW_ATE_<unknown>";
23042
23043 return name;
23044 }
23045
23046 static void
23047 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23048 {
23049 unsigned int i;
23050
23051 print_spaces (indent, f);
23052 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23053 dwarf_tag_name (die->tag), die->abbrev,
23054 sect_offset_str (die->sect_off));
23055
23056 if (die->parent != NULL)
23057 {
23058 print_spaces (indent, f);
23059 fprintf_unfiltered (f, " parent at offset: %s\n",
23060 sect_offset_str (die->parent->sect_off));
23061 }
23062
23063 print_spaces (indent, f);
23064 fprintf_unfiltered (f, " has children: %s\n",
23065 dwarf_bool_name (die->child != NULL));
23066
23067 print_spaces (indent, f);
23068 fprintf_unfiltered (f, " attributes:\n");
23069
23070 for (i = 0; i < die->num_attrs; ++i)
23071 {
23072 print_spaces (indent, f);
23073 fprintf_unfiltered (f, " %s (%s) ",
23074 dwarf_attr_name (die->attrs[i].name),
23075 dwarf_form_name (die->attrs[i].form));
23076
23077 switch (die->attrs[i].form)
23078 {
23079 case DW_FORM_addr:
23080 case DW_FORM_GNU_addr_index:
23081 fprintf_unfiltered (f, "address: ");
23082 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
23083 break;
23084 case DW_FORM_block2:
23085 case DW_FORM_block4:
23086 case DW_FORM_block:
23087 case DW_FORM_block1:
23088 fprintf_unfiltered (f, "block: size %s",
23089 pulongest (DW_BLOCK (&die->attrs[i])->size));
23090 break;
23091 case DW_FORM_exprloc:
23092 fprintf_unfiltered (f, "expression: size %s",
23093 pulongest (DW_BLOCK (&die->attrs[i])->size));
23094 break;
23095 case DW_FORM_data16:
23096 fprintf_unfiltered (f, "constant of 16 bytes");
23097 break;
23098 case DW_FORM_ref_addr:
23099 fprintf_unfiltered (f, "ref address: ");
23100 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23101 break;
23102 case DW_FORM_GNU_ref_alt:
23103 fprintf_unfiltered (f, "alt ref address: ");
23104 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23105 break;
23106 case DW_FORM_ref1:
23107 case DW_FORM_ref2:
23108 case DW_FORM_ref4:
23109 case DW_FORM_ref8:
23110 case DW_FORM_ref_udata:
23111 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23112 (long) (DW_UNSND (&die->attrs[i])));
23113 break;
23114 case DW_FORM_data1:
23115 case DW_FORM_data2:
23116 case DW_FORM_data4:
23117 case DW_FORM_data8:
23118 case DW_FORM_udata:
23119 case DW_FORM_sdata:
23120 fprintf_unfiltered (f, "constant: %s",
23121 pulongest (DW_UNSND (&die->attrs[i])));
23122 break;
23123 case DW_FORM_sec_offset:
23124 fprintf_unfiltered (f, "section offset: %s",
23125 pulongest (DW_UNSND (&die->attrs[i])));
23126 break;
23127 case DW_FORM_ref_sig8:
23128 fprintf_unfiltered (f, "signature: %s",
23129 hex_string (DW_SIGNATURE (&die->attrs[i])));
23130 break;
23131 case DW_FORM_string:
23132 case DW_FORM_strp:
23133 case DW_FORM_line_strp:
23134 case DW_FORM_GNU_str_index:
23135 case DW_FORM_GNU_strp_alt:
23136 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23137 DW_STRING (&die->attrs[i])
23138 ? DW_STRING (&die->attrs[i]) : "",
23139 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23140 break;
23141 case DW_FORM_flag:
23142 if (DW_UNSND (&die->attrs[i]))
23143 fprintf_unfiltered (f, "flag: TRUE");
23144 else
23145 fprintf_unfiltered (f, "flag: FALSE");
23146 break;
23147 case DW_FORM_flag_present:
23148 fprintf_unfiltered (f, "flag: TRUE");
23149 break;
23150 case DW_FORM_indirect:
23151 /* The reader will have reduced the indirect form to
23152 the "base form" so this form should not occur. */
23153 fprintf_unfiltered (f,
23154 "unexpected attribute form: DW_FORM_indirect");
23155 break;
23156 case DW_FORM_implicit_const:
23157 fprintf_unfiltered (f, "constant: %s",
23158 plongest (DW_SND (&die->attrs[i])));
23159 break;
23160 default:
23161 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23162 die->attrs[i].form);
23163 break;
23164 }
23165 fprintf_unfiltered (f, "\n");
23166 }
23167 }
23168
23169 static void
23170 dump_die_for_error (struct die_info *die)
23171 {
23172 dump_die_shallow (gdb_stderr, 0, die);
23173 }
23174
23175 static void
23176 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23177 {
23178 int indent = level * 4;
23179
23180 gdb_assert (die != NULL);
23181
23182 if (level >= max_level)
23183 return;
23184
23185 dump_die_shallow (f, indent, die);
23186
23187 if (die->child != NULL)
23188 {
23189 print_spaces (indent, f);
23190 fprintf_unfiltered (f, " Children:");
23191 if (level + 1 < max_level)
23192 {
23193 fprintf_unfiltered (f, "\n");
23194 dump_die_1 (f, level + 1, max_level, die->child);
23195 }
23196 else
23197 {
23198 fprintf_unfiltered (f,
23199 " [not printed, max nesting level reached]\n");
23200 }
23201 }
23202
23203 if (die->sibling != NULL && level > 0)
23204 {
23205 dump_die_1 (f, level, max_level, die->sibling);
23206 }
23207 }
23208
23209 /* This is called from the pdie macro in gdbinit.in.
23210 It's not static so gcc will keep a copy callable from gdb. */
23211
23212 void
23213 dump_die (struct die_info *die, int max_level)
23214 {
23215 dump_die_1 (gdb_stdlog, 0, max_level, die);
23216 }
23217
23218 static void
23219 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23220 {
23221 void **slot;
23222
23223 slot = htab_find_slot_with_hash (cu->die_hash, die,
23224 to_underlying (die->sect_off),
23225 INSERT);
23226
23227 *slot = die;
23228 }
23229
23230 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23231 required kind. */
23232
23233 static sect_offset
23234 dwarf2_get_ref_die_offset (const struct attribute *attr)
23235 {
23236 if (attr_form_is_ref (attr))
23237 return (sect_offset) DW_UNSND (attr);
23238
23239 complaint (&symfile_complaints,
23240 _("unsupported die ref attribute form: '%s'"),
23241 dwarf_form_name (attr->form));
23242 return {};
23243 }
23244
23245 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23246 * the value held by the attribute is not constant. */
23247
23248 static LONGEST
23249 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23250 {
23251 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23252 return DW_SND (attr);
23253 else if (attr->form == DW_FORM_udata
23254 || attr->form == DW_FORM_data1
23255 || attr->form == DW_FORM_data2
23256 || attr->form == DW_FORM_data4
23257 || attr->form == DW_FORM_data8)
23258 return DW_UNSND (attr);
23259 else
23260 {
23261 /* For DW_FORM_data16 see attr_form_is_constant. */
23262 complaint (&symfile_complaints,
23263 _("Attribute value is not a constant (%s)"),
23264 dwarf_form_name (attr->form));
23265 return default_value;
23266 }
23267 }
23268
23269 /* Follow reference or signature attribute ATTR of SRC_DIE.
23270 On entry *REF_CU is the CU of SRC_DIE.
23271 On exit *REF_CU is the CU of the result. */
23272
23273 static struct die_info *
23274 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23275 struct dwarf2_cu **ref_cu)
23276 {
23277 struct die_info *die;
23278
23279 if (attr_form_is_ref (attr))
23280 die = follow_die_ref (src_die, attr, ref_cu);
23281 else if (attr->form == DW_FORM_ref_sig8)
23282 die = follow_die_sig (src_die, attr, ref_cu);
23283 else
23284 {
23285 dump_die_for_error (src_die);
23286 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23287 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23288 }
23289
23290 return die;
23291 }
23292
23293 /* Follow reference OFFSET.
23294 On entry *REF_CU is the CU of the source die referencing OFFSET.
23295 On exit *REF_CU is the CU of the result.
23296 Returns NULL if OFFSET is invalid. */
23297
23298 static struct die_info *
23299 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23300 struct dwarf2_cu **ref_cu)
23301 {
23302 struct die_info temp_die;
23303 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23304 struct dwarf2_per_objfile *dwarf2_per_objfile
23305 = cu->per_cu->dwarf2_per_objfile;
23306 struct objfile *objfile = dwarf2_per_objfile->objfile;
23307
23308 gdb_assert (cu->per_cu != NULL);
23309
23310 target_cu = cu;
23311
23312 if (cu->per_cu->is_debug_types)
23313 {
23314 /* .debug_types CUs cannot reference anything outside their CU.
23315 If they need to, they have to reference a signatured type via
23316 DW_FORM_ref_sig8. */
23317 if (!offset_in_cu_p (&cu->header, sect_off))
23318 return NULL;
23319 }
23320 else if (offset_in_dwz != cu->per_cu->is_dwz
23321 || !offset_in_cu_p (&cu->header, sect_off))
23322 {
23323 struct dwarf2_per_cu_data *per_cu;
23324
23325 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23326 dwarf2_per_objfile);
23327
23328 /* If necessary, add it to the queue and load its DIEs. */
23329 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23330 load_full_comp_unit (per_cu, cu->language);
23331
23332 target_cu = per_cu->cu;
23333 }
23334 else if (cu->dies == NULL)
23335 {
23336 /* We're loading full DIEs during partial symbol reading. */
23337 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23338 load_full_comp_unit (cu->per_cu, language_minimal);
23339 }
23340
23341 *ref_cu = target_cu;
23342 temp_die.sect_off = sect_off;
23343 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23344 &temp_die,
23345 to_underlying (sect_off));
23346 }
23347
23348 /* Follow reference attribute ATTR of SRC_DIE.
23349 On entry *REF_CU is the CU of SRC_DIE.
23350 On exit *REF_CU is the CU of the result. */
23351
23352 static struct die_info *
23353 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23354 struct dwarf2_cu **ref_cu)
23355 {
23356 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23357 struct dwarf2_cu *cu = *ref_cu;
23358 struct die_info *die;
23359
23360 die = follow_die_offset (sect_off,
23361 (attr->form == DW_FORM_GNU_ref_alt
23362 || cu->per_cu->is_dwz),
23363 ref_cu);
23364 if (!die)
23365 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23366 "at %s [in module %s]"),
23367 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23368 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23369
23370 return die;
23371 }
23372
23373 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23374 Returned value is intended for DW_OP_call*. Returned
23375 dwarf2_locexpr_baton->data has lifetime of
23376 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23377
23378 struct dwarf2_locexpr_baton
23379 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23380 struct dwarf2_per_cu_data *per_cu,
23381 CORE_ADDR (*get_frame_pc) (void *baton),
23382 void *baton)
23383 {
23384 struct dwarf2_cu *cu;
23385 struct die_info *die;
23386 struct attribute *attr;
23387 struct dwarf2_locexpr_baton retval;
23388 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23389 struct dwarf2_per_objfile *dwarf2_per_objfile
23390 = get_dwarf2_per_objfile (objfile);
23391
23392 if (per_cu->cu == NULL)
23393 load_cu (per_cu);
23394 cu = per_cu->cu;
23395 if (cu == NULL)
23396 {
23397 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23398 Instead just throw an error, not much else we can do. */
23399 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23400 sect_offset_str (sect_off), objfile_name (objfile));
23401 }
23402
23403 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23404 if (!die)
23405 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23406 sect_offset_str (sect_off), objfile_name (objfile));
23407
23408 attr = dwarf2_attr (die, DW_AT_location, cu);
23409 if (!attr)
23410 {
23411 /* DWARF: "If there is no such attribute, then there is no effect.".
23412 DATA is ignored if SIZE is 0. */
23413
23414 retval.data = NULL;
23415 retval.size = 0;
23416 }
23417 else if (attr_form_is_section_offset (attr))
23418 {
23419 struct dwarf2_loclist_baton loclist_baton;
23420 CORE_ADDR pc = (*get_frame_pc) (baton);
23421 size_t size;
23422
23423 fill_in_loclist_baton (cu, &loclist_baton, attr);
23424
23425 retval.data = dwarf2_find_location_expression (&loclist_baton,
23426 &size, pc);
23427 retval.size = size;
23428 }
23429 else
23430 {
23431 if (!attr_form_is_block (attr))
23432 error (_("Dwarf Error: DIE at %s referenced in module %s "
23433 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23434 sect_offset_str (sect_off), objfile_name (objfile));
23435
23436 retval.data = DW_BLOCK (attr)->data;
23437 retval.size = DW_BLOCK (attr)->size;
23438 }
23439 retval.per_cu = cu->per_cu;
23440
23441 age_cached_comp_units (dwarf2_per_objfile);
23442
23443 return retval;
23444 }
23445
23446 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23447 offset. */
23448
23449 struct dwarf2_locexpr_baton
23450 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23451 struct dwarf2_per_cu_data *per_cu,
23452 CORE_ADDR (*get_frame_pc) (void *baton),
23453 void *baton)
23454 {
23455 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23456
23457 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23458 }
23459
23460 /* Write a constant of a given type as target-ordered bytes into
23461 OBSTACK. */
23462
23463 static const gdb_byte *
23464 write_constant_as_bytes (struct obstack *obstack,
23465 enum bfd_endian byte_order,
23466 struct type *type,
23467 ULONGEST value,
23468 LONGEST *len)
23469 {
23470 gdb_byte *result;
23471
23472 *len = TYPE_LENGTH (type);
23473 result = (gdb_byte *) obstack_alloc (obstack, *len);
23474 store_unsigned_integer (result, *len, byte_order, value);
23475
23476 return result;
23477 }
23478
23479 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23480 pointer to the constant bytes and set LEN to the length of the
23481 data. If memory is needed, allocate it on OBSTACK. If the DIE
23482 does not have a DW_AT_const_value, return NULL. */
23483
23484 const gdb_byte *
23485 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23486 struct dwarf2_per_cu_data *per_cu,
23487 struct obstack *obstack,
23488 LONGEST *len)
23489 {
23490 struct dwarf2_cu *cu;
23491 struct die_info *die;
23492 struct attribute *attr;
23493 const gdb_byte *result = NULL;
23494 struct type *type;
23495 LONGEST value;
23496 enum bfd_endian byte_order;
23497 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23498
23499 if (per_cu->cu == NULL)
23500 load_cu (per_cu);
23501 cu = per_cu->cu;
23502 if (cu == NULL)
23503 {
23504 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23505 Instead just throw an error, not much else we can do. */
23506 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23507 sect_offset_str (sect_off), objfile_name (objfile));
23508 }
23509
23510 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23511 if (!die)
23512 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23513 sect_offset_str (sect_off), objfile_name (objfile));
23514
23515 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23516 if (attr == NULL)
23517 return NULL;
23518
23519 byte_order = (bfd_big_endian (objfile->obfd)
23520 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23521
23522 switch (attr->form)
23523 {
23524 case DW_FORM_addr:
23525 case DW_FORM_GNU_addr_index:
23526 {
23527 gdb_byte *tem;
23528
23529 *len = cu->header.addr_size;
23530 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23531 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23532 result = tem;
23533 }
23534 break;
23535 case DW_FORM_string:
23536 case DW_FORM_strp:
23537 case DW_FORM_GNU_str_index:
23538 case DW_FORM_GNU_strp_alt:
23539 /* DW_STRING is already allocated on the objfile obstack, point
23540 directly to it. */
23541 result = (const gdb_byte *) DW_STRING (attr);
23542 *len = strlen (DW_STRING (attr));
23543 break;
23544 case DW_FORM_block1:
23545 case DW_FORM_block2:
23546 case DW_FORM_block4:
23547 case DW_FORM_block:
23548 case DW_FORM_exprloc:
23549 case DW_FORM_data16:
23550 result = DW_BLOCK (attr)->data;
23551 *len = DW_BLOCK (attr)->size;
23552 break;
23553
23554 /* The DW_AT_const_value attributes are supposed to carry the
23555 symbol's value "represented as it would be on the target
23556 architecture." By the time we get here, it's already been
23557 converted to host endianness, so we just need to sign- or
23558 zero-extend it as appropriate. */
23559 case DW_FORM_data1:
23560 type = die_type (die, cu);
23561 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23562 if (result == NULL)
23563 result = write_constant_as_bytes (obstack, byte_order,
23564 type, value, len);
23565 break;
23566 case DW_FORM_data2:
23567 type = die_type (die, cu);
23568 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23569 if (result == NULL)
23570 result = write_constant_as_bytes (obstack, byte_order,
23571 type, value, len);
23572 break;
23573 case DW_FORM_data4:
23574 type = die_type (die, cu);
23575 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23576 if (result == NULL)
23577 result = write_constant_as_bytes (obstack, byte_order,
23578 type, value, len);
23579 break;
23580 case DW_FORM_data8:
23581 type = die_type (die, cu);
23582 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23583 if (result == NULL)
23584 result = write_constant_as_bytes (obstack, byte_order,
23585 type, value, len);
23586 break;
23587
23588 case DW_FORM_sdata:
23589 case DW_FORM_implicit_const:
23590 type = die_type (die, cu);
23591 result = write_constant_as_bytes (obstack, byte_order,
23592 type, DW_SND (attr), len);
23593 break;
23594
23595 case DW_FORM_udata:
23596 type = die_type (die, cu);
23597 result = write_constant_as_bytes (obstack, byte_order,
23598 type, DW_UNSND (attr), len);
23599 break;
23600
23601 default:
23602 complaint (&symfile_complaints,
23603 _("unsupported const value attribute form: '%s'"),
23604 dwarf_form_name (attr->form));
23605 break;
23606 }
23607
23608 return result;
23609 }
23610
23611 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23612 valid type for this die is found. */
23613
23614 struct type *
23615 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23616 struct dwarf2_per_cu_data *per_cu)
23617 {
23618 struct dwarf2_cu *cu;
23619 struct die_info *die;
23620
23621 if (per_cu->cu == NULL)
23622 load_cu (per_cu);
23623 cu = per_cu->cu;
23624 if (!cu)
23625 return NULL;
23626
23627 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23628 if (!die)
23629 return NULL;
23630
23631 return die_type (die, cu);
23632 }
23633
23634 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23635 PER_CU. */
23636
23637 struct type *
23638 dwarf2_get_die_type (cu_offset die_offset,
23639 struct dwarf2_per_cu_data *per_cu)
23640 {
23641 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23642 return get_die_type_at_offset (die_offset_sect, per_cu);
23643 }
23644
23645 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23646 On entry *REF_CU is the CU of SRC_DIE.
23647 On exit *REF_CU is the CU of the result.
23648 Returns NULL if the referenced DIE isn't found. */
23649
23650 static struct die_info *
23651 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23652 struct dwarf2_cu **ref_cu)
23653 {
23654 struct die_info temp_die;
23655 struct dwarf2_cu *sig_cu;
23656 struct die_info *die;
23657
23658 /* While it might be nice to assert sig_type->type == NULL here,
23659 we can get here for DW_AT_imported_declaration where we need
23660 the DIE not the type. */
23661
23662 /* If necessary, add it to the queue and load its DIEs. */
23663
23664 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23665 read_signatured_type (sig_type);
23666
23667 sig_cu = sig_type->per_cu.cu;
23668 gdb_assert (sig_cu != NULL);
23669 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23670 temp_die.sect_off = sig_type->type_offset_in_section;
23671 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23672 to_underlying (temp_die.sect_off));
23673 if (die)
23674 {
23675 struct dwarf2_per_objfile *dwarf2_per_objfile
23676 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23677
23678 /* For .gdb_index version 7 keep track of included TUs.
23679 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23680 if (dwarf2_per_objfile->index_table != NULL
23681 && dwarf2_per_objfile->index_table->version <= 7)
23682 {
23683 VEC_safe_push (dwarf2_per_cu_ptr,
23684 (*ref_cu)->per_cu->imported_symtabs,
23685 sig_cu->per_cu);
23686 }
23687
23688 *ref_cu = sig_cu;
23689 return die;
23690 }
23691
23692 return NULL;
23693 }
23694
23695 /* Follow signatured type referenced by ATTR in SRC_DIE.
23696 On entry *REF_CU is the CU of SRC_DIE.
23697 On exit *REF_CU is the CU of the result.
23698 The result is the DIE of the type.
23699 If the referenced type cannot be found an error is thrown. */
23700
23701 static struct die_info *
23702 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23703 struct dwarf2_cu **ref_cu)
23704 {
23705 ULONGEST signature = DW_SIGNATURE (attr);
23706 struct signatured_type *sig_type;
23707 struct die_info *die;
23708
23709 gdb_assert (attr->form == DW_FORM_ref_sig8);
23710
23711 sig_type = lookup_signatured_type (*ref_cu, signature);
23712 /* sig_type will be NULL if the signatured type is missing from
23713 the debug info. */
23714 if (sig_type == NULL)
23715 {
23716 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23717 " from DIE at %s [in module %s]"),
23718 hex_string (signature), sect_offset_str (src_die->sect_off),
23719 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23720 }
23721
23722 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23723 if (die == NULL)
23724 {
23725 dump_die_for_error (src_die);
23726 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23727 " from DIE at %s [in module %s]"),
23728 hex_string (signature), sect_offset_str (src_die->sect_off),
23729 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23730 }
23731
23732 return die;
23733 }
23734
23735 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23736 reading in and processing the type unit if necessary. */
23737
23738 static struct type *
23739 get_signatured_type (struct die_info *die, ULONGEST signature,
23740 struct dwarf2_cu *cu)
23741 {
23742 struct dwarf2_per_objfile *dwarf2_per_objfile
23743 = cu->per_cu->dwarf2_per_objfile;
23744 struct signatured_type *sig_type;
23745 struct dwarf2_cu *type_cu;
23746 struct die_info *type_die;
23747 struct type *type;
23748
23749 sig_type = lookup_signatured_type (cu, signature);
23750 /* sig_type will be NULL if the signatured type is missing from
23751 the debug info. */
23752 if (sig_type == NULL)
23753 {
23754 complaint (&symfile_complaints,
23755 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23756 " from DIE at %s [in module %s]"),
23757 hex_string (signature), sect_offset_str (die->sect_off),
23758 objfile_name (dwarf2_per_objfile->objfile));
23759 return build_error_marker_type (cu, die);
23760 }
23761
23762 /* If we already know the type we're done. */
23763 if (sig_type->type != NULL)
23764 return sig_type->type;
23765
23766 type_cu = cu;
23767 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23768 if (type_die != NULL)
23769 {
23770 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23771 is created. This is important, for example, because for c++ classes
23772 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23773 type = read_type_die (type_die, type_cu);
23774 if (type == NULL)
23775 {
23776 complaint (&symfile_complaints,
23777 _("Dwarf Error: Cannot build signatured type %s"
23778 " referenced from DIE at %s [in module %s]"),
23779 hex_string (signature), sect_offset_str (die->sect_off),
23780 objfile_name (dwarf2_per_objfile->objfile));
23781 type = build_error_marker_type (cu, die);
23782 }
23783 }
23784 else
23785 {
23786 complaint (&symfile_complaints,
23787 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23788 " from DIE at %s [in module %s]"),
23789 hex_string (signature), sect_offset_str (die->sect_off),
23790 objfile_name (dwarf2_per_objfile->objfile));
23791 type = build_error_marker_type (cu, die);
23792 }
23793 sig_type->type = type;
23794
23795 return type;
23796 }
23797
23798 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23799 reading in and processing the type unit if necessary. */
23800
23801 static struct type *
23802 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23803 struct dwarf2_cu *cu) /* ARI: editCase function */
23804 {
23805 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23806 if (attr_form_is_ref (attr))
23807 {
23808 struct dwarf2_cu *type_cu = cu;
23809 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23810
23811 return read_type_die (type_die, type_cu);
23812 }
23813 else if (attr->form == DW_FORM_ref_sig8)
23814 {
23815 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23816 }
23817 else
23818 {
23819 struct dwarf2_per_objfile *dwarf2_per_objfile
23820 = cu->per_cu->dwarf2_per_objfile;
23821
23822 complaint (&symfile_complaints,
23823 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23824 " at %s [in module %s]"),
23825 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23826 objfile_name (dwarf2_per_objfile->objfile));
23827 return build_error_marker_type (cu, die);
23828 }
23829 }
23830
23831 /* Load the DIEs associated with type unit PER_CU into memory. */
23832
23833 static void
23834 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23835 {
23836 struct signatured_type *sig_type;
23837
23838 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23839 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23840
23841 /* We have the per_cu, but we need the signatured_type.
23842 Fortunately this is an easy translation. */
23843 gdb_assert (per_cu->is_debug_types);
23844 sig_type = (struct signatured_type *) per_cu;
23845
23846 gdb_assert (per_cu->cu == NULL);
23847
23848 read_signatured_type (sig_type);
23849
23850 gdb_assert (per_cu->cu != NULL);
23851 }
23852
23853 /* die_reader_func for read_signatured_type.
23854 This is identical to load_full_comp_unit_reader,
23855 but is kept separate for now. */
23856
23857 static void
23858 read_signatured_type_reader (const struct die_reader_specs *reader,
23859 const gdb_byte *info_ptr,
23860 struct die_info *comp_unit_die,
23861 int has_children,
23862 void *data)
23863 {
23864 struct dwarf2_cu *cu = reader->cu;
23865
23866 gdb_assert (cu->die_hash == NULL);
23867 cu->die_hash =
23868 htab_create_alloc_ex (cu->header.length / 12,
23869 die_hash,
23870 die_eq,
23871 NULL,
23872 &cu->comp_unit_obstack,
23873 hashtab_obstack_allocate,
23874 dummy_obstack_deallocate);
23875
23876 if (has_children)
23877 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23878 &info_ptr, comp_unit_die);
23879 cu->dies = comp_unit_die;
23880 /* comp_unit_die is not stored in die_hash, no need. */
23881
23882 /* We try not to read any attributes in this function, because not
23883 all CUs needed for references have been loaded yet, and symbol
23884 table processing isn't initialized. But we have to set the CU language,
23885 or we won't be able to build types correctly.
23886 Similarly, if we do not read the producer, we can not apply
23887 producer-specific interpretation. */
23888 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23889 }
23890
23891 /* Read in a signatured type and build its CU and DIEs.
23892 If the type is a stub for the real type in a DWO file,
23893 read in the real type from the DWO file as well. */
23894
23895 static void
23896 read_signatured_type (struct signatured_type *sig_type)
23897 {
23898 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23899
23900 gdb_assert (per_cu->is_debug_types);
23901 gdb_assert (per_cu->cu == NULL);
23902
23903 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23904 read_signatured_type_reader, NULL);
23905 sig_type->per_cu.tu_read = 1;
23906 }
23907
23908 /* Decode simple location descriptions.
23909 Given a pointer to a dwarf block that defines a location, compute
23910 the location and return the value.
23911
23912 NOTE drow/2003-11-18: This function is called in two situations
23913 now: for the address of static or global variables (partial symbols
23914 only) and for offsets into structures which are expected to be
23915 (more or less) constant. The partial symbol case should go away,
23916 and only the constant case should remain. That will let this
23917 function complain more accurately. A few special modes are allowed
23918 without complaint for global variables (for instance, global
23919 register values and thread-local values).
23920
23921 A location description containing no operations indicates that the
23922 object is optimized out. The return value is 0 for that case.
23923 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23924 callers will only want a very basic result and this can become a
23925 complaint.
23926
23927 Note that stack[0] is unused except as a default error return. */
23928
23929 static CORE_ADDR
23930 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23931 {
23932 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23933 size_t i;
23934 size_t size = blk->size;
23935 const gdb_byte *data = blk->data;
23936 CORE_ADDR stack[64];
23937 int stacki;
23938 unsigned int bytes_read, unsnd;
23939 gdb_byte op;
23940
23941 i = 0;
23942 stacki = 0;
23943 stack[stacki] = 0;
23944 stack[++stacki] = 0;
23945
23946 while (i < size)
23947 {
23948 op = data[i++];
23949 switch (op)
23950 {
23951 case DW_OP_lit0:
23952 case DW_OP_lit1:
23953 case DW_OP_lit2:
23954 case DW_OP_lit3:
23955 case DW_OP_lit4:
23956 case DW_OP_lit5:
23957 case DW_OP_lit6:
23958 case DW_OP_lit7:
23959 case DW_OP_lit8:
23960 case DW_OP_lit9:
23961 case DW_OP_lit10:
23962 case DW_OP_lit11:
23963 case DW_OP_lit12:
23964 case DW_OP_lit13:
23965 case DW_OP_lit14:
23966 case DW_OP_lit15:
23967 case DW_OP_lit16:
23968 case DW_OP_lit17:
23969 case DW_OP_lit18:
23970 case DW_OP_lit19:
23971 case DW_OP_lit20:
23972 case DW_OP_lit21:
23973 case DW_OP_lit22:
23974 case DW_OP_lit23:
23975 case DW_OP_lit24:
23976 case DW_OP_lit25:
23977 case DW_OP_lit26:
23978 case DW_OP_lit27:
23979 case DW_OP_lit28:
23980 case DW_OP_lit29:
23981 case DW_OP_lit30:
23982 case DW_OP_lit31:
23983 stack[++stacki] = op - DW_OP_lit0;
23984 break;
23985
23986 case DW_OP_reg0:
23987 case DW_OP_reg1:
23988 case DW_OP_reg2:
23989 case DW_OP_reg3:
23990 case DW_OP_reg4:
23991 case DW_OP_reg5:
23992 case DW_OP_reg6:
23993 case DW_OP_reg7:
23994 case DW_OP_reg8:
23995 case DW_OP_reg9:
23996 case DW_OP_reg10:
23997 case DW_OP_reg11:
23998 case DW_OP_reg12:
23999 case DW_OP_reg13:
24000 case DW_OP_reg14:
24001 case DW_OP_reg15:
24002 case DW_OP_reg16:
24003 case DW_OP_reg17:
24004 case DW_OP_reg18:
24005 case DW_OP_reg19:
24006 case DW_OP_reg20:
24007 case DW_OP_reg21:
24008 case DW_OP_reg22:
24009 case DW_OP_reg23:
24010 case DW_OP_reg24:
24011 case DW_OP_reg25:
24012 case DW_OP_reg26:
24013 case DW_OP_reg27:
24014 case DW_OP_reg28:
24015 case DW_OP_reg29:
24016 case DW_OP_reg30:
24017 case DW_OP_reg31:
24018 stack[++stacki] = op - DW_OP_reg0;
24019 if (i < size)
24020 dwarf2_complex_location_expr_complaint ();
24021 break;
24022
24023 case DW_OP_regx:
24024 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24025 i += bytes_read;
24026 stack[++stacki] = unsnd;
24027 if (i < size)
24028 dwarf2_complex_location_expr_complaint ();
24029 break;
24030
24031 case DW_OP_addr:
24032 stack[++stacki] = read_address (objfile->obfd, &data[i],
24033 cu, &bytes_read);
24034 i += bytes_read;
24035 break;
24036
24037 case DW_OP_const1u:
24038 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24039 i += 1;
24040 break;
24041
24042 case DW_OP_const1s:
24043 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24044 i += 1;
24045 break;
24046
24047 case DW_OP_const2u:
24048 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24049 i += 2;
24050 break;
24051
24052 case DW_OP_const2s:
24053 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24054 i += 2;
24055 break;
24056
24057 case DW_OP_const4u:
24058 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24059 i += 4;
24060 break;
24061
24062 case DW_OP_const4s:
24063 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24064 i += 4;
24065 break;
24066
24067 case DW_OP_const8u:
24068 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24069 i += 8;
24070 break;
24071
24072 case DW_OP_constu:
24073 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24074 &bytes_read);
24075 i += bytes_read;
24076 break;
24077
24078 case DW_OP_consts:
24079 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24080 i += bytes_read;
24081 break;
24082
24083 case DW_OP_dup:
24084 stack[stacki + 1] = stack[stacki];
24085 stacki++;
24086 break;
24087
24088 case DW_OP_plus:
24089 stack[stacki - 1] += stack[stacki];
24090 stacki--;
24091 break;
24092
24093 case DW_OP_plus_uconst:
24094 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24095 &bytes_read);
24096 i += bytes_read;
24097 break;
24098
24099 case DW_OP_minus:
24100 stack[stacki - 1] -= stack[stacki];
24101 stacki--;
24102 break;
24103
24104 case DW_OP_deref:
24105 /* If we're not the last op, then we definitely can't encode
24106 this using GDB's address_class enum. This is valid for partial
24107 global symbols, although the variable's address will be bogus
24108 in the psymtab. */
24109 if (i < size)
24110 dwarf2_complex_location_expr_complaint ();
24111 break;
24112
24113 case DW_OP_GNU_push_tls_address:
24114 case DW_OP_form_tls_address:
24115 /* The top of the stack has the offset from the beginning
24116 of the thread control block at which the variable is located. */
24117 /* Nothing should follow this operator, so the top of stack would
24118 be returned. */
24119 /* This is valid for partial global symbols, but the variable's
24120 address will be bogus in the psymtab. Make it always at least
24121 non-zero to not look as a variable garbage collected by linker
24122 which have DW_OP_addr 0. */
24123 if (i < size)
24124 dwarf2_complex_location_expr_complaint ();
24125 stack[stacki]++;
24126 break;
24127
24128 case DW_OP_GNU_uninit:
24129 break;
24130
24131 case DW_OP_GNU_addr_index:
24132 case DW_OP_GNU_const_index:
24133 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24134 &bytes_read);
24135 i += bytes_read;
24136 break;
24137
24138 default:
24139 {
24140 const char *name = get_DW_OP_name (op);
24141
24142 if (name)
24143 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
24144 name);
24145 else
24146 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
24147 op);
24148 }
24149
24150 return (stack[stacki]);
24151 }
24152
24153 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24154 outside of the allocated space. Also enforce minimum>0. */
24155 if (stacki >= ARRAY_SIZE (stack) - 1)
24156 {
24157 complaint (&symfile_complaints,
24158 _("location description stack overflow"));
24159 return 0;
24160 }
24161
24162 if (stacki <= 0)
24163 {
24164 complaint (&symfile_complaints,
24165 _("location description stack underflow"));
24166 return 0;
24167 }
24168 }
24169 return (stack[stacki]);
24170 }
24171
24172 /* memory allocation interface */
24173
24174 static struct dwarf_block *
24175 dwarf_alloc_block (struct dwarf2_cu *cu)
24176 {
24177 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24178 }
24179
24180 static struct die_info *
24181 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24182 {
24183 struct die_info *die;
24184 size_t size = sizeof (struct die_info);
24185
24186 if (num_attrs > 1)
24187 size += (num_attrs - 1) * sizeof (struct attribute);
24188
24189 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24190 memset (die, 0, sizeof (struct die_info));
24191 return (die);
24192 }
24193
24194 \f
24195 /* Macro support. */
24196
24197 /* Return file name relative to the compilation directory of file number I in
24198 *LH's file name table. The result is allocated using xmalloc; the caller is
24199 responsible for freeing it. */
24200
24201 static char *
24202 file_file_name (int file, struct line_header *lh)
24203 {
24204 /* Is the file number a valid index into the line header's file name
24205 table? Remember that file numbers start with one, not zero. */
24206 if (1 <= file && file <= lh->file_names.size ())
24207 {
24208 const file_entry &fe = lh->file_names[file - 1];
24209
24210 if (!IS_ABSOLUTE_PATH (fe.name))
24211 {
24212 const char *dir = fe.include_dir (lh);
24213 if (dir != NULL)
24214 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
24215 }
24216 return xstrdup (fe.name);
24217 }
24218 else
24219 {
24220 /* The compiler produced a bogus file number. We can at least
24221 record the macro definitions made in the file, even if we
24222 won't be able to find the file by name. */
24223 char fake_name[80];
24224
24225 xsnprintf (fake_name, sizeof (fake_name),
24226 "<bad macro file number %d>", file);
24227
24228 complaint (&symfile_complaints,
24229 _("bad file number in macro information (%d)"),
24230 file);
24231
24232 return xstrdup (fake_name);
24233 }
24234 }
24235
24236 /* Return the full name of file number I in *LH's file name table.
24237 Use COMP_DIR as the name of the current directory of the
24238 compilation. The result is allocated using xmalloc; the caller is
24239 responsible for freeing it. */
24240 static char *
24241 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24242 {
24243 /* Is the file number a valid index into the line header's file name
24244 table? Remember that file numbers start with one, not zero. */
24245 if (1 <= file && file <= lh->file_names.size ())
24246 {
24247 char *relative = file_file_name (file, lh);
24248
24249 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24250 return relative;
24251 return reconcat (relative, comp_dir, SLASH_STRING,
24252 relative, (char *) NULL);
24253 }
24254 else
24255 return file_file_name (file, lh);
24256 }
24257
24258
24259 static struct macro_source_file *
24260 macro_start_file (int file, int line,
24261 struct macro_source_file *current_file,
24262 struct line_header *lh)
24263 {
24264 /* File name relative to the compilation directory of this source file. */
24265 char *file_name = file_file_name (file, lh);
24266
24267 if (! current_file)
24268 {
24269 /* Note: We don't create a macro table for this compilation unit
24270 at all until we actually get a filename. */
24271 struct macro_table *macro_table = get_macro_table ();
24272
24273 /* If we have no current file, then this must be the start_file
24274 directive for the compilation unit's main source file. */
24275 current_file = macro_set_main (macro_table, file_name);
24276 macro_define_special (macro_table);
24277 }
24278 else
24279 current_file = macro_include (current_file, line, file_name);
24280
24281 xfree (file_name);
24282
24283 return current_file;
24284 }
24285
24286 static const char *
24287 consume_improper_spaces (const char *p, const char *body)
24288 {
24289 if (*p == ' ')
24290 {
24291 complaint (&symfile_complaints,
24292 _("macro definition contains spaces "
24293 "in formal argument list:\n`%s'"),
24294 body);
24295
24296 while (*p == ' ')
24297 p++;
24298 }
24299
24300 return p;
24301 }
24302
24303
24304 static void
24305 parse_macro_definition (struct macro_source_file *file, int line,
24306 const char *body)
24307 {
24308 const char *p;
24309
24310 /* The body string takes one of two forms. For object-like macro
24311 definitions, it should be:
24312
24313 <macro name> " " <definition>
24314
24315 For function-like macro definitions, it should be:
24316
24317 <macro name> "() " <definition>
24318 or
24319 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24320
24321 Spaces may appear only where explicitly indicated, and in the
24322 <definition>.
24323
24324 The Dwarf 2 spec says that an object-like macro's name is always
24325 followed by a space, but versions of GCC around March 2002 omit
24326 the space when the macro's definition is the empty string.
24327
24328 The Dwarf 2 spec says that there should be no spaces between the
24329 formal arguments in a function-like macro's formal argument list,
24330 but versions of GCC around March 2002 include spaces after the
24331 commas. */
24332
24333
24334 /* Find the extent of the macro name. The macro name is terminated
24335 by either a space or null character (for an object-like macro) or
24336 an opening paren (for a function-like macro). */
24337 for (p = body; *p; p++)
24338 if (*p == ' ' || *p == '(')
24339 break;
24340
24341 if (*p == ' ' || *p == '\0')
24342 {
24343 /* It's an object-like macro. */
24344 int name_len = p - body;
24345 char *name = savestring (body, name_len);
24346 const char *replacement;
24347
24348 if (*p == ' ')
24349 replacement = body + name_len + 1;
24350 else
24351 {
24352 dwarf2_macro_malformed_definition_complaint (body);
24353 replacement = body + name_len;
24354 }
24355
24356 macro_define_object (file, line, name, replacement);
24357
24358 xfree (name);
24359 }
24360 else if (*p == '(')
24361 {
24362 /* It's a function-like macro. */
24363 char *name = savestring (body, p - body);
24364 int argc = 0;
24365 int argv_size = 1;
24366 char **argv = XNEWVEC (char *, argv_size);
24367
24368 p++;
24369
24370 p = consume_improper_spaces (p, body);
24371
24372 /* Parse the formal argument list. */
24373 while (*p && *p != ')')
24374 {
24375 /* Find the extent of the current argument name. */
24376 const char *arg_start = p;
24377
24378 while (*p && *p != ',' && *p != ')' && *p != ' ')
24379 p++;
24380
24381 if (! *p || p == arg_start)
24382 dwarf2_macro_malformed_definition_complaint (body);
24383 else
24384 {
24385 /* Make sure argv has room for the new argument. */
24386 if (argc >= argv_size)
24387 {
24388 argv_size *= 2;
24389 argv = XRESIZEVEC (char *, argv, argv_size);
24390 }
24391
24392 argv[argc++] = savestring (arg_start, p - arg_start);
24393 }
24394
24395 p = consume_improper_spaces (p, body);
24396
24397 /* Consume the comma, if present. */
24398 if (*p == ',')
24399 {
24400 p++;
24401
24402 p = consume_improper_spaces (p, body);
24403 }
24404 }
24405
24406 if (*p == ')')
24407 {
24408 p++;
24409
24410 if (*p == ' ')
24411 /* Perfectly formed definition, no complaints. */
24412 macro_define_function (file, line, name,
24413 argc, (const char **) argv,
24414 p + 1);
24415 else if (*p == '\0')
24416 {
24417 /* Complain, but do define it. */
24418 dwarf2_macro_malformed_definition_complaint (body);
24419 macro_define_function (file, line, name,
24420 argc, (const char **) argv,
24421 p);
24422 }
24423 else
24424 /* Just complain. */
24425 dwarf2_macro_malformed_definition_complaint (body);
24426 }
24427 else
24428 /* Just complain. */
24429 dwarf2_macro_malformed_definition_complaint (body);
24430
24431 xfree (name);
24432 {
24433 int i;
24434
24435 for (i = 0; i < argc; i++)
24436 xfree (argv[i]);
24437 }
24438 xfree (argv);
24439 }
24440 else
24441 dwarf2_macro_malformed_definition_complaint (body);
24442 }
24443
24444 /* Skip some bytes from BYTES according to the form given in FORM.
24445 Returns the new pointer. */
24446
24447 static const gdb_byte *
24448 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24449 enum dwarf_form form,
24450 unsigned int offset_size,
24451 struct dwarf2_section_info *section)
24452 {
24453 unsigned int bytes_read;
24454
24455 switch (form)
24456 {
24457 case DW_FORM_data1:
24458 case DW_FORM_flag:
24459 ++bytes;
24460 break;
24461
24462 case DW_FORM_data2:
24463 bytes += 2;
24464 break;
24465
24466 case DW_FORM_data4:
24467 bytes += 4;
24468 break;
24469
24470 case DW_FORM_data8:
24471 bytes += 8;
24472 break;
24473
24474 case DW_FORM_data16:
24475 bytes += 16;
24476 break;
24477
24478 case DW_FORM_string:
24479 read_direct_string (abfd, bytes, &bytes_read);
24480 bytes += bytes_read;
24481 break;
24482
24483 case DW_FORM_sec_offset:
24484 case DW_FORM_strp:
24485 case DW_FORM_GNU_strp_alt:
24486 bytes += offset_size;
24487 break;
24488
24489 case DW_FORM_block:
24490 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24491 bytes += bytes_read;
24492 break;
24493
24494 case DW_FORM_block1:
24495 bytes += 1 + read_1_byte (abfd, bytes);
24496 break;
24497 case DW_FORM_block2:
24498 bytes += 2 + read_2_bytes (abfd, bytes);
24499 break;
24500 case DW_FORM_block4:
24501 bytes += 4 + read_4_bytes (abfd, bytes);
24502 break;
24503
24504 case DW_FORM_sdata:
24505 case DW_FORM_udata:
24506 case DW_FORM_GNU_addr_index:
24507 case DW_FORM_GNU_str_index:
24508 bytes = gdb_skip_leb128 (bytes, buffer_end);
24509 if (bytes == NULL)
24510 {
24511 dwarf2_section_buffer_overflow_complaint (section);
24512 return NULL;
24513 }
24514 break;
24515
24516 case DW_FORM_implicit_const:
24517 break;
24518
24519 default:
24520 {
24521 complaint (&symfile_complaints,
24522 _("invalid form 0x%x in `%s'"),
24523 form, get_section_name (section));
24524 return NULL;
24525 }
24526 }
24527
24528 return bytes;
24529 }
24530
24531 /* A helper for dwarf_decode_macros that handles skipping an unknown
24532 opcode. Returns an updated pointer to the macro data buffer; or,
24533 on error, issues a complaint and returns NULL. */
24534
24535 static const gdb_byte *
24536 skip_unknown_opcode (unsigned int opcode,
24537 const gdb_byte **opcode_definitions,
24538 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24539 bfd *abfd,
24540 unsigned int offset_size,
24541 struct dwarf2_section_info *section)
24542 {
24543 unsigned int bytes_read, i;
24544 unsigned long arg;
24545 const gdb_byte *defn;
24546
24547 if (opcode_definitions[opcode] == NULL)
24548 {
24549 complaint (&symfile_complaints,
24550 _("unrecognized DW_MACFINO opcode 0x%x"),
24551 opcode);
24552 return NULL;
24553 }
24554
24555 defn = opcode_definitions[opcode];
24556 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24557 defn += bytes_read;
24558
24559 for (i = 0; i < arg; ++i)
24560 {
24561 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24562 (enum dwarf_form) defn[i], offset_size,
24563 section);
24564 if (mac_ptr == NULL)
24565 {
24566 /* skip_form_bytes already issued the complaint. */
24567 return NULL;
24568 }
24569 }
24570
24571 return mac_ptr;
24572 }
24573
24574 /* A helper function which parses the header of a macro section.
24575 If the macro section is the extended (for now called "GNU") type,
24576 then this updates *OFFSET_SIZE. Returns a pointer to just after
24577 the header, or issues a complaint and returns NULL on error. */
24578
24579 static const gdb_byte *
24580 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24581 bfd *abfd,
24582 const gdb_byte *mac_ptr,
24583 unsigned int *offset_size,
24584 int section_is_gnu)
24585 {
24586 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24587
24588 if (section_is_gnu)
24589 {
24590 unsigned int version, flags;
24591
24592 version = read_2_bytes (abfd, mac_ptr);
24593 if (version != 4 && version != 5)
24594 {
24595 complaint (&symfile_complaints,
24596 _("unrecognized version `%d' in .debug_macro section"),
24597 version);
24598 return NULL;
24599 }
24600 mac_ptr += 2;
24601
24602 flags = read_1_byte (abfd, mac_ptr);
24603 ++mac_ptr;
24604 *offset_size = (flags & 1) ? 8 : 4;
24605
24606 if ((flags & 2) != 0)
24607 /* We don't need the line table offset. */
24608 mac_ptr += *offset_size;
24609
24610 /* Vendor opcode descriptions. */
24611 if ((flags & 4) != 0)
24612 {
24613 unsigned int i, count;
24614
24615 count = read_1_byte (abfd, mac_ptr);
24616 ++mac_ptr;
24617 for (i = 0; i < count; ++i)
24618 {
24619 unsigned int opcode, bytes_read;
24620 unsigned long arg;
24621
24622 opcode = read_1_byte (abfd, mac_ptr);
24623 ++mac_ptr;
24624 opcode_definitions[opcode] = mac_ptr;
24625 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24626 mac_ptr += bytes_read;
24627 mac_ptr += arg;
24628 }
24629 }
24630 }
24631
24632 return mac_ptr;
24633 }
24634
24635 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24636 including DW_MACRO_import. */
24637
24638 static void
24639 dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24640 bfd *abfd,
24641 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24642 struct macro_source_file *current_file,
24643 struct line_header *lh,
24644 struct dwarf2_section_info *section,
24645 int section_is_gnu, int section_is_dwz,
24646 unsigned int offset_size,
24647 htab_t include_hash)
24648 {
24649 struct objfile *objfile = dwarf2_per_objfile->objfile;
24650 enum dwarf_macro_record_type macinfo_type;
24651 int at_commandline;
24652 const gdb_byte *opcode_definitions[256];
24653
24654 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24655 &offset_size, section_is_gnu);
24656 if (mac_ptr == NULL)
24657 {
24658 /* We already issued a complaint. */
24659 return;
24660 }
24661
24662 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24663 GDB is still reading the definitions from command line. First
24664 DW_MACINFO_start_file will need to be ignored as it was already executed
24665 to create CURRENT_FILE for the main source holding also the command line
24666 definitions. On first met DW_MACINFO_start_file this flag is reset to
24667 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24668
24669 at_commandline = 1;
24670
24671 do
24672 {
24673 /* Do we at least have room for a macinfo type byte? */
24674 if (mac_ptr >= mac_end)
24675 {
24676 dwarf2_section_buffer_overflow_complaint (section);
24677 break;
24678 }
24679
24680 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24681 mac_ptr++;
24682
24683 /* Note that we rely on the fact that the corresponding GNU and
24684 DWARF constants are the same. */
24685 DIAGNOSTIC_PUSH
24686 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24687 switch (macinfo_type)
24688 {
24689 /* A zero macinfo type indicates the end of the macro
24690 information. */
24691 case 0:
24692 break;
24693
24694 case DW_MACRO_define:
24695 case DW_MACRO_undef:
24696 case DW_MACRO_define_strp:
24697 case DW_MACRO_undef_strp:
24698 case DW_MACRO_define_sup:
24699 case DW_MACRO_undef_sup:
24700 {
24701 unsigned int bytes_read;
24702 int line;
24703 const char *body;
24704 int is_define;
24705
24706 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24707 mac_ptr += bytes_read;
24708
24709 if (macinfo_type == DW_MACRO_define
24710 || macinfo_type == DW_MACRO_undef)
24711 {
24712 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24713 mac_ptr += bytes_read;
24714 }
24715 else
24716 {
24717 LONGEST str_offset;
24718
24719 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24720 mac_ptr += offset_size;
24721
24722 if (macinfo_type == DW_MACRO_define_sup
24723 || macinfo_type == DW_MACRO_undef_sup
24724 || section_is_dwz)
24725 {
24726 struct dwz_file *dwz
24727 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24728
24729 body = read_indirect_string_from_dwz (objfile,
24730 dwz, str_offset);
24731 }
24732 else
24733 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24734 abfd, str_offset);
24735 }
24736
24737 is_define = (macinfo_type == DW_MACRO_define
24738 || macinfo_type == DW_MACRO_define_strp
24739 || macinfo_type == DW_MACRO_define_sup);
24740 if (! current_file)
24741 {
24742 /* DWARF violation as no main source is present. */
24743 complaint (&symfile_complaints,
24744 _("debug info with no main source gives macro %s "
24745 "on line %d: %s"),
24746 is_define ? _("definition") : _("undefinition"),
24747 line, body);
24748 break;
24749 }
24750 if ((line == 0 && !at_commandline)
24751 || (line != 0 && at_commandline))
24752 complaint (&symfile_complaints,
24753 _("debug info gives %s macro %s with %s line %d: %s"),
24754 at_commandline ? _("command-line") : _("in-file"),
24755 is_define ? _("definition") : _("undefinition"),
24756 line == 0 ? _("zero") : _("non-zero"), line, body);
24757
24758 if (is_define)
24759 parse_macro_definition (current_file, line, body);
24760 else
24761 {
24762 gdb_assert (macinfo_type == DW_MACRO_undef
24763 || macinfo_type == DW_MACRO_undef_strp
24764 || macinfo_type == DW_MACRO_undef_sup);
24765 macro_undef (current_file, line, body);
24766 }
24767 }
24768 break;
24769
24770 case DW_MACRO_start_file:
24771 {
24772 unsigned int bytes_read;
24773 int line, file;
24774
24775 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24776 mac_ptr += bytes_read;
24777 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24778 mac_ptr += bytes_read;
24779
24780 if ((line == 0 && !at_commandline)
24781 || (line != 0 && at_commandline))
24782 complaint (&symfile_complaints,
24783 _("debug info gives source %d included "
24784 "from %s at %s line %d"),
24785 file, at_commandline ? _("command-line") : _("file"),
24786 line == 0 ? _("zero") : _("non-zero"), line);
24787
24788 if (at_commandline)
24789 {
24790 /* This DW_MACRO_start_file was executed in the
24791 pass one. */
24792 at_commandline = 0;
24793 }
24794 else
24795 current_file = macro_start_file (file, line, current_file, lh);
24796 }
24797 break;
24798
24799 case DW_MACRO_end_file:
24800 if (! current_file)
24801 complaint (&symfile_complaints,
24802 _("macro debug info has an unmatched "
24803 "`close_file' directive"));
24804 else
24805 {
24806 current_file = current_file->included_by;
24807 if (! current_file)
24808 {
24809 enum dwarf_macro_record_type next_type;
24810
24811 /* GCC circa March 2002 doesn't produce the zero
24812 type byte marking the end of the compilation
24813 unit. Complain if it's not there, but exit no
24814 matter what. */
24815
24816 /* Do we at least have room for a macinfo type byte? */
24817 if (mac_ptr >= mac_end)
24818 {
24819 dwarf2_section_buffer_overflow_complaint (section);
24820 return;
24821 }
24822
24823 /* We don't increment mac_ptr here, so this is just
24824 a look-ahead. */
24825 next_type
24826 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24827 mac_ptr);
24828 if (next_type != 0)
24829 complaint (&symfile_complaints,
24830 _("no terminating 0-type entry for "
24831 "macros in `.debug_macinfo' section"));
24832
24833 return;
24834 }
24835 }
24836 break;
24837
24838 case DW_MACRO_import:
24839 case DW_MACRO_import_sup:
24840 {
24841 LONGEST offset;
24842 void **slot;
24843 bfd *include_bfd = abfd;
24844 struct dwarf2_section_info *include_section = section;
24845 const gdb_byte *include_mac_end = mac_end;
24846 int is_dwz = section_is_dwz;
24847 const gdb_byte *new_mac_ptr;
24848
24849 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24850 mac_ptr += offset_size;
24851
24852 if (macinfo_type == DW_MACRO_import_sup)
24853 {
24854 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24855
24856 dwarf2_read_section (objfile, &dwz->macro);
24857
24858 include_section = &dwz->macro;
24859 include_bfd = get_section_bfd_owner (include_section);
24860 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24861 is_dwz = 1;
24862 }
24863
24864 new_mac_ptr = include_section->buffer + offset;
24865 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24866
24867 if (*slot != NULL)
24868 {
24869 /* This has actually happened; see
24870 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24871 complaint (&symfile_complaints,
24872 _("recursive DW_MACRO_import in "
24873 ".debug_macro section"));
24874 }
24875 else
24876 {
24877 *slot = (void *) new_mac_ptr;
24878
24879 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24880 include_bfd, new_mac_ptr,
24881 include_mac_end, current_file, lh,
24882 section, section_is_gnu, is_dwz,
24883 offset_size, include_hash);
24884
24885 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24886 }
24887 }
24888 break;
24889
24890 case DW_MACINFO_vendor_ext:
24891 if (!section_is_gnu)
24892 {
24893 unsigned int bytes_read;
24894
24895 /* This reads the constant, but since we don't recognize
24896 any vendor extensions, we ignore it. */
24897 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24898 mac_ptr += bytes_read;
24899 read_direct_string (abfd, mac_ptr, &bytes_read);
24900 mac_ptr += bytes_read;
24901
24902 /* We don't recognize any vendor extensions. */
24903 break;
24904 }
24905 /* FALLTHROUGH */
24906
24907 default:
24908 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24909 mac_ptr, mac_end, abfd, offset_size,
24910 section);
24911 if (mac_ptr == NULL)
24912 return;
24913 break;
24914 }
24915 DIAGNOSTIC_POP
24916 } while (macinfo_type != 0);
24917 }
24918
24919 static void
24920 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24921 int section_is_gnu)
24922 {
24923 struct dwarf2_per_objfile *dwarf2_per_objfile
24924 = cu->per_cu->dwarf2_per_objfile;
24925 struct objfile *objfile = dwarf2_per_objfile->objfile;
24926 struct line_header *lh = cu->line_header;
24927 bfd *abfd;
24928 const gdb_byte *mac_ptr, *mac_end;
24929 struct macro_source_file *current_file = 0;
24930 enum dwarf_macro_record_type macinfo_type;
24931 unsigned int offset_size = cu->header.offset_size;
24932 const gdb_byte *opcode_definitions[256];
24933 void **slot;
24934 struct dwarf2_section_info *section;
24935 const char *section_name;
24936
24937 if (cu->dwo_unit != NULL)
24938 {
24939 if (section_is_gnu)
24940 {
24941 section = &cu->dwo_unit->dwo_file->sections.macro;
24942 section_name = ".debug_macro.dwo";
24943 }
24944 else
24945 {
24946 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24947 section_name = ".debug_macinfo.dwo";
24948 }
24949 }
24950 else
24951 {
24952 if (section_is_gnu)
24953 {
24954 section = &dwarf2_per_objfile->macro;
24955 section_name = ".debug_macro";
24956 }
24957 else
24958 {
24959 section = &dwarf2_per_objfile->macinfo;
24960 section_name = ".debug_macinfo";
24961 }
24962 }
24963
24964 dwarf2_read_section (objfile, section);
24965 if (section->buffer == NULL)
24966 {
24967 complaint (&symfile_complaints, _("missing %s section"), section_name);
24968 return;
24969 }
24970 abfd = get_section_bfd_owner (section);
24971
24972 /* First pass: Find the name of the base filename.
24973 This filename is needed in order to process all macros whose definition
24974 (or undefinition) comes from the command line. These macros are defined
24975 before the first DW_MACINFO_start_file entry, and yet still need to be
24976 associated to the base file.
24977
24978 To determine the base file name, we scan the macro definitions until we
24979 reach the first DW_MACINFO_start_file entry. We then initialize
24980 CURRENT_FILE accordingly so that any macro definition found before the
24981 first DW_MACINFO_start_file can still be associated to the base file. */
24982
24983 mac_ptr = section->buffer + offset;
24984 mac_end = section->buffer + section->size;
24985
24986 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24987 &offset_size, section_is_gnu);
24988 if (mac_ptr == NULL)
24989 {
24990 /* We already issued a complaint. */
24991 return;
24992 }
24993
24994 do
24995 {
24996 /* Do we at least have room for a macinfo type byte? */
24997 if (mac_ptr >= mac_end)
24998 {
24999 /* Complaint is printed during the second pass as GDB will probably
25000 stop the first pass earlier upon finding
25001 DW_MACINFO_start_file. */
25002 break;
25003 }
25004
25005 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
25006 mac_ptr++;
25007
25008 /* Note that we rely on the fact that the corresponding GNU and
25009 DWARF constants are the same. */
25010 DIAGNOSTIC_PUSH
25011 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
25012 switch (macinfo_type)
25013 {
25014 /* A zero macinfo type indicates the end of the macro
25015 information. */
25016 case 0:
25017 break;
25018
25019 case DW_MACRO_define:
25020 case DW_MACRO_undef:
25021 /* Only skip the data by MAC_PTR. */
25022 {
25023 unsigned int bytes_read;
25024
25025 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25026 mac_ptr += bytes_read;
25027 read_direct_string (abfd, mac_ptr, &bytes_read);
25028 mac_ptr += bytes_read;
25029 }
25030 break;
25031
25032 case DW_MACRO_start_file:
25033 {
25034 unsigned int bytes_read;
25035 int line, file;
25036
25037 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25038 mac_ptr += bytes_read;
25039 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25040 mac_ptr += bytes_read;
25041
25042 current_file = macro_start_file (file, line, current_file, lh);
25043 }
25044 break;
25045
25046 case DW_MACRO_end_file:
25047 /* No data to skip by MAC_PTR. */
25048 break;
25049
25050 case DW_MACRO_define_strp:
25051 case DW_MACRO_undef_strp:
25052 case DW_MACRO_define_sup:
25053 case DW_MACRO_undef_sup:
25054 {
25055 unsigned int bytes_read;
25056
25057 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25058 mac_ptr += bytes_read;
25059 mac_ptr += offset_size;
25060 }
25061 break;
25062
25063 case DW_MACRO_import:
25064 case DW_MACRO_import_sup:
25065 /* Note that, according to the spec, a transparent include
25066 chain cannot call DW_MACRO_start_file. So, we can just
25067 skip this opcode. */
25068 mac_ptr += offset_size;
25069 break;
25070
25071 case DW_MACINFO_vendor_ext:
25072 /* Only skip the data by MAC_PTR. */
25073 if (!section_is_gnu)
25074 {
25075 unsigned int bytes_read;
25076
25077 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25078 mac_ptr += bytes_read;
25079 read_direct_string (abfd, mac_ptr, &bytes_read);
25080 mac_ptr += bytes_read;
25081 }
25082 /* FALLTHROUGH */
25083
25084 default:
25085 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25086 mac_ptr, mac_end, abfd, offset_size,
25087 section);
25088 if (mac_ptr == NULL)
25089 return;
25090 break;
25091 }
25092 DIAGNOSTIC_POP
25093 } while (macinfo_type != 0 && current_file == NULL);
25094
25095 /* Second pass: Process all entries.
25096
25097 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25098 command-line macro definitions/undefinitions. This flag is unset when we
25099 reach the first DW_MACINFO_start_file entry. */
25100
25101 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25102 htab_eq_pointer,
25103 NULL, xcalloc, xfree));
25104 mac_ptr = section->buffer + offset;
25105 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25106 *slot = (void *) mac_ptr;
25107 dwarf_decode_macro_bytes (dwarf2_per_objfile,
25108 abfd, mac_ptr, mac_end,
25109 current_file, lh, section,
25110 section_is_gnu, 0, offset_size,
25111 include_hash.get ());
25112 }
25113
25114 /* Check if the attribute's form is a DW_FORM_block*
25115 if so return true else false. */
25116
25117 static int
25118 attr_form_is_block (const struct attribute *attr)
25119 {
25120 return (attr == NULL ? 0 :
25121 attr->form == DW_FORM_block1
25122 || attr->form == DW_FORM_block2
25123 || attr->form == DW_FORM_block4
25124 || attr->form == DW_FORM_block
25125 || attr->form == DW_FORM_exprloc);
25126 }
25127
25128 /* Return non-zero if ATTR's value is a section offset --- classes
25129 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25130 You may use DW_UNSND (attr) to retrieve such offsets.
25131
25132 Section 7.5.4, "Attribute Encodings", explains that no attribute
25133 may have a value that belongs to more than one of these classes; it
25134 would be ambiguous if we did, because we use the same forms for all
25135 of them. */
25136
25137 static int
25138 attr_form_is_section_offset (const struct attribute *attr)
25139 {
25140 return (attr->form == DW_FORM_data4
25141 || attr->form == DW_FORM_data8
25142 || attr->form == DW_FORM_sec_offset);
25143 }
25144
25145 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25146 zero otherwise. When this function returns true, you can apply
25147 dwarf2_get_attr_constant_value to it.
25148
25149 However, note that for some attributes you must check
25150 attr_form_is_section_offset before using this test. DW_FORM_data4
25151 and DW_FORM_data8 are members of both the constant class, and of
25152 the classes that contain offsets into other debug sections
25153 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25154 that, if an attribute's can be either a constant or one of the
25155 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25156 taken as section offsets, not constants.
25157
25158 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25159 cannot handle that. */
25160
25161 static int
25162 attr_form_is_constant (const struct attribute *attr)
25163 {
25164 switch (attr->form)
25165 {
25166 case DW_FORM_sdata:
25167 case DW_FORM_udata:
25168 case DW_FORM_data1:
25169 case DW_FORM_data2:
25170 case DW_FORM_data4:
25171 case DW_FORM_data8:
25172 case DW_FORM_implicit_const:
25173 return 1;
25174 default:
25175 return 0;
25176 }
25177 }
25178
25179
25180 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25181 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25182
25183 static int
25184 attr_form_is_ref (const struct attribute *attr)
25185 {
25186 switch (attr->form)
25187 {
25188 case DW_FORM_ref_addr:
25189 case DW_FORM_ref1:
25190 case DW_FORM_ref2:
25191 case DW_FORM_ref4:
25192 case DW_FORM_ref8:
25193 case DW_FORM_ref_udata:
25194 case DW_FORM_GNU_ref_alt:
25195 return 1;
25196 default:
25197 return 0;
25198 }
25199 }
25200
25201 /* Return the .debug_loc section to use for CU.
25202 For DWO files use .debug_loc.dwo. */
25203
25204 static struct dwarf2_section_info *
25205 cu_debug_loc_section (struct dwarf2_cu *cu)
25206 {
25207 struct dwarf2_per_objfile *dwarf2_per_objfile
25208 = cu->per_cu->dwarf2_per_objfile;
25209
25210 if (cu->dwo_unit)
25211 {
25212 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25213
25214 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25215 }
25216 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25217 : &dwarf2_per_objfile->loc);
25218 }
25219
25220 /* A helper function that fills in a dwarf2_loclist_baton. */
25221
25222 static void
25223 fill_in_loclist_baton (struct dwarf2_cu *cu,
25224 struct dwarf2_loclist_baton *baton,
25225 const struct attribute *attr)
25226 {
25227 struct dwarf2_per_objfile *dwarf2_per_objfile
25228 = cu->per_cu->dwarf2_per_objfile;
25229 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25230
25231 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25232
25233 baton->per_cu = cu->per_cu;
25234 gdb_assert (baton->per_cu);
25235 /* We don't know how long the location list is, but make sure we
25236 don't run off the edge of the section. */
25237 baton->size = section->size - DW_UNSND (attr);
25238 baton->data = section->buffer + DW_UNSND (attr);
25239 baton->base_address = cu->base_address;
25240 baton->from_dwo = cu->dwo_unit != NULL;
25241 }
25242
25243 static void
25244 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25245 struct dwarf2_cu *cu, int is_block)
25246 {
25247 struct dwarf2_per_objfile *dwarf2_per_objfile
25248 = cu->per_cu->dwarf2_per_objfile;
25249 struct objfile *objfile = dwarf2_per_objfile->objfile;
25250 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25251
25252 if (attr_form_is_section_offset (attr)
25253 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25254 the section. If so, fall through to the complaint in the
25255 other branch. */
25256 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25257 {
25258 struct dwarf2_loclist_baton *baton;
25259
25260 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25261
25262 fill_in_loclist_baton (cu, baton, attr);
25263
25264 if (cu->base_known == 0)
25265 complaint (&symfile_complaints,
25266 _("Location list used without "
25267 "specifying the CU base address."));
25268
25269 SYMBOL_ACLASS_INDEX (sym) = (is_block
25270 ? dwarf2_loclist_block_index
25271 : dwarf2_loclist_index);
25272 SYMBOL_LOCATION_BATON (sym) = baton;
25273 }
25274 else
25275 {
25276 struct dwarf2_locexpr_baton *baton;
25277
25278 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25279 baton->per_cu = cu->per_cu;
25280 gdb_assert (baton->per_cu);
25281
25282 if (attr_form_is_block (attr))
25283 {
25284 /* Note that we're just copying the block's data pointer
25285 here, not the actual data. We're still pointing into the
25286 info_buffer for SYM's objfile; right now we never release
25287 that buffer, but when we do clean up properly this may
25288 need to change. */
25289 baton->size = DW_BLOCK (attr)->size;
25290 baton->data = DW_BLOCK (attr)->data;
25291 }
25292 else
25293 {
25294 dwarf2_invalid_attrib_class_complaint ("location description",
25295 SYMBOL_NATURAL_NAME (sym));
25296 baton->size = 0;
25297 }
25298
25299 SYMBOL_ACLASS_INDEX (sym) = (is_block
25300 ? dwarf2_locexpr_block_index
25301 : dwarf2_locexpr_index);
25302 SYMBOL_LOCATION_BATON (sym) = baton;
25303 }
25304 }
25305
25306 /* Return the OBJFILE associated with the compilation unit CU. If CU
25307 came from a separate debuginfo file, then the master objfile is
25308 returned. */
25309
25310 struct objfile *
25311 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25312 {
25313 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25314
25315 /* Return the master objfile, so that we can report and look up the
25316 correct file containing this variable. */
25317 if (objfile->separate_debug_objfile_backlink)
25318 objfile = objfile->separate_debug_objfile_backlink;
25319
25320 return objfile;
25321 }
25322
25323 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25324 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25325 CU_HEADERP first. */
25326
25327 static const struct comp_unit_head *
25328 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25329 struct dwarf2_per_cu_data *per_cu)
25330 {
25331 const gdb_byte *info_ptr;
25332
25333 if (per_cu->cu)
25334 return &per_cu->cu->header;
25335
25336 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25337
25338 memset (cu_headerp, 0, sizeof (*cu_headerp));
25339 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25340 rcuh_kind::COMPILE);
25341
25342 return cu_headerp;
25343 }
25344
25345 /* Return the address size given in the compilation unit header for CU. */
25346
25347 int
25348 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25349 {
25350 struct comp_unit_head cu_header_local;
25351 const struct comp_unit_head *cu_headerp;
25352
25353 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25354
25355 return cu_headerp->addr_size;
25356 }
25357
25358 /* Return the offset size given in the compilation unit header for CU. */
25359
25360 int
25361 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25362 {
25363 struct comp_unit_head cu_header_local;
25364 const struct comp_unit_head *cu_headerp;
25365
25366 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25367
25368 return cu_headerp->offset_size;
25369 }
25370
25371 /* See its dwarf2loc.h declaration. */
25372
25373 int
25374 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25375 {
25376 struct comp_unit_head cu_header_local;
25377 const struct comp_unit_head *cu_headerp;
25378
25379 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25380
25381 if (cu_headerp->version == 2)
25382 return cu_headerp->addr_size;
25383 else
25384 return cu_headerp->offset_size;
25385 }
25386
25387 /* Return the text offset of the CU. The returned offset comes from
25388 this CU's objfile. If this objfile came from a separate debuginfo
25389 file, then the offset may be different from the corresponding
25390 offset in the parent objfile. */
25391
25392 CORE_ADDR
25393 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25394 {
25395 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25396
25397 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25398 }
25399
25400 /* Return DWARF version number of PER_CU. */
25401
25402 short
25403 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25404 {
25405 return per_cu->dwarf_version;
25406 }
25407
25408 /* Locate the .debug_info compilation unit from CU's objfile which contains
25409 the DIE at OFFSET. Raises an error on failure. */
25410
25411 static struct dwarf2_per_cu_data *
25412 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25413 unsigned int offset_in_dwz,
25414 struct dwarf2_per_objfile *dwarf2_per_objfile)
25415 {
25416 struct dwarf2_per_cu_data *this_cu;
25417 int low, high;
25418 const sect_offset *cu_off;
25419
25420 low = 0;
25421 high = dwarf2_per_objfile->n_comp_units - 1;
25422 while (high > low)
25423 {
25424 struct dwarf2_per_cu_data *mid_cu;
25425 int mid = low + (high - low) / 2;
25426
25427 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25428 cu_off = &mid_cu->sect_off;
25429 if (mid_cu->is_dwz > offset_in_dwz
25430 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
25431 high = mid;
25432 else
25433 low = mid + 1;
25434 }
25435 gdb_assert (low == high);
25436 this_cu = dwarf2_per_objfile->all_comp_units[low];
25437 cu_off = &this_cu->sect_off;
25438 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
25439 {
25440 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25441 error (_("Dwarf Error: could not find partial DIE containing "
25442 "offset %s [in module %s]"),
25443 sect_offset_str (sect_off),
25444 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25445
25446 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25447 <= sect_off);
25448 return dwarf2_per_objfile->all_comp_units[low-1];
25449 }
25450 else
25451 {
25452 this_cu = dwarf2_per_objfile->all_comp_units[low];
25453 if (low == dwarf2_per_objfile->n_comp_units - 1
25454 && sect_off >= this_cu->sect_off + this_cu->length)
25455 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25456 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25457 return this_cu;
25458 }
25459 }
25460
25461 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25462
25463 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25464 : per_cu (per_cu_),
25465 mark (0),
25466 has_loclist (0),
25467 checked_producer (0),
25468 producer_is_gxx_lt_4_6 (0),
25469 producer_is_gcc_lt_4_3 (0),
25470 producer_is_icc_lt_14 (0),
25471 processing_has_namespace_info (0)
25472 {
25473 per_cu->cu = this;
25474 }
25475
25476 /* Destroy a dwarf2_cu. */
25477
25478 dwarf2_cu::~dwarf2_cu ()
25479 {
25480 per_cu->cu = NULL;
25481 }
25482
25483 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25484
25485 static void
25486 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25487 enum language pretend_language)
25488 {
25489 struct attribute *attr;
25490
25491 /* Set the language we're debugging. */
25492 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25493 if (attr)
25494 set_cu_language (DW_UNSND (attr), cu);
25495 else
25496 {
25497 cu->language = pretend_language;
25498 cu->language_defn = language_def (cu->language);
25499 }
25500
25501 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25502 }
25503
25504 /* Free all cached compilation units. */
25505
25506 static void
25507 free_cached_comp_units (void *data)
25508 {
25509 struct dwarf2_per_objfile *dwarf2_per_objfile
25510 = (struct dwarf2_per_objfile *) data;
25511
25512 dwarf2_per_objfile->free_cached_comp_units ();
25513 }
25514
25515 /* Increase the age counter on each cached compilation unit, and free
25516 any that are too old. */
25517
25518 static void
25519 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25520 {
25521 struct dwarf2_per_cu_data *per_cu, **last_chain;
25522
25523 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25524 per_cu = dwarf2_per_objfile->read_in_chain;
25525 while (per_cu != NULL)
25526 {
25527 per_cu->cu->last_used ++;
25528 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25529 dwarf2_mark (per_cu->cu);
25530 per_cu = per_cu->cu->read_in_chain;
25531 }
25532
25533 per_cu = dwarf2_per_objfile->read_in_chain;
25534 last_chain = &dwarf2_per_objfile->read_in_chain;
25535 while (per_cu != NULL)
25536 {
25537 struct dwarf2_per_cu_data *next_cu;
25538
25539 next_cu = per_cu->cu->read_in_chain;
25540
25541 if (!per_cu->cu->mark)
25542 {
25543 delete per_cu->cu;
25544 *last_chain = next_cu;
25545 }
25546 else
25547 last_chain = &per_cu->cu->read_in_chain;
25548
25549 per_cu = next_cu;
25550 }
25551 }
25552
25553 /* Remove a single compilation unit from the cache. */
25554
25555 static void
25556 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25557 {
25558 struct dwarf2_per_cu_data *per_cu, **last_chain;
25559 struct dwarf2_per_objfile *dwarf2_per_objfile
25560 = target_per_cu->dwarf2_per_objfile;
25561
25562 per_cu = dwarf2_per_objfile->read_in_chain;
25563 last_chain = &dwarf2_per_objfile->read_in_chain;
25564 while (per_cu != NULL)
25565 {
25566 struct dwarf2_per_cu_data *next_cu;
25567
25568 next_cu = per_cu->cu->read_in_chain;
25569
25570 if (per_cu == target_per_cu)
25571 {
25572 delete per_cu->cu;
25573 per_cu->cu = NULL;
25574 *last_chain = next_cu;
25575 break;
25576 }
25577 else
25578 last_chain = &per_cu->cu->read_in_chain;
25579
25580 per_cu = next_cu;
25581 }
25582 }
25583
25584 /* Release all extra memory associated with OBJFILE. */
25585
25586 void
25587 dwarf2_free_objfile (struct objfile *objfile)
25588 {
25589 struct dwarf2_per_objfile *dwarf2_per_objfile
25590 = get_dwarf2_per_objfile (objfile);
25591
25592 delete dwarf2_per_objfile;
25593 }
25594
25595 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25596 We store these in a hash table separate from the DIEs, and preserve them
25597 when the DIEs are flushed out of cache.
25598
25599 The CU "per_cu" pointer is needed because offset alone is not enough to
25600 uniquely identify the type. A file may have multiple .debug_types sections,
25601 or the type may come from a DWO file. Furthermore, while it's more logical
25602 to use per_cu->section+offset, with Fission the section with the data is in
25603 the DWO file but we don't know that section at the point we need it.
25604 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25605 because we can enter the lookup routine, get_die_type_at_offset, from
25606 outside this file, and thus won't necessarily have PER_CU->cu.
25607 Fortunately, PER_CU is stable for the life of the objfile. */
25608
25609 struct dwarf2_per_cu_offset_and_type
25610 {
25611 const struct dwarf2_per_cu_data *per_cu;
25612 sect_offset sect_off;
25613 struct type *type;
25614 };
25615
25616 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25617
25618 static hashval_t
25619 per_cu_offset_and_type_hash (const void *item)
25620 {
25621 const struct dwarf2_per_cu_offset_and_type *ofs
25622 = (const struct dwarf2_per_cu_offset_and_type *) item;
25623
25624 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25625 }
25626
25627 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25628
25629 static int
25630 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25631 {
25632 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25633 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25634 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25635 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25636
25637 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25638 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25639 }
25640
25641 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25642 table if necessary. For convenience, return TYPE.
25643
25644 The DIEs reading must have careful ordering to:
25645 * Not cause infite loops trying to read in DIEs as a prerequisite for
25646 reading current DIE.
25647 * Not trying to dereference contents of still incompletely read in types
25648 while reading in other DIEs.
25649 * Enable referencing still incompletely read in types just by a pointer to
25650 the type without accessing its fields.
25651
25652 Therefore caller should follow these rules:
25653 * Try to fetch any prerequisite types we may need to build this DIE type
25654 before building the type and calling set_die_type.
25655 * After building type call set_die_type for current DIE as soon as
25656 possible before fetching more types to complete the current type.
25657 * Make the type as complete as possible before fetching more types. */
25658
25659 static struct type *
25660 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25661 {
25662 struct dwarf2_per_objfile *dwarf2_per_objfile
25663 = cu->per_cu->dwarf2_per_objfile;
25664 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25665 struct objfile *objfile = dwarf2_per_objfile->objfile;
25666 struct attribute *attr;
25667 struct dynamic_prop prop;
25668
25669 /* For Ada types, make sure that the gnat-specific data is always
25670 initialized (if not already set). There are a few types where
25671 we should not be doing so, because the type-specific area is
25672 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25673 where the type-specific area is used to store the floatformat).
25674 But this is not a problem, because the gnat-specific information
25675 is actually not needed for these types. */
25676 if (need_gnat_info (cu)
25677 && TYPE_CODE (type) != TYPE_CODE_FUNC
25678 && TYPE_CODE (type) != TYPE_CODE_FLT
25679 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25680 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25681 && TYPE_CODE (type) != TYPE_CODE_METHOD
25682 && !HAVE_GNAT_AUX_INFO (type))
25683 INIT_GNAT_SPECIFIC (type);
25684
25685 /* Read DW_AT_allocated and set in type. */
25686 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25687 if (attr_form_is_block (attr))
25688 {
25689 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25690 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25691 }
25692 else if (attr != NULL)
25693 {
25694 complaint (&symfile_complaints,
25695 _("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25696 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25697 sect_offset_str (die->sect_off));
25698 }
25699
25700 /* Read DW_AT_associated and set in type. */
25701 attr = dwarf2_attr (die, DW_AT_associated, cu);
25702 if (attr_form_is_block (attr))
25703 {
25704 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25705 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25706 }
25707 else if (attr != NULL)
25708 {
25709 complaint (&symfile_complaints,
25710 _("DW_AT_associated has the wrong form (%s) at DIE %s"),
25711 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25712 sect_offset_str (die->sect_off));
25713 }
25714
25715 /* Read DW_AT_data_location and set in type. */
25716 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25717 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25718 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25719
25720 if (dwarf2_per_objfile->die_type_hash == NULL)
25721 {
25722 dwarf2_per_objfile->die_type_hash =
25723 htab_create_alloc_ex (127,
25724 per_cu_offset_and_type_hash,
25725 per_cu_offset_and_type_eq,
25726 NULL,
25727 &objfile->objfile_obstack,
25728 hashtab_obstack_allocate,
25729 dummy_obstack_deallocate);
25730 }
25731
25732 ofs.per_cu = cu->per_cu;
25733 ofs.sect_off = die->sect_off;
25734 ofs.type = type;
25735 slot = (struct dwarf2_per_cu_offset_and_type **)
25736 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25737 if (*slot)
25738 complaint (&symfile_complaints,
25739 _("A problem internal to GDB: DIE %s has type already set"),
25740 sect_offset_str (die->sect_off));
25741 *slot = XOBNEW (&objfile->objfile_obstack,
25742 struct dwarf2_per_cu_offset_and_type);
25743 **slot = ofs;
25744 return type;
25745 }
25746
25747 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25748 or return NULL if the die does not have a saved type. */
25749
25750 static struct type *
25751 get_die_type_at_offset (sect_offset sect_off,
25752 struct dwarf2_per_cu_data *per_cu)
25753 {
25754 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25755 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25756
25757 if (dwarf2_per_objfile->die_type_hash == NULL)
25758 return NULL;
25759
25760 ofs.per_cu = per_cu;
25761 ofs.sect_off = sect_off;
25762 slot = ((struct dwarf2_per_cu_offset_and_type *)
25763 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25764 if (slot)
25765 return slot->type;
25766 else
25767 return NULL;
25768 }
25769
25770 /* Look up the type for DIE in CU in die_type_hash,
25771 or return NULL if DIE does not have a saved type. */
25772
25773 static struct type *
25774 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25775 {
25776 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25777 }
25778
25779 /* Add a dependence relationship from CU to REF_PER_CU. */
25780
25781 static void
25782 dwarf2_add_dependence (struct dwarf2_cu *cu,
25783 struct dwarf2_per_cu_data *ref_per_cu)
25784 {
25785 void **slot;
25786
25787 if (cu->dependencies == NULL)
25788 cu->dependencies
25789 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25790 NULL, &cu->comp_unit_obstack,
25791 hashtab_obstack_allocate,
25792 dummy_obstack_deallocate);
25793
25794 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25795 if (*slot == NULL)
25796 *slot = ref_per_cu;
25797 }
25798
25799 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25800 Set the mark field in every compilation unit in the
25801 cache that we must keep because we are keeping CU. */
25802
25803 static int
25804 dwarf2_mark_helper (void **slot, void *data)
25805 {
25806 struct dwarf2_per_cu_data *per_cu;
25807
25808 per_cu = (struct dwarf2_per_cu_data *) *slot;
25809
25810 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25811 reading of the chain. As such dependencies remain valid it is not much
25812 useful to track and undo them during QUIT cleanups. */
25813 if (per_cu->cu == NULL)
25814 return 1;
25815
25816 if (per_cu->cu->mark)
25817 return 1;
25818 per_cu->cu->mark = 1;
25819
25820 if (per_cu->cu->dependencies != NULL)
25821 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25822
25823 return 1;
25824 }
25825
25826 /* Set the mark field in CU and in every other compilation unit in the
25827 cache that we must keep because we are keeping CU. */
25828
25829 static void
25830 dwarf2_mark (struct dwarf2_cu *cu)
25831 {
25832 if (cu->mark)
25833 return;
25834 cu->mark = 1;
25835 if (cu->dependencies != NULL)
25836 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25837 }
25838
25839 static void
25840 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25841 {
25842 while (per_cu)
25843 {
25844 per_cu->cu->mark = 0;
25845 per_cu = per_cu->cu->read_in_chain;
25846 }
25847 }
25848
25849 /* Trivial hash function for partial_die_info: the hash value of a DIE
25850 is its offset in .debug_info for this objfile. */
25851
25852 static hashval_t
25853 partial_die_hash (const void *item)
25854 {
25855 const struct partial_die_info *part_die
25856 = (const struct partial_die_info *) item;
25857
25858 return to_underlying (part_die->sect_off);
25859 }
25860
25861 /* Trivial comparison function for partial_die_info structures: two DIEs
25862 are equal if they have the same offset. */
25863
25864 static int
25865 partial_die_eq (const void *item_lhs, const void *item_rhs)
25866 {
25867 const struct partial_die_info *part_die_lhs
25868 = (const struct partial_die_info *) item_lhs;
25869 const struct partial_die_info *part_die_rhs
25870 = (const struct partial_die_info *) item_rhs;
25871
25872 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25873 }
25874
25875 static struct cmd_list_element *set_dwarf_cmdlist;
25876 static struct cmd_list_element *show_dwarf_cmdlist;
25877
25878 static void
25879 set_dwarf_cmd (const char *args, int from_tty)
25880 {
25881 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25882 gdb_stdout);
25883 }
25884
25885 static void
25886 show_dwarf_cmd (const char *args, int from_tty)
25887 {
25888 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25889 }
25890
25891 /* The "save gdb-index" command. */
25892
25893 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25894 error checking. */
25895
25896 static void
25897 file_write (FILE *file, const void *data, size_t size)
25898 {
25899 if (fwrite (data, 1, size, file) != size)
25900 error (_("couldn't data write to file"));
25901 }
25902
25903 /* Write the contents of VEC to FILE, with error checking. */
25904
25905 template<typename Elem, typename Alloc>
25906 static void
25907 file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25908 {
25909 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25910 }
25911
25912 /* In-memory buffer to prepare data to be written later to a file. */
25913 class data_buf
25914 {
25915 public:
25916 /* Copy DATA to the end of the buffer. */
25917 template<typename T>
25918 void append_data (const T &data)
25919 {
25920 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25921 reinterpret_cast<const gdb_byte *> (&data + 1),
25922 grow (sizeof (data)));
25923 }
25924
25925 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25926 terminating zero is appended too. */
25927 void append_cstr0 (const char *cstr)
25928 {
25929 const size_t size = strlen (cstr) + 1;
25930 std::copy (cstr, cstr + size, grow (size));
25931 }
25932
25933 /* Store INPUT as ULEB128 to the end of buffer. */
25934 void append_unsigned_leb128 (ULONGEST input)
25935 {
25936 for (;;)
25937 {
25938 gdb_byte output = input & 0x7f;
25939 input >>= 7;
25940 if (input)
25941 output |= 0x80;
25942 append_data (output);
25943 if (input == 0)
25944 break;
25945 }
25946 }
25947
25948 /* Accept a host-format integer in VAL and append it to the buffer
25949 as a target-format integer which is LEN bytes long. */
25950 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25951 {
25952 ::store_unsigned_integer (grow (len), len, byte_order, val);
25953 }
25954
25955 /* Return the size of the buffer. */
25956 size_t size () const
25957 {
25958 return m_vec.size ();
25959 }
25960
25961 /* Return true iff the buffer is empty. */
25962 bool empty () const
25963 {
25964 return m_vec.empty ();
25965 }
25966
25967 /* Write the buffer to FILE. */
25968 void file_write (FILE *file) const
25969 {
25970 ::file_write (file, m_vec);
25971 }
25972
25973 private:
25974 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25975 the start of the new block. */
25976 gdb_byte *grow (size_t size)
25977 {
25978 m_vec.resize (m_vec.size () + size);
25979 return &*m_vec.end () - size;
25980 }
25981
25982 gdb::byte_vector m_vec;
25983 };
25984
25985 /* An entry in the symbol table. */
25986 struct symtab_index_entry
25987 {
25988 /* The name of the symbol. */
25989 const char *name;
25990 /* The offset of the name in the constant pool. */
25991 offset_type index_offset;
25992 /* A sorted vector of the indices of all the CUs that hold an object
25993 of this name. */
25994 std::vector<offset_type> cu_indices;
25995 };
25996
25997 /* The symbol table. This is a power-of-2-sized hash table. */
25998 struct mapped_symtab
25999 {
26000 mapped_symtab ()
26001 {
26002 data.resize (1024);
26003 }
26004
26005 offset_type n_elements = 0;
26006 std::vector<symtab_index_entry> data;
26007 };
26008
26009 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
26010 the slot.
26011
26012 Function is used only during write_hash_table so no index format backward
26013 compatibility is needed. */
26014
26015 static symtab_index_entry &
26016 find_slot (struct mapped_symtab *symtab, const char *name)
26017 {
26018 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
26019
26020 index = hash & (symtab->data.size () - 1);
26021 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
26022
26023 for (;;)
26024 {
26025 if (symtab->data[index].name == NULL
26026 || strcmp (name, symtab->data[index].name) == 0)
26027 return symtab->data[index];
26028 index = (index + step) & (symtab->data.size () - 1);
26029 }
26030 }
26031
26032 /* Expand SYMTAB's hash table. */
26033
26034 static void
26035 hash_expand (struct mapped_symtab *symtab)
26036 {
26037 auto old_entries = std::move (symtab->data);
26038
26039 symtab->data.clear ();
26040 symtab->data.resize (old_entries.size () * 2);
26041
26042 for (auto &it : old_entries)
26043 if (it.name != NULL)
26044 {
26045 auto &ref = find_slot (symtab, it.name);
26046 ref = std::move (it);
26047 }
26048 }
26049
26050 /* Add an entry to SYMTAB. NAME is the name of the symbol.
26051 CU_INDEX is the index of the CU in which the symbol appears.
26052 IS_STATIC is one if the symbol is static, otherwise zero (global). */
26053
26054 static void
26055 add_index_entry (struct mapped_symtab *symtab, const char *name,
26056 int is_static, gdb_index_symbol_kind kind,
26057 offset_type cu_index)
26058 {
26059 offset_type cu_index_and_attrs;
26060
26061 ++symtab->n_elements;
26062 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
26063 hash_expand (symtab);
26064
26065 symtab_index_entry &slot = find_slot (symtab, name);
26066 if (slot.name == NULL)
26067 {
26068 slot.name = name;
26069 /* index_offset is set later. */
26070 }
26071
26072 cu_index_and_attrs = 0;
26073 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
26074 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
26075 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
26076
26077 /* We don't want to record an index value twice as we want to avoid the
26078 duplication.
26079 We process all global symbols and then all static symbols
26080 (which would allow us to avoid the duplication by only having to check
26081 the last entry pushed), but a symbol could have multiple kinds in one CU.
26082 To keep things simple we don't worry about the duplication here and
26083 sort and uniqufy the list after we've processed all symbols. */
26084 slot.cu_indices.push_back (cu_index_and_attrs);
26085 }
26086
26087 /* Sort and remove duplicates of all symbols' cu_indices lists. */
26088
26089 static void
26090 uniquify_cu_indices (struct mapped_symtab *symtab)
26091 {
26092 for (auto &entry : symtab->data)
26093 {
26094 if (entry.name != NULL && !entry.cu_indices.empty ())
26095 {
26096 auto &cu_indices = entry.cu_indices;
26097 std::sort (cu_indices.begin (), cu_indices.end ());
26098 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
26099 cu_indices.erase (from, cu_indices.end ());
26100 }
26101 }
26102 }
26103
26104 /* A form of 'const char *' suitable for container keys. Only the
26105 pointer is stored. The strings themselves are compared, not the
26106 pointers. */
26107 class c_str_view
26108 {
26109 public:
26110 c_str_view (const char *cstr)
26111 : m_cstr (cstr)
26112 {}
26113
26114 bool operator== (const c_str_view &other) const
26115 {
26116 return strcmp (m_cstr, other.m_cstr) == 0;
26117 }
26118
26119 /* Return the underlying C string. Note, the returned string is
26120 only a reference with lifetime of this object. */
26121 const char *c_str () const
26122 {
26123 return m_cstr;
26124 }
26125
26126 private:
26127 friend class c_str_view_hasher;
26128 const char *const m_cstr;
26129 };
26130
26131 /* A std::unordered_map::hasher for c_str_view that uses the right
26132 hash function for strings in a mapped index. */
26133 class c_str_view_hasher
26134 {
26135 public:
26136 size_t operator () (const c_str_view &x) const
26137 {
26138 return mapped_index_string_hash (INT_MAX, x.m_cstr);
26139 }
26140 };
26141
26142 /* A std::unordered_map::hasher for std::vector<>. */
26143 template<typename T>
26144 class vector_hasher
26145 {
26146 public:
26147 size_t operator () (const std::vector<T> &key) const
26148 {
26149 return iterative_hash (key.data (),
26150 sizeof (key.front ()) * key.size (), 0);
26151 }
26152 };
26153
26154 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
26155 constant pool entries going into the data buffer CPOOL. */
26156
26157 static void
26158 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
26159 {
26160 {
26161 /* Elements are sorted vectors of the indices of all the CUs that
26162 hold an object of this name. */
26163 std::unordered_map<std::vector<offset_type>, offset_type,
26164 vector_hasher<offset_type>>
26165 symbol_hash_table;
26166
26167 /* We add all the index vectors to the constant pool first, to
26168 ensure alignment is ok. */
26169 for (symtab_index_entry &entry : symtab->data)
26170 {
26171 if (entry.name == NULL)
26172 continue;
26173 gdb_assert (entry.index_offset == 0);
26174
26175 /* Finding before inserting is faster than always trying to
26176 insert, because inserting always allocates a node, does the
26177 lookup, and then destroys the new node if another node
26178 already had the same key. C++17 try_emplace will avoid
26179 this. */
26180 const auto found
26181 = symbol_hash_table.find (entry.cu_indices);
26182 if (found != symbol_hash_table.end ())
26183 {
26184 entry.index_offset = found->second;
26185 continue;
26186 }
26187
26188 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
26189 entry.index_offset = cpool.size ();
26190 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
26191 for (const auto index : entry.cu_indices)
26192 cpool.append_data (MAYBE_SWAP (index));
26193 }
26194 }
26195
26196 /* Now write out the hash table. */
26197 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
26198 for (const auto &entry : symtab->data)
26199 {
26200 offset_type str_off, vec_off;
26201
26202 if (entry.name != NULL)
26203 {
26204 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
26205 if (insertpair.second)
26206 cpool.append_cstr0 (entry.name);
26207 str_off = insertpair.first->second;
26208 vec_off = entry.index_offset;
26209 }
26210 else
26211 {
26212 /* While 0 is a valid constant pool index, it is not valid
26213 to have 0 for both offsets. */
26214 str_off = 0;
26215 vec_off = 0;
26216 }
26217
26218 output.append_data (MAYBE_SWAP (str_off));
26219 output.append_data (MAYBE_SWAP (vec_off));
26220 }
26221 }
26222
26223 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
26224
26225 /* Helper struct for building the address table. */
26226 struct addrmap_index_data
26227 {
26228 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
26229 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
26230 {}
26231
26232 struct objfile *objfile;
26233 data_buf &addr_vec;
26234 psym_index_map &cu_index_htab;
26235
26236 /* Non-zero if the previous_* fields are valid.
26237 We can't write an entry until we see the next entry (since it is only then
26238 that we know the end of the entry). */
26239 int previous_valid;
26240 /* Index of the CU in the table of all CUs in the index file. */
26241 unsigned int previous_cu_index;
26242 /* Start address of the CU. */
26243 CORE_ADDR previous_cu_start;
26244 };
26245
26246 /* Write an address entry to ADDR_VEC. */
26247
26248 static void
26249 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
26250 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
26251 {
26252 CORE_ADDR baseaddr;
26253
26254 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
26255
26256 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
26257 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
26258 addr_vec.append_data (MAYBE_SWAP (cu_index));
26259 }
26260
26261 /* Worker function for traversing an addrmap to build the address table. */
26262
26263 static int
26264 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
26265 {
26266 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
26267 struct partial_symtab *pst = (struct partial_symtab *) obj;
26268
26269 if (data->previous_valid)
26270 add_address_entry (data->objfile, data->addr_vec,
26271 data->previous_cu_start, start_addr,
26272 data->previous_cu_index);
26273
26274 data->previous_cu_start = start_addr;
26275 if (pst != NULL)
26276 {
26277 const auto it = data->cu_index_htab.find (pst);
26278 gdb_assert (it != data->cu_index_htab.cend ());
26279 data->previous_cu_index = it->second;
26280 data->previous_valid = 1;
26281 }
26282 else
26283 data->previous_valid = 0;
26284
26285 return 0;
26286 }
26287
26288 /* Write OBJFILE's address map to ADDR_VEC.
26289 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
26290 in the index file. */
26291
26292 static void
26293 write_address_map (struct objfile *objfile, data_buf &addr_vec,
26294 psym_index_map &cu_index_htab)
26295 {
26296 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
26297
26298 /* When writing the address table, we have to cope with the fact that
26299 the addrmap iterator only provides the start of a region; we have to
26300 wait until the next invocation to get the start of the next region. */
26301
26302 addrmap_index_data.objfile = objfile;
26303 addrmap_index_data.previous_valid = 0;
26304
26305 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
26306 &addrmap_index_data);
26307
26308 /* It's highly unlikely the last entry (end address = 0xff...ff)
26309 is valid, but we should still handle it.
26310 The end address is recorded as the start of the next region, but that
26311 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
26312 anyway. */
26313 if (addrmap_index_data.previous_valid)
26314 add_address_entry (objfile, addr_vec,
26315 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
26316 addrmap_index_data.previous_cu_index);
26317 }
26318
26319 /* Return the symbol kind of PSYM. */
26320
26321 static gdb_index_symbol_kind
26322 symbol_kind (struct partial_symbol *psym)
26323 {
26324 domain_enum domain = PSYMBOL_DOMAIN (psym);
26325 enum address_class aclass = PSYMBOL_CLASS (psym);
26326
26327 switch (domain)
26328 {
26329 case VAR_DOMAIN:
26330 switch (aclass)
26331 {
26332 case LOC_BLOCK:
26333 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
26334 case LOC_TYPEDEF:
26335 return GDB_INDEX_SYMBOL_KIND_TYPE;
26336 case LOC_COMPUTED:
26337 case LOC_CONST_BYTES:
26338 case LOC_OPTIMIZED_OUT:
26339 case LOC_STATIC:
26340 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
26341 case LOC_CONST:
26342 /* Note: It's currently impossible to recognize psyms as enum values
26343 short of reading the type info. For now punt. */
26344 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
26345 default:
26346 /* There are other LOC_FOO values that one might want to classify
26347 as variables, but dwarf2read.c doesn't currently use them. */
26348 return GDB_INDEX_SYMBOL_KIND_OTHER;
26349 }
26350 case STRUCT_DOMAIN:
26351 return GDB_INDEX_SYMBOL_KIND_TYPE;
26352 default:
26353 return GDB_INDEX_SYMBOL_KIND_OTHER;
26354 }
26355 }
26356
26357 /* Add a list of partial symbols to SYMTAB. */
26358
26359 static void
26360 write_psymbols (struct mapped_symtab *symtab,
26361 std::unordered_set<partial_symbol *> &psyms_seen,
26362 struct partial_symbol **psymp,
26363 int count,
26364 offset_type cu_index,
26365 int is_static)
26366 {
26367 for (; count-- > 0; ++psymp)
26368 {
26369 struct partial_symbol *psym = *psymp;
26370
26371 if (SYMBOL_LANGUAGE (psym) == language_ada)
26372 error (_("Ada is not currently supported by the index"));
26373
26374 /* Only add a given psymbol once. */
26375 if (psyms_seen.insert (psym).second)
26376 {
26377 gdb_index_symbol_kind kind = symbol_kind (psym);
26378
26379 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
26380 is_static, kind, cu_index);
26381 }
26382 }
26383 }
26384
26385 /* A helper struct used when iterating over debug_types. */
26386 struct signatured_type_index_data
26387 {
26388 signatured_type_index_data (data_buf &types_list_,
26389 std::unordered_set<partial_symbol *> &psyms_seen_)
26390 : types_list (types_list_), psyms_seen (psyms_seen_)
26391 {}
26392
26393 struct objfile *objfile;
26394 struct mapped_symtab *symtab;
26395 data_buf &types_list;
26396 std::unordered_set<partial_symbol *> &psyms_seen;
26397 int cu_index;
26398 };
26399
26400 /* A helper function that writes a single signatured_type to an
26401 obstack. */
26402
26403 static int
26404 write_one_signatured_type (void **slot, void *d)
26405 {
26406 struct signatured_type_index_data *info
26407 = (struct signatured_type_index_data *) d;
26408 struct signatured_type *entry = (struct signatured_type *) *slot;
26409 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26410
26411 write_psymbols (info->symtab,
26412 info->psyms_seen,
26413 &info->objfile->global_psymbols[psymtab->globals_offset],
26414 psymtab->n_global_syms, info->cu_index,
26415 0);
26416 write_psymbols (info->symtab,
26417 info->psyms_seen,
26418 &info->objfile->static_psymbols[psymtab->statics_offset],
26419 psymtab->n_static_syms, info->cu_index,
26420 1);
26421
26422 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26423 to_underlying (entry->per_cu.sect_off));
26424 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26425 to_underlying (entry->type_offset_in_tu));
26426 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
26427
26428 ++info->cu_index;
26429
26430 return 1;
26431 }
26432
26433 /* Recurse into all "included" dependencies and count their symbols as
26434 if they appeared in this psymtab. */
26435
26436 static void
26437 recursively_count_psymbols (struct partial_symtab *psymtab,
26438 size_t &psyms_seen)
26439 {
26440 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26441 if (psymtab->dependencies[i]->user != NULL)
26442 recursively_count_psymbols (psymtab->dependencies[i],
26443 psyms_seen);
26444
26445 psyms_seen += psymtab->n_global_syms;
26446 psyms_seen += psymtab->n_static_syms;
26447 }
26448
26449 /* Recurse into all "included" dependencies and write their symbols as
26450 if they appeared in this psymtab. */
26451
26452 static void
26453 recursively_write_psymbols (struct objfile *objfile,
26454 struct partial_symtab *psymtab,
26455 struct mapped_symtab *symtab,
26456 std::unordered_set<partial_symbol *> &psyms_seen,
26457 offset_type cu_index)
26458 {
26459 int i;
26460
26461 for (i = 0; i < psymtab->number_of_dependencies; ++i)
26462 if (psymtab->dependencies[i]->user != NULL)
26463 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26464 symtab, psyms_seen, cu_index);
26465
26466 write_psymbols (symtab,
26467 psyms_seen,
26468 &objfile->global_psymbols[psymtab->globals_offset],
26469 psymtab->n_global_syms, cu_index,
26470 0);
26471 write_psymbols (symtab,
26472 psyms_seen,
26473 &objfile->static_psymbols[psymtab->statics_offset],
26474 psymtab->n_static_syms, cu_index,
26475 1);
26476 }
26477
26478 /* DWARF-5 .debug_names builder. */
26479 class debug_names
26480 {
26481 public:
26482 debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile, bool is_dwarf64,
26483 bfd_endian dwarf5_byte_order)
26484 : m_dwarf5_byte_order (dwarf5_byte_order),
26485 m_dwarf32 (dwarf5_byte_order),
26486 m_dwarf64 (dwarf5_byte_order),
26487 m_dwarf (is_dwarf64
26488 ? static_cast<dwarf &> (m_dwarf64)
26489 : static_cast<dwarf &> (m_dwarf32)),
26490 m_name_table_string_offs (m_dwarf.name_table_string_offs),
26491 m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
26492 m_debugstrlookup (dwarf2_per_objfile)
26493 {}
26494
26495 int dwarf5_offset_size () const
26496 {
26497 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
26498 return dwarf5_is_dwarf64 ? 8 : 4;
26499 }
26500
26501 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
26502 enum class unit_kind { cu, tu };
26503
26504 /* Insert one symbol. */
26505 void insert (const partial_symbol *psym, int cu_index, bool is_static,
26506 unit_kind kind)
26507 {
26508 const int dwarf_tag = psymbol_tag (psym);
26509 if (dwarf_tag == 0)
26510 return;
26511 const char *const name = SYMBOL_SEARCH_NAME (psym);
26512 const auto insertpair
26513 = m_name_to_value_set.emplace (c_str_view (name),
26514 std::set<symbol_value> ());
26515 std::set<symbol_value> &value_set = insertpair.first->second;
26516 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
26517 }
26518
26519 /* Build all the tables. All symbols must be already inserted.
26520 This function does not call file_write, caller has to do it
26521 afterwards. */
26522 void build ()
26523 {
26524 /* Verify the build method has not be called twice. */
26525 gdb_assert (m_abbrev_table.empty ());
26526 const size_t name_count = m_name_to_value_set.size ();
26527 m_bucket_table.resize
26528 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26529 m_hash_table.reserve (name_count);
26530 m_name_table_string_offs.reserve (name_count);
26531 m_name_table_entry_offs.reserve (name_count);
26532
26533 /* Map each hash of symbol to its name and value. */
26534 struct hash_it_pair
26535 {
26536 uint32_t hash;
26537 decltype (m_name_to_value_set)::const_iterator it;
26538 };
26539 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26540 bucket_hash.resize (m_bucket_table.size ());
26541 for (decltype (m_name_to_value_set)::const_iterator it
26542 = m_name_to_value_set.cbegin ();
26543 it != m_name_to_value_set.cend ();
26544 ++it)
26545 {
26546 const char *const name = it->first.c_str ();
26547 const uint32_t hash = dwarf5_djb_hash (name);
26548 hash_it_pair hashitpair;
26549 hashitpair.hash = hash;
26550 hashitpair.it = it;
26551 auto &slot = bucket_hash[hash % bucket_hash.size()];
26552 slot.push_front (std::move (hashitpair));
26553 }
26554 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26555 {
26556 const std::forward_list<hash_it_pair> &hashitlist
26557 = bucket_hash[bucket_ix];
26558 if (hashitlist.empty ())
26559 continue;
26560 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26561 /* The hashes array is indexed starting at 1. */
26562 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26563 sizeof (bucket_slot), m_dwarf5_byte_order,
26564 m_hash_table.size () + 1);
26565 for (const hash_it_pair &hashitpair : hashitlist)
26566 {
26567 m_hash_table.push_back (0);
26568 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26569 (&m_hash_table.back ()),
26570 sizeof (m_hash_table.back ()),
26571 m_dwarf5_byte_order, hashitpair.hash);
26572 const c_str_view &name = hashitpair.it->first;
26573 const std::set<symbol_value> &value_set = hashitpair.it->second;
26574 m_name_table_string_offs.push_back_reorder
26575 (m_debugstrlookup.lookup (name.c_str ()));
26576 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26577 gdb_assert (!value_set.empty ());
26578 for (const symbol_value &value : value_set)
26579 {
26580 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
26581 value.is_static,
26582 value.kind)];
26583 if (idx == 0)
26584 {
26585 idx = m_idx_next++;
26586 m_abbrev_table.append_unsigned_leb128 (idx);
26587 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
26588 m_abbrev_table.append_unsigned_leb128
26589 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
26590 : DW_IDX_type_unit);
26591 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26592 m_abbrev_table.append_unsigned_leb128 (value.is_static
26593 ? DW_IDX_GNU_internal
26594 : DW_IDX_GNU_external);
26595 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26596
26597 /* Terminate attributes list. */
26598 m_abbrev_table.append_unsigned_leb128 (0);
26599 m_abbrev_table.append_unsigned_leb128 (0);
26600 }
26601
26602 m_entry_pool.append_unsigned_leb128 (idx);
26603 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26604 }
26605
26606 /* Terminate the list of CUs. */
26607 m_entry_pool.append_unsigned_leb128 (0);
26608 }
26609 }
26610 gdb_assert (m_hash_table.size () == name_count);
26611
26612 /* Terminate tags list. */
26613 m_abbrev_table.append_unsigned_leb128 (0);
26614 }
26615
26616 /* Return .debug_names bucket count. This must be called only after
26617 calling the build method. */
26618 uint32_t bucket_count () const
26619 {
26620 /* Verify the build method has been already called. */
26621 gdb_assert (!m_abbrev_table.empty ());
26622 const uint32_t retval = m_bucket_table.size ();
26623
26624 /* Check for overflow. */
26625 gdb_assert (retval == m_bucket_table.size ());
26626 return retval;
26627 }
26628
26629 /* Return .debug_names names count. This must be called only after
26630 calling the build method. */
26631 uint32_t name_count () const
26632 {
26633 /* Verify the build method has been already called. */
26634 gdb_assert (!m_abbrev_table.empty ());
26635 const uint32_t retval = m_hash_table.size ();
26636
26637 /* Check for overflow. */
26638 gdb_assert (retval == m_hash_table.size ());
26639 return retval;
26640 }
26641
26642 /* Return number of bytes of .debug_names abbreviation table. This
26643 must be called only after calling the build method. */
26644 uint32_t abbrev_table_bytes () const
26645 {
26646 gdb_assert (!m_abbrev_table.empty ());
26647 return m_abbrev_table.size ();
26648 }
26649
26650 /* Recurse into all "included" dependencies and store their symbols
26651 as if they appeared in this psymtab. */
26652 void recursively_write_psymbols
26653 (struct objfile *objfile,
26654 struct partial_symtab *psymtab,
26655 std::unordered_set<partial_symbol *> &psyms_seen,
26656 int cu_index)
26657 {
26658 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26659 if (psymtab->dependencies[i]->user != NULL)
26660 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26661 psyms_seen, cu_index);
26662
26663 write_psymbols (psyms_seen,
26664 &objfile->global_psymbols[psymtab->globals_offset],
26665 psymtab->n_global_syms, cu_index, false, unit_kind::cu);
26666 write_psymbols (psyms_seen,
26667 &objfile->static_psymbols[psymtab->statics_offset],
26668 psymtab->n_static_syms, cu_index, true, unit_kind::cu);
26669 }
26670
26671 /* Return number of bytes the .debug_names section will have. This
26672 must be called only after calling the build method. */
26673 size_t bytes () const
26674 {
26675 /* Verify the build method has been already called. */
26676 gdb_assert (!m_abbrev_table.empty ());
26677 size_t expected_bytes = 0;
26678 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26679 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26680 expected_bytes += m_name_table_string_offs.bytes ();
26681 expected_bytes += m_name_table_entry_offs.bytes ();
26682 expected_bytes += m_abbrev_table.size ();
26683 expected_bytes += m_entry_pool.size ();
26684 return expected_bytes;
26685 }
26686
26687 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26688 FILE_STR. This must be called only after calling the build
26689 method. */
26690 void file_write (FILE *file_names, FILE *file_str) const
26691 {
26692 /* Verify the build method has been already called. */
26693 gdb_assert (!m_abbrev_table.empty ());
26694 ::file_write (file_names, m_bucket_table);
26695 ::file_write (file_names, m_hash_table);
26696 m_name_table_string_offs.file_write (file_names);
26697 m_name_table_entry_offs.file_write (file_names);
26698 m_abbrev_table.file_write (file_names);
26699 m_entry_pool.file_write (file_names);
26700 m_debugstrlookup.file_write (file_str);
26701 }
26702
26703 /* A helper user data for write_one_signatured_type. */
26704 class write_one_signatured_type_data
26705 {
26706 public:
26707 write_one_signatured_type_data (debug_names &nametable_,
26708 signatured_type_index_data &&info_)
26709 : nametable (nametable_), info (std::move (info_))
26710 {}
26711 debug_names &nametable;
26712 struct signatured_type_index_data info;
26713 };
26714
26715 /* A helper function to pass write_one_signatured_type to
26716 htab_traverse_noresize. */
26717 static int
26718 write_one_signatured_type (void **slot, void *d)
26719 {
26720 write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
26721 struct signatured_type_index_data *info = &data->info;
26722 struct signatured_type *entry = (struct signatured_type *) *slot;
26723
26724 data->nametable.write_one_signatured_type (entry, info);
26725
26726 return 1;
26727 }
26728
26729 private:
26730
26731 /* Storage for symbol names mapping them to their .debug_str section
26732 offsets. */
26733 class debug_str_lookup
26734 {
26735 public:
26736
26737 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26738 All .debug_str section strings are automatically stored. */
26739 debug_str_lookup (struct dwarf2_per_objfile *dwarf2_per_objfile)
26740 : m_abfd (dwarf2_per_objfile->objfile->obfd),
26741 m_dwarf2_per_objfile (dwarf2_per_objfile)
26742 {
26743 dwarf2_read_section (dwarf2_per_objfile->objfile,
26744 &dwarf2_per_objfile->str);
26745 if (dwarf2_per_objfile->str.buffer == NULL)
26746 return;
26747 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26748 data < (dwarf2_per_objfile->str.buffer
26749 + dwarf2_per_objfile->str.size);)
26750 {
26751 const char *const s = reinterpret_cast<const char *> (data);
26752 const auto insertpair
26753 = m_str_table.emplace (c_str_view (s),
26754 data - dwarf2_per_objfile->str.buffer);
26755 if (!insertpair.second)
26756 complaint (&symfile_complaints,
26757 _("Duplicate string \"%s\" in "
26758 ".debug_str section [in module %s]"),
26759 s, bfd_get_filename (m_abfd));
26760 data += strlen (s) + 1;
26761 }
26762 }
26763
26764 /* Return offset of symbol name S in the .debug_str section. Add
26765 such symbol to the section's end if it does not exist there
26766 yet. */
26767 size_t lookup (const char *s)
26768 {
26769 const auto it = m_str_table.find (c_str_view (s));
26770 if (it != m_str_table.end ())
26771 return it->second;
26772 const size_t offset = (m_dwarf2_per_objfile->str.size
26773 + m_str_add_buf.size ());
26774 m_str_table.emplace (c_str_view (s), offset);
26775 m_str_add_buf.append_cstr0 (s);
26776 return offset;
26777 }
26778
26779 /* Append the end of the .debug_str section to FILE. */
26780 void file_write (FILE *file) const
26781 {
26782 m_str_add_buf.file_write (file);
26783 }
26784
26785 private:
26786 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26787 bfd *const m_abfd;
26788 struct dwarf2_per_objfile *m_dwarf2_per_objfile;
26789
26790 /* Data to add at the end of .debug_str for new needed symbol names. */
26791 data_buf m_str_add_buf;
26792 };
26793
26794 /* Container to map used DWARF tags to their .debug_names abbreviation
26795 tags. */
26796 class index_key
26797 {
26798 public:
26799 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
26800 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
26801 {
26802 }
26803
26804 bool
26805 operator== (const index_key &other) const
26806 {
26807 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
26808 && kind == other.kind);
26809 }
26810
26811 const int dwarf_tag;
26812 const bool is_static;
26813 const unit_kind kind;
26814 };
26815
26816 /* Provide std::unordered_map::hasher for index_key. */
26817 class index_key_hasher
26818 {
26819 public:
26820 size_t
26821 operator () (const index_key &key) const
26822 {
26823 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26824 }
26825 };
26826
26827 /* Parameters of one symbol entry. */
26828 class symbol_value
26829 {
26830 public:
26831 const int dwarf_tag, cu_index;
26832 const bool is_static;
26833 const unit_kind kind;
26834
26835 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
26836 unit_kind kind_)
26837 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
26838 kind (kind_)
26839 {}
26840
26841 bool
26842 operator< (const symbol_value &other) const
26843 {
26844 #define X(n) \
26845 do \
26846 { \
26847 if (n < other.n) \
26848 return true; \
26849 if (n > other.n) \
26850 return false; \
26851 } \
26852 while (0)
26853 X (dwarf_tag);
26854 X (is_static);
26855 X (kind);
26856 X (cu_index);
26857 #undef X
26858 return false;
26859 }
26860 };
26861
26862 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26863 output. */
26864 class offset_vec
26865 {
26866 protected:
26867 const bfd_endian dwarf5_byte_order;
26868 public:
26869 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26870 : dwarf5_byte_order (dwarf5_byte_order_)
26871 {}
26872
26873 /* Call std::vector::reserve for NELEM elements. */
26874 virtual void reserve (size_t nelem) = 0;
26875
26876 /* Call std::vector::push_back with store_unsigned_integer byte
26877 reordering for ELEM. */
26878 virtual void push_back_reorder (size_t elem) = 0;
26879
26880 /* Return expected output size in bytes. */
26881 virtual size_t bytes () const = 0;
26882
26883 /* Write name table to FILE. */
26884 virtual void file_write (FILE *file) const = 0;
26885 };
26886
26887 /* Template to unify DWARF-32 and DWARF-64 output. */
26888 template<typename OffsetSize>
26889 class offset_vec_tmpl : public offset_vec
26890 {
26891 public:
26892 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26893 : offset_vec (dwarf5_byte_order_)
26894 {}
26895
26896 /* Implement offset_vec::reserve. */
26897 void reserve (size_t nelem) override
26898 {
26899 m_vec.reserve (nelem);
26900 }
26901
26902 /* Implement offset_vec::push_back_reorder. */
26903 void push_back_reorder (size_t elem) override
26904 {
26905 m_vec.push_back (elem);
26906 /* Check for overflow. */
26907 gdb_assert (m_vec.back () == elem);
26908 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26909 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26910 }
26911
26912 /* Implement offset_vec::bytes. */
26913 size_t bytes () const override
26914 {
26915 return m_vec.size () * sizeof (m_vec[0]);
26916 }
26917
26918 /* Implement offset_vec::file_write. */
26919 void file_write (FILE *file) const override
26920 {
26921 ::file_write (file, m_vec);
26922 }
26923
26924 private:
26925 std::vector<OffsetSize> m_vec;
26926 };
26927
26928 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26929 respecting name table width. */
26930 class dwarf
26931 {
26932 public:
26933 offset_vec &name_table_string_offs, &name_table_entry_offs;
26934
26935 dwarf (offset_vec &name_table_string_offs_,
26936 offset_vec &name_table_entry_offs_)
26937 : name_table_string_offs (name_table_string_offs_),
26938 name_table_entry_offs (name_table_entry_offs_)
26939 {
26940 }
26941 };
26942
26943 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26944 respecting name table width. */
26945 template<typename OffsetSize>
26946 class dwarf_tmpl : public dwarf
26947 {
26948 public:
26949 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26950 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26951 m_name_table_string_offs (dwarf5_byte_order_),
26952 m_name_table_entry_offs (dwarf5_byte_order_)
26953 {}
26954
26955 private:
26956 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26957 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26958 };
26959
26960 /* Try to reconstruct original DWARF tag for given partial_symbol.
26961 This function is not DWARF-5 compliant but it is sufficient for
26962 GDB as a DWARF-5 index consumer. */
26963 static int psymbol_tag (const struct partial_symbol *psym)
26964 {
26965 domain_enum domain = PSYMBOL_DOMAIN (psym);
26966 enum address_class aclass = PSYMBOL_CLASS (psym);
26967
26968 switch (domain)
26969 {
26970 case VAR_DOMAIN:
26971 switch (aclass)
26972 {
26973 case LOC_BLOCK:
26974 return DW_TAG_subprogram;
26975 case LOC_TYPEDEF:
26976 return DW_TAG_typedef;
26977 case LOC_COMPUTED:
26978 case LOC_CONST_BYTES:
26979 case LOC_OPTIMIZED_OUT:
26980 case LOC_STATIC:
26981 return DW_TAG_variable;
26982 case LOC_CONST:
26983 /* Note: It's currently impossible to recognize psyms as enum values
26984 short of reading the type info. For now punt. */
26985 return DW_TAG_variable;
26986 default:
26987 /* There are other LOC_FOO values that one might want to classify
26988 as variables, but dwarf2read.c doesn't currently use them. */
26989 return DW_TAG_variable;
26990 }
26991 case STRUCT_DOMAIN:
26992 return DW_TAG_structure_type;
26993 default:
26994 return 0;
26995 }
26996 }
26997
26998 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26999 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
27000 struct partial_symbol **psymp, int count, int cu_index,
27001 bool is_static, unit_kind kind)
27002 {
27003 for (; count-- > 0; ++psymp)
27004 {
27005 struct partial_symbol *psym = *psymp;
27006
27007 if (SYMBOL_LANGUAGE (psym) == language_ada)
27008 error (_("Ada is not currently supported by the index"));
27009
27010 /* Only add a given psymbol once. */
27011 if (psyms_seen.insert (psym).second)
27012 insert (psym, cu_index, is_static, kind);
27013 }
27014 }
27015
27016 /* A helper function that writes a single signatured_type
27017 to a debug_names. */
27018 void
27019 write_one_signatured_type (struct signatured_type *entry,
27020 struct signatured_type_index_data *info)
27021 {
27022 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
27023
27024 write_psymbols (info->psyms_seen,
27025 &info->objfile->global_psymbols[psymtab->globals_offset],
27026 psymtab->n_global_syms, info->cu_index, false,
27027 unit_kind::tu);
27028 write_psymbols (info->psyms_seen,
27029 &info->objfile->static_psymbols[psymtab->statics_offset],
27030 psymtab->n_static_syms, info->cu_index, true,
27031 unit_kind::tu);
27032
27033 info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
27034 to_underlying (entry->per_cu.sect_off));
27035
27036 ++info->cu_index;
27037 }
27038
27039 /* Store value of each symbol. */
27040 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
27041 m_name_to_value_set;
27042
27043 /* Tables of DWARF-5 .debug_names. They are in object file byte
27044 order. */
27045 std::vector<uint32_t> m_bucket_table;
27046 std::vector<uint32_t> m_hash_table;
27047
27048 const bfd_endian m_dwarf5_byte_order;
27049 dwarf_tmpl<uint32_t> m_dwarf32;
27050 dwarf_tmpl<uint64_t> m_dwarf64;
27051 dwarf &m_dwarf;
27052 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
27053 debug_str_lookup m_debugstrlookup;
27054
27055 /* Map each used .debug_names abbreviation tag parameter to its
27056 index value. */
27057 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
27058
27059 /* Next unused .debug_names abbreviation tag for
27060 m_indexkey_to_idx. */
27061 int m_idx_next = 1;
27062
27063 /* .debug_names abbreviation table. */
27064 data_buf m_abbrev_table;
27065
27066 /* .debug_names entry pool. */
27067 data_buf m_entry_pool;
27068 };
27069
27070 /* Return iff any of the needed offsets does not fit into 32-bit
27071 .debug_names section. */
27072
27073 static bool
27074 check_dwarf64_offsets (struct dwarf2_per_objfile *dwarf2_per_objfile)
27075 {
27076 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27077 {
27078 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
27079
27080 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
27081 return true;
27082 }
27083 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
27084 {
27085 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
27086 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
27087
27088 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
27089 return true;
27090 }
27091 return false;
27092 }
27093
27094 /* The psyms_seen set is potentially going to be largish (~40k
27095 elements when indexing a -g3 build of GDB itself). Estimate the
27096 number of elements in order to avoid too many rehashes, which
27097 require rebuilding buckets and thus many trips to
27098 malloc/free. */
27099
27100 static size_t
27101 psyms_seen_size (struct dwarf2_per_objfile *dwarf2_per_objfile)
27102 {
27103 size_t psyms_count = 0;
27104 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27105 {
27106 struct dwarf2_per_cu_data *per_cu
27107 = dwarf2_per_objfile->all_comp_units[i];
27108 struct partial_symtab *psymtab = per_cu->v.psymtab;
27109
27110 if (psymtab != NULL && psymtab->user == NULL)
27111 recursively_count_psymbols (psymtab, psyms_count);
27112 }
27113 /* Generating an index for gdb itself shows a ratio of
27114 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
27115 return psyms_count / 4;
27116 }
27117
27118 /* Write new .gdb_index section for OBJFILE into OUT_FILE.
27119 Return how many bytes were expected to be written into OUT_FILE. */
27120
27121 static size_t
27122 write_gdbindex (struct dwarf2_per_objfile *dwarf2_per_objfile, FILE *out_file)
27123 {
27124 struct objfile *objfile = dwarf2_per_objfile->objfile;
27125 mapped_symtab symtab;
27126 data_buf cu_list;
27127
27128 /* While we're scanning CU's create a table that maps a psymtab pointer
27129 (which is what addrmap records) to its index (which is what is recorded
27130 in the index file). This will later be needed to write the address
27131 table. */
27132 psym_index_map cu_index_htab;
27133 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
27134
27135 /* The CU list is already sorted, so we don't need to do additional
27136 work here. Also, the debug_types entries do not appear in
27137 all_comp_units, but only in their own hash table. */
27138
27139 std::unordered_set<partial_symbol *> psyms_seen
27140 (psyms_seen_size (dwarf2_per_objfile));
27141 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27142 {
27143 struct dwarf2_per_cu_data *per_cu
27144 = dwarf2_per_objfile->all_comp_units[i];
27145 struct partial_symtab *psymtab = per_cu->v.psymtab;
27146
27147 /* CU of a shared file from 'dwz -m' may be unused by this main file.
27148 It may be referenced from a local scope but in such case it does not
27149 need to be present in .gdb_index. */
27150 if (psymtab == NULL)
27151 continue;
27152
27153 if (psymtab->user == NULL)
27154 recursively_write_psymbols (objfile, psymtab, &symtab,
27155 psyms_seen, i);
27156
27157 const auto insertpair = cu_index_htab.emplace (psymtab, i);
27158 gdb_assert (insertpair.second);
27159
27160 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
27161 to_underlying (per_cu->sect_off));
27162 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
27163 }
27164
27165 /* Dump the address map. */
27166 data_buf addr_vec;
27167 write_address_map (objfile, addr_vec, cu_index_htab);
27168
27169 /* Write out the .debug_type entries, if any. */
27170 data_buf types_cu_list;
27171 if (dwarf2_per_objfile->signatured_types)
27172 {
27173 signatured_type_index_data sig_data (types_cu_list,
27174 psyms_seen);
27175
27176 sig_data.objfile = objfile;
27177 sig_data.symtab = &symtab;
27178 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
27179 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
27180 write_one_signatured_type, &sig_data);
27181 }
27182
27183 /* Now that we've processed all symbols we can shrink their cu_indices
27184 lists. */
27185 uniquify_cu_indices (&symtab);
27186
27187 data_buf symtab_vec, constant_pool;
27188 write_hash_table (&symtab, symtab_vec, constant_pool);
27189
27190 data_buf contents;
27191 const offset_type size_of_contents = 6 * sizeof (offset_type);
27192 offset_type total_len = size_of_contents;
27193
27194 /* The version number. */
27195 contents.append_data (MAYBE_SWAP (8));
27196
27197 /* The offset of the CU list from the start of the file. */
27198 contents.append_data (MAYBE_SWAP (total_len));
27199 total_len += cu_list.size ();
27200
27201 /* The offset of the types CU list from the start of the file. */
27202 contents.append_data (MAYBE_SWAP (total_len));
27203 total_len += types_cu_list.size ();
27204
27205 /* The offset of the address table from the start of the file. */
27206 contents.append_data (MAYBE_SWAP (total_len));
27207 total_len += addr_vec.size ();
27208
27209 /* The offset of the symbol table from the start of the file. */
27210 contents.append_data (MAYBE_SWAP (total_len));
27211 total_len += symtab_vec.size ();
27212
27213 /* The offset of the constant pool from the start of the file. */
27214 contents.append_data (MAYBE_SWAP (total_len));
27215 total_len += constant_pool.size ();
27216
27217 gdb_assert (contents.size () == size_of_contents);
27218
27219 contents.file_write (out_file);
27220 cu_list.file_write (out_file);
27221 types_cu_list.file_write (out_file);
27222 addr_vec.file_write (out_file);
27223 symtab_vec.file_write (out_file);
27224 constant_pool.file_write (out_file);
27225
27226 return total_len;
27227 }
27228
27229 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
27230 static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
27231
27232 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
27233 needed addition to .debug_str section to OUT_FILE_STR. Return how
27234 many bytes were expected to be written into OUT_FILE. */
27235
27236 static size_t
27237 write_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
27238 FILE *out_file, FILE *out_file_str)
27239 {
27240 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (dwarf2_per_objfile);
27241 struct objfile *objfile = dwarf2_per_objfile->objfile;
27242 const enum bfd_endian dwarf5_byte_order
27243 = gdbarch_byte_order (get_objfile_arch (objfile));
27244
27245 /* The CU list is already sorted, so we don't need to do additional
27246 work here. Also, the debug_types entries do not appear in
27247 all_comp_units, but only in their own hash table. */
27248 data_buf cu_list;
27249 debug_names nametable (dwarf2_per_objfile, dwarf5_is_dwarf64,
27250 dwarf5_byte_order);
27251 std::unordered_set<partial_symbol *>
27252 psyms_seen (psyms_seen_size (dwarf2_per_objfile));
27253 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27254 {
27255 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
27256 partial_symtab *psymtab = per_cu->v.psymtab;
27257
27258 /* CU of a shared file from 'dwz -m' may be unused by this main
27259 file. It may be referenced from a local scope but in such
27260 case it does not need to be present in .debug_names. */
27261 if (psymtab == NULL)
27262 continue;
27263
27264 if (psymtab->user == NULL)
27265 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
27266
27267 cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
27268 to_underlying (per_cu->sect_off));
27269 }
27270
27271 /* Write out the .debug_type entries, if any. */
27272 data_buf types_cu_list;
27273 if (dwarf2_per_objfile->signatured_types)
27274 {
27275 debug_names::write_one_signatured_type_data sig_data (nametable,
27276 signatured_type_index_data (types_cu_list, psyms_seen));
27277
27278 sig_data.info.objfile = objfile;
27279 /* It is used only for gdb_index. */
27280 sig_data.info.symtab = nullptr;
27281 sig_data.info.cu_index = 0;
27282 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
27283 debug_names::write_one_signatured_type,
27284 &sig_data);
27285 }
27286
27287 nametable.build ();
27288
27289 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
27290
27291 const offset_type bytes_of_header
27292 = ((dwarf5_is_dwarf64 ? 12 : 4)
27293 + 2 + 2 + 7 * 4
27294 + sizeof (dwarf5_gdb_augmentation));
27295 size_t expected_bytes = 0;
27296 expected_bytes += bytes_of_header;
27297 expected_bytes += cu_list.size ();
27298 expected_bytes += types_cu_list.size ();
27299 expected_bytes += nametable.bytes ();
27300 data_buf header;
27301
27302 if (!dwarf5_is_dwarf64)
27303 {
27304 const uint64_t size64 = expected_bytes - 4;
27305 gdb_assert (size64 < 0xfffffff0);
27306 header.append_uint (4, dwarf5_byte_order, size64);
27307 }
27308 else
27309 {
27310 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
27311 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
27312 }
27313
27314 /* The version number. */
27315 header.append_uint (2, dwarf5_byte_order, 5);
27316
27317 /* Padding. */
27318 header.append_uint (2, dwarf5_byte_order, 0);
27319
27320 /* comp_unit_count - The number of CUs in the CU list. */
27321 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
27322
27323 /* local_type_unit_count - The number of TUs in the local TU
27324 list. */
27325 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
27326
27327 /* foreign_type_unit_count - The number of TUs in the foreign TU
27328 list. */
27329 header.append_uint (4, dwarf5_byte_order, 0);
27330
27331 /* bucket_count - The number of hash buckets in the hash lookup
27332 table. */
27333 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
27334
27335 /* name_count - The number of unique names in the index. */
27336 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
27337
27338 /* abbrev_table_size - The size in bytes of the abbreviations
27339 table. */
27340 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
27341
27342 /* augmentation_string_size - The size in bytes of the augmentation
27343 string. This value is rounded up to a multiple of 4. */
27344 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
27345 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
27346 header.append_data (dwarf5_gdb_augmentation);
27347
27348 gdb_assert (header.size () == bytes_of_header);
27349
27350 header.file_write (out_file);
27351 cu_list.file_write (out_file);
27352 types_cu_list.file_write (out_file);
27353 nametable.file_write (out_file, out_file_str);
27354
27355 return expected_bytes;
27356 }
27357
27358 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
27359 position is at the end of the file. */
27360
27361 static void
27362 assert_file_size (FILE *file, const char *filename, size_t expected_size)
27363 {
27364 const auto file_size = ftell (file);
27365 if (file_size == -1)
27366 error (_("Can't get `%s' size"), filename);
27367 gdb_assert (file_size == expected_size);
27368 }
27369
27370 /* Create an index file for OBJFILE in the directory DIR. */
27371
27372 static void
27373 write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
27374 const char *dir,
27375 dw_index_kind index_kind)
27376 {
27377 struct objfile *objfile = dwarf2_per_objfile->objfile;
27378
27379 if (dwarf2_per_objfile->using_index)
27380 error (_("Cannot use an index to create the index"));
27381
27382 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
27383 error (_("Cannot make an index when the file has multiple .debug_types sections"));
27384
27385 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
27386 return;
27387
27388 struct stat st;
27389 if (stat (objfile_name (objfile), &st) < 0)
27390 perror_with_name (objfile_name (objfile));
27391
27392 std::string filename (std::string (dir) + SLASH_STRING
27393 + lbasename (objfile_name (objfile))
27394 + (index_kind == dw_index_kind::DEBUG_NAMES
27395 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
27396
27397 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
27398 if (!out_file)
27399 error (_("Can't open `%s' for writing"), filename.c_str ());
27400
27401 /* Order matters here; we want FILE to be closed before FILENAME is
27402 unlinked, because on MS-Windows one cannot delete a file that is
27403 still open. (Don't call anything here that might throw until
27404 file_closer is created.) */
27405 gdb::unlinker unlink_file (filename.c_str ());
27406 gdb_file_up close_out_file (out_file);
27407
27408 if (index_kind == dw_index_kind::DEBUG_NAMES)
27409 {
27410 std::string filename_str (std::string (dir) + SLASH_STRING
27411 + lbasename (objfile_name (objfile))
27412 + DEBUG_STR_SUFFIX);
27413 FILE *out_file_str
27414 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
27415 if (!out_file_str)
27416 error (_("Can't open `%s' for writing"), filename_str.c_str ());
27417 gdb::unlinker unlink_file_str (filename_str.c_str ());
27418 gdb_file_up close_out_file_str (out_file_str);
27419
27420 const size_t total_len
27421 = write_debug_names (dwarf2_per_objfile, out_file, out_file_str);
27422 assert_file_size (out_file, filename.c_str (), total_len);
27423
27424 /* We want to keep the file .debug_str file too. */
27425 unlink_file_str.keep ();
27426 }
27427 else
27428 {
27429 const size_t total_len
27430 = write_gdbindex (dwarf2_per_objfile, out_file);
27431 assert_file_size (out_file, filename.c_str (), total_len);
27432 }
27433
27434 /* We want to keep the file. */
27435 unlink_file.keep ();
27436 }
27437
27438 /* Implementation of the `save gdb-index' command.
27439
27440 Note that the .gdb_index file format used by this command is
27441 documented in the GDB manual. Any changes here must be documented
27442 there. */
27443
27444 static void
27445 save_gdb_index_command (const char *arg, int from_tty)
27446 {
27447 struct objfile *objfile;
27448 const char dwarf5space[] = "-dwarf-5 ";
27449 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
27450
27451 if (!arg)
27452 arg = "";
27453
27454 arg = skip_spaces (arg);
27455 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
27456 {
27457 index_kind = dw_index_kind::DEBUG_NAMES;
27458 arg += strlen (dwarf5space);
27459 arg = skip_spaces (arg);
27460 }
27461
27462 if (!*arg)
27463 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
27464
27465 ALL_OBJFILES (objfile)
27466 {
27467 struct stat st;
27468
27469 /* If the objfile does not correspond to an actual file, skip it. */
27470 if (stat (objfile_name (objfile), &st) < 0)
27471 continue;
27472
27473 struct dwarf2_per_objfile *dwarf2_per_objfile
27474 = get_dwarf2_per_objfile (objfile);
27475
27476 if (dwarf2_per_objfile != NULL)
27477 {
27478 TRY
27479 {
27480 write_psymtabs_to_index (dwarf2_per_objfile, arg, index_kind);
27481 }
27482 CATCH (except, RETURN_MASK_ERROR)
27483 {
27484 exception_fprintf (gdb_stderr, except,
27485 _("Error while writing index for `%s': "),
27486 objfile_name (objfile));
27487 }
27488 END_CATCH
27489 }
27490
27491 }
27492 }
27493
27494 \f
27495
27496 int dwarf_always_disassemble;
27497
27498 static void
27499 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
27500 struct cmd_list_element *c, const char *value)
27501 {
27502 fprintf_filtered (file,
27503 _("Whether to always disassemble "
27504 "DWARF expressions is %s.\n"),
27505 value);
27506 }
27507
27508 static void
27509 show_check_physname (struct ui_file *file, int from_tty,
27510 struct cmd_list_element *c, const char *value)
27511 {
27512 fprintf_filtered (file,
27513 _("Whether to check \"physname\" is %s.\n"),
27514 value);
27515 }
27516
27517 void
27518 _initialize_dwarf2_read (void)
27519 {
27520 struct cmd_list_element *c;
27521
27522 dwarf2_objfile_data_key = register_objfile_data ();
27523
27524 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
27525 Set DWARF specific variables.\n\
27526 Configure DWARF variables such as the cache size"),
27527 &set_dwarf_cmdlist, "maintenance set dwarf ",
27528 0/*allow-unknown*/, &maintenance_set_cmdlist);
27529
27530 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
27531 Show DWARF specific variables\n\
27532 Show DWARF variables such as the cache size"),
27533 &show_dwarf_cmdlist, "maintenance show dwarf ",
27534 0/*allow-unknown*/, &maintenance_show_cmdlist);
27535
27536 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
27537 &dwarf_max_cache_age, _("\
27538 Set the upper bound on the age of cached DWARF compilation units."), _("\
27539 Show the upper bound on the age of cached DWARF compilation units."), _("\
27540 A higher limit means that cached compilation units will be stored\n\
27541 in memory longer, and more total memory will be used. Zero disables\n\
27542 caching, which can slow down startup."),
27543 NULL,
27544 show_dwarf_max_cache_age,
27545 &set_dwarf_cmdlist,
27546 &show_dwarf_cmdlist);
27547
27548 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
27549 &dwarf_always_disassemble, _("\
27550 Set whether `info address' always disassembles DWARF expressions."), _("\
27551 Show whether `info address' always disassembles DWARF expressions."), _("\
27552 When enabled, DWARF expressions are always printed in an assembly-like\n\
27553 syntax. When disabled, expressions will be printed in a more\n\
27554 conversational style, when possible."),
27555 NULL,
27556 show_dwarf_always_disassemble,
27557 &set_dwarf_cmdlist,
27558 &show_dwarf_cmdlist);
27559
27560 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27561 Set debugging of the DWARF reader."), _("\
27562 Show debugging of the DWARF reader."), _("\
27563 When enabled (non-zero), debugging messages are printed during DWARF\n\
27564 reading and symtab expansion. A value of 1 (one) provides basic\n\
27565 information. A value greater than 1 provides more verbose information."),
27566 NULL,
27567 NULL,
27568 &setdebuglist, &showdebuglist);
27569
27570 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27571 Set debugging of the DWARF DIE reader."), _("\
27572 Show debugging of the DWARF DIE reader."), _("\
27573 When enabled (non-zero), DIEs are dumped after they are read in.\n\
27574 The value is the maximum depth to print."),
27575 NULL,
27576 NULL,
27577 &setdebuglist, &showdebuglist);
27578
27579 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27580 Set debugging of the dwarf line reader."), _("\
27581 Show debugging of the dwarf line reader."), _("\
27582 When enabled (non-zero), line number entries are dumped as they are read in.\n\
27583 A value of 1 (one) provides basic information.\n\
27584 A value greater than 1 provides more verbose information."),
27585 NULL,
27586 NULL,
27587 &setdebuglist, &showdebuglist);
27588
27589 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27590 Set cross-checking of \"physname\" code against demangler."), _("\
27591 Show cross-checking of \"physname\" code against demangler."), _("\
27592 When enabled, GDB's internal \"physname\" code is checked against\n\
27593 the demangler."),
27594 NULL, show_check_physname,
27595 &setdebuglist, &showdebuglist);
27596
27597 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27598 no_class, &use_deprecated_index_sections, _("\
27599 Set whether to use deprecated gdb_index sections."), _("\
27600 Show whether to use deprecated gdb_index sections."), _("\
27601 When enabled, deprecated .gdb_index sections are used anyway.\n\
27602 Normally they are ignored either because of a missing feature or\n\
27603 performance issue.\n\
27604 Warning: This option must be enabled before gdb reads the file."),
27605 NULL,
27606 NULL,
27607 &setlist, &showlist);
27608
27609 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
27610 _("\
27611 Save a gdb-index file.\n\
27612 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27613 \n\
27614 No options create one file with .gdb-index extension for pre-DWARF-5\n\
27615 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27616 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
27617 &save_cmdlist);
27618 set_cmd_completer (c, filename_completer);
27619
27620 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27621 &dwarf2_locexpr_funcs);
27622 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27623 &dwarf2_loclist_funcs);
27624
27625 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27626 &dwarf2_block_frame_base_locexpr_funcs);
27627 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27628 &dwarf2_block_frame_base_loclist_funcs);
27629
27630 #if GDB_SELF_TEST
27631 selftests::register_test ("dw2_expand_symtabs_matching",
27632 selftests::dw2_expand_symtabs_matching::run_test);
27633 #endif
27634 }
This page took 1.036149 seconds and 4 git commands to generate.