Remove one argument abbrev_len in read_partial_die
[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 const gdb_byte *);
1838
1839 static struct partial_die_info *find_partial_die (sect_offset, int,
1840 struct dwarf2_cu *);
1841
1842 static void fixup_partial_die (struct partial_die_info *,
1843 struct dwarf2_cu *);
1844
1845 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1846 struct attribute *, struct attr_abbrev *,
1847 const gdb_byte *);
1848
1849 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1850
1851 static int read_1_signed_byte (bfd *, const gdb_byte *);
1852
1853 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1854
1855 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1856
1857 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1858
1859 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1860 unsigned int *);
1861
1862 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1863
1864 static LONGEST read_checked_initial_length_and_offset
1865 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1866 unsigned int *, unsigned int *);
1867
1868 static LONGEST read_offset (bfd *, const gdb_byte *,
1869 const struct comp_unit_head *,
1870 unsigned int *);
1871
1872 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1873
1874 static sect_offset read_abbrev_offset
1875 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1876 struct dwarf2_section_info *, sect_offset);
1877
1878 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1879
1880 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1881
1882 static const char *read_indirect_string
1883 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1884 const struct comp_unit_head *, unsigned int *);
1885
1886 static const char *read_indirect_line_string
1887 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1888 const struct comp_unit_head *, unsigned int *);
1889
1890 static const char *read_indirect_string_at_offset
1891 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1892 LONGEST str_offset);
1893
1894 static const char *read_indirect_string_from_dwz
1895 (struct objfile *objfile, struct dwz_file *, LONGEST);
1896
1897 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1898
1899 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1900 const gdb_byte *,
1901 unsigned int *);
1902
1903 static const char *read_str_index (const struct die_reader_specs *reader,
1904 ULONGEST str_index);
1905
1906 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1907
1908 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1909 struct dwarf2_cu *);
1910
1911 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1912 unsigned int);
1913
1914 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1915 struct dwarf2_cu *cu);
1916
1917 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1918 struct dwarf2_cu *cu);
1919
1920 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1921
1922 static struct die_info *die_specification (struct die_info *die,
1923 struct dwarf2_cu **);
1924
1925 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1926 struct dwarf2_cu *cu);
1927
1928 static void dwarf_decode_lines (struct line_header *, const char *,
1929 struct dwarf2_cu *, struct partial_symtab *,
1930 CORE_ADDR, int decode_mapping);
1931
1932 static void dwarf2_start_subfile (const char *, const char *);
1933
1934 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1935 const char *, const char *,
1936 CORE_ADDR);
1937
1938 static struct symbol *new_symbol (struct die_info *, struct type *,
1939 struct dwarf2_cu *, struct symbol * = NULL);
1940
1941 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1942 struct dwarf2_cu *);
1943
1944 static void dwarf2_const_value_attr (const struct attribute *attr,
1945 struct type *type,
1946 const char *name,
1947 struct obstack *obstack,
1948 struct dwarf2_cu *cu, LONGEST *value,
1949 const gdb_byte **bytes,
1950 struct dwarf2_locexpr_baton **baton);
1951
1952 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1953
1954 static int need_gnat_info (struct dwarf2_cu *);
1955
1956 static struct type *die_descriptive_type (struct die_info *,
1957 struct dwarf2_cu *);
1958
1959 static void set_descriptive_type (struct type *, struct die_info *,
1960 struct dwarf2_cu *);
1961
1962 static struct type *die_containing_type (struct die_info *,
1963 struct dwarf2_cu *);
1964
1965 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1966 struct dwarf2_cu *);
1967
1968 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1969
1970 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1971
1972 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1973
1974 static char *typename_concat (struct obstack *obs, const char *prefix,
1975 const char *suffix, int physname,
1976 struct dwarf2_cu *cu);
1977
1978 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1979
1980 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1981
1982 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1983
1984 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1985
1986 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1987
1988 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1989
1990 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1991 struct dwarf2_cu *, struct partial_symtab *);
1992
1993 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1994 values. Keep the items ordered with increasing constraints compliance. */
1995 enum pc_bounds_kind
1996 {
1997 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1998 PC_BOUNDS_NOT_PRESENT,
1999
2000 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
2001 were present but they do not form a valid range of PC addresses. */
2002 PC_BOUNDS_INVALID,
2003
2004 /* Discontiguous range was found - that is DW_AT_ranges was found. */
2005 PC_BOUNDS_RANGES,
2006
2007 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
2008 PC_BOUNDS_HIGH_LOW,
2009 };
2010
2011 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
2012 CORE_ADDR *, CORE_ADDR *,
2013 struct dwarf2_cu *,
2014 struct partial_symtab *);
2015
2016 static void get_scope_pc_bounds (struct die_info *,
2017 CORE_ADDR *, CORE_ADDR *,
2018 struct dwarf2_cu *);
2019
2020 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
2021 CORE_ADDR, struct dwarf2_cu *);
2022
2023 static void dwarf2_add_field (struct field_info *, struct die_info *,
2024 struct dwarf2_cu *);
2025
2026 static void dwarf2_attach_fields_to_type (struct field_info *,
2027 struct type *, struct dwarf2_cu *);
2028
2029 static void dwarf2_add_member_fn (struct field_info *,
2030 struct die_info *, struct type *,
2031 struct dwarf2_cu *);
2032
2033 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
2034 struct type *,
2035 struct dwarf2_cu *);
2036
2037 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
2038
2039 static void read_common_block (struct die_info *, struct dwarf2_cu *);
2040
2041 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
2042
2043 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
2044
2045 static struct using_direct **using_directives (enum language);
2046
2047 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
2048
2049 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
2050
2051 static struct type *read_module_type (struct die_info *die,
2052 struct dwarf2_cu *cu);
2053
2054 static const char *namespace_name (struct die_info *die,
2055 int *is_anonymous, struct dwarf2_cu *);
2056
2057 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
2058
2059 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
2060
2061 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
2062 struct dwarf2_cu *);
2063
2064 static struct die_info *read_die_and_siblings_1
2065 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
2066 struct die_info *);
2067
2068 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
2069 const gdb_byte *info_ptr,
2070 const gdb_byte **new_info_ptr,
2071 struct die_info *parent);
2072
2073 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
2074 struct die_info **, const gdb_byte *,
2075 int *, int);
2076
2077 static const gdb_byte *read_full_die (const struct die_reader_specs *,
2078 struct die_info **, const gdb_byte *,
2079 int *);
2080
2081 static void process_die (struct die_info *, struct dwarf2_cu *);
2082
2083 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
2084 struct obstack *);
2085
2086 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
2087
2088 static const char *dwarf2_full_name (const char *name,
2089 struct die_info *die,
2090 struct dwarf2_cu *cu);
2091
2092 static const char *dwarf2_physname (const char *name, struct die_info *die,
2093 struct dwarf2_cu *cu);
2094
2095 static struct die_info *dwarf2_extension (struct die_info *die,
2096 struct dwarf2_cu **);
2097
2098 static const char *dwarf_tag_name (unsigned int);
2099
2100 static const char *dwarf_attr_name (unsigned int);
2101
2102 static const char *dwarf_form_name (unsigned int);
2103
2104 static const char *dwarf_bool_name (unsigned int);
2105
2106 static const char *dwarf_type_encoding_name (unsigned int);
2107
2108 static struct die_info *sibling_die (struct die_info *);
2109
2110 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
2111
2112 static void dump_die_for_error (struct die_info *);
2113
2114 static void dump_die_1 (struct ui_file *, int level, int max_level,
2115 struct die_info *);
2116
2117 /*static*/ void dump_die (struct die_info *, int max_level);
2118
2119 static void store_in_ref_table (struct die_info *,
2120 struct dwarf2_cu *);
2121
2122 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
2123
2124 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
2125
2126 static struct die_info *follow_die_ref_or_sig (struct die_info *,
2127 const struct attribute *,
2128 struct dwarf2_cu **);
2129
2130 static struct die_info *follow_die_ref (struct die_info *,
2131 const struct attribute *,
2132 struct dwarf2_cu **);
2133
2134 static struct die_info *follow_die_sig (struct die_info *,
2135 const struct attribute *,
2136 struct dwarf2_cu **);
2137
2138 static struct type *get_signatured_type (struct die_info *, ULONGEST,
2139 struct dwarf2_cu *);
2140
2141 static struct type *get_DW_AT_signature_type (struct die_info *,
2142 const struct attribute *,
2143 struct dwarf2_cu *);
2144
2145 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
2146
2147 static void read_signatured_type (struct signatured_type *);
2148
2149 static int attr_to_dynamic_prop (const struct attribute *attr,
2150 struct die_info *die, struct dwarf2_cu *cu,
2151 struct dynamic_prop *prop);
2152
2153 /* memory allocation interface */
2154
2155 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
2156
2157 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
2158
2159 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2160
2161 static int attr_form_is_block (const struct attribute *);
2162
2163 static int attr_form_is_section_offset (const struct attribute *);
2164
2165 static int attr_form_is_constant (const struct attribute *);
2166
2167 static int attr_form_is_ref (const struct attribute *);
2168
2169 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2170 struct dwarf2_loclist_baton *baton,
2171 const struct attribute *attr);
2172
2173 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
2174 struct symbol *sym,
2175 struct dwarf2_cu *cu,
2176 int is_block);
2177
2178 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2179 const gdb_byte *info_ptr,
2180 struct abbrev_info *abbrev);
2181
2182 static hashval_t partial_die_hash (const void *item);
2183
2184 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2185
2186 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
2187 (sect_offset sect_off, unsigned int offset_in_dwz,
2188 struct dwarf2_per_objfile *dwarf2_per_objfile);
2189
2190 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
2191 struct die_info *comp_unit_die,
2192 enum language pretend_language);
2193
2194 static void free_cached_comp_units (void *);
2195
2196 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2197
2198 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
2199
2200 static struct type *set_die_type (struct die_info *, struct type *,
2201 struct dwarf2_cu *);
2202
2203 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2204
2205 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2206
2207 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2208 enum language);
2209
2210 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2211 enum language);
2212
2213 static void process_full_type_unit (struct dwarf2_per_cu_data *,
2214 enum language);
2215
2216 static void dwarf2_add_dependence (struct dwarf2_cu *,
2217 struct dwarf2_per_cu_data *);
2218
2219 static void dwarf2_mark (struct dwarf2_cu *);
2220
2221 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2222
2223 static struct type *get_die_type_at_offset (sect_offset,
2224 struct dwarf2_per_cu_data *);
2225
2226 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
2227
2228 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2229 enum language pretend_language);
2230
2231 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
2232
2233 /* Class, the destructor of which frees all allocated queue entries. This
2234 will only have work to do if an error was thrown while processing the
2235 dwarf. If no error was thrown then the queue entries should have all
2236 been processed, and freed, as we went along. */
2237
2238 class dwarf2_queue_guard
2239 {
2240 public:
2241 dwarf2_queue_guard () = default;
2242
2243 /* Free any entries remaining on the queue. There should only be
2244 entries left if we hit an error while processing the dwarf. */
2245 ~dwarf2_queue_guard ()
2246 {
2247 struct dwarf2_queue_item *item, *last;
2248
2249 item = dwarf2_queue;
2250 while (item)
2251 {
2252 /* Anything still marked queued is likely to be in an
2253 inconsistent state, so discard it. */
2254 if (item->per_cu->queued)
2255 {
2256 if (item->per_cu->cu != NULL)
2257 free_one_cached_comp_unit (item->per_cu);
2258 item->per_cu->queued = 0;
2259 }
2260
2261 last = item;
2262 item = item->next;
2263 xfree (last);
2264 }
2265
2266 dwarf2_queue = dwarf2_queue_tail = NULL;
2267 }
2268 };
2269
2270 /* The return type of find_file_and_directory. Note, the enclosed
2271 string pointers are only valid while this object is valid. */
2272
2273 struct file_and_directory
2274 {
2275 /* The filename. This is never NULL. */
2276 const char *name;
2277
2278 /* The compilation directory. NULL if not known. If we needed to
2279 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2280 points directly to the DW_AT_comp_dir string attribute owned by
2281 the obstack that owns the DIE. */
2282 const char *comp_dir;
2283
2284 /* If we needed to build a new string for comp_dir, this is what
2285 owns the storage. */
2286 std::string comp_dir_storage;
2287 };
2288
2289 static file_and_directory find_file_and_directory (struct die_info *die,
2290 struct dwarf2_cu *cu);
2291
2292 static char *file_full_name (int file, struct line_header *lh,
2293 const char *comp_dir);
2294
2295 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2296 enum class rcuh_kind { COMPILE, TYPE };
2297
2298 static const gdb_byte *read_and_check_comp_unit_head
2299 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2300 struct comp_unit_head *header,
2301 struct dwarf2_section_info *section,
2302 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2303 rcuh_kind section_kind);
2304
2305 static void init_cutu_and_read_dies
2306 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2307 int use_existing_cu, int keep,
2308 die_reader_func_ftype *die_reader_func, void *data);
2309
2310 static void init_cutu_and_read_dies_simple
2311 (struct dwarf2_per_cu_data *this_cu,
2312 die_reader_func_ftype *die_reader_func, void *data);
2313
2314 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2315
2316 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2317
2318 static struct dwo_unit *lookup_dwo_unit_in_dwp
2319 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2320 struct dwp_file *dwp_file, const char *comp_dir,
2321 ULONGEST signature, int is_debug_types);
2322
2323 static struct dwp_file *get_dwp_file
2324 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2325
2326 static struct dwo_unit *lookup_dwo_comp_unit
2327 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2328
2329 static struct dwo_unit *lookup_dwo_type_unit
2330 (struct signatured_type *, const char *, const char *);
2331
2332 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2333
2334 static void free_dwo_file_cleanup (void *);
2335
2336 struct free_dwo_file_cleanup_data
2337 {
2338 struct dwo_file *dwo_file;
2339 struct dwarf2_per_objfile *dwarf2_per_objfile;
2340 };
2341
2342 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2343
2344 static void check_producer (struct dwarf2_cu *cu);
2345
2346 static void free_line_header_voidp (void *arg);
2347 \f
2348 /* Various complaints about symbol reading that don't abort the process. */
2349
2350 static void
2351 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2352 {
2353 complaint (&symfile_complaints,
2354 _("statement list doesn't fit in .debug_line section"));
2355 }
2356
2357 static void
2358 dwarf2_debug_line_missing_file_complaint (void)
2359 {
2360 complaint (&symfile_complaints,
2361 _(".debug_line section has line data without a file"));
2362 }
2363
2364 static void
2365 dwarf2_debug_line_missing_end_sequence_complaint (void)
2366 {
2367 complaint (&symfile_complaints,
2368 _(".debug_line section has line "
2369 "program sequence without an end"));
2370 }
2371
2372 static void
2373 dwarf2_complex_location_expr_complaint (void)
2374 {
2375 complaint (&symfile_complaints, _("location expression too complex"));
2376 }
2377
2378 static void
2379 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2380 int arg3)
2381 {
2382 complaint (&symfile_complaints,
2383 _("const value length mismatch for '%s', got %d, expected %d"),
2384 arg1, arg2, arg3);
2385 }
2386
2387 static void
2388 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2389 {
2390 complaint (&symfile_complaints,
2391 _("debug info runs off end of %s section"
2392 " [in module %s]"),
2393 get_section_name (section),
2394 get_section_file_name (section));
2395 }
2396
2397 static void
2398 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2399 {
2400 complaint (&symfile_complaints,
2401 _("macro debug info contains a "
2402 "malformed macro definition:\n`%s'"),
2403 arg1);
2404 }
2405
2406 static void
2407 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2408 {
2409 complaint (&symfile_complaints,
2410 _("invalid attribute class or form for '%s' in '%s'"),
2411 arg1, arg2);
2412 }
2413
2414 /* Hash function for line_header_hash. */
2415
2416 static hashval_t
2417 line_header_hash (const struct line_header *ofs)
2418 {
2419 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2420 }
2421
2422 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2423
2424 static hashval_t
2425 line_header_hash_voidp (const void *item)
2426 {
2427 const struct line_header *ofs = (const struct line_header *) item;
2428
2429 return line_header_hash (ofs);
2430 }
2431
2432 /* Equality function for line_header_hash. */
2433
2434 static int
2435 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2436 {
2437 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2438 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2439
2440 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2441 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2442 }
2443
2444 \f
2445
2446 /* Read the given attribute value as an address, taking the attribute's
2447 form into account. */
2448
2449 static CORE_ADDR
2450 attr_value_as_address (struct attribute *attr)
2451 {
2452 CORE_ADDR addr;
2453
2454 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2455 {
2456 /* Aside from a few clearly defined exceptions, attributes that
2457 contain an address must always be in DW_FORM_addr form.
2458 Unfortunately, some compilers happen to be violating this
2459 requirement by encoding addresses using other forms, such
2460 as DW_FORM_data4 for example. For those broken compilers,
2461 we try to do our best, without any guarantee of success,
2462 to interpret the address correctly. It would also be nice
2463 to generate a complaint, but that would require us to maintain
2464 a list of legitimate cases where a non-address form is allowed,
2465 as well as update callers to pass in at least the CU's DWARF
2466 version. This is more overhead than what we're willing to
2467 expand for a pretty rare case. */
2468 addr = DW_UNSND (attr);
2469 }
2470 else
2471 addr = DW_ADDR (attr);
2472
2473 return addr;
2474 }
2475
2476 /* The suffix for an index file. */
2477 #define INDEX4_SUFFIX ".gdb-index"
2478 #define INDEX5_SUFFIX ".debug_names"
2479 #define DEBUG_STR_SUFFIX ".debug_str"
2480
2481 /* See declaration. */
2482
2483 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2484 const dwarf2_debug_sections *names)
2485 : objfile (objfile_)
2486 {
2487 if (names == NULL)
2488 names = &dwarf2_elf_names;
2489
2490 bfd *obfd = objfile->obfd;
2491
2492 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2493 locate_sections (obfd, sec, *names);
2494 }
2495
2496 static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2497
2498 dwarf2_per_objfile::~dwarf2_per_objfile ()
2499 {
2500 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2501 free_cached_comp_units ();
2502
2503 if (quick_file_names_table)
2504 htab_delete (quick_file_names_table);
2505
2506 if (line_header_hash)
2507 htab_delete (line_header_hash);
2508
2509 for (int ix = 0; ix < n_comp_units; ++ix)
2510 VEC_free (dwarf2_per_cu_ptr, all_comp_units[ix]->imported_symtabs);
2511
2512 for (int ix = 0; ix < n_type_units; ++ix)
2513 VEC_free (dwarf2_per_cu_ptr,
2514 all_type_units[ix]->per_cu.imported_symtabs);
2515 xfree (all_type_units);
2516
2517 VEC_free (dwarf2_section_info_def, types);
2518
2519 if (dwo_files != NULL)
2520 free_dwo_files (dwo_files, objfile);
2521 if (dwp_file != NULL)
2522 gdb_bfd_unref (dwp_file->dbfd);
2523
2524 if (dwz_file != NULL && dwz_file->dwz_bfd)
2525 gdb_bfd_unref (dwz_file->dwz_bfd);
2526
2527 if (index_table != NULL)
2528 index_table->~mapped_index ();
2529
2530 /* Everything else should be on the objfile obstack. */
2531 }
2532
2533 /* See declaration. */
2534
2535 void
2536 dwarf2_per_objfile::free_cached_comp_units ()
2537 {
2538 dwarf2_per_cu_data *per_cu = read_in_chain;
2539 dwarf2_per_cu_data **last_chain = &read_in_chain;
2540 while (per_cu != NULL)
2541 {
2542 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2543
2544 delete per_cu->cu;
2545 *last_chain = next_cu;
2546 per_cu = next_cu;
2547 }
2548 }
2549
2550 /* Try to locate the sections we need for DWARF 2 debugging
2551 information and return true if we have enough to do something.
2552 NAMES points to the dwarf2 section names, or is NULL if the standard
2553 ELF names are used. */
2554
2555 int
2556 dwarf2_has_info (struct objfile *objfile,
2557 const struct dwarf2_debug_sections *names)
2558 {
2559 if (objfile->flags & OBJF_READNEVER)
2560 return 0;
2561
2562 struct dwarf2_per_objfile *dwarf2_per_objfile
2563 = get_dwarf2_per_objfile (objfile);
2564
2565 if (dwarf2_per_objfile == NULL)
2566 {
2567 /* Initialize per-objfile state. */
2568 dwarf2_per_objfile
2569 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2570 names);
2571 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
2572 }
2573 return (!dwarf2_per_objfile->info.is_virtual
2574 && dwarf2_per_objfile->info.s.section != NULL
2575 && !dwarf2_per_objfile->abbrev.is_virtual
2576 && dwarf2_per_objfile->abbrev.s.section != NULL);
2577 }
2578
2579 /* Return the containing section of virtual section SECTION. */
2580
2581 static struct dwarf2_section_info *
2582 get_containing_section (const struct dwarf2_section_info *section)
2583 {
2584 gdb_assert (section->is_virtual);
2585 return section->s.containing_section;
2586 }
2587
2588 /* Return the bfd owner of SECTION. */
2589
2590 static struct bfd *
2591 get_section_bfd_owner (const struct dwarf2_section_info *section)
2592 {
2593 if (section->is_virtual)
2594 {
2595 section = get_containing_section (section);
2596 gdb_assert (!section->is_virtual);
2597 }
2598 return section->s.section->owner;
2599 }
2600
2601 /* Return the bfd section of SECTION.
2602 Returns NULL if the section is not present. */
2603
2604 static asection *
2605 get_section_bfd_section (const struct dwarf2_section_info *section)
2606 {
2607 if (section->is_virtual)
2608 {
2609 section = get_containing_section (section);
2610 gdb_assert (!section->is_virtual);
2611 }
2612 return section->s.section;
2613 }
2614
2615 /* Return the name of SECTION. */
2616
2617 static const char *
2618 get_section_name (const struct dwarf2_section_info *section)
2619 {
2620 asection *sectp = get_section_bfd_section (section);
2621
2622 gdb_assert (sectp != NULL);
2623 return bfd_section_name (get_section_bfd_owner (section), sectp);
2624 }
2625
2626 /* Return the name of the file SECTION is in. */
2627
2628 static const char *
2629 get_section_file_name (const struct dwarf2_section_info *section)
2630 {
2631 bfd *abfd = get_section_bfd_owner (section);
2632
2633 return bfd_get_filename (abfd);
2634 }
2635
2636 /* Return the id of SECTION.
2637 Returns 0 if SECTION doesn't exist. */
2638
2639 static int
2640 get_section_id (const struct dwarf2_section_info *section)
2641 {
2642 asection *sectp = get_section_bfd_section (section);
2643
2644 if (sectp == NULL)
2645 return 0;
2646 return sectp->id;
2647 }
2648
2649 /* Return the flags of SECTION.
2650 SECTION (or containing section if this is a virtual section) must exist. */
2651
2652 static int
2653 get_section_flags (const struct dwarf2_section_info *section)
2654 {
2655 asection *sectp = get_section_bfd_section (section);
2656
2657 gdb_assert (sectp != NULL);
2658 return bfd_get_section_flags (sectp->owner, sectp);
2659 }
2660
2661 /* When loading sections, we look either for uncompressed section or for
2662 compressed section names. */
2663
2664 static int
2665 section_is_p (const char *section_name,
2666 const struct dwarf2_section_names *names)
2667 {
2668 if (names->normal != NULL
2669 && strcmp (section_name, names->normal) == 0)
2670 return 1;
2671 if (names->compressed != NULL
2672 && strcmp (section_name, names->compressed) == 0)
2673 return 1;
2674 return 0;
2675 }
2676
2677 /* See declaration. */
2678
2679 void
2680 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2681 const dwarf2_debug_sections &names)
2682 {
2683 flagword aflag = bfd_get_section_flags (abfd, sectp);
2684
2685 if ((aflag & SEC_HAS_CONTENTS) == 0)
2686 {
2687 }
2688 else if (section_is_p (sectp->name, &names.info))
2689 {
2690 this->info.s.section = sectp;
2691 this->info.size = bfd_get_section_size (sectp);
2692 }
2693 else if (section_is_p (sectp->name, &names.abbrev))
2694 {
2695 this->abbrev.s.section = sectp;
2696 this->abbrev.size = bfd_get_section_size (sectp);
2697 }
2698 else if (section_is_p (sectp->name, &names.line))
2699 {
2700 this->line.s.section = sectp;
2701 this->line.size = bfd_get_section_size (sectp);
2702 }
2703 else if (section_is_p (sectp->name, &names.loc))
2704 {
2705 this->loc.s.section = sectp;
2706 this->loc.size = bfd_get_section_size (sectp);
2707 }
2708 else if (section_is_p (sectp->name, &names.loclists))
2709 {
2710 this->loclists.s.section = sectp;
2711 this->loclists.size = bfd_get_section_size (sectp);
2712 }
2713 else if (section_is_p (sectp->name, &names.macinfo))
2714 {
2715 this->macinfo.s.section = sectp;
2716 this->macinfo.size = bfd_get_section_size (sectp);
2717 }
2718 else if (section_is_p (sectp->name, &names.macro))
2719 {
2720 this->macro.s.section = sectp;
2721 this->macro.size = bfd_get_section_size (sectp);
2722 }
2723 else if (section_is_p (sectp->name, &names.str))
2724 {
2725 this->str.s.section = sectp;
2726 this->str.size = bfd_get_section_size (sectp);
2727 }
2728 else if (section_is_p (sectp->name, &names.line_str))
2729 {
2730 this->line_str.s.section = sectp;
2731 this->line_str.size = bfd_get_section_size (sectp);
2732 }
2733 else if (section_is_p (sectp->name, &names.addr))
2734 {
2735 this->addr.s.section = sectp;
2736 this->addr.size = bfd_get_section_size (sectp);
2737 }
2738 else if (section_is_p (sectp->name, &names.frame))
2739 {
2740 this->frame.s.section = sectp;
2741 this->frame.size = bfd_get_section_size (sectp);
2742 }
2743 else if (section_is_p (sectp->name, &names.eh_frame))
2744 {
2745 this->eh_frame.s.section = sectp;
2746 this->eh_frame.size = bfd_get_section_size (sectp);
2747 }
2748 else if (section_is_p (sectp->name, &names.ranges))
2749 {
2750 this->ranges.s.section = sectp;
2751 this->ranges.size = bfd_get_section_size (sectp);
2752 }
2753 else if (section_is_p (sectp->name, &names.rnglists))
2754 {
2755 this->rnglists.s.section = sectp;
2756 this->rnglists.size = bfd_get_section_size (sectp);
2757 }
2758 else if (section_is_p (sectp->name, &names.types))
2759 {
2760 struct dwarf2_section_info type_section;
2761
2762 memset (&type_section, 0, sizeof (type_section));
2763 type_section.s.section = sectp;
2764 type_section.size = bfd_get_section_size (sectp);
2765
2766 VEC_safe_push (dwarf2_section_info_def, this->types,
2767 &type_section);
2768 }
2769 else if (section_is_p (sectp->name, &names.gdb_index))
2770 {
2771 this->gdb_index.s.section = sectp;
2772 this->gdb_index.size = bfd_get_section_size (sectp);
2773 }
2774 else if (section_is_p (sectp->name, &names.debug_names))
2775 {
2776 this->debug_names.s.section = sectp;
2777 this->debug_names.size = bfd_get_section_size (sectp);
2778 }
2779 else if (section_is_p (sectp->name, &names.debug_aranges))
2780 {
2781 this->debug_aranges.s.section = sectp;
2782 this->debug_aranges.size = bfd_get_section_size (sectp);
2783 }
2784
2785 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2786 && bfd_section_vma (abfd, sectp) == 0)
2787 this->has_section_at_zero = true;
2788 }
2789
2790 /* A helper function that decides whether a section is empty,
2791 or not present. */
2792
2793 static int
2794 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2795 {
2796 if (section->is_virtual)
2797 return section->size == 0;
2798 return section->s.section == NULL || section->size == 0;
2799 }
2800
2801 /* Read the contents of the section INFO.
2802 OBJFILE is the main object file, but not necessarily the file where
2803 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2804 of the DWO file.
2805 If the section is compressed, uncompress it before returning. */
2806
2807 static void
2808 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2809 {
2810 asection *sectp;
2811 bfd *abfd;
2812 gdb_byte *buf, *retbuf;
2813
2814 if (info->readin)
2815 return;
2816 info->buffer = NULL;
2817 info->readin = 1;
2818
2819 if (dwarf2_section_empty_p (info))
2820 return;
2821
2822 sectp = get_section_bfd_section (info);
2823
2824 /* If this is a virtual section we need to read in the real one first. */
2825 if (info->is_virtual)
2826 {
2827 struct dwarf2_section_info *containing_section =
2828 get_containing_section (info);
2829
2830 gdb_assert (sectp != NULL);
2831 if ((sectp->flags & SEC_RELOC) != 0)
2832 {
2833 error (_("Dwarf Error: DWP format V2 with relocations is not"
2834 " supported in section %s [in module %s]"),
2835 get_section_name (info), get_section_file_name (info));
2836 }
2837 dwarf2_read_section (objfile, containing_section);
2838 /* Other code should have already caught virtual sections that don't
2839 fit. */
2840 gdb_assert (info->virtual_offset + info->size
2841 <= containing_section->size);
2842 /* If the real section is empty or there was a problem reading the
2843 section we shouldn't get here. */
2844 gdb_assert (containing_section->buffer != NULL);
2845 info->buffer = containing_section->buffer + info->virtual_offset;
2846 return;
2847 }
2848
2849 /* If the section has relocations, we must read it ourselves.
2850 Otherwise we attach it to the BFD. */
2851 if ((sectp->flags & SEC_RELOC) == 0)
2852 {
2853 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2854 return;
2855 }
2856
2857 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2858 info->buffer = buf;
2859
2860 /* When debugging .o files, we may need to apply relocations; see
2861 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2862 We never compress sections in .o files, so we only need to
2863 try this when the section is not compressed. */
2864 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2865 if (retbuf != NULL)
2866 {
2867 info->buffer = retbuf;
2868 return;
2869 }
2870
2871 abfd = get_section_bfd_owner (info);
2872 gdb_assert (abfd != NULL);
2873
2874 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2875 || bfd_bread (buf, info->size, abfd) != info->size)
2876 {
2877 error (_("Dwarf Error: Can't read DWARF data"
2878 " in section %s [in module %s]"),
2879 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2880 }
2881 }
2882
2883 /* A helper function that returns the size of a section in a safe way.
2884 If you are positive that the section has been read before using the
2885 size, then it is safe to refer to the dwarf2_section_info object's
2886 "size" field directly. In other cases, you must call this
2887 function, because for compressed sections the size field is not set
2888 correctly until the section has been read. */
2889
2890 static bfd_size_type
2891 dwarf2_section_size (struct objfile *objfile,
2892 struct dwarf2_section_info *info)
2893 {
2894 if (!info->readin)
2895 dwarf2_read_section (objfile, info);
2896 return info->size;
2897 }
2898
2899 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2900 SECTION_NAME. */
2901
2902 void
2903 dwarf2_get_section_info (struct objfile *objfile,
2904 enum dwarf2_section_enum sect,
2905 asection **sectp, const gdb_byte **bufp,
2906 bfd_size_type *sizep)
2907 {
2908 struct dwarf2_per_objfile *data
2909 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2910 dwarf2_objfile_data_key);
2911 struct dwarf2_section_info *info;
2912
2913 /* We may see an objfile without any DWARF, in which case we just
2914 return nothing. */
2915 if (data == NULL)
2916 {
2917 *sectp = NULL;
2918 *bufp = NULL;
2919 *sizep = 0;
2920 return;
2921 }
2922 switch (sect)
2923 {
2924 case DWARF2_DEBUG_FRAME:
2925 info = &data->frame;
2926 break;
2927 case DWARF2_EH_FRAME:
2928 info = &data->eh_frame;
2929 break;
2930 default:
2931 gdb_assert_not_reached ("unexpected section");
2932 }
2933
2934 dwarf2_read_section (objfile, info);
2935
2936 *sectp = get_section_bfd_section (info);
2937 *bufp = info->buffer;
2938 *sizep = info->size;
2939 }
2940
2941 /* A helper function to find the sections for a .dwz file. */
2942
2943 static void
2944 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2945 {
2946 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2947
2948 /* Note that we only support the standard ELF names, because .dwz
2949 is ELF-only (at the time of writing). */
2950 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2951 {
2952 dwz_file->abbrev.s.section = sectp;
2953 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2954 }
2955 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2956 {
2957 dwz_file->info.s.section = sectp;
2958 dwz_file->info.size = bfd_get_section_size (sectp);
2959 }
2960 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2961 {
2962 dwz_file->str.s.section = sectp;
2963 dwz_file->str.size = bfd_get_section_size (sectp);
2964 }
2965 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2966 {
2967 dwz_file->line.s.section = sectp;
2968 dwz_file->line.size = bfd_get_section_size (sectp);
2969 }
2970 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2971 {
2972 dwz_file->macro.s.section = sectp;
2973 dwz_file->macro.size = bfd_get_section_size (sectp);
2974 }
2975 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2976 {
2977 dwz_file->gdb_index.s.section = sectp;
2978 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2979 }
2980 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2981 {
2982 dwz_file->debug_names.s.section = sectp;
2983 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2984 }
2985 }
2986
2987 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2988 there is no .gnu_debugaltlink section in the file. Error if there
2989 is such a section but the file cannot be found. */
2990
2991 static struct dwz_file *
2992 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2993 {
2994 const char *filename;
2995 struct dwz_file *result;
2996 bfd_size_type buildid_len_arg;
2997 size_t buildid_len;
2998 bfd_byte *buildid;
2999
3000 if (dwarf2_per_objfile->dwz_file != NULL)
3001 return dwarf2_per_objfile->dwz_file;
3002
3003 bfd_set_error (bfd_error_no_error);
3004 gdb::unique_xmalloc_ptr<char> data
3005 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
3006 &buildid_len_arg, &buildid));
3007 if (data == NULL)
3008 {
3009 if (bfd_get_error () == bfd_error_no_error)
3010 return NULL;
3011 error (_("could not read '.gnu_debugaltlink' section: %s"),
3012 bfd_errmsg (bfd_get_error ()));
3013 }
3014
3015 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
3016
3017 buildid_len = (size_t) buildid_len_arg;
3018
3019 filename = data.get ();
3020
3021 std::string abs_storage;
3022 if (!IS_ABSOLUTE_PATH (filename))
3023 {
3024 gdb::unique_xmalloc_ptr<char> abs
3025 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
3026
3027 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
3028 filename = abs_storage.c_str ();
3029 }
3030
3031 /* First try the file name given in the section. If that doesn't
3032 work, try to use the build-id instead. */
3033 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
3034 if (dwz_bfd != NULL)
3035 {
3036 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
3037 dwz_bfd.release ();
3038 }
3039
3040 if (dwz_bfd == NULL)
3041 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
3042
3043 if (dwz_bfd == NULL)
3044 error (_("could not find '.gnu_debugaltlink' file for %s"),
3045 objfile_name (dwarf2_per_objfile->objfile));
3046
3047 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
3048 struct dwz_file);
3049 result->dwz_bfd = dwz_bfd.release ();
3050
3051 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
3052
3053 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
3054 dwarf2_per_objfile->dwz_file = result;
3055 return result;
3056 }
3057 \f
3058 /* DWARF quick_symbols_functions support. */
3059
3060 /* TUs can share .debug_line entries, and there can be a lot more TUs than
3061 unique line tables, so we maintain a separate table of all .debug_line
3062 derived entries to support the sharing.
3063 All the quick functions need is the list of file names. We discard the
3064 line_header when we're done and don't need to record it here. */
3065 struct quick_file_names
3066 {
3067 /* The data used to construct the hash key. */
3068 struct stmt_list_hash hash;
3069
3070 /* The number of entries in file_names, real_names. */
3071 unsigned int num_file_names;
3072
3073 /* The file names from the line table, after being run through
3074 file_full_name. */
3075 const char **file_names;
3076
3077 /* The file names from the line table after being run through
3078 gdb_realpath. These are computed lazily. */
3079 const char **real_names;
3080 };
3081
3082 /* When using the index (and thus not using psymtabs), each CU has an
3083 object of this type. This is used to hold information needed by
3084 the various "quick" methods. */
3085 struct dwarf2_per_cu_quick_data
3086 {
3087 /* The file table. This can be NULL if there was no file table
3088 or it's currently not read in.
3089 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
3090 struct quick_file_names *file_names;
3091
3092 /* The corresponding symbol table. This is NULL if symbols for this
3093 CU have not yet been read. */
3094 struct compunit_symtab *compunit_symtab;
3095
3096 /* A temporary mark bit used when iterating over all CUs in
3097 expand_symtabs_matching. */
3098 unsigned int mark : 1;
3099
3100 /* True if we've tried to read the file table and found there isn't one.
3101 There will be no point in trying to read it again next time. */
3102 unsigned int no_file_data : 1;
3103 };
3104
3105 /* Utility hash function for a stmt_list_hash. */
3106
3107 static hashval_t
3108 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
3109 {
3110 hashval_t v = 0;
3111
3112 if (stmt_list_hash->dwo_unit != NULL)
3113 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
3114 v += to_underlying (stmt_list_hash->line_sect_off);
3115 return v;
3116 }
3117
3118 /* Utility equality function for a stmt_list_hash. */
3119
3120 static int
3121 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
3122 const struct stmt_list_hash *rhs)
3123 {
3124 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
3125 return 0;
3126 if (lhs->dwo_unit != NULL
3127 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
3128 return 0;
3129
3130 return lhs->line_sect_off == rhs->line_sect_off;
3131 }
3132
3133 /* Hash function for a quick_file_names. */
3134
3135 static hashval_t
3136 hash_file_name_entry (const void *e)
3137 {
3138 const struct quick_file_names *file_data
3139 = (const struct quick_file_names *) e;
3140
3141 return hash_stmt_list_entry (&file_data->hash);
3142 }
3143
3144 /* Equality function for a quick_file_names. */
3145
3146 static int
3147 eq_file_name_entry (const void *a, const void *b)
3148 {
3149 const struct quick_file_names *ea = (const struct quick_file_names *) a;
3150 const struct quick_file_names *eb = (const struct quick_file_names *) b;
3151
3152 return eq_stmt_list_entry (&ea->hash, &eb->hash);
3153 }
3154
3155 /* Delete function for a quick_file_names. */
3156
3157 static void
3158 delete_file_name_entry (void *e)
3159 {
3160 struct quick_file_names *file_data = (struct quick_file_names *) e;
3161 int i;
3162
3163 for (i = 0; i < file_data->num_file_names; ++i)
3164 {
3165 xfree ((void*) file_data->file_names[i]);
3166 if (file_data->real_names)
3167 xfree ((void*) file_data->real_names[i]);
3168 }
3169
3170 /* The space for the struct itself lives on objfile_obstack,
3171 so we don't free it here. */
3172 }
3173
3174 /* Create a quick_file_names hash table. */
3175
3176 static htab_t
3177 create_quick_file_names_table (unsigned int nr_initial_entries)
3178 {
3179 return htab_create_alloc (nr_initial_entries,
3180 hash_file_name_entry, eq_file_name_entry,
3181 delete_file_name_entry, xcalloc, xfree);
3182 }
3183
3184 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3185 have to be created afterwards. You should call age_cached_comp_units after
3186 processing PER_CU->CU. dw2_setup must have been already called. */
3187
3188 static void
3189 load_cu (struct dwarf2_per_cu_data *per_cu)
3190 {
3191 if (per_cu->is_debug_types)
3192 load_full_type_unit (per_cu);
3193 else
3194 load_full_comp_unit (per_cu, language_minimal);
3195
3196 if (per_cu->cu == NULL)
3197 return; /* Dummy CU. */
3198
3199 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
3200 }
3201
3202 /* Read in the symbols for PER_CU. */
3203
3204 static void
3205 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3206 {
3207 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3208
3209 /* Skip type_unit_groups, reading the type units they contain
3210 is handled elsewhere. */
3211 if (IS_TYPE_UNIT_GROUP (per_cu))
3212 return;
3213
3214 /* The destructor of dwarf2_queue_guard frees any entries left on
3215 the queue. After this point we're guaranteed to leave this function
3216 with the dwarf queue empty. */
3217 dwarf2_queue_guard q_guard;
3218
3219 if (dwarf2_per_objfile->using_index
3220 ? per_cu->v.quick->compunit_symtab == NULL
3221 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3222 {
3223 queue_comp_unit (per_cu, language_minimal);
3224 load_cu (per_cu);
3225
3226 /* If we just loaded a CU from a DWO, and we're working with an index
3227 that may badly handle TUs, load all the TUs in that DWO as well.
3228 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3229 if (!per_cu->is_debug_types
3230 && per_cu->cu != NULL
3231 && per_cu->cu->dwo_unit != NULL
3232 && dwarf2_per_objfile->index_table != NULL
3233 && dwarf2_per_objfile->index_table->version <= 7
3234 /* DWP files aren't supported yet. */
3235 && get_dwp_file (dwarf2_per_objfile) == NULL)
3236 queue_and_load_all_dwo_tus (per_cu);
3237 }
3238
3239 process_queue (dwarf2_per_objfile);
3240
3241 /* Age the cache, releasing compilation units that have not
3242 been used recently. */
3243 age_cached_comp_units (dwarf2_per_objfile);
3244 }
3245
3246 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3247 the objfile from which this CU came. Returns the resulting symbol
3248 table. */
3249
3250 static struct compunit_symtab *
3251 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3252 {
3253 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3254
3255 gdb_assert (dwarf2_per_objfile->using_index);
3256 if (!per_cu->v.quick->compunit_symtab)
3257 {
3258 struct cleanup *back_to = make_cleanup (free_cached_comp_units,
3259 dwarf2_per_objfile);
3260 scoped_restore decrementer = increment_reading_symtab ();
3261 dw2_do_instantiate_symtab (per_cu);
3262 process_cu_includes (dwarf2_per_objfile);
3263 do_cleanups (back_to);
3264 }
3265
3266 return per_cu->v.quick->compunit_symtab;
3267 }
3268
3269 /* Return the CU/TU given its index.
3270
3271 This is intended for loops like:
3272
3273 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3274 + dwarf2_per_objfile->n_type_units); ++i)
3275 {
3276 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3277
3278 ...;
3279 }
3280 */
3281
3282 static struct dwarf2_per_cu_data *
3283 dw2_get_cutu (struct dwarf2_per_objfile *dwarf2_per_objfile,
3284 int index)
3285 {
3286 if (index >= dwarf2_per_objfile->n_comp_units)
3287 {
3288 index -= dwarf2_per_objfile->n_comp_units;
3289 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3290 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
3291 }
3292
3293 return dwarf2_per_objfile->all_comp_units[index];
3294 }
3295
3296 /* Return the CU given its index.
3297 This differs from dw2_get_cutu in that it's for when you know INDEX
3298 refers to a CU. */
3299
3300 static struct dwarf2_per_cu_data *
3301 dw2_get_cu (struct dwarf2_per_objfile *dwarf2_per_objfile, int index)
3302 {
3303 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
3304
3305 return dwarf2_per_objfile->all_comp_units[index];
3306 }
3307
3308 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3309 objfile_obstack, and constructed with the specified field
3310 values. */
3311
3312 static dwarf2_per_cu_data *
3313 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3314 struct dwarf2_section_info *section,
3315 int is_dwz,
3316 sect_offset sect_off, ULONGEST length)
3317 {
3318 struct objfile *objfile = dwarf2_per_objfile->objfile;
3319 dwarf2_per_cu_data *the_cu
3320 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3321 struct dwarf2_per_cu_data);
3322 the_cu->sect_off = sect_off;
3323 the_cu->length = length;
3324 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
3325 the_cu->section = section;
3326 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3327 struct dwarf2_per_cu_quick_data);
3328 the_cu->is_dwz = is_dwz;
3329 return the_cu;
3330 }
3331
3332 /* A helper for create_cus_from_index that handles a given list of
3333 CUs. */
3334
3335 static void
3336 create_cus_from_index_list (struct objfile *objfile,
3337 const gdb_byte *cu_list, offset_type n_elements,
3338 struct dwarf2_section_info *section,
3339 int is_dwz,
3340 int base_offset)
3341 {
3342 offset_type i;
3343 struct dwarf2_per_objfile *dwarf2_per_objfile
3344 = get_dwarf2_per_objfile (objfile);
3345
3346 for (i = 0; i < n_elements; i += 2)
3347 {
3348 gdb_static_assert (sizeof (ULONGEST) >= 8);
3349
3350 sect_offset sect_off
3351 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3352 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3353 cu_list += 2 * 8;
3354
3355 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
3356 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3357 sect_off, length);
3358 }
3359 }
3360
3361 /* Read the CU list from the mapped index, and use it to create all
3362 the CU objects for this objfile. */
3363
3364 static void
3365 create_cus_from_index (struct objfile *objfile,
3366 const gdb_byte *cu_list, offset_type cu_list_elements,
3367 const gdb_byte *dwz_list, offset_type dwz_elements)
3368 {
3369 struct dwz_file *dwz;
3370 struct dwarf2_per_objfile *dwarf2_per_objfile
3371 = get_dwarf2_per_objfile (objfile);
3372
3373 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3374 dwarf2_per_objfile->all_comp_units =
3375 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3376 dwarf2_per_objfile->n_comp_units);
3377
3378 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3379 &dwarf2_per_objfile->info, 0, 0);
3380
3381 if (dwz_elements == 0)
3382 return;
3383
3384 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3385 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3386 cu_list_elements / 2);
3387 }
3388
3389 /* Create the signatured type hash table from the index. */
3390
3391 static void
3392 create_signatured_type_table_from_index (struct objfile *objfile,
3393 struct dwarf2_section_info *section,
3394 const gdb_byte *bytes,
3395 offset_type elements)
3396 {
3397 offset_type i;
3398 htab_t sig_types_hash;
3399 struct dwarf2_per_objfile *dwarf2_per_objfile
3400 = get_dwarf2_per_objfile (objfile);
3401
3402 dwarf2_per_objfile->n_type_units
3403 = dwarf2_per_objfile->n_allocated_type_units
3404 = elements / 3;
3405 dwarf2_per_objfile->all_type_units =
3406 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3407
3408 sig_types_hash = allocate_signatured_type_table (objfile);
3409
3410 for (i = 0; i < elements; i += 3)
3411 {
3412 struct signatured_type *sig_type;
3413 ULONGEST signature;
3414 void **slot;
3415 cu_offset type_offset_in_tu;
3416
3417 gdb_static_assert (sizeof (ULONGEST) >= 8);
3418 sect_offset sect_off
3419 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3420 type_offset_in_tu
3421 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3422 BFD_ENDIAN_LITTLE);
3423 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3424 bytes += 3 * 8;
3425
3426 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3427 struct signatured_type);
3428 sig_type->signature = signature;
3429 sig_type->type_offset_in_tu = type_offset_in_tu;
3430 sig_type->per_cu.is_debug_types = 1;
3431 sig_type->per_cu.section = section;
3432 sig_type->per_cu.sect_off = sect_off;
3433 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3434 sig_type->per_cu.v.quick
3435 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3436 struct dwarf2_per_cu_quick_data);
3437
3438 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3439 *slot = sig_type;
3440
3441 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3442 }
3443
3444 dwarf2_per_objfile->signatured_types = sig_types_hash;
3445 }
3446
3447 /* Create the signatured type hash table from .debug_names. */
3448
3449 static void
3450 create_signatured_type_table_from_debug_names
3451 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3452 const mapped_debug_names &map,
3453 struct dwarf2_section_info *section,
3454 struct dwarf2_section_info *abbrev_section)
3455 {
3456 struct objfile *objfile = dwarf2_per_objfile->objfile;
3457
3458 dwarf2_read_section (objfile, section);
3459 dwarf2_read_section (objfile, abbrev_section);
3460
3461 dwarf2_per_objfile->n_type_units
3462 = dwarf2_per_objfile->n_allocated_type_units
3463 = map.tu_count;
3464 dwarf2_per_objfile->all_type_units
3465 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3466
3467 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3468
3469 for (uint32_t i = 0; i < map.tu_count; ++i)
3470 {
3471 struct signatured_type *sig_type;
3472 ULONGEST signature;
3473 void **slot;
3474 cu_offset type_offset_in_tu;
3475
3476 sect_offset sect_off
3477 = (sect_offset) (extract_unsigned_integer
3478 (map.tu_table_reordered + i * map.offset_size,
3479 map.offset_size,
3480 map.dwarf5_byte_order));
3481
3482 comp_unit_head cu_header;
3483 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3484 abbrev_section,
3485 section->buffer + to_underlying (sect_off),
3486 rcuh_kind::TYPE);
3487
3488 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3489 struct signatured_type);
3490 sig_type->signature = cu_header.signature;
3491 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3492 sig_type->per_cu.is_debug_types = 1;
3493 sig_type->per_cu.section = section;
3494 sig_type->per_cu.sect_off = sect_off;
3495 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3496 sig_type->per_cu.v.quick
3497 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3498 struct dwarf2_per_cu_quick_data);
3499
3500 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3501 *slot = sig_type;
3502
3503 dwarf2_per_objfile->all_type_units[i] = sig_type;
3504 }
3505
3506 dwarf2_per_objfile->signatured_types = sig_types_hash;
3507 }
3508
3509 /* Read the address map data from the mapped index, and use it to
3510 populate the objfile's psymtabs_addrmap. */
3511
3512 static void
3513 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3514 struct mapped_index *index)
3515 {
3516 struct objfile *objfile = dwarf2_per_objfile->objfile;
3517 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3518 const gdb_byte *iter, *end;
3519 struct addrmap *mutable_map;
3520 CORE_ADDR baseaddr;
3521
3522 auto_obstack temp_obstack;
3523
3524 mutable_map = addrmap_create_mutable (&temp_obstack);
3525
3526 iter = index->address_table.data ();
3527 end = iter + index->address_table.size ();
3528
3529 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3530
3531 while (iter < end)
3532 {
3533 ULONGEST hi, lo, cu_index;
3534 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3535 iter += 8;
3536 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3537 iter += 8;
3538 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3539 iter += 4;
3540
3541 if (lo > hi)
3542 {
3543 complaint (&symfile_complaints,
3544 _(".gdb_index address table has invalid range (%s - %s)"),
3545 hex_string (lo), hex_string (hi));
3546 continue;
3547 }
3548
3549 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3550 {
3551 complaint (&symfile_complaints,
3552 _(".gdb_index address table has invalid CU number %u"),
3553 (unsigned) cu_index);
3554 continue;
3555 }
3556
3557 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3558 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3559 addrmap_set_empty (mutable_map, lo, hi - 1,
3560 dw2_get_cutu (dwarf2_per_objfile, cu_index));
3561 }
3562
3563 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3564 &objfile->objfile_obstack);
3565 }
3566
3567 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3568 populate the objfile's psymtabs_addrmap. */
3569
3570 static void
3571 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3572 struct dwarf2_section_info *section)
3573 {
3574 struct objfile *objfile = dwarf2_per_objfile->objfile;
3575 bfd *abfd = objfile->obfd;
3576 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3577 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3578 SECT_OFF_TEXT (objfile));
3579
3580 auto_obstack temp_obstack;
3581 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3582
3583 std::unordered_map<sect_offset,
3584 dwarf2_per_cu_data *,
3585 gdb::hash_enum<sect_offset>>
3586 debug_info_offset_to_per_cu;
3587 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3588 {
3589 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, cui);
3590 const auto insertpair
3591 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3592 if (!insertpair.second)
3593 {
3594 warning (_("Section .debug_aranges in %s has duplicate "
3595 "debug_info_offset %s, ignoring .debug_aranges."),
3596 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3597 return;
3598 }
3599 }
3600
3601 dwarf2_read_section (objfile, section);
3602
3603 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3604
3605 const gdb_byte *addr = section->buffer;
3606
3607 while (addr < section->buffer + section->size)
3608 {
3609 const gdb_byte *const entry_addr = addr;
3610 unsigned int bytes_read;
3611
3612 const LONGEST entry_length = read_initial_length (abfd, addr,
3613 &bytes_read);
3614 addr += bytes_read;
3615
3616 const gdb_byte *const entry_end = addr + entry_length;
3617 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3618 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3619 if (addr + entry_length > section->buffer + section->size)
3620 {
3621 warning (_("Section .debug_aranges in %s entry at offset %zu "
3622 "length %s exceeds section length %s, "
3623 "ignoring .debug_aranges."),
3624 objfile_name (objfile), entry_addr - section->buffer,
3625 plongest (bytes_read + entry_length),
3626 pulongest (section->size));
3627 return;
3628 }
3629
3630 /* The version number. */
3631 const uint16_t version = read_2_bytes (abfd, addr);
3632 addr += 2;
3633 if (version != 2)
3634 {
3635 warning (_("Section .debug_aranges in %s entry at offset %zu "
3636 "has unsupported version %d, ignoring .debug_aranges."),
3637 objfile_name (objfile), entry_addr - section->buffer,
3638 version);
3639 return;
3640 }
3641
3642 const uint64_t debug_info_offset
3643 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3644 addr += offset_size;
3645 const auto per_cu_it
3646 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3647 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3648 {
3649 warning (_("Section .debug_aranges in %s entry at offset %zu "
3650 "debug_info_offset %s does not exists, "
3651 "ignoring .debug_aranges."),
3652 objfile_name (objfile), entry_addr - section->buffer,
3653 pulongest (debug_info_offset));
3654 return;
3655 }
3656 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3657
3658 const uint8_t address_size = *addr++;
3659 if (address_size < 1 || address_size > 8)
3660 {
3661 warning (_("Section .debug_aranges in %s entry at offset %zu "
3662 "address_size %u is invalid, ignoring .debug_aranges."),
3663 objfile_name (objfile), entry_addr - section->buffer,
3664 address_size);
3665 return;
3666 }
3667
3668 const uint8_t segment_selector_size = *addr++;
3669 if (segment_selector_size != 0)
3670 {
3671 warning (_("Section .debug_aranges in %s entry at offset %zu "
3672 "segment_selector_size %u is not supported, "
3673 "ignoring .debug_aranges."),
3674 objfile_name (objfile), entry_addr - section->buffer,
3675 segment_selector_size);
3676 return;
3677 }
3678
3679 /* Must pad to an alignment boundary that is twice the address
3680 size. It is undocumented by the DWARF standard but GCC does
3681 use it. */
3682 for (size_t padding = ((-(addr - section->buffer))
3683 & (2 * address_size - 1));
3684 padding > 0; padding--)
3685 if (*addr++ != 0)
3686 {
3687 warning (_("Section .debug_aranges in %s entry at offset %zu "
3688 "padding is not zero, ignoring .debug_aranges."),
3689 objfile_name (objfile), entry_addr - section->buffer);
3690 return;
3691 }
3692
3693 for (;;)
3694 {
3695 if (addr + 2 * address_size > entry_end)
3696 {
3697 warning (_("Section .debug_aranges in %s entry at offset %zu "
3698 "address list is not properly terminated, "
3699 "ignoring .debug_aranges."),
3700 objfile_name (objfile), entry_addr - section->buffer);
3701 return;
3702 }
3703 ULONGEST start = extract_unsigned_integer (addr, address_size,
3704 dwarf5_byte_order);
3705 addr += address_size;
3706 ULONGEST length = extract_unsigned_integer (addr, address_size,
3707 dwarf5_byte_order);
3708 addr += address_size;
3709 if (start == 0 && length == 0)
3710 break;
3711 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3712 {
3713 /* Symbol was eliminated due to a COMDAT group. */
3714 continue;
3715 }
3716 ULONGEST end = start + length;
3717 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3718 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3719 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3720 }
3721 }
3722
3723 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3724 &objfile->objfile_obstack);
3725 }
3726
3727 /* The hash function for strings in the mapped index. This is the same as
3728 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3729 implementation. This is necessary because the hash function is tied to the
3730 format of the mapped index file. The hash values do not have to match with
3731 SYMBOL_HASH_NEXT.
3732
3733 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3734
3735 static hashval_t
3736 mapped_index_string_hash (int index_version, const void *p)
3737 {
3738 const unsigned char *str = (const unsigned char *) p;
3739 hashval_t r = 0;
3740 unsigned char c;
3741
3742 while ((c = *str++) != 0)
3743 {
3744 if (index_version >= 5)
3745 c = tolower (c);
3746 r = r * 67 + c - 113;
3747 }
3748
3749 return r;
3750 }
3751
3752 /* Find a slot in the mapped index INDEX for the object named NAME.
3753 If NAME is found, set *VEC_OUT to point to the CU vector in the
3754 constant pool and return true. If NAME cannot be found, return
3755 false. */
3756
3757 static bool
3758 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3759 offset_type **vec_out)
3760 {
3761 offset_type hash;
3762 offset_type slot, step;
3763 int (*cmp) (const char *, const char *);
3764
3765 gdb::unique_xmalloc_ptr<char> without_params;
3766 if (current_language->la_language == language_cplus
3767 || current_language->la_language == language_fortran
3768 || current_language->la_language == language_d)
3769 {
3770 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3771 not contain any. */
3772
3773 if (strchr (name, '(') != NULL)
3774 {
3775 without_params = cp_remove_params (name);
3776
3777 if (without_params != NULL)
3778 name = without_params.get ();
3779 }
3780 }
3781
3782 /* Index version 4 did not support case insensitive searches. But the
3783 indices for case insensitive languages are built in lowercase, therefore
3784 simulate our NAME being searched is also lowercased. */
3785 hash = mapped_index_string_hash ((index->version == 4
3786 && case_sensitivity == case_sensitive_off
3787 ? 5 : index->version),
3788 name);
3789
3790 slot = hash & (index->symbol_table.size () - 1);
3791 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3792 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3793
3794 for (;;)
3795 {
3796 const char *str;
3797
3798 const auto &bucket = index->symbol_table[slot];
3799 if (bucket.name == 0 && bucket.vec == 0)
3800 return false;
3801
3802 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3803 if (!cmp (name, str))
3804 {
3805 *vec_out = (offset_type *) (index->constant_pool
3806 + MAYBE_SWAP (bucket.vec));
3807 return true;
3808 }
3809
3810 slot = (slot + step) & (index->symbol_table.size () - 1);
3811 }
3812 }
3813
3814 /* A helper function that reads the .gdb_index from SECTION and fills
3815 in MAP. FILENAME is the name of the file containing the section;
3816 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3817 ok to use deprecated sections.
3818
3819 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3820 out parameters that are filled in with information about the CU and
3821 TU lists in the section.
3822
3823 Returns 1 if all went well, 0 otherwise. */
3824
3825 static int
3826 read_index_from_section (struct objfile *objfile,
3827 const char *filename,
3828 int deprecated_ok,
3829 struct dwarf2_section_info *section,
3830 struct mapped_index *map,
3831 const gdb_byte **cu_list,
3832 offset_type *cu_list_elements,
3833 const gdb_byte **types_list,
3834 offset_type *types_list_elements)
3835 {
3836 const gdb_byte *addr;
3837 offset_type version;
3838 offset_type *metadata;
3839 int i;
3840
3841 if (dwarf2_section_empty_p (section))
3842 return 0;
3843
3844 /* Older elfutils strip versions could keep the section in the main
3845 executable while splitting it for the separate debug info file. */
3846 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3847 return 0;
3848
3849 dwarf2_read_section (objfile, section);
3850
3851 addr = section->buffer;
3852 /* Version check. */
3853 version = MAYBE_SWAP (*(offset_type *) addr);
3854 /* Versions earlier than 3 emitted every copy of a psymbol. This
3855 causes the index to behave very poorly for certain requests. Version 3
3856 contained incomplete addrmap. So, it seems better to just ignore such
3857 indices. */
3858 if (version < 4)
3859 {
3860 static int warning_printed = 0;
3861 if (!warning_printed)
3862 {
3863 warning (_("Skipping obsolete .gdb_index section in %s."),
3864 filename);
3865 warning_printed = 1;
3866 }
3867 return 0;
3868 }
3869 /* Index version 4 uses a different hash function than index version
3870 5 and later.
3871
3872 Versions earlier than 6 did not emit psymbols for inlined
3873 functions. Using these files will cause GDB not to be able to
3874 set breakpoints on inlined functions by name, so we ignore these
3875 indices unless the user has done
3876 "set use-deprecated-index-sections on". */
3877 if (version < 6 && !deprecated_ok)
3878 {
3879 static int warning_printed = 0;
3880 if (!warning_printed)
3881 {
3882 warning (_("\
3883 Skipping deprecated .gdb_index section in %s.\n\
3884 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3885 to use the section anyway."),
3886 filename);
3887 warning_printed = 1;
3888 }
3889 return 0;
3890 }
3891 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3892 of the TU (for symbols coming from TUs),
3893 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3894 Plus gold-generated indices can have duplicate entries for global symbols,
3895 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3896 These are just performance bugs, and we can't distinguish gdb-generated
3897 indices from gold-generated ones, so issue no warning here. */
3898
3899 /* Indexes with higher version than the one supported by GDB may be no
3900 longer backward compatible. */
3901 if (version > 8)
3902 return 0;
3903
3904 map->version = version;
3905 map->total_size = section->size;
3906
3907 metadata = (offset_type *) (addr + sizeof (offset_type));
3908
3909 i = 0;
3910 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3911 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3912 / 8);
3913 ++i;
3914
3915 *types_list = addr + MAYBE_SWAP (metadata[i]);
3916 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3917 - MAYBE_SWAP (metadata[i]))
3918 / 8);
3919 ++i;
3920
3921 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3922 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3923 map->address_table
3924 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3925 ++i;
3926
3927 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3928 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3929 map->symbol_table
3930 = gdb::array_view<mapped_index::symbol_table_slot>
3931 ((mapped_index::symbol_table_slot *) symbol_table,
3932 (mapped_index::symbol_table_slot *) symbol_table_end);
3933
3934 ++i;
3935 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3936
3937 return 1;
3938 }
3939
3940 /* Read .gdb_index. If everything went ok, initialize the "quick"
3941 elements of all the CUs and return 1. Otherwise, return 0. */
3942
3943 static int
3944 dwarf2_read_index (struct objfile *objfile)
3945 {
3946 struct mapped_index local_map, *map;
3947 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3948 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3949 struct dwz_file *dwz;
3950 struct dwarf2_per_objfile *dwarf2_per_objfile
3951 = get_dwarf2_per_objfile (objfile);
3952
3953 if (!read_index_from_section (objfile, objfile_name (objfile),
3954 use_deprecated_index_sections,
3955 &dwarf2_per_objfile->gdb_index, &local_map,
3956 &cu_list, &cu_list_elements,
3957 &types_list, &types_list_elements))
3958 return 0;
3959
3960 /* Don't use the index if it's empty. */
3961 if (local_map.symbol_table.empty ())
3962 return 0;
3963
3964 /* If there is a .dwz file, read it so we can get its CU list as
3965 well. */
3966 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3967 if (dwz != NULL)
3968 {
3969 struct mapped_index dwz_map;
3970 const gdb_byte *dwz_types_ignore;
3971 offset_type dwz_types_elements_ignore;
3972
3973 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3974 1,
3975 &dwz->gdb_index, &dwz_map,
3976 &dwz_list, &dwz_list_elements,
3977 &dwz_types_ignore,
3978 &dwz_types_elements_ignore))
3979 {
3980 warning (_("could not read '.gdb_index' section from %s; skipping"),
3981 bfd_get_filename (dwz->dwz_bfd));
3982 return 0;
3983 }
3984 }
3985
3986 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3987 dwz_list_elements);
3988
3989 if (types_list_elements)
3990 {
3991 struct dwarf2_section_info *section;
3992
3993 /* We can only handle a single .debug_types when we have an
3994 index. */
3995 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3996 return 0;
3997
3998 section = VEC_index (dwarf2_section_info_def,
3999 dwarf2_per_objfile->types, 0);
4000
4001 create_signatured_type_table_from_index (objfile, section, types_list,
4002 types_list_elements);
4003 }
4004
4005 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
4006
4007 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
4008 map = new (map) mapped_index ();
4009 *map = local_map;
4010
4011 dwarf2_per_objfile->index_table = map;
4012 dwarf2_per_objfile->using_index = 1;
4013 dwarf2_per_objfile->quick_file_names_table =
4014 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
4015
4016 return 1;
4017 }
4018
4019 /* die_reader_func for dw2_get_file_names. */
4020
4021 static void
4022 dw2_get_file_names_reader (const struct die_reader_specs *reader,
4023 const gdb_byte *info_ptr,
4024 struct die_info *comp_unit_die,
4025 int has_children,
4026 void *data)
4027 {
4028 struct dwarf2_cu *cu = reader->cu;
4029 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
4030 struct dwarf2_per_objfile *dwarf2_per_objfile
4031 = cu->per_cu->dwarf2_per_objfile;
4032 struct objfile *objfile = dwarf2_per_objfile->objfile;
4033 struct dwarf2_per_cu_data *lh_cu;
4034 struct attribute *attr;
4035 int i;
4036 void **slot;
4037 struct quick_file_names *qfn;
4038
4039 gdb_assert (! this_cu->is_debug_types);
4040
4041 /* Our callers never want to match partial units -- instead they
4042 will match the enclosing full CU. */
4043 if (comp_unit_die->tag == DW_TAG_partial_unit)
4044 {
4045 this_cu->v.quick->no_file_data = 1;
4046 return;
4047 }
4048
4049 lh_cu = this_cu;
4050 slot = NULL;
4051
4052 line_header_up lh;
4053 sect_offset line_offset {};
4054
4055 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4056 if (attr)
4057 {
4058 struct quick_file_names find_entry;
4059
4060 line_offset = (sect_offset) DW_UNSND (attr);
4061
4062 /* We may have already read in this line header (TU line header sharing).
4063 If we have we're done. */
4064 find_entry.hash.dwo_unit = cu->dwo_unit;
4065 find_entry.hash.line_sect_off = line_offset;
4066 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
4067 &find_entry, INSERT);
4068 if (*slot != NULL)
4069 {
4070 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
4071 return;
4072 }
4073
4074 lh = dwarf_decode_line_header (line_offset, cu);
4075 }
4076 if (lh == NULL)
4077 {
4078 lh_cu->v.quick->no_file_data = 1;
4079 return;
4080 }
4081
4082 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
4083 qfn->hash.dwo_unit = cu->dwo_unit;
4084 qfn->hash.line_sect_off = line_offset;
4085 gdb_assert (slot != NULL);
4086 *slot = qfn;
4087
4088 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
4089
4090 qfn->num_file_names = lh->file_names.size ();
4091 qfn->file_names =
4092 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
4093 for (i = 0; i < lh->file_names.size (); ++i)
4094 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
4095 qfn->real_names = NULL;
4096
4097 lh_cu->v.quick->file_names = qfn;
4098 }
4099
4100 /* A helper for the "quick" functions which attempts to read the line
4101 table for THIS_CU. */
4102
4103 static struct quick_file_names *
4104 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
4105 {
4106 /* This should never be called for TUs. */
4107 gdb_assert (! this_cu->is_debug_types);
4108 /* Nor type unit groups. */
4109 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
4110
4111 if (this_cu->v.quick->file_names != NULL)
4112 return this_cu->v.quick->file_names;
4113 /* If we know there is no line data, no point in looking again. */
4114 if (this_cu->v.quick->no_file_data)
4115 return NULL;
4116
4117 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
4118
4119 if (this_cu->v.quick->no_file_data)
4120 return NULL;
4121 return this_cu->v.quick->file_names;
4122 }
4123
4124 /* A helper for the "quick" functions which computes and caches the
4125 real path for a given file name from the line table. */
4126
4127 static const char *
4128 dw2_get_real_path (struct objfile *objfile,
4129 struct quick_file_names *qfn, int index)
4130 {
4131 if (qfn->real_names == NULL)
4132 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
4133 qfn->num_file_names, const char *);
4134
4135 if (qfn->real_names[index] == NULL)
4136 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
4137
4138 return qfn->real_names[index];
4139 }
4140
4141 static struct symtab *
4142 dw2_find_last_source_symtab (struct objfile *objfile)
4143 {
4144 struct dwarf2_per_objfile *dwarf2_per_objfile
4145 = get_dwarf2_per_objfile (objfile);
4146 int index = dwarf2_per_objfile->n_comp_units - 1;
4147 dwarf2_per_cu_data *dwarf_cu = dw2_get_cutu (dwarf2_per_objfile, index);
4148 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
4149
4150 if (cust == NULL)
4151 return NULL;
4152
4153 return compunit_primary_filetab (cust);
4154 }
4155
4156 /* Traversal function for dw2_forget_cached_source_info. */
4157
4158 static int
4159 dw2_free_cached_file_names (void **slot, void *info)
4160 {
4161 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
4162
4163 if (file_data->real_names)
4164 {
4165 int i;
4166
4167 for (i = 0; i < file_data->num_file_names; ++i)
4168 {
4169 xfree ((void*) file_data->real_names[i]);
4170 file_data->real_names[i] = NULL;
4171 }
4172 }
4173
4174 return 1;
4175 }
4176
4177 static void
4178 dw2_forget_cached_source_info (struct objfile *objfile)
4179 {
4180 struct dwarf2_per_objfile *dwarf2_per_objfile
4181 = get_dwarf2_per_objfile (objfile);
4182
4183 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
4184 dw2_free_cached_file_names, NULL);
4185 }
4186
4187 /* Helper function for dw2_map_symtabs_matching_filename that expands
4188 the symtabs and calls the iterator. */
4189
4190 static int
4191 dw2_map_expand_apply (struct objfile *objfile,
4192 struct dwarf2_per_cu_data *per_cu,
4193 const char *name, const char *real_path,
4194 gdb::function_view<bool (symtab *)> callback)
4195 {
4196 struct compunit_symtab *last_made = objfile->compunit_symtabs;
4197
4198 /* Don't visit already-expanded CUs. */
4199 if (per_cu->v.quick->compunit_symtab)
4200 return 0;
4201
4202 /* This may expand more than one symtab, and we want to iterate over
4203 all of them. */
4204 dw2_instantiate_symtab (per_cu);
4205
4206 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4207 last_made, callback);
4208 }
4209
4210 /* Implementation of the map_symtabs_matching_filename method. */
4211
4212 static bool
4213 dw2_map_symtabs_matching_filename
4214 (struct objfile *objfile, const char *name, const char *real_path,
4215 gdb::function_view<bool (symtab *)> callback)
4216 {
4217 int i;
4218 const char *name_basename = lbasename (name);
4219 struct dwarf2_per_objfile *dwarf2_per_objfile
4220 = get_dwarf2_per_objfile (objfile);
4221
4222 /* The rule is CUs specify all the files, including those used by
4223 any TU, so there's no need to scan TUs here. */
4224
4225 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4226 {
4227 int j;
4228 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
4229 struct quick_file_names *file_data;
4230
4231 /* We only need to look at symtabs not already expanded. */
4232 if (per_cu->v.quick->compunit_symtab)
4233 continue;
4234
4235 file_data = dw2_get_file_names (per_cu);
4236 if (file_data == NULL)
4237 continue;
4238
4239 for (j = 0; j < file_data->num_file_names; ++j)
4240 {
4241 const char *this_name = file_data->file_names[j];
4242 const char *this_real_name;
4243
4244 if (compare_filenames_for_search (this_name, name))
4245 {
4246 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4247 callback))
4248 return true;
4249 continue;
4250 }
4251
4252 /* Before we invoke realpath, which can get expensive when many
4253 files are involved, do a quick comparison of the basenames. */
4254 if (! basenames_may_differ
4255 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4256 continue;
4257
4258 this_real_name = dw2_get_real_path (objfile, file_data, j);
4259 if (compare_filenames_for_search (this_real_name, name))
4260 {
4261 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4262 callback))
4263 return true;
4264 continue;
4265 }
4266
4267 if (real_path != NULL)
4268 {
4269 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4270 gdb_assert (IS_ABSOLUTE_PATH (name));
4271 if (this_real_name != NULL
4272 && FILENAME_CMP (real_path, this_real_name) == 0)
4273 {
4274 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4275 callback))
4276 return true;
4277 continue;
4278 }
4279 }
4280 }
4281 }
4282
4283 return false;
4284 }
4285
4286 /* Struct used to manage iterating over all CUs looking for a symbol. */
4287
4288 struct dw2_symtab_iterator
4289 {
4290 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
4291 struct dwarf2_per_objfile *dwarf2_per_objfile;
4292 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4293 int want_specific_block;
4294 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4295 Unused if !WANT_SPECIFIC_BLOCK. */
4296 int block_index;
4297 /* The kind of symbol we're looking for. */
4298 domain_enum domain;
4299 /* The list of CUs from the index entry of the symbol,
4300 or NULL if not found. */
4301 offset_type *vec;
4302 /* The next element in VEC to look at. */
4303 int next;
4304 /* The number of elements in VEC, or zero if there is no match. */
4305 int length;
4306 /* Have we seen a global version of the symbol?
4307 If so we can ignore all further global instances.
4308 This is to work around gold/15646, inefficient gold-generated
4309 indices. */
4310 int global_seen;
4311 };
4312
4313 /* Initialize the index symtab iterator ITER.
4314 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4315 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
4316
4317 static void
4318 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
4319 struct dwarf2_per_objfile *dwarf2_per_objfile,
4320 int want_specific_block,
4321 int block_index,
4322 domain_enum domain,
4323 const char *name)
4324 {
4325 iter->dwarf2_per_objfile = dwarf2_per_objfile;
4326 iter->want_specific_block = want_specific_block;
4327 iter->block_index = block_index;
4328 iter->domain = domain;
4329 iter->next = 0;
4330 iter->global_seen = 0;
4331
4332 mapped_index *index = dwarf2_per_objfile->index_table;
4333
4334 /* index is NULL if OBJF_READNOW. */
4335 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
4336 iter->length = MAYBE_SWAP (*iter->vec);
4337 else
4338 {
4339 iter->vec = NULL;
4340 iter->length = 0;
4341 }
4342 }
4343
4344 /* Return the next matching CU or NULL if there are no more. */
4345
4346 static struct dwarf2_per_cu_data *
4347 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4348 {
4349 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4350
4351 for ( ; iter->next < iter->length; ++iter->next)
4352 {
4353 offset_type cu_index_and_attrs =
4354 MAYBE_SWAP (iter->vec[iter->next + 1]);
4355 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4356 struct dwarf2_per_cu_data *per_cu;
4357 int want_static = iter->block_index != GLOBAL_BLOCK;
4358 /* This value is only valid for index versions >= 7. */
4359 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4360 gdb_index_symbol_kind symbol_kind =
4361 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4362 /* Only check the symbol attributes if they're present.
4363 Indices prior to version 7 don't record them,
4364 and indices >= 7 may elide them for certain symbols
4365 (gold does this). */
4366 int attrs_valid =
4367 (dwarf2_per_objfile->index_table->version >= 7
4368 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4369
4370 /* Don't crash on bad data. */
4371 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4372 + dwarf2_per_objfile->n_type_units))
4373 {
4374 complaint (&symfile_complaints,
4375 _(".gdb_index entry has bad CU index"
4376 " [in module %s]"),
4377 objfile_name (dwarf2_per_objfile->objfile));
4378 continue;
4379 }
4380
4381 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
4382
4383 /* Skip if already read in. */
4384 if (per_cu->v.quick->compunit_symtab)
4385 continue;
4386
4387 /* Check static vs global. */
4388 if (attrs_valid)
4389 {
4390 if (iter->want_specific_block
4391 && want_static != is_static)
4392 continue;
4393 /* Work around gold/15646. */
4394 if (!is_static && iter->global_seen)
4395 continue;
4396 if (!is_static)
4397 iter->global_seen = 1;
4398 }
4399
4400 /* Only check the symbol's kind if it has one. */
4401 if (attrs_valid)
4402 {
4403 switch (iter->domain)
4404 {
4405 case VAR_DOMAIN:
4406 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4407 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4408 /* Some types are also in VAR_DOMAIN. */
4409 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4410 continue;
4411 break;
4412 case STRUCT_DOMAIN:
4413 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4414 continue;
4415 break;
4416 case LABEL_DOMAIN:
4417 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4418 continue;
4419 break;
4420 default:
4421 break;
4422 }
4423 }
4424
4425 ++iter->next;
4426 return per_cu;
4427 }
4428
4429 return NULL;
4430 }
4431
4432 static struct compunit_symtab *
4433 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4434 const char *name, domain_enum domain)
4435 {
4436 struct compunit_symtab *stab_best = NULL;
4437 struct dwarf2_per_objfile *dwarf2_per_objfile
4438 = get_dwarf2_per_objfile (objfile);
4439
4440 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4441
4442 struct dw2_symtab_iterator iter;
4443 struct dwarf2_per_cu_data *per_cu;
4444
4445 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4446
4447 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4448 {
4449 struct symbol *sym, *with_opaque = NULL;
4450 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4451 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4452 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4453
4454 sym = block_find_symbol (block, name, domain,
4455 block_find_non_opaque_type_preferred,
4456 &with_opaque);
4457
4458 /* Some caution must be observed with overloaded functions
4459 and methods, since the index will not contain any overload
4460 information (but NAME might contain it). */
4461
4462 if (sym != NULL
4463 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4464 return stab;
4465 if (with_opaque != NULL
4466 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4467 stab_best = stab;
4468
4469 /* Keep looking through other CUs. */
4470 }
4471
4472 return stab_best;
4473 }
4474
4475 static void
4476 dw2_print_stats (struct objfile *objfile)
4477 {
4478 struct dwarf2_per_objfile *dwarf2_per_objfile
4479 = get_dwarf2_per_objfile (objfile);
4480 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4481 int count = 0;
4482
4483 for (int i = 0; i < total; ++i)
4484 {
4485 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4486
4487 if (!per_cu->v.quick->compunit_symtab)
4488 ++count;
4489 }
4490 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4491 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4492 }
4493
4494 /* This dumps minimal information about the index.
4495 It is called via "mt print objfiles".
4496 One use is to verify .gdb_index has been loaded by the
4497 gdb.dwarf2/gdb-index.exp testcase. */
4498
4499 static void
4500 dw2_dump (struct objfile *objfile)
4501 {
4502 struct dwarf2_per_objfile *dwarf2_per_objfile
4503 = get_dwarf2_per_objfile (objfile);
4504
4505 gdb_assert (dwarf2_per_objfile->using_index);
4506 printf_filtered (".gdb_index:");
4507 if (dwarf2_per_objfile->index_table != NULL)
4508 {
4509 printf_filtered (" version %d\n",
4510 dwarf2_per_objfile->index_table->version);
4511 }
4512 else
4513 printf_filtered (" faked for \"readnow\"\n");
4514 printf_filtered ("\n");
4515 }
4516
4517 static void
4518 dw2_relocate (struct objfile *objfile,
4519 const struct section_offsets *new_offsets,
4520 const struct section_offsets *delta)
4521 {
4522 /* There's nothing to relocate here. */
4523 }
4524
4525 static void
4526 dw2_expand_symtabs_for_function (struct objfile *objfile,
4527 const char *func_name)
4528 {
4529 struct dwarf2_per_objfile *dwarf2_per_objfile
4530 = get_dwarf2_per_objfile (objfile);
4531
4532 struct dw2_symtab_iterator iter;
4533 struct dwarf2_per_cu_data *per_cu;
4534
4535 /* Note: It doesn't matter what we pass for block_index here. */
4536 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4537 func_name);
4538
4539 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4540 dw2_instantiate_symtab (per_cu);
4541
4542 }
4543
4544 static void
4545 dw2_expand_all_symtabs (struct objfile *objfile)
4546 {
4547 struct dwarf2_per_objfile *dwarf2_per_objfile
4548 = get_dwarf2_per_objfile (objfile);
4549 int total_units = (dwarf2_per_objfile->n_comp_units
4550 + dwarf2_per_objfile->n_type_units);
4551
4552 for (int i = 0; i < total_units; ++i)
4553 {
4554 struct dwarf2_per_cu_data *per_cu
4555 = dw2_get_cutu (dwarf2_per_objfile, i);
4556
4557 dw2_instantiate_symtab (per_cu);
4558 }
4559 }
4560
4561 static void
4562 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4563 const char *fullname)
4564 {
4565 struct dwarf2_per_objfile *dwarf2_per_objfile
4566 = get_dwarf2_per_objfile (objfile);
4567
4568 /* We don't need to consider type units here.
4569 This is only called for examining code, e.g. expand_line_sal.
4570 There can be an order of magnitude (or more) more type units
4571 than comp units, and we avoid them if we can. */
4572
4573 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4574 {
4575 int j;
4576 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4577 struct quick_file_names *file_data;
4578
4579 /* We only need to look at symtabs not already expanded. */
4580 if (per_cu->v.quick->compunit_symtab)
4581 continue;
4582
4583 file_data = dw2_get_file_names (per_cu);
4584 if (file_data == NULL)
4585 continue;
4586
4587 for (j = 0; j < file_data->num_file_names; ++j)
4588 {
4589 const char *this_fullname = file_data->file_names[j];
4590
4591 if (filename_cmp (this_fullname, fullname) == 0)
4592 {
4593 dw2_instantiate_symtab (per_cu);
4594 break;
4595 }
4596 }
4597 }
4598 }
4599
4600 static void
4601 dw2_map_matching_symbols (struct objfile *objfile,
4602 const char * name, domain_enum domain,
4603 int global,
4604 int (*callback) (struct block *,
4605 struct symbol *, void *),
4606 void *data, symbol_name_match_type match,
4607 symbol_compare_ftype *ordered_compare)
4608 {
4609 /* Currently unimplemented; used for Ada. The function can be called if the
4610 current language is Ada for a non-Ada objfile using GNU index. As Ada
4611 does not look for non-Ada symbols this function should just return. */
4612 }
4613
4614 /* Symbol name matcher for .gdb_index names.
4615
4616 Symbol names in .gdb_index have a few particularities:
4617
4618 - There's no indication of which is the language of each symbol.
4619
4620 Since each language has its own symbol name matching algorithm,
4621 and we don't know which language is the right one, we must match
4622 each symbol against all languages. This would be a potential
4623 performance problem if it were not mitigated by the
4624 mapped_index::name_components lookup table, which significantly
4625 reduces the number of times we need to call into this matcher,
4626 making it a non-issue.
4627
4628 - Symbol names in the index have no overload (parameter)
4629 information. I.e., in C++, "foo(int)" and "foo(long)" both
4630 appear as "foo" in the index, for example.
4631
4632 This means that the lookup names passed to the symbol name
4633 matcher functions must have no parameter information either
4634 because (e.g.) symbol search name "foo" does not match
4635 lookup-name "foo(int)" [while swapping search name for lookup
4636 name would match].
4637 */
4638 class gdb_index_symbol_name_matcher
4639 {
4640 public:
4641 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4642 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4643
4644 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4645 Returns true if any matcher matches. */
4646 bool matches (const char *symbol_name);
4647
4648 private:
4649 /* A reference to the lookup name we're matching against. */
4650 const lookup_name_info &m_lookup_name;
4651
4652 /* A vector holding all the different symbol name matchers, for all
4653 languages. */
4654 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4655 };
4656
4657 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4658 (const lookup_name_info &lookup_name)
4659 : m_lookup_name (lookup_name)
4660 {
4661 /* Prepare the vector of comparison functions upfront, to avoid
4662 doing the same work for each symbol. Care is taken to avoid
4663 matching with the same matcher more than once if/when multiple
4664 languages use the same matcher function. */
4665 auto &matchers = m_symbol_name_matcher_funcs;
4666 matchers.reserve (nr_languages);
4667
4668 matchers.push_back (default_symbol_name_matcher);
4669
4670 for (int i = 0; i < nr_languages; i++)
4671 {
4672 const language_defn *lang = language_def ((enum language) i);
4673 symbol_name_matcher_ftype *name_matcher
4674 = get_symbol_name_matcher (lang, m_lookup_name);
4675
4676 /* Don't insert the same comparison routine more than once.
4677 Note that we do this linear walk instead of a seemingly
4678 cheaper sorted insert, or use a std::set or something like
4679 that, because relative order of function addresses is not
4680 stable. This is not a problem in practice because the number
4681 of supported languages is low, and the cost here is tiny
4682 compared to the number of searches we'll do afterwards using
4683 this object. */
4684 if (name_matcher != default_symbol_name_matcher
4685 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4686 == matchers.end ()))
4687 matchers.push_back (name_matcher);
4688 }
4689 }
4690
4691 bool
4692 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4693 {
4694 for (auto matches_name : m_symbol_name_matcher_funcs)
4695 if (matches_name (symbol_name, m_lookup_name, NULL))
4696 return true;
4697
4698 return false;
4699 }
4700
4701 /* Starting from a search name, return the string that finds the upper
4702 bound of all strings that start with SEARCH_NAME in a sorted name
4703 list. Returns the empty string to indicate that the upper bound is
4704 the end of the list. */
4705
4706 static std::string
4707 make_sort_after_prefix_name (const char *search_name)
4708 {
4709 /* When looking to complete "func", we find the upper bound of all
4710 symbols that start with "func" by looking for where we'd insert
4711 the closest string that would follow "func" in lexicographical
4712 order. Usually, that's "func"-with-last-character-incremented,
4713 i.e. "fund". Mind non-ASCII characters, though. Usually those
4714 will be UTF-8 multi-byte sequences, but we can't be certain.
4715 Especially mind the 0xff character, which is a valid character in
4716 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4717 rule out compilers allowing it in identifiers. Note that
4718 conveniently, strcmp/strcasecmp are specified to compare
4719 characters interpreted as unsigned char. So what we do is treat
4720 the whole string as a base 256 number composed of a sequence of
4721 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4722 to 0, and carries 1 to the following more-significant position.
4723 If the very first character in SEARCH_NAME ends up incremented
4724 and carries/overflows, then the upper bound is the end of the
4725 list. The string after the empty string is also the empty
4726 string.
4727
4728 Some examples of this operation:
4729
4730 SEARCH_NAME => "+1" RESULT
4731
4732 "abc" => "abd"
4733 "ab\xff" => "ac"
4734 "\xff" "a" "\xff" => "\xff" "b"
4735 "\xff" => ""
4736 "\xff\xff" => ""
4737 "" => ""
4738
4739 Then, with these symbols for example:
4740
4741 func
4742 func1
4743 fund
4744
4745 completing "func" looks for symbols between "func" and
4746 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4747 which finds "func" and "func1", but not "fund".
4748
4749 And with:
4750
4751 funcÿ (Latin1 'ÿ' [0xff])
4752 funcÿ1
4753 fund
4754
4755 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4756 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4757
4758 And with:
4759
4760 ÿÿ (Latin1 'ÿ' [0xff])
4761 ÿÿ1
4762
4763 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4764 the end of the list.
4765 */
4766 std::string after = search_name;
4767 while (!after.empty () && (unsigned char) after.back () == 0xff)
4768 after.pop_back ();
4769 if (!after.empty ())
4770 after.back () = (unsigned char) after.back () + 1;
4771 return after;
4772 }
4773
4774 /* See declaration. */
4775
4776 std::pair<std::vector<name_component>::const_iterator,
4777 std::vector<name_component>::const_iterator>
4778 mapped_index_base::find_name_components_bounds
4779 (const lookup_name_info &lookup_name_without_params) const
4780 {
4781 auto *name_cmp
4782 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4783
4784 const char *cplus
4785 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4786
4787 /* Comparison function object for lower_bound that matches against a
4788 given symbol name. */
4789 auto lookup_compare_lower = [&] (const name_component &elem,
4790 const char *name)
4791 {
4792 const char *elem_qualified = this->symbol_name_at (elem.idx);
4793 const char *elem_name = elem_qualified + elem.name_offset;
4794 return name_cmp (elem_name, name) < 0;
4795 };
4796
4797 /* Comparison function object for upper_bound that matches against a
4798 given symbol name. */
4799 auto lookup_compare_upper = [&] (const char *name,
4800 const name_component &elem)
4801 {
4802 const char *elem_qualified = this->symbol_name_at (elem.idx);
4803 const char *elem_name = elem_qualified + elem.name_offset;
4804 return name_cmp (name, elem_name) < 0;
4805 };
4806
4807 auto begin = this->name_components.begin ();
4808 auto end = this->name_components.end ();
4809
4810 /* Find the lower bound. */
4811 auto lower = [&] ()
4812 {
4813 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4814 return begin;
4815 else
4816 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4817 } ();
4818
4819 /* Find the upper bound. */
4820 auto upper = [&] ()
4821 {
4822 if (lookup_name_without_params.completion_mode ())
4823 {
4824 /* In completion mode, we want UPPER to point past all
4825 symbols names that have the same prefix. I.e., with
4826 these symbols, and completing "func":
4827
4828 function << lower bound
4829 function1
4830 other_function << upper bound
4831
4832 We find the upper bound by looking for the insertion
4833 point of "func"-with-last-character-incremented,
4834 i.e. "fund". */
4835 std::string after = make_sort_after_prefix_name (cplus);
4836 if (after.empty ())
4837 return end;
4838 return std::lower_bound (lower, end, after.c_str (),
4839 lookup_compare_lower);
4840 }
4841 else
4842 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4843 } ();
4844
4845 return {lower, upper};
4846 }
4847
4848 /* See declaration. */
4849
4850 void
4851 mapped_index_base::build_name_components ()
4852 {
4853 if (!this->name_components.empty ())
4854 return;
4855
4856 this->name_components_casing = case_sensitivity;
4857 auto *name_cmp
4858 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4859
4860 /* The code below only knows how to break apart components of C++
4861 symbol names (and other languages that use '::' as
4862 namespace/module separator). If we add support for wild matching
4863 to some language that uses some other operator (E.g., Ada, Go and
4864 D use '.'), then we'll need to try splitting the symbol name
4865 according to that language too. Note that Ada does support wild
4866 matching, but doesn't currently support .gdb_index. */
4867 auto count = this->symbol_name_count ();
4868 for (offset_type idx = 0; idx < count; idx++)
4869 {
4870 if (this->symbol_name_slot_invalid (idx))
4871 continue;
4872
4873 const char *name = this->symbol_name_at (idx);
4874
4875 /* Add each name component to the name component table. */
4876 unsigned int previous_len = 0;
4877 for (unsigned int current_len = cp_find_first_component (name);
4878 name[current_len] != '\0';
4879 current_len += cp_find_first_component (name + current_len))
4880 {
4881 gdb_assert (name[current_len] == ':');
4882 this->name_components.push_back ({previous_len, idx});
4883 /* Skip the '::'. */
4884 current_len += 2;
4885 previous_len = current_len;
4886 }
4887 this->name_components.push_back ({previous_len, idx});
4888 }
4889
4890 /* Sort name_components elements by name. */
4891 auto name_comp_compare = [&] (const name_component &left,
4892 const name_component &right)
4893 {
4894 const char *left_qualified = this->symbol_name_at (left.idx);
4895 const char *right_qualified = this->symbol_name_at (right.idx);
4896
4897 const char *left_name = left_qualified + left.name_offset;
4898 const char *right_name = right_qualified + right.name_offset;
4899
4900 return name_cmp (left_name, right_name) < 0;
4901 };
4902
4903 std::sort (this->name_components.begin (),
4904 this->name_components.end (),
4905 name_comp_compare);
4906 }
4907
4908 /* Helper for dw2_expand_symtabs_matching that works with a
4909 mapped_index_base instead of the containing objfile. This is split
4910 to a separate function in order to be able to unit test the
4911 name_components matching using a mock mapped_index_base. For each
4912 symbol name that matches, calls MATCH_CALLBACK, passing it the
4913 symbol's index in the mapped_index_base symbol table. */
4914
4915 static void
4916 dw2_expand_symtabs_matching_symbol
4917 (mapped_index_base &index,
4918 const lookup_name_info &lookup_name_in,
4919 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4920 enum search_domain kind,
4921 gdb::function_view<void (offset_type)> match_callback)
4922 {
4923 lookup_name_info lookup_name_without_params
4924 = lookup_name_in.make_ignore_params ();
4925 gdb_index_symbol_name_matcher lookup_name_matcher
4926 (lookup_name_without_params);
4927
4928 /* Build the symbol name component sorted vector, if we haven't
4929 yet. */
4930 index.build_name_components ();
4931
4932 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4933
4934 /* Now for each symbol name in range, check to see if we have a name
4935 match, and if so, call the MATCH_CALLBACK callback. */
4936
4937 /* The same symbol may appear more than once in the range though.
4938 E.g., if we're looking for symbols that complete "w", and we have
4939 a symbol named "w1::w2", we'll find the two name components for
4940 that same symbol in the range. To be sure we only call the
4941 callback once per symbol, we first collect the symbol name
4942 indexes that matched in a temporary vector and ignore
4943 duplicates. */
4944 std::vector<offset_type> matches;
4945 matches.reserve (std::distance (bounds.first, bounds.second));
4946
4947 for (; bounds.first != bounds.second; ++bounds.first)
4948 {
4949 const char *qualified = index.symbol_name_at (bounds.first->idx);
4950
4951 if (!lookup_name_matcher.matches (qualified)
4952 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4953 continue;
4954
4955 matches.push_back (bounds.first->idx);
4956 }
4957
4958 std::sort (matches.begin (), matches.end ());
4959
4960 /* Finally call the callback, once per match. */
4961 ULONGEST prev = -1;
4962 for (offset_type idx : matches)
4963 {
4964 if (prev != idx)
4965 {
4966 match_callback (idx);
4967 prev = idx;
4968 }
4969 }
4970
4971 /* Above we use a type wider than idx's for 'prev', since 0 and
4972 (offset_type)-1 are both possible values. */
4973 static_assert (sizeof (prev) > sizeof (offset_type), "");
4974 }
4975
4976 #if GDB_SELF_TEST
4977
4978 namespace selftests { namespace dw2_expand_symtabs_matching {
4979
4980 /* A mock .gdb_index/.debug_names-like name index table, enough to
4981 exercise dw2_expand_symtabs_matching_symbol, which works with the
4982 mapped_index_base interface. Builds an index from the symbol list
4983 passed as parameter to the constructor. */
4984 class mock_mapped_index : public mapped_index_base
4985 {
4986 public:
4987 mock_mapped_index (gdb::array_view<const char *> symbols)
4988 : m_symbol_table (symbols)
4989 {}
4990
4991 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4992
4993 /* Return the number of names in the symbol table. */
4994 virtual size_t symbol_name_count () const
4995 {
4996 return m_symbol_table.size ();
4997 }
4998
4999 /* Get the name of the symbol at IDX in the symbol table. */
5000 virtual const char *symbol_name_at (offset_type idx) const
5001 {
5002 return m_symbol_table[idx];
5003 }
5004
5005 private:
5006 gdb::array_view<const char *> m_symbol_table;
5007 };
5008
5009 /* Convenience function that converts a NULL pointer to a "<null>"
5010 string, to pass to print routines. */
5011
5012 static const char *
5013 string_or_null (const char *str)
5014 {
5015 return str != NULL ? str : "<null>";
5016 }
5017
5018 /* Check if a lookup_name_info built from
5019 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
5020 index. EXPECTED_LIST is the list of expected matches, in expected
5021 matching order. If no match expected, then an empty list is
5022 specified. Returns true on success. On failure prints a warning
5023 indicating the file:line that failed, and returns false. */
5024
5025 static bool
5026 check_match (const char *file, int line,
5027 mock_mapped_index &mock_index,
5028 const char *name, symbol_name_match_type match_type,
5029 bool completion_mode,
5030 std::initializer_list<const char *> expected_list)
5031 {
5032 lookup_name_info lookup_name (name, match_type, completion_mode);
5033
5034 bool matched = true;
5035
5036 auto mismatch = [&] (const char *expected_str,
5037 const char *got)
5038 {
5039 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
5040 "expected=\"%s\", got=\"%s\"\n"),
5041 file, line,
5042 (match_type == symbol_name_match_type::FULL
5043 ? "FULL" : "WILD"),
5044 name, string_or_null (expected_str), string_or_null (got));
5045 matched = false;
5046 };
5047
5048 auto expected_it = expected_list.begin ();
5049 auto expected_end = expected_list.end ();
5050
5051 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
5052 NULL, ALL_DOMAIN,
5053 [&] (offset_type idx)
5054 {
5055 const char *matched_name = mock_index.symbol_name_at (idx);
5056 const char *expected_str
5057 = expected_it == expected_end ? NULL : *expected_it++;
5058
5059 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
5060 mismatch (expected_str, matched_name);
5061 });
5062
5063 const char *expected_str
5064 = expected_it == expected_end ? NULL : *expected_it++;
5065 if (expected_str != NULL)
5066 mismatch (expected_str, NULL);
5067
5068 return matched;
5069 }
5070
5071 /* The symbols added to the mock mapped_index for testing (in
5072 canonical form). */
5073 static const char *test_symbols[] = {
5074 "function",
5075 "std::bar",
5076 "std::zfunction",
5077 "std::zfunction2",
5078 "w1::w2",
5079 "ns::foo<char*>",
5080 "ns::foo<int>",
5081 "ns::foo<long>",
5082 "ns2::tmpl<int>::foo2",
5083 "(anonymous namespace)::A::B::C",
5084
5085 /* These are used to check that the increment-last-char in the
5086 matching algorithm for completion doesn't match "t1_fund" when
5087 completing "t1_func". */
5088 "t1_func",
5089 "t1_func1",
5090 "t1_fund",
5091 "t1_fund1",
5092
5093 /* A UTF-8 name with multi-byte sequences to make sure that
5094 cp-name-parser understands this as a single identifier ("função"
5095 is "function" in PT). */
5096 u8"u8função",
5097
5098 /* \377 (0xff) is Latin1 'ÿ'. */
5099 "yfunc\377",
5100
5101 /* \377 (0xff) is Latin1 'ÿ'. */
5102 "\377",
5103 "\377\377123",
5104
5105 /* A name with all sorts of complications. Starts with "z" to make
5106 it easier for the completion tests below. */
5107 #define Z_SYM_NAME \
5108 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
5109 "::tuple<(anonymous namespace)::ui*, " \
5110 "std::default_delete<(anonymous namespace)::ui>, void>"
5111
5112 Z_SYM_NAME
5113 };
5114
5115 /* Returns true if the mapped_index_base::find_name_component_bounds
5116 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
5117 in completion mode. */
5118
5119 static bool
5120 check_find_bounds_finds (mapped_index_base &index,
5121 const char *search_name,
5122 gdb::array_view<const char *> expected_syms)
5123 {
5124 lookup_name_info lookup_name (search_name,
5125 symbol_name_match_type::FULL, true);
5126
5127 auto bounds = index.find_name_components_bounds (lookup_name);
5128
5129 size_t distance = std::distance (bounds.first, bounds.second);
5130 if (distance != expected_syms.size ())
5131 return false;
5132
5133 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
5134 {
5135 auto nc_elem = bounds.first + exp_elem;
5136 const char *qualified = index.symbol_name_at (nc_elem->idx);
5137 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
5138 return false;
5139 }
5140
5141 return true;
5142 }
5143
5144 /* Test the lower-level mapped_index::find_name_component_bounds
5145 method. */
5146
5147 static void
5148 test_mapped_index_find_name_component_bounds ()
5149 {
5150 mock_mapped_index mock_index (test_symbols);
5151
5152 mock_index.build_name_components ();
5153
5154 /* Test the lower-level mapped_index::find_name_component_bounds
5155 method in completion mode. */
5156 {
5157 static const char *expected_syms[] = {
5158 "t1_func",
5159 "t1_func1",
5160 };
5161
5162 SELF_CHECK (check_find_bounds_finds (mock_index,
5163 "t1_func", expected_syms));
5164 }
5165
5166 /* Check that the increment-last-char in the name matching algorithm
5167 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5168 {
5169 static const char *expected_syms1[] = {
5170 "\377",
5171 "\377\377123",
5172 };
5173 SELF_CHECK (check_find_bounds_finds (mock_index,
5174 "\377", expected_syms1));
5175
5176 static const char *expected_syms2[] = {
5177 "\377\377123",
5178 };
5179 SELF_CHECK (check_find_bounds_finds (mock_index,
5180 "\377\377", expected_syms2));
5181 }
5182 }
5183
5184 /* Test dw2_expand_symtabs_matching_symbol. */
5185
5186 static void
5187 test_dw2_expand_symtabs_matching_symbol ()
5188 {
5189 mock_mapped_index mock_index (test_symbols);
5190
5191 /* We let all tests run until the end even if some fails, for debug
5192 convenience. */
5193 bool any_mismatch = false;
5194
5195 /* Create the expected symbols list (an initializer_list). Needed
5196 because lists have commas, and we need to pass them to CHECK,
5197 which is a macro. */
5198 #define EXPECT(...) { __VA_ARGS__ }
5199
5200 /* Wrapper for check_match that passes down the current
5201 __FILE__/__LINE__. */
5202 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5203 any_mismatch |= !check_match (__FILE__, __LINE__, \
5204 mock_index, \
5205 NAME, MATCH_TYPE, COMPLETION_MODE, \
5206 EXPECTED_LIST)
5207
5208 /* Identity checks. */
5209 for (const char *sym : test_symbols)
5210 {
5211 /* Should be able to match all existing symbols. */
5212 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5213 EXPECT (sym));
5214
5215 /* Should be able to match all existing symbols with
5216 parameters. */
5217 std::string with_params = std::string (sym) + "(int)";
5218 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5219 EXPECT (sym));
5220
5221 /* Should be able to match all existing symbols with
5222 parameters and qualifiers. */
5223 with_params = std::string (sym) + " ( int ) const";
5224 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5225 EXPECT (sym));
5226
5227 /* This should really find sym, but cp-name-parser.y doesn't
5228 know about lvalue/rvalue qualifiers yet. */
5229 with_params = std::string (sym) + " ( int ) &&";
5230 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5231 {});
5232 }
5233
5234 /* Check that the name matching algorithm for completion doesn't get
5235 confused with Latin1 'ÿ' / 0xff. */
5236 {
5237 static const char str[] = "\377";
5238 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5239 EXPECT ("\377", "\377\377123"));
5240 }
5241
5242 /* Check that the increment-last-char in the matching algorithm for
5243 completion doesn't match "t1_fund" when completing "t1_func". */
5244 {
5245 static const char str[] = "t1_func";
5246 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5247 EXPECT ("t1_func", "t1_func1"));
5248 }
5249
5250 /* Check that completion mode works at each prefix of the expected
5251 symbol name. */
5252 {
5253 static const char str[] = "function(int)";
5254 size_t len = strlen (str);
5255 std::string lookup;
5256
5257 for (size_t i = 1; i < len; i++)
5258 {
5259 lookup.assign (str, i);
5260 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5261 EXPECT ("function"));
5262 }
5263 }
5264
5265 /* While "w" is a prefix of both components, the match function
5266 should still only be called once. */
5267 {
5268 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5269 EXPECT ("w1::w2"));
5270 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5271 EXPECT ("w1::w2"));
5272 }
5273
5274 /* Same, with a "complicated" symbol. */
5275 {
5276 static const char str[] = Z_SYM_NAME;
5277 size_t len = strlen (str);
5278 std::string lookup;
5279
5280 for (size_t i = 1; i < len; i++)
5281 {
5282 lookup.assign (str, i);
5283 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5284 EXPECT (Z_SYM_NAME));
5285 }
5286 }
5287
5288 /* In FULL mode, an incomplete symbol doesn't match. */
5289 {
5290 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5291 {});
5292 }
5293
5294 /* A complete symbol with parameters matches any overload, since the
5295 index has no overload info. */
5296 {
5297 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5298 EXPECT ("std::zfunction", "std::zfunction2"));
5299 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5300 EXPECT ("std::zfunction", "std::zfunction2"));
5301 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5302 EXPECT ("std::zfunction", "std::zfunction2"));
5303 }
5304
5305 /* Check that whitespace is ignored appropriately. A symbol with a
5306 template argument list. */
5307 {
5308 static const char expected[] = "ns::foo<int>";
5309 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5310 EXPECT (expected));
5311 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5312 EXPECT (expected));
5313 }
5314
5315 /* Check that whitespace is ignored appropriately. A symbol with a
5316 template argument list that includes a pointer. */
5317 {
5318 static const char expected[] = "ns::foo<char*>";
5319 /* Try both completion and non-completion modes. */
5320 static const bool completion_mode[2] = {false, true};
5321 for (size_t i = 0; i < 2; i++)
5322 {
5323 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5324 completion_mode[i], EXPECT (expected));
5325 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5326 completion_mode[i], EXPECT (expected));
5327
5328 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5329 completion_mode[i], EXPECT (expected));
5330 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5331 completion_mode[i], EXPECT (expected));
5332 }
5333 }
5334
5335 {
5336 /* Check method qualifiers are ignored. */
5337 static const char expected[] = "ns::foo<char*>";
5338 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5339 symbol_name_match_type::FULL, true, EXPECT (expected));
5340 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5341 symbol_name_match_type::FULL, true, EXPECT (expected));
5342 CHECK_MATCH ("foo < char * > ( int ) const",
5343 symbol_name_match_type::WILD, true, EXPECT (expected));
5344 CHECK_MATCH ("foo < char * > ( int ) &&",
5345 symbol_name_match_type::WILD, true, EXPECT (expected));
5346 }
5347
5348 /* Test lookup names that don't match anything. */
5349 {
5350 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5351 {});
5352
5353 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5354 {});
5355 }
5356
5357 /* Some wild matching tests, exercising "(anonymous namespace)",
5358 which should not be confused with a parameter list. */
5359 {
5360 static const char *syms[] = {
5361 "A::B::C",
5362 "B::C",
5363 "C",
5364 "A :: B :: C ( int )",
5365 "B :: C ( int )",
5366 "C ( int )",
5367 };
5368
5369 for (const char *s : syms)
5370 {
5371 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5372 EXPECT ("(anonymous namespace)::A::B::C"));
5373 }
5374 }
5375
5376 {
5377 static const char expected[] = "ns2::tmpl<int>::foo2";
5378 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5379 EXPECT (expected));
5380 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5381 EXPECT (expected));
5382 }
5383
5384 SELF_CHECK (!any_mismatch);
5385
5386 #undef EXPECT
5387 #undef CHECK_MATCH
5388 }
5389
5390 static void
5391 run_test ()
5392 {
5393 test_mapped_index_find_name_component_bounds ();
5394 test_dw2_expand_symtabs_matching_symbol ();
5395 }
5396
5397 }} // namespace selftests::dw2_expand_symtabs_matching
5398
5399 #endif /* GDB_SELF_TEST */
5400
5401 /* If FILE_MATCHER is NULL or if PER_CU has
5402 dwarf2_per_cu_quick_data::MARK set (see
5403 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5404 EXPANSION_NOTIFY on it. */
5405
5406 static void
5407 dw2_expand_symtabs_matching_one
5408 (struct dwarf2_per_cu_data *per_cu,
5409 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5410 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5411 {
5412 if (file_matcher == NULL || per_cu->v.quick->mark)
5413 {
5414 bool symtab_was_null
5415 = (per_cu->v.quick->compunit_symtab == NULL);
5416
5417 dw2_instantiate_symtab (per_cu);
5418
5419 if (expansion_notify != NULL
5420 && symtab_was_null
5421 && per_cu->v.quick->compunit_symtab != NULL)
5422 expansion_notify (per_cu->v.quick->compunit_symtab);
5423 }
5424 }
5425
5426 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5427 matched, to expand corresponding CUs that were marked. IDX is the
5428 index of the symbol name that matched. */
5429
5430 static void
5431 dw2_expand_marked_cus
5432 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5433 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5434 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5435 search_domain kind)
5436 {
5437 offset_type *vec, vec_len, vec_idx;
5438 bool global_seen = false;
5439 mapped_index &index = *dwarf2_per_objfile->index_table;
5440
5441 vec = (offset_type *) (index.constant_pool
5442 + MAYBE_SWAP (index.symbol_table[idx].vec));
5443 vec_len = MAYBE_SWAP (vec[0]);
5444 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5445 {
5446 struct dwarf2_per_cu_data *per_cu;
5447 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5448 /* This value is only valid for index versions >= 7. */
5449 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5450 gdb_index_symbol_kind symbol_kind =
5451 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5452 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5453 /* Only check the symbol attributes if they're present.
5454 Indices prior to version 7 don't record them,
5455 and indices >= 7 may elide them for certain symbols
5456 (gold does this). */
5457 int attrs_valid =
5458 (index.version >= 7
5459 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5460
5461 /* Work around gold/15646. */
5462 if (attrs_valid)
5463 {
5464 if (!is_static && global_seen)
5465 continue;
5466 if (!is_static)
5467 global_seen = true;
5468 }
5469
5470 /* Only check the symbol's kind if it has one. */
5471 if (attrs_valid)
5472 {
5473 switch (kind)
5474 {
5475 case VARIABLES_DOMAIN:
5476 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5477 continue;
5478 break;
5479 case FUNCTIONS_DOMAIN:
5480 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5481 continue;
5482 break;
5483 case TYPES_DOMAIN:
5484 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5485 continue;
5486 break;
5487 default:
5488 break;
5489 }
5490 }
5491
5492 /* Don't crash on bad data. */
5493 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5494 + dwarf2_per_objfile->n_type_units))
5495 {
5496 complaint (&symfile_complaints,
5497 _(".gdb_index entry has bad CU index"
5498 " [in module %s]"),
5499 objfile_name (dwarf2_per_objfile->objfile));
5500 continue;
5501 }
5502
5503 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
5504 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5505 expansion_notify);
5506 }
5507 }
5508
5509 /* If FILE_MATCHER is non-NULL, set all the
5510 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5511 that match FILE_MATCHER. */
5512
5513 static void
5514 dw_expand_symtabs_matching_file_matcher
5515 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5516 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5517 {
5518 if (file_matcher == NULL)
5519 return;
5520
5521 objfile *const objfile = dwarf2_per_objfile->objfile;
5522
5523 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5524 htab_eq_pointer,
5525 NULL, xcalloc, xfree));
5526 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5527 htab_eq_pointer,
5528 NULL, xcalloc, xfree));
5529
5530 /* The rule is CUs specify all the files, including those used by
5531 any TU, so there's no need to scan TUs here. */
5532
5533 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5534 {
5535 int j;
5536 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5537 struct quick_file_names *file_data;
5538 void **slot;
5539
5540 QUIT;
5541
5542 per_cu->v.quick->mark = 0;
5543
5544 /* We only need to look at symtabs not already expanded. */
5545 if (per_cu->v.quick->compunit_symtab)
5546 continue;
5547
5548 file_data = dw2_get_file_names (per_cu);
5549 if (file_data == NULL)
5550 continue;
5551
5552 if (htab_find (visited_not_found.get (), file_data) != NULL)
5553 continue;
5554 else if (htab_find (visited_found.get (), file_data) != NULL)
5555 {
5556 per_cu->v.quick->mark = 1;
5557 continue;
5558 }
5559
5560 for (j = 0; j < file_data->num_file_names; ++j)
5561 {
5562 const char *this_real_name;
5563
5564 if (file_matcher (file_data->file_names[j], false))
5565 {
5566 per_cu->v.quick->mark = 1;
5567 break;
5568 }
5569
5570 /* Before we invoke realpath, which can get expensive when many
5571 files are involved, do a quick comparison of the basenames. */
5572 if (!basenames_may_differ
5573 && !file_matcher (lbasename (file_data->file_names[j]),
5574 true))
5575 continue;
5576
5577 this_real_name = dw2_get_real_path (objfile, file_data, j);
5578 if (file_matcher (this_real_name, false))
5579 {
5580 per_cu->v.quick->mark = 1;
5581 break;
5582 }
5583 }
5584
5585 slot = htab_find_slot (per_cu->v.quick->mark
5586 ? visited_found.get ()
5587 : visited_not_found.get (),
5588 file_data, INSERT);
5589 *slot = file_data;
5590 }
5591 }
5592
5593 static void
5594 dw2_expand_symtabs_matching
5595 (struct objfile *objfile,
5596 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5597 const lookup_name_info &lookup_name,
5598 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5599 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5600 enum search_domain kind)
5601 {
5602 struct dwarf2_per_objfile *dwarf2_per_objfile
5603 = get_dwarf2_per_objfile (objfile);
5604
5605 /* index_table is NULL if OBJF_READNOW. */
5606 if (!dwarf2_per_objfile->index_table)
5607 return;
5608
5609 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5610
5611 mapped_index &index = *dwarf2_per_objfile->index_table;
5612
5613 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5614 symbol_matcher,
5615 kind, [&] (offset_type idx)
5616 {
5617 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5618 expansion_notify, kind);
5619 });
5620 }
5621
5622 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5623 symtab. */
5624
5625 static struct compunit_symtab *
5626 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5627 CORE_ADDR pc)
5628 {
5629 int i;
5630
5631 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5632 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5633 return cust;
5634
5635 if (cust->includes == NULL)
5636 return NULL;
5637
5638 for (i = 0; cust->includes[i]; ++i)
5639 {
5640 struct compunit_symtab *s = cust->includes[i];
5641
5642 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5643 if (s != NULL)
5644 return s;
5645 }
5646
5647 return NULL;
5648 }
5649
5650 static struct compunit_symtab *
5651 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5652 struct bound_minimal_symbol msymbol,
5653 CORE_ADDR pc,
5654 struct obj_section *section,
5655 int warn_if_readin)
5656 {
5657 struct dwarf2_per_cu_data *data;
5658 struct compunit_symtab *result;
5659
5660 if (!objfile->psymtabs_addrmap)
5661 return NULL;
5662
5663 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5664 pc);
5665 if (!data)
5666 return NULL;
5667
5668 if (warn_if_readin && data->v.quick->compunit_symtab)
5669 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5670 paddress (get_objfile_arch (objfile), pc));
5671
5672 result
5673 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5674 pc);
5675 gdb_assert (result != NULL);
5676 return result;
5677 }
5678
5679 static void
5680 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5681 void *data, int need_fullname)
5682 {
5683 struct dwarf2_per_objfile *dwarf2_per_objfile
5684 = get_dwarf2_per_objfile (objfile);
5685
5686 if (!dwarf2_per_objfile->filenames_cache)
5687 {
5688 dwarf2_per_objfile->filenames_cache.emplace ();
5689
5690 htab_up visited (htab_create_alloc (10,
5691 htab_hash_pointer, htab_eq_pointer,
5692 NULL, xcalloc, xfree));
5693
5694 /* The rule is CUs specify all the files, including those used
5695 by any TU, so there's no need to scan TUs here. We can
5696 ignore file names coming from already-expanded CUs. */
5697
5698 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5699 {
5700 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
5701
5702 if (per_cu->v.quick->compunit_symtab)
5703 {
5704 void **slot = htab_find_slot (visited.get (),
5705 per_cu->v.quick->file_names,
5706 INSERT);
5707
5708 *slot = per_cu->v.quick->file_names;
5709 }
5710 }
5711
5712 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5713 {
5714 dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5715 struct quick_file_names *file_data;
5716 void **slot;
5717
5718 /* We only need to look at symtabs not already expanded. */
5719 if (per_cu->v.quick->compunit_symtab)
5720 continue;
5721
5722 file_data = dw2_get_file_names (per_cu);
5723 if (file_data == NULL)
5724 continue;
5725
5726 slot = htab_find_slot (visited.get (), file_data, INSERT);
5727 if (*slot)
5728 {
5729 /* Already visited. */
5730 continue;
5731 }
5732 *slot = file_data;
5733
5734 for (int j = 0; j < file_data->num_file_names; ++j)
5735 {
5736 const char *filename = file_data->file_names[j];
5737 dwarf2_per_objfile->filenames_cache->seen (filename);
5738 }
5739 }
5740 }
5741
5742 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5743 {
5744 gdb::unique_xmalloc_ptr<char> this_real_name;
5745
5746 if (need_fullname)
5747 this_real_name = gdb_realpath (filename);
5748 (*fun) (filename, this_real_name.get (), data);
5749 });
5750 }
5751
5752 static int
5753 dw2_has_symbols (struct objfile *objfile)
5754 {
5755 return 1;
5756 }
5757
5758 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5759 {
5760 dw2_has_symbols,
5761 dw2_find_last_source_symtab,
5762 dw2_forget_cached_source_info,
5763 dw2_map_symtabs_matching_filename,
5764 dw2_lookup_symbol,
5765 dw2_print_stats,
5766 dw2_dump,
5767 dw2_relocate,
5768 dw2_expand_symtabs_for_function,
5769 dw2_expand_all_symtabs,
5770 dw2_expand_symtabs_with_fullname,
5771 dw2_map_matching_symbols,
5772 dw2_expand_symtabs_matching,
5773 dw2_find_pc_sect_compunit_symtab,
5774 NULL,
5775 dw2_map_symbol_filenames
5776 };
5777
5778 /* DWARF-5 debug_names reader. */
5779
5780 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5781 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5782
5783 /* A helper function that reads the .debug_names section in SECTION
5784 and fills in MAP. FILENAME is the name of the file containing the
5785 section; it is used for error reporting.
5786
5787 Returns true if all went well, false otherwise. */
5788
5789 static bool
5790 read_debug_names_from_section (struct objfile *objfile,
5791 const char *filename,
5792 struct dwarf2_section_info *section,
5793 mapped_debug_names &map)
5794 {
5795 if (dwarf2_section_empty_p (section))
5796 return false;
5797
5798 /* Older elfutils strip versions could keep the section in the main
5799 executable while splitting it for the separate debug info file. */
5800 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5801 return false;
5802
5803 dwarf2_read_section (objfile, section);
5804
5805 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5806
5807 const gdb_byte *addr = section->buffer;
5808
5809 bfd *const abfd = get_section_bfd_owner (section);
5810
5811 unsigned int bytes_read;
5812 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5813 addr += bytes_read;
5814
5815 map.dwarf5_is_dwarf64 = bytes_read != 4;
5816 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5817 if (bytes_read + length != section->size)
5818 {
5819 /* There may be multiple per-CU indices. */
5820 warning (_("Section .debug_names in %s length %s does not match "
5821 "section length %s, ignoring .debug_names."),
5822 filename, plongest (bytes_read + length),
5823 pulongest (section->size));
5824 return false;
5825 }
5826
5827 /* The version number. */
5828 uint16_t version = read_2_bytes (abfd, addr);
5829 addr += 2;
5830 if (version != 5)
5831 {
5832 warning (_("Section .debug_names in %s has unsupported version %d, "
5833 "ignoring .debug_names."),
5834 filename, version);
5835 return false;
5836 }
5837
5838 /* Padding. */
5839 uint16_t padding = read_2_bytes (abfd, addr);
5840 addr += 2;
5841 if (padding != 0)
5842 {
5843 warning (_("Section .debug_names in %s has unsupported padding %d, "
5844 "ignoring .debug_names."),
5845 filename, padding);
5846 return false;
5847 }
5848
5849 /* comp_unit_count - The number of CUs in the CU list. */
5850 map.cu_count = read_4_bytes (abfd, addr);
5851 addr += 4;
5852
5853 /* local_type_unit_count - The number of TUs in the local TU
5854 list. */
5855 map.tu_count = read_4_bytes (abfd, addr);
5856 addr += 4;
5857
5858 /* foreign_type_unit_count - The number of TUs in the foreign TU
5859 list. */
5860 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5861 addr += 4;
5862 if (foreign_tu_count != 0)
5863 {
5864 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5865 "ignoring .debug_names."),
5866 filename, static_cast<unsigned long> (foreign_tu_count));
5867 return false;
5868 }
5869
5870 /* bucket_count - The number of hash buckets in the hash lookup
5871 table. */
5872 map.bucket_count = read_4_bytes (abfd, addr);
5873 addr += 4;
5874
5875 /* name_count - The number of unique names in the index. */
5876 map.name_count = read_4_bytes (abfd, addr);
5877 addr += 4;
5878
5879 /* abbrev_table_size - The size in bytes of the abbreviations
5880 table. */
5881 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5882 addr += 4;
5883
5884 /* augmentation_string_size - The size in bytes of the augmentation
5885 string. This value is rounded up to a multiple of 4. */
5886 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5887 addr += 4;
5888 map.augmentation_is_gdb = ((augmentation_string_size
5889 == sizeof (dwarf5_augmentation))
5890 && memcmp (addr, dwarf5_augmentation,
5891 sizeof (dwarf5_augmentation)) == 0);
5892 augmentation_string_size += (-augmentation_string_size) & 3;
5893 addr += augmentation_string_size;
5894
5895 /* List of CUs */
5896 map.cu_table_reordered = addr;
5897 addr += map.cu_count * map.offset_size;
5898
5899 /* List of Local TUs */
5900 map.tu_table_reordered = addr;
5901 addr += map.tu_count * map.offset_size;
5902
5903 /* Hash Lookup Table */
5904 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5905 addr += map.bucket_count * 4;
5906 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5907 addr += map.name_count * 4;
5908
5909 /* Name Table */
5910 map.name_table_string_offs_reordered = addr;
5911 addr += map.name_count * map.offset_size;
5912 map.name_table_entry_offs_reordered = addr;
5913 addr += map.name_count * map.offset_size;
5914
5915 const gdb_byte *abbrev_table_start = addr;
5916 for (;;)
5917 {
5918 unsigned int bytes_read;
5919 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5920 addr += bytes_read;
5921 if (index_num == 0)
5922 break;
5923
5924 const auto insertpair
5925 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5926 if (!insertpair.second)
5927 {
5928 warning (_("Section .debug_names in %s has duplicate index %s, "
5929 "ignoring .debug_names."),
5930 filename, pulongest (index_num));
5931 return false;
5932 }
5933 mapped_debug_names::index_val &indexval = insertpair.first->second;
5934 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5935 addr += bytes_read;
5936
5937 for (;;)
5938 {
5939 mapped_debug_names::index_val::attr attr;
5940 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5941 addr += bytes_read;
5942 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5943 addr += bytes_read;
5944 if (attr.form == DW_FORM_implicit_const)
5945 {
5946 attr.implicit_const = read_signed_leb128 (abfd, addr,
5947 &bytes_read);
5948 addr += bytes_read;
5949 }
5950 if (attr.dw_idx == 0 && attr.form == 0)
5951 break;
5952 indexval.attr_vec.push_back (std::move (attr));
5953 }
5954 }
5955 if (addr != abbrev_table_start + abbrev_table_size)
5956 {
5957 warning (_("Section .debug_names in %s has abbreviation_table "
5958 "of size %zu vs. written as %u, ignoring .debug_names."),
5959 filename, addr - abbrev_table_start, abbrev_table_size);
5960 return false;
5961 }
5962 map.entry_pool = addr;
5963
5964 return true;
5965 }
5966
5967 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5968 list. */
5969
5970 static void
5971 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5972 const mapped_debug_names &map,
5973 dwarf2_section_info &section,
5974 bool is_dwz, int base_offset)
5975 {
5976 sect_offset sect_off_prev;
5977 for (uint32_t i = 0; i <= map.cu_count; ++i)
5978 {
5979 sect_offset sect_off_next;
5980 if (i < map.cu_count)
5981 {
5982 sect_off_next
5983 = (sect_offset) (extract_unsigned_integer
5984 (map.cu_table_reordered + i * map.offset_size,
5985 map.offset_size,
5986 map.dwarf5_byte_order));
5987 }
5988 else
5989 sect_off_next = (sect_offset) section.size;
5990 if (i >= 1)
5991 {
5992 const ULONGEST length = sect_off_next - sect_off_prev;
5993 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
5994 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5995 sect_off_prev, length);
5996 }
5997 sect_off_prev = sect_off_next;
5998 }
5999 }
6000
6001 /* Read the CU list from the mapped index, and use it to create all
6002 the CU objects for this dwarf2_per_objfile. */
6003
6004 static void
6005 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
6006 const mapped_debug_names &map,
6007 const mapped_debug_names &dwz_map)
6008 {
6009 struct objfile *objfile = dwarf2_per_objfile->objfile;
6010
6011 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
6012 dwarf2_per_objfile->all_comp_units
6013 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
6014 dwarf2_per_objfile->n_comp_units);
6015
6016 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
6017 dwarf2_per_objfile->info,
6018 false /* is_dwz */,
6019 0 /* base_offset */);
6020
6021 if (dwz_map.cu_count == 0)
6022 return;
6023
6024 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
6025 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
6026 true /* is_dwz */,
6027 map.cu_count /* base_offset */);
6028 }
6029
6030 /* Read .debug_names. If everything went ok, initialize the "quick"
6031 elements of all the CUs and return true. Otherwise, return false. */
6032
6033 static bool
6034 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
6035 {
6036 mapped_debug_names local_map (dwarf2_per_objfile);
6037 mapped_debug_names dwz_map (dwarf2_per_objfile);
6038 struct objfile *objfile = dwarf2_per_objfile->objfile;
6039
6040 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
6041 &dwarf2_per_objfile->debug_names,
6042 local_map))
6043 return false;
6044
6045 /* Don't use the index if it's empty. */
6046 if (local_map.name_count == 0)
6047 return false;
6048
6049 /* If there is a .dwz file, read it so we can get its CU list as
6050 well. */
6051 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
6052 if (dwz != NULL)
6053 {
6054 if (!read_debug_names_from_section (objfile,
6055 bfd_get_filename (dwz->dwz_bfd),
6056 &dwz->debug_names, dwz_map))
6057 {
6058 warning (_("could not read '.debug_names' section from %s; skipping"),
6059 bfd_get_filename (dwz->dwz_bfd));
6060 return false;
6061 }
6062 }
6063
6064 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
6065
6066 if (local_map.tu_count != 0)
6067 {
6068 /* We can only handle a single .debug_types when we have an
6069 index. */
6070 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
6071 return false;
6072
6073 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
6074 dwarf2_per_objfile->types, 0);
6075
6076 create_signatured_type_table_from_debug_names
6077 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
6078 }
6079
6080 create_addrmap_from_aranges (dwarf2_per_objfile,
6081 &dwarf2_per_objfile->debug_aranges);
6082
6083 dwarf2_per_objfile->debug_names_table.reset
6084 (new mapped_debug_names (dwarf2_per_objfile));
6085 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
6086 dwarf2_per_objfile->using_index = 1;
6087 dwarf2_per_objfile->quick_file_names_table =
6088 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6089
6090 return true;
6091 }
6092
6093 /* Symbol name hashing function as specified by DWARF-5. */
6094
6095 static uint32_t
6096 dwarf5_djb_hash (const char *str_)
6097 {
6098 const unsigned char *str = (const unsigned char *) str_;
6099
6100 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
6101 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
6102
6103 uint32_t hash = 5381;
6104 while (int c = *str++)
6105 hash = hash * 33 + tolower (c);
6106 return hash;
6107 }
6108
6109 /* Type used to manage iterating over all CUs looking for a symbol for
6110 .debug_names. */
6111
6112 class dw2_debug_names_iterator
6113 {
6114 public:
6115 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
6116 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
6117 dw2_debug_names_iterator (const mapped_debug_names &map,
6118 bool want_specific_block,
6119 block_enum block_index, domain_enum domain,
6120 const char *name)
6121 : m_map (map), m_want_specific_block (want_specific_block),
6122 m_block_index (block_index), m_domain (domain),
6123 m_addr (find_vec_in_debug_names (map, name))
6124 {}
6125
6126 dw2_debug_names_iterator (const mapped_debug_names &map,
6127 search_domain search, uint32_t namei)
6128 : m_map (map),
6129 m_search (search),
6130 m_addr (find_vec_in_debug_names (map, namei))
6131 {}
6132
6133 /* Return the next matching CU or NULL if there are no more. */
6134 dwarf2_per_cu_data *next ();
6135
6136 private:
6137 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6138 const char *name);
6139 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6140 uint32_t namei);
6141
6142 /* The internalized form of .debug_names. */
6143 const mapped_debug_names &m_map;
6144
6145 /* If true, only look for symbols that match BLOCK_INDEX. */
6146 const bool m_want_specific_block = false;
6147
6148 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
6149 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
6150 value. */
6151 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
6152
6153 /* The kind of symbol we're looking for. */
6154 const domain_enum m_domain = UNDEF_DOMAIN;
6155 const search_domain m_search = ALL_DOMAIN;
6156
6157 /* The list of CUs from the index entry of the symbol, or NULL if
6158 not found. */
6159 const gdb_byte *m_addr;
6160 };
6161
6162 const char *
6163 mapped_debug_names::namei_to_name (uint32_t namei) const
6164 {
6165 const ULONGEST namei_string_offs
6166 = extract_unsigned_integer ((name_table_string_offs_reordered
6167 + namei * offset_size),
6168 offset_size,
6169 dwarf5_byte_order);
6170 return read_indirect_string_at_offset
6171 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
6172 }
6173
6174 /* Find a slot in .debug_names for the object named NAME. If NAME is
6175 found, return pointer to its pool data. If NAME cannot be found,
6176 return NULL. */
6177
6178 const gdb_byte *
6179 dw2_debug_names_iterator::find_vec_in_debug_names
6180 (const mapped_debug_names &map, const char *name)
6181 {
6182 int (*cmp) (const char *, const char *);
6183
6184 if (current_language->la_language == language_cplus
6185 || current_language->la_language == language_fortran
6186 || current_language->la_language == language_d)
6187 {
6188 /* NAME is already canonical. Drop any qualifiers as
6189 .debug_names does not contain any. */
6190
6191 if (strchr (name, '(') != NULL)
6192 {
6193 gdb::unique_xmalloc_ptr<char> without_params
6194 = cp_remove_params (name);
6195
6196 if (without_params != NULL)
6197 {
6198 name = without_params.get();
6199 }
6200 }
6201 }
6202
6203 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6204
6205 const uint32_t full_hash = dwarf5_djb_hash (name);
6206 uint32_t namei
6207 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6208 (map.bucket_table_reordered
6209 + (full_hash % map.bucket_count)), 4,
6210 map.dwarf5_byte_order);
6211 if (namei == 0)
6212 return NULL;
6213 --namei;
6214 if (namei >= map.name_count)
6215 {
6216 complaint (&symfile_complaints,
6217 _("Wrong .debug_names with name index %u but name_count=%u "
6218 "[in module %s]"),
6219 namei, map.name_count,
6220 objfile_name (map.dwarf2_per_objfile->objfile));
6221 return NULL;
6222 }
6223
6224 for (;;)
6225 {
6226 const uint32_t namei_full_hash
6227 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6228 (map.hash_table_reordered + namei), 4,
6229 map.dwarf5_byte_order);
6230 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6231 return NULL;
6232
6233 if (full_hash == namei_full_hash)
6234 {
6235 const char *const namei_string = map.namei_to_name (namei);
6236
6237 #if 0 /* An expensive sanity check. */
6238 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6239 {
6240 complaint (&symfile_complaints,
6241 _("Wrong .debug_names hash for string at index %u "
6242 "[in module %s]"),
6243 namei, objfile_name (dwarf2_per_objfile->objfile));
6244 return NULL;
6245 }
6246 #endif
6247
6248 if (cmp (namei_string, name) == 0)
6249 {
6250 const ULONGEST namei_entry_offs
6251 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6252 + namei * map.offset_size),
6253 map.offset_size, map.dwarf5_byte_order);
6254 return map.entry_pool + namei_entry_offs;
6255 }
6256 }
6257
6258 ++namei;
6259 if (namei >= map.name_count)
6260 return NULL;
6261 }
6262 }
6263
6264 const gdb_byte *
6265 dw2_debug_names_iterator::find_vec_in_debug_names
6266 (const mapped_debug_names &map, uint32_t namei)
6267 {
6268 if (namei >= map.name_count)
6269 {
6270 complaint (&symfile_complaints,
6271 _("Wrong .debug_names with name index %u but name_count=%u "
6272 "[in module %s]"),
6273 namei, map.name_count,
6274 objfile_name (map.dwarf2_per_objfile->objfile));
6275 return NULL;
6276 }
6277
6278 const ULONGEST namei_entry_offs
6279 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6280 + namei * map.offset_size),
6281 map.offset_size, map.dwarf5_byte_order);
6282 return map.entry_pool + namei_entry_offs;
6283 }
6284
6285 /* See dw2_debug_names_iterator. */
6286
6287 dwarf2_per_cu_data *
6288 dw2_debug_names_iterator::next ()
6289 {
6290 if (m_addr == NULL)
6291 return NULL;
6292
6293 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
6294 struct objfile *objfile = dwarf2_per_objfile->objfile;
6295 bfd *const abfd = objfile->obfd;
6296
6297 again:
6298
6299 unsigned int bytes_read;
6300 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6301 m_addr += bytes_read;
6302 if (abbrev == 0)
6303 return NULL;
6304
6305 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6306 if (indexval_it == m_map.abbrev_map.cend ())
6307 {
6308 complaint (&symfile_complaints,
6309 _("Wrong .debug_names undefined abbrev code %s "
6310 "[in module %s]"),
6311 pulongest (abbrev), objfile_name (objfile));
6312 return NULL;
6313 }
6314 const mapped_debug_names::index_val &indexval = indexval_it->second;
6315 bool have_is_static = false;
6316 bool is_static;
6317 dwarf2_per_cu_data *per_cu = NULL;
6318 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6319 {
6320 ULONGEST ull;
6321 switch (attr.form)
6322 {
6323 case DW_FORM_implicit_const:
6324 ull = attr.implicit_const;
6325 break;
6326 case DW_FORM_flag_present:
6327 ull = 1;
6328 break;
6329 case DW_FORM_udata:
6330 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6331 m_addr += bytes_read;
6332 break;
6333 default:
6334 complaint (&symfile_complaints,
6335 _("Unsupported .debug_names form %s [in module %s]"),
6336 dwarf_form_name (attr.form),
6337 objfile_name (objfile));
6338 return NULL;
6339 }
6340 switch (attr.dw_idx)
6341 {
6342 case DW_IDX_compile_unit:
6343 /* Don't crash on bad data. */
6344 if (ull >= dwarf2_per_objfile->n_comp_units)
6345 {
6346 complaint (&symfile_complaints,
6347 _(".debug_names entry has bad CU index %s"
6348 " [in module %s]"),
6349 pulongest (ull),
6350 objfile_name (dwarf2_per_objfile->objfile));
6351 continue;
6352 }
6353 per_cu = dw2_get_cutu (dwarf2_per_objfile, ull);
6354 break;
6355 case DW_IDX_type_unit:
6356 /* Don't crash on bad data. */
6357 if (ull >= dwarf2_per_objfile->n_type_units)
6358 {
6359 complaint (&symfile_complaints,
6360 _(".debug_names entry has bad TU index %s"
6361 " [in module %s]"),
6362 pulongest (ull),
6363 objfile_name (dwarf2_per_objfile->objfile));
6364 continue;
6365 }
6366 per_cu = dw2_get_cutu (dwarf2_per_objfile,
6367 dwarf2_per_objfile->n_comp_units + ull);
6368 break;
6369 case DW_IDX_GNU_internal:
6370 if (!m_map.augmentation_is_gdb)
6371 break;
6372 have_is_static = true;
6373 is_static = true;
6374 break;
6375 case DW_IDX_GNU_external:
6376 if (!m_map.augmentation_is_gdb)
6377 break;
6378 have_is_static = true;
6379 is_static = false;
6380 break;
6381 }
6382 }
6383
6384 /* Skip if already read in. */
6385 if (per_cu->v.quick->compunit_symtab)
6386 goto again;
6387
6388 /* Check static vs global. */
6389 if (have_is_static)
6390 {
6391 const bool want_static = m_block_index != GLOBAL_BLOCK;
6392 if (m_want_specific_block && want_static != is_static)
6393 goto again;
6394 }
6395
6396 /* Match dw2_symtab_iter_next, symbol_kind
6397 and debug_names::psymbol_tag. */
6398 switch (m_domain)
6399 {
6400 case VAR_DOMAIN:
6401 switch (indexval.dwarf_tag)
6402 {
6403 case DW_TAG_variable:
6404 case DW_TAG_subprogram:
6405 /* Some types are also in VAR_DOMAIN. */
6406 case DW_TAG_typedef:
6407 case DW_TAG_structure_type:
6408 break;
6409 default:
6410 goto again;
6411 }
6412 break;
6413 case STRUCT_DOMAIN:
6414 switch (indexval.dwarf_tag)
6415 {
6416 case DW_TAG_typedef:
6417 case DW_TAG_structure_type:
6418 break;
6419 default:
6420 goto again;
6421 }
6422 break;
6423 case LABEL_DOMAIN:
6424 switch (indexval.dwarf_tag)
6425 {
6426 case 0:
6427 case DW_TAG_variable:
6428 break;
6429 default:
6430 goto again;
6431 }
6432 break;
6433 default:
6434 break;
6435 }
6436
6437 /* Match dw2_expand_symtabs_matching, symbol_kind and
6438 debug_names::psymbol_tag. */
6439 switch (m_search)
6440 {
6441 case VARIABLES_DOMAIN:
6442 switch (indexval.dwarf_tag)
6443 {
6444 case DW_TAG_variable:
6445 break;
6446 default:
6447 goto again;
6448 }
6449 break;
6450 case FUNCTIONS_DOMAIN:
6451 switch (indexval.dwarf_tag)
6452 {
6453 case DW_TAG_subprogram:
6454 break;
6455 default:
6456 goto again;
6457 }
6458 break;
6459 case TYPES_DOMAIN:
6460 switch (indexval.dwarf_tag)
6461 {
6462 case DW_TAG_typedef:
6463 case DW_TAG_structure_type:
6464 break;
6465 default:
6466 goto again;
6467 }
6468 break;
6469 default:
6470 break;
6471 }
6472
6473 return per_cu;
6474 }
6475
6476 static struct compunit_symtab *
6477 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6478 const char *name, domain_enum domain)
6479 {
6480 const block_enum block_index = static_cast<block_enum> (block_index_int);
6481 struct dwarf2_per_objfile *dwarf2_per_objfile
6482 = get_dwarf2_per_objfile (objfile);
6483
6484 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6485 if (!mapp)
6486 {
6487 /* index is NULL if OBJF_READNOW. */
6488 return NULL;
6489 }
6490 const auto &map = *mapp;
6491
6492 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6493 block_index, domain, name);
6494
6495 struct compunit_symtab *stab_best = NULL;
6496 struct dwarf2_per_cu_data *per_cu;
6497 while ((per_cu = iter.next ()) != NULL)
6498 {
6499 struct symbol *sym, *with_opaque = NULL;
6500 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6501 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6502 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6503
6504 sym = block_find_symbol (block, name, domain,
6505 block_find_non_opaque_type_preferred,
6506 &with_opaque);
6507
6508 /* Some caution must be observed with overloaded functions and
6509 methods, since the index will not contain any overload
6510 information (but NAME might contain it). */
6511
6512 if (sym != NULL
6513 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6514 return stab;
6515 if (with_opaque != NULL
6516 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6517 stab_best = stab;
6518
6519 /* Keep looking through other CUs. */
6520 }
6521
6522 return stab_best;
6523 }
6524
6525 /* This dumps minimal information about .debug_names. It is called
6526 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6527 uses this to verify that .debug_names has been loaded. */
6528
6529 static void
6530 dw2_debug_names_dump (struct objfile *objfile)
6531 {
6532 struct dwarf2_per_objfile *dwarf2_per_objfile
6533 = get_dwarf2_per_objfile (objfile);
6534
6535 gdb_assert (dwarf2_per_objfile->using_index);
6536 printf_filtered (".debug_names:");
6537 if (dwarf2_per_objfile->debug_names_table)
6538 printf_filtered (" exists\n");
6539 else
6540 printf_filtered (" faked for \"readnow\"\n");
6541 printf_filtered ("\n");
6542 }
6543
6544 static void
6545 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6546 const char *func_name)
6547 {
6548 struct dwarf2_per_objfile *dwarf2_per_objfile
6549 = get_dwarf2_per_objfile (objfile);
6550
6551 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6552 if (dwarf2_per_objfile->debug_names_table)
6553 {
6554 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6555
6556 /* Note: It doesn't matter what we pass for block_index here. */
6557 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6558 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6559
6560 struct dwarf2_per_cu_data *per_cu;
6561 while ((per_cu = iter.next ()) != NULL)
6562 dw2_instantiate_symtab (per_cu);
6563 }
6564 }
6565
6566 static void
6567 dw2_debug_names_expand_symtabs_matching
6568 (struct objfile *objfile,
6569 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6570 const lookup_name_info &lookup_name,
6571 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6572 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6573 enum search_domain kind)
6574 {
6575 struct dwarf2_per_objfile *dwarf2_per_objfile
6576 = get_dwarf2_per_objfile (objfile);
6577
6578 /* debug_names_table is NULL if OBJF_READNOW. */
6579 if (!dwarf2_per_objfile->debug_names_table)
6580 return;
6581
6582 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6583
6584 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6585
6586 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6587 symbol_matcher,
6588 kind, [&] (offset_type namei)
6589 {
6590 /* The name was matched, now expand corresponding CUs that were
6591 marked. */
6592 dw2_debug_names_iterator iter (map, kind, namei);
6593
6594 struct dwarf2_per_cu_data *per_cu;
6595 while ((per_cu = iter.next ()) != NULL)
6596 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6597 expansion_notify);
6598 });
6599 }
6600
6601 const struct quick_symbol_functions dwarf2_debug_names_functions =
6602 {
6603 dw2_has_symbols,
6604 dw2_find_last_source_symtab,
6605 dw2_forget_cached_source_info,
6606 dw2_map_symtabs_matching_filename,
6607 dw2_debug_names_lookup_symbol,
6608 dw2_print_stats,
6609 dw2_debug_names_dump,
6610 dw2_relocate,
6611 dw2_debug_names_expand_symtabs_for_function,
6612 dw2_expand_all_symtabs,
6613 dw2_expand_symtabs_with_fullname,
6614 dw2_map_matching_symbols,
6615 dw2_debug_names_expand_symtabs_matching,
6616 dw2_find_pc_sect_compunit_symtab,
6617 NULL,
6618 dw2_map_symbol_filenames
6619 };
6620
6621 /* See symfile.h. */
6622
6623 bool
6624 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6625 {
6626 struct dwarf2_per_objfile *dwarf2_per_objfile
6627 = get_dwarf2_per_objfile (objfile);
6628
6629 /* If we're about to read full symbols, don't bother with the
6630 indices. In this case we also don't care if some other debug
6631 format is making psymtabs, because they are all about to be
6632 expanded anyway. */
6633 if ((objfile->flags & OBJF_READNOW))
6634 {
6635 int i;
6636
6637 dwarf2_per_objfile->using_index = 1;
6638 create_all_comp_units (dwarf2_per_objfile);
6639 create_all_type_units (dwarf2_per_objfile);
6640 dwarf2_per_objfile->quick_file_names_table =
6641 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6642
6643 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
6644 + dwarf2_per_objfile->n_type_units); ++i)
6645 {
6646 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
6647
6648 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6649 struct dwarf2_per_cu_quick_data);
6650 }
6651
6652 /* Return 1 so that gdb sees the "quick" functions. However,
6653 these functions will be no-ops because we will have expanded
6654 all symtabs. */
6655 *index_kind = dw_index_kind::GDB_INDEX;
6656 return true;
6657 }
6658
6659 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6660 {
6661 *index_kind = dw_index_kind::DEBUG_NAMES;
6662 return true;
6663 }
6664
6665 if (dwarf2_read_index (objfile))
6666 {
6667 *index_kind = dw_index_kind::GDB_INDEX;
6668 return true;
6669 }
6670
6671 return false;
6672 }
6673
6674 \f
6675
6676 /* Build a partial symbol table. */
6677
6678 void
6679 dwarf2_build_psymtabs (struct objfile *objfile)
6680 {
6681 struct dwarf2_per_objfile *dwarf2_per_objfile
6682 = get_dwarf2_per_objfile (objfile);
6683
6684 if (objfile->global_psymbols.capacity () == 0
6685 && objfile->static_psymbols.capacity () == 0)
6686 init_psymbol_list (objfile, 1024);
6687
6688 TRY
6689 {
6690 /* This isn't really ideal: all the data we allocate on the
6691 objfile's obstack is still uselessly kept around. However,
6692 freeing it seems unsafe. */
6693 psymtab_discarder psymtabs (objfile);
6694 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6695 psymtabs.keep ();
6696 }
6697 CATCH (except, RETURN_MASK_ERROR)
6698 {
6699 exception_print (gdb_stderr, except);
6700 }
6701 END_CATCH
6702 }
6703
6704 /* Return the total length of the CU described by HEADER. */
6705
6706 static unsigned int
6707 get_cu_length (const struct comp_unit_head *header)
6708 {
6709 return header->initial_length_size + header->length;
6710 }
6711
6712 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6713
6714 static inline bool
6715 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6716 {
6717 sect_offset bottom = cu_header->sect_off;
6718 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6719
6720 return sect_off >= bottom && sect_off < top;
6721 }
6722
6723 /* Find the base address of the compilation unit for range lists and
6724 location lists. It will normally be specified by DW_AT_low_pc.
6725 In DWARF-3 draft 4, the base address could be overridden by
6726 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6727 compilation units with discontinuous ranges. */
6728
6729 static void
6730 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6731 {
6732 struct attribute *attr;
6733
6734 cu->base_known = 0;
6735 cu->base_address = 0;
6736
6737 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6738 if (attr)
6739 {
6740 cu->base_address = attr_value_as_address (attr);
6741 cu->base_known = 1;
6742 }
6743 else
6744 {
6745 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6746 if (attr)
6747 {
6748 cu->base_address = attr_value_as_address (attr);
6749 cu->base_known = 1;
6750 }
6751 }
6752 }
6753
6754 /* Read in the comp unit header information from the debug_info at info_ptr.
6755 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6756 NOTE: This leaves members offset, first_die_offset to be filled in
6757 by the caller. */
6758
6759 static const gdb_byte *
6760 read_comp_unit_head (struct comp_unit_head *cu_header,
6761 const gdb_byte *info_ptr,
6762 struct dwarf2_section_info *section,
6763 rcuh_kind section_kind)
6764 {
6765 int signed_addr;
6766 unsigned int bytes_read;
6767 const char *filename = get_section_file_name (section);
6768 bfd *abfd = get_section_bfd_owner (section);
6769
6770 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6771 cu_header->initial_length_size = bytes_read;
6772 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6773 info_ptr += bytes_read;
6774 cu_header->version = read_2_bytes (abfd, info_ptr);
6775 info_ptr += 2;
6776 if (cu_header->version < 5)
6777 switch (section_kind)
6778 {
6779 case rcuh_kind::COMPILE:
6780 cu_header->unit_type = DW_UT_compile;
6781 break;
6782 case rcuh_kind::TYPE:
6783 cu_header->unit_type = DW_UT_type;
6784 break;
6785 default:
6786 internal_error (__FILE__, __LINE__,
6787 _("read_comp_unit_head: invalid section_kind"));
6788 }
6789 else
6790 {
6791 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6792 (read_1_byte (abfd, info_ptr));
6793 info_ptr += 1;
6794 switch (cu_header->unit_type)
6795 {
6796 case DW_UT_compile:
6797 if (section_kind != rcuh_kind::COMPILE)
6798 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6799 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6800 filename);
6801 break;
6802 case DW_UT_type:
6803 section_kind = rcuh_kind::TYPE;
6804 break;
6805 default:
6806 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6807 "(is %d, should be %d or %d) [in module %s]"),
6808 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6809 }
6810
6811 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6812 info_ptr += 1;
6813 }
6814 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6815 cu_header,
6816 &bytes_read);
6817 info_ptr += bytes_read;
6818 if (cu_header->version < 5)
6819 {
6820 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6821 info_ptr += 1;
6822 }
6823 signed_addr = bfd_get_sign_extend_vma (abfd);
6824 if (signed_addr < 0)
6825 internal_error (__FILE__, __LINE__,
6826 _("read_comp_unit_head: dwarf from non elf file"));
6827 cu_header->signed_addr_p = signed_addr;
6828
6829 if (section_kind == rcuh_kind::TYPE)
6830 {
6831 LONGEST type_offset;
6832
6833 cu_header->signature = read_8_bytes (abfd, info_ptr);
6834 info_ptr += 8;
6835
6836 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6837 info_ptr += bytes_read;
6838 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6839 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6840 error (_("Dwarf Error: Too big type_offset in compilation unit "
6841 "header (is %s) [in module %s]"), plongest (type_offset),
6842 filename);
6843 }
6844
6845 return info_ptr;
6846 }
6847
6848 /* Helper function that returns the proper abbrev section for
6849 THIS_CU. */
6850
6851 static struct dwarf2_section_info *
6852 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6853 {
6854 struct dwarf2_section_info *abbrev;
6855 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6856
6857 if (this_cu->is_dwz)
6858 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6859 else
6860 abbrev = &dwarf2_per_objfile->abbrev;
6861
6862 return abbrev;
6863 }
6864
6865 /* Subroutine of read_and_check_comp_unit_head and
6866 read_and_check_type_unit_head to simplify them.
6867 Perform various error checking on the header. */
6868
6869 static void
6870 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6871 struct comp_unit_head *header,
6872 struct dwarf2_section_info *section,
6873 struct dwarf2_section_info *abbrev_section)
6874 {
6875 const char *filename = get_section_file_name (section);
6876
6877 if (header->version < 2 || header->version > 5)
6878 error (_("Dwarf Error: wrong version in compilation unit header "
6879 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6880 filename);
6881
6882 if (to_underlying (header->abbrev_sect_off)
6883 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6884 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6885 "(offset %s + 6) [in module %s]"),
6886 sect_offset_str (header->abbrev_sect_off),
6887 sect_offset_str (header->sect_off),
6888 filename);
6889
6890 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6891 avoid potential 32-bit overflow. */
6892 if (((ULONGEST) header->sect_off + get_cu_length (header))
6893 > section->size)
6894 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6895 "(offset %s + 0) [in module %s]"),
6896 header->length, sect_offset_str (header->sect_off),
6897 filename);
6898 }
6899
6900 /* Read in a CU/TU header and perform some basic error checking.
6901 The contents of the header are stored in HEADER.
6902 The result is a pointer to the start of the first DIE. */
6903
6904 static const gdb_byte *
6905 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6906 struct comp_unit_head *header,
6907 struct dwarf2_section_info *section,
6908 struct dwarf2_section_info *abbrev_section,
6909 const gdb_byte *info_ptr,
6910 rcuh_kind section_kind)
6911 {
6912 const gdb_byte *beg_of_comp_unit = info_ptr;
6913
6914 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6915
6916 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6917
6918 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6919
6920 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6921 abbrev_section);
6922
6923 return info_ptr;
6924 }
6925
6926 /* Fetch the abbreviation table offset from a comp or type unit header. */
6927
6928 static sect_offset
6929 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6930 struct dwarf2_section_info *section,
6931 sect_offset sect_off)
6932 {
6933 bfd *abfd = get_section_bfd_owner (section);
6934 const gdb_byte *info_ptr;
6935 unsigned int initial_length_size, offset_size;
6936 uint16_t version;
6937
6938 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6939 info_ptr = section->buffer + to_underlying (sect_off);
6940 read_initial_length (abfd, info_ptr, &initial_length_size);
6941 offset_size = initial_length_size == 4 ? 4 : 8;
6942 info_ptr += initial_length_size;
6943
6944 version = read_2_bytes (abfd, info_ptr);
6945 info_ptr += 2;
6946 if (version >= 5)
6947 {
6948 /* Skip unit type and address size. */
6949 info_ptr += 2;
6950 }
6951
6952 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6953 }
6954
6955 /* Allocate a new partial symtab for file named NAME and mark this new
6956 partial symtab as being an include of PST. */
6957
6958 static void
6959 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6960 struct objfile *objfile)
6961 {
6962 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6963
6964 if (!IS_ABSOLUTE_PATH (subpst->filename))
6965 {
6966 /* It shares objfile->objfile_obstack. */
6967 subpst->dirname = pst->dirname;
6968 }
6969
6970 subpst->textlow = 0;
6971 subpst->texthigh = 0;
6972
6973 subpst->dependencies
6974 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6975 subpst->dependencies[0] = pst;
6976 subpst->number_of_dependencies = 1;
6977
6978 subpst->globals_offset = 0;
6979 subpst->n_global_syms = 0;
6980 subpst->statics_offset = 0;
6981 subpst->n_static_syms = 0;
6982 subpst->compunit_symtab = NULL;
6983 subpst->read_symtab = pst->read_symtab;
6984 subpst->readin = 0;
6985
6986 /* No private part is necessary for include psymtabs. This property
6987 can be used to differentiate between such include psymtabs and
6988 the regular ones. */
6989 subpst->read_symtab_private = NULL;
6990 }
6991
6992 /* Read the Line Number Program data and extract the list of files
6993 included by the source file represented by PST. Build an include
6994 partial symtab for each of these included files. */
6995
6996 static void
6997 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6998 struct die_info *die,
6999 struct partial_symtab *pst)
7000 {
7001 line_header_up lh;
7002 struct attribute *attr;
7003
7004 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7005 if (attr)
7006 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
7007 if (lh == NULL)
7008 return; /* No linetable, so no includes. */
7009
7010 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
7011 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
7012 }
7013
7014 static hashval_t
7015 hash_signatured_type (const void *item)
7016 {
7017 const struct signatured_type *sig_type
7018 = (const struct signatured_type *) item;
7019
7020 /* This drops the top 32 bits of the signature, but is ok for a hash. */
7021 return sig_type->signature;
7022 }
7023
7024 static int
7025 eq_signatured_type (const void *item_lhs, const void *item_rhs)
7026 {
7027 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
7028 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
7029
7030 return lhs->signature == rhs->signature;
7031 }
7032
7033 /* Allocate a hash table for signatured types. */
7034
7035 static htab_t
7036 allocate_signatured_type_table (struct objfile *objfile)
7037 {
7038 return htab_create_alloc_ex (41,
7039 hash_signatured_type,
7040 eq_signatured_type,
7041 NULL,
7042 &objfile->objfile_obstack,
7043 hashtab_obstack_allocate,
7044 dummy_obstack_deallocate);
7045 }
7046
7047 /* A helper function to add a signatured type CU to a table. */
7048
7049 static int
7050 add_signatured_type_cu_to_table (void **slot, void *datum)
7051 {
7052 struct signatured_type *sigt = (struct signatured_type *) *slot;
7053 struct signatured_type ***datap = (struct signatured_type ***) datum;
7054
7055 **datap = sigt;
7056 ++*datap;
7057
7058 return 1;
7059 }
7060
7061 /* A helper for create_debug_types_hash_table. Read types from SECTION
7062 and fill them into TYPES_HTAB. It will process only type units,
7063 therefore DW_UT_type. */
7064
7065 static void
7066 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7067 struct dwo_file *dwo_file,
7068 dwarf2_section_info *section, htab_t &types_htab,
7069 rcuh_kind section_kind)
7070 {
7071 struct objfile *objfile = dwarf2_per_objfile->objfile;
7072 struct dwarf2_section_info *abbrev_section;
7073 bfd *abfd;
7074 const gdb_byte *info_ptr, *end_ptr;
7075
7076 abbrev_section = (dwo_file != NULL
7077 ? &dwo_file->sections.abbrev
7078 : &dwarf2_per_objfile->abbrev);
7079
7080 if (dwarf_read_debug)
7081 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
7082 get_section_name (section),
7083 get_section_file_name (abbrev_section));
7084
7085 dwarf2_read_section (objfile, section);
7086 info_ptr = section->buffer;
7087
7088 if (info_ptr == NULL)
7089 return;
7090
7091 /* We can't set abfd until now because the section may be empty or
7092 not present, in which case the bfd is unknown. */
7093 abfd = get_section_bfd_owner (section);
7094
7095 /* We don't use init_cutu_and_read_dies_simple, or some such, here
7096 because we don't need to read any dies: the signature is in the
7097 header. */
7098
7099 end_ptr = info_ptr + section->size;
7100 while (info_ptr < end_ptr)
7101 {
7102 struct signatured_type *sig_type;
7103 struct dwo_unit *dwo_tu;
7104 void **slot;
7105 const gdb_byte *ptr = info_ptr;
7106 struct comp_unit_head header;
7107 unsigned int length;
7108
7109 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
7110
7111 /* Initialize it due to a false compiler warning. */
7112 header.signature = -1;
7113 header.type_cu_offset_in_tu = (cu_offset) -1;
7114
7115 /* We need to read the type's signature in order to build the hash
7116 table, but we don't need anything else just yet. */
7117
7118 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
7119 abbrev_section, ptr, section_kind);
7120
7121 length = get_cu_length (&header);
7122
7123 /* Skip dummy type units. */
7124 if (ptr >= info_ptr + length
7125 || peek_abbrev_code (abfd, ptr) == 0
7126 || header.unit_type != DW_UT_type)
7127 {
7128 info_ptr += length;
7129 continue;
7130 }
7131
7132 if (types_htab == NULL)
7133 {
7134 if (dwo_file)
7135 types_htab = allocate_dwo_unit_table (objfile);
7136 else
7137 types_htab = allocate_signatured_type_table (objfile);
7138 }
7139
7140 if (dwo_file)
7141 {
7142 sig_type = NULL;
7143 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7144 struct dwo_unit);
7145 dwo_tu->dwo_file = dwo_file;
7146 dwo_tu->signature = header.signature;
7147 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
7148 dwo_tu->section = section;
7149 dwo_tu->sect_off = sect_off;
7150 dwo_tu->length = length;
7151 }
7152 else
7153 {
7154 /* N.B.: type_offset is not usable if this type uses a DWO file.
7155 The real type_offset is in the DWO file. */
7156 dwo_tu = NULL;
7157 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7158 struct signatured_type);
7159 sig_type->signature = header.signature;
7160 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
7161 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7162 sig_type->per_cu.is_debug_types = 1;
7163 sig_type->per_cu.section = section;
7164 sig_type->per_cu.sect_off = sect_off;
7165 sig_type->per_cu.length = length;
7166 }
7167
7168 slot = htab_find_slot (types_htab,
7169 dwo_file ? (void*) dwo_tu : (void *) sig_type,
7170 INSERT);
7171 gdb_assert (slot != NULL);
7172 if (*slot != NULL)
7173 {
7174 sect_offset dup_sect_off;
7175
7176 if (dwo_file)
7177 {
7178 const struct dwo_unit *dup_tu
7179 = (const struct dwo_unit *) *slot;
7180
7181 dup_sect_off = dup_tu->sect_off;
7182 }
7183 else
7184 {
7185 const struct signatured_type *dup_tu
7186 = (const struct signatured_type *) *slot;
7187
7188 dup_sect_off = dup_tu->per_cu.sect_off;
7189 }
7190
7191 complaint (&symfile_complaints,
7192 _("debug type entry at offset %s is duplicate to"
7193 " the entry at offset %s, signature %s"),
7194 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
7195 hex_string (header.signature));
7196 }
7197 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
7198
7199 if (dwarf_read_debug > 1)
7200 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
7201 sect_offset_str (sect_off),
7202 hex_string (header.signature));
7203
7204 info_ptr += length;
7205 }
7206 }
7207
7208 /* Create the hash table of all entries in the .debug_types
7209 (or .debug_types.dwo) section(s).
7210 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7211 otherwise it is NULL.
7212
7213 The result is a pointer to the hash table or NULL if there are no types.
7214
7215 Note: This function processes DWO files only, not DWP files. */
7216
7217 static void
7218 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7219 struct dwo_file *dwo_file,
7220 VEC (dwarf2_section_info_def) *types,
7221 htab_t &types_htab)
7222 {
7223 int ix;
7224 struct dwarf2_section_info *section;
7225
7226 if (VEC_empty (dwarf2_section_info_def, types))
7227 return;
7228
7229 for (ix = 0;
7230 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7231 ++ix)
7232 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
7233 types_htab, rcuh_kind::TYPE);
7234 }
7235
7236 /* Create the hash table of all entries in the .debug_types section,
7237 and initialize all_type_units.
7238 The result is zero if there is an error (e.g. missing .debug_types section),
7239 otherwise non-zero. */
7240
7241 static int
7242 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7243 {
7244 htab_t types_htab = NULL;
7245 struct signatured_type **iter;
7246
7247 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
7248 &dwarf2_per_objfile->info, types_htab,
7249 rcuh_kind::COMPILE);
7250 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
7251 dwarf2_per_objfile->types, types_htab);
7252 if (types_htab == NULL)
7253 {
7254 dwarf2_per_objfile->signatured_types = NULL;
7255 return 0;
7256 }
7257
7258 dwarf2_per_objfile->signatured_types = types_htab;
7259
7260 dwarf2_per_objfile->n_type_units
7261 = dwarf2_per_objfile->n_allocated_type_units
7262 = htab_elements (types_htab);
7263 dwarf2_per_objfile->all_type_units =
7264 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
7265 iter = &dwarf2_per_objfile->all_type_units[0];
7266 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7267 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7268 == dwarf2_per_objfile->n_type_units);
7269
7270 return 1;
7271 }
7272
7273 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7274 If SLOT is non-NULL, it is the entry to use in the hash table.
7275 Otherwise we find one. */
7276
7277 static struct signatured_type *
7278 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
7279 void **slot)
7280 {
7281 struct objfile *objfile = dwarf2_per_objfile->objfile;
7282 int n_type_units = dwarf2_per_objfile->n_type_units;
7283 struct signatured_type *sig_type;
7284
7285 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7286 ++n_type_units;
7287 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7288 {
7289 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7290 dwarf2_per_objfile->n_allocated_type_units = 1;
7291 dwarf2_per_objfile->n_allocated_type_units *= 2;
7292 dwarf2_per_objfile->all_type_units
7293 = XRESIZEVEC (struct signatured_type *,
7294 dwarf2_per_objfile->all_type_units,
7295 dwarf2_per_objfile->n_allocated_type_units);
7296 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7297 }
7298 dwarf2_per_objfile->n_type_units = n_type_units;
7299
7300 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7301 struct signatured_type);
7302 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7303 sig_type->signature = sig;
7304 sig_type->per_cu.is_debug_types = 1;
7305 if (dwarf2_per_objfile->using_index)
7306 {
7307 sig_type->per_cu.v.quick =
7308 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7309 struct dwarf2_per_cu_quick_data);
7310 }
7311
7312 if (slot == NULL)
7313 {
7314 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7315 sig_type, INSERT);
7316 }
7317 gdb_assert (*slot == NULL);
7318 *slot = sig_type;
7319 /* The rest of sig_type must be filled in by the caller. */
7320 return sig_type;
7321 }
7322
7323 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7324 Fill in SIG_ENTRY with DWO_ENTRY. */
7325
7326 static void
7327 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
7328 struct signatured_type *sig_entry,
7329 struct dwo_unit *dwo_entry)
7330 {
7331 /* Make sure we're not clobbering something we don't expect to. */
7332 gdb_assert (! sig_entry->per_cu.queued);
7333 gdb_assert (sig_entry->per_cu.cu == NULL);
7334 if (dwarf2_per_objfile->using_index)
7335 {
7336 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7337 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7338 }
7339 else
7340 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7341 gdb_assert (sig_entry->signature == dwo_entry->signature);
7342 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7343 gdb_assert (sig_entry->type_unit_group == NULL);
7344 gdb_assert (sig_entry->dwo_unit == NULL);
7345
7346 sig_entry->per_cu.section = dwo_entry->section;
7347 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7348 sig_entry->per_cu.length = dwo_entry->length;
7349 sig_entry->per_cu.reading_dwo_directly = 1;
7350 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7351 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7352 sig_entry->dwo_unit = dwo_entry;
7353 }
7354
7355 /* Subroutine of lookup_signatured_type.
7356 If we haven't read the TU yet, create the signatured_type data structure
7357 for a TU to be read in directly from a DWO file, bypassing the stub.
7358 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7359 using .gdb_index, then when reading a CU we want to stay in the DWO file
7360 containing that CU. Otherwise we could end up reading several other DWO
7361 files (due to comdat folding) to process the transitive closure of all the
7362 mentioned TUs, and that can be slow. The current DWO file will have every
7363 type signature that it needs.
7364 We only do this for .gdb_index because in the psymtab case we already have
7365 to read all the DWOs to build the type unit groups. */
7366
7367 static struct signatured_type *
7368 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7369 {
7370 struct dwarf2_per_objfile *dwarf2_per_objfile
7371 = cu->per_cu->dwarf2_per_objfile;
7372 struct objfile *objfile = dwarf2_per_objfile->objfile;
7373 struct dwo_file *dwo_file;
7374 struct dwo_unit find_dwo_entry, *dwo_entry;
7375 struct signatured_type find_sig_entry, *sig_entry;
7376 void **slot;
7377
7378 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7379
7380 /* If TU skeletons have been removed then we may not have read in any
7381 TUs yet. */
7382 if (dwarf2_per_objfile->signatured_types == NULL)
7383 {
7384 dwarf2_per_objfile->signatured_types
7385 = allocate_signatured_type_table (objfile);
7386 }
7387
7388 /* We only ever need to read in one copy of a signatured type.
7389 Use the global signatured_types array to do our own comdat-folding
7390 of types. If this is the first time we're reading this TU, and
7391 the TU has an entry in .gdb_index, replace the recorded data from
7392 .gdb_index with this TU. */
7393
7394 find_sig_entry.signature = sig;
7395 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7396 &find_sig_entry, INSERT);
7397 sig_entry = (struct signatured_type *) *slot;
7398
7399 /* We can get here with the TU already read, *or* in the process of being
7400 read. Don't reassign the global entry to point to this DWO if that's
7401 the case. Also note that if the TU is already being read, it may not
7402 have come from a DWO, the program may be a mix of Fission-compiled
7403 code and non-Fission-compiled code. */
7404
7405 /* Have we already tried to read this TU?
7406 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7407 needn't exist in the global table yet). */
7408 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7409 return sig_entry;
7410
7411 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7412 dwo_unit of the TU itself. */
7413 dwo_file = cu->dwo_unit->dwo_file;
7414
7415 /* Ok, this is the first time we're reading this TU. */
7416 if (dwo_file->tus == NULL)
7417 return NULL;
7418 find_dwo_entry.signature = sig;
7419 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7420 if (dwo_entry == NULL)
7421 return NULL;
7422
7423 /* If the global table doesn't have an entry for this TU, add one. */
7424 if (sig_entry == NULL)
7425 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7426
7427 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7428 sig_entry->per_cu.tu_read = 1;
7429 return sig_entry;
7430 }
7431
7432 /* Subroutine of lookup_signatured_type.
7433 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7434 then try the DWP file. If the TU stub (skeleton) has been removed then
7435 it won't be in .gdb_index. */
7436
7437 static struct signatured_type *
7438 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7439 {
7440 struct dwarf2_per_objfile *dwarf2_per_objfile
7441 = cu->per_cu->dwarf2_per_objfile;
7442 struct objfile *objfile = dwarf2_per_objfile->objfile;
7443 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7444 struct dwo_unit *dwo_entry;
7445 struct signatured_type find_sig_entry, *sig_entry;
7446 void **slot;
7447
7448 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7449 gdb_assert (dwp_file != NULL);
7450
7451 /* If TU skeletons have been removed then we may not have read in any
7452 TUs yet. */
7453 if (dwarf2_per_objfile->signatured_types == NULL)
7454 {
7455 dwarf2_per_objfile->signatured_types
7456 = allocate_signatured_type_table (objfile);
7457 }
7458
7459 find_sig_entry.signature = sig;
7460 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7461 &find_sig_entry, INSERT);
7462 sig_entry = (struct signatured_type *) *slot;
7463
7464 /* Have we already tried to read this TU?
7465 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7466 needn't exist in the global table yet). */
7467 if (sig_entry != NULL)
7468 return sig_entry;
7469
7470 if (dwp_file->tus == NULL)
7471 return NULL;
7472 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7473 sig, 1 /* is_debug_types */);
7474 if (dwo_entry == NULL)
7475 return NULL;
7476
7477 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7478 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7479
7480 return sig_entry;
7481 }
7482
7483 /* Lookup a signature based type for DW_FORM_ref_sig8.
7484 Returns NULL if signature SIG is not present in the table.
7485 It is up to the caller to complain about this. */
7486
7487 static struct signatured_type *
7488 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7489 {
7490 struct dwarf2_per_objfile *dwarf2_per_objfile
7491 = cu->per_cu->dwarf2_per_objfile;
7492
7493 if (cu->dwo_unit
7494 && dwarf2_per_objfile->using_index)
7495 {
7496 /* We're in a DWO/DWP file, and we're using .gdb_index.
7497 These cases require special processing. */
7498 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7499 return lookup_dwo_signatured_type (cu, sig);
7500 else
7501 return lookup_dwp_signatured_type (cu, sig);
7502 }
7503 else
7504 {
7505 struct signatured_type find_entry, *entry;
7506
7507 if (dwarf2_per_objfile->signatured_types == NULL)
7508 return NULL;
7509 find_entry.signature = sig;
7510 entry = ((struct signatured_type *)
7511 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7512 return entry;
7513 }
7514 }
7515 \f
7516 /* Low level DIE reading support. */
7517
7518 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7519
7520 static void
7521 init_cu_die_reader (struct die_reader_specs *reader,
7522 struct dwarf2_cu *cu,
7523 struct dwarf2_section_info *section,
7524 struct dwo_file *dwo_file,
7525 struct abbrev_table *abbrev_table)
7526 {
7527 gdb_assert (section->readin && section->buffer != NULL);
7528 reader->abfd = get_section_bfd_owner (section);
7529 reader->cu = cu;
7530 reader->dwo_file = dwo_file;
7531 reader->die_section = section;
7532 reader->buffer = section->buffer;
7533 reader->buffer_end = section->buffer + section->size;
7534 reader->comp_dir = NULL;
7535 reader->abbrev_table = abbrev_table;
7536 }
7537
7538 /* Subroutine of init_cutu_and_read_dies to simplify it.
7539 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7540 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7541 already.
7542
7543 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7544 from it to the DIE in the DWO. If NULL we are skipping the stub.
7545 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7546 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7547 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7548 STUB_COMP_DIR may be non-NULL.
7549 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7550 are filled in with the info of the DIE from the DWO file.
7551 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7552 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7553 kept around for at least as long as *RESULT_READER.
7554
7555 The result is non-zero if a valid (non-dummy) DIE was found. */
7556
7557 static int
7558 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7559 struct dwo_unit *dwo_unit,
7560 struct die_info *stub_comp_unit_die,
7561 const char *stub_comp_dir,
7562 struct die_reader_specs *result_reader,
7563 const gdb_byte **result_info_ptr,
7564 struct die_info **result_comp_unit_die,
7565 int *result_has_children,
7566 abbrev_table_up *result_dwo_abbrev_table)
7567 {
7568 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7569 struct objfile *objfile = dwarf2_per_objfile->objfile;
7570 struct dwarf2_cu *cu = this_cu->cu;
7571 bfd *abfd;
7572 const gdb_byte *begin_info_ptr, *info_ptr;
7573 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7574 int i,num_extra_attrs;
7575 struct dwarf2_section_info *dwo_abbrev_section;
7576 struct attribute *attr;
7577 struct die_info *comp_unit_die;
7578
7579 /* At most one of these may be provided. */
7580 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7581
7582 /* These attributes aren't processed until later:
7583 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7584 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7585 referenced later. However, these attributes are found in the stub
7586 which we won't have later. In order to not impose this complication
7587 on the rest of the code, we read them here and copy them to the
7588 DWO CU/TU die. */
7589
7590 stmt_list = NULL;
7591 low_pc = NULL;
7592 high_pc = NULL;
7593 ranges = NULL;
7594 comp_dir = NULL;
7595
7596 if (stub_comp_unit_die != NULL)
7597 {
7598 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7599 DWO file. */
7600 if (! this_cu->is_debug_types)
7601 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7602 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7603 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7604 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7605 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7606
7607 /* There should be a DW_AT_addr_base attribute here (if needed).
7608 We need the value before we can process DW_FORM_GNU_addr_index. */
7609 cu->addr_base = 0;
7610 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7611 if (attr)
7612 cu->addr_base = DW_UNSND (attr);
7613
7614 /* There should be a DW_AT_ranges_base attribute here (if needed).
7615 We need the value before we can process DW_AT_ranges. */
7616 cu->ranges_base = 0;
7617 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7618 if (attr)
7619 cu->ranges_base = DW_UNSND (attr);
7620 }
7621 else if (stub_comp_dir != NULL)
7622 {
7623 /* Reconstruct the comp_dir attribute to simplify the code below. */
7624 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7625 comp_dir->name = DW_AT_comp_dir;
7626 comp_dir->form = DW_FORM_string;
7627 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7628 DW_STRING (comp_dir) = stub_comp_dir;
7629 }
7630
7631 /* Set up for reading the DWO CU/TU. */
7632 cu->dwo_unit = dwo_unit;
7633 dwarf2_section_info *section = dwo_unit->section;
7634 dwarf2_read_section (objfile, section);
7635 abfd = get_section_bfd_owner (section);
7636 begin_info_ptr = info_ptr = (section->buffer
7637 + to_underlying (dwo_unit->sect_off));
7638 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7639
7640 if (this_cu->is_debug_types)
7641 {
7642 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7643
7644 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7645 &cu->header, section,
7646 dwo_abbrev_section,
7647 info_ptr, rcuh_kind::TYPE);
7648 /* This is not an assert because it can be caused by bad debug info. */
7649 if (sig_type->signature != cu->header.signature)
7650 {
7651 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7652 " TU at offset %s [in module %s]"),
7653 hex_string (sig_type->signature),
7654 hex_string (cu->header.signature),
7655 sect_offset_str (dwo_unit->sect_off),
7656 bfd_get_filename (abfd));
7657 }
7658 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7659 /* For DWOs coming from DWP files, we don't know the CU length
7660 nor the type's offset in the TU until now. */
7661 dwo_unit->length = get_cu_length (&cu->header);
7662 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7663
7664 /* Establish the type offset that can be used to lookup the type.
7665 For DWO files, we don't know it until now. */
7666 sig_type->type_offset_in_section
7667 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7668 }
7669 else
7670 {
7671 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7672 &cu->header, section,
7673 dwo_abbrev_section,
7674 info_ptr, rcuh_kind::COMPILE);
7675 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7676 /* For DWOs coming from DWP files, we don't know the CU length
7677 until now. */
7678 dwo_unit->length = get_cu_length (&cu->header);
7679 }
7680
7681 *result_dwo_abbrev_table
7682 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7683 cu->header.abbrev_sect_off);
7684 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7685 result_dwo_abbrev_table->get ());
7686
7687 /* Read in the die, but leave space to copy over the attributes
7688 from the stub. This has the benefit of simplifying the rest of
7689 the code - all the work to maintain the illusion of a single
7690 DW_TAG_{compile,type}_unit DIE is done here. */
7691 num_extra_attrs = ((stmt_list != NULL)
7692 + (low_pc != NULL)
7693 + (high_pc != NULL)
7694 + (ranges != NULL)
7695 + (comp_dir != NULL));
7696 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7697 result_has_children, num_extra_attrs);
7698
7699 /* Copy over the attributes from the stub to the DIE we just read in. */
7700 comp_unit_die = *result_comp_unit_die;
7701 i = comp_unit_die->num_attrs;
7702 if (stmt_list != NULL)
7703 comp_unit_die->attrs[i++] = *stmt_list;
7704 if (low_pc != NULL)
7705 comp_unit_die->attrs[i++] = *low_pc;
7706 if (high_pc != NULL)
7707 comp_unit_die->attrs[i++] = *high_pc;
7708 if (ranges != NULL)
7709 comp_unit_die->attrs[i++] = *ranges;
7710 if (comp_dir != NULL)
7711 comp_unit_die->attrs[i++] = *comp_dir;
7712 comp_unit_die->num_attrs += num_extra_attrs;
7713
7714 if (dwarf_die_debug)
7715 {
7716 fprintf_unfiltered (gdb_stdlog,
7717 "Read die from %s@0x%x of %s:\n",
7718 get_section_name (section),
7719 (unsigned) (begin_info_ptr - section->buffer),
7720 bfd_get_filename (abfd));
7721 dump_die (comp_unit_die, dwarf_die_debug);
7722 }
7723
7724 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7725 TUs by skipping the stub and going directly to the entry in the DWO file.
7726 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7727 to get it via circuitous means. Blech. */
7728 if (comp_dir != NULL)
7729 result_reader->comp_dir = DW_STRING (comp_dir);
7730
7731 /* Skip dummy compilation units. */
7732 if (info_ptr >= begin_info_ptr + dwo_unit->length
7733 || peek_abbrev_code (abfd, info_ptr) == 0)
7734 return 0;
7735
7736 *result_info_ptr = info_ptr;
7737 return 1;
7738 }
7739
7740 /* Subroutine of init_cutu_and_read_dies to simplify it.
7741 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7742 Returns NULL if the specified DWO unit cannot be found. */
7743
7744 static struct dwo_unit *
7745 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7746 struct die_info *comp_unit_die)
7747 {
7748 struct dwarf2_cu *cu = this_cu->cu;
7749 ULONGEST signature;
7750 struct dwo_unit *dwo_unit;
7751 const char *comp_dir, *dwo_name;
7752
7753 gdb_assert (cu != NULL);
7754
7755 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7756 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7757 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7758
7759 if (this_cu->is_debug_types)
7760 {
7761 struct signatured_type *sig_type;
7762
7763 /* Since this_cu is the first member of struct signatured_type,
7764 we can go from a pointer to one to a pointer to the other. */
7765 sig_type = (struct signatured_type *) this_cu;
7766 signature = sig_type->signature;
7767 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7768 }
7769 else
7770 {
7771 struct attribute *attr;
7772
7773 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7774 if (! attr)
7775 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7776 " [in module %s]"),
7777 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7778 signature = DW_UNSND (attr);
7779 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7780 signature);
7781 }
7782
7783 return dwo_unit;
7784 }
7785
7786 /* Subroutine of init_cutu_and_read_dies to simplify it.
7787 See it for a description of the parameters.
7788 Read a TU directly from a DWO file, bypassing the stub. */
7789
7790 static void
7791 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7792 int use_existing_cu, int keep,
7793 die_reader_func_ftype *die_reader_func,
7794 void *data)
7795 {
7796 std::unique_ptr<dwarf2_cu> new_cu;
7797 struct signatured_type *sig_type;
7798 struct die_reader_specs reader;
7799 const gdb_byte *info_ptr;
7800 struct die_info *comp_unit_die;
7801 int has_children;
7802 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7803
7804 /* Verify we can do the following downcast, and that we have the
7805 data we need. */
7806 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7807 sig_type = (struct signatured_type *) this_cu;
7808 gdb_assert (sig_type->dwo_unit != NULL);
7809
7810 if (use_existing_cu && this_cu->cu != NULL)
7811 {
7812 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7813 /* There's no need to do the rereading_dwo_cu handling that
7814 init_cutu_and_read_dies does since we don't read the stub. */
7815 }
7816 else
7817 {
7818 /* If !use_existing_cu, this_cu->cu must be NULL. */
7819 gdb_assert (this_cu->cu == NULL);
7820 new_cu.reset (new dwarf2_cu (this_cu));
7821 }
7822
7823 /* A future optimization, if needed, would be to use an existing
7824 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7825 could share abbrev tables. */
7826
7827 /* The abbreviation table used by READER, this must live at least as long as
7828 READER. */
7829 abbrev_table_up dwo_abbrev_table;
7830
7831 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7832 NULL /* stub_comp_unit_die */,
7833 sig_type->dwo_unit->dwo_file->comp_dir,
7834 &reader, &info_ptr,
7835 &comp_unit_die, &has_children,
7836 &dwo_abbrev_table) == 0)
7837 {
7838 /* Dummy die. */
7839 return;
7840 }
7841
7842 /* All the "real" work is done here. */
7843 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7844
7845 /* This duplicates the code in init_cutu_and_read_dies,
7846 but the alternative is making the latter more complex.
7847 This function is only for the special case of using DWO files directly:
7848 no point in overly complicating the general case just to handle this. */
7849 if (new_cu != NULL && keep)
7850 {
7851 /* Link this CU into read_in_chain. */
7852 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7853 dwarf2_per_objfile->read_in_chain = this_cu;
7854 /* The chain owns it now. */
7855 new_cu.release ();
7856 }
7857 }
7858
7859 /* Initialize a CU (or TU) and read its DIEs.
7860 If the CU defers to a DWO file, read the DWO file as well.
7861
7862 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7863 Otherwise the table specified in the comp unit header is read in and used.
7864 This is an optimization for when we already have the abbrev table.
7865
7866 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7867 Otherwise, a new CU is allocated with xmalloc.
7868
7869 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7870 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7871
7872 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7873 linker) then DIE_READER_FUNC will not get called. */
7874
7875 static void
7876 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7877 struct abbrev_table *abbrev_table,
7878 int use_existing_cu, int keep,
7879 die_reader_func_ftype *die_reader_func,
7880 void *data)
7881 {
7882 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7883 struct objfile *objfile = dwarf2_per_objfile->objfile;
7884 struct dwarf2_section_info *section = this_cu->section;
7885 bfd *abfd = get_section_bfd_owner (section);
7886 struct dwarf2_cu *cu;
7887 const gdb_byte *begin_info_ptr, *info_ptr;
7888 struct die_reader_specs reader;
7889 struct die_info *comp_unit_die;
7890 int has_children;
7891 struct attribute *attr;
7892 struct signatured_type *sig_type = NULL;
7893 struct dwarf2_section_info *abbrev_section;
7894 /* Non-zero if CU currently points to a DWO file and we need to
7895 reread it. When this happens we need to reread the skeleton die
7896 before we can reread the DWO file (this only applies to CUs, not TUs). */
7897 int rereading_dwo_cu = 0;
7898
7899 if (dwarf_die_debug)
7900 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7901 this_cu->is_debug_types ? "type" : "comp",
7902 sect_offset_str (this_cu->sect_off));
7903
7904 if (use_existing_cu)
7905 gdb_assert (keep);
7906
7907 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7908 file (instead of going through the stub), short-circuit all of this. */
7909 if (this_cu->reading_dwo_directly)
7910 {
7911 /* Narrow down the scope of possibilities to have to understand. */
7912 gdb_assert (this_cu->is_debug_types);
7913 gdb_assert (abbrev_table == NULL);
7914 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7915 die_reader_func, data);
7916 return;
7917 }
7918
7919 /* This is cheap if the section is already read in. */
7920 dwarf2_read_section (objfile, section);
7921
7922 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7923
7924 abbrev_section = get_abbrev_section_for_cu (this_cu);
7925
7926 std::unique_ptr<dwarf2_cu> new_cu;
7927 if (use_existing_cu && this_cu->cu != NULL)
7928 {
7929 cu = this_cu->cu;
7930 /* If this CU is from a DWO file we need to start over, we need to
7931 refetch the attributes from the skeleton CU.
7932 This could be optimized by retrieving those attributes from when we
7933 were here the first time: the previous comp_unit_die was stored in
7934 comp_unit_obstack. But there's no data yet that we need this
7935 optimization. */
7936 if (cu->dwo_unit != NULL)
7937 rereading_dwo_cu = 1;
7938 }
7939 else
7940 {
7941 /* If !use_existing_cu, this_cu->cu must be NULL. */
7942 gdb_assert (this_cu->cu == NULL);
7943 new_cu.reset (new dwarf2_cu (this_cu));
7944 cu = new_cu.get ();
7945 }
7946
7947 /* Get the header. */
7948 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7949 {
7950 /* We already have the header, there's no need to read it in again. */
7951 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7952 }
7953 else
7954 {
7955 if (this_cu->is_debug_types)
7956 {
7957 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7958 &cu->header, section,
7959 abbrev_section, info_ptr,
7960 rcuh_kind::TYPE);
7961
7962 /* Since per_cu is the first member of struct signatured_type,
7963 we can go from a pointer to one to a pointer to the other. */
7964 sig_type = (struct signatured_type *) this_cu;
7965 gdb_assert (sig_type->signature == cu->header.signature);
7966 gdb_assert (sig_type->type_offset_in_tu
7967 == cu->header.type_cu_offset_in_tu);
7968 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7969
7970 /* LENGTH has not been set yet for type units if we're
7971 using .gdb_index. */
7972 this_cu->length = get_cu_length (&cu->header);
7973
7974 /* Establish the type offset that can be used to lookup the type. */
7975 sig_type->type_offset_in_section =
7976 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7977
7978 this_cu->dwarf_version = cu->header.version;
7979 }
7980 else
7981 {
7982 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7983 &cu->header, section,
7984 abbrev_section,
7985 info_ptr,
7986 rcuh_kind::COMPILE);
7987
7988 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7989 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7990 this_cu->dwarf_version = cu->header.version;
7991 }
7992 }
7993
7994 /* Skip dummy compilation units. */
7995 if (info_ptr >= begin_info_ptr + this_cu->length
7996 || peek_abbrev_code (abfd, info_ptr) == 0)
7997 return;
7998
7999 /* If we don't have them yet, read the abbrevs for this compilation unit.
8000 And if we need to read them now, make sure they're freed when we're
8001 done (own the table through ABBREV_TABLE_HOLDER). */
8002 abbrev_table_up abbrev_table_holder;
8003 if (abbrev_table != NULL)
8004 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
8005 else
8006 {
8007 abbrev_table_holder
8008 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8009 cu->header.abbrev_sect_off);
8010 abbrev_table = abbrev_table_holder.get ();
8011 }
8012
8013 /* Read the top level CU/TU die. */
8014 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
8015 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8016
8017 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
8018 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
8019 table from the DWO file and pass the ownership over to us. It will be
8020 referenced from READER, so we must make sure to free it after we're done
8021 with READER.
8022
8023 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
8024 DWO CU, that this test will fail (the attribute will not be present). */
8025 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
8026 abbrev_table_up dwo_abbrev_table;
8027 if (attr)
8028 {
8029 struct dwo_unit *dwo_unit;
8030 struct die_info *dwo_comp_unit_die;
8031
8032 if (has_children)
8033 {
8034 complaint (&symfile_complaints,
8035 _("compilation unit with DW_AT_GNU_dwo_name"
8036 " has children (offset %s) [in module %s]"),
8037 sect_offset_str (this_cu->sect_off),
8038 bfd_get_filename (abfd));
8039 }
8040 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
8041 if (dwo_unit != NULL)
8042 {
8043 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
8044 comp_unit_die, NULL,
8045 &reader, &info_ptr,
8046 &dwo_comp_unit_die, &has_children,
8047 &dwo_abbrev_table) == 0)
8048 {
8049 /* Dummy die. */
8050 return;
8051 }
8052 comp_unit_die = dwo_comp_unit_die;
8053 }
8054 else
8055 {
8056 /* Yikes, we couldn't find the rest of the DIE, we only have
8057 the stub. A complaint has already been logged. There's
8058 not much more we can do except pass on the stub DIE to
8059 die_reader_func. We don't want to throw an error on bad
8060 debug info. */
8061 }
8062 }
8063
8064 /* All of the above is setup for this call. Yikes. */
8065 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8066
8067 /* Done, clean up. */
8068 if (new_cu != NULL && keep)
8069 {
8070 /* Link this CU into read_in_chain. */
8071 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
8072 dwarf2_per_objfile->read_in_chain = this_cu;
8073 /* The chain owns it now. */
8074 new_cu.release ();
8075 }
8076 }
8077
8078 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
8079 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
8080 to have already done the lookup to find the DWO file).
8081
8082 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
8083 THIS_CU->is_debug_types, but nothing else.
8084
8085 We fill in THIS_CU->length.
8086
8087 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
8088 linker) then DIE_READER_FUNC will not get called.
8089
8090 THIS_CU->cu is always freed when done.
8091 This is done in order to not leave THIS_CU->cu in a state where we have
8092 to care whether it refers to the "main" CU or the DWO CU. */
8093
8094 static void
8095 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
8096 struct dwo_file *dwo_file,
8097 die_reader_func_ftype *die_reader_func,
8098 void *data)
8099 {
8100 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
8101 struct objfile *objfile = dwarf2_per_objfile->objfile;
8102 struct dwarf2_section_info *section = this_cu->section;
8103 bfd *abfd = get_section_bfd_owner (section);
8104 struct dwarf2_section_info *abbrev_section;
8105 const gdb_byte *begin_info_ptr, *info_ptr;
8106 struct die_reader_specs reader;
8107 struct die_info *comp_unit_die;
8108 int has_children;
8109
8110 if (dwarf_die_debug)
8111 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
8112 this_cu->is_debug_types ? "type" : "comp",
8113 sect_offset_str (this_cu->sect_off));
8114
8115 gdb_assert (this_cu->cu == NULL);
8116
8117 abbrev_section = (dwo_file != NULL
8118 ? &dwo_file->sections.abbrev
8119 : get_abbrev_section_for_cu (this_cu));
8120
8121 /* This is cheap if the section is already read in. */
8122 dwarf2_read_section (objfile, section);
8123
8124 struct dwarf2_cu cu (this_cu);
8125
8126 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
8127 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
8128 &cu.header, section,
8129 abbrev_section, info_ptr,
8130 (this_cu->is_debug_types
8131 ? rcuh_kind::TYPE
8132 : rcuh_kind::COMPILE));
8133
8134 this_cu->length = get_cu_length (&cu.header);
8135
8136 /* Skip dummy compilation units. */
8137 if (info_ptr >= begin_info_ptr + this_cu->length
8138 || peek_abbrev_code (abfd, info_ptr) == 0)
8139 return;
8140
8141 abbrev_table_up abbrev_table
8142 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8143 cu.header.abbrev_sect_off);
8144
8145 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
8146 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8147
8148 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8149 }
8150
8151 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
8152 does not lookup the specified DWO file.
8153 This cannot be used to read DWO files.
8154
8155 THIS_CU->cu is always freed when done.
8156 This is done in order to not leave THIS_CU->cu in a state where we have
8157 to care whether it refers to the "main" CU or the DWO CU.
8158 We can revisit this if the data shows there's a performance issue. */
8159
8160 static void
8161 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8162 die_reader_func_ftype *die_reader_func,
8163 void *data)
8164 {
8165 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
8166 }
8167 \f
8168 /* Type Unit Groups.
8169
8170 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8171 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8172 so that all types coming from the same compilation (.o file) are grouped
8173 together. A future step could be to put the types in the same symtab as
8174 the CU the types ultimately came from. */
8175
8176 static hashval_t
8177 hash_type_unit_group (const void *item)
8178 {
8179 const struct type_unit_group *tu_group
8180 = (const struct type_unit_group *) item;
8181
8182 return hash_stmt_list_entry (&tu_group->hash);
8183 }
8184
8185 static int
8186 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
8187 {
8188 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8189 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
8190
8191 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
8192 }
8193
8194 /* Allocate a hash table for type unit groups. */
8195
8196 static htab_t
8197 allocate_type_unit_groups_table (struct objfile *objfile)
8198 {
8199 return htab_create_alloc_ex (3,
8200 hash_type_unit_group,
8201 eq_type_unit_group,
8202 NULL,
8203 &objfile->objfile_obstack,
8204 hashtab_obstack_allocate,
8205 dummy_obstack_deallocate);
8206 }
8207
8208 /* Type units that don't have DW_AT_stmt_list are grouped into their own
8209 partial symtabs. We combine several TUs per psymtab to not let the size
8210 of any one psymtab grow too big. */
8211 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8212 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
8213
8214 /* Helper routine for get_type_unit_group.
8215 Create the type_unit_group object used to hold one or more TUs. */
8216
8217 static struct type_unit_group *
8218 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
8219 {
8220 struct dwarf2_per_objfile *dwarf2_per_objfile
8221 = cu->per_cu->dwarf2_per_objfile;
8222 struct objfile *objfile = dwarf2_per_objfile->objfile;
8223 struct dwarf2_per_cu_data *per_cu;
8224 struct type_unit_group *tu_group;
8225
8226 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8227 struct type_unit_group);
8228 per_cu = &tu_group->per_cu;
8229 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8230
8231 if (dwarf2_per_objfile->using_index)
8232 {
8233 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8234 struct dwarf2_per_cu_quick_data);
8235 }
8236 else
8237 {
8238 unsigned int line_offset = to_underlying (line_offset_struct);
8239 struct partial_symtab *pst;
8240 char *name;
8241
8242 /* Give the symtab a useful name for debug purposes. */
8243 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8244 name = xstrprintf ("<type_units_%d>",
8245 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8246 else
8247 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8248
8249 pst = create_partial_symtab (per_cu, name);
8250 pst->anonymous = 1;
8251
8252 xfree (name);
8253 }
8254
8255 tu_group->hash.dwo_unit = cu->dwo_unit;
8256 tu_group->hash.line_sect_off = line_offset_struct;
8257
8258 return tu_group;
8259 }
8260
8261 /* Look up the type_unit_group for type unit CU, and create it if necessary.
8262 STMT_LIST is a DW_AT_stmt_list attribute. */
8263
8264 static struct type_unit_group *
8265 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
8266 {
8267 struct dwarf2_per_objfile *dwarf2_per_objfile
8268 = cu->per_cu->dwarf2_per_objfile;
8269 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8270 struct type_unit_group *tu_group;
8271 void **slot;
8272 unsigned int line_offset;
8273 struct type_unit_group type_unit_group_for_lookup;
8274
8275 if (dwarf2_per_objfile->type_unit_groups == NULL)
8276 {
8277 dwarf2_per_objfile->type_unit_groups =
8278 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
8279 }
8280
8281 /* Do we need to create a new group, or can we use an existing one? */
8282
8283 if (stmt_list)
8284 {
8285 line_offset = DW_UNSND (stmt_list);
8286 ++tu_stats->nr_symtab_sharers;
8287 }
8288 else
8289 {
8290 /* Ugh, no stmt_list. Rare, but we have to handle it.
8291 We can do various things here like create one group per TU or
8292 spread them over multiple groups to split up the expansion work.
8293 To avoid worst case scenarios (too many groups or too large groups)
8294 we, umm, group them in bunches. */
8295 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8296 | (tu_stats->nr_stmt_less_type_units
8297 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8298 ++tu_stats->nr_stmt_less_type_units;
8299 }
8300
8301 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
8302 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
8303 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8304 &type_unit_group_for_lookup, INSERT);
8305 if (*slot != NULL)
8306 {
8307 tu_group = (struct type_unit_group *) *slot;
8308 gdb_assert (tu_group != NULL);
8309 }
8310 else
8311 {
8312 sect_offset line_offset_struct = (sect_offset) line_offset;
8313 tu_group = create_type_unit_group (cu, line_offset_struct);
8314 *slot = tu_group;
8315 ++tu_stats->nr_symtabs;
8316 }
8317
8318 return tu_group;
8319 }
8320 \f
8321 /* Partial symbol tables. */
8322
8323 /* Create a psymtab named NAME and assign it to PER_CU.
8324
8325 The caller must fill in the following details:
8326 dirname, textlow, texthigh. */
8327
8328 static struct partial_symtab *
8329 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8330 {
8331 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8332 struct partial_symtab *pst;
8333
8334 pst = start_psymtab_common (objfile, name, 0,
8335 objfile->global_psymbols,
8336 objfile->static_psymbols);
8337
8338 pst->psymtabs_addrmap_supported = 1;
8339
8340 /* This is the glue that links PST into GDB's symbol API. */
8341 pst->read_symtab_private = per_cu;
8342 pst->read_symtab = dwarf2_read_symtab;
8343 per_cu->v.psymtab = pst;
8344
8345 return pst;
8346 }
8347
8348 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8349 type. */
8350
8351 struct process_psymtab_comp_unit_data
8352 {
8353 /* True if we are reading a DW_TAG_partial_unit. */
8354
8355 int want_partial_unit;
8356
8357 /* The "pretend" language that is used if the CU doesn't declare a
8358 language. */
8359
8360 enum language pretend_language;
8361 };
8362
8363 /* die_reader_func for process_psymtab_comp_unit. */
8364
8365 static void
8366 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8367 const gdb_byte *info_ptr,
8368 struct die_info *comp_unit_die,
8369 int has_children,
8370 void *data)
8371 {
8372 struct dwarf2_cu *cu = reader->cu;
8373 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8374 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8375 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8376 CORE_ADDR baseaddr;
8377 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8378 struct partial_symtab *pst;
8379 enum pc_bounds_kind cu_bounds_kind;
8380 const char *filename;
8381 struct process_psymtab_comp_unit_data *info
8382 = (struct process_psymtab_comp_unit_data *) data;
8383
8384 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8385 return;
8386
8387 gdb_assert (! per_cu->is_debug_types);
8388
8389 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8390
8391 cu->list_in_scope = &file_symbols;
8392
8393 /* Allocate a new partial symbol table structure. */
8394 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8395 if (filename == NULL)
8396 filename = "";
8397
8398 pst = create_partial_symtab (per_cu, filename);
8399
8400 /* This must be done before calling dwarf2_build_include_psymtabs. */
8401 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8402
8403 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8404
8405 dwarf2_find_base_address (comp_unit_die, cu);
8406
8407 /* Possibly set the default values of LOWPC and HIGHPC from
8408 `DW_AT_ranges'. */
8409 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8410 &best_highpc, cu, pst);
8411 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8412 /* Store the contiguous range if it is not empty; it can be empty for
8413 CUs with no code. */
8414 addrmap_set_empty (objfile->psymtabs_addrmap,
8415 gdbarch_adjust_dwarf2_addr (gdbarch,
8416 best_lowpc + baseaddr),
8417 gdbarch_adjust_dwarf2_addr (gdbarch,
8418 best_highpc + baseaddr) - 1,
8419 pst);
8420
8421 /* Check if comp unit has_children.
8422 If so, read the rest of the partial symbols from this comp unit.
8423 If not, there's no more debug_info for this comp unit. */
8424 if (has_children)
8425 {
8426 struct partial_die_info *first_die;
8427 CORE_ADDR lowpc, highpc;
8428
8429 lowpc = ((CORE_ADDR) -1);
8430 highpc = ((CORE_ADDR) 0);
8431
8432 first_die = load_partial_dies (reader, info_ptr, 1);
8433
8434 scan_partial_symbols (first_die, &lowpc, &highpc,
8435 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8436
8437 /* If we didn't find a lowpc, set it to highpc to avoid
8438 complaints from `maint check'. */
8439 if (lowpc == ((CORE_ADDR) -1))
8440 lowpc = highpc;
8441
8442 /* If the compilation unit didn't have an explicit address range,
8443 then use the information extracted from its child dies. */
8444 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8445 {
8446 best_lowpc = lowpc;
8447 best_highpc = highpc;
8448 }
8449 }
8450 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8451 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
8452
8453 end_psymtab_common (objfile, pst);
8454
8455 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8456 {
8457 int i;
8458 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8459 struct dwarf2_per_cu_data *iter;
8460
8461 /* Fill in 'dependencies' here; we fill in 'users' in a
8462 post-pass. */
8463 pst->number_of_dependencies = len;
8464 pst->dependencies =
8465 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8466 for (i = 0;
8467 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8468 i, iter);
8469 ++i)
8470 pst->dependencies[i] = iter->v.psymtab;
8471
8472 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8473 }
8474
8475 /* Get the list of files included in the current compilation unit,
8476 and build a psymtab for each of them. */
8477 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8478
8479 if (dwarf_read_debug)
8480 {
8481 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8482
8483 fprintf_unfiltered (gdb_stdlog,
8484 "Psymtab for %s unit @%s: %s - %s"
8485 ", %d global, %d static syms\n",
8486 per_cu->is_debug_types ? "type" : "comp",
8487 sect_offset_str (per_cu->sect_off),
8488 paddress (gdbarch, pst->textlow),
8489 paddress (gdbarch, pst->texthigh),
8490 pst->n_global_syms, pst->n_static_syms);
8491 }
8492 }
8493
8494 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8495 Process compilation unit THIS_CU for a psymtab. */
8496
8497 static void
8498 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8499 int want_partial_unit,
8500 enum language pretend_language)
8501 {
8502 /* If this compilation unit was already read in, free the
8503 cached copy in order to read it in again. This is
8504 necessary because we skipped some symbols when we first
8505 read in the compilation unit (see load_partial_dies).
8506 This problem could be avoided, but the benefit is unclear. */
8507 if (this_cu->cu != NULL)
8508 free_one_cached_comp_unit (this_cu);
8509
8510 if (this_cu->is_debug_types)
8511 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8512 NULL);
8513 else
8514 {
8515 process_psymtab_comp_unit_data info;
8516 info.want_partial_unit = want_partial_unit;
8517 info.pretend_language = pretend_language;
8518 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8519 process_psymtab_comp_unit_reader, &info);
8520 }
8521
8522 /* Age out any secondary CUs. */
8523 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8524 }
8525
8526 /* Reader function for build_type_psymtabs. */
8527
8528 static void
8529 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8530 const gdb_byte *info_ptr,
8531 struct die_info *type_unit_die,
8532 int has_children,
8533 void *data)
8534 {
8535 struct dwarf2_per_objfile *dwarf2_per_objfile
8536 = reader->cu->per_cu->dwarf2_per_objfile;
8537 struct objfile *objfile = dwarf2_per_objfile->objfile;
8538 struct dwarf2_cu *cu = reader->cu;
8539 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8540 struct signatured_type *sig_type;
8541 struct type_unit_group *tu_group;
8542 struct attribute *attr;
8543 struct partial_die_info *first_die;
8544 CORE_ADDR lowpc, highpc;
8545 struct partial_symtab *pst;
8546
8547 gdb_assert (data == NULL);
8548 gdb_assert (per_cu->is_debug_types);
8549 sig_type = (struct signatured_type *) per_cu;
8550
8551 if (! has_children)
8552 return;
8553
8554 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8555 tu_group = get_type_unit_group (cu, attr);
8556
8557 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8558
8559 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8560 cu->list_in_scope = &file_symbols;
8561 pst = create_partial_symtab (per_cu, "");
8562 pst->anonymous = 1;
8563
8564 first_die = load_partial_dies (reader, info_ptr, 1);
8565
8566 lowpc = (CORE_ADDR) -1;
8567 highpc = (CORE_ADDR) 0;
8568 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8569
8570 end_psymtab_common (objfile, pst);
8571 }
8572
8573 /* Struct used to sort TUs by their abbreviation table offset. */
8574
8575 struct tu_abbrev_offset
8576 {
8577 struct signatured_type *sig_type;
8578 sect_offset abbrev_offset;
8579 };
8580
8581 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
8582
8583 static int
8584 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
8585 {
8586 const struct tu_abbrev_offset * const *a
8587 = (const struct tu_abbrev_offset * const*) ap;
8588 const struct tu_abbrev_offset * const *b
8589 = (const struct tu_abbrev_offset * const*) bp;
8590 sect_offset aoff = (*a)->abbrev_offset;
8591 sect_offset boff = (*b)->abbrev_offset;
8592
8593 return (aoff > boff) - (aoff < boff);
8594 }
8595
8596 /* Efficiently read all the type units.
8597 This does the bulk of the work for build_type_psymtabs.
8598
8599 The efficiency is because we sort TUs by the abbrev table they use and
8600 only read each abbrev table once. In one program there are 200K TUs
8601 sharing 8K abbrev tables.
8602
8603 The main purpose of this function is to support building the
8604 dwarf2_per_objfile->type_unit_groups table.
8605 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8606 can collapse the search space by grouping them by stmt_list.
8607 The savings can be significant, in the same program from above the 200K TUs
8608 share 8K stmt_list tables.
8609
8610 FUNC is expected to call get_type_unit_group, which will create the
8611 struct type_unit_group if necessary and add it to
8612 dwarf2_per_objfile->type_unit_groups. */
8613
8614 static void
8615 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8616 {
8617 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8618 struct cleanup *cleanups;
8619 abbrev_table_up abbrev_table;
8620 sect_offset abbrev_offset;
8621 struct tu_abbrev_offset *sorted_by_abbrev;
8622 int i;
8623
8624 /* It's up to the caller to not call us multiple times. */
8625 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8626
8627 if (dwarf2_per_objfile->n_type_units == 0)
8628 return;
8629
8630 /* TUs typically share abbrev tables, and there can be way more TUs than
8631 abbrev tables. Sort by abbrev table to reduce the number of times we
8632 read each abbrev table in.
8633 Alternatives are to punt or to maintain a cache of abbrev tables.
8634 This is simpler and efficient enough for now.
8635
8636 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8637 symtab to use). Typically TUs with the same abbrev offset have the same
8638 stmt_list value too so in practice this should work well.
8639
8640 The basic algorithm here is:
8641
8642 sort TUs by abbrev table
8643 for each TU with same abbrev table:
8644 read abbrev table if first user
8645 read TU top level DIE
8646 [IWBN if DWO skeletons had DW_AT_stmt_list]
8647 call FUNC */
8648
8649 if (dwarf_read_debug)
8650 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8651
8652 /* Sort in a separate table to maintain the order of all_type_units
8653 for .gdb_index: TU indices directly index all_type_units. */
8654 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
8655 dwarf2_per_objfile->n_type_units);
8656 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8657 {
8658 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8659
8660 sorted_by_abbrev[i].sig_type = sig_type;
8661 sorted_by_abbrev[i].abbrev_offset =
8662 read_abbrev_offset (dwarf2_per_objfile,
8663 sig_type->per_cu.section,
8664 sig_type->per_cu.sect_off);
8665 }
8666 cleanups = make_cleanup (xfree, sorted_by_abbrev);
8667 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
8668 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
8669
8670 abbrev_offset = (sect_offset) ~(unsigned) 0;
8671
8672 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8673 {
8674 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8675
8676 /* Switch to the next abbrev table if necessary. */
8677 if (abbrev_table == NULL
8678 || tu->abbrev_offset != abbrev_offset)
8679 {
8680 abbrev_offset = tu->abbrev_offset;
8681 abbrev_table =
8682 abbrev_table_read_table (dwarf2_per_objfile,
8683 &dwarf2_per_objfile->abbrev,
8684 abbrev_offset);
8685 ++tu_stats->nr_uniq_abbrev_tables;
8686 }
8687
8688 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table.get (),
8689 0, 0, build_type_psymtabs_reader, NULL);
8690 }
8691
8692 do_cleanups (cleanups);
8693 }
8694
8695 /* Print collected type unit statistics. */
8696
8697 static void
8698 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8699 {
8700 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8701
8702 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8703 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8704 dwarf2_per_objfile->n_type_units);
8705 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8706 tu_stats->nr_uniq_abbrev_tables);
8707 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8708 tu_stats->nr_symtabs);
8709 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8710 tu_stats->nr_symtab_sharers);
8711 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8712 tu_stats->nr_stmt_less_type_units);
8713 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8714 tu_stats->nr_all_type_units_reallocs);
8715 }
8716
8717 /* Traversal function for build_type_psymtabs. */
8718
8719 static int
8720 build_type_psymtab_dependencies (void **slot, void *info)
8721 {
8722 struct dwarf2_per_objfile *dwarf2_per_objfile
8723 = (struct dwarf2_per_objfile *) info;
8724 struct objfile *objfile = dwarf2_per_objfile->objfile;
8725 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8726 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8727 struct partial_symtab *pst = per_cu->v.psymtab;
8728 int len = VEC_length (sig_type_ptr, tu_group->tus);
8729 struct signatured_type *iter;
8730 int i;
8731
8732 gdb_assert (len > 0);
8733 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8734
8735 pst->number_of_dependencies = len;
8736 pst->dependencies =
8737 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8738 for (i = 0;
8739 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8740 ++i)
8741 {
8742 gdb_assert (iter->per_cu.is_debug_types);
8743 pst->dependencies[i] = iter->per_cu.v.psymtab;
8744 iter->type_unit_group = tu_group;
8745 }
8746
8747 VEC_free (sig_type_ptr, tu_group->tus);
8748
8749 return 1;
8750 }
8751
8752 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8753 Build partial symbol tables for the .debug_types comp-units. */
8754
8755 static void
8756 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8757 {
8758 if (! create_all_type_units (dwarf2_per_objfile))
8759 return;
8760
8761 build_type_psymtabs_1 (dwarf2_per_objfile);
8762 }
8763
8764 /* Traversal function for process_skeletonless_type_unit.
8765 Read a TU in a DWO file and build partial symbols for it. */
8766
8767 static int
8768 process_skeletonless_type_unit (void **slot, void *info)
8769 {
8770 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8771 struct dwarf2_per_objfile *dwarf2_per_objfile
8772 = (struct dwarf2_per_objfile *) info;
8773 struct signatured_type find_entry, *entry;
8774
8775 /* If this TU doesn't exist in the global table, add it and read it in. */
8776
8777 if (dwarf2_per_objfile->signatured_types == NULL)
8778 {
8779 dwarf2_per_objfile->signatured_types
8780 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8781 }
8782
8783 find_entry.signature = dwo_unit->signature;
8784 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8785 INSERT);
8786 /* If we've already seen this type there's nothing to do. What's happening
8787 is we're doing our own version of comdat-folding here. */
8788 if (*slot != NULL)
8789 return 1;
8790
8791 /* This does the job that create_all_type_units would have done for
8792 this TU. */
8793 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8794 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8795 *slot = entry;
8796
8797 /* This does the job that build_type_psymtabs_1 would have done. */
8798 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8799 build_type_psymtabs_reader, NULL);
8800
8801 return 1;
8802 }
8803
8804 /* Traversal function for process_skeletonless_type_units. */
8805
8806 static int
8807 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8808 {
8809 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8810
8811 if (dwo_file->tus != NULL)
8812 {
8813 htab_traverse_noresize (dwo_file->tus,
8814 process_skeletonless_type_unit, info);
8815 }
8816
8817 return 1;
8818 }
8819
8820 /* Scan all TUs of DWO files, verifying we've processed them.
8821 This is needed in case a TU was emitted without its skeleton.
8822 Note: This can't be done until we know what all the DWO files are. */
8823
8824 static void
8825 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8826 {
8827 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8828 if (get_dwp_file (dwarf2_per_objfile) == NULL
8829 && dwarf2_per_objfile->dwo_files != NULL)
8830 {
8831 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8832 process_dwo_file_for_skeletonless_type_units,
8833 dwarf2_per_objfile);
8834 }
8835 }
8836
8837 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8838
8839 static void
8840 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8841 {
8842 int i;
8843
8844 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8845 {
8846 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8847 struct partial_symtab *pst = per_cu->v.psymtab;
8848 int j;
8849
8850 if (pst == NULL)
8851 continue;
8852
8853 for (j = 0; j < pst->number_of_dependencies; ++j)
8854 {
8855 /* Set the 'user' field only if it is not already set. */
8856 if (pst->dependencies[j]->user == NULL)
8857 pst->dependencies[j]->user = pst;
8858 }
8859 }
8860 }
8861
8862 /* Build the partial symbol table by doing a quick pass through the
8863 .debug_info and .debug_abbrev sections. */
8864
8865 static void
8866 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8867 {
8868 struct cleanup *back_to;
8869 int i;
8870 struct objfile *objfile = dwarf2_per_objfile->objfile;
8871
8872 if (dwarf_read_debug)
8873 {
8874 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8875 objfile_name (objfile));
8876 }
8877
8878 dwarf2_per_objfile->reading_partial_symbols = 1;
8879
8880 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8881
8882 /* Any cached compilation units will be linked by the per-objfile
8883 read_in_chain. Make sure to free them when we're done. */
8884 back_to = make_cleanup (free_cached_comp_units, dwarf2_per_objfile);
8885
8886 build_type_psymtabs (dwarf2_per_objfile);
8887
8888 create_all_comp_units (dwarf2_per_objfile);
8889
8890 /* Create a temporary address map on a temporary obstack. We later
8891 copy this to the final obstack. */
8892 auto_obstack temp_obstack;
8893
8894 scoped_restore save_psymtabs_addrmap
8895 = make_scoped_restore (&objfile->psymtabs_addrmap,
8896 addrmap_create_mutable (&temp_obstack));
8897
8898 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8899 {
8900 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8901
8902 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8903 }
8904
8905 /* This has to wait until we read the CUs, we need the list of DWOs. */
8906 process_skeletonless_type_units (dwarf2_per_objfile);
8907
8908 /* Now that all TUs have been processed we can fill in the dependencies. */
8909 if (dwarf2_per_objfile->type_unit_groups != NULL)
8910 {
8911 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8912 build_type_psymtab_dependencies, dwarf2_per_objfile);
8913 }
8914
8915 if (dwarf_read_debug)
8916 print_tu_stats (dwarf2_per_objfile);
8917
8918 set_partial_user (dwarf2_per_objfile);
8919
8920 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8921 &objfile->objfile_obstack);
8922 /* At this point we want to keep the address map. */
8923 save_psymtabs_addrmap.release ();
8924
8925 do_cleanups (back_to);
8926
8927 if (dwarf_read_debug)
8928 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8929 objfile_name (objfile));
8930 }
8931
8932 /* die_reader_func for load_partial_comp_unit. */
8933
8934 static void
8935 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8936 const gdb_byte *info_ptr,
8937 struct die_info *comp_unit_die,
8938 int has_children,
8939 void *data)
8940 {
8941 struct dwarf2_cu *cu = reader->cu;
8942
8943 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8944
8945 /* Check if comp unit has_children.
8946 If so, read the rest of the partial symbols from this comp unit.
8947 If not, there's no more debug_info for this comp unit. */
8948 if (has_children)
8949 load_partial_dies (reader, info_ptr, 0);
8950 }
8951
8952 /* Load the partial DIEs for a secondary CU into memory.
8953 This is also used when rereading a primary CU with load_all_dies. */
8954
8955 static void
8956 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8957 {
8958 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8959 load_partial_comp_unit_reader, NULL);
8960 }
8961
8962 static void
8963 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8964 struct dwarf2_section_info *section,
8965 struct dwarf2_section_info *abbrev_section,
8966 unsigned int is_dwz,
8967 int *n_allocated,
8968 int *n_comp_units,
8969 struct dwarf2_per_cu_data ***all_comp_units)
8970 {
8971 const gdb_byte *info_ptr;
8972 struct objfile *objfile = dwarf2_per_objfile->objfile;
8973
8974 if (dwarf_read_debug)
8975 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8976 get_section_name (section),
8977 get_section_file_name (section));
8978
8979 dwarf2_read_section (objfile, section);
8980
8981 info_ptr = section->buffer;
8982
8983 while (info_ptr < section->buffer + section->size)
8984 {
8985 struct dwarf2_per_cu_data *this_cu;
8986
8987 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8988
8989 comp_unit_head cu_header;
8990 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8991 abbrev_section, info_ptr,
8992 rcuh_kind::COMPILE);
8993
8994 /* Save the compilation unit for later lookup. */
8995 if (cu_header.unit_type != DW_UT_type)
8996 {
8997 this_cu = XOBNEW (&objfile->objfile_obstack,
8998 struct dwarf2_per_cu_data);
8999 memset (this_cu, 0, sizeof (*this_cu));
9000 }
9001 else
9002 {
9003 auto sig_type = XOBNEW (&objfile->objfile_obstack,
9004 struct signatured_type);
9005 memset (sig_type, 0, sizeof (*sig_type));
9006 sig_type->signature = cu_header.signature;
9007 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
9008 this_cu = &sig_type->per_cu;
9009 }
9010 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9011 this_cu->sect_off = sect_off;
9012 this_cu->length = cu_header.length + cu_header.initial_length_size;
9013 this_cu->is_dwz = is_dwz;
9014 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
9015 this_cu->section = section;
9016
9017 if (*n_comp_units == *n_allocated)
9018 {
9019 *n_allocated *= 2;
9020 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
9021 *all_comp_units, *n_allocated);
9022 }
9023 (*all_comp_units)[*n_comp_units] = this_cu;
9024 ++*n_comp_units;
9025
9026 info_ptr = info_ptr + this_cu->length;
9027 }
9028 }
9029
9030 /* Create a list of all compilation units in OBJFILE.
9031 This is only done for -readnow and building partial symtabs. */
9032
9033 static void
9034 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
9035 {
9036 int n_allocated;
9037 int n_comp_units;
9038 struct dwarf2_per_cu_data **all_comp_units;
9039 struct dwz_file *dwz;
9040 struct objfile *objfile = dwarf2_per_objfile->objfile;
9041
9042 n_comp_units = 0;
9043 n_allocated = 10;
9044 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
9045
9046 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
9047 &dwarf2_per_objfile->abbrev, 0,
9048 &n_allocated, &n_comp_units, &all_comp_units);
9049
9050 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
9051 if (dwz != NULL)
9052 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
9053 1, &n_allocated, &n_comp_units,
9054 &all_comp_units);
9055
9056 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
9057 struct dwarf2_per_cu_data *,
9058 n_comp_units);
9059 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
9060 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
9061 xfree (all_comp_units);
9062 dwarf2_per_objfile->n_comp_units = n_comp_units;
9063 }
9064
9065 /* Process all loaded DIEs for compilation unit CU, starting at
9066 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
9067 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
9068 DW_AT_ranges). See the comments of add_partial_subprogram on how
9069 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
9070
9071 static void
9072 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
9073 CORE_ADDR *highpc, int set_addrmap,
9074 struct dwarf2_cu *cu)
9075 {
9076 struct partial_die_info *pdi;
9077
9078 /* Now, march along the PDI's, descending into ones which have
9079 interesting children but skipping the children of the other ones,
9080 until we reach the end of the compilation unit. */
9081
9082 pdi = first_die;
9083
9084 while (pdi != NULL)
9085 {
9086 fixup_partial_die (pdi, cu);
9087
9088 /* Anonymous namespaces or modules have no name but have interesting
9089 children, so we need to look at them. Ditto for anonymous
9090 enums. */
9091
9092 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
9093 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
9094 || pdi->tag == DW_TAG_imported_unit
9095 || pdi->tag == DW_TAG_inlined_subroutine)
9096 {
9097 switch (pdi->tag)
9098 {
9099 case DW_TAG_subprogram:
9100 case DW_TAG_inlined_subroutine:
9101 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9102 break;
9103 case DW_TAG_constant:
9104 case DW_TAG_variable:
9105 case DW_TAG_typedef:
9106 case DW_TAG_union_type:
9107 if (!pdi->is_declaration)
9108 {
9109 add_partial_symbol (pdi, cu);
9110 }
9111 break;
9112 case DW_TAG_class_type:
9113 case DW_TAG_interface_type:
9114 case DW_TAG_structure_type:
9115 if (!pdi->is_declaration)
9116 {
9117 add_partial_symbol (pdi, cu);
9118 }
9119 if (cu->language == language_rust && pdi->has_children)
9120 scan_partial_symbols (pdi->die_child, lowpc, highpc,
9121 set_addrmap, cu);
9122 break;
9123 case DW_TAG_enumeration_type:
9124 if (!pdi->is_declaration)
9125 add_partial_enumeration (pdi, cu);
9126 break;
9127 case DW_TAG_base_type:
9128 case DW_TAG_subrange_type:
9129 /* File scope base type definitions are added to the partial
9130 symbol table. */
9131 add_partial_symbol (pdi, cu);
9132 break;
9133 case DW_TAG_namespace:
9134 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
9135 break;
9136 case DW_TAG_module:
9137 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
9138 break;
9139 case DW_TAG_imported_unit:
9140 {
9141 struct dwarf2_per_cu_data *per_cu;
9142
9143 /* For now we don't handle imported units in type units. */
9144 if (cu->per_cu->is_debug_types)
9145 {
9146 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9147 " supported in type units [in module %s]"),
9148 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9149 }
9150
9151 per_cu = dwarf2_find_containing_comp_unit
9152 (pdi->d.sect_off, pdi->is_dwz,
9153 cu->per_cu->dwarf2_per_objfile);
9154
9155 /* Go read the partial unit, if needed. */
9156 if (per_cu->v.psymtab == NULL)
9157 process_psymtab_comp_unit (per_cu, 1, cu->language);
9158
9159 VEC_safe_push (dwarf2_per_cu_ptr,
9160 cu->per_cu->imported_symtabs, per_cu);
9161 }
9162 break;
9163 case DW_TAG_imported_declaration:
9164 add_partial_symbol (pdi, cu);
9165 break;
9166 default:
9167 break;
9168 }
9169 }
9170
9171 /* If the die has a sibling, skip to the sibling. */
9172
9173 pdi = pdi->die_sibling;
9174 }
9175 }
9176
9177 /* Functions used to compute the fully scoped name of a partial DIE.
9178
9179 Normally, this is simple. For C++, the parent DIE's fully scoped
9180 name is concatenated with "::" and the partial DIE's name.
9181 Enumerators are an exception; they use the scope of their parent
9182 enumeration type, i.e. the name of the enumeration type is not
9183 prepended to the enumerator.
9184
9185 There are two complexities. One is DW_AT_specification; in this
9186 case "parent" means the parent of the target of the specification,
9187 instead of the direct parent of the DIE. The other is compilers
9188 which do not emit DW_TAG_namespace; in this case we try to guess
9189 the fully qualified name of structure types from their members'
9190 linkage names. This must be done using the DIE's children rather
9191 than the children of any DW_AT_specification target. We only need
9192 to do this for structures at the top level, i.e. if the target of
9193 any DW_AT_specification (if any; otherwise the DIE itself) does not
9194 have a parent. */
9195
9196 /* Compute the scope prefix associated with PDI's parent, in
9197 compilation unit CU. The result will be allocated on CU's
9198 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9199 field. NULL is returned if no prefix is necessary. */
9200 static const char *
9201 partial_die_parent_scope (struct partial_die_info *pdi,
9202 struct dwarf2_cu *cu)
9203 {
9204 const char *grandparent_scope;
9205 struct partial_die_info *parent, *real_pdi;
9206
9207 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9208 then this means the parent of the specification DIE. */
9209
9210 real_pdi = pdi;
9211 while (real_pdi->has_specification)
9212 real_pdi = find_partial_die (real_pdi->spec_offset,
9213 real_pdi->spec_is_dwz, cu);
9214
9215 parent = real_pdi->die_parent;
9216 if (parent == NULL)
9217 return NULL;
9218
9219 if (parent->scope_set)
9220 return parent->scope;
9221
9222 fixup_partial_die (parent, cu);
9223
9224 grandparent_scope = partial_die_parent_scope (parent, cu);
9225
9226 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9227 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9228 Work around this problem here. */
9229 if (cu->language == language_cplus
9230 && parent->tag == DW_TAG_namespace
9231 && strcmp (parent->name, "::") == 0
9232 && grandparent_scope == NULL)
9233 {
9234 parent->scope = NULL;
9235 parent->scope_set = 1;
9236 return NULL;
9237 }
9238
9239 if (pdi->tag == DW_TAG_enumerator)
9240 /* Enumerators should not get the name of the enumeration as a prefix. */
9241 parent->scope = grandparent_scope;
9242 else if (parent->tag == DW_TAG_namespace
9243 || parent->tag == DW_TAG_module
9244 || parent->tag == DW_TAG_structure_type
9245 || parent->tag == DW_TAG_class_type
9246 || parent->tag == DW_TAG_interface_type
9247 || parent->tag == DW_TAG_union_type
9248 || parent->tag == DW_TAG_enumeration_type)
9249 {
9250 if (grandparent_scope == NULL)
9251 parent->scope = parent->name;
9252 else
9253 parent->scope = typename_concat (&cu->comp_unit_obstack,
9254 grandparent_scope,
9255 parent->name, 0, cu);
9256 }
9257 else
9258 {
9259 /* FIXME drow/2004-04-01: What should we be doing with
9260 function-local names? For partial symbols, we should probably be
9261 ignoring them. */
9262 complaint (&symfile_complaints,
9263 _("unhandled containing DIE tag %d for DIE at %s"),
9264 parent->tag, sect_offset_str (pdi->sect_off));
9265 parent->scope = grandparent_scope;
9266 }
9267
9268 parent->scope_set = 1;
9269 return parent->scope;
9270 }
9271
9272 /* Return the fully scoped name associated with PDI, from compilation unit
9273 CU. The result will be allocated with malloc. */
9274
9275 static char *
9276 partial_die_full_name (struct partial_die_info *pdi,
9277 struct dwarf2_cu *cu)
9278 {
9279 const char *parent_scope;
9280
9281 /* If this is a template instantiation, we can not work out the
9282 template arguments from partial DIEs. So, unfortunately, we have
9283 to go through the full DIEs. At least any work we do building
9284 types here will be reused if full symbols are loaded later. */
9285 if (pdi->has_template_arguments)
9286 {
9287 fixup_partial_die (pdi, cu);
9288
9289 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9290 {
9291 struct die_info *die;
9292 struct attribute attr;
9293 struct dwarf2_cu *ref_cu = cu;
9294
9295 /* DW_FORM_ref_addr is using section offset. */
9296 attr.name = (enum dwarf_attribute) 0;
9297 attr.form = DW_FORM_ref_addr;
9298 attr.u.unsnd = to_underlying (pdi->sect_off);
9299 die = follow_die_ref (NULL, &attr, &ref_cu);
9300
9301 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9302 }
9303 }
9304
9305 parent_scope = partial_die_parent_scope (pdi, cu);
9306 if (parent_scope == NULL)
9307 return NULL;
9308 else
9309 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
9310 }
9311
9312 static void
9313 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
9314 {
9315 struct dwarf2_per_objfile *dwarf2_per_objfile
9316 = cu->per_cu->dwarf2_per_objfile;
9317 struct objfile *objfile = dwarf2_per_objfile->objfile;
9318 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9319 CORE_ADDR addr = 0;
9320 const char *actual_name = NULL;
9321 CORE_ADDR baseaddr;
9322 char *built_actual_name;
9323
9324 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9325
9326 built_actual_name = partial_die_full_name (pdi, cu);
9327 if (built_actual_name != NULL)
9328 actual_name = built_actual_name;
9329
9330 if (actual_name == NULL)
9331 actual_name = pdi->name;
9332
9333 switch (pdi->tag)
9334 {
9335 case DW_TAG_inlined_subroutine:
9336 case DW_TAG_subprogram:
9337 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
9338 if (pdi->is_external || cu->language == language_ada)
9339 {
9340 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9341 of the global scope. But in Ada, we want to be able to access
9342 nested procedures globally. So all Ada subprograms are stored
9343 in the global scope. */
9344 add_psymbol_to_list (actual_name, strlen (actual_name),
9345 built_actual_name != NULL,
9346 VAR_DOMAIN, LOC_BLOCK,
9347 &objfile->global_psymbols,
9348 addr, cu->language, objfile);
9349 }
9350 else
9351 {
9352 add_psymbol_to_list (actual_name, strlen (actual_name),
9353 built_actual_name != NULL,
9354 VAR_DOMAIN, LOC_BLOCK,
9355 &objfile->static_psymbols,
9356 addr, cu->language, objfile);
9357 }
9358
9359 if (pdi->main_subprogram && actual_name != NULL)
9360 set_objfile_main_name (objfile, actual_name, cu->language);
9361 break;
9362 case DW_TAG_constant:
9363 {
9364 std::vector<partial_symbol *> *list;
9365
9366 if (pdi->is_external)
9367 list = &objfile->global_psymbols;
9368 else
9369 list = &objfile->static_psymbols;
9370 add_psymbol_to_list (actual_name, strlen (actual_name),
9371 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9372 list, 0, cu->language, objfile);
9373 }
9374 break;
9375 case DW_TAG_variable:
9376 if (pdi->d.locdesc)
9377 addr = decode_locdesc (pdi->d.locdesc, cu);
9378
9379 if (pdi->d.locdesc
9380 && addr == 0
9381 && !dwarf2_per_objfile->has_section_at_zero)
9382 {
9383 /* A global or static variable may also have been stripped
9384 out by the linker if unused, in which case its address
9385 will be nullified; do not add such variables into partial
9386 symbol table then. */
9387 }
9388 else if (pdi->is_external)
9389 {
9390 /* Global Variable.
9391 Don't enter into the minimal symbol tables as there is
9392 a minimal symbol table entry from the ELF symbols already.
9393 Enter into partial symbol table if it has a location
9394 descriptor or a type.
9395 If the location descriptor is missing, new_symbol will create
9396 a LOC_UNRESOLVED symbol, the address of the variable will then
9397 be determined from the minimal symbol table whenever the variable
9398 is referenced.
9399 The address for the partial symbol table entry is not
9400 used by GDB, but it comes in handy for debugging partial symbol
9401 table building. */
9402
9403 if (pdi->d.locdesc || pdi->has_type)
9404 add_psymbol_to_list (actual_name, strlen (actual_name),
9405 built_actual_name != NULL,
9406 VAR_DOMAIN, LOC_STATIC,
9407 &objfile->global_psymbols,
9408 addr + baseaddr,
9409 cu->language, objfile);
9410 }
9411 else
9412 {
9413 int has_loc = pdi->d.locdesc != NULL;
9414
9415 /* Static Variable. Skip symbols whose value we cannot know (those
9416 without location descriptors or constant values). */
9417 if (!has_loc && !pdi->has_const_value)
9418 {
9419 xfree (built_actual_name);
9420 return;
9421 }
9422
9423 add_psymbol_to_list (actual_name, strlen (actual_name),
9424 built_actual_name != NULL,
9425 VAR_DOMAIN, LOC_STATIC,
9426 &objfile->static_psymbols,
9427 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
9428 cu->language, objfile);
9429 }
9430 break;
9431 case DW_TAG_typedef:
9432 case DW_TAG_base_type:
9433 case DW_TAG_subrange_type:
9434 add_psymbol_to_list (actual_name, strlen (actual_name),
9435 built_actual_name != NULL,
9436 VAR_DOMAIN, LOC_TYPEDEF,
9437 &objfile->static_psymbols,
9438 0, cu->language, objfile);
9439 break;
9440 case DW_TAG_imported_declaration:
9441 case DW_TAG_namespace:
9442 add_psymbol_to_list (actual_name, strlen (actual_name),
9443 built_actual_name != NULL,
9444 VAR_DOMAIN, LOC_TYPEDEF,
9445 &objfile->global_psymbols,
9446 0, cu->language, objfile);
9447 break;
9448 case DW_TAG_module:
9449 add_psymbol_to_list (actual_name, strlen (actual_name),
9450 built_actual_name != NULL,
9451 MODULE_DOMAIN, LOC_TYPEDEF,
9452 &objfile->global_psymbols,
9453 0, cu->language, objfile);
9454 break;
9455 case DW_TAG_class_type:
9456 case DW_TAG_interface_type:
9457 case DW_TAG_structure_type:
9458 case DW_TAG_union_type:
9459 case DW_TAG_enumeration_type:
9460 /* Skip external references. The DWARF standard says in the section
9461 about "Structure, Union, and Class Type Entries": "An incomplete
9462 structure, union or class type is represented by a structure,
9463 union or class entry that does not have a byte size attribute
9464 and that has a DW_AT_declaration attribute." */
9465 if (!pdi->has_byte_size && pdi->is_declaration)
9466 {
9467 xfree (built_actual_name);
9468 return;
9469 }
9470
9471 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9472 static vs. global. */
9473 add_psymbol_to_list (actual_name, strlen (actual_name),
9474 built_actual_name != NULL,
9475 STRUCT_DOMAIN, LOC_TYPEDEF,
9476 cu->language == language_cplus
9477 ? &objfile->global_psymbols
9478 : &objfile->static_psymbols,
9479 0, cu->language, objfile);
9480
9481 break;
9482 case DW_TAG_enumerator:
9483 add_psymbol_to_list (actual_name, strlen (actual_name),
9484 built_actual_name != NULL,
9485 VAR_DOMAIN, LOC_CONST,
9486 cu->language == language_cplus
9487 ? &objfile->global_psymbols
9488 : &objfile->static_psymbols,
9489 0, cu->language, objfile);
9490 break;
9491 default:
9492 break;
9493 }
9494
9495 xfree (built_actual_name);
9496 }
9497
9498 /* Read a partial die corresponding to a namespace; also, add a symbol
9499 corresponding to that namespace to the symbol table. NAMESPACE is
9500 the name of the enclosing namespace. */
9501
9502 static void
9503 add_partial_namespace (struct partial_die_info *pdi,
9504 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9505 int set_addrmap, struct dwarf2_cu *cu)
9506 {
9507 /* Add a symbol for the namespace. */
9508
9509 add_partial_symbol (pdi, cu);
9510
9511 /* Now scan partial symbols in that namespace. */
9512
9513 if (pdi->has_children)
9514 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9515 }
9516
9517 /* Read a partial die corresponding to a Fortran module. */
9518
9519 static void
9520 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9521 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9522 {
9523 /* Add a symbol for the namespace. */
9524
9525 add_partial_symbol (pdi, cu);
9526
9527 /* Now scan partial symbols in that module. */
9528
9529 if (pdi->has_children)
9530 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9531 }
9532
9533 /* Read a partial die corresponding to a subprogram or an inlined
9534 subprogram and create a partial symbol for that subprogram.
9535 When the CU language allows it, this routine also defines a partial
9536 symbol for each nested subprogram that this subprogram contains.
9537 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9538 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9539
9540 PDI may also be a lexical block, in which case we simply search
9541 recursively for subprograms defined inside that lexical block.
9542 Again, this is only performed when the CU language allows this
9543 type of definitions. */
9544
9545 static void
9546 add_partial_subprogram (struct partial_die_info *pdi,
9547 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9548 int set_addrmap, struct dwarf2_cu *cu)
9549 {
9550 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9551 {
9552 if (pdi->has_pc_info)
9553 {
9554 if (pdi->lowpc < *lowpc)
9555 *lowpc = pdi->lowpc;
9556 if (pdi->highpc > *highpc)
9557 *highpc = pdi->highpc;
9558 if (set_addrmap)
9559 {
9560 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9561 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9562 CORE_ADDR baseaddr;
9563 CORE_ADDR highpc;
9564 CORE_ADDR lowpc;
9565
9566 baseaddr = ANOFFSET (objfile->section_offsets,
9567 SECT_OFF_TEXT (objfile));
9568 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9569 pdi->lowpc + baseaddr);
9570 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9571 pdi->highpc + baseaddr);
9572 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9573 cu->per_cu->v.psymtab);
9574 }
9575 }
9576
9577 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9578 {
9579 if (!pdi->is_declaration)
9580 /* Ignore subprogram DIEs that do not have a name, they are
9581 illegal. Do not emit a complaint at this point, we will
9582 do so when we convert this psymtab into a symtab. */
9583 if (pdi->name)
9584 add_partial_symbol (pdi, cu);
9585 }
9586 }
9587
9588 if (! pdi->has_children)
9589 return;
9590
9591 if (cu->language == language_ada)
9592 {
9593 pdi = pdi->die_child;
9594 while (pdi != NULL)
9595 {
9596 fixup_partial_die (pdi, cu);
9597 if (pdi->tag == DW_TAG_subprogram
9598 || pdi->tag == DW_TAG_inlined_subroutine
9599 || pdi->tag == DW_TAG_lexical_block)
9600 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9601 pdi = pdi->die_sibling;
9602 }
9603 }
9604 }
9605
9606 /* Read a partial die corresponding to an enumeration type. */
9607
9608 static void
9609 add_partial_enumeration (struct partial_die_info *enum_pdi,
9610 struct dwarf2_cu *cu)
9611 {
9612 struct partial_die_info *pdi;
9613
9614 if (enum_pdi->name != NULL)
9615 add_partial_symbol (enum_pdi, cu);
9616
9617 pdi = enum_pdi->die_child;
9618 while (pdi)
9619 {
9620 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9621 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9622 else
9623 add_partial_symbol (pdi, cu);
9624 pdi = pdi->die_sibling;
9625 }
9626 }
9627
9628 /* Return the initial uleb128 in the die at INFO_PTR. */
9629
9630 static unsigned int
9631 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9632 {
9633 unsigned int bytes_read;
9634
9635 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9636 }
9637
9638 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9639 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9640
9641 Return the corresponding abbrev, or NULL if the number is zero (indicating
9642 an empty DIE). In either case *BYTES_READ will be set to the length of
9643 the initial number. */
9644
9645 static struct abbrev_info *
9646 peek_die_abbrev (const die_reader_specs &reader,
9647 const gdb_byte *info_ptr, unsigned int *bytes_read)
9648 {
9649 dwarf2_cu *cu = reader.cu;
9650 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9651 unsigned int abbrev_number
9652 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9653
9654 if (abbrev_number == 0)
9655 return NULL;
9656
9657 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9658 if (!abbrev)
9659 {
9660 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9661 " at offset %s [in module %s]"),
9662 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9663 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9664 }
9665
9666 return abbrev;
9667 }
9668
9669 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9670 Returns a pointer to the end of a series of DIEs, terminated by an empty
9671 DIE. Any children of the skipped DIEs will also be skipped. */
9672
9673 static const gdb_byte *
9674 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9675 {
9676 while (1)
9677 {
9678 unsigned int bytes_read;
9679 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9680
9681 if (abbrev == NULL)
9682 return info_ptr + bytes_read;
9683 else
9684 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9685 }
9686 }
9687
9688 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9689 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9690 abbrev corresponding to that skipped uleb128 should be passed in
9691 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9692 children. */
9693
9694 static const gdb_byte *
9695 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9696 struct abbrev_info *abbrev)
9697 {
9698 unsigned int bytes_read;
9699 struct attribute attr;
9700 bfd *abfd = reader->abfd;
9701 struct dwarf2_cu *cu = reader->cu;
9702 const gdb_byte *buffer = reader->buffer;
9703 const gdb_byte *buffer_end = reader->buffer_end;
9704 unsigned int form, i;
9705
9706 for (i = 0; i < abbrev->num_attrs; i++)
9707 {
9708 /* The only abbrev we care about is DW_AT_sibling. */
9709 if (abbrev->attrs[i].name == DW_AT_sibling)
9710 {
9711 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9712 if (attr.form == DW_FORM_ref_addr)
9713 complaint (&symfile_complaints,
9714 _("ignoring absolute DW_AT_sibling"));
9715 else
9716 {
9717 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9718 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9719
9720 if (sibling_ptr < info_ptr)
9721 complaint (&symfile_complaints,
9722 _("DW_AT_sibling points backwards"));
9723 else if (sibling_ptr > reader->buffer_end)
9724 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9725 else
9726 return sibling_ptr;
9727 }
9728 }
9729
9730 /* If it isn't DW_AT_sibling, skip this attribute. */
9731 form = abbrev->attrs[i].form;
9732 skip_attribute:
9733 switch (form)
9734 {
9735 case DW_FORM_ref_addr:
9736 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9737 and later it is offset sized. */
9738 if (cu->header.version == 2)
9739 info_ptr += cu->header.addr_size;
9740 else
9741 info_ptr += cu->header.offset_size;
9742 break;
9743 case DW_FORM_GNU_ref_alt:
9744 info_ptr += cu->header.offset_size;
9745 break;
9746 case DW_FORM_addr:
9747 info_ptr += cu->header.addr_size;
9748 break;
9749 case DW_FORM_data1:
9750 case DW_FORM_ref1:
9751 case DW_FORM_flag:
9752 info_ptr += 1;
9753 break;
9754 case DW_FORM_flag_present:
9755 case DW_FORM_implicit_const:
9756 break;
9757 case DW_FORM_data2:
9758 case DW_FORM_ref2:
9759 info_ptr += 2;
9760 break;
9761 case DW_FORM_data4:
9762 case DW_FORM_ref4:
9763 info_ptr += 4;
9764 break;
9765 case DW_FORM_data8:
9766 case DW_FORM_ref8:
9767 case DW_FORM_ref_sig8:
9768 info_ptr += 8;
9769 break;
9770 case DW_FORM_data16:
9771 info_ptr += 16;
9772 break;
9773 case DW_FORM_string:
9774 read_direct_string (abfd, info_ptr, &bytes_read);
9775 info_ptr += bytes_read;
9776 break;
9777 case DW_FORM_sec_offset:
9778 case DW_FORM_strp:
9779 case DW_FORM_GNU_strp_alt:
9780 info_ptr += cu->header.offset_size;
9781 break;
9782 case DW_FORM_exprloc:
9783 case DW_FORM_block:
9784 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9785 info_ptr += bytes_read;
9786 break;
9787 case DW_FORM_block1:
9788 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9789 break;
9790 case DW_FORM_block2:
9791 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9792 break;
9793 case DW_FORM_block4:
9794 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9795 break;
9796 case DW_FORM_sdata:
9797 case DW_FORM_udata:
9798 case DW_FORM_ref_udata:
9799 case DW_FORM_GNU_addr_index:
9800 case DW_FORM_GNU_str_index:
9801 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9802 break;
9803 case DW_FORM_indirect:
9804 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9805 info_ptr += bytes_read;
9806 /* We need to continue parsing from here, so just go back to
9807 the top. */
9808 goto skip_attribute;
9809
9810 default:
9811 error (_("Dwarf Error: Cannot handle %s "
9812 "in DWARF reader [in module %s]"),
9813 dwarf_form_name (form),
9814 bfd_get_filename (abfd));
9815 }
9816 }
9817
9818 if (abbrev->has_children)
9819 return skip_children (reader, info_ptr);
9820 else
9821 return info_ptr;
9822 }
9823
9824 /* Locate ORIG_PDI's sibling.
9825 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9826
9827 static const gdb_byte *
9828 locate_pdi_sibling (const struct die_reader_specs *reader,
9829 struct partial_die_info *orig_pdi,
9830 const gdb_byte *info_ptr)
9831 {
9832 /* Do we know the sibling already? */
9833
9834 if (orig_pdi->sibling)
9835 return orig_pdi->sibling;
9836
9837 /* Are there any children to deal with? */
9838
9839 if (!orig_pdi->has_children)
9840 return info_ptr;
9841
9842 /* Skip the children the long way. */
9843
9844 return skip_children (reader, info_ptr);
9845 }
9846
9847 /* Expand this partial symbol table into a full symbol table. SELF is
9848 not NULL. */
9849
9850 static void
9851 dwarf2_read_symtab (struct partial_symtab *self,
9852 struct objfile *objfile)
9853 {
9854 struct dwarf2_per_objfile *dwarf2_per_objfile
9855 = get_dwarf2_per_objfile (objfile);
9856
9857 if (self->readin)
9858 {
9859 warning (_("bug: psymtab for %s is already read in."),
9860 self->filename);
9861 }
9862 else
9863 {
9864 if (info_verbose)
9865 {
9866 printf_filtered (_("Reading in symbols for %s..."),
9867 self->filename);
9868 gdb_flush (gdb_stdout);
9869 }
9870
9871 /* If this psymtab is constructed from a debug-only objfile, the
9872 has_section_at_zero flag will not necessarily be correct. We
9873 can get the correct value for this flag by looking at the data
9874 associated with the (presumably stripped) associated objfile. */
9875 if (objfile->separate_debug_objfile_backlink)
9876 {
9877 struct dwarf2_per_objfile *dpo_backlink
9878 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9879
9880 dwarf2_per_objfile->has_section_at_zero
9881 = dpo_backlink->has_section_at_zero;
9882 }
9883
9884 dwarf2_per_objfile->reading_partial_symbols = 0;
9885
9886 psymtab_to_symtab_1 (self);
9887
9888 /* Finish up the debug error message. */
9889 if (info_verbose)
9890 printf_filtered (_("done.\n"));
9891 }
9892
9893 process_cu_includes (dwarf2_per_objfile);
9894 }
9895 \f
9896 /* Reading in full CUs. */
9897
9898 /* Add PER_CU to the queue. */
9899
9900 static void
9901 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9902 enum language pretend_language)
9903 {
9904 struct dwarf2_queue_item *item;
9905
9906 per_cu->queued = 1;
9907 item = XNEW (struct dwarf2_queue_item);
9908 item->per_cu = per_cu;
9909 item->pretend_language = pretend_language;
9910 item->next = NULL;
9911
9912 if (dwarf2_queue == NULL)
9913 dwarf2_queue = item;
9914 else
9915 dwarf2_queue_tail->next = item;
9916
9917 dwarf2_queue_tail = item;
9918 }
9919
9920 /* If PER_CU is not yet queued, add it to the queue.
9921 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9922 dependency.
9923 The result is non-zero if PER_CU was queued, otherwise the result is zero
9924 meaning either PER_CU is already queued or it is already loaded.
9925
9926 N.B. There is an invariant here that if a CU is queued then it is loaded.
9927 The caller is required to load PER_CU if we return non-zero. */
9928
9929 static int
9930 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9931 struct dwarf2_per_cu_data *per_cu,
9932 enum language pretend_language)
9933 {
9934 /* We may arrive here during partial symbol reading, if we need full
9935 DIEs to process an unusual case (e.g. template arguments). Do
9936 not queue PER_CU, just tell our caller to load its DIEs. */
9937 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9938 {
9939 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9940 return 1;
9941 return 0;
9942 }
9943
9944 /* Mark the dependence relation so that we don't flush PER_CU
9945 too early. */
9946 if (dependent_cu != NULL)
9947 dwarf2_add_dependence (dependent_cu, per_cu);
9948
9949 /* If it's already on the queue, we have nothing to do. */
9950 if (per_cu->queued)
9951 return 0;
9952
9953 /* If the compilation unit is already loaded, just mark it as
9954 used. */
9955 if (per_cu->cu != NULL)
9956 {
9957 per_cu->cu->last_used = 0;
9958 return 0;
9959 }
9960
9961 /* Add it to the queue. */
9962 queue_comp_unit (per_cu, pretend_language);
9963
9964 return 1;
9965 }
9966
9967 /* Process the queue. */
9968
9969 static void
9970 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9971 {
9972 struct dwarf2_queue_item *item, *next_item;
9973
9974 if (dwarf_read_debug)
9975 {
9976 fprintf_unfiltered (gdb_stdlog,
9977 "Expanding one or more symtabs of objfile %s ...\n",
9978 objfile_name (dwarf2_per_objfile->objfile));
9979 }
9980
9981 /* The queue starts out with one item, but following a DIE reference
9982 may load a new CU, adding it to the end of the queue. */
9983 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9984 {
9985 if ((dwarf2_per_objfile->using_index
9986 ? !item->per_cu->v.quick->compunit_symtab
9987 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9988 /* Skip dummy CUs. */
9989 && item->per_cu->cu != NULL)
9990 {
9991 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9992 unsigned int debug_print_threshold;
9993 char buf[100];
9994
9995 if (per_cu->is_debug_types)
9996 {
9997 struct signatured_type *sig_type =
9998 (struct signatured_type *) per_cu;
9999
10000 sprintf (buf, "TU %s at offset %s",
10001 hex_string (sig_type->signature),
10002 sect_offset_str (per_cu->sect_off));
10003 /* There can be 100s of TUs.
10004 Only print them in verbose mode. */
10005 debug_print_threshold = 2;
10006 }
10007 else
10008 {
10009 sprintf (buf, "CU at offset %s",
10010 sect_offset_str (per_cu->sect_off));
10011 debug_print_threshold = 1;
10012 }
10013
10014 if (dwarf_read_debug >= debug_print_threshold)
10015 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
10016
10017 if (per_cu->is_debug_types)
10018 process_full_type_unit (per_cu, item->pretend_language);
10019 else
10020 process_full_comp_unit (per_cu, item->pretend_language);
10021
10022 if (dwarf_read_debug >= debug_print_threshold)
10023 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
10024 }
10025
10026 item->per_cu->queued = 0;
10027 next_item = item->next;
10028 xfree (item);
10029 }
10030
10031 dwarf2_queue_tail = NULL;
10032
10033 if (dwarf_read_debug)
10034 {
10035 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
10036 objfile_name (dwarf2_per_objfile->objfile));
10037 }
10038 }
10039
10040 /* Read in full symbols for PST, and anything it depends on. */
10041
10042 static void
10043 psymtab_to_symtab_1 (struct partial_symtab *pst)
10044 {
10045 struct dwarf2_per_cu_data *per_cu;
10046 int i;
10047
10048 if (pst->readin)
10049 return;
10050
10051 for (i = 0; i < pst->number_of_dependencies; i++)
10052 if (!pst->dependencies[i]->readin
10053 && pst->dependencies[i]->user == NULL)
10054 {
10055 /* Inform about additional files that need to be read in. */
10056 if (info_verbose)
10057 {
10058 /* FIXME: i18n: Need to make this a single string. */
10059 fputs_filtered (" ", gdb_stdout);
10060 wrap_here ("");
10061 fputs_filtered ("and ", gdb_stdout);
10062 wrap_here ("");
10063 printf_filtered ("%s...", pst->dependencies[i]->filename);
10064 wrap_here (""); /* Flush output. */
10065 gdb_flush (gdb_stdout);
10066 }
10067 psymtab_to_symtab_1 (pst->dependencies[i]);
10068 }
10069
10070 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10071
10072 if (per_cu == NULL)
10073 {
10074 /* It's an include file, no symbols to read for it.
10075 Everything is in the parent symtab. */
10076 pst->readin = 1;
10077 return;
10078 }
10079
10080 dw2_do_instantiate_symtab (per_cu);
10081 }
10082
10083 /* Trivial hash function for die_info: the hash value of a DIE
10084 is its offset in .debug_info for this objfile. */
10085
10086 static hashval_t
10087 die_hash (const void *item)
10088 {
10089 const struct die_info *die = (const struct die_info *) item;
10090
10091 return to_underlying (die->sect_off);
10092 }
10093
10094 /* Trivial comparison function for die_info structures: two DIEs
10095 are equal if they have the same offset. */
10096
10097 static int
10098 die_eq (const void *item_lhs, const void *item_rhs)
10099 {
10100 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
10101 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
10102
10103 return die_lhs->sect_off == die_rhs->sect_off;
10104 }
10105
10106 /* die_reader_func for load_full_comp_unit.
10107 This is identical to read_signatured_type_reader,
10108 but is kept separate for now. */
10109
10110 static void
10111 load_full_comp_unit_reader (const struct die_reader_specs *reader,
10112 const gdb_byte *info_ptr,
10113 struct die_info *comp_unit_die,
10114 int has_children,
10115 void *data)
10116 {
10117 struct dwarf2_cu *cu = reader->cu;
10118 enum language *language_ptr = (enum language *) data;
10119
10120 gdb_assert (cu->die_hash == NULL);
10121 cu->die_hash =
10122 htab_create_alloc_ex (cu->header.length / 12,
10123 die_hash,
10124 die_eq,
10125 NULL,
10126 &cu->comp_unit_obstack,
10127 hashtab_obstack_allocate,
10128 dummy_obstack_deallocate);
10129
10130 if (has_children)
10131 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10132 &info_ptr, comp_unit_die);
10133 cu->dies = comp_unit_die;
10134 /* comp_unit_die is not stored in die_hash, no need. */
10135
10136 /* We try not to read any attributes in this function, because not
10137 all CUs needed for references have been loaded yet, and symbol
10138 table processing isn't initialized. But we have to set the CU language,
10139 or we won't be able to build types correctly.
10140 Similarly, if we do not read the producer, we can not apply
10141 producer-specific interpretation. */
10142 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
10143 }
10144
10145 /* Load the DIEs associated with PER_CU into memory. */
10146
10147 static void
10148 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10149 enum language pretend_language)
10150 {
10151 gdb_assert (! this_cu->is_debug_types);
10152
10153 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10154 load_full_comp_unit_reader, &pretend_language);
10155 }
10156
10157 /* Add a DIE to the delayed physname list. */
10158
10159 static void
10160 add_to_method_list (struct type *type, int fnfield_index, int index,
10161 const char *name, struct die_info *die,
10162 struct dwarf2_cu *cu)
10163 {
10164 struct delayed_method_info mi;
10165 mi.type = type;
10166 mi.fnfield_index = fnfield_index;
10167 mi.index = index;
10168 mi.name = name;
10169 mi.die = die;
10170 cu->method_list.push_back (mi);
10171 }
10172
10173 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10174 "const" / "volatile". If so, decrements LEN by the length of the
10175 modifier and return true. Otherwise return false. */
10176
10177 template<size_t N>
10178 static bool
10179 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10180 {
10181 size_t mod_len = sizeof (mod) - 1;
10182 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10183 {
10184 len -= mod_len;
10185 return true;
10186 }
10187 return false;
10188 }
10189
10190 /* Compute the physnames of any methods on the CU's method list.
10191
10192 The computation of method physnames is delayed in order to avoid the
10193 (bad) condition that one of the method's formal parameters is of an as yet
10194 incomplete type. */
10195
10196 static void
10197 compute_delayed_physnames (struct dwarf2_cu *cu)
10198 {
10199 /* Only C++ delays computing physnames. */
10200 if (cu->method_list.empty ())
10201 return;
10202 gdb_assert (cu->language == language_cplus);
10203
10204 for (struct delayed_method_info &mi : cu->method_list)
10205 {
10206 const char *physname;
10207 struct fn_fieldlist *fn_flp
10208 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
10209 physname = dwarf2_physname (mi.name, mi.die, cu);
10210 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
10211 = physname ? physname : "";
10212
10213 /* Since there's no tag to indicate whether a method is a
10214 const/volatile overload, extract that information out of the
10215 demangled name. */
10216 if (physname != NULL)
10217 {
10218 size_t len = strlen (physname);
10219
10220 while (1)
10221 {
10222 if (physname[len] == ')') /* shortcut */
10223 break;
10224 else if (check_modifier (physname, len, " const"))
10225 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
10226 else if (check_modifier (physname, len, " volatile"))
10227 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
10228 else
10229 break;
10230 }
10231 }
10232 }
10233
10234 /* The list is no longer needed. */
10235 cu->method_list.clear ();
10236 }
10237
10238 /* Go objects should be embedded in a DW_TAG_module DIE,
10239 and it's not clear if/how imported objects will appear.
10240 To keep Go support simple until that's worked out,
10241 go back through what we've read and create something usable.
10242 We could do this while processing each DIE, and feels kinda cleaner,
10243 but that way is more invasive.
10244 This is to, for example, allow the user to type "p var" or "b main"
10245 without having to specify the package name, and allow lookups
10246 of module.object to work in contexts that use the expression
10247 parser. */
10248
10249 static void
10250 fixup_go_packaging (struct dwarf2_cu *cu)
10251 {
10252 char *package_name = NULL;
10253 struct pending *list;
10254 int i;
10255
10256 for (list = global_symbols; list != NULL; list = list->next)
10257 {
10258 for (i = 0; i < list->nsyms; ++i)
10259 {
10260 struct symbol *sym = list->symbol[i];
10261
10262 if (SYMBOL_LANGUAGE (sym) == language_go
10263 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10264 {
10265 char *this_package_name = go_symbol_package_name (sym);
10266
10267 if (this_package_name == NULL)
10268 continue;
10269 if (package_name == NULL)
10270 package_name = this_package_name;
10271 else
10272 {
10273 struct objfile *objfile
10274 = cu->per_cu->dwarf2_per_objfile->objfile;
10275 if (strcmp (package_name, this_package_name) != 0)
10276 complaint (&symfile_complaints,
10277 _("Symtab %s has objects from two different Go packages: %s and %s"),
10278 (symbol_symtab (sym) != NULL
10279 ? symtab_to_filename_for_display
10280 (symbol_symtab (sym))
10281 : objfile_name (objfile)),
10282 this_package_name, package_name);
10283 xfree (this_package_name);
10284 }
10285 }
10286 }
10287 }
10288
10289 if (package_name != NULL)
10290 {
10291 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10292 const char *saved_package_name
10293 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10294 package_name,
10295 strlen (package_name));
10296 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10297 saved_package_name);
10298 struct symbol *sym;
10299
10300 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10301
10302 sym = allocate_symbol (objfile);
10303 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
10304 SYMBOL_SET_NAMES (sym, saved_package_name,
10305 strlen (saved_package_name), 0, objfile);
10306 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10307 e.g., "main" finds the "main" module and not C's main(). */
10308 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10309 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
10310 SYMBOL_TYPE (sym) = type;
10311
10312 add_symbol_to_list (sym, &global_symbols);
10313
10314 xfree (package_name);
10315 }
10316 }
10317
10318 /* Return the symtab for PER_CU. This works properly regardless of
10319 whether we're using the index or psymtabs. */
10320
10321 static struct compunit_symtab *
10322 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10323 {
10324 return (per_cu->dwarf2_per_objfile->using_index
10325 ? per_cu->v.quick->compunit_symtab
10326 : per_cu->v.psymtab->compunit_symtab);
10327 }
10328
10329 /* A helper function for computing the list of all symbol tables
10330 included by PER_CU. */
10331
10332 static void
10333 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10334 htab_t all_children, htab_t all_type_symtabs,
10335 struct dwarf2_per_cu_data *per_cu,
10336 struct compunit_symtab *immediate_parent)
10337 {
10338 void **slot;
10339 int ix;
10340 struct compunit_symtab *cust;
10341 struct dwarf2_per_cu_data *iter;
10342
10343 slot = htab_find_slot (all_children, per_cu, INSERT);
10344 if (*slot != NULL)
10345 {
10346 /* This inclusion and its children have been processed. */
10347 return;
10348 }
10349
10350 *slot = per_cu;
10351 /* Only add a CU if it has a symbol table. */
10352 cust = get_compunit_symtab (per_cu);
10353 if (cust != NULL)
10354 {
10355 /* If this is a type unit only add its symbol table if we haven't
10356 seen it yet (type unit per_cu's can share symtabs). */
10357 if (per_cu->is_debug_types)
10358 {
10359 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10360 if (*slot == NULL)
10361 {
10362 *slot = cust;
10363 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10364 if (cust->user == NULL)
10365 cust->user = immediate_parent;
10366 }
10367 }
10368 else
10369 {
10370 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10371 if (cust->user == NULL)
10372 cust->user = immediate_parent;
10373 }
10374 }
10375
10376 for (ix = 0;
10377 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10378 ++ix)
10379 {
10380 recursively_compute_inclusions (result, all_children,
10381 all_type_symtabs, iter, cust);
10382 }
10383 }
10384
10385 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10386 PER_CU. */
10387
10388 static void
10389 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10390 {
10391 gdb_assert (! per_cu->is_debug_types);
10392
10393 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10394 {
10395 int ix, len;
10396 struct dwarf2_per_cu_data *per_cu_iter;
10397 struct compunit_symtab *compunit_symtab_iter;
10398 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10399 htab_t all_children, all_type_symtabs;
10400 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10401
10402 /* If we don't have a symtab, we can just skip this case. */
10403 if (cust == NULL)
10404 return;
10405
10406 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10407 NULL, xcalloc, xfree);
10408 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10409 NULL, xcalloc, xfree);
10410
10411 for (ix = 0;
10412 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10413 ix, per_cu_iter);
10414 ++ix)
10415 {
10416 recursively_compute_inclusions (&result_symtabs, all_children,
10417 all_type_symtabs, per_cu_iter,
10418 cust);
10419 }
10420
10421 /* Now we have a transitive closure of all the included symtabs. */
10422 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10423 cust->includes
10424 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10425 struct compunit_symtab *, len + 1);
10426 for (ix = 0;
10427 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10428 compunit_symtab_iter);
10429 ++ix)
10430 cust->includes[ix] = compunit_symtab_iter;
10431 cust->includes[len] = NULL;
10432
10433 VEC_free (compunit_symtab_ptr, result_symtabs);
10434 htab_delete (all_children);
10435 htab_delete (all_type_symtabs);
10436 }
10437 }
10438
10439 /* Compute the 'includes' field for the symtabs of all the CUs we just
10440 read. */
10441
10442 static void
10443 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10444 {
10445 int ix;
10446 struct dwarf2_per_cu_data *iter;
10447
10448 for (ix = 0;
10449 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10450 ix, iter);
10451 ++ix)
10452 {
10453 if (! iter->is_debug_types)
10454 compute_compunit_symtab_includes (iter);
10455 }
10456
10457 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10458 }
10459
10460 /* Generate full symbol information for PER_CU, whose DIEs have
10461 already been loaded into memory. */
10462
10463 static void
10464 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10465 enum language pretend_language)
10466 {
10467 struct dwarf2_cu *cu = per_cu->cu;
10468 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10469 struct objfile *objfile = dwarf2_per_objfile->objfile;
10470 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10471 CORE_ADDR lowpc, highpc;
10472 struct compunit_symtab *cust;
10473 CORE_ADDR baseaddr;
10474 struct block *static_block;
10475 CORE_ADDR addr;
10476
10477 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10478
10479 buildsym_init ();
10480 scoped_free_pendings free_pending;
10481
10482 /* Clear the list here in case something was left over. */
10483 cu->method_list.clear ();
10484
10485 cu->list_in_scope = &file_symbols;
10486
10487 cu->language = pretend_language;
10488 cu->language_defn = language_def (cu->language);
10489
10490 /* Do line number decoding in read_file_scope () */
10491 process_die (cu->dies, cu);
10492
10493 /* For now fudge the Go package. */
10494 if (cu->language == language_go)
10495 fixup_go_packaging (cu);
10496
10497 /* Now that we have processed all the DIEs in the CU, all the types
10498 should be complete, and it should now be safe to compute all of the
10499 physnames. */
10500 compute_delayed_physnames (cu);
10501
10502 /* Some compilers don't define a DW_AT_high_pc attribute for the
10503 compilation unit. If the DW_AT_high_pc is missing, synthesize
10504 it, by scanning the DIE's below the compilation unit. */
10505 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10506
10507 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10508 static_block = end_symtab_get_static_block (addr, 0, 1);
10509
10510 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10511 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10512 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10513 addrmap to help ensure it has an accurate map of pc values belonging to
10514 this comp unit. */
10515 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10516
10517 cust = end_symtab_from_static_block (static_block,
10518 SECT_OFF_TEXT (objfile), 0);
10519
10520 if (cust != NULL)
10521 {
10522 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10523
10524 /* Set symtab language to language from DW_AT_language. If the
10525 compilation is from a C file generated by language preprocessors, do
10526 not set the language if it was already deduced by start_subfile. */
10527 if (!(cu->language == language_c
10528 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10529 COMPUNIT_FILETABS (cust)->language = cu->language;
10530
10531 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10532 produce DW_AT_location with location lists but it can be possibly
10533 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10534 there were bugs in prologue debug info, fixed later in GCC-4.5
10535 by "unwind info for epilogues" patch (which is not directly related).
10536
10537 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10538 needed, it would be wrong due to missing DW_AT_producer there.
10539
10540 Still one can confuse GDB by using non-standard GCC compilation
10541 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10542 */
10543 if (cu->has_loclist && gcc_4_minor >= 5)
10544 cust->locations_valid = 1;
10545
10546 if (gcc_4_minor >= 5)
10547 cust->epilogue_unwind_valid = 1;
10548
10549 cust->call_site_htab = cu->call_site_htab;
10550 }
10551
10552 if (dwarf2_per_objfile->using_index)
10553 per_cu->v.quick->compunit_symtab = cust;
10554 else
10555 {
10556 struct partial_symtab *pst = per_cu->v.psymtab;
10557 pst->compunit_symtab = cust;
10558 pst->readin = 1;
10559 }
10560
10561 /* Push it for inclusion processing later. */
10562 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10563 }
10564
10565 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10566 already been loaded into memory. */
10567
10568 static void
10569 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10570 enum language pretend_language)
10571 {
10572 struct dwarf2_cu *cu = per_cu->cu;
10573 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10574 struct objfile *objfile = dwarf2_per_objfile->objfile;
10575 struct compunit_symtab *cust;
10576 struct signatured_type *sig_type;
10577
10578 gdb_assert (per_cu->is_debug_types);
10579 sig_type = (struct signatured_type *) per_cu;
10580
10581 buildsym_init ();
10582 scoped_free_pendings free_pending;
10583
10584 /* Clear the list here in case something was left over. */
10585 cu->method_list.clear ();
10586
10587 cu->list_in_scope = &file_symbols;
10588
10589 cu->language = pretend_language;
10590 cu->language_defn = language_def (cu->language);
10591
10592 /* The symbol tables are set up in read_type_unit_scope. */
10593 process_die (cu->dies, cu);
10594
10595 /* For now fudge the Go package. */
10596 if (cu->language == language_go)
10597 fixup_go_packaging (cu);
10598
10599 /* Now that we have processed all the DIEs in the CU, all the types
10600 should be complete, and it should now be safe to compute all of the
10601 physnames. */
10602 compute_delayed_physnames (cu);
10603
10604 /* TUs share symbol tables.
10605 If this is the first TU to use this symtab, complete the construction
10606 of it with end_expandable_symtab. Otherwise, complete the addition of
10607 this TU's symbols to the existing symtab. */
10608 if (sig_type->type_unit_group->compunit_symtab == NULL)
10609 {
10610 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10611 sig_type->type_unit_group->compunit_symtab = cust;
10612
10613 if (cust != NULL)
10614 {
10615 /* Set symtab language to language from DW_AT_language. If the
10616 compilation is from a C file generated by language preprocessors,
10617 do not set the language if it was already deduced by
10618 start_subfile. */
10619 if (!(cu->language == language_c
10620 && COMPUNIT_FILETABS (cust)->language != language_c))
10621 COMPUNIT_FILETABS (cust)->language = cu->language;
10622 }
10623 }
10624 else
10625 {
10626 augment_type_symtab ();
10627 cust = sig_type->type_unit_group->compunit_symtab;
10628 }
10629
10630 if (dwarf2_per_objfile->using_index)
10631 per_cu->v.quick->compunit_symtab = cust;
10632 else
10633 {
10634 struct partial_symtab *pst = per_cu->v.psymtab;
10635 pst->compunit_symtab = cust;
10636 pst->readin = 1;
10637 }
10638 }
10639
10640 /* Process an imported unit DIE. */
10641
10642 static void
10643 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10644 {
10645 struct attribute *attr;
10646
10647 /* For now we don't handle imported units in type units. */
10648 if (cu->per_cu->is_debug_types)
10649 {
10650 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10651 " supported in type units [in module %s]"),
10652 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10653 }
10654
10655 attr = dwarf2_attr (die, DW_AT_import, cu);
10656 if (attr != NULL)
10657 {
10658 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10659 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10660 dwarf2_per_cu_data *per_cu
10661 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10662 cu->per_cu->dwarf2_per_objfile);
10663
10664 /* If necessary, add it to the queue and load its DIEs. */
10665 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10666 load_full_comp_unit (per_cu, cu->language);
10667
10668 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10669 per_cu);
10670 }
10671 }
10672
10673 /* RAII object that represents a process_die scope: i.e.,
10674 starts/finishes processing a DIE. */
10675 class process_die_scope
10676 {
10677 public:
10678 process_die_scope (die_info *die, dwarf2_cu *cu)
10679 : m_die (die), m_cu (cu)
10680 {
10681 /* We should only be processing DIEs not already in process. */
10682 gdb_assert (!m_die->in_process);
10683 m_die->in_process = true;
10684 }
10685
10686 ~process_die_scope ()
10687 {
10688 m_die->in_process = false;
10689
10690 /* If we're done processing the DIE for the CU that owns the line
10691 header, we don't need the line header anymore. */
10692 if (m_cu->line_header_die_owner == m_die)
10693 {
10694 delete m_cu->line_header;
10695 m_cu->line_header = NULL;
10696 m_cu->line_header_die_owner = NULL;
10697 }
10698 }
10699
10700 private:
10701 die_info *m_die;
10702 dwarf2_cu *m_cu;
10703 };
10704
10705 /* Process a die and its children. */
10706
10707 static void
10708 process_die (struct die_info *die, struct dwarf2_cu *cu)
10709 {
10710 process_die_scope scope (die, cu);
10711
10712 switch (die->tag)
10713 {
10714 case DW_TAG_padding:
10715 break;
10716 case DW_TAG_compile_unit:
10717 case DW_TAG_partial_unit:
10718 read_file_scope (die, cu);
10719 break;
10720 case DW_TAG_type_unit:
10721 read_type_unit_scope (die, cu);
10722 break;
10723 case DW_TAG_subprogram:
10724 case DW_TAG_inlined_subroutine:
10725 read_func_scope (die, cu);
10726 break;
10727 case DW_TAG_lexical_block:
10728 case DW_TAG_try_block:
10729 case DW_TAG_catch_block:
10730 read_lexical_block_scope (die, cu);
10731 break;
10732 case DW_TAG_call_site:
10733 case DW_TAG_GNU_call_site:
10734 read_call_site_scope (die, cu);
10735 break;
10736 case DW_TAG_class_type:
10737 case DW_TAG_interface_type:
10738 case DW_TAG_structure_type:
10739 case DW_TAG_union_type:
10740 process_structure_scope (die, cu);
10741 break;
10742 case DW_TAG_enumeration_type:
10743 process_enumeration_scope (die, cu);
10744 break;
10745
10746 /* These dies have a type, but processing them does not create
10747 a symbol or recurse to process the children. Therefore we can
10748 read them on-demand through read_type_die. */
10749 case DW_TAG_subroutine_type:
10750 case DW_TAG_set_type:
10751 case DW_TAG_array_type:
10752 case DW_TAG_pointer_type:
10753 case DW_TAG_ptr_to_member_type:
10754 case DW_TAG_reference_type:
10755 case DW_TAG_rvalue_reference_type:
10756 case DW_TAG_string_type:
10757 break;
10758
10759 case DW_TAG_base_type:
10760 case DW_TAG_subrange_type:
10761 case DW_TAG_typedef:
10762 /* Add a typedef symbol for the type definition, if it has a
10763 DW_AT_name. */
10764 new_symbol (die, read_type_die (die, cu), cu);
10765 break;
10766 case DW_TAG_common_block:
10767 read_common_block (die, cu);
10768 break;
10769 case DW_TAG_common_inclusion:
10770 break;
10771 case DW_TAG_namespace:
10772 cu->processing_has_namespace_info = 1;
10773 read_namespace (die, cu);
10774 break;
10775 case DW_TAG_module:
10776 cu->processing_has_namespace_info = 1;
10777 read_module (die, cu);
10778 break;
10779 case DW_TAG_imported_declaration:
10780 cu->processing_has_namespace_info = 1;
10781 if (read_namespace_alias (die, cu))
10782 break;
10783 /* The declaration is not a global namespace alias: fall through. */
10784 case DW_TAG_imported_module:
10785 cu->processing_has_namespace_info = 1;
10786 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10787 || cu->language != language_fortran))
10788 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10789 dwarf_tag_name (die->tag));
10790 read_import_statement (die, cu);
10791 break;
10792
10793 case DW_TAG_imported_unit:
10794 process_imported_unit_die (die, cu);
10795 break;
10796
10797 case DW_TAG_variable:
10798 read_variable (die, cu);
10799 break;
10800
10801 default:
10802 new_symbol (die, NULL, cu);
10803 break;
10804 }
10805 }
10806 \f
10807 /* DWARF name computation. */
10808
10809 /* A helper function for dwarf2_compute_name which determines whether DIE
10810 needs to have the name of the scope prepended to the name listed in the
10811 die. */
10812
10813 static int
10814 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10815 {
10816 struct attribute *attr;
10817
10818 switch (die->tag)
10819 {
10820 case DW_TAG_namespace:
10821 case DW_TAG_typedef:
10822 case DW_TAG_class_type:
10823 case DW_TAG_interface_type:
10824 case DW_TAG_structure_type:
10825 case DW_TAG_union_type:
10826 case DW_TAG_enumeration_type:
10827 case DW_TAG_enumerator:
10828 case DW_TAG_subprogram:
10829 case DW_TAG_inlined_subroutine:
10830 case DW_TAG_member:
10831 case DW_TAG_imported_declaration:
10832 return 1;
10833
10834 case DW_TAG_variable:
10835 case DW_TAG_constant:
10836 /* We only need to prefix "globally" visible variables. These include
10837 any variable marked with DW_AT_external or any variable that
10838 lives in a namespace. [Variables in anonymous namespaces
10839 require prefixing, but they are not DW_AT_external.] */
10840
10841 if (dwarf2_attr (die, DW_AT_specification, cu))
10842 {
10843 struct dwarf2_cu *spec_cu = cu;
10844
10845 return die_needs_namespace (die_specification (die, &spec_cu),
10846 spec_cu);
10847 }
10848
10849 attr = dwarf2_attr (die, DW_AT_external, cu);
10850 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10851 && die->parent->tag != DW_TAG_module)
10852 return 0;
10853 /* A variable in a lexical block of some kind does not need a
10854 namespace, even though in C++ such variables may be external
10855 and have a mangled name. */
10856 if (die->parent->tag == DW_TAG_lexical_block
10857 || die->parent->tag == DW_TAG_try_block
10858 || die->parent->tag == DW_TAG_catch_block
10859 || die->parent->tag == DW_TAG_subprogram)
10860 return 0;
10861 return 1;
10862
10863 default:
10864 return 0;
10865 }
10866 }
10867
10868 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10869 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10870 defined for the given DIE. */
10871
10872 static struct attribute *
10873 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10874 {
10875 struct attribute *attr;
10876
10877 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10878 if (attr == NULL)
10879 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10880
10881 return attr;
10882 }
10883
10884 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10885 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10886 defined for the given DIE. */
10887
10888 static const char *
10889 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10890 {
10891 const char *linkage_name;
10892
10893 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10894 if (linkage_name == NULL)
10895 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10896
10897 return linkage_name;
10898 }
10899
10900 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10901 compute the physname for the object, which include a method's:
10902 - formal parameters (C++),
10903 - receiver type (Go),
10904
10905 The term "physname" is a bit confusing.
10906 For C++, for example, it is the demangled name.
10907 For Go, for example, it's the mangled name.
10908
10909 For Ada, return the DIE's linkage name rather than the fully qualified
10910 name. PHYSNAME is ignored..
10911
10912 The result is allocated on the objfile_obstack and canonicalized. */
10913
10914 static const char *
10915 dwarf2_compute_name (const char *name,
10916 struct die_info *die, struct dwarf2_cu *cu,
10917 int physname)
10918 {
10919 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10920
10921 if (name == NULL)
10922 name = dwarf2_name (die, cu);
10923
10924 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10925 but otherwise compute it by typename_concat inside GDB.
10926 FIXME: Actually this is not really true, or at least not always true.
10927 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10928 Fortran names because there is no mangling standard. So new_symbol
10929 will set the demangled name to the result of dwarf2_full_name, and it is
10930 the demangled name that GDB uses if it exists. */
10931 if (cu->language == language_ada
10932 || (cu->language == language_fortran && physname))
10933 {
10934 /* For Ada unit, we prefer the linkage name over the name, as
10935 the former contains the exported name, which the user expects
10936 to be able to reference. Ideally, we want the user to be able
10937 to reference this entity using either natural or linkage name,
10938 but we haven't started looking at this enhancement yet. */
10939 const char *linkage_name = dw2_linkage_name (die, cu);
10940
10941 if (linkage_name != NULL)
10942 return linkage_name;
10943 }
10944
10945 /* These are the only languages we know how to qualify names in. */
10946 if (name != NULL
10947 && (cu->language == language_cplus
10948 || cu->language == language_fortran || cu->language == language_d
10949 || cu->language == language_rust))
10950 {
10951 if (die_needs_namespace (die, cu))
10952 {
10953 const char *prefix;
10954 const char *canonical_name = NULL;
10955
10956 string_file buf;
10957
10958 prefix = determine_prefix (die, cu);
10959 if (*prefix != '\0')
10960 {
10961 char *prefixed_name = typename_concat (NULL, prefix, name,
10962 physname, cu);
10963
10964 buf.puts (prefixed_name);
10965 xfree (prefixed_name);
10966 }
10967 else
10968 buf.puts (name);
10969
10970 /* Template parameters may be specified in the DIE's DW_AT_name, or
10971 as children with DW_TAG_template_type_param or
10972 DW_TAG_value_type_param. If the latter, add them to the name
10973 here. If the name already has template parameters, then
10974 skip this step; some versions of GCC emit both, and
10975 it is more efficient to use the pre-computed name.
10976
10977 Something to keep in mind about this process: it is very
10978 unlikely, or in some cases downright impossible, to produce
10979 something that will match the mangled name of a function.
10980 If the definition of the function has the same debug info,
10981 we should be able to match up with it anyway. But fallbacks
10982 using the minimal symbol, for instance to find a method
10983 implemented in a stripped copy of libstdc++, will not work.
10984 If we do not have debug info for the definition, we will have to
10985 match them up some other way.
10986
10987 When we do name matching there is a related problem with function
10988 templates; two instantiated function templates are allowed to
10989 differ only by their return types, which we do not add here. */
10990
10991 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10992 {
10993 struct attribute *attr;
10994 struct die_info *child;
10995 int first = 1;
10996
10997 die->building_fullname = 1;
10998
10999 for (child = die->child; child != NULL; child = child->sibling)
11000 {
11001 struct type *type;
11002 LONGEST value;
11003 const gdb_byte *bytes;
11004 struct dwarf2_locexpr_baton *baton;
11005 struct value *v;
11006
11007 if (child->tag != DW_TAG_template_type_param
11008 && child->tag != DW_TAG_template_value_param)
11009 continue;
11010
11011 if (first)
11012 {
11013 buf.puts ("<");
11014 first = 0;
11015 }
11016 else
11017 buf.puts (", ");
11018
11019 attr = dwarf2_attr (child, DW_AT_type, cu);
11020 if (attr == NULL)
11021 {
11022 complaint (&symfile_complaints,
11023 _("template parameter missing DW_AT_type"));
11024 buf.puts ("UNKNOWN_TYPE");
11025 continue;
11026 }
11027 type = die_type (child, cu);
11028
11029 if (child->tag == DW_TAG_template_type_param)
11030 {
11031 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
11032 continue;
11033 }
11034
11035 attr = dwarf2_attr (child, DW_AT_const_value, cu);
11036 if (attr == NULL)
11037 {
11038 complaint (&symfile_complaints,
11039 _("template parameter missing "
11040 "DW_AT_const_value"));
11041 buf.puts ("UNKNOWN_VALUE");
11042 continue;
11043 }
11044
11045 dwarf2_const_value_attr (attr, type, name,
11046 &cu->comp_unit_obstack, cu,
11047 &value, &bytes, &baton);
11048
11049 if (TYPE_NOSIGN (type))
11050 /* GDB prints characters as NUMBER 'CHAR'. If that's
11051 changed, this can use value_print instead. */
11052 c_printchar (value, type, &buf);
11053 else
11054 {
11055 struct value_print_options opts;
11056
11057 if (baton != NULL)
11058 v = dwarf2_evaluate_loc_desc (type, NULL,
11059 baton->data,
11060 baton->size,
11061 baton->per_cu);
11062 else if (bytes != NULL)
11063 {
11064 v = allocate_value (type);
11065 memcpy (value_contents_writeable (v), bytes,
11066 TYPE_LENGTH (type));
11067 }
11068 else
11069 v = value_from_longest (type, value);
11070
11071 /* Specify decimal so that we do not depend on
11072 the radix. */
11073 get_formatted_print_options (&opts, 'd');
11074 opts.raw = 1;
11075 value_print (v, &buf, &opts);
11076 release_value (v);
11077 value_free (v);
11078 }
11079 }
11080
11081 die->building_fullname = 0;
11082
11083 if (!first)
11084 {
11085 /* Close the argument list, with a space if necessary
11086 (nested templates). */
11087 if (!buf.empty () && buf.string ().back () == '>')
11088 buf.puts (" >");
11089 else
11090 buf.puts (">");
11091 }
11092 }
11093
11094 /* For C++ methods, append formal parameter type
11095 information, if PHYSNAME. */
11096
11097 if (physname && die->tag == DW_TAG_subprogram
11098 && cu->language == language_cplus)
11099 {
11100 struct type *type = read_type_die (die, cu);
11101
11102 c_type_print_args (type, &buf, 1, cu->language,
11103 &type_print_raw_options);
11104
11105 if (cu->language == language_cplus)
11106 {
11107 /* Assume that an artificial first parameter is
11108 "this", but do not crash if it is not. RealView
11109 marks unnamed (and thus unused) parameters as
11110 artificial; there is no way to differentiate
11111 the two cases. */
11112 if (TYPE_NFIELDS (type) > 0
11113 && TYPE_FIELD_ARTIFICIAL (type, 0)
11114 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11115 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11116 0))))
11117 buf.puts (" const");
11118 }
11119 }
11120
11121 const std::string &intermediate_name = buf.string ();
11122
11123 if (cu->language == language_cplus)
11124 canonical_name
11125 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11126 &objfile->per_bfd->storage_obstack);
11127
11128 /* If we only computed INTERMEDIATE_NAME, or if
11129 INTERMEDIATE_NAME is already canonical, then we need to
11130 copy it to the appropriate obstack. */
11131 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11132 name = ((const char *)
11133 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11134 intermediate_name.c_str (),
11135 intermediate_name.length ()));
11136 else
11137 name = canonical_name;
11138 }
11139 }
11140
11141 return name;
11142 }
11143
11144 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11145 If scope qualifiers are appropriate they will be added. The result
11146 will be allocated on the storage_obstack, or NULL if the DIE does
11147 not have a name. NAME may either be from a previous call to
11148 dwarf2_name or NULL.
11149
11150 The output string will be canonicalized (if C++). */
11151
11152 static const char *
11153 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11154 {
11155 return dwarf2_compute_name (name, die, cu, 0);
11156 }
11157
11158 /* Construct a physname for the given DIE in CU. NAME may either be
11159 from a previous call to dwarf2_name or NULL. The result will be
11160 allocated on the objfile_objstack or NULL if the DIE does not have a
11161 name.
11162
11163 The output string will be canonicalized (if C++). */
11164
11165 static const char *
11166 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11167 {
11168 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11169 const char *retval, *mangled = NULL, *canon = NULL;
11170 int need_copy = 1;
11171
11172 /* In this case dwarf2_compute_name is just a shortcut not building anything
11173 on its own. */
11174 if (!die_needs_namespace (die, cu))
11175 return dwarf2_compute_name (name, die, cu, 1);
11176
11177 mangled = dw2_linkage_name (die, cu);
11178
11179 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11180 See https://github.com/rust-lang/rust/issues/32925. */
11181 if (cu->language == language_rust && mangled != NULL
11182 && strchr (mangled, '{') != NULL)
11183 mangled = NULL;
11184
11185 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11186 has computed. */
11187 gdb::unique_xmalloc_ptr<char> demangled;
11188 if (mangled != NULL)
11189 {
11190
11191 if (cu->language == language_go)
11192 {
11193 /* This is a lie, but we already lie to the caller new_symbol.
11194 new_symbol assumes we return the mangled name.
11195 This just undoes that lie until things are cleaned up. */
11196 }
11197 else
11198 {
11199 /* Use DMGL_RET_DROP for C++ template functions to suppress
11200 their return type. It is easier for GDB users to search
11201 for such functions as `name(params)' than `long name(params)'.
11202 In such case the minimal symbol names do not match the full
11203 symbol names but for template functions there is never a need
11204 to look up their definition from their declaration so
11205 the only disadvantage remains the minimal symbol variant
11206 `long name(params)' does not have the proper inferior type. */
11207 demangled.reset (gdb_demangle (mangled,
11208 (DMGL_PARAMS | DMGL_ANSI
11209 | DMGL_RET_DROP)));
11210 }
11211 if (demangled)
11212 canon = demangled.get ();
11213 else
11214 {
11215 canon = mangled;
11216 need_copy = 0;
11217 }
11218 }
11219
11220 if (canon == NULL || check_physname)
11221 {
11222 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11223
11224 if (canon != NULL && strcmp (physname, canon) != 0)
11225 {
11226 /* It may not mean a bug in GDB. The compiler could also
11227 compute DW_AT_linkage_name incorrectly. But in such case
11228 GDB would need to be bug-to-bug compatible. */
11229
11230 complaint (&symfile_complaints,
11231 _("Computed physname <%s> does not match demangled <%s> "
11232 "(from linkage <%s>) - DIE at %s [in module %s]"),
11233 physname, canon, mangled, sect_offset_str (die->sect_off),
11234 objfile_name (objfile));
11235
11236 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11237 is available here - over computed PHYSNAME. It is safer
11238 against both buggy GDB and buggy compilers. */
11239
11240 retval = canon;
11241 }
11242 else
11243 {
11244 retval = physname;
11245 need_copy = 0;
11246 }
11247 }
11248 else
11249 retval = canon;
11250
11251 if (need_copy)
11252 retval = ((const char *)
11253 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11254 retval, strlen (retval)));
11255
11256 return retval;
11257 }
11258
11259 /* Inspect DIE in CU for a namespace alias. If one exists, record
11260 a new symbol for it.
11261
11262 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11263
11264 static int
11265 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11266 {
11267 struct attribute *attr;
11268
11269 /* If the die does not have a name, this is not a namespace
11270 alias. */
11271 attr = dwarf2_attr (die, DW_AT_name, cu);
11272 if (attr != NULL)
11273 {
11274 int num;
11275 struct die_info *d = die;
11276 struct dwarf2_cu *imported_cu = cu;
11277
11278 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11279 keep inspecting DIEs until we hit the underlying import. */
11280 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11281 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11282 {
11283 attr = dwarf2_attr (d, DW_AT_import, cu);
11284 if (attr == NULL)
11285 break;
11286
11287 d = follow_die_ref (d, attr, &imported_cu);
11288 if (d->tag != DW_TAG_imported_declaration)
11289 break;
11290 }
11291
11292 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11293 {
11294 complaint (&symfile_complaints,
11295 _("DIE at %s has too many recursively imported "
11296 "declarations"), sect_offset_str (d->sect_off));
11297 return 0;
11298 }
11299
11300 if (attr != NULL)
11301 {
11302 struct type *type;
11303 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11304
11305 type = get_die_type_at_offset (sect_off, cu->per_cu);
11306 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11307 {
11308 /* This declaration is a global namespace alias. Add
11309 a symbol for it whose type is the aliased namespace. */
11310 new_symbol (die, type, cu);
11311 return 1;
11312 }
11313 }
11314 }
11315
11316 return 0;
11317 }
11318
11319 /* Return the using directives repository (global or local?) to use in the
11320 current context for LANGUAGE.
11321
11322 For Ada, imported declarations can materialize renamings, which *may* be
11323 global. However it is impossible (for now?) in DWARF to distinguish
11324 "external" imported declarations and "static" ones. As all imported
11325 declarations seem to be static in all other languages, make them all CU-wide
11326 global only in Ada. */
11327
11328 static struct using_direct **
11329 using_directives (enum language language)
11330 {
11331 if (language == language_ada && context_stack_depth == 0)
11332 return &global_using_directives;
11333 else
11334 return &local_using_directives;
11335 }
11336
11337 /* Read the import statement specified by the given die and record it. */
11338
11339 static void
11340 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11341 {
11342 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11343 struct attribute *import_attr;
11344 struct die_info *imported_die, *child_die;
11345 struct dwarf2_cu *imported_cu;
11346 const char *imported_name;
11347 const char *imported_name_prefix;
11348 const char *canonical_name;
11349 const char *import_alias;
11350 const char *imported_declaration = NULL;
11351 const char *import_prefix;
11352 std::vector<const char *> excludes;
11353
11354 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11355 if (import_attr == NULL)
11356 {
11357 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11358 dwarf_tag_name (die->tag));
11359 return;
11360 }
11361
11362 imported_cu = cu;
11363 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11364 imported_name = dwarf2_name (imported_die, imported_cu);
11365 if (imported_name == NULL)
11366 {
11367 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11368
11369 The import in the following code:
11370 namespace A
11371 {
11372 typedef int B;
11373 }
11374
11375 int main ()
11376 {
11377 using A::B;
11378 B b;
11379 return b;
11380 }
11381
11382 ...
11383 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11384 <52> DW_AT_decl_file : 1
11385 <53> DW_AT_decl_line : 6
11386 <54> DW_AT_import : <0x75>
11387 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11388 <59> DW_AT_name : B
11389 <5b> DW_AT_decl_file : 1
11390 <5c> DW_AT_decl_line : 2
11391 <5d> DW_AT_type : <0x6e>
11392 ...
11393 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11394 <76> DW_AT_byte_size : 4
11395 <77> DW_AT_encoding : 5 (signed)
11396
11397 imports the wrong die ( 0x75 instead of 0x58 ).
11398 This case will be ignored until the gcc bug is fixed. */
11399 return;
11400 }
11401
11402 /* Figure out the local name after import. */
11403 import_alias = dwarf2_name (die, cu);
11404
11405 /* Figure out where the statement is being imported to. */
11406 import_prefix = determine_prefix (die, cu);
11407
11408 /* Figure out what the scope of the imported die is and prepend it
11409 to the name of the imported die. */
11410 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11411
11412 if (imported_die->tag != DW_TAG_namespace
11413 && imported_die->tag != DW_TAG_module)
11414 {
11415 imported_declaration = imported_name;
11416 canonical_name = imported_name_prefix;
11417 }
11418 else if (strlen (imported_name_prefix) > 0)
11419 canonical_name = obconcat (&objfile->objfile_obstack,
11420 imported_name_prefix,
11421 (cu->language == language_d ? "." : "::"),
11422 imported_name, (char *) NULL);
11423 else
11424 canonical_name = imported_name;
11425
11426 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11427 for (child_die = die->child; child_die && child_die->tag;
11428 child_die = sibling_die (child_die))
11429 {
11430 /* DWARF-4: A Fortran use statement with a “rename list” may be
11431 represented by an imported module entry with an import attribute
11432 referring to the module and owned entries corresponding to those
11433 entities that are renamed as part of being imported. */
11434
11435 if (child_die->tag != DW_TAG_imported_declaration)
11436 {
11437 complaint (&symfile_complaints,
11438 _("child DW_TAG_imported_declaration expected "
11439 "- DIE at %s [in module %s]"),
11440 sect_offset_str (child_die->sect_off),
11441 objfile_name (objfile));
11442 continue;
11443 }
11444
11445 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11446 if (import_attr == NULL)
11447 {
11448 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11449 dwarf_tag_name (child_die->tag));
11450 continue;
11451 }
11452
11453 imported_cu = cu;
11454 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11455 &imported_cu);
11456 imported_name = dwarf2_name (imported_die, imported_cu);
11457 if (imported_name == NULL)
11458 {
11459 complaint (&symfile_complaints,
11460 _("child DW_TAG_imported_declaration has unknown "
11461 "imported name - DIE at %s [in module %s]"),
11462 sect_offset_str (child_die->sect_off),
11463 objfile_name (objfile));
11464 continue;
11465 }
11466
11467 excludes.push_back (imported_name);
11468
11469 process_die (child_die, cu);
11470 }
11471
11472 add_using_directive (using_directives (cu->language),
11473 import_prefix,
11474 canonical_name,
11475 import_alias,
11476 imported_declaration,
11477 excludes,
11478 0,
11479 &objfile->objfile_obstack);
11480 }
11481
11482 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11483 types, but gives them a size of zero. Starting with version 14,
11484 ICC is compatible with GCC. */
11485
11486 static int
11487 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11488 {
11489 if (!cu->checked_producer)
11490 check_producer (cu);
11491
11492 return cu->producer_is_icc_lt_14;
11493 }
11494
11495 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11496 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11497 this, it was first present in GCC release 4.3.0. */
11498
11499 static int
11500 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11501 {
11502 if (!cu->checked_producer)
11503 check_producer (cu);
11504
11505 return cu->producer_is_gcc_lt_4_3;
11506 }
11507
11508 static file_and_directory
11509 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11510 {
11511 file_and_directory res;
11512
11513 /* Find the filename. Do not use dwarf2_name here, since the filename
11514 is not a source language identifier. */
11515 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11516 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11517
11518 if (res.comp_dir == NULL
11519 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11520 && IS_ABSOLUTE_PATH (res.name))
11521 {
11522 res.comp_dir_storage = ldirname (res.name);
11523 if (!res.comp_dir_storage.empty ())
11524 res.comp_dir = res.comp_dir_storage.c_str ();
11525 }
11526 if (res.comp_dir != NULL)
11527 {
11528 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11529 directory, get rid of it. */
11530 const char *cp = strchr (res.comp_dir, ':');
11531
11532 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11533 res.comp_dir = cp + 1;
11534 }
11535
11536 if (res.name == NULL)
11537 res.name = "<unknown>";
11538
11539 return res;
11540 }
11541
11542 /* Handle DW_AT_stmt_list for a compilation unit.
11543 DIE is the DW_TAG_compile_unit die for CU.
11544 COMP_DIR is the compilation directory. LOWPC is passed to
11545 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11546
11547 static void
11548 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11549 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11550 {
11551 struct dwarf2_per_objfile *dwarf2_per_objfile
11552 = cu->per_cu->dwarf2_per_objfile;
11553 struct objfile *objfile = dwarf2_per_objfile->objfile;
11554 struct attribute *attr;
11555 struct line_header line_header_local;
11556 hashval_t line_header_local_hash;
11557 void **slot;
11558 int decode_mapping;
11559
11560 gdb_assert (! cu->per_cu->is_debug_types);
11561
11562 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11563 if (attr == NULL)
11564 return;
11565
11566 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11567
11568 /* The line header hash table is only created if needed (it exists to
11569 prevent redundant reading of the line table for partial_units).
11570 If we're given a partial_unit, we'll need it. If we're given a
11571 compile_unit, then use the line header hash table if it's already
11572 created, but don't create one just yet. */
11573
11574 if (dwarf2_per_objfile->line_header_hash == NULL
11575 && die->tag == DW_TAG_partial_unit)
11576 {
11577 dwarf2_per_objfile->line_header_hash
11578 = htab_create_alloc_ex (127, line_header_hash_voidp,
11579 line_header_eq_voidp,
11580 free_line_header_voidp,
11581 &objfile->objfile_obstack,
11582 hashtab_obstack_allocate,
11583 dummy_obstack_deallocate);
11584 }
11585
11586 line_header_local.sect_off = line_offset;
11587 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11588 line_header_local_hash = line_header_hash (&line_header_local);
11589 if (dwarf2_per_objfile->line_header_hash != NULL)
11590 {
11591 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11592 &line_header_local,
11593 line_header_local_hash, NO_INSERT);
11594
11595 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11596 is not present in *SLOT (since if there is something in *SLOT then
11597 it will be for a partial_unit). */
11598 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11599 {
11600 gdb_assert (*slot != NULL);
11601 cu->line_header = (struct line_header *) *slot;
11602 return;
11603 }
11604 }
11605
11606 /* dwarf_decode_line_header does not yet provide sufficient information.
11607 We always have to call also dwarf_decode_lines for it. */
11608 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11609 if (lh == NULL)
11610 return;
11611
11612 cu->line_header = lh.release ();
11613 cu->line_header_die_owner = die;
11614
11615 if (dwarf2_per_objfile->line_header_hash == NULL)
11616 slot = NULL;
11617 else
11618 {
11619 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11620 &line_header_local,
11621 line_header_local_hash, INSERT);
11622 gdb_assert (slot != NULL);
11623 }
11624 if (slot != NULL && *slot == NULL)
11625 {
11626 /* This newly decoded line number information unit will be owned
11627 by line_header_hash hash table. */
11628 *slot = cu->line_header;
11629 cu->line_header_die_owner = NULL;
11630 }
11631 else
11632 {
11633 /* We cannot free any current entry in (*slot) as that struct line_header
11634 may be already used by multiple CUs. Create only temporary decoded
11635 line_header for this CU - it may happen at most once for each line
11636 number information unit. And if we're not using line_header_hash
11637 then this is what we want as well. */
11638 gdb_assert (die->tag != DW_TAG_partial_unit);
11639 }
11640 decode_mapping = (die->tag != DW_TAG_partial_unit);
11641 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11642 decode_mapping);
11643
11644 }
11645
11646 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11647
11648 static void
11649 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11650 {
11651 struct dwarf2_per_objfile *dwarf2_per_objfile
11652 = cu->per_cu->dwarf2_per_objfile;
11653 struct objfile *objfile = dwarf2_per_objfile->objfile;
11654 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11655 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11656 CORE_ADDR highpc = ((CORE_ADDR) 0);
11657 struct attribute *attr;
11658 struct die_info *child_die;
11659 CORE_ADDR baseaddr;
11660
11661 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11662
11663 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11664
11665 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11666 from finish_block. */
11667 if (lowpc == ((CORE_ADDR) -1))
11668 lowpc = highpc;
11669 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11670
11671 file_and_directory fnd = find_file_and_directory (die, cu);
11672
11673 prepare_one_comp_unit (cu, die, cu->language);
11674
11675 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11676 standardised yet. As a workaround for the language detection we fall
11677 back to the DW_AT_producer string. */
11678 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11679 cu->language = language_opencl;
11680
11681 /* Similar hack for Go. */
11682 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11683 set_cu_language (DW_LANG_Go, cu);
11684
11685 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11686
11687 /* Decode line number information if present. We do this before
11688 processing child DIEs, so that the line header table is available
11689 for DW_AT_decl_file. */
11690 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11691
11692 /* Process all dies in compilation unit. */
11693 if (die->child != NULL)
11694 {
11695 child_die = die->child;
11696 while (child_die && child_die->tag)
11697 {
11698 process_die (child_die, cu);
11699 child_die = sibling_die (child_die);
11700 }
11701 }
11702
11703 /* Decode macro information, if present. Dwarf 2 macro information
11704 refers to information in the line number info statement program
11705 header, so we can only read it if we've read the header
11706 successfully. */
11707 attr = dwarf2_attr (die, DW_AT_macros, cu);
11708 if (attr == NULL)
11709 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11710 if (attr && cu->line_header)
11711 {
11712 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11713 complaint (&symfile_complaints,
11714 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11715
11716 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11717 }
11718 else
11719 {
11720 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11721 if (attr && cu->line_header)
11722 {
11723 unsigned int macro_offset = DW_UNSND (attr);
11724
11725 dwarf_decode_macros (cu, macro_offset, 0);
11726 }
11727 }
11728 }
11729
11730 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11731 Create the set of symtabs used by this TU, or if this TU is sharing
11732 symtabs with another TU and the symtabs have already been created
11733 then restore those symtabs in the line header.
11734 We don't need the pc/line-number mapping for type units. */
11735
11736 static void
11737 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
11738 {
11739 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11740 struct type_unit_group *tu_group;
11741 int first_time;
11742 struct attribute *attr;
11743 unsigned int i;
11744 struct signatured_type *sig_type;
11745
11746 gdb_assert (per_cu->is_debug_types);
11747 sig_type = (struct signatured_type *) per_cu;
11748
11749 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11750
11751 /* If we're using .gdb_index (includes -readnow) then
11752 per_cu->type_unit_group may not have been set up yet. */
11753 if (sig_type->type_unit_group == NULL)
11754 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11755 tu_group = sig_type->type_unit_group;
11756
11757 /* If we've already processed this stmt_list there's no real need to
11758 do it again, we could fake it and just recreate the part we need
11759 (file name,index -> symtab mapping). If data shows this optimization
11760 is useful we can do it then. */
11761 first_time = tu_group->compunit_symtab == NULL;
11762
11763 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11764 debug info. */
11765 line_header_up lh;
11766 if (attr != NULL)
11767 {
11768 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11769 lh = dwarf_decode_line_header (line_offset, cu);
11770 }
11771 if (lh == NULL)
11772 {
11773 if (first_time)
11774 dwarf2_start_symtab (cu, "", NULL, 0);
11775 else
11776 {
11777 gdb_assert (tu_group->symtabs == NULL);
11778 restart_symtab (tu_group->compunit_symtab, "", 0);
11779 }
11780 return;
11781 }
11782
11783 cu->line_header = lh.release ();
11784 cu->line_header_die_owner = die;
11785
11786 if (first_time)
11787 {
11788 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
11789
11790 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11791 still initializing it, and our caller (a few levels up)
11792 process_full_type_unit still needs to know if this is the first
11793 time. */
11794
11795 tu_group->num_symtabs = cu->line_header->file_names.size ();
11796 tu_group->symtabs = XNEWVEC (struct symtab *,
11797 cu->line_header->file_names.size ());
11798
11799 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11800 {
11801 file_entry &fe = cu->line_header->file_names[i];
11802
11803 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
11804
11805 if (current_subfile->symtab == NULL)
11806 {
11807 /* NOTE: start_subfile will recognize when it's been
11808 passed a file it has already seen. So we can't
11809 assume there's a simple mapping from
11810 cu->line_header->file_names to subfiles, plus
11811 cu->line_header->file_names may contain dups. */
11812 current_subfile->symtab
11813 = allocate_symtab (cust, current_subfile->name);
11814 }
11815
11816 fe.symtab = current_subfile->symtab;
11817 tu_group->symtabs[i] = fe.symtab;
11818 }
11819 }
11820 else
11821 {
11822 restart_symtab (tu_group->compunit_symtab, "", 0);
11823
11824 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11825 {
11826 file_entry &fe = cu->line_header->file_names[i];
11827
11828 fe.symtab = tu_group->symtabs[i];
11829 }
11830 }
11831
11832 /* The main symtab is allocated last. Type units don't have DW_AT_name
11833 so they don't have a "real" (so to speak) symtab anyway.
11834 There is later code that will assign the main symtab to all symbols
11835 that don't have one. We need to handle the case of a symbol with a
11836 missing symtab (DW_AT_decl_file) anyway. */
11837 }
11838
11839 /* Process DW_TAG_type_unit.
11840 For TUs we want to skip the first top level sibling if it's not the
11841 actual type being defined by this TU. In this case the first top
11842 level sibling is there to provide context only. */
11843
11844 static void
11845 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11846 {
11847 struct die_info *child_die;
11848
11849 prepare_one_comp_unit (cu, die, language_minimal);
11850
11851 /* Initialize (or reinitialize) the machinery for building symtabs.
11852 We do this before processing child DIEs, so that the line header table
11853 is available for DW_AT_decl_file. */
11854 setup_type_unit_groups (die, cu);
11855
11856 if (die->child != NULL)
11857 {
11858 child_die = die->child;
11859 while (child_die && child_die->tag)
11860 {
11861 process_die (child_die, cu);
11862 child_die = sibling_die (child_die);
11863 }
11864 }
11865 }
11866 \f
11867 /* DWO/DWP files.
11868
11869 http://gcc.gnu.org/wiki/DebugFission
11870 http://gcc.gnu.org/wiki/DebugFissionDWP
11871
11872 To simplify handling of both DWO files ("object" files with the DWARF info)
11873 and DWP files (a file with the DWOs packaged up into one file), we treat
11874 DWP files as having a collection of virtual DWO files. */
11875
11876 static hashval_t
11877 hash_dwo_file (const void *item)
11878 {
11879 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11880 hashval_t hash;
11881
11882 hash = htab_hash_string (dwo_file->dwo_name);
11883 if (dwo_file->comp_dir != NULL)
11884 hash += htab_hash_string (dwo_file->comp_dir);
11885 return hash;
11886 }
11887
11888 static int
11889 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11890 {
11891 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11892 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11893
11894 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11895 return 0;
11896 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11897 return lhs->comp_dir == rhs->comp_dir;
11898 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11899 }
11900
11901 /* Allocate a hash table for DWO files. */
11902
11903 static htab_t
11904 allocate_dwo_file_hash_table (struct objfile *objfile)
11905 {
11906 return htab_create_alloc_ex (41,
11907 hash_dwo_file,
11908 eq_dwo_file,
11909 NULL,
11910 &objfile->objfile_obstack,
11911 hashtab_obstack_allocate,
11912 dummy_obstack_deallocate);
11913 }
11914
11915 /* Lookup DWO file DWO_NAME. */
11916
11917 static void **
11918 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11919 const char *dwo_name,
11920 const char *comp_dir)
11921 {
11922 struct dwo_file find_entry;
11923 void **slot;
11924
11925 if (dwarf2_per_objfile->dwo_files == NULL)
11926 dwarf2_per_objfile->dwo_files
11927 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11928
11929 memset (&find_entry, 0, sizeof (find_entry));
11930 find_entry.dwo_name = dwo_name;
11931 find_entry.comp_dir = comp_dir;
11932 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11933
11934 return slot;
11935 }
11936
11937 static hashval_t
11938 hash_dwo_unit (const void *item)
11939 {
11940 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11941
11942 /* This drops the top 32 bits of the id, but is ok for a hash. */
11943 return dwo_unit->signature;
11944 }
11945
11946 static int
11947 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11948 {
11949 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11950 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11951
11952 /* The signature is assumed to be unique within the DWO file.
11953 So while object file CU dwo_id's always have the value zero,
11954 that's OK, assuming each object file DWO file has only one CU,
11955 and that's the rule for now. */
11956 return lhs->signature == rhs->signature;
11957 }
11958
11959 /* Allocate a hash table for DWO CUs,TUs.
11960 There is one of these tables for each of CUs,TUs for each DWO file. */
11961
11962 static htab_t
11963 allocate_dwo_unit_table (struct objfile *objfile)
11964 {
11965 /* Start out with a pretty small number.
11966 Generally DWO files contain only one CU and maybe some TUs. */
11967 return htab_create_alloc_ex (3,
11968 hash_dwo_unit,
11969 eq_dwo_unit,
11970 NULL,
11971 &objfile->objfile_obstack,
11972 hashtab_obstack_allocate,
11973 dummy_obstack_deallocate);
11974 }
11975
11976 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11977
11978 struct create_dwo_cu_data
11979 {
11980 struct dwo_file *dwo_file;
11981 struct dwo_unit dwo_unit;
11982 };
11983
11984 /* die_reader_func for create_dwo_cu. */
11985
11986 static void
11987 create_dwo_cu_reader (const struct die_reader_specs *reader,
11988 const gdb_byte *info_ptr,
11989 struct die_info *comp_unit_die,
11990 int has_children,
11991 void *datap)
11992 {
11993 struct dwarf2_cu *cu = reader->cu;
11994 sect_offset sect_off = cu->per_cu->sect_off;
11995 struct dwarf2_section_info *section = cu->per_cu->section;
11996 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11997 struct dwo_file *dwo_file = data->dwo_file;
11998 struct dwo_unit *dwo_unit = &data->dwo_unit;
11999 struct attribute *attr;
12000
12001 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
12002 if (attr == NULL)
12003 {
12004 complaint (&symfile_complaints,
12005 _("Dwarf Error: debug entry at offset %s is missing"
12006 " its dwo_id [in module %s]"),
12007 sect_offset_str (sect_off), dwo_file->dwo_name);
12008 return;
12009 }
12010
12011 dwo_unit->dwo_file = dwo_file;
12012 dwo_unit->signature = DW_UNSND (attr);
12013 dwo_unit->section = section;
12014 dwo_unit->sect_off = sect_off;
12015 dwo_unit->length = cu->per_cu->length;
12016
12017 if (dwarf_read_debug)
12018 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
12019 sect_offset_str (sect_off),
12020 hex_string (dwo_unit->signature));
12021 }
12022
12023 /* Create the dwo_units for the CUs in a DWO_FILE.
12024 Note: This function processes DWO files only, not DWP files. */
12025
12026 static void
12027 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12028 struct dwo_file &dwo_file, dwarf2_section_info &section,
12029 htab_t &cus_htab)
12030 {
12031 struct objfile *objfile = dwarf2_per_objfile->objfile;
12032 const gdb_byte *info_ptr, *end_ptr;
12033
12034 dwarf2_read_section (objfile, &section);
12035 info_ptr = section.buffer;
12036
12037 if (info_ptr == NULL)
12038 return;
12039
12040 if (dwarf_read_debug)
12041 {
12042 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
12043 get_section_name (&section),
12044 get_section_file_name (&section));
12045 }
12046
12047 end_ptr = info_ptr + section.size;
12048 while (info_ptr < end_ptr)
12049 {
12050 struct dwarf2_per_cu_data per_cu;
12051 struct create_dwo_cu_data create_dwo_cu_data;
12052 struct dwo_unit *dwo_unit;
12053 void **slot;
12054 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
12055
12056 memset (&create_dwo_cu_data.dwo_unit, 0,
12057 sizeof (create_dwo_cu_data.dwo_unit));
12058 memset (&per_cu, 0, sizeof (per_cu));
12059 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
12060 per_cu.is_debug_types = 0;
12061 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12062 per_cu.section = &section;
12063 create_dwo_cu_data.dwo_file = &dwo_file;
12064
12065 init_cutu_and_read_dies_no_follow (
12066 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12067 info_ptr += per_cu.length;
12068
12069 // If the unit could not be parsed, skip it.
12070 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12071 continue;
12072
12073 if (cus_htab == NULL)
12074 cus_htab = allocate_dwo_unit_table (objfile);
12075
12076 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12077 *dwo_unit = create_dwo_cu_data.dwo_unit;
12078 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12079 gdb_assert (slot != NULL);
12080 if (*slot != NULL)
12081 {
12082 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12083 sect_offset dup_sect_off = dup_cu->sect_off;
12084
12085 complaint (&symfile_complaints,
12086 _("debug cu entry at offset %s is duplicate to"
12087 " the entry at offset %s, signature %s"),
12088 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12089 hex_string (dwo_unit->signature));
12090 }
12091 *slot = (void *)dwo_unit;
12092 }
12093 }
12094
12095 /* DWP file .debug_{cu,tu}_index section format:
12096 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12097
12098 DWP Version 1:
12099
12100 Both index sections have the same format, and serve to map a 64-bit
12101 signature to a set of section numbers. Each section begins with a header,
12102 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12103 indexes, and a pool of 32-bit section numbers. The index sections will be
12104 aligned at 8-byte boundaries in the file.
12105
12106 The index section header consists of:
12107
12108 V, 32 bit version number
12109 -, 32 bits unused
12110 N, 32 bit number of compilation units or type units in the index
12111 M, 32 bit number of slots in the hash table
12112
12113 Numbers are recorded using the byte order of the application binary.
12114
12115 The hash table begins at offset 16 in the section, and consists of an array
12116 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12117 order of the application binary). Unused slots in the hash table are 0.
12118 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12119
12120 The parallel table begins immediately after the hash table
12121 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12122 array of 32-bit indexes (using the byte order of the application binary),
12123 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12124 table contains a 32-bit index into the pool of section numbers. For unused
12125 hash table slots, the corresponding entry in the parallel table will be 0.
12126
12127 The pool of section numbers begins immediately following the hash table
12128 (at offset 16 + 12 * M from the beginning of the section). The pool of
12129 section numbers consists of an array of 32-bit words (using the byte order
12130 of the application binary). Each item in the array is indexed starting
12131 from 0. The hash table entry provides the index of the first section
12132 number in the set. Additional section numbers in the set follow, and the
12133 set is terminated by a 0 entry (section number 0 is not used in ELF).
12134
12135 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12136 section must be the first entry in the set, and the .debug_abbrev.dwo must
12137 be the second entry. Other members of the set may follow in any order.
12138
12139 ---
12140
12141 DWP Version 2:
12142
12143 DWP Version 2 combines all the .debug_info, etc. sections into one,
12144 and the entries in the index tables are now offsets into these sections.
12145 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12146 section.
12147
12148 Index Section Contents:
12149 Header
12150 Hash Table of Signatures dwp_hash_table.hash_table
12151 Parallel Table of Indices dwp_hash_table.unit_table
12152 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12153 Table of Section Sizes dwp_hash_table.v2.sizes
12154
12155 The index section header consists of:
12156
12157 V, 32 bit version number
12158 L, 32 bit number of columns in the table of section offsets
12159 N, 32 bit number of compilation units or type units in the index
12160 M, 32 bit number of slots in the hash table
12161
12162 Numbers are recorded using the byte order of the application binary.
12163
12164 The hash table has the same format as version 1.
12165 The parallel table of indices has the same format as version 1,
12166 except that the entries are origin-1 indices into the table of sections
12167 offsets and the table of section sizes.
12168
12169 The table of offsets begins immediately following the parallel table
12170 (at offset 16 + 12 * M from the beginning of the section). The table is
12171 a two-dimensional array of 32-bit words (using the byte order of the
12172 application binary), with L columns and N+1 rows, in row-major order.
12173 Each row in the array is indexed starting from 0. The first row provides
12174 a key to the remaining rows: each column in this row provides an identifier
12175 for a debug section, and the offsets in the same column of subsequent rows
12176 refer to that section. The section identifiers are:
12177
12178 DW_SECT_INFO 1 .debug_info.dwo
12179 DW_SECT_TYPES 2 .debug_types.dwo
12180 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12181 DW_SECT_LINE 4 .debug_line.dwo
12182 DW_SECT_LOC 5 .debug_loc.dwo
12183 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12184 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12185 DW_SECT_MACRO 8 .debug_macro.dwo
12186
12187 The offsets provided by the CU and TU index sections are the base offsets
12188 for the contributions made by each CU or TU to the corresponding section
12189 in the package file. Each CU and TU header contains an abbrev_offset
12190 field, used to find the abbreviations table for that CU or TU within the
12191 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12192 be interpreted as relative to the base offset given in the index section.
12193 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12194 should be interpreted as relative to the base offset for .debug_line.dwo,
12195 and offsets into other debug sections obtained from DWARF attributes should
12196 also be interpreted as relative to the corresponding base offset.
12197
12198 The table of sizes begins immediately following the table of offsets.
12199 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12200 with L columns and N rows, in row-major order. Each row in the array is
12201 indexed starting from 1 (row 0 is shared by the two tables).
12202
12203 ---
12204
12205 Hash table lookup is handled the same in version 1 and 2:
12206
12207 We assume that N and M will not exceed 2^32 - 1.
12208 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12209
12210 Given a 64-bit compilation unit signature or a type signature S, an entry
12211 in the hash table is located as follows:
12212
12213 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12214 the low-order k bits all set to 1.
12215
12216 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12217
12218 3) If the hash table entry at index H matches the signature, use that
12219 entry. If the hash table entry at index H is unused (all zeroes),
12220 terminate the search: the signature is not present in the table.
12221
12222 4) Let H = (H + H') modulo M. Repeat at Step 3.
12223
12224 Because M > N and H' and M are relatively prime, the search is guaranteed
12225 to stop at an unused slot or find the match. */
12226
12227 /* Create a hash table to map DWO IDs to their CU/TU entry in
12228 .debug_{info,types}.dwo in DWP_FILE.
12229 Returns NULL if there isn't one.
12230 Note: This function processes DWP files only, not DWO files. */
12231
12232 static struct dwp_hash_table *
12233 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12234 struct dwp_file *dwp_file, int is_debug_types)
12235 {
12236 struct objfile *objfile = dwarf2_per_objfile->objfile;
12237 bfd *dbfd = dwp_file->dbfd;
12238 const gdb_byte *index_ptr, *index_end;
12239 struct dwarf2_section_info *index;
12240 uint32_t version, nr_columns, nr_units, nr_slots;
12241 struct dwp_hash_table *htab;
12242
12243 if (is_debug_types)
12244 index = &dwp_file->sections.tu_index;
12245 else
12246 index = &dwp_file->sections.cu_index;
12247
12248 if (dwarf2_section_empty_p (index))
12249 return NULL;
12250 dwarf2_read_section (objfile, index);
12251
12252 index_ptr = index->buffer;
12253 index_end = index_ptr + index->size;
12254
12255 version = read_4_bytes (dbfd, index_ptr);
12256 index_ptr += 4;
12257 if (version == 2)
12258 nr_columns = read_4_bytes (dbfd, index_ptr);
12259 else
12260 nr_columns = 0;
12261 index_ptr += 4;
12262 nr_units = read_4_bytes (dbfd, index_ptr);
12263 index_ptr += 4;
12264 nr_slots = read_4_bytes (dbfd, index_ptr);
12265 index_ptr += 4;
12266
12267 if (version != 1 && version != 2)
12268 {
12269 error (_("Dwarf Error: unsupported DWP file version (%s)"
12270 " [in module %s]"),
12271 pulongest (version), dwp_file->name);
12272 }
12273 if (nr_slots != (nr_slots & -nr_slots))
12274 {
12275 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12276 " is not power of 2 [in module %s]"),
12277 pulongest (nr_slots), dwp_file->name);
12278 }
12279
12280 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12281 htab->version = version;
12282 htab->nr_columns = nr_columns;
12283 htab->nr_units = nr_units;
12284 htab->nr_slots = nr_slots;
12285 htab->hash_table = index_ptr;
12286 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12287
12288 /* Exit early if the table is empty. */
12289 if (nr_slots == 0 || nr_units == 0
12290 || (version == 2 && nr_columns == 0))
12291 {
12292 /* All must be zero. */
12293 if (nr_slots != 0 || nr_units != 0
12294 || (version == 2 && nr_columns != 0))
12295 {
12296 complaint (&symfile_complaints,
12297 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12298 " all zero [in modules %s]"),
12299 dwp_file->name);
12300 }
12301 return htab;
12302 }
12303
12304 if (version == 1)
12305 {
12306 htab->section_pool.v1.indices =
12307 htab->unit_table + sizeof (uint32_t) * nr_slots;
12308 /* It's harder to decide whether the section is too small in v1.
12309 V1 is deprecated anyway so we punt. */
12310 }
12311 else
12312 {
12313 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12314 int *ids = htab->section_pool.v2.section_ids;
12315 /* Reverse map for error checking. */
12316 int ids_seen[DW_SECT_MAX + 1];
12317 int i;
12318
12319 if (nr_columns < 2)
12320 {
12321 error (_("Dwarf Error: bad DWP hash table, too few columns"
12322 " in section table [in module %s]"),
12323 dwp_file->name);
12324 }
12325 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12326 {
12327 error (_("Dwarf Error: bad DWP hash table, too many columns"
12328 " in section table [in module %s]"),
12329 dwp_file->name);
12330 }
12331 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12332 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12333 for (i = 0; i < nr_columns; ++i)
12334 {
12335 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12336
12337 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12338 {
12339 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12340 " in section table [in module %s]"),
12341 id, dwp_file->name);
12342 }
12343 if (ids_seen[id] != -1)
12344 {
12345 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12346 " id %d in section table [in module %s]"),
12347 id, dwp_file->name);
12348 }
12349 ids_seen[id] = i;
12350 ids[i] = id;
12351 }
12352 /* Must have exactly one info or types section. */
12353 if (((ids_seen[DW_SECT_INFO] != -1)
12354 + (ids_seen[DW_SECT_TYPES] != -1))
12355 != 1)
12356 {
12357 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12358 " DWO info/types section [in module %s]"),
12359 dwp_file->name);
12360 }
12361 /* Must have an abbrev section. */
12362 if (ids_seen[DW_SECT_ABBREV] == -1)
12363 {
12364 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12365 " section [in module %s]"),
12366 dwp_file->name);
12367 }
12368 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12369 htab->section_pool.v2.sizes =
12370 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12371 * nr_units * nr_columns);
12372 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12373 * nr_units * nr_columns))
12374 > index_end)
12375 {
12376 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12377 " [in module %s]"),
12378 dwp_file->name);
12379 }
12380 }
12381
12382 return htab;
12383 }
12384
12385 /* Update SECTIONS with the data from SECTP.
12386
12387 This function is like the other "locate" section routines that are
12388 passed to bfd_map_over_sections, but in this context the sections to
12389 read comes from the DWP V1 hash table, not the full ELF section table.
12390
12391 The result is non-zero for success, or zero if an error was found. */
12392
12393 static int
12394 locate_v1_virtual_dwo_sections (asection *sectp,
12395 struct virtual_v1_dwo_sections *sections)
12396 {
12397 const struct dwop_section_names *names = &dwop_section_names;
12398
12399 if (section_is_p (sectp->name, &names->abbrev_dwo))
12400 {
12401 /* There can be only one. */
12402 if (sections->abbrev.s.section != NULL)
12403 return 0;
12404 sections->abbrev.s.section = sectp;
12405 sections->abbrev.size = bfd_get_section_size (sectp);
12406 }
12407 else if (section_is_p (sectp->name, &names->info_dwo)
12408 || section_is_p (sectp->name, &names->types_dwo))
12409 {
12410 /* There can be only one. */
12411 if (sections->info_or_types.s.section != NULL)
12412 return 0;
12413 sections->info_or_types.s.section = sectp;
12414 sections->info_or_types.size = bfd_get_section_size (sectp);
12415 }
12416 else if (section_is_p (sectp->name, &names->line_dwo))
12417 {
12418 /* There can be only one. */
12419 if (sections->line.s.section != NULL)
12420 return 0;
12421 sections->line.s.section = sectp;
12422 sections->line.size = bfd_get_section_size (sectp);
12423 }
12424 else if (section_is_p (sectp->name, &names->loc_dwo))
12425 {
12426 /* There can be only one. */
12427 if (sections->loc.s.section != NULL)
12428 return 0;
12429 sections->loc.s.section = sectp;
12430 sections->loc.size = bfd_get_section_size (sectp);
12431 }
12432 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12433 {
12434 /* There can be only one. */
12435 if (sections->macinfo.s.section != NULL)
12436 return 0;
12437 sections->macinfo.s.section = sectp;
12438 sections->macinfo.size = bfd_get_section_size (sectp);
12439 }
12440 else if (section_is_p (sectp->name, &names->macro_dwo))
12441 {
12442 /* There can be only one. */
12443 if (sections->macro.s.section != NULL)
12444 return 0;
12445 sections->macro.s.section = sectp;
12446 sections->macro.size = bfd_get_section_size (sectp);
12447 }
12448 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12449 {
12450 /* There can be only one. */
12451 if (sections->str_offsets.s.section != NULL)
12452 return 0;
12453 sections->str_offsets.s.section = sectp;
12454 sections->str_offsets.size = bfd_get_section_size (sectp);
12455 }
12456 else
12457 {
12458 /* No other kind of section is valid. */
12459 return 0;
12460 }
12461
12462 return 1;
12463 }
12464
12465 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12466 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12467 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12468 This is for DWP version 1 files. */
12469
12470 static struct dwo_unit *
12471 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12472 struct dwp_file *dwp_file,
12473 uint32_t unit_index,
12474 const char *comp_dir,
12475 ULONGEST signature, int is_debug_types)
12476 {
12477 struct objfile *objfile = dwarf2_per_objfile->objfile;
12478 const struct dwp_hash_table *dwp_htab =
12479 is_debug_types ? dwp_file->tus : dwp_file->cus;
12480 bfd *dbfd = dwp_file->dbfd;
12481 const char *kind = is_debug_types ? "TU" : "CU";
12482 struct dwo_file *dwo_file;
12483 struct dwo_unit *dwo_unit;
12484 struct virtual_v1_dwo_sections sections;
12485 void **dwo_file_slot;
12486 int i;
12487
12488 gdb_assert (dwp_file->version == 1);
12489
12490 if (dwarf_read_debug)
12491 {
12492 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12493 kind,
12494 pulongest (unit_index), hex_string (signature),
12495 dwp_file->name);
12496 }
12497
12498 /* Fetch the sections of this DWO unit.
12499 Put a limit on the number of sections we look for so that bad data
12500 doesn't cause us to loop forever. */
12501
12502 #define MAX_NR_V1_DWO_SECTIONS \
12503 (1 /* .debug_info or .debug_types */ \
12504 + 1 /* .debug_abbrev */ \
12505 + 1 /* .debug_line */ \
12506 + 1 /* .debug_loc */ \
12507 + 1 /* .debug_str_offsets */ \
12508 + 1 /* .debug_macro or .debug_macinfo */ \
12509 + 1 /* trailing zero */)
12510
12511 memset (&sections, 0, sizeof (sections));
12512
12513 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12514 {
12515 asection *sectp;
12516 uint32_t section_nr =
12517 read_4_bytes (dbfd,
12518 dwp_htab->section_pool.v1.indices
12519 + (unit_index + i) * sizeof (uint32_t));
12520
12521 if (section_nr == 0)
12522 break;
12523 if (section_nr >= dwp_file->num_sections)
12524 {
12525 error (_("Dwarf Error: bad DWP hash table, section number too large"
12526 " [in module %s]"),
12527 dwp_file->name);
12528 }
12529
12530 sectp = dwp_file->elf_sections[section_nr];
12531 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12532 {
12533 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12534 " [in module %s]"),
12535 dwp_file->name);
12536 }
12537 }
12538
12539 if (i < 2
12540 || dwarf2_section_empty_p (&sections.info_or_types)
12541 || dwarf2_section_empty_p (&sections.abbrev))
12542 {
12543 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12544 " [in module %s]"),
12545 dwp_file->name);
12546 }
12547 if (i == MAX_NR_V1_DWO_SECTIONS)
12548 {
12549 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12550 " [in module %s]"),
12551 dwp_file->name);
12552 }
12553
12554 /* It's easier for the rest of the code if we fake a struct dwo_file and
12555 have dwo_unit "live" in that. At least for now.
12556
12557 The DWP file can be made up of a random collection of CUs and TUs.
12558 However, for each CU + set of TUs that came from the same original DWO
12559 file, we can combine them back into a virtual DWO file to save space
12560 (fewer struct dwo_file objects to allocate). Remember that for really
12561 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12562
12563 std::string virtual_dwo_name =
12564 string_printf ("virtual-dwo/%d-%d-%d-%d",
12565 get_section_id (&sections.abbrev),
12566 get_section_id (&sections.line),
12567 get_section_id (&sections.loc),
12568 get_section_id (&sections.str_offsets));
12569 /* Can we use an existing virtual DWO file? */
12570 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12571 virtual_dwo_name.c_str (),
12572 comp_dir);
12573 /* Create one if necessary. */
12574 if (*dwo_file_slot == NULL)
12575 {
12576 if (dwarf_read_debug)
12577 {
12578 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12579 virtual_dwo_name.c_str ());
12580 }
12581 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12582 dwo_file->dwo_name
12583 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12584 virtual_dwo_name.c_str (),
12585 virtual_dwo_name.size ());
12586 dwo_file->comp_dir = comp_dir;
12587 dwo_file->sections.abbrev = sections.abbrev;
12588 dwo_file->sections.line = sections.line;
12589 dwo_file->sections.loc = sections.loc;
12590 dwo_file->sections.macinfo = sections.macinfo;
12591 dwo_file->sections.macro = sections.macro;
12592 dwo_file->sections.str_offsets = sections.str_offsets;
12593 /* The "str" section is global to the entire DWP file. */
12594 dwo_file->sections.str = dwp_file->sections.str;
12595 /* The info or types section is assigned below to dwo_unit,
12596 there's no need to record it in dwo_file.
12597 Also, we can't simply record type sections in dwo_file because
12598 we record a pointer into the vector in dwo_unit. As we collect more
12599 types we'll grow the vector and eventually have to reallocate space
12600 for it, invalidating all copies of pointers into the previous
12601 contents. */
12602 *dwo_file_slot = dwo_file;
12603 }
12604 else
12605 {
12606 if (dwarf_read_debug)
12607 {
12608 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12609 virtual_dwo_name.c_str ());
12610 }
12611 dwo_file = (struct dwo_file *) *dwo_file_slot;
12612 }
12613
12614 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12615 dwo_unit->dwo_file = dwo_file;
12616 dwo_unit->signature = signature;
12617 dwo_unit->section =
12618 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12619 *dwo_unit->section = sections.info_or_types;
12620 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12621
12622 return dwo_unit;
12623 }
12624
12625 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12626 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12627 piece within that section used by a TU/CU, return a virtual section
12628 of just that piece. */
12629
12630 static struct dwarf2_section_info
12631 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12632 struct dwarf2_section_info *section,
12633 bfd_size_type offset, bfd_size_type size)
12634 {
12635 struct dwarf2_section_info result;
12636 asection *sectp;
12637
12638 gdb_assert (section != NULL);
12639 gdb_assert (!section->is_virtual);
12640
12641 memset (&result, 0, sizeof (result));
12642 result.s.containing_section = section;
12643 result.is_virtual = 1;
12644
12645 if (size == 0)
12646 return result;
12647
12648 sectp = get_section_bfd_section (section);
12649
12650 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12651 bounds of the real section. This is a pretty-rare event, so just
12652 flag an error (easier) instead of a warning and trying to cope. */
12653 if (sectp == NULL
12654 || offset + size > bfd_get_section_size (sectp))
12655 {
12656 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12657 " in section %s [in module %s]"),
12658 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12659 objfile_name (dwarf2_per_objfile->objfile));
12660 }
12661
12662 result.virtual_offset = offset;
12663 result.size = size;
12664 return result;
12665 }
12666
12667 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12668 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12669 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12670 This is for DWP version 2 files. */
12671
12672 static struct dwo_unit *
12673 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12674 struct dwp_file *dwp_file,
12675 uint32_t unit_index,
12676 const char *comp_dir,
12677 ULONGEST signature, int is_debug_types)
12678 {
12679 struct objfile *objfile = dwarf2_per_objfile->objfile;
12680 const struct dwp_hash_table *dwp_htab =
12681 is_debug_types ? dwp_file->tus : dwp_file->cus;
12682 bfd *dbfd = dwp_file->dbfd;
12683 const char *kind = is_debug_types ? "TU" : "CU";
12684 struct dwo_file *dwo_file;
12685 struct dwo_unit *dwo_unit;
12686 struct virtual_v2_dwo_sections sections;
12687 void **dwo_file_slot;
12688 int i;
12689
12690 gdb_assert (dwp_file->version == 2);
12691
12692 if (dwarf_read_debug)
12693 {
12694 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12695 kind,
12696 pulongest (unit_index), hex_string (signature),
12697 dwp_file->name);
12698 }
12699
12700 /* Fetch the section offsets of this DWO unit. */
12701
12702 memset (&sections, 0, sizeof (sections));
12703
12704 for (i = 0; i < dwp_htab->nr_columns; ++i)
12705 {
12706 uint32_t offset = read_4_bytes (dbfd,
12707 dwp_htab->section_pool.v2.offsets
12708 + (((unit_index - 1) * dwp_htab->nr_columns
12709 + i)
12710 * sizeof (uint32_t)));
12711 uint32_t size = read_4_bytes (dbfd,
12712 dwp_htab->section_pool.v2.sizes
12713 + (((unit_index - 1) * dwp_htab->nr_columns
12714 + i)
12715 * sizeof (uint32_t)));
12716
12717 switch (dwp_htab->section_pool.v2.section_ids[i])
12718 {
12719 case DW_SECT_INFO:
12720 case DW_SECT_TYPES:
12721 sections.info_or_types_offset = offset;
12722 sections.info_or_types_size = size;
12723 break;
12724 case DW_SECT_ABBREV:
12725 sections.abbrev_offset = offset;
12726 sections.abbrev_size = size;
12727 break;
12728 case DW_SECT_LINE:
12729 sections.line_offset = offset;
12730 sections.line_size = size;
12731 break;
12732 case DW_SECT_LOC:
12733 sections.loc_offset = offset;
12734 sections.loc_size = size;
12735 break;
12736 case DW_SECT_STR_OFFSETS:
12737 sections.str_offsets_offset = offset;
12738 sections.str_offsets_size = size;
12739 break;
12740 case DW_SECT_MACINFO:
12741 sections.macinfo_offset = offset;
12742 sections.macinfo_size = size;
12743 break;
12744 case DW_SECT_MACRO:
12745 sections.macro_offset = offset;
12746 sections.macro_size = size;
12747 break;
12748 }
12749 }
12750
12751 /* It's easier for the rest of the code if we fake a struct dwo_file and
12752 have dwo_unit "live" in that. At least for now.
12753
12754 The DWP file can be made up of a random collection of CUs and TUs.
12755 However, for each CU + set of TUs that came from the same original DWO
12756 file, we can combine them back into a virtual DWO file to save space
12757 (fewer struct dwo_file objects to allocate). Remember that for really
12758 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12759
12760 std::string virtual_dwo_name =
12761 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12762 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12763 (long) (sections.line_size ? sections.line_offset : 0),
12764 (long) (sections.loc_size ? sections.loc_offset : 0),
12765 (long) (sections.str_offsets_size
12766 ? sections.str_offsets_offset : 0));
12767 /* Can we use an existing virtual DWO file? */
12768 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12769 virtual_dwo_name.c_str (),
12770 comp_dir);
12771 /* Create one if necessary. */
12772 if (*dwo_file_slot == NULL)
12773 {
12774 if (dwarf_read_debug)
12775 {
12776 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12777 virtual_dwo_name.c_str ());
12778 }
12779 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12780 dwo_file->dwo_name
12781 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12782 virtual_dwo_name.c_str (),
12783 virtual_dwo_name.size ());
12784 dwo_file->comp_dir = comp_dir;
12785 dwo_file->sections.abbrev =
12786 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12787 sections.abbrev_offset, sections.abbrev_size);
12788 dwo_file->sections.line =
12789 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12790 sections.line_offset, sections.line_size);
12791 dwo_file->sections.loc =
12792 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12793 sections.loc_offset, sections.loc_size);
12794 dwo_file->sections.macinfo =
12795 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12796 sections.macinfo_offset, sections.macinfo_size);
12797 dwo_file->sections.macro =
12798 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12799 sections.macro_offset, sections.macro_size);
12800 dwo_file->sections.str_offsets =
12801 create_dwp_v2_section (dwarf2_per_objfile,
12802 &dwp_file->sections.str_offsets,
12803 sections.str_offsets_offset,
12804 sections.str_offsets_size);
12805 /* The "str" section is global to the entire DWP file. */
12806 dwo_file->sections.str = dwp_file->sections.str;
12807 /* The info or types section is assigned below to dwo_unit,
12808 there's no need to record it in dwo_file.
12809 Also, we can't simply record type sections in dwo_file because
12810 we record a pointer into the vector in dwo_unit. As we collect more
12811 types we'll grow the vector and eventually have to reallocate space
12812 for it, invalidating all copies of pointers into the previous
12813 contents. */
12814 *dwo_file_slot = dwo_file;
12815 }
12816 else
12817 {
12818 if (dwarf_read_debug)
12819 {
12820 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12821 virtual_dwo_name.c_str ());
12822 }
12823 dwo_file = (struct dwo_file *) *dwo_file_slot;
12824 }
12825
12826 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12827 dwo_unit->dwo_file = dwo_file;
12828 dwo_unit->signature = signature;
12829 dwo_unit->section =
12830 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12831 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12832 is_debug_types
12833 ? &dwp_file->sections.types
12834 : &dwp_file->sections.info,
12835 sections.info_or_types_offset,
12836 sections.info_or_types_size);
12837 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12838
12839 return dwo_unit;
12840 }
12841
12842 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12843 Returns NULL if the signature isn't found. */
12844
12845 static struct dwo_unit *
12846 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12847 struct dwp_file *dwp_file, const char *comp_dir,
12848 ULONGEST signature, int is_debug_types)
12849 {
12850 const struct dwp_hash_table *dwp_htab =
12851 is_debug_types ? dwp_file->tus : dwp_file->cus;
12852 bfd *dbfd = dwp_file->dbfd;
12853 uint32_t mask = dwp_htab->nr_slots - 1;
12854 uint32_t hash = signature & mask;
12855 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12856 unsigned int i;
12857 void **slot;
12858 struct dwo_unit find_dwo_cu;
12859
12860 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12861 find_dwo_cu.signature = signature;
12862 slot = htab_find_slot (is_debug_types
12863 ? dwp_file->loaded_tus
12864 : dwp_file->loaded_cus,
12865 &find_dwo_cu, INSERT);
12866
12867 if (*slot != NULL)
12868 return (struct dwo_unit *) *slot;
12869
12870 /* Use a for loop so that we don't loop forever on bad debug info. */
12871 for (i = 0; i < dwp_htab->nr_slots; ++i)
12872 {
12873 ULONGEST signature_in_table;
12874
12875 signature_in_table =
12876 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12877 if (signature_in_table == signature)
12878 {
12879 uint32_t unit_index =
12880 read_4_bytes (dbfd,
12881 dwp_htab->unit_table + hash * sizeof (uint32_t));
12882
12883 if (dwp_file->version == 1)
12884 {
12885 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12886 dwp_file, unit_index,
12887 comp_dir, signature,
12888 is_debug_types);
12889 }
12890 else
12891 {
12892 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12893 dwp_file, unit_index,
12894 comp_dir, signature,
12895 is_debug_types);
12896 }
12897 return (struct dwo_unit *) *slot;
12898 }
12899 if (signature_in_table == 0)
12900 return NULL;
12901 hash = (hash + hash2) & mask;
12902 }
12903
12904 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12905 " [in module %s]"),
12906 dwp_file->name);
12907 }
12908
12909 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12910 Open the file specified by FILE_NAME and hand it off to BFD for
12911 preliminary analysis. Return a newly initialized bfd *, which
12912 includes a canonicalized copy of FILE_NAME.
12913 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12914 SEARCH_CWD is true if the current directory is to be searched.
12915 It will be searched before debug-file-directory.
12916 If successful, the file is added to the bfd include table of the
12917 objfile's bfd (see gdb_bfd_record_inclusion).
12918 If unable to find/open the file, return NULL.
12919 NOTE: This function is derived from symfile_bfd_open. */
12920
12921 static gdb_bfd_ref_ptr
12922 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12923 const char *file_name, int is_dwp, int search_cwd)
12924 {
12925 int desc;
12926 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12927 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12928 to debug_file_directory. */
12929 const char *search_path;
12930 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12931
12932 gdb::unique_xmalloc_ptr<char> search_path_holder;
12933 if (search_cwd)
12934 {
12935 if (*debug_file_directory != '\0')
12936 {
12937 search_path_holder.reset (concat (".", dirname_separator_string,
12938 debug_file_directory,
12939 (char *) NULL));
12940 search_path = search_path_holder.get ();
12941 }
12942 else
12943 search_path = ".";
12944 }
12945 else
12946 search_path = debug_file_directory;
12947
12948 openp_flags flags = OPF_RETURN_REALPATH;
12949 if (is_dwp)
12950 flags |= OPF_SEARCH_IN_PATH;
12951
12952 gdb::unique_xmalloc_ptr<char> absolute_name;
12953 desc = openp (search_path, flags, file_name,
12954 O_RDONLY | O_BINARY, &absolute_name);
12955 if (desc < 0)
12956 return NULL;
12957
12958 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12959 gnutarget, desc));
12960 if (sym_bfd == NULL)
12961 return NULL;
12962 bfd_set_cacheable (sym_bfd.get (), 1);
12963
12964 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12965 return NULL;
12966
12967 /* Success. Record the bfd as having been included by the objfile's bfd.
12968 This is important because things like demangled_names_hash lives in the
12969 objfile's per_bfd space and may have references to things like symbol
12970 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12971 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12972
12973 return sym_bfd;
12974 }
12975
12976 /* Try to open DWO file FILE_NAME.
12977 COMP_DIR is the DW_AT_comp_dir attribute.
12978 The result is the bfd handle of the file.
12979 If there is a problem finding or opening the file, return NULL.
12980 Upon success, the canonicalized path of the file is stored in the bfd,
12981 same as symfile_bfd_open. */
12982
12983 static gdb_bfd_ref_ptr
12984 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12985 const char *file_name, const char *comp_dir)
12986 {
12987 if (IS_ABSOLUTE_PATH (file_name))
12988 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12989 0 /*is_dwp*/, 0 /*search_cwd*/);
12990
12991 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12992
12993 if (comp_dir != NULL)
12994 {
12995 char *path_to_try = concat (comp_dir, SLASH_STRING,
12996 file_name, (char *) NULL);
12997
12998 /* NOTE: If comp_dir is a relative path, this will also try the
12999 search path, which seems useful. */
13000 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
13001 path_to_try,
13002 0 /*is_dwp*/,
13003 1 /*search_cwd*/));
13004 xfree (path_to_try);
13005 if (abfd != NULL)
13006 return abfd;
13007 }
13008
13009 /* That didn't work, try debug-file-directory, which, despite its name,
13010 is a list of paths. */
13011
13012 if (*debug_file_directory == '\0')
13013 return NULL;
13014
13015 return try_open_dwop_file (dwarf2_per_objfile, file_name,
13016 0 /*is_dwp*/, 1 /*search_cwd*/);
13017 }
13018
13019 /* This function is mapped across the sections and remembers the offset and
13020 size of each of the DWO debugging sections we are interested in. */
13021
13022 static void
13023 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
13024 {
13025 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
13026 const struct dwop_section_names *names = &dwop_section_names;
13027
13028 if (section_is_p (sectp->name, &names->abbrev_dwo))
13029 {
13030 dwo_sections->abbrev.s.section = sectp;
13031 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
13032 }
13033 else if (section_is_p (sectp->name, &names->info_dwo))
13034 {
13035 dwo_sections->info.s.section = sectp;
13036 dwo_sections->info.size = bfd_get_section_size (sectp);
13037 }
13038 else if (section_is_p (sectp->name, &names->line_dwo))
13039 {
13040 dwo_sections->line.s.section = sectp;
13041 dwo_sections->line.size = bfd_get_section_size (sectp);
13042 }
13043 else if (section_is_p (sectp->name, &names->loc_dwo))
13044 {
13045 dwo_sections->loc.s.section = sectp;
13046 dwo_sections->loc.size = bfd_get_section_size (sectp);
13047 }
13048 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13049 {
13050 dwo_sections->macinfo.s.section = sectp;
13051 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
13052 }
13053 else if (section_is_p (sectp->name, &names->macro_dwo))
13054 {
13055 dwo_sections->macro.s.section = sectp;
13056 dwo_sections->macro.size = bfd_get_section_size (sectp);
13057 }
13058 else if (section_is_p (sectp->name, &names->str_dwo))
13059 {
13060 dwo_sections->str.s.section = sectp;
13061 dwo_sections->str.size = bfd_get_section_size (sectp);
13062 }
13063 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13064 {
13065 dwo_sections->str_offsets.s.section = sectp;
13066 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
13067 }
13068 else if (section_is_p (sectp->name, &names->types_dwo))
13069 {
13070 struct dwarf2_section_info type_section;
13071
13072 memset (&type_section, 0, sizeof (type_section));
13073 type_section.s.section = sectp;
13074 type_section.size = bfd_get_section_size (sectp);
13075 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
13076 &type_section);
13077 }
13078 }
13079
13080 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13081 by PER_CU. This is for the non-DWP case.
13082 The result is NULL if DWO_NAME can't be found. */
13083
13084 static struct dwo_file *
13085 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13086 const char *dwo_name, const char *comp_dir)
13087 {
13088 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13089 struct objfile *objfile = dwarf2_per_objfile->objfile;
13090 struct dwo_file *dwo_file;
13091 struct cleanup *cleanups;
13092
13093 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
13094 if (dbfd == NULL)
13095 {
13096 if (dwarf_read_debug)
13097 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13098 return NULL;
13099 }
13100 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
13101 dwo_file->dwo_name = dwo_name;
13102 dwo_file->comp_dir = comp_dir;
13103 dwo_file->dbfd = dbfd.release ();
13104
13105 free_dwo_file_cleanup_data *cleanup_data = XNEW (free_dwo_file_cleanup_data);
13106 cleanup_data->dwo_file = dwo_file;
13107 cleanup_data->dwarf2_per_objfile = dwarf2_per_objfile;
13108
13109 cleanups = make_cleanup (free_dwo_file_cleanup, cleanup_data);
13110
13111 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13112 &dwo_file->sections);
13113
13114 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13115 dwo_file->cus);
13116
13117 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file,
13118 dwo_file->sections.types, dwo_file->tus);
13119
13120 discard_cleanups (cleanups);
13121
13122 if (dwarf_read_debug)
13123 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13124
13125 return dwo_file;
13126 }
13127
13128 /* This function is mapped across the sections and remembers the offset and
13129 size of each of the DWP debugging sections common to version 1 and 2 that
13130 we are interested in. */
13131
13132 static void
13133 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13134 void *dwp_file_ptr)
13135 {
13136 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13137 const struct dwop_section_names *names = &dwop_section_names;
13138 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13139
13140 /* Record the ELF section number for later lookup: this is what the
13141 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13142 gdb_assert (elf_section_nr < dwp_file->num_sections);
13143 dwp_file->elf_sections[elf_section_nr] = sectp;
13144
13145 /* Look for specific sections that we need. */
13146 if (section_is_p (sectp->name, &names->str_dwo))
13147 {
13148 dwp_file->sections.str.s.section = sectp;
13149 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13150 }
13151 else if (section_is_p (sectp->name, &names->cu_index))
13152 {
13153 dwp_file->sections.cu_index.s.section = sectp;
13154 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13155 }
13156 else if (section_is_p (sectp->name, &names->tu_index))
13157 {
13158 dwp_file->sections.tu_index.s.section = sectp;
13159 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13160 }
13161 }
13162
13163 /* This function is mapped across the sections and remembers the offset and
13164 size of each of the DWP version 2 debugging sections that we are interested
13165 in. This is split into a separate function because we don't know if we
13166 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13167
13168 static void
13169 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13170 {
13171 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13172 const struct dwop_section_names *names = &dwop_section_names;
13173 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13174
13175 /* Record the ELF section number for later lookup: this is what the
13176 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13177 gdb_assert (elf_section_nr < dwp_file->num_sections);
13178 dwp_file->elf_sections[elf_section_nr] = sectp;
13179
13180 /* Look for specific sections that we need. */
13181 if (section_is_p (sectp->name, &names->abbrev_dwo))
13182 {
13183 dwp_file->sections.abbrev.s.section = sectp;
13184 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13185 }
13186 else if (section_is_p (sectp->name, &names->info_dwo))
13187 {
13188 dwp_file->sections.info.s.section = sectp;
13189 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13190 }
13191 else if (section_is_p (sectp->name, &names->line_dwo))
13192 {
13193 dwp_file->sections.line.s.section = sectp;
13194 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13195 }
13196 else if (section_is_p (sectp->name, &names->loc_dwo))
13197 {
13198 dwp_file->sections.loc.s.section = sectp;
13199 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13200 }
13201 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13202 {
13203 dwp_file->sections.macinfo.s.section = sectp;
13204 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13205 }
13206 else if (section_is_p (sectp->name, &names->macro_dwo))
13207 {
13208 dwp_file->sections.macro.s.section = sectp;
13209 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13210 }
13211 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13212 {
13213 dwp_file->sections.str_offsets.s.section = sectp;
13214 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13215 }
13216 else if (section_is_p (sectp->name, &names->types_dwo))
13217 {
13218 dwp_file->sections.types.s.section = sectp;
13219 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13220 }
13221 }
13222
13223 /* Hash function for dwp_file loaded CUs/TUs. */
13224
13225 static hashval_t
13226 hash_dwp_loaded_cutus (const void *item)
13227 {
13228 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13229
13230 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13231 return dwo_unit->signature;
13232 }
13233
13234 /* Equality function for dwp_file loaded CUs/TUs. */
13235
13236 static int
13237 eq_dwp_loaded_cutus (const void *a, const void *b)
13238 {
13239 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13240 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13241
13242 return dua->signature == dub->signature;
13243 }
13244
13245 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13246
13247 static htab_t
13248 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13249 {
13250 return htab_create_alloc_ex (3,
13251 hash_dwp_loaded_cutus,
13252 eq_dwp_loaded_cutus,
13253 NULL,
13254 &objfile->objfile_obstack,
13255 hashtab_obstack_allocate,
13256 dummy_obstack_deallocate);
13257 }
13258
13259 /* Try to open DWP file FILE_NAME.
13260 The result is the bfd handle of the file.
13261 If there is a problem finding or opening the file, return NULL.
13262 Upon success, the canonicalized path of the file is stored in the bfd,
13263 same as symfile_bfd_open. */
13264
13265 static gdb_bfd_ref_ptr
13266 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13267 const char *file_name)
13268 {
13269 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13270 1 /*is_dwp*/,
13271 1 /*search_cwd*/));
13272 if (abfd != NULL)
13273 return abfd;
13274
13275 /* Work around upstream bug 15652.
13276 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13277 [Whether that's a "bug" is debatable, but it is getting in our way.]
13278 We have no real idea where the dwp file is, because gdb's realpath-ing
13279 of the executable's path may have discarded the needed info.
13280 [IWBN if the dwp file name was recorded in the executable, akin to
13281 .gnu_debuglink, but that doesn't exist yet.]
13282 Strip the directory from FILE_NAME and search again. */
13283 if (*debug_file_directory != '\0')
13284 {
13285 /* Don't implicitly search the current directory here.
13286 If the user wants to search "." to handle this case,
13287 it must be added to debug-file-directory. */
13288 return try_open_dwop_file (dwarf2_per_objfile,
13289 lbasename (file_name), 1 /*is_dwp*/,
13290 0 /*search_cwd*/);
13291 }
13292
13293 return NULL;
13294 }
13295
13296 /* Initialize the use of the DWP file for the current objfile.
13297 By convention the name of the DWP file is ${objfile}.dwp.
13298 The result is NULL if it can't be found. */
13299
13300 static struct dwp_file *
13301 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13302 {
13303 struct objfile *objfile = dwarf2_per_objfile->objfile;
13304 struct dwp_file *dwp_file;
13305
13306 /* Try to find first .dwp for the binary file before any symbolic links
13307 resolving. */
13308
13309 /* If the objfile is a debug file, find the name of the real binary
13310 file and get the name of dwp file from there. */
13311 std::string dwp_name;
13312 if (objfile->separate_debug_objfile_backlink != NULL)
13313 {
13314 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13315 const char *backlink_basename = lbasename (backlink->original_name);
13316
13317 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13318 }
13319 else
13320 dwp_name = objfile->original_name;
13321
13322 dwp_name += ".dwp";
13323
13324 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13325 if (dbfd == NULL
13326 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13327 {
13328 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13329 dwp_name = objfile_name (objfile);
13330 dwp_name += ".dwp";
13331 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13332 }
13333
13334 if (dbfd == NULL)
13335 {
13336 if (dwarf_read_debug)
13337 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13338 return NULL;
13339 }
13340 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
13341 dwp_file->name = bfd_get_filename (dbfd.get ());
13342 dwp_file->dbfd = dbfd.release ();
13343
13344 /* +1: section 0 is unused */
13345 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13346 dwp_file->elf_sections =
13347 OBSTACK_CALLOC (&objfile->objfile_obstack,
13348 dwp_file->num_sections, asection *);
13349
13350 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13351 dwp_file);
13352
13353 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
13354
13355 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
13356
13357 /* The DWP file version is stored in the hash table. Oh well. */
13358 if (dwp_file->cus && dwp_file->tus
13359 && dwp_file->cus->version != dwp_file->tus->version)
13360 {
13361 /* Technically speaking, we should try to limp along, but this is
13362 pretty bizarre. We use pulongest here because that's the established
13363 portability solution (e.g, we cannot use %u for uint32_t). */
13364 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13365 " TU version %s [in DWP file %s]"),
13366 pulongest (dwp_file->cus->version),
13367 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13368 }
13369
13370 if (dwp_file->cus)
13371 dwp_file->version = dwp_file->cus->version;
13372 else if (dwp_file->tus)
13373 dwp_file->version = dwp_file->tus->version;
13374 else
13375 dwp_file->version = 2;
13376
13377 if (dwp_file->version == 2)
13378 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13379 dwp_file);
13380
13381 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13382 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13383
13384 if (dwarf_read_debug)
13385 {
13386 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13387 fprintf_unfiltered (gdb_stdlog,
13388 " %s CUs, %s TUs\n",
13389 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13390 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13391 }
13392
13393 return dwp_file;
13394 }
13395
13396 /* Wrapper around open_and_init_dwp_file, only open it once. */
13397
13398 static struct dwp_file *
13399 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13400 {
13401 if (! dwarf2_per_objfile->dwp_checked)
13402 {
13403 dwarf2_per_objfile->dwp_file
13404 = open_and_init_dwp_file (dwarf2_per_objfile);
13405 dwarf2_per_objfile->dwp_checked = 1;
13406 }
13407 return dwarf2_per_objfile->dwp_file;
13408 }
13409
13410 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13411 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13412 or in the DWP file for the objfile, referenced by THIS_UNIT.
13413 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13414 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13415
13416 This is called, for example, when wanting to read a variable with a
13417 complex location. Therefore we don't want to do file i/o for every call.
13418 Therefore we don't want to look for a DWO file on every call.
13419 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13420 then we check if we've already seen DWO_NAME, and only THEN do we check
13421 for a DWO file.
13422
13423 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13424 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13425
13426 static struct dwo_unit *
13427 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13428 const char *dwo_name, const char *comp_dir,
13429 ULONGEST signature, int is_debug_types)
13430 {
13431 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13432 struct objfile *objfile = dwarf2_per_objfile->objfile;
13433 const char *kind = is_debug_types ? "TU" : "CU";
13434 void **dwo_file_slot;
13435 struct dwo_file *dwo_file;
13436 struct dwp_file *dwp_file;
13437
13438 /* First see if there's a DWP file.
13439 If we have a DWP file but didn't find the DWO inside it, don't
13440 look for the original DWO file. It makes gdb behave differently
13441 depending on whether one is debugging in the build tree. */
13442
13443 dwp_file = get_dwp_file (dwarf2_per_objfile);
13444 if (dwp_file != NULL)
13445 {
13446 const struct dwp_hash_table *dwp_htab =
13447 is_debug_types ? dwp_file->tus : dwp_file->cus;
13448
13449 if (dwp_htab != NULL)
13450 {
13451 struct dwo_unit *dwo_cutu =
13452 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13453 signature, is_debug_types);
13454
13455 if (dwo_cutu != NULL)
13456 {
13457 if (dwarf_read_debug)
13458 {
13459 fprintf_unfiltered (gdb_stdlog,
13460 "Virtual DWO %s %s found: @%s\n",
13461 kind, hex_string (signature),
13462 host_address_to_string (dwo_cutu));
13463 }
13464 return dwo_cutu;
13465 }
13466 }
13467 }
13468 else
13469 {
13470 /* No DWP file, look for the DWO file. */
13471
13472 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13473 dwo_name, comp_dir);
13474 if (*dwo_file_slot == NULL)
13475 {
13476 /* Read in the file and build a table of the CUs/TUs it contains. */
13477 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13478 }
13479 /* NOTE: This will be NULL if unable to open the file. */
13480 dwo_file = (struct dwo_file *) *dwo_file_slot;
13481
13482 if (dwo_file != NULL)
13483 {
13484 struct dwo_unit *dwo_cutu = NULL;
13485
13486 if (is_debug_types && dwo_file->tus)
13487 {
13488 struct dwo_unit find_dwo_cutu;
13489
13490 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13491 find_dwo_cutu.signature = signature;
13492 dwo_cutu
13493 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13494 }
13495 else if (!is_debug_types && dwo_file->cus)
13496 {
13497 struct dwo_unit find_dwo_cutu;
13498
13499 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13500 find_dwo_cutu.signature = signature;
13501 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13502 &find_dwo_cutu);
13503 }
13504
13505 if (dwo_cutu != NULL)
13506 {
13507 if (dwarf_read_debug)
13508 {
13509 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13510 kind, dwo_name, hex_string (signature),
13511 host_address_to_string (dwo_cutu));
13512 }
13513 return dwo_cutu;
13514 }
13515 }
13516 }
13517
13518 /* We didn't find it. This could mean a dwo_id mismatch, or
13519 someone deleted the DWO/DWP file, or the search path isn't set up
13520 correctly to find the file. */
13521
13522 if (dwarf_read_debug)
13523 {
13524 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13525 kind, dwo_name, hex_string (signature));
13526 }
13527
13528 /* This is a warning and not a complaint because it can be caused by
13529 pilot error (e.g., user accidentally deleting the DWO). */
13530 {
13531 /* Print the name of the DWP file if we looked there, helps the user
13532 better diagnose the problem. */
13533 std::string dwp_text;
13534
13535 if (dwp_file != NULL)
13536 dwp_text = string_printf (" [in DWP file %s]",
13537 lbasename (dwp_file->name));
13538
13539 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13540 " [in module %s]"),
13541 kind, dwo_name, hex_string (signature),
13542 dwp_text.c_str (),
13543 this_unit->is_debug_types ? "TU" : "CU",
13544 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13545 }
13546 return NULL;
13547 }
13548
13549 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13550 See lookup_dwo_cutu_unit for details. */
13551
13552 static struct dwo_unit *
13553 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13554 const char *dwo_name, const char *comp_dir,
13555 ULONGEST signature)
13556 {
13557 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13558 }
13559
13560 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13561 See lookup_dwo_cutu_unit for details. */
13562
13563 static struct dwo_unit *
13564 lookup_dwo_type_unit (struct signatured_type *this_tu,
13565 const char *dwo_name, const char *comp_dir)
13566 {
13567 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13568 }
13569
13570 /* Traversal function for queue_and_load_all_dwo_tus. */
13571
13572 static int
13573 queue_and_load_dwo_tu (void **slot, void *info)
13574 {
13575 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13576 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13577 ULONGEST signature = dwo_unit->signature;
13578 struct signatured_type *sig_type =
13579 lookup_dwo_signatured_type (per_cu->cu, signature);
13580
13581 if (sig_type != NULL)
13582 {
13583 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13584
13585 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13586 a real dependency of PER_CU on SIG_TYPE. That is detected later
13587 while processing PER_CU. */
13588 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13589 load_full_type_unit (sig_cu);
13590 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13591 }
13592
13593 return 1;
13594 }
13595
13596 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13597 The DWO may have the only definition of the type, though it may not be
13598 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13599 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13600
13601 static void
13602 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13603 {
13604 struct dwo_unit *dwo_unit;
13605 struct dwo_file *dwo_file;
13606
13607 gdb_assert (!per_cu->is_debug_types);
13608 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13609 gdb_assert (per_cu->cu != NULL);
13610
13611 dwo_unit = per_cu->cu->dwo_unit;
13612 gdb_assert (dwo_unit != NULL);
13613
13614 dwo_file = dwo_unit->dwo_file;
13615 if (dwo_file->tus != NULL)
13616 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13617 }
13618
13619 /* Free all resources associated with DWO_FILE.
13620 Close the DWO file and munmap the sections.
13621 All memory should be on the objfile obstack. */
13622
13623 static void
13624 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
13625 {
13626
13627 /* Note: dbfd is NULL for virtual DWO files. */
13628 gdb_bfd_unref (dwo_file->dbfd);
13629
13630 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13631 }
13632
13633 /* Wrapper for free_dwo_file for use in cleanups. */
13634
13635 static void
13636 free_dwo_file_cleanup (void *arg)
13637 {
13638 struct free_dwo_file_cleanup_data *data
13639 = (struct free_dwo_file_cleanup_data *) arg;
13640 struct objfile *objfile = data->dwarf2_per_objfile->objfile;
13641
13642 free_dwo_file (data->dwo_file, objfile);
13643
13644 xfree (data);
13645 }
13646
13647 /* Traversal function for free_dwo_files. */
13648
13649 static int
13650 free_dwo_file_from_slot (void **slot, void *info)
13651 {
13652 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13653 struct objfile *objfile = (struct objfile *) info;
13654
13655 free_dwo_file (dwo_file, objfile);
13656
13657 return 1;
13658 }
13659
13660 /* Free all resources associated with DWO_FILES. */
13661
13662 static void
13663 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13664 {
13665 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13666 }
13667 \f
13668 /* Read in various DIEs. */
13669
13670 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13671 Inherit only the children of the DW_AT_abstract_origin DIE not being
13672 already referenced by DW_AT_abstract_origin from the children of the
13673 current DIE. */
13674
13675 static void
13676 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13677 {
13678 struct die_info *child_die;
13679 sect_offset *offsetp;
13680 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13681 struct die_info *origin_die;
13682 /* Iterator of the ORIGIN_DIE children. */
13683 struct die_info *origin_child_die;
13684 struct attribute *attr;
13685 struct dwarf2_cu *origin_cu;
13686 struct pending **origin_previous_list_in_scope;
13687
13688 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13689 if (!attr)
13690 return;
13691
13692 /* Note that following die references may follow to a die in a
13693 different cu. */
13694
13695 origin_cu = cu;
13696 origin_die = follow_die_ref (die, attr, &origin_cu);
13697
13698 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13699 symbols in. */
13700 origin_previous_list_in_scope = origin_cu->list_in_scope;
13701 origin_cu->list_in_scope = cu->list_in_scope;
13702
13703 if (die->tag != origin_die->tag
13704 && !(die->tag == DW_TAG_inlined_subroutine
13705 && origin_die->tag == DW_TAG_subprogram))
13706 complaint (&symfile_complaints,
13707 _("DIE %s and its abstract origin %s have different tags"),
13708 sect_offset_str (die->sect_off),
13709 sect_offset_str (origin_die->sect_off));
13710
13711 std::vector<sect_offset> offsets;
13712
13713 for (child_die = die->child;
13714 child_die && child_die->tag;
13715 child_die = sibling_die (child_die))
13716 {
13717 struct die_info *child_origin_die;
13718 struct dwarf2_cu *child_origin_cu;
13719
13720 /* We are trying to process concrete instance entries:
13721 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13722 it's not relevant to our analysis here. i.e. detecting DIEs that are
13723 present in the abstract instance but not referenced in the concrete
13724 one. */
13725 if (child_die->tag == DW_TAG_call_site
13726 || child_die->tag == DW_TAG_GNU_call_site)
13727 continue;
13728
13729 /* For each CHILD_DIE, find the corresponding child of
13730 ORIGIN_DIE. If there is more than one layer of
13731 DW_AT_abstract_origin, follow them all; there shouldn't be,
13732 but GCC versions at least through 4.4 generate this (GCC PR
13733 40573). */
13734 child_origin_die = child_die;
13735 child_origin_cu = cu;
13736 while (1)
13737 {
13738 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13739 child_origin_cu);
13740 if (attr == NULL)
13741 break;
13742 child_origin_die = follow_die_ref (child_origin_die, attr,
13743 &child_origin_cu);
13744 }
13745
13746 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13747 counterpart may exist. */
13748 if (child_origin_die != child_die)
13749 {
13750 if (child_die->tag != child_origin_die->tag
13751 && !(child_die->tag == DW_TAG_inlined_subroutine
13752 && child_origin_die->tag == DW_TAG_subprogram))
13753 complaint (&symfile_complaints,
13754 _("Child DIE %s and its abstract origin %s have "
13755 "different tags"),
13756 sect_offset_str (child_die->sect_off),
13757 sect_offset_str (child_origin_die->sect_off));
13758 if (child_origin_die->parent != origin_die)
13759 complaint (&symfile_complaints,
13760 _("Child DIE %s and its abstract origin %s have "
13761 "different parents"),
13762 sect_offset_str (child_die->sect_off),
13763 sect_offset_str (child_origin_die->sect_off));
13764 else
13765 offsets.push_back (child_origin_die->sect_off);
13766 }
13767 }
13768 std::sort (offsets.begin (), offsets.end ());
13769 sect_offset *offsets_end = offsets.data () + offsets.size ();
13770 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13771 if (offsetp[-1] == *offsetp)
13772 complaint (&symfile_complaints,
13773 _("Multiple children of DIE %s refer "
13774 "to DIE %s as their abstract origin"),
13775 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13776
13777 offsetp = offsets.data ();
13778 origin_child_die = origin_die->child;
13779 while (origin_child_die && origin_child_die->tag)
13780 {
13781 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13782 while (offsetp < offsets_end
13783 && *offsetp < origin_child_die->sect_off)
13784 offsetp++;
13785 if (offsetp >= offsets_end
13786 || *offsetp > origin_child_die->sect_off)
13787 {
13788 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13789 Check whether we're already processing ORIGIN_CHILD_DIE.
13790 This can happen with mutually referenced abstract_origins.
13791 PR 16581. */
13792 if (!origin_child_die->in_process)
13793 process_die (origin_child_die, origin_cu);
13794 }
13795 origin_child_die = sibling_die (origin_child_die);
13796 }
13797 origin_cu->list_in_scope = origin_previous_list_in_scope;
13798 }
13799
13800 static void
13801 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13802 {
13803 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13804 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13805 struct context_stack *newobj;
13806 CORE_ADDR lowpc;
13807 CORE_ADDR highpc;
13808 struct die_info *child_die;
13809 struct attribute *attr, *call_line, *call_file;
13810 const char *name;
13811 CORE_ADDR baseaddr;
13812 struct block *block;
13813 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13814 std::vector<struct symbol *> template_args;
13815 struct template_symbol *templ_func = NULL;
13816
13817 if (inlined_func)
13818 {
13819 /* If we do not have call site information, we can't show the
13820 caller of this inlined function. That's too confusing, so
13821 only use the scope for local variables. */
13822 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13823 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13824 if (call_line == NULL || call_file == NULL)
13825 {
13826 read_lexical_block_scope (die, cu);
13827 return;
13828 }
13829 }
13830
13831 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13832
13833 name = dwarf2_name (die, cu);
13834
13835 /* Ignore functions with missing or empty names. These are actually
13836 illegal according to the DWARF standard. */
13837 if (name == NULL)
13838 {
13839 complaint (&symfile_complaints,
13840 _("missing name for subprogram DIE at %s"),
13841 sect_offset_str (die->sect_off));
13842 return;
13843 }
13844
13845 /* Ignore functions with missing or invalid low and high pc attributes. */
13846 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13847 <= PC_BOUNDS_INVALID)
13848 {
13849 attr = dwarf2_attr (die, DW_AT_external, cu);
13850 if (!attr || !DW_UNSND (attr))
13851 complaint (&symfile_complaints,
13852 _("cannot get low and high bounds "
13853 "for subprogram DIE at %s"),
13854 sect_offset_str (die->sect_off));
13855 return;
13856 }
13857
13858 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13859 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13860
13861 /* If we have any template arguments, then we must allocate a
13862 different sort of symbol. */
13863 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13864 {
13865 if (child_die->tag == DW_TAG_template_type_param
13866 || child_die->tag == DW_TAG_template_value_param)
13867 {
13868 templ_func = allocate_template_symbol (objfile);
13869 templ_func->subclass = SYMBOL_TEMPLATE;
13870 break;
13871 }
13872 }
13873
13874 newobj = push_context (0, lowpc);
13875 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13876 (struct symbol *) templ_func);
13877
13878 /* If there is a location expression for DW_AT_frame_base, record
13879 it. */
13880 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13881 if (attr)
13882 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13883
13884 /* If there is a location for the static link, record it. */
13885 newobj->static_link = NULL;
13886 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13887 if (attr)
13888 {
13889 newobj->static_link
13890 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13891 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13892 }
13893
13894 cu->list_in_scope = &local_symbols;
13895
13896 if (die->child != NULL)
13897 {
13898 child_die = die->child;
13899 while (child_die && child_die->tag)
13900 {
13901 if (child_die->tag == DW_TAG_template_type_param
13902 || child_die->tag == DW_TAG_template_value_param)
13903 {
13904 struct symbol *arg = new_symbol (child_die, NULL, cu);
13905
13906 if (arg != NULL)
13907 template_args.push_back (arg);
13908 }
13909 else
13910 process_die (child_die, cu);
13911 child_die = sibling_die (child_die);
13912 }
13913 }
13914
13915 inherit_abstract_dies (die, cu);
13916
13917 /* If we have a DW_AT_specification, we might need to import using
13918 directives from the context of the specification DIE. See the
13919 comment in determine_prefix. */
13920 if (cu->language == language_cplus
13921 && dwarf2_attr (die, DW_AT_specification, cu))
13922 {
13923 struct dwarf2_cu *spec_cu = cu;
13924 struct die_info *spec_die = die_specification (die, &spec_cu);
13925
13926 while (spec_die)
13927 {
13928 child_die = spec_die->child;
13929 while (child_die && child_die->tag)
13930 {
13931 if (child_die->tag == DW_TAG_imported_module)
13932 process_die (child_die, spec_cu);
13933 child_die = sibling_die (child_die);
13934 }
13935
13936 /* In some cases, GCC generates specification DIEs that
13937 themselves contain DW_AT_specification attributes. */
13938 spec_die = die_specification (spec_die, &spec_cu);
13939 }
13940 }
13941
13942 newobj = pop_context ();
13943 /* Make a block for the local symbols within. */
13944 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
13945 newobj->static_link, lowpc, highpc);
13946
13947 /* For C++, set the block's scope. */
13948 if ((cu->language == language_cplus
13949 || cu->language == language_fortran
13950 || cu->language == language_d
13951 || cu->language == language_rust)
13952 && cu->processing_has_namespace_info)
13953 block_set_scope (block, determine_prefix (die, cu),
13954 &objfile->objfile_obstack);
13955
13956 /* If we have address ranges, record them. */
13957 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13958
13959 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
13960
13961 /* Attach template arguments to function. */
13962 if (!template_args.empty ())
13963 {
13964 gdb_assert (templ_func != NULL);
13965
13966 templ_func->n_template_arguments = template_args.size ();
13967 templ_func->template_arguments
13968 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13969 templ_func->n_template_arguments);
13970 memcpy (templ_func->template_arguments,
13971 template_args.data (),
13972 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13973 }
13974
13975 /* In C++, we can have functions nested inside functions (e.g., when
13976 a function declares a class that has methods). This means that
13977 when we finish processing a function scope, we may need to go
13978 back to building a containing block's symbol lists. */
13979 local_symbols = newobj->locals;
13980 local_using_directives = newobj->local_using_directives;
13981
13982 /* If we've finished processing a top-level function, subsequent
13983 symbols go in the file symbol list. */
13984 if (outermost_context_p ())
13985 cu->list_in_scope = &file_symbols;
13986 }
13987
13988 /* Process all the DIES contained within a lexical block scope. Start
13989 a new scope, process the dies, and then close the scope. */
13990
13991 static void
13992 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13993 {
13994 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13995 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13996 struct context_stack *newobj;
13997 CORE_ADDR lowpc, highpc;
13998 struct die_info *child_die;
13999 CORE_ADDR baseaddr;
14000
14001 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14002
14003 /* Ignore blocks with missing or invalid low and high pc attributes. */
14004 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
14005 as multiple lexical blocks? Handling children in a sane way would
14006 be nasty. Might be easier to properly extend generic blocks to
14007 describe ranges. */
14008 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
14009 {
14010 case PC_BOUNDS_NOT_PRESENT:
14011 /* DW_TAG_lexical_block has no attributes, process its children as if
14012 there was no wrapping by that DW_TAG_lexical_block.
14013 GCC does no longer produces such DWARF since GCC r224161. */
14014 for (child_die = die->child;
14015 child_die != NULL && child_die->tag;
14016 child_die = sibling_die (child_die))
14017 process_die (child_die, cu);
14018 return;
14019 case PC_BOUNDS_INVALID:
14020 return;
14021 }
14022 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14023 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
14024
14025 push_context (0, lowpc);
14026 if (die->child != NULL)
14027 {
14028 child_die = die->child;
14029 while (child_die && child_die->tag)
14030 {
14031 process_die (child_die, cu);
14032 child_die = sibling_die (child_die);
14033 }
14034 }
14035 inherit_abstract_dies (die, cu);
14036 newobj = pop_context ();
14037
14038 if (local_symbols != NULL || local_using_directives != NULL)
14039 {
14040 struct block *block
14041 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
14042 newobj->start_addr, highpc);
14043
14044 /* Note that recording ranges after traversing children, as we
14045 do here, means that recording a parent's ranges entails
14046 walking across all its children's ranges as they appear in
14047 the address map, which is quadratic behavior.
14048
14049 It would be nicer to record the parent's ranges before
14050 traversing its children, simply overriding whatever you find
14051 there. But since we don't even decide whether to create a
14052 block until after we've traversed its children, that's hard
14053 to do. */
14054 dwarf2_record_block_ranges (die, block, baseaddr, cu);
14055 }
14056 local_symbols = newobj->locals;
14057 local_using_directives = newobj->local_using_directives;
14058 }
14059
14060 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
14061
14062 static void
14063 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
14064 {
14065 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14066 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14067 CORE_ADDR pc, baseaddr;
14068 struct attribute *attr;
14069 struct call_site *call_site, call_site_local;
14070 void **slot;
14071 int nparams;
14072 struct die_info *child_die;
14073
14074 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14075
14076 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
14077 if (attr == NULL)
14078 {
14079 /* This was a pre-DWARF-5 GNU extension alias
14080 for DW_AT_call_return_pc. */
14081 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14082 }
14083 if (!attr)
14084 {
14085 complaint (&symfile_complaints,
14086 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
14087 "DIE %s [in module %s]"),
14088 sect_offset_str (die->sect_off), objfile_name (objfile));
14089 return;
14090 }
14091 pc = attr_value_as_address (attr) + baseaddr;
14092 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
14093
14094 if (cu->call_site_htab == NULL)
14095 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14096 NULL, &objfile->objfile_obstack,
14097 hashtab_obstack_allocate, NULL);
14098 call_site_local.pc = pc;
14099 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14100 if (*slot != NULL)
14101 {
14102 complaint (&symfile_complaints,
14103 _("Duplicate PC %s for DW_TAG_call_site "
14104 "DIE %s [in module %s]"),
14105 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
14106 objfile_name (objfile));
14107 return;
14108 }
14109
14110 /* Count parameters at the caller. */
14111
14112 nparams = 0;
14113 for (child_die = die->child; child_die && child_die->tag;
14114 child_die = sibling_die (child_die))
14115 {
14116 if (child_die->tag != DW_TAG_call_site_parameter
14117 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14118 {
14119 complaint (&symfile_complaints,
14120 _("Tag %d is not DW_TAG_call_site_parameter in "
14121 "DW_TAG_call_site child DIE %s [in module %s]"),
14122 child_die->tag, sect_offset_str (child_die->sect_off),
14123 objfile_name (objfile));
14124 continue;
14125 }
14126
14127 nparams++;
14128 }
14129
14130 call_site
14131 = ((struct call_site *)
14132 obstack_alloc (&objfile->objfile_obstack,
14133 sizeof (*call_site)
14134 + (sizeof (*call_site->parameter) * (nparams - 1))));
14135 *slot = call_site;
14136 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14137 call_site->pc = pc;
14138
14139 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14140 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14141 {
14142 struct die_info *func_die;
14143
14144 /* Skip also over DW_TAG_inlined_subroutine. */
14145 for (func_die = die->parent;
14146 func_die && func_die->tag != DW_TAG_subprogram
14147 && func_die->tag != DW_TAG_subroutine_type;
14148 func_die = func_die->parent);
14149
14150 /* DW_AT_call_all_calls is a superset
14151 of DW_AT_call_all_tail_calls. */
14152 if (func_die
14153 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14154 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14155 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14156 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14157 {
14158 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14159 not complete. But keep CALL_SITE for look ups via call_site_htab,
14160 both the initial caller containing the real return address PC and
14161 the final callee containing the current PC of a chain of tail
14162 calls do not need to have the tail call list complete. But any
14163 function candidate for a virtual tail call frame searched via
14164 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14165 determined unambiguously. */
14166 }
14167 else
14168 {
14169 struct type *func_type = NULL;
14170
14171 if (func_die)
14172 func_type = get_die_type (func_die, cu);
14173 if (func_type != NULL)
14174 {
14175 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14176
14177 /* Enlist this call site to the function. */
14178 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14179 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14180 }
14181 else
14182 complaint (&symfile_complaints,
14183 _("Cannot find function owning DW_TAG_call_site "
14184 "DIE %s [in module %s]"),
14185 sect_offset_str (die->sect_off), objfile_name (objfile));
14186 }
14187 }
14188
14189 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14190 if (attr == NULL)
14191 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14192 if (attr == NULL)
14193 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14194 if (attr == NULL)
14195 {
14196 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14197 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14198 }
14199 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14200 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14201 /* Keep NULL DWARF_BLOCK. */;
14202 else if (attr_form_is_block (attr))
14203 {
14204 struct dwarf2_locexpr_baton *dlbaton;
14205
14206 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14207 dlbaton->data = DW_BLOCK (attr)->data;
14208 dlbaton->size = DW_BLOCK (attr)->size;
14209 dlbaton->per_cu = cu->per_cu;
14210
14211 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14212 }
14213 else if (attr_form_is_ref (attr))
14214 {
14215 struct dwarf2_cu *target_cu = cu;
14216 struct die_info *target_die;
14217
14218 target_die = follow_die_ref (die, attr, &target_cu);
14219 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14220 if (die_is_declaration (target_die, target_cu))
14221 {
14222 const char *target_physname;
14223
14224 /* Prefer the mangled name; otherwise compute the demangled one. */
14225 target_physname = dw2_linkage_name (target_die, target_cu);
14226 if (target_physname == NULL)
14227 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14228 if (target_physname == NULL)
14229 complaint (&symfile_complaints,
14230 _("DW_AT_call_target target DIE has invalid "
14231 "physname, for referencing DIE %s [in module %s]"),
14232 sect_offset_str (die->sect_off), objfile_name (objfile));
14233 else
14234 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14235 }
14236 else
14237 {
14238 CORE_ADDR lowpc;
14239
14240 /* DW_AT_entry_pc should be preferred. */
14241 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14242 <= PC_BOUNDS_INVALID)
14243 complaint (&symfile_complaints,
14244 _("DW_AT_call_target target DIE has invalid "
14245 "low pc, for referencing DIE %s [in module %s]"),
14246 sect_offset_str (die->sect_off), objfile_name (objfile));
14247 else
14248 {
14249 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14250 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14251 }
14252 }
14253 }
14254 else
14255 complaint (&symfile_complaints,
14256 _("DW_TAG_call_site DW_AT_call_target is neither "
14257 "block nor reference, for DIE %s [in module %s]"),
14258 sect_offset_str (die->sect_off), objfile_name (objfile));
14259
14260 call_site->per_cu = cu->per_cu;
14261
14262 for (child_die = die->child;
14263 child_die && child_die->tag;
14264 child_die = sibling_die (child_die))
14265 {
14266 struct call_site_parameter *parameter;
14267 struct attribute *loc, *origin;
14268
14269 if (child_die->tag != DW_TAG_call_site_parameter
14270 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14271 {
14272 /* Already printed the complaint above. */
14273 continue;
14274 }
14275
14276 gdb_assert (call_site->parameter_count < nparams);
14277 parameter = &call_site->parameter[call_site->parameter_count];
14278
14279 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14280 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14281 register is contained in DW_AT_call_value. */
14282
14283 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14284 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14285 if (origin == NULL)
14286 {
14287 /* This was a pre-DWARF-5 GNU extension alias
14288 for DW_AT_call_parameter. */
14289 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14290 }
14291 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14292 {
14293 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14294
14295 sect_offset sect_off
14296 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14297 if (!offset_in_cu_p (&cu->header, sect_off))
14298 {
14299 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14300 binding can be done only inside one CU. Such referenced DIE
14301 therefore cannot be even moved to DW_TAG_partial_unit. */
14302 complaint (&symfile_complaints,
14303 _("DW_AT_call_parameter offset is not in CU for "
14304 "DW_TAG_call_site child DIE %s [in module %s]"),
14305 sect_offset_str (child_die->sect_off),
14306 objfile_name (objfile));
14307 continue;
14308 }
14309 parameter->u.param_cu_off
14310 = (cu_offset) (sect_off - cu->header.sect_off);
14311 }
14312 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14313 {
14314 complaint (&symfile_complaints,
14315 _("No DW_FORM_block* DW_AT_location for "
14316 "DW_TAG_call_site child DIE %s [in module %s]"),
14317 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14318 continue;
14319 }
14320 else
14321 {
14322 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14323 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14324 if (parameter->u.dwarf_reg != -1)
14325 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14326 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14327 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14328 &parameter->u.fb_offset))
14329 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14330 else
14331 {
14332 complaint (&symfile_complaints,
14333 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14334 "for DW_FORM_block* DW_AT_location is supported for "
14335 "DW_TAG_call_site child DIE %s "
14336 "[in module %s]"),
14337 sect_offset_str (child_die->sect_off),
14338 objfile_name (objfile));
14339 continue;
14340 }
14341 }
14342
14343 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14344 if (attr == NULL)
14345 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14346 if (!attr_form_is_block (attr))
14347 {
14348 complaint (&symfile_complaints,
14349 _("No DW_FORM_block* DW_AT_call_value for "
14350 "DW_TAG_call_site child DIE %s [in module %s]"),
14351 sect_offset_str (child_die->sect_off),
14352 objfile_name (objfile));
14353 continue;
14354 }
14355 parameter->value = DW_BLOCK (attr)->data;
14356 parameter->value_size = DW_BLOCK (attr)->size;
14357
14358 /* Parameters are not pre-cleared by memset above. */
14359 parameter->data_value = NULL;
14360 parameter->data_value_size = 0;
14361 call_site->parameter_count++;
14362
14363 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14364 if (attr == NULL)
14365 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14366 if (attr)
14367 {
14368 if (!attr_form_is_block (attr))
14369 complaint (&symfile_complaints,
14370 _("No DW_FORM_block* DW_AT_call_data_value for "
14371 "DW_TAG_call_site child DIE %s [in module %s]"),
14372 sect_offset_str (child_die->sect_off),
14373 objfile_name (objfile));
14374 else
14375 {
14376 parameter->data_value = DW_BLOCK (attr)->data;
14377 parameter->data_value_size = DW_BLOCK (attr)->size;
14378 }
14379 }
14380 }
14381 }
14382
14383 /* Helper function for read_variable. If DIE represents a virtual
14384 table, then return the type of the concrete object that is
14385 associated with the virtual table. Otherwise, return NULL. */
14386
14387 static struct type *
14388 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14389 {
14390 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14391 if (attr == NULL)
14392 return NULL;
14393
14394 /* Find the type DIE. */
14395 struct die_info *type_die = NULL;
14396 struct dwarf2_cu *type_cu = cu;
14397
14398 if (attr_form_is_ref (attr))
14399 type_die = follow_die_ref (die, attr, &type_cu);
14400 if (type_die == NULL)
14401 return NULL;
14402
14403 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14404 return NULL;
14405 return die_containing_type (type_die, type_cu);
14406 }
14407
14408 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14409
14410 static void
14411 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14412 {
14413 struct rust_vtable_symbol *storage = NULL;
14414
14415 if (cu->language == language_rust)
14416 {
14417 struct type *containing_type = rust_containing_type (die, cu);
14418
14419 if (containing_type != NULL)
14420 {
14421 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14422
14423 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14424 struct rust_vtable_symbol);
14425 initialize_objfile_symbol (storage);
14426 storage->concrete_type = containing_type;
14427 storage->subclass = SYMBOL_RUST_VTABLE;
14428 }
14429 }
14430
14431 new_symbol (die, NULL, cu, storage);
14432 }
14433
14434 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14435 reading .debug_rnglists.
14436 Callback's type should be:
14437 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14438 Return true if the attributes are present and valid, otherwise,
14439 return false. */
14440
14441 template <typename Callback>
14442 static bool
14443 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14444 Callback &&callback)
14445 {
14446 struct dwarf2_per_objfile *dwarf2_per_objfile
14447 = cu->per_cu->dwarf2_per_objfile;
14448 struct objfile *objfile = dwarf2_per_objfile->objfile;
14449 bfd *obfd = objfile->obfd;
14450 /* Base address selection entry. */
14451 CORE_ADDR base;
14452 int found_base;
14453 const gdb_byte *buffer;
14454 CORE_ADDR baseaddr;
14455 bool overflow = false;
14456
14457 found_base = cu->base_known;
14458 base = cu->base_address;
14459
14460 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14461 if (offset >= dwarf2_per_objfile->rnglists.size)
14462 {
14463 complaint (&symfile_complaints,
14464 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14465 offset);
14466 return false;
14467 }
14468 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14469
14470 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14471
14472 while (1)
14473 {
14474 /* Initialize it due to a false compiler warning. */
14475 CORE_ADDR range_beginning = 0, range_end = 0;
14476 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14477 + dwarf2_per_objfile->rnglists.size);
14478 unsigned int bytes_read;
14479
14480 if (buffer == buf_end)
14481 {
14482 overflow = true;
14483 break;
14484 }
14485 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14486 switch (rlet)
14487 {
14488 case DW_RLE_end_of_list:
14489 break;
14490 case DW_RLE_base_address:
14491 if (buffer + cu->header.addr_size > buf_end)
14492 {
14493 overflow = true;
14494 break;
14495 }
14496 base = read_address (obfd, buffer, cu, &bytes_read);
14497 found_base = 1;
14498 buffer += bytes_read;
14499 break;
14500 case DW_RLE_start_length:
14501 if (buffer + cu->header.addr_size > buf_end)
14502 {
14503 overflow = true;
14504 break;
14505 }
14506 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14507 buffer += bytes_read;
14508 range_end = (range_beginning
14509 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14510 buffer += bytes_read;
14511 if (buffer > buf_end)
14512 {
14513 overflow = true;
14514 break;
14515 }
14516 break;
14517 case DW_RLE_offset_pair:
14518 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14519 buffer += bytes_read;
14520 if (buffer > buf_end)
14521 {
14522 overflow = true;
14523 break;
14524 }
14525 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14526 buffer += bytes_read;
14527 if (buffer > buf_end)
14528 {
14529 overflow = true;
14530 break;
14531 }
14532 break;
14533 case DW_RLE_start_end:
14534 if (buffer + 2 * cu->header.addr_size > buf_end)
14535 {
14536 overflow = true;
14537 break;
14538 }
14539 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14540 buffer += bytes_read;
14541 range_end = read_address (obfd, buffer, cu, &bytes_read);
14542 buffer += bytes_read;
14543 break;
14544 default:
14545 complaint (&symfile_complaints,
14546 _("Invalid .debug_rnglists data (no base address)"));
14547 return false;
14548 }
14549 if (rlet == DW_RLE_end_of_list || overflow)
14550 break;
14551 if (rlet == DW_RLE_base_address)
14552 continue;
14553
14554 if (!found_base)
14555 {
14556 /* We have no valid base address for the ranges
14557 data. */
14558 complaint (&symfile_complaints,
14559 _("Invalid .debug_rnglists data (no base address)"));
14560 return false;
14561 }
14562
14563 if (range_beginning > range_end)
14564 {
14565 /* Inverted range entries are invalid. */
14566 complaint (&symfile_complaints,
14567 _("Invalid .debug_rnglists data (inverted range)"));
14568 return false;
14569 }
14570
14571 /* Empty range entries have no effect. */
14572 if (range_beginning == range_end)
14573 continue;
14574
14575 range_beginning += base;
14576 range_end += base;
14577
14578 /* A not-uncommon case of bad debug info.
14579 Don't pollute the addrmap with bad data. */
14580 if (range_beginning + baseaddr == 0
14581 && !dwarf2_per_objfile->has_section_at_zero)
14582 {
14583 complaint (&symfile_complaints,
14584 _(".debug_rnglists entry has start address of zero"
14585 " [in module %s]"), objfile_name (objfile));
14586 continue;
14587 }
14588
14589 callback (range_beginning, range_end);
14590 }
14591
14592 if (overflow)
14593 {
14594 complaint (&symfile_complaints,
14595 _("Offset %d is not terminated "
14596 "for DW_AT_ranges attribute"),
14597 offset);
14598 return false;
14599 }
14600
14601 return true;
14602 }
14603
14604 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14605 Callback's type should be:
14606 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14607 Return 1 if the attributes are present and valid, otherwise, return 0. */
14608
14609 template <typename Callback>
14610 static int
14611 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14612 Callback &&callback)
14613 {
14614 struct dwarf2_per_objfile *dwarf2_per_objfile
14615 = cu->per_cu->dwarf2_per_objfile;
14616 struct objfile *objfile = dwarf2_per_objfile->objfile;
14617 struct comp_unit_head *cu_header = &cu->header;
14618 bfd *obfd = objfile->obfd;
14619 unsigned int addr_size = cu_header->addr_size;
14620 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14621 /* Base address selection entry. */
14622 CORE_ADDR base;
14623 int found_base;
14624 unsigned int dummy;
14625 const gdb_byte *buffer;
14626 CORE_ADDR baseaddr;
14627
14628 if (cu_header->version >= 5)
14629 return dwarf2_rnglists_process (offset, cu, callback);
14630
14631 found_base = cu->base_known;
14632 base = cu->base_address;
14633
14634 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14635 if (offset >= dwarf2_per_objfile->ranges.size)
14636 {
14637 complaint (&symfile_complaints,
14638 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14639 offset);
14640 return 0;
14641 }
14642 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14643
14644 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14645
14646 while (1)
14647 {
14648 CORE_ADDR range_beginning, range_end;
14649
14650 range_beginning = read_address (obfd, buffer, cu, &dummy);
14651 buffer += addr_size;
14652 range_end = read_address (obfd, buffer, cu, &dummy);
14653 buffer += addr_size;
14654 offset += 2 * addr_size;
14655
14656 /* An end of list marker is a pair of zero addresses. */
14657 if (range_beginning == 0 && range_end == 0)
14658 /* Found the end of list entry. */
14659 break;
14660
14661 /* Each base address selection entry is a pair of 2 values.
14662 The first is the largest possible address, the second is
14663 the base address. Check for a base address here. */
14664 if ((range_beginning & mask) == mask)
14665 {
14666 /* If we found the largest possible address, then we already
14667 have the base address in range_end. */
14668 base = range_end;
14669 found_base = 1;
14670 continue;
14671 }
14672
14673 if (!found_base)
14674 {
14675 /* We have no valid base address for the ranges
14676 data. */
14677 complaint (&symfile_complaints,
14678 _("Invalid .debug_ranges data (no base address)"));
14679 return 0;
14680 }
14681
14682 if (range_beginning > range_end)
14683 {
14684 /* Inverted range entries are invalid. */
14685 complaint (&symfile_complaints,
14686 _("Invalid .debug_ranges data (inverted range)"));
14687 return 0;
14688 }
14689
14690 /* Empty range entries have no effect. */
14691 if (range_beginning == range_end)
14692 continue;
14693
14694 range_beginning += base;
14695 range_end += base;
14696
14697 /* A not-uncommon case of bad debug info.
14698 Don't pollute the addrmap with bad data. */
14699 if (range_beginning + baseaddr == 0
14700 && !dwarf2_per_objfile->has_section_at_zero)
14701 {
14702 complaint (&symfile_complaints,
14703 _(".debug_ranges entry has start address of zero"
14704 " [in module %s]"), objfile_name (objfile));
14705 continue;
14706 }
14707
14708 callback (range_beginning, range_end);
14709 }
14710
14711 return 1;
14712 }
14713
14714 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14715 Return 1 if the attributes are present and valid, otherwise, return 0.
14716 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14717
14718 static int
14719 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14720 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14721 struct partial_symtab *ranges_pst)
14722 {
14723 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14724 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14725 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14726 SECT_OFF_TEXT (objfile));
14727 int low_set = 0;
14728 CORE_ADDR low = 0;
14729 CORE_ADDR high = 0;
14730 int retval;
14731
14732 retval = dwarf2_ranges_process (offset, cu,
14733 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14734 {
14735 if (ranges_pst != NULL)
14736 {
14737 CORE_ADDR lowpc;
14738 CORE_ADDR highpc;
14739
14740 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14741 range_beginning + baseaddr);
14742 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14743 range_end + baseaddr);
14744 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14745 ranges_pst);
14746 }
14747
14748 /* FIXME: This is recording everything as a low-high
14749 segment of consecutive addresses. We should have a
14750 data structure for discontiguous block ranges
14751 instead. */
14752 if (! low_set)
14753 {
14754 low = range_beginning;
14755 high = range_end;
14756 low_set = 1;
14757 }
14758 else
14759 {
14760 if (range_beginning < low)
14761 low = range_beginning;
14762 if (range_end > high)
14763 high = range_end;
14764 }
14765 });
14766 if (!retval)
14767 return 0;
14768
14769 if (! low_set)
14770 /* If the first entry is an end-of-list marker, the range
14771 describes an empty scope, i.e. no instructions. */
14772 return 0;
14773
14774 if (low_return)
14775 *low_return = low;
14776 if (high_return)
14777 *high_return = high;
14778 return 1;
14779 }
14780
14781 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14782 definition for the return value. *LOWPC and *HIGHPC are set iff
14783 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14784
14785 static enum pc_bounds_kind
14786 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14787 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14788 struct partial_symtab *pst)
14789 {
14790 struct dwarf2_per_objfile *dwarf2_per_objfile
14791 = cu->per_cu->dwarf2_per_objfile;
14792 struct attribute *attr;
14793 struct attribute *attr_high;
14794 CORE_ADDR low = 0;
14795 CORE_ADDR high = 0;
14796 enum pc_bounds_kind ret;
14797
14798 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14799 if (attr_high)
14800 {
14801 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14802 if (attr)
14803 {
14804 low = attr_value_as_address (attr);
14805 high = attr_value_as_address (attr_high);
14806 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14807 high += low;
14808 }
14809 else
14810 /* Found high w/o low attribute. */
14811 return PC_BOUNDS_INVALID;
14812
14813 /* Found consecutive range of addresses. */
14814 ret = PC_BOUNDS_HIGH_LOW;
14815 }
14816 else
14817 {
14818 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14819 if (attr != NULL)
14820 {
14821 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14822 We take advantage of the fact that DW_AT_ranges does not appear
14823 in DW_TAG_compile_unit of DWO files. */
14824 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14825 unsigned int ranges_offset = (DW_UNSND (attr)
14826 + (need_ranges_base
14827 ? cu->ranges_base
14828 : 0));
14829
14830 /* Value of the DW_AT_ranges attribute is the offset in the
14831 .debug_ranges section. */
14832 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14833 return PC_BOUNDS_INVALID;
14834 /* Found discontinuous range of addresses. */
14835 ret = PC_BOUNDS_RANGES;
14836 }
14837 else
14838 return PC_BOUNDS_NOT_PRESENT;
14839 }
14840
14841 /* read_partial_die has also the strict LOW < HIGH requirement. */
14842 if (high <= low)
14843 return PC_BOUNDS_INVALID;
14844
14845 /* When using the GNU linker, .gnu.linkonce. sections are used to
14846 eliminate duplicate copies of functions and vtables and such.
14847 The linker will arbitrarily choose one and discard the others.
14848 The AT_*_pc values for such functions refer to local labels in
14849 these sections. If the section from that file was discarded, the
14850 labels are not in the output, so the relocs get a value of 0.
14851 If this is a discarded function, mark the pc bounds as invalid,
14852 so that GDB will ignore it. */
14853 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14854 return PC_BOUNDS_INVALID;
14855
14856 *lowpc = low;
14857 if (highpc)
14858 *highpc = high;
14859 return ret;
14860 }
14861
14862 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14863 its low and high PC addresses. Do nothing if these addresses could not
14864 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14865 and HIGHPC to the high address if greater than HIGHPC. */
14866
14867 static void
14868 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14869 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14870 struct dwarf2_cu *cu)
14871 {
14872 CORE_ADDR low, high;
14873 struct die_info *child = die->child;
14874
14875 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14876 {
14877 *lowpc = std::min (*lowpc, low);
14878 *highpc = std::max (*highpc, high);
14879 }
14880
14881 /* If the language does not allow nested subprograms (either inside
14882 subprograms or lexical blocks), we're done. */
14883 if (cu->language != language_ada)
14884 return;
14885
14886 /* Check all the children of the given DIE. If it contains nested
14887 subprograms, then check their pc bounds. Likewise, we need to
14888 check lexical blocks as well, as they may also contain subprogram
14889 definitions. */
14890 while (child && child->tag)
14891 {
14892 if (child->tag == DW_TAG_subprogram
14893 || child->tag == DW_TAG_lexical_block)
14894 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14895 child = sibling_die (child);
14896 }
14897 }
14898
14899 /* Get the low and high pc's represented by the scope DIE, and store
14900 them in *LOWPC and *HIGHPC. If the correct values can't be
14901 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14902
14903 static void
14904 get_scope_pc_bounds (struct die_info *die,
14905 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14906 struct dwarf2_cu *cu)
14907 {
14908 CORE_ADDR best_low = (CORE_ADDR) -1;
14909 CORE_ADDR best_high = (CORE_ADDR) 0;
14910 CORE_ADDR current_low, current_high;
14911
14912 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14913 >= PC_BOUNDS_RANGES)
14914 {
14915 best_low = current_low;
14916 best_high = current_high;
14917 }
14918 else
14919 {
14920 struct die_info *child = die->child;
14921
14922 while (child && child->tag)
14923 {
14924 switch (child->tag) {
14925 case DW_TAG_subprogram:
14926 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14927 break;
14928 case DW_TAG_namespace:
14929 case DW_TAG_module:
14930 /* FIXME: carlton/2004-01-16: Should we do this for
14931 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14932 that current GCC's always emit the DIEs corresponding
14933 to definitions of methods of classes as children of a
14934 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14935 the DIEs giving the declarations, which could be
14936 anywhere). But I don't see any reason why the
14937 standards says that they have to be there. */
14938 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14939
14940 if (current_low != ((CORE_ADDR) -1))
14941 {
14942 best_low = std::min (best_low, current_low);
14943 best_high = std::max (best_high, current_high);
14944 }
14945 break;
14946 default:
14947 /* Ignore. */
14948 break;
14949 }
14950
14951 child = sibling_die (child);
14952 }
14953 }
14954
14955 *lowpc = best_low;
14956 *highpc = best_high;
14957 }
14958
14959 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14960 in DIE. */
14961
14962 static void
14963 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14964 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14965 {
14966 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14967 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14968 struct attribute *attr;
14969 struct attribute *attr_high;
14970
14971 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14972 if (attr_high)
14973 {
14974 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14975 if (attr)
14976 {
14977 CORE_ADDR low = attr_value_as_address (attr);
14978 CORE_ADDR high = attr_value_as_address (attr_high);
14979
14980 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14981 high += low;
14982
14983 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14984 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14985 record_block_range (block, low, high - 1);
14986 }
14987 }
14988
14989 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14990 if (attr)
14991 {
14992 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14993 We take advantage of the fact that DW_AT_ranges does not appear
14994 in DW_TAG_compile_unit of DWO files. */
14995 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14996
14997 /* The value of the DW_AT_ranges attribute is the offset of the
14998 address range list in the .debug_ranges section. */
14999 unsigned long offset = (DW_UNSND (attr)
15000 + (need_ranges_base ? cu->ranges_base : 0));
15001 const gdb_byte *buffer;
15002
15003 /* For some target architectures, but not others, the
15004 read_address function sign-extends the addresses it returns.
15005 To recognize base address selection entries, we need a
15006 mask. */
15007 unsigned int addr_size = cu->header.addr_size;
15008 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
15009
15010 /* The base address, to which the next pair is relative. Note
15011 that this 'base' is a DWARF concept: most entries in a range
15012 list are relative, to reduce the number of relocs against the
15013 debugging information. This is separate from this function's
15014 'baseaddr' argument, which GDB uses to relocate debugging
15015 information from a shared library based on the address at
15016 which the library was loaded. */
15017 CORE_ADDR base = cu->base_address;
15018 int base_known = cu->base_known;
15019
15020 dwarf2_ranges_process (offset, cu,
15021 [&] (CORE_ADDR start, CORE_ADDR end)
15022 {
15023 start += baseaddr;
15024 end += baseaddr;
15025 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
15026 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
15027 record_block_range (block, start, end - 1);
15028 });
15029 }
15030 }
15031
15032 /* Check whether the producer field indicates either of GCC < 4.6, or the
15033 Intel C/C++ compiler, and cache the result in CU. */
15034
15035 static void
15036 check_producer (struct dwarf2_cu *cu)
15037 {
15038 int major, minor;
15039
15040 if (cu->producer == NULL)
15041 {
15042 /* For unknown compilers expect their behavior is DWARF version
15043 compliant.
15044
15045 GCC started to support .debug_types sections by -gdwarf-4 since
15046 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
15047 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
15048 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
15049 interpreted incorrectly by GDB now - GCC PR debug/48229. */
15050 }
15051 else if (producer_is_gcc (cu->producer, &major, &minor))
15052 {
15053 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
15054 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
15055 }
15056 else if (producer_is_icc (cu->producer, &major, &minor))
15057 cu->producer_is_icc_lt_14 = major < 14;
15058 else
15059 {
15060 /* For other non-GCC compilers, expect their behavior is DWARF version
15061 compliant. */
15062 }
15063
15064 cu->checked_producer = 1;
15065 }
15066
15067 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
15068 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
15069 during 4.6.0 experimental. */
15070
15071 static int
15072 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
15073 {
15074 if (!cu->checked_producer)
15075 check_producer (cu);
15076
15077 return cu->producer_is_gxx_lt_4_6;
15078 }
15079
15080 /* Return the default accessibility type if it is not overriden by
15081 DW_AT_accessibility. */
15082
15083 static enum dwarf_access_attribute
15084 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15085 {
15086 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15087 {
15088 /* The default DWARF 2 accessibility for members is public, the default
15089 accessibility for inheritance is private. */
15090
15091 if (die->tag != DW_TAG_inheritance)
15092 return DW_ACCESS_public;
15093 else
15094 return DW_ACCESS_private;
15095 }
15096 else
15097 {
15098 /* DWARF 3+ defines the default accessibility a different way. The same
15099 rules apply now for DW_TAG_inheritance as for the members and it only
15100 depends on the container kind. */
15101
15102 if (die->parent->tag == DW_TAG_class_type)
15103 return DW_ACCESS_private;
15104 else
15105 return DW_ACCESS_public;
15106 }
15107 }
15108
15109 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15110 offset. If the attribute was not found return 0, otherwise return
15111 1. If it was found but could not properly be handled, set *OFFSET
15112 to 0. */
15113
15114 static int
15115 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15116 LONGEST *offset)
15117 {
15118 struct attribute *attr;
15119
15120 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15121 if (attr != NULL)
15122 {
15123 *offset = 0;
15124
15125 /* Note that we do not check for a section offset first here.
15126 This is because DW_AT_data_member_location is new in DWARF 4,
15127 so if we see it, we can assume that a constant form is really
15128 a constant and not a section offset. */
15129 if (attr_form_is_constant (attr))
15130 *offset = dwarf2_get_attr_constant_value (attr, 0);
15131 else if (attr_form_is_section_offset (attr))
15132 dwarf2_complex_location_expr_complaint ();
15133 else if (attr_form_is_block (attr))
15134 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15135 else
15136 dwarf2_complex_location_expr_complaint ();
15137
15138 return 1;
15139 }
15140
15141 return 0;
15142 }
15143
15144 /* Add an aggregate field to the field list. */
15145
15146 static void
15147 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15148 struct dwarf2_cu *cu)
15149 {
15150 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15151 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15152 struct nextfield *new_field;
15153 struct attribute *attr;
15154 struct field *fp;
15155 const char *fieldname = "";
15156
15157 /* Allocate a new field list entry and link it in. */
15158 new_field = XNEW (struct nextfield);
15159 make_cleanup (xfree, new_field);
15160 memset (new_field, 0, sizeof (struct nextfield));
15161
15162 if (die->tag == DW_TAG_inheritance)
15163 {
15164 new_field->next = fip->baseclasses;
15165 fip->baseclasses = new_field;
15166 }
15167 else
15168 {
15169 new_field->next = fip->fields;
15170 fip->fields = new_field;
15171 }
15172 fip->nfields++;
15173
15174 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15175 if (attr)
15176 new_field->accessibility = DW_UNSND (attr);
15177 else
15178 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15179 if (new_field->accessibility != DW_ACCESS_public)
15180 fip->non_public_fields = 1;
15181
15182 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15183 if (attr)
15184 new_field->virtuality = DW_UNSND (attr);
15185 else
15186 new_field->virtuality = DW_VIRTUALITY_none;
15187
15188 fp = &new_field->field;
15189
15190 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15191 {
15192 LONGEST offset;
15193
15194 /* Data member other than a C++ static data member. */
15195
15196 /* Get type of field. */
15197 fp->type = die_type (die, cu);
15198
15199 SET_FIELD_BITPOS (*fp, 0);
15200
15201 /* Get bit size of field (zero if none). */
15202 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15203 if (attr)
15204 {
15205 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15206 }
15207 else
15208 {
15209 FIELD_BITSIZE (*fp) = 0;
15210 }
15211
15212 /* Get bit offset of field. */
15213 if (handle_data_member_location (die, cu, &offset))
15214 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15215 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15216 if (attr)
15217 {
15218 if (gdbarch_bits_big_endian (gdbarch))
15219 {
15220 /* For big endian bits, the DW_AT_bit_offset gives the
15221 additional bit offset from the MSB of the containing
15222 anonymous object to the MSB of the field. We don't
15223 have to do anything special since we don't need to
15224 know the size of the anonymous object. */
15225 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15226 }
15227 else
15228 {
15229 /* For little endian bits, compute the bit offset to the
15230 MSB of the anonymous object, subtract off the number of
15231 bits from the MSB of the field to the MSB of the
15232 object, and then subtract off the number of bits of
15233 the field itself. The result is the bit offset of
15234 the LSB of the field. */
15235 int anonymous_size;
15236 int bit_offset = DW_UNSND (attr);
15237
15238 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15239 if (attr)
15240 {
15241 /* The size of the anonymous object containing
15242 the bit field is explicit, so use the
15243 indicated size (in bytes). */
15244 anonymous_size = DW_UNSND (attr);
15245 }
15246 else
15247 {
15248 /* The size of the anonymous object containing
15249 the bit field must be inferred from the type
15250 attribute of the data member containing the
15251 bit field. */
15252 anonymous_size = TYPE_LENGTH (fp->type);
15253 }
15254 SET_FIELD_BITPOS (*fp,
15255 (FIELD_BITPOS (*fp)
15256 + anonymous_size * bits_per_byte
15257 - bit_offset - FIELD_BITSIZE (*fp)));
15258 }
15259 }
15260 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15261 if (attr != NULL)
15262 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15263 + dwarf2_get_attr_constant_value (attr, 0)));
15264
15265 /* Get name of field. */
15266 fieldname = dwarf2_name (die, cu);
15267 if (fieldname == NULL)
15268 fieldname = "";
15269
15270 /* The name is already allocated along with this objfile, so we don't
15271 need to duplicate it for the type. */
15272 fp->name = fieldname;
15273
15274 /* Change accessibility for artificial fields (e.g. virtual table
15275 pointer or virtual base class pointer) to private. */
15276 if (dwarf2_attr (die, DW_AT_artificial, cu))
15277 {
15278 FIELD_ARTIFICIAL (*fp) = 1;
15279 new_field->accessibility = DW_ACCESS_private;
15280 fip->non_public_fields = 1;
15281 }
15282 }
15283 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15284 {
15285 /* C++ static member. */
15286
15287 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15288 is a declaration, but all versions of G++ as of this writing
15289 (so through at least 3.2.1) incorrectly generate
15290 DW_TAG_variable tags. */
15291
15292 const char *physname;
15293
15294 /* Get name of field. */
15295 fieldname = dwarf2_name (die, cu);
15296 if (fieldname == NULL)
15297 return;
15298
15299 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15300 if (attr
15301 /* Only create a symbol if this is an external value.
15302 new_symbol checks this and puts the value in the global symbol
15303 table, which we want. If it is not external, new_symbol
15304 will try to put the value in cu->list_in_scope which is wrong. */
15305 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15306 {
15307 /* A static const member, not much different than an enum as far as
15308 we're concerned, except that we can support more types. */
15309 new_symbol (die, NULL, cu);
15310 }
15311
15312 /* Get physical name. */
15313 physname = dwarf2_physname (fieldname, die, cu);
15314
15315 /* The name is already allocated along with this objfile, so we don't
15316 need to duplicate it for the type. */
15317 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15318 FIELD_TYPE (*fp) = die_type (die, cu);
15319 FIELD_NAME (*fp) = fieldname;
15320 }
15321 else if (die->tag == DW_TAG_inheritance)
15322 {
15323 LONGEST offset;
15324
15325 /* C++ base class field. */
15326 if (handle_data_member_location (die, cu, &offset))
15327 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15328 FIELD_BITSIZE (*fp) = 0;
15329 FIELD_TYPE (*fp) = die_type (die, cu);
15330 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15331 fip->nbaseclasses++;
15332 }
15333 }
15334
15335 /* Can the type given by DIE define another type? */
15336
15337 static bool
15338 type_can_define_types (const struct die_info *die)
15339 {
15340 switch (die->tag)
15341 {
15342 case DW_TAG_typedef:
15343 case DW_TAG_class_type:
15344 case DW_TAG_structure_type:
15345 case DW_TAG_union_type:
15346 case DW_TAG_enumeration_type:
15347 return true;
15348
15349 default:
15350 return false;
15351 }
15352 }
15353
15354 /* Add a type definition defined in the scope of the FIP's class. */
15355
15356 static void
15357 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15358 struct dwarf2_cu *cu)
15359 {
15360 struct decl_field_list *new_field;
15361 struct decl_field *fp;
15362
15363 /* Allocate a new field list entry and link it in. */
15364 new_field = XCNEW (struct decl_field_list);
15365 make_cleanup (xfree, new_field);
15366
15367 gdb_assert (type_can_define_types (die));
15368
15369 fp = &new_field->field;
15370
15371 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15372 fp->name = dwarf2_name (die, cu);
15373 fp->type = read_type_die (die, cu);
15374
15375 /* Save accessibility. */
15376 enum dwarf_access_attribute accessibility;
15377 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15378 if (attr != NULL)
15379 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15380 else
15381 accessibility = dwarf2_default_access_attribute (die, cu);
15382 switch (accessibility)
15383 {
15384 case DW_ACCESS_public:
15385 /* The assumed value if neither private nor protected. */
15386 break;
15387 case DW_ACCESS_private:
15388 fp->is_private = 1;
15389 break;
15390 case DW_ACCESS_protected:
15391 fp->is_protected = 1;
15392 break;
15393 default:
15394 complaint (&symfile_complaints,
15395 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15396 }
15397
15398 if (die->tag == DW_TAG_typedef)
15399 {
15400 new_field->next = fip->typedef_field_list;
15401 fip->typedef_field_list = new_field;
15402 fip->typedef_field_list_count++;
15403 }
15404 else
15405 {
15406 new_field->next = fip->nested_types_list;
15407 fip->nested_types_list = new_field;
15408 fip->nested_types_list_count++;
15409 }
15410 }
15411
15412 /* Create the vector of fields, and attach it to the type. */
15413
15414 static void
15415 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15416 struct dwarf2_cu *cu)
15417 {
15418 int nfields = fip->nfields;
15419
15420 /* Record the field count, allocate space for the array of fields,
15421 and create blank accessibility bitfields if necessary. */
15422 TYPE_NFIELDS (type) = nfields;
15423 TYPE_FIELDS (type) = (struct field *)
15424 TYPE_ALLOC (type, sizeof (struct field) * nfields);
15425 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
15426
15427 if (fip->non_public_fields && cu->language != language_ada)
15428 {
15429 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15430
15431 TYPE_FIELD_PRIVATE_BITS (type) =
15432 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15433 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15434
15435 TYPE_FIELD_PROTECTED_BITS (type) =
15436 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15437 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15438
15439 TYPE_FIELD_IGNORE_BITS (type) =
15440 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15441 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15442 }
15443
15444 /* If the type has baseclasses, allocate and clear a bit vector for
15445 TYPE_FIELD_VIRTUAL_BITS. */
15446 if (fip->nbaseclasses && cu->language != language_ada)
15447 {
15448 int num_bytes = B_BYTES (fip->nbaseclasses);
15449 unsigned char *pointer;
15450
15451 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15452 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15453 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15454 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
15455 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
15456 }
15457
15458 /* Copy the saved-up fields into the field vector. Start from the head of
15459 the list, adding to the tail of the field array, so that they end up in
15460 the same order in the array in which they were added to the list. */
15461 while (nfields-- > 0)
15462 {
15463 struct nextfield *fieldp;
15464
15465 if (fip->fields)
15466 {
15467 fieldp = fip->fields;
15468 fip->fields = fieldp->next;
15469 }
15470 else
15471 {
15472 fieldp = fip->baseclasses;
15473 fip->baseclasses = fieldp->next;
15474 }
15475
15476 TYPE_FIELD (type, nfields) = fieldp->field;
15477 switch (fieldp->accessibility)
15478 {
15479 case DW_ACCESS_private:
15480 if (cu->language != language_ada)
15481 SET_TYPE_FIELD_PRIVATE (type, nfields);
15482 break;
15483
15484 case DW_ACCESS_protected:
15485 if (cu->language != language_ada)
15486 SET_TYPE_FIELD_PROTECTED (type, nfields);
15487 break;
15488
15489 case DW_ACCESS_public:
15490 break;
15491
15492 default:
15493 /* Unknown accessibility. Complain and treat it as public. */
15494 {
15495 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15496 fieldp->accessibility);
15497 }
15498 break;
15499 }
15500 if (nfields < fip->nbaseclasses)
15501 {
15502 switch (fieldp->virtuality)
15503 {
15504 case DW_VIRTUALITY_virtual:
15505 case DW_VIRTUALITY_pure_virtual:
15506 if (cu->language == language_ada)
15507 error (_("unexpected virtuality in component of Ada type"));
15508 SET_TYPE_FIELD_VIRTUAL (type, nfields);
15509 break;
15510 }
15511 }
15512 }
15513 }
15514
15515 /* Return true if this member function is a constructor, false
15516 otherwise. */
15517
15518 static int
15519 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15520 {
15521 const char *fieldname;
15522 const char *type_name;
15523 int len;
15524
15525 if (die->parent == NULL)
15526 return 0;
15527
15528 if (die->parent->tag != DW_TAG_structure_type
15529 && die->parent->tag != DW_TAG_union_type
15530 && die->parent->tag != DW_TAG_class_type)
15531 return 0;
15532
15533 fieldname = dwarf2_name (die, cu);
15534 type_name = dwarf2_name (die->parent, cu);
15535 if (fieldname == NULL || type_name == NULL)
15536 return 0;
15537
15538 len = strlen (fieldname);
15539 return (strncmp (fieldname, type_name, len) == 0
15540 && (type_name[len] == '\0' || type_name[len] == '<'));
15541 }
15542
15543 /* Add a member function to the proper fieldlist. */
15544
15545 static void
15546 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15547 struct type *type, struct dwarf2_cu *cu)
15548 {
15549 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15550 struct attribute *attr;
15551 struct fnfieldlist *flp;
15552 int i;
15553 struct fn_field *fnp;
15554 const char *fieldname;
15555 struct nextfnfield *new_fnfield;
15556 struct type *this_type;
15557 enum dwarf_access_attribute accessibility;
15558
15559 if (cu->language == language_ada)
15560 error (_("unexpected member function in Ada type"));
15561
15562 /* Get name of member function. */
15563 fieldname = dwarf2_name (die, cu);
15564 if (fieldname == NULL)
15565 return;
15566
15567 /* Look up member function name in fieldlist. */
15568 for (i = 0; i < fip->nfnfields; i++)
15569 {
15570 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15571 break;
15572 }
15573
15574 /* Create new list element if necessary. */
15575 if (i < fip->nfnfields)
15576 flp = &fip->fnfieldlists[i];
15577 else
15578 {
15579 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
15580 {
15581 fip->fnfieldlists = (struct fnfieldlist *)
15582 xrealloc (fip->fnfieldlists,
15583 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
15584 * sizeof (struct fnfieldlist));
15585 if (fip->nfnfields == 0)
15586 make_cleanup (free_current_contents, &fip->fnfieldlists);
15587 }
15588 flp = &fip->fnfieldlists[fip->nfnfields];
15589 flp->name = fieldname;
15590 flp->length = 0;
15591 flp->head = NULL;
15592 i = fip->nfnfields++;
15593 }
15594
15595 /* Create a new member function field and chain it to the field list
15596 entry. */
15597 new_fnfield = XNEW (struct nextfnfield);
15598 make_cleanup (xfree, new_fnfield);
15599 memset (new_fnfield, 0, sizeof (struct nextfnfield));
15600 new_fnfield->next = flp->head;
15601 flp->head = new_fnfield;
15602 flp->length++;
15603
15604 /* Fill in the member function field info. */
15605 fnp = &new_fnfield->fnfield;
15606
15607 /* Delay processing of the physname until later. */
15608 if (cu->language == language_cplus)
15609 {
15610 add_to_method_list (type, i, flp->length - 1, fieldname,
15611 die, cu);
15612 }
15613 else
15614 {
15615 const char *physname = dwarf2_physname (fieldname, die, cu);
15616 fnp->physname = physname ? physname : "";
15617 }
15618
15619 fnp->type = alloc_type (objfile);
15620 this_type = read_type_die (die, cu);
15621 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15622 {
15623 int nparams = TYPE_NFIELDS (this_type);
15624
15625 /* TYPE is the domain of this method, and THIS_TYPE is the type
15626 of the method itself (TYPE_CODE_METHOD). */
15627 smash_to_method_type (fnp->type, type,
15628 TYPE_TARGET_TYPE (this_type),
15629 TYPE_FIELDS (this_type),
15630 TYPE_NFIELDS (this_type),
15631 TYPE_VARARGS (this_type));
15632
15633 /* Handle static member functions.
15634 Dwarf2 has no clean way to discern C++ static and non-static
15635 member functions. G++ helps GDB by marking the first
15636 parameter for non-static member functions (which is the this
15637 pointer) as artificial. We obtain this information from
15638 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15639 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15640 fnp->voffset = VOFFSET_STATIC;
15641 }
15642 else
15643 complaint (&symfile_complaints, _("member function type missing for '%s'"),
15644 dwarf2_full_name (fieldname, die, cu));
15645
15646 /* Get fcontext from DW_AT_containing_type if present. */
15647 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15648 fnp->fcontext = die_containing_type (die, cu);
15649
15650 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15651 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15652
15653 /* Get accessibility. */
15654 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15655 if (attr)
15656 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15657 else
15658 accessibility = dwarf2_default_access_attribute (die, cu);
15659 switch (accessibility)
15660 {
15661 case DW_ACCESS_private:
15662 fnp->is_private = 1;
15663 break;
15664 case DW_ACCESS_protected:
15665 fnp->is_protected = 1;
15666 break;
15667 }
15668
15669 /* Check for artificial methods. */
15670 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15671 if (attr && DW_UNSND (attr) != 0)
15672 fnp->is_artificial = 1;
15673
15674 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15675
15676 /* Get index in virtual function table if it is a virtual member
15677 function. For older versions of GCC, this is an offset in the
15678 appropriate virtual table, as specified by DW_AT_containing_type.
15679 For everyone else, it is an expression to be evaluated relative
15680 to the object address. */
15681
15682 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15683 if (attr)
15684 {
15685 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15686 {
15687 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15688 {
15689 /* Old-style GCC. */
15690 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15691 }
15692 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15693 || (DW_BLOCK (attr)->size > 1
15694 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15695 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15696 {
15697 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15698 if ((fnp->voffset % cu->header.addr_size) != 0)
15699 dwarf2_complex_location_expr_complaint ();
15700 else
15701 fnp->voffset /= cu->header.addr_size;
15702 fnp->voffset += 2;
15703 }
15704 else
15705 dwarf2_complex_location_expr_complaint ();
15706
15707 if (!fnp->fcontext)
15708 {
15709 /* If there is no `this' field and no DW_AT_containing_type,
15710 we cannot actually find a base class context for the
15711 vtable! */
15712 if (TYPE_NFIELDS (this_type) == 0
15713 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15714 {
15715 complaint (&symfile_complaints,
15716 _("cannot determine context for virtual member "
15717 "function \"%s\" (offset %s)"),
15718 fieldname, sect_offset_str (die->sect_off));
15719 }
15720 else
15721 {
15722 fnp->fcontext
15723 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15724 }
15725 }
15726 }
15727 else if (attr_form_is_section_offset (attr))
15728 {
15729 dwarf2_complex_location_expr_complaint ();
15730 }
15731 else
15732 {
15733 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15734 fieldname);
15735 }
15736 }
15737 else
15738 {
15739 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15740 if (attr && DW_UNSND (attr))
15741 {
15742 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15743 complaint (&symfile_complaints,
15744 _("Member function \"%s\" (offset %s) is virtual "
15745 "but the vtable offset is not specified"),
15746 fieldname, sect_offset_str (die->sect_off));
15747 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15748 TYPE_CPLUS_DYNAMIC (type) = 1;
15749 }
15750 }
15751 }
15752
15753 /* Create the vector of member function fields, and attach it to the type. */
15754
15755 static void
15756 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15757 struct dwarf2_cu *cu)
15758 {
15759 struct fnfieldlist *flp;
15760 int i;
15761
15762 if (cu->language == language_ada)
15763 error (_("unexpected member functions in Ada type"));
15764
15765 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15766 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15767 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
15768
15769 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
15770 {
15771 struct nextfnfield *nfp = flp->head;
15772 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15773 int k;
15774
15775 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
15776 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
15777 fn_flp->fn_fields = (struct fn_field *)
15778 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
15779 for (k = flp->length; (k--, nfp); nfp = nfp->next)
15780 fn_flp->fn_fields[k] = nfp->fnfield;
15781 }
15782
15783 TYPE_NFN_FIELDS (type) = fip->nfnfields;
15784 }
15785
15786 /* Returns non-zero if NAME is the name of a vtable member in CU's
15787 language, zero otherwise. */
15788 static int
15789 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15790 {
15791 static const char vptr[] = "_vptr";
15792
15793 /* Look for the C++ form of the vtable. */
15794 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15795 return 1;
15796
15797 return 0;
15798 }
15799
15800 /* GCC outputs unnamed structures that are really pointers to member
15801 functions, with the ABI-specified layout. If TYPE describes
15802 such a structure, smash it into a member function type.
15803
15804 GCC shouldn't do this; it should just output pointer to member DIEs.
15805 This is GCC PR debug/28767. */
15806
15807 static void
15808 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15809 {
15810 struct type *pfn_type, *self_type, *new_type;
15811
15812 /* Check for a structure with no name and two children. */
15813 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15814 return;
15815
15816 /* Check for __pfn and __delta members. */
15817 if (TYPE_FIELD_NAME (type, 0) == NULL
15818 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15819 || TYPE_FIELD_NAME (type, 1) == NULL
15820 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15821 return;
15822
15823 /* Find the type of the method. */
15824 pfn_type = TYPE_FIELD_TYPE (type, 0);
15825 if (pfn_type == NULL
15826 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15827 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15828 return;
15829
15830 /* Look for the "this" argument. */
15831 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15832 if (TYPE_NFIELDS (pfn_type) == 0
15833 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15834 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15835 return;
15836
15837 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15838 new_type = alloc_type (objfile);
15839 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15840 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15841 TYPE_VARARGS (pfn_type));
15842 smash_to_methodptr_type (type, new_type);
15843 }
15844
15845
15846 /* Called when we find the DIE that starts a structure or union scope
15847 (definition) to create a type for the structure or union. Fill in
15848 the type's name and general properties; the members will not be
15849 processed until process_structure_scope. A symbol table entry for
15850 the type will also not be done until process_structure_scope (assuming
15851 the type has a name).
15852
15853 NOTE: we need to call these functions regardless of whether or not the
15854 DIE has a DW_AT_name attribute, since it might be an anonymous
15855 structure or union. This gets the type entered into our set of
15856 user defined types. */
15857
15858 static struct type *
15859 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15860 {
15861 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15862 struct type *type;
15863 struct attribute *attr;
15864 const char *name;
15865
15866 /* If the definition of this type lives in .debug_types, read that type.
15867 Don't follow DW_AT_specification though, that will take us back up
15868 the chain and we want to go down. */
15869 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15870 if (attr)
15871 {
15872 type = get_DW_AT_signature_type (die, attr, cu);
15873
15874 /* The type's CU may not be the same as CU.
15875 Ensure TYPE is recorded with CU in die_type_hash. */
15876 return set_die_type (die, type, cu);
15877 }
15878
15879 type = alloc_type (objfile);
15880 INIT_CPLUS_SPECIFIC (type);
15881
15882 name = dwarf2_name (die, cu);
15883 if (name != NULL)
15884 {
15885 if (cu->language == language_cplus
15886 || cu->language == language_d
15887 || cu->language == language_rust)
15888 {
15889 const char *full_name = dwarf2_full_name (name, die, cu);
15890
15891 /* dwarf2_full_name might have already finished building the DIE's
15892 type. If so, there is no need to continue. */
15893 if (get_die_type (die, cu) != NULL)
15894 return get_die_type (die, cu);
15895
15896 TYPE_TAG_NAME (type) = full_name;
15897 if (die->tag == DW_TAG_structure_type
15898 || die->tag == DW_TAG_class_type)
15899 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15900 }
15901 else
15902 {
15903 /* The name is already allocated along with this objfile, so
15904 we don't need to duplicate it for the type. */
15905 TYPE_TAG_NAME (type) = name;
15906 if (die->tag == DW_TAG_class_type)
15907 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15908 }
15909 }
15910
15911 if (die->tag == DW_TAG_structure_type)
15912 {
15913 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15914 }
15915 else if (die->tag == DW_TAG_union_type)
15916 {
15917 TYPE_CODE (type) = TYPE_CODE_UNION;
15918 }
15919 else
15920 {
15921 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15922 }
15923
15924 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15925 TYPE_DECLARED_CLASS (type) = 1;
15926
15927 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15928 if (attr)
15929 {
15930 if (attr_form_is_constant (attr))
15931 TYPE_LENGTH (type) = DW_UNSND (attr);
15932 else
15933 {
15934 /* For the moment, dynamic type sizes are not supported
15935 by GDB's struct type. The actual size is determined
15936 on-demand when resolving the type of a given object,
15937 so set the type's length to zero for now. Otherwise,
15938 we record an expression as the length, and that expression
15939 could lead to a very large value, which could eventually
15940 lead to us trying to allocate that much memory when creating
15941 a value of that type. */
15942 TYPE_LENGTH (type) = 0;
15943 }
15944 }
15945 else
15946 {
15947 TYPE_LENGTH (type) = 0;
15948 }
15949
15950 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15951 {
15952 /* ICC<14 does not output the required DW_AT_declaration on
15953 incomplete types, but gives them a size of zero. */
15954 TYPE_STUB (type) = 1;
15955 }
15956 else
15957 TYPE_STUB_SUPPORTED (type) = 1;
15958
15959 if (die_is_declaration (die, cu))
15960 TYPE_STUB (type) = 1;
15961 else if (attr == NULL && die->child == NULL
15962 && producer_is_realview (cu->producer))
15963 /* RealView does not output the required DW_AT_declaration
15964 on incomplete types. */
15965 TYPE_STUB (type) = 1;
15966
15967 /* We need to add the type field to the die immediately so we don't
15968 infinitely recurse when dealing with pointers to the structure
15969 type within the structure itself. */
15970 set_die_type (die, type, cu);
15971
15972 /* set_die_type should be already done. */
15973 set_descriptive_type (type, die, cu);
15974
15975 return type;
15976 }
15977
15978 /* Finish creating a structure or union type, including filling in
15979 its members and creating a symbol for it. */
15980
15981 static void
15982 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15983 {
15984 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15985 struct die_info *child_die;
15986 struct type *type;
15987
15988 type = get_die_type (die, cu);
15989 if (type == NULL)
15990 type = read_structure_type (die, cu);
15991
15992 if (die->child != NULL && ! die_is_declaration (die, cu))
15993 {
15994 struct field_info fi;
15995 std::vector<struct symbol *> template_args;
15996 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
15997
15998 memset (&fi, 0, sizeof (struct field_info));
15999
16000 child_die = die->child;
16001
16002 while (child_die && child_die->tag)
16003 {
16004 if (child_die->tag == DW_TAG_member
16005 || child_die->tag == DW_TAG_variable)
16006 {
16007 /* NOTE: carlton/2002-11-05: A C++ static data member
16008 should be a DW_TAG_member that is a declaration, but
16009 all versions of G++ as of this writing (so through at
16010 least 3.2.1) incorrectly generate DW_TAG_variable
16011 tags for them instead. */
16012 dwarf2_add_field (&fi, child_die, cu);
16013 }
16014 else if (child_die->tag == DW_TAG_subprogram)
16015 {
16016 /* Rust doesn't have member functions in the C++ sense.
16017 However, it does emit ordinary functions as children
16018 of a struct DIE. */
16019 if (cu->language == language_rust)
16020 read_func_scope (child_die, cu);
16021 else
16022 {
16023 /* C++ member function. */
16024 dwarf2_add_member_fn (&fi, child_die, type, cu);
16025 }
16026 }
16027 else if (child_die->tag == DW_TAG_inheritance)
16028 {
16029 /* C++ base class field. */
16030 dwarf2_add_field (&fi, child_die, cu);
16031 }
16032 else if (type_can_define_types (child_die))
16033 dwarf2_add_type_defn (&fi, child_die, cu);
16034 else if (child_die->tag == DW_TAG_template_type_param
16035 || child_die->tag == DW_TAG_template_value_param)
16036 {
16037 struct symbol *arg = new_symbol (child_die, NULL, cu);
16038
16039 if (arg != NULL)
16040 template_args.push_back (arg);
16041 }
16042
16043 child_die = sibling_die (child_die);
16044 }
16045
16046 /* Attach template arguments to type. */
16047 if (!template_args.empty ())
16048 {
16049 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16050 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16051 TYPE_TEMPLATE_ARGUMENTS (type)
16052 = XOBNEWVEC (&objfile->objfile_obstack,
16053 struct symbol *,
16054 TYPE_N_TEMPLATE_ARGUMENTS (type));
16055 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16056 template_args.data (),
16057 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16058 * sizeof (struct symbol *)));
16059 }
16060
16061 /* Attach fields and member functions to the type. */
16062 if (fi.nfields)
16063 dwarf2_attach_fields_to_type (&fi, type, cu);
16064 if (fi.nfnfields)
16065 {
16066 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16067
16068 /* Get the type which refers to the base class (possibly this
16069 class itself) which contains the vtable pointer for the current
16070 class from the DW_AT_containing_type attribute. This use of
16071 DW_AT_containing_type is a GNU extension. */
16072
16073 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16074 {
16075 struct type *t = die_containing_type (die, cu);
16076
16077 set_type_vptr_basetype (type, t);
16078 if (type == t)
16079 {
16080 int i;
16081
16082 /* Our own class provides vtbl ptr. */
16083 for (i = TYPE_NFIELDS (t) - 1;
16084 i >= TYPE_N_BASECLASSES (t);
16085 --i)
16086 {
16087 const char *fieldname = TYPE_FIELD_NAME (t, i);
16088
16089 if (is_vtable_name (fieldname, cu))
16090 {
16091 set_type_vptr_fieldno (type, i);
16092 break;
16093 }
16094 }
16095
16096 /* Complain if virtual function table field not found. */
16097 if (i < TYPE_N_BASECLASSES (t))
16098 complaint (&symfile_complaints,
16099 _("virtual function table pointer "
16100 "not found when defining class '%s'"),
16101 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16102 "");
16103 }
16104 else
16105 {
16106 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16107 }
16108 }
16109 else if (cu->producer
16110 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16111 {
16112 /* The IBM XLC compiler does not provide direct indication
16113 of the containing type, but the vtable pointer is
16114 always named __vfp. */
16115
16116 int i;
16117
16118 for (i = TYPE_NFIELDS (type) - 1;
16119 i >= TYPE_N_BASECLASSES (type);
16120 --i)
16121 {
16122 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16123 {
16124 set_type_vptr_fieldno (type, i);
16125 set_type_vptr_basetype (type, type);
16126 break;
16127 }
16128 }
16129 }
16130 }
16131
16132 /* Copy fi.typedef_field_list linked list elements content into the
16133 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16134 if (fi.typedef_field_list)
16135 {
16136 int i = fi.typedef_field_list_count;
16137
16138 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16139 TYPE_TYPEDEF_FIELD_ARRAY (type)
16140 = ((struct decl_field *)
16141 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
16142 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
16143
16144 /* Reverse the list order to keep the debug info elements order. */
16145 while (--i >= 0)
16146 {
16147 struct decl_field *dest, *src;
16148
16149 dest = &TYPE_TYPEDEF_FIELD (type, i);
16150 src = &fi.typedef_field_list->field;
16151 fi.typedef_field_list = fi.typedef_field_list->next;
16152 *dest = *src;
16153 }
16154 }
16155
16156 /* Copy fi.nested_types_list linked list elements content into the
16157 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16158 if (fi.nested_types_list != NULL && cu->language != language_ada)
16159 {
16160 int i = fi.nested_types_list_count;
16161
16162 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16163 TYPE_NESTED_TYPES_ARRAY (type)
16164 = ((struct decl_field *)
16165 TYPE_ALLOC (type, sizeof (struct decl_field) * i));
16166 TYPE_NESTED_TYPES_COUNT (type) = i;
16167
16168 /* Reverse the list order to keep the debug info elements order. */
16169 while (--i >= 0)
16170 {
16171 struct decl_field *dest, *src;
16172
16173 dest = &TYPE_NESTED_TYPES_FIELD (type, i);
16174 src = &fi.nested_types_list->field;
16175 fi.nested_types_list = fi.nested_types_list->next;
16176 *dest = *src;
16177 }
16178 }
16179
16180 do_cleanups (back_to);
16181 }
16182
16183 quirk_gcc_member_function_pointer (type, objfile);
16184
16185 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16186 snapshots) has been known to create a die giving a declaration
16187 for a class that has, as a child, a die giving a definition for a
16188 nested class. So we have to process our children even if the
16189 current die is a declaration. Normally, of course, a declaration
16190 won't have any children at all. */
16191
16192 child_die = die->child;
16193
16194 while (child_die != NULL && child_die->tag)
16195 {
16196 if (child_die->tag == DW_TAG_member
16197 || child_die->tag == DW_TAG_variable
16198 || child_die->tag == DW_TAG_inheritance
16199 || child_die->tag == DW_TAG_template_value_param
16200 || child_die->tag == DW_TAG_template_type_param)
16201 {
16202 /* Do nothing. */
16203 }
16204 else
16205 process_die (child_die, cu);
16206
16207 child_die = sibling_die (child_die);
16208 }
16209
16210 /* Do not consider external references. According to the DWARF standard,
16211 these DIEs are identified by the fact that they have no byte_size
16212 attribute, and a declaration attribute. */
16213 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16214 || !die_is_declaration (die, cu))
16215 new_symbol (die, type, cu);
16216 }
16217
16218 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16219 update TYPE using some information only available in DIE's children. */
16220
16221 static void
16222 update_enumeration_type_from_children (struct die_info *die,
16223 struct type *type,
16224 struct dwarf2_cu *cu)
16225 {
16226 struct die_info *child_die;
16227 int unsigned_enum = 1;
16228 int flag_enum = 1;
16229 ULONGEST mask = 0;
16230
16231 auto_obstack obstack;
16232
16233 for (child_die = die->child;
16234 child_die != NULL && child_die->tag;
16235 child_die = sibling_die (child_die))
16236 {
16237 struct attribute *attr;
16238 LONGEST value;
16239 const gdb_byte *bytes;
16240 struct dwarf2_locexpr_baton *baton;
16241 const char *name;
16242
16243 if (child_die->tag != DW_TAG_enumerator)
16244 continue;
16245
16246 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16247 if (attr == NULL)
16248 continue;
16249
16250 name = dwarf2_name (child_die, cu);
16251 if (name == NULL)
16252 name = "<anonymous enumerator>";
16253
16254 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16255 &value, &bytes, &baton);
16256 if (value < 0)
16257 {
16258 unsigned_enum = 0;
16259 flag_enum = 0;
16260 }
16261 else if ((mask & value) != 0)
16262 flag_enum = 0;
16263 else
16264 mask |= value;
16265
16266 /* If we already know that the enum type is neither unsigned, nor
16267 a flag type, no need to look at the rest of the enumerates. */
16268 if (!unsigned_enum && !flag_enum)
16269 break;
16270 }
16271
16272 if (unsigned_enum)
16273 TYPE_UNSIGNED (type) = 1;
16274 if (flag_enum)
16275 TYPE_FLAG_ENUM (type) = 1;
16276 }
16277
16278 /* Given a DW_AT_enumeration_type die, set its type. We do not
16279 complete the type's fields yet, or create any symbols. */
16280
16281 static struct type *
16282 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16283 {
16284 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16285 struct type *type;
16286 struct attribute *attr;
16287 const char *name;
16288
16289 /* If the definition of this type lives in .debug_types, read that type.
16290 Don't follow DW_AT_specification though, that will take us back up
16291 the chain and we want to go down. */
16292 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16293 if (attr)
16294 {
16295 type = get_DW_AT_signature_type (die, attr, cu);
16296
16297 /* The type's CU may not be the same as CU.
16298 Ensure TYPE is recorded with CU in die_type_hash. */
16299 return set_die_type (die, type, cu);
16300 }
16301
16302 type = alloc_type (objfile);
16303
16304 TYPE_CODE (type) = TYPE_CODE_ENUM;
16305 name = dwarf2_full_name (NULL, die, cu);
16306 if (name != NULL)
16307 TYPE_TAG_NAME (type) = name;
16308
16309 attr = dwarf2_attr (die, DW_AT_type, cu);
16310 if (attr != NULL)
16311 {
16312 struct type *underlying_type = die_type (die, cu);
16313
16314 TYPE_TARGET_TYPE (type) = underlying_type;
16315 }
16316
16317 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16318 if (attr)
16319 {
16320 TYPE_LENGTH (type) = DW_UNSND (attr);
16321 }
16322 else
16323 {
16324 TYPE_LENGTH (type) = 0;
16325 }
16326
16327 /* The enumeration DIE can be incomplete. In Ada, any type can be
16328 declared as private in the package spec, and then defined only
16329 inside the package body. Such types are known as Taft Amendment
16330 Types. When another package uses such a type, an incomplete DIE
16331 may be generated by the compiler. */
16332 if (die_is_declaration (die, cu))
16333 TYPE_STUB (type) = 1;
16334
16335 /* Finish the creation of this type by using the enum's children.
16336 We must call this even when the underlying type has been provided
16337 so that we can determine if we're looking at a "flag" enum. */
16338 update_enumeration_type_from_children (die, type, cu);
16339
16340 /* If this type has an underlying type that is not a stub, then we
16341 may use its attributes. We always use the "unsigned" attribute
16342 in this situation, because ordinarily we guess whether the type
16343 is unsigned -- but the guess can be wrong and the underlying type
16344 can tell us the reality. However, we defer to a local size
16345 attribute if one exists, because this lets the compiler override
16346 the underlying type if needed. */
16347 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16348 {
16349 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16350 if (TYPE_LENGTH (type) == 0)
16351 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16352 }
16353
16354 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16355
16356 return set_die_type (die, type, cu);
16357 }
16358
16359 /* Given a pointer to a die which begins an enumeration, process all
16360 the dies that define the members of the enumeration, and create the
16361 symbol for the enumeration type.
16362
16363 NOTE: We reverse the order of the element list. */
16364
16365 static void
16366 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16367 {
16368 struct type *this_type;
16369
16370 this_type = get_die_type (die, cu);
16371 if (this_type == NULL)
16372 this_type = read_enumeration_type (die, cu);
16373
16374 if (die->child != NULL)
16375 {
16376 struct die_info *child_die;
16377 struct symbol *sym;
16378 struct field *fields = NULL;
16379 int num_fields = 0;
16380 const char *name;
16381
16382 child_die = die->child;
16383 while (child_die && child_die->tag)
16384 {
16385 if (child_die->tag != DW_TAG_enumerator)
16386 {
16387 process_die (child_die, cu);
16388 }
16389 else
16390 {
16391 name = dwarf2_name (child_die, cu);
16392 if (name)
16393 {
16394 sym = new_symbol (child_die, this_type, cu);
16395
16396 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16397 {
16398 fields = (struct field *)
16399 xrealloc (fields,
16400 (num_fields + DW_FIELD_ALLOC_CHUNK)
16401 * sizeof (struct field));
16402 }
16403
16404 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16405 FIELD_TYPE (fields[num_fields]) = NULL;
16406 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16407 FIELD_BITSIZE (fields[num_fields]) = 0;
16408
16409 num_fields++;
16410 }
16411 }
16412
16413 child_die = sibling_die (child_die);
16414 }
16415
16416 if (num_fields)
16417 {
16418 TYPE_NFIELDS (this_type) = num_fields;
16419 TYPE_FIELDS (this_type) = (struct field *)
16420 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16421 memcpy (TYPE_FIELDS (this_type), fields,
16422 sizeof (struct field) * num_fields);
16423 xfree (fields);
16424 }
16425 }
16426
16427 /* If we are reading an enum from a .debug_types unit, and the enum
16428 is a declaration, and the enum is not the signatured type in the
16429 unit, then we do not want to add a symbol for it. Adding a
16430 symbol would in some cases obscure the true definition of the
16431 enum, giving users an incomplete type when the definition is
16432 actually available. Note that we do not want to do this for all
16433 enums which are just declarations, because C++0x allows forward
16434 enum declarations. */
16435 if (cu->per_cu->is_debug_types
16436 && die_is_declaration (die, cu))
16437 {
16438 struct signatured_type *sig_type;
16439
16440 sig_type = (struct signatured_type *) cu->per_cu;
16441 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16442 if (sig_type->type_offset_in_section != die->sect_off)
16443 return;
16444 }
16445
16446 new_symbol (die, this_type, cu);
16447 }
16448
16449 /* Extract all information from a DW_TAG_array_type DIE and put it in
16450 the DIE's type field. For now, this only handles one dimensional
16451 arrays. */
16452
16453 static struct type *
16454 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16455 {
16456 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16457 struct die_info *child_die;
16458 struct type *type;
16459 struct type *element_type, *range_type, *index_type;
16460 struct attribute *attr;
16461 const char *name;
16462 struct dynamic_prop *byte_stride_prop = NULL;
16463 unsigned int bit_stride = 0;
16464
16465 element_type = die_type (die, cu);
16466
16467 /* The die_type call above may have already set the type for this DIE. */
16468 type = get_die_type (die, cu);
16469 if (type)
16470 return type;
16471
16472 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16473 if (attr != NULL)
16474 {
16475 int stride_ok;
16476
16477 byte_stride_prop
16478 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16479 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16480 if (!stride_ok)
16481 {
16482 complaint (&symfile_complaints,
16483 _("unable to read array DW_AT_byte_stride "
16484 " - DIE at %s [in module %s]"),
16485 sect_offset_str (die->sect_off),
16486 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16487 /* Ignore this attribute. We will likely not be able to print
16488 arrays of this type correctly, but there is little we can do
16489 to help if we cannot read the attribute's value. */
16490 byte_stride_prop = NULL;
16491 }
16492 }
16493
16494 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16495 if (attr != NULL)
16496 bit_stride = DW_UNSND (attr);
16497
16498 /* Irix 6.2 native cc creates array types without children for
16499 arrays with unspecified length. */
16500 if (die->child == NULL)
16501 {
16502 index_type = objfile_type (objfile)->builtin_int;
16503 range_type = create_static_range_type (NULL, index_type, 0, -1);
16504 type = create_array_type_with_stride (NULL, element_type, range_type,
16505 byte_stride_prop, bit_stride);
16506 return set_die_type (die, type, cu);
16507 }
16508
16509 std::vector<struct type *> range_types;
16510 child_die = die->child;
16511 while (child_die && child_die->tag)
16512 {
16513 if (child_die->tag == DW_TAG_subrange_type)
16514 {
16515 struct type *child_type = read_type_die (child_die, cu);
16516
16517 if (child_type != NULL)
16518 {
16519 /* The range type was succesfully read. Save it for the
16520 array type creation. */
16521 range_types.push_back (child_type);
16522 }
16523 }
16524 child_die = sibling_die (child_die);
16525 }
16526
16527 /* Dwarf2 dimensions are output from left to right, create the
16528 necessary array types in backwards order. */
16529
16530 type = element_type;
16531
16532 if (read_array_order (die, cu) == DW_ORD_col_major)
16533 {
16534 int i = 0;
16535
16536 while (i < range_types.size ())
16537 type = create_array_type_with_stride (NULL, type, range_types[i++],
16538 byte_stride_prop, bit_stride);
16539 }
16540 else
16541 {
16542 size_t ndim = range_types.size ();
16543 while (ndim-- > 0)
16544 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16545 byte_stride_prop, bit_stride);
16546 }
16547
16548 /* Understand Dwarf2 support for vector types (like they occur on
16549 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16550 array type. This is not part of the Dwarf2/3 standard yet, but a
16551 custom vendor extension. The main difference between a regular
16552 array and the vector variant is that vectors are passed by value
16553 to functions. */
16554 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16555 if (attr)
16556 make_vector_type (type);
16557
16558 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16559 implementation may choose to implement triple vectors using this
16560 attribute. */
16561 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16562 if (attr)
16563 {
16564 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16565 TYPE_LENGTH (type) = DW_UNSND (attr);
16566 else
16567 complaint (&symfile_complaints,
16568 _("DW_AT_byte_size for array type smaller "
16569 "than the total size of elements"));
16570 }
16571
16572 name = dwarf2_name (die, cu);
16573 if (name)
16574 TYPE_NAME (type) = name;
16575
16576 /* Install the type in the die. */
16577 set_die_type (die, type, cu);
16578
16579 /* set_die_type should be already done. */
16580 set_descriptive_type (type, die, cu);
16581
16582 return type;
16583 }
16584
16585 static enum dwarf_array_dim_ordering
16586 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16587 {
16588 struct attribute *attr;
16589
16590 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16591
16592 if (attr)
16593 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16594
16595 /* GNU F77 is a special case, as at 08/2004 array type info is the
16596 opposite order to the dwarf2 specification, but data is still
16597 laid out as per normal fortran.
16598
16599 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16600 version checking. */
16601
16602 if (cu->language == language_fortran
16603 && cu->producer && strstr (cu->producer, "GNU F77"))
16604 {
16605 return DW_ORD_row_major;
16606 }
16607
16608 switch (cu->language_defn->la_array_ordering)
16609 {
16610 case array_column_major:
16611 return DW_ORD_col_major;
16612 case array_row_major:
16613 default:
16614 return DW_ORD_row_major;
16615 };
16616 }
16617
16618 /* Extract all information from a DW_TAG_set_type DIE and put it in
16619 the DIE's type field. */
16620
16621 static struct type *
16622 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16623 {
16624 struct type *domain_type, *set_type;
16625 struct attribute *attr;
16626
16627 domain_type = die_type (die, cu);
16628
16629 /* The die_type call above may have already set the type for this DIE. */
16630 set_type = get_die_type (die, cu);
16631 if (set_type)
16632 return set_type;
16633
16634 set_type = create_set_type (NULL, domain_type);
16635
16636 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16637 if (attr)
16638 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16639
16640 return set_die_type (die, set_type, cu);
16641 }
16642
16643 /* A helper for read_common_block that creates a locexpr baton.
16644 SYM is the symbol which we are marking as computed.
16645 COMMON_DIE is the DIE for the common block.
16646 COMMON_LOC is the location expression attribute for the common
16647 block itself.
16648 MEMBER_LOC is the location expression attribute for the particular
16649 member of the common block that we are processing.
16650 CU is the CU from which the above come. */
16651
16652 static void
16653 mark_common_block_symbol_computed (struct symbol *sym,
16654 struct die_info *common_die,
16655 struct attribute *common_loc,
16656 struct attribute *member_loc,
16657 struct dwarf2_cu *cu)
16658 {
16659 struct dwarf2_per_objfile *dwarf2_per_objfile
16660 = cu->per_cu->dwarf2_per_objfile;
16661 struct objfile *objfile = dwarf2_per_objfile->objfile;
16662 struct dwarf2_locexpr_baton *baton;
16663 gdb_byte *ptr;
16664 unsigned int cu_off;
16665 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16666 LONGEST offset = 0;
16667
16668 gdb_assert (common_loc && member_loc);
16669 gdb_assert (attr_form_is_block (common_loc));
16670 gdb_assert (attr_form_is_block (member_loc)
16671 || attr_form_is_constant (member_loc));
16672
16673 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16674 baton->per_cu = cu->per_cu;
16675 gdb_assert (baton->per_cu);
16676
16677 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16678
16679 if (attr_form_is_constant (member_loc))
16680 {
16681 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16682 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16683 }
16684 else
16685 baton->size += DW_BLOCK (member_loc)->size;
16686
16687 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16688 baton->data = ptr;
16689
16690 *ptr++ = DW_OP_call4;
16691 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16692 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16693 ptr += 4;
16694
16695 if (attr_form_is_constant (member_loc))
16696 {
16697 *ptr++ = DW_OP_addr;
16698 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16699 ptr += cu->header.addr_size;
16700 }
16701 else
16702 {
16703 /* We have to copy the data here, because DW_OP_call4 will only
16704 use a DW_AT_location attribute. */
16705 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16706 ptr += DW_BLOCK (member_loc)->size;
16707 }
16708
16709 *ptr++ = DW_OP_plus;
16710 gdb_assert (ptr - baton->data == baton->size);
16711
16712 SYMBOL_LOCATION_BATON (sym) = baton;
16713 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16714 }
16715
16716 /* Create appropriate locally-scoped variables for all the
16717 DW_TAG_common_block entries. Also create a struct common_block
16718 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16719 is used to sepate the common blocks name namespace from regular
16720 variable names. */
16721
16722 static void
16723 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16724 {
16725 struct attribute *attr;
16726
16727 attr = dwarf2_attr (die, DW_AT_location, cu);
16728 if (attr)
16729 {
16730 /* Support the .debug_loc offsets. */
16731 if (attr_form_is_block (attr))
16732 {
16733 /* Ok. */
16734 }
16735 else if (attr_form_is_section_offset (attr))
16736 {
16737 dwarf2_complex_location_expr_complaint ();
16738 attr = NULL;
16739 }
16740 else
16741 {
16742 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16743 "common block member");
16744 attr = NULL;
16745 }
16746 }
16747
16748 if (die->child != NULL)
16749 {
16750 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16751 struct die_info *child_die;
16752 size_t n_entries = 0, size;
16753 struct common_block *common_block;
16754 struct symbol *sym;
16755
16756 for (child_die = die->child;
16757 child_die && child_die->tag;
16758 child_die = sibling_die (child_die))
16759 ++n_entries;
16760
16761 size = (sizeof (struct common_block)
16762 + (n_entries - 1) * sizeof (struct symbol *));
16763 common_block
16764 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16765 size);
16766 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16767 common_block->n_entries = 0;
16768
16769 for (child_die = die->child;
16770 child_die && child_die->tag;
16771 child_die = sibling_die (child_die))
16772 {
16773 /* Create the symbol in the DW_TAG_common_block block in the current
16774 symbol scope. */
16775 sym = new_symbol (child_die, NULL, cu);
16776 if (sym != NULL)
16777 {
16778 struct attribute *member_loc;
16779
16780 common_block->contents[common_block->n_entries++] = sym;
16781
16782 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16783 cu);
16784 if (member_loc)
16785 {
16786 /* GDB has handled this for a long time, but it is
16787 not specified by DWARF. It seems to have been
16788 emitted by gfortran at least as recently as:
16789 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16790 complaint (&symfile_complaints,
16791 _("Variable in common block has "
16792 "DW_AT_data_member_location "
16793 "- DIE at %s [in module %s]"),
16794 sect_offset_str (child_die->sect_off),
16795 objfile_name (objfile));
16796
16797 if (attr_form_is_section_offset (member_loc))
16798 dwarf2_complex_location_expr_complaint ();
16799 else if (attr_form_is_constant (member_loc)
16800 || attr_form_is_block (member_loc))
16801 {
16802 if (attr)
16803 mark_common_block_symbol_computed (sym, die, attr,
16804 member_loc, cu);
16805 }
16806 else
16807 dwarf2_complex_location_expr_complaint ();
16808 }
16809 }
16810 }
16811
16812 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16813 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16814 }
16815 }
16816
16817 /* Create a type for a C++ namespace. */
16818
16819 static struct type *
16820 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16821 {
16822 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16823 const char *previous_prefix, *name;
16824 int is_anonymous;
16825 struct type *type;
16826
16827 /* For extensions, reuse the type of the original namespace. */
16828 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16829 {
16830 struct die_info *ext_die;
16831 struct dwarf2_cu *ext_cu = cu;
16832
16833 ext_die = dwarf2_extension (die, &ext_cu);
16834 type = read_type_die (ext_die, ext_cu);
16835
16836 /* EXT_CU may not be the same as CU.
16837 Ensure TYPE is recorded with CU in die_type_hash. */
16838 return set_die_type (die, type, cu);
16839 }
16840
16841 name = namespace_name (die, &is_anonymous, cu);
16842
16843 /* Now build the name of the current namespace. */
16844
16845 previous_prefix = determine_prefix (die, cu);
16846 if (previous_prefix[0] != '\0')
16847 name = typename_concat (&objfile->objfile_obstack,
16848 previous_prefix, name, 0, cu);
16849
16850 /* Create the type. */
16851 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16852 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16853
16854 return set_die_type (die, type, cu);
16855 }
16856
16857 /* Read a namespace scope. */
16858
16859 static void
16860 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16861 {
16862 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16863 int is_anonymous;
16864
16865 /* Add a symbol associated to this if we haven't seen the namespace
16866 before. Also, add a using directive if it's an anonymous
16867 namespace. */
16868
16869 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16870 {
16871 struct type *type;
16872
16873 type = read_type_die (die, cu);
16874 new_symbol (die, type, cu);
16875
16876 namespace_name (die, &is_anonymous, cu);
16877 if (is_anonymous)
16878 {
16879 const char *previous_prefix = determine_prefix (die, cu);
16880
16881 std::vector<const char *> excludes;
16882 add_using_directive (using_directives (cu->language),
16883 previous_prefix, TYPE_NAME (type), NULL,
16884 NULL, excludes, 0, &objfile->objfile_obstack);
16885 }
16886 }
16887
16888 if (die->child != NULL)
16889 {
16890 struct die_info *child_die = die->child;
16891
16892 while (child_die && child_die->tag)
16893 {
16894 process_die (child_die, cu);
16895 child_die = sibling_die (child_die);
16896 }
16897 }
16898 }
16899
16900 /* Read a Fortran module as type. This DIE can be only a declaration used for
16901 imported module. Still we need that type as local Fortran "use ... only"
16902 declaration imports depend on the created type in determine_prefix. */
16903
16904 static struct type *
16905 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16906 {
16907 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16908 const char *module_name;
16909 struct type *type;
16910
16911 module_name = dwarf2_name (die, cu);
16912 if (!module_name)
16913 complaint (&symfile_complaints,
16914 _("DW_TAG_module has no name, offset %s"),
16915 sect_offset_str (die->sect_off));
16916 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16917
16918 /* determine_prefix uses TYPE_TAG_NAME. */
16919 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16920
16921 return set_die_type (die, type, cu);
16922 }
16923
16924 /* Read a Fortran module. */
16925
16926 static void
16927 read_module (struct die_info *die, struct dwarf2_cu *cu)
16928 {
16929 struct die_info *child_die = die->child;
16930 struct type *type;
16931
16932 type = read_type_die (die, cu);
16933 new_symbol (die, type, cu);
16934
16935 while (child_die && child_die->tag)
16936 {
16937 process_die (child_die, cu);
16938 child_die = sibling_die (child_die);
16939 }
16940 }
16941
16942 /* Return the name of the namespace represented by DIE. Set
16943 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16944 namespace. */
16945
16946 static const char *
16947 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16948 {
16949 struct die_info *current_die;
16950 const char *name = NULL;
16951
16952 /* Loop through the extensions until we find a name. */
16953
16954 for (current_die = die;
16955 current_die != NULL;
16956 current_die = dwarf2_extension (die, &cu))
16957 {
16958 /* We don't use dwarf2_name here so that we can detect the absence
16959 of a name -> anonymous namespace. */
16960 name = dwarf2_string_attr (die, DW_AT_name, cu);
16961
16962 if (name != NULL)
16963 break;
16964 }
16965
16966 /* Is it an anonymous namespace? */
16967
16968 *is_anonymous = (name == NULL);
16969 if (*is_anonymous)
16970 name = CP_ANONYMOUS_NAMESPACE_STR;
16971
16972 return name;
16973 }
16974
16975 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16976 the user defined type vector. */
16977
16978 static struct type *
16979 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16980 {
16981 struct gdbarch *gdbarch
16982 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16983 struct comp_unit_head *cu_header = &cu->header;
16984 struct type *type;
16985 struct attribute *attr_byte_size;
16986 struct attribute *attr_address_class;
16987 int byte_size, addr_class;
16988 struct type *target_type;
16989
16990 target_type = die_type (die, cu);
16991
16992 /* The die_type call above may have already set the type for this DIE. */
16993 type = get_die_type (die, cu);
16994 if (type)
16995 return type;
16996
16997 type = lookup_pointer_type (target_type);
16998
16999 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17000 if (attr_byte_size)
17001 byte_size = DW_UNSND (attr_byte_size);
17002 else
17003 byte_size = cu_header->addr_size;
17004
17005 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17006 if (attr_address_class)
17007 addr_class = DW_UNSND (attr_address_class);
17008 else
17009 addr_class = DW_ADDR_none;
17010
17011 /* If the pointer size or address class is different than the
17012 default, create a type variant marked as such and set the
17013 length accordingly. */
17014 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
17015 {
17016 if (gdbarch_address_class_type_flags_p (gdbarch))
17017 {
17018 int type_flags;
17019
17020 type_flags = gdbarch_address_class_type_flags
17021 (gdbarch, byte_size, addr_class);
17022 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17023 == 0);
17024 type = make_type_with_address_space (type, type_flags);
17025 }
17026 else if (TYPE_LENGTH (type) != byte_size)
17027 {
17028 complaint (&symfile_complaints,
17029 _("invalid pointer size %d"), byte_size);
17030 }
17031 else
17032 {
17033 /* Should we also complain about unhandled address classes? */
17034 }
17035 }
17036
17037 TYPE_LENGTH (type) = byte_size;
17038 return set_die_type (die, type, cu);
17039 }
17040
17041 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17042 the user defined type vector. */
17043
17044 static struct type *
17045 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17046 {
17047 struct type *type;
17048 struct type *to_type;
17049 struct type *domain;
17050
17051 to_type = die_type (die, cu);
17052 domain = die_containing_type (die, cu);
17053
17054 /* The calls above may have already set the type for this DIE. */
17055 type = get_die_type (die, cu);
17056 if (type)
17057 return type;
17058
17059 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17060 type = lookup_methodptr_type (to_type);
17061 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17062 {
17063 struct type *new_type
17064 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17065
17066 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17067 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17068 TYPE_VARARGS (to_type));
17069 type = lookup_methodptr_type (new_type);
17070 }
17071 else
17072 type = lookup_memberptr_type (to_type, domain);
17073
17074 return set_die_type (die, type, cu);
17075 }
17076
17077 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17078 the user defined type vector. */
17079
17080 static struct type *
17081 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17082 enum type_code refcode)
17083 {
17084 struct comp_unit_head *cu_header = &cu->header;
17085 struct type *type, *target_type;
17086 struct attribute *attr;
17087
17088 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17089
17090 target_type = die_type (die, cu);
17091
17092 /* The die_type call above may have already set the type for this DIE. */
17093 type = get_die_type (die, cu);
17094 if (type)
17095 return type;
17096
17097 type = lookup_reference_type (target_type, refcode);
17098 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17099 if (attr)
17100 {
17101 TYPE_LENGTH (type) = DW_UNSND (attr);
17102 }
17103 else
17104 {
17105 TYPE_LENGTH (type) = cu_header->addr_size;
17106 }
17107 return set_die_type (die, type, cu);
17108 }
17109
17110 /* Add the given cv-qualifiers to the element type of the array. GCC
17111 outputs DWARF type qualifiers that apply to an array, not the
17112 element type. But GDB relies on the array element type to carry
17113 the cv-qualifiers. This mimics section 6.7.3 of the C99
17114 specification. */
17115
17116 static struct type *
17117 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17118 struct type *base_type, int cnst, int voltl)
17119 {
17120 struct type *el_type, *inner_array;
17121
17122 base_type = copy_type (base_type);
17123 inner_array = base_type;
17124
17125 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17126 {
17127 TYPE_TARGET_TYPE (inner_array) =
17128 copy_type (TYPE_TARGET_TYPE (inner_array));
17129 inner_array = TYPE_TARGET_TYPE (inner_array);
17130 }
17131
17132 el_type = TYPE_TARGET_TYPE (inner_array);
17133 cnst |= TYPE_CONST (el_type);
17134 voltl |= TYPE_VOLATILE (el_type);
17135 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17136
17137 return set_die_type (die, base_type, cu);
17138 }
17139
17140 static struct type *
17141 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17142 {
17143 struct type *base_type, *cv_type;
17144
17145 base_type = die_type (die, cu);
17146
17147 /* The die_type call above may have already set the type for this DIE. */
17148 cv_type = get_die_type (die, cu);
17149 if (cv_type)
17150 return cv_type;
17151
17152 /* In case the const qualifier is applied to an array type, the element type
17153 is so qualified, not the array type (section 6.7.3 of C99). */
17154 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17155 return add_array_cv_type (die, cu, base_type, 1, 0);
17156
17157 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17158 return set_die_type (die, cv_type, cu);
17159 }
17160
17161 static struct type *
17162 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17163 {
17164 struct type *base_type, *cv_type;
17165
17166 base_type = die_type (die, cu);
17167
17168 /* The die_type call above may have already set the type for this DIE. */
17169 cv_type = get_die_type (die, cu);
17170 if (cv_type)
17171 return cv_type;
17172
17173 /* In case the volatile qualifier is applied to an array type, the
17174 element type is so qualified, not the array type (section 6.7.3
17175 of C99). */
17176 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17177 return add_array_cv_type (die, cu, base_type, 0, 1);
17178
17179 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17180 return set_die_type (die, cv_type, cu);
17181 }
17182
17183 /* Handle DW_TAG_restrict_type. */
17184
17185 static struct type *
17186 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17187 {
17188 struct type *base_type, *cv_type;
17189
17190 base_type = die_type (die, cu);
17191
17192 /* The die_type call above may have already set the type for this DIE. */
17193 cv_type = get_die_type (die, cu);
17194 if (cv_type)
17195 return cv_type;
17196
17197 cv_type = make_restrict_type (base_type);
17198 return set_die_type (die, cv_type, cu);
17199 }
17200
17201 /* Handle DW_TAG_atomic_type. */
17202
17203 static struct type *
17204 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17205 {
17206 struct type *base_type, *cv_type;
17207
17208 base_type = die_type (die, cu);
17209
17210 /* The die_type call above may have already set the type for this DIE. */
17211 cv_type = get_die_type (die, cu);
17212 if (cv_type)
17213 return cv_type;
17214
17215 cv_type = make_atomic_type (base_type);
17216 return set_die_type (die, cv_type, cu);
17217 }
17218
17219 /* Extract all information from a DW_TAG_string_type DIE and add to
17220 the user defined type vector. It isn't really a user defined type,
17221 but it behaves like one, with other DIE's using an AT_user_def_type
17222 attribute to reference it. */
17223
17224 static struct type *
17225 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17226 {
17227 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17228 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17229 struct type *type, *range_type, *index_type, *char_type;
17230 struct attribute *attr;
17231 unsigned int length;
17232
17233 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17234 if (attr)
17235 {
17236 length = DW_UNSND (attr);
17237 }
17238 else
17239 {
17240 /* Check for the DW_AT_byte_size attribute. */
17241 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17242 if (attr)
17243 {
17244 length = DW_UNSND (attr);
17245 }
17246 else
17247 {
17248 length = 1;
17249 }
17250 }
17251
17252 index_type = objfile_type (objfile)->builtin_int;
17253 range_type = create_static_range_type (NULL, index_type, 1, length);
17254 char_type = language_string_char_type (cu->language_defn, gdbarch);
17255 type = create_string_type (NULL, char_type, range_type);
17256
17257 return set_die_type (die, type, cu);
17258 }
17259
17260 /* Assuming that DIE corresponds to a function, returns nonzero
17261 if the function is prototyped. */
17262
17263 static int
17264 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17265 {
17266 struct attribute *attr;
17267
17268 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17269 if (attr && (DW_UNSND (attr) != 0))
17270 return 1;
17271
17272 /* The DWARF standard implies that the DW_AT_prototyped attribute
17273 is only meaninful for C, but the concept also extends to other
17274 languages that allow unprototyped functions (Eg: Objective C).
17275 For all other languages, assume that functions are always
17276 prototyped. */
17277 if (cu->language != language_c
17278 && cu->language != language_objc
17279 && cu->language != language_opencl)
17280 return 1;
17281
17282 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17283 prototyped and unprototyped functions; default to prototyped,
17284 since that is more common in modern code (and RealView warns
17285 about unprototyped functions). */
17286 if (producer_is_realview (cu->producer))
17287 return 1;
17288
17289 return 0;
17290 }
17291
17292 /* Handle DIES due to C code like:
17293
17294 struct foo
17295 {
17296 int (*funcp)(int a, long l);
17297 int b;
17298 };
17299
17300 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17301
17302 static struct type *
17303 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17304 {
17305 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17306 struct type *type; /* Type that this function returns. */
17307 struct type *ftype; /* Function that returns above type. */
17308 struct attribute *attr;
17309
17310 type = die_type (die, cu);
17311
17312 /* The die_type call above may have already set the type for this DIE. */
17313 ftype = get_die_type (die, cu);
17314 if (ftype)
17315 return ftype;
17316
17317 ftype = lookup_function_type (type);
17318
17319 if (prototyped_function_p (die, cu))
17320 TYPE_PROTOTYPED (ftype) = 1;
17321
17322 /* Store the calling convention in the type if it's available in
17323 the subroutine die. Otherwise set the calling convention to
17324 the default value DW_CC_normal. */
17325 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17326 if (attr)
17327 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17328 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17329 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17330 else
17331 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17332
17333 /* Record whether the function returns normally to its caller or not
17334 if the DWARF producer set that information. */
17335 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17336 if (attr && (DW_UNSND (attr) != 0))
17337 TYPE_NO_RETURN (ftype) = 1;
17338
17339 /* We need to add the subroutine type to the die immediately so
17340 we don't infinitely recurse when dealing with parameters
17341 declared as the same subroutine type. */
17342 set_die_type (die, ftype, cu);
17343
17344 if (die->child != NULL)
17345 {
17346 struct type *void_type = objfile_type (objfile)->builtin_void;
17347 struct die_info *child_die;
17348 int nparams, iparams;
17349
17350 /* Count the number of parameters.
17351 FIXME: GDB currently ignores vararg functions, but knows about
17352 vararg member functions. */
17353 nparams = 0;
17354 child_die = die->child;
17355 while (child_die && child_die->tag)
17356 {
17357 if (child_die->tag == DW_TAG_formal_parameter)
17358 nparams++;
17359 else if (child_die->tag == DW_TAG_unspecified_parameters)
17360 TYPE_VARARGS (ftype) = 1;
17361 child_die = sibling_die (child_die);
17362 }
17363
17364 /* Allocate storage for parameters and fill them in. */
17365 TYPE_NFIELDS (ftype) = nparams;
17366 TYPE_FIELDS (ftype) = (struct field *)
17367 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17368
17369 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17370 even if we error out during the parameters reading below. */
17371 for (iparams = 0; iparams < nparams; iparams++)
17372 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17373
17374 iparams = 0;
17375 child_die = die->child;
17376 while (child_die && child_die->tag)
17377 {
17378 if (child_die->tag == DW_TAG_formal_parameter)
17379 {
17380 struct type *arg_type;
17381
17382 /* DWARF version 2 has no clean way to discern C++
17383 static and non-static member functions. G++ helps
17384 GDB by marking the first parameter for non-static
17385 member functions (which is the this pointer) as
17386 artificial. We pass this information to
17387 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17388
17389 DWARF version 3 added DW_AT_object_pointer, which GCC
17390 4.5 does not yet generate. */
17391 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17392 if (attr)
17393 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17394 else
17395 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17396 arg_type = die_type (child_die, cu);
17397
17398 /* RealView does not mark THIS as const, which the testsuite
17399 expects. GCC marks THIS as const in method definitions,
17400 but not in the class specifications (GCC PR 43053). */
17401 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17402 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17403 {
17404 int is_this = 0;
17405 struct dwarf2_cu *arg_cu = cu;
17406 const char *name = dwarf2_name (child_die, cu);
17407
17408 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17409 if (attr)
17410 {
17411 /* If the compiler emits this, use it. */
17412 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17413 is_this = 1;
17414 }
17415 else if (name && strcmp (name, "this") == 0)
17416 /* Function definitions will have the argument names. */
17417 is_this = 1;
17418 else if (name == NULL && iparams == 0)
17419 /* Declarations may not have the names, so like
17420 elsewhere in GDB, assume an artificial first
17421 argument is "this". */
17422 is_this = 1;
17423
17424 if (is_this)
17425 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17426 arg_type, 0);
17427 }
17428
17429 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17430 iparams++;
17431 }
17432 child_die = sibling_die (child_die);
17433 }
17434 }
17435
17436 return ftype;
17437 }
17438
17439 static struct type *
17440 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17441 {
17442 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17443 const char *name = NULL;
17444 struct type *this_type, *target_type;
17445
17446 name = dwarf2_full_name (NULL, die, cu);
17447 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17448 TYPE_TARGET_STUB (this_type) = 1;
17449 set_die_type (die, this_type, cu);
17450 target_type = die_type (die, cu);
17451 if (target_type != this_type)
17452 TYPE_TARGET_TYPE (this_type) = target_type;
17453 else
17454 {
17455 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17456 spec and cause infinite loops in GDB. */
17457 complaint (&symfile_complaints,
17458 _("Self-referential DW_TAG_typedef "
17459 "- DIE at %s [in module %s]"),
17460 sect_offset_str (die->sect_off), objfile_name (objfile));
17461 TYPE_TARGET_TYPE (this_type) = NULL;
17462 }
17463 return this_type;
17464 }
17465
17466 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17467 (which may be different from NAME) to the architecture back-end to allow
17468 it to guess the correct format if necessary. */
17469
17470 static struct type *
17471 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17472 const char *name_hint)
17473 {
17474 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17475 const struct floatformat **format;
17476 struct type *type;
17477
17478 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17479 if (format)
17480 type = init_float_type (objfile, bits, name, format);
17481 else
17482 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17483
17484 return type;
17485 }
17486
17487 /* Find a representation of a given base type and install
17488 it in the TYPE field of the die. */
17489
17490 static struct type *
17491 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17492 {
17493 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17494 struct type *type;
17495 struct attribute *attr;
17496 int encoding = 0, bits = 0;
17497 const char *name;
17498
17499 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17500 if (attr)
17501 {
17502 encoding = DW_UNSND (attr);
17503 }
17504 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17505 if (attr)
17506 {
17507 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17508 }
17509 name = dwarf2_name (die, cu);
17510 if (!name)
17511 {
17512 complaint (&symfile_complaints,
17513 _("DW_AT_name missing from DW_TAG_base_type"));
17514 }
17515
17516 switch (encoding)
17517 {
17518 case DW_ATE_address:
17519 /* Turn DW_ATE_address into a void * pointer. */
17520 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17521 type = init_pointer_type (objfile, bits, name, type);
17522 break;
17523 case DW_ATE_boolean:
17524 type = init_boolean_type (objfile, bits, 1, name);
17525 break;
17526 case DW_ATE_complex_float:
17527 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17528 type = init_complex_type (objfile, name, type);
17529 break;
17530 case DW_ATE_decimal_float:
17531 type = init_decfloat_type (objfile, bits, name);
17532 break;
17533 case DW_ATE_float:
17534 type = dwarf2_init_float_type (objfile, bits, name, name);
17535 break;
17536 case DW_ATE_signed:
17537 type = init_integer_type (objfile, bits, 0, name);
17538 break;
17539 case DW_ATE_unsigned:
17540 if (cu->language == language_fortran
17541 && name
17542 && startswith (name, "character("))
17543 type = init_character_type (objfile, bits, 1, name);
17544 else
17545 type = init_integer_type (objfile, bits, 1, name);
17546 break;
17547 case DW_ATE_signed_char:
17548 if (cu->language == language_ada || cu->language == language_m2
17549 || cu->language == language_pascal
17550 || cu->language == language_fortran)
17551 type = init_character_type (objfile, bits, 0, name);
17552 else
17553 type = init_integer_type (objfile, bits, 0, name);
17554 break;
17555 case DW_ATE_unsigned_char:
17556 if (cu->language == language_ada || cu->language == language_m2
17557 || cu->language == language_pascal
17558 || cu->language == language_fortran
17559 || cu->language == language_rust)
17560 type = init_character_type (objfile, bits, 1, name);
17561 else
17562 type = init_integer_type (objfile, bits, 1, name);
17563 break;
17564 case DW_ATE_UTF:
17565 {
17566 gdbarch *arch = get_objfile_arch (objfile);
17567
17568 if (bits == 16)
17569 type = builtin_type (arch)->builtin_char16;
17570 else if (bits == 32)
17571 type = builtin_type (arch)->builtin_char32;
17572 else
17573 {
17574 complaint (&symfile_complaints,
17575 _("unsupported DW_ATE_UTF bit size: '%d'"),
17576 bits);
17577 type = init_integer_type (objfile, bits, 1, name);
17578 }
17579 return set_die_type (die, type, cu);
17580 }
17581 break;
17582
17583 default:
17584 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17585 dwarf_type_encoding_name (encoding));
17586 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17587 break;
17588 }
17589
17590 if (name && strcmp (name, "char") == 0)
17591 TYPE_NOSIGN (type) = 1;
17592
17593 return set_die_type (die, type, cu);
17594 }
17595
17596 /* Parse dwarf attribute if it's a block, reference or constant and put the
17597 resulting value of the attribute into struct bound_prop.
17598 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17599
17600 static int
17601 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17602 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17603 {
17604 struct dwarf2_property_baton *baton;
17605 struct obstack *obstack
17606 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17607
17608 if (attr == NULL || prop == NULL)
17609 return 0;
17610
17611 if (attr_form_is_block (attr))
17612 {
17613 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17614 baton->referenced_type = NULL;
17615 baton->locexpr.per_cu = cu->per_cu;
17616 baton->locexpr.size = DW_BLOCK (attr)->size;
17617 baton->locexpr.data = DW_BLOCK (attr)->data;
17618 prop->data.baton = baton;
17619 prop->kind = PROP_LOCEXPR;
17620 gdb_assert (prop->data.baton != NULL);
17621 }
17622 else if (attr_form_is_ref (attr))
17623 {
17624 struct dwarf2_cu *target_cu = cu;
17625 struct die_info *target_die;
17626 struct attribute *target_attr;
17627
17628 target_die = follow_die_ref (die, attr, &target_cu);
17629 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17630 if (target_attr == NULL)
17631 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17632 target_cu);
17633 if (target_attr == NULL)
17634 return 0;
17635
17636 switch (target_attr->name)
17637 {
17638 case DW_AT_location:
17639 if (attr_form_is_section_offset (target_attr))
17640 {
17641 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17642 baton->referenced_type = die_type (target_die, target_cu);
17643 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17644 prop->data.baton = baton;
17645 prop->kind = PROP_LOCLIST;
17646 gdb_assert (prop->data.baton != NULL);
17647 }
17648 else if (attr_form_is_block (target_attr))
17649 {
17650 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17651 baton->referenced_type = die_type (target_die, target_cu);
17652 baton->locexpr.per_cu = cu->per_cu;
17653 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17654 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17655 prop->data.baton = baton;
17656 prop->kind = PROP_LOCEXPR;
17657 gdb_assert (prop->data.baton != NULL);
17658 }
17659 else
17660 {
17661 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17662 "dynamic property");
17663 return 0;
17664 }
17665 break;
17666 case DW_AT_data_member_location:
17667 {
17668 LONGEST offset;
17669
17670 if (!handle_data_member_location (target_die, target_cu,
17671 &offset))
17672 return 0;
17673
17674 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17675 baton->referenced_type = read_type_die (target_die->parent,
17676 target_cu);
17677 baton->offset_info.offset = offset;
17678 baton->offset_info.type = die_type (target_die, target_cu);
17679 prop->data.baton = baton;
17680 prop->kind = PROP_ADDR_OFFSET;
17681 break;
17682 }
17683 }
17684 }
17685 else if (attr_form_is_constant (attr))
17686 {
17687 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17688 prop->kind = PROP_CONST;
17689 }
17690 else
17691 {
17692 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17693 dwarf2_name (die, cu));
17694 return 0;
17695 }
17696
17697 return 1;
17698 }
17699
17700 /* Read the given DW_AT_subrange DIE. */
17701
17702 static struct type *
17703 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17704 {
17705 struct type *base_type, *orig_base_type;
17706 struct type *range_type;
17707 struct attribute *attr;
17708 struct dynamic_prop low, high;
17709 int low_default_is_valid;
17710 int high_bound_is_count = 0;
17711 const char *name;
17712 LONGEST negative_mask;
17713
17714 orig_base_type = die_type (die, cu);
17715 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17716 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17717 creating the range type, but we use the result of check_typedef
17718 when examining properties of the type. */
17719 base_type = check_typedef (orig_base_type);
17720
17721 /* The die_type call above may have already set the type for this DIE. */
17722 range_type = get_die_type (die, cu);
17723 if (range_type)
17724 return range_type;
17725
17726 low.kind = PROP_CONST;
17727 high.kind = PROP_CONST;
17728 high.data.const_val = 0;
17729
17730 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17731 omitting DW_AT_lower_bound. */
17732 switch (cu->language)
17733 {
17734 case language_c:
17735 case language_cplus:
17736 low.data.const_val = 0;
17737 low_default_is_valid = 1;
17738 break;
17739 case language_fortran:
17740 low.data.const_val = 1;
17741 low_default_is_valid = 1;
17742 break;
17743 case language_d:
17744 case language_objc:
17745 case language_rust:
17746 low.data.const_val = 0;
17747 low_default_is_valid = (cu->header.version >= 4);
17748 break;
17749 case language_ada:
17750 case language_m2:
17751 case language_pascal:
17752 low.data.const_val = 1;
17753 low_default_is_valid = (cu->header.version >= 4);
17754 break;
17755 default:
17756 low.data.const_val = 0;
17757 low_default_is_valid = 0;
17758 break;
17759 }
17760
17761 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17762 if (attr)
17763 attr_to_dynamic_prop (attr, die, cu, &low);
17764 else if (!low_default_is_valid)
17765 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17766 "- DIE at %s [in module %s]"),
17767 sect_offset_str (die->sect_off),
17768 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17769
17770 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
17771 if (!attr_to_dynamic_prop (attr, die, cu, &high))
17772 {
17773 attr = dwarf2_attr (die, DW_AT_count, cu);
17774 if (attr_to_dynamic_prop (attr, die, cu, &high))
17775 {
17776 /* If bounds are constant do the final calculation here. */
17777 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17778 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17779 else
17780 high_bound_is_count = 1;
17781 }
17782 }
17783
17784 /* Dwarf-2 specifications explicitly allows to create subrange types
17785 without specifying a base type.
17786 In that case, the base type must be set to the type of
17787 the lower bound, upper bound or count, in that order, if any of these
17788 three attributes references an object that has a type.
17789 If no base type is found, the Dwarf-2 specifications say that
17790 a signed integer type of size equal to the size of an address should
17791 be used.
17792 For the following C code: `extern char gdb_int [];'
17793 GCC produces an empty range DIE.
17794 FIXME: muller/2010-05-28: Possible references to object for low bound,
17795 high bound or count are not yet handled by this code. */
17796 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17797 {
17798 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17799 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17800 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17801 struct type *int_type = objfile_type (objfile)->builtin_int;
17802
17803 /* Test "int", "long int", and "long long int" objfile types,
17804 and select the first one having a size above or equal to the
17805 architecture address size. */
17806 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17807 base_type = int_type;
17808 else
17809 {
17810 int_type = objfile_type (objfile)->builtin_long;
17811 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17812 base_type = int_type;
17813 else
17814 {
17815 int_type = objfile_type (objfile)->builtin_long_long;
17816 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17817 base_type = int_type;
17818 }
17819 }
17820 }
17821
17822 /* Normally, the DWARF producers are expected to use a signed
17823 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17824 But this is unfortunately not always the case, as witnessed
17825 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17826 is used instead. To work around that ambiguity, we treat
17827 the bounds as signed, and thus sign-extend their values, when
17828 the base type is signed. */
17829 negative_mask =
17830 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17831 if (low.kind == PROP_CONST
17832 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17833 low.data.const_val |= negative_mask;
17834 if (high.kind == PROP_CONST
17835 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17836 high.data.const_val |= negative_mask;
17837
17838 range_type = create_range_type (NULL, orig_base_type, &low, &high);
17839
17840 if (high_bound_is_count)
17841 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17842
17843 /* Ada expects an empty array on no boundary attributes. */
17844 if (attr == NULL && cu->language != language_ada)
17845 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17846
17847 name = dwarf2_name (die, cu);
17848 if (name)
17849 TYPE_NAME (range_type) = name;
17850
17851 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17852 if (attr)
17853 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17854
17855 set_die_type (die, range_type, cu);
17856
17857 /* set_die_type should be already done. */
17858 set_descriptive_type (range_type, die, cu);
17859
17860 return range_type;
17861 }
17862
17863 static struct type *
17864 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17865 {
17866 struct type *type;
17867
17868 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17869 NULL);
17870 TYPE_NAME (type) = dwarf2_name (die, cu);
17871
17872 /* In Ada, an unspecified type is typically used when the description
17873 of the type is defered to a different unit. When encountering
17874 such a type, we treat it as a stub, and try to resolve it later on,
17875 when needed. */
17876 if (cu->language == language_ada)
17877 TYPE_STUB (type) = 1;
17878
17879 return set_die_type (die, type, cu);
17880 }
17881
17882 /* Read a single die and all its descendents. Set the die's sibling
17883 field to NULL; set other fields in the die correctly, and set all
17884 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17885 location of the info_ptr after reading all of those dies. PARENT
17886 is the parent of the die in question. */
17887
17888 static struct die_info *
17889 read_die_and_children (const struct die_reader_specs *reader,
17890 const gdb_byte *info_ptr,
17891 const gdb_byte **new_info_ptr,
17892 struct die_info *parent)
17893 {
17894 struct die_info *die;
17895 const gdb_byte *cur_ptr;
17896 int has_children;
17897
17898 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17899 if (die == NULL)
17900 {
17901 *new_info_ptr = cur_ptr;
17902 return NULL;
17903 }
17904 store_in_ref_table (die, reader->cu);
17905
17906 if (has_children)
17907 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17908 else
17909 {
17910 die->child = NULL;
17911 *new_info_ptr = cur_ptr;
17912 }
17913
17914 die->sibling = NULL;
17915 die->parent = parent;
17916 return die;
17917 }
17918
17919 /* Read a die, all of its descendents, and all of its siblings; set
17920 all of the fields of all of the dies correctly. Arguments are as
17921 in read_die_and_children. */
17922
17923 static struct die_info *
17924 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17925 const gdb_byte *info_ptr,
17926 const gdb_byte **new_info_ptr,
17927 struct die_info *parent)
17928 {
17929 struct die_info *first_die, *last_sibling;
17930 const gdb_byte *cur_ptr;
17931
17932 cur_ptr = info_ptr;
17933 first_die = last_sibling = NULL;
17934
17935 while (1)
17936 {
17937 struct die_info *die
17938 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17939
17940 if (die == NULL)
17941 {
17942 *new_info_ptr = cur_ptr;
17943 return first_die;
17944 }
17945
17946 if (!first_die)
17947 first_die = die;
17948 else
17949 last_sibling->sibling = die;
17950
17951 last_sibling = die;
17952 }
17953 }
17954
17955 /* Read a die, all of its descendents, and all of its siblings; set
17956 all of the fields of all of the dies correctly. Arguments are as
17957 in read_die_and_children.
17958 This the main entry point for reading a DIE and all its children. */
17959
17960 static struct die_info *
17961 read_die_and_siblings (const struct die_reader_specs *reader,
17962 const gdb_byte *info_ptr,
17963 const gdb_byte **new_info_ptr,
17964 struct die_info *parent)
17965 {
17966 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17967 new_info_ptr, parent);
17968
17969 if (dwarf_die_debug)
17970 {
17971 fprintf_unfiltered (gdb_stdlog,
17972 "Read die from %s@0x%x of %s:\n",
17973 get_section_name (reader->die_section),
17974 (unsigned) (info_ptr - reader->die_section->buffer),
17975 bfd_get_filename (reader->abfd));
17976 dump_die (die, dwarf_die_debug);
17977 }
17978
17979 return die;
17980 }
17981
17982 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17983 attributes.
17984 The caller is responsible for filling in the extra attributes
17985 and updating (*DIEP)->num_attrs.
17986 Set DIEP to point to a newly allocated die with its information,
17987 except for its child, sibling, and parent fields.
17988 Set HAS_CHILDREN to tell whether the die has children or not. */
17989
17990 static const gdb_byte *
17991 read_full_die_1 (const struct die_reader_specs *reader,
17992 struct die_info **diep, const gdb_byte *info_ptr,
17993 int *has_children, int num_extra_attrs)
17994 {
17995 unsigned int abbrev_number, bytes_read, i;
17996 struct abbrev_info *abbrev;
17997 struct die_info *die;
17998 struct dwarf2_cu *cu = reader->cu;
17999 bfd *abfd = reader->abfd;
18000
18001 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18002 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18003 info_ptr += bytes_read;
18004 if (!abbrev_number)
18005 {
18006 *diep = NULL;
18007 *has_children = 0;
18008 return info_ptr;
18009 }
18010
18011 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18012 if (!abbrev)
18013 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18014 abbrev_number,
18015 bfd_get_filename (abfd));
18016
18017 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18018 die->sect_off = sect_off;
18019 die->tag = abbrev->tag;
18020 die->abbrev = abbrev_number;
18021
18022 /* Make the result usable.
18023 The caller needs to update num_attrs after adding the extra
18024 attributes. */
18025 die->num_attrs = abbrev->num_attrs;
18026
18027 for (i = 0; i < abbrev->num_attrs; ++i)
18028 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18029 info_ptr);
18030
18031 *diep = die;
18032 *has_children = abbrev->has_children;
18033 return info_ptr;
18034 }
18035
18036 /* Read a die and all its attributes.
18037 Set DIEP to point to a newly allocated die with its information,
18038 except for its child, sibling, and parent fields.
18039 Set HAS_CHILDREN to tell whether the die has children or not. */
18040
18041 static const gdb_byte *
18042 read_full_die (const struct die_reader_specs *reader,
18043 struct die_info **diep, const gdb_byte *info_ptr,
18044 int *has_children)
18045 {
18046 const gdb_byte *result;
18047
18048 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18049
18050 if (dwarf_die_debug)
18051 {
18052 fprintf_unfiltered (gdb_stdlog,
18053 "Read die from %s@0x%x of %s:\n",
18054 get_section_name (reader->die_section),
18055 (unsigned) (info_ptr - reader->die_section->buffer),
18056 bfd_get_filename (reader->abfd));
18057 dump_die (*diep, dwarf_die_debug);
18058 }
18059
18060 return result;
18061 }
18062 \f
18063 /* Abbreviation tables.
18064
18065 In DWARF version 2, the description of the debugging information is
18066 stored in a separate .debug_abbrev section. Before we read any
18067 dies from a section we read in all abbreviations and install them
18068 in a hash table. */
18069
18070 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18071
18072 struct abbrev_info *
18073 abbrev_table::alloc_abbrev ()
18074 {
18075 struct abbrev_info *abbrev;
18076
18077 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18078 memset (abbrev, 0, sizeof (struct abbrev_info));
18079
18080 return abbrev;
18081 }
18082
18083 /* Add an abbreviation to the table. */
18084
18085 void
18086 abbrev_table::add_abbrev (unsigned int abbrev_number,
18087 struct abbrev_info *abbrev)
18088 {
18089 unsigned int hash_number;
18090
18091 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18092 abbrev->next = m_abbrevs[hash_number];
18093 m_abbrevs[hash_number] = abbrev;
18094 }
18095
18096 /* Look up an abbrev in the table.
18097 Returns NULL if the abbrev is not found. */
18098
18099 struct abbrev_info *
18100 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18101 {
18102 unsigned int hash_number;
18103 struct abbrev_info *abbrev;
18104
18105 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18106 abbrev = m_abbrevs[hash_number];
18107
18108 while (abbrev)
18109 {
18110 if (abbrev->number == abbrev_number)
18111 return abbrev;
18112 abbrev = abbrev->next;
18113 }
18114 return NULL;
18115 }
18116
18117 /* Read in an abbrev table. */
18118
18119 static abbrev_table_up
18120 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18121 struct dwarf2_section_info *section,
18122 sect_offset sect_off)
18123 {
18124 struct objfile *objfile = dwarf2_per_objfile->objfile;
18125 bfd *abfd = get_section_bfd_owner (section);
18126 const gdb_byte *abbrev_ptr;
18127 struct abbrev_info *cur_abbrev;
18128 unsigned int abbrev_number, bytes_read, abbrev_name;
18129 unsigned int abbrev_form;
18130 struct attr_abbrev *cur_attrs;
18131 unsigned int allocated_attrs;
18132
18133 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18134
18135 dwarf2_read_section (objfile, section);
18136 abbrev_ptr = section->buffer + to_underlying (sect_off);
18137 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18138 abbrev_ptr += bytes_read;
18139
18140 allocated_attrs = ATTR_ALLOC_CHUNK;
18141 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18142
18143 /* Loop until we reach an abbrev number of 0. */
18144 while (abbrev_number)
18145 {
18146 cur_abbrev = abbrev_table->alloc_abbrev ();
18147
18148 /* read in abbrev header */
18149 cur_abbrev->number = abbrev_number;
18150 cur_abbrev->tag
18151 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18152 abbrev_ptr += bytes_read;
18153 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18154 abbrev_ptr += 1;
18155
18156 /* now read in declarations */
18157 for (;;)
18158 {
18159 LONGEST implicit_const;
18160
18161 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18162 abbrev_ptr += bytes_read;
18163 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18164 abbrev_ptr += bytes_read;
18165 if (abbrev_form == DW_FORM_implicit_const)
18166 {
18167 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18168 &bytes_read);
18169 abbrev_ptr += bytes_read;
18170 }
18171 else
18172 {
18173 /* Initialize it due to a false compiler warning. */
18174 implicit_const = -1;
18175 }
18176
18177 if (abbrev_name == 0)
18178 break;
18179
18180 if (cur_abbrev->num_attrs == allocated_attrs)
18181 {
18182 allocated_attrs += ATTR_ALLOC_CHUNK;
18183 cur_attrs
18184 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18185 }
18186
18187 cur_attrs[cur_abbrev->num_attrs].name
18188 = (enum dwarf_attribute) abbrev_name;
18189 cur_attrs[cur_abbrev->num_attrs].form
18190 = (enum dwarf_form) abbrev_form;
18191 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18192 ++cur_abbrev->num_attrs;
18193 }
18194
18195 cur_abbrev->attrs =
18196 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18197 cur_abbrev->num_attrs);
18198 memcpy (cur_abbrev->attrs, cur_attrs,
18199 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18200
18201 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18202
18203 /* Get next abbreviation.
18204 Under Irix6 the abbreviations for a compilation unit are not
18205 always properly terminated with an abbrev number of 0.
18206 Exit loop if we encounter an abbreviation which we have
18207 already read (which means we are about to read the abbreviations
18208 for the next compile unit) or if the end of the abbreviation
18209 table is reached. */
18210 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18211 break;
18212 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18213 abbrev_ptr += bytes_read;
18214 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18215 break;
18216 }
18217
18218 xfree (cur_attrs);
18219 return abbrev_table;
18220 }
18221
18222 /* Returns nonzero if TAG represents a type that we might generate a partial
18223 symbol for. */
18224
18225 static int
18226 is_type_tag_for_partial (int tag)
18227 {
18228 switch (tag)
18229 {
18230 #if 0
18231 /* Some types that would be reasonable to generate partial symbols for,
18232 that we don't at present. */
18233 case DW_TAG_array_type:
18234 case DW_TAG_file_type:
18235 case DW_TAG_ptr_to_member_type:
18236 case DW_TAG_set_type:
18237 case DW_TAG_string_type:
18238 case DW_TAG_subroutine_type:
18239 #endif
18240 case DW_TAG_base_type:
18241 case DW_TAG_class_type:
18242 case DW_TAG_interface_type:
18243 case DW_TAG_enumeration_type:
18244 case DW_TAG_structure_type:
18245 case DW_TAG_subrange_type:
18246 case DW_TAG_typedef:
18247 case DW_TAG_union_type:
18248 return 1;
18249 default:
18250 return 0;
18251 }
18252 }
18253
18254 /* Load all DIEs that are interesting for partial symbols into memory. */
18255
18256 static struct partial_die_info *
18257 load_partial_dies (const struct die_reader_specs *reader,
18258 const gdb_byte *info_ptr, int building_psymtab)
18259 {
18260 struct dwarf2_cu *cu = reader->cu;
18261 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18262 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18263 unsigned int bytes_read;
18264 unsigned int load_all = 0;
18265 int nesting_level = 1;
18266
18267 parent_die = NULL;
18268 last_die = NULL;
18269
18270 gdb_assert (cu->per_cu != NULL);
18271 if (cu->per_cu->load_all_dies)
18272 load_all = 1;
18273
18274 cu->partial_dies
18275 = htab_create_alloc_ex (cu->header.length / 12,
18276 partial_die_hash,
18277 partial_die_eq,
18278 NULL,
18279 &cu->comp_unit_obstack,
18280 hashtab_obstack_allocate,
18281 dummy_obstack_deallocate);
18282
18283 while (1)
18284 {
18285 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18286
18287 /* A NULL abbrev means the end of a series of children. */
18288 if (abbrev == NULL)
18289 {
18290 if (--nesting_level == 0)
18291 return first_die;
18292
18293 info_ptr += bytes_read;
18294 last_die = parent_die;
18295 parent_die = parent_die->die_parent;
18296 continue;
18297 }
18298
18299 /* Check for template arguments. We never save these; if
18300 they're seen, we just mark the parent, and go on our way. */
18301 if (parent_die != NULL
18302 && cu->language == language_cplus
18303 && (abbrev->tag == DW_TAG_template_type_param
18304 || abbrev->tag == DW_TAG_template_value_param))
18305 {
18306 parent_die->has_template_arguments = 1;
18307
18308 if (!load_all)
18309 {
18310 /* We don't need a partial DIE for the template argument. */
18311 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18312 continue;
18313 }
18314 }
18315
18316 /* We only recurse into c++ subprograms looking for template arguments.
18317 Skip their other children. */
18318 if (!load_all
18319 && cu->language == language_cplus
18320 && parent_die != NULL
18321 && parent_die->tag == DW_TAG_subprogram)
18322 {
18323 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18324 continue;
18325 }
18326
18327 /* Check whether this DIE is interesting enough to save. Normally
18328 we would not be interested in members here, but there may be
18329 later variables referencing them via DW_AT_specification (for
18330 static members). */
18331 if (!load_all
18332 && !is_type_tag_for_partial (abbrev->tag)
18333 && abbrev->tag != DW_TAG_constant
18334 && abbrev->tag != DW_TAG_enumerator
18335 && abbrev->tag != DW_TAG_subprogram
18336 && abbrev->tag != DW_TAG_inlined_subroutine
18337 && abbrev->tag != DW_TAG_lexical_block
18338 && abbrev->tag != DW_TAG_variable
18339 && abbrev->tag != DW_TAG_namespace
18340 && abbrev->tag != DW_TAG_module
18341 && abbrev->tag != DW_TAG_member
18342 && abbrev->tag != DW_TAG_imported_unit
18343 && abbrev->tag != DW_TAG_imported_declaration)
18344 {
18345 /* Otherwise we skip to the next sibling, if any. */
18346 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18347 continue;
18348 }
18349
18350 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18351 abbrev);
18352
18353 info_ptr = read_partial_die (reader, &pdi, *abbrev,
18354 (const gdb_byte *) info_ptr + bytes_read);
18355
18356 /* This two-pass algorithm for processing partial symbols has a
18357 high cost in cache pressure. Thus, handle some simple cases
18358 here which cover the majority of C partial symbols. DIEs
18359 which neither have specification tags in them, nor could have
18360 specification tags elsewhere pointing at them, can simply be
18361 processed and discarded.
18362
18363 This segment is also optional; scan_partial_symbols and
18364 add_partial_symbol will handle these DIEs if we chain
18365 them in normally. When compilers which do not emit large
18366 quantities of duplicate debug information are more common,
18367 this code can probably be removed. */
18368
18369 /* Any complete simple types at the top level (pretty much all
18370 of them, for a language without namespaces), can be processed
18371 directly. */
18372 if (parent_die == NULL
18373 && pdi.has_specification == 0
18374 && pdi.is_declaration == 0
18375 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18376 || pdi.tag == DW_TAG_base_type
18377 || pdi.tag == DW_TAG_subrange_type))
18378 {
18379 if (building_psymtab && pdi.name != NULL)
18380 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18381 VAR_DOMAIN, LOC_TYPEDEF,
18382 &objfile->static_psymbols,
18383 0, cu->language, objfile);
18384 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18385 continue;
18386 }
18387
18388 /* The exception for DW_TAG_typedef with has_children above is
18389 a workaround of GCC PR debug/47510. In the case of this complaint
18390 type_name_no_tag_or_error will error on such types later.
18391
18392 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18393 it could not find the child DIEs referenced later, this is checked
18394 above. In correct DWARF DW_TAG_typedef should have no children. */
18395
18396 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18397 complaint (&symfile_complaints,
18398 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18399 "- DIE at %s [in module %s]"),
18400 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18401
18402 /* If we're at the second level, and we're an enumerator, and
18403 our parent has no specification (meaning possibly lives in a
18404 namespace elsewhere), then we can add the partial symbol now
18405 instead of queueing it. */
18406 if (pdi.tag == DW_TAG_enumerator
18407 && parent_die != NULL
18408 && parent_die->die_parent == NULL
18409 && parent_die->tag == DW_TAG_enumeration_type
18410 && parent_die->has_specification == 0)
18411 {
18412 if (pdi.name == NULL)
18413 complaint (&symfile_complaints,
18414 _("malformed enumerator DIE ignored"));
18415 else if (building_psymtab)
18416 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18417 VAR_DOMAIN, LOC_CONST,
18418 cu->language == language_cplus
18419 ? &objfile->global_psymbols
18420 : &objfile->static_psymbols,
18421 0, cu->language, objfile);
18422
18423 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18424 continue;
18425 }
18426
18427 struct partial_die_info *part_die
18428 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18429
18430 /* We'll save this DIE so link it in. */
18431 part_die->die_parent = parent_die;
18432 part_die->die_sibling = NULL;
18433 part_die->die_child = NULL;
18434
18435 if (last_die && last_die == parent_die)
18436 last_die->die_child = part_die;
18437 else if (last_die)
18438 last_die->die_sibling = part_die;
18439
18440 last_die = part_die;
18441
18442 if (first_die == NULL)
18443 first_die = part_die;
18444
18445 /* Maybe add the DIE to the hash table. Not all DIEs that we
18446 find interesting need to be in the hash table, because we
18447 also have the parent/sibling/child chains; only those that we
18448 might refer to by offset later during partial symbol reading.
18449
18450 For now this means things that might have be the target of a
18451 DW_AT_specification, DW_AT_abstract_origin, or
18452 DW_AT_extension. DW_AT_extension will refer only to
18453 namespaces; DW_AT_abstract_origin refers to functions (and
18454 many things under the function DIE, but we do not recurse
18455 into function DIEs during partial symbol reading) and
18456 possibly variables as well; DW_AT_specification refers to
18457 declarations. Declarations ought to have the DW_AT_declaration
18458 flag. It happens that GCC forgets to put it in sometimes, but
18459 only for functions, not for types.
18460
18461 Adding more things than necessary to the hash table is harmless
18462 except for the performance cost. Adding too few will result in
18463 wasted time in find_partial_die, when we reread the compilation
18464 unit with load_all_dies set. */
18465
18466 if (load_all
18467 || abbrev->tag == DW_TAG_constant
18468 || abbrev->tag == DW_TAG_subprogram
18469 || abbrev->tag == DW_TAG_variable
18470 || abbrev->tag == DW_TAG_namespace
18471 || part_die->is_declaration)
18472 {
18473 void **slot;
18474
18475 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18476 to_underlying (part_die->sect_off),
18477 INSERT);
18478 *slot = part_die;
18479 }
18480
18481 /* For some DIEs we want to follow their children (if any). For C
18482 we have no reason to follow the children of structures; for other
18483 languages we have to, so that we can get at method physnames
18484 to infer fully qualified class names, for DW_AT_specification,
18485 and for C++ template arguments. For C++, we also look one level
18486 inside functions to find template arguments (if the name of the
18487 function does not already contain the template arguments).
18488
18489 For Ada, we need to scan the children of subprograms and lexical
18490 blocks as well because Ada allows the definition of nested
18491 entities that could be interesting for the debugger, such as
18492 nested subprograms for instance. */
18493 if (last_die->has_children
18494 && (load_all
18495 || last_die->tag == DW_TAG_namespace
18496 || last_die->tag == DW_TAG_module
18497 || last_die->tag == DW_TAG_enumeration_type
18498 || (cu->language == language_cplus
18499 && last_die->tag == DW_TAG_subprogram
18500 && (last_die->name == NULL
18501 || strchr (last_die->name, '<') == NULL))
18502 || (cu->language != language_c
18503 && (last_die->tag == DW_TAG_class_type
18504 || last_die->tag == DW_TAG_interface_type
18505 || last_die->tag == DW_TAG_structure_type
18506 || last_die->tag == DW_TAG_union_type))
18507 || (cu->language == language_ada
18508 && (last_die->tag == DW_TAG_subprogram
18509 || last_die->tag == DW_TAG_lexical_block))))
18510 {
18511 nesting_level++;
18512 parent_die = last_die;
18513 continue;
18514 }
18515
18516 /* Otherwise we skip to the next sibling, if any. */
18517 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18518
18519 /* Back to the top, do it again. */
18520 }
18521 }
18522
18523 partial_die_info::partial_die_info (sect_offset sect_off_,
18524 struct abbrev_info *abbrev)
18525 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18526 {
18527 }
18528
18529 /* Read a minimal amount of information into the minimal die structure.
18530 INFO_PTR should point just after the initial uleb128 of a DIE. */
18531
18532 static const gdb_byte *
18533 read_partial_die (const struct die_reader_specs *reader,
18534 struct partial_die_info *part_die,
18535 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 for (i = 0; i < abbrev.num_attrs; ++i)
18550 {
18551 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18552
18553 /* Store the data if it is of an attribute we want to keep in a
18554 partial symbol table. */
18555 switch (attr.name)
18556 {
18557 case DW_AT_name:
18558 switch (part_die->tag)
18559 {
18560 case DW_TAG_compile_unit:
18561 case DW_TAG_partial_unit:
18562 case DW_TAG_type_unit:
18563 /* Compilation units have a DW_AT_name that is a filename, not
18564 a source language identifier. */
18565 case DW_TAG_enumeration_type:
18566 case DW_TAG_enumerator:
18567 /* These tags always have simple identifiers already; no need
18568 to canonicalize them. */
18569 part_die->name = DW_STRING (&attr);
18570 break;
18571 default:
18572 part_die->name
18573 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18574 &objfile->per_bfd->storage_obstack);
18575 break;
18576 }
18577 break;
18578 case DW_AT_linkage_name:
18579 case DW_AT_MIPS_linkage_name:
18580 /* Note that both forms of linkage name might appear. We
18581 assume they will be the same, and we only store the last
18582 one we see. */
18583 if (cu->language == language_ada)
18584 part_die->name = DW_STRING (&attr);
18585 part_die->linkage_name = DW_STRING (&attr);
18586 break;
18587 case DW_AT_low_pc:
18588 has_low_pc_attr = 1;
18589 part_die->lowpc = attr_value_as_address (&attr);
18590 break;
18591 case DW_AT_high_pc:
18592 has_high_pc_attr = 1;
18593 part_die->highpc = attr_value_as_address (&attr);
18594 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18595 high_pc_relative = 1;
18596 break;
18597 case DW_AT_location:
18598 /* Support the .debug_loc offsets. */
18599 if (attr_form_is_block (&attr))
18600 {
18601 part_die->d.locdesc = DW_BLOCK (&attr);
18602 }
18603 else if (attr_form_is_section_offset (&attr))
18604 {
18605 dwarf2_complex_location_expr_complaint ();
18606 }
18607 else
18608 {
18609 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18610 "partial symbol information");
18611 }
18612 break;
18613 case DW_AT_external:
18614 part_die->is_external = DW_UNSND (&attr);
18615 break;
18616 case DW_AT_declaration:
18617 part_die->is_declaration = DW_UNSND (&attr);
18618 break;
18619 case DW_AT_type:
18620 part_die->has_type = 1;
18621 break;
18622 case DW_AT_abstract_origin:
18623 case DW_AT_specification:
18624 case DW_AT_extension:
18625 part_die->has_specification = 1;
18626 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
18627 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18628 || cu->per_cu->is_dwz);
18629 break;
18630 case DW_AT_sibling:
18631 /* Ignore absolute siblings, they might point outside of
18632 the current compile unit. */
18633 if (attr.form == DW_FORM_ref_addr)
18634 complaint (&symfile_complaints,
18635 _("ignoring absolute DW_AT_sibling"));
18636 else
18637 {
18638 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18639 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18640
18641 if (sibling_ptr < info_ptr)
18642 complaint (&symfile_complaints,
18643 _("DW_AT_sibling points backwards"));
18644 else if (sibling_ptr > reader->buffer_end)
18645 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18646 else
18647 part_die->sibling = sibling_ptr;
18648 }
18649 break;
18650 case DW_AT_byte_size:
18651 part_die->has_byte_size = 1;
18652 break;
18653 case DW_AT_const_value:
18654 part_die->has_const_value = 1;
18655 break;
18656 case DW_AT_calling_convention:
18657 /* DWARF doesn't provide a way to identify a program's source-level
18658 entry point. DW_AT_calling_convention attributes are only meant
18659 to describe functions' calling conventions.
18660
18661 However, because it's a necessary piece of information in
18662 Fortran, and before DWARF 4 DW_CC_program was the only
18663 piece of debugging information whose definition refers to
18664 a 'main program' at all, several compilers marked Fortran
18665 main programs with DW_CC_program --- even when those
18666 functions use the standard calling conventions.
18667
18668 Although DWARF now specifies a way to provide this
18669 information, we support this practice for backward
18670 compatibility. */
18671 if (DW_UNSND (&attr) == DW_CC_program
18672 && cu->language == language_fortran)
18673 part_die->main_subprogram = 1;
18674 break;
18675 case DW_AT_inline:
18676 if (DW_UNSND (&attr) == DW_INL_inlined
18677 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18678 part_die->may_be_inlined = 1;
18679 break;
18680
18681 case DW_AT_import:
18682 if (part_die->tag == DW_TAG_imported_unit)
18683 {
18684 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
18685 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18686 || cu->per_cu->is_dwz);
18687 }
18688 break;
18689
18690 case DW_AT_main_subprogram:
18691 part_die->main_subprogram = DW_UNSND (&attr);
18692 break;
18693
18694 default:
18695 break;
18696 }
18697 }
18698
18699 if (high_pc_relative)
18700 part_die->highpc += part_die->lowpc;
18701
18702 if (has_low_pc_attr && has_high_pc_attr)
18703 {
18704 /* When using the GNU linker, .gnu.linkonce. sections are used to
18705 eliminate duplicate copies of functions and vtables and such.
18706 The linker will arbitrarily choose one and discard the others.
18707 The AT_*_pc values for such functions refer to local labels in
18708 these sections. If the section from that file was discarded, the
18709 labels are not in the output, so the relocs get a value of 0.
18710 If this is a discarded function, mark the pc bounds as invalid,
18711 so that GDB will ignore it. */
18712 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18713 {
18714 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18715
18716 complaint (&symfile_complaints,
18717 _("DW_AT_low_pc %s is zero "
18718 "for DIE at %s [in module %s]"),
18719 paddress (gdbarch, part_die->lowpc),
18720 sect_offset_str (part_die->sect_off),
18721 objfile_name (objfile));
18722 }
18723 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18724 else if (part_die->lowpc >= part_die->highpc)
18725 {
18726 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18727
18728 complaint (&symfile_complaints,
18729 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18730 "for DIE at %s [in module %s]"),
18731 paddress (gdbarch, part_die->lowpc),
18732 paddress (gdbarch, part_die->highpc),
18733 sect_offset_str (part_die->sect_off),
18734 objfile_name (objfile));
18735 }
18736 else
18737 part_die->has_pc_info = 1;
18738 }
18739
18740 return info_ptr;
18741 }
18742
18743 /* Find a cached partial DIE at OFFSET in CU. */
18744
18745 struct partial_die_info *
18746 dwarf2_cu::find_partial_die (sect_offset sect_off)
18747 {
18748 struct partial_die_info *lookup_die = NULL;
18749 struct partial_die_info part_die (sect_off);
18750
18751 lookup_die = ((struct partial_die_info *)
18752 htab_find_with_hash (partial_dies, &part_die,
18753 to_underlying (sect_off)));
18754
18755 return lookup_die;
18756 }
18757
18758 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18759 except in the case of .debug_types DIEs which do not reference
18760 outside their CU (they do however referencing other types via
18761 DW_FORM_ref_sig8). */
18762
18763 static struct partial_die_info *
18764 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18765 {
18766 struct dwarf2_per_objfile *dwarf2_per_objfile
18767 = cu->per_cu->dwarf2_per_objfile;
18768 struct objfile *objfile = dwarf2_per_objfile->objfile;
18769 struct dwarf2_per_cu_data *per_cu = NULL;
18770 struct partial_die_info *pd = NULL;
18771
18772 if (offset_in_dwz == cu->per_cu->is_dwz
18773 && offset_in_cu_p (&cu->header, sect_off))
18774 {
18775 pd = cu->find_partial_die (sect_off);
18776 if (pd != NULL)
18777 return pd;
18778 /* We missed recording what we needed.
18779 Load all dies and try again. */
18780 per_cu = cu->per_cu;
18781 }
18782 else
18783 {
18784 /* TUs don't reference other CUs/TUs (except via type signatures). */
18785 if (cu->per_cu->is_debug_types)
18786 {
18787 error (_("Dwarf Error: Type Unit at offset %s contains"
18788 " external reference to offset %s [in module %s].\n"),
18789 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18790 bfd_get_filename (objfile->obfd));
18791 }
18792 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18793 dwarf2_per_objfile);
18794
18795 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18796 load_partial_comp_unit (per_cu);
18797
18798 per_cu->cu->last_used = 0;
18799 pd = per_cu->cu->find_partial_die (sect_off);
18800 }
18801
18802 /* If we didn't find it, and not all dies have been loaded,
18803 load them all and try again. */
18804
18805 if (pd == NULL && per_cu->load_all_dies == 0)
18806 {
18807 per_cu->load_all_dies = 1;
18808
18809 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18810 THIS_CU->cu may already be in use. So we can't just free it and
18811 replace its DIEs with the ones we read in. Instead, we leave those
18812 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18813 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18814 set. */
18815 load_partial_comp_unit (per_cu);
18816
18817 pd = per_cu->cu->find_partial_die (sect_off);
18818 }
18819
18820 if (pd == NULL)
18821 internal_error (__FILE__, __LINE__,
18822 _("could not find partial DIE %s "
18823 "in cache [from module %s]\n"),
18824 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18825 return pd;
18826 }
18827
18828 /* See if we can figure out if the class lives in a namespace. We do
18829 this by looking for a member function; its demangled name will
18830 contain namespace info, if there is any. */
18831
18832 static void
18833 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18834 struct dwarf2_cu *cu)
18835 {
18836 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18837 what template types look like, because the demangler
18838 frequently doesn't give the same name as the debug info. We
18839 could fix this by only using the demangled name to get the
18840 prefix (but see comment in read_structure_type). */
18841
18842 struct partial_die_info *real_pdi;
18843 struct partial_die_info *child_pdi;
18844
18845 /* If this DIE (this DIE's specification, if any) has a parent, then
18846 we should not do this. We'll prepend the parent's fully qualified
18847 name when we create the partial symbol. */
18848
18849 real_pdi = struct_pdi;
18850 while (real_pdi->has_specification)
18851 real_pdi = find_partial_die (real_pdi->spec_offset,
18852 real_pdi->spec_is_dwz, cu);
18853
18854 if (real_pdi->die_parent != NULL)
18855 return;
18856
18857 for (child_pdi = struct_pdi->die_child;
18858 child_pdi != NULL;
18859 child_pdi = child_pdi->die_sibling)
18860 {
18861 if (child_pdi->tag == DW_TAG_subprogram
18862 && child_pdi->linkage_name != NULL)
18863 {
18864 char *actual_class_name
18865 = language_class_name_from_physname (cu->language_defn,
18866 child_pdi->linkage_name);
18867 if (actual_class_name != NULL)
18868 {
18869 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18870 struct_pdi->name
18871 = ((const char *)
18872 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18873 actual_class_name,
18874 strlen (actual_class_name)));
18875 xfree (actual_class_name);
18876 }
18877 break;
18878 }
18879 }
18880 }
18881
18882 /* Adjust PART_DIE before generating a symbol for it. This function
18883 may set the is_external flag or change the DIE's name. */
18884
18885 static void
18886 fixup_partial_die (struct partial_die_info *part_die,
18887 struct dwarf2_cu *cu)
18888 {
18889 /* Once we've fixed up a die, there's no point in doing so again.
18890 This also avoids a memory leak if we were to call
18891 guess_partial_die_structure_name multiple times. */
18892 if (part_die->fixup_called)
18893 return;
18894
18895 /* If we found a reference attribute and the DIE has no name, try
18896 to find a name in the referred to DIE. */
18897
18898 if (part_die->name == NULL && part_die->has_specification)
18899 {
18900 struct partial_die_info *spec_die;
18901
18902 spec_die = find_partial_die (part_die->spec_offset,
18903 part_die->spec_is_dwz, cu);
18904
18905 fixup_partial_die (spec_die, cu);
18906
18907 if (spec_die->name)
18908 {
18909 part_die->name = spec_die->name;
18910
18911 /* Copy DW_AT_external attribute if it is set. */
18912 if (spec_die->is_external)
18913 part_die->is_external = spec_die->is_external;
18914 }
18915 }
18916
18917 /* Set default names for some unnamed DIEs. */
18918
18919 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
18920 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
18921
18922 /* If there is no parent die to provide a namespace, and there are
18923 children, see if we can determine the namespace from their linkage
18924 name. */
18925 if (cu->language == language_cplus
18926 && !VEC_empty (dwarf2_section_info_def,
18927 cu->per_cu->dwarf2_per_objfile->types)
18928 && part_die->die_parent == NULL
18929 && part_die->has_children
18930 && (part_die->tag == DW_TAG_class_type
18931 || part_die->tag == DW_TAG_structure_type
18932 || part_die->tag == DW_TAG_union_type))
18933 guess_partial_die_structure_name (part_die, cu);
18934
18935 /* GCC might emit a nameless struct or union that has a linkage
18936 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18937 if (part_die->name == NULL
18938 && (part_die->tag == DW_TAG_class_type
18939 || part_die->tag == DW_TAG_interface_type
18940 || part_die->tag == DW_TAG_structure_type
18941 || part_die->tag == DW_TAG_union_type)
18942 && part_die->linkage_name != NULL)
18943 {
18944 char *demangled;
18945
18946 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
18947 if (demangled)
18948 {
18949 const char *base;
18950
18951 /* Strip any leading namespaces/classes, keep only the base name.
18952 DW_AT_name for named DIEs does not contain the prefixes. */
18953 base = strrchr (demangled, ':');
18954 if (base && base > demangled && base[-1] == ':')
18955 base++;
18956 else
18957 base = demangled;
18958
18959 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18960 part_die->name
18961 = ((const char *)
18962 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18963 base, strlen (base)));
18964 xfree (demangled);
18965 }
18966 }
18967
18968 part_die->fixup_called = 1;
18969 }
18970
18971 /* Read an attribute value described by an attribute form. */
18972
18973 static const gdb_byte *
18974 read_attribute_value (const struct die_reader_specs *reader,
18975 struct attribute *attr, unsigned form,
18976 LONGEST implicit_const, const gdb_byte *info_ptr)
18977 {
18978 struct dwarf2_cu *cu = reader->cu;
18979 struct dwarf2_per_objfile *dwarf2_per_objfile
18980 = cu->per_cu->dwarf2_per_objfile;
18981 struct objfile *objfile = dwarf2_per_objfile->objfile;
18982 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18983 bfd *abfd = reader->abfd;
18984 struct comp_unit_head *cu_header = &cu->header;
18985 unsigned int bytes_read;
18986 struct dwarf_block *blk;
18987
18988 attr->form = (enum dwarf_form) form;
18989 switch (form)
18990 {
18991 case DW_FORM_ref_addr:
18992 if (cu->header.version == 2)
18993 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18994 else
18995 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18996 &cu->header, &bytes_read);
18997 info_ptr += bytes_read;
18998 break;
18999 case DW_FORM_GNU_ref_alt:
19000 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19001 info_ptr += bytes_read;
19002 break;
19003 case DW_FORM_addr:
19004 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19005 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19006 info_ptr += bytes_read;
19007 break;
19008 case DW_FORM_block2:
19009 blk = dwarf_alloc_block (cu);
19010 blk->size = read_2_bytes (abfd, info_ptr);
19011 info_ptr += 2;
19012 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19013 info_ptr += blk->size;
19014 DW_BLOCK (attr) = blk;
19015 break;
19016 case DW_FORM_block4:
19017 blk = dwarf_alloc_block (cu);
19018 blk->size = read_4_bytes (abfd, info_ptr);
19019 info_ptr += 4;
19020 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19021 info_ptr += blk->size;
19022 DW_BLOCK (attr) = blk;
19023 break;
19024 case DW_FORM_data2:
19025 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19026 info_ptr += 2;
19027 break;
19028 case DW_FORM_data4:
19029 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19030 info_ptr += 4;
19031 break;
19032 case DW_FORM_data8:
19033 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19034 info_ptr += 8;
19035 break;
19036 case DW_FORM_data16:
19037 blk = dwarf_alloc_block (cu);
19038 blk->size = 16;
19039 blk->data = read_n_bytes (abfd, info_ptr, 16);
19040 info_ptr += 16;
19041 DW_BLOCK (attr) = blk;
19042 break;
19043 case DW_FORM_sec_offset:
19044 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19045 info_ptr += bytes_read;
19046 break;
19047 case DW_FORM_string:
19048 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19049 DW_STRING_IS_CANONICAL (attr) = 0;
19050 info_ptr += bytes_read;
19051 break;
19052 case DW_FORM_strp:
19053 if (!cu->per_cu->is_dwz)
19054 {
19055 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19056 abfd, info_ptr, cu_header,
19057 &bytes_read);
19058 DW_STRING_IS_CANONICAL (attr) = 0;
19059 info_ptr += bytes_read;
19060 break;
19061 }
19062 /* FALLTHROUGH */
19063 case DW_FORM_line_strp:
19064 if (!cu->per_cu->is_dwz)
19065 {
19066 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19067 abfd, info_ptr,
19068 cu_header, &bytes_read);
19069 DW_STRING_IS_CANONICAL (attr) = 0;
19070 info_ptr += bytes_read;
19071 break;
19072 }
19073 /* FALLTHROUGH */
19074 case DW_FORM_GNU_strp_alt:
19075 {
19076 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19077 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19078 &bytes_read);
19079
19080 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19081 dwz, str_offset);
19082 DW_STRING_IS_CANONICAL (attr) = 0;
19083 info_ptr += bytes_read;
19084 }
19085 break;
19086 case DW_FORM_exprloc:
19087 case DW_FORM_block:
19088 blk = dwarf_alloc_block (cu);
19089 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19090 info_ptr += bytes_read;
19091 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19092 info_ptr += blk->size;
19093 DW_BLOCK (attr) = blk;
19094 break;
19095 case DW_FORM_block1:
19096 blk = dwarf_alloc_block (cu);
19097 blk->size = read_1_byte (abfd, info_ptr);
19098 info_ptr += 1;
19099 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19100 info_ptr += blk->size;
19101 DW_BLOCK (attr) = blk;
19102 break;
19103 case DW_FORM_data1:
19104 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19105 info_ptr += 1;
19106 break;
19107 case DW_FORM_flag:
19108 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19109 info_ptr += 1;
19110 break;
19111 case DW_FORM_flag_present:
19112 DW_UNSND (attr) = 1;
19113 break;
19114 case DW_FORM_sdata:
19115 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19116 info_ptr += bytes_read;
19117 break;
19118 case DW_FORM_udata:
19119 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19120 info_ptr += bytes_read;
19121 break;
19122 case DW_FORM_ref1:
19123 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19124 + read_1_byte (abfd, info_ptr));
19125 info_ptr += 1;
19126 break;
19127 case DW_FORM_ref2:
19128 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19129 + read_2_bytes (abfd, info_ptr));
19130 info_ptr += 2;
19131 break;
19132 case DW_FORM_ref4:
19133 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19134 + read_4_bytes (abfd, info_ptr));
19135 info_ptr += 4;
19136 break;
19137 case DW_FORM_ref8:
19138 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19139 + read_8_bytes (abfd, info_ptr));
19140 info_ptr += 8;
19141 break;
19142 case DW_FORM_ref_sig8:
19143 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19144 info_ptr += 8;
19145 break;
19146 case DW_FORM_ref_udata:
19147 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19148 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19149 info_ptr += bytes_read;
19150 break;
19151 case DW_FORM_indirect:
19152 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19153 info_ptr += bytes_read;
19154 if (form == DW_FORM_implicit_const)
19155 {
19156 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19157 info_ptr += bytes_read;
19158 }
19159 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19160 info_ptr);
19161 break;
19162 case DW_FORM_implicit_const:
19163 DW_SND (attr) = implicit_const;
19164 break;
19165 case DW_FORM_GNU_addr_index:
19166 if (reader->dwo_file == NULL)
19167 {
19168 /* For now flag a hard error.
19169 Later we can turn this into a complaint. */
19170 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19171 dwarf_form_name (form),
19172 bfd_get_filename (abfd));
19173 }
19174 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19175 info_ptr += bytes_read;
19176 break;
19177 case DW_FORM_GNU_str_index:
19178 if (reader->dwo_file == NULL)
19179 {
19180 /* For now flag a hard error.
19181 Later we can turn this into a complaint if warranted. */
19182 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19183 dwarf_form_name (form),
19184 bfd_get_filename (abfd));
19185 }
19186 {
19187 ULONGEST str_index =
19188 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19189
19190 DW_STRING (attr) = read_str_index (reader, str_index);
19191 DW_STRING_IS_CANONICAL (attr) = 0;
19192 info_ptr += bytes_read;
19193 }
19194 break;
19195 default:
19196 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19197 dwarf_form_name (form),
19198 bfd_get_filename (abfd));
19199 }
19200
19201 /* Super hack. */
19202 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19203 attr->form = DW_FORM_GNU_ref_alt;
19204
19205 /* We have seen instances where the compiler tried to emit a byte
19206 size attribute of -1 which ended up being encoded as an unsigned
19207 0xffffffff. Although 0xffffffff is technically a valid size value,
19208 an object of this size seems pretty unlikely so we can relatively
19209 safely treat these cases as if the size attribute was invalid and
19210 treat them as zero by default. */
19211 if (attr->name == DW_AT_byte_size
19212 && form == DW_FORM_data4
19213 && DW_UNSND (attr) >= 0xffffffff)
19214 {
19215 complaint
19216 (&symfile_complaints,
19217 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19218 hex_string (DW_UNSND (attr)));
19219 DW_UNSND (attr) = 0;
19220 }
19221
19222 return info_ptr;
19223 }
19224
19225 /* Read an attribute described by an abbreviated attribute. */
19226
19227 static const gdb_byte *
19228 read_attribute (const struct die_reader_specs *reader,
19229 struct attribute *attr, struct attr_abbrev *abbrev,
19230 const gdb_byte *info_ptr)
19231 {
19232 attr->name = abbrev->name;
19233 return read_attribute_value (reader, attr, abbrev->form,
19234 abbrev->implicit_const, info_ptr);
19235 }
19236
19237 /* Read dwarf information from a buffer. */
19238
19239 static unsigned int
19240 read_1_byte (bfd *abfd, const gdb_byte *buf)
19241 {
19242 return bfd_get_8 (abfd, buf);
19243 }
19244
19245 static int
19246 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19247 {
19248 return bfd_get_signed_8 (abfd, buf);
19249 }
19250
19251 static unsigned int
19252 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19253 {
19254 return bfd_get_16 (abfd, buf);
19255 }
19256
19257 static int
19258 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19259 {
19260 return bfd_get_signed_16 (abfd, buf);
19261 }
19262
19263 static unsigned int
19264 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19265 {
19266 return bfd_get_32 (abfd, buf);
19267 }
19268
19269 static int
19270 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19271 {
19272 return bfd_get_signed_32 (abfd, buf);
19273 }
19274
19275 static ULONGEST
19276 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19277 {
19278 return bfd_get_64 (abfd, buf);
19279 }
19280
19281 static CORE_ADDR
19282 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19283 unsigned int *bytes_read)
19284 {
19285 struct comp_unit_head *cu_header = &cu->header;
19286 CORE_ADDR retval = 0;
19287
19288 if (cu_header->signed_addr_p)
19289 {
19290 switch (cu_header->addr_size)
19291 {
19292 case 2:
19293 retval = bfd_get_signed_16 (abfd, buf);
19294 break;
19295 case 4:
19296 retval = bfd_get_signed_32 (abfd, buf);
19297 break;
19298 case 8:
19299 retval = bfd_get_signed_64 (abfd, buf);
19300 break;
19301 default:
19302 internal_error (__FILE__, __LINE__,
19303 _("read_address: bad switch, signed [in module %s]"),
19304 bfd_get_filename (abfd));
19305 }
19306 }
19307 else
19308 {
19309 switch (cu_header->addr_size)
19310 {
19311 case 2:
19312 retval = bfd_get_16 (abfd, buf);
19313 break;
19314 case 4:
19315 retval = bfd_get_32 (abfd, buf);
19316 break;
19317 case 8:
19318 retval = bfd_get_64 (abfd, buf);
19319 break;
19320 default:
19321 internal_error (__FILE__, __LINE__,
19322 _("read_address: bad switch, "
19323 "unsigned [in module %s]"),
19324 bfd_get_filename (abfd));
19325 }
19326 }
19327
19328 *bytes_read = cu_header->addr_size;
19329 return retval;
19330 }
19331
19332 /* Read the initial length from a section. The (draft) DWARF 3
19333 specification allows the initial length to take up either 4 bytes
19334 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19335 bytes describe the length and all offsets will be 8 bytes in length
19336 instead of 4.
19337
19338 An older, non-standard 64-bit format is also handled by this
19339 function. The older format in question stores the initial length
19340 as an 8-byte quantity without an escape value. Lengths greater
19341 than 2^32 aren't very common which means that the initial 4 bytes
19342 is almost always zero. Since a length value of zero doesn't make
19343 sense for the 32-bit format, this initial zero can be considered to
19344 be an escape value which indicates the presence of the older 64-bit
19345 format. As written, the code can't detect (old format) lengths
19346 greater than 4GB. If it becomes necessary to handle lengths
19347 somewhat larger than 4GB, we could allow other small values (such
19348 as the non-sensical values of 1, 2, and 3) to also be used as
19349 escape values indicating the presence of the old format.
19350
19351 The value returned via bytes_read should be used to increment the
19352 relevant pointer after calling read_initial_length().
19353
19354 [ Note: read_initial_length() and read_offset() are based on the
19355 document entitled "DWARF Debugging Information Format", revision
19356 3, draft 8, dated November 19, 2001. This document was obtained
19357 from:
19358
19359 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19360
19361 This document is only a draft and is subject to change. (So beware.)
19362
19363 Details regarding the older, non-standard 64-bit format were
19364 determined empirically by examining 64-bit ELF files produced by
19365 the SGI toolchain on an IRIX 6.5 machine.
19366
19367 - Kevin, July 16, 2002
19368 ] */
19369
19370 static LONGEST
19371 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19372 {
19373 LONGEST length = bfd_get_32 (abfd, buf);
19374
19375 if (length == 0xffffffff)
19376 {
19377 length = bfd_get_64 (abfd, buf + 4);
19378 *bytes_read = 12;
19379 }
19380 else if (length == 0)
19381 {
19382 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19383 length = bfd_get_64 (abfd, buf);
19384 *bytes_read = 8;
19385 }
19386 else
19387 {
19388 *bytes_read = 4;
19389 }
19390
19391 return length;
19392 }
19393
19394 /* Cover function for read_initial_length.
19395 Returns the length of the object at BUF, and stores the size of the
19396 initial length in *BYTES_READ and stores the size that offsets will be in
19397 *OFFSET_SIZE.
19398 If the initial length size is not equivalent to that specified in
19399 CU_HEADER then issue a complaint.
19400 This is useful when reading non-comp-unit headers. */
19401
19402 static LONGEST
19403 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19404 const struct comp_unit_head *cu_header,
19405 unsigned int *bytes_read,
19406 unsigned int *offset_size)
19407 {
19408 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19409
19410 gdb_assert (cu_header->initial_length_size == 4
19411 || cu_header->initial_length_size == 8
19412 || cu_header->initial_length_size == 12);
19413
19414 if (cu_header->initial_length_size != *bytes_read)
19415 complaint (&symfile_complaints,
19416 _("intermixed 32-bit and 64-bit DWARF sections"));
19417
19418 *offset_size = (*bytes_read == 4) ? 4 : 8;
19419 return length;
19420 }
19421
19422 /* Read an offset from the data stream. The size of the offset is
19423 given by cu_header->offset_size. */
19424
19425 static LONGEST
19426 read_offset (bfd *abfd, const gdb_byte *buf,
19427 const struct comp_unit_head *cu_header,
19428 unsigned int *bytes_read)
19429 {
19430 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19431
19432 *bytes_read = cu_header->offset_size;
19433 return offset;
19434 }
19435
19436 /* Read an offset from the data stream. */
19437
19438 static LONGEST
19439 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19440 {
19441 LONGEST retval = 0;
19442
19443 switch (offset_size)
19444 {
19445 case 4:
19446 retval = bfd_get_32 (abfd, buf);
19447 break;
19448 case 8:
19449 retval = bfd_get_64 (abfd, buf);
19450 break;
19451 default:
19452 internal_error (__FILE__, __LINE__,
19453 _("read_offset_1: bad switch [in module %s]"),
19454 bfd_get_filename (abfd));
19455 }
19456
19457 return retval;
19458 }
19459
19460 static const gdb_byte *
19461 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19462 {
19463 /* If the size of a host char is 8 bits, we can return a pointer
19464 to the buffer, otherwise we have to copy the data to a buffer
19465 allocated on the temporary obstack. */
19466 gdb_assert (HOST_CHAR_BIT == 8);
19467 return buf;
19468 }
19469
19470 static const char *
19471 read_direct_string (bfd *abfd, const gdb_byte *buf,
19472 unsigned int *bytes_read_ptr)
19473 {
19474 /* If the size of a host char is 8 bits, we can return a pointer
19475 to the string, otherwise we have to copy the string to a buffer
19476 allocated on the temporary obstack. */
19477 gdb_assert (HOST_CHAR_BIT == 8);
19478 if (*buf == '\0')
19479 {
19480 *bytes_read_ptr = 1;
19481 return NULL;
19482 }
19483 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19484 return (const char *) buf;
19485 }
19486
19487 /* Return pointer to string at section SECT offset STR_OFFSET with error
19488 reporting strings FORM_NAME and SECT_NAME. */
19489
19490 static const char *
19491 read_indirect_string_at_offset_from (struct objfile *objfile,
19492 bfd *abfd, LONGEST str_offset,
19493 struct dwarf2_section_info *sect,
19494 const char *form_name,
19495 const char *sect_name)
19496 {
19497 dwarf2_read_section (objfile, sect);
19498 if (sect->buffer == NULL)
19499 error (_("%s used without %s section [in module %s]"),
19500 form_name, sect_name, bfd_get_filename (abfd));
19501 if (str_offset >= sect->size)
19502 error (_("%s pointing outside of %s section [in module %s]"),
19503 form_name, sect_name, bfd_get_filename (abfd));
19504 gdb_assert (HOST_CHAR_BIT == 8);
19505 if (sect->buffer[str_offset] == '\0')
19506 return NULL;
19507 return (const char *) (sect->buffer + str_offset);
19508 }
19509
19510 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19511
19512 static const char *
19513 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19514 bfd *abfd, LONGEST str_offset)
19515 {
19516 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19517 abfd, str_offset,
19518 &dwarf2_per_objfile->str,
19519 "DW_FORM_strp", ".debug_str");
19520 }
19521
19522 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19523
19524 static const char *
19525 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19526 bfd *abfd, LONGEST str_offset)
19527 {
19528 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19529 abfd, str_offset,
19530 &dwarf2_per_objfile->line_str,
19531 "DW_FORM_line_strp",
19532 ".debug_line_str");
19533 }
19534
19535 /* Read a string at offset STR_OFFSET in the .debug_str section from
19536 the .dwz file DWZ. Throw an error if the offset is too large. If
19537 the string consists of a single NUL byte, return NULL; otherwise
19538 return a pointer to the string. */
19539
19540 static const char *
19541 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19542 LONGEST str_offset)
19543 {
19544 dwarf2_read_section (objfile, &dwz->str);
19545
19546 if (dwz->str.buffer == NULL)
19547 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19548 "section [in module %s]"),
19549 bfd_get_filename (dwz->dwz_bfd));
19550 if (str_offset >= dwz->str.size)
19551 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19552 ".debug_str section [in module %s]"),
19553 bfd_get_filename (dwz->dwz_bfd));
19554 gdb_assert (HOST_CHAR_BIT == 8);
19555 if (dwz->str.buffer[str_offset] == '\0')
19556 return NULL;
19557 return (const char *) (dwz->str.buffer + str_offset);
19558 }
19559
19560 /* Return pointer to string at .debug_str offset as read from BUF.
19561 BUF is assumed to be in a compilation unit described by CU_HEADER.
19562 Return *BYTES_READ_PTR count of bytes read from BUF. */
19563
19564 static const char *
19565 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19566 const gdb_byte *buf,
19567 const struct comp_unit_head *cu_header,
19568 unsigned int *bytes_read_ptr)
19569 {
19570 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19571
19572 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19573 }
19574
19575 /* Return pointer to string at .debug_line_str offset as read from BUF.
19576 BUF is assumed to be in a compilation unit described by CU_HEADER.
19577 Return *BYTES_READ_PTR count of bytes read from BUF. */
19578
19579 static const char *
19580 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19581 bfd *abfd, const gdb_byte *buf,
19582 const struct comp_unit_head *cu_header,
19583 unsigned int *bytes_read_ptr)
19584 {
19585 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19586
19587 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19588 str_offset);
19589 }
19590
19591 ULONGEST
19592 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19593 unsigned int *bytes_read_ptr)
19594 {
19595 ULONGEST result;
19596 unsigned int num_read;
19597 int shift;
19598 unsigned char byte;
19599
19600 result = 0;
19601 shift = 0;
19602 num_read = 0;
19603 while (1)
19604 {
19605 byte = bfd_get_8 (abfd, buf);
19606 buf++;
19607 num_read++;
19608 result |= ((ULONGEST) (byte & 127) << shift);
19609 if ((byte & 128) == 0)
19610 {
19611 break;
19612 }
19613 shift += 7;
19614 }
19615 *bytes_read_ptr = num_read;
19616 return result;
19617 }
19618
19619 static LONGEST
19620 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19621 unsigned int *bytes_read_ptr)
19622 {
19623 LONGEST result;
19624 int shift, num_read;
19625 unsigned char byte;
19626
19627 result = 0;
19628 shift = 0;
19629 num_read = 0;
19630 while (1)
19631 {
19632 byte = bfd_get_8 (abfd, buf);
19633 buf++;
19634 num_read++;
19635 result |= ((LONGEST) (byte & 127) << shift);
19636 shift += 7;
19637 if ((byte & 128) == 0)
19638 {
19639 break;
19640 }
19641 }
19642 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19643 result |= -(((LONGEST) 1) << shift);
19644 *bytes_read_ptr = num_read;
19645 return result;
19646 }
19647
19648 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19649 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19650 ADDR_SIZE is the size of addresses from the CU header. */
19651
19652 static CORE_ADDR
19653 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19654 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19655 {
19656 struct objfile *objfile = dwarf2_per_objfile->objfile;
19657 bfd *abfd = objfile->obfd;
19658 const gdb_byte *info_ptr;
19659
19660 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19661 if (dwarf2_per_objfile->addr.buffer == NULL)
19662 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19663 objfile_name (objfile));
19664 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19665 error (_("DW_FORM_addr_index pointing outside of "
19666 ".debug_addr section [in module %s]"),
19667 objfile_name (objfile));
19668 info_ptr = (dwarf2_per_objfile->addr.buffer
19669 + addr_base + addr_index * addr_size);
19670 if (addr_size == 4)
19671 return bfd_get_32 (abfd, info_ptr);
19672 else
19673 return bfd_get_64 (abfd, info_ptr);
19674 }
19675
19676 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19677
19678 static CORE_ADDR
19679 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19680 {
19681 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19682 cu->addr_base, cu->header.addr_size);
19683 }
19684
19685 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19686
19687 static CORE_ADDR
19688 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19689 unsigned int *bytes_read)
19690 {
19691 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19692 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19693
19694 return read_addr_index (cu, addr_index);
19695 }
19696
19697 /* Data structure to pass results from dwarf2_read_addr_index_reader
19698 back to dwarf2_read_addr_index. */
19699
19700 struct dwarf2_read_addr_index_data
19701 {
19702 ULONGEST addr_base;
19703 int addr_size;
19704 };
19705
19706 /* die_reader_func for dwarf2_read_addr_index. */
19707
19708 static void
19709 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19710 const gdb_byte *info_ptr,
19711 struct die_info *comp_unit_die,
19712 int has_children,
19713 void *data)
19714 {
19715 struct dwarf2_cu *cu = reader->cu;
19716 struct dwarf2_read_addr_index_data *aidata =
19717 (struct dwarf2_read_addr_index_data *) data;
19718
19719 aidata->addr_base = cu->addr_base;
19720 aidata->addr_size = cu->header.addr_size;
19721 }
19722
19723 /* Given an index in .debug_addr, fetch the value.
19724 NOTE: This can be called during dwarf expression evaluation,
19725 long after the debug information has been read, and thus per_cu->cu
19726 may no longer exist. */
19727
19728 CORE_ADDR
19729 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19730 unsigned int addr_index)
19731 {
19732 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19733 struct objfile *objfile = dwarf2_per_objfile->objfile;
19734 struct dwarf2_cu *cu = per_cu->cu;
19735 ULONGEST addr_base;
19736 int addr_size;
19737
19738 /* We need addr_base and addr_size.
19739 If we don't have PER_CU->cu, we have to get it.
19740 Nasty, but the alternative is storing the needed info in PER_CU,
19741 which at this point doesn't seem justified: it's not clear how frequently
19742 it would get used and it would increase the size of every PER_CU.
19743 Entry points like dwarf2_per_cu_addr_size do a similar thing
19744 so we're not in uncharted territory here.
19745 Alas we need to be a bit more complicated as addr_base is contained
19746 in the DIE.
19747
19748 We don't need to read the entire CU(/TU).
19749 We just need the header and top level die.
19750
19751 IWBN to use the aging mechanism to let us lazily later discard the CU.
19752 For now we skip this optimization. */
19753
19754 if (cu != NULL)
19755 {
19756 addr_base = cu->addr_base;
19757 addr_size = cu->header.addr_size;
19758 }
19759 else
19760 {
19761 struct dwarf2_read_addr_index_data aidata;
19762
19763 /* Note: We can't use init_cutu_and_read_dies_simple here,
19764 we need addr_base. */
19765 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19766 dwarf2_read_addr_index_reader, &aidata);
19767 addr_base = aidata.addr_base;
19768 addr_size = aidata.addr_size;
19769 }
19770
19771 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19772 addr_size);
19773 }
19774
19775 /* Given a DW_FORM_GNU_str_index, fetch the string.
19776 This is only used by the Fission support. */
19777
19778 static const char *
19779 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19780 {
19781 struct dwarf2_cu *cu = reader->cu;
19782 struct dwarf2_per_objfile *dwarf2_per_objfile
19783 = cu->per_cu->dwarf2_per_objfile;
19784 struct objfile *objfile = dwarf2_per_objfile->objfile;
19785 const char *objf_name = objfile_name (objfile);
19786 bfd *abfd = objfile->obfd;
19787 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19788 struct dwarf2_section_info *str_offsets_section =
19789 &reader->dwo_file->sections.str_offsets;
19790 const gdb_byte *info_ptr;
19791 ULONGEST str_offset;
19792 static const char form_name[] = "DW_FORM_GNU_str_index";
19793
19794 dwarf2_read_section (objfile, str_section);
19795 dwarf2_read_section (objfile, str_offsets_section);
19796 if (str_section->buffer == NULL)
19797 error (_("%s used without .debug_str.dwo section"
19798 " in CU at offset %s [in module %s]"),
19799 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19800 if (str_offsets_section->buffer == NULL)
19801 error (_("%s used without .debug_str_offsets.dwo section"
19802 " in CU at offset %s [in module %s]"),
19803 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19804 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19805 error (_("%s pointing outside of .debug_str_offsets.dwo"
19806 " section in CU at offset %s [in module %s]"),
19807 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19808 info_ptr = (str_offsets_section->buffer
19809 + str_index * cu->header.offset_size);
19810 if (cu->header.offset_size == 4)
19811 str_offset = bfd_get_32 (abfd, info_ptr);
19812 else
19813 str_offset = bfd_get_64 (abfd, info_ptr);
19814 if (str_offset >= str_section->size)
19815 error (_("Offset from %s pointing outside of"
19816 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19817 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19818 return (const char *) (str_section->buffer + str_offset);
19819 }
19820
19821 /* Return the length of an LEB128 number in BUF. */
19822
19823 static int
19824 leb128_size (const gdb_byte *buf)
19825 {
19826 const gdb_byte *begin = buf;
19827 gdb_byte byte;
19828
19829 while (1)
19830 {
19831 byte = *buf++;
19832 if ((byte & 128) == 0)
19833 return buf - begin;
19834 }
19835 }
19836
19837 static void
19838 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19839 {
19840 switch (lang)
19841 {
19842 case DW_LANG_C89:
19843 case DW_LANG_C99:
19844 case DW_LANG_C11:
19845 case DW_LANG_C:
19846 case DW_LANG_UPC:
19847 cu->language = language_c;
19848 break;
19849 case DW_LANG_Java:
19850 case DW_LANG_C_plus_plus:
19851 case DW_LANG_C_plus_plus_11:
19852 case DW_LANG_C_plus_plus_14:
19853 cu->language = language_cplus;
19854 break;
19855 case DW_LANG_D:
19856 cu->language = language_d;
19857 break;
19858 case DW_LANG_Fortran77:
19859 case DW_LANG_Fortran90:
19860 case DW_LANG_Fortran95:
19861 case DW_LANG_Fortran03:
19862 case DW_LANG_Fortran08:
19863 cu->language = language_fortran;
19864 break;
19865 case DW_LANG_Go:
19866 cu->language = language_go;
19867 break;
19868 case DW_LANG_Mips_Assembler:
19869 cu->language = language_asm;
19870 break;
19871 case DW_LANG_Ada83:
19872 case DW_LANG_Ada95:
19873 cu->language = language_ada;
19874 break;
19875 case DW_LANG_Modula2:
19876 cu->language = language_m2;
19877 break;
19878 case DW_LANG_Pascal83:
19879 cu->language = language_pascal;
19880 break;
19881 case DW_LANG_ObjC:
19882 cu->language = language_objc;
19883 break;
19884 case DW_LANG_Rust:
19885 case DW_LANG_Rust_old:
19886 cu->language = language_rust;
19887 break;
19888 case DW_LANG_Cobol74:
19889 case DW_LANG_Cobol85:
19890 default:
19891 cu->language = language_minimal;
19892 break;
19893 }
19894 cu->language_defn = language_def (cu->language);
19895 }
19896
19897 /* Return the named attribute or NULL if not there. */
19898
19899 static struct attribute *
19900 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19901 {
19902 for (;;)
19903 {
19904 unsigned int i;
19905 struct attribute *spec = NULL;
19906
19907 for (i = 0; i < die->num_attrs; ++i)
19908 {
19909 if (die->attrs[i].name == name)
19910 return &die->attrs[i];
19911 if (die->attrs[i].name == DW_AT_specification
19912 || die->attrs[i].name == DW_AT_abstract_origin)
19913 spec = &die->attrs[i];
19914 }
19915
19916 if (!spec)
19917 break;
19918
19919 die = follow_die_ref (die, spec, &cu);
19920 }
19921
19922 return NULL;
19923 }
19924
19925 /* Return the named attribute or NULL if not there,
19926 but do not follow DW_AT_specification, etc.
19927 This is for use in contexts where we're reading .debug_types dies.
19928 Following DW_AT_specification, DW_AT_abstract_origin will take us
19929 back up the chain, and we want to go down. */
19930
19931 static struct attribute *
19932 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19933 {
19934 unsigned int i;
19935
19936 for (i = 0; i < die->num_attrs; ++i)
19937 if (die->attrs[i].name == name)
19938 return &die->attrs[i];
19939
19940 return NULL;
19941 }
19942
19943 /* Return the string associated with a string-typed attribute, or NULL if it
19944 is either not found or is of an incorrect type. */
19945
19946 static const char *
19947 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19948 {
19949 struct attribute *attr;
19950 const char *str = NULL;
19951
19952 attr = dwarf2_attr (die, name, cu);
19953
19954 if (attr != NULL)
19955 {
19956 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19957 || attr->form == DW_FORM_string
19958 || attr->form == DW_FORM_GNU_str_index
19959 || attr->form == DW_FORM_GNU_strp_alt)
19960 str = DW_STRING (attr);
19961 else
19962 complaint (&symfile_complaints,
19963 _("string type expected for attribute %s for "
19964 "DIE at %s in module %s"),
19965 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19966 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19967 }
19968
19969 return str;
19970 }
19971
19972 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19973 and holds a non-zero value. This function should only be used for
19974 DW_FORM_flag or DW_FORM_flag_present attributes. */
19975
19976 static int
19977 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19978 {
19979 struct attribute *attr = dwarf2_attr (die, name, cu);
19980
19981 return (attr && DW_UNSND (attr));
19982 }
19983
19984 static int
19985 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19986 {
19987 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19988 which value is non-zero. However, we have to be careful with
19989 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19990 (via dwarf2_flag_true_p) follows this attribute. So we may
19991 end up accidently finding a declaration attribute that belongs
19992 to a different DIE referenced by the specification attribute,
19993 even though the given DIE does not have a declaration attribute. */
19994 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19995 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19996 }
19997
19998 /* Return the die giving the specification for DIE, if there is
19999 one. *SPEC_CU is the CU containing DIE on input, and the CU
20000 containing the return value on output. If there is no
20001 specification, but there is an abstract origin, that is
20002 returned. */
20003
20004 static struct die_info *
20005 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20006 {
20007 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20008 *spec_cu);
20009
20010 if (spec_attr == NULL)
20011 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20012
20013 if (spec_attr == NULL)
20014 return NULL;
20015 else
20016 return follow_die_ref (die, spec_attr, spec_cu);
20017 }
20018
20019 /* Stub for free_line_header to match void * callback types. */
20020
20021 static void
20022 free_line_header_voidp (void *arg)
20023 {
20024 struct line_header *lh = (struct line_header *) arg;
20025
20026 delete lh;
20027 }
20028
20029 void
20030 line_header::add_include_dir (const char *include_dir)
20031 {
20032 if (dwarf_line_debug >= 2)
20033 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20034 include_dirs.size () + 1, include_dir);
20035
20036 include_dirs.push_back (include_dir);
20037 }
20038
20039 void
20040 line_header::add_file_name (const char *name,
20041 dir_index d_index,
20042 unsigned int mod_time,
20043 unsigned int length)
20044 {
20045 if (dwarf_line_debug >= 2)
20046 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
20047 (unsigned) file_names.size () + 1, name);
20048
20049 file_names.emplace_back (name, d_index, mod_time, length);
20050 }
20051
20052 /* A convenience function to find the proper .debug_line section for a CU. */
20053
20054 static struct dwarf2_section_info *
20055 get_debug_line_section (struct dwarf2_cu *cu)
20056 {
20057 struct dwarf2_section_info *section;
20058 struct dwarf2_per_objfile *dwarf2_per_objfile
20059 = cu->per_cu->dwarf2_per_objfile;
20060
20061 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20062 DWO file. */
20063 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20064 section = &cu->dwo_unit->dwo_file->sections.line;
20065 else if (cu->per_cu->is_dwz)
20066 {
20067 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20068
20069 section = &dwz->line;
20070 }
20071 else
20072 section = &dwarf2_per_objfile->line;
20073
20074 return section;
20075 }
20076
20077 /* Read directory or file name entry format, starting with byte of
20078 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20079 entries count and the entries themselves in the described entry
20080 format. */
20081
20082 static void
20083 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20084 bfd *abfd, const gdb_byte **bufp,
20085 struct line_header *lh,
20086 const struct comp_unit_head *cu_header,
20087 void (*callback) (struct line_header *lh,
20088 const char *name,
20089 dir_index d_index,
20090 unsigned int mod_time,
20091 unsigned int length))
20092 {
20093 gdb_byte format_count, formati;
20094 ULONGEST data_count, datai;
20095 const gdb_byte *buf = *bufp;
20096 const gdb_byte *format_header_data;
20097 unsigned int bytes_read;
20098
20099 format_count = read_1_byte (abfd, buf);
20100 buf += 1;
20101 format_header_data = buf;
20102 for (formati = 0; formati < format_count; formati++)
20103 {
20104 read_unsigned_leb128 (abfd, buf, &bytes_read);
20105 buf += bytes_read;
20106 read_unsigned_leb128 (abfd, buf, &bytes_read);
20107 buf += bytes_read;
20108 }
20109
20110 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20111 buf += bytes_read;
20112 for (datai = 0; datai < data_count; datai++)
20113 {
20114 const gdb_byte *format = format_header_data;
20115 struct file_entry fe;
20116
20117 for (formati = 0; formati < format_count; formati++)
20118 {
20119 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20120 format += bytes_read;
20121
20122 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20123 format += bytes_read;
20124
20125 gdb::optional<const char *> string;
20126 gdb::optional<unsigned int> uint;
20127
20128 switch (form)
20129 {
20130 case DW_FORM_string:
20131 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20132 buf += bytes_read;
20133 break;
20134
20135 case DW_FORM_line_strp:
20136 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20137 abfd, buf,
20138 cu_header,
20139 &bytes_read));
20140 buf += bytes_read;
20141 break;
20142
20143 case DW_FORM_data1:
20144 uint.emplace (read_1_byte (abfd, buf));
20145 buf += 1;
20146 break;
20147
20148 case DW_FORM_data2:
20149 uint.emplace (read_2_bytes (abfd, buf));
20150 buf += 2;
20151 break;
20152
20153 case DW_FORM_data4:
20154 uint.emplace (read_4_bytes (abfd, buf));
20155 buf += 4;
20156 break;
20157
20158 case DW_FORM_data8:
20159 uint.emplace (read_8_bytes (abfd, buf));
20160 buf += 8;
20161 break;
20162
20163 case DW_FORM_udata:
20164 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20165 buf += bytes_read;
20166 break;
20167
20168 case DW_FORM_block:
20169 /* It is valid only for DW_LNCT_timestamp which is ignored by
20170 current GDB. */
20171 break;
20172 }
20173
20174 switch (content_type)
20175 {
20176 case DW_LNCT_path:
20177 if (string.has_value ())
20178 fe.name = *string;
20179 break;
20180 case DW_LNCT_directory_index:
20181 if (uint.has_value ())
20182 fe.d_index = (dir_index) *uint;
20183 break;
20184 case DW_LNCT_timestamp:
20185 if (uint.has_value ())
20186 fe.mod_time = *uint;
20187 break;
20188 case DW_LNCT_size:
20189 if (uint.has_value ())
20190 fe.length = *uint;
20191 break;
20192 case DW_LNCT_MD5:
20193 break;
20194 default:
20195 complaint (&symfile_complaints,
20196 _("Unknown format content type %s"),
20197 pulongest (content_type));
20198 }
20199 }
20200
20201 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20202 }
20203
20204 *bufp = buf;
20205 }
20206
20207 /* Read the statement program header starting at OFFSET in
20208 .debug_line, or .debug_line.dwo. Return a pointer
20209 to a struct line_header, allocated using xmalloc.
20210 Returns NULL if there is a problem reading the header, e.g., if it
20211 has a version we don't understand.
20212
20213 NOTE: the strings in the include directory and file name tables of
20214 the returned object point into the dwarf line section buffer,
20215 and must not be freed. */
20216
20217 static line_header_up
20218 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20219 {
20220 const gdb_byte *line_ptr;
20221 unsigned int bytes_read, offset_size;
20222 int i;
20223 const char *cur_dir, *cur_file;
20224 struct dwarf2_section_info *section;
20225 bfd *abfd;
20226 struct dwarf2_per_objfile *dwarf2_per_objfile
20227 = cu->per_cu->dwarf2_per_objfile;
20228
20229 section = get_debug_line_section (cu);
20230 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20231 if (section->buffer == NULL)
20232 {
20233 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20234 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20235 else
20236 complaint (&symfile_complaints, _("missing .debug_line section"));
20237 return 0;
20238 }
20239
20240 /* We can't do this until we know the section is non-empty.
20241 Only then do we know we have such a section. */
20242 abfd = get_section_bfd_owner (section);
20243
20244 /* Make sure that at least there's room for the total_length field.
20245 That could be 12 bytes long, but we're just going to fudge that. */
20246 if (to_underlying (sect_off) + 4 >= section->size)
20247 {
20248 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20249 return 0;
20250 }
20251
20252 line_header_up lh (new line_header ());
20253
20254 lh->sect_off = sect_off;
20255 lh->offset_in_dwz = cu->per_cu->is_dwz;
20256
20257 line_ptr = section->buffer + to_underlying (sect_off);
20258
20259 /* Read in the header. */
20260 lh->total_length =
20261 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20262 &bytes_read, &offset_size);
20263 line_ptr += bytes_read;
20264 if (line_ptr + lh->total_length > (section->buffer + section->size))
20265 {
20266 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20267 return 0;
20268 }
20269 lh->statement_program_end = line_ptr + lh->total_length;
20270 lh->version = read_2_bytes (abfd, line_ptr);
20271 line_ptr += 2;
20272 if (lh->version > 5)
20273 {
20274 /* This is a version we don't understand. The format could have
20275 changed in ways we don't handle properly so just punt. */
20276 complaint (&symfile_complaints,
20277 _("unsupported version in .debug_line section"));
20278 return NULL;
20279 }
20280 if (lh->version >= 5)
20281 {
20282 gdb_byte segment_selector_size;
20283
20284 /* Skip address size. */
20285 read_1_byte (abfd, line_ptr);
20286 line_ptr += 1;
20287
20288 segment_selector_size = read_1_byte (abfd, line_ptr);
20289 line_ptr += 1;
20290 if (segment_selector_size != 0)
20291 {
20292 complaint (&symfile_complaints,
20293 _("unsupported segment selector size %u "
20294 "in .debug_line section"),
20295 segment_selector_size);
20296 return NULL;
20297 }
20298 }
20299 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20300 line_ptr += offset_size;
20301 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20302 line_ptr += 1;
20303 if (lh->version >= 4)
20304 {
20305 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20306 line_ptr += 1;
20307 }
20308 else
20309 lh->maximum_ops_per_instruction = 1;
20310
20311 if (lh->maximum_ops_per_instruction == 0)
20312 {
20313 lh->maximum_ops_per_instruction = 1;
20314 complaint (&symfile_complaints,
20315 _("invalid maximum_ops_per_instruction "
20316 "in `.debug_line' section"));
20317 }
20318
20319 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20320 line_ptr += 1;
20321 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20322 line_ptr += 1;
20323 lh->line_range = read_1_byte (abfd, line_ptr);
20324 line_ptr += 1;
20325 lh->opcode_base = read_1_byte (abfd, line_ptr);
20326 line_ptr += 1;
20327 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20328
20329 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20330 for (i = 1; i < lh->opcode_base; ++i)
20331 {
20332 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20333 line_ptr += 1;
20334 }
20335
20336 if (lh->version >= 5)
20337 {
20338 /* Read directory table. */
20339 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20340 &cu->header,
20341 [] (struct line_header *lh, const char *name,
20342 dir_index d_index, unsigned int mod_time,
20343 unsigned int length)
20344 {
20345 lh->add_include_dir (name);
20346 });
20347
20348 /* Read file name table. */
20349 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20350 &cu->header,
20351 [] (struct line_header *lh, const char *name,
20352 dir_index d_index, unsigned int mod_time,
20353 unsigned int length)
20354 {
20355 lh->add_file_name (name, d_index, mod_time, length);
20356 });
20357 }
20358 else
20359 {
20360 /* Read directory table. */
20361 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20362 {
20363 line_ptr += bytes_read;
20364 lh->add_include_dir (cur_dir);
20365 }
20366 line_ptr += bytes_read;
20367
20368 /* Read file name table. */
20369 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20370 {
20371 unsigned int mod_time, length;
20372 dir_index d_index;
20373
20374 line_ptr += bytes_read;
20375 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20376 line_ptr += bytes_read;
20377 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20378 line_ptr += bytes_read;
20379 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20380 line_ptr += bytes_read;
20381
20382 lh->add_file_name (cur_file, d_index, mod_time, length);
20383 }
20384 line_ptr += bytes_read;
20385 }
20386 lh->statement_program_start = line_ptr;
20387
20388 if (line_ptr > (section->buffer + section->size))
20389 complaint (&symfile_complaints,
20390 _("line number info header doesn't "
20391 "fit in `.debug_line' section"));
20392
20393 return lh;
20394 }
20395
20396 /* Subroutine of dwarf_decode_lines to simplify it.
20397 Return the file name of the psymtab for included file FILE_INDEX
20398 in line header LH of PST.
20399 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20400 If space for the result is malloc'd, *NAME_HOLDER will be set.
20401 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20402
20403 static const char *
20404 psymtab_include_file_name (const struct line_header *lh, int file_index,
20405 const struct partial_symtab *pst,
20406 const char *comp_dir,
20407 gdb::unique_xmalloc_ptr<char> *name_holder)
20408 {
20409 const file_entry &fe = lh->file_names[file_index];
20410 const char *include_name = fe.name;
20411 const char *include_name_to_compare = include_name;
20412 const char *pst_filename;
20413 int file_is_pst;
20414
20415 const char *dir_name = fe.include_dir (lh);
20416
20417 gdb::unique_xmalloc_ptr<char> hold_compare;
20418 if (!IS_ABSOLUTE_PATH (include_name)
20419 && (dir_name != NULL || comp_dir != NULL))
20420 {
20421 /* Avoid creating a duplicate psymtab for PST.
20422 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20423 Before we do the comparison, however, we need to account
20424 for DIR_NAME and COMP_DIR.
20425 First prepend dir_name (if non-NULL). If we still don't
20426 have an absolute path prepend comp_dir (if non-NULL).
20427 However, the directory we record in the include-file's
20428 psymtab does not contain COMP_DIR (to match the
20429 corresponding symtab(s)).
20430
20431 Example:
20432
20433 bash$ cd /tmp
20434 bash$ gcc -g ./hello.c
20435 include_name = "hello.c"
20436 dir_name = "."
20437 DW_AT_comp_dir = comp_dir = "/tmp"
20438 DW_AT_name = "./hello.c"
20439
20440 */
20441
20442 if (dir_name != NULL)
20443 {
20444 name_holder->reset (concat (dir_name, SLASH_STRING,
20445 include_name, (char *) NULL));
20446 include_name = name_holder->get ();
20447 include_name_to_compare = include_name;
20448 }
20449 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20450 {
20451 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20452 include_name, (char *) NULL));
20453 include_name_to_compare = hold_compare.get ();
20454 }
20455 }
20456
20457 pst_filename = pst->filename;
20458 gdb::unique_xmalloc_ptr<char> copied_name;
20459 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20460 {
20461 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20462 pst_filename, (char *) NULL));
20463 pst_filename = copied_name.get ();
20464 }
20465
20466 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20467
20468 if (file_is_pst)
20469 return NULL;
20470 return include_name;
20471 }
20472
20473 /* State machine to track the state of the line number program. */
20474
20475 class lnp_state_machine
20476 {
20477 public:
20478 /* Initialize a machine state for the start of a line number
20479 program. */
20480 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20481
20482 file_entry *current_file ()
20483 {
20484 /* lh->file_names is 0-based, but the file name numbers in the
20485 statement program are 1-based. */
20486 return m_line_header->file_name_at (m_file);
20487 }
20488
20489 /* Record the line in the state machine. END_SEQUENCE is true if
20490 we're processing the end of a sequence. */
20491 void record_line (bool end_sequence);
20492
20493 /* Check address and if invalid nop-out the rest of the lines in this
20494 sequence. */
20495 void check_line_address (struct dwarf2_cu *cu,
20496 const gdb_byte *line_ptr,
20497 CORE_ADDR lowpc, CORE_ADDR address);
20498
20499 void handle_set_discriminator (unsigned int discriminator)
20500 {
20501 m_discriminator = discriminator;
20502 m_line_has_non_zero_discriminator |= discriminator != 0;
20503 }
20504
20505 /* Handle DW_LNE_set_address. */
20506 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20507 {
20508 m_op_index = 0;
20509 address += baseaddr;
20510 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20511 }
20512
20513 /* Handle DW_LNS_advance_pc. */
20514 void handle_advance_pc (CORE_ADDR adjust);
20515
20516 /* Handle a special opcode. */
20517 void handle_special_opcode (unsigned char op_code);
20518
20519 /* Handle DW_LNS_advance_line. */
20520 void handle_advance_line (int line_delta)
20521 {
20522 advance_line (line_delta);
20523 }
20524
20525 /* Handle DW_LNS_set_file. */
20526 void handle_set_file (file_name_index file);
20527
20528 /* Handle DW_LNS_negate_stmt. */
20529 void handle_negate_stmt ()
20530 {
20531 m_is_stmt = !m_is_stmt;
20532 }
20533
20534 /* Handle DW_LNS_const_add_pc. */
20535 void handle_const_add_pc ();
20536
20537 /* Handle DW_LNS_fixed_advance_pc. */
20538 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20539 {
20540 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20541 m_op_index = 0;
20542 }
20543
20544 /* Handle DW_LNS_copy. */
20545 void handle_copy ()
20546 {
20547 record_line (false);
20548 m_discriminator = 0;
20549 }
20550
20551 /* Handle DW_LNE_end_sequence. */
20552 void handle_end_sequence ()
20553 {
20554 m_record_line_callback = ::record_line;
20555 }
20556
20557 private:
20558 /* Advance the line by LINE_DELTA. */
20559 void advance_line (int line_delta)
20560 {
20561 m_line += line_delta;
20562
20563 if (line_delta != 0)
20564 m_line_has_non_zero_discriminator = m_discriminator != 0;
20565 }
20566
20567 gdbarch *m_gdbarch;
20568
20569 /* True if we're recording lines.
20570 Otherwise we're building partial symtabs and are just interested in
20571 finding include files mentioned by the line number program. */
20572 bool m_record_lines_p;
20573
20574 /* The line number header. */
20575 line_header *m_line_header;
20576
20577 /* These are part of the standard DWARF line number state machine,
20578 and initialized according to the DWARF spec. */
20579
20580 unsigned char m_op_index = 0;
20581 /* The line table index (1-based) of the current file. */
20582 file_name_index m_file = (file_name_index) 1;
20583 unsigned int m_line = 1;
20584
20585 /* These are initialized in the constructor. */
20586
20587 CORE_ADDR m_address;
20588 bool m_is_stmt;
20589 unsigned int m_discriminator;
20590
20591 /* Additional bits of state we need to track. */
20592
20593 /* The last file that we called dwarf2_start_subfile for.
20594 This is only used for TLLs. */
20595 unsigned int m_last_file = 0;
20596 /* The last file a line number was recorded for. */
20597 struct subfile *m_last_subfile = NULL;
20598
20599 /* The function to call to record a line. */
20600 record_line_ftype *m_record_line_callback = NULL;
20601
20602 /* The last line number that was recorded, used to coalesce
20603 consecutive entries for the same line. This can happen, for
20604 example, when discriminators are present. PR 17276. */
20605 unsigned int m_last_line = 0;
20606 bool m_line_has_non_zero_discriminator = false;
20607 };
20608
20609 void
20610 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20611 {
20612 CORE_ADDR addr_adj = (((m_op_index + adjust)
20613 / m_line_header->maximum_ops_per_instruction)
20614 * m_line_header->minimum_instruction_length);
20615 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20616 m_op_index = ((m_op_index + adjust)
20617 % m_line_header->maximum_ops_per_instruction);
20618 }
20619
20620 void
20621 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20622 {
20623 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20624 CORE_ADDR addr_adj = (((m_op_index
20625 + (adj_opcode / m_line_header->line_range))
20626 / m_line_header->maximum_ops_per_instruction)
20627 * m_line_header->minimum_instruction_length);
20628 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20629 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20630 % m_line_header->maximum_ops_per_instruction);
20631
20632 int line_delta = (m_line_header->line_base
20633 + (adj_opcode % m_line_header->line_range));
20634 advance_line (line_delta);
20635 record_line (false);
20636 m_discriminator = 0;
20637 }
20638
20639 void
20640 lnp_state_machine::handle_set_file (file_name_index file)
20641 {
20642 m_file = file;
20643
20644 const file_entry *fe = current_file ();
20645 if (fe == NULL)
20646 dwarf2_debug_line_missing_file_complaint ();
20647 else if (m_record_lines_p)
20648 {
20649 const char *dir = fe->include_dir (m_line_header);
20650
20651 m_last_subfile = current_subfile;
20652 m_line_has_non_zero_discriminator = m_discriminator != 0;
20653 dwarf2_start_subfile (fe->name, dir);
20654 }
20655 }
20656
20657 void
20658 lnp_state_machine::handle_const_add_pc ()
20659 {
20660 CORE_ADDR adjust
20661 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20662
20663 CORE_ADDR addr_adj
20664 = (((m_op_index + adjust)
20665 / m_line_header->maximum_ops_per_instruction)
20666 * m_line_header->minimum_instruction_length);
20667
20668 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20669 m_op_index = ((m_op_index + adjust)
20670 % m_line_header->maximum_ops_per_instruction);
20671 }
20672
20673 /* Ignore this record_line request. */
20674
20675 static void
20676 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20677 {
20678 return;
20679 }
20680
20681 /* Return non-zero if we should add LINE to the line number table.
20682 LINE is the line to add, LAST_LINE is the last line that was added,
20683 LAST_SUBFILE is the subfile for LAST_LINE.
20684 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20685 had a non-zero discriminator.
20686
20687 We have to be careful in the presence of discriminators.
20688 E.g., for this line:
20689
20690 for (i = 0; i < 100000; i++);
20691
20692 clang can emit four line number entries for that one line,
20693 each with a different discriminator.
20694 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20695
20696 However, we want gdb to coalesce all four entries into one.
20697 Otherwise the user could stepi into the middle of the line and
20698 gdb would get confused about whether the pc really was in the
20699 middle of the line.
20700
20701 Things are further complicated by the fact that two consecutive
20702 line number entries for the same line is a heuristic used by gcc
20703 to denote the end of the prologue. So we can't just discard duplicate
20704 entries, we have to be selective about it. The heuristic we use is
20705 that we only collapse consecutive entries for the same line if at least
20706 one of those entries has a non-zero discriminator. PR 17276.
20707
20708 Note: Addresses in the line number state machine can never go backwards
20709 within one sequence, thus this coalescing is ok. */
20710
20711 static int
20712 dwarf_record_line_p (unsigned int line, unsigned int last_line,
20713 int line_has_non_zero_discriminator,
20714 struct subfile *last_subfile)
20715 {
20716 if (current_subfile != last_subfile)
20717 return 1;
20718 if (line != last_line)
20719 return 1;
20720 /* Same line for the same file that we've seen already.
20721 As a last check, for pr 17276, only record the line if the line
20722 has never had a non-zero discriminator. */
20723 if (!line_has_non_zero_discriminator)
20724 return 1;
20725 return 0;
20726 }
20727
20728 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20729 in the line table of subfile SUBFILE. */
20730
20731 static void
20732 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20733 unsigned int line, CORE_ADDR address,
20734 record_line_ftype p_record_line)
20735 {
20736 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20737
20738 if (dwarf_line_debug)
20739 {
20740 fprintf_unfiltered (gdb_stdlog,
20741 "Recording line %u, file %s, address %s\n",
20742 line, lbasename (subfile->name),
20743 paddress (gdbarch, address));
20744 }
20745
20746 (*p_record_line) (subfile, line, addr);
20747 }
20748
20749 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20750 Mark the end of a set of line number records.
20751 The arguments are the same as for dwarf_record_line_1.
20752 If SUBFILE is NULL the request is ignored. */
20753
20754 static void
20755 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20756 CORE_ADDR address, record_line_ftype p_record_line)
20757 {
20758 if (subfile == NULL)
20759 return;
20760
20761 if (dwarf_line_debug)
20762 {
20763 fprintf_unfiltered (gdb_stdlog,
20764 "Finishing current line, file %s, address %s\n",
20765 lbasename (subfile->name),
20766 paddress (gdbarch, address));
20767 }
20768
20769 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20770 }
20771
20772 void
20773 lnp_state_machine::record_line (bool end_sequence)
20774 {
20775 if (dwarf_line_debug)
20776 {
20777 fprintf_unfiltered (gdb_stdlog,
20778 "Processing actual line %u: file %u,"
20779 " address %s, is_stmt %u, discrim %u\n",
20780 m_line, to_underlying (m_file),
20781 paddress (m_gdbarch, m_address),
20782 m_is_stmt, m_discriminator);
20783 }
20784
20785 file_entry *fe = current_file ();
20786
20787 if (fe == NULL)
20788 dwarf2_debug_line_missing_file_complaint ();
20789 /* For now we ignore lines not starting on an instruction boundary.
20790 But not when processing end_sequence for compatibility with the
20791 previous version of the code. */
20792 else if (m_op_index == 0 || end_sequence)
20793 {
20794 fe->included_p = 1;
20795 if (m_record_lines_p && m_is_stmt)
20796 {
20797 if (m_last_subfile != current_subfile || end_sequence)
20798 {
20799 dwarf_finish_line (m_gdbarch, m_last_subfile,
20800 m_address, m_record_line_callback);
20801 }
20802
20803 if (!end_sequence)
20804 {
20805 if (dwarf_record_line_p (m_line, m_last_line,
20806 m_line_has_non_zero_discriminator,
20807 m_last_subfile))
20808 {
20809 dwarf_record_line_1 (m_gdbarch, current_subfile,
20810 m_line, m_address,
20811 m_record_line_callback);
20812 }
20813 m_last_subfile = current_subfile;
20814 m_last_line = m_line;
20815 }
20816 }
20817 }
20818 }
20819
20820 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20821 bool record_lines_p)
20822 {
20823 m_gdbarch = arch;
20824 m_record_lines_p = record_lines_p;
20825 m_line_header = lh;
20826
20827 m_record_line_callback = ::record_line;
20828
20829 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20830 was a line entry for it so that the backend has a chance to adjust it
20831 and also record it in case it needs it. This is currently used by MIPS
20832 code, cf. `mips_adjust_dwarf2_line'. */
20833 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20834 m_is_stmt = lh->default_is_stmt;
20835 m_discriminator = 0;
20836 }
20837
20838 void
20839 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20840 const gdb_byte *line_ptr,
20841 CORE_ADDR lowpc, CORE_ADDR address)
20842 {
20843 /* If address < lowpc then it's not a usable value, it's outside the
20844 pc range of the CU. However, we restrict the test to only address
20845 values of zero to preserve GDB's previous behaviour which is to
20846 handle the specific case of a function being GC'd by the linker. */
20847
20848 if (address == 0 && address < lowpc)
20849 {
20850 /* This line table is for a function which has been
20851 GCd by the linker. Ignore it. PR gdb/12528 */
20852
20853 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20854 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20855
20856 complaint (&symfile_complaints,
20857 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20858 line_offset, objfile_name (objfile));
20859 m_record_line_callback = noop_record_line;
20860 /* Note: record_line_callback is left as noop_record_line until
20861 we see DW_LNE_end_sequence. */
20862 }
20863 }
20864
20865 /* Subroutine of dwarf_decode_lines to simplify it.
20866 Process the line number information in LH.
20867 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20868 program in order to set included_p for every referenced header. */
20869
20870 static void
20871 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20872 const int decode_for_pst_p, CORE_ADDR lowpc)
20873 {
20874 const gdb_byte *line_ptr, *extended_end;
20875 const gdb_byte *line_end;
20876 unsigned int bytes_read, extended_len;
20877 unsigned char op_code, extended_op;
20878 CORE_ADDR baseaddr;
20879 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20880 bfd *abfd = objfile->obfd;
20881 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20882 /* True if we're recording line info (as opposed to building partial
20883 symtabs and just interested in finding include files mentioned by
20884 the line number program). */
20885 bool record_lines_p = !decode_for_pst_p;
20886
20887 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20888
20889 line_ptr = lh->statement_program_start;
20890 line_end = lh->statement_program_end;
20891
20892 /* Read the statement sequences until there's nothing left. */
20893 while (line_ptr < line_end)
20894 {
20895 /* The DWARF line number program state machine. Reset the state
20896 machine at the start of each sequence. */
20897 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20898 bool end_sequence = false;
20899
20900 if (record_lines_p)
20901 {
20902 /* Start a subfile for the current file of the state
20903 machine. */
20904 const file_entry *fe = state_machine.current_file ();
20905
20906 if (fe != NULL)
20907 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
20908 }
20909
20910 /* Decode the table. */
20911 while (line_ptr < line_end && !end_sequence)
20912 {
20913 op_code = read_1_byte (abfd, line_ptr);
20914 line_ptr += 1;
20915
20916 if (op_code >= lh->opcode_base)
20917 {
20918 /* Special opcode. */
20919 state_machine.handle_special_opcode (op_code);
20920 }
20921 else switch (op_code)
20922 {
20923 case DW_LNS_extended_op:
20924 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20925 &bytes_read);
20926 line_ptr += bytes_read;
20927 extended_end = line_ptr + extended_len;
20928 extended_op = read_1_byte (abfd, line_ptr);
20929 line_ptr += 1;
20930 switch (extended_op)
20931 {
20932 case DW_LNE_end_sequence:
20933 state_machine.handle_end_sequence ();
20934 end_sequence = true;
20935 break;
20936 case DW_LNE_set_address:
20937 {
20938 CORE_ADDR address
20939 = read_address (abfd, line_ptr, cu, &bytes_read);
20940 line_ptr += bytes_read;
20941
20942 state_machine.check_line_address (cu, line_ptr,
20943 lowpc, address);
20944 state_machine.handle_set_address (baseaddr, address);
20945 }
20946 break;
20947 case DW_LNE_define_file:
20948 {
20949 const char *cur_file;
20950 unsigned int mod_time, length;
20951 dir_index dindex;
20952
20953 cur_file = read_direct_string (abfd, line_ptr,
20954 &bytes_read);
20955 line_ptr += bytes_read;
20956 dindex = (dir_index)
20957 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20958 line_ptr += bytes_read;
20959 mod_time =
20960 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20961 line_ptr += bytes_read;
20962 length =
20963 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20964 line_ptr += bytes_read;
20965 lh->add_file_name (cur_file, dindex, mod_time, length);
20966 }
20967 break;
20968 case DW_LNE_set_discriminator:
20969 {
20970 /* The discriminator is not interesting to the
20971 debugger; just ignore it. We still need to
20972 check its value though:
20973 if there are consecutive entries for the same
20974 (non-prologue) line we want to coalesce them.
20975 PR 17276. */
20976 unsigned int discr
20977 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20978 line_ptr += bytes_read;
20979
20980 state_machine.handle_set_discriminator (discr);
20981 }
20982 break;
20983 default:
20984 complaint (&symfile_complaints,
20985 _("mangled .debug_line section"));
20986 return;
20987 }
20988 /* Make sure that we parsed the extended op correctly. If e.g.
20989 we expected a different address size than the producer used,
20990 we may have read the wrong number of bytes. */
20991 if (line_ptr != extended_end)
20992 {
20993 complaint (&symfile_complaints,
20994 _("mangled .debug_line section"));
20995 return;
20996 }
20997 break;
20998 case DW_LNS_copy:
20999 state_machine.handle_copy ();
21000 break;
21001 case DW_LNS_advance_pc:
21002 {
21003 CORE_ADDR adjust
21004 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21005 line_ptr += bytes_read;
21006
21007 state_machine.handle_advance_pc (adjust);
21008 }
21009 break;
21010 case DW_LNS_advance_line:
21011 {
21012 int line_delta
21013 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21014 line_ptr += bytes_read;
21015
21016 state_machine.handle_advance_line (line_delta);
21017 }
21018 break;
21019 case DW_LNS_set_file:
21020 {
21021 file_name_index file
21022 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21023 &bytes_read);
21024 line_ptr += bytes_read;
21025
21026 state_machine.handle_set_file (file);
21027 }
21028 break;
21029 case DW_LNS_set_column:
21030 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21031 line_ptr += bytes_read;
21032 break;
21033 case DW_LNS_negate_stmt:
21034 state_machine.handle_negate_stmt ();
21035 break;
21036 case DW_LNS_set_basic_block:
21037 break;
21038 /* Add to the address register of the state machine the
21039 address increment value corresponding to special opcode
21040 255. I.e., this value is scaled by the minimum
21041 instruction length since special opcode 255 would have
21042 scaled the increment. */
21043 case DW_LNS_const_add_pc:
21044 state_machine.handle_const_add_pc ();
21045 break;
21046 case DW_LNS_fixed_advance_pc:
21047 {
21048 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21049 line_ptr += 2;
21050
21051 state_machine.handle_fixed_advance_pc (addr_adj);
21052 }
21053 break;
21054 default:
21055 {
21056 /* Unknown standard opcode, ignore it. */
21057 int i;
21058
21059 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21060 {
21061 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21062 line_ptr += bytes_read;
21063 }
21064 }
21065 }
21066 }
21067
21068 if (!end_sequence)
21069 dwarf2_debug_line_missing_end_sequence_complaint ();
21070
21071 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21072 in which case we still finish recording the last line). */
21073 state_machine.record_line (true);
21074 }
21075 }
21076
21077 /* Decode the Line Number Program (LNP) for the given line_header
21078 structure and CU. The actual information extracted and the type
21079 of structures created from the LNP depends on the value of PST.
21080
21081 1. If PST is NULL, then this procedure uses the data from the program
21082 to create all necessary symbol tables, and their linetables.
21083
21084 2. If PST is not NULL, this procedure reads the program to determine
21085 the list of files included by the unit represented by PST, and
21086 builds all the associated partial symbol tables.
21087
21088 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21089 It is used for relative paths in the line table.
21090 NOTE: When processing partial symtabs (pst != NULL),
21091 comp_dir == pst->dirname.
21092
21093 NOTE: It is important that psymtabs have the same file name (via strcmp)
21094 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21095 symtab we don't use it in the name of the psymtabs we create.
21096 E.g. expand_line_sal requires this when finding psymtabs to expand.
21097 A good testcase for this is mb-inline.exp.
21098
21099 LOWPC is the lowest address in CU (or 0 if not known).
21100
21101 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21102 for its PC<->lines mapping information. Otherwise only the filename
21103 table is read in. */
21104
21105 static void
21106 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21107 struct dwarf2_cu *cu, struct partial_symtab *pst,
21108 CORE_ADDR lowpc, int decode_mapping)
21109 {
21110 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21111 const int decode_for_pst_p = (pst != NULL);
21112
21113 if (decode_mapping)
21114 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21115
21116 if (decode_for_pst_p)
21117 {
21118 int file_index;
21119
21120 /* Now that we're done scanning the Line Header Program, we can
21121 create the psymtab of each included file. */
21122 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21123 if (lh->file_names[file_index].included_p == 1)
21124 {
21125 gdb::unique_xmalloc_ptr<char> name_holder;
21126 const char *include_name =
21127 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21128 &name_holder);
21129 if (include_name != NULL)
21130 dwarf2_create_include_psymtab (include_name, pst, objfile);
21131 }
21132 }
21133 else
21134 {
21135 /* Make sure a symtab is created for every file, even files
21136 which contain only variables (i.e. no code with associated
21137 line numbers). */
21138 struct compunit_symtab *cust = buildsym_compunit_symtab ();
21139 int i;
21140
21141 for (i = 0; i < lh->file_names.size (); i++)
21142 {
21143 file_entry &fe = lh->file_names[i];
21144
21145 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
21146
21147 if (current_subfile->symtab == NULL)
21148 {
21149 current_subfile->symtab
21150 = allocate_symtab (cust, current_subfile->name);
21151 }
21152 fe.symtab = current_subfile->symtab;
21153 }
21154 }
21155 }
21156
21157 /* Start a subfile for DWARF. FILENAME is the name of the file and
21158 DIRNAME the name of the source directory which contains FILENAME
21159 or NULL if not known.
21160 This routine tries to keep line numbers from identical absolute and
21161 relative file names in a common subfile.
21162
21163 Using the `list' example from the GDB testsuite, which resides in
21164 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21165 of /srcdir/list0.c yields the following debugging information for list0.c:
21166
21167 DW_AT_name: /srcdir/list0.c
21168 DW_AT_comp_dir: /compdir
21169 files.files[0].name: list0.h
21170 files.files[0].dir: /srcdir
21171 files.files[1].name: list0.c
21172 files.files[1].dir: /srcdir
21173
21174 The line number information for list0.c has to end up in a single
21175 subfile, so that `break /srcdir/list0.c:1' works as expected.
21176 start_subfile will ensure that this happens provided that we pass the
21177 concatenation of files.files[1].dir and files.files[1].name as the
21178 subfile's name. */
21179
21180 static void
21181 dwarf2_start_subfile (const char *filename, const char *dirname)
21182 {
21183 char *copy = NULL;
21184
21185 /* In order not to lose the line information directory,
21186 we concatenate it to the filename when it makes sense.
21187 Note that the Dwarf3 standard says (speaking of filenames in line
21188 information): ``The directory index is ignored for file names
21189 that represent full path names''. Thus ignoring dirname in the
21190 `else' branch below isn't an issue. */
21191
21192 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21193 {
21194 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21195 filename = copy;
21196 }
21197
21198 start_subfile (filename);
21199
21200 if (copy != NULL)
21201 xfree (copy);
21202 }
21203
21204 /* Start a symtab for DWARF.
21205 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21206
21207 static struct compunit_symtab *
21208 dwarf2_start_symtab (struct dwarf2_cu *cu,
21209 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21210 {
21211 struct compunit_symtab *cust
21212 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21213 low_pc, cu->language);
21214
21215 record_debugformat ("DWARF 2");
21216 record_producer (cu->producer);
21217
21218 /* We assume that we're processing GCC output. */
21219 processing_gcc_compilation = 2;
21220
21221 cu->processing_has_namespace_info = 0;
21222
21223 return cust;
21224 }
21225
21226 static void
21227 var_decode_location (struct attribute *attr, struct symbol *sym,
21228 struct dwarf2_cu *cu)
21229 {
21230 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21231 struct comp_unit_head *cu_header = &cu->header;
21232
21233 /* NOTE drow/2003-01-30: There used to be a comment and some special
21234 code here to turn a symbol with DW_AT_external and a
21235 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21236 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21237 with some versions of binutils) where shared libraries could have
21238 relocations against symbols in their debug information - the
21239 minimal symbol would have the right address, but the debug info
21240 would not. It's no longer necessary, because we will explicitly
21241 apply relocations when we read in the debug information now. */
21242
21243 /* A DW_AT_location attribute with no contents indicates that a
21244 variable has been optimized away. */
21245 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21246 {
21247 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21248 return;
21249 }
21250
21251 /* Handle one degenerate form of location expression specially, to
21252 preserve GDB's previous behavior when section offsets are
21253 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21254 then mark this symbol as LOC_STATIC. */
21255
21256 if (attr_form_is_block (attr)
21257 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21258 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21259 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21260 && (DW_BLOCK (attr)->size
21261 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21262 {
21263 unsigned int dummy;
21264
21265 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21266 SYMBOL_VALUE_ADDRESS (sym) =
21267 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21268 else
21269 SYMBOL_VALUE_ADDRESS (sym) =
21270 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21271 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21272 fixup_symbol_section (sym, objfile);
21273 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21274 SYMBOL_SECTION (sym));
21275 return;
21276 }
21277
21278 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21279 expression evaluator, and use LOC_COMPUTED only when necessary
21280 (i.e. when the value of a register or memory location is
21281 referenced, or a thread-local block, etc.). Then again, it might
21282 not be worthwhile. I'm assuming that it isn't unless performance
21283 or memory numbers show me otherwise. */
21284
21285 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21286
21287 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21288 cu->has_loclist = 1;
21289 }
21290
21291 /* Given a pointer to a DWARF information entry, figure out if we need
21292 to make a symbol table entry for it, and if so, create a new entry
21293 and return a pointer to it.
21294 If TYPE is NULL, determine symbol type from the die, otherwise
21295 used the passed type.
21296 If SPACE is not NULL, use it to hold the new symbol. If it is
21297 NULL, allocate a new symbol on the objfile's obstack. */
21298
21299 static struct symbol *
21300 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21301 struct symbol *space)
21302 {
21303 struct dwarf2_per_objfile *dwarf2_per_objfile
21304 = cu->per_cu->dwarf2_per_objfile;
21305 struct objfile *objfile = dwarf2_per_objfile->objfile;
21306 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21307 struct symbol *sym = NULL;
21308 const char *name;
21309 struct attribute *attr = NULL;
21310 struct attribute *attr2 = NULL;
21311 CORE_ADDR baseaddr;
21312 struct pending **list_to_add = NULL;
21313
21314 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21315
21316 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21317
21318 name = dwarf2_name (die, cu);
21319 if (name)
21320 {
21321 const char *linkagename;
21322 int suppress_add = 0;
21323
21324 if (space)
21325 sym = space;
21326 else
21327 sym = allocate_symbol (objfile);
21328 OBJSTAT (objfile, n_syms++);
21329
21330 /* Cache this symbol's name and the name's demangled form (if any). */
21331 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21332 linkagename = dwarf2_physname (name, die, cu);
21333 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21334
21335 /* Fortran does not have mangling standard and the mangling does differ
21336 between gfortran, iFort etc. */
21337 if (cu->language == language_fortran
21338 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21339 symbol_set_demangled_name (&(sym->ginfo),
21340 dwarf2_full_name (name, die, cu),
21341 NULL);
21342
21343 /* Default assumptions.
21344 Use the passed type or decode it from the die. */
21345 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21346 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21347 if (type != NULL)
21348 SYMBOL_TYPE (sym) = type;
21349 else
21350 SYMBOL_TYPE (sym) = die_type (die, cu);
21351 attr = dwarf2_attr (die,
21352 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21353 cu);
21354 if (attr)
21355 {
21356 SYMBOL_LINE (sym) = DW_UNSND (attr);
21357 }
21358
21359 attr = dwarf2_attr (die,
21360 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21361 cu);
21362 if (attr)
21363 {
21364 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21365 struct file_entry *fe;
21366
21367 if (cu->line_header != NULL)
21368 fe = cu->line_header->file_name_at (file_index);
21369 else
21370 fe = NULL;
21371
21372 if (fe == NULL)
21373 complaint (&symfile_complaints,
21374 _("file index out of range"));
21375 else
21376 symbol_set_symtab (sym, fe->symtab);
21377 }
21378
21379 switch (die->tag)
21380 {
21381 case DW_TAG_label:
21382 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21383 if (attr)
21384 {
21385 CORE_ADDR addr;
21386
21387 addr = attr_value_as_address (attr);
21388 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21389 SYMBOL_VALUE_ADDRESS (sym) = addr;
21390 }
21391 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21392 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21393 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21394 add_symbol_to_list (sym, cu->list_in_scope);
21395 break;
21396 case DW_TAG_subprogram:
21397 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21398 finish_block. */
21399 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21400 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21401 if ((attr2 && (DW_UNSND (attr2) != 0))
21402 || cu->language == language_ada)
21403 {
21404 /* Subprograms marked external are stored as a global symbol.
21405 Ada subprograms, whether marked external or not, are always
21406 stored as a global symbol, because we want to be able to
21407 access them globally. For instance, we want to be able
21408 to break on a nested subprogram without having to
21409 specify the context. */
21410 list_to_add = &global_symbols;
21411 }
21412 else
21413 {
21414 list_to_add = cu->list_in_scope;
21415 }
21416 break;
21417 case DW_TAG_inlined_subroutine:
21418 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21419 finish_block. */
21420 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21421 SYMBOL_INLINED (sym) = 1;
21422 list_to_add = cu->list_in_scope;
21423 break;
21424 case DW_TAG_template_value_param:
21425 suppress_add = 1;
21426 /* Fall through. */
21427 case DW_TAG_constant:
21428 case DW_TAG_variable:
21429 case DW_TAG_member:
21430 /* Compilation with minimal debug info may result in
21431 variables with missing type entries. Change the
21432 misleading `void' type to something sensible. */
21433 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21434 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21435
21436 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21437 /* In the case of DW_TAG_member, we should only be called for
21438 static const members. */
21439 if (die->tag == DW_TAG_member)
21440 {
21441 /* dwarf2_add_field uses die_is_declaration,
21442 so we do the same. */
21443 gdb_assert (die_is_declaration (die, cu));
21444 gdb_assert (attr);
21445 }
21446 if (attr)
21447 {
21448 dwarf2_const_value (attr, sym, cu);
21449 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21450 if (!suppress_add)
21451 {
21452 if (attr2 && (DW_UNSND (attr2) != 0))
21453 list_to_add = &global_symbols;
21454 else
21455 list_to_add = cu->list_in_scope;
21456 }
21457 break;
21458 }
21459 attr = dwarf2_attr (die, DW_AT_location, cu);
21460 if (attr)
21461 {
21462 var_decode_location (attr, sym, cu);
21463 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21464
21465 /* Fortran explicitly imports any global symbols to the local
21466 scope by DW_TAG_common_block. */
21467 if (cu->language == language_fortran && die->parent
21468 && die->parent->tag == DW_TAG_common_block)
21469 attr2 = NULL;
21470
21471 if (SYMBOL_CLASS (sym) == LOC_STATIC
21472 && SYMBOL_VALUE_ADDRESS (sym) == 0
21473 && !dwarf2_per_objfile->has_section_at_zero)
21474 {
21475 /* When a static variable is eliminated by the linker,
21476 the corresponding debug information is not stripped
21477 out, but the variable address is set to null;
21478 do not add such variables into symbol table. */
21479 }
21480 else if (attr2 && (DW_UNSND (attr2) != 0))
21481 {
21482 /* Workaround gfortran PR debug/40040 - it uses
21483 DW_AT_location for variables in -fPIC libraries which may
21484 get overriden by other libraries/executable and get
21485 a different address. Resolve it by the minimal symbol
21486 which may come from inferior's executable using copy
21487 relocation. Make this workaround only for gfortran as for
21488 other compilers GDB cannot guess the minimal symbol
21489 Fortran mangling kind. */
21490 if (cu->language == language_fortran && die->parent
21491 && die->parent->tag == DW_TAG_module
21492 && cu->producer
21493 && startswith (cu->producer, "GNU Fortran"))
21494 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21495
21496 /* A variable with DW_AT_external is never static,
21497 but it may be block-scoped. */
21498 list_to_add = (cu->list_in_scope == &file_symbols
21499 ? &global_symbols : cu->list_in_scope);
21500 }
21501 else
21502 list_to_add = cu->list_in_scope;
21503 }
21504 else
21505 {
21506 /* We do not know the address of this symbol.
21507 If it is an external symbol and we have type information
21508 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21509 The address of the variable will then be determined from
21510 the minimal symbol table whenever the variable is
21511 referenced. */
21512 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21513
21514 /* Fortran explicitly imports any global symbols to the local
21515 scope by DW_TAG_common_block. */
21516 if (cu->language == language_fortran && die->parent
21517 && die->parent->tag == DW_TAG_common_block)
21518 {
21519 /* SYMBOL_CLASS doesn't matter here because
21520 read_common_block is going to reset it. */
21521 if (!suppress_add)
21522 list_to_add = cu->list_in_scope;
21523 }
21524 else if (attr2 && (DW_UNSND (attr2) != 0)
21525 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21526 {
21527 /* A variable with DW_AT_external is never static, but it
21528 may be block-scoped. */
21529 list_to_add = (cu->list_in_scope == &file_symbols
21530 ? &global_symbols : cu->list_in_scope);
21531
21532 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21533 }
21534 else if (!die_is_declaration (die, cu))
21535 {
21536 /* Use the default LOC_OPTIMIZED_OUT class. */
21537 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21538 if (!suppress_add)
21539 list_to_add = cu->list_in_scope;
21540 }
21541 }
21542 break;
21543 case DW_TAG_formal_parameter:
21544 /* If we are inside a function, mark this as an argument. If
21545 not, we might be looking at an argument to an inlined function
21546 when we do not have enough information to show inlined frames;
21547 pretend it's a local variable in that case so that the user can
21548 still see it. */
21549 if (context_stack_depth > 0
21550 && context_stack[context_stack_depth - 1].name != NULL)
21551 SYMBOL_IS_ARGUMENT (sym) = 1;
21552 attr = dwarf2_attr (die, DW_AT_location, cu);
21553 if (attr)
21554 {
21555 var_decode_location (attr, sym, cu);
21556 }
21557 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21558 if (attr)
21559 {
21560 dwarf2_const_value (attr, sym, cu);
21561 }
21562
21563 list_to_add = cu->list_in_scope;
21564 break;
21565 case DW_TAG_unspecified_parameters:
21566 /* From varargs functions; gdb doesn't seem to have any
21567 interest in this information, so just ignore it for now.
21568 (FIXME?) */
21569 break;
21570 case DW_TAG_template_type_param:
21571 suppress_add = 1;
21572 /* Fall through. */
21573 case DW_TAG_class_type:
21574 case DW_TAG_interface_type:
21575 case DW_TAG_structure_type:
21576 case DW_TAG_union_type:
21577 case DW_TAG_set_type:
21578 case DW_TAG_enumeration_type:
21579 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21580 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21581
21582 {
21583 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21584 really ever be static objects: otherwise, if you try
21585 to, say, break of a class's method and you're in a file
21586 which doesn't mention that class, it won't work unless
21587 the check for all static symbols in lookup_symbol_aux
21588 saves you. See the OtherFileClass tests in
21589 gdb.c++/namespace.exp. */
21590
21591 if (!suppress_add)
21592 {
21593 list_to_add = (cu->list_in_scope == &file_symbols
21594 && cu->language == language_cplus
21595 ? &global_symbols : cu->list_in_scope);
21596
21597 /* The semantics of C++ state that "struct foo {
21598 ... }" also defines a typedef for "foo". */
21599 if (cu->language == language_cplus
21600 || cu->language == language_ada
21601 || cu->language == language_d
21602 || cu->language == language_rust)
21603 {
21604 /* The symbol's name is already allocated along
21605 with this objfile, so we don't need to
21606 duplicate it for the type. */
21607 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21608 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21609 }
21610 }
21611 }
21612 break;
21613 case DW_TAG_typedef:
21614 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21615 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21616 list_to_add = cu->list_in_scope;
21617 break;
21618 case DW_TAG_base_type:
21619 case DW_TAG_subrange_type:
21620 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21621 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21622 list_to_add = cu->list_in_scope;
21623 break;
21624 case DW_TAG_enumerator:
21625 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21626 if (attr)
21627 {
21628 dwarf2_const_value (attr, sym, cu);
21629 }
21630 {
21631 /* NOTE: carlton/2003-11-10: See comment above in the
21632 DW_TAG_class_type, etc. block. */
21633
21634 list_to_add = (cu->list_in_scope == &file_symbols
21635 && cu->language == language_cplus
21636 ? &global_symbols : cu->list_in_scope);
21637 }
21638 break;
21639 case DW_TAG_imported_declaration:
21640 case DW_TAG_namespace:
21641 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21642 list_to_add = &global_symbols;
21643 break;
21644 case DW_TAG_module:
21645 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21646 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21647 list_to_add = &global_symbols;
21648 break;
21649 case DW_TAG_common_block:
21650 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21651 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21652 add_symbol_to_list (sym, cu->list_in_scope);
21653 break;
21654 default:
21655 /* Not a tag we recognize. Hopefully we aren't processing
21656 trash data, but since we must specifically ignore things
21657 we don't recognize, there is nothing else we should do at
21658 this point. */
21659 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
21660 dwarf_tag_name (die->tag));
21661 break;
21662 }
21663
21664 if (suppress_add)
21665 {
21666 sym->hash_next = objfile->template_symbols;
21667 objfile->template_symbols = sym;
21668 list_to_add = NULL;
21669 }
21670
21671 if (list_to_add != NULL)
21672 add_symbol_to_list (sym, list_to_add);
21673
21674 /* For the benefit of old versions of GCC, check for anonymous
21675 namespaces based on the demangled name. */
21676 if (!cu->processing_has_namespace_info
21677 && cu->language == language_cplus)
21678 cp_scan_for_anonymous_namespaces (sym, objfile);
21679 }
21680 return (sym);
21681 }
21682
21683 /* Given an attr with a DW_FORM_dataN value in host byte order,
21684 zero-extend it as appropriate for the symbol's type. The DWARF
21685 standard (v4) is not entirely clear about the meaning of using
21686 DW_FORM_dataN for a constant with a signed type, where the type is
21687 wider than the data. The conclusion of a discussion on the DWARF
21688 list was that this is unspecified. We choose to always zero-extend
21689 because that is the interpretation long in use by GCC. */
21690
21691 static gdb_byte *
21692 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21693 struct dwarf2_cu *cu, LONGEST *value, int bits)
21694 {
21695 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21696 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21697 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21698 LONGEST l = DW_UNSND (attr);
21699
21700 if (bits < sizeof (*value) * 8)
21701 {
21702 l &= ((LONGEST) 1 << bits) - 1;
21703 *value = l;
21704 }
21705 else if (bits == sizeof (*value) * 8)
21706 *value = l;
21707 else
21708 {
21709 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21710 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21711 return bytes;
21712 }
21713
21714 return NULL;
21715 }
21716
21717 /* Read a constant value from an attribute. Either set *VALUE, or if
21718 the value does not fit in *VALUE, set *BYTES - either already
21719 allocated on the objfile obstack, or newly allocated on OBSTACK,
21720 or, set *BATON, if we translated the constant to a location
21721 expression. */
21722
21723 static void
21724 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21725 const char *name, struct obstack *obstack,
21726 struct dwarf2_cu *cu,
21727 LONGEST *value, const gdb_byte **bytes,
21728 struct dwarf2_locexpr_baton **baton)
21729 {
21730 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21731 struct comp_unit_head *cu_header = &cu->header;
21732 struct dwarf_block *blk;
21733 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21734 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21735
21736 *value = 0;
21737 *bytes = NULL;
21738 *baton = NULL;
21739
21740 switch (attr->form)
21741 {
21742 case DW_FORM_addr:
21743 case DW_FORM_GNU_addr_index:
21744 {
21745 gdb_byte *data;
21746
21747 if (TYPE_LENGTH (type) != cu_header->addr_size)
21748 dwarf2_const_value_length_mismatch_complaint (name,
21749 cu_header->addr_size,
21750 TYPE_LENGTH (type));
21751 /* Symbols of this form are reasonably rare, so we just
21752 piggyback on the existing location code rather than writing
21753 a new implementation of symbol_computed_ops. */
21754 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21755 (*baton)->per_cu = cu->per_cu;
21756 gdb_assert ((*baton)->per_cu);
21757
21758 (*baton)->size = 2 + cu_header->addr_size;
21759 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21760 (*baton)->data = data;
21761
21762 data[0] = DW_OP_addr;
21763 store_unsigned_integer (&data[1], cu_header->addr_size,
21764 byte_order, DW_ADDR (attr));
21765 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21766 }
21767 break;
21768 case DW_FORM_string:
21769 case DW_FORM_strp:
21770 case DW_FORM_GNU_str_index:
21771 case DW_FORM_GNU_strp_alt:
21772 /* DW_STRING is already allocated on the objfile obstack, point
21773 directly to it. */
21774 *bytes = (const gdb_byte *) DW_STRING (attr);
21775 break;
21776 case DW_FORM_block1:
21777 case DW_FORM_block2:
21778 case DW_FORM_block4:
21779 case DW_FORM_block:
21780 case DW_FORM_exprloc:
21781 case DW_FORM_data16:
21782 blk = DW_BLOCK (attr);
21783 if (TYPE_LENGTH (type) != blk->size)
21784 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21785 TYPE_LENGTH (type));
21786 *bytes = blk->data;
21787 break;
21788
21789 /* The DW_AT_const_value attributes are supposed to carry the
21790 symbol's value "represented as it would be on the target
21791 architecture." By the time we get here, it's already been
21792 converted to host endianness, so we just need to sign- or
21793 zero-extend it as appropriate. */
21794 case DW_FORM_data1:
21795 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21796 break;
21797 case DW_FORM_data2:
21798 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21799 break;
21800 case DW_FORM_data4:
21801 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21802 break;
21803 case DW_FORM_data8:
21804 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21805 break;
21806
21807 case DW_FORM_sdata:
21808 case DW_FORM_implicit_const:
21809 *value = DW_SND (attr);
21810 break;
21811
21812 case DW_FORM_udata:
21813 *value = DW_UNSND (attr);
21814 break;
21815
21816 default:
21817 complaint (&symfile_complaints,
21818 _("unsupported const value attribute form: '%s'"),
21819 dwarf_form_name (attr->form));
21820 *value = 0;
21821 break;
21822 }
21823 }
21824
21825
21826 /* Copy constant value from an attribute to a symbol. */
21827
21828 static void
21829 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21830 struct dwarf2_cu *cu)
21831 {
21832 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21833 LONGEST value;
21834 const gdb_byte *bytes;
21835 struct dwarf2_locexpr_baton *baton;
21836
21837 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21838 SYMBOL_PRINT_NAME (sym),
21839 &objfile->objfile_obstack, cu,
21840 &value, &bytes, &baton);
21841
21842 if (baton != NULL)
21843 {
21844 SYMBOL_LOCATION_BATON (sym) = baton;
21845 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21846 }
21847 else if (bytes != NULL)
21848 {
21849 SYMBOL_VALUE_BYTES (sym) = bytes;
21850 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21851 }
21852 else
21853 {
21854 SYMBOL_VALUE (sym) = value;
21855 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21856 }
21857 }
21858
21859 /* Return the type of the die in question using its DW_AT_type attribute. */
21860
21861 static struct type *
21862 die_type (struct die_info *die, struct dwarf2_cu *cu)
21863 {
21864 struct attribute *type_attr;
21865
21866 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21867 if (!type_attr)
21868 {
21869 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21870 /* A missing DW_AT_type represents a void type. */
21871 return objfile_type (objfile)->builtin_void;
21872 }
21873
21874 return lookup_die_type (die, type_attr, cu);
21875 }
21876
21877 /* True iff CU's producer generates GNAT Ada auxiliary information
21878 that allows to find parallel types through that information instead
21879 of having to do expensive parallel lookups by type name. */
21880
21881 static int
21882 need_gnat_info (struct dwarf2_cu *cu)
21883 {
21884 /* Assume that the Ada compiler was GNAT, which always produces
21885 the auxiliary information. */
21886 return (cu->language == language_ada);
21887 }
21888
21889 /* Return the auxiliary type of the die in question using its
21890 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21891 attribute is not present. */
21892
21893 static struct type *
21894 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21895 {
21896 struct attribute *type_attr;
21897
21898 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21899 if (!type_attr)
21900 return NULL;
21901
21902 return lookup_die_type (die, type_attr, cu);
21903 }
21904
21905 /* If DIE has a descriptive_type attribute, then set the TYPE's
21906 descriptive type accordingly. */
21907
21908 static void
21909 set_descriptive_type (struct type *type, struct die_info *die,
21910 struct dwarf2_cu *cu)
21911 {
21912 struct type *descriptive_type = die_descriptive_type (die, cu);
21913
21914 if (descriptive_type)
21915 {
21916 ALLOCATE_GNAT_AUX_TYPE (type);
21917 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21918 }
21919 }
21920
21921 /* Return the containing type of the die in question using its
21922 DW_AT_containing_type attribute. */
21923
21924 static struct type *
21925 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21926 {
21927 struct attribute *type_attr;
21928 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21929
21930 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21931 if (!type_attr)
21932 error (_("Dwarf Error: Problem turning containing type into gdb type "
21933 "[in module %s]"), objfile_name (objfile));
21934
21935 return lookup_die_type (die, type_attr, cu);
21936 }
21937
21938 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21939
21940 static struct type *
21941 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21942 {
21943 struct dwarf2_per_objfile *dwarf2_per_objfile
21944 = cu->per_cu->dwarf2_per_objfile;
21945 struct objfile *objfile = dwarf2_per_objfile->objfile;
21946 char *message, *saved;
21947
21948 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
21949 objfile_name (objfile),
21950 sect_offset_str (cu->header.sect_off),
21951 sect_offset_str (die->sect_off));
21952 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21953 message, strlen (message));
21954 xfree (message);
21955
21956 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21957 }
21958
21959 /* Look up the type of DIE in CU using its type attribute ATTR.
21960 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21961 DW_AT_containing_type.
21962 If there is no type substitute an error marker. */
21963
21964 static struct type *
21965 lookup_die_type (struct die_info *die, const struct attribute *attr,
21966 struct dwarf2_cu *cu)
21967 {
21968 struct dwarf2_per_objfile *dwarf2_per_objfile
21969 = cu->per_cu->dwarf2_per_objfile;
21970 struct objfile *objfile = dwarf2_per_objfile->objfile;
21971 struct type *this_type;
21972
21973 gdb_assert (attr->name == DW_AT_type
21974 || attr->name == DW_AT_GNAT_descriptive_type
21975 || attr->name == DW_AT_containing_type);
21976
21977 /* First see if we have it cached. */
21978
21979 if (attr->form == DW_FORM_GNU_ref_alt)
21980 {
21981 struct dwarf2_per_cu_data *per_cu;
21982 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21983
21984 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21985 dwarf2_per_objfile);
21986 this_type = get_die_type_at_offset (sect_off, per_cu);
21987 }
21988 else if (attr_form_is_ref (attr))
21989 {
21990 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21991
21992 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21993 }
21994 else if (attr->form == DW_FORM_ref_sig8)
21995 {
21996 ULONGEST signature = DW_SIGNATURE (attr);
21997
21998 return get_signatured_type (die, signature, cu);
21999 }
22000 else
22001 {
22002 complaint (&symfile_complaints,
22003 _("Dwarf Error: Bad type attribute %s in DIE"
22004 " at %s [in module %s]"),
22005 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22006 objfile_name (objfile));
22007 return build_error_marker_type (cu, die);
22008 }
22009
22010 /* If not cached we need to read it in. */
22011
22012 if (this_type == NULL)
22013 {
22014 struct die_info *type_die = NULL;
22015 struct dwarf2_cu *type_cu = cu;
22016
22017 if (attr_form_is_ref (attr))
22018 type_die = follow_die_ref (die, attr, &type_cu);
22019 if (type_die == NULL)
22020 return build_error_marker_type (cu, die);
22021 /* If we find the type now, it's probably because the type came
22022 from an inter-CU reference and the type's CU got expanded before
22023 ours. */
22024 this_type = read_type_die (type_die, type_cu);
22025 }
22026
22027 /* If we still don't have a type use an error marker. */
22028
22029 if (this_type == NULL)
22030 return build_error_marker_type (cu, die);
22031
22032 return this_type;
22033 }
22034
22035 /* Return the type in DIE, CU.
22036 Returns NULL for invalid types.
22037
22038 This first does a lookup in die_type_hash,
22039 and only reads the die in if necessary.
22040
22041 NOTE: This can be called when reading in partial or full symbols. */
22042
22043 static struct type *
22044 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22045 {
22046 struct type *this_type;
22047
22048 this_type = get_die_type (die, cu);
22049 if (this_type)
22050 return this_type;
22051
22052 return read_type_die_1 (die, cu);
22053 }
22054
22055 /* Read the type in DIE, CU.
22056 Returns NULL for invalid types. */
22057
22058 static struct type *
22059 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22060 {
22061 struct type *this_type = NULL;
22062
22063 switch (die->tag)
22064 {
22065 case DW_TAG_class_type:
22066 case DW_TAG_interface_type:
22067 case DW_TAG_structure_type:
22068 case DW_TAG_union_type:
22069 this_type = read_structure_type (die, cu);
22070 break;
22071 case DW_TAG_enumeration_type:
22072 this_type = read_enumeration_type (die, cu);
22073 break;
22074 case DW_TAG_subprogram:
22075 case DW_TAG_subroutine_type:
22076 case DW_TAG_inlined_subroutine:
22077 this_type = read_subroutine_type (die, cu);
22078 break;
22079 case DW_TAG_array_type:
22080 this_type = read_array_type (die, cu);
22081 break;
22082 case DW_TAG_set_type:
22083 this_type = read_set_type (die, cu);
22084 break;
22085 case DW_TAG_pointer_type:
22086 this_type = read_tag_pointer_type (die, cu);
22087 break;
22088 case DW_TAG_ptr_to_member_type:
22089 this_type = read_tag_ptr_to_member_type (die, cu);
22090 break;
22091 case DW_TAG_reference_type:
22092 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22093 break;
22094 case DW_TAG_rvalue_reference_type:
22095 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22096 break;
22097 case DW_TAG_const_type:
22098 this_type = read_tag_const_type (die, cu);
22099 break;
22100 case DW_TAG_volatile_type:
22101 this_type = read_tag_volatile_type (die, cu);
22102 break;
22103 case DW_TAG_restrict_type:
22104 this_type = read_tag_restrict_type (die, cu);
22105 break;
22106 case DW_TAG_string_type:
22107 this_type = read_tag_string_type (die, cu);
22108 break;
22109 case DW_TAG_typedef:
22110 this_type = read_typedef (die, cu);
22111 break;
22112 case DW_TAG_subrange_type:
22113 this_type = read_subrange_type (die, cu);
22114 break;
22115 case DW_TAG_base_type:
22116 this_type = read_base_type (die, cu);
22117 break;
22118 case DW_TAG_unspecified_type:
22119 this_type = read_unspecified_type (die, cu);
22120 break;
22121 case DW_TAG_namespace:
22122 this_type = read_namespace_type (die, cu);
22123 break;
22124 case DW_TAG_module:
22125 this_type = read_module_type (die, cu);
22126 break;
22127 case DW_TAG_atomic_type:
22128 this_type = read_tag_atomic_type (die, cu);
22129 break;
22130 default:
22131 complaint (&symfile_complaints,
22132 _("unexpected tag in read_type_die: '%s'"),
22133 dwarf_tag_name (die->tag));
22134 break;
22135 }
22136
22137 return this_type;
22138 }
22139
22140 /* See if we can figure out if the class lives in a namespace. We do
22141 this by looking for a member function; its demangled name will
22142 contain namespace info, if there is any.
22143 Return the computed name or NULL.
22144 Space for the result is allocated on the objfile's obstack.
22145 This is the full-die version of guess_partial_die_structure_name.
22146 In this case we know DIE has no useful parent. */
22147
22148 static char *
22149 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22150 {
22151 struct die_info *spec_die;
22152 struct dwarf2_cu *spec_cu;
22153 struct die_info *child;
22154 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22155
22156 spec_cu = cu;
22157 spec_die = die_specification (die, &spec_cu);
22158 if (spec_die != NULL)
22159 {
22160 die = spec_die;
22161 cu = spec_cu;
22162 }
22163
22164 for (child = die->child;
22165 child != NULL;
22166 child = child->sibling)
22167 {
22168 if (child->tag == DW_TAG_subprogram)
22169 {
22170 const char *linkage_name = dw2_linkage_name (child, cu);
22171
22172 if (linkage_name != NULL)
22173 {
22174 char *actual_name
22175 = language_class_name_from_physname (cu->language_defn,
22176 linkage_name);
22177 char *name = NULL;
22178
22179 if (actual_name != NULL)
22180 {
22181 const char *die_name = dwarf2_name (die, cu);
22182
22183 if (die_name != NULL
22184 && strcmp (die_name, actual_name) != 0)
22185 {
22186 /* Strip off the class name from the full name.
22187 We want the prefix. */
22188 int die_name_len = strlen (die_name);
22189 int actual_name_len = strlen (actual_name);
22190
22191 /* Test for '::' as a sanity check. */
22192 if (actual_name_len > die_name_len + 2
22193 && actual_name[actual_name_len
22194 - die_name_len - 1] == ':')
22195 name = (char *) obstack_copy0 (
22196 &objfile->per_bfd->storage_obstack,
22197 actual_name, actual_name_len - die_name_len - 2);
22198 }
22199 }
22200 xfree (actual_name);
22201 return name;
22202 }
22203 }
22204 }
22205
22206 return NULL;
22207 }
22208
22209 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22210 prefix part in such case. See
22211 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22212
22213 static const char *
22214 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22215 {
22216 struct attribute *attr;
22217 const char *base;
22218
22219 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22220 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22221 return NULL;
22222
22223 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22224 return NULL;
22225
22226 attr = dw2_linkage_name_attr (die, cu);
22227 if (attr == NULL || DW_STRING (attr) == NULL)
22228 return NULL;
22229
22230 /* dwarf2_name had to be already called. */
22231 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22232
22233 /* Strip the base name, keep any leading namespaces/classes. */
22234 base = strrchr (DW_STRING (attr), ':');
22235 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22236 return "";
22237
22238 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22239 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22240 DW_STRING (attr),
22241 &base[-1] - DW_STRING (attr));
22242 }
22243
22244 /* Return the name of the namespace/class that DIE is defined within,
22245 or "" if we can't tell. The caller should not xfree the result.
22246
22247 For example, if we're within the method foo() in the following
22248 code:
22249
22250 namespace N {
22251 class C {
22252 void foo () {
22253 }
22254 };
22255 }
22256
22257 then determine_prefix on foo's die will return "N::C". */
22258
22259 static const char *
22260 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22261 {
22262 struct dwarf2_per_objfile *dwarf2_per_objfile
22263 = cu->per_cu->dwarf2_per_objfile;
22264 struct die_info *parent, *spec_die;
22265 struct dwarf2_cu *spec_cu;
22266 struct type *parent_type;
22267 const char *retval;
22268
22269 if (cu->language != language_cplus
22270 && cu->language != language_fortran && cu->language != language_d
22271 && cu->language != language_rust)
22272 return "";
22273
22274 retval = anonymous_struct_prefix (die, cu);
22275 if (retval)
22276 return retval;
22277
22278 /* We have to be careful in the presence of DW_AT_specification.
22279 For example, with GCC 3.4, given the code
22280
22281 namespace N {
22282 void foo() {
22283 // Definition of N::foo.
22284 }
22285 }
22286
22287 then we'll have a tree of DIEs like this:
22288
22289 1: DW_TAG_compile_unit
22290 2: DW_TAG_namespace // N
22291 3: DW_TAG_subprogram // declaration of N::foo
22292 4: DW_TAG_subprogram // definition of N::foo
22293 DW_AT_specification // refers to die #3
22294
22295 Thus, when processing die #4, we have to pretend that we're in
22296 the context of its DW_AT_specification, namely the contex of die
22297 #3. */
22298 spec_cu = cu;
22299 spec_die = die_specification (die, &spec_cu);
22300 if (spec_die == NULL)
22301 parent = die->parent;
22302 else
22303 {
22304 parent = spec_die->parent;
22305 cu = spec_cu;
22306 }
22307
22308 if (parent == NULL)
22309 return "";
22310 else if (parent->building_fullname)
22311 {
22312 const char *name;
22313 const char *parent_name;
22314
22315 /* It has been seen on RealView 2.2 built binaries,
22316 DW_TAG_template_type_param types actually _defined_ as
22317 children of the parent class:
22318
22319 enum E {};
22320 template class <class Enum> Class{};
22321 Class<enum E> class_e;
22322
22323 1: DW_TAG_class_type (Class)
22324 2: DW_TAG_enumeration_type (E)
22325 3: DW_TAG_enumerator (enum1:0)
22326 3: DW_TAG_enumerator (enum2:1)
22327 ...
22328 2: DW_TAG_template_type_param
22329 DW_AT_type DW_FORM_ref_udata (E)
22330
22331 Besides being broken debug info, it can put GDB into an
22332 infinite loop. Consider:
22333
22334 When we're building the full name for Class<E>, we'll start
22335 at Class, and go look over its template type parameters,
22336 finding E. We'll then try to build the full name of E, and
22337 reach here. We're now trying to build the full name of E,
22338 and look over the parent DIE for containing scope. In the
22339 broken case, if we followed the parent DIE of E, we'd again
22340 find Class, and once again go look at its template type
22341 arguments, etc., etc. Simply don't consider such parent die
22342 as source-level parent of this die (it can't be, the language
22343 doesn't allow it), and break the loop here. */
22344 name = dwarf2_name (die, cu);
22345 parent_name = dwarf2_name (parent, cu);
22346 complaint (&symfile_complaints,
22347 _("template param type '%s' defined within parent '%s'"),
22348 name ? name : "<unknown>",
22349 parent_name ? parent_name : "<unknown>");
22350 return "";
22351 }
22352 else
22353 switch (parent->tag)
22354 {
22355 case DW_TAG_namespace:
22356 parent_type = read_type_die (parent, cu);
22357 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22358 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22359 Work around this problem here. */
22360 if (cu->language == language_cplus
22361 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22362 return "";
22363 /* We give a name to even anonymous namespaces. */
22364 return TYPE_TAG_NAME (parent_type);
22365 case DW_TAG_class_type:
22366 case DW_TAG_interface_type:
22367 case DW_TAG_structure_type:
22368 case DW_TAG_union_type:
22369 case DW_TAG_module:
22370 parent_type = read_type_die (parent, cu);
22371 if (TYPE_TAG_NAME (parent_type) != NULL)
22372 return TYPE_TAG_NAME (parent_type);
22373 else
22374 /* An anonymous structure is only allowed non-static data
22375 members; no typedefs, no member functions, et cetera.
22376 So it does not need a prefix. */
22377 return "";
22378 case DW_TAG_compile_unit:
22379 case DW_TAG_partial_unit:
22380 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22381 if (cu->language == language_cplus
22382 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22383 && die->child != NULL
22384 && (die->tag == DW_TAG_class_type
22385 || die->tag == DW_TAG_structure_type
22386 || die->tag == DW_TAG_union_type))
22387 {
22388 char *name = guess_full_die_structure_name (die, cu);
22389 if (name != NULL)
22390 return name;
22391 }
22392 return "";
22393 case DW_TAG_enumeration_type:
22394 parent_type = read_type_die (parent, cu);
22395 if (TYPE_DECLARED_CLASS (parent_type))
22396 {
22397 if (TYPE_TAG_NAME (parent_type) != NULL)
22398 return TYPE_TAG_NAME (parent_type);
22399 return "";
22400 }
22401 /* Fall through. */
22402 default:
22403 return determine_prefix (parent, cu);
22404 }
22405 }
22406
22407 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22408 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22409 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22410 an obconcat, otherwise allocate storage for the result. The CU argument is
22411 used to determine the language and hence, the appropriate separator. */
22412
22413 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22414
22415 static char *
22416 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22417 int physname, struct dwarf2_cu *cu)
22418 {
22419 const char *lead = "";
22420 const char *sep;
22421
22422 if (suffix == NULL || suffix[0] == '\0'
22423 || prefix == NULL || prefix[0] == '\0')
22424 sep = "";
22425 else if (cu->language == language_d)
22426 {
22427 /* For D, the 'main' function could be defined in any module, but it
22428 should never be prefixed. */
22429 if (strcmp (suffix, "D main") == 0)
22430 {
22431 prefix = "";
22432 sep = "";
22433 }
22434 else
22435 sep = ".";
22436 }
22437 else if (cu->language == language_fortran && physname)
22438 {
22439 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22440 DW_AT_MIPS_linkage_name is preferred and used instead. */
22441
22442 lead = "__";
22443 sep = "_MOD_";
22444 }
22445 else
22446 sep = "::";
22447
22448 if (prefix == NULL)
22449 prefix = "";
22450 if (suffix == NULL)
22451 suffix = "";
22452
22453 if (obs == NULL)
22454 {
22455 char *retval
22456 = ((char *)
22457 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22458
22459 strcpy (retval, lead);
22460 strcat (retval, prefix);
22461 strcat (retval, sep);
22462 strcat (retval, suffix);
22463 return retval;
22464 }
22465 else
22466 {
22467 /* We have an obstack. */
22468 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22469 }
22470 }
22471
22472 /* Return sibling of die, NULL if no sibling. */
22473
22474 static struct die_info *
22475 sibling_die (struct die_info *die)
22476 {
22477 return die->sibling;
22478 }
22479
22480 /* Get name of a die, return NULL if not found. */
22481
22482 static const char *
22483 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22484 struct obstack *obstack)
22485 {
22486 if (name && cu->language == language_cplus)
22487 {
22488 std::string canon_name = cp_canonicalize_string (name);
22489
22490 if (!canon_name.empty ())
22491 {
22492 if (canon_name != name)
22493 name = (const char *) obstack_copy0 (obstack,
22494 canon_name.c_str (),
22495 canon_name.length ());
22496 }
22497 }
22498
22499 return name;
22500 }
22501
22502 /* Get name of a die, return NULL if not found.
22503 Anonymous namespaces are converted to their magic string. */
22504
22505 static const char *
22506 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22507 {
22508 struct attribute *attr;
22509 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22510
22511 attr = dwarf2_attr (die, DW_AT_name, cu);
22512 if ((!attr || !DW_STRING (attr))
22513 && die->tag != DW_TAG_namespace
22514 && die->tag != DW_TAG_class_type
22515 && die->tag != DW_TAG_interface_type
22516 && die->tag != DW_TAG_structure_type
22517 && die->tag != DW_TAG_union_type)
22518 return NULL;
22519
22520 switch (die->tag)
22521 {
22522 case DW_TAG_compile_unit:
22523 case DW_TAG_partial_unit:
22524 /* Compilation units have a DW_AT_name that is a filename, not
22525 a source language identifier. */
22526 case DW_TAG_enumeration_type:
22527 case DW_TAG_enumerator:
22528 /* These tags always have simple identifiers already; no need
22529 to canonicalize them. */
22530 return DW_STRING (attr);
22531
22532 case DW_TAG_namespace:
22533 if (attr != NULL && DW_STRING (attr) != NULL)
22534 return DW_STRING (attr);
22535 return CP_ANONYMOUS_NAMESPACE_STR;
22536
22537 case DW_TAG_class_type:
22538 case DW_TAG_interface_type:
22539 case DW_TAG_structure_type:
22540 case DW_TAG_union_type:
22541 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22542 structures or unions. These were of the form "._%d" in GCC 4.1,
22543 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22544 and GCC 4.4. We work around this problem by ignoring these. */
22545 if (attr && DW_STRING (attr)
22546 && (startswith (DW_STRING (attr), "._")
22547 || startswith (DW_STRING (attr), "<anonymous")))
22548 return NULL;
22549
22550 /* GCC might emit a nameless typedef that has a linkage name. See
22551 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22552 if (!attr || DW_STRING (attr) == NULL)
22553 {
22554 char *demangled = NULL;
22555
22556 attr = dw2_linkage_name_attr (die, cu);
22557 if (attr == NULL || DW_STRING (attr) == NULL)
22558 return NULL;
22559
22560 /* Avoid demangling DW_STRING (attr) the second time on a second
22561 call for the same DIE. */
22562 if (!DW_STRING_IS_CANONICAL (attr))
22563 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22564
22565 if (demangled)
22566 {
22567 const char *base;
22568
22569 /* FIXME: we already did this for the partial symbol... */
22570 DW_STRING (attr)
22571 = ((const char *)
22572 obstack_copy0 (&objfile->per_bfd->storage_obstack,
22573 demangled, strlen (demangled)));
22574 DW_STRING_IS_CANONICAL (attr) = 1;
22575 xfree (demangled);
22576
22577 /* Strip any leading namespaces/classes, keep only the base name.
22578 DW_AT_name for named DIEs does not contain the prefixes. */
22579 base = strrchr (DW_STRING (attr), ':');
22580 if (base && base > DW_STRING (attr) && base[-1] == ':')
22581 return &base[1];
22582 else
22583 return DW_STRING (attr);
22584 }
22585 }
22586 break;
22587
22588 default:
22589 break;
22590 }
22591
22592 if (!DW_STRING_IS_CANONICAL (attr))
22593 {
22594 DW_STRING (attr)
22595 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22596 &objfile->per_bfd->storage_obstack);
22597 DW_STRING_IS_CANONICAL (attr) = 1;
22598 }
22599 return DW_STRING (attr);
22600 }
22601
22602 /* Return the die that this die in an extension of, or NULL if there
22603 is none. *EXT_CU is the CU containing DIE on input, and the CU
22604 containing the return value on output. */
22605
22606 static struct die_info *
22607 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22608 {
22609 struct attribute *attr;
22610
22611 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22612 if (attr == NULL)
22613 return NULL;
22614
22615 return follow_die_ref (die, attr, ext_cu);
22616 }
22617
22618 /* Convert a DIE tag into its string name. */
22619
22620 static const char *
22621 dwarf_tag_name (unsigned tag)
22622 {
22623 const char *name = get_DW_TAG_name (tag);
22624
22625 if (name == NULL)
22626 return "DW_TAG_<unknown>";
22627
22628 return name;
22629 }
22630
22631 /* Convert a DWARF attribute code into its string name. */
22632
22633 static const char *
22634 dwarf_attr_name (unsigned attr)
22635 {
22636 const char *name;
22637
22638 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22639 if (attr == DW_AT_MIPS_fde)
22640 return "DW_AT_MIPS_fde";
22641 #else
22642 if (attr == DW_AT_HP_block_index)
22643 return "DW_AT_HP_block_index";
22644 #endif
22645
22646 name = get_DW_AT_name (attr);
22647
22648 if (name == NULL)
22649 return "DW_AT_<unknown>";
22650
22651 return name;
22652 }
22653
22654 /* Convert a DWARF value form code into its string name. */
22655
22656 static const char *
22657 dwarf_form_name (unsigned form)
22658 {
22659 const char *name = get_DW_FORM_name (form);
22660
22661 if (name == NULL)
22662 return "DW_FORM_<unknown>";
22663
22664 return name;
22665 }
22666
22667 static const char *
22668 dwarf_bool_name (unsigned mybool)
22669 {
22670 if (mybool)
22671 return "TRUE";
22672 else
22673 return "FALSE";
22674 }
22675
22676 /* Convert a DWARF type code into its string name. */
22677
22678 static const char *
22679 dwarf_type_encoding_name (unsigned enc)
22680 {
22681 const char *name = get_DW_ATE_name (enc);
22682
22683 if (name == NULL)
22684 return "DW_ATE_<unknown>";
22685
22686 return name;
22687 }
22688
22689 static void
22690 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22691 {
22692 unsigned int i;
22693
22694 print_spaces (indent, f);
22695 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22696 dwarf_tag_name (die->tag), die->abbrev,
22697 sect_offset_str (die->sect_off));
22698
22699 if (die->parent != NULL)
22700 {
22701 print_spaces (indent, f);
22702 fprintf_unfiltered (f, " parent at offset: %s\n",
22703 sect_offset_str (die->parent->sect_off));
22704 }
22705
22706 print_spaces (indent, f);
22707 fprintf_unfiltered (f, " has children: %s\n",
22708 dwarf_bool_name (die->child != NULL));
22709
22710 print_spaces (indent, f);
22711 fprintf_unfiltered (f, " attributes:\n");
22712
22713 for (i = 0; i < die->num_attrs; ++i)
22714 {
22715 print_spaces (indent, f);
22716 fprintf_unfiltered (f, " %s (%s) ",
22717 dwarf_attr_name (die->attrs[i].name),
22718 dwarf_form_name (die->attrs[i].form));
22719
22720 switch (die->attrs[i].form)
22721 {
22722 case DW_FORM_addr:
22723 case DW_FORM_GNU_addr_index:
22724 fprintf_unfiltered (f, "address: ");
22725 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22726 break;
22727 case DW_FORM_block2:
22728 case DW_FORM_block4:
22729 case DW_FORM_block:
22730 case DW_FORM_block1:
22731 fprintf_unfiltered (f, "block: size %s",
22732 pulongest (DW_BLOCK (&die->attrs[i])->size));
22733 break;
22734 case DW_FORM_exprloc:
22735 fprintf_unfiltered (f, "expression: size %s",
22736 pulongest (DW_BLOCK (&die->attrs[i])->size));
22737 break;
22738 case DW_FORM_data16:
22739 fprintf_unfiltered (f, "constant of 16 bytes");
22740 break;
22741 case DW_FORM_ref_addr:
22742 fprintf_unfiltered (f, "ref address: ");
22743 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22744 break;
22745 case DW_FORM_GNU_ref_alt:
22746 fprintf_unfiltered (f, "alt ref address: ");
22747 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22748 break;
22749 case DW_FORM_ref1:
22750 case DW_FORM_ref2:
22751 case DW_FORM_ref4:
22752 case DW_FORM_ref8:
22753 case DW_FORM_ref_udata:
22754 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22755 (long) (DW_UNSND (&die->attrs[i])));
22756 break;
22757 case DW_FORM_data1:
22758 case DW_FORM_data2:
22759 case DW_FORM_data4:
22760 case DW_FORM_data8:
22761 case DW_FORM_udata:
22762 case DW_FORM_sdata:
22763 fprintf_unfiltered (f, "constant: %s",
22764 pulongest (DW_UNSND (&die->attrs[i])));
22765 break;
22766 case DW_FORM_sec_offset:
22767 fprintf_unfiltered (f, "section offset: %s",
22768 pulongest (DW_UNSND (&die->attrs[i])));
22769 break;
22770 case DW_FORM_ref_sig8:
22771 fprintf_unfiltered (f, "signature: %s",
22772 hex_string (DW_SIGNATURE (&die->attrs[i])));
22773 break;
22774 case DW_FORM_string:
22775 case DW_FORM_strp:
22776 case DW_FORM_line_strp:
22777 case DW_FORM_GNU_str_index:
22778 case DW_FORM_GNU_strp_alt:
22779 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22780 DW_STRING (&die->attrs[i])
22781 ? DW_STRING (&die->attrs[i]) : "",
22782 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22783 break;
22784 case DW_FORM_flag:
22785 if (DW_UNSND (&die->attrs[i]))
22786 fprintf_unfiltered (f, "flag: TRUE");
22787 else
22788 fprintf_unfiltered (f, "flag: FALSE");
22789 break;
22790 case DW_FORM_flag_present:
22791 fprintf_unfiltered (f, "flag: TRUE");
22792 break;
22793 case DW_FORM_indirect:
22794 /* The reader will have reduced the indirect form to
22795 the "base form" so this form should not occur. */
22796 fprintf_unfiltered (f,
22797 "unexpected attribute form: DW_FORM_indirect");
22798 break;
22799 case DW_FORM_implicit_const:
22800 fprintf_unfiltered (f, "constant: %s",
22801 plongest (DW_SND (&die->attrs[i])));
22802 break;
22803 default:
22804 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22805 die->attrs[i].form);
22806 break;
22807 }
22808 fprintf_unfiltered (f, "\n");
22809 }
22810 }
22811
22812 static void
22813 dump_die_for_error (struct die_info *die)
22814 {
22815 dump_die_shallow (gdb_stderr, 0, die);
22816 }
22817
22818 static void
22819 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22820 {
22821 int indent = level * 4;
22822
22823 gdb_assert (die != NULL);
22824
22825 if (level >= max_level)
22826 return;
22827
22828 dump_die_shallow (f, indent, die);
22829
22830 if (die->child != NULL)
22831 {
22832 print_spaces (indent, f);
22833 fprintf_unfiltered (f, " Children:");
22834 if (level + 1 < max_level)
22835 {
22836 fprintf_unfiltered (f, "\n");
22837 dump_die_1 (f, level + 1, max_level, die->child);
22838 }
22839 else
22840 {
22841 fprintf_unfiltered (f,
22842 " [not printed, max nesting level reached]\n");
22843 }
22844 }
22845
22846 if (die->sibling != NULL && level > 0)
22847 {
22848 dump_die_1 (f, level, max_level, die->sibling);
22849 }
22850 }
22851
22852 /* This is called from the pdie macro in gdbinit.in.
22853 It's not static so gcc will keep a copy callable from gdb. */
22854
22855 void
22856 dump_die (struct die_info *die, int max_level)
22857 {
22858 dump_die_1 (gdb_stdlog, 0, max_level, die);
22859 }
22860
22861 static void
22862 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22863 {
22864 void **slot;
22865
22866 slot = htab_find_slot_with_hash (cu->die_hash, die,
22867 to_underlying (die->sect_off),
22868 INSERT);
22869
22870 *slot = die;
22871 }
22872
22873 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22874 required kind. */
22875
22876 static sect_offset
22877 dwarf2_get_ref_die_offset (const struct attribute *attr)
22878 {
22879 if (attr_form_is_ref (attr))
22880 return (sect_offset) DW_UNSND (attr);
22881
22882 complaint (&symfile_complaints,
22883 _("unsupported die ref attribute form: '%s'"),
22884 dwarf_form_name (attr->form));
22885 return {};
22886 }
22887
22888 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22889 * the value held by the attribute is not constant. */
22890
22891 static LONGEST
22892 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22893 {
22894 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22895 return DW_SND (attr);
22896 else if (attr->form == DW_FORM_udata
22897 || attr->form == DW_FORM_data1
22898 || attr->form == DW_FORM_data2
22899 || attr->form == DW_FORM_data4
22900 || attr->form == DW_FORM_data8)
22901 return DW_UNSND (attr);
22902 else
22903 {
22904 /* For DW_FORM_data16 see attr_form_is_constant. */
22905 complaint (&symfile_complaints,
22906 _("Attribute value is not a constant (%s)"),
22907 dwarf_form_name (attr->form));
22908 return default_value;
22909 }
22910 }
22911
22912 /* Follow reference or signature attribute ATTR of SRC_DIE.
22913 On entry *REF_CU is the CU of SRC_DIE.
22914 On exit *REF_CU is the CU of the result. */
22915
22916 static struct die_info *
22917 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22918 struct dwarf2_cu **ref_cu)
22919 {
22920 struct die_info *die;
22921
22922 if (attr_form_is_ref (attr))
22923 die = follow_die_ref (src_die, attr, ref_cu);
22924 else if (attr->form == DW_FORM_ref_sig8)
22925 die = follow_die_sig (src_die, attr, ref_cu);
22926 else
22927 {
22928 dump_die_for_error (src_die);
22929 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22930 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22931 }
22932
22933 return die;
22934 }
22935
22936 /* Follow reference OFFSET.
22937 On entry *REF_CU is the CU of the source die referencing OFFSET.
22938 On exit *REF_CU is the CU of the result.
22939 Returns NULL if OFFSET is invalid. */
22940
22941 static struct die_info *
22942 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22943 struct dwarf2_cu **ref_cu)
22944 {
22945 struct die_info temp_die;
22946 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22947 struct dwarf2_per_objfile *dwarf2_per_objfile
22948 = cu->per_cu->dwarf2_per_objfile;
22949 struct objfile *objfile = dwarf2_per_objfile->objfile;
22950
22951 gdb_assert (cu->per_cu != NULL);
22952
22953 target_cu = cu;
22954
22955 if (cu->per_cu->is_debug_types)
22956 {
22957 /* .debug_types CUs cannot reference anything outside their CU.
22958 If they need to, they have to reference a signatured type via
22959 DW_FORM_ref_sig8. */
22960 if (!offset_in_cu_p (&cu->header, sect_off))
22961 return NULL;
22962 }
22963 else if (offset_in_dwz != cu->per_cu->is_dwz
22964 || !offset_in_cu_p (&cu->header, sect_off))
22965 {
22966 struct dwarf2_per_cu_data *per_cu;
22967
22968 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22969 dwarf2_per_objfile);
22970
22971 /* If necessary, add it to the queue and load its DIEs. */
22972 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22973 load_full_comp_unit (per_cu, cu->language);
22974
22975 target_cu = per_cu->cu;
22976 }
22977 else if (cu->dies == NULL)
22978 {
22979 /* We're loading full DIEs during partial symbol reading. */
22980 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22981 load_full_comp_unit (cu->per_cu, language_minimal);
22982 }
22983
22984 *ref_cu = target_cu;
22985 temp_die.sect_off = sect_off;
22986 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22987 &temp_die,
22988 to_underlying (sect_off));
22989 }
22990
22991 /* Follow reference attribute ATTR of SRC_DIE.
22992 On entry *REF_CU is the CU of SRC_DIE.
22993 On exit *REF_CU is the CU of the result. */
22994
22995 static struct die_info *
22996 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22997 struct dwarf2_cu **ref_cu)
22998 {
22999 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23000 struct dwarf2_cu *cu = *ref_cu;
23001 struct die_info *die;
23002
23003 die = follow_die_offset (sect_off,
23004 (attr->form == DW_FORM_GNU_ref_alt
23005 || cu->per_cu->is_dwz),
23006 ref_cu);
23007 if (!die)
23008 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23009 "at %s [in module %s]"),
23010 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23011 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23012
23013 return die;
23014 }
23015
23016 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23017 Returned value is intended for DW_OP_call*. Returned
23018 dwarf2_locexpr_baton->data has lifetime of
23019 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23020
23021 struct dwarf2_locexpr_baton
23022 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23023 struct dwarf2_per_cu_data *per_cu,
23024 CORE_ADDR (*get_frame_pc) (void *baton),
23025 void *baton)
23026 {
23027 struct dwarf2_cu *cu;
23028 struct die_info *die;
23029 struct attribute *attr;
23030 struct dwarf2_locexpr_baton retval;
23031 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23032 struct dwarf2_per_objfile *dwarf2_per_objfile
23033 = get_dwarf2_per_objfile (objfile);
23034
23035 if (per_cu->cu == NULL)
23036 load_cu (per_cu);
23037 cu = per_cu->cu;
23038 if (cu == NULL)
23039 {
23040 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23041 Instead just throw an error, not much else we can do. */
23042 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23043 sect_offset_str (sect_off), objfile_name (objfile));
23044 }
23045
23046 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23047 if (!die)
23048 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23049 sect_offset_str (sect_off), objfile_name (objfile));
23050
23051 attr = dwarf2_attr (die, DW_AT_location, cu);
23052 if (!attr)
23053 {
23054 /* DWARF: "If there is no such attribute, then there is no effect.".
23055 DATA is ignored if SIZE is 0. */
23056
23057 retval.data = NULL;
23058 retval.size = 0;
23059 }
23060 else if (attr_form_is_section_offset (attr))
23061 {
23062 struct dwarf2_loclist_baton loclist_baton;
23063 CORE_ADDR pc = (*get_frame_pc) (baton);
23064 size_t size;
23065
23066 fill_in_loclist_baton (cu, &loclist_baton, attr);
23067
23068 retval.data = dwarf2_find_location_expression (&loclist_baton,
23069 &size, pc);
23070 retval.size = size;
23071 }
23072 else
23073 {
23074 if (!attr_form_is_block (attr))
23075 error (_("Dwarf Error: DIE at %s referenced in module %s "
23076 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23077 sect_offset_str (sect_off), objfile_name (objfile));
23078
23079 retval.data = DW_BLOCK (attr)->data;
23080 retval.size = DW_BLOCK (attr)->size;
23081 }
23082 retval.per_cu = cu->per_cu;
23083
23084 age_cached_comp_units (dwarf2_per_objfile);
23085
23086 return retval;
23087 }
23088
23089 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23090 offset. */
23091
23092 struct dwarf2_locexpr_baton
23093 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23094 struct dwarf2_per_cu_data *per_cu,
23095 CORE_ADDR (*get_frame_pc) (void *baton),
23096 void *baton)
23097 {
23098 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23099
23100 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23101 }
23102
23103 /* Write a constant of a given type as target-ordered bytes into
23104 OBSTACK. */
23105
23106 static const gdb_byte *
23107 write_constant_as_bytes (struct obstack *obstack,
23108 enum bfd_endian byte_order,
23109 struct type *type,
23110 ULONGEST value,
23111 LONGEST *len)
23112 {
23113 gdb_byte *result;
23114
23115 *len = TYPE_LENGTH (type);
23116 result = (gdb_byte *) obstack_alloc (obstack, *len);
23117 store_unsigned_integer (result, *len, byte_order, value);
23118
23119 return result;
23120 }
23121
23122 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23123 pointer to the constant bytes and set LEN to the length of the
23124 data. If memory is needed, allocate it on OBSTACK. If the DIE
23125 does not have a DW_AT_const_value, return NULL. */
23126
23127 const gdb_byte *
23128 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23129 struct dwarf2_per_cu_data *per_cu,
23130 struct obstack *obstack,
23131 LONGEST *len)
23132 {
23133 struct dwarf2_cu *cu;
23134 struct die_info *die;
23135 struct attribute *attr;
23136 const gdb_byte *result = NULL;
23137 struct type *type;
23138 LONGEST value;
23139 enum bfd_endian byte_order;
23140 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23141
23142 if (per_cu->cu == NULL)
23143 load_cu (per_cu);
23144 cu = per_cu->cu;
23145 if (cu == NULL)
23146 {
23147 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23148 Instead just throw an error, not much else we can do. */
23149 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23150 sect_offset_str (sect_off), objfile_name (objfile));
23151 }
23152
23153 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23154 if (!die)
23155 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23156 sect_offset_str (sect_off), objfile_name (objfile));
23157
23158 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23159 if (attr == NULL)
23160 return NULL;
23161
23162 byte_order = (bfd_big_endian (objfile->obfd)
23163 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23164
23165 switch (attr->form)
23166 {
23167 case DW_FORM_addr:
23168 case DW_FORM_GNU_addr_index:
23169 {
23170 gdb_byte *tem;
23171
23172 *len = cu->header.addr_size;
23173 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23174 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23175 result = tem;
23176 }
23177 break;
23178 case DW_FORM_string:
23179 case DW_FORM_strp:
23180 case DW_FORM_GNU_str_index:
23181 case DW_FORM_GNU_strp_alt:
23182 /* DW_STRING is already allocated on the objfile obstack, point
23183 directly to it. */
23184 result = (const gdb_byte *) DW_STRING (attr);
23185 *len = strlen (DW_STRING (attr));
23186 break;
23187 case DW_FORM_block1:
23188 case DW_FORM_block2:
23189 case DW_FORM_block4:
23190 case DW_FORM_block:
23191 case DW_FORM_exprloc:
23192 case DW_FORM_data16:
23193 result = DW_BLOCK (attr)->data;
23194 *len = DW_BLOCK (attr)->size;
23195 break;
23196
23197 /* The DW_AT_const_value attributes are supposed to carry the
23198 symbol's value "represented as it would be on the target
23199 architecture." By the time we get here, it's already been
23200 converted to host endianness, so we just need to sign- or
23201 zero-extend it as appropriate. */
23202 case DW_FORM_data1:
23203 type = die_type (die, cu);
23204 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23205 if (result == NULL)
23206 result = write_constant_as_bytes (obstack, byte_order,
23207 type, value, len);
23208 break;
23209 case DW_FORM_data2:
23210 type = die_type (die, cu);
23211 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23212 if (result == NULL)
23213 result = write_constant_as_bytes (obstack, byte_order,
23214 type, value, len);
23215 break;
23216 case DW_FORM_data4:
23217 type = die_type (die, cu);
23218 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23219 if (result == NULL)
23220 result = write_constant_as_bytes (obstack, byte_order,
23221 type, value, len);
23222 break;
23223 case DW_FORM_data8:
23224 type = die_type (die, cu);
23225 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23226 if (result == NULL)
23227 result = write_constant_as_bytes (obstack, byte_order,
23228 type, value, len);
23229 break;
23230
23231 case DW_FORM_sdata:
23232 case DW_FORM_implicit_const:
23233 type = die_type (die, cu);
23234 result = write_constant_as_bytes (obstack, byte_order,
23235 type, DW_SND (attr), len);
23236 break;
23237
23238 case DW_FORM_udata:
23239 type = die_type (die, cu);
23240 result = write_constant_as_bytes (obstack, byte_order,
23241 type, DW_UNSND (attr), len);
23242 break;
23243
23244 default:
23245 complaint (&symfile_complaints,
23246 _("unsupported const value attribute form: '%s'"),
23247 dwarf_form_name (attr->form));
23248 break;
23249 }
23250
23251 return result;
23252 }
23253
23254 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23255 valid type for this die is found. */
23256
23257 struct type *
23258 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23259 struct dwarf2_per_cu_data *per_cu)
23260 {
23261 struct dwarf2_cu *cu;
23262 struct die_info *die;
23263
23264 if (per_cu->cu == NULL)
23265 load_cu (per_cu);
23266 cu = per_cu->cu;
23267 if (!cu)
23268 return NULL;
23269
23270 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23271 if (!die)
23272 return NULL;
23273
23274 return die_type (die, cu);
23275 }
23276
23277 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23278 PER_CU. */
23279
23280 struct type *
23281 dwarf2_get_die_type (cu_offset die_offset,
23282 struct dwarf2_per_cu_data *per_cu)
23283 {
23284 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23285 return get_die_type_at_offset (die_offset_sect, per_cu);
23286 }
23287
23288 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23289 On entry *REF_CU is the CU of SRC_DIE.
23290 On exit *REF_CU is the CU of the result.
23291 Returns NULL if the referenced DIE isn't found. */
23292
23293 static struct die_info *
23294 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23295 struct dwarf2_cu **ref_cu)
23296 {
23297 struct die_info temp_die;
23298 struct dwarf2_cu *sig_cu;
23299 struct die_info *die;
23300
23301 /* While it might be nice to assert sig_type->type == NULL here,
23302 we can get here for DW_AT_imported_declaration where we need
23303 the DIE not the type. */
23304
23305 /* If necessary, add it to the queue and load its DIEs. */
23306
23307 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23308 read_signatured_type (sig_type);
23309
23310 sig_cu = sig_type->per_cu.cu;
23311 gdb_assert (sig_cu != NULL);
23312 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23313 temp_die.sect_off = sig_type->type_offset_in_section;
23314 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23315 to_underlying (temp_die.sect_off));
23316 if (die)
23317 {
23318 struct dwarf2_per_objfile *dwarf2_per_objfile
23319 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23320
23321 /* For .gdb_index version 7 keep track of included TUs.
23322 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23323 if (dwarf2_per_objfile->index_table != NULL
23324 && dwarf2_per_objfile->index_table->version <= 7)
23325 {
23326 VEC_safe_push (dwarf2_per_cu_ptr,
23327 (*ref_cu)->per_cu->imported_symtabs,
23328 sig_cu->per_cu);
23329 }
23330
23331 *ref_cu = sig_cu;
23332 return die;
23333 }
23334
23335 return NULL;
23336 }
23337
23338 /* Follow signatured type referenced by ATTR in SRC_DIE.
23339 On entry *REF_CU is the CU of SRC_DIE.
23340 On exit *REF_CU is the CU of the result.
23341 The result is the DIE of the type.
23342 If the referenced type cannot be found an error is thrown. */
23343
23344 static struct die_info *
23345 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23346 struct dwarf2_cu **ref_cu)
23347 {
23348 ULONGEST signature = DW_SIGNATURE (attr);
23349 struct signatured_type *sig_type;
23350 struct die_info *die;
23351
23352 gdb_assert (attr->form == DW_FORM_ref_sig8);
23353
23354 sig_type = lookup_signatured_type (*ref_cu, signature);
23355 /* sig_type will be NULL if the signatured type is missing from
23356 the debug info. */
23357 if (sig_type == NULL)
23358 {
23359 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23360 " from DIE at %s [in module %s]"),
23361 hex_string (signature), sect_offset_str (src_die->sect_off),
23362 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23363 }
23364
23365 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23366 if (die == NULL)
23367 {
23368 dump_die_for_error (src_die);
23369 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23370 " from DIE at %s [in module %s]"),
23371 hex_string (signature), sect_offset_str (src_die->sect_off),
23372 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23373 }
23374
23375 return die;
23376 }
23377
23378 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23379 reading in and processing the type unit if necessary. */
23380
23381 static struct type *
23382 get_signatured_type (struct die_info *die, ULONGEST signature,
23383 struct dwarf2_cu *cu)
23384 {
23385 struct dwarf2_per_objfile *dwarf2_per_objfile
23386 = cu->per_cu->dwarf2_per_objfile;
23387 struct signatured_type *sig_type;
23388 struct dwarf2_cu *type_cu;
23389 struct die_info *type_die;
23390 struct type *type;
23391
23392 sig_type = lookup_signatured_type (cu, signature);
23393 /* sig_type will be NULL if the signatured type is missing from
23394 the debug info. */
23395 if (sig_type == NULL)
23396 {
23397 complaint (&symfile_complaints,
23398 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23399 " from DIE at %s [in module %s]"),
23400 hex_string (signature), sect_offset_str (die->sect_off),
23401 objfile_name (dwarf2_per_objfile->objfile));
23402 return build_error_marker_type (cu, die);
23403 }
23404
23405 /* If we already know the type we're done. */
23406 if (sig_type->type != NULL)
23407 return sig_type->type;
23408
23409 type_cu = cu;
23410 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23411 if (type_die != NULL)
23412 {
23413 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23414 is created. This is important, for example, because for c++ classes
23415 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23416 type = read_type_die (type_die, type_cu);
23417 if (type == NULL)
23418 {
23419 complaint (&symfile_complaints,
23420 _("Dwarf Error: Cannot build signatured type %s"
23421 " referenced from DIE at %s [in module %s]"),
23422 hex_string (signature), sect_offset_str (die->sect_off),
23423 objfile_name (dwarf2_per_objfile->objfile));
23424 type = build_error_marker_type (cu, die);
23425 }
23426 }
23427 else
23428 {
23429 complaint (&symfile_complaints,
23430 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23431 " from DIE at %s [in module %s]"),
23432 hex_string (signature), sect_offset_str (die->sect_off),
23433 objfile_name (dwarf2_per_objfile->objfile));
23434 type = build_error_marker_type (cu, die);
23435 }
23436 sig_type->type = type;
23437
23438 return type;
23439 }
23440
23441 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23442 reading in and processing the type unit if necessary. */
23443
23444 static struct type *
23445 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23446 struct dwarf2_cu *cu) /* ARI: editCase function */
23447 {
23448 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23449 if (attr_form_is_ref (attr))
23450 {
23451 struct dwarf2_cu *type_cu = cu;
23452 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23453
23454 return read_type_die (type_die, type_cu);
23455 }
23456 else if (attr->form == DW_FORM_ref_sig8)
23457 {
23458 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23459 }
23460 else
23461 {
23462 struct dwarf2_per_objfile *dwarf2_per_objfile
23463 = cu->per_cu->dwarf2_per_objfile;
23464
23465 complaint (&symfile_complaints,
23466 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23467 " at %s [in module %s]"),
23468 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23469 objfile_name (dwarf2_per_objfile->objfile));
23470 return build_error_marker_type (cu, die);
23471 }
23472 }
23473
23474 /* Load the DIEs associated with type unit PER_CU into memory. */
23475
23476 static void
23477 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23478 {
23479 struct signatured_type *sig_type;
23480
23481 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23482 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23483
23484 /* We have the per_cu, but we need the signatured_type.
23485 Fortunately this is an easy translation. */
23486 gdb_assert (per_cu->is_debug_types);
23487 sig_type = (struct signatured_type *) per_cu;
23488
23489 gdb_assert (per_cu->cu == NULL);
23490
23491 read_signatured_type (sig_type);
23492
23493 gdb_assert (per_cu->cu != NULL);
23494 }
23495
23496 /* die_reader_func for read_signatured_type.
23497 This is identical to load_full_comp_unit_reader,
23498 but is kept separate for now. */
23499
23500 static void
23501 read_signatured_type_reader (const struct die_reader_specs *reader,
23502 const gdb_byte *info_ptr,
23503 struct die_info *comp_unit_die,
23504 int has_children,
23505 void *data)
23506 {
23507 struct dwarf2_cu *cu = reader->cu;
23508
23509 gdb_assert (cu->die_hash == NULL);
23510 cu->die_hash =
23511 htab_create_alloc_ex (cu->header.length / 12,
23512 die_hash,
23513 die_eq,
23514 NULL,
23515 &cu->comp_unit_obstack,
23516 hashtab_obstack_allocate,
23517 dummy_obstack_deallocate);
23518
23519 if (has_children)
23520 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23521 &info_ptr, comp_unit_die);
23522 cu->dies = comp_unit_die;
23523 /* comp_unit_die is not stored in die_hash, no need. */
23524
23525 /* We try not to read any attributes in this function, because not
23526 all CUs needed for references have been loaded yet, and symbol
23527 table processing isn't initialized. But we have to set the CU language,
23528 or we won't be able to build types correctly.
23529 Similarly, if we do not read the producer, we can not apply
23530 producer-specific interpretation. */
23531 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23532 }
23533
23534 /* Read in a signatured type and build its CU and DIEs.
23535 If the type is a stub for the real type in a DWO file,
23536 read in the real type from the DWO file as well. */
23537
23538 static void
23539 read_signatured_type (struct signatured_type *sig_type)
23540 {
23541 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23542
23543 gdb_assert (per_cu->is_debug_types);
23544 gdb_assert (per_cu->cu == NULL);
23545
23546 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23547 read_signatured_type_reader, NULL);
23548 sig_type->per_cu.tu_read = 1;
23549 }
23550
23551 /* Decode simple location descriptions.
23552 Given a pointer to a dwarf block that defines a location, compute
23553 the location and return the value.
23554
23555 NOTE drow/2003-11-18: This function is called in two situations
23556 now: for the address of static or global variables (partial symbols
23557 only) and for offsets into structures which are expected to be
23558 (more or less) constant. The partial symbol case should go away,
23559 and only the constant case should remain. That will let this
23560 function complain more accurately. A few special modes are allowed
23561 without complaint for global variables (for instance, global
23562 register values and thread-local values).
23563
23564 A location description containing no operations indicates that the
23565 object is optimized out. The return value is 0 for that case.
23566 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23567 callers will only want a very basic result and this can become a
23568 complaint.
23569
23570 Note that stack[0] is unused except as a default error return. */
23571
23572 static CORE_ADDR
23573 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23574 {
23575 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23576 size_t i;
23577 size_t size = blk->size;
23578 const gdb_byte *data = blk->data;
23579 CORE_ADDR stack[64];
23580 int stacki;
23581 unsigned int bytes_read, unsnd;
23582 gdb_byte op;
23583
23584 i = 0;
23585 stacki = 0;
23586 stack[stacki] = 0;
23587 stack[++stacki] = 0;
23588
23589 while (i < size)
23590 {
23591 op = data[i++];
23592 switch (op)
23593 {
23594 case DW_OP_lit0:
23595 case DW_OP_lit1:
23596 case DW_OP_lit2:
23597 case DW_OP_lit3:
23598 case DW_OP_lit4:
23599 case DW_OP_lit5:
23600 case DW_OP_lit6:
23601 case DW_OP_lit7:
23602 case DW_OP_lit8:
23603 case DW_OP_lit9:
23604 case DW_OP_lit10:
23605 case DW_OP_lit11:
23606 case DW_OP_lit12:
23607 case DW_OP_lit13:
23608 case DW_OP_lit14:
23609 case DW_OP_lit15:
23610 case DW_OP_lit16:
23611 case DW_OP_lit17:
23612 case DW_OP_lit18:
23613 case DW_OP_lit19:
23614 case DW_OP_lit20:
23615 case DW_OP_lit21:
23616 case DW_OP_lit22:
23617 case DW_OP_lit23:
23618 case DW_OP_lit24:
23619 case DW_OP_lit25:
23620 case DW_OP_lit26:
23621 case DW_OP_lit27:
23622 case DW_OP_lit28:
23623 case DW_OP_lit29:
23624 case DW_OP_lit30:
23625 case DW_OP_lit31:
23626 stack[++stacki] = op - DW_OP_lit0;
23627 break;
23628
23629 case DW_OP_reg0:
23630 case DW_OP_reg1:
23631 case DW_OP_reg2:
23632 case DW_OP_reg3:
23633 case DW_OP_reg4:
23634 case DW_OP_reg5:
23635 case DW_OP_reg6:
23636 case DW_OP_reg7:
23637 case DW_OP_reg8:
23638 case DW_OP_reg9:
23639 case DW_OP_reg10:
23640 case DW_OP_reg11:
23641 case DW_OP_reg12:
23642 case DW_OP_reg13:
23643 case DW_OP_reg14:
23644 case DW_OP_reg15:
23645 case DW_OP_reg16:
23646 case DW_OP_reg17:
23647 case DW_OP_reg18:
23648 case DW_OP_reg19:
23649 case DW_OP_reg20:
23650 case DW_OP_reg21:
23651 case DW_OP_reg22:
23652 case DW_OP_reg23:
23653 case DW_OP_reg24:
23654 case DW_OP_reg25:
23655 case DW_OP_reg26:
23656 case DW_OP_reg27:
23657 case DW_OP_reg28:
23658 case DW_OP_reg29:
23659 case DW_OP_reg30:
23660 case DW_OP_reg31:
23661 stack[++stacki] = op - DW_OP_reg0;
23662 if (i < size)
23663 dwarf2_complex_location_expr_complaint ();
23664 break;
23665
23666 case DW_OP_regx:
23667 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23668 i += bytes_read;
23669 stack[++stacki] = unsnd;
23670 if (i < size)
23671 dwarf2_complex_location_expr_complaint ();
23672 break;
23673
23674 case DW_OP_addr:
23675 stack[++stacki] = read_address (objfile->obfd, &data[i],
23676 cu, &bytes_read);
23677 i += bytes_read;
23678 break;
23679
23680 case DW_OP_const1u:
23681 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23682 i += 1;
23683 break;
23684
23685 case DW_OP_const1s:
23686 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23687 i += 1;
23688 break;
23689
23690 case DW_OP_const2u:
23691 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23692 i += 2;
23693 break;
23694
23695 case DW_OP_const2s:
23696 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23697 i += 2;
23698 break;
23699
23700 case DW_OP_const4u:
23701 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23702 i += 4;
23703 break;
23704
23705 case DW_OP_const4s:
23706 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23707 i += 4;
23708 break;
23709
23710 case DW_OP_const8u:
23711 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23712 i += 8;
23713 break;
23714
23715 case DW_OP_constu:
23716 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23717 &bytes_read);
23718 i += bytes_read;
23719 break;
23720
23721 case DW_OP_consts:
23722 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23723 i += bytes_read;
23724 break;
23725
23726 case DW_OP_dup:
23727 stack[stacki + 1] = stack[stacki];
23728 stacki++;
23729 break;
23730
23731 case DW_OP_plus:
23732 stack[stacki - 1] += stack[stacki];
23733 stacki--;
23734 break;
23735
23736 case DW_OP_plus_uconst:
23737 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23738 &bytes_read);
23739 i += bytes_read;
23740 break;
23741
23742 case DW_OP_minus:
23743 stack[stacki - 1] -= stack[stacki];
23744 stacki--;
23745 break;
23746
23747 case DW_OP_deref:
23748 /* If we're not the last op, then we definitely can't encode
23749 this using GDB's address_class enum. This is valid for partial
23750 global symbols, although the variable's address will be bogus
23751 in the psymtab. */
23752 if (i < size)
23753 dwarf2_complex_location_expr_complaint ();
23754 break;
23755
23756 case DW_OP_GNU_push_tls_address:
23757 case DW_OP_form_tls_address:
23758 /* The top of the stack has the offset from the beginning
23759 of the thread control block at which the variable is located. */
23760 /* Nothing should follow this operator, so the top of stack would
23761 be returned. */
23762 /* This is valid for partial global symbols, but the variable's
23763 address will be bogus in the psymtab. Make it always at least
23764 non-zero to not look as a variable garbage collected by linker
23765 which have DW_OP_addr 0. */
23766 if (i < size)
23767 dwarf2_complex_location_expr_complaint ();
23768 stack[stacki]++;
23769 break;
23770
23771 case DW_OP_GNU_uninit:
23772 break;
23773
23774 case DW_OP_GNU_addr_index:
23775 case DW_OP_GNU_const_index:
23776 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23777 &bytes_read);
23778 i += bytes_read;
23779 break;
23780
23781 default:
23782 {
23783 const char *name = get_DW_OP_name (op);
23784
23785 if (name)
23786 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23787 name);
23788 else
23789 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23790 op);
23791 }
23792
23793 return (stack[stacki]);
23794 }
23795
23796 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23797 outside of the allocated space. Also enforce minimum>0. */
23798 if (stacki >= ARRAY_SIZE (stack) - 1)
23799 {
23800 complaint (&symfile_complaints,
23801 _("location description stack overflow"));
23802 return 0;
23803 }
23804
23805 if (stacki <= 0)
23806 {
23807 complaint (&symfile_complaints,
23808 _("location description stack underflow"));
23809 return 0;
23810 }
23811 }
23812 return (stack[stacki]);
23813 }
23814
23815 /* memory allocation interface */
23816
23817 static struct dwarf_block *
23818 dwarf_alloc_block (struct dwarf2_cu *cu)
23819 {
23820 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23821 }
23822
23823 static struct die_info *
23824 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23825 {
23826 struct die_info *die;
23827 size_t size = sizeof (struct die_info);
23828
23829 if (num_attrs > 1)
23830 size += (num_attrs - 1) * sizeof (struct attribute);
23831
23832 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23833 memset (die, 0, sizeof (struct die_info));
23834 return (die);
23835 }
23836
23837 \f
23838 /* Macro support. */
23839
23840 /* Return file name relative to the compilation directory of file number I in
23841 *LH's file name table. The result is allocated using xmalloc; the caller is
23842 responsible for freeing it. */
23843
23844 static char *
23845 file_file_name (int file, struct line_header *lh)
23846 {
23847 /* Is the file number a valid index into the line header's file name
23848 table? Remember that file numbers start with one, not zero. */
23849 if (1 <= file && file <= lh->file_names.size ())
23850 {
23851 const file_entry &fe = lh->file_names[file - 1];
23852
23853 if (!IS_ABSOLUTE_PATH (fe.name))
23854 {
23855 const char *dir = fe.include_dir (lh);
23856 if (dir != NULL)
23857 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23858 }
23859 return xstrdup (fe.name);
23860 }
23861 else
23862 {
23863 /* The compiler produced a bogus file number. We can at least
23864 record the macro definitions made in the file, even if we
23865 won't be able to find the file by name. */
23866 char fake_name[80];
23867
23868 xsnprintf (fake_name, sizeof (fake_name),
23869 "<bad macro file number %d>", file);
23870
23871 complaint (&symfile_complaints,
23872 _("bad file number in macro information (%d)"),
23873 file);
23874
23875 return xstrdup (fake_name);
23876 }
23877 }
23878
23879 /* Return the full name of file number I in *LH's file name table.
23880 Use COMP_DIR as the name of the current directory of the
23881 compilation. The result is allocated using xmalloc; the caller is
23882 responsible for freeing it. */
23883 static char *
23884 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23885 {
23886 /* Is the file number a valid index into the line header's file name
23887 table? Remember that file numbers start with one, not zero. */
23888 if (1 <= file && file <= lh->file_names.size ())
23889 {
23890 char *relative = file_file_name (file, lh);
23891
23892 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23893 return relative;
23894 return reconcat (relative, comp_dir, SLASH_STRING,
23895 relative, (char *) NULL);
23896 }
23897 else
23898 return file_file_name (file, lh);
23899 }
23900
23901
23902 static struct macro_source_file *
23903 macro_start_file (int file, int line,
23904 struct macro_source_file *current_file,
23905 struct line_header *lh)
23906 {
23907 /* File name relative to the compilation directory of this source file. */
23908 char *file_name = file_file_name (file, lh);
23909
23910 if (! current_file)
23911 {
23912 /* Note: We don't create a macro table for this compilation unit
23913 at all until we actually get a filename. */
23914 struct macro_table *macro_table = get_macro_table ();
23915
23916 /* If we have no current file, then this must be the start_file
23917 directive for the compilation unit's main source file. */
23918 current_file = macro_set_main (macro_table, file_name);
23919 macro_define_special (macro_table);
23920 }
23921 else
23922 current_file = macro_include (current_file, line, file_name);
23923
23924 xfree (file_name);
23925
23926 return current_file;
23927 }
23928
23929 static const char *
23930 consume_improper_spaces (const char *p, const char *body)
23931 {
23932 if (*p == ' ')
23933 {
23934 complaint (&symfile_complaints,
23935 _("macro definition contains spaces "
23936 "in formal argument list:\n`%s'"),
23937 body);
23938
23939 while (*p == ' ')
23940 p++;
23941 }
23942
23943 return p;
23944 }
23945
23946
23947 static void
23948 parse_macro_definition (struct macro_source_file *file, int line,
23949 const char *body)
23950 {
23951 const char *p;
23952
23953 /* The body string takes one of two forms. For object-like macro
23954 definitions, it should be:
23955
23956 <macro name> " " <definition>
23957
23958 For function-like macro definitions, it should be:
23959
23960 <macro name> "() " <definition>
23961 or
23962 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23963
23964 Spaces may appear only where explicitly indicated, and in the
23965 <definition>.
23966
23967 The Dwarf 2 spec says that an object-like macro's name is always
23968 followed by a space, but versions of GCC around March 2002 omit
23969 the space when the macro's definition is the empty string.
23970
23971 The Dwarf 2 spec says that there should be no spaces between the
23972 formal arguments in a function-like macro's formal argument list,
23973 but versions of GCC around March 2002 include spaces after the
23974 commas. */
23975
23976
23977 /* Find the extent of the macro name. The macro name is terminated
23978 by either a space or null character (for an object-like macro) or
23979 an opening paren (for a function-like macro). */
23980 for (p = body; *p; p++)
23981 if (*p == ' ' || *p == '(')
23982 break;
23983
23984 if (*p == ' ' || *p == '\0')
23985 {
23986 /* It's an object-like macro. */
23987 int name_len = p - body;
23988 char *name = savestring (body, name_len);
23989 const char *replacement;
23990
23991 if (*p == ' ')
23992 replacement = body + name_len + 1;
23993 else
23994 {
23995 dwarf2_macro_malformed_definition_complaint (body);
23996 replacement = body + name_len;
23997 }
23998
23999 macro_define_object (file, line, name, replacement);
24000
24001 xfree (name);
24002 }
24003 else if (*p == '(')
24004 {
24005 /* It's a function-like macro. */
24006 char *name = savestring (body, p - body);
24007 int argc = 0;
24008 int argv_size = 1;
24009 char **argv = XNEWVEC (char *, argv_size);
24010
24011 p++;
24012
24013 p = consume_improper_spaces (p, body);
24014
24015 /* Parse the formal argument list. */
24016 while (*p && *p != ')')
24017 {
24018 /* Find the extent of the current argument name. */
24019 const char *arg_start = p;
24020
24021 while (*p && *p != ',' && *p != ')' && *p != ' ')
24022 p++;
24023
24024 if (! *p || p == arg_start)
24025 dwarf2_macro_malformed_definition_complaint (body);
24026 else
24027 {
24028 /* Make sure argv has room for the new argument. */
24029 if (argc >= argv_size)
24030 {
24031 argv_size *= 2;
24032 argv = XRESIZEVEC (char *, argv, argv_size);
24033 }
24034
24035 argv[argc++] = savestring (arg_start, p - arg_start);
24036 }
24037
24038 p = consume_improper_spaces (p, body);
24039
24040 /* Consume the comma, if present. */
24041 if (*p == ',')
24042 {
24043 p++;
24044
24045 p = consume_improper_spaces (p, body);
24046 }
24047 }
24048
24049 if (*p == ')')
24050 {
24051 p++;
24052
24053 if (*p == ' ')
24054 /* Perfectly formed definition, no complaints. */
24055 macro_define_function (file, line, name,
24056 argc, (const char **) argv,
24057 p + 1);
24058 else if (*p == '\0')
24059 {
24060 /* Complain, but do define it. */
24061 dwarf2_macro_malformed_definition_complaint (body);
24062 macro_define_function (file, line, name,
24063 argc, (const char **) argv,
24064 p);
24065 }
24066 else
24067 /* Just complain. */
24068 dwarf2_macro_malformed_definition_complaint (body);
24069 }
24070 else
24071 /* Just complain. */
24072 dwarf2_macro_malformed_definition_complaint (body);
24073
24074 xfree (name);
24075 {
24076 int i;
24077
24078 for (i = 0; i < argc; i++)
24079 xfree (argv[i]);
24080 }
24081 xfree (argv);
24082 }
24083 else
24084 dwarf2_macro_malformed_definition_complaint (body);
24085 }
24086
24087 /* Skip some bytes from BYTES according to the form given in FORM.
24088 Returns the new pointer. */
24089
24090 static const gdb_byte *
24091 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24092 enum dwarf_form form,
24093 unsigned int offset_size,
24094 struct dwarf2_section_info *section)
24095 {
24096 unsigned int bytes_read;
24097
24098 switch (form)
24099 {
24100 case DW_FORM_data1:
24101 case DW_FORM_flag:
24102 ++bytes;
24103 break;
24104
24105 case DW_FORM_data2:
24106 bytes += 2;
24107 break;
24108
24109 case DW_FORM_data4:
24110 bytes += 4;
24111 break;
24112
24113 case DW_FORM_data8:
24114 bytes += 8;
24115 break;
24116
24117 case DW_FORM_data16:
24118 bytes += 16;
24119 break;
24120
24121 case DW_FORM_string:
24122 read_direct_string (abfd, bytes, &bytes_read);
24123 bytes += bytes_read;
24124 break;
24125
24126 case DW_FORM_sec_offset:
24127 case DW_FORM_strp:
24128 case DW_FORM_GNU_strp_alt:
24129 bytes += offset_size;
24130 break;
24131
24132 case DW_FORM_block:
24133 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24134 bytes += bytes_read;
24135 break;
24136
24137 case DW_FORM_block1:
24138 bytes += 1 + read_1_byte (abfd, bytes);
24139 break;
24140 case DW_FORM_block2:
24141 bytes += 2 + read_2_bytes (abfd, bytes);
24142 break;
24143 case DW_FORM_block4:
24144 bytes += 4 + read_4_bytes (abfd, bytes);
24145 break;
24146
24147 case DW_FORM_sdata:
24148 case DW_FORM_udata:
24149 case DW_FORM_GNU_addr_index:
24150 case DW_FORM_GNU_str_index:
24151 bytes = gdb_skip_leb128 (bytes, buffer_end);
24152 if (bytes == NULL)
24153 {
24154 dwarf2_section_buffer_overflow_complaint (section);
24155 return NULL;
24156 }
24157 break;
24158
24159 case DW_FORM_implicit_const:
24160 break;
24161
24162 default:
24163 {
24164 complaint (&symfile_complaints,
24165 _("invalid form 0x%x in `%s'"),
24166 form, get_section_name (section));
24167 return NULL;
24168 }
24169 }
24170
24171 return bytes;
24172 }
24173
24174 /* A helper for dwarf_decode_macros that handles skipping an unknown
24175 opcode. Returns an updated pointer to the macro data buffer; or,
24176 on error, issues a complaint and returns NULL. */
24177
24178 static const gdb_byte *
24179 skip_unknown_opcode (unsigned int opcode,
24180 const gdb_byte **opcode_definitions,
24181 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24182 bfd *abfd,
24183 unsigned int offset_size,
24184 struct dwarf2_section_info *section)
24185 {
24186 unsigned int bytes_read, i;
24187 unsigned long arg;
24188 const gdb_byte *defn;
24189
24190 if (opcode_definitions[opcode] == NULL)
24191 {
24192 complaint (&symfile_complaints,
24193 _("unrecognized DW_MACFINO opcode 0x%x"),
24194 opcode);
24195 return NULL;
24196 }
24197
24198 defn = opcode_definitions[opcode];
24199 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24200 defn += bytes_read;
24201
24202 for (i = 0; i < arg; ++i)
24203 {
24204 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24205 (enum dwarf_form) defn[i], offset_size,
24206 section);
24207 if (mac_ptr == NULL)
24208 {
24209 /* skip_form_bytes already issued the complaint. */
24210 return NULL;
24211 }
24212 }
24213
24214 return mac_ptr;
24215 }
24216
24217 /* A helper function which parses the header of a macro section.
24218 If the macro section is the extended (for now called "GNU") type,
24219 then this updates *OFFSET_SIZE. Returns a pointer to just after
24220 the header, or issues a complaint and returns NULL on error. */
24221
24222 static const gdb_byte *
24223 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24224 bfd *abfd,
24225 const gdb_byte *mac_ptr,
24226 unsigned int *offset_size,
24227 int section_is_gnu)
24228 {
24229 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24230
24231 if (section_is_gnu)
24232 {
24233 unsigned int version, flags;
24234
24235 version = read_2_bytes (abfd, mac_ptr);
24236 if (version != 4 && version != 5)
24237 {
24238 complaint (&symfile_complaints,
24239 _("unrecognized version `%d' in .debug_macro section"),
24240 version);
24241 return NULL;
24242 }
24243 mac_ptr += 2;
24244
24245 flags = read_1_byte (abfd, mac_ptr);
24246 ++mac_ptr;
24247 *offset_size = (flags & 1) ? 8 : 4;
24248
24249 if ((flags & 2) != 0)
24250 /* We don't need the line table offset. */
24251 mac_ptr += *offset_size;
24252
24253 /* Vendor opcode descriptions. */
24254 if ((flags & 4) != 0)
24255 {
24256 unsigned int i, count;
24257
24258 count = read_1_byte (abfd, mac_ptr);
24259 ++mac_ptr;
24260 for (i = 0; i < count; ++i)
24261 {
24262 unsigned int opcode, bytes_read;
24263 unsigned long arg;
24264
24265 opcode = read_1_byte (abfd, mac_ptr);
24266 ++mac_ptr;
24267 opcode_definitions[opcode] = mac_ptr;
24268 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24269 mac_ptr += bytes_read;
24270 mac_ptr += arg;
24271 }
24272 }
24273 }
24274
24275 return mac_ptr;
24276 }
24277
24278 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24279 including DW_MACRO_import. */
24280
24281 static void
24282 dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24283 bfd *abfd,
24284 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24285 struct macro_source_file *current_file,
24286 struct line_header *lh,
24287 struct dwarf2_section_info *section,
24288 int section_is_gnu, int section_is_dwz,
24289 unsigned int offset_size,
24290 htab_t include_hash)
24291 {
24292 struct objfile *objfile = dwarf2_per_objfile->objfile;
24293 enum dwarf_macro_record_type macinfo_type;
24294 int at_commandline;
24295 const gdb_byte *opcode_definitions[256];
24296
24297 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24298 &offset_size, section_is_gnu);
24299 if (mac_ptr == NULL)
24300 {
24301 /* We already issued a complaint. */
24302 return;
24303 }
24304
24305 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24306 GDB is still reading the definitions from command line. First
24307 DW_MACINFO_start_file will need to be ignored as it was already executed
24308 to create CURRENT_FILE for the main source holding also the command line
24309 definitions. On first met DW_MACINFO_start_file this flag is reset to
24310 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24311
24312 at_commandline = 1;
24313
24314 do
24315 {
24316 /* Do we at least have room for a macinfo type byte? */
24317 if (mac_ptr >= mac_end)
24318 {
24319 dwarf2_section_buffer_overflow_complaint (section);
24320 break;
24321 }
24322
24323 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24324 mac_ptr++;
24325
24326 /* Note that we rely on the fact that the corresponding GNU and
24327 DWARF constants are the same. */
24328 DIAGNOSTIC_PUSH
24329 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24330 switch (macinfo_type)
24331 {
24332 /* A zero macinfo type indicates the end of the macro
24333 information. */
24334 case 0:
24335 break;
24336
24337 case DW_MACRO_define:
24338 case DW_MACRO_undef:
24339 case DW_MACRO_define_strp:
24340 case DW_MACRO_undef_strp:
24341 case DW_MACRO_define_sup:
24342 case DW_MACRO_undef_sup:
24343 {
24344 unsigned int bytes_read;
24345 int line;
24346 const char *body;
24347 int is_define;
24348
24349 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24350 mac_ptr += bytes_read;
24351
24352 if (macinfo_type == DW_MACRO_define
24353 || macinfo_type == DW_MACRO_undef)
24354 {
24355 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24356 mac_ptr += bytes_read;
24357 }
24358 else
24359 {
24360 LONGEST str_offset;
24361
24362 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24363 mac_ptr += offset_size;
24364
24365 if (macinfo_type == DW_MACRO_define_sup
24366 || macinfo_type == DW_MACRO_undef_sup
24367 || section_is_dwz)
24368 {
24369 struct dwz_file *dwz
24370 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24371
24372 body = read_indirect_string_from_dwz (objfile,
24373 dwz, str_offset);
24374 }
24375 else
24376 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24377 abfd, str_offset);
24378 }
24379
24380 is_define = (macinfo_type == DW_MACRO_define
24381 || macinfo_type == DW_MACRO_define_strp
24382 || macinfo_type == DW_MACRO_define_sup);
24383 if (! current_file)
24384 {
24385 /* DWARF violation as no main source is present. */
24386 complaint (&symfile_complaints,
24387 _("debug info with no main source gives macro %s "
24388 "on line %d: %s"),
24389 is_define ? _("definition") : _("undefinition"),
24390 line, body);
24391 break;
24392 }
24393 if ((line == 0 && !at_commandline)
24394 || (line != 0 && at_commandline))
24395 complaint (&symfile_complaints,
24396 _("debug info gives %s macro %s with %s line %d: %s"),
24397 at_commandline ? _("command-line") : _("in-file"),
24398 is_define ? _("definition") : _("undefinition"),
24399 line == 0 ? _("zero") : _("non-zero"), line, body);
24400
24401 if (is_define)
24402 parse_macro_definition (current_file, line, body);
24403 else
24404 {
24405 gdb_assert (macinfo_type == DW_MACRO_undef
24406 || macinfo_type == DW_MACRO_undef_strp
24407 || macinfo_type == DW_MACRO_undef_sup);
24408 macro_undef (current_file, line, body);
24409 }
24410 }
24411 break;
24412
24413 case DW_MACRO_start_file:
24414 {
24415 unsigned int bytes_read;
24416 int line, file;
24417
24418 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24419 mac_ptr += bytes_read;
24420 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24421 mac_ptr += bytes_read;
24422
24423 if ((line == 0 && !at_commandline)
24424 || (line != 0 && at_commandline))
24425 complaint (&symfile_complaints,
24426 _("debug info gives source %d included "
24427 "from %s at %s line %d"),
24428 file, at_commandline ? _("command-line") : _("file"),
24429 line == 0 ? _("zero") : _("non-zero"), line);
24430
24431 if (at_commandline)
24432 {
24433 /* This DW_MACRO_start_file was executed in the
24434 pass one. */
24435 at_commandline = 0;
24436 }
24437 else
24438 current_file = macro_start_file (file, line, current_file, lh);
24439 }
24440 break;
24441
24442 case DW_MACRO_end_file:
24443 if (! current_file)
24444 complaint (&symfile_complaints,
24445 _("macro debug info has an unmatched "
24446 "`close_file' directive"));
24447 else
24448 {
24449 current_file = current_file->included_by;
24450 if (! current_file)
24451 {
24452 enum dwarf_macro_record_type next_type;
24453
24454 /* GCC circa March 2002 doesn't produce the zero
24455 type byte marking the end of the compilation
24456 unit. Complain if it's not there, but exit no
24457 matter what. */
24458
24459 /* Do we at least have room for a macinfo type byte? */
24460 if (mac_ptr >= mac_end)
24461 {
24462 dwarf2_section_buffer_overflow_complaint (section);
24463 return;
24464 }
24465
24466 /* We don't increment mac_ptr here, so this is just
24467 a look-ahead. */
24468 next_type
24469 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24470 mac_ptr);
24471 if (next_type != 0)
24472 complaint (&symfile_complaints,
24473 _("no terminating 0-type entry for "
24474 "macros in `.debug_macinfo' section"));
24475
24476 return;
24477 }
24478 }
24479 break;
24480
24481 case DW_MACRO_import:
24482 case DW_MACRO_import_sup:
24483 {
24484 LONGEST offset;
24485 void **slot;
24486 bfd *include_bfd = abfd;
24487 struct dwarf2_section_info *include_section = section;
24488 const gdb_byte *include_mac_end = mac_end;
24489 int is_dwz = section_is_dwz;
24490 const gdb_byte *new_mac_ptr;
24491
24492 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24493 mac_ptr += offset_size;
24494
24495 if (macinfo_type == DW_MACRO_import_sup)
24496 {
24497 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24498
24499 dwarf2_read_section (objfile, &dwz->macro);
24500
24501 include_section = &dwz->macro;
24502 include_bfd = get_section_bfd_owner (include_section);
24503 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24504 is_dwz = 1;
24505 }
24506
24507 new_mac_ptr = include_section->buffer + offset;
24508 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24509
24510 if (*slot != NULL)
24511 {
24512 /* This has actually happened; see
24513 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24514 complaint (&symfile_complaints,
24515 _("recursive DW_MACRO_import in "
24516 ".debug_macro section"));
24517 }
24518 else
24519 {
24520 *slot = (void *) new_mac_ptr;
24521
24522 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24523 include_bfd, new_mac_ptr,
24524 include_mac_end, current_file, lh,
24525 section, section_is_gnu, is_dwz,
24526 offset_size, include_hash);
24527
24528 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24529 }
24530 }
24531 break;
24532
24533 case DW_MACINFO_vendor_ext:
24534 if (!section_is_gnu)
24535 {
24536 unsigned int bytes_read;
24537
24538 /* This reads the constant, but since we don't recognize
24539 any vendor extensions, we ignore it. */
24540 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24541 mac_ptr += bytes_read;
24542 read_direct_string (abfd, mac_ptr, &bytes_read);
24543 mac_ptr += bytes_read;
24544
24545 /* We don't recognize any vendor extensions. */
24546 break;
24547 }
24548 /* FALLTHROUGH */
24549
24550 default:
24551 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24552 mac_ptr, mac_end, abfd, offset_size,
24553 section);
24554 if (mac_ptr == NULL)
24555 return;
24556 break;
24557 }
24558 DIAGNOSTIC_POP
24559 } while (macinfo_type != 0);
24560 }
24561
24562 static void
24563 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24564 int section_is_gnu)
24565 {
24566 struct dwarf2_per_objfile *dwarf2_per_objfile
24567 = cu->per_cu->dwarf2_per_objfile;
24568 struct objfile *objfile = dwarf2_per_objfile->objfile;
24569 struct line_header *lh = cu->line_header;
24570 bfd *abfd;
24571 const gdb_byte *mac_ptr, *mac_end;
24572 struct macro_source_file *current_file = 0;
24573 enum dwarf_macro_record_type macinfo_type;
24574 unsigned int offset_size = cu->header.offset_size;
24575 const gdb_byte *opcode_definitions[256];
24576 void **slot;
24577 struct dwarf2_section_info *section;
24578 const char *section_name;
24579
24580 if (cu->dwo_unit != NULL)
24581 {
24582 if (section_is_gnu)
24583 {
24584 section = &cu->dwo_unit->dwo_file->sections.macro;
24585 section_name = ".debug_macro.dwo";
24586 }
24587 else
24588 {
24589 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24590 section_name = ".debug_macinfo.dwo";
24591 }
24592 }
24593 else
24594 {
24595 if (section_is_gnu)
24596 {
24597 section = &dwarf2_per_objfile->macro;
24598 section_name = ".debug_macro";
24599 }
24600 else
24601 {
24602 section = &dwarf2_per_objfile->macinfo;
24603 section_name = ".debug_macinfo";
24604 }
24605 }
24606
24607 dwarf2_read_section (objfile, section);
24608 if (section->buffer == NULL)
24609 {
24610 complaint (&symfile_complaints, _("missing %s section"), section_name);
24611 return;
24612 }
24613 abfd = get_section_bfd_owner (section);
24614
24615 /* First pass: Find the name of the base filename.
24616 This filename is needed in order to process all macros whose definition
24617 (or undefinition) comes from the command line. These macros are defined
24618 before the first DW_MACINFO_start_file entry, and yet still need to be
24619 associated to the base file.
24620
24621 To determine the base file name, we scan the macro definitions until we
24622 reach the first DW_MACINFO_start_file entry. We then initialize
24623 CURRENT_FILE accordingly so that any macro definition found before the
24624 first DW_MACINFO_start_file can still be associated to the base file. */
24625
24626 mac_ptr = section->buffer + offset;
24627 mac_end = section->buffer + section->size;
24628
24629 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24630 &offset_size, section_is_gnu);
24631 if (mac_ptr == NULL)
24632 {
24633 /* We already issued a complaint. */
24634 return;
24635 }
24636
24637 do
24638 {
24639 /* Do we at least have room for a macinfo type byte? */
24640 if (mac_ptr >= mac_end)
24641 {
24642 /* Complaint is printed during the second pass as GDB will probably
24643 stop the first pass earlier upon finding
24644 DW_MACINFO_start_file. */
24645 break;
24646 }
24647
24648 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24649 mac_ptr++;
24650
24651 /* Note that we rely on the fact that the corresponding GNU and
24652 DWARF constants are the same. */
24653 DIAGNOSTIC_PUSH
24654 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24655 switch (macinfo_type)
24656 {
24657 /* A zero macinfo type indicates the end of the macro
24658 information. */
24659 case 0:
24660 break;
24661
24662 case DW_MACRO_define:
24663 case DW_MACRO_undef:
24664 /* Only skip the data by MAC_PTR. */
24665 {
24666 unsigned int bytes_read;
24667
24668 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24669 mac_ptr += bytes_read;
24670 read_direct_string (abfd, mac_ptr, &bytes_read);
24671 mac_ptr += bytes_read;
24672 }
24673 break;
24674
24675 case DW_MACRO_start_file:
24676 {
24677 unsigned int bytes_read;
24678 int line, file;
24679
24680 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24681 mac_ptr += bytes_read;
24682 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24683 mac_ptr += bytes_read;
24684
24685 current_file = macro_start_file (file, line, current_file, lh);
24686 }
24687 break;
24688
24689 case DW_MACRO_end_file:
24690 /* No data to skip by MAC_PTR. */
24691 break;
24692
24693 case DW_MACRO_define_strp:
24694 case DW_MACRO_undef_strp:
24695 case DW_MACRO_define_sup:
24696 case DW_MACRO_undef_sup:
24697 {
24698 unsigned int bytes_read;
24699
24700 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24701 mac_ptr += bytes_read;
24702 mac_ptr += offset_size;
24703 }
24704 break;
24705
24706 case DW_MACRO_import:
24707 case DW_MACRO_import_sup:
24708 /* Note that, according to the spec, a transparent include
24709 chain cannot call DW_MACRO_start_file. So, we can just
24710 skip this opcode. */
24711 mac_ptr += offset_size;
24712 break;
24713
24714 case DW_MACINFO_vendor_ext:
24715 /* Only skip the data by MAC_PTR. */
24716 if (!section_is_gnu)
24717 {
24718 unsigned int bytes_read;
24719
24720 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24721 mac_ptr += bytes_read;
24722 read_direct_string (abfd, mac_ptr, &bytes_read);
24723 mac_ptr += bytes_read;
24724 }
24725 /* FALLTHROUGH */
24726
24727 default:
24728 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24729 mac_ptr, mac_end, abfd, offset_size,
24730 section);
24731 if (mac_ptr == NULL)
24732 return;
24733 break;
24734 }
24735 DIAGNOSTIC_POP
24736 } while (macinfo_type != 0 && current_file == NULL);
24737
24738 /* Second pass: Process all entries.
24739
24740 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24741 command-line macro definitions/undefinitions. This flag is unset when we
24742 reach the first DW_MACINFO_start_file entry. */
24743
24744 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24745 htab_eq_pointer,
24746 NULL, xcalloc, xfree));
24747 mac_ptr = section->buffer + offset;
24748 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24749 *slot = (void *) mac_ptr;
24750 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24751 abfd, mac_ptr, mac_end,
24752 current_file, lh, section,
24753 section_is_gnu, 0, offset_size,
24754 include_hash.get ());
24755 }
24756
24757 /* Check if the attribute's form is a DW_FORM_block*
24758 if so return true else false. */
24759
24760 static int
24761 attr_form_is_block (const struct attribute *attr)
24762 {
24763 return (attr == NULL ? 0 :
24764 attr->form == DW_FORM_block1
24765 || attr->form == DW_FORM_block2
24766 || attr->form == DW_FORM_block4
24767 || attr->form == DW_FORM_block
24768 || attr->form == DW_FORM_exprloc);
24769 }
24770
24771 /* Return non-zero if ATTR's value is a section offset --- classes
24772 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24773 You may use DW_UNSND (attr) to retrieve such offsets.
24774
24775 Section 7.5.4, "Attribute Encodings", explains that no attribute
24776 may have a value that belongs to more than one of these classes; it
24777 would be ambiguous if we did, because we use the same forms for all
24778 of them. */
24779
24780 static int
24781 attr_form_is_section_offset (const struct attribute *attr)
24782 {
24783 return (attr->form == DW_FORM_data4
24784 || attr->form == DW_FORM_data8
24785 || attr->form == DW_FORM_sec_offset);
24786 }
24787
24788 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24789 zero otherwise. When this function returns true, you can apply
24790 dwarf2_get_attr_constant_value to it.
24791
24792 However, note that for some attributes you must check
24793 attr_form_is_section_offset before using this test. DW_FORM_data4
24794 and DW_FORM_data8 are members of both the constant class, and of
24795 the classes that contain offsets into other debug sections
24796 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24797 that, if an attribute's can be either a constant or one of the
24798 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
24799 taken as section offsets, not constants.
24800
24801 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24802 cannot handle that. */
24803
24804 static int
24805 attr_form_is_constant (const struct attribute *attr)
24806 {
24807 switch (attr->form)
24808 {
24809 case DW_FORM_sdata:
24810 case DW_FORM_udata:
24811 case DW_FORM_data1:
24812 case DW_FORM_data2:
24813 case DW_FORM_data4:
24814 case DW_FORM_data8:
24815 case DW_FORM_implicit_const:
24816 return 1;
24817 default:
24818 return 0;
24819 }
24820 }
24821
24822
24823 /* DW_ADDR is always stored already as sect_offset; despite for the forms
24824 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24825
24826 static int
24827 attr_form_is_ref (const struct attribute *attr)
24828 {
24829 switch (attr->form)
24830 {
24831 case DW_FORM_ref_addr:
24832 case DW_FORM_ref1:
24833 case DW_FORM_ref2:
24834 case DW_FORM_ref4:
24835 case DW_FORM_ref8:
24836 case DW_FORM_ref_udata:
24837 case DW_FORM_GNU_ref_alt:
24838 return 1;
24839 default:
24840 return 0;
24841 }
24842 }
24843
24844 /* Return the .debug_loc section to use for CU.
24845 For DWO files use .debug_loc.dwo. */
24846
24847 static struct dwarf2_section_info *
24848 cu_debug_loc_section (struct dwarf2_cu *cu)
24849 {
24850 struct dwarf2_per_objfile *dwarf2_per_objfile
24851 = cu->per_cu->dwarf2_per_objfile;
24852
24853 if (cu->dwo_unit)
24854 {
24855 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24856
24857 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24858 }
24859 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24860 : &dwarf2_per_objfile->loc);
24861 }
24862
24863 /* A helper function that fills in a dwarf2_loclist_baton. */
24864
24865 static void
24866 fill_in_loclist_baton (struct dwarf2_cu *cu,
24867 struct dwarf2_loclist_baton *baton,
24868 const struct attribute *attr)
24869 {
24870 struct dwarf2_per_objfile *dwarf2_per_objfile
24871 = cu->per_cu->dwarf2_per_objfile;
24872 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24873
24874 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
24875
24876 baton->per_cu = cu->per_cu;
24877 gdb_assert (baton->per_cu);
24878 /* We don't know how long the location list is, but make sure we
24879 don't run off the edge of the section. */
24880 baton->size = section->size - DW_UNSND (attr);
24881 baton->data = section->buffer + DW_UNSND (attr);
24882 baton->base_address = cu->base_address;
24883 baton->from_dwo = cu->dwo_unit != NULL;
24884 }
24885
24886 static void
24887 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24888 struct dwarf2_cu *cu, int is_block)
24889 {
24890 struct dwarf2_per_objfile *dwarf2_per_objfile
24891 = cu->per_cu->dwarf2_per_objfile;
24892 struct objfile *objfile = dwarf2_per_objfile->objfile;
24893 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24894
24895 if (attr_form_is_section_offset (attr)
24896 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24897 the section. If so, fall through to the complaint in the
24898 other branch. */
24899 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24900 {
24901 struct dwarf2_loclist_baton *baton;
24902
24903 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24904
24905 fill_in_loclist_baton (cu, baton, attr);
24906
24907 if (cu->base_known == 0)
24908 complaint (&symfile_complaints,
24909 _("Location list used without "
24910 "specifying the CU base address."));
24911
24912 SYMBOL_ACLASS_INDEX (sym) = (is_block
24913 ? dwarf2_loclist_block_index
24914 : dwarf2_loclist_index);
24915 SYMBOL_LOCATION_BATON (sym) = baton;
24916 }
24917 else
24918 {
24919 struct dwarf2_locexpr_baton *baton;
24920
24921 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24922 baton->per_cu = cu->per_cu;
24923 gdb_assert (baton->per_cu);
24924
24925 if (attr_form_is_block (attr))
24926 {
24927 /* Note that we're just copying the block's data pointer
24928 here, not the actual data. We're still pointing into the
24929 info_buffer for SYM's objfile; right now we never release
24930 that buffer, but when we do clean up properly this may
24931 need to change. */
24932 baton->size = DW_BLOCK (attr)->size;
24933 baton->data = DW_BLOCK (attr)->data;
24934 }
24935 else
24936 {
24937 dwarf2_invalid_attrib_class_complaint ("location description",
24938 SYMBOL_NATURAL_NAME (sym));
24939 baton->size = 0;
24940 }
24941
24942 SYMBOL_ACLASS_INDEX (sym) = (is_block
24943 ? dwarf2_locexpr_block_index
24944 : dwarf2_locexpr_index);
24945 SYMBOL_LOCATION_BATON (sym) = baton;
24946 }
24947 }
24948
24949 /* Return the OBJFILE associated with the compilation unit CU. If CU
24950 came from a separate debuginfo file, then the master objfile is
24951 returned. */
24952
24953 struct objfile *
24954 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24955 {
24956 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24957
24958 /* Return the master objfile, so that we can report and look up the
24959 correct file containing this variable. */
24960 if (objfile->separate_debug_objfile_backlink)
24961 objfile = objfile->separate_debug_objfile_backlink;
24962
24963 return objfile;
24964 }
24965
24966 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24967 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24968 CU_HEADERP first. */
24969
24970 static const struct comp_unit_head *
24971 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24972 struct dwarf2_per_cu_data *per_cu)
24973 {
24974 const gdb_byte *info_ptr;
24975
24976 if (per_cu->cu)
24977 return &per_cu->cu->header;
24978
24979 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24980
24981 memset (cu_headerp, 0, sizeof (*cu_headerp));
24982 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24983 rcuh_kind::COMPILE);
24984
24985 return cu_headerp;
24986 }
24987
24988 /* Return the address size given in the compilation unit header for CU. */
24989
24990 int
24991 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24992 {
24993 struct comp_unit_head cu_header_local;
24994 const struct comp_unit_head *cu_headerp;
24995
24996 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24997
24998 return cu_headerp->addr_size;
24999 }
25000
25001 /* Return the offset size given in the compilation unit header for CU. */
25002
25003 int
25004 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25005 {
25006 struct comp_unit_head cu_header_local;
25007 const struct comp_unit_head *cu_headerp;
25008
25009 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25010
25011 return cu_headerp->offset_size;
25012 }
25013
25014 /* See its dwarf2loc.h declaration. */
25015
25016 int
25017 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25018 {
25019 struct comp_unit_head cu_header_local;
25020 const struct comp_unit_head *cu_headerp;
25021
25022 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25023
25024 if (cu_headerp->version == 2)
25025 return cu_headerp->addr_size;
25026 else
25027 return cu_headerp->offset_size;
25028 }
25029
25030 /* Return the text offset of the CU. The returned offset comes from
25031 this CU's objfile. If this objfile came from a separate debuginfo
25032 file, then the offset may be different from the corresponding
25033 offset in the parent objfile. */
25034
25035 CORE_ADDR
25036 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25037 {
25038 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25039
25040 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25041 }
25042
25043 /* Return DWARF version number of PER_CU. */
25044
25045 short
25046 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25047 {
25048 return per_cu->dwarf_version;
25049 }
25050
25051 /* Locate the .debug_info compilation unit from CU's objfile which contains
25052 the DIE at OFFSET. Raises an error on failure. */
25053
25054 static struct dwarf2_per_cu_data *
25055 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25056 unsigned int offset_in_dwz,
25057 struct dwarf2_per_objfile *dwarf2_per_objfile)
25058 {
25059 struct dwarf2_per_cu_data *this_cu;
25060 int low, high;
25061 const sect_offset *cu_off;
25062
25063 low = 0;
25064 high = dwarf2_per_objfile->n_comp_units - 1;
25065 while (high > low)
25066 {
25067 struct dwarf2_per_cu_data *mid_cu;
25068 int mid = low + (high - low) / 2;
25069
25070 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25071 cu_off = &mid_cu->sect_off;
25072 if (mid_cu->is_dwz > offset_in_dwz
25073 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
25074 high = mid;
25075 else
25076 low = mid + 1;
25077 }
25078 gdb_assert (low == high);
25079 this_cu = dwarf2_per_objfile->all_comp_units[low];
25080 cu_off = &this_cu->sect_off;
25081 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
25082 {
25083 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25084 error (_("Dwarf Error: could not find partial DIE containing "
25085 "offset %s [in module %s]"),
25086 sect_offset_str (sect_off),
25087 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25088
25089 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25090 <= sect_off);
25091 return dwarf2_per_objfile->all_comp_units[low-1];
25092 }
25093 else
25094 {
25095 this_cu = dwarf2_per_objfile->all_comp_units[low];
25096 if (low == dwarf2_per_objfile->n_comp_units - 1
25097 && sect_off >= this_cu->sect_off + this_cu->length)
25098 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25099 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25100 return this_cu;
25101 }
25102 }
25103
25104 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25105
25106 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25107 : per_cu (per_cu_),
25108 mark (0),
25109 has_loclist (0),
25110 checked_producer (0),
25111 producer_is_gxx_lt_4_6 (0),
25112 producer_is_gcc_lt_4_3 (0),
25113 producer_is_icc_lt_14 (0),
25114 processing_has_namespace_info (0)
25115 {
25116 per_cu->cu = this;
25117 }
25118
25119 /* Destroy a dwarf2_cu. */
25120
25121 dwarf2_cu::~dwarf2_cu ()
25122 {
25123 per_cu->cu = NULL;
25124 }
25125
25126 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25127
25128 static void
25129 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25130 enum language pretend_language)
25131 {
25132 struct attribute *attr;
25133
25134 /* Set the language we're debugging. */
25135 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25136 if (attr)
25137 set_cu_language (DW_UNSND (attr), cu);
25138 else
25139 {
25140 cu->language = pretend_language;
25141 cu->language_defn = language_def (cu->language);
25142 }
25143
25144 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25145 }
25146
25147 /* Free all cached compilation units. */
25148
25149 static void
25150 free_cached_comp_units (void *data)
25151 {
25152 struct dwarf2_per_objfile *dwarf2_per_objfile
25153 = (struct dwarf2_per_objfile *) data;
25154
25155 dwarf2_per_objfile->free_cached_comp_units ();
25156 }
25157
25158 /* Increase the age counter on each cached compilation unit, and free
25159 any that are too old. */
25160
25161 static void
25162 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25163 {
25164 struct dwarf2_per_cu_data *per_cu, **last_chain;
25165
25166 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25167 per_cu = dwarf2_per_objfile->read_in_chain;
25168 while (per_cu != NULL)
25169 {
25170 per_cu->cu->last_used ++;
25171 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25172 dwarf2_mark (per_cu->cu);
25173 per_cu = per_cu->cu->read_in_chain;
25174 }
25175
25176 per_cu = dwarf2_per_objfile->read_in_chain;
25177 last_chain = &dwarf2_per_objfile->read_in_chain;
25178 while (per_cu != NULL)
25179 {
25180 struct dwarf2_per_cu_data *next_cu;
25181
25182 next_cu = per_cu->cu->read_in_chain;
25183
25184 if (!per_cu->cu->mark)
25185 {
25186 delete per_cu->cu;
25187 *last_chain = next_cu;
25188 }
25189 else
25190 last_chain = &per_cu->cu->read_in_chain;
25191
25192 per_cu = next_cu;
25193 }
25194 }
25195
25196 /* Remove a single compilation unit from the cache. */
25197
25198 static void
25199 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25200 {
25201 struct dwarf2_per_cu_data *per_cu, **last_chain;
25202 struct dwarf2_per_objfile *dwarf2_per_objfile
25203 = target_per_cu->dwarf2_per_objfile;
25204
25205 per_cu = dwarf2_per_objfile->read_in_chain;
25206 last_chain = &dwarf2_per_objfile->read_in_chain;
25207 while (per_cu != NULL)
25208 {
25209 struct dwarf2_per_cu_data *next_cu;
25210
25211 next_cu = per_cu->cu->read_in_chain;
25212
25213 if (per_cu == target_per_cu)
25214 {
25215 delete per_cu->cu;
25216 per_cu->cu = NULL;
25217 *last_chain = next_cu;
25218 break;
25219 }
25220 else
25221 last_chain = &per_cu->cu->read_in_chain;
25222
25223 per_cu = next_cu;
25224 }
25225 }
25226
25227 /* Release all extra memory associated with OBJFILE. */
25228
25229 void
25230 dwarf2_free_objfile (struct objfile *objfile)
25231 {
25232 struct dwarf2_per_objfile *dwarf2_per_objfile
25233 = get_dwarf2_per_objfile (objfile);
25234
25235 delete dwarf2_per_objfile;
25236 }
25237
25238 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25239 We store these in a hash table separate from the DIEs, and preserve them
25240 when the DIEs are flushed out of cache.
25241
25242 The CU "per_cu" pointer is needed because offset alone is not enough to
25243 uniquely identify the type. A file may have multiple .debug_types sections,
25244 or the type may come from a DWO file. Furthermore, while it's more logical
25245 to use per_cu->section+offset, with Fission the section with the data is in
25246 the DWO file but we don't know that section at the point we need it.
25247 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25248 because we can enter the lookup routine, get_die_type_at_offset, from
25249 outside this file, and thus won't necessarily have PER_CU->cu.
25250 Fortunately, PER_CU is stable for the life of the objfile. */
25251
25252 struct dwarf2_per_cu_offset_and_type
25253 {
25254 const struct dwarf2_per_cu_data *per_cu;
25255 sect_offset sect_off;
25256 struct type *type;
25257 };
25258
25259 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25260
25261 static hashval_t
25262 per_cu_offset_and_type_hash (const void *item)
25263 {
25264 const struct dwarf2_per_cu_offset_and_type *ofs
25265 = (const struct dwarf2_per_cu_offset_and_type *) item;
25266
25267 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25268 }
25269
25270 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25271
25272 static int
25273 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25274 {
25275 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25276 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25277 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25278 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25279
25280 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25281 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25282 }
25283
25284 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25285 table if necessary. For convenience, return TYPE.
25286
25287 The DIEs reading must have careful ordering to:
25288 * Not cause infite loops trying to read in DIEs as a prerequisite for
25289 reading current DIE.
25290 * Not trying to dereference contents of still incompletely read in types
25291 while reading in other DIEs.
25292 * Enable referencing still incompletely read in types just by a pointer to
25293 the type without accessing its fields.
25294
25295 Therefore caller should follow these rules:
25296 * Try to fetch any prerequisite types we may need to build this DIE type
25297 before building the type and calling set_die_type.
25298 * After building type call set_die_type for current DIE as soon as
25299 possible before fetching more types to complete the current type.
25300 * Make the type as complete as possible before fetching more types. */
25301
25302 static struct type *
25303 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25304 {
25305 struct dwarf2_per_objfile *dwarf2_per_objfile
25306 = cu->per_cu->dwarf2_per_objfile;
25307 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25308 struct objfile *objfile = dwarf2_per_objfile->objfile;
25309 struct attribute *attr;
25310 struct dynamic_prop prop;
25311
25312 /* For Ada types, make sure that the gnat-specific data is always
25313 initialized (if not already set). There are a few types where
25314 we should not be doing so, because the type-specific area is
25315 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25316 where the type-specific area is used to store the floatformat).
25317 But this is not a problem, because the gnat-specific information
25318 is actually not needed for these types. */
25319 if (need_gnat_info (cu)
25320 && TYPE_CODE (type) != TYPE_CODE_FUNC
25321 && TYPE_CODE (type) != TYPE_CODE_FLT
25322 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25323 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25324 && TYPE_CODE (type) != TYPE_CODE_METHOD
25325 && !HAVE_GNAT_AUX_INFO (type))
25326 INIT_GNAT_SPECIFIC (type);
25327
25328 /* Read DW_AT_allocated and set in type. */
25329 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25330 if (attr_form_is_block (attr))
25331 {
25332 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25333 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25334 }
25335 else if (attr != NULL)
25336 {
25337 complaint (&symfile_complaints,
25338 _("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25339 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25340 sect_offset_str (die->sect_off));
25341 }
25342
25343 /* Read DW_AT_associated and set in type. */
25344 attr = dwarf2_attr (die, DW_AT_associated, cu);
25345 if (attr_form_is_block (attr))
25346 {
25347 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25348 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25349 }
25350 else if (attr != NULL)
25351 {
25352 complaint (&symfile_complaints,
25353 _("DW_AT_associated has the wrong form (%s) at DIE %s"),
25354 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25355 sect_offset_str (die->sect_off));
25356 }
25357
25358 /* Read DW_AT_data_location and set in type. */
25359 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25360 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25361 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25362
25363 if (dwarf2_per_objfile->die_type_hash == NULL)
25364 {
25365 dwarf2_per_objfile->die_type_hash =
25366 htab_create_alloc_ex (127,
25367 per_cu_offset_and_type_hash,
25368 per_cu_offset_and_type_eq,
25369 NULL,
25370 &objfile->objfile_obstack,
25371 hashtab_obstack_allocate,
25372 dummy_obstack_deallocate);
25373 }
25374
25375 ofs.per_cu = cu->per_cu;
25376 ofs.sect_off = die->sect_off;
25377 ofs.type = type;
25378 slot = (struct dwarf2_per_cu_offset_and_type **)
25379 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25380 if (*slot)
25381 complaint (&symfile_complaints,
25382 _("A problem internal to GDB: DIE %s has type already set"),
25383 sect_offset_str (die->sect_off));
25384 *slot = XOBNEW (&objfile->objfile_obstack,
25385 struct dwarf2_per_cu_offset_and_type);
25386 **slot = ofs;
25387 return type;
25388 }
25389
25390 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25391 or return NULL if the die does not have a saved type. */
25392
25393 static struct type *
25394 get_die_type_at_offset (sect_offset sect_off,
25395 struct dwarf2_per_cu_data *per_cu)
25396 {
25397 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25398 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25399
25400 if (dwarf2_per_objfile->die_type_hash == NULL)
25401 return NULL;
25402
25403 ofs.per_cu = per_cu;
25404 ofs.sect_off = sect_off;
25405 slot = ((struct dwarf2_per_cu_offset_and_type *)
25406 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25407 if (slot)
25408 return slot->type;
25409 else
25410 return NULL;
25411 }
25412
25413 /* Look up the type for DIE in CU in die_type_hash,
25414 or return NULL if DIE does not have a saved type. */
25415
25416 static struct type *
25417 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25418 {
25419 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25420 }
25421
25422 /* Add a dependence relationship from CU to REF_PER_CU. */
25423
25424 static void
25425 dwarf2_add_dependence (struct dwarf2_cu *cu,
25426 struct dwarf2_per_cu_data *ref_per_cu)
25427 {
25428 void **slot;
25429
25430 if (cu->dependencies == NULL)
25431 cu->dependencies
25432 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25433 NULL, &cu->comp_unit_obstack,
25434 hashtab_obstack_allocate,
25435 dummy_obstack_deallocate);
25436
25437 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25438 if (*slot == NULL)
25439 *slot = ref_per_cu;
25440 }
25441
25442 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25443 Set the mark field in every compilation unit in the
25444 cache that we must keep because we are keeping CU. */
25445
25446 static int
25447 dwarf2_mark_helper (void **slot, void *data)
25448 {
25449 struct dwarf2_per_cu_data *per_cu;
25450
25451 per_cu = (struct dwarf2_per_cu_data *) *slot;
25452
25453 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25454 reading of the chain. As such dependencies remain valid it is not much
25455 useful to track and undo them during QUIT cleanups. */
25456 if (per_cu->cu == NULL)
25457 return 1;
25458
25459 if (per_cu->cu->mark)
25460 return 1;
25461 per_cu->cu->mark = 1;
25462
25463 if (per_cu->cu->dependencies != NULL)
25464 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25465
25466 return 1;
25467 }
25468
25469 /* Set the mark field in CU and in every other compilation unit in the
25470 cache that we must keep because we are keeping CU. */
25471
25472 static void
25473 dwarf2_mark (struct dwarf2_cu *cu)
25474 {
25475 if (cu->mark)
25476 return;
25477 cu->mark = 1;
25478 if (cu->dependencies != NULL)
25479 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25480 }
25481
25482 static void
25483 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25484 {
25485 while (per_cu)
25486 {
25487 per_cu->cu->mark = 0;
25488 per_cu = per_cu->cu->read_in_chain;
25489 }
25490 }
25491
25492 /* Trivial hash function for partial_die_info: the hash value of a DIE
25493 is its offset in .debug_info for this objfile. */
25494
25495 static hashval_t
25496 partial_die_hash (const void *item)
25497 {
25498 const struct partial_die_info *part_die
25499 = (const struct partial_die_info *) item;
25500
25501 return to_underlying (part_die->sect_off);
25502 }
25503
25504 /* Trivial comparison function for partial_die_info structures: two DIEs
25505 are equal if they have the same offset. */
25506
25507 static int
25508 partial_die_eq (const void *item_lhs, const void *item_rhs)
25509 {
25510 const struct partial_die_info *part_die_lhs
25511 = (const struct partial_die_info *) item_lhs;
25512 const struct partial_die_info *part_die_rhs
25513 = (const struct partial_die_info *) item_rhs;
25514
25515 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25516 }
25517
25518 static struct cmd_list_element *set_dwarf_cmdlist;
25519 static struct cmd_list_element *show_dwarf_cmdlist;
25520
25521 static void
25522 set_dwarf_cmd (const char *args, int from_tty)
25523 {
25524 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25525 gdb_stdout);
25526 }
25527
25528 static void
25529 show_dwarf_cmd (const char *args, int from_tty)
25530 {
25531 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25532 }
25533
25534 /* The "save gdb-index" command. */
25535
25536 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25537 error checking. */
25538
25539 static void
25540 file_write (FILE *file, const void *data, size_t size)
25541 {
25542 if (fwrite (data, 1, size, file) != size)
25543 error (_("couldn't data write to file"));
25544 }
25545
25546 /* Write the contents of VEC to FILE, with error checking. */
25547
25548 template<typename Elem, typename Alloc>
25549 static void
25550 file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25551 {
25552 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25553 }
25554
25555 /* In-memory buffer to prepare data to be written later to a file. */
25556 class data_buf
25557 {
25558 public:
25559 /* Copy DATA to the end of the buffer. */
25560 template<typename T>
25561 void append_data (const T &data)
25562 {
25563 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25564 reinterpret_cast<const gdb_byte *> (&data + 1),
25565 grow (sizeof (data)));
25566 }
25567
25568 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25569 terminating zero is appended too. */
25570 void append_cstr0 (const char *cstr)
25571 {
25572 const size_t size = strlen (cstr) + 1;
25573 std::copy (cstr, cstr + size, grow (size));
25574 }
25575
25576 /* Store INPUT as ULEB128 to the end of buffer. */
25577 void append_unsigned_leb128 (ULONGEST input)
25578 {
25579 for (;;)
25580 {
25581 gdb_byte output = input & 0x7f;
25582 input >>= 7;
25583 if (input)
25584 output |= 0x80;
25585 append_data (output);
25586 if (input == 0)
25587 break;
25588 }
25589 }
25590
25591 /* Accept a host-format integer in VAL and append it to the buffer
25592 as a target-format integer which is LEN bytes long. */
25593 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25594 {
25595 ::store_unsigned_integer (grow (len), len, byte_order, val);
25596 }
25597
25598 /* Return the size of the buffer. */
25599 size_t size () const
25600 {
25601 return m_vec.size ();
25602 }
25603
25604 /* Return true iff the buffer is empty. */
25605 bool empty () const
25606 {
25607 return m_vec.empty ();
25608 }
25609
25610 /* Write the buffer to FILE. */
25611 void file_write (FILE *file) const
25612 {
25613 ::file_write (file, m_vec);
25614 }
25615
25616 private:
25617 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25618 the start of the new block. */
25619 gdb_byte *grow (size_t size)
25620 {
25621 m_vec.resize (m_vec.size () + size);
25622 return &*m_vec.end () - size;
25623 }
25624
25625 gdb::byte_vector m_vec;
25626 };
25627
25628 /* An entry in the symbol table. */
25629 struct symtab_index_entry
25630 {
25631 /* The name of the symbol. */
25632 const char *name;
25633 /* The offset of the name in the constant pool. */
25634 offset_type index_offset;
25635 /* A sorted vector of the indices of all the CUs that hold an object
25636 of this name. */
25637 std::vector<offset_type> cu_indices;
25638 };
25639
25640 /* The symbol table. This is a power-of-2-sized hash table. */
25641 struct mapped_symtab
25642 {
25643 mapped_symtab ()
25644 {
25645 data.resize (1024);
25646 }
25647
25648 offset_type n_elements = 0;
25649 std::vector<symtab_index_entry> data;
25650 };
25651
25652 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
25653 the slot.
25654
25655 Function is used only during write_hash_table so no index format backward
25656 compatibility is needed. */
25657
25658 static symtab_index_entry &
25659 find_slot (struct mapped_symtab *symtab, const char *name)
25660 {
25661 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
25662
25663 index = hash & (symtab->data.size () - 1);
25664 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
25665
25666 for (;;)
25667 {
25668 if (symtab->data[index].name == NULL
25669 || strcmp (name, symtab->data[index].name) == 0)
25670 return symtab->data[index];
25671 index = (index + step) & (symtab->data.size () - 1);
25672 }
25673 }
25674
25675 /* Expand SYMTAB's hash table. */
25676
25677 static void
25678 hash_expand (struct mapped_symtab *symtab)
25679 {
25680 auto old_entries = std::move (symtab->data);
25681
25682 symtab->data.clear ();
25683 symtab->data.resize (old_entries.size () * 2);
25684
25685 for (auto &it : old_entries)
25686 if (it.name != NULL)
25687 {
25688 auto &ref = find_slot (symtab, it.name);
25689 ref = std::move (it);
25690 }
25691 }
25692
25693 /* Add an entry to SYMTAB. NAME is the name of the symbol.
25694 CU_INDEX is the index of the CU in which the symbol appears.
25695 IS_STATIC is one if the symbol is static, otherwise zero (global). */
25696
25697 static void
25698 add_index_entry (struct mapped_symtab *symtab, const char *name,
25699 int is_static, gdb_index_symbol_kind kind,
25700 offset_type cu_index)
25701 {
25702 offset_type cu_index_and_attrs;
25703
25704 ++symtab->n_elements;
25705 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
25706 hash_expand (symtab);
25707
25708 symtab_index_entry &slot = find_slot (symtab, name);
25709 if (slot.name == NULL)
25710 {
25711 slot.name = name;
25712 /* index_offset is set later. */
25713 }
25714
25715 cu_index_and_attrs = 0;
25716 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
25717 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
25718 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
25719
25720 /* We don't want to record an index value twice as we want to avoid the
25721 duplication.
25722 We process all global symbols and then all static symbols
25723 (which would allow us to avoid the duplication by only having to check
25724 the last entry pushed), but a symbol could have multiple kinds in one CU.
25725 To keep things simple we don't worry about the duplication here and
25726 sort and uniqufy the list after we've processed all symbols. */
25727 slot.cu_indices.push_back (cu_index_and_attrs);
25728 }
25729
25730 /* Sort and remove duplicates of all symbols' cu_indices lists. */
25731
25732 static void
25733 uniquify_cu_indices (struct mapped_symtab *symtab)
25734 {
25735 for (auto &entry : symtab->data)
25736 {
25737 if (entry.name != NULL && !entry.cu_indices.empty ())
25738 {
25739 auto &cu_indices = entry.cu_indices;
25740 std::sort (cu_indices.begin (), cu_indices.end ());
25741 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
25742 cu_indices.erase (from, cu_indices.end ());
25743 }
25744 }
25745 }
25746
25747 /* A form of 'const char *' suitable for container keys. Only the
25748 pointer is stored. The strings themselves are compared, not the
25749 pointers. */
25750 class c_str_view
25751 {
25752 public:
25753 c_str_view (const char *cstr)
25754 : m_cstr (cstr)
25755 {}
25756
25757 bool operator== (const c_str_view &other) const
25758 {
25759 return strcmp (m_cstr, other.m_cstr) == 0;
25760 }
25761
25762 /* Return the underlying C string. Note, the returned string is
25763 only a reference with lifetime of this object. */
25764 const char *c_str () const
25765 {
25766 return m_cstr;
25767 }
25768
25769 private:
25770 friend class c_str_view_hasher;
25771 const char *const m_cstr;
25772 };
25773
25774 /* A std::unordered_map::hasher for c_str_view that uses the right
25775 hash function for strings in a mapped index. */
25776 class c_str_view_hasher
25777 {
25778 public:
25779 size_t operator () (const c_str_view &x) const
25780 {
25781 return mapped_index_string_hash (INT_MAX, x.m_cstr);
25782 }
25783 };
25784
25785 /* A std::unordered_map::hasher for std::vector<>. */
25786 template<typename T>
25787 class vector_hasher
25788 {
25789 public:
25790 size_t operator () (const std::vector<T> &key) const
25791 {
25792 return iterative_hash (key.data (),
25793 sizeof (key.front ()) * key.size (), 0);
25794 }
25795 };
25796
25797 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
25798 constant pool entries going into the data buffer CPOOL. */
25799
25800 static void
25801 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
25802 {
25803 {
25804 /* Elements are sorted vectors of the indices of all the CUs that
25805 hold an object of this name. */
25806 std::unordered_map<std::vector<offset_type>, offset_type,
25807 vector_hasher<offset_type>>
25808 symbol_hash_table;
25809
25810 /* We add all the index vectors to the constant pool first, to
25811 ensure alignment is ok. */
25812 for (symtab_index_entry &entry : symtab->data)
25813 {
25814 if (entry.name == NULL)
25815 continue;
25816 gdb_assert (entry.index_offset == 0);
25817
25818 /* Finding before inserting is faster than always trying to
25819 insert, because inserting always allocates a node, does the
25820 lookup, and then destroys the new node if another node
25821 already had the same key. C++17 try_emplace will avoid
25822 this. */
25823 const auto found
25824 = symbol_hash_table.find (entry.cu_indices);
25825 if (found != symbol_hash_table.end ())
25826 {
25827 entry.index_offset = found->second;
25828 continue;
25829 }
25830
25831 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
25832 entry.index_offset = cpool.size ();
25833 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
25834 for (const auto index : entry.cu_indices)
25835 cpool.append_data (MAYBE_SWAP (index));
25836 }
25837 }
25838
25839 /* Now write out the hash table. */
25840 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
25841 for (const auto &entry : symtab->data)
25842 {
25843 offset_type str_off, vec_off;
25844
25845 if (entry.name != NULL)
25846 {
25847 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
25848 if (insertpair.second)
25849 cpool.append_cstr0 (entry.name);
25850 str_off = insertpair.first->second;
25851 vec_off = entry.index_offset;
25852 }
25853 else
25854 {
25855 /* While 0 is a valid constant pool index, it is not valid
25856 to have 0 for both offsets. */
25857 str_off = 0;
25858 vec_off = 0;
25859 }
25860
25861 output.append_data (MAYBE_SWAP (str_off));
25862 output.append_data (MAYBE_SWAP (vec_off));
25863 }
25864 }
25865
25866 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
25867
25868 /* Helper struct for building the address table. */
25869 struct addrmap_index_data
25870 {
25871 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
25872 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
25873 {}
25874
25875 struct objfile *objfile;
25876 data_buf &addr_vec;
25877 psym_index_map &cu_index_htab;
25878
25879 /* Non-zero if the previous_* fields are valid.
25880 We can't write an entry until we see the next entry (since it is only then
25881 that we know the end of the entry). */
25882 int previous_valid;
25883 /* Index of the CU in the table of all CUs in the index file. */
25884 unsigned int previous_cu_index;
25885 /* Start address of the CU. */
25886 CORE_ADDR previous_cu_start;
25887 };
25888
25889 /* Write an address entry to ADDR_VEC. */
25890
25891 static void
25892 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
25893 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
25894 {
25895 CORE_ADDR baseaddr;
25896
25897 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25898
25899 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
25900 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
25901 addr_vec.append_data (MAYBE_SWAP (cu_index));
25902 }
25903
25904 /* Worker function for traversing an addrmap to build the address table. */
25905
25906 static int
25907 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
25908 {
25909 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
25910 struct partial_symtab *pst = (struct partial_symtab *) obj;
25911
25912 if (data->previous_valid)
25913 add_address_entry (data->objfile, data->addr_vec,
25914 data->previous_cu_start, start_addr,
25915 data->previous_cu_index);
25916
25917 data->previous_cu_start = start_addr;
25918 if (pst != NULL)
25919 {
25920 const auto it = data->cu_index_htab.find (pst);
25921 gdb_assert (it != data->cu_index_htab.cend ());
25922 data->previous_cu_index = it->second;
25923 data->previous_valid = 1;
25924 }
25925 else
25926 data->previous_valid = 0;
25927
25928 return 0;
25929 }
25930
25931 /* Write OBJFILE's address map to ADDR_VEC.
25932 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
25933 in the index file. */
25934
25935 static void
25936 write_address_map (struct objfile *objfile, data_buf &addr_vec,
25937 psym_index_map &cu_index_htab)
25938 {
25939 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
25940
25941 /* When writing the address table, we have to cope with the fact that
25942 the addrmap iterator only provides the start of a region; we have to
25943 wait until the next invocation to get the start of the next region. */
25944
25945 addrmap_index_data.objfile = objfile;
25946 addrmap_index_data.previous_valid = 0;
25947
25948 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
25949 &addrmap_index_data);
25950
25951 /* It's highly unlikely the last entry (end address = 0xff...ff)
25952 is valid, but we should still handle it.
25953 The end address is recorded as the start of the next region, but that
25954 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
25955 anyway. */
25956 if (addrmap_index_data.previous_valid)
25957 add_address_entry (objfile, addr_vec,
25958 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
25959 addrmap_index_data.previous_cu_index);
25960 }
25961
25962 /* Return the symbol kind of PSYM. */
25963
25964 static gdb_index_symbol_kind
25965 symbol_kind (struct partial_symbol *psym)
25966 {
25967 domain_enum domain = PSYMBOL_DOMAIN (psym);
25968 enum address_class aclass = PSYMBOL_CLASS (psym);
25969
25970 switch (domain)
25971 {
25972 case VAR_DOMAIN:
25973 switch (aclass)
25974 {
25975 case LOC_BLOCK:
25976 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
25977 case LOC_TYPEDEF:
25978 return GDB_INDEX_SYMBOL_KIND_TYPE;
25979 case LOC_COMPUTED:
25980 case LOC_CONST_BYTES:
25981 case LOC_OPTIMIZED_OUT:
25982 case LOC_STATIC:
25983 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25984 case LOC_CONST:
25985 /* Note: It's currently impossible to recognize psyms as enum values
25986 short of reading the type info. For now punt. */
25987 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25988 default:
25989 /* There are other LOC_FOO values that one might want to classify
25990 as variables, but dwarf2read.c doesn't currently use them. */
25991 return GDB_INDEX_SYMBOL_KIND_OTHER;
25992 }
25993 case STRUCT_DOMAIN:
25994 return GDB_INDEX_SYMBOL_KIND_TYPE;
25995 default:
25996 return GDB_INDEX_SYMBOL_KIND_OTHER;
25997 }
25998 }
25999
26000 /* Add a list of partial symbols to SYMTAB. */
26001
26002 static void
26003 write_psymbols (struct mapped_symtab *symtab,
26004 std::unordered_set<partial_symbol *> &psyms_seen,
26005 struct partial_symbol **psymp,
26006 int count,
26007 offset_type cu_index,
26008 int is_static)
26009 {
26010 for (; count-- > 0; ++psymp)
26011 {
26012 struct partial_symbol *psym = *psymp;
26013
26014 if (SYMBOL_LANGUAGE (psym) == language_ada)
26015 error (_("Ada is not currently supported by the index"));
26016
26017 /* Only add a given psymbol once. */
26018 if (psyms_seen.insert (psym).second)
26019 {
26020 gdb_index_symbol_kind kind = symbol_kind (psym);
26021
26022 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
26023 is_static, kind, cu_index);
26024 }
26025 }
26026 }
26027
26028 /* A helper struct used when iterating over debug_types. */
26029 struct signatured_type_index_data
26030 {
26031 signatured_type_index_data (data_buf &types_list_,
26032 std::unordered_set<partial_symbol *> &psyms_seen_)
26033 : types_list (types_list_), psyms_seen (psyms_seen_)
26034 {}
26035
26036 struct objfile *objfile;
26037 struct mapped_symtab *symtab;
26038 data_buf &types_list;
26039 std::unordered_set<partial_symbol *> &psyms_seen;
26040 int cu_index;
26041 };
26042
26043 /* A helper function that writes a single signatured_type to an
26044 obstack. */
26045
26046 static int
26047 write_one_signatured_type (void **slot, void *d)
26048 {
26049 struct signatured_type_index_data *info
26050 = (struct signatured_type_index_data *) d;
26051 struct signatured_type *entry = (struct signatured_type *) *slot;
26052 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26053
26054 write_psymbols (info->symtab,
26055 info->psyms_seen,
26056 &info->objfile->global_psymbols[psymtab->globals_offset],
26057 psymtab->n_global_syms, info->cu_index,
26058 0);
26059 write_psymbols (info->symtab,
26060 info->psyms_seen,
26061 &info->objfile->static_psymbols[psymtab->statics_offset],
26062 psymtab->n_static_syms, info->cu_index,
26063 1);
26064
26065 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26066 to_underlying (entry->per_cu.sect_off));
26067 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26068 to_underlying (entry->type_offset_in_tu));
26069 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
26070
26071 ++info->cu_index;
26072
26073 return 1;
26074 }
26075
26076 /* Recurse into all "included" dependencies and count their symbols as
26077 if they appeared in this psymtab. */
26078
26079 static void
26080 recursively_count_psymbols (struct partial_symtab *psymtab,
26081 size_t &psyms_seen)
26082 {
26083 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26084 if (psymtab->dependencies[i]->user != NULL)
26085 recursively_count_psymbols (psymtab->dependencies[i],
26086 psyms_seen);
26087
26088 psyms_seen += psymtab->n_global_syms;
26089 psyms_seen += psymtab->n_static_syms;
26090 }
26091
26092 /* Recurse into all "included" dependencies and write their symbols as
26093 if they appeared in this psymtab. */
26094
26095 static void
26096 recursively_write_psymbols (struct objfile *objfile,
26097 struct partial_symtab *psymtab,
26098 struct mapped_symtab *symtab,
26099 std::unordered_set<partial_symbol *> &psyms_seen,
26100 offset_type cu_index)
26101 {
26102 int i;
26103
26104 for (i = 0; i < psymtab->number_of_dependencies; ++i)
26105 if (psymtab->dependencies[i]->user != NULL)
26106 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26107 symtab, psyms_seen, cu_index);
26108
26109 write_psymbols (symtab,
26110 psyms_seen,
26111 &objfile->global_psymbols[psymtab->globals_offset],
26112 psymtab->n_global_syms, cu_index,
26113 0);
26114 write_psymbols (symtab,
26115 psyms_seen,
26116 &objfile->static_psymbols[psymtab->statics_offset],
26117 psymtab->n_static_syms, cu_index,
26118 1);
26119 }
26120
26121 /* DWARF-5 .debug_names builder. */
26122 class debug_names
26123 {
26124 public:
26125 debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile, bool is_dwarf64,
26126 bfd_endian dwarf5_byte_order)
26127 : m_dwarf5_byte_order (dwarf5_byte_order),
26128 m_dwarf32 (dwarf5_byte_order),
26129 m_dwarf64 (dwarf5_byte_order),
26130 m_dwarf (is_dwarf64
26131 ? static_cast<dwarf &> (m_dwarf64)
26132 : static_cast<dwarf &> (m_dwarf32)),
26133 m_name_table_string_offs (m_dwarf.name_table_string_offs),
26134 m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
26135 m_debugstrlookup (dwarf2_per_objfile)
26136 {}
26137
26138 int dwarf5_offset_size () const
26139 {
26140 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
26141 return dwarf5_is_dwarf64 ? 8 : 4;
26142 }
26143
26144 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
26145 enum class unit_kind { cu, tu };
26146
26147 /* Insert one symbol. */
26148 void insert (const partial_symbol *psym, int cu_index, bool is_static,
26149 unit_kind kind)
26150 {
26151 const int dwarf_tag = psymbol_tag (psym);
26152 if (dwarf_tag == 0)
26153 return;
26154 const char *const name = SYMBOL_SEARCH_NAME (psym);
26155 const auto insertpair
26156 = m_name_to_value_set.emplace (c_str_view (name),
26157 std::set<symbol_value> ());
26158 std::set<symbol_value> &value_set = insertpair.first->second;
26159 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
26160 }
26161
26162 /* Build all the tables. All symbols must be already inserted.
26163 This function does not call file_write, caller has to do it
26164 afterwards. */
26165 void build ()
26166 {
26167 /* Verify the build method has not be called twice. */
26168 gdb_assert (m_abbrev_table.empty ());
26169 const size_t name_count = m_name_to_value_set.size ();
26170 m_bucket_table.resize
26171 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26172 m_hash_table.reserve (name_count);
26173 m_name_table_string_offs.reserve (name_count);
26174 m_name_table_entry_offs.reserve (name_count);
26175
26176 /* Map each hash of symbol to its name and value. */
26177 struct hash_it_pair
26178 {
26179 uint32_t hash;
26180 decltype (m_name_to_value_set)::const_iterator it;
26181 };
26182 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26183 bucket_hash.resize (m_bucket_table.size ());
26184 for (decltype (m_name_to_value_set)::const_iterator it
26185 = m_name_to_value_set.cbegin ();
26186 it != m_name_to_value_set.cend ();
26187 ++it)
26188 {
26189 const char *const name = it->first.c_str ();
26190 const uint32_t hash = dwarf5_djb_hash (name);
26191 hash_it_pair hashitpair;
26192 hashitpair.hash = hash;
26193 hashitpair.it = it;
26194 auto &slot = bucket_hash[hash % bucket_hash.size()];
26195 slot.push_front (std::move (hashitpair));
26196 }
26197 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26198 {
26199 const std::forward_list<hash_it_pair> &hashitlist
26200 = bucket_hash[bucket_ix];
26201 if (hashitlist.empty ())
26202 continue;
26203 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26204 /* The hashes array is indexed starting at 1. */
26205 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26206 sizeof (bucket_slot), m_dwarf5_byte_order,
26207 m_hash_table.size () + 1);
26208 for (const hash_it_pair &hashitpair : hashitlist)
26209 {
26210 m_hash_table.push_back (0);
26211 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26212 (&m_hash_table.back ()),
26213 sizeof (m_hash_table.back ()),
26214 m_dwarf5_byte_order, hashitpair.hash);
26215 const c_str_view &name = hashitpair.it->first;
26216 const std::set<symbol_value> &value_set = hashitpair.it->second;
26217 m_name_table_string_offs.push_back_reorder
26218 (m_debugstrlookup.lookup (name.c_str ()));
26219 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26220 gdb_assert (!value_set.empty ());
26221 for (const symbol_value &value : value_set)
26222 {
26223 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
26224 value.is_static,
26225 value.kind)];
26226 if (idx == 0)
26227 {
26228 idx = m_idx_next++;
26229 m_abbrev_table.append_unsigned_leb128 (idx);
26230 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
26231 m_abbrev_table.append_unsigned_leb128
26232 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
26233 : DW_IDX_type_unit);
26234 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26235 m_abbrev_table.append_unsigned_leb128 (value.is_static
26236 ? DW_IDX_GNU_internal
26237 : DW_IDX_GNU_external);
26238 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26239
26240 /* Terminate attributes list. */
26241 m_abbrev_table.append_unsigned_leb128 (0);
26242 m_abbrev_table.append_unsigned_leb128 (0);
26243 }
26244
26245 m_entry_pool.append_unsigned_leb128 (idx);
26246 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26247 }
26248
26249 /* Terminate the list of CUs. */
26250 m_entry_pool.append_unsigned_leb128 (0);
26251 }
26252 }
26253 gdb_assert (m_hash_table.size () == name_count);
26254
26255 /* Terminate tags list. */
26256 m_abbrev_table.append_unsigned_leb128 (0);
26257 }
26258
26259 /* Return .debug_names bucket count. This must be called only after
26260 calling the build method. */
26261 uint32_t bucket_count () const
26262 {
26263 /* Verify the build method has been already called. */
26264 gdb_assert (!m_abbrev_table.empty ());
26265 const uint32_t retval = m_bucket_table.size ();
26266
26267 /* Check for overflow. */
26268 gdb_assert (retval == m_bucket_table.size ());
26269 return retval;
26270 }
26271
26272 /* Return .debug_names names count. This must be called only after
26273 calling the build method. */
26274 uint32_t name_count () const
26275 {
26276 /* Verify the build method has been already called. */
26277 gdb_assert (!m_abbrev_table.empty ());
26278 const uint32_t retval = m_hash_table.size ();
26279
26280 /* Check for overflow. */
26281 gdb_assert (retval == m_hash_table.size ());
26282 return retval;
26283 }
26284
26285 /* Return number of bytes of .debug_names abbreviation table. This
26286 must be called only after calling the build method. */
26287 uint32_t abbrev_table_bytes () const
26288 {
26289 gdb_assert (!m_abbrev_table.empty ());
26290 return m_abbrev_table.size ();
26291 }
26292
26293 /* Recurse into all "included" dependencies and store their symbols
26294 as if they appeared in this psymtab. */
26295 void recursively_write_psymbols
26296 (struct objfile *objfile,
26297 struct partial_symtab *psymtab,
26298 std::unordered_set<partial_symbol *> &psyms_seen,
26299 int cu_index)
26300 {
26301 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26302 if (psymtab->dependencies[i]->user != NULL)
26303 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26304 psyms_seen, cu_index);
26305
26306 write_psymbols (psyms_seen,
26307 &objfile->global_psymbols[psymtab->globals_offset],
26308 psymtab->n_global_syms, cu_index, false, unit_kind::cu);
26309 write_psymbols (psyms_seen,
26310 &objfile->static_psymbols[psymtab->statics_offset],
26311 psymtab->n_static_syms, cu_index, true, unit_kind::cu);
26312 }
26313
26314 /* Return number of bytes the .debug_names section will have. This
26315 must be called only after calling the build method. */
26316 size_t bytes () const
26317 {
26318 /* Verify the build method has been already called. */
26319 gdb_assert (!m_abbrev_table.empty ());
26320 size_t expected_bytes = 0;
26321 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26322 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26323 expected_bytes += m_name_table_string_offs.bytes ();
26324 expected_bytes += m_name_table_entry_offs.bytes ();
26325 expected_bytes += m_abbrev_table.size ();
26326 expected_bytes += m_entry_pool.size ();
26327 return expected_bytes;
26328 }
26329
26330 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26331 FILE_STR. This must be called only after calling the build
26332 method. */
26333 void file_write (FILE *file_names, FILE *file_str) const
26334 {
26335 /* Verify the build method has been already called. */
26336 gdb_assert (!m_abbrev_table.empty ());
26337 ::file_write (file_names, m_bucket_table);
26338 ::file_write (file_names, m_hash_table);
26339 m_name_table_string_offs.file_write (file_names);
26340 m_name_table_entry_offs.file_write (file_names);
26341 m_abbrev_table.file_write (file_names);
26342 m_entry_pool.file_write (file_names);
26343 m_debugstrlookup.file_write (file_str);
26344 }
26345
26346 /* A helper user data for write_one_signatured_type. */
26347 class write_one_signatured_type_data
26348 {
26349 public:
26350 write_one_signatured_type_data (debug_names &nametable_,
26351 signatured_type_index_data &&info_)
26352 : nametable (nametable_), info (std::move (info_))
26353 {}
26354 debug_names &nametable;
26355 struct signatured_type_index_data info;
26356 };
26357
26358 /* A helper function to pass write_one_signatured_type to
26359 htab_traverse_noresize. */
26360 static int
26361 write_one_signatured_type (void **slot, void *d)
26362 {
26363 write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
26364 struct signatured_type_index_data *info = &data->info;
26365 struct signatured_type *entry = (struct signatured_type *) *slot;
26366
26367 data->nametable.write_one_signatured_type (entry, info);
26368
26369 return 1;
26370 }
26371
26372 private:
26373
26374 /* Storage for symbol names mapping them to their .debug_str section
26375 offsets. */
26376 class debug_str_lookup
26377 {
26378 public:
26379
26380 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26381 All .debug_str section strings are automatically stored. */
26382 debug_str_lookup (struct dwarf2_per_objfile *dwarf2_per_objfile)
26383 : m_abfd (dwarf2_per_objfile->objfile->obfd),
26384 m_dwarf2_per_objfile (dwarf2_per_objfile)
26385 {
26386 dwarf2_read_section (dwarf2_per_objfile->objfile,
26387 &dwarf2_per_objfile->str);
26388 if (dwarf2_per_objfile->str.buffer == NULL)
26389 return;
26390 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26391 data < (dwarf2_per_objfile->str.buffer
26392 + dwarf2_per_objfile->str.size);)
26393 {
26394 const char *const s = reinterpret_cast<const char *> (data);
26395 const auto insertpair
26396 = m_str_table.emplace (c_str_view (s),
26397 data - dwarf2_per_objfile->str.buffer);
26398 if (!insertpair.second)
26399 complaint (&symfile_complaints,
26400 _("Duplicate string \"%s\" in "
26401 ".debug_str section [in module %s]"),
26402 s, bfd_get_filename (m_abfd));
26403 data += strlen (s) + 1;
26404 }
26405 }
26406
26407 /* Return offset of symbol name S in the .debug_str section. Add
26408 such symbol to the section's end if it does not exist there
26409 yet. */
26410 size_t lookup (const char *s)
26411 {
26412 const auto it = m_str_table.find (c_str_view (s));
26413 if (it != m_str_table.end ())
26414 return it->second;
26415 const size_t offset = (m_dwarf2_per_objfile->str.size
26416 + m_str_add_buf.size ());
26417 m_str_table.emplace (c_str_view (s), offset);
26418 m_str_add_buf.append_cstr0 (s);
26419 return offset;
26420 }
26421
26422 /* Append the end of the .debug_str section to FILE. */
26423 void file_write (FILE *file) const
26424 {
26425 m_str_add_buf.file_write (file);
26426 }
26427
26428 private:
26429 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26430 bfd *const m_abfd;
26431 struct dwarf2_per_objfile *m_dwarf2_per_objfile;
26432
26433 /* Data to add at the end of .debug_str for new needed symbol names. */
26434 data_buf m_str_add_buf;
26435 };
26436
26437 /* Container to map used DWARF tags to their .debug_names abbreviation
26438 tags. */
26439 class index_key
26440 {
26441 public:
26442 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
26443 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
26444 {
26445 }
26446
26447 bool
26448 operator== (const index_key &other) const
26449 {
26450 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
26451 && kind == other.kind);
26452 }
26453
26454 const int dwarf_tag;
26455 const bool is_static;
26456 const unit_kind kind;
26457 };
26458
26459 /* Provide std::unordered_map::hasher for index_key. */
26460 class index_key_hasher
26461 {
26462 public:
26463 size_t
26464 operator () (const index_key &key) const
26465 {
26466 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26467 }
26468 };
26469
26470 /* Parameters of one symbol entry. */
26471 class symbol_value
26472 {
26473 public:
26474 const int dwarf_tag, cu_index;
26475 const bool is_static;
26476 const unit_kind kind;
26477
26478 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
26479 unit_kind kind_)
26480 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
26481 kind (kind_)
26482 {}
26483
26484 bool
26485 operator< (const symbol_value &other) const
26486 {
26487 #define X(n) \
26488 do \
26489 { \
26490 if (n < other.n) \
26491 return true; \
26492 if (n > other.n) \
26493 return false; \
26494 } \
26495 while (0)
26496 X (dwarf_tag);
26497 X (is_static);
26498 X (kind);
26499 X (cu_index);
26500 #undef X
26501 return false;
26502 }
26503 };
26504
26505 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26506 output. */
26507 class offset_vec
26508 {
26509 protected:
26510 const bfd_endian dwarf5_byte_order;
26511 public:
26512 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26513 : dwarf5_byte_order (dwarf5_byte_order_)
26514 {}
26515
26516 /* Call std::vector::reserve for NELEM elements. */
26517 virtual void reserve (size_t nelem) = 0;
26518
26519 /* Call std::vector::push_back with store_unsigned_integer byte
26520 reordering for ELEM. */
26521 virtual void push_back_reorder (size_t elem) = 0;
26522
26523 /* Return expected output size in bytes. */
26524 virtual size_t bytes () const = 0;
26525
26526 /* Write name table to FILE. */
26527 virtual void file_write (FILE *file) const = 0;
26528 };
26529
26530 /* Template to unify DWARF-32 and DWARF-64 output. */
26531 template<typename OffsetSize>
26532 class offset_vec_tmpl : public offset_vec
26533 {
26534 public:
26535 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26536 : offset_vec (dwarf5_byte_order_)
26537 {}
26538
26539 /* Implement offset_vec::reserve. */
26540 void reserve (size_t nelem) override
26541 {
26542 m_vec.reserve (nelem);
26543 }
26544
26545 /* Implement offset_vec::push_back_reorder. */
26546 void push_back_reorder (size_t elem) override
26547 {
26548 m_vec.push_back (elem);
26549 /* Check for overflow. */
26550 gdb_assert (m_vec.back () == elem);
26551 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26552 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26553 }
26554
26555 /* Implement offset_vec::bytes. */
26556 size_t bytes () const override
26557 {
26558 return m_vec.size () * sizeof (m_vec[0]);
26559 }
26560
26561 /* Implement offset_vec::file_write. */
26562 void file_write (FILE *file) const override
26563 {
26564 ::file_write (file, m_vec);
26565 }
26566
26567 private:
26568 std::vector<OffsetSize> m_vec;
26569 };
26570
26571 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26572 respecting name table width. */
26573 class dwarf
26574 {
26575 public:
26576 offset_vec &name_table_string_offs, &name_table_entry_offs;
26577
26578 dwarf (offset_vec &name_table_string_offs_,
26579 offset_vec &name_table_entry_offs_)
26580 : name_table_string_offs (name_table_string_offs_),
26581 name_table_entry_offs (name_table_entry_offs_)
26582 {
26583 }
26584 };
26585
26586 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26587 respecting name table width. */
26588 template<typename OffsetSize>
26589 class dwarf_tmpl : public dwarf
26590 {
26591 public:
26592 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26593 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26594 m_name_table_string_offs (dwarf5_byte_order_),
26595 m_name_table_entry_offs (dwarf5_byte_order_)
26596 {}
26597
26598 private:
26599 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26600 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26601 };
26602
26603 /* Try to reconstruct original DWARF tag for given partial_symbol.
26604 This function is not DWARF-5 compliant but it is sufficient for
26605 GDB as a DWARF-5 index consumer. */
26606 static int psymbol_tag (const struct partial_symbol *psym)
26607 {
26608 domain_enum domain = PSYMBOL_DOMAIN (psym);
26609 enum address_class aclass = PSYMBOL_CLASS (psym);
26610
26611 switch (domain)
26612 {
26613 case VAR_DOMAIN:
26614 switch (aclass)
26615 {
26616 case LOC_BLOCK:
26617 return DW_TAG_subprogram;
26618 case LOC_TYPEDEF:
26619 return DW_TAG_typedef;
26620 case LOC_COMPUTED:
26621 case LOC_CONST_BYTES:
26622 case LOC_OPTIMIZED_OUT:
26623 case LOC_STATIC:
26624 return DW_TAG_variable;
26625 case LOC_CONST:
26626 /* Note: It's currently impossible to recognize psyms as enum values
26627 short of reading the type info. For now punt. */
26628 return DW_TAG_variable;
26629 default:
26630 /* There are other LOC_FOO values that one might want to classify
26631 as variables, but dwarf2read.c doesn't currently use them. */
26632 return DW_TAG_variable;
26633 }
26634 case STRUCT_DOMAIN:
26635 return DW_TAG_structure_type;
26636 default:
26637 return 0;
26638 }
26639 }
26640
26641 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26642 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
26643 struct partial_symbol **psymp, int count, int cu_index,
26644 bool is_static, unit_kind kind)
26645 {
26646 for (; count-- > 0; ++psymp)
26647 {
26648 struct partial_symbol *psym = *psymp;
26649
26650 if (SYMBOL_LANGUAGE (psym) == language_ada)
26651 error (_("Ada is not currently supported by the index"));
26652
26653 /* Only add a given psymbol once. */
26654 if (psyms_seen.insert (psym).second)
26655 insert (psym, cu_index, is_static, kind);
26656 }
26657 }
26658
26659 /* A helper function that writes a single signatured_type
26660 to a debug_names. */
26661 void
26662 write_one_signatured_type (struct signatured_type *entry,
26663 struct signatured_type_index_data *info)
26664 {
26665 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26666
26667 write_psymbols (info->psyms_seen,
26668 &info->objfile->global_psymbols[psymtab->globals_offset],
26669 psymtab->n_global_syms, info->cu_index, false,
26670 unit_kind::tu);
26671 write_psymbols (info->psyms_seen,
26672 &info->objfile->static_psymbols[psymtab->statics_offset],
26673 psymtab->n_static_syms, info->cu_index, true,
26674 unit_kind::tu);
26675
26676 info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
26677 to_underlying (entry->per_cu.sect_off));
26678
26679 ++info->cu_index;
26680 }
26681
26682 /* Store value of each symbol. */
26683 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
26684 m_name_to_value_set;
26685
26686 /* Tables of DWARF-5 .debug_names. They are in object file byte
26687 order. */
26688 std::vector<uint32_t> m_bucket_table;
26689 std::vector<uint32_t> m_hash_table;
26690
26691 const bfd_endian m_dwarf5_byte_order;
26692 dwarf_tmpl<uint32_t> m_dwarf32;
26693 dwarf_tmpl<uint64_t> m_dwarf64;
26694 dwarf &m_dwarf;
26695 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
26696 debug_str_lookup m_debugstrlookup;
26697
26698 /* Map each used .debug_names abbreviation tag parameter to its
26699 index value. */
26700 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
26701
26702 /* Next unused .debug_names abbreviation tag for
26703 m_indexkey_to_idx. */
26704 int m_idx_next = 1;
26705
26706 /* .debug_names abbreviation table. */
26707 data_buf m_abbrev_table;
26708
26709 /* .debug_names entry pool. */
26710 data_buf m_entry_pool;
26711 };
26712
26713 /* Return iff any of the needed offsets does not fit into 32-bit
26714 .debug_names section. */
26715
26716 static bool
26717 check_dwarf64_offsets (struct dwarf2_per_objfile *dwarf2_per_objfile)
26718 {
26719 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26720 {
26721 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
26722
26723 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26724 return true;
26725 }
26726 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
26727 {
26728 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
26729 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
26730
26731 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26732 return true;
26733 }
26734 return false;
26735 }
26736
26737 /* The psyms_seen set is potentially going to be largish (~40k
26738 elements when indexing a -g3 build of GDB itself). Estimate the
26739 number of elements in order to avoid too many rehashes, which
26740 require rebuilding buckets and thus many trips to
26741 malloc/free. */
26742
26743 static size_t
26744 psyms_seen_size (struct dwarf2_per_objfile *dwarf2_per_objfile)
26745 {
26746 size_t psyms_count = 0;
26747 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26748 {
26749 struct dwarf2_per_cu_data *per_cu
26750 = dwarf2_per_objfile->all_comp_units[i];
26751 struct partial_symtab *psymtab = per_cu->v.psymtab;
26752
26753 if (psymtab != NULL && psymtab->user == NULL)
26754 recursively_count_psymbols (psymtab, psyms_count);
26755 }
26756 /* Generating an index for gdb itself shows a ratio of
26757 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
26758 return psyms_count / 4;
26759 }
26760
26761 /* Write new .gdb_index section for OBJFILE into OUT_FILE.
26762 Return how many bytes were expected to be written into OUT_FILE. */
26763
26764 static size_t
26765 write_gdbindex (struct dwarf2_per_objfile *dwarf2_per_objfile, FILE *out_file)
26766 {
26767 struct objfile *objfile = dwarf2_per_objfile->objfile;
26768 mapped_symtab symtab;
26769 data_buf cu_list;
26770
26771 /* While we're scanning CU's create a table that maps a psymtab pointer
26772 (which is what addrmap records) to its index (which is what is recorded
26773 in the index file). This will later be needed to write the address
26774 table. */
26775 psym_index_map cu_index_htab;
26776 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
26777
26778 /* The CU list is already sorted, so we don't need to do additional
26779 work here. Also, the debug_types entries do not appear in
26780 all_comp_units, but only in their own hash table. */
26781
26782 std::unordered_set<partial_symbol *> psyms_seen
26783 (psyms_seen_size (dwarf2_per_objfile));
26784 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26785 {
26786 struct dwarf2_per_cu_data *per_cu
26787 = dwarf2_per_objfile->all_comp_units[i];
26788 struct partial_symtab *psymtab = per_cu->v.psymtab;
26789
26790 /* CU of a shared file from 'dwz -m' may be unused by this main file.
26791 It may be referenced from a local scope but in such case it does not
26792 need to be present in .gdb_index. */
26793 if (psymtab == NULL)
26794 continue;
26795
26796 if (psymtab->user == NULL)
26797 recursively_write_psymbols (objfile, psymtab, &symtab,
26798 psyms_seen, i);
26799
26800 const auto insertpair = cu_index_htab.emplace (psymtab, i);
26801 gdb_assert (insertpair.second);
26802
26803 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
26804 to_underlying (per_cu->sect_off));
26805 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
26806 }
26807
26808 /* Dump the address map. */
26809 data_buf addr_vec;
26810 write_address_map (objfile, addr_vec, cu_index_htab);
26811
26812 /* Write out the .debug_type entries, if any. */
26813 data_buf types_cu_list;
26814 if (dwarf2_per_objfile->signatured_types)
26815 {
26816 signatured_type_index_data sig_data (types_cu_list,
26817 psyms_seen);
26818
26819 sig_data.objfile = objfile;
26820 sig_data.symtab = &symtab;
26821 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
26822 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26823 write_one_signatured_type, &sig_data);
26824 }
26825
26826 /* Now that we've processed all symbols we can shrink their cu_indices
26827 lists. */
26828 uniquify_cu_indices (&symtab);
26829
26830 data_buf symtab_vec, constant_pool;
26831 write_hash_table (&symtab, symtab_vec, constant_pool);
26832
26833 data_buf contents;
26834 const offset_type size_of_contents = 6 * sizeof (offset_type);
26835 offset_type total_len = size_of_contents;
26836
26837 /* The version number. */
26838 contents.append_data (MAYBE_SWAP (8));
26839
26840 /* The offset of the CU list from the start of the file. */
26841 contents.append_data (MAYBE_SWAP (total_len));
26842 total_len += cu_list.size ();
26843
26844 /* The offset of the types CU list from the start of the file. */
26845 contents.append_data (MAYBE_SWAP (total_len));
26846 total_len += types_cu_list.size ();
26847
26848 /* The offset of the address table from the start of the file. */
26849 contents.append_data (MAYBE_SWAP (total_len));
26850 total_len += addr_vec.size ();
26851
26852 /* The offset of the symbol table from the start of the file. */
26853 contents.append_data (MAYBE_SWAP (total_len));
26854 total_len += symtab_vec.size ();
26855
26856 /* The offset of the constant pool from the start of the file. */
26857 contents.append_data (MAYBE_SWAP (total_len));
26858 total_len += constant_pool.size ();
26859
26860 gdb_assert (contents.size () == size_of_contents);
26861
26862 contents.file_write (out_file);
26863 cu_list.file_write (out_file);
26864 types_cu_list.file_write (out_file);
26865 addr_vec.file_write (out_file);
26866 symtab_vec.file_write (out_file);
26867 constant_pool.file_write (out_file);
26868
26869 return total_len;
26870 }
26871
26872 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
26873 static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
26874
26875 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
26876 needed addition to .debug_str section to OUT_FILE_STR. Return how
26877 many bytes were expected to be written into OUT_FILE. */
26878
26879 static size_t
26880 write_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
26881 FILE *out_file, FILE *out_file_str)
26882 {
26883 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (dwarf2_per_objfile);
26884 struct objfile *objfile = dwarf2_per_objfile->objfile;
26885 const enum bfd_endian dwarf5_byte_order
26886 = gdbarch_byte_order (get_objfile_arch (objfile));
26887
26888 /* The CU list is already sorted, so we don't need to do additional
26889 work here. Also, the debug_types entries do not appear in
26890 all_comp_units, but only in their own hash table. */
26891 data_buf cu_list;
26892 debug_names nametable (dwarf2_per_objfile, dwarf5_is_dwarf64,
26893 dwarf5_byte_order);
26894 std::unordered_set<partial_symbol *>
26895 psyms_seen (psyms_seen_size (dwarf2_per_objfile));
26896 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26897 {
26898 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
26899 partial_symtab *psymtab = per_cu->v.psymtab;
26900
26901 /* CU of a shared file from 'dwz -m' may be unused by this main
26902 file. It may be referenced from a local scope but in such
26903 case it does not need to be present in .debug_names. */
26904 if (psymtab == NULL)
26905 continue;
26906
26907 if (psymtab->user == NULL)
26908 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
26909
26910 cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
26911 to_underlying (per_cu->sect_off));
26912 }
26913
26914 /* Write out the .debug_type entries, if any. */
26915 data_buf types_cu_list;
26916 if (dwarf2_per_objfile->signatured_types)
26917 {
26918 debug_names::write_one_signatured_type_data sig_data (nametable,
26919 signatured_type_index_data (types_cu_list, psyms_seen));
26920
26921 sig_data.info.objfile = objfile;
26922 /* It is used only for gdb_index. */
26923 sig_data.info.symtab = nullptr;
26924 sig_data.info.cu_index = 0;
26925 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26926 debug_names::write_one_signatured_type,
26927 &sig_data);
26928 }
26929
26930 nametable.build ();
26931
26932 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
26933
26934 const offset_type bytes_of_header
26935 = ((dwarf5_is_dwarf64 ? 12 : 4)
26936 + 2 + 2 + 7 * 4
26937 + sizeof (dwarf5_gdb_augmentation));
26938 size_t expected_bytes = 0;
26939 expected_bytes += bytes_of_header;
26940 expected_bytes += cu_list.size ();
26941 expected_bytes += types_cu_list.size ();
26942 expected_bytes += nametable.bytes ();
26943 data_buf header;
26944
26945 if (!dwarf5_is_dwarf64)
26946 {
26947 const uint64_t size64 = expected_bytes - 4;
26948 gdb_assert (size64 < 0xfffffff0);
26949 header.append_uint (4, dwarf5_byte_order, size64);
26950 }
26951 else
26952 {
26953 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
26954 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
26955 }
26956
26957 /* The version number. */
26958 header.append_uint (2, dwarf5_byte_order, 5);
26959
26960 /* Padding. */
26961 header.append_uint (2, dwarf5_byte_order, 0);
26962
26963 /* comp_unit_count - The number of CUs in the CU list. */
26964 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
26965
26966 /* local_type_unit_count - The number of TUs in the local TU
26967 list. */
26968 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
26969
26970 /* foreign_type_unit_count - The number of TUs in the foreign TU
26971 list. */
26972 header.append_uint (4, dwarf5_byte_order, 0);
26973
26974 /* bucket_count - The number of hash buckets in the hash lookup
26975 table. */
26976 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
26977
26978 /* name_count - The number of unique names in the index. */
26979 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
26980
26981 /* abbrev_table_size - The size in bytes of the abbreviations
26982 table. */
26983 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
26984
26985 /* augmentation_string_size - The size in bytes of the augmentation
26986 string. This value is rounded up to a multiple of 4. */
26987 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
26988 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
26989 header.append_data (dwarf5_gdb_augmentation);
26990
26991 gdb_assert (header.size () == bytes_of_header);
26992
26993 header.file_write (out_file);
26994 cu_list.file_write (out_file);
26995 types_cu_list.file_write (out_file);
26996 nametable.file_write (out_file, out_file_str);
26997
26998 return expected_bytes;
26999 }
27000
27001 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
27002 position is at the end of the file. */
27003
27004 static void
27005 assert_file_size (FILE *file, const char *filename, size_t expected_size)
27006 {
27007 const auto file_size = ftell (file);
27008 if (file_size == -1)
27009 error (_("Can't get `%s' size"), filename);
27010 gdb_assert (file_size == expected_size);
27011 }
27012
27013 /* Create an index file for OBJFILE in the directory DIR. */
27014
27015 static void
27016 write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
27017 const char *dir,
27018 dw_index_kind index_kind)
27019 {
27020 struct objfile *objfile = dwarf2_per_objfile->objfile;
27021
27022 if (dwarf2_per_objfile->using_index)
27023 error (_("Cannot use an index to create the index"));
27024
27025 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
27026 error (_("Cannot make an index when the file has multiple .debug_types sections"));
27027
27028 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
27029 return;
27030
27031 struct stat st;
27032 if (stat (objfile_name (objfile), &st) < 0)
27033 perror_with_name (objfile_name (objfile));
27034
27035 std::string filename (std::string (dir) + SLASH_STRING
27036 + lbasename (objfile_name (objfile))
27037 + (index_kind == dw_index_kind::DEBUG_NAMES
27038 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
27039
27040 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
27041 if (!out_file)
27042 error (_("Can't open `%s' for writing"), filename.c_str ());
27043
27044 /* Order matters here; we want FILE to be closed before FILENAME is
27045 unlinked, because on MS-Windows one cannot delete a file that is
27046 still open. (Don't call anything here that might throw until
27047 file_closer is created.) */
27048 gdb::unlinker unlink_file (filename.c_str ());
27049 gdb_file_up close_out_file (out_file);
27050
27051 if (index_kind == dw_index_kind::DEBUG_NAMES)
27052 {
27053 std::string filename_str (std::string (dir) + SLASH_STRING
27054 + lbasename (objfile_name (objfile))
27055 + DEBUG_STR_SUFFIX);
27056 FILE *out_file_str
27057 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
27058 if (!out_file_str)
27059 error (_("Can't open `%s' for writing"), filename_str.c_str ());
27060 gdb::unlinker unlink_file_str (filename_str.c_str ());
27061 gdb_file_up close_out_file_str (out_file_str);
27062
27063 const size_t total_len
27064 = write_debug_names (dwarf2_per_objfile, out_file, out_file_str);
27065 assert_file_size (out_file, filename.c_str (), total_len);
27066
27067 /* We want to keep the file .debug_str file too. */
27068 unlink_file_str.keep ();
27069 }
27070 else
27071 {
27072 const size_t total_len
27073 = write_gdbindex (dwarf2_per_objfile, out_file);
27074 assert_file_size (out_file, filename.c_str (), total_len);
27075 }
27076
27077 /* We want to keep the file. */
27078 unlink_file.keep ();
27079 }
27080
27081 /* Implementation of the `save gdb-index' command.
27082
27083 Note that the .gdb_index file format used by this command is
27084 documented in the GDB manual. Any changes here must be documented
27085 there. */
27086
27087 static void
27088 save_gdb_index_command (const char *arg, int from_tty)
27089 {
27090 struct objfile *objfile;
27091 const char dwarf5space[] = "-dwarf-5 ";
27092 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
27093
27094 if (!arg)
27095 arg = "";
27096
27097 arg = skip_spaces (arg);
27098 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
27099 {
27100 index_kind = dw_index_kind::DEBUG_NAMES;
27101 arg += strlen (dwarf5space);
27102 arg = skip_spaces (arg);
27103 }
27104
27105 if (!*arg)
27106 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
27107
27108 ALL_OBJFILES (objfile)
27109 {
27110 struct stat st;
27111
27112 /* If the objfile does not correspond to an actual file, skip it. */
27113 if (stat (objfile_name (objfile), &st) < 0)
27114 continue;
27115
27116 struct dwarf2_per_objfile *dwarf2_per_objfile
27117 = get_dwarf2_per_objfile (objfile);
27118
27119 if (dwarf2_per_objfile != NULL)
27120 {
27121 TRY
27122 {
27123 write_psymtabs_to_index (dwarf2_per_objfile, arg, index_kind);
27124 }
27125 CATCH (except, RETURN_MASK_ERROR)
27126 {
27127 exception_fprintf (gdb_stderr, except,
27128 _("Error while writing index for `%s': "),
27129 objfile_name (objfile));
27130 }
27131 END_CATCH
27132 }
27133
27134 }
27135 }
27136
27137 \f
27138
27139 int dwarf_always_disassemble;
27140
27141 static void
27142 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
27143 struct cmd_list_element *c, const char *value)
27144 {
27145 fprintf_filtered (file,
27146 _("Whether to always disassemble "
27147 "DWARF expressions is %s.\n"),
27148 value);
27149 }
27150
27151 static void
27152 show_check_physname (struct ui_file *file, int from_tty,
27153 struct cmd_list_element *c, const char *value)
27154 {
27155 fprintf_filtered (file,
27156 _("Whether to check \"physname\" is %s.\n"),
27157 value);
27158 }
27159
27160 void
27161 _initialize_dwarf2_read (void)
27162 {
27163 struct cmd_list_element *c;
27164
27165 dwarf2_objfile_data_key = register_objfile_data ();
27166
27167 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
27168 Set DWARF specific variables.\n\
27169 Configure DWARF variables such as the cache size"),
27170 &set_dwarf_cmdlist, "maintenance set dwarf ",
27171 0/*allow-unknown*/, &maintenance_set_cmdlist);
27172
27173 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
27174 Show DWARF specific variables\n\
27175 Show DWARF variables such as the cache size"),
27176 &show_dwarf_cmdlist, "maintenance show dwarf ",
27177 0/*allow-unknown*/, &maintenance_show_cmdlist);
27178
27179 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
27180 &dwarf_max_cache_age, _("\
27181 Set the upper bound on the age of cached DWARF compilation units."), _("\
27182 Show the upper bound on the age of cached DWARF compilation units."), _("\
27183 A higher limit means that cached compilation units will be stored\n\
27184 in memory longer, and more total memory will be used. Zero disables\n\
27185 caching, which can slow down startup."),
27186 NULL,
27187 show_dwarf_max_cache_age,
27188 &set_dwarf_cmdlist,
27189 &show_dwarf_cmdlist);
27190
27191 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
27192 &dwarf_always_disassemble, _("\
27193 Set whether `info address' always disassembles DWARF expressions."), _("\
27194 Show whether `info address' always disassembles DWARF expressions."), _("\
27195 When enabled, DWARF expressions are always printed in an assembly-like\n\
27196 syntax. When disabled, expressions will be printed in a more\n\
27197 conversational style, when possible."),
27198 NULL,
27199 show_dwarf_always_disassemble,
27200 &set_dwarf_cmdlist,
27201 &show_dwarf_cmdlist);
27202
27203 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27204 Set debugging of the DWARF reader."), _("\
27205 Show debugging of the DWARF reader."), _("\
27206 When enabled (non-zero), debugging messages are printed during DWARF\n\
27207 reading and symtab expansion. A value of 1 (one) provides basic\n\
27208 information. A value greater than 1 provides more verbose information."),
27209 NULL,
27210 NULL,
27211 &setdebuglist, &showdebuglist);
27212
27213 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27214 Set debugging of the DWARF DIE reader."), _("\
27215 Show debugging of the DWARF DIE reader."), _("\
27216 When enabled (non-zero), DIEs are dumped after they are read in.\n\
27217 The value is the maximum depth to print."),
27218 NULL,
27219 NULL,
27220 &setdebuglist, &showdebuglist);
27221
27222 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27223 Set debugging of the dwarf line reader."), _("\
27224 Show debugging of the dwarf line reader."), _("\
27225 When enabled (non-zero), line number entries are dumped as they are read in.\n\
27226 A value of 1 (one) provides basic information.\n\
27227 A value greater than 1 provides more verbose information."),
27228 NULL,
27229 NULL,
27230 &setdebuglist, &showdebuglist);
27231
27232 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27233 Set cross-checking of \"physname\" code against demangler."), _("\
27234 Show cross-checking of \"physname\" code against demangler."), _("\
27235 When enabled, GDB's internal \"physname\" code is checked against\n\
27236 the demangler."),
27237 NULL, show_check_physname,
27238 &setdebuglist, &showdebuglist);
27239
27240 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27241 no_class, &use_deprecated_index_sections, _("\
27242 Set whether to use deprecated gdb_index sections."), _("\
27243 Show whether to use deprecated gdb_index sections."), _("\
27244 When enabled, deprecated .gdb_index sections are used anyway.\n\
27245 Normally they are ignored either because of a missing feature or\n\
27246 performance issue.\n\
27247 Warning: This option must be enabled before gdb reads the file."),
27248 NULL,
27249 NULL,
27250 &setlist, &showlist);
27251
27252 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
27253 _("\
27254 Save a gdb-index file.\n\
27255 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27256 \n\
27257 No options create one file with .gdb-index extension for pre-DWARF-5\n\
27258 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27259 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
27260 &save_cmdlist);
27261 set_cmd_completer (c, filename_completer);
27262
27263 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27264 &dwarf2_locexpr_funcs);
27265 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27266 &dwarf2_loclist_funcs);
27267
27268 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27269 &dwarf2_block_frame_base_locexpr_funcs);
27270 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27271 &dwarf2_block_frame_base_loclist_funcs);
27272
27273 #if GDB_SELF_TEST
27274 selftests::register_test ("dw2_expand_symtabs_matching",
27275 selftests::dw2_expand_symtabs_matching::run_test);
27276 #endif
27277 }
This page took 0.61268 seconds and 5 git commands to generate.