Class-fy partial_die_info
[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
90 /* When == 1, print basic high level tracing messages.
91 When > 1, be more verbose.
92 This is in contrast to the low level DIE reading of dwarf_die_debug. */
93 static unsigned int dwarf_read_debug = 0;
94
95 /* When non-zero, dump DIEs after they are read in. */
96 static unsigned int dwarf_die_debug = 0;
97
98 /* When non-zero, dump line number entries as they are read in. */
99 static unsigned int dwarf_line_debug = 0;
100
101 /* When non-zero, cross-check physname against demangler. */
102 static int check_physname = 0;
103
104 /* When non-zero, do not reject deprecated .gdb_index sections. */
105 static int use_deprecated_index_sections = 0;
106
107 static const struct objfile_data *dwarf2_objfile_data_key;
108
109 /* The "aclass" indices for various kinds of computed DWARF symbols. */
110
111 static int dwarf2_locexpr_index;
112 static int dwarf2_loclist_index;
113 static int dwarf2_locexpr_block_index;
114 static int dwarf2_loclist_block_index;
115
116 /* A descriptor for dwarf sections.
117
118 S.ASECTION, SIZE are typically initialized when the objfile is first
119 scanned. BUFFER, READIN are filled in later when the section is read.
120 If the section contained compressed data then SIZE is updated to record
121 the uncompressed size of the section.
122
123 DWP file format V2 introduces a wrinkle that is easiest to handle by
124 creating the concept of virtual sections contained within a real section.
125 In DWP V2 the sections of the input DWO files are concatenated together
126 into one section, but section offsets are kept relative to the original
127 input section.
128 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
129 the real section this "virtual" section is contained in, and BUFFER,SIZE
130 describe the virtual section. */
131
132 struct dwarf2_section_info
133 {
134 union
135 {
136 /* If this is a real section, the bfd section. */
137 asection *section;
138 /* If this is a virtual section, pointer to the containing ("real")
139 section. */
140 struct dwarf2_section_info *containing_section;
141 } s;
142 /* Pointer to section data, only valid if readin. */
143 const gdb_byte *buffer;
144 /* The size of the section, real or virtual. */
145 bfd_size_type size;
146 /* If this is a virtual section, the offset in the real section.
147 Only valid if is_virtual. */
148 bfd_size_type virtual_offset;
149 /* True if we have tried to read this section. */
150 char readin;
151 /* True if this is a virtual section, False otherwise.
152 This specifies which of s.section and s.containing_section to use. */
153 char is_virtual;
154 };
155
156 typedef struct dwarf2_section_info dwarf2_section_info_def;
157 DEF_VEC_O (dwarf2_section_info_def);
158
159 /* All offsets in the index are of this type. It must be
160 architecture-independent. */
161 typedef uint32_t offset_type;
162
163 DEF_VEC_I (offset_type);
164
165 /* Ensure only legit values are used. */
166 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
167 do { \
168 gdb_assert ((unsigned int) (value) <= 1); \
169 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
170 } while (0)
171
172 /* Ensure only legit values are used. */
173 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
174 do { \
175 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
176 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
177 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
178 } while (0)
179
180 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
181 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
182 do { \
183 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
184 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
185 } while (0)
186
187 #if WORDS_BIGENDIAN
188
189 /* Convert VALUE between big- and little-endian. */
190
191 static offset_type
192 byte_swap (offset_type value)
193 {
194 offset_type result;
195
196 result = (value & 0xff) << 24;
197 result |= (value & 0xff00) << 8;
198 result |= (value & 0xff0000) >> 8;
199 result |= (value & 0xff000000) >> 24;
200 return result;
201 }
202
203 #define MAYBE_SWAP(V) byte_swap (V)
204
205 #else
206 #define MAYBE_SWAP(V) static_cast<offset_type> (V)
207 #endif /* WORDS_BIGENDIAN */
208
209 /* An index into a (C++) symbol name component in a symbol name as
210 recorded in the mapped_index's symbol table. For each C++ symbol
211 in the symbol table, we record one entry for the start of each
212 component in the symbol in a table of name components, and then
213 sort the table, in order to be able to binary search symbol names,
214 ignoring leading namespaces, both completion and regular look up.
215 For example, for symbol "A::B::C", we'll have an entry that points
216 to "A::B::C", another that points to "B::C", and another for "C".
217 Note that function symbols in GDB index have no parameter
218 information, just the function/method names. You can convert a
219 name_component to a "const char *" using the
220 'mapped_index::symbol_name_at(offset_type)' method. */
221
222 struct name_component
223 {
224 /* Offset in the symbol name where the component starts. Stored as
225 a (32-bit) offset instead of a pointer to save memory and improve
226 locality on 64-bit architectures. */
227 offset_type name_offset;
228
229 /* The symbol's index in the symbol and constant pool tables of a
230 mapped_index. */
231 offset_type idx;
232 };
233
234 /* Base class containing bits shared by both .gdb_index and
235 .debug_name indexes. */
236
237 struct mapped_index_base
238 {
239 /* The name_component table (a sorted vector). See name_component's
240 description above. */
241 std::vector<name_component> name_components;
242
243 /* How NAME_COMPONENTS is sorted. */
244 enum case_sensitivity name_components_casing;
245
246 /* Return the number of names in the symbol table. */
247 virtual size_t symbol_name_count () const = 0;
248
249 /* Get the name of the symbol at IDX in the symbol table. */
250 virtual const char *symbol_name_at (offset_type idx) const = 0;
251
252 /* Return whether the name at IDX in the symbol table should be
253 ignored. */
254 virtual bool symbol_name_slot_invalid (offset_type idx) const
255 {
256 return false;
257 }
258
259 /* Build the symbol name component sorted vector, if we haven't
260 yet. */
261 void build_name_components ();
262
263 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
264 possible matches for LN_NO_PARAMS in the name component
265 vector. */
266 std::pair<std::vector<name_component>::const_iterator,
267 std::vector<name_component>::const_iterator>
268 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
269
270 /* Prevent deleting/destroying via a base class pointer. */
271 protected:
272 ~mapped_index_base() = default;
273 };
274
275 /* A description of the mapped index. The file format is described in
276 a comment by the code that writes the index. */
277 struct mapped_index final : public mapped_index_base
278 {
279 /* A slot/bucket in the symbol table hash. */
280 struct symbol_table_slot
281 {
282 const offset_type name;
283 const offset_type vec;
284 };
285
286 /* Index data format version. */
287 int version;
288
289 /* The total length of the buffer. */
290 off_t total_size;
291
292 /* The address table data. */
293 gdb::array_view<const gdb_byte> address_table;
294
295 /* The symbol table, implemented as a hash table. */
296 gdb::array_view<symbol_table_slot> symbol_table;
297
298 /* A pointer to the constant pool. */
299 const char *constant_pool;
300
301 bool symbol_name_slot_invalid (offset_type idx) const override
302 {
303 const auto &bucket = this->symbol_table[idx];
304 return bucket.name == 0 && bucket.vec;
305 }
306
307 /* Convenience method to get at the name of the symbol at IDX in the
308 symbol table. */
309 const char *symbol_name_at (offset_type idx) const override
310 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
311
312 size_t symbol_name_count () const override
313 { return this->symbol_table.size (); }
314 };
315
316 /* A description of the mapped .debug_names.
317 Uninitialized map has CU_COUNT 0. */
318 struct mapped_debug_names final : public mapped_index_base
319 {
320 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
321 : dwarf2_per_objfile (dwarf2_per_objfile_)
322 {}
323
324 struct dwarf2_per_objfile *dwarf2_per_objfile;
325 bfd_endian dwarf5_byte_order;
326 bool dwarf5_is_dwarf64;
327 bool augmentation_is_gdb;
328 uint8_t offset_size;
329 uint32_t cu_count = 0;
330 uint32_t tu_count, bucket_count, name_count;
331 const gdb_byte *cu_table_reordered, *tu_table_reordered;
332 const uint32_t *bucket_table_reordered, *hash_table_reordered;
333 const gdb_byte *name_table_string_offs_reordered;
334 const gdb_byte *name_table_entry_offs_reordered;
335 const gdb_byte *entry_pool;
336
337 struct index_val
338 {
339 ULONGEST dwarf_tag;
340 struct attr
341 {
342 /* Attribute name DW_IDX_*. */
343 ULONGEST dw_idx;
344
345 /* Attribute form DW_FORM_*. */
346 ULONGEST form;
347
348 /* Value if FORM is DW_FORM_implicit_const. */
349 LONGEST implicit_const;
350 };
351 std::vector<attr> attr_vec;
352 };
353
354 std::unordered_map<ULONGEST, index_val> abbrev_map;
355
356 const char *namei_to_name (uint32_t namei) const;
357
358 /* Implementation of the mapped_index_base virtual interface, for
359 the name_components cache. */
360
361 const char *symbol_name_at (offset_type idx) const override
362 { return namei_to_name (idx); }
363
364 size_t symbol_name_count () const override
365 { return this->name_count; }
366 };
367
368 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
369 DEF_VEC_P (dwarf2_per_cu_ptr);
370
371 struct tu_stats
372 {
373 int nr_uniq_abbrev_tables;
374 int nr_symtabs;
375 int nr_symtab_sharers;
376 int nr_stmt_less_type_units;
377 int nr_all_type_units_reallocs;
378 };
379
380 /* Collection of data recorded per objfile.
381 This hangs off of dwarf2_objfile_data_key. */
382
383 struct dwarf2_per_objfile : public allocate_on_obstack
384 {
385 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
386 dwarf2 section names, or is NULL if the standard ELF names are
387 used. */
388 dwarf2_per_objfile (struct objfile *objfile,
389 const dwarf2_debug_sections *names);
390
391 ~dwarf2_per_objfile ();
392
393 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
394
395 /* Free all cached compilation units. */
396 void free_cached_comp_units ();
397 private:
398 /* This function is mapped across the sections and remembers the
399 offset and size of each of the debugging sections we are
400 interested in. */
401 void locate_sections (bfd *abfd, asection *sectp,
402 const dwarf2_debug_sections &names);
403
404 public:
405 dwarf2_section_info info {};
406 dwarf2_section_info abbrev {};
407 dwarf2_section_info line {};
408 dwarf2_section_info loc {};
409 dwarf2_section_info loclists {};
410 dwarf2_section_info macinfo {};
411 dwarf2_section_info macro {};
412 dwarf2_section_info str {};
413 dwarf2_section_info line_str {};
414 dwarf2_section_info ranges {};
415 dwarf2_section_info rnglists {};
416 dwarf2_section_info addr {};
417 dwarf2_section_info frame {};
418 dwarf2_section_info eh_frame {};
419 dwarf2_section_info gdb_index {};
420 dwarf2_section_info debug_names {};
421 dwarf2_section_info debug_aranges {};
422
423 VEC (dwarf2_section_info_def) *types = NULL;
424
425 /* Back link. */
426 struct objfile *objfile = NULL;
427
428 /* Table of all the compilation units. This is used to locate
429 the target compilation unit of a particular reference. */
430 struct dwarf2_per_cu_data **all_comp_units = NULL;
431
432 /* The number of compilation units in ALL_COMP_UNITS. */
433 int n_comp_units = 0;
434
435 /* The number of .debug_types-related CUs. */
436 int n_type_units = 0;
437
438 /* The number of elements allocated in all_type_units.
439 If there are skeleton-less TUs, we add them to all_type_units lazily. */
440 int n_allocated_type_units = 0;
441
442 /* The .debug_types-related CUs (TUs).
443 This is stored in malloc space because we may realloc it. */
444 struct signatured_type **all_type_units = NULL;
445
446 /* Table of struct type_unit_group objects.
447 The hash key is the DW_AT_stmt_list value. */
448 htab_t type_unit_groups {};
449
450 /* A table mapping .debug_types signatures to its signatured_type entry.
451 This is NULL if the .debug_types section hasn't been read in yet. */
452 htab_t signatured_types {};
453
454 /* Type unit statistics, to see how well the scaling improvements
455 are doing. */
456 struct tu_stats tu_stats {};
457
458 /* A chain of compilation units that are currently read in, so that
459 they can be freed later. */
460 dwarf2_per_cu_data *read_in_chain = NULL;
461
462 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
463 This is NULL if the table hasn't been allocated yet. */
464 htab_t dwo_files {};
465
466 /* True if we've checked for whether there is a DWP file. */
467 bool dwp_checked = false;
468
469 /* The DWP file if there is one, or NULL. */
470 struct dwp_file *dwp_file = NULL;
471
472 /* The shared '.dwz' file, if one exists. This is used when the
473 original data was compressed using 'dwz -m'. */
474 struct dwz_file *dwz_file = NULL;
475
476 /* A flag indicating whether this objfile has a section loaded at a
477 VMA of 0. */
478 bool has_section_at_zero = false;
479
480 /* True if we are using the mapped index,
481 or we are faking it for OBJF_READNOW's sake. */
482 bool using_index = false;
483
484 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
485 mapped_index *index_table = NULL;
486
487 /* The mapped index, or NULL if .debug_names is missing or not being used. */
488 std::unique_ptr<mapped_debug_names> debug_names_table;
489
490 /* When using index_table, this keeps track of all quick_file_names entries.
491 TUs typically share line table entries with a CU, so we maintain a
492 separate table of all line table entries to support the sharing.
493 Note that while there can be way more TUs than CUs, we've already
494 sorted all the TUs into "type unit groups", grouped by their
495 DW_AT_stmt_list value. Therefore the only sharing done here is with a
496 CU and its associated TU group if there is one. */
497 htab_t quick_file_names_table {};
498
499 /* Set during partial symbol reading, to prevent queueing of full
500 symbols. */
501 bool reading_partial_symbols = false;
502
503 /* Table mapping type DIEs to their struct type *.
504 This is NULL if not allocated yet.
505 The mapping is done via (CU/TU + DIE offset) -> type. */
506 htab_t die_type_hash {};
507
508 /* The CUs we recently read. */
509 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
510
511 /* Table containing line_header indexed by offset and offset_in_dwz. */
512 htab_t line_header_hash {};
513
514 /* Table containing all filenames. This is an optional because the
515 table is lazily constructed on first access. */
516 gdb::optional<filename_seen_cache> filenames_cache;
517 };
518
519 /* Get the dwarf2_per_objfile associated to OBJFILE. */
520
521 struct dwarf2_per_objfile *
522 get_dwarf2_per_objfile (struct objfile *objfile)
523 {
524 return ((struct dwarf2_per_objfile *)
525 objfile_data (objfile, dwarf2_objfile_data_key));
526 }
527
528 /* Set the dwarf2_per_objfile associated to OBJFILE. */
529
530 void
531 set_dwarf2_per_objfile (struct objfile *objfile,
532 struct dwarf2_per_objfile *dwarf2_per_objfile)
533 {
534 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
535 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
536 }
537
538 /* Default names of the debugging sections. */
539
540 /* Note that if the debugging section has been compressed, it might
541 have a name like .zdebug_info. */
542
543 static const struct dwarf2_debug_sections dwarf2_elf_names =
544 {
545 { ".debug_info", ".zdebug_info" },
546 { ".debug_abbrev", ".zdebug_abbrev" },
547 { ".debug_line", ".zdebug_line" },
548 { ".debug_loc", ".zdebug_loc" },
549 { ".debug_loclists", ".zdebug_loclists" },
550 { ".debug_macinfo", ".zdebug_macinfo" },
551 { ".debug_macro", ".zdebug_macro" },
552 { ".debug_str", ".zdebug_str" },
553 { ".debug_line_str", ".zdebug_line_str" },
554 { ".debug_ranges", ".zdebug_ranges" },
555 { ".debug_rnglists", ".zdebug_rnglists" },
556 { ".debug_types", ".zdebug_types" },
557 { ".debug_addr", ".zdebug_addr" },
558 { ".debug_frame", ".zdebug_frame" },
559 { ".eh_frame", NULL },
560 { ".gdb_index", ".zgdb_index" },
561 { ".debug_names", ".zdebug_names" },
562 { ".debug_aranges", ".zdebug_aranges" },
563 23
564 };
565
566 /* List of DWO/DWP sections. */
567
568 static const struct dwop_section_names
569 {
570 struct dwarf2_section_names abbrev_dwo;
571 struct dwarf2_section_names info_dwo;
572 struct dwarf2_section_names line_dwo;
573 struct dwarf2_section_names loc_dwo;
574 struct dwarf2_section_names loclists_dwo;
575 struct dwarf2_section_names macinfo_dwo;
576 struct dwarf2_section_names macro_dwo;
577 struct dwarf2_section_names str_dwo;
578 struct dwarf2_section_names str_offsets_dwo;
579 struct dwarf2_section_names types_dwo;
580 struct dwarf2_section_names cu_index;
581 struct dwarf2_section_names tu_index;
582 }
583 dwop_section_names =
584 {
585 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
586 { ".debug_info.dwo", ".zdebug_info.dwo" },
587 { ".debug_line.dwo", ".zdebug_line.dwo" },
588 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
589 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
590 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
591 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
592 { ".debug_str.dwo", ".zdebug_str.dwo" },
593 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
594 { ".debug_types.dwo", ".zdebug_types.dwo" },
595 { ".debug_cu_index", ".zdebug_cu_index" },
596 { ".debug_tu_index", ".zdebug_tu_index" },
597 };
598
599 /* local data types */
600
601 /* The data in a compilation unit header, after target2host
602 translation, looks like this. */
603 struct comp_unit_head
604 {
605 unsigned int length;
606 short version;
607 unsigned char addr_size;
608 unsigned char signed_addr_p;
609 sect_offset abbrev_sect_off;
610
611 /* Size of file offsets; either 4 or 8. */
612 unsigned int offset_size;
613
614 /* Size of the length field; either 4 or 12. */
615 unsigned int initial_length_size;
616
617 enum dwarf_unit_type unit_type;
618
619 /* Offset to the first byte of this compilation unit header in the
620 .debug_info section, for resolving relative reference dies. */
621 sect_offset sect_off;
622
623 /* Offset to first die in this cu from the start of the cu.
624 This will be the first byte following the compilation unit header. */
625 cu_offset first_die_cu_offset;
626
627 /* 64-bit signature of this type unit - it is valid only for
628 UNIT_TYPE DW_UT_type. */
629 ULONGEST signature;
630
631 /* For types, offset in the type's DIE of the type defined by this TU. */
632 cu_offset type_cu_offset_in_tu;
633 };
634
635 /* Type used for delaying computation of method physnames.
636 See comments for compute_delayed_physnames. */
637 struct delayed_method_info
638 {
639 /* The type to which the method is attached, i.e., its parent class. */
640 struct type *type;
641
642 /* The index of the method in the type's function fieldlists. */
643 int fnfield_index;
644
645 /* The index of the method in the fieldlist. */
646 int index;
647
648 /* The name of the DIE. */
649 const char *name;
650
651 /* The DIE associated with this method. */
652 struct die_info *die;
653 };
654
655 /* Internal state when decoding a particular compilation unit. */
656 struct dwarf2_cu
657 {
658 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
659 ~dwarf2_cu ();
660
661 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
662
663 /* The header of the compilation unit. */
664 struct comp_unit_head header {};
665
666 /* Base address of this compilation unit. */
667 CORE_ADDR base_address = 0;
668
669 /* Non-zero if base_address has been set. */
670 int base_known = 0;
671
672 /* The language we are debugging. */
673 enum language language = language_unknown;
674 const struct language_defn *language_defn = nullptr;
675
676 const char *producer = nullptr;
677
678 /* The generic symbol table building routines have separate lists for
679 file scope symbols and all all other scopes (local scopes). So
680 we need to select the right one to pass to add_symbol_to_list().
681 We do it by keeping a pointer to the correct list in list_in_scope.
682
683 FIXME: The original dwarf code just treated the file scope as the
684 first local scope, and all other local scopes as nested local
685 scopes, and worked fine. Check to see if we really need to
686 distinguish these in buildsym.c. */
687 struct pending **list_in_scope = nullptr;
688
689 /* Hash table holding all the loaded partial DIEs
690 with partial_die->offset.SECT_OFF as hash. */
691 htab_t partial_dies = nullptr;
692
693 /* Storage for things with the same lifetime as this read-in compilation
694 unit, including partial DIEs. */
695 auto_obstack comp_unit_obstack;
696
697 /* When multiple dwarf2_cu structures are living in memory, this field
698 chains them all together, so that they can be released efficiently.
699 We will probably also want a generation counter so that most-recently-used
700 compilation units are cached... */
701 struct dwarf2_per_cu_data *read_in_chain = nullptr;
702
703 /* Backlink to our per_cu entry. */
704 struct dwarf2_per_cu_data *per_cu;
705
706 /* How many compilation units ago was this CU last referenced? */
707 int last_used = 0;
708
709 /* A hash table of DIE cu_offset for following references with
710 die_info->offset.sect_off as hash. */
711 htab_t die_hash = nullptr;
712
713 /* Full DIEs if read in. */
714 struct die_info *dies = nullptr;
715
716 /* A set of pointers to dwarf2_per_cu_data objects for compilation
717 units referenced by this one. Only set during full symbol processing;
718 partial symbol tables do not have dependencies. */
719 htab_t dependencies = nullptr;
720
721 /* Header data from the line table, during full symbol processing. */
722 struct line_header *line_header = nullptr;
723 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
724 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
725 this is the DW_TAG_compile_unit die for this CU. We'll hold on
726 to the line header as long as this DIE is being processed. See
727 process_die_scope. */
728 die_info *line_header_die_owner = nullptr;
729
730 /* A list of methods which need to have physnames computed
731 after all type information has been read. */
732 std::vector<delayed_method_info> method_list;
733
734 /* To be copied to symtab->call_site_htab. */
735 htab_t call_site_htab = nullptr;
736
737 /* Non-NULL if this CU came from a DWO file.
738 There is an invariant here that is important to remember:
739 Except for attributes copied from the top level DIE in the "main"
740 (or "stub") file in preparation for reading the DWO file
741 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
742 Either there isn't a DWO file (in which case this is NULL and the point
743 is moot), or there is and either we're not going to read it (in which
744 case this is NULL) or there is and we are reading it (in which case this
745 is non-NULL). */
746 struct dwo_unit *dwo_unit = nullptr;
747
748 /* The DW_AT_addr_base attribute if present, zero otherwise
749 (zero is a valid value though).
750 Note this value comes from the Fission stub CU/TU's DIE. */
751 ULONGEST addr_base = 0;
752
753 /* The DW_AT_ranges_base attribute if present, zero otherwise
754 (zero is a valid value though).
755 Note this value comes from the Fission stub CU/TU's DIE.
756 Also note that the value is zero in the non-DWO case so this value can
757 be used without needing to know whether DWO files are in use or not.
758 N.B. This does not apply to DW_AT_ranges appearing in
759 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
760 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
761 DW_AT_ranges_base *would* have to be applied, and we'd have to care
762 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
763 ULONGEST ranges_base = 0;
764
765 /* Mark used when releasing cached dies. */
766 unsigned int mark : 1;
767
768 /* This CU references .debug_loc. See the symtab->locations_valid field.
769 This test is imperfect as there may exist optimized debug code not using
770 any location list and still facing inlining issues if handled as
771 unoptimized code. For a future better test see GCC PR other/32998. */
772 unsigned int has_loclist : 1;
773
774 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
775 if all the producer_is_* fields are valid. This information is cached
776 because profiling CU expansion showed excessive time spent in
777 producer_is_gxx_lt_4_6. */
778 unsigned int checked_producer : 1;
779 unsigned int producer_is_gxx_lt_4_6 : 1;
780 unsigned int producer_is_gcc_lt_4_3 : 1;
781 unsigned int producer_is_icc_lt_14 : 1;
782
783 /* When set, the file that we're processing is known to have
784 debugging info for C++ namespaces. GCC 3.3.x did not produce
785 this information, but later versions do. */
786
787 unsigned int processing_has_namespace_info : 1;
788
789 struct partial_die_info *find_partial_die (sect_offset sect_off);
790 };
791
792 /* Persistent data held for a compilation unit, even when not
793 processing it. We put a pointer to this structure in the
794 read_symtab_private field of the psymtab. */
795
796 struct dwarf2_per_cu_data
797 {
798 /* The start offset and length of this compilation unit.
799 NOTE: Unlike comp_unit_head.length, this length includes
800 initial_length_size.
801 If the DIE refers to a DWO file, this is always of the original die,
802 not the DWO file. */
803 sect_offset sect_off;
804 unsigned int length;
805
806 /* DWARF standard version this data has been read from (such as 4 or 5). */
807 short dwarf_version;
808
809 /* Flag indicating this compilation unit will be read in before
810 any of the current compilation units are processed. */
811 unsigned int queued : 1;
812
813 /* This flag will be set when reading partial DIEs if we need to load
814 absolutely all DIEs for this compilation unit, instead of just the ones
815 we think are interesting. It gets set if we look for a DIE in the
816 hash table and don't find it. */
817 unsigned int load_all_dies : 1;
818
819 /* Non-zero if this CU is from .debug_types.
820 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
821 this is non-zero. */
822 unsigned int is_debug_types : 1;
823
824 /* Non-zero if this CU is from the .dwz file. */
825 unsigned int is_dwz : 1;
826
827 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
828 This flag is only valid if is_debug_types is true.
829 We can't read a CU directly from a DWO file: There are required
830 attributes in the stub. */
831 unsigned int reading_dwo_directly : 1;
832
833 /* Non-zero if the TU has been read.
834 This is used to assist the "Stay in DWO Optimization" for Fission:
835 When reading a DWO, it's faster to read TUs from the DWO instead of
836 fetching them from random other DWOs (due to comdat folding).
837 If the TU has already been read, the optimization is unnecessary
838 (and unwise - we don't want to change where gdb thinks the TU lives
839 "midflight").
840 This flag is only valid if is_debug_types is true. */
841 unsigned int tu_read : 1;
842
843 /* The section this CU/TU lives in.
844 If the DIE refers to a DWO file, this is always the original die,
845 not the DWO file. */
846 struct dwarf2_section_info *section;
847
848 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
849 of the CU cache it gets reset to NULL again. This is left as NULL for
850 dummy CUs (a CU header, but nothing else). */
851 struct dwarf2_cu *cu;
852
853 /* The corresponding dwarf2_per_objfile. */
854 struct dwarf2_per_objfile *dwarf2_per_objfile;
855
856 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
857 is active. Otherwise, the 'psymtab' field is active. */
858 union
859 {
860 /* The partial symbol table associated with this compilation unit,
861 or NULL for unread partial units. */
862 struct partial_symtab *psymtab;
863
864 /* Data needed by the "quick" functions. */
865 struct dwarf2_per_cu_quick_data *quick;
866 } v;
867
868 /* The CUs we import using DW_TAG_imported_unit. This is filled in
869 while reading psymtabs, used to compute the psymtab dependencies,
870 and then cleared. Then it is filled in again while reading full
871 symbols, and only deleted when the objfile is destroyed.
872
873 This is also used to work around a difference between the way gold
874 generates .gdb_index version <=7 and the way gdb does. Arguably this
875 is a gold bug. For symbols coming from TUs, gold records in the index
876 the CU that includes the TU instead of the TU itself. This breaks
877 dw2_lookup_symbol: It assumes that if the index says symbol X lives
878 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
879 will find X. Alas TUs live in their own symtab, so after expanding CU Y
880 we need to look in TU Z to find X. Fortunately, this is akin to
881 DW_TAG_imported_unit, so we just use the same mechanism: For
882 .gdb_index version <=7 this also records the TUs that the CU referred
883 to. Concurrently with this change gdb was modified to emit version 8
884 indices so we only pay a price for gold generated indices.
885 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
886 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
887 };
888
889 /* Entry in the signatured_types hash table. */
890
891 struct signatured_type
892 {
893 /* The "per_cu" object of this type.
894 This struct is used iff per_cu.is_debug_types.
895 N.B.: This is the first member so that it's easy to convert pointers
896 between them. */
897 struct dwarf2_per_cu_data per_cu;
898
899 /* The type's signature. */
900 ULONGEST signature;
901
902 /* Offset in the TU of the type's DIE, as read from the TU header.
903 If this TU is a DWO stub and the definition lives in a DWO file
904 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
905 cu_offset type_offset_in_tu;
906
907 /* Offset in the section of the type's DIE.
908 If the definition lives in a DWO file, this is the offset in the
909 .debug_types.dwo section.
910 The value is zero until the actual value is known.
911 Zero is otherwise not a valid section offset. */
912 sect_offset type_offset_in_section;
913
914 /* Type units are grouped by their DW_AT_stmt_list entry so that they
915 can share them. This points to the containing symtab. */
916 struct type_unit_group *type_unit_group;
917
918 /* The type.
919 The first time we encounter this type we fully read it in and install it
920 in the symbol tables. Subsequent times we only need the type. */
921 struct type *type;
922
923 /* Containing DWO unit.
924 This field is valid iff per_cu.reading_dwo_directly. */
925 struct dwo_unit *dwo_unit;
926 };
927
928 typedef struct signatured_type *sig_type_ptr;
929 DEF_VEC_P (sig_type_ptr);
930
931 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
932 This includes type_unit_group and quick_file_names. */
933
934 struct stmt_list_hash
935 {
936 /* The DWO unit this table is from or NULL if there is none. */
937 struct dwo_unit *dwo_unit;
938
939 /* Offset in .debug_line or .debug_line.dwo. */
940 sect_offset line_sect_off;
941 };
942
943 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
944 an object of this type. */
945
946 struct type_unit_group
947 {
948 /* dwarf2read.c's main "handle" on a TU symtab.
949 To simplify things we create an artificial CU that "includes" all the
950 type units using this stmt_list so that the rest of the code still has
951 a "per_cu" handle on the symtab.
952 This PER_CU is recognized by having no section. */
953 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
954 struct dwarf2_per_cu_data per_cu;
955
956 /* The TUs that share this DW_AT_stmt_list entry.
957 This is added to while parsing type units to build partial symtabs,
958 and is deleted afterwards and not used again. */
959 VEC (sig_type_ptr) *tus;
960
961 /* The compunit symtab.
962 Type units in a group needn't all be defined in the same source file,
963 so we create an essentially anonymous symtab as the compunit symtab. */
964 struct compunit_symtab *compunit_symtab;
965
966 /* The data used to construct the hash key. */
967 struct stmt_list_hash hash;
968
969 /* The number of symtabs from the line header.
970 The value here must match line_header.num_file_names. */
971 unsigned int num_symtabs;
972
973 /* The symbol tables for this TU (obtained from the files listed in
974 DW_AT_stmt_list).
975 WARNING: The order of entries here must match the order of entries
976 in the line header. After the first TU using this type_unit_group, the
977 line header for the subsequent TUs is recreated from this. This is done
978 because we need to use the same symtabs for each TU using the same
979 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
980 there's no guarantee the line header doesn't have duplicate entries. */
981 struct symtab **symtabs;
982 };
983
984 /* These sections are what may appear in a (real or virtual) DWO file. */
985
986 struct dwo_sections
987 {
988 struct dwarf2_section_info abbrev;
989 struct dwarf2_section_info line;
990 struct dwarf2_section_info loc;
991 struct dwarf2_section_info loclists;
992 struct dwarf2_section_info macinfo;
993 struct dwarf2_section_info macro;
994 struct dwarf2_section_info str;
995 struct dwarf2_section_info str_offsets;
996 /* In the case of a virtual DWO file, these two are unused. */
997 struct dwarf2_section_info info;
998 VEC (dwarf2_section_info_def) *types;
999 };
1000
1001 /* CUs/TUs in DWP/DWO files. */
1002
1003 struct dwo_unit
1004 {
1005 /* Backlink to the containing struct dwo_file. */
1006 struct dwo_file *dwo_file;
1007
1008 /* The "id" that distinguishes this CU/TU.
1009 .debug_info calls this "dwo_id", .debug_types calls this "signature".
1010 Since signatures came first, we stick with it for consistency. */
1011 ULONGEST signature;
1012
1013 /* The section this CU/TU lives in, in the DWO file. */
1014 struct dwarf2_section_info *section;
1015
1016 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
1017 sect_offset sect_off;
1018 unsigned int length;
1019
1020 /* For types, offset in the type's DIE of the type defined by this TU. */
1021 cu_offset type_offset_in_tu;
1022 };
1023
1024 /* include/dwarf2.h defines the DWP section codes.
1025 It defines a max value but it doesn't define a min value, which we
1026 use for error checking, so provide one. */
1027
1028 enum dwp_v2_section_ids
1029 {
1030 DW_SECT_MIN = 1
1031 };
1032
1033 /* Data for one DWO file.
1034
1035 This includes virtual DWO files (a virtual DWO file is a DWO file as it
1036 appears in a DWP file). DWP files don't really have DWO files per se -
1037 comdat folding of types "loses" the DWO file they came from, and from
1038 a high level view DWP files appear to contain a mass of random types.
1039 However, to maintain consistency with the non-DWP case we pretend DWP
1040 files contain virtual DWO files, and we assign each TU with one virtual
1041 DWO file (generally based on the line and abbrev section offsets -
1042 a heuristic that seems to work in practice). */
1043
1044 struct dwo_file
1045 {
1046 /* The DW_AT_GNU_dwo_name attribute.
1047 For virtual DWO files the name is constructed from the section offsets
1048 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
1049 from related CU+TUs. */
1050 const char *dwo_name;
1051
1052 /* The DW_AT_comp_dir attribute. */
1053 const char *comp_dir;
1054
1055 /* The bfd, when the file is open. Otherwise this is NULL.
1056 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
1057 bfd *dbfd;
1058
1059 /* The sections that make up this DWO file.
1060 Remember that for virtual DWO files in DWP V2, these are virtual
1061 sections (for lack of a better name). */
1062 struct dwo_sections sections;
1063
1064 /* The CUs in the file.
1065 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
1066 an extension to handle LLVM's Link Time Optimization output (where
1067 multiple source files may be compiled into a single object/dwo pair). */
1068 htab_t cus;
1069
1070 /* Table of TUs in the file.
1071 Each element is a struct dwo_unit. */
1072 htab_t tus;
1073 };
1074
1075 /* These sections are what may appear in a DWP file. */
1076
1077 struct dwp_sections
1078 {
1079 /* These are used by both DWP version 1 and 2. */
1080 struct dwarf2_section_info str;
1081 struct dwarf2_section_info cu_index;
1082 struct dwarf2_section_info tu_index;
1083
1084 /* These are only used by DWP version 2 files.
1085 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
1086 sections are referenced by section number, and are not recorded here.
1087 In DWP version 2 there is at most one copy of all these sections, each
1088 section being (effectively) comprised of the concatenation of all of the
1089 individual sections that exist in the version 1 format.
1090 To keep the code simple we treat each of these concatenated pieces as a
1091 section itself (a virtual section?). */
1092 struct dwarf2_section_info abbrev;
1093 struct dwarf2_section_info info;
1094 struct dwarf2_section_info line;
1095 struct dwarf2_section_info loc;
1096 struct dwarf2_section_info macinfo;
1097 struct dwarf2_section_info macro;
1098 struct dwarf2_section_info str_offsets;
1099 struct dwarf2_section_info types;
1100 };
1101
1102 /* These sections are what may appear in a virtual DWO file in DWP version 1.
1103 A virtual DWO file is a DWO file as it appears in a DWP file. */
1104
1105 struct virtual_v1_dwo_sections
1106 {
1107 struct dwarf2_section_info abbrev;
1108 struct dwarf2_section_info line;
1109 struct dwarf2_section_info loc;
1110 struct dwarf2_section_info macinfo;
1111 struct dwarf2_section_info macro;
1112 struct dwarf2_section_info str_offsets;
1113 /* Each DWP hash table entry records one CU or one TU.
1114 That is recorded here, and copied to dwo_unit.section. */
1115 struct dwarf2_section_info info_or_types;
1116 };
1117
1118 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1119 In version 2, the sections of the DWO files are concatenated together
1120 and stored in one section of that name. Thus each ELF section contains
1121 several "virtual" sections. */
1122
1123 struct virtual_v2_dwo_sections
1124 {
1125 bfd_size_type abbrev_offset;
1126 bfd_size_type abbrev_size;
1127
1128 bfd_size_type line_offset;
1129 bfd_size_type line_size;
1130
1131 bfd_size_type loc_offset;
1132 bfd_size_type loc_size;
1133
1134 bfd_size_type macinfo_offset;
1135 bfd_size_type macinfo_size;
1136
1137 bfd_size_type macro_offset;
1138 bfd_size_type macro_size;
1139
1140 bfd_size_type str_offsets_offset;
1141 bfd_size_type str_offsets_size;
1142
1143 /* Each DWP hash table entry records one CU or one TU.
1144 That is recorded here, and copied to dwo_unit.section. */
1145 bfd_size_type info_or_types_offset;
1146 bfd_size_type info_or_types_size;
1147 };
1148
1149 /* Contents of DWP hash tables. */
1150
1151 struct dwp_hash_table
1152 {
1153 uint32_t version, nr_columns;
1154 uint32_t nr_units, nr_slots;
1155 const gdb_byte *hash_table, *unit_table;
1156 union
1157 {
1158 struct
1159 {
1160 const gdb_byte *indices;
1161 } v1;
1162 struct
1163 {
1164 /* This is indexed by column number and gives the id of the section
1165 in that column. */
1166 #define MAX_NR_V2_DWO_SECTIONS \
1167 (1 /* .debug_info or .debug_types */ \
1168 + 1 /* .debug_abbrev */ \
1169 + 1 /* .debug_line */ \
1170 + 1 /* .debug_loc */ \
1171 + 1 /* .debug_str_offsets */ \
1172 + 1 /* .debug_macro or .debug_macinfo */)
1173 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1174 const gdb_byte *offsets;
1175 const gdb_byte *sizes;
1176 } v2;
1177 } section_pool;
1178 };
1179
1180 /* Data for one DWP file. */
1181
1182 struct dwp_file
1183 {
1184 /* Name of the file. */
1185 const char *name;
1186
1187 /* File format version. */
1188 int version;
1189
1190 /* The bfd. */
1191 bfd *dbfd;
1192
1193 /* Section info for this file. */
1194 struct dwp_sections sections;
1195
1196 /* Table of CUs in the file. */
1197 const struct dwp_hash_table *cus;
1198
1199 /* Table of TUs in the file. */
1200 const struct dwp_hash_table *tus;
1201
1202 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1203 htab_t loaded_cus;
1204 htab_t loaded_tus;
1205
1206 /* Table to map ELF section numbers to their sections.
1207 This is only needed for the DWP V1 file format. */
1208 unsigned int num_sections;
1209 asection **elf_sections;
1210 };
1211
1212 /* This represents a '.dwz' file. */
1213
1214 struct dwz_file
1215 {
1216 /* A dwz file can only contain a few sections. */
1217 struct dwarf2_section_info abbrev;
1218 struct dwarf2_section_info info;
1219 struct dwarf2_section_info str;
1220 struct dwarf2_section_info line;
1221 struct dwarf2_section_info macro;
1222 struct dwarf2_section_info gdb_index;
1223 struct dwarf2_section_info debug_names;
1224
1225 /* The dwz's BFD. */
1226 bfd *dwz_bfd;
1227 };
1228
1229 /* Struct used to pass misc. parameters to read_die_and_children, et
1230 al. which are used for both .debug_info and .debug_types dies.
1231 All parameters here are unchanging for the life of the call. This
1232 struct exists to abstract away the constant parameters of die reading. */
1233
1234 struct die_reader_specs
1235 {
1236 /* The bfd of die_section. */
1237 bfd* abfd;
1238
1239 /* The CU of the DIE we are parsing. */
1240 struct dwarf2_cu *cu;
1241
1242 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1243 struct dwo_file *dwo_file;
1244
1245 /* The section the die comes from.
1246 This is either .debug_info or .debug_types, or the .dwo variants. */
1247 struct dwarf2_section_info *die_section;
1248
1249 /* die_section->buffer. */
1250 const gdb_byte *buffer;
1251
1252 /* The end of the buffer. */
1253 const gdb_byte *buffer_end;
1254
1255 /* The value of the DW_AT_comp_dir attribute. */
1256 const char *comp_dir;
1257
1258 /* The abbreviation table to use when reading the DIEs. */
1259 struct abbrev_table *abbrev_table;
1260 };
1261
1262 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1263 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1264 const gdb_byte *info_ptr,
1265 struct die_info *comp_unit_die,
1266 int has_children,
1267 void *data);
1268
1269 /* A 1-based directory index. This is a strong typedef to prevent
1270 accidentally using a directory index as a 0-based index into an
1271 array/vector. */
1272 enum class dir_index : unsigned int {};
1273
1274 /* Likewise, a 1-based file name index. */
1275 enum class file_name_index : unsigned int {};
1276
1277 struct file_entry
1278 {
1279 file_entry () = default;
1280
1281 file_entry (const char *name_, dir_index d_index_,
1282 unsigned int mod_time_, unsigned int length_)
1283 : name (name_),
1284 d_index (d_index_),
1285 mod_time (mod_time_),
1286 length (length_)
1287 {}
1288
1289 /* Return the include directory at D_INDEX stored in LH. Returns
1290 NULL if D_INDEX is out of bounds. */
1291 const char *include_dir (const line_header *lh) const;
1292
1293 /* The file name. Note this is an observing pointer. The memory is
1294 owned by debug_line_buffer. */
1295 const char *name {};
1296
1297 /* The directory index (1-based). */
1298 dir_index d_index {};
1299
1300 unsigned int mod_time {};
1301
1302 unsigned int length {};
1303
1304 /* True if referenced by the Line Number Program. */
1305 bool included_p {};
1306
1307 /* The associated symbol table, if any. */
1308 struct symtab *symtab {};
1309 };
1310
1311 /* The line number information for a compilation unit (found in the
1312 .debug_line section) begins with a "statement program header",
1313 which contains the following information. */
1314 struct line_header
1315 {
1316 line_header ()
1317 : offset_in_dwz {}
1318 {}
1319
1320 /* Add an entry to the include directory table. */
1321 void add_include_dir (const char *include_dir);
1322
1323 /* Add an entry to the file name table. */
1324 void add_file_name (const char *name, dir_index d_index,
1325 unsigned int mod_time, unsigned int length);
1326
1327 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1328 is out of bounds. */
1329 const char *include_dir_at (dir_index index) const
1330 {
1331 /* Convert directory index number (1-based) to vector index
1332 (0-based). */
1333 size_t vec_index = to_underlying (index) - 1;
1334
1335 if (vec_index >= include_dirs.size ())
1336 return NULL;
1337 return include_dirs[vec_index];
1338 }
1339
1340 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1341 is out of bounds. */
1342 file_entry *file_name_at (file_name_index index)
1343 {
1344 /* Convert file name index number (1-based) to vector index
1345 (0-based). */
1346 size_t vec_index = to_underlying (index) - 1;
1347
1348 if (vec_index >= file_names.size ())
1349 return NULL;
1350 return &file_names[vec_index];
1351 }
1352
1353 /* Const version of the above. */
1354 const file_entry *file_name_at (unsigned int index) const
1355 {
1356 if (index >= file_names.size ())
1357 return NULL;
1358 return &file_names[index];
1359 }
1360
1361 /* Offset of line number information in .debug_line section. */
1362 sect_offset sect_off {};
1363
1364 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1365 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1366
1367 unsigned int total_length {};
1368 unsigned short version {};
1369 unsigned int header_length {};
1370 unsigned char minimum_instruction_length {};
1371 unsigned char maximum_ops_per_instruction {};
1372 unsigned char default_is_stmt {};
1373 int line_base {};
1374 unsigned char line_range {};
1375 unsigned char opcode_base {};
1376
1377 /* standard_opcode_lengths[i] is the number of operands for the
1378 standard opcode whose value is i. This means that
1379 standard_opcode_lengths[0] is unused, and the last meaningful
1380 element is standard_opcode_lengths[opcode_base - 1]. */
1381 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1382
1383 /* The include_directories table. Note these are observing
1384 pointers. The memory is owned by debug_line_buffer. */
1385 std::vector<const char *> include_dirs;
1386
1387 /* The file_names table. */
1388 std::vector<file_entry> file_names;
1389
1390 /* The start and end of the statement program following this
1391 header. These point into dwarf2_per_objfile->line_buffer. */
1392 const gdb_byte *statement_program_start {}, *statement_program_end {};
1393 };
1394
1395 typedef std::unique_ptr<line_header> line_header_up;
1396
1397 const char *
1398 file_entry::include_dir (const line_header *lh) const
1399 {
1400 return lh->include_dir_at (d_index);
1401 }
1402
1403 /* When we construct a partial symbol table entry we only
1404 need this much information. */
1405 struct partial_die_info : public allocate_on_obstack
1406 {
1407 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1408
1409 /* Disable assign but still keep copy ctor, which is needed
1410 load_partial_dies. */
1411 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1412
1413 /* Offset of this DIE. */
1414 const sect_offset sect_off;
1415
1416 /* DWARF-2 tag for this DIE. */
1417 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1418
1419 /* Assorted flags describing the data found in this DIE. */
1420 const unsigned int has_children : 1;
1421
1422 unsigned int is_external : 1;
1423 unsigned int is_declaration : 1;
1424 unsigned int has_type : 1;
1425 unsigned int has_specification : 1;
1426 unsigned int has_pc_info : 1;
1427 unsigned int may_be_inlined : 1;
1428
1429 /* This DIE has been marked DW_AT_main_subprogram. */
1430 unsigned int main_subprogram : 1;
1431
1432 /* Flag set if the SCOPE field of this structure has been
1433 computed. */
1434 unsigned int scope_set : 1;
1435
1436 /* Flag set if the DIE has a byte_size attribute. */
1437 unsigned int has_byte_size : 1;
1438
1439 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1440 unsigned int has_const_value : 1;
1441
1442 /* Flag set if any of the DIE's children are template arguments. */
1443 unsigned int has_template_arguments : 1;
1444
1445 /* Flag set if fixup_partial_die has been called on this die. */
1446 unsigned int fixup_called : 1;
1447
1448 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1449 unsigned int is_dwz : 1;
1450
1451 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1452 unsigned int spec_is_dwz : 1;
1453
1454 /* The name of this DIE. Normally the value of DW_AT_name, but
1455 sometimes a default name for unnamed DIEs. */
1456 const char *name = nullptr;
1457
1458 /* The linkage name, if present. */
1459 const char *linkage_name = nullptr;
1460
1461 /* The scope to prepend to our children. This is generally
1462 allocated on the comp_unit_obstack, so will disappear
1463 when this compilation unit leaves the cache. */
1464 const char *scope = nullptr;
1465
1466 /* Some data associated with the partial DIE. The tag determines
1467 which field is live. */
1468 union
1469 {
1470 /* The location description associated with this DIE, if any. */
1471 struct dwarf_block *locdesc;
1472 /* The offset of an import, for DW_TAG_imported_unit. */
1473 sect_offset sect_off;
1474 } d {};
1475
1476 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1477 CORE_ADDR lowpc = 0;
1478 CORE_ADDR highpc = 0;
1479
1480 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1481 DW_AT_sibling, if any. */
1482 /* NOTE: This member isn't strictly necessary, read_partial_die could
1483 return DW_AT_sibling values to its caller load_partial_dies. */
1484 const gdb_byte *sibling = nullptr;
1485
1486 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1487 DW_AT_specification (or DW_AT_abstract_origin or
1488 DW_AT_extension). */
1489 sect_offset spec_offset {};
1490
1491 /* Pointers to this DIE's parent, first child, and next sibling,
1492 if any. */
1493 struct partial_die_info *die_parent = nullptr;
1494 struct partial_die_info *die_child = nullptr;
1495 struct partial_die_info *die_sibling = nullptr;
1496
1497 friend struct partial_die_info *
1498 dwarf2_cu::find_partial_die (sect_offset sect_off);
1499
1500 private:
1501 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1502 partial_die_info (sect_offset sect_off)
1503 : partial_die_info (sect_off, DW_TAG_padding, 0)
1504 {
1505 }
1506
1507 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1508 int has_children_)
1509 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1510 {
1511 is_external = 0;
1512 is_declaration = 0;
1513 has_type = 0;
1514 has_specification = 0;
1515 has_pc_info = 0;
1516 may_be_inlined = 0;
1517 main_subprogram = 0;
1518 scope_set = 0;
1519 has_byte_size = 0;
1520 has_const_value = 0;
1521 has_template_arguments = 0;
1522 fixup_called = 0;
1523 is_dwz = 0;
1524 spec_is_dwz = 0;
1525 }
1526 };
1527
1528 /* This data structure holds the information of an abbrev. */
1529 struct abbrev_info
1530 {
1531 unsigned int number; /* number identifying abbrev */
1532 enum dwarf_tag tag; /* dwarf tag */
1533 unsigned short has_children; /* boolean */
1534 unsigned short num_attrs; /* number of attributes */
1535 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1536 struct abbrev_info *next; /* next in chain */
1537 };
1538
1539 struct attr_abbrev
1540 {
1541 ENUM_BITFIELD(dwarf_attribute) name : 16;
1542 ENUM_BITFIELD(dwarf_form) form : 16;
1543
1544 /* It is valid only if FORM is DW_FORM_implicit_const. */
1545 LONGEST implicit_const;
1546 };
1547
1548 /* Size of abbrev_table.abbrev_hash_table. */
1549 #define ABBREV_HASH_SIZE 121
1550
1551 /* Top level data structure to contain an abbreviation table. */
1552
1553 struct abbrev_table
1554 {
1555 explicit abbrev_table (sect_offset off)
1556 : sect_off (off)
1557 {
1558 m_abbrevs =
1559 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1560 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1561 }
1562
1563 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1564
1565 /* Allocate space for a struct abbrev_info object in
1566 ABBREV_TABLE. */
1567 struct abbrev_info *alloc_abbrev ();
1568
1569 /* Add an abbreviation to the table. */
1570 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1571
1572 /* Look up an abbrev in the table.
1573 Returns NULL if the abbrev is not found. */
1574
1575 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1576
1577
1578 /* Where the abbrev table came from.
1579 This is used as a sanity check when the table is used. */
1580 const sect_offset sect_off;
1581
1582 /* Storage for the abbrev table. */
1583 auto_obstack abbrev_obstack;
1584
1585 private:
1586
1587 /* Hash table of abbrevs.
1588 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1589 It could be statically allocated, but the previous code didn't so we
1590 don't either. */
1591 struct abbrev_info **m_abbrevs;
1592 };
1593
1594 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1595
1596 /* Attributes have a name and a value. */
1597 struct attribute
1598 {
1599 ENUM_BITFIELD(dwarf_attribute) name : 16;
1600 ENUM_BITFIELD(dwarf_form) form : 15;
1601
1602 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1603 field should be in u.str (existing only for DW_STRING) but it is kept
1604 here for better struct attribute alignment. */
1605 unsigned int string_is_canonical : 1;
1606
1607 union
1608 {
1609 const char *str;
1610 struct dwarf_block *blk;
1611 ULONGEST unsnd;
1612 LONGEST snd;
1613 CORE_ADDR addr;
1614 ULONGEST signature;
1615 }
1616 u;
1617 };
1618
1619 /* This data structure holds a complete die structure. */
1620 struct die_info
1621 {
1622 /* DWARF-2 tag for this DIE. */
1623 ENUM_BITFIELD(dwarf_tag) tag : 16;
1624
1625 /* Number of attributes */
1626 unsigned char num_attrs;
1627
1628 /* True if we're presently building the full type name for the
1629 type derived from this DIE. */
1630 unsigned char building_fullname : 1;
1631
1632 /* True if this die is in process. PR 16581. */
1633 unsigned char in_process : 1;
1634
1635 /* Abbrev number */
1636 unsigned int abbrev;
1637
1638 /* Offset in .debug_info or .debug_types section. */
1639 sect_offset sect_off;
1640
1641 /* The dies in a compilation unit form an n-ary tree. PARENT
1642 points to this die's parent; CHILD points to the first child of
1643 this node; and all the children of a given node are chained
1644 together via their SIBLING fields. */
1645 struct die_info *child; /* Its first child, if any. */
1646 struct die_info *sibling; /* Its next sibling, if any. */
1647 struct die_info *parent; /* Its parent, if any. */
1648
1649 /* An array of attributes, with NUM_ATTRS elements. There may be
1650 zero, but it's not common and zero-sized arrays are not
1651 sufficiently portable C. */
1652 struct attribute attrs[1];
1653 };
1654
1655 /* Get at parts of an attribute structure. */
1656
1657 #define DW_STRING(attr) ((attr)->u.str)
1658 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1659 #define DW_UNSND(attr) ((attr)->u.unsnd)
1660 #define DW_BLOCK(attr) ((attr)->u.blk)
1661 #define DW_SND(attr) ((attr)->u.snd)
1662 #define DW_ADDR(attr) ((attr)->u.addr)
1663 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1664
1665 /* Blocks are a bunch of untyped bytes. */
1666 struct dwarf_block
1667 {
1668 size_t size;
1669
1670 /* Valid only if SIZE is not zero. */
1671 const gdb_byte *data;
1672 };
1673
1674 #ifndef ATTR_ALLOC_CHUNK
1675 #define ATTR_ALLOC_CHUNK 4
1676 #endif
1677
1678 /* Allocate fields for structs, unions and enums in this size. */
1679 #ifndef DW_FIELD_ALLOC_CHUNK
1680 #define DW_FIELD_ALLOC_CHUNK 4
1681 #endif
1682
1683 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1684 but this would require a corresponding change in unpack_field_as_long
1685 and friends. */
1686 static int bits_per_byte = 8;
1687
1688 struct nextfield
1689 {
1690 struct nextfield *next;
1691 int accessibility;
1692 int virtuality;
1693 struct field field;
1694 };
1695
1696 struct nextfnfield
1697 {
1698 struct nextfnfield *next;
1699 struct fn_field fnfield;
1700 };
1701
1702 struct fnfieldlist
1703 {
1704 const char *name;
1705 int length;
1706 struct nextfnfield *head;
1707 };
1708
1709 struct decl_field_list
1710 {
1711 struct decl_field field;
1712 struct decl_field_list *next;
1713 };
1714
1715 /* The routines that read and process dies for a C struct or C++ class
1716 pass lists of data member fields and lists of member function fields
1717 in an instance of a field_info structure, as defined below. */
1718 struct field_info
1719 {
1720 /* List of data member and baseclasses fields. */
1721 struct nextfield *fields, *baseclasses;
1722
1723 /* Number of fields (including baseclasses). */
1724 int nfields;
1725
1726 /* Number of baseclasses. */
1727 int nbaseclasses;
1728
1729 /* Set if the accesibility of one of the fields is not public. */
1730 int non_public_fields;
1731
1732 /* Member function fieldlist array, contains name of possibly overloaded
1733 member function, number of overloaded member functions and a pointer
1734 to the head of the member function field chain. */
1735 struct fnfieldlist *fnfieldlists;
1736
1737 /* Number of entries in the fnfieldlists array. */
1738 int nfnfields;
1739
1740 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1741 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1742 struct decl_field_list *typedef_field_list;
1743 unsigned typedef_field_list_count;
1744
1745 /* Nested types defined by this class and the number of elements in this
1746 list. */
1747 struct decl_field_list *nested_types_list;
1748 unsigned nested_types_list_count;
1749 };
1750
1751 /* One item on the queue of compilation units to read in full symbols
1752 for. */
1753 struct dwarf2_queue_item
1754 {
1755 struct dwarf2_per_cu_data *per_cu;
1756 enum language pretend_language;
1757 struct dwarf2_queue_item *next;
1758 };
1759
1760 /* The current queue. */
1761 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1762
1763 /* Loaded secondary compilation units are kept in memory until they
1764 have not been referenced for the processing of this many
1765 compilation units. Set this to zero to disable caching. Cache
1766 sizes of up to at least twenty will improve startup time for
1767 typical inter-CU-reference binaries, at an obvious memory cost. */
1768 static int dwarf_max_cache_age = 5;
1769 static void
1770 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1771 struct cmd_list_element *c, const char *value)
1772 {
1773 fprintf_filtered (file, _("The upper bound on the age of cached "
1774 "DWARF compilation units is %s.\n"),
1775 value);
1776 }
1777 \f
1778 /* local function prototypes */
1779
1780 static const char *get_section_name (const struct dwarf2_section_info *);
1781
1782 static const char *get_section_file_name (const struct dwarf2_section_info *);
1783
1784 static void dwarf2_find_base_address (struct die_info *die,
1785 struct dwarf2_cu *cu);
1786
1787 static struct partial_symtab *create_partial_symtab
1788 (struct dwarf2_per_cu_data *per_cu, const char *name);
1789
1790 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1791 const gdb_byte *info_ptr,
1792 struct die_info *type_unit_die,
1793 int has_children, void *data);
1794
1795 static void dwarf2_build_psymtabs_hard
1796 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1797
1798 static void scan_partial_symbols (struct partial_die_info *,
1799 CORE_ADDR *, CORE_ADDR *,
1800 int, struct dwarf2_cu *);
1801
1802 static void add_partial_symbol (struct partial_die_info *,
1803 struct dwarf2_cu *);
1804
1805 static void add_partial_namespace (struct partial_die_info *pdi,
1806 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1807 int set_addrmap, struct dwarf2_cu *cu);
1808
1809 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1810 CORE_ADDR *highpc, int set_addrmap,
1811 struct dwarf2_cu *cu);
1812
1813 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1814 struct dwarf2_cu *cu);
1815
1816 static void add_partial_subprogram (struct partial_die_info *pdi,
1817 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1818 int need_pc, struct dwarf2_cu *cu);
1819
1820 static void dwarf2_read_symtab (struct partial_symtab *,
1821 struct objfile *);
1822
1823 static void psymtab_to_symtab_1 (struct partial_symtab *);
1824
1825 static abbrev_table_up abbrev_table_read_table
1826 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1827 sect_offset);
1828
1829 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1830
1831 static struct partial_die_info *load_partial_dies
1832 (const struct die_reader_specs *, const gdb_byte *, int);
1833
1834 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1835 struct partial_die_info *,
1836 const struct abbrev_info &,
1837 unsigned int,
1838 const gdb_byte *);
1839
1840 static struct partial_die_info *find_partial_die (sect_offset, int,
1841 struct dwarf2_cu *);
1842
1843 static void fixup_partial_die (struct partial_die_info *,
1844 struct dwarf2_cu *);
1845
1846 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1847 struct attribute *, struct attr_abbrev *,
1848 const gdb_byte *);
1849
1850 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1851
1852 static int read_1_signed_byte (bfd *, const gdb_byte *);
1853
1854 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1855
1856 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1857
1858 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1859
1860 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1861 unsigned int *);
1862
1863 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1864
1865 static LONGEST read_checked_initial_length_and_offset
1866 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1867 unsigned int *, unsigned int *);
1868
1869 static LONGEST read_offset (bfd *, const gdb_byte *,
1870 const struct comp_unit_head *,
1871 unsigned int *);
1872
1873 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1874
1875 static sect_offset read_abbrev_offset
1876 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1877 struct dwarf2_section_info *, sect_offset);
1878
1879 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1880
1881 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1882
1883 static const char *read_indirect_string
1884 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1885 const struct comp_unit_head *, unsigned int *);
1886
1887 static const char *read_indirect_line_string
1888 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1889 const struct comp_unit_head *, unsigned int *);
1890
1891 static const char *read_indirect_string_at_offset
1892 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1893 LONGEST str_offset);
1894
1895 static const char *read_indirect_string_from_dwz
1896 (struct objfile *objfile, struct dwz_file *, LONGEST);
1897
1898 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1899
1900 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1901 const gdb_byte *,
1902 unsigned int *);
1903
1904 static const char *read_str_index (const struct die_reader_specs *reader,
1905 ULONGEST str_index);
1906
1907 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1908
1909 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1910 struct dwarf2_cu *);
1911
1912 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1913 unsigned int);
1914
1915 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1916 struct dwarf2_cu *cu);
1917
1918 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1919 struct dwarf2_cu *cu);
1920
1921 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1922
1923 static struct die_info *die_specification (struct die_info *die,
1924 struct dwarf2_cu **);
1925
1926 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1927 struct dwarf2_cu *cu);
1928
1929 static void dwarf_decode_lines (struct line_header *, const char *,
1930 struct dwarf2_cu *, struct partial_symtab *,
1931 CORE_ADDR, int decode_mapping);
1932
1933 static void dwarf2_start_subfile (const char *, const char *);
1934
1935 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1936 const char *, const char *,
1937 CORE_ADDR);
1938
1939 static struct symbol *new_symbol (struct die_info *, struct type *,
1940 struct dwarf2_cu *, struct symbol * = NULL);
1941
1942 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1943 struct dwarf2_cu *);
1944
1945 static void dwarf2_const_value_attr (const struct attribute *attr,
1946 struct type *type,
1947 const char *name,
1948 struct obstack *obstack,
1949 struct dwarf2_cu *cu, LONGEST *value,
1950 const gdb_byte **bytes,
1951 struct dwarf2_locexpr_baton **baton);
1952
1953 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1954
1955 static int need_gnat_info (struct dwarf2_cu *);
1956
1957 static struct type *die_descriptive_type (struct die_info *,
1958 struct dwarf2_cu *);
1959
1960 static void set_descriptive_type (struct type *, struct die_info *,
1961 struct dwarf2_cu *);
1962
1963 static struct type *die_containing_type (struct die_info *,
1964 struct dwarf2_cu *);
1965
1966 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1967 struct dwarf2_cu *);
1968
1969 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1970
1971 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1972
1973 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1974
1975 static char *typename_concat (struct obstack *obs, const char *prefix,
1976 const char *suffix, int physname,
1977 struct dwarf2_cu *cu);
1978
1979 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1980
1981 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1982
1983 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1984
1985 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1986
1987 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1988
1989 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1990
1991 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1992 struct dwarf2_cu *, struct partial_symtab *);
1993
1994 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1995 values. Keep the items ordered with increasing constraints compliance. */
1996 enum pc_bounds_kind
1997 {
1998 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1999 PC_BOUNDS_NOT_PRESENT,
2000
2001 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
2002 were present but they do not form a valid range of PC addresses. */
2003 PC_BOUNDS_INVALID,
2004
2005 /* Discontiguous range was found - that is DW_AT_ranges was found. */
2006 PC_BOUNDS_RANGES,
2007
2008 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
2009 PC_BOUNDS_HIGH_LOW,
2010 };
2011
2012 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
2013 CORE_ADDR *, CORE_ADDR *,
2014 struct dwarf2_cu *,
2015 struct partial_symtab *);
2016
2017 static void get_scope_pc_bounds (struct die_info *,
2018 CORE_ADDR *, CORE_ADDR *,
2019 struct dwarf2_cu *);
2020
2021 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
2022 CORE_ADDR, struct dwarf2_cu *);
2023
2024 static void dwarf2_add_field (struct field_info *, struct die_info *,
2025 struct dwarf2_cu *);
2026
2027 static void dwarf2_attach_fields_to_type (struct field_info *,
2028 struct type *, struct dwarf2_cu *);
2029
2030 static void dwarf2_add_member_fn (struct field_info *,
2031 struct die_info *, struct type *,
2032 struct dwarf2_cu *);
2033
2034 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
2035 struct type *,
2036 struct dwarf2_cu *);
2037
2038 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
2039
2040 static void read_common_block (struct die_info *, struct dwarf2_cu *);
2041
2042 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
2043
2044 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
2045
2046 static struct using_direct **using_directives (enum language);
2047
2048 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
2049
2050 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
2051
2052 static struct type *read_module_type (struct die_info *die,
2053 struct dwarf2_cu *cu);
2054
2055 static const char *namespace_name (struct die_info *die,
2056 int *is_anonymous, struct dwarf2_cu *);
2057
2058 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
2059
2060 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
2061
2062 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
2063 struct dwarf2_cu *);
2064
2065 static struct die_info *read_die_and_siblings_1
2066 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
2067 struct die_info *);
2068
2069 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
2070 const gdb_byte *info_ptr,
2071 const gdb_byte **new_info_ptr,
2072 struct die_info *parent);
2073
2074 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
2075 struct die_info **, const gdb_byte *,
2076 int *, int);
2077
2078 static const gdb_byte *read_full_die (const struct die_reader_specs *,
2079 struct die_info **, const gdb_byte *,
2080 int *);
2081
2082 static void process_die (struct die_info *, struct dwarf2_cu *);
2083
2084 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
2085 struct obstack *);
2086
2087 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
2088
2089 static const char *dwarf2_full_name (const char *name,
2090 struct die_info *die,
2091 struct dwarf2_cu *cu);
2092
2093 static const char *dwarf2_physname (const char *name, struct die_info *die,
2094 struct dwarf2_cu *cu);
2095
2096 static struct die_info *dwarf2_extension (struct die_info *die,
2097 struct dwarf2_cu **);
2098
2099 static const char *dwarf_tag_name (unsigned int);
2100
2101 static const char *dwarf_attr_name (unsigned int);
2102
2103 static const char *dwarf_form_name (unsigned int);
2104
2105 static const char *dwarf_bool_name (unsigned int);
2106
2107 static const char *dwarf_type_encoding_name (unsigned int);
2108
2109 static struct die_info *sibling_die (struct die_info *);
2110
2111 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
2112
2113 static void dump_die_for_error (struct die_info *);
2114
2115 static void dump_die_1 (struct ui_file *, int level, int max_level,
2116 struct die_info *);
2117
2118 /*static*/ void dump_die (struct die_info *, int max_level);
2119
2120 static void store_in_ref_table (struct die_info *,
2121 struct dwarf2_cu *);
2122
2123 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
2124
2125 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
2126
2127 static struct die_info *follow_die_ref_or_sig (struct die_info *,
2128 const struct attribute *,
2129 struct dwarf2_cu **);
2130
2131 static struct die_info *follow_die_ref (struct die_info *,
2132 const struct attribute *,
2133 struct dwarf2_cu **);
2134
2135 static struct die_info *follow_die_sig (struct die_info *,
2136 const struct attribute *,
2137 struct dwarf2_cu **);
2138
2139 static struct type *get_signatured_type (struct die_info *, ULONGEST,
2140 struct dwarf2_cu *);
2141
2142 static struct type *get_DW_AT_signature_type (struct die_info *,
2143 const struct attribute *,
2144 struct dwarf2_cu *);
2145
2146 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
2147
2148 static void read_signatured_type (struct signatured_type *);
2149
2150 static int attr_to_dynamic_prop (const struct attribute *attr,
2151 struct die_info *die, struct dwarf2_cu *cu,
2152 struct dynamic_prop *prop);
2153
2154 /* memory allocation interface */
2155
2156 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
2157
2158 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
2159
2160 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2161
2162 static int attr_form_is_block (const struct attribute *);
2163
2164 static int attr_form_is_section_offset (const struct attribute *);
2165
2166 static int attr_form_is_constant (const struct attribute *);
2167
2168 static int attr_form_is_ref (const struct attribute *);
2169
2170 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2171 struct dwarf2_loclist_baton *baton,
2172 const struct attribute *attr);
2173
2174 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
2175 struct symbol *sym,
2176 struct dwarf2_cu *cu,
2177 int is_block);
2178
2179 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2180 const gdb_byte *info_ptr,
2181 struct abbrev_info *abbrev);
2182
2183 static hashval_t partial_die_hash (const void *item);
2184
2185 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2186
2187 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
2188 (sect_offset sect_off, unsigned int offset_in_dwz,
2189 struct dwarf2_per_objfile *dwarf2_per_objfile);
2190
2191 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
2192 struct die_info *comp_unit_die,
2193 enum language pretend_language);
2194
2195 static void free_cached_comp_units (void *);
2196
2197 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2198
2199 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
2200
2201 static struct type *set_die_type (struct die_info *, struct type *,
2202 struct dwarf2_cu *);
2203
2204 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2205
2206 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2207
2208 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2209 enum language);
2210
2211 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2212 enum language);
2213
2214 static void process_full_type_unit (struct dwarf2_per_cu_data *,
2215 enum language);
2216
2217 static void dwarf2_add_dependence (struct dwarf2_cu *,
2218 struct dwarf2_per_cu_data *);
2219
2220 static void dwarf2_mark (struct dwarf2_cu *);
2221
2222 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2223
2224 static struct type *get_die_type_at_offset (sect_offset,
2225 struct dwarf2_per_cu_data *);
2226
2227 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
2228
2229 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2230 enum language pretend_language);
2231
2232 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
2233
2234 /* Class, the destructor of which frees all allocated queue entries. This
2235 will only have work to do if an error was thrown while processing the
2236 dwarf. If no error was thrown then the queue entries should have all
2237 been processed, and freed, as we went along. */
2238
2239 class dwarf2_queue_guard
2240 {
2241 public:
2242 dwarf2_queue_guard () = default;
2243
2244 /* Free any entries remaining on the queue. There should only be
2245 entries left if we hit an error while processing the dwarf. */
2246 ~dwarf2_queue_guard ()
2247 {
2248 struct dwarf2_queue_item *item, *last;
2249
2250 item = dwarf2_queue;
2251 while (item)
2252 {
2253 /* Anything still marked queued is likely to be in an
2254 inconsistent state, so discard it. */
2255 if (item->per_cu->queued)
2256 {
2257 if (item->per_cu->cu != NULL)
2258 free_one_cached_comp_unit (item->per_cu);
2259 item->per_cu->queued = 0;
2260 }
2261
2262 last = item;
2263 item = item->next;
2264 xfree (last);
2265 }
2266
2267 dwarf2_queue = dwarf2_queue_tail = NULL;
2268 }
2269 };
2270
2271 /* The return type of find_file_and_directory. Note, the enclosed
2272 string pointers are only valid while this object is valid. */
2273
2274 struct file_and_directory
2275 {
2276 /* The filename. This is never NULL. */
2277 const char *name;
2278
2279 /* The compilation directory. NULL if not known. If we needed to
2280 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2281 points directly to the DW_AT_comp_dir string attribute owned by
2282 the obstack that owns the DIE. */
2283 const char *comp_dir;
2284
2285 /* If we needed to build a new string for comp_dir, this is what
2286 owns the storage. */
2287 std::string comp_dir_storage;
2288 };
2289
2290 static file_and_directory find_file_and_directory (struct die_info *die,
2291 struct dwarf2_cu *cu);
2292
2293 static char *file_full_name (int file, struct line_header *lh,
2294 const char *comp_dir);
2295
2296 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2297 enum class rcuh_kind { COMPILE, TYPE };
2298
2299 static const gdb_byte *read_and_check_comp_unit_head
2300 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2301 struct comp_unit_head *header,
2302 struct dwarf2_section_info *section,
2303 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2304 rcuh_kind section_kind);
2305
2306 static void init_cutu_and_read_dies
2307 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2308 int use_existing_cu, int keep,
2309 die_reader_func_ftype *die_reader_func, void *data);
2310
2311 static void init_cutu_and_read_dies_simple
2312 (struct dwarf2_per_cu_data *this_cu,
2313 die_reader_func_ftype *die_reader_func, void *data);
2314
2315 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2316
2317 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2318
2319 static struct dwo_unit *lookup_dwo_unit_in_dwp
2320 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2321 struct dwp_file *dwp_file, const char *comp_dir,
2322 ULONGEST signature, int is_debug_types);
2323
2324 static struct dwp_file *get_dwp_file
2325 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2326
2327 static struct dwo_unit *lookup_dwo_comp_unit
2328 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2329
2330 static struct dwo_unit *lookup_dwo_type_unit
2331 (struct signatured_type *, const char *, const char *);
2332
2333 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2334
2335 static void free_dwo_file_cleanup (void *);
2336
2337 struct free_dwo_file_cleanup_data
2338 {
2339 struct dwo_file *dwo_file;
2340 struct dwarf2_per_objfile *dwarf2_per_objfile;
2341 };
2342
2343 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2344
2345 static void check_producer (struct dwarf2_cu *cu);
2346
2347 static void free_line_header_voidp (void *arg);
2348 \f
2349 /* Various complaints about symbol reading that don't abort the process. */
2350
2351 static void
2352 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2353 {
2354 complaint (&symfile_complaints,
2355 _("statement list doesn't fit in .debug_line section"));
2356 }
2357
2358 static void
2359 dwarf2_debug_line_missing_file_complaint (void)
2360 {
2361 complaint (&symfile_complaints,
2362 _(".debug_line section has line data without a file"));
2363 }
2364
2365 static void
2366 dwarf2_debug_line_missing_end_sequence_complaint (void)
2367 {
2368 complaint (&symfile_complaints,
2369 _(".debug_line section has line "
2370 "program sequence without an end"));
2371 }
2372
2373 static void
2374 dwarf2_complex_location_expr_complaint (void)
2375 {
2376 complaint (&symfile_complaints, _("location expression too complex"));
2377 }
2378
2379 static void
2380 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2381 int arg3)
2382 {
2383 complaint (&symfile_complaints,
2384 _("const value length mismatch for '%s', got %d, expected %d"),
2385 arg1, arg2, arg3);
2386 }
2387
2388 static void
2389 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2390 {
2391 complaint (&symfile_complaints,
2392 _("debug info runs off end of %s section"
2393 " [in module %s]"),
2394 get_section_name (section),
2395 get_section_file_name (section));
2396 }
2397
2398 static void
2399 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2400 {
2401 complaint (&symfile_complaints,
2402 _("macro debug info contains a "
2403 "malformed macro definition:\n`%s'"),
2404 arg1);
2405 }
2406
2407 static void
2408 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2409 {
2410 complaint (&symfile_complaints,
2411 _("invalid attribute class or form for '%s' in '%s'"),
2412 arg1, arg2);
2413 }
2414
2415 /* Hash function for line_header_hash. */
2416
2417 static hashval_t
2418 line_header_hash (const struct line_header *ofs)
2419 {
2420 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2421 }
2422
2423 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2424
2425 static hashval_t
2426 line_header_hash_voidp (const void *item)
2427 {
2428 const struct line_header *ofs = (const struct line_header *) item;
2429
2430 return line_header_hash (ofs);
2431 }
2432
2433 /* Equality function for line_header_hash. */
2434
2435 static int
2436 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2437 {
2438 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2439 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2440
2441 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2442 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2443 }
2444
2445 \f
2446
2447 /* Read the given attribute value as an address, taking the attribute's
2448 form into account. */
2449
2450 static CORE_ADDR
2451 attr_value_as_address (struct attribute *attr)
2452 {
2453 CORE_ADDR addr;
2454
2455 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2456 {
2457 /* Aside from a few clearly defined exceptions, attributes that
2458 contain an address must always be in DW_FORM_addr form.
2459 Unfortunately, some compilers happen to be violating this
2460 requirement by encoding addresses using other forms, such
2461 as DW_FORM_data4 for example. For those broken compilers,
2462 we try to do our best, without any guarantee of success,
2463 to interpret the address correctly. It would also be nice
2464 to generate a complaint, but that would require us to maintain
2465 a list of legitimate cases where a non-address form is allowed,
2466 as well as update callers to pass in at least the CU's DWARF
2467 version. This is more overhead than what we're willing to
2468 expand for a pretty rare case. */
2469 addr = DW_UNSND (attr);
2470 }
2471 else
2472 addr = DW_ADDR (attr);
2473
2474 return addr;
2475 }
2476
2477 /* The suffix for an index file. */
2478 #define INDEX4_SUFFIX ".gdb-index"
2479 #define INDEX5_SUFFIX ".debug_names"
2480 #define DEBUG_STR_SUFFIX ".debug_str"
2481
2482 /* See declaration. */
2483
2484 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2485 const dwarf2_debug_sections *names)
2486 : objfile (objfile_)
2487 {
2488 if (names == NULL)
2489 names = &dwarf2_elf_names;
2490
2491 bfd *obfd = objfile->obfd;
2492
2493 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2494 locate_sections (obfd, sec, *names);
2495 }
2496
2497 static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2498
2499 dwarf2_per_objfile::~dwarf2_per_objfile ()
2500 {
2501 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2502 free_cached_comp_units ();
2503
2504 if (quick_file_names_table)
2505 htab_delete (quick_file_names_table);
2506
2507 if (line_header_hash)
2508 htab_delete (line_header_hash);
2509
2510 for (int ix = 0; ix < n_comp_units; ++ix)
2511 VEC_free (dwarf2_per_cu_ptr, all_comp_units[ix]->imported_symtabs);
2512
2513 for (int ix = 0; ix < n_type_units; ++ix)
2514 VEC_free (dwarf2_per_cu_ptr,
2515 all_type_units[ix]->per_cu.imported_symtabs);
2516 xfree (all_type_units);
2517
2518 VEC_free (dwarf2_section_info_def, types);
2519
2520 if (dwo_files != NULL)
2521 free_dwo_files (dwo_files, objfile);
2522 if (dwp_file != NULL)
2523 gdb_bfd_unref (dwp_file->dbfd);
2524
2525 if (dwz_file != NULL && dwz_file->dwz_bfd)
2526 gdb_bfd_unref (dwz_file->dwz_bfd);
2527
2528 if (index_table != NULL)
2529 index_table->~mapped_index ();
2530
2531 /* Everything else should be on the objfile obstack. */
2532 }
2533
2534 /* See declaration. */
2535
2536 void
2537 dwarf2_per_objfile::free_cached_comp_units ()
2538 {
2539 dwarf2_per_cu_data *per_cu = read_in_chain;
2540 dwarf2_per_cu_data **last_chain = &read_in_chain;
2541 while (per_cu != NULL)
2542 {
2543 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2544
2545 delete per_cu->cu;
2546 *last_chain = next_cu;
2547 per_cu = next_cu;
2548 }
2549 }
2550
2551 /* Try to locate the sections we need for DWARF 2 debugging
2552 information and return true if we have enough to do something.
2553 NAMES points to the dwarf2 section names, or is NULL if the standard
2554 ELF names are used. */
2555
2556 int
2557 dwarf2_has_info (struct objfile *objfile,
2558 const struct dwarf2_debug_sections *names)
2559 {
2560 if (objfile->flags & OBJF_READNEVER)
2561 return 0;
2562
2563 struct dwarf2_per_objfile *dwarf2_per_objfile
2564 = get_dwarf2_per_objfile (objfile);
2565
2566 if (dwarf2_per_objfile == NULL)
2567 {
2568 /* Initialize per-objfile state. */
2569 dwarf2_per_objfile
2570 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2571 names);
2572 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
2573 }
2574 return (!dwarf2_per_objfile->info.is_virtual
2575 && dwarf2_per_objfile->info.s.section != NULL
2576 && !dwarf2_per_objfile->abbrev.is_virtual
2577 && dwarf2_per_objfile->abbrev.s.section != NULL);
2578 }
2579
2580 /* Return the containing section of virtual section SECTION. */
2581
2582 static struct dwarf2_section_info *
2583 get_containing_section (const struct dwarf2_section_info *section)
2584 {
2585 gdb_assert (section->is_virtual);
2586 return section->s.containing_section;
2587 }
2588
2589 /* Return the bfd owner of SECTION. */
2590
2591 static struct bfd *
2592 get_section_bfd_owner (const struct dwarf2_section_info *section)
2593 {
2594 if (section->is_virtual)
2595 {
2596 section = get_containing_section (section);
2597 gdb_assert (!section->is_virtual);
2598 }
2599 return section->s.section->owner;
2600 }
2601
2602 /* Return the bfd section of SECTION.
2603 Returns NULL if the section is not present. */
2604
2605 static asection *
2606 get_section_bfd_section (const struct dwarf2_section_info *section)
2607 {
2608 if (section->is_virtual)
2609 {
2610 section = get_containing_section (section);
2611 gdb_assert (!section->is_virtual);
2612 }
2613 return section->s.section;
2614 }
2615
2616 /* Return the name of SECTION. */
2617
2618 static const char *
2619 get_section_name (const struct dwarf2_section_info *section)
2620 {
2621 asection *sectp = get_section_bfd_section (section);
2622
2623 gdb_assert (sectp != NULL);
2624 return bfd_section_name (get_section_bfd_owner (section), sectp);
2625 }
2626
2627 /* Return the name of the file SECTION is in. */
2628
2629 static const char *
2630 get_section_file_name (const struct dwarf2_section_info *section)
2631 {
2632 bfd *abfd = get_section_bfd_owner (section);
2633
2634 return bfd_get_filename (abfd);
2635 }
2636
2637 /* Return the id of SECTION.
2638 Returns 0 if SECTION doesn't exist. */
2639
2640 static int
2641 get_section_id (const struct dwarf2_section_info *section)
2642 {
2643 asection *sectp = get_section_bfd_section (section);
2644
2645 if (sectp == NULL)
2646 return 0;
2647 return sectp->id;
2648 }
2649
2650 /* Return the flags of SECTION.
2651 SECTION (or containing section if this is a virtual section) must exist. */
2652
2653 static int
2654 get_section_flags (const struct dwarf2_section_info *section)
2655 {
2656 asection *sectp = get_section_bfd_section (section);
2657
2658 gdb_assert (sectp != NULL);
2659 return bfd_get_section_flags (sectp->owner, sectp);
2660 }
2661
2662 /* When loading sections, we look either for uncompressed section or for
2663 compressed section names. */
2664
2665 static int
2666 section_is_p (const char *section_name,
2667 const struct dwarf2_section_names *names)
2668 {
2669 if (names->normal != NULL
2670 && strcmp (section_name, names->normal) == 0)
2671 return 1;
2672 if (names->compressed != NULL
2673 && strcmp (section_name, names->compressed) == 0)
2674 return 1;
2675 return 0;
2676 }
2677
2678 /* See declaration. */
2679
2680 void
2681 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2682 const dwarf2_debug_sections &names)
2683 {
2684 flagword aflag = bfd_get_section_flags (abfd, sectp);
2685
2686 if ((aflag & SEC_HAS_CONTENTS) == 0)
2687 {
2688 }
2689 else if (section_is_p (sectp->name, &names.info))
2690 {
2691 this->info.s.section = sectp;
2692 this->info.size = bfd_get_section_size (sectp);
2693 }
2694 else if (section_is_p (sectp->name, &names.abbrev))
2695 {
2696 this->abbrev.s.section = sectp;
2697 this->abbrev.size = bfd_get_section_size (sectp);
2698 }
2699 else if (section_is_p (sectp->name, &names.line))
2700 {
2701 this->line.s.section = sectp;
2702 this->line.size = bfd_get_section_size (sectp);
2703 }
2704 else if (section_is_p (sectp->name, &names.loc))
2705 {
2706 this->loc.s.section = sectp;
2707 this->loc.size = bfd_get_section_size (sectp);
2708 }
2709 else if (section_is_p (sectp->name, &names.loclists))
2710 {
2711 this->loclists.s.section = sectp;
2712 this->loclists.size = bfd_get_section_size (sectp);
2713 }
2714 else if (section_is_p (sectp->name, &names.macinfo))
2715 {
2716 this->macinfo.s.section = sectp;
2717 this->macinfo.size = bfd_get_section_size (sectp);
2718 }
2719 else if (section_is_p (sectp->name, &names.macro))
2720 {
2721 this->macro.s.section = sectp;
2722 this->macro.size = bfd_get_section_size (sectp);
2723 }
2724 else if (section_is_p (sectp->name, &names.str))
2725 {
2726 this->str.s.section = sectp;
2727 this->str.size = bfd_get_section_size (sectp);
2728 }
2729 else if (section_is_p (sectp->name, &names.line_str))
2730 {
2731 this->line_str.s.section = sectp;
2732 this->line_str.size = bfd_get_section_size (sectp);
2733 }
2734 else if (section_is_p (sectp->name, &names.addr))
2735 {
2736 this->addr.s.section = sectp;
2737 this->addr.size = bfd_get_section_size (sectp);
2738 }
2739 else if (section_is_p (sectp->name, &names.frame))
2740 {
2741 this->frame.s.section = sectp;
2742 this->frame.size = bfd_get_section_size (sectp);
2743 }
2744 else if (section_is_p (sectp->name, &names.eh_frame))
2745 {
2746 this->eh_frame.s.section = sectp;
2747 this->eh_frame.size = bfd_get_section_size (sectp);
2748 }
2749 else if (section_is_p (sectp->name, &names.ranges))
2750 {
2751 this->ranges.s.section = sectp;
2752 this->ranges.size = bfd_get_section_size (sectp);
2753 }
2754 else if (section_is_p (sectp->name, &names.rnglists))
2755 {
2756 this->rnglists.s.section = sectp;
2757 this->rnglists.size = bfd_get_section_size (sectp);
2758 }
2759 else if (section_is_p (sectp->name, &names.types))
2760 {
2761 struct dwarf2_section_info type_section;
2762
2763 memset (&type_section, 0, sizeof (type_section));
2764 type_section.s.section = sectp;
2765 type_section.size = bfd_get_section_size (sectp);
2766
2767 VEC_safe_push (dwarf2_section_info_def, this->types,
2768 &type_section);
2769 }
2770 else if (section_is_p (sectp->name, &names.gdb_index))
2771 {
2772 this->gdb_index.s.section = sectp;
2773 this->gdb_index.size = bfd_get_section_size (sectp);
2774 }
2775 else if (section_is_p (sectp->name, &names.debug_names))
2776 {
2777 this->debug_names.s.section = sectp;
2778 this->debug_names.size = bfd_get_section_size (sectp);
2779 }
2780 else if (section_is_p (sectp->name, &names.debug_aranges))
2781 {
2782 this->debug_aranges.s.section = sectp;
2783 this->debug_aranges.size = bfd_get_section_size (sectp);
2784 }
2785
2786 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2787 && bfd_section_vma (abfd, sectp) == 0)
2788 this->has_section_at_zero = true;
2789 }
2790
2791 /* A helper function that decides whether a section is empty,
2792 or not present. */
2793
2794 static int
2795 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2796 {
2797 if (section->is_virtual)
2798 return section->size == 0;
2799 return section->s.section == NULL || section->size == 0;
2800 }
2801
2802 /* Read the contents of the section INFO.
2803 OBJFILE is the main object file, but not necessarily the file where
2804 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2805 of the DWO file.
2806 If the section is compressed, uncompress it before returning. */
2807
2808 static void
2809 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2810 {
2811 asection *sectp;
2812 bfd *abfd;
2813 gdb_byte *buf, *retbuf;
2814
2815 if (info->readin)
2816 return;
2817 info->buffer = NULL;
2818 info->readin = 1;
2819
2820 if (dwarf2_section_empty_p (info))
2821 return;
2822
2823 sectp = get_section_bfd_section (info);
2824
2825 /* If this is a virtual section we need to read in the real one first. */
2826 if (info->is_virtual)
2827 {
2828 struct dwarf2_section_info *containing_section =
2829 get_containing_section (info);
2830
2831 gdb_assert (sectp != NULL);
2832 if ((sectp->flags & SEC_RELOC) != 0)
2833 {
2834 error (_("Dwarf Error: DWP format V2 with relocations is not"
2835 " supported in section %s [in module %s]"),
2836 get_section_name (info), get_section_file_name (info));
2837 }
2838 dwarf2_read_section (objfile, containing_section);
2839 /* Other code should have already caught virtual sections that don't
2840 fit. */
2841 gdb_assert (info->virtual_offset + info->size
2842 <= containing_section->size);
2843 /* If the real section is empty or there was a problem reading the
2844 section we shouldn't get here. */
2845 gdb_assert (containing_section->buffer != NULL);
2846 info->buffer = containing_section->buffer + info->virtual_offset;
2847 return;
2848 }
2849
2850 /* If the section has relocations, we must read it ourselves.
2851 Otherwise we attach it to the BFD. */
2852 if ((sectp->flags & SEC_RELOC) == 0)
2853 {
2854 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2855 return;
2856 }
2857
2858 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2859 info->buffer = buf;
2860
2861 /* When debugging .o files, we may need to apply relocations; see
2862 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2863 We never compress sections in .o files, so we only need to
2864 try this when the section is not compressed. */
2865 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2866 if (retbuf != NULL)
2867 {
2868 info->buffer = retbuf;
2869 return;
2870 }
2871
2872 abfd = get_section_bfd_owner (info);
2873 gdb_assert (abfd != NULL);
2874
2875 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2876 || bfd_bread (buf, info->size, abfd) != info->size)
2877 {
2878 error (_("Dwarf Error: Can't read DWARF data"
2879 " in section %s [in module %s]"),
2880 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2881 }
2882 }
2883
2884 /* A helper function that returns the size of a section in a safe way.
2885 If you are positive that the section has been read before using the
2886 size, then it is safe to refer to the dwarf2_section_info object's
2887 "size" field directly. In other cases, you must call this
2888 function, because for compressed sections the size field is not set
2889 correctly until the section has been read. */
2890
2891 static bfd_size_type
2892 dwarf2_section_size (struct objfile *objfile,
2893 struct dwarf2_section_info *info)
2894 {
2895 if (!info->readin)
2896 dwarf2_read_section (objfile, info);
2897 return info->size;
2898 }
2899
2900 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2901 SECTION_NAME. */
2902
2903 void
2904 dwarf2_get_section_info (struct objfile *objfile,
2905 enum dwarf2_section_enum sect,
2906 asection **sectp, const gdb_byte **bufp,
2907 bfd_size_type *sizep)
2908 {
2909 struct dwarf2_per_objfile *data
2910 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2911 dwarf2_objfile_data_key);
2912 struct dwarf2_section_info *info;
2913
2914 /* We may see an objfile without any DWARF, in which case we just
2915 return nothing. */
2916 if (data == NULL)
2917 {
2918 *sectp = NULL;
2919 *bufp = NULL;
2920 *sizep = 0;
2921 return;
2922 }
2923 switch (sect)
2924 {
2925 case DWARF2_DEBUG_FRAME:
2926 info = &data->frame;
2927 break;
2928 case DWARF2_EH_FRAME:
2929 info = &data->eh_frame;
2930 break;
2931 default:
2932 gdb_assert_not_reached ("unexpected section");
2933 }
2934
2935 dwarf2_read_section (objfile, info);
2936
2937 *sectp = get_section_bfd_section (info);
2938 *bufp = info->buffer;
2939 *sizep = info->size;
2940 }
2941
2942 /* A helper function to find the sections for a .dwz file. */
2943
2944 static void
2945 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2946 {
2947 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2948
2949 /* Note that we only support the standard ELF names, because .dwz
2950 is ELF-only (at the time of writing). */
2951 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2952 {
2953 dwz_file->abbrev.s.section = sectp;
2954 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2955 }
2956 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2957 {
2958 dwz_file->info.s.section = sectp;
2959 dwz_file->info.size = bfd_get_section_size (sectp);
2960 }
2961 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2962 {
2963 dwz_file->str.s.section = sectp;
2964 dwz_file->str.size = bfd_get_section_size (sectp);
2965 }
2966 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2967 {
2968 dwz_file->line.s.section = sectp;
2969 dwz_file->line.size = bfd_get_section_size (sectp);
2970 }
2971 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2972 {
2973 dwz_file->macro.s.section = sectp;
2974 dwz_file->macro.size = bfd_get_section_size (sectp);
2975 }
2976 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2977 {
2978 dwz_file->gdb_index.s.section = sectp;
2979 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2980 }
2981 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2982 {
2983 dwz_file->debug_names.s.section = sectp;
2984 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2985 }
2986 }
2987
2988 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2989 there is no .gnu_debugaltlink section in the file. Error if there
2990 is such a section but the file cannot be found. */
2991
2992 static struct dwz_file *
2993 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2994 {
2995 const char *filename;
2996 struct dwz_file *result;
2997 bfd_size_type buildid_len_arg;
2998 size_t buildid_len;
2999 bfd_byte *buildid;
3000
3001 if (dwarf2_per_objfile->dwz_file != NULL)
3002 return dwarf2_per_objfile->dwz_file;
3003
3004 bfd_set_error (bfd_error_no_error);
3005 gdb::unique_xmalloc_ptr<char> data
3006 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
3007 &buildid_len_arg, &buildid));
3008 if (data == NULL)
3009 {
3010 if (bfd_get_error () == bfd_error_no_error)
3011 return NULL;
3012 error (_("could not read '.gnu_debugaltlink' section: %s"),
3013 bfd_errmsg (bfd_get_error ()));
3014 }
3015
3016 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
3017
3018 buildid_len = (size_t) buildid_len_arg;
3019
3020 filename = data.get ();
3021
3022 std::string abs_storage;
3023 if (!IS_ABSOLUTE_PATH (filename))
3024 {
3025 gdb::unique_xmalloc_ptr<char> abs
3026 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
3027
3028 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
3029 filename = abs_storage.c_str ();
3030 }
3031
3032 /* First try the file name given in the section. If that doesn't
3033 work, try to use the build-id instead. */
3034 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
3035 if (dwz_bfd != NULL)
3036 {
3037 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
3038 dwz_bfd.release ();
3039 }
3040
3041 if (dwz_bfd == NULL)
3042 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
3043
3044 if (dwz_bfd == NULL)
3045 error (_("could not find '.gnu_debugaltlink' file for %s"),
3046 objfile_name (dwarf2_per_objfile->objfile));
3047
3048 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
3049 struct dwz_file);
3050 result->dwz_bfd = dwz_bfd.release ();
3051
3052 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
3053
3054 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
3055 dwarf2_per_objfile->dwz_file = result;
3056 return result;
3057 }
3058 \f
3059 /* DWARF quick_symbols_functions support. */
3060
3061 /* TUs can share .debug_line entries, and there can be a lot more TUs than
3062 unique line tables, so we maintain a separate table of all .debug_line
3063 derived entries to support the sharing.
3064 All the quick functions need is the list of file names. We discard the
3065 line_header when we're done and don't need to record it here. */
3066 struct quick_file_names
3067 {
3068 /* The data used to construct the hash key. */
3069 struct stmt_list_hash hash;
3070
3071 /* The number of entries in file_names, real_names. */
3072 unsigned int num_file_names;
3073
3074 /* The file names from the line table, after being run through
3075 file_full_name. */
3076 const char **file_names;
3077
3078 /* The file names from the line table after being run through
3079 gdb_realpath. These are computed lazily. */
3080 const char **real_names;
3081 };
3082
3083 /* When using the index (and thus not using psymtabs), each CU has an
3084 object of this type. This is used to hold information needed by
3085 the various "quick" methods. */
3086 struct dwarf2_per_cu_quick_data
3087 {
3088 /* The file table. This can be NULL if there was no file table
3089 or it's currently not read in.
3090 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
3091 struct quick_file_names *file_names;
3092
3093 /* The corresponding symbol table. This is NULL if symbols for this
3094 CU have not yet been read. */
3095 struct compunit_symtab *compunit_symtab;
3096
3097 /* A temporary mark bit used when iterating over all CUs in
3098 expand_symtabs_matching. */
3099 unsigned int mark : 1;
3100
3101 /* True if we've tried to read the file table and found there isn't one.
3102 There will be no point in trying to read it again next time. */
3103 unsigned int no_file_data : 1;
3104 };
3105
3106 /* Utility hash function for a stmt_list_hash. */
3107
3108 static hashval_t
3109 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
3110 {
3111 hashval_t v = 0;
3112
3113 if (stmt_list_hash->dwo_unit != NULL)
3114 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
3115 v += to_underlying (stmt_list_hash->line_sect_off);
3116 return v;
3117 }
3118
3119 /* Utility equality function for a stmt_list_hash. */
3120
3121 static int
3122 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
3123 const struct stmt_list_hash *rhs)
3124 {
3125 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
3126 return 0;
3127 if (lhs->dwo_unit != NULL
3128 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
3129 return 0;
3130
3131 return lhs->line_sect_off == rhs->line_sect_off;
3132 }
3133
3134 /* Hash function for a quick_file_names. */
3135
3136 static hashval_t
3137 hash_file_name_entry (const void *e)
3138 {
3139 const struct quick_file_names *file_data
3140 = (const struct quick_file_names *) e;
3141
3142 return hash_stmt_list_entry (&file_data->hash);
3143 }
3144
3145 /* Equality function for a quick_file_names. */
3146
3147 static int
3148 eq_file_name_entry (const void *a, const void *b)
3149 {
3150 const struct quick_file_names *ea = (const struct quick_file_names *) a;
3151 const struct quick_file_names *eb = (const struct quick_file_names *) b;
3152
3153 return eq_stmt_list_entry (&ea->hash, &eb->hash);
3154 }
3155
3156 /* Delete function for a quick_file_names. */
3157
3158 static void
3159 delete_file_name_entry (void *e)
3160 {
3161 struct quick_file_names *file_data = (struct quick_file_names *) e;
3162 int i;
3163
3164 for (i = 0; i < file_data->num_file_names; ++i)
3165 {
3166 xfree ((void*) file_data->file_names[i]);
3167 if (file_data->real_names)
3168 xfree ((void*) file_data->real_names[i]);
3169 }
3170
3171 /* The space for the struct itself lives on objfile_obstack,
3172 so we don't free it here. */
3173 }
3174
3175 /* Create a quick_file_names hash table. */
3176
3177 static htab_t
3178 create_quick_file_names_table (unsigned int nr_initial_entries)
3179 {
3180 return htab_create_alloc (nr_initial_entries,
3181 hash_file_name_entry, eq_file_name_entry,
3182 delete_file_name_entry, xcalloc, xfree);
3183 }
3184
3185 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3186 have to be created afterwards. You should call age_cached_comp_units after
3187 processing PER_CU->CU. dw2_setup must have been already called. */
3188
3189 static void
3190 load_cu (struct dwarf2_per_cu_data *per_cu)
3191 {
3192 if (per_cu->is_debug_types)
3193 load_full_type_unit (per_cu);
3194 else
3195 load_full_comp_unit (per_cu, language_minimal);
3196
3197 if (per_cu->cu == NULL)
3198 return; /* Dummy CU. */
3199
3200 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
3201 }
3202
3203 /* Read in the symbols for PER_CU. */
3204
3205 static void
3206 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3207 {
3208 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3209
3210 /* Skip type_unit_groups, reading the type units they contain
3211 is handled elsewhere. */
3212 if (IS_TYPE_UNIT_GROUP (per_cu))
3213 return;
3214
3215 /* The destructor of dwarf2_queue_guard frees any entries left on
3216 the queue. After this point we're guaranteed to leave this function
3217 with the dwarf queue empty. */
3218 dwarf2_queue_guard q_guard;
3219
3220 if (dwarf2_per_objfile->using_index
3221 ? per_cu->v.quick->compunit_symtab == NULL
3222 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3223 {
3224 queue_comp_unit (per_cu, language_minimal);
3225 load_cu (per_cu);
3226
3227 /* If we just loaded a CU from a DWO, and we're working with an index
3228 that may badly handle TUs, load all the TUs in that DWO as well.
3229 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3230 if (!per_cu->is_debug_types
3231 && per_cu->cu != NULL
3232 && per_cu->cu->dwo_unit != NULL
3233 && dwarf2_per_objfile->index_table != NULL
3234 && dwarf2_per_objfile->index_table->version <= 7
3235 /* DWP files aren't supported yet. */
3236 && get_dwp_file (dwarf2_per_objfile) == NULL)
3237 queue_and_load_all_dwo_tus (per_cu);
3238 }
3239
3240 process_queue (dwarf2_per_objfile);
3241
3242 /* Age the cache, releasing compilation units that have not
3243 been used recently. */
3244 age_cached_comp_units (dwarf2_per_objfile);
3245 }
3246
3247 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3248 the objfile from which this CU came. Returns the resulting symbol
3249 table. */
3250
3251 static struct compunit_symtab *
3252 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3253 {
3254 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3255
3256 gdb_assert (dwarf2_per_objfile->using_index);
3257 if (!per_cu->v.quick->compunit_symtab)
3258 {
3259 struct cleanup *back_to = make_cleanup (free_cached_comp_units,
3260 dwarf2_per_objfile);
3261 scoped_restore decrementer = increment_reading_symtab ();
3262 dw2_do_instantiate_symtab (per_cu);
3263 process_cu_includes (dwarf2_per_objfile);
3264 do_cleanups (back_to);
3265 }
3266
3267 return per_cu->v.quick->compunit_symtab;
3268 }
3269
3270 /* Return the CU/TU given its index.
3271
3272 This is intended for loops like:
3273
3274 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3275 + dwarf2_per_objfile->n_type_units); ++i)
3276 {
3277 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3278
3279 ...;
3280 }
3281 */
3282
3283 static struct dwarf2_per_cu_data *
3284 dw2_get_cutu (struct dwarf2_per_objfile *dwarf2_per_objfile,
3285 int index)
3286 {
3287 if (index >= dwarf2_per_objfile->n_comp_units)
3288 {
3289 index -= dwarf2_per_objfile->n_comp_units;
3290 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3291 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
3292 }
3293
3294 return dwarf2_per_objfile->all_comp_units[index];
3295 }
3296
3297 /* Return the CU given its index.
3298 This differs from dw2_get_cutu in that it's for when you know INDEX
3299 refers to a CU. */
3300
3301 static struct dwarf2_per_cu_data *
3302 dw2_get_cu (struct dwarf2_per_objfile *dwarf2_per_objfile, int index)
3303 {
3304 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
3305
3306 return dwarf2_per_objfile->all_comp_units[index];
3307 }
3308
3309 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3310 objfile_obstack, and constructed with the specified field
3311 values. */
3312
3313 static dwarf2_per_cu_data *
3314 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3315 struct dwarf2_section_info *section,
3316 int is_dwz,
3317 sect_offset sect_off, ULONGEST length)
3318 {
3319 struct objfile *objfile = dwarf2_per_objfile->objfile;
3320 dwarf2_per_cu_data *the_cu
3321 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3322 struct dwarf2_per_cu_data);
3323 the_cu->sect_off = sect_off;
3324 the_cu->length = length;
3325 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
3326 the_cu->section = section;
3327 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3328 struct dwarf2_per_cu_quick_data);
3329 the_cu->is_dwz = is_dwz;
3330 return the_cu;
3331 }
3332
3333 /* A helper for create_cus_from_index that handles a given list of
3334 CUs. */
3335
3336 static void
3337 create_cus_from_index_list (struct objfile *objfile,
3338 const gdb_byte *cu_list, offset_type n_elements,
3339 struct dwarf2_section_info *section,
3340 int is_dwz,
3341 int base_offset)
3342 {
3343 offset_type i;
3344 struct dwarf2_per_objfile *dwarf2_per_objfile
3345 = get_dwarf2_per_objfile (objfile);
3346
3347 for (i = 0; i < n_elements; i += 2)
3348 {
3349 gdb_static_assert (sizeof (ULONGEST) >= 8);
3350
3351 sect_offset sect_off
3352 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3353 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3354 cu_list += 2 * 8;
3355
3356 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
3357 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3358 sect_off, length);
3359 }
3360 }
3361
3362 /* Read the CU list from the mapped index, and use it to create all
3363 the CU objects for this objfile. */
3364
3365 static void
3366 create_cus_from_index (struct objfile *objfile,
3367 const gdb_byte *cu_list, offset_type cu_list_elements,
3368 const gdb_byte *dwz_list, offset_type dwz_elements)
3369 {
3370 struct dwz_file *dwz;
3371 struct dwarf2_per_objfile *dwarf2_per_objfile
3372 = get_dwarf2_per_objfile (objfile);
3373
3374 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3375 dwarf2_per_objfile->all_comp_units =
3376 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3377 dwarf2_per_objfile->n_comp_units);
3378
3379 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3380 &dwarf2_per_objfile->info, 0, 0);
3381
3382 if (dwz_elements == 0)
3383 return;
3384
3385 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3386 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3387 cu_list_elements / 2);
3388 }
3389
3390 /* Create the signatured type hash table from the index. */
3391
3392 static void
3393 create_signatured_type_table_from_index (struct objfile *objfile,
3394 struct dwarf2_section_info *section,
3395 const gdb_byte *bytes,
3396 offset_type elements)
3397 {
3398 offset_type i;
3399 htab_t sig_types_hash;
3400 struct dwarf2_per_objfile *dwarf2_per_objfile
3401 = get_dwarf2_per_objfile (objfile);
3402
3403 dwarf2_per_objfile->n_type_units
3404 = dwarf2_per_objfile->n_allocated_type_units
3405 = elements / 3;
3406 dwarf2_per_objfile->all_type_units =
3407 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3408
3409 sig_types_hash = allocate_signatured_type_table (objfile);
3410
3411 for (i = 0; i < elements; i += 3)
3412 {
3413 struct signatured_type *sig_type;
3414 ULONGEST signature;
3415 void **slot;
3416 cu_offset type_offset_in_tu;
3417
3418 gdb_static_assert (sizeof (ULONGEST) >= 8);
3419 sect_offset sect_off
3420 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3421 type_offset_in_tu
3422 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3423 BFD_ENDIAN_LITTLE);
3424 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3425 bytes += 3 * 8;
3426
3427 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3428 struct signatured_type);
3429 sig_type->signature = signature;
3430 sig_type->type_offset_in_tu = type_offset_in_tu;
3431 sig_type->per_cu.is_debug_types = 1;
3432 sig_type->per_cu.section = section;
3433 sig_type->per_cu.sect_off = sect_off;
3434 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3435 sig_type->per_cu.v.quick
3436 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3437 struct dwarf2_per_cu_quick_data);
3438
3439 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3440 *slot = sig_type;
3441
3442 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3443 }
3444
3445 dwarf2_per_objfile->signatured_types = sig_types_hash;
3446 }
3447
3448 /* Create the signatured type hash table from .debug_names. */
3449
3450 static void
3451 create_signatured_type_table_from_debug_names
3452 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3453 const mapped_debug_names &map,
3454 struct dwarf2_section_info *section,
3455 struct dwarf2_section_info *abbrev_section)
3456 {
3457 struct objfile *objfile = dwarf2_per_objfile->objfile;
3458
3459 dwarf2_read_section (objfile, section);
3460 dwarf2_read_section (objfile, abbrev_section);
3461
3462 dwarf2_per_objfile->n_type_units
3463 = dwarf2_per_objfile->n_allocated_type_units
3464 = map.tu_count;
3465 dwarf2_per_objfile->all_type_units
3466 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3467
3468 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3469
3470 for (uint32_t i = 0; i < map.tu_count; ++i)
3471 {
3472 struct signatured_type *sig_type;
3473 ULONGEST signature;
3474 void **slot;
3475 cu_offset type_offset_in_tu;
3476
3477 sect_offset sect_off
3478 = (sect_offset) (extract_unsigned_integer
3479 (map.tu_table_reordered + i * map.offset_size,
3480 map.offset_size,
3481 map.dwarf5_byte_order));
3482
3483 comp_unit_head cu_header;
3484 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3485 abbrev_section,
3486 section->buffer + to_underlying (sect_off),
3487 rcuh_kind::TYPE);
3488
3489 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3490 struct signatured_type);
3491 sig_type->signature = cu_header.signature;
3492 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3493 sig_type->per_cu.is_debug_types = 1;
3494 sig_type->per_cu.section = section;
3495 sig_type->per_cu.sect_off = sect_off;
3496 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3497 sig_type->per_cu.v.quick
3498 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3499 struct dwarf2_per_cu_quick_data);
3500
3501 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3502 *slot = sig_type;
3503
3504 dwarf2_per_objfile->all_type_units[i] = sig_type;
3505 }
3506
3507 dwarf2_per_objfile->signatured_types = sig_types_hash;
3508 }
3509
3510 /* Read the address map data from the mapped index, and use it to
3511 populate the objfile's psymtabs_addrmap. */
3512
3513 static void
3514 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3515 struct mapped_index *index)
3516 {
3517 struct objfile *objfile = dwarf2_per_objfile->objfile;
3518 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3519 const gdb_byte *iter, *end;
3520 struct addrmap *mutable_map;
3521 CORE_ADDR baseaddr;
3522
3523 auto_obstack temp_obstack;
3524
3525 mutable_map = addrmap_create_mutable (&temp_obstack);
3526
3527 iter = index->address_table.data ();
3528 end = iter + index->address_table.size ();
3529
3530 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3531
3532 while (iter < end)
3533 {
3534 ULONGEST hi, lo, cu_index;
3535 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3536 iter += 8;
3537 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3538 iter += 8;
3539 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3540 iter += 4;
3541
3542 if (lo > hi)
3543 {
3544 complaint (&symfile_complaints,
3545 _(".gdb_index address table has invalid range (%s - %s)"),
3546 hex_string (lo), hex_string (hi));
3547 continue;
3548 }
3549
3550 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3551 {
3552 complaint (&symfile_complaints,
3553 _(".gdb_index address table has invalid CU number %u"),
3554 (unsigned) cu_index);
3555 continue;
3556 }
3557
3558 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3559 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3560 addrmap_set_empty (mutable_map, lo, hi - 1,
3561 dw2_get_cutu (dwarf2_per_objfile, cu_index));
3562 }
3563
3564 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3565 &objfile->objfile_obstack);
3566 }
3567
3568 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3569 populate the objfile's psymtabs_addrmap. */
3570
3571 static void
3572 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3573 struct dwarf2_section_info *section)
3574 {
3575 struct objfile *objfile = dwarf2_per_objfile->objfile;
3576 bfd *abfd = objfile->obfd;
3577 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3578 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3579 SECT_OFF_TEXT (objfile));
3580
3581 auto_obstack temp_obstack;
3582 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3583
3584 std::unordered_map<sect_offset,
3585 dwarf2_per_cu_data *,
3586 gdb::hash_enum<sect_offset>>
3587 debug_info_offset_to_per_cu;
3588 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3589 {
3590 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, cui);
3591 const auto insertpair
3592 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3593 if (!insertpair.second)
3594 {
3595 warning (_("Section .debug_aranges in %s has duplicate "
3596 "debug_info_offset %s, ignoring .debug_aranges."),
3597 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3598 return;
3599 }
3600 }
3601
3602 dwarf2_read_section (objfile, section);
3603
3604 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3605
3606 const gdb_byte *addr = section->buffer;
3607
3608 while (addr < section->buffer + section->size)
3609 {
3610 const gdb_byte *const entry_addr = addr;
3611 unsigned int bytes_read;
3612
3613 const LONGEST entry_length = read_initial_length (abfd, addr,
3614 &bytes_read);
3615 addr += bytes_read;
3616
3617 const gdb_byte *const entry_end = addr + entry_length;
3618 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3619 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3620 if (addr + entry_length > section->buffer + section->size)
3621 {
3622 warning (_("Section .debug_aranges in %s entry at offset %zu "
3623 "length %s exceeds section length %s, "
3624 "ignoring .debug_aranges."),
3625 objfile_name (objfile), entry_addr - section->buffer,
3626 plongest (bytes_read + entry_length),
3627 pulongest (section->size));
3628 return;
3629 }
3630
3631 /* The version number. */
3632 const uint16_t version = read_2_bytes (abfd, addr);
3633 addr += 2;
3634 if (version != 2)
3635 {
3636 warning (_("Section .debug_aranges in %s entry at offset %zu "
3637 "has unsupported version %d, ignoring .debug_aranges."),
3638 objfile_name (objfile), entry_addr - section->buffer,
3639 version);
3640 return;
3641 }
3642
3643 const uint64_t debug_info_offset
3644 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3645 addr += offset_size;
3646 const auto per_cu_it
3647 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3648 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3649 {
3650 warning (_("Section .debug_aranges in %s entry at offset %zu "
3651 "debug_info_offset %s does not exists, "
3652 "ignoring .debug_aranges."),
3653 objfile_name (objfile), entry_addr - section->buffer,
3654 pulongest (debug_info_offset));
3655 return;
3656 }
3657 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3658
3659 const uint8_t address_size = *addr++;
3660 if (address_size < 1 || address_size > 8)
3661 {
3662 warning (_("Section .debug_aranges in %s entry at offset %zu "
3663 "address_size %u is invalid, ignoring .debug_aranges."),
3664 objfile_name (objfile), entry_addr - section->buffer,
3665 address_size);
3666 return;
3667 }
3668
3669 const uint8_t segment_selector_size = *addr++;
3670 if (segment_selector_size != 0)
3671 {
3672 warning (_("Section .debug_aranges in %s entry at offset %zu "
3673 "segment_selector_size %u is not supported, "
3674 "ignoring .debug_aranges."),
3675 objfile_name (objfile), entry_addr - section->buffer,
3676 segment_selector_size);
3677 return;
3678 }
3679
3680 /* Must pad to an alignment boundary that is twice the address
3681 size. It is undocumented by the DWARF standard but GCC does
3682 use it. */
3683 for (size_t padding = ((-(addr - section->buffer))
3684 & (2 * address_size - 1));
3685 padding > 0; padding--)
3686 if (*addr++ != 0)
3687 {
3688 warning (_("Section .debug_aranges in %s entry at offset %zu "
3689 "padding is not zero, ignoring .debug_aranges."),
3690 objfile_name (objfile), entry_addr - section->buffer);
3691 return;
3692 }
3693
3694 for (;;)
3695 {
3696 if (addr + 2 * address_size > entry_end)
3697 {
3698 warning (_("Section .debug_aranges in %s entry at offset %zu "
3699 "address list is not properly terminated, "
3700 "ignoring .debug_aranges."),
3701 objfile_name (objfile), entry_addr - section->buffer);
3702 return;
3703 }
3704 ULONGEST start = extract_unsigned_integer (addr, address_size,
3705 dwarf5_byte_order);
3706 addr += address_size;
3707 ULONGEST length = extract_unsigned_integer (addr, address_size,
3708 dwarf5_byte_order);
3709 addr += address_size;
3710 if (start == 0 && length == 0)
3711 break;
3712 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3713 {
3714 /* Symbol was eliminated due to a COMDAT group. */
3715 continue;
3716 }
3717 ULONGEST end = start + length;
3718 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3719 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3720 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3721 }
3722 }
3723
3724 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3725 &objfile->objfile_obstack);
3726 }
3727
3728 /* The hash function for strings in the mapped index. This is the same as
3729 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3730 implementation. This is necessary because the hash function is tied to the
3731 format of the mapped index file. The hash values do not have to match with
3732 SYMBOL_HASH_NEXT.
3733
3734 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3735
3736 static hashval_t
3737 mapped_index_string_hash (int index_version, const void *p)
3738 {
3739 const unsigned char *str = (const unsigned char *) p;
3740 hashval_t r = 0;
3741 unsigned char c;
3742
3743 while ((c = *str++) != 0)
3744 {
3745 if (index_version >= 5)
3746 c = tolower (c);
3747 r = r * 67 + c - 113;
3748 }
3749
3750 return r;
3751 }
3752
3753 /* Find a slot in the mapped index INDEX for the object named NAME.
3754 If NAME is found, set *VEC_OUT to point to the CU vector in the
3755 constant pool and return true. If NAME cannot be found, return
3756 false. */
3757
3758 static bool
3759 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3760 offset_type **vec_out)
3761 {
3762 offset_type hash;
3763 offset_type slot, step;
3764 int (*cmp) (const char *, const char *);
3765
3766 gdb::unique_xmalloc_ptr<char> without_params;
3767 if (current_language->la_language == language_cplus
3768 || current_language->la_language == language_fortran
3769 || current_language->la_language == language_d)
3770 {
3771 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3772 not contain any. */
3773
3774 if (strchr (name, '(') != NULL)
3775 {
3776 without_params = cp_remove_params (name);
3777
3778 if (without_params != NULL)
3779 name = without_params.get ();
3780 }
3781 }
3782
3783 /* Index version 4 did not support case insensitive searches. But the
3784 indices for case insensitive languages are built in lowercase, therefore
3785 simulate our NAME being searched is also lowercased. */
3786 hash = mapped_index_string_hash ((index->version == 4
3787 && case_sensitivity == case_sensitive_off
3788 ? 5 : index->version),
3789 name);
3790
3791 slot = hash & (index->symbol_table.size () - 1);
3792 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3793 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3794
3795 for (;;)
3796 {
3797 const char *str;
3798
3799 const auto &bucket = index->symbol_table[slot];
3800 if (bucket.name == 0 && bucket.vec == 0)
3801 return false;
3802
3803 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3804 if (!cmp (name, str))
3805 {
3806 *vec_out = (offset_type *) (index->constant_pool
3807 + MAYBE_SWAP (bucket.vec));
3808 return true;
3809 }
3810
3811 slot = (slot + step) & (index->symbol_table.size () - 1);
3812 }
3813 }
3814
3815 /* A helper function that reads the .gdb_index from SECTION and fills
3816 in MAP. FILENAME is the name of the file containing the section;
3817 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3818 ok to use deprecated sections.
3819
3820 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3821 out parameters that are filled in with information about the CU and
3822 TU lists in the section.
3823
3824 Returns 1 if all went well, 0 otherwise. */
3825
3826 static int
3827 read_index_from_section (struct objfile *objfile,
3828 const char *filename,
3829 int deprecated_ok,
3830 struct dwarf2_section_info *section,
3831 struct mapped_index *map,
3832 const gdb_byte **cu_list,
3833 offset_type *cu_list_elements,
3834 const gdb_byte **types_list,
3835 offset_type *types_list_elements)
3836 {
3837 const gdb_byte *addr;
3838 offset_type version;
3839 offset_type *metadata;
3840 int i;
3841
3842 if (dwarf2_section_empty_p (section))
3843 return 0;
3844
3845 /* Older elfutils strip versions could keep the section in the main
3846 executable while splitting it for the separate debug info file. */
3847 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3848 return 0;
3849
3850 dwarf2_read_section (objfile, section);
3851
3852 addr = section->buffer;
3853 /* Version check. */
3854 version = MAYBE_SWAP (*(offset_type *) addr);
3855 /* Versions earlier than 3 emitted every copy of a psymbol. This
3856 causes the index to behave very poorly for certain requests. Version 3
3857 contained incomplete addrmap. So, it seems better to just ignore such
3858 indices. */
3859 if (version < 4)
3860 {
3861 static int warning_printed = 0;
3862 if (!warning_printed)
3863 {
3864 warning (_("Skipping obsolete .gdb_index section in %s."),
3865 filename);
3866 warning_printed = 1;
3867 }
3868 return 0;
3869 }
3870 /* Index version 4 uses a different hash function than index version
3871 5 and later.
3872
3873 Versions earlier than 6 did not emit psymbols for inlined
3874 functions. Using these files will cause GDB not to be able to
3875 set breakpoints on inlined functions by name, so we ignore these
3876 indices unless the user has done
3877 "set use-deprecated-index-sections on". */
3878 if (version < 6 && !deprecated_ok)
3879 {
3880 static int warning_printed = 0;
3881 if (!warning_printed)
3882 {
3883 warning (_("\
3884 Skipping deprecated .gdb_index section in %s.\n\
3885 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3886 to use the section anyway."),
3887 filename);
3888 warning_printed = 1;
3889 }
3890 return 0;
3891 }
3892 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3893 of the TU (for symbols coming from TUs),
3894 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3895 Plus gold-generated indices can have duplicate entries for global symbols,
3896 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3897 These are just performance bugs, and we can't distinguish gdb-generated
3898 indices from gold-generated ones, so issue no warning here. */
3899
3900 /* Indexes with higher version than the one supported by GDB may be no
3901 longer backward compatible. */
3902 if (version > 8)
3903 return 0;
3904
3905 map->version = version;
3906 map->total_size = section->size;
3907
3908 metadata = (offset_type *) (addr + sizeof (offset_type));
3909
3910 i = 0;
3911 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3912 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3913 / 8);
3914 ++i;
3915
3916 *types_list = addr + MAYBE_SWAP (metadata[i]);
3917 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3918 - MAYBE_SWAP (metadata[i]))
3919 / 8);
3920 ++i;
3921
3922 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3923 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3924 map->address_table
3925 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3926 ++i;
3927
3928 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3929 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3930 map->symbol_table
3931 = gdb::array_view<mapped_index::symbol_table_slot>
3932 ((mapped_index::symbol_table_slot *) symbol_table,
3933 (mapped_index::symbol_table_slot *) symbol_table_end);
3934
3935 ++i;
3936 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3937
3938 return 1;
3939 }
3940
3941 /* Read .gdb_index. If everything went ok, initialize the "quick"
3942 elements of all the CUs and return 1. Otherwise, return 0. */
3943
3944 static int
3945 dwarf2_read_index (struct objfile *objfile)
3946 {
3947 struct mapped_index local_map, *map;
3948 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3949 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3950 struct dwz_file *dwz;
3951 struct dwarf2_per_objfile *dwarf2_per_objfile
3952 = get_dwarf2_per_objfile (objfile);
3953
3954 if (!read_index_from_section (objfile, objfile_name (objfile),
3955 use_deprecated_index_sections,
3956 &dwarf2_per_objfile->gdb_index, &local_map,
3957 &cu_list, &cu_list_elements,
3958 &types_list, &types_list_elements))
3959 return 0;
3960
3961 /* Don't use the index if it's empty. */
3962 if (local_map.symbol_table.empty ())
3963 return 0;
3964
3965 /* If there is a .dwz file, read it so we can get its CU list as
3966 well. */
3967 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3968 if (dwz != NULL)
3969 {
3970 struct mapped_index dwz_map;
3971 const gdb_byte *dwz_types_ignore;
3972 offset_type dwz_types_elements_ignore;
3973
3974 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3975 1,
3976 &dwz->gdb_index, &dwz_map,
3977 &dwz_list, &dwz_list_elements,
3978 &dwz_types_ignore,
3979 &dwz_types_elements_ignore))
3980 {
3981 warning (_("could not read '.gdb_index' section from %s; skipping"),
3982 bfd_get_filename (dwz->dwz_bfd));
3983 return 0;
3984 }
3985 }
3986
3987 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3988 dwz_list_elements);
3989
3990 if (types_list_elements)
3991 {
3992 struct dwarf2_section_info *section;
3993
3994 /* We can only handle a single .debug_types when we have an
3995 index. */
3996 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3997 return 0;
3998
3999 section = VEC_index (dwarf2_section_info_def,
4000 dwarf2_per_objfile->types, 0);
4001
4002 create_signatured_type_table_from_index (objfile, section, types_list,
4003 types_list_elements);
4004 }
4005
4006 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
4007
4008 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
4009 map = new (map) mapped_index ();
4010 *map = local_map;
4011
4012 dwarf2_per_objfile->index_table = map;
4013 dwarf2_per_objfile->using_index = 1;
4014 dwarf2_per_objfile->quick_file_names_table =
4015 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
4016
4017 return 1;
4018 }
4019
4020 /* die_reader_func for dw2_get_file_names. */
4021
4022 static void
4023 dw2_get_file_names_reader (const struct die_reader_specs *reader,
4024 const gdb_byte *info_ptr,
4025 struct die_info *comp_unit_die,
4026 int has_children,
4027 void *data)
4028 {
4029 struct dwarf2_cu *cu = reader->cu;
4030 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
4031 struct dwarf2_per_objfile *dwarf2_per_objfile
4032 = cu->per_cu->dwarf2_per_objfile;
4033 struct objfile *objfile = dwarf2_per_objfile->objfile;
4034 struct dwarf2_per_cu_data *lh_cu;
4035 struct attribute *attr;
4036 int i;
4037 void **slot;
4038 struct quick_file_names *qfn;
4039
4040 gdb_assert (! this_cu->is_debug_types);
4041
4042 /* Our callers never want to match partial units -- instead they
4043 will match the enclosing full CU. */
4044 if (comp_unit_die->tag == DW_TAG_partial_unit)
4045 {
4046 this_cu->v.quick->no_file_data = 1;
4047 return;
4048 }
4049
4050 lh_cu = this_cu;
4051 slot = NULL;
4052
4053 line_header_up lh;
4054 sect_offset line_offset {};
4055
4056 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4057 if (attr)
4058 {
4059 struct quick_file_names find_entry;
4060
4061 line_offset = (sect_offset) DW_UNSND (attr);
4062
4063 /* We may have already read in this line header (TU line header sharing).
4064 If we have we're done. */
4065 find_entry.hash.dwo_unit = cu->dwo_unit;
4066 find_entry.hash.line_sect_off = line_offset;
4067 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
4068 &find_entry, INSERT);
4069 if (*slot != NULL)
4070 {
4071 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
4072 return;
4073 }
4074
4075 lh = dwarf_decode_line_header (line_offset, cu);
4076 }
4077 if (lh == NULL)
4078 {
4079 lh_cu->v.quick->no_file_data = 1;
4080 return;
4081 }
4082
4083 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
4084 qfn->hash.dwo_unit = cu->dwo_unit;
4085 qfn->hash.line_sect_off = line_offset;
4086 gdb_assert (slot != NULL);
4087 *slot = qfn;
4088
4089 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
4090
4091 qfn->num_file_names = lh->file_names.size ();
4092 qfn->file_names =
4093 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
4094 for (i = 0; i < lh->file_names.size (); ++i)
4095 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
4096 qfn->real_names = NULL;
4097
4098 lh_cu->v.quick->file_names = qfn;
4099 }
4100
4101 /* A helper for the "quick" functions which attempts to read the line
4102 table for THIS_CU. */
4103
4104 static struct quick_file_names *
4105 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
4106 {
4107 /* This should never be called for TUs. */
4108 gdb_assert (! this_cu->is_debug_types);
4109 /* Nor type unit groups. */
4110 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
4111
4112 if (this_cu->v.quick->file_names != NULL)
4113 return this_cu->v.quick->file_names;
4114 /* If we know there is no line data, no point in looking again. */
4115 if (this_cu->v.quick->no_file_data)
4116 return NULL;
4117
4118 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
4119
4120 if (this_cu->v.quick->no_file_data)
4121 return NULL;
4122 return this_cu->v.quick->file_names;
4123 }
4124
4125 /* A helper for the "quick" functions which computes and caches the
4126 real path for a given file name from the line table. */
4127
4128 static const char *
4129 dw2_get_real_path (struct objfile *objfile,
4130 struct quick_file_names *qfn, int index)
4131 {
4132 if (qfn->real_names == NULL)
4133 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
4134 qfn->num_file_names, const char *);
4135
4136 if (qfn->real_names[index] == NULL)
4137 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
4138
4139 return qfn->real_names[index];
4140 }
4141
4142 static struct symtab *
4143 dw2_find_last_source_symtab (struct objfile *objfile)
4144 {
4145 struct dwarf2_per_objfile *dwarf2_per_objfile
4146 = get_dwarf2_per_objfile (objfile);
4147 int index = dwarf2_per_objfile->n_comp_units - 1;
4148 dwarf2_per_cu_data *dwarf_cu = dw2_get_cutu (dwarf2_per_objfile, index);
4149 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
4150
4151 if (cust == NULL)
4152 return NULL;
4153
4154 return compunit_primary_filetab (cust);
4155 }
4156
4157 /* Traversal function for dw2_forget_cached_source_info. */
4158
4159 static int
4160 dw2_free_cached_file_names (void **slot, void *info)
4161 {
4162 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
4163
4164 if (file_data->real_names)
4165 {
4166 int i;
4167
4168 for (i = 0; i < file_data->num_file_names; ++i)
4169 {
4170 xfree ((void*) file_data->real_names[i]);
4171 file_data->real_names[i] = NULL;
4172 }
4173 }
4174
4175 return 1;
4176 }
4177
4178 static void
4179 dw2_forget_cached_source_info (struct objfile *objfile)
4180 {
4181 struct dwarf2_per_objfile *dwarf2_per_objfile
4182 = get_dwarf2_per_objfile (objfile);
4183
4184 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
4185 dw2_free_cached_file_names, NULL);
4186 }
4187
4188 /* Helper function for dw2_map_symtabs_matching_filename that expands
4189 the symtabs and calls the iterator. */
4190
4191 static int
4192 dw2_map_expand_apply (struct objfile *objfile,
4193 struct dwarf2_per_cu_data *per_cu,
4194 const char *name, const char *real_path,
4195 gdb::function_view<bool (symtab *)> callback)
4196 {
4197 struct compunit_symtab *last_made = objfile->compunit_symtabs;
4198
4199 /* Don't visit already-expanded CUs. */
4200 if (per_cu->v.quick->compunit_symtab)
4201 return 0;
4202
4203 /* This may expand more than one symtab, and we want to iterate over
4204 all of them. */
4205 dw2_instantiate_symtab (per_cu);
4206
4207 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4208 last_made, callback);
4209 }
4210
4211 /* Implementation of the map_symtabs_matching_filename method. */
4212
4213 static bool
4214 dw2_map_symtabs_matching_filename
4215 (struct objfile *objfile, const char *name, const char *real_path,
4216 gdb::function_view<bool (symtab *)> callback)
4217 {
4218 int i;
4219 const char *name_basename = lbasename (name);
4220 struct dwarf2_per_objfile *dwarf2_per_objfile
4221 = get_dwarf2_per_objfile (objfile);
4222
4223 /* The rule is CUs specify all the files, including those used by
4224 any TU, so there's no need to scan TUs here. */
4225
4226 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4227 {
4228 int j;
4229 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
4230 struct quick_file_names *file_data;
4231
4232 /* We only need to look at symtabs not already expanded. */
4233 if (per_cu->v.quick->compunit_symtab)
4234 continue;
4235
4236 file_data = dw2_get_file_names (per_cu);
4237 if (file_data == NULL)
4238 continue;
4239
4240 for (j = 0; j < file_data->num_file_names; ++j)
4241 {
4242 const char *this_name = file_data->file_names[j];
4243 const char *this_real_name;
4244
4245 if (compare_filenames_for_search (this_name, name))
4246 {
4247 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4248 callback))
4249 return true;
4250 continue;
4251 }
4252
4253 /* Before we invoke realpath, which can get expensive when many
4254 files are involved, do a quick comparison of the basenames. */
4255 if (! basenames_may_differ
4256 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4257 continue;
4258
4259 this_real_name = dw2_get_real_path (objfile, file_data, j);
4260 if (compare_filenames_for_search (this_real_name, name))
4261 {
4262 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4263 callback))
4264 return true;
4265 continue;
4266 }
4267
4268 if (real_path != NULL)
4269 {
4270 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4271 gdb_assert (IS_ABSOLUTE_PATH (name));
4272 if (this_real_name != NULL
4273 && FILENAME_CMP (real_path, this_real_name) == 0)
4274 {
4275 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4276 callback))
4277 return true;
4278 continue;
4279 }
4280 }
4281 }
4282 }
4283
4284 return false;
4285 }
4286
4287 /* Struct used to manage iterating over all CUs looking for a symbol. */
4288
4289 struct dw2_symtab_iterator
4290 {
4291 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
4292 struct dwarf2_per_objfile *dwarf2_per_objfile;
4293 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4294 int want_specific_block;
4295 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4296 Unused if !WANT_SPECIFIC_BLOCK. */
4297 int block_index;
4298 /* The kind of symbol we're looking for. */
4299 domain_enum domain;
4300 /* The list of CUs from the index entry of the symbol,
4301 or NULL if not found. */
4302 offset_type *vec;
4303 /* The next element in VEC to look at. */
4304 int next;
4305 /* The number of elements in VEC, or zero if there is no match. */
4306 int length;
4307 /* Have we seen a global version of the symbol?
4308 If so we can ignore all further global instances.
4309 This is to work around gold/15646, inefficient gold-generated
4310 indices. */
4311 int global_seen;
4312 };
4313
4314 /* Initialize the index symtab iterator ITER.
4315 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4316 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
4317
4318 static void
4319 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
4320 struct dwarf2_per_objfile *dwarf2_per_objfile,
4321 int want_specific_block,
4322 int block_index,
4323 domain_enum domain,
4324 const char *name)
4325 {
4326 iter->dwarf2_per_objfile = dwarf2_per_objfile;
4327 iter->want_specific_block = want_specific_block;
4328 iter->block_index = block_index;
4329 iter->domain = domain;
4330 iter->next = 0;
4331 iter->global_seen = 0;
4332
4333 mapped_index *index = dwarf2_per_objfile->index_table;
4334
4335 /* index is NULL if OBJF_READNOW. */
4336 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
4337 iter->length = MAYBE_SWAP (*iter->vec);
4338 else
4339 {
4340 iter->vec = NULL;
4341 iter->length = 0;
4342 }
4343 }
4344
4345 /* Return the next matching CU or NULL if there are no more. */
4346
4347 static struct dwarf2_per_cu_data *
4348 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4349 {
4350 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4351
4352 for ( ; iter->next < iter->length; ++iter->next)
4353 {
4354 offset_type cu_index_and_attrs =
4355 MAYBE_SWAP (iter->vec[iter->next + 1]);
4356 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4357 struct dwarf2_per_cu_data *per_cu;
4358 int want_static = iter->block_index != GLOBAL_BLOCK;
4359 /* This value is only valid for index versions >= 7. */
4360 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4361 gdb_index_symbol_kind symbol_kind =
4362 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4363 /* Only check the symbol attributes if they're present.
4364 Indices prior to version 7 don't record them,
4365 and indices >= 7 may elide them for certain symbols
4366 (gold does this). */
4367 int attrs_valid =
4368 (dwarf2_per_objfile->index_table->version >= 7
4369 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4370
4371 /* Don't crash on bad data. */
4372 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4373 + dwarf2_per_objfile->n_type_units))
4374 {
4375 complaint (&symfile_complaints,
4376 _(".gdb_index entry has bad CU index"
4377 " [in module %s]"),
4378 objfile_name (dwarf2_per_objfile->objfile));
4379 continue;
4380 }
4381
4382 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
4383
4384 /* Skip if already read in. */
4385 if (per_cu->v.quick->compunit_symtab)
4386 continue;
4387
4388 /* Check static vs global. */
4389 if (attrs_valid)
4390 {
4391 if (iter->want_specific_block
4392 && want_static != is_static)
4393 continue;
4394 /* Work around gold/15646. */
4395 if (!is_static && iter->global_seen)
4396 continue;
4397 if (!is_static)
4398 iter->global_seen = 1;
4399 }
4400
4401 /* Only check the symbol's kind if it has one. */
4402 if (attrs_valid)
4403 {
4404 switch (iter->domain)
4405 {
4406 case VAR_DOMAIN:
4407 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4408 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4409 /* Some types are also in VAR_DOMAIN. */
4410 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4411 continue;
4412 break;
4413 case STRUCT_DOMAIN:
4414 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4415 continue;
4416 break;
4417 case LABEL_DOMAIN:
4418 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4419 continue;
4420 break;
4421 default:
4422 break;
4423 }
4424 }
4425
4426 ++iter->next;
4427 return per_cu;
4428 }
4429
4430 return NULL;
4431 }
4432
4433 static struct compunit_symtab *
4434 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4435 const char *name, domain_enum domain)
4436 {
4437 struct compunit_symtab *stab_best = NULL;
4438 struct dwarf2_per_objfile *dwarf2_per_objfile
4439 = get_dwarf2_per_objfile (objfile);
4440
4441 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4442
4443 struct dw2_symtab_iterator iter;
4444 struct dwarf2_per_cu_data *per_cu;
4445
4446 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4447
4448 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4449 {
4450 struct symbol *sym, *with_opaque = NULL;
4451 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4452 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4453 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4454
4455 sym = block_find_symbol (block, name, domain,
4456 block_find_non_opaque_type_preferred,
4457 &with_opaque);
4458
4459 /* Some caution must be observed with overloaded functions
4460 and methods, since the index will not contain any overload
4461 information (but NAME might contain it). */
4462
4463 if (sym != NULL
4464 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4465 return stab;
4466 if (with_opaque != NULL
4467 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4468 stab_best = stab;
4469
4470 /* Keep looking through other CUs. */
4471 }
4472
4473 return stab_best;
4474 }
4475
4476 static void
4477 dw2_print_stats (struct objfile *objfile)
4478 {
4479 struct dwarf2_per_objfile *dwarf2_per_objfile
4480 = get_dwarf2_per_objfile (objfile);
4481 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4482 int count = 0;
4483
4484 for (int i = 0; i < total; ++i)
4485 {
4486 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4487
4488 if (!per_cu->v.quick->compunit_symtab)
4489 ++count;
4490 }
4491 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4492 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4493 }
4494
4495 /* This dumps minimal information about the index.
4496 It is called via "mt print objfiles".
4497 One use is to verify .gdb_index has been loaded by the
4498 gdb.dwarf2/gdb-index.exp testcase. */
4499
4500 static void
4501 dw2_dump (struct objfile *objfile)
4502 {
4503 struct dwarf2_per_objfile *dwarf2_per_objfile
4504 = get_dwarf2_per_objfile (objfile);
4505
4506 gdb_assert (dwarf2_per_objfile->using_index);
4507 printf_filtered (".gdb_index:");
4508 if (dwarf2_per_objfile->index_table != NULL)
4509 {
4510 printf_filtered (" version %d\n",
4511 dwarf2_per_objfile->index_table->version);
4512 }
4513 else
4514 printf_filtered (" faked for \"readnow\"\n");
4515 printf_filtered ("\n");
4516 }
4517
4518 static void
4519 dw2_relocate (struct objfile *objfile,
4520 const struct section_offsets *new_offsets,
4521 const struct section_offsets *delta)
4522 {
4523 /* There's nothing to relocate here. */
4524 }
4525
4526 static void
4527 dw2_expand_symtabs_for_function (struct objfile *objfile,
4528 const char *func_name)
4529 {
4530 struct dwarf2_per_objfile *dwarf2_per_objfile
4531 = get_dwarf2_per_objfile (objfile);
4532
4533 struct dw2_symtab_iterator iter;
4534 struct dwarf2_per_cu_data *per_cu;
4535
4536 /* Note: It doesn't matter what we pass for block_index here. */
4537 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4538 func_name);
4539
4540 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4541 dw2_instantiate_symtab (per_cu);
4542
4543 }
4544
4545 static void
4546 dw2_expand_all_symtabs (struct objfile *objfile)
4547 {
4548 struct dwarf2_per_objfile *dwarf2_per_objfile
4549 = get_dwarf2_per_objfile (objfile);
4550 int total_units = (dwarf2_per_objfile->n_comp_units
4551 + dwarf2_per_objfile->n_type_units);
4552
4553 for (int i = 0; i < total_units; ++i)
4554 {
4555 struct dwarf2_per_cu_data *per_cu
4556 = dw2_get_cutu (dwarf2_per_objfile, i);
4557
4558 dw2_instantiate_symtab (per_cu);
4559 }
4560 }
4561
4562 static void
4563 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4564 const char *fullname)
4565 {
4566 struct dwarf2_per_objfile *dwarf2_per_objfile
4567 = get_dwarf2_per_objfile (objfile);
4568
4569 /* We don't need to consider type units here.
4570 This is only called for examining code, e.g. expand_line_sal.
4571 There can be an order of magnitude (or more) more type units
4572 than comp units, and we avoid them if we can. */
4573
4574 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4575 {
4576 int j;
4577 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4578 struct quick_file_names *file_data;
4579
4580 /* We only need to look at symtabs not already expanded. */
4581 if (per_cu->v.quick->compunit_symtab)
4582 continue;
4583
4584 file_data = dw2_get_file_names (per_cu);
4585 if (file_data == NULL)
4586 continue;
4587
4588 for (j = 0; j < file_data->num_file_names; ++j)
4589 {
4590 const char *this_fullname = file_data->file_names[j];
4591
4592 if (filename_cmp (this_fullname, fullname) == 0)
4593 {
4594 dw2_instantiate_symtab (per_cu);
4595 break;
4596 }
4597 }
4598 }
4599 }
4600
4601 static void
4602 dw2_map_matching_symbols (struct objfile *objfile,
4603 const char * name, domain_enum domain,
4604 int global,
4605 int (*callback) (struct block *,
4606 struct symbol *, void *),
4607 void *data, symbol_name_match_type match,
4608 symbol_compare_ftype *ordered_compare)
4609 {
4610 /* Currently unimplemented; used for Ada. The function can be called if the
4611 current language is Ada for a non-Ada objfile using GNU index. As Ada
4612 does not look for non-Ada symbols this function should just return. */
4613 }
4614
4615 /* Symbol name matcher for .gdb_index names.
4616
4617 Symbol names in .gdb_index have a few particularities:
4618
4619 - There's no indication of which is the language of each symbol.
4620
4621 Since each language has its own symbol name matching algorithm,
4622 and we don't know which language is the right one, we must match
4623 each symbol against all languages. This would be a potential
4624 performance problem if it were not mitigated by the
4625 mapped_index::name_components lookup table, which significantly
4626 reduces the number of times we need to call into this matcher,
4627 making it a non-issue.
4628
4629 - Symbol names in the index have no overload (parameter)
4630 information. I.e., in C++, "foo(int)" and "foo(long)" both
4631 appear as "foo" in the index, for example.
4632
4633 This means that the lookup names passed to the symbol name
4634 matcher functions must have no parameter information either
4635 because (e.g.) symbol search name "foo" does not match
4636 lookup-name "foo(int)" [while swapping search name for lookup
4637 name would match].
4638 */
4639 class gdb_index_symbol_name_matcher
4640 {
4641 public:
4642 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4643 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4644
4645 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4646 Returns true if any matcher matches. */
4647 bool matches (const char *symbol_name);
4648
4649 private:
4650 /* A reference to the lookup name we're matching against. */
4651 const lookup_name_info &m_lookup_name;
4652
4653 /* A vector holding all the different symbol name matchers, for all
4654 languages. */
4655 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4656 };
4657
4658 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4659 (const lookup_name_info &lookup_name)
4660 : m_lookup_name (lookup_name)
4661 {
4662 /* Prepare the vector of comparison functions upfront, to avoid
4663 doing the same work for each symbol. Care is taken to avoid
4664 matching with the same matcher more than once if/when multiple
4665 languages use the same matcher function. */
4666 auto &matchers = m_symbol_name_matcher_funcs;
4667 matchers.reserve (nr_languages);
4668
4669 matchers.push_back (default_symbol_name_matcher);
4670
4671 for (int i = 0; i < nr_languages; i++)
4672 {
4673 const language_defn *lang = language_def ((enum language) i);
4674 symbol_name_matcher_ftype *name_matcher
4675 = get_symbol_name_matcher (lang, m_lookup_name);
4676
4677 /* Don't insert the same comparison routine more than once.
4678 Note that we do this linear walk instead of a seemingly
4679 cheaper sorted insert, or use a std::set or something like
4680 that, because relative order of function addresses is not
4681 stable. This is not a problem in practice because the number
4682 of supported languages is low, and the cost here is tiny
4683 compared to the number of searches we'll do afterwards using
4684 this object. */
4685 if (name_matcher != default_symbol_name_matcher
4686 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4687 == matchers.end ()))
4688 matchers.push_back (name_matcher);
4689 }
4690 }
4691
4692 bool
4693 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4694 {
4695 for (auto matches_name : m_symbol_name_matcher_funcs)
4696 if (matches_name (symbol_name, m_lookup_name, NULL))
4697 return true;
4698
4699 return false;
4700 }
4701
4702 /* Starting from a search name, return the string that finds the upper
4703 bound of all strings that start with SEARCH_NAME in a sorted name
4704 list. Returns the empty string to indicate that the upper bound is
4705 the end of the list. */
4706
4707 static std::string
4708 make_sort_after_prefix_name (const char *search_name)
4709 {
4710 /* When looking to complete "func", we find the upper bound of all
4711 symbols that start with "func" by looking for where we'd insert
4712 the closest string that would follow "func" in lexicographical
4713 order. Usually, that's "func"-with-last-character-incremented,
4714 i.e. "fund". Mind non-ASCII characters, though. Usually those
4715 will be UTF-8 multi-byte sequences, but we can't be certain.
4716 Especially mind the 0xff character, which is a valid character in
4717 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4718 rule out compilers allowing it in identifiers. Note that
4719 conveniently, strcmp/strcasecmp are specified to compare
4720 characters interpreted as unsigned char. So what we do is treat
4721 the whole string as a base 256 number composed of a sequence of
4722 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4723 to 0, and carries 1 to the following more-significant position.
4724 If the very first character in SEARCH_NAME ends up incremented
4725 and carries/overflows, then the upper bound is the end of the
4726 list. The string after the empty string is also the empty
4727 string.
4728
4729 Some examples of this operation:
4730
4731 SEARCH_NAME => "+1" RESULT
4732
4733 "abc" => "abd"
4734 "ab\xff" => "ac"
4735 "\xff" "a" "\xff" => "\xff" "b"
4736 "\xff" => ""
4737 "\xff\xff" => ""
4738 "" => ""
4739
4740 Then, with these symbols for example:
4741
4742 func
4743 func1
4744 fund
4745
4746 completing "func" looks for symbols between "func" and
4747 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4748 which finds "func" and "func1", but not "fund".
4749
4750 And with:
4751
4752 funcÿ (Latin1 'ÿ' [0xff])
4753 funcÿ1
4754 fund
4755
4756 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4757 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4758
4759 And with:
4760
4761 ÿÿ (Latin1 'ÿ' [0xff])
4762 ÿÿ1
4763
4764 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4765 the end of the list.
4766 */
4767 std::string after = search_name;
4768 while (!after.empty () && (unsigned char) after.back () == 0xff)
4769 after.pop_back ();
4770 if (!after.empty ())
4771 after.back () = (unsigned char) after.back () + 1;
4772 return after;
4773 }
4774
4775 /* See declaration. */
4776
4777 std::pair<std::vector<name_component>::const_iterator,
4778 std::vector<name_component>::const_iterator>
4779 mapped_index_base::find_name_components_bounds
4780 (const lookup_name_info &lookup_name_without_params) const
4781 {
4782 auto *name_cmp
4783 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4784
4785 const char *cplus
4786 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4787
4788 /* Comparison function object for lower_bound that matches against a
4789 given symbol name. */
4790 auto lookup_compare_lower = [&] (const name_component &elem,
4791 const char *name)
4792 {
4793 const char *elem_qualified = this->symbol_name_at (elem.idx);
4794 const char *elem_name = elem_qualified + elem.name_offset;
4795 return name_cmp (elem_name, name) < 0;
4796 };
4797
4798 /* Comparison function object for upper_bound that matches against a
4799 given symbol name. */
4800 auto lookup_compare_upper = [&] (const char *name,
4801 const name_component &elem)
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 (name, elem_name) < 0;
4806 };
4807
4808 auto begin = this->name_components.begin ();
4809 auto end = this->name_components.end ();
4810
4811 /* Find the lower bound. */
4812 auto lower = [&] ()
4813 {
4814 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4815 return begin;
4816 else
4817 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4818 } ();
4819
4820 /* Find the upper bound. */
4821 auto upper = [&] ()
4822 {
4823 if (lookup_name_without_params.completion_mode ())
4824 {
4825 /* In completion mode, we want UPPER to point past all
4826 symbols names that have the same prefix. I.e., with
4827 these symbols, and completing "func":
4828
4829 function << lower bound
4830 function1
4831 other_function << upper bound
4832
4833 We find the upper bound by looking for the insertion
4834 point of "func"-with-last-character-incremented,
4835 i.e. "fund". */
4836 std::string after = make_sort_after_prefix_name (cplus);
4837 if (after.empty ())
4838 return end;
4839 return std::lower_bound (lower, end, after.c_str (),
4840 lookup_compare_lower);
4841 }
4842 else
4843 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4844 } ();
4845
4846 return {lower, upper};
4847 }
4848
4849 /* See declaration. */
4850
4851 void
4852 mapped_index_base::build_name_components ()
4853 {
4854 if (!this->name_components.empty ())
4855 return;
4856
4857 this->name_components_casing = case_sensitivity;
4858 auto *name_cmp
4859 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4860
4861 /* The code below only knows how to break apart components of C++
4862 symbol names (and other languages that use '::' as
4863 namespace/module separator). If we add support for wild matching
4864 to some language that uses some other operator (E.g., Ada, Go and
4865 D use '.'), then we'll need to try splitting the symbol name
4866 according to that language too. Note that Ada does support wild
4867 matching, but doesn't currently support .gdb_index. */
4868 auto count = this->symbol_name_count ();
4869 for (offset_type idx = 0; idx < count; idx++)
4870 {
4871 if (this->symbol_name_slot_invalid (idx))
4872 continue;
4873
4874 const char *name = this->symbol_name_at (idx);
4875
4876 /* Add each name component to the name component table. */
4877 unsigned int previous_len = 0;
4878 for (unsigned int current_len = cp_find_first_component (name);
4879 name[current_len] != '\0';
4880 current_len += cp_find_first_component (name + current_len))
4881 {
4882 gdb_assert (name[current_len] == ':');
4883 this->name_components.push_back ({previous_len, idx});
4884 /* Skip the '::'. */
4885 current_len += 2;
4886 previous_len = current_len;
4887 }
4888 this->name_components.push_back ({previous_len, idx});
4889 }
4890
4891 /* Sort name_components elements by name. */
4892 auto name_comp_compare = [&] (const name_component &left,
4893 const name_component &right)
4894 {
4895 const char *left_qualified = this->symbol_name_at (left.idx);
4896 const char *right_qualified = this->symbol_name_at (right.idx);
4897
4898 const char *left_name = left_qualified + left.name_offset;
4899 const char *right_name = right_qualified + right.name_offset;
4900
4901 return name_cmp (left_name, right_name) < 0;
4902 };
4903
4904 std::sort (this->name_components.begin (),
4905 this->name_components.end (),
4906 name_comp_compare);
4907 }
4908
4909 /* Helper for dw2_expand_symtabs_matching that works with a
4910 mapped_index_base instead of the containing objfile. This is split
4911 to a separate function in order to be able to unit test the
4912 name_components matching using a mock mapped_index_base. For each
4913 symbol name that matches, calls MATCH_CALLBACK, passing it the
4914 symbol's index in the mapped_index_base symbol table. */
4915
4916 static void
4917 dw2_expand_symtabs_matching_symbol
4918 (mapped_index_base &index,
4919 const lookup_name_info &lookup_name_in,
4920 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4921 enum search_domain kind,
4922 gdb::function_view<void (offset_type)> match_callback)
4923 {
4924 lookup_name_info lookup_name_without_params
4925 = lookup_name_in.make_ignore_params ();
4926 gdb_index_symbol_name_matcher lookup_name_matcher
4927 (lookup_name_without_params);
4928
4929 /* Build the symbol name component sorted vector, if we haven't
4930 yet. */
4931 index.build_name_components ();
4932
4933 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4934
4935 /* Now for each symbol name in range, check to see if we have a name
4936 match, and if so, call the MATCH_CALLBACK callback. */
4937
4938 /* The same symbol may appear more than once in the range though.
4939 E.g., if we're looking for symbols that complete "w", and we have
4940 a symbol named "w1::w2", we'll find the two name components for
4941 that same symbol in the range. To be sure we only call the
4942 callback once per symbol, we first collect the symbol name
4943 indexes that matched in a temporary vector and ignore
4944 duplicates. */
4945 std::vector<offset_type> matches;
4946 matches.reserve (std::distance (bounds.first, bounds.second));
4947
4948 for (; bounds.first != bounds.second; ++bounds.first)
4949 {
4950 const char *qualified = index.symbol_name_at (bounds.first->idx);
4951
4952 if (!lookup_name_matcher.matches (qualified)
4953 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4954 continue;
4955
4956 matches.push_back (bounds.first->idx);
4957 }
4958
4959 std::sort (matches.begin (), matches.end ());
4960
4961 /* Finally call the callback, once per match. */
4962 ULONGEST prev = -1;
4963 for (offset_type idx : matches)
4964 {
4965 if (prev != idx)
4966 {
4967 match_callback (idx);
4968 prev = idx;
4969 }
4970 }
4971
4972 /* Above we use a type wider than idx's for 'prev', since 0 and
4973 (offset_type)-1 are both possible values. */
4974 static_assert (sizeof (prev) > sizeof (offset_type), "");
4975 }
4976
4977 #if GDB_SELF_TEST
4978
4979 namespace selftests { namespace dw2_expand_symtabs_matching {
4980
4981 /* A mock .gdb_index/.debug_names-like name index table, enough to
4982 exercise dw2_expand_symtabs_matching_symbol, which works with the
4983 mapped_index_base interface. Builds an index from the symbol list
4984 passed as parameter to the constructor. */
4985 class mock_mapped_index : public mapped_index_base
4986 {
4987 public:
4988 mock_mapped_index (gdb::array_view<const char *> symbols)
4989 : m_symbol_table (symbols)
4990 {}
4991
4992 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4993
4994 /* Return the number of names in the symbol table. */
4995 virtual size_t symbol_name_count () const
4996 {
4997 return m_symbol_table.size ();
4998 }
4999
5000 /* Get the name of the symbol at IDX in the symbol table. */
5001 virtual const char *symbol_name_at (offset_type idx) const
5002 {
5003 return m_symbol_table[idx];
5004 }
5005
5006 private:
5007 gdb::array_view<const char *> m_symbol_table;
5008 };
5009
5010 /* Convenience function that converts a NULL pointer to a "<null>"
5011 string, to pass to print routines. */
5012
5013 static const char *
5014 string_or_null (const char *str)
5015 {
5016 return str != NULL ? str : "<null>";
5017 }
5018
5019 /* Check if a lookup_name_info built from
5020 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
5021 index. EXPECTED_LIST is the list of expected matches, in expected
5022 matching order. If no match expected, then an empty list is
5023 specified. Returns true on success. On failure prints a warning
5024 indicating the file:line that failed, and returns false. */
5025
5026 static bool
5027 check_match (const char *file, int line,
5028 mock_mapped_index &mock_index,
5029 const char *name, symbol_name_match_type match_type,
5030 bool completion_mode,
5031 std::initializer_list<const char *> expected_list)
5032 {
5033 lookup_name_info lookup_name (name, match_type, completion_mode);
5034
5035 bool matched = true;
5036
5037 auto mismatch = [&] (const char *expected_str,
5038 const char *got)
5039 {
5040 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
5041 "expected=\"%s\", got=\"%s\"\n"),
5042 file, line,
5043 (match_type == symbol_name_match_type::FULL
5044 ? "FULL" : "WILD"),
5045 name, string_or_null (expected_str), string_or_null (got));
5046 matched = false;
5047 };
5048
5049 auto expected_it = expected_list.begin ();
5050 auto expected_end = expected_list.end ();
5051
5052 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
5053 NULL, ALL_DOMAIN,
5054 [&] (offset_type idx)
5055 {
5056 const char *matched_name = mock_index.symbol_name_at (idx);
5057 const char *expected_str
5058 = expected_it == expected_end ? NULL : *expected_it++;
5059
5060 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
5061 mismatch (expected_str, matched_name);
5062 });
5063
5064 const char *expected_str
5065 = expected_it == expected_end ? NULL : *expected_it++;
5066 if (expected_str != NULL)
5067 mismatch (expected_str, NULL);
5068
5069 return matched;
5070 }
5071
5072 /* The symbols added to the mock mapped_index for testing (in
5073 canonical form). */
5074 static const char *test_symbols[] = {
5075 "function",
5076 "std::bar",
5077 "std::zfunction",
5078 "std::zfunction2",
5079 "w1::w2",
5080 "ns::foo<char*>",
5081 "ns::foo<int>",
5082 "ns::foo<long>",
5083 "ns2::tmpl<int>::foo2",
5084 "(anonymous namespace)::A::B::C",
5085
5086 /* These are used to check that the increment-last-char in the
5087 matching algorithm for completion doesn't match "t1_fund" when
5088 completing "t1_func". */
5089 "t1_func",
5090 "t1_func1",
5091 "t1_fund",
5092 "t1_fund1",
5093
5094 /* A UTF-8 name with multi-byte sequences to make sure that
5095 cp-name-parser understands this as a single identifier ("função"
5096 is "function" in PT). */
5097 u8"u8função",
5098
5099 /* \377 (0xff) is Latin1 'ÿ'. */
5100 "yfunc\377",
5101
5102 /* \377 (0xff) is Latin1 'ÿ'. */
5103 "\377",
5104 "\377\377123",
5105
5106 /* A name with all sorts of complications. Starts with "z" to make
5107 it easier for the completion tests below. */
5108 #define Z_SYM_NAME \
5109 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
5110 "::tuple<(anonymous namespace)::ui*, " \
5111 "std::default_delete<(anonymous namespace)::ui>, void>"
5112
5113 Z_SYM_NAME
5114 };
5115
5116 /* Returns true if the mapped_index_base::find_name_component_bounds
5117 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
5118 in completion mode. */
5119
5120 static bool
5121 check_find_bounds_finds (mapped_index_base &index,
5122 const char *search_name,
5123 gdb::array_view<const char *> expected_syms)
5124 {
5125 lookup_name_info lookup_name (search_name,
5126 symbol_name_match_type::FULL, true);
5127
5128 auto bounds = index.find_name_components_bounds (lookup_name);
5129
5130 size_t distance = std::distance (bounds.first, bounds.second);
5131 if (distance != expected_syms.size ())
5132 return false;
5133
5134 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
5135 {
5136 auto nc_elem = bounds.first + exp_elem;
5137 const char *qualified = index.symbol_name_at (nc_elem->idx);
5138 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
5139 return false;
5140 }
5141
5142 return true;
5143 }
5144
5145 /* Test the lower-level mapped_index::find_name_component_bounds
5146 method. */
5147
5148 static void
5149 test_mapped_index_find_name_component_bounds ()
5150 {
5151 mock_mapped_index mock_index (test_symbols);
5152
5153 mock_index.build_name_components ();
5154
5155 /* Test the lower-level mapped_index::find_name_component_bounds
5156 method in completion mode. */
5157 {
5158 static const char *expected_syms[] = {
5159 "t1_func",
5160 "t1_func1",
5161 };
5162
5163 SELF_CHECK (check_find_bounds_finds (mock_index,
5164 "t1_func", expected_syms));
5165 }
5166
5167 /* Check that the increment-last-char in the name matching algorithm
5168 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5169 {
5170 static const char *expected_syms1[] = {
5171 "\377",
5172 "\377\377123",
5173 };
5174 SELF_CHECK (check_find_bounds_finds (mock_index,
5175 "\377", expected_syms1));
5176
5177 static const char *expected_syms2[] = {
5178 "\377\377123",
5179 };
5180 SELF_CHECK (check_find_bounds_finds (mock_index,
5181 "\377\377", expected_syms2));
5182 }
5183 }
5184
5185 /* Test dw2_expand_symtabs_matching_symbol. */
5186
5187 static void
5188 test_dw2_expand_symtabs_matching_symbol ()
5189 {
5190 mock_mapped_index mock_index (test_symbols);
5191
5192 /* We let all tests run until the end even if some fails, for debug
5193 convenience. */
5194 bool any_mismatch = false;
5195
5196 /* Create the expected symbols list (an initializer_list). Needed
5197 because lists have commas, and we need to pass them to CHECK,
5198 which is a macro. */
5199 #define EXPECT(...) { __VA_ARGS__ }
5200
5201 /* Wrapper for check_match that passes down the current
5202 __FILE__/__LINE__. */
5203 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5204 any_mismatch |= !check_match (__FILE__, __LINE__, \
5205 mock_index, \
5206 NAME, MATCH_TYPE, COMPLETION_MODE, \
5207 EXPECTED_LIST)
5208
5209 /* Identity checks. */
5210 for (const char *sym : test_symbols)
5211 {
5212 /* Should be able to match all existing symbols. */
5213 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5214 EXPECT (sym));
5215
5216 /* Should be able to match all existing symbols with
5217 parameters. */
5218 std::string with_params = std::string (sym) + "(int)";
5219 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5220 EXPECT (sym));
5221
5222 /* Should be able to match all existing symbols with
5223 parameters and qualifiers. */
5224 with_params = std::string (sym) + " ( int ) const";
5225 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5226 EXPECT (sym));
5227
5228 /* This should really find sym, but cp-name-parser.y doesn't
5229 know about lvalue/rvalue qualifiers yet. */
5230 with_params = std::string (sym) + " ( int ) &&";
5231 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5232 {});
5233 }
5234
5235 /* Check that the name matching algorithm for completion doesn't get
5236 confused with Latin1 'ÿ' / 0xff. */
5237 {
5238 static const char str[] = "\377";
5239 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5240 EXPECT ("\377", "\377\377123"));
5241 }
5242
5243 /* Check that the increment-last-char in the matching algorithm for
5244 completion doesn't match "t1_fund" when completing "t1_func". */
5245 {
5246 static const char str[] = "t1_func";
5247 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5248 EXPECT ("t1_func", "t1_func1"));
5249 }
5250
5251 /* Check that completion mode works at each prefix of the expected
5252 symbol name. */
5253 {
5254 static const char str[] = "function(int)";
5255 size_t len = strlen (str);
5256 std::string lookup;
5257
5258 for (size_t i = 1; i < len; i++)
5259 {
5260 lookup.assign (str, i);
5261 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5262 EXPECT ("function"));
5263 }
5264 }
5265
5266 /* While "w" is a prefix of both components, the match function
5267 should still only be called once. */
5268 {
5269 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5270 EXPECT ("w1::w2"));
5271 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5272 EXPECT ("w1::w2"));
5273 }
5274
5275 /* Same, with a "complicated" symbol. */
5276 {
5277 static const char str[] = Z_SYM_NAME;
5278 size_t len = strlen (str);
5279 std::string lookup;
5280
5281 for (size_t i = 1; i < len; i++)
5282 {
5283 lookup.assign (str, i);
5284 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5285 EXPECT (Z_SYM_NAME));
5286 }
5287 }
5288
5289 /* In FULL mode, an incomplete symbol doesn't match. */
5290 {
5291 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5292 {});
5293 }
5294
5295 /* A complete symbol with parameters matches any overload, since the
5296 index has no overload info. */
5297 {
5298 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5299 EXPECT ("std::zfunction", "std::zfunction2"));
5300 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5301 EXPECT ("std::zfunction", "std::zfunction2"));
5302 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5303 EXPECT ("std::zfunction", "std::zfunction2"));
5304 }
5305
5306 /* Check that whitespace is ignored appropriately. A symbol with a
5307 template argument list. */
5308 {
5309 static const char expected[] = "ns::foo<int>";
5310 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5311 EXPECT (expected));
5312 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5313 EXPECT (expected));
5314 }
5315
5316 /* Check that whitespace is ignored appropriately. A symbol with a
5317 template argument list that includes a pointer. */
5318 {
5319 static const char expected[] = "ns::foo<char*>";
5320 /* Try both completion and non-completion modes. */
5321 static const bool completion_mode[2] = {false, true};
5322 for (size_t i = 0; i < 2; i++)
5323 {
5324 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5325 completion_mode[i], EXPECT (expected));
5326 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5327 completion_mode[i], EXPECT (expected));
5328
5329 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5330 completion_mode[i], EXPECT (expected));
5331 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5332 completion_mode[i], EXPECT (expected));
5333 }
5334 }
5335
5336 {
5337 /* Check method qualifiers are ignored. */
5338 static const char expected[] = "ns::foo<char*>";
5339 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5340 symbol_name_match_type::FULL, true, EXPECT (expected));
5341 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5342 symbol_name_match_type::FULL, true, EXPECT (expected));
5343 CHECK_MATCH ("foo < char * > ( int ) const",
5344 symbol_name_match_type::WILD, true, EXPECT (expected));
5345 CHECK_MATCH ("foo < char * > ( int ) &&",
5346 symbol_name_match_type::WILD, true, EXPECT (expected));
5347 }
5348
5349 /* Test lookup names that don't match anything. */
5350 {
5351 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5352 {});
5353
5354 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5355 {});
5356 }
5357
5358 /* Some wild matching tests, exercising "(anonymous namespace)",
5359 which should not be confused with a parameter list. */
5360 {
5361 static const char *syms[] = {
5362 "A::B::C",
5363 "B::C",
5364 "C",
5365 "A :: B :: C ( int )",
5366 "B :: C ( int )",
5367 "C ( int )",
5368 };
5369
5370 for (const char *s : syms)
5371 {
5372 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5373 EXPECT ("(anonymous namespace)::A::B::C"));
5374 }
5375 }
5376
5377 {
5378 static const char expected[] = "ns2::tmpl<int>::foo2";
5379 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5380 EXPECT (expected));
5381 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5382 EXPECT (expected));
5383 }
5384
5385 SELF_CHECK (!any_mismatch);
5386
5387 #undef EXPECT
5388 #undef CHECK_MATCH
5389 }
5390
5391 static void
5392 run_test ()
5393 {
5394 test_mapped_index_find_name_component_bounds ();
5395 test_dw2_expand_symtabs_matching_symbol ();
5396 }
5397
5398 }} // namespace selftests::dw2_expand_symtabs_matching
5399
5400 #endif /* GDB_SELF_TEST */
5401
5402 /* If FILE_MATCHER is NULL or if PER_CU has
5403 dwarf2_per_cu_quick_data::MARK set (see
5404 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5405 EXPANSION_NOTIFY on it. */
5406
5407 static void
5408 dw2_expand_symtabs_matching_one
5409 (struct dwarf2_per_cu_data *per_cu,
5410 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5411 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5412 {
5413 if (file_matcher == NULL || per_cu->v.quick->mark)
5414 {
5415 bool symtab_was_null
5416 = (per_cu->v.quick->compunit_symtab == NULL);
5417
5418 dw2_instantiate_symtab (per_cu);
5419
5420 if (expansion_notify != NULL
5421 && symtab_was_null
5422 && per_cu->v.quick->compunit_symtab != NULL)
5423 expansion_notify (per_cu->v.quick->compunit_symtab);
5424 }
5425 }
5426
5427 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5428 matched, to expand corresponding CUs that were marked. IDX is the
5429 index of the symbol name that matched. */
5430
5431 static void
5432 dw2_expand_marked_cus
5433 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5434 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5435 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5436 search_domain kind)
5437 {
5438 offset_type *vec, vec_len, vec_idx;
5439 bool global_seen = false;
5440 mapped_index &index = *dwarf2_per_objfile->index_table;
5441
5442 vec = (offset_type *) (index.constant_pool
5443 + MAYBE_SWAP (index.symbol_table[idx].vec));
5444 vec_len = MAYBE_SWAP (vec[0]);
5445 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5446 {
5447 struct dwarf2_per_cu_data *per_cu;
5448 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5449 /* This value is only valid for index versions >= 7. */
5450 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5451 gdb_index_symbol_kind symbol_kind =
5452 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5453 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5454 /* Only check the symbol attributes if they're present.
5455 Indices prior to version 7 don't record them,
5456 and indices >= 7 may elide them for certain symbols
5457 (gold does this). */
5458 int attrs_valid =
5459 (index.version >= 7
5460 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5461
5462 /* Work around gold/15646. */
5463 if (attrs_valid)
5464 {
5465 if (!is_static && global_seen)
5466 continue;
5467 if (!is_static)
5468 global_seen = true;
5469 }
5470
5471 /* Only check the symbol's kind if it has one. */
5472 if (attrs_valid)
5473 {
5474 switch (kind)
5475 {
5476 case VARIABLES_DOMAIN:
5477 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5478 continue;
5479 break;
5480 case FUNCTIONS_DOMAIN:
5481 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5482 continue;
5483 break;
5484 case TYPES_DOMAIN:
5485 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5486 continue;
5487 break;
5488 default:
5489 break;
5490 }
5491 }
5492
5493 /* Don't crash on bad data. */
5494 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5495 + dwarf2_per_objfile->n_type_units))
5496 {
5497 complaint (&symfile_complaints,
5498 _(".gdb_index entry has bad CU index"
5499 " [in module %s]"),
5500 objfile_name (dwarf2_per_objfile->objfile));
5501 continue;
5502 }
5503
5504 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
5505 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5506 expansion_notify);
5507 }
5508 }
5509
5510 /* If FILE_MATCHER is non-NULL, set all the
5511 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5512 that match FILE_MATCHER. */
5513
5514 static void
5515 dw_expand_symtabs_matching_file_matcher
5516 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5517 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5518 {
5519 if (file_matcher == NULL)
5520 return;
5521
5522 objfile *const objfile = dwarf2_per_objfile->objfile;
5523
5524 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5525 htab_eq_pointer,
5526 NULL, xcalloc, xfree));
5527 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5528 htab_eq_pointer,
5529 NULL, xcalloc, xfree));
5530
5531 /* The rule is CUs specify all the files, including those used by
5532 any TU, so there's no need to scan TUs here. */
5533
5534 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5535 {
5536 int j;
5537 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5538 struct quick_file_names *file_data;
5539 void **slot;
5540
5541 QUIT;
5542
5543 per_cu->v.quick->mark = 0;
5544
5545 /* We only need to look at symtabs not already expanded. */
5546 if (per_cu->v.quick->compunit_symtab)
5547 continue;
5548
5549 file_data = dw2_get_file_names (per_cu);
5550 if (file_data == NULL)
5551 continue;
5552
5553 if (htab_find (visited_not_found.get (), file_data) != NULL)
5554 continue;
5555 else if (htab_find (visited_found.get (), file_data) != NULL)
5556 {
5557 per_cu->v.quick->mark = 1;
5558 continue;
5559 }
5560
5561 for (j = 0; j < file_data->num_file_names; ++j)
5562 {
5563 const char *this_real_name;
5564
5565 if (file_matcher (file_data->file_names[j], false))
5566 {
5567 per_cu->v.quick->mark = 1;
5568 break;
5569 }
5570
5571 /* Before we invoke realpath, which can get expensive when many
5572 files are involved, do a quick comparison of the basenames. */
5573 if (!basenames_may_differ
5574 && !file_matcher (lbasename (file_data->file_names[j]),
5575 true))
5576 continue;
5577
5578 this_real_name = dw2_get_real_path (objfile, file_data, j);
5579 if (file_matcher (this_real_name, false))
5580 {
5581 per_cu->v.quick->mark = 1;
5582 break;
5583 }
5584 }
5585
5586 slot = htab_find_slot (per_cu->v.quick->mark
5587 ? visited_found.get ()
5588 : visited_not_found.get (),
5589 file_data, INSERT);
5590 *slot = file_data;
5591 }
5592 }
5593
5594 static void
5595 dw2_expand_symtabs_matching
5596 (struct objfile *objfile,
5597 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5598 const lookup_name_info &lookup_name,
5599 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5600 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5601 enum search_domain kind)
5602 {
5603 struct dwarf2_per_objfile *dwarf2_per_objfile
5604 = get_dwarf2_per_objfile (objfile);
5605
5606 /* index_table is NULL if OBJF_READNOW. */
5607 if (!dwarf2_per_objfile->index_table)
5608 return;
5609
5610 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5611
5612 mapped_index &index = *dwarf2_per_objfile->index_table;
5613
5614 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5615 symbol_matcher,
5616 kind, [&] (offset_type idx)
5617 {
5618 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5619 expansion_notify, kind);
5620 });
5621 }
5622
5623 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5624 symtab. */
5625
5626 static struct compunit_symtab *
5627 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5628 CORE_ADDR pc)
5629 {
5630 int i;
5631
5632 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5633 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5634 return cust;
5635
5636 if (cust->includes == NULL)
5637 return NULL;
5638
5639 for (i = 0; cust->includes[i]; ++i)
5640 {
5641 struct compunit_symtab *s = cust->includes[i];
5642
5643 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5644 if (s != NULL)
5645 return s;
5646 }
5647
5648 return NULL;
5649 }
5650
5651 static struct compunit_symtab *
5652 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5653 struct bound_minimal_symbol msymbol,
5654 CORE_ADDR pc,
5655 struct obj_section *section,
5656 int warn_if_readin)
5657 {
5658 struct dwarf2_per_cu_data *data;
5659 struct compunit_symtab *result;
5660
5661 if (!objfile->psymtabs_addrmap)
5662 return NULL;
5663
5664 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5665 pc);
5666 if (!data)
5667 return NULL;
5668
5669 if (warn_if_readin && data->v.quick->compunit_symtab)
5670 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5671 paddress (get_objfile_arch (objfile), pc));
5672
5673 result
5674 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5675 pc);
5676 gdb_assert (result != NULL);
5677 return result;
5678 }
5679
5680 static void
5681 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5682 void *data, int need_fullname)
5683 {
5684 struct dwarf2_per_objfile *dwarf2_per_objfile
5685 = get_dwarf2_per_objfile (objfile);
5686
5687 if (!dwarf2_per_objfile->filenames_cache)
5688 {
5689 dwarf2_per_objfile->filenames_cache.emplace ();
5690
5691 htab_up visited (htab_create_alloc (10,
5692 htab_hash_pointer, htab_eq_pointer,
5693 NULL, xcalloc, xfree));
5694
5695 /* The rule is CUs specify all the files, including those used
5696 by any TU, so there's no need to scan TUs here. We can
5697 ignore file names coming from already-expanded CUs. */
5698
5699 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5700 {
5701 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
5702
5703 if (per_cu->v.quick->compunit_symtab)
5704 {
5705 void **slot = htab_find_slot (visited.get (),
5706 per_cu->v.quick->file_names,
5707 INSERT);
5708
5709 *slot = per_cu->v.quick->file_names;
5710 }
5711 }
5712
5713 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5714 {
5715 dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5716 struct quick_file_names *file_data;
5717 void **slot;
5718
5719 /* We only need to look at symtabs not already expanded. */
5720 if (per_cu->v.quick->compunit_symtab)
5721 continue;
5722
5723 file_data = dw2_get_file_names (per_cu);
5724 if (file_data == NULL)
5725 continue;
5726
5727 slot = htab_find_slot (visited.get (), file_data, INSERT);
5728 if (*slot)
5729 {
5730 /* Already visited. */
5731 continue;
5732 }
5733 *slot = file_data;
5734
5735 for (int j = 0; j < file_data->num_file_names; ++j)
5736 {
5737 const char *filename = file_data->file_names[j];
5738 dwarf2_per_objfile->filenames_cache->seen (filename);
5739 }
5740 }
5741 }
5742
5743 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5744 {
5745 gdb::unique_xmalloc_ptr<char> this_real_name;
5746
5747 if (need_fullname)
5748 this_real_name = gdb_realpath (filename);
5749 (*fun) (filename, this_real_name.get (), data);
5750 });
5751 }
5752
5753 static int
5754 dw2_has_symbols (struct objfile *objfile)
5755 {
5756 return 1;
5757 }
5758
5759 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5760 {
5761 dw2_has_symbols,
5762 dw2_find_last_source_symtab,
5763 dw2_forget_cached_source_info,
5764 dw2_map_symtabs_matching_filename,
5765 dw2_lookup_symbol,
5766 dw2_print_stats,
5767 dw2_dump,
5768 dw2_relocate,
5769 dw2_expand_symtabs_for_function,
5770 dw2_expand_all_symtabs,
5771 dw2_expand_symtabs_with_fullname,
5772 dw2_map_matching_symbols,
5773 dw2_expand_symtabs_matching,
5774 dw2_find_pc_sect_compunit_symtab,
5775 NULL,
5776 dw2_map_symbol_filenames
5777 };
5778
5779 /* DWARF-5 debug_names reader. */
5780
5781 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5782 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5783
5784 /* A helper function that reads the .debug_names section in SECTION
5785 and fills in MAP. FILENAME is the name of the file containing the
5786 section; it is used for error reporting.
5787
5788 Returns true if all went well, false otherwise. */
5789
5790 static bool
5791 read_debug_names_from_section (struct objfile *objfile,
5792 const char *filename,
5793 struct dwarf2_section_info *section,
5794 mapped_debug_names &map)
5795 {
5796 if (dwarf2_section_empty_p (section))
5797 return false;
5798
5799 /* Older elfutils strip versions could keep the section in the main
5800 executable while splitting it for the separate debug info file. */
5801 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5802 return false;
5803
5804 dwarf2_read_section (objfile, section);
5805
5806 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5807
5808 const gdb_byte *addr = section->buffer;
5809
5810 bfd *const abfd = get_section_bfd_owner (section);
5811
5812 unsigned int bytes_read;
5813 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5814 addr += bytes_read;
5815
5816 map.dwarf5_is_dwarf64 = bytes_read != 4;
5817 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5818 if (bytes_read + length != section->size)
5819 {
5820 /* There may be multiple per-CU indices. */
5821 warning (_("Section .debug_names in %s length %s does not match "
5822 "section length %s, ignoring .debug_names."),
5823 filename, plongest (bytes_read + length),
5824 pulongest (section->size));
5825 return false;
5826 }
5827
5828 /* The version number. */
5829 uint16_t version = read_2_bytes (abfd, addr);
5830 addr += 2;
5831 if (version != 5)
5832 {
5833 warning (_("Section .debug_names in %s has unsupported version %d, "
5834 "ignoring .debug_names."),
5835 filename, version);
5836 return false;
5837 }
5838
5839 /* Padding. */
5840 uint16_t padding = read_2_bytes (abfd, addr);
5841 addr += 2;
5842 if (padding != 0)
5843 {
5844 warning (_("Section .debug_names in %s has unsupported padding %d, "
5845 "ignoring .debug_names."),
5846 filename, padding);
5847 return false;
5848 }
5849
5850 /* comp_unit_count - The number of CUs in the CU list. */
5851 map.cu_count = read_4_bytes (abfd, addr);
5852 addr += 4;
5853
5854 /* local_type_unit_count - The number of TUs in the local TU
5855 list. */
5856 map.tu_count = read_4_bytes (abfd, addr);
5857 addr += 4;
5858
5859 /* foreign_type_unit_count - The number of TUs in the foreign TU
5860 list. */
5861 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5862 addr += 4;
5863 if (foreign_tu_count != 0)
5864 {
5865 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5866 "ignoring .debug_names."),
5867 filename, static_cast<unsigned long> (foreign_tu_count));
5868 return false;
5869 }
5870
5871 /* bucket_count - The number of hash buckets in the hash lookup
5872 table. */
5873 map.bucket_count = read_4_bytes (abfd, addr);
5874 addr += 4;
5875
5876 /* name_count - The number of unique names in the index. */
5877 map.name_count = read_4_bytes (abfd, addr);
5878 addr += 4;
5879
5880 /* abbrev_table_size - The size in bytes of the abbreviations
5881 table. */
5882 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5883 addr += 4;
5884
5885 /* augmentation_string_size - The size in bytes of the augmentation
5886 string. This value is rounded up to a multiple of 4. */
5887 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5888 addr += 4;
5889 map.augmentation_is_gdb = ((augmentation_string_size
5890 == sizeof (dwarf5_augmentation))
5891 && memcmp (addr, dwarf5_augmentation,
5892 sizeof (dwarf5_augmentation)) == 0);
5893 augmentation_string_size += (-augmentation_string_size) & 3;
5894 addr += augmentation_string_size;
5895
5896 /* List of CUs */
5897 map.cu_table_reordered = addr;
5898 addr += map.cu_count * map.offset_size;
5899
5900 /* List of Local TUs */
5901 map.tu_table_reordered = addr;
5902 addr += map.tu_count * map.offset_size;
5903
5904 /* Hash Lookup Table */
5905 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5906 addr += map.bucket_count * 4;
5907 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5908 addr += map.name_count * 4;
5909
5910 /* Name Table */
5911 map.name_table_string_offs_reordered = addr;
5912 addr += map.name_count * map.offset_size;
5913 map.name_table_entry_offs_reordered = addr;
5914 addr += map.name_count * map.offset_size;
5915
5916 const gdb_byte *abbrev_table_start = addr;
5917 for (;;)
5918 {
5919 unsigned int bytes_read;
5920 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5921 addr += bytes_read;
5922 if (index_num == 0)
5923 break;
5924
5925 const auto insertpair
5926 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5927 if (!insertpair.second)
5928 {
5929 warning (_("Section .debug_names in %s has duplicate index %s, "
5930 "ignoring .debug_names."),
5931 filename, pulongest (index_num));
5932 return false;
5933 }
5934 mapped_debug_names::index_val &indexval = insertpair.first->second;
5935 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5936 addr += bytes_read;
5937
5938 for (;;)
5939 {
5940 mapped_debug_names::index_val::attr attr;
5941 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5942 addr += bytes_read;
5943 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5944 addr += bytes_read;
5945 if (attr.form == DW_FORM_implicit_const)
5946 {
5947 attr.implicit_const = read_signed_leb128 (abfd, addr,
5948 &bytes_read);
5949 addr += bytes_read;
5950 }
5951 if (attr.dw_idx == 0 && attr.form == 0)
5952 break;
5953 indexval.attr_vec.push_back (std::move (attr));
5954 }
5955 }
5956 if (addr != abbrev_table_start + abbrev_table_size)
5957 {
5958 warning (_("Section .debug_names in %s has abbreviation_table "
5959 "of size %zu vs. written as %u, ignoring .debug_names."),
5960 filename, addr - abbrev_table_start, abbrev_table_size);
5961 return false;
5962 }
5963 map.entry_pool = addr;
5964
5965 return true;
5966 }
5967
5968 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5969 list. */
5970
5971 static void
5972 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5973 const mapped_debug_names &map,
5974 dwarf2_section_info &section,
5975 bool is_dwz, int base_offset)
5976 {
5977 sect_offset sect_off_prev;
5978 for (uint32_t i = 0; i <= map.cu_count; ++i)
5979 {
5980 sect_offset sect_off_next;
5981 if (i < map.cu_count)
5982 {
5983 sect_off_next
5984 = (sect_offset) (extract_unsigned_integer
5985 (map.cu_table_reordered + i * map.offset_size,
5986 map.offset_size,
5987 map.dwarf5_byte_order));
5988 }
5989 else
5990 sect_off_next = (sect_offset) section.size;
5991 if (i >= 1)
5992 {
5993 const ULONGEST length = sect_off_next - sect_off_prev;
5994 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
5995 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5996 sect_off_prev, length);
5997 }
5998 sect_off_prev = sect_off_next;
5999 }
6000 }
6001
6002 /* Read the CU list from the mapped index, and use it to create all
6003 the CU objects for this dwarf2_per_objfile. */
6004
6005 static void
6006 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
6007 const mapped_debug_names &map,
6008 const mapped_debug_names &dwz_map)
6009 {
6010 struct objfile *objfile = dwarf2_per_objfile->objfile;
6011
6012 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
6013 dwarf2_per_objfile->all_comp_units
6014 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
6015 dwarf2_per_objfile->n_comp_units);
6016
6017 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
6018 dwarf2_per_objfile->info,
6019 false /* is_dwz */,
6020 0 /* base_offset */);
6021
6022 if (dwz_map.cu_count == 0)
6023 return;
6024
6025 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
6026 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
6027 true /* is_dwz */,
6028 map.cu_count /* base_offset */);
6029 }
6030
6031 /* Read .debug_names. If everything went ok, initialize the "quick"
6032 elements of all the CUs and return true. Otherwise, return false. */
6033
6034 static bool
6035 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
6036 {
6037 mapped_debug_names local_map (dwarf2_per_objfile);
6038 mapped_debug_names dwz_map (dwarf2_per_objfile);
6039 struct objfile *objfile = dwarf2_per_objfile->objfile;
6040
6041 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
6042 &dwarf2_per_objfile->debug_names,
6043 local_map))
6044 return false;
6045
6046 /* Don't use the index if it's empty. */
6047 if (local_map.name_count == 0)
6048 return false;
6049
6050 /* If there is a .dwz file, read it so we can get its CU list as
6051 well. */
6052 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
6053 if (dwz != NULL)
6054 {
6055 if (!read_debug_names_from_section (objfile,
6056 bfd_get_filename (dwz->dwz_bfd),
6057 &dwz->debug_names, dwz_map))
6058 {
6059 warning (_("could not read '.debug_names' section from %s; skipping"),
6060 bfd_get_filename (dwz->dwz_bfd));
6061 return false;
6062 }
6063 }
6064
6065 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
6066
6067 if (local_map.tu_count != 0)
6068 {
6069 /* We can only handle a single .debug_types when we have an
6070 index. */
6071 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
6072 return false;
6073
6074 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
6075 dwarf2_per_objfile->types, 0);
6076
6077 create_signatured_type_table_from_debug_names
6078 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
6079 }
6080
6081 create_addrmap_from_aranges (dwarf2_per_objfile,
6082 &dwarf2_per_objfile->debug_aranges);
6083
6084 dwarf2_per_objfile->debug_names_table.reset
6085 (new mapped_debug_names (dwarf2_per_objfile));
6086 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
6087 dwarf2_per_objfile->using_index = 1;
6088 dwarf2_per_objfile->quick_file_names_table =
6089 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6090
6091 return true;
6092 }
6093
6094 /* Symbol name hashing function as specified by DWARF-5. */
6095
6096 static uint32_t
6097 dwarf5_djb_hash (const char *str_)
6098 {
6099 const unsigned char *str = (const unsigned char *) str_;
6100
6101 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
6102 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
6103
6104 uint32_t hash = 5381;
6105 while (int c = *str++)
6106 hash = hash * 33 + tolower (c);
6107 return hash;
6108 }
6109
6110 /* Type used to manage iterating over all CUs looking for a symbol for
6111 .debug_names. */
6112
6113 class dw2_debug_names_iterator
6114 {
6115 public:
6116 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
6117 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
6118 dw2_debug_names_iterator (const mapped_debug_names &map,
6119 bool want_specific_block,
6120 block_enum block_index, domain_enum domain,
6121 const char *name)
6122 : m_map (map), m_want_specific_block (want_specific_block),
6123 m_block_index (block_index), m_domain (domain),
6124 m_addr (find_vec_in_debug_names (map, name))
6125 {}
6126
6127 dw2_debug_names_iterator (const mapped_debug_names &map,
6128 search_domain search, uint32_t namei)
6129 : m_map (map),
6130 m_search (search),
6131 m_addr (find_vec_in_debug_names (map, namei))
6132 {}
6133
6134 /* Return the next matching CU or NULL if there are no more. */
6135 dwarf2_per_cu_data *next ();
6136
6137 private:
6138 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6139 const char *name);
6140 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6141 uint32_t namei);
6142
6143 /* The internalized form of .debug_names. */
6144 const mapped_debug_names &m_map;
6145
6146 /* If true, only look for symbols that match BLOCK_INDEX. */
6147 const bool m_want_specific_block = false;
6148
6149 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
6150 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
6151 value. */
6152 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
6153
6154 /* The kind of symbol we're looking for. */
6155 const domain_enum m_domain = UNDEF_DOMAIN;
6156 const search_domain m_search = ALL_DOMAIN;
6157
6158 /* The list of CUs from the index entry of the symbol, or NULL if
6159 not found. */
6160 const gdb_byte *m_addr;
6161 };
6162
6163 const char *
6164 mapped_debug_names::namei_to_name (uint32_t namei) const
6165 {
6166 const ULONGEST namei_string_offs
6167 = extract_unsigned_integer ((name_table_string_offs_reordered
6168 + namei * offset_size),
6169 offset_size,
6170 dwarf5_byte_order);
6171 return read_indirect_string_at_offset
6172 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
6173 }
6174
6175 /* Find a slot in .debug_names for the object named NAME. If NAME is
6176 found, return pointer to its pool data. If NAME cannot be found,
6177 return NULL. */
6178
6179 const gdb_byte *
6180 dw2_debug_names_iterator::find_vec_in_debug_names
6181 (const mapped_debug_names &map, const char *name)
6182 {
6183 int (*cmp) (const char *, const char *);
6184
6185 if (current_language->la_language == language_cplus
6186 || current_language->la_language == language_fortran
6187 || current_language->la_language == language_d)
6188 {
6189 /* NAME is already canonical. Drop any qualifiers as
6190 .debug_names does not contain any. */
6191
6192 if (strchr (name, '(') != NULL)
6193 {
6194 gdb::unique_xmalloc_ptr<char> without_params
6195 = cp_remove_params (name);
6196
6197 if (without_params != NULL)
6198 {
6199 name = without_params.get();
6200 }
6201 }
6202 }
6203
6204 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6205
6206 const uint32_t full_hash = dwarf5_djb_hash (name);
6207 uint32_t namei
6208 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6209 (map.bucket_table_reordered
6210 + (full_hash % map.bucket_count)), 4,
6211 map.dwarf5_byte_order);
6212 if (namei == 0)
6213 return NULL;
6214 --namei;
6215 if (namei >= map.name_count)
6216 {
6217 complaint (&symfile_complaints,
6218 _("Wrong .debug_names with name index %u but name_count=%u "
6219 "[in module %s]"),
6220 namei, map.name_count,
6221 objfile_name (map.dwarf2_per_objfile->objfile));
6222 return NULL;
6223 }
6224
6225 for (;;)
6226 {
6227 const uint32_t namei_full_hash
6228 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6229 (map.hash_table_reordered + namei), 4,
6230 map.dwarf5_byte_order);
6231 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6232 return NULL;
6233
6234 if (full_hash == namei_full_hash)
6235 {
6236 const char *const namei_string = map.namei_to_name (namei);
6237
6238 #if 0 /* An expensive sanity check. */
6239 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6240 {
6241 complaint (&symfile_complaints,
6242 _("Wrong .debug_names hash for string at index %u "
6243 "[in module %s]"),
6244 namei, objfile_name (dwarf2_per_objfile->objfile));
6245 return NULL;
6246 }
6247 #endif
6248
6249 if (cmp (namei_string, name) == 0)
6250 {
6251 const ULONGEST namei_entry_offs
6252 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6253 + namei * map.offset_size),
6254 map.offset_size, map.dwarf5_byte_order);
6255 return map.entry_pool + namei_entry_offs;
6256 }
6257 }
6258
6259 ++namei;
6260 if (namei >= map.name_count)
6261 return NULL;
6262 }
6263 }
6264
6265 const gdb_byte *
6266 dw2_debug_names_iterator::find_vec_in_debug_names
6267 (const mapped_debug_names &map, uint32_t namei)
6268 {
6269 if (namei >= map.name_count)
6270 {
6271 complaint (&symfile_complaints,
6272 _("Wrong .debug_names with name index %u but name_count=%u "
6273 "[in module %s]"),
6274 namei, map.name_count,
6275 objfile_name (map.dwarf2_per_objfile->objfile));
6276 return NULL;
6277 }
6278
6279 const ULONGEST namei_entry_offs
6280 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6281 + namei * map.offset_size),
6282 map.offset_size, map.dwarf5_byte_order);
6283 return map.entry_pool + namei_entry_offs;
6284 }
6285
6286 /* See dw2_debug_names_iterator. */
6287
6288 dwarf2_per_cu_data *
6289 dw2_debug_names_iterator::next ()
6290 {
6291 if (m_addr == NULL)
6292 return NULL;
6293
6294 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
6295 struct objfile *objfile = dwarf2_per_objfile->objfile;
6296 bfd *const abfd = objfile->obfd;
6297
6298 again:
6299
6300 unsigned int bytes_read;
6301 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6302 m_addr += bytes_read;
6303 if (abbrev == 0)
6304 return NULL;
6305
6306 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6307 if (indexval_it == m_map.abbrev_map.cend ())
6308 {
6309 complaint (&symfile_complaints,
6310 _("Wrong .debug_names undefined abbrev code %s "
6311 "[in module %s]"),
6312 pulongest (abbrev), objfile_name (objfile));
6313 return NULL;
6314 }
6315 const mapped_debug_names::index_val &indexval = indexval_it->second;
6316 bool have_is_static = false;
6317 bool is_static;
6318 dwarf2_per_cu_data *per_cu = NULL;
6319 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6320 {
6321 ULONGEST ull;
6322 switch (attr.form)
6323 {
6324 case DW_FORM_implicit_const:
6325 ull = attr.implicit_const;
6326 break;
6327 case DW_FORM_flag_present:
6328 ull = 1;
6329 break;
6330 case DW_FORM_udata:
6331 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6332 m_addr += bytes_read;
6333 break;
6334 default:
6335 complaint (&symfile_complaints,
6336 _("Unsupported .debug_names form %s [in module %s]"),
6337 dwarf_form_name (attr.form),
6338 objfile_name (objfile));
6339 return NULL;
6340 }
6341 switch (attr.dw_idx)
6342 {
6343 case DW_IDX_compile_unit:
6344 /* Don't crash on bad data. */
6345 if (ull >= dwarf2_per_objfile->n_comp_units)
6346 {
6347 complaint (&symfile_complaints,
6348 _(".debug_names entry has bad CU index %s"
6349 " [in module %s]"),
6350 pulongest (ull),
6351 objfile_name (dwarf2_per_objfile->objfile));
6352 continue;
6353 }
6354 per_cu = dw2_get_cutu (dwarf2_per_objfile, ull);
6355 break;
6356 case DW_IDX_type_unit:
6357 /* Don't crash on bad data. */
6358 if (ull >= dwarf2_per_objfile->n_type_units)
6359 {
6360 complaint (&symfile_complaints,
6361 _(".debug_names entry has bad TU index %s"
6362 " [in module %s]"),
6363 pulongest (ull),
6364 objfile_name (dwarf2_per_objfile->objfile));
6365 continue;
6366 }
6367 per_cu = dw2_get_cutu (dwarf2_per_objfile,
6368 dwarf2_per_objfile->n_comp_units + ull);
6369 break;
6370 case DW_IDX_GNU_internal:
6371 if (!m_map.augmentation_is_gdb)
6372 break;
6373 have_is_static = true;
6374 is_static = true;
6375 break;
6376 case DW_IDX_GNU_external:
6377 if (!m_map.augmentation_is_gdb)
6378 break;
6379 have_is_static = true;
6380 is_static = false;
6381 break;
6382 }
6383 }
6384
6385 /* Skip if already read in. */
6386 if (per_cu->v.quick->compunit_symtab)
6387 goto again;
6388
6389 /* Check static vs global. */
6390 if (have_is_static)
6391 {
6392 const bool want_static = m_block_index != GLOBAL_BLOCK;
6393 if (m_want_specific_block && want_static != is_static)
6394 goto again;
6395 }
6396
6397 /* Match dw2_symtab_iter_next, symbol_kind
6398 and debug_names::psymbol_tag. */
6399 switch (m_domain)
6400 {
6401 case VAR_DOMAIN:
6402 switch (indexval.dwarf_tag)
6403 {
6404 case DW_TAG_variable:
6405 case DW_TAG_subprogram:
6406 /* Some types are also in VAR_DOMAIN. */
6407 case DW_TAG_typedef:
6408 case DW_TAG_structure_type:
6409 break;
6410 default:
6411 goto again;
6412 }
6413 break;
6414 case STRUCT_DOMAIN:
6415 switch (indexval.dwarf_tag)
6416 {
6417 case DW_TAG_typedef:
6418 case DW_TAG_structure_type:
6419 break;
6420 default:
6421 goto again;
6422 }
6423 break;
6424 case LABEL_DOMAIN:
6425 switch (indexval.dwarf_tag)
6426 {
6427 case 0:
6428 case DW_TAG_variable:
6429 break;
6430 default:
6431 goto again;
6432 }
6433 break;
6434 default:
6435 break;
6436 }
6437
6438 /* Match dw2_expand_symtabs_matching, symbol_kind and
6439 debug_names::psymbol_tag. */
6440 switch (m_search)
6441 {
6442 case VARIABLES_DOMAIN:
6443 switch (indexval.dwarf_tag)
6444 {
6445 case DW_TAG_variable:
6446 break;
6447 default:
6448 goto again;
6449 }
6450 break;
6451 case FUNCTIONS_DOMAIN:
6452 switch (indexval.dwarf_tag)
6453 {
6454 case DW_TAG_subprogram:
6455 break;
6456 default:
6457 goto again;
6458 }
6459 break;
6460 case TYPES_DOMAIN:
6461 switch (indexval.dwarf_tag)
6462 {
6463 case DW_TAG_typedef:
6464 case DW_TAG_structure_type:
6465 break;
6466 default:
6467 goto again;
6468 }
6469 break;
6470 default:
6471 break;
6472 }
6473
6474 return per_cu;
6475 }
6476
6477 static struct compunit_symtab *
6478 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6479 const char *name, domain_enum domain)
6480 {
6481 const block_enum block_index = static_cast<block_enum> (block_index_int);
6482 struct dwarf2_per_objfile *dwarf2_per_objfile
6483 = get_dwarf2_per_objfile (objfile);
6484
6485 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6486 if (!mapp)
6487 {
6488 /* index is NULL if OBJF_READNOW. */
6489 return NULL;
6490 }
6491 const auto &map = *mapp;
6492
6493 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6494 block_index, domain, name);
6495
6496 struct compunit_symtab *stab_best = NULL;
6497 struct dwarf2_per_cu_data *per_cu;
6498 while ((per_cu = iter.next ()) != NULL)
6499 {
6500 struct symbol *sym, *with_opaque = NULL;
6501 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6502 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6503 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6504
6505 sym = block_find_symbol (block, name, domain,
6506 block_find_non_opaque_type_preferred,
6507 &with_opaque);
6508
6509 /* Some caution must be observed with overloaded functions and
6510 methods, since the index will not contain any overload
6511 information (but NAME might contain it). */
6512
6513 if (sym != NULL
6514 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6515 return stab;
6516 if (with_opaque != NULL
6517 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6518 stab_best = stab;
6519
6520 /* Keep looking through other CUs. */
6521 }
6522
6523 return stab_best;
6524 }
6525
6526 /* This dumps minimal information about .debug_names. It is called
6527 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6528 uses this to verify that .debug_names has been loaded. */
6529
6530 static void
6531 dw2_debug_names_dump (struct objfile *objfile)
6532 {
6533 struct dwarf2_per_objfile *dwarf2_per_objfile
6534 = get_dwarf2_per_objfile (objfile);
6535
6536 gdb_assert (dwarf2_per_objfile->using_index);
6537 printf_filtered (".debug_names:");
6538 if (dwarf2_per_objfile->debug_names_table)
6539 printf_filtered (" exists\n");
6540 else
6541 printf_filtered (" faked for \"readnow\"\n");
6542 printf_filtered ("\n");
6543 }
6544
6545 static void
6546 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6547 const char *func_name)
6548 {
6549 struct dwarf2_per_objfile *dwarf2_per_objfile
6550 = get_dwarf2_per_objfile (objfile);
6551
6552 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6553 if (dwarf2_per_objfile->debug_names_table)
6554 {
6555 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6556
6557 /* Note: It doesn't matter what we pass for block_index here. */
6558 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6559 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6560
6561 struct dwarf2_per_cu_data *per_cu;
6562 while ((per_cu = iter.next ()) != NULL)
6563 dw2_instantiate_symtab (per_cu);
6564 }
6565 }
6566
6567 static void
6568 dw2_debug_names_expand_symtabs_matching
6569 (struct objfile *objfile,
6570 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6571 const lookup_name_info &lookup_name,
6572 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6573 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6574 enum search_domain kind)
6575 {
6576 struct dwarf2_per_objfile *dwarf2_per_objfile
6577 = get_dwarf2_per_objfile (objfile);
6578
6579 /* debug_names_table is NULL if OBJF_READNOW. */
6580 if (!dwarf2_per_objfile->debug_names_table)
6581 return;
6582
6583 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6584
6585 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6586
6587 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6588 symbol_matcher,
6589 kind, [&] (offset_type namei)
6590 {
6591 /* The name was matched, now expand corresponding CUs that were
6592 marked. */
6593 dw2_debug_names_iterator iter (map, kind, namei);
6594
6595 struct dwarf2_per_cu_data *per_cu;
6596 while ((per_cu = iter.next ()) != NULL)
6597 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6598 expansion_notify);
6599 });
6600 }
6601
6602 const struct quick_symbol_functions dwarf2_debug_names_functions =
6603 {
6604 dw2_has_symbols,
6605 dw2_find_last_source_symtab,
6606 dw2_forget_cached_source_info,
6607 dw2_map_symtabs_matching_filename,
6608 dw2_debug_names_lookup_symbol,
6609 dw2_print_stats,
6610 dw2_debug_names_dump,
6611 dw2_relocate,
6612 dw2_debug_names_expand_symtabs_for_function,
6613 dw2_expand_all_symtabs,
6614 dw2_expand_symtabs_with_fullname,
6615 dw2_map_matching_symbols,
6616 dw2_debug_names_expand_symtabs_matching,
6617 dw2_find_pc_sect_compunit_symtab,
6618 NULL,
6619 dw2_map_symbol_filenames
6620 };
6621
6622 /* See symfile.h. */
6623
6624 bool
6625 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6626 {
6627 struct dwarf2_per_objfile *dwarf2_per_objfile
6628 = get_dwarf2_per_objfile (objfile);
6629
6630 /* If we're about to read full symbols, don't bother with the
6631 indices. In this case we also don't care if some other debug
6632 format is making psymtabs, because they are all about to be
6633 expanded anyway. */
6634 if ((objfile->flags & OBJF_READNOW))
6635 {
6636 int i;
6637
6638 dwarf2_per_objfile->using_index = 1;
6639 create_all_comp_units (dwarf2_per_objfile);
6640 create_all_type_units (dwarf2_per_objfile);
6641 dwarf2_per_objfile->quick_file_names_table =
6642 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6643
6644 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
6645 + dwarf2_per_objfile->n_type_units); ++i)
6646 {
6647 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
6648
6649 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6650 struct dwarf2_per_cu_quick_data);
6651 }
6652
6653 /* Return 1 so that gdb sees the "quick" functions. However,
6654 these functions will be no-ops because we will have expanded
6655 all symtabs. */
6656 *index_kind = dw_index_kind::GDB_INDEX;
6657 return true;
6658 }
6659
6660 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6661 {
6662 *index_kind = dw_index_kind::DEBUG_NAMES;
6663 return true;
6664 }
6665
6666 if (dwarf2_read_index (objfile))
6667 {
6668 *index_kind = dw_index_kind::GDB_INDEX;
6669 return true;
6670 }
6671
6672 return false;
6673 }
6674
6675 \f
6676
6677 /* Build a partial symbol table. */
6678
6679 void
6680 dwarf2_build_psymtabs (struct objfile *objfile)
6681 {
6682 struct dwarf2_per_objfile *dwarf2_per_objfile
6683 = get_dwarf2_per_objfile (objfile);
6684
6685 if (objfile->global_psymbols.capacity () == 0
6686 && objfile->static_psymbols.capacity () == 0)
6687 init_psymbol_list (objfile, 1024);
6688
6689 TRY
6690 {
6691 /* This isn't really ideal: all the data we allocate on the
6692 objfile's obstack is still uselessly kept around. However,
6693 freeing it seems unsafe. */
6694 psymtab_discarder psymtabs (objfile);
6695 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6696 psymtabs.keep ();
6697 }
6698 CATCH (except, RETURN_MASK_ERROR)
6699 {
6700 exception_print (gdb_stderr, except);
6701 }
6702 END_CATCH
6703 }
6704
6705 /* Return the total length of the CU described by HEADER. */
6706
6707 static unsigned int
6708 get_cu_length (const struct comp_unit_head *header)
6709 {
6710 return header->initial_length_size + header->length;
6711 }
6712
6713 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6714
6715 static inline bool
6716 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6717 {
6718 sect_offset bottom = cu_header->sect_off;
6719 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6720
6721 return sect_off >= bottom && sect_off < top;
6722 }
6723
6724 /* Find the base address of the compilation unit for range lists and
6725 location lists. It will normally be specified by DW_AT_low_pc.
6726 In DWARF-3 draft 4, the base address could be overridden by
6727 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6728 compilation units with discontinuous ranges. */
6729
6730 static void
6731 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6732 {
6733 struct attribute *attr;
6734
6735 cu->base_known = 0;
6736 cu->base_address = 0;
6737
6738 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6739 if (attr)
6740 {
6741 cu->base_address = attr_value_as_address (attr);
6742 cu->base_known = 1;
6743 }
6744 else
6745 {
6746 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6747 if (attr)
6748 {
6749 cu->base_address = attr_value_as_address (attr);
6750 cu->base_known = 1;
6751 }
6752 }
6753 }
6754
6755 /* Read in the comp unit header information from the debug_info at info_ptr.
6756 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6757 NOTE: This leaves members offset, first_die_offset to be filled in
6758 by the caller. */
6759
6760 static const gdb_byte *
6761 read_comp_unit_head (struct comp_unit_head *cu_header,
6762 const gdb_byte *info_ptr,
6763 struct dwarf2_section_info *section,
6764 rcuh_kind section_kind)
6765 {
6766 int signed_addr;
6767 unsigned int bytes_read;
6768 const char *filename = get_section_file_name (section);
6769 bfd *abfd = get_section_bfd_owner (section);
6770
6771 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6772 cu_header->initial_length_size = bytes_read;
6773 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6774 info_ptr += bytes_read;
6775 cu_header->version = read_2_bytes (abfd, info_ptr);
6776 info_ptr += 2;
6777 if (cu_header->version < 5)
6778 switch (section_kind)
6779 {
6780 case rcuh_kind::COMPILE:
6781 cu_header->unit_type = DW_UT_compile;
6782 break;
6783 case rcuh_kind::TYPE:
6784 cu_header->unit_type = DW_UT_type;
6785 break;
6786 default:
6787 internal_error (__FILE__, __LINE__,
6788 _("read_comp_unit_head: invalid section_kind"));
6789 }
6790 else
6791 {
6792 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6793 (read_1_byte (abfd, info_ptr));
6794 info_ptr += 1;
6795 switch (cu_header->unit_type)
6796 {
6797 case DW_UT_compile:
6798 if (section_kind != rcuh_kind::COMPILE)
6799 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6800 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6801 filename);
6802 break;
6803 case DW_UT_type:
6804 section_kind = rcuh_kind::TYPE;
6805 break;
6806 default:
6807 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6808 "(is %d, should be %d or %d) [in module %s]"),
6809 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6810 }
6811
6812 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6813 info_ptr += 1;
6814 }
6815 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6816 cu_header,
6817 &bytes_read);
6818 info_ptr += bytes_read;
6819 if (cu_header->version < 5)
6820 {
6821 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6822 info_ptr += 1;
6823 }
6824 signed_addr = bfd_get_sign_extend_vma (abfd);
6825 if (signed_addr < 0)
6826 internal_error (__FILE__, __LINE__,
6827 _("read_comp_unit_head: dwarf from non elf file"));
6828 cu_header->signed_addr_p = signed_addr;
6829
6830 if (section_kind == rcuh_kind::TYPE)
6831 {
6832 LONGEST type_offset;
6833
6834 cu_header->signature = read_8_bytes (abfd, info_ptr);
6835 info_ptr += 8;
6836
6837 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6838 info_ptr += bytes_read;
6839 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6840 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6841 error (_("Dwarf Error: Too big type_offset in compilation unit "
6842 "header (is %s) [in module %s]"), plongest (type_offset),
6843 filename);
6844 }
6845
6846 return info_ptr;
6847 }
6848
6849 /* Helper function that returns the proper abbrev section for
6850 THIS_CU. */
6851
6852 static struct dwarf2_section_info *
6853 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6854 {
6855 struct dwarf2_section_info *abbrev;
6856 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6857
6858 if (this_cu->is_dwz)
6859 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6860 else
6861 abbrev = &dwarf2_per_objfile->abbrev;
6862
6863 return abbrev;
6864 }
6865
6866 /* Subroutine of read_and_check_comp_unit_head and
6867 read_and_check_type_unit_head to simplify them.
6868 Perform various error checking on the header. */
6869
6870 static void
6871 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6872 struct comp_unit_head *header,
6873 struct dwarf2_section_info *section,
6874 struct dwarf2_section_info *abbrev_section)
6875 {
6876 const char *filename = get_section_file_name (section);
6877
6878 if (header->version < 2 || header->version > 5)
6879 error (_("Dwarf Error: wrong version in compilation unit header "
6880 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6881 filename);
6882
6883 if (to_underlying (header->abbrev_sect_off)
6884 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6885 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6886 "(offset %s + 6) [in module %s]"),
6887 sect_offset_str (header->abbrev_sect_off),
6888 sect_offset_str (header->sect_off),
6889 filename);
6890
6891 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6892 avoid potential 32-bit overflow. */
6893 if (((ULONGEST) header->sect_off + get_cu_length (header))
6894 > section->size)
6895 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6896 "(offset %s + 0) [in module %s]"),
6897 header->length, sect_offset_str (header->sect_off),
6898 filename);
6899 }
6900
6901 /* Read in a CU/TU header and perform some basic error checking.
6902 The contents of the header are stored in HEADER.
6903 The result is a pointer to the start of the first DIE. */
6904
6905 static const gdb_byte *
6906 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6907 struct comp_unit_head *header,
6908 struct dwarf2_section_info *section,
6909 struct dwarf2_section_info *abbrev_section,
6910 const gdb_byte *info_ptr,
6911 rcuh_kind section_kind)
6912 {
6913 const gdb_byte *beg_of_comp_unit = info_ptr;
6914
6915 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6916
6917 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6918
6919 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6920
6921 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6922 abbrev_section);
6923
6924 return info_ptr;
6925 }
6926
6927 /* Fetch the abbreviation table offset from a comp or type unit header. */
6928
6929 static sect_offset
6930 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6931 struct dwarf2_section_info *section,
6932 sect_offset sect_off)
6933 {
6934 bfd *abfd = get_section_bfd_owner (section);
6935 const gdb_byte *info_ptr;
6936 unsigned int initial_length_size, offset_size;
6937 uint16_t version;
6938
6939 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6940 info_ptr = section->buffer + to_underlying (sect_off);
6941 read_initial_length (abfd, info_ptr, &initial_length_size);
6942 offset_size = initial_length_size == 4 ? 4 : 8;
6943 info_ptr += initial_length_size;
6944
6945 version = read_2_bytes (abfd, info_ptr);
6946 info_ptr += 2;
6947 if (version >= 5)
6948 {
6949 /* Skip unit type and address size. */
6950 info_ptr += 2;
6951 }
6952
6953 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6954 }
6955
6956 /* Allocate a new partial symtab for file named NAME and mark this new
6957 partial symtab as being an include of PST. */
6958
6959 static void
6960 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6961 struct objfile *objfile)
6962 {
6963 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6964
6965 if (!IS_ABSOLUTE_PATH (subpst->filename))
6966 {
6967 /* It shares objfile->objfile_obstack. */
6968 subpst->dirname = pst->dirname;
6969 }
6970
6971 subpst->textlow = 0;
6972 subpst->texthigh = 0;
6973
6974 subpst->dependencies
6975 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6976 subpst->dependencies[0] = pst;
6977 subpst->number_of_dependencies = 1;
6978
6979 subpst->globals_offset = 0;
6980 subpst->n_global_syms = 0;
6981 subpst->statics_offset = 0;
6982 subpst->n_static_syms = 0;
6983 subpst->compunit_symtab = NULL;
6984 subpst->read_symtab = pst->read_symtab;
6985 subpst->readin = 0;
6986
6987 /* No private part is necessary for include psymtabs. This property
6988 can be used to differentiate between such include psymtabs and
6989 the regular ones. */
6990 subpst->read_symtab_private = NULL;
6991 }
6992
6993 /* Read the Line Number Program data and extract the list of files
6994 included by the source file represented by PST. Build an include
6995 partial symtab for each of these included files. */
6996
6997 static void
6998 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6999 struct die_info *die,
7000 struct partial_symtab *pst)
7001 {
7002 line_header_up lh;
7003 struct attribute *attr;
7004
7005 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7006 if (attr)
7007 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
7008 if (lh == NULL)
7009 return; /* No linetable, so no includes. */
7010
7011 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
7012 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
7013 }
7014
7015 static hashval_t
7016 hash_signatured_type (const void *item)
7017 {
7018 const struct signatured_type *sig_type
7019 = (const struct signatured_type *) item;
7020
7021 /* This drops the top 32 bits of the signature, but is ok for a hash. */
7022 return sig_type->signature;
7023 }
7024
7025 static int
7026 eq_signatured_type (const void *item_lhs, const void *item_rhs)
7027 {
7028 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
7029 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
7030
7031 return lhs->signature == rhs->signature;
7032 }
7033
7034 /* Allocate a hash table for signatured types. */
7035
7036 static htab_t
7037 allocate_signatured_type_table (struct objfile *objfile)
7038 {
7039 return htab_create_alloc_ex (41,
7040 hash_signatured_type,
7041 eq_signatured_type,
7042 NULL,
7043 &objfile->objfile_obstack,
7044 hashtab_obstack_allocate,
7045 dummy_obstack_deallocate);
7046 }
7047
7048 /* A helper function to add a signatured type CU to a table. */
7049
7050 static int
7051 add_signatured_type_cu_to_table (void **slot, void *datum)
7052 {
7053 struct signatured_type *sigt = (struct signatured_type *) *slot;
7054 struct signatured_type ***datap = (struct signatured_type ***) datum;
7055
7056 **datap = sigt;
7057 ++*datap;
7058
7059 return 1;
7060 }
7061
7062 /* A helper for create_debug_types_hash_table. Read types from SECTION
7063 and fill them into TYPES_HTAB. It will process only type units,
7064 therefore DW_UT_type. */
7065
7066 static void
7067 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7068 struct dwo_file *dwo_file,
7069 dwarf2_section_info *section, htab_t &types_htab,
7070 rcuh_kind section_kind)
7071 {
7072 struct objfile *objfile = dwarf2_per_objfile->objfile;
7073 struct dwarf2_section_info *abbrev_section;
7074 bfd *abfd;
7075 const gdb_byte *info_ptr, *end_ptr;
7076
7077 abbrev_section = (dwo_file != NULL
7078 ? &dwo_file->sections.abbrev
7079 : &dwarf2_per_objfile->abbrev);
7080
7081 if (dwarf_read_debug)
7082 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
7083 get_section_name (section),
7084 get_section_file_name (abbrev_section));
7085
7086 dwarf2_read_section (objfile, section);
7087 info_ptr = section->buffer;
7088
7089 if (info_ptr == NULL)
7090 return;
7091
7092 /* We can't set abfd until now because the section may be empty or
7093 not present, in which case the bfd is unknown. */
7094 abfd = get_section_bfd_owner (section);
7095
7096 /* We don't use init_cutu_and_read_dies_simple, or some such, here
7097 because we don't need to read any dies: the signature is in the
7098 header. */
7099
7100 end_ptr = info_ptr + section->size;
7101 while (info_ptr < end_ptr)
7102 {
7103 struct signatured_type *sig_type;
7104 struct dwo_unit *dwo_tu;
7105 void **slot;
7106 const gdb_byte *ptr = info_ptr;
7107 struct comp_unit_head header;
7108 unsigned int length;
7109
7110 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
7111
7112 /* Initialize it due to a false compiler warning. */
7113 header.signature = -1;
7114 header.type_cu_offset_in_tu = (cu_offset) -1;
7115
7116 /* We need to read the type's signature in order to build the hash
7117 table, but we don't need anything else just yet. */
7118
7119 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
7120 abbrev_section, ptr, section_kind);
7121
7122 length = get_cu_length (&header);
7123
7124 /* Skip dummy type units. */
7125 if (ptr >= info_ptr + length
7126 || peek_abbrev_code (abfd, ptr) == 0
7127 || header.unit_type != DW_UT_type)
7128 {
7129 info_ptr += length;
7130 continue;
7131 }
7132
7133 if (types_htab == NULL)
7134 {
7135 if (dwo_file)
7136 types_htab = allocate_dwo_unit_table (objfile);
7137 else
7138 types_htab = allocate_signatured_type_table (objfile);
7139 }
7140
7141 if (dwo_file)
7142 {
7143 sig_type = NULL;
7144 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7145 struct dwo_unit);
7146 dwo_tu->dwo_file = dwo_file;
7147 dwo_tu->signature = header.signature;
7148 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
7149 dwo_tu->section = section;
7150 dwo_tu->sect_off = sect_off;
7151 dwo_tu->length = length;
7152 }
7153 else
7154 {
7155 /* N.B.: type_offset is not usable if this type uses a DWO file.
7156 The real type_offset is in the DWO file. */
7157 dwo_tu = NULL;
7158 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7159 struct signatured_type);
7160 sig_type->signature = header.signature;
7161 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
7162 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7163 sig_type->per_cu.is_debug_types = 1;
7164 sig_type->per_cu.section = section;
7165 sig_type->per_cu.sect_off = sect_off;
7166 sig_type->per_cu.length = length;
7167 }
7168
7169 slot = htab_find_slot (types_htab,
7170 dwo_file ? (void*) dwo_tu : (void *) sig_type,
7171 INSERT);
7172 gdb_assert (slot != NULL);
7173 if (*slot != NULL)
7174 {
7175 sect_offset dup_sect_off;
7176
7177 if (dwo_file)
7178 {
7179 const struct dwo_unit *dup_tu
7180 = (const struct dwo_unit *) *slot;
7181
7182 dup_sect_off = dup_tu->sect_off;
7183 }
7184 else
7185 {
7186 const struct signatured_type *dup_tu
7187 = (const struct signatured_type *) *slot;
7188
7189 dup_sect_off = dup_tu->per_cu.sect_off;
7190 }
7191
7192 complaint (&symfile_complaints,
7193 _("debug type entry at offset %s is duplicate to"
7194 " the entry at offset %s, signature %s"),
7195 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
7196 hex_string (header.signature));
7197 }
7198 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
7199
7200 if (dwarf_read_debug > 1)
7201 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
7202 sect_offset_str (sect_off),
7203 hex_string (header.signature));
7204
7205 info_ptr += length;
7206 }
7207 }
7208
7209 /* Create the hash table of all entries in the .debug_types
7210 (or .debug_types.dwo) section(s).
7211 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7212 otherwise it is NULL.
7213
7214 The result is a pointer to the hash table or NULL if there are no types.
7215
7216 Note: This function processes DWO files only, not DWP files. */
7217
7218 static void
7219 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7220 struct dwo_file *dwo_file,
7221 VEC (dwarf2_section_info_def) *types,
7222 htab_t &types_htab)
7223 {
7224 int ix;
7225 struct dwarf2_section_info *section;
7226
7227 if (VEC_empty (dwarf2_section_info_def, types))
7228 return;
7229
7230 for (ix = 0;
7231 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7232 ++ix)
7233 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
7234 types_htab, rcuh_kind::TYPE);
7235 }
7236
7237 /* Create the hash table of all entries in the .debug_types section,
7238 and initialize all_type_units.
7239 The result is zero if there is an error (e.g. missing .debug_types section),
7240 otherwise non-zero. */
7241
7242 static int
7243 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7244 {
7245 htab_t types_htab = NULL;
7246 struct signatured_type **iter;
7247
7248 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
7249 &dwarf2_per_objfile->info, types_htab,
7250 rcuh_kind::COMPILE);
7251 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
7252 dwarf2_per_objfile->types, types_htab);
7253 if (types_htab == NULL)
7254 {
7255 dwarf2_per_objfile->signatured_types = NULL;
7256 return 0;
7257 }
7258
7259 dwarf2_per_objfile->signatured_types = types_htab;
7260
7261 dwarf2_per_objfile->n_type_units
7262 = dwarf2_per_objfile->n_allocated_type_units
7263 = htab_elements (types_htab);
7264 dwarf2_per_objfile->all_type_units =
7265 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
7266 iter = &dwarf2_per_objfile->all_type_units[0];
7267 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7268 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7269 == dwarf2_per_objfile->n_type_units);
7270
7271 return 1;
7272 }
7273
7274 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7275 If SLOT is non-NULL, it is the entry to use in the hash table.
7276 Otherwise we find one. */
7277
7278 static struct signatured_type *
7279 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
7280 void **slot)
7281 {
7282 struct objfile *objfile = dwarf2_per_objfile->objfile;
7283 int n_type_units = dwarf2_per_objfile->n_type_units;
7284 struct signatured_type *sig_type;
7285
7286 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7287 ++n_type_units;
7288 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7289 {
7290 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7291 dwarf2_per_objfile->n_allocated_type_units = 1;
7292 dwarf2_per_objfile->n_allocated_type_units *= 2;
7293 dwarf2_per_objfile->all_type_units
7294 = XRESIZEVEC (struct signatured_type *,
7295 dwarf2_per_objfile->all_type_units,
7296 dwarf2_per_objfile->n_allocated_type_units);
7297 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7298 }
7299 dwarf2_per_objfile->n_type_units = n_type_units;
7300
7301 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7302 struct signatured_type);
7303 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7304 sig_type->signature = sig;
7305 sig_type->per_cu.is_debug_types = 1;
7306 if (dwarf2_per_objfile->using_index)
7307 {
7308 sig_type->per_cu.v.quick =
7309 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7310 struct dwarf2_per_cu_quick_data);
7311 }
7312
7313 if (slot == NULL)
7314 {
7315 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7316 sig_type, INSERT);
7317 }
7318 gdb_assert (*slot == NULL);
7319 *slot = sig_type;
7320 /* The rest of sig_type must be filled in by the caller. */
7321 return sig_type;
7322 }
7323
7324 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7325 Fill in SIG_ENTRY with DWO_ENTRY. */
7326
7327 static void
7328 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
7329 struct signatured_type *sig_entry,
7330 struct dwo_unit *dwo_entry)
7331 {
7332 /* Make sure we're not clobbering something we don't expect to. */
7333 gdb_assert (! sig_entry->per_cu.queued);
7334 gdb_assert (sig_entry->per_cu.cu == NULL);
7335 if (dwarf2_per_objfile->using_index)
7336 {
7337 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7338 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7339 }
7340 else
7341 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7342 gdb_assert (sig_entry->signature == dwo_entry->signature);
7343 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7344 gdb_assert (sig_entry->type_unit_group == NULL);
7345 gdb_assert (sig_entry->dwo_unit == NULL);
7346
7347 sig_entry->per_cu.section = dwo_entry->section;
7348 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7349 sig_entry->per_cu.length = dwo_entry->length;
7350 sig_entry->per_cu.reading_dwo_directly = 1;
7351 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7352 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7353 sig_entry->dwo_unit = dwo_entry;
7354 }
7355
7356 /* Subroutine of lookup_signatured_type.
7357 If we haven't read the TU yet, create the signatured_type data structure
7358 for a TU to be read in directly from a DWO file, bypassing the stub.
7359 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7360 using .gdb_index, then when reading a CU we want to stay in the DWO file
7361 containing that CU. Otherwise we could end up reading several other DWO
7362 files (due to comdat folding) to process the transitive closure of all the
7363 mentioned TUs, and that can be slow. The current DWO file will have every
7364 type signature that it needs.
7365 We only do this for .gdb_index because in the psymtab case we already have
7366 to read all the DWOs to build the type unit groups. */
7367
7368 static struct signatured_type *
7369 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7370 {
7371 struct dwarf2_per_objfile *dwarf2_per_objfile
7372 = cu->per_cu->dwarf2_per_objfile;
7373 struct objfile *objfile = dwarf2_per_objfile->objfile;
7374 struct dwo_file *dwo_file;
7375 struct dwo_unit find_dwo_entry, *dwo_entry;
7376 struct signatured_type find_sig_entry, *sig_entry;
7377 void **slot;
7378
7379 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7380
7381 /* If TU skeletons have been removed then we may not have read in any
7382 TUs yet. */
7383 if (dwarf2_per_objfile->signatured_types == NULL)
7384 {
7385 dwarf2_per_objfile->signatured_types
7386 = allocate_signatured_type_table (objfile);
7387 }
7388
7389 /* We only ever need to read in one copy of a signatured type.
7390 Use the global signatured_types array to do our own comdat-folding
7391 of types. If this is the first time we're reading this TU, and
7392 the TU has an entry in .gdb_index, replace the recorded data from
7393 .gdb_index with this TU. */
7394
7395 find_sig_entry.signature = sig;
7396 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7397 &find_sig_entry, INSERT);
7398 sig_entry = (struct signatured_type *) *slot;
7399
7400 /* We can get here with the TU already read, *or* in the process of being
7401 read. Don't reassign the global entry to point to this DWO if that's
7402 the case. Also note that if the TU is already being read, it may not
7403 have come from a DWO, the program may be a mix of Fission-compiled
7404 code and non-Fission-compiled code. */
7405
7406 /* Have we already tried to read this TU?
7407 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7408 needn't exist in the global table yet). */
7409 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7410 return sig_entry;
7411
7412 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7413 dwo_unit of the TU itself. */
7414 dwo_file = cu->dwo_unit->dwo_file;
7415
7416 /* Ok, this is the first time we're reading this TU. */
7417 if (dwo_file->tus == NULL)
7418 return NULL;
7419 find_dwo_entry.signature = sig;
7420 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7421 if (dwo_entry == NULL)
7422 return NULL;
7423
7424 /* If the global table doesn't have an entry for this TU, add one. */
7425 if (sig_entry == NULL)
7426 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7427
7428 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7429 sig_entry->per_cu.tu_read = 1;
7430 return sig_entry;
7431 }
7432
7433 /* Subroutine of lookup_signatured_type.
7434 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7435 then try the DWP file. If the TU stub (skeleton) has been removed then
7436 it won't be in .gdb_index. */
7437
7438 static struct signatured_type *
7439 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7440 {
7441 struct dwarf2_per_objfile *dwarf2_per_objfile
7442 = cu->per_cu->dwarf2_per_objfile;
7443 struct objfile *objfile = dwarf2_per_objfile->objfile;
7444 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7445 struct dwo_unit *dwo_entry;
7446 struct signatured_type find_sig_entry, *sig_entry;
7447 void **slot;
7448
7449 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7450 gdb_assert (dwp_file != NULL);
7451
7452 /* If TU skeletons have been removed then we may not have read in any
7453 TUs yet. */
7454 if (dwarf2_per_objfile->signatured_types == NULL)
7455 {
7456 dwarf2_per_objfile->signatured_types
7457 = allocate_signatured_type_table (objfile);
7458 }
7459
7460 find_sig_entry.signature = sig;
7461 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7462 &find_sig_entry, INSERT);
7463 sig_entry = (struct signatured_type *) *slot;
7464
7465 /* Have we already tried to read this TU?
7466 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7467 needn't exist in the global table yet). */
7468 if (sig_entry != NULL)
7469 return sig_entry;
7470
7471 if (dwp_file->tus == NULL)
7472 return NULL;
7473 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7474 sig, 1 /* is_debug_types */);
7475 if (dwo_entry == NULL)
7476 return NULL;
7477
7478 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7479 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7480
7481 return sig_entry;
7482 }
7483
7484 /* Lookup a signature based type for DW_FORM_ref_sig8.
7485 Returns NULL if signature SIG is not present in the table.
7486 It is up to the caller to complain about this. */
7487
7488 static struct signatured_type *
7489 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7490 {
7491 struct dwarf2_per_objfile *dwarf2_per_objfile
7492 = cu->per_cu->dwarf2_per_objfile;
7493
7494 if (cu->dwo_unit
7495 && dwarf2_per_objfile->using_index)
7496 {
7497 /* We're in a DWO/DWP file, and we're using .gdb_index.
7498 These cases require special processing. */
7499 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7500 return lookup_dwo_signatured_type (cu, sig);
7501 else
7502 return lookup_dwp_signatured_type (cu, sig);
7503 }
7504 else
7505 {
7506 struct signatured_type find_entry, *entry;
7507
7508 if (dwarf2_per_objfile->signatured_types == NULL)
7509 return NULL;
7510 find_entry.signature = sig;
7511 entry = ((struct signatured_type *)
7512 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7513 return entry;
7514 }
7515 }
7516 \f
7517 /* Low level DIE reading support. */
7518
7519 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7520
7521 static void
7522 init_cu_die_reader (struct die_reader_specs *reader,
7523 struct dwarf2_cu *cu,
7524 struct dwarf2_section_info *section,
7525 struct dwo_file *dwo_file,
7526 struct abbrev_table *abbrev_table)
7527 {
7528 gdb_assert (section->readin && section->buffer != NULL);
7529 reader->abfd = get_section_bfd_owner (section);
7530 reader->cu = cu;
7531 reader->dwo_file = dwo_file;
7532 reader->die_section = section;
7533 reader->buffer = section->buffer;
7534 reader->buffer_end = section->buffer + section->size;
7535 reader->comp_dir = NULL;
7536 reader->abbrev_table = abbrev_table;
7537 }
7538
7539 /* Subroutine of init_cutu_and_read_dies to simplify it.
7540 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7541 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7542 already.
7543
7544 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7545 from it to the DIE in the DWO. If NULL we are skipping the stub.
7546 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7547 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7548 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7549 STUB_COMP_DIR may be non-NULL.
7550 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7551 are filled in with the info of the DIE from the DWO file.
7552 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7553 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7554 kept around for at least as long as *RESULT_READER.
7555
7556 The result is non-zero if a valid (non-dummy) DIE was found. */
7557
7558 static int
7559 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7560 struct dwo_unit *dwo_unit,
7561 struct die_info *stub_comp_unit_die,
7562 const char *stub_comp_dir,
7563 struct die_reader_specs *result_reader,
7564 const gdb_byte **result_info_ptr,
7565 struct die_info **result_comp_unit_die,
7566 int *result_has_children,
7567 abbrev_table_up *result_dwo_abbrev_table)
7568 {
7569 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7570 struct objfile *objfile = dwarf2_per_objfile->objfile;
7571 struct dwarf2_cu *cu = this_cu->cu;
7572 bfd *abfd;
7573 const gdb_byte *begin_info_ptr, *info_ptr;
7574 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7575 int i,num_extra_attrs;
7576 struct dwarf2_section_info *dwo_abbrev_section;
7577 struct attribute *attr;
7578 struct die_info *comp_unit_die;
7579
7580 /* At most one of these may be provided. */
7581 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7582
7583 /* These attributes aren't processed until later:
7584 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7585 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7586 referenced later. However, these attributes are found in the stub
7587 which we won't have later. In order to not impose this complication
7588 on the rest of the code, we read them here and copy them to the
7589 DWO CU/TU die. */
7590
7591 stmt_list = NULL;
7592 low_pc = NULL;
7593 high_pc = NULL;
7594 ranges = NULL;
7595 comp_dir = NULL;
7596
7597 if (stub_comp_unit_die != NULL)
7598 {
7599 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7600 DWO file. */
7601 if (! this_cu->is_debug_types)
7602 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7603 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7604 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7605 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7606 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7607
7608 /* There should be a DW_AT_addr_base attribute here (if needed).
7609 We need the value before we can process DW_FORM_GNU_addr_index. */
7610 cu->addr_base = 0;
7611 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7612 if (attr)
7613 cu->addr_base = DW_UNSND (attr);
7614
7615 /* There should be a DW_AT_ranges_base attribute here (if needed).
7616 We need the value before we can process DW_AT_ranges. */
7617 cu->ranges_base = 0;
7618 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7619 if (attr)
7620 cu->ranges_base = DW_UNSND (attr);
7621 }
7622 else if (stub_comp_dir != NULL)
7623 {
7624 /* Reconstruct the comp_dir attribute to simplify the code below. */
7625 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7626 comp_dir->name = DW_AT_comp_dir;
7627 comp_dir->form = DW_FORM_string;
7628 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7629 DW_STRING (comp_dir) = stub_comp_dir;
7630 }
7631
7632 /* Set up for reading the DWO CU/TU. */
7633 cu->dwo_unit = dwo_unit;
7634 dwarf2_section_info *section = dwo_unit->section;
7635 dwarf2_read_section (objfile, section);
7636 abfd = get_section_bfd_owner (section);
7637 begin_info_ptr = info_ptr = (section->buffer
7638 + to_underlying (dwo_unit->sect_off));
7639 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7640
7641 if (this_cu->is_debug_types)
7642 {
7643 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7644
7645 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7646 &cu->header, section,
7647 dwo_abbrev_section,
7648 info_ptr, rcuh_kind::TYPE);
7649 /* This is not an assert because it can be caused by bad debug info. */
7650 if (sig_type->signature != cu->header.signature)
7651 {
7652 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7653 " TU at offset %s [in module %s]"),
7654 hex_string (sig_type->signature),
7655 hex_string (cu->header.signature),
7656 sect_offset_str (dwo_unit->sect_off),
7657 bfd_get_filename (abfd));
7658 }
7659 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7660 /* For DWOs coming from DWP files, we don't know the CU length
7661 nor the type's offset in the TU until now. */
7662 dwo_unit->length = get_cu_length (&cu->header);
7663 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7664
7665 /* Establish the type offset that can be used to lookup the type.
7666 For DWO files, we don't know it until now. */
7667 sig_type->type_offset_in_section
7668 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7669 }
7670 else
7671 {
7672 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7673 &cu->header, section,
7674 dwo_abbrev_section,
7675 info_ptr, rcuh_kind::COMPILE);
7676 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7677 /* For DWOs coming from DWP files, we don't know the CU length
7678 until now. */
7679 dwo_unit->length = get_cu_length (&cu->header);
7680 }
7681
7682 *result_dwo_abbrev_table
7683 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7684 cu->header.abbrev_sect_off);
7685 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7686 result_dwo_abbrev_table->get ());
7687
7688 /* Read in the die, but leave space to copy over the attributes
7689 from the stub. This has the benefit of simplifying the rest of
7690 the code - all the work to maintain the illusion of a single
7691 DW_TAG_{compile,type}_unit DIE is done here. */
7692 num_extra_attrs = ((stmt_list != NULL)
7693 + (low_pc != NULL)
7694 + (high_pc != NULL)
7695 + (ranges != NULL)
7696 + (comp_dir != NULL));
7697 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7698 result_has_children, num_extra_attrs);
7699
7700 /* Copy over the attributes from the stub to the DIE we just read in. */
7701 comp_unit_die = *result_comp_unit_die;
7702 i = comp_unit_die->num_attrs;
7703 if (stmt_list != NULL)
7704 comp_unit_die->attrs[i++] = *stmt_list;
7705 if (low_pc != NULL)
7706 comp_unit_die->attrs[i++] = *low_pc;
7707 if (high_pc != NULL)
7708 comp_unit_die->attrs[i++] = *high_pc;
7709 if (ranges != NULL)
7710 comp_unit_die->attrs[i++] = *ranges;
7711 if (comp_dir != NULL)
7712 comp_unit_die->attrs[i++] = *comp_dir;
7713 comp_unit_die->num_attrs += num_extra_attrs;
7714
7715 if (dwarf_die_debug)
7716 {
7717 fprintf_unfiltered (gdb_stdlog,
7718 "Read die from %s@0x%x of %s:\n",
7719 get_section_name (section),
7720 (unsigned) (begin_info_ptr - section->buffer),
7721 bfd_get_filename (abfd));
7722 dump_die (comp_unit_die, dwarf_die_debug);
7723 }
7724
7725 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7726 TUs by skipping the stub and going directly to the entry in the DWO file.
7727 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7728 to get it via circuitous means. Blech. */
7729 if (comp_dir != NULL)
7730 result_reader->comp_dir = DW_STRING (comp_dir);
7731
7732 /* Skip dummy compilation units. */
7733 if (info_ptr >= begin_info_ptr + dwo_unit->length
7734 || peek_abbrev_code (abfd, info_ptr) == 0)
7735 return 0;
7736
7737 *result_info_ptr = info_ptr;
7738 return 1;
7739 }
7740
7741 /* Subroutine of init_cutu_and_read_dies to simplify it.
7742 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7743 Returns NULL if the specified DWO unit cannot be found. */
7744
7745 static struct dwo_unit *
7746 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7747 struct die_info *comp_unit_die)
7748 {
7749 struct dwarf2_cu *cu = this_cu->cu;
7750 ULONGEST signature;
7751 struct dwo_unit *dwo_unit;
7752 const char *comp_dir, *dwo_name;
7753
7754 gdb_assert (cu != NULL);
7755
7756 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7757 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7758 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7759
7760 if (this_cu->is_debug_types)
7761 {
7762 struct signatured_type *sig_type;
7763
7764 /* Since this_cu is the first member of struct signatured_type,
7765 we can go from a pointer to one to a pointer to the other. */
7766 sig_type = (struct signatured_type *) this_cu;
7767 signature = sig_type->signature;
7768 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7769 }
7770 else
7771 {
7772 struct attribute *attr;
7773
7774 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7775 if (! attr)
7776 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7777 " [in module %s]"),
7778 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7779 signature = DW_UNSND (attr);
7780 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7781 signature);
7782 }
7783
7784 return dwo_unit;
7785 }
7786
7787 /* Subroutine of init_cutu_and_read_dies to simplify it.
7788 See it for a description of the parameters.
7789 Read a TU directly from a DWO file, bypassing the stub. */
7790
7791 static void
7792 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7793 int use_existing_cu, int keep,
7794 die_reader_func_ftype *die_reader_func,
7795 void *data)
7796 {
7797 std::unique_ptr<dwarf2_cu> new_cu;
7798 struct signatured_type *sig_type;
7799 struct die_reader_specs reader;
7800 const gdb_byte *info_ptr;
7801 struct die_info *comp_unit_die;
7802 int has_children;
7803 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7804
7805 /* Verify we can do the following downcast, and that we have the
7806 data we need. */
7807 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7808 sig_type = (struct signatured_type *) this_cu;
7809 gdb_assert (sig_type->dwo_unit != NULL);
7810
7811 if (use_existing_cu && this_cu->cu != NULL)
7812 {
7813 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7814 /* There's no need to do the rereading_dwo_cu handling that
7815 init_cutu_and_read_dies does since we don't read the stub. */
7816 }
7817 else
7818 {
7819 /* If !use_existing_cu, this_cu->cu must be NULL. */
7820 gdb_assert (this_cu->cu == NULL);
7821 new_cu.reset (new dwarf2_cu (this_cu));
7822 }
7823
7824 /* A future optimization, if needed, would be to use an existing
7825 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7826 could share abbrev tables. */
7827
7828 /* The abbreviation table used by READER, this must live at least as long as
7829 READER. */
7830 abbrev_table_up dwo_abbrev_table;
7831
7832 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7833 NULL /* stub_comp_unit_die */,
7834 sig_type->dwo_unit->dwo_file->comp_dir,
7835 &reader, &info_ptr,
7836 &comp_unit_die, &has_children,
7837 &dwo_abbrev_table) == 0)
7838 {
7839 /* Dummy die. */
7840 return;
7841 }
7842
7843 /* All the "real" work is done here. */
7844 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7845
7846 /* This duplicates the code in init_cutu_and_read_dies,
7847 but the alternative is making the latter more complex.
7848 This function is only for the special case of using DWO files directly:
7849 no point in overly complicating the general case just to handle this. */
7850 if (new_cu != NULL && keep)
7851 {
7852 /* Link this CU into read_in_chain. */
7853 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7854 dwarf2_per_objfile->read_in_chain = this_cu;
7855 /* The chain owns it now. */
7856 new_cu.release ();
7857 }
7858 }
7859
7860 /* Initialize a CU (or TU) and read its DIEs.
7861 If the CU defers to a DWO file, read the DWO file as well.
7862
7863 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7864 Otherwise the table specified in the comp unit header is read in and used.
7865 This is an optimization for when we already have the abbrev table.
7866
7867 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7868 Otherwise, a new CU is allocated with xmalloc.
7869
7870 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7871 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7872
7873 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7874 linker) then DIE_READER_FUNC will not get called. */
7875
7876 static void
7877 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7878 struct abbrev_table *abbrev_table,
7879 int use_existing_cu, int keep,
7880 die_reader_func_ftype *die_reader_func,
7881 void *data)
7882 {
7883 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7884 struct objfile *objfile = dwarf2_per_objfile->objfile;
7885 struct dwarf2_section_info *section = this_cu->section;
7886 bfd *abfd = get_section_bfd_owner (section);
7887 struct dwarf2_cu *cu;
7888 const gdb_byte *begin_info_ptr, *info_ptr;
7889 struct die_reader_specs reader;
7890 struct die_info *comp_unit_die;
7891 int has_children;
7892 struct attribute *attr;
7893 struct signatured_type *sig_type = NULL;
7894 struct dwarf2_section_info *abbrev_section;
7895 /* Non-zero if CU currently points to a DWO file and we need to
7896 reread it. When this happens we need to reread the skeleton die
7897 before we can reread the DWO file (this only applies to CUs, not TUs). */
7898 int rereading_dwo_cu = 0;
7899
7900 if (dwarf_die_debug)
7901 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7902 this_cu->is_debug_types ? "type" : "comp",
7903 sect_offset_str (this_cu->sect_off));
7904
7905 if (use_existing_cu)
7906 gdb_assert (keep);
7907
7908 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7909 file (instead of going through the stub), short-circuit all of this. */
7910 if (this_cu->reading_dwo_directly)
7911 {
7912 /* Narrow down the scope of possibilities to have to understand. */
7913 gdb_assert (this_cu->is_debug_types);
7914 gdb_assert (abbrev_table == NULL);
7915 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7916 die_reader_func, data);
7917 return;
7918 }
7919
7920 /* This is cheap if the section is already read in. */
7921 dwarf2_read_section (objfile, section);
7922
7923 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7924
7925 abbrev_section = get_abbrev_section_for_cu (this_cu);
7926
7927 std::unique_ptr<dwarf2_cu> new_cu;
7928 if (use_existing_cu && this_cu->cu != NULL)
7929 {
7930 cu = this_cu->cu;
7931 /* If this CU is from a DWO file we need to start over, we need to
7932 refetch the attributes from the skeleton CU.
7933 This could be optimized by retrieving those attributes from when we
7934 were here the first time: the previous comp_unit_die was stored in
7935 comp_unit_obstack. But there's no data yet that we need this
7936 optimization. */
7937 if (cu->dwo_unit != NULL)
7938 rereading_dwo_cu = 1;
7939 }
7940 else
7941 {
7942 /* If !use_existing_cu, this_cu->cu must be NULL. */
7943 gdb_assert (this_cu->cu == NULL);
7944 new_cu.reset (new dwarf2_cu (this_cu));
7945 cu = new_cu.get ();
7946 }
7947
7948 /* Get the header. */
7949 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7950 {
7951 /* We already have the header, there's no need to read it in again. */
7952 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7953 }
7954 else
7955 {
7956 if (this_cu->is_debug_types)
7957 {
7958 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7959 &cu->header, section,
7960 abbrev_section, info_ptr,
7961 rcuh_kind::TYPE);
7962
7963 /* Since per_cu is the first member of struct signatured_type,
7964 we can go from a pointer to one to a pointer to the other. */
7965 sig_type = (struct signatured_type *) this_cu;
7966 gdb_assert (sig_type->signature == cu->header.signature);
7967 gdb_assert (sig_type->type_offset_in_tu
7968 == cu->header.type_cu_offset_in_tu);
7969 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7970
7971 /* LENGTH has not been set yet for type units if we're
7972 using .gdb_index. */
7973 this_cu->length = get_cu_length (&cu->header);
7974
7975 /* Establish the type offset that can be used to lookup the type. */
7976 sig_type->type_offset_in_section =
7977 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7978
7979 this_cu->dwarf_version = cu->header.version;
7980 }
7981 else
7982 {
7983 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7984 &cu->header, section,
7985 abbrev_section,
7986 info_ptr,
7987 rcuh_kind::COMPILE);
7988
7989 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7990 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7991 this_cu->dwarf_version = cu->header.version;
7992 }
7993 }
7994
7995 /* Skip dummy compilation units. */
7996 if (info_ptr >= begin_info_ptr + this_cu->length
7997 || peek_abbrev_code (abfd, info_ptr) == 0)
7998 return;
7999
8000 /* If we don't have them yet, read the abbrevs for this compilation unit.
8001 And if we need to read them now, make sure they're freed when we're
8002 done (own the table through ABBREV_TABLE_HOLDER). */
8003 abbrev_table_up abbrev_table_holder;
8004 if (abbrev_table != NULL)
8005 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
8006 else
8007 {
8008 abbrev_table_holder
8009 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8010 cu->header.abbrev_sect_off);
8011 abbrev_table = abbrev_table_holder.get ();
8012 }
8013
8014 /* Read the top level CU/TU die. */
8015 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
8016 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8017
8018 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
8019 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
8020 table from the DWO file and pass the ownership over to us. It will be
8021 referenced from READER, so we must make sure to free it after we're done
8022 with READER.
8023
8024 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
8025 DWO CU, that this test will fail (the attribute will not be present). */
8026 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
8027 abbrev_table_up dwo_abbrev_table;
8028 if (attr)
8029 {
8030 struct dwo_unit *dwo_unit;
8031 struct die_info *dwo_comp_unit_die;
8032
8033 if (has_children)
8034 {
8035 complaint (&symfile_complaints,
8036 _("compilation unit with DW_AT_GNU_dwo_name"
8037 " has children (offset %s) [in module %s]"),
8038 sect_offset_str (this_cu->sect_off),
8039 bfd_get_filename (abfd));
8040 }
8041 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
8042 if (dwo_unit != NULL)
8043 {
8044 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
8045 comp_unit_die, NULL,
8046 &reader, &info_ptr,
8047 &dwo_comp_unit_die, &has_children,
8048 &dwo_abbrev_table) == 0)
8049 {
8050 /* Dummy die. */
8051 return;
8052 }
8053 comp_unit_die = dwo_comp_unit_die;
8054 }
8055 else
8056 {
8057 /* Yikes, we couldn't find the rest of the DIE, we only have
8058 the stub. A complaint has already been logged. There's
8059 not much more we can do except pass on the stub DIE to
8060 die_reader_func. We don't want to throw an error on bad
8061 debug info. */
8062 }
8063 }
8064
8065 /* All of the above is setup for this call. Yikes. */
8066 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8067
8068 /* Done, clean up. */
8069 if (new_cu != NULL && keep)
8070 {
8071 /* Link this CU into read_in_chain. */
8072 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
8073 dwarf2_per_objfile->read_in_chain = this_cu;
8074 /* The chain owns it now. */
8075 new_cu.release ();
8076 }
8077 }
8078
8079 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
8080 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
8081 to have already done the lookup to find the DWO file).
8082
8083 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
8084 THIS_CU->is_debug_types, but nothing else.
8085
8086 We fill in THIS_CU->length.
8087
8088 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
8089 linker) then DIE_READER_FUNC will not get called.
8090
8091 THIS_CU->cu is always freed when done.
8092 This is done in order to not leave THIS_CU->cu in a state where we have
8093 to care whether it refers to the "main" CU or the DWO CU. */
8094
8095 static void
8096 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
8097 struct dwo_file *dwo_file,
8098 die_reader_func_ftype *die_reader_func,
8099 void *data)
8100 {
8101 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
8102 struct objfile *objfile = dwarf2_per_objfile->objfile;
8103 struct dwarf2_section_info *section = this_cu->section;
8104 bfd *abfd = get_section_bfd_owner (section);
8105 struct dwarf2_section_info *abbrev_section;
8106 const gdb_byte *begin_info_ptr, *info_ptr;
8107 struct die_reader_specs reader;
8108 struct die_info *comp_unit_die;
8109 int has_children;
8110
8111 if (dwarf_die_debug)
8112 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
8113 this_cu->is_debug_types ? "type" : "comp",
8114 sect_offset_str (this_cu->sect_off));
8115
8116 gdb_assert (this_cu->cu == NULL);
8117
8118 abbrev_section = (dwo_file != NULL
8119 ? &dwo_file->sections.abbrev
8120 : get_abbrev_section_for_cu (this_cu));
8121
8122 /* This is cheap if the section is already read in. */
8123 dwarf2_read_section (objfile, section);
8124
8125 struct dwarf2_cu cu (this_cu);
8126
8127 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
8128 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
8129 &cu.header, section,
8130 abbrev_section, info_ptr,
8131 (this_cu->is_debug_types
8132 ? rcuh_kind::TYPE
8133 : rcuh_kind::COMPILE));
8134
8135 this_cu->length = get_cu_length (&cu.header);
8136
8137 /* Skip dummy compilation units. */
8138 if (info_ptr >= begin_info_ptr + this_cu->length
8139 || peek_abbrev_code (abfd, info_ptr) == 0)
8140 return;
8141
8142 abbrev_table_up abbrev_table
8143 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8144 cu.header.abbrev_sect_off);
8145
8146 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
8147 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8148
8149 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8150 }
8151
8152 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
8153 does not lookup the specified DWO file.
8154 This cannot be used to read DWO files.
8155
8156 THIS_CU->cu is always freed when done.
8157 This is done in order to not leave THIS_CU->cu in a state where we have
8158 to care whether it refers to the "main" CU or the DWO CU.
8159 We can revisit this if the data shows there's a performance issue. */
8160
8161 static void
8162 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8163 die_reader_func_ftype *die_reader_func,
8164 void *data)
8165 {
8166 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
8167 }
8168 \f
8169 /* Type Unit Groups.
8170
8171 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8172 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8173 so that all types coming from the same compilation (.o file) are grouped
8174 together. A future step could be to put the types in the same symtab as
8175 the CU the types ultimately came from. */
8176
8177 static hashval_t
8178 hash_type_unit_group (const void *item)
8179 {
8180 const struct type_unit_group *tu_group
8181 = (const struct type_unit_group *) item;
8182
8183 return hash_stmt_list_entry (&tu_group->hash);
8184 }
8185
8186 static int
8187 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
8188 {
8189 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8190 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
8191
8192 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
8193 }
8194
8195 /* Allocate a hash table for type unit groups. */
8196
8197 static htab_t
8198 allocate_type_unit_groups_table (struct objfile *objfile)
8199 {
8200 return htab_create_alloc_ex (3,
8201 hash_type_unit_group,
8202 eq_type_unit_group,
8203 NULL,
8204 &objfile->objfile_obstack,
8205 hashtab_obstack_allocate,
8206 dummy_obstack_deallocate);
8207 }
8208
8209 /* Type units that don't have DW_AT_stmt_list are grouped into their own
8210 partial symtabs. We combine several TUs per psymtab to not let the size
8211 of any one psymtab grow too big. */
8212 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8213 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
8214
8215 /* Helper routine for get_type_unit_group.
8216 Create the type_unit_group object used to hold one or more TUs. */
8217
8218 static struct type_unit_group *
8219 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
8220 {
8221 struct dwarf2_per_objfile *dwarf2_per_objfile
8222 = cu->per_cu->dwarf2_per_objfile;
8223 struct objfile *objfile = dwarf2_per_objfile->objfile;
8224 struct dwarf2_per_cu_data *per_cu;
8225 struct type_unit_group *tu_group;
8226
8227 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8228 struct type_unit_group);
8229 per_cu = &tu_group->per_cu;
8230 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8231
8232 if (dwarf2_per_objfile->using_index)
8233 {
8234 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8235 struct dwarf2_per_cu_quick_data);
8236 }
8237 else
8238 {
8239 unsigned int line_offset = to_underlying (line_offset_struct);
8240 struct partial_symtab *pst;
8241 char *name;
8242
8243 /* Give the symtab a useful name for debug purposes. */
8244 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8245 name = xstrprintf ("<type_units_%d>",
8246 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8247 else
8248 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8249
8250 pst = create_partial_symtab (per_cu, name);
8251 pst->anonymous = 1;
8252
8253 xfree (name);
8254 }
8255
8256 tu_group->hash.dwo_unit = cu->dwo_unit;
8257 tu_group->hash.line_sect_off = line_offset_struct;
8258
8259 return tu_group;
8260 }
8261
8262 /* Look up the type_unit_group for type unit CU, and create it if necessary.
8263 STMT_LIST is a DW_AT_stmt_list attribute. */
8264
8265 static struct type_unit_group *
8266 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
8267 {
8268 struct dwarf2_per_objfile *dwarf2_per_objfile
8269 = cu->per_cu->dwarf2_per_objfile;
8270 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8271 struct type_unit_group *tu_group;
8272 void **slot;
8273 unsigned int line_offset;
8274 struct type_unit_group type_unit_group_for_lookup;
8275
8276 if (dwarf2_per_objfile->type_unit_groups == NULL)
8277 {
8278 dwarf2_per_objfile->type_unit_groups =
8279 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
8280 }
8281
8282 /* Do we need to create a new group, or can we use an existing one? */
8283
8284 if (stmt_list)
8285 {
8286 line_offset = DW_UNSND (stmt_list);
8287 ++tu_stats->nr_symtab_sharers;
8288 }
8289 else
8290 {
8291 /* Ugh, no stmt_list. Rare, but we have to handle it.
8292 We can do various things here like create one group per TU or
8293 spread them over multiple groups to split up the expansion work.
8294 To avoid worst case scenarios (too many groups or too large groups)
8295 we, umm, group them in bunches. */
8296 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8297 | (tu_stats->nr_stmt_less_type_units
8298 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8299 ++tu_stats->nr_stmt_less_type_units;
8300 }
8301
8302 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
8303 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
8304 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8305 &type_unit_group_for_lookup, INSERT);
8306 if (*slot != NULL)
8307 {
8308 tu_group = (struct type_unit_group *) *slot;
8309 gdb_assert (tu_group != NULL);
8310 }
8311 else
8312 {
8313 sect_offset line_offset_struct = (sect_offset) line_offset;
8314 tu_group = create_type_unit_group (cu, line_offset_struct);
8315 *slot = tu_group;
8316 ++tu_stats->nr_symtabs;
8317 }
8318
8319 return tu_group;
8320 }
8321 \f
8322 /* Partial symbol tables. */
8323
8324 /* Create a psymtab named NAME and assign it to PER_CU.
8325
8326 The caller must fill in the following details:
8327 dirname, textlow, texthigh. */
8328
8329 static struct partial_symtab *
8330 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8331 {
8332 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8333 struct partial_symtab *pst;
8334
8335 pst = start_psymtab_common (objfile, name, 0,
8336 objfile->global_psymbols,
8337 objfile->static_psymbols);
8338
8339 pst->psymtabs_addrmap_supported = 1;
8340
8341 /* This is the glue that links PST into GDB's symbol API. */
8342 pst->read_symtab_private = per_cu;
8343 pst->read_symtab = dwarf2_read_symtab;
8344 per_cu->v.psymtab = pst;
8345
8346 return pst;
8347 }
8348
8349 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8350 type. */
8351
8352 struct process_psymtab_comp_unit_data
8353 {
8354 /* True if we are reading a DW_TAG_partial_unit. */
8355
8356 int want_partial_unit;
8357
8358 /* The "pretend" language that is used if the CU doesn't declare a
8359 language. */
8360
8361 enum language pretend_language;
8362 };
8363
8364 /* die_reader_func for process_psymtab_comp_unit. */
8365
8366 static void
8367 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8368 const gdb_byte *info_ptr,
8369 struct die_info *comp_unit_die,
8370 int has_children,
8371 void *data)
8372 {
8373 struct dwarf2_cu *cu = reader->cu;
8374 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8375 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8376 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8377 CORE_ADDR baseaddr;
8378 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8379 struct partial_symtab *pst;
8380 enum pc_bounds_kind cu_bounds_kind;
8381 const char *filename;
8382 struct process_psymtab_comp_unit_data *info
8383 = (struct process_psymtab_comp_unit_data *) data;
8384
8385 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8386 return;
8387
8388 gdb_assert (! per_cu->is_debug_types);
8389
8390 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8391
8392 cu->list_in_scope = &file_symbols;
8393
8394 /* Allocate a new partial symbol table structure. */
8395 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8396 if (filename == NULL)
8397 filename = "";
8398
8399 pst = create_partial_symtab (per_cu, filename);
8400
8401 /* This must be done before calling dwarf2_build_include_psymtabs. */
8402 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8403
8404 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8405
8406 dwarf2_find_base_address (comp_unit_die, cu);
8407
8408 /* Possibly set the default values of LOWPC and HIGHPC from
8409 `DW_AT_ranges'. */
8410 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8411 &best_highpc, cu, pst);
8412 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8413 /* Store the contiguous range if it is not empty; it can be empty for
8414 CUs with no code. */
8415 addrmap_set_empty (objfile->psymtabs_addrmap,
8416 gdbarch_adjust_dwarf2_addr (gdbarch,
8417 best_lowpc + baseaddr),
8418 gdbarch_adjust_dwarf2_addr (gdbarch,
8419 best_highpc + baseaddr) - 1,
8420 pst);
8421
8422 /* Check if comp unit has_children.
8423 If so, read the rest of the partial symbols from this comp unit.
8424 If not, there's no more debug_info for this comp unit. */
8425 if (has_children)
8426 {
8427 struct partial_die_info *first_die;
8428 CORE_ADDR lowpc, highpc;
8429
8430 lowpc = ((CORE_ADDR) -1);
8431 highpc = ((CORE_ADDR) 0);
8432
8433 first_die = load_partial_dies (reader, info_ptr, 1);
8434
8435 scan_partial_symbols (first_die, &lowpc, &highpc,
8436 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8437
8438 /* If we didn't find a lowpc, set it to highpc to avoid
8439 complaints from `maint check'. */
8440 if (lowpc == ((CORE_ADDR) -1))
8441 lowpc = highpc;
8442
8443 /* If the compilation unit didn't have an explicit address range,
8444 then use the information extracted from its child dies. */
8445 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8446 {
8447 best_lowpc = lowpc;
8448 best_highpc = highpc;
8449 }
8450 }
8451 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8452 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
8453
8454 end_psymtab_common (objfile, pst);
8455
8456 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8457 {
8458 int i;
8459 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8460 struct dwarf2_per_cu_data *iter;
8461
8462 /* Fill in 'dependencies' here; we fill in 'users' in a
8463 post-pass. */
8464 pst->number_of_dependencies = len;
8465 pst->dependencies =
8466 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8467 for (i = 0;
8468 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8469 i, iter);
8470 ++i)
8471 pst->dependencies[i] = iter->v.psymtab;
8472
8473 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8474 }
8475
8476 /* Get the list of files included in the current compilation unit,
8477 and build a psymtab for each of them. */
8478 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8479
8480 if (dwarf_read_debug)
8481 {
8482 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8483
8484 fprintf_unfiltered (gdb_stdlog,
8485 "Psymtab for %s unit @%s: %s - %s"
8486 ", %d global, %d static syms\n",
8487 per_cu->is_debug_types ? "type" : "comp",
8488 sect_offset_str (per_cu->sect_off),
8489 paddress (gdbarch, pst->textlow),
8490 paddress (gdbarch, pst->texthigh),
8491 pst->n_global_syms, pst->n_static_syms);
8492 }
8493 }
8494
8495 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8496 Process compilation unit THIS_CU for a psymtab. */
8497
8498 static void
8499 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8500 int want_partial_unit,
8501 enum language pretend_language)
8502 {
8503 /* If this compilation unit was already read in, free the
8504 cached copy in order to read it in again. This is
8505 necessary because we skipped some symbols when we first
8506 read in the compilation unit (see load_partial_dies).
8507 This problem could be avoided, but the benefit is unclear. */
8508 if (this_cu->cu != NULL)
8509 free_one_cached_comp_unit (this_cu);
8510
8511 if (this_cu->is_debug_types)
8512 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8513 NULL);
8514 else
8515 {
8516 process_psymtab_comp_unit_data info;
8517 info.want_partial_unit = want_partial_unit;
8518 info.pretend_language = pretend_language;
8519 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8520 process_psymtab_comp_unit_reader, &info);
8521 }
8522
8523 /* Age out any secondary CUs. */
8524 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8525 }
8526
8527 /* Reader function for build_type_psymtabs. */
8528
8529 static void
8530 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8531 const gdb_byte *info_ptr,
8532 struct die_info *type_unit_die,
8533 int has_children,
8534 void *data)
8535 {
8536 struct dwarf2_per_objfile *dwarf2_per_objfile
8537 = reader->cu->per_cu->dwarf2_per_objfile;
8538 struct objfile *objfile = dwarf2_per_objfile->objfile;
8539 struct dwarf2_cu *cu = reader->cu;
8540 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8541 struct signatured_type *sig_type;
8542 struct type_unit_group *tu_group;
8543 struct attribute *attr;
8544 struct partial_die_info *first_die;
8545 CORE_ADDR lowpc, highpc;
8546 struct partial_symtab *pst;
8547
8548 gdb_assert (data == NULL);
8549 gdb_assert (per_cu->is_debug_types);
8550 sig_type = (struct signatured_type *) per_cu;
8551
8552 if (! has_children)
8553 return;
8554
8555 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8556 tu_group = get_type_unit_group (cu, attr);
8557
8558 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8559
8560 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8561 cu->list_in_scope = &file_symbols;
8562 pst = create_partial_symtab (per_cu, "");
8563 pst->anonymous = 1;
8564
8565 first_die = load_partial_dies (reader, info_ptr, 1);
8566
8567 lowpc = (CORE_ADDR) -1;
8568 highpc = (CORE_ADDR) 0;
8569 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8570
8571 end_psymtab_common (objfile, pst);
8572 }
8573
8574 /* Struct used to sort TUs by their abbreviation table offset. */
8575
8576 struct tu_abbrev_offset
8577 {
8578 struct signatured_type *sig_type;
8579 sect_offset abbrev_offset;
8580 };
8581
8582 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
8583
8584 static int
8585 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
8586 {
8587 const struct tu_abbrev_offset * const *a
8588 = (const struct tu_abbrev_offset * const*) ap;
8589 const struct tu_abbrev_offset * const *b
8590 = (const struct tu_abbrev_offset * const*) bp;
8591 sect_offset aoff = (*a)->abbrev_offset;
8592 sect_offset boff = (*b)->abbrev_offset;
8593
8594 return (aoff > boff) - (aoff < boff);
8595 }
8596
8597 /* Efficiently read all the type units.
8598 This does the bulk of the work for build_type_psymtabs.
8599
8600 The efficiency is because we sort TUs by the abbrev table they use and
8601 only read each abbrev table once. In one program there are 200K TUs
8602 sharing 8K abbrev tables.
8603
8604 The main purpose of this function is to support building the
8605 dwarf2_per_objfile->type_unit_groups table.
8606 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8607 can collapse the search space by grouping them by stmt_list.
8608 The savings can be significant, in the same program from above the 200K TUs
8609 share 8K stmt_list tables.
8610
8611 FUNC is expected to call get_type_unit_group, which will create the
8612 struct type_unit_group if necessary and add it to
8613 dwarf2_per_objfile->type_unit_groups. */
8614
8615 static void
8616 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8617 {
8618 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8619 struct cleanup *cleanups;
8620 abbrev_table_up abbrev_table;
8621 sect_offset abbrev_offset;
8622 struct tu_abbrev_offset *sorted_by_abbrev;
8623 int i;
8624
8625 /* It's up to the caller to not call us multiple times. */
8626 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8627
8628 if (dwarf2_per_objfile->n_type_units == 0)
8629 return;
8630
8631 /* TUs typically share abbrev tables, and there can be way more TUs than
8632 abbrev tables. Sort by abbrev table to reduce the number of times we
8633 read each abbrev table in.
8634 Alternatives are to punt or to maintain a cache of abbrev tables.
8635 This is simpler and efficient enough for now.
8636
8637 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8638 symtab to use). Typically TUs with the same abbrev offset have the same
8639 stmt_list value too so in practice this should work well.
8640
8641 The basic algorithm here is:
8642
8643 sort TUs by abbrev table
8644 for each TU with same abbrev table:
8645 read abbrev table if first user
8646 read TU top level DIE
8647 [IWBN if DWO skeletons had DW_AT_stmt_list]
8648 call FUNC */
8649
8650 if (dwarf_read_debug)
8651 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8652
8653 /* Sort in a separate table to maintain the order of all_type_units
8654 for .gdb_index: TU indices directly index all_type_units. */
8655 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
8656 dwarf2_per_objfile->n_type_units);
8657 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8658 {
8659 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8660
8661 sorted_by_abbrev[i].sig_type = sig_type;
8662 sorted_by_abbrev[i].abbrev_offset =
8663 read_abbrev_offset (dwarf2_per_objfile,
8664 sig_type->per_cu.section,
8665 sig_type->per_cu.sect_off);
8666 }
8667 cleanups = make_cleanup (xfree, sorted_by_abbrev);
8668 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
8669 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
8670
8671 abbrev_offset = (sect_offset) ~(unsigned) 0;
8672
8673 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8674 {
8675 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8676
8677 /* Switch to the next abbrev table if necessary. */
8678 if (abbrev_table == NULL
8679 || tu->abbrev_offset != abbrev_offset)
8680 {
8681 abbrev_offset = tu->abbrev_offset;
8682 abbrev_table =
8683 abbrev_table_read_table (dwarf2_per_objfile,
8684 &dwarf2_per_objfile->abbrev,
8685 abbrev_offset);
8686 ++tu_stats->nr_uniq_abbrev_tables;
8687 }
8688
8689 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table.get (),
8690 0, 0, build_type_psymtabs_reader, NULL);
8691 }
8692
8693 do_cleanups (cleanups);
8694 }
8695
8696 /* Print collected type unit statistics. */
8697
8698 static void
8699 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8700 {
8701 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8702
8703 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8704 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8705 dwarf2_per_objfile->n_type_units);
8706 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8707 tu_stats->nr_uniq_abbrev_tables);
8708 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8709 tu_stats->nr_symtabs);
8710 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8711 tu_stats->nr_symtab_sharers);
8712 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8713 tu_stats->nr_stmt_less_type_units);
8714 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8715 tu_stats->nr_all_type_units_reallocs);
8716 }
8717
8718 /* Traversal function for build_type_psymtabs. */
8719
8720 static int
8721 build_type_psymtab_dependencies (void **slot, void *info)
8722 {
8723 struct dwarf2_per_objfile *dwarf2_per_objfile
8724 = (struct dwarf2_per_objfile *) info;
8725 struct objfile *objfile = dwarf2_per_objfile->objfile;
8726 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8727 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8728 struct partial_symtab *pst = per_cu->v.psymtab;
8729 int len = VEC_length (sig_type_ptr, tu_group->tus);
8730 struct signatured_type *iter;
8731 int i;
8732
8733 gdb_assert (len > 0);
8734 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8735
8736 pst->number_of_dependencies = len;
8737 pst->dependencies =
8738 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8739 for (i = 0;
8740 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8741 ++i)
8742 {
8743 gdb_assert (iter->per_cu.is_debug_types);
8744 pst->dependencies[i] = iter->per_cu.v.psymtab;
8745 iter->type_unit_group = tu_group;
8746 }
8747
8748 VEC_free (sig_type_ptr, tu_group->tus);
8749
8750 return 1;
8751 }
8752
8753 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8754 Build partial symbol tables for the .debug_types comp-units. */
8755
8756 static void
8757 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8758 {
8759 if (! create_all_type_units (dwarf2_per_objfile))
8760 return;
8761
8762 build_type_psymtabs_1 (dwarf2_per_objfile);
8763 }
8764
8765 /* Traversal function for process_skeletonless_type_unit.
8766 Read a TU in a DWO file and build partial symbols for it. */
8767
8768 static int
8769 process_skeletonless_type_unit (void **slot, void *info)
8770 {
8771 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8772 struct dwarf2_per_objfile *dwarf2_per_objfile
8773 = (struct dwarf2_per_objfile *) info;
8774 struct signatured_type find_entry, *entry;
8775
8776 /* If this TU doesn't exist in the global table, add it and read it in. */
8777
8778 if (dwarf2_per_objfile->signatured_types == NULL)
8779 {
8780 dwarf2_per_objfile->signatured_types
8781 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8782 }
8783
8784 find_entry.signature = dwo_unit->signature;
8785 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8786 INSERT);
8787 /* If we've already seen this type there's nothing to do. What's happening
8788 is we're doing our own version of comdat-folding here. */
8789 if (*slot != NULL)
8790 return 1;
8791
8792 /* This does the job that create_all_type_units would have done for
8793 this TU. */
8794 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8795 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8796 *slot = entry;
8797
8798 /* This does the job that build_type_psymtabs_1 would have done. */
8799 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8800 build_type_psymtabs_reader, NULL);
8801
8802 return 1;
8803 }
8804
8805 /* Traversal function for process_skeletonless_type_units. */
8806
8807 static int
8808 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8809 {
8810 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8811
8812 if (dwo_file->tus != NULL)
8813 {
8814 htab_traverse_noresize (dwo_file->tus,
8815 process_skeletonless_type_unit, info);
8816 }
8817
8818 return 1;
8819 }
8820
8821 /* Scan all TUs of DWO files, verifying we've processed them.
8822 This is needed in case a TU was emitted without its skeleton.
8823 Note: This can't be done until we know what all the DWO files are. */
8824
8825 static void
8826 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8827 {
8828 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8829 if (get_dwp_file (dwarf2_per_objfile) == NULL
8830 && dwarf2_per_objfile->dwo_files != NULL)
8831 {
8832 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8833 process_dwo_file_for_skeletonless_type_units,
8834 dwarf2_per_objfile);
8835 }
8836 }
8837
8838 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8839
8840 static void
8841 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8842 {
8843 int i;
8844
8845 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8846 {
8847 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8848 struct partial_symtab *pst = per_cu->v.psymtab;
8849 int j;
8850
8851 if (pst == NULL)
8852 continue;
8853
8854 for (j = 0; j < pst->number_of_dependencies; ++j)
8855 {
8856 /* Set the 'user' field only if it is not already set. */
8857 if (pst->dependencies[j]->user == NULL)
8858 pst->dependencies[j]->user = pst;
8859 }
8860 }
8861 }
8862
8863 /* Build the partial symbol table by doing a quick pass through the
8864 .debug_info and .debug_abbrev sections. */
8865
8866 static void
8867 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8868 {
8869 struct cleanup *back_to;
8870 int i;
8871 struct objfile *objfile = dwarf2_per_objfile->objfile;
8872
8873 if (dwarf_read_debug)
8874 {
8875 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8876 objfile_name (objfile));
8877 }
8878
8879 dwarf2_per_objfile->reading_partial_symbols = 1;
8880
8881 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8882
8883 /* Any cached compilation units will be linked by the per-objfile
8884 read_in_chain. Make sure to free them when we're done. */
8885 back_to = make_cleanup (free_cached_comp_units, dwarf2_per_objfile);
8886
8887 build_type_psymtabs (dwarf2_per_objfile);
8888
8889 create_all_comp_units (dwarf2_per_objfile);
8890
8891 /* Create a temporary address map on a temporary obstack. We later
8892 copy this to the final obstack. */
8893 auto_obstack temp_obstack;
8894
8895 scoped_restore save_psymtabs_addrmap
8896 = make_scoped_restore (&objfile->psymtabs_addrmap,
8897 addrmap_create_mutable (&temp_obstack));
8898
8899 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8900 {
8901 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8902
8903 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8904 }
8905
8906 /* This has to wait until we read the CUs, we need the list of DWOs. */
8907 process_skeletonless_type_units (dwarf2_per_objfile);
8908
8909 /* Now that all TUs have been processed we can fill in the dependencies. */
8910 if (dwarf2_per_objfile->type_unit_groups != NULL)
8911 {
8912 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8913 build_type_psymtab_dependencies, dwarf2_per_objfile);
8914 }
8915
8916 if (dwarf_read_debug)
8917 print_tu_stats (dwarf2_per_objfile);
8918
8919 set_partial_user (dwarf2_per_objfile);
8920
8921 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8922 &objfile->objfile_obstack);
8923 /* At this point we want to keep the address map. */
8924 save_psymtabs_addrmap.release ();
8925
8926 do_cleanups (back_to);
8927
8928 if (dwarf_read_debug)
8929 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8930 objfile_name (objfile));
8931 }
8932
8933 /* die_reader_func for load_partial_comp_unit. */
8934
8935 static void
8936 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8937 const gdb_byte *info_ptr,
8938 struct die_info *comp_unit_die,
8939 int has_children,
8940 void *data)
8941 {
8942 struct dwarf2_cu *cu = reader->cu;
8943
8944 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8945
8946 /* Check if comp unit has_children.
8947 If so, read the rest of the partial symbols from this comp unit.
8948 If not, there's no more debug_info for this comp unit. */
8949 if (has_children)
8950 load_partial_dies (reader, info_ptr, 0);
8951 }
8952
8953 /* Load the partial DIEs for a secondary CU into memory.
8954 This is also used when rereading a primary CU with load_all_dies. */
8955
8956 static void
8957 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8958 {
8959 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8960 load_partial_comp_unit_reader, NULL);
8961 }
8962
8963 static void
8964 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8965 struct dwarf2_section_info *section,
8966 struct dwarf2_section_info *abbrev_section,
8967 unsigned int is_dwz,
8968 int *n_allocated,
8969 int *n_comp_units,
8970 struct dwarf2_per_cu_data ***all_comp_units)
8971 {
8972 const gdb_byte *info_ptr;
8973 struct objfile *objfile = dwarf2_per_objfile->objfile;
8974
8975 if (dwarf_read_debug)
8976 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8977 get_section_name (section),
8978 get_section_file_name (section));
8979
8980 dwarf2_read_section (objfile, section);
8981
8982 info_ptr = section->buffer;
8983
8984 while (info_ptr < section->buffer + section->size)
8985 {
8986 struct dwarf2_per_cu_data *this_cu;
8987
8988 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8989
8990 comp_unit_head cu_header;
8991 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8992 abbrev_section, info_ptr,
8993 rcuh_kind::COMPILE);
8994
8995 /* Save the compilation unit for later lookup. */
8996 if (cu_header.unit_type != DW_UT_type)
8997 {
8998 this_cu = XOBNEW (&objfile->objfile_obstack,
8999 struct dwarf2_per_cu_data);
9000 memset (this_cu, 0, sizeof (*this_cu));
9001 }
9002 else
9003 {
9004 auto sig_type = XOBNEW (&objfile->objfile_obstack,
9005 struct signatured_type);
9006 memset (sig_type, 0, sizeof (*sig_type));
9007 sig_type->signature = cu_header.signature;
9008 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
9009 this_cu = &sig_type->per_cu;
9010 }
9011 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9012 this_cu->sect_off = sect_off;
9013 this_cu->length = cu_header.length + cu_header.initial_length_size;
9014 this_cu->is_dwz = is_dwz;
9015 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
9016 this_cu->section = section;
9017
9018 if (*n_comp_units == *n_allocated)
9019 {
9020 *n_allocated *= 2;
9021 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
9022 *all_comp_units, *n_allocated);
9023 }
9024 (*all_comp_units)[*n_comp_units] = this_cu;
9025 ++*n_comp_units;
9026
9027 info_ptr = info_ptr + this_cu->length;
9028 }
9029 }
9030
9031 /* Create a list of all compilation units in OBJFILE.
9032 This is only done for -readnow and building partial symtabs. */
9033
9034 static void
9035 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
9036 {
9037 int n_allocated;
9038 int n_comp_units;
9039 struct dwarf2_per_cu_data **all_comp_units;
9040 struct dwz_file *dwz;
9041 struct objfile *objfile = dwarf2_per_objfile->objfile;
9042
9043 n_comp_units = 0;
9044 n_allocated = 10;
9045 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
9046
9047 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
9048 &dwarf2_per_objfile->abbrev, 0,
9049 &n_allocated, &n_comp_units, &all_comp_units);
9050
9051 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
9052 if (dwz != NULL)
9053 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
9054 1, &n_allocated, &n_comp_units,
9055 &all_comp_units);
9056
9057 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
9058 struct dwarf2_per_cu_data *,
9059 n_comp_units);
9060 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
9061 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
9062 xfree (all_comp_units);
9063 dwarf2_per_objfile->n_comp_units = n_comp_units;
9064 }
9065
9066 /* Process all loaded DIEs for compilation unit CU, starting at
9067 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
9068 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
9069 DW_AT_ranges). See the comments of add_partial_subprogram on how
9070 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
9071
9072 static void
9073 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
9074 CORE_ADDR *highpc, int set_addrmap,
9075 struct dwarf2_cu *cu)
9076 {
9077 struct partial_die_info *pdi;
9078
9079 /* Now, march along the PDI's, descending into ones which have
9080 interesting children but skipping the children of the other ones,
9081 until we reach the end of the compilation unit. */
9082
9083 pdi = first_die;
9084
9085 while (pdi != NULL)
9086 {
9087 fixup_partial_die (pdi, cu);
9088
9089 /* Anonymous namespaces or modules have no name but have interesting
9090 children, so we need to look at them. Ditto for anonymous
9091 enums. */
9092
9093 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
9094 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
9095 || pdi->tag == DW_TAG_imported_unit
9096 || pdi->tag == DW_TAG_inlined_subroutine)
9097 {
9098 switch (pdi->tag)
9099 {
9100 case DW_TAG_subprogram:
9101 case DW_TAG_inlined_subroutine:
9102 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9103 break;
9104 case DW_TAG_constant:
9105 case DW_TAG_variable:
9106 case DW_TAG_typedef:
9107 case DW_TAG_union_type:
9108 if (!pdi->is_declaration)
9109 {
9110 add_partial_symbol (pdi, cu);
9111 }
9112 break;
9113 case DW_TAG_class_type:
9114 case DW_TAG_interface_type:
9115 case DW_TAG_structure_type:
9116 if (!pdi->is_declaration)
9117 {
9118 add_partial_symbol (pdi, cu);
9119 }
9120 if (cu->language == language_rust && pdi->has_children)
9121 scan_partial_symbols (pdi->die_child, lowpc, highpc,
9122 set_addrmap, cu);
9123 break;
9124 case DW_TAG_enumeration_type:
9125 if (!pdi->is_declaration)
9126 add_partial_enumeration (pdi, cu);
9127 break;
9128 case DW_TAG_base_type:
9129 case DW_TAG_subrange_type:
9130 /* File scope base type definitions are added to the partial
9131 symbol table. */
9132 add_partial_symbol (pdi, cu);
9133 break;
9134 case DW_TAG_namespace:
9135 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
9136 break;
9137 case DW_TAG_module:
9138 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
9139 break;
9140 case DW_TAG_imported_unit:
9141 {
9142 struct dwarf2_per_cu_data *per_cu;
9143
9144 /* For now we don't handle imported units in type units. */
9145 if (cu->per_cu->is_debug_types)
9146 {
9147 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9148 " supported in type units [in module %s]"),
9149 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9150 }
9151
9152 per_cu = dwarf2_find_containing_comp_unit
9153 (pdi->d.sect_off, pdi->is_dwz,
9154 cu->per_cu->dwarf2_per_objfile);
9155
9156 /* Go read the partial unit, if needed. */
9157 if (per_cu->v.psymtab == NULL)
9158 process_psymtab_comp_unit (per_cu, 1, cu->language);
9159
9160 VEC_safe_push (dwarf2_per_cu_ptr,
9161 cu->per_cu->imported_symtabs, per_cu);
9162 }
9163 break;
9164 case DW_TAG_imported_declaration:
9165 add_partial_symbol (pdi, cu);
9166 break;
9167 default:
9168 break;
9169 }
9170 }
9171
9172 /* If the die has a sibling, skip to the sibling. */
9173
9174 pdi = pdi->die_sibling;
9175 }
9176 }
9177
9178 /* Functions used to compute the fully scoped name of a partial DIE.
9179
9180 Normally, this is simple. For C++, the parent DIE's fully scoped
9181 name is concatenated with "::" and the partial DIE's name.
9182 Enumerators are an exception; they use the scope of their parent
9183 enumeration type, i.e. the name of the enumeration type is not
9184 prepended to the enumerator.
9185
9186 There are two complexities. One is DW_AT_specification; in this
9187 case "parent" means the parent of the target of the specification,
9188 instead of the direct parent of the DIE. The other is compilers
9189 which do not emit DW_TAG_namespace; in this case we try to guess
9190 the fully qualified name of structure types from their members'
9191 linkage names. This must be done using the DIE's children rather
9192 than the children of any DW_AT_specification target. We only need
9193 to do this for structures at the top level, i.e. if the target of
9194 any DW_AT_specification (if any; otherwise the DIE itself) does not
9195 have a parent. */
9196
9197 /* Compute the scope prefix associated with PDI's parent, in
9198 compilation unit CU. The result will be allocated on CU's
9199 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9200 field. NULL is returned if no prefix is necessary. */
9201 static const char *
9202 partial_die_parent_scope (struct partial_die_info *pdi,
9203 struct dwarf2_cu *cu)
9204 {
9205 const char *grandparent_scope;
9206 struct partial_die_info *parent, *real_pdi;
9207
9208 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9209 then this means the parent of the specification DIE. */
9210
9211 real_pdi = pdi;
9212 while (real_pdi->has_specification)
9213 real_pdi = find_partial_die (real_pdi->spec_offset,
9214 real_pdi->spec_is_dwz, cu);
9215
9216 parent = real_pdi->die_parent;
9217 if (parent == NULL)
9218 return NULL;
9219
9220 if (parent->scope_set)
9221 return parent->scope;
9222
9223 fixup_partial_die (parent, cu);
9224
9225 grandparent_scope = partial_die_parent_scope (parent, cu);
9226
9227 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9228 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9229 Work around this problem here. */
9230 if (cu->language == language_cplus
9231 && parent->tag == DW_TAG_namespace
9232 && strcmp (parent->name, "::") == 0
9233 && grandparent_scope == NULL)
9234 {
9235 parent->scope = NULL;
9236 parent->scope_set = 1;
9237 return NULL;
9238 }
9239
9240 if (pdi->tag == DW_TAG_enumerator)
9241 /* Enumerators should not get the name of the enumeration as a prefix. */
9242 parent->scope = grandparent_scope;
9243 else if (parent->tag == DW_TAG_namespace
9244 || parent->tag == DW_TAG_module
9245 || parent->tag == DW_TAG_structure_type
9246 || parent->tag == DW_TAG_class_type
9247 || parent->tag == DW_TAG_interface_type
9248 || parent->tag == DW_TAG_union_type
9249 || parent->tag == DW_TAG_enumeration_type)
9250 {
9251 if (grandparent_scope == NULL)
9252 parent->scope = parent->name;
9253 else
9254 parent->scope = typename_concat (&cu->comp_unit_obstack,
9255 grandparent_scope,
9256 parent->name, 0, cu);
9257 }
9258 else
9259 {
9260 /* FIXME drow/2004-04-01: What should we be doing with
9261 function-local names? For partial symbols, we should probably be
9262 ignoring them. */
9263 complaint (&symfile_complaints,
9264 _("unhandled containing DIE tag %d for DIE at %s"),
9265 parent->tag, sect_offset_str (pdi->sect_off));
9266 parent->scope = grandparent_scope;
9267 }
9268
9269 parent->scope_set = 1;
9270 return parent->scope;
9271 }
9272
9273 /* Return the fully scoped name associated with PDI, from compilation unit
9274 CU. The result will be allocated with malloc. */
9275
9276 static char *
9277 partial_die_full_name (struct partial_die_info *pdi,
9278 struct dwarf2_cu *cu)
9279 {
9280 const char *parent_scope;
9281
9282 /* If this is a template instantiation, we can not work out the
9283 template arguments from partial DIEs. So, unfortunately, we have
9284 to go through the full DIEs. At least any work we do building
9285 types here will be reused if full symbols are loaded later. */
9286 if (pdi->has_template_arguments)
9287 {
9288 fixup_partial_die (pdi, cu);
9289
9290 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9291 {
9292 struct die_info *die;
9293 struct attribute attr;
9294 struct dwarf2_cu *ref_cu = cu;
9295
9296 /* DW_FORM_ref_addr is using section offset. */
9297 attr.name = (enum dwarf_attribute) 0;
9298 attr.form = DW_FORM_ref_addr;
9299 attr.u.unsnd = to_underlying (pdi->sect_off);
9300 die = follow_die_ref (NULL, &attr, &ref_cu);
9301
9302 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9303 }
9304 }
9305
9306 parent_scope = partial_die_parent_scope (pdi, cu);
9307 if (parent_scope == NULL)
9308 return NULL;
9309 else
9310 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
9311 }
9312
9313 static void
9314 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
9315 {
9316 struct dwarf2_per_objfile *dwarf2_per_objfile
9317 = cu->per_cu->dwarf2_per_objfile;
9318 struct objfile *objfile = dwarf2_per_objfile->objfile;
9319 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9320 CORE_ADDR addr = 0;
9321 const char *actual_name = NULL;
9322 CORE_ADDR baseaddr;
9323 char *built_actual_name;
9324
9325 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9326
9327 built_actual_name = partial_die_full_name (pdi, cu);
9328 if (built_actual_name != NULL)
9329 actual_name = built_actual_name;
9330
9331 if (actual_name == NULL)
9332 actual_name = pdi->name;
9333
9334 switch (pdi->tag)
9335 {
9336 case DW_TAG_inlined_subroutine:
9337 case DW_TAG_subprogram:
9338 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
9339 if (pdi->is_external || cu->language == language_ada)
9340 {
9341 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9342 of the global scope. But in Ada, we want to be able to access
9343 nested procedures globally. So all Ada subprograms are stored
9344 in the global scope. */
9345 add_psymbol_to_list (actual_name, strlen (actual_name),
9346 built_actual_name != NULL,
9347 VAR_DOMAIN, LOC_BLOCK,
9348 &objfile->global_psymbols,
9349 addr, cu->language, objfile);
9350 }
9351 else
9352 {
9353 add_psymbol_to_list (actual_name, strlen (actual_name),
9354 built_actual_name != NULL,
9355 VAR_DOMAIN, LOC_BLOCK,
9356 &objfile->static_psymbols,
9357 addr, cu->language, objfile);
9358 }
9359
9360 if (pdi->main_subprogram && actual_name != NULL)
9361 set_objfile_main_name (objfile, actual_name, cu->language);
9362 break;
9363 case DW_TAG_constant:
9364 {
9365 std::vector<partial_symbol *> *list;
9366
9367 if (pdi->is_external)
9368 list = &objfile->global_psymbols;
9369 else
9370 list = &objfile->static_psymbols;
9371 add_psymbol_to_list (actual_name, strlen (actual_name),
9372 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9373 list, 0, cu->language, objfile);
9374 }
9375 break;
9376 case DW_TAG_variable:
9377 if (pdi->d.locdesc)
9378 addr = decode_locdesc (pdi->d.locdesc, cu);
9379
9380 if (pdi->d.locdesc
9381 && addr == 0
9382 && !dwarf2_per_objfile->has_section_at_zero)
9383 {
9384 /* A global or static variable may also have been stripped
9385 out by the linker if unused, in which case its address
9386 will be nullified; do not add such variables into partial
9387 symbol table then. */
9388 }
9389 else if (pdi->is_external)
9390 {
9391 /* Global Variable.
9392 Don't enter into the minimal symbol tables as there is
9393 a minimal symbol table entry from the ELF symbols already.
9394 Enter into partial symbol table if it has a location
9395 descriptor or a type.
9396 If the location descriptor is missing, new_symbol will create
9397 a LOC_UNRESOLVED symbol, the address of the variable will then
9398 be determined from the minimal symbol table whenever the variable
9399 is referenced.
9400 The address for the partial symbol table entry is not
9401 used by GDB, but it comes in handy for debugging partial symbol
9402 table building. */
9403
9404 if (pdi->d.locdesc || pdi->has_type)
9405 add_psymbol_to_list (actual_name, strlen (actual_name),
9406 built_actual_name != NULL,
9407 VAR_DOMAIN, LOC_STATIC,
9408 &objfile->global_psymbols,
9409 addr + baseaddr,
9410 cu->language, objfile);
9411 }
9412 else
9413 {
9414 int has_loc = pdi->d.locdesc != NULL;
9415
9416 /* Static Variable. Skip symbols whose value we cannot know (those
9417 without location descriptors or constant values). */
9418 if (!has_loc && !pdi->has_const_value)
9419 {
9420 xfree (built_actual_name);
9421 return;
9422 }
9423
9424 add_psymbol_to_list (actual_name, strlen (actual_name),
9425 built_actual_name != NULL,
9426 VAR_DOMAIN, LOC_STATIC,
9427 &objfile->static_psymbols,
9428 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
9429 cu->language, objfile);
9430 }
9431 break;
9432 case DW_TAG_typedef:
9433 case DW_TAG_base_type:
9434 case DW_TAG_subrange_type:
9435 add_psymbol_to_list (actual_name, strlen (actual_name),
9436 built_actual_name != NULL,
9437 VAR_DOMAIN, LOC_TYPEDEF,
9438 &objfile->static_psymbols,
9439 0, cu->language, objfile);
9440 break;
9441 case DW_TAG_imported_declaration:
9442 case DW_TAG_namespace:
9443 add_psymbol_to_list (actual_name, strlen (actual_name),
9444 built_actual_name != NULL,
9445 VAR_DOMAIN, LOC_TYPEDEF,
9446 &objfile->global_psymbols,
9447 0, cu->language, objfile);
9448 break;
9449 case DW_TAG_module:
9450 add_psymbol_to_list (actual_name, strlen (actual_name),
9451 built_actual_name != NULL,
9452 MODULE_DOMAIN, LOC_TYPEDEF,
9453 &objfile->global_psymbols,
9454 0, cu->language, objfile);
9455 break;
9456 case DW_TAG_class_type:
9457 case DW_TAG_interface_type:
9458 case DW_TAG_structure_type:
9459 case DW_TAG_union_type:
9460 case DW_TAG_enumeration_type:
9461 /* Skip external references. The DWARF standard says in the section
9462 about "Structure, Union, and Class Type Entries": "An incomplete
9463 structure, union or class type is represented by a structure,
9464 union or class entry that does not have a byte size attribute
9465 and that has a DW_AT_declaration attribute." */
9466 if (!pdi->has_byte_size && pdi->is_declaration)
9467 {
9468 xfree (built_actual_name);
9469 return;
9470 }
9471
9472 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9473 static vs. global. */
9474 add_psymbol_to_list (actual_name, strlen (actual_name),
9475 built_actual_name != NULL,
9476 STRUCT_DOMAIN, LOC_TYPEDEF,
9477 cu->language == language_cplus
9478 ? &objfile->global_psymbols
9479 : &objfile->static_psymbols,
9480 0, cu->language, objfile);
9481
9482 break;
9483 case DW_TAG_enumerator:
9484 add_psymbol_to_list (actual_name, strlen (actual_name),
9485 built_actual_name != NULL,
9486 VAR_DOMAIN, LOC_CONST,
9487 cu->language == language_cplus
9488 ? &objfile->global_psymbols
9489 : &objfile->static_psymbols,
9490 0, cu->language, objfile);
9491 break;
9492 default:
9493 break;
9494 }
9495
9496 xfree (built_actual_name);
9497 }
9498
9499 /* Read a partial die corresponding to a namespace; also, add a symbol
9500 corresponding to that namespace to the symbol table. NAMESPACE is
9501 the name of the enclosing namespace. */
9502
9503 static void
9504 add_partial_namespace (struct partial_die_info *pdi,
9505 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9506 int set_addrmap, struct dwarf2_cu *cu)
9507 {
9508 /* Add a symbol for the namespace. */
9509
9510 add_partial_symbol (pdi, cu);
9511
9512 /* Now scan partial symbols in that namespace. */
9513
9514 if (pdi->has_children)
9515 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9516 }
9517
9518 /* Read a partial die corresponding to a Fortran module. */
9519
9520 static void
9521 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9522 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9523 {
9524 /* Add a symbol for the namespace. */
9525
9526 add_partial_symbol (pdi, cu);
9527
9528 /* Now scan partial symbols in that module. */
9529
9530 if (pdi->has_children)
9531 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9532 }
9533
9534 /* Read a partial die corresponding to a subprogram or an inlined
9535 subprogram and create a partial symbol for that subprogram.
9536 When the CU language allows it, this routine also defines a partial
9537 symbol for each nested subprogram that this subprogram contains.
9538 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9539 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9540
9541 PDI may also be a lexical block, in which case we simply search
9542 recursively for subprograms defined inside that lexical block.
9543 Again, this is only performed when the CU language allows this
9544 type of definitions. */
9545
9546 static void
9547 add_partial_subprogram (struct partial_die_info *pdi,
9548 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9549 int set_addrmap, struct dwarf2_cu *cu)
9550 {
9551 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9552 {
9553 if (pdi->has_pc_info)
9554 {
9555 if (pdi->lowpc < *lowpc)
9556 *lowpc = pdi->lowpc;
9557 if (pdi->highpc > *highpc)
9558 *highpc = pdi->highpc;
9559 if (set_addrmap)
9560 {
9561 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9562 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9563 CORE_ADDR baseaddr;
9564 CORE_ADDR highpc;
9565 CORE_ADDR lowpc;
9566
9567 baseaddr = ANOFFSET (objfile->section_offsets,
9568 SECT_OFF_TEXT (objfile));
9569 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9570 pdi->lowpc + baseaddr);
9571 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9572 pdi->highpc + baseaddr);
9573 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9574 cu->per_cu->v.psymtab);
9575 }
9576 }
9577
9578 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9579 {
9580 if (!pdi->is_declaration)
9581 /* Ignore subprogram DIEs that do not have a name, they are
9582 illegal. Do not emit a complaint at this point, we will
9583 do so when we convert this psymtab into a symtab. */
9584 if (pdi->name)
9585 add_partial_symbol (pdi, cu);
9586 }
9587 }
9588
9589 if (! pdi->has_children)
9590 return;
9591
9592 if (cu->language == language_ada)
9593 {
9594 pdi = pdi->die_child;
9595 while (pdi != NULL)
9596 {
9597 fixup_partial_die (pdi, cu);
9598 if (pdi->tag == DW_TAG_subprogram
9599 || pdi->tag == DW_TAG_inlined_subroutine
9600 || pdi->tag == DW_TAG_lexical_block)
9601 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9602 pdi = pdi->die_sibling;
9603 }
9604 }
9605 }
9606
9607 /* Read a partial die corresponding to an enumeration type. */
9608
9609 static void
9610 add_partial_enumeration (struct partial_die_info *enum_pdi,
9611 struct dwarf2_cu *cu)
9612 {
9613 struct partial_die_info *pdi;
9614
9615 if (enum_pdi->name != NULL)
9616 add_partial_symbol (enum_pdi, cu);
9617
9618 pdi = enum_pdi->die_child;
9619 while (pdi)
9620 {
9621 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9622 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9623 else
9624 add_partial_symbol (pdi, cu);
9625 pdi = pdi->die_sibling;
9626 }
9627 }
9628
9629 /* Return the initial uleb128 in the die at INFO_PTR. */
9630
9631 static unsigned int
9632 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9633 {
9634 unsigned int bytes_read;
9635
9636 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9637 }
9638
9639 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9640 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9641
9642 Return the corresponding abbrev, or NULL if the number is zero (indicating
9643 an empty DIE). In either case *BYTES_READ will be set to the length of
9644 the initial number. */
9645
9646 static struct abbrev_info *
9647 peek_die_abbrev (const die_reader_specs &reader,
9648 const gdb_byte *info_ptr, unsigned int *bytes_read)
9649 {
9650 dwarf2_cu *cu = reader.cu;
9651 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9652 unsigned int abbrev_number
9653 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9654
9655 if (abbrev_number == 0)
9656 return NULL;
9657
9658 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9659 if (!abbrev)
9660 {
9661 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9662 " at offset %s [in module %s]"),
9663 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9664 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9665 }
9666
9667 return abbrev;
9668 }
9669
9670 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9671 Returns a pointer to the end of a series of DIEs, terminated by an empty
9672 DIE. Any children of the skipped DIEs will also be skipped. */
9673
9674 static const gdb_byte *
9675 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9676 {
9677 while (1)
9678 {
9679 unsigned int bytes_read;
9680 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9681
9682 if (abbrev == NULL)
9683 return info_ptr + bytes_read;
9684 else
9685 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9686 }
9687 }
9688
9689 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9690 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9691 abbrev corresponding to that skipped uleb128 should be passed in
9692 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9693 children. */
9694
9695 static const gdb_byte *
9696 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9697 struct abbrev_info *abbrev)
9698 {
9699 unsigned int bytes_read;
9700 struct attribute attr;
9701 bfd *abfd = reader->abfd;
9702 struct dwarf2_cu *cu = reader->cu;
9703 const gdb_byte *buffer = reader->buffer;
9704 const gdb_byte *buffer_end = reader->buffer_end;
9705 unsigned int form, i;
9706
9707 for (i = 0; i < abbrev->num_attrs; i++)
9708 {
9709 /* The only abbrev we care about is DW_AT_sibling. */
9710 if (abbrev->attrs[i].name == DW_AT_sibling)
9711 {
9712 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9713 if (attr.form == DW_FORM_ref_addr)
9714 complaint (&symfile_complaints,
9715 _("ignoring absolute DW_AT_sibling"));
9716 else
9717 {
9718 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9719 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9720
9721 if (sibling_ptr < info_ptr)
9722 complaint (&symfile_complaints,
9723 _("DW_AT_sibling points backwards"));
9724 else if (sibling_ptr > reader->buffer_end)
9725 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9726 else
9727 return sibling_ptr;
9728 }
9729 }
9730
9731 /* If it isn't DW_AT_sibling, skip this attribute. */
9732 form = abbrev->attrs[i].form;
9733 skip_attribute:
9734 switch (form)
9735 {
9736 case DW_FORM_ref_addr:
9737 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9738 and later it is offset sized. */
9739 if (cu->header.version == 2)
9740 info_ptr += cu->header.addr_size;
9741 else
9742 info_ptr += cu->header.offset_size;
9743 break;
9744 case DW_FORM_GNU_ref_alt:
9745 info_ptr += cu->header.offset_size;
9746 break;
9747 case DW_FORM_addr:
9748 info_ptr += cu->header.addr_size;
9749 break;
9750 case DW_FORM_data1:
9751 case DW_FORM_ref1:
9752 case DW_FORM_flag:
9753 info_ptr += 1;
9754 break;
9755 case DW_FORM_flag_present:
9756 case DW_FORM_implicit_const:
9757 break;
9758 case DW_FORM_data2:
9759 case DW_FORM_ref2:
9760 info_ptr += 2;
9761 break;
9762 case DW_FORM_data4:
9763 case DW_FORM_ref4:
9764 info_ptr += 4;
9765 break;
9766 case DW_FORM_data8:
9767 case DW_FORM_ref8:
9768 case DW_FORM_ref_sig8:
9769 info_ptr += 8;
9770 break;
9771 case DW_FORM_data16:
9772 info_ptr += 16;
9773 break;
9774 case DW_FORM_string:
9775 read_direct_string (abfd, info_ptr, &bytes_read);
9776 info_ptr += bytes_read;
9777 break;
9778 case DW_FORM_sec_offset:
9779 case DW_FORM_strp:
9780 case DW_FORM_GNU_strp_alt:
9781 info_ptr += cu->header.offset_size;
9782 break;
9783 case DW_FORM_exprloc:
9784 case DW_FORM_block:
9785 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9786 info_ptr += bytes_read;
9787 break;
9788 case DW_FORM_block1:
9789 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9790 break;
9791 case DW_FORM_block2:
9792 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9793 break;
9794 case DW_FORM_block4:
9795 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9796 break;
9797 case DW_FORM_sdata:
9798 case DW_FORM_udata:
9799 case DW_FORM_ref_udata:
9800 case DW_FORM_GNU_addr_index:
9801 case DW_FORM_GNU_str_index:
9802 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9803 break;
9804 case DW_FORM_indirect:
9805 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9806 info_ptr += bytes_read;
9807 /* We need to continue parsing from here, so just go back to
9808 the top. */
9809 goto skip_attribute;
9810
9811 default:
9812 error (_("Dwarf Error: Cannot handle %s "
9813 "in DWARF reader [in module %s]"),
9814 dwarf_form_name (form),
9815 bfd_get_filename (abfd));
9816 }
9817 }
9818
9819 if (abbrev->has_children)
9820 return skip_children (reader, info_ptr);
9821 else
9822 return info_ptr;
9823 }
9824
9825 /* Locate ORIG_PDI's sibling.
9826 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9827
9828 static const gdb_byte *
9829 locate_pdi_sibling (const struct die_reader_specs *reader,
9830 struct partial_die_info *orig_pdi,
9831 const gdb_byte *info_ptr)
9832 {
9833 /* Do we know the sibling already? */
9834
9835 if (orig_pdi->sibling)
9836 return orig_pdi->sibling;
9837
9838 /* Are there any children to deal with? */
9839
9840 if (!orig_pdi->has_children)
9841 return info_ptr;
9842
9843 /* Skip the children the long way. */
9844
9845 return skip_children (reader, info_ptr);
9846 }
9847
9848 /* Expand this partial symbol table into a full symbol table. SELF is
9849 not NULL. */
9850
9851 static void
9852 dwarf2_read_symtab (struct partial_symtab *self,
9853 struct objfile *objfile)
9854 {
9855 struct dwarf2_per_objfile *dwarf2_per_objfile
9856 = get_dwarf2_per_objfile (objfile);
9857
9858 if (self->readin)
9859 {
9860 warning (_("bug: psymtab for %s is already read in."),
9861 self->filename);
9862 }
9863 else
9864 {
9865 if (info_verbose)
9866 {
9867 printf_filtered (_("Reading in symbols for %s..."),
9868 self->filename);
9869 gdb_flush (gdb_stdout);
9870 }
9871
9872 /* If this psymtab is constructed from a debug-only objfile, the
9873 has_section_at_zero flag will not necessarily be correct. We
9874 can get the correct value for this flag by looking at the data
9875 associated with the (presumably stripped) associated objfile. */
9876 if (objfile->separate_debug_objfile_backlink)
9877 {
9878 struct dwarf2_per_objfile *dpo_backlink
9879 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9880
9881 dwarf2_per_objfile->has_section_at_zero
9882 = dpo_backlink->has_section_at_zero;
9883 }
9884
9885 dwarf2_per_objfile->reading_partial_symbols = 0;
9886
9887 psymtab_to_symtab_1 (self);
9888
9889 /* Finish up the debug error message. */
9890 if (info_verbose)
9891 printf_filtered (_("done.\n"));
9892 }
9893
9894 process_cu_includes (dwarf2_per_objfile);
9895 }
9896 \f
9897 /* Reading in full CUs. */
9898
9899 /* Add PER_CU to the queue. */
9900
9901 static void
9902 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9903 enum language pretend_language)
9904 {
9905 struct dwarf2_queue_item *item;
9906
9907 per_cu->queued = 1;
9908 item = XNEW (struct dwarf2_queue_item);
9909 item->per_cu = per_cu;
9910 item->pretend_language = pretend_language;
9911 item->next = NULL;
9912
9913 if (dwarf2_queue == NULL)
9914 dwarf2_queue = item;
9915 else
9916 dwarf2_queue_tail->next = item;
9917
9918 dwarf2_queue_tail = item;
9919 }
9920
9921 /* If PER_CU is not yet queued, add it to the queue.
9922 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9923 dependency.
9924 The result is non-zero if PER_CU was queued, otherwise the result is zero
9925 meaning either PER_CU is already queued or it is already loaded.
9926
9927 N.B. There is an invariant here that if a CU is queued then it is loaded.
9928 The caller is required to load PER_CU if we return non-zero. */
9929
9930 static int
9931 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9932 struct dwarf2_per_cu_data *per_cu,
9933 enum language pretend_language)
9934 {
9935 /* We may arrive here during partial symbol reading, if we need full
9936 DIEs to process an unusual case (e.g. template arguments). Do
9937 not queue PER_CU, just tell our caller to load its DIEs. */
9938 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9939 {
9940 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9941 return 1;
9942 return 0;
9943 }
9944
9945 /* Mark the dependence relation so that we don't flush PER_CU
9946 too early. */
9947 if (dependent_cu != NULL)
9948 dwarf2_add_dependence (dependent_cu, per_cu);
9949
9950 /* If it's already on the queue, we have nothing to do. */
9951 if (per_cu->queued)
9952 return 0;
9953
9954 /* If the compilation unit is already loaded, just mark it as
9955 used. */
9956 if (per_cu->cu != NULL)
9957 {
9958 per_cu->cu->last_used = 0;
9959 return 0;
9960 }
9961
9962 /* Add it to the queue. */
9963 queue_comp_unit (per_cu, pretend_language);
9964
9965 return 1;
9966 }
9967
9968 /* Process the queue. */
9969
9970 static void
9971 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9972 {
9973 struct dwarf2_queue_item *item, *next_item;
9974
9975 if (dwarf_read_debug)
9976 {
9977 fprintf_unfiltered (gdb_stdlog,
9978 "Expanding one or more symtabs of objfile %s ...\n",
9979 objfile_name (dwarf2_per_objfile->objfile));
9980 }
9981
9982 /* The queue starts out with one item, but following a DIE reference
9983 may load a new CU, adding it to the end of the queue. */
9984 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9985 {
9986 if ((dwarf2_per_objfile->using_index
9987 ? !item->per_cu->v.quick->compunit_symtab
9988 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9989 /* Skip dummy CUs. */
9990 && item->per_cu->cu != NULL)
9991 {
9992 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9993 unsigned int debug_print_threshold;
9994 char buf[100];
9995
9996 if (per_cu->is_debug_types)
9997 {
9998 struct signatured_type *sig_type =
9999 (struct signatured_type *) per_cu;
10000
10001 sprintf (buf, "TU %s at offset %s",
10002 hex_string (sig_type->signature),
10003 sect_offset_str (per_cu->sect_off));
10004 /* There can be 100s of TUs.
10005 Only print them in verbose mode. */
10006 debug_print_threshold = 2;
10007 }
10008 else
10009 {
10010 sprintf (buf, "CU at offset %s",
10011 sect_offset_str (per_cu->sect_off));
10012 debug_print_threshold = 1;
10013 }
10014
10015 if (dwarf_read_debug >= debug_print_threshold)
10016 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
10017
10018 if (per_cu->is_debug_types)
10019 process_full_type_unit (per_cu, item->pretend_language);
10020 else
10021 process_full_comp_unit (per_cu, item->pretend_language);
10022
10023 if (dwarf_read_debug >= debug_print_threshold)
10024 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
10025 }
10026
10027 item->per_cu->queued = 0;
10028 next_item = item->next;
10029 xfree (item);
10030 }
10031
10032 dwarf2_queue_tail = NULL;
10033
10034 if (dwarf_read_debug)
10035 {
10036 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
10037 objfile_name (dwarf2_per_objfile->objfile));
10038 }
10039 }
10040
10041 /* Read in full symbols for PST, and anything it depends on. */
10042
10043 static void
10044 psymtab_to_symtab_1 (struct partial_symtab *pst)
10045 {
10046 struct dwarf2_per_cu_data *per_cu;
10047 int i;
10048
10049 if (pst->readin)
10050 return;
10051
10052 for (i = 0; i < pst->number_of_dependencies; i++)
10053 if (!pst->dependencies[i]->readin
10054 && pst->dependencies[i]->user == NULL)
10055 {
10056 /* Inform about additional files that need to be read in. */
10057 if (info_verbose)
10058 {
10059 /* FIXME: i18n: Need to make this a single string. */
10060 fputs_filtered (" ", gdb_stdout);
10061 wrap_here ("");
10062 fputs_filtered ("and ", gdb_stdout);
10063 wrap_here ("");
10064 printf_filtered ("%s...", pst->dependencies[i]->filename);
10065 wrap_here (""); /* Flush output. */
10066 gdb_flush (gdb_stdout);
10067 }
10068 psymtab_to_symtab_1 (pst->dependencies[i]);
10069 }
10070
10071 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10072
10073 if (per_cu == NULL)
10074 {
10075 /* It's an include file, no symbols to read for it.
10076 Everything is in the parent symtab. */
10077 pst->readin = 1;
10078 return;
10079 }
10080
10081 dw2_do_instantiate_symtab (per_cu);
10082 }
10083
10084 /* Trivial hash function for die_info: the hash value of a DIE
10085 is its offset in .debug_info for this objfile. */
10086
10087 static hashval_t
10088 die_hash (const void *item)
10089 {
10090 const struct die_info *die = (const struct die_info *) item;
10091
10092 return to_underlying (die->sect_off);
10093 }
10094
10095 /* Trivial comparison function for die_info structures: two DIEs
10096 are equal if they have the same offset. */
10097
10098 static int
10099 die_eq (const void *item_lhs, const void *item_rhs)
10100 {
10101 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
10102 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
10103
10104 return die_lhs->sect_off == die_rhs->sect_off;
10105 }
10106
10107 /* die_reader_func for load_full_comp_unit.
10108 This is identical to read_signatured_type_reader,
10109 but is kept separate for now. */
10110
10111 static void
10112 load_full_comp_unit_reader (const struct die_reader_specs *reader,
10113 const gdb_byte *info_ptr,
10114 struct die_info *comp_unit_die,
10115 int has_children,
10116 void *data)
10117 {
10118 struct dwarf2_cu *cu = reader->cu;
10119 enum language *language_ptr = (enum language *) data;
10120
10121 gdb_assert (cu->die_hash == NULL);
10122 cu->die_hash =
10123 htab_create_alloc_ex (cu->header.length / 12,
10124 die_hash,
10125 die_eq,
10126 NULL,
10127 &cu->comp_unit_obstack,
10128 hashtab_obstack_allocate,
10129 dummy_obstack_deallocate);
10130
10131 if (has_children)
10132 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10133 &info_ptr, comp_unit_die);
10134 cu->dies = comp_unit_die;
10135 /* comp_unit_die is not stored in die_hash, no need. */
10136
10137 /* We try not to read any attributes in this function, because not
10138 all CUs needed for references have been loaded yet, and symbol
10139 table processing isn't initialized. But we have to set the CU language,
10140 or we won't be able to build types correctly.
10141 Similarly, if we do not read the producer, we can not apply
10142 producer-specific interpretation. */
10143 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
10144 }
10145
10146 /* Load the DIEs associated with PER_CU into memory. */
10147
10148 static void
10149 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10150 enum language pretend_language)
10151 {
10152 gdb_assert (! this_cu->is_debug_types);
10153
10154 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10155 load_full_comp_unit_reader, &pretend_language);
10156 }
10157
10158 /* Add a DIE to the delayed physname list. */
10159
10160 static void
10161 add_to_method_list (struct type *type, int fnfield_index, int index,
10162 const char *name, struct die_info *die,
10163 struct dwarf2_cu *cu)
10164 {
10165 struct delayed_method_info mi;
10166 mi.type = type;
10167 mi.fnfield_index = fnfield_index;
10168 mi.index = index;
10169 mi.name = name;
10170 mi.die = die;
10171 cu->method_list.push_back (mi);
10172 }
10173
10174 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10175 "const" / "volatile". If so, decrements LEN by the length of the
10176 modifier and return true. Otherwise return false. */
10177
10178 template<size_t N>
10179 static bool
10180 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10181 {
10182 size_t mod_len = sizeof (mod) - 1;
10183 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10184 {
10185 len -= mod_len;
10186 return true;
10187 }
10188 return false;
10189 }
10190
10191 /* Compute the physnames of any methods on the CU's method list.
10192
10193 The computation of method physnames is delayed in order to avoid the
10194 (bad) condition that one of the method's formal parameters is of an as yet
10195 incomplete type. */
10196
10197 static void
10198 compute_delayed_physnames (struct dwarf2_cu *cu)
10199 {
10200 /* Only C++ delays computing physnames. */
10201 if (cu->method_list.empty ())
10202 return;
10203 gdb_assert (cu->language == language_cplus);
10204
10205 for (struct delayed_method_info &mi : cu->method_list)
10206 {
10207 const char *physname;
10208 struct fn_fieldlist *fn_flp
10209 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
10210 physname = dwarf2_physname (mi.name, mi.die, cu);
10211 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
10212 = physname ? physname : "";
10213
10214 /* Since there's no tag to indicate whether a method is a
10215 const/volatile overload, extract that information out of the
10216 demangled name. */
10217 if (physname != NULL)
10218 {
10219 size_t len = strlen (physname);
10220
10221 while (1)
10222 {
10223 if (physname[len] == ')') /* shortcut */
10224 break;
10225 else if (check_modifier (physname, len, " const"))
10226 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
10227 else if (check_modifier (physname, len, " volatile"))
10228 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
10229 else
10230 break;
10231 }
10232 }
10233 }
10234
10235 /* The list is no longer needed. */
10236 cu->method_list.clear ();
10237 }
10238
10239 /* Go objects should be embedded in a DW_TAG_module DIE,
10240 and it's not clear if/how imported objects will appear.
10241 To keep Go support simple until that's worked out,
10242 go back through what we've read and create something usable.
10243 We could do this while processing each DIE, and feels kinda cleaner,
10244 but that way is more invasive.
10245 This is to, for example, allow the user to type "p var" or "b main"
10246 without having to specify the package name, and allow lookups
10247 of module.object to work in contexts that use the expression
10248 parser. */
10249
10250 static void
10251 fixup_go_packaging (struct dwarf2_cu *cu)
10252 {
10253 char *package_name = NULL;
10254 struct pending *list;
10255 int i;
10256
10257 for (list = global_symbols; list != NULL; list = list->next)
10258 {
10259 for (i = 0; i < list->nsyms; ++i)
10260 {
10261 struct symbol *sym = list->symbol[i];
10262
10263 if (SYMBOL_LANGUAGE (sym) == language_go
10264 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10265 {
10266 char *this_package_name = go_symbol_package_name (sym);
10267
10268 if (this_package_name == NULL)
10269 continue;
10270 if (package_name == NULL)
10271 package_name = this_package_name;
10272 else
10273 {
10274 struct objfile *objfile
10275 = cu->per_cu->dwarf2_per_objfile->objfile;
10276 if (strcmp (package_name, this_package_name) != 0)
10277 complaint (&symfile_complaints,
10278 _("Symtab %s has objects from two different Go packages: %s and %s"),
10279 (symbol_symtab (sym) != NULL
10280 ? symtab_to_filename_for_display
10281 (symbol_symtab (sym))
10282 : objfile_name (objfile)),
10283 this_package_name, package_name);
10284 xfree (this_package_name);
10285 }
10286 }
10287 }
10288 }
10289
10290 if (package_name != NULL)
10291 {
10292 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10293 const char *saved_package_name
10294 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10295 package_name,
10296 strlen (package_name));
10297 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10298 saved_package_name);
10299 struct symbol *sym;
10300
10301 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10302
10303 sym = allocate_symbol (objfile);
10304 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
10305 SYMBOL_SET_NAMES (sym, saved_package_name,
10306 strlen (saved_package_name), 0, objfile);
10307 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10308 e.g., "main" finds the "main" module and not C's main(). */
10309 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10310 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
10311 SYMBOL_TYPE (sym) = type;
10312
10313 add_symbol_to_list (sym, &global_symbols);
10314
10315 xfree (package_name);
10316 }
10317 }
10318
10319 /* Return the symtab for PER_CU. This works properly regardless of
10320 whether we're using the index or psymtabs. */
10321
10322 static struct compunit_symtab *
10323 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10324 {
10325 return (per_cu->dwarf2_per_objfile->using_index
10326 ? per_cu->v.quick->compunit_symtab
10327 : per_cu->v.psymtab->compunit_symtab);
10328 }
10329
10330 /* A helper function for computing the list of all symbol tables
10331 included by PER_CU. */
10332
10333 static void
10334 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10335 htab_t all_children, htab_t all_type_symtabs,
10336 struct dwarf2_per_cu_data *per_cu,
10337 struct compunit_symtab *immediate_parent)
10338 {
10339 void **slot;
10340 int ix;
10341 struct compunit_symtab *cust;
10342 struct dwarf2_per_cu_data *iter;
10343
10344 slot = htab_find_slot (all_children, per_cu, INSERT);
10345 if (*slot != NULL)
10346 {
10347 /* This inclusion and its children have been processed. */
10348 return;
10349 }
10350
10351 *slot = per_cu;
10352 /* Only add a CU if it has a symbol table. */
10353 cust = get_compunit_symtab (per_cu);
10354 if (cust != NULL)
10355 {
10356 /* If this is a type unit only add its symbol table if we haven't
10357 seen it yet (type unit per_cu's can share symtabs). */
10358 if (per_cu->is_debug_types)
10359 {
10360 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10361 if (*slot == NULL)
10362 {
10363 *slot = cust;
10364 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10365 if (cust->user == NULL)
10366 cust->user = immediate_parent;
10367 }
10368 }
10369 else
10370 {
10371 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10372 if (cust->user == NULL)
10373 cust->user = immediate_parent;
10374 }
10375 }
10376
10377 for (ix = 0;
10378 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10379 ++ix)
10380 {
10381 recursively_compute_inclusions (result, all_children,
10382 all_type_symtabs, iter, cust);
10383 }
10384 }
10385
10386 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10387 PER_CU. */
10388
10389 static void
10390 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10391 {
10392 gdb_assert (! per_cu->is_debug_types);
10393
10394 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10395 {
10396 int ix, len;
10397 struct dwarf2_per_cu_data *per_cu_iter;
10398 struct compunit_symtab *compunit_symtab_iter;
10399 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10400 htab_t all_children, all_type_symtabs;
10401 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10402
10403 /* If we don't have a symtab, we can just skip this case. */
10404 if (cust == NULL)
10405 return;
10406
10407 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10408 NULL, xcalloc, xfree);
10409 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10410 NULL, xcalloc, xfree);
10411
10412 for (ix = 0;
10413 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10414 ix, per_cu_iter);
10415 ++ix)
10416 {
10417 recursively_compute_inclusions (&result_symtabs, all_children,
10418 all_type_symtabs, per_cu_iter,
10419 cust);
10420 }
10421
10422 /* Now we have a transitive closure of all the included symtabs. */
10423 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10424 cust->includes
10425 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10426 struct compunit_symtab *, len + 1);
10427 for (ix = 0;
10428 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10429 compunit_symtab_iter);
10430 ++ix)
10431 cust->includes[ix] = compunit_symtab_iter;
10432 cust->includes[len] = NULL;
10433
10434 VEC_free (compunit_symtab_ptr, result_symtabs);
10435 htab_delete (all_children);
10436 htab_delete (all_type_symtabs);
10437 }
10438 }
10439
10440 /* Compute the 'includes' field for the symtabs of all the CUs we just
10441 read. */
10442
10443 static void
10444 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10445 {
10446 int ix;
10447 struct dwarf2_per_cu_data *iter;
10448
10449 for (ix = 0;
10450 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10451 ix, iter);
10452 ++ix)
10453 {
10454 if (! iter->is_debug_types)
10455 compute_compunit_symtab_includes (iter);
10456 }
10457
10458 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10459 }
10460
10461 /* Generate full symbol information for PER_CU, whose DIEs have
10462 already been loaded into memory. */
10463
10464 static void
10465 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10466 enum language pretend_language)
10467 {
10468 struct dwarf2_cu *cu = per_cu->cu;
10469 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10470 struct objfile *objfile = dwarf2_per_objfile->objfile;
10471 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10472 CORE_ADDR lowpc, highpc;
10473 struct compunit_symtab *cust;
10474 CORE_ADDR baseaddr;
10475 struct block *static_block;
10476 CORE_ADDR addr;
10477
10478 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10479
10480 buildsym_init ();
10481 scoped_free_pendings free_pending;
10482
10483 /* Clear the list here in case something was left over. */
10484 cu->method_list.clear ();
10485
10486 cu->list_in_scope = &file_symbols;
10487
10488 cu->language = pretend_language;
10489 cu->language_defn = language_def (cu->language);
10490
10491 /* Do line number decoding in read_file_scope () */
10492 process_die (cu->dies, cu);
10493
10494 /* For now fudge the Go package. */
10495 if (cu->language == language_go)
10496 fixup_go_packaging (cu);
10497
10498 /* Now that we have processed all the DIEs in the CU, all the types
10499 should be complete, and it should now be safe to compute all of the
10500 physnames. */
10501 compute_delayed_physnames (cu);
10502
10503 /* Some compilers don't define a DW_AT_high_pc attribute for the
10504 compilation unit. If the DW_AT_high_pc is missing, synthesize
10505 it, by scanning the DIE's below the compilation unit. */
10506 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10507
10508 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10509 static_block = end_symtab_get_static_block (addr, 0, 1);
10510
10511 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10512 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10513 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10514 addrmap to help ensure it has an accurate map of pc values belonging to
10515 this comp unit. */
10516 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10517
10518 cust = end_symtab_from_static_block (static_block,
10519 SECT_OFF_TEXT (objfile), 0);
10520
10521 if (cust != NULL)
10522 {
10523 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10524
10525 /* Set symtab language to language from DW_AT_language. If the
10526 compilation is from a C file generated by language preprocessors, do
10527 not set the language if it was already deduced by start_subfile. */
10528 if (!(cu->language == language_c
10529 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10530 COMPUNIT_FILETABS (cust)->language = cu->language;
10531
10532 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10533 produce DW_AT_location with location lists but it can be possibly
10534 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10535 there were bugs in prologue debug info, fixed later in GCC-4.5
10536 by "unwind info for epilogues" patch (which is not directly related).
10537
10538 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10539 needed, it would be wrong due to missing DW_AT_producer there.
10540
10541 Still one can confuse GDB by using non-standard GCC compilation
10542 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10543 */
10544 if (cu->has_loclist && gcc_4_minor >= 5)
10545 cust->locations_valid = 1;
10546
10547 if (gcc_4_minor >= 5)
10548 cust->epilogue_unwind_valid = 1;
10549
10550 cust->call_site_htab = cu->call_site_htab;
10551 }
10552
10553 if (dwarf2_per_objfile->using_index)
10554 per_cu->v.quick->compunit_symtab = cust;
10555 else
10556 {
10557 struct partial_symtab *pst = per_cu->v.psymtab;
10558 pst->compunit_symtab = cust;
10559 pst->readin = 1;
10560 }
10561
10562 /* Push it for inclusion processing later. */
10563 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10564 }
10565
10566 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10567 already been loaded into memory. */
10568
10569 static void
10570 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10571 enum language pretend_language)
10572 {
10573 struct dwarf2_cu *cu = per_cu->cu;
10574 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10575 struct objfile *objfile = dwarf2_per_objfile->objfile;
10576 struct compunit_symtab *cust;
10577 struct signatured_type *sig_type;
10578
10579 gdb_assert (per_cu->is_debug_types);
10580 sig_type = (struct signatured_type *) per_cu;
10581
10582 buildsym_init ();
10583 scoped_free_pendings free_pending;
10584
10585 /* Clear the list here in case something was left over. */
10586 cu->method_list.clear ();
10587
10588 cu->list_in_scope = &file_symbols;
10589
10590 cu->language = pretend_language;
10591 cu->language_defn = language_def (cu->language);
10592
10593 /* The symbol tables are set up in read_type_unit_scope. */
10594 process_die (cu->dies, cu);
10595
10596 /* For now fudge the Go package. */
10597 if (cu->language == language_go)
10598 fixup_go_packaging (cu);
10599
10600 /* Now that we have processed all the DIEs in the CU, all the types
10601 should be complete, and it should now be safe to compute all of the
10602 physnames. */
10603 compute_delayed_physnames (cu);
10604
10605 /* TUs share symbol tables.
10606 If this is the first TU to use this symtab, complete the construction
10607 of it with end_expandable_symtab. Otherwise, complete the addition of
10608 this TU's symbols to the existing symtab. */
10609 if (sig_type->type_unit_group->compunit_symtab == NULL)
10610 {
10611 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10612 sig_type->type_unit_group->compunit_symtab = cust;
10613
10614 if (cust != NULL)
10615 {
10616 /* Set symtab language to language from DW_AT_language. If the
10617 compilation is from a C file generated by language preprocessors,
10618 do not set the language if it was already deduced by
10619 start_subfile. */
10620 if (!(cu->language == language_c
10621 && COMPUNIT_FILETABS (cust)->language != language_c))
10622 COMPUNIT_FILETABS (cust)->language = cu->language;
10623 }
10624 }
10625 else
10626 {
10627 augment_type_symtab ();
10628 cust = sig_type->type_unit_group->compunit_symtab;
10629 }
10630
10631 if (dwarf2_per_objfile->using_index)
10632 per_cu->v.quick->compunit_symtab = cust;
10633 else
10634 {
10635 struct partial_symtab *pst = per_cu->v.psymtab;
10636 pst->compunit_symtab = cust;
10637 pst->readin = 1;
10638 }
10639 }
10640
10641 /* Process an imported unit DIE. */
10642
10643 static void
10644 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10645 {
10646 struct attribute *attr;
10647
10648 /* For now we don't handle imported units in type units. */
10649 if (cu->per_cu->is_debug_types)
10650 {
10651 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10652 " supported in type units [in module %s]"),
10653 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10654 }
10655
10656 attr = dwarf2_attr (die, DW_AT_import, cu);
10657 if (attr != NULL)
10658 {
10659 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10660 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10661 dwarf2_per_cu_data *per_cu
10662 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10663 cu->per_cu->dwarf2_per_objfile);
10664
10665 /* If necessary, add it to the queue and load its DIEs. */
10666 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10667 load_full_comp_unit (per_cu, cu->language);
10668
10669 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10670 per_cu);
10671 }
10672 }
10673
10674 /* RAII object that represents a process_die scope: i.e.,
10675 starts/finishes processing a DIE. */
10676 class process_die_scope
10677 {
10678 public:
10679 process_die_scope (die_info *die, dwarf2_cu *cu)
10680 : m_die (die), m_cu (cu)
10681 {
10682 /* We should only be processing DIEs not already in process. */
10683 gdb_assert (!m_die->in_process);
10684 m_die->in_process = true;
10685 }
10686
10687 ~process_die_scope ()
10688 {
10689 m_die->in_process = false;
10690
10691 /* If we're done processing the DIE for the CU that owns the line
10692 header, we don't need the line header anymore. */
10693 if (m_cu->line_header_die_owner == m_die)
10694 {
10695 delete m_cu->line_header;
10696 m_cu->line_header = NULL;
10697 m_cu->line_header_die_owner = NULL;
10698 }
10699 }
10700
10701 private:
10702 die_info *m_die;
10703 dwarf2_cu *m_cu;
10704 };
10705
10706 /* Process a die and its children. */
10707
10708 static void
10709 process_die (struct die_info *die, struct dwarf2_cu *cu)
10710 {
10711 process_die_scope scope (die, cu);
10712
10713 switch (die->tag)
10714 {
10715 case DW_TAG_padding:
10716 break;
10717 case DW_TAG_compile_unit:
10718 case DW_TAG_partial_unit:
10719 read_file_scope (die, cu);
10720 break;
10721 case DW_TAG_type_unit:
10722 read_type_unit_scope (die, cu);
10723 break;
10724 case DW_TAG_subprogram:
10725 case DW_TAG_inlined_subroutine:
10726 read_func_scope (die, cu);
10727 break;
10728 case DW_TAG_lexical_block:
10729 case DW_TAG_try_block:
10730 case DW_TAG_catch_block:
10731 read_lexical_block_scope (die, cu);
10732 break;
10733 case DW_TAG_call_site:
10734 case DW_TAG_GNU_call_site:
10735 read_call_site_scope (die, cu);
10736 break;
10737 case DW_TAG_class_type:
10738 case DW_TAG_interface_type:
10739 case DW_TAG_structure_type:
10740 case DW_TAG_union_type:
10741 process_structure_scope (die, cu);
10742 break;
10743 case DW_TAG_enumeration_type:
10744 process_enumeration_scope (die, cu);
10745 break;
10746
10747 /* These dies have a type, but processing them does not create
10748 a symbol or recurse to process the children. Therefore we can
10749 read them on-demand through read_type_die. */
10750 case DW_TAG_subroutine_type:
10751 case DW_TAG_set_type:
10752 case DW_TAG_array_type:
10753 case DW_TAG_pointer_type:
10754 case DW_TAG_ptr_to_member_type:
10755 case DW_TAG_reference_type:
10756 case DW_TAG_rvalue_reference_type:
10757 case DW_TAG_string_type:
10758 break;
10759
10760 case DW_TAG_base_type:
10761 case DW_TAG_subrange_type:
10762 case DW_TAG_typedef:
10763 /* Add a typedef symbol for the type definition, if it has a
10764 DW_AT_name. */
10765 new_symbol (die, read_type_die (die, cu), cu);
10766 break;
10767 case DW_TAG_common_block:
10768 read_common_block (die, cu);
10769 break;
10770 case DW_TAG_common_inclusion:
10771 break;
10772 case DW_TAG_namespace:
10773 cu->processing_has_namespace_info = 1;
10774 read_namespace (die, cu);
10775 break;
10776 case DW_TAG_module:
10777 cu->processing_has_namespace_info = 1;
10778 read_module (die, cu);
10779 break;
10780 case DW_TAG_imported_declaration:
10781 cu->processing_has_namespace_info = 1;
10782 if (read_namespace_alias (die, cu))
10783 break;
10784 /* The declaration is not a global namespace alias: fall through. */
10785 case DW_TAG_imported_module:
10786 cu->processing_has_namespace_info = 1;
10787 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10788 || cu->language != language_fortran))
10789 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10790 dwarf_tag_name (die->tag));
10791 read_import_statement (die, cu);
10792 break;
10793
10794 case DW_TAG_imported_unit:
10795 process_imported_unit_die (die, cu);
10796 break;
10797
10798 case DW_TAG_variable:
10799 read_variable (die, cu);
10800 break;
10801
10802 default:
10803 new_symbol (die, NULL, cu);
10804 break;
10805 }
10806 }
10807 \f
10808 /* DWARF name computation. */
10809
10810 /* A helper function for dwarf2_compute_name which determines whether DIE
10811 needs to have the name of the scope prepended to the name listed in the
10812 die. */
10813
10814 static int
10815 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10816 {
10817 struct attribute *attr;
10818
10819 switch (die->tag)
10820 {
10821 case DW_TAG_namespace:
10822 case DW_TAG_typedef:
10823 case DW_TAG_class_type:
10824 case DW_TAG_interface_type:
10825 case DW_TAG_structure_type:
10826 case DW_TAG_union_type:
10827 case DW_TAG_enumeration_type:
10828 case DW_TAG_enumerator:
10829 case DW_TAG_subprogram:
10830 case DW_TAG_inlined_subroutine:
10831 case DW_TAG_member:
10832 case DW_TAG_imported_declaration:
10833 return 1;
10834
10835 case DW_TAG_variable:
10836 case DW_TAG_constant:
10837 /* We only need to prefix "globally" visible variables. These include
10838 any variable marked with DW_AT_external or any variable that
10839 lives in a namespace. [Variables in anonymous namespaces
10840 require prefixing, but they are not DW_AT_external.] */
10841
10842 if (dwarf2_attr (die, DW_AT_specification, cu))
10843 {
10844 struct dwarf2_cu *spec_cu = cu;
10845
10846 return die_needs_namespace (die_specification (die, &spec_cu),
10847 spec_cu);
10848 }
10849
10850 attr = dwarf2_attr (die, DW_AT_external, cu);
10851 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10852 && die->parent->tag != DW_TAG_module)
10853 return 0;
10854 /* A variable in a lexical block of some kind does not need a
10855 namespace, even though in C++ such variables may be external
10856 and have a mangled name. */
10857 if (die->parent->tag == DW_TAG_lexical_block
10858 || die->parent->tag == DW_TAG_try_block
10859 || die->parent->tag == DW_TAG_catch_block
10860 || die->parent->tag == DW_TAG_subprogram)
10861 return 0;
10862 return 1;
10863
10864 default:
10865 return 0;
10866 }
10867 }
10868
10869 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10870 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10871 defined for the given DIE. */
10872
10873 static struct attribute *
10874 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10875 {
10876 struct attribute *attr;
10877
10878 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10879 if (attr == NULL)
10880 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10881
10882 return attr;
10883 }
10884
10885 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10886 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10887 defined for the given DIE. */
10888
10889 static const char *
10890 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10891 {
10892 const char *linkage_name;
10893
10894 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10895 if (linkage_name == NULL)
10896 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10897
10898 return linkage_name;
10899 }
10900
10901 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10902 compute the physname for the object, which include a method's:
10903 - formal parameters (C++),
10904 - receiver type (Go),
10905
10906 The term "physname" is a bit confusing.
10907 For C++, for example, it is the demangled name.
10908 For Go, for example, it's the mangled name.
10909
10910 For Ada, return the DIE's linkage name rather than the fully qualified
10911 name. PHYSNAME is ignored..
10912
10913 The result is allocated on the objfile_obstack and canonicalized. */
10914
10915 static const char *
10916 dwarf2_compute_name (const char *name,
10917 struct die_info *die, struct dwarf2_cu *cu,
10918 int physname)
10919 {
10920 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10921
10922 if (name == NULL)
10923 name = dwarf2_name (die, cu);
10924
10925 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10926 but otherwise compute it by typename_concat inside GDB.
10927 FIXME: Actually this is not really true, or at least not always true.
10928 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10929 Fortran names because there is no mangling standard. So new_symbol
10930 will set the demangled name to the result of dwarf2_full_name, and it is
10931 the demangled name that GDB uses if it exists. */
10932 if (cu->language == language_ada
10933 || (cu->language == language_fortran && physname))
10934 {
10935 /* For Ada unit, we prefer the linkage name over the name, as
10936 the former contains the exported name, which the user expects
10937 to be able to reference. Ideally, we want the user to be able
10938 to reference this entity using either natural or linkage name,
10939 but we haven't started looking at this enhancement yet. */
10940 const char *linkage_name = dw2_linkage_name (die, cu);
10941
10942 if (linkage_name != NULL)
10943 return linkage_name;
10944 }
10945
10946 /* These are the only languages we know how to qualify names in. */
10947 if (name != NULL
10948 && (cu->language == language_cplus
10949 || cu->language == language_fortran || cu->language == language_d
10950 || cu->language == language_rust))
10951 {
10952 if (die_needs_namespace (die, cu))
10953 {
10954 const char *prefix;
10955 const char *canonical_name = NULL;
10956
10957 string_file buf;
10958
10959 prefix = determine_prefix (die, cu);
10960 if (*prefix != '\0')
10961 {
10962 char *prefixed_name = typename_concat (NULL, prefix, name,
10963 physname, cu);
10964
10965 buf.puts (prefixed_name);
10966 xfree (prefixed_name);
10967 }
10968 else
10969 buf.puts (name);
10970
10971 /* Template parameters may be specified in the DIE's DW_AT_name, or
10972 as children with DW_TAG_template_type_param or
10973 DW_TAG_value_type_param. If the latter, add them to the name
10974 here. If the name already has template parameters, then
10975 skip this step; some versions of GCC emit both, and
10976 it is more efficient to use the pre-computed name.
10977
10978 Something to keep in mind about this process: it is very
10979 unlikely, or in some cases downright impossible, to produce
10980 something that will match the mangled name of a function.
10981 If the definition of the function has the same debug info,
10982 we should be able to match up with it anyway. But fallbacks
10983 using the minimal symbol, for instance to find a method
10984 implemented in a stripped copy of libstdc++, will not work.
10985 If we do not have debug info for the definition, we will have to
10986 match them up some other way.
10987
10988 When we do name matching there is a related problem with function
10989 templates; two instantiated function templates are allowed to
10990 differ only by their return types, which we do not add here. */
10991
10992 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10993 {
10994 struct attribute *attr;
10995 struct die_info *child;
10996 int first = 1;
10997
10998 die->building_fullname = 1;
10999
11000 for (child = die->child; child != NULL; child = child->sibling)
11001 {
11002 struct type *type;
11003 LONGEST value;
11004 const gdb_byte *bytes;
11005 struct dwarf2_locexpr_baton *baton;
11006 struct value *v;
11007
11008 if (child->tag != DW_TAG_template_type_param
11009 && child->tag != DW_TAG_template_value_param)
11010 continue;
11011
11012 if (first)
11013 {
11014 buf.puts ("<");
11015 first = 0;
11016 }
11017 else
11018 buf.puts (", ");
11019
11020 attr = dwarf2_attr (child, DW_AT_type, cu);
11021 if (attr == NULL)
11022 {
11023 complaint (&symfile_complaints,
11024 _("template parameter missing DW_AT_type"));
11025 buf.puts ("UNKNOWN_TYPE");
11026 continue;
11027 }
11028 type = die_type (child, cu);
11029
11030 if (child->tag == DW_TAG_template_type_param)
11031 {
11032 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
11033 continue;
11034 }
11035
11036 attr = dwarf2_attr (child, DW_AT_const_value, cu);
11037 if (attr == NULL)
11038 {
11039 complaint (&symfile_complaints,
11040 _("template parameter missing "
11041 "DW_AT_const_value"));
11042 buf.puts ("UNKNOWN_VALUE");
11043 continue;
11044 }
11045
11046 dwarf2_const_value_attr (attr, type, name,
11047 &cu->comp_unit_obstack, cu,
11048 &value, &bytes, &baton);
11049
11050 if (TYPE_NOSIGN (type))
11051 /* GDB prints characters as NUMBER 'CHAR'. If that's
11052 changed, this can use value_print instead. */
11053 c_printchar (value, type, &buf);
11054 else
11055 {
11056 struct value_print_options opts;
11057
11058 if (baton != NULL)
11059 v = dwarf2_evaluate_loc_desc (type, NULL,
11060 baton->data,
11061 baton->size,
11062 baton->per_cu);
11063 else if (bytes != NULL)
11064 {
11065 v = allocate_value (type);
11066 memcpy (value_contents_writeable (v), bytes,
11067 TYPE_LENGTH (type));
11068 }
11069 else
11070 v = value_from_longest (type, value);
11071
11072 /* Specify decimal so that we do not depend on
11073 the radix. */
11074 get_formatted_print_options (&opts, 'd');
11075 opts.raw = 1;
11076 value_print (v, &buf, &opts);
11077 release_value (v);
11078 value_free (v);
11079 }
11080 }
11081
11082 die->building_fullname = 0;
11083
11084 if (!first)
11085 {
11086 /* Close the argument list, with a space if necessary
11087 (nested templates). */
11088 if (!buf.empty () && buf.string ().back () == '>')
11089 buf.puts (" >");
11090 else
11091 buf.puts (">");
11092 }
11093 }
11094
11095 /* For C++ methods, append formal parameter type
11096 information, if PHYSNAME. */
11097
11098 if (physname && die->tag == DW_TAG_subprogram
11099 && cu->language == language_cplus)
11100 {
11101 struct type *type = read_type_die (die, cu);
11102
11103 c_type_print_args (type, &buf, 1, cu->language,
11104 &type_print_raw_options);
11105
11106 if (cu->language == language_cplus)
11107 {
11108 /* Assume that an artificial first parameter is
11109 "this", but do not crash if it is not. RealView
11110 marks unnamed (and thus unused) parameters as
11111 artificial; there is no way to differentiate
11112 the two cases. */
11113 if (TYPE_NFIELDS (type) > 0
11114 && TYPE_FIELD_ARTIFICIAL (type, 0)
11115 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11116 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11117 0))))
11118 buf.puts (" const");
11119 }
11120 }
11121
11122 const std::string &intermediate_name = buf.string ();
11123
11124 if (cu->language == language_cplus)
11125 canonical_name
11126 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11127 &objfile->per_bfd->storage_obstack);
11128
11129 /* If we only computed INTERMEDIATE_NAME, or if
11130 INTERMEDIATE_NAME is already canonical, then we need to
11131 copy it to the appropriate obstack. */
11132 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11133 name = ((const char *)
11134 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11135 intermediate_name.c_str (),
11136 intermediate_name.length ()));
11137 else
11138 name = canonical_name;
11139 }
11140 }
11141
11142 return name;
11143 }
11144
11145 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11146 If scope qualifiers are appropriate they will be added. The result
11147 will be allocated on the storage_obstack, or NULL if the DIE does
11148 not have a name. NAME may either be from a previous call to
11149 dwarf2_name or NULL.
11150
11151 The output string will be canonicalized (if C++). */
11152
11153 static const char *
11154 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11155 {
11156 return dwarf2_compute_name (name, die, cu, 0);
11157 }
11158
11159 /* Construct a physname for the given DIE in CU. NAME may either be
11160 from a previous call to dwarf2_name or NULL. The result will be
11161 allocated on the objfile_objstack or NULL if the DIE does not have a
11162 name.
11163
11164 The output string will be canonicalized (if C++). */
11165
11166 static const char *
11167 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11168 {
11169 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11170 const char *retval, *mangled = NULL, *canon = NULL;
11171 int need_copy = 1;
11172
11173 /* In this case dwarf2_compute_name is just a shortcut not building anything
11174 on its own. */
11175 if (!die_needs_namespace (die, cu))
11176 return dwarf2_compute_name (name, die, cu, 1);
11177
11178 mangled = dw2_linkage_name (die, cu);
11179
11180 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11181 See https://github.com/rust-lang/rust/issues/32925. */
11182 if (cu->language == language_rust && mangled != NULL
11183 && strchr (mangled, '{') != NULL)
11184 mangled = NULL;
11185
11186 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11187 has computed. */
11188 gdb::unique_xmalloc_ptr<char> demangled;
11189 if (mangled != NULL)
11190 {
11191
11192 if (cu->language == language_go)
11193 {
11194 /* This is a lie, but we already lie to the caller new_symbol.
11195 new_symbol assumes we return the mangled name.
11196 This just undoes that lie until things are cleaned up. */
11197 }
11198 else
11199 {
11200 /* Use DMGL_RET_DROP for C++ template functions to suppress
11201 their return type. It is easier for GDB users to search
11202 for such functions as `name(params)' than `long name(params)'.
11203 In such case the minimal symbol names do not match the full
11204 symbol names but for template functions there is never a need
11205 to look up their definition from their declaration so
11206 the only disadvantage remains the minimal symbol variant
11207 `long name(params)' does not have the proper inferior type. */
11208 demangled.reset (gdb_demangle (mangled,
11209 (DMGL_PARAMS | DMGL_ANSI
11210 | DMGL_RET_DROP)));
11211 }
11212 if (demangled)
11213 canon = demangled.get ();
11214 else
11215 {
11216 canon = mangled;
11217 need_copy = 0;
11218 }
11219 }
11220
11221 if (canon == NULL || check_physname)
11222 {
11223 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11224
11225 if (canon != NULL && strcmp (physname, canon) != 0)
11226 {
11227 /* It may not mean a bug in GDB. The compiler could also
11228 compute DW_AT_linkage_name incorrectly. But in such case
11229 GDB would need to be bug-to-bug compatible. */
11230
11231 complaint (&symfile_complaints,
11232 _("Computed physname <%s> does not match demangled <%s> "
11233 "(from linkage <%s>) - DIE at %s [in module %s]"),
11234 physname, canon, mangled, sect_offset_str (die->sect_off),
11235 objfile_name (objfile));
11236
11237 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11238 is available here - over computed PHYSNAME. It is safer
11239 against both buggy GDB and buggy compilers. */
11240
11241 retval = canon;
11242 }
11243 else
11244 {
11245 retval = physname;
11246 need_copy = 0;
11247 }
11248 }
11249 else
11250 retval = canon;
11251
11252 if (need_copy)
11253 retval = ((const char *)
11254 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11255 retval, strlen (retval)));
11256
11257 return retval;
11258 }
11259
11260 /* Inspect DIE in CU for a namespace alias. If one exists, record
11261 a new symbol for it.
11262
11263 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11264
11265 static int
11266 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11267 {
11268 struct attribute *attr;
11269
11270 /* If the die does not have a name, this is not a namespace
11271 alias. */
11272 attr = dwarf2_attr (die, DW_AT_name, cu);
11273 if (attr != NULL)
11274 {
11275 int num;
11276 struct die_info *d = die;
11277 struct dwarf2_cu *imported_cu = cu;
11278
11279 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11280 keep inspecting DIEs until we hit the underlying import. */
11281 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11282 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11283 {
11284 attr = dwarf2_attr (d, DW_AT_import, cu);
11285 if (attr == NULL)
11286 break;
11287
11288 d = follow_die_ref (d, attr, &imported_cu);
11289 if (d->tag != DW_TAG_imported_declaration)
11290 break;
11291 }
11292
11293 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11294 {
11295 complaint (&symfile_complaints,
11296 _("DIE at %s has too many recursively imported "
11297 "declarations"), sect_offset_str (d->sect_off));
11298 return 0;
11299 }
11300
11301 if (attr != NULL)
11302 {
11303 struct type *type;
11304 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11305
11306 type = get_die_type_at_offset (sect_off, cu->per_cu);
11307 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11308 {
11309 /* This declaration is a global namespace alias. Add
11310 a symbol for it whose type is the aliased namespace. */
11311 new_symbol (die, type, cu);
11312 return 1;
11313 }
11314 }
11315 }
11316
11317 return 0;
11318 }
11319
11320 /* Return the using directives repository (global or local?) to use in the
11321 current context for LANGUAGE.
11322
11323 For Ada, imported declarations can materialize renamings, which *may* be
11324 global. However it is impossible (for now?) in DWARF to distinguish
11325 "external" imported declarations and "static" ones. As all imported
11326 declarations seem to be static in all other languages, make them all CU-wide
11327 global only in Ada. */
11328
11329 static struct using_direct **
11330 using_directives (enum language language)
11331 {
11332 if (language == language_ada && context_stack_depth == 0)
11333 return &global_using_directives;
11334 else
11335 return &local_using_directives;
11336 }
11337
11338 /* Read the import statement specified by the given die and record it. */
11339
11340 static void
11341 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11342 {
11343 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11344 struct attribute *import_attr;
11345 struct die_info *imported_die, *child_die;
11346 struct dwarf2_cu *imported_cu;
11347 const char *imported_name;
11348 const char *imported_name_prefix;
11349 const char *canonical_name;
11350 const char *import_alias;
11351 const char *imported_declaration = NULL;
11352 const char *import_prefix;
11353 std::vector<const char *> excludes;
11354
11355 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11356 if (import_attr == NULL)
11357 {
11358 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11359 dwarf_tag_name (die->tag));
11360 return;
11361 }
11362
11363 imported_cu = cu;
11364 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11365 imported_name = dwarf2_name (imported_die, imported_cu);
11366 if (imported_name == NULL)
11367 {
11368 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11369
11370 The import in the following code:
11371 namespace A
11372 {
11373 typedef int B;
11374 }
11375
11376 int main ()
11377 {
11378 using A::B;
11379 B b;
11380 return b;
11381 }
11382
11383 ...
11384 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11385 <52> DW_AT_decl_file : 1
11386 <53> DW_AT_decl_line : 6
11387 <54> DW_AT_import : <0x75>
11388 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11389 <59> DW_AT_name : B
11390 <5b> DW_AT_decl_file : 1
11391 <5c> DW_AT_decl_line : 2
11392 <5d> DW_AT_type : <0x6e>
11393 ...
11394 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11395 <76> DW_AT_byte_size : 4
11396 <77> DW_AT_encoding : 5 (signed)
11397
11398 imports the wrong die ( 0x75 instead of 0x58 ).
11399 This case will be ignored until the gcc bug is fixed. */
11400 return;
11401 }
11402
11403 /* Figure out the local name after import. */
11404 import_alias = dwarf2_name (die, cu);
11405
11406 /* Figure out where the statement is being imported to. */
11407 import_prefix = determine_prefix (die, cu);
11408
11409 /* Figure out what the scope of the imported die is and prepend it
11410 to the name of the imported die. */
11411 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11412
11413 if (imported_die->tag != DW_TAG_namespace
11414 && imported_die->tag != DW_TAG_module)
11415 {
11416 imported_declaration = imported_name;
11417 canonical_name = imported_name_prefix;
11418 }
11419 else if (strlen (imported_name_prefix) > 0)
11420 canonical_name = obconcat (&objfile->objfile_obstack,
11421 imported_name_prefix,
11422 (cu->language == language_d ? "." : "::"),
11423 imported_name, (char *) NULL);
11424 else
11425 canonical_name = imported_name;
11426
11427 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11428 for (child_die = die->child; child_die && child_die->tag;
11429 child_die = sibling_die (child_die))
11430 {
11431 /* DWARF-4: A Fortran use statement with a “rename list” may be
11432 represented by an imported module entry with an import attribute
11433 referring to the module and owned entries corresponding to those
11434 entities that are renamed as part of being imported. */
11435
11436 if (child_die->tag != DW_TAG_imported_declaration)
11437 {
11438 complaint (&symfile_complaints,
11439 _("child DW_TAG_imported_declaration expected "
11440 "- DIE at %s [in module %s]"),
11441 sect_offset_str (child_die->sect_off),
11442 objfile_name (objfile));
11443 continue;
11444 }
11445
11446 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11447 if (import_attr == NULL)
11448 {
11449 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11450 dwarf_tag_name (child_die->tag));
11451 continue;
11452 }
11453
11454 imported_cu = cu;
11455 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11456 &imported_cu);
11457 imported_name = dwarf2_name (imported_die, imported_cu);
11458 if (imported_name == NULL)
11459 {
11460 complaint (&symfile_complaints,
11461 _("child DW_TAG_imported_declaration has unknown "
11462 "imported name - DIE at %s [in module %s]"),
11463 sect_offset_str (child_die->sect_off),
11464 objfile_name (objfile));
11465 continue;
11466 }
11467
11468 excludes.push_back (imported_name);
11469
11470 process_die (child_die, cu);
11471 }
11472
11473 add_using_directive (using_directives (cu->language),
11474 import_prefix,
11475 canonical_name,
11476 import_alias,
11477 imported_declaration,
11478 excludes,
11479 0,
11480 &objfile->objfile_obstack);
11481 }
11482
11483 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11484 types, but gives them a size of zero. Starting with version 14,
11485 ICC is compatible with GCC. */
11486
11487 static int
11488 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11489 {
11490 if (!cu->checked_producer)
11491 check_producer (cu);
11492
11493 return cu->producer_is_icc_lt_14;
11494 }
11495
11496 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11497 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11498 this, it was first present in GCC release 4.3.0. */
11499
11500 static int
11501 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11502 {
11503 if (!cu->checked_producer)
11504 check_producer (cu);
11505
11506 return cu->producer_is_gcc_lt_4_3;
11507 }
11508
11509 static file_and_directory
11510 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11511 {
11512 file_and_directory res;
11513
11514 /* Find the filename. Do not use dwarf2_name here, since the filename
11515 is not a source language identifier. */
11516 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11517 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11518
11519 if (res.comp_dir == NULL
11520 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11521 && IS_ABSOLUTE_PATH (res.name))
11522 {
11523 res.comp_dir_storage = ldirname (res.name);
11524 if (!res.comp_dir_storage.empty ())
11525 res.comp_dir = res.comp_dir_storage.c_str ();
11526 }
11527 if (res.comp_dir != NULL)
11528 {
11529 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11530 directory, get rid of it. */
11531 const char *cp = strchr (res.comp_dir, ':');
11532
11533 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11534 res.comp_dir = cp + 1;
11535 }
11536
11537 if (res.name == NULL)
11538 res.name = "<unknown>";
11539
11540 return res;
11541 }
11542
11543 /* Handle DW_AT_stmt_list for a compilation unit.
11544 DIE is the DW_TAG_compile_unit die for CU.
11545 COMP_DIR is the compilation directory. LOWPC is passed to
11546 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11547
11548 static void
11549 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11550 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11551 {
11552 struct dwarf2_per_objfile *dwarf2_per_objfile
11553 = cu->per_cu->dwarf2_per_objfile;
11554 struct objfile *objfile = dwarf2_per_objfile->objfile;
11555 struct attribute *attr;
11556 struct line_header line_header_local;
11557 hashval_t line_header_local_hash;
11558 void **slot;
11559 int decode_mapping;
11560
11561 gdb_assert (! cu->per_cu->is_debug_types);
11562
11563 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11564 if (attr == NULL)
11565 return;
11566
11567 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11568
11569 /* The line header hash table is only created if needed (it exists to
11570 prevent redundant reading of the line table for partial_units).
11571 If we're given a partial_unit, we'll need it. If we're given a
11572 compile_unit, then use the line header hash table if it's already
11573 created, but don't create one just yet. */
11574
11575 if (dwarf2_per_objfile->line_header_hash == NULL
11576 && die->tag == DW_TAG_partial_unit)
11577 {
11578 dwarf2_per_objfile->line_header_hash
11579 = htab_create_alloc_ex (127, line_header_hash_voidp,
11580 line_header_eq_voidp,
11581 free_line_header_voidp,
11582 &objfile->objfile_obstack,
11583 hashtab_obstack_allocate,
11584 dummy_obstack_deallocate);
11585 }
11586
11587 line_header_local.sect_off = line_offset;
11588 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11589 line_header_local_hash = line_header_hash (&line_header_local);
11590 if (dwarf2_per_objfile->line_header_hash != NULL)
11591 {
11592 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11593 &line_header_local,
11594 line_header_local_hash, NO_INSERT);
11595
11596 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11597 is not present in *SLOT (since if there is something in *SLOT then
11598 it will be for a partial_unit). */
11599 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11600 {
11601 gdb_assert (*slot != NULL);
11602 cu->line_header = (struct line_header *) *slot;
11603 return;
11604 }
11605 }
11606
11607 /* dwarf_decode_line_header does not yet provide sufficient information.
11608 We always have to call also dwarf_decode_lines for it. */
11609 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11610 if (lh == NULL)
11611 return;
11612
11613 cu->line_header = lh.release ();
11614 cu->line_header_die_owner = die;
11615
11616 if (dwarf2_per_objfile->line_header_hash == NULL)
11617 slot = NULL;
11618 else
11619 {
11620 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11621 &line_header_local,
11622 line_header_local_hash, INSERT);
11623 gdb_assert (slot != NULL);
11624 }
11625 if (slot != NULL && *slot == NULL)
11626 {
11627 /* This newly decoded line number information unit will be owned
11628 by line_header_hash hash table. */
11629 *slot = cu->line_header;
11630 cu->line_header_die_owner = NULL;
11631 }
11632 else
11633 {
11634 /* We cannot free any current entry in (*slot) as that struct line_header
11635 may be already used by multiple CUs. Create only temporary decoded
11636 line_header for this CU - it may happen at most once for each line
11637 number information unit. And if we're not using line_header_hash
11638 then this is what we want as well. */
11639 gdb_assert (die->tag != DW_TAG_partial_unit);
11640 }
11641 decode_mapping = (die->tag != DW_TAG_partial_unit);
11642 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11643 decode_mapping);
11644
11645 }
11646
11647 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11648
11649 static void
11650 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11651 {
11652 struct dwarf2_per_objfile *dwarf2_per_objfile
11653 = cu->per_cu->dwarf2_per_objfile;
11654 struct objfile *objfile = dwarf2_per_objfile->objfile;
11655 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11656 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11657 CORE_ADDR highpc = ((CORE_ADDR) 0);
11658 struct attribute *attr;
11659 struct die_info *child_die;
11660 CORE_ADDR baseaddr;
11661
11662 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11663
11664 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11665
11666 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11667 from finish_block. */
11668 if (lowpc == ((CORE_ADDR) -1))
11669 lowpc = highpc;
11670 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11671
11672 file_and_directory fnd = find_file_and_directory (die, cu);
11673
11674 prepare_one_comp_unit (cu, die, cu->language);
11675
11676 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11677 standardised yet. As a workaround for the language detection we fall
11678 back to the DW_AT_producer string. */
11679 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11680 cu->language = language_opencl;
11681
11682 /* Similar hack for Go. */
11683 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11684 set_cu_language (DW_LANG_Go, cu);
11685
11686 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11687
11688 /* Decode line number information if present. We do this before
11689 processing child DIEs, so that the line header table is available
11690 for DW_AT_decl_file. */
11691 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11692
11693 /* Process all dies in compilation unit. */
11694 if (die->child != NULL)
11695 {
11696 child_die = die->child;
11697 while (child_die && child_die->tag)
11698 {
11699 process_die (child_die, cu);
11700 child_die = sibling_die (child_die);
11701 }
11702 }
11703
11704 /* Decode macro information, if present. Dwarf 2 macro information
11705 refers to information in the line number info statement program
11706 header, so we can only read it if we've read the header
11707 successfully. */
11708 attr = dwarf2_attr (die, DW_AT_macros, cu);
11709 if (attr == NULL)
11710 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11711 if (attr && cu->line_header)
11712 {
11713 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11714 complaint (&symfile_complaints,
11715 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11716
11717 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11718 }
11719 else
11720 {
11721 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11722 if (attr && cu->line_header)
11723 {
11724 unsigned int macro_offset = DW_UNSND (attr);
11725
11726 dwarf_decode_macros (cu, macro_offset, 0);
11727 }
11728 }
11729 }
11730
11731 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11732 Create the set of symtabs used by this TU, or if this TU is sharing
11733 symtabs with another TU and the symtabs have already been created
11734 then restore those symtabs in the line header.
11735 We don't need the pc/line-number mapping for type units. */
11736
11737 static void
11738 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
11739 {
11740 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11741 struct type_unit_group *tu_group;
11742 int first_time;
11743 struct attribute *attr;
11744 unsigned int i;
11745 struct signatured_type *sig_type;
11746
11747 gdb_assert (per_cu->is_debug_types);
11748 sig_type = (struct signatured_type *) per_cu;
11749
11750 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11751
11752 /* If we're using .gdb_index (includes -readnow) then
11753 per_cu->type_unit_group may not have been set up yet. */
11754 if (sig_type->type_unit_group == NULL)
11755 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11756 tu_group = sig_type->type_unit_group;
11757
11758 /* If we've already processed this stmt_list there's no real need to
11759 do it again, we could fake it and just recreate the part we need
11760 (file name,index -> symtab mapping). If data shows this optimization
11761 is useful we can do it then. */
11762 first_time = tu_group->compunit_symtab == NULL;
11763
11764 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11765 debug info. */
11766 line_header_up lh;
11767 if (attr != NULL)
11768 {
11769 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11770 lh = dwarf_decode_line_header (line_offset, cu);
11771 }
11772 if (lh == NULL)
11773 {
11774 if (first_time)
11775 dwarf2_start_symtab (cu, "", NULL, 0);
11776 else
11777 {
11778 gdb_assert (tu_group->symtabs == NULL);
11779 restart_symtab (tu_group->compunit_symtab, "", 0);
11780 }
11781 return;
11782 }
11783
11784 cu->line_header = lh.release ();
11785 cu->line_header_die_owner = die;
11786
11787 if (first_time)
11788 {
11789 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
11790
11791 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11792 still initializing it, and our caller (a few levels up)
11793 process_full_type_unit still needs to know if this is the first
11794 time. */
11795
11796 tu_group->num_symtabs = cu->line_header->file_names.size ();
11797 tu_group->symtabs = XNEWVEC (struct symtab *,
11798 cu->line_header->file_names.size ());
11799
11800 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11801 {
11802 file_entry &fe = cu->line_header->file_names[i];
11803
11804 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
11805
11806 if (current_subfile->symtab == NULL)
11807 {
11808 /* NOTE: start_subfile will recognize when it's been
11809 passed a file it has already seen. So we can't
11810 assume there's a simple mapping from
11811 cu->line_header->file_names to subfiles, plus
11812 cu->line_header->file_names may contain dups. */
11813 current_subfile->symtab
11814 = allocate_symtab (cust, current_subfile->name);
11815 }
11816
11817 fe.symtab = current_subfile->symtab;
11818 tu_group->symtabs[i] = fe.symtab;
11819 }
11820 }
11821 else
11822 {
11823 restart_symtab (tu_group->compunit_symtab, "", 0);
11824
11825 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11826 {
11827 file_entry &fe = cu->line_header->file_names[i];
11828
11829 fe.symtab = tu_group->symtabs[i];
11830 }
11831 }
11832
11833 /* The main symtab is allocated last. Type units don't have DW_AT_name
11834 so they don't have a "real" (so to speak) symtab anyway.
11835 There is later code that will assign the main symtab to all symbols
11836 that don't have one. We need to handle the case of a symbol with a
11837 missing symtab (DW_AT_decl_file) anyway. */
11838 }
11839
11840 /* Process DW_TAG_type_unit.
11841 For TUs we want to skip the first top level sibling if it's not the
11842 actual type being defined by this TU. In this case the first top
11843 level sibling is there to provide context only. */
11844
11845 static void
11846 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11847 {
11848 struct die_info *child_die;
11849
11850 prepare_one_comp_unit (cu, die, language_minimal);
11851
11852 /* Initialize (or reinitialize) the machinery for building symtabs.
11853 We do this before processing child DIEs, so that the line header table
11854 is available for DW_AT_decl_file. */
11855 setup_type_unit_groups (die, cu);
11856
11857 if (die->child != NULL)
11858 {
11859 child_die = die->child;
11860 while (child_die && child_die->tag)
11861 {
11862 process_die (child_die, cu);
11863 child_die = sibling_die (child_die);
11864 }
11865 }
11866 }
11867 \f
11868 /* DWO/DWP files.
11869
11870 http://gcc.gnu.org/wiki/DebugFission
11871 http://gcc.gnu.org/wiki/DebugFissionDWP
11872
11873 To simplify handling of both DWO files ("object" files with the DWARF info)
11874 and DWP files (a file with the DWOs packaged up into one file), we treat
11875 DWP files as having a collection of virtual DWO files. */
11876
11877 static hashval_t
11878 hash_dwo_file (const void *item)
11879 {
11880 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11881 hashval_t hash;
11882
11883 hash = htab_hash_string (dwo_file->dwo_name);
11884 if (dwo_file->comp_dir != NULL)
11885 hash += htab_hash_string (dwo_file->comp_dir);
11886 return hash;
11887 }
11888
11889 static int
11890 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11891 {
11892 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11893 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11894
11895 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11896 return 0;
11897 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11898 return lhs->comp_dir == rhs->comp_dir;
11899 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11900 }
11901
11902 /* Allocate a hash table for DWO files. */
11903
11904 static htab_t
11905 allocate_dwo_file_hash_table (struct objfile *objfile)
11906 {
11907 return htab_create_alloc_ex (41,
11908 hash_dwo_file,
11909 eq_dwo_file,
11910 NULL,
11911 &objfile->objfile_obstack,
11912 hashtab_obstack_allocate,
11913 dummy_obstack_deallocate);
11914 }
11915
11916 /* Lookup DWO file DWO_NAME. */
11917
11918 static void **
11919 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11920 const char *dwo_name,
11921 const char *comp_dir)
11922 {
11923 struct dwo_file find_entry;
11924 void **slot;
11925
11926 if (dwarf2_per_objfile->dwo_files == NULL)
11927 dwarf2_per_objfile->dwo_files
11928 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11929
11930 memset (&find_entry, 0, sizeof (find_entry));
11931 find_entry.dwo_name = dwo_name;
11932 find_entry.comp_dir = comp_dir;
11933 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11934
11935 return slot;
11936 }
11937
11938 static hashval_t
11939 hash_dwo_unit (const void *item)
11940 {
11941 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11942
11943 /* This drops the top 32 bits of the id, but is ok for a hash. */
11944 return dwo_unit->signature;
11945 }
11946
11947 static int
11948 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11949 {
11950 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11951 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11952
11953 /* The signature is assumed to be unique within the DWO file.
11954 So while object file CU dwo_id's always have the value zero,
11955 that's OK, assuming each object file DWO file has only one CU,
11956 and that's the rule for now. */
11957 return lhs->signature == rhs->signature;
11958 }
11959
11960 /* Allocate a hash table for DWO CUs,TUs.
11961 There is one of these tables for each of CUs,TUs for each DWO file. */
11962
11963 static htab_t
11964 allocate_dwo_unit_table (struct objfile *objfile)
11965 {
11966 /* Start out with a pretty small number.
11967 Generally DWO files contain only one CU and maybe some TUs. */
11968 return htab_create_alloc_ex (3,
11969 hash_dwo_unit,
11970 eq_dwo_unit,
11971 NULL,
11972 &objfile->objfile_obstack,
11973 hashtab_obstack_allocate,
11974 dummy_obstack_deallocate);
11975 }
11976
11977 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11978
11979 struct create_dwo_cu_data
11980 {
11981 struct dwo_file *dwo_file;
11982 struct dwo_unit dwo_unit;
11983 };
11984
11985 /* die_reader_func for create_dwo_cu. */
11986
11987 static void
11988 create_dwo_cu_reader (const struct die_reader_specs *reader,
11989 const gdb_byte *info_ptr,
11990 struct die_info *comp_unit_die,
11991 int has_children,
11992 void *datap)
11993 {
11994 struct dwarf2_cu *cu = reader->cu;
11995 sect_offset sect_off = cu->per_cu->sect_off;
11996 struct dwarf2_section_info *section = cu->per_cu->section;
11997 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11998 struct dwo_file *dwo_file = data->dwo_file;
11999 struct dwo_unit *dwo_unit = &data->dwo_unit;
12000 struct attribute *attr;
12001
12002 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
12003 if (attr == NULL)
12004 {
12005 complaint (&symfile_complaints,
12006 _("Dwarf Error: debug entry at offset %s is missing"
12007 " its dwo_id [in module %s]"),
12008 sect_offset_str (sect_off), dwo_file->dwo_name);
12009 return;
12010 }
12011
12012 dwo_unit->dwo_file = dwo_file;
12013 dwo_unit->signature = DW_UNSND (attr);
12014 dwo_unit->section = section;
12015 dwo_unit->sect_off = sect_off;
12016 dwo_unit->length = cu->per_cu->length;
12017
12018 if (dwarf_read_debug)
12019 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
12020 sect_offset_str (sect_off),
12021 hex_string (dwo_unit->signature));
12022 }
12023
12024 /* Create the dwo_units for the CUs in a DWO_FILE.
12025 Note: This function processes DWO files only, not DWP files. */
12026
12027 static void
12028 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12029 struct dwo_file &dwo_file, dwarf2_section_info &section,
12030 htab_t &cus_htab)
12031 {
12032 struct objfile *objfile = dwarf2_per_objfile->objfile;
12033 const gdb_byte *info_ptr, *end_ptr;
12034
12035 dwarf2_read_section (objfile, &section);
12036 info_ptr = section.buffer;
12037
12038 if (info_ptr == NULL)
12039 return;
12040
12041 if (dwarf_read_debug)
12042 {
12043 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
12044 get_section_name (&section),
12045 get_section_file_name (&section));
12046 }
12047
12048 end_ptr = info_ptr + section.size;
12049 while (info_ptr < end_ptr)
12050 {
12051 struct dwarf2_per_cu_data per_cu;
12052 struct create_dwo_cu_data create_dwo_cu_data;
12053 struct dwo_unit *dwo_unit;
12054 void **slot;
12055 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
12056
12057 memset (&create_dwo_cu_data.dwo_unit, 0,
12058 sizeof (create_dwo_cu_data.dwo_unit));
12059 memset (&per_cu, 0, sizeof (per_cu));
12060 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
12061 per_cu.is_debug_types = 0;
12062 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12063 per_cu.section = &section;
12064 create_dwo_cu_data.dwo_file = &dwo_file;
12065
12066 init_cutu_and_read_dies_no_follow (
12067 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12068 info_ptr += per_cu.length;
12069
12070 // If the unit could not be parsed, skip it.
12071 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12072 continue;
12073
12074 if (cus_htab == NULL)
12075 cus_htab = allocate_dwo_unit_table (objfile);
12076
12077 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12078 *dwo_unit = create_dwo_cu_data.dwo_unit;
12079 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12080 gdb_assert (slot != NULL);
12081 if (*slot != NULL)
12082 {
12083 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12084 sect_offset dup_sect_off = dup_cu->sect_off;
12085
12086 complaint (&symfile_complaints,
12087 _("debug cu entry at offset %s is duplicate to"
12088 " the entry at offset %s, signature %s"),
12089 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12090 hex_string (dwo_unit->signature));
12091 }
12092 *slot = (void *)dwo_unit;
12093 }
12094 }
12095
12096 /* DWP file .debug_{cu,tu}_index section format:
12097 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12098
12099 DWP Version 1:
12100
12101 Both index sections have the same format, and serve to map a 64-bit
12102 signature to a set of section numbers. Each section begins with a header,
12103 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12104 indexes, and a pool of 32-bit section numbers. The index sections will be
12105 aligned at 8-byte boundaries in the file.
12106
12107 The index section header consists of:
12108
12109 V, 32 bit version number
12110 -, 32 bits unused
12111 N, 32 bit number of compilation units or type units in the index
12112 M, 32 bit number of slots in the hash table
12113
12114 Numbers are recorded using the byte order of the application binary.
12115
12116 The hash table begins at offset 16 in the section, and consists of an array
12117 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12118 order of the application binary). Unused slots in the hash table are 0.
12119 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12120
12121 The parallel table begins immediately after the hash table
12122 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12123 array of 32-bit indexes (using the byte order of the application binary),
12124 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12125 table contains a 32-bit index into the pool of section numbers. For unused
12126 hash table slots, the corresponding entry in the parallel table will be 0.
12127
12128 The pool of section numbers begins immediately following the hash table
12129 (at offset 16 + 12 * M from the beginning of the section). The pool of
12130 section numbers consists of an array of 32-bit words (using the byte order
12131 of the application binary). Each item in the array is indexed starting
12132 from 0. The hash table entry provides the index of the first section
12133 number in the set. Additional section numbers in the set follow, and the
12134 set is terminated by a 0 entry (section number 0 is not used in ELF).
12135
12136 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12137 section must be the first entry in the set, and the .debug_abbrev.dwo must
12138 be the second entry. Other members of the set may follow in any order.
12139
12140 ---
12141
12142 DWP Version 2:
12143
12144 DWP Version 2 combines all the .debug_info, etc. sections into one,
12145 and the entries in the index tables are now offsets into these sections.
12146 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12147 section.
12148
12149 Index Section Contents:
12150 Header
12151 Hash Table of Signatures dwp_hash_table.hash_table
12152 Parallel Table of Indices dwp_hash_table.unit_table
12153 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12154 Table of Section Sizes dwp_hash_table.v2.sizes
12155
12156 The index section header consists of:
12157
12158 V, 32 bit version number
12159 L, 32 bit number of columns in the table of section offsets
12160 N, 32 bit number of compilation units or type units in the index
12161 M, 32 bit number of slots in the hash table
12162
12163 Numbers are recorded using the byte order of the application binary.
12164
12165 The hash table has the same format as version 1.
12166 The parallel table of indices has the same format as version 1,
12167 except that the entries are origin-1 indices into the table of sections
12168 offsets and the table of section sizes.
12169
12170 The table of offsets begins immediately following the parallel table
12171 (at offset 16 + 12 * M from the beginning of the section). The table is
12172 a two-dimensional array of 32-bit words (using the byte order of the
12173 application binary), with L columns and N+1 rows, in row-major order.
12174 Each row in the array is indexed starting from 0. The first row provides
12175 a key to the remaining rows: each column in this row provides an identifier
12176 for a debug section, and the offsets in the same column of subsequent rows
12177 refer to that section. The section identifiers are:
12178
12179 DW_SECT_INFO 1 .debug_info.dwo
12180 DW_SECT_TYPES 2 .debug_types.dwo
12181 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12182 DW_SECT_LINE 4 .debug_line.dwo
12183 DW_SECT_LOC 5 .debug_loc.dwo
12184 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12185 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12186 DW_SECT_MACRO 8 .debug_macro.dwo
12187
12188 The offsets provided by the CU and TU index sections are the base offsets
12189 for the contributions made by each CU or TU to the corresponding section
12190 in the package file. Each CU and TU header contains an abbrev_offset
12191 field, used to find the abbreviations table for that CU or TU within the
12192 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12193 be interpreted as relative to the base offset given in the index section.
12194 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12195 should be interpreted as relative to the base offset for .debug_line.dwo,
12196 and offsets into other debug sections obtained from DWARF attributes should
12197 also be interpreted as relative to the corresponding base offset.
12198
12199 The table of sizes begins immediately following the table of offsets.
12200 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12201 with L columns and N rows, in row-major order. Each row in the array is
12202 indexed starting from 1 (row 0 is shared by the two tables).
12203
12204 ---
12205
12206 Hash table lookup is handled the same in version 1 and 2:
12207
12208 We assume that N and M will not exceed 2^32 - 1.
12209 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12210
12211 Given a 64-bit compilation unit signature or a type signature S, an entry
12212 in the hash table is located as follows:
12213
12214 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12215 the low-order k bits all set to 1.
12216
12217 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12218
12219 3) If the hash table entry at index H matches the signature, use that
12220 entry. If the hash table entry at index H is unused (all zeroes),
12221 terminate the search: the signature is not present in the table.
12222
12223 4) Let H = (H + H') modulo M. Repeat at Step 3.
12224
12225 Because M > N and H' and M are relatively prime, the search is guaranteed
12226 to stop at an unused slot or find the match. */
12227
12228 /* Create a hash table to map DWO IDs to their CU/TU entry in
12229 .debug_{info,types}.dwo in DWP_FILE.
12230 Returns NULL if there isn't one.
12231 Note: This function processes DWP files only, not DWO files. */
12232
12233 static struct dwp_hash_table *
12234 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12235 struct dwp_file *dwp_file, int is_debug_types)
12236 {
12237 struct objfile *objfile = dwarf2_per_objfile->objfile;
12238 bfd *dbfd = dwp_file->dbfd;
12239 const gdb_byte *index_ptr, *index_end;
12240 struct dwarf2_section_info *index;
12241 uint32_t version, nr_columns, nr_units, nr_slots;
12242 struct dwp_hash_table *htab;
12243
12244 if (is_debug_types)
12245 index = &dwp_file->sections.tu_index;
12246 else
12247 index = &dwp_file->sections.cu_index;
12248
12249 if (dwarf2_section_empty_p (index))
12250 return NULL;
12251 dwarf2_read_section (objfile, index);
12252
12253 index_ptr = index->buffer;
12254 index_end = index_ptr + index->size;
12255
12256 version = read_4_bytes (dbfd, index_ptr);
12257 index_ptr += 4;
12258 if (version == 2)
12259 nr_columns = read_4_bytes (dbfd, index_ptr);
12260 else
12261 nr_columns = 0;
12262 index_ptr += 4;
12263 nr_units = read_4_bytes (dbfd, index_ptr);
12264 index_ptr += 4;
12265 nr_slots = read_4_bytes (dbfd, index_ptr);
12266 index_ptr += 4;
12267
12268 if (version != 1 && version != 2)
12269 {
12270 error (_("Dwarf Error: unsupported DWP file version (%s)"
12271 " [in module %s]"),
12272 pulongest (version), dwp_file->name);
12273 }
12274 if (nr_slots != (nr_slots & -nr_slots))
12275 {
12276 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12277 " is not power of 2 [in module %s]"),
12278 pulongest (nr_slots), dwp_file->name);
12279 }
12280
12281 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12282 htab->version = version;
12283 htab->nr_columns = nr_columns;
12284 htab->nr_units = nr_units;
12285 htab->nr_slots = nr_slots;
12286 htab->hash_table = index_ptr;
12287 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12288
12289 /* Exit early if the table is empty. */
12290 if (nr_slots == 0 || nr_units == 0
12291 || (version == 2 && nr_columns == 0))
12292 {
12293 /* All must be zero. */
12294 if (nr_slots != 0 || nr_units != 0
12295 || (version == 2 && nr_columns != 0))
12296 {
12297 complaint (&symfile_complaints,
12298 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12299 " all zero [in modules %s]"),
12300 dwp_file->name);
12301 }
12302 return htab;
12303 }
12304
12305 if (version == 1)
12306 {
12307 htab->section_pool.v1.indices =
12308 htab->unit_table + sizeof (uint32_t) * nr_slots;
12309 /* It's harder to decide whether the section is too small in v1.
12310 V1 is deprecated anyway so we punt. */
12311 }
12312 else
12313 {
12314 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12315 int *ids = htab->section_pool.v2.section_ids;
12316 /* Reverse map for error checking. */
12317 int ids_seen[DW_SECT_MAX + 1];
12318 int i;
12319
12320 if (nr_columns < 2)
12321 {
12322 error (_("Dwarf Error: bad DWP hash table, too few columns"
12323 " in section table [in module %s]"),
12324 dwp_file->name);
12325 }
12326 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12327 {
12328 error (_("Dwarf Error: bad DWP hash table, too many columns"
12329 " in section table [in module %s]"),
12330 dwp_file->name);
12331 }
12332 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12333 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12334 for (i = 0; i < nr_columns; ++i)
12335 {
12336 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12337
12338 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12339 {
12340 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12341 " in section table [in module %s]"),
12342 id, dwp_file->name);
12343 }
12344 if (ids_seen[id] != -1)
12345 {
12346 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12347 " id %d in section table [in module %s]"),
12348 id, dwp_file->name);
12349 }
12350 ids_seen[id] = i;
12351 ids[i] = id;
12352 }
12353 /* Must have exactly one info or types section. */
12354 if (((ids_seen[DW_SECT_INFO] != -1)
12355 + (ids_seen[DW_SECT_TYPES] != -1))
12356 != 1)
12357 {
12358 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12359 " DWO info/types section [in module %s]"),
12360 dwp_file->name);
12361 }
12362 /* Must have an abbrev section. */
12363 if (ids_seen[DW_SECT_ABBREV] == -1)
12364 {
12365 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12366 " section [in module %s]"),
12367 dwp_file->name);
12368 }
12369 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12370 htab->section_pool.v2.sizes =
12371 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12372 * nr_units * nr_columns);
12373 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12374 * nr_units * nr_columns))
12375 > index_end)
12376 {
12377 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12378 " [in module %s]"),
12379 dwp_file->name);
12380 }
12381 }
12382
12383 return htab;
12384 }
12385
12386 /* Update SECTIONS with the data from SECTP.
12387
12388 This function is like the other "locate" section routines that are
12389 passed to bfd_map_over_sections, but in this context the sections to
12390 read comes from the DWP V1 hash table, not the full ELF section table.
12391
12392 The result is non-zero for success, or zero if an error was found. */
12393
12394 static int
12395 locate_v1_virtual_dwo_sections (asection *sectp,
12396 struct virtual_v1_dwo_sections *sections)
12397 {
12398 const struct dwop_section_names *names = &dwop_section_names;
12399
12400 if (section_is_p (sectp->name, &names->abbrev_dwo))
12401 {
12402 /* There can be only one. */
12403 if (sections->abbrev.s.section != NULL)
12404 return 0;
12405 sections->abbrev.s.section = sectp;
12406 sections->abbrev.size = bfd_get_section_size (sectp);
12407 }
12408 else if (section_is_p (sectp->name, &names->info_dwo)
12409 || section_is_p (sectp->name, &names->types_dwo))
12410 {
12411 /* There can be only one. */
12412 if (sections->info_or_types.s.section != NULL)
12413 return 0;
12414 sections->info_or_types.s.section = sectp;
12415 sections->info_or_types.size = bfd_get_section_size (sectp);
12416 }
12417 else if (section_is_p (sectp->name, &names->line_dwo))
12418 {
12419 /* There can be only one. */
12420 if (sections->line.s.section != NULL)
12421 return 0;
12422 sections->line.s.section = sectp;
12423 sections->line.size = bfd_get_section_size (sectp);
12424 }
12425 else if (section_is_p (sectp->name, &names->loc_dwo))
12426 {
12427 /* There can be only one. */
12428 if (sections->loc.s.section != NULL)
12429 return 0;
12430 sections->loc.s.section = sectp;
12431 sections->loc.size = bfd_get_section_size (sectp);
12432 }
12433 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12434 {
12435 /* There can be only one. */
12436 if (sections->macinfo.s.section != NULL)
12437 return 0;
12438 sections->macinfo.s.section = sectp;
12439 sections->macinfo.size = bfd_get_section_size (sectp);
12440 }
12441 else if (section_is_p (sectp->name, &names->macro_dwo))
12442 {
12443 /* There can be only one. */
12444 if (sections->macro.s.section != NULL)
12445 return 0;
12446 sections->macro.s.section = sectp;
12447 sections->macro.size = bfd_get_section_size (sectp);
12448 }
12449 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12450 {
12451 /* There can be only one. */
12452 if (sections->str_offsets.s.section != NULL)
12453 return 0;
12454 sections->str_offsets.s.section = sectp;
12455 sections->str_offsets.size = bfd_get_section_size (sectp);
12456 }
12457 else
12458 {
12459 /* No other kind of section is valid. */
12460 return 0;
12461 }
12462
12463 return 1;
12464 }
12465
12466 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12467 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12468 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12469 This is for DWP version 1 files. */
12470
12471 static struct dwo_unit *
12472 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12473 struct dwp_file *dwp_file,
12474 uint32_t unit_index,
12475 const char *comp_dir,
12476 ULONGEST signature, int is_debug_types)
12477 {
12478 struct objfile *objfile = dwarf2_per_objfile->objfile;
12479 const struct dwp_hash_table *dwp_htab =
12480 is_debug_types ? dwp_file->tus : dwp_file->cus;
12481 bfd *dbfd = dwp_file->dbfd;
12482 const char *kind = is_debug_types ? "TU" : "CU";
12483 struct dwo_file *dwo_file;
12484 struct dwo_unit *dwo_unit;
12485 struct virtual_v1_dwo_sections sections;
12486 void **dwo_file_slot;
12487 int i;
12488
12489 gdb_assert (dwp_file->version == 1);
12490
12491 if (dwarf_read_debug)
12492 {
12493 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12494 kind,
12495 pulongest (unit_index), hex_string (signature),
12496 dwp_file->name);
12497 }
12498
12499 /* Fetch the sections of this DWO unit.
12500 Put a limit on the number of sections we look for so that bad data
12501 doesn't cause us to loop forever. */
12502
12503 #define MAX_NR_V1_DWO_SECTIONS \
12504 (1 /* .debug_info or .debug_types */ \
12505 + 1 /* .debug_abbrev */ \
12506 + 1 /* .debug_line */ \
12507 + 1 /* .debug_loc */ \
12508 + 1 /* .debug_str_offsets */ \
12509 + 1 /* .debug_macro or .debug_macinfo */ \
12510 + 1 /* trailing zero */)
12511
12512 memset (&sections, 0, sizeof (sections));
12513
12514 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12515 {
12516 asection *sectp;
12517 uint32_t section_nr =
12518 read_4_bytes (dbfd,
12519 dwp_htab->section_pool.v1.indices
12520 + (unit_index + i) * sizeof (uint32_t));
12521
12522 if (section_nr == 0)
12523 break;
12524 if (section_nr >= dwp_file->num_sections)
12525 {
12526 error (_("Dwarf Error: bad DWP hash table, section number too large"
12527 " [in module %s]"),
12528 dwp_file->name);
12529 }
12530
12531 sectp = dwp_file->elf_sections[section_nr];
12532 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12533 {
12534 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12535 " [in module %s]"),
12536 dwp_file->name);
12537 }
12538 }
12539
12540 if (i < 2
12541 || dwarf2_section_empty_p (&sections.info_or_types)
12542 || dwarf2_section_empty_p (&sections.abbrev))
12543 {
12544 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12545 " [in module %s]"),
12546 dwp_file->name);
12547 }
12548 if (i == MAX_NR_V1_DWO_SECTIONS)
12549 {
12550 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12551 " [in module %s]"),
12552 dwp_file->name);
12553 }
12554
12555 /* It's easier for the rest of the code if we fake a struct dwo_file and
12556 have dwo_unit "live" in that. At least for now.
12557
12558 The DWP file can be made up of a random collection of CUs and TUs.
12559 However, for each CU + set of TUs that came from the same original DWO
12560 file, we can combine them back into a virtual DWO file to save space
12561 (fewer struct dwo_file objects to allocate). Remember that for really
12562 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12563
12564 std::string virtual_dwo_name =
12565 string_printf ("virtual-dwo/%d-%d-%d-%d",
12566 get_section_id (&sections.abbrev),
12567 get_section_id (&sections.line),
12568 get_section_id (&sections.loc),
12569 get_section_id (&sections.str_offsets));
12570 /* Can we use an existing virtual DWO file? */
12571 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12572 virtual_dwo_name.c_str (),
12573 comp_dir);
12574 /* Create one if necessary. */
12575 if (*dwo_file_slot == NULL)
12576 {
12577 if (dwarf_read_debug)
12578 {
12579 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12580 virtual_dwo_name.c_str ());
12581 }
12582 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12583 dwo_file->dwo_name
12584 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12585 virtual_dwo_name.c_str (),
12586 virtual_dwo_name.size ());
12587 dwo_file->comp_dir = comp_dir;
12588 dwo_file->sections.abbrev = sections.abbrev;
12589 dwo_file->sections.line = sections.line;
12590 dwo_file->sections.loc = sections.loc;
12591 dwo_file->sections.macinfo = sections.macinfo;
12592 dwo_file->sections.macro = sections.macro;
12593 dwo_file->sections.str_offsets = sections.str_offsets;
12594 /* The "str" section is global to the entire DWP file. */
12595 dwo_file->sections.str = dwp_file->sections.str;
12596 /* The info or types section is assigned below to dwo_unit,
12597 there's no need to record it in dwo_file.
12598 Also, we can't simply record type sections in dwo_file because
12599 we record a pointer into the vector in dwo_unit. As we collect more
12600 types we'll grow the vector and eventually have to reallocate space
12601 for it, invalidating all copies of pointers into the previous
12602 contents. */
12603 *dwo_file_slot = dwo_file;
12604 }
12605 else
12606 {
12607 if (dwarf_read_debug)
12608 {
12609 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12610 virtual_dwo_name.c_str ());
12611 }
12612 dwo_file = (struct dwo_file *) *dwo_file_slot;
12613 }
12614
12615 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12616 dwo_unit->dwo_file = dwo_file;
12617 dwo_unit->signature = signature;
12618 dwo_unit->section =
12619 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12620 *dwo_unit->section = sections.info_or_types;
12621 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12622
12623 return dwo_unit;
12624 }
12625
12626 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12627 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12628 piece within that section used by a TU/CU, return a virtual section
12629 of just that piece. */
12630
12631 static struct dwarf2_section_info
12632 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12633 struct dwarf2_section_info *section,
12634 bfd_size_type offset, bfd_size_type size)
12635 {
12636 struct dwarf2_section_info result;
12637 asection *sectp;
12638
12639 gdb_assert (section != NULL);
12640 gdb_assert (!section->is_virtual);
12641
12642 memset (&result, 0, sizeof (result));
12643 result.s.containing_section = section;
12644 result.is_virtual = 1;
12645
12646 if (size == 0)
12647 return result;
12648
12649 sectp = get_section_bfd_section (section);
12650
12651 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12652 bounds of the real section. This is a pretty-rare event, so just
12653 flag an error (easier) instead of a warning and trying to cope. */
12654 if (sectp == NULL
12655 || offset + size > bfd_get_section_size (sectp))
12656 {
12657 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12658 " in section %s [in module %s]"),
12659 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12660 objfile_name (dwarf2_per_objfile->objfile));
12661 }
12662
12663 result.virtual_offset = offset;
12664 result.size = size;
12665 return result;
12666 }
12667
12668 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12669 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12670 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12671 This is for DWP version 2 files. */
12672
12673 static struct dwo_unit *
12674 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12675 struct dwp_file *dwp_file,
12676 uint32_t unit_index,
12677 const char *comp_dir,
12678 ULONGEST signature, int is_debug_types)
12679 {
12680 struct objfile *objfile = dwarf2_per_objfile->objfile;
12681 const struct dwp_hash_table *dwp_htab =
12682 is_debug_types ? dwp_file->tus : dwp_file->cus;
12683 bfd *dbfd = dwp_file->dbfd;
12684 const char *kind = is_debug_types ? "TU" : "CU";
12685 struct dwo_file *dwo_file;
12686 struct dwo_unit *dwo_unit;
12687 struct virtual_v2_dwo_sections sections;
12688 void **dwo_file_slot;
12689 int i;
12690
12691 gdb_assert (dwp_file->version == 2);
12692
12693 if (dwarf_read_debug)
12694 {
12695 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12696 kind,
12697 pulongest (unit_index), hex_string (signature),
12698 dwp_file->name);
12699 }
12700
12701 /* Fetch the section offsets of this DWO unit. */
12702
12703 memset (&sections, 0, sizeof (sections));
12704
12705 for (i = 0; i < dwp_htab->nr_columns; ++i)
12706 {
12707 uint32_t offset = read_4_bytes (dbfd,
12708 dwp_htab->section_pool.v2.offsets
12709 + (((unit_index - 1) * dwp_htab->nr_columns
12710 + i)
12711 * sizeof (uint32_t)));
12712 uint32_t size = read_4_bytes (dbfd,
12713 dwp_htab->section_pool.v2.sizes
12714 + (((unit_index - 1) * dwp_htab->nr_columns
12715 + i)
12716 * sizeof (uint32_t)));
12717
12718 switch (dwp_htab->section_pool.v2.section_ids[i])
12719 {
12720 case DW_SECT_INFO:
12721 case DW_SECT_TYPES:
12722 sections.info_or_types_offset = offset;
12723 sections.info_or_types_size = size;
12724 break;
12725 case DW_SECT_ABBREV:
12726 sections.abbrev_offset = offset;
12727 sections.abbrev_size = size;
12728 break;
12729 case DW_SECT_LINE:
12730 sections.line_offset = offset;
12731 sections.line_size = size;
12732 break;
12733 case DW_SECT_LOC:
12734 sections.loc_offset = offset;
12735 sections.loc_size = size;
12736 break;
12737 case DW_SECT_STR_OFFSETS:
12738 sections.str_offsets_offset = offset;
12739 sections.str_offsets_size = size;
12740 break;
12741 case DW_SECT_MACINFO:
12742 sections.macinfo_offset = offset;
12743 sections.macinfo_size = size;
12744 break;
12745 case DW_SECT_MACRO:
12746 sections.macro_offset = offset;
12747 sections.macro_size = size;
12748 break;
12749 }
12750 }
12751
12752 /* It's easier for the rest of the code if we fake a struct dwo_file and
12753 have dwo_unit "live" in that. At least for now.
12754
12755 The DWP file can be made up of a random collection of CUs and TUs.
12756 However, for each CU + set of TUs that came from the same original DWO
12757 file, we can combine them back into a virtual DWO file to save space
12758 (fewer struct dwo_file objects to allocate). Remember that for really
12759 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12760
12761 std::string virtual_dwo_name =
12762 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12763 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12764 (long) (sections.line_size ? sections.line_offset : 0),
12765 (long) (sections.loc_size ? sections.loc_offset : 0),
12766 (long) (sections.str_offsets_size
12767 ? sections.str_offsets_offset : 0));
12768 /* Can we use an existing virtual DWO file? */
12769 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12770 virtual_dwo_name.c_str (),
12771 comp_dir);
12772 /* Create one if necessary. */
12773 if (*dwo_file_slot == NULL)
12774 {
12775 if (dwarf_read_debug)
12776 {
12777 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12778 virtual_dwo_name.c_str ());
12779 }
12780 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12781 dwo_file->dwo_name
12782 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12783 virtual_dwo_name.c_str (),
12784 virtual_dwo_name.size ());
12785 dwo_file->comp_dir = comp_dir;
12786 dwo_file->sections.abbrev =
12787 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12788 sections.abbrev_offset, sections.abbrev_size);
12789 dwo_file->sections.line =
12790 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12791 sections.line_offset, sections.line_size);
12792 dwo_file->sections.loc =
12793 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12794 sections.loc_offset, sections.loc_size);
12795 dwo_file->sections.macinfo =
12796 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12797 sections.macinfo_offset, sections.macinfo_size);
12798 dwo_file->sections.macro =
12799 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12800 sections.macro_offset, sections.macro_size);
12801 dwo_file->sections.str_offsets =
12802 create_dwp_v2_section (dwarf2_per_objfile,
12803 &dwp_file->sections.str_offsets,
12804 sections.str_offsets_offset,
12805 sections.str_offsets_size);
12806 /* The "str" section is global to the entire DWP file. */
12807 dwo_file->sections.str = dwp_file->sections.str;
12808 /* The info or types section is assigned below to dwo_unit,
12809 there's no need to record it in dwo_file.
12810 Also, we can't simply record type sections in dwo_file because
12811 we record a pointer into the vector in dwo_unit. As we collect more
12812 types we'll grow the vector and eventually have to reallocate space
12813 for it, invalidating all copies of pointers into the previous
12814 contents. */
12815 *dwo_file_slot = dwo_file;
12816 }
12817 else
12818 {
12819 if (dwarf_read_debug)
12820 {
12821 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12822 virtual_dwo_name.c_str ());
12823 }
12824 dwo_file = (struct dwo_file *) *dwo_file_slot;
12825 }
12826
12827 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12828 dwo_unit->dwo_file = dwo_file;
12829 dwo_unit->signature = signature;
12830 dwo_unit->section =
12831 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12832 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12833 is_debug_types
12834 ? &dwp_file->sections.types
12835 : &dwp_file->sections.info,
12836 sections.info_or_types_offset,
12837 sections.info_or_types_size);
12838 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12839
12840 return dwo_unit;
12841 }
12842
12843 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12844 Returns NULL if the signature isn't found. */
12845
12846 static struct dwo_unit *
12847 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12848 struct dwp_file *dwp_file, const char *comp_dir,
12849 ULONGEST signature, int is_debug_types)
12850 {
12851 const struct dwp_hash_table *dwp_htab =
12852 is_debug_types ? dwp_file->tus : dwp_file->cus;
12853 bfd *dbfd = dwp_file->dbfd;
12854 uint32_t mask = dwp_htab->nr_slots - 1;
12855 uint32_t hash = signature & mask;
12856 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12857 unsigned int i;
12858 void **slot;
12859 struct dwo_unit find_dwo_cu;
12860
12861 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12862 find_dwo_cu.signature = signature;
12863 slot = htab_find_slot (is_debug_types
12864 ? dwp_file->loaded_tus
12865 : dwp_file->loaded_cus,
12866 &find_dwo_cu, INSERT);
12867
12868 if (*slot != NULL)
12869 return (struct dwo_unit *) *slot;
12870
12871 /* Use a for loop so that we don't loop forever on bad debug info. */
12872 for (i = 0; i < dwp_htab->nr_slots; ++i)
12873 {
12874 ULONGEST signature_in_table;
12875
12876 signature_in_table =
12877 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12878 if (signature_in_table == signature)
12879 {
12880 uint32_t unit_index =
12881 read_4_bytes (dbfd,
12882 dwp_htab->unit_table + hash * sizeof (uint32_t));
12883
12884 if (dwp_file->version == 1)
12885 {
12886 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12887 dwp_file, unit_index,
12888 comp_dir, signature,
12889 is_debug_types);
12890 }
12891 else
12892 {
12893 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12894 dwp_file, unit_index,
12895 comp_dir, signature,
12896 is_debug_types);
12897 }
12898 return (struct dwo_unit *) *slot;
12899 }
12900 if (signature_in_table == 0)
12901 return NULL;
12902 hash = (hash + hash2) & mask;
12903 }
12904
12905 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12906 " [in module %s]"),
12907 dwp_file->name);
12908 }
12909
12910 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12911 Open the file specified by FILE_NAME and hand it off to BFD for
12912 preliminary analysis. Return a newly initialized bfd *, which
12913 includes a canonicalized copy of FILE_NAME.
12914 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12915 SEARCH_CWD is true if the current directory is to be searched.
12916 It will be searched before debug-file-directory.
12917 If successful, the file is added to the bfd include table of the
12918 objfile's bfd (see gdb_bfd_record_inclusion).
12919 If unable to find/open the file, return NULL.
12920 NOTE: This function is derived from symfile_bfd_open. */
12921
12922 static gdb_bfd_ref_ptr
12923 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12924 const char *file_name, int is_dwp, int search_cwd)
12925 {
12926 int desc;
12927 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12928 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12929 to debug_file_directory. */
12930 const char *search_path;
12931 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12932
12933 gdb::unique_xmalloc_ptr<char> search_path_holder;
12934 if (search_cwd)
12935 {
12936 if (*debug_file_directory != '\0')
12937 {
12938 search_path_holder.reset (concat (".", dirname_separator_string,
12939 debug_file_directory,
12940 (char *) NULL));
12941 search_path = search_path_holder.get ();
12942 }
12943 else
12944 search_path = ".";
12945 }
12946 else
12947 search_path = debug_file_directory;
12948
12949 openp_flags flags = OPF_RETURN_REALPATH;
12950 if (is_dwp)
12951 flags |= OPF_SEARCH_IN_PATH;
12952
12953 gdb::unique_xmalloc_ptr<char> absolute_name;
12954 desc = openp (search_path, flags, file_name,
12955 O_RDONLY | O_BINARY, &absolute_name);
12956 if (desc < 0)
12957 return NULL;
12958
12959 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12960 gnutarget, desc));
12961 if (sym_bfd == NULL)
12962 return NULL;
12963 bfd_set_cacheable (sym_bfd.get (), 1);
12964
12965 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12966 return NULL;
12967
12968 /* Success. Record the bfd as having been included by the objfile's bfd.
12969 This is important because things like demangled_names_hash lives in the
12970 objfile's per_bfd space and may have references to things like symbol
12971 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12972 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12973
12974 return sym_bfd;
12975 }
12976
12977 /* Try to open DWO file FILE_NAME.
12978 COMP_DIR is the DW_AT_comp_dir attribute.
12979 The result is the bfd handle of the file.
12980 If there is a problem finding or opening the file, return NULL.
12981 Upon success, the canonicalized path of the file is stored in the bfd,
12982 same as symfile_bfd_open. */
12983
12984 static gdb_bfd_ref_ptr
12985 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12986 const char *file_name, const char *comp_dir)
12987 {
12988 if (IS_ABSOLUTE_PATH (file_name))
12989 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12990 0 /*is_dwp*/, 0 /*search_cwd*/);
12991
12992 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12993
12994 if (comp_dir != NULL)
12995 {
12996 char *path_to_try = concat (comp_dir, SLASH_STRING,
12997 file_name, (char *) NULL);
12998
12999 /* NOTE: If comp_dir is a relative path, this will also try the
13000 search path, which seems useful. */
13001 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
13002 path_to_try,
13003 0 /*is_dwp*/,
13004 1 /*search_cwd*/));
13005 xfree (path_to_try);
13006 if (abfd != NULL)
13007 return abfd;
13008 }
13009
13010 /* That didn't work, try debug-file-directory, which, despite its name,
13011 is a list of paths. */
13012
13013 if (*debug_file_directory == '\0')
13014 return NULL;
13015
13016 return try_open_dwop_file (dwarf2_per_objfile, file_name,
13017 0 /*is_dwp*/, 1 /*search_cwd*/);
13018 }
13019
13020 /* This function is mapped across the sections and remembers the offset and
13021 size of each of the DWO debugging sections we are interested in. */
13022
13023 static void
13024 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
13025 {
13026 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
13027 const struct dwop_section_names *names = &dwop_section_names;
13028
13029 if (section_is_p (sectp->name, &names->abbrev_dwo))
13030 {
13031 dwo_sections->abbrev.s.section = sectp;
13032 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
13033 }
13034 else if (section_is_p (sectp->name, &names->info_dwo))
13035 {
13036 dwo_sections->info.s.section = sectp;
13037 dwo_sections->info.size = bfd_get_section_size (sectp);
13038 }
13039 else if (section_is_p (sectp->name, &names->line_dwo))
13040 {
13041 dwo_sections->line.s.section = sectp;
13042 dwo_sections->line.size = bfd_get_section_size (sectp);
13043 }
13044 else if (section_is_p (sectp->name, &names->loc_dwo))
13045 {
13046 dwo_sections->loc.s.section = sectp;
13047 dwo_sections->loc.size = bfd_get_section_size (sectp);
13048 }
13049 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13050 {
13051 dwo_sections->macinfo.s.section = sectp;
13052 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
13053 }
13054 else if (section_is_p (sectp->name, &names->macro_dwo))
13055 {
13056 dwo_sections->macro.s.section = sectp;
13057 dwo_sections->macro.size = bfd_get_section_size (sectp);
13058 }
13059 else if (section_is_p (sectp->name, &names->str_dwo))
13060 {
13061 dwo_sections->str.s.section = sectp;
13062 dwo_sections->str.size = bfd_get_section_size (sectp);
13063 }
13064 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13065 {
13066 dwo_sections->str_offsets.s.section = sectp;
13067 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
13068 }
13069 else if (section_is_p (sectp->name, &names->types_dwo))
13070 {
13071 struct dwarf2_section_info type_section;
13072
13073 memset (&type_section, 0, sizeof (type_section));
13074 type_section.s.section = sectp;
13075 type_section.size = bfd_get_section_size (sectp);
13076 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
13077 &type_section);
13078 }
13079 }
13080
13081 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13082 by PER_CU. This is for the non-DWP case.
13083 The result is NULL if DWO_NAME can't be found. */
13084
13085 static struct dwo_file *
13086 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13087 const char *dwo_name, const char *comp_dir)
13088 {
13089 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13090 struct objfile *objfile = dwarf2_per_objfile->objfile;
13091 struct dwo_file *dwo_file;
13092 struct cleanup *cleanups;
13093
13094 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
13095 if (dbfd == NULL)
13096 {
13097 if (dwarf_read_debug)
13098 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13099 return NULL;
13100 }
13101 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
13102 dwo_file->dwo_name = dwo_name;
13103 dwo_file->comp_dir = comp_dir;
13104 dwo_file->dbfd = dbfd.release ();
13105
13106 free_dwo_file_cleanup_data *cleanup_data = XNEW (free_dwo_file_cleanup_data);
13107 cleanup_data->dwo_file = dwo_file;
13108 cleanup_data->dwarf2_per_objfile = dwarf2_per_objfile;
13109
13110 cleanups = make_cleanup (free_dwo_file_cleanup, cleanup_data);
13111
13112 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13113 &dwo_file->sections);
13114
13115 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13116 dwo_file->cus);
13117
13118 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file,
13119 dwo_file->sections.types, dwo_file->tus);
13120
13121 discard_cleanups (cleanups);
13122
13123 if (dwarf_read_debug)
13124 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13125
13126 return dwo_file;
13127 }
13128
13129 /* This function is mapped across the sections and remembers the offset and
13130 size of each of the DWP debugging sections common to version 1 and 2 that
13131 we are interested in. */
13132
13133 static void
13134 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13135 void *dwp_file_ptr)
13136 {
13137 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13138 const struct dwop_section_names *names = &dwop_section_names;
13139 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13140
13141 /* Record the ELF section number for later lookup: this is what the
13142 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13143 gdb_assert (elf_section_nr < dwp_file->num_sections);
13144 dwp_file->elf_sections[elf_section_nr] = sectp;
13145
13146 /* Look for specific sections that we need. */
13147 if (section_is_p (sectp->name, &names->str_dwo))
13148 {
13149 dwp_file->sections.str.s.section = sectp;
13150 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13151 }
13152 else if (section_is_p (sectp->name, &names->cu_index))
13153 {
13154 dwp_file->sections.cu_index.s.section = sectp;
13155 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13156 }
13157 else if (section_is_p (sectp->name, &names->tu_index))
13158 {
13159 dwp_file->sections.tu_index.s.section = sectp;
13160 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13161 }
13162 }
13163
13164 /* This function is mapped across the sections and remembers the offset and
13165 size of each of the DWP version 2 debugging sections that we are interested
13166 in. This is split into a separate function because we don't know if we
13167 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13168
13169 static void
13170 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13171 {
13172 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13173 const struct dwop_section_names *names = &dwop_section_names;
13174 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13175
13176 /* Record the ELF section number for later lookup: this is what the
13177 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13178 gdb_assert (elf_section_nr < dwp_file->num_sections);
13179 dwp_file->elf_sections[elf_section_nr] = sectp;
13180
13181 /* Look for specific sections that we need. */
13182 if (section_is_p (sectp->name, &names->abbrev_dwo))
13183 {
13184 dwp_file->sections.abbrev.s.section = sectp;
13185 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13186 }
13187 else if (section_is_p (sectp->name, &names->info_dwo))
13188 {
13189 dwp_file->sections.info.s.section = sectp;
13190 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13191 }
13192 else if (section_is_p (sectp->name, &names->line_dwo))
13193 {
13194 dwp_file->sections.line.s.section = sectp;
13195 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13196 }
13197 else if (section_is_p (sectp->name, &names->loc_dwo))
13198 {
13199 dwp_file->sections.loc.s.section = sectp;
13200 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13201 }
13202 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13203 {
13204 dwp_file->sections.macinfo.s.section = sectp;
13205 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13206 }
13207 else if (section_is_p (sectp->name, &names->macro_dwo))
13208 {
13209 dwp_file->sections.macro.s.section = sectp;
13210 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13211 }
13212 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13213 {
13214 dwp_file->sections.str_offsets.s.section = sectp;
13215 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13216 }
13217 else if (section_is_p (sectp->name, &names->types_dwo))
13218 {
13219 dwp_file->sections.types.s.section = sectp;
13220 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13221 }
13222 }
13223
13224 /* Hash function for dwp_file loaded CUs/TUs. */
13225
13226 static hashval_t
13227 hash_dwp_loaded_cutus (const void *item)
13228 {
13229 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13230
13231 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13232 return dwo_unit->signature;
13233 }
13234
13235 /* Equality function for dwp_file loaded CUs/TUs. */
13236
13237 static int
13238 eq_dwp_loaded_cutus (const void *a, const void *b)
13239 {
13240 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13241 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13242
13243 return dua->signature == dub->signature;
13244 }
13245
13246 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13247
13248 static htab_t
13249 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13250 {
13251 return htab_create_alloc_ex (3,
13252 hash_dwp_loaded_cutus,
13253 eq_dwp_loaded_cutus,
13254 NULL,
13255 &objfile->objfile_obstack,
13256 hashtab_obstack_allocate,
13257 dummy_obstack_deallocate);
13258 }
13259
13260 /* Try to open DWP file FILE_NAME.
13261 The result is the bfd handle of the file.
13262 If there is a problem finding or opening the file, return NULL.
13263 Upon success, the canonicalized path of the file is stored in the bfd,
13264 same as symfile_bfd_open. */
13265
13266 static gdb_bfd_ref_ptr
13267 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13268 const char *file_name)
13269 {
13270 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13271 1 /*is_dwp*/,
13272 1 /*search_cwd*/));
13273 if (abfd != NULL)
13274 return abfd;
13275
13276 /* Work around upstream bug 15652.
13277 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13278 [Whether that's a "bug" is debatable, but it is getting in our way.]
13279 We have no real idea where the dwp file is, because gdb's realpath-ing
13280 of the executable's path may have discarded the needed info.
13281 [IWBN if the dwp file name was recorded in the executable, akin to
13282 .gnu_debuglink, but that doesn't exist yet.]
13283 Strip the directory from FILE_NAME and search again. */
13284 if (*debug_file_directory != '\0')
13285 {
13286 /* Don't implicitly search the current directory here.
13287 If the user wants to search "." to handle this case,
13288 it must be added to debug-file-directory. */
13289 return try_open_dwop_file (dwarf2_per_objfile,
13290 lbasename (file_name), 1 /*is_dwp*/,
13291 0 /*search_cwd*/);
13292 }
13293
13294 return NULL;
13295 }
13296
13297 /* Initialize the use of the DWP file for the current objfile.
13298 By convention the name of the DWP file is ${objfile}.dwp.
13299 The result is NULL if it can't be found. */
13300
13301 static struct dwp_file *
13302 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13303 {
13304 struct objfile *objfile = dwarf2_per_objfile->objfile;
13305 struct dwp_file *dwp_file;
13306
13307 /* Try to find first .dwp for the binary file before any symbolic links
13308 resolving. */
13309
13310 /* If the objfile is a debug file, find the name of the real binary
13311 file and get the name of dwp file from there. */
13312 std::string dwp_name;
13313 if (objfile->separate_debug_objfile_backlink != NULL)
13314 {
13315 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13316 const char *backlink_basename = lbasename (backlink->original_name);
13317
13318 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13319 }
13320 else
13321 dwp_name = objfile->original_name;
13322
13323 dwp_name += ".dwp";
13324
13325 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13326 if (dbfd == NULL
13327 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13328 {
13329 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13330 dwp_name = objfile_name (objfile);
13331 dwp_name += ".dwp";
13332 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13333 }
13334
13335 if (dbfd == NULL)
13336 {
13337 if (dwarf_read_debug)
13338 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13339 return NULL;
13340 }
13341 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
13342 dwp_file->name = bfd_get_filename (dbfd.get ());
13343 dwp_file->dbfd = dbfd.release ();
13344
13345 /* +1: section 0 is unused */
13346 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13347 dwp_file->elf_sections =
13348 OBSTACK_CALLOC (&objfile->objfile_obstack,
13349 dwp_file->num_sections, asection *);
13350
13351 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13352 dwp_file);
13353
13354 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
13355
13356 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
13357
13358 /* The DWP file version is stored in the hash table. Oh well. */
13359 if (dwp_file->cus && dwp_file->tus
13360 && dwp_file->cus->version != dwp_file->tus->version)
13361 {
13362 /* Technically speaking, we should try to limp along, but this is
13363 pretty bizarre. We use pulongest here because that's the established
13364 portability solution (e.g, we cannot use %u for uint32_t). */
13365 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13366 " TU version %s [in DWP file %s]"),
13367 pulongest (dwp_file->cus->version),
13368 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13369 }
13370
13371 if (dwp_file->cus)
13372 dwp_file->version = dwp_file->cus->version;
13373 else if (dwp_file->tus)
13374 dwp_file->version = dwp_file->tus->version;
13375 else
13376 dwp_file->version = 2;
13377
13378 if (dwp_file->version == 2)
13379 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13380 dwp_file);
13381
13382 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13383 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13384
13385 if (dwarf_read_debug)
13386 {
13387 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13388 fprintf_unfiltered (gdb_stdlog,
13389 " %s CUs, %s TUs\n",
13390 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13391 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13392 }
13393
13394 return dwp_file;
13395 }
13396
13397 /* Wrapper around open_and_init_dwp_file, only open it once. */
13398
13399 static struct dwp_file *
13400 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13401 {
13402 if (! dwarf2_per_objfile->dwp_checked)
13403 {
13404 dwarf2_per_objfile->dwp_file
13405 = open_and_init_dwp_file (dwarf2_per_objfile);
13406 dwarf2_per_objfile->dwp_checked = 1;
13407 }
13408 return dwarf2_per_objfile->dwp_file;
13409 }
13410
13411 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13412 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13413 or in the DWP file for the objfile, referenced by THIS_UNIT.
13414 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13415 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13416
13417 This is called, for example, when wanting to read a variable with a
13418 complex location. Therefore we don't want to do file i/o for every call.
13419 Therefore we don't want to look for a DWO file on every call.
13420 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13421 then we check if we've already seen DWO_NAME, and only THEN do we check
13422 for a DWO file.
13423
13424 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13425 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13426
13427 static struct dwo_unit *
13428 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13429 const char *dwo_name, const char *comp_dir,
13430 ULONGEST signature, int is_debug_types)
13431 {
13432 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13433 struct objfile *objfile = dwarf2_per_objfile->objfile;
13434 const char *kind = is_debug_types ? "TU" : "CU";
13435 void **dwo_file_slot;
13436 struct dwo_file *dwo_file;
13437 struct dwp_file *dwp_file;
13438
13439 /* First see if there's a DWP file.
13440 If we have a DWP file but didn't find the DWO inside it, don't
13441 look for the original DWO file. It makes gdb behave differently
13442 depending on whether one is debugging in the build tree. */
13443
13444 dwp_file = get_dwp_file (dwarf2_per_objfile);
13445 if (dwp_file != NULL)
13446 {
13447 const struct dwp_hash_table *dwp_htab =
13448 is_debug_types ? dwp_file->tus : dwp_file->cus;
13449
13450 if (dwp_htab != NULL)
13451 {
13452 struct dwo_unit *dwo_cutu =
13453 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13454 signature, is_debug_types);
13455
13456 if (dwo_cutu != NULL)
13457 {
13458 if (dwarf_read_debug)
13459 {
13460 fprintf_unfiltered (gdb_stdlog,
13461 "Virtual DWO %s %s found: @%s\n",
13462 kind, hex_string (signature),
13463 host_address_to_string (dwo_cutu));
13464 }
13465 return dwo_cutu;
13466 }
13467 }
13468 }
13469 else
13470 {
13471 /* No DWP file, look for the DWO file. */
13472
13473 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13474 dwo_name, comp_dir);
13475 if (*dwo_file_slot == NULL)
13476 {
13477 /* Read in the file and build a table of the CUs/TUs it contains. */
13478 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13479 }
13480 /* NOTE: This will be NULL if unable to open the file. */
13481 dwo_file = (struct dwo_file *) *dwo_file_slot;
13482
13483 if (dwo_file != NULL)
13484 {
13485 struct dwo_unit *dwo_cutu = NULL;
13486
13487 if (is_debug_types && dwo_file->tus)
13488 {
13489 struct dwo_unit find_dwo_cutu;
13490
13491 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13492 find_dwo_cutu.signature = signature;
13493 dwo_cutu
13494 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13495 }
13496 else if (!is_debug_types && dwo_file->cus)
13497 {
13498 struct dwo_unit find_dwo_cutu;
13499
13500 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13501 find_dwo_cutu.signature = signature;
13502 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13503 &find_dwo_cutu);
13504 }
13505
13506 if (dwo_cutu != NULL)
13507 {
13508 if (dwarf_read_debug)
13509 {
13510 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13511 kind, dwo_name, hex_string (signature),
13512 host_address_to_string (dwo_cutu));
13513 }
13514 return dwo_cutu;
13515 }
13516 }
13517 }
13518
13519 /* We didn't find it. This could mean a dwo_id mismatch, or
13520 someone deleted the DWO/DWP file, or the search path isn't set up
13521 correctly to find the file. */
13522
13523 if (dwarf_read_debug)
13524 {
13525 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13526 kind, dwo_name, hex_string (signature));
13527 }
13528
13529 /* This is a warning and not a complaint because it can be caused by
13530 pilot error (e.g., user accidentally deleting the DWO). */
13531 {
13532 /* Print the name of the DWP file if we looked there, helps the user
13533 better diagnose the problem. */
13534 std::string dwp_text;
13535
13536 if (dwp_file != NULL)
13537 dwp_text = string_printf (" [in DWP file %s]",
13538 lbasename (dwp_file->name));
13539
13540 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13541 " [in module %s]"),
13542 kind, dwo_name, hex_string (signature),
13543 dwp_text.c_str (),
13544 this_unit->is_debug_types ? "TU" : "CU",
13545 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13546 }
13547 return NULL;
13548 }
13549
13550 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13551 See lookup_dwo_cutu_unit for details. */
13552
13553 static struct dwo_unit *
13554 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13555 const char *dwo_name, const char *comp_dir,
13556 ULONGEST signature)
13557 {
13558 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13559 }
13560
13561 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13562 See lookup_dwo_cutu_unit for details. */
13563
13564 static struct dwo_unit *
13565 lookup_dwo_type_unit (struct signatured_type *this_tu,
13566 const char *dwo_name, const char *comp_dir)
13567 {
13568 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13569 }
13570
13571 /* Traversal function for queue_and_load_all_dwo_tus. */
13572
13573 static int
13574 queue_and_load_dwo_tu (void **slot, void *info)
13575 {
13576 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13577 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13578 ULONGEST signature = dwo_unit->signature;
13579 struct signatured_type *sig_type =
13580 lookup_dwo_signatured_type (per_cu->cu, signature);
13581
13582 if (sig_type != NULL)
13583 {
13584 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13585
13586 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13587 a real dependency of PER_CU on SIG_TYPE. That is detected later
13588 while processing PER_CU. */
13589 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13590 load_full_type_unit (sig_cu);
13591 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13592 }
13593
13594 return 1;
13595 }
13596
13597 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13598 The DWO may have the only definition of the type, though it may not be
13599 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13600 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13601
13602 static void
13603 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13604 {
13605 struct dwo_unit *dwo_unit;
13606 struct dwo_file *dwo_file;
13607
13608 gdb_assert (!per_cu->is_debug_types);
13609 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13610 gdb_assert (per_cu->cu != NULL);
13611
13612 dwo_unit = per_cu->cu->dwo_unit;
13613 gdb_assert (dwo_unit != NULL);
13614
13615 dwo_file = dwo_unit->dwo_file;
13616 if (dwo_file->tus != NULL)
13617 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13618 }
13619
13620 /* Free all resources associated with DWO_FILE.
13621 Close the DWO file and munmap the sections.
13622 All memory should be on the objfile obstack. */
13623
13624 static void
13625 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
13626 {
13627
13628 /* Note: dbfd is NULL for virtual DWO files. */
13629 gdb_bfd_unref (dwo_file->dbfd);
13630
13631 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13632 }
13633
13634 /* Wrapper for free_dwo_file for use in cleanups. */
13635
13636 static void
13637 free_dwo_file_cleanup (void *arg)
13638 {
13639 struct free_dwo_file_cleanup_data *data
13640 = (struct free_dwo_file_cleanup_data *) arg;
13641 struct objfile *objfile = data->dwarf2_per_objfile->objfile;
13642
13643 free_dwo_file (data->dwo_file, objfile);
13644
13645 xfree (data);
13646 }
13647
13648 /* Traversal function for free_dwo_files. */
13649
13650 static int
13651 free_dwo_file_from_slot (void **slot, void *info)
13652 {
13653 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13654 struct objfile *objfile = (struct objfile *) info;
13655
13656 free_dwo_file (dwo_file, objfile);
13657
13658 return 1;
13659 }
13660
13661 /* Free all resources associated with DWO_FILES. */
13662
13663 static void
13664 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13665 {
13666 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13667 }
13668 \f
13669 /* Read in various DIEs. */
13670
13671 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13672 Inherit only the children of the DW_AT_abstract_origin DIE not being
13673 already referenced by DW_AT_abstract_origin from the children of the
13674 current DIE. */
13675
13676 static void
13677 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13678 {
13679 struct die_info *child_die;
13680 sect_offset *offsetp;
13681 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13682 struct die_info *origin_die;
13683 /* Iterator of the ORIGIN_DIE children. */
13684 struct die_info *origin_child_die;
13685 struct attribute *attr;
13686 struct dwarf2_cu *origin_cu;
13687 struct pending **origin_previous_list_in_scope;
13688
13689 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13690 if (!attr)
13691 return;
13692
13693 /* Note that following die references may follow to a die in a
13694 different cu. */
13695
13696 origin_cu = cu;
13697 origin_die = follow_die_ref (die, attr, &origin_cu);
13698
13699 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13700 symbols in. */
13701 origin_previous_list_in_scope = origin_cu->list_in_scope;
13702 origin_cu->list_in_scope = cu->list_in_scope;
13703
13704 if (die->tag != origin_die->tag
13705 && !(die->tag == DW_TAG_inlined_subroutine
13706 && origin_die->tag == DW_TAG_subprogram))
13707 complaint (&symfile_complaints,
13708 _("DIE %s and its abstract origin %s have different tags"),
13709 sect_offset_str (die->sect_off),
13710 sect_offset_str (origin_die->sect_off));
13711
13712 std::vector<sect_offset> offsets;
13713
13714 for (child_die = die->child;
13715 child_die && child_die->tag;
13716 child_die = sibling_die (child_die))
13717 {
13718 struct die_info *child_origin_die;
13719 struct dwarf2_cu *child_origin_cu;
13720
13721 /* We are trying to process concrete instance entries:
13722 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13723 it's not relevant to our analysis here. i.e. detecting DIEs that are
13724 present in the abstract instance but not referenced in the concrete
13725 one. */
13726 if (child_die->tag == DW_TAG_call_site
13727 || child_die->tag == DW_TAG_GNU_call_site)
13728 continue;
13729
13730 /* For each CHILD_DIE, find the corresponding child of
13731 ORIGIN_DIE. If there is more than one layer of
13732 DW_AT_abstract_origin, follow them all; there shouldn't be,
13733 but GCC versions at least through 4.4 generate this (GCC PR
13734 40573). */
13735 child_origin_die = child_die;
13736 child_origin_cu = cu;
13737 while (1)
13738 {
13739 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13740 child_origin_cu);
13741 if (attr == NULL)
13742 break;
13743 child_origin_die = follow_die_ref (child_origin_die, attr,
13744 &child_origin_cu);
13745 }
13746
13747 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13748 counterpart may exist. */
13749 if (child_origin_die != child_die)
13750 {
13751 if (child_die->tag != child_origin_die->tag
13752 && !(child_die->tag == DW_TAG_inlined_subroutine
13753 && child_origin_die->tag == DW_TAG_subprogram))
13754 complaint (&symfile_complaints,
13755 _("Child DIE %s and its abstract origin %s have "
13756 "different tags"),
13757 sect_offset_str (child_die->sect_off),
13758 sect_offset_str (child_origin_die->sect_off));
13759 if (child_origin_die->parent != origin_die)
13760 complaint (&symfile_complaints,
13761 _("Child DIE %s and its abstract origin %s have "
13762 "different parents"),
13763 sect_offset_str (child_die->sect_off),
13764 sect_offset_str (child_origin_die->sect_off));
13765 else
13766 offsets.push_back (child_origin_die->sect_off);
13767 }
13768 }
13769 std::sort (offsets.begin (), offsets.end ());
13770 sect_offset *offsets_end = offsets.data () + offsets.size ();
13771 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13772 if (offsetp[-1] == *offsetp)
13773 complaint (&symfile_complaints,
13774 _("Multiple children of DIE %s refer "
13775 "to DIE %s as their abstract origin"),
13776 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13777
13778 offsetp = offsets.data ();
13779 origin_child_die = origin_die->child;
13780 while (origin_child_die && origin_child_die->tag)
13781 {
13782 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13783 while (offsetp < offsets_end
13784 && *offsetp < origin_child_die->sect_off)
13785 offsetp++;
13786 if (offsetp >= offsets_end
13787 || *offsetp > origin_child_die->sect_off)
13788 {
13789 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13790 Check whether we're already processing ORIGIN_CHILD_DIE.
13791 This can happen with mutually referenced abstract_origins.
13792 PR 16581. */
13793 if (!origin_child_die->in_process)
13794 process_die (origin_child_die, origin_cu);
13795 }
13796 origin_child_die = sibling_die (origin_child_die);
13797 }
13798 origin_cu->list_in_scope = origin_previous_list_in_scope;
13799 }
13800
13801 static void
13802 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13803 {
13804 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13805 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13806 struct context_stack *newobj;
13807 CORE_ADDR lowpc;
13808 CORE_ADDR highpc;
13809 struct die_info *child_die;
13810 struct attribute *attr, *call_line, *call_file;
13811 const char *name;
13812 CORE_ADDR baseaddr;
13813 struct block *block;
13814 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13815 std::vector<struct symbol *> template_args;
13816 struct template_symbol *templ_func = NULL;
13817
13818 if (inlined_func)
13819 {
13820 /* If we do not have call site information, we can't show the
13821 caller of this inlined function. That's too confusing, so
13822 only use the scope for local variables. */
13823 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13824 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13825 if (call_line == NULL || call_file == NULL)
13826 {
13827 read_lexical_block_scope (die, cu);
13828 return;
13829 }
13830 }
13831
13832 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13833
13834 name = dwarf2_name (die, cu);
13835
13836 /* Ignore functions with missing or empty names. These are actually
13837 illegal according to the DWARF standard. */
13838 if (name == NULL)
13839 {
13840 complaint (&symfile_complaints,
13841 _("missing name for subprogram DIE at %s"),
13842 sect_offset_str (die->sect_off));
13843 return;
13844 }
13845
13846 /* Ignore functions with missing or invalid low and high pc attributes. */
13847 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13848 <= PC_BOUNDS_INVALID)
13849 {
13850 attr = dwarf2_attr (die, DW_AT_external, cu);
13851 if (!attr || !DW_UNSND (attr))
13852 complaint (&symfile_complaints,
13853 _("cannot get low and high bounds "
13854 "for subprogram DIE at %s"),
13855 sect_offset_str (die->sect_off));
13856 return;
13857 }
13858
13859 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13860 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13861
13862 /* If we have any template arguments, then we must allocate a
13863 different sort of symbol. */
13864 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13865 {
13866 if (child_die->tag == DW_TAG_template_type_param
13867 || child_die->tag == DW_TAG_template_value_param)
13868 {
13869 templ_func = allocate_template_symbol (objfile);
13870 templ_func->subclass = SYMBOL_TEMPLATE;
13871 break;
13872 }
13873 }
13874
13875 newobj = push_context (0, lowpc);
13876 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13877 (struct symbol *) templ_func);
13878
13879 /* If there is a location expression for DW_AT_frame_base, record
13880 it. */
13881 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13882 if (attr)
13883 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13884
13885 /* If there is a location for the static link, record it. */
13886 newobj->static_link = NULL;
13887 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13888 if (attr)
13889 {
13890 newobj->static_link
13891 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13892 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13893 }
13894
13895 cu->list_in_scope = &local_symbols;
13896
13897 if (die->child != NULL)
13898 {
13899 child_die = die->child;
13900 while (child_die && child_die->tag)
13901 {
13902 if (child_die->tag == DW_TAG_template_type_param
13903 || child_die->tag == DW_TAG_template_value_param)
13904 {
13905 struct symbol *arg = new_symbol (child_die, NULL, cu);
13906
13907 if (arg != NULL)
13908 template_args.push_back (arg);
13909 }
13910 else
13911 process_die (child_die, cu);
13912 child_die = sibling_die (child_die);
13913 }
13914 }
13915
13916 inherit_abstract_dies (die, cu);
13917
13918 /* If we have a DW_AT_specification, we might need to import using
13919 directives from the context of the specification DIE. See the
13920 comment in determine_prefix. */
13921 if (cu->language == language_cplus
13922 && dwarf2_attr (die, DW_AT_specification, cu))
13923 {
13924 struct dwarf2_cu *spec_cu = cu;
13925 struct die_info *spec_die = die_specification (die, &spec_cu);
13926
13927 while (spec_die)
13928 {
13929 child_die = spec_die->child;
13930 while (child_die && child_die->tag)
13931 {
13932 if (child_die->tag == DW_TAG_imported_module)
13933 process_die (child_die, spec_cu);
13934 child_die = sibling_die (child_die);
13935 }
13936
13937 /* In some cases, GCC generates specification DIEs that
13938 themselves contain DW_AT_specification attributes. */
13939 spec_die = die_specification (spec_die, &spec_cu);
13940 }
13941 }
13942
13943 newobj = pop_context ();
13944 /* Make a block for the local symbols within. */
13945 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
13946 newobj->static_link, lowpc, highpc);
13947
13948 /* For C++, set the block's scope. */
13949 if ((cu->language == language_cplus
13950 || cu->language == language_fortran
13951 || cu->language == language_d
13952 || cu->language == language_rust)
13953 && cu->processing_has_namespace_info)
13954 block_set_scope (block, determine_prefix (die, cu),
13955 &objfile->objfile_obstack);
13956
13957 /* If we have address ranges, record them. */
13958 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13959
13960 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
13961
13962 /* Attach template arguments to function. */
13963 if (!template_args.empty ())
13964 {
13965 gdb_assert (templ_func != NULL);
13966
13967 templ_func->n_template_arguments = template_args.size ();
13968 templ_func->template_arguments
13969 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13970 templ_func->n_template_arguments);
13971 memcpy (templ_func->template_arguments,
13972 template_args.data (),
13973 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13974 }
13975
13976 /* In C++, we can have functions nested inside functions (e.g., when
13977 a function declares a class that has methods). This means that
13978 when we finish processing a function scope, we may need to go
13979 back to building a containing block's symbol lists. */
13980 local_symbols = newobj->locals;
13981 local_using_directives = newobj->local_using_directives;
13982
13983 /* If we've finished processing a top-level function, subsequent
13984 symbols go in the file symbol list. */
13985 if (outermost_context_p ())
13986 cu->list_in_scope = &file_symbols;
13987 }
13988
13989 /* Process all the DIES contained within a lexical block scope. Start
13990 a new scope, process the dies, and then close the scope. */
13991
13992 static void
13993 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13994 {
13995 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13996 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13997 struct context_stack *newobj;
13998 CORE_ADDR lowpc, highpc;
13999 struct die_info *child_die;
14000 CORE_ADDR baseaddr;
14001
14002 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14003
14004 /* Ignore blocks with missing or invalid low and high pc attributes. */
14005 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
14006 as multiple lexical blocks? Handling children in a sane way would
14007 be nasty. Might be easier to properly extend generic blocks to
14008 describe ranges. */
14009 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
14010 {
14011 case PC_BOUNDS_NOT_PRESENT:
14012 /* DW_TAG_lexical_block has no attributes, process its children as if
14013 there was no wrapping by that DW_TAG_lexical_block.
14014 GCC does no longer produces such DWARF since GCC r224161. */
14015 for (child_die = die->child;
14016 child_die != NULL && child_die->tag;
14017 child_die = sibling_die (child_die))
14018 process_die (child_die, cu);
14019 return;
14020 case PC_BOUNDS_INVALID:
14021 return;
14022 }
14023 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14024 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
14025
14026 push_context (0, lowpc);
14027 if (die->child != NULL)
14028 {
14029 child_die = die->child;
14030 while (child_die && child_die->tag)
14031 {
14032 process_die (child_die, cu);
14033 child_die = sibling_die (child_die);
14034 }
14035 }
14036 inherit_abstract_dies (die, cu);
14037 newobj = pop_context ();
14038
14039 if (local_symbols != NULL || local_using_directives != NULL)
14040 {
14041 struct block *block
14042 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
14043 newobj->start_addr, highpc);
14044
14045 /* Note that recording ranges after traversing children, as we
14046 do here, means that recording a parent's ranges entails
14047 walking across all its children's ranges as they appear in
14048 the address map, which is quadratic behavior.
14049
14050 It would be nicer to record the parent's ranges before
14051 traversing its children, simply overriding whatever you find
14052 there. But since we don't even decide whether to create a
14053 block until after we've traversed its children, that's hard
14054 to do. */
14055 dwarf2_record_block_ranges (die, block, baseaddr, cu);
14056 }
14057 local_symbols = newobj->locals;
14058 local_using_directives = newobj->local_using_directives;
14059 }
14060
14061 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
14062
14063 static void
14064 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
14065 {
14066 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14067 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14068 CORE_ADDR pc, baseaddr;
14069 struct attribute *attr;
14070 struct call_site *call_site, call_site_local;
14071 void **slot;
14072 int nparams;
14073 struct die_info *child_die;
14074
14075 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14076
14077 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
14078 if (attr == NULL)
14079 {
14080 /* This was a pre-DWARF-5 GNU extension alias
14081 for DW_AT_call_return_pc. */
14082 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14083 }
14084 if (!attr)
14085 {
14086 complaint (&symfile_complaints,
14087 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
14088 "DIE %s [in module %s]"),
14089 sect_offset_str (die->sect_off), objfile_name (objfile));
14090 return;
14091 }
14092 pc = attr_value_as_address (attr) + baseaddr;
14093 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
14094
14095 if (cu->call_site_htab == NULL)
14096 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14097 NULL, &objfile->objfile_obstack,
14098 hashtab_obstack_allocate, NULL);
14099 call_site_local.pc = pc;
14100 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14101 if (*slot != NULL)
14102 {
14103 complaint (&symfile_complaints,
14104 _("Duplicate PC %s for DW_TAG_call_site "
14105 "DIE %s [in module %s]"),
14106 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
14107 objfile_name (objfile));
14108 return;
14109 }
14110
14111 /* Count parameters at the caller. */
14112
14113 nparams = 0;
14114 for (child_die = die->child; child_die && child_die->tag;
14115 child_die = sibling_die (child_die))
14116 {
14117 if (child_die->tag != DW_TAG_call_site_parameter
14118 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14119 {
14120 complaint (&symfile_complaints,
14121 _("Tag %d is not DW_TAG_call_site_parameter in "
14122 "DW_TAG_call_site child DIE %s [in module %s]"),
14123 child_die->tag, sect_offset_str (child_die->sect_off),
14124 objfile_name (objfile));
14125 continue;
14126 }
14127
14128 nparams++;
14129 }
14130
14131 call_site
14132 = ((struct call_site *)
14133 obstack_alloc (&objfile->objfile_obstack,
14134 sizeof (*call_site)
14135 + (sizeof (*call_site->parameter) * (nparams - 1))));
14136 *slot = call_site;
14137 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14138 call_site->pc = pc;
14139
14140 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14141 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14142 {
14143 struct die_info *func_die;
14144
14145 /* Skip also over DW_TAG_inlined_subroutine. */
14146 for (func_die = die->parent;
14147 func_die && func_die->tag != DW_TAG_subprogram
14148 && func_die->tag != DW_TAG_subroutine_type;
14149 func_die = func_die->parent);
14150
14151 /* DW_AT_call_all_calls is a superset
14152 of DW_AT_call_all_tail_calls. */
14153 if (func_die
14154 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14155 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14156 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14157 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14158 {
14159 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14160 not complete. But keep CALL_SITE for look ups via call_site_htab,
14161 both the initial caller containing the real return address PC and
14162 the final callee containing the current PC of a chain of tail
14163 calls do not need to have the tail call list complete. But any
14164 function candidate for a virtual tail call frame searched via
14165 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14166 determined unambiguously. */
14167 }
14168 else
14169 {
14170 struct type *func_type = NULL;
14171
14172 if (func_die)
14173 func_type = get_die_type (func_die, cu);
14174 if (func_type != NULL)
14175 {
14176 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14177
14178 /* Enlist this call site to the function. */
14179 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14180 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14181 }
14182 else
14183 complaint (&symfile_complaints,
14184 _("Cannot find function owning DW_TAG_call_site "
14185 "DIE %s [in module %s]"),
14186 sect_offset_str (die->sect_off), objfile_name (objfile));
14187 }
14188 }
14189
14190 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14191 if (attr == NULL)
14192 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14193 if (attr == NULL)
14194 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14195 if (attr == NULL)
14196 {
14197 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14198 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14199 }
14200 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14201 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14202 /* Keep NULL DWARF_BLOCK. */;
14203 else if (attr_form_is_block (attr))
14204 {
14205 struct dwarf2_locexpr_baton *dlbaton;
14206
14207 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14208 dlbaton->data = DW_BLOCK (attr)->data;
14209 dlbaton->size = DW_BLOCK (attr)->size;
14210 dlbaton->per_cu = cu->per_cu;
14211
14212 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14213 }
14214 else if (attr_form_is_ref (attr))
14215 {
14216 struct dwarf2_cu *target_cu = cu;
14217 struct die_info *target_die;
14218
14219 target_die = follow_die_ref (die, attr, &target_cu);
14220 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14221 if (die_is_declaration (target_die, target_cu))
14222 {
14223 const char *target_physname;
14224
14225 /* Prefer the mangled name; otherwise compute the demangled one. */
14226 target_physname = dw2_linkage_name (target_die, target_cu);
14227 if (target_physname == NULL)
14228 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14229 if (target_physname == NULL)
14230 complaint (&symfile_complaints,
14231 _("DW_AT_call_target target DIE has invalid "
14232 "physname, for referencing DIE %s [in module %s]"),
14233 sect_offset_str (die->sect_off), objfile_name (objfile));
14234 else
14235 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14236 }
14237 else
14238 {
14239 CORE_ADDR lowpc;
14240
14241 /* DW_AT_entry_pc should be preferred. */
14242 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14243 <= PC_BOUNDS_INVALID)
14244 complaint (&symfile_complaints,
14245 _("DW_AT_call_target target DIE has invalid "
14246 "low pc, for referencing DIE %s [in module %s]"),
14247 sect_offset_str (die->sect_off), objfile_name (objfile));
14248 else
14249 {
14250 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14251 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14252 }
14253 }
14254 }
14255 else
14256 complaint (&symfile_complaints,
14257 _("DW_TAG_call_site DW_AT_call_target is neither "
14258 "block nor reference, for DIE %s [in module %s]"),
14259 sect_offset_str (die->sect_off), objfile_name (objfile));
14260
14261 call_site->per_cu = cu->per_cu;
14262
14263 for (child_die = die->child;
14264 child_die && child_die->tag;
14265 child_die = sibling_die (child_die))
14266 {
14267 struct call_site_parameter *parameter;
14268 struct attribute *loc, *origin;
14269
14270 if (child_die->tag != DW_TAG_call_site_parameter
14271 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14272 {
14273 /* Already printed the complaint above. */
14274 continue;
14275 }
14276
14277 gdb_assert (call_site->parameter_count < nparams);
14278 parameter = &call_site->parameter[call_site->parameter_count];
14279
14280 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14281 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14282 register is contained in DW_AT_call_value. */
14283
14284 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14285 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14286 if (origin == NULL)
14287 {
14288 /* This was a pre-DWARF-5 GNU extension alias
14289 for DW_AT_call_parameter. */
14290 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14291 }
14292 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14293 {
14294 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14295
14296 sect_offset sect_off
14297 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14298 if (!offset_in_cu_p (&cu->header, sect_off))
14299 {
14300 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14301 binding can be done only inside one CU. Such referenced DIE
14302 therefore cannot be even moved to DW_TAG_partial_unit. */
14303 complaint (&symfile_complaints,
14304 _("DW_AT_call_parameter offset is not in CU for "
14305 "DW_TAG_call_site child DIE %s [in module %s]"),
14306 sect_offset_str (child_die->sect_off),
14307 objfile_name (objfile));
14308 continue;
14309 }
14310 parameter->u.param_cu_off
14311 = (cu_offset) (sect_off - cu->header.sect_off);
14312 }
14313 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14314 {
14315 complaint (&symfile_complaints,
14316 _("No DW_FORM_block* DW_AT_location for "
14317 "DW_TAG_call_site child DIE %s [in module %s]"),
14318 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14319 continue;
14320 }
14321 else
14322 {
14323 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14324 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14325 if (parameter->u.dwarf_reg != -1)
14326 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14327 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14328 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14329 &parameter->u.fb_offset))
14330 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14331 else
14332 {
14333 complaint (&symfile_complaints,
14334 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14335 "for DW_FORM_block* DW_AT_location is supported for "
14336 "DW_TAG_call_site child DIE %s "
14337 "[in module %s]"),
14338 sect_offset_str (child_die->sect_off),
14339 objfile_name (objfile));
14340 continue;
14341 }
14342 }
14343
14344 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14345 if (attr == NULL)
14346 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14347 if (!attr_form_is_block (attr))
14348 {
14349 complaint (&symfile_complaints,
14350 _("No DW_FORM_block* DW_AT_call_value for "
14351 "DW_TAG_call_site child DIE %s [in module %s]"),
14352 sect_offset_str (child_die->sect_off),
14353 objfile_name (objfile));
14354 continue;
14355 }
14356 parameter->value = DW_BLOCK (attr)->data;
14357 parameter->value_size = DW_BLOCK (attr)->size;
14358
14359 /* Parameters are not pre-cleared by memset above. */
14360 parameter->data_value = NULL;
14361 parameter->data_value_size = 0;
14362 call_site->parameter_count++;
14363
14364 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14365 if (attr == NULL)
14366 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14367 if (attr)
14368 {
14369 if (!attr_form_is_block (attr))
14370 complaint (&symfile_complaints,
14371 _("No DW_FORM_block* DW_AT_call_data_value for "
14372 "DW_TAG_call_site child DIE %s [in module %s]"),
14373 sect_offset_str (child_die->sect_off),
14374 objfile_name (objfile));
14375 else
14376 {
14377 parameter->data_value = DW_BLOCK (attr)->data;
14378 parameter->data_value_size = DW_BLOCK (attr)->size;
14379 }
14380 }
14381 }
14382 }
14383
14384 /* Helper function for read_variable. If DIE represents a virtual
14385 table, then return the type of the concrete object that is
14386 associated with the virtual table. Otherwise, return NULL. */
14387
14388 static struct type *
14389 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14390 {
14391 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14392 if (attr == NULL)
14393 return NULL;
14394
14395 /* Find the type DIE. */
14396 struct die_info *type_die = NULL;
14397 struct dwarf2_cu *type_cu = cu;
14398
14399 if (attr_form_is_ref (attr))
14400 type_die = follow_die_ref (die, attr, &type_cu);
14401 if (type_die == NULL)
14402 return NULL;
14403
14404 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14405 return NULL;
14406 return die_containing_type (type_die, type_cu);
14407 }
14408
14409 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14410
14411 static void
14412 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14413 {
14414 struct rust_vtable_symbol *storage = NULL;
14415
14416 if (cu->language == language_rust)
14417 {
14418 struct type *containing_type = rust_containing_type (die, cu);
14419
14420 if (containing_type != NULL)
14421 {
14422 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14423
14424 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14425 struct rust_vtable_symbol);
14426 initialize_objfile_symbol (storage);
14427 storage->concrete_type = containing_type;
14428 storage->subclass = SYMBOL_RUST_VTABLE;
14429 }
14430 }
14431
14432 new_symbol (die, NULL, cu, storage);
14433 }
14434
14435 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14436 reading .debug_rnglists.
14437 Callback's type should be:
14438 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14439 Return true if the attributes are present and valid, otherwise,
14440 return false. */
14441
14442 template <typename Callback>
14443 static bool
14444 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14445 Callback &&callback)
14446 {
14447 struct dwarf2_per_objfile *dwarf2_per_objfile
14448 = cu->per_cu->dwarf2_per_objfile;
14449 struct objfile *objfile = dwarf2_per_objfile->objfile;
14450 bfd *obfd = objfile->obfd;
14451 /* Base address selection entry. */
14452 CORE_ADDR base;
14453 int found_base;
14454 const gdb_byte *buffer;
14455 CORE_ADDR baseaddr;
14456 bool overflow = false;
14457
14458 found_base = cu->base_known;
14459 base = cu->base_address;
14460
14461 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14462 if (offset >= dwarf2_per_objfile->rnglists.size)
14463 {
14464 complaint (&symfile_complaints,
14465 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14466 offset);
14467 return false;
14468 }
14469 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14470
14471 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14472
14473 while (1)
14474 {
14475 /* Initialize it due to a false compiler warning. */
14476 CORE_ADDR range_beginning = 0, range_end = 0;
14477 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14478 + dwarf2_per_objfile->rnglists.size);
14479 unsigned int bytes_read;
14480
14481 if (buffer == buf_end)
14482 {
14483 overflow = true;
14484 break;
14485 }
14486 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14487 switch (rlet)
14488 {
14489 case DW_RLE_end_of_list:
14490 break;
14491 case DW_RLE_base_address:
14492 if (buffer + cu->header.addr_size > buf_end)
14493 {
14494 overflow = true;
14495 break;
14496 }
14497 base = read_address (obfd, buffer, cu, &bytes_read);
14498 found_base = 1;
14499 buffer += bytes_read;
14500 break;
14501 case DW_RLE_start_length:
14502 if (buffer + cu->header.addr_size > buf_end)
14503 {
14504 overflow = true;
14505 break;
14506 }
14507 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14508 buffer += bytes_read;
14509 range_end = (range_beginning
14510 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14511 buffer += bytes_read;
14512 if (buffer > buf_end)
14513 {
14514 overflow = true;
14515 break;
14516 }
14517 break;
14518 case DW_RLE_offset_pair:
14519 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14520 buffer += bytes_read;
14521 if (buffer > buf_end)
14522 {
14523 overflow = true;
14524 break;
14525 }
14526 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14527 buffer += bytes_read;
14528 if (buffer > buf_end)
14529 {
14530 overflow = true;
14531 break;
14532 }
14533 break;
14534 case DW_RLE_start_end:
14535 if (buffer + 2 * cu->header.addr_size > buf_end)
14536 {
14537 overflow = true;
14538 break;
14539 }
14540 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14541 buffer += bytes_read;
14542 range_end = read_address (obfd, buffer, cu, &bytes_read);
14543 buffer += bytes_read;
14544 break;
14545 default:
14546 complaint (&symfile_complaints,
14547 _("Invalid .debug_rnglists data (no base address)"));
14548 return false;
14549 }
14550 if (rlet == DW_RLE_end_of_list || overflow)
14551 break;
14552 if (rlet == DW_RLE_base_address)
14553 continue;
14554
14555 if (!found_base)
14556 {
14557 /* We have no valid base address for the ranges
14558 data. */
14559 complaint (&symfile_complaints,
14560 _("Invalid .debug_rnglists data (no base address)"));
14561 return false;
14562 }
14563
14564 if (range_beginning > range_end)
14565 {
14566 /* Inverted range entries are invalid. */
14567 complaint (&symfile_complaints,
14568 _("Invalid .debug_rnglists data (inverted range)"));
14569 return false;
14570 }
14571
14572 /* Empty range entries have no effect. */
14573 if (range_beginning == range_end)
14574 continue;
14575
14576 range_beginning += base;
14577 range_end += base;
14578
14579 /* A not-uncommon case of bad debug info.
14580 Don't pollute the addrmap with bad data. */
14581 if (range_beginning + baseaddr == 0
14582 && !dwarf2_per_objfile->has_section_at_zero)
14583 {
14584 complaint (&symfile_complaints,
14585 _(".debug_rnglists entry has start address of zero"
14586 " [in module %s]"), objfile_name (objfile));
14587 continue;
14588 }
14589
14590 callback (range_beginning, range_end);
14591 }
14592
14593 if (overflow)
14594 {
14595 complaint (&symfile_complaints,
14596 _("Offset %d is not terminated "
14597 "for DW_AT_ranges attribute"),
14598 offset);
14599 return false;
14600 }
14601
14602 return true;
14603 }
14604
14605 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14606 Callback's type should be:
14607 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14608 Return 1 if the attributes are present and valid, otherwise, return 0. */
14609
14610 template <typename Callback>
14611 static int
14612 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14613 Callback &&callback)
14614 {
14615 struct dwarf2_per_objfile *dwarf2_per_objfile
14616 = cu->per_cu->dwarf2_per_objfile;
14617 struct objfile *objfile = dwarf2_per_objfile->objfile;
14618 struct comp_unit_head *cu_header = &cu->header;
14619 bfd *obfd = objfile->obfd;
14620 unsigned int addr_size = cu_header->addr_size;
14621 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14622 /* Base address selection entry. */
14623 CORE_ADDR base;
14624 int found_base;
14625 unsigned int dummy;
14626 const gdb_byte *buffer;
14627 CORE_ADDR baseaddr;
14628
14629 if (cu_header->version >= 5)
14630 return dwarf2_rnglists_process (offset, cu, callback);
14631
14632 found_base = cu->base_known;
14633 base = cu->base_address;
14634
14635 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14636 if (offset >= dwarf2_per_objfile->ranges.size)
14637 {
14638 complaint (&symfile_complaints,
14639 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14640 offset);
14641 return 0;
14642 }
14643 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14644
14645 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14646
14647 while (1)
14648 {
14649 CORE_ADDR range_beginning, range_end;
14650
14651 range_beginning = read_address (obfd, buffer, cu, &dummy);
14652 buffer += addr_size;
14653 range_end = read_address (obfd, buffer, cu, &dummy);
14654 buffer += addr_size;
14655 offset += 2 * addr_size;
14656
14657 /* An end of list marker is a pair of zero addresses. */
14658 if (range_beginning == 0 && range_end == 0)
14659 /* Found the end of list entry. */
14660 break;
14661
14662 /* Each base address selection entry is a pair of 2 values.
14663 The first is the largest possible address, the second is
14664 the base address. Check for a base address here. */
14665 if ((range_beginning & mask) == mask)
14666 {
14667 /* If we found the largest possible address, then we already
14668 have the base address in range_end. */
14669 base = range_end;
14670 found_base = 1;
14671 continue;
14672 }
14673
14674 if (!found_base)
14675 {
14676 /* We have no valid base address for the ranges
14677 data. */
14678 complaint (&symfile_complaints,
14679 _("Invalid .debug_ranges data (no base address)"));
14680 return 0;
14681 }
14682
14683 if (range_beginning > range_end)
14684 {
14685 /* Inverted range entries are invalid. */
14686 complaint (&symfile_complaints,
14687 _("Invalid .debug_ranges data (inverted range)"));
14688 return 0;
14689 }
14690
14691 /* Empty range entries have no effect. */
14692 if (range_beginning == range_end)
14693 continue;
14694
14695 range_beginning += base;
14696 range_end += base;
14697
14698 /* A not-uncommon case of bad debug info.
14699 Don't pollute the addrmap with bad data. */
14700 if (range_beginning + baseaddr == 0
14701 && !dwarf2_per_objfile->has_section_at_zero)
14702 {
14703 complaint (&symfile_complaints,
14704 _(".debug_ranges entry has start address of zero"
14705 " [in module %s]"), objfile_name (objfile));
14706 continue;
14707 }
14708
14709 callback (range_beginning, range_end);
14710 }
14711
14712 return 1;
14713 }
14714
14715 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14716 Return 1 if the attributes are present and valid, otherwise, return 0.
14717 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14718
14719 static int
14720 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14721 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14722 struct partial_symtab *ranges_pst)
14723 {
14724 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14725 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14726 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14727 SECT_OFF_TEXT (objfile));
14728 int low_set = 0;
14729 CORE_ADDR low = 0;
14730 CORE_ADDR high = 0;
14731 int retval;
14732
14733 retval = dwarf2_ranges_process (offset, cu,
14734 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14735 {
14736 if (ranges_pst != NULL)
14737 {
14738 CORE_ADDR lowpc;
14739 CORE_ADDR highpc;
14740
14741 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14742 range_beginning + baseaddr);
14743 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14744 range_end + baseaddr);
14745 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14746 ranges_pst);
14747 }
14748
14749 /* FIXME: This is recording everything as a low-high
14750 segment of consecutive addresses. We should have a
14751 data structure for discontiguous block ranges
14752 instead. */
14753 if (! low_set)
14754 {
14755 low = range_beginning;
14756 high = range_end;
14757 low_set = 1;
14758 }
14759 else
14760 {
14761 if (range_beginning < low)
14762 low = range_beginning;
14763 if (range_end > high)
14764 high = range_end;
14765 }
14766 });
14767 if (!retval)
14768 return 0;
14769
14770 if (! low_set)
14771 /* If the first entry is an end-of-list marker, the range
14772 describes an empty scope, i.e. no instructions. */
14773 return 0;
14774
14775 if (low_return)
14776 *low_return = low;
14777 if (high_return)
14778 *high_return = high;
14779 return 1;
14780 }
14781
14782 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14783 definition for the return value. *LOWPC and *HIGHPC are set iff
14784 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14785
14786 static enum pc_bounds_kind
14787 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14788 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14789 struct partial_symtab *pst)
14790 {
14791 struct dwarf2_per_objfile *dwarf2_per_objfile
14792 = cu->per_cu->dwarf2_per_objfile;
14793 struct attribute *attr;
14794 struct attribute *attr_high;
14795 CORE_ADDR low = 0;
14796 CORE_ADDR high = 0;
14797 enum pc_bounds_kind ret;
14798
14799 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14800 if (attr_high)
14801 {
14802 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14803 if (attr)
14804 {
14805 low = attr_value_as_address (attr);
14806 high = attr_value_as_address (attr_high);
14807 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14808 high += low;
14809 }
14810 else
14811 /* Found high w/o low attribute. */
14812 return PC_BOUNDS_INVALID;
14813
14814 /* Found consecutive range of addresses. */
14815 ret = PC_BOUNDS_HIGH_LOW;
14816 }
14817 else
14818 {
14819 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14820 if (attr != NULL)
14821 {
14822 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14823 We take advantage of the fact that DW_AT_ranges does not appear
14824 in DW_TAG_compile_unit of DWO files. */
14825 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14826 unsigned int ranges_offset = (DW_UNSND (attr)
14827 + (need_ranges_base
14828 ? cu->ranges_base
14829 : 0));
14830
14831 /* Value of the DW_AT_ranges attribute is the offset in the
14832 .debug_ranges section. */
14833 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14834 return PC_BOUNDS_INVALID;
14835 /* Found discontinuous range of addresses. */
14836 ret = PC_BOUNDS_RANGES;
14837 }
14838 else
14839 return PC_BOUNDS_NOT_PRESENT;
14840 }
14841
14842 /* read_partial_die has also the strict LOW < HIGH requirement. */
14843 if (high <= low)
14844 return PC_BOUNDS_INVALID;
14845
14846 /* When using the GNU linker, .gnu.linkonce. sections are used to
14847 eliminate duplicate copies of functions and vtables and such.
14848 The linker will arbitrarily choose one and discard the others.
14849 The AT_*_pc values for such functions refer to local labels in
14850 these sections. If the section from that file was discarded, the
14851 labels are not in the output, so the relocs get a value of 0.
14852 If this is a discarded function, mark the pc bounds as invalid,
14853 so that GDB will ignore it. */
14854 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14855 return PC_BOUNDS_INVALID;
14856
14857 *lowpc = low;
14858 if (highpc)
14859 *highpc = high;
14860 return ret;
14861 }
14862
14863 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14864 its low and high PC addresses. Do nothing if these addresses could not
14865 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14866 and HIGHPC to the high address if greater than HIGHPC. */
14867
14868 static void
14869 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14870 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14871 struct dwarf2_cu *cu)
14872 {
14873 CORE_ADDR low, high;
14874 struct die_info *child = die->child;
14875
14876 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14877 {
14878 *lowpc = std::min (*lowpc, low);
14879 *highpc = std::max (*highpc, high);
14880 }
14881
14882 /* If the language does not allow nested subprograms (either inside
14883 subprograms or lexical blocks), we're done. */
14884 if (cu->language != language_ada)
14885 return;
14886
14887 /* Check all the children of the given DIE. If it contains nested
14888 subprograms, then check their pc bounds. Likewise, we need to
14889 check lexical blocks as well, as they may also contain subprogram
14890 definitions. */
14891 while (child && child->tag)
14892 {
14893 if (child->tag == DW_TAG_subprogram
14894 || child->tag == DW_TAG_lexical_block)
14895 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14896 child = sibling_die (child);
14897 }
14898 }
14899
14900 /* Get the low and high pc's represented by the scope DIE, and store
14901 them in *LOWPC and *HIGHPC. If the correct values can't be
14902 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14903
14904 static void
14905 get_scope_pc_bounds (struct die_info *die,
14906 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14907 struct dwarf2_cu *cu)
14908 {
14909 CORE_ADDR best_low = (CORE_ADDR) -1;
14910 CORE_ADDR best_high = (CORE_ADDR) 0;
14911 CORE_ADDR current_low, current_high;
14912
14913 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14914 >= PC_BOUNDS_RANGES)
14915 {
14916 best_low = current_low;
14917 best_high = current_high;
14918 }
14919 else
14920 {
14921 struct die_info *child = die->child;
14922
14923 while (child && child->tag)
14924 {
14925 switch (child->tag) {
14926 case DW_TAG_subprogram:
14927 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14928 break;
14929 case DW_TAG_namespace:
14930 case DW_TAG_module:
14931 /* FIXME: carlton/2004-01-16: Should we do this for
14932 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14933 that current GCC's always emit the DIEs corresponding
14934 to definitions of methods of classes as children of a
14935 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14936 the DIEs giving the declarations, which could be
14937 anywhere). But I don't see any reason why the
14938 standards says that they have to be there. */
14939 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14940
14941 if (current_low != ((CORE_ADDR) -1))
14942 {
14943 best_low = std::min (best_low, current_low);
14944 best_high = std::max (best_high, current_high);
14945 }
14946 break;
14947 default:
14948 /* Ignore. */
14949 break;
14950 }
14951
14952 child = sibling_die (child);
14953 }
14954 }
14955
14956 *lowpc = best_low;
14957 *highpc = best_high;
14958 }
14959
14960 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14961 in DIE. */
14962
14963 static void
14964 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14965 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14966 {
14967 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14968 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14969 struct attribute *attr;
14970 struct attribute *attr_high;
14971
14972 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14973 if (attr_high)
14974 {
14975 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14976 if (attr)
14977 {
14978 CORE_ADDR low = attr_value_as_address (attr);
14979 CORE_ADDR high = attr_value_as_address (attr_high);
14980
14981 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14982 high += low;
14983
14984 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14985 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14986 record_block_range (block, low, high - 1);
14987 }
14988 }
14989
14990 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14991 if (attr)
14992 {
14993 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14994 We take advantage of the fact that DW_AT_ranges does not appear
14995 in DW_TAG_compile_unit of DWO files. */
14996 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14997
14998 /* The value of the DW_AT_ranges attribute is the offset of the
14999 address range list in the .debug_ranges section. */
15000 unsigned long offset = (DW_UNSND (attr)
15001 + (need_ranges_base ? cu->ranges_base : 0));
15002 const gdb_byte *buffer;
15003
15004 /* For some target architectures, but not others, the
15005 read_address function sign-extends the addresses it returns.
15006 To recognize base address selection entries, we need a
15007 mask. */
15008 unsigned int addr_size = cu->header.addr_size;
15009 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
15010
15011 /* The base address, to which the next pair is relative. Note
15012 that this 'base' is a DWARF concept: most entries in a range
15013 list are relative, to reduce the number of relocs against the
15014 debugging information. This is separate from this function's
15015 'baseaddr' argument, which GDB uses to relocate debugging
15016 information from a shared library based on the address at
15017 which the library was loaded. */
15018 CORE_ADDR base = cu->base_address;
15019 int base_known = cu->base_known;
15020
15021 dwarf2_ranges_process (offset, cu,
15022 [&] (CORE_ADDR start, CORE_ADDR end)
15023 {
15024 start += baseaddr;
15025 end += baseaddr;
15026 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
15027 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
15028 record_block_range (block, start, end - 1);
15029 });
15030 }
15031 }
15032
15033 /* Check whether the producer field indicates either of GCC < 4.6, or the
15034 Intel C/C++ compiler, and cache the result in CU. */
15035
15036 static void
15037 check_producer (struct dwarf2_cu *cu)
15038 {
15039 int major, minor;
15040
15041 if (cu->producer == NULL)
15042 {
15043 /* For unknown compilers expect their behavior is DWARF version
15044 compliant.
15045
15046 GCC started to support .debug_types sections by -gdwarf-4 since
15047 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
15048 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
15049 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
15050 interpreted incorrectly by GDB now - GCC PR debug/48229. */
15051 }
15052 else if (producer_is_gcc (cu->producer, &major, &minor))
15053 {
15054 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
15055 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
15056 }
15057 else if (producer_is_icc (cu->producer, &major, &minor))
15058 cu->producer_is_icc_lt_14 = major < 14;
15059 else
15060 {
15061 /* For other non-GCC compilers, expect their behavior is DWARF version
15062 compliant. */
15063 }
15064
15065 cu->checked_producer = 1;
15066 }
15067
15068 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
15069 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
15070 during 4.6.0 experimental. */
15071
15072 static int
15073 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
15074 {
15075 if (!cu->checked_producer)
15076 check_producer (cu);
15077
15078 return cu->producer_is_gxx_lt_4_6;
15079 }
15080
15081 /* Return the default accessibility type if it is not overriden by
15082 DW_AT_accessibility. */
15083
15084 static enum dwarf_access_attribute
15085 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15086 {
15087 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15088 {
15089 /* The default DWARF 2 accessibility for members is public, the default
15090 accessibility for inheritance is private. */
15091
15092 if (die->tag != DW_TAG_inheritance)
15093 return DW_ACCESS_public;
15094 else
15095 return DW_ACCESS_private;
15096 }
15097 else
15098 {
15099 /* DWARF 3+ defines the default accessibility a different way. The same
15100 rules apply now for DW_TAG_inheritance as for the members and it only
15101 depends on the container kind. */
15102
15103 if (die->parent->tag == DW_TAG_class_type)
15104 return DW_ACCESS_private;
15105 else
15106 return DW_ACCESS_public;
15107 }
15108 }
15109
15110 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15111 offset. If the attribute was not found return 0, otherwise return
15112 1. If it was found but could not properly be handled, set *OFFSET
15113 to 0. */
15114
15115 static int
15116 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15117 LONGEST *offset)
15118 {
15119 struct attribute *attr;
15120
15121 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15122 if (attr != NULL)
15123 {
15124 *offset = 0;
15125
15126 /* Note that we do not check for a section offset first here.
15127 This is because DW_AT_data_member_location is new in DWARF 4,
15128 so if we see it, we can assume that a constant form is really
15129 a constant and not a section offset. */
15130 if (attr_form_is_constant (attr))
15131 *offset = dwarf2_get_attr_constant_value (attr, 0);
15132 else if (attr_form_is_section_offset (attr))
15133 dwarf2_complex_location_expr_complaint ();
15134 else if (attr_form_is_block (attr))
15135 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15136 else
15137 dwarf2_complex_location_expr_complaint ();
15138
15139 return 1;
15140 }
15141
15142 return 0;
15143 }
15144
15145 /* Add an aggregate field to the field list. */
15146
15147 static void
15148 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15149 struct dwarf2_cu *cu)
15150 {
15151 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15152 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15153 struct nextfield *new_field;
15154 struct attribute *attr;
15155 struct field *fp;
15156 const char *fieldname = "";
15157
15158 /* Allocate a new field list entry and link it in. */
15159 new_field = XNEW (struct nextfield);
15160 make_cleanup (xfree, new_field);
15161 memset (new_field, 0, sizeof (struct nextfield));
15162
15163 if (die->tag == DW_TAG_inheritance)
15164 {
15165 new_field->next = fip->baseclasses;
15166 fip->baseclasses = new_field;
15167 }
15168 else
15169 {
15170 new_field->next = fip->fields;
15171 fip->fields = new_field;
15172 }
15173 fip->nfields++;
15174
15175 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15176 if (attr)
15177 new_field->accessibility = DW_UNSND (attr);
15178 else
15179 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15180 if (new_field->accessibility != DW_ACCESS_public)
15181 fip->non_public_fields = 1;
15182
15183 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15184 if (attr)
15185 new_field->virtuality = DW_UNSND (attr);
15186 else
15187 new_field->virtuality = DW_VIRTUALITY_none;
15188
15189 fp = &new_field->field;
15190
15191 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15192 {
15193 LONGEST offset;
15194
15195 /* Data member other than a C++ static data member. */
15196
15197 /* Get type of field. */
15198 fp->type = die_type (die, cu);
15199
15200 SET_FIELD_BITPOS (*fp, 0);
15201
15202 /* Get bit size of field (zero if none). */
15203 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15204 if (attr)
15205 {
15206 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15207 }
15208 else
15209 {
15210 FIELD_BITSIZE (*fp) = 0;
15211 }
15212
15213 /* Get bit offset of field. */
15214 if (handle_data_member_location (die, cu, &offset))
15215 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15216 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15217 if (attr)
15218 {
15219 if (gdbarch_bits_big_endian (gdbarch))
15220 {
15221 /* For big endian bits, the DW_AT_bit_offset gives the
15222 additional bit offset from the MSB of the containing
15223 anonymous object to the MSB of the field. We don't
15224 have to do anything special since we don't need to
15225 know the size of the anonymous object. */
15226 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15227 }
15228 else
15229 {
15230 /* For little endian bits, compute the bit offset to the
15231 MSB of the anonymous object, subtract off the number of
15232 bits from the MSB of the field to the MSB of the
15233 object, and then subtract off the number of bits of
15234 the field itself. The result is the bit offset of
15235 the LSB of the field. */
15236 int anonymous_size;
15237 int bit_offset = DW_UNSND (attr);
15238
15239 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15240 if (attr)
15241 {
15242 /* The size of the anonymous object containing
15243 the bit field is explicit, so use the
15244 indicated size (in bytes). */
15245 anonymous_size = DW_UNSND (attr);
15246 }
15247 else
15248 {
15249 /* The size of the anonymous object containing
15250 the bit field must be inferred from the type
15251 attribute of the data member containing the
15252 bit field. */
15253 anonymous_size = TYPE_LENGTH (fp->type);
15254 }
15255 SET_FIELD_BITPOS (*fp,
15256 (FIELD_BITPOS (*fp)
15257 + anonymous_size * bits_per_byte
15258 - bit_offset - FIELD_BITSIZE (*fp)));
15259 }
15260 }
15261 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15262 if (attr != NULL)
15263 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15264 + dwarf2_get_attr_constant_value (attr, 0)));
15265
15266 /* Get name of field. */
15267 fieldname = dwarf2_name (die, cu);
15268 if (fieldname == NULL)
15269 fieldname = "";
15270
15271 /* The name is already allocated along with this objfile, so we don't
15272 need to duplicate it for the type. */
15273 fp->name = fieldname;
15274
15275 /* Change accessibility for artificial fields (e.g. virtual table
15276 pointer or virtual base class pointer) to private. */
15277 if (dwarf2_attr (die, DW_AT_artificial, cu))
15278 {
15279 FIELD_ARTIFICIAL (*fp) = 1;
15280 new_field->accessibility = DW_ACCESS_private;
15281 fip->non_public_fields = 1;
15282 }
15283 }
15284 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15285 {
15286 /* C++ static member. */
15287
15288 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15289 is a declaration, but all versions of G++ as of this writing
15290 (so through at least 3.2.1) incorrectly generate
15291 DW_TAG_variable tags. */
15292
15293 const char *physname;
15294
15295 /* Get name of field. */
15296 fieldname = dwarf2_name (die, cu);
15297 if (fieldname == NULL)
15298 return;
15299
15300 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15301 if (attr
15302 /* Only create a symbol if this is an external value.
15303 new_symbol checks this and puts the value in the global symbol
15304 table, which we want. If it is not external, new_symbol
15305 will try to put the value in cu->list_in_scope which is wrong. */
15306 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15307 {
15308 /* A static const member, not much different than an enum as far as
15309 we're concerned, except that we can support more types. */
15310 new_symbol (die, NULL, cu);
15311 }
15312
15313 /* Get physical name. */
15314 physname = dwarf2_physname (fieldname, die, cu);
15315
15316 /* The name is already allocated along with this objfile, so we don't
15317 need to duplicate it for the type. */
15318 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15319 FIELD_TYPE (*fp) = die_type (die, cu);
15320 FIELD_NAME (*fp) = fieldname;
15321 }
15322 else if (die->tag == DW_TAG_inheritance)
15323 {
15324 LONGEST offset;
15325
15326 /* C++ base class field. */
15327 if (handle_data_member_location (die, cu, &offset))
15328 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15329 FIELD_BITSIZE (*fp) = 0;
15330 FIELD_TYPE (*fp) = die_type (die, cu);
15331 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15332 fip->nbaseclasses++;
15333 }
15334 }
15335
15336 /* Can the type given by DIE define another type? */
15337
15338 static bool
15339 type_can_define_types (const struct die_info *die)
15340 {
15341 switch (die->tag)
15342 {
15343 case DW_TAG_typedef:
15344 case DW_TAG_class_type:
15345 case DW_TAG_structure_type:
15346 case DW_TAG_union_type:
15347 case DW_TAG_enumeration_type:
15348 return true;
15349
15350 default:
15351 return false;
15352 }
15353 }
15354
15355 /* Add a type definition defined in the scope of the FIP's class. */
15356
15357 static void
15358 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15359 struct dwarf2_cu *cu)
15360 {
15361 struct decl_field_list *new_field;
15362 struct decl_field *fp;
15363
15364 /* Allocate a new field list entry and link it in. */
15365 new_field = XCNEW (struct decl_field_list);
15366 make_cleanup (xfree, new_field);
15367
15368 gdb_assert (type_can_define_types (die));
15369
15370 fp = &new_field->field;
15371
15372 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15373 fp->name = dwarf2_name (die, cu);
15374 fp->type = read_type_die (die, cu);
15375
15376 /* Save accessibility. */
15377 enum dwarf_access_attribute accessibility;
15378 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15379 if (attr != NULL)
15380 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15381 else
15382 accessibility = dwarf2_default_access_attribute (die, cu);
15383 switch (accessibility)
15384 {
15385 case DW_ACCESS_public:
15386 /* The assumed value if neither private nor protected. */
15387 break;
15388 case DW_ACCESS_private:
15389 fp->is_private = 1;
15390 break;
15391 case DW_ACCESS_protected:
15392 fp->is_protected = 1;
15393 break;
15394 default:
15395 complaint (&symfile_complaints,
15396 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15397 }
15398
15399 if (die->tag == DW_TAG_typedef)
15400 {
15401 new_field->next = fip->typedef_field_list;
15402 fip->typedef_field_list = new_field;
15403 fip->typedef_field_list_count++;
15404 }
15405 else
15406 {
15407 new_field->next = fip->nested_types_list;
15408 fip->nested_types_list = new_field;
15409 fip->nested_types_list_count++;
15410 }
15411 }
15412
15413 /* Create the vector of fields, and attach it to the type. */
15414
15415 static void
15416 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15417 struct dwarf2_cu *cu)
15418 {
15419 int nfields = fip->nfields;
15420
15421 /* Record the field count, allocate space for the array of fields,
15422 and create blank accessibility bitfields if necessary. */
15423 TYPE_NFIELDS (type) = nfields;
15424 TYPE_FIELDS (type) = (struct field *)
15425 TYPE_ALLOC (type, sizeof (struct field) * nfields);
15426 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
15427
15428 if (fip->non_public_fields && cu->language != language_ada)
15429 {
15430 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15431
15432 TYPE_FIELD_PRIVATE_BITS (type) =
15433 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15434 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15435
15436 TYPE_FIELD_PROTECTED_BITS (type) =
15437 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15438 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15439
15440 TYPE_FIELD_IGNORE_BITS (type) =
15441 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15442 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15443 }
15444
15445 /* If the type has baseclasses, allocate and clear a bit vector for
15446 TYPE_FIELD_VIRTUAL_BITS. */
15447 if (fip->nbaseclasses && cu->language != language_ada)
15448 {
15449 int num_bytes = B_BYTES (fip->nbaseclasses);
15450 unsigned char *pointer;
15451
15452 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15453 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15454 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15455 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
15456 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
15457 }
15458
15459 /* Copy the saved-up fields into the field vector. Start from the head of
15460 the list, adding to the tail of the field array, so that they end up in
15461 the same order in the array in which they were added to the list. */
15462 while (nfields-- > 0)
15463 {
15464 struct nextfield *fieldp;
15465
15466 if (fip->fields)
15467 {
15468 fieldp = fip->fields;
15469 fip->fields = fieldp->next;
15470 }
15471 else
15472 {
15473 fieldp = fip->baseclasses;
15474 fip->baseclasses = fieldp->next;
15475 }
15476
15477 TYPE_FIELD (type, nfields) = fieldp->field;
15478 switch (fieldp->accessibility)
15479 {
15480 case DW_ACCESS_private:
15481 if (cu->language != language_ada)
15482 SET_TYPE_FIELD_PRIVATE (type, nfields);
15483 break;
15484
15485 case DW_ACCESS_protected:
15486 if (cu->language != language_ada)
15487 SET_TYPE_FIELD_PROTECTED (type, nfields);
15488 break;
15489
15490 case DW_ACCESS_public:
15491 break;
15492
15493 default:
15494 /* Unknown accessibility. Complain and treat it as public. */
15495 {
15496 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15497 fieldp->accessibility);
15498 }
15499 break;
15500 }
15501 if (nfields < fip->nbaseclasses)
15502 {
15503 switch (fieldp->virtuality)
15504 {
15505 case DW_VIRTUALITY_virtual:
15506 case DW_VIRTUALITY_pure_virtual:
15507 if (cu->language == language_ada)
15508 error (_("unexpected virtuality in component of Ada type"));
15509 SET_TYPE_FIELD_VIRTUAL (type, nfields);
15510 break;
15511 }
15512 }
15513 }
15514 }
15515
15516 /* Return true if this member function is a constructor, false
15517 otherwise. */
15518
15519 static int
15520 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15521 {
15522 const char *fieldname;
15523 const char *type_name;
15524 int len;
15525
15526 if (die->parent == NULL)
15527 return 0;
15528
15529 if (die->parent->tag != DW_TAG_structure_type
15530 && die->parent->tag != DW_TAG_union_type
15531 && die->parent->tag != DW_TAG_class_type)
15532 return 0;
15533
15534 fieldname = dwarf2_name (die, cu);
15535 type_name = dwarf2_name (die->parent, cu);
15536 if (fieldname == NULL || type_name == NULL)
15537 return 0;
15538
15539 len = strlen (fieldname);
15540 return (strncmp (fieldname, type_name, len) == 0
15541 && (type_name[len] == '\0' || type_name[len] == '<'));
15542 }
15543
15544 /* Add a member function to the proper fieldlist. */
15545
15546 static void
15547 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15548 struct type *type, struct dwarf2_cu *cu)
15549 {
15550 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15551 struct attribute *attr;
15552 struct fnfieldlist *flp;
15553 int i;
15554 struct fn_field *fnp;
15555 const char *fieldname;
15556 struct nextfnfield *new_fnfield;
15557 struct type *this_type;
15558 enum dwarf_access_attribute accessibility;
15559
15560 if (cu->language == language_ada)
15561 error (_("unexpected member function in Ada type"));
15562
15563 /* Get name of member function. */
15564 fieldname = dwarf2_name (die, cu);
15565 if (fieldname == NULL)
15566 return;
15567
15568 /* Look up member function name in fieldlist. */
15569 for (i = 0; i < fip->nfnfields; i++)
15570 {
15571 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15572 break;
15573 }
15574
15575 /* Create new list element if necessary. */
15576 if (i < fip->nfnfields)
15577 flp = &fip->fnfieldlists[i];
15578 else
15579 {
15580 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
15581 {
15582 fip->fnfieldlists = (struct fnfieldlist *)
15583 xrealloc (fip->fnfieldlists,
15584 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
15585 * sizeof (struct fnfieldlist));
15586 if (fip->nfnfields == 0)
15587 make_cleanup (free_current_contents, &fip->fnfieldlists);
15588 }
15589 flp = &fip->fnfieldlists[fip->nfnfields];
15590 flp->name = fieldname;
15591 flp->length = 0;
15592 flp->head = NULL;
15593 i = fip->nfnfields++;
15594 }
15595
15596 /* Create a new member function field and chain it to the field list
15597 entry. */
15598 new_fnfield = XNEW (struct nextfnfield);
15599 make_cleanup (xfree, new_fnfield);
15600 memset (new_fnfield, 0, sizeof (struct nextfnfield));
15601 new_fnfield->next = flp->head;
15602 flp->head = new_fnfield;
15603 flp->length++;
15604
15605 /* Fill in the member function field info. */
15606 fnp = &new_fnfield->fnfield;
15607
15608 /* Delay processing of the physname until later. */
15609 if (cu->language == language_cplus)
15610 {
15611 add_to_method_list (type, i, flp->length - 1, fieldname,
15612 die, cu);
15613 }
15614 else
15615 {
15616 const char *physname = dwarf2_physname (fieldname, die, cu);
15617 fnp->physname = physname ? physname : "";
15618 }
15619
15620 fnp->type = alloc_type (objfile);
15621 this_type = read_type_die (die, cu);
15622 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15623 {
15624 int nparams = TYPE_NFIELDS (this_type);
15625
15626 /* TYPE is the domain of this method, and THIS_TYPE is the type
15627 of the method itself (TYPE_CODE_METHOD). */
15628 smash_to_method_type (fnp->type, type,
15629 TYPE_TARGET_TYPE (this_type),
15630 TYPE_FIELDS (this_type),
15631 TYPE_NFIELDS (this_type),
15632 TYPE_VARARGS (this_type));
15633
15634 /* Handle static member functions.
15635 Dwarf2 has no clean way to discern C++ static and non-static
15636 member functions. G++ helps GDB by marking the first
15637 parameter for non-static member functions (which is the this
15638 pointer) as artificial. We obtain this information from
15639 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15640 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15641 fnp->voffset = VOFFSET_STATIC;
15642 }
15643 else
15644 complaint (&symfile_complaints, _("member function type missing for '%s'"),
15645 dwarf2_full_name (fieldname, die, cu));
15646
15647 /* Get fcontext from DW_AT_containing_type if present. */
15648 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15649 fnp->fcontext = die_containing_type (die, cu);
15650
15651 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15652 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15653
15654 /* Get accessibility. */
15655 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15656 if (attr)
15657 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15658 else
15659 accessibility = dwarf2_default_access_attribute (die, cu);
15660 switch (accessibility)
15661 {
15662 case DW_ACCESS_private:
15663 fnp->is_private = 1;
15664 break;
15665 case DW_ACCESS_protected:
15666 fnp->is_protected = 1;
15667 break;
15668 }
15669
15670 /* Check for artificial methods. */
15671 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15672 if (attr && DW_UNSND (attr) != 0)
15673 fnp->is_artificial = 1;
15674
15675 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15676
15677 /* Get index in virtual function table if it is a virtual member
15678 function. For older versions of GCC, this is an offset in the
15679 appropriate virtual table, as specified by DW_AT_containing_type.
15680 For everyone else, it is an expression to be evaluated relative
15681 to the object address. */
15682
15683 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15684 if (attr)
15685 {
15686 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15687 {
15688 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15689 {
15690 /* Old-style GCC. */
15691 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15692 }
15693 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15694 || (DW_BLOCK (attr)->size > 1
15695 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15696 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15697 {
15698 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15699 if ((fnp->voffset % cu->header.addr_size) != 0)
15700 dwarf2_complex_location_expr_complaint ();
15701 else
15702 fnp->voffset /= cu->header.addr_size;
15703 fnp->voffset += 2;
15704 }
15705 else
15706 dwarf2_complex_location_expr_complaint ();
15707
15708 if (!fnp->fcontext)
15709 {
15710 /* If there is no `this' field and no DW_AT_containing_type,
15711 we cannot actually find a base class context for the
15712 vtable! */
15713 if (TYPE_NFIELDS (this_type) == 0
15714 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15715 {
15716 complaint (&symfile_complaints,
15717 _("cannot determine context for virtual member "
15718 "function \"%s\" (offset %s)"),
15719 fieldname, sect_offset_str (die->sect_off));
15720 }
15721 else
15722 {
15723 fnp->fcontext
15724 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15725 }
15726 }
15727 }
15728 else if (attr_form_is_section_offset (attr))
15729 {
15730 dwarf2_complex_location_expr_complaint ();
15731 }
15732 else
15733 {
15734 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15735 fieldname);
15736 }
15737 }
15738 else
15739 {
15740 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15741 if (attr && DW_UNSND (attr))
15742 {
15743 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15744 complaint (&symfile_complaints,
15745 _("Member function \"%s\" (offset %s) is virtual "
15746 "but the vtable offset is not specified"),
15747 fieldname, sect_offset_str (die->sect_off));
15748 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15749 TYPE_CPLUS_DYNAMIC (type) = 1;
15750 }
15751 }
15752 }
15753
15754 /* Create the vector of member function fields, and attach it to the type. */
15755
15756 static void
15757 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15758 struct dwarf2_cu *cu)
15759 {
15760 struct fnfieldlist *flp;
15761 int i;
15762
15763 if (cu->language == language_ada)
15764 error (_("unexpected member functions in Ada type"));
15765
15766 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15767 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15768 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
15769
15770 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
15771 {
15772 struct nextfnfield *nfp = flp->head;
15773 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15774 int k;
15775
15776 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
15777 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
15778 fn_flp->fn_fields = (struct fn_field *)
15779 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
15780 for (k = flp->length; (k--, nfp); nfp = nfp->next)
15781 fn_flp->fn_fields[k] = nfp->fnfield;
15782 }
15783
15784 TYPE_NFN_FIELDS (type) = fip->nfnfields;
15785 }
15786
15787 /* Returns non-zero if NAME is the name of a vtable member in CU's
15788 language, zero otherwise. */
15789 static int
15790 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15791 {
15792 static const char vptr[] = "_vptr";
15793
15794 /* Look for the C++ form of the vtable. */
15795 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15796 return 1;
15797
15798 return 0;
15799 }
15800
15801 /* GCC outputs unnamed structures that are really pointers to member
15802 functions, with the ABI-specified layout. If TYPE describes
15803 such a structure, smash it into a member function type.
15804
15805 GCC shouldn't do this; it should just output pointer to member DIEs.
15806 This is GCC PR debug/28767. */
15807
15808 static void
15809 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15810 {
15811 struct type *pfn_type, *self_type, *new_type;
15812
15813 /* Check for a structure with no name and two children. */
15814 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15815 return;
15816
15817 /* Check for __pfn and __delta members. */
15818 if (TYPE_FIELD_NAME (type, 0) == NULL
15819 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15820 || TYPE_FIELD_NAME (type, 1) == NULL
15821 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15822 return;
15823
15824 /* Find the type of the method. */
15825 pfn_type = TYPE_FIELD_TYPE (type, 0);
15826 if (pfn_type == NULL
15827 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15828 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15829 return;
15830
15831 /* Look for the "this" argument. */
15832 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15833 if (TYPE_NFIELDS (pfn_type) == 0
15834 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15835 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15836 return;
15837
15838 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15839 new_type = alloc_type (objfile);
15840 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15841 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15842 TYPE_VARARGS (pfn_type));
15843 smash_to_methodptr_type (type, new_type);
15844 }
15845
15846
15847 /* Called when we find the DIE that starts a structure or union scope
15848 (definition) to create a type for the structure or union. Fill in
15849 the type's name and general properties; the members will not be
15850 processed until process_structure_scope. A symbol table entry for
15851 the type will also not be done until process_structure_scope (assuming
15852 the type has a name).
15853
15854 NOTE: we need to call these functions regardless of whether or not the
15855 DIE has a DW_AT_name attribute, since it might be an anonymous
15856 structure or union. This gets the type entered into our set of
15857 user defined types. */
15858
15859 static struct type *
15860 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15861 {
15862 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15863 struct type *type;
15864 struct attribute *attr;
15865 const char *name;
15866
15867 /* If the definition of this type lives in .debug_types, read that type.
15868 Don't follow DW_AT_specification though, that will take us back up
15869 the chain and we want to go down. */
15870 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15871 if (attr)
15872 {
15873 type = get_DW_AT_signature_type (die, attr, cu);
15874
15875 /* The type's CU may not be the same as CU.
15876 Ensure TYPE is recorded with CU in die_type_hash. */
15877 return set_die_type (die, type, cu);
15878 }
15879
15880 type = alloc_type (objfile);
15881 INIT_CPLUS_SPECIFIC (type);
15882
15883 name = dwarf2_name (die, cu);
15884 if (name != NULL)
15885 {
15886 if (cu->language == language_cplus
15887 || cu->language == language_d
15888 || cu->language == language_rust)
15889 {
15890 const char *full_name = dwarf2_full_name (name, die, cu);
15891
15892 /* dwarf2_full_name might have already finished building the DIE's
15893 type. If so, there is no need to continue. */
15894 if (get_die_type (die, cu) != NULL)
15895 return get_die_type (die, cu);
15896
15897 TYPE_TAG_NAME (type) = full_name;
15898 if (die->tag == DW_TAG_structure_type
15899 || die->tag == DW_TAG_class_type)
15900 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15901 }
15902 else
15903 {
15904 /* The name is already allocated along with this objfile, so
15905 we don't need to duplicate it for the type. */
15906 TYPE_TAG_NAME (type) = name;
15907 if (die->tag == DW_TAG_class_type)
15908 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15909 }
15910 }
15911
15912 if (die->tag == DW_TAG_structure_type)
15913 {
15914 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15915 }
15916 else if (die->tag == DW_TAG_union_type)
15917 {
15918 TYPE_CODE (type) = TYPE_CODE_UNION;
15919 }
15920 else
15921 {
15922 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15923 }
15924
15925 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15926 TYPE_DECLARED_CLASS (type) = 1;
15927
15928 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15929 if (attr)
15930 {
15931 if (attr_form_is_constant (attr))
15932 TYPE_LENGTH (type) = DW_UNSND (attr);
15933 else
15934 {
15935 /* For the moment, dynamic type sizes are not supported
15936 by GDB's struct type. The actual size is determined
15937 on-demand when resolving the type of a given object,
15938 so set the type's length to zero for now. Otherwise,
15939 we record an expression as the length, and that expression
15940 could lead to a very large value, which could eventually
15941 lead to us trying to allocate that much memory when creating
15942 a value of that type. */
15943 TYPE_LENGTH (type) = 0;
15944 }
15945 }
15946 else
15947 {
15948 TYPE_LENGTH (type) = 0;
15949 }
15950
15951 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15952 {
15953 /* ICC<14 does not output the required DW_AT_declaration on
15954 incomplete types, but gives them a size of zero. */
15955 TYPE_STUB (type) = 1;
15956 }
15957 else
15958 TYPE_STUB_SUPPORTED (type) = 1;
15959
15960 if (die_is_declaration (die, cu))
15961 TYPE_STUB (type) = 1;
15962 else if (attr == NULL && die->child == NULL
15963 && producer_is_realview (cu->producer))
15964 /* RealView does not output the required DW_AT_declaration
15965 on incomplete types. */
15966 TYPE_STUB (type) = 1;
15967
15968 /* We need to add the type field to the die immediately so we don't
15969 infinitely recurse when dealing with pointers to the structure
15970 type within the structure itself. */
15971 set_die_type (die, type, cu);
15972
15973 /* set_die_type should be already done. */
15974 set_descriptive_type (type, die, cu);
15975
15976 return type;
15977 }
15978
15979 /* Finish creating a structure or union type, including filling in
15980 its members and creating a symbol for it. */
15981
15982 static void
15983 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15984 {
15985 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15986 struct die_info *child_die;
15987 struct type *type;
15988
15989 type = get_die_type (die, cu);
15990 if (type == NULL)
15991 type = read_structure_type (die, cu);
15992
15993 if (die->child != NULL && ! die_is_declaration (die, cu))
15994 {
15995 struct field_info fi;
15996 std::vector<struct symbol *> template_args;
15997 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
15998
15999 memset (&fi, 0, sizeof (struct field_info));
16000
16001 child_die = die->child;
16002
16003 while (child_die && child_die->tag)
16004 {
16005 if (child_die->tag == DW_TAG_member
16006 || child_die->tag == DW_TAG_variable)
16007 {
16008 /* NOTE: carlton/2002-11-05: A C++ static data member
16009 should be a DW_TAG_member that is a declaration, but
16010 all versions of G++ as of this writing (so through at
16011 least 3.2.1) incorrectly generate DW_TAG_variable
16012 tags for them instead. */
16013 dwarf2_add_field (&fi, child_die, cu);
16014 }
16015 else if (child_die->tag == DW_TAG_subprogram)
16016 {
16017 /* Rust doesn't have member functions in the C++ sense.
16018 However, it does emit ordinary functions as children
16019 of a struct DIE. */
16020 if (cu->language == language_rust)
16021 read_func_scope (child_die, cu);
16022 else
16023 {
16024 /* C++ member function. */
16025 dwarf2_add_member_fn (&fi, child_die, type, cu);
16026 }
16027 }
16028 else if (child_die->tag == DW_TAG_inheritance)
16029 {
16030 /* C++ base class field. */
16031 dwarf2_add_field (&fi, child_die, cu);
16032 }
16033 else if (type_can_define_types (child_die))
16034 dwarf2_add_type_defn (&fi, child_die, cu);
16035 else if (child_die->tag == DW_TAG_template_type_param
16036 || child_die->tag == DW_TAG_template_value_param)
16037 {
16038 struct symbol *arg = new_symbol (child_die, NULL, cu);
16039
16040 if (arg != NULL)
16041 template_args.push_back (arg);
16042 }
16043
16044 child_die = sibling_die (child_die);
16045 }
16046
16047 /* Attach template arguments to type. */
16048 if (!template_args.empty ())
16049 {
16050 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16051 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16052 TYPE_TEMPLATE_ARGUMENTS (type)
16053 = XOBNEWVEC (&objfile->objfile_obstack,
16054 struct symbol *,
16055 TYPE_N_TEMPLATE_ARGUMENTS (type));
16056 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16057 template_args.data (),
16058 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16059 * sizeof (struct symbol *)));
16060 }
16061
16062 /* Attach fields and member functions to the type. */
16063 if (fi.nfields)
16064 dwarf2_attach_fields_to_type (&fi, type, cu);
16065 if (fi.nfnfields)
16066 {
16067 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16068
16069 /* Get the type which refers to the base class (possibly this
16070 class itself) which contains the vtable pointer for the current
16071 class from the DW_AT_containing_type attribute. This use of
16072 DW_AT_containing_type is a GNU extension. */
16073
16074 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16075 {
16076 struct type *t = die_containing_type (die, cu);
16077
16078 set_type_vptr_basetype (type, t);
16079 if (type == t)
16080 {
16081 int i;
16082
16083 /* Our own class provides vtbl ptr. */
16084 for (i = TYPE_NFIELDS (t) - 1;
16085 i >= TYPE_N_BASECLASSES (t);
16086 --i)
16087 {
16088 const char *fieldname = TYPE_FIELD_NAME (t, i);
16089
16090 if (is_vtable_name (fieldname, cu))
16091 {
16092 set_type_vptr_fieldno (type, i);
16093 break;
16094 }
16095 }
16096
16097 /* Complain if virtual function table field not found. */
16098 if (i < TYPE_N_BASECLASSES (t))
16099 complaint (&symfile_complaints,
16100 _("virtual function table pointer "
16101 "not found when defining class '%s'"),
16102 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16103 "");
16104 }
16105 else
16106 {
16107 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16108 }
16109 }
16110 else if (cu->producer
16111 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16112 {
16113 /* The IBM XLC compiler does not provide direct indication
16114 of the containing type, but the vtable pointer is
16115 always named __vfp. */
16116
16117 int i;
16118
16119 for (i = TYPE_NFIELDS (type) - 1;
16120 i >= TYPE_N_BASECLASSES (type);
16121 --i)
16122 {
16123 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16124 {
16125 set_type_vptr_fieldno (type, i);
16126 set_type_vptr_basetype (type, type);
16127 break;
16128 }
16129 }
16130 }
16131 }
16132
16133 /* Copy fi.typedef_field_list linked list elements content into the
16134 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16135 if (fi.typedef_field_list)
16136 {
16137 int i = fi.typedef_field_list_count;
16138
16139 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16140 TYPE_TYPEDEF_FIELD_ARRAY (type)
16141 = ((struct decl_field *)
16142 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
16143 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
16144
16145 /* Reverse the list order to keep the debug info elements order. */
16146 while (--i >= 0)
16147 {
16148 struct decl_field *dest, *src;
16149
16150 dest = &TYPE_TYPEDEF_FIELD (type, i);
16151 src = &fi.typedef_field_list->field;
16152 fi.typedef_field_list = fi.typedef_field_list->next;
16153 *dest = *src;
16154 }
16155 }
16156
16157 /* Copy fi.nested_types_list linked list elements content into the
16158 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16159 if (fi.nested_types_list != NULL && cu->language != language_ada)
16160 {
16161 int i = fi.nested_types_list_count;
16162
16163 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16164 TYPE_NESTED_TYPES_ARRAY (type)
16165 = ((struct decl_field *)
16166 TYPE_ALLOC (type, sizeof (struct decl_field) * i));
16167 TYPE_NESTED_TYPES_COUNT (type) = i;
16168
16169 /* Reverse the list order to keep the debug info elements order. */
16170 while (--i >= 0)
16171 {
16172 struct decl_field *dest, *src;
16173
16174 dest = &TYPE_NESTED_TYPES_FIELD (type, i);
16175 src = &fi.nested_types_list->field;
16176 fi.nested_types_list = fi.nested_types_list->next;
16177 *dest = *src;
16178 }
16179 }
16180
16181 do_cleanups (back_to);
16182 }
16183
16184 quirk_gcc_member_function_pointer (type, objfile);
16185
16186 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16187 snapshots) has been known to create a die giving a declaration
16188 for a class that has, as a child, a die giving a definition for a
16189 nested class. So we have to process our children even if the
16190 current die is a declaration. Normally, of course, a declaration
16191 won't have any children at all. */
16192
16193 child_die = die->child;
16194
16195 while (child_die != NULL && child_die->tag)
16196 {
16197 if (child_die->tag == DW_TAG_member
16198 || child_die->tag == DW_TAG_variable
16199 || child_die->tag == DW_TAG_inheritance
16200 || child_die->tag == DW_TAG_template_value_param
16201 || child_die->tag == DW_TAG_template_type_param)
16202 {
16203 /* Do nothing. */
16204 }
16205 else
16206 process_die (child_die, cu);
16207
16208 child_die = sibling_die (child_die);
16209 }
16210
16211 /* Do not consider external references. According to the DWARF standard,
16212 these DIEs are identified by the fact that they have no byte_size
16213 attribute, and a declaration attribute. */
16214 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16215 || !die_is_declaration (die, cu))
16216 new_symbol (die, type, cu);
16217 }
16218
16219 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16220 update TYPE using some information only available in DIE's children. */
16221
16222 static void
16223 update_enumeration_type_from_children (struct die_info *die,
16224 struct type *type,
16225 struct dwarf2_cu *cu)
16226 {
16227 struct die_info *child_die;
16228 int unsigned_enum = 1;
16229 int flag_enum = 1;
16230 ULONGEST mask = 0;
16231
16232 auto_obstack obstack;
16233
16234 for (child_die = die->child;
16235 child_die != NULL && child_die->tag;
16236 child_die = sibling_die (child_die))
16237 {
16238 struct attribute *attr;
16239 LONGEST value;
16240 const gdb_byte *bytes;
16241 struct dwarf2_locexpr_baton *baton;
16242 const char *name;
16243
16244 if (child_die->tag != DW_TAG_enumerator)
16245 continue;
16246
16247 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16248 if (attr == NULL)
16249 continue;
16250
16251 name = dwarf2_name (child_die, cu);
16252 if (name == NULL)
16253 name = "<anonymous enumerator>";
16254
16255 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16256 &value, &bytes, &baton);
16257 if (value < 0)
16258 {
16259 unsigned_enum = 0;
16260 flag_enum = 0;
16261 }
16262 else if ((mask & value) != 0)
16263 flag_enum = 0;
16264 else
16265 mask |= value;
16266
16267 /* If we already know that the enum type is neither unsigned, nor
16268 a flag type, no need to look at the rest of the enumerates. */
16269 if (!unsigned_enum && !flag_enum)
16270 break;
16271 }
16272
16273 if (unsigned_enum)
16274 TYPE_UNSIGNED (type) = 1;
16275 if (flag_enum)
16276 TYPE_FLAG_ENUM (type) = 1;
16277 }
16278
16279 /* Given a DW_AT_enumeration_type die, set its type. We do not
16280 complete the type's fields yet, or create any symbols. */
16281
16282 static struct type *
16283 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16284 {
16285 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16286 struct type *type;
16287 struct attribute *attr;
16288 const char *name;
16289
16290 /* If the definition of this type lives in .debug_types, read that type.
16291 Don't follow DW_AT_specification though, that will take us back up
16292 the chain and we want to go down. */
16293 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16294 if (attr)
16295 {
16296 type = get_DW_AT_signature_type (die, attr, cu);
16297
16298 /* The type's CU may not be the same as CU.
16299 Ensure TYPE is recorded with CU in die_type_hash. */
16300 return set_die_type (die, type, cu);
16301 }
16302
16303 type = alloc_type (objfile);
16304
16305 TYPE_CODE (type) = TYPE_CODE_ENUM;
16306 name = dwarf2_full_name (NULL, die, cu);
16307 if (name != NULL)
16308 TYPE_TAG_NAME (type) = name;
16309
16310 attr = dwarf2_attr (die, DW_AT_type, cu);
16311 if (attr != NULL)
16312 {
16313 struct type *underlying_type = die_type (die, cu);
16314
16315 TYPE_TARGET_TYPE (type) = underlying_type;
16316 }
16317
16318 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16319 if (attr)
16320 {
16321 TYPE_LENGTH (type) = DW_UNSND (attr);
16322 }
16323 else
16324 {
16325 TYPE_LENGTH (type) = 0;
16326 }
16327
16328 /* The enumeration DIE can be incomplete. In Ada, any type can be
16329 declared as private in the package spec, and then defined only
16330 inside the package body. Such types are known as Taft Amendment
16331 Types. When another package uses such a type, an incomplete DIE
16332 may be generated by the compiler. */
16333 if (die_is_declaration (die, cu))
16334 TYPE_STUB (type) = 1;
16335
16336 /* Finish the creation of this type by using the enum's children.
16337 We must call this even when the underlying type has been provided
16338 so that we can determine if we're looking at a "flag" enum. */
16339 update_enumeration_type_from_children (die, type, cu);
16340
16341 /* If this type has an underlying type that is not a stub, then we
16342 may use its attributes. We always use the "unsigned" attribute
16343 in this situation, because ordinarily we guess whether the type
16344 is unsigned -- but the guess can be wrong and the underlying type
16345 can tell us the reality. However, we defer to a local size
16346 attribute if one exists, because this lets the compiler override
16347 the underlying type if needed. */
16348 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16349 {
16350 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16351 if (TYPE_LENGTH (type) == 0)
16352 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16353 }
16354
16355 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16356
16357 return set_die_type (die, type, cu);
16358 }
16359
16360 /* Given a pointer to a die which begins an enumeration, process all
16361 the dies that define the members of the enumeration, and create the
16362 symbol for the enumeration type.
16363
16364 NOTE: We reverse the order of the element list. */
16365
16366 static void
16367 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16368 {
16369 struct type *this_type;
16370
16371 this_type = get_die_type (die, cu);
16372 if (this_type == NULL)
16373 this_type = read_enumeration_type (die, cu);
16374
16375 if (die->child != NULL)
16376 {
16377 struct die_info *child_die;
16378 struct symbol *sym;
16379 struct field *fields = NULL;
16380 int num_fields = 0;
16381 const char *name;
16382
16383 child_die = die->child;
16384 while (child_die && child_die->tag)
16385 {
16386 if (child_die->tag != DW_TAG_enumerator)
16387 {
16388 process_die (child_die, cu);
16389 }
16390 else
16391 {
16392 name = dwarf2_name (child_die, cu);
16393 if (name)
16394 {
16395 sym = new_symbol (child_die, this_type, cu);
16396
16397 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16398 {
16399 fields = (struct field *)
16400 xrealloc (fields,
16401 (num_fields + DW_FIELD_ALLOC_CHUNK)
16402 * sizeof (struct field));
16403 }
16404
16405 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16406 FIELD_TYPE (fields[num_fields]) = NULL;
16407 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16408 FIELD_BITSIZE (fields[num_fields]) = 0;
16409
16410 num_fields++;
16411 }
16412 }
16413
16414 child_die = sibling_die (child_die);
16415 }
16416
16417 if (num_fields)
16418 {
16419 TYPE_NFIELDS (this_type) = num_fields;
16420 TYPE_FIELDS (this_type) = (struct field *)
16421 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16422 memcpy (TYPE_FIELDS (this_type), fields,
16423 sizeof (struct field) * num_fields);
16424 xfree (fields);
16425 }
16426 }
16427
16428 /* If we are reading an enum from a .debug_types unit, and the enum
16429 is a declaration, and the enum is not the signatured type in the
16430 unit, then we do not want to add a symbol for it. Adding a
16431 symbol would in some cases obscure the true definition of the
16432 enum, giving users an incomplete type when the definition is
16433 actually available. Note that we do not want to do this for all
16434 enums which are just declarations, because C++0x allows forward
16435 enum declarations. */
16436 if (cu->per_cu->is_debug_types
16437 && die_is_declaration (die, cu))
16438 {
16439 struct signatured_type *sig_type;
16440
16441 sig_type = (struct signatured_type *) cu->per_cu;
16442 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16443 if (sig_type->type_offset_in_section != die->sect_off)
16444 return;
16445 }
16446
16447 new_symbol (die, this_type, cu);
16448 }
16449
16450 /* Extract all information from a DW_TAG_array_type DIE and put it in
16451 the DIE's type field. For now, this only handles one dimensional
16452 arrays. */
16453
16454 static struct type *
16455 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16456 {
16457 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16458 struct die_info *child_die;
16459 struct type *type;
16460 struct type *element_type, *range_type, *index_type;
16461 struct attribute *attr;
16462 const char *name;
16463 struct dynamic_prop *byte_stride_prop = NULL;
16464 unsigned int bit_stride = 0;
16465
16466 element_type = die_type (die, cu);
16467
16468 /* The die_type call above may have already set the type for this DIE. */
16469 type = get_die_type (die, cu);
16470 if (type)
16471 return type;
16472
16473 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16474 if (attr != NULL)
16475 {
16476 int stride_ok;
16477
16478 byte_stride_prop
16479 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16480 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16481 if (!stride_ok)
16482 {
16483 complaint (&symfile_complaints,
16484 _("unable to read array DW_AT_byte_stride "
16485 " - DIE at %s [in module %s]"),
16486 sect_offset_str (die->sect_off),
16487 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16488 /* Ignore this attribute. We will likely not be able to print
16489 arrays of this type correctly, but there is little we can do
16490 to help if we cannot read the attribute's value. */
16491 byte_stride_prop = NULL;
16492 }
16493 }
16494
16495 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16496 if (attr != NULL)
16497 bit_stride = DW_UNSND (attr);
16498
16499 /* Irix 6.2 native cc creates array types without children for
16500 arrays with unspecified length. */
16501 if (die->child == NULL)
16502 {
16503 index_type = objfile_type (objfile)->builtin_int;
16504 range_type = create_static_range_type (NULL, index_type, 0, -1);
16505 type = create_array_type_with_stride (NULL, element_type, range_type,
16506 byte_stride_prop, bit_stride);
16507 return set_die_type (die, type, cu);
16508 }
16509
16510 std::vector<struct type *> range_types;
16511 child_die = die->child;
16512 while (child_die && child_die->tag)
16513 {
16514 if (child_die->tag == DW_TAG_subrange_type)
16515 {
16516 struct type *child_type = read_type_die (child_die, cu);
16517
16518 if (child_type != NULL)
16519 {
16520 /* The range type was succesfully read. Save it for the
16521 array type creation. */
16522 range_types.push_back (child_type);
16523 }
16524 }
16525 child_die = sibling_die (child_die);
16526 }
16527
16528 /* Dwarf2 dimensions are output from left to right, create the
16529 necessary array types in backwards order. */
16530
16531 type = element_type;
16532
16533 if (read_array_order (die, cu) == DW_ORD_col_major)
16534 {
16535 int i = 0;
16536
16537 while (i < range_types.size ())
16538 type = create_array_type_with_stride (NULL, type, range_types[i++],
16539 byte_stride_prop, bit_stride);
16540 }
16541 else
16542 {
16543 size_t ndim = range_types.size ();
16544 while (ndim-- > 0)
16545 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16546 byte_stride_prop, bit_stride);
16547 }
16548
16549 /* Understand Dwarf2 support for vector types (like they occur on
16550 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16551 array type. This is not part of the Dwarf2/3 standard yet, but a
16552 custom vendor extension. The main difference between a regular
16553 array and the vector variant is that vectors are passed by value
16554 to functions. */
16555 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16556 if (attr)
16557 make_vector_type (type);
16558
16559 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16560 implementation may choose to implement triple vectors using this
16561 attribute. */
16562 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16563 if (attr)
16564 {
16565 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16566 TYPE_LENGTH (type) = DW_UNSND (attr);
16567 else
16568 complaint (&symfile_complaints,
16569 _("DW_AT_byte_size for array type smaller "
16570 "than the total size of elements"));
16571 }
16572
16573 name = dwarf2_name (die, cu);
16574 if (name)
16575 TYPE_NAME (type) = name;
16576
16577 /* Install the type in the die. */
16578 set_die_type (die, type, cu);
16579
16580 /* set_die_type should be already done. */
16581 set_descriptive_type (type, die, cu);
16582
16583 return type;
16584 }
16585
16586 static enum dwarf_array_dim_ordering
16587 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16588 {
16589 struct attribute *attr;
16590
16591 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16592
16593 if (attr)
16594 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16595
16596 /* GNU F77 is a special case, as at 08/2004 array type info is the
16597 opposite order to the dwarf2 specification, but data is still
16598 laid out as per normal fortran.
16599
16600 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16601 version checking. */
16602
16603 if (cu->language == language_fortran
16604 && cu->producer && strstr (cu->producer, "GNU F77"))
16605 {
16606 return DW_ORD_row_major;
16607 }
16608
16609 switch (cu->language_defn->la_array_ordering)
16610 {
16611 case array_column_major:
16612 return DW_ORD_col_major;
16613 case array_row_major:
16614 default:
16615 return DW_ORD_row_major;
16616 };
16617 }
16618
16619 /* Extract all information from a DW_TAG_set_type DIE and put it in
16620 the DIE's type field. */
16621
16622 static struct type *
16623 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16624 {
16625 struct type *domain_type, *set_type;
16626 struct attribute *attr;
16627
16628 domain_type = die_type (die, cu);
16629
16630 /* The die_type call above may have already set the type for this DIE. */
16631 set_type = get_die_type (die, cu);
16632 if (set_type)
16633 return set_type;
16634
16635 set_type = create_set_type (NULL, domain_type);
16636
16637 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16638 if (attr)
16639 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16640
16641 return set_die_type (die, set_type, cu);
16642 }
16643
16644 /* A helper for read_common_block that creates a locexpr baton.
16645 SYM is the symbol which we are marking as computed.
16646 COMMON_DIE is the DIE for the common block.
16647 COMMON_LOC is the location expression attribute for the common
16648 block itself.
16649 MEMBER_LOC is the location expression attribute for the particular
16650 member of the common block that we are processing.
16651 CU is the CU from which the above come. */
16652
16653 static void
16654 mark_common_block_symbol_computed (struct symbol *sym,
16655 struct die_info *common_die,
16656 struct attribute *common_loc,
16657 struct attribute *member_loc,
16658 struct dwarf2_cu *cu)
16659 {
16660 struct dwarf2_per_objfile *dwarf2_per_objfile
16661 = cu->per_cu->dwarf2_per_objfile;
16662 struct objfile *objfile = dwarf2_per_objfile->objfile;
16663 struct dwarf2_locexpr_baton *baton;
16664 gdb_byte *ptr;
16665 unsigned int cu_off;
16666 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16667 LONGEST offset = 0;
16668
16669 gdb_assert (common_loc && member_loc);
16670 gdb_assert (attr_form_is_block (common_loc));
16671 gdb_assert (attr_form_is_block (member_loc)
16672 || attr_form_is_constant (member_loc));
16673
16674 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16675 baton->per_cu = cu->per_cu;
16676 gdb_assert (baton->per_cu);
16677
16678 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16679
16680 if (attr_form_is_constant (member_loc))
16681 {
16682 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16683 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16684 }
16685 else
16686 baton->size += DW_BLOCK (member_loc)->size;
16687
16688 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16689 baton->data = ptr;
16690
16691 *ptr++ = DW_OP_call4;
16692 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16693 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16694 ptr += 4;
16695
16696 if (attr_form_is_constant (member_loc))
16697 {
16698 *ptr++ = DW_OP_addr;
16699 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16700 ptr += cu->header.addr_size;
16701 }
16702 else
16703 {
16704 /* We have to copy the data here, because DW_OP_call4 will only
16705 use a DW_AT_location attribute. */
16706 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16707 ptr += DW_BLOCK (member_loc)->size;
16708 }
16709
16710 *ptr++ = DW_OP_plus;
16711 gdb_assert (ptr - baton->data == baton->size);
16712
16713 SYMBOL_LOCATION_BATON (sym) = baton;
16714 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16715 }
16716
16717 /* Create appropriate locally-scoped variables for all the
16718 DW_TAG_common_block entries. Also create a struct common_block
16719 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16720 is used to sepate the common blocks name namespace from regular
16721 variable names. */
16722
16723 static void
16724 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16725 {
16726 struct attribute *attr;
16727
16728 attr = dwarf2_attr (die, DW_AT_location, cu);
16729 if (attr)
16730 {
16731 /* Support the .debug_loc offsets. */
16732 if (attr_form_is_block (attr))
16733 {
16734 /* Ok. */
16735 }
16736 else if (attr_form_is_section_offset (attr))
16737 {
16738 dwarf2_complex_location_expr_complaint ();
16739 attr = NULL;
16740 }
16741 else
16742 {
16743 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16744 "common block member");
16745 attr = NULL;
16746 }
16747 }
16748
16749 if (die->child != NULL)
16750 {
16751 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16752 struct die_info *child_die;
16753 size_t n_entries = 0, size;
16754 struct common_block *common_block;
16755 struct symbol *sym;
16756
16757 for (child_die = die->child;
16758 child_die && child_die->tag;
16759 child_die = sibling_die (child_die))
16760 ++n_entries;
16761
16762 size = (sizeof (struct common_block)
16763 + (n_entries - 1) * sizeof (struct symbol *));
16764 common_block
16765 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16766 size);
16767 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16768 common_block->n_entries = 0;
16769
16770 for (child_die = die->child;
16771 child_die && child_die->tag;
16772 child_die = sibling_die (child_die))
16773 {
16774 /* Create the symbol in the DW_TAG_common_block block in the current
16775 symbol scope. */
16776 sym = new_symbol (child_die, NULL, cu);
16777 if (sym != NULL)
16778 {
16779 struct attribute *member_loc;
16780
16781 common_block->contents[common_block->n_entries++] = sym;
16782
16783 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16784 cu);
16785 if (member_loc)
16786 {
16787 /* GDB has handled this for a long time, but it is
16788 not specified by DWARF. It seems to have been
16789 emitted by gfortran at least as recently as:
16790 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16791 complaint (&symfile_complaints,
16792 _("Variable in common block has "
16793 "DW_AT_data_member_location "
16794 "- DIE at %s [in module %s]"),
16795 sect_offset_str (child_die->sect_off),
16796 objfile_name (objfile));
16797
16798 if (attr_form_is_section_offset (member_loc))
16799 dwarf2_complex_location_expr_complaint ();
16800 else if (attr_form_is_constant (member_loc)
16801 || attr_form_is_block (member_loc))
16802 {
16803 if (attr)
16804 mark_common_block_symbol_computed (sym, die, attr,
16805 member_loc, cu);
16806 }
16807 else
16808 dwarf2_complex_location_expr_complaint ();
16809 }
16810 }
16811 }
16812
16813 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16814 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16815 }
16816 }
16817
16818 /* Create a type for a C++ namespace. */
16819
16820 static struct type *
16821 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16822 {
16823 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16824 const char *previous_prefix, *name;
16825 int is_anonymous;
16826 struct type *type;
16827
16828 /* For extensions, reuse the type of the original namespace. */
16829 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16830 {
16831 struct die_info *ext_die;
16832 struct dwarf2_cu *ext_cu = cu;
16833
16834 ext_die = dwarf2_extension (die, &ext_cu);
16835 type = read_type_die (ext_die, ext_cu);
16836
16837 /* EXT_CU may not be the same as CU.
16838 Ensure TYPE is recorded with CU in die_type_hash. */
16839 return set_die_type (die, type, cu);
16840 }
16841
16842 name = namespace_name (die, &is_anonymous, cu);
16843
16844 /* Now build the name of the current namespace. */
16845
16846 previous_prefix = determine_prefix (die, cu);
16847 if (previous_prefix[0] != '\0')
16848 name = typename_concat (&objfile->objfile_obstack,
16849 previous_prefix, name, 0, cu);
16850
16851 /* Create the type. */
16852 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16853 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16854
16855 return set_die_type (die, type, cu);
16856 }
16857
16858 /* Read a namespace scope. */
16859
16860 static void
16861 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16862 {
16863 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16864 int is_anonymous;
16865
16866 /* Add a symbol associated to this if we haven't seen the namespace
16867 before. Also, add a using directive if it's an anonymous
16868 namespace. */
16869
16870 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16871 {
16872 struct type *type;
16873
16874 type = read_type_die (die, cu);
16875 new_symbol (die, type, cu);
16876
16877 namespace_name (die, &is_anonymous, cu);
16878 if (is_anonymous)
16879 {
16880 const char *previous_prefix = determine_prefix (die, cu);
16881
16882 std::vector<const char *> excludes;
16883 add_using_directive (using_directives (cu->language),
16884 previous_prefix, TYPE_NAME (type), NULL,
16885 NULL, excludes, 0, &objfile->objfile_obstack);
16886 }
16887 }
16888
16889 if (die->child != NULL)
16890 {
16891 struct die_info *child_die = die->child;
16892
16893 while (child_die && child_die->tag)
16894 {
16895 process_die (child_die, cu);
16896 child_die = sibling_die (child_die);
16897 }
16898 }
16899 }
16900
16901 /* Read a Fortran module as type. This DIE can be only a declaration used for
16902 imported module. Still we need that type as local Fortran "use ... only"
16903 declaration imports depend on the created type in determine_prefix. */
16904
16905 static struct type *
16906 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16907 {
16908 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16909 const char *module_name;
16910 struct type *type;
16911
16912 module_name = dwarf2_name (die, cu);
16913 if (!module_name)
16914 complaint (&symfile_complaints,
16915 _("DW_TAG_module has no name, offset %s"),
16916 sect_offset_str (die->sect_off));
16917 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16918
16919 /* determine_prefix uses TYPE_TAG_NAME. */
16920 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16921
16922 return set_die_type (die, type, cu);
16923 }
16924
16925 /* Read a Fortran module. */
16926
16927 static void
16928 read_module (struct die_info *die, struct dwarf2_cu *cu)
16929 {
16930 struct die_info *child_die = die->child;
16931 struct type *type;
16932
16933 type = read_type_die (die, cu);
16934 new_symbol (die, type, cu);
16935
16936 while (child_die && child_die->tag)
16937 {
16938 process_die (child_die, cu);
16939 child_die = sibling_die (child_die);
16940 }
16941 }
16942
16943 /* Return the name of the namespace represented by DIE. Set
16944 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16945 namespace. */
16946
16947 static const char *
16948 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16949 {
16950 struct die_info *current_die;
16951 const char *name = NULL;
16952
16953 /* Loop through the extensions until we find a name. */
16954
16955 for (current_die = die;
16956 current_die != NULL;
16957 current_die = dwarf2_extension (die, &cu))
16958 {
16959 /* We don't use dwarf2_name here so that we can detect the absence
16960 of a name -> anonymous namespace. */
16961 name = dwarf2_string_attr (die, DW_AT_name, cu);
16962
16963 if (name != NULL)
16964 break;
16965 }
16966
16967 /* Is it an anonymous namespace? */
16968
16969 *is_anonymous = (name == NULL);
16970 if (*is_anonymous)
16971 name = CP_ANONYMOUS_NAMESPACE_STR;
16972
16973 return name;
16974 }
16975
16976 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16977 the user defined type vector. */
16978
16979 static struct type *
16980 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16981 {
16982 struct gdbarch *gdbarch
16983 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16984 struct comp_unit_head *cu_header = &cu->header;
16985 struct type *type;
16986 struct attribute *attr_byte_size;
16987 struct attribute *attr_address_class;
16988 int byte_size, addr_class;
16989 struct type *target_type;
16990
16991 target_type = die_type (die, cu);
16992
16993 /* The die_type call above may have already set the type for this DIE. */
16994 type = get_die_type (die, cu);
16995 if (type)
16996 return type;
16997
16998 type = lookup_pointer_type (target_type);
16999
17000 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17001 if (attr_byte_size)
17002 byte_size = DW_UNSND (attr_byte_size);
17003 else
17004 byte_size = cu_header->addr_size;
17005
17006 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17007 if (attr_address_class)
17008 addr_class = DW_UNSND (attr_address_class);
17009 else
17010 addr_class = DW_ADDR_none;
17011
17012 /* If the pointer size or address class is different than the
17013 default, create a type variant marked as such and set the
17014 length accordingly. */
17015 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
17016 {
17017 if (gdbarch_address_class_type_flags_p (gdbarch))
17018 {
17019 int type_flags;
17020
17021 type_flags = gdbarch_address_class_type_flags
17022 (gdbarch, byte_size, addr_class);
17023 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17024 == 0);
17025 type = make_type_with_address_space (type, type_flags);
17026 }
17027 else if (TYPE_LENGTH (type) != byte_size)
17028 {
17029 complaint (&symfile_complaints,
17030 _("invalid pointer size %d"), byte_size);
17031 }
17032 else
17033 {
17034 /* Should we also complain about unhandled address classes? */
17035 }
17036 }
17037
17038 TYPE_LENGTH (type) = byte_size;
17039 return set_die_type (die, type, cu);
17040 }
17041
17042 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17043 the user defined type vector. */
17044
17045 static struct type *
17046 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17047 {
17048 struct type *type;
17049 struct type *to_type;
17050 struct type *domain;
17051
17052 to_type = die_type (die, cu);
17053 domain = die_containing_type (die, cu);
17054
17055 /* The calls above may have already set the type for this DIE. */
17056 type = get_die_type (die, cu);
17057 if (type)
17058 return type;
17059
17060 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17061 type = lookup_methodptr_type (to_type);
17062 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17063 {
17064 struct type *new_type
17065 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17066
17067 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17068 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17069 TYPE_VARARGS (to_type));
17070 type = lookup_methodptr_type (new_type);
17071 }
17072 else
17073 type = lookup_memberptr_type (to_type, domain);
17074
17075 return set_die_type (die, type, cu);
17076 }
17077
17078 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17079 the user defined type vector. */
17080
17081 static struct type *
17082 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17083 enum type_code refcode)
17084 {
17085 struct comp_unit_head *cu_header = &cu->header;
17086 struct type *type, *target_type;
17087 struct attribute *attr;
17088
17089 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17090
17091 target_type = die_type (die, cu);
17092
17093 /* The die_type call above may have already set the type for this DIE. */
17094 type = get_die_type (die, cu);
17095 if (type)
17096 return type;
17097
17098 type = lookup_reference_type (target_type, refcode);
17099 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17100 if (attr)
17101 {
17102 TYPE_LENGTH (type) = DW_UNSND (attr);
17103 }
17104 else
17105 {
17106 TYPE_LENGTH (type) = cu_header->addr_size;
17107 }
17108 return set_die_type (die, type, cu);
17109 }
17110
17111 /* Add the given cv-qualifiers to the element type of the array. GCC
17112 outputs DWARF type qualifiers that apply to an array, not the
17113 element type. But GDB relies on the array element type to carry
17114 the cv-qualifiers. This mimics section 6.7.3 of the C99
17115 specification. */
17116
17117 static struct type *
17118 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17119 struct type *base_type, int cnst, int voltl)
17120 {
17121 struct type *el_type, *inner_array;
17122
17123 base_type = copy_type (base_type);
17124 inner_array = base_type;
17125
17126 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17127 {
17128 TYPE_TARGET_TYPE (inner_array) =
17129 copy_type (TYPE_TARGET_TYPE (inner_array));
17130 inner_array = TYPE_TARGET_TYPE (inner_array);
17131 }
17132
17133 el_type = TYPE_TARGET_TYPE (inner_array);
17134 cnst |= TYPE_CONST (el_type);
17135 voltl |= TYPE_VOLATILE (el_type);
17136 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17137
17138 return set_die_type (die, base_type, cu);
17139 }
17140
17141 static struct type *
17142 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17143 {
17144 struct type *base_type, *cv_type;
17145
17146 base_type = die_type (die, cu);
17147
17148 /* The die_type call above may have already set the type for this DIE. */
17149 cv_type = get_die_type (die, cu);
17150 if (cv_type)
17151 return cv_type;
17152
17153 /* In case the const qualifier is applied to an array type, the element type
17154 is so qualified, not the array type (section 6.7.3 of C99). */
17155 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17156 return add_array_cv_type (die, cu, base_type, 1, 0);
17157
17158 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17159 return set_die_type (die, cv_type, cu);
17160 }
17161
17162 static struct type *
17163 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17164 {
17165 struct type *base_type, *cv_type;
17166
17167 base_type = die_type (die, cu);
17168
17169 /* The die_type call above may have already set the type for this DIE. */
17170 cv_type = get_die_type (die, cu);
17171 if (cv_type)
17172 return cv_type;
17173
17174 /* In case the volatile qualifier is applied to an array type, the
17175 element type is so qualified, not the array type (section 6.7.3
17176 of C99). */
17177 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17178 return add_array_cv_type (die, cu, base_type, 0, 1);
17179
17180 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17181 return set_die_type (die, cv_type, cu);
17182 }
17183
17184 /* Handle DW_TAG_restrict_type. */
17185
17186 static struct type *
17187 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17188 {
17189 struct type *base_type, *cv_type;
17190
17191 base_type = die_type (die, cu);
17192
17193 /* The die_type call above may have already set the type for this DIE. */
17194 cv_type = get_die_type (die, cu);
17195 if (cv_type)
17196 return cv_type;
17197
17198 cv_type = make_restrict_type (base_type);
17199 return set_die_type (die, cv_type, cu);
17200 }
17201
17202 /* Handle DW_TAG_atomic_type. */
17203
17204 static struct type *
17205 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17206 {
17207 struct type *base_type, *cv_type;
17208
17209 base_type = die_type (die, cu);
17210
17211 /* The die_type call above may have already set the type for this DIE. */
17212 cv_type = get_die_type (die, cu);
17213 if (cv_type)
17214 return cv_type;
17215
17216 cv_type = make_atomic_type (base_type);
17217 return set_die_type (die, cv_type, cu);
17218 }
17219
17220 /* Extract all information from a DW_TAG_string_type DIE and add to
17221 the user defined type vector. It isn't really a user defined type,
17222 but it behaves like one, with other DIE's using an AT_user_def_type
17223 attribute to reference it. */
17224
17225 static struct type *
17226 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17227 {
17228 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17229 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17230 struct type *type, *range_type, *index_type, *char_type;
17231 struct attribute *attr;
17232 unsigned int length;
17233
17234 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17235 if (attr)
17236 {
17237 length = DW_UNSND (attr);
17238 }
17239 else
17240 {
17241 /* Check for the DW_AT_byte_size attribute. */
17242 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17243 if (attr)
17244 {
17245 length = DW_UNSND (attr);
17246 }
17247 else
17248 {
17249 length = 1;
17250 }
17251 }
17252
17253 index_type = objfile_type (objfile)->builtin_int;
17254 range_type = create_static_range_type (NULL, index_type, 1, length);
17255 char_type = language_string_char_type (cu->language_defn, gdbarch);
17256 type = create_string_type (NULL, char_type, range_type);
17257
17258 return set_die_type (die, type, cu);
17259 }
17260
17261 /* Assuming that DIE corresponds to a function, returns nonzero
17262 if the function is prototyped. */
17263
17264 static int
17265 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17266 {
17267 struct attribute *attr;
17268
17269 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17270 if (attr && (DW_UNSND (attr) != 0))
17271 return 1;
17272
17273 /* The DWARF standard implies that the DW_AT_prototyped attribute
17274 is only meaninful for C, but the concept also extends to other
17275 languages that allow unprototyped functions (Eg: Objective C).
17276 For all other languages, assume that functions are always
17277 prototyped. */
17278 if (cu->language != language_c
17279 && cu->language != language_objc
17280 && cu->language != language_opencl)
17281 return 1;
17282
17283 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17284 prototyped and unprototyped functions; default to prototyped,
17285 since that is more common in modern code (and RealView warns
17286 about unprototyped functions). */
17287 if (producer_is_realview (cu->producer))
17288 return 1;
17289
17290 return 0;
17291 }
17292
17293 /* Handle DIES due to C code like:
17294
17295 struct foo
17296 {
17297 int (*funcp)(int a, long l);
17298 int b;
17299 };
17300
17301 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17302
17303 static struct type *
17304 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17305 {
17306 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17307 struct type *type; /* Type that this function returns. */
17308 struct type *ftype; /* Function that returns above type. */
17309 struct attribute *attr;
17310
17311 type = die_type (die, cu);
17312
17313 /* The die_type call above may have already set the type for this DIE. */
17314 ftype = get_die_type (die, cu);
17315 if (ftype)
17316 return ftype;
17317
17318 ftype = lookup_function_type (type);
17319
17320 if (prototyped_function_p (die, cu))
17321 TYPE_PROTOTYPED (ftype) = 1;
17322
17323 /* Store the calling convention in the type if it's available in
17324 the subroutine die. Otherwise set the calling convention to
17325 the default value DW_CC_normal. */
17326 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17327 if (attr)
17328 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17329 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17330 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17331 else
17332 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17333
17334 /* Record whether the function returns normally to its caller or not
17335 if the DWARF producer set that information. */
17336 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17337 if (attr && (DW_UNSND (attr) != 0))
17338 TYPE_NO_RETURN (ftype) = 1;
17339
17340 /* We need to add the subroutine type to the die immediately so
17341 we don't infinitely recurse when dealing with parameters
17342 declared as the same subroutine type. */
17343 set_die_type (die, ftype, cu);
17344
17345 if (die->child != NULL)
17346 {
17347 struct type *void_type = objfile_type (objfile)->builtin_void;
17348 struct die_info *child_die;
17349 int nparams, iparams;
17350
17351 /* Count the number of parameters.
17352 FIXME: GDB currently ignores vararg functions, but knows about
17353 vararg member functions. */
17354 nparams = 0;
17355 child_die = die->child;
17356 while (child_die && child_die->tag)
17357 {
17358 if (child_die->tag == DW_TAG_formal_parameter)
17359 nparams++;
17360 else if (child_die->tag == DW_TAG_unspecified_parameters)
17361 TYPE_VARARGS (ftype) = 1;
17362 child_die = sibling_die (child_die);
17363 }
17364
17365 /* Allocate storage for parameters and fill them in. */
17366 TYPE_NFIELDS (ftype) = nparams;
17367 TYPE_FIELDS (ftype) = (struct field *)
17368 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17369
17370 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17371 even if we error out during the parameters reading below. */
17372 for (iparams = 0; iparams < nparams; iparams++)
17373 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17374
17375 iparams = 0;
17376 child_die = die->child;
17377 while (child_die && child_die->tag)
17378 {
17379 if (child_die->tag == DW_TAG_formal_parameter)
17380 {
17381 struct type *arg_type;
17382
17383 /* DWARF version 2 has no clean way to discern C++
17384 static and non-static member functions. G++ helps
17385 GDB by marking the first parameter for non-static
17386 member functions (which is the this pointer) as
17387 artificial. We pass this information to
17388 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17389
17390 DWARF version 3 added DW_AT_object_pointer, which GCC
17391 4.5 does not yet generate. */
17392 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17393 if (attr)
17394 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17395 else
17396 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17397 arg_type = die_type (child_die, cu);
17398
17399 /* RealView does not mark THIS as const, which the testsuite
17400 expects. GCC marks THIS as const in method definitions,
17401 but not in the class specifications (GCC PR 43053). */
17402 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17403 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17404 {
17405 int is_this = 0;
17406 struct dwarf2_cu *arg_cu = cu;
17407 const char *name = dwarf2_name (child_die, cu);
17408
17409 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17410 if (attr)
17411 {
17412 /* If the compiler emits this, use it. */
17413 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17414 is_this = 1;
17415 }
17416 else if (name && strcmp (name, "this") == 0)
17417 /* Function definitions will have the argument names. */
17418 is_this = 1;
17419 else if (name == NULL && iparams == 0)
17420 /* Declarations may not have the names, so like
17421 elsewhere in GDB, assume an artificial first
17422 argument is "this". */
17423 is_this = 1;
17424
17425 if (is_this)
17426 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17427 arg_type, 0);
17428 }
17429
17430 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17431 iparams++;
17432 }
17433 child_die = sibling_die (child_die);
17434 }
17435 }
17436
17437 return ftype;
17438 }
17439
17440 static struct type *
17441 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17442 {
17443 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17444 const char *name = NULL;
17445 struct type *this_type, *target_type;
17446
17447 name = dwarf2_full_name (NULL, die, cu);
17448 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17449 TYPE_TARGET_STUB (this_type) = 1;
17450 set_die_type (die, this_type, cu);
17451 target_type = die_type (die, cu);
17452 if (target_type != this_type)
17453 TYPE_TARGET_TYPE (this_type) = target_type;
17454 else
17455 {
17456 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17457 spec and cause infinite loops in GDB. */
17458 complaint (&symfile_complaints,
17459 _("Self-referential DW_TAG_typedef "
17460 "- DIE at %s [in module %s]"),
17461 sect_offset_str (die->sect_off), objfile_name (objfile));
17462 TYPE_TARGET_TYPE (this_type) = NULL;
17463 }
17464 return this_type;
17465 }
17466
17467 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17468 (which may be different from NAME) to the architecture back-end to allow
17469 it to guess the correct format if necessary. */
17470
17471 static struct type *
17472 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17473 const char *name_hint)
17474 {
17475 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17476 const struct floatformat **format;
17477 struct type *type;
17478
17479 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17480 if (format)
17481 type = init_float_type (objfile, bits, name, format);
17482 else
17483 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17484
17485 return type;
17486 }
17487
17488 /* Find a representation of a given base type and install
17489 it in the TYPE field of the die. */
17490
17491 static struct type *
17492 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17493 {
17494 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17495 struct type *type;
17496 struct attribute *attr;
17497 int encoding = 0, bits = 0;
17498 const char *name;
17499
17500 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17501 if (attr)
17502 {
17503 encoding = DW_UNSND (attr);
17504 }
17505 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17506 if (attr)
17507 {
17508 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17509 }
17510 name = dwarf2_name (die, cu);
17511 if (!name)
17512 {
17513 complaint (&symfile_complaints,
17514 _("DW_AT_name missing from DW_TAG_base_type"));
17515 }
17516
17517 switch (encoding)
17518 {
17519 case DW_ATE_address:
17520 /* Turn DW_ATE_address into a void * pointer. */
17521 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17522 type = init_pointer_type (objfile, bits, name, type);
17523 break;
17524 case DW_ATE_boolean:
17525 type = init_boolean_type (objfile, bits, 1, name);
17526 break;
17527 case DW_ATE_complex_float:
17528 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17529 type = init_complex_type (objfile, name, type);
17530 break;
17531 case DW_ATE_decimal_float:
17532 type = init_decfloat_type (objfile, bits, name);
17533 break;
17534 case DW_ATE_float:
17535 type = dwarf2_init_float_type (objfile, bits, name, name);
17536 break;
17537 case DW_ATE_signed:
17538 type = init_integer_type (objfile, bits, 0, name);
17539 break;
17540 case DW_ATE_unsigned:
17541 if (cu->language == language_fortran
17542 && name
17543 && startswith (name, "character("))
17544 type = init_character_type (objfile, bits, 1, name);
17545 else
17546 type = init_integer_type (objfile, bits, 1, name);
17547 break;
17548 case DW_ATE_signed_char:
17549 if (cu->language == language_ada || cu->language == language_m2
17550 || cu->language == language_pascal
17551 || cu->language == language_fortran)
17552 type = init_character_type (objfile, bits, 0, name);
17553 else
17554 type = init_integer_type (objfile, bits, 0, name);
17555 break;
17556 case DW_ATE_unsigned_char:
17557 if (cu->language == language_ada || cu->language == language_m2
17558 || cu->language == language_pascal
17559 || cu->language == language_fortran
17560 || cu->language == language_rust)
17561 type = init_character_type (objfile, bits, 1, name);
17562 else
17563 type = init_integer_type (objfile, bits, 1, name);
17564 break;
17565 case DW_ATE_UTF:
17566 {
17567 gdbarch *arch = get_objfile_arch (objfile);
17568
17569 if (bits == 16)
17570 type = builtin_type (arch)->builtin_char16;
17571 else if (bits == 32)
17572 type = builtin_type (arch)->builtin_char32;
17573 else
17574 {
17575 complaint (&symfile_complaints,
17576 _("unsupported DW_ATE_UTF bit size: '%d'"),
17577 bits);
17578 type = init_integer_type (objfile, bits, 1, name);
17579 }
17580 return set_die_type (die, type, cu);
17581 }
17582 break;
17583
17584 default:
17585 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17586 dwarf_type_encoding_name (encoding));
17587 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17588 break;
17589 }
17590
17591 if (name && strcmp (name, "char") == 0)
17592 TYPE_NOSIGN (type) = 1;
17593
17594 return set_die_type (die, type, cu);
17595 }
17596
17597 /* Parse dwarf attribute if it's a block, reference or constant and put the
17598 resulting value of the attribute into struct bound_prop.
17599 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17600
17601 static int
17602 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17603 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17604 {
17605 struct dwarf2_property_baton *baton;
17606 struct obstack *obstack
17607 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17608
17609 if (attr == NULL || prop == NULL)
17610 return 0;
17611
17612 if (attr_form_is_block (attr))
17613 {
17614 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17615 baton->referenced_type = NULL;
17616 baton->locexpr.per_cu = cu->per_cu;
17617 baton->locexpr.size = DW_BLOCK (attr)->size;
17618 baton->locexpr.data = DW_BLOCK (attr)->data;
17619 prop->data.baton = baton;
17620 prop->kind = PROP_LOCEXPR;
17621 gdb_assert (prop->data.baton != NULL);
17622 }
17623 else if (attr_form_is_ref (attr))
17624 {
17625 struct dwarf2_cu *target_cu = cu;
17626 struct die_info *target_die;
17627 struct attribute *target_attr;
17628
17629 target_die = follow_die_ref (die, attr, &target_cu);
17630 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17631 if (target_attr == NULL)
17632 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17633 target_cu);
17634 if (target_attr == NULL)
17635 return 0;
17636
17637 switch (target_attr->name)
17638 {
17639 case DW_AT_location:
17640 if (attr_form_is_section_offset (target_attr))
17641 {
17642 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17643 baton->referenced_type = die_type (target_die, target_cu);
17644 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17645 prop->data.baton = baton;
17646 prop->kind = PROP_LOCLIST;
17647 gdb_assert (prop->data.baton != NULL);
17648 }
17649 else if (attr_form_is_block (target_attr))
17650 {
17651 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17652 baton->referenced_type = die_type (target_die, target_cu);
17653 baton->locexpr.per_cu = cu->per_cu;
17654 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17655 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17656 prop->data.baton = baton;
17657 prop->kind = PROP_LOCEXPR;
17658 gdb_assert (prop->data.baton != NULL);
17659 }
17660 else
17661 {
17662 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17663 "dynamic property");
17664 return 0;
17665 }
17666 break;
17667 case DW_AT_data_member_location:
17668 {
17669 LONGEST offset;
17670
17671 if (!handle_data_member_location (target_die, target_cu,
17672 &offset))
17673 return 0;
17674
17675 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17676 baton->referenced_type = read_type_die (target_die->parent,
17677 target_cu);
17678 baton->offset_info.offset = offset;
17679 baton->offset_info.type = die_type (target_die, target_cu);
17680 prop->data.baton = baton;
17681 prop->kind = PROP_ADDR_OFFSET;
17682 break;
17683 }
17684 }
17685 }
17686 else if (attr_form_is_constant (attr))
17687 {
17688 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17689 prop->kind = PROP_CONST;
17690 }
17691 else
17692 {
17693 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17694 dwarf2_name (die, cu));
17695 return 0;
17696 }
17697
17698 return 1;
17699 }
17700
17701 /* Read the given DW_AT_subrange DIE. */
17702
17703 static struct type *
17704 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17705 {
17706 struct type *base_type, *orig_base_type;
17707 struct type *range_type;
17708 struct attribute *attr;
17709 struct dynamic_prop low, high;
17710 int low_default_is_valid;
17711 int high_bound_is_count = 0;
17712 const char *name;
17713 LONGEST negative_mask;
17714
17715 orig_base_type = die_type (die, cu);
17716 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17717 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17718 creating the range type, but we use the result of check_typedef
17719 when examining properties of the type. */
17720 base_type = check_typedef (orig_base_type);
17721
17722 /* The die_type call above may have already set the type for this DIE. */
17723 range_type = get_die_type (die, cu);
17724 if (range_type)
17725 return range_type;
17726
17727 low.kind = PROP_CONST;
17728 high.kind = PROP_CONST;
17729 high.data.const_val = 0;
17730
17731 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17732 omitting DW_AT_lower_bound. */
17733 switch (cu->language)
17734 {
17735 case language_c:
17736 case language_cplus:
17737 low.data.const_val = 0;
17738 low_default_is_valid = 1;
17739 break;
17740 case language_fortran:
17741 low.data.const_val = 1;
17742 low_default_is_valid = 1;
17743 break;
17744 case language_d:
17745 case language_objc:
17746 case language_rust:
17747 low.data.const_val = 0;
17748 low_default_is_valid = (cu->header.version >= 4);
17749 break;
17750 case language_ada:
17751 case language_m2:
17752 case language_pascal:
17753 low.data.const_val = 1;
17754 low_default_is_valid = (cu->header.version >= 4);
17755 break;
17756 default:
17757 low.data.const_val = 0;
17758 low_default_is_valid = 0;
17759 break;
17760 }
17761
17762 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17763 if (attr)
17764 attr_to_dynamic_prop (attr, die, cu, &low);
17765 else if (!low_default_is_valid)
17766 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17767 "- DIE at %s [in module %s]"),
17768 sect_offset_str (die->sect_off),
17769 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17770
17771 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
17772 if (!attr_to_dynamic_prop (attr, die, cu, &high))
17773 {
17774 attr = dwarf2_attr (die, DW_AT_count, cu);
17775 if (attr_to_dynamic_prop (attr, die, cu, &high))
17776 {
17777 /* If bounds are constant do the final calculation here. */
17778 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17779 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17780 else
17781 high_bound_is_count = 1;
17782 }
17783 }
17784
17785 /* Dwarf-2 specifications explicitly allows to create subrange types
17786 without specifying a base type.
17787 In that case, the base type must be set to the type of
17788 the lower bound, upper bound or count, in that order, if any of these
17789 three attributes references an object that has a type.
17790 If no base type is found, the Dwarf-2 specifications say that
17791 a signed integer type of size equal to the size of an address should
17792 be used.
17793 For the following C code: `extern char gdb_int [];'
17794 GCC produces an empty range DIE.
17795 FIXME: muller/2010-05-28: Possible references to object for low bound,
17796 high bound or count are not yet handled by this code. */
17797 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17798 {
17799 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17800 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17801 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17802 struct type *int_type = objfile_type (objfile)->builtin_int;
17803
17804 /* Test "int", "long int", and "long long int" objfile types,
17805 and select the first one having a size above or equal to the
17806 architecture address size. */
17807 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17808 base_type = int_type;
17809 else
17810 {
17811 int_type = objfile_type (objfile)->builtin_long;
17812 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17813 base_type = int_type;
17814 else
17815 {
17816 int_type = objfile_type (objfile)->builtin_long_long;
17817 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17818 base_type = int_type;
17819 }
17820 }
17821 }
17822
17823 /* Normally, the DWARF producers are expected to use a signed
17824 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17825 But this is unfortunately not always the case, as witnessed
17826 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17827 is used instead. To work around that ambiguity, we treat
17828 the bounds as signed, and thus sign-extend their values, when
17829 the base type is signed. */
17830 negative_mask =
17831 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17832 if (low.kind == PROP_CONST
17833 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17834 low.data.const_val |= negative_mask;
17835 if (high.kind == PROP_CONST
17836 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17837 high.data.const_val |= negative_mask;
17838
17839 range_type = create_range_type (NULL, orig_base_type, &low, &high);
17840
17841 if (high_bound_is_count)
17842 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17843
17844 /* Ada expects an empty array on no boundary attributes. */
17845 if (attr == NULL && cu->language != language_ada)
17846 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17847
17848 name = dwarf2_name (die, cu);
17849 if (name)
17850 TYPE_NAME (range_type) = name;
17851
17852 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17853 if (attr)
17854 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17855
17856 set_die_type (die, range_type, cu);
17857
17858 /* set_die_type should be already done. */
17859 set_descriptive_type (range_type, die, cu);
17860
17861 return range_type;
17862 }
17863
17864 static struct type *
17865 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17866 {
17867 struct type *type;
17868
17869 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17870 NULL);
17871 TYPE_NAME (type) = dwarf2_name (die, cu);
17872
17873 /* In Ada, an unspecified type is typically used when the description
17874 of the type is defered to a different unit. When encountering
17875 such a type, we treat it as a stub, and try to resolve it later on,
17876 when needed. */
17877 if (cu->language == language_ada)
17878 TYPE_STUB (type) = 1;
17879
17880 return set_die_type (die, type, cu);
17881 }
17882
17883 /* Read a single die and all its descendents. Set the die's sibling
17884 field to NULL; set other fields in the die correctly, and set all
17885 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17886 location of the info_ptr after reading all of those dies. PARENT
17887 is the parent of the die in question. */
17888
17889 static struct die_info *
17890 read_die_and_children (const struct die_reader_specs *reader,
17891 const gdb_byte *info_ptr,
17892 const gdb_byte **new_info_ptr,
17893 struct die_info *parent)
17894 {
17895 struct die_info *die;
17896 const gdb_byte *cur_ptr;
17897 int has_children;
17898
17899 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17900 if (die == NULL)
17901 {
17902 *new_info_ptr = cur_ptr;
17903 return NULL;
17904 }
17905 store_in_ref_table (die, reader->cu);
17906
17907 if (has_children)
17908 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17909 else
17910 {
17911 die->child = NULL;
17912 *new_info_ptr = cur_ptr;
17913 }
17914
17915 die->sibling = NULL;
17916 die->parent = parent;
17917 return die;
17918 }
17919
17920 /* Read a die, all of its descendents, and all of its siblings; set
17921 all of the fields of all of the dies correctly. Arguments are as
17922 in read_die_and_children. */
17923
17924 static struct die_info *
17925 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17926 const gdb_byte *info_ptr,
17927 const gdb_byte **new_info_ptr,
17928 struct die_info *parent)
17929 {
17930 struct die_info *first_die, *last_sibling;
17931 const gdb_byte *cur_ptr;
17932
17933 cur_ptr = info_ptr;
17934 first_die = last_sibling = NULL;
17935
17936 while (1)
17937 {
17938 struct die_info *die
17939 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17940
17941 if (die == NULL)
17942 {
17943 *new_info_ptr = cur_ptr;
17944 return first_die;
17945 }
17946
17947 if (!first_die)
17948 first_die = die;
17949 else
17950 last_sibling->sibling = die;
17951
17952 last_sibling = die;
17953 }
17954 }
17955
17956 /* Read a die, all of its descendents, and all of its siblings; set
17957 all of the fields of all of the dies correctly. Arguments are as
17958 in read_die_and_children.
17959 This the main entry point for reading a DIE and all its children. */
17960
17961 static struct die_info *
17962 read_die_and_siblings (const struct die_reader_specs *reader,
17963 const gdb_byte *info_ptr,
17964 const gdb_byte **new_info_ptr,
17965 struct die_info *parent)
17966 {
17967 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17968 new_info_ptr, parent);
17969
17970 if (dwarf_die_debug)
17971 {
17972 fprintf_unfiltered (gdb_stdlog,
17973 "Read die from %s@0x%x of %s:\n",
17974 get_section_name (reader->die_section),
17975 (unsigned) (info_ptr - reader->die_section->buffer),
17976 bfd_get_filename (reader->abfd));
17977 dump_die (die, dwarf_die_debug);
17978 }
17979
17980 return die;
17981 }
17982
17983 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17984 attributes.
17985 The caller is responsible for filling in the extra attributes
17986 and updating (*DIEP)->num_attrs.
17987 Set DIEP to point to a newly allocated die with its information,
17988 except for its child, sibling, and parent fields.
17989 Set HAS_CHILDREN to tell whether the die has children or not. */
17990
17991 static const gdb_byte *
17992 read_full_die_1 (const struct die_reader_specs *reader,
17993 struct die_info **diep, const gdb_byte *info_ptr,
17994 int *has_children, int num_extra_attrs)
17995 {
17996 unsigned int abbrev_number, bytes_read, i;
17997 struct abbrev_info *abbrev;
17998 struct die_info *die;
17999 struct dwarf2_cu *cu = reader->cu;
18000 bfd *abfd = reader->abfd;
18001
18002 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18003 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18004 info_ptr += bytes_read;
18005 if (!abbrev_number)
18006 {
18007 *diep = NULL;
18008 *has_children = 0;
18009 return info_ptr;
18010 }
18011
18012 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18013 if (!abbrev)
18014 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18015 abbrev_number,
18016 bfd_get_filename (abfd));
18017
18018 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18019 die->sect_off = sect_off;
18020 die->tag = abbrev->tag;
18021 die->abbrev = abbrev_number;
18022
18023 /* Make the result usable.
18024 The caller needs to update num_attrs after adding the extra
18025 attributes. */
18026 die->num_attrs = abbrev->num_attrs;
18027
18028 for (i = 0; i < abbrev->num_attrs; ++i)
18029 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18030 info_ptr);
18031
18032 *diep = die;
18033 *has_children = abbrev->has_children;
18034 return info_ptr;
18035 }
18036
18037 /* Read a die and all its attributes.
18038 Set DIEP to point to a newly allocated die with its information,
18039 except for its child, sibling, and parent fields.
18040 Set HAS_CHILDREN to tell whether the die has children or not. */
18041
18042 static const gdb_byte *
18043 read_full_die (const struct die_reader_specs *reader,
18044 struct die_info **diep, const gdb_byte *info_ptr,
18045 int *has_children)
18046 {
18047 const gdb_byte *result;
18048
18049 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18050
18051 if (dwarf_die_debug)
18052 {
18053 fprintf_unfiltered (gdb_stdlog,
18054 "Read die from %s@0x%x of %s:\n",
18055 get_section_name (reader->die_section),
18056 (unsigned) (info_ptr - reader->die_section->buffer),
18057 bfd_get_filename (reader->abfd));
18058 dump_die (*diep, dwarf_die_debug);
18059 }
18060
18061 return result;
18062 }
18063 \f
18064 /* Abbreviation tables.
18065
18066 In DWARF version 2, the description of the debugging information is
18067 stored in a separate .debug_abbrev section. Before we read any
18068 dies from a section we read in all abbreviations and install them
18069 in a hash table. */
18070
18071 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18072
18073 struct abbrev_info *
18074 abbrev_table::alloc_abbrev ()
18075 {
18076 struct abbrev_info *abbrev;
18077
18078 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18079 memset (abbrev, 0, sizeof (struct abbrev_info));
18080
18081 return abbrev;
18082 }
18083
18084 /* Add an abbreviation to the table. */
18085
18086 void
18087 abbrev_table::add_abbrev (unsigned int abbrev_number,
18088 struct abbrev_info *abbrev)
18089 {
18090 unsigned int hash_number;
18091
18092 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18093 abbrev->next = m_abbrevs[hash_number];
18094 m_abbrevs[hash_number] = abbrev;
18095 }
18096
18097 /* Look up an abbrev in the table.
18098 Returns NULL if the abbrev is not found. */
18099
18100 struct abbrev_info *
18101 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18102 {
18103 unsigned int hash_number;
18104 struct abbrev_info *abbrev;
18105
18106 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18107 abbrev = m_abbrevs[hash_number];
18108
18109 while (abbrev)
18110 {
18111 if (abbrev->number == abbrev_number)
18112 return abbrev;
18113 abbrev = abbrev->next;
18114 }
18115 return NULL;
18116 }
18117
18118 /* Read in an abbrev table. */
18119
18120 static abbrev_table_up
18121 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18122 struct dwarf2_section_info *section,
18123 sect_offset sect_off)
18124 {
18125 struct objfile *objfile = dwarf2_per_objfile->objfile;
18126 bfd *abfd = get_section_bfd_owner (section);
18127 const gdb_byte *abbrev_ptr;
18128 struct abbrev_info *cur_abbrev;
18129 unsigned int abbrev_number, bytes_read, abbrev_name;
18130 unsigned int abbrev_form;
18131 struct attr_abbrev *cur_attrs;
18132 unsigned int allocated_attrs;
18133
18134 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18135
18136 dwarf2_read_section (objfile, section);
18137 abbrev_ptr = section->buffer + to_underlying (sect_off);
18138 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18139 abbrev_ptr += bytes_read;
18140
18141 allocated_attrs = ATTR_ALLOC_CHUNK;
18142 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18143
18144 /* Loop until we reach an abbrev number of 0. */
18145 while (abbrev_number)
18146 {
18147 cur_abbrev = abbrev_table->alloc_abbrev ();
18148
18149 /* read in abbrev header */
18150 cur_abbrev->number = abbrev_number;
18151 cur_abbrev->tag
18152 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18153 abbrev_ptr += bytes_read;
18154 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18155 abbrev_ptr += 1;
18156
18157 /* now read in declarations */
18158 for (;;)
18159 {
18160 LONGEST implicit_const;
18161
18162 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18163 abbrev_ptr += bytes_read;
18164 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18165 abbrev_ptr += bytes_read;
18166 if (abbrev_form == DW_FORM_implicit_const)
18167 {
18168 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18169 &bytes_read);
18170 abbrev_ptr += bytes_read;
18171 }
18172 else
18173 {
18174 /* Initialize it due to a false compiler warning. */
18175 implicit_const = -1;
18176 }
18177
18178 if (abbrev_name == 0)
18179 break;
18180
18181 if (cur_abbrev->num_attrs == allocated_attrs)
18182 {
18183 allocated_attrs += ATTR_ALLOC_CHUNK;
18184 cur_attrs
18185 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18186 }
18187
18188 cur_attrs[cur_abbrev->num_attrs].name
18189 = (enum dwarf_attribute) abbrev_name;
18190 cur_attrs[cur_abbrev->num_attrs].form
18191 = (enum dwarf_form) abbrev_form;
18192 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18193 ++cur_abbrev->num_attrs;
18194 }
18195
18196 cur_abbrev->attrs =
18197 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18198 cur_abbrev->num_attrs);
18199 memcpy (cur_abbrev->attrs, cur_attrs,
18200 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18201
18202 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18203
18204 /* Get next abbreviation.
18205 Under Irix6 the abbreviations for a compilation unit are not
18206 always properly terminated with an abbrev number of 0.
18207 Exit loop if we encounter an abbreviation which we have
18208 already read (which means we are about to read the abbreviations
18209 for the next compile unit) or if the end of the abbreviation
18210 table is reached. */
18211 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18212 break;
18213 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18214 abbrev_ptr += bytes_read;
18215 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18216 break;
18217 }
18218
18219 xfree (cur_attrs);
18220 return abbrev_table;
18221 }
18222
18223 /* Returns nonzero if TAG represents a type that we might generate a partial
18224 symbol for. */
18225
18226 static int
18227 is_type_tag_for_partial (int tag)
18228 {
18229 switch (tag)
18230 {
18231 #if 0
18232 /* Some types that would be reasonable to generate partial symbols for,
18233 that we don't at present. */
18234 case DW_TAG_array_type:
18235 case DW_TAG_file_type:
18236 case DW_TAG_ptr_to_member_type:
18237 case DW_TAG_set_type:
18238 case DW_TAG_string_type:
18239 case DW_TAG_subroutine_type:
18240 #endif
18241 case DW_TAG_base_type:
18242 case DW_TAG_class_type:
18243 case DW_TAG_interface_type:
18244 case DW_TAG_enumeration_type:
18245 case DW_TAG_structure_type:
18246 case DW_TAG_subrange_type:
18247 case DW_TAG_typedef:
18248 case DW_TAG_union_type:
18249 return 1;
18250 default:
18251 return 0;
18252 }
18253 }
18254
18255 /* Load all DIEs that are interesting for partial symbols into memory. */
18256
18257 static struct partial_die_info *
18258 load_partial_dies (const struct die_reader_specs *reader,
18259 const gdb_byte *info_ptr, int building_psymtab)
18260 {
18261 struct dwarf2_cu *cu = reader->cu;
18262 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18263 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18264 unsigned int bytes_read;
18265 unsigned int load_all = 0;
18266 int nesting_level = 1;
18267
18268 parent_die = NULL;
18269 last_die = NULL;
18270
18271 gdb_assert (cu->per_cu != NULL);
18272 if (cu->per_cu->load_all_dies)
18273 load_all = 1;
18274
18275 cu->partial_dies
18276 = htab_create_alloc_ex (cu->header.length / 12,
18277 partial_die_hash,
18278 partial_die_eq,
18279 NULL,
18280 &cu->comp_unit_obstack,
18281 hashtab_obstack_allocate,
18282 dummy_obstack_deallocate);
18283
18284 while (1)
18285 {
18286 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18287
18288 /* A NULL abbrev means the end of a series of children. */
18289 if (abbrev == NULL)
18290 {
18291 if (--nesting_level == 0)
18292 return first_die;
18293
18294 info_ptr += bytes_read;
18295 last_die = parent_die;
18296 parent_die = parent_die->die_parent;
18297 continue;
18298 }
18299
18300 /* Check for template arguments. We never save these; if
18301 they're seen, we just mark the parent, and go on our way. */
18302 if (parent_die != NULL
18303 && cu->language == language_cplus
18304 && (abbrev->tag == DW_TAG_template_type_param
18305 || abbrev->tag == DW_TAG_template_value_param))
18306 {
18307 parent_die->has_template_arguments = 1;
18308
18309 if (!load_all)
18310 {
18311 /* We don't need a partial DIE for the template argument. */
18312 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18313 continue;
18314 }
18315 }
18316
18317 /* We only recurse into c++ subprograms looking for template arguments.
18318 Skip their other children. */
18319 if (!load_all
18320 && cu->language == language_cplus
18321 && parent_die != NULL
18322 && parent_die->tag == DW_TAG_subprogram)
18323 {
18324 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18325 continue;
18326 }
18327
18328 /* Check whether this DIE is interesting enough to save. Normally
18329 we would not be interested in members here, but there may be
18330 later variables referencing them via DW_AT_specification (for
18331 static members). */
18332 if (!load_all
18333 && !is_type_tag_for_partial (abbrev->tag)
18334 && abbrev->tag != DW_TAG_constant
18335 && abbrev->tag != DW_TAG_enumerator
18336 && abbrev->tag != DW_TAG_subprogram
18337 && abbrev->tag != DW_TAG_inlined_subroutine
18338 && abbrev->tag != DW_TAG_lexical_block
18339 && abbrev->tag != DW_TAG_variable
18340 && abbrev->tag != DW_TAG_namespace
18341 && abbrev->tag != DW_TAG_module
18342 && abbrev->tag != DW_TAG_member
18343 && abbrev->tag != DW_TAG_imported_unit
18344 && abbrev->tag != DW_TAG_imported_declaration)
18345 {
18346 /* Otherwise we skip to the next sibling, if any. */
18347 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18348 continue;
18349 }
18350
18351 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18352 abbrev);
18353
18354 info_ptr = read_partial_die (reader, &pdi, *abbrev, bytes_read,
18355 info_ptr);
18356
18357 /* This two-pass algorithm for processing partial symbols has a
18358 high cost in cache pressure. Thus, handle some simple cases
18359 here which cover the majority of C partial symbols. DIEs
18360 which neither have specification tags in them, nor could have
18361 specification tags elsewhere pointing at them, can simply be
18362 processed and discarded.
18363
18364 This segment is also optional; scan_partial_symbols and
18365 add_partial_symbol will handle these DIEs if we chain
18366 them in normally. When compilers which do not emit large
18367 quantities of duplicate debug information are more common,
18368 this code can probably be removed. */
18369
18370 /* Any complete simple types at the top level (pretty much all
18371 of them, for a language without namespaces), can be processed
18372 directly. */
18373 if (parent_die == NULL
18374 && pdi.has_specification == 0
18375 && pdi.is_declaration == 0
18376 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18377 || pdi.tag == DW_TAG_base_type
18378 || pdi.tag == DW_TAG_subrange_type))
18379 {
18380 if (building_psymtab && pdi.name != NULL)
18381 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18382 VAR_DOMAIN, LOC_TYPEDEF,
18383 &objfile->static_psymbols,
18384 0, cu->language, objfile);
18385 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18386 continue;
18387 }
18388
18389 /* The exception for DW_TAG_typedef with has_children above is
18390 a workaround of GCC PR debug/47510. In the case of this complaint
18391 type_name_no_tag_or_error will error on such types later.
18392
18393 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18394 it could not find the child DIEs referenced later, this is checked
18395 above. In correct DWARF DW_TAG_typedef should have no children. */
18396
18397 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18398 complaint (&symfile_complaints,
18399 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18400 "- DIE at %s [in module %s]"),
18401 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18402
18403 /* If we're at the second level, and we're an enumerator, and
18404 our parent has no specification (meaning possibly lives in a
18405 namespace elsewhere), then we can add the partial symbol now
18406 instead of queueing it. */
18407 if (pdi.tag == DW_TAG_enumerator
18408 && parent_die != NULL
18409 && parent_die->die_parent == NULL
18410 && parent_die->tag == DW_TAG_enumeration_type
18411 && parent_die->has_specification == 0)
18412 {
18413 if (pdi.name == NULL)
18414 complaint (&symfile_complaints,
18415 _("malformed enumerator DIE ignored"));
18416 else if (building_psymtab)
18417 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18418 VAR_DOMAIN, LOC_CONST,
18419 cu->language == language_cplus
18420 ? &objfile->global_psymbols
18421 : &objfile->static_psymbols,
18422 0, cu->language, objfile);
18423
18424 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18425 continue;
18426 }
18427
18428 struct partial_die_info *part_die
18429 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18430
18431 /* We'll save this DIE so link it in. */
18432 part_die->die_parent = parent_die;
18433 part_die->die_sibling = NULL;
18434 part_die->die_child = NULL;
18435
18436 if (last_die && last_die == parent_die)
18437 last_die->die_child = part_die;
18438 else if (last_die)
18439 last_die->die_sibling = part_die;
18440
18441 last_die = part_die;
18442
18443 if (first_die == NULL)
18444 first_die = part_die;
18445
18446 /* Maybe add the DIE to the hash table. Not all DIEs that we
18447 find interesting need to be in the hash table, because we
18448 also have the parent/sibling/child chains; only those that we
18449 might refer to by offset later during partial symbol reading.
18450
18451 For now this means things that might have be the target of a
18452 DW_AT_specification, DW_AT_abstract_origin, or
18453 DW_AT_extension. DW_AT_extension will refer only to
18454 namespaces; DW_AT_abstract_origin refers to functions (and
18455 many things under the function DIE, but we do not recurse
18456 into function DIEs during partial symbol reading) and
18457 possibly variables as well; DW_AT_specification refers to
18458 declarations. Declarations ought to have the DW_AT_declaration
18459 flag. It happens that GCC forgets to put it in sometimes, but
18460 only for functions, not for types.
18461
18462 Adding more things than necessary to the hash table is harmless
18463 except for the performance cost. Adding too few will result in
18464 wasted time in find_partial_die, when we reread the compilation
18465 unit with load_all_dies set. */
18466
18467 if (load_all
18468 || abbrev->tag == DW_TAG_constant
18469 || abbrev->tag == DW_TAG_subprogram
18470 || abbrev->tag == DW_TAG_variable
18471 || abbrev->tag == DW_TAG_namespace
18472 || part_die->is_declaration)
18473 {
18474 void **slot;
18475
18476 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18477 to_underlying (part_die->sect_off),
18478 INSERT);
18479 *slot = part_die;
18480 }
18481
18482 /* For some DIEs we want to follow their children (if any). For C
18483 we have no reason to follow the children of structures; for other
18484 languages we have to, so that we can get at method physnames
18485 to infer fully qualified class names, for DW_AT_specification,
18486 and for C++ template arguments. For C++, we also look one level
18487 inside functions to find template arguments (if the name of the
18488 function does not already contain the template arguments).
18489
18490 For Ada, we need to scan the children of subprograms and lexical
18491 blocks as well because Ada allows the definition of nested
18492 entities that could be interesting for the debugger, such as
18493 nested subprograms for instance. */
18494 if (last_die->has_children
18495 && (load_all
18496 || last_die->tag == DW_TAG_namespace
18497 || last_die->tag == DW_TAG_module
18498 || last_die->tag == DW_TAG_enumeration_type
18499 || (cu->language == language_cplus
18500 && last_die->tag == DW_TAG_subprogram
18501 && (last_die->name == NULL
18502 || strchr (last_die->name, '<') == NULL))
18503 || (cu->language != language_c
18504 && (last_die->tag == DW_TAG_class_type
18505 || last_die->tag == DW_TAG_interface_type
18506 || last_die->tag == DW_TAG_structure_type
18507 || last_die->tag == DW_TAG_union_type))
18508 || (cu->language == language_ada
18509 && (last_die->tag == DW_TAG_subprogram
18510 || last_die->tag == DW_TAG_lexical_block))))
18511 {
18512 nesting_level++;
18513 parent_die = last_die;
18514 continue;
18515 }
18516
18517 /* Otherwise we skip to the next sibling, if any. */
18518 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18519
18520 /* Back to the top, do it again. */
18521 }
18522 }
18523
18524 partial_die_info::partial_die_info (sect_offset sect_off_,
18525 struct abbrev_info *abbrev)
18526 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18527 {
18528 }
18529
18530 /* Read a minimal amount of information into the minimal die structure. */
18531
18532 static const gdb_byte *
18533 read_partial_die (const struct die_reader_specs *reader,
18534 struct partial_die_info *part_die,
18535 const struct abbrev_info &abbrev, unsigned int abbrev_len,
18536 const gdb_byte *info_ptr)
18537 {
18538 struct dwarf2_cu *cu = reader->cu;
18539 struct dwarf2_per_objfile *dwarf2_per_objfile
18540 = cu->per_cu->dwarf2_per_objfile;
18541 struct objfile *objfile = dwarf2_per_objfile->objfile;
18542 const gdb_byte *buffer = reader->buffer;
18543 unsigned int i;
18544 struct attribute attr;
18545 int has_low_pc_attr = 0;
18546 int has_high_pc_attr = 0;
18547 int high_pc_relative = 0;
18548
18549 info_ptr += abbrev_len;
18550
18551 for (i = 0; i < abbrev.num_attrs; ++i)
18552 {
18553 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18554
18555 /* Store the data if it is of an attribute we want to keep in a
18556 partial symbol table. */
18557 switch (attr.name)
18558 {
18559 case DW_AT_name:
18560 switch (part_die->tag)
18561 {
18562 case DW_TAG_compile_unit:
18563 case DW_TAG_partial_unit:
18564 case DW_TAG_type_unit:
18565 /* Compilation units have a DW_AT_name that is a filename, not
18566 a source language identifier. */
18567 case DW_TAG_enumeration_type:
18568 case DW_TAG_enumerator:
18569 /* These tags always have simple identifiers already; no need
18570 to canonicalize them. */
18571 part_die->name = DW_STRING (&attr);
18572 break;
18573 default:
18574 part_die->name
18575 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18576 &objfile->per_bfd->storage_obstack);
18577 break;
18578 }
18579 break;
18580 case DW_AT_linkage_name:
18581 case DW_AT_MIPS_linkage_name:
18582 /* Note that both forms of linkage name might appear. We
18583 assume they will be the same, and we only store the last
18584 one we see. */
18585 if (cu->language == language_ada)
18586 part_die->name = DW_STRING (&attr);
18587 part_die->linkage_name = DW_STRING (&attr);
18588 break;
18589 case DW_AT_low_pc:
18590 has_low_pc_attr = 1;
18591 part_die->lowpc = attr_value_as_address (&attr);
18592 break;
18593 case DW_AT_high_pc:
18594 has_high_pc_attr = 1;
18595 part_die->highpc = attr_value_as_address (&attr);
18596 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18597 high_pc_relative = 1;
18598 break;
18599 case DW_AT_location:
18600 /* Support the .debug_loc offsets. */
18601 if (attr_form_is_block (&attr))
18602 {
18603 part_die->d.locdesc = DW_BLOCK (&attr);
18604 }
18605 else if (attr_form_is_section_offset (&attr))
18606 {
18607 dwarf2_complex_location_expr_complaint ();
18608 }
18609 else
18610 {
18611 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18612 "partial symbol information");
18613 }
18614 break;
18615 case DW_AT_external:
18616 part_die->is_external = DW_UNSND (&attr);
18617 break;
18618 case DW_AT_declaration:
18619 part_die->is_declaration = DW_UNSND (&attr);
18620 break;
18621 case DW_AT_type:
18622 part_die->has_type = 1;
18623 break;
18624 case DW_AT_abstract_origin:
18625 case DW_AT_specification:
18626 case DW_AT_extension:
18627 part_die->has_specification = 1;
18628 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
18629 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18630 || cu->per_cu->is_dwz);
18631 break;
18632 case DW_AT_sibling:
18633 /* Ignore absolute siblings, they might point outside of
18634 the current compile unit. */
18635 if (attr.form == DW_FORM_ref_addr)
18636 complaint (&symfile_complaints,
18637 _("ignoring absolute DW_AT_sibling"));
18638 else
18639 {
18640 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18641 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18642
18643 if (sibling_ptr < info_ptr)
18644 complaint (&symfile_complaints,
18645 _("DW_AT_sibling points backwards"));
18646 else if (sibling_ptr > reader->buffer_end)
18647 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18648 else
18649 part_die->sibling = sibling_ptr;
18650 }
18651 break;
18652 case DW_AT_byte_size:
18653 part_die->has_byte_size = 1;
18654 break;
18655 case DW_AT_const_value:
18656 part_die->has_const_value = 1;
18657 break;
18658 case DW_AT_calling_convention:
18659 /* DWARF doesn't provide a way to identify a program's source-level
18660 entry point. DW_AT_calling_convention attributes are only meant
18661 to describe functions' calling conventions.
18662
18663 However, because it's a necessary piece of information in
18664 Fortran, and before DWARF 4 DW_CC_program was the only
18665 piece of debugging information whose definition refers to
18666 a 'main program' at all, several compilers marked Fortran
18667 main programs with DW_CC_program --- even when those
18668 functions use the standard calling conventions.
18669
18670 Although DWARF now specifies a way to provide this
18671 information, we support this practice for backward
18672 compatibility. */
18673 if (DW_UNSND (&attr) == DW_CC_program
18674 && cu->language == language_fortran)
18675 part_die->main_subprogram = 1;
18676 break;
18677 case DW_AT_inline:
18678 if (DW_UNSND (&attr) == DW_INL_inlined
18679 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18680 part_die->may_be_inlined = 1;
18681 break;
18682
18683 case DW_AT_import:
18684 if (part_die->tag == DW_TAG_imported_unit)
18685 {
18686 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
18687 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18688 || cu->per_cu->is_dwz);
18689 }
18690 break;
18691
18692 case DW_AT_main_subprogram:
18693 part_die->main_subprogram = DW_UNSND (&attr);
18694 break;
18695
18696 default:
18697 break;
18698 }
18699 }
18700
18701 if (high_pc_relative)
18702 part_die->highpc += part_die->lowpc;
18703
18704 if (has_low_pc_attr && has_high_pc_attr)
18705 {
18706 /* When using the GNU linker, .gnu.linkonce. sections are used to
18707 eliminate duplicate copies of functions and vtables and such.
18708 The linker will arbitrarily choose one and discard the others.
18709 The AT_*_pc values for such functions refer to local labels in
18710 these sections. If the section from that file was discarded, the
18711 labels are not in the output, so the relocs get a value of 0.
18712 If this is a discarded function, mark the pc bounds as invalid,
18713 so that GDB will ignore it. */
18714 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18715 {
18716 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18717
18718 complaint (&symfile_complaints,
18719 _("DW_AT_low_pc %s is zero "
18720 "for DIE at %s [in module %s]"),
18721 paddress (gdbarch, part_die->lowpc),
18722 sect_offset_str (part_die->sect_off),
18723 objfile_name (objfile));
18724 }
18725 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18726 else if (part_die->lowpc >= part_die->highpc)
18727 {
18728 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18729
18730 complaint (&symfile_complaints,
18731 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18732 "for DIE at %s [in module %s]"),
18733 paddress (gdbarch, part_die->lowpc),
18734 paddress (gdbarch, part_die->highpc),
18735 sect_offset_str (part_die->sect_off),
18736 objfile_name (objfile));
18737 }
18738 else
18739 part_die->has_pc_info = 1;
18740 }
18741
18742 return info_ptr;
18743 }
18744
18745 /* Find a cached partial DIE at OFFSET in CU. */
18746
18747 struct partial_die_info *
18748 dwarf2_cu::find_partial_die (sect_offset sect_off)
18749 {
18750 struct partial_die_info *lookup_die = NULL;
18751 struct partial_die_info part_die (sect_off);
18752
18753 lookup_die = ((struct partial_die_info *)
18754 htab_find_with_hash (partial_dies, &part_die,
18755 to_underlying (sect_off)));
18756
18757 return lookup_die;
18758 }
18759
18760 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18761 except in the case of .debug_types DIEs which do not reference
18762 outside their CU (they do however referencing other types via
18763 DW_FORM_ref_sig8). */
18764
18765 static struct partial_die_info *
18766 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18767 {
18768 struct dwarf2_per_objfile *dwarf2_per_objfile
18769 = cu->per_cu->dwarf2_per_objfile;
18770 struct objfile *objfile = dwarf2_per_objfile->objfile;
18771 struct dwarf2_per_cu_data *per_cu = NULL;
18772 struct partial_die_info *pd = NULL;
18773
18774 if (offset_in_dwz == cu->per_cu->is_dwz
18775 && offset_in_cu_p (&cu->header, sect_off))
18776 {
18777 pd = cu->find_partial_die (sect_off);
18778 if (pd != NULL)
18779 return pd;
18780 /* We missed recording what we needed.
18781 Load all dies and try again. */
18782 per_cu = cu->per_cu;
18783 }
18784 else
18785 {
18786 /* TUs don't reference other CUs/TUs (except via type signatures). */
18787 if (cu->per_cu->is_debug_types)
18788 {
18789 error (_("Dwarf Error: Type Unit at offset %s contains"
18790 " external reference to offset %s [in module %s].\n"),
18791 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18792 bfd_get_filename (objfile->obfd));
18793 }
18794 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18795 dwarf2_per_objfile);
18796
18797 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18798 load_partial_comp_unit (per_cu);
18799
18800 per_cu->cu->last_used = 0;
18801 pd = per_cu->cu->find_partial_die (sect_off);
18802 }
18803
18804 /* If we didn't find it, and not all dies have been loaded,
18805 load them all and try again. */
18806
18807 if (pd == NULL && per_cu->load_all_dies == 0)
18808 {
18809 per_cu->load_all_dies = 1;
18810
18811 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18812 THIS_CU->cu may already be in use. So we can't just free it and
18813 replace its DIEs with the ones we read in. Instead, we leave those
18814 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18815 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18816 set. */
18817 load_partial_comp_unit (per_cu);
18818
18819 pd = per_cu->cu->find_partial_die (sect_off);
18820 }
18821
18822 if (pd == NULL)
18823 internal_error (__FILE__, __LINE__,
18824 _("could not find partial DIE %s "
18825 "in cache [from module %s]\n"),
18826 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18827 return pd;
18828 }
18829
18830 /* See if we can figure out if the class lives in a namespace. We do
18831 this by looking for a member function; its demangled name will
18832 contain namespace info, if there is any. */
18833
18834 static void
18835 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18836 struct dwarf2_cu *cu)
18837 {
18838 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18839 what template types look like, because the demangler
18840 frequently doesn't give the same name as the debug info. We
18841 could fix this by only using the demangled name to get the
18842 prefix (but see comment in read_structure_type). */
18843
18844 struct partial_die_info *real_pdi;
18845 struct partial_die_info *child_pdi;
18846
18847 /* If this DIE (this DIE's specification, if any) has a parent, then
18848 we should not do this. We'll prepend the parent's fully qualified
18849 name when we create the partial symbol. */
18850
18851 real_pdi = struct_pdi;
18852 while (real_pdi->has_specification)
18853 real_pdi = find_partial_die (real_pdi->spec_offset,
18854 real_pdi->spec_is_dwz, cu);
18855
18856 if (real_pdi->die_parent != NULL)
18857 return;
18858
18859 for (child_pdi = struct_pdi->die_child;
18860 child_pdi != NULL;
18861 child_pdi = child_pdi->die_sibling)
18862 {
18863 if (child_pdi->tag == DW_TAG_subprogram
18864 && child_pdi->linkage_name != NULL)
18865 {
18866 char *actual_class_name
18867 = language_class_name_from_physname (cu->language_defn,
18868 child_pdi->linkage_name);
18869 if (actual_class_name != NULL)
18870 {
18871 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18872 struct_pdi->name
18873 = ((const char *)
18874 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18875 actual_class_name,
18876 strlen (actual_class_name)));
18877 xfree (actual_class_name);
18878 }
18879 break;
18880 }
18881 }
18882 }
18883
18884 /* Adjust PART_DIE before generating a symbol for it. This function
18885 may set the is_external flag or change the DIE's name. */
18886
18887 static void
18888 fixup_partial_die (struct partial_die_info *part_die,
18889 struct dwarf2_cu *cu)
18890 {
18891 /* Once we've fixed up a die, there's no point in doing so again.
18892 This also avoids a memory leak if we were to call
18893 guess_partial_die_structure_name multiple times. */
18894 if (part_die->fixup_called)
18895 return;
18896
18897 /* If we found a reference attribute and the DIE has no name, try
18898 to find a name in the referred to DIE. */
18899
18900 if (part_die->name == NULL && part_die->has_specification)
18901 {
18902 struct partial_die_info *spec_die;
18903
18904 spec_die = find_partial_die (part_die->spec_offset,
18905 part_die->spec_is_dwz, cu);
18906
18907 fixup_partial_die (spec_die, cu);
18908
18909 if (spec_die->name)
18910 {
18911 part_die->name = spec_die->name;
18912
18913 /* Copy DW_AT_external attribute if it is set. */
18914 if (spec_die->is_external)
18915 part_die->is_external = spec_die->is_external;
18916 }
18917 }
18918
18919 /* Set default names for some unnamed DIEs. */
18920
18921 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
18922 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
18923
18924 /* If there is no parent die to provide a namespace, and there are
18925 children, see if we can determine the namespace from their linkage
18926 name. */
18927 if (cu->language == language_cplus
18928 && !VEC_empty (dwarf2_section_info_def,
18929 cu->per_cu->dwarf2_per_objfile->types)
18930 && part_die->die_parent == NULL
18931 && part_die->has_children
18932 && (part_die->tag == DW_TAG_class_type
18933 || part_die->tag == DW_TAG_structure_type
18934 || part_die->tag == DW_TAG_union_type))
18935 guess_partial_die_structure_name (part_die, cu);
18936
18937 /* GCC might emit a nameless struct or union that has a linkage
18938 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18939 if (part_die->name == NULL
18940 && (part_die->tag == DW_TAG_class_type
18941 || part_die->tag == DW_TAG_interface_type
18942 || part_die->tag == DW_TAG_structure_type
18943 || part_die->tag == DW_TAG_union_type)
18944 && part_die->linkage_name != NULL)
18945 {
18946 char *demangled;
18947
18948 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
18949 if (demangled)
18950 {
18951 const char *base;
18952
18953 /* Strip any leading namespaces/classes, keep only the base name.
18954 DW_AT_name for named DIEs does not contain the prefixes. */
18955 base = strrchr (demangled, ':');
18956 if (base && base > demangled && base[-1] == ':')
18957 base++;
18958 else
18959 base = demangled;
18960
18961 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18962 part_die->name
18963 = ((const char *)
18964 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18965 base, strlen (base)));
18966 xfree (demangled);
18967 }
18968 }
18969
18970 part_die->fixup_called = 1;
18971 }
18972
18973 /* Read an attribute value described by an attribute form. */
18974
18975 static const gdb_byte *
18976 read_attribute_value (const struct die_reader_specs *reader,
18977 struct attribute *attr, unsigned form,
18978 LONGEST implicit_const, const gdb_byte *info_ptr)
18979 {
18980 struct dwarf2_cu *cu = reader->cu;
18981 struct dwarf2_per_objfile *dwarf2_per_objfile
18982 = cu->per_cu->dwarf2_per_objfile;
18983 struct objfile *objfile = dwarf2_per_objfile->objfile;
18984 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18985 bfd *abfd = reader->abfd;
18986 struct comp_unit_head *cu_header = &cu->header;
18987 unsigned int bytes_read;
18988 struct dwarf_block *blk;
18989
18990 attr->form = (enum dwarf_form) form;
18991 switch (form)
18992 {
18993 case DW_FORM_ref_addr:
18994 if (cu->header.version == 2)
18995 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18996 else
18997 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18998 &cu->header, &bytes_read);
18999 info_ptr += bytes_read;
19000 break;
19001 case DW_FORM_GNU_ref_alt:
19002 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19003 info_ptr += bytes_read;
19004 break;
19005 case DW_FORM_addr:
19006 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19007 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19008 info_ptr += bytes_read;
19009 break;
19010 case DW_FORM_block2:
19011 blk = dwarf_alloc_block (cu);
19012 blk->size = read_2_bytes (abfd, info_ptr);
19013 info_ptr += 2;
19014 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19015 info_ptr += blk->size;
19016 DW_BLOCK (attr) = blk;
19017 break;
19018 case DW_FORM_block4:
19019 blk = dwarf_alloc_block (cu);
19020 blk->size = read_4_bytes (abfd, info_ptr);
19021 info_ptr += 4;
19022 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19023 info_ptr += blk->size;
19024 DW_BLOCK (attr) = blk;
19025 break;
19026 case DW_FORM_data2:
19027 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19028 info_ptr += 2;
19029 break;
19030 case DW_FORM_data4:
19031 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19032 info_ptr += 4;
19033 break;
19034 case DW_FORM_data8:
19035 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19036 info_ptr += 8;
19037 break;
19038 case DW_FORM_data16:
19039 blk = dwarf_alloc_block (cu);
19040 blk->size = 16;
19041 blk->data = read_n_bytes (abfd, info_ptr, 16);
19042 info_ptr += 16;
19043 DW_BLOCK (attr) = blk;
19044 break;
19045 case DW_FORM_sec_offset:
19046 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19047 info_ptr += bytes_read;
19048 break;
19049 case DW_FORM_string:
19050 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19051 DW_STRING_IS_CANONICAL (attr) = 0;
19052 info_ptr += bytes_read;
19053 break;
19054 case DW_FORM_strp:
19055 if (!cu->per_cu->is_dwz)
19056 {
19057 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19058 abfd, info_ptr, cu_header,
19059 &bytes_read);
19060 DW_STRING_IS_CANONICAL (attr) = 0;
19061 info_ptr += bytes_read;
19062 break;
19063 }
19064 /* FALLTHROUGH */
19065 case DW_FORM_line_strp:
19066 if (!cu->per_cu->is_dwz)
19067 {
19068 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19069 abfd, info_ptr,
19070 cu_header, &bytes_read);
19071 DW_STRING_IS_CANONICAL (attr) = 0;
19072 info_ptr += bytes_read;
19073 break;
19074 }
19075 /* FALLTHROUGH */
19076 case DW_FORM_GNU_strp_alt:
19077 {
19078 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19079 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19080 &bytes_read);
19081
19082 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19083 dwz, str_offset);
19084 DW_STRING_IS_CANONICAL (attr) = 0;
19085 info_ptr += bytes_read;
19086 }
19087 break;
19088 case DW_FORM_exprloc:
19089 case DW_FORM_block:
19090 blk = dwarf_alloc_block (cu);
19091 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19092 info_ptr += bytes_read;
19093 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19094 info_ptr += blk->size;
19095 DW_BLOCK (attr) = blk;
19096 break;
19097 case DW_FORM_block1:
19098 blk = dwarf_alloc_block (cu);
19099 blk->size = read_1_byte (abfd, info_ptr);
19100 info_ptr += 1;
19101 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19102 info_ptr += blk->size;
19103 DW_BLOCK (attr) = blk;
19104 break;
19105 case DW_FORM_data1:
19106 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19107 info_ptr += 1;
19108 break;
19109 case DW_FORM_flag:
19110 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19111 info_ptr += 1;
19112 break;
19113 case DW_FORM_flag_present:
19114 DW_UNSND (attr) = 1;
19115 break;
19116 case DW_FORM_sdata:
19117 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19118 info_ptr += bytes_read;
19119 break;
19120 case DW_FORM_udata:
19121 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19122 info_ptr += bytes_read;
19123 break;
19124 case DW_FORM_ref1:
19125 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19126 + read_1_byte (abfd, info_ptr));
19127 info_ptr += 1;
19128 break;
19129 case DW_FORM_ref2:
19130 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19131 + read_2_bytes (abfd, info_ptr));
19132 info_ptr += 2;
19133 break;
19134 case DW_FORM_ref4:
19135 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19136 + read_4_bytes (abfd, info_ptr));
19137 info_ptr += 4;
19138 break;
19139 case DW_FORM_ref8:
19140 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19141 + read_8_bytes (abfd, info_ptr));
19142 info_ptr += 8;
19143 break;
19144 case DW_FORM_ref_sig8:
19145 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19146 info_ptr += 8;
19147 break;
19148 case DW_FORM_ref_udata:
19149 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19150 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19151 info_ptr += bytes_read;
19152 break;
19153 case DW_FORM_indirect:
19154 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19155 info_ptr += bytes_read;
19156 if (form == DW_FORM_implicit_const)
19157 {
19158 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19159 info_ptr += bytes_read;
19160 }
19161 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19162 info_ptr);
19163 break;
19164 case DW_FORM_implicit_const:
19165 DW_SND (attr) = implicit_const;
19166 break;
19167 case DW_FORM_GNU_addr_index:
19168 if (reader->dwo_file == NULL)
19169 {
19170 /* For now flag a hard error.
19171 Later we can turn this into a complaint. */
19172 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19173 dwarf_form_name (form),
19174 bfd_get_filename (abfd));
19175 }
19176 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19177 info_ptr += bytes_read;
19178 break;
19179 case DW_FORM_GNU_str_index:
19180 if (reader->dwo_file == NULL)
19181 {
19182 /* For now flag a hard error.
19183 Later we can turn this into a complaint if warranted. */
19184 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19185 dwarf_form_name (form),
19186 bfd_get_filename (abfd));
19187 }
19188 {
19189 ULONGEST str_index =
19190 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19191
19192 DW_STRING (attr) = read_str_index (reader, str_index);
19193 DW_STRING_IS_CANONICAL (attr) = 0;
19194 info_ptr += bytes_read;
19195 }
19196 break;
19197 default:
19198 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19199 dwarf_form_name (form),
19200 bfd_get_filename (abfd));
19201 }
19202
19203 /* Super hack. */
19204 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19205 attr->form = DW_FORM_GNU_ref_alt;
19206
19207 /* We have seen instances where the compiler tried to emit a byte
19208 size attribute of -1 which ended up being encoded as an unsigned
19209 0xffffffff. Although 0xffffffff is technically a valid size value,
19210 an object of this size seems pretty unlikely so we can relatively
19211 safely treat these cases as if the size attribute was invalid and
19212 treat them as zero by default. */
19213 if (attr->name == DW_AT_byte_size
19214 && form == DW_FORM_data4
19215 && DW_UNSND (attr) >= 0xffffffff)
19216 {
19217 complaint
19218 (&symfile_complaints,
19219 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19220 hex_string (DW_UNSND (attr)));
19221 DW_UNSND (attr) = 0;
19222 }
19223
19224 return info_ptr;
19225 }
19226
19227 /* Read an attribute described by an abbreviated attribute. */
19228
19229 static const gdb_byte *
19230 read_attribute (const struct die_reader_specs *reader,
19231 struct attribute *attr, struct attr_abbrev *abbrev,
19232 const gdb_byte *info_ptr)
19233 {
19234 attr->name = abbrev->name;
19235 return read_attribute_value (reader, attr, abbrev->form,
19236 abbrev->implicit_const, info_ptr);
19237 }
19238
19239 /* Read dwarf information from a buffer. */
19240
19241 static unsigned int
19242 read_1_byte (bfd *abfd, const gdb_byte *buf)
19243 {
19244 return bfd_get_8 (abfd, buf);
19245 }
19246
19247 static int
19248 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19249 {
19250 return bfd_get_signed_8 (abfd, buf);
19251 }
19252
19253 static unsigned int
19254 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19255 {
19256 return bfd_get_16 (abfd, buf);
19257 }
19258
19259 static int
19260 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19261 {
19262 return bfd_get_signed_16 (abfd, buf);
19263 }
19264
19265 static unsigned int
19266 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19267 {
19268 return bfd_get_32 (abfd, buf);
19269 }
19270
19271 static int
19272 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19273 {
19274 return bfd_get_signed_32 (abfd, buf);
19275 }
19276
19277 static ULONGEST
19278 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19279 {
19280 return bfd_get_64 (abfd, buf);
19281 }
19282
19283 static CORE_ADDR
19284 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19285 unsigned int *bytes_read)
19286 {
19287 struct comp_unit_head *cu_header = &cu->header;
19288 CORE_ADDR retval = 0;
19289
19290 if (cu_header->signed_addr_p)
19291 {
19292 switch (cu_header->addr_size)
19293 {
19294 case 2:
19295 retval = bfd_get_signed_16 (abfd, buf);
19296 break;
19297 case 4:
19298 retval = bfd_get_signed_32 (abfd, buf);
19299 break;
19300 case 8:
19301 retval = bfd_get_signed_64 (abfd, buf);
19302 break;
19303 default:
19304 internal_error (__FILE__, __LINE__,
19305 _("read_address: bad switch, signed [in module %s]"),
19306 bfd_get_filename (abfd));
19307 }
19308 }
19309 else
19310 {
19311 switch (cu_header->addr_size)
19312 {
19313 case 2:
19314 retval = bfd_get_16 (abfd, buf);
19315 break;
19316 case 4:
19317 retval = bfd_get_32 (abfd, buf);
19318 break;
19319 case 8:
19320 retval = bfd_get_64 (abfd, buf);
19321 break;
19322 default:
19323 internal_error (__FILE__, __LINE__,
19324 _("read_address: bad switch, "
19325 "unsigned [in module %s]"),
19326 bfd_get_filename (abfd));
19327 }
19328 }
19329
19330 *bytes_read = cu_header->addr_size;
19331 return retval;
19332 }
19333
19334 /* Read the initial length from a section. The (draft) DWARF 3
19335 specification allows the initial length to take up either 4 bytes
19336 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19337 bytes describe the length and all offsets will be 8 bytes in length
19338 instead of 4.
19339
19340 An older, non-standard 64-bit format is also handled by this
19341 function. The older format in question stores the initial length
19342 as an 8-byte quantity without an escape value. Lengths greater
19343 than 2^32 aren't very common which means that the initial 4 bytes
19344 is almost always zero. Since a length value of zero doesn't make
19345 sense for the 32-bit format, this initial zero can be considered to
19346 be an escape value which indicates the presence of the older 64-bit
19347 format. As written, the code can't detect (old format) lengths
19348 greater than 4GB. If it becomes necessary to handle lengths
19349 somewhat larger than 4GB, we could allow other small values (such
19350 as the non-sensical values of 1, 2, and 3) to also be used as
19351 escape values indicating the presence of the old format.
19352
19353 The value returned via bytes_read should be used to increment the
19354 relevant pointer after calling read_initial_length().
19355
19356 [ Note: read_initial_length() and read_offset() are based on the
19357 document entitled "DWARF Debugging Information Format", revision
19358 3, draft 8, dated November 19, 2001. This document was obtained
19359 from:
19360
19361 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19362
19363 This document is only a draft and is subject to change. (So beware.)
19364
19365 Details regarding the older, non-standard 64-bit format were
19366 determined empirically by examining 64-bit ELF files produced by
19367 the SGI toolchain on an IRIX 6.5 machine.
19368
19369 - Kevin, July 16, 2002
19370 ] */
19371
19372 static LONGEST
19373 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19374 {
19375 LONGEST length = bfd_get_32 (abfd, buf);
19376
19377 if (length == 0xffffffff)
19378 {
19379 length = bfd_get_64 (abfd, buf + 4);
19380 *bytes_read = 12;
19381 }
19382 else if (length == 0)
19383 {
19384 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19385 length = bfd_get_64 (abfd, buf);
19386 *bytes_read = 8;
19387 }
19388 else
19389 {
19390 *bytes_read = 4;
19391 }
19392
19393 return length;
19394 }
19395
19396 /* Cover function for read_initial_length.
19397 Returns the length of the object at BUF, and stores the size of the
19398 initial length in *BYTES_READ and stores the size that offsets will be in
19399 *OFFSET_SIZE.
19400 If the initial length size is not equivalent to that specified in
19401 CU_HEADER then issue a complaint.
19402 This is useful when reading non-comp-unit headers. */
19403
19404 static LONGEST
19405 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19406 const struct comp_unit_head *cu_header,
19407 unsigned int *bytes_read,
19408 unsigned int *offset_size)
19409 {
19410 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19411
19412 gdb_assert (cu_header->initial_length_size == 4
19413 || cu_header->initial_length_size == 8
19414 || cu_header->initial_length_size == 12);
19415
19416 if (cu_header->initial_length_size != *bytes_read)
19417 complaint (&symfile_complaints,
19418 _("intermixed 32-bit and 64-bit DWARF sections"));
19419
19420 *offset_size = (*bytes_read == 4) ? 4 : 8;
19421 return length;
19422 }
19423
19424 /* Read an offset from the data stream. The size of the offset is
19425 given by cu_header->offset_size. */
19426
19427 static LONGEST
19428 read_offset (bfd *abfd, const gdb_byte *buf,
19429 const struct comp_unit_head *cu_header,
19430 unsigned int *bytes_read)
19431 {
19432 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19433
19434 *bytes_read = cu_header->offset_size;
19435 return offset;
19436 }
19437
19438 /* Read an offset from the data stream. */
19439
19440 static LONGEST
19441 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19442 {
19443 LONGEST retval = 0;
19444
19445 switch (offset_size)
19446 {
19447 case 4:
19448 retval = bfd_get_32 (abfd, buf);
19449 break;
19450 case 8:
19451 retval = bfd_get_64 (abfd, buf);
19452 break;
19453 default:
19454 internal_error (__FILE__, __LINE__,
19455 _("read_offset_1: bad switch [in module %s]"),
19456 bfd_get_filename (abfd));
19457 }
19458
19459 return retval;
19460 }
19461
19462 static const gdb_byte *
19463 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19464 {
19465 /* If the size of a host char is 8 bits, we can return a pointer
19466 to the buffer, otherwise we have to copy the data to a buffer
19467 allocated on the temporary obstack. */
19468 gdb_assert (HOST_CHAR_BIT == 8);
19469 return buf;
19470 }
19471
19472 static const char *
19473 read_direct_string (bfd *abfd, const gdb_byte *buf,
19474 unsigned int *bytes_read_ptr)
19475 {
19476 /* If the size of a host char is 8 bits, we can return a pointer
19477 to the string, otherwise we have to copy the string to a buffer
19478 allocated on the temporary obstack. */
19479 gdb_assert (HOST_CHAR_BIT == 8);
19480 if (*buf == '\0')
19481 {
19482 *bytes_read_ptr = 1;
19483 return NULL;
19484 }
19485 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19486 return (const char *) buf;
19487 }
19488
19489 /* Return pointer to string at section SECT offset STR_OFFSET with error
19490 reporting strings FORM_NAME and SECT_NAME. */
19491
19492 static const char *
19493 read_indirect_string_at_offset_from (struct objfile *objfile,
19494 bfd *abfd, LONGEST str_offset,
19495 struct dwarf2_section_info *sect,
19496 const char *form_name,
19497 const char *sect_name)
19498 {
19499 dwarf2_read_section (objfile, sect);
19500 if (sect->buffer == NULL)
19501 error (_("%s used without %s section [in module %s]"),
19502 form_name, sect_name, bfd_get_filename (abfd));
19503 if (str_offset >= sect->size)
19504 error (_("%s pointing outside of %s section [in module %s]"),
19505 form_name, sect_name, bfd_get_filename (abfd));
19506 gdb_assert (HOST_CHAR_BIT == 8);
19507 if (sect->buffer[str_offset] == '\0')
19508 return NULL;
19509 return (const char *) (sect->buffer + str_offset);
19510 }
19511
19512 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19513
19514 static const char *
19515 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19516 bfd *abfd, LONGEST str_offset)
19517 {
19518 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19519 abfd, str_offset,
19520 &dwarf2_per_objfile->str,
19521 "DW_FORM_strp", ".debug_str");
19522 }
19523
19524 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19525
19526 static const char *
19527 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19528 bfd *abfd, LONGEST str_offset)
19529 {
19530 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19531 abfd, str_offset,
19532 &dwarf2_per_objfile->line_str,
19533 "DW_FORM_line_strp",
19534 ".debug_line_str");
19535 }
19536
19537 /* Read a string at offset STR_OFFSET in the .debug_str section from
19538 the .dwz file DWZ. Throw an error if the offset is too large. If
19539 the string consists of a single NUL byte, return NULL; otherwise
19540 return a pointer to the string. */
19541
19542 static const char *
19543 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19544 LONGEST str_offset)
19545 {
19546 dwarf2_read_section (objfile, &dwz->str);
19547
19548 if (dwz->str.buffer == NULL)
19549 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19550 "section [in module %s]"),
19551 bfd_get_filename (dwz->dwz_bfd));
19552 if (str_offset >= dwz->str.size)
19553 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19554 ".debug_str section [in module %s]"),
19555 bfd_get_filename (dwz->dwz_bfd));
19556 gdb_assert (HOST_CHAR_BIT == 8);
19557 if (dwz->str.buffer[str_offset] == '\0')
19558 return NULL;
19559 return (const char *) (dwz->str.buffer + str_offset);
19560 }
19561
19562 /* Return pointer to string at .debug_str offset as read from BUF.
19563 BUF is assumed to be in a compilation unit described by CU_HEADER.
19564 Return *BYTES_READ_PTR count of bytes read from BUF. */
19565
19566 static const char *
19567 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19568 const gdb_byte *buf,
19569 const struct comp_unit_head *cu_header,
19570 unsigned int *bytes_read_ptr)
19571 {
19572 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19573
19574 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19575 }
19576
19577 /* Return pointer to string at .debug_line_str offset as read from BUF.
19578 BUF is assumed to be in a compilation unit described by CU_HEADER.
19579 Return *BYTES_READ_PTR count of bytes read from BUF. */
19580
19581 static const char *
19582 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19583 bfd *abfd, const gdb_byte *buf,
19584 const struct comp_unit_head *cu_header,
19585 unsigned int *bytes_read_ptr)
19586 {
19587 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19588
19589 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19590 str_offset);
19591 }
19592
19593 ULONGEST
19594 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19595 unsigned int *bytes_read_ptr)
19596 {
19597 ULONGEST result;
19598 unsigned int num_read;
19599 int shift;
19600 unsigned char byte;
19601
19602 result = 0;
19603 shift = 0;
19604 num_read = 0;
19605 while (1)
19606 {
19607 byte = bfd_get_8 (abfd, buf);
19608 buf++;
19609 num_read++;
19610 result |= ((ULONGEST) (byte & 127) << shift);
19611 if ((byte & 128) == 0)
19612 {
19613 break;
19614 }
19615 shift += 7;
19616 }
19617 *bytes_read_ptr = num_read;
19618 return result;
19619 }
19620
19621 static LONGEST
19622 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19623 unsigned int *bytes_read_ptr)
19624 {
19625 LONGEST result;
19626 int shift, num_read;
19627 unsigned char byte;
19628
19629 result = 0;
19630 shift = 0;
19631 num_read = 0;
19632 while (1)
19633 {
19634 byte = bfd_get_8 (abfd, buf);
19635 buf++;
19636 num_read++;
19637 result |= ((LONGEST) (byte & 127) << shift);
19638 shift += 7;
19639 if ((byte & 128) == 0)
19640 {
19641 break;
19642 }
19643 }
19644 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19645 result |= -(((LONGEST) 1) << shift);
19646 *bytes_read_ptr = num_read;
19647 return result;
19648 }
19649
19650 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19651 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19652 ADDR_SIZE is the size of addresses from the CU header. */
19653
19654 static CORE_ADDR
19655 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19656 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19657 {
19658 struct objfile *objfile = dwarf2_per_objfile->objfile;
19659 bfd *abfd = objfile->obfd;
19660 const gdb_byte *info_ptr;
19661
19662 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19663 if (dwarf2_per_objfile->addr.buffer == NULL)
19664 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19665 objfile_name (objfile));
19666 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19667 error (_("DW_FORM_addr_index pointing outside of "
19668 ".debug_addr section [in module %s]"),
19669 objfile_name (objfile));
19670 info_ptr = (dwarf2_per_objfile->addr.buffer
19671 + addr_base + addr_index * addr_size);
19672 if (addr_size == 4)
19673 return bfd_get_32 (abfd, info_ptr);
19674 else
19675 return bfd_get_64 (abfd, info_ptr);
19676 }
19677
19678 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19679
19680 static CORE_ADDR
19681 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19682 {
19683 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19684 cu->addr_base, cu->header.addr_size);
19685 }
19686
19687 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19688
19689 static CORE_ADDR
19690 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19691 unsigned int *bytes_read)
19692 {
19693 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19694 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19695
19696 return read_addr_index (cu, addr_index);
19697 }
19698
19699 /* Data structure to pass results from dwarf2_read_addr_index_reader
19700 back to dwarf2_read_addr_index. */
19701
19702 struct dwarf2_read_addr_index_data
19703 {
19704 ULONGEST addr_base;
19705 int addr_size;
19706 };
19707
19708 /* die_reader_func for dwarf2_read_addr_index. */
19709
19710 static void
19711 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19712 const gdb_byte *info_ptr,
19713 struct die_info *comp_unit_die,
19714 int has_children,
19715 void *data)
19716 {
19717 struct dwarf2_cu *cu = reader->cu;
19718 struct dwarf2_read_addr_index_data *aidata =
19719 (struct dwarf2_read_addr_index_data *) data;
19720
19721 aidata->addr_base = cu->addr_base;
19722 aidata->addr_size = cu->header.addr_size;
19723 }
19724
19725 /* Given an index in .debug_addr, fetch the value.
19726 NOTE: This can be called during dwarf expression evaluation,
19727 long after the debug information has been read, and thus per_cu->cu
19728 may no longer exist. */
19729
19730 CORE_ADDR
19731 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19732 unsigned int addr_index)
19733 {
19734 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19735 struct objfile *objfile = dwarf2_per_objfile->objfile;
19736 struct dwarf2_cu *cu = per_cu->cu;
19737 ULONGEST addr_base;
19738 int addr_size;
19739
19740 /* We need addr_base and addr_size.
19741 If we don't have PER_CU->cu, we have to get it.
19742 Nasty, but the alternative is storing the needed info in PER_CU,
19743 which at this point doesn't seem justified: it's not clear how frequently
19744 it would get used and it would increase the size of every PER_CU.
19745 Entry points like dwarf2_per_cu_addr_size do a similar thing
19746 so we're not in uncharted territory here.
19747 Alas we need to be a bit more complicated as addr_base is contained
19748 in the DIE.
19749
19750 We don't need to read the entire CU(/TU).
19751 We just need the header and top level die.
19752
19753 IWBN to use the aging mechanism to let us lazily later discard the CU.
19754 For now we skip this optimization. */
19755
19756 if (cu != NULL)
19757 {
19758 addr_base = cu->addr_base;
19759 addr_size = cu->header.addr_size;
19760 }
19761 else
19762 {
19763 struct dwarf2_read_addr_index_data aidata;
19764
19765 /* Note: We can't use init_cutu_and_read_dies_simple here,
19766 we need addr_base. */
19767 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19768 dwarf2_read_addr_index_reader, &aidata);
19769 addr_base = aidata.addr_base;
19770 addr_size = aidata.addr_size;
19771 }
19772
19773 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19774 addr_size);
19775 }
19776
19777 /* Given a DW_FORM_GNU_str_index, fetch the string.
19778 This is only used by the Fission support. */
19779
19780 static const char *
19781 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19782 {
19783 struct dwarf2_cu *cu = reader->cu;
19784 struct dwarf2_per_objfile *dwarf2_per_objfile
19785 = cu->per_cu->dwarf2_per_objfile;
19786 struct objfile *objfile = dwarf2_per_objfile->objfile;
19787 const char *objf_name = objfile_name (objfile);
19788 bfd *abfd = objfile->obfd;
19789 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19790 struct dwarf2_section_info *str_offsets_section =
19791 &reader->dwo_file->sections.str_offsets;
19792 const gdb_byte *info_ptr;
19793 ULONGEST str_offset;
19794 static const char form_name[] = "DW_FORM_GNU_str_index";
19795
19796 dwarf2_read_section (objfile, str_section);
19797 dwarf2_read_section (objfile, str_offsets_section);
19798 if (str_section->buffer == NULL)
19799 error (_("%s used without .debug_str.dwo section"
19800 " in CU at offset %s [in module %s]"),
19801 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19802 if (str_offsets_section->buffer == NULL)
19803 error (_("%s used without .debug_str_offsets.dwo section"
19804 " in CU at offset %s [in module %s]"),
19805 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19806 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19807 error (_("%s pointing outside of .debug_str_offsets.dwo"
19808 " section in CU at offset %s [in module %s]"),
19809 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19810 info_ptr = (str_offsets_section->buffer
19811 + str_index * cu->header.offset_size);
19812 if (cu->header.offset_size == 4)
19813 str_offset = bfd_get_32 (abfd, info_ptr);
19814 else
19815 str_offset = bfd_get_64 (abfd, info_ptr);
19816 if (str_offset >= str_section->size)
19817 error (_("Offset from %s pointing outside of"
19818 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19819 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19820 return (const char *) (str_section->buffer + str_offset);
19821 }
19822
19823 /* Return the length of an LEB128 number in BUF. */
19824
19825 static int
19826 leb128_size (const gdb_byte *buf)
19827 {
19828 const gdb_byte *begin = buf;
19829 gdb_byte byte;
19830
19831 while (1)
19832 {
19833 byte = *buf++;
19834 if ((byte & 128) == 0)
19835 return buf - begin;
19836 }
19837 }
19838
19839 static void
19840 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19841 {
19842 switch (lang)
19843 {
19844 case DW_LANG_C89:
19845 case DW_LANG_C99:
19846 case DW_LANG_C11:
19847 case DW_LANG_C:
19848 case DW_LANG_UPC:
19849 cu->language = language_c;
19850 break;
19851 case DW_LANG_Java:
19852 case DW_LANG_C_plus_plus:
19853 case DW_LANG_C_plus_plus_11:
19854 case DW_LANG_C_plus_plus_14:
19855 cu->language = language_cplus;
19856 break;
19857 case DW_LANG_D:
19858 cu->language = language_d;
19859 break;
19860 case DW_LANG_Fortran77:
19861 case DW_LANG_Fortran90:
19862 case DW_LANG_Fortran95:
19863 case DW_LANG_Fortran03:
19864 case DW_LANG_Fortran08:
19865 cu->language = language_fortran;
19866 break;
19867 case DW_LANG_Go:
19868 cu->language = language_go;
19869 break;
19870 case DW_LANG_Mips_Assembler:
19871 cu->language = language_asm;
19872 break;
19873 case DW_LANG_Ada83:
19874 case DW_LANG_Ada95:
19875 cu->language = language_ada;
19876 break;
19877 case DW_LANG_Modula2:
19878 cu->language = language_m2;
19879 break;
19880 case DW_LANG_Pascal83:
19881 cu->language = language_pascal;
19882 break;
19883 case DW_LANG_ObjC:
19884 cu->language = language_objc;
19885 break;
19886 case DW_LANG_Rust:
19887 case DW_LANG_Rust_old:
19888 cu->language = language_rust;
19889 break;
19890 case DW_LANG_Cobol74:
19891 case DW_LANG_Cobol85:
19892 default:
19893 cu->language = language_minimal;
19894 break;
19895 }
19896 cu->language_defn = language_def (cu->language);
19897 }
19898
19899 /* Return the named attribute or NULL if not there. */
19900
19901 static struct attribute *
19902 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19903 {
19904 for (;;)
19905 {
19906 unsigned int i;
19907 struct attribute *spec = NULL;
19908
19909 for (i = 0; i < die->num_attrs; ++i)
19910 {
19911 if (die->attrs[i].name == name)
19912 return &die->attrs[i];
19913 if (die->attrs[i].name == DW_AT_specification
19914 || die->attrs[i].name == DW_AT_abstract_origin)
19915 spec = &die->attrs[i];
19916 }
19917
19918 if (!spec)
19919 break;
19920
19921 die = follow_die_ref (die, spec, &cu);
19922 }
19923
19924 return NULL;
19925 }
19926
19927 /* Return the named attribute or NULL if not there,
19928 but do not follow DW_AT_specification, etc.
19929 This is for use in contexts where we're reading .debug_types dies.
19930 Following DW_AT_specification, DW_AT_abstract_origin will take us
19931 back up the chain, and we want to go down. */
19932
19933 static struct attribute *
19934 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19935 {
19936 unsigned int i;
19937
19938 for (i = 0; i < die->num_attrs; ++i)
19939 if (die->attrs[i].name == name)
19940 return &die->attrs[i];
19941
19942 return NULL;
19943 }
19944
19945 /* Return the string associated with a string-typed attribute, or NULL if it
19946 is either not found or is of an incorrect type. */
19947
19948 static const char *
19949 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19950 {
19951 struct attribute *attr;
19952 const char *str = NULL;
19953
19954 attr = dwarf2_attr (die, name, cu);
19955
19956 if (attr != NULL)
19957 {
19958 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19959 || attr->form == DW_FORM_string
19960 || attr->form == DW_FORM_GNU_str_index
19961 || attr->form == DW_FORM_GNU_strp_alt)
19962 str = DW_STRING (attr);
19963 else
19964 complaint (&symfile_complaints,
19965 _("string type expected for attribute %s for "
19966 "DIE at %s in module %s"),
19967 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19968 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19969 }
19970
19971 return str;
19972 }
19973
19974 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19975 and holds a non-zero value. This function should only be used for
19976 DW_FORM_flag or DW_FORM_flag_present attributes. */
19977
19978 static int
19979 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19980 {
19981 struct attribute *attr = dwarf2_attr (die, name, cu);
19982
19983 return (attr && DW_UNSND (attr));
19984 }
19985
19986 static int
19987 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19988 {
19989 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19990 which value is non-zero. However, we have to be careful with
19991 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19992 (via dwarf2_flag_true_p) follows this attribute. So we may
19993 end up accidently finding a declaration attribute that belongs
19994 to a different DIE referenced by the specification attribute,
19995 even though the given DIE does not have a declaration attribute. */
19996 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19997 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19998 }
19999
20000 /* Return the die giving the specification for DIE, if there is
20001 one. *SPEC_CU is the CU containing DIE on input, and the CU
20002 containing the return value on output. If there is no
20003 specification, but there is an abstract origin, that is
20004 returned. */
20005
20006 static struct die_info *
20007 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20008 {
20009 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20010 *spec_cu);
20011
20012 if (spec_attr == NULL)
20013 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20014
20015 if (spec_attr == NULL)
20016 return NULL;
20017 else
20018 return follow_die_ref (die, spec_attr, spec_cu);
20019 }
20020
20021 /* Stub for free_line_header to match void * callback types. */
20022
20023 static void
20024 free_line_header_voidp (void *arg)
20025 {
20026 struct line_header *lh = (struct line_header *) arg;
20027
20028 delete lh;
20029 }
20030
20031 void
20032 line_header::add_include_dir (const char *include_dir)
20033 {
20034 if (dwarf_line_debug >= 2)
20035 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20036 include_dirs.size () + 1, include_dir);
20037
20038 include_dirs.push_back (include_dir);
20039 }
20040
20041 void
20042 line_header::add_file_name (const char *name,
20043 dir_index d_index,
20044 unsigned int mod_time,
20045 unsigned int length)
20046 {
20047 if (dwarf_line_debug >= 2)
20048 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
20049 (unsigned) file_names.size () + 1, name);
20050
20051 file_names.emplace_back (name, d_index, mod_time, length);
20052 }
20053
20054 /* A convenience function to find the proper .debug_line section for a CU. */
20055
20056 static struct dwarf2_section_info *
20057 get_debug_line_section (struct dwarf2_cu *cu)
20058 {
20059 struct dwarf2_section_info *section;
20060 struct dwarf2_per_objfile *dwarf2_per_objfile
20061 = cu->per_cu->dwarf2_per_objfile;
20062
20063 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20064 DWO file. */
20065 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20066 section = &cu->dwo_unit->dwo_file->sections.line;
20067 else if (cu->per_cu->is_dwz)
20068 {
20069 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20070
20071 section = &dwz->line;
20072 }
20073 else
20074 section = &dwarf2_per_objfile->line;
20075
20076 return section;
20077 }
20078
20079 /* Read directory or file name entry format, starting with byte of
20080 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20081 entries count and the entries themselves in the described entry
20082 format. */
20083
20084 static void
20085 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20086 bfd *abfd, const gdb_byte **bufp,
20087 struct line_header *lh,
20088 const struct comp_unit_head *cu_header,
20089 void (*callback) (struct line_header *lh,
20090 const char *name,
20091 dir_index d_index,
20092 unsigned int mod_time,
20093 unsigned int length))
20094 {
20095 gdb_byte format_count, formati;
20096 ULONGEST data_count, datai;
20097 const gdb_byte *buf = *bufp;
20098 const gdb_byte *format_header_data;
20099 unsigned int bytes_read;
20100
20101 format_count = read_1_byte (abfd, buf);
20102 buf += 1;
20103 format_header_data = buf;
20104 for (formati = 0; formati < format_count; formati++)
20105 {
20106 read_unsigned_leb128 (abfd, buf, &bytes_read);
20107 buf += bytes_read;
20108 read_unsigned_leb128 (abfd, buf, &bytes_read);
20109 buf += bytes_read;
20110 }
20111
20112 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20113 buf += bytes_read;
20114 for (datai = 0; datai < data_count; datai++)
20115 {
20116 const gdb_byte *format = format_header_data;
20117 struct file_entry fe;
20118
20119 for (formati = 0; formati < format_count; formati++)
20120 {
20121 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20122 format += bytes_read;
20123
20124 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20125 format += bytes_read;
20126
20127 gdb::optional<const char *> string;
20128 gdb::optional<unsigned int> uint;
20129
20130 switch (form)
20131 {
20132 case DW_FORM_string:
20133 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20134 buf += bytes_read;
20135 break;
20136
20137 case DW_FORM_line_strp:
20138 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20139 abfd, buf,
20140 cu_header,
20141 &bytes_read));
20142 buf += bytes_read;
20143 break;
20144
20145 case DW_FORM_data1:
20146 uint.emplace (read_1_byte (abfd, buf));
20147 buf += 1;
20148 break;
20149
20150 case DW_FORM_data2:
20151 uint.emplace (read_2_bytes (abfd, buf));
20152 buf += 2;
20153 break;
20154
20155 case DW_FORM_data4:
20156 uint.emplace (read_4_bytes (abfd, buf));
20157 buf += 4;
20158 break;
20159
20160 case DW_FORM_data8:
20161 uint.emplace (read_8_bytes (abfd, buf));
20162 buf += 8;
20163 break;
20164
20165 case DW_FORM_udata:
20166 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20167 buf += bytes_read;
20168 break;
20169
20170 case DW_FORM_block:
20171 /* It is valid only for DW_LNCT_timestamp which is ignored by
20172 current GDB. */
20173 break;
20174 }
20175
20176 switch (content_type)
20177 {
20178 case DW_LNCT_path:
20179 if (string.has_value ())
20180 fe.name = *string;
20181 break;
20182 case DW_LNCT_directory_index:
20183 if (uint.has_value ())
20184 fe.d_index = (dir_index) *uint;
20185 break;
20186 case DW_LNCT_timestamp:
20187 if (uint.has_value ())
20188 fe.mod_time = *uint;
20189 break;
20190 case DW_LNCT_size:
20191 if (uint.has_value ())
20192 fe.length = *uint;
20193 break;
20194 case DW_LNCT_MD5:
20195 break;
20196 default:
20197 complaint (&symfile_complaints,
20198 _("Unknown format content type %s"),
20199 pulongest (content_type));
20200 }
20201 }
20202
20203 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20204 }
20205
20206 *bufp = buf;
20207 }
20208
20209 /* Read the statement program header starting at OFFSET in
20210 .debug_line, or .debug_line.dwo. Return a pointer
20211 to a struct line_header, allocated using xmalloc.
20212 Returns NULL if there is a problem reading the header, e.g., if it
20213 has a version we don't understand.
20214
20215 NOTE: the strings in the include directory and file name tables of
20216 the returned object point into the dwarf line section buffer,
20217 and must not be freed. */
20218
20219 static line_header_up
20220 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20221 {
20222 const gdb_byte *line_ptr;
20223 unsigned int bytes_read, offset_size;
20224 int i;
20225 const char *cur_dir, *cur_file;
20226 struct dwarf2_section_info *section;
20227 bfd *abfd;
20228 struct dwarf2_per_objfile *dwarf2_per_objfile
20229 = cu->per_cu->dwarf2_per_objfile;
20230
20231 section = get_debug_line_section (cu);
20232 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20233 if (section->buffer == NULL)
20234 {
20235 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20236 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20237 else
20238 complaint (&symfile_complaints, _("missing .debug_line section"));
20239 return 0;
20240 }
20241
20242 /* We can't do this until we know the section is non-empty.
20243 Only then do we know we have such a section. */
20244 abfd = get_section_bfd_owner (section);
20245
20246 /* Make sure that at least there's room for the total_length field.
20247 That could be 12 bytes long, but we're just going to fudge that. */
20248 if (to_underlying (sect_off) + 4 >= section->size)
20249 {
20250 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20251 return 0;
20252 }
20253
20254 line_header_up lh (new line_header ());
20255
20256 lh->sect_off = sect_off;
20257 lh->offset_in_dwz = cu->per_cu->is_dwz;
20258
20259 line_ptr = section->buffer + to_underlying (sect_off);
20260
20261 /* Read in the header. */
20262 lh->total_length =
20263 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20264 &bytes_read, &offset_size);
20265 line_ptr += bytes_read;
20266 if (line_ptr + lh->total_length > (section->buffer + section->size))
20267 {
20268 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20269 return 0;
20270 }
20271 lh->statement_program_end = line_ptr + lh->total_length;
20272 lh->version = read_2_bytes (abfd, line_ptr);
20273 line_ptr += 2;
20274 if (lh->version > 5)
20275 {
20276 /* This is a version we don't understand. The format could have
20277 changed in ways we don't handle properly so just punt. */
20278 complaint (&symfile_complaints,
20279 _("unsupported version in .debug_line section"));
20280 return NULL;
20281 }
20282 if (lh->version >= 5)
20283 {
20284 gdb_byte segment_selector_size;
20285
20286 /* Skip address size. */
20287 read_1_byte (abfd, line_ptr);
20288 line_ptr += 1;
20289
20290 segment_selector_size = read_1_byte (abfd, line_ptr);
20291 line_ptr += 1;
20292 if (segment_selector_size != 0)
20293 {
20294 complaint (&symfile_complaints,
20295 _("unsupported segment selector size %u "
20296 "in .debug_line section"),
20297 segment_selector_size);
20298 return NULL;
20299 }
20300 }
20301 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20302 line_ptr += offset_size;
20303 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20304 line_ptr += 1;
20305 if (lh->version >= 4)
20306 {
20307 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20308 line_ptr += 1;
20309 }
20310 else
20311 lh->maximum_ops_per_instruction = 1;
20312
20313 if (lh->maximum_ops_per_instruction == 0)
20314 {
20315 lh->maximum_ops_per_instruction = 1;
20316 complaint (&symfile_complaints,
20317 _("invalid maximum_ops_per_instruction "
20318 "in `.debug_line' section"));
20319 }
20320
20321 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20322 line_ptr += 1;
20323 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20324 line_ptr += 1;
20325 lh->line_range = read_1_byte (abfd, line_ptr);
20326 line_ptr += 1;
20327 lh->opcode_base = read_1_byte (abfd, line_ptr);
20328 line_ptr += 1;
20329 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20330
20331 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20332 for (i = 1; i < lh->opcode_base; ++i)
20333 {
20334 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20335 line_ptr += 1;
20336 }
20337
20338 if (lh->version >= 5)
20339 {
20340 /* Read directory table. */
20341 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20342 &cu->header,
20343 [] (struct line_header *lh, const char *name,
20344 dir_index d_index, unsigned int mod_time,
20345 unsigned int length)
20346 {
20347 lh->add_include_dir (name);
20348 });
20349
20350 /* Read file name table. */
20351 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20352 &cu->header,
20353 [] (struct line_header *lh, const char *name,
20354 dir_index d_index, unsigned int mod_time,
20355 unsigned int length)
20356 {
20357 lh->add_file_name (name, d_index, mod_time, length);
20358 });
20359 }
20360 else
20361 {
20362 /* Read directory table. */
20363 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20364 {
20365 line_ptr += bytes_read;
20366 lh->add_include_dir (cur_dir);
20367 }
20368 line_ptr += bytes_read;
20369
20370 /* Read file name table. */
20371 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20372 {
20373 unsigned int mod_time, length;
20374 dir_index d_index;
20375
20376 line_ptr += bytes_read;
20377 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20378 line_ptr += bytes_read;
20379 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20380 line_ptr += bytes_read;
20381 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20382 line_ptr += bytes_read;
20383
20384 lh->add_file_name (cur_file, d_index, mod_time, length);
20385 }
20386 line_ptr += bytes_read;
20387 }
20388 lh->statement_program_start = line_ptr;
20389
20390 if (line_ptr > (section->buffer + section->size))
20391 complaint (&symfile_complaints,
20392 _("line number info header doesn't "
20393 "fit in `.debug_line' section"));
20394
20395 return lh;
20396 }
20397
20398 /* Subroutine of dwarf_decode_lines to simplify it.
20399 Return the file name of the psymtab for included file FILE_INDEX
20400 in line header LH of PST.
20401 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20402 If space for the result is malloc'd, *NAME_HOLDER will be set.
20403 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20404
20405 static const char *
20406 psymtab_include_file_name (const struct line_header *lh, int file_index,
20407 const struct partial_symtab *pst,
20408 const char *comp_dir,
20409 gdb::unique_xmalloc_ptr<char> *name_holder)
20410 {
20411 const file_entry &fe = lh->file_names[file_index];
20412 const char *include_name = fe.name;
20413 const char *include_name_to_compare = include_name;
20414 const char *pst_filename;
20415 int file_is_pst;
20416
20417 const char *dir_name = fe.include_dir (lh);
20418
20419 gdb::unique_xmalloc_ptr<char> hold_compare;
20420 if (!IS_ABSOLUTE_PATH (include_name)
20421 && (dir_name != NULL || comp_dir != NULL))
20422 {
20423 /* Avoid creating a duplicate psymtab for PST.
20424 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20425 Before we do the comparison, however, we need to account
20426 for DIR_NAME and COMP_DIR.
20427 First prepend dir_name (if non-NULL). If we still don't
20428 have an absolute path prepend comp_dir (if non-NULL).
20429 However, the directory we record in the include-file's
20430 psymtab does not contain COMP_DIR (to match the
20431 corresponding symtab(s)).
20432
20433 Example:
20434
20435 bash$ cd /tmp
20436 bash$ gcc -g ./hello.c
20437 include_name = "hello.c"
20438 dir_name = "."
20439 DW_AT_comp_dir = comp_dir = "/tmp"
20440 DW_AT_name = "./hello.c"
20441
20442 */
20443
20444 if (dir_name != NULL)
20445 {
20446 name_holder->reset (concat (dir_name, SLASH_STRING,
20447 include_name, (char *) NULL));
20448 include_name = name_holder->get ();
20449 include_name_to_compare = include_name;
20450 }
20451 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20452 {
20453 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20454 include_name, (char *) NULL));
20455 include_name_to_compare = hold_compare.get ();
20456 }
20457 }
20458
20459 pst_filename = pst->filename;
20460 gdb::unique_xmalloc_ptr<char> copied_name;
20461 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20462 {
20463 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20464 pst_filename, (char *) NULL));
20465 pst_filename = copied_name.get ();
20466 }
20467
20468 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20469
20470 if (file_is_pst)
20471 return NULL;
20472 return include_name;
20473 }
20474
20475 /* State machine to track the state of the line number program. */
20476
20477 class lnp_state_machine
20478 {
20479 public:
20480 /* Initialize a machine state for the start of a line number
20481 program. */
20482 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20483
20484 file_entry *current_file ()
20485 {
20486 /* lh->file_names is 0-based, but the file name numbers in the
20487 statement program are 1-based. */
20488 return m_line_header->file_name_at (m_file);
20489 }
20490
20491 /* Record the line in the state machine. END_SEQUENCE is true if
20492 we're processing the end of a sequence. */
20493 void record_line (bool end_sequence);
20494
20495 /* Check address and if invalid nop-out the rest of the lines in this
20496 sequence. */
20497 void check_line_address (struct dwarf2_cu *cu,
20498 const gdb_byte *line_ptr,
20499 CORE_ADDR lowpc, CORE_ADDR address);
20500
20501 void handle_set_discriminator (unsigned int discriminator)
20502 {
20503 m_discriminator = discriminator;
20504 m_line_has_non_zero_discriminator |= discriminator != 0;
20505 }
20506
20507 /* Handle DW_LNE_set_address. */
20508 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20509 {
20510 m_op_index = 0;
20511 address += baseaddr;
20512 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20513 }
20514
20515 /* Handle DW_LNS_advance_pc. */
20516 void handle_advance_pc (CORE_ADDR adjust);
20517
20518 /* Handle a special opcode. */
20519 void handle_special_opcode (unsigned char op_code);
20520
20521 /* Handle DW_LNS_advance_line. */
20522 void handle_advance_line (int line_delta)
20523 {
20524 advance_line (line_delta);
20525 }
20526
20527 /* Handle DW_LNS_set_file. */
20528 void handle_set_file (file_name_index file);
20529
20530 /* Handle DW_LNS_negate_stmt. */
20531 void handle_negate_stmt ()
20532 {
20533 m_is_stmt = !m_is_stmt;
20534 }
20535
20536 /* Handle DW_LNS_const_add_pc. */
20537 void handle_const_add_pc ();
20538
20539 /* Handle DW_LNS_fixed_advance_pc. */
20540 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20541 {
20542 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20543 m_op_index = 0;
20544 }
20545
20546 /* Handle DW_LNS_copy. */
20547 void handle_copy ()
20548 {
20549 record_line (false);
20550 m_discriminator = 0;
20551 }
20552
20553 /* Handle DW_LNE_end_sequence. */
20554 void handle_end_sequence ()
20555 {
20556 m_record_line_callback = ::record_line;
20557 }
20558
20559 private:
20560 /* Advance the line by LINE_DELTA. */
20561 void advance_line (int line_delta)
20562 {
20563 m_line += line_delta;
20564
20565 if (line_delta != 0)
20566 m_line_has_non_zero_discriminator = m_discriminator != 0;
20567 }
20568
20569 gdbarch *m_gdbarch;
20570
20571 /* True if we're recording lines.
20572 Otherwise we're building partial symtabs and are just interested in
20573 finding include files mentioned by the line number program. */
20574 bool m_record_lines_p;
20575
20576 /* The line number header. */
20577 line_header *m_line_header;
20578
20579 /* These are part of the standard DWARF line number state machine,
20580 and initialized according to the DWARF spec. */
20581
20582 unsigned char m_op_index = 0;
20583 /* The line table index (1-based) of the current file. */
20584 file_name_index m_file = (file_name_index) 1;
20585 unsigned int m_line = 1;
20586
20587 /* These are initialized in the constructor. */
20588
20589 CORE_ADDR m_address;
20590 bool m_is_stmt;
20591 unsigned int m_discriminator;
20592
20593 /* Additional bits of state we need to track. */
20594
20595 /* The last file that we called dwarf2_start_subfile for.
20596 This is only used for TLLs. */
20597 unsigned int m_last_file = 0;
20598 /* The last file a line number was recorded for. */
20599 struct subfile *m_last_subfile = NULL;
20600
20601 /* The function to call to record a line. */
20602 record_line_ftype *m_record_line_callback = NULL;
20603
20604 /* The last line number that was recorded, used to coalesce
20605 consecutive entries for the same line. This can happen, for
20606 example, when discriminators are present. PR 17276. */
20607 unsigned int m_last_line = 0;
20608 bool m_line_has_non_zero_discriminator = false;
20609 };
20610
20611 void
20612 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20613 {
20614 CORE_ADDR addr_adj = (((m_op_index + adjust)
20615 / m_line_header->maximum_ops_per_instruction)
20616 * m_line_header->minimum_instruction_length);
20617 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20618 m_op_index = ((m_op_index + adjust)
20619 % m_line_header->maximum_ops_per_instruction);
20620 }
20621
20622 void
20623 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20624 {
20625 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20626 CORE_ADDR addr_adj = (((m_op_index
20627 + (adj_opcode / m_line_header->line_range))
20628 / m_line_header->maximum_ops_per_instruction)
20629 * m_line_header->minimum_instruction_length);
20630 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20631 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20632 % m_line_header->maximum_ops_per_instruction);
20633
20634 int line_delta = (m_line_header->line_base
20635 + (adj_opcode % m_line_header->line_range));
20636 advance_line (line_delta);
20637 record_line (false);
20638 m_discriminator = 0;
20639 }
20640
20641 void
20642 lnp_state_machine::handle_set_file (file_name_index file)
20643 {
20644 m_file = file;
20645
20646 const file_entry *fe = current_file ();
20647 if (fe == NULL)
20648 dwarf2_debug_line_missing_file_complaint ();
20649 else if (m_record_lines_p)
20650 {
20651 const char *dir = fe->include_dir (m_line_header);
20652
20653 m_last_subfile = current_subfile;
20654 m_line_has_non_zero_discriminator = m_discriminator != 0;
20655 dwarf2_start_subfile (fe->name, dir);
20656 }
20657 }
20658
20659 void
20660 lnp_state_machine::handle_const_add_pc ()
20661 {
20662 CORE_ADDR adjust
20663 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20664
20665 CORE_ADDR addr_adj
20666 = (((m_op_index + adjust)
20667 / m_line_header->maximum_ops_per_instruction)
20668 * m_line_header->minimum_instruction_length);
20669
20670 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20671 m_op_index = ((m_op_index + adjust)
20672 % m_line_header->maximum_ops_per_instruction);
20673 }
20674
20675 /* Ignore this record_line request. */
20676
20677 static void
20678 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20679 {
20680 return;
20681 }
20682
20683 /* Return non-zero if we should add LINE to the line number table.
20684 LINE is the line to add, LAST_LINE is the last line that was added,
20685 LAST_SUBFILE is the subfile for LAST_LINE.
20686 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20687 had a non-zero discriminator.
20688
20689 We have to be careful in the presence of discriminators.
20690 E.g., for this line:
20691
20692 for (i = 0; i < 100000; i++);
20693
20694 clang can emit four line number entries for that one line,
20695 each with a different discriminator.
20696 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20697
20698 However, we want gdb to coalesce all four entries into one.
20699 Otherwise the user could stepi into the middle of the line and
20700 gdb would get confused about whether the pc really was in the
20701 middle of the line.
20702
20703 Things are further complicated by the fact that two consecutive
20704 line number entries for the same line is a heuristic used by gcc
20705 to denote the end of the prologue. So we can't just discard duplicate
20706 entries, we have to be selective about it. The heuristic we use is
20707 that we only collapse consecutive entries for the same line if at least
20708 one of those entries has a non-zero discriminator. PR 17276.
20709
20710 Note: Addresses in the line number state machine can never go backwards
20711 within one sequence, thus this coalescing is ok. */
20712
20713 static int
20714 dwarf_record_line_p (unsigned int line, unsigned int last_line,
20715 int line_has_non_zero_discriminator,
20716 struct subfile *last_subfile)
20717 {
20718 if (current_subfile != last_subfile)
20719 return 1;
20720 if (line != last_line)
20721 return 1;
20722 /* Same line for the same file that we've seen already.
20723 As a last check, for pr 17276, only record the line if the line
20724 has never had a non-zero discriminator. */
20725 if (!line_has_non_zero_discriminator)
20726 return 1;
20727 return 0;
20728 }
20729
20730 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20731 in the line table of subfile SUBFILE. */
20732
20733 static void
20734 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20735 unsigned int line, CORE_ADDR address,
20736 record_line_ftype p_record_line)
20737 {
20738 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20739
20740 if (dwarf_line_debug)
20741 {
20742 fprintf_unfiltered (gdb_stdlog,
20743 "Recording line %u, file %s, address %s\n",
20744 line, lbasename (subfile->name),
20745 paddress (gdbarch, address));
20746 }
20747
20748 (*p_record_line) (subfile, line, addr);
20749 }
20750
20751 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20752 Mark the end of a set of line number records.
20753 The arguments are the same as for dwarf_record_line_1.
20754 If SUBFILE is NULL the request is ignored. */
20755
20756 static void
20757 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20758 CORE_ADDR address, record_line_ftype p_record_line)
20759 {
20760 if (subfile == NULL)
20761 return;
20762
20763 if (dwarf_line_debug)
20764 {
20765 fprintf_unfiltered (gdb_stdlog,
20766 "Finishing current line, file %s, address %s\n",
20767 lbasename (subfile->name),
20768 paddress (gdbarch, address));
20769 }
20770
20771 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20772 }
20773
20774 void
20775 lnp_state_machine::record_line (bool end_sequence)
20776 {
20777 if (dwarf_line_debug)
20778 {
20779 fprintf_unfiltered (gdb_stdlog,
20780 "Processing actual line %u: file %u,"
20781 " address %s, is_stmt %u, discrim %u\n",
20782 m_line, to_underlying (m_file),
20783 paddress (m_gdbarch, m_address),
20784 m_is_stmt, m_discriminator);
20785 }
20786
20787 file_entry *fe = current_file ();
20788
20789 if (fe == NULL)
20790 dwarf2_debug_line_missing_file_complaint ();
20791 /* For now we ignore lines not starting on an instruction boundary.
20792 But not when processing end_sequence for compatibility with the
20793 previous version of the code. */
20794 else if (m_op_index == 0 || end_sequence)
20795 {
20796 fe->included_p = 1;
20797 if (m_record_lines_p && m_is_stmt)
20798 {
20799 if (m_last_subfile != current_subfile || end_sequence)
20800 {
20801 dwarf_finish_line (m_gdbarch, m_last_subfile,
20802 m_address, m_record_line_callback);
20803 }
20804
20805 if (!end_sequence)
20806 {
20807 if (dwarf_record_line_p (m_line, m_last_line,
20808 m_line_has_non_zero_discriminator,
20809 m_last_subfile))
20810 {
20811 dwarf_record_line_1 (m_gdbarch, current_subfile,
20812 m_line, m_address,
20813 m_record_line_callback);
20814 }
20815 m_last_subfile = current_subfile;
20816 m_last_line = m_line;
20817 }
20818 }
20819 }
20820 }
20821
20822 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20823 bool record_lines_p)
20824 {
20825 m_gdbarch = arch;
20826 m_record_lines_p = record_lines_p;
20827 m_line_header = lh;
20828
20829 m_record_line_callback = ::record_line;
20830
20831 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20832 was a line entry for it so that the backend has a chance to adjust it
20833 and also record it in case it needs it. This is currently used by MIPS
20834 code, cf. `mips_adjust_dwarf2_line'. */
20835 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20836 m_is_stmt = lh->default_is_stmt;
20837 m_discriminator = 0;
20838 }
20839
20840 void
20841 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20842 const gdb_byte *line_ptr,
20843 CORE_ADDR lowpc, CORE_ADDR address)
20844 {
20845 /* If address < lowpc then it's not a usable value, it's outside the
20846 pc range of the CU. However, we restrict the test to only address
20847 values of zero to preserve GDB's previous behaviour which is to
20848 handle the specific case of a function being GC'd by the linker. */
20849
20850 if (address == 0 && address < lowpc)
20851 {
20852 /* This line table is for a function which has been
20853 GCd by the linker. Ignore it. PR gdb/12528 */
20854
20855 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20856 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20857
20858 complaint (&symfile_complaints,
20859 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20860 line_offset, objfile_name (objfile));
20861 m_record_line_callback = noop_record_line;
20862 /* Note: record_line_callback is left as noop_record_line until
20863 we see DW_LNE_end_sequence. */
20864 }
20865 }
20866
20867 /* Subroutine of dwarf_decode_lines to simplify it.
20868 Process the line number information in LH.
20869 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20870 program in order to set included_p for every referenced header. */
20871
20872 static void
20873 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20874 const int decode_for_pst_p, CORE_ADDR lowpc)
20875 {
20876 const gdb_byte *line_ptr, *extended_end;
20877 const gdb_byte *line_end;
20878 unsigned int bytes_read, extended_len;
20879 unsigned char op_code, extended_op;
20880 CORE_ADDR baseaddr;
20881 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20882 bfd *abfd = objfile->obfd;
20883 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20884 /* True if we're recording line info (as opposed to building partial
20885 symtabs and just interested in finding include files mentioned by
20886 the line number program). */
20887 bool record_lines_p = !decode_for_pst_p;
20888
20889 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20890
20891 line_ptr = lh->statement_program_start;
20892 line_end = lh->statement_program_end;
20893
20894 /* Read the statement sequences until there's nothing left. */
20895 while (line_ptr < line_end)
20896 {
20897 /* The DWARF line number program state machine. Reset the state
20898 machine at the start of each sequence. */
20899 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20900 bool end_sequence = false;
20901
20902 if (record_lines_p)
20903 {
20904 /* Start a subfile for the current file of the state
20905 machine. */
20906 const file_entry *fe = state_machine.current_file ();
20907
20908 if (fe != NULL)
20909 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
20910 }
20911
20912 /* Decode the table. */
20913 while (line_ptr < line_end && !end_sequence)
20914 {
20915 op_code = read_1_byte (abfd, line_ptr);
20916 line_ptr += 1;
20917
20918 if (op_code >= lh->opcode_base)
20919 {
20920 /* Special opcode. */
20921 state_machine.handle_special_opcode (op_code);
20922 }
20923 else switch (op_code)
20924 {
20925 case DW_LNS_extended_op:
20926 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20927 &bytes_read);
20928 line_ptr += bytes_read;
20929 extended_end = line_ptr + extended_len;
20930 extended_op = read_1_byte (abfd, line_ptr);
20931 line_ptr += 1;
20932 switch (extended_op)
20933 {
20934 case DW_LNE_end_sequence:
20935 state_machine.handle_end_sequence ();
20936 end_sequence = true;
20937 break;
20938 case DW_LNE_set_address:
20939 {
20940 CORE_ADDR address
20941 = read_address (abfd, line_ptr, cu, &bytes_read);
20942 line_ptr += bytes_read;
20943
20944 state_machine.check_line_address (cu, line_ptr,
20945 lowpc, address);
20946 state_machine.handle_set_address (baseaddr, address);
20947 }
20948 break;
20949 case DW_LNE_define_file:
20950 {
20951 const char *cur_file;
20952 unsigned int mod_time, length;
20953 dir_index dindex;
20954
20955 cur_file = read_direct_string (abfd, line_ptr,
20956 &bytes_read);
20957 line_ptr += bytes_read;
20958 dindex = (dir_index)
20959 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20960 line_ptr += bytes_read;
20961 mod_time =
20962 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20963 line_ptr += bytes_read;
20964 length =
20965 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20966 line_ptr += bytes_read;
20967 lh->add_file_name (cur_file, dindex, mod_time, length);
20968 }
20969 break;
20970 case DW_LNE_set_discriminator:
20971 {
20972 /* The discriminator is not interesting to the
20973 debugger; just ignore it. We still need to
20974 check its value though:
20975 if there are consecutive entries for the same
20976 (non-prologue) line we want to coalesce them.
20977 PR 17276. */
20978 unsigned int discr
20979 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20980 line_ptr += bytes_read;
20981
20982 state_machine.handle_set_discriminator (discr);
20983 }
20984 break;
20985 default:
20986 complaint (&symfile_complaints,
20987 _("mangled .debug_line section"));
20988 return;
20989 }
20990 /* Make sure that we parsed the extended op correctly. If e.g.
20991 we expected a different address size than the producer used,
20992 we may have read the wrong number of bytes. */
20993 if (line_ptr != extended_end)
20994 {
20995 complaint (&symfile_complaints,
20996 _("mangled .debug_line section"));
20997 return;
20998 }
20999 break;
21000 case DW_LNS_copy:
21001 state_machine.handle_copy ();
21002 break;
21003 case DW_LNS_advance_pc:
21004 {
21005 CORE_ADDR adjust
21006 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21007 line_ptr += bytes_read;
21008
21009 state_machine.handle_advance_pc (adjust);
21010 }
21011 break;
21012 case DW_LNS_advance_line:
21013 {
21014 int line_delta
21015 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21016 line_ptr += bytes_read;
21017
21018 state_machine.handle_advance_line (line_delta);
21019 }
21020 break;
21021 case DW_LNS_set_file:
21022 {
21023 file_name_index file
21024 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21025 &bytes_read);
21026 line_ptr += bytes_read;
21027
21028 state_machine.handle_set_file (file);
21029 }
21030 break;
21031 case DW_LNS_set_column:
21032 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21033 line_ptr += bytes_read;
21034 break;
21035 case DW_LNS_negate_stmt:
21036 state_machine.handle_negate_stmt ();
21037 break;
21038 case DW_LNS_set_basic_block:
21039 break;
21040 /* Add to the address register of the state machine the
21041 address increment value corresponding to special opcode
21042 255. I.e., this value is scaled by the minimum
21043 instruction length since special opcode 255 would have
21044 scaled the increment. */
21045 case DW_LNS_const_add_pc:
21046 state_machine.handle_const_add_pc ();
21047 break;
21048 case DW_LNS_fixed_advance_pc:
21049 {
21050 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21051 line_ptr += 2;
21052
21053 state_machine.handle_fixed_advance_pc (addr_adj);
21054 }
21055 break;
21056 default:
21057 {
21058 /* Unknown standard opcode, ignore it. */
21059 int i;
21060
21061 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21062 {
21063 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21064 line_ptr += bytes_read;
21065 }
21066 }
21067 }
21068 }
21069
21070 if (!end_sequence)
21071 dwarf2_debug_line_missing_end_sequence_complaint ();
21072
21073 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21074 in which case we still finish recording the last line). */
21075 state_machine.record_line (true);
21076 }
21077 }
21078
21079 /* Decode the Line Number Program (LNP) for the given line_header
21080 structure and CU. The actual information extracted and the type
21081 of structures created from the LNP depends on the value of PST.
21082
21083 1. If PST is NULL, then this procedure uses the data from the program
21084 to create all necessary symbol tables, and their linetables.
21085
21086 2. If PST is not NULL, this procedure reads the program to determine
21087 the list of files included by the unit represented by PST, and
21088 builds all the associated partial symbol tables.
21089
21090 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21091 It is used for relative paths in the line table.
21092 NOTE: When processing partial symtabs (pst != NULL),
21093 comp_dir == pst->dirname.
21094
21095 NOTE: It is important that psymtabs have the same file name (via strcmp)
21096 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21097 symtab we don't use it in the name of the psymtabs we create.
21098 E.g. expand_line_sal requires this when finding psymtabs to expand.
21099 A good testcase for this is mb-inline.exp.
21100
21101 LOWPC is the lowest address in CU (or 0 if not known).
21102
21103 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21104 for its PC<->lines mapping information. Otherwise only the filename
21105 table is read in. */
21106
21107 static void
21108 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21109 struct dwarf2_cu *cu, struct partial_symtab *pst,
21110 CORE_ADDR lowpc, int decode_mapping)
21111 {
21112 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21113 const int decode_for_pst_p = (pst != NULL);
21114
21115 if (decode_mapping)
21116 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21117
21118 if (decode_for_pst_p)
21119 {
21120 int file_index;
21121
21122 /* Now that we're done scanning the Line Header Program, we can
21123 create the psymtab of each included file. */
21124 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21125 if (lh->file_names[file_index].included_p == 1)
21126 {
21127 gdb::unique_xmalloc_ptr<char> name_holder;
21128 const char *include_name =
21129 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21130 &name_holder);
21131 if (include_name != NULL)
21132 dwarf2_create_include_psymtab (include_name, pst, objfile);
21133 }
21134 }
21135 else
21136 {
21137 /* Make sure a symtab is created for every file, even files
21138 which contain only variables (i.e. no code with associated
21139 line numbers). */
21140 struct compunit_symtab *cust = buildsym_compunit_symtab ();
21141 int i;
21142
21143 for (i = 0; i < lh->file_names.size (); i++)
21144 {
21145 file_entry &fe = lh->file_names[i];
21146
21147 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
21148
21149 if (current_subfile->symtab == NULL)
21150 {
21151 current_subfile->symtab
21152 = allocate_symtab (cust, current_subfile->name);
21153 }
21154 fe.symtab = current_subfile->symtab;
21155 }
21156 }
21157 }
21158
21159 /* Start a subfile for DWARF. FILENAME is the name of the file and
21160 DIRNAME the name of the source directory which contains FILENAME
21161 or NULL if not known.
21162 This routine tries to keep line numbers from identical absolute and
21163 relative file names in a common subfile.
21164
21165 Using the `list' example from the GDB testsuite, which resides in
21166 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21167 of /srcdir/list0.c yields the following debugging information for list0.c:
21168
21169 DW_AT_name: /srcdir/list0.c
21170 DW_AT_comp_dir: /compdir
21171 files.files[0].name: list0.h
21172 files.files[0].dir: /srcdir
21173 files.files[1].name: list0.c
21174 files.files[1].dir: /srcdir
21175
21176 The line number information for list0.c has to end up in a single
21177 subfile, so that `break /srcdir/list0.c:1' works as expected.
21178 start_subfile will ensure that this happens provided that we pass the
21179 concatenation of files.files[1].dir and files.files[1].name as the
21180 subfile's name. */
21181
21182 static void
21183 dwarf2_start_subfile (const char *filename, const char *dirname)
21184 {
21185 char *copy = NULL;
21186
21187 /* In order not to lose the line information directory,
21188 we concatenate it to the filename when it makes sense.
21189 Note that the Dwarf3 standard says (speaking of filenames in line
21190 information): ``The directory index is ignored for file names
21191 that represent full path names''. Thus ignoring dirname in the
21192 `else' branch below isn't an issue. */
21193
21194 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21195 {
21196 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21197 filename = copy;
21198 }
21199
21200 start_subfile (filename);
21201
21202 if (copy != NULL)
21203 xfree (copy);
21204 }
21205
21206 /* Start a symtab for DWARF.
21207 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21208
21209 static struct compunit_symtab *
21210 dwarf2_start_symtab (struct dwarf2_cu *cu,
21211 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21212 {
21213 struct compunit_symtab *cust
21214 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21215 low_pc, cu->language);
21216
21217 record_debugformat ("DWARF 2");
21218 record_producer (cu->producer);
21219
21220 /* We assume that we're processing GCC output. */
21221 processing_gcc_compilation = 2;
21222
21223 cu->processing_has_namespace_info = 0;
21224
21225 return cust;
21226 }
21227
21228 static void
21229 var_decode_location (struct attribute *attr, struct symbol *sym,
21230 struct dwarf2_cu *cu)
21231 {
21232 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21233 struct comp_unit_head *cu_header = &cu->header;
21234
21235 /* NOTE drow/2003-01-30: There used to be a comment and some special
21236 code here to turn a symbol with DW_AT_external and a
21237 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21238 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21239 with some versions of binutils) where shared libraries could have
21240 relocations against symbols in their debug information - the
21241 minimal symbol would have the right address, but the debug info
21242 would not. It's no longer necessary, because we will explicitly
21243 apply relocations when we read in the debug information now. */
21244
21245 /* A DW_AT_location attribute with no contents indicates that a
21246 variable has been optimized away. */
21247 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21248 {
21249 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21250 return;
21251 }
21252
21253 /* Handle one degenerate form of location expression specially, to
21254 preserve GDB's previous behavior when section offsets are
21255 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21256 then mark this symbol as LOC_STATIC. */
21257
21258 if (attr_form_is_block (attr)
21259 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21260 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21261 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21262 && (DW_BLOCK (attr)->size
21263 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21264 {
21265 unsigned int dummy;
21266
21267 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21268 SYMBOL_VALUE_ADDRESS (sym) =
21269 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21270 else
21271 SYMBOL_VALUE_ADDRESS (sym) =
21272 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21273 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21274 fixup_symbol_section (sym, objfile);
21275 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21276 SYMBOL_SECTION (sym));
21277 return;
21278 }
21279
21280 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21281 expression evaluator, and use LOC_COMPUTED only when necessary
21282 (i.e. when the value of a register or memory location is
21283 referenced, or a thread-local block, etc.). Then again, it might
21284 not be worthwhile. I'm assuming that it isn't unless performance
21285 or memory numbers show me otherwise. */
21286
21287 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21288
21289 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21290 cu->has_loclist = 1;
21291 }
21292
21293 /* Given a pointer to a DWARF information entry, figure out if we need
21294 to make a symbol table entry for it, and if so, create a new entry
21295 and return a pointer to it.
21296 If TYPE is NULL, determine symbol type from the die, otherwise
21297 used the passed type.
21298 If SPACE is not NULL, use it to hold the new symbol. If it is
21299 NULL, allocate a new symbol on the objfile's obstack. */
21300
21301 static struct symbol *
21302 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21303 struct symbol *space)
21304 {
21305 struct dwarf2_per_objfile *dwarf2_per_objfile
21306 = cu->per_cu->dwarf2_per_objfile;
21307 struct objfile *objfile = dwarf2_per_objfile->objfile;
21308 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21309 struct symbol *sym = NULL;
21310 const char *name;
21311 struct attribute *attr = NULL;
21312 struct attribute *attr2 = NULL;
21313 CORE_ADDR baseaddr;
21314 struct pending **list_to_add = NULL;
21315
21316 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21317
21318 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21319
21320 name = dwarf2_name (die, cu);
21321 if (name)
21322 {
21323 const char *linkagename;
21324 int suppress_add = 0;
21325
21326 if (space)
21327 sym = space;
21328 else
21329 sym = allocate_symbol (objfile);
21330 OBJSTAT (objfile, n_syms++);
21331
21332 /* Cache this symbol's name and the name's demangled form (if any). */
21333 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21334 linkagename = dwarf2_physname (name, die, cu);
21335 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21336
21337 /* Fortran does not have mangling standard and the mangling does differ
21338 between gfortran, iFort etc. */
21339 if (cu->language == language_fortran
21340 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21341 symbol_set_demangled_name (&(sym->ginfo),
21342 dwarf2_full_name (name, die, cu),
21343 NULL);
21344
21345 /* Default assumptions.
21346 Use the passed type or decode it from the die. */
21347 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21348 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21349 if (type != NULL)
21350 SYMBOL_TYPE (sym) = type;
21351 else
21352 SYMBOL_TYPE (sym) = die_type (die, cu);
21353 attr = dwarf2_attr (die,
21354 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21355 cu);
21356 if (attr)
21357 {
21358 SYMBOL_LINE (sym) = DW_UNSND (attr);
21359 }
21360
21361 attr = dwarf2_attr (die,
21362 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21363 cu);
21364 if (attr)
21365 {
21366 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21367 struct file_entry *fe;
21368
21369 if (cu->line_header != NULL)
21370 fe = cu->line_header->file_name_at (file_index);
21371 else
21372 fe = NULL;
21373
21374 if (fe == NULL)
21375 complaint (&symfile_complaints,
21376 _("file index out of range"));
21377 else
21378 symbol_set_symtab (sym, fe->symtab);
21379 }
21380
21381 switch (die->tag)
21382 {
21383 case DW_TAG_label:
21384 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21385 if (attr)
21386 {
21387 CORE_ADDR addr;
21388
21389 addr = attr_value_as_address (attr);
21390 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21391 SYMBOL_VALUE_ADDRESS (sym) = addr;
21392 }
21393 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21394 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21395 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21396 add_symbol_to_list (sym, cu->list_in_scope);
21397 break;
21398 case DW_TAG_subprogram:
21399 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21400 finish_block. */
21401 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21402 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21403 if ((attr2 && (DW_UNSND (attr2) != 0))
21404 || cu->language == language_ada)
21405 {
21406 /* Subprograms marked external are stored as a global symbol.
21407 Ada subprograms, whether marked external or not, are always
21408 stored as a global symbol, because we want to be able to
21409 access them globally. For instance, we want to be able
21410 to break on a nested subprogram without having to
21411 specify the context. */
21412 list_to_add = &global_symbols;
21413 }
21414 else
21415 {
21416 list_to_add = cu->list_in_scope;
21417 }
21418 break;
21419 case DW_TAG_inlined_subroutine:
21420 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21421 finish_block. */
21422 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21423 SYMBOL_INLINED (sym) = 1;
21424 list_to_add = cu->list_in_scope;
21425 break;
21426 case DW_TAG_template_value_param:
21427 suppress_add = 1;
21428 /* Fall through. */
21429 case DW_TAG_constant:
21430 case DW_TAG_variable:
21431 case DW_TAG_member:
21432 /* Compilation with minimal debug info may result in
21433 variables with missing type entries. Change the
21434 misleading `void' type to something sensible. */
21435 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21436 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21437
21438 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21439 /* In the case of DW_TAG_member, we should only be called for
21440 static const members. */
21441 if (die->tag == DW_TAG_member)
21442 {
21443 /* dwarf2_add_field uses die_is_declaration,
21444 so we do the same. */
21445 gdb_assert (die_is_declaration (die, cu));
21446 gdb_assert (attr);
21447 }
21448 if (attr)
21449 {
21450 dwarf2_const_value (attr, sym, cu);
21451 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21452 if (!suppress_add)
21453 {
21454 if (attr2 && (DW_UNSND (attr2) != 0))
21455 list_to_add = &global_symbols;
21456 else
21457 list_to_add = cu->list_in_scope;
21458 }
21459 break;
21460 }
21461 attr = dwarf2_attr (die, DW_AT_location, cu);
21462 if (attr)
21463 {
21464 var_decode_location (attr, sym, cu);
21465 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21466
21467 /* Fortran explicitly imports any global symbols to the local
21468 scope by DW_TAG_common_block. */
21469 if (cu->language == language_fortran && die->parent
21470 && die->parent->tag == DW_TAG_common_block)
21471 attr2 = NULL;
21472
21473 if (SYMBOL_CLASS (sym) == LOC_STATIC
21474 && SYMBOL_VALUE_ADDRESS (sym) == 0
21475 && !dwarf2_per_objfile->has_section_at_zero)
21476 {
21477 /* When a static variable is eliminated by the linker,
21478 the corresponding debug information is not stripped
21479 out, but the variable address is set to null;
21480 do not add such variables into symbol table. */
21481 }
21482 else if (attr2 && (DW_UNSND (attr2) != 0))
21483 {
21484 /* Workaround gfortran PR debug/40040 - it uses
21485 DW_AT_location for variables in -fPIC libraries which may
21486 get overriden by other libraries/executable and get
21487 a different address. Resolve it by the minimal symbol
21488 which may come from inferior's executable using copy
21489 relocation. Make this workaround only for gfortran as for
21490 other compilers GDB cannot guess the minimal symbol
21491 Fortran mangling kind. */
21492 if (cu->language == language_fortran && die->parent
21493 && die->parent->tag == DW_TAG_module
21494 && cu->producer
21495 && startswith (cu->producer, "GNU Fortran"))
21496 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21497
21498 /* A variable with DW_AT_external is never static,
21499 but it may be block-scoped. */
21500 list_to_add = (cu->list_in_scope == &file_symbols
21501 ? &global_symbols : cu->list_in_scope);
21502 }
21503 else
21504 list_to_add = cu->list_in_scope;
21505 }
21506 else
21507 {
21508 /* We do not know the address of this symbol.
21509 If it is an external symbol and we have type information
21510 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21511 The address of the variable will then be determined from
21512 the minimal symbol table whenever the variable is
21513 referenced. */
21514 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21515
21516 /* Fortran explicitly imports any global symbols to the local
21517 scope by DW_TAG_common_block. */
21518 if (cu->language == language_fortran && die->parent
21519 && die->parent->tag == DW_TAG_common_block)
21520 {
21521 /* SYMBOL_CLASS doesn't matter here because
21522 read_common_block is going to reset it. */
21523 if (!suppress_add)
21524 list_to_add = cu->list_in_scope;
21525 }
21526 else if (attr2 && (DW_UNSND (attr2) != 0)
21527 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21528 {
21529 /* A variable with DW_AT_external is never static, but it
21530 may be block-scoped. */
21531 list_to_add = (cu->list_in_scope == &file_symbols
21532 ? &global_symbols : cu->list_in_scope);
21533
21534 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21535 }
21536 else if (!die_is_declaration (die, cu))
21537 {
21538 /* Use the default LOC_OPTIMIZED_OUT class. */
21539 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21540 if (!suppress_add)
21541 list_to_add = cu->list_in_scope;
21542 }
21543 }
21544 break;
21545 case DW_TAG_formal_parameter:
21546 /* If we are inside a function, mark this as an argument. If
21547 not, we might be looking at an argument to an inlined function
21548 when we do not have enough information to show inlined frames;
21549 pretend it's a local variable in that case so that the user can
21550 still see it. */
21551 if (context_stack_depth > 0
21552 && context_stack[context_stack_depth - 1].name != NULL)
21553 SYMBOL_IS_ARGUMENT (sym) = 1;
21554 attr = dwarf2_attr (die, DW_AT_location, cu);
21555 if (attr)
21556 {
21557 var_decode_location (attr, sym, cu);
21558 }
21559 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21560 if (attr)
21561 {
21562 dwarf2_const_value (attr, sym, cu);
21563 }
21564
21565 list_to_add = cu->list_in_scope;
21566 break;
21567 case DW_TAG_unspecified_parameters:
21568 /* From varargs functions; gdb doesn't seem to have any
21569 interest in this information, so just ignore it for now.
21570 (FIXME?) */
21571 break;
21572 case DW_TAG_template_type_param:
21573 suppress_add = 1;
21574 /* Fall through. */
21575 case DW_TAG_class_type:
21576 case DW_TAG_interface_type:
21577 case DW_TAG_structure_type:
21578 case DW_TAG_union_type:
21579 case DW_TAG_set_type:
21580 case DW_TAG_enumeration_type:
21581 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21582 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21583
21584 {
21585 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21586 really ever be static objects: otherwise, if you try
21587 to, say, break of a class's method and you're in a file
21588 which doesn't mention that class, it won't work unless
21589 the check for all static symbols in lookup_symbol_aux
21590 saves you. See the OtherFileClass tests in
21591 gdb.c++/namespace.exp. */
21592
21593 if (!suppress_add)
21594 {
21595 list_to_add = (cu->list_in_scope == &file_symbols
21596 && cu->language == language_cplus
21597 ? &global_symbols : cu->list_in_scope);
21598
21599 /* The semantics of C++ state that "struct foo {
21600 ... }" also defines a typedef for "foo". */
21601 if (cu->language == language_cplus
21602 || cu->language == language_ada
21603 || cu->language == language_d
21604 || cu->language == language_rust)
21605 {
21606 /* The symbol's name is already allocated along
21607 with this objfile, so we don't need to
21608 duplicate it for the type. */
21609 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21610 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21611 }
21612 }
21613 }
21614 break;
21615 case DW_TAG_typedef:
21616 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21617 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21618 list_to_add = cu->list_in_scope;
21619 break;
21620 case DW_TAG_base_type:
21621 case DW_TAG_subrange_type:
21622 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21623 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21624 list_to_add = cu->list_in_scope;
21625 break;
21626 case DW_TAG_enumerator:
21627 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21628 if (attr)
21629 {
21630 dwarf2_const_value (attr, sym, cu);
21631 }
21632 {
21633 /* NOTE: carlton/2003-11-10: See comment above in the
21634 DW_TAG_class_type, etc. block. */
21635
21636 list_to_add = (cu->list_in_scope == &file_symbols
21637 && cu->language == language_cplus
21638 ? &global_symbols : cu->list_in_scope);
21639 }
21640 break;
21641 case DW_TAG_imported_declaration:
21642 case DW_TAG_namespace:
21643 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21644 list_to_add = &global_symbols;
21645 break;
21646 case DW_TAG_module:
21647 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21648 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21649 list_to_add = &global_symbols;
21650 break;
21651 case DW_TAG_common_block:
21652 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21653 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21654 add_symbol_to_list (sym, cu->list_in_scope);
21655 break;
21656 default:
21657 /* Not a tag we recognize. Hopefully we aren't processing
21658 trash data, but since we must specifically ignore things
21659 we don't recognize, there is nothing else we should do at
21660 this point. */
21661 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
21662 dwarf_tag_name (die->tag));
21663 break;
21664 }
21665
21666 if (suppress_add)
21667 {
21668 sym->hash_next = objfile->template_symbols;
21669 objfile->template_symbols = sym;
21670 list_to_add = NULL;
21671 }
21672
21673 if (list_to_add != NULL)
21674 add_symbol_to_list (sym, list_to_add);
21675
21676 /* For the benefit of old versions of GCC, check for anonymous
21677 namespaces based on the demangled name. */
21678 if (!cu->processing_has_namespace_info
21679 && cu->language == language_cplus)
21680 cp_scan_for_anonymous_namespaces (sym, objfile);
21681 }
21682 return (sym);
21683 }
21684
21685 /* Given an attr with a DW_FORM_dataN value in host byte order,
21686 zero-extend it as appropriate for the symbol's type. The DWARF
21687 standard (v4) is not entirely clear about the meaning of using
21688 DW_FORM_dataN for a constant with a signed type, where the type is
21689 wider than the data. The conclusion of a discussion on the DWARF
21690 list was that this is unspecified. We choose to always zero-extend
21691 because that is the interpretation long in use by GCC. */
21692
21693 static gdb_byte *
21694 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21695 struct dwarf2_cu *cu, LONGEST *value, int bits)
21696 {
21697 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21698 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21699 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21700 LONGEST l = DW_UNSND (attr);
21701
21702 if (bits < sizeof (*value) * 8)
21703 {
21704 l &= ((LONGEST) 1 << bits) - 1;
21705 *value = l;
21706 }
21707 else if (bits == sizeof (*value) * 8)
21708 *value = l;
21709 else
21710 {
21711 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21712 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21713 return bytes;
21714 }
21715
21716 return NULL;
21717 }
21718
21719 /* Read a constant value from an attribute. Either set *VALUE, or if
21720 the value does not fit in *VALUE, set *BYTES - either already
21721 allocated on the objfile obstack, or newly allocated on OBSTACK,
21722 or, set *BATON, if we translated the constant to a location
21723 expression. */
21724
21725 static void
21726 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21727 const char *name, struct obstack *obstack,
21728 struct dwarf2_cu *cu,
21729 LONGEST *value, const gdb_byte **bytes,
21730 struct dwarf2_locexpr_baton **baton)
21731 {
21732 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21733 struct comp_unit_head *cu_header = &cu->header;
21734 struct dwarf_block *blk;
21735 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21736 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21737
21738 *value = 0;
21739 *bytes = NULL;
21740 *baton = NULL;
21741
21742 switch (attr->form)
21743 {
21744 case DW_FORM_addr:
21745 case DW_FORM_GNU_addr_index:
21746 {
21747 gdb_byte *data;
21748
21749 if (TYPE_LENGTH (type) != cu_header->addr_size)
21750 dwarf2_const_value_length_mismatch_complaint (name,
21751 cu_header->addr_size,
21752 TYPE_LENGTH (type));
21753 /* Symbols of this form are reasonably rare, so we just
21754 piggyback on the existing location code rather than writing
21755 a new implementation of symbol_computed_ops. */
21756 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21757 (*baton)->per_cu = cu->per_cu;
21758 gdb_assert ((*baton)->per_cu);
21759
21760 (*baton)->size = 2 + cu_header->addr_size;
21761 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21762 (*baton)->data = data;
21763
21764 data[0] = DW_OP_addr;
21765 store_unsigned_integer (&data[1], cu_header->addr_size,
21766 byte_order, DW_ADDR (attr));
21767 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21768 }
21769 break;
21770 case DW_FORM_string:
21771 case DW_FORM_strp:
21772 case DW_FORM_GNU_str_index:
21773 case DW_FORM_GNU_strp_alt:
21774 /* DW_STRING is already allocated on the objfile obstack, point
21775 directly to it. */
21776 *bytes = (const gdb_byte *) DW_STRING (attr);
21777 break;
21778 case DW_FORM_block1:
21779 case DW_FORM_block2:
21780 case DW_FORM_block4:
21781 case DW_FORM_block:
21782 case DW_FORM_exprloc:
21783 case DW_FORM_data16:
21784 blk = DW_BLOCK (attr);
21785 if (TYPE_LENGTH (type) != blk->size)
21786 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21787 TYPE_LENGTH (type));
21788 *bytes = blk->data;
21789 break;
21790
21791 /* The DW_AT_const_value attributes are supposed to carry the
21792 symbol's value "represented as it would be on the target
21793 architecture." By the time we get here, it's already been
21794 converted to host endianness, so we just need to sign- or
21795 zero-extend it as appropriate. */
21796 case DW_FORM_data1:
21797 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21798 break;
21799 case DW_FORM_data2:
21800 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21801 break;
21802 case DW_FORM_data4:
21803 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21804 break;
21805 case DW_FORM_data8:
21806 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21807 break;
21808
21809 case DW_FORM_sdata:
21810 case DW_FORM_implicit_const:
21811 *value = DW_SND (attr);
21812 break;
21813
21814 case DW_FORM_udata:
21815 *value = DW_UNSND (attr);
21816 break;
21817
21818 default:
21819 complaint (&symfile_complaints,
21820 _("unsupported const value attribute form: '%s'"),
21821 dwarf_form_name (attr->form));
21822 *value = 0;
21823 break;
21824 }
21825 }
21826
21827
21828 /* Copy constant value from an attribute to a symbol. */
21829
21830 static void
21831 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21832 struct dwarf2_cu *cu)
21833 {
21834 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21835 LONGEST value;
21836 const gdb_byte *bytes;
21837 struct dwarf2_locexpr_baton *baton;
21838
21839 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21840 SYMBOL_PRINT_NAME (sym),
21841 &objfile->objfile_obstack, cu,
21842 &value, &bytes, &baton);
21843
21844 if (baton != NULL)
21845 {
21846 SYMBOL_LOCATION_BATON (sym) = baton;
21847 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21848 }
21849 else if (bytes != NULL)
21850 {
21851 SYMBOL_VALUE_BYTES (sym) = bytes;
21852 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21853 }
21854 else
21855 {
21856 SYMBOL_VALUE (sym) = value;
21857 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21858 }
21859 }
21860
21861 /* Return the type of the die in question using its DW_AT_type attribute. */
21862
21863 static struct type *
21864 die_type (struct die_info *die, struct dwarf2_cu *cu)
21865 {
21866 struct attribute *type_attr;
21867
21868 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21869 if (!type_attr)
21870 {
21871 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21872 /* A missing DW_AT_type represents a void type. */
21873 return objfile_type (objfile)->builtin_void;
21874 }
21875
21876 return lookup_die_type (die, type_attr, cu);
21877 }
21878
21879 /* True iff CU's producer generates GNAT Ada auxiliary information
21880 that allows to find parallel types through that information instead
21881 of having to do expensive parallel lookups by type name. */
21882
21883 static int
21884 need_gnat_info (struct dwarf2_cu *cu)
21885 {
21886 /* Assume that the Ada compiler was GNAT, which always produces
21887 the auxiliary information. */
21888 return (cu->language == language_ada);
21889 }
21890
21891 /* Return the auxiliary type of the die in question using its
21892 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21893 attribute is not present. */
21894
21895 static struct type *
21896 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21897 {
21898 struct attribute *type_attr;
21899
21900 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21901 if (!type_attr)
21902 return NULL;
21903
21904 return lookup_die_type (die, type_attr, cu);
21905 }
21906
21907 /* If DIE has a descriptive_type attribute, then set the TYPE's
21908 descriptive type accordingly. */
21909
21910 static void
21911 set_descriptive_type (struct type *type, struct die_info *die,
21912 struct dwarf2_cu *cu)
21913 {
21914 struct type *descriptive_type = die_descriptive_type (die, cu);
21915
21916 if (descriptive_type)
21917 {
21918 ALLOCATE_GNAT_AUX_TYPE (type);
21919 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21920 }
21921 }
21922
21923 /* Return the containing type of the die in question using its
21924 DW_AT_containing_type attribute. */
21925
21926 static struct type *
21927 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21928 {
21929 struct attribute *type_attr;
21930 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21931
21932 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21933 if (!type_attr)
21934 error (_("Dwarf Error: Problem turning containing type into gdb type "
21935 "[in module %s]"), objfile_name (objfile));
21936
21937 return lookup_die_type (die, type_attr, cu);
21938 }
21939
21940 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21941
21942 static struct type *
21943 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21944 {
21945 struct dwarf2_per_objfile *dwarf2_per_objfile
21946 = cu->per_cu->dwarf2_per_objfile;
21947 struct objfile *objfile = dwarf2_per_objfile->objfile;
21948 char *message, *saved;
21949
21950 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
21951 objfile_name (objfile),
21952 sect_offset_str (cu->header.sect_off),
21953 sect_offset_str (die->sect_off));
21954 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21955 message, strlen (message));
21956 xfree (message);
21957
21958 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21959 }
21960
21961 /* Look up the type of DIE in CU using its type attribute ATTR.
21962 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21963 DW_AT_containing_type.
21964 If there is no type substitute an error marker. */
21965
21966 static struct type *
21967 lookup_die_type (struct die_info *die, const struct attribute *attr,
21968 struct dwarf2_cu *cu)
21969 {
21970 struct dwarf2_per_objfile *dwarf2_per_objfile
21971 = cu->per_cu->dwarf2_per_objfile;
21972 struct objfile *objfile = dwarf2_per_objfile->objfile;
21973 struct type *this_type;
21974
21975 gdb_assert (attr->name == DW_AT_type
21976 || attr->name == DW_AT_GNAT_descriptive_type
21977 || attr->name == DW_AT_containing_type);
21978
21979 /* First see if we have it cached. */
21980
21981 if (attr->form == DW_FORM_GNU_ref_alt)
21982 {
21983 struct dwarf2_per_cu_data *per_cu;
21984 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21985
21986 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21987 dwarf2_per_objfile);
21988 this_type = get_die_type_at_offset (sect_off, per_cu);
21989 }
21990 else if (attr_form_is_ref (attr))
21991 {
21992 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21993
21994 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21995 }
21996 else if (attr->form == DW_FORM_ref_sig8)
21997 {
21998 ULONGEST signature = DW_SIGNATURE (attr);
21999
22000 return get_signatured_type (die, signature, cu);
22001 }
22002 else
22003 {
22004 complaint (&symfile_complaints,
22005 _("Dwarf Error: Bad type attribute %s in DIE"
22006 " at %s [in module %s]"),
22007 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22008 objfile_name (objfile));
22009 return build_error_marker_type (cu, die);
22010 }
22011
22012 /* If not cached we need to read it in. */
22013
22014 if (this_type == NULL)
22015 {
22016 struct die_info *type_die = NULL;
22017 struct dwarf2_cu *type_cu = cu;
22018
22019 if (attr_form_is_ref (attr))
22020 type_die = follow_die_ref (die, attr, &type_cu);
22021 if (type_die == NULL)
22022 return build_error_marker_type (cu, die);
22023 /* If we find the type now, it's probably because the type came
22024 from an inter-CU reference and the type's CU got expanded before
22025 ours. */
22026 this_type = read_type_die (type_die, type_cu);
22027 }
22028
22029 /* If we still don't have a type use an error marker. */
22030
22031 if (this_type == NULL)
22032 return build_error_marker_type (cu, die);
22033
22034 return this_type;
22035 }
22036
22037 /* Return the type in DIE, CU.
22038 Returns NULL for invalid types.
22039
22040 This first does a lookup in die_type_hash,
22041 and only reads the die in if necessary.
22042
22043 NOTE: This can be called when reading in partial or full symbols. */
22044
22045 static struct type *
22046 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22047 {
22048 struct type *this_type;
22049
22050 this_type = get_die_type (die, cu);
22051 if (this_type)
22052 return this_type;
22053
22054 return read_type_die_1 (die, cu);
22055 }
22056
22057 /* Read the type in DIE, CU.
22058 Returns NULL for invalid types. */
22059
22060 static struct type *
22061 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22062 {
22063 struct type *this_type = NULL;
22064
22065 switch (die->tag)
22066 {
22067 case DW_TAG_class_type:
22068 case DW_TAG_interface_type:
22069 case DW_TAG_structure_type:
22070 case DW_TAG_union_type:
22071 this_type = read_structure_type (die, cu);
22072 break;
22073 case DW_TAG_enumeration_type:
22074 this_type = read_enumeration_type (die, cu);
22075 break;
22076 case DW_TAG_subprogram:
22077 case DW_TAG_subroutine_type:
22078 case DW_TAG_inlined_subroutine:
22079 this_type = read_subroutine_type (die, cu);
22080 break;
22081 case DW_TAG_array_type:
22082 this_type = read_array_type (die, cu);
22083 break;
22084 case DW_TAG_set_type:
22085 this_type = read_set_type (die, cu);
22086 break;
22087 case DW_TAG_pointer_type:
22088 this_type = read_tag_pointer_type (die, cu);
22089 break;
22090 case DW_TAG_ptr_to_member_type:
22091 this_type = read_tag_ptr_to_member_type (die, cu);
22092 break;
22093 case DW_TAG_reference_type:
22094 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22095 break;
22096 case DW_TAG_rvalue_reference_type:
22097 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22098 break;
22099 case DW_TAG_const_type:
22100 this_type = read_tag_const_type (die, cu);
22101 break;
22102 case DW_TAG_volatile_type:
22103 this_type = read_tag_volatile_type (die, cu);
22104 break;
22105 case DW_TAG_restrict_type:
22106 this_type = read_tag_restrict_type (die, cu);
22107 break;
22108 case DW_TAG_string_type:
22109 this_type = read_tag_string_type (die, cu);
22110 break;
22111 case DW_TAG_typedef:
22112 this_type = read_typedef (die, cu);
22113 break;
22114 case DW_TAG_subrange_type:
22115 this_type = read_subrange_type (die, cu);
22116 break;
22117 case DW_TAG_base_type:
22118 this_type = read_base_type (die, cu);
22119 break;
22120 case DW_TAG_unspecified_type:
22121 this_type = read_unspecified_type (die, cu);
22122 break;
22123 case DW_TAG_namespace:
22124 this_type = read_namespace_type (die, cu);
22125 break;
22126 case DW_TAG_module:
22127 this_type = read_module_type (die, cu);
22128 break;
22129 case DW_TAG_atomic_type:
22130 this_type = read_tag_atomic_type (die, cu);
22131 break;
22132 default:
22133 complaint (&symfile_complaints,
22134 _("unexpected tag in read_type_die: '%s'"),
22135 dwarf_tag_name (die->tag));
22136 break;
22137 }
22138
22139 return this_type;
22140 }
22141
22142 /* See if we can figure out if the class lives in a namespace. We do
22143 this by looking for a member function; its demangled name will
22144 contain namespace info, if there is any.
22145 Return the computed name or NULL.
22146 Space for the result is allocated on the objfile's obstack.
22147 This is the full-die version of guess_partial_die_structure_name.
22148 In this case we know DIE has no useful parent. */
22149
22150 static char *
22151 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22152 {
22153 struct die_info *spec_die;
22154 struct dwarf2_cu *spec_cu;
22155 struct die_info *child;
22156 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22157
22158 spec_cu = cu;
22159 spec_die = die_specification (die, &spec_cu);
22160 if (spec_die != NULL)
22161 {
22162 die = spec_die;
22163 cu = spec_cu;
22164 }
22165
22166 for (child = die->child;
22167 child != NULL;
22168 child = child->sibling)
22169 {
22170 if (child->tag == DW_TAG_subprogram)
22171 {
22172 const char *linkage_name = dw2_linkage_name (child, cu);
22173
22174 if (linkage_name != NULL)
22175 {
22176 char *actual_name
22177 = language_class_name_from_physname (cu->language_defn,
22178 linkage_name);
22179 char *name = NULL;
22180
22181 if (actual_name != NULL)
22182 {
22183 const char *die_name = dwarf2_name (die, cu);
22184
22185 if (die_name != NULL
22186 && strcmp (die_name, actual_name) != 0)
22187 {
22188 /* Strip off the class name from the full name.
22189 We want the prefix. */
22190 int die_name_len = strlen (die_name);
22191 int actual_name_len = strlen (actual_name);
22192
22193 /* Test for '::' as a sanity check. */
22194 if (actual_name_len > die_name_len + 2
22195 && actual_name[actual_name_len
22196 - die_name_len - 1] == ':')
22197 name = (char *) obstack_copy0 (
22198 &objfile->per_bfd->storage_obstack,
22199 actual_name, actual_name_len - die_name_len - 2);
22200 }
22201 }
22202 xfree (actual_name);
22203 return name;
22204 }
22205 }
22206 }
22207
22208 return NULL;
22209 }
22210
22211 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22212 prefix part in such case. See
22213 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22214
22215 static const char *
22216 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22217 {
22218 struct attribute *attr;
22219 const char *base;
22220
22221 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22222 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22223 return NULL;
22224
22225 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22226 return NULL;
22227
22228 attr = dw2_linkage_name_attr (die, cu);
22229 if (attr == NULL || DW_STRING (attr) == NULL)
22230 return NULL;
22231
22232 /* dwarf2_name had to be already called. */
22233 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22234
22235 /* Strip the base name, keep any leading namespaces/classes. */
22236 base = strrchr (DW_STRING (attr), ':');
22237 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22238 return "";
22239
22240 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22241 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22242 DW_STRING (attr),
22243 &base[-1] - DW_STRING (attr));
22244 }
22245
22246 /* Return the name of the namespace/class that DIE is defined within,
22247 or "" if we can't tell. The caller should not xfree the result.
22248
22249 For example, if we're within the method foo() in the following
22250 code:
22251
22252 namespace N {
22253 class C {
22254 void foo () {
22255 }
22256 };
22257 }
22258
22259 then determine_prefix on foo's die will return "N::C". */
22260
22261 static const char *
22262 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22263 {
22264 struct dwarf2_per_objfile *dwarf2_per_objfile
22265 = cu->per_cu->dwarf2_per_objfile;
22266 struct die_info *parent, *spec_die;
22267 struct dwarf2_cu *spec_cu;
22268 struct type *parent_type;
22269 const char *retval;
22270
22271 if (cu->language != language_cplus
22272 && cu->language != language_fortran && cu->language != language_d
22273 && cu->language != language_rust)
22274 return "";
22275
22276 retval = anonymous_struct_prefix (die, cu);
22277 if (retval)
22278 return retval;
22279
22280 /* We have to be careful in the presence of DW_AT_specification.
22281 For example, with GCC 3.4, given the code
22282
22283 namespace N {
22284 void foo() {
22285 // Definition of N::foo.
22286 }
22287 }
22288
22289 then we'll have a tree of DIEs like this:
22290
22291 1: DW_TAG_compile_unit
22292 2: DW_TAG_namespace // N
22293 3: DW_TAG_subprogram // declaration of N::foo
22294 4: DW_TAG_subprogram // definition of N::foo
22295 DW_AT_specification // refers to die #3
22296
22297 Thus, when processing die #4, we have to pretend that we're in
22298 the context of its DW_AT_specification, namely the contex of die
22299 #3. */
22300 spec_cu = cu;
22301 spec_die = die_specification (die, &spec_cu);
22302 if (spec_die == NULL)
22303 parent = die->parent;
22304 else
22305 {
22306 parent = spec_die->parent;
22307 cu = spec_cu;
22308 }
22309
22310 if (parent == NULL)
22311 return "";
22312 else if (parent->building_fullname)
22313 {
22314 const char *name;
22315 const char *parent_name;
22316
22317 /* It has been seen on RealView 2.2 built binaries,
22318 DW_TAG_template_type_param types actually _defined_ as
22319 children of the parent class:
22320
22321 enum E {};
22322 template class <class Enum> Class{};
22323 Class<enum E> class_e;
22324
22325 1: DW_TAG_class_type (Class)
22326 2: DW_TAG_enumeration_type (E)
22327 3: DW_TAG_enumerator (enum1:0)
22328 3: DW_TAG_enumerator (enum2:1)
22329 ...
22330 2: DW_TAG_template_type_param
22331 DW_AT_type DW_FORM_ref_udata (E)
22332
22333 Besides being broken debug info, it can put GDB into an
22334 infinite loop. Consider:
22335
22336 When we're building the full name for Class<E>, we'll start
22337 at Class, and go look over its template type parameters,
22338 finding E. We'll then try to build the full name of E, and
22339 reach here. We're now trying to build the full name of E,
22340 and look over the parent DIE for containing scope. In the
22341 broken case, if we followed the parent DIE of E, we'd again
22342 find Class, and once again go look at its template type
22343 arguments, etc., etc. Simply don't consider such parent die
22344 as source-level parent of this die (it can't be, the language
22345 doesn't allow it), and break the loop here. */
22346 name = dwarf2_name (die, cu);
22347 parent_name = dwarf2_name (parent, cu);
22348 complaint (&symfile_complaints,
22349 _("template param type '%s' defined within parent '%s'"),
22350 name ? name : "<unknown>",
22351 parent_name ? parent_name : "<unknown>");
22352 return "";
22353 }
22354 else
22355 switch (parent->tag)
22356 {
22357 case DW_TAG_namespace:
22358 parent_type = read_type_die (parent, cu);
22359 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22360 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22361 Work around this problem here. */
22362 if (cu->language == language_cplus
22363 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22364 return "";
22365 /* We give a name to even anonymous namespaces. */
22366 return TYPE_TAG_NAME (parent_type);
22367 case DW_TAG_class_type:
22368 case DW_TAG_interface_type:
22369 case DW_TAG_structure_type:
22370 case DW_TAG_union_type:
22371 case DW_TAG_module:
22372 parent_type = read_type_die (parent, cu);
22373 if (TYPE_TAG_NAME (parent_type) != NULL)
22374 return TYPE_TAG_NAME (parent_type);
22375 else
22376 /* An anonymous structure is only allowed non-static data
22377 members; no typedefs, no member functions, et cetera.
22378 So it does not need a prefix. */
22379 return "";
22380 case DW_TAG_compile_unit:
22381 case DW_TAG_partial_unit:
22382 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22383 if (cu->language == language_cplus
22384 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22385 && die->child != NULL
22386 && (die->tag == DW_TAG_class_type
22387 || die->tag == DW_TAG_structure_type
22388 || die->tag == DW_TAG_union_type))
22389 {
22390 char *name = guess_full_die_structure_name (die, cu);
22391 if (name != NULL)
22392 return name;
22393 }
22394 return "";
22395 case DW_TAG_enumeration_type:
22396 parent_type = read_type_die (parent, cu);
22397 if (TYPE_DECLARED_CLASS (parent_type))
22398 {
22399 if (TYPE_TAG_NAME (parent_type) != NULL)
22400 return TYPE_TAG_NAME (parent_type);
22401 return "";
22402 }
22403 /* Fall through. */
22404 default:
22405 return determine_prefix (parent, cu);
22406 }
22407 }
22408
22409 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22410 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22411 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22412 an obconcat, otherwise allocate storage for the result. The CU argument is
22413 used to determine the language and hence, the appropriate separator. */
22414
22415 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22416
22417 static char *
22418 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22419 int physname, struct dwarf2_cu *cu)
22420 {
22421 const char *lead = "";
22422 const char *sep;
22423
22424 if (suffix == NULL || suffix[0] == '\0'
22425 || prefix == NULL || prefix[0] == '\0')
22426 sep = "";
22427 else if (cu->language == language_d)
22428 {
22429 /* For D, the 'main' function could be defined in any module, but it
22430 should never be prefixed. */
22431 if (strcmp (suffix, "D main") == 0)
22432 {
22433 prefix = "";
22434 sep = "";
22435 }
22436 else
22437 sep = ".";
22438 }
22439 else if (cu->language == language_fortran && physname)
22440 {
22441 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22442 DW_AT_MIPS_linkage_name is preferred and used instead. */
22443
22444 lead = "__";
22445 sep = "_MOD_";
22446 }
22447 else
22448 sep = "::";
22449
22450 if (prefix == NULL)
22451 prefix = "";
22452 if (suffix == NULL)
22453 suffix = "";
22454
22455 if (obs == NULL)
22456 {
22457 char *retval
22458 = ((char *)
22459 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22460
22461 strcpy (retval, lead);
22462 strcat (retval, prefix);
22463 strcat (retval, sep);
22464 strcat (retval, suffix);
22465 return retval;
22466 }
22467 else
22468 {
22469 /* We have an obstack. */
22470 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22471 }
22472 }
22473
22474 /* Return sibling of die, NULL if no sibling. */
22475
22476 static struct die_info *
22477 sibling_die (struct die_info *die)
22478 {
22479 return die->sibling;
22480 }
22481
22482 /* Get name of a die, return NULL if not found. */
22483
22484 static const char *
22485 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22486 struct obstack *obstack)
22487 {
22488 if (name && cu->language == language_cplus)
22489 {
22490 std::string canon_name = cp_canonicalize_string (name);
22491
22492 if (!canon_name.empty ())
22493 {
22494 if (canon_name != name)
22495 name = (const char *) obstack_copy0 (obstack,
22496 canon_name.c_str (),
22497 canon_name.length ());
22498 }
22499 }
22500
22501 return name;
22502 }
22503
22504 /* Get name of a die, return NULL if not found.
22505 Anonymous namespaces are converted to their magic string. */
22506
22507 static const char *
22508 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22509 {
22510 struct attribute *attr;
22511 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22512
22513 attr = dwarf2_attr (die, DW_AT_name, cu);
22514 if ((!attr || !DW_STRING (attr))
22515 && die->tag != DW_TAG_namespace
22516 && die->tag != DW_TAG_class_type
22517 && die->tag != DW_TAG_interface_type
22518 && die->tag != DW_TAG_structure_type
22519 && die->tag != DW_TAG_union_type)
22520 return NULL;
22521
22522 switch (die->tag)
22523 {
22524 case DW_TAG_compile_unit:
22525 case DW_TAG_partial_unit:
22526 /* Compilation units have a DW_AT_name that is a filename, not
22527 a source language identifier. */
22528 case DW_TAG_enumeration_type:
22529 case DW_TAG_enumerator:
22530 /* These tags always have simple identifiers already; no need
22531 to canonicalize them. */
22532 return DW_STRING (attr);
22533
22534 case DW_TAG_namespace:
22535 if (attr != NULL && DW_STRING (attr) != NULL)
22536 return DW_STRING (attr);
22537 return CP_ANONYMOUS_NAMESPACE_STR;
22538
22539 case DW_TAG_class_type:
22540 case DW_TAG_interface_type:
22541 case DW_TAG_structure_type:
22542 case DW_TAG_union_type:
22543 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22544 structures or unions. These were of the form "._%d" in GCC 4.1,
22545 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22546 and GCC 4.4. We work around this problem by ignoring these. */
22547 if (attr && DW_STRING (attr)
22548 && (startswith (DW_STRING (attr), "._")
22549 || startswith (DW_STRING (attr), "<anonymous")))
22550 return NULL;
22551
22552 /* GCC might emit a nameless typedef that has a linkage name. See
22553 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22554 if (!attr || DW_STRING (attr) == NULL)
22555 {
22556 char *demangled = NULL;
22557
22558 attr = dw2_linkage_name_attr (die, cu);
22559 if (attr == NULL || DW_STRING (attr) == NULL)
22560 return NULL;
22561
22562 /* Avoid demangling DW_STRING (attr) the second time on a second
22563 call for the same DIE. */
22564 if (!DW_STRING_IS_CANONICAL (attr))
22565 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22566
22567 if (demangled)
22568 {
22569 const char *base;
22570
22571 /* FIXME: we already did this for the partial symbol... */
22572 DW_STRING (attr)
22573 = ((const char *)
22574 obstack_copy0 (&objfile->per_bfd->storage_obstack,
22575 demangled, strlen (demangled)));
22576 DW_STRING_IS_CANONICAL (attr) = 1;
22577 xfree (demangled);
22578
22579 /* Strip any leading namespaces/classes, keep only the base name.
22580 DW_AT_name for named DIEs does not contain the prefixes. */
22581 base = strrchr (DW_STRING (attr), ':');
22582 if (base && base > DW_STRING (attr) && base[-1] == ':')
22583 return &base[1];
22584 else
22585 return DW_STRING (attr);
22586 }
22587 }
22588 break;
22589
22590 default:
22591 break;
22592 }
22593
22594 if (!DW_STRING_IS_CANONICAL (attr))
22595 {
22596 DW_STRING (attr)
22597 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22598 &objfile->per_bfd->storage_obstack);
22599 DW_STRING_IS_CANONICAL (attr) = 1;
22600 }
22601 return DW_STRING (attr);
22602 }
22603
22604 /* Return the die that this die in an extension of, or NULL if there
22605 is none. *EXT_CU is the CU containing DIE on input, and the CU
22606 containing the return value on output. */
22607
22608 static struct die_info *
22609 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22610 {
22611 struct attribute *attr;
22612
22613 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22614 if (attr == NULL)
22615 return NULL;
22616
22617 return follow_die_ref (die, attr, ext_cu);
22618 }
22619
22620 /* Convert a DIE tag into its string name. */
22621
22622 static const char *
22623 dwarf_tag_name (unsigned tag)
22624 {
22625 const char *name = get_DW_TAG_name (tag);
22626
22627 if (name == NULL)
22628 return "DW_TAG_<unknown>";
22629
22630 return name;
22631 }
22632
22633 /* Convert a DWARF attribute code into its string name. */
22634
22635 static const char *
22636 dwarf_attr_name (unsigned attr)
22637 {
22638 const char *name;
22639
22640 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22641 if (attr == DW_AT_MIPS_fde)
22642 return "DW_AT_MIPS_fde";
22643 #else
22644 if (attr == DW_AT_HP_block_index)
22645 return "DW_AT_HP_block_index";
22646 #endif
22647
22648 name = get_DW_AT_name (attr);
22649
22650 if (name == NULL)
22651 return "DW_AT_<unknown>";
22652
22653 return name;
22654 }
22655
22656 /* Convert a DWARF value form code into its string name. */
22657
22658 static const char *
22659 dwarf_form_name (unsigned form)
22660 {
22661 const char *name = get_DW_FORM_name (form);
22662
22663 if (name == NULL)
22664 return "DW_FORM_<unknown>";
22665
22666 return name;
22667 }
22668
22669 static const char *
22670 dwarf_bool_name (unsigned mybool)
22671 {
22672 if (mybool)
22673 return "TRUE";
22674 else
22675 return "FALSE";
22676 }
22677
22678 /* Convert a DWARF type code into its string name. */
22679
22680 static const char *
22681 dwarf_type_encoding_name (unsigned enc)
22682 {
22683 const char *name = get_DW_ATE_name (enc);
22684
22685 if (name == NULL)
22686 return "DW_ATE_<unknown>";
22687
22688 return name;
22689 }
22690
22691 static void
22692 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22693 {
22694 unsigned int i;
22695
22696 print_spaces (indent, f);
22697 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22698 dwarf_tag_name (die->tag), die->abbrev,
22699 sect_offset_str (die->sect_off));
22700
22701 if (die->parent != NULL)
22702 {
22703 print_spaces (indent, f);
22704 fprintf_unfiltered (f, " parent at offset: %s\n",
22705 sect_offset_str (die->parent->sect_off));
22706 }
22707
22708 print_spaces (indent, f);
22709 fprintf_unfiltered (f, " has children: %s\n",
22710 dwarf_bool_name (die->child != NULL));
22711
22712 print_spaces (indent, f);
22713 fprintf_unfiltered (f, " attributes:\n");
22714
22715 for (i = 0; i < die->num_attrs; ++i)
22716 {
22717 print_spaces (indent, f);
22718 fprintf_unfiltered (f, " %s (%s) ",
22719 dwarf_attr_name (die->attrs[i].name),
22720 dwarf_form_name (die->attrs[i].form));
22721
22722 switch (die->attrs[i].form)
22723 {
22724 case DW_FORM_addr:
22725 case DW_FORM_GNU_addr_index:
22726 fprintf_unfiltered (f, "address: ");
22727 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22728 break;
22729 case DW_FORM_block2:
22730 case DW_FORM_block4:
22731 case DW_FORM_block:
22732 case DW_FORM_block1:
22733 fprintf_unfiltered (f, "block: size %s",
22734 pulongest (DW_BLOCK (&die->attrs[i])->size));
22735 break;
22736 case DW_FORM_exprloc:
22737 fprintf_unfiltered (f, "expression: size %s",
22738 pulongest (DW_BLOCK (&die->attrs[i])->size));
22739 break;
22740 case DW_FORM_data16:
22741 fprintf_unfiltered (f, "constant of 16 bytes");
22742 break;
22743 case DW_FORM_ref_addr:
22744 fprintf_unfiltered (f, "ref address: ");
22745 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22746 break;
22747 case DW_FORM_GNU_ref_alt:
22748 fprintf_unfiltered (f, "alt ref address: ");
22749 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22750 break;
22751 case DW_FORM_ref1:
22752 case DW_FORM_ref2:
22753 case DW_FORM_ref4:
22754 case DW_FORM_ref8:
22755 case DW_FORM_ref_udata:
22756 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22757 (long) (DW_UNSND (&die->attrs[i])));
22758 break;
22759 case DW_FORM_data1:
22760 case DW_FORM_data2:
22761 case DW_FORM_data4:
22762 case DW_FORM_data8:
22763 case DW_FORM_udata:
22764 case DW_FORM_sdata:
22765 fprintf_unfiltered (f, "constant: %s",
22766 pulongest (DW_UNSND (&die->attrs[i])));
22767 break;
22768 case DW_FORM_sec_offset:
22769 fprintf_unfiltered (f, "section offset: %s",
22770 pulongest (DW_UNSND (&die->attrs[i])));
22771 break;
22772 case DW_FORM_ref_sig8:
22773 fprintf_unfiltered (f, "signature: %s",
22774 hex_string (DW_SIGNATURE (&die->attrs[i])));
22775 break;
22776 case DW_FORM_string:
22777 case DW_FORM_strp:
22778 case DW_FORM_line_strp:
22779 case DW_FORM_GNU_str_index:
22780 case DW_FORM_GNU_strp_alt:
22781 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22782 DW_STRING (&die->attrs[i])
22783 ? DW_STRING (&die->attrs[i]) : "",
22784 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22785 break;
22786 case DW_FORM_flag:
22787 if (DW_UNSND (&die->attrs[i]))
22788 fprintf_unfiltered (f, "flag: TRUE");
22789 else
22790 fprintf_unfiltered (f, "flag: FALSE");
22791 break;
22792 case DW_FORM_flag_present:
22793 fprintf_unfiltered (f, "flag: TRUE");
22794 break;
22795 case DW_FORM_indirect:
22796 /* The reader will have reduced the indirect form to
22797 the "base form" so this form should not occur. */
22798 fprintf_unfiltered (f,
22799 "unexpected attribute form: DW_FORM_indirect");
22800 break;
22801 case DW_FORM_implicit_const:
22802 fprintf_unfiltered (f, "constant: %s",
22803 plongest (DW_SND (&die->attrs[i])));
22804 break;
22805 default:
22806 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22807 die->attrs[i].form);
22808 break;
22809 }
22810 fprintf_unfiltered (f, "\n");
22811 }
22812 }
22813
22814 static void
22815 dump_die_for_error (struct die_info *die)
22816 {
22817 dump_die_shallow (gdb_stderr, 0, die);
22818 }
22819
22820 static void
22821 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22822 {
22823 int indent = level * 4;
22824
22825 gdb_assert (die != NULL);
22826
22827 if (level >= max_level)
22828 return;
22829
22830 dump_die_shallow (f, indent, die);
22831
22832 if (die->child != NULL)
22833 {
22834 print_spaces (indent, f);
22835 fprintf_unfiltered (f, " Children:");
22836 if (level + 1 < max_level)
22837 {
22838 fprintf_unfiltered (f, "\n");
22839 dump_die_1 (f, level + 1, max_level, die->child);
22840 }
22841 else
22842 {
22843 fprintf_unfiltered (f,
22844 " [not printed, max nesting level reached]\n");
22845 }
22846 }
22847
22848 if (die->sibling != NULL && level > 0)
22849 {
22850 dump_die_1 (f, level, max_level, die->sibling);
22851 }
22852 }
22853
22854 /* This is called from the pdie macro in gdbinit.in.
22855 It's not static so gcc will keep a copy callable from gdb. */
22856
22857 void
22858 dump_die (struct die_info *die, int max_level)
22859 {
22860 dump_die_1 (gdb_stdlog, 0, max_level, die);
22861 }
22862
22863 static void
22864 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22865 {
22866 void **slot;
22867
22868 slot = htab_find_slot_with_hash (cu->die_hash, die,
22869 to_underlying (die->sect_off),
22870 INSERT);
22871
22872 *slot = die;
22873 }
22874
22875 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22876 required kind. */
22877
22878 static sect_offset
22879 dwarf2_get_ref_die_offset (const struct attribute *attr)
22880 {
22881 if (attr_form_is_ref (attr))
22882 return (sect_offset) DW_UNSND (attr);
22883
22884 complaint (&symfile_complaints,
22885 _("unsupported die ref attribute form: '%s'"),
22886 dwarf_form_name (attr->form));
22887 return {};
22888 }
22889
22890 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22891 * the value held by the attribute is not constant. */
22892
22893 static LONGEST
22894 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22895 {
22896 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22897 return DW_SND (attr);
22898 else if (attr->form == DW_FORM_udata
22899 || attr->form == DW_FORM_data1
22900 || attr->form == DW_FORM_data2
22901 || attr->form == DW_FORM_data4
22902 || attr->form == DW_FORM_data8)
22903 return DW_UNSND (attr);
22904 else
22905 {
22906 /* For DW_FORM_data16 see attr_form_is_constant. */
22907 complaint (&symfile_complaints,
22908 _("Attribute value is not a constant (%s)"),
22909 dwarf_form_name (attr->form));
22910 return default_value;
22911 }
22912 }
22913
22914 /* Follow reference or signature attribute ATTR of SRC_DIE.
22915 On entry *REF_CU is the CU of SRC_DIE.
22916 On exit *REF_CU is the CU of the result. */
22917
22918 static struct die_info *
22919 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22920 struct dwarf2_cu **ref_cu)
22921 {
22922 struct die_info *die;
22923
22924 if (attr_form_is_ref (attr))
22925 die = follow_die_ref (src_die, attr, ref_cu);
22926 else if (attr->form == DW_FORM_ref_sig8)
22927 die = follow_die_sig (src_die, attr, ref_cu);
22928 else
22929 {
22930 dump_die_for_error (src_die);
22931 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22932 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22933 }
22934
22935 return die;
22936 }
22937
22938 /* Follow reference OFFSET.
22939 On entry *REF_CU is the CU of the source die referencing OFFSET.
22940 On exit *REF_CU is the CU of the result.
22941 Returns NULL if OFFSET is invalid. */
22942
22943 static struct die_info *
22944 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22945 struct dwarf2_cu **ref_cu)
22946 {
22947 struct die_info temp_die;
22948 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22949 struct dwarf2_per_objfile *dwarf2_per_objfile
22950 = cu->per_cu->dwarf2_per_objfile;
22951 struct objfile *objfile = dwarf2_per_objfile->objfile;
22952
22953 gdb_assert (cu->per_cu != NULL);
22954
22955 target_cu = cu;
22956
22957 if (cu->per_cu->is_debug_types)
22958 {
22959 /* .debug_types CUs cannot reference anything outside their CU.
22960 If they need to, they have to reference a signatured type via
22961 DW_FORM_ref_sig8. */
22962 if (!offset_in_cu_p (&cu->header, sect_off))
22963 return NULL;
22964 }
22965 else if (offset_in_dwz != cu->per_cu->is_dwz
22966 || !offset_in_cu_p (&cu->header, sect_off))
22967 {
22968 struct dwarf2_per_cu_data *per_cu;
22969
22970 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22971 dwarf2_per_objfile);
22972
22973 /* If necessary, add it to the queue and load its DIEs. */
22974 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22975 load_full_comp_unit (per_cu, cu->language);
22976
22977 target_cu = per_cu->cu;
22978 }
22979 else if (cu->dies == NULL)
22980 {
22981 /* We're loading full DIEs during partial symbol reading. */
22982 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22983 load_full_comp_unit (cu->per_cu, language_minimal);
22984 }
22985
22986 *ref_cu = target_cu;
22987 temp_die.sect_off = sect_off;
22988 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22989 &temp_die,
22990 to_underlying (sect_off));
22991 }
22992
22993 /* Follow reference attribute ATTR of SRC_DIE.
22994 On entry *REF_CU is the CU of SRC_DIE.
22995 On exit *REF_CU is the CU of the result. */
22996
22997 static struct die_info *
22998 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22999 struct dwarf2_cu **ref_cu)
23000 {
23001 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23002 struct dwarf2_cu *cu = *ref_cu;
23003 struct die_info *die;
23004
23005 die = follow_die_offset (sect_off,
23006 (attr->form == DW_FORM_GNU_ref_alt
23007 || cu->per_cu->is_dwz),
23008 ref_cu);
23009 if (!die)
23010 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23011 "at %s [in module %s]"),
23012 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23013 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23014
23015 return die;
23016 }
23017
23018 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23019 Returned value is intended for DW_OP_call*. Returned
23020 dwarf2_locexpr_baton->data has lifetime of
23021 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23022
23023 struct dwarf2_locexpr_baton
23024 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23025 struct dwarf2_per_cu_data *per_cu,
23026 CORE_ADDR (*get_frame_pc) (void *baton),
23027 void *baton)
23028 {
23029 struct dwarf2_cu *cu;
23030 struct die_info *die;
23031 struct attribute *attr;
23032 struct dwarf2_locexpr_baton retval;
23033 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23034 struct dwarf2_per_objfile *dwarf2_per_objfile
23035 = get_dwarf2_per_objfile (objfile);
23036
23037 if (per_cu->cu == NULL)
23038 load_cu (per_cu);
23039 cu = per_cu->cu;
23040 if (cu == NULL)
23041 {
23042 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23043 Instead just throw an error, not much else we can do. */
23044 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23045 sect_offset_str (sect_off), objfile_name (objfile));
23046 }
23047
23048 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23049 if (!die)
23050 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23051 sect_offset_str (sect_off), objfile_name (objfile));
23052
23053 attr = dwarf2_attr (die, DW_AT_location, cu);
23054 if (!attr)
23055 {
23056 /* DWARF: "If there is no such attribute, then there is no effect.".
23057 DATA is ignored if SIZE is 0. */
23058
23059 retval.data = NULL;
23060 retval.size = 0;
23061 }
23062 else if (attr_form_is_section_offset (attr))
23063 {
23064 struct dwarf2_loclist_baton loclist_baton;
23065 CORE_ADDR pc = (*get_frame_pc) (baton);
23066 size_t size;
23067
23068 fill_in_loclist_baton (cu, &loclist_baton, attr);
23069
23070 retval.data = dwarf2_find_location_expression (&loclist_baton,
23071 &size, pc);
23072 retval.size = size;
23073 }
23074 else
23075 {
23076 if (!attr_form_is_block (attr))
23077 error (_("Dwarf Error: DIE at %s referenced in module %s "
23078 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23079 sect_offset_str (sect_off), objfile_name (objfile));
23080
23081 retval.data = DW_BLOCK (attr)->data;
23082 retval.size = DW_BLOCK (attr)->size;
23083 }
23084 retval.per_cu = cu->per_cu;
23085
23086 age_cached_comp_units (dwarf2_per_objfile);
23087
23088 return retval;
23089 }
23090
23091 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23092 offset. */
23093
23094 struct dwarf2_locexpr_baton
23095 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23096 struct dwarf2_per_cu_data *per_cu,
23097 CORE_ADDR (*get_frame_pc) (void *baton),
23098 void *baton)
23099 {
23100 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23101
23102 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23103 }
23104
23105 /* Write a constant of a given type as target-ordered bytes into
23106 OBSTACK. */
23107
23108 static const gdb_byte *
23109 write_constant_as_bytes (struct obstack *obstack,
23110 enum bfd_endian byte_order,
23111 struct type *type,
23112 ULONGEST value,
23113 LONGEST *len)
23114 {
23115 gdb_byte *result;
23116
23117 *len = TYPE_LENGTH (type);
23118 result = (gdb_byte *) obstack_alloc (obstack, *len);
23119 store_unsigned_integer (result, *len, byte_order, value);
23120
23121 return result;
23122 }
23123
23124 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23125 pointer to the constant bytes and set LEN to the length of the
23126 data. If memory is needed, allocate it on OBSTACK. If the DIE
23127 does not have a DW_AT_const_value, return NULL. */
23128
23129 const gdb_byte *
23130 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23131 struct dwarf2_per_cu_data *per_cu,
23132 struct obstack *obstack,
23133 LONGEST *len)
23134 {
23135 struct dwarf2_cu *cu;
23136 struct die_info *die;
23137 struct attribute *attr;
23138 const gdb_byte *result = NULL;
23139 struct type *type;
23140 LONGEST value;
23141 enum bfd_endian byte_order;
23142 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23143
23144 if (per_cu->cu == NULL)
23145 load_cu (per_cu);
23146 cu = per_cu->cu;
23147 if (cu == NULL)
23148 {
23149 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23150 Instead just throw an error, not much else we can do. */
23151 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23152 sect_offset_str (sect_off), objfile_name (objfile));
23153 }
23154
23155 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23156 if (!die)
23157 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23158 sect_offset_str (sect_off), objfile_name (objfile));
23159
23160 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23161 if (attr == NULL)
23162 return NULL;
23163
23164 byte_order = (bfd_big_endian (objfile->obfd)
23165 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23166
23167 switch (attr->form)
23168 {
23169 case DW_FORM_addr:
23170 case DW_FORM_GNU_addr_index:
23171 {
23172 gdb_byte *tem;
23173
23174 *len = cu->header.addr_size;
23175 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23176 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23177 result = tem;
23178 }
23179 break;
23180 case DW_FORM_string:
23181 case DW_FORM_strp:
23182 case DW_FORM_GNU_str_index:
23183 case DW_FORM_GNU_strp_alt:
23184 /* DW_STRING is already allocated on the objfile obstack, point
23185 directly to it. */
23186 result = (const gdb_byte *) DW_STRING (attr);
23187 *len = strlen (DW_STRING (attr));
23188 break;
23189 case DW_FORM_block1:
23190 case DW_FORM_block2:
23191 case DW_FORM_block4:
23192 case DW_FORM_block:
23193 case DW_FORM_exprloc:
23194 case DW_FORM_data16:
23195 result = DW_BLOCK (attr)->data;
23196 *len = DW_BLOCK (attr)->size;
23197 break;
23198
23199 /* The DW_AT_const_value attributes are supposed to carry the
23200 symbol's value "represented as it would be on the target
23201 architecture." By the time we get here, it's already been
23202 converted to host endianness, so we just need to sign- or
23203 zero-extend it as appropriate. */
23204 case DW_FORM_data1:
23205 type = die_type (die, cu);
23206 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23207 if (result == NULL)
23208 result = write_constant_as_bytes (obstack, byte_order,
23209 type, value, len);
23210 break;
23211 case DW_FORM_data2:
23212 type = die_type (die, cu);
23213 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23214 if (result == NULL)
23215 result = write_constant_as_bytes (obstack, byte_order,
23216 type, value, len);
23217 break;
23218 case DW_FORM_data4:
23219 type = die_type (die, cu);
23220 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23221 if (result == NULL)
23222 result = write_constant_as_bytes (obstack, byte_order,
23223 type, value, len);
23224 break;
23225 case DW_FORM_data8:
23226 type = die_type (die, cu);
23227 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23228 if (result == NULL)
23229 result = write_constant_as_bytes (obstack, byte_order,
23230 type, value, len);
23231 break;
23232
23233 case DW_FORM_sdata:
23234 case DW_FORM_implicit_const:
23235 type = die_type (die, cu);
23236 result = write_constant_as_bytes (obstack, byte_order,
23237 type, DW_SND (attr), len);
23238 break;
23239
23240 case DW_FORM_udata:
23241 type = die_type (die, cu);
23242 result = write_constant_as_bytes (obstack, byte_order,
23243 type, DW_UNSND (attr), len);
23244 break;
23245
23246 default:
23247 complaint (&symfile_complaints,
23248 _("unsupported const value attribute form: '%s'"),
23249 dwarf_form_name (attr->form));
23250 break;
23251 }
23252
23253 return result;
23254 }
23255
23256 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23257 valid type for this die is found. */
23258
23259 struct type *
23260 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23261 struct dwarf2_per_cu_data *per_cu)
23262 {
23263 struct dwarf2_cu *cu;
23264 struct die_info *die;
23265
23266 if (per_cu->cu == NULL)
23267 load_cu (per_cu);
23268 cu = per_cu->cu;
23269 if (!cu)
23270 return NULL;
23271
23272 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23273 if (!die)
23274 return NULL;
23275
23276 return die_type (die, cu);
23277 }
23278
23279 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23280 PER_CU. */
23281
23282 struct type *
23283 dwarf2_get_die_type (cu_offset die_offset,
23284 struct dwarf2_per_cu_data *per_cu)
23285 {
23286 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23287 return get_die_type_at_offset (die_offset_sect, per_cu);
23288 }
23289
23290 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23291 On entry *REF_CU is the CU of SRC_DIE.
23292 On exit *REF_CU is the CU of the result.
23293 Returns NULL if the referenced DIE isn't found. */
23294
23295 static struct die_info *
23296 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23297 struct dwarf2_cu **ref_cu)
23298 {
23299 struct die_info temp_die;
23300 struct dwarf2_cu *sig_cu;
23301 struct die_info *die;
23302
23303 /* While it might be nice to assert sig_type->type == NULL here,
23304 we can get here for DW_AT_imported_declaration where we need
23305 the DIE not the type. */
23306
23307 /* If necessary, add it to the queue and load its DIEs. */
23308
23309 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23310 read_signatured_type (sig_type);
23311
23312 sig_cu = sig_type->per_cu.cu;
23313 gdb_assert (sig_cu != NULL);
23314 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23315 temp_die.sect_off = sig_type->type_offset_in_section;
23316 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23317 to_underlying (temp_die.sect_off));
23318 if (die)
23319 {
23320 struct dwarf2_per_objfile *dwarf2_per_objfile
23321 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23322
23323 /* For .gdb_index version 7 keep track of included TUs.
23324 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23325 if (dwarf2_per_objfile->index_table != NULL
23326 && dwarf2_per_objfile->index_table->version <= 7)
23327 {
23328 VEC_safe_push (dwarf2_per_cu_ptr,
23329 (*ref_cu)->per_cu->imported_symtabs,
23330 sig_cu->per_cu);
23331 }
23332
23333 *ref_cu = sig_cu;
23334 return die;
23335 }
23336
23337 return NULL;
23338 }
23339
23340 /* Follow signatured type referenced by ATTR in SRC_DIE.
23341 On entry *REF_CU is the CU of SRC_DIE.
23342 On exit *REF_CU is the CU of the result.
23343 The result is the DIE of the type.
23344 If the referenced type cannot be found an error is thrown. */
23345
23346 static struct die_info *
23347 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23348 struct dwarf2_cu **ref_cu)
23349 {
23350 ULONGEST signature = DW_SIGNATURE (attr);
23351 struct signatured_type *sig_type;
23352 struct die_info *die;
23353
23354 gdb_assert (attr->form == DW_FORM_ref_sig8);
23355
23356 sig_type = lookup_signatured_type (*ref_cu, signature);
23357 /* sig_type will be NULL if the signatured type is missing from
23358 the debug info. */
23359 if (sig_type == NULL)
23360 {
23361 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23362 " from DIE at %s [in module %s]"),
23363 hex_string (signature), sect_offset_str (src_die->sect_off),
23364 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23365 }
23366
23367 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23368 if (die == NULL)
23369 {
23370 dump_die_for_error (src_die);
23371 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23372 " from DIE at %s [in module %s]"),
23373 hex_string (signature), sect_offset_str (src_die->sect_off),
23374 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23375 }
23376
23377 return die;
23378 }
23379
23380 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23381 reading in and processing the type unit if necessary. */
23382
23383 static struct type *
23384 get_signatured_type (struct die_info *die, ULONGEST signature,
23385 struct dwarf2_cu *cu)
23386 {
23387 struct dwarf2_per_objfile *dwarf2_per_objfile
23388 = cu->per_cu->dwarf2_per_objfile;
23389 struct signatured_type *sig_type;
23390 struct dwarf2_cu *type_cu;
23391 struct die_info *type_die;
23392 struct type *type;
23393
23394 sig_type = lookup_signatured_type (cu, signature);
23395 /* sig_type will be NULL if the signatured type is missing from
23396 the debug info. */
23397 if (sig_type == NULL)
23398 {
23399 complaint (&symfile_complaints,
23400 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23401 " from DIE at %s [in module %s]"),
23402 hex_string (signature), sect_offset_str (die->sect_off),
23403 objfile_name (dwarf2_per_objfile->objfile));
23404 return build_error_marker_type (cu, die);
23405 }
23406
23407 /* If we already know the type we're done. */
23408 if (sig_type->type != NULL)
23409 return sig_type->type;
23410
23411 type_cu = cu;
23412 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23413 if (type_die != NULL)
23414 {
23415 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23416 is created. This is important, for example, because for c++ classes
23417 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23418 type = read_type_die (type_die, type_cu);
23419 if (type == NULL)
23420 {
23421 complaint (&symfile_complaints,
23422 _("Dwarf Error: Cannot build signatured type %s"
23423 " referenced from DIE at %s [in module %s]"),
23424 hex_string (signature), sect_offset_str (die->sect_off),
23425 objfile_name (dwarf2_per_objfile->objfile));
23426 type = build_error_marker_type (cu, die);
23427 }
23428 }
23429 else
23430 {
23431 complaint (&symfile_complaints,
23432 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23433 " from DIE at %s [in module %s]"),
23434 hex_string (signature), sect_offset_str (die->sect_off),
23435 objfile_name (dwarf2_per_objfile->objfile));
23436 type = build_error_marker_type (cu, die);
23437 }
23438 sig_type->type = type;
23439
23440 return type;
23441 }
23442
23443 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23444 reading in and processing the type unit if necessary. */
23445
23446 static struct type *
23447 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23448 struct dwarf2_cu *cu) /* ARI: editCase function */
23449 {
23450 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23451 if (attr_form_is_ref (attr))
23452 {
23453 struct dwarf2_cu *type_cu = cu;
23454 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23455
23456 return read_type_die (type_die, type_cu);
23457 }
23458 else if (attr->form == DW_FORM_ref_sig8)
23459 {
23460 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23461 }
23462 else
23463 {
23464 struct dwarf2_per_objfile *dwarf2_per_objfile
23465 = cu->per_cu->dwarf2_per_objfile;
23466
23467 complaint (&symfile_complaints,
23468 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23469 " at %s [in module %s]"),
23470 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23471 objfile_name (dwarf2_per_objfile->objfile));
23472 return build_error_marker_type (cu, die);
23473 }
23474 }
23475
23476 /* Load the DIEs associated with type unit PER_CU into memory. */
23477
23478 static void
23479 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23480 {
23481 struct signatured_type *sig_type;
23482
23483 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23484 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23485
23486 /* We have the per_cu, but we need the signatured_type.
23487 Fortunately this is an easy translation. */
23488 gdb_assert (per_cu->is_debug_types);
23489 sig_type = (struct signatured_type *) per_cu;
23490
23491 gdb_assert (per_cu->cu == NULL);
23492
23493 read_signatured_type (sig_type);
23494
23495 gdb_assert (per_cu->cu != NULL);
23496 }
23497
23498 /* die_reader_func for read_signatured_type.
23499 This is identical to load_full_comp_unit_reader,
23500 but is kept separate for now. */
23501
23502 static void
23503 read_signatured_type_reader (const struct die_reader_specs *reader,
23504 const gdb_byte *info_ptr,
23505 struct die_info *comp_unit_die,
23506 int has_children,
23507 void *data)
23508 {
23509 struct dwarf2_cu *cu = reader->cu;
23510
23511 gdb_assert (cu->die_hash == NULL);
23512 cu->die_hash =
23513 htab_create_alloc_ex (cu->header.length / 12,
23514 die_hash,
23515 die_eq,
23516 NULL,
23517 &cu->comp_unit_obstack,
23518 hashtab_obstack_allocate,
23519 dummy_obstack_deallocate);
23520
23521 if (has_children)
23522 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23523 &info_ptr, comp_unit_die);
23524 cu->dies = comp_unit_die;
23525 /* comp_unit_die is not stored in die_hash, no need. */
23526
23527 /* We try not to read any attributes in this function, because not
23528 all CUs needed for references have been loaded yet, and symbol
23529 table processing isn't initialized. But we have to set the CU language,
23530 or we won't be able to build types correctly.
23531 Similarly, if we do not read the producer, we can not apply
23532 producer-specific interpretation. */
23533 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23534 }
23535
23536 /* Read in a signatured type and build its CU and DIEs.
23537 If the type is a stub for the real type in a DWO file,
23538 read in the real type from the DWO file as well. */
23539
23540 static void
23541 read_signatured_type (struct signatured_type *sig_type)
23542 {
23543 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23544
23545 gdb_assert (per_cu->is_debug_types);
23546 gdb_assert (per_cu->cu == NULL);
23547
23548 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23549 read_signatured_type_reader, NULL);
23550 sig_type->per_cu.tu_read = 1;
23551 }
23552
23553 /* Decode simple location descriptions.
23554 Given a pointer to a dwarf block that defines a location, compute
23555 the location and return the value.
23556
23557 NOTE drow/2003-11-18: This function is called in two situations
23558 now: for the address of static or global variables (partial symbols
23559 only) and for offsets into structures which are expected to be
23560 (more or less) constant. The partial symbol case should go away,
23561 and only the constant case should remain. That will let this
23562 function complain more accurately. A few special modes are allowed
23563 without complaint for global variables (for instance, global
23564 register values and thread-local values).
23565
23566 A location description containing no operations indicates that the
23567 object is optimized out. The return value is 0 for that case.
23568 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23569 callers will only want a very basic result and this can become a
23570 complaint.
23571
23572 Note that stack[0] is unused except as a default error return. */
23573
23574 static CORE_ADDR
23575 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23576 {
23577 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23578 size_t i;
23579 size_t size = blk->size;
23580 const gdb_byte *data = blk->data;
23581 CORE_ADDR stack[64];
23582 int stacki;
23583 unsigned int bytes_read, unsnd;
23584 gdb_byte op;
23585
23586 i = 0;
23587 stacki = 0;
23588 stack[stacki] = 0;
23589 stack[++stacki] = 0;
23590
23591 while (i < size)
23592 {
23593 op = data[i++];
23594 switch (op)
23595 {
23596 case DW_OP_lit0:
23597 case DW_OP_lit1:
23598 case DW_OP_lit2:
23599 case DW_OP_lit3:
23600 case DW_OP_lit4:
23601 case DW_OP_lit5:
23602 case DW_OP_lit6:
23603 case DW_OP_lit7:
23604 case DW_OP_lit8:
23605 case DW_OP_lit9:
23606 case DW_OP_lit10:
23607 case DW_OP_lit11:
23608 case DW_OP_lit12:
23609 case DW_OP_lit13:
23610 case DW_OP_lit14:
23611 case DW_OP_lit15:
23612 case DW_OP_lit16:
23613 case DW_OP_lit17:
23614 case DW_OP_lit18:
23615 case DW_OP_lit19:
23616 case DW_OP_lit20:
23617 case DW_OP_lit21:
23618 case DW_OP_lit22:
23619 case DW_OP_lit23:
23620 case DW_OP_lit24:
23621 case DW_OP_lit25:
23622 case DW_OP_lit26:
23623 case DW_OP_lit27:
23624 case DW_OP_lit28:
23625 case DW_OP_lit29:
23626 case DW_OP_lit30:
23627 case DW_OP_lit31:
23628 stack[++stacki] = op - DW_OP_lit0;
23629 break;
23630
23631 case DW_OP_reg0:
23632 case DW_OP_reg1:
23633 case DW_OP_reg2:
23634 case DW_OP_reg3:
23635 case DW_OP_reg4:
23636 case DW_OP_reg5:
23637 case DW_OP_reg6:
23638 case DW_OP_reg7:
23639 case DW_OP_reg8:
23640 case DW_OP_reg9:
23641 case DW_OP_reg10:
23642 case DW_OP_reg11:
23643 case DW_OP_reg12:
23644 case DW_OP_reg13:
23645 case DW_OP_reg14:
23646 case DW_OP_reg15:
23647 case DW_OP_reg16:
23648 case DW_OP_reg17:
23649 case DW_OP_reg18:
23650 case DW_OP_reg19:
23651 case DW_OP_reg20:
23652 case DW_OP_reg21:
23653 case DW_OP_reg22:
23654 case DW_OP_reg23:
23655 case DW_OP_reg24:
23656 case DW_OP_reg25:
23657 case DW_OP_reg26:
23658 case DW_OP_reg27:
23659 case DW_OP_reg28:
23660 case DW_OP_reg29:
23661 case DW_OP_reg30:
23662 case DW_OP_reg31:
23663 stack[++stacki] = op - DW_OP_reg0;
23664 if (i < size)
23665 dwarf2_complex_location_expr_complaint ();
23666 break;
23667
23668 case DW_OP_regx:
23669 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23670 i += bytes_read;
23671 stack[++stacki] = unsnd;
23672 if (i < size)
23673 dwarf2_complex_location_expr_complaint ();
23674 break;
23675
23676 case DW_OP_addr:
23677 stack[++stacki] = read_address (objfile->obfd, &data[i],
23678 cu, &bytes_read);
23679 i += bytes_read;
23680 break;
23681
23682 case DW_OP_const1u:
23683 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23684 i += 1;
23685 break;
23686
23687 case DW_OP_const1s:
23688 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23689 i += 1;
23690 break;
23691
23692 case DW_OP_const2u:
23693 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23694 i += 2;
23695 break;
23696
23697 case DW_OP_const2s:
23698 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23699 i += 2;
23700 break;
23701
23702 case DW_OP_const4u:
23703 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23704 i += 4;
23705 break;
23706
23707 case DW_OP_const4s:
23708 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23709 i += 4;
23710 break;
23711
23712 case DW_OP_const8u:
23713 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23714 i += 8;
23715 break;
23716
23717 case DW_OP_constu:
23718 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23719 &bytes_read);
23720 i += bytes_read;
23721 break;
23722
23723 case DW_OP_consts:
23724 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23725 i += bytes_read;
23726 break;
23727
23728 case DW_OP_dup:
23729 stack[stacki + 1] = stack[stacki];
23730 stacki++;
23731 break;
23732
23733 case DW_OP_plus:
23734 stack[stacki - 1] += stack[stacki];
23735 stacki--;
23736 break;
23737
23738 case DW_OP_plus_uconst:
23739 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23740 &bytes_read);
23741 i += bytes_read;
23742 break;
23743
23744 case DW_OP_minus:
23745 stack[stacki - 1] -= stack[stacki];
23746 stacki--;
23747 break;
23748
23749 case DW_OP_deref:
23750 /* If we're not the last op, then we definitely can't encode
23751 this using GDB's address_class enum. This is valid for partial
23752 global symbols, although the variable's address will be bogus
23753 in the psymtab. */
23754 if (i < size)
23755 dwarf2_complex_location_expr_complaint ();
23756 break;
23757
23758 case DW_OP_GNU_push_tls_address:
23759 case DW_OP_form_tls_address:
23760 /* The top of the stack has the offset from the beginning
23761 of the thread control block at which the variable is located. */
23762 /* Nothing should follow this operator, so the top of stack would
23763 be returned. */
23764 /* This is valid for partial global symbols, but the variable's
23765 address will be bogus in the psymtab. Make it always at least
23766 non-zero to not look as a variable garbage collected by linker
23767 which have DW_OP_addr 0. */
23768 if (i < size)
23769 dwarf2_complex_location_expr_complaint ();
23770 stack[stacki]++;
23771 break;
23772
23773 case DW_OP_GNU_uninit:
23774 break;
23775
23776 case DW_OP_GNU_addr_index:
23777 case DW_OP_GNU_const_index:
23778 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23779 &bytes_read);
23780 i += bytes_read;
23781 break;
23782
23783 default:
23784 {
23785 const char *name = get_DW_OP_name (op);
23786
23787 if (name)
23788 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23789 name);
23790 else
23791 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23792 op);
23793 }
23794
23795 return (stack[stacki]);
23796 }
23797
23798 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23799 outside of the allocated space. Also enforce minimum>0. */
23800 if (stacki >= ARRAY_SIZE (stack) - 1)
23801 {
23802 complaint (&symfile_complaints,
23803 _("location description stack overflow"));
23804 return 0;
23805 }
23806
23807 if (stacki <= 0)
23808 {
23809 complaint (&symfile_complaints,
23810 _("location description stack underflow"));
23811 return 0;
23812 }
23813 }
23814 return (stack[stacki]);
23815 }
23816
23817 /* memory allocation interface */
23818
23819 static struct dwarf_block *
23820 dwarf_alloc_block (struct dwarf2_cu *cu)
23821 {
23822 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23823 }
23824
23825 static struct die_info *
23826 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23827 {
23828 struct die_info *die;
23829 size_t size = sizeof (struct die_info);
23830
23831 if (num_attrs > 1)
23832 size += (num_attrs - 1) * sizeof (struct attribute);
23833
23834 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23835 memset (die, 0, sizeof (struct die_info));
23836 return (die);
23837 }
23838
23839 \f
23840 /* Macro support. */
23841
23842 /* Return file name relative to the compilation directory of file number I in
23843 *LH's file name table. The result is allocated using xmalloc; the caller is
23844 responsible for freeing it. */
23845
23846 static char *
23847 file_file_name (int file, struct line_header *lh)
23848 {
23849 /* Is the file number a valid index into the line header's file name
23850 table? Remember that file numbers start with one, not zero. */
23851 if (1 <= file && file <= lh->file_names.size ())
23852 {
23853 const file_entry &fe = lh->file_names[file - 1];
23854
23855 if (!IS_ABSOLUTE_PATH (fe.name))
23856 {
23857 const char *dir = fe.include_dir (lh);
23858 if (dir != NULL)
23859 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23860 }
23861 return xstrdup (fe.name);
23862 }
23863 else
23864 {
23865 /* The compiler produced a bogus file number. We can at least
23866 record the macro definitions made in the file, even if we
23867 won't be able to find the file by name. */
23868 char fake_name[80];
23869
23870 xsnprintf (fake_name, sizeof (fake_name),
23871 "<bad macro file number %d>", file);
23872
23873 complaint (&symfile_complaints,
23874 _("bad file number in macro information (%d)"),
23875 file);
23876
23877 return xstrdup (fake_name);
23878 }
23879 }
23880
23881 /* Return the full name of file number I in *LH's file name table.
23882 Use COMP_DIR as the name of the current directory of the
23883 compilation. The result is allocated using xmalloc; the caller is
23884 responsible for freeing it. */
23885 static char *
23886 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23887 {
23888 /* Is the file number a valid index into the line header's file name
23889 table? Remember that file numbers start with one, not zero. */
23890 if (1 <= file && file <= lh->file_names.size ())
23891 {
23892 char *relative = file_file_name (file, lh);
23893
23894 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23895 return relative;
23896 return reconcat (relative, comp_dir, SLASH_STRING,
23897 relative, (char *) NULL);
23898 }
23899 else
23900 return file_file_name (file, lh);
23901 }
23902
23903
23904 static struct macro_source_file *
23905 macro_start_file (int file, int line,
23906 struct macro_source_file *current_file,
23907 struct line_header *lh)
23908 {
23909 /* File name relative to the compilation directory of this source file. */
23910 char *file_name = file_file_name (file, lh);
23911
23912 if (! current_file)
23913 {
23914 /* Note: We don't create a macro table for this compilation unit
23915 at all until we actually get a filename. */
23916 struct macro_table *macro_table = get_macro_table ();
23917
23918 /* If we have no current file, then this must be the start_file
23919 directive for the compilation unit's main source file. */
23920 current_file = macro_set_main (macro_table, file_name);
23921 macro_define_special (macro_table);
23922 }
23923 else
23924 current_file = macro_include (current_file, line, file_name);
23925
23926 xfree (file_name);
23927
23928 return current_file;
23929 }
23930
23931 static const char *
23932 consume_improper_spaces (const char *p, const char *body)
23933 {
23934 if (*p == ' ')
23935 {
23936 complaint (&symfile_complaints,
23937 _("macro definition contains spaces "
23938 "in formal argument list:\n`%s'"),
23939 body);
23940
23941 while (*p == ' ')
23942 p++;
23943 }
23944
23945 return p;
23946 }
23947
23948
23949 static void
23950 parse_macro_definition (struct macro_source_file *file, int line,
23951 const char *body)
23952 {
23953 const char *p;
23954
23955 /* The body string takes one of two forms. For object-like macro
23956 definitions, it should be:
23957
23958 <macro name> " " <definition>
23959
23960 For function-like macro definitions, it should be:
23961
23962 <macro name> "() " <definition>
23963 or
23964 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23965
23966 Spaces may appear only where explicitly indicated, and in the
23967 <definition>.
23968
23969 The Dwarf 2 spec says that an object-like macro's name is always
23970 followed by a space, but versions of GCC around March 2002 omit
23971 the space when the macro's definition is the empty string.
23972
23973 The Dwarf 2 spec says that there should be no spaces between the
23974 formal arguments in a function-like macro's formal argument list,
23975 but versions of GCC around March 2002 include spaces after the
23976 commas. */
23977
23978
23979 /* Find the extent of the macro name. The macro name is terminated
23980 by either a space or null character (for an object-like macro) or
23981 an opening paren (for a function-like macro). */
23982 for (p = body; *p; p++)
23983 if (*p == ' ' || *p == '(')
23984 break;
23985
23986 if (*p == ' ' || *p == '\0')
23987 {
23988 /* It's an object-like macro. */
23989 int name_len = p - body;
23990 char *name = savestring (body, name_len);
23991 const char *replacement;
23992
23993 if (*p == ' ')
23994 replacement = body + name_len + 1;
23995 else
23996 {
23997 dwarf2_macro_malformed_definition_complaint (body);
23998 replacement = body + name_len;
23999 }
24000
24001 macro_define_object (file, line, name, replacement);
24002
24003 xfree (name);
24004 }
24005 else if (*p == '(')
24006 {
24007 /* It's a function-like macro. */
24008 char *name = savestring (body, p - body);
24009 int argc = 0;
24010 int argv_size = 1;
24011 char **argv = XNEWVEC (char *, argv_size);
24012
24013 p++;
24014
24015 p = consume_improper_spaces (p, body);
24016
24017 /* Parse the formal argument list. */
24018 while (*p && *p != ')')
24019 {
24020 /* Find the extent of the current argument name. */
24021 const char *arg_start = p;
24022
24023 while (*p && *p != ',' && *p != ')' && *p != ' ')
24024 p++;
24025
24026 if (! *p || p == arg_start)
24027 dwarf2_macro_malformed_definition_complaint (body);
24028 else
24029 {
24030 /* Make sure argv has room for the new argument. */
24031 if (argc >= argv_size)
24032 {
24033 argv_size *= 2;
24034 argv = XRESIZEVEC (char *, argv, argv_size);
24035 }
24036
24037 argv[argc++] = savestring (arg_start, p - arg_start);
24038 }
24039
24040 p = consume_improper_spaces (p, body);
24041
24042 /* Consume the comma, if present. */
24043 if (*p == ',')
24044 {
24045 p++;
24046
24047 p = consume_improper_spaces (p, body);
24048 }
24049 }
24050
24051 if (*p == ')')
24052 {
24053 p++;
24054
24055 if (*p == ' ')
24056 /* Perfectly formed definition, no complaints. */
24057 macro_define_function (file, line, name,
24058 argc, (const char **) argv,
24059 p + 1);
24060 else if (*p == '\0')
24061 {
24062 /* Complain, but do define it. */
24063 dwarf2_macro_malformed_definition_complaint (body);
24064 macro_define_function (file, line, name,
24065 argc, (const char **) argv,
24066 p);
24067 }
24068 else
24069 /* Just complain. */
24070 dwarf2_macro_malformed_definition_complaint (body);
24071 }
24072 else
24073 /* Just complain. */
24074 dwarf2_macro_malformed_definition_complaint (body);
24075
24076 xfree (name);
24077 {
24078 int i;
24079
24080 for (i = 0; i < argc; i++)
24081 xfree (argv[i]);
24082 }
24083 xfree (argv);
24084 }
24085 else
24086 dwarf2_macro_malformed_definition_complaint (body);
24087 }
24088
24089 /* Skip some bytes from BYTES according to the form given in FORM.
24090 Returns the new pointer. */
24091
24092 static const gdb_byte *
24093 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24094 enum dwarf_form form,
24095 unsigned int offset_size,
24096 struct dwarf2_section_info *section)
24097 {
24098 unsigned int bytes_read;
24099
24100 switch (form)
24101 {
24102 case DW_FORM_data1:
24103 case DW_FORM_flag:
24104 ++bytes;
24105 break;
24106
24107 case DW_FORM_data2:
24108 bytes += 2;
24109 break;
24110
24111 case DW_FORM_data4:
24112 bytes += 4;
24113 break;
24114
24115 case DW_FORM_data8:
24116 bytes += 8;
24117 break;
24118
24119 case DW_FORM_data16:
24120 bytes += 16;
24121 break;
24122
24123 case DW_FORM_string:
24124 read_direct_string (abfd, bytes, &bytes_read);
24125 bytes += bytes_read;
24126 break;
24127
24128 case DW_FORM_sec_offset:
24129 case DW_FORM_strp:
24130 case DW_FORM_GNU_strp_alt:
24131 bytes += offset_size;
24132 break;
24133
24134 case DW_FORM_block:
24135 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24136 bytes += bytes_read;
24137 break;
24138
24139 case DW_FORM_block1:
24140 bytes += 1 + read_1_byte (abfd, bytes);
24141 break;
24142 case DW_FORM_block2:
24143 bytes += 2 + read_2_bytes (abfd, bytes);
24144 break;
24145 case DW_FORM_block4:
24146 bytes += 4 + read_4_bytes (abfd, bytes);
24147 break;
24148
24149 case DW_FORM_sdata:
24150 case DW_FORM_udata:
24151 case DW_FORM_GNU_addr_index:
24152 case DW_FORM_GNU_str_index:
24153 bytes = gdb_skip_leb128 (bytes, buffer_end);
24154 if (bytes == NULL)
24155 {
24156 dwarf2_section_buffer_overflow_complaint (section);
24157 return NULL;
24158 }
24159 break;
24160
24161 case DW_FORM_implicit_const:
24162 break;
24163
24164 default:
24165 {
24166 complaint (&symfile_complaints,
24167 _("invalid form 0x%x in `%s'"),
24168 form, get_section_name (section));
24169 return NULL;
24170 }
24171 }
24172
24173 return bytes;
24174 }
24175
24176 /* A helper for dwarf_decode_macros that handles skipping an unknown
24177 opcode. Returns an updated pointer to the macro data buffer; or,
24178 on error, issues a complaint and returns NULL. */
24179
24180 static const gdb_byte *
24181 skip_unknown_opcode (unsigned int opcode,
24182 const gdb_byte **opcode_definitions,
24183 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24184 bfd *abfd,
24185 unsigned int offset_size,
24186 struct dwarf2_section_info *section)
24187 {
24188 unsigned int bytes_read, i;
24189 unsigned long arg;
24190 const gdb_byte *defn;
24191
24192 if (opcode_definitions[opcode] == NULL)
24193 {
24194 complaint (&symfile_complaints,
24195 _("unrecognized DW_MACFINO opcode 0x%x"),
24196 opcode);
24197 return NULL;
24198 }
24199
24200 defn = opcode_definitions[opcode];
24201 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24202 defn += bytes_read;
24203
24204 for (i = 0; i < arg; ++i)
24205 {
24206 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24207 (enum dwarf_form) defn[i], offset_size,
24208 section);
24209 if (mac_ptr == NULL)
24210 {
24211 /* skip_form_bytes already issued the complaint. */
24212 return NULL;
24213 }
24214 }
24215
24216 return mac_ptr;
24217 }
24218
24219 /* A helper function which parses the header of a macro section.
24220 If the macro section is the extended (for now called "GNU") type,
24221 then this updates *OFFSET_SIZE. Returns a pointer to just after
24222 the header, or issues a complaint and returns NULL on error. */
24223
24224 static const gdb_byte *
24225 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24226 bfd *abfd,
24227 const gdb_byte *mac_ptr,
24228 unsigned int *offset_size,
24229 int section_is_gnu)
24230 {
24231 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24232
24233 if (section_is_gnu)
24234 {
24235 unsigned int version, flags;
24236
24237 version = read_2_bytes (abfd, mac_ptr);
24238 if (version != 4 && version != 5)
24239 {
24240 complaint (&symfile_complaints,
24241 _("unrecognized version `%d' in .debug_macro section"),
24242 version);
24243 return NULL;
24244 }
24245 mac_ptr += 2;
24246
24247 flags = read_1_byte (abfd, mac_ptr);
24248 ++mac_ptr;
24249 *offset_size = (flags & 1) ? 8 : 4;
24250
24251 if ((flags & 2) != 0)
24252 /* We don't need the line table offset. */
24253 mac_ptr += *offset_size;
24254
24255 /* Vendor opcode descriptions. */
24256 if ((flags & 4) != 0)
24257 {
24258 unsigned int i, count;
24259
24260 count = read_1_byte (abfd, mac_ptr);
24261 ++mac_ptr;
24262 for (i = 0; i < count; ++i)
24263 {
24264 unsigned int opcode, bytes_read;
24265 unsigned long arg;
24266
24267 opcode = read_1_byte (abfd, mac_ptr);
24268 ++mac_ptr;
24269 opcode_definitions[opcode] = mac_ptr;
24270 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24271 mac_ptr += bytes_read;
24272 mac_ptr += arg;
24273 }
24274 }
24275 }
24276
24277 return mac_ptr;
24278 }
24279
24280 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24281 including DW_MACRO_import. */
24282
24283 static void
24284 dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24285 bfd *abfd,
24286 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24287 struct macro_source_file *current_file,
24288 struct line_header *lh,
24289 struct dwarf2_section_info *section,
24290 int section_is_gnu, int section_is_dwz,
24291 unsigned int offset_size,
24292 htab_t include_hash)
24293 {
24294 struct objfile *objfile = dwarf2_per_objfile->objfile;
24295 enum dwarf_macro_record_type macinfo_type;
24296 int at_commandline;
24297 const gdb_byte *opcode_definitions[256];
24298
24299 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24300 &offset_size, section_is_gnu);
24301 if (mac_ptr == NULL)
24302 {
24303 /* We already issued a complaint. */
24304 return;
24305 }
24306
24307 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24308 GDB is still reading the definitions from command line. First
24309 DW_MACINFO_start_file will need to be ignored as it was already executed
24310 to create CURRENT_FILE for the main source holding also the command line
24311 definitions. On first met DW_MACINFO_start_file this flag is reset to
24312 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24313
24314 at_commandline = 1;
24315
24316 do
24317 {
24318 /* Do we at least have room for a macinfo type byte? */
24319 if (mac_ptr >= mac_end)
24320 {
24321 dwarf2_section_buffer_overflow_complaint (section);
24322 break;
24323 }
24324
24325 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24326 mac_ptr++;
24327
24328 /* Note that we rely on the fact that the corresponding GNU and
24329 DWARF constants are the same. */
24330 DIAGNOSTIC_PUSH
24331 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24332 switch (macinfo_type)
24333 {
24334 /* A zero macinfo type indicates the end of the macro
24335 information. */
24336 case 0:
24337 break;
24338
24339 case DW_MACRO_define:
24340 case DW_MACRO_undef:
24341 case DW_MACRO_define_strp:
24342 case DW_MACRO_undef_strp:
24343 case DW_MACRO_define_sup:
24344 case DW_MACRO_undef_sup:
24345 {
24346 unsigned int bytes_read;
24347 int line;
24348 const char *body;
24349 int is_define;
24350
24351 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24352 mac_ptr += bytes_read;
24353
24354 if (macinfo_type == DW_MACRO_define
24355 || macinfo_type == DW_MACRO_undef)
24356 {
24357 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24358 mac_ptr += bytes_read;
24359 }
24360 else
24361 {
24362 LONGEST str_offset;
24363
24364 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24365 mac_ptr += offset_size;
24366
24367 if (macinfo_type == DW_MACRO_define_sup
24368 || macinfo_type == DW_MACRO_undef_sup
24369 || section_is_dwz)
24370 {
24371 struct dwz_file *dwz
24372 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24373
24374 body = read_indirect_string_from_dwz (objfile,
24375 dwz, str_offset);
24376 }
24377 else
24378 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24379 abfd, str_offset);
24380 }
24381
24382 is_define = (macinfo_type == DW_MACRO_define
24383 || macinfo_type == DW_MACRO_define_strp
24384 || macinfo_type == DW_MACRO_define_sup);
24385 if (! current_file)
24386 {
24387 /* DWARF violation as no main source is present. */
24388 complaint (&symfile_complaints,
24389 _("debug info with no main source gives macro %s "
24390 "on line %d: %s"),
24391 is_define ? _("definition") : _("undefinition"),
24392 line, body);
24393 break;
24394 }
24395 if ((line == 0 && !at_commandline)
24396 || (line != 0 && at_commandline))
24397 complaint (&symfile_complaints,
24398 _("debug info gives %s macro %s with %s line %d: %s"),
24399 at_commandline ? _("command-line") : _("in-file"),
24400 is_define ? _("definition") : _("undefinition"),
24401 line == 0 ? _("zero") : _("non-zero"), line, body);
24402
24403 if (is_define)
24404 parse_macro_definition (current_file, line, body);
24405 else
24406 {
24407 gdb_assert (macinfo_type == DW_MACRO_undef
24408 || macinfo_type == DW_MACRO_undef_strp
24409 || macinfo_type == DW_MACRO_undef_sup);
24410 macro_undef (current_file, line, body);
24411 }
24412 }
24413 break;
24414
24415 case DW_MACRO_start_file:
24416 {
24417 unsigned int bytes_read;
24418 int line, file;
24419
24420 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24421 mac_ptr += bytes_read;
24422 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24423 mac_ptr += bytes_read;
24424
24425 if ((line == 0 && !at_commandline)
24426 || (line != 0 && at_commandline))
24427 complaint (&symfile_complaints,
24428 _("debug info gives source %d included "
24429 "from %s at %s line %d"),
24430 file, at_commandline ? _("command-line") : _("file"),
24431 line == 0 ? _("zero") : _("non-zero"), line);
24432
24433 if (at_commandline)
24434 {
24435 /* This DW_MACRO_start_file was executed in the
24436 pass one. */
24437 at_commandline = 0;
24438 }
24439 else
24440 current_file = macro_start_file (file, line, current_file, lh);
24441 }
24442 break;
24443
24444 case DW_MACRO_end_file:
24445 if (! current_file)
24446 complaint (&symfile_complaints,
24447 _("macro debug info has an unmatched "
24448 "`close_file' directive"));
24449 else
24450 {
24451 current_file = current_file->included_by;
24452 if (! current_file)
24453 {
24454 enum dwarf_macro_record_type next_type;
24455
24456 /* GCC circa March 2002 doesn't produce the zero
24457 type byte marking the end of the compilation
24458 unit. Complain if it's not there, but exit no
24459 matter what. */
24460
24461 /* Do we at least have room for a macinfo type byte? */
24462 if (mac_ptr >= mac_end)
24463 {
24464 dwarf2_section_buffer_overflow_complaint (section);
24465 return;
24466 }
24467
24468 /* We don't increment mac_ptr here, so this is just
24469 a look-ahead. */
24470 next_type
24471 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24472 mac_ptr);
24473 if (next_type != 0)
24474 complaint (&symfile_complaints,
24475 _("no terminating 0-type entry for "
24476 "macros in `.debug_macinfo' section"));
24477
24478 return;
24479 }
24480 }
24481 break;
24482
24483 case DW_MACRO_import:
24484 case DW_MACRO_import_sup:
24485 {
24486 LONGEST offset;
24487 void **slot;
24488 bfd *include_bfd = abfd;
24489 struct dwarf2_section_info *include_section = section;
24490 const gdb_byte *include_mac_end = mac_end;
24491 int is_dwz = section_is_dwz;
24492 const gdb_byte *new_mac_ptr;
24493
24494 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24495 mac_ptr += offset_size;
24496
24497 if (macinfo_type == DW_MACRO_import_sup)
24498 {
24499 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24500
24501 dwarf2_read_section (objfile, &dwz->macro);
24502
24503 include_section = &dwz->macro;
24504 include_bfd = get_section_bfd_owner (include_section);
24505 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24506 is_dwz = 1;
24507 }
24508
24509 new_mac_ptr = include_section->buffer + offset;
24510 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24511
24512 if (*slot != NULL)
24513 {
24514 /* This has actually happened; see
24515 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24516 complaint (&symfile_complaints,
24517 _("recursive DW_MACRO_import in "
24518 ".debug_macro section"));
24519 }
24520 else
24521 {
24522 *slot = (void *) new_mac_ptr;
24523
24524 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24525 include_bfd, new_mac_ptr,
24526 include_mac_end, current_file, lh,
24527 section, section_is_gnu, is_dwz,
24528 offset_size, include_hash);
24529
24530 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24531 }
24532 }
24533 break;
24534
24535 case DW_MACINFO_vendor_ext:
24536 if (!section_is_gnu)
24537 {
24538 unsigned int bytes_read;
24539
24540 /* This reads the constant, but since we don't recognize
24541 any vendor extensions, we ignore it. */
24542 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24543 mac_ptr += bytes_read;
24544 read_direct_string (abfd, mac_ptr, &bytes_read);
24545 mac_ptr += bytes_read;
24546
24547 /* We don't recognize any vendor extensions. */
24548 break;
24549 }
24550 /* FALLTHROUGH */
24551
24552 default:
24553 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24554 mac_ptr, mac_end, abfd, offset_size,
24555 section);
24556 if (mac_ptr == NULL)
24557 return;
24558 break;
24559 }
24560 DIAGNOSTIC_POP
24561 } while (macinfo_type != 0);
24562 }
24563
24564 static void
24565 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24566 int section_is_gnu)
24567 {
24568 struct dwarf2_per_objfile *dwarf2_per_objfile
24569 = cu->per_cu->dwarf2_per_objfile;
24570 struct objfile *objfile = dwarf2_per_objfile->objfile;
24571 struct line_header *lh = cu->line_header;
24572 bfd *abfd;
24573 const gdb_byte *mac_ptr, *mac_end;
24574 struct macro_source_file *current_file = 0;
24575 enum dwarf_macro_record_type macinfo_type;
24576 unsigned int offset_size = cu->header.offset_size;
24577 const gdb_byte *opcode_definitions[256];
24578 void **slot;
24579 struct dwarf2_section_info *section;
24580 const char *section_name;
24581
24582 if (cu->dwo_unit != NULL)
24583 {
24584 if (section_is_gnu)
24585 {
24586 section = &cu->dwo_unit->dwo_file->sections.macro;
24587 section_name = ".debug_macro.dwo";
24588 }
24589 else
24590 {
24591 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24592 section_name = ".debug_macinfo.dwo";
24593 }
24594 }
24595 else
24596 {
24597 if (section_is_gnu)
24598 {
24599 section = &dwarf2_per_objfile->macro;
24600 section_name = ".debug_macro";
24601 }
24602 else
24603 {
24604 section = &dwarf2_per_objfile->macinfo;
24605 section_name = ".debug_macinfo";
24606 }
24607 }
24608
24609 dwarf2_read_section (objfile, section);
24610 if (section->buffer == NULL)
24611 {
24612 complaint (&symfile_complaints, _("missing %s section"), section_name);
24613 return;
24614 }
24615 abfd = get_section_bfd_owner (section);
24616
24617 /* First pass: Find the name of the base filename.
24618 This filename is needed in order to process all macros whose definition
24619 (or undefinition) comes from the command line. These macros are defined
24620 before the first DW_MACINFO_start_file entry, and yet still need to be
24621 associated to the base file.
24622
24623 To determine the base file name, we scan the macro definitions until we
24624 reach the first DW_MACINFO_start_file entry. We then initialize
24625 CURRENT_FILE accordingly so that any macro definition found before the
24626 first DW_MACINFO_start_file can still be associated to the base file. */
24627
24628 mac_ptr = section->buffer + offset;
24629 mac_end = section->buffer + section->size;
24630
24631 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24632 &offset_size, section_is_gnu);
24633 if (mac_ptr == NULL)
24634 {
24635 /* We already issued a complaint. */
24636 return;
24637 }
24638
24639 do
24640 {
24641 /* Do we at least have room for a macinfo type byte? */
24642 if (mac_ptr >= mac_end)
24643 {
24644 /* Complaint is printed during the second pass as GDB will probably
24645 stop the first pass earlier upon finding
24646 DW_MACINFO_start_file. */
24647 break;
24648 }
24649
24650 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24651 mac_ptr++;
24652
24653 /* Note that we rely on the fact that the corresponding GNU and
24654 DWARF constants are the same. */
24655 DIAGNOSTIC_PUSH
24656 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24657 switch (macinfo_type)
24658 {
24659 /* A zero macinfo type indicates the end of the macro
24660 information. */
24661 case 0:
24662 break;
24663
24664 case DW_MACRO_define:
24665 case DW_MACRO_undef:
24666 /* Only skip the data by MAC_PTR. */
24667 {
24668 unsigned int bytes_read;
24669
24670 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24671 mac_ptr += bytes_read;
24672 read_direct_string (abfd, mac_ptr, &bytes_read);
24673 mac_ptr += bytes_read;
24674 }
24675 break;
24676
24677 case DW_MACRO_start_file:
24678 {
24679 unsigned int bytes_read;
24680 int line, file;
24681
24682 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24683 mac_ptr += bytes_read;
24684 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24685 mac_ptr += bytes_read;
24686
24687 current_file = macro_start_file (file, line, current_file, lh);
24688 }
24689 break;
24690
24691 case DW_MACRO_end_file:
24692 /* No data to skip by MAC_PTR. */
24693 break;
24694
24695 case DW_MACRO_define_strp:
24696 case DW_MACRO_undef_strp:
24697 case DW_MACRO_define_sup:
24698 case DW_MACRO_undef_sup:
24699 {
24700 unsigned int bytes_read;
24701
24702 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24703 mac_ptr += bytes_read;
24704 mac_ptr += offset_size;
24705 }
24706 break;
24707
24708 case DW_MACRO_import:
24709 case DW_MACRO_import_sup:
24710 /* Note that, according to the spec, a transparent include
24711 chain cannot call DW_MACRO_start_file. So, we can just
24712 skip this opcode. */
24713 mac_ptr += offset_size;
24714 break;
24715
24716 case DW_MACINFO_vendor_ext:
24717 /* Only skip the data by MAC_PTR. */
24718 if (!section_is_gnu)
24719 {
24720 unsigned int bytes_read;
24721
24722 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24723 mac_ptr += bytes_read;
24724 read_direct_string (abfd, mac_ptr, &bytes_read);
24725 mac_ptr += bytes_read;
24726 }
24727 /* FALLTHROUGH */
24728
24729 default:
24730 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24731 mac_ptr, mac_end, abfd, offset_size,
24732 section);
24733 if (mac_ptr == NULL)
24734 return;
24735 break;
24736 }
24737 DIAGNOSTIC_POP
24738 } while (macinfo_type != 0 && current_file == NULL);
24739
24740 /* Second pass: Process all entries.
24741
24742 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24743 command-line macro definitions/undefinitions. This flag is unset when we
24744 reach the first DW_MACINFO_start_file entry. */
24745
24746 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24747 htab_eq_pointer,
24748 NULL, xcalloc, xfree));
24749 mac_ptr = section->buffer + offset;
24750 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24751 *slot = (void *) mac_ptr;
24752 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24753 abfd, mac_ptr, mac_end,
24754 current_file, lh, section,
24755 section_is_gnu, 0, offset_size,
24756 include_hash.get ());
24757 }
24758
24759 /* Check if the attribute's form is a DW_FORM_block*
24760 if so return true else false. */
24761
24762 static int
24763 attr_form_is_block (const struct attribute *attr)
24764 {
24765 return (attr == NULL ? 0 :
24766 attr->form == DW_FORM_block1
24767 || attr->form == DW_FORM_block2
24768 || attr->form == DW_FORM_block4
24769 || attr->form == DW_FORM_block
24770 || attr->form == DW_FORM_exprloc);
24771 }
24772
24773 /* Return non-zero if ATTR's value is a section offset --- classes
24774 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24775 You may use DW_UNSND (attr) to retrieve such offsets.
24776
24777 Section 7.5.4, "Attribute Encodings", explains that no attribute
24778 may have a value that belongs to more than one of these classes; it
24779 would be ambiguous if we did, because we use the same forms for all
24780 of them. */
24781
24782 static int
24783 attr_form_is_section_offset (const struct attribute *attr)
24784 {
24785 return (attr->form == DW_FORM_data4
24786 || attr->form == DW_FORM_data8
24787 || attr->form == DW_FORM_sec_offset);
24788 }
24789
24790 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24791 zero otherwise. When this function returns true, you can apply
24792 dwarf2_get_attr_constant_value to it.
24793
24794 However, note that for some attributes you must check
24795 attr_form_is_section_offset before using this test. DW_FORM_data4
24796 and DW_FORM_data8 are members of both the constant class, and of
24797 the classes that contain offsets into other debug sections
24798 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24799 that, if an attribute's can be either a constant or one of the
24800 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
24801 taken as section offsets, not constants.
24802
24803 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24804 cannot handle that. */
24805
24806 static int
24807 attr_form_is_constant (const struct attribute *attr)
24808 {
24809 switch (attr->form)
24810 {
24811 case DW_FORM_sdata:
24812 case DW_FORM_udata:
24813 case DW_FORM_data1:
24814 case DW_FORM_data2:
24815 case DW_FORM_data4:
24816 case DW_FORM_data8:
24817 case DW_FORM_implicit_const:
24818 return 1;
24819 default:
24820 return 0;
24821 }
24822 }
24823
24824
24825 /* DW_ADDR is always stored already as sect_offset; despite for the forms
24826 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24827
24828 static int
24829 attr_form_is_ref (const struct attribute *attr)
24830 {
24831 switch (attr->form)
24832 {
24833 case DW_FORM_ref_addr:
24834 case DW_FORM_ref1:
24835 case DW_FORM_ref2:
24836 case DW_FORM_ref4:
24837 case DW_FORM_ref8:
24838 case DW_FORM_ref_udata:
24839 case DW_FORM_GNU_ref_alt:
24840 return 1;
24841 default:
24842 return 0;
24843 }
24844 }
24845
24846 /* Return the .debug_loc section to use for CU.
24847 For DWO files use .debug_loc.dwo. */
24848
24849 static struct dwarf2_section_info *
24850 cu_debug_loc_section (struct dwarf2_cu *cu)
24851 {
24852 struct dwarf2_per_objfile *dwarf2_per_objfile
24853 = cu->per_cu->dwarf2_per_objfile;
24854
24855 if (cu->dwo_unit)
24856 {
24857 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24858
24859 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24860 }
24861 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24862 : &dwarf2_per_objfile->loc);
24863 }
24864
24865 /* A helper function that fills in a dwarf2_loclist_baton. */
24866
24867 static void
24868 fill_in_loclist_baton (struct dwarf2_cu *cu,
24869 struct dwarf2_loclist_baton *baton,
24870 const struct attribute *attr)
24871 {
24872 struct dwarf2_per_objfile *dwarf2_per_objfile
24873 = cu->per_cu->dwarf2_per_objfile;
24874 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24875
24876 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
24877
24878 baton->per_cu = cu->per_cu;
24879 gdb_assert (baton->per_cu);
24880 /* We don't know how long the location list is, but make sure we
24881 don't run off the edge of the section. */
24882 baton->size = section->size - DW_UNSND (attr);
24883 baton->data = section->buffer + DW_UNSND (attr);
24884 baton->base_address = cu->base_address;
24885 baton->from_dwo = cu->dwo_unit != NULL;
24886 }
24887
24888 static void
24889 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24890 struct dwarf2_cu *cu, int is_block)
24891 {
24892 struct dwarf2_per_objfile *dwarf2_per_objfile
24893 = cu->per_cu->dwarf2_per_objfile;
24894 struct objfile *objfile = dwarf2_per_objfile->objfile;
24895 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24896
24897 if (attr_form_is_section_offset (attr)
24898 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24899 the section. If so, fall through to the complaint in the
24900 other branch. */
24901 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24902 {
24903 struct dwarf2_loclist_baton *baton;
24904
24905 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24906
24907 fill_in_loclist_baton (cu, baton, attr);
24908
24909 if (cu->base_known == 0)
24910 complaint (&symfile_complaints,
24911 _("Location list used without "
24912 "specifying the CU base address."));
24913
24914 SYMBOL_ACLASS_INDEX (sym) = (is_block
24915 ? dwarf2_loclist_block_index
24916 : dwarf2_loclist_index);
24917 SYMBOL_LOCATION_BATON (sym) = baton;
24918 }
24919 else
24920 {
24921 struct dwarf2_locexpr_baton *baton;
24922
24923 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24924 baton->per_cu = cu->per_cu;
24925 gdb_assert (baton->per_cu);
24926
24927 if (attr_form_is_block (attr))
24928 {
24929 /* Note that we're just copying the block's data pointer
24930 here, not the actual data. We're still pointing into the
24931 info_buffer for SYM's objfile; right now we never release
24932 that buffer, but when we do clean up properly this may
24933 need to change. */
24934 baton->size = DW_BLOCK (attr)->size;
24935 baton->data = DW_BLOCK (attr)->data;
24936 }
24937 else
24938 {
24939 dwarf2_invalid_attrib_class_complaint ("location description",
24940 SYMBOL_NATURAL_NAME (sym));
24941 baton->size = 0;
24942 }
24943
24944 SYMBOL_ACLASS_INDEX (sym) = (is_block
24945 ? dwarf2_locexpr_block_index
24946 : dwarf2_locexpr_index);
24947 SYMBOL_LOCATION_BATON (sym) = baton;
24948 }
24949 }
24950
24951 /* Return the OBJFILE associated with the compilation unit CU. If CU
24952 came from a separate debuginfo file, then the master objfile is
24953 returned. */
24954
24955 struct objfile *
24956 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24957 {
24958 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24959
24960 /* Return the master objfile, so that we can report and look up the
24961 correct file containing this variable. */
24962 if (objfile->separate_debug_objfile_backlink)
24963 objfile = objfile->separate_debug_objfile_backlink;
24964
24965 return objfile;
24966 }
24967
24968 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24969 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24970 CU_HEADERP first. */
24971
24972 static const struct comp_unit_head *
24973 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24974 struct dwarf2_per_cu_data *per_cu)
24975 {
24976 const gdb_byte *info_ptr;
24977
24978 if (per_cu->cu)
24979 return &per_cu->cu->header;
24980
24981 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24982
24983 memset (cu_headerp, 0, sizeof (*cu_headerp));
24984 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24985 rcuh_kind::COMPILE);
24986
24987 return cu_headerp;
24988 }
24989
24990 /* Return the address size given in the compilation unit header for CU. */
24991
24992 int
24993 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24994 {
24995 struct comp_unit_head cu_header_local;
24996 const struct comp_unit_head *cu_headerp;
24997
24998 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24999
25000 return cu_headerp->addr_size;
25001 }
25002
25003 /* Return the offset size given in the compilation unit header for CU. */
25004
25005 int
25006 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25007 {
25008 struct comp_unit_head cu_header_local;
25009 const struct comp_unit_head *cu_headerp;
25010
25011 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25012
25013 return cu_headerp->offset_size;
25014 }
25015
25016 /* See its dwarf2loc.h declaration. */
25017
25018 int
25019 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25020 {
25021 struct comp_unit_head cu_header_local;
25022 const struct comp_unit_head *cu_headerp;
25023
25024 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25025
25026 if (cu_headerp->version == 2)
25027 return cu_headerp->addr_size;
25028 else
25029 return cu_headerp->offset_size;
25030 }
25031
25032 /* Return the text offset of the CU. The returned offset comes from
25033 this CU's objfile. If this objfile came from a separate debuginfo
25034 file, then the offset may be different from the corresponding
25035 offset in the parent objfile. */
25036
25037 CORE_ADDR
25038 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25039 {
25040 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25041
25042 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25043 }
25044
25045 /* Return DWARF version number of PER_CU. */
25046
25047 short
25048 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25049 {
25050 return per_cu->dwarf_version;
25051 }
25052
25053 /* Locate the .debug_info compilation unit from CU's objfile which contains
25054 the DIE at OFFSET. Raises an error on failure. */
25055
25056 static struct dwarf2_per_cu_data *
25057 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25058 unsigned int offset_in_dwz,
25059 struct dwarf2_per_objfile *dwarf2_per_objfile)
25060 {
25061 struct dwarf2_per_cu_data *this_cu;
25062 int low, high;
25063 const sect_offset *cu_off;
25064
25065 low = 0;
25066 high = dwarf2_per_objfile->n_comp_units - 1;
25067 while (high > low)
25068 {
25069 struct dwarf2_per_cu_data *mid_cu;
25070 int mid = low + (high - low) / 2;
25071
25072 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25073 cu_off = &mid_cu->sect_off;
25074 if (mid_cu->is_dwz > offset_in_dwz
25075 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
25076 high = mid;
25077 else
25078 low = mid + 1;
25079 }
25080 gdb_assert (low == high);
25081 this_cu = dwarf2_per_objfile->all_comp_units[low];
25082 cu_off = &this_cu->sect_off;
25083 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
25084 {
25085 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25086 error (_("Dwarf Error: could not find partial DIE containing "
25087 "offset %s [in module %s]"),
25088 sect_offset_str (sect_off),
25089 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25090
25091 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25092 <= sect_off);
25093 return dwarf2_per_objfile->all_comp_units[low-1];
25094 }
25095 else
25096 {
25097 this_cu = dwarf2_per_objfile->all_comp_units[low];
25098 if (low == dwarf2_per_objfile->n_comp_units - 1
25099 && sect_off >= this_cu->sect_off + this_cu->length)
25100 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25101 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25102 return this_cu;
25103 }
25104 }
25105
25106 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25107
25108 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25109 : per_cu (per_cu_),
25110 mark (0),
25111 has_loclist (0),
25112 checked_producer (0),
25113 producer_is_gxx_lt_4_6 (0),
25114 producer_is_gcc_lt_4_3 (0),
25115 producer_is_icc_lt_14 (0),
25116 processing_has_namespace_info (0)
25117 {
25118 per_cu->cu = this;
25119 }
25120
25121 /* Destroy a dwarf2_cu. */
25122
25123 dwarf2_cu::~dwarf2_cu ()
25124 {
25125 per_cu->cu = NULL;
25126 }
25127
25128 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25129
25130 static void
25131 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25132 enum language pretend_language)
25133 {
25134 struct attribute *attr;
25135
25136 /* Set the language we're debugging. */
25137 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25138 if (attr)
25139 set_cu_language (DW_UNSND (attr), cu);
25140 else
25141 {
25142 cu->language = pretend_language;
25143 cu->language_defn = language_def (cu->language);
25144 }
25145
25146 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25147 }
25148
25149 /* Free all cached compilation units. */
25150
25151 static void
25152 free_cached_comp_units (void *data)
25153 {
25154 struct dwarf2_per_objfile *dwarf2_per_objfile
25155 = (struct dwarf2_per_objfile *) data;
25156
25157 dwarf2_per_objfile->free_cached_comp_units ();
25158 }
25159
25160 /* Increase the age counter on each cached compilation unit, and free
25161 any that are too old. */
25162
25163 static void
25164 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25165 {
25166 struct dwarf2_per_cu_data *per_cu, **last_chain;
25167
25168 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25169 per_cu = dwarf2_per_objfile->read_in_chain;
25170 while (per_cu != NULL)
25171 {
25172 per_cu->cu->last_used ++;
25173 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25174 dwarf2_mark (per_cu->cu);
25175 per_cu = per_cu->cu->read_in_chain;
25176 }
25177
25178 per_cu = dwarf2_per_objfile->read_in_chain;
25179 last_chain = &dwarf2_per_objfile->read_in_chain;
25180 while (per_cu != NULL)
25181 {
25182 struct dwarf2_per_cu_data *next_cu;
25183
25184 next_cu = per_cu->cu->read_in_chain;
25185
25186 if (!per_cu->cu->mark)
25187 {
25188 delete per_cu->cu;
25189 *last_chain = next_cu;
25190 }
25191 else
25192 last_chain = &per_cu->cu->read_in_chain;
25193
25194 per_cu = next_cu;
25195 }
25196 }
25197
25198 /* Remove a single compilation unit from the cache. */
25199
25200 static void
25201 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25202 {
25203 struct dwarf2_per_cu_data *per_cu, **last_chain;
25204 struct dwarf2_per_objfile *dwarf2_per_objfile
25205 = target_per_cu->dwarf2_per_objfile;
25206
25207 per_cu = dwarf2_per_objfile->read_in_chain;
25208 last_chain = &dwarf2_per_objfile->read_in_chain;
25209 while (per_cu != NULL)
25210 {
25211 struct dwarf2_per_cu_data *next_cu;
25212
25213 next_cu = per_cu->cu->read_in_chain;
25214
25215 if (per_cu == target_per_cu)
25216 {
25217 delete per_cu->cu;
25218 per_cu->cu = NULL;
25219 *last_chain = next_cu;
25220 break;
25221 }
25222 else
25223 last_chain = &per_cu->cu->read_in_chain;
25224
25225 per_cu = next_cu;
25226 }
25227 }
25228
25229 /* Release all extra memory associated with OBJFILE. */
25230
25231 void
25232 dwarf2_free_objfile (struct objfile *objfile)
25233 {
25234 struct dwarf2_per_objfile *dwarf2_per_objfile
25235 = get_dwarf2_per_objfile (objfile);
25236
25237 delete dwarf2_per_objfile;
25238 }
25239
25240 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25241 We store these in a hash table separate from the DIEs, and preserve them
25242 when the DIEs are flushed out of cache.
25243
25244 The CU "per_cu" pointer is needed because offset alone is not enough to
25245 uniquely identify the type. A file may have multiple .debug_types sections,
25246 or the type may come from a DWO file. Furthermore, while it's more logical
25247 to use per_cu->section+offset, with Fission the section with the data is in
25248 the DWO file but we don't know that section at the point we need it.
25249 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25250 because we can enter the lookup routine, get_die_type_at_offset, from
25251 outside this file, and thus won't necessarily have PER_CU->cu.
25252 Fortunately, PER_CU is stable for the life of the objfile. */
25253
25254 struct dwarf2_per_cu_offset_and_type
25255 {
25256 const struct dwarf2_per_cu_data *per_cu;
25257 sect_offset sect_off;
25258 struct type *type;
25259 };
25260
25261 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25262
25263 static hashval_t
25264 per_cu_offset_and_type_hash (const void *item)
25265 {
25266 const struct dwarf2_per_cu_offset_and_type *ofs
25267 = (const struct dwarf2_per_cu_offset_and_type *) item;
25268
25269 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25270 }
25271
25272 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25273
25274 static int
25275 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25276 {
25277 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25278 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25279 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25280 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25281
25282 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25283 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25284 }
25285
25286 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25287 table if necessary. For convenience, return TYPE.
25288
25289 The DIEs reading must have careful ordering to:
25290 * Not cause infite loops trying to read in DIEs as a prerequisite for
25291 reading current DIE.
25292 * Not trying to dereference contents of still incompletely read in types
25293 while reading in other DIEs.
25294 * Enable referencing still incompletely read in types just by a pointer to
25295 the type without accessing its fields.
25296
25297 Therefore caller should follow these rules:
25298 * Try to fetch any prerequisite types we may need to build this DIE type
25299 before building the type and calling set_die_type.
25300 * After building type call set_die_type for current DIE as soon as
25301 possible before fetching more types to complete the current type.
25302 * Make the type as complete as possible before fetching more types. */
25303
25304 static struct type *
25305 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25306 {
25307 struct dwarf2_per_objfile *dwarf2_per_objfile
25308 = cu->per_cu->dwarf2_per_objfile;
25309 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25310 struct objfile *objfile = dwarf2_per_objfile->objfile;
25311 struct attribute *attr;
25312 struct dynamic_prop prop;
25313
25314 /* For Ada types, make sure that the gnat-specific data is always
25315 initialized (if not already set). There are a few types where
25316 we should not be doing so, because the type-specific area is
25317 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25318 where the type-specific area is used to store the floatformat).
25319 But this is not a problem, because the gnat-specific information
25320 is actually not needed for these types. */
25321 if (need_gnat_info (cu)
25322 && TYPE_CODE (type) != TYPE_CODE_FUNC
25323 && TYPE_CODE (type) != TYPE_CODE_FLT
25324 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25325 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25326 && TYPE_CODE (type) != TYPE_CODE_METHOD
25327 && !HAVE_GNAT_AUX_INFO (type))
25328 INIT_GNAT_SPECIFIC (type);
25329
25330 /* Read DW_AT_allocated and set in type. */
25331 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25332 if (attr_form_is_block (attr))
25333 {
25334 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25335 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25336 }
25337 else if (attr != NULL)
25338 {
25339 complaint (&symfile_complaints,
25340 _("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25341 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25342 sect_offset_str (die->sect_off));
25343 }
25344
25345 /* Read DW_AT_associated and set in type. */
25346 attr = dwarf2_attr (die, DW_AT_associated, cu);
25347 if (attr_form_is_block (attr))
25348 {
25349 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25350 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25351 }
25352 else if (attr != NULL)
25353 {
25354 complaint (&symfile_complaints,
25355 _("DW_AT_associated has the wrong form (%s) at DIE %s"),
25356 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25357 sect_offset_str (die->sect_off));
25358 }
25359
25360 /* Read DW_AT_data_location and set in type. */
25361 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25362 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25363 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25364
25365 if (dwarf2_per_objfile->die_type_hash == NULL)
25366 {
25367 dwarf2_per_objfile->die_type_hash =
25368 htab_create_alloc_ex (127,
25369 per_cu_offset_and_type_hash,
25370 per_cu_offset_and_type_eq,
25371 NULL,
25372 &objfile->objfile_obstack,
25373 hashtab_obstack_allocate,
25374 dummy_obstack_deallocate);
25375 }
25376
25377 ofs.per_cu = cu->per_cu;
25378 ofs.sect_off = die->sect_off;
25379 ofs.type = type;
25380 slot = (struct dwarf2_per_cu_offset_and_type **)
25381 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25382 if (*slot)
25383 complaint (&symfile_complaints,
25384 _("A problem internal to GDB: DIE %s has type already set"),
25385 sect_offset_str (die->sect_off));
25386 *slot = XOBNEW (&objfile->objfile_obstack,
25387 struct dwarf2_per_cu_offset_and_type);
25388 **slot = ofs;
25389 return type;
25390 }
25391
25392 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25393 or return NULL if the die does not have a saved type. */
25394
25395 static struct type *
25396 get_die_type_at_offset (sect_offset sect_off,
25397 struct dwarf2_per_cu_data *per_cu)
25398 {
25399 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25400 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25401
25402 if (dwarf2_per_objfile->die_type_hash == NULL)
25403 return NULL;
25404
25405 ofs.per_cu = per_cu;
25406 ofs.sect_off = sect_off;
25407 slot = ((struct dwarf2_per_cu_offset_and_type *)
25408 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25409 if (slot)
25410 return slot->type;
25411 else
25412 return NULL;
25413 }
25414
25415 /* Look up the type for DIE in CU in die_type_hash,
25416 or return NULL if DIE does not have a saved type. */
25417
25418 static struct type *
25419 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25420 {
25421 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25422 }
25423
25424 /* Add a dependence relationship from CU to REF_PER_CU. */
25425
25426 static void
25427 dwarf2_add_dependence (struct dwarf2_cu *cu,
25428 struct dwarf2_per_cu_data *ref_per_cu)
25429 {
25430 void **slot;
25431
25432 if (cu->dependencies == NULL)
25433 cu->dependencies
25434 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25435 NULL, &cu->comp_unit_obstack,
25436 hashtab_obstack_allocate,
25437 dummy_obstack_deallocate);
25438
25439 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25440 if (*slot == NULL)
25441 *slot = ref_per_cu;
25442 }
25443
25444 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25445 Set the mark field in every compilation unit in the
25446 cache that we must keep because we are keeping CU. */
25447
25448 static int
25449 dwarf2_mark_helper (void **slot, void *data)
25450 {
25451 struct dwarf2_per_cu_data *per_cu;
25452
25453 per_cu = (struct dwarf2_per_cu_data *) *slot;
25454
25455 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25456 reading of the chain. As such dependencies remain valid it is not much
25457 useful to track and undo them during QUIT cleanups. */
25458 if (per_cu->cu == NULL)
25459 return 1;
25460
25461 if (per_cu->cu->mark)
25462 return 1;
25463 per_cu->cu->mark = 1;
25464
25465 if (per_cu->cu->dependencies != NULL)
25466 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25467
25468 return 1;
25469 }
25470
25471 /* Set the mark field in CU and in every other compilation unit in the
25472 cache that we must keep because we are keeping CU. */
25473
25474 static void
25475 dwarf2_mark (struct dwarf2_cu *cu)
25476 {
25477 if (cu->mark)
25478 return;
25479 cu->mark = 1;
25480 if (cu->dependencies != NULL)
25481 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25482 }
25483
25484 static void
25485 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25486 {
25487 while (per_cu)
25488 {
25489 per_cu->cu->mark = 0;
25490 per_cu = per_cu->cu->read_in_chain;
25491 }
25492 }
25493
25494 /* Trivial hash function for partial_die_info: the hash value of a DIE
25495 is its offset in .debug_info for this objfile. */
25496
25497 static hashval_t
25498 partial_die_hash (const void *item)
25499 {
25500 const struct partial_die_info *part_die
25501 = (const struct partial_die_info *) item;
25502
25503 return to_underlying (part_die->sect_off);
25504 }
25505
25506 /* Trivial comparison function for partial_die_info structures: two DIEs
25507 are equal if they have the same offset. */
25508
25509 static int
25510 partial_die_eq (const void *item_lhs, const void *item_rhs)
25511 {
25512 const struct partial_die_info *part_die_lhs
25513 = (const struct partial_die_info *) item_lhs;
25514 const struct partial_die_info *part_die_rhs
25515 = (const struct partial_die_info *) item_rhs;
25516
25517 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25518 }
25519
25520 static struct cmd_list_element *set_dwarf_cmdlist;
25521 static struct cmd_list_element *show_dwarf_cmdlist;
25522
25523 static void
25524 set_dwarf_cmd (const char *args, int from_tty)
25525 {
25526 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25527 gdb_stdout);
25528 }
25529
25530 static void
25531 show_dwarf_cmd (const char *args, int from_tty)
25532 {
25533 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25534 }
25535
25536 /* The "save gdb-index" command. */
25537
25538 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25539 error checking. */
25540
25541 static void
25542 file_write (FILE *file, const void *data, size_t size)
25543 {
25544 if (fwrite (data, 1, size, file) != size)
25545 error (_("couldn't data write to file"));
25546 }
25547
25548 /* Write the contents of VEC to FILE, with error checking. */
25549
25550 template<typename Elem, typename Alloc>
25551 static void
25552 file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25553 {
25554 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25555 }
25556
25557 /* In-memory buffer to prepare data to be written later to a file. */
25558 class data_buf
25559 {
25560 public:
25561 /* Copy DATA to the end of the buffer. */
25562 template<typename T>
25563 void append_data (const T &data)
25564 {
25565 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25566 reinterpret_cast<const gdb_byte *> (&data + 1),
25567 grow (sizeof (data)));
25568 }
25569
25570 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25571 terminating zero is appended too. */
25572 void append_cstr0 (const char *cstr)
25573 {
25574 const size_t size = strlen (cstr) + 1;
25575 std::copy (cstr, cstr + size, grow (size));
25576 }
25577
25578 /* Store INPUT as ULEB128 to the end of buffer. */
25579 void append_unsigned_leb128 (ULONGEST input)
25580 {
25581 for (;;)
25582 {
25583 gdb_byte output = input & 0x7f;
25584 input >>= 7;
25585 if (input)
25586 output |= 0x80;
25587 append_data (output);
25588 if (input == 0)
25589 break;
25590 }
25591 }
25592
25593 /* Accept a host-format integer in VAL and append it to the buffer
25594 as a target-format integer which is LEN bytes long. */
25595 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25596 {
25597 ::store_unsigned_integer (grow (len), len, byte_order, val);
25598 }
25599
25600 /* Return the size of the buffer. */
25601 size_t size () const
25602 {
25603 return m_vec.size ();
25604 }
25605
25606 /* Return true iff the buffer is empty. */
25607 bool empty () const
25608 {
25609 return m_vec.empty ();
25610 }
25611
25612 /* Write the buffer to FILE. */
25613 void file_write (FILE *file) const
25614 {
25615 ::file_write (file, m_vec);
25616 }
25617
25618 private:
25619 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25620 the start of the new block. */
25621 gdb_byte *grow (size_t size)
25622 {
25623 m_vec.resize (m_vec.size () + size);
25624 return &*m_vec.end () - size;
25625 }
25626
25627 gdb::byte_vector m_vec;
25628 };
25629
25630 /* An entry in the symbol table. */
25631 struct symtab_index_entry
25632 {
25633 /* The name of the symbol. */
25634 const char *name;
25635 /* The offset of the name in the constant pool. */
25636 offset_type index_offset;
25637 /* A sorted vector of the indices of all the CUs that hold an object
25638 of this name. */
25639 std::vector<offset_type> cu_indices;
25640 };
25641
25642 /* The symbol table. This is a power-of-2-sized hash table. */
25643 struct mapped_symtab
25644 {
25645 mapped_symtab ()
25646 {
25647 data.resize (1024);
25648 }
25649
25650 offset_type n_elements = 0;
25651 std::vector<symtab_index_entry> data;
25652 };
25653
25654 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
25655 the slot.
25656
25657 Function is used only during write_hash_table so no index format backward
25658 compatibility is needed. */
25659
25660 static symtab_index_entry &
25661 find_slot (struct mapped_symtab *symtab, const char *name)
25662 {
25663 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
25664
25665 index = hash & (symtab->data.size () - 1);
25666 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
25667
25668 for (;;)
25669 {
25670 if (symtab->data[index].name == NULL
25671 || strcmp (name, symtab->data[index].name) == 0)
25672 return symtab->data[index];
25673 index = (index + step) & (symtab->data.size () - 1);
25674 }
25675 }
25676
25677 /* Expand SYMTAB's hash table. */
25678
25679 static void
25680 hash_expand (struct mapped_symtab *symtab)
25681 {
25682 auto old_entries = std::move (symtab->data);
25683
25684 symtab->data.clear ();
25685 symtab->data.resize (old_entries.size () * 2);
25686
25687 for (auto &it : old_entries)
25688 if (it.name != NULL)
25689 {
25690 auto &ref = find_slot (symtab, it.name);
25691 ref = std::move (it);
25692 }
25693 }
25694
25695 /* Add an entry to SYMTAB. NAME is the name of the symbol.
25696 CU_INDEX is the index of the CU in which the symbol appears.
25697 IS_STATIC is one if the symbol is static, otherwise zero (global). */
25698
25699 static void
25700 add_index_entry (struct mapped_symtab *symtab, const char *name,
25701 int is_static, gdb_index_symbol_kind kind,
25702 offset_type cu_index)
25703 {
25704 offset_type cu_index_and_attrs;
25705
25706 ++symtab->n_elements;
25707 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
25708 hash_expand (symtab);
25709
25710 symtab_index_entry &slot = find_slot (symtab, name);
25711 if (slot.name == NULL)
25712 {
25713 slot.name = name;
25714 /* index_offset is set later. */
25715 }
25716
25717 cu_index_and_attrs = 0;
25718 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
25719 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
25720 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
25721
25722 /* We don't want to record an index value twice as we want to avoid the
25723 duplication.
25724 We process all global symbols and then all static symbols
25725 (which would allow us to avoid the duplication by only having to check
25726 the last entry pushed), but a symbol could have multiple kinds in one CU.
25727 To keep things simple we don't worry about the duplication here and
25728 sort and uniqufy the list after we've processed all symbols. */
25729 slot.cu_indices.push_back (cu_index_and_attrs);
25730 }
25731
25732 /* Sort and remove duplicates of all symbols' cu_indices lists. */
25733
25734 static void
25735 uniquify_cu_indices (struct mapped_symtab *symtab)
25736 {
25737 for (auto &entry : symtab->data)
25738 {
25739 if (entry.name != NULL && !entry.cu_indices.empty ())
25740 {
25741 auto &cu_indices = entry.cu_indices;
25742 std::sort (cu_indices.begin (), cu_indices.end ());
25743 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
25744 cu_indices.erase (from, cu_indices.end ());
25745 }
25746 }
25747 }
25748
25749 /* A form of 'const char *' suitable for container keys. Only the
25750 pointer is stored. The strings themselves are compared, not the
25751 pointers. */
25752 class c_str_view
25753 {
25754 public:
25755 c_str_view (const char *cstr)
25756 : m_cstr (cstr)
25757 {}
25758
25759 bool operator== (const c_str_view &other) const
25760 {
25761 return strcmp (m_cstr, other.m_cstr) == 0;
25762 }
25763
25764 /* Return the underlying C string. Note, the returned string is
25765 only a reference with lifetime of this object. */
25766 const char *c_str () const
25767 {
25768 return m_cstr;
25769 }
25770
25771 private:
25772 friend class c_str_view_hasher;
25773 const char *const m_cstr;
25774 };
25775
25776 /* A std::unordered_map::hasher for c_str_view that uses the right
25777 hash function for strings in a mapped index. */
25778 class c_str_view_hasher
25779 {
25780 public:
25781 size_t operator () (const c_str_view &x) const
25782 {
25783 return mapped_index_string_hash (INT_MAX, x.m_cstr);
25784 }
25785 };
25786
25787 /* A std::unordered_map::hasher for std::vector<>. */
25788 template<typename T>
25789 class vector_hasher
25790 {
25791 public:
25792 size_t operator () (const std::vector<T> &key) const
25793 {
25794 return iterative_hash (key.data (),
25795 sizeof (key.front ()) * key.size (), 0);
25796 }
25797 };
25798
25799 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
25800 constant pool entries going into the data buffer CPOOL. */
25801
25802 static void
25803 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
25804 {
25805 {
25806 /* Elements are sorted vectors of the indices of all the CUs that
25807 hold an object of this name. */
25808 std::unordered_map<std::vector<offset_type>, offset_type,
25809 vector_hasher<offset_type>>
25810 symbol_hash_table;
25811
25812 /* We add all the index vectors to the constant pool first, to
25813 ensure alignment is ok. */
25814 for (symtab_index_entry &entry : symtab->data)
25815 {
25816 if (entry.name == NULL)
25817 continue;
25818 gdb_assert (entry.index_offset == 0);
25819
25820 /* Finding before inserting is faster than always trying to
25821 insert, because inserting always allocates a node, does the
25822 lookup, and then destroys the new node if another node
25823 already had the same key. C++17 try_emplace will avoid
25824 this. */
25825 const auto found
25826 = symbol_hash_table.find (entry.cu_indices);
25827 if (found != symbol_hash_table.end ())
25828 {
25829 entry.index_offset = found->second;
25830 continue;
25831 }
25832
25833 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
25834 entry.index_offset = cpool.size ();
25835 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
25836 for (const auto index : entry.cu_indices)
25837 cpool.append_data (MAYBE_SWAP (index));
25838 }
25839 }
25840
25841 /* Now write out the hash table. */
25842 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
25843 for (const auto &entry : symtab->data)
25844 {
25845 offset_type str_off, vec_off;
25846
25847 if (entry.name != NULL)
25848 {
25849 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
25850 if (insertpair.second)
25851 cpool.append_cstr0 (entry.name);
25852 str_off = insertpair.first->second;
25853 vec_off = entry.index_offset;
25854 }
25855 else
25856 {
25857 /* While 0 is a valid constant pool index, it is not valid
25858 to have 0 for both offsets. */
25859 str_off = 0;
25860 vec_off = 0;
25861 }
25862
25863 output.append_data (MAYBE_SWAP (str_off));
25864 output.append_data (MAYBE_SWAP (vec_off));
25865 }
25866 }
25867
25868 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
25869
25870 /* Helper struct for building the address table. */
25871 struct addrmap_index_data
25872 {
25873 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
25874 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
25875 {}
25876
25877 struct objfile *objfile;
25878 data_buf &addr_vec;
25879 psym_index_map &cu_index_htab;
25880
25881 /* Non-zero if the previous_* fields are valid.
25882 We can't write an entry until we see the next entry (since it is only then
25883 that we know the end of the entry). */
25884 int previous_valid;
25885 /* Index of the CU in the table of all CUs in the index file. */
25886 unsigned int previous_cu_index;
25887 /* Start address of the CU. */
25888 CORE_ADDR previous_cu_start;
25889 };
25890
25891 /* Write an address entry to ADDR_VEC. */
25892
25893 static void
25894 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
25895 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
25896 {
25897 CORE_ADDR baseaddr;
25898
25899 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25900
25901 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
25902 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
25903 addr_vec.append_data (MAYBE_SWAP (cu_index));
25904 }
25905
25906 /* Worker function for traversing an addrmap to build the address table. */
25907
25908 static int
25909 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
25910 {
25911 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
25912 struct partial_symtab *pst = (struct partial_symtab *) obj;
25913
25914 if (data->previous_valid)
25915 add_address_entry (data->objfile, data->addr_vec,
25916 data->previous_cu_start, start_addr,
25917 data->previous_cu_index);
25918
25919 data->previous_cu_start = start_addr;
25920 if (pst != NULL)
25921 {
25922 const auto it = data->cu_index_htab.find (pst);
25923 gdb_assert (it != data->cu_index_htab.cend ());
25924 data->previous_cu_index = it->second;
25925 data->previous_valid = 1;
25926 }
25927 else
25928 data->previous_valid = 0;
25929
25930 return 0;
25931 }
25932
25933 /* Write OBJFILE's address map to ADDR_VEC.
25934 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
25935 in the index file. */
25936
25937 static void
25938 write_address_map (struct objfile *objfile, data_buf &addr_vec,
25939 psym_index_map &cu_index_htab)
25940 {
25941 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
25942
25943 /* When writing the address table, we have to cope with the fact that
25944 the addrmap iterator only provides the start of a region; we have to
25945 wait until the next invocation to get the start of the next region. */
25946
25947 addrmap_index_data.objfile = objfile;
25948 addrmap_index_data.previous_valid = 0;
25949
25950 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
25951 &addrmap_index_data);
25952
25953 /* It's highly unlikely the last entry (end address = 0xff...ff)
25954 is valid, but we should still handle it.
25955 The end address is recorded as the start of the next region, but that
25956 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
25957 anyway. */
25958 if (addrmap_index_data.previous_valid)
25959 add_address_entry (objfile, addr_vec,
25960 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
25961 addrmap_index_data.previous_cu_index);
25962 }
25963
25964 /* Return the symbol kind of PSYM. */
25965
25966 static gdb_index_symbol_kind
25967 symbol_kind (struct partial_symbol *psym)
25968 {
25969 domain_enum domain = PSYMBOL_DOMAIN (psym);
25970 enum address_class aclass = PSYMBOL_CLASS (psym);
25971
25972 switch (domain)
25973 {
25974 case VAR_DOMAIN:
25975 switch (aclass)
25976 {
25977 case LOC_BLOCK:
25978 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
25979 case LOC_TYPEDEF:
25980 return GDB_INDEX_SYMBOL_KIND_TYPE;
25981 case LOC_COMPUTED:
25982 case LOC_CONST_BYTES:
25983 case LOC_OPTIMIZED_OUT:
25984 case LOC_STATIC:
25985 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25986 case LOC_CONST:
25987 /* Note: It's currently impossible to recognize psyms as enum values
25988 short of reading the type info. For now punt. */
25989 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25990 default:
25991 /* There are other LOC_FOO values that one might want to classify
25992 as variables, but dwarf2read.c doesn't currently use them. */
25993 return GDB_INDEX_SYMBOL_KIND_OTHER;
25994 }
25995 case STRUCT_DOMAIN:
25996 return GDB_INDEX_SYMBOL_KIND_TYPE;
25997 default:
25998 return GDB_INDEX_SYMBOL_KIND_OTHER;
25999 }
26000 }
26001
26002 /* Add a list of partial symbols to SYMTAB. */
26003
26004 static void
26005 write_psymbols (struct mapped_symtab *symtab,
26006 std::unordered_set<partial_symbol *> &psyms_seen,
26007 struct partial_symbol **psymp,
26008 int count,
26009 offset_type cu_index,
26010 int is_static)
26011 {
26012 for (; count-- > 0; ++psymp)
26013 {
26014 struct partial_symbol *psym = *psymp;
26015
26016 if (SYMBOL_LANGUAGE (psym) == language_ada)
26017 error (_("Ada is not currently supported by the index"));
26018
26019 /* Only add a given psymbol once. */
26020 if (psyms_seen.insert (psym).second)
26021 {
26022 gdb_index_symbol_kind kind = symbol_kind (psym);
26023
26024 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
26025 is_static, kind, cu_index);
26026 }
26027 }
26028 }
26029
26030 /* A helper struct used when iterating over debug_types. */
26031 struct signatured_type_index_data
26032 {
26033 signatured_type_index_data (data_buf &types_list_,
26034 std::unordered_set<partial_symbol *> &psyms_seen_)
26035 : types_list (types_list_), psyms_seen (psyms_seen_)
26036 {}
26037
26038 struct objfile *objfile;
26039 struct mapped_symtab *symtab;
26040 data_buf &types_list;
26041 std::unordered_set<partial_symbol *> &psyms_seen;
26042 int cu_index;
26043 };
26044
26045 /* A helper function that writes a single signatured_type to an
26046 obstack. */
26047
26048 static int
26049 write_one_signatured_type (void **slot, void *d)
26050 {
26051 struct signatured_type_index_data *info
26052 = (struct signatured_type_index_data *) d;
26053 struct signatured_type *entry = (struct signatured_type *) *slot;
26054 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26055
26056 write_psymbols (info->symtab,
26057 info->psyms_seen,
26058 &info->objfile->global_psymbols[psymtab->globals_offset],
26059 psymtab->n_global_syms, info->cu_index,
26060 0);
26061 write_psymbols (info->symtab,
26062 info->psyms_seen,
26063 &info->objfile->static_psymbols[psymtab->statics_offset],
26064 psymtab->n_static_syms, info->cu_index,
26065 1);
26066
26067 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26068 to_underlying (entry->per_cu.sect_off));
26069 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26070 to_underlying (entry->type_offset_in_tu));
26071 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
26072
26073 ++info->cu_index;
26074
26075 return 1;
26076 }
26077
26078 /* Recurse into all "included" dependencies and count their symbols as
26079 if they appeared in this psymtab. */
26080
26081 static void
26082 recursively_count_psymbols (struct partial_symtab *psymtab,
26083 size_t &psyms_seen)
26084 {
26085 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26086 if (psymtab->dependencies[i]->user != NULL)
26087 recursively_count_psymbols (psymtab->dependencies[i],
26088 psyms_seen);
26089
26090 psyms_seen += psymtab->n_global_syms;
26091 psyms_seen += psymtab->n_static_syms;
26092 }
26093
26094 /* Recurse into all "included" dependencies and write their symbols as
26095 if they appeared in this psymtab. */
26096
26097 static void
26098 recursively_write_psymbols (struct objfile *objfile,
26099 struct partial_symtab *psymtab,
26100 struct mapped_symtab *symtab,
26101 std::unordered_set<partial_symbol *> &psyms_seen,
26102 offset_type cu_index)
26103 {
26104 int i;
26105
26106 for (i = 0; i < psymtab->number_of_dependencies; ++i)
26107 if (psymtab->dependencies[i]->user != NULL)
26108 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26109 symtab, psyms_seen, cu_index);
26110
26111 write_psymbols (symtab,
26112 psyms_seen,
26113 &objfile->global_psymbols[psymtab->globals_offset],
26114 psymtab->n_global_syms, cu_index,
26115 0);
26116 write_psymbols (symtab,
26117 psyms_seen,
26118 &objfile->static_psymbols[psymtab->statics_offset],
26119 psymtab->n_static_syms, cu_index,
26120 1);
26121 }
26122
26123 /* DWARF-5 .debug_names builder. */
26124 class debug_names
26125 {
26126 public:
26127 debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile, bool is_dwarf64,
26128 bfd_endian dwarf5_byte_order)
26129 : m_dwarf5_byte_order (dwarf5_byte_order),
26130 m_dwarf32 (dwarf5_byte_order),
26131 m_dwarf64 (dwarf5_byte_order),
26132 m_dwarf (is_dwarf64
26133 ? static_cast<dwarf &> (m_dwarf64)
26134 : static_cast<dwarf &> (m_dwarf32)),
26135 m_name_table_string_offs (m_dwarf.name_table_string_offs),
26136 m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
26137 m_debugstrlookup (dwarf2_per_objfile)
26138 {}
26139
26140 int dwarf5_offset_size () const
26141 {
26142 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
26143 return dwarf5_is_dwarf64 ? 8 : 4;
26144 }
26145
26146 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
26147 enum class unit_kind { cu, tu };
26148
26149 /* Insert one symbol. */
26150 void insert (const partial_symbol *psym, int cu_index, bool is_static,
26151 unit_kind kind)
26152 {
26153 const int dwarf_tag = psymbol_tag (psym);
26154 if (dwarf_tag == 0)
26155 return;
26156 const char *const name = SYMBOL_SEARCH_NAME (psym);
26157 const auto insertpair
26158 = m_name_to_value_set.emplace (c_str_view (name),
26159 std::set<symbol_value> ());
26160 std::set<symbol_value> &value_set = insertpair.first->second;
26161 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
26162 }
26163
26164 /* Build all the tables. All symbols must be already inserted.
26165 This function does not call file_write, caller has to do it
26166 afterwards. */
26167 void build ()
26168 {
26169 /* Verify the build method has not be called twice. */
26170 gdb_assert (m_abbrev_table.empty ());
26171 const size_t name_count = m_name_to_value_set.size ();
26172 m_bucket_table.resize
26173 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26174 m_hash_table.reserve (name_count);
26175 m_name_table_string_offs.reserve (name_count);
26176 m_name_table_entry_offs.reserve (name_count);
26177
26178 /* Map each hash of symbol to its name and value. */
26179 struct hash_it_pair
26180 {
26181 uint32_t hash;
26182 decltype (m_name_to_value_set)::const_iterator it;
26183 };
26184 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26185 bucket_hash.resize (m_bucket_table.size ());
26186 for (decltype (m_name_to_value_set)::const_iterator it
26187 = m_name_to_value_set.cbegin ();
26188 it != m_name_to_value_set.cend ();
26189 ++it)
26190 {
26191 const char *const name = it->first.c_str ();
26192 const uint32_t hash = dwarf5_djb_hash (name);
26193 hash_it_pair hashitpair;
26194 hashitpair.hash = hash;
26195 hashitpair.it = it;
26196 auto &slot = bucket_hash[hash % bucket_hash.size()];
26197 slot.push_front (std::move (hashitpair));
26198 }
26199 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26200 {
26201 const std::forward_list<hash_it_pair> &hashitlist
26202 = bucket_hash[bucket_ix];
26203 if (hashitlist.empty ())
26204 continue;
26205 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26206 /* The hashes array is indexed starting at 1. */
26207 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26208 sizeof (bucket_slot), m_dwarf5_byte_order,
26209 m_hash_table.size () + 1);
26210 for (const hash_it_pair &hashitpair : hashitlist)
26211 {
26212 m_hash_table.push_back (0);
26213 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26214 (&m_hash_table.back ()),
26215 sizeof (m_hash_table.back ()),
26216 m_dwarf5_byte_order, hashitpair.hash);
26217 const c_str_view &name = hashitpair.it->first;
26218 const std::set<symbol_value> &value_set = hashitpair.it->second;
26219 m_name_table_string_offs.push_back_reorder
26220 (m_debugstrlookup.lookup (name.c_str ()));
26221 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26222 gdb_assert (!value_set.empty ());
26223 for (const symbol_value &value : value_set)
26224 {
26225 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
26226 value.is_static,
26227 value.kind)];
26228 if (idx == 0)
26229 {
26230 idx = m_idx_next++;
26231 m_abbrev_table.append_unsigned_leb128 (idx);
26232 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
26233 m_abbrev_table.append_unsigned_leb128
26234 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
26235 : DW_IDX_type_unit);
26236 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26237 m_abbrev_table.append_unsigned_leb128 (value.is_static
26238 ? DW_IDX_GNU_internal
26239 : DW_IDX_GNU_external);
26240 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26241
26242 /* Terminate attributes list. */
26243 m_abbrev_table.append_unsigned_leb128 (0);
26244 m_abbrev_table.append_unsigned_leb128 (0);
26245 }
26246
26247 m_entry_pool.append_unsigned_leb128 (idx);
26248 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26249 }
26250
26251 /* Terminate the list of CUs. */
26252 m_entry_pool.append_unsigned_leb128 (0);
26253 }
26254 }
26255 gdb_assert (m_hash_table.size () == name_count);
26256
26257 /* Terminate tags list. */
26258 m_abbrev_table.append_unsigned_leb128 (0);
26259 }
26260
26261 /* Return .debug_names bucket count. This must be called only after
26262 calling the build method. */
26263 uint32_t bucket_count () const
26264 {
26265 /* Verify the build method has been already called. */
26266 gdb_assert (!m_abbrev_table.empty ());
26267 const uint32_t retval = m_bucket_table.size ();
26268
26269 /* Check for overflow. */
26270 gdb_assert (retval == m_bucket_table.size ());
26271 return retval;
26272 }
26273
26274 /* Return .debug_names names count. This must be called only after
26275 calling the build method. */
26276 uint32_t name_count () const
26277 {
26278 /* Verify the build method has been already called. */
26279 gdb_assert (!m_abbrev_table.empty ());
26280 const uint32_t retval = m_hash_table.size ();
26281
26282 /* Check for overflow. */
26283 gdb_assert (retval == m_hash_table.size ());
26284 return retval;
26285 }
26286
26287 /* Return number of bytes of .debug_names abbreviation table. This
26288 must be called only after calling the build method. */
26289 uint32_t abbrev_table_bytes () const
26290 {
26291 gdb_assert (!m_abbrev_table.empty ());
26292 return m_abbrev_table.size ();
26293 }
26294
26295 /* Recurse into all "included" dependencies and store their symbols
26296 as if they appeared in this psymtab. */
26297 void recursively_write_psymbols
26298 (struct objfile *objfile,
26299 struct partial_symtab *psymtab,
26300 std::unordered_set<partial_symbol *> &psyms_seen,
26301 int cu_index)
26302 {
26303 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26304 if (psymtab->dependencies[i]->user != NULL)
26305 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26306 psyms_seen, cu_index);
26307
26308 write_psymbols (psyms_seen,
26309 &objfile->global_psymbols[psymtab->globals_offset],
26310 psymtab->n_global_syms, cu_index, false, unit_kind::cu);
26311 write_psymbols (psyms_seen,
26312 &objfile->static_psymbols[psymtab->statics_offset],
26313 psymtab->n_static_syms, cu_index, true, unit_kind::cu);
26314 }
26315
26316 /* Return number of bytes the .debug_names section will have. This
26317 must be called only after calling the build method. */
26318 size_t bytes () const
26319 {
26320 /* Verify the build method has been already called. */
26321 gdb_assert (!m_abbrev_table.empty ());
26322 size_t expected_bytes = 0;
26323 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26324 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26325 expected_bytes += m_name_table_string_offs.bytes ();
26326 expected_bytes += m_name_table_entry_offs.bytes ();
26327 expected_bytes += m_abbrev_table.size ();
26328 expected_bytes += m_entry_pool.size ();
26329 return expected_bytes;
26330 }
26331
26332 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26333 FILE_STR. This must be called only after calling the build
26334 method. */
26335 void file_write (FILE *file_names, FILE *file_str) const
26336 {
26337 /* Verify the build method has been already called. */
26338 gdb_assert (!m_abbrev_table.empty ());
26339 ::file_write (file_names, m_bucket_table);
26340 ::file_write (file_names, m_hash_table);
26341 m_name_table_string_offs.file_write (file_names);
26342 m_name_table_entry_offs.file_write (file_names);
26343 m_abbrev_table.file_write (file_names);
26344 m_entry_pool.file_write (file_names);
26345 m_debugstrlookup.file_write (file_str);
26346 }
26347
26348 /* A helper user data for write_one_signatured_type. */
26349 class write_one_signatured_type_data
26350 {
26351 public:
26352 write_one_signatured_type_data (debug_names &nametable_,
26353 signatured_type_index_data &&info_)
26354 : nametable (nametable_), info (std::move (info_))
26355 {}
26356 debug_names &nametable;
26357 struct signatured_type_index_data info;
26358 };
26359
26360 /* A helper function to pass write_one_signatured_type to
26361 htab_traverse_noresize. */
26362 static int
26363 write_one_signatured_type (void **slot, void *d)
26364 {
26365 write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
26366 struct signatured_type_index_data *info = &data->info;
26367 struct signatured_type *entry = (struct signatured_type *) *slot;
26368
26369 data->nametable.write_one_signatured_type (entry, info);
26370
26371 return 1;
26372 }
26373
26374 private:
26375
26376 /* Storage for symbol names mapping them to their .debug_str section
26377 offsets. */
26378 class debug_str_lookup
26379 {
26380 public:
26381
26382 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26383 All .debug_str section strings are automatically stored. */
26384 debug_str_lookup (struct dwarf2_per_objfile *dwarf2_per_objfile)
26385 : m_abfd (dwarf2_per_objfile->objfile->obfd),
26386 m_dwarf2_per_objfile (dwarf2_per_objfile)
26387 {
26388 dwarf2_read_section (dwarf2_per_objfile->objfile,
26389 &dwarf2_per_objfile->str);
26390 if (dwarf2_per_objfile->str.buffer == NULL)
26391 return;
26392 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26393 data < (dwarf2_per_objfile->str.buffer
26394 + dwarf2_per_objfile->str.size);)
26395 {
26396 const char *const s = reinterpret_cast<const char *> (data);
26397 const auto insertpair
26398 = m_str_table.emplace (c_str_view (s),
26399 data - dwarf2_per_objfile->str.buffer);
26400 if (!insertpair.second)
26401 complaint (&symfile_complaints,
26402 _("Duplicate string \"%s\" in "
26403 ".debug_str section [in module %s]"),
26404 s, bfd_get_filename (m_abfd));
26405 data += strlen (s) + 1;
26406 }
26407 }
26408
26409 /* Return offset of symbol name S in the .debug_str section. Add
26410 such symbol to the section's end if it does not exist there
26411 yet. */
26412 size_t lookup (const char *s)
26413 {
26414 const auto it = m_str_table.find (c_str_view (s));
26415 if (it != m_str_table.end ())
26416 return it->second;
26417 const size_t offset = (m_dwarf2_per_objfile->str.size
26418 + m_str_add_buf.size ());
26419 m_str_table.emplace (c_str_view (s), offset);
26420 m_str_add_buf.append_cstr0 (s);
26421 return offset;
26422 }
26423
26424 /* Append the end of the .debug_str section to FILE. */
26425 void file_write (FILE *file) const
26426 {
26427 m_str_add_buf.file_write (file);
26428 }
26429
26430 private:
26431 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26432 bfd *const m_abfd;
26433 struct dwarf2_per_objfile *m_dwarf2_per_objfile;
26434
26435 /* Data to add at the end of .debug_str for new needed symbol names. */
26436 data_buf m_str_add_buf;
26437 };
26438
26439 /* Container to map used DWARF tags to their .debug_names abbreviation
26440 tags. */
26441 class index_key
26442 {
26443 public:
26444 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
26445 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
26446 {
26447 }
26448
26449 bool
26450 operator== (const index_key &other) const
26451 {
26452 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
26453 && kind == other.kind);
26454 }
26455
26456 const int dwarf_tag;
26457 const bool is_static;
26458 const unit_kind kind;
26459 };
26460
26461 /* Provide std::unordered_map::hasher for index_key. */
26462 class index_key_hasher
26463 {
26464 public:
26465 size_t
26466 operator () (const index_key &key) const
26467 {
26468 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26469 }
26470 };
26471
26472 /* Parameters of one symbol entry. */
26473 class symbol_value
26474 {
26475 public:
26476 const int dwarf_tag, cu_index;
26477 const bool is_static;
26478 const unit_kind kind;
26479
26480 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
26481 unit_kind kind_)
26482 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
26483 kind (kind_)
26484 {}
26485
26486 bool
26487 operator< (const symbol_value &other) const
26488 {
26489 #define X(n) \
26490 do \
26491 { \
26492 if (n < other.n) \
26493 return true; \
26494 if (n > other.n) \
26495 return false; \
26496 } \
26497 while (0)
26498 X (dwarf_tag);
26499 X (is_static);
26500 X (kind);
26501 X (cu_index);
26502 #undef X
26503 return false;
26504 }
26505 };
26506
26507 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26508 output. */
26509 class offset_vec
26510 {
26511 protected:
26512 const bfd_endian dwarf5_byte_order;
26513 public:
26514 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26515 : dwarf5_byte_order (dwarf5_byte_order_)
26516 {}
26517
26518 /* Call std::vector::reserve for NELEM elements. */
26519 virtual void reserve (size_t nelem) = 0;
26520
26521 /* Call std::vector::push_back with store_unsigned_integer byte
26522 reordering for ELEM. */
26523 virtual void push_back_reorder (size_t elem) = 0;
26524
26525 /* Return expected output size in bytes. */
26526 virtual size_t bytes () const = 0;
26527
26528 /* Write name table to FILE. */
26529 virtual void file_write (FILE *file) const = 0;
26530 };
26531
26532 /* Template to unify DWARF-32 and DWARF-64 output. */
26533 template<typename OffsetSize>
26534 class offset_vec_tmpl : public offset_vec
26535 {
26536 public:
26537 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26538 : offset_vec (dwarf5_byte_order_)
26539 {}
26540
26541 /* Implement offset_vec::reserve. */
26542 void reserve (size_t nelem) override
26543 {
26544 m_vec.reserve (nelem);
26545 }
26546
26547 /* Implement offset_vec::push_back_reorder. */
26548 void push_back_reorder (size_t elem) override
26549 {
26550 m_vec.push_back (elem);
26551 /* Check for overflow. */
26552 gdb_assert (m_vec.back () == elem);
26553 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26554 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26555 }
26556
26557 /* Implement offset_vec::bytes. */
26558 size_t bytes () const override
26559 {
26560 return m_vec.size () * sizeof (m_vec[0]);
26561 }
26562
26563 /* Implement offset_vec::file_write. */
26564 void file_write (FILE *file) const override
26565 {
26566 ::file_write (file, m_vec);
26567 }
26568
26569 private:
26570 std::vector<OffsetSize> m_vec;
26571 };
26572
26573 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26574 respecting name table width. */
26575 class dwarf
26576 {
26577 public:
26578 offset_vec &name_table_string_offs, &name_table_entry_offs;
26579
26580 dwarf (offset_vec &name_table_string_offs_,
26581 offset_vec &name_table_entry_offs_)
26582 : name_table_string_offs (name_table_string_offs_),
26583 name_table_entry_offs (name_table_entry_offs_)
26584 {
26585 }
26586 };
26587
26588 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26589 respecting name table width. */
26590 template<typename OffsetSize>
26591 class dwarf_tmpl : public dwarf
26592 {
26593 public:
26594 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26595 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26596 m_name_table_string_offs (dwarf5_byte_order_),
26597 m_name_table_entry_offs (dwarf5_byte_order_)
26598 {}
26599
26600 private:
26601 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26602 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26603 };
26604
26605 /* Try to reconstruct original DWARF tag for given partial_symbol.
26606 This function is not DWARF-5 compliant but it is sufficient for
26607 GDB as a DWARF-5 index consumer. */
26608 static int psymbol_tag (const struct partial_symbol *psym)
26609 {
26610 domain_enum domain = PSYMBOL_DOMAIN (psym);
26611 enum address_class aclass = PSYMBOL_CLASS (psym);
26612
26613 switch (domain)
26614 {
26615 case VAR_DOMAIN:
26616 switch (aclass)
26617 {
26618 case LOC_BLOCK:
26619 return DW_TAG_subprogram;
26620 case LOC_TYPEDEF:
26621 return DW_TAG_typedef;
26622 case LOC_COMPUTED:
26623 case LOC_CONST_BYTES:
26624 case LOC_OPTIMIZED_OUT:
26625 case LOC_STATIC:
26626 return DW_TAG_variable;
26627 case LOC_CONST:
26628 /* Note: It's currently impossible to recognize psyms as enum values
26629 short of reading the type info. For now punt. */
26630 return DW_TAG_variable;
26631 default:
26632 /* There are other LOC_FOO values that one might want to classify
26633 as variables, but dwarf2read.c doesn't currently use them. */
26634 return DW_TAG_variable;
26635 }
26636 case STRUCT_DOMAIN:
26637 return DW_TAG_structure_type;
26638 default:
26639 return 0;
26640 }
26641 }
26642
26643 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26644 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
26645 struct partial_symbol **psymp, int count, int cu_index,
26646 bool is_static, unit_kind kind)
26647 {
26648 for (; count-- > 0; ++psymp)
26649 {
26650 struct partial_symbol *psym = *psymp;
26651
26652 if (SYMBOL_LANGUAGE (psym) == language_ada)
26653 error (_("Ada is not currently supported by the index"));
26654
26655 /* Only add a given psymbol once. */
26656 if (psyms_seen.insert (psym).second)
26657 insert (psym, cu_index, is_static, kind);
26658 }
26659 }
26660
26661 /* A helper function that writes a single signatured_type
26662 to a debug_names. */
26663 void
26664 write_one_signatured_type (struct signatured_type *entry,
26665 struct signatured_type_index_data *info)
26666 {
26667 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26668
26669 write_psymbols (info->psyms_seen,
26670 &info->objfile->global_psymbols[psymtab->globals_offset],
26671 psymtab->n_global_syms, info->cu_index, false,
26672 unit_kind::tu);
26673 write_psymbols (info->psyms_seen,
26674 &info->objfile->static_psymbols[psymtab->statics_offset],
26675 psymtab->n_static_syms, info->cu_index, true,
26676 unit_kind::tu);
26677
26678 info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
26679 to_underlying (entry->per_cu.sect_off));
26680
26681 ++info->cu_index;
26682 }
26683
26684 /* Store value of each symbol. */
26685 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
26686 m_name_to_value_set;
26687
26688 /* Tables of DWARF-5 .debug_names. They are in object file byte
26689 order. */
26690 std::vector<uint32_t> m_bucket_table;
26691 std::vector<uint32_t> m_hash_table;
26692
26693 const bfd_endian m_dwarf5_byte_order;
26694 dwarf_tmpl<uint32_t> m_dwarf32;
26695 dwarf_tmpl<uint64_t> m_dwarf64;
26696 dwarf &m_dwarf;
26697 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
26698 debug_str_lookup m_debugstrlookup;
26699
26700 /* Map each used .debug_names abbreviation tag parameter to its
26701 index value. */
26702 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
26703
26704 /* Next unused .debug_names abbreviation tag for
26705 m_indexkey_to_idx. */
26706 int m_idx_next = 1;
26707
26708 /* .debug_names abbreviation table. */
26709 data_buf m_abbrev_table;
26710
26711 /* .debug_names entry pool. */
26712 data_buf m_entry_pool;
26713 };
26714
26715 /* Return iff any of the needed offsets does not fit into 32-bit
26716 .debug_names section. */
26717
26718 static bool
26719 check_dwarf64_offsets (struct dwarf2_per_objfile *dwarf2_per_objfile)
26720 {
26721 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26722 {
26723 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
26724
26725 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26726 return true;
26727 }
26728 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
26729 {
26730 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
26731 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
26732
26733 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26734 return true;
26735 }
26736 return false;
26737 }
26738
26739 /* The psyms_seen set is potentially going to be largish (~40k
26740 elements when indexing a -g3 build of GDB itself). Estimate the
26741 number of elements in order to avoid too many rehashes, which
26742 require rebuilding buckets and thus many trips to
26743 malloc/free. */
26744
26745 static size_t
26746 psyms_seen_size (struct dwarf2_per_objfile *dwarf2_per_objfile)
26747 {
26748 size_t psyms_count = 0;
26749 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26750 {
26751 struct dwarf2_per_cu_data *per_cu
26752 = dwarf2_per_objfile->all_comp_units[i];
26753 struct partial_symtab *psymtab = per_cu->v.psymtab;
26754
26755 if (psymtab != NULL && psymtab->user == NULL)
26756 recursively_count_psymbols (psymtab, psyms_count);
26757 }
26758 /* Generating an index for gdb itself shows a ratio of
26759 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
26760 return psyms_count / 4;
26761 }
26762
26763 /* Write new .gdb_index section for OBJFILE into OUT_FILE.
26764 Return how many bytes were expected to be written into OUT_FILE. */
26765
26766 static size_t
26767 write_gdbindex (struct dwarf2_per_objfile *dwarf2_per_objfile, FILE *out_file)
26768 {
26769 struct objfile *objfile = dwarf2_per_objfile->objfile;
26770 mapped_symtab symtab;
26771 data_buf cu_list;
26772
26773 /* While we're scanning CU's create a table that maps a psymtab pointer
26774 (which is what addrmap records) to its index (which is what is recorded
26775 in the index file). This will later be needed to write the address
26776 table. */
26777 psym_index_map cu_index_htab;
26778 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
26779
26780 /* The CU list is already sorted, so we don't need to do additional
26781 work here. Also, the debug_types entries do not appear in
26782 all_comp_units, but only in their own hash table. */
26783
26784 std::unordered_set<partial_symbol *> psyms_seen
26785 (psyms_seen_size (dwarf2_per_objfile));
26786 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26787 {
26788 struct dwarf2_per_cu_data *per_cu
26789 = dwarf2_per_objfile->all_comp_units[i];
26790 struct partial_symtab *psymtab = per_cu->v.psymtab;
26791
26792 /* CU of a shared file from 'dwz -m' may be unused by this main file.
26793 It may be referenced from a local scope but in such case it does not
26794 need to be present in .gdb_index. */
26795 if (psymtab == NULL)
26796 continue;
26797
26798 if (psymtab->user == NULL)
26799 recursively_write_psymbols (objfile, psymtab, &symtab,
26800 psyms_seen, i);
26801
26802 const auto insertpair = cu_index_htab.emplace (psymtab, i);
26803 gdb_assert (insertpair.second);
26804
26805 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
26806 to_underlying (per_cu->sect_off));
26807 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
26808 }
26809
26810 /* Dump the address map. */
26811 data_buf addr_vec;
26812 write_address_map (objfile, addr_vec, cu_index_htab);
26813
26814 /* Write out the .debug_type entries, if any. */
26815 data_buf types_cu_list;
26816 if (dwarf2_per_objfile->signatured_types)
26817 {
26818 signatured_type_index_data sig_data (types_cu_list,
26819 psyms_seen);
26820
26821 sig_data.objfile = objfile;
26822 sig_data.symtab = &symtab;
26823 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
26824 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26825 write_one_signatured_type, &sig_data);
26826 }
26827
26828 /* Now that we've processed all symbols we can shrink their cu_indices
26829 lists. */
26830 uniquify_cu_indices (&symtab);
26831
26832 data_buf symtab_vec, constant_pool;
26833 write_hash_table (&symtab, symtab_vec, constant_pool);
26834
26835 data_buf contents;
26836 const offset_type size_of_contents = 6 * sizeof (offset_type);
26837 offset_type total_len = size_of_contents;
26838
26839 /* The version number. */
26840 contents.append_data (MAYBE_SWAP (8));
26841
26842 /* The offset of the CU list from the start of the file. */
26843 contents.append_data (MAYBE_SWAP (total_len));
26844 total_len += cu_list.size ();
26845
26846 /* The offset of the types CU list from the start of the file. */
26847 contents.append_data (MAYBE_SWAP (total_len));
26848 total_len += types_cu_list.size ();
26849
26850 /* The offset of the address table from the start of the file. */
26851 contents.append_data (MAYBE_SWAP (total_len));
26852 total_len += addr_vec.size ();
26853
26854 /* The offset of the symbol table from the start of the file. */
26855 contents.append_data (MAYBE_SWAP (total_len));
26856 total_len += symtab_vec.size ();
26857
26858 /* The offset of the constant pool from the start of the file. */
26859 contents.append_data (MAYBE_SWAP (total_len));
26860 total_len += constant_pool.size ();
26861
26862 gdb_assert (contents.size () == size_of_contents);
26863
26864 contents.file_write (out_file);
26865 cu_list.file_write (out_file);
26866 types_cu_list.file_write (out_file);
26867 addr_vec.file_write (out_file);
26868 symtab_vec.file_write (out_file);
26869 constant_pool.file_write (out_file);
26870
26871 return total_len;
26872 }
26873
26874 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
26875 static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
26876
26877 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
26878 needed addition to .debug_str section to OUT_FILE_STR. Return how
26879 many bytes were expected to be written into OUT_FILE. */
26880
26881 static size_t
26882 write_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
26883 FILE *out_file, FILE *out_file_str)
26884 {
26885 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (dwarf2_per_objfile);
26886 struct objfile *objfile = dwarf2_per_objfile->objfile;
26887 const enum bfd_endian dwarf5_byte_order
26888 = gdbarch_byte_order (get_objfile_arch (objfile));
26889
26890 /* The CU list is already sorted, so we don't need to do additional
26891 work here. Also, the debug_types entries do not appear in
26892 all_comp_units, but only in their own hash table. */
26893 data_buf cu_list;
26894 debug_names nametable (dwarf2_per_objfile, dwarf5_is_dwarf64,
26895 dwarf5_byte_order);
26896 std::unordered_set<partial_symbol *>
26897 psyms_seen (psyms_seen_size (dwarf2_per_objfile));
26898 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26899 {
26900 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
26901 partial_symtab *psymtab = per_cu->v.psymtab;
26902
26903 /* CU of a shared file from 'dwz -m' may be unused by this main
26904 file. It may be referenced from a local scope but in such
26905 case it does not need to be present in .debug_names. */
26906 if (psymtab == NULL)
26907 continue;
26908
26909 if (psymtab->user == NULL)
26910 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
26911
26912 cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
26913 to_underlying (per_cu->sect_off));
26914 }
26915
26916 /* Write out the .debug_type entries, if any. */
26917 data_buf types_cu_list;
26918 if (dwarf2_per_objfile->signatured_types)
26919 {
26920 debug_names::write_one_signatured_type_data sig_data (nametable,
26921 signatured_type_index_data (types_cu_list, psyms_seen));
26922
26923 sig_data.info.objfile = objfile;
26924 /* It is used only for gdb_index. */
26925 sig_data.info.symtab = nullptr;
26926 sig_data.info.cu_index = 0;
26927 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26928 debug_names::write_one_signatured_type,
26929 &sig_data);
26930 }
26931
26932 nametable.build ();
26933
26934 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
26935
26936 const offset_type bytes_of_header
26937 = ((dwarf5_is_dwarf64 ? 12 : 4)
26938 + 2 + 2 + 7 * 4
26939 + sizeof (dwarf5_gdb_augmentation));
26940 size_t expected_bytes = 0;
26941 expected_bytes += bytes_of_header;
26942 expected_bytes += cu_list.size ();
26943 expected_bytes += types_cu_list.size ();
26944 expected_bytes += nametable.bytes ();
26945 data_buf header;
26946
26947 if (!dwarf5_is_dwarf64)
26948 {
26949 const uint64_t size64 = expected_bytes - 4;
26950 gdb_assert (size64 < 0xfffffff0);
26951 header.append_uint (4, dwarf5_byte_order, size64);
26952 }
26953 else
26954 {
26955 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
26956 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
26957 }
26958
26959 /* The version number. */
26960 header.append_uint (2, dwarf5_byte_order, 5);
26961
26962 /* Padding. */
26963 header.append_uint (2, dwarf5_byte_order, 0);
26964
26965 /* comp_unit_count - The number of CUs in the CU list. */
26966 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
26967
26968 /* local_type_unit_count - The number of TUs in the local TU
26969 list. */
26970 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
26971
26972 /* foreign_type_unit_count - The number of TUs in the foreign TU
26973 list. */
26974 header.append_uint (4, dwarf5_byte_order, 0);
26975
26976 /* bucket_count - The number of hash buckets in the hash lookup
26977 table. */
26978 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
26979
26980 /* name_count - The number of unique names in the index. */
26981 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
26982
26983 /* abbrev_table_size - The size in bytes of the abbreviations
26984 table. */
26985 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
26986
26987 /* augmentation_string_size - The size in bytes of the augmentation
26988 string. This value is rounded up to a multiple of 4. */
26989 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
26990 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
26991 header.append_data (dwarf5_gdb_augmentation);
26992
26993 gdb_assert (header.size () == bytes_of_header);
26994
26995 header.file_write (out_file);
26996 cu_list.file_write (out_file);
26997 types_cu_list.file_write (out_file);
26998 nametable.file_write (out_file, out_file_str);
26999
27000 return expected_bytes;
27001 }
27002
27003 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
27004 position is at the end of the file. */
27005
27006 static void
27007 assert_file_size (FILE *file, const char *filename, size_t expected_size)
27008 {
27009 const auto file_size = ftell (file);
27010 if (file_size == -1)
27011 error (_("Can't get `%s' size"), filename);
27012 gdb_assert (file_size == expected_size);
27013 }
27014
27015 /* Create an index file for OBJFILE in the directory DIR. */
27016
27017 static void
27018 write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
27019 const char *dir,
27020 dw_index_kind index_kind)
27021 {
27022 struct objfile *objfile = dwarf2_per_objfile->objfile;
27023
27024 if (dwarf2_per_objfile->using_index)
27025 error (_("Cannot use an index to create the index"));
27026
27027 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
27028 error (_("Cannot make an index when the file has multiple .debug_types sections"));
27029
27030 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
27031 return;
27032
27033 struct stat st;
27034 if (stat (objfile_name (objfile), &st) < 0)
27035 perror_with_name (objfile_name (objfile));
27036
27037 std::string filename (std::string (dir) + SLASH_STRING
27038 + lbasename (objfile_name (objfile))
27039 + (index_kind == dw_index_kind::DEBUG_NAMES
27040 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
27041
27042 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
27043 if (!out_file)
27044 error (_("Can't open `%s' for writing"), filename.c_str ());
27045
27046 /* Order matters here; we want FILE to be closed before FILENAME is
27047 unlinked, because on MS-Windows one cannot delete a file that is
27048 still open. (Don't call anything here that might throw until
27049 file_closer is created.) */
27050 gdb::unlinker unlink_file (filename.c_str ());
27051 gdb_file_up close_out_file (out_file);
27052
27053 if (index_kind == dw_index_kind::DEBUG_NAMES)
27054 {
27055 std::string filename_str (std::string (dir) + SLASH_STRING
27056 + lbasename (objfile_name (objfile))
27057 + DEBUG_STR_SUFFIX);
27058 FILE *out_file_str
27059 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
27060 if (!out_file_str)
27061 error (_("Can't open `%s' for writing"), filename_str.c_str ());
27062 gdb::unlinker unlink_file_str (filename_str.c_str ());
27063 gdb_file_up close_out_file_str (out_file_str);
27064
27065 const size_t total_len
27066 = write_debug_names (dwarf2_per_objfile, out_file, out_file_str);
27067 assert_file_size (out_file, filename.c_str (), total_len);
27068
27069 /* We want to keep the file .debug_str file too. */
27070 unlink_file_str.keep ();
27071 }
27072 else
27073 {
27074 const size_t total_len
27075 = write_gdbindex (dwarf2_per_objfile, out_file);
27076 assert_file_size (out_file, filename.c_str (), total_len);
27077 }
27078
27079 /* We want to keep the file. */
27080 unlink_file.keep ();
27081 }
27082
27083 /* Implementation of the `save gdb-index' command.
27084
27085 Note that the .gdb_index file format used by this command is
27086 documented in the GDB manual. Any changes here must be documented
27087 there. */
27088
27089 static void
27090 save_gdb_index_command (const char *arg, int from_tty)
27091 {
27092 struct objfile *objfile;
27093 const char dwarf5space[] = "-dwarf-5 ";
27094 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
27095
27096 if (!arg)
27097 arg = "";
27098
27099 arg = skip_spaces (arg);
27100 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
27101 {
27102 index_kind = dw_index_kind::DEBUG_NAMES;
27103 arg += strlen (dwarf5space);
27104 arg = skip_spaces (arg);
27105 }
27106
27107 if (!*arg)
27108 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
27109
27110 ALL_OBJFILES (objfile)
27111 {
27112 struct stat st;
27113
27114 /* If the objfile does not correspond to an actual file, skip it. */
27115 if (stat (objfile_name (objfile), &st) < 0)
27116 continue;
27117
27118 struct dwarf2_per_objfile *dwarf2_per_objfile
27119 = get_dwarf2_per_objfile (objfile);
27120
27121 if (dwarf2_per_objfile != NULL)
27122 {
27123 TRY
27124 {
27125 write_psymtabs_to_index (dwarf2_per_objfile, arg, index_kind);
27126 }
27127 CATCH (except, RETURN_MASK_ERROR)
27128 {
27129 exception_fprintf (gdb_stderr, except,
27130 _("Error while writing index for `%s': "),
27131 objfile_name (objfile));
27132 }
27133 END_CATCH
27134 }
27135
27136 }
27137 }
27138
27139 \f
27140
27141 int dwarf_always_disassemble;
27142
27143 static void
27144 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
27145 struct cmd_list_element *c, const char *value)
27146 {
27147 fprintf_filtered (file,
27148 _("Whether to always disassemble "
27149 "DWARF expressions is %s.\n"),
27150 value);
27151 }
27152
27153 static void
27154 show_check_physname (struct ui_file *file, int from_tty,
27155 struct cmd_list_element *c, const char *value)
27156 {
27157 fprintf_filtered (file,
27158 _("Whether to check \"physname\" is %s.\n"),
27159 value);
27160 }
27161
27162 void
27163 _initialize_dwarf2_read (void)
27164 {
27165 struct cmd_list_element *c;
27166
27167 dwarf2_objfile_data_key = register_objfile_data ();
27168
27169 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
27170 Set DWARF specific variables.\n\
27171 Configure DWARF variables such as the cache size"),
27172 &set_dwarf_cmdlist, "maintenance set dwarf ",
27173 0/*allow-unknown*/, &maintenance_set_cmdlist);
27174
27175 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
27176 Show DWARF specific variables\n\
27177 Show DWARF variables such as the cache size"),
27178 &show_dwarf_cmdlist, "maintenance show dwarf ",
27179 0/*allow-unknown*/, &maintenance_show_cmdlist);
27180
27181 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
27182 &dwarf_max_cache_age, _("\
27183 Set the upper bound on the age of cached DWARF compilation units."), _("\
27184 Show the upper bound on the age of cached DWARF compilation units."), _("\
27185 A higher limit means that cached compilation units will be stored\n\
27186 in memory longer, and more total memory will be used. Zero disables\n\
27187 caching, which can slow down startup."),
27188 NULL,
27189 show_dwarf_max_cache_age,
27190 &set_dwarf_cmdlist,
27191 &show_dwarf_cmdlist);
27192
27193 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
27194 &dwarf_always_disassemble, _("\
27195 Set whether `info address' always disassembles DWARF expressions."), _("\
27196 Show whether `info address' always disassembles DWARF expressions."), _("\
27197 When enabled, DWARF expressions are always printed in an assembly-like\n\
27198 syntax. When disabled, expressions will be printed in a more\n\
27199 conversational style, when possible."),
27200 NULL,
27201 show_dwarf_always_disassemble,
27202 &set_dwarf_cmdlist,
27203 &show_dwarf_cmdlist);
27204
27205 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27206 Set debugging of the DWARF reader."), _("\
27207 Show debugging of the DWARF reader."), _("\
27208 When enabled (non-zero), debugging messages are printed during DWARF\n\
27209 reading and symtab expansion. A value of 1 (one) provides basic\n\
27210 information. A value greater than 1 provides more verbose information."),
27211 NULL,
27212 NULL,
27213 &setdebuglist, &showdebuglist);
27214
27215 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27216 Set debugging of the DWARF DIE reader."), _("\
27217 Show debugging of the DWARF DIE reader."), _("\
27218 When enabled (non-zero), DIEs are dumped after they are read in.\n\
27219 The value is the maximum depth to print."),
27220 NULL,
27221 NULL,
27222 &setdebuglist, &showdebuglist);
27223
27224 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27225 Set debugging of the dwarf line reader."), _("\
27226 Show debugging of the dwarf line reader."), _("\
27227 When enabled (non-zero), line number entries are dumped as they are read in.\n\
27228 A value of 1 (one) provides basic information.\n\
27229 A value greater than 1 provides more verbose information."),
27230 NULL,
27231 NULL,
27232 &setdebuglist, &showdebuglist);
27233
27234 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27235 Set cross-checking of \"physname\" code against demangler."), _("\
27236 Show cross-checking of \"physname\" code against demangler."), _("\
27237 When enabled, GDB's internal \"physname\" code is checked against\n\
27238 the demangler."),
27239 NULL, show_check_physname,
27240 &setdebuglist, &showdebuglist);
27241
27242 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27243 no_class, &use_deprecated_index_sections, _("\
27244 Set whether to use deprecated gdb_index sections."), _("\
27245 Show whether to use deprecated gdb_index sections."), _("\
27246 When enabled, deprecated .gdb_index sections are used anyway.\n\
27247 Normally they are ignored either because of a missing feature or\n\
27248 performance issue.\n\
27249 Warning: This option must be enabled before gdb reads the file."),
27250 NULL,
27251 NULL,
27252 &setlist, &showlist);
27253
27254 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
27255 _("\
27256 Save a gdb-index file.\n\
27257 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27258 \n\
27259 No options create one file with .gdb-index extension for pre-DWARF-5\n\
27260 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27261 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
27262 &save_cmdlist);
27263 set_cmd_completer (c, filename_completer);
27264
27265 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27266 &dwarf2_locexpr_funcs);
27267 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27268 &dwarf2_loclist_funcs);
27269
27270 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27271 &dwarf2_block_frame_base_locexpr_funcs);
27272 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27273 &dwarf2_block_frame_base_loclist_funcs);
27274
27275 #if GDB_SELF_TEST
27276 selftests::register_test ("dw2_expand_symtabs_matching",
27277 selftests::dw2_expand_symtabs_matching::run_test);
27278 #endif
27279 }
This page took 0.640337 seconds and 5 git commands to generate.