Use std::vector for field lists in dwarf2read.c
[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 && pdi->has_children)
9120 scan_partial_symbols (pdi->die_child, lowpc, highpc,
9121 set_addrmap, cu);
9122 break;
9123 case DW_TAG_enumeration_type:
9124 if (!pdi->is_declaration)
9125 add_partial_enumeration (pdi, cu);
9126 break;
9127 case DW_TAG_base_type:
9128 case DW_TAG_subrange_type:
9129 /* File scope base type definitions are added to the partial
9130 symbol table. */
9131 add_partial_symbol (pdi, cu);
9132 break;
9133 case DW_TAG_namespace:
9134 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
9135 break;
9136 case DW_TAG_module:
9137 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
9138 break;
9139 case DW_TAG_imported_unit:
9140 {
9141 struct dwarf2_per_cu_data *per_cu;
9142
9143 /* For now we don't handle imported units in type units. */
9144 if (cu->per_cu->is_debug_types)
9145 {
9146 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9147 " supported in type units [in module %s]"),
9148 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9149 }
9150
9151 per_cu = dwarf2_find_containing_comp_unit
9152 (pdi->d.sect_off, pdi->is_dwz,
9153 cu->per_cu->dwarf2_per_objfile);
9154
9155 /* Go read the partial unit, if needed. */
9156 if (per_cu->v.psymtab == NULL)
9157 process_psymtab_comp_unit (per_cu, 1, cu->language);
9158
9159 VEC_safe_push (dwarf2_per_cu_ptr,
9160 cu->per_cu->imported_symtabs, per_cu);
9161 }
9162 break;
9163 case DW_TAG_imported_declaration:
9164 add_partial_symbol (pdi, cu);
9165 break;
9166 default:
9167 break;
9168 }
9169 }
9170
9171 /* If the die has a sibling, skip to the sibling. */
9172
9173 pdi = pdi->die_sibling;
9174 }
9175 }
9176
9177 /* Functions used to compute the fully scoped name of a partial DIE.
9178
9179 Normally, this is simple. For C++, the parent DIE's fully scoped
9180 name is concatenated with "::" and the partial DIE's name.
9181 Enumerators are an exception; they use the scope of their parent
9182 enumeration type, i.e. the name of the enumeration type is not
9183 prepended to the enumerator.
9184
9185 There are two complexities. One is DW_AT_specification; in this
9186 case "parent" means the parent of the target of the specification,
9187 instead of the direct parent of the DIE. The other is compilers
9188 which do not emit DW_TAG_namespace; in this case we try to guess
9189 the fully qualified name of structure types from their members'
9190 linkage names. This must be done using the DIE's children rather
9191 than the children of any DW_AT_specification target. We only need
9192 to do this for structures at the top level, i.e. if the target of
9193 any DW_AT_specification (if any; otherwise the DIE itself) does not
9194 have a parent. */
9195
9196 /* Compute the scope prefix associated with PDI's parent, in
9197 compilation unit CU. The result will be allocated on CU's
9198 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9199 field. NULL is returned if no prefix is necessary. */
9200 static const char *
9201 partial_die_parent_scope (struct partial_die_info *pdi,
9202 struct dwarf2_cu *cu)
9203 {
9204 const char *grandparent_scope;
9205 struct partial_die_info *parent, *real_pdi;
9206
9207 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9208 then this means the parent of the specification DIE. */
9209
9210 real_pdi = pdi;
9211 while (real_pdi->has_specification)
9212 real_pdi = find_partial_die (real_pdi->spec_offset,
9213 real_pdi->spec_is_dwz, cu);
9214
9215 parent = real_pdi->die_parent;
9216 if (parent == NULL)
9217 return NULL;
9218
9219 if (parent->scope_set)
9220 return parent->scope;
9221
9222 parent->fixup (cu);
9223
9224 grandparent_scope = partial_die_parent_scope (parent, cu);
9225
9226 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9227 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9228 Work around this problem here. */
9229 if (cu->language == language_cplus
9230 && parent->tag == DW_TAG_namespace
9231 && strcmp (parent->name, "::") == 0
9232 && grandparent_scope == NULL)
9233 {
9234 parent->scope = NULL;
9235 parent->scope_set = 1;
9236 return NULL;
9237 }
9238
9239 if (pdi->tag == DW_TAG_enumerator)
9240 /* Enumerators should not get the name of the enumeration as a prefix. */
9241 parent->scope = grandparent_scope;
9242 else if (parent->tag == DW_TAG_namespace
9243 || parent->tag == DW_TAG_module
9244 || parent->tag == DW_TAG_structure_type
9245 || parent->tag == DW_TAG_class_type
9246 || parent->tag == DW_TAG_interface_type
9247 || parent->tag == DW_TAG_union_type
9248 || parent->tag == DW_TAG_enumeration_type)
9249 {
9250 if (grandparent_scope == NULL)
9251 parent->scope = parent->name;
9252 else
9253 parent->scope = typename_concat (&cu->comp_unit_obstack,
9254 grandparent_scope,
9255 parent->name, 0, cu);
9256 }
9257 else
9258 {
9259 /* FIXME drow/2004-04-01: What should we be doing with
9260 function-local names? For partial symbols, we should probably be
9261 ignoring them. */
9262 complaint (&symfile_complaints,
9263 _("unhandled containing DIE tag %d for DIE at %s"),
9264 parent->tag, sect_offset_str (pdi->sect_off));
9265 parent->scope = grandparent_scope;
9266 }
9267
9268 parent->scope_set = 1;
9269 return parent->scope;
9270 }
9271
9272 /* Return the fully scoped name associated with PDI, from compilation unit
9273 CU. The result will be allocated with malloc. */
9274
9275 static char *
9276 partial_die_full_name (struct partial_die_info *pdi,
9277 struct dwarf2_cu *cu)
9278 {
9279 const char *parent_scope;
9280
9281 /* If this is a template instantiation, we can not work out the
9282 template arguments from partial DIEs. So, unfortunately, we have
9283 to go through the full DIEs. At least any work we do building
9284 types here will be reused if full symbols are loaded later. */
9285 if (pdi->has_template_arguments)
9286 {
9287 pdi->fixup (cu);
9288
9289 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9290 {
9291 struct die_info *die;
9292 struct attribute attr;
9293 struct dwarf2_cu *ref_cu = cu;
9294
9295 /* DW_FORM_ref_addr is using section offset. */
9296 attr.name = (enum dwarf_attribute) 0;
9297 attr.form = DW_FORM_ref_addr;
9298 attr.u.unsnd = to_underlying (pdi->sect_off);
9299 die = follow_die_ref (NULL, &attr, &ref_cu);
9300
9301 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9302 }
9303 }
9304
9305 parent_scope = partial_die_parent_scope (pdi, cu);
9306 if (parent_scope == NULL)
9307 return NULL;
9308 else
9309 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
9310 }
9311
9312 static void
9313 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
9314 {
9315 struct dwarf2_per_objfile *dwarf2_per_objfile
9316 = cu->per_cu->dwarf2_per_objfile;
9317 struct objfile *objfile = dwarf2_per_objfile->objfile;
9318 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9319 CORE_ADDR addr = 0;
9320 const char *actual_name = NULL;
9321 CORE_ADDR baseaddr;
9322 char *built_actual_name;
9323
9324 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9325
9326 built_actual_name = partial_die_full_name (pdi, cu);
9327 if (built_actual_name != NULL)
9328 actual_name = built_actual_name;
9329
9330 if (actual_name == NULL)
9331 actual_name = pdi->name;
9332
9333 switch (pdi->tag)
9334 {
9335 case DW_TAG_inlined_subroutine:
9336 case DW_TAG_subprogram:
9337 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
9338 if (pdi->is_external || cu->language == language_ada)
9339 {
9340 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9341 of the global scope. But in Ada, we want to be able to access
9342 nested procedures globally. So all Ada subprograms are stored
9343 in the global scope. */
9344 add_psymbol_to_list (actual_name, strlen (actual_name),
9345 built_actual_name != NULL,
9346 VAR_DOMAIN, LOC_BLOCK,
9347 &objfile->global_psymbols,
9348 addr, cu->language, objfile);
9349 }
9350 else
9351 {
9352 add_psymbol_to_list (actual_name, strlen (actual_name),
9353 built_actual_name != NULL,
9354 VAR_DOMAIN, LOC_BLOCK,
9355 &objfile->static_psymbols,
9356 addr, cu->language, objfile);
9357 }
9358
9359 if (pdi->main_subprogram && actual_name != NULL)
9360 set_objfile_main_name (objfile, actual_name, cu->language);
9361 break;
9362 case DW_TAG_constant:
9363 {
9364 std::vector<partial_symbol *> *list;
9365
9366 if (pdi->is_external)
9367 list = &objfile->global_psymbols;
9368 else
9369 list = &objfile->static_psymbols;
9370 add_psymbol_to_list (actual_name, strlen (actual_name),
9371 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9372 list, 0, cu->language, objfile);
9373 }
9374 break;
9375 case DW_TAG_variable:
9376 if (pdi->d.locdesc)
9377 addr = decode_locdesc (pdi->d.locdesc, cu);
9378
9379 if (pdi->d.locdesc
9380 && addr == 0
9381 && !dwarf2_per_objfile->has_section_at_zero)
9382 {
9383 /* A global or static variable may also have been stripped
9384 out by the linker if unused, in which case its address
9385 will be nullified; do not add such variables into partial
9386 symbol table then. */
9387 }
9388 else if (pdi->is_external)
9389 {
9390 /* Global Variable.
9391 Don't enter into the minimal symbol tables as there is
9392 a minimal symbol table entry from the ELF symbols already.
9393 Enter into partial symbol table if it has a location
9394 descriptor or a type.
9395 If the location descriptor is missing, new_symbol will create
9396 a LOC_UNRESOLVED symbol, the address of the variable will then
9397 be determined from the minimal symbol table whenever the variable
9398 is referenced.
9399 The address for the partial symbol table entry is not
9400 used by GDB, but it comes in handy for debugging partial symbol
9401 table building. */
9402
9403 if (pdi->d.locdesc || pdi->has_type)
9404 add_psymbol_to_list (actual_name, strlen (actual_name),
9405 built_actual_name != NULL,
9406 VAR_DOMAIN, LOC_STATIC,
9407 &objfile->global_psymbols,
9408 addr + baseaddr,
9409 cu->language, objfile);
9410 }
9411 else
9412 {
9413 int has_loc = pdi->d.locdesc != NULL;
9414
9415 /* Static Variable. Skip symbols whose value we cannot know (those
9416 without location descriptors or constant values). */
9417 if (!has_loc && !pdi->has_const_value)
9418 {
9419 xfree (built_actual_name);
9420 return;
9421 }
9422
9423 add_psymbol_to_list (actual_name, strlen (actual_name),
9424 built_actual_name != NULL,
9425 VAR_DOMAIN, LOC_STATIC,
9426 &objfile->static_psymbols,
9427 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
9428 cu->language, objfile);
9429 }
9430 break;
9431 case DW_TAG_typedef:
9432 case DW_TAG_base_type:
9433 case DW_TAG_subrange_type:
9434 add_psymbol_to_list (actual_name, strlen (actual_name),
9435 built_actual_name != NULL,
9436 VAR_DOMAIN, LOC_TYPEDEF,
9437 &objfile->static_psymbols,
9438 0, cu->language, objfile);
9439 break;
9440 case DW_TAG_imported_declaration:
9441 case DW_TAG_namespace:
9442 add_psymbol_to_list (actual_name, strlen (actual_name),
9443 built_actual_name != NULL,
9444 VAR_DOMAIN, LOC_TYPEDEF,
9445 &objfile->global_psymbols,
9446 0, cu->language, objfile);
9447 break;
9448 case DW_TAG_module:
9449 add_psymbol_to_list (actual_name, strlen (actual_name),
9450 built_actual_name != NULL,
9451 MODULE_DOMAIN, LOC_TYPEDEF,
9452 &objfile->global_psymbols,
9453 0, cu->language, objfile);
9454 break;
9455 case DW_TAG_class_type:
9456 case DW_TAG_interface_type:
9457 case DW_TAG_structure_type:
9458 case DW_TAG_union_type:
9459 case DW_TAG_enumeration_type:
9460 /* Skip external references. The DWARF standard says in the section
9461 about "Structure, Union, and Class Type Entries": "An incomplete
9462 structure, union or class type is represented by a structure,
9463 union or class entry that does not have a byte size attribute
9464 and that has a DW_AT_declaration attribute." */
9465 if (!pdi->has_byte_size && pdi->is_declaration)
9466 {
9467 xfree (built_actual_name);
9468 return;
9469 }
9470
9471 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9472 static vs. global. */
9473 add_psymbol_to_list (actual_name, strlen (actual_name),
9474 built_actual_name != NULL,
9475 STRUCT_DOMAIN, LOC_TYPEDEF,
9476 cu->language == language_cplus
9477 ? &objfile->global_psymbols
9478 : &objfile->static_psymbols,
9479 0, cu->language, objfile);
9480
9481 break;
9482 case DW_TAG_enumerator:
9483 add_psymbol_to_list (actual_name, strlen (actual_name),
9484 built_actual_name != NULL,
9485 VAR_DOMAIN, LOC_CONST,
9486 cu->language == language_cplus
9487 ? &objfile->global_psymbols
9488 : &objfile->static_psymbols,
9489 0, cu->language, objfile);
9490 break;
9491 default:
9492 break;
9493 }
9494
9495 xfree (built_actual_name);
9496 }
9497
9498 /* Read a partial die corresponding to a namespace; also, add a symbol
9499 corresponding to that namespace to the symbol table. NAMESPACE is
9500 the name of the enclosing namespace. */
9501
9502 static void
9503 add_partial_namespace (struct partial_die_info *pdi,
9504 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9505 int set_addrmap, struct dwarf2_cu *cu)
9506 {
9507 /* Add a symbol for the namespace. */
9508
9509 add_partial_symbol (pdi, cu);
9510
9511 /* Now scan partial symbols in that namespace. */
9512
9513 if (pdi->has_children)
9514 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9515 }
9516
9517 /* Read a partial die corresponding to a Fortran module. */
9518
9519 static void
9520 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9521 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9522 {
9523 /* Add a symbol for the namespace. */
9524
9525 add_partial_symbol (pdi, cu);
9526
9527 /* Now scan partial symbols in that module. */
9528
9529 if (pdi->has_children)
9530 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9531 }
9532
9533 /* Read a partial die corresponding to a subprogram or an inlined
9534 subprogram and create a partial symbol for that subprogram.
9535 When the CU language allows it, this routine also defines a partial
9536 symbol for each nested subprogram that this subprogram contains.
9537 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9538 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9539
9540 PDI may also be a lexical block, in which case we simply search
9541 recursively for subprograms defined inside that lexical block.
9542 Again, this is only performed when the CU language allows this
9543 type of definitions. */
9544
9545 static void
9546 add_partial_subprogram (struct partial_die_info *pdi,
9547 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9548 int set_addrmap, struct dwarf2_cu *cu)
9549 {
9550 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9551 {
9552 if (pdi->has_pc_info)
9553 {
9554 if (pdi->lowpc < *lowpc)
9555 *lowpc = pdi->lowpc;
9556 if (pdi->highpc > *highpc)
9557 *highpc = pdi->highpc;
9558 if (set_addrmap)
9559 {
9560 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9561 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9562 CORE_ADDR baseaddr;
9563 CORE_ADDR highpc;
9564 CORE_ADDR lowpc;
9565
9566 baseaddr = ANOFFSET (objfile->section_offsets,
9567 SECT_OFF_TEXT (objfile));
9568 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9569 pdi->lowpc + baseaddr);
9570 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9571 pdi->highpc + baseaddr);
9572 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9573 cu->per_cu->v.psymtab);
9574 }
9575 }
9576
9577 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9578 {
9579 if (!pdi->is_declaration)
9580 /* Ignore subprogram DIEs that do not have a name, they are
9581 illegal. Do not emit a complaint at this point, we will
9582 do so when we convert this psymtab into a symtab. */
9583 if (pdi->name)
9584 add_partial_symbol (pdi, cu);
9585 }
9586 }
9587
9588 if (! pdi->has_children)
9589 return;
9590
9591 if (cu->language == language_ada)
9592 {
9593 pdi = pdi->die_child;
9594 while (pdi != NULL)
9595 {
9596 pdi->fixup (cu);
9597 if (pdi->tag == DW_TAG_subprogram
9598 || pdi->tag == DW_TAG_inlined_subroutine
9599 || pdi->tag == DW_TAG_lexical_block)
9600 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9601 pdi = pdi->die_sibling;
9602 }
9603 }
9604 }
9605
9606 /* Read a partial die corresponding to an enumeration type. */
9607
9608 static void
9609 add_partial_enumeration (struct partial_die_info *enum_pdi,
9610 struct dwarf2_cu *cu)
9611 {
9612 struct partial_die_info *pdi;
9613
9614 if (enum_pdi->name != NULL)
9615 add_partial_symbol (enum_pdi, cu);
9616
9617 pdi = enum_pdi->die_child;
9618 while (pdi)
9619 {
9620 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9621 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9622 else
9623 add_partial_symbol (pdi, cu);
9624 pdi = pdi->die_sibling;
9625 }
9626 }
9627
9628 /* Return the initial uleb128 in the die at INFO_PTR. */
9629
9630 static unsigned int
9631 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9632 {
9633 unsigned int bytes_read;
9634
9635 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9636 }
9637
9638 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9639 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9640
9641 Return the corresponding abbrev, or NULL if the number is zero (indicating
9642 an empty DIE). In either case *BYTES_READ will be set to the length of
9643 the initial number. */
9644
9645 static struct abbrev_info *
9646 peek_die_abbrev (const die_reader_specs &reader,
9647 const gdb_byte *info_ptr, unsigned int *bytes_read)
9648 {
9649 dwarf2_cu *cu = reader.cu;
9650 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9651 unsigned int abbrev_number
9652 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9653
9654 if (abbrev_number == 0)
9655 return NULL;
9656
9657 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9658 if (!abbrev)
9659 {
9660 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9661 " at offset %s [in module %s]"),
9662 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9663 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9664 }
9665
9666 return abbrev;
9667 }
9668
9669 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9670 Returns a pointer to the end of a series of DIEs, terminated by an empty
9671 DIE. Any children of the skipped DIEs will also be skipped. */
9672
9673 static const gdb_byte *
9674 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9675 {
9676 while (1)
9677 {
9678 unsigned int bytes_read;
9679 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9680
9681 if (abbrev == NULL)
9682 return info_ptr + bytes_read;
9683 else
9684 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9685 }
9686 }
9687
9688 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9689 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9690 abbrev corresponding to that skipped uleb128 should be passed in
9691 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9692 children. */
9693
9694 static const gdb_byte *
9695 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9696 struct abbrev_info *abbrev)
9697 {
9698 unsigned int bytes_read;
9699 struct attribute attr;
9700 bfd *abfd = reader->abfd;
9701 struct dwarf2_cu *cu = reader->cu;
9702 const gdb_byte *buffer = reader->buffer;
9703 const gdb_byte *buffer_end = reader->buffer_end;
9704 unsigned int form, i;
9705
9706 for (i = 0; i < abbrev->num_attrs; i++)
9707 {
9708 /* The only abbrev we care about is DW_AT_sibling. */
9709 if (abbrev->attrs[i].name == DW_AT_sibling)
9710 {
9711 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9712 if (attr.form == DW_FORM_ref_addr)
9713 complaint (&symfile_complaints,
9714 _("ignoring absolute DW_AT_sibling"));
9715 else
9716 {
9717 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9718 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9719
9720 if (sibling_ptr < info_ptr)
9721 complaint (&symfile_complaints,
9722 _("DW_AT_sibling points backwards"));
9723 else if (sibling_ptr > reader->buffer_end)
9724 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9725 else
9726 return sibling_ptr;
9727 }
9728 }
9729
9730 /* If it isn't DW_AT_sibling, skip this attribute. */
9731 form = abbrev->attrs[i].form;
9732 skip_attribute:
9733 switch (form)
9734 {
9735 case DW_FORM_ref_addr:
9736 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9737 and later it is offset sized. */
9738 if (cu->header.version == 2)
9739 info_ptr += cu->header.addr_size;
9740 else
9741 info_ptr += cu->header.offset_size;
9742 break;
9743 case DW_FORM_GNU_ref_alt:
9744 info_ptr += cu->header.offset_size;
9745 break;
9746 case DW_FORM_addr:
9747 info_ptr += cu->header.addr_size;
9748 break;
9749 case DW_FORM_data1:
9750 case DW_FORM_ref1:
9751 case DW_FORM_flag:
9752 info_ptr += 1;
9753 break;
9754 case DW_FORM_flag_present:
9755 case DW_FORM_implicit_const:
9756 break;
9757 case DW_FORM_data2:
9758 case DW_FORM_ref2:
9759 info_ptr += 2;
9760 break;
9761 case DW_FORM_data4:
9762 case DW_FORM_ref4:
9763 info_ptr += 4;
9764 break;
9765 case DW_FORM_data8:
9766 case DW_FORM_ref8:
9767 case DW_FORM_ref_sig8:
9768 info_ptr += 8;
9769 break;
9770 case DW_FORM_data16:
9771 info_ptr += 16;
9772 break;
9773 case DW_FORM_string:
9774 read_direct_string (abfd, info_ptr, &bytes_read);
9775 info_ptr += bytes_read;
9776 break;
9777 case DW_FORM_sec_offset:
9778 case DW_FORM_strp:
9779 case DW_FORM_GNU_strp_alt:
9780 info_ptr += cu->header.offset_size;
9781 break;
9782 case DW_FORM_exprloc:
9783 case DW_FORM_block:
9784 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9785 info_ptr += bytes_read;
9786 break;
9787 case DW_FORM_block1:
9788 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9789 break;
9790 case DW_FORM_block2:
9791 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9792 break;
9793 case DW_FORM_block4:
9794 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9795 break;
9796 case DW_FORM_sdata:
9797 case DW_FORM_udata:
9798 case DW_FORM_ref_udata:
9799 case DW_FORM_GNU_addr_index:
9800 case DW_FORM_GNU_str_index:
9801 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9802 break;
9803 case DW_FORM_indirect:
9804 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9805 info_ptr += bytes_read;
9806 /* We need to continue parsing from here, so just go back to
9807 the top. */
9808 goto skip_attribute;
9809
9810 default:
9811 error (_("Dwarf Error: Cannot handle %s "
9812 "in DWARF reader [in module %s]"),
9813 dwarf_form_name (form),
9814 bfd_get_filename (abfd));
9815 }
9816 }
9817
9818 if (abbrev->has_children)
9819 return skip_children (reader, info_ptr);
9820 else
9821 return info_ptr;
9822 }
9823
9824 /* Locate ORIG_PDI's sibling.
9825 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9826
9827 static const gdb_byte *
9828 locate_pdi_sibling (const struct die_reader_specs *reader,
9829 struct partial_die_info *orig_pdi,
9830 const gdb_byte *info_ptr)
9831 {
9832 /* Do we know the sibling already? */
9833
9834 if (orig_pdi->sibling)
9835 return orig_pdi->sibling;
9836
9837 /* Are there any children to deal with? */
9838
9839 if (!orig_pdi->has_children)
9840 return info_ptr;
9841
9842 /* Skip the children the long way. */
9843
9844 return skip_children (reader, info_ptr);
9845 }
9846
9847 /* Expand this partial symbol table into a full symbol table. SELF is
9848 not NULL. */
9849
9850 static void
9851 dwarf2_read_symtab (struct partial_symtab *self,
9852 struct objfile *objfile)
9853 {
9854 struct dwarf2_per_objfile *dwarf2_per_objfile
9855 = get_dwarf2_per_objfile (objfile);
9856
9857 if (self->readin)
9858 {
9859 warning (_("bug: psymtab for %s is already read in."),
9860 self->filename);
9861 }
9862 else
9863 {
9864 if (info_verbose)
9865 {
9866 printf_filtered (_("Reading in symbols for %s..."),
9867 self->filename);
9868 gdb_flush (gdb_stdout);
9869 }
9870
9871 /* If this psymtab is constructed from a debug-only objfile, the
9872 has_section_at_zero flag will not necessarily be correct. We
9873 can get the correct value for this flag by looking at the data
9874 associated with the (presumably stripped) associated objfile. */
9875 if (objfile->separate_debug_objfile_backlink)
9876 {
9877 struct dwarf2_per_objfile *dpo_backlink
9878 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9879
9880 dwarf2_per_objfile->has_section_at_zero
9881 = dpo_backlink->has_section_at_zero;
9882 }
9883
9884 dwarf2_per_objfile->reading_partial_symbols = 0;
9885
9886 psymtab_to_symtab_1 (self);
9887
9888 /* Finish up the debug error message. */
9889 if (info_verbose)
9890 printf_filtered (_("done.\n"));
9891 }
9892
9893 process_cu_includes (dwarf2_per_objfile);
9894 }
9895 \f
9896 /* Reading in full CUs. */
9897
9898 /* Add PER_CU to the queue. */
9899
9900 static void
9901 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9902 enum language pretend_language)
9903 {
9904 struct dwarf2_queue_item *item;
9905
9906 per_cu->queued = 1;
9907 item = XNEW (struct dwarf2_queue_item);
9908 item->per_cu = per_cu;
9909 item->pretend_language = pretend_language;
9910 item->next = NULL;
9911
9912 if (dwarf2_queue == NULL)
9913 dwarf2_queue = item;
9914 else
9915 dwarf2_queue_tail->next = item;
9916
9917 dwarf2_queue_tail = item;
9918 }
9919
9920 /* If PER_CU is not yet queued, add it to the queue.
9921 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9922 dependency.
9923 The result is non-zero if PER_CU was queued, otherwise the result is zero
9924 meaning either PER_CU is already queued or it is already loaded.
9925
9926 N.B. There is an invariant here that if a CU is queued then it is loaded.
9927 The caller is required to load PER_CU if we return non-zero. */
9928
9929 static int
9930 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9931 struct dwarf2_per_cu_data *per_cu,
9932 enum language pretend_language)
9933 {
9934 /* We may arrive here during partial symbol reading, if we need full
9935 DIEs to process an unusual case (e.g. template arguments). Do
9936 not queue PER_CU, just tell our caller to load its DIEs. */
9937 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9938 {
9939 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9940 return 1;
9941 return 0;
9942 }
9943
9944 /* Mark the dependence relation so that we don't flush PER_CU
9945 too early. */
9946 if (dependent_cu != NULL)
9947 dwarf2_add_dependence (dependent_cu, per_cu);
9948
9949 /* If it's already on the queue, we have nothing to do. */
9950 if (per_cu->queued)
9951 return 0;
9952
9953 /* If the compilation unit is already loaded, just mark it as
9954 used. */
9955 if (per_cu->cu != NULL)
9956 {
9957 per_cu->cu->last_used = 0;
9958 return 0;
9959 }
9960
9961 /* Add it to the queue. */
9962 queue_comp_unit (per_cu, pretend_language);
9963
9964 return 1;
9965 }
9966
9967 /* Process the queue. */
9968
9969 static void
9970 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9971 {
9972 struct dwarf2_queue_item *item, *next_item;
9973
9974 if (dwarf_read_debug)
9975 {
9976 fprintf_unfiltered (gdb_stdlog,
9977 "Expanding one or more symtabs of objfile %s ...\n",
9978 objfile_name (dwarf2_per_objfile->objfile));
9979 }
9980
9981 /* The queue starts out with one item, but following a DIE reference
9982 may load a new CU, adding it to the end of the queue. */
9983 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9984 {
9985 if ((dwarf2_per_objfile->using_index
9986 ? !item->per_cu->v.quick->compunit_symtab
9987 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9988 /* Skip dummy CUs. */
9989 && item->per_cu->cu != NULL)
9990 {
9991 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9992 unsigned int debug_print_threshold;
9993 char buf[100];
9994
9995 if (per_cu->is_debug_types)
9996 {
9997 struct signatured_type *sig_type =
9998 (struct signatured_type *) per_cu;
9999
10000 sprintf (buf, "TU %s at offset %s",
10001 hex_string (sig_type->signature),
10002 sect_offset_str (per_cu->sect_off));
10003 /* There can be 100s of TUs.
10004 Only print them in verbose mode. */
10005 debug_print_threshold = 2;
10006 }
10007 else
10008 {
10009 sprintf (buf, "CU at offset %s",
10010 sect_offset_str (per_cu->sect_off));
10011 debug_print_threshold = 1;
10012 }
10013
10014 if (dwarf_read_debug >= debug_print_threshold)
10015 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
10016
10017 if (per_cu->is_debug_types)
10018 process_full_type_unit (per_cu, item->pretend_language);
10019 else
10020 process_full_comp_unit (per_cu, item->pretend_language);
10021
10022 if (dwarf_read_debug >= debug_print_threshold)
10023 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
10024 }
10025
10026 item->per_cu->queued = 0;
10027 next_item = item->next;
10028 xfree (item);
10029 }
10030
10031 dwarf2_queue_tail = NULL;
10032
10033 if (dwarf_read_debug)
10034 {
10035 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
10036 objfile_name (dwarf2_per_objfile->objfile));
10037 }
10038 }
10039
10040 /* Read in full symbols for PST, and anything it depends on. */
10041
10042 static void
10043 psymtab_to_symtab_1 (struct partial_symtab *pst)
10044 {
10045 struct dwarf2_per_cu_data *per_cu;
10046 int i;
10047
10048 if (pst->readin)
10049 return;
10050
10051 for (i = 0; i < pst->number_of_dependencies; i++)
10052 if (!pst->dependencies[i]->readin
10053 && pst->dependencies[i]->user == NULL)
10054 {
10055 /* Inform about additional files that need to be read in. */
10056 if (info_verbose)
10057 {
10058 /* FIXME: i18n: Need to make this a single string. */
10059 fputs_filtered (" ", gdb_stdout);
10060 wrap_here ("");
10061 fputs_filtered ("and ", gdb_stdout);
10062 wrap_here ("");
10063 printf_filtered ("%s...", pst->dependencies[i]->filename);
10064 wrap_here (""); /* Flush output. */
10065 gdb_flush (gdb_stdout);
10066 }
10067 psymtab_to_symtab_1 (pst->dependencies[i]);
10068 }
10069
10070 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10071
10072 if (per_cu == NULL)
10073 {
10074 /* It's an include file, no symbols to read for it.
10075 Everything is in the parent symtab. */
10076 pst->readin = 1;
10077 return;
10078 }
10079
10080 dw2_do_instantiate_symtab (per_cu);
10081 }
10082
10083 /* Trivial hash function for die_info: the hash value of a DIE
10084 is its offset in .debug_info for this objfile. */
10085
10086 static hashval_t
10087 die_hash (const void *item)
10088 {
10089 const struct die_info *die = (const struct die_info *) item;
10090
10091 return to_underlying (die->sect_off);
10092 }
10093
10094 /* Trivial comparison function for die_info structures: two DIEs
10095 are equal if they have the same offset. */
10096
10097 static int
10098 die_eq (const void *item_lhs, const void *item_rhs)
10099 {
10100 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
10101 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
10102
10103 return die_lhs->sect_off == die_rhs->sect_off;
10104 }
10105
10106 /* die_reader_func for load_full_comp_unit.
10107 This is identical to read_signatured_type_reader,
10108 but is kept separate for now. */
10109
10110 static void
10111 load_full_comp_unit_reader (const struct die_reader_specs *reader,
10112 const gdb_byte *info_ptr,
10113 struct die_info *comp_unit_die,
10114 int has_children,
10115 void *data)
10116 {
10117 struct dwarf2_cu *cu = reader->cu;
10118 enum language *language_ptr = (enum language *) data;
10119
10120 gdb_assert (cu->die_hash == NULL);
10121 cu->die_hash =
10122 htab_create_alloc_ex (cu->header.length / 12,
10123 die_hash,
10124 die_eq,
10125 NULL,
10126 &cu->comp_unit_obstack,
10127 hashtab_obstack_allocate,
10128 dummy_obstack_deallocate);
10129
10130 if (has_children)
10131 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10132 &info_ptr, comp_unit_die);
10133 cu->dies = comp_unit_die;
10134 /* comp_unit_die is not stored in die_hash, no need. */
10135
10136 /* We try not to read any attributes in this function, because not
10137 all CUs needed for references have been loaded yet, and symbol
10138 table processing isn't initialized. But we have to set the CU language,
10139 or we won't be able to build types correctly.
10140 Similarly, if we do not read the producer, we can not apply
10141 producer-specific interpretation. */
10142 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
10143 }
10144
10145 /* Load the DIEs associated with PER_CU into memory. */
10146
10147 static void
10148 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10149 enum language pretend_language)
10150 {
10151 gdb_assert (! this_cu->is_debug_types);
10152
10153 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10154 load_full_comp_unit_reader, &pretend_language);
10155 }
10156
10157 /* Add a DIE to the delayed physname list. */
10158
10159 static void
10160 add_to_method_list (struct type *type, int fnfield_index, int index,
10161 const char *name, struct die_info *die,
10162 struct dwarf2_cu *cu)
10163 {
10164 struct delayed_method_info mi;
10165 mi.type = type;
10166 mi.fnfield_index = fnfield_index;
10167 mi.index = index;
10168 mi.name = name;
10169 mi.die = die;
10170 cu->method_list.push_back (mi);
10171 }
10172
10173 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10174 "const" / "volatile". If so, decrements LEN by the length of the
10175 modifier and return true. Otherwise return false. */
10176
10177 template<size_t N>
10178 static bool
10179 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10180 {
10181 size_t mod_len = sizeof (mod) - 1;
10182 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10183 {
10184 len -= mod_len;
10185 return true;
10186 }
10187 return false;
10188 }
10189
10190 /* Compute the physnames of any methods on the CU's method list.
10191
10192 The computation of method physnames is delayed in order to avoid the
10193 (bad) condition that one of the method's formal parameters is of an as yet
10194 incomplete type. */
10195
10196 static void
10197 compute_delayed_physnames (struct dwarf2_cu *cu)
10198 {
10199 /* Only C++ delays computing physnames. */
10200 if (cu->method_list.empty ())
10201 return;
10202 gdb_assert (cu->language == language_cplus);
10203
10204 for (struct delayed_method_info &mi : cu->method_list)
10205 {
10206 const char *physname;
10207 struct fn_fieldlist *fn_flp
10208 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
10209 physname = dwarf2_physname (mi.name, mi.die, cu);
10210 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
10211 = physname ? physname : "";
10212
10213 /* Since there's no tag to indicate whether a method is a
10214 const/volatile overload, extract that information out of the
10215 demangled name. */
10216 if (physname != NULL)
10217 {
10218 size_t len = strlen (physname);
10219
10220 while (1)
10221 {
10222 if (physname[len] == ')') /* shortcut */
10223 break;
10224 else if (check_modifier (physname, len, " const"))
10225 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
10226 else if (check_modifier (physname, len, " volatile"))
10227 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
10228 else
10229 break;
10230 }
10231 }
10232 }
10233
10234 /* The list is no longer needed. */
10235 cu->method_list.clear ();
10236 }
10237
10238 /* Go objects should be embedded in a DW_TAG_module DIE,
10239 and it's not clear if/how imported objects will appear.
10240 To keep Go support simple until that's worked out,
10241 go back through what we've read and create something usable.
10242 We could do this while processing each DIE, and feels kinda cleaner,
10243 but that way is more invasive.
10244 This is to, for example, allow the user to type "p var" or "b main"
10245 without having to specify the package name, and allow lookups
10246 of module.object to work in contexts that use the expression
10247 parser. */
10248
10249 static void
10250 fixup_go_packaging (struct dwarf2_cu *cu)
10251 {
10252 char *package_name = NULL;
10253 struct pending *list;
10254 int i;
10255
10256 for (list = global_symbols; list != NULL; list = list->next)
10257 {
10258 for (i = 0; i < list->nsyms; ++i)
10259 {
10260 struct symbol *sym = list->symbol[i];
10261
10262 if (SYMBOL_LANGUAGE (sym) == language_go
10263 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10264 {
10265 char *this_package_name = go_symbol_package_name (sym);
10266
10267 if (this_package_name == NULL)
10268 continue;
10269 if (package_name == NULL)
10270 package_name = this_package_name;
10271 else
10272 {
10273 struct objfile *objfile
10274 = cu->per_cu->dwarf2_per_objfile->objfile;
10275 if (strcmp (package_name, this_package_name) != 0)
10276 complaint (&symfile_complaints,
10277 _("Symtab %s has objects from two different Go packages: %s and %s"),
10278 (symbol_symtab (sym) != NULL
10279 ? symtab_to_filename_for_display
10280 (symbol_symtab (sym))
10281 : objfile_name (objfile)),
10282 this_package_name, package_name);
10283 xfree (this_package_name);
10284 }
10285 }
10286 }
10287 }
10288
10289 if (package_name != NULL)
10290 {
10291 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10292 const char *saved_package_name
10293 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10294 package_name,
10295 strlen (package_name));
10296 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10297 saved_package_name);
10298 struct symbol *sym;
10299
10300 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10301
10302 sym = allocate_symbol (objfile);
10303 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
10304 SYMBOL_SET_NAMES (sym, saved_package_name,
10305 strlen (saved_package_name), 0, objfile);
10306 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10307 e.g., "main" finds the "main" module and not C's main(). */
10308 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10309 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
10310 SYMBOL_TYPE (sym) = type;
10311
10312 add_symbol_to_list (sym, &global_symbols);
10313
10314 xfree (package_name);
10315 }
10316 }
10317
10318 /* Allocate a fully-qualified name consisting of the two parts on the
10319 obstack. */
10320
10321 static const char *
10322 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
10323 {
10324 return obconcat (obstack, p1, "::", p2, (char *) NULL);
10325 }
10326
10327 /* A helper that allocates a struct discriminant_info to attach to a
10328 union type. */
10329
10330 static struct discriminant_info *
10331 alloc_discriminant_info (struct type *type, int discriminant_index,
10332 int default_index)
10333 {
10334 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10335 gdb_assert (discriminant_index == -1
10336 || (discriminant_index >= 0
10337 && discriminant_index < TYPE_NFIELDS (type)));
10338 gdb_assert (default_index == -1
10339 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
10340
10341 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
10342
10343 struct discriminant_info *disc
10344 = ((struct discriminant_info *)
10345 TYPE_ZALLOC (type,
10346 offsetof (struct discriminant_info, discriminants)
10347 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
10348 disc->default_index = default_index;
10349 disc->discriminant_index = discriminant_index;
10350
10351 struct dynamic_prop prop;
10352 prop.kind = PROP_UNDEFINED;
10353 prop.data.baton = disc;
10354
10355 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
10356
10357 return disc;
10358 }
10359
10360 /* Some versions of rustc emitted enums in an unusual way.
10361
10362 Ordinary enums were emitted as unions. The first element of each
10363 structure in the union was named "RUST$ENUM$DISR". This element
10364 held the discriminant.
10365
10366 These versions of Rust also implemented the "non-zero"
10367 optimization. When the enum had two values, and one is empty and
10368 the other holds a pointer that cannot be zero, the pointer is used
10369 as the discriminant, with a zero value meaning the empty variant.
10370 Here, the union's first member is of the form
10371 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10372 where the fieldnos are the indices of the fields that should be
10373 traversed in order to find the field (which may be several fields deep)
10374 and the variantname is the name of the variant of the case when the
10375 field is zero.
10376
10377 This function recognizes whether TYPE is of one of these forms,
10378 and, if so, smashes it to be a variant type. */
10379
10380 static void
10381 quirk_rust_enum (struct type *type, struct objfile *objfile)
10382 {
10383 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10384
10385 /* We don't need to deal with empty enums. */
10386 if (TYPE_NFIELDS (type) == 0)
10387 return;
10388
10389 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10390 if (TYPE_NFIELDS (type) == 1
10391 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10392 {
10393 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10394
10395 /* Decode the field name to find the offset of the
10396 discriminant. */
10397 ULONGEST bit_offset = 0;
10398 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10399 while (name[0] >= '0' && name[0] <= '9')
10400 {
10401 char *tail;
10402 unsigned long index = strtoul (name, &tail, 10);
10403 name = tail;
10404 if (*name != '$'
10405 || index >= TYPE_NFIELDS (field_type)
10406 || (TYPE_FIELD_LOC_KIND (field_type, index)
10407 != FIELD_LOC_KIND_BITPOS))
10408 {
10409 complaint (&symfile_complaints,
10410 _("Could not parse Rust enum encoding string \"%s\""
10411 "[in module %s]"),
10412 TYPE_FIELD_NAME (type, 0),
10413 objfile_name (objfile));
10414 return;
10415 }
10416 ++name;
10417
10418 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10419 field_type = TYPE_FIELD_TYPE (field_type, index);
10420 }
10421
10422 /* Make a union to hold the variants. */
10423 struct type *union_type = alloc_type (objfile);
10424 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10425 TYPE_NFIELDS (union_type) = 3;
10426 TYPE_FIELDS (union_type)
10427 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10428 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10429
10430 /* Put the discriminant must at index 0. */
10431 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10432 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10433 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10434 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10435
10436 /* The order of fields doesn't really matter, so put the real
10437 field at index 1 and the data-less field at index 2. */
10438 struct discriminant_info *disc
10439 = alloc_discriminant_info (union_type, 0, 1);
10440 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10441 TYPE_FIELD_NAME (union_type, 1)
10442 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10443 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10444 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10445 TYPE_FIELD_NAME (union_type, 1));
10446
10447 const char *dataless_name
10448 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10449 name);
10450 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10451 dataless_name);
10452 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10453 /* NAME points into the original discriminant name, which
10454 already has the correct lifetime. */
10455 TYPE_FIELD_NAME (union_type, 2) = name;
10456 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10457 disc->discriminants[2] = 0;
10458
10459 /* Smash this type to be a structure type. We have to do this
10460 because the type has already been recorded. */
10461 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10462 TYPE_NFIELDS (type) = 1;
10463 TYPE_FIELDS (type)
10464 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10465
10466 /* Install the variant part. */
10467 TYPE_FIELD_TYPE (type, 0) = union_type;
10468 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10469 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10470 }
10471 else if (TYPE_NFIELDS (type) == 1)
10472 {
10473 /* We assume that a union with a single field is a univariant
10474 enum. */
10475 /* Smash this type to be a structure type. We have to do this
10476 because the type has already been recorded. */
10477 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10478
10479 /* Make a union to hold the variants. */
10480 struct type *union_type = alloc_type (objfile);
10481 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10482 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10483 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10484 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10485
10486 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10487 const char *variant_name
10488 = rust_last_path_segment (TYPE_NAME (field_type));
10489 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10490 TYPE_NAME (field_type)
10491 = rust_fully_qualify (&objfile->objfile_obstack,
10492 TYPE_NAME (type), variant_name);
10493
10494 /* Install the union in the outer struct type. */
10495 TYPE_NFIELDS (type) = 1;
10496 TYPE_FIELDS (type)
10497 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10498 TYPE_FIELD_TYPE (type, 0) = union_type;
10499 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10500 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10501
10502 alloc_discriminant_info (union_type, -1, 0);
10503 }
10504 else
10505 {
10506 struct type *disr_type = nullptr;
10507 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10508 {
10509 disr_type = TYPE_FIELD_TYPE (type, i);
10510
10511 if (TYPE_NFIELDS (disr_type) == 0)
10512 {
10513 /* Could be data-less variant, so keep going. */
10514 }
10515 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10516 "RUST$ENUM$DISR") != 0)
10517 {
10518 /* Not a Rust enum. */
10519 return;
10520 }
10521 else
10522 {
10523 /* Found one. */
10524 break;
10525 }
10526 }
10527
10528 /* If we got here without a discriminant, then it's probably
10529 just a union. */
10530 if (disr_type == nullptr)
10531 return;
10532
10533 /* Smash this type to be a structure type. We have to do this
10534 because the type has already been recorded. */
10535 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10536
10537 /* Make a union to hold the variants. */
10538 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10539 struct type *union_type = alloc_type (objfile);
10540 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10541 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10542 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10543 TYPE_FIELDS (union_type)
10544 = (struct field *) TYPE_ZALLOC (union_type,
10545 (TYPE_NFIELDS (union_type)
10546 * sizeof (struct field)));
10547
10548 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10549 TYPE_NFIELDS (type) * sizeof (struct field));
10550
10551 /* Install the discriminant at index 0 in the union. */
10552 TYPE_FIELD (union_type, 0) = *disr_field;
10553 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10554 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10555
10556 /* Install the union in the outer struct type. */
10557 TYPE_FIELD_TYPE (type, 0) = union_type;
10558 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10559 TYPE_NFIELDS (type) = 1;
10560
10561 /* Set the size and offset of the union type. */
10562 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10563
10564 /* We need a way to find the correct discriminant given a
10565 variant name. For convenience we build a map here. */
10566 struct type *enum_type = FIELD_TYPE (*disr_field);
10567 std::unordered_map<std::string, ULONGEST> discriminant_map;
10568 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10569 {
10570 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10571 {
10572 const char *name
10573 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10574 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10575 }
10576 }
10577
10578 int n_fields = TYPE_NFIELDS (union_type);
10579 struct discriminant_info *disc
10580 = alloc_discriminant_info (union_type, 0, -1);
10581 /* Skip the discriminant here. */
10582 for (int i = 1; i < n_fields; ++i)
10583 {
10584 /* Find the final word in the name of this variant's type.
10585 That name can be used to look up the correct
10586 discriminant. */
10587 const char *variant_name
10588 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10589 i)));
10590
10591 auto iter = discriminant_map.find (variant_name);
10592 if (iter != discriminant_map.end ())
10593 disc->discriminants[i] = iter->second;
10594
10595 /* Remove the discriminant field. */
10596 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10597 --TYPE_NFIELDS (sub_type);
10598 ++TYPE_FIELDS (sub_type);
10599 TYPE_FIELD_NAME (union_type, i) = variant_name;
10600 TYPE_NAME (sub_type)
10601 = rust_fully_qualify (&objfile->objfile_obstack,
10602 TYPE_NAME (type), variant_name);
10603 }
10604 }
10605 }
10606
10607 /* Rewrite some Rust unions to be structures with variants parts. */
10608
10609 static void
10610 rust_union_quirks (struct dwarf2_cu *cu)
10611 {
10612 gdb_assert (cu->language == language_rust);
10613 for (struct type *type : cu->rust_unions)
10614 quirk_rust_enum (type, cu->per_cu->dwarf2_per_objfile->objfile);
10615 }
10616
10617 /* Return the symtab for PER_CU. This works properly regardless of
10618 whether we're using the index or psymtabs. */
10619
10620 static struct compunit_symtab *
10621 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10622 {
10623 return (per_cu->dwarf2_per_objfile->using_index
10624 ? per_cu->v.quick->compunit_symtab
10625 : per_cu->v.psymtab->compunit_symtab);
10626 }
10627
10628 /* A helper function for computing the list of all symbol tables
10629 included by PER_CU. */
10630
10631 static void
10632 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10633 htab_t all_children, htab_t all_type_symtabs,
10634 struct dwarf2_per_cu_data *per_cu,
10635 struct compunit_symtab *immediate_parent)
10636 {
10637 void **slot;
10638 int ix;
10639 struct compunit_symtab *cust;
10640 struct dwarf2_per_cu_data *iter;
10641
10642 slot = htab_find_slot (all_children, per_cu, INSERT);
10643 if (*slot != NULL)
10644 {
10645 /* This inclusion and its children have been processed. */
10646 return;
10647 }
10648
10649 *slot = per_cu;
10650 /* Only add a CU if it has a symbol table. */
10651 cust = get_compunit_symtab (per_cu);
10652 if (cust != NULL)
10653 {
10654 /* If this is a type unit only add its symbol table if we haven't
10655 seen it yet (type unit per_cu's can share symtabs). */
10656 if (per_cu->is_debug_types)
10657 {
10658 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10659 if (*slot == NULL)
10660 {
10661 *slot = cust;
10662 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10663 if (cust->user == NULL)
10664 cust->user = immediate_parent;
10665 }
10666 }
10667 else
10668 {
10669 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10670 if (cust->user == NULL)
10671 cust->user = immediate_parent;
10672 }
10673 }
10674
10675 for (ix = 0;
10676 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10677 ++ix)
10678 {
10679 recursively_compute_inclusions (result, all_children,
10680 all_type_symtabs, iter, cust);
10681 }
10682 }
10683
10684 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10685 PER_CU. */
10686
10687 static void
10688 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10689 {
10690 gdb_assert (! per_cu->is_debug_types);
10691
10692 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10693 {
10694 int ix, len;
10695 struct dwarf2_per_cu_data *per_cu_iter;
10696 struct compunit_symtab *compunit_symtab_iter;
10697 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10698 htab_t all_children, all_type_symtabs;
10699 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10700
10701 /* If we don't have a symtab, we can just skip this case. */
10702 if (cust == NULL)
10703 return;
10704
10705 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10706 NULL, xcalloc, xfree);
10707 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10708 NULL, xcalloc, xfree);
10709
10710 for (ix = 0;
10711 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10712 ix, per_cu_iter);
10713 ++ix)
10714 {
10715 recursively_compute_inclusions (&result_symtabs, all_children,
10716 all_type_symtabs, per_cu_iter,
10717 cust);
10718 }
10719
10720 /* Now we have a transitive closure of all the included symtabs. */
10721 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10722 cust->includes
10723 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10724 struct compunit_symtab *, len + 1);
10725 for (ix = 0;
10726 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10727 compunit_symtab_iter);
10728 ++ix)
10729 cust->includes[ix] = compunit_symtab_iter;
10730 cust->includes[len] = NULL;
10731
10732 VEC_free (compunit_symtab_ptr, result_symtabs);
10733 htab_delete (all_children);
10734 htab_delete (all_type_symtabs);
10735 }
10736 }
10737
10738 /* Compute the 'includes' field for the symtabs of all the CUs we just
10739 read. */
10740
10741 static void
10742 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10743 {
10744 int ix;
10745 struct dwarf2_per_cu_data *iter;
10746
10747 for (ix = 0;
10748 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10749 ix, iter);
10750 ++ix)
10751 {
10752 if (! iter->is_debug_types)
10753 compute_compunit_symtab_includes (iter);
10754 }
10755
10756 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10757 }
10758
10759 /* Generate full symbol information for PER_CU, whose DIEs have
10760 already been loaded into memory. */
10761
10762 static void
10763 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10764 enum language pretend_language)
10765 {
10766 struct dwarf2_cu *cu = per_cu->cu;
10767 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10768 struct objfile *objfile = dwarf2_per_objfile->objfile;
10769 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10770 CORE_ADDR lowpc, highpc;
10771 struct compunit_symtab *cust;
10772 CORE_ADDR baseaddr;
10773 struct block *static_block;
10774 CORE_ADDR addr;
10775
10776 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10777
10778 buildsym_init ();
10779 scoped_free_pendings free_pending;
10780
10781 /* Clear the list here in case something was left over. */
10782 cu->method_list.clear ();
10783
10784 cu->list_in_scope = &file_symbols;
10785
10786 cu->language = pretend_language;
10787 cu->language_defn = language_def (cu->language);
10788
10789 /* Do line number decoding in read_file_scope () */
10790 process_die (cu->dies, cu);
10791
10792 /* For now fudge the Go package. */
10793 if (cu->language == language_go)
10794 fixup_go_packaging (cu);
10795
10796 /* Now that we have processed all the DIEs in the CU, all the types
10797 should be complete, and it should now be safe to compute all of the
10798 physnames. */
10799 compute_delayed_physnames (cu);
10800
10801 if (cu->language == language_rust)
10802 rust_union_quirks (cu);
10803
10804 /* Some compilers don't define a DW_AT_high_pc attribute for the
10805 compilation unit. If the DW_AT_high_pc is missing, synthesize
10806 it, by scanning the DIE's below the compilation unit. */
10807 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10808
10809 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10810 static_block = end_symtab_get_static_block (addr, 0, 1);
10811
10812 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10813 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10814 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10815 addrmap to help ensure it has an accurate map of pc values belonging to
10816 this comp unit. */
10817 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10818
10819 cust = end_symtab_from_static_block (static_block,
10820 SECT_OFF_TEXT (objfile), 0);
10821
10822 if (cust != NULL)
10823 {
10824 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10825
10826 /* Set symtab language to language from DW_AT_language. If the
10827 compilation is from a C file generated by language preprocessors, do
10828 not set the language if it was already deduced by start_subfile. */
10829 if (!(cu->language == language_c
10830 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10831 COMPUNIT_FILETABS (cust)->language = cu->language;
10832
10833 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10834 produce DW_AT_location with location lists but it can be possibly
10835 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10836 there were bugs in prologue debug info, fixed later in GCC-4.5
10837 by "unwind info for epilogues" patch (which is not directly related).
10838
10839 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10840 needed, it would be wrong due to missing DW_AT_producer there.
10841
10842 Still one can confuse GDB by using non-standard GCC compilation
10843 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10844 */
10845 if (cu->has_loclist && gcc_4_minor >= 5)
10846 cust->locations_valid = 1;
10847
10848 if (gcc_4_minor >= 5)
10849 cust->epilogue_unwind_valid = 1;
10850
10851 cust->call_site_htab = cu->call_site_htab;
10852 }
10853
10854 if (dwarf2_per_objfile->using_index)
10855 per_cu->v.quick->compunit_symtab = cust;
10856 else
10857 {
10858 struct partial_symtab *pst = per_cu->v.psymtab;
10859 pst->compunit_symtab = cust;
10860 pst->readin = 1;
10861 }
10862
10863 /* Push it for inclusion processing later. */
10864 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10865 }
10866
10867 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10868 already been loaded into memory. */
10869
10870 static void
10871 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10872 enum language pretend_language)
10873 {
10874 struct dwarf2_cu *cu = per_cu->cu;
10875 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10876 struct objfile *objfile = dwarf2_per_objfile->objfile;
10877 struct compunit_symtab *cust;
10878 struct signatured_type *sig_type;
10879
10880 gdb_assert (per_cu->is_debug_types);
10881 sig_type = (struct signatured_type *) per_cu;
10882
10883 buildsym_init ();
10884 scoped_free_pendings free_pending;
10885
10886 /* Clear the list here in case something was left over. */
10887 cu->method_list.clear ();
10888
10889 cu->list_in_scope = &file_symbols;
10890
10891 cu->language = pretend_language;
10892 cu->language_defn = language_def (cu->language);
10893
10894 /* The symbol tables are set up in read_type_unit_scope. */
10895 process_die (cu->dies, cu);
10896
10897 /* For now fudge the Go package. */
10898 if (cu->language == language_go)
10899 fixup_go_packaging (cu);
10900
10901 /* Now that we have processed all the DIEs in the CU, all the types
10902 should be complete, and it should now be safe to compute all of the
10903 physnames. */
10904 compute_delayed_physnames (cu);
10905
10906 if (cu->language == language_rust)
10907 rust_union_quirks (cu);
10908
10909 /* TUs share symbol tables.
10910 If this is the first TU to use this symtab, complete the construction
10911 of it with end_expandable_symtab. Otherwise, complete the addition of
10912 this TU's symbols to the existing symtab. */
10913 if (sig_type->type_unit_group->compunit_symtab == NULL)
10914 {
10915 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10916 sig_type->type_unit_group->compunit_symtab = cust;
10917
10918 if (cust != NULL)
10919 {
10920 /* Set symtab language to language from DW_AT_language. If the
10921 compilation is from a C file generated by language preprocessors,
10922 do not set the language if it was already deduced by
10923 start_subfile. */
10924 if (!(cu->language == language_c
10925 && COMPUNIT_FILETABS (cust)->language != language_c))
10926 COMPUNIT_FILETABS (cust)->language = cu->language;
10927 }
10928 }
10929 else
10930 {
10931 augment_type_symtab ();
10932 cust = sig_type->type_unit_group->compunit_symtab;
10933 }
10934
10935 if (dwarf2_per_objfile->using_index)
10936 per_cu->v.quick->compunit_symtab = cust;
10937 else
10938 {
10939 struct partial_symtab *pst = per_cu->v.psymtab;
10940 pst->compunit_symtab = cust;
10941 pst->readin = 1;
10942 }
10943 }
10944
10945 /* Process an imported unit DIE. */
10946
10947 static void
10948 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10949 {
10950 struct attribute *attr;
10951
10952 /* For now we don't handle imported units in type units. */
10953 if (cu->per_cu->is_debug_types)
10954 {
10955 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10956 " supported in type units [in module %s]"),
10957 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10958 }
10959
10960 attr = dwarf2_attr (die, DW_AT_import, cu);
10961 if (attr != NULL)
10962 {
10963 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10964 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10965 dwarf2_per_cu_data *per_cu
10966 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10967 cu->per_cu->dwarf2_per_objfile);
10968
10969 /* If necessary, add it to the queue and load its DIEs. */
10970 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10971 load_full_comp_unit (per_cu, cu->language);
10972
10973 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10974 per_cu);
10975 }
10976 }
10977
10978 /* RAII object that represents a process_die scope: i.e.,
10979 starts/finishes processing a DIE. */
10980 class process_die_scope
10981 {
10982 public:
10983 process_die_scope (die_info *die, dwarf2_cu *cu)
10984 : m_die (die), m_cu (cu)
10985 {
10986 /* We should only be processing DIEs not already in process. */
10987 gdb_assert (!m_die->in_process);
10988 m_die->in_process = true;
10989 }
10990
10991 ~process_die_scope ()
10992 {
10993 m_die->in_process = false;
10994
10995 /* If we're done processing the DIE for the CU that owns the line
10996 header, we don't need the line header anymore. */
10997 if (m_cu->line_header_die_owner == m_die)
10998 {
10999 delete m_cu->line_header;
11000 m_cu->line_header = NULL;
11001 m_cu->line_header_die_owner = NULL;
11002 }
11003 }
11004
11005 private:
11006 die_info *m_die;
11007 dwarf2_cu *m_cu;
11008 };
11009
11010 /* Process a die and its children. */
11011
11012 static void
11013 process_die (struct die_info *die, struct dwarf2_cu *cu)
11014 {
11015 process_die_scope scope (die, cu);
11016
11017 switch (die->tag)
11018 {
11019 case DW_TAG_padding:
11020 break;
11021 case DW_TAG_compile_unit:
11022 case DW_TAG_partial_unit:
11023 read_file_scope (die, cu);
11024 break;
11025 case DW_TAG_type_unit:
11026 read_type_unit_scope (die, cu);
11027 break;
11028 case DW_TAG_subprogram:
11029 case DW_TAG_inlined_subroutine:
11030 read_func_scope (die, cu);
11031 break;
11032 case DW_TAG_lexical_block:
11033 case DW_TAG_try_block:
11034 case DW_TAG_catch_block:
11035 read_lexical_block_scope (die, cu);
11036 break;
11037 case DW_TAG_call_site:
11038 case DW_TAG_GNU_call_site:
11039 read_call_site_scope (die, cu);
11040 break;
11041 case DW_TAG_class_type:
11042 case DW_TAG_interface_type:
11043 case DW_TAG_structure_type:
11044 case DW_TAG_union_type:
11045 process_structure_scope (die, cu);
11046 break;
11047 case DW_TAG_enumeration_type:
11048 process_enumeration_scope (die, cu);
11049 break;
11050
11051 /* These dies have a type, but processing them does not create
11052 a symbol or recurse to process the children. Therefore we can
11053 read them on-demand through read_type_die. */
11054 case DW_TAG_subroutine_type:
11055 case DW_TAG_set_type:
11056 case DW_TAG_array_type:
11057 case DW_TAG_pointer_type:
11058 case DW_TAG_ptr_to_member_type:
11059 case DW_TAG_reference_type:
11060 case DW_TAG_rvalue_reference_type:
11061 case DW_TAG_string_type:
11062 break;
11063
11064 case DW_TAG_base_type:
11065 case DW_TAG_subrange_type:
11066 case DW_TAG_typedef:
11067 /* Add a typedef symbol for the type definition, if it has a
11068 DW_AT_name. */
11069 new_symbol (die, read_type_die (die, cu), cu);
11070 break;
11071 case DW_TAG_common_block:
11072 read_common_block (die, cu);
11073 break;
11074 case DW_TAG_common_inclusion:
11075 break;
11076 case DW_TAG_namespace:
11077 cu->processing_has_namespace_info = 1;
11078 read_namespace (die, cu);
11079 break;
11080 case DW_TAG_module:
11081 cu->processing_has_namespace_info = 1;
11082 read_module (die, cu);
11083 break;
11084 case DW_TAG_imported_declaration:
11085 cu->processing_has_namespace_info = 1;
11086 if (read_namespace_alias (die, cu))
11087 break;
11088 /* The declaration is not a global namespace alias: fall through. */
11089 case DW_TAG_imported_module:
11090 cu->processing_has_namespace_info = 1;
11091 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
11092 || cu->language != language_fortran))
11093 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
11094 dwarf_tag_name (die->tag));
11095 read_import_statement (die, cu);
11096 break;
11097
11098 case DW_TAG_imported_unit:
11099 process_imported_unit_die (die, cu);
11100 break;
11101
11102 case DW_TAG_variable:
11103 read_variable (die, cu);
11104 break;
11105
11106 default:
11107 new_symbol (die, NULL, cu);
11108 break;
11109 }
11110 }
11111 \f
11112 /* DWARF name computation. */
11113
11114 /* A helper function for dwarf2_compute_name which determines whether DIE
11115 needs to have the name of the scope prepended to the name listed in the
11116 die. */
11117
11118 static int
11119 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
11120 {
11121 struct attribute *attr;
11122
11123 switch (die->tag)
11124 {
11125 case DW_TAG_namespace:
11126 case DW_TAG_typedef:
11127 case DW_TAG_class_type:
11128 case DW_TAG_interface_type:
11129 case DW_TAG_structure_type:
11130 case DW_TAG_union_type:
11131 case DW_TAG_enumeration_type:
11132 case DW_TAG_enumerator:
11133 case DW_TAG_subprogram:
11134 case DW_TAG_inlined_subroutine:
11135 case DW_TAG_member:
11136 case DW_TAG_imported_declaration:
11137 return 1;
11138
11139 case DW_TAG_variable:
11140 case DW_TAG_constant:
11141 /* We only need to prefix "globally" visible variables. These include
11142 any variable marked with DW_AT_external or any variable that
11143 lives in a namespace. [Variables in anonymous namespaces
11144 require prefixing, but they are not DW_AT_external.] */
11145
11146 if (dwarf2_attr (die, DW_AT_specification, cu))
11147 {
11148 struct dwarf2_cu *spec_cu = cu;
11149
11150 return die_needs_namespace (die_specification (die, &spec_cu),
11151 spec_cu);
11152 }
11153
11154 attr = dwarf2_attr (die, DW_AT_external, cu);
11155 if (attr == NULL && die->parent->tag != DW_TAG_namespace
11156 && die->parent->tag != DW_TAG_module)
11157 return 0;
11158 /* A variable in a lexical block of some kind does not need a
11159 namespace, even though in C++ such variables may be external
11160 and have a mangled name. */
11161 if (die->parent->tag == DW_TAG_lexical_block
11162 || die->parent->tag == DW_TAG_try_block
11163 || die->parent->tag == DW_TAG_catch_block
11164 || die->parent->tag == DW_TAG_subprogram)
11165 return 0;
11166 return 1;
11167
11168 default:
11169 return 0;
11170 }
11171 }
11172
11173 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
11174 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
11175 defined for the given DIE. */
11176
11177 static struct attribute *
11178 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
11179 {
11180 struct attribute *attr;
11181
11182 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
11183 if (attr == NULL)
11184 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
11185
11186 return attr;
11187 }
11188
11189 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
11190 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
11191 defined for the given DIE. */
11192
11193 static const char *
11194 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
11195 {
11196 const char *linkage_name;
11197
11198 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
11199 if (linkage_name == NULL)
11200 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
11201
11202 return linkage_name;
11203 }
11204
11205 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
11206 compute the physname for the object, which include a method's:
11207 - formal parameters (C++),
11208 - receiver type (Go),
11209
11210 The term "physname" is a bit confusing.
11211 For C++, for example, it is the demangled name.
11212 For Go, for example, it's the mangled name.
11213
11214 For Ada, return the DIE's linkage name rather than the fully qualified
11215 name. PHYSNAME is ignored..
11216
11217 The result is allocated on the objfile_obstack and canonicalized. */
11218
11219 static const char *
11220 dwarf2_compute_name (const char *name,
11221 struct die_info *die, struct dwarf2_cu *cu,
11222 int physname)
11223 {
11224 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11225
11226 if (name == NULL)
11227 name = dwarf2_name (die, cu);
11228
11229 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
11230 but otherwise compute it by typename_concat inside GDB.
11231 FIXME: Actually this is not really true, or at least not always true.
11232 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
11233 Fortran names because there is no mangling standard. So new_symbol
11234 will set the demangled name to the result of dwarf2_full_name, and it is
11235 the demangled name that GDB uses if it exists. */
11236 if (cu->language == language_ada
11237 || (cu->language == language_fortran && physname))
11238 {
11239 /* For Ada unit, we prefer the linkage name over the name, as
11240 the former contains the exported name, which the user expects
11241 to be able to reference. Ideally, we want the user to be able
11242 to reference this entity using either natural or linkage name,
11243 but we haven't started looking at this enhancement yet. */
11244 const char *linkage_name = dw2_linkage_name (die, cu);
11245
11246 if (linkage_name != NULL)
11247 return linkage_name;
11248 }
11249
11250 /* These are the only languages we know how to qualify names in. */
11251 if (name != NULL
11252 && (cu->language == language_cplus
11253 || cu->language == language_fortran || cu->language == language_d
11254 || cu->language == language_rust))
11255 {
11256 if (die_needs_namespace (die, cu))
11257 {
11258 const char *prefix;
11259 const char *canonical_name = NULL;
11260
11261 string_file buf;
11262
11263 prefix = determine_prefix (die, cu);
11264 if (*prefix != '\0')
11265 {
11266 char *prefixed_name = typename_concat (NULL, prefix, name,
11267 physname, cu);
11268
11269 buf.puts (prefixed_name);
11270 xfree (prefixed_name);
11271 }
11272 else
11273 buf.puts (name);
11274
11275 /* Template parameters may be specified in the DIE's DW_AT_name, or
11276 as children with DW_TAG_template_type_param or
11277 DW_TAG_value_type_param. If the latter, add them to the name
11278 here. If the name already has template parameters, then
11279 skip this step; some versions of GCC emit both, and
11280 it is more efficient to use the pre-computed name.
11281
11282 Something to keep in mind about this process: it is very
11283 unlikely, or in some cases downright impossible, to produce
11284 something that will match the mangled name of a function.
11285 If the definition of the function has the same debug info,
11286 we should be able to match up with it anyway. But fallbacks
11287 using the minimal symbol, for instance to find a method
11288 implemented in a stripped copy of libstdc++, will not work.
11289 If we do not have debug info for the definition, we will have to
11290 match them up some other way.
11291
11292 When we do name matching there is a related problem with function
11293 templates; two instantiated function templates are allowed to
11294 differ only by their return types, which we do not add here. */
11295
11296 if (cu->language == language_cplus && strchr (name, '<') == NULL)
11297 {
11298 struct attribute *attr;
11299 struct die_info *child;
11300 int first = 1;
11301
11302 die->building_fullname = 1;
11303
11304 for (child = die->child; child != NULL; child = child->sibling)
11305 {
11306 struct type *type;
11307 LONGEST value;
11308 const gdb_byte *bytes;
11309 struct dwarf2_locexpr_baton *baton;
11310 struct value *v;
11311
11312 if (child->tag != DW_TAG_template_type_param
11313 && child->tag != DW_TAG_template_value_param)
11314 continue;
11315
11316 if (first)
11317 {
11318 buf.puts ("<");
11319 first = 0;
11320 }
11321 else
11322 buf.puts (", ");
11323
11324 attr = dwarf2_attr (child, DW_AT_type, cu);
11325 if (attr == NULL)
11326 {
11327 complaint (&symfile_complaints,
11328 _("template parameter missing DW_AT_type"));
11329 buf.puts ("UNKNOWN_TYPE");
11330 continue;
11331 }
11332 type = die_type (child, cu);
11333
11334 if (child->tag == DW_TAG_template_type_param)
11335 {
11336 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
11337 continue;
11338 }
11339
11340 attr = dwarf2_attr (child, DW_AT_const_value, cu);
11341 if (attr == NULL)
11342 {
11343 complaint (&symfile_complaints,
11344 _("template parameter missing "
11345 "DW_AT_const_value"));
11346 buf.puts ("UNKNOWN_VALUE");
11347 continue;
11348 }
11349
11350 dwarf2_const_value_attr (attr, type, name,
11351 &cu->comp_unit_obstack, cu,
11352 &value, &bytes, &baton);
11353
11354 if (TYPE_NOSIGN (type))
11355 /* GDB prints characters as NUMBER 'CHAR'. If that's
11356 changed, this can use value_print instead. */
11357 c_printchar (value, type, &buf);
11358 else
11359 {
11360 struct value_print_options opts;
11361
11362 if (baton != NULL)
11363 v = dwarf2_evaluate_loc_desc (type, NULL,
11364 baton->data,
11365 baton->size,
11366 baton->per_cu);
11367 else if (bytes != NULL)
11368 {
11369 v = allocate_value (type);
11370 memcpy (value_contents_writeable (v), bytes,
11371 TYPE_LENGTH (type));
11372 }
11373 else
11374 v = value_from_longest (type, value);
11375
11376 /* Specify decimal so that we do not depend on
11377 the radix. */
11378 get_formatted_print_options (&opts, 'd');
11379 opts.raw = 1;
11380 value_print (v, &buf, &opts);
11381 release_value (v);
11382 value_free (v);
11383 }
11384 }
11385
11386 die->building_fullname = 0;
11387
11388 if (!first)
11389 {
11390 /* Close the argument list, with a space if necessary
11391 (nested templates). */
11392 if (!buf.empty () && buf.string ().back () == '>')
11393 buf.puts (" >");
11394 else
11395 buf.puts (">");
11396 }
11397 }
11398
11399 /* For C++ methods, append formal parameter type
11400 information, if PHYSNAME. */
11401
11402 if (physname && die->tag == DW_TAG_subprogram
11403 && cu->language == language_cplus)
11404 {
11405 struct type *type = read_type_die (die, cu);
11406
11407 c_type_print_args (type, &buf, 1, cu->language,
11408 &type_print_raw_options);
11409
11410 if (cu->language == language_cplus)
11411 {
11412 /* Assume that an artificial first parameter is
11413 "this", but do not crash if it is not. RealView
11414 marks unnamed (and thus unused) parameters as
11415 artificial; there is no way to differentiate
11416 the two cases. */
11417 if (TYPE_NFIELDS (type) > 0
11418 && TYPE_FIELD_ARTIFICIAL (type, 0)
11419 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11420 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11421 0))))
11422 buf.puts (" const");
11423 }
11424 }
11425
11426 const std::string &intermediate_name = buf.string ();
11427
11428 if (cu->language == language_cplus)
11429 canonical_name
11430 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11431 &objfile->per_bfd->storage_obstack);
11432
11433 /* If we only computed INTERMEDIATE_NAME, or if
11434 INTERMEDIATE_NAME is already canonical, then we need to
11435 copy it to the appropriate obstack. */
11436 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11437 name = ((const char *)
11438 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11439 intermediate_name.c_str (),
11440 intermediate_name.length ()));
11441 else
11442 name = canonical_name;
11443 }
11444 }
11445
11446 return name;
11447 }
11448
11449 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11450 If scope qualifiers are appropriate they will be added. The result
11451 will be allocated on the storage_obstack, or NULL if the DIE does
11452 not have a name. NAME may either be from a previous call to
11453 dwarf2_name or NULL.
11454
11455 The output string will be canonicalized (if C++). */
11456
11457 static const char *
11458 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11459 {
11460 return dwarf2_compute_name (name, die, cu, 0);
11461 }
11462
11463 /* Construct a physname for the given DIE in CU. NAME may either be
11464 from a previous call to dwarf2_name or NULL. The result will be
11465 allocated on the objfile_objstack or NULL if the DIE does not have a
11466 name.
11467
11468 The output string will be canonicalized (if C++). */
11469
11470 static const char *
11471 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11472 {
11473 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11474 const char *retval, *mangled = NULL, *canon = NULL;
11475 int need_copy = 1;
11476
11477 /* In this case dwarf2_compute_name is just a shortcut not building anything
11478 on its own. */
11479 if (!die_needs_namespace (die, cu))
11480 return dwarf2_compute_name (name, die, cu, 1);
11481
11482 mangled = dw2_linkage_name (die, cu);
11483
11484 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11485 See https://github.com/rust-lang/rust/issues/32925. */
11486 if (cu->language == language_rust && mangled != NULL
11487 && strchr (mangled, '{') != NULL)
11488 mangled = NULL;
11489
11490 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11491 has computed. */
11492 gdb::unique_xmalloc_ptr<char> demangled;
11493 if (mangled != NULL)
11494 {
11495
11496 if (cu->language == language_go)
11497 {
11498 /* This is a lie, but we already lie to the caller new_symbol.
11499 new_symbol assumes we return the mangled name.
11500 This just undoes that lie until things are cleaned up. */
11501 }
11502 else
11503 {
11504 /* Use DMGL_RET_DROP for C++ template functions to suppress
11505 their return type. It is easier for GDB users to search
11506 for such functions as `name(params)' than `long name(params)'.
11507 In such case the minimal symbol names do not match the full
11508 symbol names but for template functions there is never a need
11509 to look up their definition from their declaration so
11510 the only disadvantage remains the minimal symbol variant
11511 `long name(params)' does not have the proper inferior type. */
11512 demangled.reset (gdb_demangle (mangled,
11513 (DMGL_PARAMS | DMGL_ANSI
11514 | DMGL_RET_DROP)));
11515 }
11516 if (demangled)
11517 canon = demangled.get ();
11518 else
11519 {
11520 canon = mangled;
11521 need_copy = 0;
11522 }
11523 }
11524
11525 if (canon == NULL || check_physname)
11526 {
11527 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11528
11529 if (canon != NULL && strcmp (physname, canon) != 0)
11530 {
11531 /* It may not mean a bug in GDB. The compiler could also
11532 compute DW_AT_linkage_name incorrectly. But in such case
11533 GDB would need to be bug-to-bug compatible. */
11534
11535 complaint (&symfile_complaints,
11536 _("Computed physname <%s> does not match demangled <%s> "
11537 "(from linkage <%s>) - DIE at %s [in module %s]"),
11538 physname, canon, mangled, sect_offset_str (die->sect_off),
11539 objfile_name (objfile));
11540
11541 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11542 is available here - over computed PHYSNAME. It is safer
11543 against both buggy GDB and buggy compilers. */
11544
11545 retval = canon;
11546 }
11547 else
11548 {
11549 retval = physname;
11550 need_copy = 0;
11551 }
11552 }
11553 else
11554 retval = canon;
11555
11556 if (need_copy)
11557 retval = ((const char *)
11558 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11559 retval, strlen (retval)));
11560
11561 return retval;
11562 }
11563
11564 /* Inspect DIE in CU for a namespace alias. If one exists, record
11565 a new symbol for it.
11566
11567 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11568
11569 static int
11570 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11571 {
11572 struct attribute *attr;
11573
11574 /* If the die does not have a name, this is not a namespace
11575 alias. */
11576 attr = dwarf2_attr (die, DW_AT_name, cu);
11577 if (attr != NULL)
11578 {
11579 int num;
11580 struct die_info *d = die;
11581 struct dwarf2_cu *imported_cu = cu;
11582
11583 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11584 keep inspecting DIEs until we hit the underlying import. */
11585 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11586 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11587 {
11588 attr = dwarf2_attr (d, DW_AT_import, cu);
11589 if (attr == NULL)
11590 break;
11591
11592 d = follow_die_ref (d, attr, &imported_cu);
11593 if (d->tag != DW_TAG_imported_declaration)
11594 break;
11595 }
11596
11597 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11598 {
11599 complaint (&symfile_complaints,
11600 _("DIE at %s has too many recursively imported "
11601 "declarations"), sect_offset_str (d->sect_off));
11602 return 0;
11603 }
11604
11605 if (attr != NULL)
11606 {
11607 struct type *type;
11608 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11609
11610 type = get_die_type_at_offset (sect_off, cu->per_cu);
11611 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11612 {
11613 /* This declaration is a global namespace alias. Add
11614 a symbol for it whose type is the aliased namespace. */
11615 new_symbol (die, type, cu);
11616 return 1;
11617 }
11618 }
11619 }
11620
11621 return 0;
11622 }
11623
11624 /* Return the using directives repository (global or local?) to use in the
11625 current context for LANGUAGE.
11626
11627 For Ada, imported declarations can materialize renamings, which *may* be
11628 global. However it is impossible (for now?) in DWARF to distinguish
11629 "external" imported declarations and "static" ones. As all imported
11630 declarations seem to be static in all other languages, make them all CU-wide
11631 global only in Ada. */
11632
11633 static struct using_direct **
11634 using_directives (enum language language)
11635 {
11636 if (language == language_ada && context_stack_depth == 0)
11637 return &global_using_directives;
11638 else
11639 return &local_using_directives;
11640 }
11641
11642 /* Read the import statement specified by the given die and record it. */
11643
11644 static void
11645 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11646 {
11647 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11648 struct attribute *import_attr;
11649 struct die_info *imported_die, *child_die;
11650 struct dwarf2_cu *imported_cu;
11651 const char *imported_name;
11652 const char *imported_name_prefix;
11653 const char *canonical_name;
11654 const char *import_alias;
11655 const char *imported_declaration = NULL;
11656 const char *import_prefix;
11657 std::vector<const char *> excludes;
11658
11659 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11660 if (import_attr == NULL)
11661 {
11662 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11663 dwarf_tag_name (die->tag));
11664 return;
11665 }
11666
11667 imported_cu = cu;
11668 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11669 imported_name = dwarf2_name (imported_die, imported_cu);
11670 if (imported_name == NULL)
11671 {
11672 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11673
11674 The import in the following code:
11675 namespace A
11676 {
11677 typedef int B;
11678 }
11679
11680 int main ()
11681 {
11682 using A::B;
11683 B b;
11684 return b;
11685 }
11686
11687 ...
11688 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11689 <52> DW_AT_decl_file : 1
11690 <53> DW_AT_decl_line : 6
11691 <54> DW_AT_import : <0x75>
11692 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11693 <59> DW_AT_name : B
11694 <5b> DW_AT_decl_file : 1
11695 <5c> DW_AT_decl_line : 2
11696 <5d> DW_AT_type : <0x6e>
11697 ...
11698 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11699 <76> DW_AT_byte_size : 4
11700 <77> DW_AT_encoding : 5 (signed)
11701
11702 imports the wrong die ( 0x75 instead of 0x58 ).
11703 This case will be ignored until the gcc bug is fixed. */
11704 return;
11705 }
11706
11707 /* Figure out the local name after import. */
11708 import_alias = dwarf2_name (die, cu);
11709
11710 /* Figure out where the statement is being imported to. */
11711 import_prefix = determine_prefix (die, cu);
11712
11713 /* Figure out what the scope of the imported die is and prepend it
11714 to the name of the imported die. */
11715 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11716
11717 if (imported_die->tag != DW_TAG_namespace
11718 && imported_die->tag != DW_TAG_module)
11719 {
11720 imported_declaration = imported_name;
11721 canonical_name = imported_name_prefix;
11722 }
11723 else if (strlen (imported_name_prefix) > 0)
11724 canonical_name = obconcat (&objfile->objfile_obstack,
11725 imported_name_prefix,
11726 (cu->language == language_d ? "." : "::"),
11727 imported_name, (char *) NULL);
11728 else
11729 canonical_name = imported_name;
11730
11731 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11732 for (child_die = die->child; child_die && child_die->tag;
11733 child_die = sibling_die (child_die))
11734 {
11735 /* DWARF-4: A Fortran use statement with a “rename list” may be
11736 represented by an imported module entry with an import attribute
11737 referring to the module and owned entries corresponding to those
11738 entities that are renamed as part of being imported. */
11739
11740 if (child_die->tag != DW_TAG_imported_declaration)
11741 {
11742 complaint (&symfile_complaints,
11743 _("child DW_TAG_imported_declaration expected "
11744 "- DIE at %s [in module %s]"),
11745 sect_offset_str (child_die->sect_off),
11746 objfile_name (objfile));
11747 continue;
11748 }
11749
11750 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11751 if (import_attr == NULL)
11752 {
11753 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11754 dwarf_tag_name (child_die->tag));
11755 continue;
11756 }
11757
11758 imported_cu = cu;
11759 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11760 &imported_cu);
11761 imported_name = dwarf2_name (imported_die, imported_cu);
11762 if (imported_name == NULL)
11763 {
11764 complaint (&symfile_complaints,
11765 _("child DW_TAG_imported_declaration has unknown "
11766 "imported name - DIE at %s [in module %s]"),
11767 sect_offset_str (child_die->sect_off),
11768 objfile_name (objfile));
11769 continue;
11770 }
11771
11772 excludes.push_back (imported_name);
11773
11774 process_die (child_die, cu);
11775 }
11776
11777 add_using_directive (using_directives (cu->language),
11778 import_prefix,
11779 canonical_name,
11780 import_alias,
11781 imported_declaration,
11782 excludes,
11783 0,
11784 &objfile->objfile_obstack);
11785 }
11786
11787 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11788 types, but gives them a size of zero. Starting with version 14,
11789 ICC is compatible with GCC. */
11790
11791 static int
11792 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11793 {
11794 if (!cu->checked_producer)
11795 check_producer (cu);
11796
11797 return cu->producer_is_icc_lt_14;
11798 }
11799
11800 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11801 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11802 this, it was first present in GCC release 4.3.0. */
11803
11804 static int
11805 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11806 {
11807 if (!cu->checked_producer)
11808 check_producer (cu);
11809
11810 return cu->producer_is_gcc_lt_4_3;
11811 }
11812
11813 static file_and_directory
11814 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11815 {
11816 file_and_directory res;
11817
11818 /* Find the filename. Do not use dwarf2_name here, since the filename
11819 is not a source language identifier. */
11820 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11821 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11822
11823 if (res.comp_dir == NULL
11824 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11825 && IS_ABSOLUTE_PATH (res.name))
11826 {
11827 res.comp_dir_storage = ldirname (res.name);
11828 if (!res.comp_dir_storage.empty ())
11829 res.comp_dir = res.comp_dir_storage.c_str ();
11830 }
11831 if (res.comp_dir != NULL)
11832 {
11833 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11834 directory, get rid of it. */
11835 const char *cp = strchr (res.comp_dir, ':');
11836
11837 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11838 res.comp_dir = cp + 1;
11839 }
11840
11841 if (res.name == NULL)
11842 res.name = "<unknown>";
11843
11844 return res;
11845 }
11846
11847 /* Handle DW_AT_stmt_list for a compilation unit.
11848 DIE is the DW_TAG_compile_unit die for CU.
11849 COMP_DIR is the compilation directory. LOWPC is passed to
11850 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11851
11852 static void
11853 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11854 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11855 {
11856 struct dwarf2_per_objfile *dwarf2_per_objfile
11857 = cu->per_cu->dwarf2_per_objfile;
11858 struct objfile *objfile = dwarf2_per_objfile->objfile;
11859 struct attribute *attr;
11860 struct line_header line_header_local;
11861 hashval_t line_header_local_hash;
11862 void **slot;
11863 int decode_mapping;
11864
11865 gdb_assert (! cu->per_cu->is_debug_types);
11866
11867 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11868 if (attr == NULL)
11869 return;
11870
11871 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11872
11873 /* The line header hash table is only created if needed (it exists to
11874 prevent redundant reading of the line table for partial_units).
11875 If we're given a partial_unit, we'll need it. If we're given a
11876 compile_unit, then use the line header hash table if it's already
11877 created, but don't create one just yet. */
11878
11879 if (dwarf2_per_objfile->line_header_hash == NULL
11880 && die->tag == DW_TAG_partial_unit)
11881 {
11882 dwarf2_per_objfile->line_header_hash
11883 = htab_create_alloc_ex (127, line_header_hash_voidp,
11884 line_header_eq_voidp,
11885 free_line_header_voidp,
11886 &objfile->objfile_obstack,
11887 hashtab_obstack_allocate,
11888 dummy_obstack_deallocate);
11889 }
11890
11891 line_header_local.sect_off = line_offset;
11892 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11893 line_header_local_hash = line_header_hash (&line_header_local);
11894 if (dwarf2_per_objfile->line_header_hash != NULL)
11895 {
11896 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11897 &line_header_local,
11898 line_header_local_hash, NO_INSERT);
11899
11900 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11901 is not present in *SLOT (since if there is something in *SLOT then
11902 it will be for a partial_unit). */
11903 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11904 {
11905 gdb_assert (*slot != NULL);
11906 cu->line_header = (struct line_header *) *slot;
11907 return;
11908 }
11909 }
11910
11911 /* dwarf_decode_line_header does not yet provide sufficient information.
11912 We always have to call also dwarf_decode_lines for it. */
11913 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11914 if (lh == NULL)
11915 return;
11916
11917 cu->line_header = lh.release ();
11918 cu->line_header_die_owner = die;
11919
11920 if (dwarf2_per_objfile->line_header_hash == NULL)
11921 slot = NULL;
11922 else
11923 {
11924 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11925 &line_header_local,
11926 line_header_local_hash, INSERT);
11927 gdb_assert (slot != NULL);
11928 }
11929 if (slot != NULL && *slot == NULL)
11930 {
11931 /* This newly decoded line number information unit will be owned
11932 by line_header_hash hash table. */
11933 *slot = cu->line_header;
11934 cu->line_header_die_owner = NULL;
11935 }
11936 else
11937 {
11938 /* We cannot free any current entry in (*slot) as that struct line_header
11939 may be already used by multiple CUs. Create only temporary decoded
11940 line_header for this CU - it may happen at most once for each line
11941 number information unit. And if we're not using line_header_hash
11942 then this is what we want as well. */
11943 gdb_assert (die->tag != DW_TAG_partial_unit);
11944 }
11945 decode_mapping = (die->tag != DW_TAG_partial_unit);
11946 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11947 decode_mapping);
11948
11949 }
11950
11951 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11952
11953 static void
11954 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11955 {
11956 struct dwarf2_per_objfile *dwarf2_per_objfile
11957 = cu->per_cu->dwarf2_per_objfile;
11958 struct objfile *objfile = dwarf2_per_objfile->objfile;
11959 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11960 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11961 CORE_ADDR highpc = ((CORE_ADDR) 0);
11962 struct attribute *attr;
11963 struct die_info *child_die;
11964 CORE_ADDR baseaddr;
11965
11966 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11967
11968 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11969
11970 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11971 from finish_block. */
11972 if (lowpc == ((CORE_ADDR) -1))
11973 lowpc = highpc;
11974 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11975
11976 file_and_directory fnd = find_file_and_directory (die, cu);
11977
11978 prepare_one_comp_unit (cu, die, cu->language);
11979
11980 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11981 standardised yet. As a workaround for the language detection we fall
11982 back to the DW_AT_producer string. */
11983 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11984 cu->language = language_opencl;
11985
11986 /* Similar hack for Go. */
11987 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11988 set_cu_language (DW_LANG_Go, cu);
11989
11990 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11991
11992 /* Decode line number information if present. We do this before
11993 processing child DIEs, so that the line header table is available
11994 for DW_AT_decl_file. */
11995 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11996
11997 /* Process all dies in compilation unit. */
11998 if (die->child != NULL)
11999 {
12000 child_die = die->child;
12001 while (child_die && child_die->tag)
12002 {
12003 process_die (child_die, cu);
12004 child_die = sibling_die (child_die);
12005 }
12006 }
12007
12008 /* Decode macro information, if present. Dwarf 2 macro information
12009 refers to information in the line number info statement program
12010 header, so we can only read it if we've read the header
12011 successfully. */
12012 attr = dwarf2_attr (die, DW_AT_macros, cu);
12013 if (attr == NULL)
12014 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
12015 if (attr && cu->line_header)
12016 {
12017 if (dwarf2_attr (die, DW_AT_macro_info, cu))
12018 complaint (&symfile_complaints,
12019 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
12020
12021 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
12022 }
12023 else
12024 {
12025 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
12026 if (attr && cu->line_header)
12027 {
12028 unsigned int macro_offset = DW_UNSND (attr);
12029
12030 dwarf_decode_macros (cu, macro_offset, 0);
12031 }
12032 }
12033 }
12034
12035 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
12036 Create the set of symtabs used by this TU, or if this TU is sharing
12037 symtabs with another TU and the symtabs have already been created
12038 then restore those symtabs in the line header.
12039 We don't need the pc/line-number mapping for type units. */
12040
12041 static void
12042 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
12043 {
12044 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
12045 struct type_unit_group *tu_group;
12046 int first_time;
12047 struct attribute *attr;
12048 unsigned int i;
12049 struct signatured_type *sig_type;
12050
12051 gdb_assert (per_cu->is_debug_types);
12052 sig_type = (struct signatured_type *) per_cu;
12053
12054 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
12055
12056 /* If we're using .gdb_index (includes -readnow) then
12057 per_cu->type_unit_group may not have been set up yet. */
12058 if (sig_type->type_unit_group == NULL)
12059 sig_type->type_unit_group = get_type_unit_group (cu, attr);
12060 tu_group = sig_type->type_unit_group;
12061
12062 /* If we've already processed this stmt_list there's no real need to
12063 do it again, we could fake it and just recreate the part we need
12064 (file name,index -> symtab mapping). If data shows this optimization
12065 is useful we can do it then. */
12066 first_time = tu_group->compunit_symtab == NULL;
12067
12068 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
12069 debug info. */
12070 line_header_up lh;
12071 if (attr != NULL)
12072 {
12073 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
12074 lh = dwarf_decode_line_header (line_offset, cu);
12075 }
12076 if (lh == NULL)
12077 {
12078 if (first_time)
12079 dwarf2_start_symtab (cu, "", NULL, 0);
12080 else
12081 {
12082 gdb_assert (tu_group->symtabs == NULL);
12083 restart_symtab (tu_group->compunit_symtab, "", 0);
12084 }
12085 return;
12086 }
12087
12088 cu->line_header = lh.release ();
12089 cu->line_header_die_owner = die;
12090
12091 if (first_time)
12092 {
12093 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
12094
12095 /* Note: We don't assign tu_group->compunit_symtab yet because we're
12096 still initializing it, and our caller (a few levels up)
12097 process_full_type_unit still needs to know if this is the first
12098 time. */
12099
12100 tu_group->num_symtabs = cu->line_header->file_names.size ();
12101 tu_group->symtabs = XNEWVEC (struct symtab *,
12102 cu->line_header->file_names.size ());
12103
12104 for (i = 0; i < cu->line_header->file_names.size (); ++i)
12105 {
12106 file_entry &fe = cu->line_header->file_names[i];
12107
12108 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
12109
12110 if (current_subfile->symtab == NULL)
12111 {
12112 /* NOTE: start_subfile will recognize when it's been
12113 passed a file it has already seen. So we can't
12114 assume there's a simple mapping from
12115 cu->line_header->file_names to subfiles, plus
12116 cu->line_header->file_names may contain dups. */
12117 current_subfile->symtab
12118 = allocate_symtab (cust, current_subfile->name);
12119 }
12120
12121 fe.symtab = current_subfile->symtab;
12122 tu_group->symtabs[i] = fe.symtab;
12123 }
12124 }
12125 else
12126 {
12127 restart_symtab (tu_group->compunit_symtab, "", 0);
12128
12129 for (i = 0; i < cu->line_header->file_names.size (); ++i)
12130 {
12131 file_entry &fe = cu->line_header->file_names[i];
12132
12133 fe.symtab = tu_group->symtabs[i];
12134 }
12135 }
12136
12137 /* The main symtab is allocated last. Type units don't have DW_AT_name
12138 so they don't have a "real" (so to speak) symtab anyway.
12139 There is later code that will assign the main symtab to all symbols
12140 that don't have one. We need to handle the case of a symbol with a
12141 missing symtab (DW_AT_decl_file) anyway. */
12142 }
12143
12144 /* Process DW_TAG_type_unit.
12145 For TUs we want to skip the first top level sibling if it's not the
12146 actual type being defined by this TU. In this case the first top
12147 level sibling is there to provide context only. */
12148
12149 static void
12150 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
12151 {
12152 struct die_info *child_die;
12153
12154 prepare_one_comp_unit (cu, die, language_minimal);
12155
12156 /* Initialize (or reinitialize) the machinery for building symtabs.
12157 We do this before processing child DIEs, so that the line header table
12158 is available for DW_AT_decl_file. */
12159 setup_type_unit_groups (die, cu);
12160
12161 if (die->child != NULL)
12162 {
12163 child_die = die->child;
12164 while (child_die && child_die->tag)
12165 {
12166 process_die (child_die, cu);
12167 child_die = sibling_die (child_die);
12168 }
12169 }
12170 }
12171 \f
12172 /* DWO/DWP files.
12173
12174 http://gcc.gnu.org/wiki/DebugFission
12175 http://gcc.gnu.org/wiki/DebugFissionDWP
12176
12177 To simplify handling of both DWO files ("object" files with the DWARF info)
12178 and DWP files (a file with the DWOs packaged up into one file), we treat
12179 DWP files as having a collection of virtual DWO files. */
12180
12181 static hashval_t
12182 hash_dwo_file (const void *item)
12183 {
12184 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
12185 hashval_t hash;
12186
12187 hash = htab_hash_string (dwo_file->dwo_name);
12188 if (dwo_file->comp_dir != NULL)
12189 hash += htab_hash_string (dwo_file->comp_dir);
12190 return hash;
12191 }
12192
12193 static int
12194 eq_dwo_file (const void *item_lhs, const void *item_rhs)
12195 {
12196 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
12197 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
12198
12199 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
12200 return 0;
12201 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
12202 return lhs->comp_dir == rhs->comp_dir;
12203 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
12204 }
12205
12206 /* Allocate a hash table for DWO files. */
12207
12208 static htab_t
12209 allocate_dwo_file_hash_table (struct objfile *objfile)
12210 {
12211 return htab_create_alloc_ex (41,
12212 hash_dwo_file,
12213 eq_dwo_file,
12214 NULL,
12215 &objfile->objfile_obstack,
12216 hashtab_obstack_allocate,
12217 dummy_obstack_deallocate);
12218 }
12219
12220 /* Lookup DWO file DWO_NAME. */
12221
12222 static void **
12223 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
12224 const char *dwo_name,
12225 const char *comp_dir)
12226 {
12227 struct dwo_file find_entry;
12228 void **slot;
12229
12230 if (dwarf2_per_objfile->dwo_files == NULL)
12231 dwarf2_per_objfile->dwo_files
12232 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
12233
12234 memset (&find_entry, 0, sizeof (find_entry));
12235 find_entry.dwo_name = dwo_name;
12236 find_entry.comp_dir = comp_dir;
12237 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
12238
12239 return slot;
12240 }
12241
12242 static hashval_t
12243 hash_dwo_unit (const void *item)
12244 {
12245 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12246
12247 /* This drops the top 32 bits of the id, but is ok for a hash. */
12248 return dwo_unit->signature;
12249 }
12250
12251 static int
12252 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
12253 {
12254 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
12255 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
12256
12257 /* The signature is assumed to be unique within the DWO file.
12258 So while object file CU dwo_id's always have the value zero,
12259 that's OK, assuming each object file DWO file has only one CU,
12260 and that's the rule for now. */
12261 return lhs->signature == rhs->signature;
12262 }
12263
12264 /* Allocate a hash table for DWO CUs,TUs.
12265 There is one of these tables for each of CUs,TUs for each DWO file. */
12266
12267 static htab_t
12268 allocate_dwo_unit_table (struct objfile *objfile)
12269 {
12270 /* Start out with a pretty small number.
12271 Generally DWO files contain only one CU and maybe some TUs. */
12272 return htab_create_alloc_ex (3,
12273 hash_dwo_unit,
12274 eq_dwo_unit,
12275 NULL,
12276 &objfile->objfile_obstack,
12277 hashtab_obstack_allocate,
12278 dummy_obstack_deallocate);
12279 }
12280
12281 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
12282
12283 struct create_dwo_cu_data
12284 {
12285 struct dwo_file *dwo_file;
12286 struct dwo_unit dwo_unit;
12287 };
12288
12289 /* die_reader_func for create_dwo_cu. */
12290
12291 static void
12292 create_dwo_cu_reader (const struct die_reader_specs *reader,
12293 const gdb_byte *info_ptr,
12294 struct die_info *comp_unit_die,
12295 int has_children,
12296 void *datap)
12297 {
12298 struct dwarf2_cu *cu = reader->cu;
12299 sect_offset sect_off = cu->per_cu->sect_off;
12300 struct dwarf2_section_info *section = cu->per_cu->section;
12301 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
12302 struct dwo_file *dwo_file = data->dwo_file;
12303 struct dwo_unit *dwo_unit = &data->dwo_unit;
12304 struct attribute *attr;
12305
12306 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
12307 if (attr == NULL)
12308 {
12309 complaint (&symfile_complaints,
12310 _("Dwarf Error: debug entry at offset %s is missing"
12311 " its dwo_id [in module %s]"),
12312 sect_offset_str (sect_off), dwo_file->dwo_name);
12313 return;
12314 }
12315
12316 dwo_unit->dwo_file = dwo_file;
12317 dwo_unit->signature = DW_UNSND (attr);
12318 dwo_unit->section = section;
12319 dwo_unit->sect_off = sect_off;
12320 dwo_unit->length = cu->per_cu->length;
12321
12322 if (dwarf_read_debug)
12323 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
12324 sect_offset_str (sect_off),
12325 hex_string (dwo_unit->signature));
12326 }
12327
12328 /* Create the dwo_units for the CUs in a DWO_FILE.
12329 Note: This function processes DWO files only, not DWP files. */
12330
12331 static void
12332 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12333 struct dwo_file &dwo_file, dwarf2_section_info &section,
12334 htab_t &cus_htab)
12335 {
12336 struct objfile *objfile = dwarf2_per_objfile->objfile;
12337 const gdb_byte *info_ptr, *end_ptr;
12338
12339 dwarf2_read_section (objfile, &section);
12340 info_ptr = section.buffer;
12341
12342 if (info_ptr == NULL)
12343 return;
12344
12345 if (dwarf_read_debug)
12346 {
12347 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
12348 get_section_name (&section),
12349 get_section_file_name (&section));
12350 }
12351
12352 end_ptr = info_ptr + section.size;
12353 while (info_ptr < end_ptr)
12354 {
12355 struct dwarf2_per_cu_data per_cu;
12356 struct create_dwo_cu_data create_dwo_cu_data;
12357 struct dwo_unit *dwo_unit;
12358 void **slot;
12359 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
12360
12361 memset (&create_dwo_cu_data.dwo_unit, 0,
12362 sizeof (create_dwo_cu_data.dwo_unit));
12363 memset (&per_cu, 0, sizeof (per_cu));
12364 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
12365 per_cu.is_debug_types = 0;
12366 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12367 per_cu.section = &section;
12368 create_dwo_cu_data.dwo_file = &dwo_file;
12369
12370 init_cutu_and_read_dies_no_follow (
12371 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12372 info_ptr += per_cu.length;
12373
12374 // If the unit could not be parsed, skip it.
12375 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12376 continue;
12377
12378 if (cus_htab == NULL)
12379 cus_htab = allocate_dwo_unit_table (objfile);
12380
12381 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12382 *dwo_unit = create_dwo_cu_data.dwo_unit;
12383 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12384 gdb_assert (slot != NULL);
12385 if (*slot != NULL)
12386 {
12387 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12388 sect_offset dup_sect_off = dup_cu->sect_off;
12389
12390 complaint (&symfile_complaints,
12391 _("debug cu entry at offset %s is duplicate to"
12392 " the entry at offset %s, signature %s"),
12393 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12394 hex_string (dwo_unit->signature));
12395 }
12396 *slot = (void *)dwo_unit;
12397 }
12398 }
12399
12400 /* DWP file .debug_{cu,tu}_index section format:
12401 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12402
12403 DWP Version 1:
12404
12405 Both index sections have the same format, and serve to map a 64-bit
12406 signature to a set of section numbers. Each section begins with a header,
12407 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12408 indexes, and a pool of 32-bit section numbers. The index sections will be
12409 aligned at 8-byte boundaries in the file.
12410
12411 The index section header consists of:
12412
12413 V, 32 bit version number
12414 -, 32 bits unused
12415 N, 32 bit number of compilation units or type units in the index
12416 M, 32 bit number of slots in the hash table
12417
12418 Numbers are recorded using the byte order of the application binary.
12419
12420 The hash table begins at offset 16 in the section, and consists of an array
12421 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12422 order of the application binary). Unused slots in the hash table are 0.
12423 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12424
12425 The parallel table begins immediately after the hash table
12426 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12427 array of 32-bit indexes (using the byte order of the application binary),
12428 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12429 table contains a 32-bit index into the pool of section numbers. For unused
12430 hash table slots, the corresponding entry in the parallel table will be 0.
12431
12432 The pool of section numbers begins immediately following the hash table
12433 (at offset 16 + 12 * M from the beginning of the section). The pool of
12434 section numbers consists of an array of 32-bit words (using the byte order
12435 of the application binary). Each item in the array is indexed starting
12436 from 0. The hash table entry provides the index of the first section
12437 number in the set. Additional section numbers in the set follow, and the
12438 set is terminated by a 0 entry (section number 0 is not used in ELF).
12439
12440 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12441 section must be the first entry in the set, and the .debug_abbrev.dwo must
12442 be the second entry. Other members of the set may follow in any order.
12443
12444 ---
12445
12446 DWP Version 2:
12447
12448 DWP Version 2 combines all the .debug_info, etc. sections into one,
12449 and the entries in the index tables are now offsets into these sections.
12450 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12451 section.
12452
12453 Index Section Contents:
12454 Header
12455 Hash Table of Signatures dwp_hash_table.hash_table
12456 Parallel Table of Indices dwp_hash_table.unit_table
12457 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12458 Table of Section Sizes dwp_hash_table.v2.sizes
12459
12460 The index section header consists of:
12461
12462 V, 32 bit version number
12463 L, 32 bit number of columns in the table of section offsets
12464 N, 32 bit number of compilation units or type units in the index
12465 M, 32 bit number of slots in the hash table
12466
12467 Numbers are recorded using the byte order of the application binary.
12468
12469 The hash table has the same format as version 1.
12470 The parallel table of indices has the same format as version 1,
12471 except that the entries are origin-1 indices into the table of sections
12472 offsets and the table of section sizes.
12473
12474 The table of offsets begins immediately following the parallel table
12475 (at offset 16 + 12 * M from the beginning of the section). The table is
12476 a two-dimensional array of 32-bit words (using the byte order of the
12477 application binary), with L columns and N+1 rows, in row-major order.
12478 Each row in the array is indexed starting from 0. The first row provides
12479 a key to the remaining rows: each column in this row provides an identifier
12480 for a debug section, and the offsets in the same column of subsequent rows
12481 refer to that section. The section identifiers are:
12482
12483 DW_SECT_INFO 1 .debug_info.dwo
12484 DW_SECT_TYPES 2 .debug_types.dwo
12485 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12486 DW_SECT_LINE 4 .debug_line.dwo
12487 DW_SECT_LOC 5 .debug_loc.dwo
12488 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12489 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12490 DW_SECT_MACRO 8 .debug_macro.dwo
12491
12492 The offsets provided by the CU and TU index sections are the base offsets
12493 for the contributions made by each CU or TU to the corresponding section
12494 in the package file. Each CU and TU header contains an abbrev_offset
12495 field, used to find the abbreviations table for that CU or TU within the
12496 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12497 be interpreted as relative to the base offset given in the index section.
12498 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12499 should be interpreted as relative to the base offset for .debug_line.dwo,
12500 and offsets into other debug sections obtained from DWARF attributes should
12501 also be interpreted as relative to the corresponding base offset.
12502
12503 The table of sizes begins immediately following the table of offsets.
12504 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12505 with L columns and N rows, in row-major order. Each row in the array is
12506 indexed starting from 1 (row 0 is shared by the two tables).
12507
12508 ---
12509
12510 Hash table lookup is handled the same in version 1 and 2:
12511
12512 We assume that N and M will not exceed 2^32 - 1.
12513 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12514
12515 Given a 64-bit compilation unit signature or a type signature S, an entry
12516 in the hash table is located as follows:
12517
12518 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12519 the low-order k bits all set to 1.
12520
12521 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12522
12523 3) If the hash table entry at index H matches the signature, use that
12524 entry. If the hash table entry at index H is unused (all zeroes),
12525 terminate the search: the signature is not present in the table.
12526
12527 4) Let H = (H + H') modulo M. Repeat at Step 3.
12528
12529 Because M > N and H' and M are relatively prime, the search is guaranteed
12530 to stop at an unused slot or find the match. */
12531
12532 /* Create a hash table to map DWO IDs to their CU/TU entry in
12533 .debug_{info,types}.dwo in DWP_FILE.
12534 Returns NULL if there isn't one.
12535 Note: This function processes DWP files only, not DWO files. */
12536
12537 static struct dwp_hash_table *
12538 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12539 struct dwp_file *dwp_file, int is_debug_types)
12540 {
12541 struct objfile *objfile = dwarf2_per_objfile->objfile;
12542 bfd *dbfd = dwp_file->dbfd;
12543 const gdb_byte *index_ptr, *index_end;
12544 struct dwarf2_section_info *index;
12545 uint32_t version, nr_columns, nr_units, nr_slots;
12546 struct dwp_hash_table *htab;
12547
12548 if (is_debug_types)
12549 index = &dwp_file->sections.tu_index;
12550 else
12551 index = &dwp_file->sections.cu_index;
12552
12553 if (dwarf2_section_empty_p (index))
12554 return NULL;
12555 dwarf2_read_section (objfile, index);
12556
12557 index_ptr = index->buffer;
12558 index_end = index_ptr + index->size;
12559
12560 version = read_4_bytes (dbfd, index_ptr);
12561 index_ptr += 4;
12562 if (version == 2)
12563 nr_columns = read_4_bytes (dbfd, index_ptr);
12564 else
12565 nr_columns = 0;
12566 index_ptr += 4;
12567 nr_units = read_4_bytes (dbfd, index_ptr);
12568 index_ptr += 4;
12569 nr_slots = read_4_bytes (dbfd, index_ptr);
12570 index_ptr += 4;
12571
12572 if (version != 1 && version != 2)
12573 {
12574 error (_("Dwarf Error: unsupported DWP file version (%s)"
12575 " [in module %s]"),
12576 pulongest (version), dwp_file->name);
12577 }
12578 if (nr_slots != (nr_slots & -nr_slots))
12579 {
12580 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12581 " is not power of 2 [in module %s]"),
12582 pulongest (nr_slots), dwp_file->name);
12583 }
12584
12585 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12586 htab->version = version;
12587 htab->nr_columns = nr_columns;
12588 htab->nr_units = nr_units;
12589 htab->nr_slots = nr_slots;
12590 htab->hash_table = index_ptr;
12591 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12592
12593 /* Exit early if the table is empty. */
12594 if (nr_slots == 0 || nr_units == 0
12595 || (version == 2 && nr_columns == 0))
12596 {
12597 /* All must be zero. */
12598 if (nr_slots != 0 || nr_units != 0
12599 || (version == 2 && nr_columns != 0))
12600 {
12601 complaint (&symfile_complaints,
12602 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12603 " all zero [in modules %s]"),
12604 dwp_file->name);
12605 }
12606 return htab;
12607 }
12608
12609 if (version == 1)
12610 {
12611 htab->section_pool.v1.indices =
12612 htab->unit_table + sizeof (uint32_t) * nr_slots;
12613 /* It's harder to decide whether the section is too small in v1.
12614 V1 is deprecated anyway so we punt. */
12615 }
12616 else
12617 {
12618 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12619 int *ids = htab->section_pool.v2.section_ids;
12620 /* Reverse map for error checking. */
12621 int ids_seen[DW_SECT_MAX + 1];
12622 int i;
12623
12624 if (nr_columns < 2)
12625 {
12626 error (_("Dwarf Error: bad DWP hash table, too few columns"
12627 " in section table [in module %s]"),
12628 dwp_file->name);
12629 }
12630 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12631 {
12632 error (_("Dwarf Error: bad DWP hash table, too many columns"
12633 " in section table [in module %s]"),
12634 dwp_file->name);
12635 }
12636 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12637 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12638 for (i = 0; i < nr_columns; ++i)
12639 {
12640 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12641
12642 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12643 {
12644 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12645 " in section table [in module %s]"),
12646 id, dwp_file->name);
12647 }
12648 if (ids_seen[id] != -1)
12649 {
12650 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12651 " id %d in section table [in module %s]"),
12652 id, dwp_file->name);
12653 }
12654 ids_seen[id] = i;
12655 ids[i] = id;
12656 }
12657 /* Must have exactly one info or types section. */
12658 if (((ids_seen[DW_SECT_INFO] != -1)
12659 + (ids_seen[DW_SECT_TYPES] != -1))
12660 != 1)
12661 {
12662 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12663 " DWO info/types section [in module %s]"),
12664 dwp_file->name);
12665 }
12666 /* Must have an abbrev section. */
12667 if (ids_seen[DW_SECT_ABBREV] == -1)
12668 {
12669 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12670 " section [in module %s]"),
12671 dwp_file->name);
12672 }
12673 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12674 htab->section_pool.v2.sizes =
12675 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12676 * nr_units * nr_columns);
12677 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12678 * nr_units * nr_columns))
12679 > index_end)
12680 {
12681 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12682 " [in module %s]"),
12683 dwp_file->name);
12684 }
12685 }
12686
12687 return htab;
12688 }
12689
12690 /* Update SECTIONS with the data from SECTP.
12691
12692 This function is like the other "locate" section routines that are
12693 passed to bfd_map_over_sections, but in this context the sections to
12694 read comes from the DWP V1 hash table, not the full ELF section table.
12695
12696 The result is non-zero for success, or zero if an error was found. */
12697
12698 static int
12699 locate_v1_virtual_dwo_sections (asection *sectp,
12700 struct virtual_v1_dwo_sections *sections)
12701 {
12702 const struct dwop_section_names *names = &dwop_section_names;
12703
12704 if (section_is_p (sectp->name, &names->abbrev_dwo))
12705 {
12706 /* There can be only one. */
12707 if (sections->abbrev.s.section != NULL)
12708 return 0;
12709 sections->abbrev.s.section = sectp;
12710 sections->abbrev.size = bfd_get_section_size (sectp);
12711 }
12712 else if (section_is_p (sectp->name, &names->info_dwo)
12713 || section_is_p (sectp->name, &names->types_dwo))
12714 {
12715 /* There can be only one. */
12716 if (sections->info_or_types.s.section != NULL)
12717 return 0;
12718 sections->info_or_types.s.section = sectp;
12719 sections->info_or_types.size = bfd_get_section_size (sectp);
12720 }
12721 else if (section_is_p (sectp->name, &names->line_dwo))
12722 {
12723 /* There can be only one. */
12724 if (sections->line.s.section != NULL)
12725 return 0;
12726 sections->line.s.section = sectp;
12727 sections->line.size = bfd_get_section_size (sectp);
12728 }
12729 else if (section_is_p (sectp->name, &names->loc_dwo))
12730 {
12731 /* There can be only one. */
12732 if (sections->loc.s.section != NULL)
12733 return 0;
12734 sections->loc.s.section = sectp;
12735 sections->loc.size = bfd_get_section_size (sectp);
12736 }
12737 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12738 {
12739 /* There can be only one. */
12740 if (sections->macinfo.s.section != NULL)
12741 return 0;
12742 sections->macinfo.s.section = sectp;
12743 sections->macinfo.size = bfd_get_section_size (sectp);
12744 }
12745 else if (section_is_p (sectp->name, &names->macro_dwo))
12746 {
12747 /* There can be only one. */
12748 if (sections->macro.s.section != NULL)
12749 return 0;
12750 sections->macro.s.section = sectp;
12751 sections->macro.size = bfd_get_section_size (sectp);
12752 }
12753 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12754 {
12755 /* There can be only one. */
12756 if (sections->str_offsets.s.section != NULL)
12757 return 0;
12758 sections->str_offsets.s.section = sectp;
12759 sections->str_offsets.size = bfd_get_section_size (sectp);
12760 }
12761 else
12762 {
12763 /* No other kind of section is valid. */
12764 return 0;
12765 }
12766
12767 return 1;
12768 }
12769
12770 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12771 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12772 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12773 This is for DWP version 1 files. */
12774
12775 static struct dwo_unit *
12776 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12777 struct dwp_file *dwp_file,
12778 uint32_t unit_index,
12779 const char *comp_dir,
12780 ULONGEST signature, int is_debug_types)
12781 {
12782 struct objfile *objfile = dwarf2_per_objfile->objfile;
12783 const struct dwp_hash_table *dwp_htab =
12784 is_debug_types ? dwp_file->tus : dwp_file->cus;
12785 bfd *dbfd = dwp_file->dbfd;
12786 const char *kind = is_debug_types ? "TU" : "CU";
12787 struct dwo_file *dwo_file;
12788 struct dwo_unit *dwo_unit;
12789 struct virtual_v1_dwo_sections sections;
12790 void **dwo_file_slot;
12791 int i;
12792
12793 gdb_assert (dwp_file->version == 1);
12794
12795 if (dwarf_read_debug)
12796 {
12797 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12798 kind,
12799 pulongest (unit_index), hex_string (signature),
12800 dwp_file->name);
12801 }
12802
12803 /* Fetch the sections of this DWO unit.
12804 Put a limit on the number of sections we look for so that bad data
12805 doesn't cause us to loop forever. */
12806
12807 #define MAX_NR_V1_DWO_SECTIONS \
12808 (1 /* .debug_info or .debug_types */ \
12809 + 1 /* .debug_abbrev */ \
12810 + 1 /* .debug_line */ \
12811 + 1 /* .debug_loc */ \
12812 + 1 /* .debug_str_offsets */ \
12813 + 1 /* .debug_macro or .debug_macinfo */ \
12814 + 1 /* trailing zero */)
12815
12816 memset (&sections, 0, sizeof (sections));
12817
12818 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12819 {
12820 asection *sectp;
12821 uint32_t section_nr =
12822 read_4_bytes (dbfd,
12823 dwp_htab->section_pool.v1.indices
12824 + (unit_index + i) * sizeof (uint32_t));
12825
12826 if (section_nr == 0)
12827 break;
12828 if (section_nr >= dwp_file->num_sections)
12829 {
12830 error (_("Dwarf Error: bad DWP hash table, section number too large"
12831 " [in module %s]"),
12832 dwp_file->name);
12833 }
12834
12835 sectp = dwp_file->elf_sections[section_nr];
12836 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12837 {
12838 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12839 " [in module %s]"),
12840 dwp_file->name);
12841 }
12842 }
12843
12844 if (i < 2
12845 || dwarf2_section_empty_p (&sections.info_or_types)
12846 || dwarf2_section_empty_p (&sections.abbrev))
12847 {
12848 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12849 " [in module %s]"),
12850 dwp_file->name);
12851 }
12852 if (i == MAX_NR_V1_DWO_SECTIONS)
12853 {
12854 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12855 " [in module %s]"),
12856 dwp_file->name);
12857 }
12858
12859 /* It's easier for the rest of the code if we fake a struct dwo_file and
12860 have dwo_unit "live" in that. At least for now.
12861
12862 The DWP file can be made up of a random collection of CUs and TUs.
12863 However, for each CU + set of TUs that came from the same original DWO
12864 file, we can combine them back into a virtual DWO file to save space
12865 (fewer struct dwo_file objects to allocate). Remember that for really
12866 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12867
12868 std::string virtual_dwo_name =
12869 string_printf ("virtual-dwo/%d-%d-%d-%d",
12870 get_section_id (&sections.abbrev),
12871 get_section_id (&sections.line),
12872 get_section_id (&sections.loc),
12873 get_section_id (&sections.str_offsets));
12874 /* Can we use an existing virtual DWO file? */
12875 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12876 virtual_dwo_name.c_str (),
12877 comp_dir);
12878 /* Create one if necessary. */
12879 if (*dwo_file_slot == NULL)
12880 {
12881 if (dwarf_read_debug)
12882 {
12883 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12884 virtual_dwo_name.c_str ());
12885 }
12886 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12887 dwo_file->dwo_name
12888 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12889 virtual_dwo_name.c_str (),
12890 virtual_dwo_name.size ());
12891 dwo_file->comp_dir = comp_dir;
12892 dwo_file->sections.abbrev = sections.abbrev;
12893 dwo_file->sections.line = sections.line;
12894 dwo_file->sections.loc = sections.loc;
12895 dwo_file->sections.macinfo = sections.macinfo;
12896 dwo_file->sections.macro = sections.macro;
12897 dwo_file->sections.str_offsets = sections.str_offsets;
12898 /* The "str" section is global to the entire DWP file. */
12899 dwo_file->sections.str = dwp_file->sections.str;
12900 /* The info or types section is assigned below to dwo_unit,
12901 there's no need to record it in dwo_file.
12902 Also, we can't simply record type sections in dwo_file because
12903 we record a pointer into the vector in dwo_unit. As we collect more
12904 types we'll grow the vector and eventually have to reallocate space
12905 for it, invalidating all copies of pointers into the previous
12906 contents. */
12907 *dwo_file_slot = dwo_file;
12908 }
12909 else
12910 {
12911 if (dwarf_read_debug)
12912 {
12913 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12914 virtual_dwo_name.c_str ());
12915 }
12916 dwo_file = (struct dwo_file *) *dwo_file_slot;
12917 }
12918
12919 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12920 dwo_unit->dwo_file = dwo_file;
12921 dwo_unit->signature = signature;
12922 dwo_unit->section =
12923 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12924 *dwo_unit->section = sections.info_or_types;
12925 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12926
12927 return dwo_unit;
12928 }
12929
12930 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12931 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12932 piece within that section used by a TU/CU, return a virtual section
12933 of just that piece. */
12934
12935 static struct dwarf2_section_info
12936 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12937 struct dwarf2_section_info *section,
12938 bfd_size_type offset, bfd_size_type size)
12939 {
12940 struct dwarf2_section_info result;
12941 asection *sectp;
12942
12943 gdb_assert (section != NULL);
12944 gdb_assert (!section->is_virtual);
12945
12946 memset (&result, 0, sizeof (result));
12947 result.s.containing_section = section;
12948 result.is_virtual = 1;
12949
12950 if (size == 0)
12951 return result;
12952
12953 sectp = get_section_bfd_section (section);
12954
12955 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12956 bounds of the real section. This is a pretty-rare event, so just
12957 flag an error (easier) instead of a warning and trying to cope. */
12958 if (sectp == NULL
12959 || offset + size > bfd_get_section_size (sectp))
12960 {
12961 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12962 " in section %s [in module %s]"),
12963 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12964 objfile_name (dwarf2_per_objfile->objfile));
12965 }
12966
12967 result.virtual_offset = offset;
12968 result.size = size;
12969 return result;
12970 }
12971
12972 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12973 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12974 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12975 This is for DWP version 2 files. */
12976
12977 static struct dwo_unit *
12978 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12979 struct dwp_file *dwp_file,
12980 uint32_t unit_index,
12981 const char *comp_dir,
12982 ULONGEST signature, int is_debug_types)
12983 {
12984 struct objfile *objfile = dwarf2_per_objfile->objfile;
12985 const struct dwp_hash_table *dwp_htab =
12986 is_debug_types ? dwp_file->tus : dwp_file->cus;
12987 bfd *dbfd = dwp_file->dbfd;
12988 const char *kind = is_debug_types ? "TU" : "CU";
12989 struct dwo_file *dwo_file;
12990 struct dwo_unit *dwo_unit;
12991 struct virtual_v2_dwo_sections sections;
12992 void **dwo_file_slot;
12993 int i;
12994
12995 gdb_assert (dwp_file->version == 2);
12996
12997 if (dwarf_read_debug)
12998 {
12999 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
13000 kind,
13001 pulongest (unit_index), hex_string (signature),
13002 dwp_file->name);
13003 }
13004
13005 /* Fetch the section offsets of this DWO unit. */
13006
13007 memset (&sections, 0, sizeof (sections));
13008
13009 for (i = 0; i < dwp_htab->nr_columns; ++i)
13010 {
13011 uint32_t offset = read_4_bytes (dbfd,
13012 dwp_htab->section_pool.v2.offsets
13013 + (((unit_index - 1) * dwp_htab->nr_columns
13014 + i)
13015 * sizeof (uint32_t)));
13016 uint32_t size = read_4_bytes (dbfd,
13017 dwp_htab->section_pool.v2.sizes
13018 + (((unit_index - 1) * dwp_htab->nr_columns
13019 + i)
13020 * sizeof (uint32_t)));
13021
13022 switch (dwp_htab->section_pool.v2.section_ids[i])
13023 {
13024 case DW_SECT_INFO:
13025 case DW_SECT_TYPES:
13026 sections.info_or_types_offset = offset;
13027 sections.info_or_types_size = size;
13028 break;
13029 case DW_SECT_ABBREV:
13030 sections.abbrev_offset = offset;
13031 sections.abbrev_size = size;
13032 break;
13033 case DW_SECT_LINE:
13034 sections.line_offset = offset;
13035 sections.line_size = size;
13036 break;
13037 case DW_SECT_LOC:
13038 sections.loc_offset = offset;
13039 sections.loc_size = size;
13040 break;
13041 case DW_SECT_STR_OFFSETS:
13042 sections.str_offsets_offset = offset;
13043 sections.str_offsets_size = size;
13044 break;
13045 case DW_SECT_MACINFO:
13046 sections.macinfo_offset = offset;
13047 sections.macinfo_size = size;
13048 break;
13049 case DW_SECT_MACRO:
13050 sections.macro_offset = offset;
13051 sections.macro_size = size;
13052 break;
13053 }
13054 }
13055
13056 /* It's easier for the rest of the code if we fake a struct dwo_file and
13057 have dwo_unit "live" in that. At least for now.
13058
13059 The DWP file can be made up of a random collection of CUs and TUs.
13060 However, for each CU + set of TUs that came from the same original DWO
13061 file, we can combine them back into a virtual DWO file to save space
13062 (fewer struct dwo_file objects to allocate). Remember that for really
13063 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
13064
13065 std::string virtual_dwo_name =
13066 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
13067 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
13068 (long) (sections.line_size ? sections.line_offset : 0),
13069 (long) (sections.loc_size ? sections.loc_offset : 0),
13070 (long) (sections.str_offsets_size
13071 ? sections.str_offsets_offset : 0));
13072 /* Can we use an existing virtual DWO file? */
13073 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13074 virtual_dwo_name.c_str (),
13075 comp_dir);
13076 /* Create one if necessary. */
13077 if (*dwo_file_slot == NULL)
13078 {
13079 if (dwarf_read_debug)
13080 {
13081 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
13082 virtual_dwo_name.c_str ());
13083 }
13084 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
13085 dwo_file->dwo_name
13086 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
13087 virtual_dwo_name.c_str (),
13088 virtual_dwo_name.size ());
13089 dwo_file->comp_dir = comp_dir;
13090 dwo_file->sections.abbrev =
13091 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
13092 sections.abbrev_offset, sections.abbrev_size);
13093 dwo_file->sections.line =
13094 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
13095 sections.line_offset, sections.line_size);
13096 dwo_file->sections.loc =
13097 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
13098 sections.loc_offset, sections.loc_size);
13099 dwo_file->sections.macinfo =
13100 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
13101 sections.macinfo_offset, sections.macinfo_size);
13102 dwo_file->sections.macro =
13103 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
13104 sections.macro_offset, sections.macro_size);
13105 dwo_file->sections.str_offsets =
13106 create_dwp_v2_section (dwarf2_per_objfile,
13107 &dwp_file->sections.str_offsets,
13108 sections.str_offsets_offset,
13109 sections.str_offsets_size);
13110 /* The "str" section is global to the entire DWP file. */
13111 dwo_file->sections.str = dwp_file->sections.str;
13112 /* The info or types section is assigned below to dwo_unit,
13113 there's no need to record it in dwo_file.
13114 Also, we can't simply record type sections in dwo_file because
13115 we record a pointer into the vector in dwo_unit. As we collect more
13116 types we'll grow the vector and eventually have to reallocate space
13117 for it, invalidating all copies of pointers into the previous
13118 contents. */
13119 *dwo_file_slot = dwo_file;
13120 }
13121 else
13122 {
13123 if (dwarf_read_debug)
13124 {
13125 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
13126 virtual_dwo_name.c_str ());
13127 }
13128 dwo_file = (struct dwo_file *) *dwo_file_slot;
13129 }
13130
13131 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
13132 dwo_unit->dwo_file = dwo_file;
13133 dwo_unit->signature = signature;
13134 dwo_unit->section =
13135 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
13136 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
13137 is_debug_types
13138 ? &dwp_file->sections.types
13139 : &dwp_file->sections.info,
13140 sections.info_or_types_offset,
13141 sections.info_or_types_size);
13142 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
13143
13144 return dwo_unit;
13145 }
13146
13147 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
13148 Returns NULL if the signature isn't found. */
13149
13150 static struct dwo_unit *
13151 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
13152 struct dwp_file *dwp_file, const char *comp_dir,
13153 ULONGEST signature, int is_debug_types)
13154 {
13155 const struct dwp_hash_table *dwp_htab =
13156 is_debug_types ? dwp_file->tus : dwp_file->cus;
13157 bfd *dbfd = dwp_file->dbfd;
13158 uint32_t mask = dwp_htab->nr_slots - 1;
13159 uint32_t hash = signature & mask;
13160 uint32_t hash2 = ((signature >> 32) & mask) | 1;
13161 unsigned int i;
13162 void **slot;
13163 struct dwo_unit find_dwo_cu;
13164
13165 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
13166 find_dwo_cu.signature = signature;
13167 slot = htab_find_slot (is_debug_types
13168 ? dwp_file->loaded_tus
13169 : dwp_file->loaded_cus,
13170 &find_dwo_cu, INSERT);
13171
13172 if (*slot != NULL)
13173 return (struct dwo_unit *) *slot;
13174
13175 /* Use a for loop so that we don't loop forever on bad debug info. */
13176 for (i = 0; i < dwp_htab->nr_slots; ++i)
13177 {
13178 ULONGEST signature_in_table;
13179
13180 signature_in_table =
13181 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
13182 if (signature_in_table == signature)
13183 {
13184 uint32_t unit_index =
13185 read_4_bytes (dbfd,
13186 dwp_htab->unit_table + hash * sizeof (uint32_t));
13187
13188 if (dwp_file->version == 1)
13189 {
13190 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
13191 dwp_file, unit_index,
13192 comp_dir, signature,
13193 is_debug_types);
13194 }
13195 else
13196 {
13197 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
13198 dwp_file, unit_index,
13199 comp_dir, signature,
13200 is_debug_types);
13201 }
13202 return (struct dwo_unit *) *slot;
13203 }
13204 if (signature_in_table == 0)
13205 return NULL;
13206 hash = (hash + hash2) & mask;
13207 }
13208
13209 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
13210 " [in module %s]"),
13211 dwp_file->name);
13212 }
13213
13214 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
13215 Open the file specified by FILE_NAME and hand it off to BFD for
13216 preliminary analysis. Return a newly initialized bfd *, which
13217 includes a canonicalized copy of FILE_NAME.
13218 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
13219 SEARCH_CWD is true if the current directory is to be searched.
13220 It will be searched before debug-file-directory.
13221 If successful, the file is added to the bfd include table of the
13222 objfile's bfd (see gdb_bfd_record_inclusion).
13223 If unable to find/open the file, return NULL.
13224 NOTE: This function is derived from symfile_bfd_open. */
13225
13226 static gdb_bfd_ref_ptr
13227 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13228 const char *file_name, int is_dwp, int search_cwd)
13229 {
13230 int desc;
13231 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
13232 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
13233 to debug_file_directory. */
13234 const char *search_path;
13235 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
13236
13237 gdb::unique_xmalloc_ptr<char> search_path_holder;
13238 if (search_cwd)
13239 {
13240 if (*debug_file_directory != '\0')
13241 {
13242 search_path_holder.reset (concat (".", dirname_separator_string,
13243 debug_file_directory,
13244 (char *) NULL));
13245 search_path = search_path_holder.get ();
13246 }
13247 else
13248 search_path = ".";
13249 }
13250 else
13251 search_path = debug_file_directory;
13252
13253 openp_flags flags = OPF_RETURN_REALPATH;
13254 if (is_dwp)
13255 flags |= OPF_SEARCH_IN_PATH;
13256
13257 gdb::unique_xmalloc_ptr<char> absolute_name;
13258 desc = openp (search_path, flags, file_name,
13259 O_RDONLY | O_BINARY, &absolute_name);
13260 if (desc < 0)
13261 return NULL;
13262
13263 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
13264 gnutarget, desc));
13265 if (sym_bfd == NULL)
13266 return NULL;
13267 bfd_set_cacheable (sym_bfd.get (), 1);
13268
13269 if (!bfd_check_format (sym_bfd.get (), bfd_object))
13270 return NULL;
13271
13272 /* Success. Record the bfd as having been included by the objfile's bfd.
13273 This is important because things like demangled_names_hash lives in the
13274 objfile's per_bfd space and may have references to things like symbol
13275 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
13276 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13277
13278 return sym_bfd;
13279 }
13280
13281 /* Try to open DWO file FILE_NAME.
13282 COMP_DIR is the DW_AT_comp_dir attribute.
13283 The result is the bfd handle of the file.
13284 If there is a problem finding or opening the file, return NULL.
13285 Upon success, the canonicalized path of the file is stored in the bfd,
13286 same as symfile_bfd_open. */
13287
13288 static gdb_bfd_ref_ptr
13289 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13290 const char *file_name, const char *comp_dir)
13291 {
13292 if (IS_ABSOLUTE_PATH (file_name))
13293 return try_open_dwop_file (dwarf2_per_objfile, file_name,
13294 0 /*is_dwp*/, 0 /*search_cwd*/);
13295
13296 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
13297
13298 if (comp_dir != NULL)
13299 {
13300 char *path_to_try = concat (comp_dir, SLASH_STRING,
13301 file_name, (char *) NULL);
13302
13303 /* NOTE: If comp_dir is a relative path, this will also try the
13304 search path, which seems useful. */
13305 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
13306 path_to_try,
13307 0 /*is_dwp*/,
13308 1 /*search_cwd*/));
13309 xfree (path_to_try);
13310 if (abfd != NULL)
13311 return abfd;
13312 }
13313
13314 /* That didn't work, try debug-file-directory, which, despite its name,
13315 is a list of paths. */
13316
13317 if (*debug_file_directory == '\0')
13318 return NULL;
13319
13320 return try_open_dwop_file (dwarf2_per_objfile, file_name,
13321 0 /*is_dwp*/, 1 /*search_cwd*/);
13322 }
13323
13324 /* This function is mapped across the sections and remembers the offset and
13325 size of each of the DWO debugging sections we are interested in. */
13326
13327 static void
13328 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
13329 {
13330 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
13331 const struct dwop_section_names *names = &dwop_section_names;
13332
13333 if (section_is_p (sectp->name, &names->abbrev_dwo))
13334 {
13335 dwo_sections->abbrev.s.section = sectp;
13336 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
13337 }
13338 else if (section_is_p (sectp->name, &names->info_dwo))
13339 {
13340 dwo_sections->info.s.section = sectp;
13341 dwo_sections->info.size = bfd_get_section_size (sectp);
13342 }
13343 else if (section_is_p (sectp->name, &names->line_dwo))
13344 {
13345 dwo_sections->line.s.section = sectp;
13346 dwo_sections->line.size = bfd_get_section_size (sectp);
13347 }
13348 else if (section_is_p (sectp->name, &names->loc_dwo))
13349 {
13350 dwo_sections->loc.s.section = sectp;
13351 dwo_sections->loc.size = bfd_get_section_size (sectp);
13352 }
13353 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13354 {
13355 dwo_sections->macinfo.s.section = sectp;
13356 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
13357 }
13358 else if (section_is_p (sectp->name, &names->macro_dwo))
13359 {
13360 dwo_sections->macro.s.section = sectp;
13361 dwo_sections->macro.size = bfd_get_section_size (sectp);
13362 }
13363 else if (section_is_p (sectp->name, &names->str_dwo))
13364 {
13365 dwo_sections->str.s.section = sectp;
13366 dwo_sections->str.size = bfd_get_section_size (sectp);
13367 }
13368 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13369 {
13370 dwo_sections->str_offsets.s.section = sectp;
13371 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
13372 }
13373 else if (section_is_p (sectp->name, &names->types_dwo))
13374 {
13375 struct dwarf2_section_info type_section;
13376
13377 memset (&type_section, 0, sizeof (type_section));
13378 type_section.s.section = sectp;
13379 type_section.size = bfd_get_section_size (sectp);
13380 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
13381 &type_section);
13382 }
13383 }
13384
13385 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13386 by PER_CU. This is for the non-DWP case.
13387 The result is NULL if DWO_NAME can't be found. */
13388
13389 static struct dwo_file *
13390 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13391 const char *dwo_name, const char *comp_dir)
13392 {
13393 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13394 struct objfile *objfile = dwarf2_per_objfile->objfile;
13395 struct dwo_file *dwo_file;
13396 struct cleanup *cleanups;
13397
13398 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
13399 if (dbfd == NULL)
13400 {
13401 if (dwarf_read_debug)
13402 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13403 return NULL;
13404 }
13405 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
13406 dwo_file->dwo_name = dwo_name;
13407 dwo_file->comp_dir = comp_dir;
13408 dwo_file->dbfd = dbfd.release ();
13409
13410 free_dwo_file_cleanup_data *cleanup_data = XNEW (free_dwo_file_cleanup_data);
13411 cleanup_data->dwo_file = dwo_file;
13412 cleanup_data->dwarf2_per_objfile = dwarf2_per_objfile;
13413
13414 cleanups = make_cleanup (free_dwo_file_cleanup, cleanup_data);
13415
13416 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13417 &dwo_file->sections);
13418
13419 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13420 dwo_file->cus);
13421
13422 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file,
13423 dwo_file->sections.types, dwo_file->tus);
13424
13425 discard_cleanups (cleanups);
13426
13427 if (dwarf_read_debug)
13428 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13429
13430 return dwo_file;
13431 }
13432
13433 /* This function is mapped across the sections and remembers the offset and
13434 size of each of the DWP debugging sections common to version 1 and 2 that
13435 we are interested in. */
13436
13437 static void
13438 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13439 void *dwp_file_ptr)
13440 {
13441 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13442 const struct dwop_section_names *names = &dwop_section_names;
13443 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13444
13445 /* Record the ELF section number for later lookup: this is what the
13446 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13447 gdb_assert (elf_section_nr < dwp_file->num_sections);
13448 dwp_file->elf_sections[elf_section_nr] = sectp;
13449
13450 /* Look for specific sections that we need. */
13451 if (section_is_p (sectp->name, &names->str_dwo))
13452 {
13453 dwp_file->sections.str.s.section = sectp;
13454 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13455 }
13456 else if (section_is_p (sectp->name, &names->cu_index))
13457 {
13458 dwp_file->sections.cu_index.s.section = sectp;
13459 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13460 }
13461 else if (section_is_p (sectp->name, &names->tu_index))
13462 {
13463 dwp_file->sections.tu_index.s.section = sectp;
13464 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13465 }
13466 }
13467
13468 /* This function is mapped across the sections and remembers the offset and
13469 size of each of the DWP version 2 debugging sections that we are interested
13470 in. This is split into a separate function because we don't know if we
13471 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13472
13473 static void
13474 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13475 {
13476 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13477 const struct dwop_section_names *names = &dwop_section_names;
13478 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13479
13480 /* Record the ELF section number for later lookup: this is what the
13481 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13482 gdb_assert (elf_section_nr < dwp_file->num_sections);
13483 dwp_file->elf_sections[elf_section_nr] = sectp;
13484
13485 /* Look for specific sections that we need. */
13486 if (section_is_p (sectp->name, &names->abbrev_dwo))
13487 {
13488 dwp_file->sections.abbrev.s.section = sectp;
13489 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13490 }
13491 else if (section_is_p (sectp->name, &names->info_dwo))
13492 {
13493 dwp_file->sections.info.s.section = sectp;
13494 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13495 }
13496 else if (section_is_p (sectp->name, &names->line_dwo))
13497 {
13498 dwp_file->sections.line.s.section = sectp;
13499 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13500 }
13501 else if (section_is_p (sectp->name, &names->loc_dwo))
13502 {
13503 dwp_file->sections.loc.s.section = sectp;
13504 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13505 }
13506 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13507 {
13508 dwp_file->sections.macinfo.s.section = sectp;
13509 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13510 }
13511 else if (section_is_p (sectp->name, &names->macro_dwo))
13512 {
13513 dwp_file->sections.macro.s.section = sectp;
13514 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13515 }
13516 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13517 {
13518 dwp_file->sections.str_offsets.s.section = sectp;
13519 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13520 }
13521 else if (section_is_p (sectp->name, &names->types_dwo))
13522 {
13523 dwp_file->sections.types.s.section = sectp;
13524 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13525 }
13526 }
13527
13528 /* Hash function for dwp_file loaded CUs/TUs. */
13529
13530 static hashval_t
13531 hash_dwp_loaded_cutus (const void *item)
13532 {
13533 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13534
13535 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13536 return dwo_unit->signature;
13537 }
13538
13539 /* Equality function for dwp_file loaded CUs/TUs. */
13540
13541 static int
13542 eq_dwp_loaded_cutus (const void *a, const void *b)
13543 {
13544 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13545 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13546
13547 return dua->signature == dub->signature;
13548 }
13549
13550 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13551
13552 static htab_t
13553 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13554 {
13555 return htab_create_alloc_ex (3,
13556 hash_dwp_loaded_cutus,
13557 eq_dwp_loaded_cutus,
13558 NULL,
13559 &objfile->objfile_obstack,
13560 hashtab_obstack_allocate,
13561 dummy_obstack_deallocate);
13562 }
13563
13564 /* Try to open DWP file FILE_NAME.
13565 The result is the bfd handle of the file.
13566 If there is a problem finding or opening the file, return NULL.
13567 Upon success, the canonicalized path of the file is stored in the bfd,
13568 same as symfile_bfd_open. */
13569
13570 static gdb_bfd_ref_ptr
13571 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13572 const char *file_name)
13573 {
13574 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13575 1 /*is_dwp*/,
13576 1 /*search_cwd*/));
13577 if (abfd != NULL)
13578 return abfd;
13579
13580 /* Work around upstream bug 15652.
13581 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13582 [Whether that's a "bug" is debatable, but it is getting in our way.]
13583 We have no real idea where the dwp file is, because gdb's realpath-ing
13584 of the executable's path may have discarded the needed info.
13585 [IWBN if the dwp file name was recorded in the executable, akin to
13586 .gnu_debuglink, but that doesn't exist yet.]
13587 Strip the directory from FILE_NAME and search again. */
13588 if (*debug_file_directory != '\0')
13589 {
13590 /* Don't implicitly search the current directory here.
13591 If the user wants to search "." to handle this case,
13592 it must be added to debug-file-directory. */
13593 return try_open_dwop_file (dwarf2_per_objfile,
13594 lbasename (file_name), 1 /*is_dwp*/,
13595 0 /*search_cwd*/);
13596 }
13597
13598 return NULL;
13599 }
13600
13601 /* Initialize the use of the DWP file for the current objfile.
13602 By convention the name of the DWP file is ${objfile}.dwp.
13603 The result is NULL if it can't be found. */
13604
13605 static struct dwp_file *
13606 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13607 {
13608 struct objfile *objfile = dwarf2_per_objfile->objfile;
13609 struct dwp_file *dwp_file;
13610
13611 /* Try to find first .dwp for the binary file before any symbolic links
13612 resolving. */
13613
13614 /* If the objfile is a debug file, find the name of the real binary
13615 file and get the name of dwp file from there. */
13616 std::string dwp_name;
13617 if (objfile->separate_debug_objfile_backlink != NULL)
13618 {
13619 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13620 const char *backlink_basename = lbasename (backlink->original_name);
13621
13622 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13623 }
13624 else
13625 dwp_name = objfile->original_name;
13626
13627 dwp_name += ".dwp";
13628
13629 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13630 if (dbfd == NULL
13631 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13632 {
13633 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13634 dwp_name = objfile_name (objfile);
13635 dwp_name += ".dwp";
13636 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13637 }
13638
13639 if (dbfd == NULL)
13640 {
13641 if (dwarf_read_debug)
13642 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13643 return NULL;
13644 }
13645 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
13646 dwp_file->name = bfd_get_filename (dbfd.get ());
13647 dwp_file->dbfd = dbfd.release ();
13648
13649 /* +1: section 0 is unused */
13650 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13651 dwp_file->elf_sections =
13652 OBSTACK_CALLOC (&objfile->objfile_obstack,
13653 dwp_file->num_sections, asection *);
13654
13655 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13656 dwp_file);
13657
13658 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
13659
13660 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
13661
13662 /* The DWP file version is stored in the hash table. Oh well. */
13663 if (dwp_file->cus && dwp_file->tus
13664 && dwp_file->cus->version != dwp_file->tus->version)
13665 {
13666 /* Technically speaking, we should try to limp along, but this is
13667 pretty bizarre. We use pulongest here because that's the established
13668 portability solution (e.g, we cannot use %u for uint32_t). */
13669 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13670 " TU version %s [in DWP file %s]"),
13671 pulongest (dwp_file->cus->version),
13672 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13673 }
13674
13675 if (dwp_file->cus)
13676 dwp_file->version = dwp_file->cus->version;
13677 else if (dwp_file->tus)
13678 dwp_file->version = dwp_file->tus->version;
13679 else
13680 dwp_file->version = 2;
13681
13682 if (dwp_file->version == 2)
13683 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13684 dwp_file);
13685
13686 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13687 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13688
13689 if (dwarf_read_debug)
13690 {
13691 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13692 fprintf_unfiltered (gdb_stdlog,
13693 " %s CUs, %s TUs\n",
13694 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13695 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13696 }
13697
13698 return dwp_file;
13699 }
13700
13701 /* Wrapper around open_and_init_dwp_file, only open it once. */
13702
13703 static struct dwp_file *
13704 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13705 {
13706 if (! dwarf2_per_objfile->dwp_checked)
13707 {
13708 dwarf2_per_objfile->dwp_file
13709 = open_and_init_dwp_file (dwarf2_per_objfile);
13710 dwarf2_per_objfile->dwp_checked = 1;
13711 }
13712 return dwarf2_per_objfile->dwp_file;
13713 }
13714
13715 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13716 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13717 or in the DWP file for the objfile, referenced by THIS_UNIT.
13718 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13719 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13720
13721 This is called, for example, when wanting to read a variable with a
13722 complex location. Therefore we don't want to do file i/o for every call.
13723 Therefore we don't want to look for a DWO file on every call.
13724 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13725 then we check if we've already seen DWO_NAME, and only THEN do we check
13726 for a DWO file.
13727
13728 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13729 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13730
13731 static struct dwo_unit *
13732 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13733 const char *dwo_name, const char *comp_dir,
13734 ULONGEST signature, int is_debug_types)
13735 {
13736 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13737 struct objfile *objfile = dwarf2_per_objfile->objfile;
13738 const char *kind = is_debug_types ? "TU" : "CU";
13739 void **dwo_file_slot;
13740 struct dwo_file *dwo_file;
13741 struct dwp_file *dwp_file;
13742
13743 /* First see if there's a DWP file.
13744 If we have a DWP file but didn't find the DWO inside it, don't
13745 look for the original DWO file. It makes gdb behave differently
13746 depending on whether one is debugging in the build tree. */
13747
13748 dwp_file = get_dwp_file (dwarf2_per_objfile);
13749 if (dwp_file != NULL)
13750 {
13751 const struct dwp_hash_table *dwp_htab =
13752 is_debug_types ? dwp_file->tus : dwp_file->cus;
13753
13754 if (dwp_htab != NULL)
13755 {
13756 struct dwo_unit *dwo_cutu =
13757 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13758 signature, is_debug_types);
13759
13760 if (dwo_cutu != NULL)
13761 {
13762 if (dwarf_read_debug)
13763 {
13764 fprintf_unfiltered (gdb_stdlog,
13765 "Virtual DWO %s %s found: @%s\n",
13766 kind, hex_string (signature),
13767 host_address_to_string (dwo_cutu));
13768 }
13769 return dwo_cutu;
13770 }
13771 }
13772 }
13773 else
13774 {
13775 /* No DWP file, look for the DWO file. */
13776
13777 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13778 dwo_name, comp_dir);
13779 if (*dwo_file_slot == NULL)
13780 {
13781 /* Read in the file and build a table of the CUs/TUs it contains. */
13782 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13783 }
13784 /* NOTE: This will be NULL if unable to open the file. */
13785 dwo_file = (struct dwo_file *) *dwo_file_slot;
13786
13787 if (dwo_file != NULL)
13788 {
13789 struct dwo_unit *dwo_cutu = NULL;
13790
13791 if (is_debug_types && dwo_file->tus)
13792 {
13793 struct dwo_unit find_dwo_cutu;
13794
13795 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13796 find_dwo_cutu.signature = signature;
13797 dwo_cutu
13798 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13799 }
13800 else if (!is_debug_types && dwo_file->cus)
13801 {
13802 struct dwo_unit find_dwo_cutu;
13803
13804 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13805 find_dwo_cutu.signature = signature;
13806 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13807 &find_dwo_cutu);
13808 }
13809
13810 if (dwo_cutu != NULL)
13811 {
13812 if (dwarf_read_debug)
13813 {
13814 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13815 kind, dwo_name, hex_string (signature),
13816 host_address_to_string (dwo_cutu));
13817 }
13818 return dwo_cutu;
13819 }
13820 }
13821 }
13822
13823 /* We didn't find it. This could mean a dwo_id mismatch, or
13824 someone deleted the DWO/DWP file, or the search path isn't set up
13825 correctly to find the file. */
13826
13827 if (dwarf_read_debug)
13828 {
13829 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13830 kind, dwo_name, hex_string (signature));
13831 }
13832
13833 /* This is a warning and not a complaint because it can be caused by
13834 pilot error (e.g., user accidentally deleting the DWO). */
13835 {
13836 /* Print the name of the DWP file if we looked there, helps the user
13837 better diagnose the problem. */
13838 std::string dwp_text;
13839
13840 if (dwp_file != NULL)
13841 dwp_text = string_printf (" [in DWP file %s]",
13842 lbasename (dwp_file->name));
13843
13844 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13845 " [in module %s]"),
13846 kind, dwo_name, hex_string (signature),
13847 dwp_text.c_str (),
13848 this_unit->is_debug_types ? "TU" : "CU",
13849 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13850 }
13851 return NULL;
13852 }
13853
13854 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13855 See lookup_dwo_cutu_unit for details. */
13856
13857 static struct dwo_unit *
13858 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13859 const char *dwo_name, const char *comp_dir,
13860 ULONGEST signature)
13861 {
13862 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13863 }
13864
13865 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13866 See lookup_dwo_cutu_unit for details. */
13867
13868 static struct dwo_unit *
13869 lookup_dwo_type_unit (struct signatured_type *this_tu,
13870 const char *dwo_name, const char *comp_dir)
13871 {
13872 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13873 }
13874
13875 /* Traversal function for queue_and_load_all_dwo_tus. */
13876
13877 static int
13878 queue_and_load_dwo_tu (void **slot, void *info)
13879 {
13880 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13881 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13882 ULONGEST signature = dwo_unit->signature;
13883 struct signatured_type *sig_type =
13884 lookup_dwo_signatured_type (per_cu->cu, signature);
13885
13886 if (sig_type != NULL)
13887 {
13888 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13889
13890 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13891 a real dependency of PER_CU on SIG_TYPE. That is detected later
13892 while processing PER_CU. */
13893 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13894 load_full_type_unit (sig_cu);
13895 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13896 }
13897
13898 return 1;
13899 }
13900
13901 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13902 The DWO may have the only definition of the type, though it may not be
13903 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13904 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13905
13906 static void
13907 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13908 {
13909 struct dwo_unit *dwo_unit;
13910 struct dwo_file *dwo_file;
13911
13912 gdb_assert (!per_cu->is_debug_types);
13913 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13914 gdb_assert (per_cu->cu != NULL);
13915
13916 dwo_unit = per_cu->cu->dwo_unit;
13917 gdb_assert (dwo_unit != NULL);
13918
13919 dwo_file = dwo_unit->dwo_file;
13920 if (dwo_file->tus != NULL)
13921 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13922 }
13923
13924 /* Free all resources associated with DWO_FILE.
13925 Close the DWO file and munmap the sections.
13926 All memory should be on the objfile obstack. */
13927
13928 static void
13929 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
13930 {
13931
13932 /* Note: dbfd is NULL for virtual DWO files. */
13933 gdb_bfd_unref (dwo_file->dbfd);
13934
13935 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13936 }
13937
13938 /* Wrapper for free_dwo_file for use in cleanups. */
13939
13940 static void
13941 free_dwo_file_cleanup (void *arg)
13942 {
13943 struct free_dwo_file_cleanup_data *data
13944 = (struct free_dwo_file_cleanup_data *) arg;
13945 struct objfile *objfile = data->dwarf2_per_objfile->objfile;
13946
13947 free_dwo_file (data->dwo_file, objfile);
13948
13949 xfree (data);
13950 }
13951
13952 /* Traversal function for free_dwo_files. */
13953
13954 static int
13955 free_dwo_file_from_slot (void **slot, void *info)
13956 {
13957 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13958 struct objfile *objfile = (struct objfile *) info;
13959
13960 free_dwo_file (dwo_file, objfile);
13961
13962 return 1;
13963 }
13964
13965 /* Free all resources associated with DWO_FILES. */
13966
13967 static void
13968 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13969 {
13970 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13971 }
13972 \f
13973 /* Read in various DIEs. */
13974
13975 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13976 Inherit only the children of the DW_AT_abstract_origin DIE not being
13977 already referenced by DW_AT_abstract_origin from the children of the
13978 current DIE. */
13979
13980 static void
13981 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13982 {
13983 struct die_info *child_die;
13984 sect_offset *offsetp;
13985 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13986 struct die_info *origin_die;
13987 /* Iterator of the ORIGIN_DIE children. */
13988 struct die_info *origin_child_die;
13989 struct attribute *attr;
13990 struct dwarf2_cu *origin_cu;
13991 struct pending **origin_previous_list_in_scope;
13992
13993 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13994 if (!attr)
13995 return;
13996
13997 /* Note that following die references may follow to a die in a
13998 different cu. */
13999
14000 origin_cu = cu;
14001 origin_die = follow_die_ref (die, attr, &origin_cu);
14002
14003 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
14004 symbols in. */
14005 origin_previous_list_in_scope = origin_cu->list_in_scope;
14006 origin_cu->list_in_scope = cu->list_in_scope;
14007
14008 if (die->tag != origin_die->tag
14009 && !(die->tag == DW_TAG_inlined_subroutine
14010 && origin_die->tag == DW_TAG_subprogram))
14011 complaint (&symfile_complaints,
14012 _("DIE %s and its abstract origin %s have different tags"),
14013 sect_offset_str (die->sect_off),
14014 sect_offset_str (origin_die->sect_off));
14015
14016 std::vector<sect_offset> offsets;
14017
14018 for (child_die = die->child;
14019 child_die && child_die->tag;
14020 child_die = sibling_die (child_die))
14021 {
14022 struct die_info *child_origin_die;
14023 struct dwarf2_cu *child_origin_cu;
14024
14025 /* We are trying to process concrete instance entries:
14026 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
14027 it's not relevant to our analysis here. i.e. detecting DIEs that are
14028 present in the abstract instance but not referenced in the concrete
14029 one. */
14030 if (child_die->tag == DW_TAG_call_site
14031 || child_die->tag == DW_TAG_GNU_call_site)
14032 continue;
14033
14034 /* For each CHILD_DIE, find the corresponding child of
14035 ORIGIN_DIE. If there is more than one layer of
14036 DW_AT_abstract_origin, follow them all; there shouldn't be,
14037 but GCC versions at least through 4.4 generate this (GCC PR
14038 40573). */
14039 child_origin_die = child_die;
14040 child_origin_cu = cu;
14041 while (1)
14042 {
14043 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
14044 child_origin_cu);
14045 if (attr == NULL)
14046 break;
14047 child_origin_die = follow_die_ref (child_origin_die, attr,
14048 &child_origin_cu);
14049 }
14050
14051 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
14052 counterpart may exist. */
14053 if (child_origin_die != child_die)
14054 {
14055 if (child_die->tag != child_origin_die->tag
14056 && !(child_die->tag == DW_TAG_inlined_subroutine
14057 && child_origin_die->tag == DW_TAG_subprogram))
14058 complaint (&symfile_complaints,
14059 _("Child DIE %s and its abstract origin %s have "
14060 "different tags"),
14061 sect_offset_str (child_die->sect_off),
14062 sect_offset_str (child_origin_die->sect_off));
14063 if (child_origin_die->parent != origin_die)
14064 complaint (&symfile_complaints,
14065 _("Child DIE %s and its abstract origin %s have "
14066 "different parents"),
14067 sect_offset_str (child_die->sect_off),
14068 sect_offset_str (child_origin_die->sect_off));
14069 else
14070 offsets.push_back (child_origin_die->sect_off);
14071 }
14072 }
14073 std::sort (offsets.begin (), offsets.end ());
14074 sect_offset *offsets_end = offsets.data () + offsets.size ();
14075 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
14076 if (offsetp[-1] == *offsetp)
14077 complaint (&symfile_complaints,
14078 _("Multiple children of DIE %s refer "
14079 "to DIE %s as their abstract origin"),
14080 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
14081
14082 offsetp = offsets.data ();
14083 origin_child_die = origin_die->child;
14084 while (origin_child_die && origin_child_die->tag)
14085 {
14086 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
14087 while (offsetp < offsets_end
14088 && *offsetp < origin_child_die->sect_off)
14089 offsetp++;
14090 if (offsetp >= offsets_end
14091 || *offsetp > origin_child_die->sect_off)
14092 {
14093 /* Found that ORIGIN_CHILD_DIE is really not referenced.
14094 Check whether we're already processing ORIGIN_CHILD_DIE.
14095 This can happen with mutually referenced abstract_origins.
14096 PR 16581. */
14097 if (!origin_child_die->in_process)
14098 process_die (origin_child_die, origin_cu);
14099 }
14100 origin_child_die = sibling_die (origin_child_die);
14101 }
14102 origin_cu->list_in_scope = origin_previous_list_in_scope;
14103 }
14104
14105 static void
14106 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
14107 {
14108 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14109 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14110 struct context_stack *newobj;
14111 CORE_ADDR lowpc;
14112 CORE_ADDR highpc;
14113 struct die_info *child_die;
14114 struct attribute *attr, *call_line, *call_file;
14115 const char *name;
14116 CORE_ADDR baseaddr;
14117 struct block *block;
14118 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
14119 std::vector<struct symbol *> template_args;
14120 struct template_symbol *templ_func = NULL;
14121
14122 if (inlined_func)
14123 {
14124 /* If we do not have call site information, we can't show the
14125 caller of this inlined function. That's too confusing, so
14126 only use the scope for local variables. */
14127 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
14128 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
14129 if (call_line == NULL || call_file == NULL)
14130 {
14131 read_lexical_block_scope (die, cu);
14132 return;
14133 }
14134 }
14135
14136 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14137
14138 name = dwarf2_name (die, cu);
14139
14140 /* Ignore functions with missing or empty names. These are actually
14141 illegal according to the DWARF standard. */
14142 if (name == NULL)
14143 {
14144 complaint (&symfile_complaints,
14145 _("missing name for subprogram DIE at %s"),
14146 sect_offset_str (die->sect_off));
14147 return;
14148 }
14149
14150 /* Ignore functions with missing or invalid low and high pc attributes. */
14151 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
14152 <= PC_BOUNDS_INVALID)
14153 {
14154 attr = dwarf2_attr (die, DW_AT_external, cu);
14155 if (!attr || !DW_UNSND (attr))
14156 complaint (&symfile_complaints,
14157 _("cannot get low and high bounds "
14158 "for subprogram DIE at %s"),
14159 sect_offset_str (die->sect_off));
14160 return;
14161 }
14162
14163 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14164 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
14165
14166 /* If we have any template arguments, then we must allocate a
14167 different sort of symbol. */
14168 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
14169 {
14170 if (child_die->tag == DW_TAG_template_type_param
14171 || child_die->tag == DW_TAG_template_value_param)
14172 {
14173 templ_func = allocate_template_symbol (objfile);
14174 templ_func->subclass = SYMBOL_TEMPLATE;
14175 break;
14176 }
14177 }
14178
14179 newobj = push_context (0, lowpc);
14180 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
14181 (struct symbol *) templ_func);
14182
14183 /* If there is a location expression for DW_AT_frame_base, record
14184 it. */
14185 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
14186 if (attr)
14187 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
14188
14189 /* If there is a location for the static link, record it. */
14190 newobj->static_link = NULL;
14191 attr = dwarf2_attr (die, DW_AT_static_link, cu);
14192 if (attr)
14193 {
14194 newobj->static_link
14195 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
14196 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
14197 }
14198
14199 cu->list_in_scope = &local_symbols;
14200
14201 if (die->child != NULL)
14202 {
14203 child_die = die->child;
14204 while (child_die && child_die->tag)
14205 {
14206 if (child_die->tag == DW_TAG_template_type_param
14207 || child_die->tag == DW_TAG_template_value_param)
14208 {
14209 struct symbol *arg = new_symbol (child_die, NULL, cu);
14210
14211 if (arg != NULL)
14212 template_args.push_back (arg);
14213 }
14214 else
14215 process_die (child_die, cu);
14216 child_die = sibling_die (child_die);
14217 }
14218 }
14219
14220 inherit_abstract_dies (die, cu);
14221
14222 /* If we have a DW_AT_specification, we might need to import using
14223 directives from the context of the specification DIE. See the
14224 comment in determine_prefix. */
14225 if (cu->language == language_cplus
14226 && dwarf2_attr (die, DW_AT_specification, cu))
14227 {
14228 struct dwarf2_cu *spec_cu = cu;
14229 struct die_info *spec_die = die_specification (die, &spec_cu);
14230
14231 while (spec_die)
14232 {
14233 child_die = spec_die->child;
14234 while (child_die && child_die->tag)
14235 {
14236 if (child_die->tag == DW_TAG_imported_module)
14237 process_die (child_die, spec_cu);
14238 child_die = sibling_die (child_die);
14239 }
14240
14241 /* In some cases, GCC generates specification DIEs that
14242 themselves contain DW_AT_specification attributes. */
14243 spec_die = die_specification (spec_die, &spec_cu);
14244 }
14245 }
14246
14247 newobj = pop_context ();
14248 /* Make a block for the local symbols within. */
14249 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
14250 newobj->static_link, lowpc, highpc);
14251
14252 /* For C++, set the block's scope. */
14253 if ((cu->language == language_cplus
14254 || cu->language == language_fortran
14255 || cu->language == language_d
14256 || cu->language == language_rust)
14257 && cu->processing_has_namespace_info)
14258 block_set_scope (block, determine_prefix (die, cu),
14259 &objfile->objfile_obstack);
14260
14261 /* If we have address ranges, record them. */
14262 dwarf2_record_block_ranges (die, block, baseaddr, cu);
14263
14264 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
14265
14266 /* Attach template arguments to function. */
14267 if (!template_args.empty ())
14268 {
14269 gdb_assert (templ_func != NULL);
14270
14271 templ_func->n_template_arguments = template_args.size ();
14272 templ_func->template_arguments
14273 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
14274 templ_func->n_template_arguments);
14275 memcpy (templ_func->template_arguments,
14276 template_args.data (),
14277 (templ_func->n_template_arguments * sizeof (struct symbol *)));
14278 }
14279
14280 /* In C++, we can have functions nested inside functions (e.g., when
14281 a function declares a class that has methods). This means that
14282 when we finish processing a function scope, we may need to go
14283 back to building a containing block's symbol lists. */
14284 local_symbols = newobj->locals;
14285 local_using_directives = newobj->local_using_directives;
14286
14287 /* If we've finished processing a top-level function, subsequent
14288 symbols go in the file symbol list. */
14289 if (outermost_context_p ())
14290 cu->list_in_scope = &file_symbols;
14291 }
14292
14293 /* Process all the DIES contained within a lexical block scope. Start
14294 a new scope, process the dies, and then close the scope. */
14295
14296 static void
14297 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
14298 {
14299 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14300 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14301 struct context_stack *newobj;
14302 CORE_ADDR lowpc, highpc;
14303 struct die_info *child_die;
14304 CORE_ADDR baseaddr;
14305
14306 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14307
14308 /* Ignore blocks with missing or invalid low and high pc attributes. */
14309 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
14310 as multiple lexical blocks? Handling children in a sane way would
14311 be nasty. Might be easier to properly extend generic blocks to
14312 describe ranges. */
14313 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
14314 {
14315 case PC_BOUNDS_NOT_PRESENT:
14316 /* DW_TAG_lexical_block has no attributes, process its children as if
14317 there was no wrapping by that DW_TAG_lexical_block.
14318 GCC does no longer produces such DWARF since GCC r224161. */
14319 for (child_die = die->child;
14320 child_die != NULL && child_die->tag;
14321 child_die = sibling_die (child_die))
14322 process_die (child_die, cu);
14323 return;
14324 case PC_BOUNDS_INVALID:
14325 return;
14326 }
14327 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14328 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
14329
14330 push_context (0, lowpc);
14331 if (die->child != NULL)
14332 {
14333 child_die = die->child;
14334 while (child_die && child_die->tag)
14335 {
14336 process_die (child_die, cu);
14337 child_die = sibling_die (child_die);
14338 }
14339 }
14340 inherit_abstract_dies (die, cu);
14341 newobj = pop_context ();
14342
14343 if (local_symbols != NULL || local_using_directives != NULL)
14344 {
14345 struct block *block
14346 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
14347 newobj->start_addr, highpc);
14348
14349 /* Note that recording ranges after traversing children, as we
14350 do here, means that recording a parent's ranges entails
14351 walking across all its children's ranges as they appear in
14352 the address map, which is quadratic behavior.
14353
14354 It would be nicer to record the parent's ranges before
14355 traversing its children, simply overriding whatever you find
14356 there. But since we don't even decide whether to create a
14357 block until after we've traversed its children, that's hard
14358 to do. */
14359 dwarf2_record_block_ranges (die, block, baseaddr, cu);
14360 }
14361 local_symbols = newobj->locals;
14362 local_using_directives = newobj->local_using_directives;
14363 }
14364
14365 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
14366
14367 static void
14368 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
14369 {
14370 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14371 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14372 CORE_ADDR pc, baseaddr;
14373 struct attribute *attr;
14374 struct call_site *call_site, call_site_local;
14375 void **slot;
14376 int nparams;
14377 struct die_info *child_die;
14378
14379 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14380
14381 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
14382 if (attr == NULL)
14383 {
14384 /* This was a pre-DWARF-5 GNU extension alias
14385 for DW_AT_call_return_pc. */
14386 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14387 }
14388 if (!attr)
14389 {
14390 complaint (&symfile_complaints,
14391 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
14392 "DIE %s [in module %s]"),
14393 sect_offset_str (die->sect_off), objfile_name (objfile));
14394 return;
14395 }
14396 pc = attr_value_as_address (attr) + baseaddr;
14397 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
14398
14399 if (cu->call_site_htab == NULL)
14400 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14401 NULL, &objfile->objfile_obstack,
14402 hashtab_obstack_allocate, NULL);
14403 call_site_local.pc = pc;
14404 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14405 if (*slot != NULL)
14406 {
14407 complaint (&symfile_complaints,
14408 _("Duplicate PC %s for DW_TAG_call_site "
14409 "DIE %s [in module %s]"),
14410 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
14411 objfile_name (objfile));
14412 return;
14413 }
14414
14415 /* Count parameters at the caller. */
14416
14417 nparams = 0;
14418 for (child_die = die->child; child_die && child_die->tag;
14419 child_die = sibling_die (child_die))
14420 {
14421 if (child_die->tag != DW_TAG_call_site_parameter
14422 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14423 {
14424 complaint (&symfile_complaints,
14425 _("Tag %d is not DW_TAG_call_site_parameter in "
14426 "DW_TAG_call_site child DIE %s [in module %s]"),
14427 child_die->tag, sect_offset_str (child_die->sect_off),
14428 objfile_name (objfile));
14429 continue;
14430 }
14431
14432 nparams++;
14433 }
14434
14435 call_site
14436 = ((struct call_site *)
14437 obstack_alloc (&objfile->objfile_obstack,
14438 sizeof (*call_site)
14439 + (sizeof (*call_site->parameter) * (nparams - 1))));
14440 *slot = call_site;
14441 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14442 call_site->pc = pc;
14443
14444 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14445 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14446 {
14447 struct die_info *func_die;
14448
14449 /* Skip also over DW_TAG_inlined_subroutine. */
14450 for (func_die = die->parent;
14451 func_die && func_die->tag != DW_TAG_subprogram
14452 && func_die->tag != DW_TAG_subroutine_type;
14453 func_die = func_die->parent);
14454
14455 /* DW_AT_call_all_calls is a superset
14456 of DW_AT_call_all_tail_calls. */
14457 if (func_die
14458 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14459 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14460 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14461 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14462 {
14463 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14464 not complete. But keep CALL_SITE for look ups via call_site_htab,
14465 both the initial caller containing the real return address PC and
14466 the final callee containing the current PC of a chain of tail
14467 calls do not need to have the tail call list complete. But any
14468 function candidate for a virtual tail call frame searched via
14469 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14470 determined unambiguously. */
14471 }
14472 else
14473 {
14474 struct type *func_type = NULL;
14475
14476 if (func_die)
14477 func_type = get_die_type (func_die, cu);
14478 if (func_type != NULL)
14479 {
14480 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14481
14482 /* Enlist this call site to the function. */
14483 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14484 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14485 }
14486 else
14487 complaint (&symfile_complaints,
14488 _("Cannot find function owning DW_TAG_call_site "
14489 "DIE %s [in module %s]"),
14490 sect_offset_str (die->sect_off), objfile_name (objfile));
14491 }
14492 }
14493
14494 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14495 if (attr == NULL)
14496 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14497 if (attr == NULL)
14498 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14499 if (attr == NULL)
14500 {
14501 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14502 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14503 }
14504 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14505 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14506 /* Keep NULL DWARF_BLOCK. */;
14507 else if (attr_form_is_block (attr))
14508 {
14509 struct dwarf2_locexpr_baton *dlbaton;
14510
14511 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14512 dlbaton->data = DW_BLOCK (attr)->data;
14513 dlbaton->size = DW_BLOCK (attr)->size;
14514 dlbaton->per_cu = cu->per_cu;
14515
14516 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14517 }
14518 else if (attr_form_is_ref (attr))
14519 {
14520 struct dwarf2_cu *target_cu = cu;
14521 struct die_info *target_die;
14522
14523 target_die = follow_die_ref (die, attr, &target_cu);
14524 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14525 if (die_is_declaration (target_die, target_cu))
14526 {
14527 const char *target_physname;
14528
14529 /* Prefer the mangled name; otherwise compute the demangled one. */
14530 target_physname = dw2_linkage_name (target_die, target_cu);
14531 if (target_physname == NULL)
14532 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14533 if (target_physname == NULL)
14534 complaint (&symfile_complaints,
14535 _("DW_AT_call_target target DIE has invalid "
14536 "physname, for referencing DIE %s [in module %s]"),
14537 sect_offset_str (die->sect_off), objfile_name (objfile));
14538 else
14539 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14540 }
14541 else
14542 {
14543 CORE_ADDR lowpc;
14544
14545 /* DW_AT_entry_pc should be preferred. */
14546 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14547 <= PC_BOUNDS_INVALID)
14548 complaint (&symfile_complaints,
14549 _("DW_AT_call_target target DIE has invalid "
14550 "low pc, for referencing DIE %s [in module %s]"),
14551 sect_offset_str (die->sect_off), objfile_name (objfile));
14552 else
14553 {
14554 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14555 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14556 }
14557 }
14558 }
14559 else
14560 complaint (&symfile_complaints,
14561 _("DW_TAG_call_site DW_AT_call_target is neither "
14562 "block nor reference, for DIE %s [in module %s]"),
14563 sect_offset_str (die->sect_off), objfile_name (objfile));
14564
14565 call_site->per_cu = cu->per_cu;
14566
14567 for (child_die = die->child;
14568 child_die && child_die->tag;
14569 child_die = sibling_die (child_die))
14570 {
14571 struct call_site_parameter *parameter;
14572 struct attribute *loc, *origin;
14573
14574 if (child_die->tag != DW_TAG_call_site_parameter
14575 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14576 {
14577 /* Already printed the complaint above. */
14578 continue;
14579 }
14580
14581 gdb_assert (call_site->parameter_count < nparams);
14582 parameter = &call_site->parameter[call_site->parameter_count];
14583
14584 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14585 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14586 register is contained in DW_AT_call_value. */
14587
14588 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14589 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14590 if (origin == NULL)
14591 {
14592 /* This was a pre-DWARF-5 GNU extension alias
14593 for DW_AT_call_parameter. */
14594 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14595 }
14596 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14597 {
14598 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14599
14600 sect_offset sect_off
14601 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14602 if (!offset_in_cu_p (&cu->header, sect_off))
14603 {
14604 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14605 binding can be done only inside one CU. Such referenced DIE
14606 therefore cannot be even moved to DW_TAG_partial_unit. */
14607 complaint (&symfile_complaints,
14608 _("DW_AT_call_parameter offset is not in CU for "
14609 "DW_TAG_call_site child DIE %s [in module %s]"),
14610 sect_offset_str (child_die->sect_off),
14611 objfile_name (objfile));
14612 continue;
14613 }
14614 parameter->u.param_cu_off
14615 = (cu_offset) (sect_off - cu->header.sect_off);
14616 }
14617 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14618 {
14619 complaint (&symfile_complaints,
14620 _("No DW_FORM_block* DW_AT_location for "
14621 "DW_TAG_call_site child DIE %s [in module %s]"),
14622 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14623 continue;
14624 }
14625 else
14626 {
14627 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14628 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14629 if (parameter->u.dwarf_reg != -1)
14630 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14631 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14632 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14633 &parameter->u.fb_offset))
14634 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14635 else
14636 {
14637 complaint (&symfile_complaints,
14638 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14639 "for DW_FORM_block* DW_AT_location is supported for "
14640 "DW_TAG_call_site child DIE %s "
14641 "[in module %s]"),
14642 sect_offset_str (child_die->sect_off),
14643 objfile_name (objfile));
14644 continue;
14645 }
14646 }
14647
14648 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14649 if (attr == NULL)
14650 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14651 if (!attr_form_is_block (attr))
14652 {
14653 complaint (&symfile_complaints,
14654 _("No DW_FORM_block* DW_AT_call_value for "
14655 "DW_TAG_call_site child DIE %s [in module %s]"),
14656 sect_offset_str (child_die->sect_off),
14657 objfile_name (objfile));
14658 continue;
14659 }
14660 parameter->value = DW_BLOCK (attr)->data;
14661 parameter->value_size = DW_BLOCK (attr)->size;
14662
14663 /* Parameters are not pre-cleared by memset above. */
14664 parameter->data_value = NULL;
14665 parameter->data_value_size = 0;
14666 call_site->parameter_count++;
14667
14668 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14669 if (attr == NULL)
14670 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14671 if (attr)
14672 {
14673 if (!attr_form_is_block (attr))
14674 complaint (&symfile_complaints,
14675 _("No DW_FORM_block* DW_AT_call_data_value for "
14676 "DW_TAG_call_site child DIE %s [in module %s]"),
14677 sect_offset_str (child_die->sect_off),
14678 objfile_name (objfile));
14679 else
14680 {
14681 parameter->data_value = DW_BLOCK (attr)->data;
14682 parameter->data_value_size = DW_BLOCK (attr)->size;
14683 }
14684 }
14685 }
14686 }
14687
14688 /* Helper function for read_variable. If DIE represents a virtual
14689 table, then return the type of the concrete object that is
14690 associated with the virtual table. Otherwise, return NULL. */
14691
14692 static struct type *
14693 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14694 {
14695 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14696 if (attr == NULL)
14697 return NULL;
14698
14699 /* Find the type DIE. */
14700 struct die_info *type_die = NULL;
14701 struct dwarf2_cu *type_cu = cu;
14702
14703 if (attr_form_is_ref (attr))
14704 type_die = follow_die_ref (die, attr, &type_cu);
14705 if (type_die == NULL)
14706 return NULL;
14707
14708 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14709 return NULL;
14710 return die_containing_type (type_die, type_cu);
14711 }
14712
14713 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14714
14715 static void
14716 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14717 {
14718 struct rust_vtable_symbol *storage = NULL;
14719
14720 if (cu->language == language_rust)
14721 {
14722 struct type *containing_type = rust_containing_type (die, cu);
14723
14724 if (containing_type != NULL)
14725 {
14726 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14727
14728 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14729 struct rust_vtable_symbol);
14730 initialize_objfile_symbol (storage);
14731 storage->concrete_type = containing_type;
14732 storage->subclass = SYMBOL_RUST_VTABLE;
14733 }
14734 }
14735
14736 new_symbol (die, NULL, cu, storage);
14737 }
14738
14739 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14740 reading .debug_rnglists.
14741 Callback's type should be:
14742 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14743 Return true if the attributes are present and valid, otherwise,
14744 return false. */
14745
14746 template <typename Callback>
14747 static bool
14748 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14749 Callback &&callback)
14750 {
14751 struct dwarf2_per_objfile *dwarf2_per_objfile
14752 = cu->per_cu->dwarf2_per_objfile;
14753 struct objfile *objfile = dwarf2_per_objfile->objfile;
14754 bfd *obfd = objfile->obfd;
14755 /* Base address selection entry. */
14756 CORE_ADDR base;
14757 int found_base;
14758 const gdb_byte *buffer;
14759 CORE_ADDR baseaddr;
14760 bool overflow = false;
14761
14762 found_base = cu->base_known;
14763 base = cu->base_address;
14764
14765 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14766 if (offset >= dwarf2_per_objfile->rnglists.size)
14767 {
14768 complaint (&symfile_complaints,
14769 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14770 offset);
14771 return false;
14772 }
14773 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14774
14775 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14776
14777 while (1)
14778 {
14779 /* Initialize it due to a false compiler warning. */
14780 CORE_ADDR range_beginning = 0, range_end = 0;
14781 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14782 + dwarf2_per_objfile->rnglists.size);
14783 unsigned int bytes_read;
14784
14785 if (buffer == buf_end)
14786 {
14787 overflow = true;
14788 break;
14789 }
14790 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14791 switch (rlet)
14792 {
14793 case DW_RLE_end_of_list:
14794 break;
14795 case DW_RLE_base_address:
14796 if (buffer + cu->header.addr_size > buf_end)
14797 {
14798 overflow = true;
14799 break;
14800 }
14801 base = read_address (obfd, buffer, cu, &bytes_read);
14802 found_base = 1;
14803 buffer += bytes_read;
14804 break;
14805 case DW_RLE_start_length:
14806 if (buffer + cu->header.addr_size > buf_end)
14807 {
14808 overflow = true;
14809 break;
14810 }
14811 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14812 buffer += bytes_read;
14813 range_end = (range_beginning
14814 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14815 buffer += bytes_read;
14816 if (buffer > buf_end)
14817 {
14818 overflow = true;
14819 break;
14820 }
14821 break;
14822 case DW_RLE_offset_pair:
14823 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14824 buffer += bytes_read;
14825 if (buffer > buf_end)
14826 {
14827 overflow = true;
14828 break;
14829 }
14830 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14831 buffer += bytes_read;
14832 if (buffer > buf_end)
14833 {
14834 overflow = true;
14835 break;
14836 }
14837 break;
14838 case DW_RLE_start_end:
14839 if (buffer + 2 * cu->header.addr_size > buf_end)
14840 {
14841 overflow = true;
14842 break;
14843 }
14844 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14845 buffer += bytes_read;
14846 range_end = read_address (obfd, buffer, cu, &bytes_read);
14847 buffer += bytes_read;
14848 break;
14849 default:
14850 complaint (&symfile_complaints,
14851 _("Invalid .debug_rnglists data (no base address)"));
14852 return false;
14853 }
14854 if (rlet == DW_RLE_end_of_list || overflow)
14855 break;
14856 if (rlet == DW_RLE_base_address)
14857 continue;
14858
14859 if (!found_base)
14860 {
14861 /* We have no valid base address for the ranges
14862 data. */
14863 complaint (&symfile_complaints,
14864 _("Invalid .debug_rnglists data (no base address)"));
14865 return false;
14866 }
14867
14868 if (range_beginning > range_end)
14869 {
14870 /* Inverted range entries are invalid. */
14871 complaint (&symfile_complaints,
14872 _("Invalid .debug_rnglists data (inverted range)"));
14873 return false;
14874 }
14875
14876 /* Empty range entries have no effect. */
14877 if (range_beginning == range_end)
14878 continue;
14879
14880 range_beginning += base;
14881 range_end += base;
14882
14883 /* A not-uncommon case of bad debug info.
14884 Don't pollute the addrmap with bad data. */
14885 if (range_beginning + baseaddr == 0
14886 && !dwarf2_per_objfile->has_section_at_zero)
14887 {
14888 complaint (&symfile_complaints,
14889 _(".debug_rnglists entry has start address of zero"
14890 " [in module %s]"), objfile_name (objfile));
14891 continue;
14892 }
14893
14894 callback (range_beginning, range_end);
14895 }
14896
14897 if (overflow)
14898 {
14899 complaint (&symfile_complaints,
14900 _("Offset %d is not terminated "
14901 "for DW_AT_ranges attribute"),
14902 offset);
14903 return false;
14904 }
14905
14906 return true;
14907 }
14908
14909 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14910 Callback's type should be:
14911 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14912 Return 1 if the attributes are present and valid, otherwise, return 0. */
14913
14914 template <typename Callback>
14915 static int
14916 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14917 Callback &&callback)
14918 {
14919 struct dwarf2_per_objfile *dwarf2_per_objfile
14920 = cu->per_cu->dwarf2_per_objfile;
14921 struct objfile *objfile = dwarf2_per_objfile->objfile;
14922 struct comp_unit_head *cu_header = &cu->header;
14923 bfd *obfd = objfile->obfd;
14924 unsigned int addr_size = cu_header->addr_size;
14925 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14926 /* Base address selection entry. */
14927 CORE_ADDR base;
14928 int found_base;
14929 unsigned int dummy;
14930 const gdb_byte *buffer;
14931 CORE_ADDR baseaddr;
14932
14933 if (cu_header->version >= 5)
14934 return dwarf2_rnglists_process (offset, cu, callback);
14935
14936 found_base = cu->base_known;
14937 base = cu->base_address;
14938
14939 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14940 if (offset >= dwarf2_per_objfile->ranges.size)
14941 {
14942 complaint (&symfile_complaints,
14943 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14944 offset);
14945 return 0;
14946 }
14947 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14948
14949 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14950
14951 while (1)
14952 {
14953 CORE_ADDR range_beginning, range_end;
14954
14955 range_beginning = read_address (obfd, buffer, cu, &dummy);
14956 buffer += addr_size;
14957 range_end = read_address (obfd, buffer, cu, &dummy);
14958 buffer += addr_size;
14959 offset += 2 * addr_size;
14960
14961 /* An end of list marker is a pair of zero addresses. */
14962 if (range_beginning == 0 && range_end == 0)
14963 /* Found the end of list entry. */
14964 break;
14965
14966 /* Each base address selection entry is a pair of 2 values.
14967 The first is the largest possible address, the second is
14968 the base address. Check for a base address here. */
14969 if ((range_beginning & mask) == mask)
14970 {
14971 /* If we found the largest possible address, then we already
14972 have the base address in range_end. */
14973 base = range_end;
14974 found_base = 1;
14975 continue;
14976 }
14977
14978 if (!found_base)
14979 {
14980 /* We have no valid base address for the ranges
14981 data. */
14982 complaint (&symfile_complaints,
14983 _("Invalid .debug_ranges data (no base address)"));
14984 return 0;
14985 }
14986
14987 if (range_beginning > range_end)
14988 {
14989 /* Inverted range entries are invalid. */
14990 complaint (&symfile_complaints,
14991 _("Invalid .debug_ranges data (inverted range)"));
14992 return 0;
14993 }
14994
14995 /* Empty range entries have no effect. */
14996 if (range_beginning == range_end)
14997 continue;
14998
14999 range_beginning += base;
15000 range_end += base;
15001
15002 /* A not-uncommon case of bad debug info.
15003 Don't pollute the addrmap with bad data. */
15004 if (range_beginning + baseaddr == 0
15005 && !dwarf2_per_objfile->has_section_at_zero)
15006 {
15007 complaint (&symfile_complaints,
15008 _(".debug_ranges entry has start address of zero"
15009 " [in module %s]"), objfile_name (objfile));
15010 continue;
15011 }
15012
15013 callback (range_beginning, range_end);
15014 }
15015
15016 return 1;
15017 }
15018
15019 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
15020 Return 1 if the attributes are present and valid, otherwise, return 0.
15021 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
15022
15023 static int
15024 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
15025 CORE_ADDR *high_return, struct dwarf2_cu *cu,
15026 struct partial_symtab *ranges_pst)
15027 {
15028 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15029 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15030 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
15031 SECT_OFF_TEXT (objfile));
15032 int low_set = 0;
15033 CORE_ADDR low = 0;
15034 CORE_ADDR high = 0;
15035 int retval;
15036
15037 retval = dwarf2_ranges_process (offset, cu,
15038 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
15039 {
15040 if (ranges_pst != NULL)
15041 {
15042 CORE_ADDR lowpc;
15043 CORE_ADDR highpc;
15044
15045 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
15046 range_beginning + baseaddr);
15047 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
15048 range_end + baseaddr);
15049 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
15050 ranges_pst);
15051 }
15052
15053 /* FIXME: This is recording everything as a low-high
15054 segment of consecutive addresses. We should have a
15055 data structure for discontiguous block ranges
15056 instead. */
15057 if (! low_set)
15058 {
15059 low = range_beginning;
15060 high = range_end;
15061 low_set = 1;
15062 }
15063 else
15064 {
15065 if (range_beginning < low)
15066 low = range_beginning;
15067 if (range_end > high)
15068 high = range_end;
15069 }
15070 });
15071 if (!retval)
15072 return 0;
15073
15074 if (! low_set)
15075 /* If the first entry is an end-of-list marker, the range
15076 describes an empty scope, i.e. no instructions. */
15077 return 0;
15078
15079 if (low_return)
15080 *low_return = low;
15081 if (high_return)
15082 *high_return = high;
15083 return 1;
15084 }
15085
15086 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
15087 definition for the return value. *LOWPC and *HIGHPC are set iff
15088 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
15089
15090 static enum pc_bounds_kind
15091 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
15092 CORE_ADDR *highpc, struct dwarf2_cu *cu,
15093 struct partial_symtab *pst)
15094 {
15095 struct dwarf2_per_objfile *dwarf2_per_objfile
15096 = cu->per_cu->dwarf2_per_objfile;
15097 struct attribute *attr;
15098 struct attribute *attr_high;
15099 CORE_ADDR low = 0;
15100 CORE_ADDR high = 0;
15101 enum pc_bounds_kind ret;
15102
15103 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
15104 if (attr_high)
15105 {
15106 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15107 if (attr)
15108 {
15109 low = attr_value_as_address (attr);
15110 high = attr_value_as_address (attr_high);
15111 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
15112 high += low;
15113 }
15114 else
15115 /* Found high w/o low attribute. */
15116 return PC_BOUNDS_INVALID;
15117
15118 /* Found consecutive range of addresses. */
15119 ret = PC_BOUNDS_HIGH_LOW;
15120 }
15121 else
15122 {
15123 attr = dwarf2_attr (die, DW_AT_ranges, cu);
15124 if (attr != NULL)
15125 {
15126 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
15127 We take advantage of the fact that DW_AT_ranges does not appear
15128 in DW_TAG_compile_unit of DWO files. */
15129 int need_ranges_base = die->tag != DW_TAG_compile_unit;
15130 unsigned int ranges_offset = (DW_UNSND (attr)
15131 + (need_ranges_base
15132 ? cu->ranges_base
15133 : 0));
15134
15135 /* Value of the DW_AT_ranges attribute is the offset in the
15136 .debug_ranges section. */
15137 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
15138 return PC_BOUNDS_INVALID;
15139 /* Found discontinuous range of addresses. */
15140 ret = PC_BOUNDS_RANGES;
15141 }
15142 else
15143 return PC_BOUNDS_NOT_PRESENT;
15144 }
15145
15146 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
15147 if (high <= low)
15148 return PC_BOUNDS_INVALID;
15149
15150 /* When using the GNU linker, .gnu.linkonce. sections are used to
15151 eliminate duplicate copies of functions and vtables and such.
15152 The linker will arbitrarily choose one and discard the others.
15153 The AT_*_pc values for such functions refer to local labels in
15154 these sections. If the section from that file was discarded, the
15155 labels are not in the output, so the relocs get a value of 0.
15156 If this is a discarded function, mark the pc bounds as invalid,
15157 so that GDB will ignore it. */
15158 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
15159 return PC_BOUNDS_INVALID;
15160
15161 *lowpc = low;
15162 if (highpc)
15163 *highpc = high;
15164 return ret;
15165 }
15166
15167 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
15168 its low and high PC addresses. Do nothing if these addresses could not
15169 be determined. Otherwise, set LOWPC to the low address if it is smaller,
15170 and HIGHPC to the high address if greater than HIGHPC. */
15171
15172 static void
15173 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
15174 CORE_ADDR *lowpc, CORE_ADDR *highpc,
15175 struct dwarf2_cu *cu)
15176 {
15177 CORE_ADDR low, high;
15178 struct die_info *child = die->child;
15179
15180 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
15181 {
15182 *lowpc = std::min (*lowpc, low);
15183 *highpc = std::max (*highpc, high);
15184 }
15185
15186 /* If the language does not allow nested subprograms (either inside
15187 subprograms or lexical blocks), we're done. */
15188 if (cu->language != language_ada)
15189 return;
15190
15191 /* Check all the children of the given DIE. If it contains nested
15192 subprograms, then check their pc bounds. Likewise, we need to
15193 check lexical blocks as well, as they may also contain subprogram
15194 definitions. */
15195 while (child && child->tag)
15196 {
15197 if (child->tag == DW_TAG_subprogram
15198 || child->tag == DW_TAG_lexical_block)
15199 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
15200 child = sibling_die (child);
15201 }
15202 }
15203
15204 /* Get the low and high pc's represented by the scope DIE, and store
15205 them in *LOWPC and *HIGHPC. If the correct values can't be
15206 determined, set *LOWPC to -1 and *HIGHPC to 0. */
15207
15208 static void
15209 get_scope_pc_bounds (struct die_info *die,
15210 CORE_ADDR *lowpc, CORE_ADDR *highpc,
15211 struct dwarf2_cu *cu)
15212 {
15213 CORE_ADDR best_low = (CORE_ADDR) -1;
15214 CORE_ADDR best_high = (CORE_ADDR) 0;
15215 CORE_ADDR current_low, current_high;
15216
15217 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
15218 >= PC_BOUNDS_RANGES)
15219 {
15220 best_low = current_low;
15221 best_high = current_high;
15222 }
15223 else
15224 {
15225 struct die_info *child = die->child;
15226
15227 while (child && child->tag)
15228 {
15229 switch (child->tag) {
15230 case DW_TAG_subprogram:
15231 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
15232 break;
15233 case DW_TAG_namespace:
15234 case DW_TAG_module:
15235 /* FIXME: carlton/2004-01-16: Should we do this for
15236 DW_TAG_class_type/DW_TAG_structure_type, too? I think
15237 that current GCC's always emit the DIEs corresponding
15238 to definitions of methods of classes as children of a
15239 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
15240 the DIEs giving the declarations, which could be
15241 anywhere). But I don't see any reason why the
15242 standards says that they have to be there. */
15243 get_scope_pc_bounds (child, &current_low, &current_high, cu);
15244
15245 if (current_low != ((CORE_ADDR) -1))
15246 {
15247 best_low = std::min (best_low, current_low);
15248 best_high = std::max (best_high, current_high);
15249 }
15250 break;
15251 default:
15252 /* Ignore. */
15253 break;
15254 }
15255
15256 child = sibling_die (child);
15257 }
15258 }
15259
15260 *lowpc = best_low;
15261 *highpc = best_high;
15262 }
15263
15264 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
15265 in DIE. */
15266
15267 static void
15268 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
15269 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
15270 {
15271 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15272 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15273 struct attribute *attr;
15274 struct attribute *attr_high;
15275
15276 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
15277 if (attr_high)
15278 {
15279 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15280 if (attr)
15281 {
15282 CORE_ADDR low = attr_value_as_address (attr);
15283 CORE_ADDR high = attr_value_as_address (attr_high);
15284
15285 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
15286 high += low;
15287
15288 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
15289 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
15290 record_block_range (block, low, high - 1);
15291 }
15292 }
15293
15294 attr = dwarf2_attr (die, DW_AT_ranges, cu);
15295 if (attr)
15296 {
15297 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
15298 We take advantage of the fact that DW_AT_ranges does not appear
15299 in DW_TAG_compile_unit of DWO files. */
15300 int need_ranges_base = die->tag != DW_TAG_compile_unit;
15301
15302 /* The value of the DW_AT_ranges attribute is the offset of the
15303 address range list in the .debug_ranges section. */
15304 unsigned long offset = (DW_UNSND (attr)
15305 + (need_ranges_base ? cu->ranges_base : 0));
15306 const gdb_byte *buffer;
15307
15308 /* For some target architectures, but not others, the
15309 read_address function sign-extends the addresses it returns.
15310 To recognize base address selection entries, we need a
15311 mask. */
15312 unsigned int addr_size = cu->header.addr_size;
15313 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
15314
15315 /* The base address, to which the next pair is relative. Note
15316 that this 'base' is a DWARF concept: most entries in a range
15317 list are relative, to reduce the number of relocs against the
15318 debugging information. This is separate from this function's
15319 'baseaddr' argument, which GDB uses to relocate debugging
15320 information from a shared library based on the address at
15321 which the library was loaded. */
15322 CORE_ADDR base = cu->base_address;
15323 int base_known = cu->base_known;
15324
15325 dwarf2_ranges_process (offset, cu,
15326 [&] (CORE_ADDR start, CORE_ADDR end)
15327 {
15328 start += baseaddr;
15329 end += baseaddr;
15330 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
15331 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
15332 record_block_range (block, start, end - 1);
15333 });
15334 }
15335 }
15336
15337 /* Check whether the producer field indicates either of GCC < 4.6, or the
15338 Intel C/C++ compiler, and cache the result in CU. */
15339
15340 static void
15341 check_producer (struct dwarf2_cu *cu)
15342 {
15343 int major, minor;
15344
15345 if (cu->producer == NULL)
15346 {
15347 /* For unknown compilers expect their behavior is DWARF version
15348 compliant.
15349
15350 GCC started to support .debug_types sections by -gdwarf-4 since
15351 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
15352 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
15353 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
15354 interpreted incorrectly by GDB now - GCC PR debug/48229. */
15355 }
15356 else if (producer_is_gcc (cu->producer, &major, &minor))
15357 {
15358 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
15359 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
15360 }
15361 else if (producer_is_icc (cu->producer, &major, &minor))
15362 cu->producer_is_icc_lt_14 = major < 14;
15363 else
15364 {
15365 /* For other non-GCC compilers, expect their behavior is DWARF version
15366 compliant. */
15367 }
15368
15369 cu->checked_producer = 1;
15370 }
15371
15372 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
15373 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
15374 during 4.6.0 experimental. */
15375
15376 static int
15377 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
15378 {
15379 if (!cu->checked_producer)
15380 check_producer (cu);
15381
15382 return cu->producer_is_gxx_lt_4_6;
15383 }
15384
15385 /* Return the default accessibility type if it is not overriden by
15386 DW_AT_accessibility. */
15387
15388 static enum dwarf_access_attribute
15389 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15390 {
15391 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15392 {
15393 /* The default DWARF 2 accessibility for members is public, the default
15394 accessibility for inheritance is private. */
15395
15396 if (die->tag != DW_TAG_inheritance)
15397 return DW_ACCESS_public;
15398 else
15399 return DW_ACCESS_private;
15400 }
15401 else
15402 {
15403 /* DWARF 3+ defines the default accessibility a different way. The same
15404 rules apply now for DW_TAG_inheritance as for the members and it only
15405 depends on the container kind. */
15406
15407 if (die->parent->tag == DW_TAG_class_type)
15408 return DW_ACCESS_private;
15409 else
15410 return DW_ACCESS_public;
15411 }
15412 }
15413
15414 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15415 offset. If the attribute was not found return 0, otherwise return
15416 1. If it was found but could not properly be handled, set *OFFSET
15417 to 0. */
15418
15419 static int
15420 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15421 LONGEST *offset)
15422 {
15423 struct attribute *attr;
15424
15425 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15426 if (attr != NULL)
15427 {
15428 *offset = 0;
15429
15430 /* Note that we do not check for a section offset first here.
15431 This is because DW_AT_data_member_location is new in DWARF 4,
15432 so if we see it, we can assume that a constant form is really
15433 a constant and not a section offset. */
15434 if (attr_form_is_constant (attr))
15435 *offset = dwarf2_get_attr_constant_value (attr, 0);
15436 else if (attr_form_is_section_offset (attr))
15437 dwarf2_complex_location_expr_complaint ();
15438 else if (attr_form_is_block (attr))
15439 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15440 else
15441 dwarf2_complex_location_expr_complaint ();
15442
15443 return 1;
15444 }
15445
15446 return 0;
15447 }
15448
15449 /* Add an aggregate field to the field list. */
15450
15451 static void
15452 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15453 struct dwarf2_cu *cu)
15454 {
15455 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15456 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15457 struct nextfield *new_field;
15458 struct attribute *attr;
15459 struct field *fp;
15460 const char *fieldname = "";
15461
15462 if (die->tag == DW_TAG_inheritance)
15463 {
15464 fip->baseclasses.emplace_back ();
15465 new_field = &fip->baseclasses.back ();
15466 }
15467 else
15468 {
15469 fip->fields.emplace_back ();
15470 new_field = &fip->fields.back ();
15471 }
15472
15473 fip->nfields++;
15474
15475 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15476 if (attr)
15477 new_field->accessibility = DW_UNSND (attr);
15478 else
15479 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15480 if (new_field->accessibility != DW_ACCESS_public)
15481 fip->non_public_fields = 1;
15482
15483 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15484 if (attr)
15485 new_field->virtuality = DW_UNSND (attr);
15486 else
15487 new_field->virtuality = DW_VIRTUALITY_none;
15488
15489 fp = &new_field->field;
15490
15491 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15492 {
15493 LONGEST offset;
15494
15495 /* Data member other than a C++ static data member. */
15496
15497 /* Get type of field. */
15498 fp->type = die_type (die, cu);
15499
15500 SET_FIELD_BITPOS (*fp, 0);
15501
15502 /* Get bit size of field (zero if none). */
15503 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15504 if (attr)
15505 {
15506 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15507 }
15508 else
15509 {
15510 FIELD_BITSIZE (*fp) = 0;
15511 }
15512
15513 /* Get bit offset of field. */
15514 if (handle_data_member_location (die, cu, &offset))
15515 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15516 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15517 if (attr)
15518 {
15519 if (gdbarch_bits_big_endian (gdbarch))
15520 {
15521 /* For big endian bits, the DW_AT_bit_offset gives the
15522 additional bit offset from the MSB of the containing
15523 anonymous object to the MSB of the field. We don't
15524 have to do anything special since we don't need to
15525 know the size of the anonymous object. */
15526 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15527 }
15528 else
15529 {
15530 /* For little endian bits, compute the bit offset to the
15531 MSB of the anonymous object, subtract off the number of
15532 bits from the MSB of the field to the MSB of the
15533 object, and then subtract off the number of bits of
15534 the field itself. The result is the bit offset of
15535 the LSB of the field. */
15536 int anonymous_size;
15537 int bit_offset = DW_UNSND (attr);
15538
15539 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15540 if (attr)
15541 {
15542 /* The size of the anonymous object containing
15543 the bit field is explicit, so use the
15544 indicated size (in bytes). */
15545 anonymous_size = DW_UNSND (attr);
15546 }
15547 else
15548 {
15549 /* The size of the anonymous object containing
15550 the bit field must be inferred from the type
15551 attribute of the data member containing the
15552 bit field. */
15553 anonymous_size = TYPE_LENGTH (fp->type);
15554 }
15555 SET_FIELD_BITPOS (*fp,
15556 (FIELD_BITPOS (*fp)
15557 + anonymous_size * bits_per_byte
15558 - bit_offset - FIELD_BITSIZE (*fp)));
15559 }
15560 }
15561 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15562 if (attr != NULL)
15563 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15564 + dwarf2_get_attr_constant_value (attr, 0)));
15565
15566 /* Get name of field. */
15567 fieldname = dwarf2_name (die, cu);
15568 if (fieldname == NULL)
15569 fieldname = "";
15570
15571 /* The name is already allocated along with this objfile, so we don't
15572 need to duplicate it for the type. */
15573 fp->name = fieldname;
15574
15575 /* Change accessibility for artificial fields (e.g. virtual table
15576 pointer or virtual base class pointer) to private. */
15577 if (dwarf2_attr (die, DW_AT_artificial, cu))
15578 {
15579 FIELD_ARTIFICIAL (*fp) = 1;
15580 new_field->accessibility = DW_ACCESS_private;
15581 fip->non_public_fields = 1;
15582 }
15583 }
15584 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15585 {
15586 /* C++ static member. */
15587
15588 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15589 is a declaration, but all versions of G++ as of this writing
15590 (so through at least 3.2.1) incorrectly generate
15591 DW_TAG_variable tags. */
15592
15593 const char *physname;
15594
15595 /* Get name of field. */
15596 fieldname = dwarf2_name (die, cu);
15597 if (fieldname == NULL)
15598 return;
15599
15600 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15601 if (attr
15602 /* Only create a symbol if this is an external value.
15603 new_symbol checks this and puts the value in the global symbol
15604 table, which we want. If it is not external, new_symbol
15605 will try to put the value in cu->list_in_scope which is wrong. */
15606 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15607 {
15608 /* A static const member, not much different than an enum as far as
15609 we're concerned, except that we can support more types. */
15610 new_symbol (die, NULL, cu);
15611 }
15612
15613 /* Get physical name. */
15614 physname = dwarf2_physname (fieldname, die, cu);
15615
15616 /* The name is already allocated along with this objfile, so we don't
15617 need to duplicate it for the type. */
15618 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15619 FIELD_TYPE (*fp) = die_type (die, cu);
15620 FIELD_NAME (*fp) = fieldname;
15621 }
15622 else if (die->tag == DW_TAG_inheritance)
15623 {
15624 LONGEST offset;
15625
15626 /* C++ base class field. */
15627 if (handle_data_member_location (die, cu, &offset))
15628 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15629 FIELD_BITSIZE (*fp) = 0;
15630 FIELD_TYPE (*fp) = die_type (die, cu);
15631 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15632 }
15633 else if (die->tag == DW_TAG_variant_part)
15634 {
15635 /* process_structure_scope will treat this DIE as a union. */
15636 process_structure_scope (die, cu);
15637
15638 /* The variant part is relative to the start of the enclosing
15639 structure. */
15640 SET_FIELD_BITPOS (*fp, 0);
15641 fp->type = get_die_type (die, cu);
15642 fp->artificial = 1;
15643 fp->name = "<<variant>>";
15644 }
15645 else
15646 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15647 }
15648
15649 /* Can the type given by DIE define another type? */
15650
15651 static bool
15652 type_can_define_types (const struct die_info *die)
15653 {
15654 switch (die->tag)
15655 {
15656 case DW_TAG_typedef:
15657 case DW_TAG_class_type:
15658 case DW_TAG_structure_type:
15659 case DW_TAG_union_type:
15660 case DW_TAG_enumeration_type:
15661 return true;
15662
15663 default:
15664 return false;
15665 }
15666 }
15667
15668 /* Add a type definition defined in the scope of the FIP's class. */
15669
15670 static void
15671 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15672 struct dwarf2_cu *cu)
15673 {
15674 struct decl_field fp;
15675 memset (&fp, 0, sizeof (fp));
15676
15677 gdb_assert (type_can_define_types (die));
15678
15679 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15680 fp.name = dwarf2_name (die, cu);
15681 fp.type = read_type_die (die, cu);
15682
15683 /* Save accessibility. */
15684 enum dwarf_access_attribute accessibility;
15685 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15686 if (attr != NULL)
15687 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15688 else
15689 accessibility = dwarf2_default_access_attribute (die, cu);
15690 switch (accessibility)
15691 {
15692 case DW_ACCESS_public:
15693 /* The assumed value if neither private nor protected. */
15694 break;
15695 case DW_ACCESS_private:
15696 fp.is_private = 1;
15697 break;
15698 case DW_ACCESS_protected:
15699 fp.is_protected = 1;
15700 break;
15701 default:
15702 complaint (&symfile_complaints,
15703 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15704 }
15705
15706 if (die->tag == DW_TAG_typedef)
15707 fip->typedef_field_list.push_back (fp);
15708 else
15709 fip->nested_types_list.push_back (fp);
15710 }
15711
15712 /* Create the vector of fields, and attach it to the type. */
15713
15714 static void
15715 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15716 struct dwarf2_cu *cu)
15717 {
15718 int nfields = fip->nfields;
15719
15720 /* Record the field count, allocate space for the array of fields,
15721 and create blank accessibility bitfields if necessary. */
15722 TYPE_NFIELDS (type) = nfields;
15723 TYPE_FIELDS (type) = (struct field *)
15724 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15725
15726 if (fip->non_public_fields && cu->language != language_ada)
15727 {
15728 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15729
15730 TYPE_FIELD_PRIVATE_BITS (type) =
15731 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15732 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15733
15734 TYPE_FIELD_PROTECTED_BITS (type) =
15735 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15736 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15737
15738 TYPE_FIELD_IGNORE_BITS (type) =
15739 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15740 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15741 }
15742
15743 /* If the type has baseclasses, allocate and clear a bit vector for
15744 TYPE_FIELD_VIRTUAL_BITS. */
15745 if (!fip->baseclasses.empty () && cu->language != language_ada)
15746 {
15747 int num_bytes = B_BYTES (fip->baseclasses.size ());
15748 unsigned char *pointer;
15749
15750 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15751 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15752 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15753 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15754 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15755 }
15756
15757 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15758 {
15759 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15760
15761 for (int index = 0; index < nfields; ++index)
15762 {
15763 struct nextfield &field = fip->fields[index];
15764
15765 if (field.variant.is_discriminant)
15766 di->discriminant_index = index;
15767 else if (field.variant.default_branch)
15768 di->default_index = index;
15769 else
15770 di->discriminants[index] = field.variant.discriminant_value;
15771 }
15772 }
15773
15774 /* Copy the saved-up fields into the field vector. */
15775 for (int i = 0; i < nfields; ++i)
15776 {
15777 struct nextfield &field
15778 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15779 : fip->fields[i - fip->baseclasses.size ()]);
15780
15781 TYPE_FIELD (type, i) = field.field;
15782 switch (field.accessibility)
15783 {
15784 case DW_ACCESS_private:
15785 if (cu->language != language_ada)
15786 SET_TYPE_FIELD_PRIVATE (type, i);
15787 break;
15788
15789 case DW_ACCESS_protected:
15790 if (cu->language != language_ada)
15791 SET_TYPE_FIELD_PROTECTED (type, i);
15792 break;
15793
15794 case DW_ACCESS_public:
15795 break;
15796
15797 default:
15798 /* Unknown accessibility. Complain and treat it as public. */
15799 {
15800 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15801 field.accessibility);
15802 }
15803 break;
15804 }
15805 if (i < fip->baseclasses.size ())
15806 {
15807 switch (field.virtuality)
15808 {
15809 case DW_VIRTUALITY_virtual:
15810 case DW_VIRTUALITY_pure_virtual:
15811 if (cu->language == language_ada)
15812 error (_("unexpected virtuality in component of Ada type"));
15813 SET_TYPE_FIELD_VIRTUAL (type, i);
15814 break;
15815 }
15816 }
15817 }
15818 }
15819
15820 /* Return true if this member function is a constructor, false
15821 otherwise. */
15822
15823 static int
15824 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15825 {
15826 const char *fieldname;
15827 const char *type_name;
15828 int len;
15829
15830 if (die->parent == NULL)
15831 return 0;
15832
15833 if (die->parent->tag != DW_TAG_structure_type
15834 && die->parent->tag != DW_TAG_union_type
15835 && die->parent->tag != DW_TAG_class_type)
15836 return 0;
15837
15838 fieldname = dwarf2_name (die, cu);
15839 type_name = dwarf2_name (die->parent, cu);
15840 if (fieldname == NULL || type_name == NULL)
15841 return 0;
15842
15843 len = strlen (fieldname);
15844 return (strncmp (fieldname, type_name, len) == 0
15845 && (type_name[len] == '\0' || type_name[len] == '<'));
15846 }
15847
15848 /* Add a member function to the proper fieldlist. */
15849
15850 static void
15851 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15852 struct type *type, struct dwarf2_cu *cu)
15853 {
15854 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15855 struct attribute *attr;
15856 int i;
15857 struct fnfieldlist *flp = nullptr;
15858 struct fn_field *fnp;
15859 const char *fieldname;
15860 struct type *this_type;
15861 enum dwarf_access_attribute accessibility;
15862
15863 if (cu->language == language_ada)
15864 error (_("unexpected member function in Ada type"));
15865
15866 /* Get name of member function. */
15867 fieldname = dwarf2_name (die, cu);
15868 if (fieldname == NULL)
15869 return;
15870
15871 /* Look up member function name in fieldlist. */
15872 for (i = 0; i < fip->fnfieldlists.size (); i++)
15873 {
15874 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15875 {
15876 flp = &fip->fnfieldlists[i];
15877 break;
15878 }
15879 }
15880
15881 /* Create a new fnfieldlist if necessary. */
15882 if (flp == nullptr)
15883 {
15884 fip->fnfieldlists.emplace_back ();
15885 flp = &fip->fnfieldlists.back ();
15886 flp->name = fieldname;
15887 i = fip->fnfieldlists.size () - 1;
15888 }
15889
15890 /* Create a new member function field and add it to the vector of
15891 fnfieldlists. */
15892 flp->fnfields.emplace_back ();
15893 fnp = &flp->fnfields.back ();
15894
15895 /* Delay processing of the physname until later. */
15896 if (cu->language == language_cplus)
15897 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15898 die, cu);
15899 else
15900 {
15901 const char *physname = dwarf2_physname (fieldname, die, cu);
15902 fnp->physname = physname ? physname : "";
15903 }
15904
15905 fnp->type = alloc_type (objfile);
15906 this_type = read_type_die (die, cu);
15907 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15908 {
15909 int nparams = TYPE_NFIELDS (this_type);
15910
15911 /* TYPE is the domain of this method, and THIS_TYPE is the type
15912 of the method itself (TYPE_CODE_METHOD). */
15913 smash_to_method_type (fnp->type, type,
15914 TYPE_TARGET_TYPE (this_type),
15915 TYPE_FIELDS (this_type),
15916 TYPE_NFIELDS (this_type),
15917 TYPE_VARARGS (this_type));
15918
15919 /* Handle static member functions.
15920 Dwarf2 has no clean way to discern C++ static and non-static
15921 member functions. G++ helps GDB by marking the first
15922 parameter for non-static member functions (which is the this
15923 pointer) as artificial. We obtain this information from
15924 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15925 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15926 fnp->voffset = VOFFSET_STATIC;
15927 }
15928 else
15929 complaint (&symfile_complaints, _("member function type missing for '%s'"),
15930 dwarf2_full_name (fieldname, die, cu));
15931
15932 /* Get fcontext from DW_AT_containing_type if present. */
15933 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15934 fnp->fcontext = die_containing_type (die, cu);
15935
15936 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15937 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15938
15939 /* Get accessibility. */
15940 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15941 if (attr)
15942 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15943 else
15944 accessibility = dwarf2_default_access_attribute (die, cu);
15945 switch (accessibility)
15946 {
15947 case DW_ACCESS_private:
15948 fnp->is_private = 1;
15949 break;
15950 case DW_ACCESS_protected:
15951 fnp->is_protected = 1;
15952 break;
15953 }
15954
15955 /* Check for artificial methods. */
15956 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15957 if (attr && DW_UNSND (attr) != 0)
15958 fnp->is_artificial = 1;
15959
15960 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15961
15962 /* Get index in virtual function table if it is a virtual member
15963 function. For older versions of GCC, this is an offset in the
15964 appropriate virtual table, as specified by DW_AT_containing_type.
15965 For everyone else, it is an expression to be evaluated relative
15966 to the object address. */
15967
15968 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15969 if (attr)
15970 {
15971 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15972 {
15973 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15974 {
15975 /* Old-style GCC. */
15976 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15977 }
15978 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15979 || (DW_BLOCK (attr)->size > 1
15980 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15981 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15982 {
15983 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15984 if ((fnp->voffset % cu->header.addr_size) != 0)
15985 dwarf2_complex_location_expr_complaint ();
15986 else
15987 fnp->voffset /= cu->header.addr_size;
15988 fnp->voffset += 2;
15989 }
15990 else
15991 dwarf2_complex_location_expr_complaint ();
15992
15993 if (!fnp->fcontext)
15994 {
15995 /* If there is no `this' field and no DW_AT_containing_type,
15996 we cannot actually find a base class context for the
15997 vtable! */
15998 if (TYPE_NFIELDS (this_type) == 0
15999 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
16000 {
16001 complaint (&symfile_complaints,
16002 _("cannot determine context for virtual member "
16003 "function \"%s\" (offset %s)"),
16004 fieldname, sect_offset_str (die->sect_off));
16005 }
16006 else
16007 {
16008 fnp->fcontext
16009 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
16010 }
16011 }
16012 }
16013 else if (attr_form_is_section_offset (attr))
16014 {
16015 dwarf2_complex_location_expr_complaint ();
16016 }
16017 else
16018 {
16019 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
16020 fieldname);
16021 }
16022 }
16023 else
16024 {
16025 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
16026 if (attr && DW_UNSND (attr))
16027 {
16028 /* GCC does this, as of 2008-08-25; PR debug/37237. */
16029 complaint (&symfile_complaints,
16030 _("Member function \"%s\" (offset %s) is virtual "
16031 "but the vtable offset is not specified"),
16032 fieldname, sect_offset_str (die->sect_off));
16033 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16034 TYPE_CPLUS_DYNAMIC (type) = 1;
16035 }
16036 }
16037 }
16038
16039 /* Create the vector of member function fields, and attach it to the type. */
16040
16041 static void
16042 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
16043 struct dwarf2_cu *cu)
16044 {
16045 if (cu->language == language_ada)
16046 error (_("unexpected member functions in Ada type"));
16047
16048 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16049 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
16050 TYPE_ALLOC (type,
16051 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
16052
16053 for (int i = 0; i < fip->fnfieldlists.size (); i++)
16054 {
16055 struct fnfieldlist &nf = fip->fnfieldlists[i];
16056 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
16057
16058 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
16059 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
16060 fn_flp->fn_fields = (struct fn_field *)
16061 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
16062
16063 for (int k = 0; k < nf.fnfields.size (); ++k)
16064 fn_flp->fn_fields[k] = nf.fnfields[k];
16065 }
16066
16067 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
16068 }
16069
16070 /* Returns non-zero if NAME is the name of a vtable member in CU's
16071 language, zero otherwise. */
16072 static int
16073 is_vtable_name (const char *name, struct dwarf2_cu *cu)
16074 {
16075 static const char vptr[] = "_vptr";
16076
16077 /* Look for the C++ form of the vtable. */
16078 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
16079 return 1;
16080
16081 return 0;
16082 }
16083
16084 /* GCC outputs unnamed structures that are really pointers to member
16085 functions, with the ABI-specified layout. If TYPE describes
16086 such a structure, smash it into a member function type.
16087
16088 GCC shouldn't do this; it should just output pointer to member DIEs.
16089 This is GCC PR debug/28767. */
16090
16091 static void
16092 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
16093 {
16094 struct type *pfn_type, *self_type, *new_type;
16095
16096 /* Check for a structure with no name and two children. */
16097 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
16098 return;
16099
16100 /* Check for __pfn and __delta members. */
16101 if (TYPE_FIELD_NAME (type, 0) == NULL
16102 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
16103 || TYPE_FIELD_NAME (type, 1) == NULL
16104 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
16105 return;
16106
16107 /* Find the type of the method. */
16108 pfn_type = TYPE_FIELD_TYPE (type, 0);
16109 if (pfn_type == NULL
16110 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
16111 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
16112 return;
16113
16114 /* Look for the "this" argument. */
16115 pfn_type = TYPE_TARGET_TYPE (pfn_type);
16116 if (TYPE_NFIELDS (pfn_type) == 0
16117 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
16118 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
16119 return;
16120
16121 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
16122 new_type = alloc_type (objfile);
16123 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
16124 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
16125 TYPE_VARARGS (pfn_type));
16126 smash_to_methodptr_type (type, new_type);
16127 }
16128
16129
16130 /* Called when we find the DIE that starts a structure or union scope
16131 (definition) to create a type for the structure or union. Fill in
16132 the type's name and general properties; the members will not be
16133 processed until process_structure_scope. A symbol table entry for
16134 the type will also not be done until process_structure_scope (assuming
16135 the type has a name).
16136
16137 NOTE: we need to call these functions regardless of whether or not the
16138 DIE has a DW_AT_name attribute, since it might be an anonymous
16139 structure or union. This gets the type entered into our set of
16140 user defined types. */
16141
16142 static struct type *
16143 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
16144 {
16145 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16146 struct type *type;
16147 struct attribute *attr;
16148 const char *name;
16149
16150 /* If the definition of this type lives in .debug_types, read that type.
16151 Don't follow DW_AT_specification though, that will take us back up
16152 the chain and we want to go down. */
16153 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16154 if (attr)
16155 {
16156 type = get_DW_AT_signature_type (die, attr, cu);
16157
16158 /* The type's CU may not be the same as CU.
16159 Ensure TYPE is recorded with CU in die_type_hash. */
16160 return set_die_type (die, type, cu);
16161 }
16162
16163 type = alloc_type (objfile);
16164 INIT_CPLUS_SPECIFIC (type);
16165
16166 name = dwarf2_name (die, cu);
16167 if (name != NULL)
16168 {
16169 if (cu->language == language_cplus
16170 || cu->language == language_d
16171 || cu->language == language_rust)
16172 {
16173 const char *full_name = dwarf2_full_name (name, die, cu);
16174
16175 /* dwarf2_full_name might have already finished building the DIE's
16176 type. If so, there is no need to continue. */
16177 if (get_die_type (die, cu) != NULL)
16178 return get_die_type (die, cu);
16179
16180 TYPE_TAG_NAME (type) = full_name;
16181 if (die->tag == DW_TAG_structure_type
16182 || die->tag == DW_TAG_class_type)
16183 TYPE_NAME (type) = TYPE_TAG_NAME (type);
16184 }
16185 else
16186 {
16187 /* The name is already allocated along with this objfile, so
16188 we don't need to duplicate it for the type. */
16189 TYPE_TAG_NAME (type) = name;
16190 if (die->tag == DW_TAG_class_type)
16191 TYPE_NAME (type) = TYPE_TAG_NAME (type);
16192 }
16193 }
16194
16195 if (die->tag == DW_TAG_structure_type)
16196 {
16197 TYPE_CODE (type) = TYPE_CODE_STRUCT;
16198 }
16199 else if (die->tag == DW_TAG_union_type)
16200 {
16201 TYPE_CODE (type) = TYPE_CODE_UNION;
16202 }
16203 else if (die->tag == DW_TAG_variant_part)
16204 {
16205 TYPE_CODE (type) = TYPE_CODE_UNION;
16206 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
16207 }
16208 else
16209 {
16210 TYPE_CODE (type) = TYPE_CODE_STRUCT;
16211 }
16212
16213 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16214 TYPE_DECLARED_CLASS (type) = 1;
16215
16216 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16217 if (attr)
16218 {
16219 if (attr_form_is_constant (attr))
16220 TYPE_LENGTH (type) = DW_UNSND (attr);
16221 else
16222 {
16223 /* For the moment, dynamic type sizes are not supported
16224 by GDB's struct type. The actual size is determined
16225 on-demand when resolving the type of a given object,
16226 so set the type's length to zero for now. Otherwise,
16227 we record an expression as the length, and that expression
16228 could lead to a very large value, which could eventually
16229 lead to us trying to allocate that much memory when creating
16230 a value of that type. */
16231 TYPE_LENGTH (type) = 0;
16232 }
16233 }
16234 else
16235 {
16236 TYPE_LENGTH (type) = 0;
16237 }
16238
16239 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16240 {
16241 /* ICC<14 does not output the required DW_AT_declaration on
16242 incomplete types, but gives them a size of zero. */
16243 TYPE_STUB (type) = 1;
16244 }
16245 else
16246 TYPE_STUB_SUPPORTED (type) = 1;
16247
16248 if (die_is_declaration (die, cu))
16249 TYPE_STUB (type) = 1;
16250 else if (attr == NULL && die->child == NULL
16251 && producer_is_realview (cu->producer))
16252 /* RealView does not output the required DW_AT_declaration
16253 on incomplete types. */
16254 TYPE_STUB (type) = 1;
16255
16256 /* We need to add the type field to the die immediately so we don't
16257 infinitely recurse when dealing with pointers to the structure
16258 type within the structure itself. */
16259 set_die_type (die, type, cu);
16260
16261 /* set_die_type should be already done. */
16262 set_descriptive_type (type, die, cu);
16263
16264 return type;
16265 }
16266
16267 /* A helper for process_structure_scope that handles a single member
16268 DIE. */
16269
16270 static void
16271 handle_struct_member_die (struct die_info *child_die, struct type *type,
16272 struct field_info *fi,
16273 std::vector<struct symbol *> *template_args,
16274 struct dwarf2_cu *cu)
16275 {
16276 if (child_die->tag == DW_TAG_member
16277 || child_die->tag == DW_TAG_variable
16278 || child_die->tag == DW_TAG_variant_part)
16279 {
16280 /* NOTE: carlton/2002-11-05: A C++ static data member
16281 should be a DW_TAG_member that is a declaration, but
16282 all versions of G++ as of this writing (so through at
16283 least 3.2.1) incorrectly generate DW_TAG_variable
16284 tags for them instead. */
16285 dwarf2_add_field (fi, child_die, cu);
16286 }
16287 else if (child_die->tag == DW_TAG_subprogram)
16288 {
16289 /* Rust doesn't have member functions in the C++ sense.
16290 However, it does emit ordinary functions as children
16291 of a struct DIE. */
16292 if (cu->language == language_rust)
16293 read_func_scope (child_die, cu);
16294 else
16295 {
16296 /* C++ member function. */
16297 dwarf2_add_member_fn (fi, child_die, type, cu);
16298 }
16299 }
16300 else if (child_die->tag == DW_TAG_inheritance)
16301 {
16302 /* C++ base class field. */
16303 dwarf2_add_field (fi, child_die, cu);
16304 }
16305 else if (type_can_define_types (child_die))
16306 dwarf2_add_type_defn (fi, child_die, cu);
16307 else if (child_die->tag == DW_TAG_template_type_param
16308 || child_die->tag == DW_TAG_template_value_param)
16309 {
16310 struct symbol *arg = new_symbol (child_die, NULL, cu);
16311
16312 if (arg != NULL)
16313 template_args->push_back (arg);
16314 }
16315 else if (child_die->tag == DW_TAG_variant)
16316 {
16317 /* In a variant we want to get the discriminant and also add a
16318 field for our sole member child. */
16319 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16320
16321 for (struct die_info *variant_child = child_die->child;
16322 variant_child != NULL;
16323 variant_child = sibling_die (variant_child))
16324 {
16325 if (variant_child->tag == DW_TAG_member)
16326 {
16327 handle_struct_member_die (variant_child, type, fi,
16328 template_args, cu);
16329 /* Only handle the one. */
16330 break;
16331 }
16332 }
16333
16334 /* We don't handle this but we might as well report it if we see
16335 it. */
16336 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
16337 complaint (&symfile_complaints,
16338 _("DW_AT_discr_list is not supported yet"
16339 " - DIE at %s [in module %s]"),
16340 sect_offset_str (child_die->sect_off),
16341 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16342
16343 /* The first field was just added, so we can stash the
16344 discriminant there. */
16345 gdb_assert (!fi->fields.empty ());
16346 if (discr == NULL)
16347 fi->fields.back ().variant.default_branch = true;
16348 else
16349 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
16350 }
16351 }
16352
16353 /* Finish creating a structure or union type, including filling in
16354 its members and creating a symbol for it. */
16355
16356 static void
16357 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16358 {
16359 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16360 struct die_info *child_die;
16361 struct type *type;
16362
16363 type = get_die_type (die, cu);
16364 if (type == NULL)
16365 type = read_structure_type (die, cu);
16366
16367 /* When reading a DW_TAG_variant_part, we need to notice when we
16368 read the discriminant member, so we can record it later in the
16369 discriminant_info. */
16370 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16371 sect_offset discr_offset;
16372
16373 if (is_variant_part)
16374 {
16375 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16376 if (discr == NULL)
16377 {
16378 /* Maybe it's a univariant form, an extension we support.
16379 In this case arrange not to check the offset. */
16380 is_variant_part = false;
16381 }
16382 else if (attr_form_is_ref (discr))
16383 {
16384 struct dwarf2_cu *target_cu = cu;
16385 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16386
16387 discr_offset = target_die->sect_off;
16388 }
16389 else
16390 {
16391 complaint (&symfile_complaints,
16392 _("DW_AT_discr does not have DIE reference form"
16393 " - DIE at %s [in module %s]"),
16394 sect_offset_str (die->sect_off),
16395 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16396 is_variant_part = false;
16397 }
16398 }
16399
16400 if (die->child != NULL && ! die_is_declaration (die, cu))
16401 {
16402 struct field_info fi;
16403 std::vector<struct symbol *> template_args;
16404
16405 child_die = die->child;
16406
16407 while (child_die && child_die->tag)
16408 {
16409 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16410
16411 if (is_variant_part && discr_offset == child_die->sect_off)
16412 fi.fields.back ().variant.is_discriminant = true;
16413
16414 child_die = sibling_die (child_die);
16415 }
16416
16417 /* Attach template arguments to type. */
16418 if (!template_args.empty ())
16419 {
16420 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16421 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16422 TYPE_TEMPLATE_ARGUMENTS (type)
16423 = XOBNEWVEC (&objfile->objfile_obstack,
16424 struct symbol *,
16425 TYPE_N_TEMPLATE_ARGUMENTS (type));
16426 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16427 template_args.data (),
16428 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16429 * sizeof (struct symbol *)));
16430 }
16431
16432 /* Attach fields and member functions to the type. */
16433 if (fi.nfields)
16434 dwarf2_attach_fields_to_type (&fi, type, cu);
16435 if (!fi.fnfieldlists.empty ())
16436 {
16437 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16438
16439 /* Get the type which refers to the base class (possibly this
16440 class itself) which contains the vtable pointer for the current
16441 class from the DW_AT_containing_type attribute. This use of
16442 DW_AT_containing_type is a GNU extension. */
16443
16444 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16445 {
16446 struct type *t = die_containing_type (die, cu);
16447
16448 set_type_vptr_basetype (type, t);
16449 if (type == t)
16450 {
16451 int i;
16452
16453 /* Our own class provides vtbl ptr. */
16454 for (i = TYPE_NFIELDS (t) - 1;
16455 i >= TYPE_N_BASECLASSES (t);
16456 --i)
16457 {
16458 const char *fieldname = TYPE_FIELD_NAME (t, i);
16459
16460 if (is_vtable_name (fieldname, cu))
16461 {
16462 set_type_vptr_fieldno (type, i);
16463 break;
16464 }
16465 }
16466
16467 /* Complain if virtual function table field not found. */
16468 if (i < TYPE_N_BASECLASSES (t))
16469 complaint (&symfile_complaints,
16470 _("virtual function table pointer "
16471 "not found when defining class '%s'"),
16472 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16473 "");
16474 }
16475 else
16476 {
16477 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16478 }
16479 }
16480 else if (cu->producer
16481 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16482 {
16483 /* The IBM XLC compiler does not provide direct indication
16484 of the containing type, but the vtable pointer is
16485 always named __vfp. */
16486
16487 int i;
16488
16489 for (i = TYPE_NFIELDS (type) - 1;
16490 i >= TYPE_N_BASECLASSES (type);
16491 --i)
16492 {
16493 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16494 {
16495 set_type_vptr_fieldno (type, i);
16496 set_type_vptr_basetype (type, type);
16497 break;
16498 }
16499 }
16500 }
16501 }
16502
16503 /* Copy fi.typedef_field_list linked list elements content into the
16504 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16505 if (!fi.typedef_field_list.empty ())
16506 {
16507 int count = fi.typedef_field_list.size ();
16508
16509 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16510 TYPE_TYPEDEF_FIELD_ARRAY (type)
16511 = ((struct decl_field *)
16512 TYPE_ALLOC (type,
16513 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16514 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16515
16516 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16517 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16518 }
16519
16520 /* Copy fi.nested_types_list linked list elements content into the
16521 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16522 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16523 {
16524 int count = fi.nested_types_list.size ();
16525
16526 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16527 TYPE_NESTED_TYPES_ARRAY (type)
16528 = ((struct decl_field *)
16529 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16530 TYPE_NESTED_TYPES_COUNT (type) = count;
16531
16532 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16533 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16534 }
16535 }
16536
16537 quirk_gcc_member_function_pointer (type, objfile);
16538 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16539 cu->rust_unions.push_back (type);
16540
16541 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16542 snapshots) has been known to create a die giving a declaration
16543 for a class that has, as a child, a die giving a definition for a
16544 nested class. So we have to process our children even if the
16545 current die is a declaration. Normally, of course, a declaration
16546 won't have any children at all. */
16547
16548 child_die = die->child;
16549
16550 while (child_die != NULL && child_die->tag)
16551 {
16552 if (child_die->tag == DW_TAG_member
16553 || child_die->tag == DW_TAG_variable
16554 || child_die->tag == DW_TAG_inheritance
16555 || child_die->tag == DW_TAG_template_value_param
16556 || child_die->tag == DW_TAG_template_type_param)
16557 {
16558 /* Do nothing. */
16559 }
16560 else
16561 process_die (child_die, cu);
16562
16563 child_die = sibling_die (child_die);
16564 }
16565
16566 /* Do not consider external references. According to the DWARF standard,
16567 these DIEs are identified by the fact that they have no byte_size
16568 attribute, and a declaration attribute. */
16569 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16570 || !die_is_declaration (die, cu))
16571 new_symbol (die, type, cu);
16572 }
16573
16574 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16575 update TYPE using some information only available in DIE's children. */
16576
16577 static void
16578 update_enumeration_type_from_children (struct die_info *die,
16579 struct type *type,
16580 struct dwarf2_cu *cu)
16581 {
16582 struct die_info *child_die;
16583 int unsigned_enum = 1;
16584 int flag_enum = 1;
16585 ULONGEST mask = 0;
16586
16587 auto_obstack obstack;
16588
16589 for (child_die = die->child;
16590 child_die != NULL && child_die->tag;
16591 child_die = sibling_die (child_die))
16592 {
16593 struct attribute *attr;
16594 LONGEST value;
16595 const gdb_byte *bytes;
16596 struct dwarf2_locexpr_baton *baton;
16597 const char *name;
16598
16599 if (child_die->tag != DW_TAG_enumerator)
16600 continue;
16601
16602 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16603 if (attr == NULL)
16604 continue;
16605
16606 name = dwarf2_name (child_die, cu);
16607 if (name == NULL)
16608 name = "<anonymous enumerator>";
16609
16610 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16611 &value, &bytes, &baton);
16612 if (value < 0)
16613 {
16614 unsigned_enum = 0;
16615 flag_enum = 0;
16616 }
16617 else if ((mask & value) != 0)
16618 flag_enum = 0;
16619 else
16620 mask |= value;
16621
16622 /* If we already know that the enum type is neither unsigned, nor
16623 a flag type, no need to look at the rest of the enumerates. */
16624 if (!unsigned_enum && !flag_enum)
16625 break;
16626 }
16627
16628 if (unsigned_enum)
16629 TYPE_UNSIGNED (type) = 1;
16630 if (flag_enum)
16631 TYPE_FLAG_ENUM (type) = 1;
16632 }
16633
16634 /* Given a DW_AT_enumeration_type die, set its type. We do not
16635 complete the type's fields yet, or create any symbols. */
16636
16637 static struct type *
16638 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16639 {
16640 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16641 struct type *type;
16642 struct attribute *attr;
16643 const char *name;
16644
16645 /* If the definition of this type lives in .debug_types, read that type.
16646 Don't follow DW_AT_specification though, that will take us back up
16647 the chain and we want to go down. */
16648 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16649 if (attr)
16650 {
16651 type = get_DW_AT_signature_type (die, attr, cu);
16652
16653 /* The type's CU may not be the same as CU.
16654 Ensure TYPE is recorded with CU in die_type_hash. */
16655 return set_die_type (die, type, cu);
16656 }
16657
16658 type = alloc_type (objfile);
16659
16660 TYPE_CODE (type) = TYPE_CODE_ENUM;
16661 name = dwarf2_full_name (NULL, die, cu);
16662 if (name != NULL)
16663 TYPE_TAG_NAME (type) = name;
16664
16665 attr = dwarf2_attr (die, DW_AT_type, cu);
16666 if (attr != NULL)
16667 {
16668 struct type *underlying_type = die_type (die, cu);
16669
16670 TYPE_TARGET_TYPE (type) = underlying_type;
16671 }
16672
16673 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16674 if (attr)
16675 {
16676 TYPE_LENGTH (type) = DW_UNSND (attr);
16677 }
16678 else
16679 {
16680 TYPE_LENGTH (type) = 0;
16681 }
16682
16683 /* The enumeration DIE can be incomplete. In Ada, any type can be
16684 declared as private in the package spec, and then defined only
16685 inside the package body. Such types are known as Taft Amendment
16686 Types. When another package uses such a type, an incomplete DIE
16687 may be generated by the compiler. */
16688 if (die_is_declaration (die, cu))
16689 TYPE_STUB (type) = 1;
16690
16691 /* Finish the creation of this type by using the enum's children.
16692 We must call this even when the underlying type has been provided
16693 so that we can determine if we're looking at a "flag" enum. */
16694 update_enumeration_type_from_children (die, type, cu);
16695
16696 /* If this type has an underlying type that is not a stub, then we
16697 may use its attributes. We always use the "unsigned" attribute
16698 in this situation, because ordinarily we guess whether the type
16699 is unsigned -- but the guess can be wrong and the underlying type
16700 can tell us the reality. However, we defer to a local size
16701 attribute if one exists, because this lets the compiler override
16702 the underlying type if needed. */
16703 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16704 {
16705 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16706 if (TYPE_LENGTH (type) == 0)
16707 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16708 }
16709
16710 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16711
16712 return set_die_type (die, type, cu);
16713 }
16714
16715 /* Given a pointer to a die which begins an enumeration, process all
16716 the dies that define the members of the enumeration, and create the
16717 symbol for the enumeration type.
16718
16719 NOTE: We reverse the order of the element list. */
16720
16721 static void
16722 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16723 {
16724 struct type *this_type;
16725
16726 this_type = get_die_type (die, cu);
16727 if (this_type == NULL)
16728 this_type = read_enumeration_type (die, cu);
16729
16730 if (die->child != NULL)
16731 {
16732 struct die_info *child_die;
16733 struct symbol *sym;
16734 struct field *fields = NULL;
16735 int num_fields = 0;
16736 const char *name;
16737
16738 child_die = die->child;
16739 while (child_die && child_die->tag)
16740 {
16741 if (child_die->tag != DW_TAG_enumerator)
16742 {
16743 process_die (child_die, cu);
16744 }
16745 else
16746 {
16747 name = dwarf2_name (child_die, cu);
16748 if (name)
16749 {
16750 sym = new_symbol (child_die, this_type, cu);
16751
16752 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16753 {
16754 fields = (struct field *)
16755 xrealloc (fields,
16756 (num_fields + DW_FIELD_ALLOC_CHUNK)
16757 * sizeof (struct field));
16758 }
16759
16760 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16761 FIELD_TYPE (fields[num_fields]) = NULL;
16762 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16763 FIELD_BITSIZE (fields[num_fields]) = 0;
16764
16765 num_fields++;
16766 }
16767 }
16768
16769 child_die = sibling_die (child_die);
16770 }
16771
16772 if (num_fields)
16773 {
16774 TYPE_NFIELDS (this_type) = num_fields;
16775 TYPE_FIELDS (this_type) = (struct field *)
16776 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16777 memcpy (TYPE_FIELDS (this_type), fields,
16778 sizeof (struct field) * num_fields);
16779 xfree (fields);
16780 }
16781 }
16782
16783 /* If we are reading an enum from a .debug_types unit, and the enum
16784 is a declaration, and the enum is not the signatured type in the
16785 unit, then we do not want to add a symbol for it. Adding a
16786 symbol would in some cases obscure the true definition of the
16787 enum, giving users an incomplete type when the definition is
16788 actually available. Note that we do not want to do this for all
16789 enums which are just declarations, because C++0x allows forward
16790 enum declarations. */
16791 if (cu->per_cu->is_debug_types
16792 && die_is_declaration (die, cu))
16793 {
16794 struct signatured_type *sig_type;
16795
16796 sig_type = (struct signatured_type *) cu->per_cu;
16797 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16798 if (sig_type->type_offset_in_section != die->sect_off)
16799 return;
16800 }
16801
16802 new_symbol (die, this_type, cu);
16803 }
16804
16805 /* Extract all information from a DW_TAG_array_type DIE and put it in
16806 the DIE's type field. For now, this only handles one dimensional
16807 arrays. */
16808
16809 static struct type *
16810 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16811 {
16812 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16813 struct die_info *child_die;
16814 struct type *type;
16815 struct type *element_type, *range_type, *index_type;
16816 struct attribute *attr;
16817 const char *name;
16818 struct dynamic_prop *byte_stride_prop = NULL;
16819 unsigned int bit_stride = 0;
16820
16821 element_type = die_type (die, cu);
16822
16823 /* The die_type call above may have already set the type for this DIE. */
16824 type = get_die_type (die, cu);
16825 if (type)
16826 return type;
16827
16828 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16829 if (attr != NULL)
16830 {
16831 int stride_ok;
16832
16833 byte_stride_prop
16834 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16835 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16836 if (!stride_ok)
16837 {
16838 complaint (&symfile_complaints,
16839 _("unable to read array DW_AT_byte_stride "
16840 " - DIE at %s [in module %s]"),
16841 sect_offset_str (die->sect_off),
16842 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16843 /* Ignore this attribute. We will likely not be able to print
16844 arrays of this type correctly, but there is little we can do
16845 to help if we cannot read the attribute's value. */
16846 byte_stride_prop = NULL;
16847 }
16848 }
16849
16850 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16851 if (attr != NULL)
16852 bit_stride = DW_UNSND (attr);
16853
16854 /* Irix 6.2 native cc creates array types without children for
16855 arrays with unspecified length. */
16856 if (die->child == NULL)
16857 {
16858 index_type = objfile_type (objfile)->builtin_int;
16859 range_type = create_static_range_type (NULL, index_type, 0, -1);
16860 type = create_array_type_with_stride (NULL, element_type, range_type,
16861 byte_stride_prop, bit_stride);
16862 return set_die_type (die, type, cu);
16863 }
16864
16865 std::vector<struct type *> range_types;
16866 child_die = die->child;
16867 while (child_die && child_die->tag)
16868 {
16869 if (child_die->tag == DW_TAG_subrange_type)
16870 {
16871 struct type *child_type = read_type_die (child_die, cu);
16872
16873 if (child_type != NULL)
16874 {
16875 /* The range type was succesfully read. Save it for the
16876 array type creation. */
16877 range_types.push_back (child_type);
16878 }
16879 }
16880 child_die = sibling_die (child_die);
16881 }
16882
16883 /* Dwarf2 dimensions are output from left to right, create the
16884 necessary array types in backwards order. */
16885
16886 type = element_type;
16887
16888 if (read_array_order (die, cu) == DW_ORD_col_major)
16889 {
16890 int i = 0;
16891
16892 while (i < range_types.size ())
16893 type = create_array_type_with_stride (NULL, type, range_types[i++],
16894 byte_stride_prop, bit_stride);
16895 }
16896 else
16897 {
16898 size_t ndim = range_types.size ();
16899 while (ndim-- > 0)
16900 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16901 byte_stride_prop, bit_stride);
16902 }
16903
16904 /* Understand Dwarf2 support for vector types (like they occur on
16905 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16906 array type. This is not part of the Dwarf2/3 standard yet, but a
16907 custom vendor extension. The main difference between a regular
16908 array and the vector variant is that vectors are passed by value
16909 to functions. */
16910 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16911 if (attr)
16912 make_vector_type (type);
16913
16914 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16915 implementation may choose to implement triple vectors using this
16916 attribute. */
16917 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16918 if (attr)
16919 {
16920 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16921 TYPE_LENGTH (type) = DW_UNSND (attr);
16922 else
16923 complaint (&symfile_complaints,
16924 _("DW_AT_byte_size for array type smaller "
16925 "than the total size of elements"));
16926 }
16927
16928 name = dwarf2_name (die, cu);
16929 if (name)
16930 TYPE_NAME (type) = name;
16931
16932 /* Install the type in the die. */
16933 set_die_type (die, type, cu);
16934
16935 /* set_die_type should be already done. */
16936 set_descriptive_type (type, die, cu);
16937
16938 return type;
16939 }
16940
16941 static enum dwarf_array_dim_ordering
16942 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16943 {
16944 struct attribute *attr;
16945
16946 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16947
16948 if (attr)
16949 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16950
16951 /* GNU F77 is a special case, as at 08/2004 array type info is the
16952 opposite order to the dwarf2 specification, but data is still
16953 laid out as per normal fortran.
16954
16955 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16956 version checking. */
16957
16958 if (cu->language == language_fortran
16959 && cu->producer && strstr (cu->producer, "GNU F77"))
16960 {
16961 return DW_ORD_row_major;
16962 }
16963
16964 switch (cu->language_defn->la_array_ordering)
16965 {
16966 case array_column_major:
16967 return DW_ORD_col_major;
16968 case array_row_major:
16969 default:
16970 return DW_ORD_row_major;
16971 };
16972 }
16973
16974 /* Extract all information from a DW_TAG_set_type DIE and put it in
16975 the DIE's type field. */
16976
16977 static struct type *
16978 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16979 {
16980 struct type *domain_type, *set_type;
16981 struct attribute *attr;
16982
16983 domain_type = die_type (die, cu);
16984
16985 /* The die_type call above may have already set the type for this DIE. */
16986 set_type = get_die_type (die, cu);
16987 if (set_type)
16988 return set_type;
16989
16990 set_type = create_set_type (NULL, domain_type);
16991
16992 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16993 if (attr)
16994 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16995
16996 return set_die_type (die, set_type, cu);
16997 }
16998
16999 /* A helper for read_common_block that creates a locexpr baton.
17000 SYM is the symbol which we are marking as computed.
17001 COMMON_DIE is the DIE for the common block.
17002 COMMON_LOC is the location expression attribute for the common
17003 block itself.
17004 MEMBER_LOC is the location expression attribute for the particular
17005 member of the common block that we are processing.
17006 CU is the CU from which the above come. */
17007
17008 static void
17009 mark_common_block_symbol_computed (struct symbol *sym,
17010 struct die_info *common_die,
17011 struct attribute *common_loc,
17012 struct attribute *member_loc,
17013 struct dwarf2_cu *cu)
17014 {
17015 struct dwarf2_per_objfile *dwarf2_per_objfile
17016 = cu->per_cu->dwarf2_per_objfile;
17017 struct objfile *objfile = dwarf2_per_objfile->objfile;
17018 struct dwarf2_locexpr_baton *baton;
17019 gdb_byte *ptr;
17020 unsigned int cu_off;
17021 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
17022 LONGEST offset = 0;
17023
17024 gdb_assert (common_loc && member_loc);
17025 gdb_assert (attr_form_is_block (common_loc));
17026 gdb_assert (attr_form_is_block (member_loc)
17027 || attr_form_is_constant (member_loc));
17028
17029 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17030 baton->per_cu = cu->per_cu;
17031 gdb_assert (baton->per_cu);
17032
17033 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17034
17035 if (attr_form_is_constant (member_loc))
17036 {
17037 offset = dwarf2_get_attr_constant_value (member_loc, 0);
17038 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17039 }
17040 else
17041 baton->size += DW_BLOCK (member_loc)->size;
17042
17043 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17044 baton->data = ptr;
17045
17046 *ptr++ = DW_OP_call4;
17047 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17048 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17049 ptr += 4;
17050
17051 if (attr_form_is_constant (member_loc))
17052 {
17053 *ptr++ = DW_OP_addr;
17054 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17055 ptr += cu->header.addr_size;
17056 }
17057 else
17058 {
17059 /* We have to copy the data here, because DW_OP_call4 will only
17060 use a DW_AT_location attribute. */
17061 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
17062 ptr += DW_BLOCK (member_loc)->size;
17063 }
17064
17065 *ptr++ = DW_OP_plus;
17066 gdb_assert (ptr - baton->data == baton->size);
17067
17068 SYMBOL_LOCATION_BATON (sym) = baton;
17069 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17070 }
17071
17072 /* Create appropriate locally-scoped variables for all the
17073 DW_TAG_common_block entries. Also create a struct common_block
17074 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17075 is used to sepate the common blocks name namespace from regular
17076 variable names. */
17077
17078 static void
17079 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17080 {
17081 struct attribute *attr;
17082
17083 attr = dwarf2_attr (die, DW_AT_location, cu);
17084 if (attr)
17085 {
17086 /* Support the .debug_loc offsets. */
17087 if (attr_form_is_block (attr))
17088 {
17089 /* Ok. */
17090 }
17091 else if (attr_form_is_section_offset (attr))
17092 {
17093 dwarf2_complex_location_expr_complaint ();
17094 attr = NULL;
17095 }
17096 else
17097 {
17098 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17099 "common block member");
17100 attr = NULL;
17101 }
17102 }
17103
17104 if (die->child != NULL)
17105 {
17106 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17107 struct die_info *child_die;
17108 size_t n_entries = 0, size;
17109 struct common_block *common_block;
17110 struct symbol *sym;
17111
17112 for (child_die = die->child;
17113 child_die && child_die->tag;
17114 child_die = sibling_die (child_die))
17115 ++n_entries;
17116
17117 size = (sizeof (struct common_block)
17118 + (n_entries - 1) * sizeof (struct symbol *));
17119 common_block
17120 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17121 size);
17122 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17123 common_block->n_entries = 0;
17124
17125 for (child_die = die->child;
17126 child_die && child_die->tag;
17127 child_die = sibling_die (child_die))
17128 {
17129 /* Create the symbol in the DW_TAG_common_block block in the current
17130 symbol scope. */
17131 sym = new_symbol (child_die, NULL, cu);
17132 if (sym != NULL)
17133 {
17134 struct attribute *member_loc;
17135
17136 common_block->contents[common_block->n_entries++] = sym;
17137
17138 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17139 cu);
17140 if (member_loc)
17141 {
17142 /* GDB has handled this for a long time, but it is
17143 not specified by DWARF. It seems to have been
17144 emitted by gfortran at least as recently as:
17145 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17146 complaint (&symfile_complaints,
17147 _("Variable in common block has "
17148 "DW_AT_data_member_location "
17149 "- DIE at %s [in module %s]"),
17150 sect_offset_str (child_die->sect_off),
17151 objfile_name (objfile));
17152
17153 if (attr_form_is_section_offset (member_loc))
17154 dwarf2_complex_location_expr_complaint ();
17155 else if (attr_form_is_constant (member_loc)
17156 || attr_form_is_block (member_loc))
17157 {
17158 if (attr)
17159 mark_common_block_symbol_computed (sym, die, attr,
17160 member_loc, cu);
17161 }
17162 else
17163 dwarf2_complex_location_expr_complaint ();
17164 }
17165 }
17166 }
17167
17168 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17169 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17170 }
17171 }
17172
17173 /* Create a type for a C++ namespace. */
17174
17175 static struct type *
17176 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17177 {
17178 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17179 const char *previous_prefix, *name;
17180 int is_anonymous;
17181 struct type *type;
17182
17183 /* For extensions, reuse the type of the original namespace. */
17184 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17185 {
17186 struct die_info *ext_die;
17187 struct dwarf2_cu *ext_cu = cu;
17188
17189 ext_die = dwarf2_extension (die, &ext_cu);
17190 type = read_type_die (ext_die, ext_cu);
17191
17192 /* EXT_CU may not be the same as CU.
17193 Ensure TYPE is recorded with CU in die_type_hash. */
17194 return set_die_type (die, type, cu);
17195 }
17196
17197 name = namespace_name (die, &is_anonymous, cu);
17198
17199 /* Now build the name of the current namespace. */
17200
17201 previous_prefix = determine_prefix (die, cu);
17202 if (previous_prefix[0] != '\0')
17203 name = typename_concat (&objfile->objfile_obstack,
17204 previous_prefix, name, 0, cu);
17205
17206 /* Create the type. */
17207 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17208 TYPE_TAG_NAME (type) = TYPE_NAME (type);
17209
17210 return set_die_type (die, type, cu);
17211 }
17212
17213 /* Read a namespace scope. */
17214
17215 static void
17216 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17217 {
17218 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17219 int is_anonymous;
17220
17221 /* Add a symbol associated to this if we haven't seen the namespace
17222 before. Also, add a using directive if it's an anonymous
17223 namespace. */
17224
17225 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17226 {
17227 struct type *type;
17228
17229 type = read_type_die (die, cu);
17230 new_symbol (die, type, cu);
17231
17232 namespace_name (die, &is_anonymous, cu);
17233 if (is_anonymous)
17234 {
17235 const char *previous_prefix = determine_prefix (die, cu);
17236
17237 std::vector<const char *> excludes;
17238 add_using_directive (using_directives (cu->language),
17239 previous_prefix, TYPE_NAME (type), NULL,
17240 NULL, excludes, 0, &objfile->objfile_obstack);
17241 }
17242 }
17243
17244 if (die->child != NULL)
17245 {
17246 struct die_info *child_die = die->child;
17247
17248 while (child_die && child_die->tag)
17249 {
17250 process_die (child_die, cu);
17251 child_die = sibling_die (child_die);
17252 }
17253 }
17254 }
17255
17256 /* Read a Fortran module as type. This DIE can be only a declaration used for
17257 imported module. Still we need that type as local Fortran "use ... only"
17258 declaration imports depend on the created type in determine_prefix. */
17259
17260 static struct type *
17261 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17262 {
17263 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17264 const char *module_name;
17265 struct type *type;
17266
17267 module_name = dwarf2_name (die, cu);
17268 if (!module_name)
17269 complaint (&symfile_complaints,
17270 _("DW_TAG_module has no name, offset %s"),
17271 sect_offset_str (die->sect_off));
17272 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17273
17274 /* determine_prefix uses TYPE_TAG_NAME. */
17275 TYPE_TAG_NAME (type) = TYPE_NAME (type);
17276
17277 return set_die_type (die, type, cu);
17278 }
17279
17280 /* Read a Fortran module. */
17281
17282 static void
17283 read_module (struct die_info *die, struct dwarf2_cu *cu)
17284 {
17285 struct die_info *child_die = die->child;
17286 struct type *type;
17287
17288 type = read_type_die (die, cu);
17289 new_symbol (die, type, cu);
17290
17291 while (child_die && child_die->tag)
17292 {
17293 process_die (child_die, cu);
17294 child_die = sibling_die (child_die);
17295 }
17296 }
17297
17298 /* Return the name of the namespace represented by DIE. Set
17299 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17300 namespace. */
17301
17302 static const char *
17303 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17304 {
17305 struct die_info *current_die;
17306 const char *name = NULL;
17307
17308 /* Loop through the extensions until we find a name. */
17309
17310 for (current_die = die;
17311 current_die != NULL;
17312 current_die = dwarf2_extension (die, &cu))
17313 {
17314 /* We don't use dwarf2_name here so that we can detect the absence
17315 of a name -> anonymous namespace. */
17316 name = dwarf2_string_attr (die, DW_AT_name, cu);
17317
17318 if (name != NULL)
17319 break;
17320 }
17321
17322 /* Is it an anonymous namespace? */
17323
17324 *is_anonymous = (name == NULL);
17325 if (*is_anonymous)
17326 name = CP_ANONYMOUS_NAMESPACE_STR;
17327
17328 return name;
17329 }
17330
17331 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17332 the user defined type vector. */
17333
17334 static struct type *
17335 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17336 {
17337 struct gdbarch *gdbarch
17338 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17339 struct comp_unit_head *cu_header = &cu->header;
17340 struct type *type;
17341 struct attribute *attr_byte_size;
17342 struct attribute *attr_address_class;
17343 int byte_size, addr_class;
17344 struct type *target_type;
17345
17346 target_type = die_type (die, cu);
17347
17348 /* The die_type call above may have already set the type for this DIE. */
17349 type = get_die_type (die, cu);
17350 if (type)
17351 return type;
17352
17353 type = lookup_pointer_type (target_type);
17354
17355 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17356 if (attr_byte_size)
17357 byte_size = DW_UNSND (attr_byte_size);
17358 else
17359 byte_size = cu_header->addr_size;
17360
17361 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17362 if (attr_address_class)
17363 addr_class = DW_UNSND (attr_address_class);
17364 else
17365 addr_class = DW_ADDR_none;
17366
17367 /* If the pointer size or address class is different than the
17368 default, create a type variant marked as such and set the
17369 length accordingly. */
17370 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
17371 {
17372 if (gdbarch_address_class_type_flags_p (gdbarch))
17373 {
17374 int type_flags;
17375
17376 type_flags = gdbarch_address_class_type_flags
17377 (gdbarch, byte_size, addr_class);
17378 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17379 == 0);
17380 type = make_type_with_address_space (type, type_flags);
17381 }
17382 else if (TYPE_LENGTH (type) != byte_size)
17383 {
17384 complaint (&symfile_complaints,
17385 _("invalid pointer size %d"), byte_size);
17386 }
17387 else
17388 {
17389 /* Should we also complain about unhandled address classes? */
17390 }
17391 }
17392
17393 TYPE_LENGTH (type) = byte_size;
17394 return set_die_type (die, type, cu);
17395 }
17396
17397 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17398 the user defined type vector. */
17399
17400 static struct type *
17401 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17402 {
17403 struct type *type;
17404 struct type *to_type;
17405 struct type *domain;
17406
17407 to_type = die_type (die, cu);
17408 domain = die_containing_type (die, cu);
17409
17410 /* The calls above may have already set the type for this DIE. */
17411 type = get_die_type (die, cu);
17412 if (type)
17413 return type;
17414
17415 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17416 type = lookup_methodptr_type (to_type);
17417 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17418 {
17419 struct type *new_type
17420 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17421
17422 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17423 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17424 TYPE_VARARGS (to_type));
17425 type = lookup_methodptr_type (new_type);
17426 }
17427 else
17428 type = lookup_memberptr_type (to_type, domain);
17429
17430 return set_die_type (die, type, cu);
17431 }
17432
17433 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17434 the user defined type vector. */
17435
17436 static struct type *
17437 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17438 enum type_code refcode)
17439 {
17440 struct comp_unit_head *cu_header = &cu->header;
17441 struct type *type, *target_type;
17442 struct attribute *attr;
17443
17444 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17445
17446 target_type = die_type (die, cu);
17447
17448 /* The die_type call above may have already set the type for this DIE. */
17449 type = get_die_type (die, cu);
17450 if (type)
17451 return type;
17452
17453 type = lookup_reference_type (target_type, refcode);
17454 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17455 if (attr)
17456 {
17457 TYPE_LENGTH (type) = DW_UNSND (attr);
17458 }
17459 else
17460 {
17461 TYPE_LENGTH (type) = cu_header->addr_size;
17462 }
17463 return set_die_type (die, type, cu);
17464 }
17465
17466 /* Add the given cv-qualifiers to the element type of the array. GCC
17467 outputs DWARF type qualifiers that apply to an array, not the
17468 element type. But GDB relies on the array element type to carry
17469 the cv-qualifiers. This mimics section 6.7.3 of the C99
17470 specification. */
17471
17472 static struct type *
17473 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17474 struct type *base_type, int cnst, int voltl)
17475 {
17476 struct type *el_type, *inner_array;
17477
17478 base_type = copy_type (base_type);
17479 inner_array = base_type;
17480
17481 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17482 {
17483 TYPE_TARGET_TYPE (inner_array) =
17484 copy_type (TYPE_TARGET_TYPE (inner_array));
17485 inner_array = TYPE_TARGET_TYPE (inner_array);
17486 }
17487
17488 el_type = TYPE_TARGET_TYPE (inner_array);
17489 cnst |= TYPE_CONST (el_type);
17490 voltl |= TYPE_VOLATILE (el_type);
17491 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17492
17493 return set_die_type (die, base_type, cu);
17494 }
17495
17496 static struct type *
17497 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17498 {
17499 struct type *base_type, *cv_type;
17500
17501 base_type = die_type (die, cu);
17502
17503 /* The die_type call above may have already set the type for this DIE. */
17504 cv_type = get_die_type (die, cu);
17505 if (cv_type)
17506 return cv_type;
17507
17508 /* In case the const qualifier is applied to an array type, the element type
17509 is so qualified, not the array type (section 6.7.3 of C99). */
17510 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17511 return add_array_cv_type (die, cu, base_type, 1, 0);
17512
17513 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17514 return set_die_type (die, cv_type, cu);
17515 }
17516
17517 static struct type *
17518 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17519 {
17520 struct type *base_type, *cv_type;
17521
17522 base_type = die_type (die, cu);
17523
17524 /* The die_type call above may have already set the type for this DIE. */
17525 cv_type = get_die_type (die, cu);
17526 if (cv_type)
17527 return cv_type;
17528
17529 /* In case the volatile qualifier is applied to an array type, the
17530 element type is so qualified, not the array type (section 6.7.3
17531 of C99). */
17532 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17533 return add_array_cv_type (die, cu, base_type, 0, 1);
17534
17535 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17536 return set_die_type (die, cv_type, cu);
17537 }
17538
17539 /* Handle DW_TAG_restrict_type. */
17540
17541 static struct type *
17542 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17543 {
17544 struct type *base_type, *cv_type;
17545
17546 base_type = die_type (die, cu);
17547
17548 /* The die_type call above may have already set the type for this DIE. */
17549 cv_type = get_die_type (die, cu);
17550 if (cv_type)
17551 return cv_type;
17552
17553 cv_type = make_restrict_type (base_type);
17554 return set_die_type (die, cv_type, cu);
17555 }
17556
17557 /* Handle DW_TAG_atomic_type. */
17558
17559 static struct type *
17560 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17561 {
17562 struct type *base_type, *cv_type;
17563
17564 base_type = die_type (die, cu);
17565
17566 /* The die_type call above may have already set the type for this DIE. */
17567 cv_type = get_die_type (die, cu);
17568 if (cv_type)
17569 return cv_type;
17570
17571 cv_type = make_atomic_type (base_type);
17572 return set_die_type (die, cv_type, cu);
17573 }
17574
17575 /* Extract all information from a DW_TAG_string_type DIE and add to
17576 the user defined type vector. It isn't really a user defined type,
17577 but it behaves like one, with other DIE's using an AT_user_def_type
17578 attribute to reference it. */
17579
17580 static struct type *
17581 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17582 {
17583 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17584 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17585 struct type *type, *range_type, *index_type, *char_type;
17586 struct attribute *attr;
17587 unsigned int length;
17588
17589 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17590 if (attr)
17591 {
17592 length = DW_UNSND (attr);
17593 }
17594 else
17595 {
17596 /* Check for the DW_AT_byte_size attribute. */
17597 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17598 if (attr)
17599 {
17600 length = DW_UNSND (attr);
17601 }
17602 else
17603 {
17604 length = 1;
17605 }
17606 }
17607
17608 index_type = objfile_type (objfile)->builtin_int;
17609 range_type = create_static_range_type (NULL, index_type, 1, length);
17610 char_type = language_string_char_type (cu->language_defn, gdbarch);
17611 type = create_string_type (NULL, char_type, range_type);
17612
17613 return set_die_type (die, type, cu);
17614 }
17615
17616 /* Assuming that DIE corresponds to a function, returns nonzero
17617 if the function is prototyped. */
17618
17619 static int
17620 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17621 {
17622 struct attribute *attr;
17623
17624 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17625 if (attr && (DW_UNSND (attr) != 0))
17626 return 1;
17627
17628 /* The DWARF standard implies that the DW_AT_prototyped attribute
17629 is only meaninful for C, but the concept also extends to other
17630 languages that allow unprototyped functions (Eg: Objective C).
17631 For all other languages, assume that functions are always
17632 prototyped. */
17633 if (cu->language != language_c
17634 && cu->language != language_objc
17635 && cu->language != language_opencl)
17636 return 1;
17637
17638 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17639 prototyped and unprototyped functions; default to prototyped,
17640 since that is more common in modern code (and RealView warns
17641 about unprototyped functions). */
17642 if (producer_is_realview (cu->producer))
17643 return 1;
17644
17645 return 0;
17646 }
17647
17648 /* Handle DIES due to C code like:
17649
17650 struct foo
17651 {
17652 int (*funcp)(int a, long l);
17653 int b;
17654 };
17655
17656 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17657
17658 static struct type *
17659 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17660 {
17661 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17662 struct type *type; /* Type that this function returns. */
17663 struct type *ftype; /* Function that returns above type. */
17664 struct attribute *attr;
17665
17666 type = die_type (die, cu);
17667
17668 /* The die_type call above may have already set the type for this DIE. */
17669 ftype = get_die_type (die, cu);
17670 if (ftype)
17671 return ftype;
17672
17673 ftype = lookup_function_type (type);
17674
17675 if (prototyped_function_p (die, cu))
17676 TYPE_PROTOTYPED (ftype) = 1;
17677
17678 /* Store the calling convention in the type if it's available in
17679 the subroutine die. Otherwise set the calling convention to
17680 the default value DW_CC_normal. */
17681 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17682 if (attr)
17683 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17684 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17685 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17686 else
17687 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17688
17689 /* Record whether the function returns normally to its caller or not
17690 if the DWARF producer set that information. */
17691 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17692 if (attr && (DW_UNSND (attr) != 0))
17693 TYPE_NO_RETURN (ftype) = 1;
17694
17695 /* We need to add the subroutine type to the die immediately so
17696 we don't infinitely recurse when dealing with parameters
17697 declared as the same subroutine type. */
17698 set_die_type (die, ftype, cu);
17699
17700 if (die->child != NULL)
17701 {
17702 struct type *void_type = objfile_type (objfile)->builtin_void;
17703 struct die_info *child_die;
17704 int nparams, iparams;
17705
17706 /* Count the number of parameters.
17707 FIXME: GDB currently ignores vararg functions, but knows about
17708 vararg member functions. */
17709 nparams = 0;
17710 child_die = die->child;
17711 while (child_die && child_die->tag)
17712 {
17713 if (child_die->tag == DW_TAG_formal_parameter)
17714 nparams++;
17715 else if (child_die->tag == DW_TAG_unspecified_parameters)
17716 TYPE_VARARGS (ftype) = 1;
17717 child_die = sibling_die (child_die);
17718 }
17719
17720 /* Allocate storage for parameters and fill them in. */
17721 TYPE_NFIELDS (ftype) = nparams;
17722 TYPE_FIELDS (ftype) = (struct field *)
17723 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17724
17725 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17726 even if we error out during the parameters reading below. */
17727 for (iparams = 0; iparams < nparams; iparams++)
17728 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17729
17730 iparams = 0;
17731 child_die = die->child;
17732 while (child_die && child_die->tag)
17733 {
17734 if (child_die->tag == DW_TAG_formal_parameter)
17735 {
17736 struct type *arg_type;
17737
17738 /* DWARF version 2 has no clean way to discern C++
17739 static and non-static member functions. G++ helps
17740 GDB by marking the first parameter for non-static
17741 member functions (which is the this pointer) as
17742 artificial. We pass this information to
17743 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17744
17745 DWARF version 3 added DW_AT_object_pointer, which GCC
17746 4.5 does not yet generate. */
17747 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17748 if (attr)
17749 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17750 else
17751 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17752 arg_type = die_type (child_die, cu);
17753
17754 /* RealView does not mark THIS as const, which the testsuite
17755 expects. GCC marks THIS as const in method definitions,
17756 but not in the class specifications (GCC PR 43053). */
17757 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17758 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17759 {
17760 int is_this = 0;
17761 struct dwarf2_cu *arg_cu = cu;
17762 const char *name = dwarf2_name (child_die, cu);
17763
17764 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17765 if (attr)
17766 {
17767 /* If the compiler emits this, use it. */
17768 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17769 is_this = 1;
17770 }
17771 else if (name && strcmp (name, "this") == 0)
17772 /* Function definitions will have the argument names. */
17773 is_this = 1;
17774 else if (name == NULL && iparams == 0)
17775 /* Declarations may not have the names, so like
17776 elsewhere in GDB, assume an artificial first
17777 argument is "this". */
17778 is_this = 1;
17779
17780 if (is_this)
17781 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17782 arg_type, 0);
17783 }
17784
17785 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17786 iparams++;
17787 }
17788 child_die = sibling_die (child_die);
17789 }
17790 }
17791
17792 return ftype;
17793 }
17794
17795 static struct type *
17796 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17797 {
17798 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17799 const char *name = NULL;
17800 struct type *this_type, *target_type;
17801
17802 name = dwarf2_full_name (NULL, die, cu);
17803 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17804 TYPE_TARGET_STUB (this_type) = 1;
17805 set_die_type (die, this_type, cu);
17806 target_type = die_type (die, cu);
17807 if (target_type != this_type)
17808 TYPE_TARGET_TYPE (this_type) = target_type;
17809 else
17810 {
17811 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17812 spec and cause infinite loops in GDB. */
17813 complaint (&symfile_complaints,
17814 _("Self-referential DW_TAG_typedef "
17815 "- DIE at %s [in module %s]"),
17816 sect_offset_str (die->sect_off), objfile_name (objfile));
17817 TYPE_TARGET_TYPE (this_type) = NULL;
17818 }
17819 return this_type;
17820 }
17821
17822 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17823 (which may be different from NAME) to the architecture back-end to allow
17824 it to guess the correct format if necessary. */
17825
17826 static struct type *
17827 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17828 const char *name_hint)
17829 {
17830 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17831 const struct floatformat **format;
17832 struct type *type;
17833
17834 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17835 if (format)
17836 type = init_float_type (objfile, bits, name, format);
17837 else
17838 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17839
17840 return type;
17841 }
17842
17843 /* Find a representation of a given base type and install
17844 it in the TYPE field of the die. */
17845
17846 static struct type *
17847 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17848 {
17849 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17850 struct type *type;
17851 struct attribute *attr;
17852 int encoding = 0, bits = 0;
17853 const char *name;
17854
17855 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17856 if (attr)
17857 {
17858 encoding = DW_UNSND (attr);
17859 }
17860 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17861 if (attr)
17862 {
17863 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17864 }
17865 name = dwarf2_name (die, cu);
17866 if (!name)
17867 {
17868 complaint (&symfile_complaints,
17869 _("DW_AT_name missing from DW_TAG_base_type"));
17870 }
17871
17872 switch (encoding)
17873 {
17874 case DW_ATE_address:
17875 /* Turn DW_ATE_address into a void * pointer. */
17876 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17877 type = init_pointer_type (objfile, bits, name, type);
17878 break;
17879 case DW_ATE_boolean:
17880 type = init_boolean_type (objfile, bits, 1, name);
17881 break;
17882 case DW_ATE_complex_float:
17883 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17884 type = init_complex_type (objfile, name, type);
17885 break;
17886 case DW_ATE_decimal_float:
17887 type = init_decfloat_type (objfile, bits, name);
17888 break;
17889 case DW_ATE_float:
17890 type = dwarf2_init_float_type (objfile, bits, name, name);
17891 break;
17892 case DW_ATE_signed:
17893 type = init_integer_type (objfile, bits, 0, name);
17894 break;
17895 case DW_ATE_unsigned:
17896 if (cu->language == language_fortran
17897 && name
17898 && startswith (name, "character("))
17899 type = init_character_type (objfile, bits, 1, name);
17900 else
17901 type = init_integer_type (objfile, bits, 1, name);
17902 break;
17903 case DW_ATE_signed_char:
17904 if (cu->language == language_ada || cu->language == language_m2
17905 || cu->language == language_pascal
17906 || cu->language == language_fortran)
17907 type = init_character_type (objfile, bits, 0, name);
17908 else
17909 type = init_integer_type (objfile, bits, 0, name);
17910 break;
17911 case DW_ATE_unsigned_char:
17912 if (cu->language == language_ada || cu->language == language_m2
17913 || cu->language == language_pascal
17914 || cu->language == language_fortran
17915 || cu->language == language_rust)
17916 type = init_character_type (objfile, bits, 1, name);
17917 else
17918 type = init_integer_type (objfile, bits, 1, name);
17919 break;
17920 case DW_ATE_UTF:
17921 {
17922 gdbarch *arch = get_objfile_arch (objfile);
17923
17924 if (bits == 16)
17925 type = builtin_type (arch)->builtin_char16;
17926 else if (bits == 32)
17927 type = builtin_type (arch)->builtin_char32;
17928 else
17929 {
17930 complaint (&symfile_complaints,
17931 _("unsupported DW_ATE_UTF bit size: '%d'"),
17932 bits);
17933 type = init_integer_type (objfile, bits, 1, name);
17934 }
17935 return set_die_type (die, type, cu);
17936 }
17937 break;
17938
17939 default:
17940 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17941 dwarf_type_encoding_name (encoding));
17942 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17943 break;
17944 }
17945
17946 if (name && strcmp (name, "char") == 0)
17947 TYPE_NOSIGN (type) = 1;
17948
17949 return set_die_type (die, type, cu);
17950 }
17951
17952 /* Parse dwarf attribute if it's a block, reference or constant and put the
17953 resulting value of the attribute into struct bound_prop.
17954 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17955
17956 static int
17957 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17958 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17959 {
17960 struct dwarf2_property_baton *baton;
17961 struct obstack *obstack
17962 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17963
17964 if (attr == NULL || prop == NULL)
17965 return 0;
17966
17967 if (attr_form_is_block (attr))
17968 {
17969 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17970 baton->referenced_type = NULL;
17971 baton->locexpr.per_cu = cu->per_cu;
17972 baton->locexpr.size = DW_BLOCK (attr)->size;
17973 baton->locexpr.data = DW_BLOCK (attr)->data;
17974 prop->data.baton = baton;
17975 prop->kind = PROP_LOCEXPR;
17976 gdb_assert (prop->data.baton != NULL);
17977 }
17978 else if (attr_form_is_ref (attr))
17979 {
17980 struct dwarf2_cu *target_cu = cu;
17981 struct die_info *target_die;
17982 struct attribute *target_attr;
17983
17984 target_die = follow_die_ref (die, attr, &target_cu);
17985 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17986 if (target_attr == NULL)
17987 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17988 target_cu);
17989 if (target_attr == NULL)
17990 return 0;
17991
17992 switch (target_attr->name)
17993 {
17994 case DW_AT_location:
17995 if (attr_form_is_section_offset (target_attr))
17996 {
17997 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17998 baton->referenced_type = die_type (target_die, target_cu);
17999 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18000 prop->data.baton = baton;
18001 prop->kind = PROP_LOCLIST;
18002 gdb_assert (prop->data.baton != NULL);
18003 }
18004 else if (attr_form_is_block (target_attr))
18005 {
18006 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18007 baton->referenced_type = die_type (target_die, target_cu);
18008 baton->locexpr.per_cu = cu->per_cu;
18009 baton->locexpr.size = DW_BLOCK (target_attr)->size;
18010 baton->locexpr.data = DW_BLOCK (target_attr)->data;
18011 prop->data.baton = baton;
18012 prop->kind = PROP_LOCEXPR;
18013 gdb_assert (prop->data.baton != NULL);
18014 }
18015 else
18016 {
18017 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18018 "dynamic property");
18019 return 0;
18020 }
18021 break;
18022 case DW_AT_data_member_location:
18023 {
18024 LONGEST offset;
18025
18026 if (!handle_data_member_location (target_die, target_cu,
18027 &offset))
18028 return 0;
18029
18030 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18031 baton->referenced_type = read_type_die (target_die->parent,
18032 target_cu);
18033 baton->offset_info.offset = offset;
18034 baton->offset_info.type = die_type (target_die, target_cu);
18035 prop->data.baton = baton;
18036 prop->kind = PROP_ADDR_OFFSET;
18037 break;
18038 }
18039 }
18040 }
18041 else if (attr_form_is_constant (attr))
18042 {
18043 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
18044 prop->kind = PROP_CONST;
18045 }
18046 else
18047 {
18048 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18049 dwarf2_name (die, cu));
18050 return 0;
18051 }
18052
18053 return 1;
18054 }
18055
18056 /* Read the given DW_AT_subrange DIE. */
18057
18058 static struct type *
18059 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18060 {
18061 struct type *base_type, *orig_base_type;
18062 struct type *range_type;
18063 struct attribute *attr;
18064 struct dynamic_prop low, high;
18065 int low_default_is_valid;
18066 int high_bound_is_count = 0;
18067 const char *name;
18068 LONGEST negative_mask;
18069
18070 orig_base_type = die_type (die, cu);
18071 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18072 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18073 creating the range type, but we use the result of check_typedef
18074 when examining properties of the type. */
18075 base_type = check_typedef (orig_base_type);
18076
18077 /* The die_type call above may have already set the type for this DIE. */
18078 range_type = get_die_type (die, cu);
18079 if (range_type)
18080 return range_type;
18081
18082 low.kind = PROP_CONST;
18083 high.kind = PROP_CONST;
18084 high.data.const_val = 0;
18085
18086 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18087 omitting DW_AT_lower_bound. */
18088 switch (cu->language)
18089 {
18090 case language_c:
18091 case language_cplus:
18092 low.data.const_val = 0;
18093 low_default_is_valid = 1;
18094 break;
18095 case language_fortran:
18096 low.data.const_val = 1;
18097 low_default_is_valid = 1;
18098 break;
18099 case language_d:
18100 case language_objc:
18101 case language_rust:
18102 low.data.const_val = 0;
18103 low_default_is_valid = (cu->header.version >= 4);
18104 break;
18105 case language_ada:
18106 case language_m2:
18107 case language_pascal:
18108 low.data.const_val = 1;
18109 low_default_is_valid = (cu->header.version >= 4);
18110 break;
18111 default:
18112 low.data.const_val = 0;
18113 low_default_is_valid = 0;
18114 break;
18115 }
18116
18117 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18118 if (attr)
18119 attr_to_dynamic_prop (attr, die, cu, &low);
18120 else if (!low_default_is_valid)
18121 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
18122 "- DIE at %s [in module %s]"),
18123 sect_offset_str (die->sect_off),
18124 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18125
18126 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
18127 if (!attr_to_dynamic_prop (attr, die, cu, &high))
18128 {
18129 attr = dwarf2_attr (die, DW_AT_count, cu);
18130 if (attr_to_dynamic_prop (attr, die, cu, &high))
18131 {
18132 /* If bounds are constant do the final calculation here. */
18133 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18134 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18135 else
18136 high_bound_is_count = 1;
18137 }
18138 }
18139
18140 /* Dwarf-2 specifications explicitly allows to create subrange types
18141 without specifying a base type.
18142 In that case, the base type must be set to the type of
18143 the lower bound, upper bound or count, in that order, if any of these
18144 three attributes references an object that has a type.
18145 If no base type is found, the Dwarf-2 specifications say that
18146 a signed integer type of size equal to the size of an address should
18147 be used.
18148 For the following C code: `extern char gdb_int [];'
18149 GCC produces an empty range DIE.
18150 FIXME: muller/2010-05-28: Possible references to object for low bound,
18151 high bound or count are not yet handled by this code. */
18152 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
18153 {
18154 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18155 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18156 int addr_size = gdbarch_addr_bit (gdbarch) /8;
18157 struct type *int_type = objfile_type (objfile)->builtin_int;
18158
18159 /* Test "int", "long int", and "long long int" objfile types,
18160 and select the first one having a size above or equal to the
18161 architecture address size. */
18162 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18163 base_type = int_type;
18164 else
18165 {
18166 int_type = objfile_type (objfile)->builtin_long;
18167 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18168 base_type = int_type;
18169 else
18170 {
18171 int_type = objfile_type (objfile)->builtin_long_long;
18172 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18173 base_type = int_type;
18174 }
18175 }
18176 }
18177
18178 /* Normally, the DWARF producers are expected to use a signed
18179 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18180 But this is unfortunately not always the case, as witnessed
18181 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18182 is used instead. To work around that ambiguity, we treat
18183 the bounds as signed, and thus sign-extend their values, when
18184 the base type is signed. */
18185 negative_mask =
18186 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18187 if (low.kind == PROP_CONST
18188 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18189 low.data.const_val |= negative_mask;
18190 if (high.kind == PROP_CONST
18191 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18192 high.data.const_val |= negative_mask;
18193
18194 range_type = create_range_type (NULL, orig_base_type, &low, &high);
18195
18196 if (high_bound_is_count)
18197 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18198
18199 /* Ada expects an empty array on no boundary attributes. */
18200 if (attr == NULL && cu->language != language_ada)
18201 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
18202
18203 name = dwarf2_name (die, cu);
18204 if (name)
18205 TYPE_NAME (range_type) = name;
18206
18207 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18208 if (attr)
18209 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18210
18211 set_die_type (die, range_type, cu);
18212
18213 /* set_die_type should be already done. */
18214 set_descriptive_type (range_type, die, cu);
18215
18216 return range_type;
18217 }
18218
18219 static struct type *
18220 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18221 {
18222 struct type *type;
18223
18224 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18225 NULL);
18226 TYPE_NAME (type) = dwarf2_name (die, cu);
18227
18228 /* In Ada, an unspecified type is typically used when the description
18229 of the type is defered to a different unit. When encountering
18230 such a type, we treat it as a stub, and try to resolve it later on,
18231 when needed. */
18232 if (cu->language == language_ada)
18233 TYPE_STUB (type) = 1;
18234
18235 return set_die_type (die, type, cu);
18236 }
18237
18238 /* Read a single die and all its descendents. Set the die's sibling
18239 field to NULL; set other fields in the die correctly, and set all
18240 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18241 location of the info_ptr after reading all of those dies. PARENT
18242 is the parent of the die in question. */
18243
18244 static struct die_info *
18245 read_die_and_children (const struct die_reader_specs *reader,
18246 const gdb_byte *info_ptr,
18247 const gdb_byte **new_info_ptr,
18248 struct die_info *parent)
18249 {
18250 struct die_info *die;
18251 const gdb_byte *cur_ptr;
18252 int has_children;
18253
18254 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18255 if (die == NULL)
18256 {
18257 *new_info_ptr = cur_ptr;
18258 return NULL;
18259 }
18260 store_in_ref_table (die, reader->cu);
18261
18262 if (has_children)
18263 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18264 else
18265 {
18266 die->child = NULL;
18267 *new_info_ptr = cur_ptr;
18268 }
18269
18270 die->sibling = NULL;
18271 die->parent = parent;
18272 return die;
18273 }
18274
18275 /* Read a die, all of its descendents, and all of its siblings; set
18276 all of the fields of all of the dies correctly. Arguments are as
18277 in read_die_and_children. */
18278
18279 static struct die_info *
18280 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18281 const gdb_byte *info_ptr,
18282 const gdb_byte **new_info_ptr,
18283 struct die_info *parent)
18284 {
18285 struct die_info *first_die, *last_sibling;
18286 const gdb_byte *cur_ptr;
18287
18288 cur_ptr = info_ptr;
18289 first_die = last_sibling = NULL;
18290
18291 while (1)
18292 {
18293 struct die_info *die
18294 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18295
18296 if (die == NULL)
18297 {
18298 *new_info_ptr = cur_ptr;
18299 return first_die;
18300 }
18301
18302 if (!first_die)
18303 first_die = die;
18304 else
18305 last_sibling->sibling = die;
18306
18307 last_sibling = die;
18308 }
18309 }
18310
18311 /* Read a die, all of its descendents, and all of its siblings; set
18312 all of the fields of all of the dies correctly. Arguments are as
18313 in read_die_and_children.
18314 This the main entry point for reading a DIE and all its children. */
18315
18316 static struct die_info *
18317 read_die_and_siblings (const struct die_reader_specs *reader,
18318 const gdb_byte *info_ptr,
18319 const gdb_byte **new_info_ptr,
18320 struct die_info *parent)
18321 {
18322 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18323 new_info_ptr, parent);
18324
18325 if (dwarf_die_debug)
18326 {
18327 fprintf_unfiltered (gdb_stdlog,
18328 "Read die from %s@0x%x of %s:\n",
18329 get_section_name (reader->die_section),
18330 (unsigned) (info_ptr - reader->die_section->buffer),
18331 bfd_get_filename (reader->abfd));
18332 dump_die (die, dwarf_die_debug);
18333 }
18334
18335 return die;
18336 }
18337
18338 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18339 attributes.
18340 The caller is responsible for filling in the extra attributes
18341 and updating (*DIEP)->num_attrs.
18342 Set DIEP to point to a newly allocated die with its information,
18343 except for its child, sibling, and parent fields.
18344 Set HAS_CHILDREN to tell whether the die has children or not. */
18345
18346 static const gdb_byte *
18347 read_full_die_1 (const struct die_reader_specs *reader,
18348 struct die_info **diep, const gdb_byte *info_ptr,
18349 int *has_children, int num_extra_attrs)
18350 {
18351 unsigned int abbrev_number, bytes_read, i;
18352 struct abbrev_info *abbrev;
18353 struct die_info *die;
18354 struct dwarf2_cu *cu = reader->cu;
18355 bfd *abfd = reader->abfd;
18356
18357 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18358 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18359 info_ptr += bytes_read;
18360 if (!abbrev_number)
18361 {
18362 *diep = NULL;
18363 *has_children = 0;
18364 return info_ptr;
18365 }
18366
18367 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18368 if (!abbrev)
18369 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18370 abbrev_number,
18371 bfd_get_filename (abfd));
18372
18373 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18374 die->sect_off = sect_off;
18375 die->tag = abbrev->tag;
18376 die->abbrev = abbrev_number;
18377
18378 /* Make the result usable.
18379 The caller needs to update num_attrs after adding the extra
18380 attributes. */
18381 die->num_attrs = abbrev->num_attrs;
18382
18383 for (i = 0; i < abbrev->num_attrs; ++i)
18384 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18385 info_ptr);
18386
18387 *diep = die;
18388 *has_children = abbrev->has_children;
18389 return info_ptr;
18390 }
18391
18392 /* Read a die and all its attributes.
18393 Set DIEP to point to a newly allocated die with its information,
18394 except for its child, sibling, and parent fields.
18395 Set HAS_CHILDREN to tell whether the die has children or not. */
18396
18397 static const gdb_byte *
18398 read_full_die (const struct die_reader_specs *reader,
18399 struct die_info **diep, const gdb_byte *info_ptr,
18400 int *has_children)
18401 {
18402 const gdb_byte *result;
18403
18404 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18405
18406 if (dwarf_die_debug)
18407 {
18408 fprintf_unfiltered (gdb_stdlog,
18409 "Read die from %s@0x%x of %s:\n",
18410 get_section_name (reader->die_section),
18411 (unsigned) (info_ptr - reader->die_section->buffer),
18412 bfd_get_filename (reader->abfd));
18413 dump_die (*diep, dwarf_die_debug);
18414 }
18415
18416 return result;
18417 }
18418 \f
18419 /* Abbreviation tables.
18420
18421 In DWARF version 2, the description of the debugging information is
18422 stored in a separate .debug_abbrev section. Before we read any
18423 dies from a section we read in all abbreviations and install them
18424 in a hash table. */
18425
18426 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18427
18428 struct abbrev_info *
18429 abbrev_table::alloc_abbrev ()
18430 {
18431 struct abbrev_info *abbrev;
18432
18433 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18434 memset (abbrev, 0, sizeof (struct abbrev_info));
18435
18436 return abbrev;
18437 }
18438
18439 /* Add an abbreviation to the table. */
18440
18441 void
18442 abbrev_table::add_abbrev (unsigned int abbrev_number,
18443 struct abbrev_info *abbrev)
18444 {
18445 unsigned int hash_number;
18446
18447 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18448 abbrev->next = m_abbrevs[hash_number];
18449 m_abbrevs[hash_number] = abbrev;
18450 }
18451
18452 /* Look up an abbrev in the table.
18453 Returns NULL if the abbrev is not found. */
18454
18455 struct abbrev_info *
18456 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18457 {
18458 unsigned int hash_number;
18459 struct abbrev_info *abbrev;
18460
18461 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18462 abbrev = m_abbrevs[hash_number];
18463
18464 while (abbrev)
18465 {
18466 if (abbrev->number == abbrev_number)
18467 return abbrev;
18468 abbrev = abbrev->next;
18469 }
18470 return NULL;
18471 }
18472
18473 /* Read in an abbrev table. */
18474
18475 static abbrev_table_up
18476 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18477 struct dwarf2_section_info *section,
18478 sect_offset sect_off)
18479 {
18480 struct objfile *objfile = dwarf2_per_objfile->objfile;
18481 bfd *abfd = get_section_bfd_owner (section);
18482 const gdb_byte *abbrev_ptr;
18483 struct abbrev_info *cur_abbrev;
18484 unsigned int abbrev_number, bytes_read, abbrev_name;
18485 unsigned int abbrev_form;
18486 struct attr_abbrev *cur_attrs;
18487 unsigned int allocated_attrs;
18488
18489 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18490
18491 dwarf2_read_section (objfile, section);
18492 abbrev_ptr = section->buffer + to_underlying (sect_off);
18493 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18494 abbrev_ptr += bytes_read;
18495
18496 allocated_attrs = ATTR_ALLOC_CHUNK;
18497 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18498
18499 /* Loop until we reach an abbrev number of 0. */
18500 while (abbrev_number)
18501 {
18502 cur_abbrev = abbrev_table->alloc_abbrev ();
18503
18504 /* read in abbrev header */
18505 cur_abbrev->number = abbrev_number;
18506 cur_abbrev->tag
18507 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18508 abbrev_ptr += bytes_read;
18509 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18510 abbrev_ptr += 1;
18511
18512 /* now read in declarations */
18513 for (;;)
18514 {
18515 LONGEST implicit_const;
18516
18517 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18518 abbrev_ptr += bytes_read;
18519 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18520 abbrev_ptr += bytes_read;
18521 if (abbrev_form == DW_FORM_implicit_const)
18522 {
18523 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18524 &bytes_read);
18525 abbrev_ptr += bytes_read;
18526 }
18527 else
18528 {
18529 /* Initialize it due to a false compiler warning. */
18530 implicit_const = -1;
18531 }
18532
18533 if (abbrev_name == 0)
18534 break;
18535
18536 if (cur_abbrev->num_attrs == allocated_attrs)
18537 {
18538 allocated_attrs += ATTR_ALLOC_CHUNK;
18539 cur_attrs
18540 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18541 }
18542
18543 cur_attrs[cur_abbrev->num_attrs].name
18544 = (enum dwarf_attribute) abbrev_name;
18545 cur_attrs[cur_abbrev->num_attrs].form
18546 = (enum dwarf_form) abbrev_form;
18547 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18548 ++cur_abbrev->num_attrs;
18549 }
18550
18551 cur_abbrev->attrs =
18552 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18553 cur_abbrev->num_attrs);
18554 memcpy (cur_abbrev->attrs, cur_attrs,
18555 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18556
18557 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18558
18559 /* Get next abbreviation.
18560 Under Irix6 the abbreviations for a compilation unit are not
18561 always properly terminated with an abbrev number of 0.
18562 Exit loop if we encounter an abbreviation which we have
18563 already read (which means we are about to read the abbreviations
18564 for the next compile unit) or if the end of the abbreviation
18565 table is reached. */
18566 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18567 break;
18568 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18569 abbrev_ptr += bytes_read;
18570 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18571 break;
18572 }
18573
18574 xfree (cur_attrs);
18575 return abbrev_table;
18576 }
18577
18578 /* Returns nonzero if TAG represents a type that we might generate a partial
18579 symbol for. */
18580
18581 static int
18582 is_type_tag_for_partial (int tag)
18583 {
18584 switch (tag)
18585 {
18586 #if 0
18587 /* Some types that would be reasonable to generate partial symbols for,
18588 that we don't at present. */
18589 case DW_TAG_array_type:
18590 case DW_TAG_file_type:
18591 case DW_TAG_ptr_to_member_type:
18592 case DW_TAG_set_type:
18593 case DW_TAG_string_type:
18594 case DW_TAG_subroutine_type:
18595 #endif
18596 case DW_TAG_base_type:
18597 case DW_TAG_class_type:
18598 case DW_TAG_interface_type:
18599 case DW_TAG_enumeration_type:
18600 case DW_TAG_structure_type:
18601 case DW_TAG_subrange_type:
18602 case DW_TAG_typedef:
18603 case DW_TAG_union_type:
18604 return 1;
18605 default:
18606 return 0;
18607 }
18608 }
18609
18610 /* Load all DIEs that are interesting for partial symbols into memory. */
18611
18612 static struct partial_die_info *
18613 load_partial_dies (const struct die_reader_specs *reader,
18614 const gdb_byte *info_ptr, int building_psymtab)
18615 {
18616 struct dwarf2_cu *cu = reader->cu;
18617 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18618 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18619 unsigned int bytes_read;
18620 unsigned int load_all = 0;
18621 int nesting_level = 1;
18622
18623 parent_die = NULL;
18624 last_die = NULL;
18625
18626 gdb_assert (cu->per_cu != NULL);
18627 if (cu->per_cu->load_all_dies)
18628 load_all = 1;
18629
18630 cu->partial_dies
18631 = htab_create_alloc_ex (cu->header.length / 12,
18632 partial_die_hash,
18633 partial_die_eq,
18634 NULL,
18635 &cu->comp_unit_obstack,
18636 hashtab_obstack_allocate,
18637 dummy_obstack_deallocate);
18638
18639 while (1)
18640 {
18641 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18642
18643 /* A NULL abbrev means the end of a series of children. */
18644 if (abbrev == NULL)
18645 {
18646 if (--nesting_level == 0)
18647 return first_die;
18648
18649 info_ptr += bytes_read;
18650 last_die = parent_die;
18651 parent_die = parent_die->die_parent;
18652 continue;
18653 }
18654
18655 /* Check for template arguments. We never save these; if
18656 they're seen, we just mark the parent, and go on our way. */
18657 if (parent_die != NULL
18658 && cu->language == language_cplus
18659 && (abbrev->tag == DW_TAG_template_type_param
18660 || abbrev->tag == DW_TAG_template_value_param))
18661 {
18662 parent_die->has_template_arguments = 1;
18663
18664 if (!load_all)
18665 {
18666 /* We don't need a partial DIE for the template argument. */
18667 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18668 continue;
18669 }
18670 }
18671
18672 /* We only recurse into c++ subprograms looking for template arguments.
18673 Skip their other children. */
18674 if (!load_all
18675 && cu->language == language_cplus
18676 && parent_die != NULL
18677 && parent_die->tag == DW_TAG_subprogram)
18678 {
18679 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18680 continue;
18681 }
18682
18683 /* Check whether this DIE is interesting enough to save. Normally
18684 we would not be interested in members here, but there may be
18685 later variables referencing them via DW_AT_specification (for
18686 static members). */
18687 if (!load_all
18688 && !is_type_tag_for_partial (abbrev->tag)
18689 && abbrev->tag != DW_TAG_constant
18690 && abbrev->tag != DW_TAG_enumerator
18691 && abbrev->tag != DW_TAG_subprogram
18692 && abbrev->tag != DW_TAG_inlined_subroutine
18693 && abbrev->tag != DW_TAG_lexical_block
18694 && abbrev->tag != DW_TAG_variable
18695 && abbrev->tag != DW_TAG_namespace
18696 && abbrev->tag != DW_TAG_module
18697 && abbrev->tag != DW_TAG_member
18698 && abbrev->tag != DW_TAG_imported_unit
18699 && abbrev->tag != DW_TAG_imported_declaration)
18700 {
18701 /* Otherwise we skip to the next sibling, if any. */
18702 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18703 continue;
18704 }
18705
18706 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18707 abbrev);
18708
18709 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18710
18711 /* This two-pass algorithm for processing partial symbols has a
18712 high cost in cache pressure. Thus, handle some simple cases
18713 here which cover the majority of C partial symbols. DIEs
18714 which neither have specification tags in them, nor could have
18715 specification tags elsewhere pointing at them, can simply be
18716 processed and discarded.
18717
18718 This segment is also optional; scan_partial_symbols and
18719 add_partial_symbol will handle these DIEs if we chain
18720 them in normally. When compilers which do not emit large
18721 quantities of duplicate debug information are more common,
18722 this code can probably be removed. */
18723
18724 /* Any complete simple types at the top level (pretty much all
18725 of them, for a language without namespaces), can be processed
18726 directly. */
18727 if (parent_die == NULL
18728 && pdi.has_specification == 0
18729 && pdi.is_declaration == 0
18730 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18731 || pdi.tag == DW_TAG_base_type
18732 || pdi.tag == DW_TAG_subrange_type))
18733 {
18734 if (building_psymtab && pdi.name != NULL)
18735 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18736 VAR_DOMAIN, LOC_TYPEDEF,
18737 &objfile->static_psymbols,
18738 0, cu->language, objfile);
18739 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18740 continue;
18741 }
18742
18743 /* The exception for DW_TAG_typedef with has_children above is
18744 a workaround of GCC PR debug/47510. In the case of this complaint
18745 type_name_no_tag_or_error will error on such types later.
18746
18747 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18748 it could not find the child DIEs referenced later, this is checked
18749 above. In correct DWARF DW_TAG_typedef should have no children. */
18750
18751 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18752 complaint (&symfile_complaints,
18753 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18754 "- DIE at %s [in module %s]"),
18755 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18756
18757 /* If we're at the second level, and we're an enumerator, and
18758 our parent has no specification (meaning possibly lives in a
18759 namespace elsewhere), then we can add the partial symbol now
18760 instead of queueing it. */
18761 if (pdi.tag == DW_TAG_enumerator
18762 && parent_die != NULL
18763 && parent_die->die_parent == NULL
18764 && parent_die->tag == DW_TAG_enumeration_type
18765 && parent_die->has_specification == 0)
18766 {
18767 if (pdi.name == NULL)
18768 complaint (&symfile_complaints,
18769 _("malformed enumerator DIE ignored"));
18770 else if (building_psymtab)
18771 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18772 VAR_DOMAIN, LOC_CONST,
18773 cu->language == language_cplus
18774 ? &objfile->global_psymbols
18775 : &objfile->static_psymbols,
18776 0, cu->language, objfile);
18777
18778 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18779 continue;
18780 }
18781
18782 struct partial_die_info *part_die
18783 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18784
18785 /* We'll save this DIE so link it in. */
18786 part_die->die_parent = parent_die;
18787 part_die->die_sibling = NULL;
18788 part_die->die_child = NULL;
18789
18790 if (last_die && last_die == parent_die)
18791 last_die->die_child = part_die;
18792 else if (last_die)
18793 last_die->die_sibling = part_die;
18794
18795 last_die = part_die;
18796
18797 if (first_die == NULL)
18798 first_die = part_die;
18799
18800 /* Maybe add the DIE to the hash table. Not all DIEs that we
18801 find interesting need to be in the hash table, because we
18802 also have the parent/sibling/child chains; only those that we
18803 might refer to by offset later during partial symbol reading.
18804
18805 For now this means things that might have be the target of a
18806 DW_AT_specification, DW_AT_abstract_origin, or
18807 DW_AT_extension. DW_AT_extension will refer only to
18808 namespaces; DW_AT_abstract_origin refers to functions (and
18809 many things under the function DIE, but we do not recurse
18810 into function DIEs during partial symbol reading) and
18811 possibly variables as well; DW_AT_specification refers to
18812 declarations. Declarations ought to have the DW_AT_declaration
18813 flag. It happens that GCC forgets to put it in sometimes, but
18814 only for functions, not for types.
18815
18816 Adding more things than necessary to the hash table is harmless
18817 except for the performance cost. Adding too few will result in
18818 wasted time in find_partial_die, when we reread the compilation
18819 unit with load_all_dies set. */
18820
18821 if (load_all
18822 || abbrev->tag == DW_TAG_constant
18823 || abbrev->tag == DW_TAG_subprogram
18824 || abbrev->tag == DW_TAG_variable
18825 || abbrev->tag == DW_TAG_namespace
18826 || part_die->is_declaration)
18827 {
18828 void **slot;
18829
18830 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18831 to_underlying (part_die->sect_off),
18832 INSERT);
18833 *slot = part_die;
18834 }
18835
18836 /* For some DIEs we want to follow their children (if any). For C
18837 we have no reason to follow the children of structures; for other
18838 languages we have to, so that we can get at method physnames
18839 to infer fully qualified class names, for DW_AT_specification,
18840 and for C++ template arguments. For C++, we also look one level
18841 inside functions to find template arguments (if the name of the
18842 function does not already contain the template arguments).
18843
18844 For Ada, we need to scan the children of subprograms and lexical
18845 blocks as well because Ada allows the definition of nested
18846 entities that could be interesting for the debugger, such as
18847 nested subprograms for instance. */
18848 if (last_die->has_children
18849 && (load_all
18850 || last_die->tag == DW_TAG_namespace
18851 || last_die->tag == DW_TAG_module
18852 || last_die->tag == DW_TAG_enumeration_type
18853 || (cu->language == language_cplus
18854 && last_die->tag == DW_TAG_subprogram
18855 && (last_die->name == NULL
18856 || strchr (last_die->name, '<') == NULL))
18857 || (cu->language != language_c
18858 && (last_die->tag == DW_TAG_class_type
18859 || last_die->tag == DW_TAG_interface_type
18860 || last_die->tag == DW_TAG_structure_type
18861 || last_die->tag == DW_TAG_union_type))
18862 || (cu->language == language_ada
18863 && (last_die->tag == DW_TAG_subprogram
18864 || last_die->tag == DW_TAG_lexical_block))))
18865 {
18866 nesting_level++;
18867 parent_die = last_die;
18868 continue;
18869 }
18870
18871 /* Otherwise we skip to the next sibling, if any. */
18872 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18873
18874 /* Back to the top, do it again. */
18875 }
18876 }
18877
18878 partial_die_info::partial_die_info (sect_offset sect_off_,
18879 struct abbrev_info *abbrev)
18880 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18881 {
18882 }
18883
18884 /* Read a minimal amount of information into the minimal die structure.
18885 INFO_PTR should point just after the initial uleb128 of a DIE. */
18886
18887 const gdb_byte *
18888 partial_die_info::read (const struct die_reader_specs *reader,
18889 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18890 {
18891 struct dwarf2_cu *cu = reader->cu;
18892 struct dwarf2_per_objfile *dwarf2_per_objfile
18893 = cu->per_cu->dwarf2_per_objfile;
18894 unsigned int i;
18895 int has_low_pc_attr = 0;
18896 int has_high_pc_attr = 0;
18897 int high_pc_relative = 0;
18898
18899 for (i = 0; i < abbrev.num_attrs; ++i)
18900 {
18901 struct attribute attr;
18902
18903 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18904
18905 /* Store the data if it is of an attribute we want to keep in a
18906 partial symbol table. */
18907 switch (attr.name)
18908 {
18909 case DW_AT_name:
18910 switch (tag)
18911 {
18912 case DW_TAG_compile_unit:
18913 case DW_TAG_partial_unit:
18914 case DW_TAG_type_unit:
18915 /* Compilation units have a DW_AT_name that is a filename, not
18916 a source language identifier. */
18917 case DW_TAG_enumeration_type:
18918 case DW_TAG_enumerator:
18919 /* These tags always have simple identifiers already; no need
18920 to canonicalize them. */
18921 name = DW_STRING (&attr);
18922 break;
18923 default:
18924 {
18925 struct objfile *objfile = dwarf2_per_objfile->objfile;
18926
18927 name
18928 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18929 &objfile->per_bfd->storage_obstack);
18930 }
18931 break;
18932 }
18933 break;
18934 case DW_AT_linkage_name:
18935 case DW_AT_MIPS_linkage_name:
18936 /* Note that both forms of linkage name might appear. We
18937 assume they will be the same, and we only store the last
18938 one we see. */
18939 if (cu->language == language_ada)
18940 name = DW_STRING (&attr);
18941 linkage_name = DW_STRING (&attr);
18942 break;
18943 case DW_AT_low_pc:
18944 has_low_pc_attr = 1;
18945 lowpc = attr_value_as_address (&attr);
18946 break;
18947 case DW_AT_high_pc:
18948 has_high_pc_attr = 1;
18949 highpc = attr_value_as_address (&attr);
18950 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18951 high_pc_relative = 1;
18952 break;
18953 case DW_AT_location:
18954 /* Support the .debug_loc offsets. */
18955 if (attr_form_is_block (&attr))
18956 {
18957 d.locdesc = DW_BLOCK (&attr);
18958 }
18959 else if (attr_form_is_section_offset (&attr))
18960 {
18961 dwarf2_complex_location_expr_complaint ();
18962 }
18963 else
18964 {
18965 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18966 "partial symbol information");
18967 }
18968 break;
18969 case DW_AT_external:
18970 is_external = DW_UNSND (&attr);
18971 break;
18972 case DW_AT_declaration:
18973 is_declaration = DW_UNSND (&attr);
18974 break;
18975 case DW_AT_type:
18976 has_type = 1;
18977 break;
18978 case DW_AT_abstract_origin:
18979 case DW_AT_specification:
18980 case DW_AT_extension:
18981 has_specification = 1;
18982 spec_offset = dwarf2_get_ref_die_offset (&attr);
18983 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18984 || cu->per_cu->is_dwz);
18985 break;
18986 case DW_AT_sibling:
18987 /* Ignore absolute siblings, they might point outside of
18988 the current compile unit. */
18989 if (attr.form == DW_FORM_ref_addr)
18990 complaint (&symfile_complaints,
18991 _("ignoring absolute DW_AT_sibling"));
18992 else
18993 {
18994 const gdb_byte *buffer = reader->buffer;
18995 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18996 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18997
18998 if (sibling_ptr < info_ptr)
18999 complaint (&symfile_complaints,
19000 _("DW_AT_sibling points backwards"));
19001 else if (sibling_ptr > reader->buffer_end)
19002 dwarf2_section_buffer_overflow_complaint (reader->die_section);
19003 else
19004 sibling = sibling_ptr;
19005 }
19006 break;
19007 case DW_AT_byte_size:
19008 has_byte_size = 1;
19009 break;
19010 case DW_AT_const_value:
19011 has_const_value = 1;
19012 break;
19013 case DW_AT_calling_convention:
19014 /* DWARF doesn't provide a way to identify a program's source-level
19015 entry point. DW_AT_calling_convention attributes are only meant
19016 to describe functions' calling conventions.
19017
19018 However, because it's a necessary piece of information in
19019 Fortran, and before DWARF 4 DW_CC_program was the only
19020 piece of debugging information whose definition refers to
19021 a 'main program' at all, several compilers marked Fortran
19022 main programs with DW_CC_program --- even when those
19023 functions use the standard calling conventions.
19024
19025 Although DWARF now specifies a way to provide this
19026 information, we support this practice for backward
19027 compatibility. */
19028 if (DW_UNSND (&attr) == DW_CC_program
19029 && cu->language == language_fortran)
19030 main_subprogram = 1;
19031 break;
19032 case DW_AT_inline:
19033 if (DW_UNSND (&attr) == DW_INL_inlined
19034 || DW_UNSND (&attr) == DW_INL_declared_inlined)
19035 may_be_inlined = 1;
19036 break;
19037
19038 case DW_AT_import:
19039 if (tag == DW_TAG_imported_unit)
19040 {
19041 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19042 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19043 || cu->per_cu->is_dwz);
19044 }
19045 break;
19046
19047 case DW_AT_main_subprogram:
19048 main_subprogram = DW_UNSND (&attr);
19049 break;
19050
19051 default:
19052 break;
19053 }
19054 }
19055
19056 if (high_pc_relative)
19057 highpc += lowpc;
19058
19059 if (has_low_pc_attr && has_high_pc_attr)
19060 {
19061 /* When using the GNU linker, .gnu.linkonce. sections are used to
19062 eliminate duplicate copies of functions and vtables and such.
19063 The linker will arbitrarily choose one and discard the others.
19064 The AT_*_pc values for such functions refer to local labels in
19065 these sections. If the section from that file was discarded, the
19066 labels are not in the output, so the relocs get a value of 0.
19067 If this is a discarded function, mark the pc bounds as invalid,
19068 so that GDB will ignore it. */
19069 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
19070 {
19071 struct objfile *objfile = dwarf2_per_objfile->objfile;
19072 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19073
19074 complaint (&symfile_complaints,
19075 _("DW_AT_low_pc %s is zero "
19076 "for DIE at %s [in module %s]"),
19077 paddress (gdbarch, lowpc),
19078 sect_offset_str (sect_off),
19079 objfile_name (objfile));
19080 }
19081 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19082 else if (lowpc >= highpc)
19083 {
19084 struct objfile *objfile = dwarf2_per_objfile->objfile;
19085 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19086
19087 complaint (&symfile_complaints,
19088 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19089 "for DIE at %s [in module %s]"),
19090 paddress (gdbarch, lowpc),
19091 paddress (gdbarch, highpc),
19092 sect_offset_str (sect_off),
19093 objfile_name (objfile));
19094 }
19095 else
19096 has_pc_info = 1;
19097 }
19098
19099 return info_ptr;
19100 }
19101
19102 /* Find a cached partial DIE at OFFSET in CU. */
19103
19104 struct partial_die_info *
19105 dwarf2_cu::find_partial_die (sect_offset sect_off)
19106 {
19107 struct partial_die_info *lookup_die = NULL;
19108 struct partial_die_info part_die (sect_off);
19109
19110 lookup_die = ((struct partial_die_info *)
19111 htab_find_with_hash (partial_dies, &part_die,
19112 to_underlying (sect_off)));
19113
19114 return lookup_die;
19115 }
19116
19117 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19118 except in the case of .debug_types DIEs which do not reference
19119 outside their CU (they do however referencing other types via
19120 DW_FORM_ref_sig8). */
19121
19122 static struct partial_die_info *
19123 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19124 {
19125 struct dwarf2_per_objfile *dwarf2_per_objfile
19126 = cu->per_cu->dwarf2_per_objfile;
19127 struct objfile *objfile = dwarf2_per_objfile->objfile;
19128 struct dwarf2_per_cu_data *per_cu = NULL;
19129 struct partial_die_info *pd = NULL;
19130
19131 if (offset_in_dwz == cu->per_cu->is_dwz
19132 && offset_in_cu_p (&cu->header, sect_off))
19133 {
19134 pd = cu->find_partial_die (sect_off);
19135 if (pd != NULL)
19136 return pd;
19137 /* We missed recording what we needed.
19138 Load all dies and try again. */
19139 per_cu = cu->per_cu;
19140 }
19141 else
19142 {
19143 /* TUs don't reference other CUs/TUs (except via type signatures). */
19144 if (cu->per_cu->is_debug_types)
19145 {
19146 error (_("Dwarf Error: Type Unit at offset %s contains"
19147 " external reference to offset %s [in module %s].\n"),
19148 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19149 bfd_get_filename (objfile->obfd));
19150 }
19151 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19152 dwarf2_per_objfile);
19153
19154 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19155 load_partial_comp_unit (per_cu);
19156
19157 per_cu->cu->last_used = 0;
19158 pd = per_cu->cu->find_partial_die (sect_off);
19159 }
19160
19161 /* If we didn't find it, and not all dies have been loaded,
19162 load them all and try again. */
19163
19164 if (pd == NULL && per_cu->load_all_dies == 0)
19165 {
19166 per_cu->load_all_dies = 1;
19167
19168 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19169 THIS_CU->cu may already be in use. So we can't just free it and
19170 replace its DIEs with the ones we read in. Instead, we leave those
19171 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19172 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19173 set. */
19174 load_partial_comp_unit (per_cu);
19175
19176 pd = per_cu->cu->find_partial_die (sect_off);
19177 }
19178
19179 if (pd == NULL)
19180 internal_error (__FILE__, __LINE__,
19181 _("could not find partial DIE %s "
19182 "in cache [from module %s]\n"),
19183 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19184 return pd;
19185 }
19186
19187 /* See if we can figure out if the class lives in a namespace. We do
19188 this by looking for a member function; its demangled name will
19189 contain namespace info, if there is any. */
19190
19191 static void
19192 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19193 struct dwarf2_cu *cu)
19194 {
19195 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19196 what template types look like, because the demangler
19197 frequently doesn't give the same name as the debug info. We
19198 could fix this by only using the demangled name to get the
19199 prefix (but see comment in read_structure_type). */
19200
19201 struct partial_die_info *real_pdi;
19202 struct partial_die_info *child_pdi;
19203
19204 /* If this DIE (this DIE's specification, if any) has a parent, then
19205 we should not do this. We'll prepend the parent's fully qualified
19206 name when we create the partial symbol. */
19207
19208 real_pdi = struct_pdi;
19209 while (real_pdi->has_specification)
19210 real_pdi = find_partial_die (real_pdi->spec_offset,
19211 real_pdi->spec_is_dwz, cu);
19212
19213 if (real_pdi->die_parent != NULL)
19214 return;
19215
19216 for (child_pdi = struct_pdi->die_child;
19217 child_pdi != NULL;
19218 child_pdi = child_pdi->die_sibling)
19219 {
19220 if (child_pdi->tag == DW_TAG_subprogram
19221 && child_pdi->linkage_name != NULL)
19222 {
19223 char *actual_class_name
19224 = language_class_name_from_physname (cu->language_defn,
19225 child_pdi->linkage_name);
19226 if (actual_class_name != NULL)
19227 {
19228 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19229 struct_pdi->name
19230 = ((const char *)
19231 obstack_copy0 (&objfile->per_bfd->storage_obstack,
19232 actual_class_name,
19233 strlen (actual_class_name)));
19234 xfree (actual_class_name);
19235 }
19236 break;
19237 }
19238 }
19239 }
19240
19241 void
19242 partial_die_info::fixup (struct dwarf2_cu *cu)
19243 {
19244 /* Once we've fixed up a die, there's no point in doing so again.
19245 This also avoids a memory leak if we were to call
19246 guess_partial_die_structure_name multiple times. */
19247 if (fixup_called)
19248 return;
19249
19250 /* If we found a reference attribute and the DIE has no name, try
19251 to find a name in the referred to DIE. */
19252
19253 if (name == NULL && has_specification)
19254 {
19255 struct partial_die_info *spec_die;
19256
19257 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
19258
19259 spec_die->fixup (cu);
19260
19261 if (spec_die->name)
19262 {
19263 name = spec_die->name;
19264
19265 /* Copy DW_AT_external attribute if it is set. */
19266 if (spec_die->is_external)
19267 is_external = spec_die->is_external;
19268 }
19269 }
19270
19271 /* Set default names for some unnamed DIEs. */
19272
19273 if (name == NULL && tag == DW_TAG_namespace)
19274 name = CP_ANONYMOUS_NAMESPACE_STR;
19275
19276 /* If there is no parent die to provide a namespace, and there are
19277 children, see if we can determine the namespace from their linkage
19278 name. */
19279 if (cu->language == language_cplus
19280 && !VEC_empty (dwarf2_section_info_def,
19281 cu->per_cu->dwarf2_per_objfile->types)
19282 && die_parent == NULL
19283 && has_children
19284 && (tag == DW_TAG_class_type
19285 || tag == DW_TAG_structure_type
19286 || tag == DW_TAG_union_type))
19287 guess_partial_die_structure_name (this, cu);
19288
19289 /* GCC might emit a nameless struct or union that has a linkage
19290 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19291 if (name == NULL
19292 && (tag == DW_TAG_class_type
19293 || tag == DW_TAG_interface_type
19294 || tag == DW_TAG_structure_type
19295 || tag == DW_TAG_union_type)
19296 && linkage_name != NULL)
19297 {
19298 char *demangled;
19299
19300 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
19301 if (demangled)
19302 {
19303 const char *base;
19304
19305 /* Strip any leading namespaces/classes, keep only the base name.
19306 DW_AT_name for named DIEs does not contain the prefixes. */
19307 base = strrchr (demangled, ':');
19308 if (base && base > demangled && base[-1] == ':')
19309 base++;
19310 else
19311 base = demangled;
19312
19313 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19314 name
19315 = ((const char *)
19316 obstack_copy0 (&objfile->per_bfd->storage_obstack,
19317 base, strlen (base)));
19318 xfree (demangled);
19319 }
19320 }
19321
19322 fixup_called = 1;
19323 }
19324
19325 /* Read an attribute value described by an attribute form. */
19326
19327 static const gdb_byte *
19328 read_attribute_value (const struct die_reader_specs *reader,
19329 struct attribute *attr, unsigned form,
19330 LONGEST implicit_const, const gdb_byte *info_ptr)
19331 {
19332 struct dwarf2_cu *cu = reader->cu;
19333 struct dwarf2_per_objfile *dwarf2_per_objfile
19334 = cu->per_cu->dwarf2_per_objfile;
19335 struct objfile *objfile = dwarf2_per_objfile->objfile;
19336 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19337 bfd *abfd = reader->abfd;
19338 struct comp_unit_head *cu_header = &cu->header;
19339 unsigned int bytes_read;
19340 struct dwarf_block *blk;
19341
19342 attr->form = (enum dwarf_form) form;
19343 switch (form)
19344 {
19345 case DW_FORM_ref_addr:
19346 if (cu->header.version == 2)
19347 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19348 else
19349 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19350 &cu->header, &bytes_read);
19351 info_ptr += bytes_read;
19352 break;
19353 case DW_FORM_GNU_ref_alt:
19354 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19355 info_ptr += bytes_read;
19356 break;
19357 case DW_FORM_addr:
19358 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19359 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19360 info_ptr += bytes_read;
19361 break;
19362 case DW_FORM_block2:
19363 blk = dwarf_alloc_block (cu);
19364 blk->size = read_2_bytes (abfd, info_ptr);
19365 info_ptr += 2;
19366 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19367 info_ptr += blk->size;
19368 DW_BLOCK (attr) = blk;
19369 break;
19370 case DW_FORM_block4:
19371 blk = dwarf_alloc_block (cu);
19372 blk->size = read_4_bytes (abfd, info_ptr);
19373 info_ptr += 4;
19374 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19375 info_ptr += blk->size;
19376 DW_BLOCK (attr) = blk;
19377 break;
19378 case DW_FORM_data2:
19379 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19380 info_ptr += 2;
19381 break;
19382 case DW_FORM_data4:
19383 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19384 info_ptr += 4;
19385 break;
19386 case DW_FORM_data8:
19387 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19388 info_ptr += 8;
19389 break;
19390 case DW_FORM_data16:
19391 blk = dwarf_alloc_block (cu);
19392 blk->size = 16;
19393 blk->data = read_n_bytes (abfd, info_ptr, 16);
19394 info_ptr += 16;
19395 DW_BLOCK (attr) = blk;
19396 break;
19397 case DW_FORM_sec_offset:
19398 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19399 info_ptr += bytes_read;
19400 break;
19401 case DW_FORM_string:
19402 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19403 DW_STRING_IS_CANONICAL (attr) = 0;
19404 info_ptr += bytes_read;
19405 break;
19406 case DW_FORM_strp:
19407 if (!cu->per_cu->is_dwz)
19408 {
19409 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19410 abfd, info_ptr, cu_header,
19411 &bytes_read);
19412 DW_STRING_IS_CANONICAL (attr) = 0;
19413 info_ptr += bytes_read;
19414 break;
19415 }
19416 /* FALLTHROUGH */
19417 case DW_FORM_line_strp:
19418 if (!cu->per_cu->is_dwz)
19419 {
19420 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19421 abfd, info_ptr,
19422 cu_header, &bytes_read);
19423 DW_STRING_IS_CANONICAL (attr) = 0;
19424 info_ptr += bytes_read;
19425 break;
19426 }
19427 /* FALLTHROUGH */
19428 case DW_FORM_GNU_strp_alt:
19429 {
19430 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19431 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19432 &bytes_read);
19433
19434 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19435 dwz, str_offset);
19436 DW_STRING_IS_CANONICAL (attr) = 0;
19437 info_ptr += bytes_read;
19438 }
19439 break;
19440 case DW_FORM_exprloc:
19441 case DW_FORM_block:
19442 blk = dwarf_alloc_block (cu);
19443 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19444 info_ptr += bytes_read;
19445 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19446 info_ptr += blk->size;
19447 DW_BLOCK (attr) = blk;
19448 break;
19449 case DW_FORM_block1:
19450 blk = dwarf_alloc_block (cu);
19451 blk->size = read_1_byte (abfd, info_ptr);
19452 info_ptr += 1;
19453 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19454 info_ptr += blk->size;
19455 DW_BLOCK (attr) = blk;
19456 break;
19457 case DW_FORM_data1:
19458 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19459 info_ptr += 1;
19460 break;
19461 case DW_FORM_flag:
19462 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19463 info_ptr += 1;
19464 break;
19465 case DW_FORM_flag_present:
19466 DW_UNSND (attr) = 1;
19467 break;
19468 case DW_FORM_sdata:
19469 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19470 info_ptr += bytes_read;
19471 break;
19472 case DW_FORM_udata:
19473 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19474 info_ptr += bytes_read;
19475 break;
19476 case DW_FORM_ref1:
19477 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19478 + read_1_byte (abfd, info_ptr));
19479 info_ptr += 1;
19480 break;
19481 case DW_FORM_ref2:
19482 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19483 + read_2_bytes (abfd, info_ptr));
19484 info_ptr += 2;
19485 break;
19486 case DW_FORM_ref4:
19487 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19488 + read_4_bytes (abfd, info_ptr));
19489 info_ptr += 4;
19490 break;
19491 case DW_FORM_ref8:
19492 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19493 + read_8_bytes (abfd, info_ptr));
19494 info_ptr += 8;
19495 break;
19496 case DW_FORM_ref_sig8:
19497 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19498 info_ptr += 8;
19499 break;
19500 case DW_FORM_ref_udata:
19501 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19502 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19503 info_ptr += bytes_read;
19504 break;
19505 case DW_FORM_indirect:
19506 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19507 info_ptr += bytes_read;
19508 if (form == DW_FORM_implicit_const)
19509 {
19510 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19511 info_ptr += bytes_read;
19512 }
19513 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19514 info_ptr);
19515 break;
19516 case DW_FORM_implicit_const:
19517 DW_SND (attr) = implicit_const;
19518 break;
19519 case DW_FORM_GNU_addr_index:
19520 if (reader->dwo_file == NULL)
19521 {
19522 /* For now flag a hard error.
19523 Later we can turn this into a complaint. */
19524 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19525 dwarf_form_name (form),
19526 bfd_get_filename (abfd));
19527 }
19528 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19529 info_ptr += bytes_read;
19530 break;
19531 case DW_FORM_GNU_str_index:
19532 if (reader->dwo_file == NULL)
19533 {
19534 /* For now flag a hard error.
19535 Later we can turn this into a complaint if warranted. */
19536 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19537 dwarf_form_name (form),
19538 bfd_get_filename (abfd));
19539 }
19540 {
19541 ULONGEST str_index =
19542 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19543
19544 DW_STRING (attr) = read_str_index (reader, str_index);
19545 DW_STRING_IS_CANONICAL (attr) = 0;
19546 info_ptr += bytes_read;
19547 }
19548 break;
19549 default:
19550 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19551 dwarf_form_name (form),
19552 bfd_get_filename (abfd));
19553 }
19554
19555 /* Super hack. */
19556 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19557 attr->form = DW_FORM_GNU_ref_alt;
19558
19559 /* We have seen instances where the compiler tried to emit a byte
19560 size attribute of -1 which ended up being encoded as an unsigned
19561 0xffffffff. Although 0xffffffff is technically a valid size value,
19562 an object of this size seems pretty unlikely so we can relatively
19563 safely treat these cases as if the size attribute was invalid and
19564 treat them as zero by default. */
19565 if (attr->name == DW_AT_byte_size
19566 && form == DW_FORM_data4
19567 && DW_UNSND (attr) >= 0xffffffff)
19568 {
19569 complaint
19570 (&symfile_complaints,
19571 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19572 hex_string (DW_UNSND (attr)));
19573 DW_UNSND (attr) = 0;
19574 }
19575
19576 return info_ptr;
19577 }
19578
19579 /* Read an attribute described by an abbreviated attribute. */
19580
19581 static const gdb_byte *
19582 read_attribute (const struct die_reader_specs *reader,
19583 struct attribute *attr, struct attr_abbrev *abbrev,
19584 const gdb_byte *info_ptr)
19585 {
19586 attr->name = abbrev->name;
19587 return read_attribute_value (reader, attr, abbrev->form,
19588 abbrev->implicit_const, info_ptr);
19589 }
19590
19591 /* Read dwarf information from a buffer. */
19592
19593 static unsigned int
19594 read_1_byte (bfd *abfd, const gdb_byte *buf)
19595 {
19596 return bfd_get_8 (abfd, buf);
19597 }
19598
19599 static int
19600 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19601 {
19602 return bfd_get_signed_8 (abfd, buf);
19603 }
19604
19605 static unsigned int
19606 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19607 {
19608 return bfd_get_16 (abfd, buf);
19609 }
19610
19611 static int
19612 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19613 {
19614 return bfd_get_signed_16 (abfd, buf);
19615 }
19616
19617 static unsigned int
19618 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19619 {
19620 return bfd_get_32 (abfd, buf);
19621 }
19622
19623 static int
19624 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19625 {
19626 return bfd_get_signed_32 (abfd, buf);
19627 }
19628
19629 static ULONGEST
19630 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19631 {
19632 return bfd_get_64 (abfd, buf);
19633 }
19634
19635 static CORE_ADDR
19636 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19637 unsigned int *bytes_read)
19638 {
19639 struct comp_unit_head *cu_header = &cu->header;
19640 CORE_ADDR retval = 0;
19641
19642 if (cu_header->signed_addr_p)
19643 {
19644 switch (cu_header->addr_size)
19645 {
19646 case 2:
19647 retval = bfd_get_signed_16 (abfd, buf);
19648 break;
19649 case 4:
19650 retval = bfd_get_signed_32 (abfd, buf);
19651 break;
19652 case 8:
19653 retval = bfd_get_signed_64 (abfd, buf);
19654 break;
19655 default:
19656 internal_error (__FILE__, __LINE__,
19657 _("read_address: bad switch, signed [in module %s]"),
19658 bfd_get_filename (abfd));
19659 }
19660 }
19661 else
19662 {
19663 switch (cu_header->addr_size)
19664 {
19665 case 2:
19666 retval = bfd_get_16 (abfd, buf);
19667 break;
19668 case 4:
19669 retval = bfd_get_32 (abfd, buf);
19670 break;
19671 case 8:
19672 retval = bfd_get_64 (abfd, buf);
19673 break;
19674 default:
19675 internal_error (__FILE__, __LINE__,
19676 _("read_address: bad switch, "
19677 "unsigned [in module %s]"),
19678 bfd_get_filename (abfd));
19679 }
19680 }
19681
19682 *bytes_read = cu_header->addr_size;
19683 return retval;
19684 }
19685
19686 /* Read the initial length from a section. The (draft) DWARF 3
19687 specification allows the initial length to take up either 4 bytes
19688 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19689 bytes describe the length and all offsets will be 8 bytes in length
19690 instead of 4.
19691
19692 An older, non-standard 64-bit format is also handled by this
19693 function. The older format in question stores the initial length
19694 as an 8-byte quantity without an escape value. Lengths greater
19695 than 2^32 aren't very common which means that the initial 4 bytes
19696 is almost always zero. Since a length value of zero doesn't make
19697 sense for the 32-bit format, this initial zero can be considered to
19698 be an escape value which indicates the presence of the older 64-bit
19699 format. As written, the code can't detect (old format) lengths
19700 greater than 4GB. If it becomes necessary to handle lengths
19701 somewhat larger than 4GB, we could allow other small values (such
19702 as the non-sensical values of 1, 2, and 3) to also be used as
19703 escape values indicating the presence of the old format.
19704
19705 The value returned via bytes_read should be used to increment the
19706 relevant pointer after calling read_initial_length().
19707
19708 [ Note: read_initial_length() and read_offset() are based on the
19709 document entitled "DWARF Debugging Information Format", revision
19710 3, draft 8, dated November 19, 2001. This document was obtained
19711 from:
19712
19713 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19714
19715 This document is only a draft and is subject to change. (So beware.)
19716
19717 Details regarding the older, non-standard 64-bit format were
19718 determined empirically by examining 64-bit ELF files produced by
19719 the SGI toolchain on an IRIX 6.5 machine.
19720
19721 - Kevin, July 16, 2002
19722 ] */
19723
19724 static LONGEST
19725 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19726 {
19727 LONGEST length = bfd_get_32 (abfd, buf);
19728
19729 if (length == 0xffffffff)
19730 {
19731 length = bfd_get_64 (abfd, buf + 4);
19732 *bytes_read = 12;
19733 }
19734 else if (length == 0)
19735 {
19736 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19737 length = bfd_get_64 (abfd, buf);
19738 *bytes_read = 8;
19739 }
19740 else
19741 {
19742 *bytes_read = 4;
19743 }
19744
19745 return length;
19746 }
19747
19748 /* Cover function for read_initial_length.
19749 Returns the length of the object at BUF, and stores the size of the
19750 initial length in *BYTES_READ and stores the size that offsets will be in
19751 *OFFSET_SIZE.
19752 If the initial length size is not equivalent to that specified in
19753 CU_HEADER then issue a complaint.
19754 This is useful when reading non-comp-unit headers. */
19755
19756 static LONGEST
19757 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19758 const struct comp_unit_head *cu_header,
19759 unsigned int *bytes_read,
19760 unsigned int *offset_size)
19761 {
19762 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19763
19764 gdb_assert (cu_header->initial_length_size == 4
19765 || cu_header->initial_length_size == 8
19766 || cu_header->initial_length_size == 12);
19767
19768 if (cu_header->initial_length_size != *bytes_read)
19769 complaint (&symfile_complaints,
19770 _("intermixed 32-bit and 64-bit DWARF sections"));
19771
19772 *offset_size = (*bytes_read == 4) ? 4 : 8;
19773 return length;
19774 }
19775
19776 /* Read an offset from the data stream. The size of the offset is
19777 given by cu_header->offset_size. */
19778
19779 static LONGEST
19780 read_offset (bfd *abfd, const gdb_byte *buf,
19781 const struct comp_unit_head *cu_header,
19782 unsigned int *bytes_read)
19783 {
19784 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19785
19786 *bytes_read = cu_header->offset_size;
19787 return offset;
19788 }
19789
19790 /* Read an offset from the data stream. */
19791
19792 static LONGEST
19793 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19794 {
19795 LONGEST retval = 0;
19796
19797 switch (offset_size)
19798 {
19799 case 4:
19800 retval = bfd_get_32 (abfd, buf);
19801 break;
19802 case 8:
19803 retval = bfd_get_64 (abfd, buf);
19804 break;
19805 default:
19806 internal_error (__FILE__, __LINE__,
19807 _("read_offset_1: bad switch [in module %s]"),
19808 bfd_get_filename (abfd));
19809 }
19810
19811 return retval;
19812 }
19813
19814 static const gdb_byte *
19815 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19816 {
19817 /* If the size of a host char is 8 bits, we can return a pointer
19818 to the buffer, otherwise we have to copy the data to a buffer
19819 allocated on the temporary obstack. */
19820 gdb_assert (HOST_CHAR_BIT == 8);
19821 return buf;
19822 }
19823
19824 static const char *
19825 read_direct_string (bfd *abfd, const gdb_byte *buf,
19826 unsigned int *bytes_read_ptr)
19827 {
19828 /* If the size of a host char is 8 bits, we can return a pointer
19829 to the string, otherwise we have to copy the string to a buffer
19830 allocated on the temporary obstack. */
19831 gdb_assert (HOST_CHAR_BIT == 8);
19832 if (*buf == '\0')
19833 {
19834 *bytes_read_ptr = 1;
19835 return NULL;
19836 }
19837 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19838 return (const char *) buf;
19839 }
19840
19841 /* Return pointer to string at section SECT offset STR_OFFSET with error
19842 reporting strings FORM_NAME and SECT_NAME. */
19843
19844 static const char *
19845 read_indirect_string_at_offset_from (struct objfile *objfile,
19846 bfd *abfd, LONGEST str_offset,
19847 struct dwarf2_section_info *sect,
19848 const char *form_name,
19849 const char *sect_name)
19850 {
19851 dwarf2_read_section (objfile, sect);
19852 if (sect->buffer == NULL)
19853 error (_("%s used without %s section [in module %s]"),
19854 form_name, sect_name, bfd_get_filename (abfd));
19855 if (str_offset >= sect->size)
19856 error (_("%s pointing outside of %s section [in module %s]"),
19857 form_name, sect_name, bfd_get_filename (abfd));
19858 gdb_assert (HOST_CHAR_BIT == 8);
19859 if (sect->buffer[str_offset] == '\0')
19860 return NULL;
19861 return (const char *) (sect->buffer + str_offset);
19862 }
19863
19864 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19865
19866 static const char *
19867 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19868 bfd *abfd, LONGEST str_offset)
19869 {
19870 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19871 abfd, str_offset,
19872 &dwarf2_per_objfile->str,
19873 "DW_FORM_strp", ".debug_str");
19874 }
19875
19876 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19877
19878 static const char *
19879 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19880 bfd *abfd, LONGEST str_offset)
19881 {
19882 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19883 abfd, str_offset,
19884 &dwarf2_per_objfile->line_str,
19885 "DW_FORM_line_strp",
19886 ".debug_line_str");
19887 }
19888
19889 /* Read a string at offset STR_OFFSET in the .debug_str section from
19890 the .dwz file DWZ. Throw an error if the offset is too large. If
19891 the string consists of a single NUL byte, return NULL; otherwise
19892 return a pointer to the string. */
19893
19894 static const char *
19895 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19896 LONGEST str_offset)
19897 {
19898 dwarf2_read_section (objfile, &dwz->str);
19899
19900 if (dwz->str.buffer == NULL)
19901 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19902 "section [in module %s]"),
19903 bfd_get_filename (dwz->dwz_bfd));
19904 if (str_offset >= dwz->str.size)
19905 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19906 ".debug_str section [in module %s]"),
19907 bfd_get_filename (dwz->dwz_bfd));
19908 gdb_assert (HOST_CHAR_BIT == 8);
19909 if (dwz->str.buffer[str_offset] == '\0')
19910 return NULL;
19911 return (const char *) (dwz->str.buffer + str_offset);
19912 }
19913
19914 /* Return pointer to string at .debug_str offset as read from BUF.
19915 BUF is assumed to be in a compilation unit described by CU_HEADER.
19916 Return *BYTES_READ_PTR count of bytes read from BUF. */
19917
19918 static const char *
19919 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19920 const gdb_byte *buf,
19921 const struct comp_unit_head *cu_header,
19922 unsigned int *bytes_read_ptr)
19923 {
19924 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19925
19926 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19927 }
19928
19929 /* Return pointer to string at .debug_line_str offset as read from BUF.
19930 BUF is assumed to be in a compilation unit described by CU_HEADER.
19931 Return *BYTES_READ_PTR count of bytes read from BUF. */
19932
19933 static const char *
19934 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19935 bfd *abfd, const gdb_byte *buf,
19936 const struct comp_unit_head *cu_header,
19937 unsigned int *bytes_read_ptr)
19938 {
19939 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19940
19941 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19942 str_offset);
19943 }
19944
19945 ULONGEST
19946 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19947 unsigned int *bytes_read_ptr)
19948 {
19949 ULONGEST result;
19950 unsigned int num_read;
19951 int shift;
19952 unsigned char byte;
19953
19954 result = 0;
19955 shift = 0;
19956 num_read = 0;
19957 while (1)
19958 {
19959 byte = bfd_get_8 (abfd, buf);
19960 buf++;
19961 num_read++;
19962 result |= ((ULONGEST) (byte & 127) << shift);
19963 if ((byte & 128) == 0)
19964 {
19965 break;
19966 }
19967 shift += 7;
19968 }
19969 *bytes_read_ptr = num_read;
19970 return result;
19971 }
19972
19973 static LONGEST
19974 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19975 unsigned int *bytes_read_ptr)
19976 {
19977 LONGEST result;
19978 int shift, num_read;
19979 unsigned char byte;
19980
19981 result = 0;
19982 shift = 0;
19983 num_read = 0;
19984 while (1)
19985 {
19986 byte = bfd_get_8 (abfd, buf);
19987 buf++;
19988 num_read++;
19989 result |= ((LONGEST) (byte & 127) << shift);
19990 shift += 7;
19991 if ((byte & 128) == 0)
19992 {
19993 break;
19994 }
19995 }
19996 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19997 result |= -(((LONGEST) 1) << shift);
19998 *bytes_read_ptr = num_read;
19999 return result;
20000 }
20001
20002 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20003 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
20004 ADDR_SIZE is the size of addresses from the CU header. */
20005
20006 static CORE_ADDR
20007 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20008 unsigned int addr_index, ULONGEST addr_base, int addr_size)
20009 {
20010 struct objfile *objfile = dwarf2_per_objfile->objfile;
20011 bfd *abfd = objfile->obfd;
20012 const gdb_byte *info_ptr;
20013
20014 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20015 if (dwarf2_per_objfile->addr.buffer == NULL)
20016 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20017 objfile_name (objfile));
20018 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
20019 error (_("DW_FORM_addr_index pointing outside of "
20020 ".debug_addr section [in module %s]"),
20021 objfile_name (objfile));
20022 info_ptr = (dwarf2_per_objfile->addr.buffer
20023 + addr_base + addr_index * addr_size);
20024 if (addr_size == 4)
20025 return bfd_get_32 (abfd, info_ptr);
20026 else
20027 return bfd_get_64 (abfd, info_ptr);
20028 }
20029
20030 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20031
20032 static CORE_ADDR
20033 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20034 {
20035 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20036 cu->addr_base, cu->header.addr_size);
20037 }
20038
20039 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20040
20041 static CORE_ADDR
20042 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20043 unsigned int *bytes_read)
20044 {
20045 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
20046 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20047
20048 return read_addr_index (cu, addr_index);
20049 }
20050
20051 /* Data structure to pass results from dwarf2_read_addr_index_reader
20052 back to dwarf2_read_addr_index. */
20053
20054 struct dwarf2_read_addr_index_data
20055 {
20056 ULONGEST addr_base;
20057 int addr_size;
20058 };
20059
20060 /* die_reader_func for dwarf2_read_addr_index. */
20061
20062 static void
20063 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
20064 const gdb_byte *info_ptr,
20065 struct die_info *comp_unit_die,
20066 int has_children,
20067 void *data)
20068 {
20069 struct dwarf2_cu *cu = reader->cu;
20070 struct dwarf2_read_addr_index_data *aidata =
20071 (struct dwarf2_read_addr_index_data *) data;
20072
20073 aidata->addr_base = cu->addr_base;
20074 aidata->addr_size = cu->header.addr_size;
20075 }
20076
20077 /* Given an index in .debug_addr, fetch the value.
20078 NOTE: This can be called during dwarf expression evaluation,
20079 long after the debug information has been read, and thus per_cu->cu
20080 may no longer exist. */
20081
20082 CORE_ADDR
20083 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20084 unsigned int addr_index)
20085 {
20086 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
20087 struct objfile *objfile = dwarf2_per_objfile->objfile;
20088 struct dwarf2_cu *cu = per_cu->cu;
20089 ULONGEST addr_base;
20090 int addr_size;
20091
20092 /* We need addr_base and addr_size.
20093 If we don't have PER_CU->cu, we have to get it.
20094 Nasty, but the alternative is storing the needed info in PER_CU,
20095 which at this point doesn't seem justified: it's not clear how frequently
20096 it would get used and it would increase the size of every PER_CU.
20097 Entry points like dwarf2_per_cu_addr_size do a similar thing
20098 so we're not in uncharted territory here.
20099 Alas we need to be a bit more complicated as addr_base is contained
20100 in the DIE.
20101
20102 We don't need to read the entire CU(/TU).
20103 We just need the header and top level die.
20104
20105 IWBN to use the aging mechanism to let us lazily later discard the CU.
20106 For now we skip this optimization. */
20107
20108 if (cu != NULL)
20109 {
20110 addr_base = cu->addr_base;
20111 addr_size = cu->header.addr_size;
20112 }
20113 else
20114 {
20115 struct dwarf2_read_addr_index_data aidata;
20116
20117 /* Note: We can't use init_cutu_and_read_dies_simple here,
20118 we need addr_base. */
20119 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
20120 dwarf2_read_addr_index_reader, &aidata);
20121 addr_base = aidata.addr_base;
20122 addr_size = aidata.addr_size;
20123 }
20124
20125 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20126 addr_size);
20127 }
20128
20129 /* Given a DW_FORM_GNU_str_index, fetch the string.
20130 This is only used by the Fission support. */
20131
20132 static const char *
20133 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20134 {
20135 struct dwarf2_cu *cu = reader->cu;
20136 struct dwarf2_per_objfile *dwarf2_per_objfile
20137 = cu->per_cu->dwarf2_per_objfile;
20138 struct objfile *objfile = dwarf2_per_objfile->objfile;
20139 const char *objf_name = objfile_name (objfile);
20140 bfd *abfd = objfile->obfd;
20141 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20142 struct dwarf2_section_info *str_offsets_section =
20143 &reader->dwo_file->sections.str_offsets;
20144 const gdb_byte *info_ptr;
20145 ULONGEST str_offset;
20146 static const char form_name[] = "DW_FORM_GNU_str_index";
20147
20148 dwarf2_read_section (objfile, str_section);
20149 dwarf2_read_section (objfile, str_offsets_section);
20150 if (str_section->buffer == NULL)
20151 error (_("%s used without .debug_str.dwo section"
20152 " in CU at offset %s [in module %s]"),
20153 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20154 if (str_offsets_section->buffer == NULL)
20155 error (_("%s used without .debug_str_offsets.dwo section"
20156 " in CU at offset %s [in module %s]"),
20157 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20158 if (str_index * cu->header.offset_size >= str_offsets_section->size)
20159 error (_("%s pointing outside of .debug_str_offsets.dwo"
20160 " section in CU at offset %s [in module %s]"),
20161 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20162 info_ptr = (str_offsets_section->buffer
20163 + str_index * cu->header.offset_size);
20164 if (cu->header.offset_size == 4)
20165 str_offset = bfd_get_32 (abfd, info_ptr);
20166 else
20167 str_offset = bfd_get_64 (abfd, info_ptr);
20168 if (str_offset >= str_section->size)
20169 error (_("Offset from %s pointing outside of"
20170 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20171 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20172 return (const char *) (str_section->buffer + str_offset);
20173 }
20174
20175 /* Return the length of an LEB128 number in BUF. */
20176
20177 static int
20178 leb128_size (const gdb_byte *buf)
20179 {
20180 const gdb_byte *begin = buf;
20181 gdb_byte byte;
20182
20183 while (1)
20184 {
20185 byte = *buf++;
20186 if ((byte & 128) == 0)
20187 return buf - begin;
20188 }
20189 }
20190
20191 static void
20192 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20193 {
20194 switch (lang)
20195 {
20196 case DW_LANG_C89:
20197 case DW_LANG_C99:
20198 case DW_LANG_C11:
20199 case DW_LANG_C:
20200 case DW_LANG_UPC:
20201 cu->language = language_c;
20202 break;
20203 case DW_LANG_Java:
20204 case DW_LANG_C_plus_plus:
20205 case DW_LANG_C_plus_plus_11:
20206 case DW_LANG_C_plus_plus_14:
20207 cu->language = language_cplus;
20208 break;
20209 case DW_LANG_D:
20210 cu->language = language_d;
20211 break;
20212 case DW_LANG_Fortran77:
20213 case DW_LANG_Fortran90:
20214 case DW_LANG_Fortran95:
20215 case DW_LANG_Fortran03:
20216 case DW_LANG_Fortran08:
20217 cu->language = language_fortran;
20218 break;
20219 case DW_LANG_Go:
20220 cu->language = language_go;
20221 break;
20222 case DW_LANG_Mips_Assembler:
20223 cu->language = language_asm;
20224 break;
20225 case DW_LANG_Ada83:
20226 case DW_LANG_Ada95:
20227 cu->language = language_ada;
20228 break;
20229 case DW_LANG_Modula2:
20230 cu->language = language_m2;
20231 break;
20232 case DW_LANG_Pascal83:
20233 cu->language = language_pascal;
20234 break;
20235 case DW_LANG_ObjC:
20236 cu->language = language_objc;
20237 break;
20238 case DW_LANG_Rust:
20239 case DW_LANG_Rust_old:
20240 cu->language = language_rust;
20241 break;
20242 case DW_LANG_Cobol74:
20243 case DW_LANG_Cobol85:
20244 default:
20245 cu->language = language_minimal;
20246 break;
20247 }
20248 cu->language_defn = language_def (cu->language);
20249 }
20250
20251 /* Return the named attribute or NULL if not there. */
20252
20253 static struct attribute *
20254 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20255 {
20256 for (;;)
20257 {
20258 unsigned int i;
20259 struct attribute *spec = NULL;
20260
20261 for (i = 0; i < die->num_attrs; ++i)
20262 {
20263 if (die->attrs[i].name == name)
20264 return &die->attrs[i];
20265 if (die->attrs[i].name == DW_AT_specification
20266 || die->attrs[i].name == DW_AT_abstract_origin)
20267 spec = &die->attrs[i];
20268 }
20269
20270 if (!spec)
20271 break;
20272
20273 die = follow_die_ref (die, spec, &cu);
20274 }
20275
20276 return NULL;
20277 }
20278
20279 /* Return the named attribute or NULL if not there,
20280 but do not follow DW_AT_specification, etc.
20281 This is for use in contexts where we're reading .debug_types dies.
20282 Following DW_AT_specification, DW_AT_abstract_origin will take us
20283 back up the chain, and we want to go down. */
20284
20285 static struct attribute *
20286 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20287 {
20288 unsigned int i;
20289
20290 for (i = 0; i < die->num_attrs; ++i)
20291 if (die->attrs[i].name == name)
20292 return &die->attrs[i];
20293
20294 return NULL;
20295 }
20296
20297 /* Return the string associated with a string-typed attribute, or NULL if it
20298 is either not found or is of an incorrect type. */
20299
20300 static const char *
20301 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20302 {
20303 struct attribute *attr;
20304 const char *str = NULL;
20305
20306 attr = dwarf2_attr (die, name, cu);
20307
20308 if (attr != NULL)
20309 {
20310 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20311 || attr->form == DW_FORM_string
20312 || attr->form == DW_FORM_GNU_str_index
20313 || attr->form == DW_FORM_GNU_strp_alt)
20314 str = DW_STRING (attr);
20315 else
20316 complaint (&symfile_complaints,
20317 _("string type expected for attribute %s for "
20318 "DIE at %s in module %s"),
20319 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20320 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20321 }
20322
20323 return str;
20324 }
20325
20326 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20327 and holds a non-zero value. This function should only be used for
20328 DW_FORM_flag or DW_FORM_flag_present attributes. */
20329
20330 static int
20331 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20332 {
20333 struct attribute *attr = dwarf2_attr (die, name, cu);
20334
20335 return (attr && DW_UNSND (attr));
20336 }
20337
20338 static int
20339 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20340 {
20341 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20342 which value is non-zero. However, we have to be careful with
20343 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20344 (via dwarf2_flag_true_p) follows this attribute. So we may
20345 end up accidently finding a declaration attribute that belongs
20346 to a different DIE referenced by the specification attribute,
20347 even though the given DIE does not have a declaration attribute. */
20348 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20349 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20350 }
20351
20352 /* Return the die giving the specification for DIE, if there is
20353 one. *SPEC_CU is the CU containing DIE on input, and the CU
20354 containing the return value on output. If there is no
20355 specification, but there is an abstract origin, that is
20356 returned. */
20357
20358 static struct die_info *
20359 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20360 {
20361 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20362 *spec_cu);
20363
20364 if (spec_attr == NULL)
20365 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20366
20367 if (spec_attr == NULL)
20368 return NULL;
20369 else
20370 return follow_die_ref (die, spec_attr, spec_cu);
20371 }
20372
20373 /* Stub for free_line_header to match void * callback types. */
20374
20375 static void
20376 free_line_header_voidp (void *arg)
20377 {
20378 struct line_header *lh = (struct line_header *) arg;
20379
20380 delete lh;
20381 }
20382
20383 void
20384 line_header::add_include_dir (const char *include_dir)
20385 {
20386 if (dwarf_line_debug >= 2)
20387 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20388 include_dirs.size () + 1, include_dir);
20389
20390 include_dirs.push_back (include_dir);
20391 }
20392
20393 void
20394 line_header::add_file_name (const char *name,
20395 dir_index d_index,
20396 unsigned int mod_time,
20397 unsigned int length)
20398 {
20399 if (dwarf_line_debug >= 2)
20400 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
20401 (unsigned) file_names.size () + 1, name);
20402
20403 file_names.emplace_back (name, d_index, mod_time, length);
20404 }
20405
20406 /* A convenience function to find the proper .debug_line section for a CU. */
20407
20408 static struct dwarf2_section_info *
20409 get_debug_line_section (struct dwarf2_cu *cu)
20410 {
20411 struct dwarf2_section_info *section;
20412 struct dwarf2_per_objfile *dwarf2_per_objfile
20413 = cu->per_cu->dwarf2_per_objfile;
20414
20415 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20416 DWO file. */
20417 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20418 section = &cu->dwo_unit->dwo_file->sections.line;
20419 else if (cu->per_cu->is_dwz)
20420 {
20421 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20422
20423 section = &dwz->line;
20424 }
20425 else
20426 section = &dwarf2_per_objfile->line;
20427
20428 return section;
20429 }
20430
20431 /* Read directory or file name entry format, starting with byte of
20432 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20433 entries count and the entries themselves in the described entry
20434 format. */
20435
20436 static void
20437 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20438 bfd *abfd, const gdb_byte **bufp,
20439 struct line_header *lh,
20440 const struct comp_unit_head *cu_header,
20441 void (*callback) (struct line_header *lh,
20442 const char *name,
20443 dir_index d_index,
20444 unsigned int mod_time,
20445 unsigned int length))
20446 {
20447 gdb_byte format_count, formati;
20448 ULONGEST data_count, datai;
20449 const gdb_byte *buf = *bufp;
20450 const gdb_byte *format_header_data;
20451 unsigned int bytes_read;
20452
20453 format_count = read_1_byte (abfd, buf);
20454 buf += 1;
20455 format_header_data = buf;
20456 for (formati = 0; formati < format_count; formati++)
20457 {
20458 read_unsigned_leb128 (abfd, buf, &bytes_read);
20459 buf += bytes_read;
20460 read_unsigned_leb128 (abfd, buf, &bytes_read);
20461 buf += bytes_read;
20462 }
20463
20464 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20465 buf += bytes_read;
20466 for (datai = 0; datai < data_count; datai++)
20467 {
20468 const gdb_byte *format = format_header_data;
20469 struct file_entry fe;
20470
20471 for (formati = 0; formati < format_count; formati++)
20472 {
20473 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20474 format += bytes_read;
20475
20476 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20477 format += bytes_read;
20478
20479 gdb::optional<const char *> string;
20480 gdb::optional<unsigned int> uint;
20481
20482 switch (form)
20483 {
20484 case DW_FORM_string:
20485 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20486 buf += bytes_read;
20487 break;
20488
20489 case DW_FORM_line_strp:
20490 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20491 abfd, buf,
20492 cu_header,
20493 &bytes_read));
20494 buf += bytes_read;
20495 break;
20496
20497 case DW_FORM_data1:
20498 uint.emplace (read_1_byte (abfd, buf));
20499 buf += 1;
20500 break;
20501
20502 case DW_FORM_data2:
20503 uint.emplace (read_2_bytes (abfd, buf));
20504 buf += 2;
20505 break;
20506
20507 case DW_FORM_data4:
20508 uint.emplace (read_4_bytes (abfd, buf));
20509 buf += 4;
20510 break;
20511
20512 case DW_FORM_data8:
20513 uint.emplace (read_8_bytes (abfd, buf));
20514 buf += 8;
20515 break;
20516
20517 case DW_FORM_udata:
20518 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20519 buf += bytes_read;
20520 break;
20521
20522 case DW_FORM_block:
20523 /* It is valid only for DW_LNCT_timestamp which is ignored by
20524 current GDB. */
20525 break;
20526 }
20527
20528 switch (content_type)
20529 {
20530 case DW_LNCT_path:
20531 if (string.has_value ())
20532 fe.name = *string;
20533 break;
20534 case DW_LNCT_directory_index:
20535 if (uint.has_value ())
20536 fe.d_index = (dir_index) *uint;
20537 break;
20538 case DW_LNCT_timestamp:
20539 if (uint.has_value ())
20540 fe.mod_time = *uint;
20541 break;
20542 case DW_LNCT_size:
20543 if (uint.has_value ())
20544 fe.length = *uint;
20545 break;
20546 case DW_LNCT_MD5:
20547 break;
20548 default:
20549 complaint (&symfile_complaints,
20550 _("Unknown format content type %s"),
20551 pulongest (content_type));
20552 }
20553 }
20554
20555 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20556 }
20557
20558 *bufp = buf;
20559 }
20560
20561 /* Read the statement program header starting at OFFSET in
20562 .debug_line, or .debug_line.dwo. Return a pointer
20563 to a struct line_header, allocated using xmalloc.
20564 Returns NULL if there is a problem reading the header, e.g., if it
20565 has a version we don't understand.
20566
20567 NOTE: the strings in the include directory and file name tables of
20568 the returned object point into the dwarf line section buffer,
20569 and must not be freed. */
20570
20571 static line_header_up
20572 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20573 {
20574 const gdb_byte *line_ptr;
20575 unsigned int bytes_read, offset_size;
20576 int i;
20577 const char *cur_dir, *cur_file;
20578 struct dwarf2_section_info *section;
20579 bfd *abfd;
20580 struct dwarf2_per_objfile *dwarf2_per_objfile
20581 = cu->per_cu->dwarf2_per_objfile;
20582
20583 section = get_debug_line_section (cu);
20584 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20585 if (section->buffer == NULL)
20586 {
20587 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20588 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20589 else
20590 complaint (&symfile_complaints, _("missing .debug_line section"));
20591 return 0;
20592 }
20593
20594 /* We can't do this until we know the section is non-empty.
20595 Only then do we know we have such a section. */
20596 abfd = get_section_bfd_owner (section);
20597
20598 /* Make sure that at least there's room for the total_length field.
20599 That could be 12 bytes long, but we're just going to fudge that. */
20600 if (to_underlying (sect_off) + 4 >= section->size)
20601 {
20602 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20603 return 0;
20604 }
20605
20606 line_header_up lh (new line_header ());
20607
20608 lh->sect_off = sect_off;
20609 lh->offset_in_dwz = cu->per_cu->is_dwz;
20610
20611 line_ptr = section->buffer + to_underlying (sect_off);
20612
20613 /* Read in the header. */
20614 lh->total_length =
20615 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20616 &bytes_read, &offset_size);
20617 line_ptr += bytes_read;
20618 if (line_ptr + lh->total_length > (section->buffer + section->size))
20619 {
20620 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20621 return 0;
20622 }
20623 lh->statement_program_end = line_ptr + lh->total_length;
20624 lh->version = read_2_bytes (abfd, line_ptr);
20625 line_ptr += 2;
20626 if (lh->version > 5)
20627 {
20628 /* This is a version we don't understand. The format could have
20629 changed in ways we don't handle properly so just punt. */
20630 complaint (&symfile_complaints,
20631 _("unsupported version in .debug_line section"));
20632 return NULL;
20633 }
20634 if (lh->version >= 5)
20635 {
20636 gdb_byte segment_selector_size;
20637
20638 /* Skip address size. */
20639 read_1_byte (abfd, line_ptr);
20640 line_ptr += 1;
20641
20642 segment_selector_size = read_1_byte (abfd, line_ptr);
20643 line_ptr += 1;
20644 if (segment_selector_size != 0)
20645 {
20646 complaint (&symfile_complaints,
20647 _("unsupported segment selector size %u "
20648 "in .debug_line section"),
20649 segment_selector_size);
20650 return NULL;
20651 }
20652 }
20653 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20654 line_ptr += offset_size;
20655 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20656 line_ptr += 1;
20657 if (lh->version >= 4)
20658 {
20659 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20660 line_ptr += 1;
20661 }
20662 else
20663 lh->maximum_ops_per_instruction = 1;
20664
20665 if (lh->maximum_ops_per_instruction == 0)
20666 {
20667 lh->maximum_ops_per_instruction = 1;
20668 complaint (&symfile_complaints,
20669 _("invalid maximum_ops_per_instruction "
20670 "in `.debug_line' section"));
20671 }
20672
20673 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20674 line_ptr += 1;
20675 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20676 line_ptr += 1;
20677 lh->line_range = read_1_byte (abfd, line_ptr);
20678 line_ptr += 1;
20679 lh->opcode_base = read_1_byte (abfd, line_ptr);
20680 line_ptr += 1;
20681 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20682
20683 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20684 for (i = 1; i < lh->opcode_base; ++i)
20685 {
20686 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20687 line_ptr += 1;
20688 }
20689
20690 if (lh->version >= 5)
20691 {
20692 /* Read directory table. */
20693 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20694 &cu->header,
20695 [] (struct line_header *lh, const char *name,
20696 dir_index d_index, unsigned int mod_time,
20697 unsigned int length)
20698 {
20699 lh->add_include_dir (name);
20700 });
20701
20702 /* Read file name table. */
20703 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20704 &cu->header,
20705 [] (struct line_header *lh, const char *name,
20706 dir_index d_index, unsigned int mod_time,
20707 unsigned int length)
20708 {
20709 lh->add_file_name (name, d_index, mod_time, length);
20710 });
20711 }
20712 else
20713 {
20714 /* Read directory table. */
20715 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20716 {
20717 line_ptr += bytes_read;
20718 lh->add_include_dir (cur_dir);
20719 }
20720 line_ptr += bytes_read;
20721
20722 /* Read file name table. */
20723 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20724 {
20725 unsigned int mod_time, length;
20726 dir_index d_index;
20727
20728 line_ptr += bytes_read;
20729 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20730 line_ptr += bytes_read;
20731 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20732 line_ptr += bytes_read;
20733 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20734 line_ptr += bytes_read;
20735
20736 lh->add_file_name (cur_file, d_index, mod_time, length);
20737 }
20738 line_ptr += bytes_read;
20739 }
20740 lh->statement_program_start = line_ptr;
20741
20742 if (line_ptr > (section->buffer + section->size))
20743 complaint (&symfile_complaints,
20744 _("line number info header doesn't "
20745 "fit in `.debug_line' section"));
20746
20747 return lh;
20748 }
20749
20750 /* Subroutine of dwarf_decode_lines to simplify it.
20751 Return the file name of the psymtab for included file FILE_INDEX
20752 in line header LH of PST.
20753 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20754 If space for the result is malloc'd, *NAME_HOLDER will be set.
20755 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20756
20757 static const char *
20758 psymtab_include_file_name (const struct line_header *lh, int file_index,
20759 const struct partial_symtab *pst,
20760 const char *comp_dir,
20761 gdb::unique_xmalloc_ptr<char> *name_holder)
20762 {
20763 const file_entry &fe = lh->file_names[file_index];
20764 const char *include_name = fe.name;
20765 const char *include_name_to_compare = include_name;
20766 const char *pst_filename;
20767 int file_is_pst;
20768
20769 const char *dir_name = fe.include_dir (lh);
20770
20771 gdb::unique_xmalloc_ptr<char> hold_compare;
20772 if (!IS_ABSOLUTE_PATH (include_name)
20773 && (dir_name != NULL || comp_dir != NULL))
20774 {
20775 /* Avoid creating a duplicate psymtab for PST.
20776 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20777 Before we do the comparison, however, we need to account
20778 for DIR_NAME and COMP_DIR.
20779 First prepend dir_name (if non-NULL). If we still don't
20780 have an absolute path prepend comp_dir (if non-NULL).
20781 However, the directory we record in the include-file's
20782 psymtab does not contain COMP_DIR (to match the
20783 corresponding symtab(s)).
20784
20785 Example:
20786
20787 bash$ cd /tmp
20788 bash$ gcc -g ./hello.c
20789 include_name = "hello.c"
20790 dir_name = "."
20791 DW_AT_comp_dir = comp_dir = "/tmp"
20792 DW_AT_name = "./hello.c"
20793
20794 */
20795
20796 if (dir_name != NULL)
20797 {
20798 name_holder->reset (concat (dir_name, SLASH_STRING,
20799 include_name, (char *) NULL));
20800 include_name = name_holder->get ();
20801 include_name_to_compare = include_name;
20802 }
20803 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20804 {
20805 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20806 include_name, (char *) NULL));
20807 include_name_to_compare = hold_compare.get ();
20808 }
20809 }
20810
20811 pst_filename = pst->filename;
20812 gdb::unique_xmalloc_ptr<char> copied_name;
20813 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20814 {
20815 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20816 pst_filename, (char *) NULL));
20817 pst_filename = copied_name.get ();
20818 }
20819
20820 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20821
20822 if (file_is_pst)
20823 return NULL;
20824 return include_name;
20825 }
20826
20827 /* State machine to track the state of the line number program. */
20828
20829 class lnp_state_machine
20830 {
20831 public:
20832 /* Initialize a machine state for the start of a line number
20833 program. */
20834 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20835
20836 file_entry *current_file ()
20837 {
20838 /* lh->file_names is 0-based, but the file name numbers in the
20839 statement program are 1-based. */
20840 return m_line_header->file_name_at (m_file);
20841 }
20842
20843 /* Record the line in the state machine. END_SEQUENCE is true if
20844 we're processing the end of a sequence. */
20845 void record_line (bool end_sequence);
20846
20847 /* Check address and if invalid nop-out the rest of the lines in this
20848 sequence. */
20849 void check_line_address (struct dwarf2_cu *cu,
20850 const gdb_byte *line_ptr,
20851 CORE_ADDR lowpc, CORE_ADDR address);
20852
20853 void handle_set_discriminator (unsigned int discriminator)
20854 {
20855 m_discriminator = discriminator;
20856 m_line_has_non_zero_discriminator |= discriminator != 0;
20857 }
20858
20859 /* Handle DW_LNE_set_address. */
20860 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20861 {
20862 m_op_index = 0;
20863 address += baseaddr;
20864 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20865 }
20866
20867 /* Handle DW_LNS_advance_pc. */
20868 void handle_advance_pc (CORE_ADDR adjust);
20869
20870 /* Handle a special opcode. */
20871 void handle_special_opcode (unsigned char op_code);
20872
20873 /* Handle DW_LNS_advance_line. */
20874 void handle_advance_line (int line_delta)
20875 {
20876 advance_line (line_delta);
20877 }
20878
20879 /* Handle DW_LNS_set_file. */
20880 void handle_set_file (file_name_index file);
20881
20882 /* Handle DW_LNS_negate_stmt. */
20883 void handle_negate_stmt ()
20884 {
20885 m_is_stmt = !m_is_stmt;
20886 }
20887
20888 /* Handle DW_LNS_const_add_pc. */
20889 void handle_const_add_pc ();
20890
20891 /* Handle DW_LNS_fixed_advance_pc. */
20892 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20893 {
20894 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20895 m_op_index = 0;
20896 }
20897
20898 /* Handle DW_LNS_copy. */
20899 void handle_copy ()
20900 {
20901 record_line (false);
20902 m_discriminator = 0;
20903 }
20904
20905 /* Handle DW_LNE_end_sequence. */
20906 void handle_end_sequence ()
20907 {
20908 m_record_line_callback = ::record_line;
20909 }
20910
20911 private:
20912 /* Advance the line by LINE_DELTA. */
20913 void advance_line (int line_delta)
20914 {
20915 m_line += line_delta;
20916
20917 if (line_delta != 0)
20918 m_line_has_non_zero_discriminator = m_discriminator != 0;
20919 }
20920
20921 gdbarch *m_gdbarch;
20922
20923 /* True if we're recording lines.
20924 Otherwise we're building partial symtabs and are just interested in
20925 finding include files mentioned by the line number program. */
20926 bool m_record_lines_p;
20927
20928 /* The line number header. */
20929 line_header *m_line_header;
20930
20931 /* These are part of the standard DWARF line number state machine,
20932 and initialized according to the DWARF spec. */
20933
20934 unsigned char m_op_index = 0;
20935 /* The line table index (1-based) of the current file. */
20936 file_name_index m_file = (file_name_index) 1;
20937 unsigned int m_line = 1;
20938
20939 /* These are initialized in the constructor. */
20940
20941 CORE_ADDR m_address;
20942 bool m_is_stmt;
20943 unsigned int m_discriminator;
20944
20945 /* Additional bits of state we need to track. */
20946
20947 /* The last file that we called dwarf2_start_subfile for.
20948 This is only used for TLLs. */
20949 unsigned int m_last_file = 0;
20950 /* The last file a line number was recorded for. */
20951 struct subfile *m_last_subfile = NULL;
20952
20953 /* The function to call to record a line. */
20954 record_line_ftype *m_record_line_callback = NULL;
20955
20956 /* The last line number that was recorded, used to coalesce
20957 consecutive entries for the same line. This can happen, for
20958 example, when discriminators are present. PR 17276. */
20959 unsigned int m_last_line = 0;
20960 bool m_line_has_non_zero_discriminator = false;
20961 };
20962
20963 void
20964 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20965 {
20966 CORE_ADDR addr_adj = (((m_op_index + adjust)
20967 / m_line_header->maximum_ops_per_instruction)
20968 * m_line_header->minimum_instruction_length);
20969 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20970 m_op_index = ((m_op_index + adjust)
20971 % m_line_header->maximum_ops_per_instruction);
20972 }
20973
20974 void
20975 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20976 {
20977 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20978 CORE_ADDR addr_adj = (((m_op_index
20979 + (adj_opcode / m_line_header->line_range))
20980 / m_line_header->maximum_ops_per_instruction)
20981 * m_line_header->minimum_instruction_length);
20982 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20983 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20984 % m_line_header->maximum_ops_per_instruction);
20985
20986 int line_delta = (m_line_header->line_base
20987 + (adj_opcode % m_line_header->line_range));
20988 advance_line (line_delta);
20989 record_line (false);
20990 m_discriminator = 0;
20991 }
20992
20993 void
20994 lnp_state_machine::handle_set_file (file_name_index file)
20995 {
20996 m_file = file;
20997
20998 const file_entry *fe = current_file ();
20999 if (fe == NULL)
21000 dwarf2_debug_line_missing_file_complaint ();
21001 else if (m_record_lines_p)
21002 {
21003 const char *dir = fe->include_dir (m_line_header);
21004
21005 m_last_subfile = current_subfile;
21006 m_line_has_non_zero_discriminator = m_discriminator != 0;
21007 dwarf2_start_subfile (fe->name, dir);
21008 }
21009 }
21010
21011 void
21012 lnp_state_machine::handle_const_add_pc ()
21013 {
21014 CORE_ADDR adjust
21015 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21016
21017 CORE_ADDR addr_adj
21018 = (((m_op_index + adjust)
21019 / m_line_header->maximum_ops_per_instruction)
21020 * m_line_header->minimum_instruction_length);
21021
21022 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21023 m_op_index = ((m_op_index + adjust)
21024 % m_line_header->maximum_ops_per_instruction);
21025 }
21026
21027 /* Ignore this record_line request. */
21028
21029 static void
21030 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
21031 {
21032 return;
21033 }
21034
21035 /* Return non-zero if we should add LINE to the line number table.
21036 LINE is the line to add, LAST_LINE is the last line that was added,
21037 LAST_SUBFILE is the subfile for LAST_LINE.
21038 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21039 had a non-zero discriminator.
21040
21041 We have to be careful in the presence of discriminators.
21042 E.g., for this line:
21043
21044 for (i = 0; i < 100000; i++);
21045
21046 clang can emit four line number entries for that one line,
21047 each with a different discriminator.
21048 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21049
21050 However, we want gdb to coalesce all four entries into one.
21051 Otherwise the user could stepi into the middle of the line and
21052 gdb would get confused about whether the pc really was in the
21053 middle of the line.
21054
21055 Things are further complicated by the fact that two consecutive
21056 line number entries for the same line is a heuristic used by gcc
21057 to denote the end of the prologue. So we can't just discard duplicate
21058 entries, we have to be selective about it. The heuristic we use is
21059 that we only collapse consecutive entries for the same line if at least
21060 one of those entries has a non-zero discriminator. PR 17276.
21061
21062 Note: Addresses in the line number state machine can never go backwards
21063 within one sequence, thus this coalescing is ok. */
21064
21065 static int
21066 dwarf_record_line_p (unsigned int line, unsigned int last_line,
21067 int line_has_non_zero_discriminator,
21068 struct subfile *last_subfile)
21069 {
21070 if (current_subfile != last_subfile)
21071 return 1;
21072 if (line != last_line)
21073 return 1;
21074 /* Same line for the same file that we've seen already.
21075 As a last check, for pr 17276, only record the line if the line
21076 has never had a non-zero discriminator. */
21077 if (!line_has_non_zero_discriminator)
21078 return 1;
21079 return 0;
21080 }
21081
21082 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
21083 in the line table of subfile SUBFILE. */
21084
21085 static void
21086 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21087 unsigned int line, CORE_ADDR address,
21088 record_line_ftype p_record_line)
21089 {
21090 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21091
21092 if (dwarf_line_debug)
21093 {
21094 fprintf_unfiltered (gdb_stdlog,
21095 "Recording line %u, file %s, address %s\n",
21096 line, lbasename (subfile->name),
21097 paddress (gdbarch, address));
21098 }
21099
21100 (*p_record_line) (subfile, line, addr);
21101 }
21102
21103 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21104 Mark the end of a set of line number records.
21105 The arguments are the same as for dwarf_record_line_1.
21106 If SUBFILE is NULL the request is ignored. */
21107
21108 static void
21109 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21110 CORE_ADDR address, record_line_ftype p_record_line)
21111 {
21112 if (subfile == NULL)
21113 return;
21114
21115 if (dwarf_line_debug)
21116 {
21117 fprintf_unfiltered (gdb_stdlog,
21118 "Finishing current line, file %s, address %s\n",
21119 lbasename (subfile->name),
21120 paddress (gdbarch, address));
21121 }
21122
21123 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
21124 }
21125
21126 void
21127 lnp_state_machine::record_line (bool end_sequence)
21128 {
21129 if (dwarf_line_debug)
21130 {
21131 fprintf_unfiltered (gdb_stdlog,
21132 "Processing actual line %u: file %u,"
21133 " address %s, is_stmt %u, discrim %u\n",
21134 m_line, to_underlying (m_file),
21135 paddress (m_gdbarch, m_address),
21136 m_is_stmt, m_discriminator);
21137 }
21138
21139 file_entry *fe = current_file ();
21140
21141 if (fe == NULL)
21142 dwarf2_debug_line_missing_file_complaint ();
21143 /* For now we ignore lines not starting on an instruction boundary.
21144 But not when processing end_sequence for compatibility with the
21145 previous version of the code. */
21146 else if (m_op_index == 0 || end_sequence)
21147 {
21148 fe->included_p = 1;
21149 if (m_record_lines_p && m_is_stmt)
21150 {
21151 if (m_last_subfile != current_subfile || end_sequence)
21152 {
21153 dwarf_finish_line (m_gdbarch, m_last_subfile,
21154 m_address, m_record_line_callback);
21155 }
21156
21157 if (!end_sequence)
21158 {
21159 if (dwarf_record_line_p (m_line, m_last_line,
21160 m_line_has_non_zero_discriminator,
21161 m_last_subfile))
21162 {
21163 dwarf_record_line_1 (m_gdbarch, current_subfile,
21164 m_line, m_address,
21165 m_record_line_callback);
21166 }
21167 m_last_subfile = current_subfile;
21168 m_last_line = m_line;
21169 }
21170 }
21171 }
21172 }
21173
21174 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
21175 bool record_lines_p)
21176 {
21177 m_gdbarch = arch;
21178 m_record_lines_p = record_lines_p;
21179 m_line_header = lh;
21180
21181 m_record_line_callback = ::record_line;
21182
21183 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21184 was a line entry for it so that the backend has a chance to adjust it
21185 and also record it in case it needs it. This is currently used by MIPS
21186 code, cf. `mips_adjust_dwarf2_line'. */
21187 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21188 m_is_stmt = lh->default_is_stmt;
21189 m_discriminator = 0;
21190 }
21191
21192 void
21193 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21194 const gdb_byte *line_ptr,
21195 CORE_ADDR lowpc, CORE_ADDR address)
21196 {
21197 /* If address < lowpc then it's not a usable value, it's outside the
21198 pc range of the CU. However, we restrict the test to only address
21199 values of zero to preserve GDB's previous behaviour which is to
21200 handle the specific case of a function being GC'd by the linker. */
21201
21202 if (address == 0 && address < lowpc)
21203 {
21204 /* This line table is for a function which has been
21205 GCd by the linker. Ignore it. PR gdb/12528 */
21206
21207 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21208 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21209
21210 complaint (&symfile_complaints,
21211 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21212 line_offset, objfile_name (objfile));
21213 m_record_line_callback = noop_record_line;
21214 /* Note: record_line_callback is left as noop_record_line until
21215 we see DW_LNE_end_sequence. */
21216 }
21217 }
21218
21219 /* Subroutine of dwarf_decode_lines to simplify it.
21220 Process the line number information in LH.
21221 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21222 program in order to set included_p for every referenced header. */
21223
21224 static void
21225 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21226 const int decode_for_pst_p, CORE_ADDR lowpc)
21227 {
21228 const gdb_byte *line_ptr, *extended_end;
21229 const gdb_byte *line_end;
21230 unsigned int bytes_read, extended_len;
21231 unsigned char op_code, extended_op;
21232 CORE_ADDR baseaddr;
21233 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21234 bfd *abfd = objfile->obfd;
21235 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21236 /* True if we're recording line info (as opposed to building partial
21237 symtabs and just interested in finding include files mentioned by
21238 the line number program). */
21239 bool record_lines_p = !decode_for_pst_p;
21240
21241 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21242
21243 line_ptr = lh->statement_program_start;
21244 line_end = lh->statement_program_end;
21245
21246 /* Read the statement sequences until there's nothing left. */
21247 while (line_ptr < line_end)
21248 {
21249 /* The DWARF line number program state machine. Reset the state
21250 machine at the start of each sequence. */
21251 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
21252 bool end_sequence = false;
21253
21254 if (record_lines_p)
21255 {
21256 /* Start a subfile for the current file of the state
21257 machine. */
21258 const file_entry *fe = state_machine.current_file ();
21259
21260 if (fe != NULL)
21261 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
21262 }
21263
21264 /* Decode the table. */
21265 while (line_ptr < line_end && !end_sequence)
21266 {
21267 op_code = read_1_byte (abfd, line_ptr);
21268 line_ptr += 1;
21269
21270 if (op_code >= lh->opcode_base)
21271 {
21272 /* Special opcode. */
21273 state_machine.handle_special_opcode (op_code);
21274 }
21275 else switch (op_code)
21276 {
21277 case DW_LNS_extended_op:
21278 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21279 &bytes_read);
21280 line_ptr += bytes_read;
21281 extended_end = line_ptr + extended_len;
21282 extended_op = read_1_byte (abfd, line_ptr);
21283 line_ptr += 1;
21284 switch (extended_op)
21285 {
21286 case DW_LNE_end_sequence:
21287 state_machine.handle_end_sequence ();
21288 end_sequence = true;
21289 break;
21290 case DW_LNE_set_address:
21291 {
21292 CORE_ADDR address
21293 = read_address (abfd, line_ptr, cu, &bytes_read);
21294 line_ptr += bytes_read;
21295
21296 state_machine.check_line_address (cu, line_ptr,
21297 lowpc, address);
21298 state_machine.handle_set_address (baseaddr, address);
21299 }
21300 break;
21301 case DW_LNE_define_file:
21302 {
21303 const char *cur_file;
21304 unsigned int mod_time, length;
21305 dir_index dindex;
21306
21307 cur_file = read_direct_string (abfd, line_ptr,
21308 &bytes_read);
21309 line_ptr += bytes_read;
21310 dindex = (dir_index)
21311 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21312 line_ptr += bytes_read;
21313 mod_time =
21314 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21315 line_ptr += bytes_read;
21316 length =
21317 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21318 line_ptr += bytes_read;
21319 lh->add_file_name (cur_file, dindex, mod_time, length);
21320 }
21321 break;
21322 case DW_LNE_set_discriminator:
21323 {
21324 /* The discriminator is not interesting to the
21325 debugger; just ignore it. We still need to
21326 check its value though:
21327 if there are consecutive entries for the same
21328 (non-prologue) line we want to coalesce them.
21329 PR 17276. */
21330 unsigned int discr
21331 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21332 line_ptr += bytes_read;
21333
21334 state_machine.handle_set_discriminator (discr);
21335 }
21336 break;
21337 default:
21338 complaint (&symfile_complaints,
21339 _("mangled .debug_line section"));
21340 return;
21341 }
21342 /* Make sure that we parsed the extended op correctly. If e.g.
21343 we expected a different address size than the producer used,
21344 we may have read the wrong number of bytes. */
21345 if (line_ptr != extended_end)
21346 {
21347 complaint (&symfile_complaints,
21348 _("mangled .debug_line section"));
21349 return;
21350 }
21351 break;
21352 case DW_LNS_copy:
21353 state_machine.handle_copy ();
21354 break;
21355 case DW_LNS_advance_pc:
21356 {
21357 CORE_ADDR adjust
21358 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21359 line_ptr += bytes_read;
21360
21361 state_machine.handle_advance_pc (adjust);
21362 }
21363 break;
21364 case DW_LNS_advance_line:
21365 {
21366 int line_delta
21367 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21368 line_ptr += bytes_read;
21369
21370 state_machine.handle_advance_line (line_delta);
21371 }
21372 break;
21373 case DW_LNS_set_file:
21374 {
21375 file_name_index file
21376 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21377 &bytes_read);
21378 line_ptr += bytes_read;
21379
21380 state_machine.handle_set_file (file);
21381 }
21382 break;
21383 case DW_LNS_set_column:
21384 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21385 line_ptr += bytes_read;
21386 break;
21387 case DW_LNS_negate_stmt:
21388 state_machine.handle_negate_stmt ();
21389 break;
21390 case DW_LNS_set_basic_block:
21391 break;
21392 /* Add to the address register of the state machine the
21393 address increment value corresponding to special opcode
21394 255. I.e., this value is scaled by the minimum
21395 instruction length since special opcode 255 would have
21396 scaled the increment. */
21397 case DW_LNS_const_add_pc:
21398 state_machine.handle_const_add_pc ();
21399 break;
21400 case DW_LNS_fixed_advance_pc:
21401 {
21402 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21403 line_ptr += 2;
21404
21405 state_machine.handle_fixed_advance_pc (addr_adj);
21406 }
21407 break;
21408 default:
21409 {
21410 /* Unknown standard opcode, ignore it. */
21411 int i;
21412
21413 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21414 {
21415 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21416 line_ptr += bytes_read;
21417 }
21418 }
21419 }
21420 }
21421
21422 if (!end_sequence)
21423 dwarf2_debug_line_missing_end_sequence_complaint ();
21424
21425 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21426 in which case we still finish recording the last line). */
21427 state_machine.record_line (true);
21428 }
21429 }
21430
21431 /* Decode the Line Number Program (LNP) for the given line_header
21432 structure and CU. The actual information extracted and the type
21433 of structures created from the LNP depends on the value of PST.
21434
21435 1. If PST is NULL, then this procedure uses the data from the program
21436 to create all necessary symbol tables, and their linetables.
21437
21438 2. If PST is not NULL, this procedure reads the program to determine
21439 the list of files included by the unit represented by PST, and
21440 builds all the associated partial symbol tables.
21441
21442 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21443 It is used for relative paths in the line table.
21444 NOTE: When processing partial symtabs (pst != NULL),
21445 comp_dir == pst->dirname.
21446
21447 NOTE: It is important that psymtabs have the same file name (via strcmp)
21448 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21449 symtab we don't use it in the name of the psymtabs we create.
21450 E.g. expand_line_sal requires this when finding psymtabs to expand.
21451 A good testcase for this is mb-inline.exp.
21452
21453 LOWPC is the lowest address in CU (or 0 if not known).
21454
21455 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21456 for its PC<->lines mapping information. Otherwise only the filename
21457 table is read in. */
21458
21459 static void
21460 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21461 struct dwarf2_cu *cu, struct partial_symtab *pst,
21462 CORE_ADDR lowpc, int decode_mapping)
21463 {
21464 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21465 const int decode_for_pst_p = (pst != NULL);
21466
21467 if (decode_mapping)
21468 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21469
21470 if (decode_for_pst_p)
21471 {
21472 int file_index;
21473
21474 /* Now that we're done scanning the Line Header Program, we can
21475 create the psymtab of each included file. */
21476 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21477 if (lh->file_names[file_index].included_p == 1)
21478 {
21479 gdb::unique_xmalloc_ptr<char> name_holder;
21480 const char *include_name =
21481 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21482 &name_holder);
21483 if (include_name != NULL)
21484 dwarf2_create_include_psymtab (include_name, pst, objfile);
21485 }
21486 }
21487 else
21488 {
21489 /* Make sure a symtab is created for every file, even files
21490 which contain only variables (i.e. no code with associated
21491 line numbers). */
21492 struct compunit_symtab *cust = buildsym_compunit_symtab ();
21493 int i;
21494
21495 for (i = 0; i < lh->file_names.size (); i++)
21496 {
21497 file_entry &fe = lh->file_names[i];
21498
21499 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
21500
21501 if (current_subfile->symtab == NULL)
21502 {
21503 current_subfile->symtab
21504 = allocate_symtab (cust, current_subfile->name);
21505 }
21506 fe.symtab = current_subfile->symtab;
21507 }
21508 }
21509 }
21510
21511 /* Start a subfile for DWARF. FILENAME is the name of the file and
21512 DIRNAME the name of the source directory which contains FILENAME
21513 or NULL if not known.
21514 This routine tries to keep line numbers from identical absolute and
21515 relative file names in a common subfile.
21516
21517 Using the `list' example from the GDB testsuite, which resides in
21518 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21519 of /srcdir/list0.c yields the following debugging information for list0.c:
21520
21521 DW_AT_name: /srcdir/list0.c
21522 DW_AT_comp_dir: /compdir
21523 files.files[0].name: list0.h
21524 files.files[0].dir: /srcdir
21525 files.files[1].name: list0.c
21526 files.files[1].dir: /srcdir
21527
21528 The line number information for list0.c has to end up in a single
21529 subfile, so that `break /srcdir/list0.c:1' works as expected.
21530 start_subfile will ensure that this happens provided that we pass the
21531 concatenation of files.files[1].dir and files.files[1].name as the
21532 subfile's name. */
21533
21534 static void
21535 dwarf2_start_subfile (const char *filename, const char *dirname)
21536 {
21537 char *copy = NULL;
21538
21539 /* In order not to lose the line information directory,
21540 we concatenate it to the filename when it makes sense.
21541 Note that the Dwarf3 standard says (speaking of filenames in line
21542 information): ``The directory index is ignored for file names
21543 that represent full path names''. Thus ignoring dirname in the
21544 `else' branch below isn't an issue. */
21545
21546 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21547 {
21548 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21549 filename = copy;
21550 }
21551
21552 start_subfile (filename);
21553
21554 if (copy != NULL)
21555 xfree (copy);
21556 }
21557
21558 /* Start a symtab for DWARF.
21559 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21560
21561 static struct compunit_symtab *
21562 dwarf2_start_symtab (struct dwarf2_cu *cu,
21563 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21564 {
21565 struct compunit_symtab *cust
21566 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21567 low_pc, cu->language);
21568
21569 record_debugformat ("DWARF 2");
21570 record_producer (cu->producer);
21571
21572 /* We assume that we're processing GCC output. */
21573 processing_gcc_compilation = 2;
21574
21575 cu->processing_has_namespace_info = 0;
21576
21577 return cust;
21578 }
21579
21580 static void
21581 var_decode_location (struct attribute *attr, struct symbol *sym,
21582 struct dwarf2_cu *cu)
21583 {
21584 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21585 struct comp_unit_head *cu_header = &cu->header;
21586
21587 /* NOTE drow/2003-01-30: There used to be a comment and some special
21588 code here to turn a symbol with DW_AT_external and a
21589 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21590 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21591 with some versions of binutils) where shared libraries could have
21592 relocations against symbols in their debug information - the
21593 minimal symbol would have the right address, but the debug info
21594 would not. It's no longer necessary, because we will explicitly
21595 apply relocations when we read in the debug information now. */
21596
21597 /* A DW_AT_location attribute with no contents indicates that a
21598 variable has been optimized away. */
21599 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21600 {
21601 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21602 return;
21603 }
21604
21605 /* Handle one degenerate form of location expression specially, to
21606 preserve GDB's previous behavior when section offsets are
21607 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21608 then mark this symbol as LOC_STATIC. */
21609
21610 if (attr_form_is_block (attr)
21611 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21612 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21613 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21614 && (DW_BLOCK (attr)->size
21615 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21616 {
21617 unsigned int dummy;
21618
21619 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21620 SYMBOL_VALUE_ADDRESS (sym) =
21621 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21622 else
21623 SYMBOL_VALUE_ADDRESS (sym) =
21624 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21625 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21626 fixup_symbol_section (sym, objfile);
21627 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21628 SYMBOL_SECTION (sym));
21629 return;
21630 }
21631
21632 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21633 expression evaluator, and use LOC_COMPUTED only when necessary
21634 (i.e. when the value of a register or memory location is
21635 referenced, or a thread-local block, etc.). Then again, it might
21636 not be worthwhile. I'm assuming that it isn't unless performance
21637 or memory numbers show me otherwise. */
21638
21639 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21640
21641 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21642 cu->has_loclist = 1;
21643 }
21644
21645 /* Given a pointer to a DWARF information entry, figure out if we need
21646 to make a symbol table entry for it, and if so, create a new entry
21647 and return a pointer to it.
21648 If TYPE is NULL, determine symbol type from the die, otherwise
21649 used the passed type.
21650 If SPACE is not NULL, use it to hold the new symbol. If it is
21651 NULL, allocate a new symbol on the objfile's obstack. */
21652
21653 static struct symbol *
21654 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21655 struct symbol *space)
21656 {
21657 struct dwarf2_per_objfile *dwarf2_per_objfile
21658 = cu->per_cu->dwarf2_per_objfile;
21659 struct objfile *objfile = dwarf2_per_objfile->objfile;
21660 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21661 struct symbol *sym = NULL;
21662 const char *name;
21663 struct attribute *attr = NULL;
21664 struct attribute *attr2 = NULL;
21665 CORE_ADDR baseaddr;
21666 struct pending **list_to_add = NULL;
21667
21668 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21669
21670 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21671
21672 name = dwarf2_name (die, cu);
21673 if (name)
21674 {
21675 const char *linkagename;
21676 int suppress_add = 0;
21677
21678 if (space)
21679 sym = space;
21680 else
21681 sym = allocate_symbol (objfile);
21682 OBJSTAT (objfile, n_syms++);
21683
21684 /* Cache this symbol's name and the name's demangled form (if any). */
21685 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21686 linkagename = dwarf2_physname (name, die, cu);
21687 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21688
21689 /* Fortran does not have mangling standard and the mangling does differ
21690 between gfortran, iFort etc. */
21691 if (cu->language == language_fortran
21692 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21693 symbol_set_demangled_name (&(sym->ginfo),
21694 dwarf2_full_name (name, die, cu),
21695 NULL);
21696
21697 /* Default assumptions.
21698 Use the passed type or decode it from the die. */
21699 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21700 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21701 if (type != NULL)
21702 SYMBOL_TYPE (sym) = type;
21703 else
21704 SYMBOL_TYPE (sym) = die_type (die, cu);
21705 attr = dwarf2_attr (die,
21706 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21707 cu);
21708 if (attr)
21709 {
21710 SYMBOL_LINE (sym) = DW_UNSND (attr);
21711 }
21712
21713 attr = dwarf2_attr (die,
21714 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21715 cu);
21716 if (attr)
21717 {
21718 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21719 struct file_entry *fe;
21720
21721 if (cu->line_header != NULL)
21722 fe = cu->line_header->file_name_at (file_index);
21723 else
21724 fe = NULL;
21725
21726 if (fe == NULL)
21727 complaint (&symfile_complaints,
21728 _("file index out of range"));
21729 else
21730 symbol_set_symtab (sym, fe->symtab);
21731 }
21732
21733 switch (die->tag)
21734 {
21735 case DW_TAG_label:
21736 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21737 if (attr)
21738 {
21739 CORE_ADDR addr;
21740
21741 addr = attr_value_as_address (attr);
21742 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21743 SYMBOL_VALUE_ADDRESS (sym) = addr;
21744 }
21745 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21746 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21747 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21748 add_symbol_to_list (sym, cu->list_in_scope);
21749 break;
21750 case DW_TAG_subprogram:
21751 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21752 finish_block. */
21753 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21754 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21755 if ((attr2 && (DW_UNSND (attr2) != 0))
21756 || cu->language == language_ada)
21757 {
21758 /* Subprograms marked external are stored as a global symbol.
21759 Ada subprograms, whether marked external or not, are always
21760 stored as a global symbol, because we want to be able to
21761 access them globally. For instance, we want to be able
21762 to break on a nested subprogram without having to
21763 specify the context. */
21764 list_to_add = &global_symbols;
21765 }
21766 else
21767 {
21768 list_to_add = cu->list_in_scope;
21769 }
21770 break;
21771 case DW_TAG_inlined_subroutine:
21772 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21773 finish_block. */
21774 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21775 SYMBOL_INLINED (sym) = 1;
21776 list_to_add = cu->list_in_scope;
21777 break;
21778 case DW_TAG_template_value_param:
21779 suppress_add = 1;
21780 /* Fall through. */
21781 case DW_TAG_constant:
21782 case DW_TAG_variable:
21783 case DW_TAG_member:
21784 /* Compilation with minimal debug info may result in
21785 variables with missing type entries. Change the
21786 misleading `void' type to something sensible. */
21787 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21788 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21789
21790 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21791 /* In the case of DW_TAG_member, we should only be called for
21792 static const members. */
21793 if (die->tag == DW_TAG_member)
21794 {
21795 /* dwarf2_add_field uses die_is_declaration,
21796 so we do the same. */
21797 gdb_assert (die_is_declaration (die, cu));
21798 gdb_assert (attr);
21799 }
21800 if (attr)
21801 {
21802 dwarf2_const_value (attr, sym, cu);
21803 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21804 if (!suppress_add)
21805 {
21806 if (attr2 && (DW_UNSND (attr2) != 0))
21807 list_to_add = &global_symbols;
21808 else
21809 list_to_add = cu->list_in_scope;
21810 }
21811 break;
21812 }
21813 attr = dwarf2_attr (die, DW_AT_location, cu);
21814 if (attr)
21815 {
21816 var_decode_location (attr, sym, cu);
21817 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21818
21819 /* Fortran explicitly imports any global symbols to the local
21820 scope by DW_TAG_common_block. */
21821 if (cu->language == language_fortran && die->parent
21822 && die->parent->tag == DW_TAG_common_block)
21823 attr2 = NULL;
21824
21825 if (SYMBOL_CLASS (sym) == LOC_STATIC
21826 && SYMBOL_VALUE_ADDRESS (sym) == 0
21827 && !dwarf2_per_objfile->has_section_at_zero)
21828 {
21829 /* When a static variable is eliminated by the linker,
21830 the corresponding debug information is not stripped
21831 out, but the variable address is set to null;
21832 do not add such variables into symbol table. */
21833 }
21834 else if (attr2 && (DW_UNSND (attr2) != 0))
21835 {
21836 /* Workaround gfortran PR debug/40040 - it uses
21837 DW_AT_location for variables in -fPIC libraries which may
21838 get overriden by other libraries/executable and get
21839 a different address. Resolve it by the minimal symbol
21840 which may come from inferior's executable using copy
21841 relocation. Make this workaround only for gfortran as for
21842 other compilers GDB cannot guess the minimal symbol
21843 Fortran mangling kind. */
21844 if (cu->language == language_fortran && die->parent
21845 && die->parent->tag == DW_TAG_module
21846 && cu->producer
21847 && startswith (cu->producer, "GNU Fortran"))
21848 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21849
21850 /* A variable with DW_AT_external is never static,
21851 but it may be block-scoped. */
21852 list_to_add = (cu->list_in_scope == &file_symbols
21853 ? &global_symbols : cu->list_in_scope);
21854 }
21855 else
21856 list_to_add = cu->list_in_scope;
21857 }
21858 else
21859 {
21860 /* We do not know the address of this symbol.
21861 If it is an external symbol and we have type information
21862 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21863 The address of the variable will then be determined from
21864 the minimal symbol table whenever the variable is
21865 referenced. */
21866 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21867
21868 /* Fortran explicitly imports any global symbols to the local
21869 scope by DW_TAG_common_block. */
21870 if (cu->language == language_fortran && die->parent
21871 && die->parent->tag == DW_TAG_common_block)
21872 {
21873 /* SYMBOL_CLASS doesn't matter here because
21874 read_common_block is going to reset it. */
21875 if (!suppress_add)
21876 list_to_add = cu->list_in_scope;
21877 }
21878 else if (attr2 && (DW_UNSND (attr2) != 0)
21879 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21880 {
21881 /* A variable with DW_AT_external is never static, but it
21882 may be block-scoped. */
21883 list_to_add = (cu->list_in_scope == &file_symbols
21884 ? &global_symbols : cu->list_in_scope);
21885
21886 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21887 }
21888 else if (!die_is_declaration (die, cu))
21889 {
21890 /* Use the default LOC_OPTIMIZED_OUT class. */
21891 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21892 if (!suppress_add)
21893 list_to_add = cu->list_in_scope;
21894 }
21895 }
21896 break;
21897 case DW_TAG_formal_parameter:
21898 /* If we are inside a function, mark this as an argument. If
21899 not, we might be looking at an argument to an inlined function
21900 when we do not have enough information to show inlined frames;
21901 pretend it's a local variable in that case so that the user can
21902 still see it. */
21903 if (context_stack_depth > 0
21904 && context_stack[context_stack_depth - 1].name != NULL)
21905 SYMBOL_IS_ARGUMENT (sym) = 1;
21906 attr = dwarf2_attr (die, DW_AT_location, cu);
21907 if (attr)
21908 {
21909 var_decode_location (attr, sym, cu);
21910 }
21911 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21912 if (attr)
21913 {
21914 dwarf2_const_value (attr, sym, cu);
21915 }
21916
21917 list_to_add = cu->list_in_scope;
21918 break;
21919 case DW_TAG_unspecified_parameters:
21920 /* From varargs functions; gdb doesn't seem to have any
21921 interest in this information, so just ignore it for now.
21922 (FIXME?) */
21923 break;
21924 case DW_TAG_template_type_param:
21925 suppress_add = 1;
21926 /* Fall through. */
21927 case DW_TAG_class_type:
21928 case DW_TAG_interface_type:
21929 case DW_TAG_structure_type:
21930 case DW_TAG_union_type:
21931 case DW_TAG_set_type:
21932 case DW_TAG_enumeration_type:
21933 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21934 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21935
21936 {
21937 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21938 really ever be static objects: otherwise, if you try
21939 to, say, break of a class's method and you're in a file
21940 which doesn't mention that class, it won't work unless
21941 the check for all static symbols in lookup_symbol_aux
21942 saves you. See the OtherFileClass tests in
21943 gdb.c++/namespace.exp. */
21944
21945 if (!suppress_add)
21946 {
21947 list_to_add = (cu->list_in_scope == &file_symbols
21948 && cu->language == language_cplus
21949 ? &global_symbols : cu->list_in_scope);
21950
21951 /* The semantics of C++ state that "struct foo {
21952 ... }" also defines a typedef for "foo". */
21953 if (cu->language == language_cplus
21954 || cu->language == language_ada
21955 || cu->language == language_d
21956 || cu->language == language_rust)
21957 {
21958 /* The symbol's name is already allocated along
21959 with this objfile, so we don't need to
21960 duplicate it for the type. */
21961 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21962 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21963 }
21964 }
21965 }
21966 break;
21967 case DW_TAG_typedef:
21968 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21969 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21970 list_to_add = cu->list_in_scope;
21971 break;
21972 case DW_TAG_base_type:
21973 case DW_TAG_subrange_type:
21974 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21975 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21976 list_to_add = cu->list_in_scope;
21977 break;
21978 case DW_TAG_enumerator:
21979 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21980 if (attr)
21981 {
21982 dwarf2_const_value (attr, sym, cu);
21983 }
21984 {
21985 /* NOTE: carlton/2003-11-10: See comment above in the
21986 DW_TAG_class_type, etc. block. */
21987
21988 list_to_add = (cu->list_in_scope == &file_symbols
21989 && cu->language == language_cplus
21990 ? &global_symbols : cu->list_in_scope);
21991 }
21992 break;
21993 case DW_TAG_imported_declaration:
21994 case DW_TAG_namespace:
21995 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21996 list_to_add = &global_symbols;
21997 break;
21998 case DW_TAG_module:
21999 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22000 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22001 list_to_add = &global_symbols;
22002 break;
22003 case DW_TAG_common_block:
22004 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22005 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22006 add_symbol_to_list (sym, cu->list_in_scope);
22007 break;
22008 default:
22009 /* Not a tag we recognize. Hopefully we aren't processing
22010 trash data, but since we must specifically ignore things
22011 we don't recognize, there is nothing else we should do at
22012 this point. */
22013 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
22014 dwarf_tag_name (die->tag));
22015 break;
22016 }
22017
22018 if (suppress_add)
22019 {
22020 sym->hash_next = objfile->template_symbols;
22021 objfile->template_symbols = sym;
22022 list_to_add = NULL;
22023 }
22024
22025 if (list_to_add != NULL)
22026 add_symbol_to_list (sym, list_to_add);
22027
22028 /* For the benefit of old versions of GCC, check for anonymous
22029 namespaces based on the demangled name. */
22030 if (!cu->processing_has_namespace_info
22031 && cu->language == language_cplus)
22032 cp_scan_for_anonymous_namespaces (sym, objfile);
22033 }
22034 return (sym);
22035 }
22036
22037 /* Given an attr with a DW_FORM_dataN value in host byte order,
22038 zero-extend it as appropriate for the symbol's type. The DWARF
22039 standard (v4) is not entirely clear about the meaning of using
22040 DW_FORM_dataN for a constant with a signed type, where the type is
22041 wider than the data. The conclusion of a discussion on the DWARF
22042 list was that this is unspecified. We choose to always zero-extend
22043 because that is the interpretation long in use by GCC. */
22044
22045 static gdb_byte *
22046 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22047 struct dwarf2_cu *cu, LONGEST *value, int bits)
22048 {
22049 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22050 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22051 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22052 LONGEST l = DW_UNSND (attr);
22053
22054 if (bits < sizeof (*value) * 8)
22055 {
22056 l &= ((LONGEST) 1 << bits) - 1;
22057 *value = l;
22058 }
22059 else if (bits == sizeof (*value) * 8)
22060 *value = l;
22061 else
22062 {
22063 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22064 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22065 return bytes;
22066 }
22067
22068 return NULL;
22069 }
22070
22071 /* Read a constant value from an attribute. Either set *VALUE, or if
22072 the value does not fit in *VALUE, set *BYTES - either already
22073 allocated on the objfile obstack, or newly allocated on OBSTACK,
22074 or, set *BATON, if we translated the constant to a location
22075 expression. */
22076
22077 static void
22078 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22079 const char *name, struct obstack *obstack,
22080 struct dwarf2_cu *cu,
22081 LONGEST *value, const gdb_byte **bytes,
22082 struct dwarf2_locexpr_baton **baton)
22083 {
22084 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22085 struct comp_unit_head *cu_header = &cu->header;
22086 struct dwarf_block *blk;
22087 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22088 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22089
22090 *value = 0;
22091 *bytes = NULL;
22092 *baton = NULL;
22093
22094 switch (attr->form)
22095 {
22096 case DW_FORM_addr:
22097 case DW_FORM_GNU_addr_index:
22098 {
22099 gdb_byte *data;
22100
22101 if (TYPE_LENGTH (type) != cu_header->addr_size)
22102 dwarf2_const_value_length_mismatch_complaint (name,
22103 cu_header->addr_size,
22104 TYPE_LENGTH (type));
22105 /* Symbols of this form are reasonably rare, so we just
22106 piggyback on the existing location code rather than writing
22107 a new implementation of symbol_computed_ops. */
22108 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22109 (*baton)->per_cu = cu->per_cu;
22110 gdb_assert ((*baton)->per_cu);
22111
22112 (*baton)->size = 2 + cu_header->addr_size;
22113 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22114 (*baton)->data = data;
22115
22116 data[0] = DW_OP_addr;
22117 store_unsigned_integer (&data[1], cu_header->addr_size,
22118 byte_order, DW_ADDR (attr));
22119 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22120 }
22121 break;
22122 case DW_FORM_string:
22123 case DW_FORM_strp:
22124 case DW_FORM_GNU_str_index:
22125 case DW_FORM_GNU_strp_alt:
22126 /* DW_STRING is already allocated on the objfile obstack, point
22127 directly to it. */
22128 *bytes = (const gdb_byte *) DW_STRING (attr);
22129 break;
22130 case DW_FORM_block1:
22131 case DW_FORM_block2:
22132 case DW_FORM_block4:
22133 case DW_FORM_block:
22134 case DW_FORM_exprloc:
22135 case DW_FORM_data16:
22136 blk = DW_BLOCK (attr);
22137 if (TYPE_LENGTH (type) != blk->size)
22138 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22139 TYPE_LENGTH (type));
22140 *bytes = blk->data;
22141 break;
22142
22143 /* The DW_AT_const_value attributes are supposed to carry the
22144 symbol's value "represented as it would be on the target
22145 architecture." By the time we get here, it's already been
22146 converted to host endianness, so we just need to sign- or
22147 zero-extend it as appropriate. */
22148 case DW_FORM_data1:
22149 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22150 break;
22151 case DW_FORM_data2:
22152 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22153 break;
22154 case DW_FORM_data4:
22155 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22156 break;
22157 case DW_FORM_data8:
22158 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22159 break;
22160
22161 case DW_FORM_sdata:
22162 case DW_FORM_implicit_const:
22163 *value = DW_SND (attr);
22164 break;
22165
22166 case DW_FORM_udata:
22167 *value = DW_UNSND (attr);
22168 break;
22169
22170 default:
22171 complaint (&symfile_complaints,
22172 _("unsupported const value attribute form: '%s'"),
22173 dwarf_form_name (attr->form));
22174 *value = 0;
22175 break;
22176 }
22177 }
22178
22179
22180 /* Copy constant value from an attribute to a symbol. */
22181
22182 static void
22183 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22184 struct dwarf2_cu *cu)
22185 {
22186 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22187 LONGEST value;
22188 const gdb_byte *bytes;
22189 struct dwarf2_locexpr_baton *baton;
22190
22191 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22192 SYMBOL_PRINT_NAME (sym),
22193 &objfile->objfile_obstack, cu,
22194 &value, &bytes, &baton);
22195
22196 if (baton != NULL)
22197 {
22198 SYMBOL_LOCATION_BATON (sym) = baton;
22199 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22200 }
22201 else if (bytes != NULL)
22202 {
22203 SYMBOL_VALUE_BYTES (sym) = bytes;
22204 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22205 }
22206 else
22207 {
22208 SYMBOL_VALUE (sym) = value;
22209 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22210 }
22211 }
22212
22213 /* Return the type of the die in question using its DW_AT_type attribute. */
22214
22215 static struct type *
22216 die_type (struct die_info *die, struct dwarf2_cu *cu)
22217 {
22218 struct attribute *type_attr;
22219
22220 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22221 if (!type_attr)
22222 {
22223 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22224 /* A missing DW_AT_type represents a void type. */
22225 return objfile_type (objfile)->builtin_void;
22226 }
22227
22228 return lookup_die_type (die, type_attr, cu);
22229 }
22230
22231 /* True iff CU's producer generates GNAT Ada auxiliary information
22232 that allows to find parallel types through that information instead
22233 of having to do expensive parallel lookups by type name. */
22234
22235 static int
22236 need_gnat_info (struct dwarf2_cu *cu)
22237 {
22238 /* Assume that the Ada compiler was GNAT, which always produces
22239 the auxiliary information. */
22240 return (cu->language == language_ada);
22241 }
22242
22243 /* Return the auxiliary type of the die in question using its
22244 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22245 attribute is not present. */
22246
22247 static struct type *
22248 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22249 {
22250 struct attribute *type_attr;
22251
22252 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22253 if (!type_attr)
22254 return NULL;
22255
22256 return lookup_die_type (die, type_attr, cu);
22257 }
22258
22259 /* If DIE has a descriptive_type attribute, then set the TYPE's
22260 descriptive type accordingly. */
22261
22262 static void
22263 set_descriptive_type (struct type *type, struct die_info *die,
22264 struct dwarf2_cu *cu)
22265 {
22266 struct type *descriptive_type = die_descriptive_type (die, cu);
22267
22268 if (descriptive_type)
22269 {
22270 ALLOCATE_GNAT_AUX_TYPE (type);
22271 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22272 }
22273 }
22274
22275 /* Return the containing type of the die in question using its
22276 DW_AT_containing_type attribute. */
22277
22278 static struct type *
22279 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22280 {
22281 struct attribute *type_attr;
22282 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22283
22284 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22285 if (!type_attr)
22286 error (_("Dwarf Error: Problem turning containing type into gdb type "
22287 "[in module %s]"), objfile_name (objfile));
22288
22289 return lookup_die_type (die, type_attr, cu);
22290 }
22291
22292 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22293
22294 static struct type *
22295 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22296 {
22297 struct dwarf2_per_objfile *dwarf2_per_objfile
22298 = cu->per_cu->dwarf2_per_objfile;
22299 struct objfile *objfile = dwarf2_per_objfile->objfile;
22300 char *message, *saved;
22301
22302 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
22303 objfile_name (objfile),
22304 sect_offset_str (cu->header.sect_off),
22305 sect_offset_str (die->sect_off));
22306 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
22307 message, strlen (message));
22308 xfree (message);
22309
22310 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22311 }
22312
22313 /* Look up the type of DIE in CU using its type attribute ATTR.
22314 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22315 DW_AT_containing_type.
22316 If there is no type substitute an error marker. */
22317
22318 static struct type *
22319 lookup_die_type (struct die_info *die, const struct attribute *attr,
22320 struct dwarf2_cu *cu)
22321 {
22322 struct dwarf2_per_objfile *dwarf2_per_objfile
22323 = cu->per_cu->dwarf2_per_objfile;
22324 struct objfile *objfile = dwarf2_per_objfile->objfile;
22325 struct type *this_type;
22326
22327 gdb_assert (attr->name == DW_AT_type
22328 || attr->name == DW_AT_GNAT_descriptive_type
22329 || attr->name == DW_AT_containing_type);
22330
22331 /* First see if we have it cached. */
22332
22333 if (attr->form == DW_FORM_GNU_ref_alt)
22334 {
22335 struct dwarf2_per_cu_data *per_cu;
22336 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22337
22338 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22339 dwarf2_per_objfile);
22340 this_type = get_die_type_at_offset (sect_off, per_cu);
22341 }
22342 else if (attr_form_is_ref (attr))
22343 {
22344 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22345
22346 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22347 }
22348 else if (attr->form == DW_FORM_ref_sig8)
22349 {
22350 ULONGEST signature = DW_SIGNATURE (attr);
22351
22352 return get_signatured_type (die, signature, cu);
22353 }
22354 else
22355 {
22356 complaint (&symfile_complaints,
22357 _("Dwarf Error: Bad type attribute %s in DIE"
22358 " at %s [in module %s]"),
22359 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22360 objfile_name (objfile));
22361 return build_error_marker_type (cu, die);
22362 }
22363
22364 /* If not cached we need to read it in. */
22365
22366 if (this_type == NULL)
22367 {
22368 struct die_info *type_die = NULL;
22369 struct dwarf2_cu *type_cu = cu;
22370
22371 if (attr_form_is_ref (attr))
22372 type_die = follow_die_ref (die, attr, &type_cu);
22373 if (type_die == NULL)
22374 return build_error_marker_type (cu, die);
22375 /* If we find the type now, it's probably because the type came
22376 from an inter-CU reference and the type's CU got expanded before
22377 ours. */
22378 this_type = read_type_die (type_die, type_cu);
22379 }
22380
22381 /* If we still don't have a type use an error marker. */
22382
22383 if (this_type == NULL)
22384 return build_error_marker_type (cu, die);
22385
22386 return this_type;
22387 }
22388
22389 /* Return the type in DIE, CU.
22390 Returns NULL for invalid types.
22391
22392 This first does a lookup in die_type_hash,
22393 and only reads the die in if necessary.
22394
22395 NOTE: This can be called when reading in partial or full symbols. */
22396
22397 static struct type *
22398 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22399 {
22400 struct type *this_type;
22401
22402 this_type = get_die_type (die, cu);
22403 if (this_type)
22404 return this_type;
22405
22406 return read_type_die_1 (die, cu);
22407 }
22408
22409 /* Read the type in DIE, CU.
22410 Returns NULL for invalid types. */
22411
22412 static struct type *
22413 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22414 {
22415 struct type *this_type = NULL;
22416
22417 switch (die->tag)
22418 {
22419 case DW_TAG_class_type:
22420 case DW_TAG_interface_type:
22421 case DW_TAG_structure_type:
22422 case DW_TAG_union_type:
22423 this_type = read_structure_type (die, cu);
22424 break;
22425 case DW_TAG_enumeration_type:
22426 this_type = read_enumeration_type (die, cu);
22427 break;
22428 case DW_TAG_subprogram:
22429 case DW_TAG_subroutine_type:
22430 case DW_TAG_inlined_subroutine:
22431 this_type = read_subroutine_type (die, cu);
22432 break;
22433 case DW_TAG_array_type:
22434 this_type = read_array_type (die, cu);
22435 break;
22436 case DW_TAG_set_type:
22437 this_type = read_set_type (die, cu);
22438 break;
22439 case DW_TAG_pointer_type:
22440 this_type = read_tag_pointer_type (die, cu);
22441 break;
22442 case DW_TAG_ptr_to_member_type:
22443 this_type = read_tag_ptr_to_member_type (die, cu);
22444 break;
22445 case DW_TAG_reference_type:
22446 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22447 break;
22448 case DW_TAG_rvalue_reference_type:
22449 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22450 break;
22451 case DW_TAG_const_type:
22452 this_type = read_tag_const_type (die, cu);
22453 break;
22454 case DW_TAG_volatile_type:
22455 this_type = read_tag_volatile_type (die, cu);
22456 break;
22457 case DW_TAG_restrict_type:
22458 this_type = read_tag_restrict_type (die, cu);
22459 break;
22460 case DW_TAG_string_type:
22461 this_type = read_tag_string_type (die, cu);
22462 break;
22463 case DW_TAG_typedef:
22464 this_type = read_typedef (die, cu);
22465 break;
22466 case DW_TAG_subrange_type:
22467 this_type = read_subrange_type (die, cu);
22468 break;
22469 case DW_TAG_base_type:
22470 this_type = read_base_type (die, cu);
22471 break;
22472 case DW_TAG_unspecified_type:
22473 this_type = read_unspecified_type (die, cu);
22474 break;
22475 case DW_TAG_namespace:
22476 this_type = read_namespace_type (die, cu);
22477 break;
22478 case DW_TAG_module:
22479 this_type = read_module_type (die, cu);
22480 break;
22481 case DW_TAG_atomic_type:
22482 this_type = read_tag_atomic_type (die, cu);
22483 break;
22484 default:
22485 complaint (&symfile_complaints,
22486 _("unexpected tag in read_type_die: '%s'"),
22487 dwarf_tag_name (die->tag));
22488 break;
22489 }
22490
22491 return this_type;
22492 }
22493
22494 /* See if we can figure out if the class lives in a namespace. We do
22495 this by looking for a member function; its demangled name will
22496 contain namespace info, if there is any.
22497 Return the computed name or NULL.
22498 Space for the result is allocated on the objfile's obstack.
22499 This is the full-die version of guess_partial_die_structure_name.
22500 In this case we know DIE has no useful parent. */
22501
22502 static char *
22503 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22504 {
22505 struct die_info *spec_die;
22506 struct dwarf2_cu *spec_cu;
22507 struct die_info *child;
22508 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22509
22510 spec_cu = cu;
22511 spec_die = die_specification (die, &spec_cu);
22512 if (spec_die != NULL)
22513 {
22514 die = spec_die;
22515 cu = spec_cu;
22516 }
22517
22518 for (child = die->child;
22519 child != NULL;
22520 child = child->sibling)
22521 {
22522 if (child->tag == DW_TAG_subprogram)
22523 {
22524 const char *linkage_name = dw2_linkage_name (child, cu);
22525
22526 if (linkage_name != NULL)
22527 {
22528 char *actual_name
22529 = language_class_name_from_physname (cu->language_defn,
22530 linkage_name);
22531 char *name = NULL;
22532
22533 if (actual_name != NULL)
22534 {
22535 const char *die_name = dwarf2_name (die, cu);
22536
22537 if (die_name != NULL
22538 && strcmp (die_name, actual_name) != 0)
22539 {
22540 /* Strip off the class name from the full name.
22541 We want the prefix. */
22542 int die_name_len = strlen (die_name);
22543 int actual_name_len = strlen (actual_name);
22544
22545 /* Test for '::' as a sanity check. */
22546 if (actual_name_len > die_name_len + 2
22547 && actual_name[actual_name_len
22548 - die_name_len - 1] == ':')
22549 name = (char *) obstack_copy0 (
22550 &objfile->per_bfd->storage_obstack,
22551 actual_name, actual_name_len - die_name_len - 2);
22552 }
22553 }
22554 xfree (actual_name);
22555 return name;
22556 }
22557 }
22558 }
22559
22560 return NULL;
22561 }
22562
22563 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22564 prefix part in such case. See
22565 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22566
22567 static const char *
22568 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22569 {
22570 struct attribute *attr;
22571 const char *base;
22572
22573 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22574 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22575 return NULL;
22576
22577 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22578 return NULL;
22579
22580 attr = dw2_linkage_name_attr (die, cu);
22581 if (attr == NULL || DW_STRING (attr) == NULL)
22582 return NULL;
22583
22584 /* dwarf2_name had to be already called. */
22585 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22586
22587 /* Strip the base name, keep any leading namespaces/classes. */
22588 base = strrchr (DW_STRING (attr), ':');
22589 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22590 return "";
22591
22592 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22593 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22594 DW_STRING (attr),
22595 &base[-1] - DW_STRING (attr));
22596 }
22597
22598 /* Return the name of the namespace/class that DIE is defined within,
22599 or "" if we can't tell. The caller should not xfree the result.
22600
22601 For example, if we're within the method foo() in the following
22602 code:
22603
22604 namespace N {
22605 class C {
22606 void foo () {
22607 }
22608 };
22609 }
22610
22611 then determine_prefix on foo's die will return "N::C". */
22612
22613 static const char *
22614 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22615 {
22616 struct dwarf2_per_objfile *dwarf2_per_objfile
22617 = cu->per_cu->dwarf2_per_objfile;
22618 struct die_info *parent, *spec_die;
22619 struct dwarf2_cu *spec_cu;
22620 struct type *parent_type;
22621 const char *retval;
22622
22623 if (cu->language != language_cplus
22624 && cu->language != language_fortran && cu->language != language_d
22625 && cu->language != language_rust)
22626 return "";
22627
22628 retval = anonymous_struct_prefix (die, cu);
22629 if (retval)
22630 return retval;
22631
22632 /* We have to be careful in the presence of DW_AT_specification.
22633 For example, with GCC 3.4, given the code
22634
22635 namespace N {
22636 void foo() {
22637 // Definition of N::foo.
22638 }
22639 }
22640
22641 then we'll have a tree of DIEs like this:
22642
22643 1: DW_TAG_compile_unit
22644 2: DW_TAG_namespace // N
22645 3: DW_TAG_subprogram // declaration of N::foo
22646 4: DW_TAG_subprogram // definition of N::foo
22647 DW_AT_specification // refers to die #3
22648
22649 Thus, when processing die #4, we have to pretend that we're in
22650 the context of its DW_AT_specification, namely the contex of die
22651 #3. */
22652 spec_cu = cu;
22653 spec_die = die_specification (die, &spec_cu);
22654 if (spec_die == NULL)
22655 parent = die->parent;
22656 else
22657 {
22658 parent = spec_die->parent;
22659 cu = spec_cu;
22660 }
22661
22662 if (parent == NULL)
22663 return "";
22664 else if (parent->building_fullname)
22665 {
22666 const char *name;
22667 const char *parent_name;
22668
22669 /* It has been seen on RealView 2.2 built binaries,
22670 DW_TAG_template_type_param types actually _defined_ as
22671 children of the parent class:
22672
22673 enum E {};
22674 template class <class Enum> Class{};
22675 Class<enum E> class_e;
22676
22677 1: DW_TAG_class_type (Class)
22678 2: DW_TAG_enumeration_type (E)
22679 3: DW_TAG_enumerator (enum1:0)
22680 3: DW_TAG_enumerator (enum2:1)
22681 ...
22682 2: DW_TAG_template_type_param
22683 DW_AT_type DW_FORM_ref_udata (E)
22684
22685 Besides being broken debug info, it can put GDB into an
22686 infinite loop. Consider:
22687
22688 When we're building the full name for Class<E>, we'll start
22689 at Class, and go look over its template type parameters,
22690 finding E. We'll then try to build the full name of E, and
22691 reach here. We're now trying to build the full name of E,
22692 and look over the parent DIE for containing scope. In the
22693 broken case, if we followed the parent DIE of E, we'd again
22694 find Class, and once again go look at its template type
22695 arguments, etc., etc. Simply don't consider such parent die
22696 as source-level parent of this die (it can't be, the language
22697 doesn't allow it), and break the loop here. */
22698 name = dwarf2_name (die, cu);
22699 parent_name = dwarf2_name (parent, cu);
22700 complaint (&symfile_complaints,
22701 _("template param type '%s' defined within parent '%s'"),
22702 name ? name : "<unknown>",
22703 parent_name ? parent_name : "<unknown>");
22704 return "";
22705 }
22706 else
22707 switch (parent->tag)
22708 {
22709 case DW_TAG_namespace:
22710 parent_type = read_type_die (parent, cu);
22711 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22712 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22713 Work around this problem here. */
22714 if (cu->language == language_cplus
22715 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22716 return "";
22717 /* We give a name to even anonymous namespaces. */
22718 return TYPE_TAG_NAME (parent_type);
22719 case DW_TAG_class_type:
22720 case DW_TAG_interface_type:
22721 case DW_TAG_structure_type:
22722 case DW_TAG_union_type:
22723 case DW_TAG_module:
22724 parent_type = read_type_die (parent, cu);
22725 if (TYPE_TAG_NAME (parent_type) != NULL)
22726 return TYPE_TAG_NAME (parent_type);
22727 else
22728 /* An anonymous structure is only allowed non-static data
22729 members; no typedefs, no member functions, et cetera.
22730 So it does not need a prefix. */
22731 return "";
22732 case DW_TAG_compile_unit:
22733 case DW_TAG_partial_unit:
22734 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22735 if (cu->language == language_cplus
22736 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22737 && die->child != NULL
22738 && (die->tag == DW_TAG_class_type
22739 || die->tag == DW_TAG_structure_type
22740 || die->tag == DW_TAG_union_type))
22741 {
22742 char *name = guess_full_die_structure_name (die, cu);
22743 if (name != NULL)
22744 return name;
22745 }
22746 return "";
22747 case DW_TAG_enumeration_type:
22748 parent_type = read_type_die (parent, cu);
22749 if (TYPE_DECLARED_CLASS (parent_type))
22750 {
22751 if (TYPE_TAG_NAME (parent_type) != NULL)
22752 return TYPE_TAG_NAME (parent_type);
22753 return "";
22754 }
22755 /* Fall through. */
22756 default:
22757 return determine_prefix (parent, cu);
22758 }
22759 }
22760
22761 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22762 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22763 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22764 an obconcat, otherwise allocate storage for the result. The CU argument is
22765 used to determine the language and hence, the appropriate separator. */
22766
22767 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22768
22769 static char *
22770 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22771 int physname, struct dwarf2_cu *cu)
22772 {
22773 const char *lead = "";
22774 const char *sep;
22775
22776 if (suffix == NULL || suffix[0] == '\0'
22777 || prefix == NULL || prefix[0] == '\0')
22778 sep = "";
22779 else if (cu->language == language_d)
22780 {
22781 /* For D, the 'main' function could be defined in any module, but it
22782 should never be prefixed. */
22783 if (strcmp (suffix, "D main") == 0)
22784 {
22785 prefix = "";
22786 sep = "";
22787 }
22788 else
22789 sep = ".";
22790 }
22791 else if (cu->language == language_fortran && physname)
22792 {
22793 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22794 DW_AT_MIPS_linkage_name is preferred and used instead. */
22795
22796 lead = "__";
22797 sep = "_MOD_";
22798 }
22799 else
22800 sep = "::";
22801
22802 if (prefix == NULL)
22803 prefix = "";
22804 if (suffix == NULL)
22805 suffix = "";
22806
22807 if (obs == NULL)
22808 {
22809 char *retval
22810 = ((char *)
22811 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22812
22813 strcpy (retval, lead);
22814 strcat (retval, prefix);
22815 strcat (retval, sep);
22816 strcat (retval, suffix);
22817 return retval;
22818 }
22819 else
22820 {
22821 /* We have an obstack. */
22822 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22823 }
22824 }
22825
22826 /* Return sibling of die, NULL if no sibling. */
22827
22828 static struct die_info *
22829 sibling_die (struct die_info *die)
22830 {
22831 return die->sibling;
22832 }
22833
22834 /* Get name of a die, return NULL if not found. */
22835
22836 static const char *
22837 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22838 struct obstack *obstack)
22839 {
22840 if (name && cu->language == language_cplus)
22841 {
22842 std::string canon_name = cp_canonicalize_string (name);
22843
22844 if (!canon_name.empty ())
22845 {
22846 if (canon_name != name)
22847 name = (const char *) obstack_copy0 (obstack,
22848 canon_name.c_str (),
22849 canon_name.length ());
22850 }
22851 }
22852
22853 return name;
22854 }
22855
22856 /* Get name of a die, return NULL if not found.
22857 Anonymous namespaces are converted to their magic string. */
22858
22859 static const char *
22860 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22861 {
22862 struct attribute *attr;
22863 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22864
22865 attr = dwarf2_attr (die, DW_AT_name, cu);
22866 if ((!attr || !DW_STRING (attr))
22867 && die->tag != DW_TAG_namespace
22868 && die->tag != DW_TAG_class_type
22869 && die->tag != DW_TAG_interface_type
22870 && die->tag != DW_TAG_structure_type
22871 && die->tag != DW_TAG_union_type)
22872 return NULL;
22873
22874 switch (die->tag)
22875 {
22876 case DW_TAG_compile_unit:
22877 case DW_TAG_partial_unit:
22878 /* Compilation units have a DW_AT_name that is a filename, not
22879 a source language identifier. */
22880 case DW_TAG_enumeration_type:
22881 case DW_TAG_enumerator:
22882 /* These tags always have simple identifiers already; no need
22883 to canonicalize them. */
22884 return DW_STRING (attr);
22885
22886 case DW_TAG_namespace:
22887 if (attr != NULL && DW_STRING (attr) != NULL)
22888 return DW_STRING (attr);
22889 return CP_ANONYMOUS_NAMESPACE_STR;
22890
22891 case DW_TAG_class_type:
22892 case DW_TAG_interface_type:
22893 case DW_TAG_structure_type:
22894 case DW_TAG_union_type:
22895 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22896 structures or unions. These were of the form "._%d" in GCC 4.1,
22897 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22898 and GCC 4.4. We work around this problem by ignoring these. */
22899 if (attr && DW_STRING (attr)
22900 && (startswith (DW_STRING (attr), "._")
22901 || startswith (DW_STRING (attr), "<anonymous")))
22902 return NULL;
22903
22904 /* GCC might emit a nameless typedef that has a linkage name. See
22905 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22906 if (!attr || DW_STRING (attr) == NULL)
22907 {
22908 char *demangled = NULL;
22909
22910 attr = dw2_linkage_name_attr (die, cu);
22911 if (attr == NULL || DW_STRING (attr) == NULL)
22912 return NULL;
22913
22914 /* Avoid demangling DW_STRING (attr) the second time on a second
22915 call for the same DIE. */
22916 if (!DW_STRING_IS_CANONICAL (attr))
22917 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22918
22919 if (demangled)
22920 {
22921 const char *base;
22922
22923 /* FIXME: we already did this for the partial symbol... */
22924 DW_STRING (attr)
22925 = ((const char *)
22926 obstack_copy0 (&objfile->per_bfd->storage_obstack,
22927 demangled, strlen (demangled)));
22928 DW_STRING_IS_CANONICAL (attr) = 1;
22929 xfree (demangled);
22930
22931 /* Strip any leading namespaces/classes, keep only the base name.
22932 DW_AT_name for named DIEs does not contain the prefixes. */
22933 base = strrchr (DW_STRING (attr), ':');
22934 if (base && base > DW_STRING (attr) && base[-1] == ':')
22935 return &base[1];
22936 else
22937 return DW_STRING (attr);
22938 }
22939 }
22940 break;
22941
22942 default:
22943 break;
22944 }
22945
22946 if (!DW_STRING_IS_CANONICAL (attr))
22947 {
22948 DW_STRING (attr)
22949 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22950 &objfile->per_bfd->storage_obstack);
22951 DW_STRING_IS_CANONICAL (attr) = 1;
22952 }
22953 return DW_STRING (attr);
22954 }
22955
22956 /* Return the die that this die in an extension of, or NULL if there
22957 is none. *EXT_CU is the CU containing DIE on input, and the CU
22958 containing the return value on output. */
22959
22960 static struct die_info *
22961 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22962 {
22963 struct attribute *attr;
22964
22965 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22966 if (attr == NULL)
22967 return NULL;
22968
22969 return follow_die_ref (die, attr, ext_cu);
22970 }
22971
22972 /* Convert a DIE tag into its string name. */
22973
22974 static const char *
22975 dwarf_tag_name (unsigned tag)
22976 {
22977 const char *name = get_DW_TAG_name (tag);
22978
22979 if (name == NULL)
22980 return "DW_TAG_<unknown>";
22981
22982 return name;
22983 }
22984
22985 /* Convert a DWARF attribute code into its string name. */
22986
22987 static const char *
22988 dwarf_attr_name (unsigned attr)
22989 {
22990 const char *name;
22991
22992 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22993 if (attr == DW_AT_MIPS_fde)
22994 return "DW_AT_MIPS_fde";
22995 #else
22996 if (attr == DW_AT_HP_block_index)
22997 return "DW_AT_HP_block_index";
22998 #endif
22999
23000 name = get_DW_AT_name (attr);
23001
23002 if (name == NULL)
23003 return "DW_AT_<unknown>";
23004
23005 return name;
23006 }
23007
23008 /* Convert a DWARF value form code into its string name. */
23009
23010 static const char *
23011 dwarf_form_name (unsigned form)
23012 {
23013 const char *name = get_DW_FORM_name (form);
23014
23015 if (name == NULL)
23016 return "DW_FORM_<unknown>";
23017
23018 return name;
23019 }
23020
23021 static const char *
23022 dwarf_bool_name (unsigned mybool)
23023 {
23024 if (mybool)
23025 return "TRUE";
23026 else
23027 return "FALSE";
23028 }
23029
23030 /* Convert a DWARF type code into its string name. */
23031
23032 static const char *
23033 dwarf_type_encoding_name (unsigned enc)
23034 {
23035 const char *name = get_DW_ATE_name (enc);
23036
23037 if (name == NULL)
23038 return "DW_ATE_<unknown>";
23039
23040 return name;
23041 }
23042
23043 static void
23044 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23045 {
23046 unsigned int i;
23047
23048 print_spaces (indent, f);
23049 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23050 dwarf_tag_name (die->tag), die->abbrev,
23051 sect_offset_str (die->sect_off));
23052
23053 if (die->parent != NULL)
23054 {
23055 print_spaces (indent, f);
23056 fprintf_unfiltered (f, " parent at offset: %s\n",
23057 sect_offset_str (die->parent->sect_off));
23058 }
23059
23060 print_spaces (indent, f);
23061 fprintf_unfiltered (f, " has children: %s\n",
23062 dwarf_bool_name (die->child != NULL));
23063
23064 print_spaces (indent, f);
23065 fprintf_unfiltered (f, " attributes:\n");
23066
23067 for (i = 0; i < die->num_attrs; ++i)
23068 {
23069 print_spaces (indent, f);
23070 fprintf_unfiltered (f, " %s (%s) ",
23071 dwarf_attr_name (die->attrs[i].name),
23072 dwarf_form_name (die->attrs[i].form));
23073
23074 switch (die->attrs[i].form)
23075 {
23076 case DW_FORM_addr:
23077 case DW_FORM_GNU_addr_index:
23078 fprintf_unfiltered (f, "address: ");
23079 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
23080 break;
23081 case DW_FORM_block2:
23082 case DW_FORM_block4:
23083 case DW_FORM_block:
23084 case DW_FORM_block1:
23085 fprintf_unfiltered (f, "block: size %s",
23086 pulongest (DW_BLOCK (&die->attrs[i])->size));
23087 break;
23088 case DW_FORM_exprloc:
23089 fprintf_unfiltered (f, "expression: size %s",
23090 pulongest (DW_BLOCK (&die->attrs[i])->size));
23091 break;
23092 case DW_FORM_data16:
23093 fprintf_unfiltered (f, "constant of 16 bytes");
23094 break;
23095 case DW_FORM_ref_addr:
23096 fprintf_unfiltered (f, "ref address: ");
23097 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23098 break;
23099 case DW_FORM_GNU_ref_alt:
23100 fprintf_unfiltered (f, "alt ref address: ");
23101 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23102 break;
23103 case DW_FORM_ref1:
23104 case DW_FORM_ref2:
23105 case DW_FORM_ref4:
23106 case DW_FORM_ref8:
23107 case DW_FORM_ref_udata:
23108 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23109 (long) (DW_UNSND (&die->attrs[i])));
23110 break;
23111 case DW_FORM_data1:
23112 case DW_FORM_data2:
23113 case DW_FORM_data4:
23114 case DW_FORM_data8:
23115 case DW_FORM_udata:
23116 case DW_FORM_sdata:
23117 fprintf_unfiltered (f, "constant: %s",
23118 pulongest (DW_UNSND (&die->attrs[i])));
23119 break;
23120 case DW_FORM_sec_offset:
23121 fprintf_unfiltered (f, "section offset: %s",
23122 pulongest (DW_UNSND (&die->attrs[i])));
23123 break;
23124 case DW_FORM_ref_sig8:
23125 fprintf_unfiltered (f, "signature: %s",
23126 hex_string (DW_SIGNATURE (&die->attrs[i])));
23127 break;
23128 case DW_FORM_string:
23129 case DW_FORM_strp:
23130 case DW_FORM_line_strp:
23131 case DW_FORM_GNU_str_index:
23132 case DW_FORM_GNU_strp_alt:
23133 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23134 DW_STRING (&die->attrs[i])
23135 ? DW_STRING (&die->attrs[i]) : "",
23136 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23137 break;
23138 case DW_FORM_flag:
23139 if (DW_UNSND (&die->attrs[i]))
23140 fprintf_unfiltered (f, "flag: TRUE");
23141 else
23142 fprintf_unfiltered (f, "flag: FALSE");
23143 break;
23144 case DW_FORM_flag_present:
23145 fprintf_unfiltered (f, "flag: TRUE");
23146 break;
23147 case DW_FORM_indirect:
23148 /* The reader will have reduced the indirect form to
23149 the "base form" so this form should not occur. */
23150 fprintf_unfiltered (f,
23151 "unexpected attribute form: DW_FORM_indirect");
23152 break;
23153 case DW_FORM_implicit_const:
23154 fprintf_unfiltered (f, "constant: %s",
23155 plongest (DW_SND (&die->attrs[i])));
23156 break;
23157 default:
23158 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23159 die->attrs[i].form);
23160 break;
23161 }
23162 fprintf_unfiltered (f, "\n");
23163 }
23164 }
23165
23166 static void
23167 dump_die_for_error (struct die_info *die)
23168 {
23169 dump_die_shallow (gdb_stderr, 0, die);
23170 }
23171
23172 static void
23173 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23174 {
23175 int indent = level * 4;
23176
23177 gdb_assert (die != NULL);
23178
23179 if (level >= max_level)
23180 return;
23181
23182 dump_die_shallow (f, indent, die);
23183
23184 if (die->child != NULL)
23185 {
23186 print_spaces (indent, f);
23187 fprintf_unfiltered (f, " Children:");
23188 if (level + 1 < max_level)
23189 {
23190 fprintf_unfiltered (f, "\n");
23191 dump_die_1 (f, level + 1, max_level, die->child);
23192 }
23193 else
23194 {
23195 fprintf_unfiltered (f,
23196 " [not printed, max nesting level reached]\n");
23197 }
23198 }
23199
23200 if (die->sibling != NULL && level > 0)
23201 {
23202 dump_die_1 (f, level, max_level, die->sibling);
23203 }
23204 }
23205
23206 /* This is called from the pdie macro in gdbinit.in.
23207 It's not static so gcc will keep a copy callable from gdb. */
23208
23209 void
23210 dump_die (struct die_info *die, int max_level)
23211 {
23212 dump_die_1 (gdb_stdlog, 0, max_level, die);
23213 }
23214
23215 static void
23216 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23217 {
23218 void **slot;
23219
23220 slot = htab_find_slot_with_hash (cu->die_hash, die,
23221 to_underlying (die->sect_off),
23222 INSERT);
23223
23224 *slot = die;
23225 }
23226
23227 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23228 required kind. */
23229
23230 static sect_offset
23231 dwarf2_get_ref_die_offset (const struct attribute *attr)
23232 {
23233 if (attr_form_is_ref (attr))
23234 return (sect_offset) DW_UNSND (attr);
23235
23236 complaint (&symfile_complaints,
23237 _("unsupported die ref attribute form: '%s'"),
23238 dwarf_form_name (attr->form));
23239 return {};
23240 }
23241
23242 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23243 * the value held by the attribute is not constant. */
23244
23245 static LONGEST
23246 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23247 {
23248 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23249 return DW_SND (attr);
23250 else if (attr->form == DW_FORM_udata
23251 || attr->form == DW_FORM_data1
23252 || attr->form == DW_FORM_data2
23253 || attr->form == DW_FORM_data4
23254 || attr->form == DW_FORM_data8)
23255 return DW_UNSND (attr);
23256 else
23257 {
23258 /* For DW_FORM_data16 see attr_form_is_constant. */
23259 complaint (&symfile_complaints,
23260 _("Attribute value is not a constant (%s)"),
23261 dwarf_form_name (attr->form));
23262 return default_value;
23263 }
23264 }
23265
23266 /* Follow reference or signature attribute ATTR of SRC_DIE.
23267 On entry *REF_CU is the CU of SRC_DIE.
23268 On exit *REF_CU is the CU of the result. */
23269
23270 static struct die_info *
23271 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23272 struct dwarf2_cu **ref_cu)
23273 {
23274 struct die_info *die;
23275
23276 if (attr_form_is_ref (attr))
23277 die = follow_die_ref (src_die, attr, ref_cu);
23278 else if (attr->form == DW_FORM_ref_sig8)
23279 die = follow_die_sig (src_die, attr, ref_cu);
23280 else
23281 {
23282 dump_die_for_error (src_die);
23283 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23284 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23285 }
23286
23287 return die;
23288 }
23289
23290 /* Follow reference OFFSET.
23291 On entry *REF_CU is the CU of the source die referencing OFFSET.
23292 On exit *REF_CU is the CU of the result.
23293 Returns NULL if OFFSET is invalid. */
23294
23295 static struct die_info *
23296 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23297 struct dwarf2_cu **ref_cu)
23298 {
23299 struct die_info temp_die;
23300 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23301 struct dwarf2_per_objfile *dwarf2_per_objfile
23302 = cu->per_cu->dwarf2_per_objfile;
23303 struct objfile *objfile = dwarf2_per_objfile->objfile;
23304
23305 gdb_assert (cu->per_cu != NULL);
23306
23307 target_cu = cu;
23308
23309 if (cu->per_cu->is_debug_types)
23310 {
23311 /* .debug_types CUs cannot reference anything outside their CU.
23312 If they need to, they have to reference a signatured type via
23313 DW_FORM_ref_sig8. */
23314 if (!offset_in_cu_p (&cu->header, sect_off))
23315 return NULL;
23316 }
23317 else if (offset_in_dwz != cu->per_cu->is_dwz
23318 || !offset_in_cu_p (&cu->header, sect_off))
23319 {
23320 struct dwarf2_per_cu_data *per_cu;
23321
23322 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23323 dwarf2_per_objfile);
23324
23325 /* If necessary, add it to the queue and load its DIEs. */
23326 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23327 load_full_comp_unit (per_cu, cu->language);
23328
23329 target_cu = per_cu->cu;
23330 }
23331 else if (cu->dies == NULL)
23332 {
23333 /* We're loading full DIEs during partial symbol reading. */
23334 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23335 load_full_comp_unit (cu->per_cu, language_minimal);
23336 }
23337
23338 *ref_cu = target_cu;
23339 temp_die.sect_off = sect_off;
23340 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23341 &temp_die,
23342 to_underlying (sect_off));
23343 }
23344
23345 /* Follow reference attribute ATTR of SRC_DIE.
23346 On entry *REF_CU is the CU of SRC_DIE.
23347 On exit *REF_CU is the CU of the result. */
23348
23349 static struct die_info *
23350 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23351 struct dwarf2_cu **ref_cu)
23352 {
23353 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23354 struct dwarf2_cu *cu = *ref_cu;
23355 struct die_info *die;
23356
23357 die = follow_die_offset (sect_off,
23358 (attr->form == DW_FORM_GNU_ref_alt
23359 || cu->per_cu->is_dwz),
23360 ref_cu);
23361 if (!die)
23362 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23363 "at %s [in module %s]"),
23364 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23365 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23366
23367 return die;
23368 }
23369
23370 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23371 Returned value is intended for DW_OP_call*. Returned
23372 dwarf2_locexpr_baton->data has lifetime of
23373 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23374
23375 struct dwarf2_locexpr_baton
23376 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23377 struct dwarf2_per_cu_data *per_cu,
23378 CORE_ADDR (*get_frame_pc) (void *baton),
23379 void *baton)
23380 {
23381 struct dwarf2_cu *cu;
23382 struct die_info *die;
23383 struct attribute *attr;
23384 struct dwarf2_locexpr_baton retval;
23385 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23386 struct dwarf2_per_objfile *dwarf2_per_objfile
23387 = get_dwarf2_per_objfile (objfile);
23388
23389 if (per_cu->cu == NULL)
23390 load_cu (per_cu);
23391 cu = per_cu->cu;
23392 if (cu == NULL)
23393 {
23394 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23395 Instead just throw an error, not much else we can do. */
23396 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23397 sect_offset_str (sect_off), objfile_name (objfile));
23398 }
23399
23400 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23401 if (!die)
23402 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23403 sect_offset_str (sect_off), objfile_name (objfile));
23404
23405 attr = dwarf2_attr (die, DW_AT_location, cu);
23406 if (!attr)
23407 {
23408 /* DWARF: "If there is no such attribute, then there is no effect.".
23409 DATA is ignored if SIZE is 0. */
23410
23411 retval.data = NULL;
23412 retval.size = 0;
23413 }
23414 else if (attr_form_is_section_offset (attr))
23415 {
23416 struct dwarf2_loclist_baton loclist_baton;
23417 CORE_ADDR pc = (*get_frame_pc) (baton);
23418 size_t size;
23419
23420 fill_in_loclist_baton (cu, &loclist_baton, attr);
23421
23422 retval.data = dwarf2_find_location_expression (&loclist_baton,
23423 &size, pc);
23424 retval.size = size;
23425 }
23426 else
23427 {
23428 if (!attr_form_is_block (attr))
23429 error (_("Dwarf Error: DIE at %s referenced in module %s "
23430 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23431 sect_offset_str (sect_off), objfile_name (objfile));
23432
23433 retval.data = DW_BLOCK (attr)->data;
23434 retval.size = DW_BLOCK (attr)->size;
23435 }
23436 retval.per_cu = cu->per_cu;
23437
23438 age_cached_comp_units (dwarf2_per_objfile);
23439
23440 return retval;
23441 }
23442
23443 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23444 offset. */
23445
23446 struct dwarf2_locexpr_baton
23447 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23448 struct dwarf2_per_cu_data *per_cu,
23449 CORE_ADDR (*get_frame_pc) (void *baton),
23450 void *baton)
23451 {
23452 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23453
23454 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23455 }
23456
23457 /* Write a constant of a given type as target-ordered bytes into
23458 OBSTACK. */
23459
23460 static const gdb_byte *
23461 write_constant_as_bytes (struct obstack *obstack,
23462 enum bfd_endian byte_order,
23463 struct type *type,
23464 ULONGEST value,
23465 LONGEST *len)
23466 {
23467 gdb_byte *result;
23468
23469 *len = TYPE_LENGTH (type);
23470 result = (gdb_byte *) obstack_alloc (obstack, *len);
23471 store_unsigned_integer (result, *len, byte_order, value);
23472
23473 return result;
23474 }
23475
23476 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23477 pointer to the constant bytes and set LEN to the length of the
23478 data. If memory is needed, allocate it on OBSTACK. If the DIE
23479 does not have a DW_AT_const_value, return NULL. */
23480
23481 const gdb_byte *
23482 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23483 struct dwarf2_per_cu_data *per_cu,
23484 struct obstack *obstack,
23485 LONGEST *len)
23486 {
23487 struct dwarf2_cu *cu;
23488 struct die_info *die;
23489 struct attribute *attr;
23490 const gdb_byte *result = NULL;
23491 struct type *type;
23492 LONGEST value;
23493 enum bfd_endian byte_order;
23494 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23495
23496 if (per_cu->cu == NULL)
23497 load_cu (per_cu);
23498 cu = per_cu->cu;
23499 if (cu == NULL)
23500 {
23501 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23502 Instead just throw an error, not much else we can do. */
23503 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23504 sect_offset_str (sect_off), objfile_name (objfile));
23505 }
23506
23507 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23508 if (!die)
23509 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23510 sect_offset_str (sect_off), objfile_name (objfile));
23511
23512 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23513 if (attr == NULL)
23514 return NULL;
23515
23516 byte_order = (bfd_big_endian (objfile->obfd)
23517 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23518
23519 switch (attr->form)
23520 {
23521 case DW_FORM_addr:
23522 case DW_FORM_GNU_addr_index:
23523 {
23524 gdb_byte *tem;
23525
23526 *len = cu->header.addr_size;
23527 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23528 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23529 result = tem;
23530 }
23531 break;
23532 case DW_FORM_string:
23533 case DW_FORM_strp:
23534 case DW_FORM_GNU_str_index:
23535 case DW_FORM_GNU_strp_alt:
23536 /* DW_STRING is already allocated on the objfile obstack, point
23537 directly to it. */
23538 result = (const gdb_byte *) DW_STRING (attr);
23539 *len = strlen (DW_STRING (attr));
23540 break;
23541 case DW_FORM_block1:
23542 case DW_FORM_block2:
23543 case DW_FORM_block4:
23544 case DW_FORM_block:
23545 case DW_FORM_exprloc:
23546 case DW_FORM_data16:
23547 result = DW_BLOCK (attr)->data;
23548 *len = DW_BLOCK (attr)->size;
23549 break;
23550
23551 /* The DW_AT_const_value attributes are supposed to carry the
23552 symbol's value "represented as it would be on the target
23553 architecture." By the time we get here, it's already been
23554 converted to host endianness, so we just need to sign- or
23555 zero-extend it as appropriate. */
23556 case DW_FORM_data1:
23557 type = die_type (die, cu);
23558 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23559 if (result == NULL)
23560 result = write_constant_as_bytes (obstack, byte_order,
23561 type, value, len);
23562 break;
23563 case DW_FORM_data2:
23564 type = die_type (die, cu);
23565 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23566 if (result == NULL)
23567 result = write_constant_as_bytes (obstack, byte_order,
23568 type, value, len);
23569 break;
23570 case DW_FORM_data4:
23571 type = die_type (die, cu);
23572 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23573 if (result == NULL)
23574 result = write_constant_as_bytes (obstack, byte_order,
23575 type, value, len);
23576 break;
23577 case DW_FORM_data8:
23578 type = die_type (die, cu);
23579 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23580 if (result == NULL)
23581 result = write_constant_as_bytes (obstack, byte_order,
23582 type, value, len);
23583 break;
23584
23585 case DW_FORM_sdata:
23586 case DW_FORM_implicit_const:
23587 type = die_type (die, cu);
23588 result = write_constant_as_bytes (obstack, byte_order,
23589 type, DW_SND (attr), len);
23590 break;
23591
23592 case DW_FORM_udata:
23593 type = die_type (die, cu);
23594 result = write_constant_as_bytes (obstack, byte_order,
23595 type, DW_UNSND (attr), len);
23596 break;
23597
23598 default:
23599 complaint (&symfile_complaints,
23600 _("unsupported const value attribute form: '%s'"),
23601 dwarf_form_name (attr->form));
23602 break;
23603 }
23604
23605 return result;
23606 }
23607
23608 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23609 valid type for this die is found. */
23610
23611 struct type *
23612 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23613 struct dwarf2_per_cu_data *per_cu)
23614 {
23615 struct dwarf2_cu *cu;
23616 struct die_info *die;
23617
23618 if (per_cu->cu == NULL)
23619 load_cu (per_cu);
23620 cu = per_cu->cu;
23621 if (!cu)
23622 return NULL;
23623
23624 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23625 if (!die)
23626 return NULL;
23627
23628 return die_type (die, cu);
23629 }
23630
23631 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23632 PER_CU. */
23633
23634 struct type *
23635 dwarf2_get_die_type (cu_offset die_offset,
23636 struct dwarf2_per_cu_data *per_cu)
23637 {
23638 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23639 return get_die_type_at_offset (die_offset_sect, per_cu);
23640 }
23641
23642 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23643 On entry *REF_CU is the CU of SRC_DIE.
23644 On exit *REF_CU is the CU of the result.
23645 Returns NULL if the referenced DIE isn't found. */
23646
23647 static struct die_info *
23648 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23649 struct dwarf2_cu **ref_cu)
23650 {
23651 struct die_info temp_die;
23652 struct dwarf2_cu *sig_cu;
23653 struct die_info *die;
23654
23655 /* While it might be nice to assert sig_type->type == NULL here,
23656 we can get here for DW_AT_imported_declaration where we need
23657 the DIE not the type. */
23658
23659 /* If necessary, add it to the queue and load its DIEs. */
23660
23661 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23662 read_signatured_type (sig_type);
23663
23664 sig_cu = sig_type->per_cu.cu;
23665 gdb_assert (sig_cu != NULL);
23666 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23667 temp_die.sect_off = sig_type->type_offset_in_section;
23668 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23669 to_underlying (temp_die.sect_off));
23670 if (die)
23671 {
23672 struct dwarf2_per_objfile *dwarf2_per_objfile
23673 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23674
23675 /* For .gdb_index version 7 keep track of included TUs.
23676 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23677 if (dwarf2_per_objfile->index_table != NULL
23678 && dwarf2_per_objfile->index_table->version <= 7)
23679 {
23680 VEC_safe_push (dwarf2_per_cu_ptr,
23681 (*ref_cu)->per_cu->imported_symtabs,
23682 sig_cu->per_cu);
23683 }
23684
23685 *ref_cu = sig_cu;
23686 return die;
23687 }
23688
23689 return NULL;
23690 }
23691
23692 /* Follow signatured type referenced by ATTR in SRC_DIE.
23693 On entry *REF_CU is the CU of SRC_DIE.
23694 On exit *REF_CU is the CU of the result.
23695 The result is the DIE of the type.
23696 If the referenced type cannot be found an error is thrown. */
23697
23698 static struct die_info *
23699 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23700 struct dwarf2_cu **ref_cu)
23701 {
23702 ULONGEST signature = DW_SIGNATURE (attr);
23703 struct signatured_type *sig_type;
23704 struct die_info *die;
23705
23706 gdb_assert (attr->form == DW_FORM_ref_sig8);
23707
23708 sig_type = lookup_signatured_type (*ref_cu, signature);
23709 /* sig_type will be NULL if the signatured type is missing from
23710 the debug info. */
23711 if (sig_type == NULL)
23712 {
23713 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23714 " from DIE at %s [in module %s]"),
23715 hex_string (signature), sect_offset_str (src_die->sect_off),
23716 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23717 }
23718
23719 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23720 if (die == NULL)
23721 {
23722 dump_die_for_error (src_die);
23723 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23724 " from DIE at %s [in module %s]"),
23725 hex_string (signature), sect_offset_str (src_die->sect_off),
23726 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23727 }
23728
23729 return die;
23730 }
23731
23732 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23733 reading in and processing the type unit if necessary. */
23734
23735 static struct type *
23736 get_signatured_type (struct die_info *die, ULONGEST signature,
23737 struct dwarf2_cu *cu)
23738 {
23739 struct dwarf2_per_objfile *dwarf2_per_objfile
23740 = cu->per_cu->dwarf2_per_objfile;
23741 struct signatured_type *sig_type;
23742 struct dwarf2_cu *type_cu;
23743 struct die_info *type_die;
23744 struct type *type;
23745
23746 sig_type = lookup_signatured_type (cu, signature);
23747 /* sig_type will be NULL if the signatured type is missing from
23748 the debug info. */
23749 if (sig_type == NULL)
23750 {
23751 complaint (&symfile_complaints,
23752 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23753 " from DIE at %s [in module %s]"),
23754 hex_string (signature), sect_offset_str (die->sect_off),
23755 objfile_name (dwarf2_per_objfile->objfile));
23756 return build_error_marker_type (cu, die);
23757 }
23758
23759 /* If we already know the type we're done. */
23760 if (sig_type->type != NULL)
23761 return sig_type->type;
23762
23763 type_cu = cu;
23764 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23765 if (type_die != NULL)
23766 {
23767 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23768 is created. This is important, for example, because for c++ classes
23769 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23770 type = read_type_die (type_die, type_cu);
23771 if (type == NULL)
23772 {
23773 complaint (&symfile_complaints,
23774 _("Dwarf Error: Cannot build signatured type %s"
23775 " referenced from DIE at %s [in module %s]"),
23776 hex_string (signature), sect_offset_str (die->sect_off),
23777 objfile_name (dwarf2_per_objfile->objfile));
23778 type = build_error_marker_type (cu, die);
23779 }
23780 }
23781 else
23782 {
23783 complaint (&symfile_complaints,
23784 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23785 " from DIE at %s [in module %s]"),
23786 hex_string (signature), sect_offset_str (die->sect_off),
23787 objfile_name (dwarf2_per_objfile->objfile));
23788 type = build_error_marker_type (cu, die);
23789 }
23790 sig_type->type = type;
23791
23792 return type;
23793 }
23794
23795 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23796 reading in and processing the type unit if necessary. */
23797
23798 static struct type *
23799 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23800 struct dwarf2_cu *cu) /* ARI: editCase function */
23801 {
23802 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23803 if (attr_form_is_ref (attr))
23804 {
23805 struct dwarf2_cu *type_cu = cu;
23806 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23807
23808 return read_type_die (type_die, type_cu);
23809 }
23810 else if (attr->form == DW_FORM_ref_sig8)
23811 {
23812 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23813 }
23814 else
23815 {
23816 struct dwarf2_per_objfile *dwarf2_per_objfile
23817 = cu->per_cu->dwarf2_per_objfile;
23818
23819 complaint (&symfile_complaints,
23820 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23821 " at %s [in module %s]"),
23822 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23823 objfile_name (dwarf2_per_objfile->objfile));
23824 return build_error_marker_type (cu, die);
23825 }
23826 }
23827
23828 /* Load the DIEs associated with type unit PER_CU into memory. */
23829
23830 static void
23831 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23832 {
23833 struct signatured_type *sig_type;
23834
23835 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23836 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23837
23838 /* We have the per_cu, but we need the signatured_type.
23839 Fortunately this is an easy translation. */
23840 gdb_assert (per_cu->is_debug_types);
23841 sig_type = (struct signatured_type *) per_cu;
23842
23843 gdb_assert (per_cu->cu == NULL);
23844
23845 read_signatured_type (sig_type);
23846
23847 gdb_assert (per_cu->cu != NULL);
23848 }
23849
23850 /* die_reader_func for read_signatured_type.
23851 This is identical to load_full_comp_unit_reader,
23852 but is kept separate for now. */
23853
23854 static void
23855 read_signatured_type_reader (const struct die_reader_specs *reader,
23856 const gdb_byte *info_ptr,
23857 struct die_info *comp_unit_die,
23858 int has_children,
23859 void *data)
23860 {
23861 struct dwarf2_cu *cu = reader->cu;
23862
23863 gdb_assert (cu->die_hash == NULL);
23864 cu->die_hash =
23865 htab_create_alloc_ex (cu->header.length / 12,
23866 die_hash,
23867 die_eq,
23868 NULL,
23869 &cu->comp_unit_obstack,
23870 hashtab_obstack_allocate,
23871 dummy_obstack_deallocate);
23872
23873 if (has_children)
23874 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23875 &info_ptr, comp_unit_die);
23876 cu->dies = comp_unit_die;
23877 /* comp_unit_die is not stored in die_hash, no need. */
23878
23879 /* We try not to read any attributes in this function, because not
23880 all CUs needed for references have been loaded yet, and symbol
23881 table processing isn't initialized. But we have to set the CU language,
23882 or we won't be able to build types correctly.
23883 Similarly, if we do not read the producer, we can not apply
23884 producer-specific interpretation. */
23885 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23886 }
23887
23888 /* Read in a signatured type and build its CU and DIEs.
23889 If the type is a stub for the real type in a DWO file,
23890 read in the real type from the DWO file as well. */
23891
23892 static void
23893 read_signatured_type (struct signatured_type *sig_type)
23894 {
23895 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23896
23897 gdb_assert (per_cu->is_debug_types);
23898 gdb_assert (per_cu->cu == NULL);
23899
23900 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23901 read_signatured_type_reader, NULL);
23902 sig_type->per_cu.tu_read = 1;
23903 }
23904
23905 /* Decode simple location descriptions.
23906 Given a pointer to a dwarf block that defines a location, compute
23907 the location and return the value.
23908
23909 NOTE drow/2003-11-18: This function is called in two situations
23910 now: for the address of static or global variables (partial symbols
23911 only) and for offsets into structures which are expected to be
23912 (more or less) constant. The partial symbol case should go away,
23913 and only the constant case should remain. That will let this
23914 function complain more accurately. A few special modes are allowed
23915 without complaint for global variables (for instance, global
23916 register values and thread-local values).
23917
23918 A location description containing no operations indicates that the
23919 object is optimized out. The return value is 0 for that case.
23920 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23921 callers will only want a very basic result and this can become a
23922 complaint.
23923
23924 Note that stack[0] is unused except as a default error return. */
23925
23926 static CORE_ADDR
23927 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23928 {
23929 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23930 size_t i;
23931 size_t size = blk->size;
23932 const gdb_byte *data = blk->data;
23933 CORE_ADDR stack[64];
23934 int stacki;
23935 unsigned int bytes_read, unsnd;
23936 gdb_byte op;
23937
23938 i = 0;
23939 stacki = 0;
23940 stack[stacki] = 0;
23941 stack[++stacki] = 0;
23942
23943 while (i < size)
23944 {
23945 op = data[i++];
23946 switch (op)
23947 {
23948 case DW_OP_lit0:
23949 case DW_OP_lit1:
23950 case DW_OP_lit2:
23951 case DW_OP_lit3:
23952 case DW_OP_lit4:
23953 case DW_OP_lit5:
23954 case DW_OP_lit6:
23955 case DW_OP_lit7:
23956 case DW_OP_lit8:
23957 case DW_OP_lit9:
23958 case DW_OP_lit10:
23959 case DW_OP_lit11:
23960 case DW_OP_lit12:
23961 case DW_OP_lit13:
23962 case DW_OP_lit14:
23963 case DW_OP_lit15:
23964 case DW_OP_lit16:
23965 case DW_OP_lit17:
23966 case DW_OP_lit18:
23967 case DW_OP_lit19:
23968 case DW_OP_lit20:
23969 case DW_OP_lit21:
23970 case DW_OP_lit22:
23971 case DW_OP_lit23:
23972 case DW_OP_lit24:
23973 case DW_OP_lit25:
23974 case DW_OP_lit26:
23975 case DW_OP_lit27:
23976 case DW_OP_lit28:
23977 case DW_OP_lit29:
23978 case DW_OP_lit30:
23979 case DW_OP_lit31:
23980 stack[++stacki] = op - DW_OP_lit0;
23981 break;
23982
23983 case DW_OP_reg0:
23984 case DW_OP_reg1:
23985 case DW_OP_reg2:
23986 case DW_OP_reg3:
23987 case DW_OP_reg4:
23988 case DW_OP_reg5:
23989 case DW_OP_reg6:
23990 case DW_OP_reg7:
23991 case DW_OP_reg8:
23992 case DW_OP_reg9:
23993 case DW_OP_reg10:
23994 case DW_OP_reg11:
23995 case DW_OP_reg12:
23996 case DW_OP_reg13:
23997 case DW_OP_reg14:
23998 case DW_OP_reg15:
23999 case DW_OP_reg16:
24000 case DW_OP_reg17:
24001 case DW_OP_reg18:
24002 case DW_OP_reg19:
24003 case DW_OP_reg20:
24004 case DW_OP_reg21:
24005 case DW_OP_reg22:
24006 case DW_OP_reg23:
24007 case DW_OP_reg24:
24008 case DW_OP_reg25:
24009 case DW_OP_reg26:
24010 case DW_OP_reg27:
24011 case DW_OP_reg28:
24012 case DW_OP_reg29:
24013 case DW_OP_reg30:
24014 case DW_OP_reg31:
24015 stack[++stacki] = op - DW_OP_reg0;
24016 if (i < size)
24017 dwarf2_complex_location_expr_complaint ();
24018 break;
24019
24020 case DW_OP_regx:
24021 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24022 i += bytes_read;
24023 stack[++stacki] = unsnd;
24024 if (i < size)
24025 dwarf2_complex_location_expr_complaint ();
24026 break;
24027
24028 case DW_OP_addr:
24029 stack[++stacki] = read_address (objfile->obfd, &data[i],
24030 cu, &bytes_read);
24031 i += bytes_read;
24032 break;
24033
24034 case DW_OP_const1u:
24035 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24036 i += 1;
24037 break;
24038
24039 case DW_OP_const1s:
24040 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24041 i += 1;
24042 break;
24043
24044 case DW_OP_const2u:
24045 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24046 i += 2;
24047 break;
24048
24049 case DW_OP_const2s:
24050 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24051 i += 2;
24052 break;
24053
24054 case DW_OP_const4u:
24055 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24056 i += 4;
24057 break;
24058
24059 case DW_OP_const4s:
24060 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24061 i += 4;
24062 break;
24063
24064 case DW_OP_const8u:
24065 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24066 i += 8;
24067 break;
24068
24069 case DW_OP_constu:
24070 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24071 &bytes_read);
24072 i += bytes_read;
24073 break;
24074
24075 case DW_OP_consts:
24076 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24077 i += bytes_read;
24078 break;
24079
24080 case DW_OP_dup:
24081 stack[stacki + 1] = stack[stacki];
24082 stacki++;
24083 break;
24084
24085 case DW_OP_plus:
24086 stack[stacki - 1] += stack[stacki];
24087 stacki--;
24088 break;
24089
24090 case DW_OP_plus_uconst:
24091 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24092 &bytes_read);
24093 i += bytes_read;
24094 break;
24095
24096 case DW_OP_minus:
24097 stack[stacki - 1] -= stack[stacki];
24098 stacki--;
24099 break;
24100
24101 case DW_OP_deref:
24102 /* If we're not the last op, then we definitely can't encode
24103 this using GDB's address_class enum. This is valid for partial
24104 global symbols, although the variable's address will be bogus
24105 in the psymtab. */
24106 if (i < size)
24107 dwarf2_complex_location_expr_complaint ();
24108 break;
24109
24110 case DW_OP_GNU_push_tls_address:
24111 case DW_OP_form_tls_address:
24112 /* The top of the stack has the offset from the beginning
24113 of the thread control block at which the variable is located. */
24114 /* Nothing should follow this operator, so the top of stack would
24115 be returned. */
24116 /* This is valid for partial global symbols, but the variable's
24117 address will be bogus in the psymtab. Make it always at least
24118 non-zero to not look as a variable garbage collected by linker
24119 which have DW_OP_addr 0. */
24120 if (i < size)
24121 dwarf2_complex_location_expr_complaint ();
24122 stack[stacki]++;
24123 break;
24124
24125 case DW_OP_GNU_uninit:
24126 break;
24127
24128 case DW_OP_GNU_addr_index:
24129 case DW_OP_GNU_const_index:
24130 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24131 &bytes_read);
24132 i += bytes_read;
24133 break;
24134
24135 default:
24136 {
24137 const char *name = get_DW_OP_name (op);
24138
24139 if (name)
24140 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
24141 name);
24142 else
24143 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
24144 op);
24145 }
24146
24147 return (stack[stacki]);
24148 }
24149
24150 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24151 outside of the allocated space. Also enforce minimum>0. */
24152 if (stacki >= ARRAY_SIZE (stack) - 1)
24153 {
24154 complaint (&symfile_complaints,
24155 _("location description stack overflow"));
24156 return 0;
24157 }
24158
24159 if (stacki <= 0)
24160 {
24161 complaint (&symfile_complaints,
24162 _("location description stack underflow"));
24163 return 0;
24164 }
24165 }
24166 return (stack[stacki]);
24167 }
24168
24169 /* memory allocation interface */
24170
24171 static struct dwarf_block *
24172 dwarf_alloc_block (struct dwarf2_cu *cu)
24173 {
24174 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24175 }
24176
24177 static struct die_info *
24178 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24179 {
24180 struct die_info *die;
24181 size_t size = sizeof (struct die_info);
24182
24183 if (num_attrs > 1)
24184 size += (num_attrs - 1) * sizeof (struct attribute);
24185
24186 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24187 memset (die, 0, sizeof (struct die_info));
24188 return (die);
24189 }
24190
24191 \f
24192 /* Macro support. */
24193
24194 /* Return file name relative to the compilation directory of file number I in
24195 *LH's file name table. The result is allocated using xmalloc; the caller is
24196 responsible for freeing it. */
24197
24198 static char *
24199 file_file_name (int file, struct line_header *lh)
24200 {
24201 /* Is the file number a valid index into the line header's file name
24202 table? Remember that file numbers start with one, not zero. */
24203 if (1 <= file && file <= lh->file_names.size ())
24204 {
24205 const file_entry &fe = lh->file_names[file - 1];
24206
24207 if (!IS_ABSOLUTE_PATH (fe.name))
24208 {
24209 const char *dir = fe.include_dir (lh);
24210 if (dir != NULL)
24211 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
24212 }
24213 return xstrdup (fe.name);
24214 }
24215 else
24216 {
24217 /* The compiler produced a bogus file number. We can at least
24218 record the macro definitions made in the file, even if we
24219 won't be able to find the file by name. */
24220 char fake_name[80];
24221
24222 xsnprintf (fake_name, sizeof (fake_name),
24223 "<bad macro file number %d>", file);
24224
24225 complaint (&symfile_complaints,
24226 _("bad file number in macro information (%d)"),
24227 file);
24228
24229 return xstrdup (fake_name);
24230 }
24231 }
24232
24233 /* Return the full name of file number I in *LH's file name table.
24234 Use COMP_DIR as the name of the current directory of the
24235 compilation. The result is allocated using xmalloc; the caller is
24236 responsible for freeing it. */
24237 static char *
24238 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24239 {
24240 /* Is the file number a valid index into the line header's file name
24241 table? Remember that file numbers start with one, not zero. */
24242 if (1 <= file && file <= lh->file_names.size ())
24243 {
24244 char *relative = file_file_name (file, lh);
24245
24246 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24247 return relative;
24248 return reconcat (relative, comp_dir, SLASH_STRING,
24249 relative, (char *) NULL);
24250 }
24251 else
24252 return file_file_name (file, lh);
24253 }
24254
24255
24256 static struct macro_source_file *
24257 macro_start_file (int file, int line,
24258 struct macro_source_file *current_file,
24259 struct line_header *lh)
24260 {
24261 /* File name relative to the compilation directory of this source file. */
24262 char *file_name = file_file_name (file, lh);
24263
24264 if (! current_file)
24265 {
24266 /* Note: We don't create a macro table for this compilation unit
24267 at all until we actually get a filename. */
24268 struct macro_table *macro_table = get_macro_table ();
24269
24270 /* If we have no current file, then this must be the start_file
24271 directive for the compilation unit's main source file. */
24272 current_file = macro_set_main (macro_table, file_name);
24273 macro_define_special (macro_table);
24274 }
24275 else
24276 current_file = macro_include (current_file, line, file_name);
24277
24278 xfree (file_name);
24279
24280 return current_file;
24281 }
24282
24283 static const char *
24284 consume_improper_spaces (const char *p, const char *body)
24285 {
24286 if (*p == ' ')
24287 {
24288 complaint (&symfile_complaints,
24289 _("macro definition contains spaces "
24290 "in formal argument list:\n`%s'"),
24291 body);
24292
24293 while (*p == ' ')
24294 p++;
24295 }
24296
24297 return p;
24298 }
24299
24300
24301 static void
24302 parse_macro_definition (struct macro_source_file *file, int line,
24303 const char *body)
24304 {
24305 const char *p;
24306
24307 /* The body string takes one of two forms. For object-like macro
24308 definitions, it should be:
24309
24310 <macro name> " " <definition>
24311
24312 For function-like macro definitions, it should be:
24313
24314 <macro name> "() " <definition>
24315 or
24316 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24317
24318 Spaces may appear only where explicitly indicated, and in the
24319 <definition>.
24320
24321 The Dwarf 2 spec says that an object-like macro's name is always
24322 followed by a space, but versions of GCC around March 2002 omit
24323 the space when the macro's definition is the empty string.
24324
24325 The Dwarf 2 spec says that there should be no spaces between the
24326 formal arguments in a function-like macro's formal argument list,
24327 but versions of GCC around March 2002 include spaces after the
24328 commas. */
24329
24330
24331 /* Find the extent of the macro name. The macro name is terminated
24332 by either a space or null character (for an object-like macro) or
24333 an opening paren (for a function-like macro). */
24334 for (p = body; *p; p++)
24335 if (*p == ' ' || *p == '(')
24336 break;
24337
24338 if (*p == ' ' || *p == '\0')
24339 {
24340 /* It's an object-like macro. */
24341 int name_len = p - body;
24342 char *name = savestring (body, name_len);
24343 const char *replacement;
24344
24345 if (*p == ' ')
24346 replacement = body + name_len + 1;
24347 else
24348 {
24349 dwarf2_macro_malformed_definition_complaint (body);
24350 replacement = body + name_len;
24351 }
24352
24353 macro_define_object (file, line, name, replacement);
24354
24355 xfree (name);
24356 }
24357 else if (*p == '(')
24358 {
24359 /* It's a function-like macro. */
24360 char *name = savestring (body, p - body);
24361 int argc = 0;
24362 int argv_size = 1;
24363 char **argv = XNEWVEC (char *, argv_size);
24364
24365 p++;
24366
24367 p = consume_improper_spaces (p, body);
24368
24369 /* Parse the formal argument list. */
24370 while (*p && *p != ')')
24371 {
24372 /* Find the extent of the current argument name. */
24373 const char *arg_start = p;
24374
24375 while (*p && *p != ',' && *p != ')' && *p != ' ')
24376 p++;
24377
24378 if (! *p || p == arg_start)
24379 dwarf2_macro_malformed_definition_complaint (body);
24380 else
24381 {
24382 /* Make sure argv has room for the new argument. */
24383 if (argc >= argv_size)
24384 {
24385 argv_size *= 2;
24386 argv = XRESIZEVEC (char *, argv, argv_size);
24387 }
24388
24389 argv[argc++] = savestring (arg_start, p - arg_start);
24390 }
24391
24392 p = consume_improper_spaces (p, body);
24393
24394 /* Consume the comma, if present. */
24395 if (*p == ',')
24396 {
24397 p++;
24398
24399 p = consume_improper_spaces (p, body);
24400 }
24401 }
24402
24403 if (*p == ')')
24404 {
24405 p++;
24406
24407 if (*p == ' ')
24408 /* Perfectly formed definition, no complaints. */
24409 macro_define_function (file, line, name,
24410 argc, (const char **) argv,
24411 p + 1);
24412 else if (*p == '\0')
24413 {
24414 /* Complain, but do define it. */
24415 dwarf2_macro_malformed_definition_complaint (body);
24416 macro_define_function (file, line, name,
24417 argc, (const char **) argv,
24418 p);
24419 }
24420 else
24421 /* Just complain. */
24422 dwarf2_macro_malformed_definition_complaint (body);
24423 }
24424 else
24425 /* Just complain. */
24426 dwarf2_macro_malformed_definition_complaint (body);
24427
24428 xfree (name);
24429 {
24430 int i;
24431
24432 for (i = 0; i < argc; i++)
24433 xfree (argv[i]);
24434 }
24435 xfree (argv);
24436 }
24437 else
24438 dwarf2_macro_malformed_definition_complaint (body);
24439 }
24440
24441 /* Skip some bytes from BYTES according to the form given in FORM.
24442 Returns the new pointer. */
24443
24444 static const gdb_byte *
24445 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24446 enum dwarf_form form,
24447 unsigned int offset_size,
24448 struct dwarf2_section_info *section)
24449 {
24450 unsigned int bytes_read;
24451
24452 switch (form)
24453 {
24454 case DW_FORM_data1:
24455 case DW_FORM_flag:
24456 ++bytes;
24457 break;
24458
24459 case DW_FORM_data2:
24460 bytes += 2;
24461 break;
24462
24463 case DW_FORM_data4:
24464 bytes += 4;
24465 break;
24466
24467 case DW_FORM_data8:
24468 bytes += 8;
24469 break;
24470
24471 case DW_FORM_data16:
24472 bytes += 16;
24473 break;
24474
24475 case DW_FORM_string:
24476 read_direct_string (abfd, bytes, &bytes_read);
24477 bytes += bytes_read;
24478 break;
24479
24480 case DW_FORM_sec_offset:
24481 case DW_FORM_strp:
24482 case DW_FORM_GNU_strp_alt:
24483 bytes += offset_size;
24484 break;
24485
24486 case DW_FORM_block:
24487 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24488 bytes += bytes_read;
24489 break;
24490
24491 case DW_FORM_block1:
24492 bytes += 1 + read_1_byte (abfd, bytes);
24493 break;
24494 case DW_FORM_block2:
24495 bytes += 2 + read_2_bytes (abfd, bytes);
24496 break;
24497 case DW_FORM_block4:
24498 bytes += 4 + read_4_bytes (abfd, bytes);
24499 break;
24500
24501 case DW_FORM_sdata:
24502 case DW_FORM_udata:
24503 case DW_FORM_GNU_addr_index:
24504 case DW_FORM_GNU_str_index:
24505 bytes = gdb_skip_leb128 (bytes, buffer_end);
24506 if (bytes == NULL)
24507 {
24508 dwarf2_section_buffer_overflow_complaint (section);
24509 return NULL;
24510 }
24511 break;
24512
24513 case DW_FORM_implicit_const:
24514 break;
24515
24516 default:
24517 {
24518 complaint (&symfile_complaints,
24519 _("invalid form 0x%x in `%s'"),
24520 form, get_section_name (section));
24521 return NULL;
24522 }
24523 }
24524
24525 return bytes;
24526 }
24527
24528 /* A helper for dwarf_decode_macros that handles skipping an unknown
24529 opcode. Returns an updated pointer to the macro data buffer; or,
24530 on error, issues a complaint and returns NULL. */
24531
24532 static const gdb_byte *
24533 skip_unknown_opcode (unsigned int opcode,
24534 const gdb_byte **opcode_definitions,
24535 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24536 bfd *abfd,
24537 unsigned int offset_size,
24538 struct dwarf2_section_info *section)
24539 {
24540 unsigned int bytes_read, i;
24541 unsigned long arg;
24542 const gdb_byte *defn;
24543
24544 if (opcode_definitions[opcode] == NULL)
24545 {
24546 complaint (&symfile_complaints,
24547 _("unrecognized DW_MACFINO opcode 0x%x"),
24548 opcode);
24549 return NULL;
24550 }
24551
24552 defn = opcode_definitions[opcode];
24553 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24554 defn += bytes_read;
24555
24556 for (i = 0; i < arg; ++i)
24557 {
24558 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24559 (enum dwarf_form) defn[i], offset_size,
24560 section);
24561 if (mac_ptr == NULL)
24562 {
24563 /* skip_form_bytes already issued the complaint. */
24564 return NULL;
24565 }
24566 }
24567
24568 return mac_ptr;
24569 }
24570
24571 /* A helper function which parses the header of a macro section.
24572 If the macro section is the extended (for now called "GNU") type,
24573 then this updates *OFFSET_SIZE. Returns a pointer to just after
24574 the header, or issues a complaint and returns NULL on error. */
24575
24576 static const gdb_byte *
24577 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24578 bfd *abfd,
24579 const gdb_byte *mac_ptr,
24580 unsigned int *offset_size,
24581 int section_is_gnu)
24582 {
24583 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24584
24585 if (section_is_gnu)
24586 {
24587 unsigned int version, flags;
24588
24589 version = read_2_bytes (abfd, mac_ptr);
24590 if (version != 4 && version != 5)
24591 {
24592 complaint (&symfile_complaints,
24593 _("unrecognized version `%d' in .debug_macro section"),
24594 version);
24595 return NULL;
24596 }
24597 mac_ptr += 2;
24598
24599 flags = read_1_byte (abfd, mac_ptr);
24600 ++mac_ptr;
24601 *offset_size = (flags & 1) ? 8 : 4;
24602
24603 if ((flags & 2) != 0)
24604 /* We don't need the line table offset. */
24605 mac_ptr += *offset_size;
24606
24607 /* Vendor opcode descriptions. */
24608 if ((flags & 4) != 0)
24609 {
24610 unsigned int i, count;
24611
24612 count = read_1_byte (abfd, mac_ptr);
24613 ++mac_ptr;
24614 for (i = 0; i < count; ++i)
24615 {
24616 unsigned int opcode, bytes_read;
24617 unsigned long arg;
24618
24619 opcode = read_1_byte (abfd, mac_ptr);
24620 ++mac_ptr;
24621 opcode_definitions[opcode] = mac_ptr;
24622 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24623 mac_ptr += bytes_read;
24624 mac_ptr += arg;
24625 }
24626 }
24627 }
24628
24629 return mac_ptr;
24630 }
24631
24632 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24633 including DW_MACRO_import. */
24634
24635 static void
24636 dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24637 bfd *abfd,
24638 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24639 struct macro_source_file *current_file,
24640 struct line_header *lh,
24641 struct dwarf2_section_info *section,
24642 int section_is_gnu, int section_is_dwz,
24643 unsigned int offset_size,
24644 htab_t include_hash)
24645 {
24646 struct objfile *objfile = dwarf2_per_objfile->objfile;
24647 enum dwarf_macro_record_type macinfo_type;
24648 int at_commandline;
24649 const gdb_byte *opcode_definitions[256];
24650
24651 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24652 &offset_size, section_is_gnu);
24653 if (mac_ptr == NULL)
24654 {
24655 /* We already issued a complaint. */
24656 return;
24657 }
24658
24659 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24660 GDB is still reading the definitions from command line. First
24661 DW_MACINFO_start_file will need to be ignored as it was already executed
24662 to create CURRENT_FILE for the main source holding also the command line
24663 definitions. On first met DW_MACINFO_start_file this flag is reset to
24664 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24665
24666 at_commandline = 1;
24667
24668 do
24669 {
24670 /* Do we at least have room for a macinfo type byte? */
24671 if (mac_ptr >= mac_end)
24672 {
24673 dwarf2_section_buffer_overflow_complaint (section);
24674 break;
24675 }
24676
24677 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24678 mac_ptr++;
24679
24680 /* Note that we rely on the fact that the corresponding GNU and
24681 DWARF constants are the same. */
24682 DIAGNOSTIC_PUSH
24683 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24684 switch (macinfo_type)
24685 {
24686 /* A zero macinfo type indicates the end of the macro
24687 information. */
24688 case 0:
24689 break;
24690
24691 case DW_MACRO_define:
24692 case DW_MACRO_undef:
24693 case DW_MACRO_define_strp:
24694 case DW_MACRO_undef_strp:
24695 case DW_MACRO_define_sup:
24696 case DW_MACRO_undef_sup:
24697 {
24698 unsigned int bytes_read;
24699 int line;
24700 const char *body;
24701 int is_define;
24702
24703 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24704 mac_ptr += bytes_read;
24705
24706 if (macinfo_type == DW_MACRO_define
24707 || macinfo_type == DW_MACRO_undef)
24708 {
24709 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24710 mac_ptr += bytes_read;
24711 }
24712 else
24713 {
24714 LONGEST str_offset;
24715
24716 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24717 mac_ptr += offset_size;
24718
24719 if (macinfo_type == DW_MACRO_define_sup
24720 || macinfo_type == DW_MACRO_undef_sup
24721 || section_is_dwz)
24722 {
24723 struct dwz_file *dwz
24724 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24725
24726 body = read_indirect_string_from_dwz (objfile,
24727 dwz, str_offset);
24728 }
24729 else
24730 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24731 abfd, str_offset);
24732 }
24733
24734 is_define = (macinfo_type == DW_MACRO_define
24735 || macinfo_type == DW_MACRO_define_strp
24736 || macinfo_type == DW_MACRO_define_sup);
24737 if (! current_file)
24738 {
24739 /* DWARF violation as no main source is present. */
24740 complaint (&symfile_complaints,
24741 _("debug info with no main source gives macro %s "
24742 "on line %d: %s"),
24743 is_define ? _("definition") : _("undefinition"),
24744 line, body);
24745 break;
24746 }
24747 if ((line == 0 && !at_commandline)
24748 || (line != 0 && at_commandline))
24749 complaint (&symfile_complaints,
24750 _("debug info gives %s macro %s with %s line %d: %s"),
24751 at_commandline ? _("command-line") : _("in-file"),
24752 is_define ? _("definition") : _("undefinition"),
24753 line == 0 ? _("zero") : _("non-zero"), line, body);
24754
24755 if (is_define)
24756 parse_macro_definition (current_file, line, body);
24757 else
24758 {
24759 gdb_assert (macinfo_type == DW_MACRO_undef
24760 || macinfo_type == DW_MACRO_undef_strp
24761 || macinfo_type == DW_MACRO_undef_sup);
24762 macro_undef (current_file, line, body);
24763 }
24764 }
24765 break;
24766
24767 case DW_MACRO_start_file:
24768 {
24769 unsigned int bytes_read;
24770 int line, file;
24771
24772 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24773 mac_ptr += bytes_read;
24774 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24775 mac_ptr += bytes_read;
24776
24777 if ((line == 0 && !at_commandline)
24778 || (line != 0 && at_commandline))
24779 complaint (&symfile_complaints,
24780 _("debug info gives source %d included "
24781 "from %s at %s line %d"),
24782 file, at_commandline ? _("command-line") : _("file"),
24783 line == 0 ? _("zero") : _("non-zero"), line);
24784
24785 if (at_commandline)
24786 {
24787 /* This DW_MACRO_start_file was executed in the
24788 pass one. */
24789 at_commandline = 0;
24790 }
24791 else
24792 current_file = macro_start_file (file, line, current_file, lh);
24793 }
24794 break;
24795
24796 case DW_MACRO_end_file:
24797 if (! current_file)
24798 complaint (&symfile_complaints,
24799 _("macro debug info has an unmatched "
24800 "`close_file' directive"));
24801 else
24802 {
24803 current_file = current_file->included_by;
24804 if (! current_file)
24805 {
24806 enum dwarf_macro_record_type next_type;
24807
24808 /* GCC circa March 2002 doesn't produce the zero
24809 type byte marking the end of the compilation
24810 unit. Complain if it's not there, but exit no
24811 matter what. */
24812
24813 /* Do we at least have room for a macinfo type byte? */
24814 if (mac_ptr >= mac_end)
24815 {
24816 dwarf2_section_buffer_overflow_complaint (section);
24817 return;
24818 }
24819
24820 /* We don't increment mac_ptr here, so this is just
24821 a look-ahead. */
24822 next_type
24823 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24824 mac_ptr);
24825 if (next_type != 0)
24826 complaint (&symfile_complaints,
24827 _("no terminating 0-type entry for "
24828 "macros in `.debug_macinfo' section"));
24829
24830 return;
24831 }
24832 }
24833 break;
24834
24835 case DW_MACRO_import:
24836 case DW_MACRO_import_sup:
24837 {
24838 LONGEST offset;
24839 void **slot;
24840 bfd *include_bfd = abfd;
24841 struct dwarf2_section_info *include_section = section;
24842 const gdb_byte *include_mac_end = mac_end;
24843 int is_dwz = section_is_dwz;
24844 const gdb_byte *new_mac_ptr;
24845
24846 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24847 mac_ptr += offset_size;
24848
24849 if (macinfo_type == DW_MACRO_import_sup)
24850 {
24851 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24852
24853 dwarf2_read_section (objfile, &dwz->macro);
24854
24855 include_section = &dwz->macro;
24856 include_bfd = get_section_bfd_owner (include_section);
24857 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24858 is_dwz = 1;
24859 }
24860
24861 new_mac_ptr = include_section->buffer + offset;
24862 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24863
24864 if (*slot != NULL)
24865 {
24866 /* This has actually happened; see
24867 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24868 complaint (&symfile_complaints,
24869 _("recursive DW_MACRO_import in "
24870 ".debug_macro section"));
24871 }
24872 else
24873 {
24874 *slot = (void *) new_mac_ptr;
24875
24876 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24877 include_bfd, new_mac_ptr,
24878 include_mac_end, current_file, lh,
24879 section, section_is_gnu, is_dwz,
24880 offset_size, include_hash);
24881
24882 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24883 }
24884 }
24885 break;
24886
24887 case DW_MACINFO_vendor_ext:
24888 if (!section_is_gnu)
24889 {
24890 unsigned int bytes_read;
24891
24892 /* This reads the constant, but since we don't recognize
24893 any vendor extensions, we ignore it. */
24894 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24895 mac_ptr += bytes_read;
24896 read_direct_string (abfd, mac_ptr, &bytes_read);
24897 mac_ptr += bytes_read;
24898
24899 /* We don't recognize any vendor extensions. */
24900 break;
24901 }
24902 /* FALLTHROUGH */
24903
24904 default:
24905 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24906 mac_ptr, mac_end, abfd, offset_size,
24907 section);
24908 if (mac_ptr == NULL)
24909 return;
24910 break;
24911 }
24912 DIAGNOSTIC_POP
24913 } while (macinfo_type != 0);
24914 }
24915
24916 static void
24917 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24918 int section_is_gnu)
24919 {
24920 struct dwarf2_per_objfile *dwarf2_per_objfile
24921 = cu->per_cu->dwarf2_per_objfile;
24922 struct objfile *objfile = dwarf2_per_objfile->objfile;
24923 struct line_header *lh = cu->line_header;
24924 bfd *abfd;
24925 const gdb_byte *mac_ptr, *mac_end;
24926 struct macro_source_file *current_file = 0;
24927 enum dwarf_macro_record_type macinfo_type;
24928 unsigned int offset_size = cu->header.offset_size;
24929 const gdb_byte *opcode_definitions[256];
24930 void **slot;
24931 struct dwarf2_section_info *section;
24932 const char *section_name;
24933
24934 if (cu->dwo_unit != NULL)
24935 {
24936 if (section_is_gnu)
24937 {
24938 section = &cu->dwo_unit->dwo_file->sections.macro;
24939 section_name = ".debug_macro.dwo";
24940 }
24941 else
24942 {
24943 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24944 section_name = ".debug_macinfo.dwo";
24945 }
24946 }
24947 else
24948 {
24949 if (section_is_gnu)
24950 {
24951 section = &dwarf2_per_objfile->macro;
24952 section_name = ".debug_macro";
24953 }
24954 else
24955 {
24956 section = &dwarf2_per_objfile->macinfo;
24957 section_name = ".debug_macinfo";
24958 }
24959 }
24960
24961 dwarf2_read_section (objfile, section);
24962 if (section->buffer == NULL)
24963 {
24964 complaint (&symfile_complaints, _("missing %s section"), section_name);
24965 return;
24966 }
24967 abfd = get_section_bfd_owner (section);
24968
24969 /* First pass: Find the name of the base filename.
24970 This filename is needed in order to process all macros whose definition
24971 (or undefinition) comes from the command line. These macros are defined
24972 before the first DW_MACINFO_start_file entry, and yet still need to be
24973 associated to the base file.
24974
24975 To determine the base file name, we scan the macro definitions until we
24976 reach the first DW_MACINFO_start_file entry. We then initialize
24977 CURRENT_FILE accordingly so that any macro definition found before the
24978 first DW_MACINFO_start_file can still be associated to the base file. */
24979
24980 mac_ptr = section->buffer + offset;
24981 mac_end = section->buffer + section->size;
24982
24983 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24984 &offset_size, section_is_gnu);
24985 if (mac_ptr == NULL)
24986 {
24987 /* We already issued a complaint. */
24988 return;
24989 }
24990
24991 do
24992 {
24993 /* Do we at least have room for a macinfo type byte? */
24994 if (mac_ptr >= mac_end)
24995 {
24996 /* Complaint is printed during the second pass as GDB will probably
24997 stop the first pass earlier upon finding
24998 DW_MACINFO_start_file. */
24999 break;
25000 }
25001
25002 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
25003 mac_ptr++;
25004
25005 /* Note that we rely on the fact that the corresponding GNU and
25006 DWARF constants are the same. */
25007 DIAGNOSTIC_PUSH
25008 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
25009 switch (macinfo_type)
25010 {
25011 /* A zero macinfo type indicates the end of the macro
25012 information. */
25013 case 0:
25014 break;
25015
25016 case DW_MACRO_define:
25017 case DW_MACRO_undef:
25018 /* Only skip the data by MAC_PTR. */
25019 {
25020 unsigned int bytes_read;
25021
25022 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25023 mac_ptr += bytes_read;
25024 read_direct_string (abfd, mac_ptr, &bytes_read);
25025 mac_ptr += bytes_read;
25026 }
25027 break;
25028
25029 case DW_MACRO_start_file:
25030 {
25031 unsigned int bytes_read;
25032 int line, file;
25033
25034 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25035 mac_ptr += bytes_read;
25036 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25037 mac_ptr += bytes_read;
25038
25039 current_file = macro_start_file (file, line, current_file, lh);
25040 }
25041 break;
25042
25043 case DW_MACRO_end_file:
25044 /* No data to skip by MAC_PTR. */
25045 break;
25046
25047 case DW_MACRO_define_strp:
25048 case DW_MACRO_undef_strp:
25049 case DW_MACRO_define_sup:
25050 case DW_MACRO_undef_sup:
25051 {
25052 unsigned int bytes_read;
25053
25054 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25055 mac_ptr += bytes_read;
25056 mac_ptr += offset_size;
25057 }
25058 break;
25059
25060 case DW_MACRO_import:
25061 case DW_MACRO_import_sup:
25062 /* Note that, according to the spec, a transparent include
25063 chain cannot call DW_MACRO_start_file. So, we can just
25064 skip this opcode. */
25065 mac_ptr += offset_size;
25066 break;
25067
25068 case DW_MACINFO_vendor_ext:
25069 /* Only skip the data by MAC_PTR. */
25070 if (!section_is_gnu)
25071 {
25072 unsigned int bytes_read;
25073
25074 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25075 mac_ptr += bytes_read;
25076 read_direct_string (abfd, mac_ptr, &bytes_read);
25077 mac_ptr += bytes_read;
25078 }
25079 /* FALLTHROUGH */
25080
25081 default:
25082 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25083 mac_ptr, mac_end, abfd, offset_size,
25084 section);
25085 if (mac_ptr == NULL)
25086 return;
25087 break;
25088 }
25089 DIAGNOSTIC_POP
25090 } while (macinfo_type != 0 && current_file == NULL);
25091
25092 /* Second pass: Process all entries.
25093
25094 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25095 command-line macro definitions/undefinitions. This flag is unset when we
25096 reach the first DW_MACINFO_start_file entry. */
25097
25098 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25099 htab_eq_pointer,
25100 NULL, xcalloc, xfree));
25101 mac_ptr = section->buffer + offset;
25102 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25103 *slot = (void *) mac_ptr;
25104 dwarf_decode_macro_bytes (dwarf2_per_objfile,
25105 abfd, mac_ptr, mac_end,
25106 current_file, lh, section,
25107 section_is_gnu, 0, offset_size,
25108 include_hash.get ());
25109 }
25110
25111 /* Check if the attribute's form is a DW_FORM_block*
25112 if so return true else false. */
25113
25114 static int
25115 attr_form_is_block (const struct attribute *attr)
25116 {
25117 return (attr == NULL ? 0 :
25118 attr->form == DW_FORM_block1
25119 || attr->form == DW_FORM_block2
25120 || attr->form == DW_FORM_block4
25121 || attr->form == DW_FORM_block
25122 || attr->form == DW_FORM_exprloc);
25123 }
25124
25125 /* Return non-zero if ATTR's value is a section offset --- classes
25126 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25127 You may use DW_UNSND (attr) to retrieve such offsets.
25128
25129 Section 7.5.4, "Attribute Encodings", explains that no attribute
25130 may have a value that belongs to more than one of these classes; it
25131 would be ambiguous if we did, because we use the same forms for all
25132 of them. */
25133
25134 static int
25135 attr_form_is_section_offset (const struct attribute *attr)
25136 {
25137 return (attr->form == DW_FORM_data4
25138 || attr->form == DW_FORM_data8
25139 || attr->form == DW_FORM_sec_offset);
25140 }
25141
25142 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25143 zero otherwise. When this function returns true, you can apply
25144 dwarf2_get_attr_constant_value to it.
25145
25146 However, note that for some attributes you must check
25147 attr_form_is_section_offset before using this test. DW_FORM_data4
25148 and DW_FORM_data8 are members of both the constant class, and of
25149 the classes that contain offsets into other debug sections
25150 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25151 that, if an attribute's can be either a constant or one of the
25152 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25153 taken as section offsets, not constants.
25154
25155 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25156 cannot handle that. */
25157
25158 static int
25159 attr_form_is_constant (const struct attribute *attr)
25160 {
25161 switch (attr->form)
25162 {
25163 case DW_FORM_sdata:
25164 case DW_FORM_udata:
25165 case DW_FORM_data1:
25166 case DW_FORM_data2:
25167 case DW_FORM_data4:
25168 case DW_FORM_data8:
25169 case DW_FORM_implicit_const:
25170 return 1;
25171 default:
25172 return 0;
25173 }
25174 }
25175
25176
25177 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25178 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25179
25180 static int
25181 attr_form_is_ref (const struct attribute *attr)
25182 {
25183 switch (attr->form)
25184 {
25185 case DW_FORM_ref_addr:
25186 case DW_FORM_ref1:
25187 case DW_FORM_ref2:
25188 case DW_FORM_ref4:
25189 case DW_FORM_ref8:
25190 case DW_FORM_ref_udata:
25191 case DW_FORM_GNU_ref_alt:
25192 return 1;
25193 default:
25194 return 0;
25195 }
25196 }
25197
25198 /* Return the .debug_loc section to use for CU.
25199 For DWO files use .debug_loc.dwo. */
25200
25201 static struct dwarf2_section_info *
25202 cu_debug_loc_section (struct dwarf2_cu *cu)
25203 {
25204 struct dwarf2_per_objfile *dwarf2_per_objfile
25205 = cu->per_cu->dwarf2_per_objfile;
25206
25207 if (cu->dwo_unit)
25208 {
25209 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25210
25211 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25212 }
25213 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25214 : &dwarf2_per_objfile->loc);
25215 }
25216
25217 /* A helper function that fills in a dwarf2_loclist_baton. */
25218
25219 static void
25220 fill_in_loclist_baton (struct dwarf2_cu *cu,
25221 struct dwarf2_loclist_baton *baton,
25222 const struct attribute *attr)
25223 {
25224 struct dwarf2_per_objfile *dwarf2_per_objfile
25225 = cu->per_cu->dwarf2_per_objfile;
25226 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25227
25228 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25229
25230 baton->per_cu = cu->per_cu;
25231 gdb_assert (baton->per_cu);
25232 /* We don't know how long the location list is, but make sure we
25233 don't run off the edge of the section. */
25234 baton->size = section->size - DW_UNSND (attr);
25235 baton->data = section->buffer + DW_UNSND (attr);
25236 baton->base_address = cu->base_address;
25237 baton->from_dwo = cu->dwo_unit != NULL;
25238 }
25239
25240 static void
25241 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25242 struct dwarf2_cu *cu, int is_block)
25243 {
25244 struct dwarf2_per_objfile *dwarf2_per_objfile
25245 = cu->per_cu->dwarf2_per_objfile;
25246 struct objfile *objfile = dwarf2_per_objfile->objfile;
25247 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25248
25249 if (attr_form_is_section_offset (attr)
25250 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25251 the section. If so, fall through to the complaint in the
25252 other branch. */
25253 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25254 {
25255 struct dwarf2_loclist_baton *baton;
25256
25257 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25258
25259 fill_in_loclist_baton (cu, baton, attr);
25260
25261 if (cu->base_known == 0)
25262 complaint (&symfile_complaints,
25263 _("Location list used without "
25264 "specifying the CU base address."));
25265
25266 SYMBOL_ACLASS_INDEX (sym) = (is_block
25267 ? dwarf2_loclist_block_index
25268 : dwarf2_loclist_index);
25269 SYMBOL_LOCATION_BATON (sym) = baton;
25270 }
25271 else
25272 {
25273 struct dwarf2_locexpr_baton *baton;
25274
25275 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25276 baton->per_cu = cu->per_cu;
25277 gdb_assert (baton->per_cu);
25278
25279 if (attr_form_is_block (attr))
25280 {
25281 /* Note that we're just copying the block's data pointer
25282 here, not the actual data. We're still pointing into the
25283 info_buffer for SYM's objfile; right now we never release
25284 that buffer, but when we do clean up properly this may
25285 need to change. */
25286 baton->size = DW_BLOCK (attr)->size;
25287 baton->data = DW_BLOCK (attr)->data;
25288 }
25289 else
25290 {
25291 dwarf2_invalid_attrib_class_complaint ("location description",
25292 SYMBOL_NATURAL_NAME (sym));
25293 baton->size = 0;
25294 }
25295
25296 SYMBOL_ACLASS_INDEX (sym) = (is_block
25297 ? dwarf2_locexpr_block_index
25298 : dwarf2_locexpr_index);
25299 SYMBOL_LOCATION_BATON (sym) = baton;
25300 }
25301 }
25302
25303 /* Return the OBJFILE associated with the compilation unit CU. If CU
25304 came from a separate debuginfo file, then the master objfile is
25305 returned. */
25306
25307 struct objfile *
25308 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25309 {
25310 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25311
25312 /* Return the master objfile, so that we can report and look up the
25313 correct file containing this variable. */
25314 if (objfile->separate_debug_objfile_backlink)
25315 objfile = objfile->separate_debug_objfile_backlink;
25316
25317 return objfile;
25318 }
25319
25320 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25321 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25322 CU_HEADERP first. */
25323
25324 static const struct comp_unit_head *
25325 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25326 struct dwarf2_per_cu_data *per_cu)
25327 {
25328 const gdb_byte *info_ptr;
25329
25330 if (per_cu->cu)
25331 return &per_cu->cu->header;
25332
25333 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25334
25335 memset (cu_headerp, 0, sizeof (*cu_headerp));
25336 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25337 rcuh_kind::COMPILE);
25338
25339 return cu_headerp;
25340 }
25341
25342 /* Return the address size given in the compilation unit header for CU. */
25343
25344 int
25345 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25346 {
25347 struct comp_unit_head cu_header_local;
25348 const struct comp_unit_head *cu_headerp;
25349
25350 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25351
25352 return cu_headerp->addr_size;
25353 }
25354
25355 /* Return the offset size given in the compilation unit header for CU. */
25356
25357 int
25358 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25359 {
25360 struct comp_unit_head cu_header_local;
25361 const struct comp_unit_head *cu_headerp;
25362
25363 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25364
25365 return cu_headerp->offset_size;
25366 }
25367
25368 /* See its dwarf2loc.h declaration. */
25369
25370 int
25371 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25372 {
25373 struct comp_unit_head cu_header_local;
25374 const struct comp_unit_head *cu_headerp;
25375
25376 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25377
25378 if (cu_headerp->version == 2)
25379 return cu_headerp->addr_size;
25380 else
25381 return cu_headerp->offset_size;
25382 }
25383
25384 /* Return the text offset of the CU. The returned offset comes from
25385 this CU's objfile. If this objfile came from a separate debuginfo
25386 file, then the offset may be different from the corresponding
25387 offset in the parent objfile. */
25388
25389 CORE_ADDR
25390 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25391 {
25392 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25393
25394 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25395 }
25396
25397 /* Return DWARF version number of PER_CU. */
25398
25399 short
25400 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25401 {
25402 return per_cu->dwarf_version;
25403 }
25404
25405 /* Locate the .debug_info compilation unit from CU's objfile which contains
25406 the DIE at OFFSET. Raises an error on failure. */
25407
25408 static struct dwarf2_per_cu_data *
25409 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25410 unsigned int offset_in_dwz,
25411 struct dwarf2_per_objfile *dwarf2_per_objfile)
25412 {
25413 struct dwarf2_per_cu_data *this_cu;
25414 int low, high;
25415 const sect_offset *cu_off;
25416
25417 low = 0;
25418 high = dwarf2_per_objfile->n_comp_units - 1;
25419 while (high > low)
25420 {
25421 struct dwarf2_per_cu_data *mid_cu;
25422 int mid = low + (high - low) / 2;
25423
25424 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25425 cu_off = &mid_cu->sect_off;
25426 if (mid_cu->is_dwz > offset_in_dwz
25427 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
25428 high = mid;
25429 else
25430 low = mid + 1;
25431 }
25432 gdb_assert (low == high);
25433 this_cu = dwarf2_per_objfile->all_comp_units[low];
25434 cu_off = &this_cu->sect_off;
25435 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
25436 {
25437 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25438 error (_("Dwarf Error: could not find partial DIE containing "
25439 "offset %s [in module %s]"),
25440 sect_offset_str (sect_off),
25441 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25442
25443 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25444 <= sect_off);
25445 return dwarf2_per_objfile->all_comp_units[low-1];
25446 }
25447 else
25448 {
25449 this_cu = dwarf2_per_objfile->all_comp_units[low];
25450 if (low == dwarf2_per_objfile->n_comp_units - 1
25451 && sect_off >= this_cu->sect_off + this_cu->length)
25452 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25453 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25454 return this_cu;
25455 }
25456 }
25457
25458 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25459
25460 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25461 : per_cu (per_cu_),
25462 mark (0),
25463 has_loclist (0),
25464 checked_producer (0),
25465 producer_is_gxx_lt_4_6 (0),
25466 producer_is_gcc_lt_4_3 (0),
25467 producer_is_icc_lt_14 (0),
25468 processing_has_namespace_info (0)
25469 {
25470 per_cu->cu = this;
25471 }
25472
25473 /* Destroy a dwarf2_cu. */
25474
25475 dwarf2_cu::~dwarf2_cu ()
25476 {
25477 per_cu->cu = NULL;
25478 }
25479
25480 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25481
25482 static void
25483 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25484 enum language pretend_language)
25485 {
25486 struct attribute *attr;
25487
25488 /* Set the language we're debugging. */
25489 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25490 if (attr)
25491 set_cu_language (DW_UNSND (attr), cu);
25492 else
25493 {
25494 cu->language = pretend_language;
25495 cu->language_defn = language_def (cu->language);
25496 }
25497
25498 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25499 }
25500
25501 /* Free all cached compilation units. */
25502
25503 static void
25504 free_cached_comp_units (void *data)
25505 {
25506 struct dwarf2_per_objfile *dwarf2_per_objfile
25507 = (struct dwarf2_per_objfile *) data;
25508
25509 dwarf2_per_objfile->free_cached_comp_units ();
25510 }
25511
25512 /* Increase the age counter on each cached compilation unit, and free
25513 any that are too old. */
25514
25515 static void
25516 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25517 {
25518 struct dwarf2_per_cu_data *per_cu, **last_chain;
25519
25520 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25521 per_cu = dwarf2_per_objfile->read_in_chain;
25522 while (per_cu != NULL)
25523 {
25524 per_cu->cu->last_used ++;
25525 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25526 dwarf2_mark (per_cu->cu);
25527 per_cu = per_cu->cu->read_in_chain;
25528 }
25529
25530 per_cu = dwarf2_per_objfile->read_in_chain;
25531 last_chain = &dwarf2_per_objfile->read_in_chain;
25532 while (per_cu != NULL)
25533 {
25534 struct dwarf2_per_cu_data *next_cu;
25535
25536 next_cu = per_cu->cu->read_in_chain;
25537
25538 if (!per_cu->cu->mark)
25539 {
25540 delete per_cu->cu;
25541 *last_chain = next_cu;
25542 }
25543 else
25544 last_chain = &per_cu->cu->read_in_chain;
25545
25546 per_cu = next_cu;
25547 }
25548 }
25549
25550 /* Remove a single compilation unit from the cache. */
25551
25552 static void
25553 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25554 {
25555 struct dwarf2_per_cu_data *per_cu, **last_chain;
25556 struct dwarf2_per_objfile *dwarf2_per_objfile
25557 = target_per_cu->dwarf2_per_objfile;
25558
25559 per_cu = dwarf2_per_objfile->read_in_chain;
25560 last_chain = &dwarf2_per_objfile->read_in_chain;
25561 while (per_cu != NULL)
25562 {
25563 struct dwarf2_per_cu_data *next_cu;
25564
25565 next_cu = per_cu->cu->read_in_chain;
25566
25567 if (per_cu == target_per_cu)
25568 {
25569 delete per_cu->cu;
25570 per_cu->cu = NULL;
25571 *last_chain = next_cu;
25572 break;
25573 }
25574 else
25575 last_chain = &per_cu->cu->read_in_chain;
25576
25577 per_cu = next_cu;
25578 }
25579 }
25580
25581 /* Release all extra memory associated with OBJFILE. */
25582
25583 void
25584 dwarf2_free_objfile (struct objfile *objfile)
25585 {
25586 struct dwarf2_per_objfile *dwarf2_per_objfile
25587 = get_dwarf2_per_objfile (objfile);
25588
25589 delete dwarf2_per_objfile;
25590 }
25591
25592 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25593 We store these in a hash table separate from the DIEs, and preserve them
25594 when the DIEs are flushed out of cache.
25595
25596 The CU "per_cu" pointer is needed because offset alone is not enough to
25597 uniquely identify the type. A file may have multiple .debug_types sections,
25598 or the type may come from a DWO file. Furthermore, while it's more logical
25599 to use per_cu->section+offset, with Fission the section with the data is in
25600 the DWO file but we don't know that section at the point we need it.
25601 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25602 because we can enter the lookup routine, get_die_type_at_offset, from
25603 outside this file, and thus won't necessarily have PER_CU->cu.
25604 Fortunately, PER_CU is stable for the life of the objfile. */
25605
25606 struct dwarf2_per_cu_offset_and_type
25607 {
25608 const struct dwarf2_per_cu_data *per_cu;
25609 sect_offset sect_off;
25610 struct type *type;
25611 };
25612
25613 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25614
25615 static hashval_t
25616 per_cu_offset_and_type_hash (const void *item)
25617 {
25618 const struct dwarf2_per_cu_offset_and_type *ofs
25619 = (const struct dwarf2_per_cu_offset_and_type *) item;
25620
25621 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25622 }
25623
25624 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25625
25626 static int
25627 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25628 {
25629 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25630 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25631 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25632 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25633
25634 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25635 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25636 }
25637
25638 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25639 table if necessary. For convenience, return TYPE.
25640
25641 The DIEs reading must have careful ordering to:
25642 * Not cause infite loops trying to read in DIEs as a prerequisite for
25643 reading current DIE.
25644 * Not trying to dereference contents of still incompletely read in types
25645 while reading in other DIEs.
25646 * Enable referencing still incompletely read in types just by a pointer to
25647 the type without accessing its fields.
25648
25649 Therefore caller should follow these rules:
25650 * Try to fetch any prerequisite types we may need to build this DIE type
25651 before building the type and calling set_die_type.
25652 * After building type call set_die_type for current DIE as soon as
25653 possible before fetching more types to complete the current type.
25654 * Make the type as complete as possible before fetching more types. */
25655
25656 static struct type *
25657 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25658 {
25659 struct dwarf2_per_objfile *dwarf2_per_objfile
25660 = cu->per_cu->dwarf2_per_objfile;
25661 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25662 struct objfile *objfile = dwarf2_per_objfile->objfile;
25663 struct attribute *attr;
25664 struct dynamic_prop prop;
25665
25666 /* For Ada types, make sure that the gnat-specific data is always
25667 initialized (if not already set). There are a few types where
25668 we should not be doing so, because the type-specific area is
25669 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25670 where the type-specific area is used to store the floatformat).
25671 But this is not a problem, because the gnat-specific information
25672 is actually not needed for these types. */
25673 if (need_gnat_info (cu)
25674 && TYPE_CODE (type) != TYPE_CODE_FUNC
25675 && TYPE_CODE (type) != TYPE_CODE_FLT
25676 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25677 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25678 && TYPE_CODE (type) != TYPE_CODE_METHOD
25679 && !HAVE_GNAT_AUX_INFO (type))
25680 INIT_GNAT_SPECIFIC (type);
25681
25682 /* Read DW_AT_allocated and set in type. */
25683 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25684 if (attr_form_is_block (attr))
25685 {
25686 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25687 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25688 }
25689 else if (attr != NULL)
25690 {
25691 complaint (&symfile_complaints,
25692 _("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25693 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25694 sect_offset_str (die->sect_off));
25695 }
25696
25697 /* Read DW_AT_associated and set in type. */
25698 attr = dwarf2_attr (die, DW_AT_associated, cu);
25699 if (attr_form_is_block (attr))
25700 {
25701 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25702 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25703 }
25704 else if (attr != NULL)
25705 {
25706 complaint (&symfile_complaints,
25707 _("DW_AT_associated has the wrong form (%s) at DIE %s"),
25708 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25709 sect_offset_str (die->sect_off));
25710 }
25711
25712 /* Read DW_AT_data_location and set in type. */
25713 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25714 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25715 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25716
25717 if (dwarf2_per_objfile->die_type_hash == NULL)
25718 {
25719 dwarf2_per_objfile->die_type_hash =
25720 htab_create_alloc_ex (127,
25721 per_cu_offset_and_type_hash,
25722 per_cu_offset_and_type_eq,
25723 NULL,
25724 &objfile->objfile_obstack,
25725 hashtab_obstack_allocate,
25726 dummy_obstack_deallocate);
25727 }
25728
25729 ofs.per_cu = cu->per_cu;
25730 ofs.sect_off = die->sect_off;
25731 ofs.type = type;
25732 slot = (struct dwarf2_per_cu_offset_and_type **)
25733 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25734 if (*slot)
25735 complaint (&symfile_complaints,
25736 _("A problem internal to GDB: DIE %s has type already set"),
25737 sect_offset_str (die->sect_off));
25738 *slot = XOBNEW (&objfile->objfile_obstack,
25739 struct dwarf2_per_cu_offset_and_type);
25740 **slot = ofs;
25741 return type;
25742 }
25743
25744 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25745 or return NULL if the die does not have a saved type. */
25746
25747 static struct type *
25748 get_die_type_at_offset (sect_offset sect_off,
25749 struct dwarf2_per_cu_data *per_cu)
25750 {
25751 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25752 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25753
25754 if (dwarf2_per_objfile->die_type_hash == NULL)
25755 return NULL;
25756
25757 ofs.per_cu = per_cu;
25758 ofs.sect_off = sect_off;
25759 slot = ((struct dwarf2_per_cu_offset_and_type *)
25760 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25761 if (slot)
25762 return slot->type;
25763 else
25764 return NULL;
25765 }
25766
25767 /* Look up the type for DIE in CU in die_type_hash,
25768 or return NULL if DIE does not have a saved type. */
25769
25770 static struct type *
25771 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25772 {
25773 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25774 }
25775
25776 /* Add a dependence relationship from CU to REF_PER_CU. */
25777
25778 static void
25779 dwarf2_add_dependence (struct dwarf2_cu *cu,
25780 struct dwarf2_per_cu_data *ref_per_cu)
25781 {
25782 void **slot;
25783
25784 if (cu->dependencies == NULL)
25785 cu->dependencies
25786 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25787 NULL, &cu->comp_unit_obstack,
25788 hashtab_obstack_allocate,
25789 dummy_obstack_deallocate);
25790
25791 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25792 if (*slot == NULL)
25793 *slot = ref_per_cu;
25794 }
25795
25796 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25797 Set the mark field in every compilation unit in the
25798 cache that we must keep because we are keeping CU. */
25799
25800 static int
25801 dwarf2_mark_helper (void **slot, void *data)
25802 {
25803 struct dwarf2_per_cu_data *per_cu;
25804
25805 per_cu = (struct dwarf2_per_cu_data *) *slot;
25806
25807 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25808 reading of the chain. As such dependencies remain valid it is not much
25809 useful to track and undo them during QUIT cleanups. */
25810 if (per_cu->cu == NULL)
25811 return 1;
25812
25813 if (per_cu->cu->mark)
25814 return 1;
25815 per_cu->cu->mark = 1;
25816
25817 if (per_cu->cu->dependencies != NULL)
25818 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25819
25820 return 1;
25821 }
25822
25823 /* Set the mark field in CU and in every other compilation unit in the
25824 cache that we must keep because we are keeping CU. */
25825
25826 static void
25827 dwarf2_mark (struct dwarf2_cu *cu)
25828 {
25829 if (cu->mark)
25830 return;
25831 cu->mark = 1;
25832 if (cu->dependencies != NULL)
25833 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25834 }
25835
25836 static void
25837 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25838 {
25839 while (per_cu)
25840 {
25841 per_cu->cu->mark = 0;
25842 per_cu = per_cu->cu->read_in_chain;
25843 }
25844 }
25845
25846 /* Trivial hash function for partial_die_info: the hash value of a DIE
25847 is its offset in .debug_info for this objfile. */
25848
25849 static hashval_t
25850 partial_die_hash (const void *item)
25851 {
25852 const struct partial_die_info *part_die
25853 = (const struct partial_die_info *) item;
25854
25855 return to_underlying (part_die->sect_off);
25856 }
25857
25858 /* Trivial comparison function for partial_die_info structures: two DIEs
25859 are equal if they have the same offset. */
25860
25861 static int
25862 partial_die_eq (const void *item_lhs, const void *item_rhs)
25863 {
25864 const struct partial_die_info *part_die_lhs
25865 = (const struct partial_die_info *) item_lhs;
25866 const struct partial_die_info *part_die_rhs
25867 = (const struct partial_die_info *) item_rhs;
25868
25869 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25870 }
25871
25872 static struct cmd_list_element *set_dwarf_cmdlist;
25873 static struct cmd_list_element *show_dwarf_cmdlist;
25874
25875 static void
25876 set_dwarf_cmd (const char *args, int from_tty)
25877 {
25878 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25879 gdb_stdout);
25880 }
25881
25882 static void
25883 show_dwarf_cmd (const char *args, int from_tty)
25884 {
25885 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25886 }
25887
25888 /* The "save gdb-index" command. */
25889
25890 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25891 error checking. */
25892
25893 static void
25894 file_write (FILE *file, const void *data, size_t size)
25895 {
25896 if (fwrite (data, 1, size, file) != size)
25897 error (_("couldn't data write to file"));
25898 }
25899
25900 /* Write the contents of VEC to FILE, with error checking. */
25901
25902 template<typename Elem, typename Alloc>
25903 static void
25904 file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25905 {
25906 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25907 }
25908
25909 /* In-memory buffer to prepare data to be written later to a file. */
25910 class data_buf
25911 {
25912 public:
25913 /* Copy DATA to the end of the buffer. */
25914 template<typename T>
25915 void append_data (const T &data)
25916 {
25917 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25918 reinterpret_cast<const gdb_byte *> (&data + 1),
25919 grow (sizeof (data)));
25920 }
25921
25922 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25923 terminating zero is appended too. */
25924 void append_cstr0 (const char *cstr)
25925 {
25926 const size_t size = strlen (cstr) + 1;
25927 std::copy (cstr, cstr + size, grow (size));
25928 }
25929
25930 /* Store INPUT as ULEB128 to the end of buffer. */
25931 void append_unsigned_leb128 (ULONGEST input)
25932 {
25933 for (;;)
25934 {
25935 gdb_byte output = input & 0x7f;
25936 input >>= 7;
25937 if (input)
25938 output |= 0x80;
25939 append_data (output);
25940 if (input == 0)
25941 break;
25942 }
25943 }
25944
25945 /* Accept a host-format integer in VAL and append it to the buffer
25946 as a target-format integer which is LEN bytes long. */
25947 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25948 {
25949 ::store_unsigned_integer (grow (len), len, byte_order, val);
25950 }
25951
25952 /* Return the size of the buffer. */
25953 size_t size () const
25954 {
25955 return m_vec.size ();
25956 }
25957
25958 /* Return true iff the buffer is empty. */
25959 bool empty () const
25960 {
25961 return m_vec.empty ();
25962 }
25963
25964 /* Write the buffer to FILE. */
25965 void file_write (FILE *file) const
25966 {
25967 ::file_write (file, m_vec);
25968 }
25969
25970 private:
25971 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25972 the start of the new block. */
25973 gdb_byte *grow (size_t size)
25974 {
25975 m_vec.resize (m_vec.size () + size);
25976 return &*m_vec.end () - size;
25977 }
25978
25979 gdb::byte_vector m_vec;
25980 };
25981
25982 /* An entry in the symbol table. */
25983 struct symtab_index_entry
25984 {
25985 /* The name of the symbol. */
25986 const char *name;
25987 /* The offset of the name in the constant pool. */
25988 offset_type index_offset;
25989 /* A sorted vector of the indices of all the CUs that hold an object
25990 of this name. */
25991 std::vector<offset_type> cu_indices;
25992 };
25993
25994 /* The symbol table. This is a power-of-2-sized hash table. */
25995 struct mapped_symtab
25996 {
25997 mapped_symtab ()
25998 {
25999 data.resize (1024);
26000 }
26001
26002 offset_type n_elements = 0;
26003 std::vector<symtab_index_entry> data;
26004 };
26005
26006 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
26007 the slot.
26008
26009 Function is used only during write_hash_table so no index format backward
26010 compatibility is needed. */
26011
26012 static symtab_index_entry &
26013 find_slot (struct mapped_symtab *symtab, const char *name)
26014 {
26015 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
26016
26017 index = hash & (symtab->data.size () - 1);
26018 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
26019
26020 for (;;)
26021 {
26022 if (symtab->data[index].name == NULL
26023 || strcmp (name, symtab->data[index].name) == 0)
26024 return symtab->data[index];
26025 index = (index + step) & (symtab->data.size () - 1);
26026 }
26027 }
26028
26029 /* Expand SYMTAB's hash table. */
26030
26031 static void
26032 hash_expand (struct mapped_symtab *symtab)
26033 {
26034 auto old_entries = std::move (symtab->data);
26035
26036 symtab->data.clear ();
26037 symtab->data.resize (old_entries.size () * 2);
26038
26039 for (auto &it : old_entries)
26040 if (it.name != NULL)
26041 {
26042 auto &ref = find_slot (symtab, it.name);
26043 ref = std::move (it);
26044 }
26045 }
26046
26047 /* Add an entry to SYMTAB. NAME is the name of the symbol.
26048 CU_INDEX is the index of the CU in which the symbol appears.
26049 IS_STATIC is one if the symbol is static, otherwise zero (global). */
26050
26051 static void
26052 add_index_entry (struct mapped_symtab *symtab, const char *name,
26053 int is_static, gdb_index_symbol_kind kind,
26054 offset_type cu_index)
26055 {
26056 offset_type cu_index_and_attrs;
26057
26058 ++symtab->n_elements;
26059 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
26060 hash_expand (symtab);
26061
26062 symtab_index_entry &slot = find_slot (symtab, name);
26063 if (slot.name == NULL)
26064 {
26065 slot.name = name;
26066 /* index_offset is set later. */
26067 }
26068
26069 cu_index_and_attrs = 0;
26070 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
26071 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
26072 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
26073
26074 /* We don't want to record an index value twice as we want to avoid the
26075 duplication.
26076 We process all global symbols and then all static symbols
26077 (which would allow us to avoid the duplication by only having to check
26078 the last entry pushed), but a symbol could have multiple kinds in one CU.
26079 To keep things simple we don't worry about the duplication here and
26080 sort and uniqufy the list after we've processed all symbols. */
26081 slot.cu_indices.push_back (cu_index_and_attrs);
26082 }
26083
26084 /* Sort and remove duplicates of all symbols' cu_indices lists. */
26085
26086 static void
26087 uniquify_cu_indices (struct mapped_symtab *symtab)
26088 {
26089 for (auto &entry : symtab->data)
26090 {
26091 if (entry.name != NULL && !entry.cu_indices.empty ())
26092 {
26093 auto &cu_indices = entry.cu_indices;
26094 std::sort (cu_indices.begin (), cu_indices.end ());
26095 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
26096 cu_indices.erase (from, cu_indices.end ());
26097 }
26098 }
26099 }
26100
26101 /* A form of 'const char *' suitable for container keys. Only the
26102 pointer is stored. The strings themselves are compared, not the
26103 pointers. */
26104 class c_str_view
26105 {
26106 public:
26107 c_str_view (const char *cstr)
26108 : m_cstr (cstr)
26109 {}
26110
26111 bool operator== (const c_str_view &other) const
26112 {
26113 return strcmp (m_cstr, other.m_cstr) == 0;
26114 }
26115
26116 /* Return the underlying C string. Note, the returned string is
26117 only a reference with lifetime of this object. */
26118 const char *c_str () const
26119 {
26120 return m_cstr;
26121 }
26122
26123 private:
26124 friend class c_str_view_hasher;
26125 const char *const m_cstr;
26126 };
26127
26128 /* A std::unordered_map::hasher for c_str_view that uses the right
26129 hash function for strings in a mapped index. */
26130 class c_str_view_hasher
26131 {
26132 public:
26133 size_t operator () (const c_str_view &x) const
26134 {
26135 return mapped_index_string_hash (INT_MAX, x.m_cstr);
26136 }
26137 };
26138
26139 /* A std::unordered_map::hasher for std::vector<>. */
26140 template<typename T>
26141 class vector_hasher
26142 {
26143 public:
26144 size_t operator () (const std::vector<T> &key) const
26145 {
26146 return iterative_hash (key.data (),
26147 sizeof (key.front ()) * key.size (), 0);
26148 }
26149 };
26150
26151 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
26152 constant pool entries going into the data buffer CPOOL. */
26153
26154 static void
26155 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
26156 {
26157 {
26158 /* Elements are sorted vectors of the indices of all the CUs that
26159 hold an object of this name. */
26160 std::unordered_map<std::vector<offset_type>, offset_type,
26161 vector_hasher<offset_type>>
26162 symbol_hash_table;
26163
26164 /* We add all the index vectors to the constant pool first, to
26165 ensure alignment is ok. */
26166 for (symtab_index_entry &entry : symtab->data)
26167 {
26168 if (entry.name == NULL)
26169 continue;
26170 gdb_assert (entry.index_offset == 0);
26171
26172 /* Finding before inserting is faster than always trying to
26173 insert, because inserting always allocates a node, does the
26174 lookup, and then destroys the new node if another node
26175 already had the same key. C++17 try_emplace will avoid
26176 this. */
26177 const auto found
26178 = symbol_hash_table.find (entry.cu_indices);
26179 if (found != symbol_hash_table.end ())
26180 {
26181 entry.index_offset = found->second;
26182 continue;
26183 }
26184
26185 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
26186 entry.index_offset = cpool.size ();
26187 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
26188 for (const auto index : entry.cu_indices)
26189 cpool.append_data (MAYBE_SWAP (index));
26190 }
26191 }
26192
26193 /* Now write out the hash table. */
26194 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
26195 for (const auto &entry : symtab->data)
26196 {
26197 offset_type str_off, vec_off;
26198
26199 if (entry.name != NULL)
26200 {
26201 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
26202 if (insertpair.second)
26203 cpool.append_cstr0 (entry.name);
26204 str_off = insertpair.first->second;
26205 vec_off = entry.index_offset;
26206 }
26207 else
26208 {
26209 /* While 0 is a valid constant pool index, it is not valid
26210 to have 0 for both offsets. */
26211 str_off = 0;
26212 vec_off = 0;
26213 }
26214
26215 output.append_data (MAYBE_SWAP (str_off));
26216 output.append_data (MAYBE_SWAP (vec_off));
26217 }
26218 }
26219
26220 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
26221
26222 /* Helper struct for building the address table. */
26223 struct addrmap_index_data
26224 {
26225 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
26226 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
26227 {}
26228
26229 struct objfile *objfile;
26230 data_buf &addr_vec;
26231 psym_index_map &cu_index_htab;
26232
26233 /* Non-zero if the previous_* fields are valid.
26234 We can't write an entry until we see the next entry (since it is only then
26235 that we know the end of the entry). */
26236 int previous_valid;
26237 /* Index of the CU in the table of all CUs in the index file. */
26238 unsigned int previous_cu_index;
26239 /* Start address of the CU. */
26240 CORE_ADDR previous_cu_start;
26241 };
26242
26243 /* Write an address entry to ADDR_VEC. */
26244
26245 static void
26246 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
26247 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
26248 {
26249 CORE_ADDR baseaddr;
26250
26251 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
26252
26253 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
26254 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
26255 addr_vec.append_data (MAYBE_SWAP (cu_index));
26256 }
26257
26258 /* Worker function for traversing an addrmap to build the address table. */
26259
26260 static int
26261 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
26262 {
26263 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
26264 struct partial_symtab *pst = (struct partial_symtab *) obj;
26265
26266 if (data->previous_valid)
26267 add_address_entry (data->objfile, data->addr_vec,
26268 data->previous_cu_start, start_addr,
26269 data->previous_cu_index);
26270
26271 data->previous_cu_start = start_addr;
26272 if (pst != NULL)
26273 {
26274 const auto it = data->cu_index_htab.find (pst);
26275 gdb_assert (it != data->cu_index_htab.cend ());
26276 data->previous_cu_index = it->second;
26277 data->previous_valid = 1;
26278 }
26279 else
26280 data->previous_valid = 0;
26281
26282 return 0;
26283 }
26284
26285 /* Write OBJFILE's address map to ADDR_VEC.
26286 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
26287 in the index file. */
26288
26289 static void
26290 write_address_map (struct objfile *objfile, data_buf &addr_vec,
26291 psym_index_map &cu_index_htab)
26292 {
26293 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
26294
26295 /* When writing the address table, we have to cope with the fact that
26296 the addrmap iterator only provides the start of a region; we have to
26297 wait until the next invocation to get the start of the next region. */
26298
26299 addrmap_index_data.objfile = objfile;
26300 addrmap_index_data.previous_valid = 0;
26301
26302 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
26303 &addrmap_index_data);
26304
26305 /* It's highly unlikely the last entry (end address = 0xff...ff)
26306 is valid, but we should still handle it.
26307 The end address is recorded as the start of the next region, but that
26308 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
26309 anyway. */
26310 if (addrmap_index_data.previous_valid)
26311 add_address_entry (objfile, addr_vec,
26312 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
26313 addrmap_index_data.previous_cu_index);
26314 }
26315
26316 /* Return the symbol kind of PSYM. */
26317
26318 static gdb_index_symbol_kind
26319 symbol_kind (struct partial_symbol *psym)
26320 {
26321 domain_enum domain = PSYMBOL_DOMAIN (psym);
26322 enum address_class aclass = PSYMBOL_CLASS (psym);
26323
26324 switch (domain)
26325 {
26326 case VAR_DOMAIN:
26327 switch (aclass)
26328 {
26329 case LOC_BLOCK:
26330 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
26331 case LOC_TYPEDEF:
26332 return GDB_INDEX_SYMBOL_KIND_TYPE;
26333 case LOC_COMPUTED:
26334 case LOC_CONST_BYTES:
26335 case LOC_OPTIMIZED_OUT:
26336 case LOC_STATIC:
26337 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
26338 case LOC_CONST:
26339 /* Note: It's currently impossible to recognize psyms as enum values
26340 short of reading the type info. For now punt. */
26341 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
26342 default:
26343 /* There are other LOC_FOO values that one might want to classify
26344 as variables, but dwarf2read.c doesn't currently use them. */
26345 return GDB_INDEX_SYMBOL_KIND_OTHER;
26346 }
26347 case STRUCT_DOMAIN:
26348 return GDB_INDEX_SYMBOL_KIND_TYPE;
26349 default:
26350 return GDB_INDEX_SYMBOL_KIND_OTHER;
26351 }
26352 }
26353
26354 /* Add a list of partial symbols to SYMTAB. */
26355
26356 static void
26357 write_psymbols (struct mapped_symtab *symtab,
26358 std::unordered_set<partial_symbol *> &psyms_seen,
26359 struct partial_symbol **psymp,
26360 int count,
26361 offset_type cu_index,
26362 int is_static)
26363 {
26364 for (; count-- > 0; ++psymp)
26365 {
26366 struct partial_symbol *psym = *psymp;
26367
26368 if (SYMBOL_LANGUAGE (psym) == language_ada)
26369 error (_("Ada is not currently supported by the index"));
26370
26371 /* Only add a given psymbol once. */
26372 if (psyms_seen.insert (psym).second)
26373 {
26374 gdb_index_symbol_kind kind = symbol_kind (psym);
26375
26376 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
26377 is_static, kind, cu_index);
26378 }
26379 }
26380 }
26381
26382 /* A helper struct used when iterating over debug_types. */
26383 struct signatured_type_index_data
26384 {
26385 signatured_type_index_data (data_buf &types_list_,
26386 std::unordered_set<partial_symbol *> &psyms_seen_)
26387 : types_list (types_list_), psyms_seen (psyms_seen_)
26388 {}
26389
26390 struct objfile *objfile;
26391 struct mapped_symtab *symtab;
26392 data_buf &types_list;
26393 std::unordered_set<partial_symbol *> &psyms_seen;
26394 int cu_index;
26395 };
26396
26397 /* A helper function that writes a single signatured_type to an
26398 obstack. */
26399
26400 static int
26401 write_one_signatured_type (void **slot, void *d)
26402 {
26403 struct signatured_type_index_data *info
26404 = (struct signatured_type_index_data *) d;
26405 struct signatured_type *entry = (struct signatured_type *) *slot;
26406 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26407
26408 write_psymbols (info->symtab,
26409 info->psyms_seen,
26410 &info->objfile->global_psymbols[psymtab->globals_offset],
26411 psymtab->n_global_syms, info->cu_index,
26412 0);
26413 write_psymbols (info->symtab,
26414 info->psyms_seen,
26415 &info->objfile->static_psymbols[psymtab->statics_offset],
26416 psymtab->n_static_syms, info->cu_index,
26417 1);
26418
26419 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26420 to_underlying (entry->per_cu.sect_off));
26421 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26422 to_underlying (entry->type_offset_in_tu));
26423 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
26424
26425 ++info->cu_index;
26426
26427 return 1;
26428 }
26429
26430 /* Recurse into all "included" dependencies and count their symbols as
26431 if they appeared in this psymtab. */
26432
26433 static void
26434 recursively_count_psymbols (struct partial_symtab *psymtab,
26435 size_t &psyms_seen)
26436 {
26437 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26438 if (psymtab->dependencies[i]->user != NULL)
26439 recursively_count_psymbols (psymtab->dependencies[i],
26440 psyms_seen);
26441
26442 psyms_seen += psymtab->n_global_syms;
26443 psyms_seen += psymtab->n_static_syms;
26444 }
26445
26446 /* Recurse into all "included" dependencies and write their symbols as
26447 if they appeared in this psymtab. */
26448
26449 static void
26450 recursively_write_psymbols (struct objfile *objfile,
26451 struct partial_symtab *psymtab,
26452 struct mapped_symtab *symtab,
26453 std::unordered_set<partial_symbol *> &psyms_seen,
26454 offset_type cu_index)
26455 {
26456 int i;
26457
26458 for (i = 0; i < psymtab->number_of_dependencies; ++i)
26459 if (psymtab->dependencies[i]->user != NULL)
26460 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26461 symtab, psyms_seen, cu_index);
26462
26463 write_psymbols (symtab,
26464 psyms_seen,
26465 &objfile->global_psymbols[psymtab->globals_offset],
26466 psymtab->n_global_syms, cu_index,
26467 0);
26468 write_psymbols (symtab,
26469 psyms_seen,
26470 &objfile->static_psymbols[psymtab->statics_offset],
26471 psymtab->n_static_syms, cu_index,
26472 1);
26473 }
26474
26475 /* DWARF-5 .debug_names builder. */
26476 class debug_names
26477 {
26478 public:
26479 debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile, bool is_dwarf64,
26480 bfd_endian dwarf5_byte_order)
26481 : m_dwarf5_byte_order (dwarf5_byte_order),
26482 m_dwarf32 (dwarf5_byte_order),
26483 m_dwarf64 (dwarf5_byte_order),
26484 m_dwarf (is_dwarf64
26485 ? static_cast<dwarf &> (m_dwarf64)
26486 : static_cast<dwarf &> (m_dwarf32)),
26487 m_name_table_string_offs (m_dwarf.name_table_string_offs),
26488 m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
26489 m_debugstrlookup (dwarf2_per_objfile)
26490 {}
26491
26492 int dwarf5_offset_size () const
26493 {
26494 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
26495 return dwarf5_is_dwarf64 ? 8 : 4;
26496 }
26497
26498 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
26499 enum class unit_kind { cu, tu };
26500
26501 /* Insert one symbol. */
26502 void insert (const partial_symbol *psym, int cu_index, bool is_static,
26503 unit_kind kind)
26504 {
26505 const int dwarf_tag = psymbol_tag (psym);
26506 if (dwarf_tag == 0)
26507 return;
26508 const char *const name = SYMBOL_SEARCH_NAME (psym);
26509 const auto insertpair
26510 = m_name_to_value_set.emplace (c_str_view (name),
26511 std::set<symbol_value> ());
26512 std::set<symbol_value> &value_set = insertpair.first->second;
26513 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
26514 }
26515
26516 /* Build all the tables. All symbols must be already inserted.
26517 This function does not call file_write, caller has to do it
26518 afterwards. */
26519 void build ()
26520 {
26521 /* Verify the build method has not be called twice. */
26522 gdb_assert (m_abbrev_table.empty ());
26523 const size_t name_count = m_name_to_value_set.size ();
26524 m_bucket_table.resize
26525 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26526 m_hash_table.reserve (name_count);
26527 m_name_table_string_offs.reserve (name_count);
26528 m_name_table_entry_offs.reserve (name_count);
26529
26530 /* Map each hash of symbol to its name and value. */
26531 struct hash_it_pair
26532 {
26533 uint32_t hash;
26534 decltype (m_name_to_value_set)::const_iterator it;
26535 };
26536 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26537 bucket_hash.resize (m_bucket_table.size ());
26538 for (decltype (m_name_to_value_set)::const_iterator it
26539 = m_name_to_value_set.cbegin ();
26540 it != m_name_to_value_set.cend ();
26541 ++it)
26542 {
26543 const char *const name = it->first.c_str ();
26544 const uint32_t hash = dwarf5_djb_hash (name);
26545 hash_it_pair hashitpair;
26546 hashitpair.hash = hash;
26547 hashitpair.it = it;
26548 auto &slot = bucket_hash[hash % bucket_hash.size()];
26549 slot.push_front (std::move (hashitpair));
26550 }
26551 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26552 {
26553 const std::forward_list<hash_it_pair> &hashitlist
26554 = bucket_hash[bucket_ix];
26555 if (hashitlist.empty ())
26556 continue;
26557 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26558 /* The hashes array is indexed starting at 1. */
26559 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26560 sizeof (bucket_slot), m_dwarf5_byte_order,
26561 m_hash_table.size () + 1);
26562 for (const hash_it_pair &hashitpair : hashitlist)
26563 {
26564 m_hash_table.push_back (0);
26565 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26566 (&m_hash_table.back ()),
26567 sizeof (m_hash_table.back ()),
26568 m_dwarf5_byte_order, hashitpair.hash);
26569 const c_str_view &name = hashitpair.it->first;
26570 const std::set<symbol_value> &value_set = hashitpair.it->second;
26571 m_name_table_string_offs.push_back_reorder
26572 (m_debugstrlookup.lookup (name.c_str ()));
26573 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26574 gdb_assert (!value_set.empty ());
26575 for (const symbol_value &value : value_set)
26576 {
26577 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
26578 value.is_static,
26579 value.kind)];
26580 if (idx == 0)
26581 {
26582 idx = m_idx_next++;
26583 m_abbrev_table.append_unsigned_leb128 (idx);
26584 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
26585 m_abbrev_table.append_unsigned_leb128
26586 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
26587 : DW_IDX_type_unit);
26588 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26589 m_abbrev_table.append_unsigned_leb128 (value.is_static
26590 ? DW_IDX_GNU_internal
26591 : DW_IDX_GNU_external);
26592 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26593
26594 /* Terminate attributes list. */
26595 m_abbrev_table.append_unsigned_leb128 (0);
26596 m_abbrev_table.append_unsigned_leb128 (0);
26597 }
26598
26599 m_entry_pool.append_unsigned_leb128 (idx);
26600 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26601 }
26602
26603 /* Terminate the list of CUs. */
26604 m_entry_pool.append_unsigned_leb128 (0);
26605 }
26606 }
26607 gdb_assert (m_hash_table.size () == name_count);
26608
26609 /* Terminate tags list. */
26610 m_abbrev_table.append_unsigned_leb128 (0);
26611 }
26612
26613 /* Return .debug_names bucket count. This must be called only after
26614 calling the build method. */
26615 uint32_t bucket_count () const
26616 {
26617 /* Verify the build method has been already called. */
26618 gdb_assert (!m_abbrev_table.empty ());
26619 const uint32_t retval = m_bucket_table.size ();
26620
26621 /* Check for overflow. */
26622 gdb_assert (retval == m_bucket_table.size ());
26623 return retval;
26624 }
26625
26626 /* Return .debug_names names count. This must be called only after
26627 calling the build method. */
26628 uint32_t name_count () const
26629 {
26630 /* Verify the build method has been already called. */
26631 gdb_assert (!m_abbrev_table.empty ());
26632 const uint32_t retval = m_hash_table.size ();
26633
26634 /* Check for overflow. */
26635 gdb_assert (retval == m_hash_table.size ());
26636 return retval;
26637 }
26638
26639 /* Return number of bytes of .debug_names abbreviation table. This
26640 must be called only after calling the build method. */
26641 uint32_t abbrev_table_bytes () const
26642 {
26643 gdb_assert (!m_abbrev_table.empty ());
26644 return m_abbrev_table.size ();
26645 }
26646
26647 /* Recurse into all "included" dependencies and store their symbols
26648 as if they appeared in this psymtab. */
26649 void recursively_write_psymbols
26650 (struct objfile *objfile,
26651 struct partial_symtab *psymtab,
26652 std::unordered_set<partial_symbol *> &psyms_seen,
26653 int cu_index)
26654 {
26655 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26656 if (psymtab->dependencies[i]->user != NULL)
26657 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26658 psyms_seen, cu_index);
26659
26660 write_psymbols (psyms_seen,
26661 &objfile->global_psymbols[psymtab->globals_offset],
26662 psymtab->n_global_syms, cu_index, false, unit_kind::cu);
26663 write_psymbols (psyms_seen,
26664 &objfile->static_psymbols[psymtab->statics_offset],
26665 psymtab->n_static_syms, cu_index, true, unit_kind::cu);
26666 }
26667
26668 /* Return number of bytes the .debug_names section will have. This
26669 must be called only after calling the build method. */
26670 size_t bytes () const
26671 {
26672 /* Verify the build method has been already called. */
26673 gdb_assert (!m_abbrev_table.empty ());
26674 size_t expected_bytes = 0;
26675 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26676 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26677 expected_bytes += m_name_table_string_offs.bytes ();
26678 expected_bytes += m_name_table_entry_offs.bytes ();
26679 expected_bytes += m_abbrev_table.size ();
26680 expected_bytes += m_entry_pool.size ();
26681 return expected_bytes;
26682 }
26683
26684 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26685 FILE_STR. This must be called only after calling the build
26686 method. */
26687 void file_write (FILE *file_names, FILE *file_str) const
26688 {
26689 /* Verify the build method has been already called. */
26690 gdb_assert (!m_abbrev_table.empty ());
26691 ::file_write (file_names, m_bucket_table);
26692 ::file_write (file_names, m_hash_table);
26693 m_name_table_string_offs.file_write (file_names);
26694 m_name_table_entry_offs.file_write (file_names);
26695 m_abbrev_table.file_write (file_names);
26696 m_entry_pool.file_write (file_names);
26697 m_debugstrlookup.file_write (file_str);
26698 }
26699
26700 /* A helper user data for write_one_signatured_type. */
26701 class write_one_signatured_type_data
26702 {
26703 public:
26704 write_one_signatured_type_data (debug_names &nametable_,
26705 signatured_type_index_data &&info_)
26706 : nametable (nametable_), info (std::move (info_))
26707 {}
26708 debug_names &nametable;
26709 struct signatured_type_index_data info;
26710 };
26711
26712 /* A helper function to pass write_one_signatured_type to
26713 htab_traverse_noresize. */
26714 static int
26715 write_one_signatured_type (void **slot, void *d)
26716 {
26717 write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
26718 struct signatured_type_index_data *info = &data->info;
26719 struct signatured_type *entry = (struct signatured_type *) *slot;
26720
26721 data->nametable.write_one_signatured_type (entry, info);
26722
26723 return 1;
26724 }
26725
26726 private:
26727
26728 /* Storage for symbol names mapping them to their .debug_str section
26729 offsets. */
26730 class debug_str_lookup
26731 {
26732 public:
26733
26734 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26735 All .debug_str section strings are automatically stored. */
26736 debug_str_lookup (struct dwarf2_per_objfile *dwarf2_per_objfile)
26737 : m_abfd (dwarf2_per_objfile->objfile->obfd),
26738 m_dwarf2_per_objfile (dwarf2_per_objfile)
26739 {
26740 dwarf2_read_section (dwarf2_per_objfile->objfile,
26741 &dwarf2_per_objfile->str);
26742 if (dwarf2_per_objfile->str.buffer == NULL)
26743 return;
26744 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26745 data < (dwarf2_per_objfile->str.buffer
26746 + dwarf2_per_objfile->str.size);)
26747 {
26748 const char *const s = reinterpret_cast<const char *> (data);
26749 const auto insertpair
26750 = m_str_table.emplace (c_str_view (s),
26751 data - dwarf2_per_objfile->str.buffer);
26752 if (!insertpair.second)
26753 complaint (&symfile_complaints,
26754 _("Duplicate string \"%s\" in "
26755 ".debug_str section [in module %s]"),
26756 s, bfd_get_filename (m_abfd));
26757 data += strlen (s) + 1;
26758 }
26759 }
26760
26761 /* Return offset of symbol name S in the .debug_str section. Add
26762 such symbol to the section's end if it does not exist there
26763 yet. */
26764 size_t lookup (const char *s)
26765 {
26766 const auto it = m_str_table.find (c_str_view (s));
26767 if (it != m_str_table.end ())
26768 return it->second;
26769 const size_t offset = (m_dwarf2_per_objfile->str.size
26770 + m_str_add_buf.size ());
26771 m_str_table.emplace (c_str_view (s), offset);
26772 m_str_add_buf.append_cstr0 (s);
26773 return offset;
26774 }
26775
26776 /* Append the end of the .debug_str section to FILE. */
26777 void file_write (FILE *file) const
26778 {
26779 m_str_add_buf.file_write (file);
26780 }
26781
26782 private:
26783 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26784 bfd *const m_abfd;
26785 struct dwarf2_per_objfile *m_dwarf2_per_objfile;
26786
26787 /* Data to add at the end of .debug_str for new needed symbol names. */
26788 data_buf m_str_add_buf;
26789 };
26790
26791 /* Container to map used DWARF tags to their .debug_names abbreviation
26792 tags. */
26793 class index_key
26794 {
26795 public:
26796 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
26797 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
26798 {
26799 }
26800
26801 bool
26802 operator== (const index_key &other) const
26803 {
26804 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
26805 && kind == other.kind);
26806 }
26807
26808 const int dwarf_tag;
26809 const bool is_static;
26810 const unit_kind kind;
26811 };
26812
26813 /* Provide std::unordered_map::hasher for index_key. */
26814 class index_key_hasher
26815 {
26816 public:
26817 size_t
26818 operator () (const index_key &key) const
26819 {
26820 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26821 }
26822 };
26823
26824 /* Parameters of one symbol entry. */
26825 class symbol_value
26826 {
26827 public:
26828 const int dwarf_tag, cu_index;
26829 const bool is_static;
26830 const unit_kind kind;
26831
26832 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
26833 unit_kind kind_)
26834 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
26835 kind (kind_)
26836 {}
26837
26838 bool
26839 operator< (const symbol_value &other) const
26840 {
26841 #define X(n) \
26842 do \
26843 { \
26844 if (n < other.n) \
26845 return true; \
26846 if (n > other.n) \
26847 return false; \
26848 } \
26849 while (0)
26850 X (dwarf_tag);
26851 X (is_static);
26852 X (kind);
26853 X (cu_index);
26854 #undef X
26855 return false;
26856 }
26857 };
26858
26859 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26860 output. */
26861 class offset_vec
26862 {
26863 protected:
26864 const bfd_endian dwarf5_byte_order;
26865 public:
26866 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26867 : dwarf5_byte_order (dwarf5_byte_order_)
26868 {}
26869
26870 /* Call std::vector::reserve for NELEM elements. */
26871 virtual void reserve (size_t nelem) = 0;
26872
26873 /* Call std::vector::push_back with store_unsigned_integer byte
26874 reordering for ELEM. */
26875 virtual void push_back_reorder (size_t elem) = 0;
26876
26877 /* Return expected output size in bytes. */
26878 virtual size_t bytes () const = 0;
26879
26880 /* Write name table to FILE. */
26881 virtual void file_write (FILE *file) const = 0;
26882 };
26883
26884 /* Template to unify DWARF-32 and DWARF-64 output. */
26885 template<typename OffsetSize>
26886 class offset_vec_tmpl : public offset_vec
26887 {
26888 public:
26889 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26890 : offset_vec (dwarf5_byte_order_)
26891 {}
26892
26893 /* Implement offset_vec::reserve. */
26894 void reserve (size_t nelem) override
26895 {
26896 m_vec.reserve (nelem);
26897 }
26898
26899 /* Implement offset_vec::push_back_reorder. */
26900 void push_back_reorder (size_t elem) override
26901 {
26902 m_vec.push_back (elem);
26903 /* Check for overflow. */
26904 gdb_assert (m_vec.back () == elem);
26905 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26906 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26907 }
26908
26909 /* Implement offset_vec::bytes. */
26910 size_t bytes () const override
26911 {
26912 return m_vec.size () * sizeof (m_vec[0]);
26913 }
26914
26915 /* Implement offset_vec::file_write. */
26916 void file_write (FILE *file) const override
26917 {
26918 ::file_write (file, m_vec);
26919 }
26920
26921 private:
26922 std::vector<OffsetSize> m_vec;
26923 };
26924
26925 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26926 respecting name table width. */
26927 class dwarf
26928 {
26929 public:
26930 offset_vec &name_table_string_offs, &name_table_entry_offs;
26931
26932 dwarf (offset_vec &name_table_string_offs_,
26933 offset_vec &name_table_entry_offs_)
26934 : name_table_string_offs (name_table_string_offs_),
26935 name_table_entry_offs (name_table_entry_offs_)
26936 {
26937 }
26938 };
26939
26940 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26941 respecting name table width. */
26942 template<typename OffsetSize>
26943 class dwarf_tmpl : public dwarf
26944 {
26945 public:
26946 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26947 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26948 m_name_table_string_offs (dwarf5_byte_order_),
26949 m_name_table_entry_offs (dwarf5_byte_order_)
26950 {}
26951
26952 private:
26953 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26954 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26955 };
26956
26957 /* Try to reconstruct original DWARF tag for given partial_symbol.
26958 This function is not DWARF-5 compliant but it is sufficient for
26959 GDB as a DWARF-5 index consumer. */
26960 static int psymbol_tag (const struct partial_symbol *psym)
26961 {
26962 domain_enum domain = PSYMBOL_DOMAIN (psym);
26963 enum address_class aclass = PSYMBOL_CLASS (psym);
26964
26965 switch (domain)
26966 {
26967 case VAR_DOMAIN:
26968 switch (aclass)
26969 {
26970 case LOC_BLOCK:
26971 return DW_TAG_subprogram;
26972 case LOC_TYPEDEF:
26973 return DW_TAG_typedef;
26974 case LOC_COMPUTED:
26975 case LOC_CONST_BYTES:
26976 case LOC_OPTIMIZED_OUT:
26977 case LOC_STATIC:
26978 return DW_TAG_variable;
26979 case LOC_CONST:
26980 /* Note: It's currently impossible to recognize psyms as enum values
26981 short of reading the type info. For now punt. */
26982 return DW_TAG_variable;
26983 default:
26984 /* There are other LOC_FOO values that one might want to classify
26985 as variables, but dwarf2read.c doesn't currently use them. */
26986 return DW_TAG_variable;
26987 }
26988 case STRUCT_DOMAIN:
26989 return DW_TAG_structure_type;
26990 default:
26991 return 0;
26992 }
26993 }
26994
26995 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26996 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
26997 struct partial_symbol **psymp, int count, int cu_index,
26998 bool is_static, unit_kind kind)
26999 {
27000 for (; count-- > 0; ++psymp)
27001 {
27002 struct partial_symbol *psym = *psymp;
27003
27004 if (SYMBOL_LANGUAGE (psym) == language_ada)
27005 error (_("Ada is not currently supported by the index"));
27006
27007 /* Only add a given psymbol once. */
27008 if (psyms_seen.insert (psym).second)
27009 insert (psym, cu_index, is_static, kind);
27010 }
27011 }
27012
27013 /* A helper function that writes a single signatured_type
27014 to a debug_names. */
27015 void
27016 write_one_signatured_type (struct signatured_type *entry,
27017 struct signatured_type_index_data *info)
27018 {
27019 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
27020
27021 write_psymbols (info->psyms_seen,
27022 &info->objfile->global_psymbols[psymtab->globals_offset],
27023 psymtab->n_global_syms, info->cu_index, false,
27024 unit_kind::tu);
27025 write_psymbols (info->psyms_seen,
27026 &info->objfile->static_psymbols[psymtab->statics_offset],
27027 psymtab->n_static_syms, info->cu_index, true,
27028 unit_kind::tu);
27029
27030 info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
27031 to_underlying (entry->per_cu.sect_off));
27032
27033 ++info->cu_index;
27034 }
27035
27036 /* Store value of each symbol. */
27037 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
27038 m_name_to_value_set;
27039
27040 /* Tables of DWARF-5 .debug_names. They are in object file byte
27041 order. */
27042 std::vector<uint32_t> m_bucket_table;
27043 std::vector<uint32_t> m_hash_table;
27044
27045 const bfd_endian m_dwarf5_byte_order;
27046 dwarf_tmpl<uint32_t> m_dwarf32;
27047 dwarf_tmpl<uint64_t> m_dwarf64;
27048 dwarf &m_dwarf;
27049 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
27050 debug_str_lookup m_debugstrlookup;
27051
27052 /* Map each used .debug_names abbreviation tag parameter to its
27053 index value. */
27054 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
27055
27056 /* Next unused .debug_names abbreviation tag for
27057 m_indexkey_to_idx. */
27058 int m_idx_next = 1;
27059
27060 /* .debug_names abbreviation table. */
27061 data_buf m_abbrev_table;
27062
27063 /* .debug_names entry pool. */
27064 data_buf m_entry_pool;
27065 };
27066
27067 /* Return iff any of the needed offsets does not fit into 32-bit
27068 .debug_names section. */
27069
27070 static bool
27071 check_dwarf64_offsets (struct dwarf2_per_objfile *dwarf2_per_objfile)
27072 {
27073 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27074 {
27075 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
27076
27077 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
27078 return true;
27079 }
27080 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
27081 {
27082 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
27083 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
27084
27085 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
27086 return true;
27087 }
27088 return false;
27089 }
27090
27091 /* The psyms_seen set is potentially going to be largish (~40k
27092 elements when indexing a -g3 build of GDB itself). Estimate the
27093 number of elements in order to avoid too many rehashes, which
27094 require rebuilding buckets and thus many trips to
27095 malloc/free. */
27096
27097 static size_t
27098 psyms_seen_size (struct dwarf2_per_objfile *dwarf2_per_objfile)
27099 {
27100 size_t psyms_count = 0;
27101 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27102 {
27103 struct dwarf2_per_cu_data *per_cu
27104 = dwarf2_per_objfile->all_comp_units[i];
27105 struct partial_symtab *psymtab = per_cu->v.psymtab;
27106
27107 if (psymtab != NULL && psymtab->user == NULL)
27108 recursively_count_psymbols (psymtab, psyms_count);
27109 }
27110 /* Generating an index for gdb itself shows a ratio of
27111 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
27112 return psyms_count / 4;
27113 }
27114
27115 /* Write new .gdb_index section for OBJFILE into OUT_FILE.
27116 Return how many bytes were expected to be written into OUT_FILE. */
27117
27118 static size_t
27119 write_gdbindex (struct dwarf2_per_objfile *dwarf2_per_objfile, FILE *out_file)
27120 {
27121 struct objfile *objfile = dwarf2_per_objfile->objfile;
27122 mapped_symtab symtab;
27123 data_buf cu_list;
27124
27125 /* While we're scanning CU's create a table that maps a psymtab pointer
27126 (which is what addrmap records) to its index (which is what is recorded
27127 in the index file). This will later be needed to write the address
27128 table. */
27129 psym_index_map cu_index_htab;
27130 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
27131
27132 /* The CU list is already sorted, so we don't need to do additional
27133 work here. Also, the debug_types entries do not appear in
27134 all_comp_units, but only in their own hash table. */
27135
27136 std::unordered_set<partial_symbol *> psyms_seen
27137 (psyms_seen_size (dwarf2_per_objfile));
27138 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27139 {
27140 struct dwarf2_per_cu_data *per_cu
27141 = dwarf2_per_objfile->all_comp_units[i];
27142 struct partial_symtab *psymtab = per_cu->v.psymtab;
27143
27144 /* CU of a shared file from 'dwz -m' may be unused by this main file.
27145 It may be referenced from a local scope but in such case it does not
27146 need to be present in .gdb_index. */
27147 if (psymtab == NULL)
27148 continue;
27149
27150 if (psymtab->user == NULL)
27151 recursively_write_psymbols (objfile, psymtab, &symtab,
27152 psyms_seen, i);
27153
27154 const auto insertpair = cu_index_htab.emplace (psymtab, i);
27155 gdb_assert (insertpair.second);
27156
27157 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
27158 to_underlying (per_cu->sect_off));
27159 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
27160 }
27161
27162 /* Dump the address map. */
27163 data_buf addr_vec;
27164 write_address_map (objfile, addr_vec, cu_index_htab);
27165
27166 /* Write out the .debug_type entries, if any. */
27167 data_buf types_cu_list;
27168 if (dwarf2_per_objfile->signatured_types)
27169 {
27170 signatured_type_index_data sig_data (types_cu_list,
27171 psyms_seen);
27172
27173 sig_data.objfile = objfile;
27174 sig_data.symtab = &symtab;
27175 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
27176 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
27177 write_one_signatured_type, &sig_data);
27178 }
27179
27180 /* Now that we've processed all symbols we can shrink their cu_indices
27181 lists. */
27182 uniquify_cu_indices (&symtab);
27183
27184 data_buf symtab_vec, constant_pool;
27185 write_hash_table (&symtab, symtab_vec, constant_pool);
27186
27187 data_buf contents;
27188 const offset_type size_of_contents = 6 * sizeof (offset_type);
27189 offset_type total_len = size_of_contents;
27190
27191 /* The version number. */
27192 contents.append_data (MAYBE_SWAP (8));
27193
27194 /* The offset of the CU list from the start of the file. */
27195 contents.append_data (MAYBE_SWAP (total_len));
27196 total_len += cu_list.size ();
27197
27198 /* The offset of the types CU list from the start of the file. */
27199 contents.append_data (MAYBE_SWAP (total_len));
27200 total_len += types_cu_list.size ();
27201
27202 /* The offset of the address table from the start of the file. */
27203 contents.append_data (MAYBE_SWAP (total_len));
27204 total_len += addr_vec.size ();
27205
27206 /* The offset of the symbol table from the start of the file. */
27207 contents.append_data (MAYBE_SWAP (total_len));
27208 total_len += symtab_vec.size ();
27209
27210 /* The offset of the constant pool from the start of the file. */
27211 contents.append_data (MAYBE_SWAP (total_len));
27212 total_len += constant_pool.size ();
27213
27214 gdb_assert (contents.size () == size_of_contents);
27215
27216 contents.file_write (out_file);
27217 cu_list.file_write (out_file);
27218 types_cu_list.file_write (out_file);
27219 addr_vec.file_write (out_file);
27220 symtab_vec.file_write (out_file);
27221 constant_pool.file_write (out_file);
27222
27223 return total_len;
27224 }
27225
27226 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
27227 static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
27228
27229 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
27230 needed addition to .debug_str section to OUT_FILE_STR. Return how
27231 many bytes were expected to be written into OUT_FILE. */
27232
27233 static size_t
27234 write_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
27235 FILE *out_file, FILE *out_file_str)
27236 {
27237 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (dwarf2_per_objfile);
27238 struct objfile *objfile = dwarf2_per_objfile->objfile;
27239 const enum bfd_endian dwarf5_byte_order
27240 = gdbarch_byte_order (get_objfile_arch (objfile));
27241
27242 /* The CU list is already sorted, so we don't need to do additional
27243 work here. Also, the debug_types entries do not appear in
27244 all_comp_units, but only in their own hash table. */
27245 data_buf cu_list;
27246 debug_names nametable (dwarf2_per_objfile, dwarf5_is_dwarf64,
27247 dwarf5_byte_order);
27248 std::unordered_set<partial_symbol *>
27249 psyms_seen (psyms_seen_size (dwarf2_per_objfile));
27250 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27251 {
27252 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
27253 partial_symtab *psymtab = per_cu->v.psymtab;
27254
27255 /* CU of a shared file from 'dwz -m' may be unused by this main
27256 file. It may be referenced from a local scope but in such
27257 case it does not need to be present in .debug_names. */
27258 if (psymtab == NULL)
27259 continue;
27260
27261 if (psymtab->user == NULL)
27262 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
27263
27264 cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
27265 to_underlying (per_cu->sect_off));
27266 }
27267
27268 /* Write out the .debug_type entries, if any. */
27269 data_buf types_cu_list;
27270 if (dwarf2_per_objfile->signatured_types)
27271 {
27272 debug_names::write_one_signatured_type_data sig_data (nametable,
27273 signatured_type_index_data (types_cu_list, psyms_seen));
27274
27275 sig_data.info.objfile = objfile;
27276 /* It is used only for gdb_index. */
27277 sig_data.info.symtab = nullptr;
27278 sig_data.info.cu_index = 0;
27279 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
27280 debug_names::write_one_signatured_type,
27281 &sig_data);
27282 }
27283
27284 nametable.build ();
27285
27286 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
27287
27288 const offset_type bytes_of_header
27289 = ((dwarf5_is_dwarf64 ? 12 : 4)
27290 + 2 + 2 + 7 * 4
27291 + sizeof (dwarf5_gdb_augmentation));
27292 size_t expected_bytes = 0;
27293 expected_bytes += bytes_of_header;
27294 expected_bytes += cu_list.size ();
27295 expected_bytes += types_cu_list.size ();
27296 expected_bytes += nametable.bytes ();
27297 data_buf header;
27298
27299 if (!dwarf5_is_dwarf64)
27300 {
27301 const uint64_t size64 = expected_bytes - 4;
27302 gdb_assert (size64 < 0xfffffff0);
27303 header.append_uint (4, dwarf5_byte_order, size64);
27304 }
27305 else
27306 {
27307 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
27308 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
27309 }
27310
27311 /* The version number. */
27312 header.append_uint (2, dwarf5_byte_order, 5);
27313
27314 /* Padding. */
27315 header.append_uint (2, dwarf5_byte_order, 0);
27316
27317 /* comp_unit_count - The number of CUs in the CU list. */
27318 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
27319
27320 /* local_type_unit_count - The number of TUs in the local TU
27321 list. */
27322 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
27323
27324 /* foreign_type_unit_count - The number of TUs in the foreign TU
27325 list. */
27326 header.append_uint (4, dwarf5_byte_order, 0);
27327
27328 /* bucket_count - The number of hash buckets in the hash lookup
27329 table. */
27330 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
27331
27332 /* name_count - The number of unique names in the index. */
27333 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
27334
27335 /* abbrev_table_size - The size in bytes of the abbreviations
27336 table. */
27337 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
27338
27339 /* augmentation_string_size - The size in bytes of the augmentation
27340 string. This value is rounded up to a multiple of 4. */
27341 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
27342 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
27343 header.append_data (dwarf5_gdb_augmentation);
27344
27345 gdb_assert (header.size () == bytes_of_header);
27346
27347 header.file_write (out_file);
27348 cu_list.file_write (out_file);
27349 types_cu_list.file_write (out_file);
27350 nametable.file_write (out_file, out_file_str);
27351
27352 return expected_bytes;
27353 }
27354
27355 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
27356 position is at the end of the file. */
27357
27358 static void
27359 assert_file_size (FILE *file, const char *filename, size_t expected_size)
27360 {
27361 const auto file_size = ftell (file);
27362 if (file_size == -1)
27363 error (_("Can't get `%s' size"), filename);
27364 gdb_assert (file_size == expected_size);
27365 }
27366
27367 /* Create an index file for OBJFILE in the directory DIR. */
27368
27369 static void
27370 write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
27371 const char *dir,
27372 dw_index_kind index_kind)
27373 {
27374 struct objfile *objfile = dwarf2_per_objfile->objfile;
27375
27376 if (dwarf2_per_objfile->using_index)
27377 error (_("Cannot use an index to create the index"));
27378
27379 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
27380 error (_("Cannot make an index when the file has multiple .debug_types sections"));
27381
27382 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
27383 return;
27384
27385 struct stat st;
27386 if (stat (objfile_name (objfile), &st) < 0)
27387 perror_with_name (objfile_name (objfile));
27388
27389 std::string filename (std::string (dir) + SLASH_STRING
27390 + lbasename (objfile_name (objfile))
27391 + (index_kind == dw_index_kind::DEBUG_NAMES
27392 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
27393
27394 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
27395 if (!out_file)
27396 error (_("Can't open `%s' for writing"), filename.c_str ());
27397
27398 /* Order matters here; we want FILE to be closed before FILENAME is
27399 unlinked, because on MS-Windows one cannot delete a file that is
27400 still open. (Don't call anything here that might throw until
27401 file_closer is created.) */
27402 gdb::unlinker unlink_file (filename.c_str ());
27403 gdb_file_up close_out_file (out_file);
27404
27405 if (index_kind == dw_index_kind::DEBUG_NAMES)
27406 {
27407 std::string filename_str (std::string (dir) + SLASH_STRING
27408 + lbasename (objfile_name (objfile))
27409 + DEBUG_STR_SUFFIX);
27410 FILE *out_file_str
27411 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
27412 if (!out_file_str)
27413 error (_("Can't open `%s' for writing"), filename_str.c_str ());
27414 gdb::unlinker unlink_file_str (filename_str.c_str ());
27415 gdb_file_up close_out_file_str (out_file_str);
27416
27417 const size_t total_len
27418 = write_debug_names (dwarf2_per_objfile, out_file, out_file_str);
27419 assert_file_size (out_file, filename.c_str (), total_len);
27420
27421 /* We want to keep the file .debug_str file too. */
27422 unlink_file_str.keep ();
27423 }
27424 else
27425 {
27426 const size_t total_len
27427 = write_gdbindex (dwarf2_per_objfile, out_file);
27428 assert_file_size (out_file, filename.c_str (), total_len);
27429 }
27430
27431 /* We want to keep the file. */
27432 unlink_file.keep ();
27433 }
27434
27435 /* Implementation of the `save gdb-index' command.
27436
27437 Note that the .gdb_index file format used by this command is
27438 documented in the GDB manual. Any changes here must be documented
27439 there. */
27440
27441 static void
27442 save_gdb_index_command (const char *arg, int from_tty)
27443 {
27444 struct objfile *objfile;
27445 const char dwarf5space[] = "-dwarf-5 ";
27446 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
27447
27448 if (!arg)
27449 arg = "";
27450
27451 arg = skip_spaces (arg);
27452 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
27453 {
27454 index_kind = dw_index_kind::DEBUG_NAMES;
27455 arg += strlen (dwarf5space);
27456 arg = skip_spaces (arg);
27457 }
27458
27459 if (!*arg)
27460 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
27461
27462 ALL_OBJFILES (objfile)
27463 {
27464 struct stat st;
27465
27466 /* If the objfile does not correspond to an actual file, skip it. */
27467 if (stat (objfile_name (objfile), &st) < 0)
27468 continue;
27469
27470 struct dwarf2_per_objfile *dwarf2_per_objfile
27471 = get_dwarf2_per_objfile (objfile);
27472
27473 if (dwarf2_per_objfile != NULL)
27474 {
27475 TRY
27476 {
27477 write_psymtabs_to_index (dwarf2_per_objfile, arg, index_kind);
27478 }
27479 CATCH (except, RETURN_MASK_ERROR)
27480 {
27481 exception_fprintf (gdb_stderr, except,
27482 _("Error while writing index for `%s': "),
27483 objfile_name (objfile));
27484 }
27485 END_CATCH
27486 }
27487
27488 }
27489 }
27490
27491 \f
27492
27493 int dwarf_always_disassemble;
27494
27495 static void
27496 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
27497 struct cmd_list_element *c, const char *value)
27498 {
27499 fprintf_filtered (file,
27500 _("Whether to always disassemble "
27501 "DWARF expressions is %s.\n"),
27502 value);
27503 }
27504
27505 static void
27506 show_check_physname (struct ui_file *file, int from_tty,
27507 struct cmd_list_element *c, const char *value)
27508 {
27509 fprintf_filtered (file,
27510 _("Whether to check \"physname\" is %s.\n"),
27511 value);
27512 }
27513
27514 void
27515 _initialize_dwarf2_read (void)
27516 {
27517 struct cmd_list_element *c;
27518
27519 dwarf2_objfile_data_key = register_objfile_data ();
27520
27521 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
27522 Set DWARF specific variables.\n\
27523 Configure DWARF variables such as the cache size"),
27524 &set_dwarf_cmdlist, "maintenance set dwarf ",
27525 0/*allow-unknown*/, &maintenance_set_cmdlist);
27526
27527 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
27528 Show DWARF specific variables\n\
27529 Show DWARF variables such as the cache size"),
27530 &show_dwarf_cmdlist, "maintenance show dwarf ",
27531 0/*allow-unknown*/, &maintenance_show_cmdlist);
27532
27533 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
27534 &dwarf_max_cache_age, _("\
27535 Set the upper bound on the age of cached DWARF compilation units."), _("\
27536 Show the upper bound on the age of cached DWARF compilation units."), _("\
27537 A higher limit means that cached compilation units will be stored\n\
27538 in memory longer, and more total memory will be used. Zero disables\n\
27539 caching, which can slow down startup."),
27540 NULL,
27541 show_dwarf_max_cache_age,
27542 &set_dwarf_cmdlist,
27543 &show_dwarf_cmdlist);
27544
27545 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
27546 &dwarf_always_disassemble, _("\
27547 Set whether `info address' always disassembles DWARF expressions."), _("\
27548 Show whether `info address' always disassembles DWARF expressions."), _("\
27549 When enabled, DWARF expressions are always printed in an assembly-like\n\
27550 syntax. When disabled, expressions will be printed in a more\n\
27551 conversational style, when possible."),
27552 NULL,
27553 show_dwarf_always_disassemble,
27554 &set_dwarf_cmdlist,
27555 &show_dwarf_cmdlist);
27556
27557 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27558 Set debugging of the DWARF reader."), _("\
27559 Show debugging of the DWARF reader."), _("\
27560 When enabled (non-zero), debugging messages are printed during DWARF\n\
27561 reading and symtab expansion. A value of 1 (one) provides basic\n\
27562 information. A value greater than 1 provides more verbose information."),
27563 NULL,
27564 NULL,
27565 &setdebuglist, &showdebuglist);
27566
27567 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27568 Set debugging of the DWARF DIE reader."), _("\
27569 Show debugging of the DWARF DIE reader."), _("\
27570 When enabled (non-zero), DIEs are dumped after they are read in.\n\
27571 The value is the maximum depth to print."),
27572 NULL,
27573 NULL,
27574 &setdebuglist, &showdebuglist);
27575
27576 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27577 Set debugging of the dwarf line reader."), _("\
27578 Show debugging of the dwarf line reader."), _("\
27579 When enabled (non-zero), line number entries are dumped as they are read in.\n\
27580 A value of 1 (one) provides basic information.\n\
27581 A value greater than 1 provides more verbose information."),
27582 NULL,
27583 NULL,
27584 &setdebuglist, &showdebuglist);
27585
27586 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27587 Set cross-checking of \"physname\" code against demangler."), _("\
27588 Show cross-checking of \"physname\" code against demangler."), _("\
27589 When enabled, GDB's internal \"physname\" code is checked against\n\
27590 the demangler."),
27591 NULL, show_check_physname,
27592 &setdebuglist, &showdebuglist);
27593
27594 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27595 no_class, &use_deprecated_index_sections, _("\
27596 Set whether to use deprecated gdb_index sections."), _("\
27597 Show whether to use deprecated gdb_index sections."), _("\
27598 When enabled, deprecated .gdb_index sections are used anyway.\n\
27599 Normally they are ignored either because of a missing feature or\n\
27600 performance issue.\n\
27601 Warning: This option must be enabled before gdb reads the file."),
27602 NULL,
27603 NULL,
27604 &setlist, &showlist);
27605
27606 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
27607 _("\
27608 Save a gdb-index file.\n\
27609 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27610 \n\
27611 No options create one file with .gdb-index extension for pre-DWARF-5\n\
27612 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27613 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
27614 &save_cmdlist);
27615 set_cmd_completer (c, filename_completer);
27616
27617 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27618 &dwarf2_locexpr_funcs);
27619 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27620 &dwarf2_loclist_funcs);
27621
27622 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27623 &dwarf2_block_frame_base_locexpr_funcs);
27624 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27625 &dwarf2_block_frame_base_loclist_funcs);
27626
27627 #if GDB_SELF_TEST
27628 selftests::register_test ("dw2_expand_symtabs_matching",
27629 selftests::dw2_expand_symtabs_matching::run_test);
27630 #endif
27631 }
This page took 0.564637 seconds and 5 git commands to generate.