New class allocate_on_obstack
[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
790 /* Persistent data held for a compilation unit, even when not
791 processing it. We put a pointer to this structure in the
792 read_symtab_private field of the psymtab. */
793
794 struct dwarf2_per_cu_data
795 {
796 /* The start offset and length of this compilation unit.
797 NOTE: Unlike comp_unit_head.length, this length includes
798 initial_length_size.
799 If the DIE refers to a DWO file, this is always of the original die,
800 not the DWO file. */
801 sect_offset sect_off;
802 unsigned int length;
803
804 /* DWARF standard version this data has been read from (such as 4 or 5). */
805 short dwarf_version;
806
807 /* Flag indicating this compilation unit will be read in before
808 any of the current compilation units are processed. */
809 unsigned int queued : 1;
810
811 /* This flag will be set when reading partial DIEs if we need to load
812 absolutely all DIEs for this compilation unit, instead of just the ones
813 we think are interesting. It gets set if we look for a DIE in the
814 hash table and don't find it. */
815 unsigned int load_all_dies : 1;
816
817 /* Non-zero if this CU is from .debug_types.
818 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
819 this is non-zero. */
820 unsigned int is_debug_types : 1;
821
822 /* Non-zero if this CU is from the .dwz file. */
823 unsigned int is_dwz : 1;
824
825 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
826 This flag is only valid if is_debug_types is true.
827 We can't read a CU directly from a DWO file: There are required
828 attributes in the stub. */
829 unsigned int reading_dwo_directly : 1;
830
831 /* Non-zero if the TU has been read.
832 This is used to assist the "Stay in DWO Optimization" for Fission:
833 When reading a DWO, it's faster to read TUs from the DWO instead of
834 fetching them from random other DWOs (due to comdat folding).
835 If the TU has already been read, the optimization is unnecessary
836 (and unwise - we don't want to change where gdb thinks the TU lives
837 "midflight").
838 This flag is only valid if is_debug_types is true. */
839 unsigned int tu_read : 1;
840
841 /* The section this CU/TU lives in.
842 If the DIE refers to a DWO file, this is always the original die,
843 not the DWO file. */
844 struct dwarf2_section_info *section;
845
846 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
847 of the CU cache it gets reset to NULL again. This is left as NULL for
848 dummy CUs (a CU header, but nothing else). */
849 struct dwarf2_cu *cu;
850
851 /* The corresponding dwarf2_per_objfile. */
852 struct dwarf2_per_objfile *dwarf2_per_objfile;
853
854 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
855 is active. Otherwise, the 'psymtab' field is active. */
856 union
857 {
858 /* The partial symbol table associated with this compilation unit,
859 or NULL for unread partial units. */
860 struct partial_symtab *psymtab;
861
862 /* Data needed by the "quick" functions. */
863 struct dwarf2_per_cu_quick_data *quick;
864 } v;
865
866 /* The CUs we import using DW_TAG_imported_unit. This is filled in
867 while reading psymtabs, used to compute the psymtab dependencies,
868 and then cleared. Then it is filled in again while reading full
869 symbols, and only deleted when the objfile is destroyed.
870
871 This is also used to work around a difference between the way gold
872 generates .gdb_index version <=7 and the way gdb does. Arguably this
873 is a gold bug. For symbols coming from TUs, gold records in the index
874 the CU that includes the TU instead of the TU itself. This breaks
875 dw2_lookup_symbol: It assumes that if the index says symbol X lives
876 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
877 will find X. Alas TUs live in their own symtab, so after expanding CU Y
878 we need to look in TU Z to find X. Fortunately, this is akin to
879 DW_TAG_imported_unit, so we just use the same mechanism: For
880 .gdb_index version <=7 this also records the TUs that the CU referred
881 to. Concurrently with this change gdb was modified to emit version 8
882 indices so we only pay a price for gold generated indices.
883 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
884 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
885 };
886
887 /* Entry in the signatured_types hash table. */
888
889 struct signatured_type
890 {
891 /* The "per_cu" object of this type.
892 This struct is used iff per_cu.is_debug_types.
893 N.B.: This is the first member so that it's easy to convert pointers
894 between them. */
895 struct dwarf2_per_cu_data per_cu;
896
897 /* The type's signature. */
898 ULONGEST signature;
899
900 /* Offset in the TU of the type's DIE, as read from the TU header.
901 If this TU is a DWO stub and the definition lives in a DWO file
902 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
903 cu_offset type_offset_in_tu;
904
905 /* Offset in the section of the type's DIE.
906 If the definition lives in a DWO file, this is the offset in the
907 .debug_types.dwo section.
908 The value is zero until the actual value is known.
909 Zero is otherwise not a valid section offset. */
910 sect_offset type_offset_in_section;
911
912 /* Type units are grouped by their DW_AT_stmt_list entry so that they
913 can share them. This points to the containing symtab. */
914 struct type_unit_group *type_unit_group;
915
916 /* The type.
917 The first time we encounter this type we fully read it in and install it
918 in the symbol tables. Subsequent times we only need the type. */
919 struct type *type;
920
921 /* Containing DWO unit.
922 This field is valid iff per_cu.reading_dwo_directly. */
923 struct dwo_unit *dwo_unit;
924 };
925
926 typedef struct signatured_type *sig_type_ptr;
927 DEF_VEC_P (sig_type_ptr);
928
929 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
930 This includes type_unit_group and quick_file_names. */
931
932 struct stmt_list_hash
933 {
934 /* The DWO unit this table is from or NULL if there is none. */
935 struct dwo_unit *dwo_unit;
936
937 /* Offset in .debug_line or .debug_line.dwo. */
938 sect_offset line_sect_off;
939 };
940
941 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
942 an object of this type. */
943
944 struct type_unit_group
945 {
946 /* dwarf2read.c's main "handle" on a TU symtab.
947 To simplify things we create an artificial CU that "includes" all the
948 type units using this stmt_list so that the rest of the code still has
949 a "per_cu" handle on the symtab.
950 This PER_CU is recognized by having no section. */
951 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
952 struct dwarf2_per_cu_data per_cu;
953
954 /* The TUs that share this DW_AT_stmt_list entry.
955 This is added to while parsing type units to build partial symtabs,
956 and is deleted afterwards and not used again. */
957 VEC (sig_type_ptr) *tus;
958
959 /* The compunit symtab.
960 Type units in a group needn't all be defined in the same source file,
961 so we create an essentially anonymous symtab as the compunit symtab. */
962 struct compunit_symtab *compunit_symtab;
963
964 /* The data used to construct the hash key. */
965 struct stmt_list_hash hash;
966
967 /* The number of symtabs from the line header.
968 The value here must match line_header.num_file_names. */
969 unsigned int num_symtabs;
970
971 /* The symbol tables for this TU (obtained from the files listed in
972 DW_AT_stmt_list).
973 WARNING: The order of entries here must match the order of entries
974 in the line header. After the first TU using this type_unit_group, the
975 line header for the subsequent TUs is recreated from this. This is done
976 because we need to use the same symtabs for each TU using the same
977 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
978 there's no guarantee the line header doesn't have duplicate entries. */
979 struct symtab **symtabs;
980 };
981
982 /* These sections are what may appear in a (real or virtual) DWO file. */
983
984 struct dwo_sections
985 {
986 struct dwarf2_section_info abbrev;
987 struct dwarf2_section_info line;
988 struct dwarf2_section_info loc;
989 struct dwarf2_section_info loclists;
990 struct dwarf2_section_info macinfo;
991 struct dwarf2_section_info macro;
992 struct dwarf2_section_info str;
993 struct dwarf2_section_info str_offsets;
994 /* In the case of a virtual DWO file, these two are unused. */
995 struct dwarf2_section_info info;
996 VEC (dwarf2_section_info_def) *types;
997 };
998
999 /* CUs/TUs in DWP/DWO files. */
1000
1001 struct dwo_unit
1002 {
1003 /* Backlink to the containing struct dwo_file. */
1004 struct dwo_file *dwo_file;
1005
1006 /* The "id" that distinguishes this CU/TU.
1007 .debug_info calls this "dwo_id", .debug_types calls this "signature".
1008 Since signatures came first, we stick with it for consistency. */
1009 ULONGEST signature;
1010
1011 /* The section this CU/TU lives in, in the DWO file. */
1012 struct dwarf2_section_info *section;
1013
1014 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
1015 sect_offset sect_off;
1016 unsigned int length;
1017
1018 /* For types, offset in the type's DIE of the type defined by this TU. */
1019 cu_offset type_offset_in_tu;
1020 };
1021
1022 /* include/dwarf2.h defines the DWP section codes.
1023 It defines a max value but it doesn't define a min value, which we
1024 use for error checking, so provide one. */
1025
1026 enum dwp_v2_section_ids
1027 {
1028 DW_SECT_MIN = 1
1029 };
1030
1031 /* Data for one DWO file.
1032
1033 This includes virtual DWO files (a virtual DWO file is a DWO file as it
1034 appears in a DWP file). DWP files don't really have DWO files per se -
1035 comdat folding of types "loses" the DWO file they came from, and from
1036 a high level view DWP files appear to contain a mass of random types.
1037 However, to maintain consistency with the non-DWP case we pretend DWP
1038 files contain virtual DWO files, and we assign each TU with one virtual
1039 DWO file (generally based on the line and abbrev section offsets -
1040 a heuristic that seems to work in practice). */
1041
1042 struct dwo_file
1043 {
1044 /* The DW_AT_GNU_dwo_name attribute.
1045 For virtual DWO files the name is constructed from the section offsets
1046 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
1047 from related CU+TUs. */
1048 const char *dwo_name;
1049
1050 /* The DW_AT_comp_dir attribute. */
1051 const char *comp_dir;
1052
1053 /* The bfd, when the file is open. Otherwise this is NULL.
1054 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
1055 bfd *dbfd;
1056
1057 /* The sections that make up this DWO file.
1058 Remember that for virtual DWO files in DWP V2, these are virtual
1059 sections (for lack of a better name). */
1060 struct dwo_sections sections;
1061
1062 /* The CUs in the file.
1063 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
1064 an extension to handle LLVM's Link Time Optimization output (where
1065 multiple source files may be compiled into a single object/dwo pair). */
1066 htab_t cus;
1067
1068 /* Table of TUs in the file.
1069 Each element is a struct dwo_unit. */
1070 htab_t tus;
1071 };
1072
1073 /* These sections are what may appear in a DWP file. */
1074
1075 struct dwp_sections
1076 {
1077 /* These are used by both DWP version 1 and 2. */
1078 struct dwarf2_section_info str;
1079 struct dwarf2_section_info cu_index;
1080 struct dwarf2_section_info tu_index;
1081
1082 /* These are only used by DWP version 2 files.
1083 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
1084 sections are referenced by section number, and are not recorded here.
1085 In DWP version 2 there is at most one copy of all these sections, each
1086 section being (effectively) comprised of the concatenation of all of the
1087 individual sections that exist in the version 1 format.
1088 To keep the code simple we treat each of these concatenated pieces as a
1089 section itself (a virtual section?). */
1090 struct dwarf2_section_info abbrev;
1091 struct dwarf2_section_info info;
1092 struct dwarf2_section_info line;
1093 struct dwarf2_section_info loc;
1094 struct dwarf2_section_info macinfo;
1095 struct dwarf2_section_info macro;
1096 struct dwarf2_section_info str_offsets;
1097 struct dwarf2_section_info types;
1098 };
1099
1100 /* These sections are what may appear in a virtual DWO file in DWP version 1.
1101 A virtual DWO file is a DWO file as it appears in a DWP file. */
1102
1103 struct virtual_v1_dwo_sections
1104 {
1105 struct dwarf2_section_info abbrev;
1106 struct dwarf2_section_info line;
1107 struct dwarf2_section_info loc;
1108 struct dwarf2_section_info macinfo;
1109 struct dwarf2_section_info macro;
1110 struct dwarf2_section_info str_offsets;
1111 /* Each DWP hash table entry records one CU or one TU.
1112 That is recorded here, and copied to dwo_unit.section. */
1113 struct dwarf2_section_info info_or_types;
1114 };
1115
1116 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1117 In version 2, the sections of the DWO files are concatenated together
1118 and stored in one section of that name. Thus each ELF section contains
1119 several "virtual" sections. */
1120
1121 struct virtual_v2_dwo_sections
1122 {
1123 bfd_size_type abbrev_offset;
1124 bfd_size_type abbrev_size;
1125
1126 bfd_size_type line_offset;
1127 bfd_size_type line_size;
1128
1129 bfd_size_type loc_offset;
1130 bfd_size_type loc_size;
1131
1132 bfd_size_type macinfo_offset;
1133 bfd_size_type macinfo_size;
1134
1135 bfd_size_type macro_offset;
1136 bfd_size_type macro_size;
1137
1138 bfd_size_type str_offsets_offset;
1139 bfd_size_type str_offsets_size;
1140
1141 /* Each DWP hash table entry records one CU or one TU.
1142 That is recorded here, and copied to dwo_unit.section. */
1143 bfd_size_type info_or_types_offset;
1144 bfd_size_type info_or_types_size;
1145 };
1146
1147 /* Contents of DWP hash tables. */
1148
1149 struct dwp_hash_table
1150 {
1151 uint32_t version, nr_columns;
1152 uint32_t nr_units, nr_slots;
1153 const gdb_byte *hash_table, *unit_table;
1154 union
1155 {
1156 struct
1157 {
1158 const gdb_byte *indices;
1159 } v1;
1160 struct
1161 {
1162 /* This is indexed by column number and gives the id of the section
1163 in that column. */
1164 #define MAX_NR_V2_DWO_SECTIONS \
1165 (1 /* .debug_info or .debug_types */ \
1166 + 1 /* .debug_abbrev */ \
1167 + 1 /* .debug_line */ \
1168 + 1 /* .debug_loc */ \
1169 + 1 /* .debug_str_offsets */ \
1170 + 1 /* .debug_macro or .debug_macinfo */)
1171 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1172 const gdb_byte *offsets;
1173 const gdb_byte *sizes;
1174 } v2;
1175 } section_pool;
1176 };
1177
1178 /* Data for one DWP file. */
1179
1180 struct dwp_file
1181 {
1182 /* Name of the file. */
1183 const char *name;
1184
1185 /* File format version. */
1186 int version;
1187
1188 /* The bfd. */
1189 bfd *dbfd;
1190
1191 /* Section info for this file. */
1192 struct dwp_sections sections;
1193
1194 /* Table of CUs in the file. */
1195 const struct dwp_hash_table *cus;
1196
1197 /* Table of TUs in the file. */
1198 const struct dwp_hash_table *tus;
1199
1200 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1201 htab_t loaded_cus;
1202 htab_t loaded_tus;
1203
1204 /* Table to map ELF section numbers to their sections.
1205 This is only needed for the DWP V1 file format. */
1206 unsigned int num_sections;
1207 asection **elf_sections;
1208 };
1209
1210 /* This represents a '.dwz' file. */
1211
1212 struct dwz_file
1213 {
1214 /* A dwz file can only contain a few sections. */
1215 struct dwarf2_section_info abbrev;
1216 struct dwarf2_section_info info;
1217 struct dwarf2_section_info str;
1218 struct dwarf2_section_info line;
1219 struct dwarf2_section_info macro;
1220 struct dwarf2_section_info gdb_index;
1221 struct dwarf2_section_info debug_names;
1222
1223 /* The dwz's BFD. */
1224 bfd *dwz_bfd;
1225 };
1226
1227 /* Struct used to pass misc. parameters to read_die_and_children, et
1228 al. which are used for both .debug_info and .debug_types dies.
1229 All parameters here are unchanging for the life of the call. This
1230 struct exists to abstract away the constant parameters of die reading. */
1231
1232 struct die_reader_specs
1233 {
1234 /* The bfd of die_section. */
1235 bfd* abfd;
1236
1237 /* The CU of the DIE we are parsing. */
1238 struct dwarf2_cu *cu;
1239
1240 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1241 struct dwo_file *dwo_file;
1242
1243 /* The section the die comes from.
1244 This is either .debug_info or .debug_types, or the .dwo variants. */
1245 struct dwarf2_section_info *die_section;
1246
1247 /* die_section->buffer. */
1248 const gdb_byte *buffer;
1249
1250 /* The end of the buffer. */
1251 const gdb_byte *buffer_end;
1252
1253 /* The value of the DW_AT_comp_dir attribute. */
1254 const char *comp_dir;
1255
1256 /* The abbreviation table to use when reading the DIEs. */
1257 struct abbrev_table *abbrev_table;
1258 };
1259
1260 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1261 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1262 const gdb_byte *info_ptr,
1263 struct die_info *comp_unit_die,
1264 int has_children,
1265 void *data);
1266
1267 /* A 1-based directory index. This is a strong typedef to prevent
1268 accidentally using a directory index as a 0-based index into an
1269 array/vector. */
1270 enum class dir_index : unsigned int {};
1271
1272 /* Likewise, a 1-based file name index. */
1273 enum class file_name_index : unsigned int {};
1274
1275 struct file_entry
1276 {
1277 file_entry () = default;
1278
1279 file_entry (const char *name_, dir_index d_index_,
1280 unsigned int mod_time_, unsigned int length_)
1281 : name (name_),
1282 d_index (d_index_),
1283 mod_time (mod_time_),
1284 length (length_)
1285 {}
1286
1287 /* Return the include directory at D_INDEX stored in LH. Returns
1288 NULL if D_INDEX is out of bounds. */
1289 const char *include_dir (const line_header *lh) const;
1290
1291 /* The file name. Note this is an observing pointer. The memory is
1292 owned by debug_line_buffer. */
1293 const char *name {};
1294
1295 /* The directory index (1-based). */
1296 dir_index d_index {};
1297
1298 unsigned int mod_time {};
1299
1300 unsigned int length {};
1301
1302 /* True if referenced by the Line Number Program. */
1303 bool included_p {};
1304
1305 /* The associated symbol table, if any. */
1306 struct symtab *symtab {};
1307 };
1308
1309 /* The line number information for a compilation unit (found in the
1310 .debug_line section) begins with a "statement program header",
1311 which contains the following information. */
1312 struct line_header
1313 {
1314 line_header ()
1315 : offset_in_dwz {}
1316 {}
1317
1318 /* Add an entry to the include directory table. */
1319 void add_include_dir (const char *include_dir);
1320
1321 /* Add an entry to the file name table. */
1322 void add_file_name (const char *name, dir_index d_index,
1323 unsigned int mod_time, unsigned int length);
1324
1325 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1326 is out of bounds. */
1327 const char *include_dir_at (dir_index index) const
1328 {
1329 /* Convert directory index number (1-based) to vector index
1330 (0-based). */
1331 size_t vec_index = to_underlying (index) - 1;
1332
1333 if (vec_index >= include_dirs.size ())
1334 return NULL;
1335 return include_dirs[vec_index];
1336 }
1337
1338 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1339 is out of bounds. */
1340 file_entry *file_name_at (file_name_index index)
1341 {
1342 /* Convert file name index number (1-based) to vector index
1343 (0-based). */
1344 size_t vec_index = to_underlying (index) - 1;
1345
1346 if (vec_index >= file_names.size ())
1347 return NULL;
1348 return &file_names[vec_index];
1349 }
1350
1351 /* Const version of the above. */
1352 const file_entry *file_name_at (unsigned int index) const
1353 {
1354 if (index >= file_names.size ())
1355 return NULL;
1356 return &file_names[index];
1357 }
1358
1359 /* Offset of line number information in .debug_line section. */
1360 sect_offset sect_off {};
1361
1362 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1363 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1364
1365 unsigned int total_length {};
1366 unsigned short version {};
1367 unsigned int header_length {};
1368 unsigned char minimum_instruction_length {};
1369 unsigned char maximum_ops_per_instruction {};
1370 unsigned char default_is_stmt {};
1371 int line_base {};
1372 unsigned char line_range {};
1373 unsigned char opcode_base {};
1374
1375 /* standard_opcode_lengths[i] is the number of operands for the
1376 standard opcode whose value is i. This means that
1377 standard_opcode_lengths[0] is unused, and the last meaningful
1378 element is standard_opcode_lengths[opcode_base - 1]. */
1379 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1380
1381 /* The include_directories table. Note these are observing
1382 pointers. The memory is owned by debug_line_buffer. */
1383 std::vector<const char *> include_dirs;
1384
1385 /* The file_names table. */
1386 std::vector<file_entry> file_names;
1387
1388 /* The start and end of the statement program following this
1389 header. These point into dwarf2_per_objfile->line_buffer. */
1390 const gdb_byte *statement_program_start {}, *statement_program_end {};
1391 };
1392
1393 typedef std::unique_ptr<line_header> line_header_up;
1394
1395 const char *
1396 file_entry::include_dir (const line_header *lh) const
1397 {
1398 return lh->include_dir_at (d_index);
1399 }
1400
1401 /* When we construct a partial symbol table entry we only
1402 need this much information. */
1403 struct partial_die_info
1404 {
1405 /* Offset of this DIE. */
1406 sect_offset sect_off;
1407
1408 /* DWARF-2 tag for this DIE. */
1409 ENUM_BITFIELD(dwarf_tag) tag : 16;
1410
1411 /* Assorted flags describing the data found in this DIE. */
1412 unsigned int has_children : 1;
1413 unsigned int is_external : 1;
1414 unsigned int is_declaration : 1;
1415 unsigned int has_type : 1;
1416 unsigned int has_specification : 1;
1417 unsigned int has_pc_info : 1;
1418 unsigned int may_be_inlined : 1;
1419
1420 /* This DIE has been marked DW_AT_main_subprogram. */
1421 unsigned int main_subprogram : 1;
1422
1423 /* Flag set if the SCOPE field of this structure has been
1424 computed. */
1425 unsigned int scope_set : 1;
1426
1427 /* Flag set if the DIE has a byte_size attribute. */
1428 unsigned int has_byte_size : 1;
1429
1430 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1431 unsigned int has_const_value : 1;
1432
1433 /* Flag set if any of the DIE's children are template arguments. */
1434 unsigned int has_template_arguments : 1;
1435
1436 /* Flag set if fixup_partial_die has been called on this die. */
1437 unsigned int fixup_called : 1;
1438
1439 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1440 unsigned int is_dwz : 1;
1441
1442 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1443 unsigned int spec_is_dwz : 1;
1444
1445 /* The name of this DIE. Normally the value of DW_AT_name, but
1446 sometimes a default name for unnamed DIEs. */
1447 const char *name;
1448
1449 /* The linkage name, if present. */
1450 const char *linkage_name;
1451
1452 /* The scope to prepend to our children. This is generally
1453 allocated on the comp_unit_obstack, so will disappear
1454 when this compilation unit leaves the cache. */
1455 const char *scope;
1456
1457 /* Some data associated with the partial DIE. The tag determines
1458 which field is live. */
1459 union
1460 {
1461 /* The location description associated with this DIE, if any. */
1462 struct dwarf_block *locdesc;
1463 /* The offset of an import, for DW_TAG_imported_unit. */
1464 sect_offset sect_off;
1465 } d;
1466
1467 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1468 CORE_ADDR lowpc;
1469 CORE_ADDR highpc;
1470
1471 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1472 DW_AT_sibling, if any. */
1473 /* NOTE: This member isn't strictly necessary, read_partial_die could
1474 return DW_AT_sibling values to its caller load_partial_dies. */
1475 const gdb_byte *sibling;
1476
1477 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1478 DW_AT_specification (or DW_AT_abstract_origin or
1479 DW_AT_extension). */
1480 sect_offset spec_offset;
1481
1482 /* Pointers to this DIE's parent, first child, and next sibling,
1483 if any. */
1484 struct partial_die_info *die_parent, *die_child, *die_sibling;
1485 };
1486
1487 /* This data structure holds the information of an abbrev. */
1488 struct abbrev_info
1489 {
1490 unsigned int number; /* number identifying abbrev */
1491 enum dwarf_tag tag; /* dwarf tag */
1492 unsigned short has_children; /* boolean */
1493 unsigned short num_attrs; /* number of attributes */
1494 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1495 struct abbrev_info *next; /* next in chain */
1496 };
1497
1498 struct attr_abbrev
1499 {
1500 ENUM_BITFIELD(dwarf_attribute) name : 16;
1501 ENUM_BITFIELD(dwarf_form) form : 16;
1502
1503 /* It is valid only if FORM is DW_FORM_implicit_const. */
1504 LONGEST implicit_const;
1505 };
1506
1507 /* Size of abbrev_table.abbrev_hash_table. */
1508 #define ABBREV_HASH_SIZE 121
1509
1510 /* Top level data structure to contain an abbreviation table. */
1511
1512 struct abbrev_table
1513 {
1514 explicit abbrev_table (sect_offset off)
1515 : sect_off (off)
1516 {
1517 m_abbrevs =
1518 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1519 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1520 }
1521
1522 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1523
1524 /* Allocate space for a struct abbrev_info object in
1525 ABBREV_TABLE. */
1526 struct abbrev_info *alloc_abbrev ();
1527
1528 /* Add an abbreviation to the table. */
1529 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1530
1531 /* Look up an abbrev in the table.
1532 Returns NULL if the abbrev is not found. */
1533
1534 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1535
1536
1537 /* Where the abbrev table came from.
1538 This is used as a sanity check when the table is used. */
1539 const sect_offset sect_off;
1540
1541 /* Storage for the abbrev table. */
1542 auto_obstack abbrev_obstack;
1543
1544 private:
1545
1546 /* Hash table of abbrevs.
1547 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1548 It could be statically allocated, but the previous code didn't so we
1549 don't either. */
1550 struct abbrev_info **m_abbrevs;
1551 };
1552
1553 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1554
1555 /* Attributes have a name and a value. */
1556 struct attribute
1557 {
1558 ENUM_BITFIELD(dwarf_attribute) name : 16;
1559 ENUM_BITFIELD(dwarf_form) form : 15;
1560
1561 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1562 field should be in u.str (existing only for DW_STRING) but it is kept
1563 here for better struct attribute alignment. */
1564 unsigned int string_is_canonical : 1;
1565
1566 union
1567 {
1568 const char *str;
1569 struct dwarf_block *blk;
1570 ULONGEST unsnd;
1571 LONGEST snd;
1572 CORE_ADDR addr;
1573 ULONGEST signature;
1574 }
1575 u;
1576 };
1577
1578 /* This data structure holds a complete die structure. */
1579 struct die_info
1580 {
1581 /* DWARF-2 tag for this DIE. */
1582 ENUM_BITFIELD(dwarf_tag) tag : 16;
1583
1584 /* Number of attributes */
1585 unsigned char num_attrs;
1586
1587 /* True if we're presently building the full type name for the
1588 type derived from this DIE. */
1589 unsigned char building_fullname : 1;
1590
1591 /* True if this die is in process. PR 16581. */
1592 unsigned char in_process : 1;
1593
1594 /* Abbrev number */
1595 unsigned int abbrev;
1596
1597 /* Offset in .debug_info or .debug_types section. */
1598 sect_offset sect_off;
1599
1600 /* The dies in a compilation unit form an n-ary tree. PARENT
1601 points to this die's parent; CHILD points to the first child of
1602 this node; and all the children of a given node are chained
1603 together via their SIBLING fields. */
1604 struct die_info *child; /* Its first child, if any. */
1605 struct die_info *sibling; /* Its next sibling, if any. */
1606 struct die_info *parent; /* Its parent, if any. */
1607
1608 /* An array of attributes, with NUM_ATTRS elements. There may be
1609 zero, but it's not common and zero-sized arrays are not
1610 sufficiently portable C. */
1611 struct attribute attrs[1];
1612 };
1613
1614 /* Get at parts of an attribute structure. */
1615
1616 #define DW_STRING(attr) ((attr)->u.str)
1617 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1618 #define DW_UNSND(attr) ((attr)->u.unsnd)
1619 #define DW_BLOCK(attr) ((attr)->u.blk)
1620 #define DW_SND(attr) ((attr)->u.snd)
1621 #define DW_ADDR(attr) ((attr)->u.addr)
1622 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1623
1624 /* Blocks are a bunch of untyped bytes. */
1625 struct dwarf_block
1626 {
1627 size_t size;
1628
1629 /* Valid only if SIZE is not zero. */
1630 const gdb_byte *data;
1631 };
1632
1633 #ifndef ATTR_ALLOC_CHUNK
1634 #define ATTR_ALLOC_CHUNK 4
1635 #endif
1636
1637 /* Allocate fields for structs, unions and enums in this size. */
1638 #ifndef DW_FIELD_ALLOC_CHUNK
1639 #define DW_FIELD_ALLOC_CHUNK 4
1640 #endif
1641
1642 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1643 but this would require a corresponding change in unpack_field_as_long
1644 and friends. */
1645 static int bits_per_byte = 8;
1646
1647 struct nextfield
1648 {
1649 struct nextfield *next;
1650 int accessibility;
1651 int virtuality;
1652 struct field field;
1653 };
1654
1655 struct nextfnfield
1656 {
1657 struct nextfnfield *next;
1658 struct fn_field fnfield;
1659 };
1660
1661 struct fnfieldlist
1662 {
1663 const char *name;
1664 int length;
1665 struct nextfnfield *head;
1666 };
1667
1668 struct decl_field_list
1669 {
1670 struct decl_field field;
1671 struct decl_field_list *next;
1672 };
1673
1674 /* The routines that read and process dies for a C struct or C++ class
1675 pass lists of data member fields and lists of member function fields
1676 in an instance of a field_info structure, as defined below. */
1677 struct field_info
1678 {
1679 /* List of data member and baseclasses fields. */
1680 struct nextfield *fields, *baseclasses;
1681
1682 /* Number of fields (including baseclasses). */
1683 int nfields;
1684
1685 /* Number of baseclasses. */
1686 int nbaseclasses;
1687
1688 /* Set if the accesibility of one of the fields is not public. */
1689 int non_public_fields;
1690
1691 /* Member function fieldlist array, contains name of possibly overloaded
1692 member function, number of overloaded member functions and a pointer
1693 to the head of the member function field chain. */
1694 struct fnfieldlist *fnfieldlists;
1695
1696 /* Number of entries in the fnfieldlists array. */
1697 int nfnfields;
1698
1699 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1700 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1701 struct decl_field_list *typedef_field_list;
1702 unsigned typedef_field_list_count;
1703
1704 /* Nested types defined by this class and the number of elements in this
1705 list. */
1706 struct decl_field_list *nested_types_list;
1707 unsigned nested_types_list_count;
1708 };
1709
1710 /* One item on the queue of compilation units to read in full symbols
1711 for. */
1712 struct dwarf2_queue_item
1713 {
1714 struct dwarf2_per_cu_data *per_cu;
1715 enum language pretend_language;
1716 struct dwarf2_queue_item *next;
1717 };
1718
1719 /* The current queue. */
1720 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1721
1722 /* Loaded secondary compilation units are kept in memory until they
1723 have not been referenced for the processing of this many
1724 compilation units. Set this to zero to disable caching. Cache
1725 sizes of up to at least twenty will improve startup time for
1726 typical inter-CU-reference binaries, at an obvious memory cost. */
1727 static int dwarf_max_cache_age = 5;
1728 static void
1729 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1730 struct cmd_list_element *c, const char *value)
1731 {
1732 fprintf_filtered (file, _("The upper bound on the age of cached "
1733 "DWARF compilation units is %s.\n"),
1734 value);
1735 }
1736 \f
1737 /* local function prototypes */
1738
1739 static const char *get_section_name (const struct dwarf2_section_info *);
1740
1741 static const char *get_section_file_name (const struct dwarf2_section_info *);
1742
1743 static void dwarf2_find_base_address (struct die_info *die,
1744 struct dwarf2_cu *cu);
1745
1746 static struct partial_symtab *create_partial_symtab
1747 (struct dwarf2_per_cu_data *per_cu, const char *name);
1748
1749 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1750 const gdb_byte *info_ptr,
1751 struct die_info *type_unit_die,
1752 int has_children, void *data);
1753
1754 static void dwarf2_build_psymtabs_hard
1755 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1756
1757 static void scan_partial_symbols (struct partial_die_info *,
1758 CORE_ADDR *, CORE_ADDR *,
1759 int, struct dwarf2_cu *);
1760
1761 static void add_partial_symbol (struct partial_die_info *,
1762 struct dwarf2_cu *);
1763
1764 static void add_partial_namespace (struct partial_die_info *pdi,
1765 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1766 int set_addrmap, struct dwarf2_cu *cu);
1767
1768 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1769 CORE_ADDR *highpc, int set_addrmap,
1770 struct dwarf2_cu *cu);
1771
1772 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1773 struct dwarf2_cu *cu);
1774
1775 static void add_partial_subprogram (struct partial_die_info *pdi,
1776 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1777 int need_pc, struct dwarf2_cu *cu);
1778
1779 static void dwarf2_read_symtab (struct partial_symtab *,
1780 struct objfile *);
1781
1782 static void psymtab_to_symtab_1 (struct partial_symtab *);
1783
1784 static abbrev_table_up abbrev_table_read_table
1785 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1786 sect_offset);
1787
1788 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1789
1790 static struct partial_die_info *load_partial_dies
1791 (const struct die_reader_specs *, const gdb_byte *, int);
1792
1793 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1794 struct partial_die_info *,
1795 struct abbrev_info *,
1796 unsigned int,
1797 const gdb_byte *);
1798
1799 static struct partial_die_info *find_partial_die (sect_offset, int,
1800 struct dwarf2_cu *);
1801
1802 static void fixup_partial_die (struct partial_die_info *,
1803 struct dwarf2_cu *);
1804
1805 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1806 struct attribute *, struct attr_abbrev *,
1807 const gdb_byte *);
1808
1809 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1810
1811 static int read_1_signed_byte (bfd *, const gdb_byte *);
1812
1813 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1814
1815 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1816
1817 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1818
1819 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1820 unsigned int *);
1821
1822 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1823
1824 static LONGEST read_checked_initial_length_and_offset
1825 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1826 unsigned int *, unsigned int *);
1827
1828 static LONGEST read_offset (bfd *, const gdb_byte *,
1829 const struct comp_unit_head *,
1830 unsigned int *);
1831
1832 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1833
1834 static sect_offset read_abbrev_offset
1835 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1836 struct dwarf2_section_info *, sect_offset);
1837
1838 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1839
1840 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1841
1842 static const char *read_indirect_string
1843 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1844 const struct comp_unit_head *, unsigned int *);
1845
1846 static const char *read_indirect_line_string
1847 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1848 const struct comp_unit_head *, unsigned int *);
1849
1850 static const char *read_indirect_string_at_offset
1851 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1852 LONGEST str_offset);
1853
1854 static const char *read_indirect_string_from_dwz
1855 (struct objfile *objfile, struct dwz_file *, LONGEST);
1856
1857 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1858
1859 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1860 const gdb_byte *,
1861 unsigned int *);
1862
1863 static const char *read_str_index (const struct die_reader_specs *reader,
1864 ULONGEST str_index);
1865
1866 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1867
1868 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1869 struct dwarf2_cu *);
1870
1871 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1872 unsigned int);
1873
1874 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1875 struct dwarf2_cu *cu);
1876
1877 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1878 struct dwarf2_cu *cu);
1879
1880 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1881
1882 static struct die_info *die_specification (struct die_info *die,
1883 struct dwarf2_cu **);
1884
1885 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1886 struct dwarf2_cu *cu);
1887
1888 static void dwarf_decode_lines (struct line_header *, const char *,
1889 struct dwarf2_cu *, struct partial_symtab *,
1890 CORE_ADDR, int decode_mapping);
1891
1892 static void dwarf2_start_subfile (const char *, const char *);
1893
1894 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1895 const char *, const char *,
1896 CORE_ADDR);
1897
1898 static struct symbol *new_symbol (struct die_info *, struct type *,
1899 struct dwarf2_cu *, struct symbol * = NULL);
1900
1901 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1902 struct dwarf2_cu *);
1903
1904 static void dwarf2_const_value_attr (const struct attribute *attr,
1905 struct type *type,
1906 const char *name,
1907 struct obstack *obstack,
1908 struct dwarf2_cu *cu, LONGEST *value,
1909 const gdb_byte **bytes,
1910 struct dwarf2_locexpr_baton **baton);
1911
1912 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1913
1914 static int need_gnat_info (struct dwarf2_cu *);
1915
1916 static struct type *die_descriptive_type (struct die_info *,
1917 struct dwarf2_cu *);
1918
1919 static void set_descriptive_type (struct type *, struct die_info *,
1920 struct dwarf2_cu *);
1921
1922 static struct type *die_containing_type (struct die_info *,
1923 struct dwarf2_cu *);
1924
1925 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1926 struct dwarf2_cu *);
1927
1928 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1929
1930 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1931
1932 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1933
1934 static char *typename_concat (struct obstack *obs, const char *prefix,
1935 const char *suffix, int physname,
1936 struct dwarf2_cu *cu);
1937
1938 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1939
1940 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1941
1942 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1943
1944 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1945
1946 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1947
1948 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1949
1950 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1951 struct dwarf2_cu *, struct partial_symtab *);
1952
1953 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1954 values. Keep the items ordered with increasing constraints compliance. */
1955 enum pc_bounds_kind
1956 {
1957 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1958 PC_BOUNDS_NOT_PRESENT,
1959
1960 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1961 were present but they do not form a valid range of PC addresses. */
1962 PC_BOUNDS_INVALID,
1963
1964 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1965 PC_BOUNDS_RANGES,
1966
1967 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1968 PC_BOUNDS_HIGH_LOW,
1969 };
1970
1971 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1972 CORE_ADDR *, CORE_ADDR *,
1973 struct dwarf2_cu *,
1974 struct partial_symtab *);
1975
1976 static void get_scope_pc_bounds (struct die_info *,
1977 CORE_ADDR *, CORE_ADDR *,
1978 struct dwarf2_cu *);
1979
1980 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1981 CORE_ADDR, struct dwarf2_cu *);
1982
1983 static void dwarf2_add_field (struct field_info *, struct die_info *,
1984 struct dwarf2_cu *);
1985
1986 static void dwarf2_attach_fields_to_type (struct field_info *,
1987 struct type *, struct dwarf2_cu *);
1988
1989 static void dwarf2_add_member_fn (struct field_info *,
1990 struct die_info *, struct type *,
1991 struct dwarf2_cu *);
1992
1993 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1994 struct type *,
1995 struct dwarf2_cu *);
1996
1997 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1998
1999 static void read_common_block (struct die_info *, struct dwarf2_cu *);
2000
2001 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
2002
2003 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
2004
2005 static struct using_direct **using_directives (enum language);
2006
2007 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
2008
2009 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
2010
2011 static struct type *read_module_type (struct die_info *die,
2012 struct dwarf2_cu *cu);
2013
2014 static const char *namespace_name (struct die_info *die,
2015 int *is_anonymous, struct dwarf2_cu *);
2016
2017 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
2018
2019 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
2020
2021 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
2022 struct dwarf2_cu *);
2023
2024 static struct die_info *read_die_and_siblings_1
2025 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
2026 struct die_info *);
2027
2028 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
2029 const gdb_byte *info_ptr,
2030 const gdb_byte **new_info_ptr,
2031 struct die_info *parent);
2032
2033 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
2034 struct die_info **, const gdb_byte *,
2035 int *, int);
2036
2037 static const gdb_byte *read_full_die (const struct die_reader_specs *,
2038 struct die_info **, const gdb_byte *,
2039 int *);
2040
2041 static void process_die (struct die_info *, struct dwarf2_cu *);
2042
2043 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
2044 struct obstack *);
2045
2046 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
2047
2048 static const char *dwarf2_full_name (const char *name,
2049 struct die_info *die,
2050 struct dwarf2_cu *cu);
2051
2052 static const char *dwarf2_physname (const char *name, struct die_info *die,
2053 struct dwarf2_cu *cu);
2054
2055 static struct die_info *dwarf2_extension (struct die_info *die,
2056 struct dwarf2_cu **);
2057
2058 static const char *dwarf_tag_name (unsigned int);
2059
2060 static const char *dwarf_attr_name (unsigned int);
2061
2062 static const char *dwarf_form_name (unsigned int);
2063
2064 static const char *dwarf_bool_name (unsigned int);
2065
2066 static const char *dwarf_type_encoding_name (unsigned int);
2067
2068 static struct die_info *sibling_die (struct die_info *);
2069
2070 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
2071
2072 static void dump_die_for_error (struct die_info *);
2073
2074 static void dump_die_1 (struct ui_file *, int level, int max_level,
2075 struct die_info *);
2076
2077 /*static*/ void dump_die (struct die_info *, int max_level);
2078
2079 static void store_in_ref_table (struct die_info *,
2080 struct dwarf2_cu *);
2081
2082 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
2083
2084 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
2085
2086 static struct die_info *follow_die_ref_or_sig (struct die_info *,
2087 const struct attribute *,
2088 struct dwarf2_cu **);
2089
2090 static struct die_info *follow_die_ref (struct die_info *,
2091 const struct attribute *,
2092 struct dwarf2_cu **);
2093
2094 static struct die_info *follow_die_sig (struct die_info *,
2095 const struct attribute *,
2096 struct dwarf2_cu **);
2097
2098 static struct type *get_signatured_type (struct die_info *, ULONGEST,
2099 struct dwarf2_cu *);
2100
2101 static struct type *get_DW_AT_signature_type (struct die_info *,
2102 const struct attribute *,
2103 struct dwarf2_cu *);
2104
2105 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
2106
2107 static void read_signatured_type (struct signatured_type *);
2108
2109 static int attr_to_dynamic_prop (const struct attribute *attr,
2110 struct die_info *die, struct dwarf2_cu *cu,
2111 struct dynamic_prop *prop);
2112
2113 /* memory allocation interface */
2114
2115 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
2116
2117 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
2118
2119 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2120
2121 static int attr_form_is_block (const struct attribute *);
2122
2123 static int attr_form_is_section_offset (const struct attribute *);
2124
2125 static int attr_form_is_constant (const struct attribute *);
2126
2127 static int attr_form_is_ref (const struct attribute *);
2128
2129 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2130 struct dwarf2_loclist_baton *baton,
2131 const struct attribute *attr);
2132
2133 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
2134 struct symbol *sym,
2135 struct dwarf2_cu *cu,
2136 int is_block);
2137
2138 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2139 const gdb_byte *info_ptr,
2140 struct abbrev_info *abbrev);
2141
2142 static hashval_t partial_die_hash (const void *item);
2143
2144 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2145
2146 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
2147 (sect_offset sect_off, unsigned int offset_in_dwz,
2148 struct dwarf2_per_objfile *dwarf2_per_objfile);
2149
2150 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
2151 struct die_info *comp_unit_die,
2152 enum language pretend_language);
2153
2154 static void free_cached_comp_units (void *);
2155
2156 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2157
2158 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
2159
2160 static struct type *set_die_type (struct die_info *, struct type *,
2161 struct dwarf2_cu *);
2162
2163 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2164
2165 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2166
2167 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2168 enum language);
2169
2170 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2171 enum language);
2172
2173 static void process_full_type_unit (struct dwarf2_per_cu_data *,
2174 enum language);
2175
2176 static void dwarf2_add_dependence (struct dwarf2_cu *,
2177 struct dwarf2_per_cu_data *);
2178
2179 static void dwarf2_mark (struct dwarf2_cu *);
2180
2181 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2182
2183 static struct type *get_die_type_at_offset (sect_offset,
2184 struct dwarf2_per_cu_data *);
2185
2186 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
2187
2188 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2189 enum language pretend_language);
2190
2191 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
2192
2193 /* Class, the destructor of which frees all allocated queue entries. This
2194 will only have work to do if an error was thrown while processing the
2195 dwarf. If no error was thrown then the queue entries should have all
2196 been processed, and freed, as we went along. */
2197
2198 class dwarf2_queue_guard
2199 {
2200 public:
2201 dwarf2_queue_guard () = default;
2202
2203 /* Free any entries remaining on the queue. There should only be
2204 entries left if we hit an error while processing the dwarf. */
2205 ~dwarf2_queue_guard ()
2206 {
2207 struct dwarf2_queue_item *item, *last;
2208
2209 item = dwarf2_queue;
2210 while (item)
2211 {
2212 /* Anything still marked queued is likely to be in an
2213 inconsistent state, so discard it. */
2214 if (item->per_cu->queued)
2215 {
2216 if (item->per_cu->cu != NULL)
2217 free_one_cached_comp_unit (item->per_cu);
2218 item->per_cu->queued = 0;
2219 }
2220
2221 last = item;
2222 item = item->next;
2223 xfree (last);
2224 }
2225
2226 dwarf2_queue = dwarf2_queue_tail = NULL;
2227 }
2228 };
2229
2230 /* The return type of find_file_and_directory. Note, the enclosed
2231 string pointers are only valid while this object is valid. */
2232
2233 struct file_and_directory
2234 {
2235 /* The filename. This is never NULL. */
2236 const char *name;
2237
2238 /* The compilation directory. NULL if not known. If we needed to
2239 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2240 points directly to the DW_AT_comp_dir string attribute owned by
2241 the obstack that owns the DIE. */
2242 const char *comp_dir;
2243
2244 /* If we needed to build a new string for comp_dir, this is what
2245 owns the storage. */
2246 std::string comp_dir_storage;
2247 };
2248
2249 static file_and_directory find_file_and_directory (struct die_info *die,
2250 struct dwarf2_cu *cu);
2251
2252 static char *file_full_name (int file, struct line_header *lh,
2253 const char *comp_dir);
2254
2255 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2256 enum class rcuh_kind { COMPILE, TYPE };
2257
2258 static const gdb_byte *read_and_check_comp_unit_head
2259 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2260 struct comp_unit_head *header,
2261 struct dwarf2_section_info *section,
2262 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2263 rcuh_kind section_kind);
2264
2265 static void init_cutu_and_read_dies
2266 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2267 int use_existing_cu, int keep,
2268 die_reader_func_ftype *die_reader_func, void *data);
2269
2270 static void init_cutu_and_read_dies_simple
2271 (struct dwarf2_per_cu_data *this_cu,
2272 die_reader_func_ftype *die_reader_func, void *data);
2273
2274 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2275
2276 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2277
2278 static struct dwo_unit *lookup_dwo_unit_in_dwp
2279 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2280 struct dwp_file *dwp_file, const char *comp_dir,
2281 ULONGEST signature, int is_debug_types);
2282
2283 static struct dwp_file *get_dwp_file
2284 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2285
2286 static struct dwo_unit *lookup_dwo_comp_unit
2287 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2288
2289 static struct dwo_unit *lookup_dwo_type_unit
2290 (struct signatured_type *, const char *, const char *);
2291
2292 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2293
2294 static void free_dwo_file_cleanup (void *);
2295
2296 struct free_dwo_file_cleanup_data
2297 {
2298 struct dwo_file *dwo_file;
2299 struct dwarf2_per_objfile *dwarf2_per_objfile;
2300 };
2301
2302 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2303
2304 static void check_producer (struct dwarf2_cu *cu);
2305
2306 static void free_line_header_voidp (void *arg);
2307 \f
2308 /* Various complaints about symbol reading that don't abort the process. */
2309
2310 static void
2311 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2312 {
2313 complaint (&symfile_complaints,
2314 _("statement list doesn't fit in .debug_line section"));
2315 }
2316
2317 static void
2318 dwarf2_debug_line_missing_file_complaint (void)
2319 {
2320 complaint (&symfile_complaints,
2321 _(".debug_line section has line data without a file"));
2322 }
2323
2324 static void
2325 dwarf2_debug_line_missing_end_sequence_complaint (void)
2326 {
2327 complaint (&symfile_complaints,
2328 _(".debug_line section has line "
2329 "program sequence without an end"));
2330 }
2331
2332 static void
2333 dwarf2_complex_location_expr_complaint (void)
2334 {
2335 complaint (&symfile_complaints, _("location expression too complex"));
2336 }
2337
2338 static void
2339 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2340 int arg3)
2341 {
2342 complaint (&symfile_complaints,
2343 _("const value length mismatch for '%s', got %d, expected %d"),
2344 arg1, arg2, arg3);
2345 }
2346
2347 static void
2348 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2349 {
2350 complaint (&symfile_complaints,
2351 _("debug info runs off end of %s section"
2352 " [in module %s]"),
2353 get_section_name (section),
2354 get_section_file_name (section));
2355 }
2356
2357 static void
2358 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2359 {
2360 complaint (&symfile_complaints,
2361 _("macro debug info contains a "
2362 "malformed macro definition:\n`%s'"),
2363 arg1);
2364 }
2365
2366 static void
2367 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2368 {
2369 complaint (&symfile_complaints,
2370 _("invalid attribute class or form for '%s' in '%s'"),
2371 arg1, arg2);
2372 }
2373
2374 /* Hash function for line_header_hash. */
2375
2376 static hashval_t
2377 line_header_hash (const struct line_header *ofs)
2378 {
2379 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2380 }
2381
2382 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2383
2384 static hashval_t
2385 line_header_hash_voidp (const void *item)
2386 {
2387 const struct line_header *ofs = (const struct line_header *) item;
2388
2389 return line_header_hash (ofs);
2390 }
2391
2392 /* Equality function for line_header_hash. */
2393
2394 static int
2395 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2396 {
2397 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2398 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2399
2400 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2401 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2402 }
2403
2404 \f
2405
2406 /* Read the given attribute value as an address, taking the attribute's
2407 form into account. */
2408
2409 static CORE_ADDR
2410 attr_value_as_address (struct attribute *attr)
2411 {
2412 CORE_ADDR addr;
2413
2414 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2415 {
2416 /* Aside from a few clearly defined exceptions, attributes that
2417 contain an address must always be in DW_FORM_addr form.
2418 Unfortunately, some compilers happen to be violating this
2419 requirement by encoding addresses using other forms, such
2420 as DW_FORM_data4 for example. For those broken compilers,
2421 we try to do our best, without any guarantee of success,
2422 to interpret the address correctly. It would also be nice
2423 to generate a complaint, but that would require us to maintain
2424 a list of legitimate cases where a non-address form is allowed,
2425 as well as update callers to pass in at least the CU's DWARF
2426 version. This is more overhead than what we're willing to
2427 expand for a pretty rare case. */
2428 addr = DW_UNSND (attr);
2429 }
2430 else
2431 addr = DW_ADDR (attr);
2432
2433 return addr;
2434 }
2435
2436 /* The suffix for an index file. */
2437 #define INDEX4_SUFFIX ".gdb-index"
2438 #define INDEX5_SUFFIX ".debug_names"
2439 #define DEBUG_STR_SUFFIX ".debug_str"
2440
2441 /* See declaration. */
2442
2443 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2444 const dwarf2_debug_sections *names)
2445 : objfile (objfile_)
2446 {
2447 if (names == NULL)
2448 names = &dwarf2_elf_names;
2449
2450 bfd *obfd = objfile->obfd;
2451
2452 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2453 locate_sections (obfd, sec, *names);
2454 }
2455
2456 static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2457
2458 dwarf2_per_objfile::~dwarf2_per_objfile ()
2459 {
2460 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2461 free_cached_comp_units ();
2462
2463 if (quick_file_names_table)
2464 htab_delete (quick_file_names_table);
2465
2466 if (line_header_hash)
2467 htab_delete (line_header_hash);
2468
2469 for (int ix = 0; ix < n_comp_units; ++ix)
2470 VEC_free (dwarf2_per_cu_ptr, all_comp_units[ix]->imported_symtabs);
2471
2472 for (int ix = 0; ix < n_type_units; ++ix)
2473 VEC_free (dwarf2_per_cu_ptr,
2474 all_type_units[ix]->per_cu.imported_symtabs);
2475 xfree (all_type_units);
2476
2477 VEC_free (dwarf2_section_info_def, types);
2478
2479 if (dwo_files != NULL)
2480 free_dwo_files (dwo_files, objfile);
2481 if (dwp_file != NULL)
2482 gdb_bfd_unref (dwp_file->dbfd);
2483
2484 if (dwz_file != NULL && dwz_file->dwz_bfd)
2485 gdb_bfd_unref (dwz_file->dwz_bfd);
2486
2487 if (index_table != NULL)
2488 index_table->~mapped_index ();
2489
2490 /* Everything else should be on the objfile obstack. */
2491 }
2492
2493 /* See declaration. */
2494
2495 void
2496 dwarf2_per_objfile::free_cached_comp_units ()
2497 {
2498 dwarf2_per_cu_data *per_cu = read_in_chain;
2499 dwarf2_per_cu_data **last_chain = &read_in_chain;
2500 while (per_cu != NULL)
2501 {
2502 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2503
2504 delete per_cu->cu;
2505 *last_chain = next_cu;
2506 per_cu = next_cu;
2507 }
2508 }
2509
2510 /* Try to locate the sections we need for DWARF 2 debugging
2511 information and return true if we have enough to do something.
2512 NAMES points to the dwarf2 section names, or is NULL if the standard
2513 ELF names are used. */
2514
2515 int
2516 dwarf2_has_info (struct objfile *objfile,
2517 const struct dwarf2_debug_sections *names)
2518 {
2519 if (objfile->flags & OBJF_READNEVER)
2520 return 0;
2521
2522 struct dwarf2_per_objfile *dwarf2_per_objfile
2523 = get_dwarf2_per_objfile (objfile);
2524
2525 if (dwarf2_per_objfile == NULL)
2526 {
2527 /* Initialize per-objfile state. */
2528 dwarf2_per_objfile
2529 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2530 names);
2531 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
2532 }
2533 return (!dwarf2_per_objfile->info.is_virtual
2534 && dwarf2_per_objfile->info.s.section != NULL
2535 && !dwarf2_per_objfile->abbrev.is_virtual
2536 && dwarf2_per_objfile->abbrev.s.section != NULL);
2537 }
2538
2539 /* Return the containing section of virtual section SECTION. */
2540
2541 static struct dwarf2_section_info *
2542 get_containing_section (const struct dwarf2_section_info *section)
2543 {
2544 gdb_assert (section->is_virtual);
2545 return section->s.containing_section;
2546 }
2547
2548 /* Return the bfd owner of SECTION. */
2549
2550 static struct bfd *
2551 get_section_bfd_owner (const struct dwarf2_section_info *section)
2552 {
2553 if (section->is_virtual)
2554 {
2555 section = get_containing_section (section);
2556 gdb_assert (!section->is_virtual);
2557 }
2558 return section->s.section->owner;
2559 }
2560
2561 /* Return the bfd section of SECTION.
2562 Returns NULL if the section is not present. */
2563
2564 static asection *
2565 get_section_bfd_section (const struct dwarf2_section_info *section)
2566 {
2567 if (section->is_virtual)
2568 {
2569 section = get_containing_section (section);
2570 gdb_assert (!section->is_virtual);
2571 }
2572 return section->s.section;
2573 }
2574
2575 /* Return the name of SECTION. */
2576
2577 static const char *
2578 get_section_name (const struct dwarf2_section_info *section)
2579 {
2580 asection *sectp = get_section_bfd_section (section);
2581
2582 gdb_assert (sectp != NULL);
2583 return bfd_section_name (get_section_bfd_owner (section), sectp);
2584 }
2585
2586 /* Return the name of the file SECTION is in. */
2587
2588 static const char *
2589 get_section_file_name (const struct dwarf2_section_info *section)
2590 {
2591 bfd *abfd = get_section_bfd_owner (section);
2592
2593 return bfd_get_filename (abfd);
2594 }
2595
2596 /* Return the id of SECTION.
2597 Returns 0 if SECTION doesn't exist. */
2598
2599 static int
2600 get_section_id (const struct dwarf2_section_info *section)
2601 {
2602 asection *sectp = get_section_bfd_section (section);
2603
2604 if (sectp == NULL)
2605 return 0;
2606 return sectp->id;
2607 }
2608
2609 /* Return the flags of SECTION.
2610 SECTION (or containing section if this is a virtual section) must exist. */
2611
2612 static int
2613 get_section_flags (const struct dwarf2_section_info *section)
2614 {
2615 asection *sectp = get_section_bfd_section (section);
2616
2617 gdb_assert (sectp != NULL);
2618 return bfd_get_section_flags (sectp->owner, sectp);
2619 }
2620
2621 /* When loading sections, we look either for uncompressed section or for
2622 compressed section names. */
2623
2624 static int
2625 section_is_p (const char *section_name,
2626 const struct dwarf2_section_names *names)
2627 {
2628 if (names->normal != NULL
2629 && strcmp (section_name, names->normal) == 0)
2630 return 1;
2631 if (names->compressed != NULL
2632 && strcmp (section_name, names->compressed) == 0)
2633 return 1;
2634 return 0;
2635 }
2636
2637 /* See declaration. */
2638
2639 void
2640 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2641 const dwarf2_debug_sections &names)
2642 {
2643 flagword aflag = bfd_get_section_flags (abfd, sectp);
2644
2645 if ((aflag & SEC_HAS_CONTENTS) == 0)
2646 {
2647 }
2648 else if (section_is_p (sectp->name, &names.info))
2649 {
2650 this->info.s.section = sectp;
2651 this->info.size = bfd_get_section_size (sectp);
2652 }
2653 else if (section_is_p (sectp->name, &names.abbrev))
2654 {
2655 this->abbrev.s.section = sectp;
2656 this->abbrev.size = bfd_get_section_size (sectp);
2657 }
2658 else if (section_is_p (sectp->name, &names.line))
2659 {
2660 this->line.s.section = sectp;
2661 this->line.size = bfd_get_section_size (sectp);
2662 }
2663 else if (section_is_p (sectp->name, &names.loc))
2664 {
2665 this->loc.s.section = sectp;
2666 this->loc.size = bfd_get_section_size (sectp);
2667 }
2668 else if (section_is_p (sectp->name, &names.loclists))
2669 {
2670 this->loclists.s.section = sectp;
2671 this->loclists.size = bfd_get_section_size (sectp);
2672 }
2673 else if (section_is_p (sectp->name, &names.macinfo))
2674 {
2675 this->macinfo.s.section = sectp;
2676 this->macinfo.size = bfd_get_section_size (sectp);
2677 }
2678 else if (section_is_p (sectp->name, &names.macro))
2679 {
2680 this->macro.s.section = sectp;
2681 this->macro.size = bfd_get_section_size (sectp);
2682 }
2683 else if (section_is_p (sectp->name, &names.str))
2684 {
2685 this->str.s.section = sectp;
2686 this->str.size = bfd_get_section_size (sectp);
2687 }
2688 else if (section_is_p (sectp->name, &names.line_str))
2689 {
2690 this->line_str.s.section = sectp;
2691 this->line_str.size = bfd_get_section_size (sectp);
2692 }
2693 else if (section_is_p (sectp->name, &names.addr))
2694 {
2695 this->addr.s.section = sectp;
2696 this->addr.size = bfd_get_section_size (sectp);
2697 }
2698 else if (section_is_p (sectp->name, &names.frame))
2699 {
2700 this->frame.s.section = sectp;
2701 this->frame.size = bfd_get_section_size (sectp);
2702 }
2703 else if (section_is_p (sectp->name, &names.eh_frame))
2704 {
2705 this->eh_frame.s.section = sectp;
2706 this->eh_frame.size = bfd_get_section_size (sectp);
2707 }
2708 else if (section_is_p (sectp->name, &names.ranges))
2709 {
2710 this->ranges.s.section = sectp;
2711 this->ranges.size = bfd_get_section_size (sectp);
2712 }
2713 else if (section_is_p (sectp->name, &names.rnglists))
2714 {
2715 this->rnglists.s.section = sectp;
2716 this->rnglists.size = bfd_get_section_size (sectp);
2717 }
2718 else if (section_is_p (sectp->name, &names.types))
2719 {
2720 struct dwarf2_section_info type_section;
2721
2722 memset (&type_section, 0, sizeof (type_section));
2723 type_section.s.section = sectp;
2724 type_section.size = bfd_get_section_size (sectp);
2725
2726 VEC_safe_push (dwarf2_section_info_def, this->types,
2727 &type_section);
2728 }
2729 else if (section_is_p (sectp->name, &names.gdb_index))
2730 {
2731 this->gdb_index.s.section = sectp;
2732 this->gdb_index.size = bfd_get_section_size (sectp);
2733 }
2734 else if (section_is_p (sectp->name, &names.debug_names))
2735 {
2736 this->debug_names.s.section = sectp;
2737 this->debug_names.size = bfd_get_section_size (sectp);
2738 }
2739 else if (section_is_p (sectp->name, &names.debug_aranges))
2740 {
2741 this->debug_aranges.s.section = sectp;
2742 this->debug_aranges.size = bfd_get_section_size (sectp);
2743 }
2744
2745 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2746 && bfd_section_vma (abfd, sectp) == 0)
2747 this->has_section_at_zero = true;
2748 }
2749
2750 /* A helper function that decides whether a section is empty,
2751 or not present. */
2752
2753 static int
2754 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2755 {
2756 if (section->is_virtual)
2757 return section->size == 0;
2758 return section->s.section == NULL || section->size == 0;
2759 }
2760
2761 /* Read the contents of the section INFO.
2762 OBJFILE is the main object file, but not necessarily the file where
2763 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2764 of the DWO file.
2765 If the section is compressed, uncompress it before returning. */
2766
2767 static void
2768 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2769 {
2770 asection *sectp;
2771 bfd *abfd;
2772 gdb_byte *buf, *retbuf;
2773
2774 if (info->readin)
2775 return;
2776 info->buffer = NULL;
2777 info->readin = 1;
2778
2779 if (dwarf2_section_empty_p (info))
2780 return;
2781
2782 sectp = get_section_bfd_section (info);
2783
2784 /* If this is a virtual section we need to read in the real one first. */
2785 if (info->is_virtual)
2786 {
2787 struct dwarf2_section_info *containing_section =
2788 get_containing_section (info);
2789
2790 gdb_assert (sectp != NULL);
2791 if ((sectp->flags & SEC_RELOC) != 0)
2792 {
2793 error (_("Dwarf Error: DWP format V2 with relocations is not"
2794 " supported in section %s [in module %s]"),
2795 get_section_name (info), get_section_file_name (info));
2796 }
2797 dwarf2_read_section (objfile, containing_section);
2798 /* Other code should have already caught virtual sections that don't
2799 fit. */
2800 gdb_assert (info->virtual_offset + info->size
2801 <= containing_section->size);
2802 /* If the real section is empty or there was a problem reading the
2803 section we shouldn't get here. */
2804 gdb_assert (containing_section->buffer != NULL);
2805 info->buffer = containing_section->buffer + info->virtual_offset;
2806 return;
2807 }
2808
2809 /* If the section has relocations, we must read it ourselves.
2810 Otherwise we attach it to the BFD. */
2811 if ((sectp->flags & SEC_RELOC) == 0)
2812 {
2813 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2814 return;
2815 }
2816
2817 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2818 info->buffer = buf;
2819
2820 /* When debugging .o files, we may need to apply relocations; see
2821 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2822 We never compress sections in .o files, so we only need to
2823 try this when the section is not compressed. */
2824 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2825 if (retbuf != NULL)
2826 {
2827 info->buffer = retbuf;
2828 return;
2829 }
2830
2831 abfd = get_section_bfd_owner (info);
2832 gdb_assert (abfd != NULL);
2833
2834 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2835 || bfd_bread (buf, info->size, abfd) != info->size)
2836 {
2837 error (_("Dwarf Error: Can't read DWARF data"
2838 " in section %s [in module %s]"),
2839 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2840 }
2841 }
2842
2843 /* A helper function that returns the size of a section in a safe way.
2844 If you are positive that the section has been read before using the
2845 size, then it is safe to refer to the dwarf2_section_info object's
2846 "size" field directly. In other cases, you must call this
2847 function, because for compressed sections the size field is not set
2848 correctly until the section has been read. */
2849
2850 static bfd_size_type
2851 dwarf2_section_size (struct objfile *objfile,
2852 struct dwarf2_section_info *info)
2853 {
2854 if (!info->readin)
2855 dwarf2_read_section (objfile, info);
2856 return info->size;
2857 }
2858
2859 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2860 SECTION_NAME. */
2861
2862 void
2863 dwarf2_get_section_info (struct objfile *objfile,
2864 enum dwarf2_section_enum sect,
2865 asection **sectp, const gdb_byte **bufp,
2866 bfd_size_type *sizep)
2867 {
2868 struct dwarf2_per_objfile *data
2869 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2870 dwarf2_objfile_data_key);
2871 struct dwarf2_section_info *info;
2872
2873 /* We may see an objfile without any DWARF, in which case we just
2874 return nothing. */
2875 if (data == NULL)
2876 {
2877 *sectp = NULL;
2878 *bufp = NULL;
2879 *sizep = 0;
2880 return;
2881 }
2882 switch (sect)
2883 {
2884 case DWARF2_DEBUG_FRAME:
2885 info = &data->frame;
2886 break;
2887 case DWARF2_EH_FRAME:
2888 info = &data->eh_frame;
2889 break;
2890 default:
2891 gdb_assert_not_reached ("unexpected section");
2892 }
2893
2894 dwarf2_read_section (objfile, info);
2895
2896 *sectp = get_section_bfd_section (info);
2897 *bufp = info->buffer;
2898 *sizep = info->size;
2899 }
2900
2901 /* A helper function to find the sections for a .dwz file. */
2902
2903 static void
2904 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2905 {
2906 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2907
2908 /* Note that we only support the standard ELF names, because .dwz
2909 is ELF-only (at the time of writing). */
2910 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2911 {
2912 dwz_file->abbrev.s.section = sectp;
2913 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2914 }
2915 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2916 {
2917 dwz_file->info.s.section = sectp;
2918 dwz_file->info.size = bfd_get_section_size (sectp);
2919 }
2920 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2921 {
2922 dwz_file->str.s.section = sectp;
2923 dwz_file->str.size = bfd_get_section_size (sectp);
2924 }
2925 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2926 {
2927 dwz_file->line.s.section = sectp;
2928 dwz_file->line.size = bfd_get_section_size (sectp);
2929 }
2930 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2931 {
2932 dwz_file->macro.s.section = sectp;
2933 dwz_file->macro.size = bfd_get_section_size (sectp);
2934 }
2935 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2936 {
2937 dwz_file->gdb_index.s.section = sectp;
2938 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2939 }
2940 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2941 {
2942 dwz_file->debug_names.s.section = sectp;
2943 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2944 }
2945 }
2946
2947 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2948 there is no .gnu_debugaltlink section in the file. Error if there
2949 is such a section but the file cannot be found. */
2950
2951 static struct dwz_file *
2952 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2953 {
2954 const char *filename;
2955 struct dwz_file *result;
2956 bfd_size_type buildid_len_arg;
2957 size_t buildid_len;
2958 bfd_byte *buildid;
2959
2960 if (dwarf2_per_objfile->dwz_file != NULL)
2961 return dwarf2_per_objfile->dwz_file;
2962
2963 bfd_set_error (bfd_error_no_error);
2964 gdb::unique_xmalloc_ptr<char> data
2965 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2966 &buildid_len_arg, &buildid));
2967 if (data == NULL)
2968 {
2969 if (bfd_get_error () == bfd_error_no_error)
2970 return NULL;
2971 error (_("could not read '.gnu_debugaltlink' section: %s"),
2972 bfd_errmsg (bfd_get_error ()));
2973 }
2974
2975 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2976
2977 buildid_len = (size_t) buildid_len_arg;
2978
2979 filename = data.get ();
2980
2981 std::string abs_storage;
2982 if (!IS_ABSOLUTE_PATH (filename))
2983 {
2984 gdb::unique_xmalloc_ptr<char> abs
2985 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2986
2987 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2988 filename = abs_storage.c_str ();
2989 }
2990
2991 /* First try the file name given in the section. If that doesn't
2992 work, try to use the build-id instead. */
2993 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2994 if (dwz_bfd != NULL)
2995 {
2996 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2997 dwz_bfd.release ();
2998 }
2999
3000 if (dwz_bfd == NULL)
3001 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
3002
3003 if (dwz_bfd == NULL)
3004 error (_("could not find '.gnu_debugaltlink' file for %s"),
3005 objfile_name (dwarf2_per_objfile->objfile));
3006
3007 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
3008 struct dwz_file);
3009 result->dwz_bfd = dwz_bfd.release ();
3010
3011 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
3012
3013 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
3014 dwarf2_per_objfile->dwz_file = result;
3015 return result;
3016 }
3017 \f
3018 /* DWARF quick_symbols_functions support. */
3019
3020 /* TUs can share .debug_line entries, and there can be a lot more TUs than
3021 unique line tables, so we maintain a separate table of all .debug_line
3022 derived entries to support the sharing.
3023 All the quick functions need is the list of file names. We discard the
3024 line_header when we're done and don't need to record it here. */
3025 struct quick_file_names
3026 {
3027 /* The data used to construct the hash key. */
3028 struct stmt_list_hash hash;
3029
3030 /* The number of entries in file_names, real_names. */
3031 unsigned int num_file_names;
3032
3033 /* The file names from the line table, after being run through
3034 file_full_name. */
3035 const char **file_names;
3036
3037 /* The file names from the line table after being run through
3038 gdb_realpath. These are computed lazily. */
3039 const char **real_names;
3040 };
3041
3042 /* When using the index (and thus not using psymtabs), each CU has an
3043 object of this type. This is used to hold information needed by
3044 the various "quick" methods. */
3045 struct dwarf2_per_cu_quick_data
3046 {
3047 /* The file table. This can be NULL if there was no file table
3048 or it's currently not read in.
3049 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
3050 struct quick_file_names *file_names;
3051
3052 /* The corresponding symbol table. This is NULL if symbols for this
3053 CU have not yet been read. */
3054 struct compunit_symtab *compunit_symtab;
3055
3056 /* A temporary mark bit used when iterating over all CUs in
3057 expand_symtabs_matching. */
3058 unsigned int mark : 1;
3059
3060 /* True if we've tried to read the file table and found there isn't one.
3061 There will be no point in trying to read it again next time. */
3062 unsigned int no_file_data : 1;
3063 };
3064
3065 /* Utility hash function for a stmt_list_hash. */
3066
3067 static hashval_t
3068 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
3069 {
3070 hashval_t v = 0;
3071
3072 if (stmt_list_hash->dwo_unit != NULL)
3073 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
3074 v += to_underlying (stmt_list_hash->line_sect_off);
3075 return v;
3076 }
3077
3078 /* Utility equality function for a stmt_list_hash. */
3079
3080 static int
3081 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
3082 const struct stmt_list_hash *rhs)
3083 {
3084 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
3085 return 0;
3086 if (lhs->dwo_unit != NULL
3087 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
3088 return 0;
3089
3090 return lhs->line_sect_off == rhs->line_sect_off;
3091 }
3092
3093 /* Hash function for a quick_file_names. */
3094
3095 static hashval_t
3096 hash_file_name_entry (const void *e)
3097 {
3098 const struct quick_file_names *file_data
3099 = (const struct quick_file_names *) e;
3100
3101 return hash_stmt_list_entry (&file_data->hash);
3102 }
3103
3104 /* Equality function for a quick_file_names. */
3105
3106 static int
3107 eq_file_name_entry (const void *a, const void *b)
3108 {
3109 const struct quick_file_names *ea = (const struct quick_file_names *) a;
3110 const struct quick_file_names *eb = (const struct quick_file_names *) b;
3111
3112 return eq_stmt_list_entry (&ea->hash, &eb->hash);
3113 }
3114
3115 /* Delete function for a quick_file_names. */
3116
3117 static void
3118 delete_file_name_entry (void *e)
3119 {
3120 struct quick_file_names *file_data = (struct quick_file_names *) e;
3121 int i;
3122
3123 for (i = 0; i < file_data->num_file_names; ++i)
3124 {
3125 xfree ((void*) file_data->file_names[i]);
3126 if (file_data->real_names)
3127 xfree ((void*) file_data->real_names[i]);
3128 }
3129
3130 /* The space for the struct itself lives on objfile_obstack,
3131 so we don't free it here. */
3132 }
3133
3134 /* Create a quick_file_names hash table. */
3135
3136 static htab_t
3137 create_quick_file_names_table (unsigned int nr_initial_entries)
3138 {
3139 return htab_create_alloc (nr_initial_entries,
3140 hash_file_name_entry, eq_file_name_entry,
3141 delete_file_name_entry, xcalloc, xfree);
3142 }
3143
3144 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3145 have to be created afterwards. You should call age_cached_comp_units after
3146 processing PER_CU->CU. dw2_setup must have been already called. */
3147
3148 static void
3149 load_cu (struct dwarf2_per_cu_data *per_cu)
3150 {
3151 if (per_cu->is_debug_types)
3152 load_full_type_unit (per_cu);
3153 else
3154 load_full_comp_unit (per_cu, language_minimal);
3155
3156 if (per_cu->cu == NULL)
3157 return; /* Dummy CU. */
3158
3159 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
3160 }
3161
3162 /* Read in the symbols for PER_CU. */
3163
3164 static void
3165 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3166 {
3167 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3168
3169 /* Skip type_unit_groups, reading the type units they contain
3170 is handled elsewhere. */
3171 if (IS_TYPE_UNIT_GROUP (per_cu))
3172 return;
3173
3174 /* The destructor of dwarf2_queue_guard frees any entries left on
3175 the queue. After this point we're guaranteed to leave this function
3176 with the dwarf queue empty. */
3177 dwarf2_queue_guard q_guard;
3178
3179 if (dwarf2_per_objfile->using_index
3180 ? per_cu->v.quick->compunit_symtab == NULL
3181 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3182 {
3183 queue_comp_unit (per_cu, language_minimal);
3184 load_cu (per_cu);
3185
3186 /* If we just loaded a CU from a DWO, and we're working with an index
3187 that may badly handle TUs, load all the TUs in that DWO as well.
3188 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3189 if (!per_cu->is_debug_types
3190 && per_cu->cu != NULL
3191 && per_cu->cu->dwo_unit != NULL
3192 && dwarf2_per_objfile->index_table != NULL
3193 && dwarf2_per_objfile->index_table->version <= 7
3194 /* DWP files aren't supported yet. */
3195 && get_dwp_file (dwarf2_per_objfile) == NULL)
3196 queue_and_load_all_dwo_tus (per_cu);
3197 }
3198
3199 process_queue (dwarf2_per_objfile);
3200
3201 /* Age the cache, releasing compilation units that have not
3202 been used recently. */
3203 age_cached_comp_units (dwarf2_per_objfile);
3204 }
3205
3206 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3207 the objfile from which this CU came. Returns the resulting symbol
3208 table. */
3209
3210 static struct compunit_symtab *
3211 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3212 {
3213 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3214
3215 gdb_assert (dwarf2_per_objfile->using_index);
3216 if (!per_cu->v.quick->compunit_symtab)
3217 {
3218 struct cleanup *back_to = make_cleanup (free_cached_comp_units,
3219 dwarf2_per_objfile);
3220 scoped_restore decrementer = increment_reading_symtab ();
3221 dw2_do_instantiate_symtab (per_cu);
3222 process_cu_includes (dwarf2_per_objfile);
3223 do_cleanups (back_to);
3224 }
3225
3226 return per_cu->v.quick->compunit_symtab;
3227 }
3228
3229 /* Return the CU/TU given its index.
3230
3231 This is intended for loops like:
3232
3233 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3234 + dwarf2_per_objfile->n_type_units); ++i)
3235 {
3236 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3237
3238 ...;
3239 }
3240 */
3241
3242 static struct dwarf2_per_cu_data *
3243 dw2_get_cutu (struct dwarf2_per_objfile *dwarf2_per_objfile,
3244 int index)
3245 {
3246 if (index >= dwarf2_per_objfile->n_comp_units)
3247 {
3248 index -= dwarf2_per_objfile->n_comp_units;
3249 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3250 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
3251 }
3252
3253 return dwarf2_per_objfile->all_comp_units[index];
3254 }
3255
3256 /* Return the CU given its index.
3257 This differs from dw2_get_cutu in that it's for when you know INDEX
3258 refers to a CU. */
3259
3260 static struct dwarf2_per_cu_data *
3261 dw2_get_cu (struct dwarf2_per_objfile *dwarf2_per_objfile, int index)
3262 {
3263 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
3264
3265 return dwarf2_per_objfile->all_comp_units[index];
3266 }
3267
3268 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3269 objfile_obstack, and constructed with the specified field
3270 values. */
3271
3272 static dwarf2_per_cu_data *
3273 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3274 struct dwarf2_section_info *section,
3275 int is_dwz,
3276 sect_offset sect_off, ULONGEST length)
3277 {
3278 struct objfile *objfile = dwarf2_per_objfile->objfile;
3279 dwarf2_per_cu_data *the_cu
3280 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3281 struct dwarf2_per_cu_data);
3282 the_cu->sect_off = sect_off;
3283 the_cu->length = length;
3284 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
3285 the_cu->section = section;
3286 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3287 struct dwarf2_per_cu_quick_data);
3288 the_cu->is_dwz = is_dwz;
3289 return the_cu;
3290 }
3291
3292 /* A helper for create_cus_from_index that handles a given list of
3293 CUs. */
3294
3295 static void
3296 create_cus_from_index_list (struct objfile *objfile,
3297 const gdb_byte *cu_list, offset_type n_elements,
3298 struct dwarf2_section_info *section,
3299 int is_dwz,
3300 int base_offset)
3301 {
3302 offset_type i;
3303 struct dwarf2_per_objfile *dwarf2_per_objfile
3304 = get_dwarf2_per_objfile (objfile);
3305
3306 for (i = 0; i < n_elements; i += 2)
3307 {
3308 gdb_static_assert (sizeof (ULONGEST) >= 8);
3309
3310 sect_offset sect_off
3311 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3312 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3313 cu_list += 2 * 8;
3314
3315 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
3316 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3317 sect_off, length);
3318 }
3319 }
3320
3321 /* Read the CU list from the mapped index, and use it to create all
3322 the CU objects for this objfile. */
3323
3324 static void
3325 create_cus_from_index (struct objfile *objfile,
3326 const gdb_byte *cu_list, offset_type cu_list_elements,
3327 const gdb_byte *dwz_list, offset_type dwz_elements)
3328 {
3329 struct dwz_file *dwz;
3330 struct dwarf2_per_objfile *dwarf2_per_objfile
3331 = get_dwarf2_per_objfile (objfile);
3332
3333 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3334 dwarf2_per_objfile->all_comp_units =
3335 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3336 dwarf2_per_objfile->n_comp_units);
3337
3338 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3339 &dwarf2_per_objfile->info, 0, 0);
3340
3341 if (dwz_elements == 0)
3342 return;
3343
3344 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3345 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3346 cu_list_elements / 2);
3347 }
3348
3349 /* Create the signatured type hash table from the index. */
3350
3351 static void
3352 create_signatured_type_table_from_index (struct objfile *objfile,
3353 struct dwarf2_section_info *section,
3354 const gdb_byte *bytes,
3355 offset_type elements)
3356 {
3357 offset_type i;
3358 htab_t sig_types_hash;
3359 struct dwarf2_per_objfile *dwarf2_per_objfile
3360 = get_dwarf2_per_objfile (objfile);
3361
3362 dwarf2_per_objfile->n_type_units
3363 = dwarf2_per_objfile->n_allocated_type_units
3364 = elements / 3;
3365 dwarf2_per_objfile->all_type_units =
3366 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3367
3368 sig_types_hash = allocate_signatured_type_table (objfile);
3369
3370 for (i = 0; i < elements; i += 3)
3371 {
3372 struct signatured_type *sig_type;
3373 ULONGEST signature;
3374 void **slot;
3375 cu_offset type_offset_in_tu;
3376
3377 gdb_static_assert (sizeof (ULONGEST) >= 8);
3378 sect_offset sect_off
3379 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3380 type_offset_in_tu
3381 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3382 BFD_ENDIAN_LITTLE);
3383 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3384 bytes += 3 * 8;
3385
3386 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3387 struct signatured_type);
3388 sig_type->signature = signature;
3389 sig_type->type_offset_in_tu = type_offset_in_tu;
3390 sig_type->per_cu.is_debug_types = 1;
3391 sig_type->per_cu.section = section;
3392 sig_type->per_cu.sect_off = sect_off;
3393 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3394 sig_type->per_cu.v.quick
3395 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3396 struct dwarf2_per_cu_quick_data);
3397
3398 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3399 *slot = sig_type;
3400
3401 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3402 }
3403
3404 dwarf2_per_objfile->signatured_types = sig_types_hash;
3405 }
3406
3407 /* Create the signatured type hash table from .debug_names. */
3408
3409 static void
3410 create_signatured_type_table_from_debug_names
3411 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3412 const mapped_debug_names &map,
3413 struct dwarf2_section_info *section,
3414 struct dwarf2_section_info *abbrev_section)
3415 {
3416 struct objfile *objfile = dwarf2_per_objfile->objfile;
3417
3418 dwarf2_read_section (objfile, section);
3419 dwarf2_read_section (objfile, abbrev_section);
3420
3421 dwarf2_per_objfile->n_type_units
3422 = dwarf2_per_objfile->n_allocated_type_units
3423 = map.tu_count;
3424 dwarf2_per_objfile->all_type_units
3425 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3426
3427 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3428
3429 for (uint32_t i = 0; i < map.tu_count; ++i)
3430 {
3431 struct signatured_type *sig_type;
3432 ULONGEST signature;
3433 void **slot;
3434 cu_offset type_offset_in_tu;
3435
3436 sect_offset sect_off
3437 = (sect_offset) (extract_unsigned_integer
3438 (map.tu_table_reordered + i * map.offset_size,
3439 map.offset_size,
3440 map.dwarf5_byte_order));
3441
3442 comp_unit_head cu_header;
3443 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3444 abbrev_section,
3445 section->buffer + to_underlying (sect_off),
3446 rcuh_kind::TYPE);
3447
3448 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3449 struct signatured_type);
3450 sig_type->signature = cu_header.signature;
3451 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3452 sig_type->per_cu.is_debug_types = 1;
3453 sig_type->per_cu.section = section;
3454 sig_type->per_cu.sect_off = sect_off;
3455 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3456 sig_type->per_cu.v.quick
3457 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3458 struct dwarf2_per_cu_quick_data);
3459
3460 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3461 *slot = sig_type;
3462
3463 dwarf2_per_objfile->all_type_units[i] = sig_type;
3464 }
3465
3466 dwarf2_per_objfile->signatured_types = sig_types_hash;
3467 }
3468
3469 /* Read the address map data from the mapped index, and use it to
3470 populate the objfile's psymtabs_addrmap. */
3471
3472 static void
3473 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3474 struct mapped_index *index)
3475 {
3476 struct objfile *objfile = dwarf2_per_objfile->objfile;
3477 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3478 const gdb_byte *iter, *end;
3479 struct addrmap *mutable_map;
3480 CORE_ADDR baseaddr;
3481
3482 auto_obstack temp_obstack;
3483
3484 mutable_map = addrmap_create_mutable (&temp_obstack);
3485
3486 iter = index->address_table.data ();
3487 end = iter + index->address_table.size ();
3488
3489 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3490
3491 while (iter < end)
3492 {
3493 ULONGEST hi, lo, cu_index;
3494 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3495 iter += 8;
3496 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3497 iter += 8;
3498 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3499 iter += 4;
3500
3501 if (lo > hi)
3502 {
3503 complaint (&symfile_complaints,
3504 _(".gdb_index address table has invalid range (%s - %s)"),
3505 hex_string (lo), hex_string (hi));
3506 continue;
3507 }
3508
3509 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3510 {
3511 complaint (&symfile_complaints,
3512 _(".gdb_index address table has invalid CU number %u"),
3513 (unsigned) cu_index);
3514 continue;
3515 }
3516
3517 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3518 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3519 addrmap_set_empty (mutable_map, lo, hi - 1,
3520 dw2_get_cutu (dwarf2_per_objfile, cu_index));
3521 }
3522
3523 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3524 &objfile->objfile_obstack);
3525 }
3526
3527 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3528 populate the objfile's psymtabs_addrmap. */
3529
3530 static void
3531 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3532 struct dwarf2_section_info *section)
3533 {
3534 struct objfile *objfile = dwarf2_per_objfile->objfile;
3535 bfd *abfd = objfile->obfd;
3536 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3537 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3538 SECT_OFF_TEXT (objfile));
3539
3540 auto_obstack temp_obstack;
3541 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3542
3543 std::unordered_map<sect_offset,
3544 dwarf2_per_cu_data *,
3545 gdb::hash_enum<sect_offset>>
3546 debug_info_offset_to_per_cu;
3547 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3548 {
3549 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, cui);
3550 const auto insertpair
3551 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3552 if (!insertpair.second)
3553 {
3554 warning (_("Section .debug_aranges in %s has duplicate "
3555 "debug_info_offset %u, ignoring .debug_aranges."),
3556 objfile_name (objfile), to_underlying (per_cu->sect_off));
3557 return;
3558 }
3559 }
3560
3561 dwarf2_read_section (objfile, section);
3562
3563 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3564
3565 const gdb_byte *addr = section->buffer;
3566
3567 while (addr < section->buffer + section->size)
3568 {
3569 const gdb_byte *const entry_addr = addr;
3570 unsigned int bytes_read;
3571
3572 const LONGEST entry_length = read_initial_length (abfd, addr,
3573 &bytes_read);
3574 addr += bytes_read;
3575
3576 const gdb_byte *const entry_end = addr + entry_length;
3577 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3578 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3579 if (addr + entry_length > section->buffer + section->size)
3580 {
3581 warning (_("Section .debug_aranges in %s entry at offset %zu "
3582 "length %s exceeds section length %s, "
3583 "ignoring .debug_aranges."),
3584 objfile_name (objfile), entry_addr - section->buffer,
3585 plongest (bytes_read + entry_length),
3586 pulongest (section->size));
3587 return;
3588 }
3589
3590 /* The version number. */
3591 const uint16_t version = read_2_bytes (abfd, addr);
3592 addr += 2;
3593 if (version != 2)
3594 {
3595 warning (_("Section .debug_aranges in %s entry at offset %zu "
3596 "has unsupported version %d, ignoring .debug_aranges."),
3597 objfile_name (objfile), entry_addr - section->buffer,
3598 version);
3599 return;
3600 }
3601
3602 const uint64_t debug_info_offset
3603 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3604 addr += offset_size;
3605 const auto per_cu_it
3606 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3607 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3608 {
3609 warning (_("Section .debug_aranges in %s entry at offset %zu "
3610 "debug_info_offset %s does not exists, "
3611 "ignoring .debug_aranges."),
3612 objfile_name (objfile), entry_addr - section->buffer,
3613 pulongest (debug_info_offset));
3614 return;
3615 }
3616 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3617
3618 const uint8_t address_size = *addr++;
3619 if (address_size < 1 || address_size > 8)
3620 {
3621 warning (_("Section .debug_aranges in %s entry at offset %zu "
3622 "address_size %u is invalid, ignoring .debug_aranges."),
3623 objfile_name (objfile), entry_addr - section->buffer,
3624 address_size);
3625 return;
3626 }
3627
3628 const uint8_t segment_selector_size = *addr++;
3629 if (segment_selector_size != 0)
3630 {
3631 warning (_("Section .debug_aranges in %s entry at offset %zu "
3632 "segment_selector_size %u is not supported, "
3633 "ignoring .debug_aranges."),
3634 objfile_name (objfile), entry_addr - section->buffer,
3635 segment_selector_size);
3636 return;
3637 }
3638
3639 /* Must pad to an alignment boundary that is twice the address
3640 size. It is undocumented by the DWARF standard but GCC does
3641 use it. */
3642 for (size_t padding = ((-(addr - section->buffer))
3643 & (2 * address_size - 1));
3644 padding > 0; padding--)
3645 if (*addr++ != 0)
3646 {
3647 warning (_("Section .debug_aranges in %s entry at offset %zu "
3648 "padding is not zero, ignoring .debug_aranges."),
3649 objfile_name (objfile), entry_addr - section->buffer);
3650 return;
3651 }
3652
3653 for (;;)
3654 {
3655 if (addr + 2 * address_size > entry_end)
3656 {
3657 warning (_("Section .debug_aranges in %s entry at offset %zu "
3658 "address list is not properly terminated, "
3659 "ignoring .debug_aranges."),
3660 objfile_name (objfile), entry_addr - section->buffer);
3661 return;
3662 }
3663 ULONGEST start = extract_unsigned_integer (addr, address_size,
3664 dwarf5_byte_order);
3665 addr += address_size;
3666 ULONGEST length = extract_unsigned_integer (addr, address_size,
3667 dwarf5_byte_order);
3668 addr += address_size;
3669 if (start == 0 && length == 0)
3670 break;
3671 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3672 {
3673 /* Symbol was eliminated due to a COMDAT group. */
3674 continue;
3675 }
3676 ULONGEST end = start + length;
3677 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3678 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3679 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3680 }
3681 }
3682
3683 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3684 &objfile->objfile_obstack);
3685 }
3686
3687 /* The hash function for strings in the mapped index. This is the same as
3688 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3689 implementation. This is necessary because the hash function is tied to the
3690 format of the mapped index file. The hash values do not have to match with
3691 SYMBOL_HASH_NEXT.
3692
3693 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3694
3695 static hashval_t
3696 mapped_index_string_hash (int index_version, const void *p)
3697 {
3698 const unsigned char *str = (const unsigned char *) p;
3699 hashval_t r = 0;
3700 unsigned char c;
3701
3702 while ((c = *str++) != 0)
3703 {
3704 if (index_version >= 5)
3705 c = tolower (c);
3706 r = r * 67 + c - 113;
3707 }
3708
3709 return r;
3710 }
3711
3712 /* Find a slot in the mapped index INDEX for the object named NAME.
3713 If NAME is found, set *VEC_OUT to point to the CU vector in the
3714 constant pool and return true. If NAME cannot be found, return
3715 false. */
3716
3717 static bool
3718 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3719 offset_type **vec_out)
3720 {
3721 offset_type hash;
3722 offset_type slot, step;
3723 int (*cmp) (const char *, const char *);
3724
3725 gdb::unique_xmalloc_ptr<char> without_params;
3726 if (current_language->la_language == language_cplus
3727 || current_language->la_language == language_fortran
3728 || current_language->la_language == language_d)
3729 {
3730 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3731 not contain any. */
3732
3733 if (strchr (name, '(') != NULL)
3734 {
3735 without_params = cp_remove_params (name);
3736
3737 if (without_params != NULL)
3738 name = without_params.get ();
3739 }
3740 }
3741
3742 /* Index version 4 did not support case insensitive searches. But the
3743 indices for case insensitive languages are built in lowercase, therefore
3744 simulate our NAME being searched is also lowercased. */
3745 hash = mapped_index_string_hash ((index->version == 4
3746 && case_sensitivity == case_sensitive_off
3747 ? 5 : index->version),
3748 name);
3749
3750 slot = hash & (index->symbol_table.size () - 1);
3751 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3752 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3753
3754 for (;;)
3755 {
3756 const char *str;
3757
3758 const auto &bucket = index->symbol_table[slot];
3759 if (bucket.name == 0 && bucket.vec == 0)
3760 return false;
3761
3762 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3763 if (!cmp (name, str))
3764 {
3765 *vec_out = (offset_type *) (index->constant_pool
3766 + MAYBE_SWAP (bucket.vec));
3767 return true;
3768 }
3769
3770 slot = (slot + step) & (index->symbol_table.size () - 1);
3771 }
3772 }
3773
3774 /* A helper function that reads the .gdb_index from SECTION and fills
3775 in MAP. FILENAME is the name of the file containing the section;
3776 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3777 ok to use deprecated sections.
3778
3779 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3780 out parameters that are filled in with information about the CU and
3781 TU lists in the section.
3782
3783 Returns 1 if all went well, 0 otherwise. */
3784
3785 static int
3786 read_index_from_section (struct objfile *objfile,
3787 const char *filename,
3788 int deprecated_ok,
3789 struct dwarf2_section_info *section,
3790 struct mapped_index *map,
3791 const gdb_byte **cu_list,
3792 offset_type *cu_list_elements,
3793 const gdb_byte **types_list,
3794 offset_type *types_list_elements)
3795 {
3796 const gdb_byte *addr;
3797 offset_type version;
3798 offset_type *metadata;
3799 int i;
3800
3801 if (dwarf2_section_empty_p (section))
3802 return 0;
3803
3804 /* Older elfutils strip versions could keep the section in the main
3805 executable while splitting it for the separate debug info file. */
3806 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3807 return 0;
3808
3809 dwarf2_read_section (objfile, section);
3810
3811 addr = section->buffer;
3812 /* Version check. */
3813 version = MAYBE_SWAP (*(offset_type *) addr);
3814 /* Versions earlier than 3 emitted every copy of a psymbol. This
3815 causes the index to behave very poorly for certain requests. Version 3
3816 contained incomplete addrmap. So, it seems better to just ignore such
3817 indices. */
3818 if (version < 4)
3819 {
3820 static int warning_printed = 0;
3821 if (!warning_printed)
3822 {
3823 warning (_("Skipping obsolete .gdb_index section in %s."),
3824 filename);
3825 warning_printed = 1;
3826 }
3827 return 0;
3828 }
3829 /* Index version 4 uses a different hash function than index version
3830 5 and later.
3831
3832 Versions earlier than 6 did not emit psymbols for inlined
3833 functions. Using these files will cause GDB not to be able to
3834 set breakpoints on inlined functions by name, so we ignore these
3835 indices unless the user has done
3836 "set use-deprecated-index-sections on". */
3837 if (version < 6 && !deprecated_ok)
3838 {
3839 static int warning_printed = 0;
3840 if (!warning_printed)
3841 {
3842 warning (_("\
3843 Skipping deprecated .gdb_index section in %s.\n\
3844 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3845 to use the section anyway."),
3846 filename);
3847 warning_printed = 1;
3848 }
3849 return 0;
3850 }
3851 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3852 of the TU (for symbols coming from TUs),
3853 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3854 Plus gold-generated indices can have duplicate entries for global symbols,
3855 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3856 These are just performance bugs, and we can't distinguish gdb-generated
3857 indices from gold-generated ones, so issue no warning here. */
3858
3859 /* Indexes with higher version than the one supported by GDB may be no
3860 longer backward compatible. */
3861 if (version > 8)
3862 return 0;
3863
3864 map->version = version;
3865 map->total_size = section->size;
3866
3867 metadata = (offset_type *) (addr + sizeof (offset_type));
3868
3869 i = 0;
3870 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3871 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3872 / 8);
3873 ++i;
3874
3875 *types_list = addr + MAYBE_SWAP (metadata[i]);
3876 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3877 - MAYBE_SWAP (metadata[i]))
3878 / 8);
3879 ++i;
3880
3881 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3882 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3883 map->address_table
3884 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3885 ++i;
3886
3887 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3888 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3889 map->symbol_table
3890 = gdb::array_view<mapped_index::symbol_table_slot>
3891 ((mapped_index::symbol_table_slot *) symbol_table,
3892 (mapped_index::symbol_table_slot *) symbol_table_end);
3893
3894 ++i;
3895 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3896
3897 return 1;
3898 }
3899
3900 /* Read .gdb_index. If everything went ok, initialize the "quick"
3901 elements of all the CUs and return 1. Otherwise, return 0. */
3902
3903 static int
3904 dwarf2_read_index (struct objfile *objfile)
3905 {
3906 struct mapped_index local_map, *map;
3907 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3908 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3909 struct dwz_file *dwz;
3910 struct dwarf2_per_objfile *dwarf2_per_objfile
3911 = get_dwarf2_per_objfile (objfile);
3912
3913 if (!read_index_from_section (objfile, objfile_name (objfile),
3914 use_deprecated_index_sections,
3915 &dwarf2_per_objfile->gdb_index, &local_map,
3916 &cu_list, &cu_list_elements,
3917 &types_list, &types_list_elements))
3918 return 0;
3919
3920 /* Don't use the index if it's empty. */
3921 if (local_map.symbol_table.empty ())
3922 return 0;
3923
3924 /* If there is a .dwz file, read it so we can get its CU list as
3925 well. */
3926 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3927 if (dwz != NULL)
3928 {
3929 struct mapped_index dwz_map;
3930 const gdb_byte *dwz_types_ignore;
3931 offset_type dwz_types_elements_ignore;
3932
3933 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3934 1,
3935 &dwz->gdb_index, &dwz_map,
3936 &dwz_list, &dwz_list_elements,
3937 &dwz_types_ignore,
3938 &dwz_types_elements_ignore))
3939 {
3940 warning (_("could not read '.gdb_index' section from %s; skipping"),
3941 bfd_get_filename (dwz->dwz_bfd));
3942 return 0;
3943 }
3944 }
3945
3946 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3947 dwz_list_elements);
3948
3949 if (types_list_elements)
3950 {
3951 struct dwarf2_section_info *section;
3952
3953 /* We can only handle a single .debug_types when we have an
3954 index. */
3955 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3956 return 0;
3957
3958 section = VEC_index (dwarf2_section_info_def,
3959 dwarf2_per_objfile->types, 0);
3960
3961 create_signatured_type_table_from_index (objfile, section, types_list,
3962 types_list_elements);
3963 }
3964
3965 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
3966
3967 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3968 map = new (map) mapped_index ();
3969 *map = local_map;
3970
3971 dwarf2_per_objfile->index_table = map;
3972 dwarf2_per_objfile->using_index = 1;
3973 dwarf2_per_objfile->quick_file_names_table =
3974 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3975
3976 return 1;
3977 }
3978
3979 /* die_reader_func for dw2_get_file_names. */
3980
3981 static void
3982 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3983 const gdb_byte *info_ptr,
3984 struct die_info *comp_unit_die,
3985 int has_children,
3986 void *data)
3987 {
3988 struct dwarf2_cu *cu = reader->cu;
3989 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3990 struct dwarf2_per_objfile *dwarf2_per_objfile
3991 = cu->per_cu->dwarf2_per_objfile;
3992 struct objfile *objfile = dwarf2_per_objfile->objfile;
3993 struct dwarf2_per_cu_data *lh_cu;
3994 struct attribute *attr;
3995 int i;
3996 void **slot;
3997 struct quick_file_names *qfn;
3998
3999 gdb_assert (! this_cu->is_debug_types);
4000
4001 /* Our callers never want to match partial units -- instead they
4002 will match the enclosing full CU. */
4003 if (comp_unit_die->tag == DW_TAG_partial_unit)
4004 {
4005 this_cu->v.quick->no_file_data = 1;
4006 return;
4007 }
4008
4009 lh_cu = this_cu;
4010 slot = NULL;
4011
4012 line_header_up lh;
4013 sect_offset line_offset {};
4014
4015 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4016 if (attr)
4017 {
4018 struct quick_file_names find_entry;
4019
4020 line_offset = (sect_offset) DW_UNSND (attr);
4021
4022 /* We may have already read in this line header (TU line header sharing).
4023 If we have we're done. */
4024 find_entry.hash.dwo_unit = cu->dwo_unit;
4025 find_entry.hash.line_sect_off = line_offset;
4026 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
4027 &find_entry, INSERT);
4028 if (*slot != NULL)
4029 {
4030 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
4031 return;
4032 }
4033
4034 lh = dwarf_decode_line_header (line_offset, cu);
4035 }
4036 if (lh == NULL)
4037 {
4038 lh_cu->v.quick->no_file_data = 1;
4039 return;
4040 }
4041
4042 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
4043 qfn->hash.dwo_unit = cu->dwo_unit;
4044 qfn->hash.line_sect_off = line_offset;
4045 gdb_assert (slot != NULL);
4046 *slot = qfn;
4047
4048 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
4049
4050 qfn->num_file_names = lh->file_names.size ();
4051 qfn->file_names =
4052 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
4053 for (i = 0; i < lh->file_names.size (); ++i)
4054 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
4055 qfn->real_names = NULL;
4056
4057 lh_cu->v.quick->file_names = qfn;
4058 }
4059
4060 /* A helper for the "quick" functions which attempts to read the line
4061 table for THIS_CU. */
4062
4063 static struct quick_file_names *
4064 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
4065 {
4066 /* This should never be called for TUs. */
4067 gdb_assert (! this_cu->is_debug_types);
4068 /* Nor type unit groups. */
4069 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
4070
4071 if (this_cu->v.quick->file_names != NULL)
4072 return this_cu->v.quick->file_names;
4073 /* If we know there is no line data, no point in looking again. */
4074 if (this_cu->v.quick->no_file_data)
4075 return NULL;
4076
4077 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
4078
4079 if (this_cu->v.quick->no_file_data)
4080 return NULL;
4081 return this_cu->v.quick->file_names;
4082 }
4083
4084 /* A helper for the "quick" functions which computes and caches the
4085 real path for a given file name from the line table. */
4086
4087 static const char *
4088 dw2_get_real_path (struct objfile *objfile,
4089 struct quick_file_names *qfn, int index)
4090 {
4091 if (qfn->real_names == NULL)
4092 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
4093 qfn->num_file_names, const char *);
4094
4095 if (qfn->real_names[index] == NULL)
4096 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
4097
4098 return qfn->real_names[index];
4099 }
4100
4101 static struct symtab *
4102 dw2_find_last_source_symtab (struct objfile *objfile)
4103 {
4104 struct dwarf2_per_objfile *dwarf2_per_objfile
4105 = get_dwarf2_per_objfile (objfile);
4106 int index = dwarf2_per_objfile->n_comp_units - 1;
4107 dwarf2_per_cu_data *dwarf_cu = dw2_get_cutu (dwarf2_per_objfile, index);
4108 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
4109
4110 if (cust == NULL)
4111 return NULL;
4112
4113 return compunit_primary_filetab (cust);
4114 }
4115
4116 /* Traversal function for dw2_forget_cached_source_info. */
4117
4118 static int
4119 dw2_free_cached_file_names (void **slot, void *info)
4120 {
4121 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
4122
4123 if (file_data->real_names)
4124 {
4125 int i;
4126
4127 for (i = 0; i < file_data->num_file_names; ++i)
4128 {
4129 xfree ((void*) file_data->real_names[i]);
4130 file_data->real_names[i] = NULL;
4131 }
4132 }
4133
4134 return 1;
4135 }
4136
4137 static void
4138 dw2_forget_cached_source_info (struct objfile *objfile)
4139 {
4140 struct dwarf2_per_objfile *dwarf2_per_objfile
4141 = get_dwarf2_per_objfile (objfile);
4142
4143 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
4144 dw2_free_cached_file_names, NULL);
4145 }
4146
4147 /* Helper function for dw2_map_symtabs_matching_filename that expands
4148 the symtabs and calls the iterator. */
4149
4150 static int
4151 dw2_map_expand_apply (struct objfile *objfile,
4152 struct dwarf2_per_cu_data *per_cu,
4153 const char *name, const char *real_path,
4154 gdb::function_view<bool (symtab *)> callback)
4155 {
4156 struct compunit_symtab *last_made = objfile->compunit_symtabs;
4157
4158 /* Don't visit already-expanded CUs. */
4159 if (per_cu->v.quick->compunit_symtab)
4160 return 0;
4161
4162 /* This may expand more than one symtab, and we want to iterate over
4163 all of them. */
4164 dw2_instantiate_symtab (per_cu);
4165
4166 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4167 last_made, callback);
4168 }
4169
4170 /* Implementation of the map_symtabs_matching_filename method. */
4171
4172 static bool
4173 dw2_map_symtabs_matching_filename
4174 (struct objfile *objfile, const char *name, const char *real_path,
4175 gdb::function_view<bool (symtab *)> callback)
4176 {
4177 int i;
4178 const char *name_basename = lbasename (name);
4179 struct dwarf2_per_objfile *dwarf2_per_objfile
4180 = get_dwarf2_per_objfile (objfile);
4181
4182 /* The rule is CUs specify all the files, including those used by
4183 any TU, so there's no need to scan TUs here. */
4184
4185 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4186 {
4187 int j;
4188 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
4189 struct quick_file_names *file_data;
4190
4191 /* We only need to look at symtabs not already expanded. */
4192 if (per_cu->v.quick->compunit_symtab)
4193 continue;
4194
4195 file_data = dw2_get_file_names (per_cu);
4196 if (file_data == NULL)
4197 continue;
4198
4199 for (j = 0; j < file_data->num_file_names; ++j)
4200 {
4201 const char *this_name = file_data->file_names[j];
4202 const char *this_real_name;
4203
4204 if (compare_filenames_for_search (this_name, name))
4205 {
4206 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4207 callback))
4208 return true;
4209 continue;
4210 }
4211
4212 /* Before we invoke realpath, which can get expensive when many
4213 files are involved, do a quick comparison of the basenames. */
4214 if (! basenames_may_differ
4215 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4216 continue;
4217
4218 this_real_name = dw2_get_real_path (objfile, file_data, j);
4219 if (compare_filenames_for_search (this_real_name, name))
4220 {
4221 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4222 callback))
4223 return true;
4224 continue;
4225 }
4226
4227 if (real_path != NULL)
4228 {
4229 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4230 gdb_assert (IS_ABSOLUTE_PATH (name));
4231 if (this_real_name != NULL
4232 && FILENAME_CMP (real_path, this_real_name) == 0)
4233 {
4234 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4235 callback))
4236 return true;
4237 continue;
4238 }
4239 }
4240 }
4241 }
4242
4243 return false;
4244 }
4245
4246 /* Struct used to manage iterating over all CUs looking for a symbol. */
4247
4248 struct dw2_symtab_iterator
4249 {
4250 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
4251 struct dwarf2_per_objfile *dwarf2_per_objfile;
4252 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4253 int want_specific_block;
4254 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4255 Unused if !WANT_SPECIFIC_BLOCK. */
4256 int block_index;
4257 /* The kind of symbol we're looking for. */
4258 domain_enum domain;
4259 /* The list of CUs from the index entry of the symbol,
4260 or NULL if not found. */
4261 offset_type *vec;
4262 /* The next element in VEC to look at. */
4263 int next;
4264 /* The number of elements in VEC, or zero if there is no match. */
4265 int length;
4266 /* Have we seen a global version of the symbol?
4267 If so we can ignore all further global instances.
4268 This is to work around gold/15646, inefficient gold-generated
4269 indices. */
4270 int global_seen;
4271 };
4272
4273 /* Initialize the index symtab iterator ITER.
4274 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4275 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
4276
4277 static void
4278 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
4279 struct dwarf2_per_objfile *dwarf2_per_objfile,
4280 int want_specific_block,
4281 int block_index,
4282 domain_enum domain,
4283 const char *name)
4284 {
4285 iter->dwarf2_per_objfile = dwarf2_per_objfile;
4286 iter->want_specific_block = want_specific_block;
4287 iter->block_index = block_index;
4288 iter->domain = domain;
4289 iter->next = 0;
4290 iter->global_seen = 0;
4291
4292 mapped_index *index = dwarf2_per_objfile->index_table;
4293
4294 /* index is NULL if OBJF_READNOW. */
4295 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
4296 iter->length = MAYBE_SWAP (*iter->vec);
4297 else
4298 {
4299 iter->vec = NULL;
4300 iter->length = 0;
4301 }
4302 }
4303
4304 /* Return the next matching CU or NULL if there are no more. */
4305
4306 static struct dwarf2_per_cu_data *
4307 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4308 {
4309 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4310
4311 for ( ; iter->next < iter->length; ++iter->next)
4312 {
4313 offset_type cu_index_and_attrs =
4314 MAYBE_SWAP (iter->vec[iter->next + 1]);
4315 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4316 struct dwarf2_per_cu_data *per_cu;
4317 int want_static = iter->block_index != GLOBAL_BLOCK;
4318 /* This value is only valid for index versions >= 7. */
4319 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4320 gdb_index_symbol_kind symbol_kind =
4321 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4322 /* Only check the symbol attributes if they're present.
4323 Indices prior to version 7 don't record them,
4324 and indices >= 7 may elide them for certain symbols
4325 (gold does this). */
4326 int attrs_valid =
4327 (dwarf2_per_objfile->index_table->version >= 7
4328 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4329
4330 /* Don't crash on bad data. */
4331 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4332 + dwarf2_per_objfile->n_type_units))
4333 {
4334 complaint (&symfile_complaints,
4335 _(".gdb_index entry has bad CU index"
4336 " [in module %s]"),
4337 objfile_name (dwarf2_per_objfile->objfile));
4338 continue;
4339 }
4340
4341 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
4342
4343 /* Skip if already read in. */
4344 if (per_cu->v.quick->compunit_symtab)
4345 continue;
4346
4347 /* Check static vs global. */
4348 if (attrs_valid)
4349 {
4350 if (iter->want_specific_block
4351 && want_static != is_static)
4352 continue;
4353 /* Work around gold/15646. */
4354 if (!is_static && iter->global_seen)
4355 continue;
4356 if (!is_static)
4357 iter->global_seen = 1;
4358 }
4359
4360 /* Only check the symbol's kind if it has one. */
4361 if (attrs_valid)
4362 {
4363 switch (iter->domain)
4364 {
4365 case VAR_DOMAIN:
4366 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4367 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4368 /* Some types are also in VAR_DOMAIN. */
4369 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4370 continue;
4371 break;
4372 case STRUCT_DOMAIN:
4373 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4374 continue;
4375 break;
4376 case LABEL_DOMAIN:
4377 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4378 continue;
4379 break;
4380 default:
4381 break;
4382 }
4383 }
4384
4385 ++iter->next;
4386 return per_cu;
4387 }
4388
4389 return NULL;
4390 }
4391
4392 static struct compunit_symtab *
4393 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4394 const char *name, domain_enum domain)
4395 {
4396 struct compunit_symtab *stab_best = NULL;
4397 struct dwarf2_per_objfile *dwarf2_per_objfile
4398 = get_dwarf2_per_objfile (objfile);
4399
4400 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4401
4402 struct dw2_symtab_iterator iter;
4403 struct dwarf2_per_cu_data *per_cu;
4404
4405 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4406
4407 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4408 {
4409 struct symbol *sym, *with_opaque = NULL;
4410 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4411 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4412 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4413
4414 sym = block_find_symbol (block, name, domain,
4415 block_find_non_opaque_type_preferred,
4416 &with_opaque);
4417
4418 /* Some caution must be observed with overloaded functions
4419 and methods, since the index will not contain any overload
4420 information (but NAME might contain it). */
4421
4422 if (sym != NULL
4423 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4424 return stab;
4425 if (with_opaque != NULL
4426 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4427 stab_best = stab;
4428
4429 /* Keep looking through other CUs. */
4430 }
4431
4432 return stab_best;
4433 }
4434
4435 static void
4436 dw2_print_stats (struct objfile *objfile)
4437 {
4438 struct dwarf2_per_objfile *dwarf2_per_objfile
4439 = get_dwarf2_per_objfile (objfile);
4440 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4441 int count = 0;
4442
4443 for (int i = 0; i < total; ++i)
4444 {
4445 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4446
4447 if (!per_cu->v.quick->compunit_symtab)
4448 ++count;
4449 }
4450 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4451 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4452 }
4453
4454 /* This dumps minimal information about the index.
4455 It is called via "mt print objfiles".
4456 One use is to verify .gdb_index has been loaded by the
4457 gdb.dwarf2/gdb-index.exp testcase. */
4458
4459 static void
4460 dw2_dump (struct objfile *objfile)
4461 {
4462 struct dwarf2_per_objfile *dwarf2_per_objfile
4463 = get_dwarf2_per_objfile (objfile);
4464
4465 gdb_assert (dwarf2_per_objfile->using_index);
4466 printf_filtered (".gdb_index:");
4467 if (dwarf2_per_objfile->index_table != NULL)
4468 {
4469 printf_filtered (" version %d\n",
4470 dwarf2_per_objfile->index_table->version);
4471 }
4472 else
4473 printf_filtered (" faked for \"readnow\"\n");
4474 printf_filtered ("\n");
4475 }
4476
4477 static void
4478 dw2_relocate (struct objfile *objfile,
4479 const struct section_offsets *new_offsets,
4480 const struct section_offsets *delta)
4481 {
4482 /* There's nothing to relocate here. */
4483 }
4484
4485 static void
4486 dw2_expand_symtabs_for_function (struct objfile *objfile,
4487 const char *func_name)
4488 {
4489 struct dwarf2_per_objfile *dwarf2_per_objfile
4490 = get_dwarf2_per_objfile (objfile);
4491
4492 struct dw2_symtab_iterator iter;
4493 struct dwarf2_per_cu_data *per_cu;
4494
4495 /* Note: It doesn't matter what we pass for block_index here. */
4496 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4497 func_name);
4498
4499 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4500 dw2_instantiate_symtab (per_cu);
4501
4502 }
4503
4504 static void
4505 dw2_expand_all_symtabs (struct objfile *objfile)
4506 {
4507 struct dwarf2_per_objfile *dwarf2_per_objfile
4508 = get_dwarf2_per_objfile (objfile);
4509 int total_units = (dwarf2_per_objfile->n_comp_units
4510 + dwarf2_per_objfile->n_type_units);
4511
4512 for (int i = 0; i < total_units; ++i)
4513 {
4514 struct dwarf2_per_cu_data *per_cu
4515 = dw2_get_cutu (dwarf2_per_objfile, i);
4516
4517 dw2_instantiate_symtab (per_cu);
4518 }
4519 }
4520
4521 static void
4522 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4523 const char *fullname)
4524 {
4525 struct dwarf2_per_objfile *dwarf2_per_objfile
4526 = get_dwarf2_per_objfile (objfile);
4527
4528 /* We don't need to consider type units here.
4529 This is only called for examining code, e.g. expand_line_sal.
4530 There can be an order of magnitude (or more) more type units
4531 than comp units, and we avoid them if we can. */
4532
4533 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4534 {
4535 int j;
4536 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4537 struct quick_file_names *file_data;
4538
4539 /* We only need to look at symtabs not already expanded. */
4540 if (per_cu->v.quick->compunit_symtab)
4541 continue;
4542
4543 file_data = dw2_get_file_names (per_cu);
4544 if (file_data == NULL)
4545 continue;
4546
4547 for (j = 0; j < file_data->num_file_names; ++j)
4548 {
4549 const char *this_fullname = file_data->file_names[j];
4550
4551 if (filename_cmp (this_fullname, fullname) == 0)
4552 {
4553 dw2_instantiate_symtab (per_cu);
4554 break;
4555 }
4556 }
4557 }
4558 }
4559
4560 static void
4561 dw2_map_matching_symbols (struct objfile *objfile,
4562 const char * name, domain_enum domain,
4563 int global,
4564 int (*callback) (struct block *,
4565 struct symbol *, void *),
4566 void *data, symbol_name_match_type match,
4567 symbol_compare_ftype *ordered_compare)
4568 {
4569 /* Currently unimplemented; used for Ada. The function can be called if the
4570 current language is Ada for a non-Ada objfile using GNU index. As Ada
4571 does not look for non-Ada symbols this function should just return. */
4572 }
4573
4574 /* Symbol name matcher for .gdb_index names.
4575
4576 Symbol names in .gdb_index have a few particularities:
4577
4578 - There's no indication of which is the language of each symbol.
4579
4580 Since each language has its own symbol name matching algorithm,
4581 and we don't know which language is the right one, we must match
4582 each symbol against all languages. This would be a potential
4583 performance problem if it were not mitigated by the
4584 mapped_index::name_components lookup table, which significantly
4585 reduces the number of times we need to call into this matcher,
4586 making it a non-issue.
4587
4588 - Symbol names in the index have no overload (parameter)
4589 information. I.e., in C++, "foo(int)" and "foo(long)" both
4590 appear as "foo" in the index, for example.
4591
4592 This means that the lookup names passed to the symbol name
4593 matcher functions must have no parameter information either
4594 because (e.g.) symbol search name "foo" does not match
4595 lookup-name "foo(int)" [while swapping search name for lookup
4596 name would match].
4597 */
4598 class gdb_index_symbol_name_matcher
4599 {
4600 public:
4601 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4602 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4603
4604 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4605 Returns true if any matcher matches. */
4606 bool matches (const char *symbol_name);
4607
4608 private:
4609 /* A reference to the lookup name we're matching against. */
4610 const lookup_name_info &m_lookup_name;
4611
4612 /* A vector holding all the different symbol name matchers, for all
4613 languages. */
4614 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4615 };
4616
4617 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4618 (const lookup_name_info &lookup_name)
4619 : m_lookup_name (lookup_name)
4620 {
4621 /* Prepare the vector of comparison functions upfront, to avoid
4622 doing the same work for each symbol. Care is taken to avoid
4623 matching with the same matcher more than once if/when multiple
4624 languages use the same matcher function. */
4625 auto &matchers = m_symbol_name_matcher_funcs;
4626 matchers.reserve (nr_languages);
4627
4628 matchers.push_back (default_symbol_name_matcher);
4629
4630 for (int i = 0; i < nr_languages; i++)
4631 {
4632 const language_defn *lang = language_def ((enum language) i);
4633 symbol_name_matcher_ftype *name_matcher
4634 = get_symbol_name_matcher (lang, m_lookup_name);
4635
4636 /* Don't insert the same comparison routine more than once.
4637 Note that we do this linear walk instead of a seemingly
4638 cheaper sorted insert, or use a std::set or something like
4639 that, because relative order of function addresses is not
4640 stable. This is not a problem in practice because the number
4641 of supported languages is low, and the cost here is tiny
4642 compared to the number of searches we'll do afterwards using
4643 this object. */
4644 if (name_matcher != default_symbol_name_matcher
4645 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4646 == matchers.end ()))
4647 matchers.push_back (name_matcher);
4648 }
4649 }
4650
4651 bool
4652 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4653 {
4654 for (auto matches_name : m_symbol_name_matcher_funcs)
4655 if (matches_name (symbol_name, m_lookup_name, NULL))
4656 return true;
4657
4658 return false;
4659 }
4660
4661 /* Starting from a search name, return the string that finds the upper
4662 bound of all strings that start with SEARCH_NAME in a sorted name
4663 list. Returns the empty string to indicate that the upper bound is
4664 the end of the list. */
4665
4666 static std::string
4667 make_sort_after_prefix_name (const char *search_name)
4668 {
4669 /* When looking to complete "func", we find the upper bound of all
4670 symbols that start with "func" by looking for where we'd insert
4671 the closest string that would follow "func" in lexicographical
4672 order. Usually, that's "func"-with-last-character-incremented,
4673 i.e. "fund". Mind non-ASCII characters, though. Usually those
4674 will be UTF-8 multi-byte sequences, but we can't be certain.
4675 Especially mind the 0xff character, which is a valid character in
4676 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4677 rule out compilers allowing it in identifiers. Note that
4678 conveniently, strcmp/strcasecmp are specified to compare
4679 characters interpreted as unsigned char. So what we do is treat
4680 the whole string as a base 256 number composed of a sequence of
4681 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4682 to 0, and carries 1 to the following more-significant position.
4683 If the very first character in SEARCH_NAME ends up incremented
4684 and carries/overflows, then the upper bound is the end of the
4685 list. The string after the empty string is also the empty
4686 string.
4687
4688 Some examples of this operation:
4689
4690 SEARCH_NAME => "+1" RESULT
4691
4692 "abc" => "abd"
4693 "ab\xff" => "ac"
4694 "\xff" "a" "\xff" => "\xff" "b"
4695 "\xff" => ""
4696 "\xff\xff" => ""
4697 "" => ""
4698
4699 Then, with these symbols for example:
4700
4701 func
4702 func1
4703 fund
4704
4705 completing "func" looks for symbols between "func" and
4706 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4707 which finds "func" and "func1", but not "fund".
4708
4709 And with:
4710
4711 funcÿ (Latin1 'ÿ' [0xff])
4712 funcÿ1
4713 fund
4714
4715 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4716 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4717
4718 And with:
4719
4720 ÿÿ (Latin1 'ÿ' [0xff])
4721 ÿÿ1
4722
4723 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4724 the end of the list.
4725 */
4726 std::string after = search_name;
4727 while (!after.empty () && (unsigned char) after.back () == 0xff)
4728 after.pop_back ();
4729 if (!after.empty ())
4730 after.back () = (unsigned char) after.back () + 1;
4731 return after;
4732 }
4733
4734 /* See declaration. */
4735
4736 std::pair<std::vector<name_component>::const_iterator,
4737 std::vector<name_component>::const_iterator>
4738 mapped_index_base::find_name_components_bounds
4739 (const lookup_name_info &lookup_name_without_params) const
4740 {
4741 auto *name_cmp
4742 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4743
4744 const char *cplus
4745 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4746
4747 /* Comparison function object for lower_bound that matches against a
4748 given symbol name. */
4749 auto lookup_compare_lower = [&] (const name_component &elem,
4750 const char *name)
4751 {
4752 const char *elem_qualified = this->symbol_name_at (elem.idx);
4753 const char *elem_name = elem_qualified + elem.name_offset;
4754 return name_cmp (elem_name, name) < 0;
4755 };
4756
4757 /* Comparison function object for upper_bound that matches against a
4758 given symbol name. */
4759 auto lookup_compare_upper = [&] (const char *name,
4760 const name_component &elem)
4761 {
4762 const char *elem_qualified = this->symbol_name_at (elem.idx);
4763 const char *elem_name = elem_qualified + elem.name_offset;
4764 return name_cmp (name, elem_name) < 0;
4765 };
4766
4767 auto begin = this->name_components.begin ();
4768 auto end = this->name_components.end ();
4769
4770 /* Find the lower bound. */
4771 auto lower = [&] ()
4772 {
4773 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4774 return begin;
4775 else
4776 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4777 } ();
4778
4779 /* Find the upper bound. */
4780 auto upper = [&] ()
4781 {
4782 if (lookup_name_without_params.completion_mode ())
4783 {
4784 /* In completion mode, we want UPPER to point past all
4785 symbols names that have the same prefix. I.e., with
4786 these symbols, and completing "func":
4787
4788 function << lower bound
4789 function1
4790 other_function << upper bound
4791
4792 We find the upper bound by looking for the insertion
4793 point of "func"-with-last-character-incremented,
4794 i.e. "fund". */
4795 std::string after = make_sort_after_prefix_name (cplus);
4796 if (after.empty ())
4797 return end;
4798 return std::lower_bound (lower, end, after.c_str (),
4799 lookup_compare_lower);
4800 }
4801 else
4802 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4803 } ();
4804
4805 return {lower, upper};
4806 }
4807
4808 /* See declaration. */
4809
4810 void
4811 mapped_index_base::build_name_components ()
4812 {
4813 if (!this->name_components.empty ())
4814 return;
4815
4816 this->name_components_casing = case_sensitivity;
4817 auto *name_cmp
4818 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4819
4820 /* The code below only knows how to break apart components of C++
4821 symbol names (and other languages that use '::' as
4822 namespace/module separator). If we add support for wild matching
4823 to some language that uses some other operator (E.g., Ada, Go and
4824 D use '.'), then we'll need to try splitting the symbol name
4825 according to that language too. Note that Ada does support wild
4826 matching, but doesn't currently support .gdb_index. */
4827 auto count = this->symbol_name_count ();
4828 for (offset_type idx = 0; idx < count; idx++)
4829 {
4830 if (this->symbol_name_slot_invalid (idx))
4831 continue;
4832
4833 const char *name = this->symbol_name_at (idx);
4834
4835 /* Add each name component to the name component table. */
4836 unsigned int previous_len = 0;
4837 for (unsigned int current_len = cp_find_first_component (name);
4838 name[current_len] != '\0';
4839 current_len += cp_find_first_component (name + current_len))
4840 {
4841 gdb_assert (name[current_len] == ':');
4842 this->name_components.push_back ({previous_len, idx});
4843 /* Skip the '::'. */
4844 current_len += 2;
4845 previous_len = current_len;
4846 }
4847 this->name_components.push_back ({previous_len, idx});
4848 }
4849
4850 /* Sort name_components elements by name. */
4851 auto name_comp_compare = [&] (const name_component &left,
4852 const name_component &right)
4853 {
4854 const char *left_qualified = this->symbol_name_at (left.idx);
4855 const char *right_qualified = this->symbol_name_at (right.idx);
4856
4857 const char *left_name = left_qualified + left.name_offset;
4858 const char *right_name = right_qualified + right.name_offset;
4859
4860 return name_cmp (left_name, right_name) < 0;
4861 };
4862
4863 std::sort (this->name_components.begin (),
4864 this->name_components.end (),
4865 name_comp_compare);
4866 }
4867
4868 /* Helper for dw2_expand_symtabs_matching that works with a
4869 mapped_index_base instead of the containing objfile. This is split
4870 to a separate function in order to be able to unit test the
4871 name_components matching using a mock mapped_index_base. For each
4872 symbol name that matches, calls MATCH_CALLBACK, passing it the
4873 symbol's index in the mapped_index_base symbol table. */
4874
4875 static void
4876 dw2_expand_symtabs_matching_symbol
4877 (mapped_index_base &index,
4878 const lookup_name_info &lookup_name_in,
4879 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4880 enum search_domain kind,
4881 gdb::function_view<void (offset_type)> match_callback)
4882 {
4883 lookup_name_info lookup_name_without_params
4884 = lookup_name_in.make_ignore_params ();
4885 gdb_index_symbol_name_matcher lookup_name_matcher
4886 (lookup_name_without_params);
4887
4888 /* Build the symbol name component sorted vector, if we haven't
4889 yet. */
4890 index.build_name_components ();
4891
4892 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4893
4894 /* Now for each symbol name in range, check to see if we have a name
4895 match, and if so, call the MATCH_CALLBACK callback. */
4896
4897 /* The same symbol may appear more than once in the range though.
4898 E.g., if we're looking for symbols that complete "w", and we have
4899 a symbol named "w1::w2", we'll find the two name components for
4900 that same symbol in the range. To be sure we only call the
4901 callback once per symbol, we first collect the symbol name
4902 indexes that matched in a temporary vector and ignore
4903 duplicates. */
4904 std::vector<offset_type> matches;
4905 matches.reserve (std::distance (bounds.first, bounds.second));
4906
4907 for (; bounds.first != bounds.second; ++bounds.first)
4908 {
4909 const char *qualified = index.symbol_name_at (bounds.first->idx);
4910
4911 if (!lookup_name_matcher.matches (qualified)
4912 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4913 continue;
4914
4915 matches.push_back (bounds.first->idx);
4916 }
4917
4918 std::sort (matches.begin (), matches.end ());
4919
4920 /* Finally call the callback, once per match. */
4921 ULONGEST prev = -1;
4922 for (offset_type idx : matches)
4923 {
4924 if (prev != idx)
4925 {
4926 match_callback (idx);
4927 prev = idx;
4928 }
4929 }
4930
4931 /* Above we use a type wider than idx's for 'prev', since 0 and
4932 (offset_type)-1 are both possible values. */
4933 static_assert (sizeof (prev) > sizeof (offset_type), "");
4934 }
4935
4936 #if GDB_SELF_TEST
4937
4938 namespace selftests { namespace dw2_expand_symtabs_matching {
4939
4940 /* A mock .gdb_index/.debug_names-like name index table, enough to
4941 exercise dw2_expand_symtabs_matching_symbol, which works with the
4942 mapped_index_base interface. Builds an index from the symbol list
4943 passed as parameter to the constructor. */
4944 class mock_mapped_index : public mapped_index_base
4945 {
4946 public:
4947 mock_mapped_index (gdb::array_view<const char *> symbols)
4948 : m_symbol_table (symbols)
4949 {}
4950
4951 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4952
4953 /* Return the number of names in the symbol table. */
4954 virtual size_t symbol_name_count () const
4955 {
4956 return m_symbol_table.size ();
4957 }
4958
4959 /* Get the name of the symbol at IDX in the symbol table. */
4960 virtual const char *symbol_name_at (offset_type idx) const
4961 {
4962 return m_symbol_table[idx];
4963 }
4964
4965 private:
4966 gdb::array_view<const char *> m_symbol_table;
4967 };
4968
4969 /* Convenience function that converts a NULL pointer to a "<null>"
4970 string, to pass to print routines. */
4971
4972 static const char *
4973 string_or_null (const char *str)
4974 {
4975 return str != NULL ? str : "<null>";
4976 }
4977
4978 /* Check if a lookup_name_info built from
4979 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4980 index. EXPECTED_LIST is the list of expected matches, in expected
4981 matching order. If no match expected, then an empty list is
4982 specified. Returns true on success. On failure prints a warning
4983 indicating the file:line that failed, and returns false. */
4984
4985 static bool
4986 check_match (const char *file, int line,
4987 mock_mapped_index &mock_index,
4988 const char *name, symbol_name_match_type match_type,
4989 bool completion_mode,
4990 std::initializer_list<const char *> expected_list)
4991 {
4992 lookup_name_info lookup_name (name, match_type, completion_mode);
4993
4994 bool matched = true;
4995
4996 auto mismatch = [&] (const char *expected_str,
4997 const char *got)
4998 {
4999 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
5000 "expected=\"%s\", got=\"%s\"\n"),
5001 file, line,
5002 (match_type == symbol_name_match_type::FULL
5003 ? "FULL" : "WILD"),
5004 name, string_or_null (expected_str), string_or_null (got));
5005 matched = false;
5006 };
5007
5008 auto expected_it = expected_list.begin ();
5009 auto expected_end = expected_list.end ();
5010
5011 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
5012 NULL, ALL_DOMAIN,
5013 [&] (offset_type idx)
5014 {
5015 const char *matched_name = mock_index.symbol_name_at (idx);
5016 const char *expected_str
5017 = expected_it == expected_end ? NULL : *expected_it++;
5018
5019 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
5020 mismatch (expected_str, matched_name);
5021 });
5022
5023 const char *expected_str
5024 = expected_it == expected_end ? NULL : *expected_it++;
5025 if (expected_str != NULL)
5026 mismatch (expected_str, NULL);
5027
5028 return matched;
5029 }
5030
5031 /* The symbols added to the mock mapped_index for testing (in
5032 canonical form). */
5033 static const char *test_symbols[] = {
5034 "function",
5035 "std::bar",
5036 "std::zfunction",
5037 "std::zfunction2",
5038 "w1::w2",
5039 "ns::foo<char*>",
5040 "ns::foo<int>",
5041 "ns::foo<long>",
5042 "ns2::tmpl<int>::foo2",
5043 "(anonymous namespace)::A::B::C",
5044
5045 /* These are used to check that the increment-last-char in the
5046 matching algorithm for completion doesn't match "t1_fund" when
5047 completing "t1_func". */
5048 "t1_func",
5049 "t1_func1",
5050 "t1_fund",
5051 "t1_fund1",
5052
5053 /* A UTF-8 name with multi-byte sequences to make sure that
5054 cp-name-parser understands this as a single identifier ("função"
5055 is "function" in PT). */
5056 u8"u8função",
5057
5058 /* \377 (0xff) is Latin1 'ÿ'. */
5059 "yfunc\377",
5060
5061 /* \377 (0xff) is Latin1 'ÿ'. */
5062 "\377",
5063 "\377\377123",
5064
5065 /* A name with all sorts of complications. Starts with "z" to make
5066 it easier for the completion tests below. */
5067 #define Z_SYM_NAME \
5068 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
5069 "::tuple<(anonymous namespace)::ui*, " \
5070 "std::default_delete<(anonymous namespace)::ui>, void>"
5071
5072 Z_SYM_NAME
5073 };
5074
5075 /* Returns true if the mapped_index_base::find_name_component_bounds
5076 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
5077 in completion mode. */
5078
5079 static bool
5080 check_find_bounds_finds (mapped_index_base &index,
5081 const char *search_name,
5082 gdb::array_view<const char *> expected_syms)
5083 {
5084 lookup_name_info lookup_name (search_name,
5085 symbol_name_match_type::FULL, true);
5086
5087 auto bounds = index.find_name_components_bounds (lookup_name);
5088
5089 size_t distance = std::distance (bounds.first, bounds.second);
5090 if (distance != expected_syms.size ())
5091 return false;
5092
5093 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
5094 {
5095 auto nc_elem = bounds.first + exp_elem;
5096 const char *qualified = index.symbol_name_at (nc_elem->idx);
5097 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
5098 return false;
5099 }
5100
5101 return true;
5102 }
5103
5104 /* Test the lower-level mapped_index::find_name_component_bounds
5105 method. */
5106
5107 static void
5108 test_mapped_index_find_name_component_bounds ()
5109 {
5110 mock_mapped_index mock_index (test_symbols);
5111
5112 mock_index.build_name_components ();
5113
5114 /* Test the lower-level mapped_index::find_name_component_bounds
5115 method in completion mode. */
5116 {
5117 static const char *expected_syms[] = {
5118 "t1_func",
5119 "t1_func1",
5120 };
5121
5122 SELF_CHECK (check_find_bounds_finds (mock_index,
5123 "t1_func", expected_syms));
5124 }
5125
5126 /* Check that the increment-last-char in the name matching algorithm
5127 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5128 {
5129 static const char *expected_syms1[] = {
5130 "\377",
5131 "\377\377123",
5132 };
5133 SELF_CHECK (check_find_bounds_finds (mock_index,
5134 "\377", expected_syms1));
5135
5136 static const char *expected_syms2[] = {
5137 "\377\377123",
5138 };
5139 SELF_CHECK (check_find_bounds_finds (mock_index,
5140 "\377\377", expected_syms2));
5141 }
5142 }
5143
5144 /* Test dw2_expand_symtabs_matching_symbol. */
5145
5146 static void
5147 test_dw2_expand_symtabs_matching_symbol ()
5148 {
5149 mock_mapped_index mock_index (test_symbols);
5150
5151 /* We let all tests run until the end even if some fails, for debug
5152 convenience. */
5153 bool any_mismatch = false;
5154
5155 /* Create the expected symbols list (an initializer_list). Needed
5156 because lists have commas, and we need to pass them to CHECK,
5157 which is a macro. */
5158 #define EXPECT(...) { __VA_ARGS__ }
5159
5160 /* Wrapper for check_match that passes down the current
5161 __FILE__/__LINE__. */
5162 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5163 any_mismatch |= !check_match (__FILE__, __LINE__, \
5164 mock_index, \
5165 NAME, MATCH_TYPE, COMPLETION_MODE, \
5166 EXPECTED_LIST)
5167
5168 /* Identity checks. */
5169 for (const char *sym : test_symbols)
5170 {
5171 /* Should be able to match all existing symbols. */
5172 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5173 EXPECT (sym));
5174
5175 /* Should be able to match all existing symbols with
5176 parameters. */
5177 std::string with_params = std::string (sym) + "(int)";
5178 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5179 EXPECT (sym));
5180
5181 /* Should be able to match all existing symbols with
5182 parameters and qualifiers. */
5183 with_params = std::string (sym) + " ( int ) const";
5184 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5185 EXPECT (sym));
5186
5187 /* This should really find sym, but cp-name-parser.y doesn't
5188 know about lvalue/rvalue qualifiers yet. */
5189 with_params = std::string (sym) + " ( int ) &&";
5190 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5191 {});
5192 }
5193
5194 /* Check that the name matching algorithm for completion doesn't get
5195 confused with Latin1 'ÿ' / 0xff. */
5196 {
5197 static const char str[] = "\377";
5198 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5199 EXPECT ("\377", "\377\377123"));
5200 }
5201
5202 /* Check that the increment-last-char in the matching algorithm for
5203 completion doesn't match "t1_fund" when completing "t1_func". */
5204 {
5205 static const char str[] = "t1_func";
5206 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5207 EXPECT ("t1_func", "t1_func1"));
5208 }
5209
5210 /* Check that completion mode works at each prefix of the expected
5211 symbol name. */
5212 {
5213 static const char str[] = "function(int)";
5214 size_t len = strlen (str);
5215 std::string lookup;
5216
5217 for (size_t i = 1; i < len; i++)
5218 {
5219 lookup.assign (str, i);
5220 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5221 EXPECT ("function"));
5222 }
5223 }
5224
5225 /* While "w" is a prefix of both components, the match function
5226 should still only be called once. */
5227 {
5228 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5229 EXPECT ("w1::w2"));
5230 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5231 EXPECT ("w1::w2"));
5232 }
5233
5234 /* Same, with a "complicated" symbol. */
5235 {
5236 static const char str[] = Z_SYM_NAME;
5237 size_t len = strlen (str);
5238 std::string lookup;
5239
5240 for (size_t i = 1; i < len; i++)
5241 {
5242 lookup.assign (str, i);
5243 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5244 EXPECT (Z_SYM_NAME));
5245 }
5246 }
5247
5248 /* In FULL mode, an incomplete symbol doesn't match. */
5249 {
5250 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5251 {});
5252 }
5253
5254 /* A complete symbol with parameters matches any overload, since the
5255 index has no overload info. */
5256 {
5257 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5258 EXPECT ("std::zfunction", "std::zfunction2"));
5259 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5260 EXPECT ("std::zfunction", "std::zfunction2"));
5261 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5262 EXPECT ("std::zfunction", "std::zfunction2"));
5263 }
5264
5265 /* Check that whitespace is ignored appropriately. A symbol with a
5266 template argument list. */
5267 {
5268 static const char expected[] = "ns::foo<int>";
5269 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5270 EXPECT (expected));
5271 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5272 EXPECT (expected));
5273 }
5274
5275 /* Check that whitespace is ignored appropriately. A symbol with a
5276 template argument list that includes a pointer. */
5277 {
5278 static const char expected[] = "ns::foo<char*>";
5279 /* Try both completion and non-completion modes. */
5280 static const bool completion_mode[2] = {false, true};
5281 for (size_t i = 0; i < 2; i++)
5282 {
5283 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5284 completion_mode[i], EXPECT (expected));
5285 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5286 completion_mode[i], EXPECT (expected));
5287
5288 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5289 completion_mode[i], EXPECT (expected));
5290 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5291 completion_mode[i], EXPECT (expected));
5292 }
5293 }
5294
5295 {
5296 /* Check method qualifiers are ignored. */
5297 static const char expected[] = "ns::foo<char*>";
5298 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5299 symbol_name_match_type::FULL, true, EXPECT (expected));
5300 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5301 symbol_name_match_type::FULL, true, EXPECT (expected));
5302 CHECK_MATCH ("foo < char * > ( int ) const",
5303 symbol_name_match_type::WILD, true, EXPECT (expected));
5304 CHECK_MATCH ("foo < char * > ( int ) &&",
5305 symbol_name_match_type::WILD, true, EXPECT (expected));
5306 }
5307
5308 /* Test lookup names that don't match anything. */
5309 {
5310 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5311 {});
5312
5313 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5314 {});
5315 }
5316
5317 /* Some wild matching tests, exercising "(anonymous namespace)",
5318 which should not be confused with a parameter list. */
5319 {
5320 static const char *syms[] = {
5321 "A::B::C",
5322 "B::C",
5323 "C",
5324 "A :: B :: C ( int )",
5325 "B :: C ( int )",
5326 "C ( int )",
5327 };
5328
5329 for (const char *s : syms)
5330 {
5331 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5332 EXPECT ("(anonymous namespace)::A::B::C"));
5333 }
5334 }
5335
5336 {
5337 static const char expected[] = "ns2::tmpl<int>::foo2";
5338 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5339 EXPECT (expected));
5340 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5341 EXPECT (expected));
5342 }
5343
5344 SELF_CHECK (!any_mismatch);
5345
5346 #undef EXPECT
5347 #undef CHECK_MATCH
5348 }
5349
5350 static void
5351 run_test ()
5352 {
5353 test_mapped_index_find_name_component_bounds ();
5354 test_dw2_expand_symtabs_matching_symbol ();
5355 }
5356
5357 }} // namespace selftests::dw2_expand_symtabs_matching
5358
5359 #endif /* GDB_SELF_TEST */
5360
5361 /* If FILE_MATCHER is NULL or if PER_CU has
5362 dwarf2_per_cu_quick_data::MARK set (see
5363 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5364 EXPANSION_NOTIFY on it. */
5365
5366 static void
5367 dw2_expand_symtabs_matching_one
5368 (struct dwarf2_per_cu_data *per_cu,
5369 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5370 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5371 {
5372 if (file_matcher == NULL || per_cu->v.quick->mark)
5373 {
5374 bool symtab_was_null
5375 = (per_cu->v.quick->compunit_symtab == NULL);
5376
5377 dw2_instantiate_symtab (per_cu);
5378
5379 if (expansion_notify != NULL
5380 && symtab_was_null
5381 && per_cu->v.quick->compunit_symtab != NULL)
5382 expansion_notify (per_cu->v.quick->compunit_symtab);
5383 }
5384 }
5385
5386 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5387 matched, to expand corresponding CUs that were marked. IDX is the
5388 index of the symbol name that matched. */
5389
5390 static void
5391 dw2_expand_marked_cus
5392 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5393 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5394 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5395 search_domain kind)
5396 {
5397 offset_type *vec, vec_len, vec_idx;
5398 bool global_seen = false;
5399 mapped_index &index = *dwarf2_per_objfile->index_table;
5400
5401 vec = (offset_type *) (index.constant_pool
5402 + MAYBE_SWAP (index.symbol_table[idx].vec));
5403 vec_len = MAYBE_SWAP (vec[0]);
5404 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5405 {
5406 struct dwarf2_per_cu_data *per_cu;
5407 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5408 /* This value is only valid for index versions >= 7. */
5409 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5410 gdb_index_symbol_kind symbol_kind =
5411 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5412 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5413 /* Only check the symbol attributes if they're present.
5414 Indices prior to version 7 don't record them,
5415 and indices >= 7 may elide them for certain symbols
5416 (gold does this). */
5417 int attrs_valid =
5418 (index.version >= 7
5419 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5420
5421 /* Work around gold/15646. */
5422 if (attrs_valid)
5423 {
5424 if (!is_static && global_seen)
5425 continue;
5426 if (!is_static)
5427 global_seen = true;
5428 }
5429
5430 /* Only check the symbol's kind if it has one. */
5431 if (attrs_valid)
5432 {
5433 switch (kind)
5434 {
5435 case VARIABLES_DOMAIN:
5436 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5437 continue;
5438 break;
5439 case FUNCTIONS_DOMAIN:
5440 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5441 continue;
5442 break;
5443 case TYPES_DOMAIN:
5444 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5445 continue;
5446 break;
5447 default:
5448 break;
5449 }
5450 }
5451
5452 /* Don't crash on bad data. */
5453 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5454 + dwarf2_per_objfile->n_type_units))
5455 {
5456 complaint (&symfile_complaints,
5457 _(".gdb_index entry has bad CU index"
5458 " [in module %s]"),
5459 objfile_name (dwarf2_per_objfile->objfile));
5460 continue;
5461 }
5462
5463 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
5464 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5465 expansion_notify);
5466 }
5467 }
5468
5469 /* If FILE_MATCHER is non-NULL, set all the
5470 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5471 that match FILE_MATCHER. */
5472
5473 static void
5474 dw_expand_symtabs_matching_file_matcher
5475 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5476 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5477 {
5478 if (file_matcher == NULL)
5479 return;
5480
5481 objfile *const objfile = dwarf2_per_objfile->objfile;
5482
5483 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5484 htab_eq_pointer,
5485 NULL, xcalloc, xfree));
5486 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5487 htab_eq_pointer,
5488 NULL, xcalloc, xfree));
5489
5490 /* The rule is CUs specify all the files, including those used by
5491 any TU, so there's no need to scan TUs here. */
5492
5493 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5494 {
5495 int j;
5496 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5497 struct quick_file_names *file_data;
5498 void **slot;
5499
5500 QUIT;
5501
5502 per_cu->v.quick->mark = 0;
5503
5504 /* We only need to look at symtabs not already expanded. */
5505 if (per_cu->v.quick->compunit_symtab)
5506 continue;
5507
5508 file_data = dw2_get_file_names (per_cu);
5509 if (file_data == NULL)
5510 continue;
5511
5512 if (htab_find (visited_not_found.get (), file_data) != NULL)
5513 continue;
5514 else if (htab_find (visited_found.get (), file_data) != NULL)
5515 {
5516 per_cu->v.quick->mark = 1;
5517 continue;
5518 }
5519
5520 for (j = 0; j < file_data->num_file_names; ++j)
5521 {
5522 const char *this_real_name;
5523
5524 if (file_matcher (file_data->file_names[j], false))
5525 {
5526 per_cu->v.quick->mark = 1;
5527 break;
5528 }
5529
5530 /* Before we invoke realpath, which can get expensive when many
5531 files are involved, do a quick comparison of the basenames. */
5532 if (!basenames_may_differ
5533 && !file_matcher (lbasename (file_data->file_names[j]),
5534 true))
5535 continue;
5536
5537 this_real_name = dw2_get_real_path (objfile, file_data, j);
5538 if (file_matcher (this_real_name, false))
5539 {
5540 per_cu->v.quick->mark = 1;
5541 break;
5542 }
5543 }
5544
5545 slot = htab_find_slot (per_cu->v.quick->mark
5546 ? visited_found.get ()
5547 : visited_not_found.get (),
5548 file_data, INSERT);
5549 *slot = file_data;
5550 }
5551 }
5552
5553 static void
5554 dw2_expand_symtabs_matching
5555 (struct objfile *objfile,
5556 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5557 const lookup_name_info &lookup_name,
5558 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5559 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5560 enum search_domain kind)
5561 {
5562 struct dwarf2_per_objfile *dwarf2_per_objfile
5563 = get_dwarf2_per_objfile (objfile);
5564
5565 /* index_table is NULL if OBJF_READNOW. */
5566 if (!dwarf2_per_objfile->index_table)
5567 return;
5568
5569 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5570
5571 mapped_index &index = *dwarf2_per_objfile->index_table;
5572
5573 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5574 symbol_matcher,
5575 kind, [&] (offset_type idx)
5576 {
5577 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5578 expansion_notify, kind);
5579 });
5580 }
5581
5582 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5583 symtab. */
5584
5585 static struct compunit_symtab *
5586 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5587 CORE_ADDR pc)
5588 {
5589 int i;
5590
5591 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5592 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5593 return cust;
5594
5595 if (cust->includes == NULL)
5596 return NULL;
5597
5598 for (i = 0; cust->includes[i]; ++i)
5599 {
5600 struct compunit_symtab *s = cust->includes[i];
5601
5602 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5603 if (s != NULL)
5604 return s;
5605 }
5606
5607 return NULL;
5608 }
5609
5610 static struct compunit_symtab *
5611 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5612 struct bound_minimal_symbol msymbol,
5613 CORE_ADDR pc,
5614 struct obj_section *section,
5615 int warn_if_readin)
5616 {
5617 struct dwarf2_per_cu_data *data;
5618 struct compunit_symtab *result;
5619
5620 if (!objfile->psymtabs_addrmap)
5621 return NULL;
5622
5623 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5624 pc);
5625 if (!data)
5626 return NULL;
5627
5628 if (warn_if_readin && data->v.quick->compunit_symtab)
5629 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5630 paddress (get_objfile_arch (objfile), pc));
5631
5632 result
5633 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5634 pc);
5635 gdb_assert (result != NULL);
5636 return result;
5637 }
5638
5639 static void
5640 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5641 void *data, int need_fullname)
5642 {
5643 struct dwarf2_per_objfile *dwarf2_per_objfile
5644 = get_dwarf2_per_objfile (objfile);
5645
5646 if (!dwarf2_per_objfile->filenames_cache)
5647 {
5648 dwarf2_per_objfile->filenames_cache.emplace ();
5649
5650 htab_up visited (htab_create_alloc (10,
5651 htab_hash_pointer, htab_eq_pointer,
5652 NULL, xcalloc, xfree));
5653
5654 /* The rule is CUs specify all the files, including those used
5655 by any TU, so there's no need to scan TUs here. We can
5656 ignore file names coming from already-expanded CUs. */
5657
5658 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5659 {
5660 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
5661
5662 if (per_cu->v.quick->compunit_symtab)
5663 {
5664 void **slot = htab_find_slot (visited.get (),
5665 per_cu->v.quick->file_names,
5666 INSERT);
5667
5668 *slot = per_cu->v.quick->file_names;
5669 }
5670 }
5671
5672 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5673 {
5674 dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5675 struct quick_file_names *file_data;
5676 void **slot;
5677
5678 /* We only need to look at symtabs not already expanded. */
5679 if (per_cu->v.quick->compunit_symtab)
5680 continue;
5681
5682 file_data = dw2_get_file_names (per_cu);
5683 if (file_data == NULL)
5684 continue;
5685
5686 slot = htab_find_slot (visited.get (), file_data, INSERT);
5687 if (*slot)
5688 {
5689 /* Already visited. */
5690 continue;
5691 }
5692 *slot = file_data;
5693
5694 for (int j = 0; j < file_data->num_file_names; ++j)
5695 {
5696 const char *filename = file_data->file_names[j];
5697 dwarf2_per_objfile->filenames_cache->seen (filename);
5698 }
5699 }
5700 }
5701
5702 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5703 {
5704 gdb::unique_xmalloc_ptr<char> this_real_name;
5705
5706 if (need_fullname)
5707 this_real_name = gdb_realpath (filename);
5708 (*fun) (filename, this_real_name.get (), data);
5709 });
5710 }
5711
5712 static int
5713 dw2_has_symbols (struct objfile *objfile)
5714 {
5715 return 1;
5716 }
5717
5718 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5719 {
5720 dw2_has_symbols,
5721 dw2_find_last_source_symtab,
5722 dw2_forget_cached_source_info,
5723 dw2_map_symtabs_matching_filename,
5724 dw2_lookup_symbol,
5725 dw2_print_stats,
5726 dw2_dump,
5727 dw2_relocate,
5728 dw2_expand_symtabs_for_function,
5729 dw2_expand_all_symtabs,
5730 dw2_expand_symtabs_with_fullname,
5731 dw2_map_matching_symbols,
5732 dw2_expand_symtabs_matching,
5733 dw2_find_pc_sect_compunit_symtab,
5734 NULL,
5735 dw2_map_symbol_filenames
5736 };
5737
5738 /* DWARF-5 debug_names reader. */
5739
5740 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5741 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5742
5743 /* A helper function that reads the .debug_names section in SECTION
5744 and fills in MAP. FILENAME is the name of the file containing the
5745 section; it is used for error reporting.
5746
5747 Returns true if all went well, false otherwise. */
5748
5749 static bool
5750 read_debug_names_from_section (struct objfile *objfile,
5751 const char *filename,
5752 struct dwarf2_section_info *section,
5753 mapped_debug_names &map)
5754 {
5755 if (dwarf2_section_empty_p (section))
5756 return false;
5757
5758 /* Older elfutils strip versions could keep the section in the main
5759 executable while splitting it for the separate debug info file. */
5760 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5761 return false;
5762
5763 dwarf2_read_section (objfile, section);
5764
5765 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5766
5767 const gdb_byte *addr = section->buffer;
5768
5769 bfd *const abfd = get_section_bfd_owner (section);
5770
5771 unsigned int bytes_read;
5772 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5773 addr += bytes_read;
5774
5775 map.dwarf5_is_dwarf64 = bytes_read != 4;
5776 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5777 if (bytes_read + length != section->size)
5778 {
5779 /* There may be multiple per-CU indices. */
5780 warning (_("Section .debug_names in %s length %s does not match "
5781 "section length %s, ignoring .debug_names."),
5782 filename, plongest (bytes_read + length),
5783 pulongest (section->size));
5784 return false;
5785 }
5786
5787 /* The version number. */
5788 uint16_t version = read_2_bytes (abfd, addr);
5789 addr += 2;
5790 if (version != 5)
5791 {
5792 warning (_("Section .debug_names in %s has unsupported version %d, "
5793 "ignoring .debug_names."),
5794 filename, version);
5795 return false;
5796 }
5797
5798 /* Padding. */
5799 uint16_t padding = read_2_bytes (abfd, addr);
5800 addr += 2;
5801 if (padding != 0)
5802 {
5803 warning (_("Section .debug_names in %s has unsupported padding %d, "
5804 "ignoring .debug_names."),
5805 filename, padding);
5806 return false;
5807 }
5808
5809 /* comp_unit_count - The number of CUs in the CU list. */
5810 map.cu_count = read_4_bytes (abfd, addr);
5811 addr += 4;
5812
5813 /* local_type_unit_count - The number of TUs in the local TU
5814 list. */
5815 map.tu_count = read_4_bytes (abfd, addr);
5816 addr += 4;
5817
5818 /* foreign_type_unit_count - The number of TUs in the foreign TU
5819 list. */
5820 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5821 addr += 4;
5822 if (foreign_tu_count != 0)
5823 {
5824 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5825 "ignoring .debug_names."),
5826 filename, static_cast<unsigned long> (foreign_tu_count));
5827 return false;
5828 }
5829
5830 /* bucket_count - The number of hash buckets in the hash lookup
5831 table. */
5832 map.bucket_count = read_4_bytes (abfd, addr);
5833 addr += 4;
5834
5835 /* name_count - The number of unique names in the index. */
5836 map.name_count = read_4_bytes (abfd, addr);
5837 addr += 4;
5838
5839 /* abbrev_table_size - The size in bytes of the abbreviations
5840 table. */
5841 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5842 addr += 4;
5843
5844 /* augmentation_string_size - The size in bytes of the augmentation
5845 string. This value is rounded up to a multiple of 4. */
5846 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5847 addr += 4;
5848 map.augmentation_is_gdb = ((augmentation_string_size
5849 == sizeof (dwarf5_augmentation))
5850 && memcmp (addr, dwarf5_augmentation,
5851 sizeof (dwarf5_augmentation)) == 0);
5852 augmentation_string_size += (-augmentation_string_size) & 3;
5853 addr += augmentation_string_size;
5854
5855 /* List of CUs */
5856 map.cu_table_reordered = addr;
5857 addr += map.cu_count * map.offset_size;
5858
5859 /* List of Local TUs */
5860 map.tu_table_reordered = addr;
5861 addr += map.tu_count * map.offset_size;
5862
5863 /* Hash Lookup Table */
5864 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5865 addr += map.bucket_count * 4;
5866 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5867 addr += map.name_count * 4;
5868
5869 /* Name Table */
5870 map.name_table_string_offs_reordered = addr;
5871 addr += map.name_count * map.offset_size;
5872 map.name_table_entry_offs_reordered = addr;
5873 addr += map.name_count * map.offset_size;
5874
5875 const gdb_byte *abbrev_table_start = addr;
5876 for (;;)
5877 {
5878 unsigned int bytes_read;
5879 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5880 addr += bytes_read;
5881 if (index_num == 0)
5882 break;
5883
5884 const auto insertpair
5885 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5886 if (!insertpair.second)
5887 {
5888 warning (_("Section .debug_names in %s has duplicate index %s, "
5889 "ignoring .debug_names."),
5890 filename, pulongest (index_num));
5891 return false;
5892 }
5893 mapped_debug_names::index_val &indexval = insertpair.first->second;
5894 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5895 addr += bytes_read;
5896
5897 for (;;)
5898 {
5899 mapped_debug_names::index_val::attr attr;
5900 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5901 addr += bytes_read;
5902 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5903 addr += bytes_read;
5904 if (attr.form == DW_FORM_implicit_const)
5905 {
5906 attr.implicit_const = read_signed_leb128 (abfd, addr,
5907 &bytes_read);
5908 addr += bytes_read;
5909 }
5910 if (attr.dw_idx == 0 && attr.form == 0)
5911 break;
5912 indexval.attr_vec.push_back (std::move (attr));
5913 }
5914 }
5915 if (addr != abbrev_table_start + abbrev_table_size)
5916 {
5917 warning (_("Section .debug_names in %s has abbreviation_table "
5918 "of size %zu vs. written as %u, ignoring .debug_names."),
5919 filename, addr - abbrev_table_start, abbrev_table_size);
5920 return false;
5921 }
5922 map.entry_pool = addr;
5923
5924 return true;
5925 }
5926
5927 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5928 list. */
5929
5930 static void
5931 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5932 const mapped_debug_names &map,
5933 dwarf2_section_info &section,
5934 bool is_dwz, int base_offset)
5935 {
5936 sect_offset sect_off_prev;
5937 for (uint32_t i = 0; i <= map.cu_count; ++i)
5938 {
5939 sect_offset sect_off_next;
5940 if (i < map.cu_count)
5941 {
5942 sect_off_next
5943 = (sect_offset) (extract_unsigned_integer
5944 (map.cu_table_reordered + i * map.offset_size,
5945 map.offset_size,
5946 map.dwarf5_byte_order));
5947 }
5948 else
5949 sect_off_next = (sect_offset) section.size;
5950 if (i >= 1)
5951 {
5952 const ULONGEST length = sect_off_next - sect_off_prev;
5953 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
5954 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5955 sect_off_prev, length);
5956 }
5957 sect_off_prev = sect_off_next;
5958 }
5959 }
5960
5961 /* Read the CU list from the mapped index, and use it to create all
5962 the CU objects for this dwarf2_per_objfile. */
5963
5964 static void
5965 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5966 const mapped_debug_names &map,
5967 const mapped_debug_names &dwz_map)
5968 {
5969 struct objfile *objfile = dwarf2_per_objfile->objfile;
5970
5971 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
5972 dwarf2_per_objfile->all_comp_units
5973 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
5974 dwarf2_per_objfile->n_comp_units);
5975
5976 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5977 dwarf2_per_objfile->info,
5978 false /* is_dwz */,
5979 0 /* base_offset */);
5980
5981 if (dwz_map.cu_count == 0)
5982 return;
5983
5984 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5985 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5986 true /* is_dwz */,
5987 map.cu_count /* base_offset */);
5988 }
5989
5990 /* Read .debug_names. If everything went ok, initialize the "quick"
5991 elements of all the CUs and return true. Otherwise, return false. */
5992
5993 static bool
5994 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5995 {
5996 mapped_debug_names local_map (dwarf2_per_objfile);
5997 mapped_debug_names dwz_map (dwarf2_per_objfile);
5998 struct objfile *objfile = dwarf2_per_objfile->objfile;
5999
6000 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
6001 &dwarf2_per_objfile->debug_names,
6002 local_map))
6003 return false;
6004
6005 /* Don't use the index if it's empty. */
6006 if (local_map.name_count == 0)
6007 return false;
6008
6009 /* If there is a .dwz file, read it so we can get its CU list as
6010 well. */
6011 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
6012 if (dwz != NULL)
6013 {
6014 if (!read_debug_names_from_section (objfile,
6015 bfd_get_filename (dwz->dwz_bfd),
6016 &dwz->debug_names, dwz_map))
6017 {
6018 warning (_("could not read '.debug_names' section from %s; skipping"),
6019 bfd_get_filename (dwz->dwz_bfd));
6020 return false;
6021 }
6022 }
6023
6024 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
6025
6026 if (local_map.tu_count != 0)
6027 {
6028 /* We can only handle a single .debug_types when we have an
6029 index. */
6030 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
6031 return false;
6032
6033 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
6034 dwarf2_per_objfile->types, 0);
6035
6036 create_signatured_type_table_from_debug_names
6037 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
6038 }
6039
6040 create_addrmap_from_aranges (dwarf2_per_objfile,
6041 &dwarf2_per_objfile->debug_aranges);
6042
6043 dwarf2_per_objfile->debug_names_table.reset
6044 (new mapped_debug_names (dwarf2_per_objfile));
6045 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
6046 dwarf2_per_objfile->using_index = 1;
6047 dwarf2_per_objfile->quick_file_names_table =
6048 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6049
6050 return true;
6051 }
6052
6053 /* Symbol name hashing function as specified by DWARF-5. */
6054
6055 static uint32_t
6056 dwarf5_djb_hash (const char *str_)
6057 {
6058 const unsigned char *str = (const unsigned char *) str_;
6059
6060 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
6061 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
6062
6063 uint32_t hash = 5381;
6064 while (int c = *str++)
6065 hash = hash * 33 + tolower (c);
6066 return hash;
6067 }
6068
6069 /* Type used to manage iterating over all CUs looking for a symbol for
6070 .debug_names. */
6071
6072 class dw2_debug_names_iterator
6073 {
6074 public:
6075 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
6076 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
6077 dw2_debug_names_iterator (const mapped_debug_names &map,
6078 bool want_specific_block,
6079 block_enum block_index, domain_enum domain,
6080 const char *name)
6081 : m_map (map), m_want_specific_block (want_specific_block),
6082 m_block_index (block_index), m_domain (domain),
6083 m_addr (find_vec_in_debug_names (map, name))
6084 {}
6085
6086 dw2_debug_names_iterator (const mapped_debug_names &map,
6087 search_domain search, uint32_t namei)
6088 : m_map (map),
6089 m_search (search),
6090 m_addr (find_vec_in_debug_names (map, namei))
6091 {}
6092
6093 /* Return the next matching CU or NULL if there are no more. */
6094 dwarf2_per_cu_data *next ();
6095
6096 private:
6097 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6098 const char *name);
6099 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6100 uint32_t namei);
6101
6102 /* The internalized form of .debug_names. */
6103 const mapped_debug_names &m_map;
6104
6105 /* If true, only look for symbols that match BLOCK_INDEX. */
6106 const bool m_want_specific_block = false;
6107
6108 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
6109 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
6110 value. */
6111 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
6112
6113 /* The kind of symbol we're looking for. */
6114 const domain_enum m_domain = UNDEF_DOMAIN;
6115 const search_domain m_search = ALL_DOMAIN;
6116
6117 /* The list of CUs from the index entry of the symbol, or NULL if
6118 not found. */
6119 const gdb_byte *m_addr;
6120 };
6121
6122 const char *
6123 mapped_debug_names::namei_to_name (uint32_t namei) const
6124 {
6125 const ULONGEST namei_string_offs
6126 = extract_unsigned_integer ((name_table_string_offs_reordered
6127 + namei * offset_size),
6128 offset_size,
6129 dwarf5_byte_order);
6130 return read_indirect_string_at_offset
6131 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
6132 }
6133
6134 /* Find a slot in .debug_names for the object named NAME. If NAME is
6135 found, return pointer to its pool data. If NAME cannot be found,
6136 return NULL. */
6137
6138 const gdb_byte *
6139 dw2_debug_names_iterator::find_vec_in_debug_names
6140 (const mapped_debug_names &map, const char *name)
6141 {
6142 int (*cmp) (const char *, const char *);
6143
6144 if (current_language->la_language == language_cplus
6145 || current_language->la_language == language_fortran
6146 || current_language->la_language == language_d)
6147 {
6148 /* NAME is already canonical. Drop any qualifiers as
6149 .debug_names does not contain any. */
6150
6151 if (strchr (name, '(') != NULL)
6152 {
6153 gdb::unique_xmalloc_ptr<char> without_params
6154 = cp_remove_params (name);
6155
6156 if (without_params != NULL)
6157 {
6158 name = without_params.get();
6159 }
6160 }
6161 }
6162
6163 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6164
6165 const uint32_t full_hash = dwarf5_djb_hash (name);
6166 uint32_t namei
6167 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6168 (map.bucket_table_reordered
6169 + (full_hash % map.bucket_count)), 4,
6170 map.dwarf5_byte_order);
6171 if (namei == 0)
6172 return NULL;
6173 --namei;
6174 if (namei >= map.name_count)
6175 {
6176 complaint (&symfile_complaints,
6177 _("Wrong .debug_names with name index %u but name_count=%u "
6178 "[in module %s]"),
6179 namei, map.name_count,
6180 objfile_name (map.dwarf2_per_objfile->objfile));
6181 return NULL;
6182 }
6183
6184 for (;;)
6185 {
6186 const uint32_t namei_full_hash
6187 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6188 (map.hash_table_reordered + namei), 4,
6189 map.dwarf5_byte_order);
6190 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6191 return NULL;
6192
6193 if (full_hash == namei_full_hash)
6194 {
6195 const char *const namei_string = map.namei_to_name (namei);
6196
6197 #if 0 /* An expensive sanity check. */
6198 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6199 {
6200 complaint (&symfile_complaints,
6201 _("Wrong .debug_names hash for string at index %u "
6202 "[in module %s]"),
6203 namei, objfile_name (dwarf2_per_objfile->objfile));
6204 return NULL;
6205 }
6206 #endif
6207
6208 if (cmp (namei_string, name) == 0)
6209 {
6210 const ULONGEST namei_entry_offs
6211 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6212 + namei * map.offset_size),
6213 map.offset_size, map.dwarf5_byte_order);
6214 return map.entry_pool + namei_entry_offs;
6215 }
6216 }
6217
6218 ++namei;
6219 if (namei >= map.name_count)
6220 return NULL;
6221 }
6222 }
6223
6224 const gdb_byte *
6225 dw2_debug_names_iterator::find_vec_in_debug_names
6226 (const mapped_debug_names &map, uint32_t namei)
6227 {
6228 if (namei >= map.name_count)
6229 {
6230 complaint (&symfile_complaints,
6231 _("Wrong .debug_names with name index %u but name_count=%u "
6232 "[in module %s]"),
6233 namei, map.name_count,
6234 objfile_name (map.dwarf2_per_objfile->objfile));
6235 return NULL;
6236 }
6237
6238 const ULONGEST namei_entry_offs
6239 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6240 + namei * map.offset_size),
6241 map.offset_size, map.dwarf5_byte_order);
6242 return map.entry_pool + namei_entry_offs;
6243 }
6244
6245 /* See dw2_debug_names_iterator. */
6246
6247 dwarf2_per_cu_data *
6248 dw2_debug_names_iterator::next ()
6249 {
6250 if (m_addr == NULL)
6251 return NULL;
6252
6253 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
6254 struct objfile *objfile = dwarf2_per_objfile->objfile;
6255 bfd *const abfd = objfile->obfd;
6256
6257 again:
6258
6259 unsigned int bytes_read;
6260 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6261 m_addr += bytes_read;
6262 if (abbrev == 0)
6263 return NULL;
6264
6265 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6266 if (indexval_it == m_map.abbrev_map.cend ())
6267 {
6268 complaint (&symfile_complaints,
6269 _("Wrong .debug_names undefined abbrev code %s "
6270 "[in module %s]"),
6271 pulongest (abbrev), objfile_name (objfile));
6272 return NULL;
6273 }
6274 const mapped_debug_names::index_val &indexval = indexval_it->second;
6275 bool have_is_static = false;
6276 bool is_static;
6277 dwarf2_per_cu_data *per_cu = NULL;
6278 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6279 {
6280 ULONGEST ull;
6281 switch (attr.form)
6282 {
6283 case DW_FORM_implicit_const:
6284 ull = attr.implicit_const;
6285 break;
6286 case DW_FORM_flag_present:
6287 ull = 1;
6288 break;
6289 case DW_FORM_udata:
6290 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6291 m_addr += bytes_read;
6292 break;
6293 default:
6294 complaint (&symfile_complaints,
6295 _("Unsupported .debug_names form %s [in module %s]"),
6296 dwarf_form_name (attr.form),
6297 objfile_name (objfile));
6298 return NULL;
6299 }
6300 switch (attr.dw_idx)
6301 {
6302 case DW_IDX_compile_unit:
6303 /* Don't crash on bad data. */
6304 if (ull >= dwarf2_per_objfile->n_comp_units)
6305 {
6306 complaint (&symfile_complaints,
6307 _(".debug_names entry has bad CU index %s"
6308 " [in module %s]"),
6309 pulongest (ull),
6310 objfile_name (dwarf2_per_objfile->objfile));
6311 continue;
6312 }
6313 per_cu = dw2_get_cutu (dwarf2_per_objfile, ull);
6314 break;
6315 case DW_IDX_type_unit:
6316 /* Don't crash on bad data. */
6317 if (ull >= dwarf2_per_objfile->n_type_units)
6318 {
6319 complaint (&symfile_complaints,
6320 _(".debug_names entry has bad TU index %s"
6321 " [in module %s]"),
6322 pulongest (ull),
6323 objfile_name (dwarf2_per_objfile->objfile));
6324 continue;
6325 }
6326 per_cu = dw2_get_cutu (dwarf2_per_objfile,
6327 dwarf2_per_objfile->n_comp_units + ull);
6328 break;
6329 case DW_IDX_GNU_internal:
6330 if (!m_map.augmentation_is_gdb)
6331 break;
6332 have_is_static = true;
6333 is_static = true;
6334 break;
6335 case DW_IDX_GNU_external:
6336 if (!m_map.augmentation_is_gdb)
6337 break;
6338 have_is_static = true;
6339 is_static = false;
6340 break;
6341 }
6342 }
6343
6344 /* Skip if already read in. */
6345 if (per_cu->v.quick->compunit_symtab)
6346 goto again;
6347
6348 /* Check static vs global. */
6349 if (have_is_static)
6350 {
6351 const bool want_static = m_block_index != GLOBAL_BLOCK;
6352 if (m_want_specific_block && want_static != is_static)
6353 goto again;
6354 }
6355
6356 /* Match dw2_symtab_iter_next, symbol_kind
6357 and debug_names::psymbol_tag. */
6358 switch (m_domain)
6359 {
6360 case VAR_DOMAIN:
6361 switch (indexval.dwarf_tag)
6362 {
6363 case DW_TAG_variable:
6364 case DW_TAG_subprogram:
6365 /* Some types are also in VAR_DOMAIN. */
6366 case DW_TAG_typedef:
6367 case DW_TAG_structure_type:
6368 break;
6369 default:
6370 goto again;
6371 }
6372 break;
6373 case STRUCT_DOMAIN:
6374 switch (indexval.dwarf_tag)
6375 {
6376 case DW_TAG_typedef:
6377 case DW_TAG_structure_type:
6378 break;
6379 default:
6380 goto again;
6381 }
6382 break;
6383 case LABEL_DOMAIN:
6384 switch (indexval.dwarf_tag)
6385 {
6386 case 0:
6387 case DW_TAG_variable:
6388 break;
6389 default:
6390 goto again;
6391 }
6392 break;
6393 default:
6394 break;
6395 }
6396
6397 /* Match dw2_expand_symtabs_matching, symbol_kind and
6398 debug_names::psymbol_tag. */
6399 switch (m_search)
6400 {
6401 case VARIABLES_DOMAIN:
6402 switch (indexval.dwarf_tag)
6403 {
6404 case DW_TAG_variable:
6405 break;
6406 default:
6407 goto again;
6408 }
6409 break;
6410 case FUNCTIONS_DOMAIN:
6411 switch (indexval.dwarf_tag)
6412 {
6413 case DW_TAG_subprogram:
6414 break;
6415 default:
6416 goto again;
6417 }
6418 break;
6419 case TYPES_DOMAIN:
6420 switch (indexval.dwarf_tag)
6421 {
6422 case DW_TAG_typedef:
6423 case DW_TAG_structure_type:
6424 break;
6425 default:
6426 goto again;
6427 }
6428 break;
6429 default:
6430 break;
6431 }
6432
6433 return per_cu;
6434 }
6435
6436 static struct compunit_symtab *
6437 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6438 const char *name, domain_enum domain)
6439 {
6440 const block_enum block_index = static_cast<block_enum> (block_index_int);
6441 struct dwarf2_per_objfile *dwarf2_per_objfile
6442 = get_dwarf2_per_objfile (objfile);
6443
6444 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6445 if (!mapp)
6446 {
6447 /* index is NULL if OBJF_READNOW. */
6448 return NULL;
6449 }
6450 const auto &map = *mapp;
6451
6452 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6453 block_index, domain, name);
6454
6455 struct compunit_symtab *stab_best = NULL;
6456 struct dwarf2_per_cu_data *per_cu;
6457 while ((per_cu = iter.next ()) != NULL)
6458 {
6459 struct symbol *sym, *with_opaque = NULL;
6460 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6461 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6462 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6463
6464 sym = block_find_symbol (block, name, domain,
6465 block_find_non_opaque_type_preferred,
6466 &with_opaque);
6467
6468 /* Some caution must be observed with overloaded functions and
6469 methods, since the index will not contain any overload
6470 information (but NAME might contain it). */
6471
6472 if (sym != NULL
6473 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6474 return stab;
6475 if (with_opaque != NULL
6476 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6477 stab_best = stab;
6478
6479 /* Keep looking through other CUs. */
6480 }
6481
6482 return stab_best;
6483 }
6484
6485 /* This dumps minimal information about .debug_names. It is called
6486 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6487 uses this to verify that .debug_names has been loaded. */
6488
6489 static void
6490 dw2_debug_names_dump (struct objfile *objfile)
6491 {
6492 struct dwarf2_per_objfile *dwarf2_per_objfile
6493 = get_dwarf2_per_objfile (objfile);
6494
6495 gdb_assert (dwarf2_per_objfile->using_index);
6496 printf_filtered (".debug_names:");
6497 if (dwarf2_per_objfile->debug_names_table)
6498 printf_filtered (" exists\n");
6499 else
6500 printf_filtered (" faked for \"readnow\"\n");
6501 printf_filtered ("\n");
6502 }
6503
6504 static void
6505 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6506 const char *func_name)
6507 {
6508 struct dwarf2_per_objfile *dwarf2_per_objfile
6509 = get_dwarf2_per_objfile (objfile);
6510
6511 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6512 if (dwarf2_per_objfile->debug_names_table)
6513 {
6514 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6515
6516 /* Note: It doesn't matter what we pass for block_index here. */
6517 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6518 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6519
6520 struct dwarf2_per_cu_data *per_cu;
6521 while ((per_cu = iter.next ()) != NULL)
6522 dw2_instantiate_symtab (per_cu);
6523 }
6524 }
6525
6526 static void
6527 dw2_debug_names_expand_symtabs_matching
6528 (struct objfile *objfile,
6529 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6530 const lookup_name_info &lookup_name,
6531 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6532 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6533 enum search_domain kind)
6534 {
6535 struct dwarf2_per_objfile *dwarf2_per_objfile
6536 = get_dwarf2_per_objfile (objfile);
6537
6538 /* debug_names_table is NULL if OBJF_READNOW. */
6539 if (!dwarf2_per_objfile->debug_names_table)
6540 return;
6541
6542 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6543
6544 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6545
6546 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6547 symbol_matcher,
6548 kind, [&] (offset_type namei)
6549 {
6550 /* The name was matched, now expand corresponding CUs that were
6551 marked. */
6552 dw2_debug_names_iterator iter (map, kind, namei);
6553
6554 struct dwarf2_per_cu_data *per_cu;
6555 while ((per_cu = iter.next ()) != NULL)
6556 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6557 expansion_notify);
6558 });
6559 }
6560
6561 const struct quick_symbol_functions dwarf2_debug_names_functions =
6562 {
6563 dw2_has_symbols,
6564 dw2_find_last_source_symtab,
6565 dw2_forget_cached_source_info,
6566 dw2_map_symtabs_matching_filename,
6567 dw2_debug_names_lookup_symbol,
6568 dw2_print_stats,
6569 dw2_debug_names_dump,
6570 dw2_relocate,
6571 dw2_debug_names_expand_symtabs_for_function,
6572 dw2_expand_all_symtabs,
6573 dw2_expand_symtabs_with_fullname,
6574 dw2_map_matching_symbols,
6575 dw2_debug_names_expand_symtabs_matching,
6576 dw2_find_pc_sect_compunit_symtab,
6577 NULL,
6578 dw2_map_symbol_filenames
6579 };
6580
6581 /* See symfile.h. */
6582
6583 bool
6584 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6585 {
6586 struct dwarf2_per_objfile *dwarf2_per_objfile
6587 = get_dwarf2_per_objfile (objfile);
6588
6589 /* If we're about to read full symbols, don't bother with the
6590 indices. In this case we also don't care if some other debug
6591 format is making psymtabs, because they are all about to be
6592 expanded anyway. */
6593 if ((objfile->flags & OBJF_READNOW))
6594 {
6595 int i;
6596
6597 dwarf2_per_objfile->using_index = 1;
6598 create_all_comp_units (dwarf2_per_objfile);
6599 create_all_type_units (dwarf2_per_objfile);
6600 dwarf2_per_objfile->quick_file_names_table =
6601 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6602
6603 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
6604 + dwarf2_per_objfile->n_type_units); ++i)
6605 {
6606 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
6607
6608 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6609 struct dwarf2_per_cu_quick_data);
6610 }
6611
6612 /* Return 1 so that gdb sees the "quick" functions. However,
6613 these functions will be no-ops because we will have expanded
6614 all symtabs. */
6615 *index_kind = dw_index_kind::GDB_INDEX;
6616 return true;
6617 }
6618
6619 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6620 {
6621 *index_kind = dw_index_kind::DEBUG_NAMES;
6622 return true;
6623 }
6624
6625 if (dwarf2_read_index (objfile))
6626 {
6627 *index_kind = dw_index_kind::GDB_INDEX;
6628 return true;
6629 }
6630
6631 return false;
6632 }
6633
6634 \f
6635
6636 /* Build a partial symbol table. */
6637
6638 void
6639 dwarf2_build_psymtabs (struct objfile *objfile)
6640 {
6641 struct dwarf2_per_objfile *dwarf2_per_objfile
6642 = get_dwarf2_per_objfile (objfile);
6643
6644 if (objfile->global_psymbols.capacity () == 0
6645 && objfile->static_psymbols.capacity () == 0)
6646 init_psymbol_list (objfile, 1024);
6647
6648 TRY
6649 {
6650 /* This isn't really ideal: all the data we allocate on the
6651 objfile's obstack is still uselessly kept around. However,
6652 freeing it seems unsafe. */
6653 psymtab_discarder psymtabs (objfile);
6654 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6655 psymtabs.keep ();
6656 }
6657 CATCH (except, RETURN_MASK_ERROR)
6658 {
6659 exception_print (gdb_stderr, except);
6660 }
6661 END_CATCH
6662 }
6663
6664 /* Return the total length of the CU described by HEADER. */
6665
6666 static unsigned int
6667 get_cu_length (const struct comp_unit_head *header)
6668 {
6669 return header->initial_length_size + header->length;
6670 }
6671
6672 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6673
6674 static inline bool
6675 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6676 {
6677 sect_offset bottom = cu_header->sect_off;
6678 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6679
6680 return sect_off >= bottom && sect_off < top;
6681 }
6682
6683 /* Find the base address of the compilation unit for range lists and
6684 location lists. It will normally be specified by DW_AT_low_pc.
6685 In DWARF-3 draft 4, the base address could be overridden by
6686 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6687 compilation units with discontinuous ranges. */
6688
6689 static void
6690 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6691 {
6692 struct attribute *attr;
6693
6694 cu->base_known = 0;
6695 cu->base_address = 0;
6696
6697 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6698 if (attr)
6699 {
6700 cu->base_address = attr_value_as_address (attr);
6701 cu->base_known = 1;
6702 }
6703 else
6704 {
6705 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6706 if (attr)
6707 {
6708 cu->base_address = attr_value_as_address (attr);
6709 cu->base_known = 1;
6710 }
6711 }
6712 }
6713
6714 /* Read in the comp unit header information from the debug_info at info_ptr.
6715 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6716 NOTE: This leaves members offset, first_die_offset to be filled in
6717 by the caller. */
6718
6719 static const gdb_byte *
6720 read_comp_unit_head (struct comp_unit_head *cu_header,
6721 const gdb_byte *info_ptr,
6722 struct dwarf2_section_info *section,
6723 rcuh_kind section_kind)
6724 {
6725 int signed_addr;
6726 unsigned int bytes_read;
6727 const char *filename = get_section_file_name (section);
6728 bfd *abfd = get_section_bfd_owner (section);
6729
6730 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6731 cu_header->initial_length_size = bytes_read;
6732 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6733 info_ptr += bytes_read;
6734 cu_header->version = read_2_bytes (abfd, info_ptr);
6735 info_ptr += 2;
6736 if (cu_header->version < 5)
6737 switch (section_kind)
6738 {
6739 case rcuh_kind::COMPILE:
6740 cu_header->unit_type = DW_UT_compile;
6741 break;
6742 case rcuh_kind::TYPE:
6743 cu_header->unit_type = DW_UT_type;
6744 break;
6745 default:
6746 internal_error (__FILE__, __LINE__,
6747 _("read_comp_unit_head: invalid section_kind"));
6748 }
6749 else
6750 {
6751 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6752 (read_1_byte (abfd, info_ptr));
6753 info_ptr += 1;
6754 switch (cu_header->unit_type)
6755 {
6756 case DW_UT_compile:
6757 if (section_kind != rcuh_kind::COMPILE)
6758 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6759 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6760 filename);
6761 break;
6762 case DW_UT_type:
6763 section_kind = rcuh_kind::TYPE;
6764 break;
6765 default:
6766 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6767 "(is %d, should be %d or %d) [in module %s]"),
6768 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6769 }
6770
6771 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6772 info_ptr += 1;
6773 }
6774 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6775 cu_header,
6776 &bytes_read);
6777 info_ptr += bytes_read;
6778 if (cu_header->version < 5)
6779 {
6780 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6781 info_ptr += 1;
6782 }
6783 signed_addr = bfd_get_sign_extend_vma (abfd);
6784 if (signed_addr < 0)
6785 internal_error (__FILE__, __LINE__,
6786 _("read_comp_unit_head: dwarf from non elf file"));
6787 cu_header->signed_addr_p = signed_addr;
6788
6789 if (section_kind == rcuh_kind::TYPE)
6790 {
6791 LONGEST type_offset;
6792
6793 cu_header->signature = read_8_bytes (abfd, info_ptr);
6794 info_ptr += 8;
6795
6796 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6797 info_ptr += bytes_read;
6798 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6799 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6800 error (_("Dwarf Error: Too big type_offset in compilation unit "
6801 "header (is %s) [in module %s]"), plongest (type_offset),
6802 filename);
6803 }
6804
6805 return info_ptr;
6806 }
6807
6808 /* Helper function that returns the proper abbrev section for
6809 THIS_CU. */
6810
6811 static struct dwarf2_section_info *
6812 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6813 {
6814 struct dwarf2_section_info *abbrev;
6815 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6816
6817 if (this_cu->is_dwz)
6818 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6819 else
6820 abbrev = &dwarf2_per_objfile->abbrev;
6821
6822 return abbrev;
6823 }
6824
6825 /* Subroutine of read_and_check_comp_unit_head and
6826 read_and_check_type_unit_head to simplify them.
6827 Perform various error checking on the header. */
6828
6829 static void
6830 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6831 struct comp_unit_head *header,
6832 struct dwarf2_section_info *section,
6833 struct dwarf2_section_info *abbrev_section)
6834 {
6835 const char *filename = get_section_file_name (section);
6836
6837 if (header->version < 2 || header->version > 5)
6838 error (_("Dwarf Error: wrong version in compilation unit header "
6839 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6840 filename);
6841
6842 if (to_underlying (header->abbrev_sect_off)
6843 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6844 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
6845 "(offset 0x%x + 6) [in module %s]"),
6846 to_underlying (header->abbrev_sect_off),
6847 to_underlying (header->sect_off),
6848 filename);
6849
6850 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6851 avoid potential 32-bit overflow. */
6852 if (((ULONGEST) header->sect_off + get_cu_length (header))
6853 > section->size)
6854 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6855 "(offset 0x%x + 0) [in module %s]"),
6856 header->length, to_underlying (header->sect_off),
6857 filename);
6858 }
6859
6860 /* Read in a CU/TU header and perform some basic error checking.
6861 The contents of the header are stored in HEADER.
6862 The result is a pointer to the start of the first DIE. */
6863
6864 static const gdb_byte *
6865 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6866 struct comp_unit_head *header,
6867 struct dwarf2_section_info *section,
6868 struct dwarf2_section_info *abbrev_section,
6869 const gdb_byte *info_ptr,
6870 rcuh_kind section_kind)
6871 {
6872 const gdb_byte *beg_of_comp_unit = info_ptr;
6873
6874 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6875
6876 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6877
6878 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6879
6880 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6881 abbrev_section);
6882
6883 return info_ptr;
6884 }
6885
6886 /* Fetch the abbreviation table offset from a comp or type unit header. */
6887
6888 static sect_offset
6889 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6890 struct dwarf2_section_info *section,
6891 sect_offset sect_off)
6892 {
6893 bfd *abfd = get_section_bfd_owner (section);
6894 const gdb_byte *info_ptr;
6895 unsigned int initial_length_size, offset_size;
6896 uint16_t version;
6897
6898 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6899 info_ptr = section->buffer + to_underlying (sect_off);
6900 read_initial_length (abfd, info_ptr, &initial_length_size);
6901 offset_size = initial_length_size == 4 ? 4 : 8;
6902 info_ptr += initial_length_size;
6903
6904 version = read_2_bytes (abfd, info_ptr);
6905 info_ptr += 2;
6906 if (version >= 5)
6907 {
6908 /* Skip unit type and address size. */
6909 info_ptr += 2;
6910 }
6911
6912 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6913 }
6914
6915 /* Allocate a new partial symtab for file named NAME and mark this new
6916 partial symtab as being an include of PST. */
6917
6918 static void
6919 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6920 struct objfile *objfile)
6921 {
6922 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6923
6924 if (!IS_ABSOLUTE_PATH (subpst->filename))
6925 {
6926 /* It shares objfile->objfile_obstack. */
6927 subpst->dirname = pst->dirname;
6928 }
6929
6930 subpst->textlow = 0;
6931 subpst->texthigh = 0;
6932
6933 subpst->dependencies
6934 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6935 subpst->dependencies[0] = pst;
6936 subpst->number_of_dependencies = 1;
6937
6938 subpst->globals_offset = 0;
6939 subpst->n_global_syms = 0;
6940 subpst->statics_offset = 0;
6941 subpst->n_static_syms = 0;
6942 subpst->compunit_symtab = NULL;
6943 subpst->read_symtab = pst->read_symtab;
6944 subpst->readin = 0;
6945
6946 /* No private part is necessary for include psymtabs. This property
6947 can be used to differentiate between such include psymtabs and
6948 the regular ones. */
6949 subpst->read_symtab_private = NULL;
6950 }
6951
6952 /* Read the Line Number Program data and extract the list of files
6953 included by the source file represented by PST. Build an include
6954 partial symtab for each of these included files. */
6955
6956 static void
6957 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6958 struct die_info *die,
6959 struct partial_symtab *pst)
6960 {
6961 line_header_up lh;
6962 struct attribute *attr;
6963
6964 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6965 if (attr)
6966 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6967 if (lh == NULL)
6968 return; /* No linetable, so no includes. */
6969
6970 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
6971 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
6972 }
6973
6974 static hashval_t
6975 hash_signatured_type (const void *item)
6976 {
6977 const struct signatured_type *sig_type
6978 = (const struct signatured_type *) item;
6979
6980 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6981 return sig_type->signature;
6982 }
6983
6984 static int
6985 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6986 {
6987 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6988 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6989
6990 return lhs->signature == rhs->signature;
6991 }
6992
6993 /* Allocate a hash table for signatured types. */
6994
6995 static htab_t
6996 allocate_signatured_type_table (struct objfile *objfile)
6997 {
6998 return htab_create_alloc_ex (41,
6999 hash_signatured_type,
7000 eq_signatured_type,
7001 NULL,
7002 &objfile->objfile_obstack,
7003 hashtab_obstack_allocate,
7004 dummy_obstack_deallocate);
7005 }
7006
7007 /* A helper function to add a signatured type CU to a table. */
7008
7009 static int
7010 add_signatured_type_cu_to_table (void **slot, void *datum)
7011 {
7012 struct signatured_type *sigt = (struct signatured_type *) *slot;
7013 struct signatured_type ***datap = (struct signatured_type ***) datum;
7014
7015 **datap = sigt;
7016 ++*datap;
7017
7018 return 1;
7019 }
7020
7021 /* A helper for create_debug_types_hash_table. Read types from SECTION
7022 and fill them into TYPES_HTAB. It will process only type units,
7023 therefore DW_UT_type. */
7024
7025 static void
7026 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7027 struct dwo_file *dwo_file,
7028 dwarf2_section_info *section, htab_t &types_htab,
7029 rcuh_kind section_kind)
7030 {
7031 struct objfile *objfile = dwarf2_per_objfile->objfile;
7032 struct dwarf2_section_info *abbrev_section;
7033 bfd *abfd;
7034 const gdb_byte *info_ptr, *end_ptr;
7035
7036 abbrev_section = (dwo_file != NULL
7037 ? &dwo_file->sections.abbrev
7038 : &dwarf2_per_objfile->abbrev);
7039
7040 if (dwarf_read_debug)
7041 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
7042 get_section_name (section),
7043 get_section_file_name (abbrev_section));
7044
7045 dwarf2_read_section (objfile, section);
7046 info_ptr = section->buffer;
7047
7048 if (info_ptr == NULL)
7049 return;
7050
7051 /* We can't set abfd until now because the section may be empty or
7052 not present, in which case the bfd is unknown. */
7053 abfd = get_section_bfd_owner (section);
7054
7055 /* We don't use init_cutu_and_read_dies_simple, or some such, here
7056 because we don't need to read any dies: the signature is in the
7057 header. */
7058
7059 end_ptr = info_ptr + section->size;
7060 while (info_ptr < end_ptr)
7061 {
7062 struct signatured_type *sig_type;
7063 struct dwo_unit *dwo_tu;
7064 void **slot;
7065 const gdb_byte *ptr = info_ptr;
7066 struct comp_unit_head header;
7067 unsigned int length;
7068
7069 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
7070
7071 /* Initialize it due to a false compiler warning. */
7072 header.signature = -1;
7073 header.type_cu_offset_in_tu = (cu_offset) -1;
7074
7075 /* We need to read the type's signature in order to build the hash
7076 table, but we don't need anything else just yet. */
7077
7078 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
7079 abbrev_section, ptr, section_kind);
7080
7081 length = get_cu_length (&header);
7082
7083 /* Skip dummy type units. */
7084 if (ptr >= info_ptr + length
7085 || peek_abbrev_code (abfd, ptr) == 0
7086 || header.unit_type != DW_UT_type)
7087 {
7088 info_ptr += length;
7089 continue;
7090 }
7091
7092 if (types_htab == NULL)
7093 {
7094 if (dwo_file)
7095 types_htab = allocate_dwo_unit_table (objfile);
7096 else
7097 types_htab = allocate_signatured_type_table (objfile);
7098 }
7099
7100 if (dwo_file)
7101 {
7102 sig_type = NULL;
7103 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7104 struct dwo_unit);
7105 dwo_tu->dwo_file = dwo_file;
7106 dwo_tu->signature = header.signature;
7107 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
7108 dwo_tu->section = section;
7109 dwo_tu->sect_off = sect_off;
7110 dwo_tu->length = length;
7111 }
7112 else
7113 {
7114 /* N.B.: type_offset is not usable if this type uses a DWO file.
7115 The real type_offset is in the DWO file. */
7116 dwo_tu = NULL;
7117 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7118 struct signatured_type);
7119 sig_type->signature = header.signature;
7120 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
7121 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7122 sig_type->per_cu.is_debug_types = 1;
7123 sig_type->per_cu.section = section;
7124 sig_type->per_cu.sect_off = sect_off;
7125 sig_type->per_cu.length = length;
7126 }
7127
7128 slot = htab_find_slot (types_htab,
7129 dwo_file ? (void*) dwo_tu : (void *) sig_type,
7130 INSERT);
7131 gdb_assert (slot != NULL);
7132 if (*slot != NULL)
7133 {
7134 sect_offset dup_sect_off;
7135
7136 if (dwo_file)
7137 {
7138 const struct dwo_unit *dup_tu
7139 = (const struct dwo_unit *) *slot;
7140
7141 dup_sect_off = dup_tu->sect_off;
7142 }
7143 else
7144 {
7145 const struct signatured_type *dup_tu
7146 = (const struct signatured_type *) *slot;
7147
7148 dup_sect_off = dup_tu->per_cu.sect_off;
7149 }
7150
7151 complaint (&symfile_complaints,
7152 _("debug type entry at offset 0x%x is duplicate to"
7153 " the entry at offset 0x%x, signature %s"),
7154 to_underlying (sect_off), to_underlying (dup_sect_off),
7155 hex_string (header.signature));
7156 }
7157 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
7158
7159 if (dwarf_read_debug > 1)
7160 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
7161 to_underlying (sect_off),
7162 hex_string (header.signature));
7163
7164 info_ptr += length;
7165 }
7166 }
7167
7168 /* Create the hash table of all entries in the .debug_types
7169 (or .debug_types.dwo) section(s).
7170 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7171 otherwise it is NULL.
7172
7173 The result is a pointer to the hash table or NULL if there are no types.
7174
7175 Note: This function processes DWO files only, not DWP files. */
7176
7177 static void
7178 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7179 struct dwo_file *dwo_file,
7180 VEC (dwarf2_section_info_def) *types,
7181 htab_t &types_htab)
7182 {
7183 int ix;
7184 struct dwarf2_section_info *section;
7185
7186 if (VEC_empty (dwarf2_section_info_def, types))
7187 return;
7188
7189 for (ix = 0;
7190 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7191 ++ix)
7192 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
7193 types_htab, rcuh_kind::TYPE);
7194 }
7195
7196 /* Create the hash table of all entries in the .debug_types section,
7197 and initialize all_type_units.
7198 The result is zero if there is an error (e.g. missing .debug_types section),
7199 otherwise non-zero. */
7200
7201 static int
7202 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7203 {
7204 htab_t types_htab = NULL;
7205 struct signatured_type **iter;
7206
7207 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
7208 &dwarf2_per_objfile->info, types_htab,
7209 rcuh_kind::COMPILE);
7210 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
7211 dwarf2_per_objfile->types, types_htab);
7212 if (types_htab == NULL)
7213 {
7214 dwarf2_per_objfile->signatured_types = NULL;
7215 return 0;
7216 }
7217
7218 dwarf2_per_objfile->signatured_types = types_htab;
7219
7220 dwarf2_per_objfile->n_type_units
7221 = dwarf2_per_objfile->n_allocated_type_units
7222 = htab_elements (types_htab);
7223 dwarf2_per_objfile->all_type_units =
7224 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
7225 iter = &dwarf2_per_objfile->all_type_units[0];
7226 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7227 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7228 == dwarf2_per_objfile->n_type_units);
7229
7230 return 1;
7231 }
7232
7233 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7234 If SLOT is non-NULL, it is the entry to use in the hash table.
7235 Otherwise we find one. */
7236
7237 static struct signatured_type *
7238 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
7239 void **slot)
7240 {
7241 struct objfile *objfile = dwarf2_per_objfile->objfile;
7242 int n_type_units = dwarf2_per_objfile->n_type_units;
7243 struct signatured_type *sig_type;
7244
7245 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7246 ++n_type_units;
7247 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7248 {
7249 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7250 dwarf2_per_objfile->n_allocated_type_units = 1;
7251 dwarf2_per_objfile->n_allocated_type_units *= 2;
7252 dwarf2_per_objfile->all_type_units
7253 = XRESIZEVEC (struct signatured_type *,
7254 dwarf2_per_objfile->all_type_units,
7255 dwarf2_per_objfile->n_allocated_type_units);
7256 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7257 }
7258 dwarf2_per_objfile->n_type_units = n_type_units;
7259
7260 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7261 struct signatured_type);
7262 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7263 sig_type->signature = sig;
7264 sig_type->per_cu.is_debug_types = 1;
7265 if (dwarf2_per_objfile->using_index)
7266 {
7267 sig_type->per_cu.v.quick =
7268 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7269 struct dwarf2_per_cu_quick_data);
7270 }
7271
7272 if (slot == NULL)
7273 {
7274 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7275 sig_type, INSERT);
7276 }
7277 gdb_assert (*slot == NULL);
7278 *slot = sig_type;
7279 /* The rest of sig_type must be filled in by the caller. */
7280 return sig_type;
7281 }
7282
7283 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7284 Fill in SIG_ENTRY with DWO_ENTRY. */
7285
7286 static void
7287 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
7288 struct signatured_type *sig_entry,
7289 struct dwo_unit *dwo_entry)
7290 {
7291 /* Make sure we're not clobbering something we don't expect to. */
7292 gdb_assert (! sig_entry->per_cu.queued);
7293 gdb_assert (sig_entry->per_cu.cu == NULL);
7294 if (dwarf2_per_objfile->using_index)
7295 {
7296 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7297 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7298 }
7299 else
7300 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7301 gdb_assert (sig_entry->signature == dwo_entry->signature);
7302 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7303 gdb_assert (sig_entry->type_unit_group == NULL);
7304 gdb_assert (sig_entry->dwo_unit == NULL);
7305
7306 sig_entry->per_cu.section = dwo_entry->section;
7307 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7308 sig_entry->per_cu.length = dwo_entry->length;
7309 sig_entry->per_cu.reading_dwo_directly = 1;
7310 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7311 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7312 sig_entry->dwo_unit = dwo_entry;
7313 }
7314
7315 /* Subroutine of lookup_signatured_type.
7316 If we haven't read the TU yet, create the signatured_type data structure
7317 for a TU to be read in directly from a DWO file, bypassing the stub.
7318 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7319 using .gdb_index, then when reading a CU we want to stay in the DWO file
7320 containing that CU. Otherwise we could end up reading several other DWO
7321 files (due to comdat folding) to process the transitive closure of all the
7322 mentioned TUs, and that can be slow. The current DWO file will have every
7323 type signature that it needs.
7324 We only do this for .gdb_index because in the psymtab case we already have
7325 to read all the DWOs to build the type unit groups. */
7326
7327 static struct signatured_type *
7328 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7329 {
7330 struct dwarf2_per_objfile *dwarf2_per_objfile
7331 = cu->per_cu->dwarf2_per_objfile;
7332 struct objfile *objfile = dwarf2_per_objfile->objfile;
7333 struct dwo_file *dwo_file;
7334 struct dwo_unit find_dwo_entry, *dwo_entry;
7335 struct signatured_type find_sig_entry, *sig_entry;
7336 void **slot;
7337
7338 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7339
7340 /* If TU skeletons have been removed then we may not have read in any
7341 TUs yet. */
7342 if (dwarf2_per_objfile->signatured_types == NULL)
7343 {
7344 dwarf2_per_objfile->signatured_types
7345 = allocate_signatured_type_table (objfile);
7346 }
7347
7348 /* We only ever need to read in one copy of a signatured type.
7349 Use the global signatured_types array to do our own comdat-folding
7350 of types. If this is the first time we're reading this TU, and
7351 the TU has an entry in .gdb_index, replace the recorded data from
7352 .gdb_index with this TU. */
7353
7354 find_sig_entry.signature = sig;
7355 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7356 &find_sig_entry, INSERT);
7357 sig_entry = (struct signatured_type *) *slot;
7358
7359 /* We can get here with the TU already read, *or* in the process of being
7360 read. Don't reassign the global entry to point to this DWO if that's
7361 the case. Also note that if the TU is already being read, it may not
7362 have come from a DWO, the program may be a mix of Fission-compiled
7363 code and non-Fission-compiled code. */
7364
7365 /* Have we already tried to read this TU?
7366 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7367 needn't exist in the global table yet). */
7368 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7369 return sig_entry;
7370
7371 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7372 dwo_unit of the TU itself. */
7373 dwo_file = cu->dwo_unit->dwo_file;
7374
7375 /* Ok, this is the first time we're reading this TU. */
7376 if (dwo_file->tus == NULL)
7377 return NULL;
7378 find_dwo_entry.signature = sig;
7379 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7380 if (dwo_entry == NULL)
7381 return NULL;
7382
7383 /* If the global table doesn't have an entry for this TU, add one. */
7384 if (sig_entry == NULL)
7385 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7386
7387 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7388 sig_entry->per_cu.tu_read = 1;
7389 return sig_entry;
7390 }
7391
7392 /* Subroutine of lookup_signatured_type.
7393 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7394 then try the DWP file. If the TU stub (skeleton) has been removed then
7395 it won't be in .gdb_index. */
7396
7397 static struct signatured_type *
7398 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7399 {
7400 struct dwarf2_per_objfile *dwarf2_per_objfile
7401 = cu->per_cu->dwarf2_per_objfile;
7402 struct objfile *objfile = dwarf2_per_objfile->objfile;
7403 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7404 struct dwo_unit *dwo_entry;
7405 struct signatured_type find_sig_entry, *sig_entry;
7406 void **slot;
7407
7408 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7409 gdb_assert (dwp_file != NULL);
7410
7411 /* If TU skeletons have been removed then we may not have read in any
7412 TUs yet. */
7413 if (dwarf2_per_objfile->signatured_types == NULL)
7414 {
7415 dwarf2_per_objfile->signatured_types
7416 = allocate_signatured_type_table (objfile);
7417 }
7418
7419 find_sig_entry.signature = sig;
7420 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7421 &find_sig_entry, INSERT);
7422 sig_entry = (struct signatured_type *) *slot;
7423
7424 /* Have we already tried to read this TU?
7425 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7426 needn't exist in the global table yet). */
7427 if (sig_entry != NULL)
7428 return sig_entry;
7429
7430 if (dwp_file->tus == NULL)
7431 return NULL;
7432 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7433 sig, 1 /* is_debug_types */);
7434 if (dwo_entry == NULL)
7435 return NULL;
7436
7437 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7438 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7439
7440 return sig_entry;
7441 }
7442
7443 /* Lookup a signature based type for DW_FORM_ref_sig8.
7444 Returns NULL if signature SIG is not present in the table.
7445 It is up to the caller to complain about this. */
7446
7447 static struct signatured_type *
7448 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7449 {
7450 struct dwarf2_per_objfile *dwarf2_per_objfile
7451 = cu->per_cu->dwarf2_per_objfile;
7452
7453 if (cu->dwo_unit
7454 && dwarf2_per_objfile->using_index)
7455 {
7456 /* We're in a DWO/DWP file, and we're using .gdb_index.
7457 These cases require special processing. */
7458 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7459 return lookup_dwo_signatured_type (cu, sig);
7460 else
7461 return lookup_dwp_signatured_type (cu, sig);
7462 }
7463 else
7464 {
7465 struct signatured_type find_entry, *entry;
7466
7467 if (dwarf2_per_objfile->signatured_types == NULL)
7468 return NULL;
7469 find_entry.signature = sig;
7470 entry = ((struct signatured_type *)
7471 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7472 return entry;
7473 }
7474 }
7475 \f
7476 /* Low level DIE reading support. */
7477
7478 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7479
7480 static void
7481 init_cu_die_reader (struct die_reader_specs *reader,
7482 struct dwarf2_cu *cu,
7483 struct dwarf2_section_info *section,
7484 struct dwo_file *dwo_file,
7485 struct abbrev_table *abbrev_table)
7486 {
7487 gdb_assert (section->readin && section->buffer != NULL);
7488 reader->abfd = get_section_bfd_owner (section);
7489 reader->cu = cu;
7490 reader->dwo_file = dwo_file;
7491 reader->die_section = section;
7492 reader->buffer = section->buffer;
7493 reader->buffer_end = section->buffer + section->size;
7494 reader->comp_dir = NULL;
7495 reader->abbrev_table = abbrev_table;
7496 }
7497
7498 /* Subroutine of init_cutu_and_read_dies to simplify it.
7499 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7500 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7501 already.
7502
7503 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7504 from it to the DIE in the DWO. If NULL we are skipping the stub.
7505 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7506 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7507 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7508 STUB_COMP_DIR may be non-NULL.
7509 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7510 are filled in with the info of the DIE from the DWO file.
7511 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7512 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7513 kept around for at least as long as *RESULT_READER.
7514
7515 The result is non-zero if a valid (non-dummy) DIE was found. */
7516
7517 static int
7518 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7519 struct dwo_unit *dwo_unit,
7520 struct die_info *stub_comp_unit_die,
7521 const char *stub_comp_dir,
7522 struct die_reader_specs *result_reader,
7523 const gdb_byte **result_info_ptr,
7524 struct die_info **result_comp_unit_die,
7525 int *result_has_children,
7526 abbrev_table_up *result_dwo_abbrev_table)
7527 {
7528 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7529 struct objfile *objfile = dwarf2_per_objfile->objfile;
7530 struct dwarf2_cu *cu = this_cu->cu;
7531 bfd *abfd;
7532 const gdb_byte *begin_info_ptr, *info_ptr;
7533 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7534 int i,num_extra_attrs;
7535 struct dwarf2_section_info *dwo_abbrev_section;
7536 struct attribute *attr;
7537 struct die_info *comp_unit_die;
7538
7539 /* At most one of these may be provided. */
7540 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7541
7542 /* These attributes aren't processed until later:
7543 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7544 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7545 referenced later. However, these attributes are found in the stub
7546 which we won't have later. In order to not impose this complication
7547 on the rest of the code, we read them here and copy them to the
7548 DWO CU/TU die. */
7549
7550 stmt_list = NULL;
7551 low_pc = NULL;
7552 high_pc = NULL;
7553 ranges = NULL;
7554 comp_dir = NULL;
7555
7556 if (stub_comp_unit_die != NULL)
7557 {
7558 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7559 DWO file. */
7560 if (! this_cu->is_debug_types)
7561 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7562 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7563 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7564 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7565 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7566
7567 /* There should be a DW_AT_addr_base attribute here (if needed).
7568 We need the value before we can process DW_FORM_GNU_addr_index. */
7569 cu->addr_base = 0;
7570 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7571 if (attr)
7572 cu->addr_base = DW_UNSND (attr);
7573
7574 /* There should be a DW_AT_ranges_base attribute here (if needed).
7575 We need the value before we can process DW_AT_ranges. */
7576 cu->ranges_base = 0;
7577 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7578 if (attr)
7579 cu->ranges_base = DW_UNSND (attr);
7580 }
7581 else if (stub_comp_dir != NULL)
7582 {
7583 /* Reconstruct the comp_dir attribute to simplify the code below. */
7584 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7585 comp_dir->name = DW_AT_comp_dir;
7586 comp_dir->form = DW_FORM_string;
7587 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7588 DW_STRING (comp_dir) = stub_comp_dir;
7589 }
7590
7591 /* Set up for reading the DWO CU/TU. */
7592 cu->dwo_unit = dwo_unit;
7593 dwarf2_section_info *section = dwo_unit->section;
7594 dwarf2_read_section (objfile, section);
7595 abfd = get_section_bfd_owner (section);
7596 begin_info_ptr = info_ptr = (section->buffer
7597 + to_underlying (dwo_unit->sect_off));
7598 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7599
7600 if (this_cu->is_debug_types)
7601 {
7602 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7603
7604 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7605 &cu->header, section,
7606 dwo_abbrev_section,
7607 info_ptr, rcuh_kind::TYPE);
7608 /* This is not an assert because it can be caused by bad debug info. */
7609 if (sig_type->signature != cu->header.signature)
7610 {
7611 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7612 " TU at offset 0x%x [in module %s]"),
7613 hex_string (sig_type->signature),
7614 hex_string (cu->header.signature),
7615 to_underlying (dwo_unit->sect_off),
7616 bfd_get_filename (abfd));
7617 }
7618 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7619 /* For DWOs coming from DWP files, we don't know the CU length
7620 nor the type's offset in the TU until now. */
7621 dwo_unit->length = get_cu_length (&cu->header);
7622 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7623
7624 /* Establish the type offset that can be used to lookup the type.
7625 For DWO files, we don't know it until now. */
7626 sig_type->type_offset_in_section
7627 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7628 }
7629 else
7630 {
7631 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7632 &cu->header, section,
7633 dwo_abbrev_section,
7634 info_ptr, rcuh_kind::COMPILE);
7635 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7636 /* For DWOs coming from DWP files, we don't know the CU length
7637 until now. */
7638 dwo_unit->length = get_cu_length (&cu->header);
7639 }
7640
7641 *result_dwo_abbrev_table
7642 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7643 cu->header.abbrev_sect_off);
7644 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7645 result_dwo_abbrev_table->get ());
7646
7647 /* Read in the die, but leave space to copy over the attributes
7648 from the stub. This has the benefit of simplifying the rest of
7649 the code - all the work to maintain the illusion of a single
7650 DW_TAG_{compile,type}_unit DIE is done here. */
7651 num_extra_attrs = ((stmt_list != NULL)
7652 + (low_pc != NULL)
7653 + (high_pc != NULL)
7654 + (ranges != NULL)
7655 + (comp_dir != NULL));
7656 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7657 result_has_children, num_extra_attrs);
7658
7659 /* Copy over the attributes from the stub to the DIE we just read in. */
7660 comp_unit_die = *result_comp_unit_die;
7661 i = comp_unit_die->num_attrs;
7662 if (stmt_list != NULL)
7663 comp_unit_die->attrs[i++] = *stmt_list;
7664 if (low_pc != NULL)
7665 comp_unit_die->attrs[i++] = *low_pc;
7666 if (high_pc != NULL)
7667 comp_unit_die->attrs[i++] = *high_pc;
7668 if (ranges != NULL)
7669 comp_unit_die->attrs[i++] = *ranges;
7670 if (comp_dir != NULL)
7671 comp_unit_die->attrs[i++] = *comp_dir;
7672 comp_unit_die->num_attrs += num_extra_attrs;
7673
7674 if (dwarf_die_debug)
7675 {
7676 fprintf_unfiltered (gdb_stdlog,
7677 "Read die from %s@0x%x of %s:\n",
7678 get_section_name (section),
7679 (unsigned) (begin_info_ptr - section->buffer),
7680 bfd_get_filename (abfd));
7681 dump_die (comp_unit_die, dwarf_die_debug);
7682 }
7683
7684 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7685 TUs by skipping the stub and going directly to the entry in the DWO file.
7686 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7687 to get it via circuitous means. Blech. */
7688 if (comp_dir != NULL)
7689 result_reader->comp_dir = DW_STRING (comp_dir);
7690
7691 /* Skip dummy compilation units. */
7692 if (info_ptr >= begin_info_ptr + dwo_unit->length
7693 || peek_abbrev_code (abfd, info_ptr) == 0)
7694 return 0;
7695
7696 *result_info_ptr = info_ptr;
7697 return 1;
7698 }
7699
7700 /* Subroutine of init_cutu_and_read_dies to simplify it.
7701 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7702 Returns NULL if the specified DWO unit cannot be found. */
7703
7704 static struct dwo_unit *
7705 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7706 struct die_info *comp_unit_die)
7707 {
7708 struct dwarf2_cu *cu = this_cu->cu;
7709 ULONGEST signature;
7710 struct dwo_unit *dwo_unit;
7711 const char *comp_dir, *dwo_name;
7712
7713 gdb_assert (cu != NULL);
7714
7715 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7716 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7717 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7718
7719 if (this_cu->is_debug_types)
7720 {
7721 struct signatured_type *sig_type;
7722
7723 /* Since this_cu is the first member of struct signatured_type,
7724 we can go from a pointer to one to a pointer to the other. */
7725 sig_type = (struct signatured_type *) this_cu;
7726 signature = sig_type->signature;
7727 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7728 }
7729 else
7730 {
7731 struct attribute *attr;
7732
7733 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7734 if (! attr)
7735 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7736 " [in module %s]"),
7737 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7738 signature = DW_UNSND (attr);
7739 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7740 signature);
7741 }
7742
7743 return dwo_unit;
7744 }
7745
7746 /* Subroutine of init_cutu_and_read_dies to simplify it.
7747 See it for a description of the parameters.
7748 Read a TU directly from a DWO file, bypassing the stub. */
7749
7750 static void
7751 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7752 int use_existing_cu, int keep,
7753 die_reader_func_ftype *die_reader_func,
7754 void *data)
7755 {
7756 std::unique_ptr<dwarf2_cu> new_cu;
7757 struct signatured_type *sig_type;
7758 struct die_reader_specs reader;
7759 const gdb_byte *info_ptr;
7760 struct die_info *comp_unit_die;
7761 int has_children;
7762 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7763
7764 /* Verify we can do the following downcast, and that we have the
7765 data we need. */
7766 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7767 sig_type = (struct signatured_type *) this_cu;
7768 gdb_assert (sig_type->dwo_unit != NULL);
7769
7770 if (use_existing_cu && this_cu->cu != NULL)
7771 {
7772 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7773 /* There's no need to do the rereading_dwo_cu handling that
7774 init_cutu_and_read_dies does since we don't read the stub. */
7775 }
7776 else
7777 {
7778 /* If !use_existing_cu, this_cu->cu must be NULL. */
7779 gdb_assert (this_cu->cu == NULL);
7780 new_cu.reset (new dwarf2_cu (this_cu));
7781 }
7782
7783 /* A future optimization, if needed, would be to use an existing
7784 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7785 could share abbrev tables. */
7786
7787 /* The abbreviation table used by READER, this must live at least as long as
7788 READER. */
7789 abbrev_table_up dwo_abbrev_table;
7790
7791 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7792 NULL /* stub_comp_unit_die */,
7793 sig_type->dwo_unit->dwo_file->comp_dir,
7794 &reader, &info_ptr,
7795 &comp_unit_die, &has_children,
7796 &dwo_abbrev_table) == 0)
7797 {
7798 /* Dummy die. */
7799 return;
7800 }
7801
7802 /* All the "real" work is done here. */
7803 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7804
7805 /* This duplicates the code in init_cutu_and_read_dies,
7806 but the alternative is making the latter more complex.
7807 This function is only for the special case of using DWO files directly:
7808 no point in overly complicating the general case just to handle this. */
7809 if (new_cu != NULL && keep)
7810 {
7811 /* Link this CU into read_in_chain. */
7812 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7813 dwarf2_per_objfile->read_in_chain = this_cu;
7814 /* The chain owns it now. */
7815 new_cu.release ();
7816 }
7817 }
7818
7819 /* Initialize a CU (or TU) and read its DIEs.
7820 If the CU defers to a DWO file, read the DWO file as well.
7821
7822 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7823 Otherwise the table specified in the comp unit header is read in and used.
7824 This is an optimization for when we already have the abbrev table.
7825
7826 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7827 Otherwise, a new CU is allocated with xmalloc.
7828
7829 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7830 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7831
7832 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7833 linker) then DIE_READER_FUNC will not get called. */
7834
7835 static void
7836 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7837 struct abbrev_table *abbrev_table,
7838 int use_existing_cu, int keep,
7839 die_reader_func_ftype *die_reader_func,
7840 void *data)
7841 {
7842 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7843 struct objfile *objfile = dwarf2_per_objfile->objfile;
7844 struct dwarf2_section_info *section = this_cu->section;
7845 bfd *abfd = get_section_bfd_owner (section);
7846 struct dwarf2_cu *cu;
7847 const gdb_byte *begin_info_ptr, *info_ptr;
7848 struct die_reader_specs reader;
7849 struct die_info *comp_unit_die;
7850 int has_children;
7851 struct attribute *attr;
7852 struct signatured_type *sig_type = NULL;
7853 struct dwarf2_section_info *abbrev_section;
7854 /* Non-zero if CU currently points to a DWO file and we need to
7855 reread it. When this happens we need to reread the skeleton die
7856 before we can reread the DWO file (this only applies to CUs, not TUs). */
7857 int rereading_dwo_cu = 0;
7858
7859 if (dwarf_die_debug)
7860 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
7861 this_cu->is_debug_types ? "type" : "comp",
7862 to_underlying (this_cu->sect_off));
7863
7864 if (use_existing_cu)
7865 gdb_assert (keep);
7866
7867 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7868 file (instead of going through the stub), short-circuit all of this. */
7869 if (this_cu->reading_dwo_directly)
7870 {
7871 /* Narrow down the scope of possibilities to have to understand. */
7872 gdb_assert (this_cu->is_debug_types);
7873 gdb_assert (abbrev_table == NULL);
7874 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7875 die_reader_func, data);
7876 return;
7877 }
7878
7879 /* This is cheap if the section is already read in. */
7880 dwarf2_read_section (objfile, section);
7881
7882 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7883
7884 abbrev_section = get_abbrev_section_for_cu (this_cu);
7885
7886 std::unique_ptr<dwarf2_cu> new_cu;
7887 if (use_existing_cu && this_cu->cu != NULL)
7888 {
7889 cu = this_cu->cu;
7890 /* If this CU is from a DWO file we need to start over, we need to
7891 refetch the attributes from the skeleton CU.
7892 This could be optimized by retrieving those attributes from when we
7893 were here the first time: the previous comp_unit_die was stored in
7894 comp_unit_obstack. But there's no data yet that we need this
7895 optimization. */
7896 if (cu->dwo_unit != NULL)
7897 rereading_dwo_cu = 1;
7898 }
7899 else
7900 {
7901 /* If !use_existing_cu, this_cu->cu must be NULL. */
7902 gdb_assert (this_cu->cu == NULL);
7903 new_cu.reset (new dwarf2_cu (this_cu));
7904 cu = new_cu.get ();
7905 }
7906
7907 /* Get the header. */
7908 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7909 {
7910 /* We already have the header, there's no need to read it in again. */
7911 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7912 }
7913 else
7914 {
7915 if (this_cu->is_debug_types)
7916 {
7917 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7918 &cu->header, section,
7919 abbrev_section, info_ptr,
7920 rcuh_kind::TYPE);
7921
7922 /* Since per_cu is the first member of struct signatured_type,
7923 we can go from a pointer to one to a pointer to the other. */
7924 sig_type = (struct signatured_type *) this_cu;
7925 gdb_assert (sig_type->signature == cu->header.signature);
7926 gdb_assert (sig_type->type_offset_in_tu
7927 == cu->header.type_cu_offset_in_tu);
7928 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7929
7930 /* LENGTH has not been set yet for type units if we're
7931 using .gdb_index. */
7932 this_cu->length = get_cu_length (&cu->header);
7933
7934 /* Establish the type offset that can be used to lookup the type. */
7935 sig_type->type_offset_in_section =
7936 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7937
7938 this_cu->dwarf_version = cu->header.version;
7939 }
7940 else
7941 {
7942 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7943 &cu->header, section,
7944 abbrev_section,
7945 info_ptr,
7946 rcuh_kind::COMPILE);
7947
7948 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7949 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7950 this_cu->dwarf_version = cu->header.version;
7951 }
7952 }
7953
7954 /* Skip dummy compilation units. */
7955 if (info_ptr >= begin_info_ptr + this_cu->length
7956 || peek_abbrev_code (abfd, info_ptr) == 0)
7957 return;
7958
7959 /* If we don't have them yet, read the abbrevs for this compilation unit.
7960 And if we need to read them now, make sure they're freed when we're
7961 done (own the table through ABBREV_TABLE_HOLDER). */
7962 abbrev_table_up abbrev_table_holder;
7963 if (abbrev_table != NULL)
7964 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7965 else
7966 {
7967 abbrev_table_holder
7968 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7969 cu->header.abbrev_sect_off);
7970 abbrev_table = abbrev_table_holder.get ();
7971 }
7972
7973 /* Read the top level CU/TU die. */
7974 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7975 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7976
7977 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7978 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7979 table from the DWO file and pass the ownership over to us. It will be
7980 referenced from READER, so we must make sure to free it after we're done
7981 with READER.
7982
7983 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7984 DWO CU, that this test will fail (the attribute will not be present). */
7985 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7986 abbrev_table_up dwo_abbrev_table;
7987 if (attr)
7988 {
7989 struct dwo_unit *dwo_unit;
7990 struct die_info *dwo_comp_unit_die;
7991
7992 if (has_children)
7993 {
7994 complaint (&symfile_complaints,
7995 _("compilation unit with DW_AT_GNU_dwo_name"
7996 " has children (offset 0x%x) [in module %s]"),
7997 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
7998 }
7999 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
8000 if (dwo_unit != NULL)
8001 {
8002 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
8003 comp_unit_die, NULL,
8004 &reader, &info_ptr,
8005 &dwo_comp_unit_die, &has_children,
8006 &dwo_abbrev_table) == 0)
8007 {
8008 /* Dummy die. */
8009 return;
8010 }
8011 comp_unit_die = dwo_comp_unit_die;
8012 }
8013 else
8014 {
8015 /* Yikes, we couldn't find the rest of the DIE, we only have
8016 the stub. A complaint has already been logged. There's
8017 not much more we can do except pass on the stub DIE to
8018 die_reader_func. We don't want to throw an error on bad
8019 debug info. */
8020 }
8021 }
8022
8023 /* All of the above is setup for this call. Yikes. */
8024 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8025
8026 /* Done, clean up. */
8027 if (new_cu != NULL && keep)
8028 {
8029 /* Link this CU into read_in_chain. */
8030 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
8031 dwarf2_per_objfile->read_in_chain = this_cu;
8032 /* The chain owns it now. */
8033 new_cu.release ();
8034 }
8035 }
8036
8037 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
8038 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
8039 to have already done the lookup to find the DWO file).
8040
8041 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
8042 THIS_CU->is_debug_types, but nothing else.
8043
8044 We fill in THIS_CU->length.
8045
8046 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
8047 linker) then DIE_READER_FUNC will not get called.
8048
8049 THIS_CU->cu is always freed when done.
8050 This is done in order to not leave THIS_CU->cu in a state where we have
8051 to care whether it refers to the "main" CU or the DWO CU. */
8052
8053 static void
8054 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
8055 struct dwo_file *dwo_file,
8056 die_reader_func_ftype *die_reader_func,
8057 void *data)
8058 {
8059 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
8060 struct objfile *objfile = dwarf2_per_objfile->objfile;
8061 struct dwarf2_section_info *section = this_cu->section;
8062 bfd *abfd = get_section_bfd_owner (section);
8063 struct dwarf2_section_info *abbrev_section;
8064 const gdb_byte *begin_info_ptr, *info_ptr;
8065 struct die_reader_specs reader;
8066 struct die_info *comp_unit_die;
8067 int has_children;
8068
8069 if (dwarf_die_debug)
8070 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
8071 this_cu->is_debug_types ? "type" : "comp",
8072 to_underlying (this_cu->sect_off));
8073
8074 gdb_assert (this_cu->cu == NULL);
8075
8076 abbrev_section = (dwo_file != NULL
8077 ? &dwo_file->sections.abbrev
8078 : get_abbrev_section_for_cu (this_cu));
8079
8080 /* This is cheap if the section is already read in. */
8081 dwarf2_read_section (objfile, section);
8082
8083 struct dwarf2_cu cu (this_cu);
8084
8085 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
8086 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
8087 &cu.header, section,
8088 abbrev_section, info_ptr,
8089 (this_cu->is_debug_types
8090 ? rcuh_kind::TYPE
8091 : rcuh_kind::COMPILE));
8092
8093 this_cu->length = get_cu_length (&cu.header);
8094
8095 /* Skip dummy compilation units. */
8096 if (info_ptr >= begin_info_ptr + this_cu->length
8097 || peek_abbrev_code (abfd, info_ptr) == 0)
8098 return;
8099
8100 abbrev_table_up abbrev_table
8101 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8102 cu.header.abbrev_sect_off);
8103
8104 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
8105 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8106
8107 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8108 }
8109
8110 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
8111 does not lookup the specified DWO file.
8112 This cannot be used to read DWO files.
8113
8114 THIS_CU->cu is always freed when done.
8115 This is done in order to not leave THIS_CU->cu in a state where we have
8116 to care whether it refers to the "main" CU or the DWO CU.
8117 We can revisit this if the data shows there's a performance issue. */
8118
8119 static void
8120 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8121 die_reader_func_ftype *die_reader_func,
8122 void *data)
8123 {
8124 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
8125 }
8126 \f
8127 /* Type Unit Groups.
8128
8129 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8130 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8131 so that all types coming from the same compilation (.o file) are grouped
8132 together. A future step could be to put the types in the same symtab as
8133 the CU the types ultimately came from. */
8134
8135 static hashval_t
8136 hash_type_unit_group (const void *item)
8137 {
8138 const struct type_unit_group *tu_group
8139 = (const struct type_unit_group *) item;
8140
8141 return hash_stmt_list_entry (&tu_group->hash);
8142 }
8143
8144 static int
8145 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
8146 {
8147 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8148 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
8149
8150 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
8151 }
8152
8153 /* Allocate a hash table for type unit groups. */
8154
8155 static htab_t
8156 allocate_type_unit_groups_table (struct objfile *objfile)
8157 {
8158 return htab_create_alloc_ex (3,
8159 hash_type_unit_group,
8160 eq_type_unit_group,
8161 NULL,
8162 &objfile->objfile_obstack,
8163 hashtab_obstack_allocate,
8164 dummy_obstack_deallocate);
8165 }
8166
8167 /* Type units that don't have DW_AT_stmt_list are grouped into their own
8168 partial symtabs. We combine several TUs per psymtab to not let the size
8169 of any one psymtab grow too big. */
8170 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8171 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
8172
8173 /* Helper routine for get_type_unit_group.
8174 Create the type_unit_group object used to hold one or more TUs. */
8175
8176 static struct type_unit_group *
8177 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
8178 {
8179 struct dwarf2_per_objfile *dwarf2_per_objfile
8180 = cu->per_cu->dwarf2_per_objfile;
8181 struct objfile *objfile = dwarf2_per_objfile->objfile;
8182 struct dwarf2_per_cu_data *per_cu;
8183 struct type_unit_group *tu_group;
8184
8185 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8186 struct type_unit_group);
8187 per_cu = &tu_group->per_cu;
8188 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8189
8190 if (dwarf2_per_objfile->using_index)
8191 {
8192 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8193 struct dwarf2_per_cu_quick_data);
8194 }
8195 else
8196 {
8197 unsigned int line_offset = to_underlying (line_offset_struct);
8198 struct partial_symtab *pst;
8199 char *name;
8200
8201 /* Give the symtab a useful name for debug purposes. */
8202 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8203 name = xstrprintf ("<type_units_%d>",
8204 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8205 else
8206 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8207
8208 pst = create_partial_symtab (per_cu, name);
8209 pst->anonymous = 1;
8210
8211 xfree (name);
8212 }
8213
8214 tu_group->hash.dwo_unit = cu->dwo_unit;
8215 tu_group->hash.line_sect_off = line_offset_struct;
8216
8217 return tu_group;
8218 }
8219
8220 /* Look up the type_unit_group for type unit CU, and create it if necessary.
8221 STMT_LIST is a DW_AT_stmt_list attribute. */
8222
8223 static struct type_unit_group *
8224 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
8225 {
8226 struct dwarf2_per_objfile *dwarf2_per_objfile
8227 = cu->per_cu->dwarf2_per_objfile;
8228 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8229 struct type_unit_group *tu_group;
8230 void **slot;
8231 unsigned int line_offset;
8232 struct type_unit_group type_unit_group_for_lookup;
8233
8234 if (dwarf2_per_objfile->type_unit_groups == NULL)
8235 {
8236 dwarf2_per_objfile->type_unit_groups =
8237 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
8238 }
8239
8240 /* Do we need to create a new group, or can we use an existing one? */
8241
8242 if (stmt_list)
8243 {
8244 line_offset = DW_UNSND (stmt_list);
8245 ++tu_stats->nr_symtab_sharers;
8246 }
8247 else
8248 {
8249 /* Ugh, no stmt_list. Rare, but we have to handle it.
8250 We can do various things here like create one group per TU or
8251 spread them over multiple groups to split up the expansion work.
8252 To avoid worst case scenarios (too many groups or too large groups)
8253 we, umm, group them in bunches. */
8254 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8255 | (tu_stats->nr_stmt_less_type_units
8256 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8257 ++tu_stats->nr_stmt_less_type_units;
8258 }
8259
8260 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
8261 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
8262 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8263 &type_unit_group_for_lookup, INSERT);
8264 if (*slot != NULL)
8265 {
8266 tu_group = (struct type_unit_group *) *slot;
8267 gdb_assert (tu_group != NULL);
8268 }
8269 else
8270 {
8271 sect_offset line_offset_struct = (sect_offset) line_offset;
8272 tu_group = create_type_unit_group (cu, line_offset_struct);
8273 *slot = tu_group;
8274 ++tu_stats->nr_symtabs;
8275 }
8276
8277 return tu_group;
8278 }
8279 \f
8280 /* Partial symbol tables. */
8281
8282 /* Create a psymtab named NAME and assign it to PER_CU.
8283
8284 The caller must fill in the following details:
8285 dirname, textlow, texthigh. */
8286
8287 static struct partial_symtab *
8288 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8289 {
8290 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8291 struct partial_symtab *pst;
8292
8293 pst = start_psymtab_common (objfile, name, 0,
8294 objfile->global_psymbols,
8295 objfile->static_psymbols);
8296
8297 pst->psymtabs_addrmap_supported = 1;
8298
8299 /* This is the glue that links PST into GDB's symbol API. */
8300 pst->read_symtab_private = per_cu;
8301 pst->read_symtab = dwarf2_read_symtab;
8302 per_cu->v.psymtab = pst;
8303
8304 return pst;
8305 }
8306
8307 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8308 type. */
8309
8310 struct process_psymtab_comp_unit_data
8311 {
8312 /* True if we are reading a DW_TAG_partial_unit. */
8313
8314 int want_partial_unit;
8315
8316 /* The "pretend" language that is used if the CU doesn't declare a
8317 language. */
8318
8319 enum language pretend_language;
8320 };
8321
8322 /* die_reader_func for process_psymtab_comp_unit. */
8323
8324 static void
8325 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8326 const gdb_byte *info_ptr,
8327 struct die_info *comp_unit_die,
8328 int has_children,
8329 void *data)
8330 {
8331 struct dwarf2_cu *cu = reader->cu;
8332 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8333 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8334 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8335 CORE_ADDR baseaddr;
8336 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8337 struct partial_symtab *pst;
8338 enum pc_bounds_kind cu_bounds_kind;
8339 const char *filename;
8340 struct process_psymtab_comp_unit_data *info
8341 = (struct process_psymtab_comp_unit_data *) data;
8342
8343 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8344 return;
8345
8346 gdb_assert (! per_cu->is_debug_types);
8347
8348 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8349
8350 cu->list_in_scope = &file_symbols;
8351
8352 /* Allocate a new partial symbol table structure. */
8353 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8354 if (filename == NULL)
8355 filename = "";
8356
8357 pst = create_partial_symtab (per_cu, filename);
8358
8359 /* This must be done before calling dwarf2_build_include_psymtabs. */
8360 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8361
8362 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8363
8364 dwarf2_find_base_address (comp_unit_die, cu);
8365
8366 /* Possibly set the default values of LOWPC and HIGHPC from
8367 `DW_AT_ranges'. */
8368 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8369 &best_highpc, cu, pst);
8370 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8371 /* Store the contiguous range if it is not empty; it can be empty for
8372 CUs with no code. */
8373 addrmap_set_empty (objfile->psymtabs_addrmap,
8374 gdbarch_adjust_dwarf2_addr (gdbarch,
8375 best_lowpc + baseaddr),
8376 gdbarch_adjust_dwarf2_addr (gdbarch,
8377 best_highpc + baseaddr) - 1,
8378 pst);
8379
8380 /* Check if comp unit has_children.
8381 If so, read the rest of the partial symbols from this comp unit.
8382 If not, there's no more debug_info for this comp unit. */
8383 if (has_children)
8384 {
8385 struct partial_die_info *first_die;
8386 CORE_ADDR lowpc, highpc;
8387
8388 lowpc = ((CORE_ADDR) -1);
8389 highpc = ((CORE_ADDR) 0);
8390
8391 first_die = load_partial_dies (reader, info_ptr, 1);
8392
8393 scan_partial_symbols (first_die, &lowpc, &highpc,
8394 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8395
8396 /* If we didn't find a lowpc, set it to highpc to avoid
8397 complaints from `maint check'. */
8398 if (lowpc == ((CORE_ADDR) -1))
8399 lowpc = highpc;
8400
8401 /* If the compilation unit didn't have an explicit address range,
8402 then use the information extracted from its child dies. */
8403 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8404 {
8405 best_lowpc = lowpc;
8406 best_highpc = highpc;
8407 }
8408 }
8409 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8410 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
8411
8412 end_psymtab_common (objfile, pst);
8413
8414 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8415 {
8416 int i;
8417 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8418 struct dwarf2_per_cu_data *iter;
8419
8420 /* Fill in 'dependencies' here; we fill in 'users' in a
8421 post-pass. */
8422 pst->number_of_dependencies = len;
8423 pst->dependencies =
8424 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8425 for (i = 0;
8426 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8427 i, iter);
8428 ++i)
8429 pst->dependencies[i] = iter->v.psymtab;
8430
8431 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8432 }
8433
8434 /* Get the list of files included in the current compilation unit,
8435 and build a psymtab for each of them. */
8436 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8437
8438 if (dwarf_read_debug)
8439 {
8440 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8441
8442 fprintf_unfiltered (gdb_stdlog,
8443 "Psymtab for %s unit @0x%x: %s - %s"
8444 ", %d global, %d static syms\n",
8445 per_cu->is_debug_types ? "type" : "comp",
8446 to_underlying (per_cu->sect_off),
8447 paddress (gdbarch, pst->textlow),
8448 paddress (gdbarch, pst->texthigh),
8449 pst->n_global_syms, pst->n_static_syms);
8450 }
8451 }
8452
8453 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8454 Process compilation unit THIS_CU for a psymtab. */
8455
8456 static void
8457 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8458 int want_partial_unit,
8459 enum language pretend_language)
8460 {
8461 /* If this compilation unit was already read in, free the
8462 cached copy in order to read it in again. This is
8463 necessary because we skipped some symbols when we first
8464 read in the compilation unit (see load_partial_dies).
8465 This problem could be avoided, but the benefit is unclear. */
8466 if (this_cu->cu != NULL)
8467 free_one_cached_comp_unit (this_cu);
8468
8469 if (this_cu->is_debug_types)
8470 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8471 NULL);
8472 else
8473 {
8474 process_psymtab_comp_unit_data info;
8475 info.want_partial_unit = want_partial_unit;
8476 info.pretend_language = pretend_language;
8477 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8478 process_psymtab_comp_unit_reader, &info);
8479 }
8480
8481 /* Age out any secondary CUs. */
8482 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8483 }
8484
8485 /* Reader function for build_type_psymtabs. */
8486
8487 static void
8488 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8489 const gdb_byte *info_ptr,
8490 struct die_info *type_unit_die,
8491 int has_children,
8492 void *data)
8493 {
8494 struct dwarf2_per_objfile *dwarf2_per_objfile
8495 = reader->cu->per_cu->dwarf2_per_objfile;
8496 struct objfile *objfile = dwarf2_per_objfile->objfile;
8497 struct dwarf2_cu *cu = reader->cu;
8498 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8499 struct signatured_type *sig_type;
8500 struct type_unit_group *tu_group;
8501 struct attribute *attr;
8502 struct partial_die_info *first_die;
8503 CORE_ADDR lowpc, highpc;
8504 struct partial_symtab *pst;
8505
8506 gdb_assert (data == NULL);
8507 gdb_assert (per_cu->is_debug_types);
8508 sig_type = (struct signatured_type *) per_cu;
8509
8510 if (! has_children)
8511 return;
8512
8513 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8514 tu_group = get_type_unit_group (cu, attr);
8515
8516 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8517
8518 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8519 cu->list_in_scope = &file_symbols;
8520 pst = create_partial_symtab (per_cu, "");
8521 pst->anonymous = 1;
8522
8523 first_die = load_partial_dies (reader, info_ptr, 1);
8524
8525 lowpc = (CORE_ADDR) -1;
8526 highpc = (CORE_ADDR) 0;
8527 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8528
8529 end_psymtab_common (objfile, pst);
8530 }
8531
8532 /* Struct used to sort TUs by their abbreviation table offset. */
8533
8534 struct tu_abbrev_offset
8535 {
8536 struct signatured_type *sig_type;
8537 sect_offset abbrev_offset;
8538 };
8539
8540 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
8541
8542 static int
8543 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
8544 {
8545 const struct tu_abbrev_offset * const *a
8546 = (const struct tu_abbrev_offset * const*) ap;
8547 const struct tu_abbrev_offset * const *b
8548 = (const struct tu_abbrev_offset * const*) bp;
8549 sect_offset aoff = (*a)->abbrev_offset;
8550 sect_offset boff = (*b)->abbrev_offset;
8551
8552 return (aoff > boff) - (aoff < boff);
8553 }
8554
8555 /* Efficiently read all the type units.
8556 This does the bulk of the work for build_type_psymtabs.
8557
8558 The efficiency is because we sort TUs by the abbrev table they use and
8559 only read each abbrev table once. In one program there are 200K TUs
8560 sharing 8K abbrev tables.
8561
8562 The main purpose of this function is to support building the
8563 dwarf2_per_objfile->type_unit_groups table.
8564 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8565 can collapse the search space by grouping them by stmt_list.
8566 The savings can be significant, in the same program from above the 200K TUs
8567 share 8K stmt_list tables.
8568
8569 FUNC is expected to call get_type_unit_group, which will create the
8570 struct type_unit_group if necessary and add it to
8571 dwarf2_per_objfile->type_unit_groups. */
8572
8573 static void
8574 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8575 {
8576 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8577 struct cleanup *cleanups;
8578 abbrev_table_up abbrev_table;
8579 sect_offset abbrev_offset;
8580 struct tu_abbrev_offset *sorted_by_abbrev;
8581 int i;
8582
8583 /* It's up to the caller to not call us multiple times. */
8584 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8585
8586 if (dwarf2_per_objfile->n_type_units == 0)
8587 return;
8588
8589 /* TUs typically share abbrev tables, and there can be way more TUs than
8590 abbrev tables. Sort by abbrev table to reduce the number of times we
8591 read each abbrev table in.
8592 Alternatives are to punt or to maintain a cache of abbrev tables.
8593 This is simpler and efficient enough for now.
8594
8595 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8596 symtab to use). Typically TUs with the same abbrev offset have the same
8597 stmt_list value too so in practice this should work well.
8598
8599 The basic algorithm here is:
8600
8601 sort TUs by abbrev table
8602 for each TU with same abbrev table:
8603 read abbrev table if first user
8604 read TU top level DIE
8605 [IWBN if DWO skeletons had DW_AT_stmt_list]
8606 call FUNC */
8607
8608 if (dwarf_read_debug)
8609 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8610
8611 /* Sort in a separate table to maintain the order of all_type_units
8612 for .gdb_index: TU indices directly index all_type_units. */
8613 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
8614 dwarf2_per_objfile->n_type_units);
8615 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8616 {
8617 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8618
8619 sorted_by_abbrev[i].sig_type = sig_type;
8620 sorted_by_abbrev[i].abbrev_offset =
8621 read_abbrev_offset (dwarf2_per_objfile,
8622 sig_type->per_cu.section,
8623 sig_type->per_cu.sect_off);
8624 }
8625 cleanups = make_cleanup (xfree, sorted_by_abbrev);
8626 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
8627 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
8628
8629 abbrev_offset = (sect_offset) ~(unsigned) 0;
8630
8631 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8632 {
8633 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8634
8635 /* Switch to the next abbrev table if necessary. */
8636 if (abbrev_table == NULL
8637 || tu->abbrev_offset != abbrev_offset)
8638 {
8639 abbrev_offset = tu->abbrev_offset;
8640 abbrev_table =
8641 abbrev_table_read_table (dwarf2_per_objfile,
8642 &dwarf2_per_objfile->abbrev,
8643 abbrev_offset);
8644 ++tu_stats->nr_uniq_abbrev_tables;
8645 }
8646
8647 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table.get (),
8648 0, 0, build_type_psymtabs_reader, NULL);
8649 }
8650
8651 do_cleanups (cleanups);
8652 }
8653
8654 /* Print collected type unit statistics. */
8655
8656 static void
8657 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8658 {
8659 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8660
8661 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8662 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8663 dwarf2_per_objfile->n_type_units);
8664 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8665 tu_stats->nr_uniq_abbrev_tables);
8666 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8667 tu_stats->nr_symtabs);
8668 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8669 tu_stats->nr_symtab_sharers);
8670 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8671 tu_stats->nr_stmt_less_type_units);
8672 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8673 tu_stats->nr_all_type_units_reallocs);
8674 }
8675
8676 /* Traversal function for build_type_psymtabs. */
8677
8678 static int
8679 build_type_psymtab_dependencies (void **slot, void *info)
8680 {
8681 struct dwarf2_per_objfile *dwarf2_per_objfile
8682 = (struct dwarf2_per_objfile *) info;
8683 struct objfile *objfile = dwarf2_per_objfile->objfile;
8684 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8685 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8686 struct partial_symtab *pst = per_cu->v.psymtab;
8687 int len = VEC_length (sig_type_ptr, tu_group->tus);
8688 struct signatured_type *iter;
8689 int i;
8690
8691 gdb_assert (len > 0);
8692 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8693
8694 pst->number_of_dependencies = len;
8695 pst->dependencies =
8696 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8697 for (i = 0;
8698 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8699 ++i)
8700 {
8701 gdb_assert (iter->per_cu.is_debug_types);
8702 pst->dependencies[i] = iter->per_cu.v.psymtab;
8703 iter->type_unit_group = tu_group;
8704 }
8705
8706 VEC_free (sig_type_ptr, tu_group->tus);
8707
8708 return 1;
8709 }
8710
8711 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8712 Build partial symbol tables for the .debug_types comp-units. */
8713
8714 static void
8715 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8716 {
8717 if (! create_all_type_units (dwarf2_per_objfile))
8718 return;
8719
8720 build_type_psymtabs_1 (dwarf2_per_objfile);
8721 }
8722
8723 /* Traversal function for process_skeletonless_type_unit.
8724 Read a TU in a DWO file and build partial symbols for it. */
8725
8726 static int
8727 process_skeletonless_type_unit (void **slot, void *info)
8728 {
8729 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8730 struct dwarf2_per_objfile *dwarf2_per_objfile
8731 = (struct dwarf2_per_objfile *) info;
8732 struct signatured_type find_entry, *entry;
8733
8734 /* If this TU doesn't exist in the global table, add it and read it in. */
8735
8736 if (dwarf2_per_objfile->signatured_types == NULL)
8737 {
8738 dwarf2_per_objfile->signatured_types
8739 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8740 }
8741
8742 find_entry.signature = dwo_unit->signature;
8743 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8744 INSERT);
8745 /* If we've already seen this type there's nothing to do. What's happening
8746 is we're doing our own version of comdat-folding here. */
8747 if (*slot != NULL)
8748 return 1;
8749
8750 /* This does the job that create_all_type_units would have done for
8751 this TU. */
8752 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8753 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8754 *slot = entry;
8755
8756 /* This does the job that build_type_psymtabs_1 would have done. */
8757 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8758 build_type_psymtabs_reader, NULL);
8759
8760 return 1;
8761 }
8762
8763 /* Traversal function for process_skeletonless_type_units. */
8764
8765 static int
8766 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8767 {
8768 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8769
8770 if (dwo_file->tus != NULL)
8771 {
8772 htab_traverse_noresize (dwo_file->tus,
8773 process_skeletonless_type_unit, info);
8774 }
8775
8776 return 1;
8777 }
8778
8779 /* Scan all TUs of DWO files, verifying we've processed them.
8780 This is needed in case a TU was emitted without its skeleton.
8781 Note: This can't be done until we know what all the DWO files are. */
8782
8783 static void
8784 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8785 {
8786 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8787 if (get_dwp_file (dwarf2_per_objfile) == NULL
8788 && dwarf2_per_objfile->dwo_files != NULL)
8789 {
8790 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8791 process_dwo_file_for_skeletonless_type_units,
8792 dwarf2_per_objfile);
8793 }
8794 }
8795
8796 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8797
8798 static void
8799 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8800 {
8801 int i;
8802
8803 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8804 {
8805 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8806 struct partial_symtab *pst = per_cu->v.psymtab;
8807 int j;
8808
8809 if (pst == NULL)
8810 continue;
8811
8812 for (j = 0; j < pst->number_of_dependencies; ++j)
8813 {
8814 /* Set the 'user' field only if it is not already set. */
8815 if (pst->dependencies[j]->user == NULL)
8816 pst->dependencies[j]->user = pst;
8817 }
8818 }
8819 }
8820
8821 /* Build the partial symbol table by doing a quick pass through the
8822 .debug_info and .debug_abbrev sections. */
8823
8824 static void
8825 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8826 {
8827 struct cleanup *back_to;
8828 int i;
8829 struct objfile *objfile = dwarf2_per_objfile->objfile;
8830
8831 if (dwarf_read_debug)
8832 {
8833 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8834 objfile_name (objfile));
8835 }
8836
8837 dwarf2_per_objfile->reading_partial_symbols = 1;
8838
8839 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8840
8841 /* Any cached compilation units will be linked by the per-objfile
8842 read_in_chain. Make sure to free them when we're done. */
8843 back_to = make_cleanup (free_cached_comp_units, dwarf2_per_objfile);
8844
8845 build_type_psymtabs (dwarf2_per_objfile);
8846
8847 create_all_comp_units (dwarf2_per_objfile);
8848
8849 /* Create a temporary address map on a temporary obstack. We later
8850 copy this to the final obstack. */
8851 auto_obstack temp_obstack;
8852
8853 scoped_restore save_psymtabs_addrmap
8854 = make_scoped_restore (&objfile->psymtabs_addrmap,
8855 addrmap_create_mutable (&temp_obstack));
8856
8857 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8858 {
8859 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8860
8861 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8862 }
8863
8864 /* This has to wait until we read the CUs, we need the list of DWOs. */
8865 process_skeletonless_type_units (dwarf2_per_objfile);
8866
8867 /* Now that all TUs have been processed we can fill in the dependencies. */
8868 if (dwarf2_per_objfile->type_unit_groups != NULL)
8869 {
8870 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8871 build_type_psymtab_dependencies, dwarf2_per_objfile);
8872 }
8873
8874 if (dwarf_read_debug)
8875 print_tu_stats (dwarf2_per_objfile);
8876
8877 set_partial_user (dwarf2_per_objfile);
8878
8879 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8880 &objfile->objfile_obstack);
8881 /* At this point we want to keep the address map. */
8882 save_psymtabs_addrmap.release ();
8883
8884 do_cleanups (back_to);
8885
8886 if (dwarf_read_debug)
8887 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8888 objfile_name (objfile));
8889 }
8890
8891 /* die_reader_func for load_partial_comp_unit. */
8892
8893 static void
8894 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8895 const gdb_byte *info_ptr,
8896 struct die_info *comp_unit_die,
8897 int has_children,
8898 void *data)
8899 {
8900 struct dwarf2_cu *cu = reader->cu;
8901
8902 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8903
8904 /* Check if comp unit has_children.
8905 If so, read the rest of the partial symbols from this comp unit.
8906 If not, there's no more debug_info for this comp unit. */
8907 if (has_children)
8908 load_partial_dies (reader, info_ptr, 0);
8909 }
8910
8911 /* Load the partial DIEs for a secondary CU into memory.
8912 This is also used when rereading a primary CU with load_all_dies. */
8913
8914 static void
8915 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8916 {
8917 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8918 load_partial_comp_unit_reader, NULL);
8919 }
8920
8921 static void
8922 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8923 struct dwarf2_section_info *section,
8924 struct dwarf2_section_info *abbrev_section,
8925 unsigned int is_dwz,
8926 int *n_allocated,
8927 int *n_comp_units,
8928 struct dwarf2_per_cu_data ***all_comp_units)
8929 {
8930 const gdb_byte *info_ptr;
8931 struct objfile *objfile = dwarf2_per_objfile->objfile;
8932
8933 if (dwarf_read_debug)
8934 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8935 get_section_name (section),
8936 get_section_file_name (section));
8937
8938 dwarf2_read_section (objfile, section);
8939
8940 info_ptr = section->buffer;
8941
8942 while (info_ptr < section->buffer + section->size)
8943 {
8944 struct dwarf2_per_cu_data *this_cu;
8945
8946 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8947
8948 comp_unit_head cu_header;
8949 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8950 abbrev_section, info_ptr,
8951 rcuh_kind::COMPILE);
8952
8953 /* Save the compilation unit for later lookup. */
8954 if (cu_header.unit_type != DW_UT_type)
8955 {
8956 this_cu = XOBNEW (&objfile->objfile_obstack,
8957 struct dwarf2_per_cu_data);
8958 memset (this_cu, 0, sizeof (*this_cu));
8959 }
8960 else
8961 {
8962 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8963 struct signatured_type);
8964 memset (sig_type, 0, sizeof (*sig_type));
8965 sig_type->signature = cu_header.signature;
8966 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8967 this_cu = &sig_type->per_cu;
8968 }
8969 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8970 this_cu->sect_off = sect_off;
8971 this_cu->length = cu_header.length + cu_header.initial_length_size;
8972 this_cu->is_dwz = is_dwz;
8973 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8974 this_cu->section = section;
8975
8976 if (*n_comp_units == *n_allocated)
8977 {
8978 *n_allocated *= 2;
8979 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
8980 *all_comp_units, *n_allocated);
8981 }
8982 (*all_comp_units)[*n_comp_units] = this_cu;
8983 ++*n_comp_units;
8984
8985 info_ptr = info_ptr + this_cu->length;
8986 }
8987 }
8988
8989 /* Create a list of all compilation units in OBJFILE.
8990 This is only done for -readnow and building partial symtabs. */
8991
8992 static void
8993 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8994 {
8995 int n_allocated;
8996 int n_comp_units;
8997 struct dwarf2_per_cu_data **all_comp_units;
8998 struct dwz_file *dwz;
8999 struct objfile *objfile = dwarf2_per_objfile->objfile;
9000
9001 n_comp_units = 0;
9002 n_allocated = 10;
9003 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
9004
9005 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
9006 &dwarf2_per_objfile->abbrev, 0,
9007 &n_allocated, &n_comp_units, &all_comp_units);
9008
9009 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
9010 if (dwz != NULL)
9011 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
9012 1, &n_allocated, &n_comp_units,
9013 &all_comp_units);
9014
9015 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
9016 struct dwarf2_per_cu_data *,
9017 n_comp_units);
9018 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
9019 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
9020 xfree (all_comp_units);
9021 dwarf2_per_objfile->n_comp_units = n_comp_units;
9022 }
9023
9024 /* Process all loaded DIEs for compilation unit CU, starting at
9025 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
9026 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
9027 DW_AT_ranges). See the comments of add_partial_subprogram on how
9028 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
9029
9030 static void
9031 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
9032 CORE_ADDR *highpc, int set_addrmap,
9033 struct dwarf2_cu *cu)
9034 {
9035 struct partial_die_info *pdi;
9036
9037 /* Now, march along the PDI's, descending into ones which have
9038 interesting children but skipping the children of the other ones,
9039 until we reach the end of the compilation unit. */
9040
9041 pdi = first_die;
9042
9043 while (pdi != NULL)
9044 {
9045 fixup_partial_die (pdi, cu);
9046
9047 /* Anonymous namespaces or modules have no name but have interesting
9048 children, so we need to look at them. Ditto for anonymous
9049 enums. */
9050
9051 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
9052 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
9053 || pdi->tag == DW_TAG_imported_unit
9054 || pdi->tag == DW_TAG_inlined_subroutine)
9055 {
9056 switch (pdi->tag)
9057 {
9058 case DW_TAG_subprogram:
9059 case DW_TAG_inlined_subroutine:
9060 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9061 break;
9062 case DW_TAG_constant:
9063 case DW_TAG_variable:
9064 case DW_TAG_typedef:
9065 case DW_TAG_union_type:
9066 if (!pdi->is_declaration)
9067 {
9068 add_partial_symbol (pdi, cu);
9069 }
9070 break;
9071 case DW_TAG_class_type:
9072 case DW_TAG_interface_type:
9073 case DW_TAG_structure_type:
9074 if (!pdi->is_declaration)
9075 {
9076 add_partial_symbol (pdi, cu);
9077 }
9078 if (cu->language == language_rust && pdi->has_children)
9079 scan_partial_symbols (pdi->die_child, lowpc, highpc,
9080 set_addrmap, cu);
9081 break;
9082 case DW_TAG_enumeration_type:
9083 if (!pdi->is_declaration)
9084 add_partial_enumeration (pdi, cu);
9085 break;
9086 case DW_TAG_base_type:
9087 case DW_TAG_subrange_type:
9088 /* File scope base type definitions are added to the partial
9089 symbol table. */
9090 add_partial_symbol (pdi, cu);
9091 break;
9092 case DW_TAG_namespace:
9093 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
9094 break;
9095 case DW_TAG_module:
9096 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
9097 break;
9098 case DW_TAG_imported_unit:
9099 {
9100 struct dwarf2_per_cu_data *per_cu;
9101
9102 /* For now we don't handle imported units in type units. */
9103 if (cu->per_cu->is_debug_types)
9104 {
9105 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9106 " supported in type units [in module %s]"),
9107 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9108 }
9109
9110 per_cu = dwarf2_find_containing_comp_unit
9111 (pdi->d.sect_off, pdi->is_dwz,
9112 cu->per_cu->dwarf2_per_objfile);
9113
9114 /* Go read the partial unit, if needed. */
9115 if (per_cu->v.psymtab == NULL)
9116 process_psymtab_comp_unit (per_cu, 1, cu->language);
9117
9118 VEC_safe_push (dwarf2_per_cu_ptr,
9119 cu->per_cu->imported_symtabs, per_cu);
9120 }
9121 break;
9122 case DW_TAG_imported_declaration:
9123 add_partial_symbol (pdi, cu);
9124 break;
9125 default:
9126 break;
9127 }
9128 }
9129
9130 /* If the die has a sibling, skip to the sibling. */
9131
9132 pdi = pdi->die_sibling;
9133 }
9134 }
9135
9136 /* Functions used to compute the fully scoped name of a partial DIE.
9137
9138 Normally, this is simple. For C++, the parent DIE's fully scoped
9139 name is concatenated with "::" and the partial DIE's name.
9140 Enumerators are an exception; they use the scope of their parent
9141 enumeration type, i.e. the name of the enumeration type is not
9142 prepended to the enumerator.
9143
9144 There are two complexities. One is DW_AT_specification; in this
9145 case "parent" means the parent of the target of the specification,
9146 instead of the direct parent of the DIE. The other is compilers
9147 which do not emit DW_TAG_namespace; in this case we try to guess
9148 the fully qualified name of structure types from their members'
9149 linkage names. This must be done using the DIE's children rather
9150 than the children of any DW_AT_specification target. We only need
9151 to do this for structures at the top level, i.e. if the target of
9152 any DW_AT_specification (if any; otherwise the DIE itself) does not
9153 have a parent. */
9154
9155 /* Compute the scope prefix associated with PDI's parent, in
9156 compilation unit CU. The result will be allocated on CU's
9157 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9158 field. NULL is returned if no prefix is necessary. */
9159 static const char *
9160 partial_die_parent_scope (struct partial_die_info *pdi,
9161 struct dwarf2_cu *cu)
9162 {
9163 const char *grandparent_scope;
9164 struct partial_die_info *parent, *real_pdi;
9165
9166 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9167 then this means the parent of the specification DIE. */
9168
9169 real_pdi = pdi;
9170 while (real_pdi->has_specification)
9171 real_pdi = find_partial_die (real_pdi->spec_offset,
9172 real_pdi->spec_is_dwz, cu);
9173
9174 parent = real_pdi->die_parent;
9175 if (parent == NULL)
9176 return NULL;
9177
9178 if (parent->scope_set)
9179 return parent->scope;
9180
9181 fixup_partial_die (parent, cu);
9182
9183 grandparent_scope = partial_die_parent_scope (parent, cu);
9184
9185 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9186 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9187 Work around this problem here. */
9188 if (cu->language == language_cplus
9189 && parent->tag == DW_TAG_namespace
9190 && strcmp (parent->name, "::") == 0
9191 && grandparent_scope == NULL)
9192 {
9193 parent->scope = NULL;
9194 parent->scope_set = 1;
9195 return NULL;
9196 }
9197
9198 if (pdi->tag == DW_TAG_enumerator)
9199 /* Enumerators should not get the name of the enumeration as a prefix. */
9200 parent->scope = grandparent_scope;
9201 else if (parent->tag == DW_TAG_namespace
9202 || parent->tag == DW_TAG_module
9203 || parent->tag == DW_TAG_structure_type
9204 || parent->tag == DW_TAG_class_type
9205 || parent->tag == DW_TAG_interface_type
9206 || parent->tag == DW_TAG_union_type
9207 || parent->tag == DW_TAG_enumeration_type)
9208 {
9209 if (grandparent_scope == NULL)
9210 parent->scope = parent->name;
9211 else
9212 parent->scope = typename_concat (&cu->comp_unit_obstack,
9213 grandparent_scope,
9214 parent->name, 0, cu);
9215 }
9216 else
9217 {
9218 /* FIXME drow/2004-04-01: What should we be doing with
9219 function-local names? For partial symbols, we should probably be
9220 ignoring them. */
9221 complaint (&symfile_complaints,
9222 _("unhandled containing DIE tag %d for DIE at %d"),
9223 parent->tag, to_underlying (pdi->sect_off));
9224 parent->scope = grandparent_scope;
9225 }
9226
9227 parent->scope_set = 1;
9228 return parent->scope;
9229 }
9230
9231 /* Return the fully scoped name associated with PDI, from compilation unit
9232 CU. The result will be allocated with malloc. */
9233
9234 static char *
9235 partial_die_full_name (struct partial_die_info *pdi,
9236 struct dwarf2_cu *cu)
9237 {
9238 const char *parent_scope;
9239
9240 /* If this is a template instantiation, we can not work out the
9241 template arguments from partial DIEs. So, unfortunately, we have
9242 to go through the full DIEs. At least any work we do building
9243 types here will be reused if full symbols are loaded later. */
9244 if (pdi->has_template_arguments)
9245 {
9246 fixup_partial_die (pdi, cu);
9247
9248 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9249 {
9250 struct die_info *die;
9251 struct attribute attr;
9252 struct dwarf2_cu *ref_cu = cu;
9253
9254 /* DW_FORM_ref_addr is using section offset. */
9255 attr.name = (enum dwarf_attribute) 0;
9256 attr.form = DW_FORM_ref_addr;
9257 attr.u.unsnd = to_underlying (pdi->sect_off);
9258 die = follow_die_ref (NULL, &attr, &ref_cu);
9259
9260 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9261 }
9262 }
9263
9264 parent_scope = partial_die_parent_scope (pdi, cu);
9265 if (parent_scope == NULL)
9266 return NULL;
9267 else
9268 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
9269 }
9270
9271 static void
9272 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
9273 {
9274 struct dwarf2_per_objfile *dwarf2_per_objfile
9275 = cu->per_cu->dwarf2_per_objfile;
9276 struct objfile *objfile = dwarf2_per_objfile->objfile;
9277 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9278 CORE_ADDR addr = 0;
9279 const char *actual_name = NULL;
9280 CORE_ADDR baseaddr;
9281 char *built_actual_name;
9282
9283 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9284
9285 built_actual_name = partial_die_full_name (pdi, cu);
9286 if (built_actual_name != NULL)
9287 actual_name = built_actual_name;
9288
9289 if (actual_name == NULL)
9290 actual_name = pdi->name;
9291
9292 switch (pdi->tag)
9293 {
9294 case DW_TAG_inlined_subroutine:
9295 case DW_TAG_subprogram:
9296 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
9297 if (pdi->is_external || cu->language == language_ada)
9298 {
9299 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9300 of the global scope. But in Ada, we want to be able to access
9301 nested procedures globally. So all Ada subprograms are stored
9302 in the global scope. */
9303 add_psymbol_to_list (actual_name, strlen (actual_name),
9304 built_actual_name != NULL,
9305 VAR_DOMAIN, LOC_BLOCK,
9306 &objfile->global_psymbols,
9307 addr, cu->language, objfile);
9308 }
9309 else
9310 {
9311 add_psymbol_to_list (actual_name, strlen (actual_name),
9312 built_actual_name != NULL,
9313 VAR_DOMAIN, LOC_BLOCK,
9314 &objfile->static_psymbols,
9315 addr, cu->language, objfile);
9316 }
9317
9318 if (pdi->main_subprogram && actual_name != NULL)
9319 set_objfile_main_name (objfile, actual_name, cu->language);
9320 break;
9321 case DW_TAG_constant:
9322 {
9323 std::vector<partial_symbol *> *list;
9324
9325 if (pdi->is_external)
9326 list = &objfile->global_psymbols;
9327 else
9328 list = &objfile->static_psymbols;
9329 add_psymbol_to_list (actual_name, strlen (actual_name),
9330 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9331 list, 0, cu->language, objfile);
9332 }
9333 break;
9334 case DW_TAG_variable:
9335 if (pdi->d.locdesc)
9336 addr = decode_locdesc (pdi->d.locdesc, cu);
9337
9338 if (pdi->d.locdesc
9339 && addr == 0
9340 && !dwarf2_per_objfile->has_section_at_zero)
9341 {
9342 /* A global or static variable may also have been stripped
9343 out by the linker if unused, in which case its address
9344 will be nullified; do not add such variables into partial
9345 symbol table then. */
9346 }
9347 else if (pdi->is_external)
9348 {
9349 /* Global Variable.
9350 Don't enter into the minimal symbol tables as there is
9351 a minimal symbol table entry from the ELF symbols already.
9352 Enter into partial symbol table if it has a location
9353 descriptor or a type.
9354 If the location descriptor is missing, new_symbol will create
9355 a LOC_UNRESOLVED symbol, the address of the variable will then
9356 be determined from the minimal symbol table whenever the variable
9357 is referenced.
9358 The address for the partial symbol table entry is not
9359 used by GDB, but it comes in handy for debugging partial symbol
9360 table building. */
9361
9362 if (pdi->d.locdesc || pdi->has_type)
9363 add_psymbol_to_list (actual_name, strlen (actual_name),
9364 built_actual_name != NULL,
9365 VAR_DOMAIN, LOC_STATIC,
9366 &objfile->global_psymbols,
9367 addr + baseaddr,
9368 cu->language, objfile);
9369 }
9370 else
9371 {
9372 int has_loc = pdi->d.locdesc != NULL;
9373
9374 /* Static Variable. Skip symbols whose value we cannot know (those
9375 without location descriptors or constant values). */
9376 if (!has_loc && !pdi->has_const_value)
9377 {
9378 xfree (built_actual_name);
9379 return;
9380 }
9381
9382 add_psymbol_to_list (actual_name, strlen (actual_name),
9383 built_actual_name != NULL,
9384 VAR_DOMAIN, LOC_STATIC,
9385 &objfile->static_psymbols,
9386 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
9387 cu->language, objfile);
9388 }
9389 break;
9390 case DW_TAG_typedef:
9391 case DW_TAG_base_type:
9392 case DW_TAG_subrange_type:
9393 add_psymbol_to_list (actual_name, strlen (actual_name),
9394 built_actual_name != NULL,
9395 VAR_DOMAIN, LOC_TYPEDEF,
9396 &objfile->static_psymbols,
9397 0, cu->language, objfile);
9398 break;
9399 case DW_TAG_imported_declaration:
9400 case DW_TAG_namespace:
9401 add_psymbol_to_list (actual_name, strlen (actual_name),
9402 built_actual_name != NULL,
9403 VAR_DOMAIN, LOC_TYPEDEF,
9404 &objfile->global_psymbols,
9405 0, cu->language, objfile);
9406 break;
9407 case DW_TAG_module:
9408 add_psymbol_to_list (actual_name, strlen (actual_name),
9409 built_actual_name != NULL,
9410 MODULE_DOMAIN, LOC_TYPEDEF,
9411 &objfile->global_psymbols,
9412 0, cu->language, objfile);
9413 break;
9414 case DW_TAG_class_type:
9415 case DW_TAG_interface_type:
9416 case DW_TAG_structure_type:
9417 case DW_TAG_union_type:
9418 case DW_TAG_enumeration_type:
9419 /* Skip external references. The DWARF standard says in the section
9420 about "Structure, Union, and Class Type Entries": "An incomplete
9421 structure, union or class type is represented by a structure,
9422 union or class entry that does not have a byte size attribute
9423 and that has a DW_AT_declaration attribute." */
9424 if (!pdi->has_byte_size && pdi->is_declaration)
9425 {
9426 xfree (built_actual_name);
9427 return;
9428 }
9429
9430 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9431 static vs. global. */
9432 add_psymbol_to_list (actual_name, strlen (actual_name),
9433 built_actual_name != NULL,
9434 STRUCT_DOMAIN, LOC_TYPEDEF,
9435 cu->language == language_cplus
9436 ? &objfile->global_psymbols
9437 : &objfile->static_psymbols,
9438 0, cu->language, objfile);
9439
9440 break;
9441 case DW_TAG_enumerator:
9442 add_psymbol_to_list (actual_name, strlen (actual_name),
9443 built_actual_name != NULL,
9444 VAR_DOMAIN, LOC_CONST,
9445 cu->language == language_cplus
9446 ? &objfile->global_psymbols
9447 : &objfile->static_psymbols,
9448 0, cu->language, objfile);
9449 break;
9450 default:
9451 break;
9452 }
9453
9454 xfree (built_actual_name);
9455 }
9456
9457 /* Read a partial die corresponding to a namespace; also, add a symbol
9458 corresponding to that namespace to the symbol table. NAMESPACE is
9459 the name of the enclosing namespace. */
9460
9461 static void
9462 add_partial_namespace (struct partial_die_info *pdi,
9463 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9464 int set_addrmap, struct dwarf2_cu *cu)
9465 {
9466 /* Add a symbol for the namespace. */
9467
9468 add_partial_symbol (pdi, cu);
9469
9470 /* Now scan partial symbols in that namespace. */
9471
9472 if (pdi->has_children)
9473 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9474 }
9475
9476 /* Read a partial die corresponding to a Fortran module. */
9477
9478 static void
9479 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9480 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9481 {
9482 /* Add a symbol for the namespace. */
9483
9484 add_partial_symbol (pdi, cu);
9485
9486 /* Now scan partial symbols in that module. */
9487
9488 if (pdi->has_children)
9489 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9490 }
9491
9492 /* Read a partial die corresponding to a subprogram or an inlined
9493 subprogram and create a partial symbol for that subprogram.
9494 When the CU language allows it, this routine also defines a partial
9495 symbol for each nested subprogram that this subprogram contains.
9496 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9497 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9498
9499 PDI may also be a lexical block, in which case we simply search
9500 recursively for subprograms defined inside that lexical block.
9501 Again, this is only performed when the CU language allows this
9502 type of definitions. */
9503
9504 static void
9505 add_partial_subprogram (struct partial_die_info *pdi,
9506 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9507 int set_addrmap, struct dwarf2_cu *cu)
9508 {
9509 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9510 {
9511 if (pdi->has_pc_info)
9512 {
9513 if (pdi->lowpc < *lowpc)
9514 *lowpc = pdi->lowpc;
9515 if (pdi->highpc > *highpc)
9516 *highpc = pdi->highpc;
9517 if (set_addrmap)
9518 {
9519 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9520 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9521 CORE_ADDR baseaddr;
9522 CORE_ADDR highpc;
9523 CORE_ADDR lowpc;
9524
9525 baseaddr = ANOFFSET (objfile->section_offsets,
9526 SECT_OFF_TEXT (objfile));
9527 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9528 pdi->lowpc + baseaddr);
9529 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9530 pdi->highpc + baseaddr);
9531 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9532 cu->per_cu->v.psymtab);
9533 }
9534 }
9535
9536 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9537 {
9538 if (!pdi->is_declaration)
9539 /* Ignore subprogram DIEs that do not have a name, they are
9540 illegal. Do not emit a complaint at this point, we will
9541 do so when we convert this psymtab into a symtab. */
9542 if (pdi->name)
9543 add_partial_symbol (pdi, cu);
9544 }
9545 }
9546
9547 if (! pdi->has_children)
9548 return;
9549
9550 if (cu->language == language_ada)
9551 {
9552 pdi = pdi->die_child;
9553 while (pdi != NULL)
9554 {
9555 fixup_partial_die (pdi, cu);
9556 if (pdi->tag == DW_TAG_subprogram
9557 || pdi->tag == DW_TAG_inlined_subroutine
9558 || pdi->tag == DW_TAG_lexical_block)
9559 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9560 pdi = pdi->die_sibling;
9561 }
9562 }
9563 }
9564
9565 /* Read a partial die corresponding to an enumeration type. */
9566
9567 static void
9568 add_partial_enumeration (struct partial_die_info *enum_pdi,
9569 struct dwarf2_cu *cu)
9570 {
9571 struct partial_die_info *pdi;
9572
9573 if (enum_pdi->name != NULL)
9574 add_partial_symbol (enum_pdi, cu);
9575
9576 pdi = enum_pdi->die_child;
9577 while (pdi)
9578 {
9579 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9580 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9581 else
9582 add_partial_symbol (pdi, cu);
9583 pdi = pdi->die_sibling;
9584 }
9585 }
9586
9587 /* Return the initial uleb128 in the die at INFO_PTR. */
9588
9589 static unsigned int
9590 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9591 {
9592 unsigned int bytes_read;
9593
9594 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9595 }
9596
9597 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9598 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9599
9600 Return the corresponding abbrev, or NULL if the number is zero (indicating
9601 an empty DIE). In either case *BYTES_READ will be set to the length of
9602 the initial number. */
9603
9604 static struct abbrev_info *
9605 peek_die_abbrev (const die_reader_specs &reader,
9606 const gdb_byte *info_ptr, unsigned int *bytes_read)
9607 {
9608 dwarf2_cu *cu = reader.cu;
9609 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9610 unsigned int abbrev_number
9611 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9612
9613 if (abbrev_number == 0)
9614 return NULL;
9615
9616 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9617 if (!abbrev)
9618 {
9619 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9620 " at offset 0x%x [in module %s]"),
9621 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9622 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
9623 }
9624
9625 return abbrev;
9626 }
9627
9628 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9629 Returns a pointer to the end of a series of DIEs, terminated by an empty
9630 DIE. Any children of the skipped DIEs will also be skipped. */
9631
9632 static const gdb_byte *
9633 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9634 {
9635 while (1)
9636 {
9637 unsigned int bytes_read;
9638 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9639
9640 if (abbrev == NULL)
9641 return info_ptr + bytes_read;
9642 else
9643 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9644 }
9645 }
9646
9647 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9648 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9649 abbrev corresponding to that skipped uleb128 should be passed in
9650 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9651 children. */
9652
9653 static const gdb_byte *
9654 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9655 struct abbrev_info *abbrev)
9656 {
9657 unsigned int bytes_read;
9658 struct attribute attr;
9659 bfd *abfd = reader->abfd;
9660 struct dwarf2_cu *cu = reader->cu;
9661 const gdb_byte *buffer = reader->buffer;
9662 const gdb_byte *buffer_end = reader->buffer_end;
9663 unsigned int form, i;
9664
9665 for (i = 0; i < abbrev->num_attrs; i++)
9666 {
9667 /* The only abbrev we care about is DW_AT_sibling. */
9668 if (abbrev->attrs[i].name == DW_AT_sibling)
9669 {
9670 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9671 if (attr.form == DW_FORM_ref_addr)
9672 complaint (&symfile_complaints,
9673 _("ignoring absolute DW_AT_sibling"));
9674 else
9675 {
9676 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9677 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9678
9679 if (sibling_ptr < info_ptr)
9680 complaint (&symfile_complaints,
9681 _("DW_AT_sibling points backwards"));
9682 else if (sibling_ptr > reader->buffer_end)
9683 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9684 else
9685 return sibling_ptr;
9686 }
9687 }
9688
9689 /* If it isn't DW_AT_sibling, skip this attribute. */
9690 form = abbrev->attrs[i].form;
9691 skip_attribute:
9692 switch (form)
9693 {
9694 case DW_FORM_ref_addr:
9695 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9696 and later it is offset sized. */
9697 if (cu->header.version == 2)
9698 info_ptr += cu->header.addr_size;
9699 else
9700 info_ptr += cu->header.offset_size;
9701 break;
9702 case DW_FORM_GNU_ref_alt:
9703 info_ptr += cu->header.offset_size;
9704 break;
9705 case DW_FORM_addr:
9706 info_ptr += cu->header.addr_size;
9707 break;
9708 case DW_FORM_data1:
9709 case DW_FORM_ref1:
9710 case DW_FORM_flag:
9711 info_ptr += 1;
9712 break;
9713 case DW_FORM_flag_present:
9714 case DW_FORM_implicit_const:
9715 break;
9716 case DW_FORM_data2:
9717 case DW_FORM_ref2:
9718 info_ptr += 2;
9719 break;
9720 case DW_FORM_data4:
9721 case DW_FORM_ref4:
9722 info_ptr += 4;
9723 break;
9724 case DW_FORM_data8:
9725 case DW_FORM_ref8:
9726 case DW_FORM_ref_sig8:
9727 info_ptr += 8;
9728 break;
9729 case DW_FORM_data16:
9730 info_ptr += 16;
9731 break;
9732 case DW_FORM_string:
9733 read_direct_string (abfd, info_ptr, &bytes_read);
9734 info_ptr += bytes_read;
9735 break;
9736 case DW_FORM_sec_offset:
9737 case DW_FORM_strp:
9738 case DW_FORM_GNU_strp_alt:
9739 info_ptr += cu->header.offset_size;
9740 break;
9741 case DW_FORM_exprloc:
9742 case DW_FORM_block:
9743 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9744 info_ptr += bytes_read;
9745 break;
9746 case DW_FORM_block1:
9747 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9748 break;
9749 case DW_FORM_block2:
9750 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9751 break;
9752 case DW_FORM_block4:
9753 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9754 break;
9755 case DW_FORM_sdata:
9756 case DW_FORM_udata:
9757 case DW_FORM_ref_udata:
9758 case DW_FORM_GNU_addr_index:
9759 case DW_FORM_GNU_str_index:
9760 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9761 break;
9762 case DW_FORM_indirect:
9763 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9764 info_ptr += bytes_read;
9765 /* We need to continue parsing from here, so just go back to
9766 the top. */
9767 goto skip_attribute;
9768
9769 default:
9770 error (_("Dwarf Error: Cannot handle %s "
9771 "in DWARF reader [in module %s]"),
9772 dwarf_form_name (form),
9773 bfd_get_filename (abfd));
9774 }
9775 }
9776
9777 if (abbrev->has_children)
9778 return skip_children (reader, info_ptr);
9779 else
9780 return info_ptr;
9781 }
9782
9783 /* Locate ORIG_PDI's sibling.
9784 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9785
9786 static const gdb_byte *
9787 locate_pdi_sibling (const struct die_reader_specs *reader,
9788 struct partial_die_info *orig_pdi,
9789 const gdb_byte *info_ptr)
9790 {
9791 /* Do we know the sibling already? */
9792
9793 if (orig_pdi->sibling)
9794 return orig_pdi->sibling;
9795
9796 /* Are there any children to deal with? */
9797
9798 if (!orig_pdi->has_children)
9799 return info_ptr;
9800
9801 /* Skip the children the long way. */
9802
9803 return skip_children (reader, info_ptr);
9804 }
9805
9806 /* Expand this partial symbol table into a full symbol table. SELF is
9807 not NULL. */
9808
9809 static void
9810 dwarf2_read_symtab (struct partial_symtab *self,
9811 struct objfile *objfile)
9812 {
9813 struct dwarf2_per_objfile *dwarf2_per_objfile
9814 = get_dwarf2_per_objfile (objfile);
9815
9816 if (self->readin)
9817 {
9818 warning (_("bug: psymtab for %s is already read in."),
9819 self->filename);
9820 }
9821 else
9822 {
9823 if (info_verbose)
9824 {
9825 printf_filtered (_("Reading in symbols for %s..."),
9826 self->filename);
9827 gdb_flush (gdb_stdout);
9828 }
9829
9830 /* If this psymtab is constructed from a debug-only objfile, the
9831 has_section_at_zero flag will not necessarily be correct. We
9832 can get the correct value for this flag by looking at the data
9833 associated with the (presumably stripped) associated objfile. */
9834 if (objfile->separate_debug_objfile_backlink)
9835 {
9836 struct dwarf2_per_objfile *dpo_backlink
9837 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9838
9839 dwarf2_per_objfile->has_section_at_zero
9840 = dpo_backlink->has_section_at_zero;
9841 }
9842
9843 dwarf2_per_objfile->reading_partial_symbols = 0;
9844
9845 psymtab_to_symtab_1 (self);
9846
9847 /* Finish up the debug error message. */
9848 if (info_verbose)
9849 printf_filtered (_("done.\n"));
9850 }
9851
9852 process_cu_includes (dwarf2_per_objfile);
9853 }
9854 \f
9855 /* Reading in full CUs. */
9856
9857 /* Add PER_CU to the queue. */
9858
9859 static void
9860 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9861 enum language pretend_language)
9862 {
9863 struct dwarf2_queue_item *item;
9864
9865 per_cu->queued = 1;
9866 item = XNEW (struct dwarf2_queue_item);
9867 item->per_cu = per_cu;
9868 item->pretend_language = pretend_language;
9869 item->next = NULL;
9870
9871 if (dwarf2_queue == NULL)
9872 dwarf2_queue = item;
9873 else
9874 dwarf2_queue_tail->next = item;
9875
9876 dwarf2_queue_tail = item;
9877 }
9878
9879 /* If PER_CU is not yet queued, add it to the queue.
9880 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9881 dependency.
9882 The result is non-zero if PER_CU was queued, otherwise the result is zero
9883 meaning either PER_CU is already queued or it is already loaded.
9884
9885 N.B. There is an invariant here that if a CU is queued then it is loaded.
9886 The caller is required to load PER_CU if we return non-zero. */
9887
9888 static int
9889 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9890 struct dwarf2_per_cu_data *per_cu,
9891 enum language pretend_language)
9892 {
9893 /* We may arrive here during partial symbol reading, if we need full
9894 DIEs to process an unusual case (e.g. template arguments). Do
9895 not queue PER_CU, just tell our caller to load its DIEs. */
9896 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9897 {
9898 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9899 return 1;
9900 return 0;
9901 }
9902
9903 /* Mark the dependence relation so that we don't flush PER_CU
9904 too early. */
9905 if (dependent_cu != NULL)
9906 dwarf2_add_dependence (dependent_cu, per_cu);
9907
9908 /* If it's already on the queue, we have nothing to do. */
9909 if (per_cu->queued)
9910 return 0;
9911
9912 /* If the compilation unit is already loaded, just mark it as
9913 used. */
9914 if (per_cu->cu != NULL)
9915 {
9916 per_cu->cu->last_used = 0;
9917 return 0;
9918 }
9919
9920 /* Add it to the queue. */
9921 queue_comp_unit (per_cu, pretend_language);
9922
9923 return 1;
9924 }
9925
9926 /* Process the queue. */
9927
9928 static void
9929 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9930 {
9931 struct dwarf2_queue_item *item, *next_item;
9932
9933 if (dwarf_read_debug)
9934 {
9935 fprintf_unfiltered (gdb_stdlog,
9936 "Expanding one or more symtabs of objfile %s ...\n",
9937 objfile_name (dwarf2_per_objfile->objfile));
9938 }
9939
9940 /* The queue starts out with one item, but following a DIE reference
9941 may load a new CU, adding it to the end of the queue. */
9942 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9943 {
9944 if ((dwarf2_per_objfile->using_index
9945 ? !item->per_cu->v.quick->compunit_symtab
9946 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9947 /* Skip dummy CUs. */
9948 && item->per_cu->cu != NULL)
9949 {
9950 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9951 unsigned int debug_print_threshold;
9952 char buf[100];
9953
9954 if (per_cu->is_debug_types)
9955 {
9956 struct signatured_type *sig_type =
9957 (struct signatured_type *) per_cu;
9958
9959 sprintf (buf, "TU %s at offset 0x%x",
9960 hex_string (sig_type->signature),
9961 to_underlying (per_cu->sect_off));
9962 /* There can be 100s of TUs.
9963 Only print them in verbose mode. */
9964 debug_print_threshold = 2;
9965 }
9966 else
9967 {
9968 sprintf (buf, "CU at offset 0x%x",
9969 to_underlying (per_cu->sect_off));
9970 debug_print_threshold = 1;
9971 }
9972
9973 if (dwarf_read_debug >= debug_print_threshold)
9974 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9975
9976 if (per_cu->is_debug_types)
9977 process_full_type_unit (per_cu, item->pretend_language);
9978 else
9979 process_full_comp_unit (per_cu, item->pretend_language);
9980
9981 if (dwarf_read_debug >= debug_print_threshold)
9982 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9983 }
9984
9985 item->per_cu->queued = 0;
9986 next_item = item->next;
9987 xfree (item);
9988 }
9989
9990 dwarf2_queue_tail = NULL;
9991
9992 if (dwarf_read_debug)
9993 {
9994 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9995 objfile_name (dwarf2_per_objfile->objfile));
9996 }
9997 }
9998
9999 /* Read in full symbols for PST, and anything it depends on. */
10000
10001 static void
10002 psymtab_to_symtab_1 (struct partial_symtab *pst)
10003 {
10004 struct dwarf2_per_cu_data *per_cu;
10005 int i;
10006
10007 if (pst->readin)
10008 return;
10009
10010 for (i = 0; i < pst->number_of_dependencies; i++)
10011 if (!pst->dependencies[i]->readin
10012 && pst->dependencies[i]->user == NULL)
10013 {
10014 /* Inform about additional files that need to be read in. */
10015 if (info_verbose)
10016 {
10017 /* FIXME: i18n: Need to make this a single string. */
10018 fputs_filtered (" ", gdb_stdout);
10019 wrap_here ("");
10020 fputs_filtered ("and ", gdb_stdout);
10021 wrap_here ("");
10022 printf_filtered ("%s...", pst->dependencies[i]->filename);
10023 wrap_here (""); /* Flush output. */
10024 gdb_flush (gdb_stdout);
10025 }
10026 psymtab_to_symtab_1 (pst->dependencies[i]);
10027 }
10028
10029 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10030
10031 if (per_cu == NULL)
10032 {
10033 /* It's an include file, no symbols to read for it.
10034 Everything is in the parent symtab. */
10035 pst->readin = 1;
10036 return;
10037 }
10038
10039 dw2_do_instantiate_symtab (per_cu);
10040 }
10041
10042 /* Trivial hash function for die_info: the hash value of a DIE
10043 is its offset in .debug_info for this objfile. */
10044
10045 static hashval_t
10046 die_hash (const void *item)
10047 {
10048 const struct die_info *die = (const struct die_info *) item;
10049
10050 return to_underlying (die->sect_off);
10051 }
10052
10053 /* Trivial comparison function for die_info structures: two DIEs
10054 are equal if they have the same offset. */
10055
10056 static int
10057 die_eq (const void *item_lhs, const void *item_rhs)
10058 {
10059 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
10060 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
10061
10062 return die_lhs->sect_off == die_rhs->sect_off;
10063 }
10064
10065 /* die_reader_func for load_full_comp_unit.
10066 This is identical to read_signatured_type_reader,
10067 but is kept separate for now. */
10068
10069 static void
10070 load_full_comp_unit_reader (const struct die_reader_specs *reader,
10071 const gdb_byte *info_ptr,
10072 struct die_info *comp_unit_die,
10073 int has_children,
10074 void *data)
10075 {
10076 struct dwarf2_cu *cu = reader->cu;
10077 enum language *language_ptr = (enum language *) data;
10078
10079 gdb_assert (cu->die_hash == NULL);
10080 cu->die_hash =
10081 htab_create_alloc_ex (cu->header.length / 12,
10082 die_hash,
10083 die_eq,
10084 NULL,
10085 &cu->comp_unit_obstack,
10086 hashtab_obstack_allocate,
10087 dummy_obstack_deallocate);
10088
10089 if (has_children)
10090 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10091 &info_ptr, comp_unit_die);
10092 cu->dies = comp_unit_die;
10093 /* comp_unit_die is not stored in die_hash, no need. */
10094
10095 /* We try not to read any attributes in this function, because not
10096 all CUs needed for references have been loaded yet, and symbol
10097 table processing isn't initialized. But we have to set the CU language,
10098 or we won't be able to build types correctly.
10099 Similarly, if we do not read the producer, we can not apply
10100 producer-specific interpretation. */
10101 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
10102 }
10103
10104 /* Load the DIEs associated with PER_CU into memory. */
10105
10106 static void
10107 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10108 enum language pretend_language)
10109 {
10110 gdb_assert (! this_cu->is_debug_types);
10111
10112 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10113 load_full_comp_unit_reader, &pretend_language);
10114 }
10115
10116 /* Add a DIE to the delayed physname list. */
10117
10118 static void
10119 add_to_method_list (struct type *type, int fnfield_index, int index,
10120 const char *name, struct die_info *die,
10121 struct dwarf2_cu *cu)
10122 {
10123 struct delayed_method_info mi;
10124 mi.type = type;
10125 mi.fnfield_index = fnfield_index;
10126 mi.index = index;
10127 mi.name = name;
10128 mi.die = die;
10129 cu->method_list.push_back (mi);
10130 }
10131
10132 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10133 "const" / "volatile". If so, decrements LEN by the length of the
10134 modifier and return true. Otherwise return false. */
10135
10136 template<size_t N>
10137 static bool
10138 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10139 {
10140 size_t mod_len = sizeof (mod) - 1;
10141 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10142 {
10143 len -= mod_len;
10144 return true;
10145 }
10146 return false;
10147 }
10148
10149 /* Compute the physnames of any methods on the CU's method list.
10150
10151 The computation of method physnames is delayed in order to avoid the
10152 (bad) condition that one of the method's formal parameters is of an as yet
10153 incomplete type. */
10154
10155 static void
10156 compute_delayed_physnames (struct dwarf2_cu *cu)
10157 {
10158 /* Only C++ delays computing physnames. */
10159 if (cu->method_list.empty ())
10160 return;
10161 gdb_assert (cu->language == language_cplus);
10162
10163 for (struct delayed_method_info &mi : cu->method_list)
10164 {
10165 const char *physname;
10166 struct fn_fieldlist *fn_flp
10167 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
10168 physname = dwarf2_physname (mi.name, mi.die, cu);
10169 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
10170 = physname ? physname : "";
10171
10172 /* Since there's no tag to indicate whether a method is a
10173 const/volatile overload, extract that information out of the
10174 demangled name. */
10175 if (physname != NULL)
10176 {
10177 size_t len = strlen (physname);
10178
10179 while (1)
10180 {
10181 if (physname[len] == ')') /* shortcut */
10182 break;
10183 else if (check_modifier (physname, len, " const"))
10184 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
10185 else if (check_modifier (physname, len, " volatile"))
10186 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
10187 else
10188 break;
10189 }
10190 }
10191 }
10192
10193 /* The list is no longer needed. */
10194 cu->method_list.clear ();
10195 }
10196
10197 /* Go objects should be embedded in a DW_TAG_module DIE,
10198 and it's not clear if/how imported objects will appear.
10199 To keep Go support simple until that's worked out,
10200 go back through what we've read and create something usable.
10201 We could do this while processing each DIE, and feels kinda cleaner,
10202 but that way is more invasive.
10203 This is to, for example, allow the user to type "p var" or "b main"
10204 without having to specify the package name, and allow lookups
10205 of module.object to work in contexts that use the expression
10206 parser. */
10207
10208 static void
10209 fixup_go_packaging (struct dwarf2_cu *cu)
10210 {
10211 char *package_name = NULL;
10212 struct pending *list;
10213 int i;
10214
10215 for (list = global_symbols; list != NULL; list = list->next)
10216 {
10217 for (i = 0; i < list->nsyms; ++i)
10218 {
10219 struct symbol *sym = list->symbol[i];
10220
10221 if (SYMBOL_LANGUAGE (sym) == language_go
10222 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10223 {
10224 char *this_package_name = go_symbol_package_name (sym);
10225
10226 if (this_package_name == NULL)
10227 continue;
10228 if (package_name == NULL)
10229 package_name = this_package_name;
10230 else
10231 {
10232 struct objfile *objfile
10233 = cu->per_cu->dwarf2_per_objfile->objfile;
10234 if (strcmp (package_name, this_package_name) != 0)
10235 complaint (&symfile_complaints,
10236 _("Symtab %s has objects from two different Go packages: %s and %s"),
10237 (symbol_symtab (sym) != NULL
10238 ? symtab_to_filename_for_display
10239 (symbol_symtab (sym))
10240 : objfile_name (objfile)),
10241 this_package_name, package_name);
10242 xfree (this_package_name);
10243 }
10244 }
10245 }
10246 }
10247
10248 if (package_name != NULL)
10249 {
10250 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10251 const char *saved_package_name
10252 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10253 package_name,
10254 strlen (package_name));
10255 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10256 saved_package_name);
10257 struct symbol *sym;
10258
10259 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10260
10261 sym = allocate_symbol (objfile);
10262 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
10263 SYMBOL_SET_NAMES (sym, saved_package_name,
10264 strlen (saved_package_name), 0, objfile);
10265 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10266 e.g., "main" finds the "main" module and not C's main(). */
10267 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10268 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
10269 SYMBOL_TYPE (sym) = type;
10270
10271 add_symbol_to_list (sym, &global_symbols);
10272
10273 xfree (package_name);
10274 }
10275 }
10276
10277 /* Return the symtab for PER_CU. This works properly regardless of
10278 whether we're using the index or psymtabs. */
10279
10280 static struct compunit_symtab *
10281 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10282 {
10283 return (per_cu->dwarf2_per_objfile->using_index
10284 ? per_cu->v.quick->compunit_symtab
10285 : per_cu->v.psymtab->compunit_symtab);
10286 }
10287
10288 /* A helper function for computing the list of all symbol tables
10289 included by PER_CU. */
10290
10291 static void
10292 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10293 htab_t all_children, htab_t all_type_symtabs,
10294 struct dwarf2_per_cu_data *per_cu,
10295 struct compunit_symtab *immediate_parent)
10296 {
10297 void **slot;
10298 int ix;
10299 struct compunit_symtab *cust;
10300 struct dwarf2_per_cu_data *iter;
10301
10302 slot = htab_find_slot (all_children, per_cu, INSERT);
10303 if (*slot != NULL)
10304 {
10305 /* This inclusion and its children have been processed. */
10306 return;
10307 }
10308
10309 *slot = per_cu;
10310 /* Only add a CU if it has a symbol table. */
10311 cust = get_compunit_symtab (per_cu);
10312 if (cust != NULL)
10313 {
10314 /* If this is a type unit only add its symbol table if we haven't
10315 seen it yet (type unit per_cu's can share symtabs). */
10316 if (per_cu->is_debug_types)
10317 {
10318 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10319 if (*slot == NULL)
10320 {
10321 *slot = cust;
10322 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10323 if (cust->user == NULL)
10324 cust->user = immediate_parent;
10325 }
10326 }
10327 else
10328 {
10329 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10330 if (cust->user == NULL)
10331 cust->user = immediate_parent;
10332 }
10333 }
10334
10335 for (ix = 0;
10336 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10337 ++ix)
10338 {
10339 recursively_compute_inclusions (result, all_children,
10340 all_type_symtabs, iter, cust);
10341 }
10342 }
10343
10344 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10345 PER_CU. */
10346
10347 static void
10348 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10349 {
10350 gdb_assert (! per_cu->is_debug_types);
10351
10352 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10353 {
10354 int ix, len;
10355 struct dwarf2_per_cu_data *per_cu_iter;
10356 struct compunit_symtab *compunit_symtab_iter;
10357 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10358 htab_t all_children, all_type_symtabs;
10359 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10360
10361 /* If we don't have a symtab, we can just skip this case. */
10362 if (cust == NULL)
10363 return;
10364
10365 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10366 NULL, xcalloc, xfree);
10367 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10368 NULL, xcalloc, xfree);
10369
10370 for (ix = 0;
10371 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10372 ix, per_cu_iter);
10373 ++ix)
10374 {
10375 recursively_compute_inclusions (&result_symtabs, all_children,
10376 all_type_symtabs, per_cu_iter,
10377 cust);
10378 }
10379
10380 /* Now we have a transitive closure of all the included symtabs. */
10381 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10382 cust->includes
10383 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10384 struct compunit_symtab *, len + 1);
10385 for (ix = 0;
10386 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10387 compunit_symtab_iter);
10388 ++ix)
10389 cust->includes[ix] = compunit_symtab_iter;
10390 cust->includes[len] = NULL;
10391
10392 VEC_free (compunit_symtab_ptr, result_symtabs);
10393 htab_delete (all_children);
10394 htab_delete (all_type_symtabs);
10395 }
10396 }
10397
10398 /* Compute the 'includes' field for the symtabs of all the CUs we just
10399 read. */
10400
10401 static void
10402 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10403 {
10404 int ix;
10405 struct dwarf2_per_cu_data *iter;
10406
10407 for (ix = 0;
10408 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10409 ix, iter);
10410 ++ix)
10411 {
10412 if (! iter->is_debug_types)
10413 compute_compunit_symtab_includes (iter);
10414 }
10415
10416 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10417 }
10418
10419 /* Generate full symbol information for PER_CU, whose DIEs have
10420 already been loaded into memory. */
10421
10422 static void
10423 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10424 enum language pretend_language)
10425 {
10426 struct dwarf2_cu *cu = per_cu->cu;
10427 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10428 struct objfile *objfile = dwarf2_per_objfile->objfile;
10429 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10430 CORE_ADDR lowpc, highpc;
10431 struct compunit_symtab *cust;
10432 CORE_ADDR baseaddr;
10433 struct block *static_block;
10434 CORE_ADDR addr;
10435
10436 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10437
10438 buildsym_init ();
10439 scoped_free_pendings free_pending;
10440
10441 /* Clear the list here in case something was left over. */
10442 cu->method_list.clear ();
10443
10444 cu->list_in_scope = &file_symbols;
10445
10446 cu->language = pretend_language;
10447 cu->language_defn = language_def (cu->language);
10448
10449 /* Do line number decoding in read_file_scope () */
10450 process_die (cu->dies, cu);
10451
10452 /* For now fudge the Go package. */
10453 if (cu->language == language_go)
10454 fixup_go_packaging (cu);
10455
10456 /* Now that we have processed all the DIEs in the CU, all the types
10457 should be complete, and it should now be safe to compute all of the
10458 physnames. */
10459 compute_delayed_physnames (cu);
10460
10461 /* Some compilers don't define a DW_AT_high_pc attribute for the
10462 compilation unit. If the DW_AT_high_pc is missing, synthesize
10463 it, by scanning the DIE's below the compilation unit. */
10464 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10465
10466 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10467 static_block = end_symtab_get_static_block (addr, 0, 1);
10468
10469 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10470 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10471 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10472 addrmap to help ensure it has an accurate map of pc values belonging to
10473 this comp unit. */
10474 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10475
10476 cust = end_symtab_from_static_block (static_block,
10477 SECT_OFF_TEXT (objfile), 0);
10478
10479 if (cust != NULL)
10480 {
10481 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10482
10483 /* Set symtab language to language from DW_AT_language. If the
10484 compilation is from a C file generated by language preprocessors, do
10485 not set the language if it was already deduced by start_subfile. */
10486 if (!(cu->language == language_c
10487 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10488 COMPUNIT_FILETABS (cust)->language = cu->language;
10489
10490 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10491 produce DW_AT_location with location lists but it can be possibly
10492 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10493 there were bugs in prologue debug info, fixed later in GCC-4.5
10494 by "unwind info for epilogues" patch (which is not directly related).
10495
10496 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10497 needed, it would be wrong due to missing DW_AT_producer there.
10498
10499 Still one can confuse GDB by using non-standard GCC compilation
10500 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10501 */
10502 if (cu->has_loclist && gcc_4_minor >= 5)
10503 cust->locations_valid = 1;
10504
10505 if (gcc_4_minor >= 5)
10506 cust->epilogue_unwind_valid = 1;
10507
10508 cust->call_site_htab = cu->call_site_htab;
10509 }
10510
10511 if (dwarf2_per_objfile->using_index)
10512 per_cu->v.quick->compunit_symtab = cust;
10513 else
10514 {
10515 struct partial_symtab *pst = per_cu->v.psymtab;
10516 pst->compunit_symtab = cust;
10517 pst->readin = 1;
10518 }
10519
10520 /* Push it for inclusion processing later. */
10521 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10522 }
10523
10524 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10525 already been loaded into memory. */
10526
10527 static void
10528 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10529 enum language pretend_language)
10530 {
10531 struct dwarf2_cu *cu = per_cu->cu;
10532 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10533 struct objfile *objfile = dwarf2_per_objfile->objfile;
10534 struct compunit_symtab *cust;
10535 struct signatured_type *sig_type;
10536
10537 gdb_assert (per_cu->is_debug_types);
10538 sig_type = (struct signatured_type *) per_cu;
10539
10540 buildsym_init ();
10541 scoped_free_pendings free_pending;
10542
10543 /* Clear the list here in case something was left over. */
10544 cu->method_list.clear ();
10545
10546 cu->list_in_scope = &file_symbols;
10547
10548 cu->language = pretend_language;
10549 cu->language_defn = language_def (cu->language);
10550
10551 /* The symbol tables are set up in read_type_unit_scope. */
10552 process_die (cu->dies, cu);
10553
10554 /* For now fudge the Go package. */
10555 if (cu->language == language_go)
10556 fixup_go_packaging (cu);
10557
10558 /* Now that we have processed all the DIEs in the CU, all the types
10559 should be complete, and it should now be safe to compute all of the
10560 physnames. */
10561 compute_delayed_physnames (cu);
10562
10563 /* TUs share symbol tables.
10564 If this is the first TU to use this symtab, complete the construction
10565 of it with end_expandable_symtab. Otherwise, complete the addition of
10566 this TU's symbols to the existing symtab. */
10567 if (sig_type->type_unit_group->compunit_symtab == NULL)
10568 {
10569 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10570 sig_type->type_unit_group->compunit_symtab = cust;
10571
10572 if (cust != NULL)
10573 {
10574 /* Set symtab language to language from DW_AT_language. If the
10575 compilation is from a C file generated by language preprocessors,
10576 do not set the language if it was already deduced by
10577 start_subfile. */
10578 if (!(cu->language == language_c
10579 && COMPUNIT_FILETABS (cust)->language != language_c))
10580 COMPUNIT_FILETABS (cust)->language = cu->language;
10581 }
10582 }
10583 else
10584 {
10585 augment_type_symtab ();
10586 cust = sig_type->type_unit_group->compunit_symtab;
10587 }
10588
10589 if (dwarf2_per_objfile->using_index)
10590 per_cu->v.quick->compunit_symtab = cust;
10591 else
10592 {
10593 struct partial_symtab *pst = per_cu->v.psymtab;
10594 pst->compunit_symtab = cust;
10595 pst->readin = 1;
10596 }
10597 }
10598
10599 /* Process an imported unit DIE. */
10600
10601 static void
10602 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10603 {
10604 struct attribute *attr;
10605
10606 /* For now we don't handle imported units in type units. */
10607 if (cu->per_cu->is_debug_types)
10608 {
10609 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10610 " supported in type units [in module %s]"),
10611 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10612 }
10613
10614 attr = dwarf2_attr (die, DW_AT_import, cu);
10615 if (attr != NULL)
10616 {
10617 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10618 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10619 dwarf2_per_cu_data *per_cu
10620 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10621 cu->per_cu->dwarf2_per_objfile);
10622
10623 /* If necessary, add it to the queue and load its DIEs. */
10624 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10625 load_full_comp_unit (per_cu, cu->language);
10626
10627 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10628 per_cu);
10629 }
10630 }
10631
10632 /* RAII object that represents a process_die scope: i.e.,
10633 starts/finishes processing a DIE. */
10634 class process_die_scope
10635 {
10636 public:
10637 process_die_scope (die_info *die, dwarf2_cu *cu)
10638 : m_die (die), m_cu (cu)
10639 {
10640 /* We should only be processing DIEs not already in process. */
10641 gdb_assert (!m_die->in_process);
10642 m_die->in_process = true;
10643 }
10644
10645 ~process_die_scope ()
10646 {
10647 m_die->in_process = false;
10648
10649 /* If we're done processing the DIE for the CU that owns the line
10650 header, we don't need the line header anymore. */
10651 if (m_cu->line_header_die_owner == m_die)
10652 {
10653 delete m_cu->line_header;
10654 m_cu->line_header = NULL;
10655 m_cu->line_header_die_owner = NULL;
10656 }
10657 }
10658
10659 private:
10660 die_info *m_die;
10661 dwarf2_cu *m_cu;
10662 };
10663
10664 /* Process a die and its children. */
10665
10666 static void
10667 process_die (struct die_info *die, struct dwarf2_cu *cu)
10668 {
10669 process_die_scope scope (die, cu);
10670
10671 switch (die->tag)
10672 {
10673 case DW_TAG_padding:
10674 break;
10675 case DW_TAG_compile_unit:
10676 case DW_TAG_partial_unit:
10677 read_file_scope (die, cu);
10678 break;
10679 case DW_TAG_type_unit:
10680 read_type_unit_scope (die, cu);
10681 break;
10682 case DW_TAG_subprogram:
10683 case DW_TAG_inlined_subroutine:
10684 read_func_scope (die, cu);
10685 break;
10686 case DW_TAG_lexical_block:
10687 case DW_TAG_try_block:
10688 case DW_TAG_catch_block:
10689 read_lexical_block_scope (die, cu);
10690 break;
10691 case DW_TAG_call_site:
10692 case DW_TAG_GNU_call_site:
10693 read_call_site_scope (die, cu);
10694 break;
10695 case DW_TAG_class_type:
10696 case DW_TAG_interface_type:
10697 case DW_TAG_structure_type:
10698 case DW_TAG_union_type:
10699 process_structure_scope (die, cu);
10700 break;
10701 case DW_TAG_enumeration_type:
10702 process_enumeration_scope (die, cu);
10703 break;
10704
10705 /* These dies have a type, but processing them does not create
10706 a symbol or recurse to process the children. Therefore we can
10707 read them on-demand through read_type_die. */
10708 case DW_TAG_subroutine_type:
10709 case DW_TAG_set_type:
10710 case DW_TAG_array_type:
10711 case DW_TAG_pointer_type:
10712 case DW_TAG_ptr_to_member_type:
10713 case DW_TAG_reference_type:
10714 case DW_TAG_rvalue_reference_type:
10715 case DW_TAG_string_type:
10716 break;
10717
10718 case DW_TAG_base_type:
10719 case DW_TAG_subrange_type:
10720 case DW_TAG_typedef:
10721 /* Add a typedef symbol for the type definition, if it has a
10722 DW_AT_name. */
10723 new_symbol (die, read_type_die (die, cu), cu);
10724 break;
10725 case DW_TAG_common_block:
10726 read_common_block (die, cu);
10727 break;
10728 case DW_TAG_common_inclusion:
10729 break;
10730 case DW_TAG_namespace:
10731 cu->processing_has_namespace_info = 1;
10732 read_namespace (die, cu);
10733 break;
10734 case DW_TAG_module:
10735 cu->processing_has_namespace_info = 1;
10736 read_module (die, cu);
10737 break;
10738 case DW_TAG_imported_declaration:
10739 cu->processing_has_namespace_info = 1;
10740 if (read_namespace_alias (die, cu))
10741 break;
10742 /* The declaration is not a global namespace alias: fall through. */
10743 case DW_TAG_imported_module:
10744 cu->processing_has_namespace_info = 1;
10745 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10746 || cu->language != language_fortran))
10747 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10748 dwarf_tag_name (die->tag));
10749 read_import_statement (die, cu);
10750 break;
10751
10752 case DW_TAG_imported_unit:
10753 process_imported_unit_die (die, cu);
10754 break;
10755
10756 case DW_TAG_variable:
10757 read_variable (die, cu);
10758 break;
10759
10760 default:
10761 new_symbol (die, NULL, cu);
10762 break;
10763 }
10764 }
10765 \f
10766 /* DWARF name computation. */
10767
10768 /* A helper function for dwarf2_compute_name which determines whether DIE
10769 needs to have the name of the scope prepended to the name listed in the
10770 die. */
10771
10772 static int
10773 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10774 {
10775 struct attribute *attr;
10776
10777 switch (die->tag)
10778 {
10779 case DW_TAG_namespace:
10780 case DW_TAG_typedef:
10781 case DW_TAG_class_type:
10782 case DW_TAG_interface_type:
10783 case DW_TAG_structure_type:
10784 case DW_TAG_union_type:
10785 case DW_TAG_enumeration_type:
10786 case DW_TAG_enumerator:
10787 case DW_TAG_subprogram:
10788 case DW_TAG_inlined_subroutine:
10789 case DW_TAG_member:
10790 case DW_TAG_imported_declaration:
10791 return 1;
10792
10793 case DW_TAG_variable:
10794 case DW_TAG_constant:
10795 /* We only need to prefix "globally" visible variables. These include
10796 any variable marked with DW_AT_external or any variable that
10797 lives in a namespace. [Variables in anonymous namespaces
10798 require prefixing, but they are not DW_AT_external.] */
10799
10800 if (dwarf2_attr (die, DW_AT_specification, cu))
10801 {
10802 struct dwarf2_cu *spec_cu = cu;
10803
10804 return die_needs_namespace (die_specification (die, &spec_cu),
10805 spec_cu);
10806 }
10807
10808 attr = dwarf2_attr (die, DW_AT_external, cu);
10809 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10810 && die->parent->tag != DW_TAG_module)
10811 return 0;
10812 /* A variable in a lexical block of some kind does not need a
10813 namespace, even though in C++ such variables may be external
10814 and have a mangled name. */
10815 if (die->parent->tag == DW_TAG_lexical_block
10816 || die->parent->tag == DW_TAG_try_block
10817 || die->parent->tag == DW_TAG_catch_block
10818 || die->parent->tag == DW_TAG_subprogram)
10819 return 0;
10820 return 1;
10821
10822 default:
10823 return 0;
10824 }
10825 }
10826
10827 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10828 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10829 defined for the given DIE. */
10830
10831 static struct attribute *
10832 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10833 {
10834 struct attribute *attr;
10835
10836 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10837 if (attr == NULL)
10838 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10839
10840 return attr;
10841 }
10842
10843 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10844 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10845 defined for the given DIE. */
10846
10847 static const char *
10848 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10849 {
10850 const char *linkage_name;
10851
10852 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10853 if (linkage_name == NULL)
10854 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10855
10856 return linkage_name;
10857 }
10858
10859 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10860 compute the physname for the object, which include a method's:
10861 - formal parameters (C++),
10862 - receiver type (Go),
10863
10864 The term "physname" is a bit confusing.
10865 For C++, for example, it is the demangled name.
10866 For Go, for example, it's the mangled name.
10867
10868 For Ada, return the DIE's linkage name rather than the fully qualified
10869 name. PHYSNAME is ignored..
10870
10871 The result is allocated on the objfile_obstack and canonicalized. */
10872
10873 static const char *
10874 dwarf2_compute_name (const char *name,
10875 struct die_info *die, struct dwarf2_cu *cu,
10876 int physname)
10877 {
10878 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10879
10880 if (name == NULL)
10881 name = dwarf2_name (die, cu);
10882
10883 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10884 but otherwise compute it by typename_concat inside GDB.
10885 FIXME: Actually this is not really true, or at least not always true.
10886 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10887 Fortran names because there is no mangling standard. So new_symbol
10888 will set the demangled name to the result of dwarf2_full_name, and it is
10889 the demangled name that GDB uses if it exists. */
10890 if (cu->language == language_ada
10891 || (cu->language == language_fortran && physname))
10892 {
10893 /* For Ada unit, we prefer the linkage name over the name, as
10894 the former contains the exported name, which the user expects
10895 to be able to reference. Ideally, we want the user to be able
10896 to reference this entity using either natural or linkage name,
10897 but we haven't started looking at this enhancement yet. */
10898 const char *linkage_name = dw2_linkage_name (die, cu);
10899
10900 if (linkage_name != NULL)
10901 return linkage_name;
10902 }
10903
10904 /* These are the only languages we know how to qualify names in. */
10905 if (name != NULL
10906 && (cu->language == language_cplus
10907 || cu->language == language_fortran || cu->language == language_d
10908 || cu->language == language_rust))
10909 {
10910 if (die_needs_namespace (die, cu))
10911 {
10912 const char *prefix;
10913 const char *canonical_name = NULL;
10914
10915 string_file buf;
10916
10917 prefix = determine_prefix (die, cu);
10918 if (*prefix != '\0')
10919 {
10920 char *prefixed_name = typename_concat (NULL, prefix, name,
10921 physname, cu);
10922
10923 buf.puts (prefixed_name);
10924 xfree (prefixed_name);
10925 }
10926 else
10927 buf.puts (name);
10928
10929 /* Template parameters may be specified in the DIE's DW_AT_name, or
10930 as children with DW_TAG_template_type_param or
10931 DW_TAG_value_type_param. If the latter, add them to the name
10932 here. If the name already has template parameters, then
10933 skip this step; some versions of GCC emit both, and
10934 it is more efficient to use the pre-computed name.
10935
10936 Something to keep in mind about this process: it is very
10937 unlikely, or in some cases downright impossible, to produce
10938 something that will match the mangled name of a function.
10939 If the definition of the function has the same debug info,
10940 we should be able to match up with it anyway. But fallbacks
10941 using the minimal symbol, for instance to find a method
10942 implemented in a stripped copy of libstdc++, will not work.
10943 If we do not have debug info for the definition, we will have to
10944 match them up some other way.
10945
10946 When we do name matching there is a related problem with function
10947 templates; two instantiated function templates are allowed to
10948 differ only by their return types, which we do not add here. */
10949
10950 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10951 {
10952 struct attribute *attr;
10953 struct die_info *child;
10954 int first = 1;
10955
10956 die->building_fullname = 1;
10957
10958 for (child = die->child; child != NULL; child = child->sibling)
10959 {
10960 struct type *type;
10961 LONGEST value;
10962 const gdb_byte *bytes;
10963 struct dwarf2_locexpr_baton *baton;
10964 struct value *v;
10965
10966 if (child->tag != DW_TAG_template_type_param
10967 && child->tag != DW_TAG_template_value_param)
10968 continue;
10969
10970 if (first)
10971 {
10972 buf.puts ("<");
10973 first = 0;
10974 }
10975 else
10976 buf.puts (", ");
10977
10978 attr = dwarf2_attr (child, DW_AT_type, cu);
10979 if (attr == NULL)
10980 {
10981 complaint (&symfile_complaints,
10982 _("template parameter missing DW_AT_type"));
10983 buf.puts ("UNKNOWN_TYPE");
10984 continue;
10985 }
10986 type = die_type (child, cu);
10987
10988 if (child->tag == DW_TAG_template_type_param)
10989 {
10990 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
10991 continue;
10992 }
10993
10994 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10995 if (attr == NULL)
10996 {
10997 complaint (&symfile_complaints,
10998 _("template parameter missing "
10999 "DW_AT_const_value"));
11000 buf.puts ("UNKNOWN_VALUE");
11001 continue;
11002 }
11003
11004 dwarf2_const_value_attr (attr, type, name,
11005 &cu->comp_unit_obstack, cu,
11006 &value, &bytes, &baton);
11007
11008 if (TYPE_NOSIGN (type))
11009 /* GDB prints characters as NUMBER 'CHAR'. If that's
11010 changed, this can use value_print instead. */
11011 c_printchar (value, type, &buf);
11012 else
11013 {
11014 struct value_print_options opts;
11015
11016 if (baton != NULL)
11017 v = dwarf2_evaluate_loc_desc (type, NULL,
11018 baton->data,
11019 baton->size,
11020 baton->per_cu);
11021 else if (bytes != NULL)
11022 {
11023 v = allocate_value (type);
11024 memcpy (value_contents_writeable (v), bytes,
11025 TYPE_LENGTH (type));
11026 }
11027 else
11028 v = value_from_longest (type, value);
11029
11030 /* Specify decimal so that we do not depend on
11031 the radix. */
11032 get_formatted_print_options (&opts, 'd');
11033 opts.raw = 1;
11034 value_print (v, &buf, &opts);
11035 release_value (v);
11036 value_free (v);
11037 }
11038 }
11039
11040 die->building_fullname = 0;
11041
11042 if (!first)
11043 {
11044 /* Close the argument list, with a space if necessary
11045 (nested templates). */
11046 if (!buf.empty () && buf.string ().back () == '>')
11047 buf.puts (" >");
11048 else
11049 buf.puts (">");
11050 }
11051 }
11052
11053 /* For C++ methods, append formal parameter type
11054 information, if PHYSNAME. */
11055
11056 if (physname && die->tag == DW_TAG_subprogram
11057 && cu->language == language_cplus)
11058 {
11059 struct type *type = read_type_die (die, cu);
11060
11061 c_type_print_args (type, &buf, 1, cu->language,
11062 &type_print_raw_options);
11063
11064 if (cu->language == language_cplus)
11065 {
11066 /* Assume that an artificial first parameter is
11067 "this", but do not crash if it is not. RealView
11068 marks unnamed (and thus unused) parameters as
11069 artificial; there is no way to differentiate
11070 the two cases. */
11071 if (TYPE_NFIELDS (type) > 0
11072 && TYPE_FIELD_ARTIFICIAL (type, 0)
11073 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11074 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11075 0))))
11076 buf.puts (" const");
11077 }
11078 }
11079
11080 const std::string &intermediate_name = buf.string ();
11081
11082 if (cu->language == language_cplus)
11083 canonical_name
11084 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11085 &objfile->per_bfd->storage_obstack);
11086
11087 /* If we only computed INTERMEDIATE_NAME, or if
11088 INTERMEDIATE_NAME is already canonical, then we need to
11089 copy it to the appropriate obstack. */
11090 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11091 name = ((const char *)
11092 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11093 intermediate_name.c_str (),
11094 intermediate_name.length ()));
11095 else
11096 name = canonical_name;
11097 }
11098 }
11099
11100 return name;
11101 }
11102
11103 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11104 If scope qualifiers are appropriate they will be added. The result
11105 will be allocated on the storage_obstack, or NULL if the DIE does
11106 not have a name. NAME may either be from a previous call to
11107 dwarf2_name or NULL.
11108
11109 The output string will be canonicalized (if C++). */
11110
11111 static const char *
11112 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11113 {
11114 return dwarf2_compute_name (name, die, cu, 0);
11115 }
11116
11117 /* Construct a physname for the given DIE in CU. NAME may either be
11118 from a previous call to dwarf2_name or NULL. The result will be
11119 allocated on the objfile_objstack or NULL if the DIE does not have a
11120 name.
11121
11122 The output string will be canonicalized (if C++). */
11123
11124 static const char *
11125 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11126 {
11127 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11128 const char *retval, *mangled = NULL, *canon = NULL;
11129 int need_copy = 1;
11130
11131 /* In this case dwarf2_compute_name is just a shortcut not building anything
11132 on its own. */
11133 if (!die_needs_namespace (die, cu))
11134 return dwarf2_compute_name (name, die, cu, 1);
11135
11136 mangled = dw2_linkage_name (die, cu);
11137
11138 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11139 See https://github.com/rust-lang/rust/issues/32925. */
11140 if (cu->language == language_rust && mangled != NULL
11141 && strchr (mangled, '{') != NULL)
11142 mangled = NULL;
11143
11144 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11145 has computed. */
11146 gdb::unique_xmalloc_ptr<char> demangled;
11147 if (mangled != NULL)
11148 {
11149
11150 if (cu->language == language_go)
11151 {
11152 /* This is a lie, but we already lie to the caller new_symbol.
11153 new_symbol assumes we return the mangled name.
11154 This just undoes that lie until things are cleaned up. */
11155 }
11156 else
11157 {
11158 /* Use DMGL_RET_DROP for C++ template functions to suppress
11159 their return type. It is easier for GDB users to search
11160 for such functions as `name(params)' than `long name(params)'.
11161 In such case the minimal symbol names do not match the full
11162 symbol names but for template functions there is never a need
11163 to look up their definition from their declaration so
11164 the only disadvantage remains the minimal symbol variant
11165 `long name(params)' does not have the proper inferior type. */
11166 demangled.reset (gdb_demangle (mangled,
11167 (DMGL_PARAMS | DMGL_ANSI
11168 | DMGL_RET_DROP)));
11169 }
11170 if (demangled)
11171 canon = demangled.get ();
11172 else
11173 {
11174 canon = mangled;
11175 need_copy = 0;
11176 }
11177 }
11178
11179 if (canon == NULL || check_physname)
11180 {
11181 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11182
11183 if (canon != NULL && strcmp (physname, canon) != 0)
11184 {
11185 /* It may not mean a bug in GDB. The compiler could also
11186 compute DW_AT_linkage_name incorrectly. But in such case
11187 GDB would need to be bug-to-bug compatible. */
11188
11189 complaint (&symfile_complaints,
11190 _("Computed physname <%s> does not match demangled <%s> "
11191 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
11192 physname, canon, mangled, to_underlying (die->sect_off),
11193 objfile_name (objfile));
11194
11195 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11196 is available here - over computed PHYSNAME. It is safer
11197 against both buggy GDB and buggy compilers. */
11198
11199 retval = canon;
11200 }
11201 else
11202 {
11203 retval = physname;
11204 need_copy = 0;
11205 }
11206 }
11207 else
11208 retval = canon;
11209
11210 if (need_copy)
11211 retval = ((const char *)
11212 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11213 retval, strlen (retval)));
11214
11215 return retval;
11216 }
11217
11218 /* Inspect DIE in CU for a namespace alias. If one exists, record
11219 a new symbol for it.
11220
11221 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11222
11223 static int
11224 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11225 {
11226 struct attribute *attr;
11227
11228 /* If the die does not have a name, this is not a namespace
11229 alias. */
11230 attr = dwarf2_attr (die, DW_AT_name, cu);
11231 if (attr != NULL)
11232 {
11233 int num;
11234 struct die_info *d = die;
11235 struct dwarf2_cu *imported_cu = cu;
11236
11237 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11238 keep inspecting DIEs until we hit the underlying import. */
11239 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11240 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11241 {
11242 attr = dwarf2_attr (d, DW_AT_import, cu);
11243 if (attr == NULL)
11244 break;
11245
11246 d = follow_die_ref (d, attr, &imported_cu);
11247 if (d->tag != DW_TAG_imported_declaration)
11248 break;
11249 }
11250
11251 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11252 {
11253 complaint (&symfile_complaints,
11254 _("DIE at 0x%x has too many recursively imported "
11255 "declarations"), to_underlying (d->sect_off));
11256 return 0;
11257 }
11258
11259 if (attr != NULL)
11260 {
11261 struct type *type;
11262 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11263
11264 type = get_die_type_at_offset (sect_off, cu->per_cu);
11265 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11266 {
11267 /* This declaration is a global namespace alias. Add
11268 a symbol for it whose type is the aliased namespace. */
11269 new_symbol (die, type, cu);
11270 return 1;
11271 }
11272 }
11273 }
11274
11275 return 0;
11276 }
11277
11278 /* Return the using directives repository (global or local?) to use in the
11279 current context for LANGUAGE.
11280
11281 For Ada, imported declarations can materialize renamings, which *may* be
11282 global. However it is impossible (for now?) in DWARF to distinguish
11283 "external" imported declarations and "static" ones. As all imported
11284 declarations seem to be static in all other languages, make them all CU-wide
11285 global only in Ada. */
11286
11287 static struct using_direct **
11288 using_directives (enum language language)
11289 {
11290 if (language == language_ada && context_stack_depth == 0)
11291 return &global_using_directives;
11292 else
11293 return &local_using_directives;
11294 }
11295
11296 /* Read the import statement specified by the given die and record it. */
11297
11298 static void
11299 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11300 {
11301 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11302 struct attribute *import_attr;
11303 struct die_info *imported_die, *child_die;
11304 struct dwarf2_cu *imported_cu;
11305 const char *imported_name;
11306 const char *imported_name_prefix;
11307 const char *canonical_name;
11308 const char *import_alias;
11309 const char *imported_declaration = NULL;
11310 const char *import_prefix;
11311 std::vector<const char *> excludes;
11312
11313 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11314 if (import_attr == NULL)
11315 {
11316 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11317 dwarf_tag_name (die->tag));
11318 return;
11319 }
11320
11321 imported_cu = cu;
11322 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11323 imported_name = dwarf2_name (imported_die, imported_cu);
11324 if (imported_name == NULL)
11325 {
11326 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11327
11328 The import in the following code:
11329 namespace A
11330 {
11331 typedef int B;
11332 }
11333
11334 int main ()
11335 {
11336 using A::B;
11337 B b;
11338 return b;
11339 }
11340
11341 ...
11342 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11343 <52> DW_AT_decl_file : 1
11344 <53> DW_AT_decl_line : 6
11345 <54> DW_AT_import : <0x75>
11346 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11347 <59> DW_AT_name : B
11348 <5b> DW_AT_decl_file : 1
11349 <5c> DW_AT_decl_line : 2
11350 <5d> DW_AT_type : <0x6e>
11351 ...
11352 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11353 <76> DW_AT_byte_size : 4
11354 <77> DW_AT_encoding : 5 (signed)
11355
11356 imports the wrong die ( 0x75 instead of 0x58 ).
11357 This case will be ignored until the gcc bug is fixed. */
11358 return;
11359 }
11360
11361 /* Figure out the local name after import. */
11362 import_alias = dwarf2_name (die, cu);
11363
11364 /* Figure out where the statement is being imported to. */
11365 import_prefix = determine_prefix (die, cu);
11366
11367 /* Figure out what the scope of the imported die is and prepend it
11368 to the name of the imported die. */
11369 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11370
11371 if (imported_die->tag != DW_TAG_namespace
11372 && imported_die->tag != DW_TAG_module)
11373 {
11374 imported_declaration = imported_name;
11375 canonical_name = imported_name_prefix;
11376 }
11377 else if (strlen (imported_name_prefix) > 0)
11378 canonical_name = obconcat (&objfile->objfile_obstack,
11379 imported_name_prefix,
11380 (cu->language == language_d ? "." : "::"),
11381 imported_name, (char *) NULL);
11382 else
11383 canonical_name = imported_name;
11384
11385 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11386 for (child_die = die->child; child_die && child_die->tag;
11387 child_die = sibling_die (child_die))
11388 {
11389 /* DWARF-4: A Fortran use statement with a “rename list” may be
11390 represented by an imported module entry with an import attribute
11391 referring to the module and owned entries corresponding to those
11392 entities that are renamed as part of being imported. */
11393
11394 if (child_die->tag != DW_TAG_imported_declaration)
11395 {
11396 complaint (&symfile_complaints,
11397 _("child DW_TAG_imported_declaration expected "
11398 "- DIE at 0x%x [in module %s]"),
11399 to_underlying (child_die->sect_off), objfile_name (objfile));
11400 continue;
11401 }
11402
11403 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11404 if (import_attr == NULL)
11405 {
11406 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11407 dwarf_tag_name (child_die->tag));
11408 continue;
11409 }
11410
11411 imported_cu = cu;
11412 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11413 &imported_cu);
11414 imported_name = dwarf2_name (imported_die, imported_cu);
11415 if (imported_name == NULL)
11416 {
11417 complaint (&symfile_complaints,
11418 _("child DW_TAG_imported_declaration has unknown "
11419 "imported name - DIE at 0x%x [in module %s]"),
11420 to_underlying (child_die->sect_off), objfile_name (objfile));
11421 continue;
11422 }
11423
11424 excludes.push_back (imported_name);
11425
11426 process_die (child_die, cu);
11427 }
11428
11429 add_using_directive (using_directives (cu->language),
11430 import_prefix,
11431 canonical_name,
11432 import_alias,
11433 imported_declaration,
11434 excludes,
11435 0,
11436 &objfile->objfile_obstack);
11437 }
11438
11439 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11440 types, but gives them a size of zero. Starting with version 14,
11441 ICC is compatible with GCC. */
11442
11443 static int
11444 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11445 {
11446 if (!cu->checked_producer)
11447 check_producer (cu);
11448
11449 return cu->producer_is_icc_lt_14;
11450 }
11451
11452 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11453 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11454 this, it was first present in GCC release 4.3.0. */
11455
11456 static int
11457 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11458 {
11459 if (!cu->checked_producer)
11460 check_producer (cu);
11461
11462 return cu->producer_is_gcc_lt_4_3;
11463 }
11464
11465 static file_and_directory
11466 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11467 {
11468 file_and_directory res;
11469
11470 /* Find the filename. Do not use dwarf2_name here, since the filename
11471 is not a source language identifier. */
11472 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11473 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11474
11475 if (res.comp_dir == NULL
11476 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11477 && IS_ABSOLUTE_PATH (res.name))
11478 {
11479 res.comp_dir_storage = ldirname (res.name);
11480 if (!res.comp_dir_storage.empty ())
11481 res.comp_dir = res.comp_dir_storage.c_str ();
11482 }
11483 if (res.comp_dir != NULL)
11484 {
11485 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11486 directory, get rid of it. */
11487 const char *cp = strchr (res.comp_dir, ':');
11488
11489 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11490 res.comp_dir = cp + 1;
11491 }
11492
11493 if (res.name == NULL)
11494 res.name = "<unknown>";
11495
11496 return res;
11497 }
11498
11499 /* Handle DW_AT_stmt_list for a compilation unit.
11500 DIE is the DW_TAG_compile_unit die for CU.
11501 COMP_DIR is the compilation directory. LOWPC is passed to
11502 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11503
11504 static void
11505 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11506 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11507 {
11508 struct dwarf2_per_objfile *dwarf2_per_objfile
11509 = cu->per_cu->dwarf2_per_objfile;
11510 struct objfile *objfile = dwarf2_per_objfile->objfile;
11511 struct attribute *attr;
11512 struct line_header line_header_local;
11513 hashval_t line_header_local_hash;
11514 void **slot;
11515 int decode_mapping;
11516
11517 gdb_assert (! cu->per_cu->is_debug_types);
11518
11519 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11520 if (attr == NULL)
11521 return;
11522
11523 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11524
11525 /* The line header hash table is only created if needed (it exists to
11526 prevent redundant reading of the line table for partial_units).
11527 If we're given a partial_unit, we'll need it. If we're given a
11528 compile_unit, then use the line header hash table if it's already
11529 created, but don't create one just yet. */
11530
11531 if (dwarf2_per_objfile->line_header_hash == NULL
11532 && die->tag == DW_TAG_partial_unit)
11533 {
11534 dwarf2_per_objfile->line_header_hash
11535 = htab_create_alloc_ex (127, line_header_hash_voidp,
11536 line_header_eq_voidp,
11537 free_line_header_voidp,
11538 &objfile->objfile_obstack,
11539 hashtab_obstack_allocate,
11540 dummy_obstack_deallocate);
11541 }
11542
11543 line_header_local.sect_off = line_offset;
11544 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11545 line_header_local_hash = line_header_hash (&line_header_local);
11546 if (dwarf2_per_objfile->line_header_hash != NULL)
11547 {
11548 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11549 &line_header_local,
11550 line_header_local_hash, NO_INSERT);
11551
11552 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11553 is not present in *SLOT (since if there is something in *SLOT then
11554 it will be for a partial_unit). */
11555 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11556 {
11557 gdb_assert (*slot != NULL);
11558 cu->line_header = (struct line_header *) *slot;
11559 return;
11560 }
11561 }
11562
11563 /* dwarf_decode_line_header does not yet provide sufficient information.
11564 We always have to call also dwarf_decode_lines for it. */
11565 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11566 if (lh == NULL)
11567 return;
11568
11569 cu->line_header = lh.release ();
11570 cu->line_header_die_owner = die;
11571
11572 if (dwarf2_per_objfile->line_header_hash == NULL)
11573 slot = NULL;
11574 else
11575 {
11576 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11577 &line_header_local,
11578 line_header_local_hash, INSERT);
11579 gdb_assert (slot != NULL);
11580 }
11581 if (slot != NULL && *slot == NULL)
11582 {
11583 /* This newly decoded line number information unit will be owned
11584 by line_header_hash hash table. */
11585 *slot = cu->line_header;
11586 cu->line_header_die_owner = NULL;
11587 }
11588 else
11589 {
11590 /* We cannot free any current entry in (*slot) as that struct line_header
11591 may be already used by multiple CUs. Create only temporary decoded
11592 line_header for this CU - it may happen at most once for each line
11593 number information unit. And if we're not using line_header_hash
11594 then this is what we want as well. */
11595 gdb_assert (die->tag != DW_TAG_partial_unit);
11596 }
11597 decode_mapping = (die->tag != DW_TAG_partial_unit);
11598 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11599 decode_mapping);
11600
11601 }
11602
11603 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11604
11605 static void
11606 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11607 {
11608 struct dwarf2_per_objfile *dwarf2_per_objfile
11609 = cu->per_cu->dwarf2_per_objfile;
11610 struct objfile *objfile = dwarf2_per_objfile->objfile;
11611 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11612 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11613 CORE_ADDR highpc = ((CORE_ADDR) 0);
11614 struct attribute *attr;
11615 struct die_info *child_die;
11616 CORE_ADDR baseaddr;
11617
11618 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11619
11620 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11621
11622 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11623 from finish_block. */
11624 if (lowpc == ((CORE_ADDR) -1))
11625 lowpc = highpc;
11626 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11627
11628 file_and_directory fnd = find_file_and_directory (die, cu);
11629
11630 prepare_one_comp_unit (cu, die, cu->language);
11631
11632 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11633 standardised yet. As a workaround for the language detection we fall
11634 back to the DW_AT_producer string. */
11635 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11636 cu->language = language_opencl;
11637
11638 /* Similar hack for Go. */
11639 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11640 set_cu_language (DW_LANG_Go, cu);
11641
11642 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11643
11644 /* Decode line number information if present. We do this before
11645 processing child DIEs, so that the line header table is available
11646 for DW_AT_decl_file. */
11647 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11648
11649 /* Process all dies in compilation unit. */
11650 if (die->child != NULL)
11651 {
11652 child_die = die->child;
11653 while (child_die && child_die->tag)
11654 {
11655 process_die (child_die, cu);
11656 child_die = sibling_die (child_die);
11657 }
11658 }
11659
11660 /* Decode macro information, if present. Dwarf 2 macro information
11661 refers to information in the line number info statement program
11662 header, so we can only read it if we've read the header
11663 successfully. */
11664 attr = dwarf2_attr (die, DW_AT_macros, cu);
11665 if (attr == NULL)
11666 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11667 if (attr && cu->line_header)
11668 {
11669 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11670 complaint (&symfile_complaints,
11671 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11672
11673 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11674 }
11675 else
11676 {
11677 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11678 if (attr && cu->line_header)
11679 {
11680 unsigned int macro_offset = DW_UNSND (attr);
11681
11682 dwarf_decode_macros (cu, macro_offset, 0);
11683 }
11684 }
11685 }
11686
11687 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11688 Create the set of symtabs used by this TU, or if this TU is sharing
11689 symtabs with another TU and the symtabs have already been created
11690 then restore those symtabs in the line header.
11691 We don't need the pc/line-number mapping for type units. */
11692
11693 static void
11694 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
11695 {
11696 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11697 struct type_unit_group *tu_group;
11698 int first_time;
11699 struct attribute *attr;
11700 unsigned int i;
11701 struct signatured_type *sig_type;
11702
11703 gdb_assert (per_cu->is_debug_types);
11704 sig_type = (struct signatured_type *) per_cu;
11705
11706 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11707
11708 /* If we're using .gdb_index (includes -readnow) then
11709 per_cu->type_unit_group may not have been set up yet. */
11710 if (sig_type->type_unit_group == NULL)
11711 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11712 tu_group = sig_type->type_unit_group;
11713
11714 /* If we've already processed this stmt_list there's no real need to
11715 do it again, we could fake it and just recreate the part we need
11716 (file name,index -> symtab mapping). If data shows this optimization
11717 is useful we can do it then. */
11718 first_time = tu_group->compunit_symtab == NULL;
11719
11720 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11721 debug info. */
11722 line_header_up lh;
11723 if (attr != NULL)
11724 {
11725 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11726 lh = dwarf_decode_line_header (line_offset, cu);
11727 }
11728 if (lh == NULL)
11729 {
11730 if (first_time)
11731 dwarf2_start_symtab (cu, "", NULL, 0);
11732 else
11733 {
11734 gdb_assert (tu_group->symtabs == NULL);
11735 restart_symtab (tu_group->compunit_symtab, "", 0);
11736 }
11737 return;
11738 }
11739
11740 cu->line_header = lh.release ();
11741 cu->line_header_die_owner = die;
11742
11743 if (first_time)
11744 {
11745 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
11746
11747 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11748 still initializing it, and our caller (a few levels up)
11749 process_full_type_unit still needs to know if this is the first
11750 time. */
11751
11752 tu_group->num_symtabs = cu->line_header->file_names.size ();
11753 tu_group->symtabs = XNEWVEC (struct symtab *,
11754 cu->line_header->file_names.size ());
11755
11756 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11757 {
11758 file_entry &fe = cu->line_header->file_names[i];
11759
11760 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
11761
11762 if (current_subfile->symtab == NULL)
11763 {
11764 /* NOTE: start_subfile will recognize when it's been
11765 passed a file it has already seen. So we can't
11766 assume there's a simple mapping from
11767 cu->line_header->file_names to subfiles, plus
11768 cu->line_header->file_names may contain dups. */
11769 current_subfile->symtab
11770 = allocate_symtab (cust, current_subfile->name);
11771 }
11772
11773 fe.symtab = current_subfile->symtab;
11774 tu_group->symtabs[i] = fe.symtab;
11775 }
11776 }
11777 else
11778 {
11779 restart_symtab (tu_group->compunit_symtab, "", 0);
11780
11781 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11782 {
11783 file_entry &fe = cu->line_header->file_names[i];
11784
11785 fe.symtab = tu_group->symtabs[i];
11786 }
11787 }
11788
11789 /* The main symtab is allocated last. Type units don't have DW_AT_name
11790 so they don't have a "real" (so to speak) symtab anyway.
11791 There is later code that will assign the main symtab to all symbols
11792 that don't have one. We need to handle the case of a symbol with a
11793 missing symtab (DW_AT_decl_file) anyway. */
11794 }
11795
11796 /* Process DW_TAG_type_unit.
11797 For TUs we want to skip the first top level sibling if it's not the
11798 actual type being defined by this TU. In this case the first top
11799 level sibling is there to provide context only. */
11800
11801 static void
11802 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11803 {
11804 struct die_info *child_die;
11805
11806 prepare_one_comp_unit (cu, die, language_minimal);
11807
11808 /* Initialize (or reinitialize) the machinery for building symtabs.
11809 We do this before processing child DIEs, so that the line header table
11810 is available for DW_AT_decl_file. */
11811 setup_type_unit_groups (die, cu);
11812
11813 if (die->child != NULL)
11814 {
11815 child_die = die->child;
11816 while (child_die && child_die->tag)
11817 {
11818 process_die (child_die, cu);
11819 child_die = sibling_die (child_die);
11820 }
11821 }
11822 }
11823 \f
11824 /* DWO/DWP files.
11825
11826 http://gcc.gnu.org/wiki/DebugFission
11827 http://gcc.gnu.org/wiki/DebugFissionDWP
11828
11829 To simplify handling of both DWO files ("object" files with the DWARF info)
11830 and DWP files (a file with the DWOs packaged up into one file), we treat
11831 DWP files as having a collection of virtual DWO files. */
11832
11833 static hashval_t
11834 hash_dwo_file (const void *item)
11835 {
11836 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11837 hashval_t hash;
11838
11839 hash = htab_hash_string (dwo_file->dwo_name);
11840 if (dwo_file->comp_dir != NULL)
11841 hash += htab_hash_string (dwo_file->comp_dir);
11842 return hash;
11843 }
11844
11845 static int
11846 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11847 {
11848 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11849 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11850
11851 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11852 return 0;
11853 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11854 return lhs->comp_dir == rhs->comp_dir;
11855 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11856 }
11857
11858 /* Allocate a hash table for DWO files. */
11859
11860 static htab_t
11861 allocate_dwo_file_hash_table (struct objfile *objfile)
11862 {
11863 return htab_create_alloc_ex (41,
11864 hash_dwo_file,
11865 eq_dwo_file,
11866 NULL,
11867 &objfile->objfile_obstack,
11868 hashtab_obstack_allocate,
11869 dummy_obstack_deallocate);
11870 }
11871
11872 /* Lookup DWO file DWO_NAME. */
11873
11874 static void **
11875 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11876 const char *dwo_name,
11877 const char *comp_dir)
11878 {
11879 struct dwo_file find_entry;
11880 void **slot;
11881
11882 if (dwarf2_per_objfile->dwo_files == NULL)
11883 dwarf2_per_objfile->dwo_files
11884 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11885
11886 memset (&find_entry, 0, sizeof (find_entry));
11887 find_entry.dwo_name = dwo_name;
11888 find_entry.comp_dir = comp_dir;
11889 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11890
11891 return slot;
11892 }
11893
11894 static hashval_t
11895 hash_dwo_unit (const void *item)
11896 {
11897 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11898
11899 /* This drops the top 32 bits of the id, but is ok for a hash. */
11900 return dwo_unit->signature;
11901 }
11902
11903 static int
11904 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11905 {
11906 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11907 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11908
11909 /* The signature is assumed to be unique within the DWO file.
11910 So while object file CU dwo_id's always have the value zero,
11911 that's OK, assuming each object file DWO file has only one CU,
11912 and that's the rule for now. */
11913 return lhs->signature == rhs->signature;
11914 }
11915
11916 /* Allocate a hash table for DWO CUs,TUs.
11917 There is one of these tables for each of CUs,TUs for each DWO file. */
11918
11919 static htab_t
11920 allocate_dwo_unit_table (struct objfile *objfile)
11921 {
11922 /* Start out with a pretty small number.
11923 Generally DWO files contain only one CU and maybe some TUs. */
11924 return htab_create_alloc_ex (3,
11925 hash_dwo_unit,
11926 eq_dwo_unit,
11927 NULL,
11928 &objfile->objfile_obstack,
11929 hashtab_obstack_allocate,
11930 dummy_obstack_deallocate);
11931 }
11932
11933 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11934
11935 struct create_dwo_cu_data
11936 {
11937 struct dwo_file *dwo_file;
11938 struct dwo_unit dwo_unit;
11939 };
11940
11941 /* die_reader_func for create_dwo_cu. */
11942
11943 static void
11944 create_dwo_cu_reader (const struct die_reader_specs *reader,
11945 const gdb_byte *info_ptr,
11946 struct die_info *comp_unit_die,
11947 int has_children,
11948 void *datap)
11949 {
11950 struct dwarf2_cu *cu = reader->cu;
11951 sect_offset sect_off = cu->per_cu->sect_off;
11952 struct dwarf2_section_info *section = cu->per_cu->section;
11953 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11954 struct dwo_file *dwo_file = data->dwo_file;
11955 struct dwo_unit *dwo_unit = &data->dwo_unit;
11956 struct attribute *attr;
11957
11958 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11959 if (attr == NULL)
11960 {
11961 complaint (&symfile_complaints,
11962 _("Dwarf Error: debug entry at offset 0x%x is missing"
11963 " its dwo_id [in module %s]"),
11964 to_underlying (sect_off), dwo_file->dwo_name);
11965 return;
11966 }
11967
11968 dwo_unit->dwo_file = dwo_file;
11969 dwo_unit->signature = DW_UNSND (attr);
11970 dwo_unit->section = section;
11971 dwo_unit->sect_off = sect_off;
11972 dwo_unit->length = cu->per_cu->length;
11973
11974 if (dwarf_read_debug)
11975 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
11976 to_underlying (sect_off),
11977 hex_string (dwo_unit->signature));
11978 }
11979
11980 /* Create the dwo_units for the CUs in a DWO_FILE.
11981 Note: This function processes DWO files only, not DWP files. */
11982
11983 static void
11984 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11985 struct dwo_file &dwo_file, dwarf2_section_info &section,
11986 htab_t &cus_htab)
11987 {
11988 struct objfile *objfile = dwarf2_per_objfile->objfile;
11989 const gdb_byte *info_ptr, *end_ptr;
11990
11991 dwarf2_read_section (objfile, &section);
11992 info_ptr = section.buffer;
11993
11994 if (info_ptr == NULL)
11995 return;
11996
11997 if (dwarf_read_debug)
11998 {
11999 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
12000 get_section_name (&section),
12001 get_section_file_name (&section));
12002 }
12003
12004 end_ptr = info_ptr + section.size;
12005 while (info_ptr < end_ptr)
12006 {
12007 struct dwarf2_per_cu_data per_cu;
12008 struct create_dwo_cu_data create_dwo_cu_data;
12009 struct dwo_unit *dwo_unit;
12010 void **slot;
12011 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
12012
12013 memset (&create_dwo_cu_data.dwo_unit, 0,
12014 sizeof (create_dwo_cu_data.dwo_unit));
12015 memset (&per_cu, 0, sizeof (per_cu));
12016 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
12017 per_cu.is_debug_types = 0;
12018 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12019 per_cu.section = &section;
12020 create_dwo_cu_data.dwo_file = &dwo_file;
12021
12022 init_cutu_and_read_dies_no_follow (
12023 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12024 info_ptr += per_cu.length;
12025
12026 // If the unit could not be parsed, skip it.
12027 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12028 continue;
12029
12030 if (cus_htab == NULL)
12031 cus_htab = allocate_dwo_unit_table (objfile);
12032
12033 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12034 *dwo_unit = create_dwo_cu_data.dwo_unit;
12035 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12036 gdb_assert (slot != NULL);
12037 if (*slot != NULL)
12038 {
12039 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12040 sect_offset dup_sect_off = dup_cu->sect_off;
12041
12042 complaint (&symfile_complaints,
12043 _("debug cu entry at offset 0x%x is duplicate to"
12044 " the entry at offset 0x%x, signature %s"),
12045 to_underlying (sect_off), to_underlying (dup_sect_off),
12046 hex_string (dwo_unit->signature));
12047 }
12048 *slot = (void *)dwo_unit;
12049 }
12050 }
12051
12052 /* DWP file .debug_{cu,tu}_index section format:
12053 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12054
12055 DWP Version 1:
12056
12057 Both index sections have the same format, and serve to map a 64-bit
12058 signature to a set of section numbers. Each section begins with a header,
12059 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12060 indexes, and a pool of 32-bit section numbers. The index sections will be
12061 aligned at 8-byte boundaries in the file.
12062
12063 The index section header consists of:
12064
12065 V, 32 bit version number
12066 -, 32 bits unused
12067 N, 32 bit number of compilation units or type units in the index
12068 M, 32 bit number of slots in the hash table
12069
12070 Numbers are recorded using the byte order of the application binary.
12071
12072 The hash table begins at offset 16 in the section, and consists of an array
12073 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12074 order of the application binary). Unused slots in the hash table are 0.
12075 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12076
12077 The parallel table begins immediately after the hash table
12078 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12079 array of 32-bit indexes (using the byte order of the application binary),
12080 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12081 table contains a 32-bit index into the pool of section numbers. For unused
12082 hash table slots, the corresponding entry in the parallel table will be 0.
12083
12084 The pool of section numbers begins immediately following the hash table
12085 (at offset 16 + 12 * M from the beginning of the section). The pool of
12086 section numbers consists of an array of 32-bit words (using the byte order
12087 of the application binary). Each item in the array is indexed starting
12088 from 0. The hash table entry provides the index of the first section
12089 number in the set. Additional section numbers in the set follow, and the
12090 set is terminated by a 0 entry (section number 0 is not used in ELF).
12091
12092 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12093 section must be the first entry in the set, and the .debug_abbrev.dwo must
12094 be the second entry. Other members of the set may follow in any order.
12095
12096 ---
12097
12098 DWP Version 2:
12099
12100 DWP Version 2 combines all the .debug_info, etc. sections into one,
12101 and the entries in the index tables are now offsets into these sections.
12102 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12103 section.
12104
12105 Index Section Contents:
12106 Header
12107 Hash Table of Signatures dwp_hash_table.hash_table
12108 Parallel Table of Indices dwp_hash_table.unit_table
12109 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12110 Table of Section Sizes dwp_hash_table.v2.sizes
12111
12112 The index section header consists of:
12113
12114 V, 32 bit version number
12115 L, 32 bit number of columns in the table of section offsets
12116 N, 32 bit number of compilation units or type units in the index
12117 M, 32 bit number of slots in the hash table
12118
12119 Numbers are recorded using the byte order of the application binary.
12120
12121 The hash table has the same format as version 1.
12122 The parallel table of indices has the same format as version 1,
12123 except that the entries are origin-1 indices into the table of sections
12124 offsets and the table of section sizes.
12125
12126 The table of offsets begins immediately following the parallel table
12127 (at offset 16 + 12 * M from the beginning of the section). The table is
12128 a two-dimensional array of 32-bit words (using the byte order of the
12129 application binary), with L columns and N+1 rows, in row-major order.
12130 Each row in the array is indexed starting from 0. The first row provides
12131 a key to the remaining rows: each column in this row provides an identifier
12132 for a debug section, and the offsets in the same column of subsequent rows
12133 refer to that section. The section identifiers are:
12134
12135 DW_SECT_INFO 1 .debug_info.dwo
12136 DW_SECT_TYPES 2 .debug_types.dwo
12137 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12138 DW_SECT_LINE 4 .debug_line.dwo
12139 DW_SECT_LOC 5 .debug_loc.dwo
12140 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12141 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12142 DW_SECT_MACRO 8 .debug_macro.dwo
12143
12144 The offsets provided by the CU and TU index sections are the base offsets
12145 for the contributions made by each CU or TU to the corresponding section
12146 in the package file. Each CU and TU header contains an abbrev_offset
12147 field, used to find the abbreviations table for that CU or TU within the
12148 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12149 be interpreted as relative to the base offset given in the index section.
12150 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12151 should be interpreted as relative to the base offset for .debug_line.dwo,
12152 and offsets into other debug sections obtained from DWARF attributes should
12153 also be interpreted as relative to the corresponding base offset.
12154
12155 The table of sizes begins immediately following the table of offsets.
12156 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12157 with L columns and N rows, in row-major order. Each row in the array is
12158 indexed starting from 1 (row 0 is shared by the two tables).
12159
12160 ---
12161
12162 Hash table lookup is handled the same in version 1 and 2:
12163
12164 We assume that N and M will not exceed 2^32 - 1.
12165 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12166
12167 Given a 64-bit compilation unit signature or a type signature S, an entry
12168 in the hash table is located as follows:
12169
12170 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12171 the low-order k bits all set to 1.
12172
12173 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12174
12175 3) If the hash table entry at index H matches the signature, use that
12176 entry. If the hash table entry at index H is unused (all zeroes),
12177 terminate the search: the signature is not present in the table.
12178
12179 4) Let H = (H + H') modulo M. Repeat at Step 3.
12180
12181 Because M > N and H' and M are relatively prime, the search is guaranteed
12182 to stop at an unused slot or find the match. */
12183
12184 /* Create a hash table to map DWO IDs to their CU/TU entry in
12185 .debug_{info,types}.dwo in DWP_FILE.
12186 Returns NULL if there isn't one.
12187 Note: This function processes DWP files only, not DWO files. */
12188
12189 static struct dwp_hash_table *
12190 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12191 struct dwp_file *dwp_file, int is_debug_types)
12192 {
12193 struct objfile *objfile = dwarf2_per_objfile->objfile;
12194 bfd *dbfd = dwp_file->dbfd;
12195 const gdb_byte *index_ptr, *index_end;
12196 struct dwarf2_section_info *index;
12197 uint32_t version, nr_columns, nr_units, nr_slots;
12198 struct dwp_hash_table *htab;
12199
12200 if (is_debug_types)
12201 index = &dwp_file->sections.tu_index;
12202 else
12203 index = &dwp_file->sections.cu_index;
12204
12205 if (dwarf2_section_empty_p (index))
12206 return NULL;
12207 dwarf2_read_section (objfile, index);
12208
12209 index_ptr = index->buffer;
12210 index_end = index_ptr + index->size;
12211
12212 version = read_4_bytes (dbfd, index_ptr);
12213 index_ptr += 4;
12214 if (version == 2)
12215 nr_columns = read_4_bytes (dbfd, index_ptr);
12216 else
12217 nr_columns = 0;
12218 index_ptr += 4;
12219 nr_units = read_4_bytes (dbfd, index_ptr);
12220 index_ptr += 4;
12221 nr_slots = read_4_bytes (dbfd, index_ptr);
12222 index_ptr += 4;
12223
12224 if (version != 1 && version != 2)
12225 {
12226 error (_("Dwarf Error: unsupported DWP file version (%s)"
12227 " [in module %s]"),
12228 pulongest (version), dwp_file->name);
12229 }
12230 if (nr_slots != (nr_slots & -nr_slots))
12231 {
12232 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12233 " is not power of 2 [in module %s]"),
12234 pulongest (nr_slots), dwp_file->name);
12235 }
12236
12237 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12238 htab->version = version;
12239 htab->nr_columns = nr_columns;
12240 htab->nr_units = nr_units;
12241 htab->nr_slots = nr_slots;
12242 htab->hash_table = index_ptr;
12243 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12244
12245 /* Exit early if the table is empty. */
12246 if (nr_slots == 0 || nr_units == 0
12247 || (version == 2 && nr_columns == 0))
12248 {
12249 /* All must be zero. */
12250 if (nr_slots != 0 || nr_units != 0
12251 || (version == 2 && nr_columns != 0))
12252 {
12253 complaint (&symfile_complaints,
12254 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12255 " all zero [in modules %s]"),
12256 dwp_file->name);
12257 }
12258 return htab;
12259 }
12260
12261 if (version == 1)
12262 {
12263 htab->section_pool.v1.indices =
12264 htab->unit_table + sizeof (uint32_t) * nr_slots;
12265 /* It's harder to decide whether the section is too small in v1.
12266 V1 is deprecated anyway so we punt. */
12267 }
12268 else
12269 {
12270 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12271 int *ids = htab->section_pool.v2.section_ids;
12272 /* Reverse map for error checking. */
12273 int ids_seen[DW_SECT_MAX + 1];
12274 int i;
12275
12276 if (nr_columns < 2)
12277 {
12278 error (_("Dwarf Error: bad DWP hash table, too few columns"
12279 " in section table [in module %s]"),
12280 dwp_file->name);
12281 }
12282 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12283 {
12284 error (_("Dwarf Error: bad DWP hash table, too many columns"
12285 " in section table [in module %s]"),
12286 dwp_file->name);
12287 }
12288 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12289 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12290 for (i = 0; i < nr_columns; ++i)
12291 {
12292 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12293
12294 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12295 {
12296 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12297 " in section table [in module %s]"),
12298 id, dwp_file->name);
12299 }
12300 if (ids_seen[id] != -1)
12301 {
12302 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12303 " id %d in section table [in module %s]"),
12304 id, dwp_file->name);
12305 }
12306 ids_seen[id] = i;
12307 ids[i] = id;
12308 }
12309 /* Must have exactly one info or types section. */
12310 if (((ids_seen[DW_SECT_INFO] != -1)
12311 + (ids_seen[DW_SECT_TYPES] != -1))
12312 != 1)
12313 {
12314 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12315 " DWO info/types section [in module %s]"),
12316 dwp_file->name);
12317 }
12318 /* Must have an abbrev section. */
12319 if (ids_seen[DW_SECT_ABBREV] == -1)
12320 {
12321 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12322 " section [in module %s]"),
12323 dwp_file->name);
12324 }
12325 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12326 htab->section_pool.v2.sizes =
12327 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12328 * nr_units * nr_columns);
12329 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12330 * nr_units * nr_columns))
12331 > index_end)
12332 {
12333 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12334 " [in module %s]"),
12335 dwp_file->name);
12336 }
12337 }
12338
12339 return htab;
12340 }
12341
12342 /* Update SECTIONS with the data from SECTP.
12343
12344 This function is like the other "locate" section routines that are
12345 passed to bfd_map_over_sections, but in this context the sections to
12346 read comes from the DWP V1 hash table, not the full ELF section table.
12347
12348 The result is non-zero for success, or zero if an error was found. */
12349
12350 static int
12351 locate_v1_virtual_dwo_sections (asection *sectp,
12352 struct virtual_v1_dwo_sections *sections)
12353 {
12354 const struct dwop_section_names *names = &dwop_section_names;
12355
12356 if (section_is_p (sectp->name, &names->abbrev_dwo))
12357 {
12358 /* There can be only one. */
12359 if (sections->abbrev.s.section != NULL)
12360 return 0;
12361 sections->abbrev.s.section = sectp;
12362 sections->abbrev.size = bfd_get_section_size (sectp);
12363 }
12364 else if (section_is_p (sectp->name, &names->info_dwo)
12365 || section_is_p (sectp->name, &names->types_dwo))
12366 {
12367 /* There can be only one. */
12368 if (sections->info_or_types.s.section != NULL)
12369 return 0;
12370 sections->info_or_types.s.section = sectp;
12371 sections->info_or_types.size = bfd_get_section_size (sectp);
12372 }
12373 else if (section_is_p (sectp->name, &names->line_dwo))
12374 {
12375 /* There can be only one. */
12376 if (sections->line.s.section != NULL)
12377 return 0;
12378 sections->line.s.section = sectp;
12379 sections->line.size = bfd_get_section_size (sectp);
12380 }
12381 else if (section_is_p (sectp->name, &names->loc_dwo))
12382 {
12383 /* There can be only one. */
12384 if (sections->loc.s.section != NULL)
12385 return 0;
12386 sections->loc.s.section = sectp;
12387 sections->loc.size = bfd_get_section_size (sectp);
12388 }
12389 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12390 {
12391 /* There can be only one. */
12392 if (sections->macinfo.s.section != NULL)
12393 return 0;
12394 sections->macinfo.s.section = sectp;
12395 sections->macinfo.size = bfd_get_section_size (sectp);
12396 }
12397 else if (section_is_p (sectp->name, &names->macro_dwo))
12398 {
12399 /* There can be only one. */
12400 if (sections->macro.s.section != NULL)
12401 return 0;
12402 sections->macro.s.section = sectp;
12403 sections->macro.size = bfd_get_section_size (sectp);
12404 }
12405 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12406 {
12407 /* There can be only one. */
12408 if (sections->str_offsets.s.section != NULL)
12409 return 0;
12410 sections->str_offsets.s.section = sectp;
12411 sections->str_offsets.size = bfd_get_section_size (sectp);
12412 }
12413 else
12414 {
12415 /* No other kind of section is valid. */
12416 return 0;
12417 }
12418
12419 return 1;
12420 }
12421
12422 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12423 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12424 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12425 This is for DWP version 1 files. */
12426
12427 static struct dwo_unit *
12428 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12429 struct dwp_file *dwp_file,
12430 uint32_t unit_index,
12431 const char *comp_dir,
12432 ULONGEST signature, int is_debug_types)
12433 {
12434 struct objfile *objfile = dwarf2_per_objfile->objfile;
12435 const struct dwp_hash_table *dwp_htab =
12436 is_debug_types ? dwp_file->tus : dwp_file->cus;
12437 bfd *dbfd = dwp_file->dbfd;
12438 const char *kind = is_debug_types ? "TU" : "CU";
12439 struct dwo_file *dwo_file;
12440 struct dwo_unit *dwo_unit;
12441 struct virtual_v1_dwo_sections sections;
12442 void **dwo_file_slot;
12443 int i;
12444
12445 gdb_assert (dwp_file->version == 1);
12446
12447 if (dwarf_read_debug)
12448 {
12449 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12450 kind,
12451 pulongest (unit_index), hex_string (signature),
12452 dwp_file->name);
12453 }
12454
12455 /* Fetch the sections of this DWO unit.
12456 Put a limit on the number of sections we look for so that bad data
12457 doesn't cause us to loop forever. */
12458
12459 #define MAX_NR_V1_DWO_SECTIONS \
12460 (1 /* .debug_info or .debug_types */ \
12461 + 1 /* .debug_abbrev */ \
12462 + 1 /* .debug_line */ \
12463 + 1 /* .debug_loc */ \
12464 + 1 /* .debug_str_offsets */ \
12465 + 1 /* .debug_macro or .debug_macinfo */ \
12466 + 1 /* trailing zero */)
12467
12468 memset (&sections, 0, sizeof (sections));
12469
12470 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12471 {
12472 asection *sectp;
12473 uint32_t section_nr =
12474 read_4_bytes (dbfd,
12475 dwp_htab->section_pool.v1.indices
12476 + (unit_index + i) * sizeof (uint32_t));
12477
12478 if (section_nr == 0)
12479 break;
12480 if (section_nr >= dwp_file->num_sections)
12481 {
12482 error (_("Dwarf Error: bad DWP hash table, section number too large"
12483 " [in module %s]"),
12484 dwp_file->name);
12485 }
12486
12487 sectp = dwp_file->elf_sections[section_nr];
12488 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12489 {
12490 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12491 " [in module %s]"),
12492 dwp_file->name);
12493 }
12494 }
12495
12496 if (i < 2
12497 || dwarf2_section_empty_p (&sections.info_or_types)
12498 || dwarf2_section_empty_p (&sections.abbrev))
12499 {
12500 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12501 " [in module %s]"),
12502 dwp_file->name);
12503 }
12504 if (i == MAX_NR_V1_DWO_SECTIONS)
12505 {
12506 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12507 " [in module %s]"),
12508 dwp_file->name);
12509 }
12510
12511 /* It's easier for the rest of the code if we fake a struct dwo_file and
12512 have dwo_unit "live" in that. At least for now.
12513
12514 The DWP file can be made up of a random collection of CUs and TUs.
12515 However, for each CU + set of TUs that came from the same original DWO
12516 file, we can combine them back into a virtual DWO file to save space
12517 (fewer struct dwo_file objects to allocate). Remember that for really
12518 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12519
12520 std::string virtual_dwo_name =
12521 string_printf ("virtual-dwo/%d-%d-%d-%d",
12522 get_section_id (&sections.abbrev),
12523 get_section_id (&sections.line),
12524 get_section_id (&sections.loc),
12525 get_section_id (&sections.str_offsets));
12526 /* Can we use an existing virtual DWO file? */
12527 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12528 virtual_dwo_name.c_str (),
12529 comp_dir);
12530 /* Create one if necessary. */
12531 if (*dwo_file_slot == NULL)
12532 {
12533 if (dwarf_read_debug)
12534 {
12535 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12536 virtual_dwo_name.c_str ());
12537 }
12538 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12539 dwo_file->dwo_name
12540 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12541 virtual_dwo_name.c_str (),
12542 virtual_dwo_name.size ());
12543 dwo_file->comp_dir = comp_dir;
12544 dwo_file->sections.abbrev = sections.abbrev;
12545 dwo_file->sections.line = sections.line;
12546 dwo_file->sections.loc = sections.loc;
12547 dwo_file->sections.macinfo = sections.macinfo;
12548 dwo_file->sections.macro = sections.macro;
12549 dwo_file->sections.str_offsets = sections.str_offsets;
12550 /* The "str" section is global to the entire DWP file. */
12551 dwo_file->sections.str = dwp_file->sections.str;
12552 /* The info or types section is assigned below to dwo_unit,
12553 there's no need to record it in dwo_file.
12554 Also, we can't simply record type sections in dwo_file because
12555 we record a pointer into the vector in dwo_unit. As we collect more
12556 types we'll grow the vector and eventually have to reallocate space
12557 for it, invalidating all copies of pointers into the previous
12558 contents. */
12559 *dwo_file_slot = dwo_file;
12560 }
12561 else
12562 {
12563 if (dwarf_read_debug)
12564 {
12565 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12566 virtual_dwo_name.c_str ());
12567 }
12568 dwo_file = (struct dwo_file *) *dwo_file_slot;
12569 }
12570
12571 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12572 dwo_unit->dwo_file = dwo_file;
12573 dwo_unit->signature = signature;
12574 dwo_unit->section =
12575 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12576 *dwo_unit->section = sections.info_or_types;
12577 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12578
12579 return dwo_unit;
12580 }
12581
12582 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12583 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12584 piece within that section used by a TU/CU, return a virtual section
12585 of just that piece. */
12586
12587 static struct dwarf2_section_info
12588 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12589 struct dwarf2_section_info *section,
12590 bfd_size_type offset, bfd_size_type size)
12591 {
12592 struct dwarf2_section_info result;
12593 asection *sectp;
12594
12595 gdb_assert (section != NULL);
12596 gdb_assert (!section->is_virtual);
12597
12598 memset (&result, 0, sizeof (result));
12599 result.s.containing_section = section;
12600 result.is_virtual = 1;
12601
12602 if (size == 0)
12603 return result;
12604
12605 sectp = get_section_bfd_section (section);
12606
12607 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12608 bounds of the real section. This is a pretty-rare event, so just
12609 flag an error (easier) instead of a warning and trying to cope. */
12610 if (sectp == NULL
12611 || offset + size > bfd_get_section_size (sectp))
12612 {
12613 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12614 " in section %s [in module %s]"),
12615 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12616 objfile_name (dwarf2_per_objfile->objfile));
12617 }
12618
12619 result.virtual_offset = offset;
12620 result.size = size;
12621 return result;
12622 }
12623
12624 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12625 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12626 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12627 This is for DWP version 2 files. */
12628
12629 static struct dwo_unit *
12630 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12631 struct dwp_file *dwp_file,
12632 uint32_t unit_index,
12633 const char *comp_dir,
12634 ULONGEST signature, int is_debug_types)
12635 {
12636 struct objfile *objfile = dwarf2_per_objfile->objfile;
12637 const struct dwp_hash_table *dwp_htab =
12638 is_debug_types ? dwp_file->tus : dwp_file->cus;
12639 bfd *dbfd = dwp_file->dbfd;
12640 const char *kind = is_debug_types ? "TU" : "CU";
12641 struct dwo_file *dwo_file;
12642 struct dwo_unit *dwo_unit;
12643 struct virtual_v2_dwo_sections sections;
12644 void **dwo_file_slot;
12645 int i;
12646
12647 gdb_assert (dwp_file->version == 2);
12648
12649 if (dwarf_read_debug)
12650 {
12651 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12652 kind,
12653 pulongest (unit_index), hex_string (signature),
12654 dwp_file->name);
12655 }
12656
12657 /* Fetch the section offsets of this DWO unit. */
12658
12659 memset (&sections, 0, sizeof (sections));
12660
12661 for (i = 0; i < dwp_htab->nr_columns; ++i)
12662 {
12663 uint32_t offset = read_4_bytes (dbfd,
12664 dwp_htab->section_pool.v2.offsets
12665 + (((unit_index - 1) * dwp_htab->nr_columns
12666 + i)
12667 * sizeof (uint32_t)));
12668 uint32_t size = read_4_bytes (dbfd,
12669 dwp_htab->section_pool.v2.sizes
12670 + (((unit_index - 1) * dwp_htab->nr_columns
12671 + i)
12672 * sizeof (uint32_t)));
12673
12674 switch (dwp_htab->section_pool.v2.section_ids[i])
12675 {
12676 case DW_SECT_INFO:
12677 case DW_SECT_TYPES:
12678 sections.info_or_types_offset = offset;
12679 sections.info_or_types_size = size;
12680 break;
12681 case DW_SECT_ABBREV:
12682 sections.abbrev_offset = offset;
12683 sections.abbrev_size = size;
12684 break;
12685 case DW_SECT_LINE:
12686 sections.line_offset = offset;
12687 sections.line_size = size;
12688 break;
12689 case DW_SECT_LOC:
12690 sections.loc_offset = offset;
12691 sections.loc_size = size;
12692 break;
12693 case DW_SECT_STR_OFFSETS:
12694 sections.str_offsets_offset = offset;
12695 sections.str_offsets_size = size;
12696 break;
12697 case DW_SECT_MACINFO:
12698 sections.macinfo_offset = offset;
12699 sections.macinfo_size = size;
12700 break;
12701 case DW_SECT_MACRO:
12702 sections.macro_offset = offset;
12703 sections.macro_size = size;
12704 break;
12705 }
12706 }
12707
12708 /* It's easier for the rest of the code if we fake a struct dwo_file and
12709 have dwo_unit "live" in that. At least for now.
12710
12711 The DWP file can be made up of a random collection of CUs and TUs.
12712 However, for each CU + set of TUs that came from the same original DWO
12713 file, we can combine them back into a virtual DWO file to save space
12714 (fewer struct dwo_file objects to allocate). Remember that for really
12715 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12716
12717 std::string virtual_dwo_name =
12718 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12719 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12720 (long) (sections.line_size ? sections.line_offset : 0),
12721 (long) (sections.loc_size ? sections.loc_offset : 0),
12722 (long) (sections.str_offsets_size
12723 ? sections.str_offsets_offset : 0));
12724 /* Can we use an existing virtual DWO file? */
12725 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12726 virtual_dwo_name.c_str (),
12727 comp_dir);
12728 /* Create one if necessary. */
12729 if (*dwo_file_slot == NULL)
12730 {
12731 if (dwarf_read_debug)
12732 {
12733 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12734 virtual_dwo_name.c_str ());
12735 }
12736 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12737 dwo_file->dwo_name
12738 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12739 virtual_dwo_name.c_str (),
12740 virtual_dwo_name.size ());
12741 dwo_file->comp_dir = comp_dir;
12742 dwo_file->sections.abbrev =
12743 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12744 sections.abbrev_offset, sections.abbrev_size);
12745 dwo_file->sections.line =
12746 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12747 sections.line_offset, sections.line_size);
12748 dwo_file->sections.loc =
12749 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12750 sections.loc_offset, sections.loc_size);
12751 dwo_file->sections.macinfo =
12752 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12753 sections.macinfo_offset, sections.macinfo_size);
12754 dwo_file->sections.macro =
12755 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12756 sections.macro_offset, sections.macro_size);
12757 dwo_file->sections.str_offsets =
12758 create_dwp_v2_section (dwarf2_per_objfile,
12759 &dwp_file->sections.str_offsets,
12760 sections.str_offsets_offset,
12761 sections.str_offsets_size);
12762 /* The "str" section is global to the entire DWP file. */
12763 dwo_file->sections.str = dwp_file->sections.str;
12764 /* The info or types section is assigned below to dwo_unit,
12765 there's no need to record it in dwo_file.
12766 Also, we can't simply record type sections in dwo_file because
12767 we record a pointer into the vector in dwo_unit. As we collect more
12768 types we'll grow the vector and eventually have to reallocate space
12769 for it, invalidating all copies of pointers into the previous
12770 contents. */
12771 *dwo_file_slot = dwo_file;
12772 }
12773 else
12774 {
12775 if (dwarf_read_debug)
12776 {
12777 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12778 virtual_dwo_name.c_str ());
12779 }
12780 dwo_file = (struct dwo_file *) *dwo_file_slot;
12781 }
12782
12783 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12784 dwo_unit->dwo_file = dwo_file;
12785 dwo_unit->signature = signature;
12786 dwo_unit->section =
12787 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12788 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12789 is_debug_types
12790 ? &dwp_file->sections.types
12791 : &dwp_file->sections.info,
12792 sections.info_or_types_offset,
12793 sections.info_or_types_size);
12794 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12795
12796 return dwo_unit;
12797 }
12798
12799 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12800 Returns NULL if the signature isn't found. */
12801
12802 static struct dwo_unit *
12803 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12804 struct dwp_file *dwp_file, const char *comp_dir,
12805 ULONGEST signature, int is_debug_types)
12806 {
12807 const struct dwp_hash_table *dwp_htab =
12808 is_debug_types ? dwp_file->tus : dwp_file->cus;
12809 bfd *dbfd = dwp_file->dbfd;
12810 uint32_t mask = dwp_htab->nr_slots - 1;
12811 uint32_t hash = signature & mask;
12812 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12813 unsigned int i;
12814 void **slot;
12815 struct dwo_unit find_dwo_cu;
12816
12817 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12818 find_dwo_cu.signature = signature;
12819 slot = htab_find_slot (is_debug_types
12820 ? dwp_file->loaded_tus
12821 : dwp_file->loaded_cus,
12822 &find_dwo_cu, INSERT);
12823
12824 if (*slot != NULL)
12825 return (struct dwo_unit *) *slot;
12826
12827 /* Use a for loop so that we don't loop forever on bad debug info. */
12828 for (i = 0; i < dwp_htab->nr_slots; ++i)
12829 {
12830 ULONGEST signature_in_table;
12831
12832 signature_in_table =
12833 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12834 if (signature_in_table == signature)
12835 {
12836 uint32_t unit_index =
12837 read_4_bytes (dbfd,
12838 dwp_htab->unit_table + hash * sizeof (uint32_t));
12839
12840 if (dwp_file->version == 1)
12841 {
12842 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12843 dwp_file, unit_index,
12844 comp_dir, signature,
12845 is_debug_types);
12846 }
12847 else
12848 {
12849 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12850 dwp_file, unit_index,
12851 comp_dir, signature,
12852 is_debug_types);
12853 }
12854 return (struct dwo_unit *) *slot;
12855 }
12856 if (signature_in_table == 0)
12857 return NULL;
12858 hash = (hash + hash2) & mask;
12859 }
12860
12861 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12862 " [in module %s]"),
12863 dwp_file->name);
12864 }
12865
12866 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12867 Open the file specified by FILE_NAME and hand it off to BFD for
12868 preliminary analysis. Return a newly initialized bfd *, which
12869 includes a canonicalized copy of FILE_NAME.
12870 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12871 SEARCH_CWD is true if the current directory is to be searched.
12872 It will be searched before debug-file-directory.
12873 If successful, the file is added to the bfd include table of the
12874 objfile's bfd (see gdb_bfd_record_inclusion).
12875 If unable to find/open the file, return NULL.
12876 NOTE: This function is derived from symfile_bfd_open. */
12877
12878 static gdb_bfd_ref_ptr
12879 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12880 const char *file_name, int is_dwp, int search_cwd)
12881 {
12882 int desc;
12883 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12884 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12885 to debug_file_directory. */
12886 const char *search_path;
12887 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12888
12889 gdb::unique_xmalloc_ptr<char> search_path_holder;
12890 if (search_cwd)
12891 {
12892 if (*debug_file_directory != '\0')
12893 {
12894 search_path_holder.reset (concat (".", dirname_separator_string,
12895 debug_file_directory,
12896 (char *) NULL));
12897 search_path = search_path_holder.get ();
12898 }
12899 else
12900 search_path = ".";
12901 }
12902 else
12903 search_path = debug_file_directory;
12904
12905 openp_flags flags = OPF_RETURN_REALPATH;
12906 if (is_dwp)
12907 flags |= OPF_SEARCH_IN_PATH;
12908
12909 gdb::unique_xmalloc_ptr<char> absolute_name;
12910 desc = openp (search_path, flags, file_name,
12911 O_RDONLY | O_BINARY, &absolute_name);
12912 if (desc < 0)
12913 return NULL;
12914
12915 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12916 gnutarget, desc));
12917 if (sym_bfd == NULL)
12918 return NULL;
12919 bfd_set_cacheable (sym_bfd.get (), 1);
12920
12921 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12922 return NULL;
12923
12924 /* Success. Record the bfd as having been included by the objfile's bfd.
12925 This is important because things like demangled_names_hash lives in the
12926 objfile's per_bfd space and may have references to things like symbol
12927 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12928 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12929
12930 return sym_bfd;
12931 }
12932
12933 /* Try to open DWO file FILE_NAME.
12934 COMP_DIR is the DW_AT_comp_dir attribute.
12935 The result is the bfd handle of the file.
12936 If there is a problem finding or opening the file, return NULL.
12937 Upon success, the canonicalized path of the file is stored in the bfd,
12938 same as symfile_bfd_open. */
12939
12940 static gdb_bfd_ref_ptr
12941 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12942 const char *file_name, const char *comp_dir)
12943 {
12944 if (IS_ABSOLUTE_PATH (file_name))
12945 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12946 0 /*is_dwp*/, 0 /*search_cwd*/);
12947
12948 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12949
12950 if (comp_dir != NULL)
12951 {
12952 char *path_to_try = concat (comp_dir, SLASH_STRING,
12953 file_name, (char *) NULL);
12954
12955 /* NOTE: If comp_dir is a relative path, this will also try the
12956 search path, which seems useful. */
12957 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12958 path_to_try,
12959 0 /*is_dwp*/,
12960 1 /*search_cwd*/));
12961 xfree (path_to_try);
12962 if (abfd != NULL)
12963 return abfd;
12964 }
12965
12966 /* That didn't work, try debug-file-directory, which, despite its name,
12967 is a list of paths. */
12968
12969 if (*debug_file_directory == '\0')
12970 return NULL;
12971
12972 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12973 0 /*is_dwp*/, 1 /*search_cwd*/);
12974 }
12975
12976 /* This function is mapped across the sections and remembers the offset and
12977 size of each of the DWO debugging sections we are interested in. */
12978
12979 static void
12980 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12981 {
12982 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12983 const struct dwop_section_names *names = &dwop_section_names;
12984
12985 if (section_is_p (sectp->name, &names->abbrev_dwo))
12986 {
12987 dwo_sections->abbrev.s.section = sectp;
12988 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12989 }
12990 else if (section_is_p (sectp->name, &names->info_dwo))
12991 {
12992 dwo_sections->info.s.section = sectp;
12993 dwo_sections->info.size = bfd_get_section_size (sectp);
12994 }
12995 else if (section_is_p (sectp->name, &names->line_dwo))
12996 {
12997 dwo_sections->line.s.section = sectp;
12998 dwo_sections->line.size = bfd_get_section_size (sectp);
12999 }
13000 else if (section_is_p (sectp->name, &names->loc_dwo))
13001 {
13002 dwo_sections->loc.s.section = sectp;
13003 dwo_sections->loc.size = bfd_get_section_size (sectp);
13004 }
13005 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13006 {
13007 dwo_sections->macinfo.s.section = sectp;
13008 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
13009 }
13010 else if (section_is_p (sectp->name, &names->macro_dwo))
13011 {
13012 dwo_sections->macro.s.section = sectp;
13013 dwo_sections->macro.size = bfd_get_section_size (sectp);
13014 }
13015 else if (section_is_p (sectp->name, &names->str_dwo))
13016 {
13017 dwo_sections->str.s.section = sectp;
13018 dwo_sections->str.size = bfd_get_section_size (sectp);
13019 }
13020 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13021 {
13022 dwo_sections->str_offsets.s.section = sectp;
13023 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
13024 }
13025 else if (section_is_p (sectp->name, &names->types_dwo))
13026 {
13027 struct dwarf2_section_info type_section;
13028
13029 memset (&type_section, 0, sizeof (type_section));
13030 type_section.s.section = sectp;
13031 type_section.size = bfd_get_section_size (sectp);
13032 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
13033 &type_section);
13034 }
13035 }
13036
13037 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13038 by PER_CU. This is for the non-DWP case.
13039 The result is NULL if DWO_NAME can't be found. */
13040
13041 static struct dwo_file *
13042 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13043 const char *dwo_name, const char *comp_dir)
13044 {
13045 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13046 struct objfile *objfile = dwarf2_per_objfile->objfile;
13047 struct dwo_file *dwo_file;
13048 struct cleanup *cleanups;
13049
13050 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
13051 if (dbfd == NULL)
13052 {
13053 if (dwarf_read_debug)
13054 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13055 return NULL;
13056 }
13057 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
13058 dwo_file->dwo_name = dwo_name;
13059 dwo_file->comp_dir = comp_dir;
13060 dwo_file->dbfd = dbfd.release ();
13061
13062 free_dwo_file_cleanup_data *cleanup_data = XNEW (free_dwo_file_cleanup_data);
13063 cleanup_data->dwo_file = dwo_file;
13064 cleanup_data->dwarf2_per_objfile = dwarf2_per_objfile;
13065
13066 cleanups = make_cleanup (free_dwo_file_cleanup, cleanup_data);
13067
13068 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13069 &dwo_file->sections);
13070
13071 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13072 dwo_file->cus);
13073
13074 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file,
13075 dwo_file->sections.types, dwo_file->tus);
13076
13077 discard_cleanups (cleanups);
13078
13079 if (dwarf_read_debug)
13080 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13081
13082 return dwo_file;
13083 }
13084
13085 /* This function is mapped across the sections and remembers the offset and
13086 size of each of the DWP debugging sections common to version 1 and 2 that
13087 we are interested in. */
13088
13089 static void
13090 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13091 void *dwp_file_ptr)
13092 {
13093 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13094 const struct dwop_section_names *names = &dwop_section_names;
13095 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13096
13097 /* Record the ELF section number for later lookup: this is what the
13098 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13099 gdb_assert (elf_section_nr < dwp_file->num_sections);
13100 dwp_file->elf_sections[elf_section_nr] = sectp;
13101
13102 /* Look for specific sections that we need. */
13103 if (section_is_p (sectp->name, &names->str_dwo))
13104 {
13105 dwp_file->sections.str.s.section = sectp;
13106 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13107 }
13108 else if (section_is_p (sectp->name, &names->cu_index))
13109 {
13110 dwp_file->sections.cu_index.s.section = sectp;
13111 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13112 }
13113 else if (section_is_p (sectp->name, &names->tu_index))
13114 {
13115 dwp_file->sections.tu_index.s.section = sectp;
13116 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13117 }
13118 }
13119
13120 /* This function is mapped across the sections and remembers the offset and
13121 size of each of the DWP version 2 debugging sections that we are interested
13122 in. This is split into a separate function because we don't know if we
13123 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13124
13125 static void
13126 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13127 {
13128 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13129 const struct dwop_section_names *names = &dwop_section_names;
13130 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13131
13132 /* Record the ELF section number for later lookup: this is what the
13133 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13134 gdb_assert (elf_section_nr < dwp_file->num_sections);
13135 dwp_file->elf_sections[elf_section_nr] = sectp;
13136
13137 /* Look for specific sections that we need. */
13138 if (section_is_p (sectp->name, &names->abbrev_dwo))
13139 {
13140 dwp_file->sections.abbrev.s.section = sectp;
13141 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13142 }
13143 else if (section_is_p (sectp->name, &names->info_dwo))
13144 {
13145 dwp_file->sections.info.s.section = sectp;
13146 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13147 }
13148 else if (section_is_p (sectp->name, &names->line_dwo))
13149 {
13150 dwp_file->sections.line.s.section = sectp;
13151 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13152 }
13153 else if (section_is_p (sectp->name, &names->loc_dwo))
13154 {
13155 dwp_file->sections.loc.s.section = sectp;
13156 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13157 }
13158 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13159 {
13160 dwp_file->sections.macinfo.s.section = sectp;
13161 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13162 }
13163 else if (section_is_p (sectp->name, &names->macro_dwo))
13164 {
13165 dwp_file->sections.macro.s.section = sectp;
13166 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13167 }
13168 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13169 {
13170 dwp_file->sections.str_offsets.s.section = sectp;
13171 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13172 }
13173 else if (section_is_p (sectp->name, &names->types_dwo))
13174 {
13175 dwp_file->sections.types.s.section = sectp;
13176 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13177 }
13178 }
13179
13180 /* Hash function for dwp_file loaded CUs/TUs. */
13181
13182 static hashval_t
13183 hash_dwp_loaded_cutus (const void *item)
13184 {
13185 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13186
13187 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13188 return dwo_unit->signature;
13189 }
13190
13191 /* Equality function for dwp_file loaded CUs/TUs. */
13192
13193 static int
13194 eq_dwp_loaded_cutus (const void *a, const void *b)
13195 {
13196 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13197 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13198
13199 return dua->signature == dub->signature;
13200 }
13201
13202 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13203
13204 static htab_t
13205 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13206 {
13207 return htab_create_alloc_ex (3,
13208 hash_dwp_loaded_cutus,
13209 eq_dwp_loaded_cutus,
13210 NULL,
13211 &objfile->objfile_obstack,
13212 hashtab_obstack_allocate,
13213 dummy_obstack_deallocate);
13214 }
13215
13216 /* Try to open DWP file FILE_NAME.
13217 The result is the bfd handle of the file.
13218 If there is a problem finding or opening the file, return NULL.
13219 Upon success, the canonicalized path of the file is stored in the bfd,
13220 same as symfile_bfd_open. */
13221
13222 static gdb_bfd_ref_ptr
13223 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13224 const char *file_name)
13225 {
13226 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13227 1 /*is_dwp*/,
13228 1 /*search_cwd*/));
13229 if (abfd != NULL)
13230 return abfd;
13231
13232 /* Work around upstream bug 15652.
13233 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13234 [Whether that's a "bug" is debatable, but it is getting in our way.]
13235 We have no real idea where the dwp file is, because gdb's realpath-ing
13236 of the executable's path may have discarded the needed info.
13237 [IWBN if the dwp file name was recorded in the executable, akin to
13238 .gnu_debuglink, but that doesn't exist yet.]
13239 Strip the directory from FILE_NAME and search again. */
13240 if (*debug_file_directory != '\0')
13241 {
13242 /* Don't implicitly search the current directory here.
13243 If the user wants to search "." to handle this case,
13244 it must be added to debug-file-directory. */
13245 return try_open_dwop_file (dwarf2_per_objfile,
13246 lbasename (file_name), 1 /*is_dwp*/,
13247 0 /*search_cwd*/);
13248 }
13249
13250 return NULL;
13251 }
13252
13253 /* Initialize the use of the DWP file for the current objfile.
13254 By convention the name of the DWP file is ${objfile}.dwp.
13255 The result is NULL if it can't be found. */
13256
13257 static struct dwp_file *
13258 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13259 {
13260 struct objfile *objfile = dwarf2_per_objfile->objfile;
13261 struct dwp_file *dwp_file;
13262
13263 /* Try to find first .dwp for the binary file before any symbolic links
13264 resolving. */
13265
13266 /* If the objfile is a debug file, find the name of the real binary
13267 file and get the name of dwp file from there. */
13268 std::string dwp_name;
13269 if (objfile->separate_debug_objfile_backlink != NULL)
13270 {
13271 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13272 const char *backlink_basename = lbasename (backlink->original_name);
13273
13274 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13275 }
13276 else
13277 dwp_name = objfile->original_name;
13278
13279 dwp_name += ".dwp";
13280
13281 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13282 if (dbfd == NULL
13283 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13284 {
13285 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13286 dwp_name = objfile_name (objfile);
13287 dwp_name += ".dwp";
13288 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13289 }
13290
13291 if (dbfd == NULL)
13292 {
13293 if (dwarf_read_debug)
13294 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13295 return NULL;
13296 }
13297 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
13298 dwp_file->name = bfd_get_filename (dbfd.get ());
13299 dwp_file->dbfd = dbfd.release ();
13300
13301 /* +1: section 0 is unused */
13302 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13303 dwp_file->elf_sections =
13304 OBSTACK_CALLOC (&objfile->objfile_obstack,
13305 dwp_file->num_sections, asection *);
13306
13307 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13308 dwp_file);
13309
13310 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
13311
13312 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
13313
13314 /* The DWP file version is stored in the hash table. Oh well. */
13315 if (dwp_file->cus && dwp_file->tus
13316 && dwp_file->cus->version != dwp_file->tus->version)
13317 {
13318 /* Technically speaking, we should try to limp along, but this is
13319 pretty bizarre. We use pulongest here because that's the established
13320 portability solution (e.g, we cannot use %u for uint32_t). */
13321 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13322 " TU version %s [in DWP file %s]"),
13323 pulongest (dwp_file->cus->version),
13324 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13325 }
13326
13327 if (dwp_file->cus)
13328 dwp_file->version = dwp_file->cus->version;
13329 else if (dwp_file->tus)
13330 dwp_file->version = dwp_file->tus->version;
13331 else
13332 dwp_file->version = 2;
13333
13334 if (dwp_file->version == 2)
13335 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13336 dwp_file);
13337
13338 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13339 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13340
13341 if (dwarf_read_debug)
13342 {
13343 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13344 fprintf_unfiltered (gdb_stdlog,
13345 " %s CUs, %s TUs\n",
13346 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13347 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13348 }
13349
13350 return dwp_file;
13351 }
13352
13353 /* Wrapper around open_and_init_dwp_file, only open it once. */
13354
13355 static struct dwp_file *
13356 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13357 {
13358 if (! dwarf2_per_objfile->dwp_checked)
13359 {
13360 dwarf2_per_objfile->dwp_file
13361 = open_and_init_dwp_file (dwarf2_per_objfile);
13362 dwarf2_per_objfile->dwp_checked = 1;
13363 }
13364 return dwarf2_per_objfile->dwp_file;
13365 }
13366
13367 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13368 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13369 or in the DWP file for the objfile, referenced by THIS_UNIT.
13370 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13371 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13372
13373 This is called, for example, when wanting to read a variable with a
13374 complex location. Therefore we don't want to do file i/o for every call.
13375 Therefore we don't want to look for a DWO file on every call.
13376 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13377 then we check if we've already seen DWO_NAME, and only THEN do we check
13378 for a DWO file.
13379
13380 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13381 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13382
13383 static struct dwo_unit *
13384 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13385 const char *dwo_name, const char *comp_dir,
13386 ULONGEST signature, int is_debug_types)
13387 {
13388 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13389 struct objfile *objfile = dwarf2_per_objfile->objfile;
13390 const char *kind = is_debug_types ? "TU" : "CU";
13391 void **dwo_file_slot;
13392 struct dwo_file *dwo_file;
13393 struct dwp_file *dwp_file;
13394
13395 /* First see if there's a DWP file.
13396 If we have a DWP file but didn't find the DWO inside it, don't
13397 look for the original DWO file. It makes gdb behave differently
13398 depending on whether one is debugging in the build tree. */
13399
13400 dwp_file = get_dwp_file (dwarf2_per_objfile);
13401 if (dwp_file != NULL)
13402 {
13403 const struct dwp_hash_table *dwp_htab =
13404 is_debug_types ? dwp_file->tus : dwp_file->cus;
13405
13406 if (dwp_htab != NULL)
13407 {
13408 struct dwo_unit *dwo_cutu =
13409 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13410 signature, is_debug_types);
13411
13412 if (dwo_cutu != NULL)
13413 {
13414 if (dwarf_read_debug)
13415 {
13416 fprintf_unfiltered (gdb_stdlog,
13417 "Virtual DWO %s %s found: @%s\n",
13418 kind, hex_string (signature),
13419 host_address_to_string (dwo_cutu));
13420 }
13421 return dwo_cutu;
13422 }
13423 }
13424 }
13425 else
13426 {
13427 /* No DWP file, look for the DWO file. */
13428
13429 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13430 dwo_name, comp_dir);
13431 if (*dwo_file_slot == NULL)
13432 {
13433 /* Read in the file and build a table of the CUs/TUs it contains. */
13434 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13435 }
13436 /* NOTE: This will be NULL if unable to open the file. */
13437 dwo_file = (struct dwo_file *) *dwo_file_slot;
13438
13439 if (dwo_file != NULL)
13440 {
13441 struct dwo_unit *dwo_cutu = NULL;
13442
13443 if (is_debug_types && dwo_file->tus)
13444 {
13445 struct dwo_unit find_dwo_cutu;
13446
13447 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13448 find_dwo_cutu.signature = signature;
13449 dwo_cutu
13450 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13451 }
13452 else if (!is_debug_types && dwo_file->cus)
13453 {
13454 struct dwo_unit find_dwo_cutu;
13455
13456 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13457 find_dwo_cutu.signature = signature;
13458 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13459 &find_dwo_cutu);
13460 }
13461
13462 if (dwo_cutu != NULL)
13463 {
13464 if (dwarf_read_debug)
13465 {
13466 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13467 kind, dwo_name, hex_string (signature),
13468 host_address_to_string (dwo_cutu));
13469 }
13470 return dwo_cutu;
13471 }
13472 }
13473 }
13474
13475 /* We didn't find it. This could mean a dwo_id mismatch, or
13476 someone deleted the DWO/DWP file, or the search path isn't set up
13477 correctly to find the file. */
13478
13479 if (dwarf_read_debug)
13480 {
13481 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13482 kind, dwo_name, hex_string (signature));
13483 }
13484
13485 /* This is a warning and not a complaint because it can be caused by
13486 pilot error (e.g., user accidentally deleting the DWO). */
13487 {
13488 /* Print the name of the DWP file if we looked there, helps the user
13489 better diagnose the problem. */
13490 std::string dwp_text;
13491
13492 if (dwp_file != NULL)
13493 dwp_text = string_printf (" [in DWP file %s]",
13494 lbasename (dwp_file->name));
13495
13496 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
13497 " [in module %s]"),
13498 kind, dwo_name, hex_string (signature),
13499 dwp_text.c_str (),
13500 this_unit->is_debug_types ? "TU" : "CU",
13501 to_underlying (this_unit->sect_off), objfile_name (objfile));
13502 }
13503 return NULL;
13504 }
13505
13506 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13507 See lookup_dwo_cutu_unit for details. */
13508
13509 static struct dwo_unit *
13510 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13511 const char *dwo_name, const char *comp_dir,
13512 ULONGEST signature)
13513 {
13514 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13515 }
13516
13517 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13518 See lookup_dwo_cutu_unit for details. */
13519
13520 static struct dwo_unit *
13521 lookup_dwo_type_unit (struct signatured_type *this_tu,
13522 const char *dwo_name, const char *comp_dir)
13523 {
13524 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13525 }
13526
13527 /* Traversal function for queue_and_load_all_dwo_tus. */
13528
13529 static int
13530 queue_and_load_dwo_tu (void **slot, void *info)
13531 {
13532 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13533 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13534 ULONGEST signature = dwo_unit->signature;
13535 struct signatured_type *sig_type =
13536 lookup_dwo_signatured_type (per_cu->cu, signature);
13537
13538 if (sig_type != NULL)
13539 {
13540 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13541
13542 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13543 a real dependency of PER_CU on SIG_TYPE. That is detected later
13544 while processing PER_CU. */
13545 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13546 load_full_type_unit (sig_cu);
13547 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13548 }
13549
13550 return 1;
13551 }
13552
13553 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13554 The DWO may have the only definition of the type, though it may not be
13555 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13556 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13557
13558 static void
13559 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13560 {
13561 struct dwo_unit *dwo_unit;
13562 struct dwo_file *dwo_file;
13563
13564 gdb_assert (!per_cu->is_debug_types);
13565 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13566 gdb_assert (per_cu->cu != NULL);
13567
13568 dwo_unit = per_cu->cu->dwo_unit;
13569 gdb_assert (dwo_unit != NULL);
13570
13571 dwo_file = dwo_unit->dwo_file;
13572 if (dwo_file->tus != NULL)
13573 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13574 }
13575
13576 /* Free all resources associated with DWO_FILE.
13577 Close the DWO file and munmap the sections.
13578 All memory should be on the objfile obstack. */
13579
13580 static void
13581 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
13582 {
13583
13584 /* Note: dbfd is NULL for virtual DWO files. */
13585 gdb_bfd_unref (dwo_file->dbfd);
13586
13587 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13588 }
13589
13590 /* Wrapper for free_dwo_file for use in cleanups. */
13591
13592 static void
13593 free_dwo_file_cleanup (void *arg)
13594 {
13595 struct free_dwo_file_cleanup_data *data
13596 = (struct free_dwo_file_cleanup_data *) arg;
13597 struct objfile *objfile = data->dwarf2_per_objfile->objfile;
13598
13599 free_dwo_file (data->dwo_file, objfile);
13600
13601 xfree (data);
13602 }
13603
13604 /* Traversal function for free_dwo_files. */
13605
13606 static int
13607 free_dwo_file_from_slot (void **slot, void *info)
13608 {
13609 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13610 struct objfile *objfile = (struct objfile *) info;
13611
13612 free_dwo_file (dwo_file, objfile);
13613
13614 return 1;
13615 }
13616
13617 /* Free all resources associated with DWO_FILES. */
13618
13619 static void
13620 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13621 {
13622 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13623 }
13624 \f
13625 /* Read in various DIEs. */
13626
13627 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13628 Inherit only the children of the DW_AT_abstract_origin DIE not being
13629 already referenced by DW_AT_abstract_origin from the children of the
13630 current DIE. */
13631
13632 static void
13633 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13634 {
13635 struct die_info *child_die;
13636 sect_offset *offsetp;
13637 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13638 struct die_info *origin_die;
13639 /* Iterator of the ORIGIN_DIE children. */
13640 struct die_info *origin_child_die;
13641 struct attribute *attr;
13642 struct dwarf2_cu *origin_cu;
13643 struct pending **origin_previous_list_in_scope;
13644
13645 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13646 if (!attr)
13647 return;
13648
13649 /* Note that following die references may follow to a die in a
13650 different cu. */
13651
13652 origin_cu = cu;
13653 origin_die = follow_die_ref (die, attr, &origin_cu);
13654
13655 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13656 symbols in. */
13657 origin_previous_list_in_scope = origin_cu->list_in_scope;
13658 origin_cu->list_in_scope = cu->list_in_scope;
13659
13660 if (die->tag != origin_die->tag
13661 && !(die->tag == DW_TAG_inlined_subroutine
13662 && origin_die->tag == DW_TAG_subprogram))
13663 complaint (&symfile_complaints,
13664 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
13665 to_underlying (die->sect_off),
13666 to_underlying (origin_die->sect_off));
13667
13668 std::vector<sect_offset> offsets;
13669
13670 for (child_die = die->child;
13671 child_die && child_die->tag;
13672 child_die = sibling_die (child_die))
13673 {
13674 struct die_info *child_origin_die;
13675 struct dwarf2_cu *child_origin_cu;
13676
13677 /* We are trying to process concrete instance entries:
13678 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13679 it's not relevant to our analysis here. i.e. detecting DIEs that are
13680 present in the abstract instance but not referenced in the concrete
13681 one. */
13682 if (child_die->tag == DW_TAG_call_site
13683 || child_die->tag == DW_TAG_GNU_call_site)
13684 continue;
13685
13686 /* For each CHILD_DIE, find the corresponding child of
13687 ORIGIN_DIE. If there is more than one layer of
13688 DW_AT_abstract_origin, follow them all; there shouldn't be,
13689 but GCC versions at least through 4.4 generate this (GCC PR
13690 40573). */
13691 child_origin_die = child_die;
13692 child_origin_cu = cu;
13693 while (1)
13694 {
13695 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13696 child_origin_cu);
13697 if (attr == NULL)
13698 break;
13699 child_origin_die = follow_die_ref (child_origin_die, attr,
13700 &child_origin_cu);
13701 }
13702
13703 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13704 counterpart may exist. */
13705 if (child_origin_die != child_die)
13706 {
13707 if (child_die->tag != child_origin_die->tag
13708 && !(child_die->tag == DW_TAG_inlined_subroutine
13709 && child_origin_die->tag == DW_TAG_subprogram))
13710 complaint (&symfile_complaints,
13711 _("Child DIE 0x%x and its abstract origin 0x%x have "
13712 "different tags"),
13713 to_underlying (child_die->sect_off),
13714 to_underlying (child_origin_die->sect_off));
13715 if (child_origin_die->parent != origin_die)
13716 complaint (&symfile_complaints,
13717 _("Child DIE 0x%x and its abstract origin 0x%x have "
13718 "different parents"),
13719 to_underlying (child_die->sect_off),
13720 to_underlying (child_origin_die->sect_off));
13721 else
13722 offsets.push_back (child_origin_die->sect_off);
13723 }
13724 }
13725 std::sort (offsets.begin (), offsets.end ());
13726 sect_offset *offsets_end = offsets.data () + offsets.size ();
13727 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13728 if (offsetp[-1] == *offsetp)
13729 complaint (&symfile_complaints,
13730 _("Multiple children of DIE 0x%x refer "
13731 "to DIE 0x%x as their abstract origin"),
13732 to_underlying (die->sect_off), to_underlying (*offsetp));
13733
13734 offsetp = offsets.data ();
13735 origin_child_die = origin_die->child;
13736 while (origin_child_die && origin_child_die->tag)
13737 {
13738 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13739 while (offsetp < offsets_end
13740 && *offsetp < origin_child_die->sect_off)
13741 offsetp++;
13742 if (offsetp >= offsets_end
13743 || *offsetp > origin_child_die->sect_off)
13744 {
13745 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13746 Check whether we're already processing ORIGIN_CHILD_DIE.
13747 This can happen with mutually referenced abstract_origins.
13748 PR 16581. */
13749 if (!origin_child_die->in_process)
13750 process_die (origin_child_die, origin_cu);
13751 }
13752 origin_child_die = sibling_die (origin_child_die);
13753 }
13754 origin_cu->list_in_scope = origin_previous_list_in_scope;
13755 }
13756
13757 static void
13758 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13759 {
13760 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13761 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13762 struct context_stack *newobj;
13763 CORE_ADDR lowpc;
13764 CORE_ADDR highpc;
13765 struct die_info *child_die;
13766 struct attribute *attr, *call_line, *call_file;
13767 const char *name;
13768 CORE_ADDR baseaddr;
13769 struct block *block;
13770 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13771 std::vector<struct symbol *> template_args;
13772 struct template_symbol *templ_func = NULL;
13773
13774 if (inlined_func)
13775 {
13776 /* If we do not have call site information, we can't show the
13777 caller of this inlined function. That's too confusing, so
13778 only use the scope for local variables. */
13779 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13780 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13781 if (call_line == NULL || call_file == NULL)
13782 {
13783 read_lexical_block_scope (die, cu);
13784 return;
13785 }
13786 }
13787
13788 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13789
13790 name = dwarf2_name (die, cu);
13791
13792 /* Ignore functions with missing or empty names. These are actually
13793 illegal according to the DWARF standard. */
13794 if (name == NULL)
13795 {
13796 complaint (&symfile_complaints,
13797 _("missing name for subprogram DIE at %d"),
13798 to_underlying (die->sect_off));
13799 return;
13800 }
13801
13802 /* Ignore functions with missing or invalid low and high pc attributes. */
13803 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13804 <= PC_BOUNDS_INVALID)
13805 {
13806 attr = dwarf2_attr (die, DW_AT_external, cu);
13807 if (!attr || !DW_UNSND (attr))
13808 complaint (&symfile_complaints,
13809 _("cannot get low and high bounds "
13810 "for subprogram DIE at %d"),
13811 to_underlying (die->sect_off));
13812 return;
13813 }
13814
13815 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13816 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13817
13818 /* If we have any template arguments, then we must allocate a
13819 different sort of symbol. */
13820 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13821 {
13822 if (child_die->tag == DW_TAG_template_type_param
13823 || child_die->tag == DW_TAG_template_value_param)
13824 {
13825 templ_func = allocate_template_symbol (objfile);
13826 templ_func->subclass = SYMBOL_TEMPLATE;
13827 break;
13828 }
13829 }
13830
13831 newobj = push_context (0, lowpc);
13832 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13833 (struct symbol *) templ_func);
13834
13835 /* If there is a location expression for DW_AT_frame_base, record
13836 it. */
13837 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13838 if (attr)
13839 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13840
13841 /* If there is a location for the static link, record it. */
13842 newobj->static_link = NULL;
13843 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13844 if (attr)
13845 {
13846 newobj->static_link
13847 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13848 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13849 }
13850
13851 cu->list_in_scope = &local_symbols;
13852
13853 if (die->child != NULL)
13854 {
13855 child_die = die->child;
13856 while (child_die && child_die->tag)
13857 {
13858 if (child_die->tag == DW_TAG_template_type_param
13859 || child_die->tag == DW_TAG_template_value_param)
13860 {
13861 struct symbol *arg = new_symbol (child_die, NULL, cu);
13862
13863 if (arg != NULL)
13864 template_args.push_back (arg);
13865 }
13866 else
13867 process_die (child_die, cu);
13868 child_die = sibling_die (child_die);
13869 }
13870 }
13871
13872 inherit_abstract_dies (die, cu);
13873
13874 /* If we have a DW_AT_specification, we might need to import using
13875 directives from the context of the specification DIE. See the
13876 comment in determine_prefix. */
13877 if (cu->language == language_cplus
13878 && dwarf2_attr (die, DW_AT_specification, cu))
13879 {
13880 struct dwarf2_cu *spec_cu = cu;
13881 struct die_info *spec_die = die_specification (die, &spec_cu);
13882
13883 while (spec_die)
13884 {
13885 child_die = spec_die->child;
13886 while (child_die && child_die->tag)
13887 {
13888 if (child_die->tag == DW_TAG_imported_module)
13889 process_die (child_die, spec_cu);
13890 child_die = sibling_die (child_die);
13891 }
13892
13893 /* In some cases, GCC generates specification DIEs that
13894 themselves contain DW_AT_specification attributes. */
13895 spec_die = die_specification (spec_die, &spec_cu);
13896 }
13897 }
13898
13899 newobj = pop_context ();
13900 /* Make a block for the local symbols within. */
13901 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
13902 newobj->static_link, lowpc, highpc);
13903
13904 /* For C++, set the block's scope. */
13905 if ((cu->language == language_cplus
13906 || cu->language == language_fortran
13907 || cu->language == language_d
13908 || cu->language == language_rust)
13909 && cu->processing_has_namespace_info)
13910 block_set_scope (block, determine_prefix (die, cu),
13911 &objfile->objfile_obstack);
13912
13913 /* If we have address ranges, record them. */
13914 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13915
13916 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
13917
13918 /* Attach template arguments to function. */
13919 if (!template_args.empty ())
13920 {
13921 gdb_assert (templ_func != NULL);
13922
13923 templ_func->n_template_arguments = template_args.size ();
13924 templ_func->template_arguments
13925 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13926 templ_func->n_template_arguments);
13927 memcpy (templ_func->template_arguments,
13928 template_args.data (),
13929 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13930 }
13931
13932 /* In C++, we can have functions nested inside functions (e.g., when
13933 a function declares a class that has methods). This means that
13934 when we finish processing a function scope, we may need to go
13935 back to building a containing block's symbol lists. */
13936 local_symbols = newobj->locals;
13937 local_using_directives = newobj->local_using_directives;
13938
13939 /* If we've finished processing a top-level function, subsequent
13940 symbols go in the file symbol list. */
13941 if (outermost_context_p ())
13942 cu->list_in_scope = &file_symbols;
13943 }
13944
13945 /* Process all the DIES contained within a lexical block scope. Start
13946 a new scope, process the dies, and then close the scope. */
13947
13948 static void
13949 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13950 {
13951 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13952 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13953 struct context_stack *newobj;
13954 CORE_ADDR lowpc, highpc;
13955 struct die_info *child_die;
13956 CORE_ADDR baseaddr;
13957
13958 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13959
13960 /* Ignore blocks with missing or invalid low and high pc attributes. */
13961 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13962 as multiple lexical blocks? Handling children in a sane way would
13963 be nasty. Might be easier to properly extend generic blocks to
13964 describe ranges. */
13965 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13966 {
13967 case PC_BOUNDS_NOT_PRESENT:
13968 /* DW_TAG_lexical_block has no attributes, process its children as if
13969 there was no wrapping by that DW_TAG_lexical_block.
13970 GCC does no longer produces such DWARF since GCC r224161. */
13971 for (child_die = die->child;
13972 child_die != NULL && child_die->tag;
13973 child_die = sibling_die (child_die))
13974 process_die (child_die, cu);
13975 return;
13976 case PC_BOUNDS_INVALID:
13977 return;
13978 }
13979 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13980 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13981
13982 push_context (0, lowpc);
13983 if (die->child != NULL)
13984 {
13985 child_die = die->child;
13986 while (child_die && child_die->tag)
13987 {
13988 process_die (child_die, cu);
13989 child_die = sibling_die (child_die);
13990 }
13991 }
13992 inherit_abstract_dies (die, cu);
13993 newobj = pop_context ();
13994
13995 if (local_symbols != NULL || local_using_directives != NULL)
13996 {
13997 struct block *block
13998 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
13999 newobj->start_addr, highpc);
14000
14001 /* Note that recording ranges after traversing children, as we
14002 do here, means that recording a parent's ranges entails
14003 walking across all its children's ranges as they appear in
14004 the address map, which is quadratic behavior.
14005
14006 It would be nicer to record the parent's ranges before
14007 traversing its children, simply overriding whatever you find
14008 there. But since we don't even decide whether to create a
14009 block until after we've traversed its children, that's hard
14010 to do. */
14011 dwarf2_record_block_ranges (die, block, baseaddr, cu);
14012 }
14013 local_symbols = newobj->locals;
14014 local_using_directives = newobj->local_using_directives;
14015 }
14016
14017 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
14018
14019 static void
14020 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
14021 {
14022 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14023 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14024 CORE_ADDR pc, baseaddr;
14025 struct attribute *attr;
14026 struct call_site *call_site, call_site_local;
14027 void **slot;
14028 int nparams;
14029 struct die_info *child_die;
14030
14031 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14032
14033 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
14034 if (attr == NULL)
14035 {
14036 /* This was a pre-DWARF-5 GNU extension alias
14037 for DW_AT_call_return_pc. */
14038 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14039 }
14040 if (!attr)
14041 {
14042 complaint (&symfile_complaints,
14043 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
14044 "DIE 0x%x [in module %s]"),
14045 to_underlying (die->sect_off), objfile_name (objfile));
14046 return;
14047 }
14048 pc = attr_value_as_address (attr) + baseaddr;
14049 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
14050
14051 if (cu->call_site_htab == NULL)
14052 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14053 NULL, &objfile->objfile_obstack,
14054 hashtab_obstack_allocate, NULL);
14055 call_site_local.pc = pc;
14056 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14057 if (*slot != NULL)
14058 {
14059 complaint (&symfile_complaints,
14060 _("Duplicate PC %s for DW_TAG_call_site "
14061 "DIE 0x%x [in module %s]"),
14062 paddress (gdbarch, pc), to_underlying (die->sect_off),
14063 objfile_name (objfile));
14064 return;
14065 }
14066
14067 /* Count parameters at the caller. */
14068
14069 nparams = 0;
14070 for (child_die = die->child; child_die && child_die->tag;
14071 child_die = sibling_die (child_die))
14072 {
14073 if (child_die->tag != DW_TAG_call_site_parameter
14074 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14075 {
14076 complaint (&symfile_complaints,
14077 _("Tag %d is not DW_TAG_call_site_parameter in "
14078 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14079 child_die->tag, to_underlying (child_die->sect_off),
14080 objfile_name (objfile));
14081 continue;
14082 }
14083
14084 nparams++;
14085 }
14086
14087 call_site
14088 = ((struct call_site *)
14089 obstack_alloc (&objfile->objfile_obstack,
14090 sizeof (*call_site)
14091 + (sizeof (*call_site->parameter) * (nparams - 1))));
14092 *slot = call_site;
14093 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14094 call_site->pc = pc;
14095
14096 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14097 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14098 {
14099 struct die_info *func_die;
14100
14101 /* Skip also over DW_TAG_inlined_subroutine. */
14102 for (func_die = die->parent;
14103 func_die && func_die->tag != DW_TAG_subprogram
14104 && func_die->tag != DW_TAG_subroutine_type;
14105 func_die = func_die->parent);
14106
14107 /* DW_AT_call_all_calls is a superset
14108 of DW_AT_call_all_tail_calls. */
14109 if (func_die
14110 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14111 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14112 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14113 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14114 {
14115 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14116 not complete. But keep CALL_SITE for look ups via call_site_htab,
14117 both the initial caller containing the real return address PC and
14118 the final callee containing the current PC of a chain of tail
14119 calls do not need to have the tail call list complete. But any
14120 function candidate for a virtual tail call frame searched via
14121 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14122 determined unambiguously. */
14123 }
14124 else
14125 {
14126 struct type *func_type = NULL;
14127
14128 if (func_die)
14129 func_type = get_die_type (func_die, cu);
14130 if (func_type != NULL)
14131 {
14132 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14133
14134 /* Enlist this call site to the function. */
14135 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14136 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14137 }
14138 else
14139 complaint (&symfile_complaints,
14140 _("Cannot find function owning DW_TAG_call_site "
14141 "DIE 0x%x [in module %s]"),
14142 to_underlying (die->sect_off), objfile_name (objfile));
14143 }
14144 }
14145
14146 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14147 if (attr == NULL)
14148 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14149 if (attr == NULL)
14150 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14151 if (attr == NULL)
14152 {
14153 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14154 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14155 }
14156 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14157 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14158 /* Keep NULL DWARF_BLOCK. */;
14159 else if (attr_form_is_block (attr))
14160 {
14161 struct dwarf2_locexpr_baton *dlbaton;
14162
14163 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14164 dlbaton->data = DW_BLOCK (attr)->data;
14165 dlbaton->size = DW_BLOCK (attr)->size;
14166 dlbaton->per_cu = cu->per_cu;
14167
14168 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14169 }
14170 else if (attr_form_is_ref (attr))
14171 {
14172 struct dwarf2_cu *target_cu = cu;
14173 struct die_info *target_die;
14174
14175 target_die = follow_die_ref (die, attr, &target_cu);
14176 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14177 if (die_is_declaration (target_die, target_cu))
14178 {
14179 const char *target_physname;
14180
14181 /* Prefer the mangled name; otherwise compute the demangled one. */
14182 target_physname = dw2_linkage_name (target_die, target_cu);
14183 if (target_physname == NULL)
14184 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14185 if (target_physname == NULL)
14186 complaint (&symfile_complaints,
14187 _("DW_AT_call_target target DIE has invalid "
14188 "physname, for referencing DIE 0x%x [in module %s]"),
14189 to_underlying (die->sect_off), objfile_name (objfile));
14190 else
14191 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14192 }
14193 else
14194 {
14195 CORE_ADDR lowpc;
14196
14197 /* DW_AT_entry_pc should be preferred. */
14198 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14199 <= PC_BOUNDS_INVALID)
14200 complaint (&symfile_complaints,
14201 _("DW_AT_call_target target DIE has invalid "
14202 "low pc, for referencing DIE 0x%x [in module %s]"),
14203 to_underlying (die->sect_off), objfile_name (objfile));
14204 else
14205 {
14206 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14207 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14208 }
14209 }
14210 }
14211 else
14212 complaint (&symfile_complaints,
14213 _("DW_TAG_call_site DW_AT_call_target is neither "
14214 "block nor reference, for DIE 0x%x [in module %s]"),
14215 to_underlying (die->sect_off), objfile_name (objfile));
14216
14217 call_site->per_cu = cu->per_cu;
14218
14219 for (child_die = die->child;
14220 child_die && child_die->tag;
14221 child_die = sibling_die (child_die))
14222 {
14223 struct call_site_parameter *parameter;
14224 struct attribute *loc, *origin;
14225
14226 if (child_die->tag != DW_TAG_call_site_parameter
14227 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14228 {
14229 /* Already printed the complaint above. */
14230 continue;
14231 }
14232
14233 gdb_assert (call_site->parameter_count < nparams);
14234 parameter = &call_site->parameter[call_site->parameter_count];
14235
14236 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14237 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14238 register is contained in DW_AT_call_value. */
14239
14240 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14241 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14242 if (origin == NULL)
14243 {
14244 /* This was a pre-DWARF-5 GNU extension alias
14245 for DW_AT_call_parameter. */
14246 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14247 }
14248 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14249 {
14250 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14251
14252 sect_offset sect_off
14253 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14254 if (!offset_in_cu_p (&cu->header, sect_off))
14255 {
14256 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14257 binding can be done only inside one CU. Such referenced DIE
14258 therefore cannot be even moved to DW_TAG_partial_unit. */
14259 complaint (&symfile_complaints,
14260 _("DW_AT_call_parameter offset is not in CU for "
14261 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14262 to_underlying (child_die->sect_off),
14263 objfile_name (objfile));
14264 continue;
14265 }
14266 parameter->u.param_cu_off
14267 = (cu_offset) (sect_off - cu->header.sect_off);
14268 }
14269 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14270 {
14271 complaint (&symfile_complaints,
14272 _("No DW_FORM_block* DW_AT_location for "
14273 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14274 to_underlying (child_die->sect_off), objfile_name (objfile));
14275 continue;
14276 }
14277 else
14278 {
14279 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14280 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14281 if (parameter->u.dwarf_reg != -1)
14282 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14283 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14284 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14285 &parameter->u.fb_offset))
14286 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14287 else
14288 {
14289 complaint (&symfile_complaints,
14290 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14291 "for DW_FORM_block* DW_AT_location is supported for "
14292 "DW_TAG_call_site child DIE 0x%x "
14293 "[in module %s]"),
14294 to_underlying (child_die->sect_off),
14295 objfile_name (objfile));
14296 continue;
14297 }
14298 }
14299
14300 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14301 if (attr == NULL)
14302 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14303 if (!attr_form_is_block (attr))
14304 {
14305 complaint (&symfile_complaints,
14306 _("No DW_FORM_block* DW_AT_call_value for "
14307 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14308 to_underlying (child_die->sect_off),
14309 objfile_name (objfile));
14310 continue;
14311 }
14312 parameter->value = DW_BLOCK (attr)->data;
14313 parameter->value_size = DW_BLOCK (attr)->size;
14314
14315 /* Parameters are not pre-cleared by memset above. */
14316 parameter->data_value = NULL;
14317 parameter->data_value_size = 0;
14318 call_site->parameter_count++;
14319
14320 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14321 if (attr == NULL)
14322 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14323 if (attr)
14324 {
14325 if (!attr_form_is_block (attr))
14326 complaint (&symfile_complaints,
14327 _("No DW_FORM_block* DW_AT_call_data_value for "
14328 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14329 to_underlying (child_die->sect_off),
14330 objfile_name (objfile));
14331 else
14332 {
14333 parameter->data_value = DW_BLOCK (attr)->data;
14334 parameter->data_value_size = DW_BLOCK (attr)->size;
14335 }
14336 }
14337 }
14338 }
14339
14340 /* Helper function for read_variable. If DIE represents a virtual
14341 table, then return the type of the concrete object that is
14342 associated with the virtual table. Otherwise, return NULL. */
14343
14344 static struct type *
14345 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14346 {
14347 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14348 if (attr == NULL)
14349 return NULL;
14350
14351 /* Find the type DIE. */
14352 struct die_info *type_die = NULL;
14353 struct dwarf2_cu *type_cu = cu;
14354
14355 if (attr_form_is_ref (attr))
14356 type_die = follow_die_ref (die, attr, &type_cu);
14357 if (type_die == NULL)
14358 return NULL;
14359
14360 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14361 return NULL;
14362 return die_containing_type (type_die, type_cu);
14363 }
14364
14365 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14366
14367 static void
14368 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14369 {
14370 struct rust_vtable_symbol *storage = NULL;
14371
14372 if (cu->language == language_rust)
14373 {
14374 struct type *containing_type = rust_containing_type (die, cu);
14375
14376 if (containing_type != NULL)
14377 {
14378 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14379
14380 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14381 struct rust_vtable_symbol);
14382 initialize_objfile_symbol (storage);
14383 storage->concrete_type = containing_type;
14384 storage->subclass = SYMBOL_RUST_VTABLE;
14385 }
14386 }
14387
14388 new_symbol (die, NULL, cu, storage);
14389 }
14390
14391 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14392 reading .debug_rnglists.
14393 Callback's type should be:
14394 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14395 Return true if the attributes are present and valid, otherwise,
14396 return false. */
14397
14398 template <typename Callback>
14399 static bool
14400 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14401 Callback &&callback)
14402 {
14403 struct dwarf2_per_objfile *dwarf2_per_objfile
14404 = cu->per_cu->dwarf2_per_objfile;
14405 struct objfile *objfile = dwarf2_per_objfile->objfile;
14406 bfd *obfd = objfile->obfd;
14407 /* Base address selection entry. */
14408 CORE_ADDR base;
14409 int found_base;
14410 const gdb_byte *buffer;
14411 CORE_ADDR baseaddr;
14412 bool overflow = false;
14413
14414 found_base = cu->base_known;
14415 base = cu->base_address;
14416
14417 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14418 if (offset >= dwarf2_per_objfile->rnglists.size)
14419 {
14420 complaint (&symfile_complaints,
14421 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14422 offset);
14423 return false;
14424 }
14425 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14426
14427 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14428
14429 while (1)
14430 {
14431 /* Initialize it due to a false compiler warning. */
14432 CORE_ADDR range_beginning = 0, range_end = 0;
14433 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14434 + dwarf2_per_objfile->rnglists.size);
14435 unsigned int bytes_read;
14436
14437 if (buffer == buf_end)
14438 {
14439 overflow = true;
14440 break;
14441 }
14442 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14443 switch (rlet)
14444 {
14445 case DW_RLE_end_of_list:
14446 break;
14447 case DW_RLE_base_address:
14448 if (buffer + cu->header.addr_size > buf_end)
14449 {
14450 overflow = true;
14451 break;
14452 }
14453 base = read_address (obfd, buffer, cu, &bytes_read);
14454 found_base = 1;
14455 buffer += bytes_read;
14456 break;
14457 case DW_RLE_start_length:
14458 if (buffer + cu->header.addr_size > buf_end)
14459 {
14460 overflow = true;
14461 break;
14462 }
14463 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14464 buffer += bytes_read;
14465 range_end = (range_beginning
14466 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14467 buffer += bytes_read;
14468 if (buffer > buf_end)
14469 {
14470 overflow = true;
14471 break;
14472 }
14473 break;
14474 case DW_RLE_offset_pair:
14475 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14476 buffer += bytes_read;
14477 if (buffer > buf_end)
14478 {
14479 overflow = true;
14480 break;
14481 }
14482 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14483 buffer += bytes_read;
14484 if (buffer > buf_end)
14485 {
14486 overflow = true;
14487 break;
14488 }
14489 break;
14490 case DW_RLE_start_end:
14491 if (buffer + 2 * cu->header.addr_size > buf_end)
14492 {
14493 overflow = true;
14494 break;
14495 }
14496 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14497 buffer += bytes_read;
14498 range_end = read_address (obfd, buffer, cu, &bytes_read);
14499 buffer += bytes_read;
14500 break;
14501 default:
14502 complaint (&symfile_complaints,
14503 _("Invalid .debug_rnglists data (no base address)"));
14504 return false;
14505 }
14506 if (rlet == DW_RLE_end_of_list || overflow)
14507 break;
14508 if (rlet == DW_RLE_base_address)
14509 continue;
14510
14511 if (!found_base)
14512 {
14513 /* We have no valid base address for the ranges
14514 data. */
14515 complaint (&symfile_complaints,
14516 _("Invalid .debug_rnglists data (no base address)"));
14517 return false;
14518 }
14519
14520 if (range_beginning > range_end)
14521 {
14522 /* Inverted range entries are invalid. */
14523 complaint (&symfile_complaints,
14524 _("Invalid .debug_rnglists data (inverted range)"));
14525 return false;
14526 }
14527
14528 /* Empty range entries have no effect. */
14529 if (range_beginning == range_end)
14530 continue;
14531
14532 range_beginning += base;
14533 range_end += base;
14534
14535 /* A not-uncommon case of bad debug info.
14536 Don't pollute the addrmap with bad data. */
14537 if (range_beginning + baseaddr == 0
14538 && !dwarf2_per_objfile->has_section_at_zero)
14539 {
14540 complaint (&symfile_complaints,
14541 _(".debug_rnglists entry has start address of zero"
14542 " [in module %s]"), objfile_name (objfile));
14543 continue;
14544 }
14545
14546 callback (range_beginning, range_end);
14547 }
14548
14549 if (overflow)
14550 {
14551 complaint (&symfile_complaints,
14552 _("Offset %d is not terminated "
14553 "for DW_AT_ranges attribute"),
14554 offset);
14555 return false;
14556 }
14557
14558 return true;
14559 }
14560
14561 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14562 Callback's type should be:
14563 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14564 Return 1 if the attributes are present and valid, otherwise, return 0. */
14565
14566 template <typename Callback>
14567 static int
14568 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14569 Callback &&callback)
14570 {
14571 struct dwarf2_per_objfile *dwarf2_per_objfile
14572 = cu->per_cu->dwarf2_per_objfile;
14573 struct objfile *objfile = dwarf2_per_objfile->objfile;
14574 struct comp_unit_head *cu_header = &cu->header;
14575 bfd *obfd = objfile->obfd;
14576 unsigned int addr_size = cu_header->addr_size;
14577 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14578 /* Base address selection entry. */
14579 CORE_ADDR base;
14580 int found_base;
14581 unsigned int dummy;
14582 const gdb_byte *buffer;
14583 CORE_ADDR baseaddr;
14584
14585 if (cu_header->version >= 5)
14586 return dwarf2_rnglists_process (offset, cu, callback);
14587
14588 found_base = cu->base_known;
14589 base = cu->base_address;
14590
14591 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14592 if (offset >= dwarf2_per_objfile->ranges.size)
14593 {
14594 complaint (&symfile_complaints,
14595 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14596 offset);
14597 return 0;
14598 }
14599 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14600
14601 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14602
14603 while (1)
14604 {
14605 CORE_ADDR range_beginning, range_end;
14606
14607 range_beginning = read_address (obfd, buffer, cu, &dummy);
14608 buffer += addr_size;
14609 range_end = read_address (obfd, buffer, cu, &dummy);
14610 buffer += addr_size;
14611 offset += 2 * addr_size;
14612
14613 /* An end of list marker is a pair of zero addresses. */
14614 if (range_beginning == 0 && range_end == 0)
14615 /* Found the end of list entry. */
14616 break;
14617
14618 /* Each base address selection entry is a pair of 2 values.
14619 The first is the largest possible address, the second is
14620 the base address. Check for a base address here. */
14621 if ((range_beginning & mask) == mask)
14622 {
14623 /* If we found the largest possible address, then we already
14624 have the base address in range_end. */
14625 base = range_end;
14626 found_base = 1;
14627 continue;
14628 }
14629
14630 if (!found_base)
14631 {
14632 /* We have no valid base address for the ranges
14633 data. */
14634 complaint (&symfile_complaints,
14635 _("Invalid .debug_ranges data (no base address)"));
14636 return 0;
14637 }
14638
14639 if (range_beginning > range_end)
14640 {
14641 /* Inverted range entries are invalid. */
14642 complaint (&symfile_complaints,
14643 _("Invalid .debug_ranges data (inverted range)"));
14644 return 0;
14645 }
14646
14647 /* Empty range entries have no effect. */
14648 if (range_beginning == range_end)
14649 continue;
14650
14651 range_beginning += base;
14652 range_end += base;
14653
14654 /* A not-uncommon case of bad debug info.
14655 Don't pollute the addrmap with bad data. */
14656 if (range_beginning + baseaddr == 0
14657 && !dwarf2_per_objfile->has_section_at_zero)
14658 {
14659 complaint (&symfile_complaints,
14660 _(".debug_ranges entry has start address of zero"
14661 " [in module %s]"), objfile_name (objfile));
14662 continue;
14663 }
14664
14665 callback (range_beginning, range_end);
14666 }
14667
14668 return 1;
14669 }
14670
14671 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14672 Return 1 if the attributes are present and valid, otherwise, return 0.
14673 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14674
14675 static int
14676 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14677 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14678 struct partial_symtab *ranges_pst)
14679 {
14680 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14681 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14682 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14683 SECT_OFF_TEXT (objfile));
14684 int low_set = 0;
14685 CORE_ADDR low = 0;
14686 CORE_ADDR high = 0;
14687 int retval;
14688
14689 retval = dwarf2_ranges_process (offset, cu,
14690 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14691 {
14692 if (ranges_pst != NULL)
14693 {
14694 CORE_ADDR lowpc;
14695 CORE_ADDR highpc;
14696
14697 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14698 range_beginning + baseaddr);
14699 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14700 range_end + baseaddr);
14701 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14702 ranges_pst);
14703 }
14704
14705 /* FIXME: This is recording everything as a low-high
14706 segment of consecutive addresses. We should have a
14707 data structure for discontiguous block ranges
14708 instead. */
14709 if (! low_set)
14710 {
14711 low = range_beginning;
14712 high = range_end;
14713 low_set = 1;
14714 }
14715 else
14716 {
14717 if (range_beginning < low)
14718 low = range_beginning;
14719 if (range_end > high)
14720 high = range_end;
14721 }
14722 });
14723 if (!retval)
14724 return 0;
14725
14726 if (! low_set)
14727 /* If the first entry is an end-of-list marker, the range
14728 describes an empty scope, i.e. no instructions. */
14729 return 0;
14730
14731 if (low_return)
14732 *low_return = low;
14733 if (high_return)
14734 *high_return = high;
14735 return 1;
14736 }
14737
14738 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14739 definition for the return value. *LOWPC and *HIGHPC are set iff
14740 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14741
14742 static enum pc_bounds_kind
14743 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14744 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14745 struct partial_symtab *pst)
14746 {
14747 struct dwarf2_per_objfile *dwarf2_per_objfile
14748 = cu->per_cu->dwarf2_per_objfile;
14749 struct attribute *attr;
14750 struct attribute *attr_high;
14751 CORE_ADDR low = 0;
14752 CORE_ADDR high = 0;
14753 enum pc_bounds_kind ret;
14754
14755 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14756 if (attr_high)
14757 {
14758 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14759 if (attr)
14760 {
14761 low = attr_value_as_address (attr);
14762 high = attr_value_as_address (attr_high);
14763 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14764 high += low;
14765 }
14766 else
14767 /* Found high w/o low attribute. */
14768 return PC_BOUNDS_INVALID;
14769
14770 /* Found consecutive range of addresses. */
14771 ret = PC_BOUNDS_HIGH_LOW;
14772 }
14773 else
14774 {
14775 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14776 if (attr != NULL)
14777 {
14778 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14779 We take advantage of the fact that DW_AT_ranges does not appear
14780 in DW_TAG_compile_unit of DWO files. */
14781 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14782 unsigned int ranges_offset = (DW_UNSND (attr)
14783 + (need_ranges_base
14784 ? cu->ranges_base
14785 : 0));
14786
14787 /* Value of the DW_AT_ranges attribute is the offset in the
14788 .debug_ranges section. */
14789 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14790 return PC_BOUNDS_INVALID;
14791 /* Found discontinuous range of addresses. */
14792 ret = PC_BOUNDS_RANGES;
14793 }
14794 else
14795 return PC_BOUNDS_NOT_PRESENT;
14796 }
14797
14798 /* read_partial_die has also the strict LOW < HIGH requirement. */
14799 if (high <= low)
14800 return PC_BOUNDS_INVALID;
14801
14802 /* When using the GNU linker, .gnu.linkonce. sections are used to
14803 eliminate duplicate copies of functions and vtables and such.
14804 The linker will arbitrarily choose one and discard the others.
14805 The AT_*_pc values for such functions refer to local labels in
14806 these sections. If the section from that file was discarded, the
14807 labels are not in the output, so the relocs get a value of 0.
14808 If this is a discarded function, mark the pc bounds as invalid,
14809 so that GDB will ignore it. */
14810 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14811 return PC_BOUNDS_INVALID;
14812
14813 *lowpc = low;
14814 if (highpc)
14815 *highpc = high;
14816 return ret;
14817 }
14818
14819 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14820 its low and high PC addresses. Do nothing if these addresses could not
14821 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14822 and HIGHPC to the high address if greater than HIGHPC. */
14823
14824 static void
14825 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14826 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14827 struct dwarf2_cu *cu)
14828 {
14829 CORE_ADDR low, high;
14830 struct die_info *child = die->child;
14831
14832 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14833 {
14834 *lowpc = std::min (*lowpc, low);
14835 *highpc = std::max (*highpc, high);
14836 }
14837
14838 /* If the language does not allow nested subprograms (either inside
14839 subprograms or lexical blocks), we're done. */
14840 if (cu->language != language_ada)
14841 return;
14842
14843 /* Check all the children of the given DIE. If it contains nested
14844 subprograms, then check their pc bounds. Likewise, we need to
14845 check lexical blocks as well, as they may also contain subprogram
14846 definitions. */
14847 while (child && child->tag)
14848 {
14849 if (child->tag == DW_TAG_subprogram
14850 || child->tag == DW_TAG_lexical_block)
14851 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14852 child = sibling_die (child);
14853 }
14854 }
14855
14856 /* Get the low and high pc's represented by the scope DIE, and store
14857 them in *LOWPC and *HIGHPC. If the correct values can't be
14858 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14859
14860 static void
14861 get_scope_pc_bounds (struct die_info *die,
14862 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14863 struct dwarf2_cu *cu)
14864 {
14865 CORE_ADDR best_low = (CORE_ADDR) -1;
14866 CORE_ADDR best_high = (CORE_ADDR) 0;
14867 CORE_ADDR current_low, current_high;
14868
14869 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14870 >= PC_BOUNDS_RANGES)
14871 {
14872 best_low = current_low;
14873 best_high = current_high;
14874 }
14875 else
14876 {
14877 struct die_info *child = die->child;
14878
14879 while (child && child->tag)
14880 {
14881 switch (child->tag) {
14882 case DW_TAG_subprogram:
14883 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14884 break;
14885 case DW_TAG_namespace:
14886 case DW_TAG_module:
14887 /* FIXME: carlton/2004-01-16: Should we do this for
14888 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14889 that current GCC's always emit the DIEs corresponding
14890 to definitions of methods of classes as children of a
14891 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14892 the DIEs giving the declarations, which could be
14893 anywhere). But I don't see any reason why the
14894 standards says that they have to be there. */
14895 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14896
14897 if (current_low != ((CORE_ADDR) -1))
14898 {
14899 best_low = std::min (best_low, current_low);
14900 best_high = std::max (best_high, current_high);
14901 }
14902 break;
14903 default:
14904 /* Ignore. */
14905 break;
14906 }
14907
14908 child = sibling_die (child);
14909 }
14910 }
14911
14912 *lowpc = best_low;
14913 *highpc = best_high;
14914 }
14915
14916 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14917 in DIE. */
14918
14919 static void
14920 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14921 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14922 {
14923 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14924 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14925 struct attribute *attr;
14926 struct attribute *attr_high;
14927
14928 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14929 if (attr_high)
14930 {
14931 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14932 if (attr)
14933 {
14934 CORE_ADDR low = attr_value_as_address (attr);
14935 CORE_ADDR high = attr_value_as_address (attr_high);
14936
14937 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14938 high += low;
14939
14940 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14941 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14942 record_block_range (block, low, high - 1);
14943 }
14944 }
14945
14946 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14947 if (attr)
14948 {
14949 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14950 We take advantage of the fact that DW_AT_ranges does not appear
14951 in DW_TAG_compile_unit of DWO files. */
14952 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14953
14954 /* The value of the DW_AT_ranges attribute is the offset of the
14955 address range list in the .debug_ranges section. */
14956 unsigned long offset = (DW_UNSND (attr)
14957 + (need_ranges_base ? cu->ranges_base : 0));
14958 const gdb_byte *buffer;
14959
14960 /* For some target architectures, but not others, the
14961 read_address function sign-extends the addresses it returns.
14962 To recognize base address selection entries, we need a
14963 mask. */
14964 unsigned int addr_size = cu->header.addr_size;
14965 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14966
14967 /* The base address, to which the next pair is relative. Note
14968 that this 'base' is a DWARF concept: most entries in a range
14969 list are relative, to reduce the number of relocs against the
14970 debugging information. This is separate from this function's
14971 'baseaddr' argument, which GDB uses to relocate debugging
14972 information from a shared library based on the address at
14973 which the library was loaded. */
14974 CORE_ADDR base = cu->base_address;
14975 int base_known = cu->base_known;
14976
14977 dwarf2_ranges_process (offset, cu,
14978 [&] (CORE_ADDR start, CORE_ADDR end)
14979 {
14980 start += baseaddr;
14981 end += baseaddr;
14982 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14983 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14984 record_block_range (block, start, end - 1);
14985 });
14986 }
14987 }
14988
14989 /* Check whether the producer field indicates either of GCC < 4.6, or the
14990 Intel C/C++ compiler, and cache the result in CU. */
14991
14992 static void
14993 check_producer (struct dwarf2_cu *cu)
14994 {
14995 int major, minor;
14996
14997 if (cu->producer == NULL)
14998 {
14999 /* For unknown compilers expect their behavior is DWARF version
15000 compliant.
15001
15002 GCC started to support .debug_types sections by -gdwarf-4 since
15003 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
15004 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
15005 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
15006 interpreted incorrectly by GDB now - GCC PR debug/48229. */
15007 }
15008 else if (producer_is_gcc (cu->producer, &major, &minor))
15009 {
15010 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
15011 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
15012 }
15013 else if (producer_is_icc (cu->producer, &major, &minor))
15014 cu->producer_is_icc_lt_14 = major < 14;
15015 else
15016 {
15017 /* For other non-GCC compilers, expect their behavior is DWARF version
15018 compliant. */
15019 }
15020
15021 cu->checked_producer = 1;
15022 }
15023
15024 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
15025 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
15026 during 4.6.0 experimental. */
15027
15028 static int
15029 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
15030 {
15031 if (!cu->checked_producer)
15032 check_producer (cu);
15033
15034 return cu->producer_is_gxx_lt_4_6;
15035 }
15036
15037 /* Return the default accessibility type if it is not overriden by
15038 DW_AT_accessibility. */
15039
15040 static enum dwarf_access_attribute
15041 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15042 {
15043 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15044 {
15045 /* The default DWARF 2 accessibility for members is public, the default
15046 accessibility for inheritance is private. */
15047
15048 if (die->tag != DW_TAG_inheritance)
15049 return DW_ACCESS_public;
15050 else
15051 return DW_ACCESS_private;
15052 }
15053 else
15054 {
15055 /* DWARF 3+ defines the default accessibility a different way. The same
15056 rules apply now for DW_TAG_inheritance as for the members and it only
15057 depends on the container kind. */
15058
15059 if (die->parent->tag == DW_TAG_class_type)
15060 return DW_ACCESS_private;
15061 else
15062 return DW_ACCESS_public;
15063 }
15064 }
15065
15066 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15067 offset. If the attribute was not found return 0, otherwise return
15068 1. If it was found but could not properly be handled, set *OFFSET
15069 to 0. */
15070
15071 static int
15072 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15073 LONGEST *offset)
15074 {
15075 struct attribute *attr;
15076
15077 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15078 if (attr != NULL)
15079 {
15080 *offset = 0;
15081
15082 /* Note that we do not check for a section offset first here.
15083 This is because DW_AT_data_member_location is new in DWARF 4,
15084 so if we see it, we can assume that a constant form is really
15085 a constant and not a section offset. */
15086 if (attr_form_is_constant (attr))
15087 *offset = dwarf2_get_attr_constant_value (attr, 0);
15088 else if (attr_form_is_section_offset (attr))
15089 dwarf2_complex_location_expr_complaint ();
15090 else if (attr_form_is_block (attr))
15091 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15092 else
15093 dwarf2_complex_location_expr_complaint ();
15094
15095 return 1;
15096 }
15097
15098 return 0;
15099 }
15100
15101 /* Add an aggregate field to the field list. */
15102
15103 static void
15104 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15105 struct dwarf2_cu *cu)
15106 {
15107 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15108 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15109 struct nextfield *new_field;
15110 struct attribute *attr;
15111 struct field *fp;
15112 const char *fieldname = "";
15113
15114 /* Allocate a new field list entry and link it in. */
15115 new_field = XNEW (struct nextfield);
15116 make_cleanup (xfree, new_field);
15117 memset (new_field, 0, sizeof (struct nextfield));
15118
15119 if (die->tag == DW_TAG_inheritance)
15120 {
15121 new_field->next = fip->baseclasses;
15122 fip->baseclasses = new_field;
15123 }
15124 else
15125 {
15126 new_field->next = fip->fields;
15127 fip->fields = new_field;
15128 }
15129 fip->nfields++;
15130
15131 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15132 if (attr)
15133 new_field->accessibility = DW_UNSND (attr);
15134 else
15135 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15136 if (new_field->accessibility != DW_ACCESS_public)
15137 fip->non_public_fields = 1;
15138
15139 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15140 if (attr)
15141 new_field->virtuality = DW_UNSND (attr);
15142 else
15143 new_field->virtuality = DW_VIRTUALITY_none;
15144
15145 fp = &new_field->field;
15146
15147 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15148 {
15149 LONGEST offset;
15150
15151 /* Data member other than a C++ static data member. */
15152
15153 /* Get type of field. */
15154 fp->type = die_type (die, cu);
15155
15156 SET_FIELD_BITPOS (*fp, 0);
15157
15158 /* Get bit size of field (zero if none). */
15159 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15160 if (attr)
15161 {
15162 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15163 }
15164 else
15165 {
15166 FIELD_BITSIZE (*fp) = 0;
15167 }
15168
15169 /* Get bit offset of field. */
15170 if (handle_data_member_location (die, cu, &offset))
15171 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15172 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15173 if (attr)
15174 {
15175 if (gdbarch_bits_big_endian (gdbarch))
15176 {
15177 /* For big endian bits, the DW_AT_bit_offset gives the
15178 additional bit offset from the MSB of the containing
15179 anonymous object to the MSB of the field. We don't
15180 have to do anything special since we don't need to
15181 know the size of the anonymous object. */
15182 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15183 }
15184 else
15185 {
15186 /* For little endian bits, compute the bit offset to the
15187 MSB of the anonymous object, subtract off the number of
15188 bits from the MSB of the field to the MSB of the
15189 object, and then subtract off the number of bits of
15190 the field itself. The result is the bit offset of
15191 the LSB of the field. */
15192 int anonymous_size;
15193 int bit_offset = DW_UNSND (attr);
15194
15195 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15196 if (attr)
15197 {
15198 /* The size of the anonymous object containing
15199 the bit field is explicit, so use the
15200 indicated size (in bytes). */
15201 anonymous_size = DW_UNSND (attr);
15202 }
15203 else
15204 {
15205 /* The size of the anonymous object containing
15206 the bit field must be inferred from the type
15207 attribute of the data member containing the
15208 bit field. */
15209 anonymous_size = TYPE_LENGTH (fp->type);
15210 }
15211 SET_FIELD_BITPOS (*fp,
15212 (FIELD_BITPOS (*fp)
15213 + anonymous_size * bits_per_byte
15214 - bit_offset - FIELD_BITSIZE (*fp)));
15215 }
15216 }
15217 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15218 if (attr != NULL)
15219 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15220 + dwarf2_get_attr_constant_value (attr, 0)));
15221
15222 /* Get name of field. */
15223 fieldname = dwarf2_name (die, cu);
15224 if (fieldname == NULL)
15225 fieldname = "";
15226
15227 /* The name is already allocated along with this objfile, so we don't
15228 need to duplicate it for the type. */
15229 fp->name = fieldname;
15230
15231 /* Change accessibility for artificial fields (e.g. virtual table
15232 pointer or virtual base class pointer) to private. */
15233 if (dwarf2_attr (die, DW_AT_artificial, cu))
15234 {
15235 FIELD_ARTIFICIAL (*fp) = 1;
15236 new_field->accessibility = DW_ACCESS_private;
15237 fip->non_public_fields = 1;
15238 }
15239 }
15240 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15241 {
15242 /* C++ static member. */
15243
15244 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15245 is a declaration, but all versions of G++ as of this writing
15246 (so through at least 3.2.1) incorrectly generate
15247 DW_TAG_variable tags. */
15248
15249 const char *physname;
15250
15251 /* Get name of field. */
15252 fieldname = dwarf2_name (die, cu);
15253 if (fieldname == NULL)
15254 return;
15255
15256 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15257 if (attr
15258 /* Only create a symbol if this is an external value.
15259 new_symbol checks this and puts the value in the global symbol
15260 table, which we want. If it is not external, new_symbol
15261 will try to put the value in cu->list_in_scope which is wrong. */
15262 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15263 {
15264 /* A static const member, not much different than an enum as far as
15265 we're concerned, except that we can support more types. */
15266 new_symbol (die, NULL, cu);
15267 }
15268
15269 /* Get physical name. */
15270 physname = dwarf2_physname (fieldname, die, cu);
15271
15272 /* The name is already allocated along with this objfile, so we don't
15273 need to duplicate it for the type. */
15274 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15275 FIELD_TYPE (*fp) = die_type (die, cu);
15276 FIELD_NAME (*fp) = fieldname;
15277 }
15278 else if (die->tag == DW_TAG_inheritance)
15279 {
15280 LONGEST offset;
15281
15282 /* C++ base class field. */
15283 if (handle_data_member_location (die, cu, &offset))
15284 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15285 FIELD_BITSIZE (*fp) = 0;
15286 FIELD_TYPE (*fp) = die_type (die, cu);
15287 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15288 fip->nbaseclasses++;
15289 }
15290 }
15291
15292 /* Can the type given by DIE define another type? */
15293
15294 static bool
15295 type_can_define_types (const struct die_info *die)
15296 {
15297 switch (die->tag)
15298 {
15299 case DW_TAG_typedef:
15300 case DW_TAG_class_type:
15301 case DW_TAG_structure_type:
15302 case DW_TAG_union_type:
15303 case DW_TAG_enumeration_type:
15304 return true;
15305
15306 default:
15307 return false;
15308 }
15309 }
15310
15311 /* Add a type definition defined in the scope of the FIP's class. */
15312
15313 static void
15314 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15315 struct dwarf2_cu *cu)
15316 {
15317 struct decl_field_list *new_field;
15318 struct decl_field *fp;
15319
15320 /* Allocate a new field list entry and link it in. */
15321 new_field = XCNEW (struct decl_field_list);
15322 make_cleanup (xfree, new_field);
15323
15324 gdb_assert (type_can_define_types (die));
15325
15326 fp = &new_field->field;
15327
15328 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15329 fp->name = dwarf2_name (die, cu);
15330 fp->type = read_type_die (die, cu);
15331
15332 /* Save accessibility. */
15333 enum dwarf_access_attribute accessibility;
15334 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15335 if (attr != NULL)
15336 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15337 else
15338 accessibility = dwarf2_default_access_attribute (die, cu);
15339 switch (accessibility)
15340 {
15341 case DW_ACCESS_public:
15342 /* The assumed value if neither private nor protected. */
15343 break;
15344 case DW_ACCESS_private:
15345 fp->is_private = 1;
15346 break;
15347 case DW_ACCESS_protected:
15348 fp->is_protected = 1;
15349 break;
15350 default:
15351 complaint (&symfile_complaints,
15352 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15353 }
15354
15355 if (die->tag == DW_TAG_typedef)
15356 {
15357 new_field->next = fip->typedef_field_list;
15358 fip->typedef_field_list = new_field;
15359 fip->typedef_field_list_count++;
15360 }
15361 else
15362 {
15363 new_field->next = fip->nested_types_list;
15364 fip->nested_types_list = new_field;
15365 fip->nested_types_list_count++;
15366 }
15367 }
15368
15369 /* Create the vector of fields, and attach it to the type. */
15370
15371 static void
15372 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15373 struct dwarf2_cu *cu)
15374 {
15375 int nfields = fip->nfields;
15376
15377 /* Record the field count, allocate space for the array of fields,
15378 and create blank accessibility bitfields if necessary. */
15379 TYPE_NFIELDS (type) = nfields;
15380 TYPE_FIELDS (type) = (struct field *)
15381 TYPE_ALLOC (type, sizeof (struct field) * nfields);
15382 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
15383
15384 if (fip->non_public_fields && cu->language != language_ada)
15385 {
15386 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15387
15388 TYPE_FIELD_PRIVATE_BITS (type) =
15389 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15390 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15391
15392 TYPE_FIELD_PROTECTED_BITS (type) =
15393 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15394 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15395
15396 TYPE_FIELD_IGNORE_BITS (type) =
15397 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15398 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15399 }
15400
15401 /* If the type has baseclasses, allocate and clear a bit vector for
15402 TYPE_FIELD_VIRTUAL_BITS. */
15403 if (fip->nbaseclasses && cu->language != language_ada)
15404 {
15405 int num_bytes = B_BYTES (fip->nbaseclasses);
15406 unsigned char *pointer;
15407
15408 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15409 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15410 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15411 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
15412 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
15413 }
15414
15415 /* Copy the saved-up fields into the field vector. Start from the head of
15416 the list, adding to the tail of the field array, so that they end up in
15417 the same order in the array in which they were added to the list. */
15418 while (nfields-- > 0)
15419 {
15420 struct nextfield *fieldp;
15421
15422 if (fip->fields)
15423 {
15424 fieldp = fip->fields;
15425 fip->fields = fieldp->next;
15426 }
15427 else
15428 {
15429 fieldp = fip->baseclasses;
15430 fip->baseclasses = fieldp->next;
15431 }
15432
15433 TYPE_FIELD (type, nfields) = fieldp->field;
15434 switch (fieldp->accessibility)
15435 {
15436 case DW_ACCESS_private:
15437 if (cu->language != language_ada)
15438 SET_TYPE_FIELD_PRIVATE (type, nfields);
15439 break;
15440
15441 case DW_ACCESS_protected:
15442 if (cu->language != language_ada)
15443 SET_TYPE_FIELD_PROTECTED (type, nfields);
15444 break;
15445
15446 case DW_ACCESS_public:
15447 break;
15448
15449 default:
15450 /* Unknown accessibility. Complain and treat it as public. */
15451 {
15452 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15453 fieldp->accessibility);
15454 }
15455 break;
15456 }
15457 if (nfields < fip->nbaseclasses)
15458 {
15459 switch (fieldp->virtuality)
15460 {
15461 case DW_VIRTUALITY_virtual:
15462 case DW_VIRTUALITY_pure_virtual:
15463 if (cu->language == language_ada)
15464 error (_("unexpected virtuality in component of Ada type"));
15465 SET_TYPE_FIELD_VIRTUAL (type, nfields);
15466 break;
15467 }
15468 }
15469 }
15470 }
15471
15472 /* Return true if this member function is a constructor, false
15473 otherwise. */
15474
15475 static int
15476 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15477 {
15478 const char *fieldname;
15479 const char *type_name;
15480 int len;
15481
15482 if (die->parent == NULL)
15483 return 0;
15484
15485 if (die->parent->tag != DW_TAG_structure_type
15486 && die->parent->tag != DW_TAG_union_type
15487 && die->parent->tag != DW_TAG_class_type)
15488 return 0;
15489
15490 fieldname = dwarf2_name (die, cu);
15491 type_name = dwarf2_name (die->parent, cu);
15492 if (fieldname == NULL || type_name == NULL)
15493 return 0;
15494
15495 len = strlen (fieldname);
15496 return (strncmp (fieldname, type_name, len) == 0
15497 && (type_name[len] == '\0' || type_name[len] == '<'));
15498 }
15499
15500 /* Add a member function to the proper fieldlist. */
15501
15502 static void
15503 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15504 struct type *type, struct dwarf2_cu *cu)
15505 {
15506 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15507 struct attribute *attr;
15508 struct fnfieldlist *flp;
15509 int i;
15510 struct fn_field *fnp;
15511 const char *fieldname;
15512 struct nextfnfield *new_fnfield;
15513 struct type *this_type;
15514 enum dwarf_access_attribute accessibility;
15515
15516 if (cu->language == language_ada)
15517 error (_("unexpected member function in Ada type"));
15518
15519 /* Get name of member function. */
15520 fieldname = dwarf2_name (die, cu);
15521 if (fieldname == NULL)
15522 return;
15523
15524 /* Look up member function name in fieldlist. */
15525 for (i = 0; i < fip->nfnfields; i++)
15526 {
15527 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15528 break;
15529 }
15530
15531 /* Create new list element if necessary. */
15532 if (i < fip->nfnfields)
15533 flp = &fip->fnfieldlists[i];
15534 else
15535 {
15536 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
15537 {
15538 fip->fnfieldlists = (struct fnfieldlist *)
15539 xrealloc (fip->fnfieldlists,
15540 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
15541 * sizeof (struct fnfieldlist));
15542 if (fip->nfnfields == 0)
15543 make_cleanup (free_current_contents, &fip->fnfieldlists);
15544 }
15545 flp = &fip->fnfieldlists[fip->nfnfields];
15546 flp->name = fieldname;
15547 flp->length = 0;
15548 flp->head = NULL;
15549 i = fip->nfnfields++;
15550 }
15551
15552 /* Create a new member function field and chain it to the field list
15553 entry. */
15554 new_fnfield = XNEW (struct nextfnfield);
15555 make_cleanup (xfree, new_fnfield);
15556 memset (new_fnfield, 0, sizeof (struct nextfnfield));
15557 new_fnfield->next = flp->head;
15558 flp->head = new_fnfield;
15559 flp->length++;
15560
15561 /* Fill in the member function field info. */
15562 fnp = &new_fnfield->fnfield;
15563
15564 /* Delay processing of the physname until later. */
15565 if (cu->language == language_cplus)
15566 {
15567 add_to_method_list (type, i, flp->length - 1, fieldname,
15568 die, cu);
15569 }
15570 else
15571 {
15572 const char *physname = dwarf2_physname (fieldname, die, cu);
15573 fnp->physname = physname ? physname : "";
15574 }
15575
15576 fnp->type = alloc_type (objfile);
15577 this_type = read_type_die (die, cu);
15578 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15579 {
15580 int nparams = TYPE_NFIELDS (this_type);
15581
15582 /* TYPE is the domain of this method, and THIS_TYPE is the type
15583 of the method itself (TYPE_CODE_METHOD). */
15584 smash_to_method_type (fnp->type, type,
15585 TYPE_TARGET_TYPE (this_type),
15586 TYPE_FIELDS (this_type),
15587 TYPE_NFIELDS (this_type),
15588 TYPE_VARARGS (this_type));
15589
15590 /* Handle static member functions.
15591 Dwarf2 has no clean way to discern C++ static and non-static
15592 member functions. G++ helps GDB by marking the first
15593 parameter for non-static member functions (which is the this
15594 pointer) as artificial. We obtain this information from
15595 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15596 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15597 fnp->voffset = VOFFSET_STATIC;
15598 }
15599 else
15600 complaint (&symfile_complaints, _("member function type missing for '%s'"),
15601 dwarf2_full_name (fieldname, die, cu));
15602
15603 /* Get fcontext from DW_AT_containing_type if present. */
15604 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15605 fnp->fcontext = die_containing_type (die, cu);
15606
15607 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15608 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15609
15610 /* Get accessibility. */
15611 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15612 if (attr)
15613 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15614 else
15615 accessibility = dwarf2_default_access_attribute (die, cu);
15616 switch (accessibility)
15617 {
15618 case DW_ACCESS_private:
15619 fnp->is_private = 1;
15620 break;
15621 case DW_ACCESS_protected:
15622 fnp->is_protected = 1;
15623 break;
15624 }
15625
15626 /* Check for artificial methods. */
15627 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15628 if (attr && DW_UNSND (attr) != 0)
15629 fnp->is_artificial = 1;
15630
15631 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15632
15633 /* Get index in virtual function table if it is a virtual member
15634 function. For older versions of GCC, this is an offset in the
15635 appropriate virtual table, as specified by DW_AT_containing_type.
15636 For everyone else, it is an expression to be evaluated relative
15637 to the object address. */
15638
15639 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15640 if (attr)
15641 {
15642 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15643 {
15644 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15645 {
15646 /* Old-style GCC. */
15647 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15648 }
15649 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15650 || (DW_BLOCK (attr)->size > 1
15651 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15652 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15653 {
15654 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15655 if ((fnp->voffset % cu->header.addr_size) != 0)
15656 dwarf2_complex_location_expr_complaint ();
15657 else
15658 fnp->voffset /= cu->header.addr_size;
15659 fnp->voffset += 2;
15660 }
15661 else
15662 dwarf2_complex_location_expr_complaint ();
15663
15664 if (!fnp->fcontext)
15665 {
15666 /* If there is no `this' field and no DW_AT_containing_type,
15667 we cannot actually find a base class context for the
15668 vtable! */
15669 if (TYPE_NFIELDS (this_type) == 0
15670 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15671 {
15672 complaint (&symfile_complaints,
15673 _("cannot determine context for virtual member "
15674 "function \"%s\" (offset %d)"),
15675 fieldname, to_underlying (die->sect_off));
15676 }
15677 else
15678 {
15679 fnp->fcontext
15680 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15681 }
15682 }
15683 }
15684 else if (attr_form_is_section_offset (attr))
15685 {
15686 dwarf2_complex_location_expr_complaint ();
15687 }
15688 else
15689 {
15690 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15691 fieldname);
15692 }
15693 }
15694 else
15695 {
15696 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15697 if (attr && DW_UNSND (attr))
15698 {
15699 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15700 complaint (&symfile_complaints,
15701 _("Member function \"%s\" (offset %d) is virtual "
15702 "but the vtable offset is not specified"),
15703 fieldname, to_underlying (die->sect_off));
15704 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15705 TYPE_CPLUS_DYNAMIC (type) = 1;
15706 }
15707 }
15708 }
15709
15710 /* Create the vector of member function fields, and attach it to the type. */
15711
15712 static void
15713 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15714 struct dwarf2_cu *cu)
15715 {
15716 struct fnfieldlist *flp;
15717 int i;
15718
15719 if (cu->language == language_ada)
15720 error (_("unexpected member functions in Ada type"));
15721
15722 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15723 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15724 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
15725
15726 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
15727 {
15728 struct nextfnfield *nfp = flp->head;
15729 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15730 int k;
15731
15732 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
15733 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
15734 fn_flp->fn_fields = (struct fn_field *)
15735 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
15736 for (k = flp->length; (k--, nfp); nfp = nfp->next)
15737 fn_flp->fn_fields[k] = nfp->fnfield;
15738 }
15739
15740 TYPE_NFN_FIELDS (type) = fip->nfnfields;
15741 }
15742
15743 /* Returns non-zero if NAME is the name of a vtable member in CU's
15744 language, zero otherwise. */
15745 static int
15746 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15747 {
15748 static const char vptr[] = "_vptr";
15749
15750 /* Look for the C++ form of the vtable. */
15751 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15752 return 1;
15753
15754 return 0;
15755 }
15756
15757 /* GCC outputs unnamed structures that are really pointers to member
15758 functions, with the ABI-specified layout. If TYPE describes
15759 such a structure, smash it into a member function type.
15760
15761 GCC shouldn't do this; it should just output pointer to member DIEs.
15762 This is GCC PR debug/28767. */
15763
15764 static void
15765 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15766 {
15767 struct type *pfn_type, *self_type, *new_type;
15768
15769 /* Check for a structure with no name and two children. */
15770 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15771 return;
15772
15773 /* Check for __pfn and __delta members. */
15774 if (TYPE_FIELD_NAME (type, 0) == NULL
15775 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15776 || TYPE_FIELD_NAME (type, 1) == NULL
15777 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15778 return;
15779
15780 /* Find the type of the method. */
15781 pfn_type = TYPE_FIELD_TYPE (type, 0);
15782 if (pfn_type == NULL
15783 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15784 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15785 return;
15786
15787 /* Look for the "this" argument. */
15788 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15789 if (TYPE_NFIELDS (pfn_type) == 0
15790 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15791 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15792 return;
15793
15794 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15795 new_type = alloc_type (objfile);
15796 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15797 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15798 TYPE_VARARGS (pfn_type));
15799 smash_to_methodptr_type (type, new_type);
15800 }
15801
15802
15803 /* Called when we find the DIE that starts a structure or union scope
15804 (definition) to create a type for the structure or union. Fill in
15805 the type's name and general properties; the members will not be
15806 processed until process_structure_scope. A symbol table entry for
15807 the type will also not be done until process_structure_scope (assuming
15808 the type has a name).
15809
15810 NOTE: we need to call these functions regardless of whether or not the
15811 DIE has a DW_AT_name attribute, since it might be an anonymous
15812 structure or union. This gets the type entered into our set of
15813 user defined types. */
15814
15815 static struct type *
15816 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15817 {
15818 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15819 struct type *type;
15820 struct attribute *attr;
15821 const char *name;
15822
15823 /* If the definition of this type lives in .debug_types, read that type.
15824 Don't follow DW_AT_specification though, that will take us back up
15825 the chain and we want to go down. */
15826 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15827 if (attr)
15828 {
15829 type = get_DW_AT_signature_type (die, attr, cu);
15830
15831 /* The type's CU may not be the same as CU.
15832 Ensure TYPE is recorded with CU in die_type_hash. */
15833 return set_die_type (die, type, cu);
15834 }
15835
15836 type = alloc_type (objfile);
15837 INIT_CPLUS_SPECIFIC (type);
15838
15839 name = dwarf2_name (die, cu);
15840 if (name != NULL)
15841 {
15842 if (cu->language == language_cplus
15843 || cu->language == language_d
15844 || cu->language == language_rust)
15845 {
15846 const char *full_name = dwarf2_full_name (name, die, cu);
15847
15848 /* dwarf2_full_name might have already finished building the DIE's
15849 type. If so, there is no need to continue. */
15850 if (get_die_type (die, cu) != NULL)
15851 return get_die_type (die, cu);
15852
15853 TYPE_TAG_NAME (type) = full_name;
15854 if (die->tag == DW_TAG_structure_type
15855 || die->tag == DW_TAG_class_type)
15856 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15857 }
15858 else
15859 {
15860 /* The name is already allocated along with this objfile, so
15861 we don't need to duplicate it for the type. */
15862 TYPE_TAG_NAME (type) = name;
15863 if (die->tag == DW_TAG_class_type)
15864 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15865 }
15866 }
15867
15868 if (die->tag == DW_TAG_structure_type)
15869 {
15870 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15871 }
15872 else if (die->tag == DW_TAG_union_type)
15873 {
15874 TYPE_CODE (type) = TYPE_CODE_UNION;
15875 }
15876 else
15877 {
15878 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15879 }
15880
15881 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15882 TYPE_DECLARED_CLASS (type) = 1;
15883
15884 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15885 if (attr)
15886 {
15887 if (attr_form_is_constant (attr))
15888 TYPE_LENGTH (type) = DW_UNSND (attr);
15889 else
15890 {
15891 /* For the moment, dynamic type sizes are not supported
15892 by GDB's struct type. The actual size is determined
15893 on-demand when resolving the type of a given object,
15894 so set the type's length to zero for now. Otherwise,
15895 we record an expression as the length, and that expression
15896 could lead to a very large value, which could eventually
15897 lead to us trying to allocate that much memory when creating
15898 a value of that type. */
15899 TYPE_LENGTH (type) = 0;
15900 }
15901 }
15902 else
15903 {
15904 TYPE_LENGTH (type) = 0;
15905 }
15906
15907 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15908 {
15909 /* ICC<14 does not output the required DW_AT_declaration on
15910 incomplete types, but gives them a size of zero. */
15911 TYPE_STUB (type) = 1;
15912 }
15913 else
15914 TYPE_STUB_SUPPORTED (type) = 1;
15915
15916 if (die_is_declaration (die, cu))
15917 TYPE_STUB (type) = 1;
15918 else if (attr == NULL && die->child == NULL
15919 && producer_is_realview (cu->producer))
15920 /* RealView does not output the required DW_AT_declaration
15921 on incomplete types. */
15922 TYPE_STUB (type) = 1;
15923
15924 /* We need to add the type field to the die immediately so we don't
15925 infinitely recurse when dealing with pointers to the structure
15926 type within the structure itself. */
15927 set_die_type (die, type, cu);
15928
15929 /* set_die_type should be already done. */
15930 set_descriptive_type (type, die, cu);
15931
15932 return type;
15933 }
15934
15935 /* Finish creating a structure or union type, including filling in
15936 its members and creating a symbol for it. */
15937
15938 static void
15939 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15940 {
15941 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15942 struct die_info *child_die;
15943 struct type *type;
15944
15945 type = get_die_type (die, cu);
15946 if (type == NULL)
15947 type = read_structure_type (die, cu);
15948
15949 if (die->child != NULL && ! die_is_declaration (die, cu))
15950 {
15951 struct field_info fi;
15952 std::vector<struct symbol *> template_args;
15953 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
15954
15955 memset (&fi, 0, sizeof (struct field_info));
15956
15957 child_die = die->child;
15958
15959 while (child_die && child_die->tag)
15960 {
15961 if (child_die->tag == DW_TAG_member
15962 || child_die->tag == DW_TAG_variable)
15963 {
15964 /* NOTE: carlton/2002-11-05: A C++ static data member
15965 should be a DW_TAG_member that is a declaration, but
15966 all versions of G++ as of this writing (so through at
15967 least 3.2.1) incorrectly generate DW_TAG_variable
15968 tags for them instead. */
15969 dwarf2_add_field (&fi, child_die, cu);
15970 }
15971 else if (child_die->tag == DW_TAG_subprogram)
15972 {
15973 /* Rust doesn't have member functions in the C++ sense.
15974 However, it does emit ordinary functions as children
15975 of a struct DIE. */
15976 if (cu->language == language_rust)
15977 read_func_scope (child_die, cu);
15978 else
15979 {
15980 /* C++ member function. */
15981 dwarf2_add_member_fn (&fi, child_die, type, cu);
15982 }
15983 }
15984 else if (child_die->tag == DW_TAG_inheritance)
15985 {
15986 /* C++ base class field. */
15987 dwarf2_add_field (&fi, child_die, cu);
15988 }
15989 else if (type_can_define_types (child_die))
15990 dwarf2_add_type_defn (&fi, child_die, cu);
15991 else if (child_die->tag == DW_TAG_template_type_param
15992 || child_die->tag == DW_TAG_template_value_param)
15993 {
15994 struct symbol *arg = new_symbol (child_die, NULL, cu);
15995
15996 if (arg != NULL)
15997 template_args.push_back (arg);
15998 }
15999
16000 child_die = sibling_die (child_die);
16001 }
16002
16003 /* Attach template arguments to type. */
16004 if (!template_args.empty ())
16005 {
16006 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16007 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16008 TYPE_TEMPLATE_ARGUMENTS (type)
16009 = XOBNEWVEC (&objfile->objfile_obstack,
16010 struct symbol *,
16011 TYPE_N_TEMPLATE_ARGUMENTS (type));
16012 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16013 template_args.data (),
16014 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16015 * sizeof (struct symbol *)));
16016 }
16017
16018 /* Attach fields and member functions to the type. */
16019 if (fi.nfields)
16020 dwarf2_attach_fields_to_type (&fi, type, cu);
16021 if (fi.nfnfields)
16022 {
16023 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16024
16025 /* Get the type which refers to the base class (possibly this
16026 class itself) which contains the vtable pointer for the current
16027 class from the DW_AT_containing_type attribute. This use of
16028 DW_AT_containing_type is a GNU extension. */
16029
16030 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16031 {
16032 struct type *t = die_containing_type (die, cu);
16033
16034 set_type_vptr_basetype (type, t);
16035 if (type == t)
16036 {
16037 int i;
16038
16039 /* Our own class provides vtbl ptr. */
16040 for (i = TYPE_NFIELDS (t) - 1;
16041 i >= TYPE_N_BASECLASSES (t);
16042 --i)
16043 {
16044 const char *fieldname = TYPE_FIELD_NAME (t, i);
16045
16046 if (is_vtable_name (fieldname, cu))
16047 {
16048 set_type_vptr_fieldno (type, i);
16049 break;
16050 }
16051 }
16052
16053 /* Complain if virtual function table field not found. */
16054 if (i < TYPE_N_BASECLASSES (t))
16055 complaint (&symfile_complaints,
16056 _("virtual function table pointer "
16057 "not found when defining class '%s'"),
16058 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16059 "");
16060 }
16061 else
16062 {
16063 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16064 }
16065 }
16066 else if (cu->producer
16067 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16068 {
16069 /* The IBM XLC compiler does not provide direct indication
16070 of the containing type, but the vtable pointer is
16071 always named __vfp. */
16072
16073 int i;
16074
16075 for (i = TYPE_NFIELDS (type) - 1;
16076 i >= TYPE_N_BASECLASSES (type);
16077 --i)
16078 {
16079 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16080 {
16081 set_type_vptr_fieldno (type, i);
16082 set_type_vptr_basetype (type, type);
16083 break;
16084 }
16085 }
16086 }
16087 }
16088
16089 /* Copy fi.typedef_field_list linked list elements content into the
16090 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16091 if (fi.typedef_field_list)
16092 {
16093 int i = fi.typedef_field_list_count;
16094
16095 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16096 TYPE_TYPEDEF_FIELD_ARRAY (type)
16097 = ((struct decl_field *)
16098 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
16099 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
16100
16101 /* Reverse the list order to keep the debug info elements order. */
16102 while (--i >= 0)
16103 {
16104 struct decl_field *dest, *src;
16105
16106 dest = &TYPE_TYPEDEF_FIELD (type, i);
16107 src = &fi.typedef_field_list->field;
16108 fi.typedef_field_list = fi.typedef_field_list->next;
16109 *dest = *src;
16110 }
16111 }
16112
16113 /* Copy fi.nested_types_list linked list elements content into the
16114 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16115 if (fi.nested_types_list != NULL && cu->language != language_ada)
16116 {
16117 int i = fi.nested_types_list_count;
16118
16119 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16120 TYPE_NESTED_TYPES_ARRAY (type)
16121 = ((struct decl_field *)
16122 TYPE_ALLOC (type, sizeof (struct decl_field) * i));
16123 TYPE_NESTED_TYPES_COUNT (type) = i;
16124
16125 /* Reverse the list order to keep the debug info elements order. */
16126 while (--i >= 0)
16127 {
16128 struct decl_field *dest, *src;
16129
16130 dest = &TYPE_NESTED_TYPES_FIELD (type, i);
16131 src = &fi.nested_types_list->field;
16132 fi.nested_types_list = fi.nested_types_list->next;
16133 *dest = *src;
16134 }
16135 }
16136
16137 do_cleanups (back_to);
16138 }
16139
16140 quirk_gcc_member_function_pointer (type, objfile);
16141
16142 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16143 snapshots) has been known to create a die giving a declaration
16144 for a class that has, as a child, a die giving a definition for a
16145 nested class. So we have to process our children even if the
16146 current die is a declaration. Normally, of course, a declaration
16147 won't have any children at all. */
16148
16149 child_die = die->child;
16150
16151 while (child_die != NULL && child_die->tag)
16152 {
16153 if (child_die->tag == DW_TAG_member
16154 || child_die->tag == DW_TAG_variable
16155 || child_die->tag == DW_TAG_inheritance
16156 || child_die->tag == DW_TAG_template_value_param
16157 || child_die->tag == DW_TAG_template_type_param)
16158 {
16159 /* Do nothing. */
16160 }
16161 else
16162 process_die (child_die, cu);
16163
16164 child_die = sibling_die (child_die);
16165 }
16166
16167 /* Do not consider external references. According to the DWARF standard,
16168 these DIEs are identified by the fact that they have no byte_size
16169 attribute, and a declaration attribute. */
16170 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16171 || !die_is_declaration (die, cu))
16172 new_symbol (die, type, cu);
16173 }
16174
16175 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16176 update TYPE using some information only available in DIE's children. */
16177
16178 static void
16179 update_enumeration_type_from_children (struct die_info *die,
16180 struct type *type,
16181 struct dwarf2_cu *cu)
16182 {
16183 struct die_info *child_die;
16184 int unsigned_enum = 1;
16185 int flag_enum = 1;
16186 ULONGEST mask = 0;
16187
16188 auto_obstack obstack;
16189
16190 for (child_die = die->child;
16191 child_die != NULL && child_die->tag;
16192 child_die = sibling_die (child_die))
16193 {
16194 struct attribute *attr;
16195 LONGEST value;
16196 const gdb_byte *bytes;
16197 struct dwarf2_locexpr_baton *baton;
16198 const char *name;
16199
16200 if (child_die->tag != DW_TAG_enumerator)
16201 continue;
16202
16203 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16204 if (attr == NULL)
16205 continue;
16206
16207 name = dwarf2_name (child_die, cu);
16208 if (name == NULL)
16209 name = "<anonymous enumerator>";
16210
16211 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16212 &value, &bytes, &baton);
16213 if (value < 0)
16214 {
16215 unsigned_enum = 0;
16216 flag_enum = 0;
16217 }
16218 else if ((mask & value) != 0)
16219 flag_enum = 0;
16220 else
16221 mask |= value;
16222
16223 /* If we already know that the enum type is neither unsigned, nor
16224 a flag type, no need to look at the rest of the enumerates. */
16225 if (!unsigned_enum && !flag_enum)
16226 break;
16227 }
16228
16229 if (unsigned_enum)
16230 TYPE_UNSIGNED (type) = 1;
16231 if (flag_enum)
16232 TYPE_FLAG_ENUM (type) = 1;
16233 }
16234
16235 /* Given a DW_AT_enumeration_type die, set its type. We do not
16236 complete the type's fields yet, or create any symbols. */
16237
16238 static struct type *
16239 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16240 {
16241 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16242 struct type *type;
16243 struct attribute *attr;
16244 const char *name;
16245
16246 /* If the definition of this type lives in .debug_types, read that type.
16247 Don't follow DW_AT_specification though, that will take us back up
16248 the chain and we want to go down. */
16249 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16250 if (attr)
16251 {
16252 type = get_DW_AT_signature_type (die, attr, cu);
16253
16254 /* The type's CU may not be the same as CU.
16255 Ensure TYPE is recorded with CU in die_type_hash. */
16256 return set_die_type (die, type, cu);
16257 }
16258
16259 type = alloc_type (objfile);
16260
16261 TYPE_CODE (type) = TYPE_CODE_ENUM;
16262 name = dwarf2_full_name (NULL, die, cu);
16263 if (name != NULL)
16264 TYPE_TAG_NAME (type) = name;
16265
16266 attr = dwarf2_attr (die, DW_AT_type, cu);
16267 if (attr != NULL)
16268 {
16269 struct type *underlying_type = die_type (die, cu);
16270
16271 TYPE_TARGET_TYPE (type) = underlying_type;
16272 }
16273
16274 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16275 if (attr)
16276 {
16277 TYPE_LENGTH (type) = DW_UNSND (attr);
16278 }
16279 else
16280 {
16281 TYPE_LENGTH (type) = 0;
16282 }
16283
16284 /* The enumeration DIE can be incomplete. In Ada, any type can be
16285 declared as private in the package spec, and then defined only
16286 inside the package body. Such types are known as Taft Amendment
16287 Types. When another package uses such a type, an incomplete DIE
16288 may be generated by the compiler. */
16289 if (die_is_declaration (die, cu))
16290 TYPE_STUB (type) = 1;
16291
16292 /* Finish the creation of this type by using the enum's children.
16293 We must call this even when the underlying type has been provided
16294 so that we can determine if we're looking at a "flag" enum. */
16295 update_enumeration_type_from_children (die, type, cu);
16296
16297 /* If this type has an underlying type that is not a stub, then we
16298 may use its attributes. We always use the "unsigned" attribute
16299 in this situation, because ordinarily we guess whether the type
16300 is unsigned -- but the guess can be wrong and the underlying type
16301 can tell us the reality. However, we defer to a local size
16302 attribute if one exists, because this lets the compiler override
16303 the underlying type if needed. */
16304 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16305 {
16306 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16307 if (TYPE_LENGTH (type) == 0)
16308 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16309 }
16310
16311 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16312
16313 return set_die_type (die, type, cu);
16314 }
16315
16316 /* Given a pointer to a die which begins an enumeration, process all
16317 the dies that define the members of the enumeration, and create the
16318 symbol for the enumeration type.
16319
16320 NOTE: We reverse the order of the element list. */
16321
16322 static void
16323 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16324 {
16325 struct type *this_type;
16326
16327 this_type = get_die_type (die, cu);
16328 if (this_type == NULL)
16329 this_type = read_enumeration_type (die, cu);
16330
16331 if (die->child != NULL)
16332 {
16333 struct die_info *child_die;
16334 struct symbol *sym;
16335 struct field *fields = NULL;
16336 int num_fields = 0;
16337 const char *name;
16338
16339 child_die = die->child;
16340 while (child_die && child_die->tag)
16341 {
16342 if (child_die->tag != DW_TAG_enumerator)
16343 {
16344 process_die (child_die, cu);
16345 }
16346 else
16347 {
16348 name = dwarf2_name (child_die, cu);
16349 if (name)
16350 {
16351 sym = new_symbol (child_die, this_type, cu);
16352
16353 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16354 {
16355 fields = (struct field *)
16356 xrealloc (fields,
16357 (num_fields + DW_FIELD_ALLOC_CHUNK)
16358 * sizeof (struct field));
16359 }
16360
16361 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16362 FIELD_TYPE (fields[num_fields]) = NULL;
16363 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16364 FIELD_BITSIZE (fields[num_fields]) = 0;
16365
16366 num_fields++;
16367 }
16368 }
16369
16370 child_die = sibling_die (child_die);
16371 }
16372
16373 if (num_fields)
16374 {
16375 TYPE_NFIELDS (this_type) = num_fields;
16376 TYPE_FIELDS (this_type) = (struct field *)
16377 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16378 memcpy (TYPE_FIELDS (this_type), fields,
16379 sizeof (struct field) * num_fields);
16380 xfree (fields);
16381 }
16382 }
16383
16384 /* If we are reading an enum from a .debug_types unit, and the enum
16385 is a declaration, and the enum is not the signatured type in the
16386 unit, then we do not want to add a symbol for it. Adding a
16387 symbol would in some cases obscure the true definition of the
16388 enum, giving users an incomplete type when the definition is
16389 actually available. Note that we do not want to do this for all
16390 enums which are just declarations, because C++0x allows forward
16391 enum declarations. */
16392 if (cu->per_cu->is_debug_types
16393 && die_is_declaration (die, cu))
16394 {
16395 struct signatured_type *sig_type;
16396
16397 sig_type = (struct signatured_type *) cu->per_cu;
16398 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16399 if (sig_type->type_offset_in_section != die->sect_off)
16400 return;
16401 }
16402
16403 new_symbol (die, this_type, cu);
16404 }
16405
16406 /* Extract all information from a DW_TAG_array_type DIE and put it in
16407 the DIE's type field. For now, this only handles one dimensional
16408 arrays. */
16409
16410 static struct type *
16411 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16412 {
16413 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16414 struct die_info *child_die;
16415 struct type *type;
16416 struct type *element_type, *range_type, *index_type;
16417 struct attribute *attr;
16418 const char *name;
16419 struct dynamic_prop *byte_stride_prop = NULL;
16420 unsigned int bit_stride = 0;
16421
16422 element_type = die_type (die, cu);
16423
16424 /* The die_type call above may have already set the type for this DIE. */
16425 type = get_die_type (die, cu);
16426 if (type)
16427 return type;
16428
16429 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16430 if (attr != NULL)
16431 {
16432 int stride_ok;
16433
16434 byte_stride_prop
16435 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16436 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16437 if (!stride_ok)
16438 {
16439 complaint (&symfile_complaints,
16440 _("unable to read array DW_AT_byte_stride "
16441 " - DIE at 0x%x [in module %s]"),
16442 to_underlying (die->sect_off),
16443 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16444 /* Ignore this attribute. We will likely not be able to print
16445 arrays of this type correctly, but there is little we can do
16446 to help if we cannot read the attribute's value. */
16447 byte_stride_prop = NULL;
16448 }
16449 }
16450
16451 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16452 if (attr != NULL)
16453 bit_stride = DW_UNSND (attr);
16454
16455 /* Irix 6.2 native cc creates array types without children for
16456 arrays with unspecified length. */
16457 if (die->child == NULL)
16458 {
16459 index_type = objfile_type (objfile)->builtin_int;
16460 range_type = create_static_range_type (NULL, index_type, 0, -1);
16461 type = create_array_type_with_stride (NULL, element_type, range_type,
16462 byte_stride_prop, bit_stride);
16463 return set_die_type (die, type, cu);
16464 }
16465
16466 std::vector<struct type *> range_types;
16467 child_die = die->child;
16468 while (child_die && child_die->tag)
16469 {
16470 if (child_die->tag == DW_TAG_subrange_type)
16471 {
16472 struct type *child_type = read_type_die (child_die, cu);
16473
16474 if (child_type != NULL)
16475 {
16476 /* The range type was succesfully read. Save it for the
16477 array type creation. */
16478 range_types.push_back (child_type);
16479 }
16480 }
16481 child_die = sibling_die (child_die);
16482 }
16483
16484 /* Dwarf2 dimensions are output from left to right, create the
16485 necessary array types in backwards order. */
16486
16487 type = element_type;
16488
16489 if (read_array_order (die, cu) == DW_ORD_col_major)
16490 {
16491 int i = 0;
16492
16493 while (i < range_types.size ())
16494 type = create_array_type_with_stride (NULL, type, range_types[i++],
16495 byte_stride_prop, bit_stride);
16496 }
16497 else
16498 {
16499 size_t ndim = range_types.size ();
16500 while (ndim-- > 0)
16501 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16502 byte_stride_prop, bit_stride);
16503 }
16504
16505 /* Understand Dwarf2 support for vector types (like they occur on
16506 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16507 array type. This is not part of the Dwarf2/3 standard yet, but a
16508 custom vendor extension. The main difference between a regular
16509 array and the vector variant is that vectors are passed by value
16510 to functions. */
16511 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16512 if (attr)
16513 make_vector_type (type);
16514
16515 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16516 implementation may choose to implement triple vectors using this
16517 attribute. */
16518 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16519 if (attr)
16520 {
16521 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16522 TYPE_LENGTH (type) = DW_UNSND (attr);
16523 else
16524 complaint (&symfile_complaints,
16525 _("DW_AT_byte_size for array type smaller "
16526 "than the total size of elements"));
16527 }
16528
16529 name = dwarf2_name (die, cu);
16530 if (name)
16531 TYPE_NAME (type) = name;
16532
16533 /* Install the type in the die. */
16534 set_die_type (die, type, cu);
16535
16536 /* set_die_type should be already done. */
16537 set_descriptive_type (type, die, cu);
16538
16539 return type;
16540 }
16541
16542 static enum dwarf_array_dim_ordering
16543 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16544 {
16545 struct attribute *attr;
16546
16547 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16548
16549 if (attr)
16550 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16551
16552 /* GNU F77 is a special case, as at 08/2004 array type info is the
16553 opposite order to the dwarf2 specification, but data is still
16554 laid out as per normal fortran.
16555
16556 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16557 version checking. */
16558
16559 if (cu->language == language_fortran
16560 && cu->producer && strstr (cu->producer, "GNU F77"))
16561 {
16562 return DW_ORD_row_major;
16563 }
16564
16565 switch (cu->language_defn->la_array_ordering)
16566 {
16567 case array_column_major:
16568 return DW_ORD_col_major;
16569 case array_row_major:
16570 default:
16571 return DW_ORD_row_major;
16572 };
16573 }
16574
16575 /* Extract all information from a DW_TAG_set_type DIE and put it in
16576 the DIE's type field. */
16577
16578 static struct type *
16579 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16580 {
16581 struct type *domain_type, *set_type;
16582 struct attribute *attr;
16583
16584 domain_type = die_type (die, cu);
16585
16586 /* The die_type call above may have already set the type for this DIE. */
16587 set_type = get_die_type (die, cu);
16588 if (set_type)
16589 return set_type;
16590
16591 set_type = create_set_type (NULL, domain_type);
16592
16593 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16594 if (attr)
16595 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16596
16597 return set_die_type (die, set_type, cu);
16598 }
16599
16600 /* A helper for read_common_block that creates a locexpr baton.
16601 SYM is the symbol which we are marking as computed.
16602 COMMON_DIE is the DIE for the common block.
16603 COMMON_LOC is the location expression attribute for the common
16604 block itself.
16605 MEMBER_LOC is the location expression attribute for the particular
16606 member of the common block that we are processing.
16607 CU is the CU from which the above come. */
16608
16609 static void
16610 mark_common_block_symbol_computed (struct symbol *sym,
16611 struct die_info *common_die,
16612 struct attribute *common_loc,
16613 struct attribute *member_loc,
16614 struct dwarf2_cu *cu)
16615 {
16616 struct dwarf2_per_objfile *dwarf2_per_objfile
16617 = cu->per_cu->dwarf2_per_objfile;
16618 struct objfile *objfile = dwarf2_per_objfile->objfile;
16619 struct dwarf2_locexpr_baton *baton;
16620 gdb_byte *ptr;
16621 unsigned int cu_off;
16622 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16623 LONGEST offset = 0;
16624
16625 gdb_assert (common_loc && member_loc);
16626 gdb_assert (attr_form_is_block (common_loc));
16627 gdb_assert (attr_form_is_block (member_loc)
16628 || attr_form_is_constant (member_loc));
16629
16630 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16631 baton->per_cu = cu->per_cu;
16632 gdb_assert (baton->per_cu);
16633
16634 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16635
16636 if (attr_form_is_constant (member_loc))
16637 {
16638 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16639 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16640 }
16641 else
16642 baton->size += DW_BLOCK (member_loc)->size;
16643
16644 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16645 baton->data = ptr;
16646
16647 *ptr++ = DW_OP_call4;
16648 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16649 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16650 ptr += 4;
16651
16652 if (attr_form_is_constant (member_loc))
16653 {
16654 *ptr++ = DW_OP_addr;
16655 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16656 ptr += cu->header.addr_size;
16657 }
16658 else
16659 {
16660 /* We have to copy the data here, because DW_OP_call4 will only
16661 use a DW_AT_location attribute. */
16662 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16663 ptr += DW_BLOCK (member_loc)->size;
16664 }
16665
16666 *ptr++ = DW_OP_plus;
16667 gdb_assert (ptr - baton->data == baton->size);
16668
16669 SYMBOL_LOCATION_BATON (sym) = baton;
16670 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16671 }
16672
16673 /* Create appropriate locally-scoped variables for all the
16674 DW_TAG_common_block entries. Also create a struct common_block
16675 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16676 is used to sepate the common blocks name namespace from regular
16677 variable names. */
16678
16679 static void
16680 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16681 {
16682 struct attribute *attr;
16683
16684 attr = dwarf2_attr (die, DW_AT_location, cu);
16685 if (attr)
16686 {
16687 /* Support the .debug_loc offsets. */
16688 if (attr_form_is_block (attr))
16689 {
16690 /* Ok. */
16691 }
16692 else if (attr_form_is_section_offset (attr))
16693 {
16694 dwarf2_complex_location_expr_complaint ();
16695 attr = NULL;
16696 }
16697 else
16698 {
16699 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16700 "common block member");
16701 attr = NULL;
16702 }
16703 }
16704
16705 if (die->child != NULL)
16706 {
16707 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16708 struct die_info *child_die;
16709 size_t n_entries = 0, size;
16710 struct common_block *common_block;
16711 struct symbol *sym;
16712
16713 for (child_die = die->child;
16714 child_die && child_die->tag;
16715 child_die = sibling_die (child_die))
16716 ++n_entries;
16717
16718 size = (sizeof (struct common_block)
16719 + (n_entries - 1) * sizeof (struct symbol *));
16720 common_block
16721 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16722 size);
16723 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16724 common_block->n_entries = 0;
16725
16726 for (child_die = die->child;
16727 child_die && child_die->tag;
16728 child_die = sibling_die (child_die))
16729 {
16730 /* Create the symbol in the DW_TAG_common_block block in the current
16731 symbol scope. */
16732 sym = new_symbol (child_die, NULL, cu);
16733 if (sym != NULL)
16734 {
16735 struct attribute *member_loc;
16736
16737 common_block->contents[common_block->n_entries++] = sym;
16738
16739 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16740 cu);
16741 if (member_loc)
16742 {
16743 /* GDB has handled this for a long time, but it is
16744 not specified by DWARF. It seems to have been
16745 emitted by gfortran at least as recently as:
16746 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16747 complaint (&symfile_complaints,
16748 _("Variable in common block has "
16749 "DW_AT_data_member_location "
16750 "- DIE at 0x%x [in module %s]"),
16751 to_underlying (child_die->sect_off),
16752 objfile_name (objfile));
16753
16754 if (attr_form_is_section_offset (member_loc))
16755 dwarf2_complex_location_expr_complaint ();
16756 else if (attr_form_is_constant (member_loc)
16757 || attr_form_is_block (member_loc))
16758 {
16759 if (attr)
16760 mark_common_block_symbol_computed (sym, die, attr,
16761 member_loc, cu);
16762 }
16763 else
16764 dwarf2_complex_location_expr_complaint ();
16765 }
16766 }
16767 }
16768
16769 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16770 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16771 }
16772 }
16773
16774 /* Create a type for a C++ namespace. */
16775
16776 static struct type *
16777 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16778 {
16779 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16780 const char *previous_prefix, *name;
16781 int is_anonymous;
16782 struct type *type;
16783
16784 /* For extensions, reuse the type of the original namespace. */
16785 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16786 {
16787 struct die_info *ext_die;
16788 struct dwarf2_cu *ext_cu = cu;
16789
16790 ext_die = dwarf2_extension (die, &ext_cu);
16791 type = read_type_die (ext_die, ext_cu);
16792
16793 /* EXT_CU may not be the same as CU.
16794 Ensure TYPE is recorded with CU in die_type_hash. */
16795 return set_die_type (die, type, cu);
16796 }
16797
16798 name = namespace_name (die, &is_anonymous, cu);
16799
16800 /* Now build the name of the current namespace. */
16801
16802 previous_prefix = determine_prefix (die, cu);
16803 if (previous_prefix[0] != '\0')
16804 name = typename_concat (&objfile->objfile_obstack,
16805 previous_prefix, name, 0, cu);
16806
16807 /* Create the type. */
16808 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16809 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16810
16811 return set_die_type (die, type, cu);
16812 }
16813
16814 /* Read a namespace scope. */
16815
16816 static void
16817 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16818 {
16819 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16820 int is_anonymous;
16821
16822 /* Add a symbol associated to this if we haven't seen the namespace
16823 before. Also, add a using directive if it's an anonymous
16824 namespace. */
16825
16826 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16827 {
16828 struct type *type;
16829
16830 type = read_type_die (die, cu);
16831 new_symbol (die, type, cu);
16832
16833 namespace_name (die, &is_anonymous, cu);
16834 if (is_anonymous)
16835 {
16836 const char *previous_prefix = determine_prefix (die, cu);
16837
16838 std::vector<const char *> excludes;
16839 add_using_directive (using_directives (cu->language),
16840 previous_prefix, TYPE_NAME (type), NULL,
16841 NULL, excludes, 0, &objfile->objfile_obstack);
16842 }
16843 }
16844
16845 if (die->child != NULL)
16846 {
16847 struct die_info *child_die = die->child;
16848
16849 while (child_die && child_die->tag)
16850 {
16851 process_die (child_die, cu);
16852 child_die = sibling_die (child_die);
16853 }
16854 }
16855 }
16856
16857 /* Read a Fortran module as type. This DIE can be only a declaration used for
16858 imported module. Still we need that type as local Fortran "use ... only"
16859 declaration imports depend on the created type in determine_prefix. */
16860
16861 static struct type *
16862 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16863 {
16864 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16865 const char *module_name;
16866 struct type *type;
16867
16868 module_name = dwarf2_name (die, cu);
16869 if (!module_name)
16870 complaint (&symfile_complaints,
16871 _("DW_TAG_module has no name, offset 0x%x"),
16872 to_underlying (die->sect_off));
16873 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16874
16875 /* determine_prefix uses TYPE_TAG_NAME. */
16876 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16877
16878 return set_die_type (die, type, cu);
16879 }
16880
16881 /* Read a Fortran module. */
16882
16883 static void
16884 read_module (struct die_info *die, struct dwarf2_cu *cu)
16885 {
16886 struct die_info *child_die = die->child;
16887 struct type *type;
16888
16889 type = read_type_die (die, cu);
16890 new_symbol (die, type, cu);
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 /* Return the name of the namespace represented by DIE. Set
16900 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16901 namespace. */
16902
16903 static const char *
16904 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16905 {
16906 struct die_info *current_die;
16907 const char *name = NULL;
16908
16909 /* Loop through the extensions until we find a name. */
16910
16911 for (current_die = die;
16912 current_die != NULL;
16913 current_die = dwarf2_extension (die, &cu))
16914 {
16915 /* We don't use dwarf2_name here so that we can detect the absence
16916 of a name -> anonymous namespace. */
16917 name = dwarf2_string_attr (die, DW_AT_name, cu);
16918
16919 if (name != NULL)
16920 break;
16921 }
16922
16923 /* Is it an anonymous namespace? */
16924
16925 *is_anonymous = (name == NULL);
16926 if (*is_anonymous)
16927 name = CP_ANONYMOUS_NAMESPACE_STR;
16928
16929 return name;
16930 }
16931
16932 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16933 the user defined type vector. */
16934
16935 static struct type *
16936 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16937 {
16938 struct gdbarch *gdbarch
16939 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16940 struct comp_unit_head *cu_header = &cu->header;
16941 struct type *type;
16942 struct attribute *attr_byte_size;
16943 struct attribute *attr_address_class;
16944 int byte_size, addr_class;
16945 struct type *target_type;
16946
16947 target_type = die_type (die, cu);
16948
16949 /* The die_type call above may have already set the type for this DIE. */
16950 type = get_die_type (die, cu);
16951 if (type)
16952 return type;
16953
16954 type = lookup_pointer_type (target_type);
16955
16956 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16957 if (attr_byte_size)
16958 byte_size = DW_UNSND (attr_byte_size);
16959 else
16960 byte_size = cu_header->addr_size;
16961
16962 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16963 if (attr_address_class)
16964 addr_class = DW_UNSND (attr_address_class);
16965 else
16966 addr_class = DW_ADDR_none;
16967
16968 /* If the pointer size or address class is different than the
16969 default, create a type variant marked as such and set the
16970 length accordingly. */
16971 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
16972 {
16973 if (gdbarch_address_class_type_flags_p (gdbarch))
16974 {
16975 int type_flags;
16976
16977 type_flags = gdbarch_address_class_type_flags
16978 (gdbarch, byte_size, addr_class);
16979 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16980 == 0);
16981 type = make_type_with_address_space (type, type_flags);
16982 }
16983 else if (TYPE_LENGTH (type) != byte_size)
16984 {
16985 complaint (&symfile_complaints,
16986 _("invalid pointer size %d"), byte_size);
16987 }
16988 else
16989 {
16990 /* Should we also complain about unhandled address classes? */
16991 }
16992 }
16993
16994 TYPE_LENGTH (type) = byte_size;
16995 return set_die_type (die, type, cu);
16996 }
16997
16998 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16999 the user defined type vector. */
17000
17001 static struct type *
17002 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17003 {
17004 struct type *type;
17005 struct type *to_type;
17006 struct type *domain;
17007
17008 to_type = die_type (die, cu);
17009 domain = die_containing_type (die, cu);
17010
17011 /* The calls above may have already set the type for this DIE. */
17012 type = get_die_type (die, cu);
17013 if (type)
17014 return type;
17015
17016 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17017 type = lookup_methodptr_type (to_type);
17018 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17019 {
17020 struct type *new_type
17021 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17022
17023 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17024 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17025 TYPE_VARARGS (to_type));
17026 type = lookup_methodptr_type (new_type);
17027 }
17028 else
17029 type = lookup_memberptr_type (to_type, domain);
17030
17031 return set_die_type (die, type, cu);
17032 }
17033
17034 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17035 the user defined type vector. */
17036
17037 static struct type *
17038 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17039 enum type_code refcode)
17040 {
17041 struct comp_unit_head *cu_header = &cu->header;
17042 struct type *type, *target_type;
17043 struct attribute *attr;
17044
17045 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17046
17047 target_type = die_type (die, cu);
17048
17049 /* The die_type call above may have already set the type for this DIE. */
17050 type = get_die_type (die, cu);
17051 if (type)
17052 return type;
17053
17054 type = lookup_reference_type (target_type, refcode);
17055 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17056 if (attr)
17057 {
17058 TYPE_LENGTH (type) = DW_UNSND (attr);
17059 }
17060 else
17061 {
17062 TYPE_LENGTH (type) = cu_header->addr_size;
17063 }
17064 return set_die_type (die, type, cu);
17065 }
17066
17067 /* Add the given cv-qualifiers to the element type of the array. GCC
17068 outputs DWARF type qualifiers that apply to an array, not the
17069 element type. But GDB relies on the array element type to carry
17070 the cv-qualifiers. This mimics section 6.7.3 of the C99
17071 specification. */
17072
17073 static struct type *
17074 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17075 struct type *base_type, int cnst, int voltl)
17076 {
17077 struct type *el_type, *inner_array;
17078
17079 base_type = copy_type (base_type);
17080 inner_array = base_type;
17081
17082 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17083 {
17084 TYPE_TARGET_TYPE (inner_array) =
17085 copy_type (TYPE_TARGET_TYPE (inner_array));
17086 inner_array = TYPE_TARGET_TYPE (inner_array);
17087 }
17088
17089 el_type = TYPE_TARGET_TYPE (inner_array);
17090 cnst |= TYPE_CONST (el_type);
17091 voltl |= TYPE_VOLATILE (el_type);
17092 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17093
17094 return set_die_type (die, base_type, cu);
17095 }
17096
17097 static struct type *
17098 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17099 {
17100 struct type *base_type, *cv_type;
17101
17102 base_type = die_type (die, cu);
17103
17104 /* The die_type call above may have already set the type for this DIE. */
17105 cv_type = get_die_type (die, cu);
17106 if (cv_type)
17107 return cv_type;
17108
17109 /* In case the const qualifier is applied to an array type, the element type
17110 is so qualified, not the array type (section 6.7.3 of C99). */
17111 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17112 return add_array_cv_type (die, cu, base_type, 1, 0);
17113
17114 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17115 return set_die_type (die, cv_type, cu);
17116 }
17117
17118 static struct type *
17119 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17120 {
17121 struct type *base_type, *cv_type;
17122
17123 base_type = die_type (die, cu);
17124
17125 /* The die_type call above may have already set the type for this DIE. */
17126 cv_type = get_die_type (die, cu);
17127 if (cv_type)
17128 return cv_type;
17129
17130 /* In case the volatile qualifier is applied to an array type, the
17131 element type is so qualified, not the array type (section 6.7.3
17132 of C99). */
17133 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17134 return add_array_cv_type (die, cu, base_type, 0, 1);
17135
17136 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17137 return set_die_type (die, cv_type, cu);
17138 }
17139
17140 /* Handle DW_TAG_restrict_type. */
17141
17142 static struct type *
17143 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17144 {
17145 struct type *base_type, *cv_type;
17146
17147 base_type = die_type (die, cu);
17148
17149 /* The die_type call above may have already set the type for this DIE. */
17150 cv_type = get_die_type (die, cu);
17151 if (cv_type)
17152 return cv_type;
17153
17154 cv_type = make_restrict_type (base_type);
17155 return set_die_type (die, cv_type, cu);
17156 }
17157
17158 /* Handle DW_TAG_atomic_type. */
17159
17160 static struct type *
17161 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17162 {
17163 struct type *base_type, *cv_type;
17164
17165 base_type = die_type (die, cu);
17166
17167 /* The die_type call above may have already set the type for this DIE. */
17168 cv_type = get_die_type (die, cu);
17169 if (cv_type)
17170 return cv_type;
17171
17172 cv_type = make_atomic_type (base_type);
17173 return set_die_type (die, cv_type, cu);
17174 }
17175
17176 /* Extract all information from a DW_TAG_string_type DIE and add to
17177 the user defined type vector. It isn't really a user defined type,
17178 but it behaves like one, with other DIE's using an AT_user_def_type
17179 attribute to reference it. */
17180
17181 static struct type *
17182 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17183 {
17184 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17185 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17186 struct type *type, *range_type, *index_type, *char_type;
17187 struct attribute *attr;
17188 unsigned int length;
17189
17190 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17191 if (attr)
17192 {
17193 length = DW_UNSND (attr);
17194 }
17195 else
17196 {
17197 /* Check for the DW_AT_byte_size attribute. */
17198 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17199 if (attr)
17200 {
17201 length = DW_UNSND (attr);
17202 }
17203 else
17204 {
17205 length = 1;
17206 }
17207 }
17208
17209 index_type = objfile_type (objfile)->builtin_int;
17210 range_type = create_static_range_type (NULL, index_type, 1, length);
17211 char_type = language_string_char_type (cu->language_defn, gdbarch);
17212 type = create_string_type (NULL, char_type, range_type);
17213
17214 return set_die_type (die, type, cu);
17215 }
17216
17217 /* Assuming that DIE corresponds to a function, returns nonzero
17218 if the function is prototyped. */
17219
17220 static int
17221 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17222 {
17223 struct attribute *attr;
17224
17225 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17226 if (attr && (DW_UNSND (attr) != 0))
17227 return 1;
17228
17229 /* The DWARF standard implies that the DW_AT_prototyped attribute
17230 is only meaninful for C, but the concept also extends to other
17231 languages that allow unprototyped functions (Eg: Objective C).
17232 For all other languages, assume that functions are always
17233 prototyped. */
17234 if (cu->language != language_c
17235 && cu->language != language_objc
17236 && cu->language != language_opencl)
17237 return 1;
17238
17239 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17240 prototyped and unprototyped functions; default to prototyped,
17241 since that is more common in modern code (and RealView warns
17242 about unprototyped functions). */
17243 if (producer_is_realview (cu->producer))
17244 return 1;
17245
17246 return 0;
17247 }
17248
17249 /* Handle DIES due to C code like:
17250
17251 struct foo
17252 {
17253 int (*funcp)(int a, long l);
17254 int b;
17255 };
17256
17257 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17258
17259 static struct type *
17260 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17261 {
17262 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17263 struct type *type; /* Type that this function returns. */
17264 struct type *ftype; /* Function that returns above type. */
17265 struct attribute *attr;
17266
17267 type = die_type (die, cu);
17268
17269 /* The die_type call above may have already set the type for this DIE. */
17270 ftype = get_die_type (die, cu);
17271 if (ftype)
17272 return ftype;
17273
17274 ftype = lookup_function_type (type);
17275
17276 if (prototyped_function_p (die, cu))
17277 TYPE_PROTOTYPED (ftype) = 1;
17278
17279 /* Store the calling convention in the type if it's available in
17280 the subroutine die. Otherwise set the calling convention to
17281 the default value DW_CC_normal. */
17282 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17283 if (attr)
17284 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17285 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17286 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17287 else
17288 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17289
17290 /* Record whether the function returns normally to its caller or not
17291 if the DWARF producer set that information. */
17292 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17293 if (attr && (DW_UNSND (attr) != 0))
17294 TYPE_NO_RETURN (ftype) = 1;
17295
17296 /* We need to add the subroutine type to the die immediately so
17297 we don't infinitely recurse when dealing with parameters
17298 declared as the same subroutine type. */
17299 set_die_type (die, ftype, cu);
17300
17301 if (die->child != NULL)
17302 {
17303 struct type *void_type = objfile_type (objfile)->builtin_void;
17304 struct die_info *child_die;
17305 int nparams, iparams;
17306
17307 /* Count the number of parameters.
17308 FIXME: GDB currently ignores vararg functions, but knows about
17309 vararg member functions. */
17310 nparams = 0;
17311 child_die = die->child;
17312 while (child_die && child_die->tag)
17313 {
17314 if (child_die->tag == DW_TAG_formal_parameter)
17315 nparams++;
17316 else if (child_die->tag == DW_TAG_unspecified_parameters)
17317 TYPE_VARARGS (ftype) = 1;
17318 child_die = sibling_die (child_die);
17319 }
17320
17321 /* Allocate storage for parameters and fill them in. */
17322 TYPE_NFIELDS (ftype) = nparams;
17323 TYPE_FIELDS (ftype) = (struct field *)
17324 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17325
17326 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17327 even if we error out during the parameters reading below. */
17328 for (iparams = 0; iparams < nparams; iparams++)
17329 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17330
17331 iparams = 0;
17332 child_die = die->child;
17333 while (child_die && child_die->tag)
17334 {
17335 if (child_die->tag == DW_TAG_formal_parameter)
17336 {
17337 struct type *arg_type;
17338
17339 /* DWARF version 2 has no clean way to discern C++
17340 static and non-static member functions. G++ helps
17341 GDB by marking the first parameter for non-static
17342 member functions (which is the this pointer) as
17343 artificial. We pass this information to
17344 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17345
17346 DWARF version 3 added DW_AT_object_pointer, which GCC
17347 4.5 does not yet generate. */
17348 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17349 if (attr)
17350 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17351 else
17352 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17353 arg_type = die_type (child_die, cu);
17354
17355 /* RealView does not mark THIS as const, which the testsuite
17356 expects. GCC marks THIS as const in method definitions,
17357 but not in the class specifications (GCC PR 43053). */
17358 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17359 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17360 {
17361 int is_this = 0;
17362 struct dwarf2_cu *arg_cu = cu;
17363 const char *name = dwarf2_name (child_die, cu);
17364
17365 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17366 if (attr)
17367 {
17368 /* If the compiler emits this, use it. */
17369 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17370 is_this = 1;
17371 }
17372 else if (name && strcmp (name, "this") == 0)
17373 /* Function definitions will have the argument names. */
17374 is_this = 1;
17375 else if (name == NULL && iparams == 0)
17376 /* Declarations may not have the names, so like
17377 elsewhere in GDB, assume an artificial first
17378 argument is "this". */
17379 is_this = 1;
17380
17381 if (is_this)
17382 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17383 arg_type, 0);
17384 }
17385
17386 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17387 iparams++;
17388 }
17389 child_die = sibling_die (child_die);
17390 }
17391 }
17392
17393 return ftype;
17394 }
17395
17396 static struct type *
17397 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17398 {
17399 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17400 const char *name = NULL;
17401 struct type *this_type, *target_type;
17402
17403 name = dwarf2_full_name (NULL, die, cu);
17404 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17405 TYPE_TARGET_STUB (this_type) = 1;
17406 set_die_type (die, this_type, cu);
17407 target_type = die_type (die, cu);
17408 if (target_type != this_type)
17409 TYPE_TARGET_TYPE (this_type) = target_type;
17410 else
17411 {
17412 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17413 spec and cause infinite loops in GDB. */
17414 complaint (&symfile_complaints,
17415 _("Self-referential DW_TAG_typedef "
17416 "- DIE at 0x%x [in module %s]"),
17417 to_underlying (die->sect_off), objfile_name (objfile));
17418 TYPE_TARGET_TYPE (this_type) = NULL;
17419 }
17420 return this_type;
17421 }
17422
17423 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17424 (which may be different from NAME) to the architecture back-end to allow
17425 it to guess the correct format if necessary. */
17426
17427 static struct type *
17428 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17429 const char *name_hint)
17430 {
17431 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17432 const struct floatformat **format;
17433 struct type *type;
17434
17435 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17436 if (format)
17437 type = init_float_type (objfile, bits, name, format);
17438 else
17439 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17440
17441 return type;
17442 }
17443
17444 /* Find a representation of a given base type and install
17445 it in the TYPE field of the die. */
17446
17447 static struct type *
17448 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17449 {
17450 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17451 struct type *type;
17452 struct attribute *attr;
17453 int encoding = 0, bits = 0;
17454 const char *name;
17455
17456 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17457 if (attr)
17458 {
17459 encoding = DW_UNSND (attr);
17460 }
17461 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17462 if (attr)
17463 {
17464 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17465 }
17466 name = dwarf2_name (die, cu);
17467 if (!name)
17468 {
17469 complaint (&symfile_complaints,
17470 _("DW_AT_name missing from DW_TAG_base_type"));
17471 }
17472
17473 switch (encoding)
17474 {
17475 case DW_ATE_address:
17476 /* Turn DW_ATE_address into a void * pointer. */
17477 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17478 type = init_pointer_type (objfile, bits, name, type);
17479 break;
17480 case DW_ATE_boolean:
17481 type = init_boolean_type (objfile, bits, 1, name);
17482 break;
17483 case DW_ATE_complex_float:
17484 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17485 type = init_complex_type (objfile, name, type);
17486 break;
17487 case DW_ATE_decimal_float:
17488 type = init_decfloat_type (objfile, bits, name);
17489 break;
17490 case DW_ATE_float:
17491 type = dwarf2_init_float_type (objfile, bits, name, name);
17492 break;
17493 case DW_ATE_signed:
17494 type = init_integer_type (objfile, bits, 0, name);
17495 break;
17496 case DW_ATE_unsigned:
17497 if (cu->language == language_fortran
17498 && name
17499 && startswith (name, "character("))
17500 type = init_character_type (objfile, bits, 1, name);
17501 else
17502 type = init_integer_type (objfile, bits, 1, name);
17503 break;
17504 case DW_ATE_signed_char:
17505 if (cu->language == language_ada || cu->language == language_m2
17506 || cu->language == language_pascal
17507 || cu->language == language_fortran)
17508 type = init_character_type (objfile, bits, 0, name);
17509 else
17510 type = init_integer_type (objfile, bits, 0, name);
17511 break;
17512 case DW_ATE_unsigned_char:
17513 if (cu->language == language_ada || cu->language == language_m2
17514 || cu->language == language_pascal
17515 || cu->language == language_fortran
17516 || cu->language == language_rust)
17517 type = init_character_type (objfile, bits, 1, name);
17518 else
17519 type = init_integer_type (objfile, bits, 1, name);
17520 break;
17521 case DW_ATE_UTF:
17522 {
17523 gdbarch *arch = get_objfile_arch (objfile);
17524
17525 if (bits == 16)
17526 type = builtin_type (arch)->builtin_char16;
17527 else if (bits == 32)
17528 type = builtin_type (arch)->builtin_char32;
17529 else
17530 {
17531 complaint (&symfile_complaints,
17532 _("unsupported DW_ATE_UTF bit size: '%d'"),
17533 bits);
17534 type = init_integer_type (objfile, bits, 1, name);
17535 }
17536 return set_die_type (die, type, cu);
17537 }
17538 break;
17539
17540 default:
17541 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17542 dwarf_type_encoding_name (encoding));
17543 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17544 break;
17545 }
17546
17547 if (name && strcmp (name, "char") == 0)
17548 TYPE_NOSIGN (type) = 1;
17549
17550 return set_die_type (die, type, cu);
17551 }
17552
17553 /* Parse dwarf attribute if it's a block, reference or constant and put the
17554 resulting value of the attribute into struct bound_prop.
17555 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17556
17557 static int
17558 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17559 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17560 {
17561 struct dwarf2_property_baton *baton;
17562 struct obstack *obstack
17563 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17564
17565 if (attr == NULL || prop == NULL)
17566 return 0;
17567
17568 if (attr_form_is_block (attr))
17569 {
17570 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17571 baton->referenced_type = NULL;
17572 baton->locexpr.per_cu = cu->per_cu;
17573 baton->locexpr.size = DW_BLOCK (attr)->size;
17574 baton->locexpr.data = DW_BLOCK (attr)->data;
17575 prop->data.baton = baton;
17576 prop->kind = PROP_LOCEXPR;
17577 gdb_assert (prop->data.baton != NULL);
17578 }
17579 else if (attr_form_is_ref (attr))
17580 {
17581 struct dwarf2_cu *target_cu = cu;
17582 struct die_info *target_die;
17583 struct attribute *target_attr;
17584
17585 target_die = follow_die_ref (die, attr, &target_cu);
17586 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17587 if (target_attr == NULL)
17588 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17589 target_cu);
17590 if (target_attr == NULL)
17591 return 0;
17592
17593 switch (target_attr->name)
17594 {
17595 case DW_AT_location:
17596 if (attr_form_is_section_offset (target_attr))
17597 {
17598 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17599 baton->referenced_type = die_type (target_die, target_cu);
17600 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17601 prop->data.baton = baton;
17602 prop->kind = PROP_LOCLIST;
17603 gdb_assert (prop->data.baton != NULL);
17604 }
17605 else if (attr_form_is_block (target_attr))
17606 {
17607 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17608 baton->referenced_type = die_type (target_die, target_cu);
17609 baton->locexpr.per_cu = cu->per_cu;
17610 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17611 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17612 prop->data.baton = baton;
17613 prop->kind = PROP_LOCEXPR;
17614 gdb_assert (prop->data.baton != NULL);
17615 }
17616 else
17617 {
17618 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17619 "dynamic property");
17620 return 0;
17621 }
17622 break;
17623 case DW_AT_data_member_location:
17624 {
17625 LONGEST offset;
17626
17627 if (!handle_data_member_location (target_die, target_cu,
17628 &offset))
17629 return 0;
17630
17631 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17632 baton->referenced_type = read_type_die (target_die->parent,
17633 target_cu);
17634 baton->offset_info.offset = offset;
17635 baton->offset_info.type = die_type (target_die, target_cu);
17636 prop->data.baton = baton;
17637 prop->kind = PROP_ADDR_OFFSET;
17638 break;
17639 }
17640 }
17641 }
17642 else if (attr_form_is_constant (attr))
17643 {
17644 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17645 prop->kind = PROP_CONST;
17646 }
17647 else
17648 {
17649 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17650 dwarf2_name (die, cu));
17651 return 0;
17652 }
17653
17654 return 1;
17655 }
17656
17657 /* Read the given DW_AT_subrange DIE. */
17658
17659 static struct type *
17660 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17661 {
17662 struct type *base_type, *orig_base_type;
17663 struct type *range_type;
17664 struct attribute *attr;
17665 struct dynamic_prop low, high;
17666 int low_default_is_valid;
17667 int high_bound_is_count = 0;
17668 const char *name;
17669 LONGEST negative_mask;
17670
17671 orig_base_type = die_type (die, cu);
17672 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17673 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17674 creating the range type, but we use the result of check_typedef
17675 when examining properties of the type. */
17676 base_type = check_typedef (orig_base_type);
17677
17678 /* The die_type call above may have already set the type for this DIE. */
17679 range_type = get_die_type (die, cu);
17680 if (range_type)
17681 return range_type;
17682
17683 low.kind = PROP_CONST;
17684 high.kind = PROP_CONST;
17685 high.data.const_val = 0;
17686
17687 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17688 omitting DW_AT_lower_bound. */
17689 switch (cu->language)
17690 {
17691 case language_c:
17692 case language_cplus:
17693 low.data.const_val = 0;
17694 low_default_is_valid = 1;
17695 break;
17696 case language_fortran:
17697 low.data.const_val = 1;
17698 low_default_is_valid = 1;
17699 break;
17700 case language_d:
17701 case language_objc:
17702 case language_rust:
17703 low.data.const_val = 0;
17704 low_default_is_valid = (cu->header.version >= 4);
17705 break;
17706 case language_ada:
17707 case language_m2:
17708 case language_pascal:
17709 low.data.const_val = 1;
17710 low_default_is_valid = (cu->header.version >= 4);
17711 break;
17712 default:
17713 low.data.const_val = 0;
17714 low_default_is_valid = 0;
17715 break;
17716 }
17717
17718 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17719 if (attr)
17720 attr_to_dynamic_prop (attr, die, cu, &low);
17721 else if (!low_default_is_valid)
17722 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17723 "- DIE at 0x%x [in module %s]"),
17724 to_underlying (die->sect_off),
17725 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17726
17727 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
17728 if (!attr_to_dynamic_prop (attr, die, cu, &high))
17729 {
17730 attr = dwarf2_attr (die, DW_AT_count, cu);
17731 if (attr_to_dynamic_prop (attr, die, cu, &high))
17732 {
17733 /* If bounds are constant do the final calculation here. */
17734 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17735 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17736 else
17737 high_bound_is_count = 1;
17738 }
17739 }
17740
17741 /* Dwarf-2 specifications explicitly allows to create subrange types
17742 without specifying a base type.
17743 In that case, the base type must be set to the type of
17744 the lower bound, upper bound or count, in that order, if any of these
17745 three attributes references an object that has a type.
17746 If no base type is found, the Dwarf-2 specifications say that
17747 a signed integer type of size equal to the size of an address should
17748 be used.
17749 For the following C code: `extern char gdb_int [];'
17750 GCC produces an empty range DIE.
17751 FIXME: muller/2010-05-28: Possible references to object for low bound,
17752 high bound or count are not yet handled by this code. */
17753 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17754 {
17755 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17756 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17757 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17758 struct type *int_type = objfile_type (objfile)->builtin_int;
17759
17760 /* Test "int", "long int", and "long long int" objfile types,
17761 and select the first one having a size above or equal to the
17762 architecture address size. */
17763 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17764 base_type = int_type;
17765 else
17766 {
17767 int_type = objfile_type (objfile)->builtin_long;
17768 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17769 base_type = int_type;
17770 else
17771 {
17772 int_type = objfile_type (objfile)->builtin_long_long;
17773 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17774 base_type = int_type;
17775 }
17776 }
17777 }
17778
17779 /* Normally, the DWARF producers are expected to use a signed
17780 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17781 But this is unfortunately not always the case, as witnessed
17782 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17783 is used instead. To work around that ambiguity, we treat
17784 the bounds as signed, and thus sign-extend their values, when
17785 the base type is signed. */
17786 negative_mask =
17787 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17788 if (low.kind == PROP_CONST
17789 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17790 low.data.const_val |= negative_mask;
17791 if (high.kind == PROP_CONST
17792 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17793 high.data.const_val |= negative_mask;
17794
17795 range_type = create_range_type (NULL, orig_base_type, &low, &high);
17796
17797 if (high_bound_is_count)
17798 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17799
17800 /* Ada expects an empty array on no boundary attributes. */
17801 if (attr == NULL && cu->language != language_ada)
17802 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17803
17804 name = dwarf2_name (die, cu);
17805 if (name)
17806 TYPE_NAME (range_type) = name;
17807
17808 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17809 if (attr)
17810 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17811
17812 set_die_type (die, range_type, cu);
17813
17814 /* set_die_type should be already done. */
17815 set_descriptive_type (range_type, die, cu);
17816
17817 return range_type;
17818 }
17819
17820 static struct type *
17821 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17822 {
17823 struct type *type;
17824
17825 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17826 NULL);
17827 TYPE_NAME (type) = dwarf2_name (die, cu);
17828
17829 /* In Ada, an unspecified type is typically used when the description
17830 of the type is defered to a different unit. When encountering
17831 such a type, we treat it as a stub, and try to resolve it later on,
17832 when needed. */
17833 if (cu->language == language_ada)
17834 TYPE_STUB (type) = 1;
17835
17836 return set_die_type (die, type, cu);
17837 }
17838
17839 /* Read a single die and all its descendents. Set the die's sibling
17840 field to NULL; set other fields in the die correctly, and set all
17841 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17842 location of the info_ptr after reading all of those dies. PARENT
17843 is the parent of the die in question. */
17844
17845 static struct die_info *
17846 read_die_and_children (const struct die_reader_specs *reader,
17847 const gdb_byte *info_ptr,
17848 const gdb_byte **new_info_ptr,
17849 struct die_info *parent)
17850 {
17851 struct die_info *die;
17852 const gdb_byte *cur_ptr;
17853 int has_children;
17854
17855 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17856 if (die == NULL)
17857 {
17858 *new_info_ptr = cur_ptr;
17859 return NULL;
17860 }
17861 store_in_ref_table (die, reader->cu);
17862
17863 if (has_children)
17864 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17865 else
17866 {
17867 die->child = NULL;
17868 *new_info_ptr = cur_ptr;
17869 }
17870
17871 die->sibling = NULL;
17872 die->parent = parent;
17873 return die;
17874 }
17875
17876 /* Read a die, all of its descendents, and all of its siblings; set
17877 all of the fields of all of the dies correctly. Arguments are as
17878 in read_die_and_children. */
17879
17880 static struct die_info *
17881 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17882 const gdb_byte *info_ptr,
17883 const gdb_byte **new_info_ptr,
17884 struct die_info *parent)
17885 {
17886 struct die_info *first_die, *last_sibling;
17887 const gdb_byte *cur_ptr;
17888
17889 cur_ptr = info_ptr;
17890 first_die = last_sibling = NULL;
17891
17892 while (1)
17893 {
17894 struct die_info *die
17895 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17896
17897 if (die == NULL)
17898 {
17899 *new_info_ptr = cur_ptr;
17900 return first_die;
17901 }
17902
17903 if (!first_die)
17904 first_die = die;
17905 else
17906 last_sibling->sibling = die;
17907
17908 last_sibling = die;
17909 }
17910 }
17911
17912 /* Read a die, all of its descendents, and all of its siblings; set
17913 all of the fields of all of the dies correctly. Arguments are as
17914 in read_die_and_children.
17915 This the main entry point for reading a DIE and all its children. */
17916
17917 static struct die_info *
17918 read_die_and_siblings (const struct die_reader_specs *reader,
17919 const gdb_byte *info_ptr,
17920 const gdb_byte **new_info_ptr,
17921 struct die_info *parent)
17922 {
17923 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17924 new_info_ptr, parent);
17925
17926 if (dwarf_die_debug)
17927 {
17928 fprintf_unfiltered (gdb_stdlog,
17929 "Read die from %s@0x%x of %s:\n",
17930 get_section_name (reader->die_section),
17931 (unsigned) (info_ptr - reader->die_section->buffer),
17932 bfd_get_filename (reader->abfd));
17933 dump_die (die, dwarf_die_debug);
17934 }
17935
17936 return die;
17937 }
17938
17939 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17940 attributes.
17941 The caller is responsible for filling in the extra attributes
17942 and updating (*DIEP)->num_attrs.
17943 Set DIEP to point to a newly allocated die with its information,
17944 except for its child, sibling, and parent fields.
17945 Set HAS_CHILDREN to tell whether the die has children or not. */
17946
17947 static const gdb_byte *
17948 read_full_die_1 (const struct die_reader_specs *reader,
17949 struct die_info **diep, const gdb_byte *info_ptr,
17950 int *has_children, int num_extra_attrs)
17951 {
17952 unsigned int abbrev_number, bytes_read, i;
17953 struct abbrev_info *abbrev;
17954 struct die_info *die;
17955 struct dwarf2_cu *cu = reader->cu;
17956 bfd *abfd = reader->abfd;
17957
17958 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17959 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17960 info_ptr += bytes_read;
17961 if (!abbrev_number)
17962 {
17963 *diep = NULL;
17964 *has_children = 0;
17965 return info_ptr;
17966 }
17967
17968 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17969 if (!abbrev)
17970 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17971 abbrev_number,
17972 bfd_get_filename (abfd));
17973
17974 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17975 die->sect_off = sect_off;
17976 die->tag = abbrev->tag;
17977 die->abbrev = abbrev_number;
17978
17979 /* Make the result usable.
17980 The caller needs to update num_attrs after adding the extra
17981 attributes. */
17982 die->num_attrs = abbrev->num_attrs;
17983
17984 for (i = 0; i < abbrev->num_attrs; ++i)
17985 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17986 info_ptr);
17987
17988 *diep = die;
17989 *has_children = abbrev->has_children;
17990 return info_ptr;
17991 }
17992
17993 /* Read a die and all its attributes.
17994 Set DIEP to point to a newly allocated die with its information,
17995 except for its child, sibling, and parent fields.
17996 Set HAS_CHILDREN to tell whether the die has children or not. */
17997
17998 static const gdb_byte *
17999 read_full_die (const struct die_reader_specs *reader,
18000 struct die_info **diep, const gdb_byte *info_ptr,
18001 int *has_children)
18002 {
18003 const gdb_byte *result;
18004
18005 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18006
18007 if (dwarf_die_debug)
18008 {
18009 fprintf_unfiltered (gdb_stdlog,
18010 "Read die from %s@0x%x of %s:\n",
18011 get_section_name (reader->die_section),
18012 (unsigned) (info_ptr - reader->die_section->buffer),
18013 bfd_get_filename (reader->abfd));
18014 dump_die (*diep, dwarf_die_debug);
18015 }
18016
18017 return result;
18018 }
18019 \f
18020 /* Abbreviation tables.
18021
18022 In DWARF version 2, the description of the debugging information is
18023 stored in a separate .debug_abbrev section. Before we read any
18024 dies from a section we read in all abbreviations and install them
18025 in a hash table. */
18026
18027 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18028
18029 struct abbrev_info *
18030 abbrev_table::alloc_abbrev ()
18031 {
18032 struct abbrev_info *abbrev;
18033
18034 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18035 memset (abbrev, 0, sizeof (struct abbrev_info));
18036
18037 return abbrev;
18038 }
18039
18040 /* Add an abbreviation to the table. */
18041
18042 void
18043 abbrev_table::add_abbrev (unsigned int abbrev_number,
18044 struct abbrev_info *abbrev)
18045 {
18046 unsigned int hash_number;
18047
18048 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18049 abbrev->next = m_abbrevs[hash_number];
18050 m_abbrevs[hash_number] = abbrev;
18051 }
18052
18053 /* Look up an abbrev in the table.
18054 Returns NULL if the abbrev is not found. */
18055
18056 struct abbrev_info *
18057 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18058 {
18059 unsigned int hash_number;
18060 struct abbrev_info *abbrev;
18061
18062 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18063 abbrev = m_abbrevs[hash_number];
18064
18065 while (abbrev)
18066 {
18067 if (abbrev->number == abbrev_number)
18068 return abbrev;
18069 abbrev = abbrev->next;
18070 }
18071 return NULL;
18072 }
18073
18074 /* Read in an abbrev table. */
18075
18076 static abbrev_table_up
18077 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18078 struct dwarf2_section_info *section,
18079 sect_offset sect_off)
18080 {
18081 struct objfile *objfile = dwarf2_per_objfile->objfile;
18082 bfd *abfd = get_section_bfd_owner (section);
18083 const gdb_byte *abbrev_ptr;
18084 struct abbrev_info *cur_abbrev;
18085 unsigned int abbrev_number, bytes_read, abbrev_name;
18086 unsigned int abbrev_form;
18087 struct attr_abbrev *cur_attrs;
18088 unsigned int allocated_attrs;
18089
18090 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18091
18092 dwarf2_read_section (objfile, section);
18093 abbrev_ptr = section->buffer + to_underlying (sect_off);
18094 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18095 abbrev_ptr += bytes_read;
18096
18097 allocated_attrs = ATTR_ALLOC_CHUNK;
18098 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18099
18100 /* Loop until we reach an abbrev number of 0. */
18101 while (abbrev_number)
18102 {
18103 cur_abbrev = abbrev_table->alloc_abbrev ();
18104
18105 /* read in abbrev header */
18106 cur_abbrev->number = abbrev_number;
18107 cur_abbrev->tag
18108 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18109 abbrev_ptr += bytes_read;
18110 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18111 abbrev_ptr += 1;
18112
18113 /* now read in declarations */
18114 for (;;)
18115 {
18116 LONGEST implicit_const;
18117
18118 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18119 abbrev_ptr += bytes_read;
18120 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18121 abbrev_ptr += bytes_read;
18122 if (abbrev_form == DW_FORM_implicit_const)
18123 {
18124 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18125 &bytes_read);
18126 abbrev_ptr += bytes_read;
18127 }
18128 else
18129 {
18130 /* Initialize it due to a false compiler warning. */
18131 implicit_const = -1;
18132 }
18133
18134 if (abbrev_name == 0)
18135 break;
18136
18137 if (cur_abbrev->num_attrs == allocated_attrs)
18138 {
18139 allocated_attrs += ATTR_ALLOC_CHUNK;
18140 cur_attrs
18141 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18142 }
18143
18144 cur_attrs[cur_abbrev->num_attrs].name
18145 = (enum dwarf_attribute) abbrev_name;
18146 cur_attrs[cur_abbrev->num_attrs].form
18147 = (enum dwarf_form) abbrev_form;
18148 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18149 ++cur_abbrev->num_attrs;
18150 }
18151
18152 cur_abbrev->attrs =
18153 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18154 cur_abbrev->num_attrs);
18155 memcpy (cur_abbrev->attrs, cur_attrs,
18156 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18157
18158 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18159
18160 /* Get next abbreviation.
18161 Under Irix6 the abbreviations for a compilation unit are not
18162 always properly terminated with an abbrev number of 0.
18163 Exit loop if we encounter an abbreviation which we have
18164 already read (which means we are about to read the abbreviations
18165 for the next compile unit) or if the end of the abbreviation
18166 table is reached. */
18167 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18168 break;
18169 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18170 abbrev_ptr += bytes_read;
18171 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18172 break;
18173 }
18174
18175 xfree (cur_attrs);
18176 return abbrev_table;
18177 }
18178
18179 /* Returns nonzero if TAG represents a type that we might generate a partial
18180 symbol for. */
18181
18182 static int
18183 is_type_tag_for_partial (int tag)
18184 {
18185 switch (tag)
18186 {
18187 #if 0
18188 /* Some types that would be reasonable to generate partial symbols for,
18189 that we don't at present. */
18190 case DW_TAG_array_type:
18191 case DW_TAG_file_type:
18192 case DW_TAG_ptr_to_member_type:
18193 case DW_TAG_set_type:
18194 case DW_TAG_string_type:
18195 case DW_TAG_subroutine_type:
18196 #endif
18197 case DW_TAG_base_type:
18198 case DW_TAG_class_type:
18199 case DW_TAG_interface_type:
18200 case DW_TAG_enumeration_type:
18201 case DW_TAG_structure_type:
18202 case DW_TAG_subrange_type:
18203 case DW_TAG_typedef:
18204 case DW_TAG_union_type:
18205 return 1;
18206 default:
18207 return 0;
18208 }
18209 }
18210
18211 /* Load all DIEs that are interesting for partial symbols into memory. */
18212
18213 static struct partial_die_info *
18214 load_partial_dies (const struct die_reader_specs *reader,
18215 const gdb_byte *info_ptr, int building_psymtab)
18216 {
18217 struct dwarf2_cu *cu = reader->cu;
18218 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18219 struct partial_die_info *part_die;
18220 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18221 unsigned int bytes_read;
18222 unsigned int load_all = 0;
18223 int nesting_level = 1;
18224
18225 parent_die = NULL;
18226 last_die = NULL;
18227
18228 gdb_assert (cu->per_cu != NULL);
18229 if (cu->per_cu->load_all_dies)
18230 load_all = 1;
18231
18232 cu->partial_dies
18233 = htab_create_alloc_ex (cu->header.length / 12,
18234 partial_die_hash,
18235 partial_die_eq,
18236 NULL,
18237 &cu->comp_unit_obstack,
18238 hashtab_obstack_allocate,
18239 dummy_obstack_deallocate);
18240
18241 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
18242
18243 while (1)
18244 {
18245 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18246
18247 /* A NULL abbrev means the end of a series of children. */
18248 if (abbrev == NULL)
18249 {
18250 if (--nesting_level == 0)
18251 {
18252 /* PART_DIE was probably the last thing allocated on the
18253 comp_unit_obstack, so we could call obstack_free
18254 here. We don't do that because the waste is small,
18255 and will be cleaned up when we're done with this
18256 compilation unit. This way, we're also more robust
18257 against other users of the comp_unit_obstack. */
18258 return first_die;
18259 }
18260 info_ptr += bytes_read;
18261 last_die = parent_die;
18262 parent_die = parent_die->die_parent;
18263 continue;
18264 }
18265
18266 /* Check for template arguments. We never save these; if
18267 they're seen, we just mark the parent, and go on our way. */
18268 if (parent_die != NULL
18269 && cu->language == language_cplus
18270 && (abbrev->tag == DW_TAG_template_type_param
18271 || abbrev->tag == DW_TAG_template_value_param))
18272 {
18273 parent_die->has_template_arguments = 1;
18274
18275 if (!load_all)
18276 {
18277 /* We don't need a partial DIE for the template argument. */
18278 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18279 continue;
18280 }
18281 }
18282
18283 /* We only recurse into c++ subprograms looking for template arguments.
18284 Skip their other children. */
18285 if (!load_all
18286 && cu->language == language_cplus
18287 && parent_die != NULL
18288 && parent_die->tag == DW_TAG_subprogram)
18289 {
18290 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18291 continue;
18292 }
18293
18294 /* Check whether this DIE is interesting enough to save. Normally
18295 we would not be interested in members here, but there may be
18296 later variables referencing them via DW_AT_specification (for
18297 static members). */
18298 if (!load_all
18299 && !is_type_tag_for_partial (abbrev->tag)
18300 && abbrev->tag != DW_TAG_constant
18301 && abbrev->tag != DW_TAG_enumerator
18302 && abbrev->tag != DW_TAG_subprogram
18303 && abbrev->tag != DW_TAG_inlined_subroutine
18304 && abbrev->tag != DW_TAG_lexical_block
18305 && abbrev->tag != DW_TAG_variable
18306 && abbrev->tag != DW_TAG_namespace
18307 && abbrev->tag != DW_TAG_module
18308 && abbrev->tag != DW_TAG_member
18309 && abbrev->tag != DW_TAG_imported_unit
18310 && abbrev->tag != DW_TAG_imported_declaration)
18311 {
18312 /* Otherwise we skip to the next sibling, if any. */
18313 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18314 continue;
18315 }
18316
18317 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
18318 info_ptr);
18319
18320 /* This two-pass algorithm for processing partial symbols has a
18321 high cost in cache pressure. Thus, handle some simple cases
18322 here which cover the majority of C partial symbols. DIEs
18323 which neither have specification tags in them, nor could have
18324 specification tags elsewhere pointing at them, can simply be
18325 processed and discarded.
18326
18327 This segment is also optional; scan_partial_symbols and
18328 add_partial_symbol will handle these DIEs if we chain
18329 them in normally. When compilers which do not emit large
18330 quantities of duplicate debug information are more common,
18331 this code can probably be removed. */
18332
18333 /* Any complete simple types at the top level (pretty much all
18334 of them, for a language without namespaces), can be processed
18335 directly. */
18336 if (parent_die == NULL
18337 && part_die->has_specification == 0
18338 && part_die->is_declaration == 0
18339 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
18340 || part_die->tag == DW_TAG_base_type
18341 || part_die->tag == DW_TAG_subrange_type))
18342 {
18343 if (building_psymtab && part_die->name != NULL)
18344 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
18345 VAR_DOMAIN, LOC_TYPEDEF,
18346 &objfile->static_psymbols,
18347 0, cu->language, objfile);
18348 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
18349 continue;
18350 }
18351
18352 /* The exception for DW_TAG_typedef with has_children above is
18353 a workaround of GCC PR debug/47510. In the case of this complaint
18354 type_name_no_tag_or_error will error on such types later.
18355
18356 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18357 it could not find the child DIEs referenced later, this is checked
18358 above. In correct DWARF DW_TAG_typedef should have no children. */
18359
18360 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
18361 complaint (&symfile_complaints,
18362 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18363 "- DIE at 0x%x [in module %s]"),
18364 to_underlying (part_die->sect_off), objfile_name (objfile));
18365
18366 /* If we're at the second level, and we're an enumerator, and
18367 our parent has no specification (meaning possibly lives in a
18368 namespace elsewhere), then we can add the partial symbol now
18369 instead of queueing it. */
18370 if (part_die->tag == DW_TAG_enumerator
18371 && parent_die != NULL
18372 && parent_die->die_parent == NULL
18373 && parent_die->tag == DW_TAG_enumeration_type
18374 && parent_die->has_specification == 0)
18375 {
18376 if (part_die->name == NULL)
18377 complaint (&symfile_complaints,
18378 _("malformed enumerator DIE ignored"));
18379 else if (building_psymtab)
18380 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
18381 VAR_DOMAIN, LOC_CONST,
18382 cu->language == language_cplus
18383 ? &objfile->global_psymbols
18384 : &objfile->static_psymbols,
18385 0, cu->language, objfile);
18386
18387 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
18388 continue;
18389 }
18390
18391 /* We'll save this DIE so link it in. */
18392 part_die->die_parent = parent_die;
18393 part_die->die_sibling = NULL;
18394 part_die->die_child = NULL;
18395
18396 if (last_die && last_die == parent_die)
18397 last_die->die_child = part_die;
18398 else if (last_die)
18399 last_die->die_sibling = part_die;
18400
18401 last_die = part_die;
18402
18403 if (first_die == NULL)
18404 first_die = part_die;
18405
18406 /* Maybe add the DIE to the hash table. Not all DIEs that we
18407 find interesting need to be in the hash table, because we
18408 also have the parent/sibling/child chains; only those that we
18409 might refer to by offset later during partial symbol reading.
18410
18411 For now this means things that might have be the target of a
18412 DW_AT_specification, DW_AT_abstract_origin, or
18413 DW_AT_extension. DW_AT_extension will refer only to
18414 namespaces; DW_AT_abstract_origin refers to functions (and
18415 many things under the function DIE, but we do not recurse
18416 into function DIEs during partial symbol reading) and
18417 possibly variables as well; DW_AT_specification refers to
18418 declarations. Declarations ought to have the DW_AT_declaration
18419 flag. It happens that GCC forgets to put it in sometimes, but
18420 only for functions, not for types.
18421
18422 Adding more things than necessary to the hash table is harmless
18423 except for the performance cost. Adding too few will result in
18424 wasted time in find_partial_die, when we reread the compilation
18425 unit with load_all_dies set. */
18426
18427 if (load_all
18428 || abbrev->tag == DW_TAG_constant
18429 || abbrev->tag == DW_TAG_subprogram
18430 || abbrev->tag == DW_TAG_variable
18431 || abbrev->tag == DW_TAG_namespace
18432 || part_die->is_declaration)
18433 {
18434 void **slot;
18435
18436 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18437 to_underlying (part_die->sect_off),
18438 INSERT);
18439 *slot = part_die;
18440 }
18441
18442 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
18443
18444 /* For some DIEs we want to follow their children (if any). For C
18445 we have no reason to follow the children of structures; for other
18446 languages we have to, so that we can get at method physnames
18447 to infer fully qualified class names, for DW_AT_specification,
18448 and for C++ template arguments. For C++, we also look one level
18449 inside functions to find template arguments (if the name of the
18450 function does not already contain the template arguments).
18451
18452 For Ada, we need to scan the children of subprograms and lexical
18453 blocks as well because Ada allows the definition of nested
18454 entities that could be interesting for the debugger, such as
18455 nested subprograms for instance. */
18456 if (last_die->has_children
18457 && (load_all
18458 || last_die->tag == DW_TAG_namespace
18459 || last_die->tag == DW_TAG_module
18460 || last_die->tag == DW_TAG_enumeration_type
18461 || (cu->language == language_cplus
18462 && last_die->tag == DW_TAG_subprogram
18463 && (last_die->name == NULL
18464 || strchr (last_die->name, '<') == NULL))
18465 || (cu->language != language_c
18466 && (last_die->tag == DW_TAG_class_type
18467 || last_die->tag == DW_TAG_interface_type
18468 || last_die->tag == DW_TAG_structure_type
18469 || last_die->tag == DW_TAG_union_type))
18470 || (cu->language == language_ada
18471 && (last_die->tag == DW_TAG_subprogram
18472 || last_die->tag == DW_TAG_lexical_block))))
18473 {
18474 nesting_level++;
18475 parent_die = last_die;
18476 continue;
18477 }
18478
18479 /* Otherwise we skip to the next sibling, if any. */
18480 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18481
18482 /* Back to the top, do it again. */
18483 }
18484 }
18485
18486 /* Read a minimal amount of information into the minimal die structure. */
18487
18488 static const gdb_byte *
18489 read_partial_die (const struct die_reader_specs *reader,
18490 struct partial_die_info *part_die,
18491 struct abbrev_info *abbrev, unsigned int abbrev_len,
18492 const gdb_byte *info_ptr)
18493 {
18494 struct dwarf2_cu *cu = reader->cu;
18495 struct dwarf2_per_objfile *dwarf2_per_objfile
18496 = cu->per_cu->dwarf2_per_objfile;
18497 struct objfile *objfile = dwarf2_per_objfile->objfile;
18498 const gdb_byte *buffer = reader->buffer;
18499 unsigned int i;
18500 struct attribute attr;
18501 int has_low_pc_attr = 0;
18502 int has_high_pc_attr = 0;
18503 int high_pc_relative = 0;
18504
18505 memset (part_die, 0, sizeof (struct partial_die_info));
18506
18507 part_die->sect_off = (sect_offset) (info_ptr - buffer);
18508
18509 info_ptr += abbrev_len;
18510
18511 if (abbrev == NULL)
18512 return info_ptr;
18513
18514 part_die->tag = abbrev->tag;
18515 part_die->has_children = abbrev->has_children;
18516
18517 for (i = 0; i < abbrev->num_attrs; ++i)
18518 {
18519 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
18520
18521 /* Store the data if it is of an attribute we want to keep in a
18522 partial symbol table. */
18523 switch (attr.name)
18524 {
18525 case DW_AT_name:
18526 switch (part_die->tag)
18527 {
18528 case DW_TAG_compile_unit:
18529 case DW_TAG_partial_unit:
18530 case DW_TAG_type_unit:
18531 /* Compilation units have a DW_AT_name that is a filename, not
18532 a source language identifier. */
18533 case DW_TAG_enumeration_type:
18534 case DW_TAG_enumerator:
18535 /* These tags always have simple identifiers already; no need
18536 to canonicalize them. */
18537 part_die->name = DW_STRING (&attr);
18538 break;
18539 default:
18540 part_die->name
18541 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18542 &objfile->per_bfd->storage_obstack);
18543 break;
18544 }
18545 break;
18546 case DW_AT_linkage_name:
18547 case DW_AT_MIPS_linkage_name:
18548 /* Note that both forms of linkage name might appear. We
18549 assume they will be the same, and we only store the last
18550 one we see. */
18551 if (cu->language == language_ada)
18552 part_die->name = DW_STRING (&attr);
18553 part_die->linkage_name = DW_STRING (&attr);
18554 break;
18555 case DW_AT_low_pc:
18556 has_low_pc_attr = 1;
18557 part_die->lowpc = attr_value_as_address (&attr);
18558 break;
18559 case DW_AT_high_pc:
18560 has_high_pc_attr = 1;
18561 part_die->highpc = attr_value_as_address (&attr);
18562 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18563 high_pc_relative = 1;
18564 break;
18565 case DW_AT_location:
18566 /* Support the .debug_loc offsets. */
18567 if (attr_form_is_block (&attr))
18568 {
18569 part_die->d.locdesc = DW_BLOCK (&attr);
18570 }
18571 else if (attr_form_is_section_offset (&attr))
18572 {
18573 dwarf2_complex_location_expr_complaint ();
18574 }
18575 else
18576 {
18577 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18578 "partial symbol information");
18579 }
18580 break;
18581 case DW_AT_external:
18582 part_die->is_external = DW_UNSND (&attr);
18583 break;
18584 case DW_AT_declaration:
18585 part_die->is_declaration = DW_UNSND (&attr);
18586 break;
18587 case DW_AT_type:
18588 part_die->has_type = 1;
18589 break;
18590 case DW_AT_abstract_origin:
18591 case DW_AT_specification:
18592 case DW_AT_extension:
18593 part_die->has_specification = 1;
18594 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
18595 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18596 || cu->per_cu->is_dwz);
18597 break;
18598 case DW_AT_sibling:
18599 /* Ignore absolute siblings, they might point outside of
18600 the current compile unit. */
18601 if (attr.form == DW_FORM_ref_addr)
18602 complaint (&symfile_complaints,
18603 _("ignoring absolute DW_AT_sibling"));
18604 else
18605 {
18606 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18607 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18608
18609 if (sibling_ptr < info_ptr)
18610 complaint (&symfile_complaints,
18611 _("DW_AT_sibling points backwards"));
18612 else if (sibling_ptr > reader->buffer_end)
18613 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18614 else
18615 part_die->sibling = sibling_ptr;
18616 }
18617 break;
18618 case DW_AT_byte_size:
18619 part_die->has_byte_size = 1;
18620 break;
18621 case DW_AT_const_value:
18622 part_die->has_const_value = 1;
18623 break;
18624 case DW_AT_calling_convention:
18625 /* DWARF doesn't provide a way to identify a program's source-level
18626 entry point. DW_AT_calling_convention attributes are only meant
18627 to describe functions' calling conventions.
18628
18629 However, because it's a necessary piece of information in
18630 Fortran, and before DWARF 4 DW_CC_program was the only
18631 piece of debugging information whose definition refers to
18632 a 'main program' at all, several compilers marked Fortran
18633 main programs with DW_CC_program --- even when those
18634 functions use the standard calling conventions.
18635
18636 Although DWARF now specifies a way to provide this
18637 information, we support this practice for backward
18638 compatibility. */
18639 if (DW_UNSND (&attr) == DW_CC_program
18640 && cu->language == language_fortran)
18641 part_die->main_subprogram = 1;
18642 break;
18643 case DW_AT_inline:
18644 if (DW_UNSND (&attr) == DW_INL_inlined
18645 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18646 part_die->may_be_inlined = 1;
18647 break;
18648
18649 case DW_AT_import:
18650 if (part_die->tag == DW_TAG_imported_unit)
18651 {
18652 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
18653 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18654 || cu->per_cu->is_dwz);
18655 }
18656 break;
18657
18658 case DW_AT_main_subprogram:
18659 part_die->main_subprogram = DW_UNSND (&attr);
18660 break;
18661
18662 default:
18663 break;
18664 }
18665 }
18666
18667 if (high_pc_relative)
18668 part_die->highpc += part_die->lowpc;
18669
18670 if (has_low_pc_attr && has_high_pc_attr)
18671 {
18672 /* When using the GNU linker, .gnu.linkonce. sections are used to
18673 eliminate duplicate copies of functions and vtables and such.
18674 The linker will arbitrarily choose one and discard the others.
18675 The AT_*_pc values for such functions refer to local labels in
18676 these sections. If the section from that file was discarded, the
18677 labels are not in the output, so the relocs get a value of 0.
18678 If this is a discarded function, mark the pc bounds as invalid,
18679 so that GDB will ignore it. */
18680 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18681 {
18682 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18683
18684 complaint (&symfile_complaints,
18685 _("DW_AT_low_pc %s is zero "
18686 "for DIE at 0x%x [in module %s]"),
18687 paddress (gdbarch, part_die->lowpc),
18688 to_underlying (part_die->sect_off), objfile_name (objfile));
18689 }
18690 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18691 else if (part_die->lowpc >= part_die->highpc)
18692 {
18693 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18694
18695 complaint (&symfile_complaints,
18696 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18697 "for DIE at 0x%x [in module %s]"),
18698 paddress (gdbarch, part_die->lowpc),
18699 paddress (gdbarch, part_die->highpc),
18700 to_underlying (part_die->sect_off),
18701 objfile_name (objfile));
18702 }
18703 else
18704 part_die->has_pc_info = 1;
18705 }
18706
18707 return info_ptr;
18708 }
18709
18710 /* Find a cached partial DIE at OFFSET in CU. */
18711
18712 static struct partial_die_info *
18713 find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
18714 {
18715 struct partial_die_info *lookup_die = NULL;
18716 struct partial_die_info part_die;
18717
18718 part_die.sect_off = sect_off;
18719 lookup_die = ((struct partial_die_info *)
18720 htab_find_with_hash (cu->partial_dies, &part_die,
18721 to_underlying (sect_off)));
18722
18723 return lookup_die;
18724 }
18725
18726 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18727 except in the case of .debug_types DIEs which do not reference
18728 outside their CU (they do however referencing other types via
18729 DW_FORM_ref_sig8). */
18730
18731 static struct partial_die_info *
18732 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18733 {
18734 struct dwarf2_per_objfile *dwarf2_per_objfile
18735 = cu->per_cu->dwarf2_per_objfile;
18736 struct objfile *objfile = dwarf2_per_objfile->objfile;
18737 struct dwarf2_per_cu_data *per_cu = NULL;
18738 struct partial_die_info *pd = NULL;
18739
18740 if (offset_in_dwz == cu->per_cu->is_dwz
18741 && offset_in_cu_p (&cu->header, sect_off))
18742 {
18743 pd = find_partial_die_in_comp_unit (sect_off, cu);
18744 if (pd != NULL)
18745 return pd;
18746 /* We missed recording what we needed.
18747 Load all dies and try again. */
18748 per_cu = cu->per_cu;
18749 }
18750 else
18751 {
18752 /* TUs don't reference other CUs/TUs (except via type signatures). */
18753 if (cu->per_cu->is_debug_types)
18754 {
18755 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
18756 " external reference to offset 0x%x [in module %s].\n"),
18757 to_underlying (cu->header.sect_off), to_underlying (sect_off),
18758 bfd_get_filename (objfile->obfd));
18759 }
18760 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18761 dwarf2_per_objfile);
18762
18763 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18764 load_partial_comp_unit (per_cu);
18765
18766 per_cu->cu->last_used = 0;
18767 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
18768 }
18769
18770 /* If we didn't find it, and not all dies have been loaded,
18771 load them all and try again. */
18772
18773 if (pd == NULL && per_cu->load_all_dies == 0)
18774 {
18775 per_cu->load_all_dies = 1;
18776
18777 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18778 THIS_CU->cu may already be in use. So we can't just free it and
18779 replace its DIEs with the ones we read in. Instead, we leave those
18780 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18781 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18782 set. */
18783 load_partial_comp_unit (per_cu);
18784
18785 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
18786 }
18787
18788 if (pd == NULL)
18789 internal_error (__FILE__, __LINE__,
18790 _("could not find partial DIE 0x%x "
18791 "in cache [from module %s]\n"),
18792 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
18793 return pd;
18794 }
18795
18796 /* See if we can figure out if the class lives in a namespace. We do
18797 this by looking for a member function; its demangled name will
18798 contain namespace info, if there is any. */
18799
18800 static void
18801 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18802 struct dwarf2_cu *cu)
18803 {
18804 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18805 what template types look like, because the demangler
18806 frequently doesn't give the same name as the debug info. We
18807 could fix this by only using the demangled name to get the
18808 prefix (but see comment in read_structure_type). */
18809
18810 struct partial_die_info *real_pdi;
18811 struct partial_die_info *child_pdi;
18812
18813 /* If this DIE (this DIE's specification, if any) has a parent, then
18814 we should not do this. We'll prepend the parent's fully qualified
18815 name when we create the partial symbol. */
18816
18817 real_pdi = struct_pdi;
18818 while (real_pdi->has_specification)
18819 real_pdi = find_partial_die (real_pdi->spec_offset,
18820 real_pdi->spec_is_dwz, cu);
18821
18822 if (real_pdi->die_parent != NULL)
18823 return;
18824
18825 for (child_pdi = struct_pdi->die_child;
18826 child_pdi != NULL;
18827 child_pdi = child_pdi->die_sibling)
18828 {
18829 if (child_pdi->tag == DW_TAG_subprogram
18830 && child_pdi->linkage_name != NULL)
18831 {
18832 char *actual_class_name
18833 = language_class_name_from_physname (cu->language_defn,
18834 child_pdi->linkage_name);
18835 if (actual_class_name != NULL)
18836 {
18837 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18838 struct_pdi->name
18839 = ((const char *)
18840 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18841 actual_class_name,
18842 strlen (actual_class_name)));
18843 xfree (actual_class_name);
18844 }
18845 break;
18846 }
18847 }
18848 }
18849
18850 /* Adjust PART_DIE before generating a symbol for it. This function
18851 may set the is_external flag or change the DIE's name. */
18852
18853 static void
18854 fixup_partial_die (struct partial_die_info *part_die,
18855 struct dwarf2_cu *cu)
18856 {
18857 /* Once we've fixed up a die, there's no point in doing so again.
18858 This also avoids a memory leak if we were to call
18859 guess_partial_die_structure_name multiple times. */
18860 if (part_die->fixup_called)
18861 return;
18862
18863 /* If we found a reference attribute and the DIE has no name, try
18864 to find a name in the referred to DIE. */
18865
18866 if (part_die->name == NULL && part_die->has_specification)
18867 {
18868 struct partial_die_info *spec_die;
18869
18870 spec_die = find_partial_die (part_die->spec_offset,
18871 part_die->spec_is_dwz, cu);
18872
18873 fixup_partial_die (spec_die, cu);
18874
18875 if (spec_die->name)
18876 {
18877 part_die->name = spec_die->name;
18878
18879 /* Copy DW_AT_external attribute if it is set. */
18880 if (spec_die->is_external)
18881 part_die->is_external = spec_die->is_external;
18882 }
18883 }
18884
18885 /* Set default names for some unnamed DIEs. */
18886
18887 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
18888 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
18889
18890 /* If there is no parent die to provide a namespace, and there are
18891 children, see if we can determine the namespace from their linkage
18892 name. */
18893 if (cu->language == language_cplus
18894 && !VEC_empty (dwarf2_section_info_def,
18895 cu->per_cu->dwarf2_per_objfile->types)
18896 && part_die->die_parent == NULL
18897 && part_die->has_children
18898 && (part_die->tag == DW_TAG_class_type
18899 || part_die->tag == DW_TAG_structure_type
18900 || part_die->tag == DW_TAG_union_type))
18901 guess_partial_die_structure_name (part_die, cu);
18902
18903 /* GCC might emit a nameless struct or union that has a linkage
18904 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18905 if (part_die->name == NULL
18906 && (part_die->tag == DW_TAG_class_type
18907 || part_die->tag == DW_TAG_interface_type
18908 || part_die->tag == DW_TAG_structure_type
18909 || part_die->tag == DW_TAG_union_type)
18910 && part_die->linkage_name != NULL)
18911 {
18912 char *demangled;
18913
18914 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
18915 if (demangled)
18916 {
18917 const char *base;
18918
18919 /* Strip any leading namespaces/classes, keep only the base name.
18920 DW_AT_name for named DIEs does not contain the prefixes. */
18921 base = strrchr (demangled, ':');
18922 if (base && base > demangled && base[-1] == ':')
18923 base++;
18924 else
18925 base = demangled;
18926
18927 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18928 part_die->name
18929 = ((const char *)
18930 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18931 base, strlen (base)));
18932 xfree (demangled);
18933 }
18934 }
18935
18936 part_die->fixup_called = 1;
18937 }
18938
18939 /* Read an attribute value described by an attribute form. */
18940
18941 static const gdb_byte *
18942 read_attribute_value (const struct die_reader_specs *reader,
18943 struct attribute *attr, unsigned form,
18944 LONGEST implicit_const, const gdb_byte *info_ptr)
18945 {
18946 struct dwarf2_cu *cu = reader->cu;
18947 struct dwarf2_per_objfile *dwarf2_per_objfile
18948 = cu->per_cu->dwarf2_per_objfile;
18949 struct objfile *objfile = dwarf2_per_objfile->objfile;
18950 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18951 bfd *abfd = reader->abfd;
18952 struct comp_unit_head *cu_header = &cu->header;
18953 unsigned int bytes_read;
18954 struct dwarf_block *blk;
18955
18956 attr->form = (enum dwarf_form) form;
18957 switch (form)
18958 {
18959 case DW_FORM_ref_addr:
18960 if (cu->header.version == 2)
18961 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18962 else
18963 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18964 &cu->header, &bytes_read);
18965 info_ptr += bytes_read;
18966 break;
18967 case DW_FORM_GNU_ref_alt:
18968 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18969 info_ptr += bytes_read;
18970 break;
18971 case DW_FORM_addr:
18972 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18973 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18974 info_ptr += bytes_read;
18975 break;
18976 case DW_FORM_block2:
18977 blk = dwarf_alloc_block (cu);
18978 blk->size = read_2_bytes (abfd, info_ptr);
18979 info_ptr += 2;
18980 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18981 info_ptr += blk->size;
18982 DW_BLOCK (attr) = blk;
18983 break;
18984 case DW_FORM_block4:
18985 blk = dwarf_alloc_block (cu);
18986 blk->size = read_4_bytes (abfd, info_ptr);
18987 info_ptr += 4;
18988 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18989 info_ptr += blk->size;
18990 DW_BLOCK (attr) = blk;
18991 break;
18992 case DW_FORM_data2:
18993 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18994 info_ptr += 2;
18995 break;
18996 case DW_FORM_data4:
18997 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18998 info_ptr += 4;
18999 break;
19000 case DW_FORM_data8:
19001 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19002 info_ptr += 8;
19003 break;
19004 case DW_FORM_data16:
19005 blk = dwarf_alloc_block (cu);
19006 blk->size = 16;
19007 blk->data = read_n_bytes (abfd, info_ptr, 16);
19008 info_ptr += 16;
19009 DW_BLOCK (attr) = blk;
19010 break;
19011 case DW_FORM_sec_offset:
19012 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19013 info_ptr += bytes_read;
19014 break;
19015 case DW_FORM_string:
19016 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19017 DW_STRING_IS_CANONICAL (attr) = 0;
19018 info_ptr += bytes_read;
19019 break;
19020 case DW_FORM_strp:
19021 if (!cu->per_cu->is_dwz)
19022 {
19023 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19024 abfd, info_ptr, cu_header,
19025 &bytes_read);
19026 DW_STRING_IS_CANONICAL (attr) = 0;
19027 info_ptr += bytes_read;
19028 break;
19029 }
19030 /* FALLTHROUGH */
19031 case DW_FORM_line_strp:
19032 if (!cu->per_cu->is_dwz)
19033 {
19034 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19035 abfd, info_ptr,
19036 cu_header, &bytes_read);
19037 DW_STRING_IS_CANONICAL (attr) = 0;
19038 info_ptr += bytes_read;
19039 break;
19040 }
19041 /* FALLTHROUGH */
19042 case DW_FORM_GNU_strp_alt:
19043 {
19044 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19045 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19046 &bytes_read);
19047
19048 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19049 dwz, str_offset);
19050 DW_STRING_IS_CANONICAL (attr) = 0;
19051 info_ptr += bytes_read;
19052 }
19053 break;
19054 case DW_FORM_exprloc:
19055 case DW_FORM_block:
19056 blk = dwarf_alloc_block (cu);
19057 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19058 info_ptr += bytes_read;
19059 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19060 info_ptr += blk->size;
19061 DW_BLOCK (attr) = blk;
19062 break;
19063 case DW_FORM_block1:
19064 blk = dwarf_alloc_block (cu);
19065 blk->size = read_1_byte (abfd, info_ptr);
19066 info_ptr += 1;
19067 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19068 info_ptr += blk->size;
19069 DW_BLOCK (attr) = blk;
19070 break;
19071 case DW_FORM_data1:
19072 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19073 info_ptr += 1;
19074 break;
19075 case DW_FORM_flag:
19076 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19077 info_ptr += 1;
19078 break;
19079 case DW_FORM_flag_present:
19080 DW_UNSND (attr) = 1;
19081 break;
19082 case DW_FORM_sdata:
19083 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19084 info_ptr += bytes_read;
19085 break;
19086 case DW_FORM_udata:
19087 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19088 info_ptr += bytes_read;
19089 break;
19090 case DW_FORM_ref1:
19091 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19092 + read_1_byte (abfd, info_ptr));
19093 info_ptr += 1;
19094 break;
19095 case DW_FORM_ref2:
19096 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19097 + read_2_bytes (abfd, info_ptr));
19098 info_ptr += 2;
19099 break;
19100 case DW_FORM_ref4:
19101 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19102 + read_4_bytes (abfd, info_ptr));
19103 info_ptr += 4;
19104 break;
19105 case DW_FORM_ref8:
19106 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19107 + read_8_bytes (abfd, info_ptr));
19108 info_ptr += 8;
19109 break;
19110 case DW_FORM_ref_sig8:
19111 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19112 info_ptr += 8;
19113 break;
19114 case DW_FORM_ref_udata:
19115 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19116 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19117 info_ptr += bytes_read;
19118 break;
19119 case DW_FORM_indirect:
19120 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19121 info_ptr += bytes_read;
19122 if (form == DW_FORM_implicit_const)
19123 {
19124 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19125 info_ptr += bytes_read;
19126 }
19127 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19128 info_ptr);
19129 break;
19130 case DW_FORM_implicit_const:
19131 DW_SND (attr) = implicit_const;
19132 break;
19133 case DW_FORM_GNU_addr_index:
19134 if (reader->dwo_file == NULL)
19135 {
19136 /* For now flag a hard error.
19137 Later we can turn this into a complaint. */
19138 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19139 dwarf_form_name (form),
19140 bfd_get_filename (abfd));
19141 }
19142 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19143 info_ptr += bytes_read;
19144 break;
19145 case DW_FORM_GNU_str_index:
19146 if (reader->dwo_file == NULL)
19147 {
19148 /* For now flag a hard error.
19149 Later we can turn this into a complaint if warranted. */
19150 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19151 dwarf_form_name (form),
19152 bfd_get_filename (abfd));
19153 }
19154 {
19155 ULONGEST str_index =
19156 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19157
19158 DW_STRING (attr) = read_str_index (reader, str_index);
19159 DW_STRING_IS_CANONICAL (attr) = 0;
19160 info_ptr += bytes_read;
19161 }
19162 break;
19163 default:
19164 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19165 dwarf_form_name (form),
19166 bfd_get_filename (abfd));
19167 }
19168
19169 /* Super hack. */
19170 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19171 attr->form = DW_FORM_GNU_ref_alt;
19172
19173 /* We have seen instances where the compiler tried to emit a byte
19174 size attribute of -1 which ended up being encoded as an unsigned
19175 0xffffffff. Although 0xffffffff is technically a valid size value,
19176 an object of this size seems pretty unlikely so we can relatively
19177 safely treat these cases as if the size attribute was invalid and
19178 treat them as zero by default. */
19179 if (attr->name == DW_AT_byte_size
19180 && form == DW_FORM_data4
19181 && DW_UNSND (attr) >= 0xffffffff)
19182 {
19183 complaint
19184 (&symfile_complaints,
19185 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19186 hex_string (DW_UNSND (attr)));
19187 DW_UNSND (attr) = 0;
19188 }
19189
19190 return info_ptr;
19191 }
19192
19193 /* Read an attribute described by an abbreviated attribute. */
19194
19195 static const gdb_byte *
19196 read_attribute (const struct die_reader_specs *reader,
19197 struct attribute *attr, struct attr_abbrev *abbrev,
19198 const gdb_byte *info_ptr)
19199 {
19200 attr->name = abbrev->name;
19201 return read_attribute_value (reader, attr, abbrev->form,
19202 abbrev->implicit_const, info_ptr);
19203 }
19204
19205 /* Read dwarf information from a buffer. */
19206
19207 static unsigned int
19208 read_1_byte (bfd *abfd, const gdb_byte *buf)
19209 {
19210 return bfd_get_8 (abfd, buf);
19211 }
19212
19213 static int
19214 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19215 {
19216 return bfd_get_signed_8 (abfd, buf);
19217 }
19218
19219 static unsigned int
19220 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19221 {
19222 return bfd_get_16 (abfd, buf);
19223 }
19224
19225 static int
19226 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19227 {
19228 return bfd_get_signed_16 (abfd, buf);
19229 }
19230
19231 static unsigned int
19232 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19233 {
19234 return bfd_get_32 (abfd, buf);
19235 }
19236
19237 static int
19238 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19239 {
19240 return bfd_get_signed_32 (abfd, buf);
19241 }
19242
19243 static ULONGEST
19244 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19245 {
19246 return bfd_get_64 (abfd, buf);
19247 }
19248
19249 static CORE_ADDR
19250 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19251 unsigned int *bytes_read)
19252 {
19253 struct comp_unit_head *cu_header = &cu->header;
19254 CORE_ADDR retval = 0;
19255
19256 if (cu_header->signed_addr_p)
19257 {
19258 switch (cu_header->addr_size)
19259 {
19260 case 2:
19261 retval = bfd_get_signed_16 (abfd, buf);
19262 break;
19263 case 4:
19264 retval = bfd_get_signed_32 (abfd, buf);
19265 break;
19266 case 8:
19267 retval = bfd_get_signed_64 (abfd, buf);
19268 break;
19269 default:
19270 internal_error (__FILE__, __LINE__,
19271 _("read_address: bad switch, signed [in module %s]"),
19272 bfd_get_filename (abfd));
19273 }
19274 }
19275 else
19276 {
19277 switch (cu_header->addr_size)
19278 {
19279 case 2:
19280 retval = bfd_get_16 (abfd, buf);
19281 break;
19282 case 4:
19283 retval = bfd_get_32 (abfd, buf);
19284 break;
19285 case 8:
19286 retval = bfd_get_64 (abfd, buf);
19287 break;
19288 default:
19289 internal_error (__FILE__, __LINE__,
19290 _("read_address: bad switch, "
19291 "unsigned [in module %s]"),
19292 bfd_get_filename (abfd));
19293 }
19294 }
19295
19296 *bytes_read = cu_header->addr_size;
19297 return retval;
19298 }
19299
19300 /* Read the initial length from a section. The (draft) DWARF 3
19301 specification allows the initial length to take up either 4 bytes
19302 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19303 bytes describe the length and all offsets will be 8 bytes in length
19304 instead of 4.
19305
19306 An older, non-standard 64-bit format is also handled by this
19307 function. The older format in question stores the initial length
19308 as an 8-byte quantity without an escape value. Lengths greater
19309 than 2^32 aren't very common which means that the initial 4 bytes
19310 is almost always zero. Since a length value of zero doesn't make
19311 sense for the 32-bit format, this initial zero can be considered to
19312 be an escape value which indicates the presence of the older 64-bit
19313 format. As written, the code can't detect (old format) lengths
19314 greater than 4GB. If it becomes necessary to handle lengths
19315 somewhat larger than 4GB, we could allow other small values (such
19316 as the non-sensical values of 1, 2, and 3) to also be used as
19317 escape values indicating the presence of the old format.
19318
19319 The value returned via bytes_read should be used to increment the
19320 relevant pointer after calling read_initial_length().
19321
19322 [ Note: read_initial_length() and read_offset() are based on the
19323 document entitled "DWARF Debugging Information Format", revision
19324 3, draft 8, dated November 19, 2001. This document was obtained
19325 from:
19326
19327 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19328
19329 This document is only a draft and is subject to change. (So beware.)
19330
19331 Details regarding the older, non-standard 64-bit format were
19332 determined empirically by examining 64-bit ELF files produced by
19333 the SGI toolchain on an IRIX 6.5 machine.
19334
19335 - Kevin, July 16, 2002
19336 ] */
19337
19338 static LONGEST
19339 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19340 {
19341 LONGEST length = bfd_get_32 (abfd, buf);
19342
19343 if (length == 0xffffffff)
19344 {
19345 length = bfd_get_64 (abfd, buf + 4);
19346 *bytes_read = 12;
19347 }
19348 else if (length == 0)
19349 {
19350 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19351 length = bfd_get_64 (abfd, buf);
19352 *bytes_read = 8;
19353 }
19354 else
19355 {
19356 *bytes_read = 4;
19357 }
19358
19359 return length;
19360 }
19361
19362 /* Cover function for read_initial_length.
19363 Returns the length of the object at BUF, and stores the size of the
19364 initial length in *BYTES_READ and stores the size that offsets will be in
19365 *OFFSET_SIZE.
19366 If the initial length size is not equivalent to that specified in
19367 CU_HEADER then issue a complaint.
19368 This is useful when reading non-comp-unit headers. */
19369
19370 static LONGEST
19371 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19372 const struct comp_unit_head *cu_header,
19373 unsigned int *bytes_read,
19374 unsigned int *offset_size)
19375 {
19376 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19377
19378 gdb_assert (cu_header->initial_length_size == 4
19379 || cu_header->initial_length_size == 8
19380 || cu_header->initial_length_size == 12);
19381
19382 if (cu_header->initial_length_size != *bytes_read)
19383 complaint (&symfile_complaints,
19384 _("intermixed 32-bit and 64-bit DWARF sections"));
19385
19386 *offset_size = (*bytes_read == 4) ? 4 : 8;
19387 return length;
19388 }
19389
19390 /* Read an offset from the data stream. The size of the offset is
19391 given by cu_header->offset_size. */
19392
19393 static LONGEST
19394 read_offset (bfd *abfd, const gdb_byte *buf,
19395 const struct comp_unit_head *cu_header,
19396 unsigned int *bytes_read)
19397 {
19398 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19399
19400 *bytes_read = cu_header->offset_size;
19401 return offset;
19402 }
19403
19404 /* Read an offset from the data stream. */
19405
19406 static LONGEST
19407 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19408 {
19409 LONGEST retval = 0;
19410
19411 switch (offset_size)
19412 {
19413 case 4:
19414 retval = bfd_get_32 (abfd, buf);
19415 break;
19416 case 8:
19417 retval = bfd_get_64 (abfd, buf);
19418 break;
19419 default:
19420 internal_error (__FILE__, __LINE__,
19421 _("read_offset_1: bad switch [in module %s]"),
19422 bfd_get_filename (abfd));
19423 }
19424
19425 return retval;
19426 }
19427
19428 static const gdb_byte *
19429 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19430 {
19431 /* If the size of a host char is 8 bits, we can return a pointer
19432 to the buffer, otherwise we have to copy the data to a buffer
19433 allocated on the temporary obstack. */
19434 gdb_assert (HOST_CHAR_BIT == 8);
19435 return buf;
19436 }
19437
19438 static const char *
19439 read_direct_string (bfd *abfd, const gdb_byte *buf,
19440 unsigned int *bytes_read_ptr)
19441 {
19442 /* If the size of a host char is 8 bits, we can return a pointer
19443 to the string, otherwise we have to copy the string to a buffer
19444 allocated on the temporary obstack. */
19445 gdb_assert (HOST_CHAR_BIT == 8);
19446 if (*buf == '\0')
19447 {
19448 *bytes_read_ptr = 1;
19449 return NULL;
19450 }
19451 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19452 return (const char *) buf;
19453 }
19454
19455 /* Return pointer to string at section SECT offset STR_OFFSET with error
19456 reporting strings FORM_NAME and SECT_NAME. */
19457
19458 static const char *
19459 read_indirect_string_at_offset_from (struct objfile *objfile,
19460 bfd *abfd, LONGEST str_offset,
19461 struct dwarf2_section_info *sect,
19462 const char *form_name,
19463 const char *sect_name)
19464 {
19465 dwarf2_read_section (objfile, sect);
19466 if (sect->buffer == NULL)
19467 error (_("%s used without %s section [in module %s]"),
19468 form_name, sect_name, bfd_get_filename (abfd));
19469 if (str_offset >= sect->size)
19470 error (_("%s pointing outside of %s section [in module %s]"),
19471 form_name, sect_name, bfd_get_filename (abfd));
19472 gdb_assert (HOST_CHAR_BIT == 8);
19473 if (sect->buffer[str_offset] == '\0')
19474 return NULL;
19475 return (const char *) (sect->buffer + str_offset);
19476 }
19477
19478 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19479
19480 static const char *
19481 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19482 bfd *abfd, LONGEST str_offset)
19483 {
19484 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19485 abfd, str_offset,
19486 &dwarf2_per_objfile->str,
19487 "DW_FORM_strp", ".debug_str");
19488 }
19489
19490 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19491
19492 static const char *
19493 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19494 bfd *abfd, LONGEST str_offset)
19495 {
19496 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19497 abfd, str_offset,
19498 &dwarf2_per_objfile->line_str,
19499 "DW_FORM_line_strp",
19500 ".debug_line_str");
19501 }
19502
19503 /* Read a string at offset STR_OFFSET in the .debug_str section from
19504 the .dwz file DWZ. Throw an error if the offset is too large. If
19505 the string consists of a single NUL byte, return NULL; otherwise
19506 return a pointer to the string. */
19507
19508 static const char *
19509 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19510 LONGEST str_offset)
19511 {
19512 dwarf2_read_section (objfile, &dwz->str);
19513
19514 if (dwz->str.buffer == NULL)
19515 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19516 "section [in module %s]"),
19517 bfd_get_filename (dwz->dwz_bfd));
19518 if (str_offset >= dwz->str.size)
19519 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19520 ".debug_str section [in module %s]"),
19521 bfd_get_filename (dwz->dwz_bfd));
19522 gdb_assert (HOST_CHAR_BIT == 8);
19523 if (dwz->str.buffer[str_offset] == '\0')
19524 return NULL;
19525 return (const char *) (dwz->str.buffer + str_offset);
19526 }
19527
19528 /* Return pointer to string at .debug_str offset as read from BUF.
19529 BUF is assumed to be in a compilation unit described by CU_HEADER.
19530 Return *BYTES_READ_PTR count of bytes read from BUF. */
19531
19532 static const char *
19533 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19534 const gdb_byte *buf,
19535 const struct comp_unit_head *cu_header,
19536 unsigned int *bytes_read_ptr)
19537 {
19538 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19539
19540 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19541 }
19542
19543 /* Return pointer to string at .debug_line_str offset as read from BUF.
19544 BUF is assumed to be in a compilation unit described by CU_HEADER.
19545 Return *BYTES_READ_PTR count of bytes read from BUF. */
19546
19547 static const char *
19548 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19549 bfd *abfd, const gdb_byte *buf,
19550 const struct comp_unit_head *cu_header,
19551 unsigned int *bytes_read_ptr)
19552 {
19553 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19554
19555 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19556 str_offset);
19557 }
19558
19559 ULONGEST
19560 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19561 unsigned int *bytes_read_ptr)
19562 {
19563 ULONGEST result;
19564 unsigned int num_read;
19565 int shift;
19566 unsigned char byte;
19567
19568 result = 0;
19569 shift = 0;
19570 num_read = 0;
19571 while (1)
19572 {
19573 byte = bfd_get_8 (abfd, buf);
19574 buf++;
19575 num_read++;
19576 result |= ((ULONGEST) (byte & 127) << shift);
19577 if ((byte & 128) == 0)
19578 {
19579 break;
19580 }
19581 shift += 7;
19582 }
19583 *bytes_read_ptr = num_read;
19584 return result;
19585 }
19586
19587 static LONGEST
19588 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19589 unsigned int *bytes_read_ptr)
19590 {
19591 LONGEST result;
19592 int shift, num_read;
19593 unsigned char byte;
19594
19595 result = 0;
19596 shift = 0;
19597 num_read = 0;
19598 while (1)
19599 {
19600 byte = bfd_get_8 (abfd, buf);
19601 buf++;
19602 num_read++;
19603 result |= ((LONGEST) (byte & 127) << shift);
19604 shift += 7;
19605 if ((byte & 128) == 0)
19606 {
19607 break;
19608 }
19609 }
19610 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19611 result |= -(((LONGEST) 1) << shift);
19612 *bytes_read_ptr = num_read;
19613 return result;
19614 }
19615
19616 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19617 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19618 ADDR_SIZE is the size of addresses from the CU header. */
19619
19620 static CORE_ADDR
19621 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19622 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19623 {
19624 struct objfile *objfile = dwarf2_per_objfile->objfile;
19625 bfd *abfd = objfile->obfd;
19626 const gdb_byte *info_ptr;
19627
19628 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19629 if (dwarf2_per_objfile->addr.buffer == NULL)
19630 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19631 objfile_name (objfile));
19632 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19633 error (_("DW_FORM_addr_index pointing outside of "
19634 ".debug_addr section [in module %s]"),
19635 objfile_name (objfile));
19636 info_ptr = (dwarf2_per_objfile->addr.buffer
19637 + addr_base + addr_index * addr_size);
19638 if (addr_size == 4)
19639 return bfd_get_32 (abfd, info_ptr);
19640 else
19641 return bfd_get_64 (abfd, info_ptr);
19642 }
19643
19644 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19645
19646 static CORE_ADDR
19647 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19648 {
19649 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19650 cu->addr_base, cu->header.addr_size);
19651 }
19652
19653 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19654
19655 static CORE_ADDR
19656 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19657 unsigned int *bytes_read)
19658 {
19659 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19660 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19661
19662 return read_addr_index (cu, addr_index);
19663 }
19664
19665 /* Data structure to pass results from dwarf2_read_addr_index_reader
19666 back to dwarf2_read_addr_index. */
19667
19668 struct dwarf2_read_addr_index_data
19669 {
19670 ULONGEST addr_base;
19671 int addr_size;
19672 };
19673
19674 /* die_reader_func for dwarf2_read_addr_index. */
19675
19676 static void
19677 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19678 const gdb_byte *info_ptr,
19679 struct die_info *comp_unit_die,
19680 int has_children,
19681 void *data)
19682 {
19683 struct dwarf2_cu *cu = reader->cu;
19684 struct dwarf2_read_addr_index_data *aidata =
19685 (struct dwarf2_read_addr_index_data *) data;
19686
19687 aidata->addr_base = cu->addr_base;
19688 aidata->addr_size = cu->header.addr_size;
19689 }
19690
19691 /* Given an index in .debug_addr, fetch the value.
19692 NOTE: This can be called during dwarf expression evaluation,
19693 long after the debug information has been read, and thus per_cu->cu
19694 may no longer exist. */
19695
19696 CORE_ADDR
19697 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19698 unsigned int addr_index)
19699 {
19700 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19701 struct objfile *objfile = dwarf2_per_objfile->objfile;
19702 struct dwarf2_cu *cu = per_cu->cu;
19703 ULONGEST addr_base;
19704 int addr_size;
19705
19706 /* We need addr_base and addr_size.
19707 If we don't have PER_CU->cu, we have to get it.
19708 Nasty, but the alternative is storing the needed info in PER_CU,
19709 which at this point doesn't seem justified: it's not clear how frequently
19710 it would get used and it would increase the size of every PER_CU.
19711 Entry points like dwarf2_per_cu_addr_size do a similar thing
19712 so we're not in uncharted territory here.
19713 Alas we need to be a bit more complicated as addr_base is contained
19714 in the DIE.
19715
19716 We don't need to read the entire CU(/TU).
19717 We just need the header and top level die.
19718
19719 IWBN to use the aging mechanism to let us lazily later discard the CU.
19720 For now we skip this optimization. */
19721
19722 if (cu != NULL)
19723 {
19724 addr_base = cu->addr_base;
19725 addr_size = cu->header.addr_size;
19726 }
19727 else
19728 {
19729 struct dwarf2_read_addr_index_data aidata;
19730
19731 /* Note: We can't use init_cutu_and_read_dies_simple here,
19732 we need addr_base. */
19733 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19734 dwarf2_read_addr_index_reader, &aidata);
19735 addr_base = aidata.addr_base;
19736 addr_size = aidata.addr_size;
19737 }
19738
19739 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19740 addr_size);
19741 }
19742
19743 /* Given a DW_FORM_GNU_str_index, fetch the string.
19744 This is only used by the Fission support. */
19745
19746 static const char *
19747 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19748 {
19749 struct dwarf2_cu *cu = reader->cu;
19750 struct dwarf2_per_objfile *dwarf2_per_objfile
19751 = cu->per_cu->dwarf2_per_objfile;
19752 struct objfile *objfile = dwarf2_per_objfile->objfile;
19753 const char *objf_name = objfile_name (objfile);
19754 bfd *abfd = objfile->obfd;
19755 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19756 struct dwarf2_section_info *str_offsets_section =
19757 &reader->dwo_file->sections.str_offsets;
19758 const gdb_byte *info_ptr;
19759 ULONGEST str_offset;
19760 static const char form_name[] = "DW_FORM_GNU_str_index";
19761
19762 dwarf2_read_section (objfile, str_section);
19763 dwarf2_read_section (objfile, str_offsets_section);
19764 if (str_section->buffer == NULL)
19765 error (_("%s used without .debug_str.dwo section"
19766 " in CU at offset 0x%x [in module %s]"),
19767 form_name, to_underlying (cu->header.sect_off), objf_name);
19768 if (str_offsets_section->buffer == NULL)
19769 error (_("%s used without .debug_str_offsets.dwo section"
19770 " in CU at offset 0x%x [in module %s]"),
19771 form_name, to_underlying (cu->header.sect_off), objf_name);
19772 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19773 error (_("%s pointing outside of .debug_str_offsets.dwo"
19774 " section in CU at offset 0x%x [in module %s]"),
19775 form_name, to_underlying (cu->header.sect_off), objf_name);
19776 info_ptr = (str_offsets_section->buffer
19777 + str_index * cu->header.offset_size);
19778 if (cu->header.offset_size == 4)
19779 str_offset = bfd_get_32 (abfd, info_ptr);
19780 else
19781 str_offset = bfd_get_64 (abfd, info_ptr);
19782 if (str_offset >= str_section->size)
19783 error (_("Offset from %s pointing outside of"
19784 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
19785 form_name, to_underlying (cu->header.sect_off), objf_name);
19786 return (const char *) (str_section->buffer + str_offset);
19787 }
19788
19789 /* Return the length of an LEB128 number in BUF. */
19790
19791 static int
19792 leb128_size (const gdb_byte *buf)
19793 {
19794 const gdb_byte *begin = buf;
19795 gdb_byte byte;
19796
19797 while (1)
19798 {
19799 byte = *buf++;
19800 if ((byte & 128) == 0)
19801 return buf - begin;
19802 }
19803 }
19804
19805 static void
19806 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19807 {
19808 switch (lang)
19809 {
19810 case DW_LANG_C89:
19811 case DW_LANG_C99:
19812 case DW_LANG_C11:
19813 case DW_LANG_C:
19814 case DW_LANG_UPC:
19815 cu->language = language_c;
19816 break;
19817 case DW_LANG_Java:
19818 case DW_LANG_C_plus_plus:
19819 case DW_LANG_C_plus_plus_11:
19820 case DW_LANG_C_plus_plus_14:
19821 cu->language = language_cplus;
19822 break;
19823 case DW_LANG_D:
19824 cu->language = language_d;
19825 break;
19826 case DW_LANG_Fortran77:
19827 case DW_LANG_Fortran90:
19828 case DW_LANG_Fortran95:
19829 case DW_LANG_Fortran03:
19830 case DW_LANG_Fortran08:
19831 cu->language = language_fortran;
19832 break;
19833 case DW_LANG_Go:
19834 cu->language = language_go;
19835 break;
19836 case DW_LANG_Mips_Assembler:
19837 cu->language = language_asm;
19838 break;
19839 case DW_LANG_Ada83:
19840 case DW_LANG_Ada95:
19841 cu->language = language_ada;
19842 break;
19843 case DW_LANG_Modula2:
19844 cu->language = language_m2;
19845 break;
19846 case DW_LANG_Pascal83:
19847 cu->language = language_pascal;
19848 break;
19849 case DW_LANG_ObjC:
19850 cu->language = language_objc;
19851 break;
19852 case DW_LANG_Rust:
19853 case DW_LANG_Rust_old:
19854 cu->language = language_rust;
19855 break;
19856 case DW_LANG_Cobol74:
19857 case DW_LANG_Cobol85:
19858 default:
19859 cu->language = language_minimal;
19860 break;
19861 }
19862 cu->language_defn = language_def (cu->language);
19863 }
19864
19865 /* Return the named attribute or NULL if not there. */
19866
19867 static struct attribute *
19868 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19869 {
19870 for (;;)
19871 {
19872 unsigned int i;
19873 struct attribute *spec = NULL;
19874
19875 for (i = 0; i < die->num_attrs; ++i)
19876 {
19877 if (die->attrs[i].name == name)
19878 return &die->attrs[i];
19879 if (die->attrs[i].name == DW_AT_specification
19880 || die->attrs[i].name == DW_AT_abstract_origin)
19881 spec = &die->attrs[i];
19882 }
19883
19884 if (!spec)
19885 break;
19886
19887 die = follow_die_ref (die, spec, &cu);
19888 }
19889
19890 return NULL;
19891 }
19892
19893 /* Return the named attribute or NULL if not there,
19894 but do not follow DW_AT_specification, etc.
19895 This is for use in contexts where we're reading .debug_types dies.
19896 Following DW_AT_specification, DW_AT_abstract_origin will take us
19897 back up the chain, and we want to go down. */
19898
19899 static struct attribute *
19900 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19901 {
19902 unsigned int i;
19903
19904 for (i = 0; i < die->num_attrs; ++i)
19905 if (die->attrs[i].name == name)
19906 return &die->attrs[i];
19907
19908 return NULL;
19909 }
19910
19911 /* Return the string associated with a string-typed attribute, or NULL if it
19912 is either not found or is of an incorrect type. */
19913
19914 static const char *
19915 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19916 {
19917 struct attribute *attr;
19918 const char *str = NULL;
19919
19920 attr = dwarf2_attr (die, name, cu);
19921
19922 if (attr != NULL)
19923 {
19924 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19925 || attr->form == DW_FORM_string
19926 || attr->form == DW_FORM_GNU_str_index
19927 || attr->form == DW_FORM_GNU_strp_alt)
19928 str = DW_STRING (attr);
19929 else
19930 complaint (&symfile_complaints,
19931 _("string type expected for attribute %s for "
19932 "DIE at 0x%x in module %s"),
19933 dwarf_attr_name (name), to_underlying (die->sect_off),
19934 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19935 }
19936
19937 return str;
19938 }
19939
19940 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19941 and holds a non-zero value. This function should only be used for
19942 DW_FORM_flag or DW_FORM_flag_present attributes. */
19943
19944 static int
19945 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19946 {
19947 struct attribute *attr = dwarf2_attr (die, name, cu);
19948
19949 return (attr && DW_UNSND (attr));
19950 }
19951
19952 static int
19953 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19954 {
19955 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19956 which value is non-zero. However, we have to be careful with
19957 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19958 (via dwarf2_flag_true_p) follows this attribute. So we may
19959 end up accidently finding a declaration attribute that belongs
19960 to a different DIE referenced by the specification attribute,
19961 even though the given DIE does not have a declaration attribute. */
19962 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19963 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19964 }
19965
19966 /* Return the die giving the specification for DIE, if there is
19967 one. *SPEC_CU is the CU containing DIE on input, and the CU
19968 containing the return value on output. If there is no
19969 specification, but there is an abstract origin, that is
19970 returned. */
19971
19972 static struct die_info *
19973 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19974 {
19975 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19976 *spec_cu);
19977
19978 if (spec_attr == NULL)
19979 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19980
19981 if (spec_attr == NULL)
19982 return NULL;
19983 else
19984 return follow_die_ref (die, spec_attr, spec_cu);
19985 }
19986
19987 /* Stub for free_line_header to match void * callback types. */
19988
19989 static void
19990 free_line_header_voidp (void *arg)
19991 {
19992 struct line_header *lh = (struct line_header *) arg;
19993
19994 delete lh;
19995 }
19996
19997 void
19998 line_header::add_include_dir (const char *include_dir)
19999 {
20000 if (dwarf_line_debug >= 2)
20001 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20002 include_dirs.size () + 1, include_dir);
20003
20004 include_dirs.push_back (include_dir);
20005 }
20006
20007 void
20008 line_header::add_file_name (const char *name,
20009 dir_index d_index,
20010 unsigned int mod_time,
20011 unsigned int length)
20012 {
20013 if (dwarf_line_debug >= 2)
20014 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
20015 (unsigned) file_names.size () + 1, name);
20016
20017 file_names.emplace_back (name, d_index, mod_time, length);
20018 }
20019
20020 /* A convenience function to find the proper .debug_line section for a CU. */
20021
20022 static struct dwarf2_section_info *
20023 get_debug_line_section (struct dwarf2_cu *cu)
20024 {
20025 struct dwarf2_section_info *section;
20026 struct dwarf2_per_objfile *dwarf2_per_objfile
20027 = cu->per_cu->dwarf2_per_objfile;
20028
20029 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20030 DWO file. */
20031 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20032 section = &cu->dwo_unit->dwo_file->sections.line;
20033 else if (cu->per_cu->is_dwz)
20034 {
20035 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20036
20037 section = &dwz->line;
20038 }
20039 else
20040 section = &dwarf2_per_objfile->line;
20041
20042 return section;
20043 }
20044
20045 /* Read directory or file name entry format, starting with byte of
20046 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20047 entries count and the entries themselves in the described entry
20048 format. */
20049
20050 static void
20051 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20052 bfd *abfd, const gdb_byte **bufp,
20053 struct line_header *lh,
20054 const struct comp_unit_head *cu_header,
20055 void (*callback) (struct line_header *lh,
20056 const char *name,
20057 dir_index d_index,
20058 unsigned int mod_time,
20059 unsigned int length))
20060 {
20061 gdb_byte format_count, formati;
20062 ULONGEST data_count, datai;
20063 const gdb_byte *buf = *bufp;
20064 const gdb_byte *format_header_data;
20065 unsigned int bytes_read;
20066
20067 format_count = read_1_byte (abfd, buf);
20068 buf += 1;
20069 format_header_data = buf;
20070 for (formati = 0; formati < format_count; formati++)
20071 {
20072 read_unsigned_leb128 (abfd, buf, &bytes_read);
20073 buf += bytes_read;
20074 read_unsigned_leb128 (abfd, buf, &bytes_read);
20075 buf += bytes_read;
20076 }
20077
20078 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20079 buf += bytes_read;
20080 for (datai = 0; datai < data_count; datai++)
20081 {
20082 const gdb_byte *format = format_header_data;
20083 struct file_entry fe;
20084
20085 for (formati = 0; formati < format_count; formati++)
20086 {
20087 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20088 format += bytes_read;
20089
20090 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20091 format += bytes_read;
20092
20093 gdb::optional<const char *> string;
20094 gdb::optional<unsigned int> uint;
20095
20096 switch (form)
20097 {
20098 case DW_FORM_string:
20099 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20100 buf += bytes_read;
20101 break;
20102
20103 case DW_FORM_line_strp:
20104 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20105 abfd, buf,
20106 cu_header,
20107 &bytes_read));
20108 buf += bytes_read;
20109 break;
20110
20111 case DW_FORM_data1:
20112 uint.emplace (read_1_byte (abfd, buf));
20113 buf += 1;
20114 break;
20115
20116 case DW_FORM_data2:
20117 uint.emplace (read_2_bytes (abfd, buf));
20118 buf += 2;
20119 break;
20120
20121 case DW_FORM_data4:
20122 uint.emplace (read_4_bytes (abfd, buf));
20123 buf += 4;
20124 break;
20125
20126 case DW_FORM_data8:
20127 uint.emplace (read_8_bytes (abfd, buf));
20128 buf += 8;
20129 break;
20130
20131 case DW_FORM_udata:
20132 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20133 buf += bytes_read;
20134 break;
20135
20136 case DW_FORM_block:
20137 /* It is valid only for DW_LNCT_timestamp which is ignored by
20138 current GDB. */
20139 break;
20140 }
20141
20142 switch (content_type)
20143 {
20144 case DW_LNCT_path:
20145 if (string.has_value ())
20146 fe.name = *string;
20147 break;
20148 case DW_LNCT_directory_index:
20149 if (uint.has_value ())
20150 fe.d_index = (dir_index) *uint;
20151 break;
20152 case DW_LNCT_timestamp:
20153 if (uint.has_value ())
20154 fe.mod_time = *uint;
20155 break;
20156 case DW_LNCT_size:
20157 if (uint.has_value ())
20158 fe.length = *uint;
20159 break;
20160 case DW_LNCT_MD5:
20161 break;
20162 default:
20163 complaint (&symfile_complaints,
20164 _("Unknown format content type %s"),
20165 pulongest (content_type));
20166 }
20167 }
20168
20169 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20170 }
20171
20172 *bufp = buf;
20173 }
20174
20175 /* Read the statement program header starting at OFFSET in
20176 .debug_line, or .debug_line.dwo. Return a pointer
20177 to a struct line_header, allocated using xmalloc.
20178 Returns NULL if there is a problem reading the header, e.g., if it
20179 has a version we don't understand.
20180
20181 NOTE: the strings in the include directory and file name tables of
20182 the returned object point into the dwarf line section buffer,
20183 and must not be freed. */
20184
20185 static line_header_up
20186 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20187 {
20188 const gdb_byte *line_ptr;
20189 unsigned int bytes_read, offset_size;
20190 int i;
20191 const char *cur_dir, *cur_file;
20192 struct dwarf2_section_info *section;
20193 bfd *abfd;
20194 struct dwarf2_per_objfile *dwarf2_per_objfile
20195 = cu->per_cu->dwarf2_per_objfile;
20196
20197 section = get_debug_line_section (cu);
20198 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20199 if (section->buffer == NULL)
20200 {
20201 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20202 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20203 else
20204 complaint (&symfile_complaints, _("missing .debug_line section"));
20205 return 0;
20206 }
20207
20208 /* We can't do this until we know the section is non-empty.
20209 Only then do we know we have such a section. */
20210 abfd = get_section_bfd_owner (section);
20211
20212 /* Make sure that at least there's room for the total_length field.
20213 That could be 12 bytes long, but we're just going to fudge that. */
20214 if (to_underlying (sect_off) + 4 >= section->size)
20215 {
20216 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20217 return 0;
20218 }
20219
20220 line_header_up lh (new line_header ());
20221
20222 lh->sect_off = sect_off;
20223 lh->offset_in_dwz = cu->per_cu->is_dwz;
20224
20225 line_ptr = section->buffer + to_underlying (sect_off);
20226
20227 /* Read in the header. */
20228 lh->total_length =
20229 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20230 &bytes_read, &offset_size);
20231 line_ptr += bytes_read;
20232 if (line_ptr + lh->total_length > (section->buffer + section->size))
20233 {
20234 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20235 return 0;
20236 }
20237 lh->statement_program_end = line_ptr + lh->total_length;
20238 lh->version = read_2_bytes (abfd, line_ptr);
20239 line_ptr += 2;
20240 if (lh->version > 5)
20241 {
20242 /* This is a version we don't understand. The format could have
20243 changed in ways we don't handle properly so just punt. */
20244 complaint (&symfile_complaints,
20245 _("unsupported version in .debug_line section"));
20246 return NULL;
20247 }
20248 if (lh->version >= 5)
20249 {
20250 gdb_byte segment_selector_size;
20251
20252 /* Skip address size. */
20253 read_1_byte (abfd, line_ptr);
20254 line_ptr += 1;
20255
20256 segment_selector_size = read_1_byte (abfd, line_ptr);
20257 line_ptr += 1;
20258 if (segment_selector_size != 0)
20259 {
20260 complaint (&symfile_complaints,
20261 _("unsupported segment selector size %u "
20262 "in .debug_line section"),
20263 segment_selector_size);
20264 return NULL;
20265 }
20266 }
20267 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20268 line_ptr += offset_size;
20269 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20270 line_ptr += 1;
20271 if (lh->version >= 4)
20272 {
20273 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20274 line_ptr += 1;
20275 }
20276 else
20277 lh->maximum_ops_per_instruction = 1;
20278
20279 if (lh->maximum_ops_per_instruction == 0)
20280 {
20281 lh->maximum_ops_per_instruction = 1;
20282 complaint (&symfile_complaints,
20283 _("invalid maximum_ops_per_instruction "
20284 "in `.debug_line' section"));
20285 }
20286
20287 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20288 line_ptr += 1;
20289 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20290 line_ptr += 1;
20291 lh->line_range = read_1_byte (abfd, line_ptr);
20292 line_ptr += 1;
20293 lh->opcode_base = read_1_byte (abfd, line_ptr);
20294 line_ptr += 1;
20295 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20296
20297 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20298 for (i = 1; i < lh->opcode_base; ++i)
20299 {
20300 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20301 line_ptr += 1;
20302 }
20303
20304 if (lh->version >= 5)
20305 {
20306 /* Read directory table. */
20307 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20308 &cu->header,
20309 [] (struct line_header *lh, const char *name,
20310 dir_index d_index, unsigned int mod_time,
20311 unsigned int length)
20312 {
20313 lh->add_include_dir (name);
20314 });
20315
20316 /* Read file name table. */
20317 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20318 &cu->header,
20319 [] (struct line_header *lh, const char *name,
20320 dir_index d_index, unsigned int mod_time,
20321 unsigned int length)
20322 {
20323 lh->add_file_name (name, d_index, mod_time, length);
20324 });
20325 }
20326 else
20327 {
20328 /* Read directory table. */
20329 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20330 {
20331 line_ptr += bytes_read;
20332 lh->add_include_dir (cur_dir);
20333 }
20334 line_ptr += bytes_read;
20335
20336 /* Read file name table. */
20337 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20338 {
20339 unsigned int mod_time, length;
20340 dir_index d_index;
20341
20342 line_ptr += bytes_read;
20343 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20344 line_ptr += bytes_read;
20345 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20346 line_ptr += bytes_read;
20347 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20348 line_ptr += bytes_read;
20349
20350 lh->add_file_name (cur_file, d_index, mod_time, length);
20351 }
20352 line_ptr += bytes_read;
20353 }
20354 lh->statement_program_start = line_ptr;
20355
20356 if (line_ptr > (section->buffer + section->size))
20357 complaint (&symfile_complaints,
20358 _("line number info header doesn't "
20359 "fit in `.debug_line' section"));
20360
20361 return lh;
20362 }
20363
20364 /* Subroutine of dwarf_decode_lines to simplify it.
20365 Return the file name of the psymtab for included file FILE_INDEX
20366 in line header LH of PST.
20367 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20368 If space for the result is malloc'd, *NAME_HOLDER will be set.
20369 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20370
20371 static const char *
20372 psymtab_include_file_name (const struct line_header *lh, int file_index,
20373 const struct partial_symtab *pst,
20374 const char *comp_dir,
20375 gdb::unique_xmalloc_ptr<char> *name_holder)
20376 {
20377 const file_entry &fe = lh->file_names[file_index];
20378 const char *include_name = fe.name;
20379 const char *include_name_to_compare = include_name;
20380 const char *pst_filename;
20381 int file_is_pst;
20382
20383 const char *dir_name = fe.include_dir (lh);
20384
20385 gdb::unique_xmalloc_ptr<char> hold_compare;
20386 if (!IS_ABSOLUTE_PATH (include_name)
20387 && (dir_name != NULL || comp_dir != NULL))
20388 {
20389 /* Avoid creating a duplicate psymtab for PST.
20390 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20391 Before we do the comparison, however, we need to account
20392 for DIR_NAME and COMP_DIR.
20393 First prepend dir_name (if non-NULL). If we still don't
20394 have an absolute path prepend comp_dir (if non-NULL).
20395 However, the directory we record in the include-file's
20396 psymtab does not contain COMP_DIR (to match the
20397 corresponding symtab(s)).
20398
20399 Example:
20400
20401 bash$ cd /tmp
20402 bash$ gcc -g ./hello.c
20403 include_name = "hello.c"
20404 dir_name = "."
20405 DW_AT_comp_dir = comp_dir = "/tmp"
20406 DW_AT_name = "./hello.c"
20407
20408 */
20409
20410 if (dir_name != NULL)
20411 {
20412 name_holder->reset (concat (dir_name, SLASH_STRING,
20413 include_name, (char *) NULL));
20414 include_name = name_holder->get ();
20415 include_name_to_compare = include_name;
20416 }
20417 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20418 {
20419 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20420 include_name, (char *) NULL));
20421 include_name_to_compare = hold_compare.get ();
20422 }
20423 }
20424
20425 pst_filename = pst->filename;
20426 gdb::unique_xmalloc_ptr<char> copied_name;
20427 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20428 {
20429 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20430 pst_filename, (char *) NULL));
20431 pst_filename = copied_name.get ();
20432 }
20433
20434 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20435
20436 if (file_is_pst)
20437 return NULL;
20438 return include_name;
20439 }
20440
20441 /* State machine to track the state of the line number program. */
20442
20443 class lnp_state_machine
20444 {
20445 public:
20446 /* Initialize a machine state for the start of a line number
20447 program. */
20448 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20449
20450 file_entry *current_file ()
20451 {
20452 /* lh->file_names is 0-based, but the file name numbers in the
20453 statement program are 1-based. */
20454 return m_line_header->file_name_at (m_file);
20455 }
20456
20457 /* Record the line in the state machine. END_SEQUENCE is true if
20458 we're processing the end of a sequence. */
20459 void record_line (bool end_sequence);
20460
20461 /* Check address and if invalid nop-out the rest of the lines in this
20462 sequence. */
20463 void check_line_address (struct dwarf2_cu *cu,
20464 const gdb_byte *line_ptr,
20465 CORE_ADDR lowpc, CORE_ADDR address);
20466
20467 void handle_set_discriminator (unsigned int discriminator)
20468 {
20469 m_discriminator = discriminator;
20470 m_line_has_non_zero_discriminator |= discriminator != 0;
20471 }
20472
20473 /* Handle DW_LNE_set_address. */
20474 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20475 {
20476 m_op_index = 0;
20477 address += baseaddr;
20478 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20479 }
20480
20481 /* Handle DW_LNS_advance_pc. */
20482 void handle_advance_pc (CORE_ADDR adjust);
20483
20484 /* Handle a special opcode. */
20485 void handle_special_opcode (unsigned char op_code);
20486
20487 /* Handle DW_LNS_advance_line. */
20488 void handle_advance_line (int line_delta)
20489 {
20490 advance_line (line_delta);
20491 }
20492
20493 /* Handle DW_LNS_set_file. */
20494 void handle_set_file (file_name_index file);
20495
20496 /* Handle DW_LNS_negate_stmt. */
20497 void handle_negate_stmt ()
20498 {
20499 m_is_stmt = !m_is_stmt;
20500 }
20501
20502 /* Handle DW_LNS_const_add_pc. */
20503 void handle_const_add_pc ();
20504
20505 /* Handle DW_LNS_fixed_advance_pc. */
20506 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20507 {
20508 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20509 m_op_index = 0;
20510 }
20511
20512 /* Handle DW_LNS_copy. */
20513 void handle_copy ()
20514 {
20515 record_line (false);
20516 m_discriminator = 0;
20517 }
20518
20519 /* Handle DW_LNE_end_sequence. */
20520 void handle_end_sequence ()
20521 {
20522 m_record_line_callback = ::record_line;
20523 }
20524
20525 private:
20526 /* Advance the line by LINE_DELTA. */
20527 void advance_line (int line_delta)
20528 {
20529 m_line += line_delta;
20530
20531 if (line_delta != 0)
20532 m_line_has_non_zero_discriminator = m_discriminator != 0;
20533 }
20534
20535 gdbarch *m_gdbarch;
20536
20537 /* True if we're recording lines.
20538 Otherwise we're building partial symtabs and are just interested in
20539 finding include files mentioned by the line number program. */
20540 bool m_record_lines_p;
20541
20542 /* The line number header. */
20543 line_header *m_line_header;
20544
20545 /* These are part of the standard DWARF line number state machine,
20546 and initialized according to the DWARF spec. */
20547
20548 unsigned char m_op_index = 0;
20549 /* The line table index (1-based) of the current file. */
20550 file_name_index m_file = (file_name_index) 1;
20551 unsigned int m_line = 1;
20552
20553 /* These are initialized in the constructor. */
20554
20555 CORE_ADDR m_address;
20556 bool m_is_stmt;
20557 unsigned int m_discriminator;
20558
20559 /* Additional bits of state we need to track. */
20560
20561 /* The last file that we called dwarf2_start_subfile for.
20562 This is only used for TLLs. */
20563 unsigned int m_last_file = 0;
20564 /* The last file a line number was recorded for. */
20565 struct subfile *m_last_subfile = NULL;
20566
20567 /* The function to call to record a line. */
20568 record_line_ftype *m_record_line_callback = NULL;
20569
20570 /* The last line number that was recorded, used to coalesce
20571 consecutive entries for the same line. This can happen, for
20572 example, when discriminators are present. PR 17276. */
20573 unsigned int m_last_line = 0;
20574 bool m_line_has_non_zero_discriminator = false;
20575 };
20576
20577 void
20578 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20579 {
20580 CORE_ADDR addr_adj = (((m_op_index + adjust)
20581 / m_line_header->maximum_ops_per_instruction)
20582 * m_line_header->minimum_instruction_length);
20583 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20584 m_op_index = ((m_op_index + adjust)
20585 % m_line_header->maximum_ops_per_instruction);
20586 }
20587
20588 void
20589 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20590 {
20591 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20592 CORE_ADDR addr_adj = (((m_op_index
20593 + (adj_opcode / m_line_header->line_range))
20594 / m_line_header->maximum_ops_per_instruction)
20595 * m_line_header->minimum_instruction_length);
20596 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20597 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20598 % m_line_header->maximum_ops_per_instruction);
20599
20600 int line_delta = (m_line_header->line_base
20601 + (adj_opcode % m_line_header->line_range));
20602 advance_line (line_delta);
20603 record_line (false);
20604 m_discriminator = 0;
20605 }
20606
20607 void
20608 lnp_state_machine::handle_set_file (file_name_index file)
20609 {
20610 m_file = file;
20611
20612 const file_entry *fe = current_file ();
20613 if (fe == NULL)
20614 dwarf2_debug_line_missing_file_complaint ();
20615 else if (m_record_lines_p)
20616 {
20617 const char *dir = fe->include_dir (m_line_header);
20618
20619 m_last_subfile = current_subfile;
20620 m_line_has_non_zero_discriminator = m_discriminator != 0;
20621 dwarf2_start_subfile (fe->name, dir);
20622 }
20623 }
20624
20625 void
20626 lnp_state_machine::handle_const_add_pc ()
20627 {
20628 CORE_ADDR adjust
20629 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20630
20631 CORE_ADDR addr_adj
20632 = (((m_op_index + adjust)
20633 / m_line_header->maximum_ops_per_instruction)
20634 * m_line_header->minimum_instruction_length);
20635
20636 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20637 m_op_index = ((m_op_index + adjust)
20638 % m_line_header->maximum_ops_per_instruction);
20639 }
20640
20641 /* Ignore this record_line request. */
20642
20643 static void
20644 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20645 {
20646 return;
20647 }
20648
20649 /* Return non-zero if we should add LINE to the line number table.
20650 LINE is the line to add, LAST_LINE is the last line that was added,
20651 LAST_SUBFILE is the subfile for LAST_LINE.
20652 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20653 had a non-zero discriminator.
20654
20655 We have to be careful in the presence of discriminators.
20656 E.g., for this line:
20657
20658 for (i = 0; i < 100000; i++);
20659
20660 clang can emit four line number entries for that one line,
20661 each with a different discriminator.
20662 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20663
20664 However, we want gdb to coalesce all four entries into one.
20665 Otherwise the user could stepi into the middle of the line and
20666 gdb would get confused about whether the pc really was in the
20667 middle of the line.
20668
20669 Things are further complicated by the fact that two consecutive
20670 line number entries for the same line is a heuristic used by gcc
20671 to denote the end of the prologue. So we can't just discard duplicate
20672 entries, we have to be selective about it. The heuristic we use is
20673 that we only collapse consecutive entries for the same line if at least
20674 one of those entries has a non-zero discriminator. PR 17276.
20675
20676 Note: Addresses in the line number state machine can never go backwards
20677 within one sequence, thus this coalescing is ok. */
20678
20679 static int
20680 dwarf_record_line_p (unsigned int line, unsigned int last_line,
20681 int line_has_non_zero_discriminator,
20682 struct subfile *last_subfile)
20683 {
20684 if (current_subfile != last_subfile)
20685 return 1;
20686 if (line != last_line)
20687 return 1;
20688 /* Same line for the same file that we've seen already.
20689 As a last check, for pr 17276, only record the line if the line
20690 has never had a non-zero discriminator. */
20691 if (!line_has_non_zero_discriminator)
20692 return 1;
20693 return 0;
20694 }
20695
20696 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20697 in the line table of subfile SUBFILE. */
20698
20699 static void
20700 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20701 unsigned int line, CORE_ADDR address,
20702 record_line_ftype p_record_line)
20703 {
20704 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20705
20706 if (dwarf_line_debug)
20707 {
20708 fprintf_unfiltered (gdb_stdlog,
20709 "Recording line %u, file %s, address %s\n",
20710 line, lbasename (subfile->name),
20711 paddress (gdbarch, address));
20712 }
20713
20714 (*p_record_line) (subfile, line, addr);
20715 }
20716
20717 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20718 Mark the end of a set of line number records.
20719 The arguments are the same as for dwarf_record_line_1.
20720 If SUBFILE is NULL the request is ignored. */
20721
20722 static void
20723 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20724 CORE_ADDR address, record_line_ftype p_record_line)
20725 {
20726 if (subfile == NULL)
20727 return;
20728
20729 if (dwarf_line_debug)
20730 {
20731 fprintf_unfiltered (gdb_stdlog,
20732 "Finishing current line, file %s, address %s\n",
20733 lbasename (subfile->name),
20734 paddress (gdbarch, address));
20735 }
20736
20737 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20738 }
20739
20740 void
20741 lnp_state_machine::record_line (bool end_sequence)
20742 {
20743 if (dwarf_line_debug)
20744 {
20745 fprintf_unfiltered (gdb_stdlog,
20746 "Processing actual line %u: file %u,"
20747 " address %s, is_stmt %u, discrim %u\n",
20748 m_line, to_underlying (m_file),
20749 paddress (m_gdbarch, m_address),
20750 m_is_stmt, m_discriminator);
20751 }
20752
20753 file_entry *fe = current_file ();
20754
20755 if (fe == NULL)
20756 dwarf2_debug_line_missing_file_complaint ();
20757 /* For now we ignore lines not starting on an instruction boundary.
20758 But not when processing end_sequence for compatibility with the
20759 previous version of the code. */
20760 else if (m_op_index == 0 || end_sequence)
20761 {
20762 fe->included_p = 1;
20763 if (m_record_lines_p && m_is_stmt)
20764 {
20765 if (m_last_subfile != current_subfile || end_sequence)
20766 {
20767 dwarf_finish_line (m_gdbarch, m_last_subfile,
20768 m_address, m_record_line_callback);
20769 }
20770
20771 if (!end_sequence)
20772 {
20773 if (dwarf_record_line_p (m_line, m_last_line,
20774 m_line_has_non_zero_discriminator,
20775 m_last_subfile))
20776 {
20777 dwarf_record_line_1 (m_gdbarch, current_subfile,
20778 m_line, m_address,
20779 m_record_line_callback);
20780 }
20781 m_last_subfile = current_subfile;
20782 m_last_line = m_line;
20783 }
20784 }
20785 }
20786 }
20787
20788 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20789 bool record_lines_p)
20790 {
20791 m_gdbarch = arch;
20792 m_record_lines_p = record_lines_p;
20793 m_line_header = lh;
20794
20795 m_record_line_callback = ::record_line;
20796
20797 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20798 was a line entry for it so that the backend has a chance to adjust it
20799 and also record it in case it needs it. This is currently used by MIPS
20800 code, cf. `mips_adjust_dwarf2_line'. */
20801 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20802 m_is_stmt = lh->default_is_stmt;
20803 m_discriminator = 0;
20804 }
20805
20806 void
20807 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20808 const gdb_byte *line_ptr,
20809 CORE_ADDR lowpc, CORE_ADDR address)
20810 {
20811 /* If address < lowpc then it's not a usable value, it's outside the
20812 pc range of the CU. However, we restrict the test to only address
20813 values of zero to preserve GDB's previous behaviour which is to
20814 handle the specific case of a function being GC'd by the linker. */
20815
20816 if (address == 0 && address < lowpc)
20817 {
20818 /* This line table is for a function which has been
20819 GCd by the linker. Ignore it. PR gdb/12528 */
20820
20821 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20822 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20823
20824 complaint (&symfile_complaints,
20825 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20826 line_offset, objfile_name (objfile));
20827 m_record_line_callback = noop_record_line;
20828 /* Note: record_line_callback is left as noop_record_line until
20829 we see DW_LNE_end_sequence. */
20830 }
20831 }
20832
20833 /* Subroutine of dwarf_decode_lines to simplify it.
20834 Process the line number information in LH.
20835 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20836 program in order to set included_p for every referenced header. */
20837
20838 static void
20839 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20840 const int decode_for_pst_p, CORE_ADDR lowpc)
20841 {
20842 const gdb_byte *line_ptr, *extended_end;
20843 const gdb_byte *line_end;
20844 unsigned int bytes_read, extended_len;
20845 unsigned char op_code, extended_op;
20846 CORE_ADDR baseaddr;
20847 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20848 bfd *abfd = objfile->obfd;
20849 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20850 /* True if we're recording line info (as opposed to building partial
20851 symtabs and just interested in finding include files mentioned by
20852 the line number program). */
20853 bool record_lines_p = !decode_for_pst_p;
20854
20855 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20856
20857 line_ptr = lh->statement_program_start;
20858 line_end = lh->statement_program_end;
20859
20860 /* Read the statement sequences until there's nothing left. */
20861 while (line_ptr < line_end)
20862 {
20863 /* The DWARF line number program state machine. Reset the state
20864 machine at the start of each sequence. */
20865 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20866 bool end_sequence = false;
20867
20868 if (record_lines_p)
20869 {
20870 /* Start a subfile for the current file of the state
20871 machine. */
20872 const file_entry *fe = state_machine.current_file ();
20873
20874 if (fe != NULL)
20875 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
20876 }
20877
20878 /* Decode the table. */
20879 while (line_ptr < line_end && !end_sequence)
20880 {
20881 op_code = read_1_byte (abfd, line_ptr);
20882 line_ptr += 1;
20883
20884 if (op_code >= lh->opcode_base)
20885 {
20886 /* Special opcode. */
20887 state_machine.handle_special_opcode (op_code);
20888 }
20889 else switch (op_code)
20890 {
20891 case DW_LNS_extended_op:
20892 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20893 &bytes_read);
20894 line_ptr += bytes_read;
20895 extended_end = line_ptr + extended_len;
20896 extended_op = read_1_byte (abfd, line_ptr);
20897 line_ptr += 1;
20898 switch (extended_op)
20899 {
20900 case DW_LNE_end_sequence:
20901 state_machine.handle_end_sequence ();
20902 end_sequence = true;
20903 break;
20904 case DW_LNE_set_address:
20905 {
20906 CORE_ADDR address
20907 = read_address (abfd, line_ptr, cu, &bytes_read);
20908 line_ptr += bytes_read;
20909
20910 state_machine.check_line_address (cu, line_ptr,
20911 lowpc, address);
20912 state_machine.handle_set_address (baseaddr, address);
20913 }
20914 break;
20915 case DW_LNE_define_file:
20916 {
20917 const char *cur_file;
20918 unsigned int mod_time, length;
20919 dir_index dindex;
20920
20921 cur_file = read_direct_string (abfd, line_ptr,
20922 &bytes_read);
20923 line_ptr += bytes_read;
20924 dindex = (dir_index)
20925 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20926 line_ptr += bytes_read;
20927 mod_time =
20928 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20929 line_ptr += bytes_read;
20930 length =
20931 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20932 line_ptr += bytes_read;
20933 lh->add_file_name (cur_file, dindex, mod_time, length);
20934 }
20935 break;
20936 case DW_LNE_set_discriminator:
20937 {
20938 /* The discriminator is not interesting to the
20939 debugger; just ignore it. We still need to
20940 check its value though:
20941 if there are consecutive entries for the same
20942 (non-prologue) line we want to coalesce them.
20943 PR 17276. */
20944 unsigned int discr
20945 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20946 line_ptr += bytes_read;
20947
20948 state_machine.handle_set_discriminator (discr);
20949 }
20950 break;
20951 default:
20952 complaint (&symfile_complaints,
20953 _("mangled .debug_line section"));
20954 return;
20955 }
20956 /* Make sure that we parsed the extended op correctly. If e.g.
20957 we expected a different address size than the producer used,
20958 we may have read the wrong number of bytes. */
20959 if (line_ptr != extended_end)
20960 {
20961 complaint (&symfile_complaints,
20962 _("mangled .debug_line section"));
20963 return;
20964 }
20965 break;
20966 case DW_LNS_copy:
20967 state_machine.handle_copy ();
20968 break;
20969 case DW_LNS_advance_pc:
20970 {
20971 CORE_ADDR adjust
20972 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20973 line_ptr += bytes_read;
20974
20975 state_machine.handle_advance_pc (adjust);
20976 }
20977 break;
20978 case DW_LNS_advance_line:
20979 {
20980 int line_delta
20981 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20982 line_ptr += bytes_read;
20983
20984 state_machine.handle_advance_line (line_delta);
20985 }
20986 break;
20987 case DW_LNS_set_file:
20988 {
20989 file_name_index file
20990 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20991 &bytes_read);
20992 line_ptr += bytes_read;
20993
20994 state_machine.handle_set_file (file);
20995 }
20996 break;
20997 case DW_LNS_set_column:
20998 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20999 line_ptr += bytes_read;
21000 break;
21001 case DW_LNS_negate_stmt:
21002 state_machine.handle_negate_stmt ();
21003 break;
21004 case DW_LNS_set_basic_block:
21005 break;
21006 /* Add to the address register of the state machine the
21007 address increment value corresponding to special opcode
21008 255. I.e., this value is scaled by the minimum
21009 instruction length since special opcode 255 would have
21010 scaled the increment. */
21011 case DW_LNS_const_add_pc:
21012 state_machine.handle_const_add_pc ();
21013 break;
21014 case DW_LNS_fixed_advance_pc:
21015 {
21016 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21017 line_ptr += 2;
21018
21019 state_machine.handle_fixed_advance_pc (addr_adj);
21020 }
21021 break;
21022 default:
21023 {
21024 /* Unknown standard opcode, ignore it. */
21025 int i;
21026
21027 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21028 {
21029 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21030 line_ptr += bytes_read;
21031 }
21032 }
21033 }
21034 }
21035
21036 if (!end_sequence)
21037 dwarf2_debug_line_missing_end_sequence_complaint ();
21038
21039 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21040 in which case we still finish recording the last line). */
21041 state_machine.record_line (true);
21042 }
21043 }
21044
21045 /* Decode the Line Number Program (LNP) for the given line_header
21046 structure and CU. The actual information extracted and the type
21047 of structures created from the LNP depends on the value of PST.
21048
21049 1. If PST is NULL, then this procedure uses the data from the program
21050 to create all necessary symbol tables, and their linetables.
21051
21052 2. If PST is not NULL, this procedure reads the program to determine
21053 the list of files included by the unit represented by PST, and
21054 builds all the associated partial symbol tables.
21055
21056 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21057 It is used for relative paths in the line table.
21058 NOTE: When processing partial symtabs (pst != NULL),
21059 comp_dir == pst->dirname.
21060
21061 NOTE: It is important that psymtabs have the same file name (via strcmp)
21062 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21063 symtab we don't use it in the name of the psymtabs we create.
21064 E.g. expand_line_sal requires this when finding psymtabs to expand.
21065 A good testcase for this is mb-inline.exp.
21066
21067 LOWPC is the lowest address in CU (or 0 if not known).
21068
21069 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21070 for its PC<->lines mapping information. Otherwise only the filename
21071 table is read in. */
21072
21073 static void
21074 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21075 struct dwarf2_cu *cu, struct partial_symtab *pst,
21076 CORE_ADDR lowpc, int decode_mapping)
21077 {
21078 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21079 const int decode_for_pst_p = (pst != NULL);
21080
21081 if (decode_mapping)
21082 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21083
21084 if (decode_for_pst_p)
21085 {
21086 int file_index;
21087
21088 /* Now that we're done scanning the Line Header Program, we can
21089 create the psymtab of each included file. */
21090 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21091 if (lh->file_names[file_index].included_p == 1)
21092 {
21093 gdb::unique_xmalloc_ptr<char> name_holder;
21094 const char *include_name =
21095 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21096 &name_holder);
21097 if (include_name != NULL)
21098 dwarf2_create_include_psymtab (include_name, pst, objfile);
21099 }
21100 }
21101 else
21102 {
21103 /* Make sure a symtab is created for every file, even files
21104 which contain only variables (i.e. no code with associated
21105 line numbers). */
21106 struct compunit_symtab *cust = buildsym_compunit_symtab ();
21107 int i;
21108
21109 for (i = 0; i < lh->file_names.size (); i++)
21110 {
21111 file_entry &fe = lh->file_names[i];
21112
21113 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
21114
21115 if (current_subfile->symtab == NULL)
21116 {
21117 current_subfile->symtab
21118 = allocate_symtab (cust, current_subfile->name);
21119 }
21120 fe.symtab = current_subfile->symtab;
21121 }
21122 }
21123 }
21124
21125 /* Start a subfile for DWARF. FILENAME is the name of the file and
21126 DIRNAME the name of the source directory which contains FILENAME
21127 or NULL if not known.
21128 This routine tries to keep line numbers from identical absolute and
21129 relative file names in a common subfile.
21130
21131 Using the `list' example from the GDB testsuite, which resides in
21132 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21133 of /srcdir/list0.c yields the following debugging information for list0.c:
21134
21135 DW_AT_name: /srcdir/list0.c
21136 DW_AT_comp_dir: /compdir
21137 files.files[0].name: list0.h
21138 files.files[0].dir: /srcdir
21139 files.files[1].name: list0.c
21140 files.files[1].dir: /srcdir
21141
21142 The line number information for list0.c has to end up in a single
21143 subfile, so that `break /srcdir/list0.c:1' works as expected.
21144 start_subfile will ensure that this happens provided that we pass the
21145 concatenation of files.files[1].dir and files.files[1].name as the
21146 subfile's name. */
21147
21148 static void
21149 dwarf2_start_subfile (const char *filename, const char *dirname)
21150 {
21151 char *copy = NULL;
21152
21153 /* In order not to lose the line information directory,
21154 we concatenate it to the filename when it makes sense.
21155 Note that the Dwarf3 standard says (speaking of filenames in line
21156 information): ``The directory index is ignored for file names
21157 that represent full path names''. Thus ignoring dirname in the
21158 `else' branch below isn't an issue. */
21159
21160 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21161 {
21162 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21163 filename = copy;
21164 }
21165
21166 start_subfile (filename);
21167
21168 if (copy != NULL)
21169 xfree (copy);
21170 }
21171
21172 /* Start a symtab for DWARF.
21173 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21174
21175 static struct compunit_symtab *
21176 dwarf2_start_symtab (struct dwarf2_cu *cu,
21177 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21178 {
21179 struct compunit_symtab *cust
21180 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21181 low_pc, cu->language);
21182
21183 record_debugformat ("DWARF 2");
21184 record_producer (cu->producer);
21185
21186 /* We assume that we're processing GCC output. */
21187 processing_gcc_compilation = 2;
21188
21189 cu->processing_has_namespace_info = 0;
21190
21191 return cust;
21192 }
21193
21194 static void
21195 var_decode_location (struct attribute *attr, struct symbol *sym,
21196 struct dwarf2_cu *cu)
21197 {
21198 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21199 struct comp_unit_head *cu_header = &cu->header;
21200
21201 /* NOTE drow/2003-01-30: There used to be a comment and some special
21202 code here to turn a symbol with DW_AT_external and a
21203 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21204 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21205 with some versions of binutils) where shared libraries could have
21206 relocations against symbols in their debug information - the
21207 minimal symbol would have the right address, but the debug info
21208 would not. It's no longer necessary, because we will explicitly
21209 apply relocations when we read in the debug information now. */
21210
21211 /* A DW_AT_location attribute with no contents indicates that a
21212 variable has been optimized away. */
21213 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21214 {
21215 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21216 return;
21217 }
21218
21219 /* Handle one degenerate form of location expression specially, to
21220 preserve GDB's previous behavior when section offsets are
21221 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21222 then mark this symbol as LOC_STATIC. */
21223
21224 if (attr_form_is_block (attr)
21225 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21226 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21227 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21228 && (DW_BLOCK (attr)->size
21229 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21230 {
21231 unsigned int dummy;
21232
21233 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21234 SYMBOL_VALUE_ADDRESS (sym) =
21235 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21236 else
21237 SYMBOL_VALUE_ADDRESS (sym) =
21238 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21239 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21240 fixup_symbol_section (sym, objfile);
21241 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21242 SYMBOL_SECTION (sym));
21243 return;
21244 }
21245
21246 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21247 expression evaluator, and use LOC_COMPUTED only when necessary
21248 (i.e. when the value of a register or memory location is
21249 referenced, or a thread-local block, etc.). Then again, it might
21250 not be worthwhile. I'm assuming that it isn't unless performance
21251 or memory numbers show me otherwise. */
21252
21253 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21254
21255 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21256 cu->has_loclist = 1;
21257 }
21258
21259 /* Given a pointer to a DWARF information entry, figure out if we need
21260 to make a symbol table entry for it, and if so, create a new entry
21261 and return a pointer to it.
21262 If TYPE is NULL, determine symbol type from the die, otherwise
21263 used the passed type.
21264 If SPACE is not NULL, use it to hold the new symbol. If it is
21265 NULL, allocate a new symbol on the objfile's obstack. */
21266
21267 static struct symbol *
21268 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21269 struct symbol *space)
21270 {
21271 struct dwarf2_per_objfile *dwarf2_per_objfile
21272 = cu->per_cu->dwarf2_per_objfile;
21273 struct objfile *objfile = dwarf2_per_objfile->objfile;
21274 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21275 struct symbol *sym = NULL;
21276 const char *name;
21277 struct attribute *attr = NULL;
21278 struct attribute *attr2 = NULL;
21279 CORE_ADDR baseaddr;
21280 struct pending **list_to_add = NULL;
21281
21282 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21283
21284 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21285
21286 name = dwarf2_name (die, cu);
21287 if (name)
21288 {
21289 const char *linkagename;
21290 int suppress_add = 0;
21291
21292 if (space)
21293 sym = space;
21294 else
21295 sym = allocate_symbol (objfile);
21296 OBJSTAT (objfile, n_syms++);
21297
21298 /* Cache this symbol's name and the name's demangled form (if any). */
21299 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21300 linkagename = dwarf2_physname (name, die, cu);
21301 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21302
21303 /* Fortran does not have mangling standard and the mangling does differ
21304 between gfortran, iFort etc. */
21305 if (cu->language == language_fortran
21306 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21307 symbol_set_demangled_name (&(sym->ginfo),
21308 dwarf2_full_name (name, die, cu),
21309 NULL);
21310
21311 /* Default assumptions.
21312 Use the passed type or decode it from the die. */
21313 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21314 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21315 if (type != NULL)
21316 SYMBOL_TYPE (sym) = type;
21317 else
21318 SYMBOL_TYPE (sym) = die_type (die, cu);
21319 attr = dwarf2_attr (die,
21320 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21321 cu);
21322 if (attr)
21323 {
21324 SYMBOL_LINE (sym) = DW_UNSND (attr);
21325 }
21326
21327 attr = dwarf2_attr (die,
21328 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21329 cu);
21330 if (attr)
21331 {
21332 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21333 struct file_entry *fe;
21334
21335 if (cu->line_header != NULL)
21336 fe = cu->line_header->file_name_at (file_index);
21337 else
21338 fe = NULL;
21339
21340 if (fe == NULL)
21341 complaint (&symfile_complaints,
21342 _("file index out of range"));
21343 else
21344 symbol_set_symtab (sym, fe->symtab);
21345 }
21346
21347 switch (die->tag)
21348 {
21349 case DW_TAG_label:
21350 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21351 if (attr)
21352 {
21353 CORE_ADDR addr;
21354
21355 addr = attr_value_as_address (attr);
21356 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21357 SYMBOL_VALUE_ADDRESS (sym) = addr;
21358 }
21359 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21360 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21361 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21362 add_symbol_to_list (sym, cu->list_in_scope);
21363 break;
21364 case DW_TAG_subprogram:
21365 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21366 finish_block. */
21367 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21368 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21369 if ((attr2 && (DW_UNSND (attr2) != 0))
21370 || cu->language == language_ada)
21371 {
21372 /* Subprograms marked external are stored as a global symbol.
21373 Ada subprograms, whether marked external or not, are always
21374 stored as a global symbol, because we want to be able to
21375 access them globally. For instance, we want to be able
21376 to break on a nested subprogram without having to
21377 specify the context. */
21378 list_to_add = &global_symbols;
21379 }
21380 else
21381 {
21382 list_to_add = cu->list_in_scope;
21383 }
21384 break;
21385 case DW_TAG_inlined_subroutine:
21386 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21387 finish_block. */
21388 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21389 SYMBOL_INLINED (sym) = 1;
21390 list_to_add = cu->list_in_scope;
21391 break;
21392 case DW_TAG_template_value_param:
21393 suppress_add = 1;
21394 /* Fall through. */
21395 case DW_TAG_constant:
21396 case DW_TAG_variable:
21397 case DW_TAG_member:
21398 /* Compilation with minimal debug info may result in
21399 variables with missing type entries. Change the
21400 misleading `void' type to something sensible. */
21401 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21402 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21403
21404 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21405 /* In the case of DW_TAG_member, we should only be called for
21406 static const members. */
21407 if (die->tag == DW_TAG_member)
21408 {
21409 /* dwarf2_add_field uses die_is_declaration,
21410 so we do the same. */
21411 gdb_assert (die_is_declaration (die, cu));
21412 gdb_assert (attr);
21413 }
21414 if (attr)
21415 {
21416 dwarf2_const_value (attr, sym, cu);
21417 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21418 if (!suppress_add)
21419 {
21420 if (attr2 && (DW_UNSND (attr2) != 0))
21421 list_to_add = &global_symbols;
21422 else
21423 list_to_add = cu->list_in_scope;
21424 }
21425 break;
21426 }
21427 attr = dwarf2_attr (die, DW_AT_location, cu);
21428 if (attr)
21429 {
21430 var_decode_location (attr, sym, cu);
21431 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21432
21433 /* Fortran explicitly imports any global symbols to the local
21434 scope by DW_TAG_common_block. */
21435 if (cu->language == language_fortran && die->parent
21436 && die->parent->tag == DW_TAG_common_block)
21437 attr2 = NULL;
21438
21439 if (SYMBOL_CLASS (sym) == LOC_STATIC
21440 && SYMBOL_VALUE_ADDRESS (sym) == 0
21441 && !dwarf2_per_objfile->has_section_at_zero)
21442 {
21443 /* When a static variable is eliminated by the linker,
21444 the corresponding debug information is not stripped
21445 out, but the variable address is set to null;
21446 do not add such variables into symbol table. */
21447 }
21448 else if (attr2 && (DW_UNSND (attr2) != 0))
21449 {
21450 /* Workaround gfortran PR debug/40040 - it uses
21451 DW_AT_location for variables in -fPIC libraries which may
21452 get overriden by other libraries/executable and get
21453 a different address. Resolve it by the minimal symbol
21454 which may come from inferior's executable using copy
21455 relocation. Make this workaround only for gfortran as for
21456 other compilers GDB cannot guess the minimal symbol
21457 Fortran mangling kind. */
21458 if (cu->language == language_fortran && die->parent
21459 && die->parent->tag == DW_TAG_module
21460 && cu->producer
21461 && startswith (cu->producer, "GNU Fortran"))
21462 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21463
21464 /* A variable with DW_AT_external is never static,
21465 but it may be block-scoped. */
21466 list_to_add = (cu->list_in_scope == &file_symbols
21467 ? &global_symbols : cu->list_in_scope);
21468 }
21469 else
21470 list_to_add = cu->list_in_scope;
21471 }
21472 else
21473 {
21474 /* We do not know the address of this symbol.
21475 If it is an external symbol and we have type information
21476 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21477 The address of the variable will then be determined from
21478 the minimal symbol table whenever the variable is
21479 referenced. */
21480 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21481
21482 /* Fortran explicitly imports any global symbols to the local
21483 scope by DW_TAG_common_block. */
21484 if (cu->language == language_fortran && die->parent
21485 && die->parent->tag == DW_TAG_common_block)
21486 {
21487 /* SYMBOL_CLASS doesn't matter here because
21488 read_common_block is going to reset it. */
21489 if (!suppress_add)
21490 list_to_add = cu->list_in_scope;
21491 }
21492 else if (attr2 && (DW_UNSND (attr2) != 0)
21493 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21494 {
21495 /* A variable with DW_AT_external is never static, but it
21496 may be block-scoped. */
21497 list_to_add = (cu->list_in_scope == &file_symbols
21498 ? &global_symbols : cu->list_in_scope);
21499
21500 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21501 }
21502 else if (!die_is_declaration (die, cu))
21503 {
21504 /* Use the default LOC_OPTIMIZED_OUT class. */
21505 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21506 if (!suppress_add)
21507 list_to_add = cu->list_in_scope;
21508 }
21509 }
21510 break;
21511 case DW_TAG_formal_parameter:
21512 /* If we are inside a function, mark this as an argument. If
21513 not, we might be looking at an argument to an inlined function
21514 when we do not have enough information to show inlined frames;
21515 pretend it's a local variable in that case so that the user can
21516 still see it. */
21517 if (context_stack_depth > 0
21518 && context_stack[context_stack_depth - 1].name != NULL)
21519 SYMBOL_IS_ARGUMENT (sym) = 1;
21520 attr = dwarf2_attr (die, DW_AT_location, cu);
21521 if (attr)
21522 {
21523 var_decode_location (attr, sym, cu);
21524 }
21525 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21526 if (attr)
21527 {
21528 dwarf2_const_value (attr, sym, cu);
21529 }
21530
21531 list_to_add = cu->list_in_scope;
21532 break;
21533 case DW_TAG_unspecified_parameters:
21534 /* From varargs functions; gdb doesn't seem to have any
21535 interest in this information, so just ignore it for now.
21536 (FIXME?) */
21537 break;
21538 case DW_TAG_template_type_param:
21539 suppress_add = 1;
21540 /* Fall through. */
21541 case DW_TAG_class_type:
21542 case DW_TAG_interface_type:
21543 case DW_TAG_structure_type:
21544 case DW_TAG_union_type:
21545 case DW_TAG_set_type:
21546 case DW_TAG_enumeration_type:
21547 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21548 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21549
21550 {
21551 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21552 really ever be static objects: otherwise, if you try
21553 to, say, break of a class's method and you're in a file
21554 which doesn't mention that class, it won't work unless
21555 the check for all static symbols in lookup_symbol_aux
21556 saves you. See the OtherFileClass tests in
21557 gdb.c++/namespace.exp. */
21558
21559 if (!suppress_add)
21560 {
21561 list_to_add = (cu->list_in_scope == &file_symbols
21562 && cu->language == language_cplus
21563 ? &global_symbols : cu->list_in_scope);
21564
21565 /* The semantics of C++ state that "struct foo {
21566 ... }" also defines a typedef for "foo". */
21567 if (cu->language == language_cplus
21568 || cu->language == language_ada
21569 || cu->language == language_d
21570 || cu->language == language_rust)
21571 {
21572 /* The symbol's name is already allocated along
21573 with this objfile, so we don't need to
21574 duplicate it for the type. */
21575 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21576 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21577 }
21578 }
21579 }
21580 break;
21581 case DW_TAG_typedef:
21582 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21583 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21584 list_to_add = cu->list_in_scope;
21585 break;
21586 case DW_TAG_base_type:
21587 case DW_TAG_subrange_type:
21588 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21589 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21590 list_to_add = cu->list_in_scope;
21591 break;
21592 case DW_TAG_enumerator:
21593 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21594 if (attr)
21595 {
21596 dwarf2_const_value (attr, sym, cu);
21597 }
21598 {
21599 /* NOTE: carlton/2003-11-10: See comment above in the
21600 DW_TAG_class_type, etc. block. */
21601
21602 list_to_add = (cu->list_in_scope == &file_symbols
21603 && cu->language == language_cplus
21604 ? &global_symbols : cu->list_in_scope);
21605 }
21606 break;
21607 case DW_TAG_imported_declaration:
21608 case DW_TAG_namespace:
21609 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21610 list_to_add = &global_symbols;
21611 break;
21612 case DW_TAG_module:
21613 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21614 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21615 list_to_add = &global_symbols;
21616 break;
21617 case DW_TAG_common_block:
21618 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21619 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21620 add_symbol_to_list (sym, cu->list_in_scope);
21621 break;
21622 default:
21623 /* Not a tag we recognize. Hopefully we aren't processing
21624 trash data, but since we must specifically ignore things
21625 we don't recognize, there is nothing else we should do at
21626 this point. */
21627 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
21628 dwarf_tag_name (die->tag));
21629 break;
21630 }
21631
21632 if (suppress_add)
21633 {
21634 sym->hash_next = objfile->template_symbols;
21635 objfile->template_symbols = sym;
21636 list_to_add = NULL;
21637 }
21638
21639 if (list_to_add != NULL)
21640 add_symbol_to_list (sym, list_to_add);
21641
21642 /* For the benefit of old versions of GCC, check for anonymous
21643 namespaces based on the demangled name. */
21644 if (!cu->processing_has_namespace_info
21645 && cu->language == language_cplus)
21646 cp_scan_for_anonymous_namespaces (sym, objfile);
21647 }
21648 return (sym);
21649 }
21650
21651 /* Given an attr with a DW_FORM_dataN value in host byte order,
21652 zero-extend it as appropriate for the symbol's type. The DWARF
21653 standard (v4) is not entirely clear about the meaning of using
21654 DW_FORM_dataN for a constant with a signed type, where the type is
21655 wider than the data. The conclusion of a discussion on the DWARF
21656 list was that this is unspecified. We choose to always zero-extend
21657 because that is the interpretation long in use by GCC. */
21658
21659 static gdb_byte *
21660 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21661 struct dwarf2_cu *cu, LONGEST *value, int bits)
21662 {
21663 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21664 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21665 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21666 LONGEST l = DW_UNSND (attr);
21667
21668 if (bits < sizeof (*value) * 8)
21669 {
21670 l &= ((LONGEST) 1 << bits) - 1;
21671 *value = l;
21672 }
21673 else if (bits == sizeof (*value) * 8)
21674 *value = l;
21675 else
21676 {
21677 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21678 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21679 return bytes;
21680 }
21681
21682 return NULL;
21683 }
21684
21685 /* Read a constant value from an attribute. Either set *VALUE, or if
21686 the value does not fit in *VALUE, set *BYTES - either already
21687 allocated on the objfile obstack, or newly allocated on OBSTACK,
21688 or, set *BATON, if we translated the constant to a location
21689 expression. */
21690
21691 static void
21692 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21693 const char *name, struct obstack *obstack,
21694 struct dwarf2_cu *cu,
21695 LONGEST *value, const gdb_byte **bytes,
21696 struct dwarf2_locexpr_baton **baton)
21697 {
21698 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21699 struct comp_unit_head *cu_header = &cu->header;
21700 struct dwarf_block *blk;
21701 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21702 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21703
21704 *value = 0;
21705 *bytes = NULL;
21706 *baton = NULL;
21707
21708 switch (attr->form)
21709 {
21710 case DW_FORM_addr:
21711 case DW_FORM_GNU_addr_index:
21712 {
21713 gdb_byte *data;
21714
21715 if (TYPE_LENGTH (type) != cu_header->addr_size)
21716 dwarf2_const_value_length_mismatch_complaint (name,
21717 cu_header->addr_size,
21718 TYPE_LENGTH (type));
21719 /* Symbols of this form are reasonably rare, so we just
21720 piggyback on the existing location code rather than writing
21721 a new implementation of symbol_computed_ops. */
21722 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21723 (*baton)->per_cu = cu->per_cu;
21724 gdb_assert ((*baton)->per_cu);
21725
21726 (*baton)->size = 2 + cu_header->addr_size;
21727 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21728 (*baton)->data = data;
21729
21730 data[0] = DW_OP_addr;
21731 store_unsigned_integer (&data[1], cu_header->addr_size,
21732 byte_order, DW_ADDR (attr));
21733 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21734 }
21735 break;
21736 case DW_FORM_string:
21737 case DW_FORM_strp:
21738 case DW_FORM_GNU_str_index:
21739 case DW_FORM_GNU_strp_alt:
21740 /* DW_STRING is already allocated on the objfile obstack, point
21741 directly to it. */
21742 *bytes = (const gdb_byte *) DW_STRING (attr);
21743 break;
21744 case DW_FORM_block1:
21745 case DW_FORM_block2:
21746 case DW_FORM_block4:
21747 case DW_FORM_block:
21748 case DW_FORM_exprloc:
21749 case DW_FORM_data16:
21750 blk = DW_BLOCK (attr);
21751 if (TYPE_LENGTH (type) != blk->size)
21752 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21753 TYPE_LENGTH (type));
21754 *bytes = blk->data;
21755 break;
21756
21757 /* The DW_AT_const_value attributes are supposed to carry the
21758 symbol's value "represented as it would be on the target
21759 architecture." By the time we get here, it's already been
21760 converted to host endianness, so we just need to sign- or
21761 zero-extend it as appropriate. */
21762 case DW_FORM_data1:
21763 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21764 break;
21765 case DW_FORM_data2:
21766 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21767 break;
21768 case DW_FORM_data4:
21769 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21770 break;
21771 case DW_FORM_data8:
21772 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21773 break;
21774
21775 case DW_FORM_sdata:
21776 case DW_FORM_implicit_const:
21777 *value = DW_SND (attr);
21778 break;
21779
21780 case DW_FORM_udata:
21781 *value = DW_UNSND (attr);
21782 break;
21783
21784 default:
21785 complaint (&symfile_complaints,
21786 _("unsupported const value attribute form: '%s'"),
21787 dwarf_form_name (attr->form));
21788 *value = 0;
21789 break;
21790 }
21791 }
21792
21793
21794 /* Copy constant value from an attribute to a symbol. */
21795
21796 static void
21797 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21798 struct dwarf2_cu *cu)
21799 {
21800 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21801 LONGEST value;
21802 const gdb_byte *bytes;
21803 struct dwarf2_locexpr_baton *baton;
21804
21805 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21806 SYMBOL_PRINT_NAME (sym),
21807 &objfile->objfile_obstack, cu,
21808 &value, &bytes, &baton);
21809
21810 if (baton != NULL)
21811 {
21812 SYMBOL_LOCATION_BATON (sym) = baton;
21813 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21814 }
21815 else if (bytes != NULL)
21816 {
21817 SYMBOL_VALUE_BYTES (sym) = bytes;
21818 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21819 }
21820 else
21821 {
21822 SYMBOL_VALUE (sym) = value;
21823 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21824 }
21825 }
21826
21827 /* Return the type of the die in question using its DW_AT_type attribute. */
21828
21829 static struct type *
21830 die_type (struct die_info *die, struct dwarf2_cu *cu)
21831 {
21832 struct attribute *type_attr;
21833
21834 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21835 if (!type_attr)
21836 {
21837 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21838 /* A missing DW_AT_type represents a void type. */
21839 return objfile_type (objfile)->builtin_void;
21840 }
21841
21842 return lookup_die_type (die, type_attr, cu);
21843 }
21844
21845 /* True iff CU's producer generates GNAT Ada auxiliary information
21846 that allows to find parallel types through that information instead
21847 of having to do expensive parallel lookups by type name. */
21848
21849 static int
21850 need_gnat_info (struct dwarf2_cu *cu)
21851 {
21852 /* Assume that the Ada compiler was GNAT, which always produces
21853 the auxiliary information. */
21854 return (cu->language == language_ada);
21855 }
21856
21857 /* Return the auxiliary type of the die in question using its
21858 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21859 attribute is not present. */
21860
21861 static struct type *
21862 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21863 {
21864 struct attribute *type_attr;
21865
21866 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21867 if (!type_attr)
21868 return NULL;
21869
21870 return lookup_die_type (die, type_attr, cu);
21871 }
21872
21873 /* If DIE has a descriptive_type attribute, then set the TYPE's
21874 descriptive type accordingly. */
21875
21876 static void
21877 set_descriptive_type (struct type *type, struct die_info *die,
21878 struct dwarf2_cu *cu)
21879 {
21880 struct type *descriptive_type = die_descriptive_type (die, cu);
21881
21882 if (descriptive_type)
21883 {
21884 ALLOCATE_GNAT_AUX_TYPE (type);
21885 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21886 }
21887 }
21888
21889 /* Return the containing type of the die in question using its
21890 DW_AT_containing_type attribute. */
21891
21892 static struct type *
21893 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21894 {
21895 struct attribute *type_attr;
21896 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21897
21898 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21899 if (!type_attr)
21900 error (_("Dwarf Error: Problem turning containing type into gdb type "
21901 "[in module %s]"), objfile_name (objfile));
21902
21903 return lookup_die_type (die, type_attr, cu);
21904 }
21905
21906 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21907
21908 static struct type *
21909 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21910 {
21911 struct dwarf2_per_objfile *dwarf2_per_objfile
21912 = cu->per_cu->dwarf2_per_objfile;
21913 struct objfile *objfile = dwarf2_per_objfile->objfile;
21914 char *message, *saved;
21915
21916 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
21917 objfile_name (objfile),
21918 to_underlying (cu->header.sect_off),
21919 to_underlying (die->sect_off));
21920 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21921 message, strlen (message));
21922 xfree (message);
21923
21924 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21925 }
21926
21927 /* Look up the type of DIE in CU using its type attribute ATTR.
21928 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21929 DW_AT_containing_type.
21930 If there is no type substitute an error marker. */
21931
21932 static struct type *
21933 lookup_die_type (struct die_info *die, const struct attribute *attr,
21934 struct dwarf2_cu *cu)
21935 {
21936 struct dwarf2_per_objfile *dwarf2_per_objfile
21937 = cu->per_cu->dwarf2_per_objfile;
21938 struct objfile *objfile = dwarf2_per_objfile->objfile;
21939 struct type *this_type;
21940
21941 gdb_assert (attr->name == DW_AT_type
21942 || attr->name == DW_AT_GNAT_descriptive_type
21943 || attr->name == DW_AT_containing_type);
21944
21945 /* First see if we have it cached. */
21946
21947 if (attr->form == DW_FORM_GNU_ref_alt)
21948 {
21949 struct dwarf2_per_cu_data *per_cu;
21950 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21951
21952 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21953 dwarf2_per_objfile);
21954 this_type = get_die_type_at_offset (sect_off, per_cu);
21955 }
21956 else if (attr_form_is_ref (attr))
21957 {
21958 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21959
21960 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21961 }
21962 else if (attr->form == DW_FORM_ref_sig8)
21963 {
21964 ULONGEST signature = DW_SIGNATURE (attr);
21965
21966 return get_signatured_type (die, signature, cu);
21967 }
21968 else
21969 {
21970 complaint (&symfile_complaints,
21971 _("Dwarf Error: Bad type attribute %s in DIE"
21972 " at 0x%x [in module %s]"),
21973 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
21974 objfile_name (objfile));
21975 return build_error_marker_type (cu, die);
21976 }
21977
21978 /* If not cached we need to read it in. */
21979
21980 if (this_type == NULL)
21981 {
21982 struct die_info *type_die = NULL;
21983 struct dwarf2_cu *type_cu = cu;
21984
21985 if (attr_form_is_ref (attr))
21986 type_die = follow_die_ref (die, attr, &type_cu);
21987 if (type_die == NULL)
21988 return build_error_marker_type (cu, die);
21989 /* If we find the type now, it's probably because the type came
21990 from an inter-CU reference and the type's CU got expanded before
21991 ours. */
21992 this_type = read_type_die (type_die, type_cu);
21993 }
21994
21995 /* If we still don't have a type use an error marker. */
21996
21997 if (this_type == NULL)
21998 return build_error_marker_type (cu, die);
21999
22000 return this_type;
22001 }
22002
22003 /* Return the type in DIE, CU.
22004 Returns NULL for invalid types.
22005
22006 This first does a lookup in die_type_hash,
22007 and only reads the die in if necessary.
22008
22009 NOTE: This can be called when reading in partial or full symbols. */
22010
22011 static struct type *
22012 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22013 {
22014 struct type *this_type;
22015
22016 this_type = get_die_type (die, cu);
22017 if (this_type)
22018 return this_type;
22019
22020 return read_type_die_1 (die, cu);
22021 }
22022
22023 /* Read the type in DIE, CU.
22024 Returns NULL for invalid types. */
22025
22026 static struct type *
22027 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22028 {
22029 struct type *this_type = NULL;
22030
22031 switch (die->tag)
22032 {
22033 case DW_TAG_class_type:
22034 case DW_TAG_interface_type:
22035 case DW_TAG_structure_type:
22036 case DW_TAG_union_type:
22037 this_type = read_structure_type (die, cu);
22038 break;
22039 case DW_TAG_enumeration_type:
22040 this_type = read_enumeration_type (die, cu);
22041 break;
22042 case DW_TAG_subprogram:
22043 case DW_TAG_subroutine_type:
22044 case DW_TAG_inlined_subroutine:
22045 this_type = read_subroutine_type (die, cu);
22046 break;
22047 case DW_TAG_array_type:
22048 this_type = read_array_type (die, cu);
22049 break;
22050 case DW_TAG_set_type:
22051 this_type = read_set_type (die, cu);
22052 break;
22053 case DW_TAG_pointer_type:
22054 this_type = read_tag_pointer_type (die, cu);
22055 break;
22056 case DW_TAG_ptr_to_member_type:
22057 this_type = read_tag_ptr_to_member_type (die, cu);
22058 break;
22059 case DW_TAG_reference_type:
22060 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22061 break;
22062 case DW_TAG_rvalue_reference_type:
22063 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22064 break;
22065 case DW_TAG_const_type:
22066 this_type = read_tag_const_type (die, cu);
22067 break;
22068 case DW_TAG_volatile_type:
22069 this_type = read_tag_volatile_type (die, cu);
22070 break;
22071 case DW_TAG_restrict_type:
22072 this_type = read_tag_restrict_type (die, cu);
22073 break;
22074 case DW_TAG_string_type:
22075 this_type = read_tag_string_type (die, cu);
22076 break;
22077 case DW_TAG_typedef:
22078 this_type = read_typedef (die, cu);
22079 break;
22080 case DW_TAG_subrange_type:
22081 this_type = read_subrange_type (die, cu);
22082 break;
22083 case DW_TAG_base_type:
22084 this_type = read_base_type (die, cu);
22085 break;
22086 case DW_TAG_unspecified_type:
22087 this_type = read_unspecified_type (die, cu);
22088 break;
22089 case DW_TAG_namespace:
22090 this_type = read_namespace_type (die, cu);
22091 break;
22092 case DW_TAG_module:
22093 this_type = read_module_type (die, cu);
22094 break;
22095 case DW_TAG_atomic_type:
22096 this_type = read_tag_atomic_type (die, cu);
22097 break;
22098 default:
22099 complaint (&symfile_complaints,
22100 _("unexpected tag in read_type_die: '%s'"),
22101 dwarf_tag_name (die->tag));
22102 break;
22103 }
22104
22105 return this_type;
22106 }
22107
22108 /* See if we can figure out if the class lives in a namespace. We do
22109 this by looking for a member function; its demangled name will
22110 contain namespace info, if there is any.
22111 Return the computed name or NULL.
22112 Space for the result is allocated on the objfile's obstack.
22113 This is the full-die version of guess_partial_die_structure_name.
22114 In this case we know DIE has no useful parent. */
22115
22116 static char *
22117 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22118 {
22119 struct die_info *spec_die;
22120 struct dwarf2_cu *spec_cu;
22121 struct die_info *child;
22122 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22123
22124 spec_cu = cu;
22125 spec_die = die_specification (die, &spec_cu);
22126 if (spec_die != NULL)
22127 {
22128 die = spec_die;
22129 cu = spec_cu;
22130 }
22131
22132 for (child = die->child;
22133 child != NULL;
22134 child = child->sibling)
22135 {
22136 if (child->tag == DW_TAG_subprogram)
22137 {
22138 const char *linkage_name = dw2_linkage_name (child, cu);
22139
22140 if (linkage_name != NULL)
22141 {
22142 char *actual_name
22143 = language_class_name_from_physname (cu->language_defn,
22144 linkage_name);
22145 char *name = NULL;
22146
22147 if (actual_name != NULL)
22148 {
22149 const char *die_name = dwarf2_name (die, cu);
22150
22151 if (die_name != NULL
22152 && strcmp (die_name, actual_name) != 0)
22153 {
22154 /* Strip off the class name from the full name.
22155 We want the prefix. */
22156 int die_name_len = strlen (die_name);
22157 int actual_name_len = strlen (actual_name);
22158
22159 /* Test for '::' as a sanity check. */
22160 if (actual_name_len > die_name_len + 2
22161 && actual_name[actual_name_len
22162 - die_name_len - 1] == ':')
22163 name = (char *) obstack_copy0 (
22164 &objfile->per_bfd->storage_obstack,
22165 actual_name, actual_name_len - die_name_len - 2);
22166 }
22167 }
22168 xfree (actual_name);
22169 return name;
22170 }
22171 }
22172 }
22173
22174 return NULL;
22175 }
22176
22177 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22178 prefix part in such case. See
22179 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22180
22181 static const char *
22182 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22183 {
22184 struct attribute *attr;
22185 const char *base;
22186
22187 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22188 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22189 return NULL;
22190
22191 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22192 return NULL;
22193
22194 attr = dw2_linkage_name_attr (die, cu);
22195 if (attr == NULL || DW_STRING (attr) == NULL)
22196 return NULL;
22197
22198 /* dwarf2_name had to be already called. */
22199 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22200
22201 /* Strip the base name, keep any leading namespaces/classes. */
22202 base = strrchr (DW_STRING (attr), ':');
22203 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22204 return "";
22205
22206 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22207 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22208 DW_STRING (attr),
22209 &base[-1] - DW_STRING (attr));
22210 }
22211
22212 /* Return the name of the namespace/class that DIE is defined within,
22213 or "" if we can't tell. The caller should not xfree the result.
22214
22215 For example, if we're within the method foo() in the following
22216 code:
22217
22218 namespace N {
22219 class C {
22220 void foo () {
22221 }
22222 };
22223 }
22224
22225 then determine_prefix on foo's die will return "N::C". */
22226
22227 static const char *
22228 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22229 {
22230 struct dwarf2_per_objfile *dwarf2_per_objfile
22231 = cu->per_cu->dwarf2_per_objfile;
22232 struct die_info *parent, *spec_die;
22233 struct dwarf2_cu *spec_cu;
22234 struct type *parent_type;
22235 const char *retval;
22236
22237 if (cu->language != language_cplus
22238 && cu->language != language_fortran && cu->language != language_d
22239 && cu->language != language_rust)
22240 return "";
22241
22242 retval = anonymous_struct_prefix (die, cu);
22243 if (retval)
22244 return retval;
22245
22246 /* We have to be careful in the presence of DW_AT_specification.
22247 For example, with GCC 3.4, given the code
22248
22249 namespace N {
22250 void foo() {
22251 // Definition of N::foo.
22252 }
22253 }
22254
22255 then we'll have a tree of DIEs like this:
22256
22257 1: DW_TAG_compile_unit
22258 2: DW_TAG_namespace // N
22259 3: DW_TAG_subprogram // declaration of N::foo
22260 4: DW_TAG_subprogram // definition of N::foo
22261 DW_AT_specification // refers to die #3
22262
22263 Thus, when processing die #4, we have to pretend that we're in
22264 the context of its DW_AT_specification, namely the contex of die
22265 #3. */
22266 spec_cu = cu;
22267 spec_die = die_specification (die, &spec_cu);
22268 if (spec_die == NULL)
22269 parent = die->parent;
22270 else
22271 {
22272 parent = spec_die->parent;
22273 cu = spec_cu;
22274 }
22275
22276 if (parent == NULL)
22277 return "";
22278 else if (parent->building_fullname)
22279 {
22280 const char *name;
22281 const char *parent_name;
22282
22283 /* It has been seen on RealView 2.2 built binaries,
22284 DW_TAG_template_type_param types actually _defined_ as
22285 children of the parent class:
22286
22287 enum E {};
22288 template class <class Enum> Class{};
22289 Class<enum E> class_e;
22290
22291 1: DW_TAG_class_type (Class)
22292 2: DW_TAG_enumeration_type (E)
22293 3: DW_TAG_enumerator (enum1:0)
22294 3: DW_TAG_enumerator (enum2:1)
22295 ...
22296 2: DW_TAG_template_type_param
22297 DW_AT_type DW_FORM_ref_udata (E)
22298
22299 Besides being broken debug info, it can put GDB into an
22300 infinite loop. Consider:
22301
22302 When we're building the full name for Class<E>, we'll start
22303 at Class, and go look over its template type parameters,
22304 finding E. We'll then try to build the full name of E, and
22305 reach here. We're now trying to build the full name of E,
22306 and look over the parent DIE for containing scope. In the
22307 broken case, if we followed the parent DIE of E, we'd again
22308 find Class, and once again go look at its template type
22309 arguments, etc., etc. Simply don't consider such parent die
22310 as source-level parent of this die (it can't be, the language
22311 doesn't allow it), and break the loop here. */
22312 name = dwarf2_name (die, cu);
22313 parent_name = dwarf2_name (parent, cu);
22314 complaint (&symfile_complaints,
22315 _("template param type '%s' defined within parent '%s'"),
22316 name ? name : "<unknown>",
22317 parent_name ? parent_name : "<unknown>");
22318 return "";
22319 }
22320 else
22321 switch (parent->tag)
22322 {
22323 case DW_TAG_namespace:
22324 parent_type = read_type_die (parent, cu);
22325 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22326 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22327 Work around this problem here. */
22328 if (cu->language == language_cplus
22329 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22330 return "";
22331 /* We give a name to even anonymous namespaces. */
22332 return TYPE_TAG_NAME (parent_type);
22333 case DW_TAG_class_type:
22334 case DW_TAG_interface_type:
22335 case DW_TAG_structure_type:
22336 case DW_TAG_union_type:
22337 case DW_TAG_module:
22338 parent_type = read_type_die (parent, cu);
22339 if (TYPE_TAG_NAME (parent_type) != NULL)
22340 return TYPE_TAG_NAME (parent_type);
22341 else
22342 /* An anonymous structure is only allowed non-static data
22343 members; no typedefs, no member functions, et cetera.
22344 So it does not need a prefix. */
22345 return "";
22346 case DW_TAG_compile_unit:
22347 case DW_TAG_partial_unit:
22348 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22349 if (cu->language == language_cplus
22350 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22351 && die->child != NULL
22352 && (die->tag == DW_TAG_class_type
22353 || die->tag == DW_TAG_structure_type
22354 || die->tag == DW_TAG_union_type))
22355 {
22356 char *name = guess_full_die_structure_name (die, cu);
22357 if (name != NULL)
22358 return name;
22359 }
22360 return "";
22361 case DW_TAG_enumeration_type:
22362 parent_type = read_type_die (parent, cu);
22363 if (TYPE_DECLARED_CLASS (parent_type))
22364 {
22365 if (TYPE_TAG_NAME (parent_type) != NULL)
22366 return TYPE_TAG_NAME (parent_type);
22367 return "";
22368 }
22369 /* Fall through. */
22370 default:
22371 return determine_prefix (parent, cu);
22372 }
22373 }
22374
22375 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22376 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22377 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22378 an obconcat, otherwise allocate storage for the result. The CU argument is
22379 used to determine the language and hence, the appropriate separator. */
22380
22381 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22382
22383 static char *
22384 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22385 int physname, struct dwarf2_cu *cu)
22386 {
22387 const char *lead = "";
22388 const char *sep;
22389
22390 if (suffix == NULL || suffix[0] == '\0'
22391 || prefix == NULL || prefix[0] == '\0')
22392 sep = "";
22393 else if (cu->language == language_d)
22394 {
22395 /* For D, the 'main' function could be defined in any module, but it
22396 should never be prefixed. */
22397 if (strcmp (suffix, "D main") == 0)
22398 {
22399 prefix = "";
22400 sep = "";
22401 }
22402 else
22403 sep = ".";
22404 }
22405 else if (cu->language == language_fortran && physname)
22406 {
22407 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22408 DW_AT_MIPS_linkage_name is preferred and used instead. */
22409
22410 lead = "__";
22411 sep = "_MOD_";
22412 }
22413 else
22414 sep = "::";
22415
22416 if (prefix == NULL)
22417 prefix = "";
22418 if (suffix == NULL)
22419 suffix = "";
22420
22421 if (obs == NULL)
22422 {
22423 char *retval
22424 = ((char *)
22425 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22426
22427 strcpy (retval, lead);
22428 strcat (retval, prefix);
22429 strcat (retval, sep);
22430 strcat (retval, suffix);
22431 return retval;
22432 }
22433 else
22434 {
22435 /* We have an obstack. */
22436 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22437 }
22438 }
22439
22440 /* Return sibling of die, NULL if no sibling. */
22441
22442 static struct die_info *
22443 sibling_die (struct die_info *die)
22444 {
22445 return die->sibling;
22446 }
22447
22448 /* Get name of a die, return NULL if not found. */
22449
22450 static const char *
22451 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22452 struct obstack *obstack)
22453 {
22454 if (name && cu->language == language_cplus)
22455 {
22456 std::string canon_name = cp_canonicalize_string (name);
22457
22458 if (!canon_name.empty ())
22459 {
22460 if (canon_name != name)
22461 name = (const char *) obstack_copy0 (obstack,
22462 canon_name.c_str (),
22463 canon_name.length ());
22464 }
22465 }
22466
22467 return name;
22468 }
22469
22470 /* Get name of a die, return NULL if not found.
22471 Anonymous namespaces are converted to their magic string. */
22472
22473 static const char *
22474 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22475 {
22476 struct attribute *attr;
22477 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22478
22479 attr = dwarf2_attr (die, DW_AT_name, cu);
22480 if ((!attr || !DW_STRING (attr))
22481 && die->tag != DW_TAG_namespace
22482 && die->tag != DW_TAG_class_type
22483 && die->tag != DW_TAG_interface_type
22484 && die->tag != DW_TAG_structure_type
22485 && die->tag != DW_TAG_union_type)
22486 return NULL;
22487
22488 switch (die->tag)
22489 {
22490 case DW_TAG_compile_unit:
22491 case DW_TAG_partial_unit:
22492 /* Compilation units have a DW_AT_name that is a filename, not
22493 a source language identifier. */
22494 case DW_TAG_enumeration_type:
22495 case DW_TAG_enumerator:
22496 /* These tags always have simple identifiers already; no need
22497 to canonicalize them. */
22498 return DW_STRING (attr);
22499
22500 case DW_TAG_namespace:
22501 if (attr != NULL && DW_STRING (attr) != NULL)
22502 return DW_STRING (attr);
22503 return CP_ANONYMOUS_NAMESPACE_STR;
22504
22505 case DW_TAG_class_type:
22506 case DW_TAG_interface_type:
22507 case DW_TAG_structure_type:
22508 case DW_TAG_union_type:
22509 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22510 structures or unions. These were of the form "._%d" in GCC 4.1,
22511 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22512 and GCC 4.4. We work around this problem by ignoring these. */
22513 if (attr && DW_STRING (attr)
22514 && (startswith (DW_STRING (attr), "._")
22515 || startswith (DW_STRING (attr), "<anonymous")))
22516 return NULL;
22517
22518 /* GCC might emit a nameless typedef that has a linkage name. See
22519 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22520 if (!attr || DW_STRING (attr) == NULL)
22521 {
22522 char *demangled = NULL;
22523
22524 attr = dw2_linkage_name_attr (die, cu);
22525 if (attr == NULL || DW_STRING (attr) == NULL)
22526 return NULL;
22527
22528 /* Avoid demangling DW_STRING (attr) the second time on a second
22529 call for the same DIE. */
22530 if (!DW_STRING_IS_CANONICAL (attr))
22531 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22532
22533 if (demangled)
22534 {
22535 const char *base;
22536
22537 /* FIXME: we already did this for the partial symbol... */
22538 DW_STRING (attr)
22539 = ((const char *)
22540 obstack_copy0 (&objfile->per_bfd->storage_obstack,
22541 demangled, strlen (demangled)));
22542 DW_STRING_IS_CANONICAL (attr) = 1;
22543 xfree (demangled);
22544
22545 /* Strip any leading namespaces/classes, keep only the base name.
22546 DW_AT_name for named DIEs does not contain the prefixes. */
22547 base = strrchr (DW_STRING (attr), ':');
22548 if (base && base > DW_STRING (attr) && base[-1] == ':')
22549 return &base[1];
22550 else
22551 return DW_STRING (attr);
22552 }
22553 }
22554 break;
22555
22556 default:
22557 break;
22558 }
22559
22560 if (!DW_STRING_IS_CANONICAL (attr))
22561 {
22562 DW_STRING (attr)
22563 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22564 &objfile->per_bfd->storage_obstack);
22565 DW_STRING_IS_CANONICAL (attr) = 1;
22566 }
22567 return DW_STRING (attr);
22568 }
22569
22570 /* Return the die that this die in an extension of, or NULL if there
22571 is none. *EXT_CU is the CU containing DIE on input, and the CU
22572 containing the return value on output. */
22573
22574 static struct die_info *
22575 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22576 {
22577 struct attribute *attr;
22578
22579 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22580 if (attr == NULL)
22581 return NULL;
22582
22583 return follow_die_ref (die, attr, ext_cu);
22584 }
22585
22586 /* Convert a DIE tag into its string name. */
22587
22588 static const char *
22589 dwarf_tag_name (unsigned tag)
22590 {
22591 const char *name = get_DW_TAG_name (tag);
22592
22593 if (name == NULL)
22594 return "DW_TAG_<unknown>";
22595
22596 return name;
22597 }
22598
22599 /* Convert a DWARF attribute code into its string name. */
22600
22601 static const char *
22602 dwarf_attr_name (unsigned attr)
22603 {
22604 const char *name;
22605
22606 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22607 if (attr == DW_AT_MIPS_fde)
22608 return "DW_AT_MIPS_fde";
22609 #else
22610 if (attr == DW_AT_HP_block_index)
22611 return "DW_AT_HP_block_index";
22612 #endif
22613
22614 name = get_DW_AT_name (attr);
22615
22616 if (name == NULL)
22617 return "DW_AT_<unknown>";
22618
22619 return name;
22620 }
22621
22622 /* Convert a DWARF value form code into its string name. */
22623
22624 static const char *
22625 dwarf_form_name (unsigned form)
22626 {
22627 const char *name = get_DW_FORM_name (form);
22628
22629 if (name == NULL)
22630 return "DW_FORM_<unknown>";
22631
22632 return name;
22633 }
22634
22635 static const char *
22636 dwarf_bool_name (unsigned mybool)
22637 {
22638 if (mybool)
22639 return "TRUE";
22640 else
22641 return "FALSE";
22642 }
22643
22644 /* Convert a DWARF type code into its string name. */
22645
22646 static const char *
22647 dwarf_type_encoding_name (unsigned enc)
22648 {
22649 const char *name = get_DW_ATE_name (enc);
22650
22651 if (name == NULL)
22652 return "DW_ATE_<unknown>";
22653
22654 return name;
22655 }
22656
22657 static void
22658 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22659 {
22660 unsigned int i;
22661
22662 print_spaces (indent, f);
22663 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
22664 dwarf_tag_name (die->tag), die->abbrev,
22665 to_underlying (die->sect_off));
22666
22667 if (die->parent != NULL)
22668 {
22669 print_spaces (indent, f);
22670 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
22671 to_underlying (die->parent->sect_off));
22672 }
22673
22674 print_spaces (indent, f);
22675 fprintf_unfiltered (f, " has children: %s\n",
22676 dwarf_bool_name (die->child != NULL));
22677
22678 print_spaces (indent, f);
22679 fprintf_unfiltered (f, " attributes:\n");
22680
22681 for (i = 0; i < die->num_attrs; ++i)
22682 {
22683 print_spaces (indent, f);
22684 fprintf_unfiltered (f, " %s (%s) ",
22685 dwarf_attr_name (die->attrs[i].name),
22686 dwarf_form_name (die->attrs[i].form));
22687
22688 switch (die->attrs[i].form)
22689 {
22690 case DW_FORM_addr:
22691 case DW_FORM_GNU_addr_index:
22692 fprintf_unfiltered (f, "address: ");
22693 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22694 break;
22695 case DW_FORM_block2:
22696 case DW_FORM_block4:
22697 case DW_FORM_block:
22698 case DW_FORM_block1:
22699 fprintf_unfiltered (f, "block: size %s",
22700 pulongest (DW_BLOCK (&die->attrs[i])->size));
22701 break;
22702 case DW_FORM_exprloc:
22703 fprintf_unfiltered (f, "expression: size %s",
22704 pulongest (DW_BLOCK (&die->attrs[i])->size));
22705 break;
22706 case DW_FORM_data16:
22707 fprintf_unfiltered (f, "constant of 16 bytes");
22708 break;
22709 case DW_FORM_ref_addr:
22710 fprintf_unfiltered (f, "ref address: ");
22711 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22712 break;
22713 case DW_FORM_GNU_ref_alt:
22714 fprintf_unfiltered (f, "alt ref address: ");
22715 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22716 break;
22717 case DW_FORM_ref1:
22718 case DW_FORM_ref2:
22719 case DW_FORM_ref4:
22720 case DW_FORM_ref8:
22721 case DW_FORM_ref_udata:
22722 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22723 (long) (DW_UNSND (&die->attrs[i])));
22724 break;
22725 case DW_FORM_data1:
22726 case DW_FORM_data2:
22727 case DW_FORM_data4:
22728 case DW_FORM_data8:
22729 case DW_FORM_udata:
22730 case DW_FORM_sdata:
22731 fprintf_unfiltered (f, "constant: %s",
22732 pulongest (DW_UNSND (&die->attrs[i])));
22733 break;
22734 case DW_FORM_sec_offset:
22735 fprintf_unfiltered (f, "section offset: %s",
22736 pulongest (DW_UNSND (&die->attrs[i])));
22737 break;
22738 case DW_FORM_ref_sig8:
22739 fprintf_unfiltered (f, "signature: %s",
22740 hex_string (DW_SIGNATURE (&die->attrs[i])));
22741 break;
22742 case DW_FORM_string:
22743 case DW_FORM_strp:
22744 case DW_FORM_line_strp:
22745 case DW_FORM_GNU_str_index:
22746 case DW_FORM_GNU_strp_alt:
22747 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22748 DW_STRING (&die->attrs[i])
22749 ? DW_STRING (&die->attrs[i]) : "",
22750 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22751 break;
22752 case DW_FORM_flag:
22753 if (DW_UNSND (&die->attrs[i]))
22754 fprintf_unfiltered (f, "flag: TRUE");
22755 else
22756 fprintf_unfiltered (f, "flag: FALSE");
22757 break;
22758 case DW_FORM_flag_present:
22759 fprintf_unfiltered (f, "flag: TRUE");
22760 break;
22761 case DW_FORM_indirect:
22762 /* The reader will have reduced the indirect form to
22763 the "base form" so this form should not occur. */
22764 fprintf_unfiltered (f,
22765 "unexpected attribute form: DW_FORM_indirect");
22766 break;
22767 case DW_FORM_implicit_const:
22768 fprintf_unfiltered (f, "constant: %s",
22769 plongest (DW_SND (&die->attrs[i])));
22770 break;
22771 default:
22772 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22773 die->attrs[i].form);
22774 break;
22775 }
22776 fprintf_unfiltered (f, "\n");
22777 }
22778 }
22779
22780 static void
22781 dump_die_for_error (struct die_info *die)
22782 {
22783 dump_die_shallow (gdb_stderr, 0, die);
22784 }
22785
22786 static void
22787 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22788 {
22789 int indent = level * 4;
22790
22791 gdb_assert (die != NULL);
22792
22793 if (level >= max_level)
22794 return;
22795
22796 dump_die_shallow (f, indent, die);
22797
22798 if (die->child != NULL)
22799 {
22800 print_spaces (indent, f);
22801 fprintf_unfiltered (f, " Children:");
22802 if (level + 1 < max_level)
22803 {
22804 fprintf_unfiltered (f, "\n");
22805 dump_die_1 (f, level + 1, max_level, die->child);
22806 }
22807 else
22808 {
22809 fprintf_unfiltered (f,
22810 " [not printed, max nesting level reached]\n");
22811 }
22812 }
22813
22814 if (die->sibling != NULL && level > 0)
22815 {
22816 dump_die_1 (f, level, max_level, die->sibling);
22817 }
22818 }
22819
22820 /* This is called from the pdie macro in gdbinit.in.
22821 It's not static so gcc will keep a copy callable from gdb. */
22822
22823 void
22824 dump_die (struct die_info *die, int max_level)
22825 {
22826 dump_die_1 (gdb_stdlog, 0, max_level, die);
22827 }
22828
22829 static void
22830 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22831 {
22832 void **slot;
22833
22834 slot = htab_find_slot_with_hash (cu->die_hash, die,
22835 to_underlying (die->sect_off),
22836 INSERT);
22837
22838 *slot = die;
22839 }
22840
22841 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22842 required kind. */
22843
22844 static sect_offset
22845 dwarf2_get_ref_die_offset (const struct attribute *attr)
22846 {
22847 if (attr_form_is_ref (attr))
22848 return (sect_offset) DW_UNSND (attr);
22849
22850 complaint (&symfile_complaints,
22851 _("unsupported die ref attribute form: '%s'"),
22852 dwarf_form_name (attr->form));
22853 return {};
22854 }
22855
22856 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22857 * the value held by the attribute is not constant. */
22858
22859 static LONGEST
22860 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22861 {
22862 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22863 return DW_SND (attr);
22864 else if (attr->form == DW_FORM_udata
22865 || attr->form == DW_FORM_data1
22866 || attr->form == DW_FORM_data2
22867 || attr->form == DW_FORM_data4
22868 || attr->form == DW_FORM_data8)
22869 return DW_UNSND (attr);
22870 else
22871 {
22872 /* For DW_FORM_data16 see attr_form_is_constant. */
22873 complaint (&symfile_complaints,
22874 _("Attribute value is not a constant (%s)"),
22875 dwarf_form_name (attr->form));
22876 return default_value;
22877 }
22878 }
22879
22880 /* Follow reference or signature attribute ATTR of SRC_DIE.
22881 On entry *REF_CU is the CU of SRC_DIE.
22882 On exit *REF_CU is the CU of the result. */
22883
22884 static struct die_info *
22885 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22886 struct dwarf2_cu **ref_cu)
22887 {
22888 struct die_info *die;
22889
22890 if (attr_form_is_ref (attr))
22891 die = follow_die_ref (src_die, attr, ref_cu);
22892 else if (attr->form == DW_FORM_ref_sig8)
22893 die = follow_die_sig (src_die, attr, ref_cu);
22894 else
22895 {
22896 dump_die_for_error (src_die);
22897 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22898 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22899 }
22900
22901 return die;
22902 }
22903
22904 /* Follow reference OFFSET.
22905 On entry *REF_CU is the CU of the source die referencing OFFSET.
22906 On exit *REF_CU is the CU of the result.
22907 Returns NULL if OFFSET is invalid. */
22908
22909 static struct die_info *
22910 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22911 struct dwarf2_cu **ref_cu)
22912 {
22913 struct die_info temp_die;
22914 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22915 struct dwarf2_per_objfile *dwarf2_per_objfile
22916 = cu->per_cu->dwarf2_per_objfile;
22917 struct objfile *objfile = dwarf2_per_objfile->objfile;
22918
22919 gdb_assert (cu->per_cu != NULL);
22920
22921 target_cu = cu;
22922
22923 if (cu->per_cu->is_debug_types)
22924 {
22925 /* .debug_types CUs cannot reference anything outside their CU.
22926 If they need to, they have to reference a signatured type via
22927 DW_FORM_ref_sig8. */
22928 if (!offset_in_cu_p (&cu->header, sect_off))
22929 return NULL;
22930 }
22931 else if (offset_in_dwz != cu->per_cu->is_dwz
22932 || !offset_in_cu_p (&cu->header, sect_off))
22933 {
22934 struct dwarf2_per_cu_data *per_cu;
22935
22936 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22937 dwarf2_per_objfile);
22938
22939 /* If necessary, add it to the queue and load its DIEs. */
22940 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22941 load_full_comp_unit (per_cu, cu->language);
22942
22943 target_cu = per_cu->cu;
22944 }
22945 else if (cu->dies == NULL)
22946 {
22947 /* We're loading full DIEs during partial symbol reading. */
22948 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22949 load_full_comp_unit (cu->per_cu, language_minimal);
22950 }
22951
22952 *ref_cu = target_cu;
22953 temp_die.sect_off = sect_off;
22954 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22955 &temp_die,
22956 to_underlying (sect_off));
22957 }
22958
22959 /* Follow reference attribute ATTR of SRC_DIE.
22960 On entry *REF_CU is the CU of SRC_DIE.
22961 On exit *REF_CU is the CU of the result. */
22962
22963 static struct die_info *
22964 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22965 struct dwarf2_cu **ref_cu)
22966 {
22967 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22968 struct dwarf2_cu *cu = *ref_cu;
22969 struct die_info *die;
22970
22971 die = follow_die_offset (sect_off,
22972 (attr->form == DW_FORM_GNU_ref_alt
22973 || cu->per_cu->is_dwz),
22974 ref_cu);
22975 if (!die)
22976 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
22977 "at 0x%x [in module %s]"),
22978 to_underlying (sect_off), to_underlying (src_die->sect_off),
22979 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22980
22981 return die;
22982 }
22983
22984 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22985 Returned value is intended for DW_OP_call*. Returned
22986 dwarf2_locexpr_baton->data has lifetime of
22987 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22988
22989 struct dwarf2_locexpr_baton
22990 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22991 struct dwarf2_per_cu_data *per_cu,
22992 CORE_ADDR (*get_frame_pc) (void *baton),
22993 void *baton)
22994 {
22995 struct dwarf2_cu *cu;
22996 struct die_info *die;
22997 struct attribute *attr;
22998 struct dwarf2_locexpr_baton retval;
22999 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23000 struct dwarf2_per_objfile *dwarf2_per_objfile
23001 = get_dwarf2_per_objfile (objfile);
23002
23003 if (per_cu->cu == NULL)
23004 load_cu (per_cu);
23005 cu = per_cu->cu;
23006 if (cu == NULL)
23007 {
23008 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23009 Instead just throw an error, not much else we can do. */
23010 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
23011 to_underlying (sect_off), objfile_name (objfile));
23012 }
23013
23014 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23015 if (!die)
23016 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
23017 to_underlying (sect_off), objfile_name (objfile));
23018
23019 attr = dwarf2_attr (die, DW_AT_location, cu);
23020 if (!attr)
23021 {
23022 /* DWARF: "If there is no such attribute, then there is no effect.".
23023 DATA is ignored if SIZE is 0. */
23024
23025 retval.data = NULL;
23026 retval.size = 0;
23027 }
23028 else if (attr_form_is_section_offset (attr))
23029 {
23030 struct dwarf2_loclist_baton loclist_baton;
23031 CORE_ADDR pc = (*get_frame_pc) (baton);
23032 size_t size;
23033
23034 fill_in_loclist_baton (cu, &loclist_baton, attr);
23035
23036 retval.data = dwarf2_find_location_expression (&loclist_baton,
23037 &size, pc);
23038 retval.size = size;
23039 }
23040 else
23041 {
23042 if (!attr_form_is_block (attr))
23043 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
23044 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23045 to_underlying (sect_off), objfile_name (objfile));
23046
23047 retval.data = DW_BLOCK (attr)->data;
23048 retval.size = DW_BLOCK (attr)->size;
23049 }
23050 retval.per_cu = cu->per_cu;
23051
23052 age_cached_comp_units (dwarf2_per_objfile);
23053
23054 return retval;
23055 }
23056
23057 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23058 offset. */
23059
23060 struct dwarf2_locexpr_baton
23061 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23062 struct dwarf2_per_cu_data *per_cu,
23063 CORE_ADDR (*get_frame_pc) (void *baton),
23064 void *baton)
23065 {
23066 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23067
23068 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23069 }
23070
23071 /* Write a constant of a given type as target-ordered bytes into
23072 OBSTACK. */
23073
23074 static const gdb_byte *
23075 write_constant_as_bytes (struct obstack *obstack,
23076 enum bfd_endian byte_order,
23077 struct type *type,
23078 ULONGEST value,
23079 LONGEST *len)
23080 {
23081 gdb_byte *result;
23082
23083 *len = TYPE_LENGTH (type);
23084 result = (gdb_byte *) obstack_alloc (obstack, *len);
23085 store_unsigned_integer (result, *len, byte_order, value);
23086
23087 return result;
23088 }
23089
23090 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23091 pointer to the constant bytes and set LEN to the length of the
23092 data. If memory is needed, allocate it on OBSTACK. If the DIE
23093 does not have a DW_AT_const_value, return NULL. */
23094
23095 const gdb_byte *
23096 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23097 struct dwarf2_per_cu_data *per_cu,
23098 struct obstack *obstack,
23099 LONGEST *len)
23100 {
23101 struct dwarf2_cu *cu;
23102 struct die_info *die;
23103 struct attribute *attr;
23104 const gdb_byte *result = NULL;
23105 struct type *type;
23106 LONGEST value;
23107 enum bfd_endian byte_order;
23108 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23109
23110 if (per_cu->cu == NULL)
23111 load_cu (per_cu);
23112 cu = per_cu->cu;
23113 if (cu == NULL)
23114 {
23115 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23116 Instead just throw an error, not much else we can do. */
23117 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
23118 to_underlying (sect_off), objfile_name (objfile));
23119 }
23120
23121 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23122 if (!die)
23123 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
23124 to_underlying (sect_off), objfile_name (objfile));
23125
23126
23127 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23128 if (attr == NULL)
23129 return NULL;
23130
23131 byte_order = (bfd_big_endian (objfile->obfd)
23132 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23133
23134 switch (attr->form)
23135 {
23136 case DW_FORM_addr:
23137 case DW_FORM_GNU_addr_index:
23138 {
23139 gdb_byte *tem;
23140
23141 *len = cu->header.addr_size;
23142 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23143 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23144 result = tem;
23145 }
23146 break;
23147 case DW_FORM_string:
23148 case DW_FORM_strp:
23149 case DW_FORM_GNU_str_index:
23150 case DW_FORM_GNU_strp_alt:
23151 /* DW_STRING is already allocated on the objfile obstack, point
23152 directly to it. */
23153 result = (const gdb_byte *) DW_STRING (attr);
23154 *len = strlen (DW_STRING (attr));
23155 break;
23156 case DW_FORM_block1:
23157 case DW_FORM_block2:
23158 case DW_FORM_block4:
23159 case DW_FORM_block:
23160 case DW_FORM_exprloc:
23161 case DW_FORM_data16:
23162 result = DW_BLOCK (attr)->data;
23163 *len = DW_BLOCK (attr)->size;
23164 break;
23165
23166 /* The DW_AT_const_value attributes are supposed to carry the
23167 symbol's value "represented as it would be on the target
23168 architecture." By the time we get here, it's already been
23169 converted to host endianness, so we just need to sign- or
23170 zero-extend it as appropriate. */
23171 case DW_FORM_data1:
23172 type = die_type (die, cu);
23173 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23174 if (result == NULL)
23175 result = write_constant_as_bytes (obstack, byte_order,
23176 type, value, len);
23177 break;
23178 case DW_FORM_data2:
23179 type = die_type (die, cu);
23180 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23181 if (result == NULL)
23182 result = write_constant_as_bytes (obstack, byte_order,
23183 type, value, len);
23184 break;
23185 case DW_FORM_data4:
23186 type = die_type (die, cu);
23187 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23188 if (result == NULL)
23189 result = write_constant_as_bytes (obstack, byte_order,
23190 type, value, len);
23191 break;
23192 case DW_FORM_data8:
23193 type = die_type (die, cu);
23194 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23195 if (result == NULL)
23196 result = write_constant_as_bytes (obstack, byte_order,
23197 type, value, len);
23198 break;
23199
23200 case DW_FORM_sdata:
23201 case DW_FORM_implicit_const:
23202 type = die_type (die, cu);
23203 result = write_constant_as_bytes (obstack, byte_order,
23204 type, DW_SND (attr), len);
23205 break;
23206
23207 case DW_FORM_udata:
23208 type = die_type (die, cu);
23209 result = write_constant_as_bytes (obstack, byte_order,
23210 type, DW_UNSND (attr), len);
23211 break;
23212
23213 default:
23214 complaint (&symfile_complaints,
23215 _("unsupported const value attribute form: '%s'"),
23216 dwarf_form_name (attr->form));
23217 break;
23218 }
23219
23220 return result;
23221 }
23222
23223 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23224 valid type for this die is found. */
23225
23226 struct type *
23227 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23228 struct dwarf2_per_cu_data *per_cu)
23229 {
23230 struct dwarf2_cu *cu;
23231 struct die_info *die;
23232
23233 if (per_cu->cu == NULL)
23234 load_cu (per_cu);
23235 cu = per_cu->cu;
23236 if (!cu)
23237 return NULL;
23238
23239 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23240 if (!die)
23241 return NULL;
23242
23243 return die_type (die, cu);
23244 }
23245
23246 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23247 PER_CU. */
23248
23249 struct type *
23250 dwarf2_get_die_type (cu_offset die_offset,
23251 struct dwarf2_per_cu_data *per_cu)
23252 {
23253 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23254 return get_die_type_at_offset (die_offset_sect, per_cu);
23255 }
23256
23257 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23258 On entry *REF_CU is the CU of SRC_DIE.
23259 On exit *REF_CU is the CU of the result.
23260 Returns NULL if the referenced DIE isn't found. */
23261
23262 static struct die_info *
23263 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23264 struct dwarf2_cu **ref_cu)
23265 {
23266 struct die_info temp_die;
23267 struct dwarf2_cu *sig_cu;
23268 struct die_info *die;
23269
23270 /* While it might be nice to assert sig_type->type == NULL here,
23271 we can get here for DW_AT_imported_declaration where we need
23272 the DIE not the type. */
23273
23274 /* If necessary, add it to the queue and load its DIEs. */
23275
23276 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23277 read_signatured_type (sig_type);
23278
23279 sig_cu = sig_type->per_cu.cu;
23280 gdb_assert (sig_cu != NULL);
23281 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23282 temp_die.sect_off = sig_type->type_offset_in_section;
23283 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23284 to_underlying (temp_die.sect_off));
23285 if (die)
23286 {
23287 struct dwarf2_per_objfile *dwarf2_per_objfile
23288 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23289
23290 /* For .gdb_index version 7 keep track of included TUs.
23291 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23292 if (dwarf2_per_objfile->index_table != NULL
23293 && dwarf2_per_objfile->index_table->version <= 7)
23294 {
23295 VEC_safe_push (dwarf2_per_cu_ptr,
23296 (*ref_cu)->per_cu->imported_symtabs,
23297 sig_cu->per_cu);
23298 }
23299
23300 *ref_cu = sig_cu;
23301 return die;
23302 }
23303
23304 return NULL;
23305 }
23306
23307 /* Follow signatured type referenced by ATTR in SRC_DIE.
23308 On entry *REF_CU is the CU of SRC_DIE.
23309 On exit *REF_CU is the CU of the result.
23310 The result is the DIE of the type.
23311 If the referenced type cannot be found an error is thrown. */
23312
23313 static struct die_info *
23314 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23315 struct dwarf2_cu **ref_cu)
23316 {
23317 ULONGEST signature = DW_SIGNATURE (attr);
23318 struct signatured_type *sig_type;
23319 struct die_info *die;
23320
23321 gdb_assert (attr->form == DW_FORM_ref_sig8);
23322
23323 sig_type = lookup_signatured_type (*ref_cu, signature);
23324 /* sig_type will be NULL if the signatured type is missing from
23325 the debug info. */
23326 if (sig_type == NULL)
23327 {
23328 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23329 " from DIE at 0x%x [in module %s]"),
23330 hex_string (signature), to_underlying (src_die->sect_off),
23331 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23332 }
23333
23334 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23335 if (die == NULL)
23336 {
23337 dump_die_for_error (src_die);
23338 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23339 " from DIE at 0x%x [in module %s]"),
23340 hex_string (signature), to_underlying (src_die->sect_off),
23341 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23342 }
23343
23344 return die;
23345 }
23346
23347 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23348 reading in and processing the type unit if necessary. */
23349
23350 static struct type *
23351 get_signatured_type (struct die_info *die, ULONGEST signature,
23352 struct dwarf2_cu *cu)
23353 {
23354 struct dwarf2_per_objfile *dwarf2_per_objfile
23355 = cu->per_cu->dwarf2_per_objfile;
23356 struct signatured_type *sig_type;
23357 struct dwarf2_cu *type_cu;
23358 struct die_info *type_die;
23359 struct type *type;
23360
23361 sig_type = lookup_signatured_type (cu, signature);
23362 /* sig_type will be NULL if the signatured type is missing from
23363 the debug info. */
23364 if (sig_type == NULL)
23365 {
23366 complaint (&symfile_complaints,
23367 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23368 " from DIE at 0x%x [in module %s]"),
23369 hex_string (signature), to_underlying (die->sect_off),
23370 objfile_name (dwarf2_per_objfile->objfile));
23371 return build_error_marker_type (cu, die);
23372 }
23373
23374 /* If we already know the type we're done. */
23375 if (sig_type->type != NULL)
23376 return sig_type->type;
23377
23378 type_cu = cu;
23379 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23380 if (type_die != NULL)
23381 {
23382 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23383 is created. This is important, for example, because for c++ classes
23384 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23385 type = read_type_die (type_die, type_cu);
23386 if (type == NULL)
23387 {
23388 complaint (&symfile_complaints,
23389 _("Dwarf Error: Cannot build signatured type %s"
23390 " referenced from DIE at 0x%x [in module %s]"),
23391 hex_string (signature), to_underlying (die->sect_off),
23392 objfile_name (dwarf2_per_objfile->objfile));
23393 type = build_error_marker_type (cu, die);
23394 }
23395 }
23396 else
23397 {
23398 complaint (&symfile_complaints,
23399 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23400 " from DIE at 0x%x [in module %s]"),
23401 hex_string (signature), to_underlying (die->sect_off),
23402 objfile_name (dwarf2_per_objfile->objfile));
23403 type = build_error_marker_type (cu, die);
23404 }
23405 sig_type->type = type;
23406
23407 return type;
23408 }
23409
23410 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23411 reading in and processing the type unit if necessary. */
23412
23413 static struct type *
23414 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23415 struct dwarf2_cu *cu) /* ARI: editCase function */
23416 {
23417 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23418 if (attr_form_is_ref (attr))
23419 {
23420 struct dwarf2_cu *type_cu = cu;
23421 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23422
23423 return read_type_die (type_die, type_cu);
23424 }
23425 else if (attr->form == DW_FORM_ref_sig8)
23426 {
23427 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23428 }
23429 else
23430 {
23431 struct dwarf2_per_objfile *dwarf2_per_objfile
23432 = cu->per_cu->dwarf2_per_objfile;
23433
23434 complaint (&symfile_complaints,
23435 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23436 " at 0x%x [in module %s]"),
23437 dwarf_form_name (attr->form), to_underlying (die->sect_off),
23438 objfile_name (dwarf2_per_objfile->objfile));
23439 return build_error_marker_type (cu, die);
23440 }
23441 }
23442
23443 /* Load the DIEs associated with type unit PER_CU into memory. */
23444
23445 static void
23446 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23447 {
23448 struct signatured_type *sig_type;
23449
23450 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23451 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23452
23453 /* We have the per_cu, but we need the signatured_type.
23454 Fortunately this is an easy translation. */
23455 gdb_assert (per_cu->is_debug_types);
23456 sig_type = (struct signatured_type *) per_cu;
23457
23458 gdb_assert (per_cu->cu == NULL);
23459
23460 read_signatured_type (sig_type);
23461
23462 gdb_assert (per_cu->cu != NULL);
23463 }
23464
23465 /* die_reader_func for read_signatured_type.
23466 This is identical to load_full_comp_unit_reader,
23467 but is kept separate for now. */
23468
23469 static void
23470 read_signatured_type_reader (const struct die_reader_specs *reader,
23471 const gdb_byte *info_ptr,
23472 struct die_info *comp_unit_die,
23473 int has_children,
23474 void *data)
23475 {
23476 struct dwarf2_cu *cu = reader->cu;
23477
23478 gdb_assert (cu->die_hash == NULL);
23479 cu->die_hash =
23480 htab_create_alloc_ex (cu->header.length / 12,
23481 die_hash,
23482 die_eq,
23483 NULL,
23484 &cu->comp_unit_obstack,
23485 hashtab_obstack_allocate,
23486 dummy_obstack_deallocate);
23487
23488 if (has_children)
23489 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23490 &info_ptr, comp_unit_die);
23491 cu->dies = comp_unit_die;
23492 /* comp_unit_die is not stored in die_hash, no need. */
23493
23494 /* We try not to read any attributes in this function, because not
23495 all CUs needed for references have been loaded yet, and symbol
23496 table processing isn't initialized. But we have to set the CU language,
23497 or we won't be able to build types correctly.
23498 Similarly, if we do not read the producer, we can not apply
23499 producer-specific interpretation. */
23500 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23501 }
23502
23503 /* Read in a signatured type and build its CU and DIEs.
23504 If the type is a stub for the real type in a DWO file,
23505 read in the real type from the DWO file as well. */
23506
23507 static void
23508 read_signatured_type (struct signatured_type *sig_type)
23509 {
23510 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23511
23512 gdb_assert (per_cu->is_debug_types);
23513 gdb_assert (per_cu->cu == NULL);
23514
23515 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23516 read_signatured_type_reader, NULL);
23517 sig_type->per_cu.tu_read = 1;
23518 }
23519
23520 /* Decode simple location descriptions.
23521 Given a pointer to a dwarf block that defines a location, compute
23522 the location and return the value.
23523
23524 NOTE drow/2003-11-18: This function is called in two situations
23525 now: for the address of static or global variables (partial symbols
23526 only) and for offsets into structures which are expected to be
23527 (more or less) constant. The partial symbol case should go away,
23528 and only the constant case should remain. That will let this
23529 function complain more accurately. A few special modes are allowed
23530 without complaint for global variables (for instance, global
23531 register values and thread-local values).
23532
23533 A location description containing no operations indicates that the
23534 object is optimized out. The return value is 0 for that case.
23535 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23536 callers will only want a very basic result and this can become a
23537 complaint.
23538
23539 Note that stack[0] is unused except as a default error return. */
23540
23541 static CORE_ADDR
23542 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23543 {
23544 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23545 size_t i;
23546 size_t size = blk->size;
23547 const gdb_byte *data = blk->data;
23548 CORE_ADDR stack[64];
23549 int stacki;
23550 unsigned int bytes_read, unsnd;
23551 gdb_byte op;
23552
23553 i = 0;
23554 stacki = 0;
23555 stack[stacki] = 0;
23556 stack[++stacki] = 0;
23557
23558 while (i < size)
23559 {
23560 op = data[i++];
23561 switch (op)
23562 {
23563 case DW_OP_lit0:
23564 case DW_OP_lit1:
23565 case DW_OP_lit2:
23566 case DW_OP_lit3:
23567 case DW_OP_lit4:
23568 case DW_OP_lit5:
23569 case DW_OP_lit6:
23570 case DW_OP_lit7:
23571 case DW_OP_lit8:
23572 case DW_OP_lit9:
23573 case DW_OP_lit10:
23574 case DW_OP_lit11:
23575 case DW_OP_lit12:
23576 case DW_OP_lit13:
23577 case DW_OP_lit14:
23578 case DW_OP_lit15:
23579 case DW_OP_lit16:
23580 case DW_OP_lit17:
23581 case DW_OP_lit18:
23582 case DW_OP_lit19:
23583 case DW_OP_lit20:
23584 case DW_OP_lit21:
23585 case DW_OP_lit22:
23586 case DW_OP_lit23:
23587 case DW_OP_lit24:
23588 case DW_OP_lit25:
23589 case DW_OP_lit26:
23590 case DW_OP_lit27:
23591 case DW_OP_lit28:
23592 case DW_OP_lit29:
23593 case DW_OP_lit30:
23594 case DW_OP_lit31:
23595 stack[++stacki] = op - DW_OP_lit0;
23596 break;
23597
23598 case DW_OP_reg0:
23599 case DW_OP_reg1:
23600 case DW_OP_reg2:
23601 case DW_OP_reg3:
23602 case DW_OP_reg4:
23603 case DW_OP_reg5:
23604 case DW_OP_reg6:
23605 case DW_OP_reg7:
23606 case DW_OP_reg8:
23607 case DW_OP_reg9:
23608 case DW_OP_reg10:
23609 case DW_OP_reg11:
23610 case DW_OP_reg12:
23611 case DW_OP_reg13:
23612 case DW_OP_reg14:
23613 case DW_OP_reg15:
23614 case DW_OP_reg16:
23615 case DW_OP_reg17:
23616 case DW_OP_reg18:
23617 case DW_OP_reg19:
23618 case DW_OP_reg20:
23619 case DW_OP_reg21:
23620 case DW_OP_reg22:
23621 case DW_OP_reg23:
23622 case DW_OP_reg24:
23623 case DW_OP_reg25:
23624 case DW_OP_reg26:
23625 case DW_OP_reg27:
23626 case DW_OP_reg28:
23627 case DW_OP_reg29:
23628 case DW_OP_reg30:
23629 case DW_OP_reg31:
23630 stack[++stacki] = op - DW_OP_reg0;
23631 if (i < size)
23632 dwarf2_complex_location_expr_complaint ();
23633 break;
23634
23635 case DW_OP_regx:
23636 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23637 i += bytes_read;
23638 stack[++stacki] = unsnd;
23639 if (i < size)
23640 dwarf2_complex_location_expr_complaint ();
23641 break;
23642
23643 case DW_OP_addr:
23644 stack[++stacki] = read_address (objfile->obfd, &data[i],
23645 cu, &bytes_read);
23646 i += bytes_read;
23647 break;
23648
23649 case DW_OP_const1u:
23650 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23651 i += 1;
23652 break;
23653
23654 case DW_OP_const1s:
23655 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23656 i += 1;
23657 break;
23658
23659 case DW_OP_const2u:
23660 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23661 i += 2;
23662 break;
23663
23664 case DW_OP_const2s:
23665 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23666 i += 2;
23667 break;
23668
23669 case DW_OP_const4u:
23670 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23671 i += 4;
23672 break;
23673
23674 case DW_OP_const4s:
23675 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23676 i += 4;
23677 break;
23678
23679 case DW_OP_const8u:
23680 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23681 i += 8;
23682 break;
23683
23684 case DW_OP_constu:
23685 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23686 &bytes_read);
23687 i += bytes_read;
23688 break;
23689
23690 case DW_OP_consts:
23691 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23692 i += bytes_read;
23693 break;
23694
23695 case DW_OP_dup:
23696 stack[stacki + 1] = stack[stacki];
23697 stacki++;
23698 break;
23699
23700 case DW_OP_plus:
23701 stack[stacki - 1] += stack[stacki];
23702 stacki--;
23703 break;
23704
23705 case DW_OP_plus_uconst:
23706 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23707 &bytes_read);
23708 i += bytes_read;
23709 break;
23710
23711 case DW_OP_minus:
23712 stack[stacki - 1] -= stack[stacki];
23713 stacki--;
23714 break;
23715
23716 case DW_OP_deref:
23717 /* If we're not the last op, then we definitely can't encode
23718 this using GDB's address_class enum. This is valid for partial
23719 global symbols, although the variable's address will be bogus
23720 in the psymtab. */
23721 if (i < size)
23722 dwarf2_complex_location_expr_complaint ();
23723 break;
23724
23725 case DW_OP_GNU_push_tls_address:
23726 case DW_OP_form_tls_address:
23727 /* The top of the stack has the offset from the beginning
23728 of the thread control block at which the variable is located. */
23729 /* Nothing should follow this operator, so the top of stack would
23730 be returned. */
23731 /* This is valid for partial global symbols, but the variable's
23732 address will be bogus in the psymtab. Make it always at least
23733 non-zero to not look as a variable garbage collected by linker
23734 which have DW_OP_addr 0. */
23735 if (i < size)
23736 dwarf2_complex_location_expr_complaint ();
23737 stack[stacki]++;
23738 break;
23739
23740 case DW_OP_GNU_uninit:
23741 break;
23742
23743 case DW_OP_GNU_addr_index:
23744 case DW_OP_GNU_const_index:
23745 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23746 &bytes_read);
23747 i += bytes_read;
23748 break;
23749
23750 default:
23751 {
23752 const char *name = get_DW_OP_name (op);
23753
23754 if (name)
23755 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23756 name);
23757 else
23758 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23759 op);
23760 }
23761
23762 return (stack[stacki]);
23763 }
23764
23765 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23766 outside of the allocated space. Also enforce minimum>0. */
23767 if (stacki >= ARRAY_SIZE (stack) - 1)
23768 {
23769 complaint (&symfile_complaints,
23770 _("location description stack overflow"));
23771 return 0;
23772 }
23773
23774 if (stacki <= 0)
23775 {
23776 complaint (&symfile_complaints,
23777 _("location description stack underflow"));
23778 return 0;
23779 }
23780 }
23781 return (stack[stacki]);
23782 }
23783
23784 /* memory allocation interface */
23785
23786 static struct dwarf_block *
23787 dwarf_alloc_block (struct dwarf2_cu *cu)
23788 {
23789 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23790 }
23791
23792 static struct die_info *
23793 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23794 {
23795 struct die_info *die;
23796 size_t size = sizeof (struct die_info);
23797
23798 if (num_attrs > 1)
23799 size += (num_attrs - 1) * sizeof (struct attribute);
23800
23801 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23802 memset (die, 0, sizeof (struct die_info));
23803 return (die);
23804 }
23805
23806 \f
23807 /* Macro support. */
23808
23809 /* Return file name relative to the compilation directory of file number I in
23810 *LH's file name table. The result is allocated using xmalloc; the caller is
23811 responsible for freeing it. */
23812
23813 static char *
23814 file_file_name (int file, struct line_header *lh)
23815 {
23816 /* Is the file number a valid index into the line header's file name
23817 table? Remember that file numbers start with one, not zero. */
23818 if (1 <= file && file <= lh->file_names.size ())
23819 {
23820 const file_entry &fe = lh->file_names[file - 1];
23821
23822 if (!IS_ABSOLUTE_PATH (fe.name))
23823 {
23824 const char *dir = fe.include_dir (lh);
23825 if (dir != NULL)
23826 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23827 }
23828 return xstrdup (fe.name);
23829 }
23830 else
23831 {
23832 /* The compiler produced a bogus file number. We can at least
23833 record the macro definitions made in the file, even if we
23834 won't be able to find the file by name. */
23835 char fake_name[80];
23836
23837 xsnprintf (fake_name, sizeof (fake_name),
23838 "<bad macro file number %d>", file);
23839
23840 complaint (&symfile_complaints,
23841 _("bad file number in macro information (%d)"),
23842 file);
23843
23844 return xstrdup (fake_name);
23845 }
23846 }
23847
23848 /* Return the full name of file number I in *LH's file name table.
23849 Use COMP_DIR as the name of the current directory of the
23850 compilation. The result is allocated using xmalloc; the caller is
23851 responsible for freeing it. */
23852 static char *
23853 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23854 {
23855 /* Is the file number a valid index into the line header's file name
23856 table? Remember that file numbers start with one, not zero. */
23857 if (1 <= file && file <= lh->file_names.size ())
23858 {
23859 char *relative = file_file_name (file, lh);
23860
23861 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23862 return relative;
23863 return reconcat (relative, comp_dir, SLASH_STRING,
23864 relative, (char *) NULL);
23865 }
23866 else
23867 return file_file_name (file, lh);
23868 }
23869
23870
23871 static struct macro_source_file *
23872 macro_start_file (int file, int line,
23873 struct macro_source_file *current_file,
23874 struct line_header *lh)
23875 {
23876 /* File name relative to the compilation directory of this source file. */
23877 char *file_name = file_file_name (file, lh);
23878
23879 if (! current_file)
23880 {
23881 /* Note: We don't create a macro table for this compilation unit
23882 at all until we actually get a filename. */
23883 struct macro_table *macro_table = get_macro_table ();
23884
23885 /* If we have no current file, then this must be the start_file
23886 directive for the compilation unit's main source file. */
23887 current_file = macro_set_main (macro_table, file_name);
23888 macro_define_special (macro_table);
23889 }
23890 else
23891 current_file = macro_include (current_file, line, file_name);
23892
23893 xfree (file_name);
23894
23895 return current_file;
23896 }
23897
23898 static const char *
23899 consume_improper_spaces (const char *p, const char *body)
23900 {
23901 if (*p == ' ')
23902 {
23903 complaint (&symfile_complaints,
23904 _("macro definition contains spaces "
23905 "in formal argument list:\n`%s'"),
23906 body);
23907
23908 while (*p == ' ')
23909 p++;
23910 }
23911
23912 return p;
23913 }
23914
23915
23916 static void
23917 parse_macro_definition (struct macro_source_file *file, int line,
23918 const char *body)
23919 {
23920 const char *p;
23921
23922 /* The body string takes one of two forms. For object-like macro
23923 definitions, it should be:
23924
23925 <macro name> " " <definition>
23926
23927 For function-like macro definitions, it should be:
23928
23929 <macro name> "() " <definition>
23930 or
23931 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23932
23933 Spaces may appear only where explicitly indicated, and in the
23934 <definition>.
23935
23936 The Dwarf 2 spec says that an object-like macro's name is always
23937 followed by a space, but versions of GCC around March 2002 omit
23938 the space when the macro's definition is the empty string.
23939
23940 The Dwarf 2 spec says that there should be no spaces between the
23941 formal arguments in a function-like macro's formal argument list,
23942 but versions of GCC around March 2002 include spaces after the
23943 commas. */
23944
23945
23946 /* Find the extent of the macro name. The macro name is terminated
23947 by either a space or null character (for an object-like macro) or
23948 an opening paren (for a function-like macro). */
23949 for (p = body; *p; p++)
23950 if (*p == ' ' || *p == '(')
23951 break;
23952
23953 if (*p == ' ' || *p == '\0')
23954 {
23955 /* It's an object-like macro. */
23956 int name_len = p - body;
23957 char *name = savestring (body, name_len);
23958 const char *replacement;
23959
23960 if (*p == ' ')
23961 replacement = body + name_len + 1;
23962 else
23963 {
23964 dwarf2_macro_malformed_definition_complaint (body);
23965 replacement = body + name_len;
23966 }
23967
23968 macro_define_object (file, line, name, replacement);
23969
23970 xfree (name);
23971 }
23972 else if (*p == '(')
23973 {
23974 /* It's a function-like macro. */
23975 char *name = savestring (body, p - body);
23976 int argc = 0;
23977 int argv_size = 1;
23978 char **argv = XNEWVEC (char *, argv_size);
23979
23980 p++;
23981
23982 p = consume_improper_spaces (p, body);
23983
23984 /* Parse the formal argument list. */
23985 while (*p && *p != ')')
23986 {
23987 /* Find the extent of the current argument name. */
23988 const char *arg_start = p;
23989
23990 while (*p && *p != ',' && *p != ')' && *p != ' ')
23991 p++;
23992
23993 if (! *p || p == arg_start)
23994 dwarf2_macro_malformed_definition_complaint (body);
23995 else
23996 {
23997 /* Make sure argv has room for the new argument. */
23998 if (argc >= argv_size)
23999 {
24000 argv_size *= 2;
24001 argv = XRESIZEVEC (char *, argv, argv_size);
24002 }
24003
24004 argv[argc++] = savestring (arg_start, p - arg_start);
24005 }
24006
24007 p = consume_improper_spaces (p, body);
24008
24009 /* Consume the comma, if present. */
24010 if (*p == ',')
24011 {
24012 p++;
24013
24014 p = consume_improper_spaces (p, body);
24015 }
24016 }
24017
24018 if (*p == ')')
24019 {
24020 p++;
24021
24022 if (*p == ' ')
24023 /* Perfectly formed definition, no complaints. */
24024 macro_define_function (file, line, name,
24025 argc, (const char **) argv,
24026 p + 1);
24027 else if (*p == '\0')
24028 {
24029 /* Complain, but do define it. */
24030 dwarf2_macro_malformed_definition_complaint (body);
24031 macro_define_function (file, line, name,
24032 argc, (const char **) argv,
24033 p);
24034 }
24035 else
24036 /* Just complain. */
24037 dwarf2_macro_malformed_definition_complaint (body);
24038 }
24039 else
24040 /* Just complain. */
24041 dwarf2_macro_malformed_definition_complaint (body);
24042
24043 xfree (name);
24044 {
24045 int i;
24046
24047 for (i = 0; i < argc; i++)
24048 xfree (argv[i]);
24049 }
24050 xfree (argv);
24051 }
24052 else
24053 dwarf2_macro_malformed_definition_complaint (body);
24054 }
24055
24056 /* Skip some bytes from BYTES according to the form given in FORM.
24057 Returns the new pointer. */
24058
24059 static const gdb_byte *
24060 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24061 enum dwarf_form form,
24062 unsigned int offset_size,
24063 struct dwarf2_section_info *section)
24064 {
24065 unsigned int bytes_read;
24066
24067 switch (form)
24068 {
24069 case DW_FORM_data1:
24070 case DW_FORM_flag:
24071 ++bytes;
24072 break;
24073
24074 case DW_FORM_data2:
24075 bytes += 2;
24076 break;
24077
24078 case DW_FORM_data4:
24079 bytes += 4;
24080 break;
24081
24082 case DW_FORM_data8:
24083 bytes += 8;
24084 break;
24085
24086 case DW_FORM_data16:
24087 bytes += 16;
24088 break;
24089
24090 case DW_FORM_string:
24091 read_direct_string (abfd, bytes, &bytes_read);
24092 bytes += bytes_read;
24093 break;
24094
24095 case DW_FORM_sec_offset:
24096 case DW_FORM_strp:
24097 case DW_FORM_GNU_strp_alt:
24098 bytes += offset_size;
24099 break;
24100
24101 case DW_FORM_block:
24102 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24103 bytes += bytes_read;
24104 break;
24105
24106 case DW_FORM_block1:
24107 bytes += 1 + read_1_byte (abfd, bytes);
24108 break;
24109 case DW_FORM_block2:
24110 bytes += 2 + read_2_bytes (abfd, bytes);
24111 break;
24112 case DW_FORM_block4:
24113 bytes += 4 + read_4_bytes (abfd, bytes);
24114 break;
24115
24116 case DW_FORM_sdata:
24117 case DW_FORM_udata:
24118 case DW_FORM_GNU_addr_index:
24119 case DW_FORM_GNU_str_index:
24120 bytes = gdb_skip_leb128 (bytes, buffer_end);
24121 if (bytes == NULL)
24122 {
24123 dwarf2_section_buffer_overflow_complaint (section);
24124 return NULL;
24125 }
24126 break;
24127
24128 case DW_FORM_implicit_const:
24129 break;
24130
24131 default:
24132 {
24133 complaint (&symfile_complaints,
24134 _("invalid form 0x%x in `%s'"),
24135 form, get_section_name (section));
24136 return NULL;
24137 }
24138 }
24139
24140 return bytes;
24141 }
24142
24143 /* A helper for dwarf_decode_macros that handles skipping an unknown
24144 opcode. Returns an updated pointer to the macro data buffer; or,
24145 on error, issues a complaint and returns NULL. */
24146
24147 static const gdb_byte *
24148 skip_unknown_opcode (unsigned int opcode,
24149 const gdb_byte **opcode_definitions,
24150 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24151 bfd *abfd,
24152 unsigned int offset_size,
24153 struct dwarf2_section_info *section)
24154 {
24155 unsigned int bytes_read, i;
24156 unsigned long arg;
24157 const gdb_byte *defn;
24158
24159 if (opcode_definitions[opcode] == NULL)
24160 {
24161 complaint (&symfile_complaints,
24162 _("unrecognized DW_MACFINO opcode 0x%x"),
24163 opcode);
24164 return NULL;
24165 }
24166
24167 defn = opcode_definitions[opcode];
24168 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24169 defn += bytes_read;
24170
24171 for (i = 0; i < arg; ++i)
24172 {
24173 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24174 (enum dwarf_form) defn[i], offset_size,
24175 section);
24176 if (mac_ptr == NULL)
24177 {
24178 /* skip_form_bytes already issued the complaint. */
24179 return NULL;
24180 }
24181 }
24182
24183 return mac_ptr;
24184 }
24185
24186 /* A helper function which parses the header of a macro section.
24187 If the macro section is the extended (for now called "GNU") type,
24188 then this updates *OFFSET_SIZE. Returns a pointer to just after
24189 the header, or issues a complaint and returns NULL on error. */
24190
24191 static const gdb_byte *
24192 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24193 bfd *abfd,
24194 const gdb_byte *mac_ptr,
24195 unsigned int *offset_size,
24196 int section_is_gnu)
24197 {
24198 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24199
24200 if (section_is_gnu)
24201 {
24202 unsigned int version, flags;
24203
24204 version = read_2_bytes (abfd, mac_ptr);
24205 if (version != 4 && version != 5)
24206 {
24207 complaint (&symfile_complaints,
24208 _("unrecognized version `%d' in .debug_macro section"),
24209 version);
24210 return NULL;
24211 }
24212 mac_ptr += 2;
24213
24214 flags = read_1_byte (abfd, mac_ptr);
24215 ++mac_ptr;
24216 *offset_size = (flags & 1) ? 8 : 4;
24217
24218 if ((flags & 2) != 0)
24219 /* We don't need the line table offset. */
24220 mac_ptr += *offset_size;
24221
24222 /* Vendor opcode descriptions. */
24223 if ((flags & 4) != 0)
24224 {
24225 unsigned int i, count;
24226
24227 count = read_1_byte (abfd, mac_ptr);
24228 ++mac_ptr;
24229 for (i = 0; i < count; ++i)
24230 {
24231 unsigned int opcode, bytes_read;
24232 unsigned long arg;
24233
24234 opcode = read_1_byte (abfd, mac_ptr);
24235 ++mac_ptr;
24236 opcode_definitions[opcode] = mac_ptr;
24237 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24238 mac_ptr += bytes_read;
24239 mac_ptr += arg;
24240 }
24241 }
24242 }
24243
24244 return mac_ptr;
24245 }
24246
24247 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24248 including DW_MACRO_import. */
24249
24250 static void
24251 dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24252 bfd *abfd,
24253 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24254 struct macro_source_file *current_file,
24255 struct line_header *lh,
24256 struct dwarf2_section_info *section,
24257 int section_is_gnu, int section_is_dwz,
24258 unsigned int offset_size,
24259 htab_t include_hash)
24260 {
24261 struct objfile *objfile = dwarf2_per_objfile->objfile;
24262 enum dwarf_macro_record_type macinfo_type;
24263 int at_commandline;
24264 const gdb_byte *opcode_definitions[256];
24265
24266 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24267 &offset_size, section_is_gnu);
24268 if (mac_ptr == NULL)
24269 {
24270 /* We already issued a complaint. */
24271 return;
24272 }
24273
24274 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24275 GDB is still reading the definitions from command line. First
24276 DW_MACINFO_start_file will need to be ignored as it was already executed
24277 to create CURRENT_FILE for the main source holding also the command line
24278 definitions. On first met DW_MACINFO_start_file this flag is reset to
24279 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24280
24281 at_commandline = 1;
24282
24283 do
24284 {
24285 /* Do we at least have room for a macinfo type byte? */
24286 if (mac_ptr >= mac_end)
24287 {
24288 dwarf2_section_buffer_overflow_complaint (section);
24289 break;
24290 }
24291
24292 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24293 mac_ptr++;
24294
24295 /* Note that we rely on the fact that the corresponding GNU and
24296 DWARF constants are the same. */
24297 DIAGNOSTIC_PUSH
24298 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24299 switch (macinfo_type)
24300 {
24301 /* A zero macinfo type indicates the end of the macro
24302 information. */
24303 case 0:
24304 break;
24305
24306 case DW_MACRO_define:
24307 case DW_MACRO_undef:
24308 case DW_MACRO_define_strp:
24309 case DW_MACRO_undef_strp:
24310 case DW_MACRO_define_sup:
24311 case DW_MACRO_undef_sup:
24312 {
24313 unsigned int bytes_read;
24314 int line;
24315 const char *body;
24316 int is_define;
24317
24318 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24319 mac_ptr += bytes_read;
24320
24321 if (macinfo_type == DW_MACRO_define
24322 || macinfo_type == DW_MACRO_undef)
24323 {
24324 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24325 mac_ptr += bytes_read;
24326 }
24327 else
24328 {
24329 LONGEST str_offset;
24330
24331 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24332 mac_ptr += offset_size;
24333
24334 if (macinfo_type == DW_MACRO_define_sup
24335 || macinfo_type == DW_MACRO_undef_sup
24336 || section_is_dwz)
24337 {
24338 struct dwz_file *dwz
24339 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24340
24341 body = read_indirect_string_from_dwz (objfile,
24342 dwz, str_offset);
24343 }
24344 else
24345 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24346 abfd, str_offset);
24347 }
24348
24349 is_define = (macinfo_type == DW_MACRO_define
24350 || macinfo_type == DW_MACRO_define_strp
24351 || macinfo_type == DW_MACRO_define_sup);
24352 if (! current_file)
24353 {
24354 /* DWARF violation as no main source is present. */
24355 complaint (&symfile_complaints,
24356 _("debug info with no main source gives macro %s "
24357 "on line %d: %s"),
24358 is_define ? _("definition") : _("undefinition"),
24359 line, body);
24360 break;
24361 }
24362 if ((line == 0 && !at_commandline)
24363 || (line != 0 && at_commandline))
24364 complaint (&symfile_complaints,
24365 _("debug info gives %s macro %s with %s line %d: %s"),
24366 at_commandline ? _("command-line") : _("in-file"),
24367 is_define ? _("definition") : _("undefinition"),
24368 line == 0 ? _("zero") : _("non-zero"), line, body);
24369
24370 if (is_define)
24371 parse_macro_definition (current_file, line, body);
24372 else
24373 {
24374 gdb_assert (macinfo_type == DW_MACRO_undef
24375 || macinfo_type == DW_MACRO_undef_strp
24376 || macinfo_type == DW_MACRO_undef_sup);
24377 macro_undef (current_file, line, body);
24378 }
24379 }
24380 break;
24381
24382 case DW_MACRO_start_file:
24383 {
24384 unsigned int bytes_read;
24385 int line, file;
24386
24387 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24388 mac_ptr += bytes_read;
24389 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24390 mac_ptr += bytes_read;
24391
24392 if ((line == 0 && !at_commandline)
24393 || (line != 0 && at_commandline))
24394 complaint (&symfile_complaints,
24395 _("debug info gives source %d included "
24396 "from %s at %s line %d"),
24397 file, at_commandline ? _("command-line") : _("file"),
24398 line == 0 ? _("zero") : _("non-zero"), line);
24399
24400 if (at_commandline)
24401 {
24402 /* This DW_MACRO_start_file was executed in the
24403 pass one. */
24404 at_commandline = 0;
24405 }
24406 else
24407 current_file = macro_start_file (file, line, current_file, lh);
24408 }
24409 break;
24410
24411 case DW_MACRO_end_file:
24412 if (! current_file)
24413 complaint (&symfile_complaints,
24414 _("macro debug info has an unmatched "
24415 "`close_file' directive"));
24416 else
24417 {
24418 current_file = current_file->included_by;
24419 if (! current_file)
24420 {
24421 enum dwarf_macro_record_type next_type;
24422
24423 /* GCC circa March 2002 doesn't produce the zero
24424 type byte marking the end of the compilation
24425 unit. Complain if it's not there, but exit no
24426 matter what. */
24427
24428 /* Do we at least have room for a macinfo type byte? */
24429 if (mac_ptr >= mac_end)
24430 {
24431 dwarf2_section_buffer_overflow_complaint (section);
24432 return;
24433 }
24434
24435 /* We don't increment mac_ptr here, so this is just
24436 a look-ahead. */
24437 next_type
24438 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24439 mac_ptr);
24440 if (next_type != 0)
24441 complaint (&symfile_complaints,
24442 _("no terminating 0-type entry for "
24443 "macros in `.debug_macinfo' section"));
24444
24445 return;
24446 }
24447 }
24448 break;
24449
24450 case DW_MACRO_import:
24451 case DW_MACRO_import_sup:
24452 {
24453 LONGEST offset;
24454 void **slot;
24455 bfd *include_bfd = abfd;
24456 struct dwarf2_section_info *include_section = section;
24457 const gdb_byte *include_mac_end = mac_end;
24458 int is_dwz = section_is_dwz;
24459 const gdb_byte *new_mac_ptr;
24460
24461 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24462 mac_ptr += offset_size;
24463
24464 if (macinfo_type == DW_MACRO_import_sup)
24465 {
24466 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24467
24468 dwarf2_read_section (objfile, &dwz->macro);
24469
24470 include_section = &dwz->macro;
24471 include_bfd = get_section_bfd_owner (include_section);
24472 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24473 is_dwz = 1;
24474 }
24475
24476 new_mac_ptr = include_section->buffer + offset;
24477 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24478
24479 if (*slot != NULL)
24480 {
24481 /* This has actually happened; see
24482 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24483 complaint (&symfile_complaints,
24484 _("recursive DW_MACRO_import in "
24485 ".debug_macro section"));
24486 }
24487 else
24488 {
24489 *slot = (void *) new_mac_ptr;
24490
24491 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24492 include_bfd, new_mac_ptr,
24493 include_mac_end, current_file, lh,
24494 section, section_is_gnu, is_dwz,
24495 offset_size, include_hash);
24496
24497 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24498 }
24499 }
24500 break;
24501
24502 case DW_MACINFO_vendor_ext:
24503 if (!section_is_gnu)
24504 {
24505 unsigned int bytes_read;
24506
24507 /* This reads the constant, but since we don't recognize
24508 any vendor extensions, we ignore it. */
24509 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24510 mac_ptr += bytes_read;
24511 read_direct_string (abfd, mac_ptr, &bytes_read);
24512 mac_ptr += bytes_read;
24513
24514 /* We don't recognize any vendor extensions. */
24515 break;
24516 }
24517 /* FALLTHROUGH */
24518
24519 default:
24520 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24521 mac_ptr, mac_end, abfd, offset_size,
24522 section);
24523 if (mac_ptr == NULL)
24524 return;
24525 break;
24526 }
24527 DIAGNOSTIC_POP
24528 } while (macinfo_type != 0);
24529 }
24530
24531 static void
24532 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24533 int section_is_gnu)
24534 {
24535 struct dwarf2_per_objfile *dwarf2_per_objfile
24536 = cu->per_cu->dwarf2_per_objfile;
24537 struct objfile *objfile = dwarf2_per_objfile->objfile;
24538 struct line_header *lh = cu->line_header;
24539 bfd *abfd;
24540 const gdb_byte *mac_ptr, *mac_end;
24541 struct macro_source_file *current_file = 0;
24542 enum dwarf_macro_record_type macinfo_type;
24543 unsigned int offset_size = cu->header.offset_size;
24544 const gdb_byte *opcode_definitions[256];
24545 void **slot;
24546 struct dwarf2_section_info *section;
24547 const char *section_name;
24548
24549 if (cu->dwo_unit != NULL)
24550 {
24551 if (section_is_gnu)
24552 {
24553 section = &cu->dwo_unit->dwo_file->sections.macro;
24554 section_name = ".debug_macro.dwo";
24555 }
24556 else
24557 {
24558 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24559 section_name = ".debug_macinfo.dwo";
24560 }
24561 }
24562 else
24563 {
24564 if (section_is_gnu)
24565 {
24566 section = &dwarf2_per_objfile->macro;
24567 section_name = ".debug_macro";
24568 }
24569 else
24570 {
24571 section = &dwarf2_per_objfile->macinfo;
24572 section_name = ".debug_macinfo";
24573 }
24574 }
24575
24576 dwarf2_read_section (objfile, section);
24577 if (section->buffer == NULL)
24578 {
24579 complaint (&symfile_complaints, _("missing %s section"), section_name);
24580 return;
24581 }
24582 abfd = get_section_bfd_owner (section);
24583
24584 /* First pass: Find the name of the base filename.
24585 This filename is needed in order to process all macros whose definition
24586 (or undefinition) comes from the command line. These macros are defined
24587 before the first DW_MACINFO_start_file entry, and yet still need to be
24588 associated to the base file.
24589
24590 To determine the base file name, we scan the macro definitions until we
24591 reach the first DW_MACINFO_start_file entry. We then initialize
24592 CURRENT_FILE accordingly so that any macro definition found before the
24593 first DW_MACINFO_start_file can still be associated to the base file. */
24594
24595 mac_ptr = section->buffer + offset;
24596 mac_end = section->buffer + section->size;
24597
24598 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24599 &offset_size, section_is_gnu);
24600 if (mac_ptr == NULL)
24601 {
24602 /* We already issued a complaint. */
24603 return;
24604 }
24605
24606 do
24607 {
24608 /* Do we at least have room for a macinfo type byte? */
24609 if (mac_ptr >= mac_end)
24610 {
24611 /* Complaint is printed during the second pass as GDB will probably
24612 stop the first pass earlier upon finding
24613 DW_MACINFO_start_file. */
24614 break;
24615 }
24616
24617 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24618 mac_ptr++;
24619
24620 /* Note that we rely on the fact that the corresponding GNU and
24621 DWARF constants are the same. */
24622 DIAGNOSTIC_PUSH
24623 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24624 switch (macinfo_type)
24625 {
24626 /* A zero macinfo type indicates the end of the macro
24627 information. */
24628 case 0:
24629 break;
24630
24631 case DW_MACRO_define:
24632 case DW_MACRO_undef:
24633 /* Only skip the data by MAC_PTR. */
24634 {
24635 unsigned int bytes_read;
24636
24637 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24638 mac_ptr += bytes_read;
24639 read_direct_string (abfd, mac_ptr, &bytes_read);
24640 mac_ptr += bytes_read;
24641 }
24642 break;
24643
24644 case DW_MACRO_start_file:
24645 {
24646 unsigned int bytes_read;
24647 int line, file;
24648
24649 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24650 mac_ptr += bytes_read;
24651 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24652 mac_ptr += bytes_read;
24653
24654 current_file = macro_start_file (file, line, current_file, lh);
24655 }
24656 break;
24657
24658 case DW_MACRO_end_file:
24659 /* No data to skip by MAC_PTR. */
24660 break;
24661
24662 case DW_MACRO_define_strp:
24663 case DW_MACRO_undef_strp:
24664 case DW_MACRO_define_sup:
24665 case DW_MACRO_undef_sup:
24666 {
24667 unsigned int bytes_read;
24668
24669 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24670 mac_ptr += bytes_read;
24671 mac_ptr += offset_size;
24672 }
24673 break;
24674
24675 case DW_MACRO_import:
24676 case DW_MACRO_import_sup:
24677 /* Note that, according to the spec, a transparent include
24678 chain cannot call DW_MACRO_start_file. So, we can just
24679 skip this opcode. */
24680 mac_ptr += offset_size;
24681 break;
24682
24683 case DW_MACINFO_vendor_ext:
24684 /* Only skip the data by MAC_PTR. */
24685 if (!section_is_gnu)
24686 {
24687 unsigned int bytes_read;
24688
24689 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24690 mac_ptr += bytes_read;
24691 read_direct_string (abfd, mac_ptr, &bytes_read);
24692 mac_ptr += bytes_read;
24693 }
24694 /* FALLTHROUGH */
24695
24696 default:
24697 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24698 mac_ptr, mac_end, abfd, offset_size,
24699 section);
24700 if (mac_ptr == NULL)
24701 return;
24702 break;
24703 }
24704 DIAGNOSTIC_POP
24705 } while (macinfo_type != 0 && current_file == NULL);
24706
24707 /* Second pass: Process all entries.
24708
24709 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24710 command-line macro definitions/undefinitions. This flag is unset when we
24711 reach the first DW_MACINFO_start_file entry. */
24712
24713 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24714 htab_eq_pointer,
24715 NULL, xcalloc, xfree));
24716 mac_ptr = section->buffer + offset;
24717 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24718 *slot = (void *) mac_ptr;
24719 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24720 abfd, mac_ptr, mac_end,
24721 current_file, lh, section,
24722 section_is_gnu, 0, offset_size,
24723 include_hash.get ());
24724 }
24725
24726 /* Check if the attribute's form is a DW_FORM_block*
24727 if so return true else false. */
24728
24729 static int
24730 attr_form_is_block (const struct attribute *attr)
24731 {
24732 return (attr == NULL ? 0 :
24733 attr->form == DW_FORM_block1
24734 || attr->form == DW_FORM_block2
24735 || attr->form == DW_FORM_block4
24736 || attr->form == DW_FORM_block
24737 || attr->form == DW_FORM_exprloc);
24738 }
24739
24740 /* Return non-zero if ATTR's value is a section offset --- classes
24741 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24742 You may use DW_UNSND (attr) to retrieve such offsets.
24743
24744 Section 7.5.4, "Attribute Encodings", explains that no attribute
24745 may have a value that belongs to more than one of these classes; it
24746 would be ambiguous if we did, because we use the same forms for all
24747 of them. */
24748
24749 static int
24750 attr_form_is_section_offset (const struct attribute *attr)
24751 {
24752 return (attr->form == DW_FORM_data4
24753 || attr->form == DW_FORM_data8
24754 || attr->form == DW_FORM_sec_offset);
24755 }
24756
24757 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24758 zero otherwise. When this function returns true, you can apply
24759 dwarf2_get_attr_constant_value to it.
24760
24761 However, note that for some attributes you must check
24762 attr_form_is_section_offset before using this test. DW_FORM_data4
24763 and DW_FORM_data8 are members of both the constant class, and of
24764 the classes that contain offsets into other debug sections
24765 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24766 that, if an attribute's can be either a constant or one of the
24767 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
24768 taken as section offsets, not constants.
24769
24770 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24771 cannot handle that. */
24772
24773 static int
24774 attr_form_is_constant (const struct attribute *attr)
24775 {
24776 switch (attr->form)
24777 {
24778 case DW_FORM_sdata:
24779 case DW_FORM_udata:
24780 case DW_FORM_data1:
24781 case DW_FORM_data2:
24782 case DW_FORM_data4:
24783 case DW_FORM_data8:
24784 case DW_FORM_implicit_const:
24785 return 1;
24786 default:
24787 return 0;
24788 }
24789 }
24790
24791
24792 /* DW_ADDR is always stored already as sect_offset; despite for the forms
24793 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24794
24795 static int
24796 attr_form_is_ref (const struct attribute *attr)
24797 {
24798 switch (attr->form)
24799 {
24800 case DW_FORM_ref_addr:
24801 case DW_FORM_ref1:
24802 case DW_FORM_ref2:
24803 case DW_FORM_ref4:
24804 case DW_FORM_ref8:
24805 case DW_FORM_ref_udata:
24806 case DW_FORM_GNU_ref_alt:
24807 return 1;
24808 default:
24809 return 0;
24810 }
24811 }
24812
24813 /* Return the .debug_loc section to use for CU.
24814 For DWO files use .debug_loc.dwo. */
24815
24816 static struct dwarf2_section_info *
24817 cu_debug_loc_section (struct dwarf2_cu *cu)
24818 {
24819 struct dwarf2_per_objfile *dwarf2_per_objfile
24820 = cu->per_cu->dwarf2_per_objfile;
24821
24822 if (cu->dwo_unit)
24823 {
24824 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24825
24826 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24827 }
24828 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24829 : &dwarf2_per_objfile->loc);
24830 }
24831
24832 /* A helper function that fills in a dwarf2_loclist_baton. */
24833
24834 static void
24835 fill_in_loclist_baton (struct dwarf2_cu *cu,
24836 struct dwarf2_loclist_baton *baton,
24837 const struct attribute *attr)
24838 {
24839 struct dwarf2_per_objfile *dwarf2_per_objfile
24840 = cu->per_cu->dwarf2_per_objfile;
24841 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24842
24843 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
24844
24845 baton->per_cu = cu->per_cu;
24846 gdb_assert (baton->per_cu);
24847 /* We don't know how long the location list is, but make sure we
24848 don't run off the edge of the section. */
24849 baton->size = section->size - DW_UNSND (attr);
24850 baton->data = section->buffer + DW_UNSND (attr);
24851 baton->base_address = cu->base_address;
24852 baton->from_dwo = cu->dwo_unit != NULL;
24853 }
24854
24855 static void
24856 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24857 struct dwarf2_cu *cu, int is_block)
24858 {
24859 struct dwarf2_per_objfile *dwarf2_per_objfile
24860 = cu->per_cu->dwarf2_per_objfile;
24861 struct objfile *objfile = dwarf2_per_objfile->objfile;
24862 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24863
24864 if (attr_form_is_section_offset (attr)
24865 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24866 the section. If so, fall through to the complaint in the
24867 other branch. */
24868 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24869 {
24870 struct dwarf2_loclist_baton *baton;
24871
24872 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24873
24874 fill_in_loclist_baton (cu, baton, attr);
24875
24876 if (cu->base_known == 0)
24877 complaint (&symfile_complaints,
24878 _("Location list used without "
24879 "specifying the CU base address."));
24880
24881 SYMBOL_ACLASS_INDEX (sym) = (is_block
24882 ? dwarf2_loclist_block_index
24883 : dwarf2_loclist_index);
24884 SYMBOL_LOCATION_BATON (sym) = baton;
24885 }
24886 else
24887 {
24888 struct dwarf2_locexpr_baton *baton;
24889
24890 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24891 baton->per_cu = cu->per_cu;
24892 gdb_assert (baton->per_cu);
24893
24894 if (attr_form_is_block (attr))
24895 {
24896 /* Note that we're just copying the block's data pointer
24897 here, not the actual data. We're still pointing into the
24898 info_buffer for SYM's objfile; right now we never release
24899 that buffer, but when we do clean up properly this may
24900 need to change. */
24901 baton->size = DW_BLOCK (attr)->size;
24902 baton->data = DW_BLOCK (attr)->data;
24903 }
24904 else
24905 {
24906 dwarf2_invalid_attrib_class_complaint ("location description",
24907 SYMBOL_NATURAL_NAME (sym));
24908 baton->size = 0;
24909 }
24910
24911 SYMBOL_ACLASS_INDEX (sym) = (is_block
24912 ? dwarf2_locexpr_block_index
24913 : dwarf2_locexpr_index);
24914 SYMBOL_LOCATION_BATON (sym) = baton;
24915 }
24916 }
24917
24918 /* Return the OBJFILE associated with the compilation unit CU. If CU
24919 came from a separate debuginfo file, then the master objfile is
24920 returned. */
24921
24922 struct objfile *
24923 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24924 {
24925 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24926
24927 /* Return the master objfile, so that we can report and look up the
24928 correct file containing this variable. */
24929 if (objfile->separate_debug_objfile_backlink)
24930 objfile = objfile->separate_debug_objfile_backlink;
24931
24932 return objfile;
24933 }
24934
24935 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24936 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24937 CU_HEADERP first. */
24938
24939 static const struct comp_unit_head *
24940 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24941 struct dwarf2_per_cu_data *per_cu)
24942 {
24943 const gdb_byte *info_ptr;
24944
24945 if (per_cu->cu)
24946 return &per_cu->cu->header;
24947
24948 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24949
24950 memset (cu_headerp, 0, sizeof (*cu_headerp));
24951 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24952 rcuh_kind::COMPILE);
24953
24954 return cu_headerp;
24955 }
24956
24957 /* Return the address size given in the compilation unit header for CU. */
24958
24959 int
24960 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24961 {
24962 struct comp_unit_head cu_header_local;
24963 const struct comp_unit_head *cu_headerp;
24964
24965 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24966
24967 return cu_headerp->addr_size;
24968 }
24969
24970 /* Return the offset size given in the compilation unit header for CU. */
24971
24972 int
24973 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24974 {
24975 struct comp_unit_head cu_header_local;
24976 const struct comp_unit_head *cu_headerp;
24977
24978 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24979
24980 return cu_headerp->offset_size;
24981 }
24982
24983 /* See its dwarf2loc.h declaration. */
24984
24985 int
24986 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24987 {
24988 struct comp_unit_head cu_header_local;
24989 const struct comp_unit_head *cu_headerp;
24990
24991 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24992
24993 if (cu_headerp->version == 2)
24994 return cu_headerp->addr_size;
24995 else
24996 return cu_headerp->offset_size;
24997 }
24998
24999 /* Return the text offset of the CU. The returned offset comes from
25000 this CU's objfile. If this objfile came from a separate debuginfo
25001 file, then the offset may be different from the corresponding
25002 offset in the parent objfile. */
25003
25004 CORE_ADDR
25005 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25006 {
25007 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25008
25009 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25010 }
25011
25012 /* Return DWARF version number of PER_CU. */
25013
25014 short
25015 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25016 {
25017 return per_cu->dwarf_version;
25018 }
25019
25020 /* Locate the .debug_info compilation unit from CU's objfile which contains
25021 the DIE at OFFSET. Raises an error on failure. */
25022
25023 static struct dwarf2_per_cu_data *
25024 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25025 unsigned int offset_in_dwz,
25026 struct dwarf2_per_objfile *dwarf2_per_objfile)
25027 {
25028 struct dwarf2_per_cu_data *this_cu;
25029 int low, high;
25030 const sect_offset *cu_off;
25031
25032 low = 0;
25033 high = dwarf2_per_objfile->n_comp_units - 1;
25034 while (high > low)
25035 {
25036 struct dwarf2_per_cu_data *mid_cu;
25037 int mid = low + (high - low) / 2;
25038
25039 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25040 cu_off = &mid_cu->sect_off;
25041 if (mid_cu->is_dwz > offset_in_dwz
25042 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
25043 high = mid;
25044 else
25045 low = mid + 1;
25046 }
25047 gdb_assert (low == high);
25048 this_cu = dwarf2_per_objfile->all_comp_units[low];
25049 cu_off = &this_cu->sect_off;
25050 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
25051 {
25052 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25053 error (_("Dwarf Error: could not find partial DIE containing "
25054 "offset 0x%x [in module %s]"),
25055 to_underlying (sect_off),
25056 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25057
25058 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25059 <= sect_off);
25060 return dwarf2_per_objfile->all_comp_units[low-1];
25061 }
25062 else
25063 {
25064 this_cu = dwarf2_per_objfile->all_comp_units[low];
25065 if (low == dwarf2_per_objfile->n_comp_units - 1
25066 && sect_off >= this_cu->sect_off + this_cu->length)
25067 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
25068 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25069 return this_cu;
25070 }
25071 }
25072
25073 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25074
25075 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25076 : per_cu (per_cu_),
25077 mark (0),
25078 has_loclist (0),
25079 checked_producer (0),
25080 producer_is_gxx_lt_4_6 (0),
25081 producer_is_gcc_lt_4_3 (0),
25082 producer_is_icc_lt_14 (0),
25083 processing_has_namespace_info (0)
25084 {
25085 per_cu->cu = this;
25086 }
25087
25088 /* Destroy a dwarf2_cu. */
25089
25090 dwarf2_cu::~dwarf2_cu ()
25091 {
25092 per_cu->cu = NULL;
25093 }
25094
25095 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25096
25097 static void
25098 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25099 enum language pretend_language)
25100 {
25101 struct attribute *attr;
25102
25103 /* Set the language we're debugging. */
25104 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25105 if (attr)
25106 set_cu_language (DW_UNSND (attr), cu);
25107 else
25108 {
25109 cu->language = pretend_language;
25110 cu->language_defn = language_def (cu->language);
25111 }
25112
25113 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25114 }
25115
25116 /* Free all cached compilation units. */
25117
25118 static void
25119 free_cached_comp_units (void *data)
25120 {
25121 struct dwarf2_per_objfile *dwarf2_per_objfile
25122 = (struct dwarf2_per_objfile *) data;
25123
25124 dwarf2_per_objfile->free_cached_comp_units ();
25125 }
25126
25127 /* Increase the age counter on each cached compilation unit, and free
25128 any that are too old. */
25129
25130 static void
25131 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25132 {
25133 struct dwarf2_per_cu_data *per_cu, **last_chain;
25134
25135 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25136 per_cu = dwarf2_per_objfile->read_in_chain;
25137 while (per_cu != NULL)
25138 {
25139 per_cu->cu->last_used ++;
25140 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25141 dwarf2_mark (per_cu->cu);
25142 per_cu = per_cu->cu->read_in_chain;
25143 }
25144
25145 per_cu = dwarf2_per_objfile->read_in_chain;
25146 last_chain = &dwarf2_per_objfile->read_in_chain;
25147 while (per_cu != NULL)
25148 {
25149 struct dwarf2_per_cu_data *next_cu;
25150
25151 next_cu = per_cu->cu->read_in_chain;
25152
25153 if (!per_cu->cu->mark)
25154 {
25155 delete per_cu->cu;
25156 *last_chain = next_cu;
25157 }
25158 else
25159 last_chain = &per_cu->cu->read_in_chain;
25160
25161 per_cu = next_cu;
25162 }
25163 }
25164
25165 /* Remove a single compilation unit from the cache. */
25166
25167 static void
25168 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25169 {
25170 struct dwarf2_per_cu_data *per_cu, **last_chain;
25171 struct dwarf2_per_objfile *dwarf2_per_objfile
25172 = target_per_cu->dwarf2_per_objfile;
25173
25174 per_cu = dwarf2_per_objfile->read_in_chain;
25175 last_chain = &dwarf2_per_objfile->read_in_chain;
25176 while (per_cu != NULL)
25177 {
25178 struct dwarf2_per_cu_data *next_cu;
25179
25180 next_cu = per_cu->cu->read_in_chain;
25181
25182 if (per_cu == target_per_cu)
25183 {
25184 delete per_cu->cu;
25185 per_cu->cu = NULL;
25186 *last_chain = next_cu;
25187 break;
25188 }
25189 else
25190 last_chain = &per_cu->cu->read_in_chain;
25191
25192 per_cu = next_cu;
25193 }
25194 }
25195
25196 /* Release all extra memory associated with OBJFILE. */
25197
25198 void
25199 dwarf2_free_objfile (struct objfile *objfile)
25200 {
25201 struct dwarf2_per_objfile *dwarf2_per_objfile
25202 = get_dwarf2_per_objfile (objfile);
25203
25204 delete dwarf2_per_objfile;
25205 }
25206
25207 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25208 We store these in a hash table separate from the DIEs, and preserve them
25209 when the DIEs are flushed out of cache.
25210
25211 The CU "per_cu" pointer is needed because offset alone is not enough to
25212 uniquely identify the type. A file may have multiple .debug_types sections,
25213 or the type may come from a DWO file. Furthermore, while it's more logical
25214 to use per_cu->section+offset, with Fission the section with the data is in
25215 the DWO file but we don't know that section at the point we need it.
25216 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25217 because we can enter the lookup routine, get_die_type_at_offset, from
25218 outside this file, and thus won't necessarily have PER_CU->cu.
25219 Fortunately, PER_CU is stable for the life of the objfile. */
25220
25221 struct dwarf2_per_cu_offset_and_type
25222 {
25223 const struct dwarf2_per_cu_data *per_cu;
25224 sect_offset sect_off;
25225 struct type *type;
25226 };
25227
25228 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25229
25230 static hashval_t
25231 per_cu_offset_and_type_hash (const void *item)
25232 {
25233 const struct dwarf2_per_cu_offset_and_type *ofs
25234 = (const struct dwarf2_per_cu_offset_and_type *) item;
25235
25236 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25237 }
25238
25239 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25240
25241 static int
25242 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25243 {
25244 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25245 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25246 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25247 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25248
25249 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25250 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25251 }
25252
25253 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25254 table if necessary. For convenience, return TYPE.
25255
25256 The DIEs reading must have careful ordering to:
25257 * Not cause infite loops trying to read in DIEs as a prerequisite for
25258 reading current DIE.
25259 * Not trying to dereference contents of still incompletely read in types
25260 while reading in other DIEs.
25261 * Enable referencing still incompletely read in types just by a pointer to
25262 the type without accessing its fields.
25263
25264 Therefore caller should follow these rules:
25265 * Try to fetch any prerequisite types we may need to build this DIE type
25266 before building the type and calling set_die_type.
25267 * After building type call set_die_type for current DIE as soon as
25268 possible before fetching more types to complete the current type.
25269 * Make the type as complete as possible before fetching more types. */
25270
25271 static struct type *
25272 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25273 {
25274 struct dwarf2_per_objfile *dwarf2_per_objfile
25275 = cu->per_cu->dwarf2_per_objfile;
25276 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25277 struct objfile *objfile = dwarf2_per_objfile->objfile;
25278 struct attribute *attr;
25279 struct dynamic_prop prop;
25280
25281 /* For Ada types, make sure that the gnat-specific data is always
25282 initialized (if not already set). There are a few types where
25283 we should not be doing so, because the type-specific area is
25284 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25285 where the type-specific area is used to store the floatformat).
25286 But this is not a problem, because the gnat-specific information
25287 is actually not needed for these types. */
25288 if (need_gnat_info (cu)
25289 && TYPE_CODE (type) != TYPE_CODE_FUNC
25290 && TYPE_CODE (type) != TYPE_CODE_FLT
25291 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25292 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25293 && TYPE_CODE (type) != TYPE_CODE_METHOD
25294 && !HAVE_GNAT_AUX_INFO (type))
25295 INIT_GNAT_SPECIFIC (type);
25296
25297 /* Read DW_AT_allocated and set in type. */
25298 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25299 if (attr_form_is_block (attr))
25300 {
25301 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25302 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25303 }
25304 else if (attr != NULL)
25305 {
25306 complaint (&symfile_complaints,
25307 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
25308 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25309 to_underlying (die->sect_off));
25310 }
25311
25312 /* Read DW_AT_associated and set in type. */
25313 attr = dwarf2_attr (die, DW_AT_associated, cu);
25314 if (attr_form_is_block (attr))
25315 {
25316 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25317 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25318 }
25319 else if (attr != NULL)
25320 {
25321 complaint (&symfile_complaints,
25322 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
25323 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25324 to_underlying (die->sect_off));
25325 }
25326
25327 /* Read DW_AT_data_location and set in type. */
25328 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25329 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25330 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25331
25332 if (dwarf2_per_objfile->die_type_hash == NULL)
25333 {
25334 dwarf2_per_objfile->die_type_hash =
25335 htab_create_alloc_ex (127,
25336 per_cu_offset_and_type_hash,
25337 per_cu_offset_and_type_eq,
25338 NULL,
25339 &objfile->objfile_obstack,
25340 hashtab_obstack_allocate,
25341 dummy_obstack_deallocate);
25342 }
25343
25344 ofs.per_cu = cu->per_cu;
25345 ofs.sect_off = die->sect_off;
25346 ofs.type = type;
25347 slot = (struct dwarf2_per_cu_offset_and_type **)
25348 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25349 if (*slot)
25350 complaint (&symfile_complaints,
25351 _("A problem internal to GDB: DIE 0x%x has type already set"),
25352 to_underlying (die->sect_off));
25353 *slot = XOBNEW (&objfile->objfile_obstack,
25354 struct dwarf2_per_cu_offset_and_type);
25355 **slot = ofs;
25356 return type;
25357 }
25358
25359 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25360 or return NULL if the die does not have a saved type. */
25361
25362 static struct type *
25363 get_die_type_at_offset (sect_offset sect_off,
25364 struct dwarf2_per_cu_data *per_cu)
25365 {
25366 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25367 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25368
25369 if (dwarf2_per_objfile->die_type_hash == NULL)
25370 return NULL;
25371
25372 ofs.per_cu = per_cu;
25373 ofs.sect_off = sect_off;
25374 slot = ((struct dwarf2_per_cu_offset_and_type *)
25375 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25376 if (slot)
25377 return slot->type;
25378 else
25379 return NULL;
25380 }
25381
25382 /* Look up the type for DIE in CU in die_type_hash,
25383 or return NULL if DIE does not have a saved type. */
25384
25385 static struct type *
25386 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25387 {
25388 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25389 }
25390
25391 /* Add a dependence relationship from CU to REF_PER_CU. */
25392
25393 static void
25394 dwarf2_add_dependence (struct dwarf2_cu *cu,
25395 struct dwarf2_per_cu_data *ref_per_cu)
25396 {
25397 void **slot;
25398
25399 if (cu->dependencies == NULL)
25400 cu->dependencies
25401 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25402 NULL, &cu->comp_unit_obstack,
25403 hashtab_obstack_allocate,
25404 dummy_obstack_deallocate);
25405
25406 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25407 if (*slot == NULL)
25408 *slot = ref_per_cu;
25409 }
25410
25411 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25412 Set the mark field in every compilation unit in the
25413 cache that we must keep because we are keeping CU. */
25414
25415 static int
25416 dwarf2_mark_helper (void **slot, void *data)
25417 {
25418 struct dwarf2_per_cu_data *per_cu;
25419
25420 per_cu = (struct dwarf2_per_cu_data *) *slot;
25421
25422 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25423 reading of the chain. As such dependencies remain valid it is not much
25424 useful to track and undo them during QUIT cleanups. */
25425 if (per_cu->cu == NULL)
25426 return 1;
25427
25428 if (per_cu->cu->mark)
25429 return 1;
25430 per_cu->cu->mark = 1;
25431
25432 if (per_cu->cu->dependencies != NULL)
25433 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25434
25435 return 1;
25436 }
25437
25438 /* Set the mark field in CU and in every other compilation unit in the
25439 cache that we must keep because we are keeping CU. */
25440
25441 static void
25442 dwarf2_mark (struct dwarf2_cu *cu)
25443 {
25444 if (cu->mark)
25445 return;
25446 cu->mark = 1;
25447 if (cu->dependencies != NULL)
25448 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25449 }
25450
25451 static void
25452 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25453 {
25454 while (per_cu)
25455 {
25456 per_cu->cu->mark = 0;
25457 per_cu = per_cu->cu->read_in_chain;
25458 }
25459 }
25460
25461 /* Trivial hash function for partial_die_info: the hash value of a DIE
25462 is its offset in .debug_info for this objfile. */
25463
25464 static hashval_t
25465 partial_die_hash (const void *item)
25466 {
25467 const struct partial_die_info *part_die
25468 = (const struct partial_die_info *) item;
25469
25470 return to_underlying (part_die->sect_off);
25471 }
25472
25473 /* Trivial comparison function for partial_die_info structures: two DIEs
25474 are equal if they have the same offset. */
25475
25476 static int
25477 partial_die_eq (const void *item_lhs, const void *item_rhs)
25478 {
25479 const struct partial_die_info *part_die_lhs
25480 = (const struct partial_die_info *) item_lhs;
25481 const struct partial_die_info *part_die_rhs
25482 = (const struct partial_die_info *) item_rhs;
25483
25484 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25485 }
25486
25487 static struct cmd_list_element *set_dwarf_cmdlist;
25488 static struct cmd_list_element *show_dwarf_cmdlist;
25489
25490 static void
25491 set_dwarf_cmd (const char *args, int from_tty)
25492 {
25493 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25494 gdb_stdout);
25495 }
25496
25497 static void
25498 show_dwarf_cmd (const char *args, int from_tty)
25499 {
25500 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25501 }
25502
25503 /* The "save gdb-index" command. */
25504
25505 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25506 error checking. */
25507
25508 static void
25509 file_write (FILE *file, const void *data, size_t size)
25510 {
25511 if (fwrite (data, 1, size, file) != size)
25512 error (_("couldn't data write to file"));
25513 }
25514
25515 /* Write the contents of VEC to FILE, with error checking. */
25516
25517 template<typename Elem, typename Alloc>
25518 static void
25519 file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25520 {
25521 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25522 }
25523
25524 /* In-memory buffer to prepare data to be written later to a file. */
25525 class data_buf
25526 {
25527 public:
25528 /* Copy DATA to the end of the buffer. */
25529 template<typename T>
25530 void append_data (const T &data)
25531 {
25532 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25533 reinterpret_cast<const gdb_byte *> (&data + 1),
25534 grow (sizeof (data)));
25535 }
25536
25537 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25538 terminating zero is appended too. */
25539 void append_cstr0 (const char *cstr)
25540 {
25541 const size_t size = strlen (cstr) + 1;
25542 std::copy (cstr, cstr + size, grow (size));
25543 }
25544
25545 /* Store INPUT as ULEB128 to the end of buffer. */
25546 void append_unsigned_leb128 (ULONGEST input)
25547 {
25548 for (;;)
25549 {
25550 gdb_byte output = input & 0x7f;
25551 input >>= 7;
25552 if (input)
25553 output |= 0x80;
25554 append_data (output);
25555 if (input == 0)
25556 break;
25557 }
25558 }
25559
25560 /* Accept a host-format integer in VAL and append it to the buffer
25561 as a target-format integer which is LEN bytes long. */
25562 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25563 {
25564 ::store_unsigned_integer (grow (len), len, byte_order, val);
25565 }
25566
25567 /* Return the size of the buffer. */
25568 size_t size () const
25569 {
25570 return m_vec.size ();
25571 }
25572
25573 /* Return true iff the buffer is empty. */
25574 bool empty () const
25575 {
25576 return m_vec.empty ();
25577 }
25578
25579 /* Write the buffer to FILE. */
25580 void file_write (FILE *file) const
25581 {
25582 ::file_write (file, m_vec);
25583 }
25584
25585 private:
25586 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25587 the start of the new block. */
25588 gdb_byte *grow (size_t size)
25589 {
25590 m_vec.resize (m_vec.size () + size);
25591 return &*m_vec.end () - size;
25592 }
25593
25594 gdb::byte_vector m_vec;
25595 };
25596
25597 /* An entry in the symbol table. */
25598 struct symtab_index_entry
25599 {
25600 /* The name of the symbol. */
25601 const char *name;
25602 /* The offset of the name in the constant pool. */
25603 offset_type index_offset;
25604 /* A sorted vector of the indices of all the CUs that hold an object
25605 of this name. */
25606 std::vector<offset_type> cu_indices;
25607 };
25608
25609 /* The symbol table. This is a power-of-2-sized hash table. */
25610 struct mapped_symtab
25611 {
25612 mapped_symtab ()
25613 {
25614 data.resize (1024);
25615 }
25616
25617 offset_type n_elements = 0;
25618 std::vector<symtab_index_entry> data;
25619 };
25620
25621 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
25622 the slot.
25623
25624 Function is used only during write_hash_table so no index format backward
25625 compatibility is needed. */
25626
25627 static symtab_index_entry &
25628 find_slot (struct mapped_symtab *symtab, const char *name)
25629 {
25630 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
25631
25632 index = hash & (symtab->data.size () - 1);
25633 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
25634
25635 for (;;)
25636 {
25637 if (symtab->data[index].name == NULL
25638 || strcmp (name, symtab->data[index].name) == 0)
25639 return symtab->data[index];
25640 index = (index + step) & (symtab->data.size () - 1);
25641 }
25642 }
25643
25644 /* Expand SYMTAB's hash table. */
25645
25646 static void
25647 hash_expand (struct mapped_symtab *symtab)
25648 {
25649 auto old_entries = std::move (symtab->data);
25650
25651 symtab->data.clear ();
25652 symtab->data.resize (old_entries.size () * 2);
25653
25654 for (auto &it : old_entries)
25655 if (it.name != NULL)
25656 {
25657 auto &ref = find_slot (symtab, it.name);
25658 ref = std::move (it);
25659 }
25660 }
25661
25662 /* Add an entry to SYMTAB. NAME is the name of the symbol.
25663 CU_INDEX is the index of the CU in which the symbol appears.
25664 IS_STATIC is one if the symbol is static, otherwise zero (global). */
25665
25666 static void
25667 add_index_entry (struct mapped_symtab *symtab, const char *name,
25668 int is_static, gdb_index_symbol_kind kind,
25669 offset_type cu_index)
25670 {
25671 offset_type cu_index_and_attrs;
25672
25673 ++symtab->n_elements;
25674 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
25675 hash_expand (symtab);
25676
25677 symtab_index_entry &slot = find_slot (symtab, name);
25678 if (slot.name == NULL)
25679 {
25680 slot.name = name;
25681 /* index_offset is set later. */
25682 }
25683
25684 cu_index_and_attrs = 0;
25685 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
25686 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
25687 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
25688
25689 /* We don't want to record an index value twice as we want to avoid the
25690 duplication.
25691 We process all global symbols and then all static symbols
25692 (which would allow us to avoid the duplication by only having to check
25693 the last entry pushed), but a symbol could have multiple kinds in one CU.
25694 To keep things simple we don't worry about the duplication here and
25695 sort and uniqufy the list after we've processed all symbols. */
25696 slot.cu_indices.push_back (cu_index_and_attrs);
25697 }
25698
25699 /* Sort and remove duplicates of all symbols' cu_indices lists. */
25700
25701 static void
25702 uniquify_cu_indices (struct mapped_symtab *symtab)
25703 {
25704 for (auto &entry : symtab->data)
25705 {
25706 if (entry.name != NULL && !entry.cu_indices.empty ())
25707 {
25708 auto &cu_indices = entry.cu_indices;
25709 std::sort (cu_indices.begin (), cu_indices.end ());
25710 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
25711 cu_indices.erase (from, cu_indices.end ());
25712 }
25713 }
25714 }
25715
25716 /* A form of 'const char *' suitable for container keys. Only the
25717 pointer is stored. The strings themselves are compared, not the
25718 pointers. */
25719 class c_str_view
25720 {
25721 public:
25722 c_str_view (const char *cstr)
25723 : m_cstr (cstr)
25724 {}
25725
25726 bool operator== (const c_str_view &other) const
25727 {
25728 return strcmp (m_cstr, other.m_cstr) == 0;
25729 }
25730
25731 /* Return the underlying C string. Note, the returned string is
25732 only a reference with lifetime of this object. */
25733 const char *c_str () const
25734 {
25735 return m_cstr;
25736 }
25737
25738 private:
25739 friend class c_str_view_hasher;
25740 const char *const m_cstr;
25741 };
25742
25743 /* A std::unordered_map::hasher for c_str_view that uses the right
25744 hash function for strings in a mapped index. */
25745 class c_str_view_hasher
25746 {
25747 public:
25748 size_t operator () (const c_str_view &x) const
25749 {
25750 return mapped_index_string_hash (INT_MAX, x.m_cstr);
25751 }
25752 };
25753
25754 /* A std::unordered_map::hasher for std::vector<>. */
25755 template<typename T>
25756 class vector_hasher
25757 {
25758 public:
25759 size_t operator () (const std::vector<T> &key) const
25760 {
25761 return iterative_hash (key.data (),
25762 sizeof (key.front ()) * key.size (), 0);
25763 }
25764 };
25765
25766 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
25767 constant pool entries going into the data buffer CPOOL. */
25768
25769 static void
25770 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
25771 {
25772 {
25773 /* Elements are sorted vectors of the indices of all the CUs that
25774 hold an object of this name. */
25775 std::unordered_map<std::vector<offset_type>, offset_type,
25776 vector_hasher<offset_type>>
25777 symbol_hash_table;
25778
25779 /* We add all the index vectors to the constant pool first, to
25780 ensure alignment is ok. */
25781 for (symtab_index_entry &entry : symtab->data)
25782 {
25783 if (entry.name == NULL)
25784 continue;
25785 gdb_assert (entry.index_offset == 0);
25786
25787 /* Finding before inserting is faster than always trying to
25788 insert, because inserting always allocates a node, does the
25789 lookup, and then destroys the new node if another node
25790 already had the same key. C++17 try_emplace will avoid
25791 this. */
25792 const auto found
25793 = symbol_hash_table.find (entry.cu_indices);
25794 if (found != symbol_hash_table.end ())
25795 {
25796 entry.index_offset = found->second;
25797 continue;
25798 }
25799
25800 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
25801 entry.index_offset = cpool.size ();
25802 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
25803 for (const auto index : entry.cu_indices)
25804 cpool.append_data (MAYBE_SWAP (index));
25805 }
25806 }
25807
25808 /* Now write out the hash table. */
25809 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
25810 for (const auto &entry : symtab->data)
25811 {
25812 offset_type str_off, vec_off;
25813
25814 if (entry.name != NULL)
25815 {
25816 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
25817 if (insertpair.second)
25818 cpool.append_cstr0 (entry.name);
25819 str_off = insertpair.first->second;
25820 vec_off = entry.index_offset;
25821 }
25822 else
25823 {
25824 /* While 0 is a valid constant pool index, it is not valid
25825 to have 0 for both offsets. */
25826 str_off = 0;
25827 vec_off = 0;
25828 }
25829
25830 output.append_data (MAYBE_SWAP (str_off));
25831 output.append_data (MAYBE_SWAP (vec_off));
25832 }
25833 }
25834
25835 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
25836
25837 /* Helper struct for building the address table. */
25838 struct addrmap_index_data
25839 {
25840 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
25841 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
25842 {}
25843
25844 struct objfile *objfile;
25845 data_buf &addr_vec;
25846 psym_index_map &cu_index_htab;
25847
25848 /* Non-zero if the previous_* fields are valid.
25849 We can't write an entry until we see the next entry (since it is only then
25850 that we know the end of the entry). */
25851 int previous_valid;
25852 /* Index of the CU in the table of all CUs in the index file. */
25853 unsigned int previous_cu_index;
25854 /* Start address of the CU. */
25855 CORE_ADDR previous_cu_start;
25856 };
25857
25858 /* Write an address entry to ADDR_VEC. */
25859
25860 static void
25861 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
25862 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
25863 {
25864 CORE_ADDR baseaddr;
25865
25866 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25867
25868 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
25869 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
25870 addr_vec.append_data (MAYBE_SWAP (cu_index));
25871 }
25872
25873 /* Worker function for traversing an addrmap to build the address table. */
25874
25875 static int
25876 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
25877 {
25878 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
25879 struct partial_symtab *pst = (struct partial_symtab *) obj;
25880
25881 if (data->previous_valid)
25882 add_address_entry (data->objfile, data->addr_vec,
25883 data->previous_cu_start, start_addr,
25884 data->previous_cu_index);
25885
25886 data->previous_cu_start = start_addr;
25887 if (pst != NULL)
25888 {
25889 const auto it = data->cu_index_htab.find (pst);
25890 gdb_assert (it != data->cu_index_htab.cend ());
25891 data->previous_cu_index = it->second;
25892 data->previous_valid = 1;
25893 }
25894 else
25895 data->previous_valid = 0;
25896
25897 return 0;
25898 }
25899
25900 /* Write OBJFILE's address map to ADDR_VEC.
25901 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
25902 in the index file. */
25903
25904 static void
25905 write_address_map (struct objfile *objfile, data_buf &addr_vec,
25906 psym_index_map &cu_index_htab)
25907 {
25908 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
25909
25910 /* When writing the address table, we have to cope with the fact that
25911 the addrmap iterator only provides the start of a region; we have to
25912 wait until the next invocation to get the start of the next region. */
25913
25914 addrmap_index_data.objfile = objfile;
25915 addrmap_index_data.previous_valid = 0;
25916
25917 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
25918 &addrmap_index_data);
25919
25920 /* It's highly unlikely the last entry (end address = 0xff...ff)
25921 is valid, but we should still handle it.
25922 The end address is recorded as the start of the next region, but that
25923 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
25924 anyway. */
25925 if (addrmap_index_data.previous_valid)
25926 add_address_entry (objfile, addr_vec,
25927 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
25928 addrmap_index_data.previous_cu_index);
25929 }
25930
25931 /* Return the symbol kind of PSYM. */
25932
25933 static gdb_index_symbol_kind
25934 symbol_kind (struct partial_symbol *psym)
25935 {
25936 domain_enum domain = PSYMBOL_DOMAIN (psym);
25937 enum address_class aclass = PSYMBOL_CLASS (psym);
25938
25939 switch (domain)
25940 {
25941 case VAR_DOMAIN:
25942 switch (aclass)
25943 {
25944 case LOC_BLOCK:
25945 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
25946 case LOC_TYPEDEF:
25947 return GDB_INDEX_SYMBOL_KIND_TYPE;
25948 case LOC_COMPUTED:
25949 case LOC_CONST_BYTES:
25950 case LOC_OPTIMIZED_OUT:
25951 case LOC_STATIC:
25952 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25953 case LOC_CONST:
25954 /* Note: It's currently impossible to recognize psyms as enum values
25955 short of reading the type info. For now punt. */
25956 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25957 default:
25958 /* There are other LOC_FOO values that one might want to classify
25959 as variables, but dwarf2read.c doesn't currently use them. */
25960 return GDB_INDEX_SYMBOL_KIND_OTHER;
25961 }
25962 case STRUCT_DOMAIN:
25963 return GDB_INDEX_SYMBOL_KIND_TYPE;
25964 default:
25965 return GDB_INDEX_SYMBOL_KIND_OTHER;
25966 }
25967 }
25968
25969 /* Add a list of partial symbols to SYMTAB. */
25970
25971 static void
25972 write_psymbols (struct mapped_symtab *symtab,
25973 std::unordered_set<partial_symbol *> &psyms_seen,
25974 struct partial_symbol **psymp,
25975 int count,
25976 offset_type cu_index,
25977 int is_static)
25978 {
25979 for (; count-- > 0; ++psymp)
25980 {
25981 struct partial_symbol *psym = *psymp;
25982
25983 if (SYMBOL_LANGUAGE (psym) == language_ada)
25984 error (_("Ada is not currently supported by the index"));
25985
25986 /* Only add a given psymbol once. */
25987 if (psyms_seen.insert (psym).second)
25988 {
25989 gdb_index_symbol_kind kind = symbol_kind (psym);
25990
25991 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
25992 is_static, kind, cu_index);
25993 }
25994 }
25995 }
25996
25997 /* A helper struct used when iterating over debug_types. */
25998 struct signatured_type_index_data
25999 {
26000 signatured_type_index_data (data_buf &types_list_,
26001 std::unordered_set<partial_symbol *> &psyms_seen_)
26002 : types_list (types_list_), psyms_seen (psyms_seen_)
26003 {}
26004
26005 struct objfile *objfile;
26006 struct mapped_symtab *symtab;
26007 data_buf &types_list;
26008 std::unordered_set<partial_symbol *> &psyms_seen;
26009 int cu_index;
26010 };
26011
26012 /* A helper function that writes a single signatured_type to an
26013 obstack. */
26014
26015 static int
26016 write_one_signatured_type (void **slot, void *d)
26017 {
26018 struct signatured_type_index_data *info
26019 = (struct signatured_type_index_data *) d;
26020 struct signatured_type *entry = (struct signatured_type *) *slot;
26021 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26022
26023 write_psymbols (info->symtab,
26024 info->psyms_seen,
26025 &info->objfile->global_psymbols[psymtab->globals_offset],
26026 psymtab->n_global_syms, info->cu_index,
26027 0);
26028 write_psymbols (info->symtab,
26029 info->psyms_seen,
26030 &info->objfile->static_psymbols[psymtab->statics_offset],
26031 psymtab->n_static_syms, info->cu_index,
26032 1);
26033
26034 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26035 to_underlying (entry->per_cu.sect_off));
26036 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26037 to_underlying (entry->type_offset_in_tu));
26038 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
26039
26040 ++info->cu_index;
26041
26042 return 1;
26043 }
26044
26045 /* Recurse into all "included" dependencies and count their symbols as
26046 if they appeared in this psymtab. */
26047
26048 static void
26049 recursively_count_psymbols (struct partial_symtab *psymtab,
26050 size_t &psyms_seen)
26051 {
26052 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26053 if (psymtab->dependencies[i]->user != NULL)
26054 recursively_count_psymbols (psymtab->dependencies[i],
26055 psyms_seen);
26056
26057 psyms_seen += psymtab->n_global_syms;
26058 psyms_seen += psymtab->n_static_syms;
26059 }
26060
26061 /* Recurse into all "included" dependencies and write their symbols as
26062 if they appeared in this psymtab. */
26063
26064 static void
26065 recursively_write_psymbols (struct objfile *objfile,
26066 struct partial_symtab *psymtab,
26067 struct mapped_symtab *symtab,
26068 std::unordered_set<partial_symbol *> &psyms_seen,
26069 offset_type cu_index)
26070 {
26071 int i;
26072
26073 for (i = 0; i < psymtab->number_of_dependencies; ++i)
26074 if (psymtab->dependencies[i]->user != NULL)
26075 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26076 symtab, psyms_seen, cu_index);
26077
26078 write_psymbols (symtab,
26079 psyms_seen,
26080 &objfile->global_psymbols[psymtab->globals_offset],
26081 psymtab->n_global_syms, cu_index,
26082 0);
26083 write_psymbols (symtab,
26084 psyms_seen,
26085 &objfile->static_psymbols[psymtab->statics_offset],
26086 psymtab->n_static_syms, cu_index,
26087 1);
26088 }
26089
26090 /* DWARF-5 .debug_names builder. */
26091 class debug_names
26092 {
26093 public:
26094 debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile, bool is_dwarf64,
26095 bfd_endian dwarf5_byte_order)
26096 : m_dwarf5_byte_order (dwarf5_byte_order),
26097 m_dwarf32 (dwarf5_byte_order),
26098 m_dwarf64 (dwarf5_byte_order),
26099 m_dwarf (is_dwarf64
26100 ? static_cast<dwarf &> (m_dwarf64)
26101 : static_cast<dwarf &> (m_dwarf32)),
26102 m_name_table_string_offs (m_dwarf.name_table_string_offs),
26103 m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
26104 m_debugstrlookup (dwarf2_per_objfile)
26105 {}
26106
26107 int dwarf5_offset_size () const
26108 {
26109 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
26110 return dwarf5_is_dwarf64 ? 8 : 4;
26111 }
26112
26113 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
26114 enum class unit_kind { cu, tu };
26115
26116 /* Insert one symbol. */
26117 void insert (const partial_symbol *psym, int cu_index, bool is_static,
26118 unit_kind kind)
26119 {
26120 const int dwarf_tag = psymbol_tag (psym);
26121 if (dwarf_tag == 0)
26122 return;
26123 const char *const name = SYMBOL_SEARCH_NAME (psym);
26124 const auto insertpair
26125 = m_name_to_value_set.emplace (c_str_view (name),
26126 std::set<symbol_value> ());
26127 std::set<symbol_value> &value_set = insertpair.first->second;
26128 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
26129 }
26130
26131 /* Build all the tables. All symbols must be already inserted.
26132 This function does not call file_write, caller has to do it
26133 afterwards. */
26134 void build ()
26135 {
26136 /* Verify the build method has not be called twice. */
26137 gdb_assert (m_abbrev_table.empty ());
26138 const size_t name_count = m_name_to_value_set.size ();
26139 m_bucket_table.resize
26140 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26141 m_hash_table.reserve (name_count);
26142 m_name_table_string_offs.reserve (name_count);
26143 m_name_table_entry_offs.reserve (name_count);
26144
26145 /* Map each hash of symbol to its name and value. */
26146 struct hash_it_pair
26147 {
26148 uint32_t hash;
26149 decltype (m_name_to_value_set)::const_iterator it;
26150 };
26151 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26152 bucket_hash.resize (m_bucket_table.size ());
26153 for (decltype (m_name_to_value_set)::const_iterator it
26154 = m_name_to_value_set.cbegin ();
26155 it != m_name_to_value_set.cend ();
26156 ++it)
26157 {
26158 const char *const name = it->first.c_str ();
26159 const uint32_t hash = dwarf5_djb_hash (name);
26160 hash_it_pair hashitpair;
26161 hashitpair.hash = hash;
26162 hashitpair.it = it;
26163 auto &slot = bucket_hash[hash % bucket_hash.size()];
26164 slot.push_front (std::move (hashitpair));
26165 }
26166 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26167 {
26168 const std::forward_list<hash_it_pair> &hashitlist
26169 = bucket_hash[bucket_ix];
26170 if (hashitlist.empty ())
26171 continue;
26172 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26173 /* The hashes array is indexed starting at 1. */
26174 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26175 sizeof (bucket_slot), m_dwarf5_byte_order,
26176 m_hash_table.size () + 1);
26177 for (const hash_it_pair &hashitpair : hashitlist)
26178 {
26179 m_hash_table.push_back (0);
26180 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26181 (&m_hash_table.back ()),
26182 sizeof (m_hash_table.back ()),
26183 m_dwarf5_byte_order, hashitpair.hash);
26184 const c_str_view &name = hashitpair.it->first;
26185 const std::set<symbol_value> &value_set = hashitpair.it->second;
26186 m_name_table_string_offs.push_back_reorder
26187 (m_debugstrlookup.lookup (name.c_str ()));
26188 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26189 gdb_assert (!value_set.empty ());
26190 for (const symbol_value &value : value_set)
26191 {
26192 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
26193 value.is_static,
26194 value.kind)];
26195 if (idx == 0)
26196 {
26197 idx = m_idx_next++;
26198 m_abbrev_table.append_unsigned_leb128 (idx);
26199 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
26200 m_abbrev_table.append_unsigned_leb128
26201 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
26202 : DW_IDX_type_unit);
26203 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26204 m_abbrev_table.append_unsigned_leb128 (value.is_static
26205 ? DW_IDX_GNU_internal
26206 : DW_IDX_GNU_external);
26207 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26208
26209 /* Terminate attributes list. */
26210 m_abbrev_table.append_unsigned_leb128 (0);
26211 m_abbrev_table.append_unsigned_leb128 (0);
26212 }
26213
26214 m_entry_pool.append_unsigned_leb128 (idx);
26215 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26216 }
26217
26218 /* Terminate the list of CUs. */
26219 m_entry_pool.append_unsigned_leb128 (0);
26220 }
26221 }
26222 gdb_assert (m_hash_table.size () == name_count);
26223
26224 /* Terminate tags list. */
26225 m_abbrev_table.append_unsigned_leb128 (0);
26226 }
26227
26228 /* Return .debug_names bucket count. This must be called only after
26229 calling the build method. */
26230 uint32_t bucket_count () const
26231 {
26232 /* Verify the build method has been already called. */
26233 gdb_assert (!m_abbrev_table.empty ());
26234 const uint32_t retval = m_bucket_table.size ();
26235
26236 /* Check for overflow. */
26237 gdb_assert (retval == m_bucket_table.size ());
26238 return retval;
26239 }
26240
26241 /* Return .debug_names names count. This must be called only after
26242 calling the build method. */
26243 uint32_t name_count () const
26244 {
26245 /* Verify the build method has been already called. */
26246 gdb_assert (!m_abbrev_table.empty ());
26247 const uint32_t retval = m_hash_table.size ();
26248
26249 /* Check for overflow. */
26250 gdb_assert (retval == m_hash_table.size ());
26251 return retval;
26252 }
26253
26254 /* Return number of bytes of .debug_names abbreviation table. This
26255 must be called only after calling the build method. */
26256 uint32_t abbrev_table_bytes () const
26257 {
26258 gdb_assert (!m_abbrev_table.empty ());
26259 return m_abbrev_table.size ();
26260 }
26261
26262 /* Recurse into all "included" dependencies and store their symbols
26263 as if they appeared in this psymtab. */
26264 void recursively_write_psymbols
26265 (struct objfile *objfile,
26266 struct partial_symtab *psymtab,
26267 std::unordered_set<partial_symbol *> &psyms_seen,
26268 int cu_index)
26269 {
26270 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26271 if (psymtab->dependencies[i]->user != NULL)
26272 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26273 psyms_seen, cu_index);
26274
26275 write_psymbols (psyms_seen,
26276 &objfile->global_psymbols[psymtab->globals_offset],
26277 psymtab->n_global_syms, cu_index, false, unit_kind::cu);
26278 write_psymbols (psyms_seen,
26279 &objfile->static_psymbols[psymtab->statics_offset],
26280 psymtab->n_static_syms, cu_index, true, unit_kind::cu);
26281 }
26282
26283 /* Return number of bytes the .debug_names section will have. This
26284 must be called only after calling the build method. */
26285 size_t bytes () const
26286 {
26287 /* Verify the build method has been already called. */
26288 gdb_assert (!m_abbrev_table.empty ());
26289 size_t expected_bytes = 0;
26290 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26291 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26292 expected_bytes += m_name_table_string_offs.bytes ();
26293 expected_bytes += m_name_table_entry_offs.bytes ();
26294 expected_bytes += m_abbrev_table.size ();
26295 expected_bytes += m_entry_pool.size ();
26296 return expected_bytes;
26297 }
26298
26299 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26300 FILE_STR. This must be called only after calling the build
26301 method. */
26302 void file_write (FILE *file_names, FILE *file_str) const
26303 {
26304 /* Verify the build method has been already called. */
26305 gdb_assert (!m_abbrev_table.empty ());
26306 ::file_write (file_names, m_bucket_table);
26307 ::file_write (file_names, m_hash_table);
26308 m_name_table_string_offs.file_write (file_names);
26309 m_name_table_entry_offs.file_write (file_names);
26310 m_abbrev_table.file_write (file_names);
26311 m_entry_pool.file_write (file_names);
26312 m_debugstrlookup.file_write (file_str);
26313 }
26314
26315 /* A helper user data for write_one_signatured_type. */
26316 class write_one_signatured_type_data
26317 {
26318 public:
26319 write_one_signatured_type_data (debug_names &nametable_,
26320 signatured_type_index_data &&info_)
26321 : nametable (nametable_), info (std::move (info_))
26322 {}
26323 debug_names &nametable;
26324 struct signatured_type_index_data info;
26325 };
26326
26327 /* A helper function to pass write_one_signatured_type to
26328 htab_traverse_noresize. */
26329 static int
26330 write_one_signatured_type (void **slot, void *d)
26331 {
26332 write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
26333 struct signatured_type_index_data *info = &data->info;
26334 struct signatured_type *entry = (struct signatured_type *) *slot;
26335
26336 data->nametable.write_one_signatured_type (entry, info);
26337
26338 return 1;
26339 }
26340
26341 private:
26342
26343 /* Storage for symbol names mapping them to their .debug_str section
26344 offsets. */
26345 class debug_str_lookup
26346 {
26347 public:
26348
26349 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26350 All .debug_str section strings are automatically stored. */
26351 debug_str_lookup (struct dwarf2_per_objfile *dwarf2_per_objfile)
26352 : m_abfd (dwarf2_per_objfile->objfile->obfd),
26353 m_dwarf2_per_objfile (dwarf2_per_objfile)
26354 {
26355 dwarf2_read_section (dwarf2_per_objfile->objfile,
26356 &dwarf2_per_objfile->str);
26357 if (dwarf2_per_objfile->str.buffer == NULL)
26358 return;
26359 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26360 data < (dwarf2_per_objfile->str.buffer
26361 + dwarf2_per_objfile->str.size);)
26362 {
26363 const char *const s = reinterpret_cast<const char *> (data);
26364 const auto insertpair
26365 = m_str_table.emplace (c_str_view (s),
26366 data - dwarf2_per_objfile->str.buffer);
26367 if (!insertpair.second)
26368 complaint (&symfile_complaints,
26369 _("Duplicate string \"%s\" in "
26370 ".debug_str section [in module %s]"),
26371 s, bfd_get_filename (m_abfd));
26372 data += strlen (s) + 1;
26373 }
26374 }
26375
26376 /* Return offset of symbol name S in the .debug_str section. Add
26377 such symbol to the section's end if it does not exist there
26378 yet. */
26379 size_t lookup (const char *s)
26380 {
26381 const auto it = m_str_table.find (c_str_view (s));
26382 if (it != m_str_table.end ())
26383 return it->second;
26384 const size_t offset = (m_dwarf2_per_objfile->str.size
26385 + m_str_add_buf.size ());
26386 m_str_table.emplace (c_str_view (s), offset);
26387 m_str_add_buf.append_cstr0 (s);
26388 return offset;
26389 }
26390
26391 /* Append the end of the .debug_str section to FILE. */
26392 void file_write (FILE *file) const
26393 {
26394 m_str_add_buf.file_write (file);
26395 }
26396
26397 private:
26398 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26399 bfd *const m_abfd;
26400 struct dwarf2_per_objfile *m_dwarf2_per_objfile;
26401
26402 /* Data to add at the end of .debug_str for new needed symbol names. */
26403 data_buf m_str_add_buf;
26404 };
26405
26406 /* Container to map used DWARF tags to their .debug_names abbreviation
26407 tags. */
26408 class index_key
26409 {
26410 public:
26411 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
26412 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
26413 {
26414 }
26415
26416 bool
26417 operator== (const index_key &other) const
26418 {
26419 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
26420 && kind == other.kind);
26421 }
26422
26423 const int dwarf_tag;
26424 const bool is_static;
26425 const unit_kind kind;
26426 };
26427
26428 /* Provide std::unordered_map::hasher for index_key. */
26429 class index_key_hasher
26430 {
26431 public:
26432 size_t
26433 operator () (const index_key &key) const
26434 {
26435 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26436 }
26437 };
26438
26439 /* Parameters of one symbol entry. */
26440 class symbol_value
26441 {
26442 public:
26443 const int dwarf_tag, cu_index;
26444 const bool is_static;
26445 const unit_kind kind;
26446
26447 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
26448 unit_kind kind_)
26449 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
26450 kind (kind_)
26451 {}
26452
26453 bool
26454 operator< (const symbol_value &other) const
26455 {
26456 #define X(n) \
26457 do \
26458 { \
26459 if (n < other.n) \
26460 return true; \
26461 if (n > other.n) \
26462 return false; \
26463 } \
26464 while (0)
26465 X (dwarf_tag);
26466 X (is_static);
26467 X (kind);
26468 X (cu_index);
26469 #undef X
26470 return false;
26471 }
26472 };
26473
26474 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26475 output. */
26476 class offset_vec
26477 {
26478 protected:
26479 const bfd_endian dwarf5_byte_order;
26480 public:
26481 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26482 : dwarf5_byte_order (dwarf5_byte_order_)
26483 {}
26484
26485 /* Call std::vector::reserve for NELEM elements. */
26486 virtual void reserve (size_t nelem) = 0;
26487
26488 /* Call std::vector::push_back with store_unsigned_integer byte
26489 reordering for ELEM. */
26490 virtual void push_back_reorder (size_t elem) = 0;
26491
26492 /* Return expected output size in bytes. */
26493 virtual size_t bytes () const = 0;
26494
26495 /* Write name table to FILE. */
26496 virtual void file_write (FILE *file) const = 0;
26497 };
26498
26499 /* Template to unify DWARF-32 and DWARF-64 output. */
26500 template<typename OffsetSize>
26501 class offset_vec_tmpl : public offset_vec
26502 {
26503 public:
26504 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26505 : offset_vec (dwarf5_byte_order_)
26506 {}
26507
26508 /* Implement offset_vec::reserve. */
26509 void reserve (size_t nelem) override
26510 {
26511 m_vec.reserve (nelem);
26512 }
26513
26514 /* Implement offset_vec::push_back_reorder. */
26515 void push_back_reorder (size_t elem) override
26516 {
26517 m_vec.push_back (elem);
26518 /* Check for overflow. */
26519 gdb_assert (m_vec.back () == elem);
26520 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26521 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26522 }
26523
26524 /* Implement offset_vec::bytes. */
26525 size_t bytes () const override
26526 {
26527 return m_vec.size () * sizeof (m_vec[0]);
26528 }
26529
26530 /* Implement offset_vec::file_write. */
26531 void file_write (FILE *file) const override
26532 {
26533 ::file_write (file, m_vec);
26534 }
26535
26536 private:
26537 std::vector<OffsetSize> m_vec;
26538 };
26539
26540 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26541 respecting name table width. */
26542 class dwarf
26543 {
26544 public:
26545 offset_vec &name_table_string_offs, &name_table_entry_offs;
26546
26547 dwarf (offset_vec &name_table_string_offs_,
26548 offset_vec &name_table_entry_offs_)
26549 : name_table_string_offs (name_table_string_offs_),
26550 name_table_entry_offs (name_table_entry_offs_)
26551 {
26552 }
26553 };
26554
26555 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26556 respecting name table width. */
26557 template<typename OffsetSize>
26558 class dwarf_tmpl : public dwarf
26559 {
26560 public:
26561 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26562 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26563 m_name_table_string_offs (dwarf5_byte_order_),
26564 m_name_table_entry_offs (dwarf5_byte_order_)
26565 {}
26566
26567 private:
26568 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26569 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26570 };
26571
26572 /* Try to reconstruct original DWARF tag for given partial_symbol.
26573 This function is not DWARF-5 compliant but it is sufficient for
26574 GDB as a DWARF-5 index consumer. */
26575 static int psymbol_tag (const struct partial_symbol *psym)
26576 {
26577 domain_enum domain = PSYMBOL_DOMAIN (psym);
26578 enum address_class aclass = PSYMBOL_CLASS (psym);
26579
26580 switch (domain)
26581 {
26582 case VAR_DOMAIN:
26583 switch (aclass)
26584 {
26585 case LOC_BLOCK:
26586 return DW_TAG_subprogram;
26587 case LOC_TYPEDEF:
26588 return DW_TAG_typedef;
26589 case LOC_COMPUTED:
26590 case LOC_CONST_BYTES:
26591 case LOC_OPTIMIZED_OUT:
26592 case LOC_STATIC:
26593 return DW_TAG_variable;
26594 case LOC_CONST:
26595 /* Note: It's currently impossible to recognize psyms as enum values
26596 short of reading the type info. For now punt. */
26597 return DW_TAG_variable;
26598 default:
26599 /* There are other LOC_FOO values that one might want to classify
26600 as variables, but dwarf2read.c doesn't currently use them. */
26601 return DW_TAG_variable;
26602 }
26603 case STRUCT_DOMAIN:
26604 return DW_TAG_structure_type;
26605 default:
26606 return 0;
26607 }
26608 }
26609
26610 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26611 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
26612 struct partial_symbol **psymp, int count, int cu_index,
26613 bool is_static, unit_kind kind)
26614 {
26615 for (; count-- > 0; ++psymp)
26616 {
26617 struct partial_symbol *psym = *psymp;
26618
26619 if (SYMBOL_LANGUAGE (psym) == language_ada)
26620 error (_("Ada is not currently supported by the index"));
26621
26622 /* Only add a given psymbol once. */
26623 if (psyms_seen.insert (psym).second)
26624 insert (psym, cu_index, is_static, kind);
26625 }
26626 }
26627
26628 /* A helper function that writes a single signatured_type
26629 to a debug_names. */
26630 void
26631 write_one_signatured_type (struct signatured_type *entry,
26632 struct signatured_type_index_data *info)
26633 {
26634 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26635
26636 write_psymbols (info->psyms_seen,
26637 &info->objfile->global_psymbols[psymtab->globals_offset],
26638 psymtab->n_global_syms, info->cu_index, false,
26639 unit_kind::tu);
26640 write_psymbols (info->psyms_seen,
26641 &info->objfile->static_psymbols[psymtab->statics_offset],
26642 psymtab->n_static_syms, info->cu_index, true,
26643 unit_kind::tu);
26644
26645 info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
26646 to_underlying (entry->per_cu.sect_off));
26647
26648 ++info->cu_index;
26649 }
26650
26651 /* Store value of each symbol. */
26652 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
26653 m_name_to_value_set;
26654
26655 /* Tables of DWARF-5 .debug_names. They are in object file byte
26656 order. */
26657 std::vector<uint32_t> m_bucket_table;
26658 std::vector<uint32_t> m_hash_table;
26659
26660 const bfd_endian m_dwarf5_byte_order;
26661 dwarf_tmpl<uint32_t> m_dwarf32;
26662 dwarf_tmpl<uint64_t> m_dwarf64;
26663 dwarf &m_dwarf;
26664 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
26665 debug_str_lookup m_debugstrlookup;
26666
26667 /* Map each used .debug_names abbreviation tag parameter to its
26668 index value. */
26669 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
26670
26671 /* Next unused .debug_names abbreviation tag for
26672 m_indexkey_to_idx. */
26673 int m_idx_next = 1;
26674
26675 /* .debug_names abbreviation table. */
26676 data_buf m_abbrev_table;
26677
26678 /* .debug_names entry pool. */
26679 data_buf m_entry_pool;
26680 };
26681
26682 /* Return iff any of the needed offsets does not fit into 32-bit
26683 .debug_names section. */
26684
26685 static bool
26686 check_dwarf64_offsets (struct dwarf2_per_objfile *dwarf2_per_objfile)
26687 {
26688 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26689 {
26690 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
26691
26692 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26693 return true;
26694 }
26695 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
26696 {
26697 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
26698 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
26699
26700 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26701 return true;
26702 }
26703 return false;
26704 }
26705
26706 /* The psyms_seen set is potentially going to be largish (~40k
26707 elements when indexing a -g3 build of GDB itself). Estimate the
26708 number of elements in order to avoid too many rehashes, which
26709 require rebuilding buckets and thus many trips to
26710 malloc/free. */
26711
26712 static size_t
26713 psyms_seen_size (struct dwarf2_per_objfile *dwarf2_per_objfile)
26714 {
26715 size_t psyms_count = 0;
26716 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26717 {
26718 struct dwarf2_per_cu_data *per_cu
26719 = dwarf2_per_objfile->all_comp_units[i];
26720 struct partial_symtab *psymtab = per_cu->v.psymtab;
26721
26722 if (psymtab != NULL && psymtab->user == NULL)
26723 recursively_count_psymbols (psymtab, psyms_count);
26724 }
26725 /* Generating an index for gdb itself shows a ratio of
26726 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
26727 return psyms_count / 4;
26728 }
26729
26730 /* Write new .gdb_index section for OBJFILE into OUT_FILE.
26731 Return how many bytes were expected to be written into OUT_FILE. */
26732
26733 static size_t
26734 write_gdbindex (struct dwarf2_per_objfile *dwarf2_per_objfile, FILE *out_file)
26735 {
26736 struct objfile *objfile = dwarf2_per_objfile->objfile;
26737 mapped_symtab symtab;
26738 data_buf cu_list;
26739
26740 /* While we're scanning CU's create a table that maps a psymtab pointer
26741 (which is what addrmap records) to its index (which is what is recorded
26742 in the index file). This will later be needed to write the address
26743 table. */
26744 psym_index_map cu_index_htab;
26745 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
26746
26747 /* The CU list is already sorted, so we don't need to do additional
26748 work here. Also, the debug_types entries do not appear in
26749 all_comp_units, but only in their own hash table. */
26750
26751 std::unordered_set<partial_symbol *> psyms_seen
26752 (psyms_seen_size (dwarf2_per_objfile));
26753 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26754 {
26755 struct dwarf2_per_cu_data *per_cu
26756 = dwarf2_per_objfile->all_comp_units[i];
26757 struct partial_symtab *psymtab = per_cu->v.psymtab;
26758
26759 /* CU of a shared file from 'dwz -m' may be unused by this main file.
26760 It may be referenced from a local scope but in such case it does not
26761 need to be present in .gdb_index. */
26762 if (psymtab == NULL)
26763 continue;
26764
26765 if (psymtab->user == NULL)
26766 recursively_write_psymbols (objfile, psymtab, &symtab,
26767 psyms_seen, i);
26768
26769 const auto insertpair = cu_index_htab.emplace (psymtab, i);
26770 gdb_assert (insertpair.second);
26771
26772 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
26773 to_underlying (per_cu->sect_off));
26774 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
26775 }
26776
26777 /* Dump the address map. */
26778 data_buf addr_vec;
26779 write_address_map (objfile, addr_vec, cu_index_htab);
26780
26781 /* Write out the .debug_type entries, if any. */
26782 data_buf types_cu_list;
26783 if (dwarf2_per_objfile->signatured_types)
26784 {
26785 signatured_type_index_data sig_data (types_cu_list,
26786 psyms_seen);
26787
26788 sig_data.objfile = objfile;
26789 sig_data.symtab = &symtab;
26790 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
26791 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26792 write_one_signatured_type, &sig_data);
26793 }
26794
26795 /* Now that we've processed all symbols we can shrink their cu_indices
26796 lists. */
26797 uniquify_cu_indices (&symtab);
26798
26799 data_buf symtab_vec, constant_pool;
26800 write_hash_table (&symtab, symtab_vec, constant_pool);
26801
26802 data_buf contents;
26803 const offset_type size_of_contents = 6 * sizeof (offset_type);
26804 offset_type total_len = size_of_contents;
26805
26806 /* The version number. */
26807 contents.append_data (MAYBE_SWAP (8));
26808
26809 /* The offset of the CU list from the start of the file. */
26810 contents.append_data (MAYBE_SWAP (total_len));
26811 total_len += cu_list.size ();
26812
26813 /* The offset of the types CU list from the start of the file. */
26814 contents.append_data (MAYBE_SWAP (total_len));
26815 total_len += types_cu_list.size ();
26816
26817 /* The offset of the address table from the start of the file. */
26818 contents.append_data (MAYBE_SWAP (total_len));
26819 total_len += addr_vec.size ();
26820
26821 /* The offset of the symbol table from the start of the file. */
26822 contents.append_data (MAYBE_SWAP (total_len));
26823 total_len += symtab_vec.size ();
26824
26825 /* The offset of the constant pool from the start of the file. */
26826 contents.append_data (MAYBE_SWAP (total_len));
26827 total_len += constant_pool.size ();
26828
26829 gdb_assert (contents.size () == size_of_contents);
26830
26831 contents.file_write (out_file);
26832 cu_list.file_write (out_file);
26833 types_cu_list.file_write (out_file);
26834 addr_vec.file_write (out_file);
26835 symtab_vec.file_write (out_file);
26836 constant_pool.file_write (out_file);
26837
26838 return total_len;
26839 }
26840
26841 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
26842 static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
26843
26844 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
26845 needed addition to .debug_str section to OUT_FILE_STR. Return how
26846 many bytes were expected to be written into OUT_FILE. */
26847
26848 static size_t
26849 write_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
26850 FILE *out_file, FILE *out_file_str)
26851 {
26852 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (dwarf2_per_objfile);
26853 struct objfile *objfile = dwarf2_per_objfile->objfile;
26854 const enum bfd_endian dwarf5_byte_order
26855 = gdbarch_byte_order (get_objfile_arch (objfile));
26856
26857 /* The CU list is already sorted, so we don't need to do additional
26858 work here. Also, the debug_types entries do not appear in
26859 all_comp_units, but only in their own hash table. */
26860 data_buf cu_list;
26861 debug_names nametable (dwarf2_per_objfile, dwarf5_is_dwarf64,
26862 dwarf5_byte_order);
26863 std::unordered_set<partial_symbol *>
26864 psyms_seen (psyms_seen_size (dwarf2_per_objfile));
26865 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26866 {
26867 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
26868 partial_symtab *psymtab = per_cu->v.psymtab;
26869
26870 /* CU of a shared file from 'dwz -m' may be unused by this main
26871 file. It may be referenced from a local scope but in such
26872 case it does not need to be present in .debug_names. */
26873 if (psymtab == NULL)
26874 continue;
26875
26876 if (psymtab->user == NULL)
26877 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
26878
26879 cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
26880 to_underlying (per_cu->sect_off));
26881 }
26882
26883 /* Write out the .debug_type entries, if any. */
26884 data_buf types_cu_list;
26885 if (dwarf2_per_objfile->signatured_types)
26886 {
26887 debug_names::write_one_signatured_type_data sig_data (nametable,
26888 signatured_type_index_data (types_cu_list, psyms_seen));
26889
26890 sig_data.info.objfile = objfile;
26891 /* It is used only for gdb_index. */
26892 sig_data.info.symtab = nullptr;
26893 sig_data.info.cu_index = 0;
26894 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26895 debug_names::write_one_signatured_type,
26896 &sig_data);
26897 }
26898
26899 nametable.build ();
26900
26901 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
26902
26903 const offset_type bytes_of_header
26904 = ((dwarf5_is_dwarf64 ? 12 : 4)
26905 + 2 + 2 + 7 * 4
26906 + sizeof (dwarf5_gdb_augmentation));
26907 size_t expected_bytes = 0;
26908 expected_bytes += bytes_of_header;
26909 expected_bytes += cu_list.size ();
26910 expected_bytes += types_cu_list.size ();
26911 expected_bytes += nametable.bytes ();
26912 data_buf header;
26913
26914 if (!dwarf5_is_dwarf64)
26915 {
26916 const uint64_t size64 = expected_bytes - 4;
26917 gdb_assert (size64 < 0xfffffff0);
26918 header.append_uint (4, dwarf5_byte_order, size64);
26919 }
26920 else
26921 {
26922 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
26923 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
26924 }
26925
26926 /* The version number. */
26927 header.append_uint (2, dwarf5_byte_order, 5);
26928
26929 /* Padding. */
26930 header.append_uint (2, dwarf5_byte_order, 0);
26931
26932 /* comp_unit_count - The number of CUs in the CU list. */
26933 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
26934
26935 /* local_type_unit_count - The number of TUs in the local TU
26936 list. */
26937 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
26938
26939 /* foreign_type_unit_count - The number of TUs in the foreign TU
26940 list. */
26941 header.append_uint (4, dwarf5_byte_order, 0);
26942
26943 /* bucket_count - The number of hash buckets in the hash lookup
26944 table. */
26945 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
26946
26947 /* name_count - The number of unique names in the index. */
26948 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
26949
26950 /* abbrev_table_size - The size in bytes of the abbreviations
26951 table. */
26952 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
26953
26954 /* augmentation_string_size - The size in bytes of the augmentation
26955 string. This value is rounded up to a multiple of 4. */
26956 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
26957 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
26958 header.append_data (dwarf5_gdb_augmentation);
26959
26960 gdb_assert (header.size () == bytes_of_header);
26961
26962 header.file_write (out_file);
26963 cu_list.file_write (out_file);
26964 types_cu_list.file_write (out_file);
26965 nametable.file_write (out_file, out_file_str);
26966
26967 return expected_bytes;
26968 }
26969
26970 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
26971 position is at the end of the file. */
26972
26973 static void
26974 assert_file_size (FILE *file, const char *filename, size_t expected_size)
26975 {
26976 const auto file_size = ftell (file);
26977 if (file_size == -1)
26978 error (_("Can't get `%s' size"), filename);
26979 gdb_assert (file_size == expected_size);
26980 }
26981
26982 /* Create an index file for OBJFILE in the directory DIR. */
26983
26984 static void
26985 write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
26986 const char *dir,
26987 dw_index_kind index_kind)
26988 {
26989 struct objfile *objfile = dwarf2_per_objfile->objfile;
26990
26991 if (dwarf2_per_objfile->using_index)
26992 error (_("Cannot use an index to create the index"));
26993
26994 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
26995 error (_("Cannot make an index when the file has multiple .debug_types sections"));
26996
26997 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
26998 return;
26999
27000 struct stat st;
27001 if (stat (objfile_name (objfile), &st) < 0)
27002 perror_with_name (objfile_name (objfile));
27003
27004 std::string filename (std::string (dir) + SLASH_STRING
27005 + lbasename (objfile_name (objfile))
27006 + (index_kind == dw_index_kind::DEBUG_NAMES
27007 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
27008
27009 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
27010 if (!out_file)
27011 error (_("Can't open `%s' for writing"), filename.c_str ());
27012
27013 /* Order matters here; we want FILE to be closed before FILENAME is
27014 unlinked, because on MS-Windows one cannot delete a file that is
27015 still open. (Don't call anything here that might throw until
27016 file_closer is created.) */
27017 gdb::unlinker unlink_file (filename.c_str ());
27018 gdb_file_up close_out_file (out_file);
27019
27020 if (index_kind == dw_index_kind::DEBUG_NAMES)
27021 {
27022 std::string filename_str (std::string (dir) + SLASH_STRING
27023 + lbasename (objfile_name (objfile))
27024 + DEBUG_STR_SUFFIX);
27025 FILE *out_file_str
27026 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
27027 if (!out_file_str)
27028 error (_("Can't open `%s' for writing"), filename_str.c_str ());
27029 gdb::unlinker unlink_file_str (filename_str.c_str ());
27030 gdb_file_up close_out_file_str (out_file_str);
27031
27032 const size_t total_len
27033 = write_debug_names (dwarf2_per_objfile, out_file, out_file_str);
27034 assert_file_size (out_file, filename.c_str (), total_len);
27035
27036 /* We want to keep the file .debug_str file too. */
27037 unlink_file_str.keep ();
27038 }
27039 else
27040 {
27041 const size_t total_len
27042 = write_gdbindex (dwarf2_per_objfile, out_file);
27043 assert_file_size (out_file, filename.c_str (), total_len);
27044 }
27045
27046 /* We want to keep the file. */
27047 unlink_file.keep ();
27048 }
27049
27050 /* Implementation of the `save gdb-index' command.
27051
27052 Note that the .gdb_index file format used by this command is
27053 documented in the GDB manual. Any changes here must be documented
27054 there. */
27055
27056 static void
27057 save_gdb_index_command (const char *arg, int from_tty)
27058 {
27059 struct objfile *objfile;
27060 const char dwarf5space[] = "-dwarf-5 ";
27061 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
27062
27063 if (!arg)
27064 arg = "";
27065
27066 arg = skip_spaces (arg);
27067 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
27068 {
27069 index_kind = dw_index_kind::DEBUG_NAMES;
27070 arg += strlen (dwarf5space);
27071 arg = skip_spaces (arg);
27072 }
27073
27074 if (!*arg)
27075 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
27076
27077 ALL_OBJFILES (objfile)
27078 {
27079 struct stat st;
27080
27081 /* If the objfile does not correspond to an actual file, skip it. */
27082 if (stat (objfile_name (objfile), &st) < 0)
27083 continue;
27084
27085 struct dwarf2_per_objfile *dwarf2_per_objfile
27086 = get_dwarf2_per_objfile (objfile);
27087
27088 if (dwarf2_per_objfile != NULL)
27089 {
27090 TRY
27091 {
27092 write_psymtabs_to_index (dwarf2_per_objfile, arg, index_kind);
27093 }
27094 CATCH (except, RETURN_MASK_ERROR)
27095 {
27096 exception_fprintf (gdb_stderr, except,
27097 _("Error while writing index for `%s': "),
27098 objfile_name (objfile));
27099 }
27100 END_CATCH
27101 }
27102
27103 }
27104 }
27105
27106 \f
27107
27108 int dwarf_always_disassemble;
27109
27110 static void
27111 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
27112 struct cmd_list_element *c, const char *value)
27113 {
27114 fprintf_filtered (file,
27115 _("Whether to always disassemble "
27116 "DWARF expressions is %s.\n"),
27117 value);
27118 }
27119
27120 static void
27121 show_check_physname (struct ui_file *file, int from_tty,
27122 struct cmd_list_element *c, const char *value)
27123 {
27124 fprintf_filtered (file,
27125 _("Whether to check \"physname\" is %s.\n"),
27126 value);
27127 }
27128
27129 void
27130 _initialize_dwarf2_read (void)
27131 {
27132 struct cmd_list_element *c;
27133
27134 dwarf2_objfile_data_key = register_objfile_data ();
27135
27136 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
27137 Set DWARF specific variables.\n\
27138 Configure DWARF variables such as the cache size"),
27139 &set_dwarf_cmdlist, "maintenance set dwarf ",
27140 0/*allow-unknown*/, &maintenance_set_cmdlist);
27141
27142 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
27143 Show DWARF specific variables\n\
27144 Show DWARF variables such as the cache size"),
27145 &show_dwarf_cmdlist, "maintenance show dwarf ",
27146 0/*allow-unknown*/, &maintenance_show_cmdlist);
27147
27148 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
27149 &dwarf_max_cache_age, _("\
27150 Set the upper bound on the age of cached DWARF compilation units."), _("\
27151 Show the upper bound on the age of cached DWARF compilation units."), _("\
27152 A higher limit means that cached compilation units will be stored\n\
27153 in memory longer, and more total memory will be used. Zero disables\n\
27154 caching, which can slow down startup."),
27155 NULL,
27156 show_dwarf_max_cache_age,
27157 &set_dwarf_cmdlist,
27158 &show_dwarf_cmdlist);
27159
27160 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
27161 &dwarf_always_disassemble, _("\
27162 Set whether `info address' always disassembles DWARF expressions."), _("\
27163 Show whether `info address' always disassembles DWARF expressions."), _("\
27164 When enabled, DWARF expressions are always printed in an assembly-like\n\
27165 syntax. When disabled, expressions will be printed in a more\n\
27166 conversational style, when possible."),
27167 NULL,
27168 show_dwarf_always_disassemble,
27169 &set_dwarf_cmdlist,
27170 &show_dwarf_cmdlist);
27171
27172 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27173 Set debugging of the DWARF reader."), _("\
27174 Show debugging of the DWARF reader."), _("\
27175 When enabled (non-zero), debugging messages are printed during DWARF\n\
27176 reading and symtab expansion. A value of 1 (one) provides basic\n\
27177 information. A value greater than 1 provides more verbose information."),
27178 NULL,
27179 NULL,
27180 &setdebuglist, &showdebuglist);
27181
27182 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27183 Set debugging of the DWARF DIE reader."), _("\
27184 Show debugging of the DWARF DIE reader."), _("\
27185 When enabled (non-zero), DIEs are dumped after they are read in.\n\
27186 The value is the maximum depth to print."),
27187 NULL,
27188 NULL,
27189 &setdebuglist, &showdebuglist);
27190
27191 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27192 Set debugging of the dwarf line reader."), _("\
27193 Show debugging of the dwarf line reader."), _("\
27194 When enabled (non-zero), line number entries are dumped as they are read in.\n\
27195 A value of 1 (one) provides basic information.\n\
27196 A value greater than 1 provides more verbose information."),
27197 NULL,
27198 NULL,
27199 &setdebuglist, &showdebuglist);
27200
27201 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27202 Set cross-checking of \"physname\" code against demangler."), _("\
27203 Show cross-checking of \"physname\" code against demangler."), _("\
27204 When enabled, GDB's internal \"physname\" code is checked against\n\
27205 the demangler."),
27206 NULL, show_check_physname,
27207 &setdebuglist, &showdebuglist);
27208
27209 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27210 no_class, &use_deprecated_index_sections, _("\
27211 Set whether to use deprecated gdb_index sections."), _("\
27212 Show whether to use deprecated gdb_index sections."), _("\
27213 When enabled, deprecated .gdb_index sections are used anyway.\n\
27214 Normally they are ignored either because of a missing feature or\n\
27215 performance issue.\n\
27216 Warning: This option must be enabled before gdb reads the file."),
27217 NULL,
27218 NULL,
27219 &setlist, &showlist);
27220
27221 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
27222 _("\
27223 Save a gdb-index file.\n\
27224 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27225 \n\
27226 No options create one file with .gdb-index extension for pre-DWARF-5\n\
27227 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27228 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
27229 &save_cmdlist);
27230 set_cmd_completer (c, filename_completer);
27231
27232 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27233 &dwarf2_locexpr_funcs);
27234 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27235 &dwarf2_loclist_funcs);
27236
27237 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27238 &dwarf2_block_frame_base_locexpr_funcs);
27239 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27240 &dwarf2_block_frame_base_loclist_funcs);
27241
27242 #if GDB_SELF_TEST
27243 selftests::register_test ("dw2_expand_symtabs_matching",
27244 selftests::dw2_expand_symtabs_matching::run_test);
27245 #endif
27246 }
This page took 0.635547 seconds and 5 git commands to generate.