Add psymbols for nested types
[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 DEF_VEC_I (offset_type);
166
167 /* Ensure only legit values are used. */
168 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
169 do { \
170 gdb_assert ((unsigned int) (value) <= 1); \
171 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
172 } while (0)
173
174 /* Ensure only legit values are used. */
175 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
176 do { \
177 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
178 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
179 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
180 } while (0)
181
182 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
183 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
184 do { \
185 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
186 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
187 } while (0)
188
189 #if WORDS_BIGENDIAN
190
191 /* Convert VALUE between big- and little-endian. */
192
193 static offset_type
194 byte_swap (offset_type value)
195 {
196 offset_type result;
197
198 result = (value & 0xff) << 24;
199 result |= (value & 0xff00) << 8;
200 result |= (value & 0xff0000) >> 8;
201 result |= (value & 0xff000000) >> 24;
202 return result;
203 }
204
205 #define MAYBE_SWAP(V) byte_swap (V)
206
207 #else
208 #define MAYBE_SWAP(V) static_cast<offset_type> (V)
209 #endif /* WORDS_BIGENDIAN */
210
211 /* An index into a (C++) symbol name component in a symbol name as
212 recorded in the mapped_index's symbol table. For each C++ symbol
213 in the symbol table, we record one entry for the start of each
214 component in the symbol in a table of name components, and then
215 sort the table, in order to be able to binary search symbol names,
216 ignoring leading namespaces, both completion and regular look up.
217 For example, for symbol "A::B::C", we'll have an entry that points
218 to "A::B::C", another that points to "B::C", and another for "C".
219 Note that function symbols in GDB index have no parameter
220 information, just the function/method names. You can convert a
221 name_component to a "const char *" using the
222 'mapped_index::symbol_name_at(offset_type)' method. */
223
224 struct name_component
225 {
226 /* Offset in the symbol name where the component starts. Stored as
227 a (32-bit) offset instead of a pointer to save memory and improve
228 locality on 64-bit architectures. */
229 offset_type name_offset;
230
231 /* The symbol's index in the symbol and constant pool tables of a
232 mapped_index. */
233 offset_type idx;
234 };
235
236 /* Base class containing bits shared by both .gdb_index and
237 .debug_name indexes. */
238
239 struct mapped_index_base
240 {
241 /* The name_component table (a sorted vector). See name_component's
242 description above. */
243 std::vector<name_component> name_components;
244
245 /* How NAME_COMPONENTS is sorted. */
246 enum case_sensitivity name_components_casing;
247
248 /* Return the number of names in the symbol table. */
249 virtual size_t symbol_name_count () const = 0;
250
251 /* Get the name of the symbol at IDX in the symbol table. */
252 virtual const char *symbol_name_at (offset_type idx) const = 0;
253
254 /* Return whether the name at IDX in the symbol table should be
255 ignored. */
256 virtual bool symbol_name_slot_invalid (offset_type idx) const
257 {
258 return false;
259 }
260
261 /* Build the symbol name component sorted vector, if we haven't
262 yet. */
263 void build_name_components ();
264
265 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
266 possible matches for LN_NO_PARAMS in the name component
267 vector. */
268 std::pair<std::vector<name_component>::const_iterator,
269 std::vector<name_component>::const_iterator>
270 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
271
272 /* Prevent deleting/destroying via a base class pointer. */
273 protected:
274 ~mapped_index_base() = default;
275 };
276
277 /* A description of the mapped index. The file format is described in
278 a comment by the code that writes the index. */
279 struct mapped_index final : public mapped_index_base
280 {
281 /* A slot/bucket in the symbol table hash. */
282 struct symbol_table_slot
283 {
284 const offset_type name;
285 const offset_type vec;
286 };
287
288 /* Index data format version. */
289 int version;
290
291 /* The total length of the buffer. */
292 off_t total_size;
293
294 /* The address table data. */
295 gdb::array_view<const gdb_byte> address_table;
296
297 /* The symbol table, implemented as a hash table. */
298 gdb::array_view<symbol_table_slot> symbol_table;
299
300 /* A pointer to the constant pool. */
301 const char *constant_pool;
302
303 bool symbol_name_slot_invalid (offset_type idx) const override
304 {
305 const auto &bucket = this->symbol_table[idx];
306 return bucket.name == 0 && bucket.vec;
307 }
308
309 /* Convenience method to get at the name of the symbol at IDX in the
310 symbol table. */
311 const char *symbol_name_at (offset_type idx) const override
312 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
313
314 size_t symbol_name_count () const override
315 { return this->symbol_table.size (); }
316 };
317
318 /* A description of the mapped .debug_names.
319 Uninitialized map has CU_COUNT 0. */
320 struct mapped_debug_names final : public mapped_index_base
321 {
322 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
323 : dwarf2_per_objfile (dwarf2_per_objfile_)
324 {}
325
326 struct dwarf2_per_objfile *dwarf2_per_objfile;
327 bfd_endian dwarf5_byte_order;
328 bool dwarf5_is_dwarf64;
329 bool augmentation_is_gdb;
330 uint8_t offset_size;
331 uint32_t cu_count = 0;
332 uint32_t tu_count, bucket_count, name_count;
333 const gdb_byte *cu_table_reordered, *tu_table_reordered;
334 const uint32_t *bucket_table_reordered, *hash_table_reordered;
335 const gdb_byte *name_table_string_offs_reordered;
336 const gdb_byte *name_table_entry_offs_reordered;
337 const gdb_byte *entry_pool;
338
339 struct index_val
340 {
341 ULONGEST dwarf_tag;
342 struct attr
343 {
344 /* Attribute name DW_IDX_*. */
345 ULONGEST dw_idx;
346
347 /* Attribute form DW_FORM_*. */
348 ULONGEST form;
349
350 /* Value if FORM is DW_FORM_implicit_const. */
351 LONGEST implicit_const;
352 };
353 std::vector<attr> attr_vec;
354 };
355
356 std::unordered_map<ULONGEST, index_val> abbrev_map;
357
358 const char *namei_to_name (uint32_t namei) const;
359
360 /* Implementation of the mapped_index_base virtual interface, for
361 the name_components cache. */
362
363 const char *symbol_name_at (offset_type idx) const override
364 { return namei_to_name (idx); }
365
366 size_t symbol_name_count () const override
367 { return this->name_count; }
368 };
369
370 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
371 DEF_VEC_P (dwarf2_per_cu_ptr);
372
373 struct tu_stats
374 {
375 int nr_uniq_abbrev_tables;
376 int nr_symtabs;
377 int nr_symtab_sharers;
378 int nr_stmt_less_type_units;
379 int nr_all_type_units_reallocs;
380 };
381
382 /* Collection of data recorded per objfile.
383 This hangs off of dwarf2_objfile_data_key. */
384
385 struct dwarf2_per_objfile : public allocate_on_obstack
386 {
387 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
388 dwarf2 section names, or is NULL if the standard ELF names are
389 used. */
390 dwarf2_per_objfile (struct objfile *objfile,
391 const dwarf2_debug_sections *names);
392
393 ~dwarf2_per_objfile ();
394
395 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
396
397 /* Free all cached compilation units. */
398 void free_cached_comp_units ();
399 private:
400 /* This function is mapped across the sections and remembers the
401 offset and size of each of the debugging sections we are
402 interested in. */
403 void locate_sections (bfd *abfd, asection *sectp,
404 const dwarf2_debug_sections &names);
405
406 public:
407 dwarf2_section_info info {};
408 dwarf2_section_info abbrev {};
409 dwarf2_section_info line {};
410 dwarf2_section_info loc {};
411 dwarf2_section_info loclists {};
412 dwarf2_section_info macinfo {};
413 dwarf2_section_info macro {};
414 dwarf2_section_info str {};
415 dwarf2_section_info line_str {};
416 dwarf2_section_info ranges {};
417 dwarf2_section_info rnglists {};
418 dwarf2_section_info addr {};
419 dwarf2_section_info frame {};
420 dwarf2_section_info eh_frame {};
421 dwarf2_section_info gdb_index {};
422 dwarf2_section_info debug_names {};
423 dwarf2_section_info debug_aranges {};
424
425 VEC (dwarf2_section_info_def) *types = NULL;
426
427 /* Back link. */
428 struct objfile *objfile = NULL;
429
430 /* Table of all the compilation units. This is used to locate
431 the target compilation unit of a particular reference. */
432 struct dwarf2_per_cu_data **all_comp_units = NULL;
433
434 /* The number of compilation units in ALL_COMP_UNITS. */
435 int n_comp_units = 0;
436
437 /* The number of .debug_types-related CUs. */
438 int n_type_units = 0;
439
440 /* The number of elements allocated in all_type_units.
441 If there are skeleton-less TUs, we add them to all_type_units lazily. */
442 int n_allocated_type_units = 0;
443
444 /* The .debug_types-related CUs (TUs).
445 This is stored in malloc space because we may realloc it. */
446 struct signatured_type **all_type_units = NULL;
447
448 /* Table of struct type_unit_group objects.
449 The hash key is the DW_AT_stmt_list value. */
450 htab_t type_unit_groups {};
451
452 /* A table mapping .debug_types signatures to its signatured_type entry.
453 This is NULL if the .debug_types section hasn't been read in yet. */
454 htab_t signatured_types {};
455
456 /* Type unit statistics, to see how well the scaling improvements
457 are doing. */
458 struct tu_stats tu_stats {};
459
460 /* A chain of compilation units that are currently read in, so that
461 they can be freed later. */
462 dwarf2_per_cu_data *read_in_chain = NULL;
463
464 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
465 This is NULL if the table hasn't been allocated yet. */
466 htab_t dwo_files {};
467
468 /* True if we've checked for whether there is a DWP file. */
469 bool dwp_checked = false;
470
471 /* The DWP file if there is one, or NULL. */
472 struct dwp_file *dwp_file = NULL;
473
474 /* The shared '.dwz' file, if one exists. This is used when the
475 original data was compressed using 'dwz -m'. */
476 struct dwz_file *dwz_file = NULL;
477
478 /* A flag indicating whether this objfile has a section loaded at a
479 VMA of 0. */
480 bool has_section_at_zero = false;
481
482 /* True if we are using the mapped index,
483 or we are faking it for OBJF_READNOW's sake. */
484 bool using_index = false;
485
486 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
487 mapped_index *index_table = NULL;
488
489 /* The mapped index, or NULL if .debug_names is missing or not being used. */
490 std::unique_ptr<mapped_debug_names> debug_names_table;
491
492 /* When using index_table, this keeps track of all quick_file_names entries.
493 TUs typically share line table entries with a CU, so we maintain a
494 separate table of all line table entries to support the sharing.
495 Note that while there can be way more TUs than CUs, we've already
496 sorted all the TUs into "type unit groups", grouped by their
497 DW_AT_stmt_list value. Therefore the only sharing done here is with a
498 CU and its associated TU group if there is one. */
499 htab_t quick_file_names_table {};
500
501 /* Set during partial symbol reading, to prevent queueing of full
502 symbols. */
503 bool reading_partial_symbols = false;
504
505 /* Table mapping type DIEs to their struct type *.
506 This is NULL if not allocated yet.
507 The mapping is done via (CU/TU + DIE offset) -> type. */
508 htab_t die_type_hash {};
509
510 /* The CUs we recently read. */
511 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
512
513 /* Table containing line_header indexed by offset and offset_in_dwz. */
514 htab_t line_header_hash {};
515
516 /* Table containing all filenames. This is an optional because the
517 table is lazily constructed on first access. */
518 gdb::optional<filename_seen_cache> filenames_cache;
519 };
520
521 /* Get the dwarf2_per_objfile associated to OBJFILE. */
522
523 struct dwarf2_per_objfile *
524 get_dwarf2_per_objfile (struct objfile *objfile)
525 {
526 return ((struct dwarf2_per_objfile *)
527 objfile_data (objfile, dwarf2_objfile_data_key));
528 }
529
530 /* Set the dwarf2_per_objfile associated to OBJFILE. */
531
532 void
533 set_dwarf2_per_objfile (struct objfile *objfile,
534 struct dwarf2_per_objfile *dwarf2_per_objfile)
535 {
536 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
537 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
538 }
539
540 /* Default names of the debugging sections. */
541
542 /* Note that if the debugging section has been compressed, it might
543 have a name like .zdebug_info. */
544
545 static const struct dwarf2_debug_sections dwarf2_elf_names =
546 {
547 { ".debug_info", ".zdebug_info" },
548 { ".debug_abbrev", ".zdebug_abbrev" },
549 { ".debug_line", ".zdebug_line" },
550 { ".debug_loc", ".zdebug_loc" },
551 { ".debug_loclists", ".zdebug_loclists" },
552 { ".debug_macinfo", ".zdebug_macinfo" },
553 { ".debug_macro", ".zdebug_macro" },
554 { ".debug_str", ".zdebug_str" },
555 { ".debug_line_str", ".zdebug_line_str" },
556 { ".debug_ranges", ".zdebug_ranges" },
557 { ".debug_rnglists", ".zdebug_rnglists" },
558 { ".debug_types", ".zdebug_types" },
559 { ".debug_addr", ".zdebug_addr" },
560 { ".debug_frame", ".zdebug_frame" },
561 { ".eh_frame", NULL },
562 { ".gdb_index", ".zgdb_index" },
563 { ".debug_names", ".zdebug_names" },
564 { ".debug_aranges", ".zdebug_aranges" },
565 23
566 };
567
568 /* List of DWO/DWP sections. */
569
570 static const struct dwop_section_names
571 {
572 struct dwarf2_section_names abbrev_dwo;
573 struct dwarf2_section_names info_dwo;
574 struct dwarf2_section_names line_dwo;
575 struct dwarf2_section_names loc_dwo;
576 struct dwarf2_section_names loclists_dwo;
577 struct dwarf2_section_names macinfo_dwo;
578 struct dwarf2_section_names macro_dwo;
579 struct dwarf2_section_names str_dwo;
580 struct dwarf2_section_names str_offsets_dwo;
581 struct dwarf2_section_names types_dwo;
582 struct dwarf2_section_names cu_index;
583 struct dwarf2_section_names tu_index;
584 }
585 dwop_section_names =
586 {
587 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
588 { ".debug_info.dwo", ".zdebug_info.dwo" },
589 { ".debug_line.dwo", ".zdebug_line.dwo" },
590 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
591 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
592 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
593 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
594 { ".debug_str.dwo", ".zdebug_str.dwo" },
595 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
596 { ".debug_types.dwo", ".zdebug_types.dwo" },
597 { ".debug_cu_index", ".zdebug_cu_index" },
598 { ".debug_tu_index", ".zdebug_tu_index" },
599 };
600
601 /* local data types */
602
603 /* The data in a compilation unit header, after target2host
604 translation, looks like this. */
605 struct comp_unit_head
606 {
607 unsigned int length;
608 short version;
609 unsigned char addr_size;
610 unsigned char signed_addr_p;
611 sect_offset abbrev_sect_off;
612
613 /* Size of file offsets; either 4 or 8. */
614 unsigned int offset_size;
615
616 /* Size of the length field; either 4 or 12. */
617 unsigned int initial_length_size;
618
619 enum dwarf_unit_type unit_type;
620
621 /* Offset to the first byte of this compilation unit header in the
622 .debug_info section, for resolving relative reference dies. */
623 sect_offset sect_off;
624
625 /* Offset to first die in this cu from the start of the cu.
626 This will be the first byte following the compilation unit header. */
627 cu_offset first_die_cu_offset;
628
629 /* 64-bit signature of this type unit - it is valid only for
630 UNIT_TYPE DW_UT_type. */
631 ULONGEST signature;
632
633 /* For types, offset in the type's DIE of the type defined by this TU. */
634 cu_offset type_cu_offset_in_tu;
635 };
636
637 /* Type used for delaying computation of method physnames.
638 See comments for compute_delayed_physnames. */
639 struct delayed_method_info
640 {
641 /* The type to which the method is attached, i.e., its parent class. */
642 struct type *type;
643
644 /* The index of the method in the type's function fieldlists. */
645 int fnfield_index;
646
647 /* The index of the method in the fieldlist. */
648 int index;
649
650 /* The name of the DIE. */
651 const char *name;
652
653 /* The DIE associated with this method. */
654 struct die_info *die;
655 };
656
657 /* Internal state when decoding a particular compilation unit. */
658 struct dwarf2_cu
659 {
660 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
661 ~dwarf2_cu ();
662
663 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
664
665 /* The header of the compilation unit. */
666 struct comp_unit_head header {};
667
668 /* Base address of this compilation unit. */
669 CORE_ADDR base_address = 0;
670
671 /* Non-zero if base_address has been set. */
672 int base_known = 0;
673
674 /* The language we are debugging. */
675 enum language language = language_unknown;
676 const struct language_defn *language_defn = nullptr;
677
678 const char *producer = nullptr;
679
680 /* The generic symbol table building routines have separate lists for
681 file scope symbols and all all other scopes (local scopes). So
682 we need to select the right one to pass to add_symbol_to_list().
683 We do it by keeping a pointer to the correct list in list_in_scope.
684
685 FIXME: The original dwarf code just treated the file scope as the
686 first local scope, and all other local scopes as nested local
687 scopes, and worked fine. Check to see if we really need to
688 distinguish these in buildsym.c. */
689 struct pending **list_in_scope = nullptr;
690
691 /* Hash table holding all the loaded partial DIEs
692 with partial_die->offset.SECT_OFF as hash. */
693 htab_t partial_dies = nullptr;
694
695 /* Storage for things with the same lifetime as this read-in compilation
696 unit, including partial DIEs. */
697 auto_obstack comp_unit_obstack;
698
699 /* When multiple dwarf2_cu structures are living in memory, this field
700 chains them all together, so that they can be released efficiently.
701 We will probably also want a generation counter so that most-recently-used
702 compilation units are cached... */
703 struct dwarf2_per_cu_data *read_in_chain = nullptr;
704
705 /* Backlink to our per_cu entry. */
706 struct dwarf2_per_cu_data *per_cu;
707
708 /* How many compilation units ago was this CU last referenced? */
709 int last_used = 0;
710
711 /* A hash table of DIE cu_offset for following references with
712 die_info->offset.sect_off as hash. */
713 htab_t die_hash = nullptr;
714
715 /* Full DIEs if read in. */
716 struct die_info *dies = nullptr;
717
718 /* A set of pointers to dwarf2_per_cu_data objects for compilation
719 units referenced by this one. Only set during full symbol processing;
720 partial symbol tables do not have dependencies. */
721 htab_t dependencies = nullptr;
722
723 /* Header data from the line table, during full symbol processing. */
724 struct line_header *line_header = nullptr;
725 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
726 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
727 this is the DW_TAG_compile_unit die for this CU. We'll hold on
728 to the line header as long as this DIE is being processed. See
729 process_die_scope. */
730 die_info *line_header_die_owner = nullptr;
731
732 /* A list of methods which need to have physnames computed
733 after all type information has been read. */
734 std::vector<delayed_method_info> method_list;
735
736 /* To be copied to symtab->call_site_htab. */
737 htab_t call_site_htab = nullptr;
738
739 /* Non-NULL if this CU came from a DWO file.
740 There is an invariant here that is important to remember:
741 Except for attributes copied from the top level DIE in the "main"
742 (or "stub") file in preparation for reading the DWO file
743 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
744 Either there isn't a DWO file (in which case this is NULL and the point
745 is moot), or there is and either we're not going to read it (in which
746 case this is NULL) or there is and we are reading it (in which case this
747 is non-NULL). */
748 struct dwo_unit *dwo_unit = nullptr;
749
750 /* The DW_AT_addr_base attribute if present, zero otherwise
751 (zero is a valid value though).
752 Note this value comes from the Fission stub CU/TU's DIE. */
753 ULONGEST addr_base = 0;
754
755 /* The DW_AT_ranges_base attribute if present, zero otherwise
756 (zero is a valid value though).
757 Note this value comes from the Fission stub CU/TU's DIE.
758 Also note that the value is zero in the non-DWO case so this value can
759 be used without needing to know whether DWO files are in use or not.
760 N.B. This does not apply to DW_AT_ranges appearing in
761 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
762 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
763 DW_AT_ranges_base *would* have to be applied, and we'd have to care
764 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
765 ULONGEST ranges_base = 0;
766
767 /* When reading debug info generated by older versions of rustc, we
768 have to rewrite some union types to be struct types with a
769 variant part. This rewriting must be done after the CU is fully
770 read in, because otherwise at the point of rewriting some struct
771 type might not have been fully processed. So, we keep a list of
772 all such types here and process them after expansion. */
773 std::vector<struct type *> rust_unions;
774
775 /* Mark used when releasing cached dies. */
776 unsigned int mark : 1;
777
778 /* This CU references .debug_loc. See the symtab->locations_valid field.
779 This test is imperfect as there may exist optimized debug code not using
780 any location list and still facing inlining issues if handled as
781 unoptimized code. For a future better test see GCC PR other/32998. */
782 unsigned int has_loclist : 1;
783
784 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
785 if all the producer_is_* fields are valid. This information is cached
786 because profiling CU expansion showed excessive time spent in
787 producer_is_gxx_lt_4_6. */
788 unsigned int checked_producer : 1;
789 unsigned int producer_is_gxx_lt_4_6 : 1;
790 unsigned int producer_is_gcc_lt_4_3 : 1;
791 unsigned int producer_is_icc_lt_14 : 1;
792
793 /* When set, the file that we're processing is known to have
794 debugging info for C++ namespaces. GCC 3.3.x did not produce
795 this information, but later versions do. */
796
797 unsigned int processing_has_namespace_info : 1;
798
799 struct partial_die_info *find_partial_die (sect_offset sect_off);
800 };
801
802 /* Persistent data held for a compilation unit, even when not
803 processing it. We put a pointer to this structure in the
804 read_symtab_private field of the psymtab. */
805
806 struct dwarf2_per_cu_data
807 {
808 /* The start offset and length of this compilation unit.
809 NOTE: Unlike comp_unit_head.length, this length includes
810 initial_length_size.
811 If the DIE refers to a DWO file, this is always of the original die,
812 not the DWO file. */
813 sect_offset sect_off;
814 unsigned int length;
815
816 /* DWARF standard version this data has been read from (such as 4 or 5). */
817 short dwarf_version;
818
819 /* Flag indicating this compilation unit will be read in before
820 any of the current compilation units are processed. */
821 unsigned int queued : 1;
822
823 /* This flag will be set when reading partial DIEs if we need to load
824 absolutely all DIEs for this compilation unit, instead of just the ones
825 we think are interesting. It gets set if we look for a DIE in the
826 hash table and don't find it. */
827 unsigned int load_all_dies : 1;
828
829 /* Non-zero if this CU is from .debug_types.
830 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
831 this is non-zero. */
832 unsigned int is_debug_types : 1;
833
834 /* Non-zero if this CU is from the .dwz file. */
835 unsigned int is_dwz : 1;
836
837 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
838 This flag is only valid if is_debug_types is true.
839 We can't read a CU directly from a DWO file: There are required
840 attributes in the stub. */
841 unsigned int reading_dwo_directly : 1;
842
843 /* Non-zero if the TU has been read.
844 This is used to assist the "Stay in DWO Optimization" for Fission:
845 When reading a DWO, it's faster to read TUs from the DWO instead of
846 fetching them from random other DWOs (due to comdat folding).
847 If the TU has already been read, the optimization is unnecessary
848 (and unwise - we don't want to change where gdb thinks the TU lives
849 "midflight").
850 This flag is only valid if is_debug_types is true. */
851 unsigned int tu_read : 1;
852
853 /* The section this CU/TU lives in.
854 If the DIE refers to a DWO file, this is always the original die,
855 not the DWO file. */
856 struct dwarf2_section_info *section;
857
858 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
859 of the CU cache it gets reset to NULL again. This is left as NULL for
860 dummy CUs (a CU header, but nothing else). */
861 struct dwarf2_cu *cu;
862
863 /* The corresponding dwarf2_per_objfile. */
864 struct dwarf2_per_objfile *dwarf2_per_objfile;
865
866 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
867 is active. Otherwise, the 'psymtab' field is active. */
868 union
869 {
870 /* The partial symbol table associated with this compilation unit,
871 or NULL for unread partial units. */
872 struct partial_symtab *psymtab;
873
874 /* Data needed by the "quick" functions. */
875 struct dwarf2_per_cu_quick_data *quick;
876 } v;
877
878 /* The CUs we import using DW_TAG_imported_unit. This is filled in
879 while reading psymtabs, used to compute the psymtab dependencies,
880 and then cleared. Then it is filled in again while reading full
881 symbols, and only deleted when the objfile is destroyed.
882
883 This is also used to work around a difference between the way gold
884 generates .gdb_index version <=7 and the way gdb does. Arguably this
885 is a gold bug. For symbols coming from TUs, gold records in the index
886 the CU that includes the TU instead of the TU itself. This breaks
887 dw2_lookup_symbol: It assumes that if the index says symbol X lives
888 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
889 will find X. Alas TUs live in their own symtab, so after expanding CU Y
890 we need to look in TU Z to find X. Fortunately, this is akin to
891 DW_TAG_imported_unit, so we just use the same mechanism: For
892 .gdb_index version <=7 this also records the TUs that the CU referred
893 to. Concurrently with this change gdb was modified to emit version 8
894 indices so we only pay a price for gold generated indices.
895 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
896 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
897 };
898
899 /* Entry in the signatured_types hash table. */
900
901 struct signatured_type
902 {
903 /* The "per_cu" object of this type.
904 This struct is used iff per_cu.is_debug_types.
905 N.B.: This is the first member so that it's easy to convert pointers
906 between them. */
907 struct dwarf2_per_cu_data per_cu;
908
909 /* The type's signature. */
910 ULONGEST signature;
911
912 /* Offset in the TU of the type's DIE, as read from the TU header.
913 If this TU is a DWO stub and the definition lives in a DWO file
914 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
915 cu_offset type_offset_in_tu;
916
917 /* Offset in the section of the type's DIE.
918 If the definition lives in a DWO file, this is the offset in the
919 .debug_types.dwo section.
920 The value is zero until the actual value is known.
921 Zero is otherwise not a valid section offset. */
922 sect_offset type_offset_in_section;
923
924 /* Type units are grouped by their DW_AT_stmt_list entry so that they
925 can share them. This points to the containing symtab. */
926 struct type_unit_group *type_unit_group;
927
928 /* The type.
929 The first time we encounter this type we fully read it in and install it
930 in the symbol tables. Subsequent times we only need the type. */
931 struct type *type;
932
933 /* Containing DWO unit.
934 This field is valid iff per_cu.reading_dwo_directly. */
935 struct dwo_unit *dwo_unit;
936 };
937
938 typedef struct signatured_type *sig_type_ptr;
939 DEF_VEC_P (sig_type_ptr);
940
941 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
942 This includes type_unit_group and quick_file_names. */
943
944 struct stmt_list_hash
945 {
946 /* The DWO unit this table is from or NULL if there is none. */
947 struct dwo_unit *dwo_unit;
948
949 /* Offset in .debug_line or .debug_line.dwo. */
950 sect_offset line_sect_off;
951 };
952
953 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
954 an object of this type. */
955
956 struct type_unit_group
957 {
958 /* dwarf2read.c's main "handle" on a TU symtab.
959 To simplify things we create an artificial CU that "includes" all the
960 type units using this stmt_list so that the rest of the code still has
961 a "per_cu" handle on the symtab.
962 This PER_CU is recognized by having no section. */
963 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
964 struct dwarf2_per_cu_data per_cu;
965
966 /* The TUs that share this DW_AT_stmt_list entry.
967 This is added to while parsing type units to build partial symtabs,
968 and is deleted afterwards and not used again. */
969 VEC (sig_type_ptr) *tus;
970
971 /* The compunit symtab.
972 Type units in a group needn't all be defined in the same source file,
973 so we create an essentially anonymous symtab as the compunit symtab. */
974 struct compunit_symtab *compunit_symtab;
975
976 /* The data used to construct the hash key. */
977 struct stmt_list_hash hash;
978
979 /* The number of symtabs from the line header.
980 The value here must match line_header.num_file_names. */
981 unsigned int num_symtabs;
982
983 /* The symbol tables for this TU (obtained from the files listed in
984 DW_AT_stmt_list).
985 WARNING: The order of entries here must match the order of entries
986 in the line header. After the first TU using this type_unit_group, the
987 line header for the subsequent TUs is recreated from this. This is done
988 because we need to use the same symtabs for each TU using the same
989 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
990 there's no guarantee the line header doesn't have duplicate entries. */
991 struct symtab **symtabs;
992 };
993
994 /* These sections are what may appear in a (real or virtual) DWO file. */
995
996 struct dwo_sections
997 {
998 struct dwarf2_section_info abbrev;
999 struct dwarf2_section_info line;
1000 struct dwarf2_section_info loc;
1001 struct dwarf2_section_info loclists;
1002 struct dwarf2_section_info macinfo;
1003 struct dwarf2_section_info macro;
1004 struct dwarf2_section_info str;
1005 struct dwarf2_section_info str_offsets;
1006 /* In the case of a virtual DWO file, these two are unused. */
1007 struct dwarf2_section_info info;
1008 VEC (dwarf2_section_info_def) *types;
1009 };
1010
1011 /* CUs/TUs in DWP/DWO files. */
1012
1013 struct dwo_unit
1014 {
1015 /* Backlink to the containing struct dwo_file. */
1016 struct dwo_file *dwo_file;
1017
1018 /* The "id" that distinguishes this CU/TU.
1019 .debug_info calls this "dwo_id", .debug_types calls this "signature".
1020 Since signatures came first, we stick with it for consistency. */
1021 ULONGEST signature;
1022
1023 /* The section this CU/TU lives in, in the DWO file. */
1024 struct dwarf2_section_info *section;
1025
1026 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
1027 sect_offset sect_off;
1028 unsigned int length;
1029
1030 /* For types, offset in the type's DIE of the type defined by this TU. */
1031 cu_offset type_offset_in_tu;
1032 };
1033
1034 /* include/dwarf2.h defines the DWP section codes.
1035 It defines a max value but it doesn't define a min value, which we
1036 use for error checking, so provide one. */
1037
1038 enum dwp_v2_section_ids
1039 {
1040 DW_SECT_MIN = 1
1041 };
1042
1043 /* Data for one DWO file.
1044
1045 This includes virtual DWO files (a virtual DWO file is a DWO file as it
1046 appears in a DWP file). DWP files don't really have DWO files per se -
1047 comdat folding of types "loses" the DWO file they came from, and from
1048 a high level view DWP files appear to contain a mass of random types.
1049 However, to maintain consistency with the non-DWP case we pretend DWP
1050 files contain virtual DWO files, and we assign each TU with one virtual
1051 DWO file (generally based on the line and abbrev section offsets -
1052 a heuristic that seems to work in practice). */
1053
1054 struct dwo_file
1055 {
1056 /* The DW_AT_GNU_dwo_name attribute.
1057 For virtual DWO files the name is constructed from the section offsets
1058 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
1059 from related CU+TUs. */
1060 const char *dwo_name;
1061
1062 /* The DW_AT_comp_dir attribute. */
1063 const char *comp_dir;
1064
1065 /* The bfd, when the file is open. Otherwise this is NULL.
1066 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
1067 bfd *dbfd;
1068
1069 /* The sections that make up this DWO file.
1070 Remember that for virtual DWO files in DWP V2, these are virtual
1071 sections (for lack of a better name). */
1072 struct dwo_sections sections;
1073
1074 /* The CUs in the file.
1075 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
1076 an extension to handle LLVM's Link Time Optimization output (where
1077 multiple source files may be compiled into a single object/dwo pair). */
1078 htab_t cus;
1079
1080 /* Table of TUs in the file.
1081 Each element is a struct dwo_unit. */
1082 htab_t tus;
1083 };
1084
1085 /* These sections are what may appear in a DWP file. */
1086
1087 struct dwp_sections
1088 {
1089 /* These are used by both DWP version 1 and 2. */
1090 struct dwarf2_section_info str;
1091 struct dwarf2_section_info cu_index;
1092 struct dwarf2_section_info tu_index;
1093
1094 /* These are only used by DWP version 2 files.
1095 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
1096 sections are referenced by section number, and are not recorded here.
1097 In DWP version 2 there is at most one copy of all these sections, each
1098 section being (effectively) comprised of the concatenation of all of the
1099 individual sections that exist in the version 1 format.
1100 To keep the code simple we treat each of these concatenated pieces as a
1101 section itself (a virtual section?). */
1102 struct dwarf2_section_info abbrev;
1103 struct dwarf2_section_info info;
1104 struct dwarf2_section_info line;
1105 struct dwarf2_section_info loc;
1106 struct dwarf2_section_info macinfo;
1107 struct dwarf2_section_info macro;
1108 struct dwarf2_section_info str_offsets;
1109 struct dwarf2_section_info types;
1110 };
1111
1112 /* These sections are what may appear in a virtual DWO file in DWP version 1.
1113 A virtual DWO file is a DWO file as it appears in a DWP file. */
1114
1115 struct virtual_v1_dwo_sections
1116 {
1117 struct dwarf2_section_info abbrev;
1118 struct dwarf2_section_info line;
1119 struct dwarf2_section_info loc;
1120 struct dwarf2_section_info macinfo;
1121 struct dwarf2_section_info macro;
1122 struct dwarf2_section_info str_offsets;
1123 /* Each DWP hash table entry records one CU or one TU.
1124 That is recorded here, and copied to dwo_unit.section. */
1125 struct dwarf2_section_info info_or_types;
1126 };
1127
1128 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1129 In version 2, the sections of the DWO files are concatenated together
1130 and stored in one section of that name. Thus each ELF section contains
1131 several "virtual" sections. */
1132
1133 struct virtual_v2_dwo_sections
1134 {
1135 bfd_size_type abbrev_offset;
1136 bfd_size_type abbrev_size;
1137
1138 bfd_size_type line_offset;
1139 bfd_size_type line_size;
1140
1141 bfd_size_type loc_offset;
1142 bfd_size_type loc_size;
1143
1144 bfd_size_type macinfo_offset;
1145 bfd_size_type macinfo_size;
1146
1147 bfd_size_type macro_offset;
1148 bfd_size_type macro_size;
1149
1150 bfd_size_type str_offsets_offset;
1151 bfd_size_type str_offsets_size;
1152
1153 /* Each DWP hash table entry records one CU or one TU.
1154 That is recorded here, and copied to dwo_unit.section. */
1155 bfd_size_type info_or_types_offset;
1156 bfd_size_type info_or_types_size;
1157 };
1158
1159 /* Contents of DWP hash tables. */
1160
1161 struct dwp_hash_table
1162 {
1163 uint32_t version, nr_columns;
1164 uint32_t nr_units, nr_slots;
1165 const gdb_byte *hash_table, *unit_table;
1166 union
1167 {
1168 struct
1169 {
1170 const gdb_byte *indices;
1171 } v1;
1172 struct
1173 {
1174 /* This is indexed by column number and gives the id of the section
1175 in that column. */
1176 #define MAX_NR_V2_DWO_SECTIONS \
1177 (1 /* .debug_info or .debug_types */ \
1178 + 1 /* .debug_abbrev */ \
1179 + 1 /* .debug_line */ \
1180 + 1 /* .debug_loc */ \
1181 + 1 /* .debug_str_offsets */ \
1182 + 1 /* .debug_macro or .debug_macinfo */)
1183 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1184 const gdb_byte *offsets;
1185 const gdb_byte *sizes;
1186 } v2;
1187 } section_pool;
1188 };
1189
1190 /* Data for one DWP file. */
1191
1192 struct dwp_file
1193 {
1194 /* Name of the file. */
1195 const char *name;
1196
1197 /* File format version. */
1198 int version;
1199
1200 /* The bfd. */
1201 bfd *dbfd;
1202
1203 /* Section info for this file. */
1204 struct dwp_sections sections;
1205
1206 /* Table of CUs in the file. */
1207 const struct dwp_hash_table *cus;
1208
1209 /* Table of TUs in the file. */
1210 const struct dwp_hash_table *tus;
1211
1212 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1213 htab_t loaded_cus;
1214 htab_t loaded_tus;
1215
1216 /* Table to map ELF section numbers to their sections.
1217 This is only needed for the DWP V1 file format. */
1218 unsigned int num_sections;
1219 asection **elf_sections;
1220 };
1221
1222 /* This represents a '.dwz' file. */
1223
1224 struct dwz_file
1225 {
1226 /* A dwz file can only contain a few sections. */
1227 struct dwarf2_section_info abbrev;
1228 struct dwarf2_section_info info;
1229 struct dwarf2_section_info str;
1230 struct dwarf2_section_info line;
1231 struct dwarf2_section_info macro;
1232 struct dwarf2_section_info gdb_index;
1233 struct dwarf2_section_info debug_names;
1234
1235 /* The dwz's BFD. */
1236 bfd *dwz_bfd;
1237 };
1238
1239 /* Struct used to pass misc. parameters to read_die_and_children, et
1240 al. which are used for both .debug_info and .debug_types dies.
1241 All parameters here are unchanging for the life of the call. This
1242 struct exists to abstract away the constant parameters of die reading. */
1243
1244 struct die_reader_specs
1245 {
1246 /* The bfd of die_section. */
1247 bfd* abfd;
1248
1249 /* The CU of the DIE we are parsing. */
1250 struct dwarf2_cu *cu;
1251
1252 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1253 struct dwo_file *dwo_file;
1254
1255 /* The section the die comes from.
1256 This is either .debug_info or .debug_types, or the .dwo variants. */
1257 struct dwarf2_section_info *die_section;
1258
1259 /* die_section->buffer. */
1260 const gdb_byte *buffer;
1261
1262 /* The end of the buffer. */
1263 const gdb_byte *buffer_end;
1264
1265 /* The value of the DW_AT_comp_dir attribute. */
1266 const char *comp_dir;
1267
1268 /* The abbreviation table to use when reading the DIEs. */
1269 struct abbrev_table *abbrev_table;
1270 };
1271
1272 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1273 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1274 const gdb_byte *info_ptr,
1275 struct die_info *comp_unit_die,
1276 int has_children,
1277 void *data);
1278
1279 /* A 1-based directory index. This is a strong typedef to prevent
1280 accidentally using a directory index as a 0-based index into an
1281 array/vector. */
1282 enum class dir_index : unsigned int {};
1283
1284 /* Likewise, a 1-based file name index. */
1285 enum class file_name_index : unsigned int {};
1286
1287 struct file_entry
1288 {
1289 file_entry () = default;
1290
1291 file_entry (const char *name_, dir_index d_index_,
1292 unsigned int mod_time_, unsigned int length_)
1293 : name (name_),
1294 d_index (d_index_),
1295 mod_time (mod_time_),
1296 length (length_)
1297 {}
1298
1299 /* Return the include directory at D_INDEX stored in LH. Returns
1300 NULL if D_INDEX is out of bounds. */
1301 const char *include_dir (const line_header *lh) const;
1302
1303 /* The file name. Note this is an observing pointer. The memory is
1304 owned by debug_line_buffer. */
1305 const char *name {};
1306
1307 /* The directory index (1-based). */
1308 dir_index d_index {};
1309
1310 unsigned int mod_time {};
1311
1312 unsigned int length {};
1313
1314 /* True if referenced by the Line Number Program. */
1315 bool included_p {};
1316
1317 /* The associated symbol table, if any. */
1318 struct symtab *symtab {};
1319 };
1320
1321 /* The line number information for a compilation unit (found in the
1322 .debug_line section) begins with a "statement program header",
1323 which contains the following information. */
1324 struct line_header
1325 {
1326 line_header ()
1327 : offset_in_dwz {}
1328 {}
1329
1330 /* Add an entry to the include directory table. */
1331 void add_include_dir (const char *include_dir);
1332
1333 /* Add an entry to the file name table. */
1334 void add_file_name (const char *name, dir_index d_index,
1335 unsigned int mod_time, unsigned int length);
1336
1337 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1338 is out of bounds. */
1339 const char *include_dir_at (dir_index index) const
1340 {
1341 /* Convert directory index number (1-based) to vector index
1342 (0-based). */
1343 size_t vec_index = to_underlying (index) - 1;
1344
1345 if (vec_index >= include_dirs.size ())
1346 return NULL;
1347 return include_dirs[vec_index];
1348 }
1349
1350 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1351 is out of bounds. */
1352 file_entry *file_name_at (file_name_index index)
1353 {
1354 /* Convert file name index number (1-based) to vector index
1355 (0-based). */
1356 size_t vec_index = to_underlying (index) - 1;
1357
1358 if (vec_index >= file_names.size ())
1359 return NULL;
1360 return &file_names[vec_index];
1361 }
1362
1363 /* Const version of the above. */
1364 const file_entry *file_name_at (unsigned int index) const
1365 {
1366 if (index >= file_names.size ())
1367 return NULL;
1368 return &file_names[index];
1369 }
1370
1371 /* Offset of line number information in .debug_line section. */
1372 sect_offset sect_off {};
1373
1374 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1375 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1376
1377 unsigned int total_length {};
1378 unsigned short version {};
1379 unsigned int header_length {};
1380 unsigned char minimum_instruction_length {};
1381 unsigned char maximum_ops_per_instruction {};
1382 unsigned char default_is_stmt {};
1383 int line_base {};
1384 unsigned char line_range {};
1385 unsigned char opcode_base {};
1386
1387 /* standard_opcode_lengths[i] is the number of operands for the
1388 standard opcode whose value is i. This means that
1389 standard_opcode_lengths[0] is unused, and the last meaningful
1390 element is standard_opcode_lengths[opcode_base - 1]. */
1391 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1392
1393 /* The include_directories table. Note these are observing
1394 pointers. The memory is owned by debug_line_buffer. */
1395 std::vector<const char *> include_dirs;
1396
1397 /* The file_names table. */
1398 std::vector<file_entry> file_names;
1399
1400 /* The start and end of the statement program following this
1401 header. These point into dwarf2_per_objfile->line_buffer. */
1402 const gdb_byte *statement_program_start {}, *statement_program_end {};
1403 };
1404
1405 typedef std::unique_ptr<line_header> line_header_up;
1406
1407 const char *
1408 file_entry::include_dir (const line_header *lh) const
1409 {
1410 return lh->include_dir_at (d_index);
1411 }
1412
1413 /* When we construct a partial symbol table entry we only
1414 need this much information. */
1415 struct partial_die_info : public allocate_on_obstack
1416 {
1417 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1418
1419 /* Disable assign but still keep copy ctor, which is needed
1420 load_partial_dies. */
1421 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1422
1423 /* Adjust the partial die before generating a symbol for it. This
1424 function may set the is_external flag or change the DIE's
1425 name. */
1426 void fixup (struct dwarf2_cu *cu);
1427
1428 /* Read a minimal amount of information into the minimal die
1429 structure. */
1430 const gdb_byte *read (const struct die_reader_specs *reader,
1431 const struct abbrev_info &abbrev,
1432 const gdb_byte *info_ptr);
1433
1434 /* Offset of this DIE. */
1435 const sect_offset sect_off;
1436
1437 /* DWARF-2 tag for this DIE. */
1438 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1439
1440 /* Assorted flags describing the data found in this DIE. */
1441 const unsigned int has_children : 1;
1442
1443 unsigned int is_external : 1;
1444 unsigned int is_declaration : 1;
1445 unsigned int has_type : 1;
1446 unsigned int has_specification : 1;
1447 unsigned int has_pc_info : 1;
1448 unsigned int may_be_inlined : 1;
1449
1450 /* This DIE has been marked DW_AT_main_subprogram. */
1451 unsigned int main_subprogram : 1;
1452
1453 /* Flag set if the SCOPE field of this structure has been
1454 computed. */
1455 unsigned int scope_set : 1;
1456
1457 /* Flag set if the DIE has a byte_size attribute. */
1458 unsigned int has_byte_size : 1;
1459
1460 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1461 unsigned int has_const_value : 1;
1462
1463 /* Flag set if any of the DIE's children are template arguments. */
1464 unsigned int has_template_arguments : 1;
1465
1466 /* Flag set if fixup has been called on this die. */
1467 unsigned int fixup_called : 1;
1468
1469 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1470 unsigned int is_dwz : 1;
1471
1472 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1473 unsigned int spec_is_dwz : 1;
1474
1475 /* The name of this DIE. Normally the value of DW_AT_name, but
1476 sometimes a default name for unnamed DIEs. */
1477 const char *name = nullptr;
1478
1479 /* The linkage name, if present. */
1480 const char *linkage_name = nullptr;
1481
1482 /* The scope to prepend to our children. This is generally
1483 allocated on the comp_unit_obstack, so will disappear
1484 when this compilation unit leaves the cache. */
1485 const char *scope = nullptr;
1486
1487 /* Some data associated with the partial DIE. The tag determines
1488 which field is live. */
1489 union
1490 {
1491 /* The location description associated with this DIE, if any. */
1492 struct dwarf_block *locdesc;
1493 /* The offset of an import, for DW_TAG_imported_unit. */
1494 sect_offset sect_off;
1495 } d {};
1496
1497 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1498 CORE_ADDR lowpc = 0;
1499 CORE_ADDR highpc = 0;
1500
1501 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1502 DW_AT_sibling, if any. */
1503 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1504 could return DW_AT_sibling values to its caller load_partial_dies. */
1505 const gdb_byte *sibling = nullptr;
1506
1507 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1508 DW_AT_specification (or DW_AT_abstract_origin or
1509 DW_AT_extension). */
1510 sect_offset spec_offset {};
1511
1512 /* Pointers to this DIE's parent, first child, and next sibling,
1513 if any. */
1514 struct partial_die_info *die_parent = nullptr;
1515 struct partial_die_info *die_child = nullptr;
1516 struct partial_die_info *die_sibling = nullptr;
1517
1518 friend struct partial_die_info *
1519 dwarf2_cu::find_partial_die (sect_offset sect_off);
1520
1521 private:
1522 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1523 partial_die_info (sect_offset sect_off)
1524 : partial_die_info (sect_off, DW_TAG_padding, 0)
1525 {
1526 }
1527
1528 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1529 int has_children_)
1530 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1531 {
1532 is_external = 0;
1533 is_declaration = 0;
1534 has_type = 0;
1535 has_specification = 0;
1536 has_pc_info = 0;
1537 may_be_inlined = 0;
1538 main_subprogram = 0;
1539 scope_set = 0;
1540 has_byte_size = 0;
1541 has_const_value = 0;
1542 has_template_arguments = 0;
1543 fixup_called = 0;
1544 is_dwz = 0;
1545 spec_is_dwz = 0;
1546 }
1547 };
1548
1549 /* This data structure holds the information of an abbrev. */
1550 struct abbrev_info
1551 {
1552 unsigned int number; /* number identifying abbrev */
1553 enum dwarf_tag tag; /* dwarf tag */
1554 unsigned short has_children; /* boolean */
1555 unsigned short num_attrs; /* number of attributes */
1556 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1557 struct abbrev_info *next; /* next in chain */
1558 };
1559
1560 struct attr_abbrev
1561 {
1562 ENUM_BITFIELD(dwarf_attribute) name : 16;
1563 ENUM_BITFIELD(dwarf_form) form : 16;
1564
1565 /* It is valid only if FORM is DW_FORM_implicit_const. */
1566 LONGEST implicit_const;
1567 };
1568
1569 /* Size of abbrev_table.abbrev_hash_table. */
1570 #define ABBREV_HASH_SIZE 121
1571
1572 /* Top level data structure to contain an abbreviation table. */
1573
1574 struct abbrev_table
1575 {
1576 explicit abbrev_table (sect_offset off)
1577 : sect_off (off)
1578 {
1579 m_abbrevs =
1580 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1581 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1582 }
1583
1584 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1585
1586 /* Allocate space for a struct abbrev_info object in
1587 ABBREV_TABLE. */
1588 struct abbrev_info *alloc_abbrev ();
1589
1590 /* Add an abbreviation to the table. */
1591 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1592
1593 /* Look up an abbrev in the table.
1594 Returns NULL if the abbrev is not found. */
1595
1596 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1597
1598
1599 /* Where the abbrev table came from.
1600 This is used as a sanity check when the table is used. */
1601 const sect_offset sect_off;
1602
1603 /* Storage for the abbrev table. */
1604 auto_obstack abbrev_obstack;
1605
1606 private:
1607
1608 /* Hash table of abbrevs.
1609 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1610 It could be statically allocated, but the previous code didn't so we
1611 don't either. */
1612 struct abbrev_info **m_abbrevs;
1613 };
1614
1615 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1616
1617 /* Attributes have a name and a value. */
1618 struct attribute
1619 {
1620 ENUM_BITFIELD(dwarf_attribute) name : 16;
1621 ENUM_BITFIELD(dwarf_form) form : 15;
1622
1623 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1624 field should be in u.str (existing only for DW_STRING) but it is kept
1625 here for better struct attribute alignment. */
1626 unsigned int string_is_canonical : 1;
1627
1628 union
1629 {
1630 const char *str;
1631 struct dwarf_block *blk;
1632 ULONGEST unsnd;
1633 LONGEST snd;
1634 CORE_ADDR addr;
1635 ULONGEST signature;
1636 }
1637 u;
1638 };
1639
1640 /* This data structure holds a complete die structure. */
1641 struct die_info
1642 {
1643 /* DWARF-2 tag for this DIE. */
1644 ENUM_BITFIELD(dwarf_tag) tag : 16;
1645
1646 /* Number of attributes */
1647 unsigned char num_attrs;
1648
1649 /* True if we're presently building the full type name for the
1650 type derived from this DIE. */
1651 unsigned char building_fullname : 1;
1652
1653 /* True if this die is in process. PR 16581. */
1654 unsigned char in_process : 1;
1655
1656 /* Abbrev number */
1657 unsigned int abbrev;
1658
1659 /* Offset in .debug_info or .debug_types section. */
1660 sect_offset sect_off;
1661
1662 /* The dies in a compilation unit form an n-ary tree. PARENT
1663 points to this die's parent; CHILD points to the first child of
1664 this node; and all the children of a given node are chained
1665 together via their SIBLING fields. */
1666 struct die_info *child; /* Its first child, if any. */
1667 struct die_info *sibling; /* Its next sibling, if any. */
1668 struct die_info *parent; /* Its parent, if any. */
1669
1670 /* An array of attributes, with NUM_ATTRS elements. There may be
1671 zero, but it's not common and zero-sized arrays are not
1672 sufficiently portable C. */
1673 struct attribute attrs[1];
1674 };
1675
1676 /* Get at parts of an attribute structure. */
1677
1678 #define DW_STRING(attr) ((attr)->u.str)
1679 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1680 #define DW_UNSND(attr) ((attr)->u.unsnd)
1681 #define DW_BLOCK(attr) ((attr)->u.blk)
1682 #define DW_SND(attr) ((attr)->u.snd)
1683 #define DW_ADDR(attr) ((attr)->u.addr)
1684 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1685
1686 /* Blocks are a bunch of untyped bytes. */
1687 struct dwarf_block
1688 {
1689 size_t size;
1690
1691 /* Valid only if SIZE is not zero. */
1692 const gdb_byte *data;
1693 };
1694
1695 #ifndef ATTR_ALLOC_CHUNK
1696 #define ATTR_ALLOC_CHUNK 4
1697 #endif
1698
1699 /* Allocate fields for structs, unions and enums in this size. */
1700 #ifndef DW_FIELD_ALLOC_CHUNK
1701 #define DW_FIELD_ALLOC_CHUNK 4
1702 #endif
1703
1704 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1705 but this would require a corresponding change in unpack_field_as_long
1706 and friends. */
1707 static int bits_per_byte = 8;
1708
1709 /* When reading a variant or variant part, we track a bit more
1710 information about the field, and store it in an object of this
1711 type. */
1712
1713 struct variant_field
1714 {
1715 /* If we see a DW_TAG_variant, then this will be the discriminant
1716 value. */
1717 ULONGEST discriminant_value;
1718 /* If we see a DW_TAG_variant, then this will be set if this is the
1719 default branch. */
1720 bool default_branch;
1721 /* While reading a DW_TAG_variant_part, this will be set if this
1722 field is the discriminant. */
1723 bool is_discriminant;
1724 };
1725
1726 struct nextfield
1727 {
1728 int accessibility = 0;
1729 int virtuality = 0;
1730 /* Extra information to describe a variant or variant part. */
1731 struct variant_field variant {};
1732 struct field field {};
1733 };
1734
1735 struct fnfieldlist
1736 {
1737 const char *name = nullptr;
1738 std::vector<struct fn_field> fnfields;
1739 };
1740
1741 /* The routines that read and process dies for a C struct or C++ class
1742 pass lists of data member fields and lists of member function fields
1743 in an instance of a field_info structure, as defined below. */
1744 struct field_info
1745 {
1746 /* List of data member and baseclasses fields. */
1747 std::vector<struct nextfield> fields;
1748 std::vector<struct nextfield> baseclasses;
1749
1750 /* Number of fields (including baseclasses). */
1751 int nfields = 0;
1752
1753 /* Set if the accesibility of one of the fields is not public. */
1754 int non_public_fields = 0;
1755
1756 /* Member function fieldlist array, contains name of possibly overloaded
1757 member function, number of overloaded member functions and a pointer
1758 to the head of the member function field chain. */
1759 std::vector<struct fnfieldlist> fnfieldlists;
1760
1761 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1762 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1763 std::vector<struct decl_field> typedef_field_list;
1764
1765 /* Nested types defined by this class and the number of elements in this
1766 list. */
1767 std::vector<struct decl_field> nested_types_list;
1768 };
1769
1770 /* One item on the queue of compilation units to read in full symbols
1771 for. */
1772 struct dwarf2_queue_item
1773 {
1774 struct dwarf2_per_cu_data *per_cu;
1775 enum language pretend_language;
1776 struct dwarf2_queue_item *next;
1777 };
1778
1779 /* The current queue. */
1780 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1781
1782 /* Loaded secondary compilation units are kept in memory until they
1783 have not been referenced for the processing of this many
1784 compilation units. Set this to zero to disable caching. Cache
1785 sizes of up to at least twenty will improve startup time for
1786 typical inter-CU-reference binaries, at an obvious memory cost. */
1787 static int dwarf_max_cache_age = 5;
1788 static void
1789 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1790 struct cmd_list_element *c, const char *value)
1791 {
1792 fprintf_filtered (file, _("The upper bound on the age of cached "
1793 "DWARF compilation units is %s.\n"),
1794 value);
1795 }
1796 \f
1797 /* local function prototypes */
1798
1799 static const char *get_section_name (const struct dwarf2_section_info *);
1800
1801 static const char *get_section_file_name (const struct dwarf2_section_info *);
1802
1803 static void dwarf2_find_base_address (struct die_info *die,
1804 struct dwarf2_cu *cu);
1805
1806 static struct partial_symtab *create_partial_symtab
1807 (struct dwarf2_per_cu_data *per_cu, const char *name);
1808
1809 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1810 const gdb_byte *info_ptr,
1811 struct die_info *type_unit_die,
1812 int has_children, void *data);
1813
1814 static void dwarf2_build_psymtabs_hard
1815 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1816
1817 static void scan_partial_symbols (struct partial_die_info *,
1818 CORE_ADDR *, CORE_ADDR *,
1819 int, struct dwarf2_cu *);
1820
1821 static void add_partial_symbol (struct partial_die_info *,
1822 struct dwarf2_cu *);
1823
1824 static void add_partial_namespace (struct partial_die_info *pdi,
1825 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1826 int set_addrmap, struct dwarf2_cu *cu);
1827
1828 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1829 CORE_ADDR *highpc, int set_addrmap,
1830 struct dwarf2_cu *cu);
1831
1832 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1833 struct dwarf2_cu *cu);
1834
1835 static void add_partial_subprogram (struct partial_die_info *pdi,
1836 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1837 int need_pc, struct dwarf2_cu *cu);
1838
1839 static void dwarf2_read_symtab (struct partial_symtab *,
1840 struct objfile *);
1841
1842 static void psymtab_to_symtab_1 (struct partial_symtab *);
1843
1844 static abbrev_table_up abbrev_table_read_table
1845 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1846 sect_offset);
1847
1848 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1849
1850 static struct partial_die_info *load_partial_dies
1851 (const struct die_reader_specs *, const gdb_byte *, int);
1852
1853 static struct partial_die_info *find_partial_die (sect_offset, int,
1854 struct dwarf2_cu *);
1855
1856 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1857 struct attribute *, struct attr_abbrev *,
1858 const gdb_byte *);
1859
1860 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1861
1862 static int read_1_signed_byte (bfd *, const gdb_byte *);
1863
1864 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1865
1866 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1867
1868 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1869
1870 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1871 unsigned int *);
1872
1873 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1874
1875 static LONGEST read_checked_initial_length_and_offset
1876 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1877 unsigned int *, unsigned int *);
1878
1879 static LONGEST read_offset (bfd *, const gdb_byte *,
1880 const struct comp_unit_head *,
1881 unsigned int *);
1882
1883 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1884
1885 static sect_offset read_abbrev_offset
1886 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1887 struct dwarf2_section_info *, sect_offset);
1888
1889 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1890
1891 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1892
1893 static const char *read_indirect_string
1894 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1895 const struct comp_unit_head *, unsigned int *);
1896
1897 static const char *read_indirect_line_string
1898 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1899 const struct comp_unit_head *, unsigned int *);
1900
1901 static const char *read_indirect_string_at_offset
1902 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1903 LONGEST str_offset);
1904
1905 static const char *read_indirect_string_from_dwz
1906 (struct objfile *objfile, struct dwz_file *, LONGEST);
1907
1908 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1909
1910 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1911 const gdb_byte *,
1912 unsigned int *);
1913
1914 static const char *read_str_index (const struct die_reader_specs *reader,
1915 ULONGEST str_index);
1916
1917 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1918
1919 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1920 struct dwarf2_cu *);
1921
1922 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1923 unsigned int);
1924
1925 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1926 struct dwarf2_cu *cu);
1927
1928 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1929 struct dwarf2_cu *cu);
1930
1931 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1932
1933 static struct die_info *die_specification (struct die_info *die,
1934 struct dwarf2_cu **);
1935
1936 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1937 struct dwarf2_cu *cu);
1938
1939 static void dwarf_decode_lines (struct line_header *, const char *,
1940 struct dwarf2_cu *, struct partial_symtab *,
1941 CORE_ADDR, int decode_mapping);
1942
1943 static void dwarf2_start_subfile (const char *, const char *);
1944
1945 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1946 const char *, const char *,
1947 CORE_ADDR);
1948
1949 static struct symbol *new_symbol (struct die_info *, struct type *,
1950 struct dwarf2_cu *, struct symbol * = NULL);
1951
1952 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1953 struct dwarf2_cu *);
1954
1955 static void dwarf2_const_value_attr (const struct attribute *attr,
1956 struct type *type,
1957 const char *name,
1958 struct obstack *obstack,
1959 struct dwarf2_cu *cu, LONGEST *value,
1960 const gdb_byte **bytes,
1961 struct dwarf2_locexpr_baton **baton);
1962
1963 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1964
1965 static int need_gnat_info (struct dwarf2_cu *);
1966
1967 static struct type *die_descriptive_type (struct die_info *,
1968 struct dwarf2_cu *);
1969
1970 static void set_descriptive_type (struct type *, struct die_info *,
1971 struct dwarf2_cu *);
1972
1973 static struct type *die_containing_type (struct die_info *,
1974 struct dwarf2_cu *);
1975
1976 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1977 struct dwarf2_cu *);
1978
1979 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1980
1981 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1982
1983 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1984
1985 static char *typename_concat (struct obstack *obs, const char *prefix,
1986 const char *suffix, int physname,
1987 struct dwarf2_cu *cu);
1988
1989 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1990
1991 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1992
1993 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1994
1995 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1996
1997 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1998
1999 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
2000
2001 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
2002 struct dwarf2_cu *, struct partial_symtab *);
2003
2004 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
2005 values. Keep the items ordered with increasing constraints compliance. */
2006 enum pc_bounds_kind
2007 {
2008 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
2009 PC_BOUNDS_NOT_PRESENT,
2010
2011 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
2012 were present but they do not form a valid range of PC addresses. */
2013 PC_BOUNDS_INVALID,
2014
2015 /* Discontiguous range was found - that is DW_AT_ranges was found. */
2016 PC_BOUNDS_RANGES,
2017
2018 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
2019 PC_BOUNDS_HIGH_LOW,
2020 };
2021
2022 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
2023 CORE_ADDR *, CORE_ADDR *,
2024 struct dwarf2_cu *,
2025 struct partial_symtab *);
2026
2027 static void get_scope_pc_bounds (struct die_info *,
2028 CORE_ADDR *, CORE_ADDR *,
2029 struct dwarf2_cu *);
2030
2031 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
2032 CORE_ADDR, struct dwarf2_cu *);
2033
2034 static void dwarf2_add_field (struct field_info *, struct die_info *,
2035 struct dwarf2_cu *);
2036
2037 static void dwarf2_attach_fields_to_type (struct field_info *,
2038 struct type *, struct dwarf2_cu *);
2039
2040 static void dwarf2_add_member_fn (struct field_info *,
2041 struct die_info *, struct type *,
2042 struct dwarf2_cu *);
2043
2044 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
2045 struct type *,
2046 struct dwarf2_cu *);
2047
2048 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
2049
2050 static void read_common_block (struct die_info *, struct dwarf2_cu *);
2051
2052 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
2053
2054 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
2055
2056 static struct using_direct **using_directives (enum language);
2057
2058 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
2059
2060 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
2061
2062 static struct type *read_module_type (struct die_info *die,
2063 struct dwarf2_cu *cu);
2064
2065 static const char *namespace_name (struct die_info *die,
2066 int *is_anonymous, struct dwarf2_cu *);
2067
2068 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
2069
2070 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
2071
2072 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
2073 struct dwarf2_cu *);
2074
2075 static struct die_info *read_die_and_siblings_1
2076 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
2077 struct die_info *);
2078
2079 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
2080 const gdb_byte *info_ptr,
2081 const gdb_byte **new_info_ptr,
2082 struct die_info *parent);
2083
2084 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
2085 struct die_info **, const gdb_byte *,
2086 int *, int);
2087
2088 static const gdb_byte *read_full_die (const struct die_reader_specs *,
2089 struct die_info **, const gdb_byte *,
2090 int *);
2091
2092 static void process_die (struct die_info *, struct dwarf2_cu *);
2093
2094 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
2095 struct obstack *);
2096
2097 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
2098
2099 static const char *dwarf2_full_name (const char *name,
2100 struct die_info *die,
2101 struct dwarf2_cu *cu);
2102
2103 static const char *dwarf2_physname (const char *name, struct die_info *die,
2104 struct dwarf2_cu *cu);
2105
2106 static struct die_info *dwarf2_extension (struct die_info *die,
2107 struct dwarf2_cu **);
2108
2109 static const char *dwarf_tag_name (unsigned int);
2110
2111 static const char *dwarf_attr_name (unsigned int);
2112
2113 static const char *dwarf_form_name (unsigned int);
2114
2115 static const char *dwarf_bool_name (unsigned int);
2116
2117 static const char *dwarf_type_encoding_name (unsigned int);
2118
2119 static struct die_info *sibling_die (struct die_info *);
2120
2121 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
2122
2123 static void dump_die_for_error (struct die_info *);
2124
2125 static void dump_die_1 (struct ui_file *, int level, int max_level,
2126 struct die_info *);
2127
2128 /*static*/ void dump_die (struct die_info *, int max_level);
2129
2130 static void store_in_ref_table (struct die_info *,
2131 struct dwarf2_cu *);
2132
2133 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
2134
2135 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
2136
2137 static struct die_info *follow_die_ref_or_sig (struct die_info *,
2138 const struct attribute *,
2139 struct dwarf2_cu **);
2140
2141 static struct die_info *follow_die_ref (struct die_info *,
2142 const struct attribute *,
2143 struct dwarf2_cu **);
2144
2145 static struct die_info *follow_die_sig (struct die_info *,
2146 const struct attribute *,
2147 struct dwarf2_cu **);
2148
2149 static struct type *get_signatured_type (struct die_info *, ULONGEST,
2150 struct dwarf2_cu *);
2151
2152 static struct type *get_DW_AT_signature_type (struct die_info *,
2153 const struct attribute *,
2154 struct dwarf2_cu *);
2155
2156 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
2157
2158 static void read_signatured_type (struct signatured_type *);
2159
2160 static int attr_to_dynamic_prop (const struct attribute *attr,
2161 struct die_info *die, struct dwarf2_cu *cu,
2162 struct dynamic_prop *prop);
2163
2164 /* memory allocation interface */
2165
2166 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
2167
2168 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
2169
2170 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2171
2172 static int attr_form_is_block (const struct attribute *);
2173
2174 static int attr_form_is_section_offset (const struct attribute *);
2175
2176 static int attr_form_is_constant (const struct attribute *);
2177
2178 static int attr_form_is_ref (const struct attribute *);
2179
2180 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2181 struct dwarf2_loclist_baton *baton,
2182 const struct attribute *attr);
2183
2184 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
2185 struct symbol *sym,
2186 struct dwarf2_cu *cu,
2187 int is_block);
2188
2189 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2190 const gdb_byte *info_ptr,
2191 struct abbrev_info *abbrev);
2192
2193 static hashval_t partial_die_hash (const void *item);
2194
2195 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2196
2197 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
2198 (sect_offset sect_off, unsigned int offset_in_dwz,
2199 struct dwarf2_per_objfile *dwarf2_per_objfile);
2200
2201 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
2202 struct die_info *comp_unit_die,
2203 enum language pretend_language);
2204
2205 static void free_cached_comp_units (void *);
2206
2207 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2208
2209 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
2210
2211 static struct type *set_die_type (struct die_info *, struct type *,
2212 struct dwarf2_cu *);
2213
2214 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2215
2216 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2217
2218 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2219 enum language);
2220
2221 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2222 enum language);
2223
2224 static void process_full_type_unit (struct dwarf2_per_cu_data *,
2225 enum language);
2226
2227 static void dwarf2_add_dependence (struct dwarf2_cu *,
2228 struct dwarf2_per_cu_data *);
2229
2230 static void dwarf2_mark (struct dwarf2_cu *);
2231
2232 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2233
2234 static struct type *get_die_type_at_offset (sect_offset,
2235 struct dwarf2_per_cu_data *);
2236
2237 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
2238
2239 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2240 enum language pretend_language);
2241
2242 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
2243
2244 /* Class, the destructor of which frees all allocated queue entries. This
2245 will only have work to do if an error was thrown while processing the
2246 dwarf. If no error was thrown then the queue entries should have all
2247 been processed, and freed, as we went along. */
2248
2249 class dwarf2_queue_guard
2250 {
2251 public:
2252 dwarf2_queue_guard () = default;
2253
2254 /* Free any entries remaining on the queue. There should only be
2255 entries left if we hit an error while processing the dwarf. */
2256 ~dwarf2_queue_guard ()
2257 {
2258 struct dwarf2_queue_item *item, *last;
2259
2260 item = dwarf2_queue;
2261 while (item)
2262 {
2263 /* Anything still marked queued is likely to be in an
2264 inconsistent state, so discard it. */
2265 if (item->per_cu->queued)
2266 {
2267 if (item->per_cu->cu != NULL)
2268 free_one_cached_comp_unit (item->per_cu);
2269 item->per_cu->queued = 0;
2270 }
2271
2272 last = item;
2273 item = item->next;
2274 xfree (last);
2275 }
2276
2277 dwarf2_queue = dwarf2_queue_tail = NULL;
2278 }
2279 };
2280
2281 /* The return type of find_file_and_directory. Note, the enclosed
2282 string pointers are only valid while this object is valid. */
2283
2284 struct file_and_directory
2285 {
2286 /* The filename. This is never NULL. */
2287 const char *name;
2288
2289 /* The compilation directory. NULL if not known. If we needed to
2290 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2291 points directly to the DW_AT_comp_dir string attribute owned by
2292 the obstack that owns the DIE. */
2293 const char *comp_dir;
2294
2295 /* If we needed to build a new string for comp_dir, this is what
2296 owns the storage. */
2297 std::string comp_dir_storage;
2298 };
2299
2300 static file_and_directory find_file_and_directory (struct die_info *die,
2301 struct dwarf2_cu *cu);
2302
2303 static char *file_full_name (int file, struct line_header *lh,
2304 const char *comp_dir);
2305
2306 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2307 enum class rcuh_kind { COMPILE, TYPE };
2308
2309 static const gdb_byte *read_and_check_comp_unit_head
2310 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2311 struct comp_unit_head *header,
2312 struct dwarf2_section_info *section,
2313 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2314 rcuh_kind section_kind);
2315
2316 static void init_cutu_and_read_dies
2317 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2318 int use_existing_cu, int keep,
2319 die_reader_func_ftype *die_reader_func, void *data);
2320
2321 static void init_cutu_and_read_dies_simple
2322 (struct dwarf2_per_cu_data *this_cu,
2323 die_reader_func_ftype *die_reader_func, void *data);
2324
2325 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2326
2327 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2328
2329 static struct dwo_unit *lookup_dwo_unit_in_dwp
2330 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2331 struct dwp_file *dwp_file, const char *comp_dir,
2332 ULONGEST signature, int is_debug_types);
2333
2334 static struct dwp_file *get_dwp_file
2335 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2336
2337 static struct dwo_unit *lookup_dwo_comp_unit
2338 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2339
2340 static struct dwo_unit *lookup_dwo_type_unit
2341 (struct signatured_type *, const char *, const char *);
2342
2343 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2344
2345 static void free_dwo_file_cleanup (void *);
2346
2347 struct free_dwo_file_cleanup_data
2348 {
2349 struct dwo_file *dwo_file;
2350 struct dwarf2_per_objfile *dwarf2_per_objfile;
2351 };
2352
2353 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2354
2355 static void check_producer (struct dwarf2_cu *cu);
2356
2357 static void free_line_header_voidp (void *arg);
2358 \f
2359 /* Various complaints about symbol reading that don't abort the process. */
2360
2361 static void
2362 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2363 {
2364 complaint (&symfile_complaints,
2365 _("statement list doesn't fit in .debug_line section"));
2366 }
2367
2368 static void
2369 dwarf2_debug_line_missing_file_complaint (void)
2370 {
2371 complaint (&symfile_complaints,
2372 _(".debug_line section has line data without a file"));
2373 }
2374
2375 static void
2376 dwarf2_debug_line_missing_end_sequence_complaint (void)
2377 {
2378 complaint (&symfile_complaints,
2379 _(".debug_line section has line "
2380 "program sequence without an end"));
2381 }
2382
2383 static void
2384 dwarf2_complex_location_expr_complaint (void)
2385 {
2386 complaint (&symfile_complaints, _("location expression too complex"));
2387 }
2388
2389 static void
2390 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2391 int arg3)
2392 {
2393 complaint (&symfile_complaints,
2394 _("const value length mismatch for '%s', got %d, expected %d"),
2395 arg1, arg2, arg3);
2396 }
2397
2398 static void
2399 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2400 {
2401 complaint (&symfile_complaints,
2402 _("debug info runs off end of %s section"
2403 " [in module %s]"),
2404 get_section_name (section),
2405 get_section_file_name (section));
2406 }
2407
2408 static void
2409 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2410 {
2411 complaint (&symfile_complaints,
2412 _("macro debug info contains a "
2413 "malformed macro definition:\n`%s'"),
2414 arg1);
2415 }
2416
2417 static void
2418 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2419 {
2420 complaint (&symfile_complaints,
2421 _("invalid attribute class or form for '%s' in '%s'"),
2422 arg1, arg2);
2423 }
2424
2425 /* Hash function for line_header_hash. */
2426
2427 static hashval_t
2428 line_header_hash (const struct line_header *ofs)
2429 {
2430 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2431 }
2432
2433 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2434
2435 static hashval_t
2436 line_header_hash_voidp (const void *item)
2437 {
2438 const struct line_header *ofs = (const struct line_header *) item;
2439
2440 return line_header_hash (ofs);
2441 }
2442
2443 /* Equality function for line_header_hash. */
2444
2445 static int
2446 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2447 {
2448 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2449 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2450
2451 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2452 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2453 }
2454
2455 \f
2456
2457 /* Read the given attribute value as an address, taking the attribute's
2458 form into account. */
2459
2460 static CORE_ADDR
2461 attr_value_as_address (struct attribute *attr)
2462 {
2463 CORE_ADDR addr;
2464
2465 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2466 {
2467 /* Aside from a few clearly defined exceptions, attributes that
2468 contain an address must always be in DW_FORM_addr form.
2469 Unfortunately, some compilers happen to be violating this
2470 requirement by encoding addresses using other forms, such
2471 as DW_FORM_data4 for example. For those broken compilers,
2472 we try to do our best, without any guarantee of success,
2473 to interpret the address correctly. It would also be nice
2474 to generate a complaint, but that would require us to maintain
2475 a list of legitimate cases where a non-address form is allowed,
2476 as well as update callers to pass in at least the CU's DWARF
2477 version. This is more overhead than what we're willing to
2478 expand for a pretty rare case. */
2479 addr = DW_UNSND (attr);
2480 }
2481 else
2482 addr = DW_ADDR (attr);
2483
2484 return addr;
2485 }
2486
2487 /* The suffix for an index file. */
2488 #define INDEX4_SUFFIX ".gdb-index"
2489 #define INDEX5_SUFFIX ".debug_names"
2490 #define DEBUG_STR_SUFFIX ".debug_str"
2491
2492 /* See declaration. */
2493
2494 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2495 const dwarf2_debug_sections *names)
2496 : objfile (objfile_)
2497 {
2498 if (names == NULL)
2499 names = &dwarf2_elf_names;
2500
2501 bfd *obfd = objfile->obfd;
2502
2503 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2504 locate_sections (obfd, sec, *names);
2505 }
2506
2507 static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2508
2509 dwarf2_per_objfile::~dwarf2_per_objfile ()
2510 {
2511 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2512 free_cached_comp_units ();
2513
2514 if (quick_file_names_table)
2515 htab_delete (quick_file_names_table);
2516
2517 if (line_header_hash)
2518 htab_delete (line_header_hash);
2519
2520 for (int ix = 0; ix < n_comp_units; ++ix)
2521 VEC_free (dwarf2_per_cu_ptr, all_comp_units[ix]->imported_symtabs);
2522
2523 for (int ix = 0; ix < n_type_units; ++ix)
2524 VEC_free (dwarf2_per_cu_ptr,
2525 all_type_units[ix]->per_cu.imported_symtabs);
2526 xfree (all_type_units);
2527
2528 VEC_free (dwarf2_section_info_def, types);
2529
2530 if (dwo_files != NULL)
2531 free_dwo_files (dwo_files, objfile);
2532 if (dwp_file != NULL)
2533 gdb_bfd_unref (dwp_file->dbfd);
2534
2535 if (dwz_file != NULL && dwz_file->dwz_bfd)
2536 gdb_bfd_unref (dwz_file->dwz_bfd);
2537
2538 if (index_table != NULL)
2539 index_table->~mapped_index ();
2540
2541 /* Everything else should be on the objfile obstack. */
2542 }
2543
2544 /* See declaration. */
2545
2546 void
2547 dwarf2_per_objfile::free_cached_comp_units ()
2548 {
2549 dwarf2_per_cu_data *per_cu = read_in_chain;
2550 dwarf2_per_cu_data **last_chain = &read_in_chain;
2551 while (per_cu != NULL)
2552 {
2553 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2554
2555 delete per_cu->cu;
2556 *last_chain = next_cu;
2557 per_cu = next_cu;
2558 }
2559 }
2560
2561 /* Try to locate the sections we need for DWARF 2 debugging
2562 information and return true if we have enough to do something.
2563 NAMES points to the dwarf2 section names, or is NULL if the standard
2564 ELF names are used. */
2565
2566 int
2567 dwarf2_has_info (struct objfile *objfile,
2568 const struct dwarf2_debug_sections *names)
2569 {
2570 if (objfile->flags & OBJF_READNEVER)
2571 return 0;
2572
2573 struct dwarf2_per_objfile *dwarf2_per_objfile
2574 = get_dwarf2_per_objfile (objfile);
2575
2576 if (dwarf2_per_objfile == NULL)
2577 {
2578 /* Initialize per-objfile state. */
2579 dwarf2_per_objfile
2580 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2581 names);
2582 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
2583 }
2584 return (!dwarf2_per_objfile->info.is_virtual
2585 && dwarf2_per_objfile->info.s.section != NULL
2586 && !dwarf2_per_objfile->abbrev.is_virtual
2587 && dwarf2_per_objfile->abbrev.s.section != NULL);
2588 }
2589
2590 /* Return the containing section of virtual section SECTION. */
2591
2592 static struct dwarf2_section_info *
2593 get_containing_section (const struct dwarf2_section_info *section)
2594 {
2595 gdb_assert (section->is_virtual);
2596 return section->s.containing_section;
2597 }
2598
2599 /* Return the bfd owner of SECTION. */
2600
2601 static struct bfd *
2602 get_section_bfd_owner (const struct dwarf2_section_info *section)
2603 {
2604 if (section->is_virtual)
2605 {
2606 section = get_containing_section (section);
2607 gdb_assert (!section->is_virtual);
2608 }
2609 return section->s.section->owner;
2610 }
2611
2612 /* Return the bfd section of SECTION.
2613 Returns NULL if the section is not present. */
2614
2615 static asection *
2616 get_section_bfd_section (const struct dwarf2_section_info *section)
2617 {
2618 if (section->is_virtual)
2619 {
2620 section = get_containing_section (section);
2621 gdb_assert (!section->is_virtual);
2622 }
2623 return section->s.section;
2624 }
2625
2626 /* Return the name of SECTION. */
2627
2628 static const char *
2629 get_section_name (const struct dwarf2_section_info *section)
2630 {
2631 asection *sectp = get_section_bfd_section (section);
2632
2633 gdb_assert (sectp != NULL);
2634 return bfd_section_name (get_section_bfd_owner (section), sectp);
2635 }
2636
2637 /* Return the name of the file SECTION is in. */
2638
2639 static const char *
2640 get_section_file_name (const struct dwarf2_section_info *section)
2641 {
2642 bfd *abfd = get_section_bfd_owner (section);
2643
2644 return bfd_get_filename (abfd);
2645 }
2646
2647 /* Return the id of SECTION.
2648 Returns 0 if SECTION doesn't exist. */
2649
2650 static int
2651 get_section_id (const struct dwarf2_section_info *section)
2652 {
2653 asection *sectp = get_section_bfd_section (section);
2654
2655 if (sectp == NULL)
2656 return 0;
2657 return sectp->id;
2658 }
2659
2660 /* Return the flags of SECTION.
2661 SECTION (or containing section if this is a virtual section) must exist. */
2662
2663 static int
2664 get_section_flags (const struct dwarf2_section_info *section)
2665 {
2666 asection *sectp = get_section_bfd_section (section);
2667
2668 gdb_assert (sectp != NULL);
2669 return bfd_get_section_flags (sectp->owner, sectp);
2670 }
2671
2672 /* When loading sections, we look either for uncompressed section or for
2673 compressed section names. */
2674
2675 static int
2676 section_is_p (const char *section_name,
2677 const struct dwarf2_section_names *names)
2678 {
2679 if (names->normal != NULL
2680 && strcmp (section_name, names->normal) == 0)
2681 return 1;
2682 if (names->compressed != NULL
2683 && strcmp (section_name, names->compressed) == 0)
2684 return 1;
2685 return 0;
2686 }
2687
2688 /* See declaration. */
2689
2690 void
2691 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2692 const dwarf2_debug_sections &names)
2693 {
2694 flagword aflag = bfd_get_section_flags (abfd, sectp);
2695
2696 if ((aflag & SEC_HAS_CONTENTS) == 0)
2697 {
2698 }
2699 else if (section_is_p (sectp->name, &names.info))
2700 {
2701 this->info.s.section = sectp;
2702 this->info.size = bfd_get_section_size (sectp);
2703 }
2704 else if (section_is_p (sectp->name, &names.abbrev))
2705 {
2706 this->abbrev.s.section = sectp;
2707 this->abbrev.size = bfd_get_section_size (sectp);
2708 }
2709 else if (section_is_p (sectp->name, &names.line))
2710 {
2711 this->line.s.section = sectp;
2712 this->line.size = bfd_get_section_size (sectp);
2713 }
2714 else if (section_is_p (sectp->name, &names.loc))
2715 {
2716 this->loc.s.section = sectp;
2717 this->loc.size = bfd_get_section_size (sectp);
2718 }
2719 else if (section_is_p (sectp->name, &names.loclists))
2720 {
2721 this->loclists.s.section = sectp;
2722 this->loclists.size = bfd_get_section_size (sectp);
2723 }
2724 else if (section_is_p (sectp->name, &names.macinfo))
2725 {
2726 this->macinfo.s.section = sectp;
2727 this->macinfo.size = bfd_get_section_size (sectp);
2728 }
2729 else if (section_is_p (sectp->name, &names.macro))
2730 {
2731 this->macro.s.section = sectp;
2732 this->macro.size = bfd_get_section_size (sectp);
2733 }
2734 else if (section_is_p (sectp->name, &names.str))
2735 {
2736 this->str.s.section = sectp;
2737 this->str.size = bfd_get_section_size (sectp);
2738 }
2739 else if (section_is_p (sectp->name, &names.line_str))
2740 {
2741 this->line_str.s.section = sectp;
2742 this->line_str.size = bfd_get_section_size (sectp);
2743 }
2744 else if (section_is_p (sectp->name, &names.addr))
2745 {
2746 this->addr.s.section = sectp;
2747 this->addr.size = bfd_get_section_size (sectp);
2748 }
2749 else if (section_is_p (sectp->name, &names.frame))
2750 {
2751 this->frame.s.section = sectp;
2752 this->frame.size = bfd_get_section_size (sectp);
2753 }
2754 else if (section_is_p (sectp->name, &names.eh_frame))
2755 {
2756 this->eh_frame.s.section = sectp;
2757 this->eh_frame.size = bfd_get_section_size (sectp);
2758 }
2759 else if (section_is_p (sectp->name, &names.ranges))
2760 {
2761 this->ranges.s.section = sectp;
2762 this->ranges.size = bfd_get_section_size (sectp);
2763 }
2764 else if (section_is_p (sectp->name, &names.rnglists))
2765 {
2766 this->rnglists.s.section = sectp;
2767 this->rnglists.size = bfd_get_section_size (sectp);
2768 }
2769 else if (section_is_p (sectp->name, &names.types))
2770 {
2771 struct dwarf2_section_info type_section;
2772
2773 memset (&type_section, 0, sizeof (type_section));
2774 type_section.s.section = sectp;
2775 type_section.size = bfd_get_section_size (sectp);
2776
2777 VEC_safe_push (dwarf2_section_info_def, this->types,
2778 &type_section);
2779 }
2780 else if (section_is_p (sectp->name, &names.gdb_index))
2781 {
2782 this->gdb_index.s.section = sectp;
2783 this->gdb_index.size = bfd_get_section_size (sectp);
2784 }
2785 else if (section_is_p (sectp->name, &names.debug_names))
2786 {
2787 this->debug_names.s.section = sectp;
2788 this->debug_names.size = bfd_get_section_size (sectp);
2789 }
2790 else if (section_is_p (sectp->name, &names.debug_aranges))
2791 {
2792 this->debug_aranges.s.section = sectp;
2793 this->debug_aranges.size = bfd_get_section_size (sectp);
2794 }
2795
2796 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2797 && bfd_section_vma (abfd, sectp) == 0)
2798 this->has_section_at_zero = true;
2799 }
2800
2801 /* A helper function that decides whether a section is empty,
2802 or not present. */
2803
2804 static int
2805 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2806 {
2807 if (section->is_virtual)
2808 return section->size == 0;
2809 return section->s.section == NULL || section->size == 0;
2810 }
2811
2812 /* Read the contents of the section INFO.
2813 OBJFILE is the main object file, but not necessarily the file where
2814 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2815 of the DWO file.
2816 If the section is compressed, uncompress it before returning. */
2817
2818 static void
2819 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2820 {
2821 asection *sectp;
2822 bfd *abfd;
2823 gdb_byte *buf, *retbuf;
2824
2825 if (info->readin)
2826 return;
2827 info->buffer = NULL;
2828 info->readin = 1;
2829
2830 if (dwarf2_section_empty_p (info))
2831 return;
2832
2833 sectp = get_section_bfd_section (info);
2834
2835 /* If this is a virtual section we need to read in the real one first. */
2836 if (info->is_virtual)
2837 {
2838 struct dwarf2_section_info *containing_section =
2839 get_containing_section (info);
2840
2841 gdb_assert (sectp != NULL);
2842 if ((sectp->flags & SEC_RELOC) != 0)
2843 {
2844 error (_("Dwarf Error: DWP format V2 with relocations is not"
2845 " supported in section %s [in module %s]"),
2846 get_section_name (info), get_section_file_name (info));
2847 }
2848 dwarf2_read_section (objfile, containing_section);
2849 /* Other code should have already caught virtual sections that don't
2850 fit. */
2851 gdb_assert (info->virtual_offset + info->size
2852 <= containing_section->size);
2853 /* If the real section is empty or there was a problem reading the
2854 section we shouldn't get here. */
2855 gdb_assert (containing_section->buffer != NULL);
2856 info->buffer = containing_section->buffer + info->virtual_offset;
2857 return;
2858 }
2859
2860 /* If the section has relocations, we must read it ourselves.
2861 Otherwise we attach it to the BFD. */
2862 if ((sectp->flags & SEC_RELOC) == 0)
2863 {
2864 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2865 return;
2866 }
2867
2868 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2869 info->buffer = buf;
2870
2871 /* When debugging .o files, we may need to apply relocations; see
2872 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2873 We never compress sections in .o files, so we only need to
2874 try this when the section is not compressed. */
2875 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2876 if (retbuf != NULL)
2877 {
2878 info->buffer = retbuf;
2879 return;
2880 }
2881
2882 abfd = get_section_bfd_owner (info);
2883 gdb_assert (abfd != NULL);
2884
2885 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2886 || bfd_bread (buf, info->size, abfd) != info->size)
2887 {
2888 error (_("Dwarf Error: Can't read DWARF data"
2889 " in section %s [in module %s]"),
2890 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2891 }
2892 }
2893
2894 /* A helper function that returns the size of a section in a safe way.
2895 If you are positive that the section has been read before using the
2896 size, then it is safe to refer to the dwarf2_section_info object's
2897 "size" field directly. In other cases, you must call this
2898 function, because for compressed sections the size field is not set
2899 correctly until the section has been read. */
2900
2901 static bfd_size_type
2902 dwarf2_section_size (struct objfile *objfile,
2903 struct dwarf2_section_info *info)
2904 {
2905 if (!info->readin)
2906 dwarf2_read_section (objfile, info);
2907 return info->size;
2908 }
2909
2910 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2911 SECTION_NAME. */
2912
2913 void
2914 dwarf2_get_section_info (struct objfile *objfile,
2915 enum dwarf2_section_enum sect,
2916 asection **sectp, const gdb_byte **bufp,
2917 bfd_size_type *sizep)
2918 {
2919 struct dwarf2_per_objfile *data
2920 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2921 dwarf2_objfile_data_key);
2922 struct dwarf2_section_info *info;
2923
2924 /* We may see an objfile without any DWARF, in which case we just
2925 return nothing. */
2926 if (data == NULL)
2927 {
2928 *sectp = NULL;
2929 *bufp = NULL;
2930 *sizep = 0;
2931 return;
2932 }
2933 switch (sect)
2934 {
2935 case DWARF2_DEBUG_FRAME:
2936 info = &data->frame;
2937 break;
2938 case DWARF2_EH_FRAME:
2939 info = &data->eh_frame;
2940 break;
2941 default:
2942 gdb_assert_not_reached ("unexpected section");
2943 }
2944
2945 dwarf2_read_section (objfile, info);
2946
2947 *sectp = get_section_bfd_section (info);
2948 *bufp = info->buffer;
2949 *sizep = info->size;
2950 }
2951
2952 /* A helper function to find the sections for a .dwz file. */
2953
2954 static void
2955 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2956 {
2957 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2958
2959 /* Note that we only support the standard ELF names, because .dwz
2960 is ELF-only (at the time of writing). */
2961 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2962 {
2963 dwz_file->abbrev.s.section = sectp;
2964 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2965 }
2966 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2967 {
2968 dwz_file->info.s.section = sectp;
2969 dwz_file->info.size = bfd_get_section_size (sectp);
2970 }
2971 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2972 {
2973 dwz_file->str.s.section = sectp;
2974 dwz_file->str.size = bfd_get_section_size (sectp);
2975 }
2976 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2977 {
2978 dwz_file->line.s.section = sectp;
2979 dwz_file->line.size = bfd_get_section_size (sectp);
2980 }
2981 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2982 {
2983 dwz_file->macro.s.section = sectp;
2984 dwz_file->macro.size = bfd_get_section_size (sectp);
2985 }
2986 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2987 {
2988 dwz_file->gdb_index.s.section = sectp;
2989 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2990 }
2991 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2992 {
2993 dwz_file->debug_names.s.section = sectp;
2994 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2995 }
2996 }
2997
2998 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2999 there is no .gnu_debugaltlink section in the file. Error if there
3000 is such a section but the file cannot be found. */
3001
3002 static struct dwz_file *
3003 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
3004 {
3005 const char *filename;
3006 struct dwz_file *result;
3007 bfd_size_type buildid_len_arg;
3008 size_t buildid_len;
3009 bfd_byte *buildid;
3010
3011 if (dwarf2_per_objfile->dwz_file != NULL)
3012 return dwarf2_per_objfile->dwz_file;
3013
3014 bfd_set_error (bfd_error_no_error);
3015 gdb::unique_xmalloc_ptr<char> data
3016 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
3017 &buildid_len_arg, &buildid));
3018 if (data == NULL)
3019 {
3020 if (bfd_get_error () == bfd_error_no_error)
3021 return NULL;
3022 error (_("could not read '.gnu_debugaltlink' section: %s"),
3023 bfd_errmsg (bfd_get_error ()));
3024 }
3025
3026 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
3027
3028 buildid_len = (size_t) buildid_len_arg;
3029
3030 filename = data.get ();
3031
3032 std::string abs_storage;
3033 if (!IS_ABSOLUTE_PATH (filename))
3034 {
3035 gdb::unique_xmalloc_ptr<char> abs
3036 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
3037
3038 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
3039 filename = abs_storage.c_str ();
3040 }
3041
3042 /* First try the file name given in the section. If that doesn't
3043 work, try to use the build-id instead. */
3044 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
3045 if (dwz_bfd != NULL)
3046 {
3047 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
3048 dwz_bfd.release ();
3049 }
3050
3051 if (dwz_bfd == NULL)
3052 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
3053
3054 if (dwz_bfd == NULL)
3055 error (_("could not find '.gnu_debugaltlink' file for %s"),
3056 objfile_name (dwarf2_per_objfile->objfile));
3057
3058 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
3059 struct dwz_file);
3060 result->dwz_bfd = dwz_bfd.release ();
3061
3062 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
3063
3064 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
3065 dwarf2_per_objfile->dwz_file = result;
3066 return result;
3067 }
3068 \f
3069 /* DWARF quick_symbols_functions support. */
3070
3071 /* TUs can share .debug_line entries, and there can be a lot more TUs than
3072 unique line tables, so we maintain a separate table of all .debug_line
3073 derived entries to support the sharing.
3074 All the quick functions need is the list of file names. We discard the
3075 line_header when we're done and don't need to record it here. */
3076 struct quick_file_names
3077 {
3078 /* The data used to construct the hash key. */
3079 struct stmt_list_hash hash;
3080
3081 /* The number of entries in file_names, real_names. */
3082 unsigned int num_file_names;
3083
3084 /* The file names from the line table, after being run through
3085 file_full_name. */
3086 const char **file_names;
3087
3088 /* The file names from the line table after being run through
3089 gdb_realpath. These are computed lazily. */
3090 const char **real_names;
3091 };
3092
3093 /* When using the index (and thus not using psymtabs), each CU has an
3094 object of this type. This is used to hold information needed by
3095 the various "quick" methods. */
3096 struct dwarf2_per_cu_quick_data
3097 {
3098 /* The file table. This can be NULL if there was no file table
3099 or it's currently not read in.
3100 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
3101 struct quick_file_names *file_names;
3102
3103 /* The corresponding symbol table. This is NULL if symbols for this
3104 CU have not yet been read. */
3105 struct compunit_symtab *compunit_symtab;
3106
3107 /* A temporary mark bit used when iterating over all CUs in
3108 expand_symtabs_matching. */
3109 unsigned int mark : 1;
3110
3111 /* True if we've tried to read the file table and found there isn't one.
3112 There will be no point in trying to read it again next time. */
3113 unsigned int no_file_data : 1;
3114 };
3115
3116 /* Utility hash function for a stmt_list_hash. */
3117
3118 static hashval_t
3119 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
3120 {
3121 hashval_t v = 0;
3122
3123 if (stmt_list_hash->dwo_unit != NULL)
3124 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
3125 v += to_underlying (stmt_list_hash->line_sect_off);
3126 return v;
3127 }
3128
3129 /* Utility equality function for a stmt_list_hash. */
3130
3131 static int
3132 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
3133 const struct stmt_list_hash *rhs)
3134 {
3135 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
3136 return 0;
3137 if (lhs->dwo_unit != NULL
3138 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
3139 return 0;
3140
3141 return lhs->line_sect_off == rhs->line_sect_off;
3142 }
3143
3144 /* Hash function for a quick_file_names. */
3145
3146 static hashval_t
3147 hash_file_name_entry (const void *e)
3148 {
3149 const struct quick_file_names *file_data
3150 = (const struct quick_file_names *) e;
3151
3152 return hash_stmt_list_entry (&file_data->hash);
3153 }
3154
3155 /* Equality function for a quick_file_names. */
3156
3157 static int
3158 eq_file_name_entry (const void *a, const void *b)
3159 {
3160 const struct quick_file_names *ea = (const struct quick_file_names *) a;
3161 const struct quick_file_names *eb = (const struct quick_file_names *) b;
3162
3163 return eq_stmt_list_entry (&ea->hash, &eb->hash);
3164 }
3165
3166 /* Delete function for a quick_file_names. */
3167
3168 static void
3169 delete_file_name_entry (void *e)
3170 {
3171 struct quick_file_names *file_data = (struct quick_file_names *) e;
3172 int i;
3173
3174 for (i = 0; i < file_data->num_file_names; ++i)
3175 {
3176 xfree ((void*) file_data->file_names[i]);
3177 if (file_data->real_names)
3178 xfree ((void*) file_data->real_names[i]);
3179 }
3180
3181 /* The space for the struct itself lives on objfile_obstack,
3182 so we don't free it here. */
3183 }
3184
3185 /* Create a quick_file_names hash table. */
3186
3187 static htab_t
3188 create_quick_file_names_table (unsigned int nr_initial_entries)
3189 {
3190 return htab_create_alloc (nr_initial_entries,
3191 hash_file_name_entry, eq_file_name_entry,
3192 delete_file_name_entry, xcalloc, xfree);
3193 }
3194
3195 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3196 have to be created afterwards. You should call age_cached_comp_units after
3197 processing PER_CU->CU. dw2_setup must have been already called. */
3198
3199 static void
3200 load_cu (struct dwarf2_per_cu_data *per_cu)
3201 {
3202 if (per_cu->is_debug_types)
3203 load_full_type_unit (per_cu);
3204 else
3205 load_full_comp_unit (per_cu, language_minimal);
3206
3207 if (per_cu->cu == NULL)
3208 return; /* Dummy CU. */
3209
3210 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
3211 }
3212
3213 /* Read in the symbols for PER_CU. */
3214
3215 static void
3216 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3217 {
3218 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3219
3220 /* Skip type_unit_groups, reading the type units they contain
3221 is handled elsewhere. */
3222 if (IS_TYPE_UNIT_GROUP (per_cu))
3223 return;
3224
3225 /* The destructor of dwarf2_queue_guard frees any entries left on
3226 the queue. After this point we're guaranteed to leave this function
3227 with the dwarf queue empty. */
3228 dwarf2_queue_guard q_guard;
3229
3230 if (dwarf2_per_objfile->using_index
3231 ? per_cu->v.quick->compunit_symtab == NULL
3232 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3233 {
3234 queue_comp_unit (per_cu, language_minimal);
3235 load_cu (per_cu);
3236
3237 /* If we just loaded a CU from a DWO, and we're working with an index
3238 that may badly handle TUs, load all the TUs in that DWO as well.
3239 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3240 if (!per_cu->is_debug_types
3241 && per_cu->cu != NULL
3242 && per_cu->cu->dwo_unit != NULL
3243 && dwarf2_per_objfile->index_table != NULL
3244 && dwarf2_per_objfile->index_table->version <= 7
3245 /* DWP files aren't supported yet. */
3246 && get_dwp_file (dwarf2_per_objfile) == NULL)
3247 queue_and_load_all_dwo_tus (per_cu);
3248 }
3249
3250 process_queue (dwarf2_per_objfile);
3251
3252 /* Age the cache, releasing compilation units that have not
3253 been used recently. */
3254 age_cached_comp_units (dwarf2_per_objfile);
3255 }
3256
3257 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3258 the objfile from which this CU came. Returns the resulting symbol
3259 table. */
3260
3261 static struct compunit_symtab *
3262 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3263 {
3264 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3265
3266 gdb_assert (dwarf2_per_objfile->using_index);
3267 if (!per_cu->v.quick->compunit_symtab)
3268 {
3269 struct cleanup *back_to = make_cleanup (free_cached_comp_units,
3270 dwarf2_per_objfile);
3271 scoped_restore decrementer = increment_reading_symtab ();
3272 dw2_do_instantiate_symtab (per_cu);
3273 process_cu_includes (dwarf2_per_objfile);
3274 do_cleanups (back_to);
3275 }
3276
3277 return per_cu->v.quick->compunit_symtab;
3278 }
3279
3280 /* Return the CU/TU given its index.
3281
3282 This is intended for loops like:
3283
3284 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3285 + dwarf2_per_objfile->n_type_units); ++i)
3286 {
3287 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3288
3289 ...;
3290 }
3291 */
3292
3293 static struct dwarf2_per_cu_data *
3294 dw2_get_cutu (struct dwarf2_per_objfile *dwarf2_per_objfile,
3295 int index)
3296 {
3297 if (index >= dwarf2_per_objfile->n_comp_units)
3298 {
3299 index -= dwarf2_per_objfile->n_comp_units;
3300 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3301 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
3302 }
3303
3304 return dwarf2_per_objfile->all_comp_units[index];
3305 }
3306
3307 /* Return the CU given its index.
3308 This differs from dw2_get_cutu in that it's for when you know INDEX
3309 refers to a CU. */
3310
3311 static struct dwarf2_per_cu_data *
3312 dw2_get_cu (struct dwarf2_per_objfile *dwarf2_per_objfile, int index)
3313 {
3314 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
3315
3316 return dwarf2_per_objfile->all_comp_units[index];
3317 }
3318
3319 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3320 objfile_obstack, and constructed with the specified field
3321 values. */
3322
3323 static dwarf2_per_cu_data *
3324 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3325 struct dwarf2_section_info *section,
3326 int is_dwz,
3327 sect_offset sect_off, ULONGEST length)
3328 {
3329 struct objfile *objfile = dwarf2_per_objfile->objfile;
3330 dwarf2_per_cu_data *the_cu
3331 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3332 struct dwarf2_per_cu_data);
3333 the_cu->sect_off = sect_off;
3334 the_cu->length = length;
3335 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
3336 the_cu->section = section;
3337 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3338 struct dwarf2_per_cu_quick_data);
3339 the_cu->is_dwz = is_dwz;
3340 return the_cu;
3341 }
3342
3343 /* A helper for create_cus_from_index that handles a given list of
3344 CUs. */
3345
3346 static void
3347 create_cus_from_index_list (struct objfile *objfile,
3348 const gdb_byte *cu_list, offset_type n_elements,
3349 struct dwarf2_section_info *section,
3350 int is_dwz,
3351 int base_offset)
3352 {
3353 offset_type i;
3354 struct dwarf2_per_objfile *dwarf2_per_objfile
3355 = get_dwarf2_per_objfile (objfile);
3356
3357 for (i = 0; i < n_elements; i += 2)
3358 {
3359 gdb_static_assert (sizeof (ULONGEST) >= 8);
3360
3361 sect_offset sect_off
3362 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3363 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3364 cu_list += 2 * 8;
3365
3366 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
3367 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3368 sect_off, length);
3369 }
3370 }
3371
3372 /* Read the CU list from the mapped index, and use it to create all
3373 the CU objects for this objfile. */
3374
3375 static void
3376 create_cus_from_index (struct objfile *objfile,
3377 const gdb_byte *cu_list, offset_type cu_list_elements,
3378 const gdb_byte *dwz_list, offset_type dwz_elements)
3379 {
3380 struct dwz_file *dwz;
3381 struct dwarf2_per_objfile *dwarf2_per_objfile
3382 = get_dwarf2_per_objfile (objfile);
3383
3384 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3385 dwarf2_per_objfile->all_comp_units =
3386 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3387 dwarf2_per_objfile->n_comp_units);
3388
3389 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3390 &dwarf2_per_objfile->info, 0, 0);
3391
3392 if (dwz_elements == 0)
3393 return;
3394
3395 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3396 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3397 cu_list_elements / 2);
3398 }
3399
3400 /* Create the signatured type hash table from the index. */
3401
3402 static void
3403 create_signatured_type_table_from_index (struct objfile *objfile,
3404 struct dwarf2_section_info *section,
3405 const gdb_byte *bytes,
3406 offset_type elements)
3407 {
3408 offset_type i;
3409 htab_t sig_types_hash;
3410 struct dwarf2_per_objfile *dwarf2_per_objfile
3411 = get_dwarf2_per_objfile (objfile);
3412
3413 dwarf2_per_objfile->n_type_units
3414 = dwarf2_per_objfile->n_allocated_type_units
3415 = elements / 3;
3416 dwarf2_per_objfile->all_type_units =
3417 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3418
3419 sig_types_hash = allocate_signatured_type_table (objfile);
3420
3421 for (i = 0; i < elements; i += 3)
3422 {
3423 struct signatured_type *sig_type;
3424 ULONGEST signature;
3425 void **slot;
3426 cu_offset type_offset_in_tu;
3427
3428 gdb_static_assert (sizeof (ULONGEST) >= 8);
3429 sect_offset sect_off
3430 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3431 type_offset_in_tu
3432 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3433 BFD_ENDIAN_LITTLE);
3434 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3435 bytes += 3 * 8;
3436
3437 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3438 struct signatured_type);
3439 sig_type->signature = signature;
3440 sig_type->type_offset_in_tu = type_offset_in_tu;
3441 sig_type->per_cu.is_debug_types = 1;
3442 sig_type->per_cu.section = section;
3443 sig_type->per_cu.sect_off = sect_off;
3444 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3445 sig_type->per_cu.v.quick
3446 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3447 struct dwarf2_per_cu_quick_data);
3448
3449 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3450 *slot = sig_type;
3451
3452 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3453 }
3454
3455 dwarf2_per_objfile->signatured_types = sig_types_hash;
3456 }
3457
3458 /* Create the signatured type hash table from .debug_names. */
3459
3460 static void
3461 create_signatured_type_table_from_debug_names
3462 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3463 const mapped_debug_names &map,
3464 struct dwarf2_section_info *section,
3465 struct dwarf2_section_info *abbrev_section)
3466 {
3467 struct objfile *objfile = dwarf2_per_objfile->objfile;
3468
3469 dwarf2_read_section (objfile, section);
3470 dwarf2_read_section (objfile, abbrev_section);
3471
3472 dwarf2_per_objfile->n_type_units
3473 = dwarf2_per_objfile->n_allocated_type_units
3474 = map.tu_count;
3475 dwarf2_per_objfile->all_type_units
3476 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3477
3478 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3479
3480 for (uint32_t i = 0; i < map.tu_count; ++i)
3481 {
3482 struct signatured_type *sig_type;
3483 ULONGEST signature;
3484 void **slot;
3485 cu_offset type_offset_in_tu;
3486
3487 sect_offset sect_off
3488 = (sect_offset) (extract_unsigned_integer
3489 (map.tu_table_reordered + i * map.offset_size,
3490 map.offset_size,
3491 map.dwarf5_byte_order));
3492
3493 comp_unit_head cu_header;
3494 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3495 abbrev_section,
3496 section->buffer + to_underlying (sect_off),
3497 rcuh_kind::TYPE);
3498
3499 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3500 struct signatured_type);
3501 sig_type->signature = cu_header.signature;
3502 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3503 sig_type->per_cu.is_debug_types = 1;
3504 sig_type->per_cu.section = section;
3505 sig_type->per_cu.sect_off = sect_off;
3506 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3507 sig_type->per_cu.v.quick
3508 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3509 struct dwarf2_per_cu_quick_data);
3510
3511 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3512 *slot = sig_type;
3513
3514 dwarf2_per_objfile->all_type_units[i] = sig_type;
3515 }
3516
3517 dwarf2_per_objfile->signatured_types = sig_types_hash;
3518 }
3519
3520 /* Read the address map data from the mapped index, and use it to
3521 populate the objfile's psymtabs_addrmap. */
3522
3523 static void
3524 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3525 struct mapped_index *index)
3526 {
3527 struct objfile *objfile = dwarf2_per_objfile->objfile;
3528 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3529 const gdb_byte *iter, *end;
3530 struct addrmap *mutable_map;
3531 CORE_ADDR baseaddr;
3532
3533 auto_obstack temp_obstack;
3534
3535 mutable_map = addrmap_create_mutable (&temp_obstack);
3536
3537 iter = index->address_table.data ();
3538 end = iter + index->address_table.size ();
3539
3540 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3541
3542 while (iter < end)
3543 {
3544 ULONGEST hi, lo, cu_index;
3545 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3546 iter += 8;
3547 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3548 iter += 8;
3549 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3550 iter += 4;
3551
3552 if (lo > hi)
3553 {
3554 complaint (&symfile_complaints,
3555 _(".gdb_index address table has invalid range (%s - %s)"),
3556 hex_string (lo), hex_string (hi));
3557 continue;
3558 }
3559
3560 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3561 {
3562 complaint (&symfile_complaints,
3563 _(".gdb_index address table has invalid CU number %u"),
3564 (unsigned) cu_index);
3565 continue;
3566 }
3567
3568 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3569 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3570 addrmap_set_empty (mutable_map, lo, hi - 1,
3571 dw2_get_cutu (dwarf2_per_objfile, cu_index));
3572 }
3573
3574 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3575 &objfile->objfile_obstack);
3576 }
3577
3578 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3579 populate the objfile's psymtabs_addrmap. */
3580
3581 static void
3582 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3583 struct dwarf2_section_info *section)
3584 {
3585 struct objfile *objfile = dwarf2_per_objfile->objfile;
3586 bfd *abfd = objfile->obfd;
3587 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3588 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3589 SECT_OFF_TEXT (objfile));
3590
3591 auto_obstack temp_obstack;
3592 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3593
3594 std::unordered_map<sect_offset,
3595 dwarf2_per_cu_data *,
3596 gdb::hash_enum<sect_offset>>
3597 debug_info_offset_to_per_cu;
3598 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3599 {
3600 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, cui);
3601 const auto insertpair
3602 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3603 if (!insertpair.second)
3604 {
3605 warning (_("Section .debug_aranges in %s has duplicate "
3606 "debug_info_offset %s, ignoring .debug_aranges."),
3607 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3608 return;
3609 }
3610 }
3611
3612 dwarf2_read_section (objfile, section);
3613
3614 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3615
3616 const gdb_byte *addr = section->buffer;
3617
3618 while (addr < section->buffer + section->size)
3619 {
3620 const gdb_byte *const entry_addr = addr;
3621 unsigned int bytes_read;
3622
3623 const LONGEST entry_length = read_initial_length (abfd, addr,
3624 &bytes_read);
3625 addr += bytes_read;
3626
3627 const gdb_byte *const entry_end = addr + entry_length;
3628 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3629 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3630 if (addr + entry_length > section->buffer + section->size)
3631 {
3632 warning (_("Section .debug_aranges in %s entry at offset %zu "
3633 "length %s exceeds section length %s, "
3634 "ignoring .debug_aranges."),
3635 objfile_name (objfile), entry_addr - section->buffer,
3636 plongest (bytes_read + entry_length),
3637 pulongest (section->size));
3638 return;
3639 }
3640
3641 /* The version number. */
3642 const uint16_t version = read_2_bytes (abfd, addr);
3643 addr += 2;
3644 if (version != 2)
3645 {
3646 warning (_("Section .debug_aranges in %s entry at offset %zu "
3647 "has unsupported version %d, ignoring .debug_aranges."),
3648 objfile_name (objfile), entry_addr - section->buffer,
3649 version);
3650 return;
3651 }
3652
3653 const uint64_t debug_info_offset
3654 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3655 addr += offset_size;
3656 const auto per_cu_it
3657 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3658 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3659 {
3660 warning (_("Section .debug_aranges in %s entry at offset %zu "
3661 "debug_info_offset %s does not exists, "
3662 "ignoring .debug_aranges."),
3663 objfile_name (objfile), entry_addr - section->buffer,
3664 pulongest (debug_info_offset));
3665 return;
3666 }
3667 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3668
3669 const uint8_t address_size = *addr++;
3670 if (address_size < 1 || address_size > 8)
3671 {
3672 warning (_("Section .debug_aranges in %s entry at offset %zu "
3673 "address_size %u is invalid, ignoring .debug_aranges."),
3674 objfile_name (objfile), entry_addr - section->buffer,
3675 address_size);
3676 return;
3677 }
3678
3679 const uint8_t segment_selector_size = *addr++;
3680 if (segment_selector_size != 0)
3681 {
3682 warning (_("Section .debug_aranges in %s entry at offset %zu "
3683 "segment_selector_size %u is not supported, "
3684 "ignoring .debug_aranges."),
3685 objfile_name (objfile), entry_addr - section->buffer,
3686 segment_selector_size);
3687 return;
3688 }
3689
3690 /* Must pad to an alignment boundary that is twice the address
3691 size. It is undocumented by the DWARF standard but GCC does
3692 use it. */
3693 for (size_t padding = ((-(addr - section->buffer))
3694 & (2 * address_size - 1));
3695 padding > 0; padding--)
3696 if (*addr++ != 0)
3697 {
3698 warning (_("Section .debug_aranges in %s entry at offset %zu "
3699 "padding is not zero, ignoring .debug_aranges."),
3700 objfile_name (objfile), entry_addr - section->buffer);
3701 return;
3702 }
3703
3704 for (;;)
3705 {
3706 if (addr + 2 * address_size > entry_end)
3707 {
3708 warning (_("Section .debug_aranges in %s entry at offset %zu "
3709 "address list is not properly terminated, "
3710 "ignoring .debug_aranges."),
3711 objfile_name (objfile), entry_addr - section->buffer);
3712 return;
3713 }
3714 ULONGEST start = extract_unsigned_integer (addr, address_size,
3715 dwarf5_byte_order);
3716 addr += address_size;
3717 ULONGEST length = extract_unsigned_integer (addr, address_size,
3718 dwarf5_byte_order);
3719 addr += address_size;
3720 if (start == 0 && length == 0)
3721 break;
3722 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3723 {
3724 /* Symbol was eliminated due to a COMDAT group. */
3725 continue;
3726 }
3727 ULONGEST end = start + length;
3728 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3729 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3730 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3731 }
3732 }
3733
3734 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3735 &objfile->objfile_obstack);
3736 }
3737
3738 /* The hash function for strings in the mapped index. This is the same as
3739 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3740 implementation. This is necessary because the hash function is tied to the
3741 format of the mapped index file. The hash values do not have to match with
3742 SYMBOL_HASH_NEXT.
3743
3744 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3745
3746 static hashval_t
3747 mapped_index_string_hash (int index_version, const void *p)
3748 {
3749 const unsigned char *str = (const unsigned char *) p;
3750 hashval_t r = 0;
3751 unsigned char c;
3752
3753 while ((c = *str++) != 0)
3754 {
3755 if (index_version >= 5)
3756 c = tolower (c);
3757 r = r * 67 + c - 113;
3758 }
3759
3760 return r;
3761 }
3762
3763 /* Find a slot in the mapped index INDEX for the object named NAME.
3764 If NAME is found, set *VEC_OUT to point to the CU vector in the
3765 constant pool and return true. If NAME cannot be found, return
3766 false. */
3767
3768 static bool
3769 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3770 offset_type **vec_out)
3771 {
3772 offset_type hash;
3773 offset_type slot, step;
3774 int (*cmp) (const char *, const char *);
3775
3776 gdb::unique_xmalloc_ptr<char> without_params;
3777 if (current_language->la_language == language_cplus
3778 || current_language->la_language == language_fortran
3779 || current_language->la_language == language_d)
3780 {
3781 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3782 not contain any. */
3783
3784 if (strchr (name, '(') != NULL)
3785 {
3786 without_params = cp_remove_params (name);
3787
3788 if (without_params != NULL)
3789 name = without_params.get ();
3790 }
3791 }
3792
3793 /* Index version 4 did not support case insensitive searches. But the
3794 indices for case insensitive languages are built in lowercase, therefore
3795 simulate our NAME being searched is also lowercased. */
3796 hash = mapped_index_string_hash ((index->version == 4
3797 && case_sensitivity == case_sensitive_off
3798 ? 5 : index->version),
3799 name);
3800
3801 slot = hash & (index->symbol_table.size () - 1);
3802 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3803 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3804
3805 for (;;)
3806 {
3807 const char *str;
3808
3809 const auto &bucket = index->symbol_table[slot];
3810 if (bucket.name == 0 && bucket.vec == 0)
3811 return false;
3812
3813 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3814 if (!cmp (name, str))
3815 {
3816 *vec_out = (offset_type *) (index->constant_pool
3817 + MAYBE_SWAP (bucket.vec));
3818 return true;
3819 }
3820
3821 slot = (slot + step) & (index->symbol_table.size () - 1);
3822 }
3823 }
3824
3825 /* A helper function that reads the .gdb_index from SECTION and fills
3826 in MAP. FILENAME is the name of the file containing the section;
3827 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3828 ok to use deprecated sections.
3829
3830 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3831 out parameters that are filled in with information about the CU and
3832 TU lists in the section.
3833
3834 Returns 1 if all went well, 0 otherwise. */
3835
3836 static int
3837 read_index_from_section (struct objfile *objfile,
3838 const char *filename,
3839 int deprecated_ok,
3840 struct dwarf2_section_info *section,
3841 struct mapped_index *map,
3842 const gdb_byte **cu_list,
3843 offset_type *cu_list_elements,
3844 const gdb_byte **types_list,
3845 offset_type *types_list_elements)
3846 {
3847 const gdb_byte *addr;
3848 offset_type version;
3849 offset_type *metadata;
3850 int i;
3851
3852 if (dwarf2_section_empty_p (section))
3853 return 0;
3854
3855 /* Older elfutils strip versions could keep the section in the main
3856 executable while splitting it for the separate debug info file. */
3857 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3858 return 0;
3859
3860 dwarf2_read_section (objfile, section);
3861
3862 addr = section->buffer;
3863 /* Version check. */
3864 version = MAYBE_SWAP (*(offset_type *) addr);
3865 /* Versions earlier than 3 emitted every copy of a psymbol. This
3866 causes the index to behave very poorly for certain requests. Version 3
3867 contained incomplete addrmap. So, it seems better to just ignore such
3868 indices. */
3869 if (version < 4)
3870 {
3871 static int warning_printed = 0;
3872 if (!warning_printed)
3873 {
3874 warning (_("Skipping obsolete .gdb_index section in %s."),
3875 filename);
3876 warning_printed = 1;
3877 }
3878 return 0;
3879 }
3880 /* Index version 4 uses a different hash function than index version
3881 5 and later.
3882
3883 Versions earlier than 6 did not emit psymbols for inlined
3884 functions. Using these files will cause GDB not to be able to
3885 set breakpoints on inlined functions by name, so we ignore these
3886 indices unless the user has done
3887 "set use-deprecated-index-sections on". */
3888 if (version < 6 && !deprecated_ok)
3889 {
3890 static int warning_printed = 0;
3891 if (!warning_printed)
3892 {
3893 warning (_("\
3894 Skipping deprecated .gdb_index section in %s.\n\
3895 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3896 to use the section anyway."),
3897 filename);
3898 warning_printed = 1;
3899 }
3900 return 0;
3901 }
3902 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3903 of the TU (for symbols coming from TUs),
3904 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3905 Plus gold-generated indices can have duplicate entries for global symbols,
3906 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3907 These are just performance bugs, and we can't distinguish gdb-generated
3908 indices from gold-generated ones, so issue no warning here. */
3909
3910 /* Indexes with higher version than the one supported by GDB may be no
3911 longer backward compatible. */
3912 if (version > 8)
3913 return 0;
3914
3915 map->version = version;
3916 map->total_size = section->size;
3917
3918 metadata = (offset_type *) (addr + sizeof (offset_type));
3919
3920 i = 0;
3921 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3922 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3923 / 8);
3924 ++i;
3925
3926 *types_list = addr + MAYBE_SWAP (metadata[i]);
3927 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3928 - MAYBE_SWAP (metadata[i]))
3929 / 8);
3930 ++i;
3931
3932 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3933 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3934 map->address_table
3935 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3936 ++i;
3937
3938 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3939 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3940 map->symbol_table
3941 = gdb::array_view<mapped_index::symbol_table_slot>
3942 ((mapped_index::symbol_table_slot *) symbol_table,
3943 (mapped_index::symbol_table_slot *) symbol_table_end);
3944
3945 ++i;
3946 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3947
3948 return 1;
3949 }
3950
3951 /* Read .gdb_index. If everything went ok, initialize the "quick"
3952 elements of all the CUs and return 1. Otherwise, return 0. */
3953
3954 static int
3955 dwarf2_read_index (struct objfile *objfile)
3956 {
3957 struct mapped_index local_map, *map;
3958 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3959 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3960 struct dwz_file *dwz;
3961 struct dwarf2_per_objfile *dwarf2_per_objfile
3962 = get_dwarf2_per_objfile (objfile);
3963
3964 if (!read_index_from_section (objfile, objfile_name (objfile),
3965 use_deprecated_index_sections,
3966 &dwarf2_per_objfile->gdb_index, &local_map,
3967 &cu_list, &cu_list_elements,
3968 &types_list, &types_list_elements))
3969 return 0;
3970
3971 /* Don't use the index if it's empty. */
3972 if (local_map.symbol_table.empty ())
3973 return 0;
3974
3975 /* If there is a .dwz file, read it so we can get its CU list as
3976 well. */
3977 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3978 if (dwz != NULL)
3979 {
3980 struct mapped_index dwz_map;
3981 const gdb_byte *dwz_types_ignore;
3982 offset_type dwz_types_elements_ignore;
3983
3984 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3985 1,
3986 &dwz->gdb_index, &dwz_map,
3987 &dwz_list, &dwz_list_elements,
3988 &dwz_types_ignore,
3989 &dwz_types_elements_ignore))
3990 {
3991 warning (_("could not read '.gdb_index' section from %s; skipping"),
3992 bfd_get_filename (dwz->dwz_bfd));
3993 return 0;
3994 }
3995 }
3996
3997 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3998 dwz_list_elements);
3999
4000 if (types_list_elements)
4001 {
4002 struct dwarf2_section_info *section;
4003
4004 /* We can only handle a single .debug_types when we have an
4005 index. */
4006 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
4007 return 0;
4008
4009 section = VEC_index (dwarf2_section_info_def,
4010 dwarf2_per_objfile->types, 0);
4011
4012 create_signatured_type_table_from_index (objfile, section, types_list,
4013 types_list_elements);
4014 }
4015
4016 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
4017
4018 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
4019 map = new (map) mapped_index ();
4020 *map = local_map;
4021
4022 dwarf2_per_objfile->index_table = map;
4023 dwarf2_per_objfile->using_index = 1;
4024 dwarf2_per_objfile->quick_file_names_table =
4025 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
4026
4027 return 1;
4028 }
4029
4030 /* die_reader_func for dw2_get_file_names. */
4031
4032 static void
4033 dw2_get_file_names_reader (const struct die_reader_specs *reader,
4034 const gdb_byte *info_ptr,
4035 struct die_info *comp_unit_die,
4036 int has_children,
4037 void *data)
4038 {
4039 struct dwarf2_cu *cu = reader->cu;
4040 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
4041 struct dwarf2_per_objfile *dwarf2_per_objfile
4042 = cu->per_cu->dwarf2_per_objfile;
4043 struct objfile *objfile = dwarf2_per_objfile->objfile;
4044 struct dwarf2_per_cu_data *lh_cu;
4045 struct attribute *attr;
4046 int i;
4047 void **slot;
4048 struct quick_file_names *qfn;
4049
4050 gdb_assert (! this_cu->is_debug_types);
4051
4052 /* Our callers never want to match partial units -- instead they
4053 will match the enclosing full CU. */
4054 if (comp_unit_die->tag == DW_TAG_partial_unit)
4055 {
4056 this_cu->v.quick->no_file_data = 1;
4057 return;
4058 }
4059
4060 lh_cu = this_cu;
4061 slot = NULL;
4062
4063 line_header_up lh;
4064 sect_offset line_offset {};
4065
4066 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4067 if (attr)
4068 {
4069 struct quick_file_names find_entry;
4070
4071 line_offset = (sect_offset) DW_UNSND (attr);
4072
4073 /* We may have already read in this line header (TU line header sharing).
4074 If we have we're done. */
4075 find_entry.hash.dwo_unit = cu->dwo_unit;
4076 find_entry.hash.line_sect_off = line_offset;
4077 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
4078 &find_entry, INSERT);
4079 if (*slot != NULL)
4080 {
4081 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
4082 return;
4083 }
4084
4085 lh = dwarf_decode_line_header (line_offset, cu);
4086 }
4087 if (lh == NULL)
4088 {
4089 lh_cu->v.quick->no_file_data = 1;
4090 return;
4091 }
4092
4093 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
4094 qfn->hash.dwo_unit = cu->dwo_unit;
4095 qfn->hash.line_sect_off = line_offset;
4096 gdb_assert (slot != NULL);
4097 *slot = qfn;
4098
4099 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
4100
4101 qfn->num_file_names = lh->file_names.size ();
4102 qfn->file_names =
4103 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
4104 for (i = 0; i < lh->file_names.size (); ++i)
4105 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
4106 qfn->real_names = NULL;
4107
4108 lh_cu->v.quick->file_names = qfn;
4109 }
4110
4111 /* A helper for the "quick" functions which attempts to read the line
4112 table for THIS_CU. */
4113
4114 static struct quick_file_names *
4115 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
4116 {
4117 /* This should never be called for TUs. */
4118 gdb_assert (! this_cu->is_debug_types);
4119 /* Nor type unit groups. */
4120 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
4121
4122 if (this_cu->v.quick->file_names != NULL)
4123 return this_cu->v.quick->file_names;
4124 /* If we know there is no line data, no point in looking again. */
4125 if (this_cu->v.quick->no_file_data)
4126 return NULL;
4127
4128 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
4129
4130 if (this_cu->v.quick->no_file_data)
4131 return NULL;
4132 return this_cu->v.quick->file_names;
4133 }
4134
4135 /* A helper for the "quick" functions which computes and caches the
4136 real path for a given file name from the line table. */
4137
4138 static const char *
4139 dw2_get_real_path (struct objfile *objfile,
4140 struct quick_file_names *qfn, int index)
4141 {
4142 if (qfn->real_names == NULL)
4143 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
4144 qfn->num_file_names, const char *);
4145
4146 if (qfn->real_names[index] == NULL)
4147 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
4148
4149 return qfn->real_names[index];
4150 }
4151
4152 static struct symtab *
4153 dw2_find_last_source_symtab (struct objfile *objfile)
4154 {
4155 struct dwarf2_per_objfile *dwarf2_per_objfile
4156 = get_dwarf2_per_objfile (objfile);
4157 int index = dwarf2_per_objfile->n_comp_units - 1;
4158 dwarf2_per_cu_data *dwarf_cu = dw2_get_cutu (dwarf2_per_objfile, index);
4159 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
4160
4161 if (cust == NULL)
4162 return NULL;
4163
4164 return compunit_primary_filetab (cust);
4165 }
4166
4167 /* Traversal function for dw2_forget_cached_source_info. */
4168
4169 static int
4170 dw2_free_cached_file_names (void **slot, void *info)
4171 {
4172 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
4173
4174 if (file_data->real_names)
4175 {
4176 int i;
4177
4178 for (i = 0; i < file_data->num_file_names; ++i)
4179 {
4180 xfree ((void*) file_data->real_names[i]);
4181 file_data->real_names[i] = NULL;
4182 }
4183 }
4184
4185 return 1;
4186 }
4187
4188 static void
4189 dw2_forget_cached_source_info (struct objfile *objfile)
4190 {
4191 struct dwarf2_per_objfile *dwarf2_per_objfile
4192 = get_dwarf2_per_objfile (objfile);
4193
4194 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
4195 dw2_free_cached_file_names, NULL);
4196 }
4197
4198 /* Helper function for dw2_map_symtabs_matching_filename that expands
4199 the symtabs and calls the iterator. */
4200
4201 static int
4202 dw2_map_expand_apply (struct objfile *objfile,
4203 struct dwarf2_per_cu_data *per_cu,
4204 const char *name, const char *real_path,
4205 gdb::function_view<bool (symtab *)> callback)
4206 {
4207 struct compunit_symtab *last_made = objfile->compunit_symtabs;
4208
4209 /* Don't visit already-expanded CUs. */
4210 if (per_cu->v.quick->compunit_symtab)
4211 return 0;
4212
4213 /* This may expand more than one symtab, and we want to iterate over
4214 all of them. */
4215 dw2_instantiate_symtab (per_cu);
4216
4217 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4218 last_made, callback);
4219 }
4220
4221 /* Implementation of the map_symtabs_matching_filename method. */
4222
4223 static bool
4224 dw2_map_symtabs_matching_filename
4225 (struct objfile *objfile, const char *name, const char *real_path,
4226 gdb::function_view<bool (symtab *)> callback)
4227 {
4228 int i;
4229 const char *name_basename = lbasename (name);
4230 struct dwarf2_per_objfile *dwarf2_per_objfile
4231 = get_dwarf2_per_objfile (objfile);
4232
4233 /* The rule is CUs specify all the files, including those used by
4234 any TU, so there's no need to scan TUs here. */
4235
4236 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4237 {
4238 int j;
4239 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
4240 struct quick_file_names *file_data;
4241
4242 /* We only need to look at symtabs not already expanded. */
4243 if (per_cu->v.quick->compunit_symtab)
4244 continue;
4245
4246 file_data = dw2_get_file_names (per_cu);
4247 if (file_data == NULL)
4248 continue;
4249
4250 for (j = 0; j < file_data->num_file_names; ++j)
4251 {
4252 const char *this_name = file_data->file_names[j];
4253 const char *this_real_name;
4254
4255 if (compare_filenames_for_search (this_name, name))
4256 {
4257 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4258 callback))
4259 return true;
4260 continue;
4261 }
4262
4263 /* Before we invoke realpath, which can get expensive when many
4264 files are involved, do a quick comparison of the basenames. */
4265 if (! basenames_may_differ
4266 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4267 continue;
4268
4269 this_real_name = dw2_get_real_path (objfile, file_data, j);
4270 if (compare_filenames_for_search (this_real_name, name))
4271 {
4272 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4273 callback))
4274 return true;
4275 continue;
4276 }
4277
4278 if (real_path != NULL)
4279 {
4280 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4281 gdb_assert (IS_ABSOLUTE_PATH (name));
4282 if (this_real_name != NULL
4283 && FILENAME_CMP (real_path, this_real_name) == 0)
4284 {
4285 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4286 callback))
4287 return true;
4288 continue;
4289 }
4290 }
4291 }
4292 }
4293
4294 return false;
4295 }
4296
4297 /* Struct used to manage iterating over all CUs looking for a symbol. */
4298
4299 struct dw2_symtab_iterator
4300 {
4301 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
4302 struct dwarf2_per_objfile *dwarf2_per_objfile;
4303 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4304 int want_specific_block;
4305 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4306 Unused if !WANT_SPECIFIC_BLOCK. */
4307 int block_index;
4308 /* The kind of symbol we're looking for. */
4309 domain_enum domain;
4310 /* The list of CUs from the index entry of the symbol,
4311 or NULL if not found. */
4312 offset_type *vec;
4313 /* The next element in VEC to look at. */
4314 int next;
4315 /* The number of elements in VEC, or zero if there is no match. */
4316 int length;
4317 /* Have we seen a global version of the symbol?
4318 If so we can ignore all further global instances.
4319 This is to work around gold/15646, inefficient gold-generated
4320 indices. */
4321 int global_seen;
4322 };
4323
4324 /* Initialize the index symtab iterator ITER.
4325 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4326 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
4327
4328 static void
4329 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
4330 struct dwarf2_per_objfile *dwarf2_per_objfile,
4331 int want_specific_block,
4332 int block_index,
4333 domain_enum domain,
4334 const char *name)
4335 {
4336 iter->dwarf2_per_objfile = dwarf2_per_objfile;
4337 iter->want_specific_block = want_specific_block;
4338 iter->block_index = block_index;
4339 iter->domain = domain;
4340 iter->next = 0;
4341 iter->global_seen = 0;
4342
4343 mapped_index *index = dwarf2_per_objfile->index_table;
4344
4345 /* index is NULL if OBJF_READNOW. */
4346 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
4347 iter->length = MAYBE_SWAP (*iter->vec);
4348 else
4349 {
4350 iter->vec = NULL;
4351 iter->length = 0;
4352 }
4353 }
4354
4355 /* Return the next matching CU or NULL if there are no more. */
4356
4357 static struct dwarf2_per_cu_data *
4358 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4359 {
4360 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4361
4362 for ( ; iter->next < iter->length; ++iter->next)
4363 {
4364 offset_type cu_index_and_attrs =
4365 MAYBE_SWAP (iter->vec[iter->next + 1]);
4366 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4367 struct dwarf2_per_cu_data *per_cu;
4368 int want_static = iter->block_index != GLOBAL_BLOCK;
4369 /* This value is only valid for index versions >= 7. */
4370 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4371 gdb_index_symbol_kind symbol_kind =
4372 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4373 /* Only check the symbol attributes if they're present.
4374 Indices prior to version 7 don't record them,
4375 and indices >= 7 may elide them for certain symbols
4376 (gold does this). */
4377 int attrs_valid =
4378 (dwarf2_per_objfile->index_table->version >= 7
4379 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4380
4381 /* Don't crash on bad data. */
4382 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4383 + dwarf2_per_objfile->n_type_units))
4384 {
4385 complaint (&symfile_complaints,
4386 _(".gdb_index entry has bad CU index"
4387 " [in module %s]"),
4388 objfile_name (dwarf2_per_objfile->objfile));
4389 continue;
4390 }
4391
4392 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
4393
4394 /* Skip if already read in. */
4395 if (per_cu->v.quick->compunit_symtab)
4396 continue;
4397
4398 /* Check static vs global. */
4399 if (attrs_valid)
4400 {
4401 if (iter->want_specific_block
4402 && want_static != is_static)
4403 continue;
4404 /* Work around gold/15646. */
4405 if (!is_static && iter->global_seen)
4406 continue;
4407 if (!is_static)
4408 iter->global_seen = 1;
4409 }
4410
4411 /* Only check the symbol's kind if it has one. */
4412 if (attrs_valid)
4413 {
4414 switch (iter->domain)
4415 {
4416 case VAR_DOMAIN:
4417 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4418 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4419 /* Some types are also in VAR_DOMAIN. */
4420 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4421 continue;
4422 break;
4423 case STRUCT_DOMAIN:
4424 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4425 continue;
4426 break;
4427 case LABEL_DOMAIN:
4428 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4429 continue;
4430 break;
4431 default:
4432 break;
4433 }
4434 }
4435
4436 ++iter->next;
4437 return per_cu;
4438 }
4439
4440 return NULL;
4441 }
4442
4443 static struct compunit_symtab *
4444 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4445 const char *name, domain_enum domain)
4446 {
4447 struct compunit_symtab *stab_best = NULL;
4448 struct dwarf2_per_objfile *dwarf2_per_objfile
4449 = get_dwarf2_per_objfile (objfile);
4450
4451 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4452
4453 struct dw2_symtab_iterator iter;
4454 struct dwarf2_per_cu_data *per_cu;
4455
4456 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4457
4458 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4459 {
4460 struct symbol *sym, *with_opaque = NULL;
4461 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4462 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4463 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4464
4465 sym = block_find_symbol (block, name, domain,
4466 block_find_non_opaque_type_preferred,
4467 &with_opaque);
4468
4469 /* Some caution must be observed with overloaded functions
4470 and methods, since the index will not contain any overload
4471 information (but NAME might contain it). */
4472
4473 if (sym != NULL
4474 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4475 return stab;
4476 if (with_opaque != NULL
4477 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4478 stab_best = stab;
4479
4480 /* Keep looking through other CUs. */
4481 }
4482
4483 return stab_best;
4484 }
4485
4486 static void
4487 dw2_print_stats (struct objfile *objfile)
4488 {
4489 struct dwarf2_per_objfile *dwarf2_per_objfile
4490 = get_dwarf2_per_objfile (objfile);
4491 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4492 int count = 0;
4493
4494 for (int i = 0; i < total; ++i)
4495 {
4496 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4497
4498 if (!per_cu->v.quick->compunit_symtab)
4499 ++count;
4500 }
4501 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4502 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4503 }
4504
4505 /* This dumps minimal information about the index.
4506 It is called via "mt print objfiles".
4507 One use is to verify .gdb_index has been loaded by the
4508 gdb.dwarf2/gdb-index.exp testcase. */
4509
4510 static void
4511 dw2_dump (struct objfile *objfile)
4512 {
4513 struct dwarf2_per_objfile *dwarf2_per_objfile
4514 = get_dwarf2_per_objfile (objfile);
4515
4516 gdb_assert (dwarf2_per_objfile->using_index);
4517 printf_filtered (".gdb_index:");
4518 if (dwarf2_per_objfile->index_table != NULL)
4519 {
4520 printf_filtered (" version %d\n",
4521 dwarf2_per_objfile->index_table->version);
4522 }
4523 else
4524 printf_filtered (" faked for \"readnow\"\n");
4525 printf_filtered ("\n");
4526 }
4527
4528 static void
4529 dw2_relocate (struct objfile *objfile,
4530 const struct section_offsets *new_offsets,
4531 const struct section_offsets *delta)
4532 {
4533 /* There's nothing to relocate here. */
4534 }
4535
4536 static void
4537 dw2_expand_symtabs_for_function (struct objfile *objfile,
4538 const char *func_name)
4539 {
4540 struct dwarf2_per_objfile *dwarf2_per_objfile
4541 = get_dwarf2_per_objfile (objfile);
4542
4543 struct dw2_symtab_iterator iter;
4544 struct dwarf2_per_cu_data *per_cu;
4545
4546 /* Note: It doesn't matter what we pass for block_index here. */
4547 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4548 func_name);
4549
4550 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4551 dw2_instantiate_symtab (per_cu);
4552
4553 }
4554
4555 static void
4556 dw2_expand_all_symtabs (struct objfile *objfile)
4557 {
4558 struct dwarf2_per_objfile *dwarf2_per_objfile
4559 = get_dwarf2_per_objfile (objfile);
4560 int total_units = (dwarf2_per_objfile->n_comp_units
4561 + dwarf2_per_objfile->n_type_units);
4562
4563 for (int i = 0; i < total_units; ++i)
4564 {
4565 struct dwarf2_per_cu_data *per_cu
4566 = dw2_get_cutu (dwarf2_per_objfile, i);
4567
4568 dw2_instantiate_symtab (per_cu);
4569 }
4570 }
4571
4572 static void
4573 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4574 const char *fullname)
4575 {
4576 struct dwarf2_per_objfile *dwarf2_per_objfile
4577 = get_dwarf2_per_objfile (objfile);
4578
4579 /* We don't need to consider type units here.
4580 This is only called for examining code, e.g. expand_line_sal.
4581 There can be an order of magnitude (or more) more type units
4582 than comp units, and we avoid them if we can. */
4583
4584 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4585 {
4586 int j;
4587 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4588 struct quick_file_names *file_data;
4589
4590 /* We only need to look at symtabs not already expanded. */
4591 if (per_cu->v.quick->compunit_symtab)
4592 continue;
4593
4594 file_data = dw2_get_file_names (per_cu);
4595 if (file_data == NULL)
4596 continue;
4597
4598 for (j = 0; j < file_data->num_file_names; ++j)
4599 {
4600 const char *this_fullname = file_data->file_names[j];
4601
4602 if (filename_cmp (this_fullname, fullname) == 0)
4603 {
4604 dw2_instantiate_symtab (per_cu);
4605 break;
4606 }
4607 }
4608 }
4609 }
4610
4611 static void
4612 dw2_map_matching_symbols (struct objfile *objfile,
4613 const char * name, domain_enum domain,
4614 int global,
4615 int (*callback) (struct block *,
4616 struct symbol *, void *),
4617 void *data, symbol_name_match_type match,
4618 symbol_compare_ftype *ordered_compare)
4619 {
4620 /* Currently unimplemented; used for Ada. The function can be called if the
4621 current language is Ada for a non-Ada objfile using GNU index. As Ada
4622 does not look for non-Ada symbols this function should just return. */
4623 }
4624
4625 /* Symbol name matcher for .gdb_index names.
4626
4627 Symbol names in .gdb_index have a few particularities:
4628
4629 - There's no indication of which is the language of each symbol.
4630
4631 Since each language has its own symbol name matching algorithm,
4632 and we don't know which language is the right one, we must match
4633 each symbol against all languages. This would be a potential
4634 performance problem if it were not mitigated by the
4635 mapped_index::name_components lookup table, which significantly
4636 reduces the number of times we need to call into this matcher,
4637 making it a non-issue.
4638
4639 - Symbol names in the index have no overload (parameter)
4640 information. I.e., in C++, "foo(int)" and "foo(long)" both
4641 appear as "foo" in the index, for example.
4642
4643 This means that the lookup names passed to the symbol name
4644 matcher functions must have no parameter information either
4645 because (e.g.) symbol search name "foo" does not match
4646 lookup-name "foo(int)" [while swapping search name for lookup
4647 name would match].
4648 */
4649 class gdb_index_symbol_name_matcher
4650 {
4651 public:
4652 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4653 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4654
4655 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4656 Returns true if any matcher matches. */
4657 bool matches (const char *symbol_name);
4658
4659 private:
4660 /* A reference to the lookup name we're matching against. */
4661 const lookup_name_info &m_lookup_name;
4662
4663 /* A vector holding all the different symbol name matchers, for all
4664 languages. */
4665 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4666 };
4667
4668 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4669 (const lookup_name_info &lookup_name)
4670 : m_lookup_name (lookup_name)
4671 {
4672 /* Prepare the vector of comparison functions upfront, to avoid
4673 doing the same work for each symbol. Care is taken to avoid
4674 matching with the same matcher more than once if/when multiple
4675 languages use the same matcher function. */
4676 auto &matchers = m_symbol_name_matcher_funcs;
4677 matchers.reserve (nr_languages);
4678
4679 matchers.push_back (default_symbol_name_matcher);
4680
4681 for (int i = 0; i < nr_languages; i++)
4682 {
4683 const language_defn *lang = language_def ((enum language) i);
4684 symbol_name_matcher_ftype *name_matcher
4685 = get_symbol_name_matcher (lang, m_lookup_name);
4686
4687 /* Don't insert the same comparison routine more than once.
4688 Note that we do this linear walk instead of a seemingly
4689 cheaper sorted insert, or use a std::set or something like
4690 that, because relative order of function addresses is not
4691 stable. This is not a problem in practice because the number
4692 of supported languages is low, and the cost here is tiny
4693 compared to the number of searches we'll do afterwards using
4694 this object. */
4695 if (name_matcher != default_symbol_name_matcher
4696 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4697 == matchers.end ()))
4698 matchers.push_back (name_matcher);
4699 }
4700 }
4701
4702 bool
4703 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4704 {
4705 for (auto matches_name : m_symbol_name_matcher_funcs)
4706 if (matches_name (symbol_name, m_lookup_name, NULL))
4707 return true;
4708
4709 return false;
4710 }
4711
4712 /* Starting from a search name, return the string that finds the upper
4713 bound of all strings that start with SEARCH_NAME in a sorted name
4714 list. Returns the empty string to indicate that the upper bound is
4715 the end of the list. */
4716
4717 static std::string
4718 make_sort_after_prefix_name (const char *search_name)
4719 {
4720 /* When looking to complete "func", we find the upper bound of all
4721 symbols that start with "func" by looking for where we'd insert
4722 the closest string that would follow "func" in lexicographical
4723 order. Usually, that's "func"-with-last-character-incremented,
4724 i.e. "fund". Mind non-ASCII characters, though. Usually those
4725 will be UTF-8 multi-byte sequences, but we can't be certain.
4726 Especially mind the 0xff character, which is a valid character in
4727 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4728 rule out compilers allowing it in identifiers. Note that
4729 conveniently, strcmp/strcasecmp are specified to compare
4730 characters interpreted as unsigned char. So what we do is treat
4731 the whole string as a base 256 number composed of a sequence of
4732 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4733 to 0, and carries 1 to the following more-significant position.
4734 If the very first character in SEARCH_NAME ends up incremented
4735 and carries/overflows, then the upper bound is the end of the
4736 list. The string after the empty string is also the empty
4737 string.
4738
4739 Some examples of this operation:
4740
4741 SEARCH_NAME => "+1" RESULT
4742
4743 "abc" => "abd"
4744 "ab\xff" => "ac"
4745 "\xff" "a" "\xff" => "\xff" "b"
4746 "\xff" => ""
4747 "\xff\xff" => ""
4748 "" => ""
4749
4750 Then, with these symbols for example:
4751
4752 func
4753 func1
4754 fund
4755
4756 completing "func" looks for symbols between "func" and
4757 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4758 which finds "func" and "func1", but not "fund".
4759
4760 And with:
4761
4762 funcÿ (Latin1 'ÿ' [0xff])
4763 funcÿ1
4764 fund
4765
4766 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4767 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4768
4769 And with:
4770
4771 ÿÿ (Latin1 'ÿ' [0xff])
4772 ÿÿ1
4773
4774 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4775 the end of the list.
4776 */
4777 std::string after = search_name;
4778 while (!after.empty () && (unsigned char) after.back () == 0xff)
4779 after.pop_back ();
4780 if (!after.empty ())
4781 after.back () = (unsigned char) after.back () + 1;
4782 return after;
4783 }
4784
4785 /* See declaration. */
4786
4787 std::pair<std::vector<name_component>::const_iterator,
4788 std::vector<name_component>::const_iterator>
4789 mapped_index_base::find_name_components_bounds
4790 (const lookup_name_info &lookup_name_without_params) const
4791 {
4792 auto *name_cmp
4793 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4794
4795 const char *cplus
4796 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4797
4798 /* Comparison function object for lower_bound that matches against a
4799 given symbol name. */
4800 auto lookup_compare_lower = [&] (const name_component &elem,
4801 const char *name)
4802 {
4803 const char *elem_qualified = this->symbol_name_at (elem.idx);
4804 const char *elem_name = elem_qualified + elem.name_offset;
4805 return name_cmp (elem_name, name) < 0;
4806 };
4807
4808 /* Comparison function object for upper_bound that matches against a
4809 given symbol name. */
4810 auto lookup_compare_upper = [&] (const char *name,
4811 const name_component &elem)
4812 {
4813 const char *elem_qualified = this->symbol_name_at (elem.idx);
4814 const char *elem_name = elem_qualified + elem.name_offset;
4815 return name_cmp (name, elem_name) < 0;
4816 };
4817
4818 auto begin = this->name_components.begin ();
4819 auto end = this->name_components.end ();
4820
4821 /* Find the lower bound. */
4822 auto lower = [&] ()
4823 {
4824 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4825 return begin;
4826 else
4827 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4828 } ();
4829
4830 /* Find the upper bound. */
4831 auto upper = [&] ()
4832 {
4833 if (lookup_name_without_params.completion_mode ())
4834 {
4835 /* In completion mode, we want UPPER to point past all
4836 symbols names that have the same prefix. I.e., with
4837 these symbols, and completing "func":
4838
4839 function << lower bound
4840 function1
4841 other_function << upper bound
4842
4843 We find the upper bound by looking for the insertion
4844 point of "func"-with-last-character-incremented,
4845 i.e. "fund". */
4846 std::string after = make_sort_after_prefix_name (cplus);
4847 if (after.empty ())
4848 return end;
4849 return std::lower_bound (lower, end, after.c_str (),
4850 lookup_compare_lower);
4851 }
4852 else
4853 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4854 } ();
4855
4856 return {lower, upper};
4857 }
4858
4859 /* See declaration. */
4860
4861 void
4862 mapped_index_base::build_name_components ()
4863 {
4864 if (!this->name_components.empty ())
4865 return;
4866
4867 this->name_components_casing = case_sensitivity;
4868 auto *name_cmp
4869 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4870
4871 /* The code below only knows how to break apart components of C++
4872 symbol names (and other languages that use '::' as
4873 namespace/module separator). If we add support for wild matching
4874 to some language that uses some other operator (E.g., Ada, Go and
4875 D use '.'), then we'll need to try splitting the symbol name
4876 according to that language too. Note that Ada does support wild
4877 matching, but doesn't currently support .gdb_index. */
4878 auto count = this->symbol_name_count ();
4879 for (offset_type idx = 0; idx < count; idx++)
4880 {
4881 if (this->symbol_name_slot_invalid (idx))
4882 continue;
4883
4884 const char *name = this->symbol_name_at (idx);
4885
4886 /* Add each name component to the name component table. */
4887 unsigned int previous_len = 0;
4888 for (unsigned int current_len = cp_find_first_component (name);
4889 name[current_len] != '\0';
4890 current_len += cp_find_first_component (name + current_len))
4891 {
4892 gdb_assert (name[current_len] == ':');
4893 this->name_components.push_back ({previous_len, idx});
4894 /* Skip the '::'. */
4895 current_len += 2;
4896 previous_len = current_len;
4897 }
4898 this->name_components.push_back ({previous_len, idx});
4899 }
4900
4901 /* Sort name_components elements by name. */
4902 auto name_comp_compare = [&] (const name_component &left,
4903 const name_component &right)
4904 {
4905 const char *left_qualified = this->symbol_name_at (left.idx);
4906 const char *right_qualified = this->symbol_name_at (right.idx);
4907
4908 const char *left_name = left_qualified + left.name_offset;
4909 const char *right_name = right_qualified + right.name_offset;
4910
4911 return name_cmp (left_name, right_name) < 0;
4912 };
4913
4914 std::sort (this->name_components.begin (),
4915 this->name_components.end (),
4916 name_comp_compare);
4917 }
4918
4919 /* Helper for dw2_expand_symtabs_matching that works with a
4920 mapped_index_base instead of the containing objfile. This is split
4921 to a separate function in order to be able to unit test the
4922 name_components matching using a mock mapped_index_base. For each
4923 symbol name that matches, calls MATCH_CALLBACK, passing it the
4924 symbol's index in the mapped_index_base symbol table. */
4925
4926 static void
4927 dw2_expand_symtabs_matching_symbol
4928 (mapped_index_base &index,
4929 const lookup_name_info &lookup_name_in,
4930 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4931 enum search_domain kind,
4932 gdb::function_view<void (offset_type)> match_callback)
4933 {
4934 lookup_name_info lookup_name_without_params
4935 = lookup_name_in.make_ignore_params ();
4936 gdb_index_symbol_name_matcher lookup_name_matcher
4937 (lookup_name_without_params);
4938
4939 /* Build the symbol name component sorted vector, if we haven't
4940 yet. */
4941 index.build_name_components ();
4942
4943 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4944
4945 /* Now for each symbol name in range, check to see if we have a name
4946 match, and if so, call the MATCH_CALLBACK callback. */
4947
4948 /* The same symbol may appear more than once in the range though.
4949 E.g., if we're looking for symbols that complete "w", and we have
4950 a symbol named "w1::w2", we'll find the two name components for
4951 that same symbol in the range. To be sure we only call the
4952 callback once per symbol, we first collect the symbol name
4953 indexes that matched in a temporary vector and ignore
4954 duplicates. */
4955 std::vector<offset_type> matches;
4956 matches.reserve (std::distance (bounds.first, bounds.second));
4957
4958 for (; bounds.first != bounds.second; ++bounds.first)
4959 {
4960 const char *qualified = index.symbol_name_at (bounds.first->idx);
4961
4962 if (!lookup_name_matcher.matches (qualified)
4963 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4964 continue;
4965
4966 matches.push_back (bounds.first->idx);
4967 }
4968
4969 std::sort (matches.begin (), matches.end ());
4970
4971 /* Finally call the callback, once per match. */
4972 ULONGEST prev = -1;
4973 for (offset_type idx : matches)
4974 {
4975 if (prev != idx)
4976 {
4977 match_callback (idx);
4978 prev = idx;
4979 }
4980 }
4981
4982 /* Above we use a type wider than idx's for 'prev', since 0 and
4983 (offset_type)-1 are both possible values. */
4984 static_assert (sizeof (prev) > sizeof (offset_type), "");
4985 }
4986
4987 #if GDB_SELF_TEST
4988
4989 namespace selftests { namespace dw2_expand_symtabs_matching {
4990
4991 /* A mock .gdb_index/.debug_names-like name index table, enough to
4992 exercise dw2_expand_symtabs_matching_symbol, which works with the
4993 mapped_index_base interface. Builds an index from the symbol list
4994 passed as parameter to the constructor. */
4995 class mock_mapped_index : public mapped_index_base
4996 {
4997 public:
4998 mock_mapped_index (gdb::array_view<const char *> symbols)
4999 : m_symbol_table (symbols)
5000 {}
5001
5002 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
5003
5004 /* Return the number of names in the symbol table. */
5005 virtual size_t symbol_name_count () const
5006 {
5007 return m_symbol_table.size ();
5008 }
5009
5010 /* Get the name of the symbol at IDX in the symbol table. */
5011 virtual const char *symbol_name_at (offset_type idx) const
5012 {
5013 return m_symbol_table[idx];
5014 }
5015
5016 private:
5017 gdb::array_view<const char *> m_symbol_table;
5018 };
5019
5020 /* Convenience function that converts a NULL pointer to a "<null>"
5021 string, to pass to print routines. */
5022
5023 static const char *
5024 string_or_null (const char *str)
5025 {
5026 return str != NULL ? str : "<null>";
5027 }
5028
5029 /* Check if a lookup_name_info built from
5030 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
5031 index. EXPECTED_LIST is the list of expected matches, in expected
5032 matching order. If no match expected, then an empty list is
5033 specified. Returns true on success. On failure prints a warning
5034 indicating the file:line that failed, and returns false. */
5035
5036 static bool
5037 check_match (const char *file, int line,
5038 mock_mapped_index &mock_index,
5039 const char *name, symbol_name_match_type match_type,
5040 bool completion_mode,
5041 std::initializer_list<const char *> expected_list)
5042 {
5043 lookup_name_info lookup_name (name, match_type, completion_mode);
5044
5045 bool matched = true;
5046
5047 auto mismatch = [&] (const char *expected_str,
5048 const char *got)
5049 {
5050 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
5051 "expected=\"%s\", got=\"%s\"\n"),
5052 file, line,
5053 (match_type == symbol_name_match_type::FULL
5054 ? "FULL" : "WILD"),
5055 name, string_or_null (expected_str), string_or_null (got));
5056 matched = false;
5057 };
5058
5059 auto expected_it = expected_list.begin ();
5060 auto expected_end = expected_list.end ();
5061
5062 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
5063 NULL, ALL_DOMAIN,
5064 [&] (offset_type idx)
5065 {
5066 const char *matched_name = mock_index.symbol_name_at (idx);
5067 const char *expected_str
5068 = expected_it == expected_end ? NULL : *expected_it++;
5069
5070 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
5071 mismatch (expected_str, matched_name);
5072 });
5073
5074 const char *expected_str
5075 = expected_it == expected_end ? NULL : *expected_it++;
5076 if (expected_str != NULL)
5077 mismatch (expected_str, NULL);
5078
5079 return matched;
5080 }
5081
5082 /* The symbols added to the mock mapped_index for testing (in
5083 canonical form). */
5084 static const char *test_symbols[] = {
5085 "function",
5086 "std::bar",
5087 "std::zfunction",
5088 "std::zfunction2",
5089 "w1::w2",
5090 "ns::foo<char*>",
5091 "ns::foo<int>",
5092 "ns::foo<long>",
5093 "ns2::tmpl<int>::foo2",
5094 "(anonymous namespace)::A::B::C",
5095
5096 /* These are used to check that the increment-last-char in the
5097 matching algorithm for completion doesn't match "t1_fund" when
5098 completing "t1_func". */
5099 "t1_func",
5100 "t1_func1",
5101 "t1_fund",
5102 "t1_fund1",
5103
5104 /* A UTF-8 name with multi-byte sequences to make sure that
5105 cp-name-parser understands this as a single identifier ("função"
5106 is "function" in PT). */
5107 u8"u8função",
5108
5109 /* \377 (0xff) is Latin1 'ÿ'. */
5110 "yfunc\377",
5111
5112 /* \377 (0xff) is Latin1 'ÿ'. */
5113 "\377",
5114 "\377\377123",
5115
5116 /* A name with all sorts of complications. Starts with "z" to make
5117 it easier for the completion tests below. */
5118 #define Z_SYM_NAME \
5119 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
5120 "::tuple<(anonymous namespace)::ui*, " \
5121 "std::default_delete<(anonymous namespace)::ui>, void>"
5122
5123 Z_SYM_NAME
5124 };
5125
5126 /* Returns true if the mapped_index_base::find_name_component_bounds
5127 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
5128 in completion mode. */
5129
5130 static bool
5131 check_find_bounds_finds (mapped_index_base &index,
5132 const char *search_name,
5133 gdb::array_view<const char *> expected_syms)
5134 {
5135 lookup_name_info lookup_name (search_name,
5136 symbol_name_match_type::FULL, true);
5137
5138 auto bounds = index.find_name_components_bounds (lookup_name);
5139
5140 size_t distance = std::distance (bounds.first, bounds.second);
5141 if (distance != expected_syms.size ())
5142 return false;
5143
5144 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
5145 {
5146 auto nc_elem = bounds.first + exp_elem;
5147 const char *qualified = index.symbol_name_at (nc_elem->idx);
5148 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
5149 return false;
5150 }
5151
5152 return true;
5153 }
5154
5155 /* Test the lower-level mapped_index::find_name_component_bounds
5156 method. */
5157
5158 static void
5159 test_mapped_index_find_name_component_bounds ()
5160 {
5161 mock_mapped_index mock_index (test_symbols);
5162
5163 mock_index.build_name_components ();
5164
5165 /* Test the lower-level mapped_index::find_name_component_bounds
5166 method in completion mode. */
5167 {
5168 static const char *expected_syms[] = {
5169 "t1_func",
5170 "t1_func1",
5171 };
5172
5173 SELF_CHECK (check_find_bounds_finds (mock_index,
5174 "t1_func", expected_syms));
5175 }
5176
5177 /* Check that the increment-last-char in the name matching algorithm
5178 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5179 {
5180 static const char *expected_syms1[] = {
5181 "\377",
5182 "\377\377123",
5183 };
5184 SELF_CHECK (check_find_bounds_finds (mock_index,
5185 "\377", expected_syms1));
5186
5187 static const char *expected_syms2[] = {
5188 "\377\377123",
5189 };
5190 SELF_CHECK (check_find_bounds_finds (mock_index,
5191 "\377\377", expected_syms2));
5192 }
5193 }
5194
5195 /* Test dw2_expand_symtabs_matching_symbol. */
5196
5197 static void
5198 test_dw2_expand_symtabs_matching_symbol ()
5199 {
5200 mock_mapped_index mock_index (test_symbols);
5201
5202 /* We let all tests run until the end even if some fails, for debug
5203 convenience. */
5204 bool any_mismatch = false;
5205
5206 /* Create the expected symbols list (an initializer_list). Needed
5207 because lists have commas, and we need to pass them to CHECK,
5208 which is a macro. */
5209 #define EXPECT(...) { __VA_ARGS__ }
5210
5211 /* Wrapper for check_match that passes down the current
5212 __FILE__/__LINE__. */
5213 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5214 any_mismatch |= !check_match (__FILE__, __LINE__, \
5215 mock_index, \
5216 NAME, MATCH_TYPE, COMPLETION_MODE, \
5217 EXPECTED_LIST)
5218
5219 /* Identity checks. */
5220 for (const char *sym : test_symbols)
5221 {
5222 /* Should be able to match all existing symbols. */
5223 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5224 EXPECT (sym));
5225
5226 /* Should be able to match all existing symbols with
5227 parameters. */
5228 std::string with_params = std::string (sym) + "(int)";
5229 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5230 EXPECT (sym));
5231
5232 /* Should be able to match all existing symbols with
5233 parameters and qualifiers. */
5234 with_params = std::string (sym) + " ( int ) const";
5235 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5236 EXPECT (sym));
5237
5238 /* This should really find sym, but cp-name-parser.y doesn't
5239 know about lvalue/rvalue qualifiers yet. */
5240 with_params = std::string (sym) + " ( int ) &&";
5241 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5242 {});
5243 }
5244
5245 /* Check that the name matching algorithm for completion doesn't get
5246 confused with Latin1 'ÿ' / 0xff. */
5247 {
5248 static const char str[] = "\377";
5249 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5250 EXPECT ("\377", "\377\377123"));
5251 }
5252
5253 /* Check that the increment-last-char in the matching algorithm for
5254 completion doesn't match "t1_fund" when completing "t1_func". */
5255 {
5256 static const char str[] = "t1_func";
5257 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5258 EXPECT ("t1_func", "t1_func1"));
5259 }
5260
5261 /* Check that completion mode works at each prefix of the expected
5262 symbol name. */
5263 {
5264 static const char str[] = "function(int)";
5265 size_t len = strlen (str);
5266 std::string lookup;
5267
5268 for (size_t i = 1; i < len; i++)
5269 {
5270 lookup.assign (str, i);
5271 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5272 EXPECT ("function"));
5273 }
5274 }
5275
5276 /* While "w" is a prefix of both components, the match function
5277 should still only be called once. */
5278 {
5279 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5280 EXPECT ("w1::w2"));
5281 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5282 EXPECT ("w1::w2"));
5283 }
5284
5285 /* Same, with a "complicated" symbol. */
5286 {
5287 static const char str[] = Z_SYM_NAME;
5288 size_t len = strlen (str);
5289 std::string lookup;
5290
5291 for (size_t i = 1; i < len; i++)
5292 {
5293 lookup.assign (str, i);
5294 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5295 EXPECT (Z_SYM_NAME));
5296 }
5297 }
5298
5299 /* In FULL mode, an incomplete symbol doesn't match. */
5300 {
5301 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5302 {});
5303 }
5304
5305 /* A complete symbol with parameters matches any overload, since the
5306 index has no overload info. */
5307 {
5308 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5309 EXPECT ("std::zfunction", "std::zfunction2"));
5310 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5311 EXPECT ("std::zfunction", "std::zfunction2"));
5312 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5313 EXPECT ("std::zfunction", "std::zfunction2"));
5314 }
5315
5316 /* Check that whitespace is ignored appropriately. A symbol with a
5317 template argument list. */
5318 {
5319 static const char expected[] = "ns::foo<int>";
5320 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5321 EXPECT (expected));
5322 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5323 EXPECT (expected));
5324 }
5325
5326 /* Check that whitespace is ignored appropriately. A symbol with a
5327 template argument list that includes a pointer. */
5328 {
5329 static const char expected[] = "ns::foo<char*>";
5330 /* Try both completion and non-completion modes. */
5331 static const bool completion_mode[2] = {false, true};
5332 for (size_t i = 0; i < 2; i++)
5333 {
5334 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5335 completion_mode[i], EXPECT (expected));
5336 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5337 completion_mode[i], EXPECT (expected));
5338
5339 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5340 completion_mode[i], EXPECT (expected));
5341 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5342 completion_mode[i], EXPECT (expected));
5343 }
5344 }
5345
5346 {
5347 /* Check method qualifiers are ignored. */
5348 static const char expected[] = "ns::foo<char*>";
5349 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5350 symbol_name_match_type::FULL, true, EXPECT (expected));
5351 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5352 symbol_name_match_type::FULL, true, EXPECT (expected));
5353 CHECK_MATCH ("foo < char * > ( int ) const",
5354 symbol_name_match_type::WILD, true, EXPECT (expected));
5355 CHECK_MATCH ("foo < char * > ( int ) &&",
5356 symbol_name_match_type::WILD, true, EXPECT (expected));
5357 }
5358
5359 /* Test lookup names that don't match anything. */
5360 {
5361 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5362 {});
5363
5364 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5365 {});
5366 }
5367
5368 /* Some wild matching tests, exercising "(anonymous namespace)",
5369 which should not be confused with a parameter list. */
5370 {
5371 static const char *syms[] = {
5372 "A::B::C",
5373 "B::C",
5374 "C",
5375 "A :: B :: C ( int )",
5376 "B :: C ( int )",
5377 "C ( int )",
5378 };
5379
5380 for (const char *s : syms)
5381 {
5382 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5383 EXPECT ("(anonymous namespace)::A::B::C"));
5384 }
5385 }
5386
5387 {
5388 static const char expected[] = "ns2::tmpl<int>::foo2";
5389 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5390 EXPECT (expected));
5391 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5392 EXPECT (expected));
5393 }
5394
5395 SELF_CHECK (!any_mismatch);
5396
5397 #undef EXPECT
5398 #undef CHECK_MATCH
5399 }
5400
5401 static void
5402 run_test ()
5403 {
5404 test_mapped_index_find_name_component_bounds ();
5405 test_dw2_expand_symtabs_matching_symbol ();
5406 }
5407
5408 }} // namespace selftests::dw2_expand_symtabs_matching
5409
5410 #endif /* GDB_SELF_TEST */
5411
5412 /* If FILE_MATCHER is NULL or if PER_CU has
5413 dwarf2_per_cu_quick_data::MARK set (see
5414 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5415 EXPANSION_NOTIFY on it. */
5416
5417 static void
5418 dw2_expand_symtabs_matching_one
5419 (struct dwarf2_per_cu_data *per_cu,
5420 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5421 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5422 {
5423 if (file_matcher == NULL || per_cu->v.quick->mark)
5424 {
5425 bool symtab_was_null
5426 = (per_cu->v.quick->compunit_symtab == NULL);
5427
5428 dw2_instantiate_symtab (per_cu);
5429
5430 if (expansion_notify != NULL
5431 && symtab_was_null
5432 && per_cu->v.quick->compunit_symtab != NULL)
5433 expansion_notify (per_cu->v.quick->compunit_symtab);
5434 }
5435 }
5436
5437 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5438 matched, to expand corresponding CUs that were marked. IDX is the
5439 index of the symbol name that matched. */
5440
5441 static void
5442 dw2_expand_marked_cus
5443 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5444 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5445 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5446 search_domain kind)
5447 {
5448 offset_type *vec, vec_len, vec_idx;
5449 bool global_seen = false;
5450 mapped_index &index = *dwarf2_per_objfile->index_table;
5451
5452 vec = (offset_type *) (index.constant_pool
5453 + MAYBE_SWAP (index.symbol_table[idx].vec));
5454 vec_len = MAYBE_SWAP (vec[0]);
5455 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5456 {
5457 struct dwarf2_per_cu_data *per_cu;
5458 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5459 /* This value is only valid for index versions >= 7. */
5460 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5461 gdb_index_symbol_kind symbol_kind =
5462 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5463 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5464 /* Only check the symbol attributes if they're present.
5465 Indices prior to version 7 don't record them,
5466 and indices >= 7 may elide them for certain symbols
5467 (gold does this). */
5468 int attrs_valid =
5469 (index.version >= 7
5470 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5471
5472 /* Work around gold/15646. */
5473 if (attrs_valid)
5474 {
5475 if (!is_static && global_seen)
5476 continue;
5477 if (!is_static)
5478 global_seen = true;
5479 }
5480
5481 /* Only check the symbol's kind if it has one. */
5482 if (attrs_valid)
5483 {
5484 switch (kind)
5485 {
5486 case VARIABLES_DOMAIN:
5487 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5488 continue;
5489 break;
5490 case FUNCTIONS_DOMAIN:
5491 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5492 continue;
5493 break;
5494 case TYPES_DOMAIN:
5495 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5496 continue;
5497 break;
5498 default:
5499 break;
5500 }
5501 }
5502
5503 /* Don't crash on bad data. */
5504 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5505 + dwarf2_per_objfile->n_type_units))
5506 {
5507 complaint (&symfile_complaints,
5508 _(".gdb_index entry has bad CU index"
5509 " [in module %s]"),
5510 objfile_name (dwarf2_per_objfile->objfile));
5511 continue;
5512 }
5513
5514 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
5515 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5516 expansion_notify);
5517 }
5518 }
5519
5520 /* If FILE_MATCHER is non-NULL, set all the
5521 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5522 that match FILE_MATCHER. */
5523
5524 static void
5525 dw_expand_symtabs_matching_file_matcher
5526 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5527 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5528 {
5529 if (file_matcher == NULL)
5530 return;
5531
5532 objfile *const objfile = dwarf2_per_objfile->objfile;
5533
5534 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5535 htab_eq_pointer,
5536 NULL, xcalloc, xfree));
5537 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5538 htab_eq_pointer,
5539 NULL, xcalloc, xfree));
5540
5541 /* The rule is CUs specify all the files, including those used by
5542 any TU, so there's no need to scan TUs here. */
5543
5544 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5545 {
5546 int j;
5547 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5548 struct quick_file_names *file_data;
5549 void **slot;
5550
5551 QUIT;
5552
5553 per_cu->v.quick->mark = 0;
5554
5555 /* We only need to look at symtabs not already expanded. */
5556 if (per_cu->v.quick->compunit_symtab)
5557 continue;
5558
5559 file_data = dw2_get_file_names (per_cu);
5560 if (file_data == NULL)
5561 continue;
5562
5563 if (htab_find (visited_not_found.get (), file_data) != NULL)
5564 continue;
5565 else if (htab_find (visited_found.get (), file_data) != NULL)
5566 {
5567 per_cu->v.quick->mark = 1;
5568 continue;
5569 }
5570
5571 for (j = 0; j < file_data->num_file_names; ++j)
5572 {
5573 const char *this_real_name;
5574
5575 if (file_matcher (file_data->file_names[j], false))
5576 {
5577 per_cu->v.quick->mark = 1;
5578 break;
5579 }
5580
5581 /* Before we invoke realpath, which can get expensive when many
5582 files are involved, do a quick comparison of the basenames. */
5583 if (!basenames_may_differ
5584 && !file_matcher (lbasename (file_data->file_names[j]),
5585 true))
5586 continue;
5587
5588 this_real_name = dw2_get_real_path (objfile, file_data, j);
5589 if (file_matcher (this_real_name, false))
5590 {
5591 per_cu->v.quick->mark = 1;
5592 break;
5593 }
5594 }
5595
5596 slot = htab_find_slot (per_cu->v.quick->mark
5597 ? visited_found.get ()
5598 : visited_not_found.get (),
5599 file_data, INSERT);
5600 *slot = file_data;
5601 }
5602 }
5603
5604 static void
5605 dw2_expand_symtabs_matching
5606 (struct objfile *objfile,
5607 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5608 const lookup_name_info &lookup_name,
5609 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5610 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5611 enum search_domain kind)
5612 {
5613 struct dwarf2_per_objfile *dwarf2_per_objfile
5614 = get_dwarf2_per_objfile (objfile);
5615
5616 /* index_table is NULL if OBJF_READNOW. */
5617 if (!dwarf2_per_objfile->index_table)
5618 return;
5619
5620 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5621
5622 mapped_index &index = *dwarf2_per_objfile->index_table;
5623
5624 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5625 symbol_matcher,
5626 kind, [&] (offset_type idx)
5627 {
5628 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5629 expansion_notify, kind);
5630 });
5631 }
5632
5633 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5634 symtab. */
5635
5636 static struct compunit_symtab *
5637 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5638 CORE_ADDR pc)
5639 {
5640 int i;
5641
5642 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5643 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5644 return cust;
5645
5646 if (cust->includes == NULL)
5647 return NULL;
5648
5649 for (i = 0; cust->includes[i]; ++i)
5650 {
5651 struct compunit_symtab *s = cust->includes[i];
5652
5653 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5654 if (s != NULL)
5655 return s;
5656 }
5657
5658 return NULL;
5659 }
5660
5661 static struct compunit_symtab *
5662 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5663 struct bound_minimal_symbol msymbol,
5664 CORE_ADDR pc,
5665 struct obj_section *section,
5666 int warn_if_readin)
5667 {
5668 struct dwarf2_per_cu_data *data;
5669 struct compunit_symtab *result;
5670
5671 if (!objfile->psymtabs_addrmap)
5672 return NULL;
5673
5674 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5675 pc);
5676 if (!data)
5677 return NULL;
5678
5679 if (warn_if_readin && data->v.quick->compunit_symtab)
5680 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5681 paddress (get_objfile_arch (objfile), pc));
5682
5683 result
5684 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5685 pc);
5686 gdb_assert (result != NULL);
5687 return result;
5688 }
5689
5690 static void
5691 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5692 void *data, int need_fullname)
5693 {
5694 struct dwarf2_per_objfile *dwarf2_per_objfile
5695 = get_dwarf2_per_objfile (objfile);
5696
5697 if (!dwarf2_per_objfile->filenames_cache)
5698 {
5699 dwarf2_per_objfile->filenames_cache.emplace ();
5700
5701 htab_up visited (htab_create_alloc (10,
5702 htab_hash_pointer, htab_eq_pointer,
5703 NULL, xcalloc, xfree));
5704
5705 /* The rule is CUs specify all the files, including those used
5706 by any TU, so there's no need to scan TUs here. We can
5707 ignore file names coming from already-expanded CUs. */
5708
5709 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5710 {
5711 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
5712
5713 if (per_cu->v.quick->compunit_symtab)
5714 {
5715 void **slot = htab_find_slot (visited.get (),
5716 per_cu->v.quick->file_names,
5717 INSERT);
5718
5719 *slot = per_cu->v.quick->file_names;
5720 }
5721 }
5722
5723 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5724 {
5725 dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5726 struct quick_file_names *file_data;
5727 void **slot;
5728
5729 /* We only need to look at symtabs not already expanded. */
5730 if (per_cu->v.quick->compunit_symtab)
5731 continue;
5732
5733 file_data = dw2_get_file_names (per_cu);
5734 if (file_data == NULL)
5735 continue;
5736
5737 slot = htab_find_slot (visited.get (), file_data, INSERT);
5738 if (*slot)
5739 {
5740 /* Already visited. */
5741 continue;
5742 }
5743 *slot = file_data;
5744
5745 for (int j = 0; j < file_data->num_file_names; ++j)
5746 {
5747 const char *filename = file_data->file_names[j];
5748 dwarf2_per_objfile->filenames_cache->seen (filename);
5749 }
5750 }
5751 }
5752
5753 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5754 {
5755 gdb::unique_xmalloc_ptr<char> this_real_name;
5756
5757 if (need_fullname)
5758 this_real_name = gdb_realpath (filename);
5759 (*fun) (filename, this_real_name.get (), data);
5760 });
5761 }
5762
5763 static int
5764 dw2_has_symbols (struct objfile *objfile)
5765 {
5766 return 1;
5767 }
5768
5769 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5770 {
5771 dw2_has_symbols,
5772 dw2_find_last_source_symtab,
5773 dw2_forget_cached_source_info,
5774 dw2_map_symtabs_matching_filename,
5775 dw2_lookup_symbol,
5776 dw2_print_stats,
5777 dw2_dump,
5778 dw2_relocate,
5779 dw2_expand_symtabs_for_function,
5780 dw2_expand_all_symtabs,
5781 dw2_expand_symtabs_with_fullname,
5782 dw2_map_matching_symbols,
5783 dw2_expand_symtabs_matching,
5784 dw2_find_pc_sect_compunit_symtab,
5785 NULL,
5786 dw2_map_symbol_filenames
5787 };
5788
5789 /* DWARF-5 debug_names reader. */
5790
5791 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5792 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5793
5794 /* A helper function that reads the .debug_names section in SECTION
5795 and fills in MAP. FILENAME is the name of the file containing the
5796 section; it is used for error reporting.
5797
5798 Returns true if all went well, false otherwise. */
5799
5800 static bool
5801 read_debug_names_from_section (struct objfile *objfile,
5802 const char *filename,
5803 struct dwarf2_section_info *section,
5804 mapped_debug_names &map)
5805 {
5806 if (dwarf2_section_empty_p (section))
5807 return false;
5808
5809 /* Older elfutils strip versions could keep the section in the main
5810 executable while splitting it for the separate debug info file. */
5811 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5812 return false;
5813
5814 dwarf2_read_section (objfile, section);
5815
5816 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5817
5818 const gdb_byte *addr = section->buffer;
5819
5820 bfd *const abfd = get_section_bfd_owner (section);
5821
5822 unsigned int bytes_read;
5823 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5824 addr += bytes_read;
5825
5826 map.dwarf5_is_dwarf64 = bytes_read != 4;
5827 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5828 if (bytes_read + length != section->size)
5829 {
5830 /* There may be multiple per-CU indices. */
5831 warning (_("Section .debug_names in %s length %s does not match "
5832 "section length %s, ignoring .debug_names."),
5833 filename, plongest (bytes_read + length),
5834 pulongest (section->size));
5835 return false;
5836 }
5837
5838 /* The version number. */
5839 uint16_t version = read_2_bytes (abfd, addr);
5840 addr += 2;
5841 if (version != 5)
5842 {
5843 warning (_("Section .debug_names in %s has unsupported version %d, "
5844 "ignoring .debug_names."),
5845 filename, version);
5846 return false;
5847 }
5848
5849 /* Padding. */
5850 uint16_t padding = read_2_bytes (abfd, addr);
5851 addr += 2;
5852 if (padding != 0)
5853 {
5854 warning (_("Section .debug_names in %s has unsupported padding %d, "
5855 "ignoring .debug_names."),
5856 filename, padding);
5857 return false;
5858 }
5859
5860 /* comp_unit_count - The number of CUs in the CU list. */
5861 map.cu_count = read_4_bytes (abfd, addr);
5862 addr += 4;
5863
5864 /* local_type_unit_count - The number of TUs in the local TU
5865 list. */
5866 map.tu_count = read_4_bytes (abfd, addr);
5867 addr += 4;
5868
5869 /* foreign_type_unit_count - The number of TUs in the foreign TU
5870 list. */
5871 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5872 addr += 4;
5873 if (foreign_tu_count != 0)
5874 {
5875 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5876 "ignoring .debug_names."),
5877 filename, static_cast<unsigned long> (foreign_tu_count));
5878 return false;
5879 }
5880
5881 /* bucket_count - The number of hash buckets in the hash lookup
5882 table. */
5883 map.bucket_count = read_4_bytes (abfd, addr);
5884 addr += 4;
5885
5886 /* name_count - The number of unique names in the index. */
5887 map.name_count = read_4_bytes (abfd, addr);
5888 addr += 4;
5889
5890 /* abbrev_table_size - The size in bytes of the abbreviations
5891 table. */
5892 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5893 addr += 4;
5894
5895 /* augmentation_string_size - The size in bytes of the augmentation
5896 string. This value is rounded up to a multiple of 4. */
5897 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5898 addr += 4;
5899 map.augmentation_is_gdb = ((augmentation_string_size
5900 == sizeof (dwarf5_augmentation))
5901 && memcmp (addr, dwarf5_augmentation,
5902 sizeof (dwarf5_augmentation)) == 0);
5903 augmentation_string_size += (-augmentation_string_size) & 3;
5904 addr += augmentation_string_size;
5905
5906 /* List of CUs */
5907 map.cu_table_reordered = addr;
5908 addr += map.cu_count * map.offset_size;
5909
5910 /* List of Local TUs */
5911 map.tu_table_reordered = addr;
5912 addr += map.tu_count * map.offset_size;
5913
5914 /* Hash Lookup Table */
5915 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5916 addr += map.bucket_count * 4;
5917 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5918 addr += map.name_count * 4;
5919
5920 /* Name Table */
5921 map.name_table_string_offs_reordered = addr;
5922 addr += map.name_count * map.offset_size;
5923 map.name_table_entry_offs_reordered = addr;
5924 addr += map.name_count * map.offset_size;
5925
5926 const gdb_byte *abbrev_table_start = addr;
5927 for (;;)
5928 {
5929 unsigned int bytes_read;
5930 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5931 addr += bytes_read;
5932 if (index_num == 0)
5933 break;
5934
5935 const auto insertpair
5936 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5937 if (!insertpair.second)
5938 {
5939 warning (_("Section .debug_names in %s has duplicate index %s, "
5940 "ignoring .debug_names."),
5941 filename, pulongest (index_num));
5942 return false;
5943 }
5944 mapped_debug_names::index_val &indexval = insertpair.first->second;
5945 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5946 addr += bytes_read;
5947
5948 for (;;)
5949 {
5950 mapped_debug_names::index_val::attr attr;
5951 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5952 addr += bytes_read;
5953 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5954 addr += bytes_read;
5955 if (attr.form == DW_FORM_implicit_const)
5956 {
5957 attr.implicit_const = read_signed_leb128 (abfd, addr,
5958 &bytes_read);
5959 addr += bytes_read;
5960 }
5961 if (attr.dw_idx == 0 && attr.form == 0)
5962 break;
5963 indexval.attr_vec.push_back (std::move (attr));
5964 }
5965 }
5966 if (addr != abbrev_table_start + abbrev_table_size)
5967 {
5968 warning (_("Section .debug_names in %s has abbreviation_table "
5969 "of size %zu vs. written as %u, ignoring .debug_names."),
5970 filename, addr - abbrev_table_start, abbrev_table_size);
5971 return false;
5972 }
5973 map.entry_pool = addr;
5974
5975 return true;
5976 }
5977
5978 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5979 list. */
5980
5981 static void
5982 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5983 const mapped_debug_names &map,
5984 dwarf2_section_info &section,
5985 bool is_dwz, int base_offset)
5986 {
5987 sect_offset sect_off_prev;
5988 for (uint32_t i = 0; i <= map.cu_count; ++i)
5989 {
5990 sect_offset sect_off_next;
5991 if (i < map.cu_count)
5992 {
5993 sect_off_next
5994 = (sect_offset) (extract_unsigned_integer
5995 (map.cu_table_reordered + i * map.offset_size,
5996 map.offset_size,
5997 map.dwarf5_byte_order));
5998 }
5999 else
6000 sect_off_next = (sect_offset) section.size;
6001 if (i >= 1)
6002 {
6003 const ULONGEST length = sect_off_next - sect_off_prev;
6004 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
6005 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
6006 sect_off_prev, length);
6007 }
6008 sect_off_prev = sect_off_next;
6009 }
6010 }
6011
6012 /* Read the CU list from the mapped index, and use it to create all
6013 the CU objects for this dwarf2_per_objfile. */
6014
6015 static void
6016 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
6017 const mapped_debug_names &map,
6018 const mapped_debug_names &dwz_map)
6019 {
6020 struct objfile *objfile = dwarf2_per_objfile->objfile;
6021
6022 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
6023 dwarf2_per_objfile->all_comp_units
6024 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
6025 dwarf2_per_objfile->n_comp_units);
6026
6027 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
6028 dwarf2_per_objfile->info,
6029 false /* is_dwz */,
6030 0 /* base_offset */);
6031
6032 if (dwz_map.cu_count == 0)
6033 return;
6034
6035 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
6036 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
6037 true /* is_dwz */,
6038 map.cu_count /* base_offset */);
6039 }
6040
6041 /* Read .debug_names. If everything went ok, initialize the "quick"
6042 elements of all the CUs and return true. Otherwise, return false. */
6043
6044 static bool
6045 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
6046 {
6047 mapped_debug_names local_map (dwarf2_per_objfile);
6048 mapped_debug_names dwz_map (dwarf2_per_objfile);
6049 struct objfile *objfile = dwarf2_per_objfile->objfile;
6050
6051 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
6052 &dwarf2_per_objfile->debug_names,
6053 local_map))
6054 return false;
6055
6056 /* Don't use the index if it's empty. */
6057 if (local_map.name_count == 0)
6058 return false;
6059
6060 /* If there is a .dwz file, read it so we can get its CU list as
6061 well. */
6062 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
6063 if (dwz != NULL)
6064 {
6065 if (!read_debug_names_from_section (objfile,
6066 bfd_get_filename (dwz->dwz_bfd),
6067 &dwz->debug_names, dwz_map))
6068 {
6069 warning (_("could not read '.debug_names' section from %s; skipping"),
6070 bfd_get_filename (dwz->dwz_bfd));
6071 return false;
6072 }
6073 }
6074
6075 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
6076
6077 if (local_map.tu_count != 0)
6078 {
6079 /* We can only handle a single .debug_types when we have an
6080 index. */
6081 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
6082 return false;
6083
6084 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
6085 dwarf2_per_objfile->types, 0);
6086
6087 create_signatured_type_table_from_debug_names
6088 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
6089 }
6090
6091 create_addrmap_from_aranges (dwarf2_per_objfile,
6092 &dwarf2_per_objfile->debug_aranges);
6093
6094 dwarf2_per_objfile->debug_names_table.reset
6095 (new mapped_debug_names (dwarf2_per_objfile));
6096 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
6097 dwarf2_per_objfile->using_index = 1;
6098 dwarf2_per_objfile->quick_file_names_table =
6099 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6100
6101 return true;
6102 }
6103
6104 /* Symbol name hashing function as specified by DWARF-5. */
6105
6106 static uint32_t
6107 dwarf5_djb_hash (const char *str_)
6108 {
6109 const unsigned char *str = (const unsigned char *) str_;
6110
6111 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
6112 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
6113
6114 uint32_t hash = 5381;
6115 while (int c = *str++)
6116 hash = hash * 33 + tolower (c);
6117 return hash;
6118 }
6119
6120 /* Type used to manage iterating over all CUs looking for a symbol for
6121 .debug_names. */
6122
6123 class dw2_debug_names_iterator
6124 {
6125 public:
6126 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
6127 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
6128 dw2_debug_names_iterator (const mapped_debug_names &map,
6129 bool want_specific_block,
6130 block_enum block_index, domain_enum domain,
6131 const char *name)
6132 : m_map (map), m_want_specific_block (want_specific_block),
6133 m_block_index (block_index), m_domain (domain),
6134 m_addr (find_vec_in_debug_names (map, name))
6135 {}
6136
6137 dw2_debug_names_iterator (const mapped_debug_names &map,
6138 search_domain search, uint32_t namei)
6139 : m_map (map),
6140 m_search (search),
6141 m_addr (find_vec_in_debug_names (map, namei))
6142 {}
6143
6144 /* Return the next matching CU or NULL if there are no more. */
6145 dwarf2_per_cu_data *next ();
6146
6147 private:
6148 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6149 const char *name);
6150 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6151 uint32_t namei);
6152
6153 /* The internalized form of .debug_names. */
6154 const mapped_debug_names &m_map;
6155
6156 /* If true, only look for symbols that match BLOCK_INDEX. */
6157 const bool m_want_specific_block = false;
6158
6159 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
6160 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
6161 value. */
6162 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
6163
6164 /* The kind of symbol we're looking for. */
6165 const domain_enum m_domain = UNDEF_DOMAIN;
6166 const search_domain m_search = ALL_DOMAIN;
6167
6168 /* The list of CUs from the index entry of the symbol, or NULL if
6169 not found. */
6170 const gdb_byte *m_addr;
6171 };
6172
6173 const char *
6174 mapped_debug_names::namei_to_name (uint32_t namei) const
6175 {
6176 const ULONGEST namei_string_offs
6177 = extract_unsigned_integer ((name_table_string_offs_reordered
6178 + namei * offset_size),
6179 offset_size,
6180 dwarf5_byte_order);
6181 return read_indirect_string_at_offset
6182 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
6183 }
6184
6185 /* Find a slot in .debug_names for the object named NAME. If NAME is
6186 found, return pointer to its pool data. If NAME cannot be found,
6187 return NULL. */
6188
6189 const gdb_byte *
6190 dw2_debug_names_iterator::find_vec_in_debug_names
6191 (const mapped_debug_names &map, const char *name)
6192 {
6193 int (*cmp) (const char *, const char *);
6194
6195 if (current_language->la_language == language_cplus
6196 || current_language->la_language == language_fortran
6197 || current_language->la_language == language_d)
6198 {
6199 /* NAME is already canonical. Drop any qualifiers as
6200 .debug_names does not contain any. */
6201
6202 if (strchr (name, '(') != NULL)
6203 {
6204 gdb::unique_xmalloc_ptr<char> without_params
6205 = cp_remove_params (name);
6206
6207 if (without_params != NULL)
6208 {
6209 name = without_params.get();
6210 }
6211 }
6212 }
6213
6214 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6215
6216 const uint32_t full_hash = dwarf5_djb_hash (name);
6217 uint32_t namei
6218 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6219 (map.bucket_table_reordered
6220 + (full_hash % map.bucket_count)), 4,
6221 map.dwarf5_byte_order);
6222 if (namei == 0)
6223 return NULL;
6224 --namei;
6225 if (namei >= map.name_count)
6226 {
6227 complaint (&symfile_complaints,
6228 _("Wrong .debug_names with name index %u but name_count=%u "
6229 "[in module %s]"),
6230 namei, map.name_count,
6231 objfile_name (map.dwarf2_per_objfile->objfile));
6232 return NULL;
6233 }
6234
6235 for (;;)
6236 {
6237 const uint32_t namei_full_hash
6238 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6239 (map.hash_table_reordered + namei), 4,
6240 map.dwarf5_byte_order);
6241 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6242 return NULL;
6243
6244 if (full_hash == namei_full_hash)
6245 {
6246 const char *const namei_string = map.namei_to_name (namei);
6247
6248 #if 0 /* An expensive sanity check. */
6249 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6250 {
6251 complaint (&symfile_complaints,
6252 _("Wrong .debug_names hash for string at index %u "
6253 "[in module %s]"),
6254 namei, objfile_name (dwarf2_per_objfile->objfile));
6255 return NULL;
6256 }
6257 #endif
6258
6259 if (cmp (namei_string, name) == 0)
6260 {
6261 const ULONGEST namei_entry_offs
6262 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6263 + namei * map.offset_size),
6264 map.offset_size, map.dwarf5_byte_order);
6265 return map.entry_pool + namei_entry_offs;
6266 }
6267 }
6268
6269 ++namei;
6270 if (namei >= map.name_count)
6271 return NULL;
6272 }
6273 }
6274
6275 const gdb_byte *
6276 dw2_debug_names_iterator::find_vec_in_debug_names
6277 (const mapped_debug_names &map, uint32_t namei)
6278 {
6279 if (namei >= map.name_count)
6280 {
6281 complaint (&symfile_complaints,
6282 _("Wrong .debug_names with name index %u but name_count=%u "
6283 "[in module %s]"),
6284 namei, map.name_count,
6285 objfile_name (map.dwarf2_per_objfile->objfile));
6286 return NULL;
6287 }
6288
6289 const ULONGEST namei_entry_offs
6290 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6291 + namei * map.offset_size),
6292 map.offset_size, map.dwarf5_byte_order);
6293 return map.entry_pool + namei_entry_offs;
6294 }
6295
6296 /* See dw2_debug_names_iterator. */
6297
6298 dwarf2_per_cu_data *
6299 dw2_debug_names_iterator::next ()
6300 {
6301 if (m_addr == NULL)
6302 return NULL;
6303
6304 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
6305 struct objfile *objfile = dwarf2_per_objfile->objfile;
6306 bfd *const abfd = objfile->obfd;
6307
6308 again:
6309
6310 unsigned int bytes_read;
6311 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6312 m_addr += bytes_read;
6313 if (abbrev == 0)
6314 return NULL;
6315
6316 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6317 if (indexval_it == m_map.abbrev_map.cend ())
6318 {
6319 complaint (&symfile_complaints,
6320 _("Wrong .debug_names undefined abbrev code %s "
6321 "[in module %s]"),
6322 pulongest (abbrev), objfile_name (objfile));
6323 return NULL;
6324 }
6325 const mapped_debug_names::index_val &indexval = indexval_it->second;
6326 bool have_is_static = false;
6327 bool is_static;
6328 dwarf2_per_cu_data *per_cu = NULL;
6329 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6330 {
6331 ULONGEST ull;
6332 switch (attr.form)
6333 {
6334 case DW_FORM_implicit_const:
6335 ull = attr.implicit_const;
6336 break;
6337 case DW_FORM_flag_present:
6338 ull = 1;
6339 break;
6340 case DW_FORM_udata:
6341 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6342 m_addr += bytes_read;
6343 break;
6344 default:
6345 complaint (&symfile_complaints,
6346 _("Unsupported .debug_names form %s [in module %s]"),
6347 dwarf_form_name (attr.form),
6348 objfile_name (objfile));
6349 return NULL;
6350 }
6351 switch (attr.dw_idx)
6352 {
6353 case DW_IDX_compile_unit:
6354 /* Don't crash on bad data. */
6355 if (ull >= dwarf2_per_objfile->n_comp_units)
6356 {
6357 complaint (&symfile_complaints,
6358 _(".debug_names entry has bad CU index %s"
6359 " [in module %s]"),
6360 pulongest (ull),
6361 objfile_name (dwarf2_per_objfile->objfile));
6362 continue;
6363 }
6364 per_cu = dw2_get_cutu (dwarf2_per_objfile, ull);
6365 break;
6366 case DW_IDX_type_unit:
6367 /* Don't crash on bad data. */
6368 if (ull >= dwarf2_per_objfile->n_type_units)
6369 {
6370 complaint (&symfile_complaints,
6371 _(".debug_names entry has bad TU index %s"
6372 " [in module %s]"),
6373 pulongest (ull),
6374 objfile_name (dwarf2_per_objfile->objfile));
6375 continue;
6376 }
6377 per_cu = dw2_get_cutu (dwarf2_per_objfile,
6378 dwarf2_per_objfile->n_comp_units + ull);
6379 break;
6380 case DW_IDX_GNU_internal:
6381 if (!m_map.augmentation_is_gdb)
6382 break;
6383 have_is_static = true;
6384 is_static = true;
6385 break;
6386 case DW_IDX_GNU_external:
6387 if (!m_map.augmentation_is_gdb)
6388 break;
6389 have_is_static = true;
6390 is_static = false;
6391 break;
6392 }
6393 }
6394
6395 /* Skip if already read in. */
6396 if (per_cu->v.quick->compunit_symtab)
6397 goto again;
6398
6399 /* Check static vs global. */
6400 if (have_is_static)
6401 {
6402 const bool want_static = m_block_index != GLOBAL_BLOCK;
6403 if (m_want_specific_block && want_static != is_static)
6404 goto again;
6405 }
6406
6407 /* Match dw2_symtab_iter_next, symbol_kind
6408 and debug_names::psymbol_tag. */
6409 switch (m_domain)
6410 {
6411 case VAR_DOMAIN:
6412 switch (indexval.dwarf_tag)
6413 {
6414 case DW_TAG_variable:
6415 case DW_TAG_subprogram:
6416 /* Some types are also in VAR_DOMAIN. */
6417 case DW_TAG_typedef:
6418 case DW_TAG_structure_type:
6419 break;
6420 default:
6421 goto again;
6422 }
6423 break;
6424 case STRUCT_DOMAIN:
6425 switch (indexval.dwarf_tag)
6426 {
6427 case DW_TAG_typedef:
6428 case DW_TAG_structure_type:
6429 break;
6430 default:
6431 goto again;
6432 }
6433 break;
6434 case LABEL_DOMAIN:
6435 switch (indexval.dwarf_tag)
6436 {
6437 case 0:
6438 case DW_TAG_variable:
6439 break;
6440 default:
6441 goto again;
6442 }
6443 break;
6444 default:
6445 break;
6446 }
6447
6448 /* Match dw2_expand_symtabs_matching, symbol_kind and
6449 debug_names::psymbol_tag. */
6450 switch (m_search)
6451 {
6452 case VARIABLES_DOMAIN:
6453 switch (indexval.dwarf_tag)
6454 {
6455 case DW_TAG_variable:
6456 break;
6457 default:
6458 goto again;
6459 }
6460 break;
6461 case FUNCTIONS_DOMAIN:
6462 switch (indexval.dwarf_tag)
6463 {
6464 case DW_TAG_subprogram:
6465 break;
6466 default:
6467 goto again;
6468 }
6469 break;
6470 case TYPES_DOMAIN:
6471 switch (indexval.dwarf_tag)
6472 {
6473 case DW_TAG_typedef:
6474 case DW_TAG_structure_type:
6475 break;
6476 default:
6477 goto again;
6478 }
6479 break;
6480 default:
6481 break;
6482 }
6483
6484 return per_cu;
6485 }
6486
6487 static struct compunit_symtab *
6488 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6489 const char *name, domain_enum domain)
6490 {
6491 const block_enum block_index = static_cast<block_enum> (block_index_int);
6492 struct dwarf2_per_objfile *dwarf2_per_objfile
6493 = get_dwarf2_per_objfile (objfile);
6494
6495 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6496 if (!mapp)
6497 {
6498 /* index is NULL if OBJF_READNOW. */
6499 return NULL;
6500 }
6501 const auto &map = *mapp;
6502
6503 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6504 block_index, domain, name);
6505
6506 struct compunit_symtab *stab_best = NULL;
6507 struct dwarf2_per_cu_data *per_cu;
6508 while ((per_cu = iter.next ()) != NULL)
6509 {
6510 struct symbol *sym, *with_opaque = NULL;
6511 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6512 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6513 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6514
6515 sym = block_find_symbol (block, name, domain,
6516 block_find_non_opaque_type_preferred,
6517 &with_opaque);
6518
6519 /* Some caution must be observed with overloaded functions and
6520 methods, since the index will not contain any overload
6521 information (but NAME might contain it). */
6522
6523 if (sym != NULL
6524 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6525 return stab;
6526 if (with_opaque != NULL
6527 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6528 stab_best = stab;
6529
6530 /* Keep looking through other CUs. */
6531 }
6532
6533 return stab_best;
6534 }
6535
6536 /* This dumps minimal information about .debug_names. It is called
6537 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6538 uses this to verify that .debug_names has been loaded. */
6539
6540 static void
6541 dw2_debug_names_dump (struct objfile *objfile)
6542 {
6543 struct dwarf2_per_objfile *dwarf2_per_objfile
6544 = get_dwarf2_per_objfile (objfile);
6545
6546 gdb_assert (dwarf2_per_objfile->using_index);
6547 printf_filtered (".debug_names:");
6548 if (dwarf2_per_objfile->debug_names_table)
6549 printf_filtered (" exists\n");
6550 else
6551 printf_filtered (" faked for \"readnow\"\n");
6552 printf_filtered ("\n");
6553 }
6554
6555 static void
6556 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6557 const char *func_name)
6558 {
6559 struct dwarf2_per_objfile *dwarf2_per_objfile
6560 = get_dwarf2_per_objfile (objfile);
6561
6562 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6563 if (dwarf2_per_objfile->debug_names_table)
6564 {
6565 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6566
6567 /* Note: It doesn't matter what we pass for block_index here. */
6568 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6569 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6570
6571 struct dwarf2_per_cu_data *per_cu;
6572 while ((per_cu = iter.next ()) != NULL)
6573 dw2_instantiate_symtab (per_cu);
6574 }
6575 }
6576
6577 static void
6578 dw2_debug_names_expand_symtabs_matching
6579 (struct objfile *objfile,
6580 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6581 const lookup_name_info &lookup_name,
6582 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6583 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6584 enum search_domain kind)
6585 {
6586 struct dwarf2_per_objfile *dwarf2_per_objfile
6587 = get_dwarf2_per_objfile (objfile);
6588
6589 /* debug_names_table is NULL if OBJF_READNOW. */
6590 if (!dwarf2_per_objfile->debug_names_table)
6591 return;
6592
6593 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6594
6595 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6596
6597 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6598 symbol_matcher,
6599 kind, [&] (offset_type namei)
6600 {
6601 /* The name was matched, now expand corresponding CUs that were
6602 marked. */
6603 dw2_debug_names_iterator iter (map, kind, namei);
6604
6605 struct dwarf2_per_cu_data *per_cu;
6606 while ((per_cu = iter.next ()) != NULL)
6607 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6608 expansion_notify);
6609 });
6610 }
6611
6612 const struct quick_symbol_functions dwarf2_debug_names_functions =
6613 {
6614 dw2_has_symbols,
6615 dw2_find_last_source_symtab,
6616 dw2_forget_cached_source_info,
6617 dw2_map_symtabs_matching_filename,
6618 dw2_debug_names_lookup_symbol,
6619 dw2_print_stats,
6620 dw2_debug_names_dump,
6621 dw2_relocate,
6622 dw2_debug_names_expand_symtabs_for_function,
6623 dw2_expand_all_symtabs,
6624 dw2_expand_symtabs_with_fullname,
6625 dw2_map_matching_symbols,
6626 dw2_debug_names_expand_symtabs_matching,
6627 dw2_find_pc_sect_compunit_symtab,
6628 NULL,
6629 dw2_map_symbol_filenames
6630 };
6631
6632 /* See symfile.h. */
6633
6634 bool
6635 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6636 {
6637 struct dwarf2_per_objfile *dwarf2_per_objfile
6638 = get_dwarf2_per_objfile (objfile);
6639
6640 /* If we're about to read full symbols, don't bother with the
6641 indices. In this case we also don't care if some other debug
6642 format is making psymtabs, because they are all about to be
6643 expanded anyway. */
6644 if ((objfile->flags & OBJF_READNOW))
6645 {
6646 int i;
6647
6648 dwarf2_per_objfile->using_index = 1;
6649 create_all_comp_units (dwarf2_per_objfile);
6650 create_all_type_units (dwarf2_per_objfile);
6651 dwarf2_per_objfile->quick_file_names_table =
6652 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6653
6654 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
6655 + dwarf2_per_objfile->n_type_units); ++i)
6656 {
6657 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
6658
6659 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6660 struct dwarf2_per_cu_quick_data);
6661 }
6662
6663 /* Return 1 so that gdb sees the "quick" functions. However,
6664 these functions will be no-ops because we will have expanded
6665 all symtabs. */
6666 *index_kind = dw_index_kind::GDB_INDEX;
6667 return true;
6668 }
6669
6670 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6671 {
6672 *index_kind = dw_index_kind::DEBUG_NAMES;
6673 return true;
6674 }
6675
6676 if (dwarf2_read_index (objfile))
6677 {
6678 *index_kind = dw_index_kind::GDB_INDEX;
6679 return true;
6680 }
6681
6682 return false;
6683 }
6684
6685 \f
6686
6687 /* Build a partial symbol table. */
6688
6689 void
6690 dwarf2_build_psymtabs (struct objfile *objfile)
6691 {
6692 struct dwarf2_per_objfile *dwarf2_per_objfile
6693 = get_dwarf2_per_objfile (objfile);
6694
6695 if (objfile->global_psymbols.capacity () == 0
6696 && objfile->static_psymbols.capacity () == 0)
6697 init_psymbol_list (objfile, 1024);
6698
6699 TRY
6700 {
6701 /* This isn't really ideal: all the data we allocate on the
6702 objfile's obstack is still uselessly kept around. However,
6703 freeing it seems unsafe. */
6704 psymtab_discarder psymtabs (objfile);
6705 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6706 psymtabs.keep ();
6707 }
6708 CATCH (except, RETURN_MASK_ERROR)
6709 {
6710 exception_print (gdb_stderr, except);
6711 }
6712 END_CATCH
6713 }
6714
6715 /* Return the total length of the CU described by HEADER. */
6716
6717 static unsigned int
6718 get_cu_length (const struct comp_unit_head *header)
6719 {
6720 return header->initial_length_size + header->length;
6721 }
6722
6723 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6724
6725 static inline bool
6726 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6727 {
6728 sect_offset bottom = cu_header->sect_off;
6729 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6730
6731 return sect_off >= bottom && sect_off < top;
6732 }
6733
6734 /* Find the base address of the compilation unit for range lists and
6735 location lists. It will normally be specified by DW_AT_low_pc.
6736 In DWARF-3 draft 4, the base address could be overridden by
6737 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6738 compilation units with discontinuous ranges. */
6739
6740 static void
6741 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6742 {
6743 struct attribute *attr;
6744
6745 cu->base_known = 0;
6746 cu->base_address = 0;
6747
6748 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6749 if (attr)
6750 {
6751 cu->base_address = attr_value_as_address (attr);
6752 cu->base_known = 1;
6753 }
6754 else
6755 {
6756 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6757 if (attr)
6758 {
6759 cu->base_address = attr_value_as_address (attr);
6760 cu->base_known = 1;
6761 }
6762 }
6763 }
6764
6765 /* Read in the comp unit header information from the debug_info at info_ptr.
6766 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6767 NOTE: This leaves members offset, first_die_offset to be filled in
6768 by the caller. */
6769
6770 static const gdb_byte *
6771 read_comp_unit_head (struct comp_unit_head *cu_header,
6772 const gdb_byte *info_ptr,
6773 struct dwarf2_section_info *section,
6774 rcuh_kind section_kind)
6775 {
6776 int signed_addr;
6777 unsigned int bytes_read;
6778 const char *filename = get_section_file_name (section);
6779 bfd *abfd = get_section_bfd_owner (section);
6780
6781 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6782 cu_header->initial_length_size = bytes_read;
6783 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6784 info_ptr += bytes_read;
6785 cu_header->version = read_2_bytes (abfd, info_ptr);
6786 info_ptr += 2;
6787 if (cu_header->version < 5)
6788 switch (section_kind)
6789 {
6790 case rcuh_kind::COMPILE:
6791 cu_header->unit_type = DW_UT_compile;
6792 break;
6793 case rcuh_kind::TYPE:
6794 cu_header->unit_type = DW_UT_type;
6795 break;
6796 default:
6797 internal_error (__FILE__, __LINE__,
6798 _("read_comp_unit_head: invalid section_kind"));
6799 }
6800 else
6801 {
6802 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6803 (read_1_byte (abfd, info_ptr));
6804 info_ptr += 1;
6805 switch (cu_header->unit_type)
6806 {
6807 case DW_UT_compile:
6808 if (section_kind != rcuh_kind::COMPILE)
6809 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6810 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6811 filename);
6812 break;
6813 case DW_UT_type:
6814 section_kind = rcuh_kind::TYPE;
6815 break;
6816 default:
6817 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6818 "(is %d, should be %d or %d) [in module %s]"),
6819 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6820 }
6821
6822 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6823 info_ptr += 1;
6824 }
6825 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6826 cu_header,
6827 &bytes_read);
6828 info_ptr += bytes_read;
6829 if (cu_header->version < 5)
6830 {
6831 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6832 info_ptr += 1;
6833 }
6834 signed_addr = bfd_get_sign_extend_vma (abfd);
6835 if (signed_addr < 0)
6836 internal_error (__FILE__, __LINE__,
6837 _("read_comp_unit_head: dwarf from non elf file"));
6838 cu_header->signed_addr_p = signed_addr;
6839
6840 if (section_kind == rcuh_kind::TYPE)
6841 {
6842 LONGEST type_offset;
6843
6844 cu_header->signature = read_8_bytes (abfd, info_ptr);
6845 info_ptr += 8;
6846
6847 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6848 info_ptr += bytes_read;
6849 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6850 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6851 error (_("Dwarf Error: Too big type_offset in compilation unit "
6852 "header (is %s) [in module %s]"), plongest (type_offset),
6853 filename);
6854 }
6855
6856 return info_ptr;
6857 }
6858
6859 /* Helper function that returns the proper abbrev section for
6860 THIS_CU. */
6861
6862 static struct dwarf2_section_info *
6863 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6864 {
6865 struct dwarf2_section_info *abbrev;
6866 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6867
6868 if (this_cu->is_dwz)
6869 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6870 else
6871 abbrev = &dwarf2_per_objfile->abbrev;
6872
6873 return abbrev;
6874 }
6875
6876 /* Subroutine of read_and_check_comp_unit_head and
6877 read_and_check_type_unit_head to simplify them.
6878 Perform various error checking on the header. */
6879
6880 static void
6881 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6882 struct comp_unit_head *header,
6883 struct dwarf2_section_info *section,
6884 struct dwarf2_section_info *abbrev_section)
6885 {
6886 const char *filename = get_section_file_name (section);
6887
6888 if (header->version < 2 || header->version > 5)
6889 error (_("Dwarf Error: wrong version in compilation unit header "
6890 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6891 filename);
6892
6893 if (to_underlying (header->abbrev_sect_off)
6894 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6895 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6896 "(offset %s + 6) [in module %s]"),
6897 sect_offset_str (header->abbrev_sect_off),
6898 sect_offset_str (header->sect_off),
6899 filename);
6900
6901 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6902 avoid potential 32-bit overflow. */
6903 if (((ULONGEST) header->sect_off + get_cu_length (header))
6904 > section->size)
6905 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6906 "(offset %s + 0) [in module %s]"),
6907 header->length, sect_offset_str (header->sect_off),
6908 filename);
6909 }
6910
6911 /* Read in a CU/TU header and perform some basic error checking.
6912 The contents of the header are stored in HEADER.
6913 The result is a pointer to the start of the first DIE. */
6914
6915 static const gdb_byte *
6916 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6917 struct comp_unit_head *header,
6918 struct dwarf2_section_info *section,
6919 struct dwarf2_section_info *abbrev_section,
6920 const gdb_byte *info_ptr,
6921 rcuh_kind section_kind)
6922 {
6923 const gdb_byte *beg_of_comp_unit = info_ptr;
6924
6925 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6926
6927 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6928
6929 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6930
6931 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6932 abbrev_section);
6933
6934 return info_ptr;
6935 }
6936
6937 /* Fetch the abbreviation table offset from a comp or type unit header. */
6938
6939 static sect_offset
6940 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6941 struct dwarf2_section_info *section,
6942 sect_offset sect_off)
6943 {
6944 bfd *abfd = get_section_bfd_owner (section);
6945 const gdb_byte *info_ptr;
6946 unsigned int initial_length_size, offset_size;
6947 uint16_t version;
6948
6949 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6950 info_ptr = section->buffer + to_underlying (sect_off);
6951 read_initial_length (abfd, info_ptr, &initial_length_size);
6952 offset_size = initial_length_size == 4 ? 4 : 8;
6953 info_ptr += initial_length_size;
6954
6955 version = read_2_bytes (abfd, info_ptr);
6956 info_ptr += 2;
6957 if (version >= 5)
6958 {
6959 /* Skip unit type and address size. */
6960 info_ptr += 2;
6961 }
6962
6963 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6964 }
6965
6966 /* Allocate a new partial symtab for file named NAME and mark this new
6967 partial symtab as being an include of PST. */
6968
6969 static void
6970 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6971 struct objfile *objfile)
6972 {
6973 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6974
6975 if (!IS_ABSOLUTE_PATH (subpst->filename))
6976 {
6977 /* It shares objfile->objfile_obstack. */
6978 subpst->dirname = pst->dirname;
6979 }
6980
6981 subpst->textlow = 0;
6982 subpst->texthigh = 0;
6983
6984 subpst->dependencies
6985 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6986 subpst->dependencies[0] = pst;
6987 subpst->number_of_dependencies = 1;
6988
6989 subpst->globals_offset = 0;
6990 subpst->n_global_syms = 0;
6991 subpst->statics_offset = 0;
6992 subpst->n_static_syms = 0;
6993 subpst->compunit_symtab = NULL;
6994 subpst->read_symtab = pst->read_symtab;
6995 subpst->readin = 0;
6996
6997 /* No private part is necessary for include psymtabs. This property
6998 can be used to differentiate between such include psymtabs and
6999 the regular ones. */
7000 subpst->read_symtab_private = NULL;
7001 }
7002
7003 /* Read the Line Number Program data and extract the list of files
7004 included by the source file represented by PST. Build an include
7005 partial symtab for each of these included files. */
7006
7007 static void
7008 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
7009 struct die_info *die,
7010 struct partial_symtab *pst)
7011 {
7012 line_header_up lh;
7013 struct attribute *attr;
7014
7015 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7016 if (attr)
7017 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
7018 if (lh == NULL)
7019 return; /* No linetable, so no includes. */
7020
7021 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
7022 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
7023 }
7024
7025 static hashval_t
7026 hash_signatured_type (const void *item)
7027 {
7028 const struct signatured_type *sig_type
7029 = (const struct signatured_type *) item;
7030
7031 /* This drops the top 32 bits of the signature, but is ok for a hash. */
7032 return sig_type->signature;
7033 }
7034
7035 static int
7036 eq_signatured_type (const void *item_lhs, const void *item_rhs)
7037 {
7038 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
7039 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
7040
7041 return lhs->signature == rhs->signature;
7042 }
7043
7044 /* Allocate a hash table for signatured types. */
7045
7046 static htab_t
7047 allocate_signatured_type_table (struct objfile *objfile)
7048 {
7049 return htab_create_alloc_ex (41,
7050 hash_signatured_type,
7051 eq_signatured_type,
7052 NULL,
7053 &objfile->objfile_obstack,
7054 hashtab_obstack_allocate,
7055 dummy_obstack_deallocate);
7056 }
7057
7058 /* A helper function to add a signatured type CU to a table. */
7059
7060 static int
7061 add_signatured_type_cu_to_table (void **slot, void *datum)
7062 {
7063 struct signatured_type *sigt = (struct signatured_type *) *slot;
7064 struct signatured_type ***datap = (struct signatured_type ***) datum;
7065
7066 **datap = sigt;
7067 ++*datap;
7068
7069 return 1;
7070 }
7071
7072 /* A helper for create_debug_types_hash_table. Read types from SECTION
7073 and fill them into TYPES_HTAB. It will process only type units,
7074 therefore DW_UT_type. */
7075
7076 static void
7077 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7078 struct dwo_file *dwo_file,
7079 dwarf2_section_info *section, htab_t &types_htab,
7080 rcuh_kind section_kind)
7081 {
7082 struct objfile *objfile = dwarf2_per_objfile->objfile;
7083 struct dwarf2_section_info *abbrev_section;
7084 bfd *abfd;
7085 const gdb_byte *info_ptr, *end_ptr;
7086
7087 abbrev_section = (dwo_file != NULL
7088 ? &dwo_file->sections.abbrev
7089 : &dwarf2_per_objfile->abbrev);
7090
7091 if (dwarf_read_debug)
7092 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
7093 get_section_name (section),
7094 get_section_file_name (abbrev_section));
7095
7096 dwarf2_read_section (objfile, section);
7097 info_ptr = section->buffer;
7098
7099 if (info_ptr == NULL)
7100 return;
7101
7102 /* We can't set abfd until now because the section may be empty or
7103 not present, in which case the bfd is unknown. */
7104 abfd = get_section_bfd_owner (section);
7105
7106 /* We don't use init_cutu_and_read_dies_simple, or some such, here
7107 because we don't need to read any dies: the signature is in the
7108 header. */
7109
7110 end_ptr = info_ptr + section->size;
7111 while (info_ptr < end_ptr)
7112 {
7113 struct signatured_type *sig_type;
7114 struct dwo_unit *dwo_tu;
7115 void **slot;
7116 const gdb_byte *ptr = info_ptr;
7117 struct comp_unit_head header;
7118 unsigned int length;
7119
7120 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
7121
7122 /* Initialize it due to a false compiler warning. */
7123 header.signature = -1;
7124 header.type_cu_offset_in_tu = (cu_offset) -1;
7125
7126 /* We need to read the type's signature in order to build the hash
7127 table, but we don't need anything else just yet. */
7128
7129 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
7130 abbrev_section, ptr, section_kind);
7131
7132 length = get_cu_length (&header);
7133
7134 /* Skip dummy type units. */
7135 if (ptr >= info_ptr + length
7136 || peek_abbrev_code (abfd, ptr) == 0
7137 || header.unit_type != DW_UT_type)
7138 {
7139 info_ptr += length;
7140 continue;
7141 }
7142
7143 if (types_htab == NULL)
7144 {
7145 if (dwo_file)
7146 types_htab = allocate_dwo_unit_table (objfile);
7147 else
7148 types_htab = allocate_signatured_type_table (objfile);
7149 }
7150
7151 if (dwo_file)
7152 {
7153 sig_type = NULL;
7154 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7155 struct dwo_unit);
7156 dwo_tu->dwo_file = dwo_file;
7157 dwo_tu->signature = header.signature;
7158 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
7159 dwo_tu->section = section;
7160 dwo_tu->sect_off = sect_off;
7161 dwo_tu->length = length;
7162 }
7163 else
7164 {
7165 /* N.B.: type_offset is not usable if this type uses a DWO file.
7166 The real type_offset is in the DWO file. */
7167 dwo_tu = NULL;
7168 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7169 struct signatured_type);
7170 sig_type->signature = header.signature;
7171 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
7172 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7173 sig_type->per_cu.is_debug_types = 1;
7174 sig_type->per_cu.section = section;
7175 sig_type->per_cu.sect_off = sect_off;
7176 sig_type->per_cu.length = length;
7177 }
7178
7179 slot = htab_find_slot (types_htab,
7180 dwo_file ? (void*) dwo_tu : (void *) sig_type,
7181 INSERT);
7182 gdb_assert (slot != NULL);
7183 if (*slot != NULL)
7184 {
7185 sect_offset dup_sect_off;
7186
7187 if (dwo_file)
7188 {
7189 const struct dwo_unit *dup_tu
7190 = (const struct dwo_unit *) *slot;
7191
7192 dup_sect_off = dup_tu->sect_off;
7193 }
7194 else
7195 {
7196 const struct signatured_type *dup_tu
7197 = (const struct signatured_type *) *slot;
7198
7199 dup_sect_off = dup_tu->per_cu.sect_off;
7200 }
7201
7202 complaint (&symfile_complaints,
7203 _("debug type entry at offset %s is duplicate to"
7204 " the entry at offset %s, signature %s"),
7205 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
7206 hex_string (header.signature));
7207 }
7208 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
7209
7210 if (dwarf_read_debug > 1)
7211 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
7212 sect_offset_str (sect_off),
7213 hex_string (header.signature));
7214
7215 info_ptr += length;
7216 }
7217 }
7218
7219 /* Create the hash table of all entries in the .debug_types
7220 (or .debug_types.dwo) section(s).
7221 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7222 otherwise it is NULL.
7223
7224 The result is a pointer to the hash table or NULL if there are no types.
7225
7226 Note: This function processes DWO files only, not DWP files. */
7227
7228 static void
7229 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7230 struct dwo_file *dwo_file,
7231 VEC (dwarf2_section_info_def) *types,
7232 htab_t &types_htab)
7233 {
7234 int ix;
7235 struct dwarf2_section_info *section;
7236
7237 if (VEC_empty (dwarf2_section_info_def, types))
7238 return;
7239
7240 for (ix = 0;
7241 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7242 ++ix)
7243 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
7244 types_htab, rcuh_kind::TYPE);
7245 }
7246
7247 /* Create the hash table of all entries in the .debug_types section,
7248 and initialize all_type_units.
7249 The result is zero if there is an error (e.g. missing .debug_types section),
7250 otherwise non-zero. */
7251
7252 static int
7253 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7254 {
7255 htab_t types_htab = NULL;
7256 struct signatured_type **iter;
7257
7258 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
7259 &dwarf2_per_objfile->info, types_htab,
7260 rcuh_kind::COMPILE);
7261 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
7262 dwarf2_per_objfile->types, types_htab);
7263 if (types_htab == NULL)
7264 {
7265 dwarf2_per_objfile->signatured_types = NULL;
7266 return 0;
7267 }
7268
7269 dwarf2_per_objfile->signatured_types = types_htab;
7270
7271 dwarf2_per_objfile->n_type_units
7272 = dwarf2_per_objfile->n_allocated_type_units
7273 = htab_elements (types_htab);
7274 dwarf2_per_objfile->all_type_units =
7275 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
7276 iter = &dwarf2_per_objfile->all_type_units[0];
7277 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7278 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7279 == dwarf2_per_objfile->n_type_units);
7280
7281 return 1;
7282 }
7283
7284 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7285 If SLOT is non-NULL, it is the entry to use in the hash table.
7286 Otherwise we find one. */
7287
7288 static struct signatured_type *
7289 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
7290 void **slot)
7291 {
7292 struct objfile *objfile = dwarf2_per_objfile->objfile;
7293 int n_type_units = dwarf2_per_objfile->n_type_units;
7294 struct signatured_type *sig_type;
7295
7296 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7297 ++n_type_units;
7298 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7299 {
7300 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7301 dwarf2_per_objfile->n_allocated_type_units = 1;
7302 dwarf2_per_objfile->n_allocated_type_units *= 2;
7303 dwarf2_per_objfile->all_type_units
7304 = XRESIZEVEC (struct signatured_type *,
7305 dwarf2_per_objfile->all_type_units,
7306 dwarf2_per_objfile->n_allocated_type_units);
7307 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7308 }
7309 dwarf2_per_objfile->n_type_units = n_type_units;
7310
7311 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7312 struct signatured_type);
7313 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7314 sig_type->signature = sig;
7315 sig_type->per_cu.is_debug_types = 1;
7316 if (dwarf2_per_objfile->using_index)
7317 {
7318 sig_type->per_cu.v.quick =
7319 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7320 struct dwarf2_per_cu_quick_data);
7321 }
7322
7323 if (slot == NULL)
7324 {
7325 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7326 sig_type, INSERT);
7327 }
7328 gdb_assert (*slot == NULL);
7329 *slot = sig_type;
7330 /* The rest of sig_type must be filled in by the caller. */
7331 return sig_type;
7332 }
7333
7334 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7335 Fill in SIG_ENTRY with DWO_ENTRY. */
7336
7337 static void
7338 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
7339 struct signatured_type *sig_entry,
7340 struct dwo_unit *dwo_entry)
7341 {
7342 /* Make sure we're not clobbering something we don't expect to. */
7343 gdb_assert (! sig_entry->per_cu.queued);
7344 gdb_assert (sig_entry->per_cu.cu == NULL);
7345 if (dwarf2_per_objfile->using_index)
7346 {
7347 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7348 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7349 }
7350 else
7351 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7352 gdb_assert (sig_entry->signature == dwo_entry->signature);
7353 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7354 gdb_assert (sig_entry->type_unit_group == NULL);
7355 gdb_assert (sig_entry->dwo_unit == NULL);
7356
7357 sig_entry->per_cu.section = dwo_entry->section;
7358 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7359 sig_entry->per_cu.length = dwo_entry->length;
7360 sig_entry->per_cu.reading_dwo_directly = 1;
7361 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7362 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7363 sig_entry->dwo_unit = dwo_entry;
7364 }
7365
7366 /* Subroutine of lookup_signatured_type.
7367 If we haven't read the TU yet, create the signatured_type data structure
7368 for a TU to be read in directly from a DWO file, bypassing the stub.
7369 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7370 using .gdb_index, then when reading a CU we want to stay in the DWO file
7371 containing that CU. Otherwise we could end up reading several other DWO
7372 files (due to comdat folding) to process the transitive closure of all the
7373 mentioned TUs, and that can be slow. The current DWO file will have every
7374 type signature that it needs.
7375 We only do this for .gdb_index because in the psymtab case we already have
7376 to read all the DWOs to build the type unit groups. */
7377
7378 static struct signatured_type *
7379 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7380 {
7381 struct dwarf2_per_objfile *dwarf2_per_objfile
7382 = cu->per_cu->dwarf2_per_objfile;
7383 struct objfile *objfile = dwarf2_per_objfile->objfile;
7384 struct dwo_file *dwo_file;
7385 struct dwo_unit find_dwo_entry, *dwo_entry;
7386 struct signatured_type find_sig_entry, *sig_entry;
7387 void **slot;
7388
7389 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7390
7391 /* If TU skeletons have been removed then we may not have read in any
7392 TUs yet. */
7393 if (dwarf2_per_objfile->signatured_types == NULL)
7394 {
7395 dwarf2_per_objfile->signatured_types
7396 = allocate_signatured_type_table (objfile);
7397 }
7398
7399 /* We only ever need to read in one copy of a signatured type.
7400 Use the global signatured_types array to do our own comdat-folding
7401 of types. If this is the first time we're reading this TU, and
7402 the TU has an entry in .gdb_index, replace the recorded data from
7403 .gdb_index with this TU. */
7404
7405 find_sig_entry.signature = sig;
7406 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7407 &find_sig_entry, INSERT);
7408 sig_entry = (struct signatured_type *) *slot;
7409
7410 /* We can get here with the TU already read, *or* in the process of being
7411 read. Don't reassign the global entry to point to this DWO if that's
7412 the case. Also note that if the TU is already being read, it may not
7413 have come from a DWO, the program may be a mix of Fission-compiled
7414 code and non-Fission-compiled code. */
7415
7416 /* Have we already tried to read this TU?
7417 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7418 needn't exist in the global table yet). */
7419 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7420 return sig_entry;
7421
7422 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7423 dwo_unit of the TU itself. */
7424 dwo_file = cu->dwo_unit->dwo_file;
7425
7426 /* Ok, this is the first time we're reading this TU. */
7427 if (dwo_file->tus == NULL)
7428 return NULL;
7429 find_dwo_entry.signature = sig;
7430 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7431 if (dwo_entry == NULL)
7432 return NULL;
7433
7434 /* If the global table doesn't have an entry for this TU, add one. */
7435 if (sig_entry == NULL)
7436 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7437
7438 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7439 sig_entry->per_cu.tu_read = 1;
7440 return sig_entry;
7441 }
7442
7443 /* Subroutine of lookup_signatured_type.
7444 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7445 then try the DWP file. If the TU stub (skeleton) has been removed then
7446 it won't be in .gdb_index. */
7447
7448 static struct signatured_type *
7449 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7450 {
7451 struct dwarf2_per_objfile *dwarf2_per_objfile
7452 = cu->per_cu->dwarf2_per_objfile;
7453 struct objfile *objfile = dwarf2_per_objfile->objfile;
7454 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7455 struct dwo_unit *dwo_entry;
7456 struct signatured_type find_sig_entry, *sig_entry;
7457 void **slot;
7458
7459 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7460 gdb_assert (dwp_file != NULL);
7461
7462 /* If TU skeletons have been removed then we may not have read in any
7463 TUs yet. */
7464 if (dwarf2_per_objfile->signatured_types == NULL)
7465 {
7466 dwarf2_per_objfile->signatured_types
7467 = allocate_signatured_type_table (objfile);
7468 }
7469
7470 find_sig_entry.signature = sig;
7471 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7472 &find_sig_entry, INSERT);
7473 sig_entry = (struct signatured_type *) *slot;
7474
7475 /* Have we already tried to read this TU?
7476 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7477 needn't exist in the global table yet). */
7478 if (sig_entry != NULL)
7479 return sig_entry;
7480
7481 if (dwp_file->tus == NULL)
7482 return NULL;
7483 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7484 sig, 1 /* is_debug_types */);
7485 if (dwo_entry == NULL)
7486 return NULL;
7487
7488 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7489 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7490
7491 return sig_entry;
7492 }
7493
7494 /* Lookup a signature based type for DW_FORM_ref_sig8.
7495 Returns NULL if signature SIG is not present in the table.
7496 It is up to the caller to complain about this. */
7497
7498 static struct signatured_type *
7499 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7500 {
7501 struct dwarf2_per_objfile *dwarf2_per_objfile
7502 = cu->per_cu->dwarf2_per_objfile;
7503
7504 if (cu->dwo_unit
7505 && dwarf2_per_objfile->using_index)
7506 {
7507 /* We're in a DWO/DWP file, and we're using .gdb_index.
7508 These cases require special processing. */
7509 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7510 return lookup_dwo_signatured_type (cu, sig);
7511 else
7512 return lookup_dwp_signatured_type (cu, sig);
7513 }
7514 else
7515 {
7516 struct signatured_type find_entry, *entry;
7517
7518 if (dwarf2_per_objfile->signatured_types == NULL)
7519 return NULL;
7520 find_entry.signature = sig;
7521 entry = ((struct signatured_type *)
7522 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7523 return entry;
7524 }
7525 }
7526 \f
7527 /* Low level DIE reading support. */
7528
7529 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7530
7531 static void
7532 init_cu_die_reader (struct die_reader_specs *reader,
7533 struct dwarf2_cu *cu,
7534 struct dwarf2_section_info *section,
7535 struct dwo_file *dwo_file,
7536 struct abbrev_table *abbrev_table)
7537 {
7538 gdb_assert (section->readin && section->buffer != NULL);
7539 reader->abfd = get_section_bfd_owner (section);
7540 reader->cu = cu;
7541 reader->dwo_file = dwo_file;
7542 reader->die_section = section;
7543 reader->buffer = section->buffer;
7544 reader->buffer_end = section->buffer + section->size;
7545 reader->comp_dir = NULL;
7546 reader->abbrev_table = abbrev_table;
7547 }
7548
7549 /* Subroutine of init_cutu_and_read_dies to simplify it.
7550 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7551 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7552 already.
7553
7554 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7555 from it to the DIE in the DWO. If NULL we are skipping the stub.
7556 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7557 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7558 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7559 STUB_COMP_DIR may be non-NULL.
7560 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7561 are filled in with the info of the DIE from the DWO file.
7562 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7563 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7564 kept around for at least as long as *RESULT_READER.
7565
7566 The result is non-zero if a valid (non-dummy) DIE was found. */
7567
7568 static int
7569 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7570 struct dwo_unit *dwo_unit,
7571 struct die_info *stub_comp_unit_die,
7572 const char *stub_comp_dir,
7573 struct die_reader_specs *result_reader,
7574 const gdb_byte **result_info_ptr,
7575 struct die_info **result_comp_unit_die,
7576 int *result_has_children,
7577 abbrev_table_up *result_dwo_abbrev_table)
7578 {
7579 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7580 struct objfile *objfile = dwarf2_per_objfile->objfile;
7581 struct dwarf2_cu *cu = this_cu->cu;
7582 bfd *abfd;
7583 const gdb_byte *begin_info_ptr, *info_ptr;
7584 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7585 int i,num_extra_attrs;
7586 struct dwarf2_section_info *dwo_abbrev_section;
7587 struct attribute *attr;
7588 struct die_info *comp_unit_die;
7589
7590 /* At most one of these may be provided. */
7591 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7592
7593 /* These attributes aren't processed until later:
7594 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7595 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7596 referenced later. However, these attributes are found in the stub
7597 which we won't have later. In order to not impose this complication
7598 on the rest of the code, we read them here and copy them to the
7599 DWO CU/TU die. */
7600
7601 stmt_list = NULL;
7602 low_pc = NULL;
7603 high_pc = NULL;
7604 ranges = NULL;
7605 comp_dir = NULL;
7606
7607 if (stub_comp_unit_die != NULL)
7608 {
7609 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7610 DWO file. */
7611 if (! this_cu->is_debug_types)
7612 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7613 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7614 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7615 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7616 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7617
7618 /* There should be a DW_AT_addr_base attribute here (if needed).
7619 We need the value before we can process DW_FORM_GNU_addr_index. */
7620 cu->addr_base = 0;
7621 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7622 if (attr)
7623 cu->addr_base = DW_UNSND (attr);
7624
7625 /* There should be a DW_AT_ranges_base attribute here (if needed).
7626 We need the value before we can process DW_AT_ranges. */
7627 cu->ranges_base = 0;
7628 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7629 if (attr)
7630 cu->ranges_base = DW_UNSND (attr);
7631 }
7632 else if (stub_comp_dir != NULL)
7633 {
7634 /* Reconstruct the comp_dir attribute to simplify the code below. */
7635 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7636 comp_dir->name = DW_AT_comp_dir;
7637 comp_dir->form = DW_FORM_string;
7638 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7639 DW_STRING (comp_dir) = stub_comp_dir;
7640 }
7641
7642 /* Set up for reading the DWO CU/TU. */
7643 cu->dwo_unit = dwo_unit;
7644 dwarf2_section_info *section = dwo_unit->section;
7645 dwarf2_read_section (objfile, section);
7646 abfd = get_section_bfd_owner (section);
7647 begin_info_ptr = info_ptr = (section->buffer
7648 + to_underlying (dwo_unit->sect_off));
7649 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7650
7651 if (this_cu->is_debug_types)
7652 {
7653 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7654
7655 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7656 &cu->header, section,
7657 dwo_abbrev_section,
7658 info_ptr, rcuh_kind::TYPE);
7659 /* This is not an assert because it can be caused by bad debug info. */
7660 if (sig_type->signature != cu->header.signature)
7661 {
7662 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7663 " TU at offset %s [in module %s]"),
7664 hex_string (sig_type->signature),
7665 hex_string (cu->header.signature),
7666 sect_offset_str (dwo_unit->sect_off),
7667 bfd_get_filename (abfd));
7668 }
7669 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7670 /* For DWOs coming from DWP files, we don't know the CU length
7671 nor the type's offset in the TU until now. */
7672 dwo_unit->length = get_cu_length (&cu->header);
7673 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7674
7675 /* Establish the type offset that can be used to lookup the type.
7676 For DWO files, we don't know it until now. */
7677 sig_type->type_offset_in_section
7678 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7679 }
7680 else
7681 {
7682 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7683 &cu->header, section,
7684 dwo_abbrev_section,
7685 info_ptr, rcuh_kind::COMPILE);
7686 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7687 /* For DWOs coming from DWP files, we don't know the CU length
7688 until now. */
7689 dwo_unit->length = get_cu_length (&cu->header);
7690 }
7691
7692 *result_dwo_abbrev_table
7693 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7694 cu->header.abbrev_sect_off);
7695 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7696 result_dwo_abbrev_table->get ());
7697
7698 /* Read in the die, but leave space to copy over the attributes
7699 from the stub. This has the benefit of simplifying the rest of
7700 the code - all the work to maintain the illusion of a single
7701 DW_TAG_{compile,type}_unit DIE is done here. */
7702 num_extra_attrs = ((stmt_list != NULL)
7703 + (low_pc != NULL)
7704 + (high_pc != NULL)
7705 + (ranges != NULL)
7706 + (comp_dir != NULL));
7707 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7708 result_has_children, num_extra_attrs);
7709
7710 /* Copy over the attributes from the stub to the DIE we just read in. */
7711 comp_unit_die = *result_comp_unit_die;
7712 i = comp_unit_die->num_attrs;
7713 if (stmt_list != NULL)
7714 comp_unit_die->attrs[i++] = *stmt_list;
7715 if (low_pc != NULL)
7716 comp_unit_die->attrs[i++] = *low_pc;
7717 if (high_pc != NULL)
7718 comp_unit_die->attrs[i++] = *high_pc;
7719 if (ranges != NULL)
7720 comp_unit_die->attrs[i++] = *ranges;
7721 if (comp_dir != NULL)
7722 comp_unit_die->attrs[i++] = *comp_dir;
7723 comp_unit_die->num_attrs += num_extra_attrs;
7724
7725 if (dwarf_die_debug)
7726 {
7727 fprintf_unfiltered (gdb_stdlog,
7728 "Read die from %s@0x%x of %s:\n",
7729 get_section_name (section),
7730 (unsigned) (begin_info_ptr - section->buffer),
7731 bfd_get_filename (abfd));
7732 dump_die (comp_unit_die, dwarf_die_debug);
7733 }
7734
7735 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7736 TUs by skipping the stub and going directly to the entry in the DWO file.
7737 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7738 to get it via circuitous means. Blech. */
7739 if (comp_dir != NULL)
7740 result_reader->comp_dir = DW_STRING (comp_dir);
7741
7742 /* Skip dummy compilation units. */
7743 if (info_ptr >= begin_info_ptr + dwo_unit->length
7744 || peek_abbrev_code (abfd, info_ptr) == 0)
7745 return 0;
7746
7747 *result_info_ptr = info_ptr;
7748 return 1;
7749 }
7750
7751 /* Subroutine of init_cutu_and_read_dies to simplify it.
7752 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7753 Returns NULL if the specified DWO unit cannot be found. */
7754
7755 static struct dwo_unit *
7756 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7757 struct die_info *comp_unit_die)
7758 {
7759 struct dwarf2_cu *cu = this_cu->cu;
7760 ULONGEST signature;
7761 struct dwo_unit *dwo_unit;
7762 const char *comp_dir, *dwo_name;
7763
7764 gdb_assert (cu != NULL);
7765
7766 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7767 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7768 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7769
7770 if (this_cu->is_debug_types)
7771 {
7772 struct signatured_type *sig_type;
7773
7774 /* Since this_cu is the first member of struct signatured_type,
7775 we can go from a pointer to one to a pointer to the other. */
7776 sig_type = (struct signatured_type *) this_cu;
7777 signature = sig_type->signature;
7778 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7779 }
7780 else
7781 {
7782 struct attribute *attr;
7783
7784 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7785 if (! attr)
7786 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7787 " [in module %s]"),
7788 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7789 signature = DW_UNSND (attr);
7790 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7791 signature);
7792 }
7793
7794 return dwo_unit;
7795 }
7796
7797 /* Subroutine of init_cutu_and_read_dies to simplify it.
7798 See it for a description of the parameters.
7799 Read a TU directly from a DWO file, bypassing the stub. */
7800
7801 static void
7802 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7803 int use_existing_cu, int keep,
7804 die_reader_func_ftype *die_reader_func,
7805 void *data)
7806 {
7807 std::unique_ptr<dwarf2_cu> new_cu;
7808 struct signatured_type *sig_type;
7809 struct die_reader_specs reader;
7810 const gdb_byte *info_ptr;
7811 struct die_info *comp_unit_die;
7812 int has_children;
7813 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7814
7815 /* Verify we can do the following downcast, and that we have the
7816 data we need. */
7817 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7818 sig_type = (struct signatured_type *) this_cu;
7819 gdb_assert (sig_type->dwo_unit != NULL);
7820
7821 if (use_existing_cu && this_cu->cu != NULL)
7822 {
7823 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7824 /* There's no need to do the rereading_dwo_cu handling that
7825 init_cutu_and_read_dies does since we don't read the stub. */
7826 }
7827 else
7828 {
7829 /* If !use_existing_cu, this_cu->cu must be NULL. */
7830 gdb_assert (this_cu->cu == NULL);
7831 new_cu.reset (new dwarf2_cu (this_cu));
7832 }
7833
7834 /* A future optimization, if needed, would be to use an existing
7835 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7836 could share abbrev tables. */
7837
7838 /* The abbreviation table used by READER, this must live at least as long as
7839 READER. */
7840 abbrev_table_up dwo_abbrev_table;
7841
7842 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7843 NULL /* stub_comp_unit_die */,
7844 sig_type->dwo_unit->dwo_file->comp_dir,
7845 &reader, &info_ptr,
7846 &comp_unit_die, &has_children,
7847 &dwo_abbrev_table) == 0)
7848 {
7849 /* Dummy die. */
7850 return;
7851 }
7852
7853 /* All the "real" work is done here. */
7854 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7855
7856 /* This duplicates the code in init_cutu_and_read_dies,
7857 but the alternative is making the latter more complex.
7858 This function is only for the special case of using DWO files directly:
7859 no point in overly complicating the general case just to handle this. */
7860 if (new_cu != NULL && keep)
7861 {
7862 /* Link this CU into read_in_chain. */
7863 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7864 dwarf2_per_objfile->read_in_chain = this_cu;
7865 /* The chain owns it now. */
7866 new_cu.release ();
7867 }
7868 }
7869
7870 /* Initialize a CU (or TU) and read its DIEs.
7871 If the CU defers to a DWO file, read the DWO file as well.
7872
7873 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7874 Otherwise the table specified in the comp unit header is read in and used.
7875 This is an optimization for when we already have the abbrev table.
7876
7877 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7878 Otherwise, a new CU is allocated with xmalloc.
7879
7880 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7881 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7882
7883 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7884 linker) then DIE_READER_FUNC will not get called. */
7885
7886 static void
7887 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7888 struct abbrev_table *abbrev_table,
7889 int use_existing_cu, int keep,
7890 die_reader_func_ftype *die_reader_func,
7891 void *data)
7892 {
7893 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7894 struct objfile *objfile = dwarf2_per_objfile->objfile;
7895 struct dwarf2_section_info *section = this_cu->section;
7896 bfd *abfd = get_section_bfd_owner (section);
7897 struct dwarf2_cu *cu;
7898 const gdb_byte *begin_info_ptr, *info_ptr;
7899 struct die_reader_specs reader;
7900 struct die_info *comp_unit_die;
7901 int has_children;
7902 struct attribute *attr;
7903 struct signatured_type *sig_type = NULL;
7904 struct dwarf2_section_info *abbrev_section;
7905 /* Non-zero if CU currently points to a DWO file and we need to
7906 reread it. When this happens we need to reread the skeleton die
7907 before we can reread the DWO file (this only applies to CUs, not TUs). */
7908 int rereading_dwo_cu = 0;
7909
7910 if (dwarf_die_debug)
7911 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7912 this_cu->is_debug_types ? "type" : "comp",
7913 sect_offset_str (this_cu->sect_off));
7914
7915 if (use_existing_cu)
7916 gdb_assert (keep);
7917
7918 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7919 file (instead of going through the stub), short-circuit all of this. */
7920 if (this_cu->reading_dwo_directly)
7921 {
7922 /* Narrow down the scope of possibilities to have to understand. */
7923 gdb_assert (this_cu->is_debug_types);
7924 gdb_assert (abbrev_table == NULL);
7925 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7926 die_reader_func, data);
7927 return;
7928 }
7929
7930 /* This is cheap if the section is already read in. */
7931 dwarf2_read_section (objfile, section);
7932
7933 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7934
7935 abbrev_section = get_abbrev_section_for_cu (this_cu);
7936
7937 std::unique_ptr<dwarf2_cu> new_cu;
7938 if (use_existing_cu && this_cu->cu != NULL)
7939 {
7940 cu = this_cu->cu;
7941 /* If this CU is from a DWO file we need to start over, we need to
7942 refetch the attributes from the skeleton CU.
7943 This could be optimized by retrieving those attributes from when we
7944 were here the first time: the previous comp_unit_die was stored in
7945 comp_unit_obstack. But there's no data yet that we need this
7946 optimization. */
7947 if (cu->dwo_unit != NULL)
7948 rereading_dwo_cu = 1;
7949 }
7950 else
7951 {
7952 /* If !use_existing_cu, this_cu->cu must be NULL. */
7953 gdb_assert (this_cu->cu == NULL);
7954 new_cu.reset (new dwarf2_cu (this_cu));
7955 cu = new_cu.get ();
7956 }
7957
7958 /* Get the header. */
7959 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7960 {
7961 /* We already have the header, there's no need to read it in again. */
7962 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7963 }
7964 else
7965 {
7966 if (this_cu->is_debug_types)
7967 {
7968 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7969 &cu->header, section,
7970 abbrev_section, info_ptr,
7971 rcuh_kind::TYPE);
7972
7973 /* Since per_cu is the first member of struct signatured_type,
7974 we can go from a pointer to one to a pointer to the other. */
7975 sig_type = (struct signatured_type *) this_cu;
7976 gdb_assert (sig_type->signature == cu->header.signature);
7977 gdb_assert (sig_type->type_offset_in_tu
7978 == cu->header.type_cu_offset_in_tu);
7979 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7980
7981 /* LENGTH has not been set yet for type units if we're
7982 using .gdb_index. */
7983 this_cu->length = get_cu_length (&cu->header);
7984
7985 /* Establish the type offset that can be used to lookup the type. */
7986 sig_type->type_offset_in_section =
7987 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7988
7989 this_cu->dwarf_version = cu->header.version;
7990 }
7991 else
7992 {
7993 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7994 &cu->header, section,
7995 abbrev_section,
7996 info_ptr,
7997 rcuh_kind::COMPILE);
7998
7999 gdb_assert (this_cu->sect_off == cu->header.sect_off);
8000 gdb_assert (this_cu->length == get_cu_length (&cu->header));
8001 this_cu->dwarf_version = cu->header.version;
8002 }
8003 }
8004
8005 /* Skip dummy compilation units. */
8006 if (info_ptr >= begin_info_ptr + this_cu->length
8007 || peek_abbrev_code (abfd, info_ptr) == 0)
8008 return;
8009
8010 /* If we don't have them yet, read the abbrevs for this compilation unit.
8011 And if we need to read them now, make sure they're freed when we're
8012 done (own the table through ABBREV_TABLE_HOLDER). */
8013 abbrev_table_up abbrev_table_holder;
8014 if (abbrev_table != NULL)
8015 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
8016 else
8017 {
8018 abbrev_table_holder
8019 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8020 cu->header.abbrev_sect_off);
8021 abbrev_table = abbrev_table_holder.get ();
8022 }
8023
8024 /* Read the top level CU/TU die. */
8025 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
8026 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8027
8028 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
8029 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
8030 table from the DWO file and pass the ownership over to us. It will be
8031 referenced from READER, so we must make sure to free it after we're done
8032 with READER.
8033
8034 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
8035 DWO CU, that this test will fail (the attribute will not be present). */
8036 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
8037 abbrev_table_up dwo_abbrev_table;
8038 if (attr)
8039 {
8040 struct dwo_unit *dwo_unit;
8041 struct die_info *dwo_comp_unit_die;
8042
8043 if (has_children)
8044 {
8045 complaint (&symfile_complaints,
8046 _("compilation unit with DW_AT_GNU_dwo_name"
8047 " has children (offset %s) [in module %s]"),
8048 sect_offset_str (this_cu->sect_off),
8049 bfd_get_filename (abfd));
8050 }
8051 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
8052 if (dwo_unit != NULL)
8053 {
8054 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
8055 comp_unit_die, NULL,
8056 &reader, &info_ptr,
8057 &dwo_comp_unit_die, &has_children,
8058 &dwo_abbrev_table) == 0)
8059 {
8060 /* Dummy die. */
8061 return;
8062 }
8063 comp_unit_die = dwo_comp_unit_die;
8064 }
8065 else
8066 {
8067 /* Yikes, we couldn't find the rest of the DIE, we only have
8068 the stub. A complaint has already been logged. There's
8069 not much more we can do except pass on the stub DIE to
8070 die_reader_func. We don't want to throw an error on bad
8071 debug info. */
8072 }
8073 }
8074
8075 /* All of the above is setup for this call. Yikes. */
8076 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8077
8078 /* Done, clean up. */
8079 if (new_cu != NULL && keep)
8080 {
8081 /* Link this CU into read_in_chain. */
8082 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
8083 dwarf2_per_objfile->read_in_chain = this_cu;
8084 /* The chain owns it now. */
8085 new_cu.release ();
8086 }
8087 }
8088
8089 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
8090 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
8091 to have already done the lookup to find the DWO file).
8092
8093 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
8094 THIS_CU->is_debug_types, but nothing else.
8095
8096 We fill in THIS_CU->length.
8097
8098 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
8099 linker) then DIE_READER_FUNC will not get called.
8100
8101 THIS_CU->cu is always freed when done.
8102 This is done in order to not leave THIS_CU->cu in a state where we have
8103 to care whether it refers to the "main" CU or the DWO CU. */
8104
8105 static void
8106 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
8107 struct dwo_file *dwo_file,
8108 die_reader_func_ftype *die_reader_func,
8109 void *data)
8110 {
8111 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
8112 struct objfile *objfile = dwarf2_per_objfile->objfile;
8113 struct dwarf2_section_info *section = this_cu->section;
8114 bfd *abfd = get_section_bfd_owner (section);
8115 struct dwarf2_section_info *abbrev_section;
8116 const gdb_byte *begin_info_ptr, *info_ptr;
8117 struct die_reader_specs reader;
8118 struct die_info *comp_unit_die;
8119 int has_children;
8120
8121 if (dwarf_die_debug)
8122 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
8123 this_cu->is_debug_types ? "type" : "comp",
8124 sect_offset_str (this_cu->sect_off));
8125
8126 gdb_assert (this_cu->cu == NULL);
8127
8128 abbrev_section = (dwo_file != NULL
8129 ? &dwo_file->sections.abbrev
8130 : get_abbrev_section_for_cu (this_cu));
8131
8132 /* This is cheap if the section is already read in. */
8133 dwarf2_read_section (objfile, section);
8134
8135 struct dwarf2_cu cu (this_cu);
8136
8137 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
8138 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
8139 &cu.header, section,
8140 abbrev_section, info_ptr,
8141 (this_cu->is_debug_types
8142 ? rcuh_kind::TYPE
8143 : rcuh_kind::COMPILE));
8144
8145 this_cu->length = get_cu_length (&cu.header);
8146
8147 /* Skip dummy compilation units. */
8148 if (info_ptr >= begin_info_ptr + this_cu->length
8149 || peek_abbrev_code (abfd, info_ptr) == 0)
8150 return;
8151
8152 abbrev_table_up abbrev_table
8153 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8154 cu.header.abbrev_sect_off);
8155
8156 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
8157 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8158
8159 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8160 }
8161
8162 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
8163 does not lookup the specified DWO file.
8164 This cannot be used to read DWO files.
8165
8166 THIS_CU->cu is always freed when done.
8167 This is done in order to not leave THIS_CU->cu in a state where we have
8168 to care whether it refers to the "main" CU or the DWO CU.
8169 We can revisit this if the data shows there's a performance issue. */
8170
8171 static void
8172 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8173 die_reader_func_ftype *die_reader_func,
8174 void *data)
8175 {
8176 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
8177 }
8178 \f
8179 /* Type Unit Groups.
8180
8181 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8182 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8183 so that all types coming from the same compilation (.o file) are grouped
8184 together. A future step could be to put the types in the same symtab as
8185 the CU the types ultimately came from. */
8186
8187 static hashval_t
8188 hash_type_unit_group (const void *item)
8189 {
8190 const struct type_unit_group *tu_group
8191 = (const struct type_unit_group *) item;
8192
8193 return hash_stmt_list_entry (&tu_group->hash);
8194 }
8195
8196 static int
8197 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
8198 {
8199 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8200 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
8201
8202 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
8203 }
8204
8205 /* Allocate a hash table for type unit groups. */
8206
8207 static htab_t
8208 allocate_type_unit_groups_table (struct objfile *objfile)
8209 {
8210 return htab_create_alloc_ex (3,
8211 hash_type_unit_group,
8212 eq_type_unit_group,
8213 NULL,
8214 &objfile->objfile_obstack,
8215 hashtab_obstack_allocate,
8216 dummy_obstack_deallocate);
8217 }
8218
8219 /* Type units that don't have DW_AT_stmt_list are grouped into their own
8220 partial symtabs. We combine several TUs per psymtab to not let the size
8221 of any one psymtab grow too big. */
8222 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8223 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
8224
8225 /* Helper routine for get_type_unit_group.
8226 Create the type_unit_group object used to hold one or more TUs. */
8227
8228 static struct type_unit_group *
8229 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
8230 {
8231 struct dwarf2_per_objfile *dwarf2_per_objfile
8232 = cu->per_cu->dwarf2_per_objfile;
8233 struct objfile *objfile = dwarf2_per_objfile->objfile;
8234 struct dwarf2_per_cu_data *per_cu;
8235 struct type_unit_group *tu_group;
8236
8237 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8238 struct type_unit_group);
8239 per_cu = &tu_group->per_cu;
8240 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8241
8242 if (dwarf2_per_objfile->using_index)
8243 {
8244 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8245 struct dwarf2_per_cu_quick_data);
8246 }
8247 else
8248 {
8249 unsigned int line_offset = to_underlying (line_offset_struct);
8250 struct partial_symtab *pst;
8251 char *name;
8252
8253 /* Give the symtab a useful name for debug purposes. */
8254 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8255 name = xstrprintf ("<type_units_%d>",
8256 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8257 else
8258 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8259
8260 pst = create_partial_symtab (per_cu, name);
8261 pst->anonymous = 1;
8262
8263 xfree (name);
8264 }
8265
8266 tu_group->hash.dwo_unit = cu->dwo_unit;
8267 tu_group->hash.line_sect_off = line_offset_struct;
8268
8269 return tu_group;
8270 }
8271
8272 /* Look up the type_unit_group for type unit CU, and create it if necessary.
8273 STMT_LIST is a DW_AT_stmt_list attribute. */
8274
8275 static struct type_unit_group *
8276 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
8277 {
8278 struct dwarf2_per_objfile *dwarf2_per_objfile
8279 = cu->per_cu->dwarf2_per_objfile;
8280 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8281 struct type_unit_group *tu_group;
8282 void **slot;
8283 unsigned int line_offset;
8284 struct type_unit_group type_unit_group_for_lookup;
8285
8286 if (dwarf2_per_objfile->type_unit_groups == NULL)
8287 {
8288 dwarf2_per_objfile->type_unit_groups =
8289 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
8290 }
8291
8292 /* Do we need to create a new group, or can we use an existing one? */
8293
8294 if (stmt_list)
8295 {
8296 line_offset = DW_UNSND (stmt_list);
8297 ++tu_stats->nr_symtab_sharers;
8298 }
8299 else
8300 {
8301 /* Ugh, no stmt_list. Rare, but we have to handle it.
8302 We can do various things here like create one group per TU or
8303 spread them over multiple groups to split up the expansion work.
8304 To avoid worst case scenarios (too many groups or too large groups)
8305 we, umm, group them in bunches. */
8306 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8307 | (tu_stats->nr_stmt_less_type_units
8308 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8309 ++tu_stats->nr_stmt_less_type_units;
8310 }
8311
8312 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
8313 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
8314 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8315 &type_unit_group_for_lookup, INSERT);
8316 if (*slot != NULL)
8317 {
8318 tu_group = (struct type_unit_group *) *slot;
8319 gdb_assert (tu_group != NULL);
8320 }
8321 else
8322 {
8323 sect_offset line_offset_struct = (sect_offset) line_offset;
8324 tu_group = create_type_unit_group (cu, line_offset_struct);
8325 *slot = tu_group;
8326 ++tu_stats->nr_symtabs;
8327 }
8328
8329 return tu_group;
8330 }
8331 \f
8332 /* Partial symbol tables. */
8333
8334 /* Create a psymtab named NAME and assign it to PER_CU.
8335
8336 The caller must fill in the following details:
8337 dirname, textlow, texthigh. */
8338
8339 static struct partial_symtab *
8340 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8341 {
8342 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8343 struct partial_symtab *pst;
8344
8345 pst = start_psymtab_common (objfile, name, 0,
8346 objfile->global_psymbols,
8347 objfile->static_psymbols);
8348
8349 pst->psymtabs_addrmap_supported = 1;
8350
8351 /* This is the glue that links PST into GDB's symbol API. */
8352 pst->read_symtab_private = per_cu;
8353 pst->read_symtab = dwarf2_read_symtab;
8354 per_cu->v.psymtab = pst;
8355
8356 return pst;
8357 }
8358
8359 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8360 type. */
8361
8362 struct process_psymtab_comp_unit_data
8363 {
8364 /* True if we are reading a DW_TAG_partial_unit. */
8365
8366 int want_partial_unit;
8367
8368 /* The "pretend" language that is used if the CU doesn't declare a
8369 language. */
8370
8371 enum language pretend_language;
8372 };
8373
8374 /* die_reader_func for process_psymtab_comp_unit. */
8375
8376 static void
8377 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8378 const gdb_byte *info_ptr,
8379 struct die_info *comp_unit_die,
8380 int has_children,
8381 void *data)
8382 {
8383 struct dwarf2_cu *cu = reader->cu;
8384 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8385 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8386 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8387 CORE_ADDR baseaddr;
8388 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8389 struct partial_symtab *pst;
8390 enum pc_bounds_kind cu_bounds_kind;
8391 const char *filename;
8392 struct process_psymtab_comp_unit_data *info
8393 = (struct process_psymtab_comp_unit_data *) data;
8394
8395 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8396 return;
8397
8398 gdb_assert (! per_cu->is_debug_types);
8399
8400 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8401
8402 cu->list_in_scope = &file_symbols;
8403
8404 /* Allocate a new partial symbol table structure. */
8405 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8406 if (filename == NULL)
8407 filename = "";
8408
8409 pst = create_partial_symtab (per_cu, filename);
8410
8411 /* This must be done before calling dwarf2_build_include_psymtabs. */
8412 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8413
8414 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8415
8416 dwarf2_find_base_address (comp_unit_die, cu);
8417
8418 /* Possibly set the default values of LOWPC and HIGHPC from
8419 `DW_AT_ranges'. */
8420 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8421 &best_highpc, cu, pst);
8422 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8423 /* Store the contiguous range if it is not empty; it can be empty for
8424 CUs with no code. */
8425 addrmap_set_empty (objfile->psymtabs_addrmap,
8426 gdbarch_adjust_dwarf2_addr (gdbarch,
8427 best_lowpc + baseaddr),
8428 gdbarch_adjust_dwarf2_addr (gdbarch,
8429 best_highpc + baseaddr) - 1,
8430 pst);
8431
8432 /* Check if comp unit has_children.
8433 If so, read the rest of the partial symbols from this comp unit.
8434 If not, there's no more debug_info for this comp unit. */
8435 if (has_children)
8436 {
8437 struct partial_die_info *first_die;
8438 CORE_ADDR lowpc, highpc;
8439
8440 lowpc = ((CORE_ADDR) -1);
8441 highpc = ((CORE_ADDR) 0);
8442
8443 first_die = load_partial_dies (reader, info_ptr, 1);
8444
8445 scan_partial_symbols (first_die, &lowpc, &highpc,
8446 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8447
8448 /* If we didn't find a lowpc, set it to highpc to avoid
8449 complaints from `maint check'. */
8450 if (lowpc == ((CORE_ADDR) -1))
8451 lowpc = highpc;
8452
8453 /* If the compilation unit didn't have an explicit address range,
8454 then use the information extracted from its child dies. */
8455 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8456 {
8457 best_lowpc = lowpc;
8458 best_highpc = highpc;
8459 }
8460 }
8461 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8462 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
8463
8464 end_psymtab_common (objfile, pst);
8465
8466 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8467 {
8468 int i;
8469 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8470 struct dwarf2_per_cu_data *iter;
8471
8472 /* Fill in 'dependencies' here; we fill in 'users' in a
8473 post-pass. */
8474 pst->number_of_dependencies = len;
8475 pst->dependencies =
8476 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8477 for (i = 0;
8478 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8479 i, iter);
8480 ++i)
8481 pst->dependencies[i] = iter->v.psymtab;
8482
8483 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8484 }
8485
8486 /* Get the list of files included in the current compilation unit,
8487 and build a psymtab for each of them. */
8488 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8489
8490 if (dwarf_read_debug)
8491 {
8492 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8493
8494 fprintf_unfiltered (gdb_stdlog,
8495 "Psymtab for %s unit @%s: %s - %s"
8496 ", %d global, %d static syms\n",
8497 per_cu->is_debug_types ? "type" : "comp",
8498 sect_offset_str (per_cu->sect_off),
8499 paddress (gdbarch, pst->textlow),
8500 paddress (gdbarch, pst->texthigh),
8501 pst->n_global_syms, pst->n_static_syms);
8502 }
8503 }
8504
8505 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8506 Process compilation unit THIS_CU for a psymtab. */
8507
8508 static void
8509 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8510 int want_partial_unit,
8511 enum language pretend_language)
8512 {
8513 /* If this compilation unit was already read in, free the
8514 cached copy in order to read it in again. This is
8515 necessary because we skipped some symbols when we first
8516 read in the compilation unit (see load_partial_dies).
8517 This problem could be avoided, but the benefit is unclear. */
8518 if (this_cu->cu != NULL)
8519 free_one_cached_comp_unit (this_cu);
8520
8521 if (this_cu->is_debug_types)
8522 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8523 NULL);
8524 else
8525 {
8526 process_psymtab_comp_unit_data info;
8527 info.want_partial_unit = want_partial_unit;
8528 info.pretend_language = pretend_language;
8529 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8530 process_psymtab_comp_unit_reader, &info);
8531 }
8532
8533 /* Age out any secondary CUs. */
8534 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8535 }
8536
8537 /* Reader function for build_type_psymtabs. */
8538
8539 static void
8540 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8541 const gdb_byte *info_ptr,
8542 struct die_info *type_unit_die,
8543 int has_children,
8544 void *data)
8545 {
8546 struct dwarf2_per_objfile *dwarf2_per_objfile
8547 = reader->cu->per_cu->dwarf2_per_objfile;
8548 struct objfile *objfile = dwarf2_per_objfile->objfile;
8549 struct dwarf2_cu *cu = reader->cu;
8550 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8551 struct signatured_type *sig_type;
8552 struct type_unit_group *tu_group;
8553 struct attribute *attr;
8554 struct partial_die_info *first_die;
8555 CORE_ADDR lowpc, highpc;
8556 struct partial_symtab *pst;
8557
8558 gdb_assert (data == NULL);
8559 gdb_assert (per_cu->is_debug_types);
8560 sig_type = (struct signatured_type *) per_cu;
8561
8562 if (! has_children)
8563 return;
8564
8565 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8566 tu_group = get_type_unit_group (cu, attr);
8567
8568 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8569
8570 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8571 cu->list_in_scope = &file_symbols;
8572 pst = create_partial_symtab (per_cu, "");
8573 pst->anonymous = 1;
8574
8575 first_die = load_partial_dies (reader, info_ptr, 1);
8576
8577 lowpc = (CORE_ADDR) -1;
8578 highpc = (CORE_ADDR) 0;
8579 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8580
8581 end_psymtab_common (objfile, pst);
8582 }
8583
8584 /* Struct used to sort TUs by their abbreviation table offset. */
8585
8586 struct tu_abbrev_offset
8587 {
8588 struct signatured_type *sig_type;
8589 sect_offset abbrev_offset;
8590 };
8591
8592 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8593
8594 static bool
8595 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8596 const struct tu_abbrev_offset &b)
8597 {
8598 return a.abbrev_offset < b.abbrev_offset;
8599 }
8600
8601 /* Efficiently read all the type units.
8602 This does the bulk of the work for build_type_psymtabs.
8603
8604 The efficiency is because we sort TUs by the abbrev table they use and
8605 only read each abbrev table once. In one program there are 200K TUs
8606 sharing 8K abbrev tables.
8607
8608 The main purpose of this function is to support building the
8609 dwarf2_per_objfile->type_unit_groups table.
8610 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8611 can collapse the search space by grouping them by stmt_list.
8612 The savings can be significant, in the same program from above the 200K TUs
8613 share 8K stmt_list tables.
8614
8615 FUNC is expected to call get_type_unit_group, which will create the
8616 struct type_unit_group if necessary and add it to
8617 dwarf2_per_objfile->type_unit_groups. */
8618
8619 static void
8620 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8621 {
8622 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8623 abbrev_table_up abbrev_table;
8624 sect_offset abbrev_offset;
8625 int i;
8626
8627 /* It's up to the caller to not call us multiple times. */
8628 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8629
8630 if (dwarf2_per_objfile->n_type_units == 0)
8631 return;
8632
8633 /* TUs typically share abbrev tables, and there can be way more TUs than
8634 abbrev tables. Sort by abbrev table to reduce the number of times we
8635 read each abbrev table in.
8636 Alternatives are to punt or to maintain a cache of abbrev tables.
8637 This is simpler and efficient enough for now.
8638
8639 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8640 symtab to use). Typically TUs with the same abbrev offset have the same
8641 stmt_list value too so in practice this should work well.
8642
8643 The basic algorithm here is:
8644
8645 sort TUs by abbrev table
8646 for each TU with same abbrev table:
8647 read abbrev table if first user
8648 read TU top level DIE
8649 [IWBN if DWO skeletons had DW_AT_stmt_list]
8650 call FUNC */
8651
8652 if (dwarf_read_debug)
8653 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8654
8655 /* Sort in a separate table to maintain the order of all_type_units
8656 for .gdb_index: TU indices directly index all_type_units. */
8657 std::vector<struct tu_abbrev_offset> sorted_by_abbrev
8658 (dwarf2_per_objfile->n_type_units);
8659 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8660 {
8661 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8662
8663 sorted_by_abbrev[i].sig_type = sig_type;
8664 sorted_by_abbrev[i].abbrev_offset =
8665 read_abbrev_offset (dwarf2_per_objfile,
8666 sig_type->per_cu.section,
8667 sig_type->per_cu.sect_off);
8668 }
8669 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8670 sort_tu_by_abbrev_offset);
8671
8672 abbrev_offset = (sect_offset) ~(unsigned) 0;
8673
8674 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8675 {
8676 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8677
8678 /* Switch to the next abbrev table if necessary. */
8679 if (abbrev_table == NULL
8680 || tu->abbrev_offset != abbrev_offset)
8681 {
8682 abbrev_offset = tu->abbrev_offset;
8683 abbrev_table =
8684 abbrev_table_read_table (dwarf2_per_objfile,
8685 &dwarf2_per_objfile->abbrev,
8686 abbrev_offset);
8687 ++tu_stats->nr_uniq_abbrev_tables;
8688 }
8689
8690 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table.get (),
8691 0, 0, build_type_psymtabs_reader, NULL);
8692 }
8693 }
8694
8695 /* Print collected type unit statistics. */
8696
8697 static void
8698 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8699 {
8700 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8701
8702 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8703 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8704 dwarf2_per_objfile->n_type_units);
8705 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8706 tu_stats->nr_uniq_abbrev_tables);
8707 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8708 tu_stats->nr_symtabs);
8709 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8710 tu_stats->nr_symtab_sharers);
8711 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8712 tu_stats->nr_stmt_less_type_units);
8713 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8714 tu_stats->nr_all_type_units_reallocs);
8715 }
8716
8717 /* Traversal function for build_type_psymtabs. */
8718
8719 static int
8720 build_type_psymtab_dependencies (void **slot, void *info)
8721 {
8722 struct dwarf2_per_objfile *dwarf2_per_objfile
8723 = (struct dwarf2_per_objfile *) info;
8724 struct objfile *objfile = dwarf2_per_objfile->objfile;
8725 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8726 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8727 struct partial_symtab *pst = per_cu->v.psymtab;
8728 int len = VEC_length (sig_type_ptr, tu_group->tus);
8729 struct signatured_type *iter;
8730 int i;
8731
8732 gdb_assert (len > 0);
8733 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8734
8735 pst->number_of_dependencies = len;
8736 pst->dependencies =
8737 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8738 for (i = 0;
8739 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8740 ++i)
8741 {
8742 gdb_assert (iter->per_cu.is_debug_types);
8743 pst->dependencies[i] = iter->per_cu.v.psymtab;
8744 iter->type_unit_group = tu_group;
8745 }
8746
8747 VEC_free (sig_type_ptr, tu_group->tus);
8748
8749 return 1;
8750 }
8751
8752 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8753 Build partial symbol tables for the .debug_types comp-units. */
8754
8755 static void
8756 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8757 {
8758 if (! create_all_type_units (dwarf2_per_objfile))
8759 return;
8760
8761 build_type_psymtabs_1 (dwarf2_per_objfile);
8762 }
8763
8764 /* Traversal function for process_skeletonless_type_unit.
8765 Read a TU in a DWO file and build partial symbols for it. */
8766
8767 static int
8768 process_skeletonless_type_unit (void **slot, void *info)
8769 {
8770 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8771 struct dwarf2_per_objfile *dwarf2_per_objfile
8772 = (struct dwarf2_per_objfile *) info;
8773 struct signatured_type find_entry, *entry;
8774
8775 /* If this TU doesn't exist in the global table, add it and read it in. */
8776
8777 if (dwarf2_per_objfile->signatured_types == NULL)
8778 {
8779 dwarf2_per_objfile->signatured_types
8780 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8781 }
8782
8783 find_entry.signature = dwo_unit->signature;
8784 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8785 INSERT);
8786 /* If we've already seen this type there's nothing to do. What's happening
8787 is we're doing our own version of comdat-folding here. */
8788 if (*slot != NULL)
8789 return 1;
8790
8791 /* This does the job that create_all_type_units would have done for
8792 this TU. */
8793 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8794 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8795 *slot = entry;
8796
8797 /* This does the job that build_type_psymtabs_1 would have done. */
8798 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8799 build_type_psymtabs_reader, NULL);
8800
8801 return 1;
8802 }
8803
8804 /* Traversal function for process_skeletonless_type_units. */
8805
8806 static int
8807 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8808 {
8809 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8810
8811 if (dwo_file->tus != NULL)
8812 {
8813 htab_traverse_noresize (dwo_file->tus,
8814 process_skeletonless_type_unit, info);
8815 }
8816
8817 return 1;
8818 }
8819
8820 /* Scan all TUs of DWO files, verifying we've processed them.
8821 This is needed in case a TU was emitted without its skeleton.
8822 Note: This can't be done until we know what all the DWO files are. */
8823
8824 static void
8825 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8826 {
8827 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8828 if (get_dwp_file (dwarf2_per_objfile) == NULL
8829 && dwarf2_per_objfile->dwo_files != NULL)
8830 {
8831 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8832 process_dwo_file_for_skeletonless_type_units,
8833 dwarf2_per_objfile);
8834 }
8835 }
8836
8837 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8838
8839 static void
8840 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8841 {
8842 int i;
8843
8844 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8845 {
8846 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8847 struct partial_symtab *pst = per_cu->v.psymtab;
8848 int j;
8849
8850 if (pst == NULL)
8851 continue;
8852
8853 for (j = 0; j < pst->number_of_dependencies; ++j)
8854 {
8855 /* Set the 'user' field only if it is not already set. */
8856 if (pst->dependencies[j]->user == NULL)
8857 pst->dependencies[j]->user = pst;
8858 }
8859 }
8860 }
8861
8862 /* Build the partial symbol table by doing a quick pass through the
8863 .debug_info and .debug_abbrev sections. */
8864
8865 static void
8866 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8867 {
8868 struct cleanup *back_to;
8869 int i;
8870 struct objfile *objfile = dwarf2_per_objfile->objfile;
8871
8872 if (dwarf_read_debug)
8873 {
8874 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8875 objfile_name (objfile));
8876 }
8877
8878 dwarf2_per_objfile->reading_partial_symbols = 1;
8879
8880 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8881
8882 /* Any cached compilation units will be linked by the per-objfile
8883 read_in_chain. Make sure to free them when we're done. */
8884 back_to = make_cleanup (free_cached_comp_units, dwarf2_per_objfile);
8885
8886 build_type_psymtabs (dwarf2_per_objfile);
8887
8888 create_all_comp_units (dwarf2_per_objfile);
8889
8890 /* Create a temporary address map on a temporary obstack. We later
8891 copy this to the final obstack. */
8892 auto_obstack temp_obstack;
8893
8894 scoped_restore save_psymtabs_addrmap
8895 = make_scoped_restore (&objfile->psymtabs_addrmap,
8896 addrmap_create_mutable (&temp_obstack));
8897
8898 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8899 {
8900 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8901
8902 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8903 }
8904
8905 /* This has to wait until we read the CUs, we need the list of DWOs. */
8906 process_skeletonless_type_units (dwarf2_per_objfile);
8907
8908 /* Now that all TUs have been processed we can fill in the dependencies. */
8909 if (dwarf2_per_objfile->type_unit_groups != NULL)
8910 {
8911 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8912 build_type_psymtab_dependencies, dwarf2_per_objfile);
8913 }
8914
8915 if (dwarf_read_debug)
8916 print_tu_stats (dwarf2_per_objfile);
8917
8918 set_partial_user (dwarf2_per_objfile);
8919
8920 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8921 &objfile->objfile_obstack);
8922 /* At this point we want to keep the address map. */
8923 save_psymtabs_addrmap.release ();
8924
8925 do_cleanups (back_to);
8926
8927 if (dwarf_read_debug)
8928 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8929 objfile_name (objfile));
8930 }
8931
8932 /* die_reader_func for load_partial_comp_unit. */
8933
8934 static void
8935 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8936 const gdb_byte *info_ptr,
8937 struct die_info *comp_unit_die,
8938 int has_children,
8939 void *data)
8940 {
8941 struct dwarf2_cu *cu = reader->cu;
8942
8943 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8944
8945 /* Check if comp unit has_children.
8946 If so, read the rest of the partial symbols from this comp unit.
8947 If not, there's no more debug_info for this comp unit. */
8948 if (has_children)
8949 load_partial_dies (reader, info_ptr, 0);
8950 }
8951
8952 /* Load the partial DIEs for a secondary CU into memory.
8953 This is also used when rereading a primary CU with load_all_dies. */
8954
8955 static void
8956 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8957 {
8958 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8959 load_partial_comp_unit_reader, NULL);
8960 }
8961
8962 static void
8963 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8964 struct dwarf2_section_info *section,
8965 struct dwarf2_section_info *abbrev_section,
8966 unsigned int is_dwz,
8967 int *n_allocated,
8968 int *n_comp_units,
8969 struct dwarf2_per_cu_data ***all_comp_units)
8970 {
8971 const gdb_byte *info_ptr;
8972 struct objfile *objfile = dwarf2_per_objfile->objfile;
8973
8974 if (dwarf_read_debug)
8975 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8976 get_section_name (section),
8977 get_section_file_name (section));
8978
8979 dwarf2_read_section (objfile, section);
8980
8981 info_ptr = section->buffer;
8982
8983 while (info_ptr < section->buffer + section->size)
8984 {
8985 struct dwarf2_per_cu_data *this_cu;
8986
8987 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8988
8989 comp_unit_head cu_header;
8990 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8991 abbrev_section, info_ptr,
8992 rcuh_kind::COMPILE);
8993
8994 /* Save the compilation unit for later lookup. */
8995 if (cu_header.unit_type != DW_UT_type)
8996 {
8997 this_cu = XOBNEW (&objfile->objfile_obstack,
8998 struct dwarf2_per_cu_data);
8999 memset (this_cu, 0, sizeof (*this_cu));
9000 }
9001 else
9002 {
9003 auto sig_type = XOBNEW (&objfile->objfile_obstack,
9004 struct signatured_type);
9005 memset (sig_type, 0, sizeof (*sig_type));
9006 sig_type->signature = cu_header.signature;
9007 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
9008 this_cu = &sig_type->per_cu;
9009 }
9010 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9011 this_cu->sect_off = sect_off;
9012 this_cu->length = cu_header.length + cu_header.initial_length_size;
9013 this_cu->is_dwz = is_dwz;
9014 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
9015 this_cu->section = section;
9016
9017 if (*n_comp_units == *n_allocated)
9018 {
9019 *n_allocated *= 2;
9020 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
9021 *all_comp_units, *n_allocated);
9022 }
9023 (*all_comp_units)[*n_comp_units] = this_cu;
9024 ++*n_comp_units;
9025
9026 info_ptr = info_ptr + this_cu->length;
9027 }
9028 }
9029
9030 /* Create a list of all compilation units in OBJFILE.
9031 This is only done for -readnow and building partial symtabs. */
9032
9033 static void
9034 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
9035 {
9036 int n_allocated;
9037 int n_comp_units;
9038 struct dwarf2_per_cu_data **all_comp_units;
9039 struct dwz_file *dwz;
9040 struct objfile *objfile = dwarf2_per_objfile->objfile;
9041
9042 n_comp_units = 0;
9043 n_allocated = 10;
9044 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
9045
9046 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
9047 &dwarf2_per_objfile->abbrev, 0,
9048 &n_allocated, &n_comp_units, &all_comp_units);
9049
9050 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
9051 if (dwz != NULL)
9052 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
9053 1, &n_allocated, &n_comp_units,
9054 &all_comp_units);
9055
9056 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
9057 struct dwarf2_per_cu_data *,
9058 n_comp_units);
9059 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
9060 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
9061 xfree (all_comp_units);
9062 dwarf2_per_objfile->n_comp_units = n_comp_units;
9063 }
9064
9065 /* Process all loaded DIEs for compilation unit CU, starting at
9066 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
9067 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
9068 DW_AT_ranges). See the comments of add_partial_subprogram on how
9069 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
9070
9071 static void
9072 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
9073 CORE_ADDR *highpc, int set_addrmap,
9074 struct dwarf2_cu *cu)
9075 {
9076 struct partial_die_info *pdi;
9077
9078 /* Now, march along the PDI's, descending into ones which have
9079 interesting children but skipping the children of the other ones,
9080 until we reach the end of the compilation unit. */
9081
9082 pdi = first_die;
9083
9084 while (pdi != NULL)
9085 {
9086 pdi->fixup (cu);
9087
9088 /* Anonymous namespaces or modules have no name but have interesting
9089 children, so we need to look at them. Ditto for anonymous
9090 enums. */
9091
9092 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
9093 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
9094 || pdi->tag == DW_TAG_imported_unit
9095 || pdi->tag == DW_TAG_inlined_subroutine)
9096 {
9097 switch (pdi->tag)
9098 {
9099 case DW_TAG_subprogram:
9100 case DW_TAG_inlined_subroutine:
9101 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9102 break;
9103 case DW_TAG_constant:
9104 case DW_TAG_variable:
9105 case DW_TAG_typedef:
9106 case DW_TAG_union_type:
9107 if (!pdi->is_declaration)
9108 {
9109 add_partial_symbol (pdi, cu);
9110 }
9111 break;
9112 case DW_TAG_class_type:
9113 case DW_TAG_interface_type:
9114 case DW_TAG_structure_type:
9115 if (!pdi->is_declaration)
9116 {
9117 add_partial_symbol (pdi, cu);
9118 }
9119 if ((cu->language == language_rust
9120 || cu->language == language_cplus) && pdi->has_children)
9121 scan_partial_symbols (pdi->die_child, lowpc, highpc,
9122 set_addrmap, cu);
9123 break;
9124 case DW_TAG_enumeration_type:
9125 if (!pdi->is_declaration)
9126 add_partial_enumeration (pdi, cu);
9127 break;
9128 case DW_TAG_base_type:
9129 case DW_TAG_subrange_type:
9130 /* File scope base type definitions are added to the partial
9131 symbol table. */
9132 add_partial_symbol (pdi, cu);
9133 break;
9134 case DW_TAG_namespace:
9135 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
9136 break;
9137 case DW_TAG_module:
9138 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
9139 break;
9140 case DW_TAG_imported_unit:
9141 {
9142 struct dwarf2_per_cu_data *per_cu;
9143
9144 /* For now we don't handle imported units in type units. */
9145 if (cu->per_cu->is_debug_types)
9146 {
9147 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9148 " supported in type units [in module %s]"),
9149 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9150 }
9151
9152 per_cu = dwarf2_find_containing_comp_unit
9153 (pdi->d.sect_off, pdi->is_dwz,
9154 cu->per_cu->dwarf2_per_objfile);
9155
9156 /* Go read the partial unit, if needed. */
9157 if (per_cu->v.psymtab == NULL)
9158 process_psymtab_comp_unit (per_cu, 1, cu->language);
9159
9160 VEC_safe_push (dwarf2_per_cu_ptr,
9161 cu->per_cu->imported_symtabs, per_cu);
9162 }
9163 break;
9164 case DW_TAG_imported_declaration:
9165 add_partial_symbol (pdi, cu);
9166 break;
9167 default:
9168 break;
9169 }
9170 }
9171
9172 /* If the die has a sibling, skip to the sibling. */
9173
9174 pdi = pdi->die_sibling;
9175 }
9176 }
9177
9178 /* Functions used to compute the fully scoped name of a partial DIE.
9179
9180 Normally, this is simple. For C++, the parent DIE's fully scoped
9181 name is concatenated with "::" and the partial DIE's name.
9182 Enumerators are an exception; they use the scope of their parent
9183 enumeration type, i.e. the name of the enumeration type is not
9184 prepended to the enumerator.
9185
9186 There are two complexities. One is DW_AT_specification; in this
9187 case "parent" means the parent of the target of the specification,
9188 instead of the direct parent of the DIE. The other is compilers
9189 which do not emit DW_TAG_namespace; in this case we try to guess
9190 the fully qualified name of structure types from their members'
9191 linkage names. This must be done using the DIE's children rather
9192 than the children of any DW_AT_specification target. We only need
9193 to do this for structures at the top level, i.e. if the target of
9194 any DW_AT_specification (if any; otherwise the DIE itself) does not
9195 have a parent. */
9196
9197 /* Compute the scope prefix associated with PDI's parent, in
9198 compilation unit CU. The result will be allocated on CU's
9199 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9200 field. NULL is returned if no prefix is necessary. */
9201 static const char *
9202 partial_die_parent_scope (struct partial_die_info *pdi,
9203 struct dwarf2_cu *cu)
9204 {
9205 const char *grandparent_scope;
9206 struct partial_die_info *parent, *real_pdi;
9207
9208 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9209 then this means the parent of the specification DIE. */
9210
9211 real_pdi = pdi;
9212 while (real_pdi->has_specification)
9213 real_pdi = find_partial_die (real_pdi->spec_offset,
9214 real_pdi->spec_is_dwz, cu);
9215
9216 parent = real_pdi->die_parent;
9217 if (parent == NULL)
9218 return NULL;
9219
9220 if (parent->scope_set)
9221 return parent->scope;
9222
9223 parent->fixup (cu);
9224
9225 grandparent_scope = partial_die_parent_scope (parent, cu);
9226
9227 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9228 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9229 Work around this problem here. */
9230 if (cu->language == language_cplus
9231 && parent->tag == DW_TAG_namespace
9232 && strcmp (parent->name, "::") == 0
9233 && grandparent_scope == NULL)
9234 {
9235 parent->scope = NULL;
9236 parent->scope_set = 1;
9237 return NULL;
9238 }
9239
9240 if (pdi->tag == DW_TAG_enumerator)
9241 /* Enumerators should not get the name of the enumeration as a prefix. */
9242 parent->scope = grandparent_scope;
9243 else if (parent->tag == DW_TAG_namespace
9244 || parent->tag == DW_TAG_module
9245 || parent->tag == DW_TAG_structure_type
9246 || parent->tag == DW_TAG_class_type
9247 || parent->tag == DW_TAG_interface_type
9248 || parent->tag == DW_TAG_union_type
9249 || parent->tag == DW_TAG_enumeration_type)
9250 {
9251 if (grandparent_scope == NULL)
9252 parent->scope = parent->name;
9253 else
9254 parent->scope = typename_concat (&cu->comp_unit_obstack,
9255 grandparent_scope,
9256 parent->name, 0, cu);
9257 }
9258 else
9259 {
9260 /* FIXME drow/2004-04-01: What should we be doing with
9261 function-local names? For partial symbols, we should probably be
9262 ignoring them. */
9263 complaint (&symfile_complaints,
9264 _("unhandled containing DIE tag %d for DIE at %s"),
9265 parent->tag, sect_offset_str (pdi->sect_off));
9266 parent->scope = grandparent_scope;
9267 }
9268
9269 parent->scope_set = 1;
9270 return parent->scope;
9271 }
9272
9273 /* Return the fully scoped name associated with PDI, from compilation unit
9274 CU. The result will be allocated with malloc. */
9275
9276 static char *
9277 partial_die_full_name (struct partial_die_info *pdi,
9278 struct dwarf2_cu *cu)
9279 {
9280 const char *parent_scope;
9281
9282 /* If this is a template instantiation, we can not work out the
9283 template arguments from partial DIEs. So, unfortunately, we have
9284 to go through the full DIEs. At least any work we do building
9285 types here will be reused if full symbols are loaded later. */
9286 if (pdi->has_template_arguments)
9287 {
9288 pdi->fixup (cu);
9289
9290 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9291 {
9292 struct die_info *die;
9293 struct attribute attr;
9294 struct dwarf2_cu *ref_cu = cu;
9295
9296 /* DW_FORM_ref_addr is using section offset. */
9297 attr.name = (enum dwarf_attribute) 0;
9298 attr.form = DW_FORM_ref_addr;
9299 attr.u.unsnd = to_underlying (pdi->sect_off);
9300 die = follow_die_ref (NULL, &attr, &ref_cu);
9301
9302 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9303 }
9304 }
9305
9306 parent_scope = partial_die_parent_scope (pdi, cu);
9307 if (parent_scope == NULL)
9308 return NULL;
9309 else
9310 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
9311 }
9312
9313 static void
9314 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
9315 {
9316 struct dwarf2_per_objfile *dwarf2_per_objfile
9317 = cu->per_cu->dwarf2_per_objfile;
9318 struct objfile *objfile = dwarf2_per_objfile->objfile;
9319 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9320 CORE_ADDR addr = 0;
9321 const char *actual_name = NULL;
9322 CORE_ADDR baseaddr;
9323 char *built_actual_name;
9324
9325 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9326
9327 built_actual_name = partial_die_full_name (pdi, cu);
9328 if (built_actual_name != NULL)
9329 actual_name = built_actual_name;
9330
9331 if (actual_name == NULL)
9332 actual_name = pdi->name;
9333
9334 switch (pdi->tag)
9335 {
9336 case DW_TAG_inlined_subroutine:
9337 case DW_TAG_subprogram:
9338 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
9339 if (pdi->is_external || cu->language == language_ada)
9340 {
9341 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9342 of the global scope. But in Ada, we want to be able to access
9343 nested procedures globally. So all Ada subprograms are stored
9344 in the global scope. */
9345 add_psymbol_to_list (actual_name, strlen (actual_name),
9346 built_actual_name != NULL,
9347 VAR_DOMAIN, LOC_BLOCK,
9348 &objfile->global_psymbols,
9349 addr, cu->language, objfile);
9350 }
9351 else
9352 {
9353 add_psymbol_to_list (actual_name, strlen (actual_name),
9354 built_actual_name != NULL,
9355 VAR_DOMAIN, LOC_BLOCK,
9356 &objfile->static_psymbols,
9357 addr, cu->language, objfile);
9358 }
9359
9360 if (pdi->main_subprogram && actual_name != NULL)
9361 set_objfile_main_name (objfile, actual_name, cu->language);
9362 break;
9363 case DW_TAG_constant:
9364 {
9365 std::vector<partial_symbol *> *list;
9366
9367 if (pdi->is_external)
9368 list = &objfile->global_psymbols;
9369 else
9370 list = &objfile->static_psymbols;
9371 add_psymbol_to_list (actual_name, strlen (actual_name),
9372 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9373 list, 0, cu->language, objfile);
9374 }
9375 break;
9376 case DW_TAG_variable:
9377 if (pdi->d.locdesc)
9378 addr = decode_locdesc (pdi->d.locdesc, cu);
9379
9380 if (pdi->d.locdesc
9381 && addr == 0
9382 && !dwarf2_per_objfile->has_section_at_zero)
9383 {
9384 /* A global or static variable may also have been stripped
9385 out by the linker if unused, in which case its address
9386 will be nullified; do not add such variables into partial
9387 symbol table then. */
9388 }
9389 else if (pdi->is_external)
9390 {
9391 /* Global Variable.
9392 Don't enter into the minimal symbol tables as there is
9393 a minimal symbol table entry from the ELF symbols already.
9394 Enter into partial symbol table if it has a location
9395 descriptor or a type.
9396 If the location descriptor is missing, new_symbol will create
9397 a LOC_UNRESOLVED symbol, the address of the variable will then
9398 be determined from the minimal symbol table whenever the variable
9399 is referenced.
9400 The address for the partial symbol table entry is not
9401 used by GDB, but it comes in handy for debugging partial symbol
9402 table building. */
9403
9404 if (pdi->d.locdesc || pdi->has_type)
9405 add_psymbol_to_list (actual_name, strlen (actual_name),
9406 built_actual_name != NULL,
9407 VAR_DOMAIN, LOC_STATIC,
9408 &objfile->global_psymbols,
9409 addr + baseaddr,
9410 cu->language, objfile);
9411 }
9412 else
9413 {
9414 int has_loc = pdi->d.locdesc != NULL;
9415
9416 /* Static Variable. Skip symbols whose value we cannot know (those
9417 without location descriptors or constant values). */
9418 if (!has_loc && !pdi->has_const_value)
9419 {
9420 xfree (built_actual_name);
9421 return;
9422 }
9423
9424 add_psymbol_to_list (actual_name, strlen (actual_name),
9425 built_actual_name != NULL,
9426 VAR_DOMAIN, LOC_STATIC,
9427 &objfile->static_psymbols,
9428 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
9429 cu->language, objfile);
9430 }
9431 break;
9432 case DW_TAG_typedef:
9433 case DW_TAG_base_type:
9434 case DW_TAG_subrange_type:
9435 add_psymbol_to_list (actual_name, strlen (actual_name),
9436 built_actual_name != NULL,
9437 VAR_DOMAIN, LOC_TYPEDEF,
9438 &objfile->static_psymbols,
9439 0, cu->language, objfile);
9440 break;
9441 case DW_TAG_imported_declaration:
9442 case DW_TAG_namespace:
9443 add_psymbol_to_list (actual_name, strlen (actual_name),
9444 built_actual_name != NULL,
9445 VAR_DOMAIN, LOC_TYPEDEF,
9446 &objfile->global_psymbols,
9447 0, cu->language, objfile);
9448 break;
9449 case DW_TAG_module:
9450 add_psymbol_to_list (actual_name, strlen (actual_name),
9451 built_actual_name != NULL,
9452 MODULE_DOMAIN, LOC_TYPEDEF,
9453 &objfile->global_psymbols,
9454 0, cu->language, objfile);
9455 break;
9456 case DW_TAG_class_type:
9457 case DW_TAG_interface_type:
9458 case DW_TAG_structure_type:
9459 case DW_TAG_union_type:
9460 case DW_TAG_enumeration_type:
9461 /* Skip external references. The DWARF standard says in the section
9462 about "Structure, Union, and Class Type Entries": "An incomplete
9463 structure, union or class type is represented by a structure,
9464 union or class entry that does not have a byte size attribute
9465 and that has a DW_AT_declaration attribute." */
9466 if (!pdi->has_byte_size && pdi->is_declaration)
9467 {
9468 xfree (built_actual_name);
9469 return;
9470 }
9471
9472 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9473 static vs. global. */
9474 add_psymbol_to_list (actual_name, strlen (actual_name),
9475 built_actual_name != NULL,
9476 STRUCT_DOMAIN, LOC_TYPEDEF,
9477 cu->language == language_cplus
9478 ? &objfile->global_psymbols
9479 : &objfile->static_psymbols,
9480 0, cu->language, objfile);
9481
9482 break;
9483 case DW_TAG_enumerator:
9484 add_psymbol_to_list (actual_name, strlen (actual_name),
9485 built_actual_name != NULL,
9486 VAR_DOMAIN, LOC_CONST,
9487 cu->language == language_cplus
9488 ? &objfile->global_psymbols
9489 : &objfile->static_psymbols,
9490 0, cu->language, objfile);
9491 break;
9492 default:
9493 break;
9494 }
9495
9496 xfree (built_actual_name);
9497 }
9498
9499 /* Read a partial die corresponding to a namespace; also, add a symbol
9500 corresponding to that namespace to the symbol table. NAMESPACE is
9501 the name of the enclosing namespace. */
9502
9503 static void
9504 add_partial_namespace (struct partial_die_info *pdi,
9505 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9506 int set_addrmap, struct dwarf2_cu *cu)
9507 {
9508 /* Add a symbol for the namespace. */
9509
9510 add_partial_symbol (pdi, cu);
9511
9512 /* Now scan partial symbols in that namespace. */
9513
9514 if (pdi->has_children)
9515 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9516 }
9517
9518 /* Read a partial die corresponding to a Fortran module. */
9519
9520 static void
9521 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9522 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9523 {
9524 /* Add a symbol for the namespace. */
9525
9526 add_partial_symbol (pdi, cu);
9527
9528 /* Now scan partial symbols in that module. */
9529
9530 if (pdi->has_children)
9531 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9532 }
9533
9534 /* Read a partial die corresponding to a subprogram or an inlined
9535 subprogram and create a partial symbol for that subprogram.
9536 When the CU language allows it, this routine also defines a partial
9537 symbol for each nested subprogram that this subprogram contains.
9538 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9539 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9540
9541 PDI may also be a lexical block, in which case we simply search
9542 recursively for subprograms defined inside that lexical block.
9543 Again, this is only performed when the CU language allows this
9544 type of definitions. */
9545
9546 static void
9547 add_partial_subprogram (struct partial_die_info *pdi,
9548 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9549 int set_addrmap, struct dwarf2_cu *cu)
9550 {
9551 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9552 {
9553 if (pdi->has_pc_info)
9554 {
9555 if (pdi->lowpc < *lowpc)
9556 *lowpc = pdi->lowpc;
9557 if (pdi->highpc > *highpc)
9558 *highpc = pdi->highpc;
9559 if (set_addrmap)
9560 {
9561 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9562 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9563 CORE_ADDR baseaddr;
9564 CORE_ADDR highpc;
9565 CORE_ADDR lowpc;
9566
9567 baseaddr = ANOFFSET (objfile->section_offsets,
9568 SECT_OFF_TEXT (objfile));
9569 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9570 pdi->lowpc + baseaddr);
9571 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9572 pdi->highpc + baseaddr);
9573 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9574 cu->per_cu->v.psymtab);
9575 }
9576 }
9577
9578 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9579 {
9580 if (!pdi->is_declaration)
9581 /* Ignore subprogram DIEs that do not have a name, they are
9582 illegal. Do not emit a complaint at this point, we will
9583 do so when we convert this psymtab into a symtab. */
9584 if (pdi->name)
9585 add_partial_symbol (pdi, cu);
9586 }
9587 }
9588
9589 if (! pdi->has_children)
9590 return;
9591
9592 if (cu->language == language_ada)
9593 {
9594 pdi = pdi->die_child;
9595 while (pdi != NULL)
9596 {
9597 pdi->fixup (cu);
9598 if (pdi->tag == DW_TAG_subprogram
9599 || pdi->tag == DW_TAG_inlined_subroutine
9600 || pdi->tag == DW_TAG_lexical_block)
9601 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9602 pdi = pdi->die_sibling;
9603 }
9604 }
9605 }
9606
9607 /* Read a partial die corresponding to an enumeration type. */
9608
9609 static void
9610 add_partial_enumeration (struct partial_die_info *enum_pdi,
9611 struct dwarf2_cu *cu)
9612 {
9613 struct partial_die_info *pdi;
9614
9615 if (enum_pdi->name != NULL)
9616 add_partial_symbol (enum_pdi, cu);
9617
9618 pdi = enum_pdi->die_child;
9619 while (pdi)
9620 {
9621 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9622 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9623 else
9624 add_partial_symbol (pdi, cu);
9625 pdi = pdi->die_sibling;
9626 }
9627 }
9628
9629 /* Return the initial uleb128 in the die at INFO_PTR. */
9630
9631 static unsigned int
9632 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9633 {
9634 unsigned int bytes_read;
9635
9636 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9637 }
9638
9639 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9640 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9641
9642 Return the corresponding abbrev, or NULL if the number is zero (indicating
9643 an empty DIE). In either case *BYTES_READ will be set to the length of
9644 the initial number. */
9645
9646 static struct abbrev_info *
9647 peek_die_abbrev (const die_reader_specs &reader,
9648 const gdb_byte *info_ptr, unsigned int *bytes_read)
9649 {
9650 dwarf2_cu *cu = reader.cu;
9651 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9652 unsigned int abbrev_number
9653 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9654
9655 if (abbrev_number == 0)
9656 return NULL;
9657
9658 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9659 if (!abbrev)
9660 {
9661 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9662 " at offset %s [in module %s]"),
9663 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9664 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9665 }
9666
9667 return abbrev;
9668 }
9669
9670 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9671 Returns a pointer to the end of a series of DIEs, terminated by an empty
9672 DIE. Any children of the skipped DIEs will also be skipped. */
9673
9674 static const gdb_byte *
9675 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9676 {
9677 while (1)
9678 {
9679 unsigned int bytes_read;
9680 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9681
9682 if (abbrev == NULL)
9683 return info_ptr + bytes_read;
9684 else
9685 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9686 }
9687 }
9688
9689 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9690 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9691 abbrev corresponding to that skipped uleb128 should be passed in
9692 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9693 children. */
9694
9695 static const gdb_byte *
9696 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9697 struct abbrev_info *abbrev)
9698 {
9699 unsigned int bytes_read;
9700 struct attribute attr;
9701 bfd *abfd = reader->abfd;
9702 struct dwarf2_cu *cu = reader->cu;
9703 const gdb_byte *buffer = reader->buffer;
9704 const gdb_byte *buffer_end = reader->buffer_end;
9705 unsigned int form, i;
9706
9707 for (i = 0; i < abbrev->num_attrs; i++)
9708 {
9709 /* The only abbrev we care about is DW_AT_sibling. */
9710 if (abbrev->attrs[i].name == DW_AT_sibling)
9711 {
9712 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9713 if (attr.form == DW_FORM_ref_addr)
9714 complaint (&symfile_complaints,
9715 _("ignoring absolute DW_AT_sibling"));
9716 else
9717 {
9718 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9719 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9720
9721 if (sibling_ptr < info_ptr)
9722 complaint (&symfile_complaints,
9723 _("DW_AT_sibling points backwards"));
9724 else if (sibling_ptr > reader->buffer_end)
9725 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9726 else
9727 return sibling_ptr;
9728 }
9729 }
9730
9731 /* If it isn't DW_AT_sibling, skip this attribute. */
9732 form = abbrev->attrs[i].form;
9733 skip_attribute:
9734 switch (form)
9735 {
9736 case DW_FORM_ref_addr:
9737 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9738 and later it is offset sized. */
9739 if (cu->header.version == 2)
9740 info_ptr += cu->header.addr_size;
9741 else
9742 info_ptr += cu->header.offset_size;
9743 break;
9744 case DW_FORM_GNU_ref_alt:
9745 info_ptr += cu->header.offset_size;
9746 break;
9747 case DW_FORM_addr:
9748 info_ptr += cu->header.addr_size;
9749 break;
9750 case DW_FORM_data1:
9751 case DW_FORM_ref1:
9752 case DW_FORM_flag:
9753 info_ptr += 1;
9754 break;
9755 case DW_FORM_flag_present:
9756 case DW_FORM_implicit_const:
9757 break;
9758 case DW_FORM_data2:
9759 case DW_FORM_ref2:
9760 info_ptr += 2;
9761 break;
9762 case DW_FORM_data4:
9763 case DW_FORM_ref4:
9764 info_ptr += 4;
9765 break;
9766 case DW_FORM_data8:
9767 case DW_FORM_ref8:
9768 case DW_FORM_ref_sig8:
9769 info_ptr += 8;
9770 break;
9771 case DW_FORM_data16:
9772 info_ptr += 16;
9773 break;
9774 case DW_FORM_string:
9775 read_direct_string (abfd, info_ptr, &bytes_read);
9776 info_ptr += bytes_read;
9777 break;
9778 case DW_FORM_sec_offset:
9779 case DW_FORM_strp:
9780 case DW_FORM_GNU_strp_alt:
9781 info_ptr += cu->header.offset_size;
9782 break;
9783 case DW_FORM_exprloc:
9784 case DW_FORM_block:
9785 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9786 info_ptr += bytes_read;
9787 break;
9788 case DW_FORM_block1:
9789 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9790 break;
9791 case DW_FORM_block2:
9792 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9793 break;
9794 case DW_FORM_block4:
9795 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9796 break;
9797 case DW_FORM_sdata:
9798 case DW_FORM_udata:
9799 case DW_FORM_ref_udata:
9800 case DW_FORM_GNU_addr_index:
9801 case DW_FORM_GNU_str_index:
9802 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9803 break;
9804 case DW_FORM_indirect:
9805 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9806 info_ptr += bytes_read;
9807 /* We need to continue parsing from here, so just go back to
9808 the top. */
9809 goto skip_attribute;
9810
9811 default:
9812 error (_("Dwarf Error: Cannot handle %s "
9813 "in DWARF reader [in module %s]"),
9814 dwarf_form_name (form),
9815 bfd_get_filename (abfd));
9816 }
9817 }
9818
9819 if (abbrev->has_children)
9820 return skip_children (reader, info_ptr);
9821 else
9822 return info_ptr;
9823 }
9824
9825 /* Locate ORIG_PDI's sibling.
9826 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9827
9828 static const gdb_byte *
9829 locate_pdi_sibling (const struct die_reader_specs *reader,
9830 struct partial_die_info *orig_pdi,
9831 const gdb_byte *info_ptr)
9832 {
9833 /* Do we know the sibling already? */
9834
9835 if (orig_pdi->sibling)
9836 return orig_pdi->sibling;
9837
9838 /* Are there any children to deal with? */
9839
9840 if (!orig_pdi->has_children)
9841 return info_ptr;
9842
9843 /* Skip the children the long way. */
9844
9845 return skip_children (reader, info_ptr);
9846 }
9847
9848 /* Expand this partial symbol table into a full symbol table. SELF is
9849 not NULL. */
9850
9851 static void
9852 dwarf2_read_symtab (struct partial_symtab *self,
9853 struct objfile *objfile)
9854 {
9855 struct dwarf2_per_objfile *dwarf2_per_objfile
9856 = get_dwarf2_per_objfile (objfile);
9857
9858 if (self->readin)
9859 {
9860 warning (_("bug: psymtab for %s is already read in."),
9861 self->filename);
9862 }
9863 else
9864 {
9865 if (info_verbose)
9866 {
9867 printf_filtered (_("Reading in symbols for %s..."),
9868 self->filename);
9869 gdb_flush (gdb_stdout);
9870 }
9871
9872 /* If this psymtab is constructed from a debug-only objfile, the
9873 has_section_at_zero flag will not necessarily be correct. We
9874 can get the correct value for this flag by looking at the data
9875 associated with the (presumably stripped) associated objfile. */
9876 if (objfile->separate_debug_objfile_backlink)
9877 {
9878 struct dwarf2_per_objfile *dpo_backlink
9879 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9880
9881 dwarf2_per_objfile->has_section_at_zero
9882 = dpo_backlink->has_section_at_zero;
9883 }
9884
9885 dwarf2_per_objfile->reading_partial_symbols = 0;
9886
9887 psymtab_to_symtab_1 (self);
9888
9889 /* Finish up the debug error message. */
9890 if (info_verbose)
9891 printf_filtered (_("done.\n"));
9892 }
9893
9894 process_cu_includes (dwarf2_per_objfile);
9895 }
9896 \f
9897 /* Reading in full CUs. */
9898
9899 /* Add PER_CU to the queue. */
9900
9901 static void
9902 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9903 enum language pretend_language)
9904 {
9905 struct dwarf2_queue_item *item;
9906
9907 per_cu->queued = 1;
9908 item = XNEW (struct dwarf2_queue_item);
9909 item->per_cu = per_cu;
9910 item->pretend_language = pretend_language;
9911 item->next = NULL;
9912
9913 if (dwarf2_queue == NULL)
9914 dwarf2_queue = item;
9915 else
9916 dwarf2_queue_tail->next = item;
9917
9918 dwarf2_queue_tail = item;
9919 }
9920
9921 /* If PER_CU is not yet queued, add it to the queue.
9922 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9923 dependency.
9924 The result is non-zero if PER_CU was queued, otherwise the result is zero
9925 meaning either PER_CU is already queued or it is already loaded.
9926
9927 N.B. There is an invariant here that if a CU is queued then it is loaded.
9928 The caller is required to load PER_CU if we return non-zero. */
9929
9930 static int
9931 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9932 struct dwarf2_per_cu_data *per_cu,
9933 enum language pretend_language)
9934 {
9935 /* We may arrive here during partial symbol reading, if we need full
9936 DIEs to process an unusual case (e.g. template arguments). Do
9937 not queue PER_CU, just tell our caller to load its DIEs. */
9938 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9939 {
9940 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9941 return 1;
9942 return 0;
9943 }
9944
9945 /* Mark the dependence relation so that we don't flush PER_CU
9946 too early. */
9947 if (dependent_cu != NULL)
9948 dwarf2_add_dependence (dependent_cu, per_cu);
9949
9950 /* If it's already on the queue, we have nothing to do. */
9951 if (per_cu->queued)
9952 return 0;
9953
9954 /* If the compilation unit is already loaded, just mark it as
9955 used. */
9956 if (per_cu->cu != NULL)
9957 {
9958 per_cu->cu->last_used = 0;
9959 return 0;
9960 }
9961
9962 /* Add it to the queue. */
9963 queue_comp_unit (per_cu, pretend_language);
9964
9965 return 1;
9966 }
9967
9968 /* Process the queue. */
9969
9970 static void
9971 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9972 {
9973 struct dwarf2_queue_item *item, *next_item;
9974
9975 if (dwarf_read_debug)
9976 {
9977 fprintf_unfiltered (gdb_stdlog,
9978 "Expanding one or more symtabs of objfile %s ...\n",
9979 objfile_name (dwarf2_per_objfile->objfile));
9980 }
9981
9982 /* The queue starts out with one item, but following a DIE reference
9983 may load a new CU, adding it to the end of the queue. */
9984 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9985 {
9986 if ((dwarf2_per_objfile->using_index
9987 ? !item->per_cu->v.quick->compunit_symtab
9988 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9989 /* Skip dummy CUs. */
9990 && item->per_cu->cu != NULL)
9991 {
9992 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9993 unsigned int debug_print_threshold;
9994 char buf[100];
9995
9996 if (per_cu->is_debug_types)
9997 {
9998 struct signatured_type *sig_type =
9999 (struct signatured_type *) per_cu;
10000
10001 sprintf (buf, "TU %s at offset %s",
10002 hex_string (sig_type->signature),
10003 sect_offset_str (per_cu->sect_off));
10004 /* There can be 100s of TUs.
10005 Only print them in verbose mode. */
10006 debug_print_threshold = 2;
10007 }
10008 else
10009 {
10010 sprintf (buf, "CU at offset %s",
10011 sect_offset_str (per_cu->sect_off));
10012 debug_print_threshold = 1;
10013 }
10014
10015 if (dwarf_read_debug >= debug_print_threshold)
10016 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
10017
10018 if (per_cu->is_debug_types)
10019 process_full_type_unit (per_cu, item->pretend_language);
10020 else
10021 process_full_comp_unit (per_cu, item->pretend_language);
10022
10023 if (dwarf_read_debug >= debug_print_threshold)
10024 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
10025 }
10026
10027 item->per_cu->queued = 0;
10028 next_item = item->next;
10029 xfree (item);
10030 }
10031
10032 dwarf2_queue_tail = NULL;
10033
10034 if (dwarf_read_debug)
10035 {
10036 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
10037 objfile_name (dwarf2_per_objfile->objfile));
10038 }
10039 }
10040
10041 /* Read in full symbols for PST, and anything it depends on. */
10042
10043 static void
10044 psymtab_to_symtab_1 (struct partial_symtab *pst)
10045 {
10046 struct dwarf2_per_cu_data *per_cu;
10047 int i;
10048
10049 if (pst->readin)
10050 return;
10051
10052 for (i = 0; i < pst->number_of_dependencies; i++)
10053 if (!pst->dependencies[i]->readin
10054 && pst->dependencies[i]->user == NULL)
10055 {
10056 /* Inform about additional files that need to be read in. */
10057 if (info_verbose)
10058 {
10059 /* FIXME: i18n: Need to make this a single string. */
10060 fputs_filtered (" ", gdb_stdout);
10061 wrap_here ("");
10062 fputs_filtered ("and ", gdb_stdout);
10063 wrap_here ("");
10064 printf_filtered ("%s...", pst->dependencies[i]->filename);
10065 wrap_here (""); /* Flush output. */
10066 gdb_flush (gdb_stdout);
10067 }
10068 psymtab_to_symtab_1 (pst->dependencies[i]);
10069 }
10070
10071 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10072
10073 if (per_cu == NULL)
10074 {
10075 /* It's an include file, no symbols to read for it.
10076 Everything is in the parent symtab. */
10077 pst->readin = 1;
10078 return;
10079 }
10080
10081 dw2_do_instantiate_symtab (per_cu);
10082 }
10083
10084 /* Trivial hash function for die_info: the hash value of a DIE
10085 is its offset in .debug_info for this objfile. */
10086
10087 static hashval_t
10088 die_hash (const void *item)
10089 {
10090 const struct die_info *die = (const struct die_info *) item;
10091
10092 return to_underlying (die->sect_off);
10093 }
10094
10095 /* Trivial comparison function for die_info structures: two DIEs
10096 are equal if they have the same offset. */
10097
10098 static int
10099 die_eq (const void *item_lhs, const void *item_rhs)
10100 {
10101 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
10102 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
10103
10104 return die_lhs->sect_off == die_rhs->sect_off;
10105 }
10106
10107 /* die_reader_func for load_full_comp_unit.
10108 This is identical to read_signatured_type_reader,
10109 but is kept separate for now. */
10110
10111 static void
10112 load_full_comp_unit_reader (const struct die_reader_specs *reader,
10113 const gdb_byte *info_ptr,
10114 struct die_info *comp_unit_die,
10115 int has_children,
10116 void *data)
10117 {
10118 struct dwarf2_cu *cu = reader->cu;
10119 enum language *language_ptr = (enum language *) data;
10120
10121 gdb_assert (cu->die_hash == NULL);
10122 cu->die_hash =
10123 htab_create_alloc_ex (cu->header.length / 12,
10124 die_hash,
10125 die_eq,
10126 NULL,
10127 &cu->comp_unit_obstack,
10128 hashtab_obstack_allocate,
10129 dummy_obstack_deallocate);
10130
10131 if (has_children)
10132 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10133 &info_ptr, comp_unit_die);
10134 cu->dies = comp_unit_die;
10135 /* comp_unit_die is not stored in die_hash, no need. */
10136
10137 /* We try not to read any attributes in this function, because not
10138 all CUs needed for references have been loaded yet, and symbol
10139 table processing isn't initialized. But we have to set the CU language,
10140 or we won't be able to build types correctly.
10141 Similarly, if we do not read the producer, we can not apply
10142 producer-specific interpretation. */
10143 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
10144 }
10145
10146 /* Load the DIEs associated with PER_CU into memory. */
10147
10148 static void
10149 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10150 enum language pretend_language)
10151 {
10152 gdb_assert (! this_cu->is_debug_types);
10153
10154 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10155 load_full_comp_unit_reader, &pretend_language);
10156 }
10157
10158 /* Add a DIE to the delayed physname list. */
10159
10160 static void
10161 add_to_method_list (struct type *type, int fnfield_index, int index,
10162 const char *name, struct die_info *die,
10163 struct dwarf2_cu *cu)
10164 {
10165 struct delayed_method_info mi;
10166 mi.type = type;
10167 mi.fnfield_index = fnfield_index;
10168 mi.index = index;
10169 mi.name = name;
10170 mi.die = die;
10171 cu->method_list.push_back (mi);
10172 }
10173
10174 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10175 "const" / "volatile". If so, decrements LEN by the length of the
10176 modifier and return true. Otherwise return false. */
10177
10178 template<size_t N>
10179 static bool
10180 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10181 {
10182 size_t mod_len = sizeof (mod) - 1;
10183 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10184 {
10185 len -= mod_len;
10186 return true;
10187 }
10188 return false;
10189 }
10190
10191 /* Compute the physnames of any methods on the CU's method list.
10192
10193 The computation of method physnames is delayed in order to avoid the
10194 (bad) condition that one of the method's formal parameters is of an as yet
10195 incomplete type. */
10196
10197 static void
10198 compute_delayed_physnames (struct dwarf2_cu *cu)
10199 {
10200 /* Only C++ delays computing physnames. */
10201 if (cu->method_list.empty ())
10202 return;
10203 gdb_assert (cu->language == language_cplus);
10204
10205 for (struct delayed_method_info &mi : cu->method_list)
10206 {
10207 const char *physname;
10208 struct fn_fieldlist *fn_flp
10209 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
10210 physname = dwarf2_physname (mi.name, mi.die, cu);
10211 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
10212 = physname ? physname : "";
10213
10214 /* Since there's no tag to indicate whether a method is a
10215 const/volatile overload, extract that information out of the
10216 demangled name. */
10217 if (physname != NULL)
10218 {
10219 size_t len = strlen (physname);
10220
10221 while (1)
10222 {
10223 if (physname[len] == ')') /* shortcut */
10224 break;
10225 else if (check_modifier (physname, len, " const"))
10226 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
10227 else if (check_modifier (physname, len, " volatile"))
10228 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
10229 else
10230 break;
10231 }
10232 }
10233 }
10234
10235 /* The list is no longer needed. */
10236 cu->method_list.clear ();
10237 }
10238
10239 /* Go objects should be embedded in a DW_TAG_module DIE,
10240 and it's not clear if/how imported objects will appear.
10241 To keep Go support simple until that's worked out,
10242 go back through what we've read and create something usable.
10243 We could do this while processing each DIE, and feels kinda cleaner,
10244 but that way is more invasive.
10245 This is to, for example, allow the user to type "p var" or "b main"
10246 without having to specify the package name, and allow lookups
10247 of module.object to work in contexts that use the expression
10248 parser. */
10249
10250 static void
10251 fixup_go_packaging (struct dwarf2_cu *cu)
10252 {
10253 char *package_name = NULL;
10254 struct pending *list;
10255 int i;
10256
10257 for (list = global_symbols; list != NULL; list = list->next)
10258 {
10259 for (i = 0; i < list->nsyms; ++i)
10260 {
10261 struct symbol *sym = list->symbol[i];
10262
10263 if (SYMBOL_LANGUAGE (sym) == language_go
10264 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10265 {
10266 char *this_package_name = go_symbol_package_name (sym);
10267
10268 if (this_package_name == NULL)
10269 continue;
10270 if (package_name == NULL)
10271 package_name = this_package_name;
10272 else
10273 {
10274 struct objfile *objfile
10275 = cu->per_cu->dwarf2_per_objfile->objfile;
10276 if (strcmp (package_name, this_package_name) != 0)
10277 complaint (&symfile_complaints,
10278 _("Symtab %s has objects from two different Go packages: %s and %s"),
10279 (symbol_symtab (sym) != NULL
10280 ? symtab_to_filename_for_display
10281 (symbol_symtab (sym))
10282 : objfile_name (objfile)),
10283 this_package_name, package_name);
10284 xfree (this_package_name);
10285 }
10286 }
10287 }
10288 }
10289
10290 if (package_name != NULL)
10291 {
10292 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10293 const char *saved_package_name
10294 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10295 package_name,
10296 strlen (package_name));
10297 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10298 saved_package_name);
10299 struct symbol *sym;
10300
10301 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10302
10303 sym = allocate_symbol (objfile);
10304 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
10305 SYMBOL_SET_NAMES (sym, saved_package_name,
10306 strlen (saved_package_name), 0, objfile);
10307 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10308 e.g., "main" finds the "main" module and not C's main(). */
10309 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10310 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
10311 SYMBOL_TYPE (sym) = type;
10312
10313 add_symbol_to_list (sym, &global_symbols);
10314
10315 xfree (package_name);
10316 }
10317 }
10318
10319 /* Allocate a fully-qualified name consisting of the two parts on the
10320 obstack. */
10321
10322 static const char *
10323 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
10324 {
10325 return obconcat (obstack, p1, "::", p2, (char *) NULL);
10326 }
10327
10328 /* A helper that allocates a struct discriminant_info to attach to a
10329 union type. */
10330
10331 static struct discriminant_info *
10332 alloc_discriminant_info (struct type *type, int discriminant_index,
10333 int default_index)
10334 {
10335 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10336 gdb_assert (discriminant_index == -1
10337 || (discriminant_index >= 0
10338 && discriminant_index < TYPE_NFIELDS (type)));
10339 gdb_assert (default_index == -1
10340 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
10341
10342 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
10343
10344 struct discriminant_info *disc
10345 = ((struct discriminant_info *)
10346 TYPE_ZALLOC (type,
10347 offsetof (struct discriminant_info, discriminants)
10348 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
10349 disc->default_index = default_index;
10350 disc->discriminant_index = discriminant_index;
10351
10352 struct dynamic_prop prop;
10353 prop.kind = PROP_UNDEFINED;
10354 prop.data.baton = disc;
10355
10356 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
10357
10358 return disc;
10359 }
10360
10361 /* Some versions of rustc emitted enums in an unusual way.
10362
10363 Ordinary enums were emitted as unions. The first element of each
10364 structure in the union was named "RUST$ENUM$DISR". This element
10365 held the discriminant.
10366
10367 These versions of Rust also implemented the "non-zero"
10368 optimization. When the enum had two values, and one is empty and
10369 the other holds a pointer that cannot be zero, the pointer is used
10370 as the discriminant, with a zero value meaning the empty variant.
10371 Here, the union's first member is of the form
10372 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10373 where the fieldnos are the indices of the fields that should be
10374 traversed in order to find the field (which may be several fields deep)
10375 and the variantname is the name of the variant of the case when the
10376 field is zero.
10377
10378 This function recognizes whether TYPE is of one of these forms,
10379 and, if so, smashes it to be a variant type. */
10380
10381 static void
10382 quirk_rust_enum (struct type *type, struct objfile *objfile)
10383 {
10384 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10385
10386 /* We don't need to deal with empty enums. */
10387 if (TYPE_NFIELDS (type) == 0)
10388 return;
10389
10390 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10391 if (TYPE_NFIELDS (type) == 1
10392 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10393 {
10394 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10395
10396 /* Decode the field name to find the offset of the
10397 discriminant. */
10398 ULONGEST bit_offset = 0;
10399 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10400 while (name[0] >= '0' && name[0] <= '9')
10401 {
10402 char *tail;
10403 unsigned long index = strtoul (name, &tail, 10);
10404 name = tail;
10405 if (*name != '$'
10406 || index >= TYPE_NFIELDS (field_type)
10407 || (TYPE_FIELD_LOC_KIND (field_type, index)
10408 != FIELD_LOC_KIND_BITPOS))
10409 {
10410 complaint (&symfile_complaints,
10411 _("Could not parse Rust enum encoding string \"%s\""
10412 "[in module %s]"),
10413 TYPE_FIELD_NAME (type, 0),
10414 objfile_name (objfile));
10415 return;
10416 }
10417 ++name;
10418
10419 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10420 field_type = TYPE_FIELD_TYPE (field_type, index);
10421 }
10422
10423 /* Make a union to hold the variants. */
10424 struct type *union_type = alloc_type (objfile);
10425 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10426 TYPE_NFIELDS (union_type) = 3;
10427 TYPE_FIELDS (union_type)
10428 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10429 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10430
10431 /* Put the discriminant must at index 0. */
10432 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10433 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10434 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10435 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10436
10437 /* The order of fields doesn't really matter, so put the real
10438 field at index 1 and the data-less field at index 2. */
10439 struct discriminant_info *disc
10440 = alloc_discriminant_info (union_type, 0, 1);
10441 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10442 TYPE_FIELD_NAME (union_type, 1)
10443 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10444 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10445 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10446 TYPE_FIELD_NAME (union_type, 1));
10447
10448 const char *dataless_name
10449 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10450 name);
10451 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10452 dataless_name);
10453 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10454 /* NAME points into the original discriminant name, which
10455 already has the correct lifetime. */
10456 TYPE_FIELD_NAME (union_type, 2) = name;
10457 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10458 disc->discriminants[2] = 0;
10459
10460 /* Smash this type to be a structure type. We have to do this
10461 because the type has already been recorded. */
10462 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10463 TYPE_NFIELDS (type) = 1;
10464 TYPE_FIELDS (type)
10465 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10466
10467 /* Install the variant part. */
10468 TYPE_FIELD_TYPE (type, 0) = union_type;
10469 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10470 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10471 }
10472 else if (TYPE_NFIELDS (type) == 1)
10473 {
10474 /* We assume that a union with a single field is a univariant
10475 enum. */
10476 /* Smash this type to be a structure type. We have to do this
10477 because the type has already been recorded. */
10478 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10479
10480 /* Make a union to hold the variants. */
10481 struct type *union_type = alloc_type (objfile);
10482 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10483 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10484 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10485 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10486
10487 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10488 const char *variant_name
10489 = rust_last_path_segment (TYPE_NAME (field_type));
10490 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10491 TYPE_NAME (field_type)
10492 = rust_fully_qualify (&objfile->objfile_obstack,
10493 TYPE_NAME (type), variant_name);
10494
10495 /* Install the union in the outer struct type. */
10496 TYPE_NFIELDS (type) = 1;
10497 TYPE_FIELDS (type)
10498 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10499 TYPE_FIELD_TYPE (type, 0) = union_type;
10500 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10501 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10502
10503 alloc_discriminant_info (union_type, -1, 0);
10504 }
10505 else
10506 {
10507 struct type *disr_type = nullptr;
10508 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10509 {
10510 disr_type = TYPE_FIELD_TYPE (type, i);
10511
10512 if (TYPE_NFIELDS (disr_type) == 0)
10513 {
10514 /* Could be data-less variant, so keep going. */
10515 }
10516 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10517 "RUST$ENUM$DISR") != 0)
10518 {
10519 /* Not a Rust enum. */
10520 return;
10521 }
10522 else
10523 {
10524 /* Found one. */
10525 break;
10526 }
10527 }
10528
10529 /* If we got here without a discriminant, then it's probably
10530 just a union. */
10531 if (disr_type == nullptr)
10532 return;
10533
10534 /* Smash this type to be a structure type. We have to do this
10535 because the type has already been recorded. */
10536 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10537
10538 /* Make a union to hold the variants. */
10539 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10540 struct type *union_type = alloc_type (objfile);
10541 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10542 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10543 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10544 TYPE_FIELDS (union_type)
10545 = (struct field *) TYPE_ZALLOC (union_type,
10546 (TYPE_NFIELDS (union_type)
10547 * sizeof (struct field)));
10548
10549 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10550 TYPE_NFIELDS (type) * sizeof (struct field));
10551
10552 /* Install the discriminant at index 0 in the union. */
10553 TYPE_FIELD (union_type, 0) = *disr_field;
10554 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10555 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10556
10557 /* Install the union in the outer struct type. */
10558 TYPE_FIELD_TYPE (type, 0) = union_type;
10559 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10560 TYPE_NFIELDS (type) = 1;
10561
10562 /* Set the size and offset of the union type. */
10563 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10564
10565 /* We need a way to find the correct discriminant given a
10566 variant name. For convenience we build a map here. */
10567 struct type *enum_type = FIELD_TYPE (*disr_field);
10568 std::unordered_map<std::string, ULONGEST> discriminant_map;
10569 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10570 {
10571 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10572 {
10573 const char *name
10574 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10575 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10576 }
10577 }
10578
10579 int n_fields = TYPE_NFIELDS (union_type);
10580 struct discriminant_info *disc
10581 = alloc_discriminant_info (union_type, 0, -1);
10582 /* Skip the discriminant here. */
10583 for (int i = 1; i < n_fields; ++i)
10584 {
10585 /* Find the final word in the name of this variant's type.
10586 That name can be used to look up the correct
10587 discriminant. */
10588 const char *variant_name
10589 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10590 i)));
10591
10592 auto iter = discriminant_map.find (variant_name);
10593 if (iter != discriminant_map.end ())
10594 disc->discriminants[i] = iter->second;
10595
10596 /* Remove the discriminant field. */
10597 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10598 --TYPE_NFIELDS (sub_type);
10599 ++TYPE_FIELDS (sub_type);
10600 TYPE_FIELD_NAME (union_type, i) = variant_name;
10601 TYPE_NAME (sub_type)
10602 = rust_fully_qualify (&objfile->objfile_obstack,
10603 TYPE_NAME (type), variant_name);
10604 }
10605 }
10606 }
10607
10608 /* Rewrite some Rust unions to be structures with variants parts. */
10609
10610 static void
10611 rust_union_quirks (struct dwarf2_cu *cu)
10612 {
10613 gdb_assert (cu->language == language_rust);
10614 for (struct type *type : cu->rust_unions)
10615 quirk_rust_enum (type, cu->per_cu->dwarf2_per_objfile->objfile);
10616 }
10617
10618 /* Return the symtab for PER_CU. This works properly regardless of
10619 whether we're using the index or psymtabs. */
10620
10621 static struct compunit_symtab *
10622 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10623 {
10624 return (per_cu->dwarf2_per_objfile->using_index
10625 ? per_cu->v.quick->compunit_symtab
10626 : per_cu->v.psymtab->compunit_symtab);
10627 }
10628
10629 /* A helper function for computing the list of all symbol tables
10630 included by PER_CU. */
10631
10632 static void
10633 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10634 htab_t all_children, htab_t all_type_symtabs,
10635 struct dwarf2_per_cu_data *per_cu,
10636 struct compunit_symtab *immediate_parent)
10637 {
10638 void **slot;
10639 int ix;
10640 struct compunit_symtab *cust;
10641 struct dwarf2_per_cu_data *iter;
10642
10643 slot = htab_find_slot (all_children, per_cu, INSERT);
10644 if (*slot != NULL)
10645 {
10646 /* This inclusion and its children have been processed. */
10647 return;
10648 }
10649
10650 *slot = per_cu;
10651 /* Only add a CU if it has a symbol table. */
10652 cust = get_compunit_symtab (per_cu);
10653 if (cust != NULL)
10654 {
10655 /* If this is a type unit only add its symbol table if we haven't
10656 seen it yet (type unit per_cu's can share symtabs). */
10657 if (per_cu->is_debug_types)
10658 {
10659 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10660 if (*slot == NULL)
10661 {
10662 *slot = cust;
10663 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10664 if (cust->user == NULL)
10665 cust->user = immediate_parent;
10666 }
10667 }
10668 else
10669 {
10670 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10671 if (cust->user == NULL)
10672 cust->user = immediate_parent;
10673 }
10674 }
10675
10676 for (ix = 0;
10677 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10678 ++ix)
10679 {
10680 recursively_compute_inclusions (result, all_children,
10681 all_type_symtabs, iter, cust);
10682 }
10683 }
10684
10685 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10686 PER_CU. */
10687
10688 static void
10689 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10690 {
10691 gdb_assert (! per_cu->is_debug_types);
10692
10693 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10694 {
10695 int ix, len;
10696 struct dwarf2_per_cu_data *per_cu_iter;
10697 struct compunit_symtab *compunit_symtab_iter;
10698 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10699 htab_t all_children, all_type_symtabs;
10700 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10701
10702 /* If we don't have a symtab, we can just skip this case. */
10703 if (cust == NULL)
10704 return;
10705
10706 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10707 NULL, xcalloc, xfree);
10708 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10709 NULL, xcalloc, xfree);
10710
10711 for (ix = 0;
10712 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10713 ix, per_cu_iter);
10714 ++ix)
10715 {
10716 recursively_compute_inclusions (&result_symtabs, all_children,
10717 all_type_symtabs, per_cu_iter,
10718 cust);
10719 }
10720
10721 /* Now we have a transitive closure of all the included symtabs. */
10722 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10723 cust->includes
10724 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10725 struct compunit_symtab *, len + 1);
10726 for (ix = 0;
10727 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10728 compunit_symtab_iter);
10729 ++ix)
10730 cust->includes[ix] = compunit_symtab_iter;
10731 cust->includes[len] = NULL;
10732
10733 VEC_free (compunit_symtab_ptr, result_symtabs);
10734 htab_delete (all_children);
10735 htab_delete (all_type_symtabs);
10736 }
10737 }
10738
10739 /* Compute the 'includes' field for the symtabs of all the CUs we just
10740 read. */
10741
10742 static void
10743 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10744 {
10745 int ix;
10746 struct dwarf2_per_cu_data *iter;
10747
10748 for (ix = 0;
10749 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10750 ix, iter);
10751 ++ix)
10752 {
10753 if (! iter->is_debug_types)
10754 compute_compunit_symtab_includes (iter);
10755 }
10756
10757 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10758 }
10759
10760 /* Generate full symbol information for PER_CU, whose DIEs have
10761 already been loaded into memory. */
10762
10763 static void
10764 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10765 enum language pretend_language)
10766 {
10767 struct dwarf2_cu *cu = per_cu->cu;
10768 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10769 struct objfile *objfile = dwarf2_per_objfile->objfile;
10770 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10771 CORE_ADDR lowpc, highpc;
10772 struct compunit_symtab *cust;
10773 CORE_ADDR baseaddr;
10774 struct block *static_block;
10775 CORE_ADDR addr;
10776
10777 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10778
10779 buildsym_init ();
10780 scoped_free_pendings free_pending;
10781
10782 /* Clear the list here in case something was left over. */
10783 cu->method_list.clear ();
10784
10785 cu->list_in_scope = &file_symbols;
10786
10787 cu->language = pretend_language;
10788 cu->language_defn = language_def (cu->language);
10789
10790 /* Do line number decoding in read_file_scope () */
10791 process_die (cu->dies, cu);
10792
10793 /* For now fudge the Go package. */
10794 if (cu->language == language_go)
10795 fixup_go_packaging (cu);
10796
10797 /* Now that we have processed all the DIEs in the CU, all the types
10798 should be complete, and it should now be safe to compute all of the
10799 physnames. */
10800 compute_delayed_physnames (cu);
10801
10802 if (cu->language == language_rust)
10803 rust_union_quirks (cu);
10804
10805 /* Some compilers don't define a DW_AT_high_pc attribute for the
10806 compilation unit. If the DW_AT_high_pc is missing, synthesize
10807 it, by scanning the DIE's below the compilation unit. */
10808 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10809
10810 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10811 static_block = end_symtab_get_static_block (addr, 0, 1);
10812
10813 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10814 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10815 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10816 addrmap to help ensure it has an accurate map of pc values belonging to
10817 this comp unit. */
10818 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10819
10820 cust = end_symtab_from_static_block (static_block,
10821 SECT_OFF_TEXT (objfile), 0);
10822
10823 if (cust != NULL)
10824 {
10825 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10826
10827 /* Set symtab language to language from DW_AT_language. If the
10828 compilation is from a C file generated by language preprocessors, do
10829 not set the language if it was already deduced by start_subfile. */
10830 if (!(cu->language == language_c
10831 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10832 COMPUNIT_FILETABS (cust)->language = cu->language;
10833
10834 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10835 produce DW_AT_location with location lists but it can be possibly
10836 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10837 there were bugs in prologue debug info, fixed later in GCC-4.5
10838 by "unwind info for epilogues" patch (which is not directly related).
10839
10840 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10841 needed, it would be wrong due to missing DW_AT_producer there.
10842
10843 Still one can confuse GDB by using non-standard GCC compilation
10844 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10845 */
10846 if (cu->has_loclist && gcc_4_minor >= 5)
10847 cust->locations_valid = 1;
10848
10849 if (gcc_4_minor >= 5)
10850 cust->epilogue_unwind_valid = 1;
10851
10852 cust->call_site_htab = cu->call_site_htab;
10853 }
10854
10855 if (dwarf2_per_objfile->using_index)
10856 per_cu->v.quick->compunit_symtab = cust;
10857 else
10858 {
10859 struct partial_symtab *pst = per_cu->v.psymtab;
10860 pst->compunit_symtab = cust;
10861 pst->readin = 1;
10862 }
10863
10864 /* Push it for inclusion processing later. */
10865 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10866 }
10867
10868 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10869 already been loaded into memory. */
10870
10871 static void
10872 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10873 enum language pretend_language)
10874 {
10875 struct dwarf2_cu *cu = per_cu->cu;
10876 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10877 struct objfile *objfile = dwarf2_per_objfile->objfile;
10878 struct compunit_symtab *cust;
10879 struct signatured_type *sig_type;
10880
10881 gdb_assert (per_cu->is_debug_types);
10882 sig_type = (struct signatured_type *) per_cu;
10883
10884 buildsym_init ();
10885 scoped_free_pendings free_pending;
10886
10887 /* Clear the list here in case something was left over. */
10888 cu->method_list.clear ();
10889
10890 cu->list_in_scope = &file_symbols;
10891
10892 cu->language = pretend_language;
10893 cu->language_defn = language_def (cu->language);
10894
10895 /* The symbol tables are set up in read_type_unit_scope. */
10896 process_die (cu->dies, cu);
10897
10898 /* For now fudge the Go package. */
10899 if (cu->language == language_go)
10900 fixup_go_packaging (cu);
10901
10902 /* Now that we have processed all the DIEs in the CU, all the types
10903 should be complete, and it should now be safe to compute all of the
10904 physnames. */
10905 compute_delayed_physnames (cu);
10906
10907 if (cu->language == language_rust)
10908 rust_union_quirks (cu);
10909
10910 /* TUs share symbol tables.
10911 If this is the first TU to use this symtab, complete the construction
10912 of it with end_expandable_symtab. Otherwise, complete the addition of
10913 this TU's symbols to the existing symtab. */
10914 if (sig_type->type_unit_group->compunit_symtab == NULL)
10915 {
10916 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10917 sig_type->type_unit_group->compunit_symtab = cust;
10918
10919 if (cust != NULL)
10920 {
10921 /* Set symtab language to language from DW_AT_language. If the
10922 compilation is from a C file generated by language preprocessors,
10923 do not set the language if it was already deduced by
10924 start_subfile. */
10925 if (!(cu->language == language_c
10926 && COMPUNIT_FILETABS (cust)->language != language_c))
10927 COMPUNIT_FILETABS (cust)->language = cu->language;
10928 }
10929 }
10930 else
10931 {
10932 augment_type_symtab ();
10933 cust = sig_type->type_unit_group->compunit_symtab;
10934 }
10935
10936 if (dwarf2_per_objfile->using_index)
10937 per_cu->v.quick->compunit_symtab = cust;
10938 else
10939 {
10940 struct partial_symtab *pst = per_cu->v.psymtab;
10941 pst->compunit_symtab = cust;
10942 pst->readin = 1;
10943 }
10944 }
10945
10946 /* Process an imported unit DIE. */
10947
10948 static void
10949 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10950 {
10951 struct attribute *attr;
10952
10953 /* For now we don't handle imported units in type units. */
10954 if (cu->per_cu->is_debug_types)
10955 {
10956 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10957 " supported in type units [in module %s]"),
10958 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10959 }
10960
10961 attr = dwarf2_attr (die, DW_AT_import, cu);
10962 if (attr != NULL)
10963 {
10964 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10965 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10966 dwarf2_per_cu_data *per_cu
10967 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10968 cu->per_cu->dwarf2_per_objfile);
10969
10970 /* If necessary, add it to the queue and load its DIEs. */
10971 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10972 load_full_comp_unit (per_cu, cu->language);
10973
10974 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10975 per_cu);
10976 }
10977 }
10978
10979 /* RAII object that represents a process_die scope: i.e.,
10980 starts/finishes processing a DIE. */
10981 class process_die_scope
10982 {
10983 public:
10984 process_die_scope (die_info *die, dwarf2_cu *cu)
10985 : m_die (die), m_cu (cu)
10986 {
10987 /* We should only be processing DIEs not already in process. */
10988 gdb_assert (!m_die->in_process);
10989 m_die->in_process = true;
10990 }
10991
10992 ~process_die_scope ()
10993 {
10994 m_die->in_process = false;
10995
10996 /* If we're done processing the DIE for the CU that owns the line
10997 header, we don't need the line header anymore. */
10998 if (m_cu->line_header_die_owner == m_die)
10999 {
11000 delete m_cu->line_header;
11001 m_cu->line_header = NULL;
11002 m_cu->line_header_die_owner = NULL;
11003 }
11004 }
11005
11006 private:
11007 die_info *m_die;
11008 dwarf2_cu *m_cu;
11009 };
11010
11011 /* Process a die and its children. */
11012
11013 static void
11014 process_die (struct die_info *die, struct dwarf2_cu *cu)
11015 {
11016 process_die_scope scope (die, cu);
11017
11018 switch (die->tag)
11019 {
11020 case DW_TAG_padding:
11021 break;
11022 case DW_TAG_compile_unit:
11023 case DW_TAG_partial_unit:
11024 read_file_scope (die, cu);
11025 break;
11026 case DW_TAG_type_unit:
11027 read_type_unit_scope (die, cu);
11028 break;
11029 case DW_TAG_subprogram:
11030 case DW_TAG_inlined_subroutine:
11031 read_func_scope (die, cu);
11032 break;
11033 case DW_TAG_lexical_block:
11034 case DW_TAG_try_block:
11035 case DW_TAG_catch_block:
11036 read_lexical_block_scope (die, cu);
11037 break;
11038 case DW_TAG_call_site:
11039 case DW_TAG_GNU_call_site:
11040 read_call_site_scope (die, cu);
11041 break;
11042 case DW_TAG_class_type:
11043 case DW_TAG_interface_type:
11044 case DW_TAG_structure_type:
11045 case DW_TAG_union_type:
11046 process_structure_scope (die, cu);
11047 break;
11048 case DW_TAG_enumeration_type:
11049 process_enumeration_scope (die, cu);
11050 break;
11051
11052 /* These dies have a type, but processing them does not create
11053 a symbol or recurse to process the children. Therefore we can
11054 read them on-demand through read_type_die. */
11055 case DW_TAG_subroutine_type:
11056 case DW_TAG_set_type:
11057 case DW_TAG_array_type:
11058 case DW_TAG_pointer_type:
11059 case DW_TAG_ptr_to_member_type:
11060 case DW_TAG_reference_type:
11061 case DW_TAG_rvalue_reference_type:
11062 case DW_TAG_string_type:
11063 break;
11064
11065 case DW_TAG_base_type:
11066 case DW_TAG_subrange_type:
11067 case DW_TAG_typedef:
11068 /* Add a typedef symbol for the type definition, if it has a
11069 DW_AT_name. */
11070 new_symbol (die, read_type_die (die, cu), cu);
11071 break;
11072 case DW_TAG_common_block:
11073 read_common_block (die, cu);
11074 break;
11075 case DW_TAG_common_inclusion:
11076 break;
11077 case DW_TAG_namespace:
11078 cu->processing_has_namespace_info = 1;
11079 read_namespace (die, cu);
11080 break;
11081 case DW_TAG_module:
11082 cu->processing_has_namespace_info = 1;
11083 read_module (die, cu);
11084 break;
11085 case DW_TAG_imported_declaration:
11086 cu->processing_has_namespace_info = 1;
11087 if (read_namespace_alias (die, cu))
11088 break;
11089 /* The declaration is not a global namespace alias: fall through. */
11090 case DW_TAG_imported_module:
11091 cu->processing_has_namespace_info = 1;
11092 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
11093 || cu->language != language_fortran))
11094 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
11095 dwarf_tag_name (die->tag));
11096 read_import_statement (die, cu);
11097 break;
11098
11099 case DW_TAG_imported_unit:
11100 process_imported_unit_die (die, cu);
11101 break;
11102
11103 case DW_TAG_variable:
11104 read_variable (die, cu);
11105 break;
11106
11107 default:
11108 new_symbol (die, NULL, cu);
11109 break;
11110 }
11111 }
11112 \f
11113 /* DWARF name computation. */
11114
11115 /* A helper function for dwarf2_compute_name which determines whether DIE
11116 needs to have the name of the scope prepended to the name listed in the
11117 die. */
11118
11119 static int
11120 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
11121 {
11122 struct attribute *attr;
11123
11124 switch (die->tag)
11125 {
11126 case DW_TAG_namespace:
11127 case DW_TAG_typedef:
11128 case DW_TAG_class_type:
11129 case DW_TAG_interface_type:
11130 case DW_TAG_structure_type:
11131 case DW_TAG_union_type:
11132 case DW_TAG_enumeration_type:
11133 case DW_TAG_enumerator:
11134 case DW_TAG_subprogram:
11135 case DW_TAG_inlined_subroutine:
11136 case DW_TAG_member:
11137 case DW_TAG_imported_declaration:
11138 return 1;
11139
11140 case DW_TAG_variable:
11141 case DW_TAG_constant:
11142 /* We only need to prefix "globally" visible variables. These include
11143 any variable marked with DW_AT_external or any variable that
11144 lives in a namespace. [Variables in anonymous namespaces
11145 require prefixing, but they are not DW_AT_external.] */
11146
11147 if (dwarf2_attr (die, DW_AT_specification, cu))
11148 {
11149 struct dwarf2_cu *spec_cu = cu;
11150
11151 return die_needs_namespace (die_specification (die, &spec_cu),
11152 spec_cu);
11153 }
11154
11155 attr = dwarf2_attr (die, DW_AT_external, cu);
11156 if (attr == NULL && die->parent->tag != DW_TAG_namespace
11157 && die->parent->tag != DW_TAG_module)
11158 return 0;
11159 /* A variable in a lexical block of some kind does not need a
11160 namespace, even though in C++ such variables may be external
11161 and have a mangled name. */
11162 if (die->parent->tag == DW_TAG_lexical_block
11163 || die->parent->tag == DW_TAG_try_block
11164 || die->parent->tag == DW_TAG_catch_block
11165 || die->parent->tag == DW_TAG_subprogram)
11166 return 0;
11167 return 1;
11168
11169 default:
11170 return 0;
11171 }
11172 }
11173
11174 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
11175 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
11176 defined for the given DIE. */
11177
11178 static struct attribute *
11179 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
11180 {
11181 struct attribute *attr;
11182
11183 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
11184 if (attr == NULL)
11185 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
11186
11187 return attr;
11188 }
11189
11190 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
11191 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
11192 defined for the given DIE. */
11193
11194 static const char *
11195 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
11196 {
11197 const char *linkage_name;
11198
11199 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
11200 if (linkage_name == NULL)
11201 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
11202
11203 return linkage_name;
11204 }
11205
11206 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
11207 compute the physname for the object, which include a method's:
11208 - formal parameters (C++),
11209 - receiver type (Go),
11210
11211 The term "physname" is a bit confusing.
11212 For C++, for example, it is the demangled name.
11213 For Go, for example, it's the mangled name.
11214
11215 For Ada, return the DIE's linkage name rather than the fully qualified
11216 name. PHYSNAME is ignored..
11217
11218 The result is allocated on the objfile_obstack and canonicalized. */
11219
11220 static const char *
11221 dwarf2_compute_name (const char *name,
11222 struct die_info *die, struct dwarf2_cu *cu,
11223 int physname)
11224 {
11225 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11226
11227 if (name == NULL)
11228 name = dwarf2_name (die, cu);
11229
11230 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
11231 but otherwise compute it by typename_concat inside GDB.
11232 FIXME: Actually this is not really true, or at least not always true.
11233 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
11234 Fortran names because there is no mangling standard. So new_symbol
11235 will set the demangled name to the result of dwarf2_full_name, and it is
11236 the demangled name that GDB uses if it exists. */
11237 if (cu->language == language_ada
11238 || (cu->language == language_fortran && physname))
11239 {
11240 /* For Ada unit, we prefer the linkage name over the name, as
11241 the former contains the exported name, which the user expects
11242 to be able to reference. Ideally, we want the user to be able
11243 to reference this entity using either natural or linkage name,
11244 but we haven't started looking at this enhancement yet. */
11245 const char *linkage_name = dw2_linkage_name (die, cu);
11246
11247 if (linkage_name != NULL)
11248 return linkage_name;
11249 }
11250
11251 /* These are the only languages we know how to qualify names in. */
11252 if (name != NULL
11253 && (cu->language == language_cplus
11254 || cu->language == language_fortran || cu->language == language_d
11255 || cu->language == language_rust))
11256 {
11257 if (die_needs_namespace (die, cu))
11258 {
11259 const char *prefix;
11260 const char *canonical_name = NULL;
11261
11262 string_file buf;
11263
11264 prefix = determine_prefix (die, cu);
11265 if (*prefix != '\0')
11266 {
11267 char *prefixed_name = typename_concat (NULL, prefix, name,
11268 physname, cu);
11269
11270 buf.puts (prefixed_name);
11271 xfree (prefixed_name);
11272 }
11273 else
11274 buf.puts (name);
11275
11276 /* Template parameters may be specified in the DIE's DW_AT_name, or
11277 as children with DW_TAG_template_type_param or
11278 DW_TAG_value_type_param. If the latter, add them to the name
11279 here. If the name already has template parameters, then
11280 skip this step; some versions of GCC emit both, and
11281 it is more efficient to use the pre-computed name.
11282
11283 Something to keep in mind about this process: it is very
11284 unlikely, or in some cases downright impossible, to produce
11285 something that will match the mangled name of a function.
11286 If the definition of the function has the same debug info,
11287 we should be able to match up with it anyway. But fallbacks
11288 using the minimal symbol, for instance to find a method
11289 implemented in a stripped copy of libstdc++, will not work.
11290 If we do not have debug info for the definition, we will have to
11291 match them up some other way.
11292
11293 When we do name matching there is a related problem with function
11294 templates; two instantiated function templates are allowed to
11295 differ only by their return types, which we do not add here. */
11296
11297 if (cu->language == language_cplus && strchr (name, '<') == NULL)
11298 {
11299 struct attribute *attr;
11300 struct die_info *child;
11301 int first = 1;
11302
11303 die->building_fullname = 1;
11304
11305 for (child = die->child; child != NULL; child = child->sibling)
11306 {
11307 struct type *type;
11308 LONGEST value;
11309 const gdb_byte *bytes;
11310 struct dwarf2_locexpr_baton *baton;
11311 struct value *v;
11312
11313 if (child->tag != DW_TAG_template_type_param
11314 && child->tag != DW_TAG_template_value_param)
11315 continue;
11316
11317 if (first)
11318 {
11319 buf.puts ("<");
11320 first = 0;
11321 }
11322 else
11323 buf.puts (", ");
11324
11325 attr = dwarf2_attr (child, DW_AT_type, cu);
11326 if (attr == NULL)
11327 {
11328 complaint (&symfile_complaints,
11329 _("template parameter missing DW_AT_type"));
11330 buf.puts ("UNKNOWN_TYPE");
11331 continue;
11332 }
11333 type = die_type (child, cu);
11334
11335 if (child->tag == DW_TAG_template_type_param)
11336 {
11337 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
11338 continue;
11339 }
11340
11341 attr = dwarf2_attr (child, DW_AT_const_value, cu);
11342 if (attr == NULL)
11343 {
11344 complaint (&symfile_complaints,
11345 _("template parameter missing "
11346 "DW_AT_const_value"));
11347 buf.puts ("UNKNOWN_VALUE");
11348 continue;
11349 }
11350
11351 dwarf2_const_value_attr (attr, type, name,
11352 &cu->comp_unit_obstack, cu,
11353 &value, &bytes, &baton);
11354
11355 if (TYPE_NOSIGN (type))
11356 /* GDB prints characters as NUMBER 'CHAR'. If that's
11357 changed, this can use value_print instead. */
11358 c_printchar (value, type, &buf);
11359 else
11360 {
11361 struct value_print_options opts;
11362
11363 if (baton != NULL)
11364 v = dwarf2_evaluate_loc_desc (type, NULL,
11365 baton->data,
11366 baton->size,
11367 baton->per_cu);
11368 else if (bytes != NULL)
11369 {
11370 v = allocate_value (type);
11371 memcpy (value_contents_writeable (v), bytes,
11372 TYPE_LENGTH (type));
11373 }
11374 else
11375 v = value_from_longest (type, value);
11376
11377 /* Specify decimal so that we do not depend on
11378 the radix. */
11379 get_formatted_print_options (&opts, 'd');
11380 opts.raw = 1;
11381 value_print (v, &buf, &opts);
11382 release_value (v);
11383 value_free (v);
11384 }
11385 }
11386
11387 die->building_fullname = 0;
11388
11389 if (!first)
11390 {
11391 /* Close the argument list, with a space if necessary
11392 (nested templates). */
11393 if (!buf.empty () && buf.string ().back () == '>')
11394 buf.puts (" >");
11395 else
11396 buf.puts (">");
11397 }
11398 }
11399
11400 /* For C++ methods, append formal parameter type
11401 information, if PHYSNAME. */
11402
11403 if (physname && die->tag == DW_TAG_subprogram
11404 && cu->language == language_cplus)
11405 {
11406 struct type *type = read_type_die (die, cu);
11407
11408 c_type_print_args (type, &buf, 1, cu->language,
11409 &type_print_raw_options);
11410
11411 if (cu->language == language_cplus)
11412 {
11413 /* Assume that an artificial first parameter is
11414 "this", but do not crash if it is not. RealView
11415 marks unnamed (and thus unused) parameters as
11416 artificial; there is no way to differentiate
11417 the two cases. */
11418 if (TYPE_NFIELDS (type) > 0
11419 && TYPE_FIELD_ARTIFICIAL (type, 0)
11420 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11421 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11422 0))))
11423 buf.puts (" const");
11424 }
11425 }
11426
11427 const std::string &intermediate_name = buf.string ();
11428
11429 if (cu->language == language_cplus)
11430 canonical_name
11431 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11432 &objfile->per_bfd->storage_obstack);
11433
11434 /* If we only computed INTERMEDIATE_NAME, or if
11435 INTERMEDIATE_NAME is already canonical, then we need to
11436 copy it to the appropriate obstack. */
11437 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11438 name = ((const char *)
11439 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11440 intermediate_name.c_str (),
11441 intermediate_name.length ()));
11442 else
11443 name = canonical_name;
11444 }
11445 }
11446
11447 return name;
11448 }
11449
11450 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11451 If scope qualifiers are appropriate they will be added. The result
11452 will be allocated on the storage_obstack, or NULL if the DIE does
11453 not have a name. NAME may either be from a previous call to
11454 dwarf2_name or NULL.
11455
11456 The output string will be canonicalized (if C++). */
11457
11458 static const char *
11459 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11460 {
11461 return dwarf2_compute_name (name, die, cu, 0);
11462 }
11463
11464 /* Construct a physname for the given DIE in CU. NAME may either be
11465 from a previous call to dwarf2_name or NULL. The result will be
11466 allocated on the objfile_objstack or NULL if the DIE does not have a
11467 name.
11468
11469 The output string will be canonicalized (if C++). */
11470
11471 static const char *
11472 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11473 {
11474 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11475 const char *retval, *mangled = NULL, *canon = NULL;
11476 int need_copy = 1;
11477
11478 /* In this case dwarf2_compute_name is just a shortcut not building anything
11479 on its own. */
11480 if (!die_needs_namespace (die, cu))
11481 return dwarf2_compute_name (name, die, cu, 1);
11482
11483 mangled = dw2_linkage_name (die, cu);
11484
11485 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11486 See https://github.com/rust-lang/rust/issues/32925. */
11487 if (cu->language == language_rust && mangled != NULL
11488 && strchr (mangled, '{') != NULL)
11489 mangled = NULL;
11490
11491 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11492 has computed. */
11493 gdb::unique_xmalloc_ptr<char> demangled;
11494 if (mangled != NULL)
11495 {
11496
11497 if (cu->language == language_go)
11498 {
11499 /* This is a lie, but we already lie to the caller new_symbol.
11500 new_symbol assumes we return the mangled name.
11501 This just undoes that lie until things are cleaned up. */
11502 }
11503 else
11504 {
11505 /* Use DMGL_RET_DROP for C++ template functions to suppress
11506 their return type. It is easier for GDB users to search
11507 for such functions as `name(params)' than `long name(params)'.
11508 In such case the minimal symbol names do not match the full
11509 symbol names but for template functions there is never a need
11510 to look up their definition from their declaration so
11511 the only disadvantage remains the minimal symbol variant
11512 `long name(params)' does not have the proper inferior type. */
11513 demangled.reset (gdb_demangle (mangled,
11514 (DMGL_PARAMS | DMGL_ANSI
11515 | DMGL_RET_DROP)));
11516 }
11517 if (demangled)
11518 canon = demangled.get ();
11519 else
11520 {
11521 canon = mangled;
11522 need_copy = 0;
11523 }
11524 }
11525
11526 if (canon == NULL || check_physname)
11527 {
11528 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11529
11530 if (canon != NULL && strcmp (physname, canon) != 0)
11531 {
11532 /* It may not mean a bug in GDB. The compiler could also
11533 compute DW_AT_linkage_name incorrectly. But in such case
11534 GDB would need to be bug-to-bug compatible. */
11535
11536 complaint (&symfile_complaints,
11537 _("Computed physname <%s> does not match demangled <%s> "
11538 "(from linkage <%s>) - DIE at %s [in module %s]"),
11539 physname, canon, mangled, sect_offset_str (die->sect_off),
11540 objfile_name (objfile));
11541
11542 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11543 is available here - over computed PHYSNAME. It is safer
11544 against both buggy GDB and buggy compilers. */
11545
11546 retval = canon;
11547 }
11548 else
11549 {
11550 retval = physname;
11551 need_copy = 0;
11552 }
11553 }
11554 else
11555 retval = canon;
11556
11557 if (need_copy)
11558 retval = ((const char *)
11559 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11560 retval, strlen (retval)));
11561
11562 return retval;
11563 }
11564
11565 /* Inspect DIE in CU for a namespace alias. If one exists, record
11566 a new symbol for it.
11567
11568 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11569
11570 static int
11571 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11572 {
11573 struct attribute *attr;
11574
11575 /* If the die does not have a name, this is not a namespace
11576 alias. */
11577 attr = dwarf2_attr (die, DW_AT_name, cu);
11578 if (attr != NULL)
11579 {
11580 int num;
11581 struct die_info *d = die;
11582 struct dwarf2_cu *imported_cu = cu;
11583
11584 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11585 keep inspecting DIEs until we hit the underlying import. */
11586 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11587 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11588 {
11589 attr = dwarf2_attr (d, DW_AT_import, cu);
11590 if (attr == NULL)
11591 break;
11592
11593 d = follow_die_ref (d, attr, &imported_cu);
11594 if (d->tag != DW_TAG_imported_declaration)
11595 break;
11596 }
11597
11598 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11599 {
11600 complaint (&symfile_complaints,
11601 _("DIE at %s has too many recursively imported "
11602 "declarations"), sect_offset_str (d->sect_off));
11603 return 0;
11604 }
11605
11606 if (attr != NULL)
11607 {
11608 struct type *type;
11609 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11610
11611 type = get_die_type_at_offset (sect_off, cu->per_cu);
11612 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11613 {
11614 /* This declaration is a global namespace alias. Add
11615 a symbol for it whose type is the aliased namespace. */
11616 new_symbol (die, type, cu);
11617 return 1;
11618 }
11619 }
11620 }
11621
11622 return 0;
11623 }
11624
11625 /* Return the using directives repository (global or local?) to use in the
11626 current context for LANGUAGE.
11627
11628 For Ada, imported declarations can materialize renamings, which *may* be
11629 global. However it is impossible (for now?) in DWARF to distinguish
11630 "external" imported declarations and "static" ones. As all imported
11631 declarations seem to be static in all other languages, make them all CU-wide
11632 global only in Ada. */
11633
11634 static struct using_direct **
11635 using_directives (enum language language)
11636 {
11637 if (language == language_ada && context_stack_depth == 0)
11638 return &global_using_directives;
11639 else
11640 return &local_using_directives;
11641 }
11642
11643 /* Read the import statement specified by the given die and record it. */
11644
11645 static void
11646 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11647 {
11648 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11649 struct attribute *import_attr;
11650 struct die_info *imported_die, *child_die;
11651 struct dwarf2_cu *imported_cu;
11652 const char *imported_name;
11653 const char *imported_name_prefix;
11654 const char *canonical_name;
11655 const char *import_alias;
11656 const char *imported_declaration = NULL;
11657 const char *import_prefix;
11658 std::vector<const char *> excludes;
11659
11660 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11661 if (import_attr == NULL)
11662 {
11663 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11664 dwarf_tag_name (die->tag));
11665 return;
11666 }
11667
11668 imported_cu = cu;
11669 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11670 imported_name = dwarf2_name (imported_die, imported_cu);
11671 if (imported_name == NULL)
11672 {
11673 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11674
11675 The import in the following code:
11676 namespace A
11677 {
11678 typedef int B;
11679 }
11680
11681 int main ()
11682 {
11683 using A::B;
11684 B b;
11685 return b;
11686 }
11687
11688 ...
11689 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11690 <52> DW_AT_decl_file : 1
11691 <53> DW_AT_decl_line : 6
11692 <54> DW_AT_import : <0x75>
11693 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11694 <59> DW_AT_name : B
11695 <5b> DW_AT_decl_file : 1
11696 <5c> DW_AT_decl_line : 2
11697 <5d> DW_AT_type : <0x6e>
11698 ...
11699 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11700 <76> DW_AT_byte_size : 4
11701 <77> DW_AT_encoding : 5 (signed)
11702
11703 imports the wrong die ( 0x75 instead of 0x58 ).
11704 This case will be ignored until the gcc bug is fixed. */
11705 return;
11706 }
11707
11708 /* Figure out the local name after import. */
11709 import_alias = dwarf2_name (die, cu);
11710
11711 /* Figure out where the statement is being imported to. */
11712 import_prefix = determine_prefix (die, cu);
11713
11714 /* Figure out what the scope of the imported die is and prepend it
11715 to the name of the imported die. */
11716 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11717
11718 if (imported_die->tag != DW_TAG_namespace
11719 && imported_die->tag != DW_TAG_module)
11720 {
11721 imported_declaration = imported_name;
11722 canonical_name = imported_name_prefix;
11723 }
11724 else if (strlen (imported_name_prefix) > 0)
11725 canonical_name = obconcat (&objfile->objfile_obstack,
11726 imported_name_prefix,
11727 (cu->language == language_d ? "." : "::"),
11728 imported_name, (char *) NULL);
11729 else
11730 canonical_name = imported_name;
11731
11732 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11733 for (child_die = die->child; child_die && child_die->tag;
11734 child_die = sibling_die (child_die))
11735 {
11736 /* DWARF-4: A Fortran use statement with a “rename list” may be
11737 represented by an imported module entry with an import attribute
11738 referring to the module and owned entries corresponding to those
11739 entities that are renamed as part of being imported. */
11740
11741 if (child_die->tag != DW_TAG_imported_declaration)
11742 {
11743 complaint (&symfile_complaints,
11744 _("child DW_TAG_imported_declaration expected "
11745 "- DIE at %s [in module %s]"),
11746 sect_offset_str (child_die->sect_off),
11747 objfile_name (objfile));
11748 continue;
11749 }
11750
11751 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11752 if (import_attr == NULL)
11753 {
11754 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11755 dwarf_tag_name (child_die->tag));
11756 continue;
11757 }
11758
11759 imported_cu = cu;
11760 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11761 &imported_cu);
11762 imported_name = dwarf2_name (imported_die, imported_cu);
11763 if (imported_name == NULL)
11764 {
11765 complaint (&symfile_complaints,
11766 _("child DW_TAG_imported_declaration has unknown "
11767 "imported name - DIE at %s [in module %s]"),
11768 sect_offset_str (child_die->sect_off),
11769 objfile_name (objfile));
11770 continue;
11771 }
11772
11773 excludes.push_back (imported_name);
11774
11775 process_die (child_die, cu);
11776 }
11777
11778 add_using_directive (using_directives (cu->language),
11779 import_prefix,
11780 canonical_name,
11781 import_alias,
11782 imported_declaration,
11783 excludes,
11784 0,
11785 &objfile->objfile_obstack);
11786 }
11787
11788 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11789 types, but gives them a size of zero. Starting with version 14,
11790 ICC is compatible with GCC. */
11791
11792 static int
11793 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11794 {
11795 if (!cu->checked_producer)
11796 check_producer (cu);
11797
11798 return cu->producer_is_icc_lt_14;
11799 }
11800
11801 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11802 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11803 this, it was first present in GCC release 4.3.0. */
11804
11805 static int
11806 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11807 {
11808 if (!cu->checked_producer)
11809 check_producer (cu);
11810
11811 return cu->producer_is_gcc_lt_4_3;
11812 }
11813
11814 static file_and_directory
11815 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11816 {
11817 file_and_directory res;
11818
11819 /* Find the filename. Do not use dwarf2_name here, since the filename
11820 is not a source language identifier. */
11821 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11822 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11823
11824 if (res.comp_dir == NULL
11825 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11826 && IS_ABSOLUTE_PATH (res.name))
11827 {
11828 res.comp_dir_storage = ldirname (res.name);
11829 if (!res.comp_dir_storage.empty ())
11830 res.comp_dir = res.comp_dir_storage.c_str ();
11831 }
11832 if (res.comp_dir != NULL)
11833 {
11834 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11835 directory, get rid of it. */
11836 const char *cp = strchr (res.comp_dir, ':');
11837
11838 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11839 res.comp_dir = cp + 1;
11840 }
11841
11842 if (res.name == NULL)
11843 res.name = "<unknown>";
11844
11845 return res;
11846 }
11847
11848 /* Handle DW_AT_stmt_list for a compilation unit.
11849 DIE is the DW_TAG_compile_unit die for CU.
11850 COMP_DIR is the compilation directory. LOWPC is passed to
11851 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11852
11853 static void
11854 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11855 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11856 {
11857 struct dwarf2_per_objfile *dwarf2_per_objfile
11858 = cu->per_cu->dwarf2_per_objfile;
11859 struct objfile *objfile = dwarf2_per_objfile->objfile;
11860 struct attribute *attr;
11861 struct line_header line_header_local;
11862 hashval_t line_header_local_hash;
11863 void **slot;
11864 int decode_mapping;
11865
11866 gdb_assert (! cu->per_cu->is_debug_types);
11867
11868 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11869 if (attr == NULL)
11870 return;
11871
11872 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11873
11874 /* The line header hash table is only created if needed (it exists to
11875 prevent redundant reading of the line table for partial_units).
11876 If we're given a partial_unit, we'll need it. If we're given a
11877 compile_unit, then use the line header hash table if it's already
11878 created, but don't create one just yet. */
11879
11880 if (dwarf2_per_objfile->line_header_hash == NULL
11881 && die->tag == DW_TAG_partial_unit)
11882 {
11883 dwarf2_per_objfile->line_header_hash
11884 = htab_create_alloc_ex (127, line_header_hash_voidp,
11885 line_header_eq_voidp,
11886 free_line_header_voidp,
11887 &objfile->objfile_obstack,
11888 hashtab_obstack_allocate,
11889 dummy_obstack_deallocate);
11890 }
11891
11892 line_header_local.sect_off = line_offset;
11893 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11894 line_header_local_hash = line_header_hash (&line_header_local);
11895 if (dwarf2_per_objfile->line_header_hash != NULL)
11896 {
11897 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11898 &line_header_local,
11899 line_header_local_hash, NO_INSERT);
11900
11901 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11902 is not present in *SLOT (since if there is something in *SLOT then
11903 it will be for a partial_unit). */
11904 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11905 {
11906 gdb_assert (*slot != NULL);
11907 cu->line_header = (struct line_header *) *slot;
11908 return;
11909 }
11910 }
11911
11912 /* dwarf_decode_line_header does not yet provide sufficient information.
11913 We always have to call also dwarf_decode_lines for it. */
11914 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11915 if (lh == NULL)
11916 return;
11917
11918 cu->line_header = lh.release ();
11919 cu->line_header_die_owner = die;
11920
11921 if (dwarf2_per_objfile->line_header_hash == NULL)
11922 slot = NULL;
11923 else
11924 {
11925 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11926 &line_header_local,
11927 line_header_local_hash, INSERT);
11928 gdb_assert (slot != NULL);
11929 }
11930 if (slot != NULL && *slot == NULL)
11931 {
11932 /* This newly decoded line number information unit will be owned
11933 by line_header_hash hash table. */
11934 *slot = cu->line_header;
11935 cu->line_header_die_owner = NULL;
11936 }
11937 else
11938 {
11939 /* We cannot free any current entry in (*slot) as that struct line_header
11940 may be already used by multiple CUs. Create only temporary decoded
11941 line_header for this CU - it may happen at most once for each line
11942 number information unit. And if we're not using line_header_hash
11943 then this is what we want as well. */
11944 gdb_assert (die->tag != DW_TAG_partial_unit);
11945 }
11946 decode_mapping = (die->tag != DW_TAG_partial_unit);
11947 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11948 decode_mapping);
11949
11950 }
11951
11952 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11953
11954 static void
11955 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11956 {
11957 struct dwarf2_per_objfile *dwarf2_per_objfile
11958 = cu->per_cu->dwarf2_per_objfile;
11959 struct objfile *objfile = dwarf2_per_objfile->objfile;
11960 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11961 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11962 CORE_ADDR highpc = ((CORE_ADDR) 0);
11963 struct attribute *attr;
11964 struct die_info *child_die;
11965 CORE_ADDR baseaddr;
11966
11967 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11968
11969 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11970
11971 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11972 from finish_block. */
11973 if (lowpc == ((CORE_ADDR) -1))
11974 lowpc = highpc;
11975 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11976
11977 file_and_directory fnd = find_file_and_directory (die, cu);
11978
11979 prepare_one_comp_unit (cu, die, cu->language);
11980
11981 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11982 standardised yet. As a workaround for the language detection we fall
11983 back to the DW_AT_producer string. */
11984 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11985 cu->language = language_opencl;
11986
11987 /* Similar hack for Go. */
11988 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11989 set_cu_language (DW_LANG_Go, cu);
11990
11991 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11992
11993 /* Decode line number information if present. We do this before
11994 processing child DIEs, so that the line header table is available
11995 for DW_AT_decl_file. */
11996 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11997
11998 /* Process all dies in compilation unit. */
11999 if (die->child != NULL)
12000 {
12001 child_die = die->child;
12002 while (child_die && child_die->tag)
12003 {
12004 process_die (child_die, cu);
12005 child_die = sibling_die (child_die);
12006 }
12007 }
12008
12009 /* Decode macro information, if present. Dwarf 2 macro information
12010 refers to information in the line number info statement program
12011 header, so we can only read it if we've read the header
12012 successfully. */
12013 attr = dwarf2_attr (die, DW_AT_macros, cu);
12014 if (attr == NULL)
12015 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
12016 if (attr && cu->line_header)
12017 {
12018 if (dwarf2_attr (die, DW_AT_macro_info, cu))
12019 complaint (&symfile_complaints,
12020 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
12021
12022 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
12023 }
12024 else
12025 {
12026 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
12027 if (attr && cu->line_header)
12028 {
12029 unsigned int macro_offset = DW_UNSND (attr);
12030
12031 dwarf_decode_macros (cu, macro_offset, 0);
12032 }
12033 }
12034 }
12035
12036 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
12037 Create the set of symtabs used by this TU, or if this TU is sharing
12038 symtabs with another TU and the symtabs have already been created
12039 then restore those symtabs in the line header.
12040 We don't need the pc/line-number mapping for type units. */
12041
12042 static void
12043 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
12044 {
12045 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
12046 struct type_unit_group *tu_group;
12047 int first_time;
12048 struct attribute *attr;
12049 unsigned int i;
12050 struct signatured_type *sig_type;
12051
12052 gdb_assert (per_cu->is_debug_types);
12053 sig_type = (struct signatured_type *) per_cu;
12054
12055 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
12056
12057 /* If we're using .gdb_index (includes -readnow) then
12058 per_cu->type_unit_group may not have been set up yet. */
12059 if (sig_type->type_unit_group == NULL)
12060 sig_type->type_unit_group = get_type_unit_group (cu, attr);
12061 tu_group = sig_type->type_unit_group;
12062
12063 /* If we've already processed this stmt_list there's no real need to
12064 do it again, we could fake it and just recreate the part we need
12065 (file name,index -> symtab mapping). If data shows this optimization
12066 is useful we can do it then. */
12067 first_time = tu_group->compunit_symtab == NULL;
12068
12069 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
12070 debug info. */
12071 line_header_up lh;
12072 if (attr != NULL)
12073 {
12074 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
12075 lh = dwarf_decode_line_header (line_offset, cu);
12076 }
12077 if (lh == NULL)
12078 {
12079 if (first_time)
12080 dwarf2_start_symtab (cu, "", NULL, 0);
12081 else
12082 {
12083 gdb_assert (tu_group->symtabs == NULL);
12084 restart_symtab (tu_group->compunit_symtab, "", 0);
12085 }
12086 return;
12087 }
12088
12089 cu->line_header = lh.release ();
12090 cu->line_header_die_owner = die;
12091
12092 if (first_time)
12093 {
12094 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
12095
12096 /* Note: We don't assign tu_group->compunit_symtab yet because we're
12097 still initializing it, and our caller (a few levels up)
12098 process_full_type_unit still needs to know if this is the first
12099 time. */
12100
12101 tu_group->num_symtabs = cu->line_header->file_names.size ();
12102 tu_group->symtabs = XNEWVEC (struct symtab *,
12103 cu->line_header->file_names.size ());
12104
12105 for (i = 0; i < cu->line_header->file_names.size (); ++i)
12106 {
12107 file_entry &fe = cu->line_header->file_names[i];
12108
12109 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
12110
12111 if (current_subfile->symtab == NULL)
12112 {
12113 /* NOTE: start_subfile will recognize when it's been
12114 passed a file it has already seen. So we can't
12115 assume there's a simple mapping from
12116 cu->line_header->file_names to subfiles, plus
12117 cu->line_header->file_names may contain dups. */
12118 current_subfile->symtab
12119 = allocate_symtab (cust, current_subfile->name);
12120 }
12121
12122 fe.symtab = current_subfile->symtab;
12123 tu_group->symtabs[i] = fe.symtab;
12124 }
12125 }
12126 else
12127 {
12128 restart_symtab (tu_group->compunit_symtab, "", 0);
12129
12130 for (i = 0; i < cu->line_header->file_names.size (); ++i)
12131 {
12132 file_entry &fe = cu->line_header->file_names[i];
12133
12134 fe.symtab = tu_group->symtabs[i];
12135 }
12136 }
12137
12138 /* The main symtab is allocated last. Type units don't have DW_AT_name
12139 so they don't have a "real" (so to speak) symtab anyway.
12140 There is later code that will assign the main symtab to all symbols
12141 that don't have one. We need to handle the case of a symbol with a
12142 missing symtab (DW_AT_decl_file) anyway. */
12143 }
12144
12145 /* Process DW_TAG_type_unit.
12146 For TUs we want to skip the first top level sibling if it's not the
12147 actual type being defined by this TU. In this case the first top
12148 level sibling is there to provide context only. */
12149
12150 static void
12151 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
12152 {
12153 struct die_info *child_die;
12154
12155 prepare_one_comp_unit (cu, die, language_minimal);
12156
12157 /* Initialize (or reinitialize) the machinery for building symtabs.
12158 We do this before processing child DIEs, so that the line header table
12159 is available for DW_AT_decl_file. */
12160 setup_type_unit_groups (die, cu);
12161
12162 if (die->child != NULL)
12163 {
12164 child_die = die->child;
12165 while (child_die && child_die->tag)
12166 {
12167 process_die (child_die, cu);
12168 child_die = sibling_die (child_die);
12169 }
12170 }
12171 }
12172 \f
12173 /* DWO/DWP files.
12174
12175 http://gcc.gnu.org/wiki/DebugFission
12176 http://gcc.gnu.org/wiki/DebugFissionDWP
12177
12178 To simplify handling of both DWO files ("object" files with the DWARF info)
12179 and DWP files (a file with the DWOs packaged up into one file), we treat
12180 DWP files as having a collection of virtual DWO files. */
12181
12182 static hashval_t
12183 hash_dwo_file (const void *item)
12184 {
12185 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
12186 hashval_t hash;
12187
12188 hash = htab_hash_string (dwo_file->dwo_name);
12189 if (dwo_file->comp_dir != NULL)
12190 hash += htab_hash_string (dwo_file->comp_dir);
12191 return hash;
12192 }
12193
12194 static int
12195 eq_dwo_file (const void *item_lhs, const void *item_rhs)
12196 {
12197 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
12198 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
12199
12200 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
12201 return 0;
12202 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
12203 return lhs->comp_dir == rhs->comp_dir;
12204 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
12205 }
12206
12207 /* Allocate a hash table for DWO files. */
12208
12209 static htab_t
12210 allocate_dwo_file_hash_table (struct objfile *objfile)
12211 {
12212 return htab_create_alloc_ex (41,
12213 hash_dwo_file,
12214 eq_dwo_file,
12215 NULL,
12216 &objfile->objfile_obstack,
12217 hashtab_obstack_allocate,
12218 dummy_obstack_deallocate);
12219 }
12220
12221 /* Lookup DWO file DWO_NAME. */
12222
12223 static void **
12224 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
12225 const char *dwo_name,
12226 const char *comp_dir)
12227 {
12228 struct dwo_file find_entry;
12229 void **slot;
12230
12231 if (dwarf2_per_objfile->dwo_files == NULL)
12232 dwarf2_per_objfile->dwo_files
12233 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
12234
12235 memset (&find_entry, 0, sizeof (find_entry));
12236 find_entry.dwo_name = dwo_name;
12237 find_entry.comp_dir = comp_dir;
12238 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
12239
12240 return slot;
12241 }
12242
12243 static hashval_t
12244 hash_dwo_unit (const void *item)
12245 {
12246 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12247
12248 /* This drops the top 32 bits of the id, but is ok for a hash. */
12249 return dwo_unit->signature;
12250 }
12251
12252 static int
12253 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
12254 {
12255 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
12256 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
12257
12258 /* The signature is assumed to be unique within the DWO file.
12259 So while object file CU dwo_id's always have the value zero,
12260 that's OK, assuming each object file DWO file has only one CU,
12261 and that's the rule for now. */
12262 return lhs->signature == rhs->signature;
12263 }
12264
12265 /* Allocate a hash table for DWO CUs,TUs.
12266 There is one of these tables for each of CUs,TUs for each DWO file. */
12267
12268 static htab_t
12269 allocate_dwo_unit_table (struct objfile *objfile)
12270 {
12271 /* Start out with a pretty small number.
12272 Generally DWO files contain only one CU and maybe some TUs. */
12273 return htab_create_alloc_ex (3,
12274 hash_dwo_unit,
12275 eq_dwo_unit,
12276 NULL,
12277 &objfile->objfile_obstack,
12278 hashtab_obstack_allocate,
12279 dummy_obstack_deallocate);
12280 }
12281
12282 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
12283
12284 struct create_dwo_cu_data
12285 {
12286 struct dwo_file *dwo_file;
12287 struct dwo_unit dwo_unit;
12288 };
12289
12290 /* die_reader_func for create_dwo_cu. */
12291
12292 static void
12293 create_dwo_cu_reader (const struct die_reader_specs *reader,
12294 const gdb_byte *info_ptr,
12295 struct die_info *comp_unit_die,
12296 int has_children,
12297 void *datap)
12298 {
12299 struct dwarf2_cu *cu = reader->cu;
12300 sect_offset sect_off = cu->per_cu->sect_off;
12301 struct dwarf2_section_info *section = cu->per_cu->section;
12302 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
12303 struct dwo_file *dwo_file = data->dwo_file;
12304 struct dwo_unit *dwo_unit = &data->dwo_unit;
12305 struct attribute *attr;
12306
12307 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
12308 if (attr == NULL)
12309 {
12310 complaint (&symfile_complaints,
12311 _("Dwarf Error: debug entry at offset %s is missing"
12312 " its dwo_id [in module %s]"),
12313 sect_offset_str (sect_off), dwo_file->dwo_name);
12314 return;
12315 }
12316
12317 dwo_unit->dwo_file = dwo_file;
12318 dwo_unit->signature = DW_UNSND (attr);
12319 dwo_unit->section = section;
12320 dwo_unit->sect_off = sect_off;
12321 dwo_unit->length = cu->per_cu->length;
12322
12323 if (dwarf_read_debug)
12324 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
12325 sect_offset_str (sect_off),
12326 hex_string (dwo_unit->signature));
12327 }
12328
12329 /* Create the dwo_units for the CUs in a DWO_FILE.
12330 Note: This function processes DWO files only, not DWP files. */
12331
12332 static void
12333 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12334 struct dwo_file &dwo_file, dwarf2_section_info &section,
12335 htab_t &cus_htab)
12336 {
12337 struct objfile *objfile = dwarf2_per_objfile->objfile;
12338 const gdb_byte *info_ptr, *end_ptr;
12339
12340 dwarf2_read_section (objfile, &section);
12341 info_ptr = section.buffer;
12342
12343 if (info_ptr == NULL)
12344 return;
12345
12346 if (dwarf_read_debug)
12347 {
12348 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
12349 get_section_name (&section),
12350 get_section_file_name (&section));
12351 }
12352
12353 end_ptr = info_ptr + section.size;
12354 while (info_ptr < end_ptr)
12355 {
12356 struct dwarf2_per_cu_data per_cu;
12357 struct create_dwo_cu_data create_dwo_cu_data;
12358 struct dwo_unit *dwo_unit;
12359 void **slot;
12360 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
12361
12362 memset (&create_dwo_cu_data.dwo_unit, 0,
12363 sizeof (create_dwo_cu_data.dwo_unit));
12364 memset (&per_cu, 0, sizeof (per_cu));
12365 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
12366 per_cu.is_debug_types = 0;
12367 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12368 per_cu.section = &section;
12369 create_dwo_cu_data.dwo_file = &dwo_file;
12370
12371 init_cutu_and_read_dies_no_follow (
12372 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12373 info_ptr += per_cu.length;
12374
12375 // If the unit could not be parsed, skip it.
12376 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12377 continue;
12378
12379 if (cus_htab == NULL)
12380 cus_htab = allocate_dwo_unit_table (objfile);
12381
12382 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12383 *dwo_unit = create_dwo_cu_data.dwo_unit;
12384 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12385 gdb_assert (slot != NULL);
12386 if (*slot != NULL)
12387 {
12388 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12389 sect_offset dup_sect_off = dup_cu->sect_off;
12390
12391 complaint (&symfile_complaints,
12392 _("debug cu entry at offset %s is duplicate to"
12393 " the entry at offset %s, signature %s"),
12394 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12395 hex_string (dwo_unit->signature));
12396 }
12397 *slot = (void *)dwo_unit;
12398 }
12399 }
12400
12401 /* DWP file .debug_{cu,tu}_index section format:
12402 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12403
12404 DWP Version 1:
12405
12406 Both index sections have the same format, and serve to map a 64-bit
12407 signature to a set of section numbers. Each section begins with a header,
12408 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12409 indexes, and a pool of 32-bit section numbers. The index sections will be
12410 aligned at 8-byte boundaries in the file.
12411
12412 The index section header consists of:
12413
12414 V, 32 bit version number
12415 -, 32 bits unused
12416 N, 32 bit number of compilation units or type units in the index
12417 M, 32 bit number of slots in the hash table
12418
12419 Numbers are recorded using the byte order of the application binary.
12420
12421 The hash table begins at offset 16 in the section, and consists of an array
12422 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12423 order of the application binary). Unused slots in the hash table are 0.
12424 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12425
12426 The parallel table begins immediately after the hash table
12427 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12428 array of 32-bit indexes (using the byte order of the application binary),
12429 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12430 table contains a 32-bit index into the pool of section numbers. For unused
12431 hash table slots, the corresponding entry in the parallel table will be 0.
12432
12433 The pool of section numbers begins immediately following the hash table
12434 (at offset 16 + 12 * M from the beginning of the section). The pool of
12435 section numbers consists of an array of 32-bit words (using the byte order
12436 of the application binary). Each item in the array is indexed starting
12437 from 0. The hash table entry provides the index of the first section
12438 number in the set. Additional section numbers in the set follow, and the
12439 set is terminated by a 0 entry (section number 0 is not used in ELF).
12440
12441 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12442 section must be the first entry in the set, and the .debug_abbrev.dwo must
12443 be the second entry. Other members of the set may follow in any order.
12444
12445 ---
12446
12447 DWP Version 2:
12448
12449 DWP Version 2 combines all the .debug_info, etc. sections into one,
12450 and the entries in the index tables are now offsets into these sections.
12451 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12452 section.
12453
12454 Index Section Contents:
12455 Header
12456 Hash Table of Signatures dwp_hash_table.hash_table
12457 Parallel Table of Indices dwp_hash_table.unit_table
12458 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12459 Table of Section Sizes dwp_hash_table.v2.sizes
12460
12461 The index section header consists of:
12462
12463 V, 32 bit version number
12464 L, 32 bit number of columns in the table of section offsets
12465 N, 32 bit number of compilation units or type units in the index
12466 M, 32 bit number of slots in the hash table
12467
12468 Numbers are recorded using the byte order of the application binary.
12469
12470 The hash table has the same format as version 1.
12471 The parallel table of indices has the same format as version 1,
12472 except that the entries are origin-1 indices into the table of sections
12473 offsets and the table of section sizes.
12474
12475 The table of offsets begins immediately following the parallel table
12476 (at offset 16 + 12 * M from the beginning of the section). The table is
12477 a two-dimensional array of 32-bit words (using the byte order of the
12478 application binary), with L columns and N+1 rows, in row-major order.
12479 Each row in the array is indexed starting from 0. The first row provides
12480 a key to the remaining rows: each column in this row provides an identifier
12481 for a debug section, and the offsets in the same column of subsequent rows
12482 refer to that section. The section identifiers are:
12483
12484 DW_SECT_INFO 1 .debug_info.dwo
12485 DW_SECT_TYPES 2 .debug_types.dwo
12486 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12487 DW_SECT_LINE 4 .debug_line.dwo
12488 DW_SECT_LOC 5 .debug_loc.dwo
12489 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12490 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12491 DW_SECT_MACRO 8 .debug_macro.dwo
12492
12493 The offsets provided by the CU and TU index sections are the base offsets
12494 for the contributions made by each CU or TU to the corresponding section
12495 in the package file. Each CU and TU header contains an abbrev_offset
12496 field, used to find the abbreviations table for that CU or TU within the
12497 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12498 be interpreted as relative to the base offset given in the index section.
12499 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12500 should be interpreted as relative to the base offset for .debug_line.dwo,
12501 and offsets into other debug sections obtained from DWARF attributes should
12502 also be interpreted as relative to the corresponding base offset.
12503
12504 The table of sizes begins immediately following the table of offsets.
12505 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12506 with L columns and N rows, in row-major order. Each row in the array is
12507 indexed starting from 1 (row 0 is shared by the two tables).
12508
12509 ---
12510
12511 Hash table lookup is handled the same in version 1 and 2:
12512
12513 We assume that N and M will not exceed 2^32 - 1.
12514 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12515
12516 Given a 64-bit compilation unit signature or a type signature S, an entry
12517 in the hash table is located as follows:
12518
12519 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12520 the low-order k bits all set to 1.
12521
12522 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12523
12524 3) If the hash table entry at index H matches the signature, use that
12525 entry. If the hash table entry at index H is unused (all zeroes),
12526 terminate the search: the signature is not present in the table.
12527
12528 4) Let H = (H + H') modulo M. Repeat at Step 3.
12529
12530 Because M > N and H' and M are relatively prime, the search is guaranteed
12531 to stop at an unused slot or find the match. */
12532
12533 /* Create a hash table to map DWO IDs to their CU/TU entry in
12534 .debug_{info,types}.dwo in DWP_FILE.
12535 Returns NULL if there isn't one.
12536 Note: This function processes DWP files only, not DWO files. */
12537
12538 static struct dwp_hash_table *
12539 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12540 struct dwp_file *dwp_file, int is_debug_types)
12541 {
12542 struct objfile *objfile = dwarf2_per_objfile->objfile;
12543 bfd *dbfd = dwp_file->dbfd;
12544 const gdb_byte *index_ptr, *index_end;
12545 struct dwarf2_section_info *index;
12546 uint32_t version, nr_columns, nr_units, nr_slots;
12547 struct dwp_hash_table *htab;
12548
12549 if (is_debug_types)
12550 index = &dwp_file->sections.tu_index;
12551 else
12552 index = &dwp_file->sections.cu_index;
12553
12554 if (dwarf2_section_empty_p (index))
12555 return NULL;
12556 dwarf2_read_section (objfile, index);
12557
12558 index_ptr = index->buffer;
12559 index_end = index_ptr + index->size;
12560
12561 version = read_4_bytes (dbfd, index_ptr);
12562 index_ptr += 4;
12563 if (version == 2)
12564 nr_columns = read_4_bytes (dbfd, index_ptr);
12565 else
12566 nr_columns = 0;
12567 index_ptr += 4;
12568 nr_units = read_4_bytes (dbfd, index_ptr);
12569 index_ptr += 4;
12570 nr_slots = read_4_bytes (dbfd, index_ptr);
12571 index_ptr += 4;
12572
12573 if (version != 1 && version != 2)
12574 {
12575 error (_("Dwarf Error: unsupported DWP file version (%s)"
12576 " [in module %s]"),
12577 pulongest (version), dwp_file->name);
12578 }
12579 if (nr_slots != (nr_slots & -nr_slots))
12580 {
12581 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12582 " is not power of 2 [in module %s]"),
12583 pulongest (nr_slots), dwp_file->name);
12584 }
12585
12586 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12587 htab->version = version;
12588 htab->nr_columns = nr_columns;
12589 htab->nr_units = nr_units;
12590 htab->nr_slots = nr_slots;
12591 htab->hash_table = index_ptr;
12592 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12593
12594 /* Exit early if the table is empty. */
12595 if (nr_slots == 0 || nr_units == 0
12596 || (version == 2 && nr_columns == 0))
12597 {
12598 /* All must be zero. */
12599 if (nr_slots != 0 || nr_units != 0
12600 || (version == 2 && nr_columns != 0))
12601 {
12602 complaint (&symfile_complaints,
12603 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12604 " all zero [in modules %s]"),
12605 dwp_file->name);
12606 }
12607 return htab;
12608 }
12609
12610 if (version == 1)
12611 {
12612 htab->section_pool.v1.indices =
12613 htab->unit_table + sizeof (uint32_t) * nr_slots;
12614 /* It's harder to decide whether the section is too small in v1.
12615 V1 is deprecated anyway so we punt. */
12616 }
12617 else
12618 {
12619 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12620 int *ids = htab->section_pool.v2.section_ids;
12621 /* Reverse map for error checking. */
12622 int ids_seen[DW_SECT_MAX + 1];
12623 int i;
12624
12625 if (nr_columns < 2)
12626 {
12627 error (_("Dwarf Error: bad DWP hash table, too few columns"
12628 " in section table [in module %s]"),
12629 dwp_file->name);
12630 }
12631 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12632 {
12633 error (_("Dwarf Error: bad DWP hash table, too many columns"
12634 " in section table [in module %s]"),
12635 dwp_file->name);
12636 }
12637 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12638 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12639 for (i = 0; i < nr_columns; ++i)
12640 {
12641 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12642
12643 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12644 {
12645 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12646 " in section table [in module %s]"),
12647 id, dwp_file->name);
12648 }
12649 if (ids_seen[id] != -1)
12650 {
12651 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12652 " id %d in section table [in module %s]"),
12653 id, dwp_file->name);
12654 }
12655 ids_seen[id] = i;
12656 ids[i] = id;
12657 }
12658 /* Must have exactly one info or types section. */
12659 if (((ids_seen[DW_SECT_INFO] != -1)
12660 + (ids_seen[DW_SECT_TYPES] != -1))
12661 != 1)
12662 {
12663 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12664 " DWO info/types section [in module %s]"),
12665 dwp_file->name);
12666 }
12667 /* Must have an abbrev section. */
12668 if (ids_seen[DW_SECT_ABBREV] == -1)
12669 {
12670 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12671 " section [in module %s]"),
12672 dwp_file->name);
12673 }
12674 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12675 htab->section_pool.v2.sizes =
12676 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12677 * nr_units * nr_columns);
12678 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12679 * nr_units * nr_columns))
12680 > index_end)
12681 {
12682 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12683 " [in module %s]"),
12684 dwp_file->name);
12685 }
12686 }
12687
12688 return htab;
12689 }
12690
12691 /* Update SECTIONS with the data from SECTP.
12692
12693 This function is like the other "locate" section routines that are
12694 passed to bfd_map_over_sections, but in this context the sections to
12695 read comes from the DWP V1 hash table, not the full ELF section table.
12696
12697 The result is non-zero for success, or zero if an error was found. */
12698
12699 static int
12700 locate_v1_virtual_dwo_sections (asection *sectp,
12701 struct virtual_v1_dwo_sections *sections)
12702 {
12703 const struct dwop_section_names *names = &dwop_section_names;
12704
12705 if (section_is_p (sectp->name, &names->abbrev_dwo))
12706 {
12707 /* There can be only one. */
12708 if (sections->abbrev.s.section != NULL)
12709 return 0;
12710 sections->abbrev.s.section = sectp;
12711 sections->abbrev.size = bfd_get_section_size (sectp);
12712 }
12713 else if (section_is_p (sectp->name, &names->info_dwo)
12714 || section_is_p (sectp->name, &names->types_dwo))
12715 {
12716 /* There can be only one. */
12717 if (sections->info_or_types.s.section != NULL)
12718 return 0;
12719 sections->info_or_types.s.section = sectp;
12720 sections->info_or_types.size = bfd_get_section_size (sectp);
12721 }
12722 else if (section_is_p (sectp->name, &names->line_dwo))
12723 {
12724 /* There can be only one. */
12725 if (sections->line.s.section != NULL)
12726 return 0;
12727 sections->line.s.section = sectp;
12728 sections->line.size = bfd_get_section_size (sectp);
12729 }
12730 else if (section_is_p (sectp->name, &names->loc_dwo))
12731 {
12732 /* There can be only one. */
12733 if (sections->loc.s.section != NULL)
12734 return 0;
12735 sections->loc.s.section = sectp;
12736 sections->loc.size = bfd_get_section_size (sectp);
12737 }
12738 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12739 {
12740 /* There can be only one. */
12741 if (sections->macinfo.s.section != NULL)
12742 return 0;
12743 sections->macinfo.s.section = sectp;
12744 sections->macinfo.size = bfd_get_section_size (sectp);
12745 }
12746 else if (section_is_p (sectp->name, &names->macro_dwo))
12747 {
12748 /* There can be only one. */
12749 if (sections->macro.s.section != NULL)
12750 return 0;
12751 sections->macro.s.section = sectp;
12752 sections->macro.size = bfd_get_section_size (sectp);
12753 }
12754 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12755 {
12756 /* There can be only one. */
12757 if (sections->str_offsets.s.section != NULL)
12758 return 0;
12759 sections->str_offsets.s.section = sectp;
12760 sections->str_offsets.size = bfd_get_section_size (sectp);
12761 }
12762 else
12763 {
12764 /* No other kind of section is valid. */
12765 return 0;
12766 }
12767
12768 return 1;
12769 }
12770
12771 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12772 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12773 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12774 This is for DWP version 1 files. */
12775
12776 static struct dwo_unit *
12777 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12778 struct dwp_file *dwp_file,
12779 uint32_t unit_index,
12780 const char *comp_dir,
12781 ULONGEST signature, int is_debug_types)
12782 {
12783 struct objfile *objfile = dwarf2_per_objfile->objfile;
12784 const struct dwp_hash_table *dwp_htab =
12785 is_debug_types ? dwp_file->tus : dwp_file->cus;
12786 bfd *dbfd = dwp_file->dbfd;
12787 const char *kind = is_debug_types ? "TU" : "CU";
12788 struct dwo_file *dwo_file;
12789 struct dwo_unit *dwo_unit;
12790 struct virtual_v1_dwo_sections sections;
12791 void **dwo_file_slot;
12792 int i;
12793
12794 gdb_assert (dwp_file->version == 1);
12795
12796 if (dwarf_read_debug)
12797 {
12798 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12799 kind,
12800 pulongest (unit_index), hex_string (signature),
12801 dwp_file->name);
12802 }
12803
12804 /* Fetch the sections of this DWO unit.
12805 Put a limit on the number of sections we look for so that bad data
12806 doesn't cause us to loop forever. */
12807
12808 #define MAX_NR_V1_DWO_SECTIONS \
12809 (1 /* .debug_info or .debug_types */ \
12810 + 1 /* .debug_abbrev */ \
12811 + 1 /* .debug_line */ \
12812 + 1 /* .debug_loc */ \
12813 + 1 /* .debug_str_offsets */ \
12814 + 1 /* .debug_macro or .debug_macinfo */ \
12815 + 1 /* trailing zero */)
12816
12817 memset (&sections, 0, sizeof (sections));
12818
12819 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12820 {
12821 asection *sectp;
12822 uint32_t section_nr =
12823 read_4_bytes (dbfd,
12824 dwp_htab->section_pool.v1.indices
12825 + (unit_index + i) * sizeof (uint32_t));
12826
12827 if (section_nr == 0)
12828 break;
12829 if (section_nr >= dwp_file->num_sections)
12830 {
12831 error (_("Dwarf Error: bad DWP hash table, section number too large"
12832 " [in module %s]"),
12833 dwp_file->name);
12834 }
12835
12836 sectp = dwp_file->elf_sections[section_nr];
12837 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12838 {
12839 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12840 " [in module %s]"),
12841 dwp_file->name);
12842 }
12843 }
12844
12845 if (i < 2
12846 || dwarf2_section_empty_p (&sections.info_or_types)
12847 || dwarf2_section_empty_p (&sections.abbrev))
12848 {
12849 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12850 " [in module %s]"),
12851 dwp_file->name);
12852 }
12853 if (i == MAX_NR_V1_DWO_SECTIONS)
12854 {
12855 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12856 " [in module %s]"),
12857 dwp_file->name);
12858 }
12859
12860 /* It's easier for the rest of the code if we fake a struct dwo_file and
12861 have dwo_unit "live" in that. At least for now.
12862
12863 The DWP file can be made up of a random collection of CUs and TUs.
12864 However, for each CU + set of TUs that came from the same original DWO
12865 file, we can combine them back into a virtual DWO file to save space
12866 (fewer struct dwo_file objects to allocate). Remember that for really
12867 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12868
12869 std::string virtual_dwo_name =
12870 string_printf ("virtual-dwo/%d-%d-%d-%d",
12871 get_section_id (&sections.abbrev),
12872 get_section_id (&sections.line),
12873 get_section_id (&sections.loc),
12874 get_section_id (&sections.str_offsets));
12875 /* Can we use an existing virtual DWO file? */
12876 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12877 virtual_dwo_name.c_str (),
12878 comp_dir);
12879 /* Create one if necessary. */
12880 if (*dwo_file_slot == NULL)
12881 {
12882 if (dwarf_read_debug)
12883 {
12884 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12885 virtual_dwo_name.c_str ());
12886 }
12887 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12888 dwo_file->dwo_name
12889 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12890 virtual_dwo_name.c_str (),
12891 virtual_dwo_name.size ());
12892 dwo_file->comp_dir = comp_dir;
12893 dwo_file->sections.abbrev = sections.abbrev;
12894 dwo_file->sections.line = sections.line;
12895 dwo_file->sections.loc = sections.loc;
12896 dwo_file->sections.macinfo = sections.macinfo;
12897 dwo_file->sections.macro = sections.macro;
12898 dwo_file->sections.str_offsets = sections.str_offsets;
12899 /* The "str" section is global to the entire DWP file. */
12900 dwo_file->sections.str = dwp_file->sections.str;
12901 /* The info or types section is assigned below to dwo_unit,
12902 there's no need to record it in dwo_file.
12903 Also, we can't simply record type sections in dwo_file because
12904 we record a pointer into the vector in dwo_unit. As we collect more
12905 types we'll grow the vector and eventually have to reallocate space
12906 for it, invalidating all copies of pointers into the previous
12907 contents. */
12908 *dwo_file_slot = dwo_file;
12909 }
12910 else
12911 {
12912 if (dwarf_read_debug)
12913 {
12914 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12915 virtual_dwo_name.c_str ());
12916 }
12917 dwo_file = (struct dwo_file *) *dwo_file_slot;
12918 }
12919
12920 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12921 dwo_unit->dwo_file = dwo_file;
12922 dwo_unit->signature = signature;
12923 dwo_unit->section =
12924 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12925 *dwo_unit->section = sections.info_or_types;
12926 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12927
12928 return dwo_unit;
12929 }
12930
12931 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12932 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12933 piece within that section used by a TU/CU, return a virtual section
12934 of just that piece. */
12935
12936 static struct dwarf2_section_info
12937 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12938 struct dwarf2_section_info *section,
12939 bfd_size_type offset, bfd_size_type size)
12940 {
12941 struct dwarf2_section_info result;
12942 asection *sectp;
12943
12944 gdb_assert (section != NULL);
12945 gdb_assert (!section->is_virtual);
12946
12947 memset (&result, 0, sizeof (result));
12948 result.s.containing_section = section;
12949 result.is_virtual = 1;
12950
12951 if (size == 0)
12952 return result;
12953
12954 sectp = get_section_bfd_section (section);
12955
12956 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12957 bounds of the real section. This is a pretty-rare event, so just
12958 flag an error (easier) instead of a warning and trying to cope. */
12959 if (sectp == NULL
12960 || offset + size > bfd_get_section_size (sectp))
12961 {
12962 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12963 " in section %s [in module %s]"),
12964 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12965 objfile_name (dwarf2_per_objfile->objfile));
12966 }
12967
12968 result.virtual_offset = offset;
12969 result.size = size;
12970 return result;
12971 }
12972
12973 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12974 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12975 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12976 This is for DWP version 2 files. */
12977
12978 static struct dwo_unit *
12979 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12980 struct dwp_file *dwp_file,
12981 uint32_t unit_index,
12982 const char *comp_dir,
12983 ULONGEST signature, int is_debug_types)
12984 {
12985 struct objfile *objfile = dwarf2_per_objfile->objfile;
12986 const struct dwp_hash_table *dwp_htab =
12987 is_debug_types ? dwp_file->tus : dwp_file->cus;
12988 bfd *dbfd = dwp_file->dbfd;
12989 const char *kind = is_debug_types ? "TU" : "CU";
12990 struct dwo_file *dwo_file;
12991 struct dwo_unit *dwo_unit;
12992 struct virtual_v2_dwo_sections sections;
12993 void **dwo_file_slot;
12994 int i;
12995
12996 gdb_assert (dwp_file->version == 2);
12997
12998 if (dwarf_read_debug)
12999 {
13000 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
13001 kind,
13002 pulongest (unit_index), hex_string (signature),
13003 dwp_file->name);
13004 }
13005
13006 /* Fetch the section offsets of this DWO unit. */
13007
13008 memset (&sections, 0, sizeof (sections));
13009
13010 for (i = 0; i < dwp_htab->nr_columns; ++i)
13011 {
13012 uint32_t offset = read_4_bytes (dbfd,
13013 dwp_htab->section_pool.v2.offsets
13014 + (((unit_index - 1) * dwp_htab->nr_columns
13015 + i)
13016 * sizeof (uint32_t)));
13017 uint32_t size = read_4_bytes (dbfd,
13018 dwp_htab->section_pool.v2.sizes
13019 + (((unit_index - 1) * dwp_htab->nr_columns
13020 + i)
13021 * sizeof (uint32_t)));
13022
13023 switch (dwp_htab->section_pool.v2.section_ids[i])
13024 {
13025 case DW_SECT_INFO:
13026 case DW_SECT_TYPES:
13027 sections.info_or_types_offset = offset;
13028 sections.info_or_types_size = size;
13029 break;
13030 case DW_SECT_ABBREV:
13031 sections.abbrev_offset = offset;
13032 sections.abbrev_size = size;
13033 break;
13034 case DW_SECT_LINE:
13035 sections.line_offset = offset;
13036 sections.line_size = size;
13037 break;
13038 case DW_SECT_LOC:
13039 sections.loc_offset = offset;
13040 sections.loc_size = size;
13041 break;
13042 case DW_SECT_STR_OFFSETS:
13043 sections.str_offsets_offset = offset;
13044 sections.str_offsets_size = size;
13045 break;
13046 case DW_SECT_MACINFO:
13047 sections.macinfo_offset = offset;
13048 sections.macinfo_size = size;
13049 break;
13050 case DW_SECT_MACRO:
13051 sections.macro_offset = offset;
13052 sections.macro_size = size;
13053 break;
13054 }
13055 }
13056
13057 /* It's easier for the rest of the code if we fake a struct dwo_file and
13058 have dwo_unit "live" in that. At least for now.
13059
13060 The DWP file can be made up of a random collection of CUs and TUs.
13061 However, for each CU + set of TUs that came from the same original DWO
13062 file, we can combine them back into a virtual DWO file to save space
13063 (fewer struct dwo_file objects to allocate). Remember that for really
13064 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
13065
13066 std::string virtual_dwo_name =
13067 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
13068 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
13069 (long) (sections.line_size ? sections.line_offset : 0),
13070 (long) (sections.loc_size ? sections.loc_offset : 0),
13071 (long) (sections.str_offsets_size
13072 ? sections.str_offsets_offset : 0));
13073 /* Can we use an existing virtual DWO file? */
13074 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13075 virtual_dwo_name.c_str (),
13076 comp_dir);
13077 /* Create one if necessary. */
13078 if (*dwo_file_slot == NULL)
13079 {
13080 if (dwarf_read_debug)
13081 {
13082 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
13083 virtual_dwo_name.c_str ());
13084 }
13085 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
13086 dwo_file->dwo_name
13087 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
13088 virtual_dwo_name.c_str (),
13089 virtual_dwo_name.size ());
13090 dwo_file->comp_dir = comp_dir;
13091 dwo_file->sections.abbrev =
13092 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
13093 sections.abbrev_offset, sections.abbrev_size);
13094 dwo_file->sections.line =
13095 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
13096 sections.line_offset, sections.line_size);
13097 dwo_file->sections.loc =
13098 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
13099 sections.loc_offset, sections.loc_size);
13100 dwo_file->sections.macinfo =
13101 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
13102 sections.macinfo_offset, sections.macinfo_size);
13103 dwo_file->sections.macro =
13104 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
13105 sections.macro_offset, sections.macro_size);
13106 dwo_file->sections.str_offsets =
13107 create_dwp_v2_section (dwarf2_per_objfile,
13108 &dwp_file->sections.str_offsets,
13109 sections.str_offsets_offset,
13110 sections.str_offsets_size);
13111 /* The "str" section is global to the entire DWP file. */
13112 dwo_file->sections.str = dwp_file->sections.str;
13113 /* The info or types section is assigned below to dwo_unit,
13114 there's no need to record it in dwo_file.
13115 Also, we can't simply record type sections in dwo_file because
13116 we record a pointer into the vector in dwo_unit. As we collect more
13117 types we'll grow the vector and eventually have to reallocate space
13118 for it, invalidating all copies of pointers into the previous
13119 contents. */
13120 *dwo_file_slot = dwo_file;
13121 }
13122 else
13123 {
13124 if (dwarf_read_debug)
13125 {
13126 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
13127 virtual_dwo_name.c_str ());
13128 }
13129 dwo_file = (struct dwo_file *) *dwo_file_slot;
13130 }
13131
13132 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
13133 dwo_unit->dwo_file = dwo_file;
13134 dwo_unit->signature = signature;
13135 dwo_unit->section =
13136 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
13137 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
13138 is_debug_types
13139 ? &dwp_file->sections.types
13140 : &dwp_file->sections.info,
13141 sections.info_or_types_offset,
13142 sections.info_or_types_size);
13143 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
13144
13145 return dwo_unit;
13146 }
13147
13148 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
13149 Returns NULL if the signature isn't found. */
13150
13151 static struct dwo_unit *
13152 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
13153 struct dwp_file *dwp_file, const char *comp_dir,
13154 ULONGEST signature, int is_debug_types)
13155 {
13156 const struct dwp_hash_table *dwp_htab =
13157 is_debug_types ? dwp_file->tus : dwp_file->cus;
13158 bfd *dbfd = dwp_file->dbfd;
13159 uint32_t mask = dwp_htab->nr_slots - 1;
13160 uint32_t hash = signature & mask;
13161 uint32_t hash2 = ((signature >> 32) & mask) | 1;
13162 unsigned int i;
13163 void **slot;
13164 struct dwo_unit find_dwo_cu;
13165
13166 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
13167 find_dwo_cu.signature = signature;
13168 slot = htab_find_slot (is_debug_types
13169 ? dwp_file->loaded_tus
13170 : dwp_file->loaded_cus,
13171 &find_dwo_cu, INSERT);
13172
13173 if (*slot != NULL)
13174 return (struct dwo_unit *) *slot;
13175
13176 /* Use a for loop so that we don't loop forever on bad debug info. */
13177 for (i = 0; i < dwp_htab->nr_slots; ++i)
13178 {
13179 ULONGEST signature_in_table;
13180
13181 signature_in_table =
13182 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
13183 if (signature_in_table == signature)
13184 {
13185 uint32_t unit_index =
13186 read_4_bytes (dbfd,
13187 dwp_htab->unit_table + hash * sizeof (uint32_t));
13188
13189 if (dwp_file->version == 1)
13190 {
13191 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
13192 dwp_file, unit_index,
13193 comp_dir, signature,
13194 is_debug_types);
13195 }
13196 else
13197 {
13198 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
13199 dwp_file, unit_index,
13200 comp_dir, signature,
13201 is_debug_types);
13202 }
13203 return (struct dwo_unit *) *slot;
13204 }
13205 if (signature_in_table == 0)
13206 return NULL;
13207 hash = (hash + hash2) & mask;
13208 }
13209
13210 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
13211 " [in module %s]"),
13212 dwp_file->name);
13213 }
13214
13215 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
13216 Open the file specified by FILE_NAME and hand it off to BFD for
13217 preliminary analysis. Return a newly initialized bfd *, which
13218 includes a canonicalized copy of FILE_NAME.
13219 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
13220 SEARCH_CWD is true if the current directory is to be searched.
13221 It will be searched before debug-file-directory.
13222 If successful, the file is added to the bfd include table of the
13223 objfile's bfd (see gdb_bfd_record_inclusion).
13224 If unable to find/open the file, return NULL.
13225 NOTE: This function is derived from symfile_bfd_open. */
13226
13227 static gdb_bfd_ref_ptr
13228 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13229 const char *file_name, int is_dwp, int search_cwd)
13230 {
13231 int desc;
13232 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
13233 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
13234 to debug_file_directory. */
13235 const char *search_path;
13236 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
13237
13238 gdb::unique_xmalloc_ptr<char> search_path_holder;
13239 if (search_cwd)
13240 {
13241 if (*debug_file_directory != '\0')
13242 {
13243 search_path_holder.reset (concat (".", dirname_separator_string,
13244 debug_file_directory,
13245 (char *) NULL));
13246 search_path = search_path_holder.get ();
13247 }
13248 else
13249 search_path = ".";
13250 }
13251 else
13252 search_path = debug_file_directory;
13253
13254 openp_flags flags = OPF_RETURN_REALPATH;
13255 if (is_dwp)
13256 flags |= OPF_SEARCH_IN_PATH;
13257
13258 gdb::unique_xmalloc_ptr<char> absolute_name;
13259 desc = openp (search_path, flags, file_name,
13260 O_RDONLY | O_BINARY, &absolute_name);
13261 if (desc < 0)
13262 return NULL;
13263
13264 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
13265 gnutarget, desc));
13266 if (sym_bfd == NULL)
13267 return NULL;
13268 bfd_set_cacheable (sym_bfd.get (), 1);
13269
13270 if (!bfd_check_format (sym_bfd.get (), bfd_object))
13271 return NULL;
13272
13273 /* Success. Record the bfd as having been included by the objfile's bfd.
13274 This is important because things like demangled_names_hash lives in the
13275 objfile's per_bfd space and may have references to things like symbol
13276 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
13277 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13278
13279 return sym_bfd;
13280 }
13281
13282 /* Try to open DWO file FILE_NAME.
13283 COMP_DIR is the DW_AT_comp_dir attribute.
13284 The result is the bfd handle of the file.
13285 If there is a problem finding or opening the file, return NULL.
13286 Upon success, the canonicalized path of the file is stored in the bfd,
13287 same as symfile_bfd_open. */
13288
13289 static gdb_bfd_ref_ptr
13290 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13291 const char *file_name, const char *comp_dir)
13292 {
13293 if (IS_ABSOLUTE_PATH (file_name))
13294 return try_open_dwop_file (dwarf2_per_objfile, file_name,
13295 0 /*is_dwp*/, 0 /*search_cwd*/);
13296
13297 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
13298
13299 if (comp_dir != NULL)
13300 {
13301 char *path_to_try = concat (comp_dir, SLASH_STRING,
13302 file_name, (char *) NULL);
13303
13304 /* NOTE: If comp_dir is a relative path, this will also try the
13305 search path, which seems useful. */
13306 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
13307 path_to_try,
13308 0 /*is_dwp*/,
13309 1 /*search_cwd*/));
13310 xfree (path_to_try);
13311 if (abfd != NULL)
13312 return abfd;
13313 }
13314
13315 /* That didn't work, try debug-file-directory, which, despite its name,
13316 is a list of paths. */
13317
13318 if (*debug_file_directory == '\0')
13319 return NULL;
13320
13321 return try_open_dwop_file (dwarf2_per_objfile, file_name,
13322 0 /*is_dwp*/, 1 /*search_cwd*/);
13323 }
13324
13325 /* This function is mapped across the sections and remembers the offset and
13326 size of each of the DWO debugging sections we are interested in. */
13327
13328 static void
13329 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
13330 {
13331 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
13332 const struct dwop_section_names *names = &dwop_section_names;
13333
13334 if (section_is_p (sectp->name, &names->abbrev_dwo))
13335 {
13336 dwo_sections->abbrev.s.section = sectp;
13337 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
13338 }
13339 else if (section_is_p (sectp->name, &names->info_dwo))
13340 {
13341 dwo_sections->info.s.section = sectp;
13342 dwo_sections->info.size = bfd_get_section_size (sectp);
13343 }
13344 else if (section_is_p (sectp->name, &names->line_dwo))
13345 {
13346 dwo_sections->line.s.section = sectp;
13347 dwo_sections->line.size = bfd_get_section_size (sectp);
13348 }
13349 else if (section_is_p (sectp->name, &names->loc_dwo))
13350 {
13351 dwo_sections->loc.s.section = sectp;
13352 dwo_sections->loc.size = bfd_get_section_size (sectp);
13353 }
13354 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13355 {
13356 dwo_sections->macinfo.s.section = sectp;
13357 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
13358 }
13359 else if (section_is_p (sectp->name, &names->macro_dwo))
13360 {
13361 dwo_sections->macro.s.section = sectp;
13362 dwo_sections->macro.size = bfd_get_section_size (sectp);
13363 }
13364 else if (section_is_p (sectp->name, &names->str_dwo))
13365 {
13366 dwo_sections->str.s.section = sectp;
13367 dwo_sections->str.size = bfd_get_section_size (sectp);
13368 }
13369 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13370 {
13371 dwo_sections->str_offsets.s.section = sectp;
13372 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
13373 }
13374 else if (section_is_p (sectp->name, &names->types_dwo))
13375 {
13376 struct dwarf2_section_info type_section;
13377
13378 memset (&type_section, 0, sizeof (type_section));
13379 type_section.s.section = sectp;
13380 type_section.size = bfd_get_section_size (sectp);
13381 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
13382 &type_section);
13383 }
13384 }
13385
13386 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13387 by PER_CU. This is for the non-DWP case.
13388 The result is NULL if DWO_NAME can't be found. */
13389
13390 static struct dwo_file *
13391 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13392 const char *dwo_name, const char *comp_dir)
13393 {
13394 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13395 struct objfile *objfile = dwarf2_per_objfile->objfile;
13396 struct dwo_file *dwo_file;
13397 struct cleanup *cleanups;
13398
13399 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
13400 if (dbfd == NULL)
13401 {
13402 if (dwarf_read_debug)
13403 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13404 return NULL;
13405 }
13406 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
13407 dwo_file->dwo_name = dwo_name;
13408 dwo_file->comp_dir = comp_dir;
13409 dwo_file->dbfd = dbfd.release ();
13410
13411 free_dwo_file_cleanup_data *cleanup_data = XNEW (free_dwo_file_cleanup_data);
13412 cleanup_data->dwo_file = dwo_file;
13413 cleanup_data->dwarf2_per_objfile = dwarf2_per_objfile;
13414
13415 cleanups = make_cleanup (free_dwo_file_cleanup, cleanup_data);
13416
13417 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13418 &dwo_file->sections);
13419
13420 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13421 dwo_file->cus);
13422
13423 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file,
13424 dwo_file->sections.types, dwo_file->tus);
13425
13426 discard_cleanups (cleanups);
13427
13428 if (dwarf_read_debug)
13429 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13430
13431 return dwo_file;
13432 }
13433
13434 /* This function is mapped across the sections and remembers the offset and
13435 size of each of the DWP debugging sections common to version 1 and 2 that
13436 we are interested in. */
13437
13438 static void
13439 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13440 void *dwp_file_ptr)
13441 {
13442 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13443 const struct dwop_section_names *names = &dwop_section_names;
13444 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13445
13446 /* Record the ELF section number for later lookup: this is what the
13447 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13448 gdb_assert (elf_section_nr < dwp_file->num_sections);
13449 dwp_file->elf_sections[elf_section_nr] = sectp;
13450
13451 /* Look for specific sections that we need. */
13452 if (section_is_p (sectp->name, &names->str_dwo))
13453 {
13454 dwp_file->sections.str.s.section = sectp;
13455 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13456 }
13457 else if (section_is_p (sectp->name, &names->cu_index))
13458 {
13459 dwp_file->sections.cu_index.s.section = sectp;
13460 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13461 }
13462 else if (section_is_p (sectp->name, &names->tu_index))
13463 {
13464 dwp_file->sections.tu_index.s.section = sectp;
13465 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13466 }
13467 }
13468
13469 /* This function is mapped across the sections and remembers the offset and
13470 size of each of the DWP version 2 debugging sections that we are interested
13471 in. This is split into a separate function because we don't know if we
13472 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13473
13474 static void
13475 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13476 {
13477 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13478 const struct dwop_section_names *names = &dwop_section_names;
13479 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13480
13481 /* Record the ELF section number for later lookup: this is what the
13482 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13483 gdb_assert (elf_section_nr < dwp_file->num_sections);
13484 dwp_file->elf_sections[elf_section_nr] = sectp;
13485
13486 /* Look for specific sections that we need. */
13487 if (section_is_p (sectp->name, &names->abbrev_dwo))
13488 {
13489 dwp_file->sections.abbrev.s.section = sectp;
13490 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13491 }
13492 else if (section_is_p (sectp->name, &names->info_dwo))
13493 {
13494 dwp_file->sections.info.s.section = sectp;
13495 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13496 }
13497 else if (section_is_p (sectp->name, &names->line_dwo))
13498 {
13499 dwp_file->sections.line.s.section = sectp;
13500 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13501 }
13502 else if (section_is_p (sectp->name, &names->loc_dwo))
13503 {
13504 dwp_file->sections.loc.s.section = sectp;
13505 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13506 }
13507 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13508 {
13509 dwp_file->sections.macinfo.s.section = sectp;
13510 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13511 }
13512 else if (section_is_p (sectp->name, &names->macro_dwo))
13513 {
13514 dwp_file->sections.macro.s.section = sectp;
13515 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13516 }
13517 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13518 {
13519 dwp_file->sections.str_offsets.s.section = sectp;
13520 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13521 }
13522 else if (section_is_p (sectp->name, &names->types_dwo))
13523 {
13524 dwp_file->sections.types.s.section = sectp;
13525 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13526 }
13527 }
13528
13529 /* Hash function for dwp_file loaded CUs/TUs. */
13530
13531 static hashval_t
13532 hash_dwp_loaded_cutus (const void *item)
13533 {
13534 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13535
13536 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13537 return dwo_unit->signature;
13538 }
13539
13540 /* Equality function for dwp_file loaded CUs/TUs. */
13541
13542 static int
13543 eq_dwp_loaded_cutus (const void *a, const void *b)
13544 {
13545 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13546 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13547
13548 return dua->signature == dub->signature;
13549 }
13550
13551 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13552
13553 static htab_t
13554 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13555 {
13556 return htab_create_alloc_ex (3,
13557 hash_dwp_loaded_cutus,
13558 eq_dwp_loaded_cutus,
13559 NULL,
13560 &objfile->objfile_obstack,
13561 hashtab_obstack_allocate,
13562 dummy_obstack_deallocate);
13563 }
13564
13565 /* Try to open DWP file FILE_NAME.
13566 The result is the bfd handle of the file.
13567 If there is a problem finding or opening the file, return NULL.
13568 Upon success, the canonicalized path of the file is stored in the bfd,
13569 same as symfile_bfd_open. */
13570
13571 static gdb_bfd_ref_ptr
13572 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13573 const char *file_name)
13574 {
13575 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13576 1 /*is_dwp*/,
13577 1 /*search_cwd*/));
13578 if (abfd != NULL)
13579 return abfd;
13580
13581 /* Work around upstream bug 15652.
13582 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13583 [Whether that's a "bug" is debatable, but it is getting in our way.]
13584 We have no real idea where the dwp file is, because gdb's realpath-ing
13585 of the executable's path may have discarded the needed info.
13586 [IWBN if the dwp file name was recorded in the executable, akin to
13587 .gnu_debuglink, but that doesn't exist yet.]
13588 Strip the directory from FILE_NAME and search again. */
13589 if (*debug_file_directory != '\0')
13590 {
13591 /* Don't implicitly search the current directory here.
13592 If the user wants to search "." to handle this case,
13593 it must be added to debug-file-directory. */
13594 return try_open_dwop_file (dwarf2_per_objfile,
13595 lbasename (file_name), 1 /*is_dwp*/,
13596 0 /*search_cwd*/);
13597 }
13598
13599 return NULL;
13600 }
13601
13602 /* Initialize the use of the DWP file for the current objfile.
13603 By convention the name of the DWP file is ${objfile}.dwp.
13604 The result is NULL if it can't be found. */
13605
13606 static struct dwp_file *
13607 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13608 {
13609 struct objfile *objfile = dwarf2_per_objfile->objfile;
13610 struct dwp_file *dwp_file;
13611
13612 /* Try to find first .dwp for the binary file before any symbolic links
13613 resolving. */
13614
13615 /* If the objfile is a debug file, find the name of the real binary
13616 file and get the name of dwp file from there. */
13617 std::string dwp_name;
13618 if (objfile->separate_debug_objfile_backlink != NULL)
13619 {
13620 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13621 const char *backlink_basename = lbasename (backlink->original_name);
13622
13623 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13624 }
13625 else
13626 dwp_name = objfile->original_name;
13627
13628 dwp_name += ".dwp";
13629
13630 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13631 if (dbfd == NULL
13632 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13633 {
13634 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13635 dwp_name = objfile_name (objfile);
13636 dwp_name += ".dwp";
13637 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13638 }
13639
13640 if (dbfd == NULL)
13641 {
13642 if (dwarf_read_debug)
13643 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13644 return NULL;
13645 }
13646 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
13647 dwp_file->name = bfd_get_filename (dbfd.get ());
13648 dwp_file->dbfd = dbfd.release ();
13649
13650 /* +1: section 0 is unused */
13651 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13652 dwp_file->elf_sections =
13653 OBSTACK_CALLOC (&objfile->objfile_obstack,
13654 dwp_file->num_sections, asection *);
13655
13656 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13657 dwp_file);
13658
13659 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
13660
13661 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
13662
13663 /* The DWP file version is stored in the hash table. Oh well. */
13664 if (dwp_file->cus && dwp_file->tus
13665 && dwp_file->cus->version != dwp_file->tus->version)
13666 {
13667 /* Technically speaking, we should try to limp along, but this is
13668 pretty bizarre. We use pulongest here because that's the established
13669 portability solution (e.g, we cannot use %u for uint32_t). */
13670 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13671 " TU version %s [in DWP file %s]"),
13672 pulongest (dwp_file->cus->version),
13673 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13674 }
13675
13676 if (dwp_file->cus)
13677 dwp_file->version = dwp_file->cus->version;
13678 else if (dwp_file->tus)
13679 dwp_file->version = dwp_file->tus->version;
13680 else
13681 dwp_file->version = 2;
13682
13683 if (dwp_file->version == 2)
13684 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13685 dwp_file);
13686
13687 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13688 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13689
13690 if (dwarf_read_debug)
13691 {
13692 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13693 fprintf_unfiltered (gdb_stdlog,
13694 " %s CUs, %s TUs\n",
13695 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13696 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13697 }
13698
13699 return dwp_file;
13700 }
13701
13702 /* Wrapper around open_and_init_dwp_file, only open it once. */
13703
13704 static struct dwp_file *
13705 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13706 {
13707 if (! dwarf2_per_objfile->dwp_checked)
13708 {
13709 dwarf2_per_objfile->dwp_file
13710 = open_and_init_dwp_file (dwarf2_per_objfile);
13711 dwarf2_per_objfile->dwp_checked = 1;
13712 }
13713 return dwarf2_per_objfile->dwp_file;
13714 }
13715
13716 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13717 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13718 or in the DWP file for the objfile, referenced by THIS_UNIT.
13719 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13720 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13721
13722 This is called, for example, when wanting to read a variable with a
13723 complex location. Therefore we don't want to do file i/o for every call.
13724 Therefore we don't want to look for a DWO file on every call.
13725 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13726 then we check if we've already seen DWO_NAME, and only THEN do we check
13727 for a DWO file.
13728
13729 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13730 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13731
13732 static struct dwo_unit *
13733 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13734 const char *dwo_name, const char *comp_dir,
13735 ULONGEST signature, int is_debug_types)
13736 {
13737 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13738 struct objfile *objfile = dwarf2_per_objfile->objfile;
13739 const char *kind = is_debug_types ? "TU" : "CU";
13740 void **dwo_file_slot;
13741 struct dwo_file *dwo_file;
13742 struct dwp_file *dwp_file;
13743
13744 /* First see if there's a DWP file.
13745 If we have a DWP file but didn't find the DWO inside it, don't
13746 look for the original DWO file. It makes gdb behave differently
13747 depending on whether one is debugging in the build tree. */
13748
13749 dwp_file = get_dwp_file (dwarf2_per_objfile);
13750 if (dwp_file != NULL)
13751 {
13752 const struct dwp_hash_table *dwp_htab =
13753 is_debug_types ? dwp_file->tus : dwp_file->cus;
13754
13755 if (dwp_htab != NULL)
13756 {
13757 struct dwo_unit *dwo_cutu =
13758 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13759 signature, is_debug_types);
13760
13761 if (dwo_cutu != NULL)
13762 {
13763 if (dwarf_read_debug)
13764 {
13765 fprintf_unfiltered (gdb_stdlog,
13766 "Virtual DWO %s %s found: @%s\n",
13767 kind, hex_string (signature),
13768 host_address_to_string (dwo_cutu));
13769 }
13770 return dwo_cutu;
13771 }
13772 }
13773 }
13774 else
13775 {
13776 /* No DWP file, look for the DWO file. */
13777
13778 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13779 dwo_name, comp_dir);
13780 if (*dwo_file_slot == NULL)
13781 {
13782 /* Read in the file and build a table of the CUs/TUs it contains. */
13783 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13784 }
13785 /* NOTE: This will be NULL if unable to open the file. */
13786 dwo_file = (struct dwo_file *) *dwo_file_slot;
13787
13788 if (dwo_file != NULL)
13789 {
13790 struct dwo_unit *dwo_cutu = NULL;
13791
13792 if (is_debug_types && dwo_file->tus)
13793 {
13794 struct dwo_unit find_dwo_cutu;
13795
13796 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13797 find_dwo_cutu.signature = signature;
13798 dwo_cutu
13799 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13800 }
13801 else if (!is_debug_types && dwo_file->cus)
13802 {
13803 struct dwo_unit find_dwo_cutu;
13804
13805 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13806 find_dwo_cutu.signature = signature;
13807 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13808 &find_dwo_cutu);
13809 }
13810
13811 if (dwo_cutu != NULL)
13812 {
13813 if (dwarf_read_debug)
13814 {
13815 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13816 kind, dwo_name, hex_string (signature),
13817 host_address_to_string (dwo_cutu));
13818 }
13819 return dwo_cutu;
13820 }
13821 }
13822 }
13823
13824 /* We didn't find it. This could mean a dwo_id mismatch, or
13825 someone deleted the DWO/DWP file, or the search path isn't set up
13826 correctly to find the file. */
13827
13828 if (dwarf_read_debug)
13829 {
13830 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13831 kind, dwo_name, hex_string (signature));
13832 }
13833
13834 /* This is a warning and not a complaint because it can be caused by
13835 pilot error (e.g., user accidentally deleting the DWO). */
13836 {
13837 /* Print the name of the DWP file if we looked there, helps the user
13838 better diagnose the problem. */
13839 std::string dwp_text;
13840
13841 if (dwp_file != NULL)
13842 dwp_text = string_printf (" [in DWP file %s]",
13843 lbasename (dwp_file->name));
13844
13845 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13846 " [in module %s]"),
13847 kind, dwo_name, hex_string (signature),
13848 dwp_text.c_str (),
13849 this_unit->is_debug_types ? "TU" : "CU",
13850 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13851 }
13852 return NULL;
13853 }
13854
13855 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13856 See lookup_dwo_cutu_unit for details. */
13857
13858 static struct dwo_unit *
13859 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13860 const char *dwo_name, const char *comp_dir,
13861 ULONGEST signature)
13862 {
13863 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13864 }
13865
13866 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13867 See lookup_dwo_cutu_unit for details. */
13868
13869 static struct dwo_unit *
13870 lookup_dwo_type_unit (struct signatured_type *this_tu,
13871 const char *dwo_name, const char *comp_dir)
13872 {
13873 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13874 }
13875
13876 /* Traversal function for queue_and_load_all_dwo_tus. */
13877
13878 static int
13879 queue_and_load_dwo_tu (void **slot, void *info)
13880 {
13881 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13882 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13883 ULONGEST signature = dwo_unit->signature;
13884 struct signatured_type *sig_type =
13885 lookup_dwo_signatured_type (per_cu->cu, signature);
13886
13887 if (sig_type != NULL)
13888 {
13889 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13890
13891 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13892 a real dependency of PER_CU on SIG_TYPE. That is detected later
13893 while processing PER_CU. */
13894 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13895 load_full_type_unit (sig_cu);
13896 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13897 }
13898
13899 return 1;
13900 }
13901
13902 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13903 The DWO may have the only definition of the type, though it may not be
13904 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13905 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13906
13907 static void
13908 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13909 {
13910 struct dwo_unit *dwo_unit;
13911 struct dwo_file *dwo_file;
13912
13913 gdb_assert (!per_cu->is_debug_types);
13914 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13915 gdb_assert (per_cu->cu != NULL);
13916
13917 dwo_unit = per_cu->cu->dwo_unit;
13918 gdb_assert (dwo_unit != NULL);
13919
13920 dwo_file = dwo_unit->dwo_file;
13921 if (dwo_file->tus != NULL)
13922 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13923 }
13924
13925 /* Free all resources associated with DWO_FILE.
13926 Close the DWO file and munmap the sections.
13927 All memory should be on the objfile obstack. */
13928
13929 static void
13930 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
13931 {
13932
13933 /* Note: dbfd is NULL for virtual DWO files. */
13934 gdb_bfd_unref (dwo_file->dbfd);
13935
13936 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13937 }
13938
13939 /* Wrapper for free_dwo_file for use in cleanups. */
13940
13941 static void
13942 free_dwo_file_cleanup (void *arg)
13943 {
13944 struct free_dwo_file_cleanup_data *data
13945 = (struct free_dwo_file_cleanup_data *) arg;
13946 struct objfile *objfile = data->dwarf2_per_objfile->objfile;
13947
13948 free_dwo_file (data->dwo_file, objfile);
13949
13950 xfree (data);
13951 }
13952
13953 /* Traversal function for free_dwo_files. */
13954
13955 static int
13956 free_dwo_file_from_slot (void **slot, void *info)
13957 {
13958 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13959 struct objfile *objfile = (struct objfile *) info;
13960
13961 free_dwo_file (dwo_file, objfile);
13962
13963 return 1;
13964 }
13965
13966 /* Free all resources associated with DWO_FILES. */
13967
13968 static void
13969 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13970 {
13971 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13972 }
13973 \f
13974 /* Read in various DIEs. */
13975
13976 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13977 Inherit only the children of the DW_AT_abstract_origin DIE not being
13978 already referenced by DW_AT_abstract_origin from the children of the
13979 current DIE. */
13980
13981 static void
13982 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13983 {
13984 struct die_info *child_die;
13985 sect_offset *offsetp;
13986 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13987 struct die_info *origin_die;
13988 /* Iterator of the ORIGIN_DIE children. */
13989 struct die_info *origin_child_die;
13990 struct attribute *attr;
13991 struct dwarf2_cu *origin_cu;
13992 struct pending **origin_previous_list_in_scope;
13993
13994 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13995 if (!attr)
13996 return;
13997
13998 /* Note that following die references may follow to a die in a
13999 different cu. */
14000
14001 origin_cu = cu;
14002 origin_die = follow_die_ref (die, attr, &origin_cu);
14003
14004 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
14005 symbols in. */
14006 origin_previous_list_in_scope = origin_cu->list_in_scope;
14007 origin_cu->list_in_scope = cu->list_in_scope;
14008
14009 if (die->tag != origin_die->tag
14010 && !(die->tag == DW_TAG_inlined_subroutine
14011 && origin_die->tag == DW_TAG_subprogram))
14012 complaint (&symfile_complaints,
14013 _("DIE %s and its abstract origin %s have different tags"),
14014 sect_offset_str (die->sect_off),
14015 sect_offset_str (origin_die->sect_off));
14016
14017 std::vector<sect_offset> offsets;
14018
14019 for (child_die = die->child;
14020 child_die && child_die->tag;
14021 child_die = sibling_die (child_die))
14022 {
14023 struct die_info *child_origin_die;
14024 struct dwarf2_cu *child_origin_cu;
14025
14026 /* We are trying to process concrete instance entries:
14027 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
14028 it's not relevant to our analysis here. i.e. detecting DIEs that are
14029 present in the abstract instance but not referenced in the concrete
14030 one. */
14031 if (child_die->tag == DW_TAG_call_site
14032 || child_die->tag == DW_TAG_GNU_call_site)
14033 continue;
14034
14035 /* For each CHILD_DIE, find the corresponding child of
14036 ORIGIN_DIE. If there is more than one layer of
14037 DW_AT_abstract_origin, follow them all; there shouldn't be,
14038 but GCC versions at least through 4.4 generate this (GCC PR
14039 40573). */
14040 child_origin_die = child_die;
14041 child_origin_cu = cu;
14042 while (1)
14043 {
14044 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
14045 child_origin_cu);
14046 if (attr == NULL)
14047 break;
14048 child_origin_die = follow_die_ref (child_origin_die, attr,
14049 &child_origin_cu);
14050 }
14051
14052 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
14053 counterpart may exist. */
14054 if (child_origin_die != child_die)
14055 {
14056 if (child_die->tag != child_origin_die->tag
14057 && !(child_die->tag == DW_TAG_inlined_subroutine
14058 && child_origin_die->tag == DW_TAG_subprogram))
14059 complaint (&symfile_complaints,
14060 _("Child DIE %s and its abstract origin %s have "
14061 "different tags"),
14062 sect_offset_str (child_die->sect_off),
14063 sect_offset_str (child_origin_die->sect_off));
14064 if (child_origin_die->parent != origin_die)
14065 complaint (&symfile_complaints,
14066 _("Child DIE %s and its abstract origin %s have "
14067 "different parents"),
14068 sect_offset_str (child_die->sect_off),
14069 sect_offset_str (child_origin_die->sect_off));
14070 else
14071 offsets.push_back (child_origin_die->sect_off);
14072 }
14073 }
14074 std::sort (offsets.begin (), offsets.end ());
14075 sect_offset *offsets_end = offsets.data () + offsets.size ();
14076 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
14077 if (offsetp[-1] == *offsetp)
14078 complaint (&symfile_complaints,
14079 _("Multiple children of DIE %s refer "
14080 "to DIE %s as their abstract origin"),
14081 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
14082
14083 offsetp = offsets.data ();
14084 origin_child_die = origin_die->child;
14085 while (origin_child_die && origin_child_die->tag)
14086 {
14087 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
14088 while (offsetp < offsets_end
14089 && *offsetp < origin_child_die->sect_off)
14090 offsetp++;
14091 if (offsetp >= offsets_end
14092 || *offsetp > origin_child_die->sect_off)
14093 {
14094 /* Found that ORIGIN_CHILD_DIE is really not referenced.
14095 Check whether we're already processing ORIGIN_CHILD_DIE.
14096 This can happen with mutually referenced abstract_origins.
14097 PR 16581. */
14098 if (!origin_child_die->in_process)
14099 process_die (origin_child_die, origin_cu);
14100 }
14101 origin_child_die = sibling_die (origin_child_die);
14102 }
14103 origin_cu->list_in_scope = origin_previous_list_in_scope;
14104 }
14105
14106 static void
14107 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
14108 {
14109 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14110 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14111 struct context_stack *newobj;
14112 CORE_ADDR lowpc;
14113 CORE_ADDR highpc;
14114 struct die_info *child_die;
14115 struct attribute *attr, *call_line, *call_file;
14116 const char *name;
14117 CORE_ADDR baseaddr;
14118 struct block *block;
14119 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
14120 std::vector<struct symbol *> template_args;
14121 struct template_symbol *templ_func = NULL;
14122
14123 if (inlined_func)
14124 {
14125 /* If we do not have call site information, we can't show the
14126 caller of this inlined function. That's too confusing, so
14127 only use the scope for local variables. */
14128 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
14129 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
14130 if (call_line == NULL || call_file == NULL)
14131 {
14132 read_lexical_block_scope (die, cu);
14133 return;
14134 }
14135 }
14136
14137 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14138
14139 name = dwarf2_name (die, cu);
14140
14141 /* Ignore functions with missing or empty names. These are actually
14142 illegal according to the DWARF standard. */
14143 if (name == NULL)
14144 {
14145 complaint (&symfile_complaints,
14146 _("missing name for subprogram DIE at %s"),
14147 sect_offset_str (die->sect_off));
14148 return;
14149 }
14150
14151 /* Ignore functions with missing or invalid low and high pc attributes. */
14152 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
14153 <= PC_BOUNDS_INVALID)
14154 {
14155 attr = dwarf2_attr (die, DW_AT_external, cu);
14156 if (!attr || !DW_UNSND (attr))
14157 complaint (&symfile_complaints,
14158 _("cannot get low and high bounds "
14159 "for subprogram DIE at %s"),
14160 sect_offset_str (die->sect_off));
14161 return;
14162 }
14163
14164 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14165 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
14166
14167 /* If we have any template arguments, then we must allocate a
14168 different sort of symbol. */
14169 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
14170 {
14171 if (child_die->tag == DW_TAG_template_type_param
14172 || child_die->tag == DW_TAG_template_value_param)
14173 {
14174 templ_func = allocate_template_symbol (objfile);
14175 templ_func->subclass = SYMBOL_TEMPLATE;
14176 break;
14177 }
14178 }
14179
14180 newobj = push_context (0, lowpc);
14181 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
14182 (struct symbol *) templ_func);
14183
14184 /* If there is a location expression for DW_AT_frame_base, record
14185 it. */
14186 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
14187 if (attr)
14188 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
14189
14190 /* If there is a location for the static link, record it. */
14191 newobj->static_link = NULL;
14192 attr = dwarf2_attr (die, DW_AT_static_link, cu);
14193 if (attr)
14194 {
14195 newobj->static_link
14196 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
14197 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
14198 }
14199
14200 cu->list_in_scope = &local_symbols;
14201
14202 if (die->child != NULL)
14203 {
14204 child_die = die->child;
14205 while (child_die && child_die->tag)
14206 {
14207 if (child_die->tag == DW_TAG_template_type_param
14208 || child_die->tag == DW_TAG_template_value_param)
14209 {
14210 struct symbol *arg = new_symbol (child_die, NULL, cu);
14211
14212 if (arg != NULL)
14213 template_args.push_back (arg);
14214 }
14215 else
14216 process_die (child_die, cu);
14217 child_die = sibling_die (child_die);
14218 }
14219 }
14220
14221 inherit_abstract_dies (die, cu);
14222
14223 /* If we have a DW_AT_specification, we might need to import using
14224 directives from the context of the specification DIE. See the
14225 comment in determine_prefix. */
14226 if (cu->language == language_cplus
14227 && dwarf2_attr (die, DW_AT_specification, cu))
14228 {
14229 struct dwarf2_cu *spec_cu = cu;
14230 struct die_info *spec_die = die_specification (die, &spec_cu);
14231
14232 while (spec_die)
14233 {
14234 child_die = spec_die->child;
14235 while (child_die && child_die->tag)
14236 {
14237 if (child_die->tag == DW_TAG_imported_module)
14238 process_die (child_die, spec_cu);
14239 child_die = sibling_die (child_die);
14240 }
14241
14242 /* In some cases, GCC generates specification DIEs that
14243 themselves contain DW_AT_specification attributes. */
14244 spec_die = die_specification (spec_die, &spec_cu);
14245 }
14246 }
14247
14248 newobj = pop_context ();
14249 /* Make a block for the local symbols within. */
14250 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
14251 newobj->static_link, lowpc, highpc);
14252
14253 /* For C++, set the block's scope. */
14254 if ((cu->language == language_cplus
14255 || cu->language == language_fortran
14256 || cu->language == language_d
14257 || cu->language == language_rust)
14258 && cu->processing_has_namespace_info)
14259 block_set_scope (block, determine_prefix (die, cu),
14260 &objfile->objfile_obstack);
14261
14262 /* If we have address ranges, record them. */
14263 dwarf2_record_block_ranges (die, block, baseaddr, cu);
14264
14265 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
14266
14267 /* Attach template arguments to function. */
14268 if (!template_args.empty ())
14269 {
14270 gdb_assert (templ_func != NULL);
14271
14272 templ_func->n_template_arguments = template_args.size ();
14273 templ_func->template_arguments
14274 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
14275 templ_func->n_template_arguments);
14276 memcpy (templ_func->template_arguments,
14277 template_args.data (),
14278 (templ_func->n_template_arguments * sizeof (struct symbol *)));
14279 }
14280
14281 /* In C++, we can have functions nested inside functions (e.g., when
14282 a function declares a class that has methods). This means that
14283 when we finish processing a function scope, we may need to go
14284 back to building a containing block's symbol lists. */
14285 local_symbols = newobj->locals;
14286 local_using_directives = newobj->local_using_directives;
14287
14288 /* If we've finished processing a top-level function, subsequent
14289 symbols go in the file symbol list. */
14290 if (outermost_context_p ())
14291 cu->list_in_scope = &file_symbols;
14292 }
14293
14294 /* Process all the DIES contained within a lexical block scope. Start
14295 a new scope, process the dies, and then close the scope. */
14296
14297 static void
14298 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
14299 {
14300 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14301 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14302 struct context_stack *newobj;
14303 CORE_ADDR lowpc, highpc;
14304 struct die_info *child_die;
14305 CORE_ADDR baseaddr;
14306
14307 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14308
14309 /* Ignore blocks with missing or invalid low and high pc attributes. */
14310 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
14311 as multiple lexical blocks? Handling children in a sane way would
14312 be nasty. Might be easier to properly extend generic blocks to
14313 describe ranges. */
14314 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
14315 {
14316 case PC_BOUNDS_NOT_PRESENT:
14317 /* DW_TAG_lexical_block has no attributes, process its children as if
14318 there was no wrapping by that DW_TAG_lexical_block.
14319 GCC does no longer produces such DWARF since GCC r224161. */
14320 for (child_die = die->child;
14321 child_die != NULL && child_die->tag;
14322 child_die = sibling_die (child_die))
14323 process_die (child_die, cu);
14324 return;
14325 case PC_BOUNDS_INVALID:
14326 return;
14327 }
14328 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14329 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
14330
14331 push_context (0, lowpc);
14332 if (die->child != NULL)
14333 {
14334 child_die = die->child;
14335 while (child_die && child_die->tag)
14336 {
14337 process_die (child_die, cu);
14338 child_die = sibling_die (child_die);
14339 }
14340 }
14341 inherit_abstract_dies (die, cu);
14342 newobj = pop_context ();
14343
14344 if (local_symbols != NULL || local_using_directives != NULL)
14345 {
14346 struct block *block
14347 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
14348 newobj->start_addr, highpc);
14349
14350 /* Note that recording ranges after traversing children, as we
14351 do here, means that recording a parent's ranges entails
14352 walking across all its children's ranges as they appear in
14353 the address map, which is quadratic behavior.
14354
14355 It would be nicer to record the parent's ranges before
14356 traversing its children, simply overriding whatever you find
14357 there. But since we don't even decide whether to create a
14358 block until after we've traversed its children, that's hard
14359 to do. */
14360 dwarf2_record_block_ranges (die, block, baseaddr, cu);
14361 }
14362 local_symbols = newobj->locals;
14363 local_using_directives = newobj->local_using_directives;
14364 }
14365
14366 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
14367
14368 static void
14369 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
14370 {
14371 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14372 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14373 CORE_ADDR pc, baseaddr;
14374 struct attribute *attr;
14375 struct call_site *call_site, call_site_local;
14376 void **slot;
14377 int nparams;
14378 struct die_info *child_die;
14379
14380 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14381
14382 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
14383 if (attr == NULL)
14384 {
14385 /* This was a pre-DWARF-5 GNU extension alias
14386 for DW_AT_call_return_pc. */
14387 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14388 }
14389 if (!attr)
14390 {
14391 complaint (&symfile_complaints,
14392 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
14393 "DIE %s [in module %s]"),
14394 sect_offset_str (die->sect_off), objfile_name (objfile));
14395 return;
14396 }
14397 pc = attr_value_as_address (attr) + baseaddr;
14398 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
14399
14400 if (cu->call_site_htab == NULL)
14401 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14402 NULL, &objfile->objfile_obstack,
14403 hashtab_obstack_allocate, NULL);
14404 call_site_local.pc = pc;
14405 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14406 if (*slot != NULL)
14407 {
14408 complaint (&symfile_complaints,
14409 _("Duplicate PC %s for DW_TAG_call_site "
14410 "DIE %s [in module %s]"),
14411 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
14412 objfile_name (objfile));
14413 return;
14414 }
14415
14416 /* Count parameters at the caller. */
14417
14418 nparams = 0;
14419 for (child_die = die->child; child_die && child_die->tag;
14420 child_die = sibling_die (child_die))
14421 {
14422 if (child_die->tag != DW_TAG_call_site_parameter
14423 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14424 {
14425 complaint (&symfile_complaints,
14426 _("Tag %d is not DW_TAG_call_site_parameter in "
14427 "DW_TAG_call_site child DIE %s [in module %s]"),
14428 child_die->tag, sect_offset_str (child_die->sect_off),
14429 objfile_name (objfile));
14430 continue;
14431 }
14432
14433 nparams++;
14434 }
14435
14436 call_site
14437 = ((struct call_site *)
14438 obstack_alloc (&objfile->objfile_obstack,
14439 sizeof (*call_site)
14440 + (sizeof (*call_site->parameter) * (nparams - 1))));
14441 *slot = call_site;
14442 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14443 call_site->pc = pc;
14444
14445 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14446 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14447 {
14448 struct die_info *func_die;
14449
14450 /* Skip also over DW_TAG_inlined_subroutine. */
14451 for (func_die = die->parent;
14452 func_die && func_die->tag != DW_TAG_subprogram
14453 && func_die->tag != DW_TAG_subroutine_type;
14454 func_die = func_die->parent);
14455
14456 /* DW_AT_call_all_calls is a superset
14457 of DW_AT_call_all_tail_calls. */
14458 if (func_die
14459 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14460 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14461 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14462 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14463 {
14464 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14465 not complete. But keep CALL_SITE for look ups via call_site_htab,
14466 both the initial caller containing the real return address PC and
14467 the final callee containing the current PC of a chain of tail
14468 calls do not need to have the tail call list complete. But any
14469 function candidate for a virtual tail call frame searched via
14470 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14471 determined unambiguously. */
14472 }
14473 else
14474 {
14475 struct type *func_type = NULL;
14476
14477 if (func_die)
14478 func_type = get_die_type (func_die, cu);
14479 if (func_type != NULL)
14480 {
14481 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14482
14483 /* Enlist this call site to the function. */
14484 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14485 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14486 }
14487 else
14488 complaint (&symfile_complaints,
14489 _("Cannot find function owning DW_TAG_call_site "
14490 "DIE %s [in module %s]"),
14491 sect_offset_str (die->sect_off), objfile_name (objfile));
14492 }
14493 }
14494
14495 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14496 if (attr == NULL)
14497 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14498 if (attr == NULL)
14499 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14500 if (attr == NULL)
14501 {
14502 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14503 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14504 }
14505 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14506 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14507 /* Keep NULL DWARF_BLOCK. */;
14508 else if (attr_form_is_block (attr))
14509 {
14510 struct dwarf2_locexpr_baton *dlbaton;
14511
14512 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14513 dlbaton->data = DW_BLOCK (attr)->data;
14514 dlbaton->size = DW_BLOCK (attr)->size;
14515 dlbaton->per_cu = cu->per_cu;
14516
14517 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14518 }
14519 else if (attr_form_is_ref (attr))
14520 {
14521 struct dwarf2_cu *target_cu = cu;
14522 struct die_info *target_die;
14523
14524 target_die = follow_die_ref (die, attr, &target_cu);
14525 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14526 if (die_is_declaration (target_die, target_cu))
14527 {
14528 const char *target_physname;
14529
14530 /* Prefer the mangled name; otherwise compute the demangled one. */
14531 target_physname = dw2_linkage_name (target_die, target_cu);
14532 if (target_physname == NULL)
14533 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14534 if (target_physname == NULL)
14535 complaint (&symfile_complaints,
14536 _("DW_AT_call_target target DIE has invalid "
14537 "physname, for referencing DIE %s [in module %s]"),
14538 sect_offset_str (die->sect_off), objfile_name (objfile));
14539 else
14540 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14541 }
14542 else
14543 {
14544 CORE_ADDR lowpc;
14545
14546 /* DW_AT_entry_pc should be preferred. */
14547 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14548 <= PC_BOUNDS_INVALID)
14549 complaint (&symfile_complaints,
14550 _("DW_AT_call_target target DIE has invalid "
14551 "low pc, for referencing DIE %s [in module %s]"),
14552 sect_offset_str (die->sect_off), objfile_name (objfile));
14553 else
14554 {
14555 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14556 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14557 }
14558 }
14559 }
14560 else
14561 complaint (&symfile_complaints,
14562 _("DW_TAG_call_site DW_AT_call_target is neither "
14563 "block nor reference, for DIE %s [in module %s]"),
14564 sect_offset_str (die->sect_off), objfile_name (objfile));
14565
14566 call_site->per_cu = cu->per_cu;
14567
14568 for (child_die = die->child;
14569 child_die && child_die->tag;
14570 child_die = sibling_die (child_die))
14571 {
14572 struct call_site_parameter *parameter;
14573 struct attribute *loc, *origin;
14574
14575 if (child_die->tag != DW_TAG_call_site_parameter
14576 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14577 {
14578 /* Already printed the complaint above. */
14579 continue;
14580 }
14581
14582 gdb_assert (call_site->parameter_count < nparams);
14583 parameter = &call_site->parameter[call_site->parameter_count];
14584
14585 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14586 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14587 register is contained in DW_AT_call_value. */
14588
14589 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14590 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14591 if (origin == NULL)
14592 {
14593 /* This was a pre-DWARF-5 GNU extension alias
14594 for DW_AT_call_parameter. */
14595 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14596 }
14597 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14598 {
14599 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14600
14601 sect_offset sect_off
14602 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14603 if (!offset_in_cu_p (&cu->header, sect_off))
14604 {
14605 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14606 binding can be done only inside one CU. Such referenced DIE
14607 therefore cannot be even moved to DW_TAG_partial_unit. */
14608 complaint (&symfile_complaints,
14609 _("DW_AT_call_parameter offset is not in CU for "
14610 "DW_TAG_call_site child DIE %s [in module %s]"),
14611 sect_offset_str (child_die->sect_off),
14612 objfile_name (objfile));
14613 continue;
14614 }
14615 parameter->u.param_cu_off
14616 = (cu_offset) (sect_off - cu->header.sect_off);
14617 }
14618 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14619 {
14620 complaint (&symfile_complaints,
14621 _("No DW_FORM_block* DW_AT_location for "
14622 "DW_TAG_call_site child DIE %s [in module %s]"),
14623 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14624 continue;
14625 }
14626 else
14627 {
14628 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14629 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14630 if (parameter->u.dwarf_reg != -1)
14631 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14632 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14633 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14634 &parameter->u.fb_offset))
14635 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14636 else
14637 {
14638 complaint (&symfile_complaints,
14639 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14640 "for DW_FORM_block* DW_AT_location is supported for "
14641 "DW_TAG_call_site child DIE %s "
14642 "[in module %s]"),
14643 sect_offset_str (child_die->sect_off),
14644 objfile_name (objfile));
14645 continue;
14646 }
14647 }
14648
14649 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14650 if (attr == NULL)
14651 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14652 if (!attr_form_is_block (attr))
14653 {
14654 complaint (&symfile_complaints,
14655 _("No DW_FORM_block* DW_AT_call_value for "
14656 "DW_TAG_call_site child DIE %s [in module %s]"),
14657 sect_offset_str (child_die->sect_off),
14658 objfile_name (objfile));
14659 continue;
14660 }
14661 parameter->value = DW_BLOCK (attr)->data;
14662 parameter->value_size = DW_BLOCK (attr)->size;
14663
14664 /* Parameters are not pre-cleared by memset above. */
14665 parameter->data_value = NULL;
14666 parameter->data_value_size = 0;
14667 call_site->parameter_count++;
14668
14669 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14670 if (attr == NULL)
14671 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14672 if (attr)
14673 {
14674 if (!attr_form_is_block (attr))
14675 complaint (&symfile_complaints,
14676 _("No DW_FORM_block* DW_AT_call_data_value for "
14677 "DW_TAG_call_site child DIE %s [in module %s]"),
14678 sect_offset_str (child_die->sect_off),
14679 objfile_name (objfile));
14680 else
14681 {
14682 parameter->data_value = DW_BLOCK (attr)->data;
14683 parameter->data_value_size = DW_BLOCK (attr)->size;
14684 }
14685 }
14686 }
14687 }
14688
14689 /* Helper function for read_variable. If DIE represents a virtual
14690 table, then return the type of the concrete object that is
14691 associated with the virtual table. Otherwise, return NULL. */
14692
14693 static struct type *
14694 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14695 {
14696 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14697 if (attr == NULL)
14698 return NULL;
14699
14700 /* Find the type DIE. */
14701 struct die_info *type_die = NULL;
14702 struct dwarf2_cu *type_cu = cu;
14703
14704 if (attr_form_is_ref (attr))
14705 type_die = follow_die_ref (die, attr, &type_cu);
14706 if (type_die == NULL)
14707 return NULL;
14708
14709 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14710 return NULL;
14711 return die_containing_type (type_die, type_cu);
14712 }
14713
14714 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14715
14716 static void
14717 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14718 {
14719 struct rust_vtable_symbol *storage = NULL;
14720
14721 if (cu->language == language_rust)
14722 {
14723 struct type *containing_type = rust_containing_type (die, cu);
14724
14725 if (containing_type != NULL)
14726 {
14727 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14728
14729 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14730 struct rust_vtable_symbol);
14731 initialize_objfile_symbol (storage);
14732 storage->concrete_type = containing_type;
14733 storage->subclass = SYMBOL_RUST_VTABLE;
14734 }
14735 }
14736
14737 new_symbol (die, NULL, cu, storage);
14738 }
14739
14740 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14741 reading .debug_rnglists.
14742 Callback's type should be:
14743 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14744 Return true if the attributes are present and valid, otherwise,
14745 return false. */
14746
14747 template <typename Callback>
14748 static bool
14749 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14750 Callback &&callback)
14751 {
14752 struct dwarf2_per_objfile *dwarf2_per_objfile
14753 = cu->per_cu->dwarf2_per_objfile;
14754 struct objfile *objfile = dwarf2_per_objfile->objfile;
14755 bfd *obfd = objfile->obfd;
14756 /* Base address selection entry. */
14757 CORE_ADDR base;
14758 int found_base;
14759 const gdb_byte *buffer;
14760 CORE_ADDR baseaddr;
14761 bool overflow = false;
14762
14763 found_base = cu->base_known;
14764 base = cu->base_address;
14765
14766 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14767 if (offset >= dwarf2_per_objfile->rnglists.size)
14768 {
14769 complaint (&symfile_complaints,
14770 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14771 offset);
14772 return false;
14773 }
14774 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14775
14776 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14777
14778 while (1)
14779 {
14780 /* Initialize it due to a false compiler warning. */
14781 CORE_ADDR range_beginning = 0, range_end = 0;
14782 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14783 + dwarf2_per_objfile->rnglists.size);
14784 unsigned int bytes_read;
14785
14786 if (buffer == buf_end)
14787 {
14788 overflow = true;
14789 break;
14790 }
14791 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14792 switch (rlet)
14793 {
14794 case DW_RLE_end_of_list:
14795 break;
14796 case DW_RLE_base_address:
14797 if (buffer + cu->header.addr_size > buf_end)
14798 {
14799 overflow = true;
14800 break;
14801 }
14802 base = read_address (obfd, buffer, cu, &bytes_read);
14803 found_base = 1;
14804 buffer += bytes_read;
14805 break;
14806 case DW_RLE_start_length:
14807 if (buffer + cu->header.addr_size > buf_end)
14808 {
14809 overflow = true;
14810 break;
14811 }
14812 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14813 buffer += bytes_read;
14814 range_end = (range_beginning
14815 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14816 buffer += bytes_read;
14817 if (buffer > buf_end)
14818 {
14819 overflow = true;
14820 break;
14821 }
14822 break;
14823 case DW_RLE_offset_pair:
14824 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14825 buffer += bytes_read;
14826 if (buffer > buf_end)
14827 {
14828 overflow = true;
14829 break;
14830 }
14831 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14832 buffer += bytes_read;
14833 if (buffer > buf_end)
14834 {
14835 overflow = true;
14836 break;
14837 }
14838 break;
14839 case DW_RLE_start_end:
14840 if (buffer + 2 * cu->header.addr_size > buf_end)
14841 {
14842 overflow = true;
14843 break;
14844 }
14845 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14846 buffer += bytes_read;
14847 range_end = read_address (obfd, buffer, cu, &bytes_read);
14848 buffer += bytes_read;
14849 break;
14850 default:
14851 complaint (&symfile_complaints,
14852 _("Invalid .debug_rnglists data (no base address)"));
14853 return false;
14854 }
14855 if (rlet == DW_RLE_end_of_list || overflow)
14856 break;
14857 if (rlet == DW_RLE_base_address)
14858 continue;
14859
14860 if (!found_base)
14861 {
14862 /* We have no valid base address for the ranges
14863 data. */
14864 complaint (&symfile_complaints,
14865 _("Invalid .debug_rnglists data (no base address)"));
14866 return false;
14867 }
14868
14869 if (range_beginning > range_end)
14870 {
14871 /* Inverted range entries are invalid. */
14872 complaint (&symfile_complaints,
14873 _("Invalid .debug_rnglists data (inverted range)"));
14874 return false;
14875 }
14876
14877 /* Empty range entries have no effect. */
14878 if (range_beginning == range_end)
14879 continue;
14880
14881 range_beginning += base;
14882 range_end += base;
14883
14884 /* A not-uncommon case of bad debug info.
14885 Don't pollute the addrmap with bad data. */
14886 if (range_beginning + baseaddr == 0
14887 && !dwarf2_per_objfile->has_section_at_zero)
14888 {
14889 complaint (&symfile_complaints,
14890 _(".debug_rnglists entry has start address of zero"
14891 " [in module %s]"), objfile_name (objfile));
14892 continue;
14893 }
14894
14895 callback (range_beginning, range_end);
14896 }
14897
14898 if (overflow)
14899 {
14900 complaint (&symfile_complaints,
14901 _("Offset %d is not terminated "
14902 "for DW_AT_ranges attribute"),
14903 offset);
14904 return false;
14905 }
14906
14907 return true;
14908 }
14909
14910 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14911 Callback's type should be:
14912 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14913 Return 1 if the attributes are present and valid, otherwise, return 0. */
14914
14915 template <typename Callback>
14916 static int
14917 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14918 Callback &&callback)
14919 {
14920 struct dwarf2_per_objfile *dwarf2_per_objfile
14921 = cu->per_cu->dwarf2_per_objfile;
14922 struct objfile *objfile = dwarf2_per_objfile->objfile;
14923 struct comp_unit_head *cu_header = &cu->header;
14924 bfd *obfd = objfile->obfd;
14925 unsigned int addr_size = cu_header->addr_size;
14926 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14927 /* Base address selection entry. */
14928 CORE_ADDR base;
14929 int found_base;
14930 unsigned int dummy;
14931 const gdb_byte *buffer;
14932 CORE_ADDR baseaddr;
14933
14934 if (cu_header->version >= 5)
14935 return dwarf2_rnglists_process (offset, cu, callback);
14936
14937 found_base = cu->base_known;
14938 base = cu->base_address;
14939
14940 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14941 if (offset >= dwarf2_per_objfile->ranges.size)
14942 {
14943 complaint (&symfile_complaints,
14944 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14945 offset);
14946 return 0;
14947 }
14948 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14949
14950 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14951
14952 while (1)
14953 {
14954 CORE_ADDR range_beginning, range_end;
14955
14956 range_beginning = read_address (obfd, buffer, cu, &dummy);
14957 buffer += addr_size;
14958 range_end = read_address (obfd, buffer, cu, &dummy);
14959 buffer += addr_size;
14960 offset += 2 * addr_size;
14961
14962 /* An end of list marker is a pair of zero addresses. */
14963 if (range_beginning == 0 && range_end == 0)
14964 /* Found the end of list entry. */
14965 break;
14966
14967 /* Each base address selection entry is a pair of 2 values.
14968 The first is the largest possible address, the second is
14969 the base address. Check for a base address here. */
14970 if ((range_beginning & mask) == mask)
14971 {
14972 /* If we found the largest possible address, then we already
14973 have the base address in range_end. */
14974 base = range_end;
14975 found_base = 1;
14976 continue;
14977 }
14978
14979 if (!found_base)
14980 {
14981 /* We have no valid base address for the ranges
14982 data. */
14983 complaint (&symfile_complaints,
14984 _("Invalid .debug_ranges data (no base address)"));
14985 return 0;
14986 }
14987
14988 if (range_beginning > range_end)
14989 {
14990 /* Inverted range entries are invalid. */
14991 complaint (&symfile_complaints,
14992 _("Invalid .debug_ranges data (inverted range)"));
14993 return 0;
14994 }
14995
14996 /* Empty range entries have no effect. */
14997 if (range_beginning == range_end)
14998 continue;
14999
15000 range_beginning += base;
15001 range_end += base;
15002
15003 /* A not-uncommon case of bad debug info.
15004 Don't pollute the addrmap with bad data. */
15005 if (range_beginning + baseaddr == 0
15006 && !dwarf2_per_objfile->has_section_at_zero)
15007 {
15008 complaint (&symfile_complaints,
15009 _(".debug_ranges entry has start address of zero"
15010 " [in module %s]"), objfile_name (objfile));
15011 continue;
15012 }
15013
15014 callback (range_beginning, range_end);
15015 }
15016
15017 return 1;
15018 }
15019
15020 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
15021 Return 1 if the attributes are present and valid, otherwise, return 0.
15022 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
15023
15024 static int
15025 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
15026 CORE_ADDR *high_return, struct dwarf2_cu *cu,
15027 struct partial_symtab *ranges_pst)
15028 {
15029 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15030 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15031 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
15032 SECT_OFF_TEXT (objfile));
15033 int low_set = 0;
15034 CORE_ADDR low = 0;
15035 CORE_ADDR high = 0;
15036 int retval;
15037
15038 retval = dwarf2_ranges_process (offset, cu,
15039 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
15040 {
15041 if (ranges_pst != NULL)
15042 {
15043 CORE_ADDR lowpc;
15044 CORE_ADDR highpc;
15045
15046 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
15047 range_beginning + baseaddr);
15048 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
15049 range_end + baseaddr);
15050 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
15051 ranges_pst);
15052 }
15053
15054 /* FIXME: This is recording everything as a low-high
15055 segment of consecutive addresses. We should have a
15056 data structure for discontiguous block ranges
15057 instead. */
15058 if (! low_set)
15059 {
15060 low = range_beginning;
15061 high = range_end;
15062 low_set = 1;
15063 }
15064 else
15065 {
15066 if (range_beginning < low)
15067 low = range_beginning;
15068 if (range_end > high)
15069 high = range_end;
15070 }
15071 });
15072 if (!retval)
15073 return 0;
15074
15075 if (! low_set)
15076 /* If the first entry is an end-of-list marker, the range
15077 describes an empty scope, i.e. no instructions. */
15078 return 0;
15079
15080 if (low_return)
15081 *low_return = low;
15082 if (high_return)
15083 *high_return = high;
15084 return 1;
15085 }
15086
15087 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
15088 definition for the return value. *LOWPC and *HIGHPC are set iff
15089 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
15090
15091 static enum pc_bounds_kind
15092 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
15093 CORE_ADDR *highpc, struct dwarf2_cu *cu,
15094 struct partial_symtab *pst)
15095 {
15096 struct dwarf2_per_objfile *dwarf2_per_objfile
15097 = cu->per_cu->dwarf2_per_objfile;
15098 struct attribute *attr;
15099 struct attribute *attr_high;
15100 CORE_ADDR low = 0;
15101 CORE_ADDR high = 0;
15102 enum pc_bounds_kind ret;
15103
15104 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
15105 if (attr_high)
15106 {
15107 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15108 if (attr)
15109 {
15110 low = attr_value_as_address (attr);
15111 high = attr_value_as_address (attr_high);
15112 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
15113 high += low;
15114 }
15115 else
15116 /* Found high w/o low attribute. */
15117 return PC_BOUNDS_INVALID;
15118
15119 /* Found consecutive range of addresses. */
15120 ret = PC_BOUNDS_HIGH_LOW;
15121 }
15122 else
15123 {
15124 attr = dwarf2_attr (die, DW_AT_ranges, cu);
15125 if (attr != NULL)
15126 {
15127 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
15128 We take advantage of the fact that DW_AT_ranges does not appear
15129 in DW_TAG_compile_unit of DWO files. */
15130 int need_ranges_base = die->tag != DW_TAG_compile_unit;
15131 unsigned int ranges_offset = (DW_UNSND (attr)
15132 + (need_ranges_base
15133 ? cu->ranges_base
15134 : 0));
15135
15136 /* Value of the DW_AT_ranges attribute is the offset in the
15137 .debug_ranges section. */
15138 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
15139 return PC_BOUNDS_INVALID;
15140 /* Found discontinuous range of addresses. */
15141 ret = PC_BOUNDS_RANGES;
15142 }
15143 else
15144 return PC_BOUNDS_NOT_PRESENT;
15145 }
15146
15147 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
15148 if (high <= low)
15149 return PC_BOUNDS_INVALID;
15150
15151 /* When using the GNU linker, .gnu.linkonce. sections are used to
15152 eliminate duplicate copies of functions and vtables and such.
15153 The linker will arbitrarily choose one and discard the others.
15154 The AT_*_pc values for such functions refer to local labels in
15155 these sections. If the section from that file was discarded, the
15156 labels are not in the output, so the relocs get a value of 0.
15157 If this is a discarded function, mark the pc bounds as invalid,
15158 so that GDB will ignore it. */
15159 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
15160 return PC_BOUNDS_INVALID;
15161
15162 *lowpc = low;
15163 if (highpc)
15164 *highpc = high;
15165 return ret;
15166 }
15167
15168 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
15169 its low and high PC addresses. Do nothing if these addresses could not
15170 be determined. Otherwise, set LOWPC to the low address if it is smaller,
15171 and HIGHPC to the high address if greater than HIGHPC. */
15172
15173 static void
15174 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
15175 CORE_ADDR *lowpc, CORE_ADDR *highpc,
15176 struct dwarf2_cu *cu)
15177 {
15178 CORE_ADDR low, high;
15179 struct die_info *child = die->child;
15180
15181 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
15182 {
15183 *lowpc = std::min (*lowpc, low);
15184 *highpc = std::max (*highpc, high);
15185 }
15186
15187 /* If the language does not allow nested subprograms (either inside
15188 subprograms or lexical blocks), we're done. */
15189 if (cu->language != language_ada)
15190 return;
15191
15192 /* Check all the children of the given DIE. If it contains nested
15193 subprograms, then check their pc bounds. Likewise, we need to
15194 check lexical blocks as well, as they may also contain subprogram
15195 definitions. */
15196 while (child && child->tag)
15197 {
15198 if (child->tag == DW_TAG_subprogram
15199 || child->tag == DW_TAG_lexical_block)
15200 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
15201 child = sibling_die (child);
15202 }
15203 }
15204
15205 /* Get the low and high pc's represented by the scope DIE, and store
15206 them in *LOWPC and *HIGHPC. If the correct values can't be
15207 determined, set *LOWPC to -1 and *HIGHPC to 0. */
15208
15209 static void
15210 get_scope_pc_bounds (struct die_info *die,
15211 CORE_ADDR *lowpc, CORE_ADDR *highpc,
15212 struct dwarf2_cu *cu)
15213 {
15214 CORE_ADDR best_low = (CORE_ADDR) -1;
15215 CORE_ADDR best_high = (CORE_ADDR) 0;
15216 CORE_ADDR current_low, current_high;
15217
15218 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
15219 >= PC_BOUNDS_RANGES)
15220 {
15221 best_low = current_low;
15222 best_high = current_high;
15223 }
15224 else
15225 {
15226 struct die_info *child = die->child;
15227
15228 while (child && child->tag)
15229 {
15230 switch (child->tag) {
15231 case DW_TAG_subprogram:
15232 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
15233 break;
15234 case DW_TAG_namespace:
15235 case DW_TAG_module:
15236 /* FIXME: carlton/2004-01-16: Should we do this for
15237 DW_TAG_class_type/DW_TAG_structure_type, too? I think
15238 that current GCC's always emit the DIEs corresponding
15239 to definitions of methods of classes as children of a
15240 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
15241 the DIEs giving the declarations, which could be
15242 anywhere). But I don't see any reason why the
15243 standards says that they have to be there. */
15244 get_scope_pc_bounds (child, &current_low, &current_high, cu);
15245
15246 if (current_low != ((CORE_ADDR) -1))
15247 {
15248 best_low = std::min (best_low, current_low);
15249 best_high = std::max (best_high, current_high);
15250 }
15251 break;
15252 default:
15253 /* Ignore. */
15254 break;
15255 }
15256
15257 child = sibling_die (child);
15258 }
15259 }
15260
15261 *lowpc = best_low;
15262 *highpc = best_high;
15263 }
15264
15265 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
15266 in DIE. */
15267
15268 static void
15269 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
15270 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
15271 {
15272 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15273 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15274 struct attribute *attr;
15275 struct attribute *attr_high;
15276
15277 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
15278 if (attr_high)
15279 {
15280 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15281 if (attr)
15282 {
15283 CORE_ADDR low = attr_value_as_address (attr);
15284 CORE_ADDR high = attr_value_as_address (attr_high);
15285
15286 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
15287 high += low;
15288
15289 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
15290 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
15291 record_block_range (block, low, high - 1);
15292 }
15293 }
15294
15295 attr = dwarf2_attr (die, DW_AT_ranges, cu);
15296 if (attr)
15297 {
15298 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
15299 We take advantage of the fact that DW_AT_ranges does not appear
15300 in DW_TAG_compile_unit of DWO files. */
15301 int need_ranges_base = die->tag != DW_TAG_compile_unit;
15302
15303 /* The value of the DW_AT_ranges attribute is the offset of the
15304 address range list in the .debug_ranges section. */
15305 unsigned long offset = (DW_UNSND (attr)
15306 + (need_ranges_base ? cu->ranges_base : 0));
15307 const gdb_byte *buffer;
15308
15309 /* For some target architectures, but not others, the
15310 read_address function sign-extends the addresses it returns.
15311 To recognize base address selection entries, we need a
15312 mask. */
15313 unsigned int addr_size = cu->header.addr_size;
15314 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
15315
15316 /* The base address, to which the next pair is relative. Note
15317 that this 'base' is a DWARF concept: most entries in a range
15318 list are relative, to reduce the number of relocs against the
15319 debugging information. This is separate from this function's
15320 'baseaddr' argument, which GDB uses to relocate debugging
15321 information from a shared library based on the address at
15322 which the library was loaded. */
15323 CORE_ADDR base = cu->base_address;
15324 int base_known = cu->base_known;
15325
15326 dwarf2_ranges_process (offset, cu,
15327 [&] (CORE_ADDR start, CORE_ADDR end)
15328 {
15329 start += baseaddr;
15330 end += baseaddr;
15331 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
15332 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
15333 record_block_range (block, start, end - 1);
15334 });
15335 }
15336 }
15337
15338 /* Check whether the producer field indicates either of GCC < 4.6, or the
15339 Intel C/C++ compiler, and cache the result in CU. */
15340
15341 static void
15342 check_producer (struct dwarf2_cu *cu)
15343 {
15344 int major, minor;
15345
15346 if (cu->producer == NULL)
15347 {
15348 /* For unknown compilers expect their behavior is DWARF version
15349 compliant.
15350
15351 GCC started to support .debug_types sections by -gdwarf-4 since
15352 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
15353 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
15354 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
15355 interpreted incorrectly by GDB now - GCC PR debug/48229. */
15356 }
15357 else if (producer_is_gcc (cu->producer, &major, &minor))
15358 {
15359 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
15360 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
15361 }
15362 else if (producer_is_icc (cu->producer, &major, &minor))
15363 cu->producer_is_icc_lt_14 = major < 14;
15364 else
15365 {
15366 /* For other non-GCC compilers, expect their behavior is DWARF version
15367 compliant. */
15368 }
15369
15370 cu->checked_producer = 1;
15371 }
15372
15373 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
15374 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
15375 during 4.6.0 experimental. */
15376
15377 static int
15378 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
15379 {
15380 if (!cu->checked_producer)
15381 check_producer (cu);
15382
15383 return cu->producer_is_gxx_lt_4_6;
15384 }
15385
15386 /* Return the default accessibility type if it is not overriden by
15387 DW_AT_accessibility. */
15388
15389 static enum dwarf_access_attribute
15390 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15391 {
15392 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15393 {
15394 /* The default DWARF 2 accessibility for members is public, the default
15395 accessibility for inheritance is private. */
15396
15397 if (die->tag != DW_TAG_inheritance)
15398 return DW_ACCESS_public;
15399 else
15400 return DW_ACCESS_private;
15401 }
15402 else
15403 {
15404 /* DWARF 3+ defines the default accessibility a different way. The same
15405 rules apply now for DW_TAG_inheritance as for the members and it only
15406 depends on the container kind. */
15407
15408 if (die->parent->tag == DW_TAG_class_type)
15409 return DW_ACCESS_private;
15410 else
15411 return DW_ACCESS_public;
15412 }
15413 }
15414
15415 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15416 offset. If the attribute was not found return 0, otherwise return
15417 1. If it was found but could not properly be handled, set *OFFSET
15418 to 0. */
15419
15420 static int
15421 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15422 LONGEST *offset)
15423 {
15424 struct attribute *attr;
15425
15426 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15427 if (attr != NULL)
15428 {
15429 *offset = 0;
15430
15431 /* Note that we do not check for a section offset first here.
15432 This is because DW_AT_data_member_location is new in DWARF 4,
15433 so if we see it, we can assume that a constant form is really
15434 a constant and not a section offset. */
15435 if (attr_form_is_constant (attr))
15436 *offset = dwarf2_get_attr_constant_value (attr, 0);
15437 else if (attr_form_is_section_offset (attr))
15438 dwarf2_complex_location_expr_complaint ();
15439 else if (attr_form_is_block (attr))
15440 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15441 else
15442 dwarf2_complex_location_expr_complaint ();
15443
15444 return 1;
15445 }
15446
15447 return 0;
15448 }
15449
15450 /* Add an aggregate field to the field list. */
15451
15452 static void
15453 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15454 struct dwarf2_cu *cu)
15455 {
15456 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15457 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15458 struct nextfield *new_field;
15459 struct attribute *attr;
15460 struct field *fp;
15461 const char *fieldname = "";
15462
15463 if (die->tag == DW_TAG_inheritance)
15464 {
15465 fip->baseclasses.emplace_back ();
15466 new_field = &fip->baseclasses.back ();
15467 }
15468 else
15469 {
15470 fip->fields.emplace_back ();
15471 new_field = &fip->fields.back ();
15472 }
15473
15474 fip->nfields++;
15475
15476 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15477 if (attr)
15478 new_field->accessibility = DW_UNSND (attr);
15479 else
15480 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15481 if (new_field->accessibility != DW_ACCESS_public)
15482 fip->non_public_fields = 1;
15483
15484 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15485 if (attr)
15486 new_field->virtuality = DW_UNSND (attr);
15487 else
15488 new_field->virtuality = DW_VIRTUALITY_none;
15489
15490 fp = &new_field->field;
15491
15492 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15493 {
15494 LONGEST offset;
15495
15496 /* Data member other than a C++ static data member. */
15497
15498 /* Get type of field. */
15499 fp->type = die_type (die, cu);
15500
15501 SET_FIELD_BITPOS (*fp, 0);
15502
15503 /* Get bit size of field (zero if none). */
15504 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15505 if (attr)
15506 {
15507 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15508 }
15509 else
15510 {
15511 FIELD_BITSIZE (*fp) = 0;
15512 }
15513
15514 /* Get bit offset of field. */
15515 if (handle_data_member_location (die, cu, &offset))
15516 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15517 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15518 if (attr)
15519 {
15520 if (gdbarch_bits_big_endian (gdbarch))
15521 {
15522 /* For big endian bits, the DW_AT_bit_offset gives the
15523 additional bit offset from the MSB of the containing
15524 anonymous object to the MSB of the field. We don't
15525 have to do anything special since we don't need to
15526 know the size of the anonymous object. */
15527 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15528 }
15529 else
15530 {
15531 /* For little endian bits, compute the bit offset to the
15532 MSB of the anonymous object, subtract off the number of
15533 bits from the MSB of the field to the MSB of the
15534 object, and then subtract off the number of bits of
15535 the field itself. The result is the bit offset of
15536 the LSB of the field. */
15537 int anonymous_size;
15538 int bit_offset = DW_UNSND (attr);
15539
15540 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15541 if (attr)
15542 {
15543 /* The size of the anonymous object containing
15544 the bit field is explicit, so use the
15545 indicated size (in bytes). */
15546 anonymous_size = DW_UNSND (attr);
15547 }
15548 else
15549 {
15550 /* The size of the anonymous object containing
15551 the bit field must be inferred from the type
15552 attribute of the data member containing the
15553 bit field. */
15554 anonymous_size = TYPE_LENGTH (fp->type);
15555 }
15556 SET_FIELD_BITPOS (*fp,
15557 (FIELD_BITPOS (*fp)
15558 + anonymous_size * bits_per_byte
15559 - bit_offset - FIELD_BITSIZE (*fp)));
15560 }
15561 }
15562 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15563 if (attr != NULL)
15564 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15565 + dwarf2_get_attr_constant_value (attr, 0)));
15566
15567 /* Get name of field. */
15568 fieldname = dwarf2_name (die, cu);
15569 if (fieldname == NULL)
15570 fieldname = "";
15571
15572 /* The name is already allocated along with this objfile, so we don't
15573 need to duplicate it for the type. */
15574 fp->name = fieldname;
15575
15576 /* Change accessibility for artificial fields (e.g. virtual table
15577 pointer or virtual base class pointer) to private. */
15578 if (dwarf2_attr (die, DW_AT_artificial, cu))
15579 {
15580 FIELD_ARTIFICIAL (*fp) = 1;
15581 new_field->accessibility = DW_ACCESS_private;
15582 fip->non_public_fields = 1;
15583 }
15584 }
15585 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15586 {
15587 /* C++ static member. */
15588
15589 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15590 is a declaration, but all versions of G++ as of this writing
15591 (so through at least 3.2.1) incorrectly generate
15592 DW_TAG_variable tags. */
15593
15594 const char *physname;
15595
15596 /* Get name of field. */
15597 fieldname = dwarf2_name (die, cu);
15598 if (fieldname == NULL)
15599 return;
15600
15601 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15602 if (attr
15603 /* Only create a symbol if this is an external value.
15604 new_symbol checks this and puts the value in the global symbol
15605 table, which we want. If it is not external, new_symbol
15606 will try to put the value in cu->list_in_scope which is wrong. */
15607 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15608 {
15609 /* A static const member, not much different than an enum as far as
15610 we're concerned, except that we can support more types. */
15611 new_symbol (die, NULL, cu);
15612 }
15613
15614 /* Get physical name. */
15615 physname = dwarf2_physname (fieldname, die, cu);
15616
15617 /* The name is already allocated along with this objfile, so we don't
15618 need to duplicate it for the type. */
15619 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15620 FIELD_TYPE (*fp) = die_type (die, cu);
15621 FIELD_NAME (*fp) = fieldname;
15622 }
15623 else if (die->tag == DW_TAG_inheritance)
15624 {
15625 LONGEST offset;
15626
15627 /* C++ base class field. */
15628 if (handle_data_member_location (die, cu, &offset))
15629 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15630 FIELD_BITSIZE (*fp) = 0;
15631 FIELD_TYPE (*fp) = die_type (die, cu);
15632 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15633 }
15634 else if (die->tag == DW_TAG_variant_part)
15635 {
15636 /* process_structure_scope will treat this DIE as a union. */
15637 process_structure_scope (die, cu);
15638
15639 /* The variant part is relative to the start of the enclosing
15640 structure. */
15641 SET_FIELD_BITPOS (*fp, 0);
15642 fp->type = get_die_type (die, cu);
15643 fp->artificial = 1;
15644 fp->name = "<<variant>>";
15645 }
15646 else
15647 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15648 }
15649
15650 /* Can the type given by DIE define another type? */
15651
15652 static bool
15653 type_can_define_types (const struct die_info *die)
15654 {
15655 switch (die->tag)
15656 {
15657 case DW_TAG_typedef:
15658 case DW_TAG_class_type:
15659 case DW_TAG_structure_type:
15660 case DW_TAG_union_type:
15661 case DW_TAG_enumeration_type:
15662 return true;
15663
15664 default:
15665 return false;
15666 }
15667 }
15668
15669 /* Add a type definition defined in the scope of the FIP's class. */
15670
15671 static void
15672 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15673 struct dwarf2_cu *cu)
15674 {
15675 struct decl_field fp;
15676 memset (&fp, 0, sizeof (fp));
15677
15678 gdb_assert (type_can_define_types (die));
15679
15680 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15681 fp.name = dwarf2_name (die, cu);
15682 fp.type = read_type_die (die, cu);
15683
15684 /* Save accessibility. */
15685 enum dwarf_access_attribute accessibility;
15686 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15687 if (attr != NULL)
15688 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15689 else
15690 accessibility = dwarf2_default_access_attribute (die, cu);
15691 switch (accessibility)
15692 {
15693 case DW_ACCESS_public:
15694 /* The assumed value if neither private nor protected. */
15695 break;
15696 case DW_ACCESS_private:
15697 fp.is_private = 1;
15698 break;
15699 case DW_ACCESS_protected:
15700 fp.is_protected = 1;
15701 break;
15702 default:
15703 complaint (&symfile_complaints,
15704 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15705 }
15706
15707 if (die->tag == DW_TAG_typedef)
15708 fip->typedef_field_list.push_back (fp);
15709 else
15710 fip->nested_types_list.push_back (fp);
15711 }
15712
15713 /* Create the vector of fields, and attach it to the type. */
15714
15715 static void
15716 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15717 struct dwarf2_cu *cu)
15718 {
15719 int nfields = fip->nfields;
15720
15721 /* Record the field count, allocate space for the array of fields,
15722 and create blank accessibility bitfields if necessary. */
15723 TYPE_NFIELDS (type) = nfields;
15724 TYPE_FIELDS (type) = (struct field *)
15725 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15726
15727 if (fip->non_public_fields && cu->language != language_ada)
15728 {
15729 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15730
15731 TYPE_FIELD_PRIVATE_BITS (type) =
15732 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15733 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15734
15735 TYPE_FIELD_PROTECTED_BITS (type) =
15736 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15737 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15738
15739 TYPE_FIELD_IGNORE_BITS (type) =
15740 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15741 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15742 }
15743
15744 /* If the type has baseclasses, allocate and clear a bit vector for
15745 TYPE_FIELD_VIRTUAL_BITS. */
15746 if (!fip->baseclasses.empty () && cu->language != language_ada)
15747 {
15748 int num_bytes = B_BYTES (fip->baseclasses.size ());
15749 unsigned char *pointer;
15750
15751 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15752 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15753 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15754 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15755 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15756 }
15757
15758 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15759 {
15760 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15761
15762 for (int index = 0; index < nfields; ++index)
15763 {
15764 struct nextfield &field = fip->fields[index];
15765
15766 if (field.variant.is_discriminant)
15767 di->discriminant_index = index;
15768 else if (field.variant.default_branch)
15769 di->default_index = index;
15770 else
15771 di->discriminants[index] = field.variant.discriminant_value;
15772 }
15773 }
15774
15775 /* Copy the saved-up fields into the field vector. */
15776 for (int i = 0; i < nfields; ++i)
15777 {
15778 struct nextfield &field
15779 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15780 : fip->fields[i - fip->baseclasses.size ()]);
15781
15782 TYPE_FIELD (type, i) = field.field;
15783 switch (field.accessibility)
15784 {
15785 case DW_ACCESS_private:
15786 if (cu->language != language_ada)
15787 SET_TYPE_FIELD_PRIVATE (type, i);
15788 break;
15789
15790 case DW_ACCESS_protected:
15791 if (cu->language != language_ada)
15792 SET_TYPE_FIELD_PROTECTED (type, i);
15793 break;
15794
15795 case DW_ACCESS_public:
15796 break;
15797
15798 default:
15799 /* Unknown accessibility. Complain and treat it as public. */
15800 {
15801 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15802 field.accessibility);
15803 }
15804 break;
15805 }
15806 if (i < fip->baseclasses.size ())
15807 {
15808 switch (field.virtuality)
15809 {
15810 case DW_VIRTUALITY_virtual:
15811 case DW_VIRTUALITY_pure_virtual:
15812 if (cu->language == language_ada)
15813 error (_("unexpected virtuality in component of Ada type"));
15814 SET_TYPE_FIELD_VIRTUAL (type, i);
15815 break;
15816 }
15817 }
15818 }
15819 }
15820
15821 /* Return true if this member function is a constructor, false
15822 otherwise. */
15823
15824 static int
15825 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15826 {
15827 const char *fieldname;
15828 const char *type_name;
15829 int len;
15830
15831 if (die->parent == NULL)
15832 return 0;
15833
15834 if (die->parent->tag != DW_TAG_structure_type
15835 && die->parent->tag != DW_TAG_union_type
15836 && die->parent->tag != DW_TAG_class_type)
15837 return 0;
15838
15839 fieldname = dwarf2_name (die, cu);
15840 type_name = dwarf2_name (die->parent, cu);
15841 if (fieldname == NULL || type_name == NULL)
15842 return 0;
15843
15844 len = strlen (fieldname);
15845 return (strncmp (fieldname, type_name, len) == 0
15846 && (type_name[len] == '\0' || type_name[len] == '<'));
15847 }
15848
15849 /* Add a member function to the proper fieldlist. */
15850
15851 static void
15852 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15853 struct type *type, struct dwarf2_cu *cu)
15854 {
15855 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15856 struct attribute *attr;
15857 int i;
15858 struct fnfieldlist *flp = nullptr;
15859 struct fn_field *fnp;
15860 const char *fieldname;
15861 struct type *this_type;
15862 enum dwarf_access_attribute accessibility;
15863
15864 if (cu->language == language_ada)
15865 error (_("unexpected member function in Ada type"));
15866
15867 /* Get name of member function. */
15868 fieldname = dwarf2_name (die, cu);
15869 if (fieldname == NULL)
15870 return;
15871
15872 /* Look up member function name in fieldlist. */
15873 for (i = 0; i < fip->fnfieldlists.size (); i++)
15874 {
15875 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15876 {
15877 flp = &fip->fnfieldlists[i];
15878 break;
15879 }
15880 }
15881
15882 /* Create a new fnfieldlist if necessary. */
15883 if (flp == nullptr)
15884 {
15885 fip->fnfieldlists.emplace_back ();
15886 flp = &fip->fnfieldlists.back ();
15887 flp->name = fieldname;
15888 i = fip->fnfieldlists.size () - 1;
15889 }
15890
15891 /* Create a new member function field and add it to the vector of
15892 fnfieldlists. */
15893 flp->fnfields.emplace_back ();
15894 fnp = &flp->fnfields.back ();
15895
15896 /* Delay processing of the physname until later. */
15897 if (cu->language == language_cplus)
15898 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15899 die, cu);
15900 else
15901 {
15902 const char *physname = dwarf2_physname (fieldname, die, cu);
15903 fnp->physname = physname ? physname : "";
15904 }
15905
15906 fnp->type = alloc_type (objfile);
15907 this_type = read_type_die (die, cu);
15908 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15909 {
15910 int nparams = TYPE_NFIELDS (this_type);
15911
15912 /* TYPE is the domain of this method, and THIS_TYPE is the type
15913 of the method itself (TYPE_CODE_METHOD). */
15914 smash_to_method_type (fnp->type, type,
15915 TYPE_TARGET_TYPE (this_type),
15916 TYPE_FIELDS (this_type),
15917 TYPE_NFIELDS (this_type),
15918 TYPE_VARARGS (this_type));
15919
15920 /* Handle static member functions.
15921 Dwarf2 has no clean way to discern C++ static and non-static
15922 member functions. G++ helps GDB by marking the first
15923 parameter for non-static member functions (which is the this
15924 pointer) as artificial. We obtain this information from
15925 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15926 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15927 fnp->voffset = VOFFSET_STATIC;
15928 }
15929 else
15930 complaint (&symfile_complaints, _("member function type missing for '%s'"),
15931 dwarf2_full_name (fieldname, die, cu));
15932
15933 /* Get fcontext from DW_AT_containing_type if present. */
15934 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15935 fnp->fcontext = die_containing_type (die, cu);
15936
15937 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15938 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15939
15940 /* Get accessibility. */
15941 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15942 if (attr)
15943 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15944 else
15945 accessibility = dwarf2_default_access_attribute (die, cu);
15946 switch (accessibility)
15947 {
15948 case DW_ACCESS_private:
15949 fnp->is_private = 1;
15950 break;
15951 case DW_ACCESS_protected:
15952 fnp->is_protected = 1;
15953 break;
15954 }
15955
15956 /* Check for artificial methods. */
15957 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15958 if (attr && DW_UNSND (attr) != 0)
15959 fnp->is_artificial = 1;
15960
15961 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15962
15963 /* Get index in virtual function table if it is a virtual member
15964 function. For older versions of GCC, this is an offset in the
15965 appropriate virtual table, as specified by DW_AT_containing_type.
15966 For everyone else, it is an expression to be evaluated relative
15967 to the object address. */
15968
15969 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15970 if (attr)
15971 {
15972 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15973 {
15974 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15975 {
15976 /* Old-style GCC. */
15977 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15978 }
15979 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15980 || (DW_BLOCK (attr)->size > 1
15981 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15982 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15983 {
15984 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15985 if ((fnp->voffset % cu->header.addr_size) != 0)
15986 dwarf2_complex_location_expr_complaint ();
15987 else
15988 fnp->voffset /= cu->header.addr_size;
15989 fnp->voffset += 2;
15990 }
15991 else
15992 dwarf2_complex_location_expr_complaint ();
15993
15994 if (!fnp->fcontext)
15995 {
15996 /* If there is no `this' field and no DW_AT_containing_type,
15997 we cannot actually find a base class context for the
15998 vtable! */
15999 if (TYPE_NFIELDS (this_type) == 0
16000 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
16001 {
16002 complaint (&symfile_complaints,
16003 _("cannot determine context for virtual member "
16004 "function \"%s\" (offset %s)"),
16005 fieldname, sect_offset_str (die->sect_off));
16006 }
16007 else
16008 {
16009 fnp->fcontext
16010 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
16011 }
16012 }
16013 }
16014 else if (attr_form_is_section_offset (attr))
16015 {
16016 dwarf2_complex_location_expr_complaint ();
16017 }
16018 else
16019 {
16020 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
16021 fieldname);
16022 }
16023 }
16024 else
16025 {
16026 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
16027 if (attr && DW_UNSND (attr))
16028 {
16029 /* GCC does this, as of 2008-08-25; PR debug/37237. */
16030 complaint (&symfile_complaints,
16031 _("Member function \"%s\" (offset %s) is virtual "
16032 "but the vtable offset is not specified"),
16033 fieldname, sect_offset_str (die->sect_off));
16034 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16035 TYPE_CPLUS_DYNAMIC (type) = 1;
16036 }
16037 }
16038 }
16039
16040 /* Create the vector of member function fields, and attach it to the type. */
16041
16042 static void
16043 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
16044 struct dwarf2_cu *cu)
16045 {
16046 if (cu->language == language_ada)
16047 error (_("unexpected member functions in Ada type"));
16048
16049 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16050 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
16051 TYPE_ALLOC (type,
16052 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
16053
16054 for (int i = 0; i < fip->fnfieldlists.size (); i++)
16055 {
16056 struct fnfieldlist &nf = fip->fnfieldlists[i];
16057 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
16058
16059 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
16060 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
16061 fn_flp->fn_fields = (struct fn_field *)
16062 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
16063
16064 for (int k = 0; k < nf.fnfields.size (); ++k)
16065 fn_flp->fn_fields[k] = nf.fnfields[k];
16066 }
16067
16068 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
16069 }
16070
16071 /* Returns non-zero if NAME is the name of a vtable member in CU's
16072 language, zero otherwise. */
16073 static int
16074 is_vtable_name (const char *name, struct dwarf2_cu *cu)
16075 {
16076 static const char vptr[] = "_vptr";
16077
16078 /* Look for the C++ form of the vtable. */
16079 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
16080 return 1;
16081
16082 return 0;
16083 }
16084
16085 /* GCC outputs unnamed structures that are really pointers to member
16086 functions, with the ABI-specified layout. If TYPE describes
16087 such a structure, smash it into a member function type.
16088
16089 GCC shouldn't do this; it should just output pointer to member DIEs.
16090 This is GCC PR debug/28767. */
16091
16092 static void
16093 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
16094 {
16095 struct type *pfn_type, *self_type, *new_type;
16096
16097 /* Check for a structure with no name and two children. */
16098 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
16099 return;
16100
16101 /* Check for __pfn and __delta members. */
16102 if (TYPE_FIELD_NAME (type, 0) == NULL
16103 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
16104 || TYPE_FIELD_NAME (type, 1) == NULL
16105 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
16106 return;
16107
16108 /* Find the type of the method. */
16109 pfn_type = TYPE_FIELD_TYPE (type, 0);
16110 if (pfn_type == NULL
16111 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
16112 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
16113 return;
16114
16115 /* Look for the "this" argument. */
16116 pfn_type = TYPE_TARGET_TYPE (pfn_type);
16117 if (TYPE_NFIELDS (pfn_type) == 0
16118 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
16119 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
16120 return;
16121
16122 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
16123 new_type = alloc_type (objfile);
16124 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
16125 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
16126 TYPE_VARARGS (pfn_type));
16127 smash_to_methodptr_type (type, new_type);
16128 }
16129
16130
16131 /* Called when we find the DIE that starts a structure or union scope
16132 (definition) to create a type for the structure or union. Fill in
16133 the type's name and general properties; the members will not be
16134 processed until process_structure_scope. A symbol table entry for
16135 the type will also not be done until process_structure_scope (assuming
16136 the type has a name).
16137
16138 NOTE: we need to call these functions regardless of whether or not the
16139 DIE has a DW_AT_name attribute, since it might be an anonymous
16140 structure or union. This gets the type entered into our set of
16141 user defined types. */
16142
16143 static struct type *
16144 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
16145 {
16146 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16147 struct type *type;
16148 struct attribute *attr;
16149 const char *name;
16150
16151 /* If the definition of this type lives in .debug_types, read that type.
16152 Don't follow DW_AT_specification though, that will take us back up
16153 the chain and we want to go down. */
16154 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16155 if (attr)
16156 {
16157 type = get_DW_AT_signature_type (die, attr, cu);
16158
16159 /* The type's CU may not be the same as CU.
16160 Ensure TYPE is recorded with CU in die_type_hash. */
16161 return set_die_type (die, type, cu);
16162 }
16163
16164 type = alloc_type (objfile);
16165 INIT_CPLUS_SPECIFIC (type);
16166
16167 name = dwarf2_name (die, cu);
16168 if (name != NULL)
16169 {
16170 if (cu->language == language_cplus
16171 || cu->language == language_d
16172 || cu->language == language_rust)
16173 {
16174 const char *full_name = dwarf2_full_name (name, die, cu);
16175
16176 /* dwarf2_full_name might have already finished building the DIE's
16177 type. If so, there is no need to continue. */
16178 if (get_die_type (die, cu) != NULL)
16179 return get_die_type (die, cu);
16180
16181 TYPE_TAG_NAME (type) = full_name;
16182 if (die->tag == DW_TAG_structure_type
16183 || die->tag == DW_TAG_class_type)
16184 TYPE_NAME (type) = TYPE_TAG_NAME (type);
16185 }
16186 else
16187 {
16188 /* The name is already allocated along with this objfile, so
16189 we don't need to duplicate it for the type. */
16190 TYPE_TAG_NAME (type) = name;
16191 if (die->tag == DW_TAG_class_type)
16192 TYPE_NAME (type) = TYPE_TAG_NAME (type);
16193 }
16194 }
16195
16196 if (die->tag == DW_TAG_structure_type)
16197 {
16198 TYPE_CODE (type) = TYPE_CODE_STRUCT;
16199 }
16200 else if (die->tag == DW_TAG_union_type)
16201 {
16202 TYPE_CODE (type) = TYPE_CODE_UNION;
16203 }
16204 else if (die->tag == DW_TAG_variant_part)
16205 {
16206 TYPE_CODE (type) = TYPE_CODE_UNION;
16207 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
16208 }
16209 else
16210 {
16211 TYPE_CODE (type) = TYPE_CODE_STRUCT;
16212 }
16213
16214 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16215 TYPE_DECLARED_CLASS (type) = 1;
16216
16217 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16218 if (attr)
16219 {
16220 if (attr_form_is_constant (attr))
16221 TYPE_LENGTH (type) = DW_UNSND (attr);
16222 else
16223 {
16224 /* For the moment, dynamic type sizes are not supported
16225 by GDB's struct type. The actual size is determined
16226 on-demand when resolving the type of a given object,
16227 so set the type's length to zero for now. Otherwise,
16228 we record an expression as the length, and that expression
16229 could lead to a very large value, which could eventually
16230 lead to us trying to allocate that much memory when creating
16231 a value of that type. */
16232 TYPE_LENGTH (type) = 0;
16233 }
16234 }
16235 else
16236 {
16237 TYPE_LENGTH (type) = 0;
16238 }
16239
16240 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16241 {
16242 /* ICC<14 does not output the required DW_AT_declaration on
16243 incomplete types, but gives them a size of zero. */
16244 TYPE_STUB (type) = 1;
16245 }
16246 else
16247 TYPE_STUB_SUPPORTED (type) = 1;
16248
16249 if (die_is_declaration (die, cu))
16250 TYPE_STUB (type) = 1;
16251 else if (attr == NULL && die->child == NULL
16252 && producer_is_realview (cu->producer))
16253 /* RealView does not output the required DW_AT_declaration
16254 on incomplete types. */
16255 TYPE_STUB (type) = 1;
16256
16257 /* We need to add the type field to the die immediately so we don't
16258 infinitely recurse when dealing with pointers to the structure
16259 type within the structure itself. */
16260 set_die_type (die, type, cu);
16261
16262 /* set_die_type should be already done. */
16263 set_descriptive_type (type, die, cu);
16264
16265 return type;
16266 }
16267
16268 /* A helper for process_structure_scope that handles a single member
16269 DIE. */
16270
16271 static void
16272 handle_struct_member_die (struct die_info *child_die, struct type *type,
16273 struct field_info *fi,
16274 std::vector<struct symbol *> *template_args,
16275 struct dwarf2_cu *cu)
16276 {
16277 if (child_die->tag == DW_TAG_member
16278 || child_die->tag == DW_TAG_variable
16279 || child_die->tag == DW_TAG_variant_part)
16280 {
16281 /* NOTE: carlton/2002-11-05: A C++ static data member
16282 should be a DW_TAG_member that is a declaration, but
16283 all versions of G++ as of this writing (so through at
16284 least 3.2.1) incorrectly generate DW_TAG_variable
16285 tags for them instead. */
16286 dwarf2_add_field (fi, child_die, cu);
16287 }
16288 else if (child_die->tag == DW_TAG_subprogram)
16289 {
16290 /* Rust doesn't have member functions in the C++ sense.
16291 However, it does emit ordinary functions as children
16292 of a struct DIE. */
16293 if (cu->language == language_rust)
16294 read_func_scope (child_die, cu);
16295 else
16296 {
16297 /* C++ member function. */
16298 dwarf2_add_member_fn (fi, child_die, type, cu);
16299 }
16300 }
16301 else if (child_die->tag == DW_TAG_inheritance)
16302 {
16303 /* C++ base class field. */
16304 dwarf2_add_field (fi, child_die, cu);
16305 }
16306 else if (type_can_define_types (child_die))
16307 dwarf2_add_type_defn (fi, child_die, cu);
16308 else if (child_die->tag == DW_TAG_template_type_param
16309 || child_die->tag == DW_TAG_template_value_param)
16310 {
16311 struct symbol *arg = new_symbol (child_die, NULL, cu);
16312
16313 if (arg != NULL)
16314 template_args->push_back (arg);
16315 }
16316 else if (child_die->tag == DW_TAG_variant)
16317 {
16318 /* In a variant we want to get the discriminant and also add a
16319 field for our sole member child. */
16320 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16321
16322 for (struct die_info *variant_child = child_die->child;
16323 variant_child != NULL;
16324 variant_child = sibling_die (variant_child))
16325 {
16326 if (variant_child->tag == DW_TAG_member)
16327 {
16328 handle_struct_member_die (variant_child, type, fi,
16329 template_args, cu);
16330 /* Only handle the one. */
16331 break;
16332 }
16333 }
16334
16335 /* We don't handle this but we might as well report it if we see
16336 it. */
16337 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
16338 complaint (&symfile_complaints,
16339 _("DW_AT_discr_list is not supported yet"
16340 " - DIE at %s [in module %s]"),
16341 sect_offset_str (child_die->sect_off),
16342 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16343
16344 /* The first field was just added, so we can stash the
16345 discriminant there. */
16346 gdb_assert (!fi->fields.empty ());
16347 if (discr == NULL)
16348 fi->fields.back ().variant.default_branch = true;
16349 else
16350 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
16351 }
16352 }
16353
16354 /* Finish creating a structure or union type, including filling in
16355 its members and creating a symbol for it. */
16356
16357 static void
16358 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16359 {
16360 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16361 struct die_info *child_die;
16362 struct type *type;
16363
16364 type = get_die_type (die, cu);
16365 if (type == NULL)
16366 type = read_structure_type (die, cu);
16367
16368 /* When reading a DW_TAG_variant_part, we need to notice when we
16369 read the discriminant member, so we can record it later in the
16370 discriminant_info. */
16371 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16372 sect_offset discr_offset;
16373
16374 if (is_variant_part)
16375 {
16376 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16377 if (discr == NULL)
16378 {
16379 /* Maybe it's a univariant form, an extension we support.
16380 In this case arrange not to check the offset. */
16381 is_variant_part = false;
16382 }
16383 else if (attr_form_is_ref (discr))
16384 {
16385 struct dwarf2_cu *target_cu = cu;
16386 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16387
16388 discr_offset = target_die->sect_off;
16389 }
16390 else
16391 {
16392 complaint (&symfile_complaints,
16393 _("DW_AT_discr does not have DIE reference form"
16394 " - DIE at %s [in module %s]"),
16395 sect_offset_str (die->sect_off),
16396 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16397 is_variant_part = false;
16398 }
16399 }
16400
16401 if (die->child != NULL && ! die_is_declaration (die, cu))
16402 {
16403 struct field_info fi;
16404 std::vector<struct symbol *> template_args;
16405
16406 child_die = die->child;
16407
16408 while (child_die && child_die->tag)
16409 {
16410 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16411
16412 if (is_variant_part && discr_offset == child_die->sect_off)
16413 fi.fields.back ().variant.is_discriminant = true;
16414
16415 child_die = sibling_die (child_die);
16416 }
16417
16418 /* Attach template arguments to type. */
16419 if (!template_args.empty ())
16420 {
16421 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16422 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16423 TYPE_TEMPLATE_ARGUMENTS (type)
16424 = XOBNEWVEC (&objfile->objfile_obstack,
16425 struct symbol *,
16426 TYPE_N_TEMPLATE_ARGUMENTS (type));
16427 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16428 template_args.data (),
16429 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16430 * sizeof (struct symbol *)));
16431 }
16432
16433 /* Attach fields and member functions to the type. */
16434 if (fi.nfields)
16435 dwarf2_attach_fields_to_type (&fi, type, cu);
16436 if (!fi.fnfieldlists.empty ())
16437 {
16438 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16439
16440 /* Get the type which refers to the base class (possibly this
16441 class itself) which contains the vtable pointer for the current
16442 class from the DW_AT_containing_type attribute. This use of
16443 DW_AT_containing_type is a GNU extension. */
16444
16445 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16446 {
16447 struct type *t = die_containing_type (die, cu);
16448
16449 set_type_vptr_basetype (type, t);
16450 if (type == t)
16451 {
16452 int i;
16453
16454 /* Our own class provides vtbl ptr. */
16455 for (i = TYPE_NFIELDS (t) - 1;
16456 i >= TYPE_N_BASECLASSES (t);
16457 --i)
16458 {
16459 const char *fieldname = TYPE_FIELD_NAME (t, i);
16460
16461 if (is_vtable_name (fieldname, cu))
16462 {
16463 set_type_vptr_fieldno (type, i);
16464 break;
16465 }
16466 }
16467
16468 /* Complain if virtual function table field not found. */
16469 if (i < TYPE_N_BASECLASSES (t))
16470 complaint (&symfile_complaints,
16471 _("virtual function table pointer "
16472 "not found when defining class '%s'"),
16473 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16474 "");
16475 }
16476 else
16477 {
16478 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16479 }
16480 }
16481 else if (cu->producer
16482 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16483 {
16484 /* The IBM XLC compiler does not provide direct indication
16485 of the containing type, but the vtable pointer is
16486 always named __vfp. */
16487
16488 int i;
16489
16490 for (i = TYPE_NFIELDS (type) - 1;
16491 i >= TYPE_N_BASECLASSES (type);
16492 --i)
16493 {
16494 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16495 {
16496 set_type_vptr_fieldno (type, i);
16497 set_type_vptr_basetype (type, type);
16498 break;
16499 }
16500 }
16501 }
16502 }
16503
16504 /* Copy fi.typedef_field_list linked list elements content into the
16505 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16506 if (!fi.typedef_field_list.empty ())
16507 {
16508 int count = fi.typedef_field_list.size ();
16509
16510 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16511 TYPE_TYPEDEF_FIELD_ARRAY (type)
16512 = ((struct decl_field *)
16513 TYPE_ALLOC (type,
16514 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16515 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16516
16517 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16518 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16519 }
16520
16521 /* Copy fi.nested_types_list linked list elements content into the
16522 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16523 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16524 {
16525 int count = fi.nested_types_list.size ();
16526
16527 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16528 TYPE_NESTED_TYPES_ARRAY (type)
16529 = ((struct decl_field *)
16530 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16531 TYPE_NESTED_TYPES_COUNT (type) = count;
16532
16533 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16534 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16535 }
16536 }
16537
16538 quirk_gcc_member_function_pointer (type, objfile);
16539 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16540 cu->rust_unions.push_back (type);
16541
16542 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16543 snapshots) has been known to create a die giving a declaration
16544 for a class that has, as a child, a die giving a definition for a
16545 nested class. So we have to process our children even if the
16546 current die is a declaration. Normally, of course, a declaration
16547 won't have any children at all. */
16548
16549 child_die = die->child;
16550
16551 while (child_die != NULL && child_die->tag)
16552 {
16553 if (child_die->tag == DW_TAG_member
16554 || child_die->tag == DW_TAG_variable
16555 || child_die->tag == DW_TAG_inheritance
16556 || child_die->tag == DW_TAG_template_value_param
16557 || child_die->tag == DW_TAG_template_type_param)
16558 {
16559 /* Do nothing. */
16560 }
16561 else
16562 process_die (child_die, cu);
16563
16564 child_die = sibling_die (child_die);
16565 }
16566
16567 /* Do not consider external references. According to the DWARF standard,
16568 these DIEs are identified by the fact that they have no byte_size
16569 attribute, and a declaration attribute. */
16570 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16571 || !die_is_declaration (die, cu))
16572 new_symbol (die, type, cu);
16573 }
16574
16575 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16576 update TYPE using some information only available in DIE's children. */
16577
16578 static void
16579 update_enumeration_type_from_children (struct die_info *die,
16580 struct type *type,
16581 struct dwarf2_cu *cu)
16582 {
16583 struct die_info *child_die;
16584 int unsigned_enum = 1;
16585 int flag_enum = 1;
16586 ULONGEST mask = 0;
16587
16588 auto_obstack obstack;
16589
16590 for (child_die = die->child;
16591 child_die != NULL && child_die->tag;
16592 child_die = sibling_die (child_die))
16593 {
16594 struct attribute *attr;
16595 LONGEST value;
16596 const gdb_byte *bytes;
16597 struct dwarf2_locexpr_baton *baton;
16598 const char *name;
16599
16600 if (child_die->tag != DW_TAG_enumerator)
16601 continue;
16602
16603 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16604 if (attr == NULL)
16605 continue;
16606
16607 name = dwarf2_name (child_die, cu);
16608 if (name == NULL)
16609 name = "<anonymous enumerator>";
16610
16611 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16612 &value, &bytes, &baton);
16613 if (value < 0)
16614 {
16615 unsigned_enum = 0;
16616 flag_enum = 0;
16617 }
16618 else if ((mask & value) != 0)
16619 flag_enum = 0;
16620 else
16621 mask |= value;
16622
16623 /* If we already know that the enum type is neither unsigned, nor
16624 a flag type, no need to look at the rest of the enumerates. */
16625 if (!unsigned_enum && !flag_enum)
16626 break;
16627 }
16628
16629 if (unsigned_enum)
16630 TYPE_UNSIGNED (type) = 1;
16631 if (flag_enum)
16632 TYPE_FLAG_ENUM (type) = 1;
16633 }
16634
16635 /* Given a DW_AT_enumeration_type die, set its type. We do not
16636 complete the type's fields yet, or create any symbols. */
16637
16638 static struct type *
16639 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16640 {
16641 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16642 struct type *type;
16643 struct attribute *attr;
16644 const char *name;
16645
16646 /* If the definition of this type lives in .debug_types, read that type.
16647 Don't follow DW_AT_specification though, that will take us back up
16648 the chain and we want to go down. */
16649 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16650 if (attr)
16651 {
16652 type = get_DW_AT_signature_type (die, attr, cu);
16653
16654 /* The type's CU may not be the same as CU.
16655 Ensure TYPE is recorded with CU in die_type_hash. */
16656 return set_die_type (die, type, cu);
16657 }
16658
16659 type = alloc_type (objfile);
16660
16661 TYPE_CODE (type) = TYPE_CODE_ENUM;
16662 name = dwarf2_full_name (NULL, die, cu);
16663 if (name != NULL)
16664 TYPE_TAG_NAME (type) = name;
16665
16666 attr = dwarf2_attr (die, DW_AT_type, cu);
16667 if (attr != NULL)
16668 {
16669 struct type *underlying_type = die_type (die, cu);
16670
16671 TYPE_TARGET_TYPE (type) = underlying_type;
16672 }
16673
16674 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16675 if (attr)
16676 {
16677 TYPE_LENGTH (type) = DW_UNSND (attr);
16678 }
16679 else
16680 {
16681 TYPE_LENGTH (type) = 0;
16682 }
16683
16684 /* The enumeration DIE can be incomplete. In Ada, any type can be
16685 declared as private in the package spec, and then defined only
16686 inside the package body. Such types are known as Taft Amendment
16687 Types. When another package uses such a type, an incomplete DIE
16688 may be generated by the compiler. */
16689 if (die_is_declaration (die, cu))
16690 TYPE_STUB (type) = 1;
16691
16692 /* Finish the creation of this type by using the enum's children.
16693 We must call this even when the underlying type has been provided
16694 so that we can determine if we're looking at a "flag" enum. */
16695 update_enumeration_type_from_children (die, type, cu);
16696
16697 /* If this type has an underlying type that is not a stub, then we
16698 may use its attributes. We always use the "unsigned" attribute
16699 in this situation, because ordinarily we guess whether the type
16700 is unsigned -- but the guess can be wrong and the underlying type
16701 can tell us the reality. However, we defer to a local size
16702 attribute if one exists, because this lets the compiler override
16703 the underlying type if needed. */
16704 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16705 {
16706 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16707 if (TYPE_LENGTH (type) == 0)
16708 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16709 }
16710
16711 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16712
16713 return set_die_type (die, type, cu);
16714 }
16715
16716 /* Given a pointer to a die which begins an enumeration, process all
16717 the dies that define the members of the enumeration, and create the
16718 symbol for the enumeration type.
16719
16720 NOTE: We reverse the order of the element list. */
16721
16722 static void
16723 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16724 {
16725 struct type *this_type;
16726
16727 this_type = get_die_type (die, cu);
16728 if (this_type == NULL)
16729 this_type = read_enumeration_type (die, cu);
16730
16731 if (die->child != NULL)
16732 {
16733 struct die_info *child_die;
16734 struct symbol *sym;
16735 struct field *fields = NULL;
16736 int num_fields = 0;
16737 const char *name;
16738
16739 child_die = die->child;
16740 while (child_die && child_die->tag)
16741 {
16742 if (child_die->tag != DW_TAG_enumerator)
16743 {
16744 process_die (child_die, cu);
16745 }
16746 else
16747 {
16748 name = dwarf2_name (child_die, cu);
16749 if (name)
16750 {
16751 sym = new_symbol (child_die, this_type, cu);
16752
16753 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16754 {
16755 fields = (struct field *)
16756 xrealloc (fields,
16757 (num_fields + DW_FIELD_ALLOC_CHUNK)
16758 * sizeof (struct field));
16759 }
16760
16761 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16762 FIELD_TYPE (fields[num_fields]) = NULL;
16763 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16764 FIELD_BITSIZE (fields[num_fields]) = 0;
16765
16766 num_fields++;
16767 }
16768 }
16769
16770 child_die = sibling_die (child_die);
16771 }
16772
16773 if (num_fields)
16774 {
16775 TYPE_NFIELDS (this_type) = num_fields;
16776 TYPE_FIELDS (this_type) = (struct field *)
16777 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16778 memcpy (TYPE_FIELDS (this_type), fields,
16779 sizeof (struct field) * num_fields);
16780 xfree (fields);
16781 }
16782 }
16783
16784 /* If we are reading an enum from a .debug_types unit, and the enum
16785 is a declaration, and the enum is not the signatured type in the
16786 unit, then we do not want to add a symbol for it. Adding a
16787 symbol would in some cases obscure the true definition of the
16788 enum, giving users an incomplete type when the definition is
16789 actually available. Note that we do not want to do this for all
16790 enums which are just declarations, because C++0x allows forward
16791 enum declarations. */
16792 if (cu->per_cu->is_debug_types
16793 && die_is_declaration (die, cu))
16794 {
16795 struct signatured_type *sig_type;
16796
16797 sig_type = (struct signatured_type *) cu->per_cu;
16798 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16799 if (sig_type->type_offset_in_section != die->sect_off)
16800 return;
16801 }
16802
16803 new_symbol (die, this_type, cu);
16804 }
16805
16806 /* Extract all information from a DW_TAG_array_type DIE and put it in
16807 the DIE's type field. For now, this only handles one dimensional
16808 arrays. */
16809
16810 static struct type *
16811 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16812 {
16813 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16814 struct die_info *child_die;
16815 struct type *type;
16816 struct type *element_type, *range_type, *index_type;
16817 struct attribute *attr;
16818 const char *name;
16819 struct dynamic_prop *byte_stride_prop = NULL;
16820 unsigned int bit_stride = 0;
16821
16822 element_type = die_type (die, cu);
16823
16824 /* The die_type call above may have already set the type for this DIE. */
16825 type = get_die_type (die, cu);
16826 if (type)
16827 return type;
16828
16829 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16830 if (attr != NULL)
16831 {
16832 int stride_ok;
16833
16834 byte_stride_prop
16835 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16836 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16837 if (!stride_ok)
16838 {
16839 complaint (&symfile_complaints,
16840 _("unable to read array DW_AT_byte_stride "
16841 " - DIE at %s [in module %s]"),
16842 sect_offset_str (die->sect_off),
16843 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16844 /* Ignore this attribute. We will likely not be able to print
16845 arrays of this type correctly, but there is little we can do
16846 to help if we cannot read the attribute's value. */
16847 byte_stride_prop = NULL;
16848 }
16849 }
16850
16851 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16852 if (attr != NULL)
16853 bit_stride = DW_UNSND (attr);
16854
16855 /* Irix 6.2 native cc creates array types without children for
16856 arrays with unspecified length. */
16857 if (die->child == NULL)
16858 {
16859 index_type = objfile_type (objfile)->builtin_int;
16860 range_type = create_static_range_type (NULL, index_type, 0, -1);
16861 type = create_array_type_with_stride (NULL, element_type, range_type,
16862 byte_stride_prop, bit_stride);
16863 return set_die_type (die, type, cu);
16864 }
16865
16866 std::vector<struct type *> range_types;
16867 child_die = die->child;
16868 while (child_die && child_die->tag)
16869 {
16870 if (child_die->tag == DW_TAG_subrange_type)
16871 {
16872 struct type *child_type = read_type_die (child_die, cu);
16873
16874 if (child_type != NULL)
16875 {
16876 /* The range type was succesfully read. Save it for the
16877 array type creation. */
16878 range_types.push_back (child_type);
16879 }
16880 }
16881 child_die = sibling_die (child_die);
16882 }
16883
16884 /* Dwarf2 dimensions are output from left to right, create the
16885 necessary array types in backwards order. */
16886
16887 type = element_type;
16888
16889 if (read_array_order (die, cu) == DW_ORD_col_major)
16890 {
16891 int i = 0;
16892
16893 while (i < range_types.size ())
16894 type = create_array_type_with_stride (NULL, type, range_types[i++],
16895 byte_stride_prop, bit_stride);
16896 }
16897 else
16898 {
16899 size_t ndim = range_types.size ();
16900 while (ndim-- > 0)
16901 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16902 byte_stride_prop, bit_stride);
16903 }
16904
16905 /* Understand Dwarf2 support for vector types (like they occur on
16906 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16907 array type. This is not part of the Dwarf2/3 standard yet, but a
16908 custom vendor extension. The main difference between a regular
16909 array and the vector variant is that vectors are passed by value
16910 to functions. */
16911 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16912 if (attr)
16913 make_vector_type (type);
16914
16915 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16916 implementation may choose to implement triple vectors using this
16917 attribute. */
16918 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16919 if (attr)
16920 {
16921 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16922 TYPE_LENGTH (type) = DW_UNSND (attr);
16923 else
16924 complaint (&symfile_complaints,
16925 _("DW_AT_byte_size for array type smaller "
16926 "than the total size of elements"));
16927 }
16928
16929 name = dwarf2_name (die, cu);
16930 if (name)
16931 TYPE_NAME (type) = name;
16932
16933 /* Install the type in the die. */
16934 set_die_type (die, type, cu);
16935
16936 /* set_die_type should be already done. */
16937 set_descriptive_type (type, die, cu);
16938
16939 return type;
16940 }
16941
16942 static enum dwarf_array_dim_ordering
16943 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16944 {
16945 struct attribute *attr;
16946
16947 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16948
16949 if (attr)
16950 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16951
16952 /* GNU F77 is a special case, as at 08/2004 array type info is the
16953 opposite order to the dwarf2 specification, but data is still
16954 laid out as per normal fortran.
16955
16956 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16957 version checking. */
16958
16959 if (cu->language == language_fortran
16960 && cu->producer && strstr (cu->producer, "GNU F77"))
16961 {
16962 return DW_ORD_row_major;
16963 }
16964
16965 switch (cu->language_defn->la_array_ordering)
16966 {
16967 case array_column_major:
16968 return DW_ORD_col_major;
16969 case array_row_major:
16970 default:
16971 return DW_ORD_row_major;
16972 };
16973 }
16974
16975 /* Extract all information from a DW_TAG_set_type DIE and put it in
16976 the DIE's type field. */
16977
16978 static struct type *
16979 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16980 {
16981 struct type *domain_type, *set_type;
16982 struct attribute *attr;
16983
16984 domain_type = die_type (die, cu);
16985
16986 /* The die_type call above may have already set the type for this DIE. */
16987 set_type = get_die_type (die, cu);
16988 if (set_type)
16989 return set_type;
16990
16991 set_type = create_set_type (NULL, domain_type);
16992
16993 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16994 if (attr)
16995 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16996
16997 return set_die_type (die, set_type, cu);
16998 }
16999
17000 /* A helper for read_common_block that creates a locexpr baton.
17001 SYM is the symbol which we are marking as computed.
17002 COMMON_DIE is the DIE for the common block.
17003 COMMON_LOC is the location expression attribute for the common
17004 block itself.
17005 MEMBER_LOC is the location expression attribute for the particular
17006 member of the common block that we are processing.
17007 CU is the CU from which the above come. */
17008
17009 static void
17010 mark_common_block_symbol_computed (struct symbol *sym,
17011 struct die_info *common_die,
17012 struct attribute *common_loc,
17013 struct attribute *member_loc,
17014 struct dwarf2_cu *cu)
17015 {
17016 struct dwarf2_per_objfile *dwarf2_per_objfile
17017 = cu->per_cu->dwarf2_per_objfile;
17018 struct objfile *objfile = dwarf2_per_objfile->objfile;
17019 struct dwarf2_locexpr_baton *baton;
17020 gdb_byte *ptr;
17021 unsigned int cu_off;
17022 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
17023 LONGEST offset = 0;
17024
17025 gdb_assert (common_loc && member_loc);
17026 gdb_assert (attr_form_is_block (common_loc));
17027 gdb_assert (attr_form_is_block (member_loc)
17028 || attr_form_is_constant (member_loc));
17029
17030 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17031 baton->per_cu = cu->per_cu;
17032 gdb_assert (baton->per_cu);
17033
17034 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17035
17036 if (attr_form_is_constant (member_loc))
17037 {
17038 offset = dwarf2_get_attr_constant_value (member_loc, 0);
17039 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17040 }
17041 else
17042 baton->size += DW_BLOCK (member_loc)->size;
17043
17044 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17045 baton->data = ptr;
17046
17047 *ptr++ = DW_OP_call4;
17048 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17049 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17050 ptr += 4;
17051
17052 if (attr_form_is_constant (member_loc))
17053 {
17054 *ptr++ = DW_OP_addr;
17055 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17056 ptr += cu->header.addr_size;
17057 }
17058 else
17059 {
17060 /* We have to copy the data here, because DW_OP_call4 will only
17061 use a DW_AT_location attribute. */
17062 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
17063 ptr += DW_BLOCK (member_loc)->size;
17064 }
17065
17066 *ptr++ = DW_OP_plus;
17067 gdb_assert (ptr - baton->data == baton->size);
17068
17069 SYMBOL_LOCATION_BATON (sym) = baton;
17070 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17071 }
17072
17073 /* Create appropriate locally-scoped variables for all the
17074 DW_TAG_common_block entries. Also create a struct common_block
17075 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17076 is used to sepate the common blocks name namespace from regular
17077 variable names. */
17078
17079 static void
17080 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17081 {
17082 struct attribute *attr;
17083
17084 attr = dwarf2_attr (die, DW_AT_location, cu);
17085 if (attr)
17086 {
17087 /* Support the .debug_loc offsets. */
17088 if (attr_form_is_block (attr))
17089 {
17090 /* Ok. */
17091 }
17092 else if (attr_form_is_section_offset (attr))
17093 {
17094 dwarf2_complex_location_expr_complaint ();
17095 attr = NULL;
17096 }
17097 else
17098 {
17099 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17100 "common block member");
17101 attr = NULL;
17102 }
17103 }
17104
17105 if (die->child != NULL)
17106 {
17107 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17108 struct die_info *child_die;
17109 size_t n_entries = 0, size;
17110 struct common_block *common_block;
17111 struct symbol *sym;
17112
17113 for (child_die = die->child;
17114 child_die && child_die->tag;
17115 child_die = sibling_die (child_die))
17116 ++n_entries;
17117
17118 size = (sizeof (struct common_block)
17119 + (n_entries - 1) * sizeof (struct symbol *));
17120 common_block
17121 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17122 size);
17123 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17124 common_block->n_entries = 0;
17125
17126 for (child_die = die->child;
17127 child_die && child_die->tag;
17128 child_die = sibling_die (child_die))
17129 {
17130 /* Create the symbol in the DW_TAG_common_block block in the current
17131 symbol scope. */
17132 sym = new_symbol (child_die, NULL, cu);
17133 if (sym != NULL)
17134 {
17135 struct attribute *member_loc;
17136
17137 common_block->contents[common_block->n_entries++] = sym;
17138
17139 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17140 cu);
17141 if (member_loc)
17142 {
17143 /* GDB has handled this for a long time, but it is
17144 not specified by DWARF. It seems to have been
17145 emitted by gfortran at least as recently as:
17146 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17147 complaint (&symfile_complaints,
17148 _("Variable in common block has "
17149 "DW_AT_data_member_location "
17150 "- DIE at %s [in module %s]"),
17151 sect_offset_str (child_die->sect_off),
17152 objfile_name (objfile));
17153
17154 if (attr_form_is_section_offset (member_loc))
17155 dwarf2_complex_location_expr_complaint ();
17156 else if (attr_form_is_constant (member_loc)
17157 || attr_form_is_block (member_loc))
17158 {
17159 if (attr)
17160 mark_common_block_symbol_computed (sym, die, attr,
17161 member_loc, cu);
17162 }
17163 else
17164 dwarf2_complex_location_expr_complaint ();
17165 }
17166 }
17167 }
17168
17169 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17170 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17171 }
17172 }
17173
17174 /* Create a type for a C++ namespace. */
17175
17176 static struct type *
17177 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17178 {
17179 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17180 const char *previous_prefix, *name;
17181 int is_anonymous;
17182 struct type *type;
17183
17184 /* For extensions, reuse the type of the original namespace. */
17185 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17186 {
17187 struct die_info *ext_die;
17188 struct dwarf2_cu *ext_cu = cu;
17189
17190 ext_die = dwarf2_extension (die, &ext_cu);
17191 type = read_type_die (ext_die, ext_cu);
17192
17193 /* EXT_CU may not be the same as CU.
17194 Ensure TYPE is recorded with CU in die_type_hash. */
17195 return set_die_type (die, type, cu);
17196 }
17197
17198 name = namespace_name (die, &is_anonymous, cu);
17199
17200 /* Now build the name of the current namespace. */
17201
17202 previous_prefix = determine_prefix (die, cu);
17203 if (previous_prefix[0] != '\0')
17204 name = typename_concat (&objfile->objfile_obstack,
17205 previous_prefix, name, 0, cu);
17206
17207 /* Create the type. */
17208 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17209 TYPE_TAG_NAME (type) = TYPE_NAME (type);
17210
17211 return set_die_type (die, type, cu);
17212 }
17213
17214 /* Read a namespace scope. */
17215
17216 static void
17217 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17218 {
17219 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17220 int is_anonymous;
17221
17222 /* Add a symbol associated to this if we haven't seen the namespace
17223 before. Also, add a using directive if it's an anonymous
17224 namespace. */
17225
17226 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17227 {
17228 struct type *type;
17229
17230 type = read_type_die (die, cu);
17231 new_symbol (die, type, cu);
17232
17233 namespace_name (die, &is_anonymous, cu);
17234 if (is_anonymous)
17235 {
17236 const char *previous_prefix = determine_prefix (die, cu);
17237
17238 std::vector<const char *> excludes;
17239 add_using_directive (using_directives (cu->language),
17240 previous_prefix, TYPE_NAME (type), NULL,
17241 NULL, excludes, 0, &objfile->objfile_obstack);
17242 }
17243 }
17244
17245 if (die->child != NULL)
17246 {
17247 struct die_info *child_die = die->child;
17248
17249 while (child_die && child_die->tag)
17250 {
17251 process_die (child_die, cu);
17252 child_die = sibling_die (child_die);
17253 }
17254 }
17255 }
17256
17257 /* Read a Fortran module as type. This DIE can be only a declaration used for
17258 imported module. Still we need that type as local Fortran "use ... only"
17259 declaration imports depend on the created type in determine_prefix. */
17260
17261 static struct type *
17262 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17263 {
17264 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17265 const char *module_name;
17266 struct type *type;
17267
17268 module_name = dwarf2_name (die, cu);
17269 if (!module_name)
17270 complaint (&symfile_complaints,
17271 _("DW_TAG_module has no name, offset %s"),
17272 sect_offset_str (die->sect_off));
17273 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17274
17275 /* determine_prefix uses TYPE_TAG_NAME. */
17276 TYPE_TAG_NAME (type) = TYPE_NAME (type);
17277
17278 return set_die_type (die, type, cu);
17279 }
17280
17281 /* Read a Fortran module. */
17282
17283 static void
17284 read_module (struct die_info *die, struct dwarf2_cu *cu)
17285 {
17286 struct die_info *child_die = die->child;
17287 struct type *type;
17288
17289 type = read_type_die (die, cu);
17290 new_symbol (die, type, cu);
17291
17292 while (child_die && child_die->tag)
17293 {
17294 process_die (child_die, cu);
17295 child_die = sibling_die (child_die);
17296 }
17297 }
17298
17299 /* Return the name of the namespace represented by DIE. Set
17300 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17301 namespace. */
17302
17303 static const char *
17304 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17305 {
17306 struct die_info *current_die;
17307 const char *name = NULL;
17308
17309 /* Loop through the extensions until we find a name. */
17310
17311 for (current_die = die;
17312 current_die != NULL;
17313 current_die = dwarf2_extension (die, &cu))
17314 {
17315 /* We don't use dwarf2_name here so that we can detect the absence
17316 of a name -> anonymous namespace. */
17317 name = dwarf2_string_attr (die, DW_AT_name, cu);
17318
17319 if (name != NULL)
17320 break;
17321 }
17322
17323 /* Is it an anonymous namespace? */
17324
17325 *is_anonymous = (name == NULL);
17326 if (*is_anonymous)
17327 name = CP_ANONYMOUS_NAMESPACE_STR;
17328
17329 return name;
17330 }
17331
17332 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17333 the user defined type vector. */
17334
17335 static struct type *
17336 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17337 {
17338 struct gdbarch *gdbarch
17339 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17340 struct comp_unit_head *cu_header = &cu->header;
17341 struct type *type;
17342 struct attribute *attr_byte_size;
17343 struct attribute *attr_address_class;
17344 int byte_size, addr_class;
17345 struct type *target_type;
17346
17347 target_type = die_type (die, cu);
17348
17349 /* The die_type call above may have already set the type for this DIE. */
17350 type = get_die_type (die, cu);
17351 if (type)
17352 return type;
17353
17354 type = lookup_pointer_type (target_type);
17355
17356 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17357 if (attr_byte_size)
17358 byte_size = DW_UNSND (attr_byte_size);
17359 else
17360 byte_size = cu_header->addr_size;
17361
17362 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17363 if (attr_address_class)
17364 addr_class = DW_UNSND (attr_address_class);
17365 else
17366 addr_class = DW_ADDR_none;
17367
17368 /* If the pointer size or address class is different than the
17369 default, create a type variant marked as such and set the
17370 length accordingly. */
17371 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
17372 {
17373 if (gdbarch_address_class_type_flags_p (gdbarch))
17374 {
17375 int type_flags;
17376
17377 type_flags = gdbarch_address_class_type_flags
17378 (gdbarch, byte_size, addr_class);
17379 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17380 == 0);
17381 type = make_type_with_address_space (type, type_flags);
17382 }
17383 else if (TYPE_LENGTH (type) != byte_size)
17384 {
17385 complaint (&symfile_complaints,
17386 _("invalid pointer size %d"), byte_size);
17387 }
17388 else
17389 {
17390 /* Should we also complain about unhandled address classes? */
17391 }
17392 }
17393
17394 TYPE_LENGTH (type) = byte_size;
17395 return set_die_type (die, type, cu);
17396 }
17397
17398 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17399 the user defined type vector. */
17400
17401 static struct type *
17402 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17403 {
17404 struct type *type;
17405 struct type *to_type;
17406 struct type *domain;
17407
17408 to_type = die_type (die, cu);
17409 domain = die_containing_type (die, cu);
17410
17411 /* The calls above may have already set the type for this DIE. */
17412 type = get_die_type (die, cu);
17413 if (type)
17414 return type;
17415
17416 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17417 type = lookup_methodptr_type (to_type);
17418 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17419 {
17420 struct type *new_type
17421 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17422
17423 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17424 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17425 TYPE_VARARGS (to_type));
17426 type = lookup_methodptr_type (new_type);
17427 }
17428 else
17429 type = lookup_memberptr_type (to_type, domain);
17430
17431 return set_die_type (die, type, cu);
17432 }
17433
17434 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17435 the user defined type vector. */
17436
17437 static struct type *
17438 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17439 enum type_code refcode)
17440 {
17441 struct comp_unit_head *cu_header = &cu->header;
17442 struct type *type, *target_type;
17443 struct attribute *attr;
17444
17445 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17446
17447 target_type = die_type (die, cu);
17448
17449 /* The die_type call above may have already set the type for this DIE. */
17450 type = get_die_type (die, cu);
17451 if (type)
17452 return type;
17453
17454 type = lookup_reference_type (target_type, refcode);
17455 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17456 if (attr)
17457 {
17458 TYPE_LENGTH (type) = DW_UNSND (attr);
17459 }
17460 else
17461 {
17462 TYPE_LENGTH (type) = cu_header->addr_size;
17463 }
17464 return set_die_type (die, type, cu);
17465 }
17466
17467 /* Add the given cv-qualifiers to the element type of the array. GCC
17468 outputs DWARF type qualifiers that apply to an array, not the
17469 element type. But GDB relies on the array element type to carry
17470 the cv-qualifiers. This mimics section 6.7.3 of the C99
17471 specification. */
17472
17473 static struct type *
17474 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17475 struct type *base_type, int cnst, int voltl)
17476 {
17477 struct type *el_type, *inner_array;
17478
17479 base_type = copy_type (base_type);
17480 inner_array = base_type;
17481
17482 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17483 {
17484 TYPE_TARGET_TYPE (inner_array) =
17485 copy_type (TYPE_TARGET_TYPE (inner_array));
17486 inner_array = TYPE_TARGET_TYPE (inner_array);
17487 }
17488
17489 el_type = TYPE_TARGET_TYPE (inner_array);
17490 cnst |= TYPE_CONST (el_type);
17491 voltl |= TYPE_VOLATILE (el_type);
17492 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17493
17494 return set_die_type (die, base_type, cu);
17495 }
17496
17497 static struct type *
17498 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17499 {
17500 struct type *base_type, *cv_type;
17501
17502 base_type = die_type (die, cu);
17503
17504 /* The die_type call above may have already set the type for this DIE. */
17505 cv_type = get_die_type (die, cu);
17506 if (cv_type)
17507 return cv_type;
17508
17509 /* In case the const qualifier is applied to an array type, the element type
17510 is so qualified, not the array type (section 6.7.3 of C99). */
17511 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17512 return add_array_cv_type (die, cu, base_type, 1, 0);
17513
17514 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17515 return set_die_type (die, cv_type, cu);
17516 }
17517
17518 static struct type *
17519 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17520 {
17521 struct type *base_type, *cv_type;
17522
17523 base_type = die_type (die, cu);
17524
17525 /* The die_type call above may have already set the type for this DIE. */
17526 cv_type = get_die_type (die, cu);
17527 if (cv_type)
17528 return cv_type;
17529
17530 /* In case the volatile qualifier is applied to an array type, the
17531 element type is so qualified, not the array type (section 6.7.3
17532 of C99). */
17533 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17534 return add_array_cv_type (die, cu, base_type, 0, 1);
17535
17536 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17537 return set_die_type (die, cv_type, cu);
17538 }
17539
17540 /* Handle DW_TAG_restrict_type. */
17541
17542 static struct type *
17543 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17544 {
17545 struct type *base_type, *cv_type;
17546
17547 base_type = die_type (die, cu);
17548
17549 /* The die_type call above may have already set the type for this DIE. */
17550 cv_type = get_die_type (die, cu);
17551 if (cv_type)
17552 return cv_type;
17553
17554 cv_type = make_restrict_type (base_type);
17555 return set_die_type (die, cv_type, cu);
17556 }
17557
17558 /* Handle DW_TAG_atomic_type. */
17559
17560 static struct type *
17561 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17562 {
17563 struct type *base_type, *cv_type;
17564
17565 base_type = die_type (die, cu);
17566
17567 /* The die_type call above may have already set the type for this DIE. */
17568 cv_type = get_die_type (die, cu);
17569 if (cv_type)
17570 return cv_type;
17571
17572 cv_type = make_atomic_type (base_type);
17573 return set_die_type (die, cv_type, cu);
17574 }
17575
17576 /* Extract all information from a DW_TAG_string_type DIE and add to
17577 the user defined type vector. It isn't really a user defined type,
17578 but it behaves like one, with other DIE's using an AT_user_def_type
17579 attribute to reference it. */
17580
17581 static struct type *
17582 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17583 {
17584 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17585 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17586 struct type *type, *range_type, *index_type, *char_type;
17587 struct attribute *attr;
17588 unsigned int length;
17589
17590 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17591 if (attr)
17592 {
17593 length = DW_UNSND (attr);
17594 }
17595 else
17596 {
17597 /* Check for the DW_AT_byte_size attribute. */
17598 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17599 if (attr)
17600 {
17601 length = DW_UNSND (attr);
17602 }
17603 else
17604 {
17605 length = 1;
17606 }
17607 }
17608
17609 index_type = objfile_type (objfile)->builtin_int;
17610 range_type = create_static_range_type (NULL, index_type, 1, length);
17611 char_type = language_string_char_type (cu->language_defn, gdbarch);
17612 type = create_string_type (NULL, char_type, range_type);
17613
17614 return set_die_type (die, type, cu);
17615 }
17616
17617 /* Assuming that DIE corresponds to a function, returns nonzero
17618 if the function is prototyped. */
17619
17620 static int
17621 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17622 {
17623 struct attribute *attr;
17624
17625 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17626 if (attr && (DW_UNSND (attr) != 0))
17627 return 1;
17628
17629 /* The DWARF standard implies that the DW_AT_prototyped attribute
17630 is only meaninful for C, but the concept also extends to other
17631 languages that allow unprototyped functions (Eg: Objective C).
17632 For all other languages, assume that functions are always
17633 prototyped. */
17634 if (cu->language != language_c
17635 && cu->language != language_objc
17636 && cu->language != language_opencl)
17637 return 1;
17638
17639 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17640 prototyped and unprototyped functions; default to prototyped,
17641 since that is more common in modern code (and RealView warns
17642 about unprototyped functions). */
17643 if (producer_is_realview (cu->producer))
17644 return 1;
17645
17646 return 0;
17647 }
17648
17649 /* Handle DIES due to C code like:
17650
17651 struct foo
17652 {
17653 int (*funcp)(int a, long l);
17654 int b;
17655 };
17656
17657 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17658
17659 static struct type *
17660 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17661 {
17662 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17663 struct type *type; /* Type that this function returns. */
17664 struct type *ftype; /* Function that returns above type. */
17665 struct attribute *attr;
17666
17667 type = die_type (die, cu);
17668
17669 /* The die_type call above may have already set the type for this DIE. */
17670 ftype = get_die_type (die, cu);
17671 if (ftype)
17672 return ftype;
17673
17674 ftype = lookup_function_type (type);
17675
17676 if (prototyped_function_p (die, cu))
17677 TYPE_PROTOTYPED (ftype) = 1;
17678
17679 /* Store the calling convention in the type if it's available in
17680 the subroutine die. Otherwise set the calling convention to
17681 the default value DW_CC_normal. */
17682 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17683 if (attr)
17684 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17685 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17686 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17687 else
17688 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17689
17690 /* Record whether the function returns normally to its caller or not
17691 if the DWARF producer set that information. */
17692 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17693 if (attr && (DW_UNSND (attr) != 0))
17694 TYPE_NO_RETURN (ftype) = 1;
17695
17696 /* We need to add the subroutine type to the die immediately so
17697 we don't infinitely recurse when dealing with parameters
17698 declared as the same subroutine type. */
17699 set_die_type (die, ftype, cu);
17700
17701 if (die->child != NULL)
17702 {
17703 struct type *void_type = objfile_type (objfile)->builtin_void;
17704 struct die_info *child_die;
17705 int nparams, iparams;
17706
17707 /* Count the number of parameters.
17708 FIXME: GDB currently ignores vararg functions, but knows about
17709 vararg member functions. */
17710 nparams = 0;
17711 child_die = die->child;
17712 while (child_die && child_die->tag)
17713 {
17714 if (child_die->tag == DW_TAG_formal_parameter)
17715 nparams++;
17716 else if (child_die->tag == DW_TAG_unspecified_parameters)
17717 TYPE_VARARGS (ftype) = 1;
17718 child_die = sibling_die (child_die);
17719 }
17720
17721 /* Allocate storage for parameters and fill them in. */
17722 TYPE_NFIELDS (ftype) = nparams;
17723 TYPE_FIELDS (ftype) = (struct field *)
17724 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17725
17726 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17727 even if we error out during the parameters reading below. */
17728 for (iparams = 0; iparams < nparams; iparams++)
17729 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17730
17731 iparams = 0;
17732 child_die = die->child;
17733 while (child_die && child_die->tag)
17734 {
17735 if (child_die->tag == DW_TAG_formal_parameter)
17736 {
17737 struct type *arg_type;
17738
17739 /* DWARF version 2 has no clean way to discern C++
17740 static and non-static member functions. G++ helps
17741 GDB by marking the first parameter for non-static
17742 member functions (which is the this pointer) as
17743 artificial. We pass this information to
17744 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17745
17746 DWARF version 3 added DW_AT_object_pointer, which GCC
17747 4.5 does not yet generate. */
17748 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17749 if (attr)
17750 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17751 else
17752 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17753 arg_type = die_type (child_die, cu);
17754
17755 /* RealView does not mark THIS as const, which the testsuite
17756 expects. GCC marks THIS as const in method definitions,
17757 but not in the class specifications (GCC PR 43053). */
17758 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17759 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17760 {
17761 int is_this = 0;
17762 struct dwarf2_cu *arg_cu = cu;
17763 const char *name = dwarf2_name (child_die, cu);
17764
17765 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17766 if (attr)
17767 {
17768 /* If the compiler emits this, use it. */
17769 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17770 is_this = 1;
17771 }
17772 else if (name && strcmp (name, "this") == 0)
17773 /* Function definitions will have the argument names. */
17774 is_this = 1;
17775 else if (name == NULL && iparams == 0)
17776 /* Declarations may not have the names, so like
17777 elsewhere in GDB, assume an artificial first
17778 argument is "this". */
17779 is_this = 1;
17780
17781 if (is_this)
17782 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17783 arg_type, 0);
17784 }
17785
17786 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17787 iparams++;
17788 }
17789 child_die = sibling_die (child_die);
17790 }
17791 }
17792
17793 return ftype;
17794 }
17795
17796 static struct type *
17797 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17798 {
17799 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17800 const char *name = NULL;
17801 struct type *this_type, *target_type;
17802
17803 name = dwarf2_full_name (NULL, die, cu);
17804 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17805 TYPE_TARGET_STUB (this_type) = 1;
17806 set_die_type (die, this_type, cu);
17807 target_type = die_type (die, cu);
17808 if (target_type != this_type)
17809 TYPE_TARGET_TYPE (this_type) = target_type;
17810 else
17811 {
17812 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17813 spec and cause infinite loops in GDB. */
17814 complaint (&symfile_complaints,
17815 _("Self-referential DW_TAG_typedef "
17816 "- DIE at %s [in module %s]"),
17817 sect_offset_str (die->sect_off), objfile_name (objfile));
17818 TYPE_TARGET_TYPE (this_type) = NULL;
17819 }
17820 return this_type;
17821 }
17822
17823 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17824 (which may be different from NAME) to the architecture back-end to allow
17825 it to guess the correct format if necessary. */
17826
17827 static struct type *
17828 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17829 const char *name_hint)
17830 {
17831 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17832 const struct floatformat **format;
17833 struct type *type;
17834
17835 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17836 if (format)
17837 type = init_float_type (objfile, bits, name, format);
17838 else
17839 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17840
17841 return type;
17842 }
17843
17844 /* Find a representation of a given base type and install
17845 it in the TYPE field of the die. */
17846
17847 static struct type *
17848 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17849 {
17850 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17851 struct type *type;
17852 struct attribute *attr;
17853 int encoding = 0, bits = 0;
17854 const char *name;
17855
17856 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17857 if (attr)
17858 {
17859 encoding = DW_UNSND (attr);
17860 }
17861 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17862 if (attr)
17863 {
17864 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17865 }
17866 name = dwarf2_name (die, cu);
17867 if (!name)
17868 {
17869 complaint (&symfile_complaints,
17870 _("DW_AT_name missing from DW_TAG_base_type"));
17871 }
17872
17873 switch (encoding)
17874 {
17875 case DW_ATE_address:
17876 /* Turn DW_ATE_address into a void * pointer. */
17877 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17878 type = init_pointer_type (objfile, bits, name, type);
17879 break;
17880 case DW_ATE_boolean:
17881 type = init_boolean_type (objfile, bits, 1, name);
17882 break;
17883 case DW_ATE_complex_float:
17884 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17885 type = init_complex_type (objfile, name, type);
17886 break;
17887 case DW_ATE_decimal_float:
17888 type = init_decfloat_type (objfile, bits, name);
17889 break;
17890 case DW_ATE_float:
17891 type = dwarf2_init_float_type (objfile, bits, name, name);
17892 break;
17893 case DW_ATE_signed:
17894 type = init_integer_type (objfile, bits, 0, name);
17895 break;
17896 case DW_ATE_unsigned:
17897 if (cu->language == language_fortran
17898 && name
17899 && startswith (name, "character("))
17900 type = init_character_type (objfile, bits, 1, name);
17901 else
17902 type = init_integer_type (objfile, bits, 1, name);
17903 break;
17904 case DW_ATE_signed_char:
17905 if (cu->language == language_ada || cu->language == language_m2
17906 || cu->language == language_pascal
17907 || cu->language == language_fortran)
17908 type = init_character_type (objfile, bits, 0, name);
17909 else
17910 type = init_integer_type (objfile, bits, 0, name);
17911 break;
17912 case DW_ATE_unsigned_char:
17913 if (cu->language == language_ada || cu->language == language_m2
17914 || cu->language == language_pascal
17915 || cu->language == language_fortran
17916 || cu->language == language_rust)
17917 type = init_character_type (objfile, bits, 1, name);
17918 else
17919 type = init_integer_type (objfile, bits, 1, name);
17920 break;
17921 case DW_ATE_UTF:
17922 {
17923 gdbarch *arch = get_objfile_arch (objfile);
17924
17925 if (bits == 16)
17926 type = builtin_type (arch)->builtin_char16;
17927 else if (bits == 32)
17928 type = builtin_type (arch)->builtin_char32;
17929 else
17930 {
17931 complaint (&symfile_complaints,
17932 _("unsupported DW_ATE_UTF bit size: '%d'"),
17933 bits);
17934 type = init_integer_type (objfile, bits, 1, name);
17935 }
17936 return set_die_type (die, type, cu);
17937 }
17938 break;
17939
17940 default:
17941 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17942 dwarf_type_encoding_name (encoding));
17943 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17944 break;
17945 }
17946
17947 if (name && strcmp (name, "char") == 0)
17948 TYPE_NOSIGN (type) = 1;
17949
17950 return set_die_type (die, type, cu);
17951 }
17952
17953 /* Parse dwarf attribute if it's a block, reference or constant and put the
17954 resulting value of the attribute into struct bound_prop.
17955 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17956
17957 static int
17958 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17959 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17960 {
17961 struct dwarf2_property_baton *baton;
17962 struct obstack *obstack
17963 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17964
17965 if (attr == NULL || prop == NULL)
17966 return 0;
17967
17968 if (attr_form_is_block (attr))
17969 {
17970 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17971 baton->referenced_type = NULL;
17972 baton->locexpr.per_cu = cu->per_cu;
17973 baton->locexpr.size = DW_BLOCK (attr)->size;
17974 baton->locexpr.data = DW_BLOCK (attr)->data;
17975 prop->data.baton = baton;
17976 prop->kind = PROP_LOCEXPR;
17977 gdb_assert (prop->data.baton != NULL);
17978 }
17979 else if (attr_form_is_ref (attr))
17980 {
17981 struct dwarf2_cu *target_cu = cu;
17982 struct die_info *target_die;
17983 struct attribute *target_attr;
17984
17985 target_die = follow_die_ref (die, attr, &target_cu);
17986 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17987 if (target_attr == NULL)
17988 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17989 target_cu);
17990 if (target_attr == NULL)
17991 return 0;
17992
17993 switch (target_attr->name)
17994 {
17995 case DW_AT_location:
17996 if (attr_form_is_section_offset (target_attr))
17997 {
17998 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17999 baton->referenced_type = die_type (target_die, target_cu);
18000 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18001 prop->data.baton = baton;
18002 prop->kind = PROP_LOCLIST;
18003 gdb_assert (prop->data.baton != NULL);
18004 }
18005 else if (attr_form_is_block (target_attr))
18006 {
18007 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18008 baton->referenced_type = die_type (target_die, target_cu);
18009 baton->locexpr.per_cu = cu->per_cu;
18010 baton->locexpr.size = DW_BLOCK (target_attr)->size;
18011 baton->locexpr.data = DW_BLOCK (target_attr)->data;
18012 prop->data.baton = baton;
18013 prop->kind = PROP_LOCEXPR;
18014 gdb_assert (prop->data.baton != NULL);
18015 }
18016 else
18017 {
18018 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18019 "dynamic property");
18020 return 0;
18021 }
18022 break;
18023 case DW_AT_data_member_location:
18024 {
18025 LONGEST offset;
18026
18027 if (!handle_data_member_location (target_die, target_cu,
18028 &offset))
18029 return 0;
18030
18031 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18032 baton->referenced_type = read_type_die (target_die->parent,
18033 target_cu);
18034 baton->offset_info.offset = offset;
18035 baton->offset_info.type = die_type (target_die, target_cu);
18036 prop->data.baton = baton;
18037 prop->kind = PROP_ADDR_OFFSET;
18038 break;
18039 }
18040 }
18041 }
18042 else if (attr_form_is_constant (attr))
18043 {
18044 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
18045 prop->kind = PROP_CONST;
18046 }
18047 else
18048 {
18049 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18050 dwarf2_name (die, cu));
18051 return 0;
18052 }
18053
18054 return 1;
18055 }
18056
18057 /* Read the given DW_AT_subrange DIE. */
18058
18059 static struct type *
18060 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18061 {
18062 struct type *base_type, *orig_base_type;
18063 struct type *range_type;
18064 struct attribute *attr;
18065 struct dynamic_prop low, high;
18066 int low_default_is_valid;
18067 int high_bound_is_count = 0;
18068 const char *name;
18069 LONGEST negative_mask;
18070
18071 orig_base_type = die_type (die, cu);
18072 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18073 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18074 creating the range type, but we use the result of check_typedef
18075 when examining properties of the type. */
18076 base_type = check_typedef (orig_base_type);
18077
18078 /* The die_type call above may have already set the type for this DIE. */
18079 range_type = get_die_type (die, cu);
18080 if (range_type)
18081 return range_type;
18082
18083 low.kind = PROP_CONST;
18084 high.kind = PROP_CONST;
18085 high.data.const_val = 0;
18086
18087 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18088 omitting DW_AT_lower_bound. */
18089 switch (cu->language)
18090 {
18091 case language_c:
18092 case language_cplus:
18093 low.data.const_val = 0;
18094 low_default_is_valid = 1;
18095 break;
18096 case language_fortran:
18097 low.data.const_val = 1;
18098 low_default_is_valid = 1;
18099 break;
18100 case language_d:
18101 case language_objc:
18102 case language_rust:
18103 low.data.const_val = 0;
18104 low_default_is_valid = (cu->header.version >= 4);
18105 break;
18106 case language_ada:
18107 case language_m2:
18108 case language_pascal:
18109 low.data.const_val = 1;
18110 low_default_is_valid = (cu->header.version >= 4);
18111 break;
18112 default:
18113 low.data.const_val = 0;
18114 low_default_is_valid = 0;
18115 break;
18116 }
18117
18118 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18119 if (attr)
18120 attr_to_dynamic_prop (attr, die, cu, &low);
18121 else if (!low_default_is_valid)
18122 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
18123 "- DIE at %s [in module %s]"),
18124 sect_offset_str (die->sect_off),
18125 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18126
18127 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
18128 if (!attr_to_dynamic_prop (attr, die, cu, &high))
18129 {
18130 attr = dwarf2_attr (die, DW_AT_count, cu);
18131 if (attr_to_dynamic_prop (attr, die, cu, &high))
18132 {
18133 /* If bounds are constant do the final calculation here. */
18134 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18135 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18136 else
18137 high_bound_is_count = 1;
18138 }
18139 }
18140
18141 /* Dwarf-2 specifications explicitly allows to create subrange types
18142 without specifying a base type.
18143 In that case, the base type must be set to the type of
18144 the lower bound, upper bound or count, in that order, if any of these
18145 three attributes references an object that has a type.
18146 If no base type is found, the Dwarf-2 specifications say that
18147 a signed integer type of size equal to the size of an address should
18148 be used.
18149 For the following C code: `extern char gdb_int [];'
18150 GCC produces an empty range DIE.
18151 FIXME: muller/2010-05-28: Possible references to object for low bound,
18152 high bound or count are not yet handled by this code. */
18153 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
18154 {
18155 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18156 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18157 int addr_size = gdbarch_addr_bit (gdbarch) /8;
18158 struct type *int_type = objfile_type (objfile)->builtin_int;
18159
18160 /* Test "int", "long int", and "long long int" objfile types,
18161 and select the first one having a size above or equal to the
18162 architecture address size. */
18163 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18164 base_type = int_type;
18165 else
18166 {
18167 int_type = objfile_type (objfile)->builtin_long;
18168 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18169 base_type = int_type;
18170 else
18171 {
18172 int_type = objfile_type (objfile)->builtin_long_long;
18173 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18174 base_type = int_type;
18175 }
18176 }
18177 }
18178
18179 /* Normally, the DWARF producers are expected to use a signed
18180 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18181 But this is unfortunately not always the case, as witnessed
18182 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18183 is used instead. To work around that ambiguity, we treat
18184 the bounds as signed, and thus sign-extend their values, when
18185 the base type is signed. */
18186 negative_mask =
18187 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18188 if (low.kind == PROP_CONST
18189 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18190 low.data.const_val |= negative_mask;
18191 if (high.kind == PROP_CONST
18192 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18193 high.data.const_val |= negative_mask;
18194
18195 range_type = create_range_type (NULL, orig_base_type, &low, &high);
18196
18197 if (high_bound_is_count)
18198 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18199
18200 /* Ada expects an empty array on no boundary attributes. */
18201 if (attr == NULL && cu->language != language_ada)
18202 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
18203
18204 name = dwarf2_name (die, cu);
18205 if (name)
18206 TYPE_NAME (range_type) = name;
18207
18208 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18209 if (attr)
18210 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18211
18212 set_die_type (die, range_type, cu);
18213
18214 /* set_die_type should be already done. */
18215 set_descriptive_type (range_type, die, cu);
18216
18217 return range_type;
18218 }
18219
18220 static struct type *
18221 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18222 {
18223 struct type *type;
18224
18225 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18226 NULL);
18227 TYPE_NAME (type) = dwarf2_name (die, cu);
18228
18229 /* In Ada, an unspecified type is typically used when the description
18230 of the type is defered to a different unit. When encountering
18231 such a type, we treat it as a stub, and try to resolve it later on,
18232 when needed. */
18233 if (cu->language == language_ada)
18234 TYPE_STUB (type) = 1;
18235
18236 return set_die_type (die, type, cu);
18237 }
18238
18239 /* Read a single die and all its descendents. Set the die's sibling
18240 field to NULL; set other fields in the die correctly, and set all
18241 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18242 location of the info_ptr after reading all of those dies. PARENT
18243 is the parent of the die in question. */
18244
18245 static struct die_info *
18246 read_die_and_children (const struct die_reader_specs *reader,
18247 const gdb_byte *info_ptr,
18248 const gdb_byte **new_info_ptr,
18249 struct die_info *parent)
18250 {
18251 struct die_info *die;
18252 const gdb_byte *cur_ptr;
18253 int has_children;
18254
18255 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18256 if (die == NULL)
18257 {
18258 *new_info_ptr = cur_ptr;
18259 return NULL;
18260 }
18261 store_in_ref_table (die, reader->cu);
18262
18263 if (has_children)
18264 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18265 else
18266 {
18267 die->child = NULL;
18268 *new_info_ptr = cur_ptr;
18269 }
18270
18271 die->sibling = NULL;
18272 die->parent = parent;
18273 return die;
18274 }
18275
18276 /* Read a die, all of its descendents, and all of its siblings; set
18277 all of the fields of all of the dies correctly. Arguments are as
18278 in read_die_and_children. */
18279
18280 static struct die_info *
18281 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18282 const gdb_byte *info_ptr,
18283 const gdb_byte **new_info_ptr,
18284 struct die_info *parent)
18285 {
18286 struct die_info *first_die, *last_sibling;
18287 const gdb_byte *cur_ptr;
18288
18289 cur_ptr = info_ptr;
18290 first_die = last_sibling = NULL;
18291
18292 while (1)
18293 {
18294 struct die_info *die
18295 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18296
18297 if (die == NULL)
18298 {
18299 *new_info_ptr = cur_ptr;
18300 return first_die;
18301 }
18302
18303 if (!first_die)
18304 first_die = die;
18305 else
18306 last_sibling->sibling = die;
18307
18308 last_sibling = die;
18309 }
18310 }
18311
18312 /* Read a die, all of its descendents, and all of its siblings; set
18313 all of the fields of all of the dies correctly. Arguments are as
18314 in read_die_and_children.
18315 This the main entry point for reading a DIE and all its children. */
18316
18317 static struct die_info *
18318 read_die_and_siblings (const struct die_reader_specs *reader,
18319 const gdb_byte *info_ptr,
18320 const gdb_byte **new_info_ptr,
18321 struct die_info *parent)
18322 {
18323 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18324 new_info_ptr, parent);
18325
18326 if (dwarf_die_debug)
18327 {
18328 fprintf_unfiltered (gdb_stdlog,
18329 "Read die from %s@0x%x of %s:\n",
18330 get_section_name (reader->die_section),
18331 (unsigned) (info_ptr - reader->die_section->buffer),
18332 bfd_get_filename (reader->abfd));
18333 dump_die (die, dwarf_die_debug);
18334 }
18335
18336 return die;
18337 }
18338
18339 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18340 attributes.
18341 The caller is responsible for filling in the extra attributes
18342 and updating (*DIEP)->num_attrs.
18343 Set DIEP to point to a newly allocated die with its information,
18344 except for its child, sibling, and parent fields.
18345 Set HAS_CHILDREN to tell whether the die has children or not. */
18346
18347 static const gdb_byte *
18348 read_full_die_1 (const struct die_reader_specs *reader,
18349 struct die_info **diep, const gdb_byte *info_ptr,
18350 int *has_children, int num_extra_attrs)
18351 {
18352 unsigned int abbrev_number, bytes_read, i;
18353 struct abbrev_info *abbrev;
18354 struct die_info *die;
18355 struct dwarf2_cu *cu = reader->cu;
18356 bfd *abfd = reader->abfd;
18357
18358 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18359 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18360 info_ptr += bytes_read;
18361 if (!abbrev_number)
18362 {
18363 *diep = NULL;
18364 *has_children = 0;
18365 return info_ptr;
18366 }
18367
18368 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18369 if (!abbrev)
18370 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18371 abbrev_number,
18372 bfd_get_filename (abfd));
18373
18374 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18375 die->sect_off = sect_off;
18376 die->tag = abbrev->tag;
18377 die->abbrev = abbrev_number;
18378
18379 /* Make the result usable.
18380 The caller needs to update num_attrs after adding the extra
18381 attributes. */
18382 die->num_attrs = abbrev->num_attrs;
18383
18384 for (i = 0; i < abbrev->num_attrs; ++i)
18385 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18386 info_ptr);
18387
18388 *diep = die;
18389 *has_children = abbrev->has_children;
18390 return info_ptr;
18391 }
18392
18393 /* Read a die and all its attributes.
18394 Set DIEP to point to a newly allocated die with its information,
18395 except for its child, sibling, and parent fields.
18396 Set HAS_CHILDREN to tell whether the die has children or not. */
18397
18398 static const gdb_byte *
18399 read_full_die (const struct die_reader_specs *reader,
18400 struct die_info **diep, const gdb_byte *info_ptr,
18401 int *has_children)
18402 {
18403 const gdb_byte *result;
18404
18405 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18406
18407 if (dwarf_die_debug)
18408 {
18409 fprintf_unfiltered (gdb_stdlog,
18410 "Read die from %s@0x%x of %s:\n",
18411 get_section_name (reader->die_section),
18412 (unsigned) (info_ptr - reader->die_section->buffer),
18413 bfd_get_filename (reader->abfd));
18414 dump_die (*diep, dwarf_die_debug);
18415 }
18416
18417 return result;
18418 }
18419 \f
18420 /* Abbreviation tables.
18421
18422 In DWARF version 2, the description of the debugging information is
18423 stored in a separate .debug_abbrev section. Before we read any
18424 dies from a section we read in all abbreviations and install them
18425 in a hash table. */
18426
18427 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18428
18429 struct abbrev_info *
18430 abbrev_table::alloc_abbrev ()
18431 {
18432 struct abbrev_info *abbrev;
18433
18434 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18435 memset (abbrev, 0, sizeof (struct abbrev_info));
18436
18437 return abbrev;
18438 }
18439
18440 /* Add an abbreviation to the table. */
18441
18442 void
18443 abbrev_table::add_abbrev (unsigned int abbrev_number,
18444 struct abbrev_info *abbrev)
18445 {
18446 unsigned int hash_number;
18447
18448 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18449 abbrev->next = m_abbrevs[hash_number];
18450 m_abbrevs[hash_number] = abbrev;
18451 }
18452
18453 /* Look up an abbrev in the table.
18454 Returns NULL if the abbrev is not found. */
18455
18456 struct abbrev_info *
18457 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18458 {
18459 unsigned int hash_number;
18460 struct abbrev_info *abbrev;
18461
18462 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18463 abbrev = m_abbrevs[hash_number];
18464
18465 while (abbrev)
18466 {
18467 if (abbrev->number == abbrev_number)
18468 return abbrev;
18469 abbrev = abbrev->next;
18470 }
18471 return NULL;
18472 }
18473
18474 /* Read in an abbrev table. */
18475
18476 static abbrev_table_up
18477 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18478 struct dwarf2_section_info *section,
18479 sect_offset sect_off)
18480 {
18481 struct objfile *objfile = dwarf2_per_objfile->objfile;
18482 bfd *abfd = get_section_bfd_owner (section);
18483 const gdb_byte *abbrev_ptr;
18484 struct abbrev_info *cur_abbrev;
18485 unsigned int abbrev_number, bytes_read, abbrev_name;
18486 unsigned int abbrev_form;
18487 struct attr_abbrev *cur_attrs;
18488 unsigned int allocated_attrs;
18489
18490 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18491
18492 dwarf2_read_section (objfile, section);
18493 abbrev_ptr = section->buffer + to_underlying (sect_off);
18494 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18495 abbrev_ptr += bytes_read;
18496
18497 allocated_attrs = ATTR_ALLOC_CHUNK;
18498 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18499
18500 /* Loop until we reach an abbrev number of 0. */
18501 while (abbrev_number)
18502 {
18503 cur_abbrev = abbrev_table->alloc_abbrev ();
18504
18505 /* read in abbrev header */
18506 cur_abbrev->number = abbrev_number;
18507 cur_abbrev->tag
18508 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18509 abbrev_ptr += bytes_read;
18510 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18511 abbrev_ptr += 1;
18512
18513 /* now read in declarations */
18514 for (;;)
18515 {
18516 LONGEST implicit_const;
18517
18518 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18519 abbrev_ptr += bytes_read;
18520 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18521 abbrev_ptr += bytes_read;
18522 if (abbrev_form == DW_FORM_implicit_const)
18523 {
18524 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18525 &bytes_read);
18526 abbrev_ptr += bytes_read;
18527 }
18528 else
18529 {
18530 /* Initialize it due to a false compiler warning. */
18531 implicit_const = -1;
18532 }
18533
18534 if (abbrev_name == 0)
18535 break;
18536
18537 if (cur_abbrev->num_attrs == allocated_attrs)
18538 {
18539 allocated_attrs += ATTR_ALLOC_CHUNK;
18540 cur_attrs
18541 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18542 }
18543
18544 cur_attrs[cur_abbrev->num_attrs].name
18545 = (enum dwarf_attribute) abbrev_name;
18546 cur_attrs[cur_abbrev->num_attrs].form
18547 = (enum dwarf_form) abbrev_form;
18548 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18549 ++cur_abbrev->num_attrs;
18550 }
18551
18552 cur_abbrev->attrs =
18553 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18554 cur_abbrev->num_attrs);
18555 memcpy (cur_abbrev->attrs, cur_attrs,
18556 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18557
18558 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18559
18560 /* Get next abbreviation.
18561 Under Irix6 the abbreviations for a compilation unit are not
18562 always properly terminated with an abbrev number of 0.
18563 Exit loop if we encounter an abbreviation which we have
18564 already read (which means we are about to read the abbreviations
18565 for the next compile unit) or if the end of the abbreviation
18566 table is reached. */
18567 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18568 break;
18569 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18570 abbrev_ptr += bytes_read;
18571 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18572 break;
18573 }
18574
18575 xfree (cur_attrs);
18576 return abbrev_table;
18577 }
18578
18579 /* Returns nonzero if TAG represents a type that we might generate a partial
18580 symbol for. */
18581
18582 static int
18583 is_type_tag_for_partial (int tag)
18584 {
18585 switch (tag)
18586 {
18587 #if 0
18588 /* Some types that would be reasonable to generate partial symbols for,
18589 that we don't at present. */
18590 case DW_TAG_array_type:
18591 case DW_TAG_file_type:
18592 case DW_TAG_ptr_to_member_type:
18593 case DW_TAG_set_type:
18594 case DW_TAG_string_type:
18595 case DW_TAG_subroutine_type:
18596 #endif
18597 case DW_TAG_base_type:
18598 case DW_TAG_class_type:
18599 case DW_TAG_interface_type:
18600 case DW_TAG_enumeration_type:
18601 case DW_TAG_structure_type:
18602 case DW_TAG_subrange_type:
18603 case DW_TAG_typedef:
18604 case DW_TAG_union_type:
18605 return 1;
18606 default:
18607 return 0;
18608 }
18609 }
18610
18611 /* Load all DIEs that are interesting for partial symbols into memory. */
18612
18613 static struct partial_die_info *
18614 load_partial_dies (const struct die_reader_specs *reader,
18615 const gdb_byte *info_ptr, int building_psymtab)
18616 {
18617 struct dwarf2_cu *cu = reader->cu;
18618 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18619 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18620 unsigned int bytes_read;
18621 unsigned int load_all = 0;
18622 int nesting_level = 1;
18623
18624 parent_die = NULL;
18625 last_die = NULL;
18626
18627 gdb_assert (cu->per_cu != NULL);
18628 if (cu->per_cu->load_all_dies)
18629 load_all = 1;
18630
18631 cu->partial_dies
18632 = htab_create_alloc_ex (cu->header.length / 12,
18633 partial_die_hash,
18634 partial_die_eq,
18635 NULL,
18636 &cu->comp_unit_obstack,
18637 hashtab_obstack_allocate,
18638 dummy_obstack_deallocate);
18639
18640 while (1)
18641 {
18642 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18643
18644 /* A NULL abbrev means the end of a series of children. */
18645 if (abbrev == NULL)
18646 {
18647 if (--nesting_level == 0)
18648 return first_die;
18649
18650 info_ptr += bytes_read;
18651 last_die = parent_die;
18652 parent_die = parent_die->die_parent;
18653 continue;
18654 }
18655
18656 /* Check for template arguments. We never save these; if
18657 they're seen, we just mark the parent, and go on our way. */
18658 if (parent_die != NULL
18659 && cu->language == language_cplus
18660 && (abbrev->tag == DW_TAG_template_type_param
18661 || abbrev->tag == DW_TAG_template_value_param))
18662 {
18663 parent_die->has_template_arguments = 1;
18664
18665 if (!load_all)
18666 {
18667 /* We don't need a partial DIE for the template argument. */
18668 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18669 continue;
18670 }
18671 }
18672
18673 /* We only recurse into c++ subprograms looking for template arguments.
18674 Skip their other children. */
18675 if (!load_all
18676 && cu->language == language_cplus
18677 && parent_die != NULL
18678 && parent_die->tag == DW_TAG_subprogram)
18679 {
18680 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18681 continue;
18682 }
18683
18684 /* Check whether this DIE is interesting enough to save. Normally
18685 we would not be interested in members here, but there may be
18686 later variables referencing them via DW_AT_specification (for
18687 static members). */
18688 if (!load_all
18689 && !is_type_tag_for_partial (abbrev->tag)
18690 && abbrev->tag != DW_TAG_constant
18691 && abbrev->tag != DW_TAG_enumerator
18692 && abbrev->tag != DW_TAG_subprogram
18693 && abbrev->tag != DW_TAG_inlined_subroutine
18694 && abbrev->tag != DW_TAG_lexical_block
18695 && abbrev->tag != DW_TAG_variable
18696 && abbrev->tag != DW_TAG_namespace
18697 && abbrev->tag != DW_TAG_module
18698 && abbrev->tag != DW_TAG_member
18699 && abbrev->tag != DW_TAG_imported_unit
18700 && abbrev->tag != DW_TAG_imported_declaration)
18701 {
18702 /* Otherwise we skip to the next sibling, if any. */
18703 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18704 continue;
18705 }
18706
18707 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18708 abbrev);
18709
18710 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18711
18712 /* This two-pass algorithm for processing partial symbols has a
18713 high cost in cache pressure. Thus, handle some simple cases
18714 here which cover the majority of C partial symbols. DIEs
18715 which neither have specification tags in them, nor could have
18716 specification tags elsewhere pointing at them, can simply be
18717 processed and discarded.
18718
18719 This segment is also optional; scan_partial_symbols and
18720 add_partial_symbol will handle these DIEs if we chain
18721 them in normally. When compilers which do not emit large
18722 quantities of duplicate debug information are more common,
18723 this code can probably be removed. */
18724
18725 /* Any complete simple types at the top level (pretty much all
18726 of them, for a language without namespaces), can be processed
18727 directly. */
18728 if (parent_die == NULL
18729 && pdi.has_specification == 0
18730 && pdi.is_declaration == 0
18731 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18732 || pdi.tag == DW_TAG_base_type
18733 || pdi.tag == DW_TAG_subrange_type))
18734 {
18735 if (building_psymtab && pdi.name != NULL)
18736 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18737 VAR_DOMAIN, LOC_TYPEDEF,
18738 &objfile->static_psymbols,
18739 0, cu->language, objfile);
18740 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18741 continue;
18742 }
18743
18744 /* The exception for DW_TAG_typedef with has_children above is
18745 a workaround of GCC PR debug/47510. In the case of this complaint
18746 type_name_no_tag_or_error will error on such types later.
18747
18748 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18749 it could not find the child DIEs referenced later, this is checked
18750 above. In correct DWARF DW_TAG_typedef should have no children. */
18751
18752 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18753 complaint (&symfile_complaints,
18754 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18755 "- DIE at %s [in module %s]"),
18756 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18757
18758 /* If we're at the second level, and we're an enumerator, and
18759 our parent has no specification (meaning possibly lives in a
18760 namespace elsewhere), then we can add the partial symbol now
18761 instead of queueing it. */
18762 if (pdi.tag == DW_TAG_enumerator
18763 && parent_die != NULL
18764 && parent_die->die_parent == NULL
18765 && parent_die->tag == DW_TAG_enumeration_type
18766 && parent_die->has_specification == 0)
18767 {
18768 if (pdi.name == NULL)
18769 complaint (&symfile_complaints,
18770 _("malformed enumerator DIE ignored"));
18771 else if (building_psymtab)
18772 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18773 VAR_DOMAIN, LOC_CONST,
18774 cu->language == language_cplus
18775 ? &objfile->global_psymbols
18776 : &objfile->static_psymbols,
18777 0, cu->language, objfile);
18778
18779 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18780 continue;
18781 }
18782
18783 struct partial_die_info *part_die
18784 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18785
18786 /* We'll save this DIE so link it in. */
18787 part_die->die_parent = parent_die;
18788 part_die->die_sibling = NULL;
18789 part_die->die_child = NULL;
18790
18791 if (last_die && last_die == parent_die)
18792 last_die->die_child = part_die;
18793 else if (last_die)
18794 last_die->die_sibling = part_die;
18795
18796 last_die = part_die;
18797
18798 if (first_die == NULL)
18799 first_die = part_die;
18800
18801 /* Maybe add the DIE to the hash table. Not all DIEs that we
18802 find interesting need to be in the hash table, because we
18803 also have the parent/sibling/child chains; only those that we
18804 might refer to by offset later during partial symbol reading.
18805
18806 For now this means things that might have be the target of a
18807 DW_AT_specification, DW_AT_abstract_origin, or
18808 DW_AT_extension. DW_AT_extension will refer only to
18809 namespaces; DW_AT_abstract_origin refers to functions (and
18810 many things under the function DIE, but we do not recurse
18811 into function DIEs during partial symbol reading) and
18812 possibly variables as well; DW_AT_specification refers to
18813 declarations. Declarations ought to have the DW_AT_declaration
18814 flag. It happens that GCC forgets to put it in sometimes, but
18815 only for functions, not for types.
18816
18817 Adding more things than necessary to the hash table is harmless
18818 except for the performance cost. Adding too few will result in
18819 wasted time in find_partial_die, when we reread the compilation
18820 unit with load_all_dies set. */
18821
18822 if (load_all
18823 || abbrev->tag == DW_TAG_constant
18824 || abbrev->tag == DW_TAG_subprogram
18825 || abbrev->tag == DW_TAG_variable
18826 || abbrev->tag == DW_TAG_namespace
18827 || part_die->is_declaration)
18828 {
18829 void **slot;
18830
18831 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18832 to_underlying (part_die->sect_off),
18833 INSERT);
18834 *slot = part_die;
18835 }
18836
18837 /* For some DIEs we want to follow their children (if any). For C
18838 we have no reason to follow the children of structures; for other
18839 languages we have to, so that we can get at method physnames
18840 to infer fully qualified class names, for DW_AT_specification,
18841 and for C++ template arguments. For C++, we also look one level
18842 inside functions to find template arguments (if the name of the
18843 function does not already contain the template arguments).
18844
18845 For Ada, we need to scan the children of subprograms and lexical
18846 blocks as well because Ada allows the definition of nested
18847 entities that could be interesting for the debugger, such as
18848 nested subprograms for instance. */
18849 if (last_die->has_children
18850 && (load_all
18851 || last_die->tag == DW_TAG_namespace
18852 || last_die->tag == DW_TAG_module
18853 || last_die->tag == DW_TAG_enumeration_type
18854 || (cu->language == language_cplus
18855 && last_die->tag == DW_TAG_subprogram
18856 && (last_die->name == NULL
18857 || strchr (last_die->name, '<') == NULL))
18858 || (cu->language != language_c
18859 && (last_die->tag == DW_TAG_class_type
18860 || last_die->tag == DW_TAG_interface_type
18861 || last_die->tag == DW_TAG_structure_type
18862 || last_die->tag == DW_TAG_union_type))
18863 || (cu->language == language_ada
18864 && (last_die->tag == DW_TAG_subprogram
18865 || last_die->tag == DW_TAG_lexical_block))))
18866 {
18867 nesting_level++;
18868 parent_die = last_die;
18869 continue;
18870 }
18871
18872 /* Otherwise we skip to the next sibling, if any. */
18873 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18874
18875 /* Back to the top, do it again. */
18876 }
18877 }
18878
18879 partial_die_info::partial_die_info (sect_offset sect_off_,
18880 struct abbrev_info *abbrev)
18881 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18882 {
18883 }
18884
18885 /* Read a minimal amount of information into the minimal die structure.
18886 INFO_PTR should point just after the initial uleb128 of a DIE. */
18887
18888 const gdb_byte *
18889 partial_die_info::read (const struct die_reader_specs *reader,
18890 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18891 {
18892 struct dwarf2_cu *cu = reader->cu;
18893 struct dwarf2_per_objfile *dwarf2_per_objfile
18894 = cu->per_cu->dwarf2_per_objfile;
18895 unsigned int i;
18896 int has_low_pc_attr = 0;
18897 int has_high_pc_attr = 0;
18898 int high_pc_relative = 0;
18899
18900 for (i = 0; i < abbrev.num_attrs; ++i)
18901 {
18902 struct attribute attr;
18903
18904 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18905
18906 /* Store the data if it is of an attribute we want to keep in a
18907 partial symbol table. */
18908 switch (attr.name)
18909 {
18910 case DW_AT_name:
18911 switch (tag)
18912 {
18913 case DW_TAG_compile_unit:
18914 case DW_TAG_partial_unit:
18915 case DW_TAG_type_unit:
18916 /* Compilation units have a DW_AT_name that is a filename, not
18917 a source language identifier. */
18918 case DW_TAG_enumeration_type:
18919 case DW_TAG_enumerator:
18920 /* These tags always have simple identifiers already; no need
18921 to canonicalize them. */
18922 name = DW_STRING (&attr);
18923 break;
18924 default:
18925 {
18926 struct objfile *objfile = dwarf2_per_objfile->objfile;
18927
18928 name
18929 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18930 &objfile->per_bfd->storage_obstack);
18931 }
18932 break;
18933 }
18934 break;
18935 case DW_AT_linkage_name:
18936 case DW_AT_MIPS_linkage_name:
18937 /* Note that both forms of linkage name might appear. We
18938 assume they will be the same, and we only store the last
18939 one we see. */
18940 if (cu->language == language_ada)
18941 name = DW_STRING (&attr);
18942 linkage_name = DW_STRING (&attr);
18943 break;
18944 case DW_AT_low_pc:
18945 has_low_pc_attr = 1;
18946 lowpc = attr_value_as_address (&attr);
18947 break;
18948 case DW_AT_high_pc:
18949 has_high_pc_attr = 1;
18950 highpc = attr_value_as_address (&attr);
18951 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18952 high_pc_relative = 1;
18953 break;
18954 case DW_AT_location:
18955 /* Support the .debug_loc offsets. */
18956 if (attr_form_is_block (&attr))
18957 {
18958 d.locdesc = DW_BLOCK (&attr);
18959 }
18960 else if (attr_form_is_section_offset (&attr))
18961 {
18962 dwarf2_complex_location_expr_complaint ();
18963 }
18964 else
18965 {
18966 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18967 "partial symbol information");
18968 }
18969 break;
18970 case DW_AT_external:
18971 is_external = DW_UNSND (&attr);
18972 break;
18973 case DW_AT_declaration:
18974 is_declaration = DW_UNSND (&attr);
18975 break;
18976 case DW_AT_type:
18977 has_type = 1;
18978 break;
18979 case DW_AT_abstract_origin:
18980 case DW_AT_specification:
18981 case DW_AT_extension:
18982 has_specification = 1;
18983 spec_offset = dwarf2_get_ref_die_offset (&attr);
18984 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18985 || cu->per_cu->is_dwz);
18986 break;
18987 case DW_AT_sibling:
18988 /* Ignore absolute siblings, they might point outside of
18989 the current compile unit. */
18990 if (attr.form == DW_FORM_ref_addr)
18991 complaint (&symfile_complaints,
18992 _("ignoring absolute DW_AT_sibling"));
18993 else
18994 {
18995 const gdb_byte *buffer = reader->buffer;
18996 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18997 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18998
18999 if (sibling_ptr < info_ptr)
19000 complaint (&symfile_complaints,
19001 _("DW_AT_sibling points backwards"));
19002 else if (sibling_ptr > reader->buffer_end)
19003 dwarf2_section_buffer_overflow_complaint (reader->die_section);
19004 else
19005 sibling = sibling_ptr;
19006 }
19007 break;
19008 case DW_AT_byte_size:
19009 has_byte_size = 1;
19010 break;
19011 case DW_AT_const_value:
19012 has_const_value = 1;
19013 break;
19014 case DW_AT_calling_convention:
19015 /* DWARF doesn't provide a way to identify a program's source-level
19016 entry point. DW_AT_calling_convention attributes are only meant
19017 to describe functions' calling conventions.
19018
19019 However, because it's a necessary piece of information in
19020 Fortran, and before DWARF 4 DW_CC_program was the only
19021 piece of debugging information whose definition refers to
19022 a 'main program' at all, several compilers marked Fortran
19023 main programs with DW_CC_program --- even when those
19024 functions use the standard calling conventions.
19025
19026 Although DWARF now specifies a way to provide this
19027 information, we support this practice for backward
19028 compatibility. */
19029 if (DW_UNSND (&attr) == DW_CC_program
19030 && cu->language == language_fortran)
19031 main_subprogram = 1;
19032 break;
19033 case DW_AT_inline:
19034 if (DW_UNSND (&attr) == DW_INL_inlined
19035 || DW_UNSND (&attr) == DW_INL_declared_inlined)
19036 may_be_inlined = 1;
19037 break;
19038
19039 case DW_AT_import:
19040 if (tag == DW_TAG_imported_unit)
19041 {
19042 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19043 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19044 || cu->per_cu->is_dwz);
19045 }
19046 break;
19047
19048 case DW_AT_main_subprogram:
19049 main_subprogram = DW_UNSND (&attr);
19050 break;
19051
19052 default:
19053 break;
19054 }
19055 }
19056
19057 if (high_pc_relative)
19058 highpc += lowpc;
19059
19060 if (has_low_pc_attr && has_high_pc_attr)
19061 {
19062 /* When using the GNU linker, .gnu.linkonce. sections are used to
19063 eliminate duplicate copies of functions and vtables and such.
19064 The linker will arbitrarily choose one and discard the others.
19065 The AT_*_pc values for such functions refer to local labels in
19066 these sections. If the section from that file was discarded, the
19067 labels are not in the output, so the relocs get a value of 0.
19068 If this is a discarded function, mark the pc bounds as invalid,
19069 so that GDB will ignore it. */
19070 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
19071 {
19072 struct objfile *objfile = dwarf2_per_objfile->objfile;
19073 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19074
19075 complaint (&symfile_complaints,
19076 _("DW_AT_low_pc %s is zero "
19077 "for DIE at %s [in module %s]"),
19078 paddress (gdbarch, lowpc),
19079 sect_offset_str (sect_off),
19080 objfile_name (objfile));
19081 }
19082 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19083 else if (lowpc >= highpc)
19084 {
19085 struct objfile *objfile = dwarf2_per_objfile->objfile;
19086 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19087
19088 complaint (&symfile_complaints,
19089 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19090 "for DIE at %s [in module %s]"),
19091 paddress (gdbarch, lowpc),
19092 paddress (gdbarch, highpc),
19093 sect_offset_str (sect_off),
19094 objfile_name (objfile));
19095 }
19096 else
19097 has_pc_info = 1;
19098 }
19099
19100 return info_ptr;
19101 }
19102
19103 /* Find a cached partial DIE at OFFSET in CU. */
19104
19105 struct partial_die_info *
19106 dwarf2_cu::find_partial_die (sect_offset sect_off)
19107 {
19108 struct partial_die_info *lookup_die = NULL;
19109 struct partial_die_info part_die (sect_off);
19110
19111 lookup_die = ((struct partial_die_info *)
19112 htab_find_with_hash (partial_dies, &part_die,
19113 to_underlying (sect_off)));
19114
19115 return lookup_die;
19116 }
19117
19118 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19119 except in the case of .debug_types DIEs which do not reference
19120 outside their CU (they do however referencing other types via
19121 DW_FORM_ref_sig8). */
19122
19123 static struct partial_die_info *
19124 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19125 {
19126 struct dwarf2_per_objfile *dwarf2_per_objfile
19127 = cu->per_cu->dwarf2_per_objfile;
19128 struct objfile *objfile = dwarf2_per_objfile->objfile;
19129 struct dwarf2_per_cu_data *per_cu = NULL;
19130 struct partial_die_info *pd = NULL;
19131
19132 if (offset_in_dwz == cu->per_cu->is_dwz
19133 && offset_in_cu_p (&cu->header, sect_off))
19134 {
19135 pd = cu->find_partial_die (sect_off);
19136 if (pd != NULL)
19137 return pd;
19138 /* We missed recording what we needed.
19139 Load all dies and try again. */
19140 per_cu = cu->per_cu;
19141 }
19142 else
19143 {
19144 /* TUs don't reference other CUs/TUs (except via type signatures). */
19145 if (cu->per_cu->is_debug_types)
19146 {
19147 error (_("Dwarf Error: Type Unit at offset %s contains"
19148 " external reference to offset %s [in module %s].\n"),
19149 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19150 bfd_get_filename (objfile->obfd));
19151 }
19152 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19153 dwarf2_per_objfile);
19154
19155 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19156 load_partial_comp_unit (per_cu);
19157
19158 per_cu->cu->last_used = 0;
19159 pd = per_cu->cu->find_partial_die (sect_off);
19160 }
19161
19162 /* If we didn't find it, and not all dies have been loaded,
19163 load them all and try again. */
19164
19165 if (pd == NULL && per_cu->load_all_dies == 0)
19166 {
19167 per_cu->load_all_dies = 1;
19168
19169 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19170 THIS_CU->cu may already be in use. So we can't just free it and
19171 replace its DIEs with the ones we read in. Instead, we leave those
19172 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19173 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19174 set. */
19175 load_partial_comp_unit (per_cu);
19176
19177 pd = per_cu->cu->find_partial_die (sect_off);
19178 }
19179
19180 if (pd == NULL)
19181 internal_error (__FILE__, __LINE__,
19182 _("could not find partial DIE %s "
19183 "in cache [from module %s]\n"),
19184 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19185 return pd;
19186 }
19187
19188 /* See if we can figure out if the class lives in a namespace. We do
19189 this by looking for a member function; its demangled name will
19190 contain namespace info, if there is any. */
19191
19192 static void
19193 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19194 struct dwarf2_cu *cu)
19195 {
19196 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19197 what template types look like, because the demangler
19198 frequently doesn't give the same name as the debug info. We
19199 could fix this by only using the demangled name to get the
19200 prefix (but see comment in read_structure_type). */
19201
19202 struct partial_die_info *real_pdi;
19203 struct partial_die_info *child_pdi;
19204
19205 /* If this DIE (this DIE's specification, if any) has a parent, then
19206 we should not do this. We'll prepend the parent's fully qualified
19207 name when we create the partial symbol. */
19208
19209 real_pdi = struct_pdi;
19210 while (real_pdi->has_specification)
19211 real_pdi = find_partial_die (real_pdi->spec_offset,
19212 real_pdi->spec_is_dwz, cu);
19213
19214 if (real_pdi->die_parent != NULL)
19215 return;
19216
19217 for (child_pdi = struct_pdi->die_child;
19218 child_pdi != NULL;
19219 child_pdi = child_pdi->die_sibling)
19220 {
19221 if (child_pdi->tag == DW_TAG_subprogram
19222 && child_pdi->linkage_name != NULL)
19223 {
19224 char *actual_class_name
19225 = language_class_name_from_physname (cu->language_defn,
19226 child_pdi->linkage_name);
19227 if (actual_class_name != NULL)
19228 {
19229 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19230 struct_pdi->name
19231 = ((const char *)
19232 obstack_copy0 (&objfile->per_bfd->storage_obstack,
19233 actual_class_name,
19234 strlen (actual_class_name)));
19235 xfree (actual_class_name);
19236 }
19237 break;
19238 }
19239 }
19240 }
19241
19242 void
19243 partial_die_info::fixup (struct dwarf2_cu *cu)
19244 {
19245 /* Once we've fixed up a die, there's no point in doing so again.
19246 This also avoids a memory leak if we were to call
19247 guess_partial_die_structure_name multiple times. */
19248 if (fixup_called)
19249 return;
19250
19251 /* If we found a reference attribute and the DIE has no name, try
19252 to find a name in the referred to DIE. */
19253
19254 if (name == NULL && has_specification)
19255 {
19256 struct partial_die_info *spec_die;
19257
19258 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
19259
19260 spec_die->fixup (cu);
19261
19262 if (spec_die->name)
19263 {
19264 name = spec_die->name;
19265
19266 /* Copy DW_AT_external attribute if it is set. */
19267 if (spec_die->is_external)
19268 is_external = spec_die->is_external;
19269 }
19270 }
19271
19272 /* Set default names for some unnamed DIEs. */
19273
19274 if (name == NULL && tag == DW_TAG_namespace)
19275 name = CP_ANONYMOUS_NAMESPACE_STR;
19276
19277 /* If there is no parent die to provide a namespace, and there are
19278 children, see if we can determine the namespace from their linkage
19279 name. */
19280 if (cu->language == language_cplus
19281 && !VEC_empty (dwarf2_section_info_def,
19282 cu->per_cu->dwarf2_per_objfile->types)
19283 && die_parent == NULL
19284 && has_children
19285 && (tag == DW_TAG_class_type
19286 || tag == DW_TAG_structure_type
19287 || tag == DW_TAG_union_type))
19288 guess_partial_die_structure_name (this, cu);
19289
19290 /* GCC might emit a nameless struct or union that has a linkage
19291 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19292 if (name == NULL
19293 && (tag == DW_TAG_class_type
19294 || tag == DW_TAG_interface_type
19295 || tag == DW_TAG_structure_type
19296 || tag == DW_TAG_union_type)
19297 && linkage_name != NULL)
19298 {
19299 char *demangled;
19300
19301 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
19302 if (demangled)
19303 {
19304 const char *base;
19305
19306 /* Strip any leading namespaces/classes, keep only the base name.
19307 DW_AT_name for named DIEs does not contain the prefixes. */
19308 base = strrchr (demangled, ':');
19309 if (base && base > demangled && base[-1] == ':')
19310 base++;
19311 else
19312 base = demangled;
19313
19314 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19315 name
19316 = ((const char *)
19317 obstack_copy0 (&objfile->per_bfd->storage_obstack,
19318 base, strlen (base)));
19319 xfree (demangled);
19320 }
19321 }
19322
19323 fixup_called = 1;
19324 }
19325
19326 /* Read an attribute value described by an attribute form. */
19327
19328 static const gdb_byte *
19329 read_attribute_value (const struct die_reader_specs *reader,
19330 struct attribute *attr, unsigned form,
19331 LONGEST implicit_const, const gdb_byte *info_ptr)
19332 {
19333 struct dwarf2_cu *cu = reader->cu;
19334 struct dwarf2_per_objfile *dwarf2_per_objfile
19335 = cu->per_cu->dwarf2_per_objfile;
19336 struct objfile *objfile = dwarf2_per_objfile->objfile;
19337 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19338 bfd *abfd = reader->abfd;
19339 struct comp_unit_head *cu_header = &cu->header;
19340 unsigned int bytes_read;
19341 struct dwarf_block *blk;
19342
19343 attr->form = (enum dwarf_form) form;
19344 switch (form)
19345 {
19346 case DW_FORM_ref_addr:
19347 if (cu->header.version == 2)
19348 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19349 else
19350 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19351 &cu->header, &bytes_read);
19352 info_ptr += bytes_read;
19353 break;
19354 case DW_FORM_GNU_ref_alt:
19355 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19356 info_ptr += bytes_read;
19357 break;
19358 case DW_FORM_addr:
19359 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19360 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19361 info_ptr += bytes_read;
19362 break;
19363 case DW_FORM_block2:
19364 blk = dwarf_alloc_block (cu);
19365 blk->size = read_2_bytes (abfd, info_ptr);
19366 info_ptr += 2;
19367 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19368 info_ptr += blk->size;
19369 DW_BLOCK (attr) = blk;
19370 break;
19371 case DW_FORM_block4:
19372 blk = dwarf_alloc_block (cu);
19373 blk->size = read_4_bytes (abfd, info_ptr);
19374 info_ptr += 4;
19375 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19376 info_ptr += blk->size;
19377 DW_BLOCK (attr) = blk;
19378 break;
19379 case DW_FORM_data2:
19380 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19381 info_ptr += 2;
19382 break;
19383 case DW_FORM_data4:
19384 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19385 info_ptr += 4;
19386 break;
19387 case DW_FORM_data8:
19388 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19389 info_ptr += 8;
19390 break;
19391 case DW_FORM_data16:
19392 blk = dwarf_alloc_block (cu);
19393 blk->size = 16;
19394 blk->data = read_n_bytes (abfd, info_ptr, 16);
19395 info_ptr += 16;
19396 DW_BLOCK (attr) = blk;
19397 break;
19398 case DW_FORM_sec_offset:
19399 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19400 info_ptr += bytes_read;
19401 break;
19402 case DW_FORM_string:
19403 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19404 DW_STRING_IS_CANONICAL (attr) = 0;
19405 info_ptr += bytes_read;
19406 break;
19407 case DW_FORM_strp:
19408 if (!cu->per_cu->is_dwz)
19409 {
19410 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19411 abfd, info_ptr, cu_header,
19412 &bytes_read);
19413 DW_STRING_IS_CANONICAL (attr) = 0;
19414 info_ptr += bytes_read;
19415 break;
19416 }
19417 /* FALLTHROUGH */
19418 case DW_FORM_line_strp:
19419 if (!cu->per_cu->is_dwz)
19420 {
19421 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19422 abfd, info_ptr,
19423 cu_header, &bytes_read);
19424 DW_STRING_IS_CANONICAL (attr) = 0;
19425 info_ptr += bytes_read;
19426 break;
19427 }
19428 /* FALLTHROUGH */
19429 case DW_FORM_GNU_strp_alt:
19430 {
19431 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19432 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19433 &bytes_read);
19434
19435 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19436 dwz, str_offset);
19437 DW_STRING_IS_CANONICAL (attr) = 0;
19438 info_ptr += bytes_read;
19439 }
19440 break;
19441 case DW_FORM_exprloc:
19442 case DW_FORM_block:
19443 blk = dwarf_alloc_block (cu);
19444 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19445 info_ptr += bytes_read;
19446 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19447 info_ptr += blk->size;
19448 DW_BLOCK (attr) = blk;
19449 break;
19450 case DW_FORM_block1:
19451 blk = dwarf_alloc_block (cu);
19452 blk->size = read_1_byte (abfd, info_ptr);
19453 info_ptr += 1;
19454 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19455 info_ptr += blk->size;
19456 DW_BLOCK (attr) = blk;
19457 break;
19458 case DW_FORM_data1:
19459 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19460 info_ptr += 1;
19461 break;
19462 case DW_FORM_flag:
19463 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19464 info_ptr += 1;
19465 break;
19466 case DW_FORM_flag_present:
19467 DW_UNSND (attr) = 1;
19468 break;
19469 case DW_FORM_sdata:
19470 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19471 info_ptr += bytes_read;
19472 break;
19473 case DW_FORM_udata:
19474 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19475 info_ptr += bytes_read;
19476 break;
19477 case DW_FORM_ref1:
19478 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19479 + read_1_byte (abfd, info_ptr));
19480 info_ptr += 1;
19481 break;
19482 case DW_FORM_ref2:
19483 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19484 + read_2_bytes (abfd, info_ptr));
19485 info_ptr += 2;
19486 break;
19487 case DW_FORM_ref4:
19488 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19489 + read_4_bytes (abfd, info_ptr));
19490 info_ptr += 4;
19491 break;
19492 case DW_FORM_ref8:
19493 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19494 + read_8_bytes (abfd, info_ptr));
19495 info_ptr += 8;
19496 break;
19497 case DW_FORM_ref_sig8:
19498 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19499 info_ptr += 8;
19500 break;
19501 case DW_FORM_ref_udata:
19502 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19503 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19504 info_ptr += bytes_read;
19505 break;
19506 case DW_FORM_indirect:
19507 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19508 info_ptr += bytes_read;
19509 if (form == DW_FORM_implicit_const)
19510 {
19511 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19512 info_ptr += bytes_read;
19513 }
19514 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19515 info_ptr);
19516 break;
19517 case DW_FORM_implicit_const:
19518 DW_SND (attr) = implicit_const;
19519 break;
19520 case DW_FORM_GNU_addr_index:
19521 if (reader->dwo_file == NULL)
19522 {
19523 /* For now flag a hard error.
19524 Later we can turn this into a complaint. */
19525 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19526 dwarf_form_name (form),
19527 bfd_get_filename (abfd));
19528 }
19529 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19530 info_ptr += bytes_read;
19531 break;
19532 case DW_FORM_GNU_str_index:
19533 if (reader->dwo_file == NULL)
19534 {
19535 /* For now flag a hard error.
19536 Later we can turn this into a complaint if warranted. */
19537 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19538 dwarf_form_name (form),
19539 bfd_get_filename (abfd));
19540 }
19541 {
19542 ULONGEST str_index =
19543 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19544
19545 DW_STRING (attr) = read_str_index (reader, str_index);
19546 DW_STRING_IS_CANONICAL (attr) = 0;
19547 info_ptr += bytes_read;
19548 }
19549 break;
19550 default:
19551 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19552 dwarf_form_name (form),
19553 bfd_get_filename (abfd));
19554 }
19555
19556 /* Super hack. */
19557 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19558 attr->form = DW_FORM_GNU_ref_alt;
19559
19560 /* We have seen instances where the compiler tried to emit a byte
19561 size attribute of -1 which ended up being encoded as an unsigned
19562 0xffffffff. Although 0xffffffff is technically a valid size value,
19563 an object of this size seems pretty unlikely so we can relatively
19564 safely treat these cases as if the size attribute was invalid and
19565 treat them as zero by default. */
19566 if (attr->name == DW_AT_byte_size
19567 && form == DW_FORM_data4
19568 && DW_UNSND (attr) >= 0xffffffff)
19569 {
19570 complaint
19571 (&symfile_complaints,
19572 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19573 hex_string (DW_UNSND (attr)));
19574 DW_UNSND (attr) = 0;
19575 }
19576
19577 return info_ptr;
19578 }
19579
19580 /* Read an attribute described by an abbreviated attribute. */
19581
19582 static const gdb_byte *
19583 read_attribute (const struct die_reader_specs *reader,
19584 struct attribute *attr, struct attr_abbrev *abbrev,
19585 const gdb_byte *info_ptr)
19586 {
19587 attr->name = abbrev->name;
19588 return read_attribute_value (reader, attr, abbrev->form,
19589 abbrev->implicit_const, info_ptr);
19590 }
19591
19592 /* Read dwarf information from a buffer. */
19593
19594 static unsigned int
19595 read_1_byte (bfd *abfd, const gdb_byte *buf)
19596 {
19597 return bfd_get_8 (abfd, buf);
19598 }
19599
19600 static int
19601 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19602 {
19603 return bfd_get_signed_8 (abfd, buf);
19604 }
19605
19606 static unsigned int
19607 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19608 {
19609 return bfd_get_16 (abfd, buf);
19610 }
19611
19612 static int
19613 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19614 {
19615 return bfd_get_signed_16 (abfd, buf);
19616 }
19617
19618 static unsigned int
19619 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19620 {
19621 return bfd_get_32 (abfd, buf);
19622 }
19623
19624 static int
19625 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19626 {
19627 return bfd_get_signed_32 (abfd, buf);
19628 }
19629
19630 static ULONGEST
19631 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19632 {
19633 return bfd_get_64 (abfd, buf);
19634 }
19635
19636 static CORE_ADDR
19637 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19638 unsigned int *bytes_read)
19639 {
19640 struct comp_unit_head *cu_header = &cu->header;
19641 CORE_ADDR retval = 0;
19642
19643 if (cu_header->signed_addr_p)
19644 {
19645 switch (cu_header->addr_size)
19646 {
19647 case 2:
19648 retval = bfd_get_signed_16 (abfd, buf);
19649 break;
19650 case 4:
19651 retval = bfd_get_signed_32 (abfd, buf);
19652 break;
19653 case 8:
19654 retval = bfd_get_signed_64 (abfd, buf);
19655 break;
19656 default:
19657 internal_error (__FILE__, __LINE__,
19658 _("read_address: bad switch, signed [in module %s]"),
19659 bfd_get_filename (abfd));
19660 }
19661 }
19662 else
19663 {
19664 switch (cu_header->addr_size)
19665 {
19666 case 2:
19667 retval = bfd_get_16 (abfd, buf);
19668 break;
19669 case 4:
19670 retval = bfd_get_32 (abfd, buf);
19671 break;
19672 case 8:
19673 retval = bfd_get_64 (abfd, buf);
19674 break;
19675 default:
19676 internal_error (__FILE__, __LINE__,
19677 _("read_address: bad switch, "
19678 "unsigned [in module %s]"),
19679 bfd_get_filename (abfd));
19680 }
19681 }
19682
19683 *bytes_read = cu_header->addr_size;
19684 return retval;
19685 }
19686
19687 /* Read the initial length from a section. The (draft) DWARF 3
19688 specification allows the initial length to take up either 4 bytes
19689 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19690 bytes describe the length and all offsets will be 8 bytes in length
19691 instead of 4.
19692
19693 An older, non-standard 64-bit format is also handled by this
19694 function. The older format in question stores the initial length
19695 as an 8-byte quantity without an escape value. Lengths greater
19696 than 2^32 aren't very common which means that the initial 4 bytes
19697 is almost always zero. Since a length value of zero doesn't make
19698 sense for the 32-bit format, this initial zero can be considered to
19699 be an escape value which indicates the presence of the older 64-bit
19700 format. As written, the code can't detect (old format) lengths
19701 greater than 4GB. If it becomes necessary to handle lengths
19702 somewhat larger than 4GB, we could allow other small values (such
19703 as the non-sensical values of 1, 2, and 3) to also be used as
19704 escape values indicating the presence of the old format.
19705
19706 The value returned via bytes_read should be used to increment the
19707 relevant pointer after calling read_initial_length().
19708
19709 [ Note: read_initial_length() and read_offset() are based on the
19710 document entitled "DWARF Debugging Information Format", revision
19711 3, draft 8, dated November 19, 2001. This document was obtained
19712 from:
19713
19714 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19715
19716 This document is only a draft and is subject to change. (So beware.)
19717
19718 Details regarding the older, non-standard 64-bit format were
19719 determined empirically by examining 64-bit ELF files produced by
19720 the SGI toolchain on an IRIX 6.5 machine.
19721
19722 - Kevin, July 16, 2002
19723 ] */
19724
19725 static LONGEST
19726 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19727 {
19728 LONGEST length = bfd_get_32 (abfd, buf);
19729
19730 if (length == 0xffffffff)
19731 {
19732 length = bfd_get_64 (abfd, buf + 4);
19733 *bytes_read = 12;
19734 }
19735 else if (length == 0)
19736 {
19737 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19738 length = bfd_get_64 (abfd, buf);
19739 *bytes_read = 8;
19740 }
19741 else
19742 {
19743 *bytes_read = 4;
19744 }
19745
19746 return length;
19747 }
19748
19749 /* Cover function for read_initial_length.
19750 Returns the length of the object at BUF, and stores the size of the
19751 initial length in *BYTES_READ and stores the size that offsets will be in
19752 *OFFSET_SIZE.
19753 If the initial length size is not equivalent to that specified in
19754 CU_HEADER then issue a complaint.
19755 This is useful when reading non-comp-unit headers. */
19756
19757 static LONGEST
19758 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19759 const struct comp_unit_head *cu_header,
19760 unsigned int *bytes_read,
19761 unsigned int *offset_size)
19762 {
19763 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19764
19765 gdb_assert (cu_header->initial_length_size == 4
19766 || cu_header->initial_length_size == 8
19767 || cu_header->initial_length_size == 12);
19768
19769 if (cu_header->initial_length_size != *bytes_read)
19770 complaint (&symfile_complaints,
19771 _("intermixed 32-bit and 64-bit DWARF sections"));
19772
19773 *offset_size = (*bytes_read == 4) ? 4 : 8;
19774 return length;
19775 }
19776
19777 /* Read an offset from the data stream. The size of the offset is
19778 given by cu_header->offset_size. */
19779
19780 static LONGEST
19781 read_offset (bfd *abfd, const gdb_byte *buf,
19782 const struct comp_unit_head *cu_header,
19783 unsigned int *bytes_read)
19784 {
19785 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19786
19787 *bytes_read = cu_header->offset_size;
19788 return offset;
19789 }
19790
19791 /* Read an offset from the data stream. */
19792
19793 static LONGEST
19794 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19795 {
19796 LONGEST retval = 0;
19797
19798 switch (offset_size)
19799 {
19800 case 4:
19801 retval = bfd_get_32 (abfd, buf);
19802 break;
19803 case 8:
19804 retval = bfd_get_64 (abfd, buf);
19805 break;
19806 default:
19807 internal_error (__FILE__, __LINE__,
19808 _("read_offset_1: bad switch [in module %s]"),
19809 bfd_get_filename (abfd));
19810 }
19811
19812 return retval;
19813 }
19814
19815 static const gdb_byte *
19816 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19817 {
19818 /* If the size of a host char is 8 bits, we can return a pointer
19819 to the buffer, otherwise we have to copy the data to a buffer
19820 allocated on the temporary obstack. */
19821 gdb_assert (HOST_CHAR_BIT == 8);
19822 return buf;
19823 }
19824
19825 static const char *
19826 read_direct_string (bfd *abfd, const gdb_byte *buf,
19827 unsigned int *bytes_read_ptr)
19828 {
19829 /* If the size of a host char is 8 bits, we can return a pointer
19830 to the string, otherwise we have to copy the string to a buffer
19831 allocated on the temporary obstack. */
19832 gdb_assert (HOST_CHAR_BIT == 8);
19833 if (*buf == '\0')
19834 {
19835 *bytes_read_ptr = 1;
19836 return NULL;
19837 }
19838 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19839 return (const char *) buf;
19840 }
19841
19842 /* Return pointer to string at section SECT offset STR_OFFSET with error
19843 reporting strings FORM_NAME and SECT_NAME. */
19844
19845 static const char *
19846 read_indirect_string_at_offset_from (struct objfile *objfile,
19847 bfd *abfd, LONGEST str_offset,
19848 struct dwarf2_section_info *sect,
19849 const char *form_name,
19850 const char *sect_name)
19851 {
19852 dwarf2_read_section (objfile, sect);
19853 if (sect->buffer == NULL)
19854 error (_("%s used without %s section [in module %s]"),
19855 form_name, sect_name, bfd_get_filename (abfd));
19856 if (str_offset >= sect->size)
19857 error (_("%s pointing outside of %s section [in module %s]"),
19858 form_name, sect_name, bfd_get_filename (abfd));
19859 gdb_assert (HOST_CHAR_BIT == 8);
19860 if (sect->buffer[str_offset] == '\0')
19861 return NULL;
19862 return (const char *) (sect->buffer + str_offset);
19863 }
19864
19865 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19866
19867 static const char *
19868 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19869 bfd *abfd, LONGEST str_offset)
19870 {
19871 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19872 abfd, str_offset,
19873 &dwarf2_per_objfile->str,
19874 "DW_FORM_strp", ".debug_str");
19875 }
19876
19877 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19878
19879 static const char *
19880 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19881 bfd *abfd, LONGEST str_offset)
19882 {
19883 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19884 abfd, str_offset,
19885 &dwarf2_per_objfile->line_str,
19886 "DW_FORM_line_strp",
19887 ".debug_line_str");
19888 }
19889
19890 /* Read a string at offset STR_OFFSET in the .debug_str section from
19891 the .dwz file DWZ. Throw an error if the offset is too large. If
19892 the string consists of a single NUL byte, return NULL; otherwise
19893 return a pointer to the string. */
19894
19895 static const char *
19896 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19897 LONGEST str_offset)
19898 {
19899 dwarf2_read_section (objfile, &dwz->str);
19900
19901 if (dwz->str.buffer == NULL)
19902 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19903 "section [in module %s]"),
19904 bfd_get_filename (dwz->dwz_bfd));
19905 if (str_offset >= dwz->str.size)
19906 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19907 ".debug_str section [in module %s]"),
19908 bfd_get_filename (dwz->dwz_bfd));
19909 gdb_assert (HOST_CHAR_BIT == 8);
19910 if (dwz->str.buffer[str_offset] == '\0')
19911 return NULL;
19912 return (const char *) (dwz->str.buffer + str_offset);
19913 }
19914
19915 /* Return pointer to string at .debug_str offset as read from BUF.
19916 BUF is assumed to be in a compilation unit described by CU_HEADER.
19917 Return *BYTES_READ_PTR count of bytes read from BUF. */
19918
19919 static const char *
19920 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19921 const gdb_byte *buf,
19922 const struct comp_unit_head *cu_header,
19923 unsigned int *bytes_read_ptr)
19924 {
19925 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19926
19927 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19928 }
19929
19930 /* Return pointer to string at .debug_line_str offset as read from BUF.
19931 BUF is assumed to be in a compilation unit described by CU_HEADER.
19932 Return *BYTES_READ_PTR count of bytes read from BUF. */
19933
19934 static const char *
19935 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19936 bfd *abfd, const gdb_byte *buf,
19937 const struct comp_unit_head *cu_header,
19938 unsigned int *bytes_read_ptr)
19939 {
19940 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19941
19942 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19943 str_offset);
19944 }
19945
19946 ULONGEST
19947 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19948 unsigned int *bytes_read_ptr)
19949 {
19950 ULONGEST result;
19951 unsigned int num_read;
19952 int shift;
19953 unsigned char byte;
19954
19955 result = 0;
19956 shift = 0;
19957 num_read = 0;
19958 while (1)
19959 {
19960 byte = bfd_get_8 (abfd, buf);
19961 buf++;
19962 num_read++;
19963 result |= ((ULONGEST) (byte & 127) << shift);
19964 if ((byte & 128) == 0)
19965 {
19966 break;
19967 }
19968 shift += 7;
19969 }
19970 *bytes_read_ptr = num_read;
19971 return result;
19972 }
19973
19974 static LONGEST
19975 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19976 unsigned int *bytes_read_ptr)
19977 {
19978 LONGEST result;
19979 int shift, num_read;
19980 unsigned char byte;
19981
19982 result = 0;
19983 shift = 0;
19984 num_read = 0;
19985 while (1)
19986 {
19987 byte = bfd_get_8 (abfd, buf);
19988 buf++;
19989 num_read++;
19990 result |= ((LONGEST) (byte & 127) << shift);
19991 shift += 7;
19992 if ((byte & 128) == 0)
19993 {
19994 break;
19995 }
19996 }
19997 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19998 result |= -(((LONGEST) 1) << shift);
19999 *bytes_read_ptr = num_read;
20000 return result;
20001 }
20002
20003 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20004 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
20005 ADDR_SIZE is the size of addresses from the CU header. */
20006
20007 static CORE_ADDR
20008 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20009 unsigned int addr_index, ULONGEST addr_base, int addr_size)
20010 {
20011 struct objfile *objfile = dwarf2_per_objfile->objfile;
20012 bfd *abfd = objfile->obfd;
20013 const gdb_byte *info_ptr;
20014
20015 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20016 if (dwarf2_per_objfile->addr.buffer == NULL)
20017 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20018 objfile_name (objfile));
20019 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
20020 error (_("DW_FORM_addr_index pointing outside of "
20021 ".debug_addr section [in module %s]"),
20022 objfile_name (objfile));
20023 info_ptr = (dwarf2_per_objfile->addr.buffer
20024 + addr_base + addr_index * addr_size);
20025 if (addr_size == 4)
20026 return bfd_get_32 (abfd, info_ptr);
20027 else
20028 return bfd_get_64 (abfd, info_ptr);
20029 }
20030
20031 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20032
20033 static CORE_ADDR
20034 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20035 {
20036 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20037 cu->addr_base, cu->header.addr_size);
20038 }
20039
20040 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20041
20042 static CORE_ADDR
20043 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20044 unsigned int *bytes_read)
20045 {
20046 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
20047 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20048
20049 return read_addr_index (cu, addr_index);
20050 }
20051
20052 /* Data structure to pass results from dwarf2_read_addr_index_reader
20053 back to dwarf2_read_addr_index. */
20054
20055 struct dwarf2_read_addr_index_data
20056 {
20057 ULONGEST addr_base;
20058 int addr_size;
20059 };
20060
20061 /* die_reader_func for dwarf2_read_addr_index. */
20062
20063 static void
20064 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
20065 const gdb_byte *info_ptr,
20066 struct die_info *comp_unit_die,
20067 int has_children,
20068 void *data)
20069 {
20070 struct dwarf2_cu *cu = reader->cu;
20071 struct dwarf2_read_addr_index_data *aidata =
20072 (struct dwarf2_read_addr_index_data *) data;
20073
20074 aidata->addr_base = cu->addr_base;
20075 aidata->addr_size = cu->header.addr_size;
20076 }
20077
20078 /* Given an index in .debug_addr, fetch the value.
20079 NOTE: This can be called during dwarf expression evaluation,
20080 long after the debug information has been read, and thus per_cu->cu
20081 may no longer exist. */
20082
20083 CORE_ADDR
20084 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20085 unsigned int addr_index)
20086 {
20087 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
20088 struct objfile *objfile = dwarf2_per_objfile->objfile;
20089 struct dwarf2_cu *cu = per_cu->cu;
20090 ULONGEST addr_base;
20091 int addr_size;
20092
20093 /* We need addr_base and addr_size.
20094 If we don't have PER_CU->cu, we have to get it.
20095 Nasty, but the alternative is storing the needed info in PER_CU,
20096 which at this point doesn't seem justified: it's not clear how frequently
20097 it would get used and it would increase the size of every PER_CU.
20098 Entry points like dwarf2_per_cu_addr_size do a similar thing
20099 so we're not in uncharted territory here.
20100 Alas we need to be a bit more complicated as addr_base is contained
20101 in the DIE.
20102
20103 We don't need to read the entire CU(/TU).
20104 We just need the header and top level die.
20105
20106 IWBN to use the aging mechanism to let us lazily later discard the CU.
20107 For now we skip this optimization. */
20108
20109 if (cu != NULL)
20110 {
20111 addr_base = cu->addr_base;
20112 addr_size = cu->header.addr_size;
20113 }
20114 else
20115 {
20116 struct dwarf2_read_addr_index_data aidata;
20117
20118 /* Note: We can't use init_cutu_and_read_dies_simple here,
20119 we need addr_base. */
20120 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
20121 dwarf2_read_addr_index_reader, &aidata);
20122 addr_base = aidata.addr_base;
20123 addr_size = aidata.addr_size;
20124 }
20125
20126 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20127 addr_size);
20128 }
20129
20130 /* Given a DW_FORM_GNU_str_index, fetch the string.
20131 This is only used by the Fission support. */
20132
20133 static const char *
20134 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20135 {
20136 struct dwarf2_cu *cu = reader->cu;
20137 struct dwarf2_per_objfile *dwarf2_per_objfile
20138 = cu->per_cu->dwarf2_per_objfile;
20139 struct objfile *objfile = dwarf2_per_objfile->objfile;
20140 const char *objf_name = objfile_name (objfile);
20141 bfd *abfd = objfile->obfd;
20142 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20143 struct dwarf2_section_info *str_offsets_section =
20144 &reader->dwo_file->sections.str_offsets;
20145 const gdb_byte *info_ptr;
20146 ULONGEST str_offset;
20147 static const char form_name[] = "DW_FORM_GNU_str_index";
20148
20149 dwarf2_read_section (objfile, str_section);
20150 dwarf2_read_section (objfile, str_offsets_section);
20151 if (str_section->buffer == NULL)
20152 error (_("%s used without .debug_str.dwo section"
20153 " in CU at offset %s [in module %s]"),
20154 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20155 if (str_offsets_section->buffer == NULL)
20156 error (_("%s used without .debug_str_offsets.dwo section"
20157 " in CU at offset %s [in module %s]"),
20158 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20159 if (str_index * cu->header.offset_size >= str_offsets_section->size)
20160 error (_("%s pointing outside of .debug_str_offsets.dwo"
20161 " section in CU at offset %s [in module %s]"),
20162 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20163 info_ptr = (str_offsets_section->buffer
20164 + str_index * cu->header.offset_size);
20165 if (cu->header.offset_size == 4)
20166 str_offset = bfd_get_32 (abfd, info_ptr);
20167 else
20168 str_offset = bfd_get_64 (abfd, info_ptr);
20169 if (str_offset >= str_section->size)
20170 error (_("Offset from %s pointing outside of"
20171 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20172 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20173 return (const char *) (str_section->buffer + str_offset);
20174 }
20175
20176 /* Return the length of an LEB128 number in BUF. */
20177
20178 static int
20179 leb128_size (const gdb_byte *buf)
20180 {
20181 const gdb_byte *begin = buf;
20182 gdb_byte byte;
20183
20184 while (1)
20185 {
20186 byte = *buf++;
20187 if ((byte & 128) == 0)
20188 return buf - begin;
20189 }
20190 }
20191
20192 static void
20193 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20194 {
20195 switch (lang)
20196 {
20197 case DW_LANG_C89:
20198 case DW_LANG_C99:
20199 case DW_LANG_C11:
20200 case DW_LANG_C:
20201 case DW_LANG_UPC:
20202 cu->language = language_c;
20203 break;
20204 case DW_LANG_Java:
20205 case DW_LANG_C_plus_plus:
20206 case DW_LANG_C_plus_plus_11:
20207 case DW_LANG_C_plus_plus_14:
20208 cu->language = language_cplus;
20209 break;
20210 case DW_LANG_D:
20211 cu->language = language_d;
20212 break;
20213 case DW_LANG_Fortran77:
20214 case DW_LANG_Fortran90:
20215 case DW_LANG_Fortran95:
20216 case DW_LANG_Fortran03:
20217 case DW_LANG_Fortran08:
20218 cu->language = language_fortran;
20219 break;
20220 case DW_LANG_Go:
20221 cu->language = language_go;
20222 break;
20223 case DW_LANG_Mips_Assembler:
20224 cu->language = language_asm;
20225 break;
20226 case DW_LANG_Ada83:
20227 case DW_LANG_Ada95:
20228 cu->language = language_ada;
20229 break;
20230 case DW_LANG_Modula2:
20231 cu->language = language_m2;
20232 break;
20233 case DW_LANG_Pascal83:
20234 cu->language = language_pascal;
20235 break;
20236 case DW_LANG_ObjC:
20237 cu->language = language_objc;
20238 break;
20239 case DW_LANG_Rust:
20240 case DW_LANG_Rust_old:
20241 cu->language = language_rust;
20242 break;
20243 case DW_LANG_Cobol74:
20244 case DW_LANG_Cobol85:
20245 default:
20246 cu->language = language_minimal;
20247 break;
20248 }
20249 cu->language_defn = language_def (cu->language);
20250 }
20251
20252 /* Return the named attribute or NULL if not there. */
20253
20254 static struct attribute *
20255 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20256 {
20257 for (;;)
20258 {
20259 unsigned int i;
20260 struct attribute *spec = NULL;
20261
20262 for (i = 0; i < die->num_attrs; ++i)
20263 {
20264 if (die->attrs[i].name == name)
20265 return &die->attrs[i];
20266 if (die->attrs[i].name == DW_AT_specification
20267 || die->attrs[i].name == DW_AT_abstract_origin)
20268 spec = &die->attrs[i];
20269 }
20270
20271 if (!spec)
20272 break;
20273
20274 die = follow_die_ref (die, spec, &cu);
20275 }
20276
20277 return NULL;
20278 }
20279
20280 /* Return the named attribute or NULL if not there,
20281 but do not follow DW_AT_specification, etc.
20282 This is for use in contexts where we're reading .debug_types dies.
20283 Following DW_AT_specification, DW_AT_abstract_origin will take us
20284 back up the chain, and we want to go down. */
20285
20286 static struct attribute *
20287 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20288 {
20289 unsigned int i;
20290
20291 for (i = 0; i < die->num_attrs; ++i)
20292 if (die->attrs[i].name == name)
20293 return &die->attrs[i];
20294
20295 return NULL;
20296 }
20297
20298 /* Return the string associated with a string-typed attribute, or NULL if it
20299 is either not found or is of an incorrect type. */
20300
20301 static const char *
20302 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20303 {
20304 struct attribute *attr;
20305 const char *str = NULL;
20306
20307 attr = dwarf2_attr (die, name, cu);
20308
20309 if (attr != NULL)
20310 {
20311 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20312 || attr->form == DW_FORM_string
20313 || attr->form == DW_FORM_GNU_str_index
20314 || attr->form == DW_FORM_GNU_strp_alt)
20315 str = DW_STRING (attr);
20316 else
20317 complaint (&symfile_complaints,
20318 _("string type expected for attribute %s for "
20319 "DIE at %s in module %s"),
20320 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20321 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20322 }
20323
20324 return str;
20325 }
20326
20327 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20328 and holds a non-zero value. This function should only be used for
20329 DW_FORM_flag or DW_FORM_flag_present attributes. */
20330
20331 static int
20332 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20333 {
20334 struct attribute *attr = dwarf2_attr (die, name, cu);
20335
20336 return (attr && DW_UNSND (attr));
20337 }
20338
20339 static int
20340 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20341 {
20342 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20343 which value is non-zero. However, we have to be careful with
20344 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20345 (via dwarf2_flag_true_p) follows this attribute. So we may
20346 end up accidently finding a declaration attribute that belongs
20347 to a different DIE referenced by the specification attribute,
20348 even though the given DIE does not have a declaration attribute. */
20349 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20350 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20351 }
20352
20353 /* Return the die giving the specification for DIE, if there is
20354 one. *SPEC_CU is the CU containing DIE on input, and the CU
20355 containing the return value on output. If there is no
20356 specification, but there is an abstract origin, that is
20357 returned. */
20358
20359 static struct die_info *
20360 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20361 {
20362 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20363 *spec_cu);
20364
20365 if (spec_attr == NULL)
20366 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20367
20368 if (spec_attr == NULL)
20369 return NULL;
20370 else
20371 return follow_die_ref (die, spec_attr, spec_cu);
20372 }
20373
20374 /* Stub for free_line_header to match void * callback types. */
20375
20376 static void
20377 free_line_header_voidp (void *arg)
20378 {
20379 struct line_header *lh = (struct line_header *) arg;
20380
20381 delete lh;
20382 }
20383
20384 void
20385 line_header::add_include_dir (const char *include_dir)
20386 {
20387 if (dwarf_line_debug >= 2)
20388 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20389 include_dirs.size () + 1, include_dir);
20390
20391 include_dirs.push_back (include_dir);
20392 }
20393
20394 void
20395 line_header::add_file_name (const char *name,
20396 dir_index d_index,
20397 unsigned int mod_time,
20398 unsigned int length)
20399 {
20400 if (dwarf_line_debug >= 2)
20401 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
20402 (unsigned) file_names.size () + 1, name);
20403
20404 file_names.emplace_back (name, d_index, mod_time, length);
20405 }
20406
20407 /* A convenience function to find the proper .debug_line section for a CU. */
20408
20409 static struct dwarf2_section_info *
20410 get_debug_line_section (struct dwarf2_cu *cu)
20411 {
20412 struct dwarf2_section_info *section;
20413 struct dwarf2_per_objfile *dwarf2_per_objfile
20414 = cu->per_cu->dwarf2_per_objfile;
20415
20416 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20417 DWO file. */
20418 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20419 section = &cu->dwo_unit->dwo_file->sections.line;
20420 else if (cu->per_cu->is_dwz)
20421 {
20422 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20423
20424 section = &dwz->line;
20425 }
20426 else
20427 section = &dwarf2_per_objfile->line;
20428
20429 return section;
20430 }
20431
20432 /* Read directory or file name entry format, starting with byte of
20433 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20434 entries count and the entries themselves in the described entry
20435 format. */
20436
20437 static void
20438 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20439 bfd *abfd, const gdb_byte **bufp,
20440 struct line_header *lh,
20441 const struct comp_unit_head *cu_header,
20442 void (*callback) (struct line_header *lh,
20443 const char *name,
20444 dir_index d_index,
20445 unsigned int mod_time,
20446 unsigned int length))
20447 {
20448 gdb_byte format_count, formati;
20449 ULONGEST data_count, datai;
20450 const gdb_byte *buf = *bufp;
20451 const gdb_byte *format_header_data;
20452 unsigned int bytes_read;
20453
20454 format_count = read_1_byte (abfd, buf);
20455 buf += 1;
20456 format_header_data = buf;
20457 for (formati = 0; formati < format_count; formati++)
20458 {
20459 read_unsigned_leb128 (abfd, buf, &bytes_read);
20460 buf += bytes_read;
20461 read_unsigned_leb128 (abfd, buf, &bytes_read);
20462 buf += bytes_read;
20463 }
20464
20465 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20466 buf += bytes_read;
20467 for (datai = 0; datai < data_count; datai++)
20468 {
20469 const gdb_byte *format = format_header_data;
20470 struct file_entry fe;
20471
20472 for (formati = 0; formati < format_count; formati++)
20473 {
20474 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20475 format += bytes_read;
20476
20477 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20478 format += bytes_read;
20479
20480 gdb::optional<const char *> string;
20481 gdb::optional<unsigned int> uint;
20482
20483 switch (form)
20484 {
20485 case DW_FORM_string:
20486 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20487 buf += bytes_read;
20488 break;
20489
20490 case DW_FORM_line_strp:
20491 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20492 abfd, buf,
20493 cu_header,
20494 &bytes_read));
20495 buf += bytes_read;
20496 break;
20497
20498 case DW_FORM_data1:
20499 uint.emplace (read_1_byte (abfd, buf));
20500 buf += 1;
20501 break;
20502
20503 case DW_FORM_data2:
20504 uint.emplace (read_2_bytes (abfd, buf));
20505 buf += 2;
20506 break;
20507
20508 case DW_FORM_data4:
20509 uint.emplace (read_4_bytes (abfd, buf));
20510 buf += 4;
20511 break;
20512
20513 case DW_FORM_data8:
20514 uint.emplace (read_8_bytes (abfd, buf));
20515 buf += 8;
20516 break;
20517
20518 case DW_FORM_udata:
20519 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20520 buf += bytes_read;
20521 break;
20522
20523 case DW_FORM_block:
20524 /* It is valid only for DW_LNCT_timestamp which is ignored by
20525 current GDB. */
20526 break;
20527 }
20528
20529 switch (content_type)
20530 {
20531 case DW_LNCT_path:
20532 if (string.has_value ())
20533 fe.name = *string;
20534 break;
20535 case DW_LNCT_directory_index:
20536 if (uint.has_value ())
20537 fe.d_index = (dir_index) *uint;
20538 break;
20539 case DW_LNCT_timestamp:
20540 if (uint.has_value ())
20541 fe.mod_time = *uint;
20542 break;
20543 case DW_LNCT_size:
20544 if (uint.has_value ())
20545 fe.length = *uint;
20546 break;
20547 case DW_LNCT_MD5:
20548 break;
20549 default:
20550 complaint (&symfile_complaints,
20551 _("Unknown format content type %s"),
20552 pulongest (content_type));
20553 }
20554 }
20555
20556 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20557 }
20558
20559 *bufp = buf;
20560 }
20561
20562 /* Read the statement program header starting at OFFSET in
20563 .debug_line, or .debug_line.dwo. Return a pointer
20564 to a struct line_header, allocated using xmalloc.
20565 Returns NULL if there is a problem reading the header, e.g., if it
20566 has a version we don't understand.
20567
20568 NOTE: the strings in the include directory and file name tables of
20569 the returned object point into the dwarf line section buffer,
20570 and must not be freed. */
20571
20572 static line_header_up
20573 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20574 {
20575 const gdb_byte *line_ptr;
20576 unsigned int bytes_read, offset_size;
20577 int i;
20578 const char *cur_dir, *cur_file;
20579 struct dwarf2_section_info *section;
20580 bfd *abfd;
20581 struct dwarf2_per_objfile *dwarf2_per_objfile
20582 = cu->per_cu->dwarf2_per_objfile;
20583
20584 section = get_debug_line_section (cu);
20585 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20586 if (section->buffer == NULL)
20587 {
20588 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20589 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20590 else
20591 complaint (&symfile_complaints, _("missing .debug_line section"));
20592 return 0;
20593 }
20594
20595 /* We can't do this until we know the section is non-empty.
20596 Only then do we know we have such a section. */
20597 abfd = get_section_bfd_owner (section);
20598
20599 /* Make sure that at least there's room for the total_length field.
20600 That could be 12 bytes long, but we're just going to fudge that. */
20601 if (to_underlying (sect_off) + 4 >= section->size)
20602 {
20603 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20604 return 0;
20605 }
20606
20607 line_header_up lh (new line_header ());
20608
20609 lh->sect_off = sect_off;
20610 lh->offset_in_dwz = cu->per_cu->is_dwz;
20611
20612 line_ptr = section->buffer + to_underlying (sect_off);
20613
20614 /* Read in the header. */
20615 lh->total_length =
20616 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20617 &bytes_read, &offset_size);
20618 line_ptr += bytes_read;
20619 if (line_ptr + lh->total_length > (section->buffer + section->size))
20620 {
20621 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20622 return 0;
20623 }
20624 lh->statement_program_end = line_ptr + lh->total_length;
20625 lh->version = read_2_bytes (abfd, line_ptr);
20626 line_ptr += 2;
20627 if (lh->version > 5)
20628 {
20629 /* This is a version we don't understand. The format could have
20630 changed in ways we don't handle properly so just punt. */
20631 complaint (&symfile_complaints,
20632 _("unsupported version in .debug_line section"));
20633 return NULL;
20634 }
20635 if (lh->version >= 5)
20636 {
20637 gdb_byte segment_selector_size;
20638
20639 /* Skip address size. */
20640 read_1_byte (abfd, line_ptr);
20641 line_ptr += 1;
20642
20643 segment_selector_size = read_1_byte (abfd, line_ptr);
20644 line_ptr += 1;
20645 if (segment_selector_size != 0)
20646 {
20647 complaint (&symfile_complaints,
20648 _("unsupported segment selector size %u "
20649 "in .debug_line section"),
20650 segment_selector_size);
20651 return NULL;
20652 }
20653 }
20654 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20655 line_ptr += offset_size;
20656 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20657 line_ptr += 1;
20658 if (lh->version >= 4)
20659 {
20660 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20661 line_ptr += 1;
20662 }
20663 else
20664 lh->maximum_ops_per_instruction = 1;
20665
20666 if (lh->maximum_ops_per_instruction == 0)
20667 {
20668 lh->maximum_ops_per_instruction = 1;
20669 complaint (&symfile_complaints,
20670 _("invalid maximum_ops_per_instruction "
20671 "in `.debug_line' section"));
20672 }
20673
20674 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20675 line_ptr += 1;
20676 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20677 line_ptr += 1;
20678 lh->line_range = read_1_byte (abfd, line_ptr);
20679 line_ptr += 1;
20680 lh->opcode_base = read_1_byte (abfd, line_ptr);
20681 line_ptr += 1;
20682 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20683
20684 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20685 for (i = 1; i < lh->opcode_base; ++i)
20686 {
20687 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20688 line_ptr += 1;
20689 }
20690
20691 if (lh->version >= 5)
20692 {
20693 /* Read directory table. */
20694 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20695 &cu->header,
20696 [] (struct line_header *lh, const char *name,
20697 dir_index d_index, unsigned int mod_time,
20698 unsigned int length)
20699 {
20700 lh->add_include_dir (name);
20701 });
20702
20703 /* Read file name table. */
20704 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20705 &cu->header,
20706 [] (struct line_header *lh, const char *name,
20707 dir_index d_index, unsigned int mod_time,
20708 unsigned int length)
20709 {
20710 lh->add_file_name (name, d_index, mod_time, length);
20711 });
20712 }
20713 else
20714 {
20715 /* Read directory table. */
20716 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20717 {
20718 line_ptr += bytes_read;
20719 lh->add_include_dir (cur_dir);
20720 }
20721 line_ptr += bytes_read;
20722
20723 /* Read file name table. */
20724 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20725 {
20726 unsigned int mod_time, length;
20727 dir_index d_index;
20728
20729 line_ptr += bytes_read;
20730 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20731 line_ptr += bytes_read;
20732 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20733 line_ptr += bytes_read;
20734 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20735 line_ptr += bytes_read;
20736
20737 lh->add_file_name (cur_file, d_index, mod_time, length);
20738 }
20739 line_ptr += bytes_read;
20740 }
20741 lh->statement_program_start = line_ptr;
20742
20743 if (line_ptr > (section->buffer + section->size))
20744 complaint (&symfile_complaints,
20745 _("line number info header doesn't "
20746 "fit in `.debug_line' section"));
20747
20748 return lh;
20749 }
20750
20751 /* Subroutine of dwarf_decode_lines to simplify it.
20752 Return the file name of the psymtab for included file FILE_INDEX
20753 in line header LH of PST.
20754 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20755 If space for the result is malloc'd, *NAME_HOLDER will be set.
20756 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20757
20758 static const char *
20759 psymtab_include_file_name (const struct line_header *lh, int file_index,
20760 const struct partial_symtab *pst,
20761 const char *comp_dir,
20762 gdb::unique_xmalloc_ptr<char> *name_holder)
20763 {
20764 const file_entry &fe = lh->file_names[file_index];
20765 const char *include_name = fe.name;
20766 const char *include_name_to_compare = include_name;
20767 const char *pst_filename;
20768 int file_is_pst;
20769
20770 const char *dir_name = fe.include_dir (lh);
20771
20772 gdb::unique_xmalloc_ptr<char> hold_compare;
20773 if (!IS_ABSOLUTE_PATH (include_name)
20774 && (dir_name != NULL || comp_dir != NULL))
20775 {
20776 /* Avoid creating a duplicate psymtab for PST.
20777 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20778 Before we do the comparison, however, we need to account
20779 for DIR_NAME and COMP_DIR.
20780 First prepend dir_name (if non-NULL). If we still don't
20781 have an absolute path prepend comp_dir (if non-NULL).
20782 However, the directory we record in the include-file's
20783 psymtab does not contain COMP_DIR (to match the
20784 corresponding symtab(s)).
20785
20786 Example:
20787
20788 bash$ cd /tmp
20789 bash$ gcc -g ./hello.c
20790 include_name = "hello.c"
20791 dir_name = "."
20792 DW_AT_comp_dir = comp_dir = "/tmp"
20793 DW_AT_name = "./hello.c"
20794
20795 */
20796
20797 if (dir_name != NULL)
20798 {
20799 name_holder->reset (concat (dir_name, SLASH_STRING,
20800 include_name, (char *) NULL));
20801 include_name = name_holder->get ();
20802 include_name_to_compare = include_name;
20803 }
20804 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20805 {
20806 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20807 include_name, (char *) NULL));
20808 include_name_to_compare = hold_compare.get ();
20809 }
20810 }
20811
20812 pst_filename = pst->filename;
20813 gdb::unique_xmalloc_ptr<char> copied_name;
20814 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20815 {
20816 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20817 pst_filename, (char *) NULL));
20818 pst_filename = copied_name.get ();
20819 }
20820
20821 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20822
20823 if (file_is_pst)
20824 return NULL;
20825 return include_name;
20826 }
20827
20828 /* State machine to track the state of the line number program. */
20829
20830 class lnp_state_machine
20831 {
20832 public:
20833 /* Initialize a machine state for the start of a line number
20834 program. */
20835 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20836
20837 file_entry *current_file ()
20838 {
20839 /* lh->file_names is 0-based, but the file name numbers in the
20840 statement program are 1-based. */
20841 return m_line_header->file_name_at (m_file);
20842 }
20843
20844 /* Record the line in the state machine. END_SEQUENCE is true if
20845 we're processing the end of a sequence. */
20846 void record_line (bool end_sequence);
20847
20848 /* Check address and if invalid nop-out the rest of the lines in this
20849 sequence. */
20850 void check_line_address (struct dwarf2_cu *cu,
20851 const gdb_byte *line_ptr,
20852 CORE_ADDR lowpc, CORE_ADDR address);
20853
20854 void handle_set_discriminator (unsigned int discriminator)
20855 {
20856 m_discriminator = discriminator;
20857 m_line_has_non_zero_discriminator |= discriminator != 0;
20858 }
20859
20860 /* Handle DW_LNE_set_address. */
20861 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20862 {
20863 m_op_index = 0;
20864 address += baseaddr;
20865 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20866 }
20867
20868 /* Handle DW_LNS_advance_pc. */
20869 void handle_advance_pc (CORE_ADDR adjust);
20870
20871 /* Handle a special opcode. */
20872 void handle_special_opcode (unsigned char op_code);
20873
20874 /* Handle DW_LNS_advance_line. */
20875 void handle_advance_line (int line_delta)
20876 {
20877 advance_line (line_delta);
20878 }
20879
20880 /* Handle DW_LNS_set_file. */
20881 void handle_set_file (file_name_index file);
20882
20883 /* Handle DW_LNS_negate_stmt. */
20884 void handle_negate_stmt ()
20885 {
20886 m_is_stmt = !m_is_stmt;
20887 }
20888
20889 /* Handle DW_LNS_const_add_pc. */
20890 void handle_const_add_pc ();
20891
20892 /* Handle DW_LNS_fixed_advance_pc. */
20893 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20894 {
20895 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20896 m_op_index = 0;
20897 }
20898
20899 /* Handle DW_LNS_copy. */
20900 void handle_copy ()
20901 {
20902 record_line (false);
20903 m_discriminator = 0;
20904 }
20905
20906 /* Handle DW_LNE_end_sequence. */
20907 void handle_end_sequence ()
20908 {
20909 m_record_line_callback = ::record_line;
20910 }
20911
20912 private:
20913 /* Advance the line by LINE_DELTA. */
20914 void advance_line (int line_delta)
20915 {
20916 m_line += line_delta;
20917
20918 if (line_delta != 0)
20919 m_line_has_non_zero_discriminator = m_discriminator != 0;
20920 }
20921
20922 gdbarch *m_gdbarch;
20923
20924 /* True if we're recording lines.
20925 Otherwise we're building partial symtabs and are just interested in
20926 finding include files mentioned by the line number program. */
20927 bool m_record_lines_p;
20928
20929 /* The line number header. */
20930 line_header *m_line_header;
20931
20932 /* These are part of the standard DWARF line number state machine,
20933 and initialized according to the DWARF spec. */
20934
20935 unsigned char m_op_index = 0;
20936 /* The line table index (1-based) of the current file. */
20937 file_name_index m_file = (file_name_index) 1;
20938 unsigned int m_line = 1;
20939
20940 /* These are initialized in the constructor. */
20941
20942 CORE_ADDR m_address;
20943 bool m_is_stmt;
20944 unsigned int m_discriminator;
20945
20946 /* Additional bits of state we need to track. */
20947
20948 /* The last file that we called dwarf2_start_subfile for.
20949 This is only used for TLLs. */
20950 unsigned int m_last_file = 0;
20951 /* The last file a line number was recorded for. */
20952 struct subfile *m_last_subfile = NULL;
20953
20954 /* The function to call to record a line. */
20955 record_line_ftype *m_record_line_callback = NULL;
20956
20957 /* The last line number that was recorded, used to coalesce
20958 consecutive entries for the same line. This can happen, for
20959 example, when discriminators are present. PR 17276. */
20960 unsigned int m_last_line = 0;
20961 bool m_line_has_non_zero_discriminator = false;
20962 };
20963
20964 void
20965 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20966 {
20967 CORE_ADDR addr_adj = (((m_op_index + adjust)
20968 / m_line_header->maximum_ops_per_instruction)
20969 * m_line_header->minimum_instruction_length);
20970 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20971 m_op_index = ((m_op_index + adjust)
20972 % m_line_header->maximum_ops_per_instruction);
20973 }
20974
20975 void
20976 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20977 {
20978 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20979 CORE_ADDR addr_adj = (((m_op_index
20980 + (adj_opcode / m_line_header->line_range))
20981 / m_line_header->maximum_ops_per_instruction)
20982 * m_line_header->minimum_instruction_length);
20983 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20984 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20985 % m_line_header->maximum_ops_per_instruction);
20986
20987 int line_delta = (m_line_header->line_base
20988 + (adj_opcode % m_line_header->line_range));
20989 advance_line (line_delta);
20990 record_line (false);
20991 m_discriminator = 0;
20992 }
20993
20994 void
20995 lnp_state_machine::handle_set_file (file_name_index file)
20996 {
20997 m_file = file;
20998
20999 const file_entry *fe = current_file ();
21000 if (fe == NULL)
21001 dwarf2_debug_line_missing_file_complaint ();
21002 else if (m_record_lines_p)
21003 {
21004 const char *dir = fe->include_dir (m_line_header);
21005
21006 m_last_subfile = current_subfile;
21007 m_line_has_non_zero_discriminator = m_discriminator != 0;
21008 dwarf2_start_subfile (fe->name, dir);
21009 }
21010 }
21011
21012 void
21013 lnp_state_machine::handle_const_add_pc ()
21014 {
21015 CORE_ADDR adjust
21016 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21017
21018 CORE_ADDR addr_adj
21019 = (((m_op_index + adjust)
21020 / m_line_header->maximum_ops_per_instruction)
21021 * m_line_header->minimum_instruction_length);
21022
21023 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21024 m_op_index = ((m_op_index + adjust)
21025 % m_line_header->maximum_ops_per_instruction);
21026 }
21027
21028 /* Ignore this record_line request. */
21029
21030 static void
21031 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
21032 {
21033 return;
21034 }
21035
21036 /* Return non-zero if we should add LINE to the line number table.
21037 LINE is the line to add, LAST_LINE is the last line that was added,
21038 LAST_SUBFILE is the subfile for LAST_LINE.
21039 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21040 had a non-zero discriminator.
21041
21042 We have to be careful in the presence of discriminators.
21043 E.g., for this line:
21044
21045 for (i = 0; i < 100000; i++);
21046
21047 clang can emit four line number entries for that one line,
21048 each with a different discriminator.
21049 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21050
21051 However, we want gdb to coalesce all four entries into one.
21052 Otherwise the user could stepi into the middle of the line and
21053 gdb would get confused about whether the pc really was in the
21054 middle of the line.
21055
21056 Things are further complicated by the fact that two consecutive
21057 line number entries for the same line is a heuristic used by gcc
21058 to denote the end of the prologue. So we can't just discard duplicate
21059 entries, we have to be selective about it. The heuristic we use is
21060 that we only collapse consecutive entries for the same line if at least
21061 one of those entries has a non-zero discriminator. PR 17276.
21062
21063 Note: Addresses in the line number state machine can never go backwards
21064 within one sequence, thus this coalescing is ok. */
21065
21066 static int
21067 dwarf_record_line_p (unsigned int line, unsigned int last_line,
21068 int line_has_non_zero_discriminator,
21069 struct subfile *last_subfile)
21070 {
21071 if (current_subfile != last_subfile)
21072 return 1;
21073 if (line != last_line)
21074 return 1;
21075 /* Same line for the same file that we've seen already.
21076 As a last check, for pr 17276, only record the line if the line
21077 has never had a non-zero discriminator. */
21078 if (!line_has_non_zero_discriminator)
21079 return 1;
21080 return 0;
21081 }
21082
21083 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
21084 in the line table of subfile SUBFILE. */
21085
21086 static void
21087 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21088 unsigned int line, CORE_ADDR address,
21089 record_line_ftype p_record_line)
21090 {
21091 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21092
21093 if (dwarf_line_debug)
21094 {
21095 fprintf_unfiltered (gdb_stdlog,
21096 "Recording line %u, file %s, address %s\n",
21097 line, lbasename (subfile->name),
21098 paddress (gdbarch, address));
21099 }
21100
21101 (*p_record_line) (subfile, line, addr);
21102 }
21103
21104 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21105 Mark the end of a set of line number records.
21106 The arguments are the same as for dwarf_record_line_1.
21107 If SUBFILE is NULL the request is ignored. */
21108
21109 static void
21110 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21111 CORE_ADDR address, record_line_ftype p_record_line)
21112 {
21113 if (subfile == NULL)
21114 return;
21115
21116 if (dwarf_line_debug)
21117 {
21118 fprintf_unfiltered (gdb_stdlog,
21119 "Finishing current line, file %s, address %s\n",
21120 lbasename (subfile->name),
21121 paddress (gdbarch, address));
21122 }
21123
21124 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
21125 }
21126
21127 void
21128 lnp_state_machine::record_line (bool end_sequence)
21129 {
21130 if (dwarf_line_debug)
21131 {
21132 fprintf_unfiltered (gdb_stdlog,
21133 "Processing actual line %u: file %u,"
21134 " address %s, is_stmt %u, discrim %u\n",
21135 m_line, to_underlying (m_file),
21136 paddress (m_gdbarch, m_address),
21137 m_is_stmt, m_discriminator);
21138 }
21139
21140 file_entry *fe = current_file ();
21141
21142 if (fe == NULL)
21143 dwarf2_debug_line_missing_file_complaint ();
21144 /* For now we ignore lines not starting on an instruction boundary.
21145 But not when processing end_sequence for compatibility with the
21146 previous version of the code. */
21147 else if (m_op_index == 0 || end_sequence)
21148 {
21149 fe->included_p = 1;
21150 if (m_record_lines_p && m_is_stmt)
21151 {
21152 if (m_last_subfile != current_subfile || end_sequence)
21153 {
21154 dwarf_finish_line (m_gdbarch, m_last_subfile,
21155 m_address, m_record_line_callback);
21156 }
21157
21158 if (!end_sequence)
21159 {
21160 if (dwarf_record_line_p (m_line, m_last_line,
21161 m_line_has_non_zero_discriminator,
21162 m_last_subfile))
21163 {
21164 dwarf_record_line_1 (m_gdbarch, current_subfile,
21165 m_line, m_address,
21166 m_record_line_callback);
21167 }
21168 m_last_subfile = current_subfile;
21169 m_last_line = m_line;
21170 }
21171 }
21172 }
21173 }
21174
21175 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
21176 bool record_lines_p)
21177 {
21178 m_gdbarch = arch;
21179 m_record_lines_p = record_lines_p;
21180 m_line_header = lh;
21181
21182 m_record_line_callback = ::record_line;
21183
21184 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21185 was a line entry for it so that the backend has a chance to adjust it
21186 and also record it in case it needs it. This is currently used by MIPS
21187 code, cf. `mips_adjust_dwarf2_line'. */
21188 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21189 m_is_stmt = lh->default_is_stmt;
21190 m_discriminator = 0;
21191 }
21192
21193 void
21194 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21195 const gdb_byte *line_ptr,
21196 CORE_ADDR lowpc, CORE_ADDR address)
21197 {
21198 /* If address < lowpc then it's not a usable value, it's outside the
21199 pc range of the CU. However, we restrict the test to only address
21200 values of zero to preserve GDB's previous behaviour which is to
21201 handle the specific case of a function being GC'd by the linker. */
21202
21203 if (address == 0 && address < lowpc)
21204 {
21205 /* This line table is for a function which has been
21206 GCd by the linker. Ignore it. PR gdb/12528 */
21207
21208 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21209 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21210
21211 complaint (&symfile_complaints,
21212 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21213 line_offset, objfile_name (objfile));
21214 m_record_line_callback = noop_record_line;
21215 /* Note: record_line_callback is left as noop_record_line until
21216 we see DW_LNE_end_sequence. */
21217 }
21218 }
21219
21220 /* Subroutine of dwarf_decode_lines to simplify it.
21221 Process the line number information in LH.
21222 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21223 program in order to set included_p for every referenced header. */
21224
21225 static void
21226 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21227 const int decode_for_pst_p, CORE_ADDR lowpc)
21228 {
21229 const gdb_byte *line_ptr, *extended_end;
21230 const gdb_byte *line_end;
21231 unsigned int bytes_read, extended_len;
21232 unsigned char op_code, extended_op;
21233 CORE_ADDR baseaddr;
21234 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21235 bfd *abfd = objfile->obfd;
21236 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21237 /* True if we're recording line info (as opposed to building partial
21238 symtabs and just interested in finding include files mentioned by
21239 the line number program). */
21240 bool record_lines_p = !decode_for_pst_p;
21241
21242 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21243
21244 line_ptr = lh->statement_program_start;
21245 line_end = lh->statement_program_end;
21246
21247 /* Read the statement sequences until there's nothing left. */
21248 while (line_ptr < line_end)
21249 {
21250 /* The DWARF line number program state machine. Reset the state
21251 machine at the start of each sequence. */
21252 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
21253 bool end_sequence = false;
21254
21255 if (record_lines_p)
21256 {
21257 /* Start a subfile for the current file of the state
21258 machine. */
21259 const file_entry *fe = state_machine.current_file ();
21260
21261 if (fe != NULL)
21262 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
21263 }
21264
21265 /* Decode the table. */
21266 while (line_ptr < line_end && !end_sequence)
21267 {
21268 op_code = read_1_byte (abfd, line_ptr);
21269 line_ptr += 1;
21270
21271 if (op_code >= lh->opcode_base)
21272 {
21273 /* Special opcode. */
21274 state_machine.handle_special_opcode (op_code);
21275 }
21276 else switch (op_code)
21277 {
21278 case DW_LNS_extended_op:
21279 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21280 &bytes_read);
21281 line_ptr += bytes_read;
21282 extended_end = line_ptr + extended_len;
21283 extended_op = read_1_byte (abfd, line_ptr);
21284 line_ptr += 1;
21285 switch (extended_op)
21286 {
21287 case DW_LNE_end_sequence:
21288 state_machine.handle_end_sequence ();
21289 end_sequence = true;
21290 break;
21291 case DW_LNE_set_address:
21292 {
21293 CORE_ADDR address
21294 = read_address (abfd, line_ptr, cu, &bytes_read);
21295 line_ptr += bytes_read;
21296
21297 state_machine.check_line_address (cu, line_ptr,
21298 lowpc, address);
21299 state_machine.handle_set_address (baseaddr, address);
21300 }
21301 break;
21302 case DW_LNE_define_file:
21303 {
21304 const char *cur_file;
21305 unsigned int mod_time, length;
21306 dir_index dindex;
21307
21308 cur_file = read_direct_string (abfd, line_ptr,
21309 &bytes_read);
21310 line_ptr += bytes_read;
21311 dindex = (dir_index)
21312 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21313 line_ptr += bytes_read;
21314 mod_time =
21315 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21316 line_ptr += bytes_read;
21317 length =
21318 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21319 line_ptr += bytes_read;
21320 lh->add_file_name (cur_file, dindex, mod_time, length);
21321 }
21322 break;
21323 case DW_LNE_set_discriminator:
21324 {
21325 /* The discriminator is not interesting to the
21326 debugger; just ignore it. We still need to
21327 check its value though:
21328 if there are consecutive entries for the same
21329 (non-prologue) line we want to coalesce them.
21330 PR 17276. */
21331 unsigned int discr
21332 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21333 line_ptr += bytes_read;
21334
21335 state_machine.handle_set_discriminator (discr);
21336 }
21337 break;
21338 default:
21339 complaint (&symfile_complaints,
21340 _("mangled .debug_line section"));
21341 return;
21342 }
21343 /* Make sure that we parsed the extended op correctly. If e.g.
21344 we expected a different address size than the producer used,
21345 we may have read the wrong number of bytes. */
21346 if (line_ptr != extended_end)
21347 {
21348 complaint (&symfile_complaints,
21349 _("mangled .debug_line section"));
21350 return;
21351 }
21352 break;
21353 case DW_LNS_copy:
21354 state_machine.handle_copy ();
21355 break;
21356 case DW_LNS_advance_pc:
21357 {
21358 CORE_ADDR adjust
21359 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21360 line_ptr += bytes_read;
21361
21362 state_machine.handle_advance_pc (adjust);
21363 }
21364 break;
21365 case DW_LNS_advance_line:
21366 {
21367 int line_delta
21368 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21369 line_ptr += bytes_read;
21370
21371 state_machine.handle_advance_line (line_delta);
21372 }
21373 break;
21374 case DW_LNS_set_file:
21375 {
21376 file_name_index file
21377 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21378 &bytes_read);
21379 line_ptr += bytes_read;
21380
21381 state_machine.handle_set_file (file);
21382 }
21383 break;
21384 case DW_LNS_set_column:
21385 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21386 line_ptr += bytes_read;
21387 break;
21388 case DW_LNS_negate_stmt:
21389 state_machine.handle_negate_stmt ();
21390 break;
21391 case DW_LNS_set_basic_block:
21392 break;
21393 /* Add to the address register of the state machine the
21394 address increment value corresponding to special opcode
21395 255. I.e., this value is scaled by the minimum
21396 instruction length since special opcode 255 would have
21397 scaled the increment. */
21398 case DW_LNS_const_add_pc:
21399 state_machine.handle_const_add_pc ();
21400 break;
21401 case DW_LNS_fixed_advance_pc:
21402 {
21403 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21404 line_ptr += 2;
21405
21406 state_machine.handle_fixed_advance_pc (addr_adj);
21407 }
21408 break;
21409 default:
21410 {
21411 /* Unknown standard opcode, ignore it. */
21412 int i;
21413
21414 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21415 {
21416 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21417 line_ptr += bytes_read;
21418 }
21419 }
21420 }
21421 }
21422
21423 if (!end_sequence)
21424 dwarf2_debug_line_missing_end_sequence_complaint ();
21425
21426 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21427 in which case we still finish recording the last line). */
21428 state_machine.record_line (true);
21429 }
21430 }
21431
21432 /* Decode the Line Number Program (LNP) for the given line_header
21433 structure and CU. The actual information extracted and the type
21434 of structures created from the LNP depends on the value of PST.
21435
21436 1. If PST is NULL, then this procedure uses the data from the program
21437 to create all necessary symbol tables, and their linetables.
21438
21439 2. If PST is not NULL, this procedure reads the program to determine
21440 the list of files included by the unit represented by PST, and
21441 builds all the associated partial symbol tables.
21442
21443 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21444 It is used for relative paths in the line table.
21445 NOTE: When processing partial symtabs (pst != NULL),
21446 comp_dir == pst->dirname.
21447
21448 NOTE: It is important that psymtabs have the same file name (via strcmp)
21449 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21450 symtab we don't use it in the name of the psymtabs we create.
21451 E.g. expand_line_sal requires this when finding psymtabs to expand.
21452 A good testcase for this is mb-inline.exp.
21453
21454 LOWPC is the lowest address in CU (or 0 if not known).
21455
21456 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21457 for its PC<->lines mapping information. Otherwise only the filename
21458 table is read in. */
21459
21460 static void
21461 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21462 struct dwarf2_cu *cu, struct partial_symtab *pst,
21463 CORE_ADDR lowpc, int decode_mapping)
21464 {
21465 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21466 const int decode_for_pst_p = (pst != NULL);
21467
21468 if (decode_mapping)
21469 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21470
21471 if (decode_for_pst_p)
21472 {
21473 int file_index;
21474
21475 /* Now that we're done scanning the Line Header Program, we can
21476 create the psymtab of each included file. */
21477 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21478 if (lh->file_names[file_index].included_p == 1)
21479 {
21480 gdb::unique_xmalloc_ptr<char> name_holder;
21481 const char *include_name =
21482 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21483 &name_holder);
21484 if (include_name != NULL)
21485 dwarf2_create_include_psymtab (include_name, pst, objfile);
21486 }
21487 }
21488 else
21489 {
21490 /* Make sure a symtab is created for every file, even files
21491 which contain only variables (i.e. no code with associated
21492 line numbers). */
21493 struct compunit_symtab *cust = buildsym_compunit_symtab ();
21494 int i;
21495
21496 for (i = 0; i < lh->file_names.size (); i++)
21497 {
21498 file_entry &fe = lh->file_names[i];
21499
21500 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
21501
21502 if (current_subfile->symtab == NULL)
21503 {
21504 current_subfile->symtab
21505 = allocate_symtab (cust, current_subfile->name);
21506 }
21507 fe.symtab = current_subfile->symtab;
21508 }
21509 }
21510 }
21511
21512 /* Start a subfile for DWARF. FILENAME is the name of the file and
21513 DIRNAME the name of the source directory which contains FILENAME
21514 or NULL if not known.
21515 This routine tries to keep line numbers from identical absolute and
21516 relative file names in a common subfile.
21517
21518 Using the `list' example from the GDB testsuite, which resides in
21519 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21520 of /srcdir/list0.c yields the following debugging information for list0.c:
21521
21522 DW_AT_name: /srcdir/list0.c
21523 DW_AT_comp_dir: /compdir
21524 files.files[0].name: list0.h
21525 files.files[0].dir: /srcdir
21526 files.files[1].name: list0.c
21527 files.files[1].dir: /srcdir
21528
21529 The line number information for list0.c has to end up in a single
21530 subfile, so that `break /srcdir/list0.c:1' works as expected.
21531 start_subfile will ensure that this happens provided that we pass the
21532 concatenation of files.files[1].dir and files.files[1].name as the
21533 subfile's name. */
21534
21535 static void
21536 dwarf2_start_subfile (const char *filename, const char *dirname)
21537 {
21538 char *copy = NULL;
21539
21540 /* In order not to lose the line information directory,
21541 we concatenate it to the filename when it makes sense.
21542 Note that the Dwarf3 standard says (speaking of filenames in line
21543 information): ``The directory index is ignored for file names
21544 that represent full path names''. Thus ignoring dirname in the
21545 `else' branch below isn't an issue. */
21546
21547 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21548 {
21549 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21550 filename = copy;
21551 }
21552
21553 start_subfile (filename);
21554
21555 if (copy != NULL)
21556 xfree (copy);
21557 }
21558
21559 /* Start a symtab for DWARF.
21560 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21561
21562 static struct compunit_symtab *
21563 dwarf2_start_symtab (struct dwarf2_cu *cu,
21564 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21565 {
21566 struct compunit_symtab *cust
21567 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21568 low_pc, cu->language);
21569
21570 record_debugformat ("DWARF 2");
21571 record_producer (cu->producer);
21572
21573 /* We assume that we're processing GCC output. */
21574 processing_gcc_compilation = 2;
21575
21576 cu->processing_has_namespace_info = 0;
21577
21578 return cust;
21579 }
21580
21581 static void
21582 var_decode_location (struct attribute *attr, struct symbol *sym,
21583 struct dwarf2_cu *cu)
21584 {
21585 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21586 struct comp_unit_head *cu_header = &cu->header;
21587
21588 /* NOTE drow/2003-01-30: There used to be a comment and some special
21589 code here to turn a symbol with DW_AT_external and a
21590 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21591 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21592 with some versions of binutils) where shared libraries could have
21593 relocations against symbols in their debug information - the
21594 minimal symbol would have the right address, but the debug info
21595 would not. It's no longer necessary, because we will explicitly
21596 apply relocations when we read in the debug information now. */
21597
21598 /* A DW_AT_location attribute with no contents indicates that a
21599 variable has been optimized away. */
21600 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21601 {
21602 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21603 return;
21604 }
21605
21606 /* Handle one degenerate form of location expression specially, to
21607 preserve GDB's previous behavior when section offsets are
21608 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21609 then mark this symbol as LOC_STATIC. */
21610
21611 if (attr_form_is_block (attr)
21612 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21613 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21614 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21615 && (DW_BLOCK (attr)->size
21616 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21617 {
21618 unsigned int dummy;
21619
21620 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21621 SYMBOL_VALUE_ADDRESS (sym) =
21622 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21623 else
21624 SYMBOL_VALUE_ADDRESS (sym) =
21625 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21626 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21627 fixup_symbol_section (sym, objfile);
21628 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21629 SYMBOL_SECTION (sym));
21630 return;
21631 }
21632
21633 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21634 expression evaluator, and use LOC_COMPUTED only when necessary
21635 (i.e. when the value of a register or memory location is
21636 referenced, or a thread-local block, etc.). Then again, it might
21637 not be worthwhile. I'm assuming that it isn't unless performance
21638 or memory numbers show me otherwise. */
21639
21640 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21641
21642 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21643 cu->has_loclist = 1;
21644 }
21645
21646 /* Given a pointer to a DWARF information entry, figure out if we need
21647 to make a symbol table entry for it, and if so, create a new entry
21648 and return a pointer to it.
21649 If TYPE is NULL, determine symbol type from the die, otherwise
21650 used the passed type.
21651 If SPACE is not NULL, use it to hold the new symbol. If it is
21652 NULL, allocate a new symbol on the objfile's obstack. */
21653
21654 static struct symbol *
21655 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21656 struct symbol *space)
21657 {
21658 struct dwarf2_per_objfile *dwarf2_per_objfile
21659 = cu->per_cu->dwarf2_per_objfile;
21660 struct objfile *objfile = dwarf2_per_objfile->objfile;
21661 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21662 struct symbol *sym = NULL;
21663 const char *name;
21664 struct attribute *attr = NULL;
21665 struct attribute *attr2 = NULL;
21666 CORE_ADDR baseaddr;
21667 struct pending **list_to_add = NULL;
21668
21669 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21670
21671 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21672
21673 name = dwarf2_name (die, cu);
21674 if (name)
21675 {
21676 const char *linkagename;
21677 int suppress_add = 0;
21678
21679 if (space)
21680 sym = space;
21681 else
21682 sym = allocate_symbol (objfile);
21683 OBJSTAT (objfile, n_syms++);
21684
21685 /* Cache this symbol's name and the name's demangled form (if any). */
21686 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21687 linkagename = dwarf2_physname (name, die, cu);
21688 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21689
21690 /* Fortran does not have mangling standard and the mangling does differ
21691 between gfortran, iFort etc. */
21692 if (cu->language == language_fortran
21693 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21694 symbol_set_demangled_name (&(sym->ginfo),
21695 dwarf2_full_name (name, die, cu),
21696 NULL);
21697
21698 /* Default assumptions.
21699 Use the passed type or decode it from the die. */
21700 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21701 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21702 if (type != NULL)
21703 SYMBOL_TYPE (sym) = type;
21704 else
21705 SYMBOL_TYPE (sym) = die_type (die, cu);
21706 attr = dwarf2_attr (die,
21707 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21708 cu);
21709 if (attr)
21710 {
21711 SYMBOL_LINE (sym) = DW_UNSND (attr);
21712 }
21713
21714 attr = dwarf2_attr (die,
21715 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21716 cu);
21717 if (attr)
21718 {
21719 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21720 struct file_entry *fe;
21721
21722 if (cu->line_header != NULL)
21723 fe = cu->line_header->file_name_at (file_index);
21724 else
21725 fe = NULL;
21726
21727 if (fe == NULL)
21728 complaint (&symfile_complaints,
21729 _("file index out of range"));
21730 else
21731 symbol_set_symtab (sym, fe->symtab);
21732 }
21733
21734 switch (die->tag)
21735 {
21736 case DW_TAG_label:
21737 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21738 if (attr)
21739 {
21740 CORE_ADDR addr;
21741
21742 addr = attr_value_as_address (attr);
21743 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21744 SYMBOL_VALUE_ADDRESS (sym) = addr;
21745 }
21746 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21747 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21748 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21749 add_symbol_to_list (sym, cu->list_in_scope);
21750 break;
21751 case DW_TAG_subprogram:
21752 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21753 finish_block. */
21754 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21755 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21756 if ((attr2 && (DW_UNSND (attr2) != 0))
21757 || cu->language == language_ada)
21758 {
21759 /* Subprograms marked external are stored as a global symbol.
21760 Ada subprograms, whether marked external or not, are always
21761 stored as a global symbol, because we want to be able to
21762 access them globally. For instance, we want to be able
21763 to break on a nested subprogram without having to
21764 specify the context. */
21765 list_to_add = &global_symbols;
21766 }
21767 else
21768 {
21769 list_to_add = cu->list_in_scope;
21770 }
21771 break;
21772 case DW_TAG_inlined_subroutine:
21773 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21774 finish_block. */
21775 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21776 SYMBOL_INLINED (sym) = 1;
21777 list_to_add = cu->list_in_scope;
21778 break;
21779 case DW_TAG_template_value_param:
21780 suppress_add = 1;
21781 /* Fall through. */
21782 case DW_TAG_constant:
21783 case DW_TAG_variable:
21784 case DW_TAG_member:
21785 /* Compilation with minimal debug info may result in
21786 variables with missing type entries. Change the
21787 misleading `void' type to something sensible. */
21788 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21789 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21790
21791 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21792 /* In the case of DW_TAG_member, we should only be called for
21793 static const members. */
21794 if (die->tag == DW_TAG_member)
21795 {
21796 /* dwarf2_add_field uses die_is_declaration,
21797 so we do the same. */
21798 gdb_assert (die_is_declaration (die, cu));
21799 gdb_assert (attr);
21800 }
21801 if (attr)
21802 {
21803 dwarf2_const_value (attr, sym, cu);
21804 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21805 if (!suppress_add)
21806 {
21807 if (attr2 && (DW_UNSND (attr2) != 0))
21808 list_to_add = &global_symbols;
21809 else
21810 list_to_add = cu->list_in_scope;
21811 }
21812 break;
21813 }
21814 attr = dwarf2_attr (die, DW_AT_location, cu);
21815 if (attr)
21816 {
21817 var_decode_location (attr, sym, cu);
21818 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21819
21820 /* Fortran explicitly imports any global symbols to the local
21821 scope by DW_TAG_common_block. */
21822 if (cu->language == language_fortran && die->parent
21823 && die->parent->tag == DW_TAG_common_block)
21824 attr2 = NULL;
21825
21826 if (SYMBOL_CLASS (sym) == LOC_STATIC
21827 && SYMBOL_VALUE_ADDRESS (sym) == 0
21828 && !dwarf2_per_objfile->has_section_at_zero)
21829 {
21830 /* When a static variable is eliminated by the linker,
21831 the corresponding debug information is not stripped
21832 out, but the variable address is set to null;
21833 do not add such variables into symbol table. */
21834 }
21835 else if (attr2 && (DW_UNSND (attr2) != 0))
21836 {
21837 /* Workaround gfortran PR debug/40040 - it uses
21838 DW_AT_location for variables in -fPIC libraries which may
21839 get overriden by other libraries/executable and get
21840 a different address. Resolve it by the minimal symbol
21841 which may come from inferior's executable using copy
21842 relocation. Make this workaround only for gfortran as for
21843 other compilers GDB cannot guess the minimal symbol
21844 Fortran mangling kind. */
21845 if (cu->language == language_fortran && die->parent
21846 && die->parent->tag == DW_TAG_module
21847 && cu->producer
21848 && startswith (cu->producer, "GNU Fortran"))
21849 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21850
21851 /* A variable with DW_AT_external is never static,
21852 but it may be block-scoped. */
21853 list_to_add = (cu->list_in_scope == &file_symbols
21854 ? &global_symbols : cu->list_in_scope);
21855 }
21856 else
21857 list_to_add = cu->list_in_scope;
21858 }
21859 else
21860 {
21861 /* We do not know the address of this symbol.
21862 If it is an external symbol and we have type information
21863 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21864 The address of the variable will then be determined from
21865 the minimal symbol table whenever the variable is
21866 referenced. */
21867 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21868
21869 /* Fortran explicitly imports any global symbols to the local
21870 scope by DW_TAG_common_block. */
21871 if (cu->language == language_fortran && die->parent
21872 && die->parent->tag == DW_TAG_common_block)
21873 {
21874 /* SYMBOL_CLASS doesn't matter here because
21875 read_common_block is going to reset it. */
21876 if (!suppress_add)
21877 list_to_add = cu->list_in_scope;
21878 }
21879 else if (attr2 && (DW_UNSND (attr2) != 0)
21880 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21881 {
21882 /* A variable with DW_AT_external is never static, but it
21883 may be block-scoped. */
21884 list_to_add = (cu->list_in_scope == &file_symbols
21885 ? &global_symbols : cu->list_in_scope);
21886
21887 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21888 }
21889 else if (!die_is_declaration (die, cu))
21890 {
21891 /* Use the default LOC_OPTIMIZED_OUT class. */
21892 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21893 if (!suppress_add)
21894 list_to_add = cu->list_in_scope;
21895 }
21896 }
21897 break;
21898 case DW_TAG_formal_parameter:
21899 /* If we are inside a function, mark this as an argument. If
21900 not, we might be looking at an argument to an inlined function
21901 when we do not have enough information to show inlined frames;
21902 pretend it's a local variable in that case so that the user can
21903 still see it. */
21904 if (context_stack_depth > 0
21905 && context_stack[context_stack_depth - 1].name != NULL)
21906 SYMBOL_IS_ARGUMENT (sym) = 1;
21907 attr = dwarf2_attr (die, DW_AT_location, cu);
21908 if (attr)
21909 {
21910 var_decode_location (attr, sym, cu);
21911 }
21912 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21913 if (attr)
21914 {
21915 dwarf2_const_value (attr, sym, cu);
21916 }
21917
21918 list_to_add = cu->list_in_scope;
21919 break;
21920 case DW_TAG_unspecified_parameters:
21921 /* From varargs functions; gdb doesn't seem to have any
21922 interest in this information, so just ignore it for now.
21923 (FIXME?) */
21924 break;
21925 case DW_TAG_template_type_param:
21926 suppress_add = 1;
21927 /* Fall through. */
21928 case DW_TAG_class_type:
21929 case DW_TAG_interface_type:
21930 case DW_TAG_structure_type:
21931 case DW_TAG_union_type:
21932 case DW_TAG_set_type:
21933 case DW_TAG_enumeration_type:
21934 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21935 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21936
21937 {
21938 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21939 really ever be static objects: otherwise, if you try
21940 to, say, break of a class's method and you're in a file
21941 which doesn't mention that class, it won't work unless
21942 the check for all static symbols in lookup_symbol_aux
21943 saves you. See the OtherFileClass tests in
21944 gdb.c++/namespace.exp. */
21945
21946 if (!suppress_add)
21947 {
21948 list_to_add = (cu->list_in_scope == &file_symbols
21949 && cu->language == language_cplus
21950 ? &global_symbols : cu->list_in_scope);
21951
21952 /* The semantics of C++ state that "struct foo {
21953 ... }" also defines a typedef for "foo". */
21954 if (cu->language == language_cplus
21955 || cu->language == language_ada
21956 || cu->language == language_d
21957 || cu->language == language_rust)
21958 {
21959 /* The symbol's name is already allocated along
21960 with this objfile, so we don't need to
21961 duplicate it for the type. */
21962 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21963 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21964 }
21965 }
21966 }
21967 break;
21968 case DW_TAG_typedef:
21969 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21970 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21971 list_to_add = cu->list_in_scope;
21972 break;
21973 case DW_TAG_base_type:
21974 case DW_TAG_subrange_type:
21975 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21976 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21977 list_to_add = cu->list_in_scope;
21978 break;
21979 case DW_TAG_enumerator:
21980 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21981 if (attr)
21982 {
21983 dwarf2_const_value (attr, sym, cu);
21984 }
21985 {
21986 /* NOTE: carlton/2003-11-10: See comment above in the
21987 DW_TAG_class_type, etc. block. */
21988
21989 list_to_add = (cu->list_in_scope == &file_symbols
21990 && cu->language == language_cplus
21991 ? &global_symbols : cu->list_in_scope);
21992 }
21993 break;
21994 case DW_TAG_imported_declaration:
21995 case DW_TAG_namespace:
21996 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21997 list_to_add = &global_symbols;
21998 break;
21999 case DW_TAG_module:
22000 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22001 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22002 list_to_add = &global_symbols;
22003 break;
22004 case DW_TAG_common_block:
22005 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22006 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22007 add_symbol_to_list (sym, cu->list_in_scope);
22008 break;
22009 default:
22010 /* Not a tag we recognize. Hopefully we aren't processing
22011 trash data, but since we must specifically ignore things
22012 we don't recognize, there is nothing else we should do at
22013 this point. */
22014 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
22015 dwarf_tag_name (die->tag));
22016 break;
22017 }
22018
22019 if (suppress_add)
22020 {
22021 sym->hash_next = objfile->template_symbols;
22022 objfile->template_symbols = sym;
22023 list_to_add = NULL;
22024 }
22025
22026 if (list_to_add != NULL)
22027 add_symbol_to_list (sym, list_to_add);
22028
22029 /* For the benefit of old versions of GCC, check for anonymous
22030 namespaces based on the demangled name. */
22031 if (!cu->processing_has_namespace_info
22032 && cu->language == language_cplus)
22033 cp_scan_for_anonymous_namespaces (sym, objfile);
22034 }
22035 return (sym);
22036 }
22037
22038 /* Given an attr with a DW_FORM_dataN value in host byte order,
22039 zero-extend it as appropriate for the symbol's type. The DWARF
22040 standard (v4) is not entirely clear about the meaning of using
22041 DW_FORM_dataN for a constant with a signed type, where the type is
22042 wider than the data. The conclusion of a discussion on the DWARF
22043 list was that this is unspecified. We choose to always zero-extend
22044 because that is the interpretation long in use by GCC. */
22045
22046 static gdb_byte *
22047 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22048 struct dwarf2_cu *cu, LONGEST *value, int bits)
22049 {
22050 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22051 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22052 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22053 LONGEST l = DW_UNSND (attr);
22054
22055 if (bits < sizeof (*value) * 8)
22056 {
22057 l &= ((LONGEST) 1 << bits) - 1;
22058 *value = l;
22059 }
22060 else if (bits == sizeof (*value) * 8)
22061 *value = l;
22062 else
22063 {
22064 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22065 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22066 return bytes;
22067 }
22068
22069 return NULL;
22070 }
22071
22072 /* Read a constant value from an attribute. Either set *VALUE, or if
22073 the value does not fit in *VALUE, set *BYTES - either already
22074 allocated on the objfile obstack, or newly allocated on OBSTACK,
22075 or, set *BATON, if we translated the constant to a location
22076 expression. */
22077
22078 static void
22079 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22080 const char *name, struct obstack *obstack,
22081 struct dwarf2_cu *cu,
22082 LONGEST *value, const gdb_byte **bytes,
22083 struct dwarf2_locexpr_baton **baton)
22084 {
22085 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22086 struct comp_unit_head *cu_header = &cu->header;
22087 struct dwarf_block *blk;
22088 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22089 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22090
22091 *value = 0;
22092 *bytes = NULL;
22093 *baton = NULL;
22094
22095 switch (attr->form)
22096 {
22097 case DW_FORM_addr:
22098 case DW_FORM_GNU_addr_index:
22099 {
22100 gdb_byte *data;
22101
22102 if (TYPE_LENGTH (type) != cu_header->addr_size)
22103 dwarf2_const_value_length_mismatch_complaint (name,
22104 cu_header->addr_size,
22105 TYPE_LENGTH (type));
22106 /* Symbols of this form are reasonably rare, so we just
22107 piggyback on the existing location code rather than writing
22108 a new implementation of symbol_computed_ops. */
22109 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22110 (*baton)->per_cu = cu->per_cu;
22111 gdb_assert ((*baton)->per_cu);
22112
22113 (*baton)->size = 2 + cu_header->addr_size;
22114 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22115 (*baton)->data = data;
22116
22117 data[0] = DW_OP_addr;
22118 store_unsigned_integer (&data[1], cu_header->addr_size,
22119 byte_order, DW_ADDR (attr));
22120 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22121 }
22122 break;
22123 case DW_FORM_string:
22124 case DW_FORM_strp:
22125 case DW_FORM_GNU_str_index:
22126 case DW_FORM_GNU_strp_alt:
22127 /* DW_STRING is already allocated on the objfile obstack, point
22128 directly to it. */
22129 *bytes = (const gdb_byte *) DW_STRING (attr);
22130 break;
22131 case DW_FORM_block1:
22132 case DW_FORM_block2:
22133 case DW_FORM_block4:
22134 case DW_FORM_block:
22135 case DW_FORM_exprloc:
22136 case DW_FORM_data16:
22137 blk = DW_BLOCK (attr);
22138 if (TYPE_LENGTH (type) != blk->size)
22139 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22140 TYPE_LENGTH (type));
22141 *bytes = blk->data;
22142 break;
22143
22144 /* The DW_AT_const_value attributes are supposed to carry the
22145 symbol's value "represented as it would be on the target
22146 architecture." By the time we get here, it's already been
22147 converted to host endianness, so we just need to sign- or
22148 zero-extend it as appropriate. */
22149 case DW_FORM_data1:
22150 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22151 break;
22152 case DW_FORM_data2:
22153 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22154 break;
22155 case DW_FORM_data4:
22156 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22157 break;
22158 case DW_FORM_data8:
22159 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22160 break;
22161
22162 case DW_FORM_sdata:
22163 case DW_FORM_implicit_const:
22164 *value = DW_SND (attr);
22165 break;
22166
22167 case DW_FORM_udata:
22168 *value = DW_UNSND (attr);
22169 break;
22170
22171 default:
22172 complaint (&symfile_complaints,
22173 _("unsupported const value attribute form: '%s'"),
22174 dwarf_form_name (attr->form));
22175 *value = 0;
22176 break;
22177 }
22178 }
22179
22180
22181 /* Copy constant value from an attribute to a symbol. */
22182
22183 static void
22184 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22185 struct dwarf2_cu *cu)
22186 {
22187 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22188 LONGEST value;
22189 const gdb_byte *bytes;
22190 struct dwarf2_locexpr_baton *baton;
22191
22192 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22193 SYMBOL_PRINT_NAME (sym),
22194 &objfile->objfile_obstack, cu,
22195 &value, &bytes, &baton);
22196
22197 if (baton != NULL)
22198 {
22199 SYMBOL_LOCATION_BATON (sym) = baton;
22200 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22201 }
22202 else if (bytes != NULL)
22203 {
22204 SYMBOL_VALUE_BYTES (sym) = bytes;
22205 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22206 }
22207 else
22208 {
22209 SYMBOL_VALUE (sym) = value;
22210 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22211 }
22212 }
22213
22214 /* Return the type of the die in question using its DW_AT_type attribute. */
22215
22216 static struct type *
22217 die_type (struct die_info *die, struct dwarf2_cu *cu)
22218 {
22219 struct attribute *type_attr;
22220
22221 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22222 if (!type_attr)
22223 {
22224 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22225 /* A missing DW_AT_type represents a void type. */
22226 return objfile_type (objfile)->builtin_void;
22227 }
22228
22229 return lookup_die_type (die, type_attr, cu);
22230 }
22231
22232 /* True iff CU's producer generates GNAT Ada auxiliary information
22233 that allows to find parallel types through that information instead
22234 of having to do expensive parallel lookups by type name. */
22235
22236 static int
22237 need_gnat_info (struct dwarf2_cu *cu)
22238 {
22239 /* Assume that the Ada compiler was GNAT, which always produces
22240 the auxiliary information. */
22241 return (cu->language == language_ada);
22242 }
22243
22244 /* Return the auxiliary type of the die in question using its
22245 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22246 attribute is not present. */
22247
22248 static struct type *
22249 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22250 {
22251 struct attribute *type_attr;
22252
22253 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22254 if (!type_attr)
22255 return NULL;
22256
22257 return lookup_die_type (die, type_attr, cu);
22258 }
22259
22260 /* If DIE has a descriptive_type attribute, then set the TYPE's
22261 descriptive type accordingly. */
22262
22263 static void
22264 set_descriptive_type (struct type *type, struct die_info *die,
22265 struct dwarf2_cu *cu)
22266 {
22267 struct type *descriptive_type = die_descriptive_type (die, cu);
22268
22269 if (descriptive_type)
22270 {
22271 ALLOCATE_GNAT_AUX_TYPE (type);
22272 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22273 }
22274 }
22275
22276 /* Return the containing type of the die in question using its
22277 DW_AT_containing_type attribute. */
22278
22279 static struct type *
22280 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22281 {
22282 struct attribute *type_attr;
22283 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22284
22285 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22286 if (!type_attr)
22287 error (_("Dwarf Error: Problem turning containing type into gdb type "
22288 "[in module %s]"), objfile_name (objfile));
22289
22290 return lookup_die_type (die, type_attr, cu);
22291 }
22292
22293 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22294
22295 static struct type *
22296 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22297 {
22298 struct dwarf2_per_objfile *dwarf2_per_objfile
22299 = cu->per_cu->dwarf2_per_objfile;
22300 struct objfile *objfile = dwarf2_per_objfile->objfile;
22301 char *message, *saved;
22302
22303 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
22304 objfile_name (objfile),
22305 sect_offset_str (cu->header.sect_off),
22306 sect_offset_str (die->sect_off));
22307 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
22308 message, strlen (message));
22309 xfree (message);
22310
22311 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22312 }
22313
22314 /* Look up the type of DIE in CU using its type attribute ATTR.
22315 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22316 DW_AT_containing_type.
22317 If there is no type substitute an error marker. */
22318
22319 static struct type *
22320 lookup_die_type (struct die_info *die, const struct attribute *attr,
22321 struct dwarf2_cu *cu)
22322 {
22323 struct dwarf2_per_objfile *dwarf2_per_objfile
22324 = cu->per_cu->dwarf2_per_objfile;
22325 struct objfile *objfile = dwarf2_per_objfile->objfile;
22326 struct type *this_type;
22327
22328 gdb_assert (attr->name == DW_AT_type
22329 || attr->name == DW_AT_GNAT_descriptive_type
22330 || attr->name == DW_AT_containing_type);
22331
22332 /* First see if we have it cached. */
22333
22334 if (attr->form == DW_FORM_GNU_ref_alt)
22335 {
22336 struct dwarf2_per_cu_data *per_cu;
22337 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22338
22339 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22340 dwarf2_per_objfile);
22341 this_type = get_die_type_at_offset (sect_off, per_cu);
22342 }
22343 else if (attr_form_is_ref (attr))
22344 {
22345 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22346
22347 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22348 }
22349 else if (attr->form == DW_FORM_ref_sig8)
22350 {
22351 ULONGEST signature = DW_SIGNATURE (attr);
22352
22353 return get_signatured_type (die, signature, cu);
22354 }
22355 else
22356 {
22357 complaint (&symfile_complaints,
22358 _("Dwarf Error: Bad type attribute %s in DIE"
22359 " at %s [in module %s]"),
22360 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22361 objfile_name (objfile));
22362 return build_error_marker_type (cu, die);
22363 }
22364
22365 /* If not cached we need to read it in. */
22366
22367 if (this_type == NULL)
22368 {
22369 struct die_info *type_die = NULL;
22370 struct dwarf2_cu *type_cu = cu;
22371
22372 if (attr_form_is_ref (attr))
22373 type_die = follow_die_ref (die, attr, &type_cu);
22374 if (type_die == NULL)
22375 return build_error_marker_type (cu, die);
22376 /* If we find the type now, it's probably because the type came
22377 from an inter-CU reference and the type's CU got expanded before
22378 ours. */
22379 this_type = read_type_die (type_die, type_cu);
22380 }
22381
22382 /* If we still don't have a type use an error marker. */
22383
22384 if (this_type == NULL)
22385 return build_error_marker_type (cu, die);
22386
22387 return this_type;
22388 }
22389
22390 /* Return the type in DIE, CU.
22391 Returns NULL for invalid types.
22392
22393 This first does a lookup in die_type_hash,
22394 and only reads the die in if necessary.
22395
22396 NOTE: This can be called when reading in partial or full symbols. */
22397
22398 static struct type *
22399 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22400 {
22401 struct type *this_type;
22402
22403 this_type = get_die_type (die, cu);
22404 if (this_type)
22405 return this_type;
22406
22407 return read_type_die_1 (die, cu);
22408 }
22409
22410 /* Read the type in DIE, CU.
22411 Returns NULL for invalid types. */
22412
22413 static struct type *
22414 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22415 {
22416 struct type *this_type = NULL;
22417
22418 switch (die->tag)
22419 {
22420 case DW_TAG_class_type:
22421 case DW_TAG_interface_type:
22422 case DW_TAG_structure_type:
22423 case DW_TAG_union_type:
22424 this_type = read_structure_type (die, cu);
22425 break;
22426 case DW_TAG_enumeration_type:
22427 this_type = read_enumeration_type (die, cu);
22428 break;
22429 case DW_TAG_subprogram:
22430 case DW_TAG_subroutine_type:
22431 case DW_TAG_inlined_subroutine:
22432 this_type = read_subroutine_type (die, cu);
22433 break;
22434 case DW_TAG_array_type:
22435 this_type = read_array_type (die, cu);
22436 break;
22437 case DW_TAG_set_type:
22438 this_type = read_set_type (die, cu);
22439 break;
22440 case DW_TAG_pointer_type:
22441 this_type = read_tag_pointer_type (die, cu);
22442 break;
22443 case DW_TAG_ptr_to_member_type:
22444 this_type = read_tag_ptr_to_member_type (die, cu);
22445 break;
22446 case DW_TAG_reference_type:
22447 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22448 break;
22449 case DW_TAG_rvalue_reference_type:
22450 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22451 break;
22452 case DW_TAG_const_type:
22453 this_type = read_tag_const_type (die, cu);
22454 break;
22455 case DW_TAG_volatile_type:
22456 this_type = read_tag_volatile_type (die, cu);
22457 break;
22458 case DW_TAG_restrict_type:
22459 this_type = read_tag_restrict_type (die, cu);
22460 break;
22461 case DW_TAG_string_type:
22462 this_type = read_tag_string_type (die, cu);
22463 break;
22464 case DW_TAG_typedef:
22465 this_type = read_typedef (die, cu);
22466 break;
22467 case DW_TAG_subrange_type:
22468 this_type = read_subrange_type (die, cu);
22469 break;
22470 case DW_TAG_base_type:
22471 this_type = read_base_type (die, cu);
22472 break;
22473 case DW_TAG_unspecified_type:
22474 this_type = read_unspecified_type (die, cu);
22475 break;
22476 case DW_TAG_namespace:
22477 this_type = read_namespace_type (die, cu);
22478 break;
22479 case DW_TAG_module:
22480 this_type = read_module_type (die, cu);
22481 break;
22482 case DW_TAG_atomic_type:
22483 this_type = read_tag_atomic_type (die, cu);
22484 break;
22485 default:
22486 complaint (&symfile_complaints,
22487 _("unexpected tag in read_type_die: '%s'"),
22488 dwarf_tag_name (die->tag));
22489 break;
22490 }
22491
22492 return this_type;
22493 }
22494
22495 /* See if we can figure out if the class lives in a namespace. We do
22496 this by looking for a member function; its demangled name will
22497 contain namespace info, if there is any.
22498 Return the computed name or NULL.
22499 Space for the result is allocated on the objfile's obstack.
22500 This is the full-die version of guess_partial_die_structure_name.
22501 In this case we know DIE has no useful parent. */
22502
22503 static char *
22504 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22505 {
22506 struct die_info *spec_die;
22507 struct dwarf2_cu *spec_cu;
22508 struct die_info *child;
22509 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22510
22511 spec_cu = cu;
22512 spec_die = die_specification (die, &spec_cu);
22513 if (spec_die != NULL)
22514 {
22515 die = spec_die;
22516 cu = spec_cu;
22517 }
22518
22519 for (child = die->child;
22520 child != NULL;
22521 child = child->sibling)
22522 {
22523 if (child->tag == DW_TAG_subprogram)
22524 {
22525 const char *linkage_name = dw2_linkage_name (child, cu);
22526
22527 if (linkage_name != NULL)
22528 {
22529 char *actual_name
22530 = language_class_name_from_physname (cu->language_defn,
22531 linkage_name);
22532 char *name = NULL;
22533
22534 if (actual_name != NULL)
22535 {
22536 const char *die_name = dwarf2_name (die, cu);
22537
22538 if (die_name != NULL
22539 && strcmp (die_name, actual_name) != 0)
22540 {
22541 /* Strip off the class name from the full name.
22542 We want the prefix. */
22543 int die_name_len = strlen (die_name);
22544 int actual_name_len = strlen (actual_name);
22545
22546 /* Test for '::' as a sanity check. */
22547 if (actual_name_len > die_name_len + 2
22548 && actual_name[actual_name_len
22549 - die_name_len - 1] == ':')
22550 name = (char *) obstack_copy0 (
22551 &objfile->per_bfd->storage_obstack,
22552 actual_name, actual_name_len - die_name_len - 2);
22553 }
22554 }
22555 xfree (actual_name);
22556 return name;
22557 }
22558 }
22559 }
22560
22561 return NULL;
22562 }
22563
22564 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22565 prefix part in such case. See
22566 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22567
22568 static const char *
22569 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22570 {
22571 struct attribute *attr;
22572 const char *base;
22573
22574 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22575 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22576 return NULL;
22577
22578 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22579 return NULL;
22580
22581 attr = dw2_linkage_name_attr (die, cu);
22582 if (attr == NULL || DW_STRING (attr) == NULL)
22583 return NULL;
22584
22585 /* dwarf2_name had to be already called. */
22586 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22587
22588 /* Strip the base name, keep any leading namespaces/classes. */
22589 base = strrchr (DW_STRING (attr), ':');
22590 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22591 return "";
22592
22593 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22594 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22595 DW_STRING (attr),
22596 &base[-1] - DW_STRING (attr));
22597 }
22598
22599 /* Return the name of the namespace/class that DIE is defined within,
22600 or "" if we can't tell. The caller should not xfree the result.
22601
22602 For example, if we're within the method foo() in the following
22603 code:
22604
22605 namespace N {
22606 class C {
22607 void foo () {
22608 }
22609 };
22610 }
22611
22612 then determine_prefix on foo's die will return "N::C". */
22613
22614 static const char *
22615 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22616 {
22617 struct dwarf2_per_objfile *dwarf2_per_objfile
22618 = cu->per_cu->dwarf2_per_objfile;
22619 struct die_info *parent, *spec_die;
22620 struct dwarf2_cu *spec_cu;
22621 struct type *parent_type;
22622 const char *retval;
22623
22624 if (cu->language != language_cplus
22625 && cu->language != language_fortran && cu->language != language_d
22626 && cu->language != language_rust)
22627 return "";
22628
22629 retval = anonymous_struct_prefix (die, cu);
22630 if (retval)
22631 return retval;
22632
22633 /* We have to be careful in the presence of DW_AT_specification.
22634 For example, with GCC 3.4, given the code
22635
22636 namespace N {
22637 void foo() {
22638 // Definition of N::foo.
22639 }
22640 }
22641
22642 then we'll have a tree of DIEs like this:
22643
22644 1: DW_TAG_compile_unit
22645 2: DW_TAG_namespace // N
22646 3: DW_TAG_subprogram // declaration of N::foo
22647 4: DW_TAG_subprogram // definition of N::foo
22648 DW_AT_specification // refers to die #3
22649
22650 Thus, when processing die #4, we have to pretend that we're in
22651 the context of its DW_AT_specification, namely the contex of die
22652 #3. */
22653 spec_cu = cu;
22654 spec_die = die_specification (die, &spec_cu);
22655 if (spec_die == NULL)
22656 parent = die->parent;
22657 else
22658 {
22659 parent = spec_die->parent;
22660 cu = spec_cu;
22661 }
22662
22663 if (parent == NULL)
22664 return "";
22665 else if (parent->building_fullname)
22666 {
22667 const char *name;
22668 const char *parent_name;
22669
22670 /* It has been seen on RealView 2.2 built binaries,
22671 DW_TAG_template_type_param types actually _defined_ as
22672 children of the parent class:
22673
22674 enum E {};
22675 template class <class Enum> Class{};
22676 Class<enum E> class_e;
22677
22678 1: DW_TAG_class_type (Class)
22679 2: DW_TAG_enumeration_type (E)
22680 3: DW_TAG_enumerator (enum1:0)
22681 3: DW_TAG_enumerator (enum2:1)
22682 ...
22683 2: DW_TAG_template_type_param
22684 DW_AT_type DW_FORM_ref_udata (E)
22685
22686 Besides being broken debug info, it can put GDB into an
22687 infinite loop. Consider:
22688
22689 When we're building the full name for Class<E>, we'll start
22690 at Class, and go look over its template type parameters,
22691 finding E. We'll then try to build the full name of E, and
22692 reach here. We're now trying to build the full name of E,
22693 and look over the parent DIE for containing scope. In the
22694 broken case, if we followed the parent DIE of E, we'd again
22695 find Class, and once again go look at its template type
22696 arguments, etc., etc. Simply don't consider such parent die
22697 as source-level parent of this die (it can't be, the language
22698 doesn't allow it), and break the loop here. */
22699 name = dwarf2_name (die, cu);
22700 parent_name = dwarf2_name (parent, cu);
22701 complaint (&symfile_complaints,
22702 _("template param type '%s' defined within parent '%s'"),
22703 name ? name : "<unknown>",
22704 parent_name ? parent_name : "<unknown>");
22705 return "";
22706 }
22707 else
22708 switch (parent->tag)
22709 {
22710 case DW_TAG_namespace:
22711 parent_type = read_type_die (parent, cu);
22712 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22713 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22714 Work around this problem here. */
22715 if (cu->language == language_cplus
22716 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22717 return "";
22718 /* We give a name to even anonymous namespaces. */
22719 return TYPE_TAG_NAME (parent_type);
22720 case DW_TAG_class_type:
22721 case DW_TAG_interface_type:
22722 case DW_TAG_structure_type:
22723 case DW_TAG_union_type:
22724 case DW_TAG_module:
22725 parent_type = read_type_die (parent, cu);
22726 if (TYPE_TAG_NAME (parent_type) != NULL)
22727 return TYPE_TAG_NAME (parent_type);
22728 else
22729 /* An anonymous structure is only allowed non-static data
22730 members; no typedefs, no member functions, et cetera.
22731 So it does not need a prefix. */
22732 return "";
22733 case DW_TAG_compile_unit:
22734 case DW_TAG_partial_unit:
22735 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22736 if (cu->language == language_cplus
22737 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22738 && die->child != NULL
22739 && (die->tag == DW_TAG_class_type
22740 || die->tag == DW_TAG_structure_type
22741 || die->tag == DW_TAG_union_type))
22742 {
22743 char *name = guess_full_die_structure_name (die, cu);
22744 if (name != NULL)
22745 return name;
22746 }
22747 return "";
22748 case DW_TAG_enumeration_type:
22749 parent_type = read_type_die (parent, cu);
22750 if (TYPE_DECLARED_CLASS (parent_type))
22751 {
22752 if (TYPE_TAG_NAME (parent_type) != NULL)
22753 return TYPE_TAG_NAME (parent_type);
22754 return "";
22755 }
22756 /* Fall through. */
22757 default:
22758 return determine_prefix (parent, cu);
22759 }
22760 }
22761
22762 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22763 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22764 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22765 an obconcat, otherwise allocate storage for the result. The CU argument is
22766 used to determine the language and hence, the appropriate separator. */
22767
22768 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22769
22770 static char *
22771 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22772 int physname, struct dwarf2_cu *cu)
22773 {
22774 const char *lead = "";
22775 const char *sep;
22776
22777 if (suffix == NULL || suffix[0] == '\0'
22778 || prefix == NULL || prefix[0] == '\0')
22779 sep = "";
22780 else if (cu->language == language_d)
22781 {
22782 /* For D, the 'main' function could be defined in any module, but it
22783 should never be prefixed. */
22784 if (strcmp (suffix, "D main") == 0)
22785 {
22786 prefix = "";
22787 sep = "";
22788 }
22789 else
22790 sep = ".";
22791 }
22792 else if (cu->language == language_fortran && physname)
22793 {
22794 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22795 DW_AT_MIPS_linkage_name is preferred and used instead. */
22796
22797 lead = "__";
22798 sep = "_MOD_";
22799 }
22800 else
22801 sep = "::";
22802
22803 if (prefix == NULL)
22804 prefix = "";
22805 if (suffix == NULL)
22806 suffix = "";
22807
22808 if (obs == NULL)
22809 {
22810 char *retval
22811 = ((char *)
22812 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22813
22814 strcpy (retval, lead);
22815 strcat (retval, prefix);
22816 strcat (retval, sep);
22817 strcat (retval, suffix);
22818 return retval;
22819 }
22820 else
22821 {
22822 /* We have an obstack. */
22823 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22824 }
22825 }
22826
22827 /* Return sibling of die, NULL if no sibling. */
22828
22829 static struct die_info *
22830 sibling_die (struct die_info *die)
22831 {
22832 return die->sibling;
22833 }
22834
22835 /* Get name of a die, return NULL if not found. */
22836
22837 static const char *
22838 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22839 struct obstack *obstack)
22840 {
22841 if (name && cu->language == language_cplus)
22842 {
22843 std::string canon_name = cp_canonicalize_string (name);
22844
22845 if (!canon_name.empty ())
22846 {
22847 if (canon_name != name)
22848 name = (const char *) obstack_copy0 (obstack,
22849 canon_name.c_str (),
22850 canon_name.length ());
22851 }
22852 }
22853
22854 return name;
22855 }
22856
22857 /* Get name of a die, return NULL if not found.
22858 Anonymous namespaces are converted to their magic string. */
22859
22860 static const char *
22861 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22862 {
22863 struct attribute *attr;
22864 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22865
22866 attr = dwarf2_attr (die, DW_AT_name, cu);
22867 if ((!attr || !DW_STRING (attr))
22868 && die->tag != DW_TAG_namespace
22869 && die->tag != DW_TAG_class_type
22870 && die->tag != DW_TAG_interface_type
22871 && die->tag != DW_TAG_structure_type
22872 && die->tag != DW_TAG_union_type)
22873 return NULL;
22874
22875 switch (die->tag)
22876 {
22877 case DW_TAG_compile_unit:
22878 case DW_TAG_partial_unit:
22879 /* Compilation units have a DW_AT_name that is a filename, not
22880 a source language identifier. */
22881 case DW_TAG_enumeration_type:
22882 case DW_TAG_enumerator:
22883 /* These tags always have simple identifiers already; no need
22884 to canonicalize them. */
22885 return DW_STRING (attr);
22886
22887 case DW_TAG_namespace:
22888 if (attr != NULL && DW_STRING (attr) != NULL)
22889 return DW_STRING (attr);
22890 return CP_ANONYMOUS_NAMESPACE_STR;
22891
22892 case DW_TAG_class_type:
22893 case DW_TAG_interface_type:
22894 case DW_TAG_structure_type:
22895 case DW_TAG_union_type:
22896 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22897 structures or unions. These were of the form "._%d" in GCC 4.1,
22898 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22899 and GCC 4.4. We work around this problem by ignoring these. */
22900 if (attr && DW_STRING (attr)
22901 && (startswith (DW_STRING (attr), "._")
22902 || startswith (DW_STRING (attr), "<anonymous")))
22903 return NULL;
22904
22905 /* GCC might emit a nameless typedef that has a linkage name. See
22906 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22907 if (!attr || DW_STRING (attr) == NULL)
22908 {
22909 char *demangled = NULL;
22910
22911 attr = dw2_linkage_name_attr (die, cu);
22912 if (attr == NULL || DW_STRING (attr) == NULL)
22913 return NULL;
22914
22915 /* Avoid demangling DW_STRING (attr) the second time on a second
22916 call for the same DIE. */
22917 if (!DW_STRING_IS_CANONICAL (attr))
22918 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22919
22920 if (demangled)
22921 {
22922 const char *base;
22923
22924 /* FIXME: we already did this for the partial symbol... */
22925 DW_STRING (attr)
22926 = ((const char *)
22927 obstack_copy0 (&objfile->per_bfd->storage_obstack,
22928 demangled, strlen (demangled)));
22929 DW_STRING_IS_CANONICAL (attr) = 1;
22930 xfree (demangled);
22931
22932 /* Strip any leading namespaces/classes, keep only the base name.
22933 DW_AT_name for named DIEs does not contain the prefixes. */
22934 base = strrchr (DW_STRING (attr), ':');
22935 if (base && base > DW_STRING (attr) && base[-1] == ':')
22936 return &base[1];
22937 else
22938 return DW_STRING (attr);
22939 }
22940 }
22941 break;
22942
22943 default:
22944 break;
22945 }
22946
22947 if (!DW_STRING_IS_CANONICAL (attr))
22948 {
22949 DW_STRING (attr)
22950 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22951 &objfile->per_bfd->storage_obstack);
22952 DW_STRING_IS_CANONICAL (attr) = 1;
22953 }
22954 return DW_STRING (attr);
22955 }
22956
22957 /* Return the die that this die in an extension of, or NULL if there
22958 is none. *EXT_CU is the CU containing DIE on input, and the CU
22959 containing the return value on output. */
22960
22961 static struct die_info *
22962 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22963 {
22964 struct attribute *attr;
22965
22966 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22967 if (attr == NULL)
22968 return NULL;
22969
22970 return follow_die_ref (die, attr, ext_cu);
22971 }
22972
22973 /* Convert a DIE tag into its string name. */
22974
22975 static const char *
22976 dwarf_tag_name (unsigned tag)
22977 {
22978 const char *name = get_DW_TAG_name (tag);
22979
22980 if (name == NULL)
22981 return "DW_TAG_<unknown>";
22982
22983 return name;
22984 }
22985
22986 /* Convert a DWARF attribute code into its string name. */
22987
22988 static const char *
22989 dwarf_attr_name (unsigned attr)
22990 {
22991 const char *name;
22992
22993 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22994 if (attr == DW_AT_MIPS_fde)
22995 return "DW_AT_MIPS_fde";
22996 #else
22997 if (attr == DW_AT_HP_block_index)
22998 return "DW_AT_HP_block_index";
22999 #endif
23000
23001 name = get_DW_AT_name (attr);
23002
23003 if (name == NULL)
23004 return "DW_AT_<unknown>";
23005
23006 return name;
23007 }
23008
23009 /* Convert a DWARF value form code into its string name. */
23010
23011 static const char *
23012 dwarf_form_name (unsigned form)
23013 {
23014 const char *name = get_DW_FORM_name (form);
23015
23016 if (name == NULL)
23017 return "DW_FORM_<unknown>";
23018
23019 return name;
23020 }
23021
23022 static const char *
23023 dwarf_bool_name (unsigned mybool)
23024 {
23025 if (mybool)
23026 return "TRUE";
23027 else
23028 return "FALSE";
23029 }
23030
23031 /* Convert a DWARF type code into its string name. */
23032
23033 static const char *
23034 dwarf_type_encoding_name (unsigned enc)
23035 {
23036 const char *name = get_DW_ATE_name (enc);
23037
23038 if (name == NULL)
23039 return "DW_ATE_<unknown>";
23040
23041 return name;
23042 }
23043
23044 static void
23045 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23046 {
23047 unsigned int i;
23048
23049 print_spaces (indent, f);
23050 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23051 dwarf_tag_name (die->tag), die->abbrev,
23052 sect_offset_str (die->sect_off));
23053
23054 if (die->parent != NULL)
23055 {
23056 print_spaces (indent, f);
23057 fprintf_unfiltered (f, " parent at offset: %s\n",
23058 sect_offset_str (die->parent->sect_off));
23059 }
23060
23061 print_spaces (indent, f);
23062 fprintf_unfiltered (f, " has children: %s\n",
23063 dwarf_bool_name (die->child != NULL));
23064
23065 print_spaces (indent, f);
23066 fprintf_unfiltered (f, " attributes:\n");
23067
23068 for (i = 0; i < die->num_attrs; ++i)
23069 {
23070 print_spaces (indent, f);
23071 fprintf_unfiltered (f, " %s (%s) ",
23072 dwarf_attr_name (die->attrs[i].name),
23073 dwarf_form_name (die->attrs[i].form));
23074
23075 switch (die->attrs[i].form)
23076 {
23077 case DW_FORM_addr:
23078 case DW_FORM_GNU_addr_index:
23079 fprintf_unfiltered (f, "address: ");
23080 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
23081 break;
23082 case DW_FORM_block2:
23083 case DW_FORM_block4:
23084 case DW_FORM_block:
23085 case DW_FORM_block1:
23086 fprintf_unfiltered (f, "block: size %s",
23087 pulongest (DW_BLOCK (&die->attrs[i])->size));
23088 break;
23089 case DW_FORM_exprloc:
23090 fprintf_unfiltered (f, "expression: size %s",
23091 pulongest (DW_BLOCK (&die->attrs[i])->size));
23092 break;
23093 case DW_FORM_data16:
23094 fprintf_unfiltered (f, "constant of 16 bytes");
23095 break;
23096 case DW_FORM_ref_addr:
23097 fprintf_unfiltered (f, "ref address: ");
23098 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23099 break;
23100 case DW_FORM_GNU_ref_alt:
23101 fprintf_unfiltered (f, "alt ref address: ");
23102 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23103 break;
23104 case DW_FORM_ref1:
23105 case DW_FORM_ref2:
23106 case DW_FORM_ref4:
23107 case DW_FORM_ref8:
23108 case DW_FORM_ref_udata:
23109 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23110 (long) (DW_UNSND (&die->attrs[i])));
23111 break;
23112 case DW_FORM_data1:
23113 case DW_FORM_data2:
23114 case DW_FORM_data4:
23115 case DW_FORM_data8:
23116 case DW_FORM_udata:
23117 case DW_FORM_sdata:
23118 fprintf_unfiltered (f, "constant: %s",
23119 pulongest (DW_UNSND (&die->attrs[i])));
23120 break;
23121 case DW_FORM_sec_offset:
23122 fprintf_unfiltered (f, "section offset: %s",
23123 pulongest (DW_UNSND (&die->attrs[i])));
23124 break;
23125 case DW_FORM_ref_sig8:
23126 fprintf_unfiltered (f, "signature: %s",
23127 hex_string (DW_SIGNATURE (&die->attrs[i])));
23128 break;
23129 case DW_FORM_string:
23130 case DW_FORM_strp:
23131 case DW_FORM_line_strp:
23132 case DW_FORM_GNU_str_index:
23133 case DW_FORM_GNU_strp_alt:
23134 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23135 DW_STRING (&die->attrs[i])
23136 ? DW_STRING (&die->attrs[i]) : "",
23137 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23138 break;
23139 case DW_FORM_flag:
23140 if (DW_UNSND (&die->attrs[i]))
23141 fprintf_unfiltered (f, "flag: TRUE");
23142 else
23143 fprintf_unfiltered (f, "flag: FALSE");
23144 break;
23145 case DW_FORM_flag_present:
23146 fprintf_unfiltered (f, "flag: TRUE");
23147 break;
23148 case DW_FORM_indirect:
23149 /* The reader will have reduced the indirect form to
23150 the "base form" so this form should not occur. */
23151 fprintf_unfiltered (f,
23152 "unexpected attribute form: DW_FORM_indirect");
23153 break;
23154 case DW_FORM_implicit_const:
23155 fprintf_unfiltered (f, "constant: %s",
23156 plongest (DW_SND (&die->attrs[i])));
23157 break;
23158 default:
23159 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23160 die->attrs[i].form);
23161 break;
23162 }
23163 fprintf_unfiltered (f, "\n");
23164 }
23165 }
23166
23167 static void
23168 dump_die_for_error (struct die_info *die)
23169 {
23170 dump_die_shallow (gdb_stderr, 0, die);
23171 }
23172
23173 static void
23174 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23175 {
23176 int indent = level * 4;
23177
23178 gdb_assert (die != NULL);
23179
23180 if (level >= max_level)
23181 return;
23182
23183 dump_die_shallow (f, indent, die);
23184
23185 if (die->child != NULL)
23186 {
23187 print_spaces (indent, f);
23188 fprintf_unfiltered (f, " Children:");
23189 if (level + 1 < max_level)
23190 {
23191 fprintf_unfiltered (f, "\n");
23192 dump_die_1 (f, level + 1, max_level, die->child);
23193 }
23194 else
23195 {
23196 fprintf_unfiltered (f,
23197 " [not printed, max nesting level reached]\n");
23198 }
23199 }
23200
23201 if (die->sibling != NULL && level > 0)
23202 {
23203 dump_die_1 (f, level, max_level, die->sibling);
23204 }
23205 }
23206
23207 /* This is called from the pdie macro in gdbinit.in.
23208 It's not static so gcc will keep a copy callable from gdb. */
23209
23210 void
23211 dump_die (struct die_info *die, int max_level)
23212 {
23213 dump_die_1 (gdb_stdlog, 0, max_level, die);
23214 }
23215
23216 static void
23217 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23218 {
23219 void **slot;
23220
23221 slot = htab_find_slot_with_hash (cu->die_hash, die,
23222 to_underlying (die->sect_off),
23223 INSERT);
23224
23225 *slot = die;
23226 }
23227
23228 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23229 required kind. */
23230
23231 static sect_offset
23232 dwarf2_get_ref_die_offset (const struct attribute *attr)
23233 {
23234 if (attr_form_is_ref (attr))
23235 return (sect_offset) DW_UNSND (attr);
23236
23237 complaint (&symfile_complaints,
23238 _("unsupported die ref attribute form: '%s'"),
23239 dwarf_form_name (attr->form));
23240 return {};
23241 }
23242
23243 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23244 * the value held by the attribute is not constant. */
23245
23246 static LONGEST
23247 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23248 {
23249 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23250 return DW_SND (attr);
23251 else if (attr->form == DW_FORM_udata
23252 || attr->form == DW_FORM_data1
23253 || attr->form == DW_FORM_data2
23254 || attr->form == DW_FORM_data4
23255 || attr->form == DW_FORM_data8)
23256 return DW_UNSND (attr);
23257 else
23258 {
23259 /* For DW_FORM_data16 see attr_form_is_constant. */
23260 complaint (&symfile_complaints,
23261 _("Attribute value is not a constant (%s)"),
23262 dwarf_form_name (attr->form));
23263 return default_value;
23264 }
23265 }
23266
23267 /* Follow reference or signature attribute ATTR of SRC_DIE.
23268 On entry *REF_CU is the CU of SRC_DIE.
23269 On exit *REF_CU is the CU of the result. */
23270
23271 static struct die_info *
23272 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23273 struct dwarf2_cu **ref_cu)
23274 {
23275 struct die_info *die;
23276
23277 if (attr_form_is_ref (attr))
23278 die = follow_die_ref (src_die, attr, ref_cu);
23279 else if (attr->form == DW_FORM_ref_sig8)
23280 die = follow_die_sig (src_die, attr, ref_cu);
23281 else
23282 {
23283 dump_die_for_error (src_die);
23284 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23285 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23286 }
23287
23288 return die;
23289 }
23290
23291 /* Follow reference OFFSET.
23292 On entry *REF_CU is the CU of the source die referencing OFFSET.
23293 On exit *REF_CU is the CU of the result.
23294 Returns NULL if OFFSET is invalid. */
23295
23296 static struct die_info *
23297 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23298 struct dwarf2_cu **ref_cu)
23299 {
23300 struct die_info temp_die;
23301 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23302 struct dwarf2_per_objfile *dwarf2_per_objfile
23303 = cu->per_cu->dwarf2_per_objfile;
23304 struct objfile *objfile = dwarf2_per_objfile->objfile;
23305
23306 gdb_assert (cu->per_cu != NULL);
23307
23308 target_cu = cu;
23309
23310 if (cu->per_cu->is_debug_types)
23311 {
23312 /* .debug_types CUs cannot reference anything outside their CU.
23313 If they need to, they have to reference a signatured type via
23314 DW_FORM_ref_sig8. */
23315 if (!offset_in_cu_p (&cu->header, sect_off))
23316 return NULL;
23317 }
23318 else if (offset_in_dwz != cu->per_cu->is_dwz
23319 || !offset_in_cu_p (&cu->header, sect_off))
23320 {
23321 struct dwarf2_per_cu_data *per_cu;
23322
23323 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23324 dwarf2_per_objfile);
23325
23326 /* If necessary, add it to the queue and load its DIEs. */
23327 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23328 load_full_comp_unit (per_cu, cu->language);
23329
23330 target_cu = per_cu->cu;
23331 }
23332 else if (cu->dies == NULL)
23333 {
23334 /* We're loading full DIEs during partial symbol reading. */
23335 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23336 load_full_comp_unit (cu->per_cu, language_minimal);
23337 }
23338
23339 *ref_cu = target_cu;
23340 temp_die.sect_off = sect_off;
23341 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23342 &temp_die,
23343 to_underlying (sect_off));
23344 }
23345
23346 /* Follow reference attribute ATTR of SRC_DIE.
23347 On entry *REF_CU is the CU of SRC_DIE.
23348 On exit *REF_CU is the CU of the result. */
23349
23350 static struct die_info *
23351 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23352 struct dwarf2_cu **ref_cu)
23353 {
23354 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23355 struct dwarf2_cu *cu = *ref_cu;
23356 struct die_info *die;
23357
23358 die = follow_die_offset (sect_off,
23359 (attr->form == DW_FORM_GNU_ref_alt
23360 || cu->per_cu->is_dwz),
23361 ref_cu);
23362 if (!die)
23363 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23364 "at %s [in module %s]"),
23365 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23366 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23367
23368 return die;
23369 }
23370
23371 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23372 Returned value is intended for DW_OP_call*. Returned
23373 dwarf2_locexpr_baton->data has lifetime of
23374 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23375
23376 struct dwarf2_locexpr_baton
23377 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23378 struct dwarf2_per_cu_data *per_cu,
23379 CORE_ADDR (*get_frame_pc) (void *baton),
23380 void *baton)
23381 {
23382 struct dwarf2_cu *cu;
23383 struct die_info *die;
23384 struct attribute *attr;
23385 struct dwarf2_locexpr_baton retval;
23386 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23387 struct dwarf2_per_objfile *dwarf2_per_objfile
23388 = get_dwarf2_per_objfile (objfile);
23389
23390 if (per_cu->cu == NULL)
23391 load_cu (per_cu);
23392 cu = per_cu->cu;
23393 if (cu == NULL)
23394 {
23395 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23396 Instead just throw an error, not much else we can do. */
23397 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23398 sect_offset_str (sect_off), objfile_name (objfile));
23399 }
23400
23401 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23402 if (!die)
23403 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23404 sect_offset_str (sect_off), objfile_name (objfile));
23405
23406 attr = dwarf2_attr (die, DW_AT_location, cu);
23407 if (!attr)
23408 {
23409 /* DWARF: "If there is no such attribute, then there is no effect.".
23410 DATA is ignored if SIZE is 0. */
23411
23412 retval.data = NULL;
23413 retval.size = 0;
23414 }
23415 else if (attr_form_is_section_offset (attr))
23416 {
23417 struct dwarf2_loclist_baton loclist_baton;
23418 CORE_ADDR pc = (*get_frame_pc) (baton);
23419 size_t size;
23420
23421 fill_in_loclist_baton (cu, &loclist_baton, attr);
23422
23423 retval.data = dwarf2_find_location_expression (&loclist_baton,
23424 &size, pc);
23425 retval.size = size;
23426 }
23427 else
23428 {
23429 if (!attr_form_is_block (attr))
23430 error (_("Dwarf Error: DIE at %s referenced in module %s "
23431 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23432 sect_offset_str (sect_off), objfile_name (objfile));
23433
23434 retval.data = DW_BLOCK (attr)->data;
23435 retval.size = DW_BLOCK (attr)->size;
23436 }
23437 retval.per_cu = cu->per_cu;
23438
23439 age_cached_comp_units (dwarf2_per_objfile);
23440
23441 return retval;
23442 }
23443
23444 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23445 offset. */
23446
23447 struct dwarf2_locexpr_baton
23448 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23449 struct dwarf2_per_cu_data *per_cu,
23450 CORE_ADDR (*get_frame_pc) (void *baton),
23451 void *baton)
23452 {
23453 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23454
23455 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23456 }
23457
23458 /* Write a constant of a given type as target-ordered bytes into
23459 OBSTACK. */
23460
23461 static const gdb_byte *
23462 write_constant_as_bytes (struct obstack *obstack,
23463 enum bfd_endian byte_order,
23464 struct type *type,
23465 ULONGEST value,
23466 LONGEST *len)
23467 {
23468 gdb_byte *result;
23469
23470 *len = TYPE_LENGTH (type);
23471 result = (gdb_byte *) obstack_alloc (obstack, *len);
23472 store_unsigned_integer (result, *len, byte_order, value);
23473
23474 return result;
23475 }
23476
23477 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23478 pointer to the constant bytes and set LEN to the length of the
23479 data. If memory is needed, allocate it on OBSTACK. If the DIE
23480 does not have a DW_AT_const_value, return NULL. */
23481
23482 const gdb_byte *
23483 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23484 struct dwarf2_per_cu_data *per_cu,
23485 struct obstack *obstack,
23486 LONGEST *len)
23487 {
23488 struct dwarf2_cu *cu;
23489 struct die_info *die;
23490 struct attribute *attr;
23491 const gdb_byte *result = NULL;
23492 struct type *type;
23493 LONGEST value;
23494 enum bfd_endian byte_order;
23495 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23496
23497 if (per_cu->cu == NULL)
23498 load_cu (per_cu);
23499 cu = per_cu->cu;
23500 if (cu == NULL)
23501 {
23502 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23503 Instead just throw an error, not much else we can do. */
23504 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23505 sect_offset_str (sect_off), objfile_name (objfile));
23506 }
23507
23508 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23509 if (!die)
23510 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23511 sect_offset_str (sect_off), objfile_name (objfile));
23512
23513 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23514 if (attr == NULL)
23515 return NULL;
23516
23517 byte_order = (bfd_big_endian (objfile->obfd)
23518 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23519
23520 switch (attr->form)
23521 {
23522 case DW_FORM_addr:
23523 case DW_FORM_GNU_addr_index:
23524 {
23525 gdb_byte *tem;
23526
23527 *len = cu->header.addr_size;
23528 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23529 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23530 result = tem;
23531 }
23532 break;
23533 case DW_FORM_string:
23534 case DW_FORM_strp:
23535 case DW_FORM_GNU_str_index:
23536 case DW_FORM_GNU_strp_alt:
23537 /* DW_STRING is already allocated on the objfile obstack, point
23538 directly to it. */
23539 result = (const gdb_byte *) DW_STRING (attr);
23540 *len = strlen (DW_STRING (attr));
23541 break;
23542 case DW_FORM_block1:
23543 case DW_FORM_block2:
23544 case DW_FORM_block4:
23545 case DW_FORM_block:
23546 case DW_FORM_exprloc:
23547 case DW_FORM_data16:
23548 result = DW_BLOCK (attr)->data;
23549 *len = DW_BLOCK (attr)->size;
23550 break;
23551
23552 /* The DW_AT_const_value attributes are supposed to carry the
23553 symbol's value "represented as it would be on the target
23554 architecture." By the time we get here, it's already been
23555 converted to host endianness, so we just need to sign- or
23556 zero-extend it as appropriate. */
23557 case DW_FORM_data1:
23558 type = die_type (die, cu);
23559 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23560 if (result == NULL)
23561 result = write_constant_as_bytes (obstack, byte_order,
23562 type, value, len);
23563 break;
23564 case DW_FORM_data2:
23565 type = die_type (die, cu);
23566 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23567 if (result == NULL)
23568 result = write_constant_as_bytes (obstack, byte_order,
23569 type, value, len);
23570 break;
23571 case DW_FORM_data4:
23572 type = die_type (die, cu);
23573 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23574 if (result == NULL)
23575 result = write_constant_as_bytes (obstack, byte_order,
23576 type, value, len);
23577 break;
23578 case DW_FORM_data8:
23579 type = die_type (die, cu);
23580 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23581 if (result == NULL)
23582 result = write_constant_as_bytes (obstack, byte_order,
23583 type, value, len);
23584 break;
23585
23586 case DW_FORM_sdata:
23587 case DW_FORM_implicit_const:
23588 type = die_type (die, cu);
23589 result = write_constant_as_bytes (obstack, byte_order,
23590 type, DW_SND (attr), len);
23591 break;
23592
23593 case DW_FORM_udata:
23594 type = die_type (die, cu);
23595 result = write_constant_as_bytes (obstack, byte_order,
23596 type, DW_UNSND (attr), len);
23597 break;
23598
23599 default:
23600 complaint (&symfile_complaints,
23601 _("unsupported const value attribute form: '%s'"),
23602 dwarf_form_name (attr->form));
23603 break;
23604 }
23605
23606 return result;
23607 }
23608
23609 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23610 valid type for this die is found. */
23611
23612 struct type *
23613 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23614 struct dwarf2_per_cu_data *per_cu)
23615 {
23616 struct dwarf2_cu *cu;
23617 struct die_info *die;
23618
23619 if (per_cu->cu == NULL)
23620 load_cu (per_cu);
23621 cu = per_cu->cu;
23622 if (!cu)
23623 return NULL;
23624
23625 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23626 if (!die)
23627 return NULL;
23628
23629 return die_type (die, cu);
23630 }
23631
23632 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23633 PER_CU. */
23634
23635 struct type *
23636 dwarf2_get_die_type (cu_offset die_offset,
23637 struct dwarf2_per_cu_data *per_cu)
23638 {
23639 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23640 return get_die_type_at_offset (die_offset_sect, per_cu);
23641 }
23642
23643 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23644 On entry *REF_CU is the CU of SRC_DIE.
23645 On exit *REF_CU is the CU of the result.
23646 Returns NULL if the referenced DIE isn't found. */
23647
23648 static struct die_info *
23649 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23650 struct dwarf2_cu **ref_cu)
23651 {
23652 struct die_info temp_die;
23653 struct dwarf2_cu *sig_cu;
23654 struct die_info *die;
23655
23656 /* While it might be nice to assert sig_type->type == NULL here,
23657 we can get here for DW_AT_imported_declaration where we need
23658 the DIE not the type. */
23659
23660 /* If necessary, add it to the queue and load its DIEs. */
23661
23662 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23663 read_signatured_type (sig_type);
23664
23665 sig_cu = sig_type->per_cu.cu;
23666 gdb_assert (sig_cu != NULL);
23667 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23668 temp_die.sect_off = sig_type->type_offset_in_section;
23669 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23670 to_underlying (temp_die.sect_off));
23671 if (die)
23672 {
23673 struct dwarf2_per_objfile *dwarf2_per_objfile
23674 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23675
23676 /* For .gdb_index version 7 keep track of included TUs.
23677 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23678 if (dwarf2_per_objfile->index_table != NULL
23679 && dwarf2_per_objfile->index_table->version <= 7)
23680 {
23681 VEC_safe_push (dwarf2_per_cu_ptr,
23682 (*ref_cu)->per_cu->imported_symtabs,
23683 sig_cu->per_cu);
23684 }
23685
23686 *ref_cu = sig_cu;
23687 return die;
23688 }
23689
23690 return NULL;
23691 }
23692
23693 /* Follow signatured type referenced by ATTR in SRC_DIE.
23694 On entry *REF_CU is the CU of SRC_DIE.
23695 On exit *REF_CU is the CU of the result.
23696 The result is the DIE of the type.
23697 If the referenced type cannot be found an error is thrown. */
23698
23699 static struct die_info *
23700 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23701 struct dwarf2_cu **ref_cu)
23702 {
23703 ULONGEST signature = DW_SIGNATURE (attr);
23704 struct signatured_type *sig_type;
23705 struct die_info *die;
23706
23707 gdb_assert (attr->form == DW_FORM_ref_sig8);
23708
23709 sig_type = lookup_signatured_type (*ref_cu, signature);
23710 /* sig_type will be NULL if the signatured type is missing from
23711 the debug info. */
23712 if (sig_type == NULL)
23713 {
23714 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23715 " from DIE at %s [in module %s]"),
23716 hex_string (signature), sect_offset_str (src_die->sect_off),
23717 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23718 }
23719
23720 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23721 if (die == NULL)
23722 {
23723 dump_die_for_error (src_die);
23724 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23725 " from DIE at %s [in module %s]"),
23726 hex_string (signature), sect_offset_str (src_die->sect_off),
23727 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23728 }
23729
23730 return die;
23731 }
23732
23733 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23734 reading in and processing the type unit if necessary. */
23735
23736 static struct type *
23737 get_signatured_type (struct die_info *die, ULONGEST signature,
23738 struct dwarf2_cu *cu)
23739 {
23740 struct dwarf2_per_objfile *dwarf2_per_objfile
23741 = cu->per_cu->dwarf2_per_objfile;
23742 struct signatured_type *sig_type;
23743 struct dwarf2_cu *type_cu;
23744 struct die_info *type_die;
23745 struct type *type;
23746
23747 sig_type = lookup_signatured_type (cu, signature);
23748 /* sig_type will be NULL if the signatured type is missing from
23749 the debug info. */
23750 if (sig_type == NULL)
23751 {
23752 complaint (&symfile_complaints,
23753 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23754 " from DIE at %s [in module %s]"),
23755 hex_string (signature), sect_offset_str (die->sect_off),
23756 objfile_name (dwarf2_per_objfile->objfile));
23757 return build_error_marker_type (cu, die);
23758 }
23759
23760 /* If we already know the type we're done. */
23761 if (sig_type->type != NULL)
23762 return sig_type->type;
23763
23764 type_cu = cu;
23765 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23766 if (type_die != NULL)
23767 {
23768 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23769 is created. This is important, for example, because for c++ classes
23770 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23771 type = read_type_die (type_die, type_cu);
23772 if (type == NULL)
23773 {
23774 complaint (&symfile_complaints,
23775 _("Dwarf Error: Cannot build signatured type %s"
23776 " referenced from DIE at %s [in module %s]"),
23777 hex_string (signature), sect_offset_str (die->sect_off),
23778 objfile_name (dwarf2_per_objfile->objfile));
23779 type = build_error_marker_type (cu, die);
23780 }
23781 }
23782 else
23783 {
23784 complaint (&symfile_complaints,
23785 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23786 " from DIE at %s [in module %s]"),
23787 hex_string (signature), sect_offset_str (die->sect_off),
23788 objfile_name (dwarf2_per_objfile->objfile));
23789 type = build_error_marker_type (cu, die);
23790 }
23791 sig_type->type = type;
23792
23793 return type;
23794 }
23795
23796 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23797 reading in and processing the type unit if necessary. */
23798
23799 static struct type *
23800 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23801 struct dwarf2_cu *cu) /* ARI: editCase function */
23802 {
23803 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23804 if (attr_form_is_ref (attr))
23805 {
23806 struct dwarf2_cu *type_cu = cu;
23807 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23808
23809 return read_type_die (type_die, type_cu);
23810 }
23811 else if (attr->form == DW_FORM_ref_sig8)
23812 {
23813 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23814 }
23815 else
23816 {
23817 struct dwarf2_per_objfile *dwarf2_per_objfile
23818 = cu->per_cu->dwarf2_per_objfile;
23819
23820 complaint (&symfile_complaints,
23821 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23822 " at %s [in module %s]"),
23823 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23824 objfile_name (dwarf2_per_objfile->objfile));
23825 return build_error_marker_type (cu, die);
23826 }
23827 }
23828
23829 /* Load the DIEs associated with type unit PER_CU into memory. */
23830
23831 static void
23832 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23833 {
23834 struct signatured_type *sig_type;
23835
23836 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23837 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23838
23839 /* We have the per_cu, but we need the signatured_type.
23840 Fortunately this is an easy translation. */
23841 gdb_assert (per_cu->is_debug_types);
23842 sig_type = (struct signatured_type *) per_cu;
23843
23844 gdb_assert (per_cu->cu == NULL);
23845
23846 read_signatured_type (sig_type);
23847
23848 gdb_assert (per_cu->cu != NULL);
23849 }
23850
23851 /* die_reader_func for read_signatured_type.
23852 This is identical to load_full_comp_unit_reader,
23853 but is kept separate for now. */
23854
23855 static void
23856 read_signatured_type_reader (const struct die_reader_specs *reader,
23857 const gdb_byte *info_ptr,
23858 struct die_info *comp_unit_die,
23859 int has_children,
23860 void *data)
23861 {
23862 struct dwarf2_cu *cu = reader->cu;
23863
23864 gdb_assert (cu->die_hash == NULL);
23865 cu->die_hash =
23866 htab_create_alloc_ex (cu->header.length / 12,
23867 die_hash,
23868 die_eq,
23869 NULL,
23870 &cu->comp_unit_obstack,
23871 hashtab_obstack_allocate,
23872 dummy_obstack_deallocate);
23873
23874 if (has_children)
23875 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23876 &info_ptr, comp_unit_die);
23877 cu->dies = comp_unit_die;
23878 /* comp_unit_die is not stored in die_hash, no need. */
23879
23880 /* We try not to read any attributes in this function, because not
23881 all CUs needed for references have been loaded yet, and symbol
23882 table processing isn't initialized. But we have to set the CU language,
23883 or we won't be able to build types correctly.
23884 Similarly, if we do not read the producer, we can not apply
23885 producer-specific interpretation. */
23886 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23887 }
23888
23889 /* Read in a signatured type and build its CU and DIEs.
23890 If the type is a stub for the real type in a DWO file,
23891 read in the real type from the DWO file as well. */
23892
23893 static void
23894 read_signatured_type (struct signatured_type *sig_type)
23895 {
23896 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23897
23898 gdb_assert (per_cu->is_debug_types);
23899 gdb_assert (per_cu->cu == NULL);
23900
23901 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23902 read_signatured_type_reader, NULL);
23903 sig_type->per_cu.tu_read = 1;
23904 }
23905
23906 /* Decode simple location descriptions.
23907 Given a pointer to a dwarf block that defines a location, compute
23908 the location and return the value.
23909
23910 NOTE drow/2003-11-18: This function is called in two situations
23911 now: for the address of static or global variables (partial symbols
23912 only) and for offsets into structures which are expected to be
23913 (more or less) constant. The partial symbol case should go away,
23914 and only the constant case should remain. That will let this
23915 function complain more accurately. A few special modes are allowed
23916 without complaint for global variables (for instance, global
23917 register values and thread-local values).
23918
23919 A location description containing no operations indicates that the
23920 object is optimized out. The return value is 0 for that case.
23921 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23922 callers will only want a very basic result and this can become a
23923 complaint.
23924
23925 Note that stack[0] is unused except as a default error return. */
23926
23927 static CORE_ADDR
23928 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23929 {
23930 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23931 size_t i;
23932 size_t size = blk->size;
23933 const gdb_byte *data = blk->data;
23934 CORE_ADDR stack[64];
23935 int stacki;
23936 unsigned int bytes_read, unsnd;
23937 gdb_byte op;
23938
23939 i = 0;
23940 stacki = 0;
23941 stack[stacki] = 0;
23942 stack[++stacki] = 0;
23943
23944 while (i < size)
23945 {
23946 op = data[i++];
23947 switch (op)
23948 {
23949 case DW_OP_lit0:
23950 case DW_OP_lit1:
23951 case DW_OP_lit2:
23952 case DW_OP_lit3:
23953 case DW_OP_lit4:
23954 case DW_OP_lit5:
23955 case DW_OP_lit6:
23956 case DW_OP_lit7:
23957 case DW_OP_lit8:
23958 case DW_OP_lit9:
23959 case DW_OP_lit10:
23960 case DW_OP_lit11:
23961 case DW_OP_lit12:
23962 case DW_OP_lit13:
23963 case DW_OP_lit14:
23964 case DW_OP_lit15:
23965 case DW_OP_lit16:
23966 case DW_OP_lit17:
23967 case DW_OP_lit18:
23968 case DW_OP_lit19:
23969 case DW_OP_lit20:
23970 case DW_OP_lit21:
23971 case DW_OP_lit22:
23972 case DW_OP_lit23:
23973 case DW_OP_lit24:
23974 case DW_OP_lit25:
23975 case DW_OP_lit26:
23976 case DW_OP_lit27:
23977 case DW_OP_lit28:
23978 case DW_OP_lit29:
23979 case DW_OP_lit30:
23980 case DW_OP_lit31:
23981 stack[++stacki] = op - DW_OP_lit0;
23982 break;
23983
23984 case DW_OP_reg0:
23985 case DW_OP_reg1:
23986 case DW_OP_reg2:
23987 case DW_OP_reg3:
23988 case DW_OP_reg4:
23989 case DW_OP_reg5:
23990 case DW_OP_reg6:
23991 case DW_OP_reg7:
23992 case DW_OP_reg8:
23993 case DW_OP_reg9:
23994 case DW_OP_reg10:
23995 case DW_OP_reg11:
23996 case DW_OP_reg12:
23997 case DW_OP_reg13:
23998 case DW_OP_reg14:
23999 case DW_OP_reg15:
24000 case DW_OP_reg16:
24001 case DW_OP_reg17:
24002 case DW_OP_reg18:
24003 case DW_OP_reg19:
24004 case DW_OP_reg20:
24005 case DW_OP_reg21:
24006 case DW_OP_reg22:
24007 case DW_OP_reg23:
24008 case DW_OP_reg24:
24009 case DW_OP_reg25:
24010 case DW_OP_reg26:
24011 case DW_OP_reg27:
24012 case DW_OP_reg28:
24013 case DW_OP_reg29:
24014 case DW_OP_reg30:
24015 case DW_OP_reg31:
24016 stack[++stacki] = op - DW_OP_reg0;
24017 if (i < size)
24018 dwarf2_complex_location_expr_complaint ();
24019 break;
24020
24021 case DW_OP_regx:
24022 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24023 i += bytes_read;
24024 stack[++stacki] = unsnd;
24025 if (i < size)
24026 dwarf2_complex_location_expr_complaint ();
24027 break;
24028
24029 case DW_OP_addr:
24030 stack[++stacki] = read_address (objfile->obfd, &data[i],
24031 cu, &bytes_read);
24032 i += bytes_read;
24033 break;
24034
24035 case DW_OP_const1u:
24036 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24037 i += 1;
24038 break;
24039
24040 case DW_OP_const1s:
24041 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24042 i += 1;
24043 break;
24044
24045 case DW_OP_const2u:
24046 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24047 i += 2;
24048 break;
24049
24050 case DW_OP_const2s:
24051 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24052 i += 2;
24053 break;
24054
24055 case DW_OP_const4u:
24056 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24057 i += 4;
24058 break;
24059
24060 case DW_OP_const4s:
24061 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24062 i += 4;
24063 break;
24064
24065 case DW_OP_const8u:
24066 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24067 i += 8;
24068 break;
24069
24070 case DW_OP_constu:
24071 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24072 &bytes_read);
24073 i += bytes_read;
24074 break;
24075
24076 case DW_OP_consts:
24077 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24078 i += bytes_read;
24079 break;
24080
24081 case DW_OP_dup:
24082 stack[stacki + 1] = stack[stacki];
24083 stacki++;
24084 break;
24085
24086 case DW_OP_plus:
24087 stack[stacki - 1] += stack[stacki];
24088 stacki--;
24089 break;
24090
24091 case DW_OP_plus_uconst:
24092 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24093 &bytes_read);
24094 i += bytes_read;
24095 break;
24096
24097 case DW_OP_minus:
24098 stack[stacki - 1] -= stack[stacki];
24099 stacki--;
24100 break;
24101
24102 case DW_OP_deref:
24103 /* If we're not the last op, then we definitely can't encode
24104 this using GDB's address_class enum. This is valid for partial
24105 global symbols, although the variable's address will be bogus
24106 in the psymtab. */
24107 if (i < size)
24108 dwarf2_complex_location_expr_complaint ();
24109 break;
24110
24111 case DW_OP_GNU_push_tls_address:
24112 case DW_OP_form_tls_address:
24113 /* The top of the stack has the offset from the beginning
24114 of the thread control block at which the variable is located. */
24115 /* Nothing should follow this operator, so the top of stack would
24116 be returned. */
24117 /* This is valid for partial global symbols, but the variable's
24118 address will be bogus in the psymtab. Make it always at least
24119 non-zero to not look as a variable garbage collected by linker
24120 which have DW_OP_addr 0. */
24121 if (i < size)
24122 dwarf2_complex_location_expr_complaint ();
24123 stack[stacki]++;
24124 break;
24125
24126 case DW_OP_GNU_uninit:
24127 break;
24128
24129 case DW_OP_GNU_addr_index:
24130 case DW_OP_GNU_const_index:
24131 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24132 &bytes_read);
24133 i += bytes_read;
24134 break;
24135
24136 default:
24137 {
24138 const char *name = get_DW_OP_name (op);
24139
24140 if (name)
24141 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
24142 name);
24143 else
24144 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
24145 op);
24146 }
24147
24148 return (stack[stacki]);
24149 }
24150
24151 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24152 outside of the allocated space. Also enforce minimum>0. */
24153 if (stacki >= ARRAY_SIZE (stack) - 1)
24154 {
24155 complaint (&symfile_complaints,
24156 _("location description stack overflow"));
24157 return 0;
24158 }
24159
24160 if (stacki <= 0)
24161 {
24162 complaint (&symfile_complaints,
24163 _("location description stack underflow"));
24164 return 0;
24165 }
24166 }
24167 return (stack[stacki]);
24168 }
24169
24170 /* memory allocation interface */
24171
24172 static struct dwarf_block *
24173 dwarf_alloc_block (struct dwarf2_cu *cu)
24174 {
24175 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24176 }
24177
24178 static struct die_info *
24179 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24180 {
24181 struct die_info *die;
24182 size_t size = sizeof (struct die_info);
24183
24184 if (num_attrs > 1)
24185 size += (num_attrs - 1) * sizeof (struct attribute);
24186
24187 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24188 memset (die, 0, sizeof (struct die_info));
24189 return (die);
24190 }
24191
24192 \f
24193 /* Macro support. */
24194
24195 /* Return file name relative to the compilation directory of file number I in
24196 *LH's file name table. The result is allocated using xmalloc; the caller is
24197 responsible for freeing it. */
24198
24199 static char *
24200 file_file_name (int file, struct line_header *lh)
24201 {
24202 /* Is the file number a valid index into the line header's file name
24203 table? Remember that file numbers start with one, not zero. */
24204 if (1 <= file && file <= lh->file_names.size ())
24205 {
24206 const file_entry &fe = lh->file_names[file - 1];
24207
24208 if (!IS_ABSOLUTE_PATH (fe.name))
24209 {
24210 const char *dir = fe.include_dir (lh);
24211 if (dir != NULL)
24212 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
24213 }
24214 return xstrdup (fe.name);
24215 }
24216 else
24217 {
24218 /* The compiler produced a bogus file number. We can at least
24219 record the macro definitions made in the file, even if we
24220 won't be able to find the file by name. */
24221 char fake_name[80];
24222
24223 xsnprintf (fake_name, sizeof (fake_name),
24224 "<bad macro file number %d>", file);
24225
24226 complaint (&symfile_complaints,
24227 _("bad file number in macro information (%d)"),
24228 file);
24229
24230 return xstrdup (fake_name);
24231 }
24232 }
24233
24234 /* Return the full name of file number I in *LH's file name table.
24235 Use COMP_DIR as the name of the current directory of the
24236 compilation. The result is allocated using xmalloc; the caller is
24237 responsible for freeing it. */
24238 static char *
24239 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24240 {
24241 /* Is the file number a valid index into the line header's file name
24242 table? Remember that file numbers start with one, not zero. */
24243 if (1 <= file && file <= lh->file_names.size ())
24244 {
24245 char *relative = file_file_name (file, lh);
24246
24247 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24248 return relative;
24249 return reconcat (relative, comp_dir, SLASH_STRING,
24250 relative, (char *) NULL);
24251 }
24252 else
24253 return file_file_name (file, lh);
24254 }
24255
24256
24257 static struct macro_source_file *
24258 macro_start_file (int file, int line,
24259 struct macro_source_file *current_file,
24260 struct line_header *lh)
24261 {
24262 /* File name relative to the compilation directory of this source file. */
24263 char *file_name = file_file_name (file, lh);
24264
24265 if (! current_file)
24266 {
24267 /* Note: We don't create a macro table for this compilation unit
24268 at all until we actually get a filename. */
24269 struct macro_table *macro_table = get_macro_table ();
24270
24271 /* If we have no current file, then this must be the start_file
24272 directive for the compilation unit's main source file. */
24273 current_file = macro_set_main (macro_table, file_name);
24274 macro_define_special (macro_table);
24275 }
24276 else
24277 current_file = macro_include (current_file, line, file_name);
24278
24279 xfree (file_name);
24280
24281 return current_file;
24282 }
24283
24284 static const char *
24285 consume_improper_spaces (const char *p, const char *body)
24286 {
24287 if (*p == ' ')
24288 {
24289 complaint (&symfile_complaints,
24290 _("macro definition contains spaces "
24291 "in formal argument list:\n`%s'"),
24292 body);
24293
24294 while (*p == ' ')
24295 p++;
24296 }
24297
24298 return p;
24299 }
24300
24301
24302 static void
24303 parse_macro_definition (struct macro_source_file *file, int line,
24304 const char *body)
24305 {
24306 const char *p;
24307
24308 /* The body string takes one of two forms. For object-like macro
24309 definitions, it should be:
24310
24311 <macro name> " " <definition>
24312
24313 For function-like macro definitions, it should be:
24314
24315 <macro name> "() " <definition>
24316 or
24317 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24318
24319 Spaces may appear only where explicitly indicated, and in the
24320 <definition>.
24321
24322 The Dwarf 2 spec says that an object-like macro's name is always
24323 followed by a space, but versions of GCC around March 2002 omit
24324 the space when the macro's definition is the empty string.
24325
24326 The Dwarf 2 spec says that there should be no spaces between the
24327 formal arguments in a function-like macro's formal argument list,
24328 but versions of GCC around March 2002 include spaces after the
24329 commas. */
24330
24331
24332 /* Find the extent of the macro name. The macro name is terminated
24333 by either a space or null character (for an object-like macro) or
24334 an opening paren (for a function-like macro). */
24335 for (p = body; *p; p++)
24336 if (*p == ' ' || *p == '(')
24337 break;
24338
24339 if (*p == ' ' || *p == '\0')
24340 {
24341 /* It's an object-like macro. */
24342 int name_len = p - body;
24343 char *name = savestring (body, name_len);
24344 const char *replacement;
24345
24346 if (*p == ' ')
24347 replacement = body + name_len + 1;
24348 else
24349 {
24350 dwarf2_macro_malformed_definition_complaint (body);
24351 replacement = body + name_len;
24352 }
24353
24354 macro_define_object (file, line, name, replacement);
24355
24356 xfree (name);
24357 }
24358 else if (*p == '(')
24359 {
24360 /* It's a function-like macro. */
24361 char *name = savestring (body, p - body);
24362 int argc = 0;
24363 int argv_size = 1;
24364 char **argv = XNEWVEC (char *, argv_size);
24365
24366 p++;
24367
24368 p = consume_improper_spaces (p, body);
24369
24370 /* Parse the formal argument list. */
24371 while (*p && *p != ')')
24372 {
24373 /* Find the extent of the current argument name. */
24374 const char *arg_start = p;
24375
24376 while (*p && *p != ',' && *p != ')' && *p != ' ')
24377 p++;
24378
24379 if (! *p || p == arg_start)
24380 dwarf2_macro_malformed_definition_complaint (body);
24381 else
24382 {
24383 /* Make sure argv has room for the new argument. */
24384 if (argc >= argv_size)
24385 {
24386 argv_size *= 2;
24387 argv = XRESIZEVEC (char *, argv, argv_size);
24388 }
24389
24390 argv[argc++] = savestring (arg_start, p - arg_start);
24391 }
24392
24393 p = consume_improper_spaces (p, body);
24394
24395 /* Consume the comma, if present. */
24396 if (*p == ',')
24397 {
24398 p++;
24399
24400 p = consume_improper_spaces (p, body);
24401 }
24402 }
24403
24404 if (*p == ')')
24405 {
24406 p++;
24407
24408 if (*p == ' ')
24409 /* Perfectly formed definition, no complaints. */
24410 macro_define_function (file, line, name,
24411 argc, (const char **) argv,
24412 p + 1);
24413 else if (*p == '\0')
24414 {
24415 /* Complain, but do define it. */
24416 dwarf2_macro_malformed_definition_complaint (body);
24417 macro_define_function (file, line, name,
24418 argc, (const char **) argv,
24419 p);
24420 }
24421 else
24422 /* Just complain. */
24423 dwarf2_macro_malformed_definition_complaint (body);
24424 }
24425 else
24426 /* Just complain. */
24427 dwarf2_macro_malformed_definition_complaint (body);
24428
24429 xfree (name);
24430 {
24431 int i;
24432
24433 for (i = 0; i < argc; i++)
24434 xfree (argv[i]);
24435 }
24436 xfree (argv);
24437 }
24438 else
24439 dwarf2_macro_malformed_definition_complaint (body);
24440 }
24441
24442 /* Skip some bytes from BYTES according to the form given in FORM.
24443 Returns the new pointer. */
24444
24445 static const gdb_byte *
24446 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24447 enum dwarf_form form,
24448 unsigned int offset_size,
24449 struct dwarf2_section_info *section)
24450 {
24451 unsigned int bytes_read;
24452
24453 switch (form)
24454 {
24455 case DW_FORM_data1:
24456 case DW_FORM_flag:
24457 ++bytes;
24458 break;
24459
24460 case DW_FORM_data2:
24461 bytes += 2;
24462 break;
24463
24464 case DW_FORM_data4:
24465 bytes += 4;
24466 break;
24467
24468 case DW_FORM_data8:
24469 bytes += 8;
24470 break;
24471
24472 case DW_FORM_data16:
24473 bytes += 16;
24474 break;
24475
24476 case DW_FORM_string:
24477 read_direct_string (abfd, bytes, &bytes_read);
24478 bytes += bytes_read;
24479 break;
24480
24481 case DW_FORM_sec_offset:
24482 case DW_FORM_strp:
24483 case DW_FORM_GNU_strp_alt:
24484 bytes += offset_size;
24485 break;
24486
24487 case DW_FORM_block:
24488 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24489 bytes += bytes_read;
24490 break;
24491
24492 case DW_FORM_block1:
24493 bytes += 1 + read_1_byte (abfd, bytes);
24494 break;
24495 case DW_FORM_block2:
24496 bytes += 2 + read_2_bytes (abfd, bytes);
24497 break;
24498 case DW_FORM_block4:
24499 bytes += 4 + read_4_bytes (abfd, bytes);
24500 break;
24501
24502 case DW_FORM_sdata:
24503 case DW_FORM_udata:
24504 case DW_FORM_GNU_addr_index:
24505 case DW_FORM_GNU_str_index:
24506 bytes = gdb_skip_leb128 (bytes, buffer_end);
24507 if (bytes == NULL)
24508 {
24509 dwarf2_section_buffer_overflow_complaint (section);
24510 return NULL;
24511 }
24512 break;
24513
24514 case DW_FORM_implicit_const:
24515 break;
24516
24517 default:
24518 {
24519 complaint (&symfile_complaints,
24520 _("invalid form 0x%x in `%s'"),
24521 form, get_section_name (section));
24522 return NULL;
24523 }
24524 }
24525
24526 return bytes;
24527 }
24528
24529 /* A helper for dwarf_decode_macros that handles skipping an unknown
24530 opcode. Returns an updated pointer to the macro data buffer; or,
24531 on error, issues a complaint and returns NULL. */
24532
24533 static const gdb_byte *
24534 skip_unknown_opcode (unsigned int opcode,
24535 const gdb_byte **opcode_definitions,
24536 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24537 bfd *abfd,
24538 unsigned int offset_size,
24539 struct dwarf2_section_info *section)
24540 {
24541 unsigned int bytes_read, i;
24542 unsigned long arg;
24543 const gdb_byte *defn;
24544
24545 if (opcode_definitions[opcode] == NULL)
24546 {
24547 complaint (&symfile_complaints,
24548 _("unrecognized DW_MACFINO opcode 0x%x"),
24549 opcode);
24550 return NULL;
24551 }
24552
24553 defn = opcode_definitions[opcode];
24554 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24555 defn += bytes_read;
24556
24557 for (i = 0; i < arg; ++i)
24558 {
24559 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24560 (enum dwarf_form) defn[i], offset_size,
24561 section);
24562 if (mac_ptr == NULL)
24563 {
24564 /* skip_form_bytes already issued the complaint. */
24565 return NULL;
24566 }
24567 }
24568
24569 return mac_ptr;
24570 }
24571
24572 /* A helper function which parses the header of a macro section.
24573 If the macro section is the extended (for now called "GNU") type,
24574 then this updates *OFFSET_SIZE. Returns a pointer to just after
24575 the header, or issues a complaint and returns NULL on error. */
24576
24577 static const gdb_byte *
24578 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24579 bfd *abfd,
24580 const gdb_byte *mac_ptr,
24581 unsigned int *offset_size,
24582 int section_is_gnu)
24583 {
24584 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24585
24586 if (section_is_gnu)
24587 {
24588 unsigned int version, flags;
24589
24590 version = read_2_bytes (abfd, mac_ptr);
24591 if (version != 4 && version != 5)
24592 {
24593 complaint (&symfile_complaints,
24594 _("unrecognized version `%d' in .debug_macro section"),
24595 version);
24596 return NULL;
24597 }
24598 mac_ptr += 2;
24599
24600 flags = read_1_byte (abfd, mac_ptr);
24601 ++mac_ptr;
24602 *offset_size = (flags & 1) ? 8 : 4;
24603
24604 if ((flags & 2) != 0)
24605 /* We don't need the line table offset. */
24606 mac_ptr += *offset_size;
24607
24608 /* Vendor opcode descriptions. */
24609 if ((flags & 4) != 0)
24610 {
24611 unsigned int i, count;
24612
24613 count = read_1_byte (abfd, mac_ptr);
24614 ++mac_ptr;
24615 for (i = 0; i < count; ++i)
24616 {
24617 unsigned int opcode, bytes_read;
24618 unsigned long arg;
24619
24620 opcode = read_1_byte (abfd, mac_ptr);
24621 ++mac_ptr;
24622 opcode_definitions[opcode] = mac_ptr;
24623 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24624 mac_ptr += bytes_read;
24625 mac_ptr += arg;
24626 }
24627 }
24628 }
24629
24630 return mac_ptr;
24631 }
24632
24633 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24634 including DW_MACRO_import. */
24635
24636 static void
24637 dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24638 bfd *abfd,
24639 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24640 struct macro_source_file *current_file,
24641 struct line_header *lh,
24642 struct dwarf2_section_info *section,
24643 int section_is_gnu, int section_is_dwz,
24644 unsigned int offset_size,
24645 htab_t include_hash)
24646 {
24647 struct objfile *objfile = dwarf2_per_objfile->objfile;
24648 enum dwarf_macro_record_type macinfo_type;
24649 int at_commandline;
24650 const gdb_byte *opcode_definitions[256];
24651
24652 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24653 &offset_size, section_is_gnu);
24654 if (mac_ptr == NULL)
24655 {
24656 /* We already issued a complaint. */
24657 return;
24658 }
24659
24660 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24661 GDB is still reading the definitions from command line. First
24662 DW_MACINFO_start_file will need to be ignored as it was already executed
24663 to create CURRENT_FILE for the main source holding also the command line
24664 definitions. On first met DW_MACINFO_start_file this flag is reset to
24665 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24666
24667 at_commandline = 1;
24668
24669 do
24670 {
24671 /* Do we at least have room for a macinfo type byte? */
24672 if (mac_ptr >= mac_end)
24673 {
24674 dwarf2_section_buffer_overflow_complaint (section);
24675 break;
24676 }
24677
24678 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24679 mac_ptr++;
24680
24681 /* Note that we rely on the fact that the corresponding GNU and
24682 DWARF constants are the same. */
24683 DIAGNOSTIC_PUSH
24684 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24685 switch (macinfo_type)
24686 {
24687 /* A zero macinfo type indicates the end of the macro
24688 information. */
24689 case 0:
24690 break;
24691
24692 case DW_MACRO_define:
24693 case DW_MACRO_undef:
24694 case DW_MACRO_define_strp:
24695 case DW_MACRO_undef_strp:
24696 case DW_MACRO_define_sup:
24697 case DW_MACRO_undef_sup:
24698 {
24699 unsigned int bytes_read;
24700 int line;
24701 const char *body;
24702 int is_define;
24703
24704 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24705 mac_ptr += bytes_read;
24706
24707 if (macinfo_type == DW_MACRO_define
24708 || macinfo_type == DW_MACRO_undef)
24709 {
24710 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24711 mac_ptr += bytes_read;
24712 }
24713 else
24714 {
24715 LONGEST str_offset;
24716
24717 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24718 mac_ptr += offset_size;
24719
24720 if (macinfo_type == DW_MACRO_define_sup
24721 || macinfo_type == DW_MACRO_undef_sup
24722 || section_is_dwz)
24723 {
24724 struct dwz_file *dwz
24725 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24726
24727 body = read_indirect_string_from_dwz (objfile,
24728 dwz, str_offset);
24729 }
24730 else
24731 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24732 abfd, str_offset);
24733 }
24734
24735 is_define = (macinfo_type == DW_MACRO_define
24736 || macinfo_type == DW_MACRO_define_strp
24737 || macinfo_type == DW_MACRO_define_sup);
24738 if (! current_file)
24739 {
24740 /* DWARF violation as no main source is present. */
24741 complaint (&symfile_complaints,
24742 _("debug info with no main source gives macro %s "
24743 "on line %d: %s"),
24744 is_define ? _("definition") : _("undefinition"),
24745 line, body);
24746 break;
24747 }
24748 if ((line == 0 && !at_commandline)
24749 || (line != 0 && at_commandline))
24750 complaint (&symfile_complaints,
24751 _("debug info gives %s macro %s with %s line %d: %s"),
24752 at_commandline ? _("command-line") : _("in-file"),
24753 is_define ? _("definition") : _("undefinition"),
24754 line == 0 ? _("zero") : _("non-zero"), line, body);
24755
24756 if (is_define)
24757 parse_macro_definition (current_file, line, body);
24758 else
24759 {
24760 gdb_assert (macinfo_type == DW_MACRO_undef
24761 || macinfo_type == DW_MACRO_undef_strp
24762 || macinfo_type == DW_MACRO_undef_sup);
24763 macro_undef (current_file, line, body);
24764 }
24765 }
24766 break;
24767
24768 case DW_MACRO_start_file:
24769 {
24770 unsigned int bytes_read;
24771 int line, file;
24772
24773 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24774 mac_ptr += bytes_read;
24775 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24776 mac_ptr += bytes_read;
24777
24778 if ((line == 0 && !at_commandline)
24779 || (line != 0 && at_commandline))
24780 complaint (&symfile_complaints,
24781 _("debug info gives source %d included "
24782 "from %s at %s line %d"),
24783 file, at_commandline ? _("command-line") : _("file"),
24784 line == 0 ? _("zero") : _("non-zero"), line);
24785
24786 if (at_commandline)
24787 {
24788 /* This DW_MACRO_start_file was executed in the
24789 pass one. */
24790 at_commandline = 0;
24791 }
24792 else
24793 current_file = macro_start_file (file, line, current_file, lh);
24794 }
24795 break;
24796
24797 case DW_MACRO_end_file:
24798 if (! current_file)
24799 complaint (&symfile_complaints,
24800 _("macro debug info has an unmatched "
24801 "`close_file' directive"));
24802 else
24803 {
24804 current_file = current_file->included_by;
24805 if (! current_file)
24806 {
24807 enum dwarf_macro_record_type next_type;
24808
24809 /* GCC circa March 2002 doesn't produce the zero
24810 type byte marking the end of the compilation
24811 unit. Complain if it's not there, but exit no
24812 matter what. */
24813
24814 /* Do we at least have room for a macinfo type byte? */
24815 if (mac_ptr >= mac_end)
24816 {
24817 dwarf2_section_buffer_overflow_complaint (section);
24818 return;
24819 }
24820
24821 /* We don't increment mac_ptr here, so this is just
24822 a look-ahead. */
24823 next_type
24824 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24825 mac_ptr);
24826 if (next_type != 0)
24827 complaint (&symfile_complaints,
24828 _("no terminating 0-type entry for "
24829 "macros in `.debug_macinfo' section"));
24830
24831 return;
24832 }
24833 }
24834 break;
24835
24836 case DW_MACRO_import:
24837 case DW_MACRO_import_sup:
24838 {
24839 LONGEST offset;
24840 void **slot;
24841 bfd *include_bfd = abfd;
24842 struct dwarf2_section_info *include_section = section;
24843 const gdb_byte *include_mac_end = mac_end;
24844 int is_dwz = section_is_dwz;
24845 const gdb_byte *new_mac_ptr;
24846
24847 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24848 mac_ptr += offset_size;
24849
24850 if (macinfo_type == DW_MACRO_import_sup)
24851 {
24852 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24853
24854 dwarf2_read_section (objfile, &dwz->macro);
24855
24856 include_section = &dwz->macro;
24857 include_bfd = get_section_bfd_owner (include_section);
24858 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24859 is_dwz = 1;
24860 }
24861
24862 new_mac_ptr = include_section->buffer + offset;
24863 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24864
24865 if (*slot != NULL)
24866 {
24867 /* This has actually happened; see
24868 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24869 complaint (&symfile_complaints,
24870 _("recursive DW_MACRO_import in "
24871 ".debug_macro section"));
24872 }
24873 else
24874 {
24875 *slot = (void *) new_mac_ptr;
24876
24877 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24878 include_bfd, new_mac_ptr,
24879 include_mac_end, current_file, lh,
24880 section, section_is_gnu, is_dwz,
24881 offset_size, include_hash);
24882
24883 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24884 }
24885 }
24886 break;
24887
24888 case DW_MACINFO_vendor_ext:
24889 if (!section_is_gnu)
24890 {
24891 unsigned int bytes_read;
24892
24893 /* This reads the constant, but since we don't recognize
24894 any vendor extensions, we ignore it. */
24895 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24896 mac_ptr += bytes_read;
24897 read_direct_string (abfd, mac_ptr, &bytes_read);
24898 mac_ptr += bytes_read;
24899
24900 /* We don't recognize any vendor extensions. */
24901 break;
24902 }
24903 /* FALLTHROUGH */
24904
24905 default:
24906 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24907 mac_ptr, mac_end, abfd, offset_size,
24908 section);
24909 if (mac_ptr == NULL)
24910 return;
24911 break;
24912 }
24913 DIAGNOSTIC_POP
24914 } while (macinfo_type != 0);
24915 }
24916
24917 static void
24918 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24919 int section_is_gnu)
24920 {
24921 struct dwarf2_per_objfile *dwarf2_per_objfile
24922 = cu->per_cu->dwarf2_per_objfile;
24923 struct objfile *objfile = dwarf2_per_objfile->objfile;
24924 struct line_header *lh = cu->line_header;
24925 bfd *abfd;
24926 const gdb_byte *mac_ptr, *mac_end;
24927 struct macro_source_file *current_file = 0;
24928 enum dwarf_macro_record_type macinfo_type;
24929 unsigned int offset_size = cu->header.offset_size;
24930 const gdb_byte *opcode_definitions[256];
24931 void **slot;
24932 struct dwarf2_section_info *section;
24933 const char *section_name;
24934
24935 if (cu->dwo_unit != NULL)
24936 {
24937 if (section_is_gnu)
24938 {
24939 section = &cu->dwo_unit->dwo_file->sections.macro;
24940 section_name = ".debug_macro.dwo";
24941 }
24942 else
24943 {
24944 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24945 section_name = ".debug_macinfo.dwo";
24946 }
24947 }
24948 else
24949 {
24950 if (section_is_gnu)
24951 {
24952 section = &dwarf2_per_objfile->macro;
24953 section_name = ".debug_macro";
24954 }
24955 else
24956 {
24957 section = &dwarf2_per_objfile->macinfo;
24958 section_name = ".debug_macinfo";
24959 }
24960 }
24961
24962 dwarf2_read_section (objfile, section);
24963 if (section->buffer == NULL)
24964 {
24965 complaint (&symfile_complaints, _("missing %s section"), section_name);
24966 return;
24967 }
24968 abfd = get_section_bfd_owner (section);
24969
24970 /* First pass: Find the name of the base filename.
24971 This filename is needed in order to process all macros whose definition
24972 (or undefinition) comes from the command line. These macros are defined
24973 before the first DW_MACINFO_start_file entry, and yet still need to be
24974 associated to the base file.
24975
24976 To determine the base file name, we scan the macro definitions until we
24977 reach the first DW_MACINFO_start_file entry. We then initialize
24978 CURRENT_FILE accordingly so that any macro definition found before the
24979 first DW_MACINFO_start_file can still be associated to the base file. */
24980
24981 mac_ptr = section->buffer + offset;
24982 mac_end = section->buffer + section->size;
24983
24984 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24985 &offset_size, section_is_gnu);
24986 if (mac_ptr == NULL)
24987 {
24988 /* We already issued a complaint. */
24989 return;
24990 }
24991
24992 do
24993 {
24994 /* Do we at least have room for a macinfo type byte? */
24995 if (mac_ptr >= mac_end)
24996 {
24997 /* Complaint is printed during the second pass as GDB will probably
24998 stop the first pass earlier upon finding
24999 DW_MACINFO_start_file. */
25000 break;
25001 }
25002
25003 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
25004 mac_ptr++;
25005
25006 /* Note that we rely on the fact that the corresponding GNU and
25007 DWARF constants are the same. */
25008 DIAGNOSTIC_PUSH
25009 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
25010 switch (macinfo_type)
25011 {
25012 /* A zero macinfo type indicates the end of the macro
25013 information. */
25014 case 0:
25015 break;
25016
25017 case DW_MACRO_define:
25018 case DW_MACRO_undef:
25019 /* Only skip the data by MAC_PTR. */
25020 {
25021 unsigned int bytes_read;
25022
25023 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25024 mac_ptr += bytes_read;
25025 read_direct_string (abfd, mac_ptr, &bytes_read);
25026 mac_ptr += bytes_read;
25027 }
25028 break;
25029
25030 case DW_MACRO_start_file:
25031 {
25032 unsigned int bytes_read;
25033 int line, file;
25034
25035 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25036 mac_ptr += bytes_read;
25037 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25038 mac_ptr += bytes_read;
25039
25040 current_file = macro_start_file (file, line, current_file, lh);
25041 }
25042 break;
25043
25044 case DW_MACRO_end_file:
25045 /* No data to skip by MAC_PTR. */
25046 break;
25047
25048 case DW_MACRO_define_strp:
25049 case DW_MACRO_undef_strp:
25050 case DW_MACRO_define_sup:
25051 case DW_MACRO_undef_sup:
25052 {
25053 unsigned int bytes_read;
25054
25055 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25056 mac_ptr += bytes_read;
25057 mac_ptr += offset_size;
25058 }
25059 break;
25060
25061 case DW_MACRO_import:
25062 case DW_MACRO_import_sup:
25063 /* Note that, according to the spec, a transparent include
25064 chain cannot call DW_MACRO_start_file. So, we can just
25065 skip this opcode. */
25066 mac_ptr += offset_size;
25067 break;
25068
25069 case DW_MACINFO_vendor_ext:
25070 /* Only skip the data by MAC_PTR. */
25071 if (!section_is_gnu)
25072 {
25073 unsigned int bytes_read;
25074
25075 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25076 mac_ptr += bytes_read;
25077 read_direct_string (abfd, mac_ptr, &bytes_read);
25078 mac_ptr += bytes_read;
25079 }
25080 /* FALLTHROUGH */
25081
25082 default:
25083 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25084 mac_ptr, mac_end, abfd, offset_size,
25085 section);
25086 if (mac_ptr == NULL)
25087 return;
25088 break;
25089 }
25090 DIAGNOSTIC_POP
25091 } while (macinfo_type != 0 && current_file == NULL);
25092
25093 /* Second pass: Process all entries.
25094
25095 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25096 command-line macro definitions/undefinitions. This flag is unset when we
25097 reach the first DW_MACINFO_start_file entry. */
25098
25099 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25100 htab_eq_pointer,
25101 NULL, xcalloc, xfree));
25102 mac_ptr = section->buffer + offset;
25103 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25104 *slot = (void *) mac_ptr;
25105 dwarf_decode_macro_bytes (dwarf2_per_objfile,
25106 abfd, mac_ptr, mac_end,
25107 current_file, lh, section,
25108 section_is_gnu, 0, offset_size,
25109 include_hash.get ());
25110 }
25111
25112 /* Check if the attribute's form is a DW_FORM_block*
25113 if so return true else false. */
25114
25115 static int
25116 attr_form_is_block (const struct attribute *attr)
25117 {
25118 return (attr == NULL ? 0 :
25119 attr->form == DW_FORM_block1
25120 || attr->form == DW_FORM_block2
25121 || attr->form == DW_FORM_block4
25122 || attr->form == DW_FORM_block
25123 || attr->form == DW_FORM_exprloc);
25124 }
25125
25126 /* Return non-zero if ATTR's value is a section offset --- classes
25127 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25128 You may use DW_UNSND (attr) to retrieve such offsets.
25129
25130 Section 7.5.4, "Attribute Encodings", explains that no attribute
25131 may have a value that belongs to more than one of these classes; it
25132 would be ambiguous if we did, because we use the same forms for all
25133 of them. */
25134
25135 static int
25136 attr_form_is_section_offset (const struct attribute *attr)
25137 {
25138 return (attr->form == DW_FORM_data4
25139 || attr->form == DW_FORM_data8
25140 || attr->form == DW_FORM_sec_offset);
25141 }
25142
25143 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25144 zero otherwise. When this function returns true, you can apply
25145 dwarf2_get_attr_constant_value to it.
25146
25147 However, note that for some attributes you must check
25148 attr_form_is_section_offset before using this test. DW_FORM_data4
25149 and DW_FORM_data8 are members of both the constant class, and of
25150 the classes that contain offsets into other debug sections
25151 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25152 that, if an attribute's can be either a constant or one of the
25153 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25154 taken as section offsets, not constants.
25155
25156 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25157 cannot handle that. */
25158
25159 static int
25160 attr_form_is_constant (const struct attribute *attr)
25161 {
25162 switch (attr->form)
25163 {
25164 case DW_FORM_sdata:
25165 case DW_FORM_udata:
25166 case DW_FORM_data1:
25167 case DW_FORM_data2:
25168 case DW_FORM_data4:
25169 case DW_FORM_data8:
25170 case DW_FORM_implicit_const:
25171 return 1;
25172 default:
25173 return 0;
25174 }
25175 }
25176
25177
25178 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25179 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25180
25181 static int
25182 attr_form_is_ref (const struct attribute *attr)
25183 {
25184 switch (attr->form)
25185 {
25186 case DW_FORM_ref_addr:
25187 case DW_FORM_ref1:
25188 case DW_FORM_ref2:
25189 case DW_FORM_ref4:
25190 case DW_FORM_ref8:
25191 case DW_FORM_ref_udata:
25192 case DW_FORM_GNU_ref_alt:
25193 return 1;
25194 default:
25195 return 0;
25196 }
25197 }
25198
25199 /* Return the .debug_loc section to use for CU.
25200 For DWO files use .debug_loc.dwo. */
25201
25202 static struct dwarf2_section_info *
25203 cu_debug_loc_section (struct dwarf2_cu *cu)
25204 {
25205 struct dwarf2_per_objfile *dwarf2_per_objfile
25206 = cu->per_cu->dwarf2_per_objfile;
25207
25208 if (cu->dwo_unit)
25209 {
25210 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25211
25212 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25213 }
25214 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25215 : &dwarf2_per_objfile->loc);
25216 }
25217
25218 /* A helper function that fills in a dwarf2_loclist_baton. */
25219
25220 static void
25221 fill_in_loclist_baton (struct dwarf2_cu *cu,
25222 struct dwarf2_loclist_baton *baton,
25223 const struct attribute *attr)
25224 {
25225 struct dwarf2_per_objfile *dwarf2_per_objfile
25226 = cu->per_cu->dwarf2_per_objfile;
25227 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25228
25229 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25230
25231 baton->per_cu = cu->per_cu;
25232 gdb_assert (baton->per_cu);
25233 /* We don't know how long the location list is, but make sure we
25234 don't run off the edge of the section. */
25235 baton->size = section->size - DW_UNSND (attr);
25236 baton->data = section->buffer + DW_UNSND (attr);
25237 baton->base_address = cu->base_address;
25238 baton->from_dwo = cu->dwo_unit != NULL;
25239 }
25240
25241 static void
25242 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25243 struct dwarf2_cu *cu, int is_block)
25244 {
25245 struct dwarf2_per_objfile *dwarf2_per_objfile
25246 = cu->per_cu->dwarf2_per_objfile;
25247 struct objfile *objfile = dwarf2_per_objfile->objfile;
25248 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25249
25250 if (attr_form_is_section_offset (attr)
25251 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25252 the section. If so, fall through to the complaint in the
25253 other branch. */
25254 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25255 {
25256 struct dwarf2_loclist_baton *baton;
25257
25258 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25259
25260 fill_in_loclist_baton (cu, baton, attr);
25261
25262 if (cu->base_known == 0)
25263 complaint (&symfile_complaints,
25264 _("Location list used without "
25265 "specifying the CU base address."));
25266
25267 SYMBOL_ACLASS_INDEX (sym) = (is_block
25268 ? dwarf2_loclist_block_index
25269 : dwarf2_loclist_index);
25270 SYMBOL_LOCATION_BATON (sym) = baton;
25271 }
25272 else
25273 {
25274 struct dwarf2_locexpr_baton *baton;
25275
25276 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25277 baton->per_cu = cu->per_cu;
25278 gdb_assert (baton->per_cu);
25279
25280 if (attr_form_is_block (attr))
25281 {
25282 /* Note that we're just copying the block's data pointer
25283 here, not the actual data. We're still pointing into the
25284 info_buffer for SYM's objfile; right now we never release
25285 that buffer, but when we do clean up properly this may
25286 need to change. */
25287 baton->size = DW_BLOCK (attr)->size;
25288 baton->data = DW_BLOCK (attr)->data;
25289 }
25290 else
25291 {
25292 dwarf2_invalid_attrib_class_complaint ("location description",
25293 SYMBOL_NATURAL_NAME (sym));
25294 baton->size = 0;
25295 }
25296
25297 SYMBOL_ACLASS_INDEX (sym) = (is_block
25298 ? dwarf2_locexpr_block_index
25299 : dwarf2_locexpr_index);
25300 SYMBOL_LOCATION_BATON (sym) = baton;
25301 }
25302 }
25303
25304 /* Return the OBJFILE associated with the compilation unit CU. If CU
25305 came from a separate debuginfo file, then the master objfile is
25306 returned. */
25307
25308 struct objfile *
25309 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25310 {
25311 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25312
25313 /* Return the master objfile, so that we can report and look up the
25314 correct file containing this variable. */
25315 if (objfile->separate_debug_objfile_backlink)
25316 objfile = objfile->separate_debug_objfile_backlink;
25317
25318 return objfile;
25319 }
25320
25321 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25322 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25323 CU_HEADERP first. */
25324
25325 static const struct comp_unit_head *
25326 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25327 struct dwarf2_per_cu_data *per_cu)
25328 {
25329 const gdb_byte *info_ptr;
25330
25331 if (per_cu->cu)
25332 return &per_cu->cu->header;
25333
25334 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25335
25336 memset (cu_headerp, 0, sizeof (*cu_headerp));
25337 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25338 rcuh_kind::COMPILE);
25339
25340 return cu_headerp;
25341 }
25342
25343 /* Return the address size given in the compilation unit header for CU. */
25344
25345 int
25346 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25347 {
25348 struct comp_unit_head cu_header_local;
25349 const struct comp_unit_head *cu_headerp;
25350
25351 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25352
25353 return cu_headerp->addr_size;
25354 }
25355
25356 /* Return the offset size given in the compilation unit header for CU. */
25357
25358 int
25359 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25360 {
25361 struct comp_unit_head cu_header_local;
25362 const struct comp_unit_head *cu_headerp;
25363
25364 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25365
25366 return cu_headerp->offset_size;
25367 }
25368
25369 /* See its dwarf2loc.h declaration. */
25370
25371 int
25372 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25373 {
25374 struct comp_unit_head cu_header_local;
25375 const struct comp_unit_head *cu_headerp;
25376
25377 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25378
25379 if (cu_headerp->version == 2)
25380 return cu_headerp->addr_size;
25381 else
25382 return cu_headerp->offset_size;
25383 }
25384
25385 /* Return the text offset of the CU. The returned offset comes from
25386 this CU's objfile. If this objfile came from a separate debuginfo
25387 file, then the offset may be different from the corresponding
25388 offset in the parent objfile. */
25389
25390 CORE_ADDR
25391 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25392 {
25393 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25394
25395 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25396 }
25397
25398 /* Return DWARF version number of PER_CU. */
25399
25400 short
25401 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25402 {
25403 return per_cu->dwarf_version;
25404 }
25405
25406 /* Locate the .debug_info compilation unit from CU's objfile which contains
25407 the DIE at OFFSET. Raises an error on failure. */
25408
25409 static struct dwarf2_per_cu_data *
25410 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25411 unsigned int offset_in_dwz,
25412 struct dwarf2_per_objfile *dwarf2_per_objfile)
25413 {
25414 struct dwarf2_per_cu_data *this_cu;
25415 int low, high;
25416 const sect_offset *cu_off;
25417
25418 low = 0;
25419 high = dwarf2_per_objfile->n_comp_units - 1;
25420 while (high > low)
25421 {
25422 struct dwarf2_per_cu_data *mid_cu;
25423 int mid = low + (high - low) / 2;
25424
25425 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25426 cu_off = &mid_cu->sect_off;
25427 if (mid_cu->is_dwz > offset_in_dwz
25428 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
25429 high = mid;
25430 else
25431 low = mid + 1;
25432 }
25433 gdb_assert (low == high);
25434 this_cu = dwarf2_per_objfile->all_comp_units[low];
25435 cu_off = &this_cu->sect_off;
25436 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
25437 {
25438 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25439 error (_("Dwarf Error: could not find partial DIE containing "
25440 "offset %s [in module %s]"),
25441 sect_offset_str (sect_off),
25442 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25443
25444 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25445 <= sect_off);
25446 return dwarf2_per_objfile->all_comp_units[low-1];
25447 }
25448 else
25449 {
25450 this_cu = dwarf2_per_objfile->all_comp_units[low];
25451 if (low == dwarf2_per_objfile->n_comp_units - 1
25452 && sect_off >= this_cu->sect_off + this_cu->length)
25453 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25454 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25455 return this_cu;
25456 }
25457 }
25458
25459 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25460
25461 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25462 : per_cu (per_cu_),
25463 mark (0),
25464 has_loclist (0),
25465 checked_producer (0),
25466 producer_is_gxx_lt_4_6 (0),
25467 producer_is_gcc_lt_4_3 (0),
25468 producer_is_icc_lt_14 (0),
25469 processing_has_namespace_info (0)
25470 {
25471 per_cu->cu = this;
25472 }
25473
25474 /* Destroy a dwarf2_cu. */
25475
25476 dwarf2_cu::~dwarf2_cu ()
25477 {
25478 per_cu->cu = NULL;
25479 }
25480
25481 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25482
25483 static void
25484 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25485 enum language pretend_language)
25486 {
25487 struct attribute *attr;
25488
25489 /* Set the language we're debugging. */
25490 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25491 if (attr)
25492 set_cu_language (DW_UNSND (attr), cu);
25493 else
25494 {
25495 cu->language = pretend_language;
25496 cu->language_defn = language_def (cu->language);
25497 }
25498
25499 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25500 }
25501
25502 /* Free all cached compilation units. */
25503
25504 static void
25505 free_cached_comp_units (void *data)
25506 {
25507 struct dwarf2_per_objfile *dwarf2_per_objfile
25508 = (struct dwarf2_per_objfile *) data;
25509
25510 dwarf2_per_objfile->free_cached_comp_units ();
25511 }
25512
25513 /* Increase the age counter on each cached compilation unit, and free
25514 any that are too old. */
25515
25516 static void
25517 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25518 {
25519 struct dwarf2_per_cu_data *per_cu, **last_chain;
25520
25521 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25522 per_cu = dwarf2_per_objfile->read_in_chain;
25523 while (per_cu != NULL)
25524 {
25525 per_cu->cu->last_used ++;
25526 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25527 dwarf2_mark (per_cu->cu);
25528 per_cu = per_cu->cu->read_in_chain;
25529 }
25530
25531 per_cu = dwarf2_per_objfile->read_in_chain;
25532 last_chain = &dwarf2_per_objfile->read_in_chain;
25533 while (per_cu != NULL)
25534 {
25535 struct dwarf2_per_cu_data *next_cu;
25536
25537 next_cu = per_cu->cu->read_in_chain;
25538
25539 if (!per_cu->cu->mark)
25540 {
25541 delete per_cu->cu;
25542 *last_chain = next_cu;
25543 }
25544 else
25545 last_chain = &per_cu->cu->read_in_chain;
25546
25547 per_cu = next_cu;
25548 }
25549 }
25550
25551 /* Remove a single compilation unit from the cache. */
25552
25553 static void
25554 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25555 {
25556 struct dwarf2_per_cu_data *per_cu, **last_chain;
25557 struct dwarf2_per_objfile *dwarf2_per_objfile
25558 = target_per_cu->dwarf2_per_objfile;
25559
25560 per_cu = dwarf2_per_objfile->read_in_chain;
25561 last_chain = &dwarf2_per_objfile->read_in_chain;
25562 while (per_cu != NULL)
25563 {
25564 struct dwarf2_per_cu_data *next_cu;
25565
25566 next_cu = per_cu->cu->read_in_chain;
25567
25568 if (per_cu == target_per_cu)
25569 {
25570 delete per_cu->cu;
25571 per_cu->cu = NULL;
25572 *last_chain = next_cu;
25573 break;
25574 }
25575 else
25576 last_chain = &per_cu->cu->read_in_chain;
25577
25578 per_cu = next_cu;
25579 }
25580 }
25581
25582 /* Release all extra memory associated with OBJFILE. */
25583
25584 void
25585 dwarf2_free_objfile (struct objfile *objfile)
25586 {
25587 struct dwarf2_per_objfile *dwarf2_per_objfile
25588 = get_dwarf2_per_objfile (objfile);
25589
25590 delete dwarf2_per_objfile;
25591 }
25592
25593 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25594 We store these in a hash table separate from the DIEs, and preserve them
25595 when the DIEs are flushed out of cache.
25596
25597 The CU "per_cu" pointer is needed because offset alone is not enough to
25598 uniquely identify the type. A file may have multiple .debug_types sections,
25599 or the type may come from a DWO file. Furthermore, while it's more logical
25600 to use per_cu->section+offset, with Fission the section with the data is in
25601 the DWO file but we don't know that section at the point we need it.
25602 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25603 because we can enter the lookup routine, get_die_type_at_offset, from
25604 outside this file, and thus won't necessarily have PER_CU->cu.
25605 Fortunately, PER_CU is stable for the life of the objfile. */
25606
25607 struct dwarf2_per_cu_offset_and_type
25608 {
25609 const struct dwarf2_per_cu_data *per_cu;
25610 sect_offset sect_off;
25611 struct type *type;
25612 };
25613
25614 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25615
25616 static hashval_t
25617 per_cu_offset_and_type_hash (const void *item)
25618 {
25619 const struct dwarf2_per_cu_offset_and_type *ofs
25620 = (const struct dwarf2_per_cu_offset_and_type *) item;
25621
25622 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25623 }
25624
25625 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25626
25627 static int
25628 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25629 {
25630 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25631 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25632 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25633 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25634
25635 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25636 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25637 }
25638
25639 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25640 table if necessary. For convenience, return TYPE.
25641
25642 The DIEs reading must have careful ordering to:
25643 * Not cause infite loops trying to read in DIEs as a prerequisite for
25644 reading current DIE.
25645 * Not trying to dereference contents of still incompletely read in types
25646 while reading in other DIEs.
25647 * Enable referencing still incompletely read in types just by a pointer to
25648 the type without accessing its fields.
25649
25650 Therefore caller should follow these rules:
25651 * Try to fetch any prerequisite types we may need to build this DIE type
25652 before building the type and calling set_die_type.
25653 * After building type call set_die_type for current DIE as soon as
25654 possible before fetching more types to complete the current type.
25655 * Make the type as complete as possible before fetching more types. */
25656
25657 static struct type *
25658 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25659 {
25660 struct dwarf2_per_objfile *dwarf2_per_objfile
25661 = cu->per_cu->dwarf2_per_objfile;
25662 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25663 struct objfile *objfile = dwarf2_per_objfile->objfile;
25664 struct attribute *attr;
25665 struct dynamic_prop prop;
25666
25667 /* For Ada types, make sure that the gnat-specific data is always
25668 initialized (if not already set). There are a few types where
25669 we should not be doing so, because the type-specific area is
25670 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25671 where the type-specific area is used to store the floatformat).
25672 But this is not a problem, because the gnat-specific information
25673 is actually not needed for these types. */
25674 if (need_gnat_info (cu)
25675 && TYPE_CODE (type) != TYPE_CODE_FUNC
25676 && TYPE_CODE (type) != TYPE_CODE_FLT
25677 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25678 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25679 && TYPE_CODE (type) != TYPE_CODE_METHOD
25680 && !HAVE_GNAT_AUX_INFO (type))
25681 INIT_GNAT_SPECIFIC (type);
25682
25683 /* Read DW_AT_allocated and set in type. */
25684 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25685 if (attr_form_is_block (attr))
25686 {
25687 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25688 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25689 }
25690 else if (attr != NULL)
25691 {
25692 complaint (&symfile_complaints,
25693 _("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25694 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25695 sect_offset_str (die->sect_off));
25696 }
25697
25698 /* Read DW_AT_associated and set in type. */
25699 attr = dwarf2_attr (die, DW_AT_associated, cu);
25700 if (attr_form_is_block (attr))
25701 {
25702 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25703 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25704 }
25705 else if (attr != NULL)
25706 {
25707 complaint (&symfile_complaints,
25708 _("DW_AT_associated has the wrong form (%s) at DIE %s"),
25709 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25710 sect_offset_str (die->sect_off));
25711 }
25712
25713 /* Read DW_AT_data_location and set in type. */
25714 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25715 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25716 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25717
25718 if (dwarf2_per_objfile->die_type_hash == NULL)
25719 {
25720 dwarf2_per_objfile->die_type_hash =
25721 htab_create_alloc_ex (127,
25722 per_cu_offset_and_type_hash,
25723 per_cu_offset_and_type_eq,
25724 NULL,
25725 &objfile->objfile_obstack,
25726 hashtab_obstack_allocate,
25727 dummy_obstack_deallocate);
25728 }
25729
25730 ofs.per_cu = cu->per_cu;
25731 ofs.sect_off = die->sect_off;
25732 ofs.type = type;
25733 slot = (struct dwarf2_per_cu_offset_and_type **)
25734 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25735 if (*slot)
25736 complaint (&symfile_complaints,
25737 _("A problem internal to GDB: DIE %s has type already set"),
25738 sect_offset_str (die->sect_off));
25739 *slot = XOBNEW (&objfile->objfile_obstack,
25740 struct dwarf2_per_cu_offset_and_type);
25741 **slot = ofs;
25742 return type;
25743 }
25744
25745 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25746 or return NULL if the die does not have a saved type. */
25747
25748 static struct type *
25749 get_die_type_at_offset (sect_offset sect_off,
25750 struct dwarf2_per_cu_data *per_cu)
25751 {
25752 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25753 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25754
25755 if (dwarf2_per_objfile->die_type_hash == NULL)
25756 return NULL;
25757
25758 ofs.per_cu = per_cu;
25759 ofs.sect_off = sect_off;
25760 slot = ((struct dwarf2_per_cu_offset_and_type *)
25761 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25762 if (slot)
25763 return slot->type;
25764 else
25765 return NULL;
25766 }
25767
25768 /* Look up the type for DIE in CU in die_type_hash,
25769 or return NULL if DIE does not have a saved type. */
25770
25771 static struct type *
25772 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25773 {
25774 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25775 }
25776
25777 /* Add a dependence relationship from CU to REF_PER_CU. */
25778
25779 static void
25780 dwarf2_add_dependence (struct dwarf2_cu *cu,
25781 struct dwarf2_per_cu_data *ref_per_cu)
25782 {
25783 void **slot;
25784
25785 if (cu->dependencies == NULL)
25786 cu->dependencies
25787 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25788 NULL, &cu->comp_unit_obstack,
25789 hashtab_obstack_allocate,
25790 dummy_obstack_deallocate);
25791
25792 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25793 if (*slot == NULL)
25794 *slot = ref_per_cu;
25795 }
25796
25797 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25798 Set the mark field in every compilation unit in the
25799 cache that we must keep because we are keeping CU. */
25800
25801 static int
25802 dwarf2_mark_helper (void **slot, void *data)
25803 {
25804 struct dwarf2_per_cu_data *per_cu;
25805
25806 per_cu = (struct dwarf2_per_cu_data *) *slot;
25807
25808 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25809 reading of the chain. As such dependencies remain valid it is not much
25810 useful to track and undo them during QUIT cleanups. */
25811 if (per_cu->cu == NULL)
25812 return 1;
25813
25814 if (per_cu->cu->mark)
25815 return 1;
25816 per_cu->cu->mark = 1;
25817
25818 if (per_cu->cu->dependencies != NULL)
25819 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25820
25821 return 1;
25822 }
25823
25824 /* Set the mark field in CU and in every other compilation unit in the
25825 cache that we must keep because we are keeping CU. */
25826
25827 static void
25828 dwarf2_mark (struct dwarf2_cu *cu)
25829 {
25830 if (cu->mark)
25831 return;
25832 cu->mark = 1;
25833 if (cu->dependencies != NULL)
25834 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25835 }
25836
25837 static void
25838 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25839 {
25840 while (per_cu)
25841 {
25842 per_cu->cu->mark = 0;
25843 per_cu = per_cu->cu->read_in_chain;
25844 }
25845 }
25846
25847 /* Trivial hash function for partial_die_info: the hash value of a DIE
25848 is its offset in .debug_info for this objfile. */
25849
25850 static hashval_t
25851 partial_die_hash (const void *item)
25852 {
25853 const struct partial_die_info *part_die
25854 = (const struct partial_die_info *) item;
25855
25856 return to_underlying (part_die->sect_off);
25857 }
25858
25859 /* Trivial comparison function for partial_die_info structures: two DIEs
25860 are equal if they have the same offset. */
25861
25862 static int
25863 partial_die_eq (const void *item_lhs, const void *item_rhs)
25864 {
25865 const struct partial_die_info *part_die_lhs
25866 = (const struct partial_die_info *) item_lhs;
25867 const struct partial_die_info *part_die_rhs
25868 = (const struct partial_die_info *) item_rhs;
25869
25870 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25871 }
25872
25873 static struct cmd_list_element *set_dwarf_cmdlist;
25874 static struct cmd_list_element *show_dwarf_cmdlist;
25875
25876 static void
25877 set_dwarf_cmd (const char *args, int from_tty)
25878 {
25879 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25880 gdb_stdout);
25881 }
25882
25883 static void
25884 show_dwarf_cmd (const char *args, int from_tty)
25885 {
25886 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25887 }
25888
25889 /* The "save gdb-index" command. */
25890
25891 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25892 error checking. */
25893
25894 static void
25895 file_write (FILE *file, const void *data, size_t size)
25896 {
25897 if (fwrite (data, 1, size, file) != size)
25898 error (_("couldn't data write to file"));
25899 }
25900
25901 /* Write the contents of VEC to FILE, with error checking. */
25902
25903 template<typename Elem, typename Alloc>
25904 static void
25905 file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25906 {
25907 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25908 }
25909
25910 /* In-memory buffer to prepare data to be written later to a file. */
25911 class data_buf
25912 {
25913 public:
25914 /* Copy DATA to the end of the buffer. */
25915 template<typename T>
25916 void append_data (const T &data)
25917 {
25918 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25919 reinterpret_cast<const gdb_byte *> (&data + 1),
25920 grow (sizeof (data)));
25921 }
25922
25923 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25924 terminating zero is appended too. */
25925 void append_cstr0 (const char *cstr)
25926 {
25927 const size_t size = strlen (cstr) + 1;
25928 std::copy (cstr, cstr + size, grow (size));
25929 }
25930
25931 /* Store INPUT as ULEB128 to the end of buffer. */
25932 void append_unsigned_leb128 (ULONGEST input)
25933 {
25934 for (;;)
25935 {
25936 gdb_byte output = input & 0x7f;
25937 input >>= 7;
25938 if (input)
25939 output |= 0x80;
25940 append_data (output);
25941 if (input == 0)
25942 break;
25943 }
25944 }
25945
25946 /* Accept a host-format integer in VAL and append it to the buffer
25947 as a target-format integer which is LEN bytes long. */
25948 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25949 {
25950 ::store_unsigned_integer (grow (len), len, byte_order, val);
25951 }
25952
25953 /* Return the size of the buffer. */
25954 size_t size () const
25955 {
25956 return m_vec.size ();
25957 }
25958
25959 /* Return true iff the buffer is empty. */
25960 bool empty () const
25961 {
25962 return m_vec.empty ();
25963 }
25964
25965 /* Write the buffer to FILE. */
25966 void file_write (FILE *file) const
25967 {
25968 ::file_write (file, m_vec);
25969 }
25970
25971 private:
25972 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25973 the start of the new block. */
25974 gdb_byte *grow (size_t size)
25975 {
25976 m_vec.resize (m_vec.size () + size);
25977 return &*m_vec.end () - size;
25978 }
25979
25980 gdb::byte_vector m_vec;
25981 };
25982
25983 /* An entry in the symbol table. */
25984 struct symtab_index_entry
25985 {
25986 /* The name of the symbol. */
25987 const char *name;
25988 /* The offset of the name in the constant pool. */
25989 offset_type index_offset;
25990 /* A sorted vector of the indices of all the CUs that hold an object
25991 of this name. */
25992 std::vector<offset_type> cu_indices;
25993 };
25994
25995 /* The symbol table. This is a power-of-2-sized hash table. */
25996 struct mapped_symtab
25997 {
25998 mapped_symtab ()
25999 {
26000 data.resize (1024);
26001 }
26002
26003 offset_type n_elements = 0;
26004 std::vector<symtab_index_entry> data;
26005 };
26006
26007 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
26008 the slot.
26009
26010 Function is used only during write_hash_table so no index format backward
26011 compatibility is needed. */
26012
26013 static symtab_index_entry &
26014 find_slot (struct mapped_symtab *symtab, const char *name)
26015 {
26016 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
26017
26018 index = hash & (symtab->data.size () - 1);
26019 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
26020
26021 for (;;)
26022 {
26023 if (symtab->data[index].name == NULL
26024 || strcmp (name, symtab->data[index].name) == 0)
26025 return symtab->data[index];
26026 index = (index + step) & (symtab->data.size () - 1);
26027 }
26028 }
26029
26030 /* Expand SYMTAB's hash table. */
26031
26032 static void
26033 hash_expand (struct mapped_symtab *symtab)
26034 {
26035 auto old_entries = std::move (symtab->data);
26036
26037 symtab->data.clear ();
26038 symtab->data.resize (old_entries.size () * 2);
26039
26040 for (auto &it : old_entries)
26041 if (it.name != NULL)
26042 {
26043 auto &ref = find_slot (symtab, it.name);
26044 ref = std::move (it);
26045 }
26046 }
26047
26048 /* Add an entry to SYMTAB. NAME is the name of the symbol.
26049 CU_INDEX is the index of the CU in which the symbol appears.
26050 IS_STATIC is one if the symbol is static, otherwise zero (global). */
26051
26052 static void
26053 add_index_entry (struct mapped_symtab *symtab, const char *name,
26054 int is_static, gdb_index_symbol_kind kind,
26055 offset_type cu_index)
26056 {
26057 offset_type cu_index_and_attrs;
26058
26059 ++symtab->n_elements;
26060 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
26061 hash_expand (symtab);
26062
26063 symtab_index_entry &slot = find_slot (symtab, name);
26064 if (slot.name == NULL)
26065 {
26066 slot.name = name;
26067 /* index_offset is set later. */
26068 }
26069
26070 cu_index_and_attrs = 0;
26071 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
26072 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
26073 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
26074
26075 /* We don't want to record an index value twice as we want to avoid the
26076 duplication.
26077 We process all global symbols and then all static symbols
26078 (which would allow us to avoid the duplication by only having to check
26079 the last entry pushed), but a symbol could have multiple kinds in one CU.
26080 To keep things simple we don't worry about the duplication here and
26081 sort and uniqufy the list after we've processed all symbols. */
26082 slot.cu_indices.push_back (cu_index_and_attrs);
26083 }
26084
26085 /* Sort and remove duplicates of all symbols' cu_indices lists. */
26086
26087 static void
26088 uniquify_cu_indices (struct mapped_symtab *symtab)
26089 {
26090 for (auto &entry : symtab->data)
26091 {
26092 if (entry.name != NULL && !entry.cu_indices.empty ())
26093 {
26094 auto &cu_indices = entry.cu_indices;
26095 std::sort (cu_indices.begin (), cu_indices.end ());
26096 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
26097 cu_indices.erase (from, cu_indices.end ());
26098 }
26099 }
26100 }
26101
26102 /* A form of 'const char *' suitable for container keys. Only the
26103 pointer is stored. The strings themselves are compared, not the
26104 pointers. */
26105 class c_str_view
26106 {
26107 public:
26108 c_str_view (const char *cstr)
26109 : m_cstr (cstr)
26110 {}
26111
26112 bool operator== (const c_str_view &other) const
26113 {
26114 return strcmp (m_cstr, other.m_cstr) == 0;
26115 }
26116
26117 /* Return the underlying C string. Note, the returned string is
26118 only a reference with lifetime of this object. */
26119 const char *c_str () const
26120 {
26121 return m_cstr;
26122 }
26123
26124 private:
26125 friend class c_str_view_hasher;
26126 const char *const m_cstr;
26127 };
26128
26129 /* A std::unordered_map::hasher for c_str_view that uses the right
26130 hash function for strings in a mapped index. */
26131 class c_str_view_hasher
26132 {
26133 public:
26134 size_t operator () (const c_str_view &x) const
26135 {
26136 return mapped_index_string_hash (INT_MAX, x.m_cstr);
26137 }
26138 };
26139
26140 /* A std::unordered_map::hasher for std::vector<>. */
26141 template<typename T>
26142 class vector_hasher
26143 {
26144 public:
26145 size_t operator () (const std::vector<T> &key) const
26146 {
26147 return iterative_hash (key.data (),
26148 sizeof (key.front ()) * key.size (), 0);
26149 }
26150 };
26151
26152 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
26153 constant pool entries going into the data buffer CPOOL. */
26154
26155 static void
26156 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
26157 {
26158 {
26159 /* Elements are sorted vectors of the indices of all the CUs that
26160 hold an object of this name. */
26161 std::unordered_map<std::vector<offset_type>, offset_type,
26162 vector_hasher<offset_type>>
26163 symbol_hash_table;
26164
26165 /* We add all the index vectors to the constant pool first, to
26166 ensure alignment is ok. */
26167 for (symtab_index_entry &entry : symtab->data)
26168 {
26169 if (entry.name == NULL)
26170 continue;
26171 gdb_assert (entry.index_offset == 0);
26172
26173 /* Finding before inserting is faster than always trying to
26174 insert, because inserting always allocates a node, does the
26175 lookup, and then destroys the new node if another node
26176 already had the same key. C++17 try_emplace will avoid
26177 this. */
26178 const auto found
26179 = symbol_hash_table.find (entry.cu_indices);
26180 if (found != symbol_hash_table.end ())
26181 {
26182 entry.index_offset = found->second;
26183 continue;
26184 }
26185
26186 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
26187 entry.index_offset = cpool.size ();
26188 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
26189 for (const auto index : entry.cu_indices)
26190 cpool.append_data (MAYBE_SWAP (index));
26191 }
26192 }
26193
26194 /* Now write out the hash table. */
26195 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
26196 for (const auto &entry : symtab->data)
26197 {
26198 offset_type str_off, vec_off;
26199
26200 if (entry.name != NULL)
26201 {
26202 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
26203 if (insertpair.second)
26204 cpool.append_cstr0 (entry.name);
26205 str_off = insertpair.first->second;
26206 vec_off = entry.index_offset;
26207 }
26208 else
26209 {
26210 /* While 0 is a valid constant pool index, it is not valid
26211 to have 0 for both offsets. */
26212 str_off = 0;
26213 vec_off = 0;
26214 }
26215
26216 output.append_data (MAYBE_SWAP (str_off));
26217 output.append_data (MAYBE_SWAP (vec_off));
26218 }
26219 }
26220
26221 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
26222
26223 /* Helper struct for building the address table. */
26224 struct addrmap_index_data
26225 {
26226 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
26227 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
26228 {}
26229
26230 struct objfile *objfile;
26231 data_buf &addr_vec;
26232 psym_index_map &cu_index_htab;
26233
26234 /* Non-zero if the previous_* fields are valid.
26235 We can't write an entry until we see the next entry (since it is only then
26236 that we know the end of the entry). */
26237 int previous_valid;
26238 /* Index of the CU in the table of all CUs in the index file. */
26239 unsigned int previous_cu_index;
26240 /* Start address of the CU. */
26241 CORE_ADDR previous_cu_start;
26242 };
26243
26244 /* Write an address entry to ADDR_VEC. */
26245
26246 static void
26247 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
26248 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
26249 {
26250 CORE_ADDR baseaddr;
26251
26252 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
26253
26254 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
26255 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
26256 addr_vec.append_data (MAYBE_SWAP (cu_index));
26257 }
26258
26259 /* Worker function for traversing an addrmap to build the address table. */
26260
26261 static int
26262 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
26263 {
26264 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
26265 struct partial_symtab *pst = (struct partial_symtab *) obj;
26266
26267 if (data->previous_valid)
26268 add_address_entry (data->objfile, data->addr_vec,
26269 data->previous_cu_start, start_addr,
26270 data->previous_cu_index);
26271
26272 data->previous_cu_start = start_addr;
26273 if (pst != NULL)
26274 {
26275 const auto it = data->cu_index_htab.find (pst);
26276 gdb_assert (it != data->cu_index_htab.cend ());
26277 data->previous_cu_index = it->second;
26278 data->previous_valid = 1;
26279 }
26280 else
26281 data->previous_valid = 0;
26282
26283 return 0;
26284 }
26285
26286 /* Write OBJFILE's address map to ADDR_VEC.
26287 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
26288 in the index file. */
26289
26290 static void
26291 write_address_map (struct objfile *objfile, data_buf &addr_vec,
26292 psym_index_map &cu_index_htab)
26293 {
26294 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
26295
26296 /* When writing the address table, we have to cope with the fact that
26297 the addrmap iterator only provides the start of a region; we have to
26298 wait until the next invocation to get the start of the next region. */
26299
26300 addrmap_index_data.objfile = objfile;
26301 addrmap_index_data.previous_valid = 0;
26302
26303 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
26304 &addrmap_index_data);
26305
26306 /* It's highly unlikely the last entry (end address = 0xff...ff)
26307 is valid, but we should still handle it.
26308 The end address is recorded as the start of the next region, but that
26309 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
26310 anyway. */
26311 if (addrmap_index_data.previous_valid)
26312 add_address_entry (objfile, addr_vec,
26313 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
26314 addrmap_index_data.previous_cu_index);
26315 }
26316
26317 /* Return the symbol kind of PSYM. */
26318
26319 static gdb_index_symbol_kind
26320 symbol_kind (struct partial_symbol *psym)
26321 {
26322 domain_enum domain = PSYMBOL_DOMAIN (psym);
26323 enum address_class aclass = PSYMBOL_CLASS (psym);
26324
26325 switch (domain)
26326 {
26327 case VAR_DOMAIN:
26328 switch (aclass)
26329 {
26330 case LOC_BLOCK:
26331 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
26332 case LOC_TYPEDEF:
26333 return GDB_INDEX_SYMBOL_KIND_TYPE;
26334 case LOC_COMPUTED:
26335 case LOC_CONST_BYTES:
26336 case LOC_OPTIMIZED_OUT:
26337 case LOC_STATIC:
26338 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
26339 case LOC_CONST:
26340 /* Note: It's currently impossible to recognize psyms as enum values
26341 short of reading the type info. For now punt. */
26342 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
26343 default:
26344 /* There are other LOC_FOO values that one might want to classify
26345 as variables, but dwarf2read.c doesn't currently use them. */
26346 return GDB_INDEX_SYMBOL_KIND_OTHER;
26347 }
26348 case STRUCT_DOMAIN:
26349 return GDB_INDEX_SYMBOL_KIND_TYPE;
26350 default:
26351 return GDB_INDEX_SYMBOL_KIND_OTHER;
26352 }
26353 }
26354
26355 /* Add a list of partial symbols to SYMTAB. */
26356
26357 static void
26358 write_psymbols (struct mapped_symtab *symtab,
26359 std::unordered_set<partial_symbol *> &psyms_seen,
26360 struct partial_symbol **psymp,
26361 int count,
26362 offset_type cu_index,
26363 int is_static)
26364 {
26365 for (; count-- > 0; ++psymp)
26366 {
26367 struct partial_symbol *psym = *psymp;
26368
26369 if (SYMBOL_LANGUAGE (psym) == language_ada)
26370 error (_("Ada is not currently supported by the index"));
26371
26372 /* Only add a given psymbol once. */
26373 if (psyms_seen.insert (psym).second)
26374 {
26375 gdb_index_symbol_kind kind = symbol_kind (psym);
26376
26377 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
26378 is_static, kind, cu_index);
26379 }
26380 }
26381 }
26382
26383 /* A helper struct used when iterating over debug_types. */
26384 struct signatured_type_index_data
26385 {
26386 signatured_type_index_data (data_buf &types_list_,
26387 std::unordered_set<partial_symbol *> &psyms_seen_)
26388 : types_list (types_list_), psyms_seen (psyms_seen_)
26389 {}
26390
26391 struct objfile *objfile;
26392 struct mapped_symtab *symtab;
26393 data_buf &types_list;
26394 std::unordered_set<partial_symbol *> &psyms_seen;
26395 int cu_index;
26396 };
26397
26398 /* A helper function that writes a single signatured_type to an
26399 obstack. */
26400
26401 static int
26402 write_one_signatured_type (void **slot, void *d)
26403 {
26404 struct signatured_type_index_data *info
26405 = (struct signatured_type_index_data *) d;
26406 struct signatured_type *entry = (struct signatured_type *) *slot;
26407 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26408
26409 write_psymbols (info->symtab,
26410 info->psyms_seen,
26411 &info->objfile->global_psymbols[psymtab->globals_offset],
26412 psymtab->n_global_syms, info->cu_index,
26413 0);
26414 write_psymbols (info->symtab,
26415 info->psyms_seen,
26416 &info->objfile->static_psymbols[psymtab->statics_offset],
26417 psymtab->n_static_syms, info->cu_index,
26418 1);
26419
26420 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26421 to_underlying (entry->per_cu.sect_off));
26422 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26423 to_underlying (entry->type_offset_in_tu));
26424 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
26425
26426 ++info->cu_index;
26427
26428 return 1;
26429 }
26430
26431 /* Recurse into all "included" dependencies and count their symbols as
26432 if they appeared in this psymtab. */
26433
26434 static void
26435 recursively_count_psymbols (struct partial_symtab *psymtab,
26436 size_t &psyms_seen)
26437 {
26438 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26439 if (psymtab->dependencies[i]->user != NULL)
26440 recursively_count_psymbols (psymtab->dependencies[i],
26441 psyms_seen);
26442
26443 psyms_seen += psymtab->n_global_syms;
26444 psyms_seen += psymtab->n_static_syms;
26445 }
26446
26447 /* Recurse into all "included" dependencies and write their symbols as
26448 if they appeared in this psymtab. */
26449
26450 static void
26451 recursively_write_psymbols (struct objfile *objfile,
26452 struct partial_symtab *psymtab,
26453 struct mapped_symtab *symtab,
26454 std::unordered_set<partial_symbol *> &psyms_seen,
26455 offset_type cu_index)
26456 {
26457 int i;
26458
26459 for (i = 0; i < psymtab->number_of_dependencies; ++i)
26460 if (psymtab->dependencies[i]->user != NULL)
26461 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26462 symtab, psyms_seen, cu_index);
26463
26464 write_psymbols (symtab,
26465 psyms_seen,
26466 &objfile->global_psymbols[psymtab->globals_offset],
26467 psymtab->n_global_syms, cu_index,
26468 0);
26469 write_psymbols (symtab,
26470 psyms_seen,
26471 &objfile->static_psymbols[psymtab->statics_offset],
26472 psymtab->n_static_syms, cu_index,
26473 1);
26474 }
26475
26476 /* DWARF-5 .debug_names builder. */
26477 class debug_names
26478 {
26479 public:
26480 debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile, bool is_dwarf64,
26481 bfd_endian dwarf5_byte_order)
26482 : m_dwarf5_byte_order (dwarf5_byte_order),
26483 m_dwarf32 (dwarf5_byte_order),
26484 m_dwarf64 (dwarf5_byte_order),
26485 m_dwarf (is_dwarf64
26486 ? static_cast<dwarf &> (m_dwarf64)
26487 : static_cast<dwarf &> (m_dwarf32)),
26488 m_name_table_string_offs (m_dwarf.name_table_string_offs),
26489 m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
26490 m_debugstrlookup (dwarf2_per_objfile)
26491 {}
26492
26493 int dwarf5_offset_size () const
26494 {
26495 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
26496 return dwarf5_is_dwarf64 ? 8 : 4;
26497 }
26498
26499 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
26500 enum class unit_kind { cu, tu };
26501
26502 /* Insert one symbol. */
26503 void insert (const partial_symbol *psym, int cu_index, bool is_static,
26504 unit_kind kind)
26505 {
26506 const int dwarf_tag = psymbol_tag (psym);
26507 if (dwarf_tag == 0)
26508 return;
26509 const char *const name = SYMBOL_SEARCH_NAME (psym);
26510 const auto insertpair
26511 = m_name_to_value_set.emplace (c_str_view (name),
26512 std::set<symbol_value> ());
26513 std::set<symbol_value> &value_set = insertpair.first->second;
26514 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
26515 }
26516
26517 /* Build all the tables. All symbols must be already inserted.
26518 This function does not call file_write, caller has to do it
26519 afterwards. */
26520 void build ()
26521 {
26522 /* Verify the build method has not be called twice. */
26523 gdb_assert (m_abbrev_table.empty ());
26524 const size_t name_count = m_name_to_value_set.size ();
26525 m_bucket_table.resize
26526 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26527 m_hash_table.reserve (name_count);
26528 m_name_table_string_offs.reserve (name_count);
26529 m_name_table_entry_offs.reserve (name_count);
26530
26531 /* Map each hash of symbol to its name and value. */
26532 struct hash_it_pair
26533 {
26534 uint32_t hash;
26535 decltype (m_name_to_value_set)::const_iterator it;
26536 };
26537 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26538 bucket_hash.resize (m_bucket_table.size ());
26539 for (decltype (m_name_to_value_set)::const_iterator it
26540 = m_name_to_value_set.cbegin ();
26541 it != m_name_to_value_set.cend ();
26542 ++it)
26543 {
26544 const char *const name = it->first.c_str ();
26545 const uint32_t hash = dwarf5_djb_hash (name);
26546 hash_it_pair hashitpair;
26547 hashitpair.hash = hash;
26548 hashitpair.it = it;
26549 auto &slot = bucket_hash[hash % bucket_hash.size()];
26550 slot.push_front (std::move (hashitpair));
26551 }
26552 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26553 {
26554 const std::forward_list<hash_it_pair> &hashitlist
26555 = bucket_hash[bucket_ix];
26556 if (hashitlist.empty ())
26557 continue;
26558 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26559 /* The hashes array is indexed starting at 1. */
26560 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26561 sizeof (bucket_slot), m_dwarf5_byte_order,
26562 m_hash_table.size () + 1);
26563 for (const hash_it_pair &hashitpair : hashitlist)
26564 {
26565 m_hash_table.push_back (0);
26566 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26567 (&m_hash_table.back ()),
26568 sizeof (m_hash_table.back ()),
26569 m_dwarf5_byte_order, hashitpair.hash);
26570 const c_str_view &name = hashitpair.it->first;
26571 const std::set<symbol_value> &value_set = hashitpair.it->second;
26572 m_name_table_string_offs.push_back_reorder
26573 (m_debugstrlookup.lookup (name.c_str ()));
26574 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26575 gdb_assert (!value_set.empty ());
26576 for (const symbol_value &value : value_set)
26577 {
26578 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
26579 value.is_static,
26580 value.kind)];
26581 if (idx == 0)
26582 {
26583 idx = m_idx_next++;
26584 m_abbrev_table.append_unsigned_leb128 (idx);
26585 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
26586 m_abbrev_table.append_unsigned_leb128
26587 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
26588 : DW_IDX_type_unit);
26589 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26590 m_abbrev_table.append_unsigned_leb128 (value.is_static
26591 ? DW_IDX_GNU_internal
26592 : DW_IDX_GNU_external);
26593 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26594
26595 /* Terminate attributes list. */
26596 m_abbrev_table.append_unsigned_leb128 (0);
26597 m_abbrev_table.append_unsigned_leb128 (0);
26598 }
26599
26600 m_entry_pool.append_unsigned_leb128 (idx);
26601 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26602 }
26603
26604 /* Terminate the list of CUs. */
26605 m_entry_pool.append_unsigned_leb128 (0);
26606 }
26607 }
26608 gdb_assert (m_hash_table.size () == name_count);
26609
26610 /* Terminate tags list. */
26611 m_abbrev_table.append_unsigned_leb128 (0);
26612 }
26613
26614 /* Return .debug_names bucket count. This must be called only after
26615 calling the build method. */
26616 uint32_t bucket_count () const
26617 {
26618 /* Verify the build method has been already called. */
26619 gdb_assert (!m_abbrev_table.empty ());
26620 const uint32_t retval = m_bucket_table.size ();
26621
26622 /* Check for overflow. */
26623 gdb_assert (retval == m_bucket_table.size ());
26624 return retval;
26625 }
26626
26627 /* Return .debug_names names count. This must be called only after
26628 calling the build method. */
26629 uint32_t name_count () const
26630 {
26631 /* Verify the build method has been already called. */
26632 gdb_assert (!m_abbrev_table.empty ());
26633 const uint32_t retval = m_hash_table.size ();
26634
26635 /* Check for overflow. */
26636 gdb_assert (retval == m_hash_table.size ());
26637 return retval;
26638 }
26639
26640 /* Return number of bytes of .debug_names abbreviation table. This
26641 must be called only after calling the build method. */
26642 uint32_t abbrev_table_bytes () const
26643 {
26644 gdb_assert (!m_abbrev_table.empty ());
26645 return m_abbrev_table.size ();
26646 }
26647
26648 /* Recurse into all "included" dependencies and store their symbols
26649 as if they appeared in this psymtab. */
26650 void recursively_write_psymbols
26651 (struct objfile *objfile,
26652 struct partial_symtab *psymtab,
26653 std::unordered_set<partial_symbol *> &psyms_seen,
26654 int cu_index)
26655 {
26656 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26657 if (psymtab->dependencies[i]->user != NULL)
26658 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26659 psyms_seen, cu_index);
26660
26661 write_psymbols (psyms_seen,
26662 &objfile->global_psymbols[psymtab->globals_offset],
26663 psymtab->n_global_syms, cu_index, false, unit_kind::cu);
26664 write_psymbols (psyms_seen,
26665 &objfile->static_psymbols[psymtab->statics_offset],
26666 psymtab->n_static_syms, cu_index, true, unit_kind::cu);
26667 }
26668
26669 /* Return number of bytes the .debug_names section will have. This
26670 must be called only after calling the build method. */
26671 size_t bytes () const
26672 {
26673 /* Verify the build method has been already called. */
26674 gdb_assert (!m_abbrev_table.empty ());
26675 size_t expected_bytes = 0;
26676 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26677 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26678 expected_bytes += m_name_table_string_offs.bytes ();
26679 expected_bytes += m_name_table_entry_offs.bytes ();
26680 expected_bytes += m_abbrev_table.size ();
26681 expected_bytes += m_entry_pool.size ();
26682 return expected_bytes;
26683 }
26684
26685 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26686 FILE_STR. This must be called only after calling the build
26687 method. */
26688 void file_write (FILE *file_names, FILE *file_str) const
26689 {
26690 /* Verify the build method has been already called. */
26691 gdb_assert (!m_abbrev_table.empty ());
26692 ::file_write (file_names, m_bucket_table);
26693 ::file_write (file_names, m_hash_table);
26694 m_name_table_string_offs.file_write (file_names);
26695 m_name_table_entry_offs.file_write (file_names);
26696 m_abbrev_table.file_write (file_names);
26697 m_entry_pool.file_write (file_names);
26698 m_debugstrlookup.file_write (file_str);
26699 }
26700
26701 /* A helper user data for write_one_signatured_type. */
26702 class write_one_signatured_type_data
26703 {
26704 public:
26705 write_one_signatured_type_data (debug_names &nametable_,
26706 signatured_type_index_data &&info_)
26707 : nametable (nametable_), info (std::move (info_))
26708 {}
26709 debug_names &nametable;
26710 struct signatured_type_index_data info;
26711 };
26712
26713 /* A helper function to pass write_one_signatured_type to
26714 htab_traverse_noresize. */
26715 static int
26716 write_one_signatured_type (void **slot, void *d)
26717 {
26718 write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
26719 struct signatured_type_index_data *info = &data->info;
26720 struct signatured_type *entry = (struct signatured_type *) *slot;
26721
26722 data->nametable.write_one_signatured_type (entry, info);
26723
26724 return 1;
26725 }
26726
26727 private:
26728
26729 /* Storage for symbol names mapping them to their .debug_str section
26730 offsets. */
26731 class debug_str_lookup
26732 {
26733 public:
26734
26735 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26736 All .debug_str section strings are automatically stored. */
26737 debug_str_lookup (struct dwarf2_per_objfile *dwarf2_per_objfile)
26738 : m_abfd (dwarf2_per_objfile->objfile->obfd),
26739 m_dwarf2_per_objfile (dwarf2_per_objfile)
26740 {
26741 dwarf2_read_section (dwarf2_per_objfile->objfile,
26742 &dwarf2_per_objfile->str);
26743 if (dwarf2_per_objfile->str.buffer == NULL)
26744 return;
26745 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26746 data < (dwarf2_per_objfile->str.buffer
26747 + dwarf2_per_objfile->str.size);)
26748 {
26749 const char *const s = reinterpret_cast<const char *> (data);
26750 const auto insertpair
26751 = m_str_table.emplace (c_str_view (s),
26752 data - dwarf2_per_objfile->str.buffer);
26753 if (!insertpair.second)
26754 complaint (&symfile_complaints,
26755 _("Duplicate string \"%s\" in "
26756 ".debug_str section [in module %s]"),
26757 s, bfd_get_filename (m_abfd));
26758 data += strlen (s) + 1;
26759 }
26760 }
26761
26762 /* Return offset of symbol name S in the .debug_str section. Add
26763 such symbol to the section's end if it does not exist there
26764 yet. */
26765 size_t lookup (const char *s)
26766 {
26767 const auto it = m_str_table.find (c_str_view (s));
26768 if (it != m_str_table.end ())
26769 return it->second;
26770 const size_t offset = (m_dwarf2_per_objfile->str.size
26771 + m_str_add_buf.size ());
26772 m_str_table.emplace (c_str_view (s), offset);
26773 m_str_add_buf.append_cstr0 (s);
26774 return offset;
26775 }
26776
26777 /* Append the end of the .debug_str section to FILE. */
26778 void file_write (FILE *file) const
26779 {
26780 m_str_add_buf.file_write (file);
26781 }
26782
26783 private:
26784 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26785 bfd *const m_abfd;
26786 struct dwarf2_per_objfile *m_dwarf2_per_objfile;
26787
26788 /* Data to add at the end of .debug_str for new needed symbol names. */
26789 data_buf m_str_add_buf;
26790 };
26791
26792 /* Container to map used DWARF tags to their .debug_names abbreviation
26793 tags. */
26794 class index_key
26795 {
26796 public:
26797 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
26798 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
26799 {
26800 }
26801
26802 bool
26803 operator== (const index_key &other) const
26804 {
26805 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
26806 && kind == other.kind);
26807 }
26808
26809 const int dwarf_tag;
26810 const bool is_static;
26811 const unit_kind kind;
26812 };
26813
26814 /* Provide std::unordered_map::hasher for index_key. */
26815 class index_key_hasher
26816 {
26817 public:
26818 size_t
26819 operator () (const index_key &key) const
26820 {
26821 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26822 }
26823 };
26824
26825 /* Parameters of one symbol entry. */
26826 class symbol_value
26827 {
26828 public:
26829 const int dwarf_tag, cu_index;
26830 const bool is_static;
26831 const unit_kind kind;
26832
26833 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
26834 unit_kind kind_)
26835 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
26836 kind (kind_)
26837 {}
26838
26839 bool
26840 operator< (const symbol_value &other) const
26841 {
26842 #define X(n) \
26843 do \
26844 { \
26845 if (n < other.n) \
26846 return true; \
26847 if (n > other.n) \
26848 return false; \
26849 } \
26850 while (0)
26851 X (dwarf_tag);
26852 X (is_static);
26853 X (kind);
26854 X (cu_index);
26855 #undef X
26856 return false;
26857 }
26858 };
26859
26860 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26861 output. */
26862 class offset_vec
26863 {
26864 protected:
26865 const bfd_endian dwarf5_byte_order;
26866 public:
26867 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26868 : dwarf5_byte_order (dwarf5_byte_order_)
26869 {}
26870
26871 /* Call std::vector::reserve for NELEM elements. */
26872 virtual void reserve (size_t nelem) = 0;
26873
26874 /* Call std::vector::push_back with store_unsigned_integer byte
26875 reordering for ELEM. */
26876 virtual void push_back_reorder (size_t elem) = 0;
26877
26878 /* Return expected output size in bytes. */
26879 virtual size_t bytes () const = 0;
26880
26881 /* Write name table to FILE. */
26882 virtual void file_write (FILE *file) const = 0;
26883 };
26884
26885 /* Template to unify DWARF-32 and DWARF-64 output. */
26886 template<typename OffsetSize>
26887 class offset_vec_tmpl : public offset_vec
26888 {
26889 public:
26890 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26891 : offset_vec (dwarf5_byte_order_)
26892 {}
26893
26894 /* Implement offset_vec::reserve. */
26895 void reserve (size_t nelem) override
26896 {
26897 m_vec.reserve (nelem);
26898 }
26899
26900 /* Implement offset_vec::push_back_reorder. */
26901 void push_back_reorder (size_t elem) override
26902 {
26903 m_vec.push_back (elem);
26904 /* Check for overflow. */
26905 gdb_assert (m_vec.back () == elem);
26906 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26907 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26908 }
26909
26910 /* Implement offset_vec::bytes. */
26911 size_t bytes () const override
26912 {
26913 return m_vec.size () * sizeof (m_vec[0]);
26914 }
26915
26916 /* Implement offset_vec::file_write. */
26917 void file_write (FILE *file) const override
26918 {
26919 ::file_write (file, m_vec);
26920 }
26921
26922 private:
26923 std::vector<OffsetSize> m_vec;
26924 };
26925
26926 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26927 respecting name table width. */
26928 class dwarf
26929 {
26930 public:
26931 offset_vec &name_table_string_offs, &name_table_entry_offs;
26932
26933 dwarf (offset_vec &name_table_string_offs_,
26934 offset_vec &name_table_entry_offs_)
26935 : name_table_string_offs (name_table_string_offs_),
26936 name_table_entry_offs (name_table_entry_offs_)
26937 {
26938 }
26939 };
26940
26941 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26942 respecting name table width. */
26943 template<typename OffsetSize>
26944 class dwarf_tmpl : public dwarf
26945 {
26946 public:
26947 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26948 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26949 m_name_table_string_offs (dwarf5_byte_order_),
26950 m_name_table_entry_offs (dwarf5_byte_order_)
26951 {}
26952
26953 private:
26954 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26955 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26956 };
26957
26958 /* Try to reconstruct original DWARF tag for given partial_symbol.
26959 This function is not DWARF-5 compliant but it is sufficient for
26960 GDB as a DWARF-5 index consumer. */
26961 static int psymbol_tag (const struct partial_symbol *psym)
26962 {
26963 domain_enum domain = PSYMBOL_DOMAIN (psym);
26964 enum address_class aclass = PSYMBOL_CLASS (psym);
26965
26966 switch (domain)
26967 {
26968 case VAR_DOMAIN:
26969 switch (aclass)
26970 {
26971 case LOC_BLOCK:
26972 return DW_TAG_subprogram;
26973 case LOC_TYPEDEF:
26974 return DW_TAG_typedef;
26975 case LOC_COMPUTED:
26976 case LOC_CONST_BYTES:
26977 case LOC_OPTIMIZED_OUT:
26978 case LOC_STATIC:
26979 return DW_TAG_variable;
26980 case LOC_CONST:
26981 /* Note: It's currently impossible to recognize psyms as enum values
26982 short of reading the type info. For now punt. */
26983 return DW_TAG_variable;
26984 default:
26985 /* There are other LOC_FOO values that one might want to classify
26986 as variables, but dwarf2read.c doesn't currently use them. */
26987 return DW_TAG_variable;
26988 }
26989 case STRUCT_DOMAIN:
26990 return DW_TAG_structure_type;
26991 default:
26992 return 0;
26993 }
26994 }
26995
26996 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26997 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
26998 struct partial_symbol **psymp, int count, int cu_index,
26999 bool is_static, unit_kind kind)
27000 {
27001 for (; count-- > 0; ++psymp)
27002 {
27003 struct partial_symbol *psym = *psymp;
27004
27005 if (SYMBOL_LANGUAGE (psym) == language_ada)
27006 error (_("Ada is not currently supported by the index"));
27007
27008 /* Only add a given psymbol once. */
27009 if (psyms_seen.insert (psym).second)
27010 insert (psym, cu_index, is_static, kind);
27011 }
27012 }
27013
27014 /* A helper function that writes a single signatured_type
27015 to a debug_names. */
27016 void
27017 write_one_signatured_type (struct signatured_type *entry,
27018 struct signatured_type_index_data *info)
27019 {
27020 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
27021
27022 write_psymbols (info->psyms_seen,
27023 &info->objfile->global_psymbols[psymtab->globals_offset],
27024 psymtab->n_global_syms, info->cu_index, false,
27025 unit_kind::tu);
27026 write_psymbols (info->psyms_seen,
27027 &info->objfile->static_psymbols[psymtab->statics_offset],
27028 psymtab->n_static_syms, info->cu_index, true,
27029 unit_kind::tu);
27030
27031 info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
27032 to_underlying (entry->per_cu.sect_off));
27033
27034 ++info->cu_index;
27035 }
27036
27037 /* Store value of each symbol. */
27038 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
27039 m_name_to_value_set;
27040
27041 /* Tables of DWARF-5 .debug_names. They are in object file byte
27042 order. */
27043 std::vector<uint32_t> m_bucket_table;
27044 std::vector<uint32_t> m_hash_table;
27045
27046 const bfd_endian m_dwarf5_byte_order;
27047 dwarf_tmpl<uint32_t> m_dwarf32;
27048 dwarf_tmpl<uint64_t> m_dwarf64;
27049 dwarf &m_dwarf;
27050 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
27051 debug_str_lookup m_debugstrlookup;
27052
27053 /* Map each used .debug_names abbreviation tag parameter to its
27054 index value. */
27055 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
27056
27057 /* Next unused .debug_names abbreviation tag for
27058 m_indexkey_to_idx. */
27059 int m_idx_next = 1;
27060
27061 /* .debug_names abbreviation table. */
27062 data_buf m_abbrev_table;
27063
27064 /* .debug_names entry pool. */
27065 data_buf m_entry_pool;
27066 };
27067
27068 /* Return iff any of the needed offsets does not fit into 32-bit
27069 .debug_names section. */
27070
27071 static bool
27072 check_dwarf64_offsets (struct dwarf2_per_objfile *dwarf2_per_objfile)
27073 {
27074 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27075 {
27076 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
27077
27078 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
27079 return true;
27080 }
27081 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
27082 {
27083 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
27084 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
27085
27086 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
27087 return true;
27088 }
27089 return false;
27090 }
27091
27092 /* The psyms_seen set is potentially going to be largish (~40k
27093 elements when indexing a -g3 build of GDB itself). Estimate the
27094 number of elements in order to avoid too many rehashes, which
27095 require rebuilding buckets and thus many trips to
27096 malloc/free. */
27097
27098 static size_t
27099 psyms_seen_size (struct dwarf2_per_objfile *dwarf2_per_objfile)
27100 {
27101 size_t psyms_count = 0;
27102 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27103 {
27104 struct dwarf2_per_cu_data *per_cu
27105 = dwarf2_per_objfile->all_comp_units[i];
27106 struct partial_symtab *psymtab = per_cu->v.psymtab;
27107
27108 if (psymtab != NULL && psymtab->user == NULL)
27109 recursively_count_psymbols (psymtab, psyms_count);
27110 }
27111 /* Generating an index for gdb itself shows a ratio of
27112 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
27113 return psyms_count / 4;
27114 }
27115
27116 /* Write new .gdb_index section for OBJFILE into OUT_FILE.
27117 Return how many bytes were expected to be written into OUT_FILE. */
27118
27119 static size_t
27120 write_gdbindex (struct dwarf2_per_objfile *dwarf2_per_objfile, FILE *out_file)
27121 {
27122 struct objfile *objfile = dwarf2_per_objfile->objfile;
27123 mapped_symtab symtab;
27124 data_buf cu_list;
27125
27126 /* While we're scanning CU's create a table that maps a psymtab pointer
27127 (which is what addrmap records) to its index (which is what is recorded
27128 in the index file). This will later be needed to write the address
27129 table. */
27130 psym_index_map cu_index_htab;
27131 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
27132
27133 /* The CU list is already sorted, so we don't need to do additional
27134 work here. Also, the debug_types entries do not appear in
27135 all_comp_units, but only in their own hash table. */
27136
27137 std::unordered_set<partial_symbol *> psyms_seen
27138 (psyms_seen_size (dwarf2_per_objfile));
27139 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27140 {
27141 struct dwarf2_per_cu_data *per_cu
27142 = dwarf2_per_objfile->all_comp_units[i];
27143 struct partial_symtab *psymtab = per_cu->v.psymtab;
27144
27145 /* CU of a shared file from 'dwz -m' may be unused by this main file.
27146 It may be referenced from a local scope but in such case it does not
27147 need to be present in .gdb_index. */
27148 if (psymtab == NULL)
27149 continue;
27150
27151 if (psymtab->user == NULL)
27152 recursively_write_psymbols (objfile, psymtab, &symtab,
27153 psyms_seen, i);
27154
27155 const auto insertpair = cu_index_htab.emplace (psymtab, i);
27156 gdb_assert (insertpair.second);
27157
27158 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
27159 to_underlying (per_cu->sect_off));
27160 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
27161 }
27162
27163 /* Dump the address map. */
27164 data_buf addr_vec;
27165 write_address_map (objfile, addr_vec, cu_index_htab);
27166
27167 /* Write out the .debug_type entries, if any. */
27168 data_buf types_cu_list;
27169 if (dwarf2_per_objfile->signatured_types)
27170 {
27171 signatured_type_index_data sig_data (types_cu_list,
27172 psyms_seen);
27173
27174 sig_data.objfile = objfile;
27175 sig_data.symtab = &symtab;
27176 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
27177 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
27178 write_one_signatured_type, &sig_data);
27179 }
27180
27181 /* Now that we've processed all symbols we can shrink their cu_indices
27182 lists. */
27183 uniquify_cu_indices (&symtab);
27184
27185 data_buf symtab_vec, constant_pool;
27186 write_hash_table (&symtab, symtab_vec, constant_pool);
27187
27188 data_buf contents;
27189 const offset_type size_of_contents = 6 * sizeof (offset_type);
27190 offset_type total_len = size_of_contents;
27191
27192 /* The version number. */
27193 contents.append_data (MAYBE_SWAP (8));
27194
27195 /* The offset of the CU list from the start of the file. */
27196 contents.append_data (MAYBE_SWAP (total_len));
27197 total_len += cu_list.size ();
27198
27199 /* The offset of the types CU list from the start of the file. */
27200 contents.append_data (MAYBE_SWAP (total_len));
27201 total_len += types_cu_list.size ();
27202
27203 /* The offset of the address table from the start of the file. */
27204 contents.append_data (MAYBE_SWAP (total_len));
27205 total_len += addr_vec.size ();
27206
27207 /* The offset of the symbol table from the start of the file. */
27208 contents.append_data (MAYBE_SWAP (total_len));
27209 total_len += symtab_vec.size ();
27210
27211 /* The offset of the constant pool from the start of the file. */
27212 contents.append_data (MAYBE_SWAP (total_len));
27213 total_len += constant_pool.size ();
27214
27215 gdb_assert (contents.size () == size_of_contents);
27216
27217 contents.file_write (out_file);
27218 cu_list.file_write (out_file);
27219 types_cu_list.file_write (out_file);
27220 addr_vec.file_write (out_file);
27221 symtab_vec.file_write (out_file);
27222 constant_pool.file_write (out_file);
27223
27224 return total_len;
27225 }
27226
27227 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
27228 static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
27229
27230 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
27231 needed addition to .debug_str section to OUT_FILE_STR. Return how
27232 many bytes were expected to be written into OUT_FILE. */
27233
27234 static size_t
27235 write_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
27236 FILE *out_file, FILE *out_file_str)
27237 {
27238 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (dwarf2_per_objfile);
27239 struct objfile *objfile = dwarf2_per_objfile->objfile;
27240 const enum bfd_endian dwarf5_byte_order
27241 = gdbarch_byte_order (get_objfile_arch (objfile));
27242
27243 /* The CU list is already sorted, so we don't need to do additional
27244 work here. Also, the debug_types entries do not appear in
27245 all_comp_units, but only in their own hash table. */
27246 data_buf cu_list;
27247 debug_names nametable (dwarf2_per_objfile, dwarf5_is_dwarf64,
27248 dwarf5_byte_order);
27249 std::unordered_set<partial_symbol *>
27250 psyms_seen (psyms_seen_size (dwarf2_per_objfile));
27251 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27252 {
27253 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
27254 partial_symtab *psymtab = per_cu->v.psymtab;
27255
27256 /* CU of a shared file from 'dwz -m' may be unused by this main
27257 file. It may be referenced from a local scope but in such
27258 case it does not need to be present in .debug_names. */
27259 if (psymtab == NULL)
27260 continue;
27261
27262 if (psymtab->user == NULL)
27263 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
27264
27265 cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
27266 to_underlying (per_cu->sect_off));
27267 }
27268
27269 /* Write out the .debug_type entries, if any. */
27270 data_buf types_cu_list;
27271 if (dwarf2_per_objfile->signatured_types)
27272 {
27273 debug_names::write_one_signatured_type_data sig_data (nametable,
27274 signatured_type_index_data (types_cu_list, psyms_seen));
27275
27276 sig_data.info.objfile = objfile;
27277 /* It is used only for gdb_index. */
27278 sig_data.info.symtab = nullptr;
27279 sig_data.info.cu_index = 0;
27280 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
27281 debug_names::write_one_signatured_type,
27282 &sig_data);
27283 }
27284
27285 nametable.build ();
27286
27287 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
27288
27289 const offset_type bytes_of_header
27290 = ((dwarf5_is_dwarf64 ? 12 : 4)
27291 + 2 + 2 + 7 * 4
27292 + sizeof (dwarf5_gdb_augmentation));
27293 size_t expected_bytes = 0;
27294 expected_bytes += bytes_of_header;
27295 expected_bytes += cu_list.size ();
27296 expected_bytes += types_cu_list.size ();
27297 expected_bytes += nametable.bytes ();
27298 data_buf header;
27299
27300 if (!dwarf5_is_dwarf64)
27301 {
27302 const uint64_t size64 = expected_bytes - 4;
27303 gdb_assert (size64 < 0xfffffff0);
27304 header.append_uint (4, dwarf5_byte_order, size64);
27305 }
27306 else
27307 {
27308 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
27309 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
27310 }
27311
27312 /* The version number. */
27313 header.append_uint (2, dwarf5_byte_order, 5);
27314
27315 /* Padding. */
27316 header.append_uint (2, dwarf5_byte_order, 0);
27317
27318 /* comp_unit_count - The number of CUs in the CU list. */
27319 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
27320
27321 /* local_type_unit_count - The number of TUs in the local TU
27322 list. */
27323 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
27324
27325 /* foreign_type_unit_count - The number of TUs in the foreign TU
27326 list. */
27327 header.append_uint (4, dwarf5_byte_order, 0);
27328
27329 /* bucket_count - The number of hash buckets in the hash lookup
27330 table. */
27331 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
27332
27333 /* name_count - The number of unique names in the index. */
27334 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
27335
27336 /* abbrev_table_size - The size in bytes of the abbreviations
27337 table. */
27338 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
27339
27340 /* augmentation_string_size - The size in bytes of the augmentation
27341 string. This value is rounded up to a multiple of 4. */
27342 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
27343 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
27344 header.append_data (dwarf5_gdb_augmentation);
27345
27346 gdb_assert (header.size () == bytes_of_header);
27347
27348 header.file_write (out_file);
27349 cu_list.file_write (out_file);
27350 types_cu_list.file_write (out_file);
27351 nametable.file_write (out_file, out_file_str);
27352
27353 return expected_bytes;
27354 }
27355
27356 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
27357 position is at the end of the file. */
27358
27359 static void
27360 assert_file_size (FILE *file, const char *filename, size_t expected_size)
27361 {
27362 const auto file_size = ftell (file);
27363 if (file_size == -1)
27364 error (_("Can't get `%s' size"), filename);
27365 gdb_assert (file_size == expected_size);
27366 }
27367
27368 /* Create an index file for OBJFILE in the directory DIR. */
27369
27370 static void
27371 write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
27372 const char *dir,
27373 dw_index_kind index_kind)
27374 {
27375 struct objfile *objfile = dwarf2_per_objfile->objfile;
27376
27377 if (dwarf2_per_objfile->using_index)
27378 error (_("Cannot use an index to create the index"));
27379
27380 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
27381 error (_("Cannot make an index when the file has multiple .debug_types sections"));
27382
27383 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
27384 return;
27385
27386 struct stat st;
27387 if (stat (objfile_name (objfile), &st) < 0)
27388 perror_with_name (objfile_name (objfile));
27389
27390 std::string filename (std::string (dir) + SLASH_STRING
27391 + lbasename (objfile_name (objfile))
27392 + (index_kind == dw_index_kind::DEBUG_NAMES
27393 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
27394
27395 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
27396 if (!out_file)
27397 error (_("Can't open `%s' for writing"), filename.c_str ());
27398
27399 /* Order matters here; we want FILE to be closed before FILENAME is
27400 unlinked, because on MS-Windows one cannot delete a file that is
27401 still open. (Don't call anything here that might throw until
27402 file_closer is created.) */
27403 gdb::unlinker unlink_file (filename.c_str ());
27404 gdb_file_up close_out_file (out_file);
27405
27406 if (index_kind == dw_index_kind::DEBUG_NAMES)
27407 {
27408 std::string filename_str (std::string (dir) + SLASH_STRING
27409 + lbasename (objfile_name (objfile))
27410 + DEBUG_STR_SUFFIX);
27411 FILE *out_file_str
27412 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
27413 if (!out_file_str)
27414 error (_("Can't open `%s' for writing"), filename_str.c_str ());
27415 gdb::unlinker unlink_file_str (filename_str.c_str ());
27416 gdb_file_up close_out_file_str (out_file_str);
27417
27418 const size_t total_len
27419 = write_debug_names (dwarf2_per_objfile, out_file, out_file_str);
27420 assert_file_size (out_file, filename.c_str (), total_len);
27421
27422 /* We want to keep the file .debug_str file too. */
27423 unlink_file_str.keep ();
27424 }
27425 else
27426 {
27427 const size_t total_len
27428 = write_gdbindex (dwarf2_per_objfile, out_file);
27429 assert_file_size (out_file, filename.c_str (), total_len);
27430 }
27431
27432 /* We want to keep the file. */
27433 unlink_file.keep ();
27434 }
27435
27436 /* Implementation of the `save gdb-index' command.
27437
27438 Note that the .gdb_index file format used by this command is
27439 documented in the GDB manual. Any changes here must be documented
27440 there. */
27441
27442 static void
27443 save_gdb_index_command (const char *arg, int from_tty)
27444 {
27445 struct objfile *objfile;
27446 const char dwarf5space[] = "-dwarf-5 ";
27447 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
27448
27449 if (!arg)
27450 arg = "";
27451
27452 arg = skip_spaces (arg);
27453 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
27454 {
27455 index_kind = dw_index_kind::DEBUG_NAMES;
27456 arg += strlen (dwarf5space);
27457 arg = skip_spaces (arg);
27458 }
27459
27460 if (!*arg)
27461 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
27462
27463 ALL_OBJFILES (objfile)
27464 {
27465 struct stat st;
27466
27467 /* If the objfile does not correspond to an actual file, skip it. */
27468 if (stat (objfile_name (objfile), &st) < 0)
27469 continue;
27470
27471 struct dwarf2_per_objfile *dwarf2_per_objfile
27472 = get_dwarf2_per_objfile (objfile);
27473
27474 if (dwarf2_per_objfile != NULL)
27475 {
27476 TRY
27477 {
27478 write_psymtabs_to_index (dwarf2_per_objfile, arg, index_kind);
27479 }
27480 CATCH (except, RETURN_MASK_ERROR)
27481 {
27482 exception_fprintf (gdb_stderr, except,
27483 _("Error while writing index for `%s': "),
27484 objfile_name (objfile));
27485 }
27486 END_CATCH
27487 }
27488
27489 }
27490 }
27491
27492 \f
27493
27494 int dwarf_always_disassemble;
27495
27496 static void
27497 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
27498 struct cmd_list_element *c, const char *value)
27499 {
27500 fprintf_filtered (file,
27501 _("Whether to always disassemble "
27502 "DWARF expressions is %s.\n"),
27503 value);
27504 }
27505
27506 static void
27507 show_check_physname (struct ui_file *file, int from_tty,
27508 struct cmd_list_element *c, const char *value)
27509 {
27510 fprintf_filtered (file,
27511 _("Whether to check \"physname\" is %s.\n"),
27512 value);
27513 }
27514
27515 void
27516 _initialize_dwarf2_read (void)
27517 {
27518 struct cmd_list_element *c;
27519
27520 dwarf2_objfile_data_key = register_objfile_data ();
27521
27522 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
27523 Set DWARF specific variables.\n\
27524 Configure DWARF variables such as the cache size"),
27525 &set_dwarf_cmdlist, "maintenance set dwarf ",
27526 0/*allow-unknown*/, &maintenance_set_cmdlist);
27527
27528 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
27529 Show DWARF specific variables\n\
27530 Show DWARF variables such as the cache size"),
27531 &show_dwarf_cmdlist, "maintenance show dwarf ",
27532 0/*allow-unknown*/, &maintenance_show_cmdlist);
27533
27534 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
27535 &dwarf_max_cache_age, _("\
27536 Set the upper bound on the age of cached DWARF compilation units."), _("\
27537 Show the upper bound on the age of cached DWARF compilation units."), _("\
27538 A higher limit means that cached compilation units will be stored\n\
27539 in memory longer, and more total memory will be used. Zero disables\n\
27540 caching, which can slow down startup."),
27541 NULL,
27542 show_dwarf_max_cache_age,
27543 &set_dwarf_cmdlist,
27544 &show_dwarf_cmdlist);
27545
27546 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
27547 &dwarf_always_disassemble, _("\
27548 Set whether `info address' always disassembles DWARF expressions."), _("\
27549 Show whether `info address' always disassembles DWARF expressions."), _("\
27550 When enabled, DWARF expressions are always printed in an assembly-like\n\
27551 syntax. When disabled, expressions will be printed in a more\n\
27552 conversational style, when possible."),
27553 NULL,
27554 show_dwarf_always_disassemble,
27555 &set_dwarf_cmdlist,
27556 &show_dwarf_cmdlist);
27557
27558 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27559 Set debugging of the DWARF reader."), _("\
27560 Show debugging of the DWARF reader."), _("\
27561 When enabled (non-zero), debugging messages are printed during DWARF\n\
27562 reading and symtab expansion. A value of 1 (one) provides basic\n\
27563 information. A value greater than 1 provides more verbose information."),
27564 NULL,
27565 NULL,
27566 &setdebuglist, &showdebuglist);
27567
27568 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27569 Set debugging of the DWARF DIE reader."), _("\
27570 Show debugging of the DWARF DIE reader."), _("\
27571 When enabled (non-zero), DIEs are dumped after they are read in.\n\
27572 The value is the maximum depth to print."),
27573 NULL,
27574 NULL,
27575 &setdebuglist, &showdebuglist);
27576
27577 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27578 Set debugging of the dwarf line reader."), _("\
27579 Show debugging of the dwarf line reader."), _("\
27580 When enabled (non-zero), line number entries are dumped as they are read in.\n\
27581 A value of 1 (one) provides basic information.\n\
27582 A value greater than 1 provides more verbose information."),
27583 NULL,
27584 NULL,
27585 &setdebuglist, &showdebuglist);
27586
27587 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27588 Set cross-checking of \"physname\" code against demangler."), _("\
27589 Show cross-checking of \"physname\" code against demangler."), _("\
27590 When enabled, GDB's internal \"physname\" code is checked against\n\
27591 the demangler."),
27592 NULL, show_check_physname,
27593 &setdebuglist, &showdebuglist);
27594
27595 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27596 no_class, &use_deprecated_index_sections, _("\
27597 Set whether to use deprecated gdb_index sections."), _("\
27598 Show whether to use deprecated gdb_index sections."), _("\
27599 When enabled, deprecated .gdb_index sections are used anyway.\n\
27600 Normally they are ignored either because of a missing feature or\n\
27601 performance issue.\n\
27602 Warning: This option must be enabled before gdb reads the file."),
27603 NULL,
27604 NULL,
27605 &setlist, &showlist);
27606
27607 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
27608 _("\
27609 Save a gdb-index file.\n\
27610 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27611 \n\
27612 No options create one file with .gdb-index extension for pre-DWARF-5\n\
27613 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27614 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
27615 &save_cmdlist);
27616 set_cmd_completer (c, filename_completer);
27617
27618 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27619 &dwarf2_locexpr_funcs);
27620 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27621 &dwarf2_loclist_funcs);
27622
27623 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27624 &dwarf2_block_frame_base_locexpr_funcs);
27625 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27626 &dwarf2_block_frame_base_loclist_funcs);
27627
27628 #if GDB_SELF_TEST
27629 selftests::register_test ("dw2_expand_symtabs_matching",
27630 selftests::dw2_expand_symtabs_matching::run_test);
27631 #endif
27632 }
This page took 0.667396 seconds and 4 git commands to generate.