dwarf2read.c: Rewrite/simplify mock_mapped_index
[binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2017 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 typedef struct symbol *symbolp;
91 DEF_VEC_P (symbolp);
92
93 /* When == 1, print basic high level tracing messages.
94 When > 1, be more verbose.
95 This is in contrast to the low level DIE reading of dwarf_die_debug. */
96 static unsigned int dwarf_read_debug = 0;
97
98 /* When non-zero, dump DIEs after they are read in. */
99 static unsigned int dwarf_die_debug = 0;
100
101 /* When non-zero, dump line number entries as they are read in. */
102 static unsigned int dwarf_line_debug = 0;
103
104 /* When non-zero, cross-check physname against demangler. */
105 static int check_physname = 0;
106
107 /* When non-zero, do not reject deprecated .gdb_index sections. */
108 static int use_deprecated_index_sections = 0;
109
110 static const struct objfile_data *dwarf2_objfile_data_key;
111
112 /* The "aclass" indices for various kinds of computed DWARF symbols. */
113
114 static int dwarf2_locexpr_index;
115 static int dwarf2_loclist_index;
116 static int dwarf2_locexpr_block_index;
117 static int dwarf2_loclist_block_index;
118
119 /* A descriptor for dwarf sections.
120
121 S.ASECTION, SIZE are typically initialized when the objfile is first
122 scanned. BUFFER, READIN are filled in later when the section is read.
123 If the section contained compressed data then SIZE is updated to record
124 the uncompressed size of the section.
125
126 DWP file format V2 introduces a wrinkle that is easiest to handle by
127 creating the concept of virtual sections contained within a real section.
128 In DWP V2 the sections of the input DWO files are concatenated together
129 into one section, but section offsets are kept relative to the original
130 input section.
131 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
132 the real section this "virtual" section is contained in, and BUFFER,SIZE
133 describe the virtual section. */
134
135 struct dwarf2_section_info
136 {
137 union
138 {
139 /* If this is a real section, the bfd section. */
140 asection *section;
141 /* If this is a virtual section, pointer to the containing ("real")
142 section. */
143 struct dwarf2_section_info *containing_section;
144 } s;
145 /* Pointer to section data, only valid if readin. */
146 const gdb_byte *buffer;
147 /* The size of the section, real or virtual. */
148 bfd_size_type size;
149 /* If this is a virtual section, the offset in the real section.
150 Only valid if is_virtual. */
151 bfd_size_type virtual_offset;
152 /* True if we have tried to read this section. */
153 char readin;
154 /* True if this is a virtual section, False otherwise.
155 This specifies which of s.section and s.containing_section to use. */
156 char is_virtual;
157 };
158
159 typedef struct dwarf2_section_info dwarf2_section_info_def;
160 DEF_VEC_O (dwarf2_section_info_def);
161
162 /* All offsets in the index are of this type. It must be
163 architecture-independent. */
164 typedef uint32_t offset_type;
165
166 DEF_VEC_I (offset_type);
167
168 /* Ensure only legit values are used. */
169 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
170 do { \
171 gdb_assert ((unsigned int) (value) <= 1); \
172 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
173 } while (0)
174
175 /* Ensure only legit values are used. */
176 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
177 do { \
178 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
179 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
180 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
181 } while (0)
182
183 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
184 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
185 do { \
186 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
187 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
188 } while (0)
189
190 #if WORDS_BIGENDIAN
191
192 /* Convert VALUE between big- and little-endian. */
193
194 static offset_type
195 byte_swap (offset_type value)
196 {
197 offset_type result;
198
199 result = (value & 0xff) << 24;
200 result |= (value & 0xff00) << 8;
201 result |= (value & 0xff0000) >> 8;
202 result |= (value & 0xff000000) >> 24;
203 return result;
204 }
205
206 #define MAYBE_SWAP(V) byte_swap (V)
207
208 #else
209 #define MAYBE_SWAP(V) static_cast<offset_type> (V)
210 #endif /* WORDS_BIGENDIAN */
211
212 /* An index into a (C++) symbol name component in a symbol name as
213 recorded in the mapped_index's symbol table. For each C++ symbol
214 in the symbol table, we record one entry for the start of each
215 component in the symbol in a table of name components, and then
216 sort the table, in order to be able to binary search symbol names,
217 ignoring leading namespaces, both completion and regular look up.
218 For example, for symbol "A::B::C", we'll have an entry that points
219 to "A::B::C", another that points to "B::C", and another for "C".
220 Note that function symbols in GDB index have no parameter
221 information, just the function/method names. You can convert a
222 name_component to a "const char *" using the
223 'mapped_index::symbol_name_at(offset_type)' method. */
224
225 struct name_component
226 {
227 /* Offset in the symbol name where the component starts. Stored as
228 a (32-bit) offset instead of a pointer to save memory and improve
229 locality on 64-bit architectures. */
230 offset_type name_offset;
231
232 /* The symbol's index in the symbol and constant pool tables of a
233 mapped_index. */
234 offset_type idx;
235 };
236
237 /* Base class containing bits shared by both .gdb_index and
238 .debug_name indexes. */
239
240 struct mapped_index_base
241 {
242 /* The name_component table (a sorted vector). See name_component's
243 description above. */
244 std::vector<name_component> name_components;
245
246 /* How NAME_COMPONENTS is sorted. */
247 enum case_sensitivity name_components_casing;
248
249 /* Return the number of names in the symbol table. */
250 virtual size_t symbol_name_count () const = 0;
251
252 /* Get the name of the symbol at IDX in the symbol table. */
253 virtual const char *symbol_name_at (offset_type idx) const = 0;
254
255 /* Return whether the name at IDX in the symbol table should be
256 ignored. */
257 virtual bool symbol_name_slot_invalid (offset_type idx) const
258 {
259 return false;
260 }
261
262 /* Build the symbol name component sorted vector, if we haven't
263 yet. */
264 void build_name_components ();
265
266 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
267 possible matches for LN_NO_PARAMS in the name component
268 vector. */
269 std::pair<std::vector<name_component>::const_iterator,
270 std::vector<name_component>::const_iterator>
271 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
272
273 /* Prevent deleting/destroying via a base class pointer. */
274 protected:
275 ~mapped_index_base() = default;
276 };
277
278 /* A description of the mapped index. The file format is described in
279 a comment by the code that writes the index. */
280 struct mapped_index : public mapped_index_base
281 {
282 /* A slot/bucket in the symbol table hash. */
283 struct symbol_table_slot
284 {
285 const offset_type name;
286 const offset_type vec;
287 };
288
289 /* Index data format version. */
290 int version;
291
292 /* The total length of the buffer. */
293 off_t total_size;
294
295 /* The address table data. */
296 gdb::array_view<const gdb_byte> address_table;
297
298 /* The symbol table, implemented as a hash table. */
299 gdb::array_view<symbol_table_slot> symbol_table;
300
301 /* A pointer to the constant pool. */
302 const char *constant_pool;
303
304 bool symbol_name_slot_invalid (offset_type idx) const override
305 {
306 const auto &bucket = this->symbol_table[idx];
307 return bucket.name == 0 && bucket.vec;
308 }
309
310 /* Convenience method to get at the name of the symbol at IDX in the
311 symbol table. */
312 const char *symbol_name_at (offset_type idx) const override
313 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
314
315 size_t symbol_name_count () const override
316 { return this->symbol_table.size (); }
317 };
318
319 /* A description of the mapped .debug_names.
320 Uninitialized map has CU_COUNT 0. */
321 struct mapped_debug_names : public mapped_index_base
322 {
323 bfd_endian dwarf5_byte_order;
324 bool dwarf5_is_dwarf64;
325 bool augmentation_is_gdb;
326 uint8_t offset_size;
327 uint32_t cu_count = 0;
328 uint32_t tu_count, bucket_count, name_count;
329 const gdb_byte *cu_table_reordered, *tu_table_reordered;
330 const uint32_t *bucket_table_reordered, *hash_table_reordered;
331 const gdb_byte *name_table_string_offs_reordered;
332 const gdb_byte *name_table_entry_offs_reordered;
333 const gdb_byte *entry_pool;
334
335 struct index_val
336 {
337 ULONGEST dwarf_tag;
338 struct attr
339 {
340 /* Attribute name DW_IDX_*. */
341 ULONGEST dw_idx;
342
343 /* Attribute form DW_FORM_*. */
344 ULONGEST form;
345
346 /* Value if FORM is DW_FORM_implicit_const. */
347 LONGEST implicit_const;
348 };
349 std::vector<attr> attr_vec;
350 };
351
352 std::unordered_map<ULONGEST, index_val> abbrev_map;
353
354 const char *namei_to_name (uint32_t namei) const;
355
356 /* Implementation of the mapped_index_base virtual interface, for
357 the name_components cache. */
358
359 const char *symbol_name_at (offset_type idx) const override
360 { return namei_to_name (idx); }
361
362 size_t symbol_name_count () const override
363 { return this->name_count; }
364 };
365
366 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
367 DEF_VEC_P (dwarf2_per_cu_ptr);
368
369 struct tu_stats
370 {
371 int nr_uniq_abbrev_tables;
372 int nr_symtabs;
373 int nr_symtab_sharers;
374 int nr_stmt_less_type_units;
375 int nr_all_type_units_reallocs;
376 };
377
378 /* Collection of data recorded per objfile.
379 This hangs off of dwarf2_objfile_data_key. */
380
381 struct dwarf2_per_objfile
382 {
383 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
384 dwarf2 section names, or is NULL if the standard ELF names are
385 used. */
386 dwarf2_per_objfile (struct objfile *objfile,
387 const dwarf2_debug_sections *names);
388
389 ~dwarf2_per_objfile ();
390
391 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
392
393 /* Free all cached compilation units. */
394 void free_cached_comp_units ();
395 private:
396 /* This function is mapped across the sections and remembers the
397 offset and size of each of the debugging sections we are
398 interested in. */
399 void locate_sections (bfd *abfd, asection *sectp,
400 const dwarf2_debug_sections &names);
401
402 public:
403 dwarf2_section_info info {};
404 dwarf2_section_info abbrev {};
405 dwarf2_section_info line {};
406 dwarf2_section_info loc {};
407 dwarf2_section_info loclists {};
408 dwarf2_section_info macinfo {};
409 dwarf2_section_info macro {};
410 dwarf2_section_info str {};
411 dwarf2_section_info line_str {};
412 dwarf2_section_info ranges {};
413 dwarf2_section_info rnglists {};
414 dwarf2_section_info addr {};
415 dwarf2_section_info frame {};
416 dwarf2_section_info eh_frame {};
417 dwarf2_section_info gdb_index {};
418 dwarf2_section_info debug_names {};
419 dwarf2_section_info debug_aranges {};
420
421 VEC (dwarf2_section_info_def) *types = NULL;
422
423 /* Back link. */
424 struct objfile *objfile = NULL;
425
426 /* Table of all the compilation units. This is used to locate
427 the target compilation unit of a particular reference. */
428 struct dwarf2_per_cu_data **all_comp_units = NULL;
429
430 /* The number of compilation units in ALL_COMP_UNITS. */
431 int n_comp_units = 0;
432
433 /* The number of .debug_types-related CUs. */
434 int n_type_units = 0;
435
436 /* The number of elements allocated in all_type_units.
437 If there are skeleton-less TUs, we add them to all_type_units lazily. */
438 int n_allocated_type_units = 0;
439
440 /* The .debug_types-related CUs (TUs).
441 This is stored in malloc space because we may realloc it. */
442 struct signatured_type **all_type_units = NULL;
443
444 /* Table of struct type_unit_group objects.
445 The hash key is the DW_AT_stmt_list value. */
446 htab_t type_unit_groups {};
447
448 /* A table mapping .debug_types signatures to its signatured_type entry.
449 This is NULL if the .debug_types section hasn't been read in yet. */
450 htab_t signatured_types {};
451
452 /* Type unit statistics, to see how well the scaling improvements
453 are doing. */
454 struct tu_stats tu_stats {};
455
456 /* A chain of compilation units that are currently read in, so that
457 they can be freed later. */
458 dwarf2_per_cu_data *read_in_chain = NULL;
459
460 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
461 This is NULL if the table hasn't been allocated yet. */
462 htab_t dwo_files {};
463
464 /* True if we've checked for whether there is a DWP file. */
465 bool dwp_checked = false;
466
467 /* The DWP file if there is one, or NULL. */
468 struct dwp_file *dwp_file = NULL;
469
470 /* The shared '.dwz' file, if one exists. This is used when the
471 original data was compressed using 'dwz -m'. */
472 struct dwz_file *dwz_file = NULL;
473
474 /* A flag indicating whether this objfile has a section loaded at a
475 VMA of 0. */
476 bool has_section_at_zero = false;
477
478 /* True if we are using the mapped index,
479 or we are faking it for OBJF_READNOW's sake. */
480 bool using_index = false;
481
482 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
483 mapped_index *index_table = NULL;
484
485 /* The mapped index, or NULL if .debug_names is missing or not being used. */
486 std::unique_ptr<mapped_debug_names> debug_names_table;
487
488 /* When using index_table, this keeps track of all quick_file_names entries.
489 TUs typically share line table entries with a CU, so we maintain a
490 separate table of all line table entries to support the sharing.
491 Note that while there can be way more TUs than CUs, we've already
492 sorted all the TUs into "type unit groups", grouped by their
493 DW_AT_stmt_list value. Therefore the only sharing done here is with a
494 CU and its associated TU group if there is one. */
495 htab_t quick_file_names_table {};
496
497 /* Set during partial symbol reading, to prevent queueing of full
498 symbols. */
499 bool reading_partial_symbols = false;
500
501 /* Table mapping type DIEs to their struct type *.
502 This is NULL if not allocated yet.
503 The mapping is done via (CU/TU + DIE offset) -> type. */
504 htab_t die_type_hash {};
505
506 /* The CUs we recently read. */
507 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
508
509 /* Table containing line_header indexed by offset and offset_in_dwz. */
510 htab_t line_header_hash {};
511
512 /* Table containing all filenames. This is an optional because the
513 table is lazily constructed on first access. */
514 gdb::optional<filename_seen_cache> filenames_cache;
515 };
516
517 static struct dwarf2_per_objfile *dwarf2_per_objfile;
518
519 /* Default names of the debugging sections. */
520
521 /* Note that if the debugging section has been compressed, it might
522 have a name like .zdebug_info. */
523
524 static const struct dwarf2_debug_sections dwarf2_elf_names =
525 {
526 { ".debug_info", ".zdebug_info" },
527 { ".debug_abbrev", ".zdebug_abbrev" },
528 { ".debug_line", ".zdebug_line" },
529 { ".debug_loc", ".zdebug_loc" },
530 { ".debug_loclists", ".zdebug_loclists" },
531 { ".debug_macinfo", ".zdebug_macinfo" },
532 { ".debug_macro", ".zdebug_macro" },
533 { ".debug_str", ".zdebug_str" },
534 { ".debug_line_str", ".zdebug_line_str" },
535 { ".debug_ranges", ".zdebug_ranges" },
536 { ".debug_rnglists", ".zdebug_rnglists" },
537 { ".debug_types", ".zdebug_types" },
538 { ".debug_addr", ".zdebug_addr" },
539 { ".debug_frame", ".zdebug_frame" },
540 { ".eh_frame", NULL },
541 { ".gdb_index", ".zgdb_index" },
542 { ".debug_names", ".zdebug_names" },
543 { ".debug_aranges", ".zdebug_aranges" },
544 23
545 };
546
547 /* List of DWO/DWP sections. */
548
549 static const struct dwop_section_names
550 {
551 struct dwarf2_section_names abbrev_dwo;
552 struct dwarf2_section_names info_dwo;
553 struct dwarf2_section_names line_dwo;
554 struct dwarf2_section_names loc_dwo;
555 struct dwarf2_section_names loclists_dwo;
556 struct dwarf2_section_names macinfo_dwo;
557 struct dwarf2_section_names macro_dwo;
558 struct dwarf2_section_names str_dwo;
559 struct dwarf2_section_names str_offsets_dwo;
560 struct dwarf2_section_names types_dwo;
561 struct dwarf2_section_names cu_index;
562 struct dwarf2_section_names tu_index;
563 }
564 dwop_section_names =
565 {
566 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
567 { ".debug_info.dwo", ".zdebug_info.dwo" },
568 { ".debug_line.dwo", ".zdebug_line.dwo" },
569 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
570 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
571 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
572 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
573 { ".debug_str.dwo", ".zdebug_str.dwo" },
574 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
575 { ".debug_types.dwo", ".zdebug_types.dwo" },
576 { ".debug_cu_index", ".zdebug_cu_index" },
577 { ".debug_tu_index", ".zdebug_tu_index" },
578 };
579
580 /* local data types */
581
582 /* The data in a compilation unit header, after target2host
583 translation, looks like this. */
584 struct comp_unit_head
585 {
586 unsigned int length;
587 short version;
588 unsigned char addr_size;
589 unsigned char signed_addr_p;
590 sect_offset abbrev_sect_off;
591
592 /* Size of file offsets; either 4 or 8. */
593 unsigned int offset_size;
594
595 /* Size of the length field; either 4 or 12. */
596 unsigned int initial_length_size;
597
598 enum dwarf_unit_type unit_type;
599
600 /* Offset to the first byte of this compilation unit header in the
601 .debug_info section, for resolving relative reference dies. */
602 sect_offset sect_off;
603
604 /* Offset to first die in this cu from the start of the cu.
605 This will be the first byte following the compilation unit header. */
606 cu_offset first_die_cu_offset;
607
608 /* 64-bit signature of this type unit - it is valid only for
609 UNIT_TYPE DW_UT_type. */
610 ULONGEST signature;
611
612 /* For types, offset in the type's DIE of the type defined by this TU. */
613 cu_offset type_cu_offset_in_tu;
614 };
615
616 /* Type used for delaying computation of method physnames.
617 See comments for compute_delayed_physnames. */
618 struct delayed_method_info
619 {
620 /* The type to which the method is attached, i.e., its parent class. */
621 struct type *type;
622
623 /* The index of the method in the type's function fieldlists. */
624 int fnfield_index;
625
626 /* The index of the method in the fieldlist. */
627 int index;
628
629 /* The name of the DIE. */
630 const char *name;
631
632 /* The DIE associated with this method. */
633 struct die_info *die;
634 };
635
636 typedef struct delayed_method_info delayed_method_info;
637 DEF_VEC_O (delayed_method_info);
638
639 /* Internal state when decoding a particular compilation unit. */
640 struct dwarf2_cu
641 {
642 /* The objfile containing this compilation unit. */
643 struct objfile *objfile;
644
645 /* The header of the compilation unit. */
646 struct comp_unit_head header;
647
648 /* Base address of this compilation unit. */
649 CORE_ADDR base_address;
650
651 /* Non-zero if base_address has been set. */
652 int base_known;
653
654 /* The language we are debugging. */
655 enum language language;
656 const struct language_defn *language_defn;
657
658 const char *producer;
659
660 /* The generic symbol table building routines have separate lists for
661 file scope symbols and all all other scopes (local scopes). So
662 we need to select the right one to pass to add_symbol_to_list().
663 We do it by keeping a pointer to the correct list in list_in_scope.
664
665 FIXME: The original dwarf code just treated the file scope as the
666 first local scope, and all other local scopes as nested local
667 scopes, and worked fine. Check to see if we really need to
668 distinguish these in buildsym.c. */
669 struct pending **list_in_scope;
670
671 /* The abbrev table for this CU.
672 Normally this points to the abbrev table in the objfile.
673 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
674 struct abbrev_table *abbrev_table;
675
676 /* Hash table holding all the loaded partial DIEs
677 with partial_die->offset.SECT_OFF as hash. */
678 htab_t partial_dies;
679
680 /* Storage for things with the same lifetime as this read-in compilation
681 unit, including partial DIEs. */
682 struct obstack comp_unit_obstack;
683
684 /* When multiple dwarf2_cu structures are living in memory, this field
685 chains them all together, so that they can be released efficiently.
686 We will probably also want a generation counter so that most-recently-used
687 compilation units are cached... */
688 struct dwarf2_per_cu_data *read_in_chain;
689
690 /* Backlink to our per_cu entry. */
691 struct dwarf2_per_cu_data *per_cu;
692
693 /* How many compilation units ago was this CU last referenced? */
694 int last_used;
695
696 /* A hash table of DIE cu_offset for following references with
697 die_info->offset.sect_off as hash. */
698 htab_t die_hash;
699
700 /* Full DIEs if read in. */
701 struct die_info *dies;
702
703 /* A set of pointers to dwarf2_per_cu_data objects for compilation
704 units referenced by this one. Only set during full symbol processing;
705 partial symbol tables do not have dependencies. */
706 htab_t dependencies;
707
708 /* Header data from the line table, during full symbol processing. */
709 struct line_header *line_header;
710 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
711 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
712 this is the DW_TAG_compile_unit die for this CU. We'll hold on
713 to the line header as long as this DIE is being processed. See
714 process_die_scope. */
715 die_info *line_header_die_owner;
716
717 /* A list of methods which need to have physnames computed
718 after all type information has been read. */
719 VEC (delayed_method_info) *method_list;
720
721 /* To be copied to symtab->call_site_htab. */
722 htab_t call_site_htab;
723
724 /* Non-NULL if this CU came from a DWO file.
725 There is an invariant here that is important to remember:
726 Except for attributes copied from the top level DIE in the "main"
727 (or "stub") file in preparation for reading the DWO file
728 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
729 Either there isn't a DWO file (in which case this is NULL and the point
730 is moot), or there is and either we're not going to read it (in which
731 case this is NULL) or there is and we are reading it (in which case this
732 is non-NULL). */
733 struct dwo_unit *dwo_unit;
734
735 /* The DW_AT_addr_base attribute if present, zero otherwise
736 (zero is a valid value though).
737 Note this value comes from the Fission stub CU/TU's DIE. */
738 ULONGEST addr_base;
739
740 /* The DW_AT_ranges_base attribute if present, zero otherwise
741 (zero is a valid value though).
742 Note this value comes from the Fission stub CU/TU's DIE.
743 Also note that the value is zero in the non-DWO case so this value can
744 be used without needing to know whether DWO files are in use or not.
745 N.B. This does not apply to DW_AT_ranges appearing in
746 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
747 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
748 DW_AT_ranges_base *would* have to be applied, and we'd have to care
749 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
750 ULONGEST ranges_base;
751
752 /* Mark used when releasing cached dies. */
753 unsigned int mark : 1;
754
755 /* This CU references .debug_loc. See the symtab->locations_valid field.
756 This test is imperfect as there may exist optimized debug code not using
757 any location list and still facing inlining issues if handled as
758 unoptimized code. For a future better test see GCC PR other/32998. */
759 unsigned int has_loclist : 1;
760
761 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
762 if all the producer_is_* fields are valid. This information is cached
763 because profiling CU expansion showed excessive time spent in
764 producer_is_gxx_lt_4_6. */
765 unsigned int checked_producer : 1;
766 unsigned int producer_is_gxx_lt_4_6 : 1;
767 unsigned int producer_is_gcc_lt_4_3 : 1;
768 unsigned int producer_is_icc_lt_14 : 1;
769
770 /* When set, the file that we're processing is known to have
771 debugging info for C++ namespaces. GCC 3.3.x did not produce
772 this information, but later versions do. */
773
774 unsigned int processing_has_namespace_info : 1;
775 };
776
777 /* Persistent data held for a compilation unit, even when not
778 processing it. We put a pointer to this structure in the
779 read_symtab_private field of the psymtab. */
780
781 struct dwarf2_per_cu_data
782 {
783 /* The start offset and length of this compilation unit.
784 NOTE: Unlike comp_unit_head.length, this length includes
785 initial_length_size.
786 If the DIE refers to a DWO file, this is always of the original die,
787 not the DWO file. */
788 sect_offset sect_off;
789 unsigned int length;
790
791 /* DWARF standard version this data has been read from (such as 4 or 5). */
792 short dwarf_version;
793
794 /* Flag indicating this compilation unit will be read in before
795 any of the current compilation units are processed. */
796 unsigned int queued : 1;
797
798 /* This flag will be set when reading partial DIEs if we need to load
799 absolutely all DIEs for this compilation unit, instead of just the ones
800 we think are interesting. It gets set if we look for a DIE in the
801 hash table and don't find it. */
802 unsigned int load_all_dies : 1;
803
804 /* Non-zero if this CU is from .debug_types.
805 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
806 this is non-zero. */
807 unsigned int is_debug_types : 1;
808
809 /* Non-zero if this CU is from the .dwz file. */
810 unsigned int is_dwz : 1;
811
812 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
813 This flag is only valid if is_debug_types is true.
814 We can't read a CU directly from a DWO file: There are required
815 attributes in the stub. */
816 unsigned int reading_dwo_directly : 1;
817
818 /* Non-zero if the TU has been read.
819 This is used to assist the "Stay in DWO Optimization" for Fission:
820 When reading a DWO, it's faster to read TUs from the DWO instead of
821 fetching them from random other DWOs (due to comdat folding).
822 If the TU has already been read, the optimization is unnecessary
823 (and unwise - we don't want to change where gdb thinks the TU lives
824 "midflight").
825 This flag is only valid if is_debug_types is true. */
826 unsigned int tu_read : 1;
827
828 /* The section this CU/TU lives in.
829 If the DIE refers to a DWO file, this is always the original die,
830 not the DWO file. */
831 struct dwarf2_section_info *section;
832
833 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
834 of the CU cache it gets reset to NULL again. This is left as NULL for
835 dummy CUs (a CU header, but nothing else). */
836 struct dwarf2_cu *cu;
837
838 /* The corresponding objfile.
839 Normally we can get the objfile from dwarf2_per_objfile.
840 However we can enter this file with just a "per_cu" handle. */
841 struct objfile *objfile;
842
843 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
844 is active. Otherwise, the 'psymtab' field is active. */
845 union
846 {
847 /* The partial symbol table associated with this compilation unit,
848 or NULL for unread partial units. */
849 struct partial_symtab *psymtab;
850
851 /* Data needed by the "quick" functions. */
852 struct dwarf2_per_cu_quick_data *quick;
853 } v;
854
855 /* The CUs we import using DW_TAG_imported_unit. This is filled in
856 while reading psymtabs, used to compute the psymtab dependencies,
857 and then cleared. Then it is filled in again while reading full
858 symbols, and only deleted when the objfile is destroyed.
859
860 This is also used to work around a difference between the way gold
861 generates .gdb_index version <=7 and the way gdb does. Arguably this
862 is a gold bug. For symbols coming from TUs, gold records in the index
863 the CU that includes the TU instead of the TU itself. This breaks
864 dw2_lookup_symbol: It assumes that if the index says symbol X lives
865 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
866 will find X. Alas TUs live in their own symtab, so after expanding CU Y
867 we need to look in TU Z to find X. Fortunately, this is akin to
868 DW_TAG_imported_unit, so we just use the same mechanism: For
869 .gdb_index version <=7 this also records the TUs that the CU referred
870 to. Concurrently with this change gdb was modified to emit version 8
871 indices so we only pay a price for gold generated indices.
872 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
873 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
874 };
875
876 /* Entry in the signatured_types hash table. */
877
878 struct signatured_type
879 {
880 /* The "per_cu" object of this type.
881 This struct is used iff per_cu.is_debug_types.
882 N.B.: This is the first member so that it's easy to convert pointers
883 between them. */
884 struct dwarf2_per_cu_data per_cu;
885
886 /* The type's signature. */
887 ULONGEST signature;
888
889 /* Offset in the TU of the type's DIE, as read from the TU header.
890 If this TU is a DWO stub and the definition lives in a DWO file
891 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
892 cu_offset type_offset_in_tu;
893
894 /* Offset in the section of the type's DIE.
895 If the definition lives in a DWO file, this is the offset in the
896 .debug_types.dwo section.
897 The value is zero until the actual value is known.
898 Zero is otherwise not a valid section offset. */
899 sect_offset type_offset_in_section;
900
901 /* Type units are grouped by their DW_AT_stmt_list entry so that they
902 can share them. This points to the containing symtab. */
903 struct type_unit_group *type_unit_group;
904
905 /* The type.
906 The first time we encounter this type we fully read it in and install it
907 in the symbol tables. Subsequent times we only need the type. */
908 struct type *type;
909
910 /* Containing DWO unit.
911 This field is valid iff per_cu.reading_dwo_directly. */
912 struct dwo_unit *dwo_unit;
913 };
914
915 typedef struct signatured_type *sig_type_ptr;
916 DEF_VEC_P (sig_type_ptr);
917
918 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
919 This includes type_unit_group and quick_file_names. */
920
921 struct stmt_list_hash
922 {
923 /* The DWO unit this table is from or NULL if there is none. */
924 struct dwo_unit *dwo_unit;
925
926 /* Offset in .debug_line or .debug_line.dwo. */
927 sect_offset line_sect_off;
928 };
929
930 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
931 an object of this type. */
932
933 struct type_unit_group
934 {
935 /* dwarf2read.c's main "handle" on a TU symtab.
936 To simplify things we create an artificial CU that "includes" all the
937 type units using this stmt_list so that the rest of the code still has
938 a "per_cu" handle on the symtab.
939 This PER_CU is recognized by having no section. */
940 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
941 struct dwarf2_per_cu_data per_cu;
942
943 /* The TUs that share this DW_AT_stmt_list entry.
944 This is added to while parsing type units to build partial symtabs,
945 and is deleted afterwards and not used again. */
946 VEC (sig_type_ptr) *tus;
947
948 /* The compunit symtab.
949 Type units in a group needn't all be defined in the same source file,
950 so we create an essentially anonymous symtab as the compunit symtab. */
951 struct compunit_symtab *compunit_symtab;
952
953 /* The data used to construct the hash key. */
954 struct stmt_list_hash hash;
955
956 /* The number of symtabs from the line header.
957 The value here must match line_header.num_file_names. */
958 unsigned int num_symtabs;
959
960 /* The symbol tables for this TU (obtained from the files listed in
961 DW_AT_stmt_list).
962 WARNING: The order of entries here must match the order of entries
963 in the line header. After the first TU using this type_unit_group, the
964 line header for the subsequent TUs is recreated from this. This is done
965 because we need to use the same symtabs for each TU using the same
966 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
967 there's no guarantee the line header doesn't have duplicate entries. */
968 struct symtab **symtabs;
969 };
970
971 /* These sections are what may appear in a (real or virtual) DWO file. */
972
973 struct dwo_sections
974 {
975 struct dwarf2_section_info abbrev;
976 struct dwarf2_section_info line;
977 struct dwarf2_section_info loc;
978 struct dwarf2_section_info loclists;
979 struct dwarf2_section_info macinfo;
980 struct dwarf2_section_info macro;
981 struct dwarf2_section_info str;
982 struct dwarf2_section_info str_offsets;
983 /* In the case of a virtual DWO file, these two are unused. */
984 struct dwarf2_section_info info;
985 VEC (dwarf2_section_info_def) *types;
986 };
987
988 /* CUs/TUs in DWP/DWO files. */
989
990 struct dwo_unit
991 {
992 /* Backlink to the containing struct dwo_file. */
993 struct dwo_file *dwo_file;
994
995 /* The "id" that distinguishes this CU/TU.
996 .debug_info calls this "dwo_id", .debug_types calls this "signature".
997 Since signatures came first, we stick with it for consistency. */
998 ULONGEST signature;
999
1000 /* The section this CU/TU lives in, in the DWO file. */
1001 struct dwarf2_section_info *section;
1002
1003 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
1004 sect_offset sect_off;
1005 unsigned int length;
1006
1007 /* For types, offset in the type's DIE of the type defined by this TU. */
1008 cu_offset type_offset_in_tu;
1009 };
1010
1011 /* include/dwarf2.h defines the DWP section codes.
1012 It defines a max value but it doesn't define a min value, which we
1013 use for error checking, so provide one. */
1014
1015 enum dwp_v2_section_ids
1016 {
1017 DW_SECT_MIN = 1
1018 };
1019
1020 /* Data for one DWO file.
1021
1022 This includes virtual DWO files (a virtual DWO file is a DWO file as it
1023 appears in a DWP file). DWP files don't really have DWO files per se -
1024 comdat folding of types "loses" the DWO file they came from, and from
1025 a high level view DWP files appear to contain a mass of random types.
1026 However, to maintain consistency with the non-DWP case we pretend DWP
1027 files contain virtual DWO files, and we assign each TU with one virtual
1028 DWO file (generally based on the line and abbrev section offsets -
1029 a heuristic that seems to work in practice). */
1030
1031 struct dwo_file
1032 {
1033 /* The DW_AT_GNU_dwo_name attribute.
1034 For virtual DWO files the name is constructed from the section offsets
1035 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
1036 from related CU+TUs. */
1037 const char *dwo_name;
1038
1039 /* The DW_AT_comp_dir attribute. */
1040 const char *comp_dir;
1041
1042 /* The bfd, when the file is open. Otherwise this is NULL.
1043 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
1044 bfd *dbfd;
1045
1046 /* The sections that make up this DWO file.
1047 Remember that for virtual DWO files in DWP V2, these are virtual
1048 sections (for lack of a better name). */
1049 struct dwo_sections sections;
1050
1051 /* The CUs in the file.
1052 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
1053 an extension to handle LLVM's Link Time Optimization output (where
1054 multiple source files may be compiled into a single object/dwo pair). */
1055 htab_t cus;
1056
1057 /* Table of TUs in the file.
1058 Each element is a struct dwo_unit. */
1059 htab_t tus;
1060 };
1061
1062 /* These sections are what may appear in a DWP file. */
1063
1064 struct dwp_sections
1065 {
1066 /* These are used by both DWP version 1 and 2. */
1067 struct dwarf2_section_info str;
1068 struct dwarf2_section_info cu_index;
1069 struct dwarf2_section_info tu_index;
1070
1071 /* These are only used by DWP version 2 files.
1072 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
1073 sections are referenced by section number, and are not recorded here.
1074 In DWP version 2 there is at most one copy of all these sections, each
1075 section being (effectively) comprised of the concatenation of all of the
1076 individual sections that exist in the version 1 format.
1077 To keep the code simple we treat each of these concatenated pieces as a
1078 section itself (a virtual section?). */
1079 struct dwarf2_section_info abbrev;
1080 struct dwarf2_section_info info;
1081 struct dwarf2_section_info line;
1082 struct dwarf2_section_info loc;
1083 struct dwarf2_section_info macinfo;
1084 struct dwarf2_section_info macro;
1085 struct dwarf2_section_info str_offsets;
1086 struct dwarf2_section_info types;
1087 };
1088
1089 /* These sections are what may appear in a virtual DWO file in DWP version 1.
1090 A virtual DWO file is a DWO file as it appears in a DWP file. */
1091
1092 struct virtual_v1_dwo_sections
1093 {
1094 struct dwarf2_section_info abbrev;
1095 struct dwarf2_section_info line;
1096 struct dwarf2_section_info loc;
1097 struct dwarf2_section_info macinfo;
1098 struct dwarf2_section_info macro;
1099 struct dwarf2_section_info str_offsets;
1100 /* Each DWP hash table entry records one CU or one TU.
1101 That is recorded here, and copied to dwo_unit.section. */
1102 struct dwarf2_section_info info_or_types;
1103 };
1104
1105 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1106 In version 2, the sections of the DWO files are concatenated together
1107 and stored in one section of that name. Thus each ELF section contains
1108 several "virtual" sections. */
1109
1110 struct virtual_v2_dwo_sections
1111 {
1112 bfd_size_type abbrev_offset;
1113 bfd_size_type abbrev_size;
1114
1115 bfd_size_type line_offset;
1116 bfd_size_type line_size;
1117
1118 bfd_size_type loc_offset;
1119 bfd_size_type loc_size;
1120
1121 bfd_size_type macinfo_offset;
1122 bfd_size_type macinfo_size;
1123
1124 bfd_size_type macro_offset;
1125 bfd_size_type macro_size;
1126
1127 bfd_size_type str_offsets_offset;
1128 bfd_size_type str_offsets_size;
1129
1130 /* Each DWP hash table entry records one CU or one TU.
1131 That is recorded here, and copied to dwo_unit.section. */
1132 bfd_size_type info_or_types_offset;
1133 bfd_size_type info_or_types_size;
1134 };
1135
1136 /* Contents of DWP hash tables. */
1137
1138 struct dwp_hash_table
1139 {
1140 uint32_t version, nr_columns;
1141 uint32_t nr_units, nr_slots;
1142 const gdb_byte *hash_table, *unit_table;
1143 union
1144 {
1145 struct
1146 {
1147 const gdb_byte *indices;
1148 } v1;
1149 struct
1150 {
1151 /* This is indexed by column number and gives the id of the section
1152 in that column. */
1153 #define MAX_NR_V2_DWO_SECTIONS \
1154 (1 /* .debug_info or .debug_types */ \
1155 + 1 /* .debug_abbrev */ \
1156 + 1 /* .debug_line */ \
1157 + 1 /* .debug_loc */ \
1158 + 1 /* .debug_str_offsets */ \
1159 + 1 /* .debug_macro or .debug_macinfo */)
1160 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1161 const gdb_byte *offsets;
1162 const gdb_byte *sizes;
1163 } v2;
1164 } section_pool;
1165 };
1166
1167 /* Data for one DWP file. */
1168
1169 struct dwp_file
1170 {
1171 /* Name of the file. */
1172 const char *name;
1173
1174 /* File format version. */
1175 int version;
1176
1177 /* The bfd. */
1178 bfd *dbfd;
1179
1180 /* Section info for this file. */
1181 struct dwp_sections sections;
1182
1183 /* Table of CUs in the file. */
1184 const struct dwp_hash_table *cus;
1185
1186 /* Table of TUs in the file. */
1187 const struct dwp_hash_table *tus;
1188
1189 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1190 htab_t loaded_cus;
1191 htab_t loaded_tus;
1192
1193 /* Table to map ELF section numbers to their sections.
1194 This is only needed for the DWP V1 file format. */
1195 unsigned int num_sections;
1196 asection **elf_sections;
1197 };
1198
1199 /* This represents a '.dwz' file. */
1200
1201 struct dwz_file
1202 {
1203 /* A dwz file can only contain a few sections. */
1204 struct dwarf2_section_info abbrev;
1205 struct dwarf2_section_info info;
1206 struct dwarf2_section_info str;
1207 struct dwarf2_section_info line;
1208 struct dwarf2_section_info macro;
1209 struct dwarf2_section_info gdb_index;
1210 struct dwarf2_section_info debug_names;
1211
1212 /* The dwz's BFD. */
1213 bfd *dwz_bfd;
1214 };
1215
1216 /* Struct used to pass misc. parameters to read_die_and_children, et
1217 al. which are used for both .debug_info and .debug_types dies.
1218 All parameters here are unchanging for the life of the call. This
1219 struct exists to abstract away the constant parameters of die reading. */
1220
1221 struct die_reader_specs
1222 {
1223 /* The bfd of die_section. */
1224 bfd* abfd;
1225
1226 /* The CU of the DIE we are parsing. */
1227 struct dwarf2_cu *cu;
1228
1229 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1230 struct dwo_file *dwo_file;
1231
1232 /* The section the die comes from.
1233 This is either .debug_info or .debug_types, or the .dwo variants. */
1234 struct dwarf2_section_info *die_section;
1235
1236 /* die_section->buffer. */
1237 const gdb_byte *buffer;
1238
1239 /* The end of the buffer. */
1240 const gdb_byte *buffer_end;
1241
1242 /* The value of the DW_AT_comp_dir attribute. */
1243 const char *comp_dir;
1244 };
1245
1246 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1247 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1248 const gdb_byte *info_ptr,
1249 struct die_info *comp_unit_die,
1250 int has_children,
1251 void *data);
1252
1253 /* A 1-based directory index. This is a strong typedef to prevent
1254 accidentally using a directory index as a 0-based index into an
1255 array/vector. */
1256 enum class dir_index : unsigned int {};
1257
1258 /* Likewise, a 1-based file name index. */
1259 enum class file_name_index : unsigned int {};
1260
1261 struct file_entry
1262 {
1263 file_entry () = default;
1264
1265 file_entry (const char *name_, dir_index d_index_,
1266 unsigned int mod_time_, unsigned int length_)
1267 : name (name_),
1268 d_index (d_index_),
1269 mod_time (mod_time_),
1270 length (length_)
1271 {}
1272
1273 /* Return the include directory at D_INDEX stored in LH. Returns
1274 NULL if D_INDEX is out of bounds. */
1275 const char *include_dir (const line_header *lh) const;
1276
1277 /* The file name. Note this is an observing pointer. The memory is
1278 owned by debug_line_buffer. */
1279 const char *name {};
1280
1281 /* The directory index (1-based). */
1282 dir_index d_index {};
1283
1284 unsigned int mod_time {};
1285
1286 unsigned int length {};
1287
1288 /* True if referenced by the Line Number Program. */
1289 bool included_p {};
1290
1291 /* The associated symbol table, if any. */
1292 struct symtab *symtab {};
1293 };
1294
1295 /* The line number information for a compilation unit (found in the
1296 .debug_line section) begins with a "statement program header",
1297 which contains the following information. */
1298 struct line_header
1299 {
1300 line_header ()
1301 : offset_in_dwz {}
1302 {}
1303
1304 /* Add an entry to the include directory table. */
1305 void add_include_dir (const char *include_dir);
1306
1307 /* Add an entry to the file name table. */
1308 void add_file_name (const char *name, dir_index d_index,
1309 unsigned int mod_time, unsigned int length);
1310
1311 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1312 is out of bounds. */
1313 const char *include_dir_at (dir_index index) const
1314 {
1315 /* Convert directory index number (1-based) to vector index
1316 (0-based). */
1317 size_t vec_index = to_underlying (index) - 1;
1318
1319 if (vec_index >= include_dirs.size ())
1320 return NULL;
1321 return include_dirs[vec_index];
1322 }
1323
1324 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1325 is out of bounds. */
1326 file_entry *file_name_at (file_name_index index)
1327 {
1328 /* Convert file name index number (1-based) to vector index
1329 (0-based). */
1330 size_t vec_index = to_underlying (index) - 1;
1331
1332 if (vec_index >= file_names.size ())
1333 return NULL;
1334 return &file_names[vec_index];
1335 }
1336
1337 /* Const version of the above. */
1338 const file_entry *file_name_at (unsigned int index) const
1339 {
1340 if (index >= file_names.size ())
1341 return NULL;
1342 return &file_names[index];
1343 }
1344
1345 /* Offset of line number information in .debug_line section. */
1346 sect_offset sect_off {};
1347
1348 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1349 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1350
1351 unsigned int total_length {};
1352 unsigned short version {};
1353 unsigned int header_length {};
1354 unsigned char minimum_instruction_length {};
1355 unsigned char maximum_ops_per_instruction {};
1356 unsigned char default_is_stmt {};
1357 int line_base {};
1358 unsigned char line_range {};
1359 unsigned char opcode_base {};
1360
1361 /* standard_opcode_lengths[i] is the number of operands for the
1362 standard opcode whose value is i. This means that
1363 standard_opcode_lengths[0] is unused, and the last meaningful
1364 element is standard_opcode_lengths[opcode_base - 1]. */
1365 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1366
1367 /* The include_directories table. Note these are observing
1368 pointers. The memory is owned by debug_line_buffer. */
1369 std::vector<const char *> include_dirs;
1370
1371 /* The file_names table. */
1372 std::vector<file_entry> file_names;
1373
1374 /* The start and end of the statement program following this
1375 header. These point into dwarf2_per_objfile->line_buffer. */
1376 const gdb_byte *statement_program_start {}, *statement_program_end {};
1377 };
1378
1379 typedef std::unique_ptr<line_header> line_header_up;
1380
1381 const char *
1382 file_entry::include_dir (const line_header *lh) const
1383 {
1384 return lh->include_dir_at (d_index);
1385 }
1386
1387 /* When we construct a partial symbol table entry we only
1388 need this much information. */
1389 struct partial_die_info
1390 {
1391 /* Offset of this DIE. */
1392 sect_offset sect_off;
1393
1394 /* DWARF-2 tag for this DIE. */
1395 ENUM_BITFIELD(dwarf_tag) tag : 16;
1396
1397 /* Assorted flags describing the data found in this DIE. */
1398 unsigned int has_children : 1;
1399 unsigned int is_external : 1;
1400 unsigned int is_declaration : 1;
1401 unsigned int has_type : 1;
1402 unsigned int has_specification : 1;
1403 unsigned int has_pc_info : 1;
1404 unsigned int may_be_inlined : 1;
1405
1406 /* This DIE has been marked DW_AT_main_subprogram. */
1407 unsigned int main_subprogram : 1;
1408
1409 /* Flag set if the SCOPE field of this structure has been
1410 computed. */
1411 unsigned int scope_set : 1;
1412
1413 /* Flag set if the DIE has a byte_size attribute. */
1414 unsigned int has_byte_size : 1;
1415
1416 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1417 unsigned int has_const_value : 1;
1418
1419 /* Flag set if any of the DIE's children are template arguments. */
1420 unsigned int has_template_arguments : 1;
1421
1422 /* Flag set if fixup_partial_die has been called on this die. */
1423 unsigned int fixup_called : 1;
1424
1425 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1426 unsigned int is_dwz : 1;
1427
1428 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1429 unsigned int spec_is_dwz : 1;
1430
1431 /* The name of this DIE. Normally the value of DW_AT_name, but
1432 sometimes a default name for unnamed DIEs. */
1433 const char *name;
1434
1435 /* The linkage name, if present. */
1436 const char *linkage_name;
1437
1438 /* The scope to prepend to our children. This is generally
1439 allocated on the comp_unit_obstack, so will disappear
1440 when this compilation unit leaves the cache. */
1441 const char *scope;
1442
1443 /* Some data associated with the partial DIE. The tag determines
1444 which field is live. */
1445 union
1446 {
1447 /* The location description associated with this DIE, if any. */
1448 struct dwarf_block *locdesc;
1449 /* The offset of an import, for DW_TAG_imported_unit. */
1450 sect_offset sect_off;
1451 } d;
1452
1453 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1454 CORE_ADDR lowpc;
1455 CORE_ADDR highpc;
1456
1457 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1458 DW_AT_sibling, if any. */
1459 /* NOTE: This member isn't strictly necessary, read_partial_die could
1460 return DW_AT_sibling values to its caller load_partial_dies. */
1461 const gdb_byte *sibling;
1462
1463 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1464 DW_AT_specification (or DW_AT_abstract_origin or
1465 DW_AT_extension). */
1466 sect_offset spec_offset;
1467
1468 /* Pointers to this DIE's parent, first child, and next sibling,
1469 if any. */
1470 struct partial_die_info *die_parent, *die_child, *die_sibling;
1471 };
1472
1473 /* This data structure holds the information of an abbrev. */
1474 struct abbrev_info
1475 {
1476 unsigned int number; /* number identifying abbrev */
1477 enum dwarf_tag tag; /* dwarf tag */
1478 unsigned short has_children; /* boolean */
1479 unsigned short num_attrs; /* number of attributes */
1480 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1481 struct abbrev_info *next; /* next in chain */
1482 };
1483
1484 struct attr_abbrev
1485 {
1486 ENUM_BITFIELD(dwarf_attribute) name : 16;
1487 ENUM_BITFIELD(dwarf_form) form : 16;
1488
1489 /* It is valid only if FORM is DW_FORM_implicit_const. */
1490 LONGEST implicit_const;
1491 };
1492
1493 /* Size of abbrev_table.abbrev_hash_table. */
1494 #define ABBREV_HASH_SIZE 121
1495
1496 /* Top level data structure to contain an abbreviation table. */
1497
1498 struct abbrev_table
1499 {
1500 /* Where the abbrev table came from.
1501 This is used as a sanity check when the table is used. */
1502 sect_offset sect_off;
1503
1504 /* Storage for the abbrev table. */
1505 struct obstack abbrev_obstack;
1506
1507 /* Hash table of abbrevs.
1508 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1509 It could be statically allocated, but the previous code didn't so we
1510 don't either. */
1511 struct abbrev_info **abbrevs;
1512 };
1513
1514 /* Attributes have a name and a value. */
1515 struct attribute
1516 {
1517 ENUM_BITFIELD(dwarf_attribute) name : 16;
1518 ENUM_BITFIELD(dwarf_form) form : 15;
1519
1520 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1521 field should be in u.str (existing only for DW_STRING) but it is kept
1522 here for better struct attribute alignment. */
1523 unsigned int string_is_canonical : 1;
1524
1525 union
1526 {
1527 const char *str;
1528 struct dwarf_block *blk;
1529 ULONGEST unsnd;
1530 LONGEST snd;
1531 CORE_ADDR addr;
1532 ULONGEST signature;
1533 }
1534 u;
1535 };
1536
1537 /* This data structure holds a complete die structure. */
1538 struct die_info
1539 {
1540 /* DWARF-2 tag for this DIE. */
1541 ENUM_BITFIELD(dwarf_tag) tag : 16;
1542
1543 /* Number of attributes */
1544 unsigned char num_attrs;
1545
1546 /* True if we're presently building the full type name for the
1547 type derived from this DIE. */
1548 unsigned char building_fullname : 1;
1549
1550 /* True if this die is in process. PR 16581. */
1551 unsigned char in_process : 1;
1552
1553 /* Abbrev number */
1554 unsigned int abbrev;
1555
1556 /* Offset in .debug_info or .debug_types section. */
1557 sect_offset sect_off;
1558
1559 /* The dies in a compilation unit form an n-ary tree. PARENT
1560 points to this die's parent; CHILD points to the first child of
1561 this node; and all the children of a given node are chained
1562 together via their SIBLING fields. */
1563 struct die_info *child; /* Its first child, if any. */
1564 struct die_info *sibling; /* Its next sibling, if any. */
1565 struct die_info *parent; /* Its parent, if any. */
1566
1567 /* An array of attributes, with NUM_ATTRS elements. There may be
1568 zero, but it's not common and zero-sized arrays are not
1569 sufficiently portable C. */
1570 struct attribute attrs[1];
1571 };
1572
1573 /* Get at parts of an attribute structure. */
1574
1575 #define DW_STRING(attr) ((attr)->u.str)
1576 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1577 #define DW_UNSND(attr) ((attr)->u.unsnd)
1578 #define DW_BLOCK(attr) ((attr)->u.blk)
1579 #define DW_SND(attr) ((attr)->u.snd)
1580 #define DW_ADDR(attr) ((attr)->u.addr)
1581 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1582
1583 /* Blocks are a bunch of untyped bytes. */
1584 struct dwarf_block
1585 {
1586 size_t size;
1587
1588 /* Valid only if SIZE is not zero. */
1589 const gdb_byte *data;
1590 };
1591
1592 #ifndef ATTR_ALLOC_CHUNK
1593 #define ATTR_ALLOC_CHUNK 4
1594 #endif
1595
1596 /* Allocate fields for structs, unions and enums in this size. */
1597 #ifndef DW_FIELD_ALLOC_CHUNK
1598 #define DW_FIELD_ALLOC_CHUNK 4
1599 #endif
1600
1601 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1602 but this would require a corresponding change in unpack_field_as_long
1603 and friends. */
1604 static int bits_per_byte = 8;
1605
1606 struct nextfield
1607 {
1608 struct nextfield *next;
1609 int accessibility;
1610 int virtuality;
1611 struct field field;
1612 };
1613
1614 struct nextfnfield
1615 {
1616 struct nextfnfield *next;
1617 struct fn_field fnfield;
1618 };
1619
1620 struct fnfieldlist
1621 {
1622 const char *name;
1623 int length;
1624 struct nextfnfield *head;
1625 };
1626
1627 struct decl_field_list
1628 {
1629 struct decl_field field;
1630 struct decl_field_list *next;
1631 };
1632
1633 /* The routines that read and process dies for a C struct or C++ class
1634 pass lists of data member fields and lists of member function fields
1635 in an instance of a field_info structure, as defined below. */
1636 struct field_info
1637 {
1638 /* List of data member and baseclasses fields. */
1639 struct nextfield *fields, *baseclasses;
1640
1641 /* Number of fields (including baseclasses). */
1642 int nfields;
1643
1644 /* Number of baseclasses. */
1645 int nbaseclasses;
1646
1647 /* Set if the accesibility of one of the fields is not public. */
1648 int non_public_fields;
1649
1650 /* Member function fieldlist array, contains name of possibly overloaded
1651 member function, number of overloaded member functions and a pointer
1652 to the head of the member function field chain. */
1653 struct fnfieldlist *fnfieldlists;
1654
1655 /* Number of entries in the fnfieldlists array. */
1656 int nfnfields;
1657
1658 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1659 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1660 struct decl_field_list *typedef_field_list;
1661 unsigned typedef_field_list_count;
1662
1663 /* Nested types defined by this class and the number of elements in this
1664 list. */
1665 struct decl_field_list *nested_types_list;
1666 unsigned nested_types_list_count;
1667 };
1668
1669 /* One item on the queue of compilation units to read in full symbols
1670 for. */
1671 struct dwarf2_queue_item
1672 {
1673 struct dwarf2_per_cu_data *per_cu;
1674 enum language pretend_language;
1675 struct dwarf2_queue_item *next;
1676 };
1677
1678 /* The current queue. */
1679 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1680
1681 /* Loaded secondary compilation units are kept in memory until they
1682 have not been referenced for the processing of this many
1683 compilation units. Set this to zero to disable caching. Cache
1684 sizes of up to at least twenty will improve startup time for
1685 typical inter-CU-reference binaries, at an obvious memory cost. */
1686 static int dwarf_max_cache_age = 5;
1687 static void
1688 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1689 struct cmd_list_element *c, const char *value)
1690 {
1691 fprintf_filtered (file, _("The upper bound on the age of cached "
1692 "DWARF compilation units is %s.\n"),
1693 value);
1694 }
1695 \f
1696 /* local function prototypes */
1697
1698 static const char *get_section_name (const struct dwarf2_section_info *);
1699
1700 static const char *get_section_file_name (const struct dwarf2_section_info *);
1701
1702 static void dwarf2_find_base_address (struct die_info *die,
1703 struct dwarf2_cu *cu);
1704
1705 static struct partial_symtab *create_partial_symtab
1706 (struct dwarf2_per_cu_data *per_cu, const char *name);
1707
1708 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1709 const gdb_byte *info_ptr,
1710 struct die_info *type_unit_die,
1711 int has_children, void *data);
1712
1713 static void dwarf2_build_psymtabs_hard (struct objfile *);
1714
1715 static void scan_partial_symbols (struct partial_die_info *,
1716 CORE_ADDR *, CORE_ADDR *,
1717 int, struct dwarf2_cu *);
1718
1719 static void add_partial_symbol (struct partial_die_info *,
1720 struct dwarf2_cu *);
1721
1722 static void add_partial_namespace (struct partial_die_info *pdi,
1723 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1724 int set_addrmap, struct dwarf2_cu *cu);
1725
1726 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1727 CORE_ADDR *highpc, int set_addrmap,
1728 struct dwarf2_cu *cu);
1729
1730 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1731 struct dwarf2_cu *cu);
1732
1733 static void add_partial_subprogram (struct partial_die_info *pdi,
1734 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1735 int need_pc, struct dwarf2_cu *cu);
1736
1737 static void dwarf2_read_symtab (struct partial_symtab *,
1738 struct objfile *);
1739
1740 static void psymtab_to_symtab_1 (struct partial_symtab *);
1741
1742 static struct abbrev_info *abbrev_table_lookup_abbrev
1743 (const struct abbrev_table *, unsigned int);
1744
1745 static struct abbrev_table *abbrev_table_read_table
1746 (struct dwarf2_section_info *, sect_offset);
1747
1748 static void abbrev_table_free (struct abbrev_table *);
1749
1750 static void abbrev_table_free_cleanup (void *);
1751
1752 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1753 struct dwarf2_section_info *);
1754
1755 static void dwarf2_free_abbrev_table (void *);
1756
1757 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1758
1759 static struct partial_die_info *load_partial_dies
1760 (const struct die_reader_specs *, const gdb_byte *, int);
1761
1762 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1763 struct partial_die_info *,
1764 struct abbrev_info *,
1765 unsigned int,
1766 const gdb_byte *);
1767
1768 static struct partial_die_info *find_partial_die (sect_offset, int,
1769 struct dwarf2_cu *);
1770
1771 static void fixup_partial_die (struct partial_die_info *,
1772 struct dwarf2_cu *);
1773
1774 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1775 struct attribute *, struct attr_abbrev *,
1776 const gdb_byte *);
1777
1778 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1779
1780 static int read_1_signed_byte (bfd *, const gdb_byte *);
1781
1782 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1783
1784 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1785
1786 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1787
1788 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1789 unsigned int *);
1790
1791 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1792
1793 static LONGEST read_checked_initial_length_and_offset
1794 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1795 unsigned int *, unsigned int *);
1796
1797 static LONGEST read_offset (bfd *, const gdb_byte *,
1798 const struct comp_unit_head *,
1799 unsigned int *);
1800
1801 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1802
1803 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1804 sect_offset);
1805
1806 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1807
1808 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1809
1810 static const char *read_indirect_string (bfd *, const gdb_byte *,
1811 const struct comp_unit_head *,
1812 unsigned int *);
1813
1814 static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1815 const struct comp_unit_head *,
1816 unsigned int *);
1817
1818 static const char *read_indirect_string_at_offset (bfd *abfd,
1819 LONGEST str_offset);
1820
1821 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1822
1823 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1824
1825 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1826 const gdb_byte *,
1827 unsigned int *);
1828
1829 static const char *read_str_index (const struct die_reader_specs *reader,
1830 ULONGEST str_index);
1831
1832 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1833
1834 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1835 struct dwarf2_cu *);
1836
1837 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1838 unsigned int);
1839
1840 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1841 struct dwarf2_cu *cu);
1842
1843 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1844 struct dwarf2_cu *cu);
1845
1846 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1847
1848 static struct die_info *die_specification (struct die_info *die,
1849 struct dwarf2_cu **);
1850
1851 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1852 struct dwarf2_cu *cu);
1853
1854 static void dwarf_decode_lines (struct line_header *, const char *,
1855 struct dwarf2_cu *, struct partial_symtab *,
1856 CORE_ADDR, int decode_mapping);
1857
1858 static void dwarf2_start_subfile (const char *, const char *);
1859
1860 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1861 const char *, const char *,
1862 CORE_ADDR);
1863
1864 static struct symbol *new_symbol (struct die_info *, struct type *,
1865 struct dwarf2_cu *);
1866
1867 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1868 struct dwarf2_cu *, struct symbol *);
1869
1870 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1871 struct dwarf2_cu *);
1872
1873 static void dwarf2_const_value_attr (const struct attribute *attr,
1874 struct type *type,
1875 const char *name,
1876 struct obstack *obstack,
1877 struct dwarf2_cu *cu, LONGEST *value,
1878 const gdb_byte **bytes,
1879 struct dwarf2_locexpr_baton **baton);
1880
1881 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1882
1883 static int need_gnat_info (struct dwarf2_cu *);
1884
1885 static struct type *die_descriptive_type (struct die_info *,
1886 struct dwarf2_cu *);
1887
1888 static void set_descriptive_type (struct type *, struct die_info *,
1889 struct dwarf2_cu *);
1890
1891 static struct type *die_containing_type (struct die_info *,
1892 struct dwarf2_cu *);
1893
1894 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1895 struct dwarf2_cu *);
1896
1897 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1898
1899 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1900
1901 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1902
1903 static char *typename_concat (struct obstack *obs, const char *prefix,
1904 const char *suffix, int physname,
1905 struct dwarf2_cu *cu);
1906
1907 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1908
1909 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1910
1911 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1912
1913 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1914
1915 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1916
1917 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1918
1919 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1920 struct dwarf2_cu *, struct partial_symtab *);
1921
1922 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1923 values. Keep the items ordered with increasing constraints compliance. */
1924 enum pc_bounds_kind
1925 {
1926 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1927 PC_BOUNDS_NOT_PRESENT,
1928
1929 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1930 were present but they do not form a valid range of PC addresses. */
1931 PC_BOUNDS_INVALID,
1932
1933 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1934 PC_BOUNDS_RANGES,
1935
1936 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1937 PC_BOUNDS_HIGH_LOW,
1938 };
1939
1940 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1941 CORE_ADDR *, CORE_ADDR *,
1942 struct dwarf2_cu *,
1943 struct partial_symtab *);
1944
1945 static void get_scope_pc_bounds (struct die_info *,
1946 CORE_ADDR *, CORE_ADDR *,
1947 struct dwarf2_cu *);
1948
1949 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1950 CORE_ADDR, struct dwarf2_cu *);
1951
1952 static void dwarf2_add_field (struct field_info *, struct die_info *,
1953 struct dwarf2_cu *);
1954
1955 static void dwarf2_attach_fields_to_type (struct field_info *,
1956 struct type *, struct dwarf2_cu *);
1957
1958 static void dwarf2_add_member_fn (struct field_info *,
1959 struct die_info *, struct type *,
1960 struct dwarf2_cu *);
1961
1962 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1963 struct type *,
1964 struct dwarf2_cu *);
1965
1966 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1967
1968 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1969
1970 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1971
1972 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1973
1974 static struct using_direct **using_directives (enum language);
1975
1976 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1977
1978 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1979
1980 static struct type *read_module_type (struct die_info *die,
1981 struct dwarf2_cu *cu);
1982
1983 static const char *namespace_name (struct die_info *die,
1984 int *is_anonymous, struct dwarf2_cu *);
1985
1986 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1987
1988 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1989
1990 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1991 struct dwarf2_cu *);
1992
1993 static struct die_info *read_die_and_siblings_1
1994 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1995 struct die_info *);
1996
1997 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1998 const gdb_byte *info_ptr,
1999 const gdb_byte **new_info_ptr,
2000 struct die_info *parent);
2001
2002 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
2003 struct die_info **, const gdb_byte *,
2004 int *, int);
2005
2006 static const gdb_byte *read_full_die (const struct die_reader_specs *,
2007 struct die_info **, const gdb_byte *,
2008 int *);
2009
2010 static void process_die (struct die_info *, struct dwarf2_cu *);
2011
2012 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
2013 struct obstack *);
2014
2015 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
2016
2017 static const char *dwarf2_full_name (const char *name,
2018 struct die_info *die,
2019 struct dwarf2_cu *cu);
2020
2021 static const char *dwarf2_physname (const char *name, struct die_info *die,
2022 struct dwarf2_cu *cu);
2023
2024 static struct die_info *dwarf2_extension (struct die_info *die,
2025 struct dwarf2_cu **);
2026
2027 static const char *dwarf_tag_name (unsigned int);
2028
2029 static const char *dwarf_attr_name (unsigned int);
2030
2031 static const char *dwarf_form_name (unsigned int);
2032
2033 static const char *dwarf_bool_name (unsigned int);
2034
2035 static const char *dwarf_type_encoding_name (unsigned int);
2036
2037 static struct die_info *sibling_die (struct die_info *);
2038
2039 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
2040
2041 static void dump_die_for_error (struct die_info *);
2042
2043 static void dump_die_1 (struct ui_file *, int level, int max_level,
2044 struct die_info *);
2045
2046 /*static*/ void dump_die (struct die_info *, int max_level);
2047
2048 static void store_in_ref_table (struct die_info *,
2049 struct dwarf2_cu *);
2050
2051 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
2052
2053 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
2054
2055 static struct die_info *follow_die_ref_or_sig (struct die_info *,
2056 const struct attribute *,
2057 struct dwarf2_cu **);
2058
2059 static struct die_info *follow_die_ref (struct die_info *,
2060 const struct attribute *,
2061 struct dwarf2_cu **);
2062
2063 static struct die_info *follow_die_sig (struct die_info *,
2064 const struct attribute *,
2065 struct dwarf2_cu **);
2066
2067 static struct type *get_signatured_type (struct die_info *, ULONGEST,
2068 struct dwarf2_cu *);
2069
2070 static struct type *get_DW_AT_signature_type (struct die_info *,
2071 const struct attribute *,
2072 struct dwarf2_cu *);
2073
2074 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
2075
2076 static void read_signatured_type (struct signatured_type *);
2077
2078 static int attr_to_dynamic_prop (const struct attribute *attr,
2079 struct die_info *die, struct dwarf2_cu *cu,
2080 struct dynamic_prop *prop);
2081
2082 /* memory allocation interface */
2083
2084 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
2085
2086 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
2087
2088 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2089
2090 static int attr_form_is_block (const struct attribute *);
2091
2092 static int attr_form_is_section_offset (const struct attribute *);
2093
2094 static int attr_form_is_constant (const struct attribute *);
2095
2096 static int attr_form_is_ref (const struct attribute *);
2097
2098 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2099 struct dwarf2_loclist_baton *baton,
2100 const struct attribute *attr);
2101
2102 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
2103 struct symbol *sym,
2104 struct dwarf2_cu *cu,
2105 int is_block);
2106
2107 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2108 const gdb_byte *info_ptr,
2109 struct abbrev_info *abbrev);
2110
2111 static void free_stack_comp_unit (void *);
2112
2113 static hashval_t partial_die_hash (const void *item);
2114
2115 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2116
2117 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
2118 (sect_offset sect_off, unsigned int offset_in_dwz, struct objfile *objfile);
2119
2120 static void init_one_comp_unit (struct dwarf2_cu *cu,
2121 struct dwarf2_per_cu_data *per_cu);
2122
2123 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
2124 struct die_info *comp_unit_die,
2125 enum language pretend_language);
2126
2127 static void free_heap_comp_unit (void *);
2128
2129 static void free_cached_comp_units (void *);
2130
2131 static void age_cached_comp_units (void);
2132
2133 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
2134
2135 static struct type *set_die_type (struct die_info *, struct type *,
2136 struct dwarf2_cu *);
2137
2138 static void create_all_comp_units (struct objfile *);
2139
2140 static int create_all_type_units (struct objfile *);
2141
2142 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2143 enum language);
2144
2145 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2146 enum language);
2147
2148 static void process_full_type_unit (struct dwarf2_per_cu_data *,
2149 enum language);
2150
2151 static void dwarf2_add_dependence (struct dwarf2_cu *,
2152 struct dwarf2_per_cu_data *);
2153
2154 static void dwarf2_mark (struct dwarf2_cu *);
2155
2156 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2157
2158 static struct type *get_die_type_at_offset (sect_offset,
2159 struct dwarf2_per_cu_data *);
2160
2161 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
2162
2163 static void dwarf2_release_queue (void *dummy);
2164
2165 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2166 enum language pretend_language);
2167
2168 static void process_queue (void);
2169
2170 /* The return type of find_file_and_directory. Note, the enclosed
2171 string pointers are only valid while this object is valid. */
2172
2173 struct file_and_directory
2174 {
2175 /* The filename. This is never NULL. */
2176 const char *name;
2177
2178 /* The compilation directory. NULL if not known. If we needed to
2179 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2180 points directly to the DW_AT_comp_dir string attribute owned by
2181 the obstack that owns the DIE. */
2182 const char *comp_dir;
2183
2184 /* If we needed to build a new string for comp_dir, this is what
2185 owns the storage. */
2186 std::string comp_dir_storage;
2187 };
2188
2189 static file_and_directory find_file_and_directory (struct die_info *die,
2190 struct dwarf2_cu *cu);
2191
2192 static char *file_full_name (int file, struct line_header *lh,
2193 const char *comp_dir);
2194
2195 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2196 enum class rcuh_kind { COMPILE, TYPE };
2197
2198 static const gdb_byte *read_and_check_comp_unit_head
2199 (struct comp_unit_head *header,
2200 struct dwarf2_section_info *section,
2201 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2202 rcuh_kind section_kind);
2203
2204 static void init_cutu_and_read_dies
2205 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2206 int use_existing_cu, int keep,
2207 die_reader_func_ftype *die_reader_func, void *data);
2208
2209 static void init_cutu_and_read_dies_simple
2210 (struct dwarf2_per_cu_data *this_cu,
2211 die_reader_func_ftype *die_reader_func, void *data);
2212
2213 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2214
2215 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2216
2217 static struct dwo_unit *lookup_dwo_unit_in_dwp
2218 (struct dwp_file *dwp_file, const char *comp_dir,
2219 ULONGEST signature, int is_debug_types);
2220
2221 static struct dwp_file *get_dwp_file (void);
2222
2223 static struct dwo_unit *lookup_dwo_comp_unit
2224 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2225
2226 static struct dwo_unit *lookup_dwo_type_unit
2227 (struct signatured_type *, const char *, const char *);
2228
2229 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2230
2231 static void free_dwo_file_cleanup (void *);
2232
2233 static void process_cu_includes (void);
2234
2235 static void check_producer (struct dwarf2_cu *cu);
2236
2237 static void free_line_header_voidp (void *arg);
2238 \f
2239 /* Various complaints about symbol reading that don't abort the process. */
2240
2241 static void
2242 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2243 {
2244 complaint (&symfile_complaints,
2245 _("statement list doesn't fit in .debug_line section"));
2246 }
2247
2248 static void
2249 dwarf2_debug_line_missing_file_complaint (void)
2250 {
2251 complaint (&symfile_complaints,
2252 _(".debug_line section has line data without a file"));
2253 }
2254
2255 static void
2256 dwarf2_debug_line_missing_end_sequence_complaint (void)
2257 {
2258 complaint (&symfile_complaints,
2259 _(".debug_line section has line "
2260 "program sequence without an end"));
2261 }
2262
2263 static void
2264 dwarf2_complex_location_expr_complaint (void)
2265 {
2266 complaint (&symfile_complaints, _("location expression too complex"));
2267 }
2268
2269 static void
2270 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2271 int arg3)
2272 {
2273 complaint (&symfile_complaints,
2274 _("const value length mismatch for '%s', got %d, expected %d"),
2275 arg1, arg2, arg3);
2276 }
2277
2278 static void
2279 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2280 {
2281 complaint (&symfile_complaints,
2282 _("debug info runs off end of %s section"
2283 " [in module %s]"),
2284 get_section_name (section),
2285 get_section_file_name (section));
2286 }
2287
2288 static void
2289 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2290 {
2291 complaint (&symfile_complaints,
2292 _("macro debug info contains a "
2293 "malformed macro definition:\n`%s'"),
2294 arg1);
2295 }
2296
2297 static void
2298 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2299 {
2300 complaint (&symfile_complaints,
2301 _("invalid attribute class or form for '%s' in '%s'"),
2302 arg1, arg2);
2303 }
2304
2305 /* Hash function for line_header_hash. */
2306
2307 static hashval_t
2308 line_header_hash (const struct line_header *ofs)
2309 {
2310 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2311 }
2312
2313 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2314
2315 static hashval_t
2316 line_header_hash_voidp (const void *item)
2317 {
2318 const struct line_header *ofs = (const struct line_header *) item;
2319
2320 return line_header_hash (ofs);
2321 }
2322
2323 /* Equality function for line_header_hash. */
2324
2325 static int
2326 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2327 {
2328 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2329 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2330
2331 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2332 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2333 }
2334
2335 \f
2336
2337 /* Read the given attribute value as an address, taking the attribute's
2338 form into account. */
2339
2340 static CORE_ADDR
2341 attr_value_as_address (struct attribute *attr)
2342 {
2343 CORE_ADDR addr;
2344
2345 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2346 {
2347 /* Aside from a few clearly defined exceptions, attributes that
2348 contain an address must always be in DW_FORM_addr form.
2349 Unfortunately, some compilers happen to be violating this
2350 requirement by encoding addresses using other forms, such
2351 as DW_FORM_data4 for example. For those broken compilers,
2352 we try to do our best, without any guarantee of success,
2353 to interpret the address correctly. It would also be nice
2354 to generate a complaint, but that would require us to maintain
2355 a list of legitimate cases where a non-address form is allowed,
2356 as well as update callers to pass in at least the CU's DWARF
2357 version. This is more overhead than what we're willing to
2358 expand for a pretty rare case. */
2359 addr = DW_UNSND (attr);
2360 }
2361 else
2362 addr = DW_ADDR (attr);
2363
2364 return addr;
2365 }
2366
2367 /* The suffix for an index file. */
2368 #define INDEX4_SUFFIX ".gdb-index"
2369 #define INDEX5_SUFFIX ".debug_names"
2370 #define DEBUG_STR_SUFFIX ".debug_str"
2371
2372 /* See declaration. */
2373
2374 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2375 const dwarf2_debug_sections *names)
2376 : objfile (objfile_)
2377 {
2378 if (names == NULL)
2379 names = &dwarf2_elf_names;
2380
2381 bfd *obfd = objfile->obfd;
2382
2383 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2384 locate_sections (obfd, sec, *names);
2385 }
2386
2387 dwarf2_per_objfile::~dwarf2_per_objfile ()
2388 {
2389 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2390 free_cached_comp_units ();
2391
2392 if (quick_file_names_table)
2393 htab_delete (quick_file_names_table);
2394
2395 if (line_header_hash)
2396 htab_delete (line_header_hash);
2397
2398 /* Everything else should be on the objfile obstack. */
2399 }
2400
2401 /* See declaration. */
2402
2403 void
2404 dwarf2_per_objfile::free_cached_comp_units ()
2405 {
2406 dwarf2_per_cu_data *per_cu = read_in_chain;
2407 dwarf2_per_cu_data **last_chain = &read_in_chain;
2408 while (per_cu != NULL)
2409 {
2410 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2411
2412 free_heap_comp_unit (per_cu->cu);
2413 *last_chain = next_cu;
2414 per_cu = next_cu;
2415 }
2416 }
2417
2418 /* Try to locate the sections we need for DWARF 2 debugging
2419 information and return true if we have enough to do something.
2420 NAMES points to the dwarf2 section names, or is NULL if the standard
2421 ELF names are used. */
2422
2423 int
2424 dwarf2_has_info (struct objfile *objfile,
2425 const struct dwarf2_debug_sections *names)
2426 {
2427 if (objfile->flags & OBJF_READNEVER)
2428 return 0;
2429
2430 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2431 objfile_data (objfile, dwarf2_objfile_data_key));
2432 if (!dwarf2_per_objfile)
2433 {
2434 /* Initialize per-objfile state. */
2435 struct dwarf2_per_objfile *data
2436 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
2437
2438 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2439 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
2440 }
2441 return (!dwarf2_per_objfile->info.is_virtual
2442 && dwarf2_per_objfile->info.s.section != NULL
2443 && !dwarf2_per_objfile->abbrev.is_virtual
2444 && dwarf2_per_objfile->abbrev.s.section != NULL);
2445 }
2446
2447 /* Return the containing section of virtual section SECTION. */
2448
2449 static struct dwarf2_section_info *
2450 get_containing_section (const struct dwarf2_section_info *section)
2451 {
2452 gdb_assert (section->is_virtual);
2453 return section->s.containing_section;
2454 }
2455
2456 /* Return the bfd owner of SECTION. */
2457
2458 static struct bfd *
2459 get_section_bfd_owner (const struct dwarf2_section_info *section)
2460 {
2461 if (section->is_virtual)
2462 {
2463 section = get_containing_section (section);
2464 gdb_assert (!section->is_virtual);
2465 }
2466 return section->s.section->owner;
2467 }
2468
2469 /* Return the bfd section of SECTION.
2470 Returns NULL if the section is not present. */
2471
2472 static asection *
2473 get_section_bfd_section (const struct dwarf2_section_info *section)
2474 {
2475 if (section->is_virtual)
2476 {
2477 section = get_containing_section (section);
2478 gdb_assert (!section->is_virtual);
2479 }
2480 return section->s.section;
2481 }
2482
2483 /* Return the name of SECTION. */
2484
2485 static const char *
2486 get_section_name (const struct dwarf2_section_info *section)
2487 {
2488 asection *sectp = get_section_bfd_section (section);
2489
2490 gdb_assert (sectp != NULL);
2491 return bfd_section_name (get_section_bfd_owner (section), sectp);
2492 }
2493
2494 /* Return the name of the file SECTION is in. */
2495
2496 static const char *
2497 get_section_file_name (const struct dwarf2_section_info *section)
2498 {
2499 bfd *abfd = get_section_bfd_owner (section);
2500
2501 return bfd_get_filename (abfd);
2502 }
2503
2504 /* Return the id of SECTION.
2505 Returns 0 if SECTION doesn't exist. */
2506
2507 static int
2508 get_section_id (const struct dwarf2_section_info *section)
2509 {
2510 asection *sectp = get_section_bfd_section (section);
2511
2512 if (sectp == NULL)
2513 return 0;
2514 return sectp->id;
2515 }
2516
2517 /* Return the flags of SECTION.
2518 SECTION (or containing section if this is a virtual section) must exist. */
2519
2520 static int
2521 get_section_flags (const struct dwarf2_section_info *section)
2522 {
2523 asection *sectp = get_section_bfd_section (section);
2524
2525 gdb_assert (sectp != NULL);
2526 return bfd_get_section_flags (sectp->owner, sectp);
2527 }
2528
2529 /* When loading sections, we look either for uncompressed section or for
2530 compressed section names. */
2531
2532 static int
2533 section_is_p (const char *section_name,
2534 const struct dwarf2_section_names *names)
2535 {
2536 if (names->normal != NULL
2537 && strcmp (section_name, names->normal) == 0)
2538 return 1;
2539 if (names->compressed != NULL
2540 && strcmp (section_name, names->compressed) == 0)
2541 return 1;
2542 return 0;
2543 }
2544
2545 /* See declaration. */
2546
2547 void
2548 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2549 const dwarf2_debug_sections &names)
2550 {
2551 flagword aflag = bfd_get_section_flags (abfd, sectp);
2552
2553 if ((aflag & SEC_HAS_CONTENTS) == 0)
2554 {
2555 }
2556 else if (section_is_p (sectp->name, &names.info))
2557 {
2558 this->info.s.section = sectp;
2559 this->info.size = bfd_get_section_size (sectp);
2560 }
2561 else if (section_is_p (sectp->name, &names.abbrev))
2562 {
2563 this->abbrev.s.section = sectp;
2564 this->abbrev.size = bfd_get_section_size (sectp);
2565 }
2566 else if (section_is_p (sectp->name, &names.line))
2567 {
2568 this->line.s.section = sectp;
2569 this->line.size = bfd_get_section_size (sectp);
2570 }
2571 else if (section_is_p (sectp->name, &names.loc))
2572 {
2573 this->loc.s.section = sectp;
2574 this->loc.size = bfd_get_section_size (sectp);
2575 }
2576 else if (section_is_p (sectp->name, &names.loclists))
2577 {
2578 this->loclists.s.section = sectp;
2579 this->loclists.size = bfd_get_section_size (sectp);
2580 }
2581 else if (section_is_p (sectp->name, &names.macinfo))
2582 {
2583 this->macinfo.s.section = sectp;
2584 this->macinfo.size = bfd_get_section_size (sectp);
2585 }
2586 else if (section_is_p (sectp->name, &names.macro))
2587 {
2588 this->macro.s.section = sectp;
2589 this->macro.size = bfd_get_section_size (sectp);
2590 }
2591 else if (section_is_p (sectp->name, &names.str))
2592 {
2593 this->str.s.section = sectp;
2594 this->str.size = bfd_get_section_size (sectp);
2595 }
2596 else if (section_is_p (sectp->name, &names.line_str))
2597 {
2598 this->line_str.s.section = sectp;
2599 this->line_str.size = bfd_get_section_size (sectp);
2600 }
2601 else if (section_is_p (sectp->name, &names.addr))
2602 {
2603 this->addr.s.section = sectp;
2604 this->addr.size = bfd_get_section_size (sectp);
2605 }
2606 else if (section_is_p (sectp->name, &names.frame))
2607 {
2608 this->frame.s.section = sectp;
2609 this->frame.size = bfd_get_section_size (sectp);
2610 }
2611 else if (section_is_p (sectp->name, &names.eh_frame))
2612 {
2613 this->eh_frame.s.section = sectp;
2614 this->eh_frame.size = bfd_get_section_size (sectp);
2615 }
2616 else if (section_is_p (sectp->name, &names.ranges))
2617 {
2618 this->ranges.s.section = sectp;
2619 this->ranges.size = bfd_get_section_size (sectp);
2620 }
2621 else if (section_is_p (sectp->name, &names.rnglists))
2622 {
2623 this->rnglists.s.section = sectp;
2624 this->rnglists.size = bfd_get_section_size (sectp);
2625 }
2626 else if (section_is_p (sectp->name, &names.types))
2627 {
2628 struct dwarf2_section_info type_section;
2629
2630 memset (&type_section, 0, sizeof (type_section));
2631 type_section.s.section = sectp;
2632 type_section.size = bfd_get_section_size (sectp);
2633
2634 VEC_safe_push (dwarf2_section_info_def, this->types,
2635 &type_section);
2636 }
2637 else if (section_is_p (sectp->name, &names.gdb_index))
2638 {
2639 this->gdb_index.s.section = sectp;
2640 this->gdb_index.size = bfd_get_section_size (sectp);
2641 }
2642 else if (section_is_p (sectp->name, &names.debug_names))
2643 {
2644 this->debug_names.s.section = sectp;
2645 this->debug_names.size = bfd_get_section_size (sectp);
2646 }
2647 else if (section_is_p (sectp->name, &names.debug_aranges))
2648 {
2649 this->debug_aranges.s.section = sectp;
2650 this->debug_aranges.size = bfd_get_section_size (sectp);
2651 }
2652
2653 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2654 && bfd_section_vma (abfd, sectp) == 0)
2655 this->has_section_at_zero = true;
2656 }
2657
2658 /* A helper function that decides whether a section is empty,
2659 or not present. */
2660
2661 static int
2662 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2663 {
2664 if (section->is_virtual)
2665 return section->size == 0;
2666 return section->s.section == NULL || section->size == 0;
2667 }
2668
2669 /* Read the contents of the section INFO.
2670 OBJFILE is the main object file, but not necessarily the file where
2671 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2672 of the DWO file.
2673 If the section is compressed, uncompress it before returning. */
2674
2675 static void
2676 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2677 {
2678 asection *sectp;
2679 bfd *abfd;
2680 gdb_byte *buf, *retbuf;
2681
2682 if (info->readin)
2683 return;
2684 info->buffer = NULL;
2685 info->readin = 1;
2686
2687 if (dwarf2_section_empty_p (info))
2688 return;
2689
2690 sectp = get_section_bfd_section (info);
2691
2692 /* If this is a virtual section we need to read in the real one first. */
2693 if (info->is_virtual)
2694 {
2695 struct dwarf2_section_info *containing_section =
2696 get_containing_section (info);
2697
2698 gdb_assert (sectp != NULL);
2699 if ((sectp->flags & SEC_RELOC) != 0)
2700 {
2701 error (_("Dwarf Error: DWP format V2 with relocations is not"
2702 " supported in section %s [in module %s]"),
2703 get_section_name (info), get_section_file_name (info));
2704 }
2705 dwarf2_read_section (objfile, containing_section);
2706 /* Other code should have already caught virtual sections that don't
2707 fit. */
2708 gdb_assert (info->virtual_offset + info->size
2709 <= containing_section->size);
2710 /* If the real section is empty or there was a problem reading the
2711 section we shouldn't get here. */
2712 gdb_assert (containing_section->buffer != NULL);
2713 info->buffer = containing_section->buffer + info->virtual_offset;
2714 return;
2715 }
2716
2717 /* If the section has relocations, we must read it ourselves.
2718 Otherwise we attach it to the BFD. */
2719 if ((sectp->flags & SEC_RELOC) == 0)
2720 {
2721 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2722 return;
2723 }
2724
2725 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2726 info->buffer = buf;
2727
2728 /* When debugging .o files, we may need to apply relocations; see
2729 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2730 We never compress sections in .o files, so we only need to
2731 try this when the section is not compressed. */
2732 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2733 if (retbuf != NULL)
2734 {
2735 info->buffer = retbuf;
2736 return;
2737 }
2738
2739 abfd = get_section_bfd_owner (info);
2740 gdb_assert (abfd != NULL);
2741
2742 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2743 || bfd_bread (buf, info->size, abfd) != info->size)
2744 {
2745 error (_("Dwarf Error: Can't read DWARF data"
2746 " in section %s [in module %s]"),
2747 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2748 }
2749 }
2750
2751 /* A helper function that returns the size of a section in a safe way.
2752 If you are positive that the section has been read before using the
2753 size, then it is safe to refer to the dwarf2_section_info object's
2754 "size" field directly. In other cases, you must call this
2755 function, because for compressed sections the size field is not set
2756 correctly until the section has been read. */
2757
2758 static bfd_size_type
2759 dwarf2_section_size (struct objfile *objfile,
2760 struct dwarf2_section_info *info)
2761 {
2762 if (!info->readin)
2763 dwarf2_read_section (objfile, info);
2764 return info->size;
2765 }
2766
2767 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2768 SECTION_NAME. */
2769
2770 void
2771 dwarf2_get_section_info (struct objfile *objfile,
2772 enum dwarf2_section_enum sect,
2773 asection **sectp, const gdb_byte **bufp,
2774 bfd_size_type *sizep)
2775 {
2776 struct dwarf2_per_objfile *data
2777 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2778 dwarf2_objfile_data_key);
2779 struct dwarf2_section_info *info;
2780
2781 /* We may see an objfile without any DWARF, in which case we just
2782 return nothing. */
2783 if (data == NULL)
2784 {
2785 *sectp = NULL;
2786 *bufp = NULL;
2787 *sizep = 0;
2788 return;
2789 }
2790 switch (sect)
2791 {
2792 case DWARF2_DEBUG_FRAME:
2793 info = &data->frame;
2794 break;
2795 case DWARF2_EH_FRAME:
2796 info = &data->eh_frame;
2797 break;
2798 default:
2799 gdb_assert_not_reached ("unexpected section");
2800 }
2801
2802 dwarf2_read_section (objfile, info);
2803
2804 *sectp = get_section_bfd_section (info);
2805 *bufp = info->buffer;
2806 *sizep = info->size;
2807 }
2808
2809 /* A helper function to find the sections for a .dwz file. */
2810
2811 static void
2812 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2813 {
2814 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2815
2816 /* Note that we only support the standard ELF names, because .dwz
2817 is ELF-only (at the time of writing). */
2818 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2819 {
2820 dwz_file->abbrev.s.section = sectp;
2821 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2822 }
2823 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2824 {
2825 dwz_file->info.s.section = sectp;
2826 dwz_file->info.size = bfd_get_section_size (sectp);
2827 }
2828 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2829 {
2830 dwz_file->str.s.section = sectp;
2831 dwz_file->str.size = bfd_get_section_size (sectp);
2832 }
2833 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2834 {
2835 dwz_file->line.s.section = sectp;
2836 dwz_file->line.size = bfd_get_section_size (sectp);
2837 }
2838 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2839 {
2840 dwz_file->macro.s.section = sectp;
2841 dwz_file->macro.size = bfd_get_section_size (sectp);
2842 }
2843 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2844 {
2845 dwz_file->gdb_index.s.section = sectp;
2846 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2847 }
2848 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2849 {
2850 dwz_file->debug_names.s.section = sectp;
2851 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2852 }
2853 }
2854
2855 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2856 there is no .gnu_debugaltlink section in the file. Error if there
2857 is such a section but the file cannot be found. */
2858
2859 static struct dwz_file *
2860 dwarf2_get_dwz_file (void)
2861 {
2862 const char *filename;
2863 struct dwz_file *result;
2864 bfd_size_type buildid_len_arg;
2865 size_t buildid_len;
2866 bfd_byte *buildid;
2867
2868 if (dwarf2_per_objfile->dwz_file != NULL)
2869 return dwarf2_per_objfile->dwz_file;
2870
2871 bfd_set_error (bfd_error_no_error);
2872 gdb::unique_xmalloc_ptr<char> data
2873 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2874 &buildid_len_arg, &buildid));
2875 if (data == NULL)
2876 {
2877 if (bfd_get_error () == bfd_error_no_error)
2878 return NULL;
2879 error (_("could not read '.gnu_debugaltlink' section: %s"),
2880 bfd_errmsg (bfd_get_error ()));
2881 }
2882
2883 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2884
2885 buildid_len = (size_t) buildid_len_arg;
2886
2887 filename = data.get ();
2888
2889 std::string abs_storage;
2890 if (!IS_ABSOLUTE_PATH (filename))
2891 {
2892 gdb::unique_xmalloc_ptr<char> abs
2893 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2894
2895 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2896 filename = abs_storage.c_str ();
2897 }
2898
2899 /* First try the file name given in the section. If that doesn't
2900 work, try to use the build-id instead. */
2901 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2902 if (dwz_bfd != NULL)
2903 {
2904 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2905 dwz_bfd.release ();
2906 }
2907
2908 if (dwz_bfd == NULL)
2909 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2910
2911 if (dwz_bfd == NULL)
2912 error (_("could not find '.gnu_debugaltlink' file for %s"),
2913 objfile_name (dwarf2_per_objfile->objfile));
2914
2915 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2916 struct dwz_file);
2917 result->dwz_bfd = dwz_bfd.release ();
2918
2919 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2920
2921 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2922 dwarf2_per_objfile->dwz_file = result;
2923 return result;
2924 }
2925 \f
2926 /* DWARF quick_symbols_functions support. */
2927
2928 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2929 unique line tables, so we maintain a separate table of all .debug_line
2930 derived entries to support the sharing.
2931 All the quick functions need is the list of file names. We discard the
2932 line_header when we're done and don't need to record it here. */
2933 struct quick_file_names
2934 {
2935 /* The data used to construct the hash key. */
2936 struct stmt_list_hash hash;
2937
2938 /* The number of entries in file_names, real_names. */
2939 unsigned int num_file_names;
2940
2941 /* The file names from the line table, after being run through
2942 file_full_name. */
2943 const char **file_names;
2944
2945 /* The file names from the line table after being run through
2946 gdb_realpath. These are computed lazily. */
2947 const char **real_names;
2948 };
2949
2950 /* When using the index (and thus not using psymtabs), each CU has an
2951 object of this type. This is used to hold information needed by
2952 the various "quick" methods. */
2953 struct dwarf2_per_cu_quick_data
2954 {
2955 /* The file table. This can be NULL if there was no file table
2956 or it's currently not read in.
2957 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2958 struct quick_file_names *file_names;
2959
2960 /* The corresponding symbol table. This is NULL if symbols for this
2961 CU have not yet been read. */
2962 struct compunit_symtab *compunit_symtab;
2963
2964 /* A temporary mark bit used when iterating over all CUs in
2965 expand_symtabs_matching. */
2966 unsigned int mark : 1;
2967
2968 /* True if we've tried to read the file table and found there isn't one.
2969 There will be no point in trying to read it again next time. */
2970 unsigned int no_file_data : 1;
2971 };
2972
2973 /* Utility hash function for a stmt_list_hash. */
2974
2975 static hashval_t
2976 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2977 {
2978 hashval_t v = 0;
2979
2980 if (stmt_list_hash->dwo_unit != NULL)
2981 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2982 v += to_underlying (stmt_list_hash->line_sect_off);
2983 return v;
2984 }
2985
2986 /* Utility equality function for a stmt_list_hash. */
2987
2988 static int
2989 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2990 const struct stmt_list_hash *rhs)
2991 {
2992 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2993 return 0;
2994 if (lhs->dwo_unit != NULL
2995 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2996 return 0;
2997
2998 return lhs->line_sect_off == rhs->line_sect_off;
2999 }
3000
3001 /* Hash function for a quick_file_names. */
3002
3003 static hashval_t
3004 hash_file_name_entry (const void *e)
3005 {
3006 const struct quick_file_names *file_data
3007 = (const struct quick_file_names *) e;
3008
3009 return hash_stmt_list_entry (&file_data->hash);
3010 }
3011
3012 /* Equality function for a quick_file_names. */
3013
3014 static int
3015 eq_file_name_entry (const void *a, const void *b)
3016 {
3017 const struct quick_file_names *ea = (const struct quick_file_names *) a;
3018 const struct quick_file_names *eb = (const struct quick_file_names *) b;
3019
3020 return eq_stmt_list_entry (&ea->hash, &eb->hash);
3021 }
3022
3023 /* Delete function for a quick_file_names. */
3024
3025 static void
3026 delete_file_name_entry (void *e)
3027 {
3028 struct quick_file_names *file_data = (struct quick_file_names *) e;
3029 int i;
3030
3031 for (i = 0; i < file_data->num_file_names; ++i)
3032 {
3033 xfree ((void*) file_data->file_names[i]);
3034 if (file_data->real_names)
3035 xfree ((void*) file_data->real_names[i]);
3036 }
3037
3038 /* The space for the struct itself lives on objfile_obstack,
3039 so we don't free it here. */
3040 }
3041
3042 /* Create a quick_file_names hash table. */
3043
3044 static htab_t
3045 create_quick_file_names_table (unsigned int nr_initial_entries)
3046 {
3047 return htab_create_alloc (nr_initial_entries,
3048 hash_file_name_entry, eq_file_name_entry,
3049 delete_file_name_entry, xcalloc, xfree);
3050 }
3051
3052 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3053 have to be created afterwards. You should call age_cached_comp_units after
3054 processing PER_CU->CU. dw2_setup must have been already called. */
3055
3056 static void
3057 load_cu (struct dwarf2_per_cu_data *per_cu)
3058 {
3059 if (per_cu->is_debug_types)
3060 load_full_type_unit (per_cu);
3061 else
3062 load_full_comp_unit (per_cu, language_minimal);
3063
3064 if (per_cu->cu == NULL)
3065 return; /* Dummy CU. */
3066
3067 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
3068 }
3069
3070 /* Read in the symbols for PER_CU. */
3071
3072 static void
3073 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3074 {
3075 struct cleanup *back_to;
3076
3077 /* Skip type_unit_groups, reading the type units they contain
3078 is handled elsewhere. */
3079 if (IS_TYPE_UNIT_GROUP (per_cu))
3080 return;
3081
3082 back_to = make_cleanup (dwarf2_release_queue, NULL);
3083
3084 if (dwarf2_per_objfile->using_index
3085 ? per_cu->v.quick->compunit_symtab == NULL
3086 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3087 {
3088 queue_comp_unit (per_cu, language_minimal);
3089 load_cu (per_cu);
3090
3091 /* If we just loaded a CU from a DWO, and we're working with an index
3092 that may badly handle TUs, load all the TUs in that DWO as well.
3093 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3094 if (!per_cu->is_debug_types
3095 && per_cu->cu != NULL
3096 && per_cu->cu->dwo_unit != NULL
3097 && dwarf2_per_objfile->index_table != NULL
3098 && dwarf2_per_objfile->index_table->version <= 7
3099 /* DWP files aren't supported yet. */
3100 && get_dwp_file () == NULL)
3101 queue_and_load_all_dwo_tus (per_cu);
3102 }
3103
3104 process_queue ();
3105
3106 /* Age the cache, releasing compilation units that have not
3107 been used recently. */
3108 age_cached_comp_units ();
3109
3110 do_cleanups (back_to);
3111 }
3112
3113 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3114 the objfile from which this CU came. Returns the resulting symbol
3115 table. */
3116
3117 static struct compunit_symtab *
3118 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3119 {
3120 gdb_assert (dwarf2_per_objfile->using_index);
3121 if (!per_cu->v.quick->compunit_symtab)
3122 {
3123 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
3124 scoped_restore decrementer = increment_reading_symtab ();
3125 dw2_do_instantiate_symtab (per_cu);
3126 process_cu_includes ();
3127 do_cleanups (back_to);
3128 }
3129
3130 return per_cu->v.quick->compunit_symtab;
3131 }
3132
3133 /* Return the CU/TU given its index.
3134
3135 This is intended for loops like:
3136
3137 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3138 + dwarf2_per_objfile->n_type_units); ++i)
3139 {
3140 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3141
3142 ...;
3143 }
3144 */
3145
3146 static struct dwarf2_per_cu_data *
3147 dw2_get_cutu (int index)
3148 {
3149 if (index >= dwarf2_per_objfile->n_comp_units)
3150 {
3151 index -= dwarf2_per_objfile->n_comp_units;
3152 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3153 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
3154 }
3155
3156 return dwarf2_per_objfile->all_comp_units[index];
3157 }
3158
3159 /* Return the CU given its index.
3160 This differs from dw2_get_cutu in that it's for when you know INDEX
3161 refers to a CU. */
3162
3163 static struct dwarf2_per_cu_data *
3164 dw2_get_cu (int index)
3165 {
3166 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
3167
3168 return dwarf2_per_objfile->all_comp_units[index];
3169 }
3170
3171 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3172 objfile_obstack, and constructed with the specified field
3173 values. */
3174
3175 static dwarf2_per_cu_data *
3176 create_cu_from_index_list (struct objfile *objfile,
3177 struct dwarf2_section_info *section,
3178 int is_dwz,
3179 sect_offset sect_off, ULONGEST length)
3180 {
3181 dwarf2_per_cu_data *the_cu
3182 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3183 struct dwarf2_per_cu_data);
3184 the_cu->sect_off = sect_off;
3185 the_cu->length = length;
3186 the_cu->objfile = objfile;
3187 the_cu->section = section;
3188 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3189 struct dwarf2_per_cu_quick_data);
3190 the_cu->is_dwz = is_dwz;
3191 return the_cu;
3192 }
3193
3194 /* A helper for create_cus_from_index that handles a given list of
3195 CUs. */
3196
3197 static void
3198 create_cus_from_index_list (struct objfile *objfile,
3199 const gdb_byte *cu_list, offset_type n_elements,
3200 struct dwarf2_section_info *section,
3201 int is_dwz,
3202 int base_offset)
3203 {
3204 offset_type i;
3205
3206 for (i = 0; i < n_elements; i += 2)
3207 {
3208 gdb_static_assert (sizeof (ULONGEST) >= 8);
3209
3210 sect_offset sect_off
3211 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3212 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3213 cu_list += 2 * 8;
3214
3215 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
3216 = create_cu_from_index_list (objfile, section, is_dwz, sect_off, length);
3217 }
3218 }
3219
3220 /* Read the CU list from the mapped index, and use it to create all
3221 the CU objects for this objfile. */
3222
3223 static void
3224 create_cus_from_index (struct objfile *objfile,
3225 const gdb_byte *cu_list, offset_type cu_list_elements,
3226 const gdb_byte *dwz_list, offset_type dwz_elements)
3227 {
3228 struct dwz_file *dwz;
3229
3230 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3231 dwarf2_per_objfile->all_comp_units =
3232 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3233 dwarf2_per_objfile->n_comp_units);
3234
3235 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3236 &dwarf2_per_objfile->info, 0, 0);
3237
3238 if (dwz_elements == 0)
3239 return;
3240
3241 dwz = dwarf2_get_dwz_file ();
3242 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3243 cu_list_elements / 2);
3244 }
3245
3246 /* Create the signatured type hash table from the index. */
3247
3248 static void
3249 create_signatured_type_table_from_index (struct objfile *objfile,
3250 struct dwarf2_section_info *section,
3251 const gdb_byte *bytes,
3252 offset_type elements)
3253 {
3254 offset_type i;
3255 htab_t sig_types_hash;
3256
3257 dwarf2_per_objfile->n_type_units
3258 = dwarf2_per_objfile->n_allocated_type_units
3259 = elements / 3;
3260 dwarf2_per_objfile->all_type_units =
3261 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3262
3263 sig_types_hash = allocate_signatured_type_table (objfile);
3264
3265 for (i = 0; i < elements; i += 3)
3266 {
3267 struct signatured_type *sig_type;
3268 ULONGEST signature;
3269 void **slot;
3270 cu_offset type_offset_in_tu;
3271
3272 gdb_static_assert (sizeof (ULONGEST) >= 8);
3273 sect_offset sect_off
3274 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3275 type_offset_in_tu
3276 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3277 BFD_ENDIAN_LITTLE);
3278 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3279 bytes += 3 * 8;
3280
3281 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3282 struct signatured_type);
3283 sig_type->signature = signature;
3284 sig_type->type_offset_in_tu = type_offset_in_tu;
3285 sig_type->per_cu.is_debug_types = 1;
3286 sig_type->per_cu.section = section;
3287 sig_type->per_cu.sect_off = sect_off;
3288 sig_type->per_cu.objfile = objfile;
3289 sig_type->per_cu.v.quick
3290 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3291 struct dwarf2_per_cu_quick_data);
3292
3293 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3294 *slot = sig_type;
3295
3296 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3297 }
3298
3299 dwarf2_per_objfile->signatured_types = sig_types_hash;
3300 }
3301
3302 /* Create the signatured type hash table from .debug_names. */
3303
3304 static void
3305 create_signatured_type_table_from_debug_names
3306 (struct objfile *objfile,
3307 const mapped_debug_names &map,
3308 struct dwarf2_section_info *section,
3309 struct dwarf2_section_info *abbrev_section)
3310 {
3311 dwarf2_read_section (objfile, section);
3312 dwarf2_read_section (objfile, abbrev_section);
3313
3314 dwarf2_per_objfile->n_type_units
3315 = dwarf2_per_objfile->n_allocated_type_units
3316 = map.tu_count;
3317 dwarf2_per_objfile->all_type_units
3318 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3319
3320 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3321
3322 for (uint32_t i = 0; i < map.tu_count; ++i)
3323 {
3324 struct signatured_type *sig_type;
3325 ULONGEST signature;
3326 void **slot;
3327 cu_offset type_offset_in_tu;
3328
3329 sect_offset sect_off
3330 = (sect_offset) (extract_unsigned_integer
3331 (map.tu_table_reordered + i * map.offset_size,
3332 map.offset_size,
3333 map.dwarf5_byte_order));
3334
3335 comp_unit_head cu_header;
3336 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
3337 section->buffer + to_underlying (sect_off),
3338 rcuh_kind::TYPE);
3339
3340 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3341 struct signatured_type);
3342 sig_type->signature = cu_header.signature;
3343 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3344 sig_type->per_cu.is_debug_types = 1;
3345 sig_type->per_cu.section = section;
3346 sig_type->per_cu.sect_off = sect_off;
3347 sig_type->per_cu.objfile = objfile;
3348 sig_type->per_cu.v.quick
3349 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3350 struct dwarf2_per_cu_quick_data);
3351
3352 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3353 *slot = sig_type;
3354
3355 dwarf2_per_objfile->all_type_units[i] = sig_type;
3356 }
3357
3358 dwarf2_per_objfile->signatured_types = sig_types_hash;
3359 }
3360
3361 /* Read the address map data from the mapped index, and use it to
3362 populate the objfile's psymtabs_addrmap. */
3363
3364 static void
3365 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3366 {
3367 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3368 const gdb_byte *iter, *end;
3369 struct addrmap *mutable_map;
3370 CORE_ADDR baseaddr;
3371
3372 auto_obstack temp_obstack;
3373
3374 mutable_map = addrmap_create_mutable (&temp_obstack);
3375
3376 iter = index->address_table.data ();
3377 end = iter + index->address_table.size ();
3378
3379 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3380
3381 while (iter < end)
3382 {
3383 ULONGEST hi, lo, cu_index;
3384 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3385 iter += 8;
3386 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3387 iter += 8;
3388 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3389 iter += 4;
3390
3391 if (lo > hi)
3392 {
3393 complaint (&symfile_complaints,
3394 _(".gdb_index address table has invalid range (%s - %s)"),
3395 hex_string (lo), hex_string (hi));
3396 continue;
3397 }
3398
3399 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3400 {
3401 complaint (&symfile_complaints,
3402 _(".gdb_index address table has invalid CU number %u"),
3403 (unsigned) cu_index);
3404 continue;
3405 }
3406
3407 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3408 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3409 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
3410 }
3411
3412 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3413 &objfile->objfile_obstack);
3414 }
3415
3416 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3417 populate the objfile's psymtabs_addrmap. */
3418
3419 static void
3420 create_addrmap_from_aranges (struct objfile *objfile,
3421 struct dwarf2_section_info *section)
3422 {
3423 bfd *abfd = objfile->obfd;
3424 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3425 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3426 SECT_OFF_TEXT (objfile));
3427
3428 auto_obstack temp_obstack;
3429 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3430
3431 std::unordered_map<sect_offset,
3432 dwarf2_per_cu_data *,
3433 gdb::hash_enum<sect_offset>>
3434 debug_info_offset_to_per_cu;
3435 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3436 {
3437 dwarf2_per_cu_data *per_cu = dw2_get_cutu (cui);
3438 const auto insertpair
3439 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3440 if (!insertpair.second)
3441 {
3442 warning (_("Section .debug_aranges in %s has duplicate "
3443 "debug_info_offset %u, ignoring .debug_aranges."),
3444 objfile_name (objfile), to_underlying (per_cu->sect_off));
3445 return;
3446 }
3447 }
3448
3449 dwarf2_read_section (objfile, section);
3450
3451 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3452
3453 const gdb_byte *addr = section->buffer;
3454
3455 while (addr < section->buffer + section->size)
3456 {
3457 const gdb_byte *const entry_addr = addr;
3458 unsigned int bytes_read;
3459
3460 const LONGEST entry_length = read_initial_length (abfd, addr,
3461 &bytes_read);
3462 addr += bytes_read;
3463
3464 const gdb_byte *const entry_end = addr + entry_length;
3465 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3466 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3467 if (addr + entry_length > section->buffer + section->size)
3468 {
3469 warning (_("Section .debug_aranges in %s entry at offset %zu "
3470 "length %s exceeds section length %s, "
3471 "ignoring .debug_aranges."),
3472 objfile_name (objfile), entry_addr - section->buffer,
3473 plongest (bytes_read + entry_length),
3474 pulongest (section->size));
3475 return;
3476 }
3477
3478 /* The version number. */
3479 const uint16_t version = read_2_bytes (abfd, addr);
3480 addr += 2;
3481 if (version != 2)
3482 {
3483 warning (_("Section .debug_aranges in %s entry at offset %zu "
3484 "has unsupported version %d, ignoring .debug_aranges."),
3485 objfile_name (objfile), entry_addr - section->buffer,
3486 version);
3487 return;
3488 }
3489
3490 const uint64_t debug_info_offset
3491 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3492 addr += offset_size;
3493 const auto per_cu_it
3494 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3495 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3496 {
3497 warning (_("Section .debug_aranges in %s entry at offset %zu "
3498 "debug_info_offset %s does not exists, "
3499 "ignoring .debug_aranges."),
3500 objfile_name (objfile), entry_addr - section->buffer,
3501 pulongest (debug_info_offset));
3502 return;
3503 }
3504 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3505
3506 const uint8_t address_size = *addr++;
3507 if (address_size < 1 || address_size > 8)
3508 {
3509 warning (_("Section .debug_aranges in %s entry at offset %zu "
3510 "address_size %u is invalid, ignoring .debug_aranges."),
3511 objfile_name (objfile), entry_addr - section->buffer,
3512 address_size);
3513 return;
3514 }
3515
3516 const uint8_t segment_selector_size = *addr++;
3517 if (segment_selector_size != 0)
3518 {
3519 warning (_("Section .debug_aranges in %s entry at offset %zu "
3520 "segment_selector_size %u is not supported, "
3521 "ignoring .debug_aranges."),
3522 objfile_name (objfile), entry_addr - section->buffer,
3523 segment_selector_size);
3524 return;
3525 }
3526
3527 /* Must pad to an alignment boundary that is twice the address
3528 size. It is undocumented by the DWARF standard but GCC does
3529 use it. */
3530 for (size_t padding = ((-(addr - section->buffer))
3531 & (2 * address_size - 1));
3532 padding > 0; padding--)
3533 if (*addr++ != 0)
3534 {
3535 warning (_("Section .debug_aranges in %s entry at offset %zu "
3536 "padding is not zero, ignoring .debug_aranges."),
3537 objfile_name (objfile), entry_addr - section->buffer);
3538 return;
3539 }
3540
3541 for (;;)
3542 {
3543 if (addr + 2 * address_size > entry_end)
3544 {
3545 warning (_("Section .debug_aranges in %s entry at offset %zu "
3546 "address list is not properly terminated, "
3547 "ignoring .debug_aranges."),
3548 objfile_name (objfile), entry_addr - section->buffer);
3549 return;
3550 }
3551 ULONGEST start = extract_unsigned_integer (addr, address_size,
3552 dwarf5_byte_order);
3553 addr += address_size;
3554 ULONGEST length = extract_unsigned_integer (addr, address_size,
3555 dwarf5_byte_order);
3556 addr += address_size;
3557 if (start == 0 && length == 0)
3558 break;
3559 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3560 {
3561 /* Symbol was eliminated due to a COMDAT group. */
3562 continue;
3563 }
3564 ULONGEST end = start + length;
3565 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3566 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3567 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3568 }
3569 }
3570
3571 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3572 &objfile->objfile_obstack);
3573 }
3574
3575 /* The hash function for strings in the mapped index. This is the same as
3576 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3577 implementation. This is necessary because the hash function is tied to the
3578 format of the mapped index file. The hash values do not have to match with
3579 SYMBOL_HASH_NEXT.
3580
3581 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3582
3583 static hashval_t
3584 mapped_index_string_hash (int index_version, const void *p)
3585 {
3586 const unsigned char *str = (const unsigned char *) p;
3587 hashval_t r = 0;
3588 unsigned char c;
3589
3590 while ((c = *str++) != 0)
3591 {
3592 if (index_version >= 5)
3593 c = tolower (c);
3594 r = r * 67 + c - 113;
3595 }
3596
3597 return r;
3598 }
3599
3600 /* Find a slot in the mapped index INDEX for the object named NAME.
3601 If NAME is found, set *VEC_OUT to point to the CU vector in the
3602 constant pool and return true. If NAME cannot be found, return
3603 false. */
3604
3605 static bool
3606 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3607 offset_type **vec_out)
3608 {
3609 offset_type hash;
3610 offset_type slot, step;
3611 int (*cmp) (const char *, const char *);
3612
3613 gdb::unique_xmalloc_ptr<char> without_params;
3614 if (current_language->la_language == language_cplus
3615 || current_language->la_language == language_fortran
3616 || current_language->la_language == language_d)
3617 {
3618 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3619 not contain any. */
3620
3621 if (strchr (name, '(') != NULL)
3622 {
3623 without_params = cp_remove_params (name);
3624
3625 if (without_params != NULL)
3626 name = without_params.get ();
3627 }
3628 }
3629
3630 /* Index version 4 did not support case insensitive searches. But the
3631 indices for case insensitive languages are built in lowercase, therefore
3632 simulate our NAME being searched is also lowercased. */
3633 hash = mapped_index_string_hash ((index->version == 4
3634 && case_sensitivity == case_sensitive_off
3635 ? 5 : index->version),
3636 name);
3637
3638 slot = hash & (index->symbol_table.size () - 1);
3639 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3640 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3641
3642 for (;;)
3643 {
3644 const char *str;
3645
3646 const auto &bucket = index->symbol_table[slot];
3647 if (bucket.name == 0 && bucket.vec == 0)
3648 return false;
3649
3650 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3651 if (!cmp (name, str))
3652 {
3653 *vec_out = (offset_type *) (index->constant_pool
3654 + MAYBE_SWAP (bucket.vec));
3655 return true;
3656 }
3657
3658 slot = (slot + step) & (index->symbol_table.size () - 1);
3659 }
3660 }
3661
3662 /* A helper function that reads the .gdb_index from SECTION and fills
3663 in MAP. FILENAME is the name of the file containing the section;
3664 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3665 ok to use deprecated sections.
3666
3667 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3668 out parameters that are filled in with information about the CU and
3669 TU lists in the section.
3670
3671 Returns 1 if all went well, 0 otherwise. */
3672
3673 static int
3674 read_index_from_section (struct objfile *objfile,
3675 const char *filename,
3676 int deprecated_ok,
3677 struct dwarf2_section_info *section,
3678 struct mapped_index *map,
3679 const gdb_byte **cu_list,
3680 offset_type *cu_list_elements,
3681 const gdb_byte **types_list,
3682 offset_type *types_list_elements)
3683 {
3684 const gdb_byte *addr;
3685 offset_type version;
3686 offset_type *metadata;
3687 int i;
3688
3689 if (dwarf2_section_empty_p (section))
3690 return 0;
3691
3692 /* Older elfutils strip versions could keep the section in the main
3693 executable while splitting it for the separate debug info file. */
3694 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3695 return 0;
3696
3697 dwarf2_read_section (objfile, section);
3698
3699 addr = section->buffer;
3700 /* Version check. */
3701 version = MAYBE_SWAP (*(offset_type *) addr);
3702 /* Versions earlier than 3 emitted every copy of a psymbol. This
3703 causes the index to behave very poorly for certain requests. Version 3
3704 contained incomplete addrmap. So, it seems better to just ignore such
3705 indices. */
3706 if (version < 4)
3707 {
3708 static int warning_printed = 0;
3709 if (!warning_printed)
3710 {
3711 warning (_("Skipping obsolete .gdb_index section in %s."),
3712 filename);
3713 warning_printed = 1;
3714 }
3715 return 0;
3716 }
3717 /* Index version 4 uses a different hash function than index version
3718 5 and later.
3719
3720 Versions earlier than 6 did not emit psymbols for inlined
3721 functions. Using these files will cause GDB not to be able to
3722 set breakpoints on inlined functions by name, so we ignore these
3723 indices unless the user has done
3724 "set use-deprecated-index-sections on". */
3725 if (version < 6 && !deprecated_ok)
3726 {
3727 static int warning_printed = 0;
3728 if (!warning_printed)
3729 {
3730 warning (_("\
3731 Skipping deprecated .gdb_index section in %s.\n\
3732 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3733 to use the section anyway."),
3734 filename);
3735 warning_printed = 1;
3736 }
3737 return 0;
3738 }
3739 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3740 of the TU (for symbols coming from TUs),
3741 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3742 Plus gold-generated indices can have duplicate entries for global symbols,
3743 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3744 These are just performance bugs, and we can't distinguish gdb-generated
3745 indices from gold-generated ones, so issue no warning here. */
3746
3747 /* Indexes with higher version than the one supported by GDB may be no
3748 longer backward compatible. */
3749 if (version > 8)
3750 return 0;
3751
3752 map->version = version;
3753 map->total_size = section->size;
3754
3755 metadata = (offset_type *) (addr + sizeof (offset_type));
3756
3757 i = 0;
3758 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3759 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3760 / 8);
3761 ++i;
3762
3763 *types_list = addr + MAYBE_SWAP (metadata[i]);
3764 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3765 - MAYBE_SWAP (metadata[i]))
3766 / 8);
3767 ++i;
3768
3769 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3770 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3771 map->address_table
3772 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3773 ++i;
3774
3775 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3776 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3777 map->symbol_table
3778 = gdb::array_view<mapped_index::symbol_table_slot>
3779 ((mapped_index::symbol_table_slot *) symbol_table,
3780 (mapped_index::symbol_table_slot *) symbol_table_end);
3781
3782 ++i;
3783 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3784
3785 return 1;
3786 }
3787
3788 /* Read .gdb_index. If everything went ok, initialize the "quick"
3789 elements of all the CUs and return 1. Otherwise, return 0. */
3790
3791 static int
3792 dwarf2_read_index (struct objfile *objfile)
3793 {
3794 struct mapped_index local_map, *map;
3795 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3796 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3797 struct dwz_file *dwz;
3798
3799 if (!read_index_from_section (objfile, objfile_name (objfile),
3800 use_deprecated_index_sections,
3801 &dwarf2_per_objfile->gdb_index, &local_map,
3802 &cu_list, &cu_list_elements,
3803 &types_list, &types_list_elements))
3804 return 0;
3805
3806 /* Don't use the index if it's empty. */
3807 if (local_map.symbol_table.empty ())
3808 return 0;
3809
3810 /* If there is a .dwz file, read it so we can get its CU list as
3811 well. */
3812 dwz = dwarf2_get_dwz_file ();
3813 if (dwz != NULL)
3814 {
3815 struct mapped_index dwz_map;
3816 const gdb_byte *dwz_types_ignore;
3817 offset_type dwz_types_elements_ignore;
3818
3819 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3820 1,
3821 &dwz->gdb_index, &dwz_map,
3822 &dwz_list, &dwz_list_elements,
3823 &dwz_types_ignore,
3824 &dwz_types_elements_ignore))
3825 {
3826 warning (_("could not read '.gdb_index' section from %s; skipping"),
3827 bfd_get_filename (dwz->dwz_bfd));
3828 return 0;
3829 }
3830 }
3831
3832 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3833 dwz_list_elements);
3834
3835 if (types_list_elements)
3836 {
3837 struct dwarf2_section_info *section;
3838
3839 /* We can only handle a single .debug_types when we have an
3840 index. */
3841 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3842 return 0;
3843
3844 section = VEC_index (dwarf2_section_info_def,
3845 dwarf2_per_objfile->types, 0);
3846
3847 create_signatured_type_table_from_index (objfile, section, types_list,
3848 types_list_elements);
3849 }
3850
3851 create_addrmap_from_index (objfile, &local_map);
3852
3853 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3854 map = new (map) mapped_index ();
3855 *map = local_map;
3856
3857 dwarf2_per_objfile->index_table = map;
3858 dwarf2_per_objfile->using_index = 1;
3859 dwarf2_per_objfile->quick_file_names_table =
3860 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3861
3862 return 1;
3863 }
3864
3865 /* A helper for the "quick" functions which sets the global
3866 dwarf2_per_objfile according to OBJFILE. */
3867
3868 static void
3869 dw2_setup (struct objfile *objfile)
3870 {
3871 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3872 objfile_data (objfile, dwarf2_objfile_data_key));
3873 gdb_assert (dwarf2_per_objfile);
3874 }
3875
3876 /* die_reader_func for dw2_get_file_names. */
3877
3878 static void
3879 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3880 const gdb_byte *info_ptr,
3881 struct die_info *comp_unit_die,
3882 int has_children,
3883 void *data)
3884 {
3885 struct dwarf2_cu *cu = reader->cu;
3886 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3887 struct objfile *objfile = dwarf2_per_objfile->objfile;
3888 struct dwarf2_per_cu_data *lh_cu;
3889 struct attribute *attr;
3890 int i;
3891 void **slot;
3892 struct quick_file_names *qfn;
3893
3894 gdb_assert (! this_cu->is_debug_types);
3895
3896 /* Our callers never want to match partial units -- instead they
3897 will match the enclosing full CU. */
3898 if (comp_unit_die->tag == DW_TAG_partial_unit)
3899 {
3900 this_cu->v.quick->no_file_data = 1;
3901 return;
3902 }
3903
3904 lh_cu = this_cu;
3905 slot = NULL;
3906
3907 line_header_up lh;
3908 sect_offset line_offset {};
3909
3910 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3911 if (attr)
3912 {
3913 struct quick_file_names find_entry;
3914
3915 line_offset = (sect_offset) DW_UNSND (attr);
3916
3917 /* We may have already read in this line header (TU line header sharing).
3918 If we have we're done. */
3919 find_entry.hash.dwo_unit = cu->dwo_unit;
3920 find_entry.hash.line_sect_off = line_offset;
3921 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3922 &find_entry, INSERT);
3923 if (*slot != NULL)
3924 {
3925 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3926 return;
3927 }
3928
3929 lh = dwarf_decode_line_header (line_offset, cu);
3930 }
3931 if (lh == NULL)
3932 {
3933 lh_cu->v.quick->no_file_data = 1;
3934 return;
3935 }
3936
3937 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3938 qfn->hash.dwo_unit = cu->dwo_unit;
3939 qfn->hash.line_sect_off = line_offset;
3940 gdb_assert (slot != NULL);
3941 *slot = qfn;
3942
3943 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3944
3945 qfn->num_file_names = lh->file_names.size ();
3946 qfn->file_names =
3947 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3948 for (i = 0; i < lh->file_names.size (); ++i)
3949 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3950 qfn->real_names = NULL;
3951
3952 lh_cu->v.quick->file_names = qfn;
3953 }
3954
3955 /* A helper for the "quick" functions which attempts to read the line
3956 table for THIS_CU. */
3957
3958 static struct quick_file_names *
3959 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3960 {
3961 /* This should never be called for TUs. */
3962 gdb_assert (! this_cu->is_debug_types);
3963 /* Nor type unit groups. */
3964 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3965
3966 if (this_cu->v.quick->file_names != NULL)
3967 return this_cu->v.quick->file_names;
3968 /* If we know there is no line data, no point in looking again. */
3969 if (this_cu->v.quick->no_file_data)
3970 return NULL;
3971
3972 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3973
3974 if (this_cu->v.quick->no_file_data)
3975 return NULL;
3976 return this_cu->v.quick->file_names;
3977 }
3978
3979 /* A helper for the "quick" functions which computes and caches the
3980 real path for a given file name from the line table. */
3981
3982 static const char *
3983 dw2_get_real_path (struct objfile *objfile,
3984 struct quick_file_names *qfn, int index)
3985 {
3986 if (qfn->real_names == NULL)
3987 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3988 qfn->num_file_names, const char *);
3989
3990 if (qfn->real_names[index] == NULL)
3991 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3992
3993 return qfn->real_names[index];
3994 }
3995
3996 static struct symtab *
3997 dw2_find_last_source_symtab (struct objfile *objfile)
3998 {
3999 struct compunit_symtab *cust;
4000 int index;
4001
4002 dw2_setup (objfile);
4003 index = dwarf2_per_objfile->n_comp_units - 1;
4004 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
4005 if (cust == NULL)
4006 return NULL;
4007 return compunit_primary_filetab (cust);
4008 }
4009
4010 /* Traversal function for dw2_forget_cached_source_info. */
4011
4012 static int
4013 dw2_free_cached_file_names (void **slot, void *info)
4014 {
4015 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
4016
4017 if (file_data->real_names)
4018 {
4019 int i;
4020
4021 for (i = 0; i < file_data->num_file_names; ++i)
4022 {
4023 xfree ((void*) file_data->real_names[i]);
4024 file_data->real_names[i] = NULL;
4025 }
4026 }
4027
4028 return 1;
4029 }
4030
4031 static void
4032 dw2_forget_cached_source_info (struct objfile *objfile)
4033 {
4034 dw2_setup (objfile);
4035
4036 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
4037 dw2_free_cached_file_names, NULL);
4038 }
4039
4040 /* Helper function for dw2_map_symtabs_matching_filename that expands
4041 the symtabs and calls the iterator. */
4042
4043 static int
4044 dw2_map_expand_apply (struct objfile *objfile,
4045 struct dwarf2_per_cu_data *per_cu,
4046 const char *name, const char *real_path,
4047 gdb::function_view<bool (symtab *)> callback)
4048 {
4049 struct compunit_symtab *last_made = objfile->compunit_symtabs;
4050
4051 /* Don't visit already-expanded CUs. */
4052 if (per_cu->v.quick->compunit_symtab)
4053 return 0;
4054
4055 /* This may expand more than one symtab, and we want to iterate over
4056 all of them. */
4057 dw2_instantiate_symtab (per_cu);
4058
4059 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4060 last_made, callback);
4061 }
4062
4063 /* Implementation of the map_symtabs_matching_filename method. */
4064
4065 static bool
4066 dw2_map_symtabs_matching_filename
4067 (struct objfile *objfile, const char *name, const char *real_path,
4068 gdb::function_view<bool (symtab *)> callback)
4069 {
4070 int i;
4071 const char *name_basename = lbasename (name);
4072
4073 dw2_setup (objfile);
4074
4075 /* The rule is CUs specify all the files, including those used by
4076 any TU, so there's no need to scan TUs here. */
4077
4078 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4079 {
4080 int j;
4081 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4082 struct quick_file_names *file_data;
4083
4084 /* We only need to look at symtabs not already expanded. */
4085 if (per_cu->v.quick->compunit_symtab)
4086 continue;
4087
4088 file_data = dw2_get_file_names (per_cu);
4089 if (file_data == NULL)
4090 continue;
4091
4092 for (j = 0; j < file_data->num_file_names; ++j)
4093 {
4094 const char *this_name = file_data->file_names[j];
4095 const char *this_real_name;
4096
4097 if (compare_filenames_for_search (this_name, name))
4098 {
4099 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4100 callback))
4101 return true;
4102 continue;
4103 }
4104
4105 /* Before we invoke realpath, which can get expensive when many
4106 files are involved, do a quick comparison of the basenames. */
4107 if (! basenames_may_differ
4108 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4109 continue;
4110
4111 this_real_name = dw2_get_real_path (objfile, file_data, j);
4112 if (compare_filenames_for_search (this_real_name, name))
4113 {
4114 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4115 callback))
4116 return true;
4117 continue;
4118 }
4119
4120 if (real_path != NULL)
4121 {
4122 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4123 gdb_assert (IS_ABSOLUTE_PATH (name));
4124 if (this_real_name != NULL
4125 && FILENAME_CMP (real_path, this_real_name) == 0)
4126 {
4127 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4128 callback))
4129 return true;
4130 continue;
4131 }
4132 }
4133 }
4134 }
4135
4136 return false;
4137 }
4138
4139 /* Struct used to manage iterating over all CUs looking for a symbol. */
4140
4141 struct dw2_symtab_iterator
4142 {
4143 /* The internalized form of .gdb_index. */
4144 struct mapped_index *index;
4145 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4146 int want_specific_block;
4147 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4148 Unused if !WANT_SPECIFIC_BLOCK. */
4149 int block_index;
4150 /* The kind of symbol we're looking for. */
4151 domain_enum domain;
4152 /* The list of CUs from the index entry of the symbol,
4153 or NULL if not found. */
4154 offset_type *vec;
4155 /* The next element in VEC to look at. */
4156 int next;
4157 /* The number of elements in VEC, or zero if there is no match. */
4158 int length;
4159 /* Have we seen a global version of the symbol?
4160 If so we can ignore all further global instances.
4161 This is to work around gold/15646, inefficient gold-generated
4162 indices. */
4163 int global_seen;
4164 };
4165
4166 /* Initialize the index symtab iterator ITER.
4167 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4168 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
4169
4170 static void
4171 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
4172 struct mapped_index *index,
4173 int want_specific_block,
4174 int block_index,
4175 domain_enum domain,
4176 const char *name)
4177 {
4178 iter->index = index;
4179 iter->want_specific_block = want_specific_block;
4180 iter->block_index = block_index;
4181 iter->domain = domain;
4182 iter->next = 0;
4183 iter->global_seen = 0;
4184
4185 if (find_slot_in_mapped_hash (index, name, &iter->vec))
4186 iter->length = MAYBE_SWAP (*iter->vec);
4187 else
4188 {
4189 iter->vec = NULL;
4190 iter->length = 0;
4191 }
4192 }
4193
4194 /* Return the next matching CU or NULL if there are no more. */
4195
4196 static struct dwarf2_per_cu_data *
4197 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4198 {
4199 for ( ; iter->next < iter->length; ++iter->next)
4200 {
4201 offset_type cu_index_and_attrs =
4202 MAYBE_SWAP (iter->vec[iter->next + 1]);
4203 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4204 struct dwarf2_per_cu_data *per_cu;
4205 int want_static = iter->block_index != GLOBAL_BLOCK;
4206 /* This value is only valid for index versions >= 7. */
4207 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4208 gdb_index_symbol_kind symbol_kind =
4209 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4210 /* Only check the symbol attributes if they're present.
4211 Indices prior to version 7 don't record them,
4212 and indices >= 7 may elide them for certain symbols
4213 (gold does this). */
4214 int attrs_valid =
4215 (iter->index->version >= 7
4216 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4217
4218 /* Don't crash on bad data. */
4219 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4220 + dwarf2_per_objfile->n_type_units))
4221 {
4222 complaint (&symfile_complaints,
4223 _(".gdb_index entry has bad CU index"
4224 " [in module %s]"),
4225 objfile_name (dwarf2_per_objfile->objfile));
4226 continue;
4227 }
4228
4229 per_cu = dw2_get_cutu (cu_index);
4230
4231 /* Skip if already read in. */
4232 if (per_cu->v.quick->compunit_symtab)
4233 continue;
4234
4235 /* Check static vs global. */
4236 if (attrs_valid)
4237 {
4238 if (iter->want_specific_block
4239 && want_static != is_static)
4240 continue;
4241 /* Work around gold/15646. */
4242 if (!is_static && iter->global_seen)
4243 continue;
4244 if (!is_static)
4245 iter->global_seen = 1;
4246 }
4247
4248 /* Only check the symbol's kind if it has one. */
4249 if (attrs_valid)
4250 {
4251 switch (iter->domain)
4252 {
4253 case VAR_DOMAIN:
4254 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4255 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4256 /* Some types are also in VAR_DOMAIN. */
4257 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4258 continue;
4259 break;
4260 case STRUCT_DOMAIN:
4261 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4262 continue;
4263 break;
4264 case LABEL_DOMAIN:
4265 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4266 continue;
4267 break;
4268 default:
4269 break;
4270 }
4271 }
4272
4273 ++iter->next;
4274 return per_cu;
4275 }
4276
4277 return NULL;
4278 }
4279
4280 static struct compunit_symtab *
4281 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4282 const char *name, domain_enum domain)
4283 {
4284 struct compunit_symtab *stab_best = NULL;
4285 struct mapped_index *index;
4286
4287 dw2_setup (objfile);
4288
4289 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4290
4291 index = dwarf2_per_objfile->index_table;
4292
4293 /* index is NULL if OBJF_READNOW. */
4294 if (index)
4295 {
4296 struct dw2_symtab_iterator iter;
4297 struct dwarf2_per_cu_data *per_cu;
4298
4299 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
4300
4301 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4302 {
4303 struct symbol *sym, *with_opaque = NULL;
4304 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4305 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4306 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4307
4308 sym = block_find_symbol (block, name, domain,
4309 block_find_non_opaque_type_preferred,
4310 &with_opaque);
4311
4312 /* Some caution must be observed with overloaded functions
4313 and methods, since the index will not contain any overload
4314 information (but NAME might contain it). */
4315
4316 if (sym != NULL
4317 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4318 return stab;
4319 if (with_opaque != NULL
4320 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4321 stab_best = stab;
4322
4323 /* Keep looking through other CUs. */
4324 }
4325 }
4326
4327 return stab_best;
4328 }
4329
4330 static void
4331 dw2_print_stats (struct objfile *objfile)
4332 {
4333 int i, total, count;
4334
4335 dw2_setup (objfile);
4336 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4337 count = 0;
4338 for (i = 0; i < total; ++i)
4339 {
4340 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4341
4342 if (!per_cu->v.quick->compunit_symtab)
4343 ++count;
4344 }
4345 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4346 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4347 }
4348
4349 /* This dumps minimal information about the index.
4350 It is called via "mt print objfiles".
4351 One use is to verify .gdb_index has been loaded by the
4352 gdb.dwarf2/gdb-index.exp testcase. */
4353
4354 static void
4355 dw2_dump (struct objfile *objfile)
4356 {
4357 dw2_setup (objfile);
4358 gdb_assert (dwarf2_per_objfile->using_index);
4359 printf_filtered (".gdb_index:");
4360 if (dwarf2_per_objfile->index_table != NULL)
4361 {
4362 printf_filtered (" version %d\n",
4363 dwarf2_per_objfile->index_table->version);
4364 }
4365 else
4366 printf_filtered (" faked for \"readnow\"\n");
4367 printf_filtered ("\n");
4368 }
4369
4370 static void
4371 dw2_relocate (struct objfile *objfile,
4372 const struct section_offsets *new_offsets,
4373 const struct section_offsets *delta)
4374 {
4375 /* There's nothing to relocate here. */
4376 }
4377
4378 static void
4379 dw2_expand_symtabs_for_function (struct objfile *objfile,
4380 const char *func_name)
4381 {
4382 struct mapped_index *index;
4383
4384 dw2_setup (objfile);
4385
4386 index = dwarf2_per_objfile->index_table;
4387
4388 /* index is NULL if OBJF_READNOW. */
4389 if (index)
4390 {
4391 struct dw2_symtab_iterator iter;
4392 struct dwarf2_per_cu_data *per_cu;
4393
4394 /* Note: It doesn't matter what we pass for block_index here. */
4395 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4396 func_name);
4397
4398 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4399 dw2_instantiate_symtab (per_cu);
4400 }
4401 }
4402
4403 static void
4404 dw2_expand_all_symtabs (struct objfile *objfile)
4405 {
4406 int i;
4407
4408 dw2_setup (objfile);
4409
4410 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
4411 + dwarf2_per_objfile->n_type_units); ++i)
4412 {
4413 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4414
4415 dw2_instantiate_symtab (per_cu);
4416 }
4417 }
4418
4419 static void
4420 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4421 const char *fullname)
4422 {
4423 int i;
4424
4425 dw2_setup (objfile);
4426
4427 /* We don't need to consider type units here.
4428 This is only called for examining code, e.g. expand_line_sal.
4429 There can be an order of magnitude (or more) more type units
4430 than comp units, and we avoid them if we can. */
4431
4432 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4433 {
4434 int j;
4435 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4436 struct quick_file_names *file_data;
4437
4438 /* We only need to look at symtabs not already expanded. */
4439 if (per_cu->v.quick->compunit_symtab)
4440 continue;
4441
4442 file_data = dw2_get_file_names (per_cu);
4443 if (file_data == NULL)
4444 continue;
4445
4446 for (j = 0; j < file_data->num_file_names; ++j)
4447 {
4448 const char *this_fullname = file_data->file_names[j];
4449
4450 if (filename_cmp (this_fullname, fullname) == 0)
4451 {
4452 dw2_instantiate_symtab (per_cu);
4453 break;
4454 }
4455 }
4456 }
4457 }
4458
4459 static void
4460 dw2_map_matching_symbols (struct objfile *objfile,
4461 const char * name, domain_enum domain,
4462 int global,
4463 int (*callback) (struct block *,
4464 struct symbol *, void *),
4465 void *data, symbol_name_match_type match,
4466 symbol_compare_ftype *ordered_compare)
4467 {
4468 /* Currently unimplemented; used for Ada. The function can be called if the
4469 current language is Ada for a non-Ada objfile using GNU index. As Ada
4470 does not look for non-Ada symbols this function should just return. */
4471 }
4472
4473 /* Symbol name matcher for .gdb_index names.
4474
4475 Symbol names in .gdb_index have a few particularities:
4476
4477 - There's no indication of which is the language of each symbol.
4478
4479 Since each language has its own symbol name matching algorithm,
4480 and we don't know which language is the right one, we must match
4481 each symbol against all languages. This would be a potential
4482 performance problem if it were not mitigated by the
4483 mapped_index::name_components lookup table, which significantly
4484 reduces the number of times we need to call into this matcher,
4485 making it a non-issue.
4486
4487 - Symbol names in the index have no overload (parameter)
4488 information. I.e., in C++, "foo(int)" and "foo(long)" both
4489 appear as "foo" in the index, for example.
4490
4491 This means that the lookup names passed to the symbol name
4492 matcher functions must have no parameter information either
4493 because (e.g.) symbol search name "foo" does not match
4494 lookup-name "foo(int)" [while swapping search name for lookup
4495 name would match].
4496 */
4497 class gdb_index_symbol_name_matcher
4498 {
4499 public:
4500 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4501 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4502
4503 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4504 Returns true if any matcher matches. */
4505 bool matches (const char *symbol_name);
4506
4507 private:
4508 /* A reference to the lookup name we're matching against. */
4509 const lookup_name_info &m_lookup_name;
4510
4511 /* A vector holding all the different symbol name matchers, for all
4512 languages. */
4513 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4514 };
4515
4516 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4517 (const lookup_name_info &lookup_name)
4518 : m_lookup_name (lookup_name)
4519 {
4520 /* Prepare the vector of comparison functions upfront, to avoid
4521 doing the same work for each symbol. Care is taken to avoid
4522 matching with the same matcher more than once if/when multiple
4523 languages use the same matcher function. */
4524 auto &matchers = m_symbol_name_matcher_funcs;
4525 matchers.reserve (nr_languages);
4526
4527 matchers.push_back (default_symbol_name_matcher);
4528
4529 for (int i = 0; i < nr_languages; i++)
4530 {
4531 const language_defn *lang = language_def ((enum language) i);
4532 if (lang->la_get_symbol_name_matcher != NULL)
4533 {
4534 symbol_name_matcher_ftype *name_matcher
4535 = lang->la_get_symbol_name_matcher (m_lookup_name);
4536
4537 /* Don't insert the same comparison routine more than once.
4538 Note that we do this linear walk instead of a cheaper
4539 sorted insert, or use a std::set or something like that,
4540 because relative order of function addresses is not
4541 stable. This is not a problem in practice because the
4542 number of supported languages is low, and the cost here
4543 is tiny compared to the number of searches we'll do
4544 afterwards using this object. */
4545 if (std::find (matchers.begin (), matchers.end (), name_matcher)
4546 == matchers.end ())
4547 matchers.push_back (name_matcher);
4548 }
4549 }
4550 }
4551
4552 bool
4553 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4554 {
4555 for (auto matches_name : m_symbol_name_matcher_funcs)
4556 if (matches_name (symbol_name, m_lookup_name, NULL))
4557 return true;
4558
4559 return false;
4560 }
4561
4562 /* Starting from a search name, return the string that finds the upper
4563 bound of all strings that start with SEARCH_NAME in a sorted name
4564 list. Returns the empty string to indicate that the upper bound is
4565 the end of the list. */
4566
4567 static std::string
4568 make_sort_after_prefix_name (const char *search_name)
4569 {
4570 /* When looking to complete "func", we find the upper bound of all
4571 symbols that start with "func" by looking for where we'd insert
4572 the closest string that would follow "func" in lexicographical
4573 order. Usually, that's "func"-with-last-character-incremented,
4574 i.e. "fund". Mind non-ASCII characters, though. Usually those
4575 will be UTF-8 multi-byte sequences, but we can't be certain.
4576 Especially mind the 0xff character, which is a valid character in
4577 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4578 rule out compilers allowing it in identifiers. Note that
4579 conveniently, strcmp/strcasecmp are specified to compare
4580 characters interpreted as unsigned char. So what we do is treat
4581 the whole string as a base 256 number composed of a sequence of
4582 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4583 to 0, and carries 1 to the following more-significant position.
4584 If the very first character in SEARCH_NAME ends up incremented
4585 and carries/overflows, then the upper bound is the end of the
4586 list. The string after the empty string is also the empty
4587 string.
4588
4589 Some examples of this operation:
4590
4591 SEARCH_NAME => "+1" RESULT
4592
4593 "abc" => "abd"
4594 "ab\xff" => "ac"
4595 "\xff" "a" "\xff" => "\xff" "b"
4596 "\xff" => ""
4597 "\xff\xff" => ""
4598 "" => ""
4599
4600 Then, with these symbols for example:
4601
4602 func
4603 func1
4604 fund
4605
4606 completing "func" looks for symbols between "func" and
4607 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4608 which finds "func" and "func1", but not "fund".
4609
4610 And with:
4611
4612 funcÿ (Latin1 'ÿ' [0xff])
4613 funcÿ1
4614 fund
4615
4616 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4617 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4618
4619 And with:
4620
4621 ÿÿ (Latin1 'ÿ' [0xff])
4622 ÿÿ1
4623
4624 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4625 the end of the list.
4626 */
4627 std::string after = search_name;
4628 while (!after.empty () && (unsigned char) after.back () == 0xff)
4629 after.pop_back ();
4630 if (!after.empty ())
4631 after.back () = (unsigned char) after.back () + 1;
4632 return after;
4633 }
4634
4635 /* See declaration. */
4636
4637 std::pair<std::vector<name_component>::const_iterator,
4638 std::vector<name_component>::const_iterator>
4639 mapped_index_base::find_name_components_bounds
4640 (const lookup_name_info &lookup_name_without_params) const
4641 {
4642 auto *name_cmp
4643 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4644
4645 const char *cplus
4646 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4647
4648 /* Comparison function object for lower_bound that matches against a
4649 given symbol name. */
4650 auto lookup_compare_lower = [&] (const name_component &elem,
4651 const char *name)
4652 {
4653 const char *elem_qualified = this->symbol_name_at (elem.idx);
4654 const char *elem_name = elem_qualified + elem.name_offset;
4655 return name_cmp (elem_name, name) < 0;
4656 };
4657
4658 /* Comparison function object for upper_bound that matches against a
4659 given symbol name. */
4660 auto lookup_compare_upper = [&] (const char *name,
4661 const name_component &elem)
4662 {
4663 const char *elem_qualified = this->symbol_name_at (elem.idx);
4664 const char *elem_name = elem_qualified + elem.name_offset;
4665 return name_cmp (name, elem_name) < 0;
4666 };
4667
4668 auto begin = this->name_components.begin ();
4669 auto end = this->name_components.end ();
4670
4671 /* Find the lower bound. */
4672 auto lower = [&] ()
4673 {
4674 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4675 return begin;
4676 else
4677 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4678 } ();
4679
4680 /* Find the upper bound. */
4681 auto upper = [&] ()
4682 {
4683 if (lookup_name_without_params.completion_mode ())
4684 {
4685 /* In completion mode, we want UPPER to point past all
4686 symbols names that have the same prefix. I.e., with
4687 these symbols, and completing "func":
4688
4689 function << lower bound
4690 function1
4691 other_function << upper bound
4692
4693 We find the upper bound by looking for the insertion
4694 point of "func"-with-last-character-incremented,
4695 i.e. "fund". */
4696 std::string after = make_sort_after_prefix_name (cplus);
4697 if (after.empty ())
4698 return end;
4699 return std::lower_bound (lower, end, after.c_str (),
4700 lookup_compare_lower);
4701 }
4702 else
4703 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4704 } ();
4705
4706 return {lower, upper};
4707 }
4708
4709 /* See declaration. */
4710
4711 void
4712 mapped_index_base::build_name_components ()
4713 {
4714 if (!this->name_components.empty ())
4715 return;
4716
4717 this->name_components_casing = case_sensitivity;
4718 auto *name_cmp
4719 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4720
4721 /* The code below only knows how to break apart components of C++
4722 symbol names (and other languages that use '::' as
4723 namespace/module separator). If we add support for wild matching
4724 to some language that uses some other operator (E.g., Ada, Go and
4725 D use '.'), then we'll need to try splitting the symbol name
4726 according to that language too. Note that Ada does support wild
4727 matching, but doesn't currently support .gdb_index. */
4728 auto count = this->symbol_name_count ();
4729 for (offset_type idx = 0; idx < count; idx++)
4730 {
4731 if (this->symbol_name_slot_invalid (idx))
4732 continue;
4733
4734 const char *name = this->symbol_name_at (idx);
4735
4736 /* Add each name component to the name component table. */
4737 unsigned int previous_len = 0;
4738 for (unsigned int current_len = cp_find_first_component (name);
4739 name[current_len] != '\0';
4740 current_len += cp_find_first_component (name + current_len))
4741 {
4742 gdb_assert (name[current_len] == ':');
4743 this->name_components.push_back ({previous_len, idx});
4744 /* Skip the '::'. */
4745 current_len += 2;
4746 previous_len = current_len;
4747 }
4748 this->name_components.push_back ({previous_len, idx});
4749 }
4750
4751 /* Sort name_components elements by name. */
4752 auto name_comp_compare = [&] (const name_component &left,
4753 const name_component &right)
4754 {
4755 const char *left_qualified = this->symbol_name_at (left.idx);
4756 const char *right_qualified = this->symbol_name_at (right.idx);
4757
4758 const char *left_name = left_qualified + left.name_offset;
4759 const char *right_name = right_qualified + right.name_offset;
4760
4761 return name_cmp (left_name, right_name) < 0;
4762 };
4763
4764 std::sort (this->name_components.begin (),
4765 this->name_components.end (),
4766 name_comp_compare);
4767 }
4768
4769 /* Helper for dw2_expand_symtabs_matching that works with a
4770 mapped_index_base instead of the containing objfile. This is split
4771 to a separate function in order to be able to unit test the
4772 name_components matching using a mock mapped_index_base. For each
4773 symbol name that matches, calls MATCH_CALLBACK, passing it the
4774 symbol's index in the mapped_index_base symbol table. */
4775
4776 static void
4777 dw2_expand_symtabs_matching_symbol
4778 (mapped_index_base &index,
4779 const lookup_name_info &lookup_name_in,
4780 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4781 enum search_domain kind,
4782 gdb::function_view<void (offset_type)> match_callback)
4783 {
4784 lookup_name_info lookup_name_without_params
4785 = lookup_name_in.make_ignore_params ();
4786 gdb_index_symbol_name_matcher lookup_name_matcher
4787 (lookup_name_without_params);
4788
4789 /* Build the symbol name component sorted vector, if we haven't
4790 yet. */
4791 index.build_name_components ();
4792
4793 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4794
4795 /* Now for each symbol name in range, check to see if we have a name
4796 match, and if so, call the MATCH_CALLBACK callback. */
4797
4798 /* The same symbol may appear more than once in the range though.
4799 E.g., if we're looking for symbols that complete "w", and we have
4800 a symbol named "w1::w2", we'll find the two name components for
4801 that same symbol in the range. To be sure we only call the
4802 callback once per symbol, we first collect the symbol name
4803 indexes that matched in a temporary vector and ignore
4804 duplicates. */
4805 std::vector<offset_type> matches;
4806 matches.reserve (std::distance (bounds.first, bounds.second));
4807
4808 for (; bounds.first != bounds.second; ++bounds.first)
4809 {
4810 const char *qualified = index.symbol_name_at (bounds.first->idx);
4811
4812 if (!lookup_name_matcher.matches (qualified)
4813 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4814 continue;
4815
4816 matches.push_back (bounds.first->idx);
4817 }
4818
4819 std::sort (matches.begin (), matches.end ());
4820
4821 /* Finally call the callback, once per match. */
4822 ULONGEST prev = -1;
4823 for (offset_type idx : matches)
4824 {
4825 if (prev != idx)
4826 {
4827 match_callback (idx);
4828 prev = idx;
4829 }
4830 }
4831
4832 /* Above we use a type wider than idx's for 'prev', since 0 and
4833 (offset_type)-1 are both possible values. */
4834 static_assert (sizeof (prev) > sizeof (offset_type), "");
4835 }
4836
4837 #if GDB_SELF_TEST
4838
4839 namespace selftests { namespace dw2_expand_symtabs_matching {
4840
4841 /* A mock .gdb_index/.debug_names-like name index table, enough to
4842 exercise dw2_expand_symtabs_matching_symbol, which works with the
4843 mapped_index_base interface. Builds an index from the symbol list
4844 passed as parameter to the constructor. */
4845 class mock_mapped_index : public mapped_index_base
4846 {
4847 public:
4848 mock_mapped_index (gdb::array_view<const char *> symbols)
4849 : m_symbol_table (symbols)
4850 {}
4851
4852 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4853
4854 /* Return the number of names in the symbol table. */
4855 virtual size_t symbol_name_count () const
4856 {
4857 return m_symbol_table.size ();
4858 }
4859
4860 /* Get the name of the symbol at IDX in the symbol table. */
4861 virtual const char *symbol_name_at (offset_type idx) const
4862 {
4863 return m_symbol_table[idx];
4864 }
4865
4866 private:
4867 gdb::array_view<const char *> m_symbol_table;
4868 };
4869
4870 /* Convenience function that converts a NULL pointer to a "<null>"
4871 string, to pass to print routines. */
4872
4873 static const char *
4874 string_or_null (const char *str)
4875 {
4876 return str != NULL ? str : "<null>";
4877 }
4878
4879 /* Check if a lookup_name_info built from
4880 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4881 index. EXPECTED_LIST is the list of expected matches, in expected
4882 matching order. If no match expected, then an empty list is
4883 specified. Returns true on success. On failure prints a warning
4884 indicating the file:line that failed, and returns false. */
4885
4886 static bool
4887 check_match (const char *file, int line,
4888 mock_mapped_index &mock_index,
4889 const char *name, symbol_name_match_type match_type,
4890 bool completion_mode,
4891 std::initializer_list<const char *> expected_list)
4892 {
4893 lookup_name_info lookup_name (name, match_type, completion_mode);
4894
4895 bool matched = true;
4896
4897 auto mismatch = [&] (const char *expected_str,
4898 const char *got)
4899 {
4900 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4901 "expected=\"%s\", got=\"%s\"\n"),
4902 file, line,
4903 (match_type == symbol_name_match_type::FULL
4904 ? "FULL" : "WILD"),
4905 name, string_or_null (expected_str), string_or_null (got));
4906 matched = false;
4907 };
4908
4909 auto expected_it = expected_list.begin ();
4910 auto expected_end = expected_list.end ();
4911
4912 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4913 NULL, ALL_DOMAIN,
4914 [&] (offset_type idx)
4915 {
4916 const char *matched_name = mock_index.symbol_name_at (idx);
4917 const char *expected_str
4918 = expected_it == expected_end ? NULL : *expected_it++;
4919
4920 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4921 mismatch (expected_str, matched_name);
4922 });
4923
4924 const char *expected_str
4925 = expected_it == expected_end ? NULL : *expected_it++;
4926 if (expected_str != NULL)
4927 mismatch (expected_str, NULL);
4928
4929 return matched;
4930 }
4931
4932 /* The symbols added to the mock mapped_index for testing (in
4933 canonical form). */
4934 static const char *test_symbols[] = {
4935 "function",
4936 "std::bar",
4937 "std::zfunction",
4938 "std::zfunction2",
4939 "w1::w2",
4940 "ns::foo<char*>",
4941 "ns::foo<int>",
4942 "ns::foo<long>",
4943 "ns2::tmpl<int>::foo2",
4944 "(anonymous namespace)::A::B::C",
4945
4946 /* These are used to check that the increment-last-char in the
4947 matching algorithm for completion doesn't match "t1_fund" when
4948 completing "t1_func". */
4949 "t1_func",
4950 "t1_func1",
4951 "t1_fund",
4952 "t1_fund1",
4953
4954 /* A UTF-8 name with multi-byte sequences to make sure that
4955 cp-name-parser understands this as a single identifier ("função"
4956 is "function" in PT). */
4957 u8"u8função",
4958
4959 /* \377 (0xff) is Latin1 'ÿ'. */
4960 "yfunc\377",
4961
4962 /* \377 (0xff) is Latin1 'ÿ'. */
4963 "\377",
4964 "\377\377123",
4965
4966 /* A name with all sorts of complications. Starts with "z" to make
4967 it easier for the completion tests below. */
4968 #define Z_SYM_NAME \
4969 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4970 "::tuple<(anonymous namespace)::ui*, " \
4971 "std::default_delete<(anonymous namespace)::ui>, void>"
4972
4973 Z_SYM_NAME
4974 };
4975
4976 /* Returns true if the mapped_index_base::find_name_component_bounds
4977 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4978 in completion mode. */
4979
4980 static bool
4981 check_find_bounds_finds (mapped_index_base &index,
4982 const char *search_name,
4983 gdb::array_view<const char *> expected_syms)
4984 {
4985 lookup_name_info lookup_name (search_name,
4986 symbol_name_match_type::FULL, true);
4987
4988 auto bounds = index.find_name_components_bounds (lookup_name);
4989
4990 size_t distance = std::distance (bounds.first, bounds.second);
4991 if (distance != expected_syms.size ())
4992 return false;
4993
4994 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4995 {
4996 auto nc_elem = bounds.first + exp_elem;
4997 const char *qualified = index.symbol_name_at (nc_elem->idx);
4998 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4999 return false;
5000 }
5001
5002 return true;
5003 }
5004
5005 /* Test the lower-level mapped_index::find_name_component_bounds
5006 method. */
5007
5008 static void
5009 test_mapped_index_find_name_component_bounds ()
5010 {
5011 mock_mapped_index mock_index (test_symbols);
5012
5013 mock_index.build_name_components ();
5014
5015 /* Test the lower-level mapped_index::find_name_component_bounds
5016 method in completion mode. */
5017 {
5018 static const char *expected_syms[] = {
5019 "t1_func",
5020 "t1_func1",
5021 };
5022
5023 SELF_CHECK (check_find_bounds_finds (mock_index,
5024 "t1_func", expected_syms));
5025 }
5026
5027 /* Check that the increment-last-char in the name matching algorithm
5028 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5029 {
5030 static const char *expected_syms1[] = {
5031 "\377",
5032 "\377\377123",
5033 };
5034 SELF_CHECK (check_find_bounds_finds (mock_index,
5035 "\377", expected_syms1));
5036
5037 static const char *expected_syms2[] = {
5038 "\377\377123",
5039 };
5040 SELF_CHECK (check_find_bounds_finds (mock_index,
5041 "\377\377", expected_syms2));
5042 }
5043 }
5044
5045 /* Test dw2_expand_symtabs_matching_symbol. */
5046
5047 static void
5048 test_dw2_expand_symtabs_matching_symbol ()
5049 {
5050 mock_mapped_index mock_index (test_symbols);
5051
5052 /* We let all tests run until the end even if some fails, for debug
5053 convenience. */
5054 bool any_mismatch = false;
5055
5056 /* Create the expected symbols list (an initializer_list). Needed
5057 because lists have commas, and we need to pass them to CHECK,
5058 which is a macro. */
5059 #define EXPECT(...) { __VA_ARGS__ }
5060
5061 /* Wrapper for check_match that passes down the current
5062 __FILE__/__LINE__. */
5063 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5064 any_mismatch |= !check_match (__FILE__, __LINE__, \
5065 mock_index, \
5066 NAME, MATCH_TYPE, COMPLETION_MODE, \
5067 EXPECTED_LIST)
5068
5069 /* Identity checks. */
5070 for (const char *sym : test_symbols)
5071 {
5072 /* Should be able to match all existing symbols. */
5073 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5074 EXPECT (sym));
5075
5076 /* Should be able to match all existing symbols with
5077 parameters. */
5078 std::string with_params = std::string (sym) + "(int)";
5079 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5080 EXPECT (sym));
5081
5082 /* Should be able to match all existing symbols with
5083 parameters and qualifiers. */
5084 with_params = std::string (sym) + " ( int ) const";
5085 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5086 EXPECT (sym));
5087
5088 /* This should really find sym, but cp-name-parser.y doesn't
5089 know about lvalue/rvalue qualifiers yet. */
5090 with_params = std::string (sym) + " ( int ) &&";
5091 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5092 {});
5093 }
5094
5095 /* Check that the name matching algorithm for completion doesn't get
5096 confused with Latin1 'ÿ' / 0xff. */
5097 {
5098 static const char str[] = "\377";
5099 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5100 EXPECT ("\377", "\377\377123"));
5101 }
5102
5103 /* Check that the increment-last-char in the matching algorithm for
5104 completion doesn't match "t1_fund" when completing "t1_func". */
5105 {
5106 static const char str[] = "t1_func";
5107 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5108 EXPECT ("t1_func", "t1_func1"));
5109 }
5110
5111 /* Check that completion mode works at each prefix of the expected
5112 symbol name. */
5113 {
5114 static const char str[] = "function(int)";
5115 size_t len = strlen (str);
5116 std::string lookup;
5117
5118 for (size_t i = 1; i < len; i++)
5119 {
5120 lookup.assign (str, i);
5121 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5122 EXPECT ("function"));
5123 }
5124 }
5125
5126 /* While "w" is a prefix of both components, the match function
5127 should still only be called once. */
5128 {
5129 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5130 EXPECT ("w1::w2"));
5131 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5132 EXPECT ("w1::w2"));
5133 }
5134
5135 /* Same, with a "complicated" symbol. */
5136 {
5137 static const char str[] = Z_SYM_NAME;
5138 size_t len = strlen (str);
5139 std::string lookup;
5140
5141 for (size_t i = 1; i < len; i++)
5142 {
5143 lookup.assign (str, i);
5144 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5145 EXPECT (Z_SYM_NAME));
5146 }
5147 }
5148
5149 /* In FULL mode, an incomplete symbol doesn't match. */
5150 {
5151 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5152 {});
5153 }
5154
5155 /* A complete symbol with parameters matches any overload, since the
5156 index has no overload info. */
5157 {
5158 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5159 EXPECT ("std::zfunction", "std::zfunction2"));
5160 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5161 EXPECT ("std::zfunction", "std::zfunction2"));
5162 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5163 EXPECT ("std::zfunction", "std::zfunction2"));
5164 }
5165
5166 /* Check that whitespace is ignored appropriately. A symbol with a
5167 template argument list. */
5168 {
5169 static const char expected[] = "ns::foo<int>";
5170 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5171 EXPECT (expected));
5172 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5173 EXPECT (expected));
5174 }
5175
5176 /* Check that whitespace is ignored appropriately. A symbol with a
5177 template argument list that includes a pointer. */
5178 {
5179 static const char expected[] = "ns::foo<char*>";
5180 /* Try both completion and non-completion modes. */
5181 static const bool completion_mode[2] = {false, true};
5182 for (size_t i = 0; i < 2; i++)
5183 {
5184 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5185 completion_mode[i], EXPECT (expected));
5186 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5187 completion_mode[i], EXPECT (expected));
5188
5189 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5190 completion_mode[i], EXPECT (expected));
5191 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5192 completion_mode[i], EXPECT (expected));
5193 }
5194 }
5195
5196 {
5197 /* Check method qualifiers are ignored. */
5198 static const char expected[] = "ns::foo<char*>";
5199 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5200 symbol_name_match_type::FULL, true, EXPECT (expected));
5201 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5202 symbol_name_match_type::FULL, true, EXPECT (expected));
5203 CHECK_MATCH ("foo < char * > ( int ) const",
5204 symbol_name_match_type::WILD, true, EXPECT (expected));
5205 CHECK_MATCH ("foo < char * > ( int ) &&",
5206 symbol_name_match_type::WILD, true, EXPECT (expected));
5207 }
5208
5209 /* Test lookup names that don't match anything. */
5210 {
5211 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5212 {});
5213
5214 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5215 {});
5216 }
5217
5218 /* Some wild matching tests, exercising "(anonymous namespace)",
5219 which should not be confused with a parameter list. */
5220 {
5221 static const char *syms[] = {
5222 "A::B::C",
5223 "B::C",
5224 "C",
5225 "A :: B :: C ( int )",
5226 "B :: C ( int )",
5227 "C ( int )",
5228 };
5229
5230 for (const char *s : syms)
5231 {
5232 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5233 EXPECT ("(anonymous namespace)::A::B::C"));
5234 }
5235 }
5236
5237 {
5238 static const char expected[] = "ns2::tmpl<int>::foo2";
5239 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5240 EXPECT (expected));
5241 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5242 EXPECT (expected));
5243 }
5244
5245 SELF_CHECK (!any_mismatch);
5246
5247 #undef EXPECT
5248 #undef CHECK_MATCH
5249 }
5250
5251 static void
5252 run_test ()
5253 {
5254 test_mapped_index_find_name_component_bounds ();
5255 test_dw2_expand_symtabs_matching_symbol ();
5256 }
5257
5258 }} // namespace selftests::dw2_expand_symtabs_matching
5259
5260 #endif /* GDB_SELF_TEST */
5261
5262 /* If FILE_MATCHER is NULL or if PER_CU has
5263 dwarf2_per_cu_quick_data::MARK set (see
5264 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5265 EXPANSION_NOTIFY on it. */
5266
5267 static void
5268 dw2_expand_symtabs_matching_one
5269 (struct dwarf2_per_cu_data *per_cu,
5270 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5271 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5272 {
5273 if (file_matcher == NULL || per_cu->v.quick->mark)
5274 {
5275 bool symtab_was_null
5276 = (per_cu->v.quick->compunit_symtab == NULL);
5277
5278 dw2_instantiate_symtab (per_cu);
5279
5280 if (expansion_notify != NULL
5281 && symtab_was_null
5282 && per_cu->v.quick->compunit_symtab != NULL)
5283 expansion_notify (per_cu->v.quick->compunit_symtab);
5284 }
5285 }
5286
5287 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5288 matched, to expand corresponding CUs that were marked. IDX is the
5289 index of the symbol name that matched. */
5290
5291 static void
5292 dw2_expand_marked_cus
5293 (mapped_index &index, offset_type idx,
5294 struct objfile *objfile,
5295 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5296 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5297 search_domain kind)
5298 {
5299 offset_type *vec, vec_len, vec_idx;
5300 bool global_seen = false;
5301
5302 vec = (offset_type *) (index.constant_pool
5303 + MAYBE_SWAP (index.symbol_table[idx].vec));
5304 vec_len = MAYBE_SWAP (vec[0]);
5305 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5306 {
5307 struct dwarf2_per_cu_data *per_cu;
5308 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5309 /* This value is only valid for index versions >= 7. */
5310 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5311 gdb_index_symbol_kind symbol_kind =
5312 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5313 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5314 /* Only check the symbol attributes if they're present.
5315 Indices prior to version 7 don't record them,
5316 and indices >= 7 may elide them for certain symbols
5317 (gold does this). */
5318 int attrs_valid =
5319 (index.version >= 7
5320 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5321
5322 /* Work around gold/15646. */
5323 if (attrs_valid)
5324 {
5325 if (!is_static && global_seen)
5326 continue;
5327 if (!is_static)
5328 global_seen = true;
5329 }
5330
5331 /* Only check the symbol's kind if it has one. */
5332 if (attrs_valid)
5333 {
5334 switch (kind)
5335 {
5336 case VARIABLES_DOMAIN:
5337 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5338 continue;
5339 break;
5340 case FUNCTIONS_DOMAIN:
5341 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5342 continue;
5343 break;
5344 case TYPES_DOMAIN:
5345 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5346 continue;
5347 break;
5348 default:
5349 break;
5350 }
5351 }
5352
5353 /* Don't crash on bad data. */
5354 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5355 + dwarf2_per_objfile->n_type_units))
5356 {
5357 complaint (&symfile_complaints,
5358 _(".gdb_index entry has bad CU index"
5359 " [in module %s]"), objfile_name (objfile));
5360 continue;
5361 }
5362
5363 per_cu = dw2_get_cutu (cu_index);
5364 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5365 expansion_notify);
5366 }
5367 }
5368
5369 /* If FILE_MATCHER is non-NULL, set all the
5370 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5371 that match FILE_MATCHER. */
5372
5373 static void
5374 dw_expand_symtabs_matching_file_matcher
5375 (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5376 {
5377 if (file_matcher == NULL)
5378 return;
5379
5380 objfile *const objfile = dwarf2_per_objfile->objfile;
5381
5382 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5383 htab_eq_pointer,
5384 NULL, xcalloc, xfree));
5385 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5386 htab_eq_pointer,
5387 NULL, xcalloc, xfree));
5388
5389 /* The rule is CUs specify all the files, including those used by
5390 any TU, so there's no need to scan TUs here. */
5391
5392 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5393 {
5394 int j;
5395 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5396 struct quick_file_names *file_data;
5397 void **slot;
5398
5399 QUIT;
5400
5401 per_cu->v.quick->mark = 0;
5402
5403 /* We only need to look at symtabs not already expanded. */
5404 if (per_cu->v.quick->compunit_symtab)
5405 continue;
5406
5407 file_data = dw2_get_file_names (per_cu);
5408 if (file_data == NULL)
5409 continue;
5410
5411 if (htab_find (visited_not_found.get (), file_data) != NULL)
5412 continue;
5413 else if (htab_find (visited_found.get (), file_data) != NULL)
5414 {
5415 per_cu->v.quick->mark = 1;
5416 continue;
5417 }
5418
5419 for (j = 0; j < file_data->num_file_names; ++j)
5420 {
5421 const char *this_real_name;
5422
5423 if (file_matcher (file_data->file_names[j], false))
5424 {
5425 per_cu->v.quick->mark = 1;
5426 break;
5427 }
5428
5429 /* Before we invoke realpath, which can get expensive when many
5430 files are involved, do a quick comparison of the basenames. */
5431 if (!basenames_may_differ
5432 && !file_matcher (lbasename (file_data->file_names[j]),
5433 true))
5434 continue;
5435
5436 this_real_name = dw2_get_real_path (objfile, file_data, j);
5437 if (file_matcher (this_real_name, false))
5438 {
5439 per_cu->v.quick->mark = 1;
5440 break;
5441 }
5442 }
5443
5444 slot = htab_find_slot (per_cu->v.quick->mark
5445 ? visited_found.get ()
5446 : visited_not_found.get (),
5447 file_data, INSERT);
5448 *slot = file_data;
5449 }
5450 }
5451
5452 static void
5453 dw2_expand_symtabs_matching
5454 (struct objfile *objfile,
5455 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5456 const lookup_name_info &lookup_name,
5457 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5458 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5459 enum search_domain kind)
5460 {
5461 dw2_setup (objfile);
5462
5463 /* index_table is NULL if OBJF_READNOW. */
5464 if (!dwarf2_per_objfile->index_table)
5465 return;
5466
5467 dw_expand_symtabs_matching_file_matcher (file_matcher);
5468
5469 mapped_index &index = *dwarf2_per_objfile->index_table;
5470
5471 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5472 symbol_matcher,
5473 kind, [&] (offset_type idx)
5474 {
5475 dw2_expand_marked_cus (index, idx, objfile, file_matcher,
5476 expansion_notify, kind);
5477 });
5478 }
5479
5480 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5481 symtab. */
5482
5483 static struct compunit_symtab *
5484 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5485 CORE_ADDR pc)
5486 {
5487 int i;
5488
5489 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5490 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5491 return cust;
5492
5493 if (cust->includes == NULL)
5494 return NULL;
5495
5496 for (i = 0; cust->includes[i]; ++i)
5497 {
5498 struct compunit_symtab *s = cust->includes[i];
5499
5500 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5501 if (s != NULL)
5502 return s;
5503 }
5504
5505 return NULL;
5506 }
5507
5508 static struct compunit_symtab *
5509 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5510 struct bound_minimal_symbol msymbol,
5511 CORE_ADDR pc,
5512 struct obj_section *section,
5513 int warn_if_readin)
5514 {
5515 struct dwarf2_per_cu_data *data;
5516 struct compunit_symtab *result;
5517
5518 dw2_setup (objfile);
5519
5520 if (!objfile->psymtabs_addrmap)
5521 return NULL;
5522
5523 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5524 pc);
5525 if (!data)
5526 return NULL;
5527
5528 if (warn_if_readin && data->v.quick->compunit_symtab)
5529 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5530 paddress (get_objfile_arch (objfile), pc));
5531
5532 result
5533 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5534 pc);
5535 gdb_assert (result != NULL);
5536 return result;
5537 }
5538
5539 static void
5540 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5541 void *data, int need_fullname)
5542 {
5543 dw2_setup (objfile);
5544
5545 if (!dwarf2_per_objfile->filenames_cache)
5546 {
5547 dwarf2_per_objfile->filenames_cache.emplace ();
5548
5549 htab_up visited (htab_create_alloc (10,
5550 htab_hash_pointer, htab_eq_pointer,
5551 NULL, xcalloc, xfree));
5552
5553 /* The rule is CUs specify all the files, including those used
5554 by any TU, so there's no need to scan TUs here. We can
5555 ignore file names coming from already-expanded CUs. */
5556
5557 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5558 {
5559 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
5560
5561 if (per_cu->v.quick->compunit_symtab)
5562 {
5563 void **slot = htab_find_slot (visited.get (),
5564 per_cu->v.quick->file_names,
5565 INSERT);
5566
5567 *slot = per_cu->v.quick->file_names;
5568 }
5569 }
5570
5571 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5572 {
5573 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5574 struct quick_file_names *file_data;
5575 void **slot;
5576
5577 /* We only need to look at symtabs not already expanded. */
5578 if (per_cu->v.quick->compunit_symtab)
5579 continue;
5580
5581 file_data = dw2_get_file_names (per_cu);
5582 if (file_data == NULL)
5583 continue;
5584
5585 slot = htab_find_slot (visited.get (), file_data, INSERT);
5586 if (*slot)
5587 {
5588 /* Already visited. */
5589 continue;
5590 }
5591 *slot = file_data;
5592
5593 for (int j = 0; j < file_data->num_file_names; ++j)
5594 {
5595 const char *filename = file_data->file_names[j];
5596 dwarf2_per_objfile->filenames_cache->seen (filename);
5597 }
5598 }
5599 }
5600
5601 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5602 {
5603 gdb::unique_xmalloc_ptr<char> this_real_name;
5604
5605 if (need_fullname)
5606 this_real_name = gdb_realpath (filename);
5607 (*fun) (filename, this_real_name.get (), data);
5608 });
5609 }
5610
5611 static int
5612 dw2_has_symbols (struct objfile *objfile)
5613 {
5614 return 1;
5615 }
5616
5617 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5618 {
5619 dw2_has_symbols,
5620 dw2_find_last_source_symtab,
5621 dw2_forget_cached_source_info,
5622 dw2_map_symtabs_matching_filename,
5623 dw2_lookup_symbol,
5624 dw2_print_stats,
5625 dw2_dump,
5626 dw2_relocate,
5627 dw2_expand_symtabs_for_function,
5628 dw2_expand_all_symtabs,
5629 dw2_expand_symtabs_with_fullname,
5630 dw2_map_matching_symbols,
5631 dw2_expand_symtabs_matching,
5632 dw2_find_pc_sect_compunit_symtab,
5633 NULL,
5634 dw2_map_symbol_filenames
5635 };
5636
5637 /* DWARF-5 debug_names reader. */
5638
5639 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5640 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5641
5642 /* A helper function that reads the .debug_names section in SECTION
5643 and fills in MAP. FILENAME is the name of the file containing the
5644 section; it is used for error reporting.
5645
5646 Returns true if all went well, false otherwise. */
5647
5648 static bool
5649 read_debug_names_from_section (struct objfile *objfile,
5650 const char *filename,
5651 struct dwarf2_section_info *section,
5652 mapped_debug_names &map)
5653 {
5654 if (dwarf2_section_empty_p (section))
5655 return false;
5656
5657 /* Older elfutils strip versions could keep the section in the main
5658 executable while splitting it for the separate debug info file. */
5659 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5660 return false;
5661
5662 dwarf2_read_section (objfile, section);
5663
5664 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5665
5666 const gdb_byte *addr = section->buffer;
5667
5668 bfd *const abfd = get_section_bfd_owner (section);
5669
5670 unsigned int bytes_read;
5671 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5672 addr += bytes_read;
5673
5674 map.dwarf5_is_dwarf64 = bytes_read != 4;
5675 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5676 if (bytes_read + length != section->size)
5677 {
5678 /* There may be multiple per-CU indices. */
5679 warning (_("Section .debug_names in %s length %s does not match "
5680 "section length %s, ignoring .debug_names."),
5681 filename, plongest (bytes_read + length),
5682 pulongest (section->size));
5683 return false;
5684 }
5685
5686 /* The version number. */
5687 uint16_t version = read_2_bytes (abfd, addr);
5688 addr += 2;
5689 if (version != 5)
5690 {
5691 warning (_("Section .debug_names in %s has unsupported version %d, "
5692 "ignoring .debug_names."),
5693 filename, version);
5694 return false;
5695 }
5696
5697 /* Padding. */
5698 uint16_t padding = read_2_bytes (abfd, addr);
5699 addr += 2;
5700 if (padding != 0)
5701 {
5702 warning (_("Section .debug_names in %s has unsupported padding %d, "
5703 "ignoring .debug_names."),
5704 filename, padding);
5705 return false;
5706 }
5707
5708 /* comp_unit_count - The number of CUs in the CU list. */
5709 map.cu_count = read_4_bytes (abfd, addr);
5710 addr += 4;
5711
5712 /* local_type_unit_count - The number of TUs in the local TU
5713 list. */
5714 map.tu_count = read_4_bytes (abfd, addr);
5715 addr += 4;
5716
5717 /* foreign_type_unit_count - The number of TUs in the foreign TU
5718 list. */
5719 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5720 addr += 4;
5721 if (foreign_tu_count != 0)
5722 {
5723 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5724 "ignoring .debug_names."),
5725 filename, static_cast<unsigned long> (foreign_tu_count));
5726 return false;
5727 }
5728
5729 /* bucket_count - The number of hash buckets in the hash lookup
5730 table. */
5731 map.bucket_count = read_4_bytes (abfd, addr);
5732 addr += 4;
5733
5734 /* name_count - The number of unique names in the index. */
5735 map.name_count = read_4_bytes (abfd, addr);
5736 addr += 4;
5737
5738 /* abbrev_table_size - The size in bytes of the abbreviations
5739 table. */
5740 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5741 addr += 4;
5742
5743 /* augmentation_string_size - The size in bytes of the augmentation
5744 string. This value is rounded up to a multiple of 4. */
5745 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5746 addr += 4;
5747 map.augmentation_is_gdb = ((augmentation_string_size
5748 == sizeof (dwarf5_augmentation))
5749 && memcmp (addr, dwarf5_augmentation,
5750 sizeof (dwarf5_augmentation)) == 0);
5751 augmentation_string_size += (-augmentation_string_size) & 3;
5752 addr += augmentation_string_size;
5753
5754 /* List of CUs */
5755 map.cu_table_reordered = addr;
5756 addr += map.cu_count * map.offset_size;
5757
5758 /* List of Local TUs */
5759 map.tu_table_reordered = addr;
5760 addr += map.tu_count * map.offset_size;
5761
5762 /* Hash Lookup Table */
5763 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5764 addr += map.bucket_count * 4;
5765 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5766 addr += map.name_count * 4;
5767
5768 /* Name Table */
5769 map.name_table_string_offs_reordered = addr;
5770 addr += map.name_count * map.offset_size;
5771 map.name_table_entry_offs_reordered = addr;
5772 addr += map.name_count * map.offset_size;
5773
5774 const gdb_byte *abbrev_table_start = addr;
5775 for (;;)
5776 {
5777 unsigned int bytes_read;
5778 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5779 addr += bytes_read;
5780 if (index_num == 0)
5781 break;
5782
5783 const auto insertpair
5784 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5785 if (!insertpair.second)
5786 {
5787 warning (_("Section .debug_names in %s has duplicate index %s, "
5788 "ignoring .debug_names."),
5789 filename, pulongest (index_num));
5790 return false;
5791 }
5792 mapped_debug_names::index_val &indexval = insertpair.first->second;
5793 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5794 addr += bytes_read;
5795
5796 for (;;)
5797 {
5798 mapped_debug_names::index_val::attr attr;
5799 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5800 addr += bytes_read;
5801 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5802 addr += bytes_read;
5803 if (attr.form == DW_FORM_implicit_const)
5804 {
5805 attr.implicit_const = read_signed_leb128 (abfd, addr,
5806 &bytes_read);
5807 addr += bytes_read;
5808 }
5809 if (attr.dw_idx == 0 && attr.form == 0)
5810 break;
5811 indexval.attr_vec.push_back (std::move (attr));
5812 }
5813 }
5814 if (addr != abbrev_table_start + abbrev_table_size)
5815 {
5816 warning (_("Section .debug_names in %s has abbreviation_table "
5817 "of size %zu vs. written as %u, ignoring .debug_names."),
5818 filename, addr - abbrev_table_start, abbrev_table_size);
5819 return false;
5820 }
5821 map.entry_pool = addr;
5822
5823 return true;
5824 }
5825
5826 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5827 list. */
5828
5829 static void
5830 create_cus_from_debug_names_list (struct objfile *objfile,
5831 const mapped_debug_names &map,
5832 dwarf2_section_info &section,
5833 bool is_dwz, int base_offset)
5834 {
5835 sect_offset sect_off_prev;
5836 for (uint32_t i = 0; i <= map.cu_count; ++i)
5837 {
5838 sect_offset sect_off_next;
5839 if (i < map.cu_count)
5840 {
5841 sect_off_next
5842 = (sect_offset) (extract_unsigned_integer
5843 (map.cu_table_reordered + i * map.offset_size,
5844 map.offset_size,
5845 map.dwarf5_byte_order));
5846 }
5847 else
5848 sect_off_next = (sect_offset) section.size;
5849 if (i >= 1)
5850 {
5851 const ULONGEST length = sect_off_next - sect_off_prev;
5852 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
5853 = create_cu_from_index_list (objfile, &section, is_dwz,
5854 sect_off_prev, length);
5855 }
5856 sect_off_prev = sect_off_next;
5857 }
5858 }
5859
5860 /* Read the CU list from the mapped index, and use it to create all
5861 the CU objects for this objfile. */
5862
5863 static void
5864 create_cus_from_debug_names (struct objfile *objfile,
5865 const mapped_debug_names &map,
5866 const mapped_debug_names &dwz_map)
5867 {
5868
5869 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
5870 dwarf2_per_objfile->all_comp_units
5871 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
5872 dwarf2_per_objfile->n_comp_units);
5873
5874 create_cus_from_debug_names_list (objfile, map, dwarf2_per_objfile->info,
5875 false /* is_dwz */,
5876 0 /* base_offset */);
5877
5878 if (dwz_map.cu_count == 0)
5879 return;
5880
5881 dwz_file *dwz = dwarf2_get_dwz_file ();
5882 create_cus_from_debug_names_list (objfile, dwz_map, dwz->info,
5883 true /* is_dwz */,
5884 map.cu_count /* base_offset */);
5885 }
5886
5887 /* Read .debug_names. If everything went ok, initialize the "quick"
5888 elements of all the CUs and return true. Otherwise, return false. */
5889
5890 static bool
5891 dwarf2_read_debug_names (struct objfile *objfile)
5892 {
5893 mapped_debug_names local_map, dwz_map;
5894
5895 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5896 &dwarf2_per_objfile->debug_names,
5897 local_map))
5898 return false;
5899
5900 /* Don't use the index if it's empty. */
5901 if (local_map.name_count == 0)
5902 return false;
5903
5904 /* If there is a .dwz file, read it so we can get its CU list as
5905 well. */
5906 dwz_file *dwz = dwarf2_get_dwz_file ();
5907 if (dwz != NULL)
5908 {
5909 if (!read_debug_names_from_section (objfile,
5910 bfd_get_filename (dwz->dwz_bfd),
5911 &dwz->debug_names, dwz_map))
5912 {
5913 warning (_("could not read '.debug_names' section from %s; skipping"),
5914 bfd_get_filename (dwz->dwz_bfd));
5915 return false;
5916 }
5917 }
5918
5919 create_cus_from_debug_names (objfile, local_map, dwz_map);
5920
5921 if (local_map.tu_count != 0)
5922 {
5923 /* We can only handle a single .debug_types when we have an
5924 index. */
5925 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5926 return false;
5927
5928 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5929 dwarf2_per_objfile->types, 0);
5930
5931 create_signatured_type_table_from_debug_names
5932 (objfile, local_map, section, &dwarf2_per_objfile->abbrev);
5933 }
5934
5935 create_addrmap_from_aranges (objfile, &dwarf2_per_objfile->debug_aranges);
5936
5937 dwarf2_per_objfile->debug_names_table.reset (new mapped_debug_names);
5938 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
5939 dwarf2_per_objfile->using_index = 1;
5940 dwarf2_per_objfile->quick_file_names_table =
5941 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
5942
5943 return true;
5944 }
5945
5946 /* Symbol name hashing function as specified by DWARF-5. */
5947
5948 static uint32_t
5949 dwarf5_djb_hash (const char *str_)
5950 {
5951 const unsigned char *str = (const unsigned char *) str_;
5952
5953 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
5954 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
5955
5956 uint32_t hash = 5381;
5957 while (int c = *str++)
5958 hash = hash * 33 + tolower (c);
5959 return hash;
5960 }
5961
5962 /* Type used to manage iterating over all CUs looking for a symbol for
5963 .debug_names. */
5964
5965 class dw2_debug_names_iterator
5966 {
5967 public:
5968 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5969 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5970 dw2_debug_names_iterator (const mapped_debug_names &map,
5971 bool want_specific_block,
5972 block_enum block_index, domain_enum domain,
5973 const char *name)
5974 : m_map (map), m_want_specific_block (want_specific_block),
5975 m_block_index (block_index), m_domain (domain),
5976 m_addr (find_vec_in_debug_names (map, name))
5977 {}
5978
5979 dw2_debug_names_iterator (const mapped_debug_names &map,
5980 search_domain search, uint32_t namei)
5981 : m_map (map),
5982 m_search (search),
5983 m_addr (find_vec_in_debug_names (map, namei))
5984 {}
5985
5986 /* Return the next matching CU or NULL if there are no more. */
5987 dwarf2_per_cu_data *next ();
5988
5989 private:
5990 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5991 const char *name);
5992 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5993 uint32_t namei);
5994
5995 /* The internalized form of .debug_names. */
5996 const mapped_debug_names &m_map;
5997
5998 /* If true, only look for symbols that match BLOCK_INDEX. */
5999 const bool m_want_specific_block = false;
6000
6001 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
6002 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
6003 value. */
6004 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
6005
6006 /* The kind of symbol we're looking for. */
6007 const domain_enum m_domain = UNDEF_DOMAIN;
6008 const search_domain m_search = ALL_DOMAIN;
6009
6010 /* The list of CUs from the index entry of the symbol, or NULL if
6011 not found. */
6012 const gdb_byte *m_addr;
6013 };
6014
6015 const char *
6016 mapped_debug_names::namei_to_name (uint32_t namei) const
6017 {
6018 const ULONGEST namei_string_offs
6019 = extract_unsigned_integer ((name_table_string_offs_reordered
6020 + namei * offset_size),
6021 offset_size,
6022 dwarf5_byte_order);
6023 return read_indirect_string_at_offset
6024 (dwarf2_per_objfile->objfile->obfd, namei_string_offs);
6025 }
6026
6027 /* Find a slot in .debug_names for the object named NAME. If NAME is
6028 found, return pointer to its pool data. If NAME cannot be found,
6029 return NULL. */
6030
6031 const gdb_byte *
6032 dw2_debug_names_iterator::find_vec_in_debug_names
6033 (const mapped_debug_names &map, const char *name)
6034 {
6035 int (*cmp) (const char *, const char *);
6036
6037 if (current_language->la_language == language_cplus
6038 || current_language->la_language == language_fortran
6039 || current_language->la_language == language_d)
6040 {
6041 /* NAME is already canonical. Drop any qualifiers as
6042 .debug_names does not contain any. */
6043
6044 if (strchr (name, '(') != NULL)
6045 {
6046 gdb::unique_xmalloc_ptr<char> without_params
6047 = cp_remove_params (name);
6048
6049 if (without_params != NULL)
6050 {
6051 name = without_params.get();
6052 }
6053 }
6054 }
6055
6056 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6057
6058 const uint32_t full_hash = dwarf5_djb_hash (name);
6059 uint32_t namei
6060 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6061 (map.bucket_table_reordered
6062 + (full_hash % map.bucket_count)), 4,
6063 map.dwarf5_byte_order);
6064 if (namei == 0)
6065 return NULL;
6066 --namei;
6067 if (namei >= map.name_count)
6068 {
6069 complaint (&symfile_complaints,
6070 _("Wrong .debug_names with name index %u but name_count=%u "
6071 "[in module %s]"),
6072 namei, map.name_count,
6073 objfile_name (dwarf2_per_objfile->objfile));
6074 return NULL;
6075 }
6076
6077 for (;;)
6078 {
6079 const uint32_t namei_full_hash
6080 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6081 (map.hash_table_reordered + namei), 4,
6082 map.dwarf5_byte_order);
6083 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6084 return NULL;
6085
6086 if (full_hash == namei_full_hash)
6087 {
6088 const char *const namei_string = map.namei_to_name (namei);
6089
6090 #if 0 /* An expensive sanity check. */
6091 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6092 {
6093 complaint (&symfile_complaints,
6094 _("Wrong .debug_names hash for string at index %u "
6095 "[in module %s]"),
6096 namei, objfile_name (dwarf2_per_objfile->objfile));
6097 return NULL;
6098 }
6099 #endif
6100
6101 if (cmp (namei_string, name) == 0)
6102 {
6103 const ULONGEST namei_entry_offs
6104 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6105 + namei * map.offset_size),
6106 map.offset_size, map.dwarf5_byte_order);
6107 return map.entry_pool + namei_entry_offs;
6108 }
6109 }
6110
6111 ++namei;
6112 if (namei >= map.name_count)
6113 return NULL;
6114 }
6115 }
6116
6117 const gdb_byte *
6118 dw2_debug_names_iterator::find_vec_in_debug_names
6119 (const mapped_debug_names &map, uint32_t namei)
6120 {
6121 if (namei >= map.name_count)
6122 {
6123 complaint (&symfile_complaints,
6124 _("Wrong .debug_names with name index %u but name_count=%u "
6125 "[in module %s]"),
6126 namei, map.name_count,
6127 objfile_name (dwarf2_per_objfile->objfile));
6128 return NULL;
6129 }
6130
6131 const ULONGEST namei_entry_offs
6132 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6133 + namei * map.offset_size),
6134 map.offset_size, map.dwarf5_byte_order);
6135 return map.entry_pool + namei_entry_offs;
6136 }
6137
6138 /* See dw2_debug_names_iterator. */
6139
6140 dwarf2_per_cu_data *
6141 dw2_debug_names_iterator::next ()
6142 {
6143 if (m_addr == NULL)
6144 return NULL;
6145
6146 bfd *const abfd = dwarf2_per_objfile->objfile->obfd;
6147
6148 again:
6149
6150 unsigned int bytes_read;
6151 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6152 m_addr += bytes_read;
6153 if (abbrev == 0)
6154 return NULL;
6155
6156 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6157 if (indexval_it == m_map.abbrev_map.cend ())
6158 {
6159 complaint (&symfile_complaints,
6160 _("Wrong .debug_names undefined abbrev code %s "
6161 "[in module %s]"),
6162 pulongest (abbrev), objfile_name (dwarf2_per_objfile->objfile));
6163 return NULL;
6164 }
6165 const mapped_debug_names::index_val &indexval = indexval_it->second;
6166 bool have_is_static = false;
6167 bool is_static;
6168 dwarf2_per_cu_data *per_cu = NULL;
6169 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6170 {
6171 ULONGEST ull;
6172 switch (attr.form)
6173 {
6174 case DW_FORM_implicit_const:
6175 ull = attr.implicit_const;
6176 break;
6177 case DW_FORM_flag_present:
6178 ull = 1;
6179 break;
6180 case DW_FORM_udata:
6181 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6182 m_addr += bytes_read;
6183 break;
6184 default:
6185 complaint (&symfile_complaints,
6186 _("Unsupported .debug_names form %s [in module %s]"),
6187 dwarf_form_name (attr.form),
6188 objfile_name (dwarf2_per_objfile->objfile));
6189 return NULL;
6190 }
6191 switch (attr.dw_idx)
6192 {
6193 case DW_IDX_compile_unit:
6194 /* Don't crash on bad data. */
6195 if (ull >= (dwarf2_per_objfile->n_comp_units
6196 + dwarf2_per_objfile->n_type_units))
6197 {
6198 complaint (&symfile_complaints,
6199 _(".debug_names entry has bad CU index %s"
6200 " [in module %s]"),
6201 pulongest (ull),
6202 objfile_name (dwarf2_per_objfile->objfile));
6203 continue;
6204 }
6205 per_cu = dw2_get_cutu (ull);
6206 break;
6207 case DW_IDX_GNU_internal:
6208 if (!m_map.augmentation_is_gdb)
6209 break;
6210 have_is_static = true;
6211 is_static = true;
6212 break;
6213 case DW_IDX_GNU_external:
6214 if (!m_map.augmentation_is_gdb)
6215 break;
6216 have_is_static = true;
6217 is_static = false;
6218 break;
6219 }
6220 }
6221
6222 /* Skip if already read in. */
6223 if (per_cu->v.quick->compunit_symtab)
6224 goto again;
6225
6226 /* Check static vs global. */
6227 if (have_is_static)
6228 {
6229 const bool want_static = m_block_index != GLOBAL_BLOCK;
6230 if (m_want_specific_block && want_static != is_static)
6231 goto again;
6232 }
6233
6234 /* Match dw2_symtab_iter_next, symbol_kind
6235 and debug_names::psymbol_tag. */
6236 switch (m_domain)
6237 {
6238 case VAR_DOMAIN:
6239 switch (indexval.dwarf_tag)
6240 {
6241 case DW_TAG_variable:
6242 case DW_TAG_subprogram:
6243 /* Some types are also in VAR_DOMAIN. */
6244 case DW_TAG_typedef:
6245 case DW_TAG_structure_type:
6246 break;
6247 default:
6248 goto again;
6249 }
6250 break;
6251 case STRUCT_DOMAIN:
6252 switch (indexval.dwarf_tag)
6253 {
6254 case DW_TAG_typedef:
6255 case DW_TAG_structure_type:
6256 break;
6257 default:
6258 goto again;
6259 }
6260 break;
6261 case LABEL_DOMAIN:
6262 switch (indexval.dwarf_tag)
6263 {
6264 case 0:
6265 case DW_TAG_variable:
6266 break;
6267 default:
6268 goto again;
6269 }
6270 break;
6271 default:
6272 break;
6273 }
6274
6275 /* Match dw2_expand_symtabs_matching, symbol_kind and
6276 debug_names::psymbol_tag. */
6277 switch (m_search)
6278 {
6279 case VARIABLES_DOMAIN:
6280 switch (indexval.dwarf_tag)
6281 {
6282 case DW_TAG_variable:
6283 break;
6284 default:
6285 goto again;
6286 }
6287 break;
6288 case FUNCTIONS_DOMAIN:
6289 switch (indexval.dwarf_tag)
6290 {
6291 case DW_TAG_subprogram:
6292 break;
6293 default:
6294 goto again;
6295 }
6296 break;
6297 case TYPES_DOMAIN:
6298 switch (indexval.dwarf_tag)
6299 {
6300 case DW_TAG_typedef:
6301 case DW_TAG_structure_type:
6302 break;
6303 default:
6304 goto again;
6305 }
6306 break;
6307 default:
6308 break;
6309 }
6310
6311 return per_cu;
6312 }
6313
6314 static struct compunit_symtab *
6315 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6316 const char *name, domain_enum domain)
6317 {
6318 const block_enum block_index = static_cast<block_enum> (block_index_int);
6319 dw2_setup (objfile);
6320
6321 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6322 if (!mapp)
6323 {
6324 /* index is NULL if OBJF_READNOW. */
6325 return NULL;
6326 }
6327 const auto &map = *mapp;
6328
6329 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6330 block_index, domain, name);
6331
6332 struct compunit_symtab *stab_best = NULL;
6333 struct dwarf2_per_cu_data *per_cu;
6334 while ((per_cu = iter.next ()) != NULL)
6335 {
6336 struct symbol *sym, *with_opaque = NULL;
6337 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6338 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6339 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6340
6341 sym = block_find_symbol (block, name, domain,
6342 block_find_non_opaque_type_preferred,
6343 &with_opaque);
6344
6345 /* Some caution must be observed with overloaded functions and
6346 methods, since the index will not contain any overload
6347 information (but NAME might contain it). */
6348
6349 if (sym != NULL
6350 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6351 return stab;
6352 if (with_opaque != NULL
6353 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6354 stab_best = stab;
6355
6356 /* Keep looking through other CUs. */
6357 }
6358
6359 return stab_best;
6360 }
6361
6362 /* This dumps minimal information about .debug_names. It is called
6363 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6364 uses this to verify that .debug_names has been loaded. */
6365
6366 static void
6367 dw2_debug_names_dump (struct objfile *objfile)
6368 {
6369 dw2_setup (objfile);
6370 gdb_assert (dwarf2_per_objfile->using_index);
6371 printf_filtered (".debug_names:");
6372 if (dwarf2_per_objfile->debug_names_table)
6373 printf_filtered (" exists\n");
6374 else
6375 printf_filtered (" faked for \"readnow\"\n");
6376 printf_filtered ("\n");
6377 }
6378
6379 static void
6380 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6381 const char *func_name)
6382 {
6383 dw2_setup (objfile);
6384
6385 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6386 if (dwarf2_per_objfile->debug_names_table)
6387 {
6388 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6389
6390 /* Note: It doesn't matter what we pass for block_index here. */
6391 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6392 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6393
6394 struct dwarf2_per_cu_data *per_cu;
6395 while ((per_cu = iter.next ()) != NULL)
6396 dw2_instantiate_symtab (per_cu);
6397 }
6398 }
6399
6400 static void
6401 dw2_debug_names_expand_symtabs_matching
6402 (struct objfile *objfile,
6403 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6404 const lookup_name_info &lookup_name,
6405 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6406 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6407 enum search_domain kind)
6408 {
6409 dw2_setup (objfile);
6410
6411 /* debug_names_table is NULL if OBJF_READNOW. */
6412 if (!dwarf2_per_objfile->debug_names_table)
6413 return;
6414
6415 dw_expand_symtabs_matching_file_matcher (file_matcher);
6416
6417 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6418
6419 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6420 symbol_matcher,
6421 kind, [&] (offset_type namei)
6422 {
6423 /* The name was matched, now expand corresponding CUs that were
6424 marked. */
6425 dw2_debug_names_iterator iter (map, kind, namei);
6426
6427 struct dwarf2_per_cu_data *per_cu;
6428 while ((per_cu = iter.next ()) != NULL)
6429 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6430 expansion_notify);
6431 });
6432 }
6433
6434 const struct quick_symbol_functions dwarf2_debug_names_functions =
6435 {
6436 dw2_has_symbols,
6437 dw2_find_last_source_symtab,
6438 dw2_forget_cached_source_info,
6439 dw2_map_symtabs_matching_filename,
6440 dw2_debug_names_lookup_symbol,
6441 dw2_print_stats,
6442 dw2_debug_names_dump,
6443 dw2_relocate,
6444 dw2_debug_names_expand_symtabs_for_function,
6445 dw2_expand_all_symtabs,
6446 dw2_expand_symtabs_with_fullname,
6447 dw2_map_matching_symbols,
6448 dw2_debug_names_expand_symtabs_matching,
6449 dw2_find_pc_sect_compunit_symtab,
6450 NULL,
6451 dw2_map_symbol_filenames
6452 };
6453
6454 /* Initialize for reading DWARF for this objfile. Return 0 if this
6455 file will use psymtabs, or 1 if using the GNU index. */
6456
6457 const sym_fns &
6458 dwarf2_initialize_objfile (struct objfile *objfile)
6459 {
6460 /* If we're about to read full symbols, don't bother with the
6461 indices. In this case we also don't care if some other debug
6462 format is making psymtabs, because they are all about to be
6463 expanded anyway. */
6464 if ((objfile->flags & OBJF_READNOW))
6465 {
6466 int i;
6467
6468 dwarf2_per_objfile->using_index = 1;
6469 create_all_comp_units (objfile);
6470 create_all_type_units (objfile);
6471 dwarf2_per_objfile->quick_file_names_table =
6472 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6473
6474 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
6475 + dwarf2_per_objfile->n_type_units); ++i)
6476 {
6477 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6478
6479 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6480 struct dwarf2_per_cu_quick_data);
6481 }
6482
6483 /* Return 1 so that gdb sees the "quick" functions. However,
6484 these functions will be no-ops because we will have expanded
6485 all symtabs. */
6486 return elf_sym_fns_gdb_index;
6487 }
6488
6489 if (dwarf2_read_debug_names (objfile))
6490 return elf_sym_fns_debug_names;
6491
6492 if (dwarf2_read_index (objfile))
6493 return elf_sym_fns_gdb_index;
6494
6495 return elf_sym_fns_lazy_psyms;
6496 }
6497
6498 \f
6499
6500 /* Build a partial symbol table. */
6501
6502 void
6503 dwarf2_build_psymtabs (struct objfile *objfile)
6504 {
6505
6506 if (objfile->global_psymbols.capacity () == 0
6507 && objfile->static_psymbols.capacity () == 0)
6508 init_psymbol_list (objfile, 1024);
6509
6510 TRY
6511 {
6512 /* This isn't really ideal: all the data we allocate on the
6513 objfile's obstack is still uselessly kept around. However,
6514 freeing it seems unsafe. */
6515 psymtab_discarder psymtabs (objfile);
6516 dwarf2_build_psymtabs_hard (objfile);
6517 psymtabs.keep ();
6518 }
6519 CATCH (except, RETURN_MASK_ERROR)
6520 {
6521 exception_print (gdb_stderr, except);
6522 }
6523 END_CATCH
6524 }
6525
6526 /* Return the total length of the CU described by HEADER. */
6527
6528 static unsigned int
6529 get_cu_length (const struct comp_unit_head *header)
6530 {
6531 return header->initial_length_size + header->length;
6532 }
6533
6534 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6535
6536 static inline bool
6537 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6538 {
6539 sect_offset bottom = cu_header->sect_off;
6540 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6541
6542 return sect_off >= bottom && sect_off < top;
6543 }
6544
6545 /* Find the base address of the compilation unit for range lists and
6546 location lists. It will normally be specified by DW_AT_low_pc.
6547 In DWARF-3 draft 4, the base address could be overridden by
6548 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6549 compilation units with discontinuous ranges. */
6550
6551 static void
6552 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6553 {
6554 struct attribute *attr;
6555
6556 cu->base_known = 0;
6557 cu->base_address = 0;
6558
6559 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6560 if (attr)
6561 {
6562 cu->base_address = attr_value_as_address (attr);
6563 cu->base_known = 1;
6564 }
6565 else
6566 {
6567 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6568 if (attr)
6569 {
6570 cu->base_address = attr_value_as_address (attr);
6571 cu->base_known = 1;
6572 }
6573 }
6574 }
6575
6576 /* Read in the comp unit header information from the debug_info at info_ptr.
6577 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6578 NOTE: This leaves members offset, first_die_offset to be filled in
6579 by the caller. */
6580
6581 static const gdb_byte *
6582 read_comp_unit_head (struct comp_unit_head *cu_header,
6583 const gdb_byte *info_ptr,
6584 struct dwarf2_section_info *section,
6585 rcuh_kind section_kind)
6586 {
6587 int signed_addr;
6588 unsigned int bytes_read;
6589 const char *filename = get_section_file_name (section);
6590 bfd *abfd = get_section_bfd_owner (section);
6591
6592 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6593 cu_header->initial_length_size = bytes_read;
6594 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6595 info_ptr += bytes_read;
6596 cu_header->version = read_2_bytes (abfd, info_ptr);
6597 info_ptr += 2;
6598 if (cu_header->version < 5)
6599 switch (section_kind)
6600 {
6601 case rcuh_kind::COMPILE:
6602 cu_header->unit_type = DW_UT_compile;
6603 break;
6604 case rcuh_kind::TYPE:
6605 cu_header->unit_type = DW_UT_type;
6606 break;
6607 default:
6608 internal_error (__FILE__, __LINE__,
6609 _("read_comp_unit_head: invalid section_kind"));
6610 }
6611 else
6612 {
6613 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6614 (read_1_byte (abfd, info_ptr));
6615 info_ptr += 1;
6616 switch (cu_header->unit_type)
6617 {
6618 case DW_UT_compile:
6619 if (section_kind != rcuh_kind::COMPILE)
6620 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6621 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6622 filename);
6623 break;
6624 case DW_UT_type:
6625 section_kind = rcuh_kind::TYPE;
6626 break;
6627 default:
6628 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6629 "(is %d, should be %d or %d) [in module %s]"),
6630 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6631 }
6632
6633 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6634 info_ptr += 1;
6635 }
6636 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6637 cu_header,
6638 &bytes_read);
6639 info_ptr += bytes_read;
6640 if (cu_header->version < 5)
6641 {
6642 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6643 info_ptr += 1;
6644 }
6645 signed_addr = bfd_get_sign_extend_vma (abfd);
6646 if (signed_addr < 0)
6647 internal_error (__FILE__, __LINE__,
6648 _("read_comp_unit_head: dwarf from non elf file"));
6649 cu_header->signed_addr_p = signed_addr;
6650
6651 if (section_kind == rcuh_kind::TYPE)
6652 {
6653 LONGEST type_offset;
6654
6655 cu_header->signature = read_8_bytes (abfd, info_ptr);
6656 info_ptr += 8;
6657
6658 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6659 info_ptr += bytes_read;
6660 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6661 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6662 error (_("Dwarf Error: Too big type_offset in compilation unit "
6663 "header (is %s) [in module %s]"), plongest (type_offset),
6664 filename);
6665 }
6666
6667 return info_ptr;
6668 }
6669
6670 /* Helper function that returns the proper abbrev section for
6671 THIS_CU. */
6672
6673 static struct dwarf2_section_info *
6674 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6675 {
6676 struct dwarf2_section_info *abbrev;
6677
6678 if (this_cu->is_dwz)
6679 abbrev = &dwarf2_get_dwz_file ()->abbrev;
6680 else
6681 abbrev = &dwarf2_per_objfile->abbrev;
6682
6683 return abbrev;
6684 }
6685
6686 /* Subroutine of read_and_check_comp_unit_head and
6687 read_and_check_type_unit_head to simplify them.
6688 Perform various error checking on the header. */
6689
6690 static void
6691 error_check_comp_unit_head (struct comp_unit_head *header,
6692 struct dwarf2_section_info *section,
6693 struct dwarf2_section_info *abbrev_section)
6694 {
6695 const char *filename = get_section_file_name (section);
6696
6697 if (header->version < 2 || header->version > 5)
6698 error (_("Dwarf Error: wrong version in compilation unit header "
6699 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6700 filename);
6701
6702 if (to_underlying (header->abbrev_sect_off)
6703 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6704 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
6705 "(offset 0x%x + 6) [in module %s]"),
6706 to_underlying (header->abbrev_sect_off),
6707 to_underlying (header->sect_off),
6708 filename);
6709
6710 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6711 avoid potential 32-bit overflow. */
6712 if (((ULONGEST) header->sect_off + get_cu_length (header))
6713 > section->size)
6714 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6715 "(offset 0x%x + 0) [in module %s]"),
6716 header->length, to_underlying (header->sect_off),
6717 filename);
6718 }
6719
6720 /* Read in a CU/TU header and perform some basic error checking.
6721 The contents of the header are stored in HEADER.
6722 The result is a pointer to the start of the first DIE. */
6723
6724 static const gdb_byte *
6725 read_and_check_comp_unit_head (struct comp_unit_head *header,
6726 struct dwarf2_section_info *section,
6727 struct dwarf2_section_info *abbrev_section,
6728 const gdb_byte *info_ptr,
6729 rcuh_kind section_kind)
6730 {
6731 const gdb_byte *beg_of_comp_unit = info_ptr;
6732
6733 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6734
6735 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6736
6737 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6738
6739 error_check_comp_unit_head (header, section, abbrev_section);
6740
6741 return info_ptr;
6742 }
6743
6744 /* Fetch the abbreviation table offset from a comp or type unit header. */
6745
6746 static sect_offset
6747 read_abbrev_offset (struct dwarf2_section_info *section,
6748 sect_offset sect_off)
6749 {
6750 bfd *abfd = get_section_bfd_owner (section);
6751 const gdb_byte *info_ptr;
6752 unsigned int initial_length_size, offset_size;
6753 uint16_t version;
6754
6755 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6756 info_ptr = section->buffer + to_underlying (sect_off);
6757 read_initial_length (abfd, info_ptr, &initial_length_size);
6758 offset_size = initial_length_size == 4 ? 4 : 8;
6759 info_ptr += initial_length_size;
6760
6761 version = read_2_bytes (abfd, info_ptr);
6762 info_ptr += 2;
6763 if (version >= 5)
6764 {
6765 /* Skip unit type and address size. */
6766 info_ptr += 2;
6767 }
6768
6769 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6770 }
6771
6772 /* Allocate a new partial symtab for file named NAME and mark this new
6773 partial symtab as being an include of PST. */
6774
6775 static void
6776 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6777 struct objfile *objfile)
6778 {
6779 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6780
6781 if (!IS_ABSOLUTE_PATH (subpst->filename))
6782 {
6783 /* It shares objfile->objfile_obstack. */
6784 subpst->dirname = pst->dirname;
6785 }
6786
6787 subpst->textlow = 0;
6788 subpst->texthigh = 0;
6789
6790 subpst->dependencies
6791 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6792 subpst->dependencies[0] = pst;
6793 subpst->number_of_dependencies = 1;
6794
6795 subpst->globals_offset = 0;
6796 subpst->n_global_syms = 0;
6797 subpst->statics_offset = 0;
6798 subpst->n_static_syms = 0;
6799 subpst->compunit_symtab = NULL;
6800 subpst->read_symtab = pst->read_symtab;
6801 subpst->readin = 0;
6802
6803 /* No private part is necessary for include psymtabs. This property
6804 can be used to differentiate between such include psymtabs and
6805 the regular ones. */
6806 subpst->read_symtab_private = NULL;
6807 }
6808
6809 /* Read the Line Number Program data and extract the list of files
6810 included by the source file represented by PST. Build an include
6811 partial symtab for each of these included files. */
6812
6813 static void
6814 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6815 struct die_info *die,
6816 struct partial_symtab *pst)
6817 {
6818 line_header_up lh;
6819 struct attribute *attr;
6820
6821 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6822 if (attr)
6823 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6824 if (lh == NULL)
6825 return; /* No linetable, so no includes. */
6826
6827 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
6828 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
6829 }
6830
6831 static hashval_t
6832 hash_signatured_type (const void *item)
6833 {
6834 const struct signatured_type *sig_type
6835 = (const struct signatured_type *) item;
6836
6837 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6838 return sig_type->signature;
6839 }
6840
6841 static int
6842 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6843 {
6844 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6845 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6846
6847 return lhs->signature == rhs->signature;
6848 }
6849
6850 /* Allocate a hash table for signatured types. */
6851
6852 static htab_t
6853 allocate_signatured_type_table (struct objfile *objfile)
6854 {
6855 return htab_create_alloc_ex (41,
6856 hash_signatured_type,
6857 eq_signatured_type,
6858 NULL,
6859 &objfile->objfile_obstack,
6860 hashtab_obstack_allocate,
6861 dummy_obstack_deallocate);
6862 }
6863
6864 /* A helper function to add a signatured type CU to a table. */
6865
6866 static int
6867 add_signatured_type_cu_to_table (void **slot, void *datum)
6868 {
6869 struct signatured_type *sigt = (struct signatured_type *) *slot;
6870 struct signatured_type ***datap = (struct signatured_type ***) datum;
6871
6872 **datap = sigt;
6873 ++*datap;
6874
6875 return 1;
6876 }
6877
6878 /* A helper for create_debug_types_hash_table. Read types from SECTION
6879 and fill them into TYPES_HTAB. It will process only type units,
6880 therefore DW_UT_type. */
6881
6882 static void
6883 create_debug_type_hash_table (struct dwo_file *dwo_file,
6884 dwarf2_section_info *section, htab_t &types_htab,
6885 rcuh_kind section_kind)
6886 {
6887 struct objfile *objfile = dwarf2_per_objfile->objfile;
6888 struct dwarf2_section_info *abbrev_section;
6889 bfd *abfd;
6890 const gdb_byte *info_ptr, *end_ptr;
6891
6892 abbrev_section = (dwo_file != NULL
6893 ? &dwo_file->sections.abbrev
6894 : &dwarf2_per_objfile->abbrev);
6895
6896 if (dwarf_read_debug)
6897 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6898 get_section_name (section),
6899 get_section_file_name (abbrev_section));
6900
6901 dwarf2_read_section (objfile, section);
6902 info_ptr = section->buffer;
6903
6904 if (info_ptr == NULL)
6905 return;
6906
6907 /* We can't set abfd until now because the section may be empty or
6908 not present, in which case the bfd is unknown. */
6909 abfd = get_section_bfd_owner (section);
6910
6911 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6912 because we don't need to read any dies: the signature is in the
6913 header. */
6914
6915 end_ptr = info_ptr + section->size;
6916 while (info_ptr < end_ptr)
6917 {
6918 struct signatured_type *sig_type;
6919 struct dwo_unit *dwo_tu;
6920 void **slot;
6921 const gdb_byte *ptr = info_ptr;
6922 struct comp_unit_head header;
6923 unsigned int length;
6924
6925 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6926
6927 /* Initialize it due to a false compiler warning. */
6928 header.signature = -1;
6929 header.type_cu_offset_in_tu = (cu_offset) -1;
6930
6931 /* We need to read the type's signature in order to build the hash
6932 table, but we don't need anything else just yet. */
6933
6934 ptr = read_and_check_comp_unit_head (&header, section,
6935 abbrev_section, ptr, section_kind);
6936
6937 length = get_cu_length (&header);
6938
6939 /* Skip dummy type units. */
6940 if (ptr >= info_ptr + length
6941 || peek_abbrev_code (abfd, ptr) == 0
6942 || header.unit_type != DW_UT_type)
6943 {
6944 info_ptr += length;
6945 continue;
6946 }
6947
6948 if (types_htab == NULL)
6949 {
6950 if (dwo_file)
6951 types_htab = allocate_dwo_unit_table (objfile);
6952 else
6953 types_htab = allocate_signatured_type_table (objfile);
6954 }
6955
6956 if (dwo_file)
6957 {
6958 sig_type = NULL;
6959 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6960 struct dwo_unit);
6961 dwo_tu->dwo_file = dwo_file;
6962 dwo_tu->signature = header.signature;
6963 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6964 dwo_tu->section = section;
6965 dwo_tu->sect_off = sect_off;
6966 dwo_tu->length = length;
6967 }
6968 else
6969 {
6970 /* N.B.: type_offset is not usable if this type uses a DWO file.
6971 The real type_offset is in the DWO file. */
6972 dwo_tu = NULL;
6973 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6974 struct signatured_type);
6975 sig_type->signature = header.signature;
6976 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6977 sig_type->per_cu.objfile = objfile;
6978 sig_type->per_cu.is_debug_types = 1;
6979 sig_type->per_cu.section = section;
6980 sig_type->per_cu.sect_off = sect_off;
6981 sig_type->per_cu.length = length;
6982 }
6983
6984 slot = htab_find_slot (types_htab,
6985 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6986 INSERT);
6987 gdb_assert (slot != NULL);
6988 if (*slot != NULL)
6989 {
6990 sect_offset dup_sect_off;
6991
6992 if (dwo_file)
6993 {
6994 const struct dwo_unit *dup_tu
6995 = (const struct dwo_unit *) *slot;
6996
6997 dup_sect_off = dup_tu->sect_off;
6998 }
6999 else
7000 {
7001 const struct signatured_type *dup_tu
7002 = (const struct signatured_type *) *slot;
7003
7004 dup_sect_off = dup_tu->per_cu.sect_off;
7005 }
7006
7007 complaint (&symfile_complaints,
7008 _("debug type entry at offset 0x%x is duplicate to"
7009 " the entry at offset 0x%x, signature %s"),
7010 to_underlying (sect_off), to_underlying (dup_sect_off),
7011 hex_string (header.signature));
7012 }
7013 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
7014
7015 if (dwarf_read_debug > 1)
7016 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
7017 to_underlying (sect_off),
7018 hex_string (header.signature));
7019
7020 info_ptr += length;
7021 }
7022 }
7023
7024 /* Create the hash table of all entries in the .debug_types
7025 (or .debug_types.dwo) section(s).
7026 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7027 otherwise it is NULL.
7028
7029 The result is a pointer to the hash table or NULL if there are no types.
7030
7031 Note: This function processes DWO files only, not DWP files. */
7032
7033 static void
7034 create_debug_types_hash_table (struct dwo_file *dwo_file,
7035 VEC (dwarf2_section_info_def) *types,
7036 htab_t &types_htab)
7037 {
7038 int ix;
7039 struct dwarf2_section_info *section;
7040
7041 if (VEC_empty (dwarf2_section_info_def, types))
7042 return;
7043
7044 for (ix = 0;
7045 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7046 ++ix)
7047 create_debug_type_hash_table (dwo_file, section, types_htab,
7048 rcuh_kind::TYPE);
7049 }
7050
7051 /* Create the hash table of all entries in the .debug_types section,
7052 and initialize all_type_units.
7053 The result is zero if there is an error (e.g. missing .debug_types section),
7054 otherwise non-zero. */
7055
7056 static int
7057 create_all_type_units (struct objfile *objfile)
7058 {
7059 htab_t types_htab = NULL;
7060 struct signatured_type **iter;
7061
7062 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
7063 rcuh_kind::COMPILE);
7064 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
7065 if (types_htab == NULL)
7066 {
7067 dwarf2_per_objfile->signatured_types = NULL;
7068 return 0;
7069 }
7070
7071 dwarf2_per_objfile->signatured_types = types_htab;
7072
7073 dwarf2_per_objfile->n_type_units
7074 = dwarf2_per_objfile->n_allocated_type_units
7075 = htab_elements (types_htab);
7076 dwarf2_per_objfile->all_type_units =
7077 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
7078 iter = &dwarf2_per_objfile->all_type_units[0];
7079 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7080 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7081 == dwarf2_per_objfile->n_type_units);
7082
7083 return 1;
7084 }
7085
7086 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7087 If SLOT is non-NULL, it is the entry to use in the hash table.
7088 Otherwise we find one. */
7089
7090 static struct signatured_type *
7091 add_type_unit (ULONGEST sig, void **slot)
7092 {
7093 struct objfile *objfile = dwarf2_per_objfile->objfile;
7094 int n_type_units = dwarf2_per_objfile->n_type_units;
7095 struct signatured_type *sig_type;
7096
7097 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7098 ++n_type_units;
7099 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7100 {
7101 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7102 dwarf2_per_objfile->n_allocated_type_units = 1;
7103 dwarf2_per_objfile->n_allocated_type_units *= 2;
7104 dwarf2_per_objfile->all_type_units
7105 = XRESIZEVEC (struct signatured_type *,
7106 dwarf2_per_objfile->all_type_units,
7107 dwarf2_per_objfile->n_allocated_type_units);
7108 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7109 }
7110 dwarf2_per_objfile->n_type_units = n_type_units;
7111
7112 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7113 struct signatured_type);
7114 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7115 sig_type->signature = sig;
7116 sig_type->per_cu.is_debug_types = 1;
7117 if (dwarf2_per_objfile->using_index)
7118 {
7119 sig_type->per_cu.v.quick =
7120 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7121 struct dwarf2_per_cu_quick_data);
7122 }
7123
7124 if (slot == NULL)
7125 {
7126 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7127 sig_type, INSERT);
7128 }
7129 gdb_assert (*slot == NULL);
7130 *slot = sig_type;
7131 /* The rest of sig_type must be filled in by the caller. */
7132 return sig_type;
7133 }
7134
7135 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7136 Fill in SIG_ENTRY with DWO_ENTRY. */
7137
7138 static void
7139 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
7140 struct signatured_type *sig_entry,
7141 struct dwo_unit *dwo_entry)
7142 {
7143 /* Make sure we're not clobbering something we don't expect to. */
7144 gdb_assert (! sig_entry->per_cu.queued);
7145 gdb_assert (sig_entry->per_cu.cu == NULL);
7146 if (dwarf2_per_objfile->using_index)
7147 {
7148 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7149 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7150 }
7151 else
7152 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7153 gdb_assert (sig_entry->signature == dwo_entry->signature);
7154 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7155 gdb_assert (sig_entry->type_unit_group == NULL);
7156 gdb_assert (sig_entry->dwo_unit == NULL);
7157
7158 sig_entry->per_cu.section = dwo_entry->section;
7159 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7160 sig_entry->per_cu.length = dwo_entry->length;
7161 sig_entry->per_cu.reading_dwo_directly = 1;
7162 sig_entry->per_cu.objfile = objfile;
7163 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7164 sig_entry->dwo_unit = dwo_entry;
7165 }
7166
7167 /* Subroutine of lookup_signatured_type.
7168 If we haven't read the TU yet, create the signatured_type data structure
7169 for a TU to be read in directly from a DWO file, bypassing the stub.
7170 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7171 using .gdb_index, then when reading a CU we want to stay in the DWO file
7172 containing that CU. Otherwise we could end up reading several other DWO
7173 files (due to comdat folding) to process the transitive closure of all the
7174 mentioned TUs, and that can be slow. The current DWO file will have every
7175 type signature that it needs.
7176 We only do this for .gdb_index because in the psymtab case we already have
7177 to read all the DWOs to build the type unit groups. */
7178
7179 static struct signatured_type *
7180 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7181 {
7182 struct objfile *objfile = dwarf2_per_objfile->objfile;
7183 struct dwo_file *dwo_file;
7184 struct dwo_unit find_dwo_entry, *dwo_entry;
7185 struct signatured_type find_sig_entry, *sig_entry;
7186 void **slot;
7187
7188 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7189
7190 /* If TU skeletons have been removed then we may not have read in any
7191 TUs yet. */
7192 if (dwarf2_per_objfile->signatured_types == NULL)
7193 {
7194 dwarf2_per_objfile->signatured_types
7195 = allocate_signatured_type_table (objfile);
7196 }
7197
7198 /* We only ever need to read in one copy of a signatured type.
7199 Use the global signatured_types array to do our own comdat-folding
7200 of types. If this is the first time we're reading this TU, and
7201 the TU has an entry in .gdb_index, replace the recorded data from
7202 .gdb_index with this TU. */
7203
7204 find_sig_entry.signature = sig;
7205 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7206 &find_sig_entry, INSERT);
7207 sig_entry = (struct signatured_type *) *slot;
7208
7209 /* We can get here with the TU already read, *or* in the process of being
7210 read. Don't reassign the global entry to point to this DWO if that's
7211 the case. Also note that if the TU is already being read, it may not
7212 have come from a DWO, the program may be a mix of Fission-compiled
7213 code and non-Fission-compiled code. */
7214
7215 /* Have we already tried to read this TU?
7216 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7217 needn't exist in the global table yet). */
7218 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7219 return sig_entry;
7220
7221 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7222 dwo_unit of the TU itself. */
7223 dwo_file = cu->dwo_unit->dwo_file;
7224
7225 /* Ok, this is the first time we're reading this TU. */
7226 if (dwo_file->tus == NULL)
7227 return NULL;
7228 find_dwo_entry.signature = sig;
7229 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7230 if (dwo_entry == NULL)
7231 return NULL;
7232
7233 /* If the global table doesn't have an entry for this TU, add one. */
7234 if (sig_entry == NULL)
7235 sig_entry = add_type_unit (sig, slot);
7236
7237 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
7238 sig_entry->per_cu.tu_read = 1;
7239 return sig_entry;
7240 }
7241
7242 /* Subroutine of lookup_signatured_type.
7243 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7244 then try the DWP file. If the TU stub (skeleton) has been removed then
7245 it won't be in .gdb_index. */
7246
7247 static struct signatured_type *
7248 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7249 {
7250 struct objfile *objfile = dwarf2_per_objfile->objfile;
7251 struct dwp_file *dwp_file = get_dwp_file ();
7252 struct dwo_unit *dwo_entry;
7253 struct signatured_type find_sig_entry, *sig_entry;
7254 void **slot;
7255
7256 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7257 gdb_assert (dwp_file != NULL);
7258
7259 /* If TU skeletons have been removed then we may not have read in any
7260 TUs yet. */
7261 if (dwarf2_per_objfile->signatured_types == NULL)
7262 {
7263 dwarf2_per_objfile->signatured_types
7264 = allocate_signatured_type_table (objfile);
7265 }
7266
7267 find_sig_entry.signature = sig;
7268 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7269 &find_sig_entry, INSERT);
7270 sig_entry = (struct signatured_type *) *slot;
7271
7272 /* Have we already tried to read this TU?
7273 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7274 needn't exist in the global table yet). */
7275 if (sig_entry != NULL)
7276 return sig_entry;
7277
7278 if (dwp_file->tus == NULL)
7279 return NULL;
7280 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
7281 sig, 1 /* is_debug_types */);
7282 if (dwo_entry == NULL)
7283 return NULL;
7284
7285 sig_entry = add_type_unit (sig, slot);
7286 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
7287
7288 return sig_entry;
7289 }
7290
7291 /* Lookup a signature based type for DW_FORM_ref_sig8.
7292 Returns NULL if signature SIG is not present in the table.
7293 It is up to the caller to complain about this. */
7294
7295 static struct signatured_type *
7296 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7297 {
7298 if (cu->dwo_unit
7299 && dwarf2_per_objfile->using_index)
7300 {
7301 /* We're in a DWO/DWP file, and we're using .gdb_index.
7302 These cases require special processing. */
7303 if (get_dwp_file () == NULL)
7304 return lookup_dwo_signatured_type (cu, sig);
7305 else
7306 return lookup_dwp_signatured_type (cu, sig);
7307 }
7308 else
7309 {
7310 struct signatured_type find_entry, *entry;
7311
7312 if (dwarf2_per_objfile->signatured_types == NULL)
7313 return NULL;
7314 find_entry.signature = sig;
7315 entry = ((struct signatured_type *)
7316 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7317 return entry;
7318 }
7319 }
7320 \f
7321 /* Low level DIE reading support. */
7322
7323 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7324
7325 static void
7326 init_cu_die_reader (struct die_reader_specs *reader,
7327 struct dwarf2_cu *cu,
7328 struct dwarf2_section_info *section,
7329 struct dwo_file *dwo_file)
7330 {
7331 gdb_assert (section->readin && section->buffer != NULL);
7332 reader->abfd = get_section_bfd_owner (section);
7333 reader->cu = cu;
7334 reader->dwo_file = dwo_file;
7335 reader->die_section = section;
7336 reader->buffer = section->buffer;
7337 reader->buffer_end = section->buffer + section->size;
7338 reader->comp_dir = NULL;
7339 }
7340
7341 /* Subroutine of init_cutu_and_read_dies to simplify it.
7342 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7343 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7344 already.
7345
7346 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7347 from it to the DIE in the DWO. If NULL we are skipping the stub.
7348 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7349 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7350 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7351 STUB_COMP_DIR may be non-NULL.
7352 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7353 are filled in with the info of the DIE from the DWO file.
7354 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
7355 provided an abbrev table to use.
7356 The result is non-zero if a valid (non-dummy) DIE was found. */
7357
7358 static int
7359 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7360 struct dwo_unit *dwo_unit,
7361 int abbrev_table_provided,
7362 struct die_info *stub_comp_unit_die,
7363 const char *stub_comp_dir,
7364 struct die_reader_specs *result_reader,
7365 const gdb_byte **result_info_ptr,
7366 struct die_info **result_comp_unit_die,
7367 int *result_has_children)
7368 {
7369 struct objfile *objfile = dwarf2_per_objfile->objfile;
7370 struct dwarf2_cu *cu = this_cu->cu;
7371 struct dwarf2_section_info *section;
7372 bfd *abfd;
7373 const gdb_byte *begin_info_ptr, *info_ptr;
7374 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7375 int i,num_extra_attrs;
7376 struct dwarf2_section_info *dwo_abbrev_section;
7377 struct attribute *attr;
7378 struct die_info *comp_unit_die;
7379
7380 /* At most one of these may be provided. */
7381 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7382
7383 /* These attributes aren't processed until later:
7384 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7385 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7386 referenced later. However, these attributes are found in the stub
7387 which we won't have later. In order to not impose this complication
7388 on the rest of the code, we read them here and copy them to the
7389 DWO CU/TU die. */
7390
7391 stmt_list = NULL;
7392 low_pc = NULL;
7393 high_pc = NULL;
7394 ranges = NULL;
7395 comp_dir = NULL;
7396
7397 if (stub_comp_unit_die != NULL)
7398 {
7399 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7400 DWO file. */
7401 if (! this_cu->is_debug_types)
7402 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7403 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7404 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7405 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7406 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7407
7408 /* There should be a DW_AT_addr_base attribute here (if needed).
7409 We need the value before we can process DW_FORM_GNU_addr_index. */
7410 cu->addr_base = 0;
7411 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7412 if (attr)
7413 cu->addr_base = DW_UNSND (attr);
7414
7415 /* There should be a DW_AT_ranges_base attribute here (if needed).
7416 We need the value before we can process DW_AT_ranges. */
7417 cu->ranges_base = 0;
7418 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7419 if (attr)
7420 cu->ranges_base = DW_UNSND (attr);
7421 }
7422 else if (stub_comp_dir != NULL)
7423 {
7424 /* Reconstruct the comp_dir attribute to simplify the code below. */
7425 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7426 comp_dir->name = DW_AT_comp_dir;
7427 comp_dir->form = DW_FORM_string;
7428 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7429 DW_STRING (comp_dir) = stub_comp_dir;
7430 }
7431
7432 /* Set up for reading the DWO CU/TU. */
7433 cu->dwo_unit = dwo_unit;
7434 section = dwo_unit->section;
7435 dwarf2_read_section (objfile, section);
7436 abfd = get_section_bfd_owner (section);
7437 begin_info_ptr = info_ptr = (section->buffer
7438 + to_underlying (dwo_unit->sect_off));
7439 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7440 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
7441
7442 if (this_cu->is_debug_types)
7443 {
7444 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7445
7446 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
7447 dwo_abbrev_section,
7448 info_ptr, rcuh_kind::TYPE);
7449 /* This is not an assert because it can be caused by bad debug info. */
7450 if (sig_type->signature != cu->header.signature)
7451 {
7452 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7453 " TU at offset 0x%x [in module %s]"),
7454 hex_string (sig_type->signature),
7455 hex_string (cu->header.signature),
7456 to_underlying (dwo_unit->sect_off),
7457 bfd_get_filename (abfd));
7458 }
7459 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7460 /* For DWOs coming from DWP files, we don't know the CU length
7461 nor the type's offset in the TU until now. */
7462 dwo_unit->length = get_cu_length (&cu->header);
7463 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7464
7465 /* Establish the type offset that can be used to lookup the type.
7466 For DWO files, we don't know it until now. */
7467 sig_type->type_offset_in_section
7468 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7469 }
7470 else
7471 {
7472 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
7473 dwo_abbrev_section,
7474 info_ptr, rcuh_kind::COMPILE);
7475 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7476 /* For DWOs coming from DWP files, we don't know the CU length
7477 until now. */
7478 dwo_unit->length = get_cu_length (&cu->header);
7479 }
7480
7481 /* Replace the CU's original abbrev table with the DWO's.
7482 Reminder: We can't read the abbrev table until we've read the header. */
7483 if (abbrev_table_provided)
7484 {
7485 /* Don't free the provided abbrev table, the caller of
7486 init_cutu_and_read_dies owns it. */
7487 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
7488 /* Ensure the DWO abbrev table gets freed. */
7489 make_cleanup (dwarf2_free_abbrev_table, cu);
7490 }
7491 else
7492 {
7493 dwarf2_free_abbrev_table (cu);
7494 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
7495 /* Leave any existing abbrev table cleanup as is. */
7496 }
7497
7498 /* Read in the die, but leave space to copy over the attributes
7499 from the stub. This has the benefit of simplifying the rest of
7500 the code - all the work to maintain the illusion of a single
7501 DW_TAG_{compile,type}_unit DIE is done here. */
7502 num_extra_attrs = ((stmt_list != NULL)
7503 + (low_pc != NULL)
7504 + (high_pc != NULL)
7505 + (ranges != NULL)
7506 + (comp_dir != NULL));
7507 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7508 result_has_children, num_extra_attrs);
7509
7510 /* Copy over the attributes from the stub to the DIE we just read in. */
7511 comp_unit_die = *result_comp_unit_die;
7512 i = comp_unit_die->num_attrs;
7513 if (stmt_list != NULL)
7514 comp_unit_die->attrs[i++] = *stmt_list;
7515 if (low_pc != NULL)
7516 comp_unit_die->attrs[i++] = *low_pc;
7517 if (high_pc != NULL)
7518 comp_unit_die->attrs[i++] = *high_pc;
7519 if (ranges != NULL)
7520 comp_unit_die->attrs[i++] = *ranges;
7521 if (comp_dir != NULL)
7522 comp_unit_die->attrs[i++] = *comp_dir;
7523 comp_unit_die->num_attrs += num_extra_attrs;
7524
7525 if (dwarf_die_debug)
7526 {
7527 fprintf_unfiltered (gdb_stdlog,
7528 "Read die from %s@0x%x of %s:\n",
7529 get_section_name (section),
7530 (unsigned) (begin_info_ptr - section->buffer),
7531 bfd_get_filename (abfd));
7532 dump_die (comp_unit_die, dwarf_die_debug);
7533 }
7534
7535 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7536 TUs by skipping the stub and going directly to the entry in the DWO file.
7537 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7538 to get it via circuitous means. Blech. */
7539 if (comp_dir != NULL)
7540 result_reader->comp_dir = DW_STRING (comp_dir);
7541
7542 /* Skip dummy compilation units. */
7543 if (info_ptr >= begin_info_ptr + dwo_unit->length
7544 || peek_abbrev_code (abfd, info_ptr) == 0)
7545 return 0;
7546
7547 *result_info_ptr = info_ptr;
7548 return 1;
7549 }
7550
7551 /* Subroutine of init_cutu_and_read_dies to simplify it.
7552 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7553 Returns NULL if the specified DWO unit cannot be found. */
7554
7555 static struct dwo_unit *
7556 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7557 struct die_info *comp_unit_die)
7558 {
7559 struct dwarf2_cu *cu = this_cu->cu;
7560 ULONGEST signature;
7561 struct dwo_unit *dwo_unit;
7562 const char *comp_dir, *dwo_name;
7563
7564 gdb_assert (cu != NULL);
7565
7566 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7567 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7568 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7569
7570 if (this_cu->is_debug_types)
7571 {
7572 struct signatured_type *sig_type;
7573
7574 /* Since this_cu is the first member of struct signatured_type,
7575 we can go from a pointer to one to a pointer to the other. */
7576 sig_type = (struct signatured_type *) this_cu;
7577 signature = sig_type->signature;
7578 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7579 }
7580 else
7581 {
7582 struct attribute *attr;
7583
7584 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7585 if (! attr)
7586 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7587 " [in module %s]"),
7588 dwo_name, objfile_name (this_cu->objfile));
7589 signature = DW_UNSND (attr);
7590 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7591 signature);
7592 }
7593
7594 return dwo_unit;
7595 }
7596
7597 /* Subroutine of init_cutu_and_read_dies to simplify it.
7598 See it for a description of the parameters.
7599 Read a TU directly from a DWO file, bypassing the stub.
7600
7601 Note: This function could be a little bit simpler if we shared cleanups
7602 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
7603 to do, so we keep this function self-contained. Or we could move this
7604 into our caller, but it's complex enough already. */
7605
7606 static void
7607 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7608 int use_existing_cu, int keep,
7609 die_reader_func_ftype *die_reader_func,
7610 void *data)
7611 {
7612 struct dwarf2_cu *cu;
7613 struct signatured_type *sig_type;
7614 struct cleanup *cleanups, *free_cu_cleanup = NULL;
7615 struct die_reader_specs reader;
7616 const gdb_byte *info_ptr;
7617 struct die_info *comp_unit_die;
7618 int has_children;
7619
7620 /* Verify we can do the following downcast, and that we have the
7621 data we need. */
7622 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7623 sig_type = (struct signatured_type *) this_cu;
7624 gdb_assert (sig_type->dwo_unit != NULL);
7625
7626 cleanups = make_cleanup (null_cleanup, NULL);
7627
7628 if (use_existing_cu && this_cu->cu != NULL)
7629 {
7630 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7631 cu = this_cu->cu;
7632 /* There's no need to do the rereading_dwo_cu handling that
7633 init_cutu_and_read_dies does since we don't read the stub. */
7634 }
7635 else
7636 {
7637 /* If !use_existing_cu, this_cu->cu must be NULL. */
7638 gdb_assert (this_cu->cu == NULL);
7639 cu = XNEW (struct dwarf2_cu);
7640 init_one_comp_unit (cu, this_cu);
7641 /* If an error occurs while loading, release our storage. */
7642 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
7643 }
7644
7645 /* A future optimization, if needed, would be to use an existing
7646 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7647 could share abbrev tables. */
7648
7649 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7650 0 /* abbrev_table_provided */,
7651 NULL /* stub_comp_unit_die */,
7652 sig_type->dwo_unit->dwo_file->comp_dir,
7653 &reader, &info_ptr,
7654 &comp_unit_die, &has_children) == 0)
7655 {
7656 /* Dummy die. */
7657 do_cleanups (cleanups);
7658 return;
7659 }
7660
7661 /* All the "real" work is done here. */
7662 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7663
7664 /* This duplicates the code in init_cutu_and_read_dies,
7665 but the alternative is making the latter more complex.
7666 This function is only for the special case of using DWO files directly:
7667 no point in overly complicating the general case just to handle this. */
7668 if (free_cu_cleanup != NULL)
7669 {
7670 if (keep)
7671 {
7672 /* We've successfully allocated this compilation unit. Let our
7673 caller clean it up when finished with it. */
7674 discard_cleanups (free_cu_cleanup);
7675
7676 /* We can only discard free_cu_cleanup and all subsequent cleanups.
7677 So we have to manually free the abbrev table. */
7678 dwarf2_free_abbrev_table (cu);
7679
7680 /* Link this CU into read_in_chain. */
7681 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7682 dwarf2_per_objfile->read_in_chain = this_cu;
7683 }
7684 else
7685 do_cleanups (free_cu_cleanup);
7686 }
7687
7688 do_cleanups (cleanups);
7689 }
7690
7691 /* Initialize a CU (or TU) and read its DIEs.
7692 If the CU defers to a DWO file, read the DWO file as well.
7693
7694 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7695 Otherwise the table specified in the comp unit header is read in and used.
7696 This is an optimization for when we already have the abbrev table.
7697
7698 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7699 Otherwise, a new CU is allocated with xmalloc.
7700
7701 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7702 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7703
7704 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7705 linker) then DIE_READER_FUNC will not get called. */
7706
7707 static void
7708 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7709 struct abbrev_table *abbrev_table,
7710 int use_existing_cu, int keep,
7711 die_reader_func_ftype *die_reader_func,
7712 void *data)
7713 {
7714 struct objfile *objfile = dwarf2_per_objfile->objfile;
7715 struct dwarf2_section_info *section = this_cu->section;
7716 bfd *abfd = get_section_bfd_owner (section);
7717 struct dwarf2_cu *cu;
7718 const gdb_byte *begin_info_ptr, *info_ptr;
7719 struct die_reader_specs reader;
7720 struct die_info *comp_unit_die;
7721 int has_children;
7722 struct attribute *attr;
7723 struct cleanup *cleanups, *free_cu_cleanup = NULL;
7724 struct signatured_type *sig_type = NULL;
7725 struct dwarf2_section_info *abbrev_section;
7726 /* Non-zero if CU currently points to a DWO file and we need to
7727 reread it. When this happens we need to reread the skeleton die
7728 before we can reread the DWO file (this only applies to CUs, not TUs). */
7729 int rereading_dwo_cu = 0;
7730
7731 if (dwarf_die_debug)
7732 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
7733 this_cu->is_debug_types ? "type" : "comp",
7734 to_underlying (this_cu->sect_off));
7735
7736 if (use_existing_cu)
7737 gdb_assert (keep);
7738
7739 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7740 file (instead of going through the stub), short-circuit all of this. */
7741 if (this_cu->reading_dwo_directly)
7742 {
7743 /* Narrow down the scope of possibilities to have to understand. */
7744 gdb_assert (this_cu->is_debug_types);
7745 gdb_assert (abbrev_table == NULL);
7746 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7747 die_reader_func, data);
7748 return;
7749 }
7750
7751 cleanups = make_cleanup (null_cleanup, NULL);
7752
7753 /* This is cheap if the section is already read in. */
7754 dwarf2_read_section (objfile, section);
7755
7756 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7757
7758 abbrev_section = get_abbrev_section_for_cu (this_cu);
7759
7760 if (use_existing_cu && this_cu->cu != NULL)
7761 {
7762 cu = this_cu->cu;
7763 /* If this CU is from a DWO file we need to start over, we need to
7764 refetch the attributes from the skeleton CU.
7765 This could be optimized by retrieving those attributes from when we
7766 were here the first time: the previous comp_unit_die was stored in
7767 comp_unit_obstack. But there's no data yet that we need this
7768 optimization. */
7769 if (cu->dwo_unit != NULL)
7770 rereading_dwo_cu = 1;
7771 }
7772 else
7773 {
7774 /* If !use_existing_cu, this_cu->cu must be NULL. */
7775 gdb_assert (this_cu->cu == NULL);
7776 cu = XNEW (struct dwarf2_cu);
7777 init_one_comp_unit (cu, this_cu);
7778 /* If an error occurs while loading, release our storage. */
7779 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
7780 }
7781
7782 /* Get the header. */
7783 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7784 {
7785 /* We already have the header, there's no need to read it in again. */
7786 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7787 }
7788 else
7789 {
7790 if (this_cu->is_debug_types)
7791 {
7792 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
7793 abbrev_section, info_ptr,
7794 rcuh_kind::TYPE);
7795
7796 /* Since per_cu is the first member of struct signatured_type,
7797 we can go from a pointer to one to a pointer to the other. */
7798 sig_type = (struct signatured_type *) this_cu;
7799 gdb_assert (sig_type->signature == cu->header.signature);
7800 gdb_assert (sig_type->type_offset_in_tu
7801 == cu->header.type_cu_offset_in_tu);
7802 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7803
7804 /* LENGTH has not been set yet for type units if we're
7805 using .gdb_index. */
7806 this_cu->length = get_cu_length (&cu->header);
7807
7808 /* Establish the type offset that can be used to lookup the type. */
7809 sig_type->type_offset_in_section =
7810 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7811
7812 this_cu->dwarf_version = cu->header.version;
7813 }
7814 else
7815 {
7816 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
7817 abbrev_section,
7818 info_ptr,
7819 rcuh_kind::COMPILE);
7820
7821 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7822 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7823 this_cu->dwarf_version = cu->header.version;
7824 }
7825 }
7826
7827 /* Skip dummy compilation units. */
7828 if (info_ptr >= begin_info_ptr + this_cu->length
7829 || peek_abbrev_code (abfd, info_ptr) == 0)
7830 {
7831 do_cleanups (cleanups);
7832 return;
7833 }
7834
7835 /* If we don't have them yet, read the abbrevs for this compilation unit.
7836 And if we need to read them now, make sure they're freed when we're
7837 done. Note that it's important that if the CU had an abbrev table
7838 on entry we don't free it when we're done: Somewhere up the call stack
7839 it may be in use. */
7840 if (abbrev_table != NULL)
7841 {
7842 gdb_assert (cu->abbrev_table == NULL);
7843 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7844 cu->abbrev_table = abbrev_table;
7845 }
7846 else if (cu->abbrev_table == NULL)
7847 {
7848 dwarf2_read_abbrevs (cu, abbrev_section);
7849 make_cleanup (dwarf2_free_abbrev_table, cu);
7850 }
7851 else if (rereading_dwo_cu)
7852 {
7853 dwarf2_free_abbrev_table (cu);
7854 dwarf2_read_abbrevs (cu, abbrev_section);
7855 }
7856
7857 /* Read the top level CU/TU die. */
7858 init_cu_die_reader (&reader, cu, section, NULL);
7859 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7860
7861 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7862 from the DWO file.
7863 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7864 DWO CU, that this test will fail (the attribute will not be present). */
7865 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7866 if (attr)
7867 {
7868 struct dwo_unit *dwo_unit;
7869 struct die_info *dwo_comp_unit_die;
7870
7871 if (has_children)
7872 {
7873 complaint (&symfile_complaints,
7874 _("compilation unit with DW_AT_GNU_dwo_name"
7875 " has children (offset 0x%x) [in module %s]"),
7876 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
7877 }
7878 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7879 if (dwo_unit != NULL)
7880 {
7881 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7882 abbrev_table != NULL,
7883 comp_unit_die, NULL,
7884 &reader, &info_ptr,
7885 &dwo_comp_unit_die, &has_children) == 0)
7886 {
7887 /* Dummy die. */
7888 do_cleanups (cleanups);
7889 return;
7890 }
7891 comp_unit_die = dwo_comp_unit_die;
7892 }
7893 else
7894 {
7895 /* Yikes, we couldn't find the rest of the DIE, we only have
7896 the stub. A complaint has already been logged. There's
7897 not much more we can do except pass on the stub DIE to
7898 die_reader_func. We don't want to throw an error on bad
7899 debug info. */
7900 }
7901 }
7902
7903 /* All of the above is setup for this call. Yikes. */
7904 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7905
7906 /* Done, clean up. */
7907 if (free_cu_cleanup != NULL)
7908 {
7909 if (keep)
7910 {
7911 /* We've successfully allocated this compilation unit. Let our
7912 caller clean it up when finished with it. */
7913 discard_cleanups (free_cu_cleanup);
7914
7915 /* We can only discard free_cu_cleanup and all subsequent cleanups.
7916 So we have to manually free the abbrev table. */
7917 dwarf2_free_abbrev_table (cu);
7918
7919 /* Link this CU into read_in_chain. */
7920 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7921 dwarf2_per_objfile->read_in_chain = this_cu;
7922 }
7923 else
7924 do_cleanups (free_cu_cleanup);
7925 }
7926
7927 do_cleanups (cleanups);
7928 }
7929
7930 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7931 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7932 to have already done the lookup to find the DWO file).
7933
7934 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7935 THIS_CU->is_debug_types, but nothing else.
7936
7937 We fill in THIS_CU->length.
7938
7939 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7940 linker) then DIE_READER_FUNC will not get called.
7941
7942 THIS_CU->cu is always freed when done.
7943 This is done in order to not leave THIS_CU->cu in a state where we have
7944 to care whether it refers to the "main" CU or the DWO CU. */
7945
7946 static void
7947 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7948 struct dwo_file *dwo_file,
7949 die_reader_func_ftype *die_reader_func,
7950 void *data)
7951 {
7952 struct objfile *objfile = dwarf2_per_objfile->objfile;
7953 struct dwarf2_section_info *section = this_cu->section;
7954 bfd *abfd = get_section_bfd_owner (section);
7955 struct dwarf2_section_info *abbrev_section;
7956 struct dwarf2_cu cu;
7957 const gdb_byte *begin_info_ptr, *info_ptr;
7958 struct die_reader_specs reader;
7959 struct cleanup *cleanups;
7960 struct die_info *comp_unit_die;
7961 int has_children;
7962
7963 if (dwarf_die_debug)
7964 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
7965 this_cu->is_debug_types ? "type" : "comp",
7966 to_underlying (this_cu->sect_off));
7967
7968 gdb_assert (this_cu->cu == NULL);
7969
7970 abbrev_section = (dwo_file != NULL
7971 ? &dwo_file->sections.abbrev
7972 : get_abbrev_section_for_cu (this_cu));
7973
7974 /* This is cheap if the section is already read in. */
7975 dwarf2_read_section (objfile, section);
7976
7977 init_one_comp_unit (&cu, this_cu);
7978
7979 cleanups = make_cleanup (free_stack_comp_unit, &cu);
7980
7981 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7982 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
7983 abbrev_section, info_ptr,
7984 (this_cu->is_debug_types
7985 ? rcuh_kind::TYPE
7986 : rcuh_kind::COMPILE));
7987
7988 this_cu->length = get_cu_length (&cu.header);
7989
7990 /* Skip dummy compilation units. */
7991 if (info_ptr >= begin_info_ptr + this_cu->length
7992 || peek_abbrev_code (abfd, info_ptr) == 0)
7993 {
7994 do_cleanups (cleanups);
7995 return;
7996 }
7997
7998 dwarf2_read_abbrevs (&cu, abbrev_section);
7999 make_cleanup (dwarf2_free_abbrev_table, &cu);
8000
8001 init_cu_die_reader (&reader, &cu, section, dwo_file);
8002 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8003
8004 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8005
8006 do_cleanups (cleanups);
8007 }
8008
8009 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
8010 does not lookup the specified DWO file.
8011 This cannot be used to read DWO files.
8012
8013 THIS_CU->cu is always freed when done.
8014 This is done in order to not leave THIS_CU->cu in a state where we have
8015 to care whether it refers to the "main" CU or the DWO CU.
8016 We can revisit this if the data shows there's a performance issue. */
8017
8018 static void
8019 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8020 die_reader_func_ftype *die_reader_func,
8021 void *data)
8022 {
8023 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
8024 }
8025 \f
8026 /* Type Unit Groups.
8027
8028 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8029 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8030 so that all types coming from the same compilation (.o file) are grouped
8031 together. A future step could be to put the types in the same symtab as
8032 the CU the types ultimately came from. */
8033
8034 static hashval_t
8035 hash_type_unit_group (const void *item)
8036 {
8037 const struct type_unit_group *tu_group
8038 = (const struct type_unit_group *) item;
8039
8040 return hash_stmt_list_entry (&tu_group->hash);
8041 }
8042
8043 static int
8044 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
8045 {
8046 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8047 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
8048
8049 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
8050 }
8051
8052 /* Allocate a hash table for type unit groups. */
8053
8054 static htab_t
8055 allocate_type_unit_groups_table (void)
8056 {
8057 return htab_create_alloc_ex (3,
8058 hash_type_unit_group,
8059 eq_type_unit_group,
8060 NULL,
8061 &dwarf2_per_objfile->objfile->objfile_obstack,
8062 hashtab_obstack_allocate,
8063 dummy_obstack_deallocate);
8064 }
8065
8066 /* Type units that don't have DW_AT_stmt_list are grouped into their own
8067 partial symtabs. We combine several TUs per psymtab to not let the size
8068 of any one psymtab grow too big. */
8069 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8070 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
8071
8072 /* Helper routine for get_type_unit_group.
8073 Create the type_unit_group object used to hold one or more TUs. */
8074
8075 static struct type_unit_group *
8076 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
8077 {
8078 struct objfile *objfile = dwarf2_per_objfile->objfile;
8079 struct dwarf2_per_cu_data *per_cu;
8080 struct type_unit_group *tu_group;
8081
8082 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8083 struct type_unit_group);
8084 per_cu = &tu_group->per_cu;
8085 per_cu->objfile = objfile;
8086
8087 if (dwarf2_per_objfile->using_index)
8088 {
8089 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8090 struct dwarf2_per_cu_quick_data);
8091 }
8092 else
8093 {
8094 unsigned int line_offset = to_underlying (line_offset_struct);
8095 struct partial_symtab *pst;
8096 char *name;
8097
8098 /* Give the symtab a useful name for debug purposes. */
8099 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8100 name = xstrprintf ("<type_units_%d>",
8101 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8102 else
8103 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8104
8105 pst = create_partial_symtab (per_cu, name);
8106 pst->anonymous = 1;
8107
8108 xfree (name);
8109 }
8110
8111 tu_group->hash.dwo_unit = cu->dwo_unit;
8112 tu_group->hash.line_sect_off = line_offset_struct;
8113
8114 return tu_group;
8115 }
8116
8117 /* Look up the type_unit_group for type unit CU, and create it if necessary.
8118 STMT_LIST is a DW_AT_stmt_list attribute. */
8119
8120 static struct type_unit_group *
8121 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
8122 {
8123 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8124 struct type_unit_group *tu_group;
8125 void **slot;
8126 unsigned int line_offset;
8127 struct type_unit_group type_unit_group_for_lookup;
8128
8129 if (dwarf2_per_objfile->type_unit_groups == NULL)
8130 {
8131 dwarf2_per_objfile->type_unit_groups =
8132 allocate_type_unit_groups_table ();
8133 }
8134
8135 /* Do we need to create a new group, or can we use an existing one? */
8136
8137 if (stmt_list)
8138 {
8139 line_offset = DW_UNSND (stmt_list);
8140 ++tu_stats->nr_symtab_sharers;
8141 }
8142 else
8143 {
8144 /* Ugh, no stmt_list. Rare, but we have to handle it.
8145 We can do various things here like create one group per TU or
8146 spread them over multiple groups to split up the expansion work.
8147 To avoid worst case scenarios (too many groups or too large groups)
8148 we, umm, group them in bunches. */
8149 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8150 | (tu_stats->nr_stmt_less_type_units
8151 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8152 ++tu_stats->nr_stmt_less_type_units;
8153 }
8154
8155 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
8156 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
8157 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8158 &type_unit_group_for_lookup, INSERT);
8159 if (*slot != NULL)
8160 {
8161 tu_group = (struct type_unit_group *) *slot;
8162 gdb_assert (tu_group != NULL);
8163 }
8164 else
8165 {
8166 sect_offset line_offset_struct = (sect_offset) line_offset;
8167 tu_group = create_type_unit_group (cu, line_offset_struct);
8168 *slot = tu_group;
8169 ++tu_stats->nr_symtabs;
8170 }
8171
8172 return tu_group;
8173 }
8174 \f
8175 /* Partial symbol tables. */
8176
8177 /* Create a psymtab named NAME and assign it to PER_CU.
8178
8179 The caller must fill in the following details:
8180 dirname, textlow, texthigh. */
8181
8182 static struct partial_symtab *
8183 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8184 {
8185 struct objfile *objfile = per_cu->objfile;
8186 struct partial_symtab *pst;
8187
8188 pst = start_psymtab_common (objfile, name, 0,
8189 objfile->global_psymbols,
8190 objfile->static_psymbols);
8191
8192 pst->psymtabs_addrmap_supported = 1;
8193
8194 /* This is the glue that links PST into GDB's symbol API. */
8195 pst->read_symtab_private = per_cu;
8196 pst->read_symtab = dwarf2_read_symtab;
8197 per_cu->v.psymtab = pst;
8198
8199 return pst;
8200 }
8201
8202 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8203 type. */
8204
8205 struct process_psymtab_comp_unit_data
8206 {
8207 /* True if we are reading a DW_TAG_partial_unit. */
8208
8209 int want_partial_unit;
8210
8211 /* The "pretend" language that is used if the CU doesn't declare a
8212 language. */
8213
8214 enum language pretend_language;
8215 };
8216
8217 /* die_reader_func for process_psymtab_comp_unit. */
8218
8219 static void
8220 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8221 const gdb_byte *info_ptr,
8222 struct die_info *comp_unit_die,
8223 int has_children,
8224 void *data)
8225 {
8226 struct dwarf2_cu *cu = reader->cu;
8227 struct objfile *objfile = cu->objfile;
8228 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8229 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8230 CORE_ADDR baseaddr;
8231 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8232 struct partial_symtab *pst;
8233 enum pc_bounds_kind cu_bounds_kind;
8234 const char *filename;
8235 struct process_psymtab_comp_unit_data *info
8236 = (struct process_psymtab_comp_unit_data *) data;
8237
8238 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8239 return;
8240
8241 gdb_assert (! per_cu->is_debug_types);
8242
8243 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8244
8245 cu->list_in_scope = &file_symbols;
8246
8247 /* Allocate a new partial symbol table structure. */
8248 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8249 if (filename == NULL)
8250 filename = "";
8251
8252 pst = create_partial_symtab (per_cu, filename);
8253
8254 /* This must be done before calling dwarf2_build_include_psymtabs. */
8255 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8256
8257 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8258
8259 dwarf2_find_base_address (comp_unit_die, cu);
8260
8261 /* Possibly set the default values of LOWPC and HIGHPC from
8262 `DW_AT_ranges'. */
8263 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8264 &best_highpc, cu, pst);
8265 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8266 /* Store the contiguous range if it is not empty; it can be empty for
8267 CUs with no code. */
8268 addrmap_set_empty (objfile->psymtabs_addrmap,
8269 gdbarch_adjust_dwarf2_addr (gdbarch,
8270 best_lowpc + baseaddr),
8271 gdbarch_adjust_dwarf2_addr (gdbarch,
8272 best_highpc + baseaddr) - 1,
8273 pst);
8274
8275 /* Check if comp unit has_children.
8276 If so, read the rest of the partial symbols from this comp unit.
8277 If not, there's no more debug_info for this comp unit. */
8278 if (has_children)
8279 {
8280 struct partial_die_info *first_die;
8281 CORE_ADDR lowpc, highpc;
8282
8283 lowpc = ((CORE_ADDR) -1);
8284 highpc = ((CORE_ADDR) 0);
8285
8286 first_die = load_partial_dies (reader, info_ptr, 1);
8287
8288 scan_partial_symbols (first_die, &lowpc, &highpc,
8289 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8290
8291 /* If we didn't find a lowpc, set it to highpc to avoid
8292 complaints from `maint check'. */
8293 if (lowpc == ((CORE_ADDR) -1))
8294 lowpc = highpc;
8295
8296 /* If the compilation unit didn't have an explicit address range,
8297 then use the information extracted from its child dies. */
8298 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8299 {
8300 best_lowpc = lowpc;
8301 best_highpc = highpc;
8302 }
8303 }
8304 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8305 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
8306
8307 end_psymtab_common (objfile, pst);
8308
8309 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8310 {
8311 int i;
8312 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8313 struct dwarf2_per_cu_data *iter;
8314
8315 /* Fill in 'dependencies' here; we fill in 'users' in a
8316 post-pass. */
8317 pst->number_of_dependencies = len;
8318 pst->dependencies =
8319 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8320 for (i = 0;
8321 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8322 i, iter);
8323 ++i)
8324 pst->dependencies[i] = iter->v.psymtab;
8325
8326 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8327 }
8328
8329 /* Get the list of files included in the current compilation unit,
8330 and build a psymtab for each of them. */
8331 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8332
8333 if (dwarf_read_debug)
8334 {
8335 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8336
8337 fprintf_unfiltered (gdb_stdlog,
8338 "Psymtab for %s unit @0x%x: %s - %s"
8339 ", %d global, %d static syms\n",
8340 per_cu->is_debug_types ? "type" : "comp",
8341 to_underlying (per_cu->sect_off),
8342 paddress (gdbarch, pst->textlow),
8343 paddress (gdbarch, pst->texthigh),
8344 pst->n_global_syms, pst->n_static_syms);
8345 }
8346 }
8347
8348 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8349 Process compilation unit THIS_CU for a psymtab. */
8350
8351 static void
8352 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8353 int want_partial_unit,
8354 enum language pretend_language)
8355 {
8356 /* If this compilation unit was already read in, free the
8357 cached copy in order to read it in again. This is
8358 necessary because we skipped some symbols when we first
8359 read in the compilation unit (see load_partial_dies).
8360 This problem could be avoided, but the benefit is unclear. */
8361 if (this_cu->cu != NULL)
8362 free_one_cached_comp_unit (this_cu);
8363
8364 if (this_cu->is_debug_types)
8365 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8366 NULL);
8367 else
8368 {
8369 process_psymtab_comp_unit_data info;
8370 info.want_partial_unit = want_partial_unit;
8371 info.pretend_language = pretend_language;
8372 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8373 process_psymtab_comp_unit_reader, &info);
8374 }
8375
8376 /* Age out any secondary CUs. */
8377 age_cached_comp_units ();
8378 }
8379
8380 /* Reader function for build_type_psymtabs. */
8381
8382 static void
8383 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8384 const gdb_byte *info_ptr,
8385 struct die_info *type_unit_die,
8386 int has_children,
8387 void *data)
8388 {
8389 struct objfile *objfile = dwarf2_per_objfile->objfile;
8390 struct dwarf2_cu *cu = reader->cu;
8391 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8392 struct signatured_type *sig_type;
8393 struct type_unit_group *tu_group;
8394 struct attribute *attr;
8395 struct partial_die_info *first_die;
8396 CORE_ADDR lowpc, highpc;
8397 struct partial_symtab *pst;
8398
8399 gdb_assert (data == NULL);
8400 gdb_assert (per_cu->is_debug_types);
8401 sig_type = (struct signatured_type *) per_cu;
8402
8403 if (! has_children)
8404 return;
8405
8406 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8407 tu_group = get_type_unit_group (cu, attr);
8408
8409 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8410
8411 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8412 cu->list_in_scope = &file_symbols;
8413 pst = create_partial_symtab (per_cu, "");
8414 pst->anonymous = 1;
8415
8416 first_die = load_partial_dies (reader, info_ptr, 1);
8417
8418 lowpc = (CORE_ADDR) -1;
8419 highpc = (CORE_ADDR) 0;
8420 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8421
8422 end_psymtab_common (objfile, pst);
8423 }
8424
8425 /* Struct used to sort TUs by their abbreviation table offset. */
8426
8427 struct tu_abbrev_offset
8428 {
8429 struct signatured_type *sig_type;
8430 sect_offset abbrev_offset;
8431 };
8432
8433 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
8434
8435 static int
8436 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
8437 {
8438 const struct tu_abbrev_offset * const *a
8439 = (const struct tu_abbrev_offset * const*) ap;
8440 const struct tu_abbrev_offset * const *b
8441 = (const struct tu_abbrev_offset * const*) bp;
8442 sect_offset aoff = (*a)->abbrev_offset;
8443 sect_offset boff = (*b)->abbrev_offset;
8444
8445 return (aoff > boff) - (aoff < boff);
8446 }
8447
8448 /* Efficiently read all the type units.
8449 This does the bulk of the work for build_type_psymtabs.
8450
8451 The efficiency is because we sort TUs by the abbrev table they use and
8452 only read each abbrev table once. In one program there are 200K TUs
8453 sharing 8K abbrev tables.
8454
8455 The main purpose of this function is to support building the
8456 dwarf2_per_objfile->type_unit_groups table.
8457 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8458 can collapse the search space by grouping them by stmt_list.
8459 The savings can be significant, in the same program from above the 200K TUs
8460 share 8K stmt_list tables.
8461
8462 FUNC is expected to call get_type_unit_group, which will create the
8463 struct type_unit_group if necessary and add it to
8464 dwarf2_per_objfile->type_unit_groups. */
8465
8466 static void
8467 build_type_psymtabs_1 (void)
8468 {
8469 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8470 struct cleanup *cleanups;
8471 struct abbrev_table *abbrev_table;
8472 sect_offset abbrev_offset;
8473 struct tu_abbrev_offset *sorted_by_abbrev;
8474 int i;
8475
8476 /* It's up to the caller to not call us multiple times. */
8477 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8478
8479 if (dwarf2_per_objfile->n_type_units == 0)
8480 return;
8481
8482 /* TUs typically share abbrev tables, and there can be way more TUs than
8483 abbrev tables. Sort by abbrev table to reduce the number of times we
8484 read each abbrev table in.
8485 Alternatives are to punt or to maintain a cache of abbrev tables.
8486 This is simpler and efficient enough for now.
8487
8488 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8489 symtab to use). Typically TUs with the same abbrev offset have the same
8490 stmt_list value too so in practice this should work well.
8491
8492 The basic algorithm here is:
8493
8494 sort TUs by abbrev table
8495 for each TU with same abbrev table:
8496 read abbrev table if first user
8497 read TU top level DIE
8498 [IWBN if DWO skeletons had DW_AT_stmt_list]
8499 call FUNC */
8500
8501 if (dwarf_read_debug)
8502 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8503
8504 /* Sort in a separate table to maintain the order of all_type_units
8505 for .gdb_index: TU indices directly index all_type_units. */
8506 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
8507 dwarf2_per_objfile->n_type_units);
8508 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8509 {
8510 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8511
8512 sorted_by_abbrev[i].sig_type = sig_type;
8513 sorted_by_abbrev[i].abbrev_offset =
8514 read_abbrev_offset (sig_type->per_cu.section,
8515 sig_type->per_cu.sect_off);
8516 }
8517 cleanups = make_cleanup (xfree, sorted_by_abbrev);
8518 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
8519 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
8520
8521 abbrev_offset = (sect_offset) ~(unsigned) 0;
8522 abbrev_table = NULL;
8523 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
8524
8525 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8526 {
8527 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8528
8529 /* Switch to the next abbrev table if necessary. */
8530 if (abbrev_table == NULL
8531 || tu->abbrev_offset != abbrev_offset)
8532 {
8533 if (abbrev_table != NULL)
8534 {
8535 abbrev_table_free (abbrev_table);
8536 /* Reset to NULL in case abbrev_table_read_table throws
8537 an error: abbrev_table_free_cleanup will get called. */
8538 abbrev_table = NULL;
8539 }
8540 abbrev_offset = tu->abbrev_offset;
8541 abbrev_table =
8542 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
8543 abbrev_offset);
8544 ++tu_stats->nr_uniq_abbrev_tables;
8545 }
8546
8547 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
8548 build_type_psymtabs_reader, NULL);
8549 }
8550
8551 do_cleanups (cleanups);
8552 }
8553
8554 /* Print collected type unit statistics. */
8555
8556 static void
8557 print_tu_stats (void)
8558 {
8559 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8560
8561 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8562 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8563 dwarf2_per_objfile->n_type_units);
8564 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8565 tu_stats->nr_uniq_abbrev_tables);
8566 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8567 tu_stats->nr_symtabs);
8568 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8569 tu_stats->nr_symtab_sharers);
8570 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8571 tu_stats->nr_stmt_less_type_units);
8572 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8573 tu_stats->nr_all_type_units_reallocs);
8574 }
8575
8576 /* Traversal function for build_type_psymtabs. */
8577
8578 static int
8579 build_type_psymtab_dependencies (void **slot, void *info)
8580 {
8581 struct objfile *objfile = dwarf2_per_objfile->objfile;
8582 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8583 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8584 struct partial_symtab *pst = per_cu->v.psymtab;
8585 int len = VEC_length (sig_type_ptr, tu_group->tus);
8586 struct signatured_type *iter;
8587 int i;
8588
8589 gdb_assert (len > 0);
8590 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8591
8592 pst->number_of_dependencies = len;
8593 pst->dependencies =
8594 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8595 for (i = 0;
8596 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8597 ++i)
8598 {
8599 gdb_assert (iter->per_cu.is_debug_types);
8600 pst->dependencies[i] = iter->per_cu.v.psymtab;
8601 iter->type_unit_group = tu_group;
8602 }
8603
8604 VEC_free (sig_type_ptr, tu_group->tus);
8605
8606 return 1;
8607 }
8608
8609 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8610 Build partial symbol tables for the .debug_types comp-units. */
8611
8612 static void
8613 build_type_psymtabs (struct objfile *objfile)
8614 {
8615 if (! create_all_type_units (objfile))
8616 return;
8617
8618 build_type_psymtabs_1 ();
8619 }
8620
8621 /* Traversal function for process_skeletonless_type_unit.
8622 Read a TU in a DWO file and build partial symbols for it. */
8623
8624 static int
8625 process_skeletonless_type_unit (void **slot, void *info)
8626 {
8627 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8628 struct objfile *objfile = (struct objfile *) info;
8629 struct signatured_type find_entry, *entry;
8630
8631 /* If this TU doesn't exist in the global table, add it and read it in. */
8632
8633 if (dwarf2_per_objfile->signatured_types == NULL)
8634 {
8635 dwarf2_per_objfile->signatured_types
8636 = allocate_signatured_type_table (objfile);
8637 }
8638
8639 find_entry.signature = dwo_unit->signature;
8640 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8641 INSERT);
8642 /* If we've already seen this type there's nothing to do. What's happening
8643 is we're doing our own version of comdat-folding here. */
8644 if (*slot != NULL)
8645 return 1;
8646
8647 /* This does the job that create_all_type_units would have done for
8648 this TU. */
8649 entry = add_type_unit (dwo_unit->signature, slot);
8650 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
8651 *slot = entry;
8652
8653 /* This does the job that build_type_psymtabs_1 would have done. */
8654 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8655 build_type_psymtabs_reader, NULL);
8656
8657 return 1;
8658 }
8659
8660 /* Traversal function for process_skeletonless_type_units. */
8661
8662 static int
8663 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8664 {
8665 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8666
8667 if (dwo_file->tus != NULL)
8668 {
8669 htab_traverse_noresize (dwo_file->tus,
8670 process_skeletonless_type_unit, info);
8671 }
8672
8673 return 1;
8674 }
8675
8676 /* Scan all TUs of DWO files, verifying we've processed them.
8677 This is needed in case a TU was emitted without its skeleton.
8678 Note: This can't be done until we know what all the DWO files are. */
8679
8680 static void
8681 process_skeletonless_type_units (struct objfile *objfile)
8682 {
8683 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8684 if (get_dwp_file () == NULL
8685 && dwarf2_per_objfile->dwo_files != NULL)
8686 {
8687 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8688 process_dwo_file_for_skeletonless_type_units,
8689 objfile);
8690 }
8691 }
8692
8693 /* Compute the 'user' field for each psymtab in OBJFILE. */
8694
8695 static void
8696 set_partial_user (struct objfile *objfile)
8697 {
8698 int i;
8699
8700 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8701 {
8702 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
8703 struct partial_symtab *pst = per_cu->v.psymtab;
8704 int j;
8705
8706 if (pst == NULL)
8707 continue;
8708
8709 for (j = 0; j < pst->number_of_dependencies; ++j)
8710 {
8711 /* Set the 'user' field only if it is not already set. */
8712 if (pst->dependencies[j]->user == NULL)
8713 pst->dependencies[j]->user = pst;
8714 }
8715 }
8716 }
8717
8718 /* Build the partial symbol table by doing a quick pass through the
8719 .debug_info and .debug_abbrev sections. */
8720
8721 static void
8722 dwarf2_build_psymtabs_hard (struct objfile *objfile)
8723 {
8724 struct cleanup *back_to;
8725 int i;
8726
8727 if (dwarf_read_debug)
8728 {
8729 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8730 objfile_name (objfile));
8731 }
8732
8733 dwarf2_per_objfile->reading_partial_symbols = 1;
8734
8735 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8736
8737 /* Any cached compilation units will be linked by the per-objfile
8738 read_in_chain. Make sure to free them when we're done. */
8739 back_to = make_cleanup (free_cached_comp_units, NULL);
8740
8741 build_type_psymtabs (objfile);
8742
8743 create_all_comp_units (objfile);
8744
8745 /* Create a temporary address map on a temporary obstack. We later
8746 copy this to the final obstack. */
8747 auto_obstack temp_obstack;
8748
8749 scoped_restore save_psymtabs_addrmap
8750 = make_scoped_restore (&objfile->psymtabs_addrmap,
8751 addrmap_create_mutable (&temp_obstack));
8752
8753 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8754 {
8755 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
8756
8757 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8758 }
8759
8760 /* This has to wait until we read the CUs, we need the list of DWOs. */
8761 process_skeletonless_type_units (objfile);
8762
8763 /* Now that all TUs have been processed we can fill in the dependencies. */
8764 if (dwarf2_per_objfile->type_unit_groups != NULL)
8765 {
8766 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8767 build_type_psymtab_dependencies, NULL);
8768 }
8769
8770 if (dwarf_read_debug)
8771 print_tu_stats ();
8772
8773 set_partial_user (objfile);
8774
8775 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8776 &objfile->objfile_obstack);
8777 /* At this point we want to keep the address map. */
8778 save_psymtabs_addrmap.release ();
8779
8780 do_cleanups (back_to);
8781
8782 if (dwarf_read_debug)
8783 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8784 objfile_name (objfile));
8785 }
8786
8787 /* die_reader_func for load_partial_comp_unit. */
8788
8789 static void
8790 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8791 const gdb_byte *info_ptr,
8792 struct die_info *comp_unit_die,
8793 int has_children,
8794 void *data)
8795 {
8796 struct dwarf2_cu *cu = reader->cu;
8797
8798 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8799
8800 /* Check if comp unit has_children.
8801 If so, read the rest of the partial symbols from this comp unit.
8802 If not, there's no more debug_info for this comp unit. */
8803 if (has_children)
8804 load_partial_dies (reader, info_ptr, 0);
8805 }
8806
8807 /* Load the partial DIEs for a secondary CU into memory.
8808 This is also used when rereading a primary CU with load_all_dies. */
8809
8810 static void
8811 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8812 {
8813 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8814 load_partial_comp_unit_reader, NULL);
8815 }
8816
8817 static void
8818 read_comp_units_from_section (struct objfile *objfile,
8819 struct dwarf2_section_info *section,
8820 struct dwarf2_section_info *abbrev_section,
8821 unsigned int is_dwz,
8822 int *n_allocated,
8823 int *n_comp_units,
8824 struct dwarf2_per_cu_data ***all_comp_units)
8825 {
8826 const gdb_byte *info_ptr;
8827
8828 if (dwarf_read_debug)
8829 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8830 get_section_name (section),
8831 get_section_file_name (section));
8832
8833 dwarf2_read_section (objfile, section);
8834
8835 info_ptr = section->buffer;
8836
8837 while (info_ptr < section->buffer + section->size)
8838 {
8839 struct dwarf2_per_cu_data *this_cu;
8840
8841 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8842
8843 comp_unit_head cu_header;
8844 read_and_check_comp_unit_head (&cu_header, section, abbrev_section,
8845 info_ptr, rcuh_kind::COMPILE);
8846
8847 /* Save the compilation unit for later lookup. */
8848 if (cu_header.unit_type != DW_UT_type)
8849 {
8850 this_cu = XOBNEW (&objfile->objfile_obstack,
8851 struct dwarf2_per_cu_data);
8852 memset (this_cu, 0, sizeof (*this_cu));
8853 }
8854 else
8855 {
8856 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8857 struct signatured_type);
8858 memset (sig_type, 0, sizeof (*sig_type));
8859 sig_type->signature = cu_header.signature;
8860 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8861 this_cu = &sig_type->per_cu;
8862 }
8863 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8864 this_cu->sect_off = sect_off;
8865 this_cu->length = cu_header.length + cu_header.initial_length_size;
8866 this_cu->is_dwz = is_dwz;
8867 this_cu->objfile = objfile;
8868 this_cu->section = section;
8869
8870 if (*n_comp_units == *n_allocated)
8871 {
8872 *n_allocated *= 2;
8873 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
8874 *all_comp_units, *n_allocated);
8875 }
8876 (*all_comp_units)[*n_comp_units] = this_cu;
8877 ++*n_comp_units;
8878
8879 info_ptr = info_ptr + this_cu->length;
8880 }
8881 }
8882
8883 /* Create a list of all compilation units in OBJFILE.
8884 This is only done for -readnow and building partial symtabs. */
8885
8886 static void
8887 create_all_comp_units (struct objfile *objfile)
8888 {
8889 int n_allocated;
8890 int n_comp_units;
8891 struct dwarf2_per_cu_data **all_comp_units;
8892 struct dwz_file *dwz;
8893
8894 n_comp_units = 0;
8895 n_allocated = 10;
8896 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
8897
8898 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info,
8899 &dwarf2_per_objfile->abbrev, 0,
8900 &n_allocated, &n_comp_units, &all_comp_units);
8901
8902 dwz = dwarf2_get_dwz_file ();
8903 if (dwz != NULL)
8904 read_comp_units_from_section (objfile, &dwz->info, &dwz->abbrev, 1,
8905 &n_allocated, &n_comp_units,
8906 &all_comp_units);
8907
8908 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
8909 struct dwarf2_per_cu_data *,
8910 n_comp_units);
8911 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
8912 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
8913 xfree (all_comp_units);
8914 dwarf2_per_objfile->n_comp_units = n_comp_units;
8915 }
8916
8917 /* Process all loaded DIEs for compilation unit CU, starting at
8918 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8919 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8920 DW_AT_ranges). See the comments of add_partial_subprogram on how
8921 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8922
8923 static void
8924 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8925 CORE_ADDR *highpc, int set_addrmap,
8926 struct dwarf2_cu *cu)
8927 {
8928 struct partial_die_info *pdi;
8929
8930 /* Now, march along the PDI's, descending into ones which have
8931 interesting children but skipping the children of the other ones,
8932 until we reach the end of the compilation unit. */
8933
8934 pdi = first_die;
8935
8936 while (pdi != NULL)
8937 {
8938 fixup_partial_die (pdi, cu);
8939
8940 /* Anonymous namespaces or modules have no name but have interesting
8941 children, so we need to look at them. Ditto for anonymous
8942 enums. */
8943
8944 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8945 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8946 || pdi->tag == DW_TAG_imported_unit)
8947 {
8948 switch (pdi->tag)
8949 {
8950 case DW_TAG_subprogram:
8951 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8952 break;
8953 case DW_TAG_constant:
8954 case DW_TAG_variable:
8955 case DW_TAG_typedef:
8956 case DW_TAG_union_type:
8957 if (!pdi->is_declaration)
8958 {
8959 add_partial_symbol (pdi, cu);
8960 }
8961 break;
8962 case DW_TAG_class_type:
8963 case DW_TAG_interface_type:
8964 case DW_TAG_structure_type:
8965 if (!pdi->is_declaration)
8966 {
8967 add_partial_symbol (pdi, cu);
8968 }
8969 if (cu->language == language_rust && pdi->has_children)
8970 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8971 set_addrmap, cu);
8972 break;
8973 case DW_TAG_enumeration_type:
8974 if (!pdi->is_declaration)
8975 add_partial_enumeration (pdi, cu);
8976 break;
8977 case DW_TAG_base_type:
8978 case DW_TAG_subrange_type:
8979 /* File scope base type definitions are added to the partial
8980 symbol table. */
8981 add_partial_symbol (pdi, cu);
8982 break;
8983 case DW_TAG_namespace:
8984 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8985 break;
8986 case DW_TAG_module:
8987 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8988 break;
8989 case DW_TAG_imported_unit:
8990 {
8991 struct dwarf2_per_cu_data *per_cu;
8992
8993 /* For now we don't handle imported units in type units. */
8994 if (cu->per_cu->is_debug_types)
8995 {
8996 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8997 " supported in type units [in module %s]"),
8998 objfile_name (cu->objfile));
8999 }
9000
9001 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
9002 pdi->is_dwz,
9003 cu->objfile);
9004
9005 /* Go read the partial unit, if needed. */
9006 if (per_cu->v.psymtab == NULL)
9007 process_psymtab_comp_unit (per_cu, 1, cu->language);
9008
9009 VEC_safe_push (dwarf2_per_cu_ptr,
9010 cu->per_cu->imported_symtabs, per_cu);
9011 }
9012 break;
9013 case DW_TAG_imported_declaration:
9014 add_partial_symbol (pdi, cu);
9015 break;
9016 default:
9017 break;
9018 }
9019 }
9020
9021 /* If the die has a sibling, skip to the sibling. */
9022
9023 pdi = pdi->die_sibling;
9024 }
9025 }
9026
9027 /* Functions used to compute the fully scoped name of a partial DIE.
9028
9029 Normally, this is simple. For C++, the parent DIE's fully scoped
9030 name is concatenated with "::" and the partial DIE's name.
9031 Enumerators are an exception; they use the scope of their parent
9032 enumeration type, i.e. the name of the enumeration type is not
9033 prepended to the enumerator.
9034
9035 There are two complexities. One is DW_AT_specification; in this
9036 case "parent" means the parent of the target of the specification,
9037 instead of the direct parent of the DIE. The other is compilers
9038 which do not emit DW_TAG_namespace; in this case we try to guess
9039 the fully qualified name of structure types from their members'
9040 linkage names. This must be done using the DIE's children rather
9041 than the children of any DW_AT_specification target. We only need
9042 to do this for structures at the top level, i.e. if the target of
9043 any DW_AT_specification (if any; otherwise the DIE itself) does not
9044 have a parent. */
9045
9046 /* Compute the scope prefix associated with PDI's parent, in
9047 compilation unit CU. The result will be allocated on CU's
9048 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9049 field. NULL is returned if no prefix is necessary. */
9050 static const char *
9051 partial_die_parent_scope (struct partial_die_info *pdi,
9052 struct dwarf2_cu *cu)
9053 {
9054 const char *grandparent_scope;
9055 struct partial_die_info *parent, *real_pdi;
9056
9057 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9058 then this means the parent of the specification DIE. */
9059
9060 real_pdi = pdi;
9061 while (real_pdi->has_specification)
9062 real_pdi = find_partial_die (real_pdi->spec_offset,
9063 real_pdi->spec_is_dwz, cu);
9064
9065 parent = real_pdi->die_parent;
9066 if (parent == NULL)
9067 return NULL;
9068
9069 if (parent->scope_set)
9070 return parent->scope;
9071
9072 fixup_partial_die (parent, cu);
9073
9074 grandparent_scope = partial_die_parent_scope (parent, cu);
9075
9076 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9077 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9078 Work around this problem here. */
9079 if (cu->language == language_cplus
9080 && parent->tag == DW_TAG_namespace
9081 && strcmp (parent->name, "::") == 0
9082 && grandparent_scope == NULL)
9083 {
9084 parent->scope = NULL;
9085 parent->scope_set = 1;
9086 return NULL;
9087 }
9088
9089 if (pdi->tag == DW_TAG_enumerator)
9090 /* Enumerators should not get the name of the enumeration as a prefix. */
9091 parent->scope = grandparent_scope;
9092 else if (parent->tag == DW_TAG_namespace
9093 || parent->tag == DW_TAG_module
9094 || parent->tag == DW_TAG_structure_type
9095 || parent->tag == DW_TAG_class_type
9096 || parent->tag == DW_TAG_interface_type
9097 || parent->tag == DW_TAG_union_type
9098 || parent->tag == DW_TAG_enumeration_type)
9099 {
9100 if (grandparent_scope == NULL)
9101 parent->scope = parent->name;
9102 else
9103 parent->scope = typename_concat (&cu->comp_unit_obstack,
9104 grandparent_scope,
9105 parent->name, 0, cu);
9106 }
9107 else
9108 {
9109 /* FIXME drow/2004-04-01: What should we be doing with
9110 function-local names? For partial symbols, we should probably be
9111 ignoring them. */
9112 complaint (&symfile_complaints,
9113 _("unhandled containing DIE tag %d for DIE at %d"),
9114 parent->tag, to_underlying (pdi->sect_off));
9115 parent->scope = grandparent_scope;
9116 }
9117
9118 parent->scope_set = 1;
9119 return parent->scope;
9120 }
9121
9122 /* Return the fully scoped name associated with PDI, from compilation unit
9123 CU. The result will be allocated with malloc. */
9124
9125 static char *
9126 partial_die_full_name (struct partial_die_info *pdi,
9127 struct dwarf2_cu *cu)
9128 {
9129 const char *parent_scope;
9130
9131 /* If this is a template instantiation, we can not work out the
9132 template arguments from partial DIEs. So, unfortunately, we have
9133 to go through the full DIEs. At least any work we do building
9134 types here will be reused if full symbols are loaded later. */
9135 if (pdi->has_template_arguments)
9136 {
9137 fixup_partial_die (pdi, cu);
9138
9139 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9140 {
9141 struct die_info *die;
9142 struct attribute attr;
9143 struct dwarf2_cu *ref_cu = cu;
9144
9145 /* DW_FORM_ref_addr is using section offset. */
9146 attr.name = (enum dwarf_attribute) 0;
9147 attr.form = DW_FORM_ref_addr;
9148 attr.u.unsnd = to_underlying (pdi->sect_off);
9149 die = follow_die_ref (NULL, &attr, &ref_cu);
9150
9151 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9152 }
9153 }
9154
9155 parent_scope = partial_die_parent_scope (pdi, cu);
9156 if (parent_scope == NULL)
9157 return NULL;
9158 else
9159 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
9160 }
9161
9162 static void
9163 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
9164 {
9165 struct objfile *objfile = cu->objfile;
9166 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9167 CORE_ADDR addr = 0;
9168 const char *actual_name = NULL;
9169 CORE_ADDR baseaddr;
9170 char *built_actual_name;
9171
9172 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9173
9174 built_actual_name = partial_die_full_name (pdi, cu);
9175 if (built_actual_name != NULL)
9176 actual_name = built_actual_name;
9177
9178 if (actual_name == NULL)
9179 actual_name = pdi->name;
9180
9181 switch (pdi->tag)
9182 {
9183 case DW_TAG_subprogram:
9184 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
9185 if (pdi->is_external || cu->language == language_ada)
9186 {
9187 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9188 of the global scope. But in Ada, we want to be able to access
9189 nested procedures globally. So all Ada subprograms are stored
9190 in the global scope. */
9191 add_psymbol_to_list (actual_name, strlen (actual_name),
9192 built_actual_name != NULL,
9193 VAR_DOMAIN, LOC_BLOCK,
9194 &objfile->global_psymbols,
9195 addr, cu->language, objfile);
9196 }
9197 else
9198 {
9199 add_psymbol_to_list (actual_name, strlen (actual_name),
9200 built_actual_name != NULL,
9201 VAR_DOMAIN, LOC_BLOCK,
9202 &objfile->static_psymbols,
9203 addr, cu->language, objfile);
9204 }
9205
9206 if (pdi->main_subprogram && actual_name != NULL)
9207 set_objfile_main_name (objfile, actual_name, cu->language);
9208 break;
9209 case DW_TAG_constant:
9210 {
9211 std::vector<partial_symbol *> *list;
9212
9213 if (pdi->is_external)
9214 list = &objfile->global_psymbols;
9215 else
9216 list = &objfile->static_psymbols;
9217 add_psymbol_to_list (actual_name, strlen (actual_name),
9218 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9219 list, 0, cu->language, objfile);
9220 }
9221 break;
9222 case DW_TAG_variable:
9223 if (pdi->d.locdesc)
9224 addr = decode_locdesc (pdi->d.locdesc, cu);
9225
9226 if (pdi->d.locdesc
9227 && addr == 0
9228 && !dwarf2_per_objfile->has_section_at_zero)
9229 {
9230 /* A global or static variable may also have been stripped
9231 out by the linker if unused, in which case its address
9232 will be nullified; do not add such variables into partial
9233 symbol table then. */
9234 }
9235 else if (pdi->is_external)
9236 {
9237 /* Global Variable.
9238 Don't enter into the minimal symbol tables as there is
9239 a minimal symbol table entry from the ELF symbols already.
9240 Enter into partial symbol table if it has a location
9241 descriptor or a type.
9242 If the location descriptor is missing, new_symbol will create
9243 a LOC_UNRESOLVED symbol, the address of the variable will then
9244 be determined from the minimal symbol table whenever the variable
9245 is referenced.
9246 The address for the partial symbol table entry is not
9247 used by GDB, but it comes in handy for debugging partial symbol
9248 table building. */
9249
9250 if (pdi->d.locdesc || pdi->has_type)
9251 add_psymbol_to_list (actual_name, strlen (actual_name),
9252 built_actual_name != NULL,
9253 VAR_DOMAIN, LOC_STATIC,
9254 &objfile->global_psymbols,
9255 addr + baseaddr,
9256 cu->language, objfile);
9257 }
9258 else
9259 {
9260 int has_loc = pdi->d.locdesc != NULL;
9261
9262 /* Static Variable. Skip symbols whose value we cannot know (those
9263 without location descriptors or constant values). */
9264 if (!has_loc && !pdi->has_const_value)
9265 {
9266 xfree (built_actual_name);
9267 return;
9268 }
9269
9270 add_psymbol_to_list (actual_name, strlen (actual_name),
9271 built_actual_name != NULL,
9272 VAR_DOMAIN, LOC_STATIC,
9273 &objfile->static_psymbols,
9274 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
9275 cu->language, objfile);
9276 }
9277 break;
9278 case DW_TAG_typedef:
9279 case DW_TAG_base_type:
9280 case DW_TAG_subrange_type:
9281 add_psymbol_to_list (actual_name, strlen (actual_name),
9282 built_actual_name != NULL,
9283 VAR_DOMAIN, LOC_TYPEDEF,
9284 &objfile->static_psymbols,
9285 0, cu->language, objfile);
9286 break;
9287 case DW_TAG_imported_declaration:
9288 case DW_TAG_namespace:
9289 add_psymbol_to_list (actual_name, strlen (actual_name),
9290 built_actual_name != NULL,
9291 VAR_DOMAIN, LOC_TYPEDEF,
9292 &objfile->global_psymbols,
9293 0, cu->language, objfile);
9294 break;
9295 case DW_TAG_module:
9296 add_psymbol_to_list (actual_name, strlen (actual_name),
9297 built_actual_name != NULL,
9298 MODULE_DOMAIN, LOC_TYPEDEF,
9299 &objfile->global_psymbols,
9300 0, cu->language, objfile);
9301 break;
9302 case DW_TAG_class_type:
9303 case DW_TAG_interface_type:
9304 case DW_TAG_structure_type:
9305 case DW_TAG_union_type:
9306 case DW_TAG_enumeration_type:
9307 /* Skip external references. The DWARF standard says in the section
9308 about "Structure, Union, and Class Type Entries": "An incomplete
9309 structure, union or class type is represented by a structure,
9310 union or class entry that does not have a byte size attribute
9311 and that has a DW_AT_declaration attribute." */
9312 if (!pdi->has_byte_size && pdi->is_declaration)
9313 {
9314 xfree (built_actual_name);
9315 return;
9316 }
9317
9318 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9319 static vs. global. */
9320 add_psymbol_to_list (actual_name, strlen (actual_name),
9321 built_actual_name != NULL,
9322 STRUCT_DOMAIN, LOC_TYPEDEF,
9323 cu->language == language_cplus
9324 ? &objfile->global_psymbols
9325 : &objfile->static_psymbols,
9326 0, cu->language, objfile);
9327
9328 break;
9329 case DW_TAG_enumerator:
9330 add_psymbol_to_list (actual_name, strlen (actual_name),
9331 built_actual_name != NULL,
9332 VAR_DOMAIN, LOC_CONST,
9333 cu->language == language_cplus
9334 ? &objfile->global_psymbols
9335 : &objfile->static_psymbols,
9336 0, cu->language, objfile);
9337 break;
9338 default:
9339 break;
9340 }
9341
9342 xfree (built_actual_name);
9343 }
9344
9345 /* Read a partial die corresponding to a namespace; also, add a symbol
9346 corresponding to that namespace to the symbol table. NAMESPACE is
9347 the name of the enclosing namespace. */
9348
9349 static void
9350 add_partial_namespace (struct partial_die_info *pdi,
9351 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9352 int set_addrmap, struct dwarf2_cu *cu)
9353 {
9354 /* Add a symbol for the namespace. */
9355
9356 add_partial_symbol (pdi, cu);
9357
9358 /* Now scan partial symbols in that namespace. */
9359
9360 if (pdi->has_children)
9361 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9362 }
9363
9364 /* Read a partial die corresponding to a Fortran module. */
9365
9366 static void
9367 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9368 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9369 {
9370 /* Add a symbol for the namespace. */
9371
9372 add_partial_symbol (pdi, cu);
9373
9374 /* Now scan partial symbols in that module. */
9375
9376 if (pdi->has_children)
9377 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9378 }
9379
9380 /* Read a partial die corresponding to a subprogram and create a partial
9381 symbol for that subprogram. When the CU language allows it, this
9382 routine also defines a partial symbol for each nested subprogram
9383 that this subprogram contains. If SET_ADDRMAP is true, record the
9384 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
9385 and highest PC values found in PDI.
9386
9387 PDI may also be a lexical block, in which case we simply search
9388 recursively for subprograms defined inside that lexical block.
9389 Again, this is only performed when the CU language allows this
9390 type of definitions. */
9391
9392 static void
9393 add_partial_subprogram (struct partial_die_info *pdi,
9394 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9395 int set_addrmap, struct dwarf2_cu *cu)
9396 {
9397 if (pdi->tag == DW_TAG_subprogram)
9398 {
9399 if (pdi->has_pc_info)
9400 {
9401 if (pdi->lowpc < *lowpc)
9402 *lowpc = pdi->lowpc;
9403 if (pdi->highpc > *highpc)
9404 *highpc = pdi->highpc;
9405 if (set_addrmap)
9406 {
9407 struct objfile *objfile = cu->objfile;
9408 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9409 CORE_ADDR baseaddr;
9410 CORE_ADDR highpc;
9411 CORE_ADDR lowpc;
9412
9413 baseaddr = ANOFFSET (objfile->section_offsets,
9414 SECT_OFF_TEXT (objfile));
9415 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9416 pdi->lowpc + baseaddr);
9417 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9418 pdi->highpc + baseaddr);
9419 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9420 cu->per_cu->v.psymtab);
9421 }
9422 }
9423
9424 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9425 {
9426 if (!pdi->is_declaration)
9427 /* Ignore subprogram DIEs that do not have a name, they are
9428 illegal. Do not emit a complaint at this point, we will
9429 do so when we convert this psymtab into a symtab. */
9430 if (pdi->name)
9431 add_partial_symbol (pdi, cu);
9432 }
9433 }
9434
9435 if (! pdi->has_children)
9436 return;
9437
9438 if (cu->language == language_ada)
9439 {
9440 pdi = pdi->die_child;
9441 while (pdi != NULL)
9442 {
9443 fixup_partial_die (pdi, cu);
9444 if (pdi->tag == DW_TAG_subprogram
9445 || pdi->tag == DW_TAG_lexical_block)
9446 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9447 pdi = pdi->die_sibling;
9448 }
9449 }
9450 }
9451
9452 /* Read a partial die corresponding to an enumeration type. */
9453
9454 static void
9455 add_partial_enumeration (struct partial_die_info *enum_pdi,
9456 struct dwarf2_cu *cu)
9457 {
9458 struct partial_die_info *pdi;
9459
9460 if (enum_pdi->name != NULL)
9461 add_partial_symbol (enum_pdi, cu);
9462
9463 pdi = enum_pdi->die_child;
9464 while (pdi)
9465 {
9466 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9467 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9468 else
9469 add_partial_symbol (pdi, cu);
9470 pdi = pdi->die_sibling;
9471 }
9472 }
9473
9474 /* Return the initial uleb128 in the die at INFO_PTR. */
9475
9476 static unsigned int
9477 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9478 {
9479 unsigned int bytes_read;
9480
9481 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9482 }
9483
9484 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
9485 Return the corresponding abbrev, or NULL if the number is zero (indicating
9486 an empty DIE). In either case *BYTES_READ will be set to the length of
9487 the initial number. */
9488
9489 static struct abbrev_info *
9490 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
9491 struct dwarf2_cu *cu)
9492 {
9493 bfd *abfd = cu->objfile->obfd;
9494 unsigned int abbrev_number;
9495 struct abbrev_info *abbrev;
9496
9497 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9498
9499 if (abbrev_number == 0)
9500 return NULL;
9501
9502 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
9503 if (!abbrev)
9504 {
9505 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9506 " at offset 0x%x [in module %s]"),
9507 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9508 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
9509 }
9510
9511 return abbrev;
9512 }
9513
9514 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9515 Returns a pointer to the end of a series of DIEs, terminated by an empty
9516 DIE. Any children of the skipped DIEs will also be skipped. */
9517
9518 static const gdb_byte *
9519 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9520 {
9521 struct dwarf2_cu *cu = reader->cu;
9522 struct abbrev_info *abbrev;
9523 unsigned int bytes_read;
9524
9525 while (1)
9526 {
9527 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9528 if (abbrev == NULL)
9529 return info_ptr + bytes_read;
9530 else
9531 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9532 }
9533 }
9534
9535 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9536 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9537 abbrev corresponding to that skipped uleb128 should be passed in
9538 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9539 children. */
9540
9541 static const gdb_byte *
9542 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9543 struct abbrev_info *abbrev)
9544 {
9545 unsigned int bytes_read;
9546 struct attribute attr;
9547 bfd *abfd = reader->abfd;
9548 struct dwarf2_cu *cu = reader->cu;
9549 const gdb_byte *buffer = reader->buffer;
9550 const gdb_byte *buffer_end = reader->buffer_end;
9551 unsigned int form, i;
9552
9553 for (i = 0; i < abbrev->num_attrs; i++)
9554 {
9555 /* The only abbrev we care about is DW_AT_sibling. */
9556 if (abbrev->attrs[i].name == DW_AT_sibling)
9557 {
9558 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9559 if (attr.form == DW_FORM_ref_addr)
9560 complaint (&symfile_complaints,
9561 _("ignoring absolute DW_AT_sibling"));
9562 else
9563 {
9564 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9565 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9566
9567 if (sibling_ptr < info_ptr)
9568 complaint (&symfile_complaints,
9569 _("DW_AT_sibling points backwards"));
9570 else if (sibling_ptr > reader->buffer_end)
9571 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9572 else
9573 return sibling_ptr;
9574 }
9575 }
9576
9577 /* If it isn't DW_AT_sibling, skip this attribute. */
9578 form = abbrev->attrs[i].form;
9579 skip_attribute:
9580 switch (form)
9581 {
9582 case DW_FORM_ref_addr:
9583 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9584 and later it is offset sized. */
9585 if (cu->header.version == 2)
9586 info_ptr += cu->header.addr_size;
9587 else
9588 info_ptr += cu->header.offset_size;
9589 break;
9590 case DW_FORM_GNU_ref_alt:
9591 info_ptr += cu->header.offset_size;
9592 break;
9593 case DW_FORM_addr:
9594 info_ptr += cu->header.addr_size;
9595 break;
9596 case DW_FORM_data1:
9597 case DW_FORM_ref1:
9598 case DW_FORM_flag:
9599 info_ptr += 1;
9600 break;
9601 case DW_FORM_flag_present:
9602 case DW_FORM_implicit_const:
9603 break;
9604 case DW_FORM_data2:
9605 case DW_FORM_ref2:
9606 info_ptr += 2;
9607 break;
9608 case DW_FORM_data4:
9609 case DW_FORM_ref4:
9610 info_ptr += 4;
9611 break;
9612 case DW_FORM_data8:
9613 case DW_FORM_ref8:
9614 case DW_FORM_ref_sig8:
9615 info_ptr += 8;
9616 break;
9617 case DW_FORM_data16:
9618 info_ptr += 16;
9619 break;
9620 case DW_FORM_string:
9621 read_direct_string (abfd, info_ptr, &bytes_read);
9622 info_ptr += bytes_read;
9623 break;
9624 case DW_FORM_sec_offset:
9625 case DW_FORM_strp:
9626 case DW_FORM_GNU_strp_alt:
9627 info_ptr += cu->header.offset_size;
9628 break;
9629 case DW_FORM_exprloc:
9630 case DW_FORM_block:
9631 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9632 info_ptr += bytes_read;
9633 break;
9634 case DW_FORM_block1:
9635 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9636 break;
9637 case DW_FORM_block2:
9638 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9639 break;
9640 case DW_FORM_block4:
9641 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9642 break;
9643 case DW_FORM_sdata:
9644 case DW_FORM_udata:
9645 case DW_FORM_ref_udata:
9646 case DW_FORM_GNU_addr_index:
9647 case DW_FORM_GNU_str_index:
9648 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9649 break;
9650 case DW_FORM_indirect:
9651 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9652 info_ptr += bytes_read;
9653 /* We need to continue parsing from here, so just go back to
9654 the top. */
9655 goto skip_attribute;
9656
9657 default:
9658 error (_("Dwarf Error: Cannot handle %s "
9659 "in DWARF reader [in module %s]"),
9660 dwarf_form_name (form),
9661 bfd_get_filename (abfd));
9662 }
9663 }
9664
9665 if (abbrev->has_children)
9666 return skip_children (reader, info_ptr);
9667 else
9668 return info_ptr;
9669 }
9670
9671 /* Locate ORIG_PDI's sibling.
9672 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9673
9674 static const gdb_byte *
9675 locate_pdi_sibling (const struct die_reader_specs *reader,
9676 struct partial_die_info *orig_pdi,
9677 const gdb_byte *info_ptr)
9678 {
9679 /* Do we know the sibling already? */
9680
9681 if (orig_pdi->sibling)
9682 return orig_pdi->sibling;
9683
9684 /* Are there any children to deal with? */
9685
9686 if (!orig_pdi->has_children)
9687 return info_ptr;
9688
9689 /* Skip the children the long way. */
9690
9691 return skip_children (reader, info_ptr);
9692 }
9693
9694 /* Expand this partial symbol table into a full symbol table. SELF is
9695 not NULL. */
9696
9697 static void
9698 dwarf2_read_symtab (struct partial_symtab *self,
9699 struct objfile *objfile)
9700 {
9701 if (self->readin)
9702 {
9703 warning (_("bug: psymtab for %s is already read in."),
9704 self->filename);
9705 }
9706 else
9707 {
9708 if (info_verbose)
9709 {
9710 printf_filtered (_("Reading in symbols for %s..."),
9711 self->filename);
9712 gdb_flush (gdb_stdout);
9713 }
9714
9715 /* Restore our global data. */
9716 dwarf2_per_objfile
9717 = (struct dwarf2_per_objfile *) objfile_data (objfile,
9718 dwarf2_objfile_data_key);
9719
9720 /* If this psymtab is constructed from a debug-only objfile, the
9721 has_section_at_zero flag will not necessarily be correct. We
9722 can get the correct value for this flag by looking at the data
9723 associated with the (presumably stripped) associated objfile. */
9724 if (objfile->separate_debug_objfile_backlink)
9725 {
9726 struct dwarf2_per_objfile *dpo_backlink
9727 = ((struct dwarf2_per_objfile *)
9728 objfile_data (objfile->separate_debug_objfile_backlink,
9729 dwarf2_objfile_data_key));
9730
9731 dwarf2_per_objfile->has_section_at_zero
9732 = dpo_backlink->has_section_at_zero;
9733 }
9734
9735 dwarf2_per_objfile->reading_partial_symbols = 0;
9736
9737 psymtab_to_symtab_1 (self);
9738
9739 /* Finish up the debug error message. */
9740 if (info_verbose)
9741 printf_filtered (_("done.\n"));
9742 }
9743
9744 process_cu_includes ();
9745 }
9746 \f
9747 /* Reading in full CUs. */
9748
9749 /* Add PER_CU to the queue. */
9750
9751 static void
9752 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9753 enum language pretend_language)
9754 {
9755 struct dwarf2_queue_item *item;
9756
9757 per_cu->queued = 1;
9758 item = XNEW (struct dwarf2_queue_item);
9759 item->per_cu = per_cu;
9760 item->pretend_language = pretend_language;
9761 item->next = NULL;
9762
9763 if (dwarf2_queue == NULL)
9764 dwarf2_queue = item;
9765 else
9766 dwarf2_queue_tail->next = item;
9767
9768 dwarf2_queue_tail = item;
9769 }
9770
9771 /* If PER_CU is not yet queued, add it to the queue.
9772 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9773 dependency.
9774 The result is non-zero if PER_CU was queued, otherwise the result is zero
9775 meaning either PER_CU is already queued or it is already loaded.
9776
9777 N.B. There is an invariant here that if a CU is queued then it is loaded.
9778 The caller is required to load PER_CU if we return non-zero. */
9779
9780 static int
9781 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9782 struct dwarf2_per_cu_data *per_cu,
9783 enum language pretend_language)
9784 {
9785 /* We may arrive here during partial symbol reading, if we need full
9786 DIEs to process an unusual case (e.g. template arguments). Do
9787 not queue PER_CU, just tell our caller to load its DIEs. */
9788 if (dwarf2_per_objfile->reading_partial_symbols)
9789 {
9790 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9791 return 1;
9792 return 0;
9793 }
9794
9795 /* Mark the dependence relation so that we don't flush PER_CU
9796 too early. */
9797 if (dependent_cu != NULL)
9798 dwarf2_add_dependence (dependent_cu, per_cu);
9799
9800 /* If it's already on the queue, we have nothing to do. */
9801 if (per_cu->queued)
9802 return 0;
9803
9804 /* If the compilation unit is already loaded, just mark it as
9805 used. */
9806 if (per_cu->cu != NULL)
9807 {
9808 per_cu->cu->last_used = 0;
9809 return 0;
9810 }
9811
9812 /* Add it to the queue. */
9813 queue_comp_unit (per_cu, pretend_language);
9814
9815 return 1;
9816 }
9817
9818 /* Process the queue. */
9819
9820 static void
9821 process_queue (void)
9822 {
9823 struct dwarf2_queue_item *item, *next_item;
9824
9825 if (dwarf_read_debug)
9826 {
9827 fprintf_unfiltered (gdb_stdlog,
9828 "Expanding one or more symtabs of objfile %s ...\n",
9829 objfile_name (dwarf2_per_objfile->objfile));
9830 }
9831
9832 /* The queue starts out with one item, but following a DIE reference
9833 may load a new CU, adding it to the end of the queue. */
9834 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9835 {
9836 if ((dwarf2_per_objfile->using_index
9837 ? !item->per_cu->v.quick->compunit_symtab
9838 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9839 /* Skip dummy CUs. */
9840 && item->per_cu->cu != NULL)
9841 {
9842 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9843 unsigned int debug_print_threshold;
9844 char buf[100];
9845
9846 if (per_cu->is_debug_types)
9847 {
9848 struct signatured_type *sig_type =
9849 (struct signatured_type *) per_cu;
9850
9851 sprintf (buf, "TU %s at offset 0x%x",
9852 hex_string (sig_type->signature),
9853 to_underlying (per_cu->sect_off));
9854 /* There can be 100s of TUs.
9855 Only print them in verbose mode. */
9856 debug_print_threshold = 2;
9857 }
9858 else
9859 {
9860 sprintf (buf, "CU at offset 0x%x",
9861 to_underlying (per_cu->sect_off));
9862 debug_print_threshold = 1;
9863 }
9864
9865 if (dwarf_read_debug >= debug_print_threshold)
9866 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9867
9868 if (per_cu->is_debug_types)
9869 process_full_type_unit (per_cu, item->pretend_language);
9870 else
9871 process_full_comp_unit (per_cu, item->pretend_language);
9872
9873 if (dwarf_read_debug >= debug_print_threshold)
9874 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9875 }
9876
9877 item->per_cu->queued = 0;
9878 next_item = item->next;
9879 xfree (item);
9880 }
9881
9882 dwarf2_queue_tail = NULL;
9883
9884 if (dwarf_read_debug)
9885 {
9886 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9887 objfile_name (dwarf2_per_objfile->objfile));
9888 }
9889 }
9890
9891 /* Free all allocated queue entries. This function only releases anything if
9892 an error was thrown; if the queue was processed then it would have been
9893 freed as we went along. */
9894
9895 static void
9896 dwarf2_release_queue (void *dummy)
9897 {
9898 struct dwarf2_queue_item *item, *last;
9899
9900 item = dwarf2_queue;
9901 while (item)
9902 {
9903 /* Anything still marked queued is likely to be in an
9904 inconsistent state, so discard it. */
9905 if (item->per_cu->queued)
9906 {
9907 if (item->per_cu->cu != NULL)
9908 free_one_cached_comp_unit (item->per_cu);
9909 item->per_cu->queued = 0;
9910 }
9911
9912 last = item;
9913 item = item->next;
9914 xfree (last);
9915 }
9916
9917 dwarf2_queue = dwarf2_queue_tail = NULL;
9918 }
9919
9920 /* Read in full symbols for PST, and anything it depends on. */
9921
9922 static void
9923 psymtab_to_symtab_1 (struct partial_symtab *pst)
9924 {
9925 struct dwarf2_per_cu_data *per_cu;
9926 int i;
9927
9928 if (pst->readin)
9929 return;
9930
9931 for (i = 0; i < pst->number_of_dependencies; i++)
9932 if (!pst->dependencies[i]->readin
9933 && pst->dependencies[i]->user == NULL)
9934 {
9935 /* Inform about additional files that need to be read in. */
9936 if (info_verbose)
9937 {
9938 /* FIXME: i18n: Need to make this a single string. */
9939 fputs_filtered (" ", gdb_stdout);
9940 wrap_here ("");
9941 fputs_filtered ("and ", gdb_stdout);
9942 wrap_here ("");
9943 printf_filtered ("%s...", pst->dependencies[i]->filename);
9944 wrap_here (""); /* Flush output. */
9945 gdb_flush (gdb_stdout);
9946 }
9947 psymtab_to_symtab_1 (pst->dependencies[i]);
9948 }
9949
9950 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9951
9952 if (per_cu == NULL)
9953 {
9954 /* It's an include file, no symbols to read for it.
9955 Everything is in the parent symtab. */
9956 pst->readin = 1;
9957 return;
9958 }
9959
9960 dw2_do_instantiate_symtab (per_cu);
9961 }
9962
9963 /* Trivial hash function for die_info: the hash value of a DIE
9964 is its offset in .debug_info for this objfile. */
9965
9966 static hashval_t
9967 die_hash (const void *item)
9968 {
9969 const struct die_info *die = (const struct die_info *) item;
9970
9971 return to_underlying (die->sect_off);
9972 }
9973
9974 /* Trivial comparison function for die_info structures: two DIEs
9975 are equal if they have the same offset. */
9976
9977 static int
9978 die_eq (const void *item_lhs, const void *item_rhs)
9979 {
9980 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9981 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9982
9983 return die_lhs->sect_off == die_rhs->sect_off;
9984 }
9985
9986 /* die_reader_func for load_full_comp_unit.
9987 This is identical to read_signatured_type_reader,
9988 but is kept separate for now. */
9989
9990 static void
9991 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9992 const gdb_byte *info_ptr,
9993 struct die_info *comp_unit_die,
9994 int has_children,
9995 void *data)
9996 {
9997 struct dwarf2_cu *cu = reader->cu;
9998 enum language *language_ptr = (enum language *) data;
9999
10000 gdb_assert (cu->die_hash == NULL);
10001 cu->die_hash =
10002 htab_create_alloc_ex (cu->header.length / 12,
10003 die_hash,
10004 die_eq,
10005 NULL,
10006 &cu->comp_unit_obstack,
10007 hashtab_obstack_allocate,
10008 dummy_obstack_deallocate);
10009
10010 if (has_children)
10011 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10012 &info_ptr, comp_unit_die);
10013 cu->dies = comp_unit_die;
10014 /* comp_unit_die is not stored in die_hash, no need. */
10015
10016 /* We try not to read any attributes in this function, because not
10017 all CUs needed for references have been loaded yet, and symbol
10018 table processing isn't initialized. But we have to set the CU language,
10019 or we won't be able to build types correctly.
10020 Similarly, if we do not read the producer, we can not apply
10021 producer-specific interpretation. */
10022 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
10023 }
10024
10025 /* Load the DIEs associated with PER_CU into memory. */
10026
10027 static void
10028 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10029 enum language pretend_language)
10030 {
10031 gdb_assert (! this_cu->is_debug_types);
10032
10033 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10034 load_full_comp_unit_reader, &pretend_language);
10035 }
10036
10037 /* Add a DIE to the delayed physname list. */
10038
10039 static void
10040 add_to_method_list (struct type *type, int fnfield_index, int index,
10041 const char *name, struct die_info *die,
10042 struct dwarf2_cu *cu)
10043 {
10044 struct delayed_method_info mi;
10045 mi.type = type;
10046 mi.fnfield_index = fnfield_index;
10047 mi.index = index;
10048 mi.name = name;
10049 mi.die = die;
10050 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
10051 }
10052
10053 /* A cleanup for freeing the delayed method list. */
10054
10055 static void
10056 free_delayed_list (void *ptr)
10057 {
10058 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
10059 if (cu->method_list != NULL)
10060 {
10061 VEC_free (delayed_method_info, cu->method_list);
10062 cu->method_list = NULL;
10063 }
10064 }
10065
10066 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10067 "const" / "volatile". If so, decrements LEN by the length of the
10068 modifier and return true. Otherwise return false. */
10069
10070 template<size_t N>
10071 static bool
10072 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10073 {
10074 size_t mod_len = sizeof (mod) - 1;
10075 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10076 {
10077 len -= mod_len;
10078 return true;
10079 }
10080 return false;
10081 }
10082
10083 /* Compute the physnames of any methods on the CU's method list.
10084
10085 The computation of method physnames is delayed in order to avoid the
10086 (bad) condition that one of the method's formal parameters is of an as yet
10087 incomplete type. */
10088
10089 static void
10090 compute_delayed_physnames (struct dwarf2_cu *cu)
10091 {
10092 int i;
10093 struct delayed_method_info *mi;
10094
10095 /* Only C++ delays computing physnames. */
10096 if (VEC_empty (delayed_method_info, cu->method_list))
10097 return;
10098 gdb_assert (cu->language == language_cplus);
10099
10100 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
10101 {
10102 const char *physname;
10103 struct fn_fieldlist *fn_flp
10104 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
10105 physname = dwarf2_physname (mi->name, mi->die, cu);
10106 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
10107 = physname ? physname : "";
10108
10109 /* Since there's no tag to indicate whether a method is a
10110 const/volatile overload, extract that information out of the
10111 demangled name. */
10112 if (physname != NULL)
10113 {
10114 size_t len = strlen (physname);
10115
10116 while (1)
10117 {
10118 if (physname[len] == ')') /* shortcut */
10119 break;
10120 else if (check_modifier (physname, len, " const"))
10121 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi->index) = 1;
10122 else if (check_modifier (physname, len, " volatile"))
10123 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi->index) = 1;
10124 else
10125 break;
10126 }
10127 }
10128 }
10129 }
10130
10131 /* Go objects should be embedded in a DW_TAG_module DIE,
10132 and it's not clear if/how imported objects will appear.
10133 To keep Go support simple until that's worked out,
10134 go back through what we've read and create something usable.
10135 We could do this while processing each DIE, and feels kinda cleaner,
10136 but that way is more invasive.
10137 This is to, for example, allow the user to type "p var" or "b main"
10138 without having to specify the package name, and allow lookups
10139 of module.object to work in contexts that use the expression
10140 parser. */
10141
10142 static void
10143 fixup_go_packaging (struct dwarf2_cu *cu)
10144 {
10145 char *package_name = NULL;
10146 struct pending *list;
10147 int i;
10148
10149 for (list = global_symbols; list != NULL; list = list->next)
10150 {
10151 for (i = 0; i < list->nsyms; ++i)
10152 {
10153 struct symbol *sym = list->symbol[i];
10154
10155 if (SYMBOL_LANGUAGE (sym) == language_go
10156 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10157 {
10158 char *this_package_name = go_symbol_package_name (sym);
10159
10160 if (this_package_name == NULL)
10161 continue;
10162 if (package_name == NULL)
10163 package_name = this_package_name;
10164 else
10165 {
10166 if (strcmp (package_name, this_package_name) != 0)
10167 complaint (&symfile_complaints,
10168 _("Symtab %s has objects from two different Go packages: %s and %s"),
10169 (symbol_symtab (sym) != NULL
10170 ? symtab_to_filename_for_display
10171 (symbol_symtab (sym))
10172 : objfile_name (cu->objfile)),
10173 this_package_name, package_name);
10174 xfree (this_package_name);
10175 }
10176 }
10177 }
10178 }
10179
10180 if (package_name != NULL)
10181 {
10182 struct objfile *objfile = cu->objfile;
10183 const char *saved_package_name
10184 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10185 package_name,
10186 strlen (package_name));
10187 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10188 saved_package_name);
10189 struct symbol *sym;
10190
10191 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10192
10193 sym = allocate_symbol (objfile);
10194 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
10195 SYMBOL_SET_NAMES (sym, saved_package_name,
10196 strlen (saved_package_name), 0, objfile);
10197 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10198 e.g., "main" finds the "main" module and not C's main(). */
10199 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10200 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
10201 SYMBOL_TYPE (sym) = type;
10202
10203 add_symbol_to_list (sym, &global_symbols);
10204
10205 xfree (package_name);
10206 }
10207 }
10208
10209 /* Return the symtab for PER_CU. This works properly regardless of
10210 whether we're using the index or psymtabs. */
10211
10212 static struct compunit_symtab *
10213 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10214 {
10215 return (dwarf2_per_objfile->using_index
10216 ? per_cu->v.quick->compunit_symtab
10217 : per_cu->v.psymtab->compunit_symtab);
10218 }
10219
10220 /* A helper function for computing the list of all symbol tables
10221 included by PER_CU. */
10222
10223 static void
10224 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10225 htab_t all_children, htab_t all_type_symtabs,
10226 struct dwarf2_per_cu_data *per_cu,
10227 struct compunit_symtab *immediate_parent)
10228 {
10229 void **slot;
10230 int ix;
10231 struct compunit_symtab *cust;
10232 struct dwarf2_per_cu_data *iter;
10233
10234 slot = htab_find_slot (all_children, per_cu, INSERT);
10235 if (*slot != NULL)
10236 {
10237 /* This inclusion and its children have been processed. */
10238 return;
10239 }
10240
10241 *slot = per_cu;
10242 /* Only add a CU if it has a symbol table. */
10243 cust = get_compunit_symtab (per_cu);
10244 if (cust != NULL)
10245 {
10246 /* If this is a type unit only add its symbol table if we haven't
10247 seen it yet (type unit per_cu's can share symtabs). */
10248 if (per_cu->is_debug_types)
10249 {
10250 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10251 if (*slot == NULL)
10252 {
10253 *slot = cust;
10254 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10255 if (cust->user == NULL)
10256 cust->user = immediate_parent;
10257 }
10258 }
10259 else
10260 {
10261 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10262 if (cust->user == NULL)
10263 cust->user = immediate_parent;
10264 }
10265 }
10266
10267 for (ix = 0;
10268 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10269 ++ix)
10270 {
10271 recursively_compute_inclusions (result, all_children,
10272 all_type_symtabs, iter, cust);
10273 }
10274 }
10275
10276 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10277 PER_CU. */
10278
10279 static void
10280 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10281 {
10282 gdb_assert (! per_cu->is_debug_types);
10283
10284 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10285 {
10286 int ix, len;
10287 struct dwarf2_per_cu_data *per_cu_iter;
10288 struct compunit_symtab *compunit_symtab_iter;
10289 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10290 htab_t all_children, all_type_symtabs;
10291 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10292
10293 /* If we don't have a symtab, we can just skip this case. */
10294 if (cust == NULL)
10295 return;
10296
10297 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10298 NULL, xcalloc, xfree);
10299 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10300 NULL, xcalloc, xfree);
10301
10302 for (ix = 0;
10303 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10304 ix, per_cu_iter);
10305 ++ix)
10306 {
10307 recursively_compute_inclusions (&result_symtabs, all_children,
10308 all_type_symtabs, per_cu_iter,
10309 cust);
10310 }
10311
10312 /* Now we have a transitive closure of all the included symtabs. */
10313 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10314 cust->includes
10315 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
10316 struct compunit_symtab *, len + 1);
10317 for (ix = 0;
10318 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10319 compunit_symtab_iter);
10320 ++ix)
10321 cust->includes[ix] = compunit_symtab_iter;
10322 cust->includes[len] = NULL;
10323
10324 VEC_free (compunit_symtab_ptr, result_symtabs);
10325 htab_delete (all_children);
10326 htab_delete (all_type_symtabs);
10327 }
10328 }
10329
10330 /* Compute the 'includes' field for the symtabs of all the CUs we just
10331 read. */
10332
10333 static void
10334 process_cu_includes (void)
10335 {
10336 int ix;
10337 struct dwarf2_per_cu_data *iter;
10338
10339 for (ix = 0;
10340 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10341 ix, iter);
10342 ++ix)
10343 {
10344 if (! iter->is_debug_types)
10345 compute_compunit_symtab_includes (iter);
10346 }
10347
10348 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10349 }
10350
10351 /* Generate full symbol information for PER_CU, whose DIEs have
10352 already been loaded into memory. */
10353
10354 static void
10355 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10356 enum language pretend_language)
10357 {
10358 struct dwarf2_cu *cu = per_cu->cu;
10359 struct objfile *objfile = per_cu->objfile;
10360 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10361 CORE_ADDR lowpc, highpc;
10362 struct compunit_symtab *cust;
10363 struct cleanup *delayed_list_cleanup;
10364 CORE_ADDR baseaddr;
10365 struct block *static_block;
10366 CORE_ADDR addr;
10367
10368 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10369
10370 buildsym_init ();
10371 scoped_free_pendings free_pending;
10372 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10373
10374 cu->list_in_scope = &file_symbols;
10375
10376 cu->language = pretend_language;
10377 cu->language_defn = language_def (cu->language);
10378
10379 /* Do line number decoding in read_file_scope () */
10380 process_die (cu->dies, cu);
10381
10382 /* For now fudge the Go package. */
10383 if (cu->language == language_go)
10384 fixup_go_packaging (cu);
10385
10386 /* Now that we have processed all the DIEs in the CU, all the types
10387 should be complete, and it should now be safe to compute all of the
10388 physnames. */
10389 compute_delayed_physnames (cu);
10390 do_cleanups (delayed_list_cleanup);
10391
10392 /* Some compilers don't define a DW_AT_high_pc attribute for the
10393 compilation unit. If the DW_AT_high_pc is missing, synthesize
10394 it, by scanning the DIE's below the compilation unit. */
10395 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10396
10397 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10398 static_block = end_symtab_get_static_block (addr, 0, 1);
10399
10400 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10401 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10402 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10403 addrmap to help ensure it has an accurate map of pc values belonging to
10404 this comp unit. */
10405 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10406
10407 cust = end_symtab_from_static_block (static_block,
10408 SECT_OFF_TEXT (objfile), 0);
10409
10410 if (cust != NULL)
10411 {
10412 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10413
10414 /* Set symtab language to language from DW_AT_language. If the
10415 compilation is from a C file generated by language preprocessors, do
10416 not set the language if it was already deduced by start_subfile. */
10417 if (!(cu->language == language_c
10418 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10419 COMPUNIT_FILETABS (cust)->language = cu->language;
10420
10421 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10422 produce DW_AT_location with location lists but it can be possibly
10423 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10424 there were bugs in prologue debug info, fixed later in GCC-4.5
10425 by "unwind info for epilogues" patch (which is not directly related).
10426
10427 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10428 needed, it would be wrong due to missing DW_AT_producer there.
10429
10430 Still one can confuse GDB by using non-standard GCC compilation
10431 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10432 */
10433 if (cu->has_loclist && gcc_4_minor >= 5)
10434 cust->locations_valid = 1;
10435
10436 if (gcc_4_minor >= 5)
10437 cust->epilogue_unwind_valid = 1;
10438
10439 cust->call_site_htab = cu->call_site_htab;
10440 }
10441
10442 if (dwarf2_per_objfile->using_index)
10443 per_cu->v.quick->compunit_symtab = cust;
10444 else
10445 {
10446 struct partial_symtab *pst = per_cu->v.psymtab;
10447 pst->compunit_symtab = cust;
10448 pst->readin = 1;
10449 }
10450
10451 /* Push it for inclusion processing later. */
10452 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10453 }
10454
10455 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10456 already been loaded into memory. */
10457
10458 static void
10459 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10460 enum language pretend_language)
10461 {
10462 struct dwarf2_cu *cu = per_cu->cu;
10463 struct objfile *objfile = per_cu->objfile;
10464 struct compunit_symtab *cust;
10465 struct cleanup *delayed_list_cleanup;
10466 struct signatured_type *sig_type;
10467
10468 gdb_assert (per_cu->is_debug_types);
10469 sig_type = (struct signatured_type *) per_cu;
10470
10471 buildsym_init ();
10472 scoped_free_pendings free_pending;
10473 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10474
10475 cu->list_in_scope = &file_symbols;
10476
10477 cu->language = pretend_language;
10478 cu->language_defn = language_def (cu->language);
10479
10480 /* The symbol tables are set up in read_type_unit_scope. */
10481 process_die (cu->dies, cu);
10482
10483 /* For now fudge the Go package. */
10484 if (cu->language == language_go)
10485 fixup_go_packaging (cu);
10486
10487 /* Now that we have processed all the DIEs in the CU, all the types
10488 should be complete, and it should now be safe to compute all of the
10489 physnames. */
10490 compute_delayed_physnames (cu);
10491 do_cleanups (delayed_list_cleanup);
10492
10493 /* TUs share symbol tables.
10494 If this is the first TU to use this symtab, complete the construction
10495 of it with end_expandable_symtab. Otherwise, complete the addition of
10496 this TU's symbols to the existing symtab. */
10497 if (sig_type->type_unit_group->compunit_symtab == NULL)
10498 {
10499 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10500 sig_type->type_unit_group->compunit_symtab = cust;
10501
10502 if (cust != NULL)
10503 {
10504 /* Set symtab language to language from DW_AT_language. If the
10505 compilation is from a C file generated by language preprocessors,
10506 do not set the language if it was already deduced by
10507 start_subfile. */
10508 if (!(cu->language == language_c
10509 && COMPUNIT_FILETABS (cust)->language != language_c))
10510 COMPUNIT_FILETABS (cust)->language = cu->language;
10511 }
10512 }
10513 else
10514 {
10515 augment_type_symtab ();
10516 cust = sig_type->type_unit_group->compunit_symtab;
10517 }
10518
10519 if (dwarf2_per_objfile->using_index)
10520 per_cu->v.quick->compunit_symtab = cust;
10521 else
10522 {
10523 struct partial_symtab *pst = per_cu->v.psymtab;
10524 pst->compunit_symtab = cust;
10525 pst->readin = 1;
10526 }
10527 }
10528
10529 /* Process an imported unit DIE. */
10530
10531 static void
10532 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10533 {
10534 struct attribute *attr;
10535
10536 /* For now we don't handle imported units in type units. */
10537 if (cu->per_cu->is_debug_types)
10538 {
10539 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10540 " supported in type units [in module %s]"),
10541 objfile_name (cu->objfile));
10542 }
10543
10544 attr = dwarf2_attr (die, DW_AT_import, cu);
10545 if (attr != NULL)
10546 {
10547 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10548 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10549 dwarf2_per_cu_data *per_cu
10550 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
10551
10552 /* If necessary, add it to the queue and load its DIEs. */
10553 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10554 load_full_comp_unit (per_cu, cu->language);
10555
10556 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10557 per_cu);
10558 }
10559 }
10560
10561 /* RAII object that represents a process_die scope: i.e.,
10562 starts/finishes processing a DIE. */
10563 class process_die_scope
10564 {
10565 public:
10566 process_die_scope (die_info *die, dwarf2_cu *cu)
10567 : m_die (die), m_cu (cu)
10568 {
10569 /* We should only be processing DIEs not already in process. */
10570 gdb_assert (!m_die->in_process);
10571 m_die->in_process = true;
10572 }
10573
10574 ~process_die_scope ()
10575 {
10576 m_die->in_process = false;
10577
10578 /* If we're done processing the DIE for the CU that owns the line
10579 header, we don't need the line header anymore. */
10580 if (m_cu->line_header_die_owner == m_die)
10581 {
10582 delete m_cu->line_header;
10583 m_cu->line_header = NULL;
10584 m_cu->line_header_die_owner = NULL;
10585 }
10586 }
10587
10588 private:
10589 die_info *m_die;
10590 dwarf2_cu *m_cu;
10591 };
10592
10593 /* Process a die and its children. */
10594
10595 static void
10596 process_die (struct die_info *die, struct dwarf2_cu *cu)
10597 {
10598 process_die_scope scope (die, cu);
10599
10600 switch (die->tag)
10601 {
10602 case DW_TAG_padding:
10603 break;
10604 case DW_TAG_compile_unit:
10605 case DW_TAG_partial_unit:
10606 read_file_scope (die, cu);
10607 break;
10608 case DW_TAG_type_unit:
10609 read_type_unit_scope (die, cu);
10610 break;
10611 case DW_TAG_subprogram:
10612 case DW_TAG_inlined_subroutine:
10613 read_func_scope (die, cu);
10614 break;
10615 case DW_TAG_lexical_block:
10616 case DW_TAG_try_block:
10617 case DW_TAG_catch_block:
10618 read_lexical_block_scope (die, cu);
10619 break;
10620 case DW_TAG_call_site:
10621 case DW_TAG_GNU_call_site:
10622 read_call_site_scope (die, cu);
10623 break;
10624 case DW_TAG_class_type:
10625 case DW_TAG_interface_type:
10626 case DW_TAG_structure_type:
10627 case DW_TAG_union_type:
10628 process_structure_scope (die, cu);
10629 break;
10630 case DW_TAG_enumeration_type:
10631 process_enumeration_scope (die, cu);
10632 break;
10633
10634 /* These dies have a type, but processing them does not create
10635 a symbol or recurse to process the children. Therefore we can
10636 read them on-demand through read_type_die. */
10637 case DW_TAG_subroutine_type:
10638 case DW_TAG_set_type:
10639 case DW_TAG_array_type:
10640 case DW_TAG_pointer_type:
10641 case DW_TAG_ptr_to_member_type:
10642 case DW_TAG_reference_type:
10643 case DW_TAG_rvalue_reference_type:
10644 case DW_TAG_string_type:
10645 break;
10646
10647 case DW_TAG_base_type:
10648 case DW_TAG_subrange_type:
10649 case DW_TAG_typedef:
10650 /* Add a typedef symbol for the type definition, if it has a
10651 DW_AT_name. */
10652 new_symbol (die, read_type_die (die, cu), cu);
10653 break;
10654 case DW_TAG_common_block:
10655 read_common_block (die, cu);
10656 break;
10657 case DW_TAG_common_inclusion:
10658 break;
10659 case DW_TAG_namespace:
10660 cu->processing_has_namespace_info = 1;
10661 read_namespace (die, cu);
10662 break;
10663 case DW_TAG_module:
10664 cu->processing_has_namespace_info = 1;
10665 read_module (die, cu);
10666 break;
10667 case DW_TAG_imported_declaration:
10668 cu->processing_has_namespace_info = 1;
10669 if (read_namespace_alias (die, cu))
10670 break;
10671 /* The declaration is not a global namespace alias: fall through. */
10672 case DW_TAG_imported_module:
10673 cu->processing_has_namespace_info = 1;
10674 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10675 || cu->language != language_fortran))
10676 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10677 dwarf_tag_name (die->tag));
10678 read_import_statement (die, cu);
10679 break;
10680
10681 case DW_TAG_imported_unit:
10682 process_imported_unit_die (die, cu);
10683 break;
10684
10685 case DW_TAG_variable:
10686 read_variable (die, cu);
10687 break;
10688
10689 default:
10690 new_symbol (die, NULL, cu);
10691 break;
10692 }
10693 }
10694 \f
10695 /* DWARF name computation. */
10696
10697 /* A helper function for dwarf2_compute_name which determines whether DIE
10698 needs to have the name of the scope prepended to the name listed in the
10699 die. */
10700
10701 static int
10702 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10703 {
10704 struct attribute *attr;
10705
10706 switch (die->tag)
10707 {
10708 case DW_TAG_namespace:
10709 case DW_TAG_typedef:
10710 case DW_TAG_class_type:
10711 case DW_TAG_interface_type:
10712 case DW_TAG_structure_type:
10713 case DW_TAG_union_type:
10714 case DW_TAG_enumeration_type:
10715 case DW_TAG_enumerator:
10716 case DW_TAG_subprogram:
10717 case DW_TAG_inlined_subroutine:
10718 case DW_TAG_member:
10719 case DW_TAG_imported_declaration:
10720 return 1;
10721
10722 case DW_TAG_variable:
10723 case DW_TAG_constant:
10724 /* We only need to prefix "globally" visible variables. These include
10725 any variable marked with DW_AT_external or any variable that
10726 lives in a namespace. [Variables in anonymous namespaces
10727 require prefixing, but they are not DW_AT_external.] */
10728
10729 if (dwarf2_attr (die, DW_AT_specification, cu))
10730 {
10731 struct dwarf2_cu *spec_cu = cu;
10732
10733 return die_needs_namespace (die_specification (die, &spec_cu),
10734 spec_cu);
10735 }
10736
10737 attr = dwarf2_attr (die, DW_AT_external, cu);
10738 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10739 && die->parent->tag != DW_TAG_module)
10740 return 0;
10741 /* A variable in a lexical block of some kind does not need a
10742 namespace, even though in C++ such variables may be external
10743 and have a mangled name. */
10744 if (die->parent->tag == DW_TAG_lexical_block
10745 || die->parent->tag == DW_TAG_try_block
10746 || die->parent->tag == DW_TAG_catch_block
10747 || die->parent->tag == DW_TAG_subprogram)
10748 return 0;
10749 return 1;
10750
10751 default:
10752 return 0;
10753 }
10754 }
10755
10756 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10757 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10758 defined for the given DIE. */
10759
10760 static struct attribute *
10761 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10762 {
10763 struct attribute *attr;
10764
10765 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10766 if (attr == NULL)
10767 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10768
10769 return attr;
10770 }
10771
10772 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10773 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10774 defined for the given DIE. */
10775
10776 static const char *
10777 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10778 {
10779 const char *linkage_name;
10780
10781 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10782 if (linkage_name == NULL)
10783 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10784
10785 return linkage_name;
10786 }
10787
10788 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10789 compute the physname for the object, which include a method's:
10790 - formal parameters (C++),
10791 - receiver type (Go),
10792
10793 The term "physname" is a bit confusing.
10794 For C++, for example, it is the demangled name.
10795 For Go, for example, it's the mangled name.
10796
10797 For Ada, return the DIE's linkage name rather than the fully qualified
10798 name. PHYSNAME is ignored..
10799
10800 The result is allocated on the objfile_obstack and canonicalized. */
10801
10802 static const char *
10803 dwarf2_compute_name (const char *name,
10804 struct die_info *die, struct dwarf2_cu *cu,
10805 int physname)
10806 {
10807 struct objfile *objfile = cu->objfile;
10808
10809 if (name == NULL)
10810 name = dwarf2_name (die, cu);
10811
10812 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10813 but otherwise compute it by typename_concat inside GDB.
10814 FIXME: Actually this is not really true, or at least not always true.
10815 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10816 Fortran names because there is no mangling standard. So new_symbol_full
10817 will set the demangled name to the result of dwarf2_full_name, and it is
10818 the demangled name that GDB uses if it exists. */
10819 if (cu->language == language_ada
10820 || (cu->language == language_fortran && physname))
10821 {
10822 /* For Ada unit, we prefer the linkage name over the name, as
10823 the former contains the exported name, which the user expects
10824 to be able to reference. Ideally, we want the user to be able
10825 to reference this entity using either natural or linkage name,
10826 but we haven't started looking at this enhancement yet. */
10827 const char *linkage_name = dw2_linkage_name (die, cu);
10828
10829 if (linkage_name != NULL)
10830 return linkage_name;
10831 }
10832
10833 /* These are the only languages we know how to qualify names in. */
10834 if (name != NULL
10835 && (cu->language == language_cplus
10836 || cu->language == language_fortran || cu->language == language_d
10837 || cu->language == language_rust))
10838 {
10839 if (die_needs_namespace (die, cu))
10840 {
10841 const char *prefix;
10842 const char *canonical_name = NULL;
10843
10844 string_file buf;
10845
10846 prefix = determine_prefix (die, cu);
10847 if (*prefix != '\0')
10848 {
10849 char *prefixed_name = typename_concat (NULL, prefix, name,
10850 physname, cu);
10851
10852 buf.puts (prefixed_name);
10853 xfree (prefixed_name);
10854 }
10855 else
10856 buf.puts (name);
10857
10858 /* Template parameters may be specified in the DIE's DW_AT_name, or
10859 as children with DW_TAG_template_type_param or
10860 DW_TAG_value_type_param. If the latter, add them to the name
10861 here. If the name already has template parameters, then
10862 skip this step; some versions of GCC emit both, and
10863 it is more efficient to use the pre-computed name.
10864
10865 Something to keep in mind about this process: it is very
10866 unlikely, or in some cases downright impossible, to produce
10867 something that will match the mangled name of a function.
10868 If the definition of the function has the same debug info,
10869 we should be able to match up with it anyway. But fallbacks
10870 using the minimal symbol, for instance to find a method
10871 implemented in a stripped copy of libstdc++, will not work.
10872 If we do not have debug info for the definition, we will have to
10873 match them up some other way.
10874
10875 When we do name matching there is a related problem with function
10876 templates; two instantiated function templates are allowed to
10877 differ only by their return types, which we do not add here. */
10878
10879 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10880 {
10881 struct attribute *attr;
10882 struct die_info *child;
10883 int first = 1;
10884
10885 die->building_fullname = 1;
10886
10887 for (child = die->child; child != NULL; child = child->sibling)
10888 {
10889 struct type *type;
10890 LONGEST value;
10891 const gdb_byte *bytes;
10892 struct dwarf2_locexpr_baton *baton;
10893 struct value *v;
10894
10895 if (child->tag != DW_TAG_template_type_param
10896 && child->tag != DW_TAG_template_value_param)
10897 continue;
10898
10899 if (first)
10900 {
10901 buf.puts ("<");
10902 first = 0;
10903 }
10904 else
10905 buf.puts (", ");
10906
10907 attr = dwarf2_attr (child, DW_AT_type, cu);
10908 if (attr == NULL)
10909 {
10910 complaint (&symfile_complaints,
10911 _("template parameter missing DW_AT_type"));
10912 buf.puts ("UNKNOWN_TYPE");
10913 continue;
10914 }
10915 type = die_type (child, cu);
10916
10917 if (child->tag == DW_TAG_template_type_param)
10918 {
10919 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
10920 continue;
10921 }
10922
10923 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10924 if (attr == NULL)
10925 {
10926 complaint (&symfile_complaints,
10927 _("template parameter missing "
10928 "DW_AT_const_value"));
10929 buf.puts ("UNKNOWN_VALUE");
10930 continue;
10931 }
10932
10933 dwarf2_const_value_attr (attr, type, name,
10934 &cu->comp_unit_obstack, cu,
10935 &value, &bytes, &baton);
10936
10937 if (TYPE_NOSIGN (type))
10938 /* GDB prints characters as NUMBER 'CHAR'. If that's
10939 changed, this can use value_print instead. */
10940 c_printchar (value, type, &buf);
10941 else
10942 {
10943 struct value_print_options opts;
10944
10945 if (baton != NULL)
10946 v = dwarf2_evaluate_loc_desc (type, NULL,
10947 baton->data,
10948 baton->size,
10949 baton->per_cu);
10950 else if (bytes != NULL)
10951 {
10952 v = allocate_value (type);
10953 memcpy (value_contents_writeable (v), bytes,
10954 TYPE_LENGTH (type));
10955 }
10956 else
10957 v = value_from_longest (type, value);
10958
10959 /* Specify decimal so that we do not depend on
10960 the radix. */
10961 get_formatted_print_options (&opts, 'd');
10962 opts.raw = 1;
10963 value_print (v, &buf, &opts);
10964 release_value (v);
10965 value_free (v);
10966 }
10967 }
10968
10969 die->building_fullname = 0;
10970
10971 if (!first)
10972 {
10973 /* Close the argument list, with a space if necessary
10974 (nested templates). */
10975 if (!buf.empty () && buf.string ().back () == '>')
10976 buf.puts (" >");
10977 else
10978 buf.puts (">");
10979 }
10980 }
10981
10982 /* For C++ methods, append formal parameter type
10983 information, if PHYSNAME. */
10984
10985 if (physname && die->tag == DW_TAG_subprogram
10986 && cu->language == language_cplus)
10987 {
10988 struct type *type = read_type_die (die, cu);
10989
10990 c_type_print_args (type, &buf, 1, cu->language,
10991 &type_print_raw_options);
10992
10993 if (cu->language == language_cplus)
10994 {
10995 /* Assume that an artificial first parameter is
10996 "this", but do not crash if it is not. RealView
10997 marks unnamed (and thus unused) parameters as
10998 artificial; there is no way to differentiate
10999 the two cases. */
11000 if (TYPE_NFIELDS (type) > 0
11001 && TYPE_FIELD_ARTIFICIAL (type, 0)
11002 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11003 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11004 0))))
11005 buf.puts (" const");
11006 }
11007 }
11008
11009 const std::string &intermediate_name = buf.string ();
11010
11011 if (cu->language == language_cplus)
11012 canonical_name
11013 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11014 &objfile->per_bfd->storage_obstack);
11015
11016 /* If we only computed INTERMEDIATE_NAME, or if
11017 INTERMEDIATE_NAME is already canonical, then we need to
11018 copy it to the appropriate obstack. */
11019 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11020 name = ((const char *)
11021 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11022 intermediate_name.c_str (),
11023 intermediate_name.length ()));
11024 else
11025 name = canonical_name;
11026 }
11027 }
11028
11029 return name;
11030 }
11031
11032 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11033 If scope qualifiers are appropriate they will be added. The result
11034 will be allocated on the storage_obstack, or NULL if the DIE does
11035 not have a name. NAME may either be from a previous call to
11036 dwarf2_name or NULL.
11037
11038 The output string will be canonicalized (if C++). */
11039
11040 static const char *
11041 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11042 {
11043 return dwarf2_compute_name (name, die, cu, 0);
11044 }
11045
11046 /* Construct a physname for the given DIE in CU. NAME may either be
11047 from a previous call to dwarf2_name or NULL. The result will be
11048 allocated on the objfile_objstack or NULL if the DIE does not have a
11049 name.
11050
11051 The output string will be canonicalized (if C++). */
11052
11053 static const char *
11054 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11055 {
11056 struct objfile *objfile = cu->objfile;
11057 const char *retval, *mangled = NULL, *canon = NULL;
11058 int need_copy = 1;
11059
11060 /* In this case dwarf2_compute_name is just a shortcut not building anything
11061 on its own. */
11062 if (!die_needs_namespace (die, cu))
11063 return dwarf2_compute_name (name, die, cu, 1);
11064
11065 mangled = dw2_linkage_name (die, cu);
11066
11067 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11068 See https://github.com/rust-lang/rust/issues/32925. */
11069 if (cu->language == language_rust && mangled != NULL
11070 && strchr (mangled, '{') != NULL)
11071 mangled = NULL;
11072
11073 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11074 has computed. */
11075 gdb::unique_xmalloc_ptr<char> demangled;
11076 if (mangled != NULL)
11077 {
11078 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
11079 type. It is easier for GDB users to search for such functions as
11080 `name(params)' than `long name(params)'. In such case the minimal
11081 symbol names do not match the full symbol names but for template
11082 functions there is never a need to look up their definition from their
11083 declaration so the only disadvantage remains the minimal symbol
11084 variant `long name(params)' does not have the proper inferior type.
11085 */
11086
11087 if (cu->language == language_go)
11088 {
11089 /* This is a lie, but we already lie to the caller new_symbol_full.
11090 new_symbol_full assumes we return the mangled name.
11091 This just undoes that lie until things are cleaned up. */
11092 }
11093 else
11094 {
11095 demangled.reset (gdb_demangle (mangled,
11096 (DMGL_PARAMS | DMGL_ANSI
11097 | DMGL_RET_DROP)));
11098 }
11099 if (demangled)
11100 canon = demangled.get ();
11101 else
11102 {
11103 canon = mangled;
11104 need_copy = 0;
11105 }
11106 }
11107
11108 if (canon == NULL || check_physname)
11109 {
11110 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11111
11112 if (canon != NULL && strcmp (physname, canon) != 0)
11113 {
11114 /* It may not mean a bug in GDB. The compiler could also
11115 compute DW_AT_linkage_name incorrectly. But in such case
11116 GDB would need to be bug-to-bug compatible. */
11117
11118 complaint (&symfile_complaints,
11119 _("Computed physname <%s> does not match demangled <%s> "
11120 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
11121 physname, canon, mangled, to_underlying (die->sect_off),
11122 objfile_name (objfile));
11123
11124 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11125 is available here - over computed PHYSNAME. It is safer
11126 against both buggy GDB and buggy compilers. */
11127
11128 retval = canon;
11129 }
11130 else
11131 {
11132 retval = physname;
11133 need_copy = 0;
11134 }
11135 }
11136 else
11137 retval = canon;
11138
11139 if (need_copy)
11140 retval = ((const char *)
11141 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11142 retval, strlen (retval)));
11143
11144 return retval;
11145 }
11146
11147 /* Inspect DIE in CU for a namespace alias. If one exists, record
11148 a new symbol for it.
11149
11150 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11151
11152 static int
11153 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11154 {
11155 struct attribute *attr;
11156
11157 /* If the die does not have a name, this is not a namespace
11158 alias. */
11159 attr = dwarf2_attr (die, DW_AT_name, cu);
11160 if (attr != NULL)
11161 {
11162 int num;
11163 struct die_info *d = die;
11164 struct dwarf2_cu *imported_cu = cu;
11165
11166 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11167 keep inspecting DIEs until we hit the underlying import. */
11168 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11169 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11170 {
11171 attr = dwarf2_attr (d, DW_AT_import, cu);
11172 if (attr == NULL)
11173 break;
11174
11175 d = follow_die_ref (d, attr, &imported_cu);
11176 if (d->tag != DW_TAG_imported_declaration)
11177 break;
11178 }
11179
11180 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11181 {
11182 complaint (&symfile_complaints,
11183 _("DIE at 0x%x has too many recursively imported "
11184 "declarations"), to_underlying (d->sect_off));
11185 return 0;
11186 }
11187
11188 if (attr != NULL)
11189 {
11190 struct type *type;
11191 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11192
11193 type = get_die_type_at_offset (sect_off, cu->per_cu);
11194 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11195 {
11196 /* This declaration is a global namespace alias. Add
11197 a symbol for it whose type is the aliased namespace. */
11198 new_symbol (die, type, cu);
11199 return 1;
11200 }
11201 }
11202 }
11203
11204 return 0;
11205 }
11206
11207 /* Return the using directives repository (global or local?) to use in the
11208 current context for LANGUAGE.
11209
11210 For Ada, imported declarations can materialize renamings, which *may* be
11211 global. However it is impossible (for now?) in DWARF to distinguish
11212 "external" imported declarations and "static" ones. As all imported
11213 declarations seem to be static in all other languages, make them all CU-wide
11214 global only in Ada. */
11215
11216 static struct using_direct **
11217 using_directives (enum language language)
11218 {
11219 if (language == language_ada && context_stack_depth == 0)
11220 return &global_using_directives;
11221 else
11222 return &local_using_directives;
11223 }
11224
11225 /* Read the import statement specified by the given die and record it. */
11226
11227 static void
11228 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11229 {
11230 struct objfile *objfile = cu->objfile;
11231 struct attribute *import_attr;
11232 struct die_info *imported_die, *child_die;
11233 struct dwarf2_cu *imported_cu;
11234 const char *imported_name;
11235 const char *imported_name_prefix;
11236 const char *canonical_name;
11237 const char *import_alias;
11238 const char *imported_declaration = NULL;
11239 const char *import_prefix;
11240 std::vector<const char *> excludes;
11241
11242 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11243 if (import_attr == NULL)
11244 {
11245 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11246 dwarf_tag_name (die->tag));
11247 return;
11248 }
11249
11250 imported_cu = cu;
11251 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11252 imported_name = dwarf2_name (imported_die, imported_cu);
11253 if (imported_name == NULL)
11254 {
11255 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11256
11257 The import in the following code:
11258 namespace A
11259 {
11260 typedef int B;
11261 }
11262
11263 int main ()
11264 {
11265 using A::B;
11266 B b;
11267 return b;
11268 }
11269
11270 ...
11271 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11272 <52> DW_AT_decl_file : 1
11273 <53> DW_AT_decl_line : 6
11274 <54> DW_AT_import : <0x75>
11275 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11276 <59> DW_AT_name : B
11277 <5b> DW_AT_decl_file : 1
11278 <5c> DW_AT_decl_line : 2
11279 <5d> DW_AT_type : <0x6e>
11280 ...
11281 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11282 <76> DW_AT_byte_size : 4
11283 <77> DW_AT_encoding : 5 (signed)
11284
11285 imports the wrong die ( 0x75 instead of 0x58 ).
11286 This case will be ignored until the gcc bug is fixed. */
11287 return;
11288 }
11289
11290 /* Figure out the local name after import. */
11291 import_alias = dwarf2_name (die, cu);
11292
11293 /* Figure out where the statement is being imported to. */
11294 import_prefix = determine_prefix (die, cu);
11295
11296 /* Figure out what the scope of the imported die is and prepend it
11297 to the name of the imported die. */
11298 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11299
11300 if (imported_die->tag != DW_TAG_namespace
11301 && imported_die->tag != DW_TAG_module)
11302 {
11303 imported_declaration = imported_name;
11304 canonical_name = imported_name_prefix;
11305 }
11306 else if (strlen (imported_name_prefix) > 0)
11307 canonical_name = obconcat (&objfile->objfile_obstack,
11308 imported_name_prefix,
11309 (cu->language == language_d ? "." : "::"),
11310 imported_name, (char *) NULL);
11311 else
11312 canonical_name = imported_name;
11313
11314 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11315 for (child_die = die->child; child_die && child_die->tag;
11316 child_die = sibling_die (child_die))
11317 {
11318 /* DWARF-4: A Fortran use statement with a “rename list” may be
11319 represented by an imported module entry with an import attribute
11320 referring to the module and owned entries corresponding to those
11321 entities that are renamed as part of being imported. */
11322
11323 if (child_die->tag != DW_TAG_imported_declaration)
11324 {
11325 complaint (&symfile_complaints,
11326 _("child DW_TAG_imported_declaration expected "
11327 "- DIE at 0x%x [in module %s]"),
11328 to_underlying (child_die->sect_off), objfile_name (objfile));
11329 continue;
11330 }
11331
11332 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11333 if (import_attr == NULL)
11334 {
11335 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11336 dwarf_tag_name (child_die->tag));
11337 continue;
11338 }
11339
11340 imported_cu = cu;
11341 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11342 &imported_cu);
11343 imported_name = dwarf2_name (imported_die, imported_cu);
11344 if (imported_name == NULL)
11345 {
11346 complaint (&symfile_complaints,
11347 _("child DW_TAG_imported_declaration has unknown "
11348 "imported name - DIE at 0x%x [in module %s]"),
11349 to_underlying (child_die->sect_off), objfile_name (objfile));
11350 continue;
11351 }
11352
11353 excludes.push_back (imported_name);
11354
11355 process_die (child_die, cu);
11356 }
11357
11358 add_using_directive (using_directives (cu->language),
11359 import_prefix,
11360 canonical_name,
11361 import_alias,
11362 imported_declaration,
11363 excludes,
11364 0,
11365 &objfile->objfile_obstack);
11366 }
11367
11368 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11369 types, but gives them a size of zero. Starting with version 14,
11370 ICC is compatible with GCC. */
11371
11372 static int
11373 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11374 {
11375 if (!cu->checked_producer)
11376 check_producer (cu);
11377
11378 return cu->producer_is_icc_lt_14;
11379 }
11380
11381 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11382 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11383 this, it was first present in GCC release 4.3.0. */
11384
11385 static int
11386 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11387 {
11388 if (!cu->checked_producer)
11389 check_producer (cu);
11390
11391 return cu->producer_is_gcc_lt_4_3;
11392 }
11393
11394 static file_and_directory
11395 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11396 {
11397 file_and_directory res;
11398
11399 /* Find the filename. Do not use dwarf2_name here, since the filename
11400 is not a source language identifier. */
11401 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11402 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11403
11404 if (res.comp_dir == NULL
11405 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11406 && IS_ABSOLUTE_PATH (res.name))
11407 {
11408 res.comp_dir_storage = ldirname (res.name);
11409 if (!res.comp_dir_storage.empty ())
11410 res.comp_dir = res.comp_dir_storage.c_str ();
11411 }
11412 if (res.comp_dir != NULL)
11413 {
11414 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11415 directory, get rid of it. */
11416 const char *cp = strchr (res.comp_dir, ':');
11417
11418 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11419 res.comp_dir = cp + 1;
11420 }
11421
11422 if (res.name == NULL)
11423 res.name = "<unknown>";
11424
11425 return res;
11426 }
11427
11428 /* Handle DW_AT_stmt_list for a compilation unit.
11429 DIE is the DW_TAG_compile_unit die for CU.
11430 COMP_DIR is the compilation directory. LOWPC is passed to
11431 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11432
11433 static void
11434 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11435 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11436 {
11437 struct objfile *objfile = dwarf2_per_objfile->objfile;
11438 struct attribute *attr;
11439 struct line_header line_header_local;
11440 hashval_t line_header_local_hash;
11441 void **slot;
11442 int decode_mapping;
11443
11444 gdb_assert (! cu->per_cu->is_debug_types);
11445
11446 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11447 if (attr == NULL)
11448 return;
11449
11450 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11451
11452 /* The line header hash table is only created if needed (it exists to
11453 prevent redundant reading of the line table for partial_units).
11454 If we're given a partial_unit, we'll need it. If we're given a
11455 compile_unit, then use the line header hash table if it's already
11456 created, but don't create one just yet. */
11457
11458 if (dwarf2_per_objfile->line_header_hash == NULL
11459 && die->tag == DW_TAG_partial_unit)
11460 {
11461 dwarf2_per_objfile->line_header_hash
11462 = htab_create_alloc_ex (127, line_header_hash_voidp,
11463 line_header_eq_voidp,
11464 free_line_header_voidp,
11465 &objfile->objfile_obstack,
11466 hashtab_obstack_allocate,
11467 dummy_obstack_deallocate);
11468 }
11469
11470 line_header_local.sect_off = line_offset;
11471 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11472 line_header_local_hash = line_header_hash (&line_header_local);
11473 if (dwarf2_per_objfile->line_header_hash != NULL)
11474 {
11475 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11476 &line_header_local,
11477 line_header_local_hash, NO_INSERT);
11478
11479 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11480 is not present in *SLOT (since if there is something in *SLOT then
11481 it will be for a partial_unit). */
11482 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11483 {
11484 gdb_assert (*slot != NULL);
11485 cu->line_header = (struct line_header *) *slot;
11486 return;
11487 }
11488 }
11489
11490 /* dwarf_decode_line_header does not yet provide sufficient information.
11491 We always have to call also dwarf_decode_lines for it. */
11492 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11493 if (lh == NULL)
11494 return;
11495
11496 cu->line_header = lh.release ();
11497 cu->line_header_die_owner = die;
11498
11499 if (dwarf2_per_objfile->line_header_hash == NULL)
11500 slot = NULL;
11501 else
11502 {
11503 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11504 &line_header_local,
11505 line_header_local_hash, INSERT);
11506 gdb_assert (slot != NULL);
11507 }
11508 if (slot != NULL && *slot == NULL)
11509 {
11510 /* This newly decoded line number information unit will be owned
11511 by line_header_hash hash table. */
11512 *slot = cu->line_header;
11513 cu->line_header_die_owner = NULL;
11514 }
11515 else
11516 {
11517 /* We cannot free any current entry in (*slot) as that struct line_header
11518 may be already used by multiple CUs. Create only temporary decoded
11519 line_header for this CU - it may happen at most once for each line
11520 number information unit. And if we're not using line_header_hash
11521 then this is what we want as well. */
11522 gdb_assert (die->tag != DW_TAG_partial_unit);
11523 }
11524 decode_mapping = (die->tag != DW_TAG_partial_unit);
11525 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11526 decode_mapping);
11527
11528 }
11529
11530 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11531
11532 static void
11533 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11534 {
11535 struct objfile *objfile = dwarf2_per_objfile->objfile;
11536 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11537 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11538 CORE_ADDR highpc = ((CORE_ADDR) 0);
11539 struct attribute *attr;
11540 struct die_info *child_die;
11541 CORE_ADDR baseaddr;
11542
11543 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11544
11545 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11546
11547 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11548 from finish_block. */
11549 if (lowpc == ((CORE_ADDR) -1))
11550 lowpc = highpc;
11551 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11552
11553 file_and_directory fnd = find_file_and_directory (die, cu);
11554
11555 prepare_one_comp_unit (cu, die, cu->language);
11556
11557 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11558 standardised yet. As a workaround for the language detection we fall
11559 back to the DW_AT_producer string. */
11560 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11561 cu->language = language_opencl;
11562
11563 /* Similar hack for Go. */
11564 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11565 set_cu_language (DW_LANG_Go, cu);
11566
11567 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11568
11569 /* Decode line number information if present. We do this before
11570 processing child DIEs, so that the line header table is available
11571 for DW_AT_decl_file. */
11572 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11573
11574 /* Process all dies in compilation unit. */
11575 if (die->child != NULL)
11576 {
11577 child_die = die->child;
11578 while (child_die && child_die->tag)
11579 {
11580 process_die (child_die, cu);
11581 child_die = sibling_die (child_die);
11582 }
11583 }
11584
11585 /* Decode macro information, if present. Dwarf 2 macro information
11586 refers to information in the line number info statement program
11587 header, so we can only read it if we've read the header
11588 successfully. */
11589 attr = dwarf2_attr (die, DW_AT_macros, cu);
11590 if (attr == NULL)
11591 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11592 if (attr && cu->line_header)
11593 {
11594 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11595 complaint (&symfile_complaints,
11596 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11597
11598 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11599 }
11600 else
11601 {
11602 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11603 if (attr && cu->line_header)
11604 {
11605 unsigned int macro_offset = DW_UNSND (attr);
11606
11607 dwarf_decode_macros (cu, macro_offset, 0);
11608 }
11609 }
11610 }
11611
11612 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11613 Create the set of symtabs used by this TU, or if this TU is sharing
11614 symtabs with another TU and the symtabs have already been created
11615 then restore those symtabs in the line header.
11616 We don't need the pc/line-number mapping for type units. */
11617
11618 static void
11619 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
11620 {
11621 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11622 struct type_unit_group *tu_group;
11623 int first_time;
11624 struct attribute *attr;
11625 unsigned int i;
11626 struct signatured_type *sig_type;
11627
11628 gdb_assert (per_cu->is_debug_types);
11629 sig_type = (struct signatured_type *) per_cu;
11630
11631 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11632
11633 /* If we're using .gdb_index (includes -readnow) then
11634 per_cu->type_unit_group may not have been set up yet. */
11635 if (sig_type->type_unit_group == NULL)
11636 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11637 tu_group = sig_type->type_unit_group;
11638
11639 /* If we've already processed this stmt_list there's no real need to
11640 do it again, we could fake it and just recreate the part we need
11641 (file name,index -> symtab mapping). If data shows this optimization
11642 is useful we can do it then. */
11643 first_time = tu_group->compunit_symtab == NULL;
11644
11645 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11646 debug info. */
11647 line_header_up lh;
11648 if (attr != NULL)
11649 {
11650 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11651 lh = dwarf_decode_line_header (line_offset, cu);
11652 }
11653 if (lh == NULL)
11654 {
11655 if (first_time)
11656 dwarf2_start_symtab (cu, "", NULL, 0);
11657 else
11658 {
11659 gdb_assert (tu_group->symtabs == NULL);
11660 restart_symtab (tu_group->compunit_symtab, "", 0);
11661 }
11662 return;
11663 }
11664
11665 cu->line_header = lh.release ();
11666 cu->line_header_die_owner = die;
11667
11668 if (first_time)
11669 {
11670 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
11671
11672 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11673 still initializing it, and our caller (a few levels up)
11674 process_full_type_unit still needs to know if this is the first
11675 time. */
11676
11677 tu_group->num_symtabs = cu->line_header->file_names.size ();
11678 tu_group->symtabs = XNEWVEC (struct symtab *,
11679 cu->line_header->file_names.size ());
11680
11681 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11682 {
11683 file_entry &fe = cu->line_header->file_names[i];
11684
11685 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
11686
11687 if (current_subfile->symtab == NULL)
11688 {
11689 /* NOTE: start_subfile will recognize when it's been
11690 passed a file it has already seen. So we can't
11691 assume there's a simple mapping from
11692 cu->line_header->file_names to subfiles, plus
11693 cu->line_header->file_names may contain dups. */
11694 current_subfile->symtab
11695 = allocate_symtab (cust, current_subfile->name);
11696 }
11697
11698 fe.symtab = current_subfile->symtab;
11699 tu_group->symtabs[i] = fe.symtab;
11700 }
11701 }
11702 else
11703 {
11704 restart_symtab (tu_group->compunit_symtab, "", 0);
11705
11706 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11707 {
11708 file_entry &fe = cu->line_header->file_names[i];
11709
11710 fe.symtab = tu_group->symtabs[i];
11711 }
11712 }
11713
11714 /* The main symtab is allocated last. Type units don't have DW_AT_name
11715 so they don't have a "real" (so to speak) symtab anyway.
11716 There is later code that will assign the main symtab to all symbols
11717 that don't have one. We need to handle the case of a symbol with a
11718 missing symtab (DW_AT_decl_file) anyway. */
11719 }
11720
11721 /* Process DW_TAG_type_unit.
11722 For TUs we want to skip the first top level sibling if it's not the
11723 actual type being defined by this TU. In this case the first top
11724 level sibling is there to provide context only. */
11725
11726 static void
11727 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11728 {
11729 struct die_info *child_die;
11730
11731 prepare_one_comp_unit (cu, die, language_minimal);
11732
11733 /* Initialize (or reinitialize) the machinery for building symtabs.
11734 We do this before processing child DIEs, so that the line header table
11735 is available for DW_AT_decl_file. */
11736 setup_type_unit_groups (die, cu);
11737
11738 if (die->child != NULL)
11739 {
11740 child_die = die->child;
11741 while (child_die && child_die->tag)
11742 {
11743 process_die (child_die, cu);
11744 child_die = sibling_die (child_die);
11745 }
11746 }
11747 }
11748 \f
11749 /* DWO/DWP files.
11750
11751 http://gcc.gnu.org/wiki/DebugFission
11752 http://gcc.gnu.org/wiki/DebugFissionDWP
11753
11754 To simplify handling of both DWO files ("object" files with the DWARF info)
11755 and DWP files (a file with the DWOs packaged up into one file), we treat
11756 DWP files as having a collection of virtual DWO files. */
11757
11758 static hashval_t
11759 hash_dwo_file (const void *item)
11760 {
11761 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11762 hashval_t hash;
11763
11764 hash = htab_hash_string (dwo_file->dwo_name);
11765 if (dwo_file->comp_dir != NULL)
11766 hash += htab_hash_string (dwo_file->comp_dir);
11767 return hash;
11768 }
11769
11770 static int
11771 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11772 {
11773 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11774 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11775
11776 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11777 return 0;
11778 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11779 return lhs->comp_dir == rhs->comp_dir;
11780 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11781 }
11782
11783 /* Allocate a hash table for DWO files. */
11784
11785 static htab_t
11786 allocate_dwo_file_hash_table (void)
11787 {
11788 struct objfile *objfile = dwarf2_per_objfile->objfile;
11789
11790 return htab_create_alloc_ex (41,
11791 hash_dwo_file,
11792 eq_dwo_file,
11793 NULL,
11794 &objfile->objfile_obstack,
11795 hashtab_obstack_allocate,
11796 dummy_obstack_deallocate);
11797 }
11798
11799 /* Lookup DWO file DWO_NAME. */
11800
11801 static void **
11802 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
11803 {
11804 struct dwo_file find_entry;
11805 void **slot;
11806
11807 if (dwarf2_per_objfile->dwo_files == NULL)
11808 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
11809
11810 memset (&find_entry, 0, sizeof (find_entry));
11811 find_entry.dwo_name = dwo_name;
11812 find_entry.comp_dir = comp_dir;
11813 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11814
11815 return slot;
11816 }
11817
11818 static hashval_t
11819 hash_dwo_unit (const void *item)
11820 {
11821 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11822
11823 /* This drops the top 32 bits of the id, but is ok for a hash. */
11824 return dwo_unit->signature;
11825 }
11826
11827 static int
11828 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11829 {
11830 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11831 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11832
11833 /* The signature is assumed to be unique within the DWO file.
11834 So while object file CU dwo_id's always have the value zero,
11835 that's OK, assuming each object file DWO file has only one CU,
11836 and that's the rule for now. */
11837 return lhs->signature == rhs->signature;
11838 }
11839
11840 /* Allocate a hash table for DWO CUs,TUs.
11841 There is one of these tables for each of CUs,TUs for each DWO file. */
11842
11843 static htab_t
11844 allocate_dwo_unit_table (struct objfile *objfile)
11845 {
11846 /* Start out with a pretty small number.
11847 Generally DWO files contain only one CU and maybe some TUs. */
11848 return htab_create_alloc_ex (3,
11849 hash_dwo_unit,
11850 eq_dwo_unit,
11851 NULL,
11852 &objfile->objfile_obstack,
11853 hashtab_obstack_allocate,
11854 dummy_obstack_deallocate);
11855 }
11856
11857 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11858
11859 struct create_dwo_cu_data
11860 {
11861 struct dwo_file *dwo_file;
11862 struct dwo_unit dwo_unit;
11863 };
11864
11865 /* die_reader_func for create_dwo_cu. */
11866
11867 static void
11868 create_dwo_cu_reader (const struct die_reader_specs *reader,
11869 const gdb_byte *info_ptr,
11870 struct die_info *comp_unit_die,
11871 int has_children,
11872 void *datap)
11873 {
11874 struct dwarf2_cu *cu = reader->cu;
11875 sect_offset sect_off = cu->per_cu->sect_off;
11876 struct dwarf2_section_info *section = cu->per_cu->section;
11877 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11878 struct dwo_file *dwo_file = data->dwo_file;
11879 struct dwo_unit *dwo_unit = &data->dwo_unit;
11880 struct attribute *attr;
11881
11882 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11883 if (attr == NULL)
11884 {
11885 complaint (&symfile_complaints,
11886 _("Dwarf Error: debug entry at offset 0x%x is missing"
11887 " its dwo_id [in module %s]"),
11888 to_underlying (sect_off), dwo_file->dwo_name);
11889 return;
11890 }
11891
11892 dwo_unit->dwo_file = dwo_file;
11893 dwo_unit->signature = DW_UNSND (attr);
11894 dwo_unit->section = section;
11895 dwo_unit->sect_off = sect_off;
11896 dwo_unit->length = cu->per_cu->length;
11897
11898 if (dwarf_read_debug)
11899 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
11900 to_underlying (sect_off),
11901 hex_string (dwo_unit->signature));
11902 }
11903
11904 /* Create the dwo_units for the CUs in a DWO_FILE.
11905 Note: This function processes DWO files only, not DWP files. */
11906
11907 static void
11908 create_cus_hash_table (struct dwo_file &dwo_file, dwarf2_section_info &section,
11909 htab_t &cus_htab)
11910 {
11911 struct objfile *objfile = dwarf2_per_objfile->objfile;
11912 const gdb_byte *info_ptr, *end_ptr;
11913
11914 dwarf2_read_section (objfile, &section);
11915 info_ptr = section.buffer;
11916
11917 if (info_ptr == NULL)
11918 return;
11919
11920 if (dwarf_read_debug)
11921 {
11922 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11923 get_section_name (&section),
11924 get_section_file_name (&section));
11925 }
11926
11927 end_ptr = info_ptr + section.size;
11928 while (info_ptr < end_ptr)
11929 {
11930 struct dwarf2_per_cu_data per_cu;
11931 struct create_dwo_cu_data create_dwo_cu_data;
11932 struct dwo_unit *dwo_unit;
11933 void **slot;
11934 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11935
11936 memset (&create_dwo_cu_data.dwo_unit, 0,
11937 sizeof (create_dwo_cu_data.dwo_unit));
11938 memset (&per_cu, 0, sizeof (per_cu));
11939 per_cu.objfile = objfile;
11940 per_cu.is_debug_types = 0;
11941 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11942 per_cu.section = &section;
11943 create_dwo_cu_data.dwo_file = &dwo_file;
11944
11945 init_cutu_and_read_dies_no_follow (
11946 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11947 info_ptr += per_cu.length;
11948
11949 // If the unit could not be parsed, skip it.
11950 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11951 continue;
11952
11953 if (cus_htab == NULL)
11954 cus_htab = allocate_dwo_unit_table (objfile);
11955
11956 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11957 *dwo_unit = create_dwo_cu_data.dwo_unit;
11958 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11959 gdb_assert (slot != NULL);
11960 if (*slot != NULL)
11961 {
11962 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11963 sect_offset dup_sect_off = dup_cu->sect_off;
11964
11965 complaint (&symfile_complaints,
11966 _("debug cu entry at offset 0x%x is duplicate to"
11967 " the entry at offset 0x%x, signature %s"),
11968 to_underlying (sect_off), to_underlying (dup_sect_off),
11969 hex_string (dwo_unit->signature));
11970 }
11971 *slot = (void *)dwo_unit;
11972 }
11973 }
11974
11975 /* DWP file .debug_{cu,tu}_index section format:
11976 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11977
11978 DWP Version 1:
11979
11980 Both index sections have the same format, and serve to map a 64-bit
11981 signature to a set of section numbers. Each section begins with a header,
11982 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11983 indexes, and a pool of 32-bit section numbers. The index sections will be
11984 aligned at 8-byte boundaries in the file.
11985
11986 The index section header consists of:
11987
11988 V, 32 bit version number
11989 -, 32 bits unused
11990 N, 32 bit number of compilation units or type units in the index
11991 M, 32 bit number of slots in the hash table
11992
11993 Numbers are recorded using the byte order of the application binary.
11994
11995 The hash table begins at offset 16 in the section, and consists of an array
11996 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11997 order of the application binary). Unused slots in the hash table are 0.
11998 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11999
12000 The parallel table begins immediately after the hash table
12001 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12002 array of 32-bit indexes (using the byte order of the application binary),
12003 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12004 table contains a 32-bit index into the pool of section numbers. For unused
12005 hash table slots, the corresponding entry in the parallel table will be 0.
12006
12007 The pool of section numbers begins immediately following the hash table
12008 (at offset 16 + 12 * M from the beginning of the section). The pool of
12009 section numbers consists of an array of 32-bit words (using the byte order
12010 of the application binary). Each item in the array is indexed starting
12011 from 0. The hash table entry provides the index of the first section
12012 number in the set. Additional section numbers in the set follow, and the
12013 set is terminated by a 0 entry (section number 0 is not used in ELF).
12014
12015 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12016 section must be the first entry in the set, and the .debug_abbrev.dwo must
12017 be the second entry. Other members of the set may follow in any order.
12018
12019 ---
12020
12021 DWP Version 2:
12022
12023 DWP Version 2 combines all the .debug_info, etc. sections into one,
12024 and the entries in the index tables are now offsets into these sections.
12025 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12026 section.
12027
12028 Index Section Contents:
12029 Header
12030 Hash Table of Signatures dwp_hash_table.hash_table
12031 Parallel Table of Indices dwp_hash_table.unit_table
12032 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12033 Table of Section Sizes dwp_hash_table.v2.sizes
12034
12035 The index section header consists of:
12036
12037 V, 32 bit version number
12038 L, 32 bit number of columns in the table of section offsets
12039 N, 32 bit number of compilation units or type units in the index
12040 M, 32 bit number of slots in the hash table
12041
12042 Numbers are recorded using the byte order of the application binary.
12043
12044 The hash table has the same format as version 1.
12045 The parallel table of indices has the same format as version 1,
12046 except that the entries are origin-1 indices into the table of sections
12047 offsets and the table of section sizes.
12048
12049 The table of offsets begins immediately following the parallel table
12050 (at offset 16 + 12 * M from the beginning of the section). The table is
12051 a two-dimensional array of 32-bit words (using the byte order of the
12052 application binary), with L columns and N+1 rows, in row-major order.
12053 Each row in the array is indexed starting from 0. The first row provides
12054 a key to the remaining rows: each column in this row provides an identifier
12055 for a debug section, and the offsets in the same column of subsequent rows
12056 refer to that section. The section identifiers are:
12057
12058 DW_SECT_INFO 1 .debug_info.dwo
12059 DW_SECT_TYPES 2 .debug_types.dwo
12060 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12061 DW_SECT_LINE 4 .debug_line.dwo
12062 DW_SECT_LOC 5 .debug_loc.dwo
12063 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12064 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12065 DW_SECT_MACRO 8 .debug_macro.dwo
12066
12067 The offsets provided by the CU and TU index sections are the base offsets
12068 for the contributions made by each CU or TU to the corresponding section
12069 in the package file. Each CU and TU header contains an abbrev_offset
12070 field, used to find the abbreviations table for that CU or TU within the
12071 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12072 be interpreted as relative to the base offset given in the index section.
12073 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12074 should be interpreted as relative to the base offset for .debug_line.dwo,
12075 and offsets into other debug sections obtained from DWARF attributes should
12076 also be interpreted as relative to the corresponding base offset.
12077
12078 The table of sizes begins immediately following the table of offsets.
12079 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12080 with L columns and N rows, in row-major order. Each row in the array is
12081 indexed starting from 1 (row 0 is shared by the two tables).
12082
12083 ---
12084
12085 Hash table lookup is handled the same in version 1 and 2:
12086
12087 We assume that N and M will not exceed 2^32 - 1.
12088 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12089
12090 Given a 64-bit compilation unit signature or a type signature S, an entry
12091 in the hash table is located as follows:
12092
12093 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12094 the low-order k bits all set to 1.
12095
12096 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12097
12098 3) If the hash table entry at index H matches the signature, use that
12099 entry. If the hash table entry at index H is unused (all zeroes),
12100 terminate the search: the signature is not present in the table.
12101
12102 4) Let H = (H + H') modulo M. Repeat at Step 3.
12103
12104 Because M > N and H' and M are relatively prime, the search is guaranteed
12105 to stop at an unused slot or find the match. */
12106
12107 /* Create a hash table to map DWO IDs to their CU/TU entry in
12108 .debug_{info,types}.dwo in DWP_FILE.
12109 Returns NULL if there isn't one.
12110 Note: This function processes DWP files only, not DWO files. */
12111
12112 static struct dwp_hash_table *
12113 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
12114 {
12115 struct objfile *objfile = dwarf2_per_objfile->objfile;
12116 bfd *dbfd = dwp_file->dbfd;
12117 const gdb_byte *index_ptr, *index_end;
12118 struct dwarf2_section_info *index;
12119 uint32_t version, nr_columns, nr_units, nr_slots;
12120 struct dwp_hash_table *htab;
12121
12122 if (is_debug_types)
12123 index = &dwp_file->sections.tu_index;
12124 else
12125 index = &dwp_file->sections.cu_index;
12126
12127 if (dwarf2_section_empty_p (index))
12128 return NULL;
12129 dwarf2_read_section (objfile, index);
12130
12131 index_ptr = index->buffer;
12132 index_end = index_ptr + index->size;
12133
12134 version = read_4_bytes (dbfd, index_ptr);
12135 index_ptr += 4;
12136 if (version == 2)
12137 nr_columns = read_4_bytes (dbfd, index_ptr);
12138 else
12139 nr_columns = 0;
12140 index_ptr += 4;
12141 nr_units = read_4_bytes (dbfd, index_ptr);
12142 index_ptr += 4;
12143 nr_slots = read_4_bytes (dbfd, index_ptr);
12144 index_ptr += 4;
12145
12146 if (version != 1 && version != 2)
12147 {
12148 error (_("Dwarf Error: unsupported DWP file version (%s)"
12149 " [in module %s]"),
12150 pulongest (version), dwp_file->name);
12151 }
12152 if (nr_slots != (nr_slots & -nr_slots))
12153 {
12154 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12155 " is not power of 2 [in module %s]"),
12156 pulongest (nr_slots), dwp_file->name);
12157 }
12158
12159 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12160 htab->version = version;
12161 htab->nr_columns = nr_columns;
12162 htab->nr_units = nr_units;
12163 htab->nr_slots = nr_slots;
12164 htab->hash_table = index_ptr;
12165 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12166
12167 /* Exit early if the table is empty. */
12168 if (nr_slots == 0 || nr_units == 0
12169 || (version == 2 && nr_columns == 0))
12170 {
12171 /* All must be zero. */
12172 if (nr_slots != 0 || nr_units != 0
12173 || (version == 2 && nr_columns != 0))
12174 {
12175 complaint (&symfile_complaints,
12176 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12177 " all zero [in modules %s]"),
12178 dwp_file->name);
12179 }
12180 return htab;
12181 }
12182
12183 if (version == 1)
12184 {
12185 htab->section_pool.v1.indices =
12186 htab->unit_table + sizeof (uint32_t) * nr_slots;
12187 /* It's harder to decide whether the section is too small in v1.
12188 V1 is deprecated anyway so we punt. */
12189 }
12190 else
12191 {
12192 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12193 int *ids = htab->section_pool.v2.section_ids;
12194 /* Reverse map for error checking. */
12195 int ids_seen[DW_SECT_MAX + 1];
12196 int i;
12197
12198 if (nr_columns < 2)
12199 {
12200 error (_("Dwarf Error: bad DWP hash table, too few columns"
12201 " in section table [in module %s]"),
12202 dwp_file->name);
12203 }
12204 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12205 {
12206 error (_("Dwarf Error: bad DWP hash table, too many columns"
12207 " in section table [in module %s]"),
12208 dwp_file->name);
12209 }
12210 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12211 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12212 for (i = 0; i < nr_columns; ++i)
12213 {
12214 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12215
12216 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12217 {
12218 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12219 " in section table [in module %s]"),
12220 id, dwp_file->name);
12221 }
12222 if (ids_seen[id] != -1)
12223 {
12224 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12225 " id %d in section table [in module %s]"),
12226 id, dwp_file->name);
12227 }
12228 ids_seen[id] = i;
12229 ids[i] = id;
12230 }
12231 /* Must have exactly one info or types section. */
12232 if (((ids_seen[DW_SECT_INFO] != -1)
12233 + (ids_seen[DW_SECT_TYPES] != -1))
12234 != 1)
12235 {
12236 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12237 " DWO info/types section [in module %s]"),
12238 dwp_file->name);
12239 }
12240 /* Must have an abbrev section. */
12241 if (ids_seen[DW_SECT_ABBREV] == -1)
12242 {
12243 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12244 " section [in module %s]"),
12245 dwp_file->name);
12246 }
12247 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12248 htab->section_pool.v2.sizes =
12249 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12250 * nr_units * nr_columns);
12251 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12252 * nr_units * nr_columns))
12253 > index_end)
12254 {
12255 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12256 " [in module %s]"),
12257 dwp_file->name);
12258 }
12259 }
12260
12261 return htab;
12262 }
12263
12264 /* Update SECTIONS with the data from SECTP.
12265
12266 This function is like the other "locate" section routines that are
12267 passed to bfd_map_over_sections, but in this context the sections to
12268 read comes from the DWP V1 hash table, not the full ELF section table.
12269
12270 The result is non-zero for success, or zero if an error was found. */
12271
12272 static int
12273 locate_v1_virtual_dwo_sections (asection *sectp,
12274 struct virtual_v1_dwo_sections *sections)
12275 {
12276 const struct dwop_section_names *names = &dwop_section_names;
12277
12278 if (section_is_p (sectp->name, &names->abbrev_dwo))
12279 {
12280 /* There can be only one. */
12281 if (sections->abbrev.s.section != NULL)
12282 return 0;
12283 sections->abbrev.s.section = sectp;
12284 sections->abbrev.size = bfd_get_section_size (sectp);
12285 }
12286 else if (section_is_p (sectp->name, &names->info_dwo)
12287 || section_is_p (sectp->name, &names->types_dwo))
12288 {
12289 /* There can be only one. */
12290 if (sections->info_or_types.s.section != NULL)
12291 return 0;
12292 sections->info_or_types.s.section = sectp;
12293 sections->info_or_types.size = bfd_get_section_size (sectp);
12294 }
12295 else if (section_is_p (sectp->name, &names->line_dwo))
12296 {
12297 /* There can be only one. */
12298 if (sections->line.s.section != NULL)
12299 return 0;
12300 sections->line.s.section = sectp;
12301 sections->line.size = bfd_get_section_size (sectp);
12302 }
12303 else if (section_is_p (sectp->name, &names->loc_dwo))
12304 {
12305 /* There can be only one. */
12306 if (sections->loc.s.section != NULL)
12307 return 0;
12308 sections->loc.s.section = sectp;
12309 sections->loc.size = bfd_get_section_size (sectp);
12310 }
12311 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12312 {
12313 /* There can be only one. */
12314 if (sections->macinfo.s.section != NULL)
12315 return 0;
12316 sections->macinfo.s.section = sectp;
12317 sections->macinfo.size = bfd_get_section_size (sectp);
12318 }
12319 else if (section_is_p (sectp->name, &names->macro_dwo))
12320 {
12321 /* There can be only one. */
12322 if (sections->macro.s.section != NULL)
12323 return 0;
12324 sections->macro.s.section = sectp;
12325 sections->macro.size = bfd_get_section_size (sectp);
12326 }
12327 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12328 {
12329 /* There can be only one. */
12330 if (sections->str_offsets.s.section != NULL)
12331 return 0;
12332 sections->str_offsets.s.section = sectp;
12333 sections->str_offsets.size = bfd_get_section_size (sectp);
12334 }
12335 else
12336 {
12337 /* No other kind of section is valid. */
12338 return 0;
12339 }
12340
12341 return 1;
12342 }
12343
12344 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12345 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12346 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12347 This is for DWP version 1 files. */
12348
12349 static struct dwo_unit *
12350 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
12351 uint32_t unit_index,
12352 const char *comp_dir,
12353 ULONGEST signature, int is_debug_types)
12354 {
12355 struct objfile *objfile = dwarf2_per_objfile->objfile;
12356 const struct dwp_hash_table *dwp_htab =
12357 is_debug_types ? dwp_file->tus : dwp_file->cus;
12358 bfd *dbfd = dwp_file->dbfd;
12359 const char *kind = is_debug_types ? "TU" : "CU";
12360 struct dwo_file *dwo_file;
12361 struct dwo_unit *dwo_unit;
12362 struct virtual_v1_dwo_sections sections;
12363 void **dwo_file_slot;
12364 int i;
12365
12366 gdb_assert (dwp_file->version == 1);
12367
12368 if (dwarf_read_debug)
12369 {
12370 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12371 kind,
12372 pulongest (unit_index), hex_string (signature),
12373 dwp_file->name);
12374 }
12375
12376 /* Fetch the sections of this DWO unit.
12377 Put a limit on the number of sections we look for so that bad data
12378 doesn't cause us to loop forever. */
12379
12380 #define MAX_NR_V1_DWO_SECTIONS \
12381 (1 /* .debug_info or .debug_types */ \
12382 + 1 /* .debug_abbrev */ \
12383 + 1 /* .debug_line */ \
12384 + 1 /* .debug_loc */ \
12385 + 1 /* .debug_str_offsets */ \
12386 + 1 /* .debug_macro or .debug_macinfo */ \
12387 + 1 /* trailing zero */)
12388
12389 memset (&sections, 0, sizeof (sections));
12390
12391 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12392 {
12393 asection *sectp;
12394 uint32_t section_nr =
12395 read_4_bytes (dbfd,
12396 dwp_htab->section_pool.v1.indices
12397 + (unit_index + i) * sizeof (uint32_t));
12398
12399 if (section_nr == 0)
12400 break;
12401 if (section_nr >= dwp_file->num_sections)
12402 {
12403 error (_("Dwarf Error: bad DWP hash table, section number too large"
12404 " [in module %s]"),
12405 dwp_file->name);
12406 }
12407
12408 sectp = dwp_file->elf_sections[section_nr];
12409 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12410 {
12411 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12412 " [in module %s]"),
12413 dwp_file->name);
12414 }
12415 }
12416
12417 if (i < 2
12418 || dwarf2_section_empty_p (&sections.info_or_types)
12419 || dwarf2_section_empty_p (&sections.abbrev))
12420 {
12421 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12422 " [in module %s]"),
12423 dwp_file->name);
12424 }
12425 if (i == MAX_NR_V1_DWO_SECTIONS)
12426 {
12427 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12428 " [in module %s]"),
12429 dwp_file->name);
12430 }
12431
12432 /* It's easier for the rest of the code if we fake a struct dwo_file and
12433 have dwo_unit "live" in that. At least for now.
12434
12435 The DWP file can be made up of a random collection of CUs and TUs.
12436 However, for each CU + set of TUs that came from the same original DWO
12437 file, we can combine them back into a virtual DWO file to save space
12438 (fewer struct dwo_file objects to allocate). Remember that for really
12439 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12440
12441 std::string virtual_dwo_name =
12442 string_printf ("virtual-dwo/%d-%d-%d-%d",
12443 get_section_id (&sections.abbrev),
12444 get_section_id (&sections.line),
12445 get_section_id (&sections.loc),
12446 get_section_id (&sections.str_offsets));
12447 /* Can we use an existing virtual DWO file? */
12448 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
12449 /* Create one if necessary. */
12450 if (*dwo_file_slot == NULL)
12451 {
12452 if (dwarf_read_debug)
12453 {
12454 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12455 virtual_dwo_name.c_str ());
12456 }
12457 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12458 dwo_file->dwo_name
12459 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12460 virtual_dwo_name.c_str (),
12461 virtual_dwo_name.size ());
12462 dwo_file->comp_dir = comp_dir;
12463 dwo_file->sections.abbrev = sections.abbrev;
12464 dwo_file->sections.line = sections.line;
12465 dwo_file->sections.loc = sections.loc;
12466 dwo_file->sections.macinfo = sections.macinfo;
12467 dwo_file->sections.macro = sections.macro;
12468 dwo_file->sections.str_offsets = sections.str_offsets;
12469 /* The "str" section is global to the entire DWP file. */
12470 dwo_file->sections.str = dwp_file->sections.str;
12471 /* The info or types section is assigned below to dwo_unit,
12472 there's no need to record it in dwo_file.
12473 Also, we can't simply record type sections in dwo_file because
12474 we record a pointer into the vector in dwo_unit. As we collect more
12475 types we'll grow the vector and eventually have to reallocate space
12476 for it, invalidating all copies of pointers into the previous
12477 contents. */
12478 *dwo_file_slot = dwo_file;
12479 }
12480 else
12481 {
12482 if (dwarf_read_debug)
12483 {
12484 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12485 virtual_dwo_name.c_str ());
12486 }
12487 dwo_file = (struct dwo_file *) *dwo_file_slot;
12488 }
12489
12490 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12491 dwo_unit->dwo_file = dwo_file;
12492 dwo_unit->signature = signature;
12493 dwo_unit->section =
12494 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12495 *dwo_unit->section = sections.info_or_types;
12496 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12497
12498 return dwo_unit;
12499 }
12500
12501 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12502 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12503 piece within that section used by a TU/CU, return a virtual section
12504 of just that piece. */
12505
12506 static struct dwarf2_section_info
12507 create_dwp_v2_section (struct dwarf2_section_info *section,
12508 bfd_size_type offset, bfd_size_type size)
12509 {
12510 struct dwarf2_section_info result;
12511 asection *sectp;
12512
12513 gdb_assert (section != NULL);
12514 gdb_assert (!section->is_virtual);
12515
12516 memset (&result, 0, sizeof (result));
12517 result.s.containing_section = section;
12518 result.is_virtual = 1;
12519
12520 if (size == 0)
12521 return result;
12522
12523 sectp = get_section_bfd_section (section);
12524
12525 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12526 bounds of the real section. This is a pretty-rare event, so just
12527 flag an error (easier) instead of a warning and trying to cope. */
12528 if (sectp == NULL
12529 || offset + size > bfd_get_section_size (sectp))
12530 {
12531 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12532 " in section %s [in module %s]"),
12533 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12534 objfile_name (dwarf2_per_objfile->objfile));
12535 }
12536
12537 result.virtual_offset = offset;
12538 result.size = size;
12539 return result;
12540 }
12541
12542 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12543 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12544 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12545 This is for DWP version 2 files. */
12546
12547 static struct dwo_unit *
12548 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
12549 uint32_t unit_index,
12550 const char *comp_dir,
12551 ULONGEST signature, int is_debug_types)
12552 {
12553 struct objfile *objfile = dwarf2_per_objfile->objfile;
12554 const struct dwp_hash_table *dwp_htab =
12555 is_debug_types ? dwp_file->tus : dwp_file->cus;
12556 bfd *dbfd = dwp_file->dbfd;
12557 const char *kind = is_debug_types ? "TU" : "CU";
12558 struct dwo_file *dwo_file;
12559 struct dwo_unit *dwo_unit;
12560 struct virtual_v2_dwo_sections sections;
12561 void **dwo_file_slot;
12562 int i;
12563
12564 gdb_assert (dwp_file->version == 2);
12565
12566 if (dwarf_read_debug)
12567 {
12568 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12569 kind,
12570 pulongest (unit_index), hex_string (signature),
12571 dwp_file->name);
12572 }
12573
12574 /* Fetch the section offsets of this DWO unit. */
12575
12576 memset (&sections, 0, sizeof (sections));
12577
12578 for (i = 0; i < dwp_htab->nr_columns; ++i)
12579 {
12580 uint32_t offset = read_4_bytes (dbfd,
12581 dwp_htab->section_pool.v2.offsets
12582 + (((unit_index - 1) * dwp_htab->nr_columns
12583 + i)
12584 * sizeof (uint32_t)));
12585 uint32_t size = read_4_bytes (dbfd,
12586 dwp_htab->section_pool.v2.sizes
12587 + (((unit_index - 1) * dwp_htab->nr_columns
12588 + i)
12589 * sizeof (uint32_t)));
12590
12591 switch (dwp_htab->section_pool.v2.section_ids[i])
12592 {
12593 case DW_SECT_INFO:
12594 case DW_SECT_TYPES:
12595 sections.info_or_types_offset = offset;
12596 sections.info_or_types_size = size;
12597 break;
12598 case DW_SECT_ABBREV:
12599 sections.abbrev_offset = offset;
12600 sections.abbrev_size = size;
12601 break;
12602 case DW_SECT_LINE:
12603 sections.line_offset = offset;
12604 sections.line_size = size;
12605 break;
12606 case DW_SECT_LOC:
12607 sections.loc_offset = offset;
12608 sections.loc_size = size;
12609 break;
12610 case DW_SECT_STR_OFFSETS:
12611 sections.str_offsets_offset = offset;
12612 sections.str_offsets_size = size;
12613 break;
12614 case DW_SECT_MACINFO:
12615 sections.macinfo_offset = offset;
12616 sections.macinfo_size = size;
12617 break;
12618 case DW_SECT_MACRO:
12619 sections.macro_offset = offset;
12620 sections.macro_size = size;
12621 break;
12622 }
12623 }
12624
12625 /* It's easier for the rest of the code if we fake a struct dwo_file and
12626 have dwo_unit "live" in that. At least for now.
12627
12628 The DWP file can be made up of a random collection of CUs and TUs.
12629 However, for each CU + set of TUs that came from the same original DWO
12630 file, we can combine them back into a virtual DWO file to save space
12631 (fewer struct dwo_file objects to allocate). Remember that for really
12632 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12633
12634 std::string virtual_dwo_name =
12635 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12636 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12637 (long) (sections.line_size ? sections.line_offset : 0),
12638 (long) (sections.loc_size ? sections.loc_offset : 0),
12639 (long) (sections.str_offsets_size
12640 ? sections.str_offsets_offset : 0));
12641 /* Can we use an existing virtual DWO file? */
12642 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name.c_str (), comp_dir);
12643 /* Create one if necessary. */
12644 if (*dwo_file_slot == NULL)
12645 {
12646 if (dwarf_read_debug)
12647 {
12648 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12649 virtual_dwo_name.c_str ());
12650 }
12651 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12652 dwo_file->dwo_name
12653 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12654 virtual_dwo_name.c_str (),
12655 virtual_dwo_name.size ());
12656 dwo_file->comp_dir = comp_dir;
12657 dwo_file->sections.abbrev =
12658 create_dwp_v2_section (&dwp_file->sections.abbrev,
12659 sections.abbrev_offset, sections.abbrev_size);
12660 dwo_file->sections.line =
12661 create_dwp_v2_section (&dwp_file->sections.line,
12662 sections.line_offset, sections.line_size);
12663 dwo_file->sections.loc =
12664 create_dwp_v2_section (&dwp_file->sections.loc,
12665 sections.loc_offset, sections.loc_size);
12666 dwo_file->sections.macinfo =
12667 create_dwp_v2_section (&dwp_file->sections.macinfo,
12668 sections.macinfo_offset, sections.macinfo_size);
12669 dwo_file->sections.macro =
12670 create_dwp_v2_section (&dwp_file->sections.macro,
12671 sections.macro_offset, sections.macro_size);
12672 dwo_file->sections.str_offsets =
12673 create_dwp_v2_section (&dwp_file->sections.str_offsets,
12674 sections.str_offsets_offset,
12675 sections.str_offsets_size);
12676 /* The "str" section is global to the entire DWP file. */
12677 dwo_file->sections.str = dwp_file->sections.str;
12678 /* The info or types section is assigned below to dwo_unit,
12679 there's no need to record it in dwo_file.
12680 Also, we can't simply record type sections in dwo_file because
12681 we record a pointer into the vector in dwo_unit. As we collect more
12682 types we'll grow the vector and eventually have to reallocate space
12683 for it, invalidating all copies of pointers into the previous
12684 contents. */
12685 *dwo_file_slot = dwo_file;
12686 }
12687 else
12688 {
12689 if (dwarf_read_debug)
12690 {
12691 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12692 virtual_dwo_name.c_str ());
12693 }
12694 dwo_file = (struct dwo_file *) *dwo_file_slot;
12695 }
12696
12697 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12698 dwo_unit->dwo_file = dwo_file;
12699 dwo_unit->signature = signature;
12700 dwo_unit->section =
12701 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12702 *dwo_unit->section = create_dwp_v2_section (is_debug_types
12703 ? &dwp_file->sections.types
12704 : &dwp_file->sections.info,
12705 sections.info_or_types_offset,
12706 sections.info_or_types_size);
12707 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12708
12709 return dwo_unit;
12710 }
12711
12712 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12713 Returns NULL if the signature isn't found. */
12714
12715 static struct dwo_unit *
12716 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
12717 ULONGEST signature, int is_debug_types)
12718 {
12719 const struct dwp_hash_table *dwp_htab =
12720 is_debug_types ? dwp_file->tus : dwp_file->cus;
12721 bfd *dbfd = dwp_file->dbfd;
12722 uint32_t mask = dwp_htab->nr_slots - 1;
12723 uint32_t hash = signature & mask;
12724 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12725 unsigned int i;
12726 void **slot;
12727 struct dwo_unit find_dwo_cu;
12728
12729 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12730 find_dwo_cu.signature = signature;
12731 slot = htab_find_slot (is_debug_types
12732 ? dwp_file->loaded_tus
12733 : dwp_file->loaded_cus,
12734 &find_dwo_cu, INSERT);
12735
12736 if (*slot != NULL)
12737 return (struct dwo_unit *) *slot;
12738
12739 /* Use a for loop so that we don't loop forever on bad debug info. */
12740 for (i = 0; i < dwp_htab->nr_slots; ++i)
12741 {
12742 ULONGEST signature_in_table;
12743
12744 signature_in_table =
12745 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12746 if (signature_in_table == signature)
12747 {
12748 uint32_t unit_index =
12749 read_4_bytes (dbfd,
12750 dwp_htab->unit_table + hash * sizeof (uint32_t));
12751
12752 if (dwp_file->version == 1)
12753 {
12754 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
12755 comp_dir, signature,
12756 is_debug_types);
12757 }
12758 else
12759 {
12760 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
12761 comp_dir, signature,
12762 is_debug_types);
12763 }
12764 return (struct dwo_unit *) *slot;
12765 }
12766 if (signature_in_table == 0)
12767 return NULL;
12768 hash = (hash + hash2) & mask;
12769 }
12770
12771 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12772 " [in module %s]"),
12773 dwp_file->name);
12774 }
12775
12776 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12777 Open the file specified by FILE_NAME and hand it off to BFD for
12778 preliminary analysis. Return a newly initialized bfd *, which
12779 includes a canonicalized copy of FILE_NAME.
12780 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12781 SEARCH_CWD is true if the current directory is to be searched.
12782 It will be searched before debug-file-directory.
12783 If successful, the file is added to the bfd include table of the
12784 objfile's bfd (see gdb_bfd_record_inclusion).
12785 If unable to find/open the file, return NULL.
12786 NOTE: This function is derived from symfile_bfd_open. */
12787
12788 static gdb_bfd_ref_ptr
12789 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
12790 {
12791 int desc, flags;
12792 char *absolute_name;
12793 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12794 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12795 to debug_file_directory. */
12796 char *search_path;
12797 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12798
12799 if (search_cwd)
12800 {
12801 if (*debug_file_directory != '\0')
12802 search_path = concat (".", dirname_separator_string,
12803 debug_file_directory, (char *) NULL);
12804 else
12805 search_path = xstrdup (".");
12806 }
12807 else
12808 search_path = xstrdup (debug_file_directory);
12809
12810 flags = OPF_RETURN_REALPATH;
12811 if (is_dwp)
12812 flags |= OPF_SEARCH_IN_PATH;
12813 desc = openp (search_path, flags, file_name,
12814 O_RDONLY | O_BINARY, &absolute_name);
12815 xfree (search_path);
12816 if (desc < 0)
12817 return NULL;
12818
12819 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
12820 xfree (absolute_name);
12821 if (sym_bfd == NULL)
12822 return NULL;
12823 bfd_set_cacheable (sym_bfd.get (), 1);
12824
12825 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12826 return NULL;
12827
12828 /* Success. Record the bfd as having been included by the objfile's bfd.
12829 This is important because things like demangled_names_hash lives in the
12830 objfile's per_bfd space and may have references to things like symbol
12831 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12832 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12833
12834 return sym_bfd;
12835 }
12836
12837 /* Try to open DWO file FILE_NAME.
12838 COMP_DIR is the DW_AT_comp_dir attribute.
12839 The result is the bfd handle of the file.
12840 If there is a problem finding or opening the file, return NULL.
12841 Upon success, the canonicalized path of the file is stored in the bfd,
12842 same as symfile_bfd_open. */
12843
12844 static gdb_bfd_ref_ptr
12845 open_dwo_file (const char *file_name, const char *comp_dir)
12846 {
12847 if (IS_ABSOLUTE_PATH (file_name))
12848 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
12849
12850 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12851
12852 if (comp_dir != NULL)
12853 {
12854 char *path_to_try = concat (comp_dir, SLASH_STRING,
12855 file_name, (char *) NULL);
12856
12857 /* NOTE: If comp_dir is a relative path, this will also try the
12858 search path, which seems useful. */
12859 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
12860 1 /*search_cwd*/));
12861 xfree (path_to_try);
12862 if (abfd != NULL)
12863 return abfd;
12864 }
12865
12866 /* That didn't work, try debug-file-directory, which, despite its name,
12867 is a list of paths. */
12868
12869 if (*debug_file_directory == '\0')
12870 return NULL;
12871
12872 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
12873 }
12874
12875 /* This function is mapped across the sections and remembers the offset and
12876 size of each of the DWO debugging sections we are interested in. */
12877
12878 static void
12879 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12880 {
12881 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12882 const struct dwop_section_names *names = &dwop_section_names;
12883
12884 if (section_is_p (sectp->name, &names->abbrev_dwo))
12885 {
12886 dwo_sections->abbrev.s.section = sectp;
12887 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12888 }
12889 else if (section_is_p (sectp->name, &names->info_dwo))
12890 {
12891 dwo_sections->info.s.section = sectp;
12892 dwo_sections->info.size = bfd_get_section_size (sectp);
12893 }
12894 else if (section_is_p (sectp->name, &names->line_dwo))
12895 {
12896 dwo_sections->line.s.section = sectp;
12897 dwo_sections->line.size = bfd_get_section_size (sectp);
12898 }
12899 else if (section_is_p (sectp->name, &names->loc_dwo))
12900 {
12901 dwo_sections->loc.s.section = sectp;
12902 dwo_sections->loc.size = bfd_get_section_size (sectp);
12903 }
12904 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12905 {
12906 dwo_sections->macinfo.s.section = sectp;
12907 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12908 }
12909 else if (section_is_p (sectp->name, &names->macro_dwo))
12910 {
12911 dwo_sections->macro.s.section = sectp;
12912 dwo_sections->macro.size = bfd_get_section_size (sectp);
12913 }
12914 else if (section_is_p (sectp->name, &names->str_dwo))
12915 {
12916 dwo_sections->str.s.section = sectp;
12917 dwo_sections->str.size = bfd_get_section_size (sectp);
12918 }
12919 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12920 {
12921 dwo_sections->str_offsets.s.section = sectp;
12922 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12923 }
12924 else if (section_is_p (sectp->name, &names->types_dwo))
12925 {
12926 struct dwarf2_section_info type_section;
12927
12928 memset (&type_section, 0, sizeof (type_section));
12929 type_section.s.section = sectp;
12930 type_section.size = bfd_get_section_size (sectp);
12931 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12932 &type_section);
12933 }
12934 }
12935
12936 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12937 by PER_CU. This is for the non-DWP case.
12938 The result is NULL if DWO_NAME can't be found. */
12939
12940 static struct dwo_file *
12941 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12942 const char *dwo_name, const char *comp_dir)
12943 {
12944 struct objfile *objfile = dwarf2_per_objfile->objfile;
12945 struct dwo_file *dwo_file;
12946 struct cleanup *cleanups;
12947
12948 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
12949 if (dbfd == NULL)
12950 {
12951 if (dwarf_read_debug)
12952 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12953 return NULL;
12954 }
12955 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12956 dwo_file->dwo_name = dwo_name;
12957 dwo_file->comp_dir = comp_dir;
12958 dwo_file->dbfd = dbfd.release ();
12959
12960 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
12961
12962 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
12963 &dwo_file->sections);
12964
12965 create_cus_hash_table (*dwo_file, dwo_file->sections.info, dwo_file->cus);
12966
12967 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
12968 dwo_file->tus);
12969
12970 discard_cleanups (cleanups);
12971
12972 if (dwarf_read_debug)
12973 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12974
12975 return dwo_file;
12976 }
12977
12978 /* This function is mapped across the sections and remembers the offset and
12979 size of each of the DWP debugging sections common to version 1 and 2 that
12980 we are interested in. */
12981
12982 static void
12983 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12984 void *dwp_file_ptr)
12985 {
12986 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12987 const struct dwop_section_names *names = &dwop_section_names;
12988 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12989
12990 /* Record the ELF section number for later lookup: this is what the
12991 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12992 gdb_assert (elf_section_nr < dwp_file->num_sections);
12993 dwp_file->elf_sections[elf_section_nr] = sectp;
12994
12995 /* Look for specific sections that we need. */
12996 if (section_is_p (sectp->name, &names->str_dwo))
12997 {
12998 dwp_file->sections.str.s.section = sectp;
12999 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13000 }
13001 else if (section_is_p (sectp->name, &names->cu_index))
13002 {
13003 dwp_file->sections.cu_index.s.section = sectp;
13004 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13005 }
13006 else if (section_is_p (sectp->name, &names->tu_index))
13007 {
13008 dwp_file->sections.tu_index.s.section = sectp;
13009 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13010 }
13011 }
13012
13013 /* This function is mapped across the sections and remembers the offset and
13014 size of each of the DWP version 2 debugging sections that we are interested
13015 in. This is split into a separate function because we don't know if we
13016 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13017
13018 static void
13019 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13020 {
13021 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13022 const struct dwop_section_names *names = &dwop_section_names;
13023 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13024
13025 /* Record the ELF section number for later lookup: this is what the
13026 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13027 gdb_assert (elf_section_nr < dwp_file->num_sections);
13028 dwp_file->elf_sections[elf_section_nr] = sectp;
13029
13030 /* Look for specific sections that we need. */
13031 if (section_is_p (sectp->name, &names->abbrev_dwo))
13032 {
13033 dwp_file->sections.abbrev.s.section = sectp;
13034 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13035 }
13036 else if (section_is_p (sectp->name, &names->info_dwo))
13037 {
13038 dwp_file->sections.info.s.section = sectp;
13039 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13040 }
13041 else if (section_is_p (sectp->name, &names->line_dwo))
13042 {
13043 dwp_file->sections.line.s.section = sectp;
13044 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13045 }
13046 else if (section_is_p (sectp->name, &names->loc_dwo))
13047 {
13048 dwp_file->sections.loc.s.section = sectp;
13049 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13050 }
13051 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13052 {
13053 dwp_file->sections.macinfo.s.section = sectp;
13054 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13055 }
13056 else if (section_is_p (sectp->name, &names->macro_dwo))
13057 {
13058 dwp_file->sections.macro.s.section = sectp;
13059 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13060 }
13061 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13062 {
13063 dwp_file->sections.str_offsets.s.section = sectp;
13064 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13065 }
13066 else if (section_is_p (sectp->name, &names->types_dwo))
13067 {
13068 dwp_file->sections.types.s.section = sectp;
13069 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13070 }
13071 }
13072
13073 /* Hash function for dwp_file loaded CUs/TUs. */
13074
13075 static hashval_t
13076 hash_dwp_loaded_cutus (const void *item)
13077 {
13078 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13079
13080 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13081 return dwo_unit->signature;
13082 }
13083
13084 /* Equality function for dwp_file loaded CUs/TUs. */
13085
13086 static int
13087 eq_dwp_loaded_cutus (const void *a, const void *b)
13088 {
13089 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13090 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13091
13092 return dua->signature == dub->signature;
13093 }
13094
13095 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13096
13097 static htab_t
13098 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13099 {
13100 return htab_create_alloc_ex (3,
13101 hash_dwp_loaded_cutus,
13102 eq_dwp_loaded_cutus,
13103 NULL,
13104 &objfile->objfile_obstack,
13105 hashtab_obstack_allocate,
13106 dummy_obstack_deallocate);
13107 }
13108
13109 /* Try to open DWP file FILE_NAME.
13110 The result is the bfd handle of the file.
13111 If there is a problem finding or opening the file, return NULL.
13112 Upon success, the canonicalized path of the file is stored in the bfd,
13113 same as symfile_bfd_open. */
13114
13115 static gdb_bfd_ref_ptr
13116 open_dwp_file (const char *file_name)
13117 {
13118 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
13119 1 /*search_cwd*/));
13120 if (abfd != NULL)
13121 return abfd;
13122
13123 /* Work around upstream bug 15652.
13124 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13125 [Whether that's a "bug" is debatable, but it is getting in our way.]
13126 We have no real idea where the dwp file is, because gdb's realpath-ing
13127 of the executable's path may have discarded the needed info.
13128 [IWBN if the dwp file name was recorded in the executable, akin to
13129 .gnu_debuglink, but that doesn't exist yet.]
13130 Strip the directory from FILE_NAME and search again. */
13131 if (*debug_file_directory != '\0')
13132 {
13133 /* Don't implicitly search the current directory here.
13134 If the user wants to search "." to handle this case,
13135 it must be added to debug-file-directory. */
13136 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
13137 0 /*search_cwd*/);
13138 }
13139
13140 return NULL;
13141 }
13142
13143 /* Initialize the use of the DWP file for the current objfile.
13144 By convention the name of the DWP file is ${objfile}.dwp.
13145 The result is NULL if it can't be found. */
13146
13147 static struct dwp_file *
13148 open_and_init_dwp_file (void)
13149 {
13150 struct objfile *objfile = dwarf2_per_objfile->objfile;
13151 struct dwp_file *dwp_file;
13152
13153 /* Try to find first .dwp for the binary file before any symbolic links
13154 resolving. */
13155
13156 /* If the objfile is a debug file, find the name of the real binary
13157 file and get the name of dwp file from there. */
13158 std::string dwp_name;
13159 if (objfile->separate_debug_objfile_backlink != NULL)
13160 {
13161 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13162 const char *backlink_basename = lbasename (backlink->original_name);
13163
13164 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13165 }
13166 else
13167 dwp_name = objfile->original_name;
13168
13169 dwp_name += ".dwp";
13170
13171 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
13172 if (dbfd == NULL
13173 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13174 {
13175 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13176 dwp_name = objfile_name (objfile);
13177 dwp_name += ".dwp";
13178 dbfd = open_dwp_file (dwp_name.c_str ());
13179 }
13180
13181 if (dbfd == NULL)
13182 {
13183 if (dwarf_read_debug)
13184 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13185 return NULL;
13186 }
13187 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
13188 dwp_file->name = bfd_get_filename (dbfd.get ());
13189 dwp_file->dbfd = dbfd.release ();
13190
13191 /* +1: section 0 is unused */
13192 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13193 dwp_file->elf_sections =
13194 OBSTACK_CALLOC (&objfile->objfile_obstack,
13195 dwp_file->num_sections, asection *);
13196
13197 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13198 dwp_file);
13199
13200 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
13201
13202 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
13203
13204 /* The DWP file version is stored in the hash table. Oh well. */
13205 if (dwp_file->cus && dwp_file->tus
13206 && dwp_file->cus->version != dwp_file->tus->version)
13207 {
13208 /* Technically speaking, we should try to limp along, but this is
13209 pretty bizarre. We use pulongest here because that's the established
13210 portability solution (e.g, we cannot use %u for uint32_t). */
13211 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13212 " TU version %s [in DWP file %s]"),
13213 pulongest (dwp_file->cus->version),
13214 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13215 }
13216
13217 if (dwp_file->cus)
13218 dwp_file->version = dwp_file->cus->version;
13219 else if (dwp_file->tus)
13220 dwp_file->version = dwp_file->tus->version;
13221 else
13222 dwp_file->version = 2;
13223
13224 if (dwp_file->version == 2)
13225 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13226 dwp_file);
13227
13228 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13229 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13230
13231 if (dwarf_read_debug)
13232 {
13233 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13234 fprintf_unfiltered (gdb_stdlog,
13235 " %s CUs, %s TUs\n",
13236 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13237 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13238 }
13239
13240 return dwp_file;
13241 }
13242
13243 /* Wrapper around open_and_init_dwp_file, only open it once. */
13244
13245 static struct dwp_file *
13246 get_dwp_file (void)
13247 {
13248 if (! dwarf2_per_objfile->dwp_checked)
13249 {
13250 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
13251 dwarf2_per_objfile->dwp_checked = 1;
13252 }
13253 return dwarf2_per_objfile->dwp_file;
13254 }
13255
13256 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13257 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13258 or in the DWP file for the objfile, referenced by THIS_UNIT.
13259 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13260 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13261
13262 This is called, for example, when wanting to read a variable with a
13263 complex location. Therefore we don't want to do file i/o for every call.
13264 Therefore we don't want to look for a DWO file on every call.
13265 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13266 then we check if we've already seen DWO_NAME, and only THEN do we check
13267 for a DWO file.
13268
13269 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13270 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13271
13272 static struct dwo_unit *
13273 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13274 const char *dwo_name, const char *comp_dir,
13275 ULONGEST signature, int is_debug_types)
13276 {
13277 struct objfile *objfile = dwarf2_per_objfile->objfile;
13278 const char *kind = is_debug_types ? "TU" : "CU";
13279 void **dwo_file_slot;
13280 struct dwo_file *dwo_file;
13281 struct dwp_file *dwp_file;
13282
13283 /* First see if there's a DWP file.
13284 If we have a DWP file but didn't find the DWO inside it, don't
13285 look for the original DWO file. It makes gdb behave differently
13286 depending on whether one is debugging in the build tree. */
13287
13288 dwp_file = get_dwp_file ();
13289 if (dwp_file != NULL)
13290 {
13291 const struct dwp_hash_table *dwp_htab =
13292 is_debug_types ? dwp_file->tus : dwp_file->cus;
13293
13294 if (dwp_htab != NULL)
13295 {
13296 struct dwo_unit *dwo_cutu =
13297 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
13298 signature, is_debug_types);
13299
13300 if (dwo_cutu != NULL)
13301 {
13302 if (dwarf_read_debug)
13303 {
13304 fprintf_unfiltered (gdb_stdlog,
13305 "Virtual DWO %s %s found: @%s\n",
13306 kind, hex_string (signature),
13307 host_address_to_string (dwo_cutu));
13308 }
13309 return dwo_cutu;
13310 }
13311 }
13312 }
13313 else
13314 {
13315 /* No DWP file, look for the DWO file. */
13316
13317 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
13318 if (*dwo_file_slot == NULL)
13319 {
13320 /* Read in the file and build a table of the CUs/TUs it contains. */
13321 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13322 }
13323 /* NOTE: This will be NULL if unable to open the file. */
13324 dwo_file = (struct dwo_file *) *dwo_file_slot;
13325
13326 if (dwo_file != NULL)
13327 {
13328 struct dwo_unit *dwo_cutu = NULL;
13329
13330 if (is_debug_types && dwo_file->tus)
13331 {
13332 struct dwo_unit find_dwo_cutu;
13333
13334 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13335 find_dwo_cutu.signature = signature;
13336 dwo_cutu
13337 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13338 }
13339 else if (!is_debug_types && dwo_file->cus)
13340 {
13341 struct dwo_unit find_dwo_cutu;
13342
13343 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13344 find_dwo_cutu.signature = signature;
13345 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13346 &find_dwo_cutu);
13347 }
13348
13349 if (dwo_cutu != NULL)
13350 {
13351 if (dwarf_read_debug)
13352 {
13353 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13354 kind, dwo_name, hex_string (signature),
13355 host_address_to_string (dwo_cutu));
13356 }
13357 return dwo_cutu;
13358 }
13359 }
13360 }
13361
13362 /* We didn't find it. This could mean a dwo_id mismatch, or
13363 someone deleted the DWO/DWP file, or the search path isn't set up
13364 correctly to find the file. */
13365
13366 if (dwarf_read_debug)
13367 {
13368 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13369 kind, dwo_name, hex_string (signature));
13370 }
13371
13372 /* This is a warning and not a complaint because it can be caused by
13373 pilot error (e.g., user accidentally deleting the DWO). */
13374 {
13375 /* Print the name of the DWP file if we looked there, helps the user
13376 better diagnose the problem. */
13377 std::string dwp_text;
13378
13379 if (dwp_file != NULL)
13380 dwp_text = string_printf (" [in DWP file %s]",
13381 lbasename (dwp_file->name));
13382
13383 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
13384 " [in module %s]"),
13385 kind, dwo_name, hex_string (signature),
13386 dwp_text.c_str (),
13387 this_unit->is_debug_types ? "TU" : "CU",
13388 to_underlying (this_unit->sect_off), objfile_name (objfile));
13389 }
13390 return NULL;
13391 }
13392
13393 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13394 See lookup_dwo_cutu_unit for details. */
13395
13396 static struct dwo_unit *
13397 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13398 const char *dwo_name, const char *comp_dir,
13399 ULONGEST signature)
13400 {
13401 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13402 }
13403
13404 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13405 See lookup_dwo_cutu_unit for details. */
13406
13407 static struct dwo_unit *
13408 lookup_dwo_type_unit (struct signatured_type *this_tu,
13409 const char *dwo_name, const char *comp_dir)
13410 {
13411 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13412 }
13413
13414 /* Traversal function for queue_and_load_all_dwo_tus. */
13415
13416 static int
13417 queue_and_load_dwo_tu (void **slot, void *info)
13418 {
13419 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13420 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13421 ULONGEST signature = dwo_unit->signature;
13422 struct signatured_type *sig_type =
13423 lookup_dwo_signatured_type (per_cu->cu, signature);
13424
13425 if (sig_type != NULL)
13426 {
13427 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13428
13429 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13430 a real dependency of PER_CU on SIG_TYPE. That is detected later
13431 while processing PER_CU. */
13432 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13433 load_full_type_unit (sig_cu);
13434 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13435 }
13436
13437 return 1;
13438 }
13439
13440 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13441 The DWO may have the only definition of the type, though it may not be
13442 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13443 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13444
13445 static void
13446 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13447 {
13448 struct dwo_unit *dwo_unit;
13449 struct dwo_file *dwo_file;
13450
13451 gdb_assert (!per_cu->is_debug_types);
13452 gdb_assert (get_dwp_file () == NULL);
13453 gdb_assert (per_cu->cu != NULL);
13454
13455 dwo_unit = per_cu->cu->dwo_unit;
13456 gdb_assert (dwo_unit != NULL);
13457
13458 dwo_file = dwo_unit->dwo_file;
13459 if (dwo_file->tus != NULL)
13460 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13461 }
13462
13463 /* Free all resources associated with DWO_FILE.
13464 Close the DWO file and munmap the sections.
13465 All memory should be on the objfile obstack. */
13466
13467 static void
13468 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
13469 {
13470
13471 /* Note: dbfd is NULL for virtual DWO files. */
13472 gdb_bfd_unref (dwo_file->dbfd);
13473
13474 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13475 }
13476
13477 /* Wrapper for free_dwo_file for use in cleanups. */
13478
13479 static void
13480 free_dwo_file_cleanup (void *arg)
13481 {
13482 struct dwo_file *dwo_file = (struct dwo_file *) arg;
13483 struct objfile *objfile = dwarf2_per_objfile->objfile;
13484
13485 free_dwo_file (dwo_file, objfile);
13486 }
13487
13488 /* Traversal function for free_dwo_files. */
13489
13490 static int
13491 free_dwo_file_from_slot (void **slot, void *info)
13492 {
13493 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13494 struct objfile *objfile = (struct objfile *) info;
13495
13496 free_dwo_file (dwo_file, objfile);
13497
13498 return 1;
13499 }
13500
13501 /* Free all resources associated with DWO_FILES. */
13502
13503 static void
13504 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13505 {
13506 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13507 }
13508 \f
13509 /* Read in various DIEs. */
13510
13511 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13512 Inherit only the children of the DW_AT_abstract_origin DIE not being
13513 already referenced by DW_AT_abstract_origin from the children of the
13514 current DIE. */
13515
13516 static void
13517 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13518 {
13519 struct die_info *child_die;
13520 sect_offset *offsetp;
13521 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13522 struct die_info *origin_die;
13523 /* Iterator of the ORIGIN_DIE children. */
13524 struct die_info *origin_child_die;
13525 struct attribute *attr;
13526 struct dwarf2_cu *origin_cu;
13527 struct pending **origin_previous_list_in_scope;
13528
13529 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13530 if (!attr)
13531 return;
13532
13533 /* Note that following die references may follow to a die in a
13534 different cu. */
13535
13536 origin_cu = cu;
13537 origin_die = follow_die_ref (die, attr, &origin_cu);
13538
13539 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13540 symbols in. */
13541 origin_previous_list_in_scope = origin_cu->list_in_scope;
13542 origin_cu->list_in_scope = cu->list_in_scope;
13543
13544 if (die->tag != origin_die->tag
13545 && !(die->tag == DW_TAG_inlined_subroutine
13546 && origin_die->tag == DW_TAG_subprogram))
13547 complaint (&symfile_complaints,
13548 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
13549 to_underlying (die->sect_off),
13550 to_underlying (origin_die->sect_off));
13551
13552 std::vector<sect_offset> offsets;
13553
13554 for (child_die = die->child;
13555 child_die && child_die->tag;
13556 child_die = sibling_die (child_die))
13557 {
13558 struct die_info *child_origin_die;
13559 struct dwarf2_cu *child_origin_cu;
13560
13561 /* We are trying to process concrete instance entries:
13562 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13563 it's not relevant to our analysis here. i.e. detecting DIEs that are
13564 present in the abstract instance but not referenced in the concrete
13565 one. */
13566 if (child_die->tag == DW_TAG_call_site
13567 || child_die->tag == DW_TAG_GNU_call_site)
13568 continue;
13569
13570 /* For each CHILD_DIE, find the corresponding child of
13571 ORIGIN_DIE. If there is more than one layer of
13572 DW_AT_abstract_origin, follow them all; there shouldn't be,
13573 but GCC versions at least through 4.4 generate this (GCC PR
13574 40573). */
13575 child_origin_die = child_die;
13576 child_origin_cu = cu;
13577 while (1)
13578 {
13579 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13580 child_origin_cu);
13581 if (attr == NULL)
13582 break;
13583 child_origin_die = follow_die_ref (child_origin_die, attr,
13584 &child_origin_cu);
13585 }
13586
13587 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13588 counterpart may exist. */
13589 if (child_origin_die != child_die)
13590 {
13591 if (child_die->tag != child_origin_die->tag
13592 && !(child_die->tag == DW_TAG_inlined_subroutine
13593 && child_origin_die->tag == DW_TAG_subprogram))
13594 complaint (&symfile_complaints,
13595 _("Child DIE 0x%x and its abstract origin 0x%x have "
13596 "different tags"),
13597 to_underlying (child_die->sect_off),
13598 to_underlying (child_origin_die->sect_off));
13599 if (child_origin_die->parent != origin_die)
13600 complaint (&symfile_complaints,
13601 _("Child DIE 0x%x and its abstract origin 0x%x have "
13602 "different parents"),
13603 to_underlying (child_die->sect_off),
13604 to_underlying (child_origin_die->sect_off));
13605 else
13606 offsets.push_back (child_origin_die->sect_off);
13607 }
13608 }
13609 std::sort (offsets.begin (), offsets.end ());
13610 sect_offset *offsets_end = offsets.data () + offsets.size ();
13611 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13612 if (offsetp[-1] == *offsetp)
13613 complaint (&symfile_complaints,
13614 _("Multiple children of DIE 0x%x refer "
13615 "to DIE 0x%x as their abstract origin"),
13616 to_underlying (die->sect_off), to_underlying (*offsetp));
13617
13618 offsetp = offsets.data ();
13619 origin_child_die = origin_die->child;
13620 while (origin_child_die && origin_child_die->tag)
13621 {
13622 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13623 while (offsetp < offsets_end
13624 && *offsetp < origin_child_die->sect_off)
13625 offsetp++;
13626 if (offsetp >= offsets_end
13627 || *offsetp > origin_child_die->sect_off)
13628 {
13629 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13630 Check whether we're already processing ORIGIN_CHILD_DIE.
13631 This can happen with mutually referenced abstract_origins.
13632 PR 16581. */
13633 if (!origin_child_die->in_process)
13634 process_die (origin_child_die, origin_cu);
13635 }
13636 origin_child_die = sibling_die (origin_child_die);
13637 }
13638 origin_cu->list_in_scope = origin_previous_list_in_scope;
13639 }
13640
13641 static void
13642 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13643 {
13644 struct objfile *objfile = cu->objfile;
13645 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13646 struct context_stack *newobj;
13647 CORE_ADDR lowpc;
13648 CORE_ADDR highpc;
13649 struct die_info *child_die;
13650 struct attribute *attr, *call_line, *call_file;
13651 const char *name;
13652 CORE_ADDR baseaddr;
13653 struct block *block;
13654 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13655 std::vector<struct symbol *> template_args;
13656 struct template_symbol *templ_func = NULL;
13657
13658 if (inlined_func)
13659 {
13660 /* If we do not have call site information, we can't show the
13661 caller of this inlined function. That's too confusing, so
13662 only use the scope for local variables. */
13663 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13664 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13665 if (call_line == NULL || call_file == NULL)
13666 {
13667 read_lexical_block_scope (die, cu);
13668 return;
13669 }
13670 }
13671
13672 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13673
13674 name = dwarf2_name (die, cu);
13675
13676 /* Ignore functions with missing or empty names. These are actually
13677 illegal according to the DWARF standard. */
13678 if (name == NULL)
13679 {
13680 complaint (&symfile_complaints,
13681 _("missing name for subprogram DIE at %d"),
13682 to_underlying (die->sect_off));
13683 return;
13684 }
13685
13686 /* Ignore functions with missing or invalid low and high pc attributes. */
13687 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13688 <= PC_BOUNDS_INVALID)
13689 {
13690 attr = dwarf2_attr (die, DW_AT_external, cu);
13691 if (!attr || !DW_UNSND (attr))
13692 complaint (&symfile_complaints,
13693 _("cannot get low and high bounds "
13694 "for subprogram DIE at %d"),
13695 to_underlying (die->sect_off));
13696 return;
13697 }
13698
13699 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13700 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13701
13702 /* If we have any template arguments, then we must allocate a
13703 different sort of symbol. */
13704 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13705 {
13706 if (child_die->tag == DW_TAG_template_type_param
13707 || child_die->tag == DW_TAG_template_value_param)
13708 {
13709 templ_func = allocate_template_symbol (objfile);
13710 templ_func->subclass = SYMBOL_TEMPLATE;
13711 break;
13712 }
13713 }
13714
13715 newobj = push_context (0, lowpc);
13716 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
13717 (struct symbol *) templ_func);
13718
13719 /* If there is a location expression for DW_AT_frame_base, record
13720 it. */
13721 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13722 if (attr)
13723 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13724
13725 /* If there is a location for the static link, record it. */
13726 newobj->static_link = NULL;
13727 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13728 if (attr)
13729 {
13730 newobj->static_link
13731 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13732 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13733 }
13734
13735 cu->list_in_scope = &local_symbols;
13736
13737 if (die->child != NULL)
13738 {
13739 child_die = die->child;
13740 while (child_die && child_die->tag)
13741 {
13742 if (child_die->tag == DW_TAG_template_type_param
13743 || child_die->tag == DW_TAG_template_value_param)
13744 {
13745 struct symbol *arg = new_symbol (child_die, NULL, cu);
13746
13747 if (arg != NULL)
13748 template_args.push_back (arg);
13749 }
13750 else
13751 process_die (child_die, cu);
13752 child_die = sibling_die (child_die);
13753 }
13754 }
13755
13756 inherit_abstract_dies (die, cu);
13757
13758 /* If we have a DW_AT_specification, we might need to import using
13759 directives from the context of the specification DIE. See the
13760 comment in determine_prefix. */
13761 if (cu->language == language_cplus
13762 && dwarf2_attr (die, DW_AT_specification, cu))
13763 {
13764 struct dwarf2_cu *spec_cu = cu;
13765 struct die_info *spec_die = die_specification (die, &spec_cu);
13766
13767 while (spec_die)
13768 {
13769 child_die = spec_die->child;
13770 while (child_die && child_die->tag)
13771 {
13772 if (child_die->tag == DW_TAG_imported_module)
13773 process_die (child_die, spec_cu);
13774 child_die = sibling_die (child_die);
13775 }
13776
13777 /* In some cases, GCC generates specification DIEs that
13778 themselves contain DW_AT_specification attributes. */
13779 spec_die = die_specification (spec_die, &spec_cu);
13780 }
13781 }
13782
13783 newobj = pop_context ();
13784 /* Make a block for the local symbols within. */
13785 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
13786 newobj->static_link, lowpc, highpc);
13787
13788 /* For C++, set the block's scope. */
13789 if ((cu->language == language_cplus
13790 || cu->language == language_fortran
13791 || cu->language == language_d
13792 || cu->language == language_rust)
13793 && cu->processing_has_namespace_info)
13794 block_set_scope (block, determine_prefix (die, cu),
13795 &objfile->objfile_obstack);
13796
13797 /* If we have address ranges, record them. */
13798 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13799
13800 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
13801
13802 /* Attach template arguments to function. */
13803 if (!template_args.empty ())
13804 {
13805 gdb_assert (templ_func != NULL);
13806
13807 templ_func->n_template_arguments = template_args.size ();
13808 templ_func->template_arguments
13809 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13810 templ_func->n_template_arguments);
13811 memcpy (templ_func->template_arguments,
13812 template_args.data (),
13813 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13814 }
13815
13816 /* In C++, we can have functions nested inside functions (e.g., when
13817 a function declares a class that has methods). This means that
13818 when we finish processing a function scope, we may need to go
13819 back to building a containing block's symbol lists. */
13820 local_symbols = newobj->locals;
13821 local_using_directives = newobj->local_using_directives;
13822
13823 /* If we've finished processing a top-level function, subsequent
13824 symbols go in the file symbol list. */
13825 if (outermost_context_p ())
13826 cu->list_in_scope = &file_symbols;
13827 }
13828
13829 /* Process all the DIES contained within a lexical block scope. Start
13830 a new scope, process the dies, and then close the scope. */
13831
13832 static void
13833 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13834 {
13835 struct objfile *objfile = cu->objfile;
13836 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13837 struct context_stack *newobj;
13838 CORE_ADDR lowpc, highpc;
13839 struct die_info *child_die;
13840 CORE_ADDR baseaddr;
13841
13842 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13843
13844 /* Ignore blocks with missing or invalid low and high pc attributes. */
13845 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13846 as multiple lexical blocks? Handling children in a sane way would
13847 be nasty. Might be easier to properly extend generic blocks to
13848 describe ranges. */
13849 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13850 {
13851 case PC_BOUNDS_NOT_PRESENT:
13852 /* DW_TAG_lexical_block has no attributes, process its children as if
13853 there was no wrapping by that DW_TAG_lexical_block.
13854 GCC does no longer produces such DWARF since GCC r224161. */
13855 for (child_die = die->child;
13856 child_die != NULL && child_die->tag;
13857 child_die = sibling_die (child_die))
13858 process_die (child_die, cu);
13859 return;
13860 case PC_BOUNDS_INVALID:
13861 return;
13862 }
13863 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13864 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13865
13866 push_context (0, lowpc);
13867 if (die->child != NULL)
13868 {
13869 child_die = die->child;
13870 while (child_die && child_die->tag)
13871 {
13872 process_die (child_die, cu);
13873 child_die = sibling_die (child_die);
13874 }
13875 }
13876 inherit_abstract_dies (die, cu);
13877 newobj = pop_context ();
13878
13879 if (local_symbols != NULL || local_using_directives != NULL)
13880 {
13881 struct block *block
13882 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
13883 newobj->start_addr, highpc);
13884
13885 /* Note that recording ranges after traversing children, as we
13886 do here, means that recording a parent's ranges entails
13887 walking across all its children's ranges as they appear in
13888 the address map, which is quadratic behavior.
13889
13890 It would be nicer to record the parent's ranges before
13891 traversing its children, simply overriding whatever you find
13892 there. But since we don't even decide whether to create a
13893 block until after we've traversed its children, that's hard
13894 to do. */
13895 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13896 }
13897 local_symbols = newobj->locals;
13898 local_using_directives = newobj->local_using_directives;
13899 }
13900
13901 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13902
13903 static void
13904 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13905 {
13906 struct objfile *objfile = cu->objfile;
13907 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13908 CORE_ADDR pc, baseaddr;
13909 struct attribute *attr;
13910 struct call_site *call_site, call_site_local;
13911 void **slot;
13912 int nparams;
13913 struct die_info *child_die;
13914
13915 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13916
13917 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13918 if (attr == NULL)
13919 {
13920 /* This was a pre-DWARF-5 GNU extension alias
13921 for DW_AT_call_return_pc. */
13922 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13923 }
13924 if (!attr)
13925 {
13926 complaint (&symfile_complaints,
13927 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
13928 "DIE 0x%x [in module %s]"),
13929 to_underlying (die->sect_off), objfile_name (objfile));
13930 return;
13931 }
13932 pc = attr_value_as_address (attr) + baseaddr;
13933 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13934
13935 if (cu->call_site_htab == NULL)
13936 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13937 NULL, &objfile->objfile_obstack,
13938 hashtab_obstack_allocate, NULL);
13939 call_site_local.pc = pc;
13940 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13941 if (*slot != NULL)
13942 {
13943 complaint (&symfile_complaints,
13944 _("Duplicate PC %s for DW_TAG_call_site "
13945 "DIE 0x%x [in module %s]"),
13946 paddress (gdbarch, pc), to_underlying (die->sect_off),
13947 objfile_name (objfile));
13948 return;
13949 }
13950
13951 /* Count parameters at the caller. */
13952
13953 nparams = 0;
13954 for (child_die = die->child; child_die && child_die->tag;
13955 child_die = sibling_die (child_die))
13956 {
13957 if (child_die->tag != DW_TAG_call_site_parameter
13958 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13959 {
13960 complaint (&symfile_complaints,
13961 _("Tag %d is not DW_TAG_call_site_parameter in "
13962 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
13963 child_die->tag, to_underlying (child_die->sect_off),
13964 objfile_name (objfile));
13965 continue;
13966 }
13967
13968 nparams++;
13969 }
13970
13971 call_site
13972 = ((struct call_site *)
13973 obstack_alloc (&objfile->objfile_obstack,
13974 sizeof (*call_site)
13975 + (sizeof (*call_site->parameter) * (nparams - 1))));
13976 *slot = call_site;
13977 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13978 call_site->pc = pc;
13979
13980 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13981 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13982 {
13983 struct die_info *func_die;
13984
13985 /* Skip also over DW_TAG_inlined_subroutine. */
13986 for (func_die = die->parent;
13987 func_die && func_die->tag != DW_TAG_subprogram
13988 && func_die->tag != DW_TAG_subroutine_type;
13989 func_die = func_die->parent);
13990
13991 /* DW_AT_call_all_calls is a superset
13992 of DW_AT_call_all_tail_calls. */
13993 if (func_die
13994 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13995 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13996 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13997 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13998 {
13999 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14000 not complete. But keep CALL_SITE for look ups via call_site_htab,
14001 both the initial caller containing the real return address PC and
14002 the final callee containing the current PC of a chain of tail
14003 calls do not need to have the tail call list complete. But any
14004 function candidate for a virtual tail call frame searched via
14005 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14006 determined unambiguously. */
14007 }
14008 else
14009 {
14010 struct type *func_type = NULL;
14011
14012 if (func_die)
14013 func_type = get_die_type (func_die, cu);
14014 if (func_type != NULL)
14015 {
14016 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14017
14018 /* Enlist this call site to the function. */
14019 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14020 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14021 }
14022 else
14023 complaint (&symfile_complaints,
14024 _("Cannot find function owning DW_TAG_call_site "
14025 "DIE 0x%x [in module %s]"),
14026 to_underlying (die->sect_off), objfile_name (objfile));
14027 }
14028 }
14029
14030 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14031 if (attr == NULL)
14032 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14033 if (attr == NULL)
14034 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14035 if (attr == NULL)
14036 {
14037 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14038 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14039 }
14040 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14041 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14042 /* Keep NULL DWARF_BLOCK. */;
14043 else if (attr_form_is_block (attr))
14044 {
14045 struct dwarf2_locexpr_baton *dlbaton;
14046
14047 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14048 dlbaton->data = DW_BLOCK (attr)->data;
14049 dlbaton->size = DW_BLOCK (attr)->size;
14050 dlbaton->per_cu = cu->per_cu;
14051
14052 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14053 }
14054 else if (attr_form_is_ref (attr))
14055 {
14056 struct dwarf2_cu *target_cu = cu;
14057 struct die_info *target_die;
14058
14059 target_die = follow_die_ref (die, attr, &target_cu);
14060 gdb_assert (target_cu->objfile == objfile);
14061 if (die_is_declaration (target_die, target_cu))
14062 {
14063 const char *target_physname;
14064
14065 /* Prefer the mangled name; otherwise compute the demangled one. */
14066 target_physname = dw2_linkage_name (target_die, target_cu);
14067 if (target_physname == NULL)
14068 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14069 if (target_physname == NULL)
14070 complaint (&symfile_complaints,
14071 _("DW_AT_call_target target DIE has invalid "
14072 "physname, for referencing DIE 0x%x [in module %s]"),
14073 to_underlying (die->sect_off), objfile_name (objfile));
14074 else
14075 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14076 }
14077 else
14078 {
14079 CORE_ADDR lowpc;
14080
14081 /* DW_AT_entry_pc should be preferred. */
14082 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14083 <= PC_BOUNDS_INVALID)
14084 complaint (&symfile_complaints,
14085 _("DW_AT_call_target target DIE has invalid "
14086 "low pc, for referencing DIE 0x%x [in module %s]"),
14087 to_underlying (die->sect_off), objfile_name (objfile));
14088 else
14089 {
14090 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14091 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14092 }
14093 }
14094 }
14095 else
14096 complaint (&symfile_complaints,
14097 _("DW_TAG_call_site DW_AT_call_target is neither "
14098 "block nor reference, for DIE 0x%x [in module %s]"),
14099 to_underlying (die->sect_off), objfile_name (objfile));
14100
14101 call_site->per_cu = cu->per_cu;
14102
14103 for (child_die = die->child;
14104 child_die && child_die->tag;
14105 child_die = sibling_die (child_die))
14106 {
14107 struct call_site_parameter *parameter;
14108 struct attribute *loc, *origin;
14109
14110 if (child_die->tag != DW_TAG_call_site_parameter
14111 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14112 {
14113 /* Already printed the complaint above. */
14114 continue;
14115 }
14116
14117 gdb_assert (call_site->parameter_count < nparams);
14118 parameter = &call_site->parameter[call_site->parameter_count];
14119
14120 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14121 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14122 register is contained in DW_AT_call_value. */
14123
14124 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14125 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14126 if (origin == NULL)
14127 {
14128 /* This was a pre-DWARF-5 GNU extension alias
14129 for DW_AT_call_parameter. */
14130 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14131 }
14132 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14133 {
14134 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14135
14136 sect_offset sect_off
14137 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14138 if (!offset_in_cu_p (&cu->header, sect_off))
14139 {
14140 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14141 binding can be done only inside one CU. Such referenced DIE
14142 therefore cannot be even moved to DW_TAG_partial_unit. */
14143 complaint (&symfile_complaints,
14144 _("DW_AT_call_parameter offset is not in CU for "
14145 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14146 to_underlying (child_die->sect_off),
14147 objfile_name (objfile));
14148 continue;
14149 }
14150 parameter->u.param_cu_off
14151 = (cu_offset) (sect_off - cu->header.sect_off);
14152 }
14153 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14154 {
14155 complaint (&symfile_complaints,
14156 _("No DW_FORM_block* DW_AT_location for "
14157 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14158 to_underlying (child_die->sect_off), objfile_name (objfile));
14159 continue;
14160 }
14161 else
14162 {
14163 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14164 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14165 if (parameter->u.dwarf_reg != -1)
14166 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14167 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14168 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14169 &parameter->u.fb_offset))
14170 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14171 else
14172 {
14173 complaint (&symfile_complaints,
14174 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14175 "for DW_FORM_block* DW_AT_location is supported for "
14176 "DW_TAG_call_site child DIE 0x%x "
14177 "[in module %s]"),
14178 to_underlying (child_die->sect_off),
14179 objfile_name (objfile));
14180 continue;
14181 }
14182 }
14183
14184 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14185 if (attr == NULL)
14186 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14187 if (!attr_form_is_block (attr))
14188 {
14189 complaint (&symfile_complaints,
14190 _("No DW_FORM_block* DW_AT_call_value for "
14191 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14192 to_underlying (child_die->sect_off),
14193 objfile_name (objfile));
14194 continue;
14195 }
14196 parameter->value = DW_BLOCK (attr)->data;
14197 parameter->value_size = DW_BLOCK (attr)->size;
14198
14199 /* Parameters are not pre-cleared by memset above. */
14200 parameter->data_value = NULL;
14201 parameter->data_value_size = 0;
14202 call_site->parameter_count++;
14203
14204 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14205 if (attr == NULL)
14206 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14207 if (attr)
14208 {
14209 if (!attr_form_is_block (attr))
14210 complaint (&symfile_complaints,
14211 _("No DW_FORM_block* DW_AT_call_data_value for "
14212 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14213 to_underlying (child_die->sect_off),
14214 objfile_name (objfile));
14215 else
14216 {
14217 parameter->data_value = DW_BLOCK (attr)->data;
14218 parameter->data_value_size = DW_BLOCK (attr)->size;
14219 }
14220 }
14221 }
14222 }
14223
14224 /* Helper function for read_variable. If DIE represents a virtual
14225 table, then return the type of the concrete object that is
14226 associated with the virtual table. Otherwise, return NULL. */
14227
14228 static struct type *
14229 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14230 {
14231 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14232 if (attr == NULL)
14233 return NULL;
14234
14235 /* Find the type DIE. */
14236 struct die_info *type_die = NULL;
14237 struct dwarf2_cu *type_cu = cu;
14238
14239 if (attr_form_is_ref (attr))
14240 type_die = follow_die_ref (die, attr, &type_cu);
14241 if (type_die == NULL)
14242 return NULL;
14243
14244 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14245 return NULL;
14246 return die_containing_type (type_die, type_cu);
14247 }
14248
14249 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14250
14251 static void
14252 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14253 {
14254 struct rust_vtable_symbol *storage = NULL;
14255
14256 if (cu->language == language_rust)
14257 {
14258 struct type *containing_type = rust_containing_type (die, cu);
14259
14260 if (containing_type != NULL)
14261 {
14262 struct objfile *objfile = cu->objfile;
14263
14264 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14265 struct rust_vtable_symbol);
14266 initialize_objfile_symbol (storage);
14267 storage->concrete_type = containing_type;
14268 storage->subclass = SYMBOL_RUST_VTABLE;
14269 }
14270 }
14271
14272 new_symbol_full (die, NULL, cu, storage);
14273 }
14274
14275 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14276 reading .debug_rnglists.
14277 Callback's type should be:
14278 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14279 Return true if the attributes are present and valid, otherwise,
14280 return false. */
14281
14282 template <typename Callback>
14283 static bool
14284 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14285 Callback &&callback)
14286 {
14287 struct objfile *objfile = cu->objfile;
14288 bfd *obfd = objfile->obfd;
14289 /* Base address selection entry. */
14290 CORE_ADDR base;
14291 int found_base;
14292 const gdb_byte *buffer;
14293 CORE_ADDR baseaddr;
14294 bool overflow = false;
14295
14296 found_base = cu->base_known;
14297 base = cu->base_address;
14298
14299 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14300 if (offset >= dwarf2_per_objfile->rnglists.size)
14301 {
14302 complaint (&symfile_complaints,
14303 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14304 offset);
14305 return false;
14306 }
14307 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14308
14309 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14310
14311 while (1)
14312 {
14313 /* Initialize it due to a false compiler warning. */
14314 CORE_ADDR range_beginning = 0, range_end = 0;
14315 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14316 + dwarf2_per_objfile->rnglists.size);
14317 unsigned int bytes_read;
14318
14319 if (buffer == buf_end)
14320 {
14321 overflow = true;
14322 break;
14323 }
14324 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14325 switch (rlet)
14326 {
14327 case DW_RLE_end_of_list:
14328 break;
14329 case DW_RLE_base_address:
14330 if (buffer + cu->header.addr_size > buf_end)
14331 {
14332 overflow = true;
14333 break;
14334 }
14335 base = read_address (obfd, buffer, cu, &bytes_read);
14336 found_base = 1;
14337 buffer += bytes_read;
14338 break;
14339 case DW_RLE_start_length:
14340 if (buffer + cu->header.addr_size > buf_end)
14341 {
14342 overflow = true;
14343 break;
14344 }
14345 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14346 buffer += bytes_read;
14347 range_end = (range_beginning
14348 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14349 buffer += bytes_read;
14350 if (buffer > buf_end)
14351 {
14352 overflow = true;
14353 break;
14354 }
14355 break;
14356 case DW_RLE_offset_pair:
14357 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14358 buffer += bytes_read;
14359 if (buffer > buf_end)
14360 {
14361 overflow = true;
14362 break;
14363 }
14364 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14365 buffer += bytes_read;
14366 if (buffer > buf_end)
14367 {
14368 overflow = true;
14369 break;
14370 }
14371 break;
14372 case DW_RLE_start_end:
14373 if (buffer + 2 * cu->header.addr_size > buf_end)
14374 {
14375 overflow = true;
14376 break;
14377 }
14378 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14379 buffer += bytes_read;
14380 range_end = read_address (obfd, buffer, cu, &bytes_read);
14381 buffer += bytes_read;
14382 break;
14383 default:
14384 complaint (&symfile_complaints,
14385 _("Invalid .debug_rnglists data (no base address)"));
14386 return false;
14387 }
14388 if (rlet == DW_RLE_end_of_list || overflow)
14389 break;
14390 if (rlet == DW_RLE_base_address)
14391 continue;
14392
14393 if (!found_base)
14394 {
14395 /* We have no valid base address for the ranges
14396 data. */
14397 complaint (&symfile_complaints,
14398 _("Invalid .debug_rnglists data (no base address)"));
14399 return false;
14400 }
14401
14402 if (range_beginning > range_end)
14403 {
14404 /* Inverted range entries are invalid. */
14405 complaint (&symfile_complaints,
14406 _("Invalid .debug_rnglists data (inverted range)"));
14407 return false;
14408 }
14409
14410 /* Empty range entries have no effect. */
14411 if (range_beginning == range_end)
14412 continue;
14413
14414 range_beginning += base;
14415 range_end += base;
14416
14417 /* A not-uncommon case of bad debug info.
14418 Don't pollute the addrmap with bad data. */
14419 if (range_beginning + baseaddr == 0
14420 && !dwarf2_per_objfile->has_section_at_zero)
14421 {
14422 complaint (&symfile_complaints,
14423 _(".debug_rnglists entry has start address of zero"
14424 " [in module %s]"), objfile_name (objfile));
14425 continue;
14426 }
14427
14428 callback (range_beginning, range_end);
14429 }
14430
14431 if (overflow)
14432 {
14433 complaint (&symfile_complaints,
14434 _("Offset %d is not terminated "
14435 "for DW_AT_ranges attribute"),
14436 offset);
14437 return false;
14438 }
14439
14440 return true;
14441 }
14442
14443 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14444 Callback's type should be:
14445 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14446 Return 1 if the attributes are present and valid, otherwise, return 0. */
14447
14448 template <typename Callback>
14449 static int
14450 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14451 Callback &&callback)
14452 {
14453 struct objfile *objfile = cu->objfile;
14454 struct comp_unit_head *cu_header = &cu->header;
14455 bfd *obfd = objfile->obfd;
14456 unsigned int addr_size = cu_header->addr_size;
14457 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14458 /* Base address selection entry. */
14459 CORE_ADDR base;
14460 int found_base;
14461 unsigned int dummy;
14462 const gdb_byte *buffer;
14463 CORE_ADDR baseaddr;
14464
14465 if (cu_header->version >= 5)
14466 return dwarf2_rnglists_process (offset, cu, callback);
14467
14468 found_base = cu->base_known;
14469 base = cu->base_address;
14470
14471 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14472 if (offset >= dwarf2_per_objfile->ranges.size)
14473 {
14474 complaint (&symfile_complaints,
14475 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14476 offset);
14477 return 0;
14478 }
14479 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14480
14481 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14482
14483 while (1)
14484 {
14485 CORE_ADDR range_beginning, range_end;
14486
14487 range_beginning = read_address (obfd, buffer, cu, &dummy);
14488 buffer += addr_size;
14489 range_end = read_address (obfd, buffer, cu, &dummy);
14490 buffer += addr_size;
14491 offset += 2 * addr_size;
14492
14493 /* An end of list marker is a pair of zero addresses. */
14494 if (range_beginning == 0 && range_end == 0)
14495 /* Found the end of list entry. */
14496 break;
14497
14498 /* Each base address selection entry is a pair of 2 values.
14499 The first is the largest possible address, the second is
14500 the base address. Check for a base address here. */
14501 if ((range_beginning & mask) == mask)
14502 {
14503 /* If we found the largest possible address, then we already
14504 have the base address in range_end. */
14505 base = range_end;
14506 found_base = 1;
14507 continue;
14508 }
14509
14510 if (!found_base)
14511 {
14512 /* We have no valid base address for the ranges
14513 data. */
14514 complaint (&symfile_complaints,
14515 _("Invalid .debug_ranges data (no base address)"));
14516 return 0;
14517 }
14518
14519 if (range_beginning > range_end)
14520 {
14521 /* Inverted range entries are invalid. */
14522 complaint (&symfile_complaints,
14523 _("Invalid .debug_ranges data (inverted range)"));
14524 return 0;
14525 }
14526
14527 /* Empty range entries have no effect. */
14528 if (range_beginning == range_end)
14529 continue;
14530
14531 range_beginning += base;
14532 range_end += base;
14533
14534 /* A not-uncommon case of bad debug info.
14535 Don't pollute the addrmap with bad data. */
14536 if (range_beginning + baseaddr == 0
14537 && !dwarf2_per_objfile->has_section_at_zero)
14538 {
14539 complaint (&symfile_complaints,
14540 _(".debug_ranges entry has start address of zero"
14541 " [in module %s]"), objfile_name (objfile));
14542 continue;
14543 }
14544
14545 callback (range_beginning, range_end);
14546 }
14547
14548 return 1;
14549 }
14550
14551 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14552 Return 1 if the attributes are present and valid, otherwise, return 0.
14553 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14554
14555 static int
14556 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14557 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14558 struct partial_symtab *ranges_pst)
14559 {
14560 struct objfile *objfile = cu->objfile;
14561 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14562 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14563 SECT_OFF_TEXT (objfile));
14564 int low_set = 0;
14565 CORE_ADDR low = 0;
14566 CORE_ADDR high = 0;
14567 int retval;
14568
14569 retval = dwarf2_ranges_process (offset, cu,
14570 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14571 {
14572 if (ranges_pst != NULL)
14573 {
14574 CORE_ADDR lowpc;
14575 CORE_ADDR highpc;
14576
14577 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14578 range_beginning + baseaddr);
14579 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14580 range_end + baseaddr);
14581 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14582 ranges_pst);
14583 }
14584
14585 /* FIXME: This is recording everything as a low-high
14586 segment of consecutive addresses. We should have a
14587 data structure for discontiguous block ranges
14588 instead. */
14589 if (! low_set)
14590 {
14591 low = range_beginning;
14592 high = range_end;
14593 low_set = 1;
14594 }
14595 else
14596 {
14597 if (range_beginning < low)
14598 low = range_beginning;
14599 if (range_end > high)
14600 high = range_end;
14601 }
14602 });
14603 if (!retval)
14604 return 0;
14605
14606 if (! low_set)
14607 /* If the first entry is an end-of-list marker, the range
14608 describes an empty scope, i.e. no instructions. */
14609 return 0;
14610
14611 if (low_return)
14612 *low_return = low;
14613 if (high_return)
14614 *high_return = high;
14615 return 1;
14616 }
14617
14618 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14619 definition for the return value. *LOWPC and *HIGHPC are set iff
14620 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14621
14622 static enum pc_bounds_kind
14623 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14624 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14625 struct partial_symtab *pst)
14626 {
14627 struct attribute *attr;
14628 struct attribute *attr_high;
14629 CORE_ADDR low = 0;
14630 CORE_ADDR high = 0;
14631 enum pc_bounds_kind ret;
14632
14633 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14634 if (attr_high)
14635 {
14636 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14637 if (attr)
14638 {
14639 low = attr_value_as_address (attr);
14640 high = attr_value_as_address (attr_high);
14641 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14642 high += low;
14643 }
14644 else
14645 /* Found high w/o low attribute. */
14646 return PC_BOUNDS_INVALID;
14647
14648 /* Found consecutive range of addresses. */
14649 ret = PC_BOUNDS_HIGH_LOW;
14650 }
14651 else
14652 {
14653 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14654 if (attr != NULL)
14655 {
14656 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14657 We take advantage of the fact that DW_AT_ranges does not appear
14658 in DW_TAG_compile_unit of DWO files. */
14659 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14660 unsigned int ranges_offset = (DW_UNSND (attr)
14661 + (need_ranges_base
14662 ? cu->ranges_base
14663 : 0));
14664
14665 /* Value of the DW_AT_ranges attribute is the offset in the
14666 .debug_ranges section. */
14667 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14668 return PC_BOUNDS_INVALID;
14669 /* Found discontinuous range of addresses. */
14670 ret = PC_BOUNDS_RANGES;
14671 }
14672 else
14673 return PC_BOUNDS_NOT_PRESENT;
14674 }
14675
14676 /* read_partial_die has also the strict LOW < HIGH requirement. */
14677 if (high <= low)
14678 return PC_BOUNDS_INVALID;
14679
14680 /* When using the GNU linker, .gnu.linkonce. sections are used to
14681 eliminate duplicate copies of functions and vtables and such.
14682 The linker will arbitrarily choose one and discard the others.
14683 The AT_*_pc values for such functions refer to local labels in
14684 these sections. If the section from that file was discarded, the
14685 labels are not in the output, so the relocs get a value of 0.
14686 If this is a discarded function, mark the pc bounds as invalid,
14687 so that GDB will ignore it. */
14688 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14689 return PC_BOUNDS_INVALID;
14690
14691 *lowpc = low;
14692 if (highpc)
14693 *highpc = high;
14694 return ret;
14695 }
14696
14697 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14698 its low and high PC addresses. Do nothing if these addresses could not
14699 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14700 and HIGHPC to the high address if greater than HIGHPC. */
14701
14702 static void
14703 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14704 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14705 struct dwarf2_cu *cu)
14706 {
14707 CORE_ADDR low, high;
14708 struct die_info *child = die->child;
14709
14710 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14711 {
14712 *lowpc = std::min (*lowpc, low);
14713 *highpc = std::max (*highpc, high);
14714 }
14715
14716 /* If the language does not allow nested subprograms (either inside
14717 subprograms or lexical blocks), we're done. */
14718 if (cu->language != language_ada)
14719 return;
14720
14721 /* Check all the children of the given DIE. If it contains nested
14722 subprograms, then check their pc bounds. Likewise, we need to
14723 check lexical blocks as well, as they may also contain subprogram
14724 definitions. */
14725 while (child && child->tag)
14726 {
14727 if (child->tag == DW_TAG_subprogram
14728 || child->tag == DW_TAG_lexical_block)
14729 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14730 child = sibling_die (child);
14731 }
14732 }
14733
14734 /* Get the low and high pc's represented by the scope DIE, and store
14735 them in *LOWPC and *HIGHPC. If the correct values can't be
14736 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14737
14738 static void
14739 get_scope_pc_bounds (struct die_info *die,
14740 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14741 struct dwarf2_cu *cu)
14742 {
14743 CORE_ADDR best_low = (CORE_ADDR) -1;
14744 CORE_ADDR best_high = (CORE_ADDR) 0;
14745 CORE_ADDR current_low, current_high;
14746
14747 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14748 >= PC_BOUNDS_RANGES)
14749 {
14750 best_low = current_low;
14751 best_high = current_high;
14752 }
14753 else
14754 {
14755 struct die_info *child = die->child;
14756
14757 while (child && child->tag)
14758 {
14759 switch (child->tag) {
14760 case DW_TAG_subprogram:
14761 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14762 break;
14763 case DW_TAG_namespace:
14764 case DW_TAG_module:
14765 /* FIXME: carlton/2004-01-16: Should we do this for
14766 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14767 that current GCC's always emit the DIEs corresponding
14768 to definitions of methods of classes as children of a
14769 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14770 the DIEs giving the declarations, which could be
14771 anywhere). But I don't see any reason why the
14772 standards says that they have to be there. */
14773 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14774
14775 if (current_low != ((CORE_ADDR) -1))
14776 {
14777 best_low = std::min (best_low, current_low);
14778 best_high = std::max (best_high, current_high);
14779 }
14780 break;
14781 default:
14782 /* Ignore. */
14783 break;
14784 }
14785
14786 child = sibling_die (child);
14787 }
14788 }
14789
14790 *lowpc = best_low;
14791 *highpc = best_high;
14792 }
14793
14794 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14795 in DIE. */
14796
14797 static void
14798 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14799 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14800 {
14801 struct objfile *objfile = cu->objfile;
14802 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14803 struct attribute *attr;
14804 struct attribute *attr_high;
14805
14806 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14807 if (attr_high)
14808 {
14809 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14810 if (attr)
14811 {
14812 CORE_ADDR low = attr_value_as_address (attr);
14813 CORE_ADDR high = attr_value_as_address (attr_high);
14814
14815 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14816 high += low;
14817
14818 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14819 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14820 record_block_range (block, low, high - 1);
14821 }
14822 }
14823
14824 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14825 if (attr)
14826 {
14827 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14828 We take advantage of the fact that DW_AT_ranges does not appear
14829 in DW_TAG_compile_unit of DWO files. */
14830 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14831
14832 /* The value of the DW_AT_ranges attribute is the offset of the
14833 address range list in the .debug_ranges section. */
14834 unsigned long offset = (DW_UNSND (attr)
14835 + (need_ranges_base ? cu->ranges_base : 0));
14836 const gdb_byte *buffer;
14837
14838 /* For some target architectures, but not others, the
14839 read_address function sign-extends the addresses it returns.
14840 To recognize base address selection entries, we need a
14841 mask. */
14842 unsigned int addr_size = cu->header.addr_size;
14843 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14844
14845 /* The base address, to which the next pair is relative. Note
14846 that this 'base' is a DWARF concept: most entries in a range
14847 list are relative, to reduce the number of relocs against the
14848 debugging information. This is separate from this function's
14849 'baseaddr' argument, which GDB uses to relocate debugging
14850 information from a shared library based on the address at
14851 which the library was loaded. */
14852 CORE_ADDR base = cu->base_address;
14853 int base_known = cu->base_known;
14854
14855 dwarf2_ranges_process (offset, cu,
14856 [&] (CORE_ADDR start, CORE_ADDR end)
14857 {
14858 start += baseaddr;
14859 end += baseaddr;
14860 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14861 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14862 record_block_range (block, start, end - 1);
14863 });
14864 }
14865 }
14866
14867 /* Check whether the producer field indicates either of GCC < 4.6, or the
14868 Intel C/C++ compiler, and cache the result in CU. */
14869
14870 static void
14871 check_producer (struct dwarf2_cu *cu)
14872 {
14873 int major, minor;
14874
14875 if (cu->producer == NULL)
14876 {
14877 /* For unknown compilers expect their behavior is DWARF version
14878 compliant.
14879
14880 GCC started to support .debug_types sections by -gdwarf-4 since
14881 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14882 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14883 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14884 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14885 }
14886 else if (producer_is_gcc (cu->producer, &major, &minor))
14887 {
14888 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14889 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14890 }
14891 else if (producer_is_icc (cu->producer, &major, &minor))
14892 cu->producer_is_icc_lt_14 = major < 14;
14893 else
14894 {
14895 /* For other non-GCC compilers, expect their behavior is DWARF version
14896 compliant. */
14897 }
14898
14899 cu->checked_producer = 1;
14900 }
14901
14902 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14903 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14904 during 4.6.0 experimental. */
14905
14906 static int
14907 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14908 {
14909 if (!cu->checked_producer)
14910 check_producer (cu);
14911
14912 return cu->producer_is_gxx_lt_4_6;
14913 }
14914
14915 /* Return the default accessibility type if it is not overriden by
14916 DW_AT_accessibility. */
14917
14918 static enum dwarf_access_attribute
14919 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14920 {
14921 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14922 {
14923 /* The default DWARF 2 accessibility for members is public, the default
14924 accessibility for inheritance is private. */
14925
14926 if (die->tag != DW_TAG_inheritance)
14927 return DW_ACCESS_public;
14928 else
14929 return DW_ACCESS_private;
14930 }
14931 else
14932 {
14933 /* DWARF 3+ defines the default accessibility a different way. The same
14934 rules apply now for DW_TAG_inheritance as for the members and it only
14935 depends on the container kind. */
14936
14937 if (die->parent->tag == DW_TAG_class_type)
14938 return DW_ACCESS_private;
14939 else
14940 return DW_ACCESS_public;
14941 }
14942 }
14943
14944 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14945 offset. If the attribute was not found return 0, otherwise return
14946 1. If it was found but could not properly be handled, set *OFFSET
14947 to 0. */
14948
14949 static int
14950 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14951 LONGEST *offset)
14952 {
14953 struct attribute *attr;
14954
14955 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14956 if (attr != NULL)
14957 {
14958 *offset = 0;
14959
14960 /* Note that we do not check for a section offset first here.
14961 This is because DW_AT_data_member_location is new in DWARF 4,
14962 so if we see it, we can assume that a constant form is really
14963 a constant and not a section offset. */
14964 if (attr_form_is_constant (attr))
14965 *offset = dwarf2_get_attr_constant_value (attr, 0);
14966 else if (attr_form_is_section_offset (attr))
14967 dwarf2_complex_location_expr_complaint ();
14968 else if (attr_form_is_block (attr))
14969 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14970 else
14971 dwarf2_complex_location_expr_complaint ();
14972
14973 return 1;
14974 }
14975
14976 return 0;
14977 }
14978
14979 /* Add an aggregate field to the field list. */
14980
14981 static void
14982 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14983 struct dwarf2_cu *cu)
14984 {
14985 struct objfile *objfile = cu->objfile;
14986 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14987 struct nextfield *new_field;
14988 struct attribute *attr;
14989 struct field *fp;
14990 const char *fieldname = "";
14991
14992 /* Allocate a new field list entry and link it in. */
14993 new_field = XNEW (struct nextfield);
14994 make_cleanup (xfree, new_field);
14995 memset (new_field, 0, sizeof (struct nextfield));
14996
14997 if (die->tag == DW_TAG_inheritance)
14998 {
14999 new_field->next = fip->baseclasses;
15000 fip->baseclasses = new_field;
15001 }
15002 else
15003 {
15004 new_field->next = fip->fields;
15005 fip->fields = new_field;
15006 }
15007 fip->nfields++;
15008
15009 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15010 if (attr)
15011 new_field->accessibility = DW_UNSND (attr);
15012 else
15013 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15014 if (new_field->accessibility != DW_ACCESS_public)
15015 fip->non_public_fields = 1;
15016
15017 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15018 if (attr)
15019 new_field->virtuality = DW_UNSND (attr);
15020 else
15021 new_field->virtuality = DW_VIRTUALITY_none;
15022
15023 fp = &new_field->field;
15024
15025 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15026 {
15027 LONGEST offset;
15028
15029 /* Data member other than a C++ static data member. */
15030
15031 /* Get type of field. */
15032 fp->type = die_type (die, cu);
15033
15034 SET_FIELD_BITPOS (*fp, 0);
15035
15036 /* Get bit size of field (zero if none). */
15037 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15038 if (attr)
15039 {
15040 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15041 }
15042 else
15043 {
15044 FIELD_BITSIZE (*fp) = 0;
15045 }
15046
15047 /* Get bit offset of field. */
15048 if (handle_data_member_location (die, cu, &offset))
15049 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15050 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15051 if (attr)
15052 {
15053 if (gdbarch_bits_big_endian (gdbarch))
15054 {
15055 /* For big endian bits, the DW_AT_bit_offset gives the
15056 additional bit offset from the MSB of the containing
15057 anonymous object to the MSB of the field. We don't
15058 have to do anything special since we don't need to
15059 know the size of the anonymous object. */
15060 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15061 }
15062 else
15063 {
15064 /* For little endian bits, compute the bit offset to the
15065 MSB of the anonymous object, subtract off the number of
15066 bits from the MSB of the field to the MSB of the
15067 object, and then subtract off the number of bits of
15068 the field itself. The result is the bit offset of
15069 the LSB of the field. */
15070 int anonymous_size;
15071 int bit_offset = DW_UNSND (attr);
15072
15073 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15074 if (attr)
15075 {
15076 /* The size of the anonymous object containing
15077 the bit field is explicit, so use the
15078 indicated size (in bytes). */
15079 anonymous_size = DW_UNSND (attr);
15080 }
15081 else
15082 {
15083 /* The size of the anonymous object containing
15084 the bit field must be inferred from the type
15085 attribute of the data member containing the
15086 bit field. */
15087 anonymous_size = TYPE_LENGTH (fp->type);
15088 }
15089 SET_FIELD_BITPOS (*fp,
15090 (FIELD_BITPOS (*fp)
15091 + anonymous_size * bits_per_byte
15092 - bit_offset - FIELD_BITSIZE (*fp)));
15093 }
15094 }
15095 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15096 if (attr != NULL)
15097 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15098 + dwarf2_get_attr_constant_value (attr, 0)));
15099
15100 /* Get name of field. */
15101 fieldname = dwarf2_name (die, cu);
15102 if (fieldname == NULL)
15103 fieldname = "";
15104
15105 /* The name is already allocated along with this objfile, so we don't
15106 need to duplicate it for the type. */
15107 fp->name = fieldname;
15108
15109 /* Change accessibility for artificial fields (e.g. virtual table
15110 pointer or virtual base class pointer) to private. */
15111 if (dwarf2_attr (die, DW_AT_artificial, cu))
15112 {
15113 FIELD_ARTIFICIAL (*fp) = 1;
15114 new_field->accessibility = DW_ACCESS_private;
15115 fip->non_public_fields = 1;
15116 }
15117 }
15118 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15119 {
15120 /* C++ static member. */
15121
15122 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15123 is a declaration, but all versions of G++ as of this writing
15124 (so through at least 3.2.1) incorrectly generate
15125 DW_TAG_variable tags. */
15126
15127 const char *physname;
15128
15129 /* Get name of field. */
15130 fieldname = dwarf2_name (die, cu);
15131 if (fieldname == NULL)
15132 return;
15133
15134 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15135 if (attr
15136 /* Only create a symbol if this is an external value.
15137 new_symbol checks this and puts the value in the global symbol
15138 table, which we want. If it is not external, new_symbol
15139 will try to put the value in cu->list_in_scope which is wrong. */
15140 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15141 {
15142 /* A static const member, not much different than an enum as far as
15143 we're concerned, except that we can support more types. */
15144 new_symbol (die, NULL, cu);
15145 }
15146
15147 /* Get physical name. */
15148 physname = dwarf2_physname (fieldname, die, cu);
15149
15150 /* The name is already allocated along with this objfile, so we don't
15151 need to duplicate it for the type. */
15152 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15153 FIELD_TYPE (*fp) = die_type (die, cu);
15154 FIELD_NAME (*fp) = fieldname;
15155 }
15156 else if (die->tag == DW_TAG_inheritance)
15157 {
15158 LONGEST offset;
15159
15160 /* C++ base class field. */
15161 if (handle_data_member_location (die, cu, &offset))
15162 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15163 FIELD_BITSIZE (*fp) = 0;
15164 FIELD_TYPE (*fp) = die_type (die, cu);
15165 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15166 fip->nbaseclasses++;
15167 }
15168 }
15169
15170 /* Can the type given by DIE define another type? */
15171
15172 static bool
15173 type_can_define_types (const struct die_info *die)
15174 {
15175 switch (die->tag)
15176 {
15177 case DW_TAG_typedef:
15178 case DW_TAG_class_type:
15179 case DW_TAG_structure_type:
15180 case DW_TAG_union_type:
15181 case DW_TAG_enumeration_type:
15182 return true;
15183
15184 default:
15185 return false;
15186 }
15187 }
15188
15189 /* Add a type definition defined in the scope of the FIP's class. */
15190
15191 static void
15192 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15193 struct dwarf2_cu *cu)
15194 {
15195 struct decl_field_list *new_field;
15196 struct decl_field *fp;
15197
15198 /* Allocate a new field list entry and link it in. */
15199 new_field = XCNEW (struct decl_field_list);
15200 make_cleanup (xfree, new_field);
15201
15202 gdb_assert (type_can_define_types (die));
15203
15204 fp = &new_field->field;
15205
15206 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15207 fp->name = dwarf2_name (die, cu);
15208 fp->type = read_type_die (die, cu);
15209
15210 /* Save accessibility. */
15211 enum dwarf_access_attribute accessibility;
15212 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15213 if (attr != NULL)
15214 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15215 else
15216 accessibility = dwarf2_default_access_attribute (die, cu);
15217 switch (accessibility)
15218 {
15219 case DW_ACCESS_public:
15220 /* The assumed value if neither private nor protected. */
15221 break;
15222 case DW_ACCESS_private:
15223 fp->is_private = 1;
15224 break;
15225 case DW_ACCESS_protected:
15226 fp->is_protected = 1;
15227 break;
15228 default:
15229 complaint (&symfile_complaints,
15230 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15231 }
15232
15233 if (die->tag == DW_TAG_typedef)
15234 {
15235 new_field->next = fip->typedef_field_list;
15236 fip->typedef_field_list = new_field;
15237 fip->typedef_field_list_count++;
15238 }
15239 else
15240 {
15241 new_field->next = fip->nested_types_list;
15242 fip->nested_types_list = new_field;
15243 fip->nested_types_list_count++;
15244 }
15245 }
15246
15247 /* Create the vector of fields, and attach it to the type. */
15248
15249 static void
15250 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15251 struct dwarf2_cu *cu)
15252 {
15253 int nfields = fip->nfields;
15254
15255 /* Record the field count, allocate space for the array of fields,
15256 and create blank accessibility bitfields if necessary. */
15257 TYPE_NFIELDS (type) = nfields;
15258 TYPE_FIELDS (type) = (struct field *)
15259 TYPE_ALLOC (type, sizeof (struct field) * nfields);
15260 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
15261
15262 if (fip->non_public_fields && cu->language != language_ada)
15263 {
15264 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15265
15266 TYPE_FIELD_PRIVATE_BITS (type) =
15267 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15268 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15269
15270 TYPE_FIELD_PROTECTED_BITS (type) =
15271 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15272 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15273
15274 TYPE_FIELD_IGNORE_BITS (type) =
15275 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15276 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15277 }
15278
15279 /* If the type has baseclasses, allocate and clear a bit vector for
15280 TYPE_FIELD_VIRTUAL_BITS. */
15281 if (fip->nbaseclasses && cu->language != language_ada)
15282 {
15283 int num_bytes = B_BYTES (fip->nbaseclasses);
15284 unsigned char *pointer;
15285
15286 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15287 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15288 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15289 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
15290 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
15291 }
15292
15293 /* Copy the saved-up fields into the field vector. Start from the head of
15294 the list, adding to the tail of the field array, so that they end up in
15295 the same order in the array in which they were added to the list. */
15296 while (nfields-- > 0)
15297 {
15298 struct nextfield *fieldp;
15299
15300 if (fip->fields)
15301 {
15302 fieldp = fip->fields;
15303 fip->fields = fieldp->next;
15304 }
15305 else
15306 {
15307 fieldp = fip->baseclasses;
15308 fip->baseclasses = fieldp->next;
15309 }
15310
15311 TYPE_FIELD (type, nfields) = fieldp->field;
15312 switch (fieldp->accessibility)
15313 {
15314 case DW_ACCESS_private:
15315 if (cu->language != language_ada)
15316 SET_TYPE_FIELD_PRIVATE (type, nfields);
15317 break;
15318
15319 case DW_ACCESS_protected:
15320 if (cu->language != language_ada)
15321 SET_TYPE_FIELD_PROTECTED (type, nfields);
15322 break;
15323
15324 case DW_ACCESS_public:
15325 break;
15326
15327 default:
15328 /* Unknown accessibility. Complain and treat it as public. */
15329 {
15330 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15331 fieldp->accessibility);
15332 }
15333 break;
15334 }
15335 if (nfields < fip->nbaseclasses)
15336 {
15337 switch (fieldp->virtuality)
15338 {
15339 case DW_VIRTUALITY_virtual:
15340 case DW_VIRTUALITY_pure_virtual:
15341 if (cu->language == language_ada)
15342 error (_("unexpected virtuality in component of Ada type"));
15343 SET_TYPE_FIELD_VIRTUAL (type, nfields);
15344 break;
15345 }
15346 }
15347 }
15348 }
15349
15350 /* Return true if this member function is a constructor, false
15351 otherwise. */
15352
15353 static int
15354 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15355 {
15356 const char *fieldname;
15357 const char *type_name;
15358 int len;
15359
15360 if (die->parent == NULL)
15361 return 0;
15362
15363 if (die->parent->tag != DW_TAG_structure_type
15364 && die->parent->tag != DW_TAG_union_type
15365 && die->parent->tag != DW_TAG_class_type)
15366 return 0;
15367
15368 fieldname = dwarf2_name (die, cu);
15369 type_name = dwarf2_name (die->parent, cu);
15370 if (fieldname == NULL || type_name == NULL)
15371 return 0;
15372
15373 len = strlen (fieldname);
15374 return (strncmp (fieldname, type_name, len) == 0
15375 && (type_name[len] == '\0' || type_name[len] == '<'));
15376 }
15377
15378 /* Add a member function to the proper fieldlist. */
15379
15380 static void
15381 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15382 struct type *type, struct dwarf2_cu *cu)
15383 {
15384 struct objfile *objfile = cu->objfile;
15385 struct attribute *attr;
15386 struct fnfieldlist *flp;
15387 int i;
15388 struct fn_field *fnp;
15389 const char *fieldname;
15390 struct nextfnfield *new_fnfield;
15391 struct type *this_type;
15392 enum dwarf_access_attribute accessibility;
15393
15394 if (cu->language == language_ada)
15395 error (_("unexpected member function in Ada type"));
15396
15397 /* Get name of member function. */
15398 fieldname = dwarf2_name (die, cu);
15399 if (fieldname == NULL)
15400 return;
15401
15402 /* Look up member function name in fieldlist. */
15403 for (i = 0; i < fip->nfnfields; i++)
15404 {
15405 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15406 break;
15407 }
15408
15409 /* Create new list element if necessary. */
15410 if (i < fip->nfnfields)
15411 flp = &fip->fnfieldlists[i];
15412 else
15413 {
15414 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
15415 {
15416 fip->fnfieldlists = (struct fnfieldlist *)
15417 xrealloc (fip->fnfieldlists,
15418 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
15419 * sizeof (struct fnfieldlist));
15420 if (fip->nfnfields == 0)
15421 make_cleanup (free_current_contents, &fip->fnfieldlists);
15422 }
15423 flp = &fip->fnfieldlists[fip->nfnfields];
15424 flp->name = fieldname;
15425 flp->length = 0;
15426 flp->head = NULL;
15427 i = fip->nfnfields++;
15428 }
15429
15430 /* Create a new member function field and chain it to the field list
15431 entry. */
15432 new_fnfield = XNEW (struct nextfnfield);
15433 make_cleanup (xfree, new_fnfield);
15434 memset (new_fnfield, 0, sizeof (struct nextfnfield));
15435 new_fnfield->next = flp->head;
15436 flp->head = new_fnfield;
15437 flp->length++;
15438
15439 /* Fill in the member function field info. */
15440 fnp = &new_fnfield->fnfield;
15441
15442 /* Delay processing of the physname until later. */
15443 if (cu->language == language_cplus)
15444 {
15445 add_to_method_list (type, i, flp->length - 1, fieldname,
15446 die, cu);
15447 }
15448 else
15449 {
15450 const char *physname = dwarf2_physname (fieldname, die, cu);
15451 fnp->physname = physname ? physname : "";
15452 }
15453
15454 fnp->type = alloc_type (objfile);
15455 this_type = read_type_die (die, cu);
15456 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15457 {
15458 int nparams = TYPE_NFIELDS (this_type);
15459
15460 /* TYPE is the domain of this method, and THIS_TYPE is the type
15461 of the method itself (TYPE_CODE_METHOD). */
15462 smash_to_method_type (fnp->type, type,
15463 TYPE_TARGET_TYPE (this_type),
15464 TYPE_FIELDS (this_type),
15465 TYPE_NFIELDS (this_type),
15466 TYPE_VARARGS (this_type));
15467
15468 /* Handle static member functions.
15469 Dwarf2 has no clean way to discern C++ static and non-static
15470 member functions. G++ helps GDB by marking the first
15471 parameter for non-static member functions (which is the this
15472 pointer) as artificial. We obtain this information from
15473 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15474 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15475 fnp->voffset = VOFFSET_STATIC;
15476 }
15477 else
15478 complaint (&symfile_complaints, _("member function type missing for '%s'"),
15479 dwarf2_full_name (fieldname, die, cu));
15480
15481 /* Get fcontext from DW_AT_containing_type if present. */
15482 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15483 fnp->fcontext = die_containing_type (die, cu);
15484
15485 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15486 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15487
15488 /* Get accessibility. */
15489 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15490 if (attr)
15491 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15492 else
15493 accessibility = dwarf2_default_access_attribute (die, cu);
15494 switch (accessibility)
15495 {
15496 case DW_ACCESS_private:
15497 fnp->is_private = 1;
15498 break;
15499 case DW_ACCESS_protected:
15500 fnp->is_protected = 1;
15501 break;
15502 }
15503
15504 /* Check for artificial methods. */
15505 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15506 if (attr && DW_UNSND (attr) != 0)
15507 fnp->is_artificial = 1;
15508
15509 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15510
15511 /* Get index in virtual function table if it is a virtual member
15512 function. For older versions of GCC, this is an offset in the
15513 appropriate virtual table, as specified by DW_AT_containing_type.
15514 For everyone else, it is an expression to be evaluated relative
15515 to the object address. */
15516
15517 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15518 if (attr)
15519 {
15520 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15521 {
15522 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15523 {
15524 /* Old-style GCC. */
15525 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15526 }
15527 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15528 || (DW_BLOCK (attr)->size > 1
15529 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15530 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15531 {
15532 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15533 if ((fnp->voffset % cu->header.addr_size) != 0)
15534 dwarf2_complex_location_expr_complaint ();
15535 else
15536 fnp->voffset /= cu->header.addr_size;
15537 fnp->voffset += 2;
15538 }
15539 else
15540 dwarf2_complex_location_expr_complaint ();
15541
15542 if (!fnp->fcontext)
15543 {
15544 /* If there is no `this' field and no DW_AT_containing_type,
15545 we cannot actually find a base class context for the
15546 vtable! */
15547 if (TYPE_NFIELDS (this_type) == 0
15548 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15549 {
15550 complaint (&symfile_complaints,
15551 _("cannot determine context for virtual member "
15552 "function \"%s\" (offset %d)"),
15553 fieldname, to_underlying (die->sect_off));
15554 }
15555 else
15556 {
15557 fnp->fcontext
15558 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15559 }
15560 }
15561 }
15562 else if (attr_form_is_section_offset (attr))
15563 {
15564 dwarf2_complex_location_expr_complaint ();
15565 }
15566 else
15567 {
15568 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15569 fieldname);
15570 }
15571 }
15572 else
15573 {
15574 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15575 if (attr && DW_UNSND (attr))
15576 {
15577 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15578 complaint (&symfile_complaints,
15579 _("Member function \"%s\" (offset %d) is virtual "
15580 "but the vtable offset is not specified"),
15581 fieldname, to_underlying (die->sect_off));
15582 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15583 TYPE_CPLUS_DYNAMIC (type) = 1;
15584 }
15585 }
15586 }
15587
15588 /* Create the vector of member function fields, and attach it to the type. */
15589
15590 static void
15591 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15592 struct dwarf2_cu *cu)
15593 {
15594 struct fnfieldlist *flp;
15595 int i;
15596
15597 if (cu->language == language_ada)
15598 error (_("unexpected member functions in Ada type"));
15599
15600 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15601 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15602 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
15603
15604 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
15605 {
15606 struct nextfnfield *nfp = flp->head;
15607 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15608 int k;
15609
15610 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
15611 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
15612 fn_flp->fn_fields = (struct fn_field *)
15613 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
15614 for (k = flp->length; (k--, nfp); nfp = nfp->next)
15615 fn_flp->fn_fields[k] = nfp->fnfield;
15616 }
15617
15618 TYPE_NFN_FIELDS (type) = fip->nfnfields;
15619 }
15620
15621 /* Returns non-zero if NAME is the name of a vtable member in CU's
15622 language, zero otherwise. */
15623 static int
15624 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15625 {
15626 static const char vptr[] = "_vptr";
15627
15628 /* Look for the C++ form of the vtable. */
15629 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15630 return 1;
15631
15632 return 0;
15633 }
15634
15635 /* GCC outputs unnamed structures that are really pointers to member
15636 functions, with the ABI-specified layout. If TYPE describes
15637 such a structure, smash it into a member function type.
15638
15639 GCC shouldn't do this; it should just output pointer to member DIEs.
15640 This is GCC PR debug/28767. */
15641
15642 static void
15643 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15644 {
15645 struct type *pfn_type, *self_type, *new_type;
15646
15647 /* Check for a structure with no name and two children. */
15648 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15649 return;
15650
15651 /* Check for __pfn and __delta members. */
15652 if (TYPE_FIELD_NAME (type, 0) == NULL
15653 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15654 || TYPE_FIELD_NAME (type, 1) == NULL
15655 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15656 return;
15657
15658 /* Find the type of the method. */
15659 pfn_type = TYPE_FIELD_TYPE (type, 0);
15660 if (pfn_type == NULL
15661 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15662 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15663 return;
15664
15665 /* Look for the "this" argument. */
15666 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15667 if (TYPE_NFIELDS (pfn_type) == 0
15668 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15669 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15670 return;
15671
15672 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15673 new_type = alloc_type (objfile);
15674 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15675 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15676 TYPE_VARARGS (pfn_type));
15677 smash_to_methodptr_type (type, new_type);
15678 }
15679
15680
15681 /* Called when we find the DIE that starts a structure or union scope
15682 (definition) to create a type for the structure or union. Fill in
15683 the type's name and general properties; the members will not be
15684 processed until process_structure_scope. A symbol table entry for
15685 the type will also not be done until process_structure_scope (assuming
15686 the type has a name).
15687
15688 NOTE: we need to call these functions regardless of whether or not the
15689 DIE has a DW_AT_name attribute, since it might be an anonymous
15690 structure or union. This gets the type entered into our set of
15691 user defined types. */
15692
15693 static struct type *
15694 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15695 {
15696 struct objfile *objfile = cu->objfile;
15697 struct type *type;
15698 struct attribute *attr;
15699 const char *name;
15700
15701 /* If the definition of this type lives in .debug_types, read that type.
15702 Don't follow DW_AT_specification though, that will take us back up
15703 the chain and we want to go down. */
15704 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15705 if (attr)
15706 {
15707 type = get_DW_AT_signature_type (die, attr, cu);
15708
15709 /* The type's CU may not be the same as CU.
15710 Ensure TYPE is recorded with CU in die_type_hash. */
15711 return set_die_type (die, type, cu);
15712 }
15713
15714 type = alloc_type (objfile);
15715 INIT_CPLUS_SPECIFIC (type);
15716
15717 name = dwarf2_name (die, cu);
15718 if (name != NULL)
15719 {
15720 if (cu->language == language_cplus
15721 || cu->language == language_d
15722 || cu->language == language_rust)
15723 {
15724 const char *full_name = dwarf2_full_name (name, die, cu);
15725
15726 /* dwarf2_full_name might have already finished building the DIE's
15727 type. If so, there is no need to continue. */
15728 if (get_die_type (die, cu) != NULL)
15729 return get_die_type (die, cu);
15730
15731 TYPE_TAG_NAME (type) = full_name;
15732 if (die->tag == DW_TAG_structure_type
15733 || die->tag == DW_TAG_class_type)
15734 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15735 }
15736 else
15737 {
15738 /* The name is already allocated along with this objfile, so
15739 we don't need to duplicate it for the type. */
15740 TYPE_TAG_NAME (type) = name;
15741 if (die->tag == DW_TAG_class_type)
15742 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15743 }
15744 }
15745
15746 if (die->tag == DW_TAG_structure_type)
15747 {
15748 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15749 }
15750 else if (die->tag == DW_TAG_union_type)
15751 {
15752 TYPE_CODE (type) = TYPE_CODE_UNION;
15753 }
15754 else
15755 {
15756 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15757 }
15758
15759 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15760 TYPE_DECLARED_CLASS (type) = 1;
15761
15762 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15763 if (attr)
15764 {
15765 if (attr_form_is_constant (attr))
15766 TYPE_LENGTH (type) = DW_UNSND (attr);
15767 else
15768 {
15769 /* For the moment, dynamic type sizes are not supported
15770 by GDB's struct type. The actual size is determined
15771 on-demand when resolving the type of a given object,
15772 so set the type's length to zero for now. Otherwise,
15773 we record an expression as the length, and that expression
15774 could lead to a very large value, which could eventually
15775 lead to us trying to allocate that much memory when creating
15776 a value of that type. */
15777 TYPE_LENGTH (type) = 0;
15778 }
15779 }
15780 else
15781 {
15782 TYPE_LENGTH (type) = 0;
15783 }
15784
15785 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15786 {
15787 /* ICC<14 does not output the required DW_AT_declaration on
15788 incomplete types, but gives them a size of zero. */
15789 TYPE_STUB (type) = 1;
15790 }
15791 else
15792 TYPE_STUB_SUPPORTED (type) = 1;
15793
15794 if (die_is_declaration (die, cu))
15795 TYPE_STUB (type) = 1;
15796 else if (attr == NULL && die->child == NULL
15797 && producer_is_realview (cu->producer))
15798 /* RealView does not output the required DW_AT_declaration
15799 on incomplete types. */
15800 TYPE_STUB (type) = 1;
15801
15802 /* We need to add the type field to the die immediately so we don't
15803 infinitely recurse when dealing with pointers to the structure
15804 type within the structure itself. */
15805 set_die_type (die, type, cu);
15806
15807 /* set_die_type should be already done. */
15808 set_descriptive_type (type, die, cu);
15809
15810 return type;
15811 }
15812
15813 /* Finish creating a structure or union type, including filling in
15814 its members and creating a symbol for it. */
15815
15816 static void
15817 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15818 {
15819 struct objfile *objfile = cu->objfile;
15820 struct die_info *child_die;
15821 struct type *type;
15822
15823 type = get_die_type (die, cu);
15824 if (type == NULL)
15825 type = read_structure_type (die, cu);
15826
15827 if (die->child != NULL && ! die_is_declaration (die, cu))
15828 {
15829 struct field_info fi;
15830 std::vector<struct symbol *> template_args;
15831 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
15832
15833 memset (&fi, 0, sizeof (struct field_info));
15834
15835 child_die = die->child;
15836
15837 while (child_die && child_die->tag)
15838 {
15839 if (child_die->tag == DW_TAG_member
15840 || child_die->tag == DW_TAG_variable)
15841 {
15842 /* NOTE: carlton/2002-11-05: A C++ static data member
15843 should be a DW_TAG_member that is a declaration, but
15844 all versions of G++ as of this writing (so through at
15845 least 3.2.1) incorrectly generate DW_TAG_variable
15846 tags for them instead. */
15847 dwarf2_add_field (&fi, child_die, cu);
15848 }
15849 else if (child_die->tag == DW_TAG_subprogram)
15850 {
15851 /* Rust doesn't have member functions in the C++ sense.
15852 However, it does emit ordinary functions as children
15853 of a struct DIE. */
15854 if (cu->language == language_rust)
15855 read_func_scope (child_die, cu);
15856 else
15857 {
15858 /* C++ member function. */
15859 dwarf2_add_member_fn (&fi, child_die, type, cu);
15860 }
15861 }
15862 else if (child_die->tag == DW_TAG_inheritance)
15863 {
15864 /* C++ base class field. */
15865 dwarf2_add_field (&fi, child_die, cu);
15866 }
15867 else if (type_can_define_types (child_die))
15868 dwarf2_add_type_defn (&fi, child_die, cu);
15869 else if (child_die->tag == DW_TAG_template_type_param
15870 || child_die->tag == DW_TAG_template_value_param)
15871 {
15872 struct symbol *arg = new_symbol (child_die, NULL, cu);
15873
15874 if (arg != NULL)
15875 template_args.push_back (arg);
15876 }
15877
15878 child_die = sibling_die (child_die);
15879 }
15880
15881 /* Attach template arguments to type. */
15882 if (!template_args.empty ())
15883 {
15884 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15885 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15886 TYPE_TEMPLATE_ARGUMENTS (type)
15887 = XOBNEWVEC (&objfile->objfile_obstack,
15888 struct symbol *,
15889 TYPE_N_TEMPLATE_ARGUMENTS (type));
15890 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15891 template_args.data (),
15892 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15893 * sizeof (struct symbol *)));
15894 }
15895
15896 /* Attach fields and member functions to the type. */
15897 if (fi.nfields)
15898 dwarf2_attach_fields_to_type (&fi, type, cu);
15899 if (fi.nfnfields)
15900 {
15901 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15902
15903 /* Get the type which refers to the base class (possibly this
15904 class itself) which contains the vtable pointer for the current
15905 class from the DW_AT_containing_type attribute. This use of
15906 DW_AT_containing_type is a GNU extension. */
15907
15908 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15909 {
15910 struct type *t = die_containing_type (die, cu);
15911
15912 set_type_vptr_basetype (type, t);
15913 if (type == t)
15914 {
15915 int i;
15916
15917 /* Our own class provides vtbl ptr. */
15918 for (i = TYPE_NFIELDS (t) - 1;
15919 i >= TYPE_N_BASECLASSES (t);
15920 --i)
15921 {
15922 const char *fieldname = TYPE_FIELD_NAME (t, i);
15923
15924 if (is_vtable_name (fieldname, cu))
15925 {
15926 set_type_vptr_fieldno (type, i);
15927 break;
15928 }
15929 }
15930
15931 /* Complain if virtual function table field not found. */
15932 if (i < TYPE_N_BASECLASSES (t))
15933 complaint (&symfile_complaints,
15934 _("virtual function table pointer "
15935 "not found when defining class '%s'"),
15936 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
15937 "");
15938 }
15939 else
15940 {
15941 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15942 }
15943 }
15944 else if (cu->producer
15945 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15946 {
15947 /* The IBM XLC compiler does not provide direct indication
15948 of the containing type, but the vtable pointer is
15949 always named __vfp. */
15950
15951 int i;
15952
15953 for (i = TYPE_NFIELDS (type) - 1;
15954 i >= TYPE_N_BASECLASSES (type);
15955 --i)
15956 {
15957 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15958 {
15959 set_type_vptr_fieldno (type, i);
15960 set_type_vptr_basetype (type, type);
15961 break;
15962 }
15963 }
15964 }
15965 }
15966
15967 /* Copy fi.typedef_field_list linked list elements content into the
15968 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15969 if (fi.typedef_field_list)
15970 {
15971 int i = fi.typedef_field_list_count;
15972
15973 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15974 TYPE_TYPEDEF_FIELD_ARRAY (type)
15975 = ((struct decl_field *)
15976 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
15977 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
15978
15979 /* Reverse the list order to keep the debug info elements order. */
15980 while (--i >= 0)
15981 {
15982 struct decl_field *dest, *src;
15983
15984 dest = &TYPE_TYPEDEF_FIELD (type, i);
15985 src = &fi.typedef_field_list->field;
15986 fi.typedef_field_list = fi.typedef_field_list->next;
15987 *dest = *src;
15988 }
15989 }
15990
15991 /* Copy fi.nested_types_list linked list elements content into the
15992 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15993 if (fi.nested_types_list != NULL && cu->language != language_ada)
15994 {
15995 int i = fi.nested_types_list_count;
15996
15997 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15998 TYPE_NESTED_TYPES_ARRAY (type)
15999 = ((struct decl_field *)
16000 TYPE_ALLOC (type, sizeof (struct decl_field) * i));
16001 TYPE_NESTED_TYPES_COUNT (type) = i;
16002
16003 /* Reverse the list order to keep the debug info elements order. */
16004 while (--i >= 0)
16005 {
16006 struct decl_field *dest, *src;
16007
16008 dest = &TYPE_NESTED_TYPES_FIELD (type, i);
16009 src = &fi.nested_types_list->field;
16010 fi.nested_types_list = fi.nested_types_list->next;
16011 *dest = *src;
16012 }
16013 }
16014
16015 do_cleanups (back_to);
16016 }
16017
16018 quirk_gcc_member_function_pointer (type, objfile);
16019
16020 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16021 snapshots) has been known to create a die giving a declaration
16022 for a class that has, as a child, a die giving a definition for a
16023 nested class. So we have to process our children even if the
16024 current die is a declaration. Normally, of course, a declaration
16025 won't have any children at all. */
16026
16027 child_die = die->child;
16028
16029 while (child_die != NULL && child_die->tag)
16030 {
16031 if (child_die->tag == DW_TAG_member
16032 || child_die->tag == DW_TAG_variable
16033 || child_die->tag == DW_TAG_inheritance
16034 || child_die->tag == DW_TAG_template_value_param
16035 || child_die->tag == DW_TAG_template_type_param)
16036 {
16037 /* Do nothing. */
16038 }
16039 else
16040 process_die (child_die, cu);
16041
16042 child_die = sibling_die (child_die);
16043 }
16044
16045 /* Do not consider external references. According to the DWARF standard,
16046 these DIEs are identified by the fact that they have no byte_size
16047 attribute, and a declaration attribute. */
16048 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16049 || !die_is_declaration (die, cu))
16050 new_symbol (die, type, cu);
16051 }
16052
16053 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16054 update TYPE using some information only available in DIE's children. */
16055
16056 static void
16057 update_enumeration_type_from_children (struct die_info *die,
16058 struct type *type,
16059 struct dwarf2_cu *cu)
16060 {
16061 struct die_info *child_die;
16062 int unsigned_enum = 1;
16063 int flag_enum = 1;
16064 ULONGEST mask = 0;
16065
16066 auto_obstack obstack;
16067
16068 for (child_die = die->child;
16069 child_die != NULL && child_die->tag;
16070 child_die = sibling_die (child_die))
16071 {
16072 struct attribute *attr;
16073 LONGEST value;
16074 const gdb_byte *bytes;
16075 struct dwarf2_locexpr_baton *baton;
16076 const char *name;
16077
16078 if (child_die->tag != DW_TAG_enumerator)
16079 continue;
16080
16081 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16082 if (attr == NULL)
16083 continue;
16084
16085 name = dwarf2_name (child_die, cu);
16086 if (name == NULL)
16087 name = "<anonymous enumerator>";
16088
16089 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16090 &value, &bytes, &baton);
16091 if (value < 0)
16092 {
16093 unsigned_enum = 0;
16094 flag_enum = 0;
16095 }
16096 else if ((mask & value) != 0)
16097 flag_enum = 0;
16098 else
16099 mask |= value;
16100
16101 /* If we already know that the enum type is neither unsigned, nor
16102 a flag type, no need to look at the rest of the enumerates. */
16103 if (!unsigned_enum && !flag_enum)
16104 break;
16105 }
16106
16107 if (unsigned_enum)
16108 TYPE_UNSIGNED (type) = 1;
16109 if (flag_enum)
16110 TYPE_FLAG_ENUM (type) = 1;
16111 }
16112
16113 /* Given a DW_AT_enumeration_type die, set its type. We do not
16114 complete the type's fields yet, or create any symbols. */
16115
16116 static struct type *
16117 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16118 {
16119 struct objfile *objfile = cu->objfile;
16120 struct type *type;
16121 struct attribute *attr;
16122 const char *name;
16123
16124 /* If the definition of this type lives in .debug_types, read that type.
16125 Don't follow DW_AT_specification though, that will take us back up
16126 the chain and we want to go down. */
16127 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16128 if (attr)
16129 {
16130 type = get_DW_AT_signature_type (die, attr, cu);
16131
16132 /* The type's CU may not be the same as CU.
16133 Ensure TYPE is recorded with CU in die_type_hash. */
16134 return set_die_type (die, type, cu);
16135 }
16136
16137 type = alloc_type (objfile);
16138
16139 TYPE_CODE (type) = TYPE_CODE_ENUM;
16140 name = dwarf2_full_name (NULL, die, cu);
16141 if (name != NULL)
16142 TYPE_TAG_NAME (type) = name;
16143
16144 attr = dwarf2_attr (die, DW_AT_type, cu);
16145 if (attr != NULL)
16146 {
16147 struct type *underlying_type = die_type (die, cu);
16148
16149 TYPE_TARGET_TYPE (type) = underlying_type;
16150 }
16151
16152 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16153 if (attr)
16154 {
16155 TYPE_LENGTH (type) = DW_UNSND (attr);
16156 }
16157 else
16158 {
16159 TYPE_LENGTH (type) = 0;
16160 }
16161
16162 /* The enumeration DIE can be incomplete. In Ada, any type can be
16163 declared as private in the package spec, and then defined only
16164 inside the package body. Such types are known as Taft Amendment
16165 Types. When another package uses such a type, an incomplete DIE
16166 may be generated by the compiler. */
16167 if (die_is_declaration (die, cu))
16168 TYPE_STUB (type) = 1;
16169
16170 /* Finish the creation of this type by using the enum's children.
16171 We must call this even when the underlying type has been provided
16172 so that we can determine if we're looking at a "flag" enum. */
16173 update_enumeration_type_from_children (die, type, cu);
16174
16175 /* If this type has an underlying type that is not a stub, then we
16176 may use its attributes. We always use the "unsigned" attribute
16177 in this situation, because ordinarily we guess whether the type
16178 is unsigned -- but the guess can be wrong and the underlying type
16179 can tell us the reality. However, we defer to a local size
16180 attribute if one exists, because this lets the compiler override
16181 the underlying type if needed. */
16182 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16183 {
16184 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16185 if (TYPE_LENGTH (type) == 0)
16186 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16187 }
16188
16189 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16190
16191 return set_die_type (die, type, cu);
16192 }
16193
16194 /* Given a pointer to a die which begins an enumeration, process all
16195 the dies that define the members of the enumeration, and create the
16196 symbol for the enumeration type.
16197
16198 NOTE: We reverse the order of the element list. */
16199
16200 static void
16201 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16202 {
16203 struct type *this_type;
16204
16205 this_type = get_die_type (die, cu);
16206 if (this_type == NULL)
16207 this_type = read_enumeration_type (die, cu);
16208
16209 if (die->child != NULL)
16210 {
16211 struct die_info *child_die;
16212 struct symbol *sym;
16213 struct field *fields = NULL;
16214 int num_fields = 0;
16215 const char *name;
16216
16217 child_die = die->child;
16218 while (child_die && child_die->tag)
16219 {
16220 if (child_die->tag != DW_TAG_enumerator)
16221 {
16222 process_die (child_die, cu);
16223 }
16224 else
16225 {
16226 name = dwarf2_name (child_die, cu);
16227 if (name)
16228 {
16229 sym = new_symbol (child_die, this_type, cu);
16230
16231 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16232 {
16233 fields = (struct field *)
16234 xrealloc (fields,
16235 (num_fields + DW_FIELD_ALLOC_CHUNK)
16236 * sizeof (struct field));
16237 }
16238
16239 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16240 FIELD_TYPE (fields[num_fields]) = NULL;
16241 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16242 FIELD_BITSIZE (fields[num_fields]) = 0;
16243
16244 num_fields++;
16245 }
16246 }
16247
16248 child_die = sibling_die (child_die);
16249 }
16250
16251 if (num_fields)
16252 {
16253 TYPE_NFIELDS (this_type) = num_fields;
16254 TYPE_FIELDS (this_type) = (struct field *)
16255 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16256 memcpy (TYPE_FIELDS (this_type), fields,
16257 sizeof (struct field) * num_fields);
16258 xfree (fields);
16259 }
16260 }
16261
16262 /* If we are reading an enum from a .debug_types unit, and the enum
16263 is a declaration, and the enum is not the signatured type in the
16264 unit, then we do not want to add a symbol for it. Adding a
16265 symbol would in some cases obscure the true definition of the
16266 enum, giving users an incomplete type when the definition is
16267 actually available. Note that we do not want to do this for all
16268 enums which are just declarations, because C++0x allows forward
16269 enum declarations. */
16270 if (cu->per_cu->is_debug_types
16271 && die_is_declaration (die, cu))
16272 {
16273 struct signatured_type *sig_type;
16274
16275 sig_type = (struct signatured_type *) cu->per_cu;
16276 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16277 if (sig_type->type_offset_in_section != die->sect_off)
16278 return;
16279 }
16280
16281 new_symbol (die, this_type, cu);
16282 }
16283
16284 /* Extract all information from a DW_TAG_array_type DIE and put it in
16285 the DIE's type field. For now, this only handles one dimensional
16286 arrays. */
16287
16288 static struct type *
16289 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16290 {
16291 struct objfile *objfile = cu->objfile;
16292 struct die_info *child_die;
16293 struct type *type;
16294 struct type *element_type, *range_type, *index_type;
16295 struct attribute *attr;
16296 const char *name;
16297 unsigned int bit_stride = 0;
16298
16299 element_type = die_type (die, cu);
16300
16301 /* The die_type call above may have already set the type for this DIE. */
16302 type = get_die_type (die, cu);
16303 if (type)
16304 return type;
16305
16306 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16307 if (attr != NULL)
16308 bit_stride = DW_UNSND (attr) * 8;
16309
16310 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16311 if (attr != NULL)
16312 bit_stride = DW_UNSND (attr);
16313
16314 /* Irix 6.2 native cc creates array types without children for
16315 arrays with unspecified length. */
16316 if (die->child == NULL)
16317 {
16318 index_type = objfile_type (objfile)->builtin_int;
16319 range_type = create_static_range_type (NULL, index_type, 0, -1);
16320 type = create_array_type_with_stride (NULL, element_type, range_type,
16321 bit_stride);
16322 return set_die_type (die, type, cu);
16323 }
16324
16325 std::vector<struct type *> range_types;
16326 child_die = die->child;
16327 while (child_die && child_die->tag)
16328 {
16329 if (child_die->tag == DW_TAG_subrange_type)
16330 {
16331 struct type *child_type = read_type_die (child_die, cu);
16332
16333 if (child_type != NULL)
16334 {
16335 /* The range type was succesfully read. Save it for the
16336 array type creation. */
16337 range_types.push_back (child_type);
16338 }
16339 }
16340 child_die = sibling_die (child_die);
16341 }
16342
16343 /* Dwarf2 dimensions are output from left to right, create the
16344 necessary array types in backwards order. */
16345
16346 type = element_type;
16347
16348 if (read_array_order (die, cu) == DW_ORD_col_major)
16349 {
16350 int i = 0;
16351
16352 while (i < range_types.size ())
16353 type = create_array_type_with_stride (NULL, type, range_types[i++],
16354 bit_stride);
16355 }
16356 else
16357 {
16358 size_t ndim = range_types.size ();
16359 while (ndim-- > 0)
16360 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16361 bit_stride);
16362 }
16363
16364 /* Understand Dwarf2 support for vector types (like they occur on
16365 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16366 array type. This is not part of the Dwarf2/3 standard yet, but a
16367 custom vendor extension. The main difference between a regular
16368 array and the vector variant is that vectors are passed by value
16369 to functions. */
16370 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16371 if (attr)
16372 make_vector_type (type);
16373
16374 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16375 implementation may choose to implement triple vectors using this
16376 attribute. */
16377 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16378 if (attr)
16379 {
16380 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16381 TYPE_LENGTH (type) = DW_UNSND (attr);
16382 else
16383 complaint (&symfile_complaints,
16384 _("DW_AT_byte_size for array type smaller "
16385 "than the total size of elements"));
16386 }
16387
16388 name = dwarf2_name (die, cu);
16389 if (name)
16390 TYPE_NAME (type) = name;
16391
16392 /* Install the type in the die. */
16393 set_die_type (die, type, cu);
16394
16395 /* set_die_type should be already done. */
16396 set_descriptive_type (type, die, cu);
16397
16398 return type;
16399 }
16400
16401 static enum dwarf_array_dim_ordering
16402 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16403 {
16404 struct attribute *attr;
16405
16406 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16407
16408 if (attr)
16409 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16410
16411 /* GNU F77 is a special case, as at 08/2004 array type info is the
16412 opposite order to the dwarf2 specification, but data is still
16413 laid out as per normal fortran.
16414
16415 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16416 version checking. */
16417
16418 if (cu->language == language_fortran
16419 && cu->producer && strstr (cu->producer, "GNU F77"))
16420 {
16421 return DW_ORD_row_major;
16422 }
16423
16424 switch (cu->language_defn->la_array_ordering)
16425 {
16426 case array_column_major:
16427 return DW_ORD_col_major;
16428 case array_row_major:
16429 default:
16430 return DW_ORD_row_major;
16431 };
16432 }
16433
16434 /* Extract all information from a DW_TAG_set_type DIE and put it in
16435 the DIE's type field. */
16436
16437 static struct type *
16438 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16439 {
16440 struct type *domain_type, *set_type;
16441 struct attribute *attr;
16442
16443 domain_type = die_type (die, cu);
16444
16445 /* The die_type call above may have already set the type for this DIE. */
16446 set_type = get_die_type (die, cu);
16447 if (set_type)
16448 return set_type;
16449
16450 set_type = create_set_type (NULL, domain_type);
16451
16452 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16453 if (attr)
16454 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16455
16456 return set_die_type (die, set_type, cu);
16457 }
16458
16459 /* A helper for read_common_block that creates a locexpr baton.
16460 SYM is the symbol which we are marking as computed.
16461 COMMON_DIE is the DIE for the common block.
16462 COMMON_LOC is the location expression attribute for the common
16463 block itself.
16464 MEMBER_LOC is the location expression attribute for the particular
16465 member of the common block that we are processing.
16466 CU is the CU from which the above come. */
16467
16468 static void
16469 mark_common_block_symbol_computed (struct symbol *sym,
16470 struct die_info *common_die,
16471 struct attribute *common_loc,
16472 struct attribute *member_loc,
16473 struct dwarf2_cu *cu)
16474 {
16475 struct objfile *objfile = dwarf2_per_objfile->objfile;
16476 struct dwarf2_locexpr_baton *baton;
16477 gdb_byte *ptr;
16478 unsigned int cu_off;
16479 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16480 LONGEST offset = 0;
16481
16482 gdb_assert (common_loc && member_loc);
16483 gdb_assert (attr_form_is_block (common_loc));
16484 gdb_assert (attr_form_is_block (member_loc)
16485 || attr_form_is_constant (member_loc));
16486
16487 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16488 baton->per_cu = cu->per_cu;
16489 gdb_assert (baton->per_cu);
16490
16491 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16492
16493 if (attr_form_is_constant (member_loc))
16494 {
16495 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16496 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16497 }
16498 else
16499 baton->size += DW_BLOCK (member_loc)->size;
16500
16501 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16502 baton->data = ptr;
16503
16504 *ptr++ = DW_OP_call4;
16505 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16506 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16507 ptr += 4;
16508
16509 if (attr_form_is_constant (member_loc))
16510 {
16511 *ptr++ = DW_OP_addr;
16512 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16513 ptr += cu->header.addr_size;
16514 }
16515 else
16516 {
16517 /* We have to copy the data here, because DW_OP_call4 will only
16518 use a DW_AT_location attribute. */
16519 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16520 ptr += DW_BLOCK (member_loc)->size;
16521 }
16522
16523 *ptr++ = DW_OP_plus;
16524 gdb_assert (ptr - baton->data == baton->size);
16525
16526 SYMBOL_LOCATION_BATON (sym) = baton;
16527 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16528 }
16529
16530 /* Create appropriate locally-scoped variables for all the
16531 DW_TAG_common_block entries. Also create a struct common_block
16532 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16533 is used to sepate the common blocks name namespace from regular
16534 variable names. */
16535
16536 static void
16537 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16538 {
16539 struct attribute *attr;
16540
16541 attr = dwarf2_attr (die, DW_AT_location, cu);
16542 if (attr)
16543 {
16544 /* Support the .debug_loc offsets. */
16545 if (attr_form_is_block (attr))
16546 {
16547 /* Ok. */
16548 }
16549 else if (attr_form_is_section_offset (attr))
16550 {
16551 dwarf2_complex_location_expr_complaint ();
16552 attr = NULL;
16553 }
16554 else
16555 {
16556 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16557 "common block member");
16558 attr = NULL;
16559 }
16560 }
16561
16562 if (die->child != NULL)
16563 {
16564 struct objfile *objfile = cu->objfile;
16565 struct die_info *child_die;
16566 size_t n_entries = 0, size;
16567 struct common_block *common_block;
16568 struct symbol *sym;
16569
16570 for (child_die = die->child;
16571 child_die && child_die->tag;
16572 child_die = sibling_die (child_die))
16573 ++n_entries;
16574
16575 size = (sizeof (struct common_block)
16576 + (n_entries - 1) * sizeof (struct symbol *));
16577 common_block
16578 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16579 size);
16580 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16581 common_block->n_entries = 0;
16582
16583 for (child_die = die->child;
16584 child_die && child_die->tag;
16585 child_die = sibling_die (child_die))
16586 {
16587 /* Create the symbol in the DW_TAG_common_block block in the current
16588 symbol scope. */
16589 sym = new_symbol (child_die, NULL, cu);
16590 if (sym != NULL)
16591 {
16592 struct attribute *member_loc;
16593
16594 common_block->contents[common_block->n_entries++] = sym;
16595
16596 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16597 cu);
16598 if (member_loc)
16599 {
16600 /* GDB has handled this for a long time, but it is
16601 not specified by DWARF. It seems to have been
16602 emitted by gfortran at least as recently as:
16603 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16604 complaint (&symfile_complaints,
16605 _("Variable in common block has "
16606 "DW_AT_data_member_location "
16607 "- DIE at 0x%x [in module %s]"),
16608 to_underlying (child_die->sect_off),
16609 objfile_name (cu->objfile));
16610
16611 if (attr_form_is_section_offset (member_loc))
16612 dwarf2_complex_location_expr_complaint ();
16613 else if (attr_form_is_constant (member_loc)
16614 || attr_form_is_block (member_loc))
16615 {
16616 if (attr)
16617 mark_common_block_symbol_computed (sym, die, attr,
16618 member_loc, cu);
16619 }
16620 else
16621 dwarf2_complex_location_expr_complaint ();
16622 }
16623 }
16624 }
16625
16626 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16627 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16628 }
16629 }
16630
16631 /* Create a type for a C++ namespace. */
16632
16633 static struct type *
16634 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16635 {
16636 struct objfile *objfile = cu->objfile;
16637 const char *previous_prefix, *name;
16638 int is_anonymous;
16639 struct type *type;
16640
16641 /* For extensions, reuse the type of the original namespace. */
16642 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16643 {
16644 struct die_info *ext_die;
16645 struct dwarf2_cu *ext_cu = cu;
16646
16647 ext_die = dwarf2_extension (die, &ext_cu);
16648 type = read_type_die (ext_die, ext_cu);
16649
16650 /* EXT_CU may not be the same as CU.
16651 Ensure TYPE is recorded with CU in die_type_hash. */
16652 return set_die_type (die, type, cu);
16653 }
16654
16655 name = namespace_name (die, &is_anonymous, cu);
16656
16657 /* Now build the name of the current namespace. */
16658
16659 previous_prefix = determine_prefix (die, cu);
16660 if (previous_prefix[0] != '\0')
16661 name = typename_concat (&objfile->objfile_obstack,
16662 previous_prefix, name, 0, cu);
16663
16664 /* Create the type. */
16665 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16666 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16667
16668 return set_die_type (die, type, cu);
16669 }
16670
16671 /* Read a namespace scope. */
16672
16673 static void
16674 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16675 {
16676 struct objfile *objfile = cu->objfile;
16677 int is_anonymous;
16678
16679 /* Add a symbol associated to this if we haven't seen the namespace
16680 before. Also, add a using directive if it's an anonymous
16681 namespace. */
16682
16683 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16684 {
16685 struct type *type;
16686
16687 type = read_type_die (die, cu);
16688 new_symbol (die, type, cu);
16689
16690 namespace_name (die, &is_anonymous, cu);
16691 if (is_anonymous)
16692 {
16693 const char *previous_prefix = determine_prefix (die, cu);
16694
16695 std::vector<const char *> excludes;
16696 add_using_directive (using_directives (cu->language),
16697 previous_prefix, TYPE_NAME (type), NULL,
16698 NULL, excludes, 0, &objfile->objfile_obstack);
16699 }
16700 }
16701
16702 if (die->child != NULL)
16703 {
16704 struct die_info *child_die = die->child;
16705
16706 while (child_die && child_die->tag)
16707 {
16708 process_die (child_die, cu);
16709 child_die = sibling_die (child_die);
16710 }
16711 }
16712 }
16713
16714 /* Read a Fortran module as type. This DIE can be only a declaration used for
16715 imported module. Still we need that type as local Fortran "use ... only"
16716 declaration imports depend on the created type in determine_prefix. */
16717
16718 static struct type *
16719 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16720 {
16721 struct objfile *objfile = cu->objfile;
16722 const char *module_name;
16723 struct type *type;
16724
16725 module_name = dwarf2_name (die, cu);
16726 if (!module_name)
16727 complaint (&symfile_complaints,
16728 _("DW_TAG_module has no name, offset 0x%x"),
16729 to_underlying (die->sect_off));
16730 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16731
16732 /* determine_prefix uses TYPE_TAG_NAME. */
16733 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16734
16735 return set_die_type (die, type, cu);
16736 }
16737
16738 /* Read a Fortran module. */
16739
16740 static void
16741 read_module (struct die_info *die, struct dwarf2_cu *cu)
16742 {
16743 struct die_info *child_die = die->child;
16744 struct type *type;
16745
16746 type = read_type_die (die, cu);
16747 new_symbol (die, type, cu);
16748
16749 while (child_die && child_die->tag)
16750 {
16751 process_die (child_die, cu);
16752 child_die = sibling_die (child_die);
16753 }
16754 }
16755
16756 /* Return the name of the namespace represented by DIE. Set
16757 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16758 namespace. */
16759
16760 static const char *
16761 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16762 {
16763 struct die_info *current_die;
16764 const char *name = NULL;
16765
16766 /* Loop through the extensions until we find a name. */
16767
16768 for (current_die = die;
16769 current_die != NULL;
16770 current_die = dwarf2_extension (die, &cu))
16771 {
16772 /* We don't use dwarf2_name here so that we can detect the absence
16773 of a name -> anonymous namespace. */
16774 name = dwarf2_string_attr (die, DW_AT_name, cu);
16775
16776 if (name != NULL)
16777 break;
16778 }
16779
16780 /* Is it an anonymous namespace? */
16781
16782 *is_anonymous = (name == NULL);
16783 if (*is_anonymous)
16784 name = CP_ANONYMOUS_NAMESPACE_STR;
16785
16786 return name;
16787 }
16788
16789 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16790 the user defined type vector. */
16791
16792 static struct type *
16793 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16794 {
16795 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
16796 struct comp_unit_head *cu_header = &cu->header;
16797 struct type *type;
16798 struct attribute *attr_byte_size;
16799 struct attribute *attr_address_class;
16800 int byte_size, addr_class;
16801 struct type *target_type;
16802
16803 target_type = die_type (die, cu);
16804
16805 /* The die_type call above may have already set the type for this DIE. */
16806 type = get_die_type (die, cu);
16807 if (type)
16808 return type;
16809
16810 type = lookup_pointer_type (target_type);
16811
16812 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16813 if (attr_byte_size)
16814 byte_size = DW_UNSND (attr_byte_size);
16815 else
16816 byte_size = cu_header->addr_size;
16817
16818 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16819 if (attr_address_class)
16820 addr_class = DW_UNSND (attr_address_class);
16821 else
16822 addr_class = DW_ADDR_none;
16823
16824 /* If the pointer size or address class is different than the
16825 default, create a type variant marked as such and set the
16826 length accordingly. */
16827 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
16828 {
16829 if (gdbarch_address_class_type_flags_p (gdbarch))
16830 {
16831 int type_flags;
16832
16833 type_flags = gdbarch_address_class_type_flags
16834 (gdbarch, byte_size, addr_class);
16835 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16836 == 0);
16837 type = make_type_with_address_space (type, type_flags);
16838 }
16839 else if (TYPE_LENGTH (type) != byte_size)
16840 {
16841 complaint (&symfile_complaints,
16842 _("invalid pointer size %d"), byte_size);
16843 }
16844 else
16845 {
16846 /* Should we also complain about unhandled address classes? */
16847 }
16848 }
16849
16850 TYPE_LENGTH (type) = byte_size;
16851 return set_die_type (die, type, cu);
16852 }
16853
16854 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16855 the user defined type vector. */
16856
16857 static struct type *
16858 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16859 {
16860 struct type *type;
16861 struct type *to_type;
16862 struct type *domain;
16863
16864 to_type = die_type (die, cu);
16865 domain = die_containing_type (die, cu);
16866
16867 /* The calls above may have already set the type for this DIE. */
16868 type = get_die_type (die, cu);
16869 if (type)
16870 return type;
16871
16872 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16873 type = lookup_methodptr_type (to_type);
16874 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16875 {
16876 struct type *new_type = alloc_type (cu->objfile);
16877
16878 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16879 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16880 TYPE_VARARGS (to_type));
16881 type = lookup_methodptr_type (new_type);
16882 }
16883 else
16884 type = lookup_memberptr_type (to_type, domain);
16885
16886 return set_die_type (die, type, cu);
16887 }
16888
16889 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16890 the user defined type vector. */
16891
16892 static struct type *
16893 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16894 enum type_code refcode)
16895 {
16896 struct comp_unit_head *cu_header = &cu->header;
16897 struct type *type, *target_type;
16898 struct attribute *attr;
16899
16900 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16901
16902 target_type = die_type (die, cu);
16903
16904 /* The die_type call above may have already set the type for this DIE. */
16905 type = get_die_type (die, cu);
16906 if (type)
16907 return type;
16908
16909 type = lookup_reference_type (target_type, refcode);
16910 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16911 if (attr)
16912 {
16913 TYPE_LENGTH (type) = DW_UNSND (attr);
16914 }
16915 else
16916 {
16917 TYPE_LENGTH (type) = cu_header->addr_size;
16918 }
16919 return set_die_type (die, type, cu);
16920 }
16921
16922 /* Add the given cv-qualifiers to the element type of the array. GCC
16923 outputs DWARF type qualifiers that apply to an array, not the
16924 element type. But GDB relies on the array element type to carry
16925 the cv-qualifiers. This mimics section 6.7.3 of the C99
16926 specification. */
16927
16928 static struct type *
16929 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16930 struct type *base_type, int cnst, int voltl)
16931 {
16932 struct type *el_type, *inner_array;
16933
16934 base_type = copy_type (base_type);
16935 inner_array = base_type;
16936
16937 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16938 {
16939 TYPE_TARGET_TYPE (inner_array) =
16940 copy_type (TYPE_TARGET_TYPE (inner_array));
16941 inner_array = TYPE_TARGET_TYPE (inner_array);
16942 }
16943
16944 el_type = TYPE_TARGET_TYPE (inner_array);
16945 cnst |= TYPE_CONST (el_type);
16946 voltl |= TYPE_VOLATILE (el_type);
16947 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16948
16949 return set_die_type (die, base_type, cu);
16950 }
16951
16952 static struct type *
16953 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16954 {
16955 struct type *base_type, *cv_type;
16956
16957 base_type = die_type (die, cu);
16958
16959 /* The die_type call above may have already set the type for this DIE. */
16960 cv_type = get_die_type (die, cu);
16961 if (cv_type)
16962 return cv_type;
16963
16964 /* In case the const qualifier is applied to an array type, the element type
16965 is so qualified, not the array type (section 6.7.3 of C99). */
16966 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16967 return add_array_cv_type (die, cu, base_type, 1, 0);
16968
16969 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16970 return set_die_type (die, cv_type, cu);
16971 }
16972
16973 static struct type *
16974 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16975 {
16976 struct type *base_type, *cv_type;
16977
16978 base_type = die_type (die, cu);
16979
16980 /* The die_type call above may have already set the type for this DIE. */
16981 cv_type = get_die_type (die, cu);
16982 if (cv_type)
16983 return cv_type;
16984
16985 /* In case the volatile qualifier is applied to an array type, the
16986 element type is so qualified, not the array type (section 6.7.3
16987 of C99). */
16988 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16989 return add_array_cv_type (die, cu, base_type, 0, 1);
16990
16991 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16992 return set_die_type (die, cv_type, cu);
16993 }
16994
16995 /* Handle DW_TAG_restrict_type. */
16996
16997 static struct type *
16998 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16999 {
17000 struct type *base_type, *cv_type;
17001
17002 base_type = die_type (die, cu);
17003
17004 /* The die_type call above may have already set the type for this DIE. */
17005 cv_type = get_die_type (die, cu);
17006 if (cv_type)
17007 return cv_type;
17008
17009 cv_type = make_restrict_type (base_type);
17010 return set_die_type (die, cv_type, cu);
17011 }
17012
17013 /* Handle DW_TAG_atomic_type. */
17014
17015 static struct type *
17016 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17017 {
17018 struct type *base_type, *cv_type;
17019
17020 base_type = die_type (die, cu);
17021
17022 /* The die_type call above may have already set the type for this DIE. */
17023 cv_type = get_die_type (die, cu);
17024 if (cv_type)
17025 return cv_type;
17026
17027 cv_type = make_atomic_type (base_type);
17028 return set_die_type (die, cv_type, cu);
17029 }
17030
17031 /* Extract all information from a DW_TAG_string_type DIE and add to
17032 the user defined type vector. It isn't really a user defined type,
17033 but it behaves like one, with other DIE's using an AT_user_def_type
17034 attribute to reference it. */
17035
17036 static struct type *
17037 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17038 {
17039 struct objfile *objfile = cu->objfile;
17040 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17041 struct type *type, *range_type, *index_type, *char_type;
17042 struct attribute *attr;
17043 unsigned int length;
17044
17045 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17046 if (attr)
17047 {
17048 length = DW_UNSND (attr);
17049 }
17050 else
17051 {
17052 /* Check for the DW_AT_byte_size attribute. */
17053 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17054 if (attr)
17055 {
17056 length = DW_UNSND (attr);
17057 }
17058 else
17059 {
17060 length = 1;
17061 }
17062 }
17063
17064 index_type = objfile_type (objfile)->builtin_int;
17065 range_type = create_static_range_type (NULL, index_type, 1, length);
17066 char_type = language_string_char_type (cu->language_defn, gdbarch);
17067 type = create_string_type (NULL, char_type, range_type);
17068
17069 return set_die_type (die, type, cu);
17070 }
17071
17072 /* Assuming that DIE corresponds to a function, returns nonzero
17073 if the function is prototyped. */
17074
17075 static int
17076 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17077 {
17078 struct attribute *attr;
17079
17080 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17081 if (attr && (DW_UNSND (attr) != 0))
17082 return 1;
17083
17084 /* The DWARF standard implies that the DW_AT_prototyped attribute
17085 is only meaninful for C, but the concept also extends to other
17086 languages that allow unprototyped functions (Eg: Objective C).
17087 For all other languages, assume that functions are always
17088 prototyped. */
17089 if (cu->language != language_c
17090 && cu->language != language_objc
17091 && cu->language != language_opencl)
17092 return 1;
17093
17094 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17095 prototyped and unprototyped functions; default to prototyped,
17096 since that is more common in modern code (and RealView warns
17097 about unprototyped functions). */
17098 if (producer_is_realview (cu->producer))
17099 return 1;
17100
17101 return 0;
17102 }
17103
17104 /* Handle DIES due to C code like:
17105
17106 struct foo
17107 {
17108 int (*funcp)(int a, long l);
17109 int b;
17110 };
17111
17112 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17113
17114 static struct type *
17115 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17116 {
17117 struct objfile *objfile = cu->objfile;
17118 struct type *type; /* Type that this function returns. */
17119 struct type *ftype; /* Function that returns above type. */
17120 struct attribute *attr;
17121
17122 type = die_type (die, cu);
17123
17124 /* The die_type call above may have already set the type for this DIE. */
17125 ftype = get_die_type (die, cu);
17126 if (ftype)
17127 return ftype;
17128
17129 ftype = lookup_function_type (type);
17130
17131 if (prototyped_function_p (die, cu))
17132 TYPE_PROTOTYPED (ftype) = 1;
17133
17134 /* Store the calling convention in the type if it's available in
17135 the subroutine die. Otherwise set the calling convention to
17136 the default value DW_CC_normal. */
17137 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17138 if (attr)
17139 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17140 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17141 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17142 else
17143 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17144
17145 /* Record whether the function returns normally to its caller or not
17146 if the DWARF producer set that information. */
17147 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17148 if (attr && (DW_UNSND (attr) != 0))
17149 TYPE_NO_RETURN (ftype) = 1;
17150
17151 /* We need to add the subroutine type to the die immediately so
17152 we don't infinitely recurse when dealing with parameters
17153 declared as the same subroutine type. */
17154 set_die_type (die, ftype, cu);
17155
17156 if (die->child != NULL)
17157 {
17158 struct type *void_type = objfile_type (objfile)->builtin_void;
17159 struct die_info *child_die;
17160 int nparams, iparams;
17161
17162 /* Count the number of parameters.
17163 FIXME: GDB currently ignores vararg functions, but knows about
17164 vararg member functions. */
17165 nparams = 0;
17166 child_die = die->child;
17167 while (child_die && child_die->tag)
17168 {
17169 if (child_die->tag == DW_TAG_formal_parameter)
17170 nparams++;
17171 else if (child_die->tag == DW_TAG_unspecified_parameters)
17172 TYPE_VARARGS (ftype) = 1;
17173 child_die = sibling_die (child_die);
17174 }
17175
17176 /* Allocate storage for parameters and fill them in. */
17177 TYPE_NFIELDS (ftype) = nparams;
17178 TYPE_FIELDS (ftype) = (struct field *)
17179 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17180
17181 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17182 even if we error out during the parameters reading below. */
17183 for (iparams = 0; iparams < nparams; iparams++)
17184 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17185
17186 iparams = 0;
17187 child_die = die->child;
17188 while (child_die && child_die->tag)
17189 {
17190 if (child_die->tag == DW_TAG_formal_parameter)
17191 {
17192 struct type *arg_type;
17193
17194 /* DWARF version 2 has no clean way to discern C++
17195 static and non-static member functions. G++ helps
17196 GDB by marking the first parameter for non-static
17197 member functions (which is the this pointer) as
17198 artificial. We pass this information to
17199 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17200
17201 DWARF version 3 added DW_AT_object_pointer, which GCC
17202 4.5 does not yet generate. */
17203 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17204 if (attr)
17205 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17206 else
17207 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17208 arg_type = die_type (child_die, cu);
17209
17210 /* RealView does not mark THIS as const, which the testsuite
17211 expects. GCC marks THIS as const in method definitions,
17212 but not in the class specifications (GCC PR 43053). */
17213 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17214 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17215 {
17216 int is_this = 0;
17217 struct dwarf2_cu *arg_cu = cu;
17218 const char *name = dwarf2_name (child_die, cu);
17219
17220 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17221 if (attr)
17222 {
17223 /* If the compiler emits this, use it. */
17224 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17225 is_this = 1;
17226 }
17227 else if (name && strcmp (name, "this") == 0)
17228 /* Function definitions will have the argument names. */
17229 is_this = 1;
17230 else if (name == NULL && iparams == 0)
17231 /* Declarations may not have the names, so like
17232 elsewhere in GDB, assume an artificial first
17233 argument is "this". */
17234 is_this = 1;
17235
17236 if (is_this)
17237 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17238 arg_type, 0);
17239 }
17240
17241 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17242 iparams++;
17243 }
17244 child_die = sibling_die (child_die);
17245 }
17246 }
17247
17248 return ftype;
17249 }
17250
17251 static struct type *
17252 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17253 {
17254 struct objfile *objfile = cu->objfile;
17255 const char *name = NULL;
17256 struct type *this_type, *target_type;
17257
17258 name = dwarf2_full_name (NULL, die, cu);
17259 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17260 TYPE_TARGET_STUB (this_type) = 1;
17261 set_die_type (die, this_type, cu);
17262 target_type = die_type (die, cu);
17263 if (target_type != this_type)
17264 TYPE_TARGET_TYPE (this_type) = target_type;
17265 else
17266 {
17267 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17268 spec and cause infinite loops in GDB. */
17269 complaint (&symfile_complaints,
17270 _("Self-referential DW_TAG_typedef "
17271 "- DIE at 0x%x [in module %s]"),
17272 to_underlying (die->sect_off), objfile_name (objfile));
17273 TYPE_TARGET_TYPE (this_type) = NULL;
17274 }
17275 return this_type;
17276 }
17277
17278 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17279 (which may be different from NAME) to the architecture back-end to allow
17280 it to guess the correct format if necessary. */
17281
17282 static struct type *
17283 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17284 const char *name_hint)
17285 {
17286 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17287 const struct floatformat **format;
17288 struct type *type;
17289
17290 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17291 if (format)
17292 type = init_float_type (objfile, bits, name, format);
17293 else
17294 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17295
17296 return type;
17297 }
17298
17299 /* Find a representation of a given base type and install
17300 it in the TYPE field of the die. */
17301
17302 static struct type *
17303 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17304 {
17305 struct objfile *objfile = cu->objfile;
17306 struct type *type;
17307 struct attribute *attr;
17308 int encoding = 0, bits = 0;
17309 const char *name;
17310
17311 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17312 if (attr)
17313 {
17314 encoding = DW_UNSND (attr);
17315 }
17316 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17317 if (attr)
17318 {
17319 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17320 }
17321 name = dwarf2_name (die, cu);
17322 if (!name)
17323 {
17324 complaint (&symfile_complaints,
17325 _("DW_AT_name missing from DW_TAG_base_type"));
17326 }
17327
17328 switch (encoding)
17329 {
17330 case DW_ATE_address:
17331 /* Turn DW_ATE_address into a void * pointer. */
17332 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17333 type = init_pointer_type (objfile, bits, name, type);
17334 break;
17335 case DW_ATE_boolean:
17336 type = init_boolean_type (objfile, bits, 1, name);
17337 break;
17338 case DW_ATE_complex_float:
17339 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17340 type = init_complex_type (objfile, name, type);
17341 break;
17342 case DW_ATE_decimal_float:
17343 type = init_decfloat_type (objfile, bits, name);
17344 break;
17345 case DW_ATE_float:
17346 type = dwarf2_init_float_type (objfile, bits, name, name);
17347 break;
17348 case DW_ATE_signed:
17349 type = init_integer_type (objfile, bits, 0, name);
17350 break;
17351 case DW_ATE_unsigned:
17352 if (cu->language == language_fortran
17353 && name
17354 && startswith (name, "character("))
17355 type = init_character_type (objfile, bits, 1, name);
17356 else
17357 type = init_integer_type (objfile, bits, 1, name);
17358 break;
17359 case DW_ATE_signed_char:
17360 if (cu->language == language_ada || cu->language == language_m2
17361 || cu->language == language_pascal
17362 || cu->language == language_fortran)
17363 type = init_character_type (objfile, bits, 0, name);
17364 else
17365 type = init_integer_type (objfile, bits, 0, name);
17366 break;
17367 case DW_ATE_unsigned_char:
17368 if (cu->language == language_ada || cu->language == language_m2
17369 || cu->language == language_pascal
17370 || cu->language == language_fortran
17371 || cu->language == language_rust)
17372 type = init_character_type (objfile, bits, 1, name);
17373 else
17374 type = init_integer_type (objfile, bits, 1, name);
17375 break;
17376 case DW_ATE_UTF:
17377 {
17378 gdbarch *arch = get_objfile_arch (objfile);
17379
17380 if (bits == 16)
17381 type = builtin_type (arch)->builtin_char16;
17382 else if (bits == 32)
17383 type = builtin_type (arch)->builtin_char32;
17384 else
17385 {
17386 complaint (&symfile_complaints,
17387 _("unsupported DW_ATE_UTF bit size: '%d'"),
17388 bits);
17389 type = init_integer_type (objfile, bits, 1, name);
17390 }
17391 return set_die_type (die, type, cu);
17392 }
17393 break;
17394
17395 default:
17396 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17397 dwarf_type_encoding_name (encoding));
17398 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17399 break;
17400 }
17401
17402 if (name && strcmp (name, "char") == 0)
17403 TYPE_NOSIGN (type) = 1;
17404
17405 return set_die_type (die, type, cu);
17406 }
17407
17408 /* Parse dwarf attribute if it's a block, reference or constant and put the
17409 resulting value of the attribute into struct bound_prop.
17410 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17411
17412 static int
17413 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17414 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17415 {
17416 struct dwarf2_property_baton *baton;
17417 struct obstack *obstack = &cu->objfile->objfile_obstack;
17418
17419 if (attr == NULL || prop == NULL)
17420 return 0;
17421
17422 if (attr_form_is_block (attr))
17423 {
17424 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17425 baton->referenced_type = NULL;
17426 baton->locexpr.per_cu = cu->per_cu;
17427 baton->locexpr.size = DW_BLOCK (attr)->size;
17428 baton->locexpr.data = DW_BLOCK (attr)->data;
17429 prop->data.baton = baton;
17430 prop->kind = PROP_LOCEXPR;
17431 gdb_assert (prop->data.baton != NULL);
17432 }
17433 else if (attr_form_is_ref (attr))
17434 {
17435 struct dwarf2_cu *target_cu = cu;
17436 struct die_info *target_die;
17437 struct attribute *target_attr;
17438
17439 target_die = follow_die_ref (die, attr, &target_cu);
17440 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17441 if (target_attr == NULL)
17442 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17443 target_cu);
17444 if (target_attr == NULL)
17445 return 0;
17446
17447 switch (target_attr->name)
17448 {
17449 case DW_AT_location:
17450 if (attr_form_is_section_offset (target_attr))
17451 {
17452 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17453 baton->referenced_type = die_type (target_die, target_cu);
17454 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17455 prop->data.baton = baton;
17456 prop->kind = PROP_LOCLIST;
17457 gdb_assert (prop->data.baton != NULL);
17458 }
17459 else if (attr_form_is_block (target_attr))
17460 {
17461 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17462 baton->referenced_type = die_type (target_die, target_cu);
17463 baton->locexpr.per_cu = cu->per_cu;
17464 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17465 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17466 prop->data.baton = baton;
17467 prop->kind = PROP_LOCEXPR;
17468 gdb_assert (prop->data.baton != NULL);
17469 }
17470 else
17471 {
17472 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17473 "dynamic property");
17474 return 0;
17475 }
17476 break;
17477 case DW_AT_data_member_location:
17478 {
17479 LONGEST offset;
17480
17481 if (!handle_data_member_location (target_die, target_cu,
17482 &offset))
17483 return 0;
17484
17485 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17486 baton->referenced_type = read_type_die (target_die->parent,
17487 target_cu);
17488 baton->offset_info.offset = offset;
17489 baton->offset_info.type = die_type (target_die, target_cu);
17490 prop->data.baton = baton;
17491 prop->kind = PROP_ADDR_OFFSET;
17492 break;
17493 }
17494 }
17495 }
17496 else if (attr_form_is_constant (attr))
17497 {
17498 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17499 prop->kind = PROP_CONST;
17500 }
17501 else
17502 {
17503 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17504 dwarf2_name (die, cu));
17505 return 0;
17506 }
17507
17508 return 1;
17509 }
17510
17511 /* Read the given DW_AT_subrange DIE. */
17512
17513 static struct type *
17514 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17515 {
17516 struct type *base_type, *orig_base_type;
17517 struct type *range_type;
17518 struct attribute *attr;
17519 struct dynamic_prop low, high;
17520 int low_default_is_valid;
17521 int high_bound_is_count = 0;
17522 const char *name;
17523 LONGEST negative_mask;
17524
17525 orig_base_type = die_type (die, cu);
17526 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17527 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17528 creating the range type, but we use the result of check_typedef
17529 when examining properties of the type. */
17530 base_type = check_typedef (orig_base_type);
17531
17532 /* The die_type call above may have already set the type for this DIE. */
17533 range_type = get_die_type (die, cu);
17534 if (range_type)
17535 return range_type;
17536
17537 low.kind = PROP_CONST;
17538 high.kind = PROP_CONST;
17539 high.data.const_val = 0;
17540
17541 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17542 omitting DW_AT_lower_bound. */
17543 switch (cu->language)
17544 {
17545 case language_c:
17546 case language_cplus:
17547 low.data.const_val = 0;
17548 low_default_is_valid = 1;
17549 break;
17550 case language_fortran:
17551 low.data.const_val = 1;
17552 low_default_is_valid = 1;
17553 break;
17554 case language_d:
17555 case language_objc:
17556 case language_rust:
17557 low.data.const_val = 0;
17558 low_default_is_valid = (cu->header.version >= 4);
17559 break;
17560 case language_ada:
17561 case language_m2:
17562 case language_pascal:
17563 low.data.const_val = 1;
17564 low_default_is_valid = (cu->header.version >= 4);
17565 break;
17566 default:
17567 low.data.const_val = 0;
17568 low_default_is_valid = 0;
17569 break;
17570 }
17571
17572 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17573 if (attr)
17574 attr_to_dynamic_prop (attr, die, cu, &low);
17575 else if (!low_default_is_valid)
17576 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17577 "- DIE at 0x%x [in module %s]"),
17578 to_underlying (die->sect_off), objfile_name (cu->objfile));
17579
17580 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
17581 if (!attr_to_dynamic_prop (attr, die, cu, &high))
17582 {
17583 attr = dwarf2_attr (die, DW_AT_count, cu);
17584 if (attr_to_dynamic_prop (attr, die, cu, &high))
17585 {
17586 /* If bounds are constant do the final calculation here. */
17587 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17588 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17589 else
17590 high_bound_is_count = 1;
17591 }
17592 }
17593
17594 /* Dwarf-2 specifications explicitly allows to create subrange types
17595 without specifying a base type.
17596 In that case, the base type must be set to the type of
17597 the lower bound, upper bound or count, in that order, if any of these
17598 three attributes references an object that has a type.
17599 If no base type is found, the Dwarf-2 specifications say that
17600 a signed integer type of size equal to the size of an address should
17601 be used.
17602 For the following C code: `extern char gdb_int [];'
17603 GCC produces an empty range DIE.
17604 FIXME: muller/2010-05-28: Possible references to object for low bound,
17605 high bound or count are not yet handled by this code. */
17606 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17607 {
17608 struct objfile *objfile = cu->objfile;
17609 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17610 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17611 struct type *int_type = objfile_type (objfile)->builtin_int;
17612
17613 /* Test "int", "long int", and "long long int" objfile types,
17614 and select the first one having a size above or equal to the
17615 architecture address size. */
17616 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17617 base_type = int_type;
17618 else
17619 {
17620 int_type = objfile_type (objfile)->builtin_long;
17621 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17622 base_type = int_type;
17623 else
17624 {
17625 int_type = objfile_type (objfile)->builtin_long_long;
17626 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17627 base_type = int_type;
17628 }
17629 }
17630 }
17631
17632 /* Normally, the DWARF producers are expected to use a signed
17633 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17634 But this is unfortunately not always the case, as witnessed
17635 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17636 is used instead. To work around that ambiguity, we treat
17637 the bounds as signed, and thus sign-extend their values, when
17638 the base type is signed. */
17639 negative_mask =
17640 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17641 if (low.kind == PROP_CONST
17642 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17643 low.data.const_val |= negative_mask;
17644 if (high.kind == PROP_CONST
17645 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17646 high.data.const_val |= negative_mask;
17647
17648 range_type = create_range_type (NULL, orig_base_type, &low, &high);
17649
17650 if (high_bound_is_count)
17651 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17652
17653 /* Ada expects an empty array on no boundary attributes. */
17654 if (attr == NULL && cu->language != language_ada)
17655 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17656
17657 name = dwarf2_name (die, cu);
17658 if (name)
17659 TYPE_NAME (range_type) = name;
17660
17661 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17662 if (attr)
17663 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17664
17665 set_die_type (die, range_type, cu);
17666
17667 /* set_die_type should be already done. */
17668 set_descriptive_type (range_type, die, cu);
17669
17670 return range_type;
17671 }
17672
17673 static struct type *
17674 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17675 {
17676 struct type *type;
17677
17678 /* For now, we only support the C meaning of an unspecified type: void. */
17679
17680 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
17681 TYPE_NAME (type) = dwarf2_name (die, cu);
17682
17683 return set_die_type (die, type, cu);
17684 }
17685
17686 /* Read a single die and all its descendents. Set the die's sibling
17687 field to NULL; set other fields in the die correctly, and set all
17688 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17689 location of the info_ptr after reading all of those dies. PARENT
17690 is the parent of the die in question. */
17691
17692 static struct die_info *
17693 read_die_and_children (const struct die_reader_specs *reader,
17694 const gdb_byte *info_ptr,
17695 const gdb_byte **new_info_ptr,
17696 struct die_info *parent)
17697 {
17698 struct die_info *die;
17699 const gdb_byte *cur_ptr;
17700 int has_children;
17701
17702 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17703 if (die == NULL)
17704 {
17705 *new_info_ptr = cur_ptr;
17706 return NULL;
17707 }
17708 store_in_ref_table (die, reader->cu);
17709
17710 if (has_children)
17711 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17712 else
17713 {
17714 die->child = NULL;
17715 *new_info_ptr = cur_ptr;
17716 }
17717
17718 die->sibling = NULL;
17719 die->parent = parent;
17720 return die;
17721 }
17722
17723 /* Read a die, all of its descendents, and all of its siblings; set
17724 all of the fields of all of the dies correctly. Arguments are as
17725 in read_die_and_children. */
17726
17727 static struct die_info *
17728 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17729 const gdb_byte *info_ptr,
17730 const gdb_byte **new_info_ptr,
17731 struct die_info *parent)
17732 {
17733 struct die_info *first_die, *last_sibling;
17734 const gdb_byte *cur_ptr;
17735
17736 cur_ptr = info_ptr;
17737 first_die = last_sibling = NULL;
17738
17739 while (1)
17740 {
17741 struct die_info *die
17742 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17743
17744 if (die == NULL)
17745 {
17746 *new_info_ptr = cur_ptr;
17747 return first_die;
17748 }
17749
17750 if (!first_die)
17751 first_die = die;
17752 else
17753 last_sibling->sibling = die;
17754
17755 last_sibling = die;
17756 }
17757 }
17758
17759 /* Read a die, all of its descendents, and all of its siblings; set
17760 all of the fields of all of the dies correctly. Arguments are as
17761 in read_die_and_children.
17762 This the main entry point for reading a DIE and all its children. */
17763
17764 static struct die_info *
17765 read_die_and_siblings (const struct die_reader_specs *reader,
17766 const gdb_byte *info_ptr,
17767 const gdb_byte **new_info_ptr,
17768 struct die_info *parent)
17769 {
17770 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17771 new_info_ptr, parent);
17772
17773 if (dwarf_die_debug)
17774 {
17775 fprintf_unfiltered (gdb_stdlog,
17776 "Read die from %s@0x%x of %s:\n",
17777 get_section_name (reader->die_section),
17778 (unsigned) (info_ptr - reader->die_section->buffer),
17779 bfd_get_filename (reader->abfd));
17780 dump_die (die, dwarf_die_debug);
17781 }
17782
17783 return die;
17784 }
17785
17786 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17787 attributes.
17788 The caller is responsible for filling in the extra attributes
17789 and updating (*DIEP)->num_attrs.
17790 Set DIEP to point to a newly allocated die with its information,
17791 except for its child, sibling, and parent fields.
17792 Set HAS_CHILDREN to tell whether the die has children or not. */
17793
17794 static const gdb_byte *
17795 read_full_die_1 (const struct die_reader_specs *reader,
17796 struct die_info **diep, const gdb_byte *info_ptr,
17797 int *has_children, int num_extra_attrs)
17798 {
17799 unsigned int abbrev_number, bytes_read, i;
17800 struct abbrev_info *abbrev;
17801 struct die_info *die;
17802 struct dwarf2_cu *cu = reader->cu;
17803 bfd *abfd = reader->abfd;
17804
17805 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17806 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17807 info_ptr += bytes_read;
17808 if (!abbrev_number)
17809 {
17810 *diep = NULL;
17811 *has_children = 0;
17812 return info_ptr;
17813 }
17814
17815 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
17816 if (!abbrev)
17817 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17818 abbrev_number,
17819 bfd_get_filename (abfd));
17820
17821 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17822 die->sect_off = sect_off;
17823 die->tag = abbrev->tag;
17824 die->abbrev = abbrev_number;
17825
17826 /* Make the result usable.
17827 The caller needs to update num_attrs after adding the extra
17828 attributes. */
17829 die->num_attrs = abbrev->num_attrs;
17830
17831 for (i = 0; i < abbrev->num_attrs; ++i)
17832 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17833 info_ptr);
17834
17835 *diep = die;
17836 *has_children = abbrev->has_children;
17837 return info_ptr;
17838 }
17839
17840 /* Read a die and all its attributes.
17841 Set DIEP to point to a newly allocated die with its information,
17842 except for its child, sibling, and parent fields.
17843 Set HAS_CHILDREN to tell whether the die has children or not. */
17844
17845 static const gdb_byte *
17846 read_full_die (const struct die_reader_specs *reader,
17847 struct die_info **diep, const gdb_byte *info_ptr,
17848 int *has_children)
17849 {
17850 const gdb_byte *result;
17851
17852 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17853
17854 if (dwarf_die_debug)
17855 {
17856 fprintf_unfiltered (gdb_stdlog,
17857 "Read die from %s@0x%x of %s:\n",
17858 get_section_name (reader->die_section),
17859 (unsigned) (info_ptr - reader->die_section->buffer),
17860 bfd_get_filename (reader->abfd));
17861 dump_die (*diep, dwarf_die_debug);
17862 }
17863
17864 return result;
17865 }
17866 \f
17867 /* Abbreviation tables.
17868
17869 In DWARF version 2, the description of the debugging information is
17870 stored in a separate .debug_abbrev section. Before we read any
17871 dies from a section we read in all abbreviations and install them
17872 in a hash table. */
17873
17874 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17875
17876 static struct abbrev_info *
17877 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
17878 {
17879 struct abbrev_info *abbrev;
17880
17881 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
17882 memset (abbrev, 0, sizeof (struct abbrev_info));
17883
17884 return abbrev;
17885 }
17886
17887 /* Add an abbreviation to the table. */
17888
17889 static void
17890 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
17891 unsigned int abbrev_number,
17892 struct abbrev_info *abbrev)
17893 {
17894 unsigned int hash_number;
17895
17896 hash_number = abbrev_number % ABBREV_HASH_SIZE;
17897 abbrev->next = abbrev_table->abbrevs[hash_number];
17898 abbrev_table->abbrevs[hash_number] = abbrev;
17899 }
17900
17901 /* Look up an abbrev in the table.
17902 Returns NULL if the abbrev is not found. */
17903
17904 static struct abbrev_info *
17905 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
17906 unsigned int abbrev_number)
17907 {
17908 unsigned int hash_number;
17909 struct abbrev_info *abbrev;
17910
17911 hash_number = abbrev_number % ABBREV_HASH_SIZE;
17912 abbrev = abbrev_table->abbrevs[hash_number];
17913
17914 while (abbrev)
17915 {
17916 if (abbrev->number == abbrev_number)
17917 return abbrev;
17918 abbrev = abbrev->next;
17919 }
17920 return NULL;
17921 }
17922
17923 /* Read in an abbrev table. */
17924
17925 static struct abbrev_table *
17926 abbrev_table_read_table (struct dwarf2_section_info *section,
17927 sect_offset sect_off)
17928 {
17929 struct objfile *objfile = dwarf2_per_objfile->objfile;
17930 bfd *abfd = get_section_bfd_owner (section);
17931 struct abbrev_table *abbrev_table;
17932 const gdb_byte *abbrev_ptr;
17933 struct abbrev_info *cur_abbrev;
17934 unsigned int abbrev_number, bytes_read, abbrev_name;
17935 unsigned int abbrev_form;
17936 struct attr_abbrev *cur_attrs;
17937 unsigned int allocated_attrs;
17938
17939 abbrev_table = XNEW (struct abbrev_table);
17940 abbrev_table->sect_off = sect_off;
17941 obstack_init (&abbrev_table->abbrev_obstack);
17942 abbrev_table->abbrevs =
17943 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
17944 ABBREV_HASH_SIZE);
17945 memset (abbrev_table->abbrevs, 0,
17946 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
17947
17948 dwarf2_read_section (objfile, section);
17949 abbrev_ptr = section->buffer + to_underlying (sect_off);
17950 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17951 abbrev_ptr += bytes_read;
17952
17953 allocated_attrs = ATTR_ALLOC_CHUNK;
17954 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
17955
17956 /* Loop until we reach an abbrev number of 0. */
17957 while (abbrev_number)
17958 {
17959 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
17960
17961 /* read in abbrev header */
17962 cur_abbrev->number = abbrev_number;
17963 cur_abbrev->tag
17964 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17965 abbrev_ptr += bytes_read;
17966 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
17967 abbrev_ptr += 1;
17968
17969 /* now read in declarations */
17970 for (;;)
17971 {
17972 LONGEST implicit_const;
17973
17974 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17975 abbrev_ptr += bytes_read;
17976 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17977 abbrev_ptr += bytes_read;
17978 if (abbrev_form == DW_FORM_implicit_const)
17979 {
17980 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
17981 &bytes_read);
17982 abbrev_ptr += bytes_read;
17983 }
17984 else
17985 {
17986 /* Initialize it due to a false compiler warning. */
17987 implicit_const = -1;
17988 }
17989
17990 if (abbrev_name == 0)
17991 break;
17992
17993 if (cur_abbrev->num_attrs == allocated_attrs)
17994 {
17995 allocated_attrs += ATTR_ALLOC_CHUNK;
17996 cur_attrs
17997 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
17998 }
17999
18000 cur_attrs[cur_abbrev->num_attrs].name
18001 = (enum dwarf_attribute) abbrev_name;
18002 cur_attrs[cur_abbrev->num_attrs].form
18003 = (enum dwarf_form) abbrev_form;
18004 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18005 ++cur_abbrev->num_attrs;
18006 }
18007
18008 cur_abbrev->attrs =
18009 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18010 cur_abbrev->num_attrs);
18011 memcpy (cur_abbrev->attrs, cur_attrs,
18012 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18013
18014 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
18015
18016 /* Get next abbreviation.
18017 Under Irix6 the abbreviations for a compilation unit are not
18018 always properly terminated with an abbrev number of 0.
18019 Exit loop if we encounter an abbreviation which we have
18020 already read (which means we are about to read the abbreviations
18021 for the next compile unit) or if the end of the abbreviation
18022 table is reached. */
18023 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18024 break;
18025 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18026 abbrev_ptr += bytes_read;
18027 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
18028 break;
18029 }
18030
18031 xfree (cur_attrs);
18032 return abbrev_table;
18033 }
18034
18035 /* Free the resources held by ABBREV_TABLE. */
18036
18037 static void
18038 abbrev_table_free (struct abbrev_table *abbrev_table)
18039 {
18040 obstack_free (&abbrev_table->abbrev_obstack, NULL);
18041 xfree (abbrev_table);
18042 }
18043
18044 /* Same as abbrev_table_free but as a cleanup.
18045 We pass in a pointer to the pointer to the table so that we can
18046 set the pointer to NULL when we're done. It also simplifies
18047 build_type_psymtabs_1. */
18048
18049 static void
18050 abbrev_table_free_cleanup (void *table_ptr)
18051 {
18052 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
18053
18054 if (*abbrev_table_ptr != NULL)
18055 abbrev_table_free (*abbrev_table_ptr);
18056 *abbrev_table_ptr = NULL;
18057 }
18058
18059 /* Read the abbrev table for CU from ABBREV_SECTION. */
18060
18061 static void
18062 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
18063 struct dwarf2_section_info *abbrev_section)
18064 {
18065 cu->abbrev_table =
18066 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
18067 }
18068
18069 /* Release the memory used by the abbrev table for a compilation unit. */
18070
18071 static void
18072 dwarf2_free_abbrev_table (void *ptr_to_cu)
18073 {
18074 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
18075
18076 if (cu->abbrev_table != NULL)
18077 abbrev_table_free (cu->abbrev_table);
18078 /* Set this to NULL so that we SEGV if we try to read it later,
18079 and also because free_comp_unit verifies this is NULL. */
18080 cu->abbrev_table = NULL;
18081 }
18082 \f
18083 /* Returns nonzero if TAG represents a type that we might generate a partial
18084 symbol for. */
18085
18086 static int
18087 is_type_tag_for_partial (int tag)
18088 {
18089 switch (tag)
18090 {
18091 #if 0
18092 /* Some types that would be reasonable to generate partial symbols for,
18093 that we don't at present. */
18094 case DW_TAG_array_type:
18095 case DW_TAG_file_type:
18096 case DW_TAG_ptr_to_member_type:
18097 case DW_TAG_set_type:
18098 case DW_TAG_string_type:
18099 case DW_TAG_subroutine_type:
18100 #endif
18101 case DW_TAG_base_type:
18102 case DW_TAG_class_type:
18103 case DW_TAG_interface_type:
18104 case DW_TAG_enumeration_type:
18105 case DW_TAG_structure_type:
18106 case DW_TAG_subrange_type:
18107 case DW_TAG_typedef:
18108 case DW_TAG_union_type:
18109 return 1;
18110 default:
18111 return 0;
18112 }
18113 }
18114
18115 /* Load all DIEs that are interesting for partial symbols into memory. */
18116
18117 static struct partial_die_info *
18118 load_partial_dies (const struct die_reader_specs *reader,
18119 const gdb_byte *info_ptr, int building_psymtab)
18120 {
18121 struct dwarf2_cu *cu = reader->cu;
18122 struct objfile *objfile = cu->objfile;
18123 struct partial_die_info *part_die;
18124 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18125 struct abbrev_info *abbrev;
18126 unsigned int bytes_read;
18127 unsigned int load_all = 0;
18128 int nesting_level = 1;
18129
18130 parent_die = NULL;
18131 last_die = NULL;
18132
18133 gdb_assert (cu->per_cu != NULL);
18134 if (cu->per_cu->load_all_dies)
18135 load_all = 1;
18136
18137 cu->partial_dies
18138 = htab_create_alloc_ex (cu->header.length / 12,
18139 partial_die_hash,
18140 partial_die_eq,
18141 NULL,
18142 &cu->comp_unit_obstack,
18143 hashtab_obstack_allocate,
18144 dummy_obstack_deallocate);
18145
18146 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
18147
18148 while (1)
18149 {
18150 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
18151
18152 /* A NULL abbrev means the end of a series of children. */
18153 if (abbrev == NULL)
18154 {
18155 if (--nesting_level == 0)
18156 {
18157 /* PART_DIE was probably the last thing allocated on the
18158 comp_unit_obstack, so we could call obstack_free
18159 here. We don't do that because the waste is small,
18160 and will be cleaned up when we're done with this
18161 compilation unit. This way, we're also more robust
18162 against other users of the comp_unit_obstack. */
18163 return first_die;
18164 }
18165 info_ptr += bytes_read;
18166 last_die = parent_die;
18167 parent_die = parent_die->die_parent;
18168 continue;
18169 }
18170
18171 /* Check for template arguments. We never save these; if
18172 they're seen, we just mark the parent, and go on our way. */
18173 if (parent_die != NULL
18174 && cu->language == language_cplus
18175 && (abbrev->tag == DW_TAG_template_type_param
18176 || abbrev->tag == DW_TAG_template_value_param))
18177 {
18178 parent_die->has_template_arguments = 1;
18179
18180 if (!load_all)
18181 {
18182 /* We don't need a partial DIE for the template argument. */
18183 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18184 continue;
18185 }
18186 }
18187
18188 /* We only recurse into c++ subprograms looking for template arguments.
18189 Skip their other children. */
18190 if (!load_all
18191 && cu->language == language_cplus
18192 && parent_die != NULL
18193 && parent_die->tag == DW_TAG_subprogram)
18194 {
18195 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18196 continue;
18197 }
18198
18199 /* Check whether this DIE is interesting enough to save. Normally
18200 we would not be interested in members here, but there may be
18201 later variables referencing them via DW_AT_specification (for
18202 static members). */
18203 if (!load_all
18204 && !is_type_tag_for_partial (abbrev->tag)
18205 && abbrev->tag != DW_TAG_constant
18206 && abbrev->tag != DW_TAG_enumerator
18207 && abbrev->tag != DW_TAG_subprogram
18208 && abbrev->tag != DW_TAG_lexical_block
18209 && abbrev->tag != DW_TAG_variable
18210 && abbrev->tag != DW_TAG_namespace
18211 && abbrev->tag != DW_TAG_module
18212 && abbrev->tag != DW_TAG_member
18213 && abbrev->tag != DW_TAG_imported_unit
18214 && abbrev->tag != DW_TAG_imported_declaration)
18215 {
18216 /* Otherwise we skip to the next sibling, if any. */
18217 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18218 continue;
18219 }
18220
18221 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
18222 info_ptr);
18223
18224 /* This two-pass algorithm for processing partial symbols has a
18225 high cost in cache pressure. Thus, handle some simple cases
18226 here which cover the majority of C partial symbols. DIEs
18227 which neither have specification tags in them, nor could have
18228 specification tags elsewhere pointing at them, can simply be
18229 processed and discarded.
18230
18231 This segment is also optional; scan_partial_symbols and
18232 add_partial_symbol will handle these DIEs if we chain
18233 them in normally. When compilers which do not emit large
18234 quantities of duplicate debug information are more common,
18235 this code can probably be removed. */
18236
18237 /* Any complete simple types at the top level (pretty much all
18238 of them, for a language without namespaces), can be processed
18239 directly. */
18240 if (parent_die == NULL
18241 && part_die->has_specification == 0
18242 && part_die->is_declaration == 0
18243 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
18244 || part_die->tag == DW_TAG_base_type
18245 || part_die->tag == DW_TAG_subrange_type))
18246 {
18247 if (building_psymtab && part_die->name != NULL)
18248 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
18249 VAR_DOMAIN, LOC_TYPEDEF,
18250 &objfile->static_psymbols,
18251 0, cu->language, objfile);
18252 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
18253 continue;
18254 }
18255
18256 /* The exception for DW_TAG_typedef with has_children above is
18257 a workaround of GCC PR debug/47510. In the case of this complaint
18258 type_name_no_tag_or_error will error on such types later.
18259
18260 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18261 it could not find the child DIEs referenced later, this is checked
18262 above. In correct DWARF DW_TAG_typedef should have no children. */
18263
18264 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
18265 complaint (&symfile_complaints,
18266 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18267 "- DIE at 0x%x [in module %s]"),
18268 to_underlying (part_die->sect_off), objfile_name (objfile));
18269
18270 /* If we're at the second level, and we're an enumerator, and
18271 our parent has no specification (meaning possibly lives in a
18272 namespace elsewhere), then we can add the partial symbol now
18273 instead of queueing it. */
18274 if (part_die->tag == DW_TAG_enumerator
18275 && parent_die != NULL
18276 && parent_die->die_parent == NULL
18277 && parent_die->tag == DW_TAG_enumeration_type
18278 && parent_die->has_specification == 0)
18279 {
18280 if (part_die->name == NULL)
18281 complaint (&symfile_complaints,
18282 _("malformed enumerator DIE ignored"));
18283 else if (building_psymtab)
18284 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
18285 VAR_DOMAIN, LOC_CONST,
18286 cu->language == language_cplus
18287 ? &objfile->global_psymbols
18288 : &objfile->static_psymbols,
18289 0, cu->language, objfile);
18290
18291 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
18292 continue;
18293 }
18294
18295 /* We'll save this DIE so link it in. */
18296 part_die->die_parent = parent_die;
18297 part_die->die_sibling = NULL;
18298 part_die->die_child = NULL;
18299
18300 if (last_die && last_die == parent_die)
18301 last_die->die_child = part_die;
18302 else if (last_die)
18303 last_die->die_sibling = part_die;
18304
18305 last_die = part_die;
18306
18307 if (first_die == NULL)
18308 first_die = part_die;
18309
18310 /* Maybe add the DIE to the hash table. Not all DIEs that we
18311 find interesting need to be in the hash table, because we
18312 also have the parent/sibling/child chains; only those that we
18313 might refer to by offset later during partial symbol reading.
18314
18315 For now this means things that might have be the target of a
18316 DW_AT_specification, DW_AT_abstract_origin, or
18317 DW_AT_extension. DW_AT_extension will refer only to
18318 namespaces; DW_AT_abstract_origin refers to functions (and
18319 many things under the function DIE, but we do not recurse
18320 into function DIEs during partial symbol reading) and
18321 possibly variables as well; DW_AT_specification refers to
18322 declarations. Declarations ought to have the DW_AT_declaration
18323 flag. It happens that GCC forgets to put it in sometimes, but
18324 only for functions, not for types.
18325
18326 Adding more things than necessary to the hash table is harmless
18327 except for the performance cost. Adding too few will result in
18328 wasted time in find_partial_die, when we reread the compilation
18329 unit with load_all_dies set. */
18330
18331 if (load_all
18332 || abbrev->tag == DW_TAG_constant
18333 || abbrev->tag == DW_TAG_subprogram
18334 || abbrev->tag == DW_TAG_variable
18335 || abbrev->tag == DW_TAG_namespace
18336 || part_die->is_declaration)
18337 {
18338 void **slot;
18339
18340 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18341 to_underlying (part_die->sect_off),
18342 INSERT);
18343 *slot = part_die;
18344 }
18345
18346 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
18347
18348 /* For some DIEs we want to follow their children (if any). For C
18349 we have no reason to follow the children of structures; for other
18350 languages we have to, so that we can get at method physnames
18351 to infer fully qualified class names, for DW_AT_specification,
18352 and for C++ template arguments. For C++, we also look one level
18353 inside functions to find template arguments (if the name of the
18354 function does not already contain the template arguments).
18355
18356 For Ada, we need to scan the children of subprograms and lexical
18357 blocks as well because Ada allows the definition of nested
18358 entities that could be interesting for the debugger, such as
18359 nested subprograms for instance. */
18360 if (last_die->has_children
18361 && (load_all
18362 || last_die->tag == DW_TAG_namespace
18363 || last_die->tag == DW_TAG_module
18364 || last_die->tag == DW_TAG_enumeration_type
18365 || (cu->language == language_cplus
18366 && last_die->tag == DW_TAG_subprogram
18367 && (last_die->name == NULL
18368 || strchr (last_die->name, '<') == NULL))
18369 || (cu->language != language_c
18370 && (last_die->tag == DW_TAG_class_type
18371 || last_die->tag == DW_TAG_interface_type
18372 || last_die->tag == DW_TAG_structure_type
18373 || last_die->tag == DW_TAG_union_type))
18374 || (cu->language == language_ada
18375 && (last_die->tag == DW_TAG_subprogram
18376 || last_die->tag == DW_TAG_lexical_block))))
18377 {
18378 nesting_level++;
18379 parent_die = last_die;
18380 continue;
18381 }
18382
18383 /* Otherwise we skip to the next sibling, if any. */
18384 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18385
18386 /* Back to the top, do it again. */
18387 }
18388 }
18389
18390 /* Read a minimal amount of information into the minimal die structure. */
18391
18392 static const gdb_byte *
18393 read_partial_die (const struct die_reader_specs *reader,
18394 struct partial_die_info *part_die,
18395 struct abbrev_info *abbrev, unsigned int abbrev_len,
18396 const gdb_byte *info_ptr)
18397 {
18398 struct dwarf2_cu *cu = reader->cu;
18399 struct objfile *objfile = cu->objfile;
18400 const gdb_byte *buffer = reader->buffer;
18401 unsigned int i;
18402 struct attribute attr;
18403 int has_low_pc_attr = 0;
18404 int has_high_pc_attr = 0;
18405 int high_pc_relative = 0;
18406
18407 memset (part_die, 0, sizeof (struct partial_die_info));
18408
18409 part_die->sect_off = (sect_offset) (info_ptr - buffer);
18410
18411 info_ptr += abbrev_len;
18412
18413 if (abbrev == NULL)
18414 return info_ptr;
18415
18416 part_die->tag = abbrev->tag;
18417 part_die->has_children = abbrev->has_children;
18418
18419 for (i = 0; i < abbrev->num_attrs; ++i)
18420 {
18421 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
18422
18423 /* Store the data if it is of an attribute we want to keep in a
18424 partial symbol table. */
18425 switch (attr.name)
18426 {
18427 case DW_AT_name:
18428 switch (part_die->tag)
18429 {
18430 case DW_TAG_compile_unit:
18431 case DW_TAG_partial_unit:
18432 case DW_TAG_type_unit:
18433 /* Compilation units have a DW_AT_name that is a filename, not
18434 a source language identifier. */
18435 case DW_TAG_enumeration_type:
18436 case DW_TAG_enumerator:
18437 /* These tags always have simple identifiers already; no need
18438 to canonicalize them. */
18439 part_die->name = DW_STRING (&attr);
18440 break;
18441 default:
18442 part_die->name
18443 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18444 &objfile->per_bfd->storage_obstack);
18445 break;
18446 }
18447 break;
18448 case DW_AT_linkage_name:
18449 case DW_AT_MIPS_linkage_name:
18450 /* Note that both forms of linkage name might appear. We
18451 assume they will be the same, and we only store the last
18452 one we see. */
18453 if (cu->language == language_ada)
18454 part_die->name = DW_STRING (&attr);
18455 part_die->linkage_name = DW_STRING (&attr);
18456 break;
18457 case DW_AT_low_pc:
18458 has_low_pc_attr = 1;
18459 part_die->lowpc = attr_value_as_address (&attr);
18460 break;
18461 case DW_AT_high_pc:
18462 has_high_pc_attr = 1;
18463 part_die->highpc = attr_value_as_address (&attr);
18464 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18465 high_pc_relative = 1;
18466 break;
18467 case DW_AT_location:
18468 /* Support the .debug_loc offsets. */
18469 if (attr_form_is_block (&attr))
18470 {
18471 part_die->d.locdesc = DW_BLOCK (&attr);
18472 }
18473 else if (attr_form_is_section_offset (&attr))
18474 {
18475 dwarf2_complex_location_expr_complaint ();
18476 }
18477 else
18478 {
18479 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18480 "partial symbol information");
18481 }
18482 break;
18483 case DW_AT_external:
18484 part_die->is_external = DW_UNSND (&attr);
18485 break;
18486 case DW_AT_declaration:
18487 part_die->is_declaration = DW_UNSND (&attr);
18488 break;
18489 case DW_AT_type:
18490 part_die->has_type = 1;
18491 break;
18492 case DW_AT_abstract_origin:
18493 case DW_AT_specification:
18494 case DW_AT_extension:
18495 part_die->has_specification = 1;
18496 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
18497 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18498 || cu->per_cu->is_dwz);
18499 break;
18500 case DW_AT_sibling:
18501 /* Ignore absolute siblings, they might point outside of
18502 the current compile unit. */
18503 if (attr.form == DW_FORM_ref_addr)
18504 complaint (&symfile_complaints,
18505 _("ignoring absolute DW_AT_sibling"));
18506 else
18507 {
18508 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18509 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18510
18511 if (sibling_ptr < info_ptr)
18512 complaint (&symfile_complaints,
18513 _("DW_AT_sibling points backwards"));
18514 else if (sibling_ptr > reader->buffer_end)
18515 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18516 else
18517 part_die->sibling = sibling_ptr;
18518 }
18519 break;
18520 case DW_AT_byte_size:
18521 part_die->has_byte_size = 1;
18522 break;
18523 case DW_AT_const_value:
18524 part_die->has_const_value = 1;
18525 break;
18526 case DW_AT_calling_convention:
18527 /* DWARF doesn't provide a way to identify a program's source-level
18528 entry point. DW_AT_calling_convention attributes are only meant
18529 to describe functions' calling conventions.
18530
18531 However, because it's a necessary piece of information in
18532 Fortran, and before DWARF 4 DW_CC_program was the only
18533 piece of debugging information whose definition refers to
18534 a 'main program' at all, several compilers marked Fortran
18535 main programs with DW_CC_program --- even when those
18536 functions use the standard calling conventions.
18537
18538 Although DWARF now specifies a way to provide this
18539 information, we support this practice for backward
18540 compatibility. */
18541 if (DW_UNSND (&attr) == DW_CC_program
18542 && cu->language == language_fortran)
18543 part_die->main_subprogram = 1;
18544 break;
18545 case DW_AT_inline:
18546 if (DW_UNSND (&attr) == DW_INL_inlined
18547 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18548 part_die->may_be_inlined = 1;
18549 break;
18550
18551 case DW_AT_import:
18552 if (part_die->tag == DW_TAG_imported_unit)
18553 {
18554 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
18555 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18556 || cu->per_cu->is_dwz);
18557 }
18558 break;
18559
18560 case DW_AT_main_subprogram:
18561 part_die->main_subprogram = DW_UNSND (&attr);
18562 break;
18563
18564 default:
18565 break;
18566 }
18567 }
18568
18569 if (high_pc_relative)
18570 part_die->highpc += part_die->lowpc;
18571
18572 if (has_low_pc_attr && has_high_pc_attr)
18573 {
18574 /* When using the GNU linker, .gnu.linkonce. sections are used to
18575 eliminate duplicate copies of functions and vtables and such.
18576 The linker will arbitrarily choose one and discard the others.
18577 The AT_*_pc values for such functions refer to local labels in
18578 these sections. If the section from that file was discarded, the
18579 labels are not in the output, so the relocs get a value of 0.
18580 If this is a discarded function, mark the pc bounds as invalid,
18581 so that GDB will ignore it. */
18582 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18583 {
18584 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18585
18586 complaint (&symfile_complaints,
18587 _("DW_AT_low_pc %s is zero "
18588 "for DIE at 0x%x [in module %s]"),
18589 paddress (gdbarch, part_die->lowpc),
18590 to_underlying (part_die->sect_off), objfile_name (objfile));
18591 }
18592 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18593 else if (part_die->lowpc >= part_die->highpc)
18594 {
18595 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18596
18597 complaint (&symfile_complaints,
18598 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18599 "for DIE at 0x%x [in module %s]"),
18600 paddress (gdbarch, part_die->lowpc),
18601 paddress (gdbarch, part_die->highpc),
18602 to_underlying (part_die->sect_off),
18603 objfile_name (objfile));
18604 }
18605 else
18606 part_die->has_pc_info = 1;
18607 }
18608
18609 return info_ptr;
18610 }
18611
18612 /* Find a cached partial DIE at OFFSET in CU. */
18613
18614 static struct partial_die_info *
18615 find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
18616 {
18617 struct partial_die_info *lookup_die = NULL;
18618 struct partial_die_info part_die;
18619
18620 part_die.sect_off = sect_off;
18621 lookup_die = ((struct partial_die_info *)
18622 htab_find_with_hash (cu->partial_dies, &part_die,
18623 to_underlying (sect_off)));
18624
18625 return lookup_die;
18626 }
18627
18628 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18629 except in the case of .debug_types DIEs which do not reference
18630 outside their CU (they do however referencing other types via
18631 DW_FORM_ref_sig8). */
18632
18633 static struct partial_die_info *
18634 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18635 {
18636 struct objfile *objfile = cu->objfile;
18637 struct dwarf2_per_cu_data *per_cu = NULL;
18638 struct partial_die_info *pd = NULL;
18639
18640 if (offset_in_dwz == cu->per_cu->is_dwz
18641 && offset_in_cu_p (&cu->header, sect_off))
18642 {
18643 pd = find_partial_die_in_comp_unit (sect_off, cu);
18644 if (pd != NULL)
18645 return pd;
18646 /* We missed recording what we needed.
18647 Load all dies and try again. */
18648 per_cu = cu->per_cu;
18649 }
18650 else
18651 {
18652 /* TUs don't reference other CUs/TUs (except via type signatures). */
18653 if (cu->per_cu->is_debug_types)
18654 {
18655 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
18656 " external reference to offset 0x%x [in module %s].\n"),
18657 to_underlying (cu->header.sect_off), to_underlying (sect_off),
18658 bfd_get_filename (objfile->obfd));
18659 }
18660 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18661 objfile);
18662
18663 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18664 load_partial_comp_unit (per_cu);
18665
18666 per_cu->cu->last_used = 0;
18667 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
18668 }
18669
18670 /* If we didn't find it, and not all dies have been loaded,
18671 load them all and try again. */
18672
18673 if (pd == NULL && per_cu->load_all_dies == 0)
18674 {
18675 per_cu->load_all_dies = 1;
18676
18677 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18678 THIS_CU->cu may already be in use. So we can't just free it and
18679 replace its DIEs with the ones we read in. Instead, we leave those
18680 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18681 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18682 set. */
18683 load_partial_comp_unit (per_cu);
18684
18685 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
18686 }
18687
18688 if (pd == NULL)
18689 internal_error (__FILE__, __LINE__,
18690 _("could not find partial DIE 0x%x "
18691 "in cache [from module %s]\n"),
18692 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
18693 return pd;
18694 }
18695
18696 /* See if we can figure out if the class lives in a namespace. We do
18697 this by looking for a member function; its demangled name will
18698 contain namespace info, if there is any. */
18699
18700 static void
18701 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18702 struct dwarf2_cu *cu)
18703 {
18704 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18705 what template types look like, because the demangler
18706 frequently doesn't give the same name as the debug info. We
18707 could fix this by only using the demangled name to get the
18708 prefix (but see comment in read_structure_type). */
18709
18710 struct partial_die_info *real_pdi;
18711 struct partial_die_info *child_pdi;
18712
18713 /* If this DIE (this DIE's specification, if any) has a parent, then
18714 we should not do this. We'll prepend the parent's fully qualified
18715 name when we create the partial symbol. */
18716
18717 real_pdi = struct_pdi;
18718 while (real_pdi->has_specification)
18719 real_pdi = find_partial_die (real_pdi->spec_offset,
18720 real_pdi->spec_is_dwz, cu);
18721
18722 if (real_pdi->die_parent != NULL)
18723 return;
18724
18725 for (child_pdi = struct_pdi->die_child;
18726 child_pdi != NULL;
18727 child_pdi = child_pdi->die_sibling)
18728 {
18729 if (child_pdi->tag == DW_TAG_subprogram
18730 && child_pdi->linkage_name != NULL)
18731 {
18732 char *actual_class_name
18733 = language_class_name_from_physname (cu->language_defn,
18734 child_pdi->linkage_name);
18735 if (actual_class_name != NULL)
18736 {
18737 struct_pdi->name
18738 = ((const char *)
18739 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
18740 actual_class_name,
18741 strlen (actual_class_name)));
18742 xfree (actual_class_name);
18743 }
18744 break;
18745 }
18746 }
18747 }
18748
18749 /* Adjust PART_DIE before generating a symbol for it. This function
18750 may set the is_external flag or change the DIE's name. */
18751
18752 static void
18753 fixup_partial_die (struct partial_die_info *part_die,
18754 struct dwarf2_cu *cu)
18755 {
18756 /* Once we've fixed up a die, there's no point in doing so again.
18757 This also avoids a memory leak if we were to call
18758 guess_partial_die_structure_name multiple times. */
18759 if (part_die->fixup_called)
18760 return;
18761
18762 /* If we found a reference attribute and the DIE has no name, try
18763 to find a name in the referred to DIE. */
18764
18765 if (part_die->name == NULL && part_die->has_specification)
18766 {
18767 struct partial_die_info *spec_die;
18768
18769 spec_die = find_partial_die (part_die->spec_offset,
18770 part_die->spec_is_dwz, cu);
18771
18772 fixup_partial_die (spec_die, cu);
18773
18774 if (spec_die->name)
18775 {
18776 part_die->name = spec_die->name;
18777
18778 /* Copy DW_AT_external attribute if it is set. */
18779 if (spec_die->is_external)
18780 part_die->is_external = spec_die->is_external;
18781 }
18782 }
18783
18784 /* Set default names for some unnamed DIEs. */
18785
18786 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
18787 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
18788
18789 /* If there is no parent die to provide a namespace, and there are
18790 children, see if we can determine the namespace from their linkage
18791 name. */
18792 if (cu->language == language_cplus
18793 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
18794 && part_die->die_parent == NULL
18795 && part_die->has_children
18796 && (part_die->tag == DW_TAG_class_type
18797 || part_die->tag == DW_TAG_structure_type
18798 || part_die->tag == DW_TAG_union_type))
18799 guess_partial_die_structure_name (part_die, cu);
18800
18801 /* GCC might emit a nameless struct or union that has a linkage
18802 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18803 if (part_die->name == NULL
18804 && (part_die->tag == DW_TAG_class_type
18805 || part_die->tag == DW_TAG_interface_type
18806 || part_die->tag == DW_TAG_structure_type
18807 || part_die->tag == DW_TAG_union_type)
18808 && part_die->linkage_name != NULL)
18809 {
18810 char *demangled;
18811
18812 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
18813 if (demangled)
18814 {
18815 const char *base;
18816
18817 /* Strip any leading namespaces/classes, keep only the base name.
18818 DW_AT_name for named DIEs does not contain the prefixes. */
18819 base = strrchr (demangled, ':');
18820 if (base && base > demangled && base[-1] == ':')
18821 base++;
18822 else
18823 base = demangled;
18824
18825 part_die->name
18826 = ((const char *)
18827 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
18828 base, strlen (base)));
18829 xfree (demangled);
18830 }
18831 }
18832
18833 part_die->fixup_called = 1;
18834 }
18835
18836 /* Read an attribute value described by an attribute form. */
18837
18838 static const gdb_byte *
18839 read_attribute_value (const struct die_reader_specs *reader,
18840 struct attribute *attr, unsigned form,
18841 LONGEST implicit_const, const gdb_byte *info_ptr)
18842 {
18843 struct dwarf2_cu *cu = reader->cu;
18844 struct objfile *objfile = cu->objfile;
18845 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18846 bfd *abfd = reader->abfd;
18847 struct comp_unit_head *cu_header = &cu->header;
18848 unsigned int bytes_read;
18849 struct dwarf_block *blk;
18850
18851 attr->form = (enum dwarf_form) form;
18852 switch (form)
18853 {
18854 case DW_FORM_ref_addr:
18855 if (cu->header.version == 2)
18856 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18857 else
18858 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18859 &cu->header, &bytes_read);
18860 info_ptr += bytes_read;
18861 break;
18862 case DW_FORM_GNU_ref_alt:
18863 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18864 info_ptr += bytes_read;
18865 break;
18866 case DW_FORM_addr:
18867 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18868 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18869 info_ptr += bytes_read;
18870 break;
18871 case DW_FORM_block2:
18872 blk = dwarf_alloc_block (cu);
18873 blk->size = read_2_bytes (abfd, info_ptr);
18874 info_ptr += 2;
18875 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18876 info_ptr += blk->size;
18877 DW_BLOCK (attr) = blk;
18878 break;
18879 case DW_FORM_block4:
18880 blk = dwarf_alloc_block (cu);
18881 blk->size = read_4_bytes (abfd, info_ptr);
18882 info_ptr += 4;
18883 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18884 info_ptr += blk->size;
18885 DW_BLOCK (attr) = blk;
18886 break;
18887 case DW_FORM_data2:
18888 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18889 info_ptr += 2;
18890 break;
18891 case DW_FORM_data4:
18892 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18893 info_ptr += 4;
18894 break;
18895 case DW_FORM_data8:
18896 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18897 info_ptr += 8;
18898 break;
18899 case DW_FORM_data16:
18900 blk = dwarf_alloc_block (cu);
18901 blk->size = 16;
18902 blk->data = read_n_bytes (abfd, info_ptr, 16);
18903 info_ptr += 16;
18904 DW_BLOCK (attr) = blk;
18905 break;
18906 case DW_FORM_sec_offset:
18907 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18908 info_ptr += bytes_read;
18909 break;
18910 case DW_FORM_string:
18911 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18912 DW_STRING_IS_CANONICAL (attr) = 0;
18913 info_ptr += bytes_read;
18914 break;
18915 case DW_FORM_strp:
18916 if (!cu->per_cu->is_dwz)
18917 {
18918 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
18919 &bytes_read);
18920 DW_STRING_IS_CANONICAL (attr) = 0;
18921 info_ptr += bytes_read;
18922 break;
18923 }
18924 /* FALLTHROUGH */
18925 case DW_FORM_line_strp:
18926 if (!cu->per_cu->is_dwz)
18927 {
18928 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
18929 cu_header, &bytes_read);
18930 DW_STRING_IS_CANONICAL (attr) = 0;
18931 info_ptr += bytes_read;
18932 break;
18933 }
18934 /* FALLTHROUGH */
18935 case DW_FORM_GNU_strp_alt:
18936 {
18937 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18938 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
18939 &bytes_read);
18940
18941 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
18942 DW_STRING_IS_CANONICAL (attr) = 0;
18943 info_ptr += bytes_read;
18944 }
18945 break;
18946 case DW_FORM_exprloc:
18947 case DW_FORM_block:
18948 blk = dwarf_alloc_block (cu);
18949 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18950 info_ptr += bytes_read;
18951 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18952 info_ptr += blk->size;
18953 DW_BLOCK (attr) = blk;
18954 break;
18955 case DW_FORM_block1:
18956 blk = dwarf_alloc_block (cu);
18957 blk->size = read_1_byte (abfd, info_ptr);
18958 info_ptr += 1;
18959 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18960 info_ptr += blk->size;
18961 DW_BLOCK (attr) = blk;
18962 break;
18963 case DW_FORM_data1:
18964 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18965 info_ptr += 1;
18966 break;
18967 case DW_FORM_flag:
18968 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18969 info_ptr += 1;
18970 break;
18971 case DW_FORM_flag_present:
18972 DW_UNSND (attr) = 1;
18973 break;
18974 case DW_FORM_sdata:
18975 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18976 info_ptr += bytes_read;
18977 break;
18978 case DW_FORM_udata:
18979 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18980 info_ptr += bytes_read;
18981 break;
18982 case DW_FORM_ref1:
18983 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18984 + read_1_byte (abfd, info_ptr));
18985 info_ptr += 1;
18986 break;
18987 case DW_FORM_ref2:
18988 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18989 + read_2_bytes (abfd, info_ptr));
18990 info_ptr += 2;
18991 break;
18992 case DW_FORM_ref4:
18993 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18994 + read_4_bytes (abfd, info_ptr));
18995 info_ptr += 4;
18996 break;
18997 case DW_FORM_ref8:
18998 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18999 + read_8_bytes (abfd, info_ptr));
19000 info_ptr += 8;
19001 break;
19002 case DW_FORM_ref_sig8:
19003 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19004 info_ptr += 8;
19005 break;
19006 case DW_FORM_ref_udata:
19007 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19008 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19009 info_ptr += bytes_read;
19010 break;
19011 case DW_FORM_indirect:
19012 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19013 info_ptr += bytes_read;
19014 if (form == DW_FORM_implicit_const)
19015 {
19016 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19017 info_ptr += bytes_read;
19018 }
19019 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19020 info_ptr);
19021 break;
19022 case DW_FORM_implicit_const:
19023 DW_SND (attr) = implicit_const;
19024 break;
19025 case DW_FORM_GNU_addr_index:
19026 if (reader->dwo_file == NULL)
19027 {
19028 /* For now flag a hard error.
19029 Later we can turn this into a complaint. */
19030 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19031 dwarf_form_name (form),
19032 bfd_get_filename (abfd));
19033 }
19034 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19035 info_ptr += bytes_read;
19036 break;
19037 case DW_FORM_GNU_str_index:
19038 if (reader->dwo_file == NULL)
19039 {
19040 /* For now flag a hard error.
19041 Later we can turn this into a complaint if warranted. */
19042 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19043 dwarf_form_name (form),
19044 bfd_get_filename (abfd));
19045 }
19046 {
19047 ULONGEST str_index =
19048 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19049
19050 DW_STRING (attr) = read_str_index (reader, str_index);
19051 DW_STRING_IS_CANONICAL (attr) = 0;
19052 info_ptr += bytes_read;
19053 }
19054 break;
19055 default:
19056 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19057 dwarf_form_name (form),
19058 bfd_get_filename (abfd));
19059 }
19060
19061 /* Super hack. */
19062 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19063 attr->form = DW_FORM_GNU_ref_alt;
19064
19065 /* We have seen instances where the compiler tried to emit a byte
19066 size attribute of -1 which ended up being encoded as an unsigned
19067 0xffffffff. Although 0xffffffff is technically a valid size value,
19068 an object of this size seems pretty unlikely so we can relatively
19069 safely treat these cases as if the size attribute was invalid and
19070 treat them as zero by default. */
19071 if (attr->name == DW_AT_byte_size
19072 && form == DW_FORM_data4
19073 && DW_UNSND (attr) >= 0xffffffff)
19074 {
19075 complaint
19076 (&symfile_complaints,
19077 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19078 hex_string (DW_UNSND (attr)));
19079 DW_UNSND (attr) = 0;
19080 }
19081
19082 return info_ptr;
19083 }
19084
19085 /* Read an attribute described by an abbreviated attribute. */
19086
19087 static const gdb_byte *
19088 read_attribute (const struct die_reader_specs *reader,
19089 struct attribute *attr, struct attr_abbrev *abbrev,
19090 const gdb_byte *info_ptr)
19091 {
19092 attr->name = abbrev->name;
19093 return read_attribute_value (reader, attr, abbrev->form,
19094 abbrev->implicit_const, info_ptr);
19095 }
19096
19097 /* Read dwarf information from a buffer. */
19098
19099 static unsigned int
19100 read_1_byte (bfd *abfd, const gdb_byte *buf)
19101 {
19102 return bfd_get_8 (abfd, buf);
19103 }
19104
19105 static int
19106 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19107 {
19108 return bfd_get_signed_8 (abfd, buf);
19109 }
19110
19111 static unsigned int
19112 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19113 {
19114 return bfd_get_16 (abfd, buf);
19115 }
19116
19117 static int
19118 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19119 {
19120 return bfd_get_signed_16 (abfd, buf);
19121 }
19122
19123 static unsigned int
19124 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19125 {
19126 return bfd_get_32 (abfd, buf);
19127 }
19128
19129 static int
19130 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19131 {
19132 return bfd_get_signed_32 (abfd, buf);
19133 }
19134
19135 static ULONGEST
19136 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19137 {
19138 return bfd_get_64 (abfd, buf);
19139 }
19140
19141 static CORE_ADDR
19142 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19143 unsigned int *bytes_read)
19144 {
19145 struct comp_unit_head *cu_header = &cu->header;
19146 CORE_ADDR retval = 0;
19147
19148 if (cu_header->signed_addr_p)
19149 {
19150 switch (cu_header->addr_size)
19151 {
19152 case 2:
19153 retval = bfd_get_signed_16 (abfd, buf);
19154 break;
19155 case 4:
19156 retval = bfd_get_signed_32 (abfd, buf);
19157 break;
19158 case 8:
19159 retval = bfd_get_signed_64 (abfd, buf);
19160 break;
19161 default:
19162 internal_error (__FILE__, __LINE__,
19163 _("read_address: bad switch, signed [in module %s]"),
19164 bfd_get_filename (abfd));
19165 }
19166 }
19167 else
19168 {
19169 switch (cu_header->addr_size)
19170 {
19171 case 2:
19172 retval = bfd_get_16 (abfd, buf);
19173 break;
19174 case 4:
19175 retval = bfd_get_32 (abfd, buf);
19176 break;
19177 case 8:
19178 retval = bfd_get_64 (abfd, buf);
19179 break;
19180 default:
19181 internal_error (__FILE__, __LINE__,
19182 _("read_address: bad switch, "
19183 "unsigned [in module %s]"),
19184 bfd_get_filename (abfd));
19185 }
19186 }
19187
19188 *bytes_read = cu_header->addr_size;
19189 return retval;
19190 }
19191
19192 /* Read the initial length from a section. The (draft) DWARF 3
19193 specification allows the initial length to take up either 4 bytes
19194 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19195 bytes describe the length and all offsets will be 8 bytes in length
19196 instead of 4.
19197
19198 An older, non-standard 64-bit format is also handled by this
19199 function. The older format in question stores the initial length
19200 as an 8-byte quantity without an escape value. Lengths greater
19201 than 2^32 aren't very common which means that the initial 4 bytes
19202 is almost always zero. Since a length value of zero doesn't make
19203 sense for the 32-bit format, this initial zero can be considered to
19204 be an escape value which indicates the presence of the older 64-bit
19205 format. As written, the code can't detect (old format) lengths
19206 greater than 4GB. If it becomes necessary to handle lengths
19207 somewhat larger than 4GB, we could allow other small values (such
19208 as the non-sensical values of 1, 2, and 3) to also be used as
19209 escape values indicating the presence of the old format.
19210
19211 The value returned via bytes_read should be used to increment the
19212 relevant pointer after calling read_initial_length().
19213
19214 [ Note: read_initial_length() and read_offset() are based on the
19215 document entitled "DWARF Debugging Information Format", revision
19216 3, draft 8, dated November 19, 2001. This document was obtained
19217 from:
19218
19219 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19220
19221 This document is only a draft and is subject to change. (So beware.)
19222
19223 Details regarding the older, non-standard 64-bit format were
19224 determined empirically by examining 64-bit ELF files produced by
19225 the SGI toolchain on an IRIX 6.5 machine.
19226
19227 - Kevin, July 16, 2002
19228 ] */
19229
19230 static LONGEST
19231 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19232 {
19233 LONGEST length = bfd_get_32 (abfd, buf);
19234
19235 if (length == 0xffffffff)
19236 {
19237 length = bfd_get_64 (abfd, buf + 4);
19238 *bytes_read = 12;
19239 }
19240 else if (length == 0)
19241 {
19242 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19243 length = bfd_get_64 (abfd, buf);
19244 *bytes_read = 8;
19245 }
19246 else
19247 {
19248 *bytes_read = 4;
19249 }
19250
19251 return length;
19252 }
19253
19254 /* Cover function for read_initial_length.
19255 Returns the length of the object at BUF, and stores the size of the
19256 initial length in *BYTES_READ and stores the size that offsets will be in
19257 *OFFSET_SIZE.
19258 If the initial length size is not equivalent to that specified in
19259 CU_HEADER then issue a complaint.
19260 This is useful when reading non-comp-unit headers. */
19261
19262 static LONGEST
19263 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19264 const struct comp_unit_head *cu_header,
19265 unsigned int *bytes_read,
19266 unsigned int *offset_size)
19267 {
19268 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19269
19270 gdb_assert (cu_header->initial_length_size == 4
19271 || cu_header->initial_length_size == 8
19272 || cu_header->initial_length_size == 12);
19273
19274 if (cu_header->initial_length_size != *bytes_read)
19275 complaint (&symfile_complaints,
19276 _("intermixed 32-bit and 64-bit DWARF sections"));
19277
19278 *offset_size = (*bytes_read == 4) ? 4 : 8;
19279 return length;
19280 }
19281
19282 /* Read an offset from the data stream. The size of the offset is
19283 given by cu_header->offset_size. */
19284
19285 static LONGEST
19286 read_offset (bfd *abfd, const gdb_byte *buf,
19287 const struct comp_unit_head *cu_header,
19288 unsigned int *bytes_read)
19289 {
19290 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19291
19292 *bytes_read = cu_header->offset_size;
19293 return offset;
19294 }
19295
19296 /* Read an offset from the data stream. */
19297
19298 static LONGEST
19299 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19300 {
19301 LONGEST retval = 0;
19302
19303 switch (offset_size)
19304 {
19305 case 4:
19306 retval = bfd_get_32 (abfd, buf);
19307 break;
19308 case 8:
19309 retval = bfd_get_64 (abfd, buf);
19310 break;
19311 default:
19312 internal_error (__FILE__, __LINE__,
19313 _("read_offset_1: bad switch [in module %s]"),
19314 bfd_get_filename (abfd));
19315 }
19316
19317 return retval;
19318 }
19319
19320 static const gdb_byte *
19321 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19322 {
19323 /* If the size of a host char is 8 bits, we can return a pointer
19324 to the buffer, otherwise we have to copy the data to a buffer
19325 allocated on the temporary obstack. */
19326 gdb_assert (HOST_CHAR_BIT == 8);
19327 return buf;
19328 }
19329
19330 static const char *
19331 read_direct_string (bfd *abfd, const gdb_byte *buf,
19332 unsigned int *bytes_read_ptr)
19333 {
19334 /* If the size of a host char is 8 bits, we can return a pointer
19335 to the string, otherwise we have to copy the string to a buffer
19336 allocated on the temporary obstack. */
19337 gdb_assert (HOST_CHAR_BIT == 8);
19338 if (*buf == '\0')
19339 {
19340 *bytes_read_ptr = 1;
19341 return NULL;
19342 }
19343 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19344 return (const char *) buf;
19345 }
19346
19347 /* Return pointer to string at section SECT offset STR_OFFSET with error
19348 reporting strings FORM_NAME and SECT_NAME. */
19349
19350 static const char *
19351 read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
19352 struct dwarf2_section_info *sect,
19353 const char *form_name,
19354 const char *sect_name)
19355 {
19356 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
19357 if (sect->buffer == NULL)
19358 error (_("%s used without %s section [in module %s]"),
19359 form_name, sect_name, bfd_get_filename (abfd));
19360 if (str_offset >= sect->size)
19361 error (_("%s pointing outside of %s section [in module %s]"),
19362 form_name, sect_name, bfd_get_filename (abfd));
19363 gdb_assert (HOST_CHAR_BIT == 8);
19364 if (sect->buffer[str_offset] == '\0')
19365 return NULL;
19366 return (const char *) (sect->buffer + str_offset);
19367 }
19368
19369 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19370
19371 static const char *
19372 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
19373 {
19374 return read_indirect_string_at_offset_from (abfd, str_offset,
19375 &dwarf2_per_objfile->str,
19376 "DW_FORM_strp", ".debug_str");
19377 }
19378
19379 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19380
19381 static const char *
19382 read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
19383 {
19384 return read_indirect_string_at_offset_from (abfd, str_offset,
19385 &dwarf2_per_objfile->line_str,
19386 "DW_FORM_line_strp",
19387 ".debug_line_str");
19388 }
19389
19390 /* Read a string at offset STR_OFFSET in the .debug_str section from
19391 the .dwz file DWZ. Throw an error if the offset is too large. If
19392 the string consists of a single NUL byte, return NULL; otherwise
19393 return a pointer to the string. */
19394
19395 static const char *
19396 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
19397 {
19398 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
19399
19400 if (dwz->str.buffer == NULL)
19401 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19402 "section [in module %s]"),
19403 bfd_get_filename (dwz->dwz_bfd));
19404 if (str_offset >= dwz->str.size)
19405 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19406 ".debug_str section [in module %s]"),
19407 bfd_get_filename (dwz->dwz_bfd));
19408 gdb_assert (HOST_CHAR_BIT == 8);
19409 if (dwz->str.buffer[str_offset] == '\0')
19410 return NULL;
19411 return (const char *) (dwz->str.buffer + str_offset);
19412 }
19413
19414 /* Return pointer to string at .debug_str offset as read from BUF.
19415 BUF is assumed to be in a compilation unit described by CU_HEADER.
19416 Return *BYTES_READ_PTR count of bytes read from BUF. */
19417
19418 static const char *
19419 read_indirect_string (bfd *abfd, const gdb_byte *buf,
19420 const struct comp_unit_head *cu_header,
19421 unsigned int *bytes_read_ptr)
19422 {
19423 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19424
19425 return read_indirect_string_at_offset (abfd, str_offset);
19426 }
19427
19428 /* Return pointer to string at .debug_line_str offset as read from BUF.
19429 BUF is assumed to be in a compilation unit described by CU_HEADER.
19430 Return *BYTES_READ_PTR count of bytes read from BUF. */
19431
19432 static const char *
19433 read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
19434 const struct comp_unit_head *cu_header,
19435 unsigned int *bytes_read_ptr)
19436 {
19437 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19438
19439 return read_indirect_line_string_at_offset (abfd, str_offset);
19440 }
19441
19442 ULONGEST
19443 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19444 unsigned int *bytes_read_ptr)
19445 {
19446 ULONGEST result;
19447 unsigned int num_read;
19448 int shift;
19449 unsigned char byte;
19450
19451 result = 0;
19452 shift = 0;
19453 num_read = 0;
19454 while (1)
19455 {
19456 byte = bfd_get_8 (abfd, buf);
19457 buf++;
19458 num_read++;
19459 result |= ((ULONGEST) (byte & 127) << shift);
19460 if ((byte & 128) == 0)
19461 {
19462 break;
19463 }
19464 shift += 7;
19465 }
19466 *bytes_read_ptr = num_read;
19467 return result;
19468 }
19469
19470 static LONGEST
19471 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19472 unsigned int *bytes_read_ptr)
19473 {
19474 LONGEST result;
19475 int shift, num_read;
19476 unsigned char byte;
19477
19478 result = 0;
19479 shift = 0;
19480 num_read = 0;
19481 while (1)
19482 {
19483 byte = bfd_get_8 (abfd, buf);
19484 buf++;
19485 num_read++;
19486 result |= ((LONGEST) (byte & 127) << shift);
19487 shift += 7;
19488 if ((byte & 128) == 0)
19489 {
19490 break;
19491 }
19492 }
19493 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19494 result |= -(((LONGEST) 1) << shift);
19495 *bytes_read_ptr = num_read;
19496 return result;
19497 }
19498
19499 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19500 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19501 ADDR_SIZE is the size of addresses from the CU header. */
19502
19503 static CORE_ADDR
19504 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
19505 {
19506 struct objfile *objfile = dwarf2_per_objfile->objfile;
19507 bfd *abfd = objfile->obfd;
19508 const gdb_byte *info_ptr;
19509
19510 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19511 if (dwarf2_per_objfile->addr.buffer == NULL)
19512 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19513 objfile_name (objfile));
19514 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19515 error (_("DW_FORM_addr_index pointing outside of "
19516 ".debug_addr section [in module %s]"),
19517 objfile_name (objfile));
19518 info_ptr = (dwarf2_per_objfile->addr.buffer
19519 + addr_base + addr_index * addr_size);
19520 if (addr_size == 4)
19521 return bfd_get_32 (abfd, info_ptr);
19522 else
19523 return bfd_get_64 (abfd, info_ptr);
19524 }
19525
19526 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19527
19528 static CORE_ADDR
19529 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19530 {
19531 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
19532 }
19533
19534 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19535
19536 static CORE_ADDR
19537 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19538 unsigned int *bytes_read)
19539 {
19540 bfd *abfd = cu->objfile->obfd;
19541 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19542
19543 return read_addr_index (cu, addr_index);
19544 }
19545
19546 /* Data structure to pass results from dwarf2_read_addr_index_reader
19547 back to dwarf2_read_addr_index. */
19548
19549 struct dwarf2_read_addr_index_data
19550 {
19551 ULONGEST addr_base;
19552 int addr_size;
19553 };
19554
19555 /* die_reader_func for dwarf2_read_addr_index. */
19556
19557 static void
19558 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19559 const gdb_byte *info_ptr,
19560 struct die_info *comp_unit_die,
19561 int has_children,
19562 void *data)
19563 {
19564 struct dwarf2_cu *cu = reader->cu;
19565 struct dwarf2_read_addr_index_data *aidata =
19566 (struct dwarf2_read_addr_index_data *) data;
19567
19568 aidata->addr_base = cu->addr_base;
19569 aidata->addr_size = cu->header.addr_size;
19570 }
19571
19572 /* Given an index in .debug_addr, fetch the value.
19573 NOTE: This can be called during dwarf expression evaluation,
19574 long after the debug information has been read, and thus per_cu->cu
19575 may no longer exist. */
19576
19577 CORE_ADDR
19578 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19579 unsigned int addr_index)
19580 {
19581 struct objfile *objfile = per_cu->objfile;
19582 struct dwarf2_cu *cu = per_cu->cu;
19583 ULONGEST addr_base;
19584 int addr_size;
19585
19586 /* This is intended to be called from outside this file. */
19587 dw2_setup (objfile);
19588
19589 /* We need addr_base and addr_size.
19590 If we don't have PER_CU->cu, we have to get it.
19591 Nasty, but the alternative is storing the needed info in PER_CU,
19592 which at this point doesn't seem justified: it's not clear how frequently
19593 it would get used and it would increase the size of every PER_CU.
19594 Entry points like dwarf2_per_cu_addr_size do a similar thing
19595 so we're not in uncharted territory here.
19596 Alas we need to be a bit more complicated as addr_base is contained
19597 in the DIE.
19598
19599 We don't need to read the entire CU(/TU).
19600 We just need the header and top level die.
19601
19602 IWBN to use the aging mechanism to let us lazily later discard the CU.
19603 For now we skip this optimization. */
19604
19605 if (cu != NULL)
19606 {
19607 addr_base = cu->addr_base;
19608 addr_size = cu->header.addr_size;
19609 }
19610 else
19611 {
19612 struct dwarf2_read_addr_index_data aidata;
19613
19614 /* Note: We can't use init_cutu_and_read_dies_simple here,
19615 we need addr_base. */
19616 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19617 dwarf2_read_addr_index_reader, &aidata);
19618 addr_base = aidata.addr_base;
19619 addr_size = aidata.addr_size;
19620 }
19621
19622 return read_addr_index_1 (addr_index, addr_base, addr_size);
19623 }
19624
19625 /* Given a DW_FORM_GNU_str_index, fetch the string.
19626 This is only used by the Fission support. */
19627
19628 static const char *
19629 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19630 {
19631 struct objfile *objfile = dwarf2_per_objfile->objfile;
19632 const char *objf_name = objfile_name (objfile);
19633 bfd *abfd = objfile->obfd;
19634 struct dwarf2_cu *cu = reader->cu;
19635 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19636 struct dwarf2_section_info *str_offsets_section =
19637 &reader->dwo_file->sections.str_offsets;
19638 const gdb_byte *info_ptr;
19639 ULONGEST str_offset;
19640 static const char form_name[] = "DW_FORM_GNU_str_index";
19641
19642 dwarf2_read_section (objfile, str_section);
19643 dwarf2_read_section (objfile, str_offsets_section);
19644 if (str_section->buffer == NULL)
19645 error (_("%s used without .debug_str.dwo section"
19646 " in CU at offset 0x%x [in module %s]"),
19647 form_name, to_underlying (cu->header.sect_off), objf_name);
19648 if (str_offsets_section->buffer == NULL)
19649 error (_("%s used without .debug_str_offsets.dwo section"
19650 " in CU at offset 0x%x [in module %s]"),
19651 form_name, to_underlying (cu->header.sect_off), objf_name);
19652 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19653 error (_("%s pointing outside of .debug_str_offsets.dwo"
19654 " section in CU at offset 0x%x [in module %s]"),
19655 form_name, to_underlying (cu->header.sect_off), objf_name);
19656 info_ptr = (str_offsets_section->buffer
19657 + str_index * cu->header.offset_size);
19658 if (cu->header.offset_size == 4)
19659 str_offset = bfd_get_32 (abfd, info_ptr);
19660 else
19661 str_offset = bfd_get_64 (abfd, info_ptr);
19662 if (str_offset >= str_section->size)
19663 error (_("Offset from %s pointing outside of"
19664 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
19665 form_name, to_underlying (cu->header.sect_off), objf_name);
19666 return (const char *) (str_section->buffer + str_offset);
19667 }
19668
19669 /* Return the length of an LEB128 number in BUF. */
19670
19671 static int
19672 leb128_size (const gdb_byte *buf)
19673 {
19674 const gdb_byte *begin = buf;
19675 gdb_byte byte;
19676
19677 while (1)
19678 {
19679 byte = *buf++;
19680 if ((byte & 128) == 0)
19681 return buf - begin;
19682 }
19683 }
19684
19685 static void
19686 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19687 {
19688 switch (lang)
19689 {
19690 case DW_LANG_C89:
19691 case DW_LANG_C99:
19692 case DW_LANG_C11:
19693 case DW_LANG_C:
19694 case DW_LANG_UPC:
19695 cu->language = language_c;
19696 break;
19697 case DW_LANG_Java:
19698 case DW_LANG_C_plus_plus:
19699 case DW_LANG_C_plus_plus_11:
19700 case DW_LANG_C_plus_plus_14:
19701 cu->language = language_cplus;
19702 break;
19703 case DW_LANG_D:
19704 cu->language = language_d;
19705 break;
19706 case DW_LANG_Fortran77:
19707 case DW_LANG_Fortran90:
19708 case DW_LANG_Fortran95:
19709 case DW_LANG_Fortran03:
19710 case DW_LANG_Fortran08:
19711 cu->language = language_fortran;
19712 break;
19713 case DW_LANG_Go:
19714 cu->language = language_go;
19715 break;
19716 case DW_LANG_Mips_Assembler:
19717 cu->language = language_asm;
19718 break;
19719 case DW_LANG_Ada83:
19720 case DW_LANG_Ada95:
19721 cu->language = language_ada;
19722 break;
19723 case DW_LANG_Modula2:
19724 cu->language = language_m2;
19725 break;
19726 case DW_LANG_Pascal83:
19727 cu->language = language_pascal;
19728 break;
19729 case DW_LANG_ObjC:
19730 cu->language = language_objc;
19731 break;
19732 case DW_LANG_Rust:
19733 case DW_LANG_Rust_old:
19734 cu->language = language_rust;
19735 break;
19736 case DW_LANG_Cobol74:
19737 case DW_LANG_Cobol85:
19738 default:
19739 cu->language = language_minimal;
19740 break;
19741 }
19742 cu->language_defn = language_def (cu->language);
19743 }
19744
19745 /* Return the named attribute or NULL if not there. */
19746
19747 static struct attribute *
19748 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19749 {
19750 for (;;)
19751 {
19752 unsigned int i;
19753 struct attribute *spec = NULL;
19754
19755 for (i = 0; i < die->num_attrs; ++i)
19756 {
19757 if (die->attrs[i].name == name)
19758 return &die->attrs[i];
19759 if (die->attrs[i].name == DW_AT_specification
19760 || die->attrs[i].name == DW_AT_abstract_origin)
19761 spec = &die->attrs[i];
19762 }
19763
19764 if (!spec)
19765 break;
19766
19767 die = follow_die_ref (die, spec, &cu);
19768 }
19769
19770 return NULL;
19771 }
19772
19773 /* Return the named attribute or NULL if not there,
19774 but do not follow DW_AT_specification, etc.
19775 This is for use in contexts where we're reading .debug_types dies.
19776 Following DW_AT_specification, DW_AT_abstract_origin will take us
19777 back up the chain, and we want to go down. */
19778
19779 static struct attribute *
19780 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19781 {
19782 unsigned int i;
19783
19784 for (i = 0; i < die->num_attrs; ++i)
19785 if (die->attrs[i].name == name)
19786 return &die->attrs[i];
19787
19788 return NULL;
19789 }
19790
19791 /* Return the string associated with a string-typed attribute, or NULL if it
19792 is either not found or is of an incorrect type. */
19793
19794 static const char *
19795 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19796 {
19797 struct attribute *attr;
19798 const char *str = NULL;
19799
19800 attr = dwarf2_attr (die, name, cu);
19801
19802 if (attr != NULL)
19803 {
19804 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19805 || attr->form == DW_FORM_string
19806 || attr->form == DW_FORM_GNU_str_index
19807 || attr->form == DW_FORM_GNU_strp_alt)
19808 str = DW_STRING (attr);
19809 else
19810 complaint (&symfile_complaints,
19811 _("string type expected for attribute %s for "
19812 "DIE at 0x%x in module %s"),
19813 dwarf_attr_name (name), to_underlying (die->sect_off),
19814 objfile_name (cu->objfile));
19815 }
19816
19817 return str;
19818 }
19819
19820 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19821 and holds a non-zero value. This function should only be used for
19822 DW_FORM_flag or DW_FORM_flag_present attributes. */
19823
19824 static int
19825 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19826 {
19827 struct attribute *attr = dwarf2_attr (die, name, cu);
19828
19829 return (attr && DW_UNSND (attr));
19830 }
19831
19832 static int
19833 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19834 {
19835 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19836 which value is non-zero. However, we have to be careful with
19837 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19838 (via dwarf2_flag_true_p) follows this attribute. So we may
19839 end up accidently finding a declaration attribute that belongs
19840 to a different DIE referenced by the specification attribute,
19841 even though the given DIE does not have a declaration attribute. */
19842 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19843 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19844 }
19845
19846 /* Return the die giving the specification for DIE, if there is
19847 one. *SPEC_CU is the CU containing DIE on input, and the CU
19848 containing the return value on output. If there is no
19849 specification, but there is an abstract origin, that is
19850 returned. */
19851
19852 static struct die_info *
19853 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19854 {
19855 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19856 *spec_cu);
19857
19858 if (spec_attr == NULL)
19859 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19860
19861 if (spec_attr == NULL)
19862 return NULL;
19863 else
19864 return follow_die_ref (die, spec_attr, spec_cu);
19865 }
19866
19867 /* Stub for free_line_header to match void * callback types. */
19868
19869 static void
19870 free_line_header_voidp (void *arg)
19871 {
19872 struct line_header *lh = (struct line_header *) arg;
19873
19874 delete lh;
19875 }
19876
19877 void
19878 line_header::add_include_dir (const char *include_dir)
19879 {
19880 if (dwarf_line_debug >= 2)
19881 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19882 include_dirs.size () + 1, include_dir);
19883
19884 include_dirs.push_back (include_dir);
19885 }
19886
19887 void
19888 line_header::add_file_name (const char *name,
19889 dir_index d_index,
19890 unsigned int mod_time,
19891 unsigned int length)
19892 {
19893 if (dwarf_line_debug >= 2)
19894 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
19895 (unsigned) file_names.size () + 1, name);
19896
19897 file_names.emplace_back (name, d_index, mod_time, length);
19898 }
19899
19900 /* A convenience function to find the proper .debug_line section for a CU. */
19901
19902 static struct dwarf2_section_info *
19903 get_debug_line_section (struct dwarf2_cu *cu)
19904 {
19905 struct dwarf2_section_info *section;
19906
19907 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19908 DWO file. */
19909 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19910 section = &cu->dwo_unit->dwo_file->sections.line;
19911 else if (cu->per_cu->is_dwz)
19912 {
19913 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19914
19915 section = &dwz->line;
19916 }
19917 else
19918 section = &dwarf2_per_objfile->line;
19919
19920 return section;
19921 }
19922
19923 /* Read directory or file name entry format, starting with byte of
19924 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19925 entries count and the entries themselves in the described entry
19926 format. */
19927
19928 static void
19929 read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
19930 struct line_header *lh,
19931 const struct comp_unit_head *cu_header,
19932 void (*callback) (struct line_header *lh,
19933 const char *name,
19934 dir_index d_index,
19935 unsigned int mod_time,
19936 unsigned int length))
19937 {
19938 gdb_byte format_count, formati;
19939 ULONGEST data_count, datai;
19940 const gdb_byte *buf = *bufp;
19941 const gdb_byte *format_header_data;
19942 unsigned int bytes_read;
19943
19944 format_count = read_1_byte (abfd, buf);
19945 buf += 1;
19946 format_header_data = buf;
19947 for (formati = 0; formati < format_count; formati++)
19948 {
19949 read_unsigned_leb128 (abfd, buf, &bytes_read);
19950 buf += bytes_read;
19951 read_unsigned_leb128 (abfd, buf, &bytes_read);
19952 buf += bytes_read;
19953 }
19954
19955 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19956 buf += bytes_read;
19957 for (datai = 0; datai < data_count; datai++)
19958 {
19959 const gdb_byte *format = format_header_data;
19960 struct file_entry fe;
19961
19962 for (formati = 0; formati < format_count; formati++)
19963 {
19964 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
19965 format += bytes_read;
19966
19967 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
19968 format += bytes_read;
19969
19970 gdb::optional<const char *> string;
19971 gdb::optional<unsigned int> uint;
19972
19973 switch (form)
19974 {
19975 case DW_FORM_string:
19976 string.emplace (read_direct_string (abfd, buf, &bytes_read));
19977 buf += bytes_read;
19978 break;
19979
19980 case DW_FORM_line_strp:
19981 string.emplace (read_indirect_line_string (abfd, buf,
19982 cu_header,
19983 &bytes_read));
19984 buf += bytes_read;
19985 break;
19986
19987 case DW_FORM_data1:
19988 uint.emplace (read_1_byte (abfd, buf));
19989 buf += 1;
19990 break;
19991
19992 case DW_FORM_data2:
19993 uint.emplace (read_2_bytes (abfd, buf));
19994 buf += 2;
19995 break;
19996
19997 case DW_FORM_data4:
19998 uint.emplace (read_4_bytes (abfd, buf));
19999 buf += 4;
20000 break;
20001
20002 case DW_FORM_data8:
20003 uint.emplace (read_8_bytes (abfd, buf));
20004 buf += 8;
20005 break;
20006
20007 case DW_FORM_udata:
20008 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20009 buf += bytes_read;
20010 break;
20011
20012 case DW_FORM_block:
20013 /* It is valid only for DW_LNCT_timestamp which is ignored by
20014 current GDB. */
20015 break;
20016 }
20017
20018 switch (content_type)
20019 {
20020 case DW_LNCT_path:
20021 if (string.has_value ())
20022 fe.name = *string;
20023 break;
20024 case DW_LNCT_directory_index:
20025 if (uint.has_value ())
20026 fe.d_index = (dir_index) *uint;
20027 break;
20028 case DW_LNCT_timestamp:
20029 if (uint.has_value ())
20030 fe.mod_time = *uint;
20031 break;
20032 case DW_LNCT_size:
20033 if (uint.has_value ())
20034 fe.length = *uint;
20035 break;
20036 case DW_LNCT_MD5:
20037 break;
20038 default:
20039 complaint (&symfile_complaints,
20040 _("Unknown format content type %s"),
20041 pulongest (content_type));
20042 }
20043 }
20044
20045 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20046 }
20047
20048 *bufp = buf;
20049 }
20050
20051 /* Read the statement program header starting at OFFSET in
20052 .debug_line, or .debug_line.dwo. Return a pointer
20053 to a struct line_header, allocated using xmalloc.
20054 Returns NULL if there is a problem reading the header, e.g., if it
20055 has a version we don't understand.
20056
20057 NOTE: the strings in the include directory and file name tables of
20058 the returned object point into the dwarf line section buffer,
20059 and must not be freed. */
20060
20061 static line_header_up
20062 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20063 {
20064 const gdb_byte *line_ptr;
20065 unsigned int bytes_read, offset_size;
20066 int i;
20067 const char *cur_dir, *cur_file;
20068 struct dwarf2_section_info *section;
20069 bfd *abfd;
20070
20071 section = get_debug_line_section (cu);
20072 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20073 if (section->buffer == NULL)
20074 {
20075 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20076 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20077 else
20078 complaint (&symfile_complaints, _("missing .debug_line section"));
20079 return 0;
20080 }
20081
20082 /* We can't do this until we know the section is non-empty.
20083 Only then do we know we have such a section. */
20084 abfd = get_section_bfd_owner (section);
20085
20086 /* Make sure that at least there's room for the total_length field.
20087 That could be 12 bytes long, but we're just going to fudge that. */
20088 if (to_underlying (sect_off) + 4 >= section->size)
20089 {
20090 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20091 return 0;
20092 }
20093
20094 line_header_up lh (new line_header ());
20095
20096 lh->sect_off = sect_off;
20097 lh->offset_in_dwz = cu->per_cu->is_dwz;
20098
20099 line_ptr = section->buffer + to_underlying (sect_off);
20100
20101 /* Read in the header. */
20102 lh->total_length =
20103 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20104 &bytes_read, &offset_size);
20105 line_ptr += bytes_read;
20106 if (line_ptr + lh->total_length > (section->buffer + section->size))
20107 {
20108 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20109 return 0;
20110 }
20111 lh->statement_program_end = line_ptr + lh->total_length;
20112 lh->version = read_2_bytes (abfd, line_ptr);
20113 line_ptr += 2;
20114 if (lh->version > 5)
20115 {
20116 /* This is a version we don't understand. The format could have
20117 changed in ways we don't handle properly so just punt. */
20118 complaint (&symfile_complaints,
20119 _("unsupported version in .debug_line section"));
20120 return NULL;
20121 }
20122 if (lh->version >= 5)
20123 {
20124 gdb_byte segment_selector_size;
20125
20126 /* Skip address size. */
20127 read_1_byte (abfd, line_ptr);
20128 line_ptr += 1;
20129
20130 segment_selector_size = read_1_byte (abfd, line_ptr);
20131 line_ptr += 1;
20132 if (segment_selector_size != 0)
20133 {
20134 complaint (&symfile_complaints,
20135 _("unsupported segment selector size %u "
20136 "in .debug_line section"),
20137 segment_selector_size);
20138 return NULL;
20139 }
20140 }
20141 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20142 line_ptr += offset_size;
20143 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20144 line_ptr += 1;
20145 if (lh->version >= 4)
20146 {
20147 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20148 line_ptr += 1;
20149 }
20150 else
20151 lh->maximum_ops_per_instruction = 1;
20152
20153 if (lh->maximum_ops_per_instruction == 0)
20154 {
20155 lh->maximum_ops_per_instruction = 1;
20156 complaint (&symfile_complaints,
20157 _("invalid maximum_ops_per_instruction "
20158 "in `.debug_line' section"));
20159 }
20160
20161 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20162 line_ptr += 1;
20163 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20164 line_ptr += 1;
20165 lh->line_range = read_1_byte (abfd, line_ptr);
20166 line_ptr += 1;
20167 lh->opcode_base = read_1_byte (abfd, line_ptr);
20168 line_ptr += 1;
20169 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20170
20171 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20172 for (i = 1; i < lh->opcode_base; ++i)
20173 {
20174 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20175 line_ptr += 1;
20176 }
20177
20178 if (lh->version >= 5)
20179 {
20180 /* Read directory table. */
20181 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
20182 [] (struct line_header *lh, const char *name,
20183 dir_index d_index, unsigned int mod_time,
20184 unsigned int length)
20185 {
20186 lh->add_include_dir (name);
20187 });
20188
20189 /* Read file name table. */
20190 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
20191 [] (struct line_header *lh, const char *name,
20192 dir_index d_index, unsigned int mod_time,
20193 unsigned int length)
20194 {
20195 lh->add_file_name (name, d_index, mod_time, length);
20196 });
20197 }
20198 else
20199 {
20200 /* Read directory table. */
20201 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20202 {
20203 line_ptr += bytes_read;
20204 lh->add_include_dir (cur_dir);
20205 }
20206 line_ptr += bytes_read;
20207
20208 /* Read file name table. */
20209 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20210 {
20211 unsigned int mod_time, length;
20212 dir_index d_index;
20213
20214 line_ptr += bytes_read;
20215 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20216 line_ptr += bytes_read;
20217 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20218 line_ptr += bytes_read;
20219 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20220 line_ptr += bytes_read;
20221
20222 lh->add_file_name (cur_file, d_index, mod_time, length);
20223 }
20224 line_ptr += bytes_read;
20225 }
20226 lh->statement_program_start = line_ptr;
20227
20228 if (line_ptr > (section->buffer + section->size))
20229 complaint (&symfile_complaints,
20230 _("line number info header doesn't "
20231 "fit in `.debug_line' section"));
20232
20233 return lh;
20234 }
20235
20236 /* Subroutine of dwarf_decode_lines to simplify it.
20237 Return the file name of the psymtab for included file FILE_INDEX
20238 in line header LH of PST.
20239 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20240 If space for the result is malloc'd, it will be freed by a cleanup.
20241 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
20242
20243 The function creates dangling cleanup registration. */
20244
20245 static const char *
20246 psymtab_include_file_name (const struct line_header *lh, int file_index,
20247 const struct partial_symtab *pst,
20248 const char *comp_dir)
20249 {
20250 const file_entry &fe = lh->file_names[file_index];
20251 const char *include_name = fe.name;
20252 const char *include_name_to_compare = include_name;
20253 const char *pst_filename;
20254 char *copied_name = NULL;
20255 int file_is_pst;
20256
20257 const char *dir_name = fe.include_dir (lh);
20258
20259 if (!IS_ABSOLUTE_PATH (include_name)
20260 && (dir_name != NULL || comp_dir != NULL))
20261 {
20262 /* Avoid creating a duplicate psymtab for PST.
20263 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20264 Before we do the comparison, however, we need to account
20265 for DIR_NAME and COMP_DIR.
20266 First prepend dir_name (if non-NULL). If we still don't
20267 have an absolute path prepend comp_dir (if non-NULL).
20268 However, the directory we record in the include-file's
20269 psymtab does not contain COMP_DIR (to match the
20270 corresponding symtab(s)).
20271
20272 Example:
20273
20274 bash$ cd /tmp
20275 bash$ gcc -g ./hello.c
20276 include_name = "hello.c"
20277 dir_name = "."
20278 DW_AT_comp_dir = comp_dir = "/tmp"
20279 DW_AT_name = "./hello.c"
20280
20281 */
20282
20283 if (dir_name != NULL)
20284 {
20285 char *tem = concat (dir_name, SLASH_STRING,
20286 include_name, (char *)NULL);
20287
20288 make_cleanup (xfree, tem);
20289 include_name = tem;
20290 include_name_to_compare = include_name;
20291 }
20292 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20293 {
20294 char *tem = concat (comp_dir, SLASH_STRING,
20295 include_name, (char *)NULL);
20296
20297 make_cleanup (xfree, tem);
20298 include_name_to_compare = tem;
20299 }
20300 }
20301
20302 pst_filename = pst->filename;
20303 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20304 {
20305 copied_name = concat (pst->dirname, SLASH_STRING,
20306 pst_filename, (char *)NULL);
20307 pst_filename = copied_name;
20308 }
20309
20310 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20311
20312 if (copied_name != NULL)
20313 xfree (copied_name);
20314
20315 if (file_is_pst)
20316 return NULL;
20317 return include_name;
20318 }
20319
20320 /* State machine to track the state of the line number program. */
20321
20322 class lnp_state_machine
20323 {
20324 public:
20325 /* Initialize a machine state for the start of a line number
20326 program. */
20327 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20328
20329 file_entry *current_file ()
20330 {
20331 /* lh->file_names is 0-based, but the file name numbers in the
20332 statement program are 1-based. */
20333 return m_line_header->file_name_at (m_file);
20334 }
20335
20336 /* Record the line in the state machine. END_SEQUENCE is true if
20337 we're processing the end of a sequence. */
20338 void record_line (bool end_sequence);
20339
20340 /* Check address and if invalid nop-out the rest of the lines in this
20341 sequence. */
20342 void check_line_address (struct dwarf2_cu *cu,
20343 const gdb_byte *line_ptr,
20344 CORE_ADDR lowpc, CORE_ADDR address);
20345
20346 void handle_set_discriminator (unsigned int discriminator)
20347 {
20348 m_discriminator = discriminator;
20349 m_line_has_non_zero_discriminator |= discriminator != 0;
20350 }
20351
20352 /* Handle DW_LNE_set_address. */
20353 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20354 {
20355 m_op_index = 0;
20356 address += baseaddr;
20357 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20358 }
20359
20360 /* Handle DW_LNS_advance_pc. */
20361 void handle_advance_pc (CORE_ADDR adjust);
20362
20363 /* Handle a special opcode. */
20364 void handle_special_opcode (unsigned char op_code);
20365
20366 /* Handle DW_LNS_advance_line. */
20367 void handle_advance_line (int line_delta)
20368 {
20369 advance_line (line_delta);
20370 }
20371
20372 /* Handle DW_LNS_set_file. */
20373 void handle_set_file (file_name_index file);
20374
20375 /* Handle DW_LNS_negate_stmt. */
20376 void handle_negate_stmt ()
20377 {
20378 m_is_stmt = !m_is_stmt;
20379 }
20380
20381 /* Handle DW_LNS_const_add_pc. */
20382 void handle_const_add_pc ();
20383
20384 /* Handle DW_LNS_fixed_advance_pc. */
20385 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20386 {
20387 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20388 m_op_index = 0;
20389 }
20390
20391 /* Handle DW_LNS_copy. */
20392 void handle_copy ()
20393 {
20394 record_line (false);
20395 m_discriminator = 0;
20396 }
20397
20398 /* Handle DW_LNE_end_sequence. */
20399 void handle_end_sequence ()
20400 {
20401 m_record_line_callback = ::record_line;
20402 }
20403
20404 private:
20405 /* Advance the line by LINE_DELTA. */
20406 void advance_line (int line_delta)
20407 {
20408 m_line += line_delta;
20409
20410 if (line_delta != 0)
20411 m_line_has_non_zero_discriminator = m_discriminator != 0;
20412 }
20413
20414 gdbarch *m_gdbarch;
20415
20416 /* True if we're recording lines.
20417 Otherwise we're building partial symtabs and are just interested in
20418 finding include files mentioned by the line number program. */
20419 bool m_record_lines_p;
20420
20421 /* The line number header. */
20422 line_header *m_line_header;
20423
20424 /* These are part of the standard DWARF line number state machine,
20425 and initialized according to the DWARF spec. */
20426
20427 unsigned char m_op_index = 0;
20428 /* The line table index (1-based) of the current file. */
20429 file_name_index m_file = (file_name_index) 1;
20430 unsigned int m_line = 1;
20431
20432 /* These are initialized in the constructor. */
20433
20434 CORE_ADDR m_address;
20435 bool m_is_stmt;
20436 unsigned int m_discriminator;
20437
20438 /* Additional bits of state we need to track. */
20439
20440 /* The last file that we called dwarf2_start_subfile for.
20441 This is only used for TLLs. */
20442 unsigned int m_last_file = 0;
20443 /* The last file a line number was recorded for. */
20444 struct subfile *m_last_subfile = NULL;
20445
20446 /* The function to call to record a line. */
20447 record_line_ftype *m_record_line_callback = NULL;
20448
20449 /* The last line number that was recorded, used to coalesce
20450 consecutive entries for the same line. This can happen, for
20451 example, when discriminators are present. PR 17276. */
20452 unsigned int m_last_line = 0;
20453 bool m_line_has_non_zero_discriminator = false;
20454 };
20455
20456 void
20457 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20458 {
20459 CORE_ADDR addr_adj = (((m_op_index + adjust)
20460 / m_line_header->maximum_ops_per_instruction)
20461 * m_line_header->minimum_instruction_length);
20462 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20463 m_op_index = ((m_op_index + adjust)
20464 % m_line_header->maximum_ops_per_instruction);
20465 }
20466
20467 void
20468 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20469 {
20470 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20471 CORE_ADDR addr_adj = (((m_op_index
20472 + (adj_opcode / m_line_header->line_range))
20473 / m_line_header->maximum_ops_per_instruction)
20474 * m_line_header->minimum_instruction_length);
20475 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20476 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20477 % m_line_header->maximum_ops_per_instruction);
20478
20479 int line_delta = (m_line_header->line_base
20480 + (adj_opcode % m_line_header->line_range));
20481 advance_line (line_delta);
20482 record_line (false);
20483 m_discriminator = 0;
20484 }
20485
20486 void
20487 lnp_state_machine::handle_set_file (file_name_index file)
20488 {
20489 m_file = file;
20490
20491 const file_entry *fe = current_file ();
20492 if (fe == NULL)
20493 dwarf2_debug_line_missing_file_complaint ();
20494 else if (m_record_lines_p)
20495 {
20496 const char *dir = fe->include_dir (m_line_header);
20497
20498 m_last_subfile = current_subfile;
20499 m_line_has_non_zero_discriminator = m_discriminator != 0;
20500 dwarf2_start_subfile (fe->name, dir);
20501 }
20502 }
20503
20504 void
20505 lnp_state_machine::handle_const_add_pc ()
20506 {
20507 CORE_ADDR adjust
20508 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20509
20510 CORE_ADDR addr_adj
20511 = (((m_op_index + adjust)
20512 / m_line_header->maximum_ops_per_instruction)
20513 * m_line_header->minimum_instruction_length);
20514
20515 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20516 m_op_index = ((m_op_index + adjust)
20517 % m_line_header->maximum_ops_per_instruction);
20518 }
20519
20520 /* Ignore this record_line request. */
20521
20522 static void
20523 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20524 {
20525 return;
20526 }
20527
20528 /* Return non-zero if we should add LINE to the line number table.
20529 LINE is the line to add, LAST_LINE is the last line that was added,
20530 LAST_SUBFILE is the subfile for LAST_LINE.
20531 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20532 had a non-zero discriminator.
20533
20534 We have to be careful in the presence of discriminators.
20535 E.g., for this line:
20536
20537 for (i = 0; i < 100000; i++);
20538
20539 clang can emit four line number entries for that one line,
20540 each with a different discriminator.
20541 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20542
20543 However, we want gdb to coalesce all four entries into one.
20544 Otherwise the user could stepi into the middle of the line and
20545 gdb would get confused about whether the pc really was in the
20546 middle of the line.
20547
20548 Things are further complicated by the fact that two consecutive
20549 line number entries for the same line is a heuristic used by gcc
20550 to denote the end of the prologue. So we can't just discard duplicate
20551 entries, we have to be selective about it. The heuristic we use is
20552 that we only collapse consecutive entries for the same line if at least
20553 one of those entries has a non-zero discriminator. PR 17276.
20554
20555 Note: Addresses in the line number state machine can never go backwards
20556 within one sequence, thus this coalescing is ok. */
20557
20558 static int
20559 dwarf_record_line_p (unsigned int line, unsigned int last_line,
20560 int line_has_non_zero_discriminator,
20561 struct subfile *last_subfile)
20562 {
20563 if (current_subfile != last_subfile)
20564 return 1;
20565 if (line != last_line)
20566 return 1;
20567 /* Same line for the same file that we've seen already.
20568 As a last check, for pr 17276, only record the line if the line
20569 has never had a non-zero discriminator. */
20570 if (!line_has_non_zero_discriminator)
20571 return 1;
20572 return 0;
20573 }
20574
20575 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20576 in the line table of subfile SUBFILE. */
20577
20578 static void
20579 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20580 unsigned int line, CORE_ADDR address,
20581 record_line_ftype p_record_line)
20582 {
20583 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20584
20585 if (dwarf_line_debug)
20586 {
20587 fprintf_unfiltered (gdb_stdlog,
20588 "Recording line %u, file %s, address %s\n",
20589 line, lbasename (subfile->name),
20590 paddress (gdbarch, address));
20591 }
20592
20593 (*p_record_line) (subfile, line, addr);
20594 }
20595
20596 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20597 Mark the end of a set of line number records.
20598 The arguments are the same as for dwarf_record_line_1.
20599 If SUBFILE is NULL the request is ignored. */
20600
20601 static void
20602 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20603 CORE_ADDR address, record_line_ftype p_record_line)
20604 {
20605 if (subfile == NULL)
20606 return;
20607
20608 if (dwarf_line_debug)
20609 {
20610 fprintf_unfiltered (gdb_stdlog,
20611 "Finishing current line, file %s, address %s\n",
20612 lbasename (subfile->name),
20613 paddress (gdbarch, address));
20614 }
20615
20616 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20617 }
20618
20619 void
20620 lnp_state_machine::record_line (bool end_sequence)
20621 {
20622 if (dwarf_line_debug)
20623 {
20624 fprintf_unfiltered (gdb_stdlog,
20625 "Processing actual line %u: file %u,"
20626 " address %s, is_stmt %u, discrim %u\n",
20627 m_line, to_underlying (m_file),
20628 paddress (m_gdbarch, m_address),
20629 m_is_stmt, m_discriminator);
20630 }
20631
20632 file_entry *fe = current_file ();
20633
20634 if (fe == NULL)
20635 dwarf2_debug_line_missing_file_complaint ();
20636 /* For now we ignore lines not starting on an instruction boundary.
20637 But not when processing end_sequence for compatibility with the
20638 previous version of the code. */
20639 else if (m_op_index == 0 || end_sequence)
20640 {
20641 fe->included_p = 1;
20642 if (m_record_lines_p && m_is_stmt)
20643 {
20644 if (m_last_subfile != current_subfile || end_sequence)
20645 {
20646 dwarf_finish_line (m_gdbarch, m_last_subfile,
20647 m_address, m_record_line_callback);
20648 }
20649
20650 if (!end_sequence)
20651 {
20652 if (dwarf_record_line_p (m_line, m_last_line,
20653 m_line_has_non_zero_discriminator,
20654 m_last_subfile))
20655 {
20656 dwarf_record_line_1 (m_gdbarch, current_subfile,
20657 m_line, m_address,
20658 m_record_line_callback);
20659 }
20660 m_last_subfile = current_subfile;
20661 m_last_line = m_line;
20662 }
20663 }
20664 }
20665 }
20666
20667 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20668 bool record_lines_p)
20669 {
20670 m_gdbarch = arch;
20671 m_record_lines_p = record_lines_p;
20672 m_line_header = lh;
20673
20674 m_record_line_callback = ::record_line;
20675
20676 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20677 was a line entry for it so that the backend has a chance to adjust it
20678 and also record it in case it needs it. This is currently used by MIPS
20679 code, cf. `mips_adjust_dwarf2_line'. */
20680 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20681 m_is_stmt = lh->default_is_stmt;
20682 m_discriminator = 0;
20683 }
20684
20685 void
20686 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20687 const gdb_byte *line_ptr,
20688 CORE_ADDR lowpc, CORE_ADDR address)
20689 {
20690 /* If address < lowpc then it's not a usable value, it's outside the
20691 pc range of the CU. However, we restrict the test to only address
20692 values of zero to preserve GDB's previous behaviour which is to
20693 handle the specific case of a function being GC'd by the linker. */
20694
20695 if (address == 0 && address < lowpc)
20696 {
20697 /* This line table is for a function which has been
20698 GCd by the linker. Ignore it. PR gdb/12528 */
20699
20700 struct objfile *objfile = cu->objfile;
20701 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20702
20703 complaint (&symfile_complaints,
20704 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20705 line_offset, objfile_name (objfile));
20706 m_record_line_callback = noop_record_line;
20707 /* Note: record_line_callback is left as noop_record_line until
20708 we see DW_LNE_end_sequence. */
20709 }
20710 }
20711
20712 /* Subroutine of dwarf_decode_lines to simplify it.
20713 Process the line number information in LH.
20714 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20715 program in order to set included_p for every referenced header. */
20716
20717 static void
20718 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20719 const int decode_for_pst_p, CORE_ADDR lowpc)
20720 {
20721 const gdb_byte *line_ptr, *extended_end;
20722 const gdb_byte *line_end;
20723 unsigned int bytes_read, extended_len;
20724 unsigned char op_code, extended_op;
20725 CORE_ADDR baseaddr;
20726 struct objfile *objfile = cu->objfile;
20727 bfd *abfd = objfile->obfd;
20728 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20729 /* True if we're recording line info (as opposed to building partial
20730 symtabs and just interested in finding include files mentioned by
20731 the line number program). */
20732 bool record_lines_p = !decode_for_pst_p;
20733
20734 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20735
20736 line_ptr = lh->statement_program_start;
20737 line_end = lh->statement_program_end;
20738
20739 /* Read the statement sequences until there's nothing left. */
20740 while (line_ptr < line_end)
20741 {
20742 /* The DWARF line number program state machine. Reset the state
20743 machine at the start of each sequence. */
20744 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20745 bool end_sequence = false;
20746
20747 if (record_lines_p)
20748 {
20749 /* Start a subfile for the current file of the state
20750 machine. */
20751 const file_entry *fe = state_machine.current_file ();
20752
20753 if (fe != NULL)
20754 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
20755 }
20756
20757 /* Decode the table. */
20758 while (line_ptr < line_end && !end_sequence)
20759 {
20760 op_code = read_1_byte (abfd, line_ptr);
20761 line_ptr += 1;
20762
20763 if (op_code >= lh->opcode_base)
20764 {
20765 /* Special opcode. */
20766 state_machine.handle_special_opcode (op_code);
20767 }
20768 else switch (op_code)
20769 {
20770 case DW_LNS_extended_op:
20771 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20772 &bytes_read);
20773 line_ptr += bytes_read;
20774 extended_end = line_ptr + extended_len;
20775 extended_op = read_1_byte (abfd, line_ptr);
20776 line_ptr += 1;
20777 switch (extended_op)
20778 {
20779 case DW_LNE_end_sequence:
20780 state_machine.handle_end_sequence ();
20781 end_sequence = true;
20782 break;
20783 case DW_LNE_set_address:
20784 {
20785 CORE_ADDR address
20786 = read_address (abfd, line_ptr, cu, &bytes_read);
20787 line_ptr += bytes_read;
20788
20789 state_machine.check_line_address (cu, line_ptr,
20790 lowpc, address);
20791 state_machine.handle_set_address (baseaddr, address);
20792 }
20793 break;
20794 case DW_LNE_define_file:
20795 {
20796 const char *cur_file;
20797 unsigned int mod_time, length;
20798 dir_index dindex;
20799
20800 cur_file = read_direct_string (abfd, line_ptr,
20801 &bytes_read);
20802 line_ptr += bytes_read;
20803 dindex = (dir_index)
20804 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20805 line_ptr += bytes_read;
20806 mod_time =
20807 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20808 line_ptr += bytes_read;
20809 length =
20810 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20811 line_ptr += bytes_read;
20812 lh->add_file_name (cur_file, dindex, mod_time, length);
20813 }
20814 break;
20815 case DW_LNE_set_discriminator:
20816 {
20817 /* The discriminator is not interesting to the
20818 debugger; just ignore it. We still need to
20819 check its value though:
20820 if there are consecutive entries for the same
20821 (non-prologue) line we want to coalesce them.
20822 PR 17276. */
20823 unsigned int discr
20824 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20825 line_ptr += bytes_read;
20826
20827 state_machine.handle_set_discriminator (discr);
20828 }
20829 break;
20830 default:
20831 complaint (&symfile_complaints,
20832 _("mangled .debug_line section"));
20833 return;
20834 }
20835 /* Make sure that we parsed the extended op correctly. If e.g.
20836 we expected a different address size than the producer used,
20837 we may have read the wrong number of bytes. */
20838 if (line_ptr != extended_end)
20839 {
20840 complaint (&symfile_complaints,
20841 _("mangled .debug_line section"));
20842 return;
20843 }
20844 break;
20845 case DW_LNS_copy:
20846 state_machine.handle_copy ();
20847 break;
20848 case DW_LNS_advance_pc:
20849 {
20850 CORE_ADDR adjust
20851 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20852 line_ptr += bytes_read;
20853
20854 state_machine.handle_advance_pc (adjust);
20855 }
20856 break;
20857 case DW_LNS_advance_line:
20858 {
20859 int line_delta
20860 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20861 line_ptr += bytes_read;
20862
20863 state_machine.handle_advance_line (line_delta);
20864 }
20865 break;
20866 case DW_LNS_set_file:
20867 {
20868 file_name_index file
20869 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20870 &bytes_read);
20871 line_ptr += bytes_read;
20872
20873 state_machine.handle_set_file (file);
20874 }
20875 break;
20876 case DW_LNS_set_column:
20877 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20878 line_ptr += bytes_read;
20879 break;
20880 case DW_LNS_negate_stmt:
20881 state_machine.handle_negate_stmt ();
20882 break;
20883 case DW_LNS_set_basic_block:
20884 break;
20885 /* Add to the address register of the state machine the
20886 address increment value corresponding to special opcode
20887 255. I.e., this value is scaled by the minimum
20888 instruction length since special opcode 255 would have
20889 scaled the increment. */
20890 case DW_LNS_const_add_pc:
20891 state_machine.handle_const_add_pc ();
20892 break;
20893 case DW_LNS_fixed_advance_pc:
20894 {
20895 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20896 line_ptr += 2;
20897
20898 state_machine.handle_fixed_advance_pc (addr_adj);
20899 }
20900 break;
20901 default:
20902 {
20903 /* Unknown standard opcode, ignore it. */
20904 int i;
20905
20906 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20907 {
20908 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20909 line_ptr += bytes_read;
20910 }
20911 }
20912 }
20913 }
20914
20915 if (!end_sequence)
20916 dwarf2_debug_line_missing_end_sequence_complaint ();
20917
20918 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20919 in which case we still finish recording the last line). */
20920 state_machine.record_line (true);
20921 }
20922 }
20923
20924 /* Decode the Line Number Program (LNP) for the given line_header
20925 structure and CU. The actual information extracted and the type
20926 of structures created from the LNP depends on the value of PST.
20927
20928 1. If PST is NULL, then this procedure uses the data from the program
20929 to create all necessary symbol tables, and their linetables.
20930
20931 2. If PST is not NULL, this procedure reads the program to determine
20932 the list of files included by the unit represented by PST, and
20933 builds all the associated partial symbol tables.
20934
20935 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20936 It is used for relative paths in the line table.
20937 NOTE: When processing partial symtabs (pst != NULL),
20938 comp_dir == pst->dirname.
20939
20940 NOTE: It is important that psymtabs have the same file name (via strcmp)
20941 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20942 symtab we don't use it in the name of the psymtabs we create.
20943 E.g. expand_line_sal requires this when finding psymtabs to expand.
20944 A good testcase for this is mb-inline.exp.
20945
20946 LOWPC is the lowest address in CU (or 0 if not known).
20947
20948 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20949 for its PC<->lines mapping information. Otherwise only the filename
20950 table is read in. */
20951
20952 static void
20953 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20954 struct dwarf2_cu *cu, struct partial_symtab *pst,
20955 CORE_ADDR lowpc, int decode_mapping)
20956 {
20957 struct objfile *objfile = cu->objfile;
20958 const int decode_for_pst_p = (pst != NULL);
20959
20960 if (decode_mapping)
20961 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20962
20963 if (decode_for_pst_p)
20964 {
20965 int file_index;
20966
20967 /* Now that we're done scanning the Line Header Program, we can
20968 create the psymtab of each included file. */
20969 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
20970 if (lh->file_names[file_index].included_p == 1)
20971 {
20972 const char *include_name =
20973 psymtab_include_file_name (lh, file_index, pst, comp_dir);
20974 if (include_name != NULL)
20975 dwarf2_create_include_psymtab (include_name, pst, objfile);
20976 }
20977 }
20978 else
20979 {
20980 /* Make sure a symtab is created for every file, even files
20981 which contain only variables (i.e. no code with associated
20982 line numbers). */
20983 struct compunit_symtab *cust = buildsym_compunit_symtab ();
20984 int i;
20985
20986 for (i = 0; i < lh->file_names.size (); i++)
20987 {
20988 file_entry &fe = lh->file_names[i];
20989
20990 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
20991
20992 if (current_subfile->symtab == NULL)
20993 {
20994 current_subfile->symtab
20995 = allocate_symtab (cust, current_subfile->name);
20996 }
20997 fe.symtab = current_subfile->symtab;
20998 }
20999 }
21000 }
21001
21002 /* Start a subfile for DWARF. FILENAME is the name of the file and
21003 DIRNAME the name of the source directory which contains FILENAME
21004 or NULL if not known.
21005 This routine tries to keep line numbers from identical absolute and
21006 relative file names in a common subfile.
21007
21008 Using the `list' example from the GDB testsuite, which resides in
21009 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21010 of /srcdir/list0.c yields the following debugging information for list0.c:
21011
21012 DW_AT_name: /srcdir/list0.c
21013 DW_AT_comp_dir: /compdir
21014 files.files[0].name: list0.h
21015 files.files[0].dir: /srcdir
21016 files.files[1].name: list0.c
21017 files.files[1].dir: /srcdir
21018
21019 The line number information for list0.c has to end up in a single
21020 subfile, so that `break /srcdir/list0.c:1' works as expected.
21021 start_subfile will ensure that this happens provided that we pass the
21022 concatenation of files.files[1].dir and files.files[1].name as the
21023 subfile's name. */
21024
21025 static void
21026 dwarf2_start_subfile (const char *filename, const char *dirname)
21027 {
21028 char *copy = NULL;
21029
21030 /* In order not to lose the line information directory,
21031 we concatenate it to the filename when it makes sense.
21032 Note that the Dwarf3 standard says (speaking of filenames in line
21033 information): ``The directory index is ignored for file names
21034 that represent full path names''. Thus ignoring dirname in the
21035 `else' branch below isn't an issue. */
21036
21037 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21038 {
21039 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21040 filename = copy;
21041 }
21042
21043 start_subfile (filename);
21044
21045 if (copy != NULL)
21046 xfree (copy);
21047 }
21048
21049 /* Start a symtab for DWARF.
21050 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21051
21052 static struct compunit_symtab *
21053 dwarf2_start_symtab (struct dwarf2_cu *cu,
21054 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21055 {
21056 struct compunit_symtab *cust
21057 = start_symtab (cu->objfile, name, comp_dir, low_pc, cu->language);
21058
21059 record_debugformat ("DWARF 2");
21060 record_producer (cu->producer);
21061
21062 /* We assume that we're processing GCC output. */
21063 processing_gcc_compilation = 2;
21064
21065 cu->processing_has_namespace_info = 0;
21066
21067 return cust;
21068 }
21069
21070 static void
21071 var_decode_location (struct attribute *attr, struct symbol *sym,
21072 struct dwarf2_cu *cu)
21073 {
21074 struct objfile *objfile = cu->objfile;
21075 struct comp_unit_head *cu_header = &cu->header;
21076
21077 /* NOTE drow/2003-01-30: There used to be a comment and some special
21078 code here to turn a symbol with DW_AT_external and a
21079 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21080 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21081 with some versions of binutils) where shared libraries could have
21082 relocations against symbols in their debug information - the
21083 minimal symbol would have the right address, but the debug info
21084 would not. It's no longer necessary, because we will explicitly
21085 apply relocations when we read in the debug information now. */
21086
21087 /* A DW_AT_location attribute with no contents indicates that a
21088 variable has been optimized away. */
21089 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21090 {
21091 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21092 return;
21093 }
21094
21095 /* Handle one degenerate form of location expression specially, to
21096 preserve GDB's previous behavior when section offsets are
21097 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21098 then mark this symbol as LOC_STATIC. */
21099
21100 if (attr_form_is_block (attr)
21101 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21102 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21103 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21104 && (DW_BLOCK (attr)->size
21105 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21106 {
21107 unsigned int dummy;
21108
21109 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21110 SYMBOL_VALUE_ADDRESS (sym) =
21111 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21112 else
21113 SYMBOL_VALUE_ADDRESS (sym) =
21114 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21115 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21116 fixup_symbol_section (sym, objfile);
21117 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21118 SYMBOL_SECTION (sym));
21119 return;
21120 }
21121
21122 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21123 expression evaluator, and use LOC_COMPUTED only when necessary
21124 (i.e. when the value of a register or memory location is
21125 referenced, or a thread-local block, etc.). Then again, it might
21126 not be worthwhile. I'm assuming that it isn't unless performance
21127 or memory numbers show me otherwise. */
21128
21129 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21130
21131 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21132 cu->has_loclist = 1;
21133 }
21134
21135 /* Given a pointer to a DWARF information entry, figure out if we need
21136 to make a symbol table entry for it, and if so, create a new entry
21137 and return a pointer to it.
21138 If TYPE is NULL, determine symbol type from the die, otherwise
21139 used the passed type.
21140 If SPACE is not NULL, use it to hold the new symbol. If it is
21141 NULL, allocate a new symbol on the objfile's obstack. */
21142
21143 static struct symbol *
21144 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21145 struct symbol *space)
21146 {
21147 struct objfile *objfile = cu->objfile;
21148 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21149 struct symbol *sym = NULL;
21150 const char *name;
21151 struct attribute *attr = NULL;
21152 struct attribute *attr2 = NULL;
21153 CORE_ADDR baseaddr;
21154 struct pending **list_to_add = NULL;
21155
21156 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21157
21158 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21159
21160 name = dwarf2_name (die, cu);
21161 if (name)
21162 {
21163 const char *linkagename;
21164 int suppress_add = 0;
21165
21166 if (space)
21167 sym = space;
21168 else
21169 sym = allocate_symbol (objfile);
21170 OBJSTAT (objfile, n_syms++);
21171
21172 /* Cache this symbol's name and the name's demangled form (if any). */
21173 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21174 linkagename = dwarf2_physname (name, die, cu);
21175 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21176
21177 /* Fortran does not have mangling standard and the mangling does differ
21178 between gfortran, iFort etc. */
21179 if (cu->language == language_fortran
21180 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21181 symbol_set_demangled_name (&(sym->ginfo),
21182 dwarf2_full_name (name, die, cu),
21183 NULL);
21184
21185 /* Default assumptions.
21186 Use the passed type or decode it from the die. */
21187 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21188 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21189 if (type != NULL)
21190 SYMBOL_TYPE (sym) = type;
21191 else
21192 SYMBOL_TYPE (sym) = die_type (die, cu);
21193 attr = dwarf2_attr (die,
21194 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21195 cu);
21196 if (attr)
21197 {
21198 SYMBOL_LINE (sym) = DW_UNSND (attr);
21199 }
21200
21201 attr = dwarf2_attr (die,
21202 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21203 cu);
21204 if (attr)
21205 {
21206 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21207 struct file_entry *fe;
21208
21209 if (cu->line_header != NULL)
21210 fe = cu->line_header->file_name_at (file_index);
21211 else
21212 fe = NULL;
21213
21214 if (fe == NULL)
21215 complaint (&symfile_complaints,
21216 _("file index out of range"));
21217 else
21218 symbol_set_symtab (sym, fe->symtab);
21219 }
21220
21221 switch (die->tag)
21222 {
21223 case DW_TAG_label:
21224 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21225 if (attr)
21226 {
21227 CORE_ADDR addr;
21228
21229 addr = attr_value_as_address (attr);
21230 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21231 SYMBOL_VALUE_ADDRESS (sym) = addr;
21232 }
21233 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21234 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21235 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21236 add_symbol_to_list (sym, cu->list_in_scope);
21237 break;
21238 case DW_TAG_subprogram:
21239 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21240 finish_block. */
21241 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21242 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21243 if ((attr2 && (DW_UNSND (attr2) != 0))
21244 || cu->language == language_ada)
21245 {
21246 /* Subprograms marked external are stored as a global symbol.
21247 Ada subprograms, whether marked external or not, are always
21248 stored as a global symbol, because we want to be able to
21249 access them globally. For instance, we want to be able
21250 to break on a nested subprogram without having to
21251 specify the context. */
21252 list_to_add = &global_symbols;
21253 }
21254 else
21255 {
21256 list_to_add = cu->list_in_scope;
21257 }
21258 break;
21259 case DW_TAG_inlined_subroutine:
21260 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21261 finish_block. */
21262 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21263 SYMBOL_INLINED (sym) = 1;
21264 list_to_add = cu->list_in_scope;
21265 break;
21266 case DW_TAG_template_value_param:
21267 suppress_add = 1;
21268 /* Fall through. */
21269 case DW_TAG_constant:
21270 case DW_TAG_variable:
21271 case DW_TAG_member:
21272 /* Compilation with minimal debug info may result in
21273 variables with missing type entries. Change the
21274 misleading `void' type to something sensible. */
21275 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21276 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21277
21278 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21279 /* In the case of DW_TAG_member, we should only be called for
21280 static const members. */
21281 if (die->tag == DW_TAG_member)
21282 {
21283 /* dwarf2_add_field uses die_is_declaration,
21284 so we do the same. */
21285 gdb_assert (die_is_declaration (die, cu));
21286 gdb_assert (attr);
21287 }
21288 if (attr)
21289 {
21290 dwarf2_const_value (attr, sym, cu);
21291 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21292 if (!suppress_add)
21293 {
21294 if (attr2 && (DW_UNSND (attr2) != 0))
21295 list_to_add = &global_symbols;
21296 else
21297 list_to_add = cu->list_in_scope;
21298 }
21299 break;
21300 }
21301 attr = dwarf2_attr (die, DW_AT_location, cu);
21302 if (attr)
21303 {
21304 var_decode_location (attr, sym, cu);
21305 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21306
21307 /* Fortran explicitly imports any global symbols to the local
21308 scope by DW_TAG_common_block. */
21309 if (cu->language == language_fortran && die->parent
21310 && die->parent->tag == DW_TAG_common_block)
21311 attr2 = NULL;
21312
21313 if (SYMBOL_CLASS (sym) == LOC_STATIC
21314 && SYMBOL_VALUE_ADDRESS (sym) == 0
21315 && !dwarf2_per_objfile->has_section_at_zero)
21316 {
21317 /* When a static variable is eliminated by the linker,
21318 the corresponding debug information is not stripped
21319 out, but the variable address is set to null;
21320 do not add such variables into symbol table. */
21321 }
21322 else if (attr2 && (DW_UNSND (attr2) != 0))
21323 {
21324 /* Workaround gfortran PR debug/40040 - it uses
21325 DW_AT_location for variables in -fPIC libraries which may
21326 get overriden by other libraries/executable and get
21327 a different address. Resolve it by the minimal symbol
21328 which may come from inferior's executable using copy
21329 relocation. Make this workaround only for gfortran as for
21330 other compilers GDB cannot guess the minimal symbol
21331 Fortran mangling kind. */
21332 if (cu->language == language_fortran && die->parent
21333 && die->parent->tag == DW_TAG_module
21334 && cu->producer
21335 && startswith (cu->producer, "GNU Fortran"))
21336 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21337
21338 /* A variable with DW_AT_external is never static,
21339 but it may be block-scoped. */
21340 list_to_add = (cu->list_in_scope == &file_symbols
21341 ? &global_symbols : cu->list_in_scope);
21342 }
21343 else
21344 list_to_add = cu->list_in_scope;
21345 }
21346 else
21347 {
21348 /* We do not know the address of this symbol.
21349 If it is an external symbol and we have type information
21350 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21351 The address of the variable will then be determined from
21352 the minimal symbol table whenever the variable is
21353 referenced. */
21354 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21355
21356 /* Fortran explicitly imports any global symbols to the local
21357 scope by DW_TAG_common_block. */
21358 if (cu->language == language_fortran && die->parent
21359 && die->parent->tag == DW_TAG_common_block)
21360 {
21361 /* SYMBOL_CLASS doesn't matter here because
21362 read_common_block is going to reset it. */
21363 if (!suppress_add)
21364 list_to_add = cu->list_in_scope;
21365 }
21366 else if (attr2 && (DW_UNSND (attr2) != 0)
21367 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21368 {
21369 /* A variable with DW_AT_external is never static, but it
21370 may be block-scoped. */
21371 list_to_add = (cu->list_in_scope == &file_symbols
21372 ? &global_symbols : cu->list_in_scope);
21373
21374 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21375 }
21376 else if (!die_is_declaration (die, cu))
21377 {
21378 /* Use the default LOC_OPTIMIZED_OUT class. */
21379 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21380 if (!suppress_add)
21381 list_to_add = cu->list_in_scope;
21382 }
21383 }
21384 break;
21385 case DW_TAG_formal_parameter:
21386 /* If we are inside a function, mark this as an argument. If
21387 not, we might be looking at an argument to an inlined function
21388 when we do not have enough information to show inlined frames;
21389 pretend it's a local variable in that case so that the user can
21390 still see it. */
21391 if (context_stack_depth > 0
21392 && context_stack[context_stack_depth - 1].name != NULL)
21393 SYMBOL_IS_ARGUMENT (sym) = 1;
21394 attr = dwarf2_attr (die, DW_AT_location, cu);
21395 if (attr)
21396 {
21397 var_decode_location (attr, sym, cu);
21398 }
21399 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21400 if (attr)
21401 {
21402 dwarf2_const_value (attr, sym, cu);
21403 }
21404
21405 list_to_add = cu->list_in_scope;
21406 break;
21407 case DW_TAG_unspecified_parameters:
21408 /* From varargs functions; gdb doesn't seem to have any
21409 interest in this information, so just ignore it for now.
21410 (FIXME?) */
21411 break;
21412 case DW_TAG_template_type_param:
21413 suppress_add = 1;
21414 /* Fall through. */
21415 case DW_TAG_class_type:
21416 case DW_TAG_interface_type:
21417 case DW_TAG_structure_type:
21418 case DW_TAG_union_type:
21419 case DW_TAG_set_type:
21420 case DW_TAG_enumeration_type:
21421 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21422 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21423
21424 {
21425 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21426 really ever be static objects: otherwise, if you try
21427 to, say, break of a class's method and you're in a file
21428 which doesn't mention that class, it won't work unless
21429 the check for all static symbols in lookup_symbol_aux
21430 saves you. See the OtherFileClass tests in
21431 gdb.c++/namespace.exp. */
21432
21433 if (!suppress_add)
21434 {
21435 list_to_add = (cu->list_in_scope == &file_symbols
21436 && cu->language == language_cplus
21437 ? &global_symbols : cu->list_in_scope);
21438
21439 /* The semantics of C++ state that "struct foo {
21440 ... }" also defines a typedef for "foo". */
21441 if (cu->language == language_cplus
21442 || cu->language == language_ada
21443 || cu->language == language_d
21444 || cu->language == language_rust)
21445 {
21446 /* The symbol's name is already allocated along
21447 with this objfile, so we don't need to
21448 duplicate it for the type. */
21449 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21450 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21451 }
21452 }
21453 }
21454 break;
21455 case DW_TAG_typedef:
21456 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21457 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21458 list_to_add = cu->list_in_scope;
21459 break;
21460 case DW_TAG_base_type:
21461 case DW_TAG_subrange_type:
21462 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21463 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21464 list_to_add = cu->list_in_scope;
21465 break;
21466 case DW_TAG_enumerator:
21467 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21468 if (attr)
21469 {
21470 dwarf2_const_value (attr, sym, cu);
21471 }
21472 {
21473 /* NOTE: carlton/2003-11-10: See comment above in the
21474 DW_TAG_class_type, etc. block. */
21475
21476 list_to_add = (cu->list_in_scope == &file_symbols
21477 && cu->language == language_cplus
21478 ? &global_symbols : cu->list_in_scope);
21479 }
21480 break;
21481 case DW_TAG_imported_declaration:
21482 case DW_TAG_namespace:
21483 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21484 list_to_add = &global_symbols;
21485 break;
21486 case DW_TAG_module:
21487 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21488 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21489 list_to_add = &global_symbols;
21490 break;
21491 case DW_TAG_common_block:
21492 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21493 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21494 add_symbol_to_list (sym, cu->list_in_scope);
21495 break;
21496 default:
21497 /* Not a tag we recognize. Hopefully we aren't processing
21498 trash data, but since we must specifically ignore things
21499 we don't recognize, there is nothing else we should do at
21500 this point. */
21501 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
21502 dwarf_tag_name (die->tag));
21503 break;
21504 }
21505
21506 if (suppress_add)
21507 {
21508 sym->hash_next = objfile->template_symbols;
21509 objfile->template_symbols = sym;
21510 list_to_add = NULL;
21511 }
21512
21513 if (list_to_add != NULL)
21514 add_symbol_to_list (sym, list_to_add);
21515
21516 /* For the benefit of old versions of GCC, check for anonymous
21517 namespaces based on the demangled name. */
21518 if (!cu->processing_has_namespace_info
21519 && cu->language == language_cplus)
21520 cp_scan_for_anonymous_namespaces (sym, objfile);
21521 }
21522 return (sym);
21523 }
21524
21525 /* A wrapper for new_symbol_full that always allocates a new symbol. */
21526
21527 static struct symbol *
21528 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
21529 {
21530 return new_symbol_full (die, type, cu, NULL);
21531 }
21532
21533 /* Given an attr with a DW_FORM_dataN value in host byte order,
21534 zero-extend it as appropriate for the symbol's type. The DWARF
21535 standard (v4) is not entirely clear about the meaning of using
21536 DW_FORM_dataN for a constant with a signed type, where the type is
21537 wider than the data. The conclusion of a discussion on the DWARF
21538 list was that this is unspecified. We choose to always zero-extend
21539 because that is the interpretation long in use by GCC. */
21540
21541 static gdb_byte *
21542 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21543 struct dwarf2_cu *cu, LONGEST *value, int bits)
21544 {
21545 struct objfile *objfile = cu->objfile;
21546 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21547 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21548 LONGEST l = DW_UNSND (attr);
21549
21550 if (bits < sizeof (*value) * 8)
21551 {
21552 l &= ((LONGEST) 1 << bits) - 1;
21553 *value = l;
21554 }
21555 else if (bits == sizeof (*value) * 8)
21556 *value = l;
21557 else
21558 {
21559 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21560 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21561 return bytes;
21562 }
21563
21564 return NULL;
21565 }
21566
21567 /* Read a constant value from an attribute. Either set *VALUE, or if
21568 the value does not fit in *VALUE, set *BYTES - either already
21569 allocated on the objfile obstack, or newly allocated on OBSTACK,
21570 or, set *BATON, if we translated the constant to a location
21571 expression. */
21572
21573 static void
21574 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21575 const char *name, struct obstack *obstack,
21576 struct dwarf2_cu *cu,
21577 LONGEST *value, const gdb_byte **bytes,
21578 struct dwarf2_locexpr_baton **baton)
21579 {
21580 struct objfile *objfile = cu->objfile;
21581 struct comp_unit_head *cu_header = &cu->header;
21582 struct dwarf_block *blk;
21583 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21584 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21585
21586 *value = 0;
21587 *bytes = NULL;
21588 *baton = NULL;
21589
21590 switch (attr->form)
21591 {
21592 case DW_FORM_addr:
21593 case DW_FORM_GNU_addr_index:
21594 {
21595 gdb_byte *data;
21596
21597 if (TYPE_LENGTH (type) != cu_header->addr_size)
21598 dwarf2_const_value_length_mismatch_complaint (name,
21599 cu_header->addr_size,
21600 TYPE_LENGTH (type));
21601 /* Symbols of this form are reasonably rare, so we just
21602 piggyback on the existing location code rather than writing
21603 a new implementation of symbol_computed_ops. */
21604 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21605 (*baton)->per_cu = cu->per_cu;
21606 gdb_assert ((*baton)->per_cu);
21607
21608 (*baton)->size = 2 + cu_header->addr_size;
21609 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21610 (*baton)->data = data;
21611
21612 data[0] = DW_OP_addr;
21613 store_unsigned_integer (&data[1], cu_header->addr_size,
21614 byte_order, DW_ADDR (attr));
21615 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21616 }
21617 break;
21618 case DW_FORM_string:
21619 case DW_FORM_strp:
21620 case DW_FORM_GNU_str_index:
21621 case DW_FORM_GNU_strp_alt:
21622 /* DW_STRING is already allocated on the objfile obstack, point
21623 directly to it. */
21624 *bytes = (const gdb_byte *) DW_STRING (attr);
21625 break;
21626 case DW_FORM_block1:
21627 case DW_FORM_block2:
21628 case DW_FORM_block4:
21629 case DW_FORM_block:
21630 case DW_FORM_exprloc:
21631 case DW_FORM_data16:
21632 blk = DW_BLOCK (attr);
21633 if (TYPE_LENGTH (type) != blk->size)
21634 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21635 TYPE_LENGTH (type));
21636 *bytes = blk->data;
21637 break;
21638
21639 /* The DW_AT_const_value attributes are supposed to carry the
21640 symbol's value "represented as it would be on the target
21641 architecture." By the time we get here, it's already been
21642 converted to host endianness, so we just need to sign- or
21643 zero-extend it as appropriate. */
21644 case DW_FORM_data1:
21645 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21646 break;
21647 case DW_FORM_data2:
21648 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21649 break;
21650 case DW_FORM_data4:
21651 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21652 break;
21653 case DW_FORM_data8:
21654 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21655 break;
21656
21657 case DW_FORM_sdata:
21658 case DW_FORM_implicit_const:
21659 *value = DW_SND (attr);
21660 break;
21661
21662 case DW_FORM_udata:
21663 *value = DW_UNSND (attr);
21664 break;
21665
21666 default:
21667 complaint (&symfile_complaints,
21668 _("unsupported const value attribute form: '%s'"),
21669 dwarf_form_name (attr->form));
21670 *value = 0;
21671 break;
21672 }
21673 }
21674
21675
21676 /* Copy constant value from an attribute to a symbol. */
21677
21678 static void
21679 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21680 struct dwarf2_cu *cu)
21681 {
21682 struct objfile *objfile = cu->objfile;
21683 LONGEST value;
21684 const gdb_byte *bytes;
21685 struct dwarf2_locexpr_baton *baton;
21686
21687 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21688 SYMBOL_PRINT_NAME (sym),
21689 &objfile->objfile_obstack, cu,
21690 &value, &bytes, &baton);
21691
21692 if (baton != NULL)
21693 {
21694 SYMBOL_LOCATION_BATON (sym) = baton;
21695 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21696 }
21697 else if (bytes != NULL)
21698 {
21699 SYMBOL_VALUE_BYTES (sym) = bytes;
21700 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21701 }
21702 else
21703 {
21704 SYMBOL_VALUE (sym) = value;
21705 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21706 }
21707 }
21708
21709 /* Return the type of the die in question using its DW_AT_type attribute. */
21710
21711 static struct type *
21712 die_type (struct die_info *die, struct dwarf2_cu *cu)
21713 {
21714 struct attribute *type_attr;
21715
21716 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21717 if (!type_attr)
21718 {
21719 /* A missing DW_AT_type represents a void type. */
21720 return objfile_type (cu->objfile)->builtin_void;
21721 }
21722
21723 return lookup_die_type (die, type_attr, cu);
21724 }
21725
21726 /* True iff CU's producer generates GNAT Ada auxiliary information
21727 that allows to find parallel types through that information instead
21728 of having to do expensive parallel lookups by type name. */
21729
21730 static int
21731 need_gnat_info (struct dwarf2_cu *cu)
21732 {
21733 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
21734 of GNAT produces this auxiliary information, without any indication
21735 that it is produced. Part of enhancing the FSF version of GNAT
21736 to produce that information will be to put in place an indicator
21737 that we can use in order to determine whether the descriptive type
21738 info is available or not. One suggestion that has been made is
21739 to use a new attribute, attached to the CU die. For now, assume
21740 that the descriptive type info is not available. */
21741 return 0;
21742 }
21743
21744 /* Return the auxiliary type of the die in question using its
21745 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21746 attribute is not present. */
21747
21748 static struct type *
21749 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21750 {
21751 struct attribute *type_attr;
21752
21753 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21754 if (!type_attr)
21755 return NULL;
21756
21757 return lookup_die_type (die, type_attr, cu);
21758 }
21759
21760 /* If DIE has a descriptive_type attribute, then set the TYPE's
21761 descriptive type accordingly. */
21762
21763 static void
21764 set_descriptive_type (struct type *type, struct die_info *die,
21765 struct dwarf2_cu *cu)
21766 {
21767 struct type *descriptive_type = die_descriptive_type (die, cu);
21768
21769 if (descriptive_type)
21770 {
21771 ALLOCATE_GNAT_AUX_TYPE (type);
21772 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21773 }
21774 }
21775
21776 /* Return the containing type of the die in question using its
21777 DW_AT_containing_type attribute. */
21778
21779 static struct type *
21780 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21781 {
21782 struct attribute *type_attr;
21783
21784 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21785 if (!type_attr)
21786 error (_("Dwarf Error: Problem turning containing type into gdb type "
21787 "[in module %s]"), objfile_name (cu->objfile));
21788
21789 return lookup_die_type (die, type_attr, cu);
21790 }
21791
21792 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21793
21794 static struct type *
21795 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21796 {
21797 struct objfile *objfile = dwarf2_per_objfile->objfile;
21798 char *message, *saved;
21799
21800 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
21801 objfile_name (objfile),
21802 to_underlying (cu->header.sect_off),
21803 to_underlying (die->sect_off));
21804 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21805 message, strlen (message));
21806 xfree (message);
21807
21808 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21809 }
21810
21811 /* Look up the type of DIE in CU using its type attribute ATTR.
21812 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21813 DW_AT_containing_type.
21814 If there is no type substitute an error marker. */
21815
21816 static struct type *
21817 lookup_die_type (struct die_info *die, const struct attribute *attr,
21818 struct dwarf2_cu *cu)
21819 {
21820 struct objfile *objfile = cu->objfile;
21821 struct type *this_type;
21822
21823 gdb_assert (attr->name == DW_AT_type
21824 || attr->name == DW_AT_GNAT_descriptive_type
21825 || attr->name == DW_AT_containing_type);
21826
21827 /* First see if we have it cached. */
21828
21829 if (attr->form == DW_FORM_GNU_ref_alt)
21830 {
21831 struct dwarf2_per_cu_data *per_cu;
21832 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21833
21834 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
21835 this_type = get_die_type_at_offset (sect_off, per_cu);
21836 }
21837 else if (attr_form_is_ref (attr))
21838 {
21839 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21840
21841 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21842 }
21843 else if (attr->form == DW_FORM_ref_sig8)
21844 {
21845 ULONGEST signature = DW_SIGNATURE (attr);
21846
21847 return get_signatured_type (die, signature, cu);
21848 }
21849 else
21850 {
21851 complaint (&symfile_complaints,
21852 _("Dwarf Error: Bad type attribute %s in DIE"
21853 " at 0x%x [in module %s]"),
21854 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
21855 objfile_name (objfile));
21856 return build_error_marker_type (cu, die);
21857 }
21858
21859 /* If not cached we need to read it in. */
21860
21861 if (this_type == NULL)
21862 {
21863 struct die_info *type_die = NULL;
21864 struct dwarf2_cu *type_cu = cu;
21865
21866 if (attr_form_is_ref (attr))
21867 type_die = follow_die_ref (die, attr, &type_cu);
21868 if (type_die == NULL)
21869 return build_error_marker_type (cu, die);
21870 /* If we find the type now, it's probably because the type came
21871 from an inter-CU reference and the type's CU got expanded before
21872 ours. */
21873 this_type = read_type_die (type_die, type_cu);
21874 }
21875
21876 /* If we still don't have a type use an error marker. */
21877
21878 if (this_type == NULL)
21879 return build_error_marker_type (cu, die);
21880
21881 return this_type;
21882 }
21883
21884 /* Return the type in DIE, CU.
21885 Returns NULL for invalid types.
21886
21887 This first does a lookup in die_type_hash,
21888 and only reads the die in if necessary.
21889
21890 NOTE: This can be called when reading in partial or full symbols. */
21891
21892 static struct type *
21893 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21894 {
21895 struct type *this_type;
21896
21897 this_type = get_die_type (die, cu);
21898 if (this_type)
21899 return this_type;
21900
21901 return read_type_die_1 (die, cu);
21902 }
21903
21904 /* Read the type in DIE, CU.
21905 Returns NULL for invalid types. */
21906
21907 static struct type *
21908 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21909 {
21910 struct type *this_type = NULL;
21911
21912 switch (die->tag)
21913 {
21914 case DW_TAG_class_type:
21915 case DW_TAG_interface_type:
21916 case DW_TAG_structure_type:
21917 case DW_TAG_union_type:
21918 this_type = read_structure_type (die, cu);
21919 break;
21920 case DW_TAG_enumeration_type:
21921 this_type = read_enumeration_type (die, cu);
21922 break;
21923 case DW_TAG_subprogram:
21924 case DW_TAG_subroutine_type:
21925 case DW_TAG_inlined_subroutine:
21926 this_type = read_subroutine_type (die, cu);
21927 break;
21928 case DW_TAG_array_type:
21929 this_type = read_array_type (die, cu);
21930 break;
21931 case DW_TAG_set_type:
21932 this_type = read_set_type (die, cu);
21933 break;
21934 case DW_TAG_pointer_type:
21935 this_type = read_tag_pointer_type (die, cu);
21936 break;
21937 case DW_TAG_ptr_to_member_type:
21938 this_type = read_tag_ptr_to_member_type (die, cu);
21939 break;
21940 case DW_TAG_reference_type:
21941 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21942 break;
21943 case DW_TAG_rvalue_reference_type:
21944 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21945 break;
21946 case DW_TAG_const_type:
21947 this_type = read_tag_const_type (die, cu);
21948 break;
21949 case DW_TAG_volatile_type:
21950 this_type = read_tag_volatile_type (die, cu);
21951 break;
21952 case DW_TAG_restrict_type:
21953 this_type = read_tag_restrict_type (die, cu);
21954 break;
21955 case DW_TAG_string_type:
21956 this_type = read_tag_string_type (die, cu);
21957 break;
21958 case DW_TAG_typedef:
21959 this_type = read_typedef (die, cu);
21960 break;
21961 case DW_TAG_subrange_type:
21962 this_type = read_subrange_type (die, cu);
21963 break;
21964 case DW_TAG_base_type:
21965 this_type = read_base_type (die, cu);
21966 break;
21967 case DW_TAG_unspecified_type:
21968 this_type = read_unspecified_type (die, cu);
21969 break;
21970 case DW_TAG_namespace:
21971 this_type = read_namespace_type (die, cu);
21972 break;
21973 case DW_TAG_module:
21974 this_type = read_module_type (die, cu);
21975 break;
21976 case DW_TAG_atomic_type:
21977 this_type = read_tag_atomic_type (die, cu);
21978 break;
21979 default:
21980 complaint (&symfile_complaints,
21981 _("unexpected tag in read_type_die: '%s'"),
21982 dwarf_tag_name (die->tag));
21983 break;
21984 }
21985
21986 return this_type;
21987 }
21988
21989 /* See if we can figure out if the class lives in a namespace. We do
21990 this by looking for a member function; its demangled name will
21991 contain namespace info, if there is any.
21992 Return the computed name or NULL.
21993 Space for the result is allocated on the objfile's obstack.
21994 This is the full-die version of guess_partial_die_structure_name.
21995 In this case we know DIE has no useful parent. */
21996
21997 static char *
21998 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21999 {
22000 struct die_info *spec_die;
22001 struct dwarf2_cu *spec_cu;
22002 struct die_info *child;
22003
22004 spec_cu = cu;
22005 spec_die = die_specification (die, &spec_cu);
22006 if (spec_die != NULL)
22007 {
22008 die = spec_die;
22009 cu = spec_cu;
22010 }
22011
22012 for (child = die->child;
22013 child != NULL;
22014 child = child->sibling)
22015 {
22016 if (child->tag == DW_TAG_subprogram)
22017 {
22018 const char *linkage_name = dw2_linkage_name (child, cu);
22019
22020 if (linkage_name != NULL)
22021 {
22022 char *actual_name
22023 = language_class_name_from_physname (cu->language_defn,
22024 linkage_name);
22025 char *name = NULL;
22026
22027 if (actual_name != NULL)
22028 {
22029 const char *die_name = dwarf2_name (die, cu);
22030
22031 if (die_name != NULL
22032 && strcmp (die_name, actual_name) != 0)
22033 {
22034 /* Strip off the class name from the full name.
22035 We want the prefix. */
22036 int die_name_len = strlen (die_name);
22037 int actual_name_len = strlen (actual_name);
22038
22039 /* Test for '::' as a sanity check. */
22040 if (actual_name_len > die_name_len + 2
22041 && actual_name[actual_name_len
22042 - die_name_len - 1] == ':')
22043 name = (char *) obstack_copy0 (
22044 &cu->objfile->per_bfd->storage_obstack,
22045 actual_name, actual_name_len - die_name_len - 2);
22046 }
22047 }
22048 xfree (actual_name);
22049 return name;
22050 }
22051 }
22052 }
22053
22054 return NULL;
22055 }
22056
22057 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22058 prefix part in such case. See
22059 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22060
22061 static const char *
22062 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22063 {
22064 struct attribute *attr;
22065 const char *base;
22066
22067 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22068 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22069 return NULL;
22070
22071 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22072 return NULL;
22073
22074 attr = dw2_linkage_name_attr (die, cu);
22075 if (attr == NULL || DW_STRING (attr) == NULL)
22076 return NULL;
22077
22078 /* dwarf2_name had to be already called. */
22079 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22080
22081 /* Strip the base name, keep any leading namespaces/classes. */
22082 base = strrchr (DW_STRING (attr), ':');
22083 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22084 return "";
22085
22086 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
22087 DW_STRING (attr),
22088 &base[-1] - DW_STRING (attr));
22089 }
22090
22091 /* Return the name of the namespace/class that DIE is defined within,
22092 or "" if we can't tell. The caller should not xfree the result.
22093
22094 For example, if we're within the method foo() in the following
22095 code:
22096
22097 namespace N {
22098 class C {
22099 void foo () {
22100 }
22101 };
22102 }
22103
22104 then determine_prefix on foo's die will return "N::C". */
22105
22106 static const char *
22107 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22108 {
22109 struct die_info *parent, *spec_die;
22110 struct dwarf2_cu *spec_cu;
22111 struct type *parent_type;
22112 const char *retval;
22113
22114 if (cu->language != language_cplus
22115 && cu->language != language_fortran && cu->language != language_d
22116 && cu->language != language_rust)
22117 return "";
22118
22119 retval = anonymous_struct_prefix (die, cu);
22120 if (retval)
22121 return retval;
22122
22123 /* We have to be careful in the presence of DW_AT_specification.
22124 For example, with GCC 3.4, given the code
22125
22126 namespace N {
22127 void foo() {
22128 // Definition of N::foo.
22129 }
22130 }
22131
22132 then we'll have a tree of DIEs like this:
22133
22134 1: DW_TAG_compile_unit
22135 2: DW_TAG_namespace // N
22136 3: DW_TAG_subprogram // declaration of N::foo
22137 4: DW_TAG_subprogram // definition of N::foo
22138 DW_AT_specification // refers to die #3
22139
22140 Thus, when processing die #4, we have to pretend that we're in
22141 the context of its DW_AT_specification, namely the contex of die
22142 #3. */
22143 spec_cu = cu;
22144 spec_die = die_specification (die, &spec_cu);
22145 if (spec_die == NULL)
22146 parent = die->parent;
22147 else
22148 {
22149 parent = spec_die->parent;
22150 cu = spec_cu;
22151 }
22152
22153 if (parent == NULL)
22154 return "";
22155 else if (parent->building_fullname)
22156 {
22157 const char *name;
22158 const char *parent_name;
22159
22160 /* It has been seen on RealView 2.2 built binaries,
22161 DW_TAG_template_type_param types actually _defined_ as
22162 children of the parent class:
22163
22164 enum E {};
22165 template class <class Enum> Class{};
22166 Class<enum E> class_e;
22167
22168 1: DW_TAG_class_type (Class)
22169 2: DW_TAG_enumeration_type (E)
22170 3: DW_TAG_enumerator (enum1:0)
22171 3: DW_TAG_enumerator (enum2:1)
22172 ...
22173 2: DW_TAG_template_type_param
22174 DW_AT_type DW_FORM_ref_udata (E)
22175
22176 Besides being broken debug info, it can put GDB into an
22177 infinite loop. Consider:
22178
22179 When we're building the full name for Class<E>, we'll start
22180 at Class, and go look over its template type parameters,
22181 finding E. We'll then try to build the full name of E, and
22182 reach here. We're now trying to build the full name of E,
22183 and look over the parent DIE for containing scope. In the
22184 broken case, if we followed the parent DIE of E, we'd again
22185 find Class, and once again go look at its template type
22186 arguments, etc., etc. Simply don't consider such parent die
22187 as source-level parent of this die (it can't be, the language
22188 doesn't allow it), and break the loop here. */
22189 name = dwarf2_name (die, cu);
22190 parent_name = dwarf2_name (parent, cu);
22191 complaint (&symfile_complaints,
22192 _("template param type '%s' defined within parent '%s'"),
22193 name ? name : "<unknown>",
22194 parent_name ? parent_name : "<unknown>");
22195 return "";
22196 }
22197 else
22198 switch (parent->tag)
22199 {
22200 case DW_TAG_namespace:
22201 parent_type = read_type_die (parent, cu);
22202 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22203 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22204 Work around this problem here. */
22205 if (cu->language == language_cplus
22206 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22207 return "";
22208 /* We give a name to even anonymous namespaces. */
22209 return TYPE_TAG_NAME (parent_type);
22210 case DW_TAG_class_type:
22211 case DW_TAG_interface_type:
22212 case DW_TAG_structure_type:
22213 case DW_TAG_union_type:
22214 case DW_TAG_module:
22215 parent_type = read_type_die (parent, cu);
22216 if (TYPE_TAG_NAME (parent_type) != NULL)
22217 return TYPE_TAG_NAME (parent_type);
22218 else
22219 /* An anonymous structure is only allowed non-static data
22220 members; no typedefs, no member functions, et cetera.
22221 So it does not need a prefix. */
22222 return "";
22223 case DW_TAG_compile_unit:
22224 case DW_TAG_partial_unit:
22225 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22226 if (cu->language == language_cplus
22227 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22228 && die->child != NULL
22229 && (die->tag == DW_TAG_class_type
22230 || die->tag == DW_TAG_structure_type
22231 || die->tag == DW_TAG_union_type))
22232 {
22233 char *name = guess_full_die_structure_name (die, cu);
22234 if (name != NULL)
22235 return name;
22236 }
22237 return "";
22238 case DW_TAG_enumeration_type:
22239 parent_type = read_type_die (parent, cu);
22240 if (TYPE_DECLARED_CLASS (parent_type))
22241 {
22242 if (TYPE_TAG_NAME (parent_type) != NULL)
22243 return TYPE_TAG_NAME (parent_type);
22244 return "";
22245 }
22246 /* Fall through. */
22247 default:
22248 return determine_prefix (parent, cu);
22249 }
22250 }
22251
22252 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22253 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22254 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22255 an obconcat, otherwise allocate storage for the result. The CU argument is
22256 used to determine the language and hence, the appropriate separator. */
22257
22258 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22259
22260 static char *
22261 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22262 int physname, struct dwarf2_cu *cu)
22263 {
22264 const char *lead = "";
22265 const char *sep;
22266
22267 if (suffix == NULL || suffix[0] == '\0'
22268 || prefix == NULL || prefix[0] == '\0')
22269 sep = "";
22270 else if (cu->language == language_d)
22271 {
22272 /* For D, the 'main' function could be defined in any module, but it
22273 should never be prefixed. */
22274 if (strcmp (suffix, "D main") == 0)
22275 {
22276 prefix = "";
22277 sep = "";
22278 }
22279 else
22280 sep = ".";
22281 }
22282 else if (cu->language == language_fortran && physname)
22283 {
22284 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22285 DW_AT_MIPS_linkage_name is preferred and used instead. */
22286
22287 lead = "__";
22288 sep = "_MOD_";
22289 }
22290 else
22291 sep = "::";
22292
22293 if (prefix == NULL)
22294 prefix = "";
22295 if (suffix == NULL)
22296 suffix = "";
22297
22298 if (obs == NULL)
22299 {
22300 char *retval
22301 = ((char *)
22302 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22303
22304 strcpy (retval, lead);
22305 strcat (retval, prefix);
22306 strcat (retval, sep);
22307 strcat (retval, suffix);
22308 return retval;
22309 }
22310 else
22311 {
22312 /* We have an obstack. */
22313 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22314 }
22315 }
22316
22317 /* Return sibling of die, NULL if no sibling. */
22318
22319 static struct die_info *
22320 sibling_die (struct die_info *die)
22321 {
22322 return die->sibling;
22323 }
22324
22325 /* Get name of a die, return NULL if not found. */
22326
22327 static const char *
22328 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22329 struct obstack *obstack)
22330 {
22331 if (name && cu->language == language_cplus)
22332 {
22333 std::string canon_name = cp_canonicalize_string (name);
22334
22335 if (!canon_name.empty ())
22336 {
22337 if (canon_name != name)
22338 name = (const char *) obstack_copy0 (obstack,
22339 canon_name.c_str (),
22340 canon_name.length ());
22341 }
22342 }
22343
22344 return name;
22345 }
22346
22347 /* Get name of a die, return NULL if not found.
22348 Anonymous namespaces are converted to their magic string. */
22349
22350 static const char *
22351 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22352 {
22353 struct attribute *attr;
22354
22355 attr = dwarf2_attr (die, DW_AT_name, cu);
22356 if ((!attr || !DW_STRING (attr))
22357 && die->tag != DW_TAG_namespace
22358 && die->tag != DW_TAG_class_type
22359 && die->tag != DW_TAG_interface_type
22360 && die->tag != DW_TAG_structure_type
22361 && die->tag != DW_TAG_union_type)
22362 return NULL;
22363
22364 switch (die->tag)
22365 {
22366 case DW_TAG_compile_unit:
22367 case DW_TAG_partial_unit:
22368 /* Compilation units have a DW_AT_name that is a filename, not
22369 a source language identifier. */
22370 case DW_TAG_enumeration_type:
22371 case DW_TAG_enumerator:
22372 /* These tags always have simple identifiers already; no need
22373 to canonicalize them. */
22374 return DW_STRING (attr);
22375
22376 case DW_TAG_namespace:
22377 if (attr != NULL && DW_STRING (attr) != NULL)
22378 return DW_STRING (attr);
22379 return CP_ANONYMOUS_NAMESPACE_STR;
22380
22381 case DW_TAG_class_type:
22382 case DW_TAG_interface_type:
22383 case DW_TAG_structure_type:
22384 case DW_TAG_union_type:
22385 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22386 structures or unions. These were of the form "._%d" in GCC 4.1,
22387 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22388 and GCC 4.4. We work around this problem by ignoring these. */
22389 if (attr && DW_STRING (attr)
22390 && (startswith (DW_STRING (attr), "._")
22391 || startswith (DW_STRING (attr), "<anonymous")))
22392 return NULL;
22393
22394 /* GCC might emit a nameless typedef that has a linkage name. See
22395 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22396 if (!attr || DW_STRING (attr) == NULL)
22397 {
22398 char *demangled = NULL;
22399
22400 attr = dw2_linkage_name_attr (die, cu);
22401 if (attr == NULL || DW_STRING (attr) == NULL)
22402 return NULL;
22403
22404 /* Avoid demangling DW_STRING (attr) the second time on a second
22405 call for the same DIE. */
22406 if (!DW_STRING_IS_CANONICAL (attr))
22407 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22408
22409 if (demangled)
22410 {
22411 const char *base;
22412
22413 /* FIXME: we already did this for the partial symbol... */
22414 DW_STRING (attr)
22415 = ((const char *)
22416 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
22417 demangled, strlen (demangled)));
22418 DW_STRING_IS_CANONICAL (attr) = 1;
22419 xfree (demangled);
22420
22421 /* Strip any leading namespaces/classes, keep only the base name.
22422 DW_AT_name for named DIEs does not contain the prefixes. */
22423 base = strrchr (DW_STRING (attr), ':');
22424 if (base && base > DW_STRING (attr) && base[-1] == ':')
22425 return &base[1];
22426 else
22427 return DW_STRING (attr);
22428 }
22429 }
22430 break;
22431
22432 default:
22433 break;
22434 }
22435
22436 if (!DW_STRING_IS_CANONICAL (attr))
22437 {
22438 DW_STRING (attr)
22439 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22440 &cu->objfile->per_bfd->storage_obstack);
22441 DW_STRING_IS_CANONICAL (attr) = 1;
22442 }
22443 return DW_STRING (attr);
22444 }
22445
22446 /* Return the die that this die in an extension of, or NULL if there
22447 is none. *EXT_CU is the CU containing DIE on input, and the CU
22448 containing the return value on output. */
22449
22450 static struct die_info *
22451 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22452 {
22453 struct attribute *attr;
22454
22455 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22456 if (attr == NULL)
22457 return NULL;
22458
22459 return follow_die_ref (die, attr, ext_cu);
22460 }
22461
22462 /* Convert a DIE tag into its string name. */
22463
22464 static const char *
22465 dwarf_tag_name (unsigned tag)
22466 {
22467 const char *name = get_DW_TAG_name (tag);
22468
22469 if (name == NULL)
22470 return "DW_TAG_<unknown>";
22471
22472 return name;
22473 }
22474
22475 /* Convert a DWARF attribute code into its string name. */
22476
22477 static const char *
22478 dwarf_attr_name (unsigned attr)
22479 {
22480 const char *name;
22481
22482 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22483 if (attr == DW_AT_MIPS_fde)
22484 return "DW_AT_MIPS_fde";
22485 #else
22486 if (attr == DW_AT_HP_block_index)
22487 return "DW_AT_HP_block_index";
22488 #endif
22489
22490 name = get_DW_AT_name (attr);
22491
22492 if (name == NULL)
22493 return "DW_AT_<unknown>";
22494
22495 return name;
22496 }
22497
22498 /* Convert a DWARF value form code into its string name. */
22499
22500 static const char *
22501 dwarf_form_name (unsigned form)
22502 {
22503 const char *name = get_DW_FORM_name (form);
22504
22505 if (name == NULL)
22506 return "DW_FORM_<unknown>";
22507
22508 return name;
22509 }
22510
22511 static const char *
22512 dwarf_bool_name (unsigned mybool)
22513 {
22514 if (mybool)
22515 return "TRUE";
22516 else
22517 return "FALSE";
22518 }
22519
22520 /* Convert a DWARF type code into its string name. */
22521
22522 static const char *
22523 dwarf_type_encoding_name (unsigned enc)
22524 {
22525 const char *name = get_DW_ATE_name (enc);
22526
22527 if (name == NULL)
22528 return "DW_ATE_<unknown>";
22529
22530 return name;
22531 }
22532
22533 static void
22534 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22535 {
22536 unsigned int i;
22537
22538 print_spaces (indent, f);
22539 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
22540 dwarf_tag_name (die->tag), die->abbrev,
22541 to_underlying (die->sect_off));
22542
22543 if (die->parent != NULL)
22544 {
22545 print_spaces (indent, f);
22546 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
22547 to_underlying (die->parent->sect_off));
22548 }
22549
22550 print_spaces (indent, f);
22551 fprintf_unfiltered (f, " has children: %s\n",
22552 dwarf_bool_name (die->child != NULL));
22553
22554 print_spaces (indent, f);
22555 fprintf_unfiltered (f, " attributes:\n");
22556
22557 for (i = 0; i < die->num_attrs; ++i)
22558 {
22559 print_spaces (indent, f);
22560 fprintf_unfiltered (f, " %s (%s) ",
22561 dwarf_attr_name (die->attrs[i].name),
22562 dwarf_form_name (die->attrs[i].form));
22563
22564 switch (die->attrs[i].form)
22565 {
22566 case DW_FORM_addr:
22567 case DW_FORM_GNU_addr_index:
22568 fprintf_unfiltered (f, "address: ");
22569 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22570 break;
22571 case DW_FORM_block2:
22572 case DW_FORM_block4:
22573 case DW_FORM_block:
22574 case DW_FORM_block1:
22575 fprintf_unfiltered (f, "block: size %s",
22576 pulongest (DW_BLOCK (&die->attrs[i])->size));
22577 break;
22578 case DW_FORM_exprloc:
22579 fprintf_unfiltered (f, "expression: size %s",
22580 pulongest (DW_BLOCK (&die->attrs[i])->size));
22581 break;
22582 case DW_FORM_data16:
22583 fprintf_unfiltered (f, "constant of 16 bytes");
22584 break;
22585 case DW_FORM_ref_addr:
22586 fprintf_unfiltered (f, "ref address: ");
22587 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22588 break;
22589 case DW_FORM_GNU_ref_alt:
22590 fprintf_unfiltered (f, "alt ref address: ");
22591 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22592 break;
22593 case DW_FORM_ref1:
22594 case DW_FORM_ref2:
22595 case DW_FORM_ref4:
22596 case DW_FORM_ref8:
22597 case DW_FORM_ref_udata:
22598 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22599 (long) (DW_UNSND (&die->attrs[i])));
22600 break;
22601 case DW_FORM_data1:
22602 case DW_FORM_data2:
22603 case DW_FORM_data4:
22604 case DW_FORM_data8:
22605 case DW_FORM_udata:
22606 case DW_FORM_sdata:
22607 fprintf_unfiltered (f, "constant: %s",
22608 pulongest (DW_UNSND (&die->attrs[i])));
22609 break;
22610 case DW_FORM_sec_offset:
22611 fprintf_unfiltered (f, "section offset: %s",
22612 pulongest (DW_UNSND (&die->attrs[i])));
22613 break;
22614 case DW_FORM_ref_sig8:
22615 fprintf_unfiltered (f, "signature: %s",
22616 hex_string (DW_SIGNATURE (&die->attrs[i])));
22617 break;
22618 case DW_FORM_string:
22619 case DW_FORM_strp:
22620 case DW_FORM_line_strp:
22621 case DW_FORM_GNU_str_index:
22622 case DW_FORM_GNU_strp_alt:
22623 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22624 DW_STRING (&die->attrs[i])
22625 ? DW_STRING (&die->attrs[i]) : "",
22626 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22627 break;
22628 case DW_FORM_flag:
22629 if (DW_UNSND (&die->attrs[i]))
22630 fprintf_unfiltered (f, "flag: TRUE");
22631 else
22632 fprintf_unfiltered (f, "flag: FALSE");
22633 break;
22634 case DW_FORM_flag_present:
22635 fprintf_unfiltered (f, "flag: TRUE");
22636 break;
22637 case DW_FORM_indirect:
22638 /* The reader will have reduced the indirect form to
22639 the "base form" so this form should not occur. */
22640 fprintf_unfiltered (f,
22641 "unexpected attribute form: DW_FORM_indirect");
22642 break;
22643 case DW_FORM_implicit_const:
22644 fprintf_unfiltered (f, "constant: %s",
22645 plongest (DW_SND (&die->attrs[i])));
22646 break;
22647 default:
22648 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22649 die->attrs[i].form);
22650 break;
22651 }
22652 fprintf_unfiltered (f, "\n");
22653 }
22654 }
22655
22656 static void
22657 dump_die_for_error (struct die_info *die)
22658 {
22659 dump_die_shallow (gdb_stderr, 0, die);
22660 }
22661
22662 static void
22663 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22664 {
22665 int indent = level * 4;
22666
22667 gdb_assert (die != NULL);
22668
22669 if (level >= max_level)
22670 return;
22671
22672 dump_die_shallow (f, indent, die);
22673
22674 if (die->child != NULL)
22675 {
22676 print_spaces (indent, f);
22677 fprintf_unfiltered (f, " Children:");
22678 if (level + 1 < max_level)
22679 {
22680 fprintf_unfiltered (f, "\n");
22681 dump_die_1 (f, level + 1, max_level, die->child);
22682 }
22683 else
22684 {
22685 fprintf_unfiltered (f,
22686 " [not printed, max nesting level reached]\n");
22687 }
22688 }
22689
22690 if (die->sibling != NULL && level > 0)
22691 {
22692 dump_die_1 (f, level, max_level, die->sibling);
22693 }
22694 }
22695
22696 /* This is called from the pdie macro in gdbinit.in.
22697 It's not static so gcc will keep a copy callable from gdb. */
22698
22699 void
22700 dump_die (struct die_info *die, int max_level)
22701 {
22702 dump_die_1 (gdb_stdlog, 0, max_level, die);
22703 }
22704
22705 static void
22706 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22707 {
22708 void **slot;
22709
22710 slot = htab_find_slot_with_hash (cu->die_hash, die,
22711 to_underlying (die->sect_off),
22712 INSERT);
22713
22714 *slot = die;
22715 }
22716
22717 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22718 required kind. */
22719
22720 static sect_offset
22721 dwarf2_get_ref_die_offset (const struct attribute *attr)
22722 {
22723 if (attr_form_is_ref (attr))
22724 return (sect_offset) DW_UNSND (attr);
22725
22726 complaint (&symfile_complaints,
22727 _("unsupported die ref attribute form: '%s'"),
22728 dwarf_form_name (attr->form));
22729 return {};
22730 }
22731
22732 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22733 * the value held by the attribute is not constant. */
22734
22735 static LONGEST
22736 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22737 {
22738 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22739 return DW_SND (attr);
22740 else if (attr->form == DW_FORM_udata
22741 || attr->form == DW_FORM_data1
22742 || attr->form == DW_FORM_data2
22743 || attr->form == DW_FORM_data4
22744 || attr->form == DW_FORM_data8)
22745 return DW_UNSND (attr);
22746 else
22747 {
22748 /* For DW_FORM_data16 see attr_form_is_constant. */
22749 complaint (&symfile_complaints,
22750 _("Attribute value is not a constant (%s)"),
22751 dwarf_form_name (attr->form));
22752 return default_value;
22753 }
22754 }
22755
22756 /* Follow reference or signature attribute ATTR of SRC_DIE.
22757 On entry *REF_CU is the CU of SRC_DIE.
22758 On exit *REF_CU is the CU of the result. */
22759
22760 static struct die_info *
22761 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22762 struct dwarf2_cu **ref_cu)
22763 {
22764 struct die_info *die;
22765
22766 if (attr_form_is_ref (attr))
22767 die = follow_die_ref (src_die, attr, ref_cu);
22768 else if (attr->form == DW_FORM_ref_sig8)
22769 die = follow_die_sig (src_die, attr, ref_cu);
22770 else
22771 {
22772 dump_die_for_error (src_die);
22773 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22774 objfile_name ((*ref_cu)->objfile));
22775 }
22776
22777 return die;
22778 }
22779
22780 /* Follow reference OFFSET.
22781 On entry *REF_CU is the CU of the source die referencing OFFSET.
22782 On exit *REF_CU is the CU of the result.
22783 Returns NULL if OFFSET is invalid. */
22784
22785 static struct die_info *
22786 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22787 struct dwarf2_cu **ref_cu)
22788 {
22789 struct die_info temp_die;
22790 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22791
22792 gdb_assert (cu->per_cu != NULL);
22793
22794 target_cu = cu;
22795
22796 if (cu->per_cu->is_debug_types)
22797 {
22798 /* .debug_types CUs cannot reference anything outside their CU.
22799 If they need to, they have to reference a signatured type via
22800 DW_FORM_ref_sig8. */
22801 if (!offset_in_cu_p (&cu->header, sect_off))
22802 return NULL;
22803 }
22804 else if (offset_in_dwz != cu->per_cu->is_dwz
22805 || !offset_in_cu_p (&cu->header, sect_off))
22806 {
22807 struct dwarf2_per_cu_data *per_cu;
22808
22809 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22810 cu->objfile);
22811
22812 /* If necessary, add it to the queue and load its DIEs. */
22813 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22814 load_full_comp_unit (per_cu, cu->language);
22815
22816 target_cu = per_cu->cu;
22817 }
22818 else if (cu->dies == NULL)
22819 {
22820 /* We're loading full DIEs during partial symbol reading. */
22821 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22822 load_full_comp_unit (cu->per_cu, language_minimal);
22823 }
22824
22825 *ref_cu = target_cu;
22826 temp_die.sect_off = sect_off;
22827 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22828 &temp_die,
22829 to_underlying (sect_off));
22830 }
22831
22832 /* Follow reference attribute ATTR of SRC_DIE.
22833 On entry *REF_CU is the CU of SRC_DIE.
22834 On exit *REF_CU is the CU of the result. */
22835
22836 static struct die_info *
22837 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22838 struct dwarf2_cu **ref_cu)
22839 {
22840 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22841 struct dwarf2_cu *cu = *ref_cu;
22842 struct die_info *die;
22843
22844 die = follow_die_offset (sect_off,
22845 (attr->form == DW_FORM_GNU_ref_alt
22846 || cu->per_cu->is_dwz),
22847 ref_cu);
22848 if (!die)
22849 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
22850 "at 0x%x [in module %s]"),
22851 to_underlying (sect_off), to_underlying (src_die->sect_off),
22852 objfile_name (cu->objfile));
22853
22854 return die;
22855 }
22856
22857 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22858 Returned value is intended for DW_OP_call*. Returned
22859 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
22860
22861 struct dwarf2_locexpr_baton
22862 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22863 struct dwarf2_per_cu_data *per_cu,
22864 CORE_ADDR (*get_frame_pc) (void *baton),
22865 void *baton)
22866 {
22867 struct dwarf2_cu *cu;
22868 struct die_info *die;
22869 struct attribute *attr;
22870 struct dwarf2_locexpr_baton retval;
22871
22872 dw2_setup (per_cu->objfile);
22873
22874 if (per_cu->cu == NULL)
22875 load_cu (per_cu);
22876 cu = per_cu->cu;
22877 if (cu == NULL)
22878 {
22879 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22880 Instead just throw an error, not much else we can do. */
22881 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
22882 to_underlying (sect_off), objfile_name (per_cu->objfile));
22883 }
22884
22885 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22886 if (!die)
22887 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
22888 to_underlying (sect_off), objfile_name (per_cu->objfile));
22889
22890 attr = dwarf2_attr (die, DW_AT_location, cu);
22891 if (!attr)
22892 {
22893 /* DWARF: "If there is no such attribute, then there is no effect.".
22894 DATA is ignored if SIZE is 0. */
22895
22896 retval.data = NULL;
22897 retval.size = 0;
22898 }
22899 else if (attr_form_is_section_offset (attr))
22900 {
22901 struct dwarf2_loclist_baton loclist_baton;
22902 CORE_ADDR pc = (*get_frame_pc) (baton);
22903 size_t size;
22904
22905 fill_in_loclist_baton (cu, &loclist_baton, attr);
22906
22907 retval.data = dwarf2_find_location_expression (&loclist_baton,
22908 &size, pc);
22909 retval.size = size;
22910 }
22911 else
22912 {
22913 if (!attr_form_is_block (attr))
22914 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
22915 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22916 to_underlying (sect_off), objfile_name (per_cu->objfile));
22917
22918 retval.data = DW_BLOCK (attr)->data;
22919 retval.size = DW_BLOCK (attr)->size;
22920 }
22921 retval.per_cu = cu->per_cu;
22922
22923 age_cached_comp_units ();
22924
22925 return retval;
22926 }
22927
22928 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22929 offset. */
22930
22931 struct dwarf2_locexpr_baton
22932 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22933 struct dwarf2_per_cu_data *per_cu,
22934 CORE_ADDR (*get_frame_pc) (void *baton),
22935 void *baton)
22936 {
22937 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22938
22939 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22940 }
22941
22942 /* Write a constant of a given type as target-ordered bytes into
22943 OBSTACK. */
22944
22945 static const gdb_byte *
22946 write_constant_as_bytes (struct obstack *obstack,
22947 enum bfd_endian byte_order,
22948 struct type *type,
22949 ULONGEST value,
22950 LONGEST *len)
22951 {
22952 gdb_byte *result;
22953
22954 *len = TYPE_LENGTH (type);
22955 result = (gdb_byte *) obstack_alloc (obstack, *len);
22956 store_unsigned_integer (result, *len, byte_order, value);
22957
22958 return result;
22959 }
22960
22961 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
22962 pointer to the constant bytes and set LEN to the length of the
22963 data. If memory is needed, allocate it on OBSTACK. If the DIE
22964 does not have a DW_AT_const_value, return NULL. */
22965
22966 const gdb_byte *
22967 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22968 struct dwarf2_per_cu_data *per_cu,
22969 struct obstack *obstack,
22970 LONGEST *len)
22971 {
22972 struct dwarf2_cu *cu;
22973 struct die_info *die;
22974 struct attribute *attr;
22975 const gdb_byte *result = NULL;
22976 struct type *type;
22977 LONGEST value;
22978 enum bfd_endian byte_order;
22979
22980 dw2_setup (per_cu->objfile);
22981
22982 if (per_cu->cu == NULL)
22983 load_cu (per_cu);
22984 cu = per_cu->cu;
22985 if (cu == NULL)
22986 {
22987 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22988 Instead just throw an error, not much else we can do. */
22989 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
22990 to_underlying (sect_off), objfile_name (per_cu->objfile));
22991 }
22992
22993 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22994 if (!die)
22995 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
22996 to_underlying (sect_off), objfile_name (per_cu->objfile));
22997
22998
22999 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23000 if (attr == NULL)
23001 return NULL;
23002
23003 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
23004 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23005
23006 switch (attr->form)
23007 {
23008 case DW_FORM_addr:
23009 case DW_FORM_GNU_addr_index:
23010 {
23011 gdb_byte *tem;
23012
23013 *len = cu->header.addr_size;
23014 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23015 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23016 result = tem;
23017 }
23018 break;
23019 case DW_FORM_string:
23020 case DW_FORM_strp:
23021 case DW_FORM_GNU_str_index:
23022 case DW_FORM_GNU_strp_alt:
23023 /* DW_STRING is already allocated on the objfile obstack, point
23024 directly to it. */
23025 result = (const gdb_byte *) DW_STRING (attr);
23026 *len = strlen (DW_STRING (attr));
23027 break;
23028 case DW_FORM_block1:
23029 case DW_FORM_block2:
23030 case DW_FORM_block4:
23031 case DW_FORM_block:
23032 case DW_FORM_exprloc:
23033 case DW_FORM_data16:
23034 result = DW_BLOCK (attr)->data;
23035 *len = DW_BLOCK (attr)->size;
23036 break;
23037
23038 /* The DW_AT_const_value attributes are supposed to carry the
23039 symbol's value "represented as it would be on the target
23040 architecture." By the time we get here, it's already been
23041 converted to host endianness, so we just need to sign- or
23042 zero-extend it as appropriate. */
23043 case DW_FORM_data1:
23044 type = die_type (die, cu);
23045 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23046 if (result == NULL)
23047 result = write_constant_as_bytes (obstack, byte_order,
23048 type, value, len);
23049 break;
23050 case DW_FORM_data2:
23051 type = die_type (die, cu);
23052 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23053 if (result == NULL)
23054 result = write_constant_as_bytes (obstack, byte_order,
23055 type, value, len);
23056 break;
23057 case DW_FORM_data4:
23058 type = die_type (die, cu);
23059 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23060 if (result == NULL)
23061 result = write_constant_as_bytes (obstack, byte_order,
23062 type, value, len);
23063 break;
23064 case DW_FORM_data8:
23065 type = die_type (die, cu);
23066 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23067 if (result == NULL)
23068 result = write_constant_as_bytes (obstack, byte_order,
23069 type, value, len);
23070 break;
23071
23072 case DW_FORM_sdata:
23073 case DW_FORM_implicit_const:
23074 type = die_type (die, cu);
23075 result = write_constant_as_bytes (obstack, byte_order,
23076 type, DW_SND (attr), len);
23077 break;
23078
23079 case DW_FORM_udata:
23080 type = die_type (die, cu);
23081 result = write_constant_as_bytes (obstack, byte_order,
23082 type, DW_UNSND (attr), len);
23083 break;
23084
23085 default:
23086 complaint (&symfile_complaints,
23087 _("unsupported const value attribute form: '%s'"),
23088 dwarf_form_name (attr->form));
23089 break;
23090 }
23091
23092 return result;
23093 }
23094
23095 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23096 valid type for this die is found. */
23097
23098 struct type *
23099 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23100 struct dwarf2_per_cu_data *per_cu)
23101 {
23102 struct dwarf2_cu *cu;
23103 struct die_info *die;
23104
23105 dw2_setup (per_cu->objfile);
23106
23107 if (per_cu->cu == NULL)
23108 load_cu (per_cu);
23109 cu = per_cu->cu;
23110 if (!cu)
23111 return NULL;
23112
23113 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23114 if (!die)
23115 return NULL;
23116
23117 return die_type (die, cu);
23118 }
23119
23120 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23121 PER_CU. */
23122
23123 struct type *
23124 dwarf2_get_die_type (cu_offset die_offset,
23125 struct dwarf2_per_cu_data *per_cu)
23126 {
23127 dw2_setup (per_cu->objfile);
23128
23129 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23130 return get_die_type_at_offset (die_offset_sect, per_cu);
23131 }
23132
23133 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23134 On entry *REF_CU is the CU of SRC_DIE.
23135 On exit *REF_CU is the CU of the result.
23136 Returns NULL if the referenced DIE isn't found. */
23137
23138 static struct die_info *
23139 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23140 struct dwarf2_cu **ref_cu)
23141 {
23142 struct die_info temp_die;
23143 struct dwarf2_cu *sig_cu;
23144 struct die_info *die;
23145
23146 /* While it might be nice to assert sig_type->type == NULL here,
23147 we can get here for DW_AT_imported_declaration where we need
23148 the DIE not the type. */
23149
23150 /* If necessary, add it to the queue and load its DIEs. */
23151
23152 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23153 read_signatured_type (sig_type);
23154
23155 sig_cu = sig_type->per_cu.cu;
23156 gdb_assert (sig_cu != NULL);
23157 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23158 temp_die.sect_off = sig_type->type_offset_in_section;
23159 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23160 to_underlying (temp_die.sect_off));
23161 if (die)
23162 {
23163 /* For .gdb_index version 7 keep track of included TUs.
23164 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23165 if (dwarf2_per_objfile->index_table != NULL
23166 && dwarf2_per_objfile->index_table->version <= 7)
23167 {
23168 VEC_safe_push (dwarf2_per_cu_ptr,
23169 (*ref_cu)->per_cu->imported_symtabs,
23170 sig_cu->per_cu);
23171 }
23172
23173 *ref_cu = sig_cu;
23174 return die;
23175 }
23176
23177 return NULL;
23178 }
23179
23180 /* Follow signatured type referenced by ATTR in SRC_DIE.
23181 On entry *REF_CU is the CU of SRC_DIE.
23182 On exit *REF_CU is the CU of the result.
23183 The result is the DIE of the type.
23184 If the referenced type cannot be found an error is thrown. */
23185
23186 static struct die_info *
23187 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23188 struct dwarf2_cu **ref_cu)
23189 {
23190 ULONGEST signature = DW_SIGNATURE (attr);
23191 struct signatured_type *sig_type;
23192 struct die_info *die;
23193
23194 gdb_assert (attr->form == DW_FORM_ref_sig8);
23195
23196 sig_type = lookup_signatured_type (*ref_cu, signature);
23197 /* sig_type will be NULL if the signatured type is missing from
23198 the debug info. */
23199 if (sig_type == NULL)
23200 {
23201 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23202 " from DIE at 0x%x [in module %s]"),
23203 hex_string (signature), to_underlying (src_die->sect_off),
23204 objfile_name ((*ref_cu)->objfile));
23205 }
23206
23207 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23208 if (die == NULL)
23209 {
23210 dump_die_for_error (src_die);
23211 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23212 " from DIE at 0x%x [in module %s]"),
23213 hex_string (signature), to_underlying (src_die->sect_off),
23214 objfile_name ((*ref_cu)->objfile));
23215 }
23216
23217 return die;
23218 }
23219
23220 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23221 reading in and processing the type unit if necessary. */
23222
23223 static struct type *
23224 get_signatured_type (struct die_info *die, ULONGEST signature,
23225 struct dwarf2_cu *cu)
23226 {
23227 struct signatured_type *sig_type;
23228 struct dwarf2_cu *type_cu;
23229 struct die_info *type_die;
23230 struct type *type;
23231
23232 sig_type = lookup_signatured_type (cu, signature);
23233 /* sig_type will be NULL if the signatured type is missing from
23234 the debug info. */
23235 if (sig_type == NULL)
23236 {
23237 complaint (&symfile_complaints,
23238 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23239 " from DIE at 0x%x [in module %s]"),
23240 hex_string (signature), to_underlying (die->sect_off),
23241 objfile_name (dwarf2_per_objfile->objfile));
23242 return build_error_marker_type (cu, die);
23243 }
23244
23245 /* If we already know the type we're done. */
23246 if (sig_type->type != NULL)
23247 return sig_type->type;
23248
23249 type_cu = cu;
23250 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23251 if (type_die != NULL)
23252 {
23253 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23254 is created. This is important, for example, because for c++ classes
23255 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23256 type = read_type_die (type_die, type_cu);
23257 if (type == NULL)
23258 {
23259 complaint (&symfile_complaints,
23260 _("Dwarf Error: Cannot build signatured type %s"
23261 " referenced from DIE at 0x%x [in module %s]"),
23262 hex_string (signature), to_underlying (die->sect_off),
23263 objfile_name (dwarf2_per_objfile->objfile));
23264 type = build_error_marker_type (cu, die);
23265 }
23266 }
23267 else
23268 {
23269 complaint (&symfile_complaints,
23270 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23271 " from DIE at 0x%x [in module %s]"),
23272 hex_string (signature), to_underlying (die->sect_off),
23273 objfile_name (dwarf2_per_objfile->objfile));
23274 type = build_error_marker_type (cu, die);
23275 }
23276 sig_type->type = type;
23277
23278 return type;
23279 }
23280
23281 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23282 reading in and processing the type unit if necessary. */
23283
23284 static struct type *
23285 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23286 struct dwarf2_cu *cu) /* ARI: editCase function */
23287 {
23288 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23289 if (attr_form_is_ref (attr))
23290 {
23291 struct dwarf2_cu *type_cu = cu;
23292 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23293
23294 return read_type_die (type_die, type_cu);
23295 }
23296 else if (attr->form == DW_FORM_ref_sig8)
23297 {
23298 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23299 }
23300 else
23301 {
23302 complaint (&symfile_complaints,
23303 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23304 " at 0x%x [in module %s]"),
23305 dwarf_form_name (attr->form), to_underlying (die->sect_off),
23306 objfile_name (dwarf2_per_objfile->objfile));
23307 return build_error_marker_type (cu, die);
23308 }
23309 }
23310
23311 /* Load the DIEs associated with type unit PER_CU into memory. */
23312
23313 static void
23314 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23315 {
23316 struct signatured_type *sig_type;
23317
23318 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23319 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23320
23321 /* We have the per_cu, but we need the signatured_type.
23322 Fortunately this is an easy translation. */
23323 gdb_assert (per_cu->is_debug_types);
23324 sig_type = (struct signatured_type *) per_cu;
23325
23326 gdb_assert (per_cu->cu == NULL);
23327
23328 read_signatured_type (sig_type);
23329
23330 gdb_assert (per_cu->cu != NULL);
23331 }
23332
23333 /* die_reader_func for read_signatured_type.
23334 This is identical to load_full_comp_unit_reader,
23335 but is kept separate for now. */
23336
23337 static void
23338 read_signatured_type_reader (const struct die_reader_specs *reader,
23339 const gdb_byte *info_ptr,
23340 struct die_info *comp_unit_die,
23341 int has_children,
23342 void *data)
23343 {
23344 struct dwarf2_cu *cu = reader->cu;
23345
23346 gdb_assert (cu->die_hash == NULL);
23347 cu->die_hash =
23348 htab_create_alloc_ex (cu->header.length / 12,
23349 die_hash,
23350 die_eq,
23351 NULL,
23352 &cu->comp_unit_obstack,
23353 hashtab_obstack_allocate,
23354 dummy_obstack_deallocate);
23355
23356 if (has_children)
23357 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23358 &info_ptr, comp_unit_die);
23359 cu->dies = comp_unit_die;
23360 /* comp_unit_die is not stored in die_hash, no need. */
23361
23362 /* We try not to read any attributes in this function, because not
23363 all CUs needed for references have been loaded yet, and symbol
23364 table processing isn't initialized. But we have to set the CU language,
23365 or we won't be able to build types correctly.
23366 Similarly, if we do not read the producer, we can not apply
23367 producer-specific interpretation. */
23368 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23369 }
23370
23371 /* Read in a signatured type and build its CU and DIEs.
23372 If the type is a stub for the real type in a DWO file,
23373 read in the real type from the DWO file as well. */
23374
23375 static void
23376 read_signatured_type (struct signatured_type *sig_type)
23377 {
23378 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23379
23380 gdb_assert (per_cu->is_debug_types);
23381 gdb_assert (per_cu->cu == NULL);
23382
23383 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23384 read_signatured_type_reader, NULL);
23385 sig_type->per_cu.tu_read = 1;
23386 }
23387
23388 /* Decode simple location descriptions.
23389 Given a pointer to a dwarf block that defines a location, compute
23390 the location and return the value.
23391
23392 NOTE drow/2003-11-18: This function is called in two situations
23393 now: for the address of static or global variables (partial symbols
23394 only) and for offsets into structures which are expected to be
23395 (more or less) constant. The partial symbol case should go away,
23396 and only the constant case should remain. That will let this
23397 function complain more accurately. A few special modes are allowed
23398 without complaint for global variables (for instance, global
23399 register values and thread-local values).
23400
23401 A location description containing no operations indicates that the
23402 object is optimized out. The return value is 0 for that case.
23403 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23404 callers will only want a very basic result and this can become a
23405 complaint.
23406
23407 Note that stack[0] is unused except as a default error return. */
23408
23409 static CORE_ADDR
23410 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23411 {
23412 struct objfile *objfile = cu->objfile;
23413 size_t i;
23414 size_t size = blk->size;
23415 const gdb_byte *data = blk->data;
23416 CORE_ADDR stack[64];
23417 int stacki;
23418 unsigned int bytes_read, unsnd;
23419 gdb_byte op;
23420
23421 i = 0;
23422 stacki = 0;
23423 stack[stacki] = 0;
23424 stack[++stacki] = 0;
23425
23426 while (i < size)
23427 {
23428 op = data[i++];
23429 switch (op)
23430 {
23431 case DW_OP_lit0:
23432 case DW_OP_lit1:
23433 case DW_OP_lit2:
23434 case DW_OP_lit3:
23435 case DW_OP_lit4:
23436 case DW_OP_lit5:
23437 case DW_OP_lit6:
23438 case DW_OP_lit7:
23439 case DW_OP_lit8:
23440 case DW_OP_lit9:
23441 case DW_OP_lit10:
23442 case DW_OP_lit11:
23443 case DW_OP_lit12:
23444 case DW_OP_lit13:
23445 case DW_OP_lit14:
23446 case DW_OP_lit15:
23447 case DW_OP_lit16:
23448 case DW_OP_lit17:
23449 case DW_OP_lit18:
23450 case DW_OP_lit19:
23451 case DW_OP_lit20:
23452 case DW_OP_lit21:
23453 case DW_OP_lit22:
23454 case DW_OP_lit23:
23455 case DW_OP_lit24:
23456 case DW_OP_lit25:
23457 case DW_OP_lit26:
23458 case DW_OP_lit27:
23459 case DW_OP_lit28:
23460 case DW_OP_lit29:
23461 case DW_OP_lit30:
23462 case DW_OP_lit31:
23463 stack[++stacki] = op - DW_OP_lit0;
23464 break;
23465
23466 case DW_OP_reg0:
23467 case DW_OP_reg1:
23468 case DW_OP_reg2:
23469 case DW_OP_reg3:
23470 case DW_OP_reg4:
23471 case DW_OP_reg5:
23472 case DW_OP_reg6:
23473 case DW_OP_reg7:
23474 case DW_OP_reg8:
23475 case DW_OP_reg9:
23476 case DW_OP_reg10:
23477 case DW_OP_reg11:
23478 case DW_OP_reg12:
23479 case DW_OP_reg13:
23480 case DW_OP_reg14:
23481 case DW_OP_reg15:
23482 case DW_OP_reg16:
23483 case DW_OP_reg17:
23484 case DW_OP_reg18:
23485 case DW_OP_reg19:
23486 case DW_OP_reg20:
23487 case DW_OP_reg21:
23488 case DW_OP_reg22:
23489 case DW_OP_reg23:
23490 case DW_OP_reg24:
23491 case DW_OP_reg25:
23492 case DW_OP_reg26:
23493 case DW_OP_reg27:
23494 case DW_OP_reg28:
23495 case DW_OP_reg29:
23496 case DW_OP_reg30:
23497 case DW_OP_reg31:
23498 stack[++stacki] = op - DW_OP_reg0;
23499 if (i < size)
23500 dwarf2_complex_location_expr_complaint ();
23501 break;
23502
23503 case DW_OP_regx:
23504 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23505 i += bytes_read;
23506 stack[++stacki] = unsnd;
23507 if (i < size)
23508 dwarf2_complex_location_expr_complaint ();
23509 break;
23510
23511 case DW_OP_addr:
23512 stack[++stacki] = read_address (objfile->obfd, &data[i],
23513 cu, &bytes_read);
23514 i += bytes_read;
23515 break;
23516
23517 case DW_OP_const1u:
23518 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23519 i += 1;
23520 break;
23521
23522 case DW_OP_const1s:
23523 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23524 i += 1;
23525 break;
23526
23527 case DW_OP_const2u:
23528 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23529 i += 2;
23530 break;
23531
23532 case DW_OP_const2s:
23533 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23534 i += 2;
23535 break;
23536
23537 case DW_OP_const4u:
23538 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23539 i += 4;
23540 break;
23541
23542 case DW_OP_const4s:
23543 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23544 i += 4;
23545 break;
23546
23547 case DW_OP_const8u:
23548 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23549 i += 8;
23550 break;
23551
23552 case DW_OP_constu:
23553 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23554 &bytes_read);
23555 i += bytes_read;
23556 break;
23557
23558 case DW_OP_consts:
23559 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23560 i += bytes_read;
23561 break;
23562
23563 case DW_OP_dup:
23564 stack[stacki + 1] = stack[stacki];
23565 stacki++;
23566 break;
23567
23568 case DW_OP_plus:
23569 stack[stacki - 1] += stack[stacki];
23570 stacki--;
23571 break;
23572
23573 case DW_OP_plus_uconst:
23574 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23575 &bytes_read);
23576 i += bytes_read;
23577 break;
23578
23579 case DW_OP_minus:
23580 stack[stacki - 1] -= stack[stacki];
23581 stacki--;
23582 break;
23583
23584 case DW_OP_deref:
23585 /* If we're not the last op, then we definitely can't encode
23586 this using GDB's address_class enum. This is valid for partial
23587 global symbols, although the variable's address will be bogus
23588 in the psymtab. */
23589 if (i < size)
23590 dwarf2_complex_location_expr_complaint ();
23591 break;
23592
23593 case DW_OP_GNU_push_tls_address:
23594 case DW_OP_form_tls_address:
23595 /* The top of the stack has the offset from the beginning
23596 of the thread control block at which the variable is located. */
23597 /* Nothing should follow this operator, so the top of stack would
23598 be returned. */
23599 /* This is valid for partial global symbols, but the variable's
23600 address will be bogus in the psymtab. Make it always at least
23601 non-zero to not look as a variable garbage collected by linker
23602 which have DW_OP_addr 0. */
23603 if (i < size)
23604 dwarf2_complex_location_expr_complaint ();
23605 stack[stacki]++;
23606 break;
23607
23608 case DW_OP_GNU_uninit:
23609 break;
23610
23611 case DW_OP_GNU_addr_index:
23612 case DW_OP_GNU_const_index:
23613 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23614 &bytes_read);
23615 i += bytes_read;
23616 break;
23617
23618 default:
23619 {
23620 const char *name = get_DW_OP_name (op);
23621
23622 if (name)
23623 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23624 name);
23625 else
23626 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23627 op);
23628 }
23629
23630 return (stack[stacki]);
23631 }
23632
23633 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23634 outside of the allocated space. Also enforce minimum>0. */
23635 if (stacki >= ARRAY_SIZE (stack) - 1)
23636 {
23637 complaint (&symfile_complaints,
23638 _("location description stack overflow"));
23639 return 0;
23640 }
23641
23642 if (stacki <= 0)
23643 {
23644 complaint (&symfile_complaints,
23645 _("location description stack underflow"));
23646 return 0;
23647 }
23648 }
23649 return (stack[stacki]);
23650 }
23651
23652 /* memory allocation interface */
23653
23654 static struct dwarf_block *
23655 dwarf_alloc_block (struct dwarf2_cu *cu)
23656 {
23657 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23658 }
23659
23660 static struct die_info *
23661 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23662 {
23663 struct die_info *die;
23664 size_t size = sizeof (struct die_info);
23665
23666 if (num_attrs > 1)
23667 size += (num_attrs - 1) * sizeof (struct attribute);
23668
23669 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23670 memset (die, 0, sizeof (struct die_info));
23671 return (die);
23672 }
23673
23674 \f
23675 /* Macro support. */
23676
23677 /* Return file name relative to the compilation directory of file number I in
23678 *LH's file name table. The result is allocated using xmalloc; the caller is
23679 responsible for freeing it. */
23680
23681 static char *
23682 file_file_name (int file, struct line_header *lh)
23683 {
23684 /* Is the file number a valid index into the line header's file name
23685 table? Remember that file numbers start with one, not zero. */
23686 if (1 <= file && file <= lh->file_names.size ())
23687 {
23688 const file_entry &fe = lh->file_names[file - 1];
23689
23690 if (!IS_ABSOLUTE_PATH (fe.name))
23691 {
23692 const char *dir = fe.include_dir (lh);
23693 if (dir != NULL)
23694 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23695 }
23696 return xstrdup (fe.name);
23697 }
23698 else
23699 {
23700 /* The compiler produced a bogus file number. We can at least
23701 record the macro definitions made in the file, even if we
23702 won't be able to find the file by name. */
23703 char fake_name[80];
23704
23705 xsnprintf (fake_name, sizeof (fake_name),
23706 "<bad macro file number %d>", file);
23707
23708 complaint (&symfile_complaints,
23709 _("bad file number in macro information (%d)"),
23710 file);
23711
23712 return xstrdup (fake_name);
23713 }
23714 }
23715
23716 /* Return the full name of file number I in *LH's file name table.
23717 Use COMP_DIR as the name of the current directory of the
23718 compilation. The result is allocated using xmalloc; the caller is
23719 responsible for freeing it. */
23720 static char *
23721 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23722 {
23723 /* Is the file number a valid index into the line header's file name
23724 table? Remember that file numbers start with one, not zero. */
23725 if (1 <= file && file <= lh->file_names.size ())
23726 {
23727 char *relative = file_file_name (file, lh);
23728
23729 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23730 return relative;
23731 return reconcat (relative, comp_dir, SLASH_STRING,
23732 relative, (char *) NULL);
23733 }
23734 else
23735 return file_file_name (file, lh);
23736 }
23737
23738
23739 static struct macro_source_file *
23740 macro_start_file (int file, int line,
23741 struct macro_source_file *current_file,
23742 struct line_header *lh)
23743 {
23744 /* File name relative to the compilation directory of this source file. */
23745 char *file_name = file_file_name (file, lh);
23746
23747 if (! current_file)
23748 {
23749 /* Note: We don't create a macro table for this compilation unit
23750 at all until we actually get a filename. */
23751 struct macro_table *macro_table = get_macro_table ();
23752
23753 /* If we have no current file, then this must be the start_file
23754 directive for the compilation unit's main source file. */
23755 current_file = macro_set_main (macro_table, file_name);
23756 macro_define_special (macro_table);
23757 }
23758 else
23759 current_file = macro_include (current_file, line, file_name);
23760
23761 xfree (file_name);
23762
23763 return current_file;
23764 }
23765
23766 static const char *
23767 consume_improper_spaces (const char *p, const char *body)
23768 {
23769 if (*p == ' ')
23770 {
23771 complaint (&symfile_complaints,
23772 _("macro definition contains spaces "
23773 "in formal argument list:\n`%s'"),
23774 body);
23775
23776 while (*p == ' ')
23777 p++;
23778 }
23779
23780 return p;
23781 }
23782
23783
23784 static void
23785 parse_macro_definition (struct macro_source_file *file, int line,
23786 const char *body)
23787 {
23788 const char *p;
23789
23790 /* The body string takes one of two forms. For object-like macro
23791 definitions, it should be:
23792
23793 <macro name> " " <definition>
23794
23795 For function-like macro definitions, it should be:
23796
23797 <macro name> "() " <definition>
23798 or
23799 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23800
23801 Spaces may appear only where explicitly indicated, and in the
23802 <definition>.
23803
23804 The Dwarf 2 spec says that an object-like macro's name is always
23805 followed by a space, but versions of GCC around March 2002 omit
23806 the space when the macro's definition is the empty string.
23807
23808 The Dwarf 2 spec says that there should be no spaces between the
23809 formal arguments in a function-like macro's formal argument list,
23810 but versions of GCC around March 2002 include spaces after the
23811 commas. */
23812
23813
23814 /* Find the extent of the macro name. The macro name is terminated
23815 by either a space or null character (for an object-like macro) or
23816 an opening paren (for a function-like macro). */
23817 for (p = body; *p; p++)
23818 if (*p == ' ' || *p == '(')
23819 break;
23820
23821 if (*p == ' ' || *p == '\0')
23822 {
23823 /* It's an object-like macro. */
23824 int name_len = p - body;
23825 char *name = savestring (body, name_len);
23826 const char *replacement;
23827
23828 if (*p == ' ')
23829 replacement = body + name_len + 1;
23830 else
23831 {
23832 dwarf2_macro_malformed_definition_complaint (body);
23833 replacement = body + name_len;
23834 }
23835
23836 macro_define_object (file, line, name, replacement);
23837
23838 xfree (name);
23839 }
23840 else if (*p == '(')
23841 {
23842 /* It's a function-like macro. */
23843 char *name = savestring (body, p - body);
23844 int argc = 0;
23845 int argv_size = 1;
23846 char **argv = XNEWVEC (char *, argv_size);
23847
23848 p++;
23849
23850 p = consume_improper_spaces (p, body);
23851
23852 /* Parse the formal argument list. */
23853 while (*p && *p != ')')
23854 {
23855 /* Find the extent of the current argument name. */
23856 const char *arg_start = p;
23857
23858 while (*p && *p != ',' && *p != ')' && *p != ' ')
23859 p++;
23860
23861 if (! *p || p == arg_start)
23862 dwarf2_macro_malformed_definition_complaint (body);
23863 else
23864 {
23865 /* Make sure argv has room for the new argument. */
23866 if (argc >= argv_size)
23867 {
23868 argv_size *= 2;
23869 argv = XRESIZEVEC (char *, argv, argv_size);
23870 }
23871
23872 argv[argc++] = savestring (arg_start, p - arg_start);
23873 }
23874
23875 p = consume_improper_spaces (p, body);
23876
23877 /* Consume the comma, if present. */
23878 if (*p == ',')
23879 {
23880 p++;
23881
23882 p = consume_improper_spaces (p, body);
23883 }
23884 }
23885
23886 if (*p == ')')
23887 {
23888 p++;
23889
23890 if (*p == ' ')
23891 /* Perfectly formed definition, no complaints. */
23892 macro_define_function (file, line, name,
23893 argc, (const char **) argv,
23894 p + 1);
23895 else if (*p == '\0')
23896 {
23897 /* Complain, but do define it. */
23898 dwarf2_macro_malformed_definition_complaint (body);
23899 macro_define_function (file, line, name,
23900 argc, (const char **) argv,
23901 p);
23902 }
23903 else
23904 /* Just complain. */
23905 dwarf2_macro_malformed_definition_complaint (body);
23906 }
23907 else
23908 /* Just complain. */
23909 dwarf2_macro_malformed_definition_complaint (body);
23910
23911 xfree (name);
23912 {
23913 int i;
23914
23915 for (i = 0; i < argc; i++)
23916 xfree (argv[i]);
23917 }
23918 xfree (argv);
23919 }
23920 else
23921 dwarf2_macro_malformed_definition_complaint (body);
23922 }
23923
23924 /* Skip some bytes from BYTES according to the form given in FORM.
23925 Returns the new pointer. */
23926
23927 static const gdb_byte *
23928 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
23929 enum dwarf_form form,
23930 unsigned int offset_size,
23931 struct dwarf2_section_info *section)
23932 {
23933 unsigned int bytes_read;
23934
23935 switch (form)
23936 {
23937 case DW_FORM_data1:
23938 case DW_FORM_flag:
23939 ++bytes;
23940 break;
23941
23942 case DW_FORM_data2:
23943 bytes += 2;
23944 break;
23945
23946 case DW_FORM_data4:
23947 bytes += 4;
23948 break;
23949
23950 case DW_FORM_data8:
23951 bytes += 8;
23952 break;
23953
23954 case DW_FORM_data16:
23955 bytes += 16;
23956 break;
23957
23958 case DW_FORM_string:
23959 read_direct_string (abfd, bytes, &bytes_read);
23960 bytes += bytes_read;
23961 break;
23962
23963 case DW_FORM_sec_offset:
23964 case DW_FORM_strp:
23965 case DW_FORM_GNU_strp_alt:
23966 bytes += offset_size;
23967 break;
23968
23969 case DW_FORM_block:
23970 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23971 bytes += bytes_read;
23972 break;
23973
23974 case DW_FORM_block1:
23975 bytes += 1 + read_1_byte (abfd, bytes);
23976 break;
23977 case DW_FORM_block2:
23978 bytes += 2 + read_2_bytes (abfd, bytes);
23979 break;
23980 case DW_FORM_block4:
23981 bytes += 4 + read_4_bytes (abfd, bytes);
23982 break;
23983
23984 case DW_FORM_sdata:
23985 case DW_FORM_udata:
23986 case DW_FORM_GNU_addr_index:
23987 case DW_FORM_GNU_str_index:
23988 bytes = gdb_skip_leb128 (bytes, buffer_end);
23989 if (bytes == NULL)
23990 {
23991 dwarf2_section_buffer_overflow_complaint (section);
23992 return NULL;
23993 }
23994 break;
23995
23996 case DW_FORM_implicit_const:
23997 break;
23998
23999 default:
24000 {
24001 complaint (&symfile_complaints,
24002 _("invalid form 0x%x in `%s'"),
24003 form, get_section_name (section));
24004 return NULL;
24005 }
24006 }
24007
24008 return bytes;
24009 }
24010
24011 /* A helper for dwarf_decode_macros that handles skipping an unknown
24012 opcode. Returns an updated pointer to the macro data buffer; or,
24013 on error, issues a complaint and returns NULL. */
24014
24015 static const gdb_byte *
24016 skip_unknown_opcode (unsigned int opcode,
24017 const gdb_byte **opcode_definitions,
24018 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24019 bfd *abfd,
24020 unsigned int offset_size,
24021 struct dwarf2_section_info *section)
24022 {
24023 unsigned int bytes_read, i;
24024 unsigned long arg;
24025 const gdb_byte *defn;
24026
24027 if (opcode_definitions[opcode] == NULL)
24028 {
24029 complaint (&symfile_complaints,
24030 _("unrecognized DW_MACFINO opcode 0x%x"),
24031 opcode);
24032 return NULL;
24033 }
24034
24035 defn = opcode_definitions[opcode];
24036 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24037 defn += bytes_read;
24038
24039 for (i = 0; i < arg; ++i)
24040 {
24041 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24042 (enum dwarf_form) defn[i], offset_size,
24043 section);
24044 if (mac_ptr == NULL)
24045 {
24046 /* skip_form_bytes already issued the complaint. */
24047 return NULL;
24048 }
24049 }
24050
24051 return mac_ptr;
24052 }
24053
24054 /* A helper function which parses the header of a macro section.
24055 If the macro section is the extended (for now called "GNU") type,
24056 then this updates *OFFSET_SIZE. Returns a pointer to just after
24057 the header, or issues a complaint and returns NULL on error. */
24058
24059 static const gdb_byte *
24060 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24061 bfd *abfd,
24062 const gdb_byte *mac_ptr,
24063 unsigned int *offset_size,
24064 int section_is_gnu)
24065 {
24066 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24067
24068 if (section_is_gnu)
24069 {
24070 unsigned int version, flags;
24071
24072 version = read_2_bytes (abfd, mac_ptr);
24073 if (version != 4 && version != 5)
24074 {
24075 complaint (&symfile_complaints,
24076 _("unrecognized version `%d' in .debug_macro section"),
24077 version);
24078 return NULL;
24079 }
24080 mac_ptr += 2;
24081
24082 flags = read_1_byte (abfd, mac_ptr);
24083 ++mac_ptr;
24084 *offset_size = (flags & 1) ? 8 : 4;
24085
24086 if ((flags & 2) != 0)
24087 /* We don't need the line table offset. */
24088 mac_ptr += *offset_size;
24089
24090 /* Vendor opcode descriptions. */
24091 if ((flags & 4) != 0)
24092 {
24093 unsigned int i, count;
24094
24095 count = read_1_byte (abfd, mac_ptr);
24096 ++mac_ptr;
24097 for (i = 0; i < count; ++i)
24098 {
24099 unsigned int opcode, bytes_read;
24100 unsigned long arg;
24101
24102 opcode = read_1_byte (abfd, mac_ptr);
24103 ++mac_ptr;
24104 opcode_definitions[opcode] = mac_ptr;
24105 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24106 mac_ptr += bytes_read;
24107 mac_ptr += arg;
24108 }
24109 }
24110 }
24111
24112 return mac_ptr;
24113 }
24114
24115 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24116 including DW_MACRO_import. */
24117
24118 static void
24119 dwarf_decode_macro_bytes (bfd *abfd,
24120 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24121 struct macro_source_file *current_file,
24122 struct line_header *lh,
24123 struct dwarf2_section_info *section,
24124 int section_is_gnu, int section_is_dwz,
24125 unsigned int offset_size,
24126 htab_t include_hash)
24127 {
24128 struct objfile *objfile = dwarf2_per_objfile->objfile;
24129 enum dwarf_macro_record_type macinfo_type;
24130 int at_commandline;
24131 const gdb_byte *opcode_definitions[256];
24132
24133 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24134 &offset_size, section_is_gnu);
24135 if (mac_ptr == NULL)
24136 {
24137 /* We already issued a complaint. */
24138 return;
24139 }
24140
24141 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24142 GDB is still reading the definitions from command line. First
24143 DW_MACINFO_start_file will need to be ignored as it was already executed
24144 to create CURRENT_FILE for the main source holding also the command line
24145 definitions. On first met DW_MACINFO_start_file this flag is reset to
24146 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24147
24148 at_commandline = 1;
24149
24150 do
24151 {
24152 /* Do we at least have room for a macinfo type byte? */
24153 if (mac_ptr >= mac_end)
24154 {
24155 dwarf2_section_buffer_overflow_complaint (section);
24156 break;
24157 }
24158
24159 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24160 mac_ptr++;
24161
24162 /* Note that we rely on the fact that the corresponding GNU and
24163 DWARF constants are the same. */
24164 switch (macinfo_type)
24165 {
24166 /* A zero macinfo type indicates the end of the macro
24167 information. */
24168 case 0:
24169 break;
24170
24171 case DW_MACRO_define:
24172 case DW_MACRO_undef:
24173 case DW_MACRO_define_strp:
24174 case DW_MACRO_undef_strp:
24175 case DW_MACRO_define_sup:
24176 case DW_MACRO_undef_sup:
24177 {
24178 unsigned int bytes_read;
24179 int line;
24180 const char *body;
24181 int is_define;
24182
24183 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24184 mac_ptr += bytes_read;
24185
24186 if (macinfo_type == DW_MACRO_define
24187 || macinfo_type == DW_MACRO_undef)
24188 {
24189 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24190 mac_ptr += bytes_read;
24191 }
24192 else
24193 {
24194 LONGEST str_offset;
24195
24196 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24197 mac_ptr += offset_size;
24198
24199 if (macinfo_type == DW_MACRO_define_sup
24200 || macinfo_type == DW_MACRO_undef_sup
24201 || section_is_dwz)
24202 {
24203 struct dwz_file *dwz = dwarf2_get_dwz_file ();
24204
24205 body = read_indirect_string_from_dwz (dwz, str_offset);
24206 }
24207 else
24208 body = read_indirect_string_at_offset (abfd, str_offset);
24209 }
24210
24211 is_define = (macinfo_type == DW_MACRO_define
24212 || macinfo_type == DW_MACRO_define_strp
24213 || macinfo_type == DW_MACRO_define_sup);
24214 if (! current_file)
24215 {
24216 /* DWARF violation as no main source is present. */
24217 complaint (&symfile_complaints,
24218 _("debug info with no main source gives macro %s "
24219 "on line %d: %s"),
24220 is_define ? _("definition") : _("undefinition"),
24221 line, body);
24222 break;
24223 }
24224 if ((line == 0 && !at_commandline)
24225 || (line != 0 && at_commandline))
24226 complaint (&symfile_complaints,
24227 _("debug info gives %s macro %s with %s line %d: %s"),
24228 at_commandline ? _("command-line") : _("in-file"),
24229 is_define ? _("definition") : _("undefinition"),
24230 line == 0 ? _("zero") : _("non-zero"), line, body);
24231
24232 if (is_define)
24233 parse_macro_definition (current_file, line, body);
24234 else
24235 {
24236 gdb_assert (macinfo_type == DW_MACRO_undef
24237 || macinfo_type == DW_MACRO_undef_strp
24238 || macinfo_type == DW_MACRO_undef_sup);
24239 macro_undef (current_file, line, body);
24240 }
24241 }
24242 break;
24243
24244 case DW_MACRO_start_file:
24245 {
24246 unsigned int bytes_read;
24247 int line, file;
24248
24249 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24250 mac_ptr += bytes_read;
24251 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24252 mac_ptr += bytes_read;
24253
24254 if ((line == 0 && !at_commandline)
24255 || (line != 0 && at_commandline))
24256 complaint (&symfile_complaints,
24257 _("debug info gives source %d included "
24258 "from %s at %s line %d"),
24259 file, at_commandline ? _("command-line") : _("file"),
24260 line == 0 ? _("zero") : _("non-zero"), line);
24261
24262 if (at_commandline)
24263 {
24264 /* This DW_MACRO_start_file was executed in the
24265 pass one. */
24266 at_commandline = 0;
24267 }
24268 else
24269 current_file = macro_start_file (file, line, current_file, lh);
24270 }
24271 break;
24272
24273 case DW_MACRO_end_file:
24274 if (! current_file)
24275 complaint (&symfile_complaints,
24276 _("macro debug info has an unmatched "
24277 "`close_file' directive"));
24278 else
24279 {
24280 current_file = current_file->included_by;
24281 if (! current_file)
24282 {
24283 enum dwarf_macro_record_type next_type;
24284
24285 /* GCC circa March 2002 doesn't produce the zero
24286 type byte marking the end of the compilation
24287 unit. Complain if it's not there, but exit no
24288 matter what. */
24289
24290 /* Do we at least have room for a macinfo type byte? */
24291 if (mac_ptr >= mac_end)
24292 {
24293 dwarf2_section_buffer_overflow_complaint (section);
24294 return;
24295 }
24296
24297 /* We don't increment mac_ptr here, so this is just
24298 a look-ahead. */
24299 next_type
24300 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24301 mac_ptr);
24302 if (next_type != 0)
24303 complaint (&symfile_complaints,
24304 _("no terminating 0-type entry for "
24305 "macros in `.debug_macinfo' section"));
24306
24307 return;
24308 }
24309 }
24310 break;
24311
24312 case DW_MACRO_import:
24313 case DW_MACRO_import_sup:
24314 {
24315 LONGEST offset;
24316 void **slot;
24317 bfd *include_bfd = abfd;
24318 struct dwarf2_section_info *include_section = section;
24319 const gdb_byte *include_mac_end = mac_end;
24320 int is_dwz = section_is_dwz;
24321 const gdb_byte *new_mac_ptr;
24322
24323 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24324 mac_ptr += offset_size;
24325
24326 if (macinfo_type == DW_MACRO_import_sup)
24327 {
24328 struct dwz_file *dwz = dwarf2_get_dwz_file ();
24329
24330 dwarf2_read_section (objfile, &dwz->macro);
24331
24332 include_section = &dwz->macro;
24333 include_bfd = get_section_bfd_owner (include_section);
24334 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24335 is_dwz = 1;
24336 }
24337
24338 new_mac_ptr = include_section->buffer + offset;
24339 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24340
24341 if (*slot != NULL)
24342 {
24343 /* This has actually happened; see
24344 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24345 complaint (&symfile_complaints,
24346 _("recursive DW_MACRO_import in "
24347 ".debug_macro section"));
24348 }
24349 else
24350 {
24351 *slot = (void *) new_mac_ptr;
24352
24353 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
24354 include_mac_end, current_file, lh,
24355 section, section_is_gnu, is_dwz,
24356 offset_size, include_hash);
24357
24358 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24359 }
24360 }
24361 break;
24362
24363 case DW_MACINFO_vendor_ext:
24364 if (!section_is_gnu)
24365 {
24366 unsigned int bytes_read;
24367
24368 /* This reads the constant, but since we don't recognize
24369 any vendor extensions, we ignore it. */
24370 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24371 mac_ptr += bytes_read;
24372 read_direct_string (abfd, mac_ptr, &bytes_read);
24373 mac_ptr += bytes_read;
24374
24375 /* We don't recognize any vendor extensions. */
24376 break;
24377 }
24378 /* FALLTHROUGH */
24379
24380 default:
24381 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24382 mac_ptr, mac_end, abfd, offset_size,
24383 section);
24384 if (mac_ptr == NULL)
24385 return;
24386 break;
24387 }
24388 } while (macinfo_type != 0);
24389 }
24390
24391 static void
24392 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24393 int section_is_gnu)
24394 {
24395 struct objfile *objfile = dwarf2_per_objfile->objfile;
24396 struct line_header *lh = cu->line_header;
24397 bfd *abfd;
24398 const gdb_byte *mac_ptr, *mac_end;
24399 struct macro_source_file *current_file = 0;
24400 enum dwarf_macro_record_type macinfo_type;
24401 unsigned int offset_size = cu->header.offset_size;
24402 const gdb_byte *opcode_definitions[256];
24403 void **slot;
24404 struct dwarf2_section_info *section;
24405 const char *section_name;
24406
24407 if (cu->dwo_unit != NULL)
24408 {
24409 if (section_is_gnu)
24410 {
24411 section = &cu->dwo_unit->dwo_file->sections.macro;
24412 section_name = ".debug_macro.dwo";
24413 }
24414 else
24415 {
24416 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24417 section_name = ".debug_macinfo.dwo";
24418 }
24419 }
24420 else
24421 {
24422 if (section_is_gnu)
24423 {
24424 section = &dwarf2_per_objfile->macro;
24425 section_name = ".debug_macro";
24426 }
24427 else
24428 {
24429 section = &dwarf2_per_objfile->macinfo;
24430 section_name = ".debug_macinfo";
24431 }
24432 }
24433
24434 dwarf2_read_section (objfile, section);
24435 if (section->buffer == NULL)
24436 {
24437 complaint (&symfile_complaints, _("missing %s section"), section_name);
24438 return;
24439 }
24440 abfd = get_section_bfd_owner (section);
24441
24442 /* First pass: Find the name of the base filename.
24443 This filename is needed in order to process all macros whose definition
24444 (or undefinition) comes from the command line. These macros are defined
24445 before the first DW_MACINFO_start_file entry, and yet still need to be
24446 associated to the base file.
24447
24448 To determine the base file name, we scan the macro definitions until we
24449 reach the first DW_MACINFO_start_file entry. We then initialize
24450 CURRENT_FILE accordingly so that any macro definition found before the
24451 first DW_MACINFO_start_file can still be associated to the base file. */
24452
24453 mac_ptr = section->buffer + offset;
24454 mac_end = section->buffer + section->size;
24455
24456 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24457 &offset_size, section_is_gnu);
24458 if (mac_ptr == NULL)
24459 {
24460 /* We already issued a complaint. */
24461 return;
24462 }
24463
24464 do
24465 {
24466 /* Do we at least have room for a macinfo type byte? */
24467 if (mac_ptr >= mac_end)
24468 {
24469 /* Complaint is printed during the second pass as GDB will probably
24470 stop the first pass earlier upon finding
24471 DW_MACINFO_start_file. */
24472 break;
24473 }
24474
24475 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24476 mac_ptr++;
24477
24478 /* Note that we rely on the fact that the corresponding GNU and
24479 DWARF constants are the same. */
24480 switch (macinfo_type)
24481 {
24482 /* A zero macinfo type indicates the end of the macro
24483 information. */
24484 case 0:
24485 break;
24486
24487 case DW_MACRO_define:
24488 case DW_MACRO_undef:
24489 /* Only skip the data by MAC_PTR. */
24490 {
24491 unsigned int bytes_read;
24492
24493 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24494 mac_ptr += bytes_read;
24495 read_direct_string (abfd, mac_ptr, &bytes_read);
24496 mac_ptr += bytes_read;
24497 }
24498 break;
24499
24500 case DW_MACRO_start_file:
24501 {
24502 unsigned int bytes_read;
24503 int line, file;
24504
24505 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24506 mac_ptr += bytes_read;
24507 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24508 mac_ptr += bytes_read;
24509
24510 current_file = macro_start_file (file, line, current_file, lh);
24511 }
24512 break;
24513
24514 case DW_MACRO_end_file:
24515 /* No data to skip by MAC_PTR. */
24516 break;
24517
24518 case DW_MACRO_define_strp:
24519 case DW_MACRO_undef_strp:
24520 case DW_MACRO_define_sup:
24521 case DW_MACRO_undef_sup:
24522 {
24523 unsigned int bytes_read;
24524
24525 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24526 mac_ptr += bytes_read;
24527 mac_ptr += offset_size;
24528 }
24529 break;
24530
24531 case DW_MACRO_import:
24532 case DW_MACRO_import_sup:
24533 /* Note that, according to the spec, a transparent include
24534 chain cannot call DW_MACRO_start_file. So, we can just
24535 skip this opcode. */
24536 mac_ptr += offset_size;
24537 break;
24538
24539 case DW_MACINFO_vendor_ext:
24540 /* Only skip the data by MAC_PTR. */
24541 if (!section_is_gnu)
24542 {
24543 unsigned int bytes_read;
24544
24545 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24546 mac_ptr += bytes_read;
24547 read_direct_string (abfd, mac_ptr, &bytes_read);
24548 mac_ptr += bytes_read;
24549 }
24550 /* FALLTHROUGH */
24551
24552 default:
24553 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24554 mac_ptr, mac_end, abfd, offset_size,
24555 section);
24556 if (mac_ptr == NULL)
24557 return;
24558 break;
24559 }
24560 } while (macinfo_type != 0 && current_file == NULL);
24561
24562 /* Second pass: Process all entries.
24563
24564 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24565 command-line macro definitions/undefinitions. This flag is unset when we
24566 reach the first DW_MACINFO_start_file entry. */
24567
24568 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24569 htab_eq_pointer,
24570 NULL, xcalloc, xfree));
24571 mac_ptr = section->buffer + offset;
24572 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24573 *slot = (void *) mac_ptr;
24574 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
24575 current_file, lh, section,
24576 section_is_gnu, 0, offset_size,
24577 include_hash.get ());
24578 }
24579
24580 /* Check if the attribute's form is a DW_FORM_block*
24581 if so return true else false. */
24582
24583 static int
24584 attr_form_is_block (const struct attribute *attr)
24585 {
24586 return (attr == NULL ? 0 :
24587 attr->form == DW_FORM_block1
24588 || attr->form == DW_FORM_block2
24589 || attr->form == DW_FORM_block4
24590 || attr->form == DW_FORM_block
24591 || attr->form == DW_FORM_exprloc);
24592 }
24593
24594 /* Return non-zero if ATTR's value is a section offset --- classes
24595 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24596 You may use DW_UNSND (attr) to retrieve such offsets.
24597
24598 Section 7.5.4, "Attribute Encodings", explains that no attribute
24599 may have a value that belongs to more than one of these classes; it
24600 would be ambiguous if we did, because we use the same forms for all
24601 of them. */
24602
24603 static int
24604 attr_form_is_section_offset (const struct attribute *attr)
24605 {
24606 return (attr->form == DW_FORM_data4
24607 || attr->form == DW_FORM_data8
24608 || attr->form == DW_FORM_sec_offset);
24609 }
24610
24611 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24612 zero otherwise. When this function returns true, you can apply
24613 dwarf2_get_attr_constant_value to it.
24614
24615 However, note that for some attributes you must check
24616 attr_form_is_section_offset before using this test. DW_FORM_data4
24617 and DW_FORM_data8 are members of both the constant class, and of
24618 the classes that contain offsets into other debug sections
24619 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24620 that, if an attribute's can be either a constant or one of the
24621 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
24622 taken as section offsets, not constants.
24623
24624 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24625 cannot handle that. */
24626
24627 static int
24628 attr_form_is_constant (const struct attribute *attr)
24629 {
24630 switch (attr->form)
24631 {
24632 case DW_FORM_sdata:
24633 case DW_FORM_udata:
24634 case DW_FORM_data1:
24635 case DW_FORM_data2:
24636 case DW_FORM_data4:
24637 case DW_FORM_data8:
24638 case DW_FORM_implicit_const:
24639 return 1;
24640 default:
24641 return 0;
24642 }
24643 }
24644
24645
24646 /* DW_ADDR is always stored already as sect_offset; despite for the forms
24647 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24648
24649 static int
24650 attr_form_is_ref (const struct attribute *attr)
24651 {
24652 switch (attr->form)
24653 {
24654 case DW_FORM_ref_addr:
24655 case DW_FORM_ref1:
24656 case DW_FORM_ref2:
24657 case DW_FORM_ref4:
24658 case DW_FORM_ref8:
24659 case DW_FORM_ref_udata:
24660 case DW_FORM_GNU_ref_alt:
24661 return 1;
24662 default:
24663 return 0;
24664 }
24665 }
24666
24667 /* Return the .debug_loc section to use for CU.
24668 For DWO files use .debug_loc.dwo. */
24669
24670 static struct dwarf2_section_info *
24671 cu_debug_loc_section (struct dwarf2_cu *cu)
24672 {
24673 if (cu->dwo_unit)
24674 {
24675 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24676
24677 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24678 }
24679 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24680 : &dwarf2_per_objfile->loc);
24681 }
24682
24683 /* A helper function that fills in a dwarf2_loclist_baton. */
24684
24685 static void
24686 fill_in_loclist_baton (struct dwarf2_cu *cu,
24687 struct dwarf2_loclist_baton *baton,
24688 const struct attribute *attr)
24689 {
24690 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24691
24692 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
24693
24694 baton->per_cu = cu->per_cu;
24695 gdb_assert (baton->per_cu);
24696 /* We don't know how long the location list is, but make sure we
24697 don't run off the edge of the section. */
24698 baton->size = section->size - DW_UNSND (attr);
24699 baton->data = section->buffer + DW_UNSND (attr);
24700 baton->base_address = cu->base_address;
24701 baton->from_dwo = cu->dwo_unit != NULL;
24702 }
24703
24704 static void
24705 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24706 struct dwarf2_cu *cu, int is_block)
24707 {
24708 struct objfile *objfile = dwarf2_per_objfile->objfile;
24709 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24710
24711 if (attr_form_is_section_offset (attr)
24712 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24713 the section. If so, fall through to the complaint in the
24714 other branch. */
24715 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24716 {
24717 struct dwarf2_loclist_baton *baton;
24718
24719 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24720
24721 fill_in_loclist_baton (cu, baton, attr);
24722
24723 if (cu->base_known == 0)
24724 complaint (&symfile_complaints,
24725 _("Location list used without "
24726 "specifying the CU base address."));
24727
24728 SYMBOL_ACLASS_INDEX (sym) = (is_block
24729 ? dwarf2_loclist_block_index
24730 : dwarf2_loclist_index);
24731 SYMBOL_LOCATION_BATON (sym) = baton;
24732 }
24733 else
24734 {
24735 struct dwarf2_locexpr_baton *baton;
24736
24737 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24738 baton->per_cu = cu->per_cu;
24739 gdb_assert (baton->per_cu);
24740
24741 if (attr_form_is_block (attr))
24742 {
24743 /* Note that we're just copying the block's data pointer
24744 here, not the actual data. We're still pointing into the
24745 info_buffer for SYM's objfile; right now we never release
24746 that buffer, but when we do clean up properly this may
24747 need to change. */
24748 baton->size = DW_BLOCK (attr)->size;
24749 baton->data = DW_BLOCK (attr)->data;
24750 }
24751 else
24752 {
24753 dwarf2_invalid_attrib_class_complaint ("location description",
24754 SYMBOL_NATURAL_NAME (sym));
24755 baton->size = 0;
24756 }
24757
24758 SYMBOL_ACLASS_INDEX (sym) = (is_block
24759 ? dwarf2_locexpr_block_index
24760 : dwarf2_locexpr_index);
24761 SYMBOL_LOCATION_BATON (sym) = baton;
24762 }
24763 }
24764
24765 /* Return the OBJFILE associated with the compilation unit CU. If CU
24766 came from a separate debuginfo file, then the master objfile is
24767 returned. */
24768
24769 struct objfile *
24770 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24771 {
24772 struct objfile *objfile = per_cu->objfile;
24773
24774 /* Return the master objfile, so that we can report and look up the
24775 correct file containing this variable. */
24776 if (objfile->separate_debug_objfile_backlink)
24777 objfile = objfile->separate_debug_objfile_backlink;
24778
24779 return objfile;
24780 }
24781
24782 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24783 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24784 CU_HEADERP first. */
24785
24786 static const struct comp_unit_head *
24787 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24788 struct dwarf2_per_cu_data *per_cu)
24789 {
24790 const gdb_byte *info_ptr;
24791
24792 if (per_cu->cu)
24793 return &per_cu->cu->header;
24794
24795 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24796
24797 memset (cu_headerp, 0, sizeof (*cu_headerp));
24798 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24799 rcuh_kind::COMPILE);
24800
24801 return cu_headerp;
24802 }
24803
24804 /* Return the address size given in the compilation unit header for CU. */
24805
24806 int
24807 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24808 {
24809 struct comp_unit_head cu_header_local;
24810 const struct comp_unit_head *cu_headerp;
24811
24812 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24813
24814 return cu_headerp->addr_size;
24815 }
24816
24817 /* Return the offset size given in the compilation unit header for CU. */
24818
24819 int
24820 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24821 {
24822 struct comp_unit_head cu_header_local;
24823 const struct comp_unit_head *cu_headerp;
24824
24825 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24826
24827 return cu_headerp->offset_size;
24828 }
24829
24830 /* See its dwarf2loc.h declaration. */
24831
24832 int
24833 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24834 {
24835 struct comp_unit_head cu_header_local;
24836 const struct comp_unit_head *cu_headerp;
24837
24838 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24839
24840 if (cu_headerp->version == 2)
24841 return cu_headerp->addr_size;
24842 else
24843 return cu_headerp->offset_size;
24844 }
24845
24846 /* Return the text offset of the CU. The returned offset comes from
24847 this CU's objfile. If this objfile came from a separate debuginfo
24848 file, then the offset may be different from the corresponding
24849 offset in the parent objfile. */
24850
24851 CORE_ADDR
24852 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24853 {
24854 struct objfile *objfile = per_cu->objfile;
24855
24856 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24857 }
24858
24859 /* Return DWARF version number of PER_CU. */
24860
24861 short
24862 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24863 {
24864 return per_cu->dwarf_version;
24865 }
24866
24867 /* Locate the .debug_info compilation unit from CU's objfile which contains
24868 the DIE at OFFSET. Raises an error on failure. */
24869
24870 static struct dwarf2_per_cu_data *
24871 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24872 unsigned int offset_in_dwz,
24873 struct objfile *objfile)
24874 {
24875 struct dwarf2_per_cu_data *this_cu;
24876 int low, high;
24877 const sect_offset *cu_off;
24878
24879 low = 0;
24880 high = dwarf2_per_objfile->n_comp_units - 1;
24881 while (high > low)
24882 {
24883 struct dwarf2_per_cu_data *mid_cu;
24884 int mid = low + (high - low) / 2;
24885
24886 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
24887 cu_off = &mid_cu->sect_off;
24888 if (mid_cu->is_dwz > offset_in_dwz
24889 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
24890 high = mid;
24891 else
24892 low = mid + 1;
24893 }
24894 gdb_assert (low == high);
24895 this_cu = dwarf2_per_objfile->all_comp_units[low];
24896 cu_off = &this_cu->sect_off;
24897 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
24898 {
24899 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24900 error (_("Dwarf Error: could not find partial DIE containing "
24901 "offset 0x%x [in module %s]"),
24902 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
24903
24904 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24905 <= sect_off);
24906 return dwarf2_per_objfile->all_comp_units[low-1];
24907 }
24908 else
24909 {
24910 this_cu = dwarf2_per_objfile->all_comp_units[low];
24911 if (low == dwarf2_per_objfile->n_comp_units - 1
24912 && sect_off >= this_cu->sect_off + this_cu->length)
24913 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
24914 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24915 return this_cu;
24916 }
24917 }
24918
24919 /* Initialize dwarf2_cu CU, owned by PER_CU. */
24920
24921 static void
24922 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
24923 {
24924 memset (cu, 0, sizeof (*cu));
24925 per_cu->cu = cu;
24926 cu->per_cu = per_cu;
24927 cu->objfile = per_cu->objfile;
24928 obstack_init (&cu->comp_unit_obstack);
24929 }
24930
24931 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24932
24933 static void
24934 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24935 enum language pretend_language)
24936 {
24937 struct attribute *attr;
24938
24939 /* Set the language we're debugging. */
24940 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24941 if (attr)
24942 set_cu_language (DW_UNSND (attr), cu);
24943 else
24944 {
24945 cu->language = pretend_language;
24946 cu->language_defn = language_def (cu->language);
24947 }
24948
24949 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24950 }
24951
24952 /* Release one cached compilation unit, CU. We unlink it from the tree
24953 of compilation units, but we don't remove it from the read_in_chain;
24954 the caller is responsible for that.
24955 NOTE: DATA is a void * because this function is also used as a
24956 cleanup routine. */
24957
24958 static void
24959 free_heap_comp_unit (void *data)
24960 {
24961 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
24962
24963 gdb_assert (cu->per_cu != NULL);
24964 cu->per_cu->cu = NULL;
24965 cu->per_cu = NULL;
24966
24967 obstack_free (&cu->comp_unit_obstack, NULL);
24968
24969 xfree (cu);
24970 }
24971
24972 /* This cleanup function is passed the address of a dwarf2_cu on the stack
24973 when we're finished with it. We can't free the pointer itself, but be
24974 sure to unlink it from the cache. Also release any associated storage. */
24975
24976 static void
24977 free_stack_comp_unit (void *data)
24978 {
24979 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
24980
24981 gdb_assert (cu->per_cu != NULL);
24982 cu->per_cu->cu = NULL;
24983 cu->per_cu = NULL;
24984
24985 obstack_free (&cu->comp_unit_obstack, NULL);
24986 cu->partial_dies = NULL;
24987 }
24988
24989 /* Free all cached compilation units. */
24990
24991 static void
24992 free_cached_comp_units (void *data)
24993 {
24994 dwarf2_per_objfile->free_cached_comp_units ();
24995 }
24996
24997 /* Increase the age counter on each cached compilation unit, and free
24998 any that are too old. */
24999
25000 static void
25001 age_cached_comp_units (void)
25002 {
25003 struct dwarf2_per_cu_data *per_cu, **last_chain;
25004
25005 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25006 per_cu = dwarf2_per_objfile->read_in_chain;
25007 while (per_cu != NULL)
25008 {
25009 per_cu->cu->last_used ++;
25010 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25011 dwarf2_mark (per_cu->cu);
25012 per_cu = per_cu->cu->read_in_chain;
25013 }
25014
25015 per_cu = dwarf2_per_objfile->read_in_chain;
25016 last_chain = &dwarf2_per_objfile->read_in_chain;
25017 while (per_cu != NULL)
25018 {
25019 struct dwarf2_per_cu_data *next_cu;
25020
25021 next_cu = per_cu->cu->read_in_chain;
25022
25023 if (!per_cu->cu->mark)
25024 {
25025 free_heap_comp_unit (per_cu->cu);
25026 *last_chain = next_cu;
25027 }
25028 else
25029 last_chain = &per_cu->cu->read_in_chain;
25030
25031 per_cu = next_cu;
25032 }
25033 }
25034
25035 /* Remove a single compilation unit from the cache. */
25036
25037 static void
25038 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25039 {
25040 struct dwarf2_per_cu_data *per_cu, **last_chain;
25041
25042 per_cu = dwarf2_per_objfile->read_in_chain;
25043 last_chain = &dwarf2_per_objfile->read_in_chain;
25044 while (per_cu != NULL)
25045 {
25046 struct dwarf2_per_cu_data *next_cu;
25047
25048 next_cu = per_cu->cu->read_in_chain;
25049
25050 if (per_cu == target_per_cu)
25051 {
25052 free_heap_comp_unit (per_cu->cu);
25053 per_cu->cu = NULL;
25054 *last_chain = next_cu;
25055 break;
25056 }
25057 else
25058 last_chain = &per_cu->cu->read_in_chain;
25059
25060 per_cu = next_cu;
25061 }
25062 }
25063
25064 /* Release all extra memory associated with OBJFILE. */
25065
25066 void
25067 dwarf2_free_objfile (struct objfile *objfile)
25068 {
25069 dwarf2_per_objfile
25070 = (struct dwarf2_per_objfile *) objfile_data (objfile,
25071 dwarf2_objfile_data_key);
25072
25073 if (dwarf2_per_objfile == NULL)
25074 return;
25075
25076 dwarf2_per_objfile->~dwarf2_per_objfile ();
25077 }
25078
25079 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25080 We store these in a hash table separate from the DIEs, and preserve them
25081 when the DIEs are flushed out of cache.
25082
25083 The CU "per_cu" pointer is needed because offset alone is not enough to
25084 uniquely identify the type. A file may have multiple .debug_types sections,
25085 or the type may come from a DWO file. Furthermore, while it's more logical
25086 to use per_cu->section+offset, with Fission the section with the data is in
25087 the DWO file but we don't know that section at the point we need it.
25088 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25089 because we can enter the lookup routine, get_die_type_at_offset, from
25090 outside this file, and thus won't necessarily have PER_CU->cu.
25091 Fortunately, PER_CU is stable for the life of the objfile. */
25092
25093 struct dwarf2_per_cu_offset_and_type
25094 {
25095 const struct dwarf2_per_cu_data *per_cu;
25096 sect_offset sect_off;
25097 struct type *type;
25098 };
25099
25100 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25101
25102 static hashval_t
25103 per_cu_offset_and_type_hash (const void *item)
25104 {
25105 const struct dwarf2_per_cu_offset_and_type *ofs
25106 = (const struct dwarf2_per_cu_offset_and_type *) item;
25107
25108 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25109 }
25110
25111 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25112
25113 static int
25114 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25115 {
25116 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25117 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25118 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25119 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25120
25121 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25122 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25123 }
25124
25125 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25126 table if necessary. For convenience, return TYPE.
25127
25128 The DIEs reading must have careful ordering to:
25129 * Not cause infite loops trying to read in DIEs as a prerequisite for
25130 reading current DIE.
25131 * Not trying to dereference contents of still incompletely read in types
25132 while reading in other DIEs.
25133 * Enable referencing still incompletely read in types just by a pointer to
25134 the type without accessing its fields.
25135
25136 Therefore caller should follow these rules:
25137 * Try to fetch any prerequisite types we may need to build this DIE type
25138 before building the type and calling set_die_type.
25139 * After building type call set_die_type for current DIE as soon as
25140 possible before fetching more types to complete the current type.
25141 * Make the type as complete as possible before fetching more types. */
25142
25143 static struct type *
25144 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25145 {
25146 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25147 struct objfile *objfile = cu->objfile;
25148 struct attribute *attr;
25149 struct dynamic_prop prop;
25150
25151 /* For Ada types, make sure that the gnat-specific data is always
25152 initialized (if not already set). There are a few types where
25153 we should not be doing so, because the type-specific area is
25154 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25155 where the type-specific area is used to store the floatformat).
25156 But this is not a problem, because the gnat-specific information
25157 is actually not needed for these types. */
25158 if (need_gnat_info (cu)
25159 && TYPE_CODE (type) != TYPE_CODE_FUNC
25160 && TYPE_CODE (type) != TYPE_CODE_FLT
25161 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25162 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25163 && TYPE_CODE (type) != TYPE_CODE_METHOD
25164 && !HAVE_GNAT_AUX_INFO (type))
25165 INIT_GNAT_SPECIFIC (type);
25166
25167 /* Read DW_AT_allocated and set in type. */
25168 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25169 if (attr_form_is_block (attr))
25170 {
25171 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25172 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
25173 }
25174 else if (attr != NULL)
25175 {
25176 complaint (&symfile_complaints,
25177 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
25178 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25179 to_underlying (die->sect_off));
25180 }
25181
25182 /* Read DW_AT_associated and set in type. */
25183 attr = dwarf2_attr (die, DW_AT_associated, cu);
25184 if (attr_form_is_block (attr))
25185 {
25186 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25187 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
25188 }
25189 else if (attr != NULL)
25190 {
25191 complaint (&symfile_complaints,
25192 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
25193 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25194 to_underlying (die->sect_off));
25195 }
25196
25197 /* Read DW_AT_data_location and set in type. */
25198 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25199 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25200 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
25201
25202 if (dwarf2_per_objfile->die_type_hash == NULL)
25203 {
25204 dwarf2_per_objfile->die_type_hash =
25205 htab_create_alloc_ex (127,
25206 per_cu_offset_and_type_hash,
25207 per_cu_offset_and_type_eq,
25208 NULL,
25209 &objfile->objfile_obstack,
25210 hashtab_obstack_allocate,
25211 dummy_obstack_deallocate);
25212 }
25213
25214 ofs.per_cu = cu->per_cu;
25215 ofs.sect_off = die->sect_off;
25216 ofs.type = type;
25217 slot = (struct dwarf2_per_cu_offset_and_type **)
25218 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25219 if (*slot)
25220 complaint (&symfile_complaints,
25221 _("A problem internal to GDB: DIE 0x%x has type already set"),
25222 to_underlying (die->sect_off));
25223 *slot = XOBNEW (&objfile->objfile_obstack,
25224 struct dwarf2_per_cu_offset_and_type);
25225 **slot = ofs;
25226 return type;
25227 }
25228
25229 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25230 or return NULL if the die does not have a saved type. */
25231
25232 static struct type *
25233 get_die_type_at_offset (sect_offset sect_off,
25234 struct dwarf2_per_cu_data *per_cu)
25235 {
25236 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25237
25238 if (dwarf2_per_objfile->die_type_hash == NULL)
25239 return NULL;
25240
25241 ofs.per_cu = per_cu;
25242 ofs.sect_off = sect_off;
25243 slot = ((struct dwarf2_per_cu_offset_and_type *)
25244 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25245 if (slot)
25246 return slot->type;
25247 else
25248 return NULL;
25249 }
25250
25251 /* Look up the type for DIE in CU in die_type_hash,
25252 or return NULL if DIE does not have a saved type. */
25253
25254 static struct type *
25255 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25256 {
25257 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25258 }
25259
25260 /* Add a dependence relationship from CU to REF_PER_CU. */
25261
25262 static void
25263 dwarf2_add_dependence (struct dwarf2_cu *cu,
25264 struct dwarf2_per_cu_data *ref_per_cu)
25265 {
25266 void **slot;
25267
25268 if (cu->dependencies == NULL)
25269 cu->dependencies
25270 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25271 NULL, &cu->comp_unit_obstack,
25272 hashtab_obstack_allocate,
25273 dummy_obstack_deallocate);
25274
25275 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25276 if (*slot == NULL)
25277 *slot = ref_per_cu;
25278 }
25279
25280 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25281 Set the mark field in every compilation unit in the
25282 cache that we must keep because we are keeping CU. */
25283
25284 static int
25285 dwarf2_mark_helper (void **slot, void *data)
25286 {
25287 struct dwarf2_per_cu_data *per_cu;
25288
25289 per_cu = (struct dwarf2_per_cu_data *) *slot;
25290
25291 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25292 reading of the chain. As such dependencies remain valid it is not much
25293 useful to track and undo them during QUIT cleanups. */
25294 if (per_cu->cu == NULL)
25295 return 1;
25296
25297 if (per_cu->cu->mark)
25298 return 1;
25299 per_cu->cu->mark = 1;
25300
25301 if (per_cu->cu->dependencies != NULL)
25302 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25303
25304 return 1;
25305 }
25306
25307 /* Set the mark field in CU and in every other compilation unit in the
25308 cache that we must keep because we are keeping CU. */
25309
25310 static void
25311 dwarf2_mark (struct dwarf2_cu *cu)
25312 {
25313 if (cu->mark)
25314 return;
25315 cu->mark = 1;
25316 if (cu->dependencies != NULL)
25317 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25318 }
25319
25320 static void
25321 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25322 {
25323 while (per_cu)
25324 {
25325 per_cu->cu->mark = 0;
25326 per_cu = per_cu->cu->read_in_chain;
25327 }
25328 }
25329
25330 /* Trivial hash function for partial_die_info: the hash value of a DIE
25331 is its offset in .debug_info for this objfile. */
25332
25333 static hashval_t
25334 partial_die_hash (const void *item)
25335 {
25336 const struct partial_die_info *part_die
25337 = (const struct partial_die_info *) item;
25338
25339 return to_underlying (part_die->sect_off);
25340 }
25341
25342 /* Trivial comparison function for partial_die_info structures: two DIEs
25343 are equal if they have the same offset. */
25344
25345 static int
25346 partial_die_eq (const void *item_lhs, const void *item_rhs)
25347 {
25348 const struct partial_die_info *part_die_lhs
25349 = (const struct partial_die_info *) item_lhs;
25350 const struct partial_die_info *part_die_rhs
25351 = (const struct partial_die_info *) item_rhs;
25352
25353 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25354 }
25355
25356 static struct cmd_list_element *set_dwarf_cmdlist;
25357 static struct cmd_list_element *show_dwarf_cmdlist;
25358
25359 static void
25360 set_dwarf_cmd (const char *args, int from_tty)
25361 {
25362 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25363 gdb_stdout);
25364 }
25365
25366 static void
25367 show_dwarf_cmd (const char *args, int from_tty)
25368 {
25369 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25370 }
25371
25372 /* Free data associated with OBJFILE, if necessary. */
25373
25374 static void
25375 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
25376 {
25377 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
25378 int ix;
25379
25380 /* Make sure we don't accidentally use dwarf2_per_objfile while
25381 cleaning up. */
25382 dwarf2_per_objfile = NULL;
25383
25384 for (ix = 0; ix < data->n_comp_units; ++ix)
25385 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
25386
25387 for (ix = 0; ix < data->n_type_units; ++ix)
25388 VEC_free (dwarf2_per_cu_ptr,
25389 data->all_type_units[ix]->per_cu.imported_symtabs);
25390 xfree (data->all_type_units);
25391
25392 VEC_free (dwarf2_section_info_def, data->types);
25393
25394 if (data->dwo_files)
25395 free_dwo_files (data->dwo_files, objfile);
25396 if (data->dwp_file)
25397 gdb_bfd_unref (data->dwp_file->dbfd);
25398
25399 if (data->dwz_file && data->dwz_file->dwz_bfd)
25400 gdb_bfd_unref (data->dwz_file->dwz_bfd);
25401
25402 if (data->index_table != NULL)
25403 data->index_table->~mapped_index ();
25404 }
25405
25406 \f
25407 /* The "save gdb-index" command. */
25408
25409 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25410 error checking. */
25411
25412 static void
25413 file_write (FILE *file, const void *data, size_t size)
25414 {
25415 if (fwrite (data, 1, size, file) != size)
25416 error (_("couldn't data write to file"));
25417 }
25418
25419 /* Write the contents of VEC to FILE, with error checking. */
25420
25421 template<typename Elem, typename Alloc>
25422 static void
25423 file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25424 {
25425 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25426 }
25427
25428 /* In-memory buffer to prepare data to be written later to a file. */
25429 class data_buf
25430 {
25431 public:
25432 /* Copy DATA to the end of the buffer. */
25433 template<typename T>
25434 void append_data (const T &data)
25435 {
25436 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25437 reinterpret_cast<const gdb_byte *> (&data + 1),
25438 grow (sizeof (data)));
25439 }
25440
25441 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25442 terminating zero is appended too. */
25443 void append_cstr0 (const char *cstr)
25444 {
25445 const size_t size = strlen (cstr) + 1;
25446 std::copy (cstr, cstr + size, grow (size));
25447 }
25448
25449 /* Store INPUT as ULEB128 to the end of buffer. */
25450 void append_unsigned_leb128 (ULONGEST input)
25451 {
25452 for (;;)
25453 {
25454 gdb_byte output = input & 0x7f;
25455 input >>= 7;
25456 if (input)
25457 output |= 0x80;
25458 append_data (output);
25459 if (input == 0)
25460 break;
25461 }
25462 }
25463
25464 /* Accept a host-format integer in VAL and append it to the buffer
25465 as a target-format integer which is LEN bytes long. */
25466 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25467 {
25468 ::store_unsigned_integer (grow (len), len, byte_order, val);
25469 }
25470
25471 /* Return the size of the buffer. */
25472 size_t size () const
25473 {
25474 return m_vec.size ();
25475 }
25476
25477 /* Return true iff the buffer is empty. */
25478 bool empty () const
25479 {
25480 return m_vec.empty ();
25481 }
25482
25483 /* Write the buffer to FILE. */
25484 void file_write (FILE *file) const
25485 {
25486 ::file_write (file, m_vec);
25487 }
25488
25489 private:
25490 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25491 the start of the new block. */
25492 gdb_byte *grow (size_t size)
25493 {
25494 m_vec.resize (m_vec.size () + size);
25495 return &*m_vec.end () - size;
25496 }
25497
25498 gdb::byte_vector m_vec;
25499 };
25500
25501 /* An entry in the symbol table. */
25502 struct symtab_index_entry
25503 {
25504 /* The name of the symbol. */
25505 const char *name;
25506 /* The offset of the name in the constant pool. */
25507 offset_type index_offset;
25508 /* A sorted vector of the indices of all the CUs that hold an object
25509 of this name. */
25510 std::vector<offset_type> cu_indices;
25511 };
25512
25513 /* The symbol table. This is a power-of-2-sized hash table. */
25514 struct mapped_symtab
25515 {
25516 mapped_symtab ()
25517 {
25518 data.resize (1024);
25519 }
25520
25521 offset_type n_elements = 0;
25522 std::vector<symtab_index_entry> data;
25523 };
25524
25525 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
25526 the slot.
25527
25528 Function is used only during write_hash_table so no index format backward
25529 compatibility is needed. */
25530
25531 static symtab_index_entry &
25532 find_slot (struct mapped_symtab *symtab, const char *name)
25533 {
25534 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
25535
25536 index = hash & (symtab->data.size () - 1);
25537 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
25538
25539 for (;;)
25540 {
25541 if (symtab->data[index].name == NULL
25542 || strcmp (name, symtab->data[index].name) == 0)
25543 return symtab->data[index];
25544 index = (index + step) & (symtab->data.size () - 1);
25545 }
25546 }
25547
25548 /* Expand SYMTAB's hash table. */
25549
25550 static void
25551 hash_expand (struct mapped_symtab *symtab)
25552 {
25553 auto old_entries = std::move (symtab->data);
25554
25555 symtab->data.clear ();
25556 symtab->data.resize (old_entries.size () * 2);
25557
25558 for (auto &it : old_entries)
25559 if (it.name != NULL)
25560 {
25561 auto &ref = find_slot (symtab, it.name);
25562 ref = std::move (it);
25563 }
25564 }
25565
25566 /* Add an entry to SYMTAB. NAME is the name of the symbol.
25567 CU_INDEX is the index of the CU in which the symbol appears.
25568 IS_STATIC is one if the symbol is static, otherwise zero (global). */
25569
25570 static void
25571 add_index_entry (struct mapped_symtab *symtab, const char *name,
25572 int is_static, gdb_index_symbol_kind kind,
25573 offset_type cu_index)
25574 {
25575 offset_type cu_index_and_attrs;
25576
25577 ++symtab->n_elements;
25578 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
25579 hash_expand (symtab);
25580
25581 symtab_index_entry &slot = find_slot (symtab, name);
25582 if (slot.name == NULL)
25583 {
25584 slot.name = name;
25585 /* index_offset is set later. */
25586 }
25587
25588 cu_index_and_attrs = 0;
25589 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
25590 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
25591 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
25592
25593 /* We don't want to record an index value twice as we want to avoid the
25594 duplication.
25595 We process all global symbols and then all static symbols
25596 (which would allow us to avoid the duplication by only having to check
25597 the last entry pushed), but a symbol could have multiple kinds in one CU.
25598 To keep things simple we don't worry about the duplication here and
25599 sort and uniqufy the list after we've processed all symbols. */
25600 slot.cu_indices.push_back (cu_index_and_attrs);
25601 }
25602
25603 /* Sort and remove duplicates of all symbols' cu_indices lists. */
25604
25605 static void
25606 uniquify_cu_indices (struct mapped_symtab *symtab)
25607 {
25608 for (auto &entry : symtab->data)
25609 {
25610 if (entry.name != NULL && !entry.cu_indices.empty ())
25611 {
25612 auto &cu_indices = entry.cu_indices;
25613 std::sort (cu_indices.begin (), cu_indices.end ());
25614 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
25615 cu_indices.erase (from, cu_indices.end ());
25616 }
25617 }
25618 }
25619
25620 /* A form of 'const char *' suitable for container keys. Only the
25621 pointer is stored. The strings themselves are compared, not the
25622 pointers. */
25623 class c_str_view
25624 {
25625 public:
25626 c_str_view (const char *cstr)
25627 : m_cstr (cstr)
25628 {}
25629
25630 bool operator== (const c_str_view &other) const
25631 {
25632 return strcmp (m_cstr, other.m_cstr) == 0;
25633 }
25634
25635 /* Return the underlying C string. Note, the returned string is
25636 only a reference with lifetime of this object. */
25637 const char *c_str () const
25638 {
25639 return m_cstr;
25640 }
25641
25642 private:
25643 friend class c_str_view_hasher;
25644 const char *const m_cstr;
25645 };
25646
25647 /* A std::unordered_map::hasher for c_str_view that uses the right
25648 hash function for strings in a mapped index. */
25649 class c_str_view_hasher
25650 {
25651 public:
25652 size_t operator () (const c_str_view &x) const
25653 {
25654 return mapped_index_string_hash (INT_MAX, x.m_cstr);
25655 }
25656 };
25657
25658 /* A std::unordered_map::hasher for std::vector<>. */
25659 template<typename T>
25660 class vector_hasher
25661 {
25662 public:
25663 size_t operator () (const std::vector<T> &key) const
25664 {
25665 return iterative_hash (key.data (),
25666 sizeof (key.front ()) * key.size (), 0);
25667 }
25668 };
25669
25670 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
25671 constant pool entries going into the data buffer CPOOL. */
25672
25673 static void
25674 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
25675 {
25676 {
25677 /* Elements are sorted vectors of the indices of all the CUs that
25678 hold an object of this name. */
25679 std::unordered_map<std::vector<offset_type>, offset_type,
25680 vector_hasher<offset_type>>
25681 symbol_hash_table;
25682
25683 /* We add all the index vectors to the constant pool first, to
25684 ensure alignment is ok. */
25685 for (symtab_index_entry &entry : symtab->data)
25686 {
25687 if (entry.name == NULL)
25688 continue;
25689 gdb_assert (entry.index_offset == 0);
25690
25691 /* Finding before inserting is faster than always trying to
25692 insert, because inserting always allocates a node, does the
25693 lookup, and then destroys the new node if another node
25694 already had the same key. C++17 try_emplace will avoid
25695 this. */
25696 const auto found
25697 = symbol_hash_table.find (entry.cu_indices);
25698 if (found != symbol_hash_table.end ())
25699 {
25700 entry.index_offset = found->second;
25701 continue;
25702 }
25703
25704 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
25705 entry.index_offset = cpool.size ();
25706 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
25707 for (const auto index : entry.cu_indices)
25708 cpool.append_data (MAYBE_SWAP (index));
25709 }
25710 }
25711
25712 /* Now write out the hash table. */
25713 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
25714 for (const auto &entry : symtab->data)
25715 {
25716 offset_type str_off, vec_off;
25717
25718 if (entry.name != NULL)
25719 {
25720 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
25721 if (insertpair.second)
25722 cpool.append_cstr0 (entry.name);
25723 str_off = insertpair.first->second;
25724 vec_off = entry.index_offset;
25725 }
25726 else
25727 {
25728 /* While 0 is a valid constant pool index, it is not valid
25729 to have 0 for both offsets. */
25730 str_off = 0;
25731 vec_off = 0;
25732 }
25733
25734 output.append_data (MAYBE_SWAP (str_off));
25735 output.append_data (MAYBE_SWAP (vec_off));
25736 }
25737 }
25738
25739 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
25740
25741 /* Helper struct for building the address table. */
25742 struct addrmap_index_data
25743 {
25744 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
25745 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
25746 {}
25747
25748 struct objfile *objfile;
25749 data_buf &addr_vec;
25750 psym_index_map &cu_index_htab;
25751
25752 /* Non-zero if the previous_* fields are valid.
25753 We can't write an entry until we see the next entry (since it is only then
25754 that we know the end of the entry). */
25755 int previous_valid;
25756 /* Index of the CU in the table of all CUs in the index file. */
25757 unsigned int previous_cu_index;
25758 /* Start address of the CU. */
25759 CORE_ADDR previous_cu_start;
25760 };
25761
25762 /* Write an address entry to ADDR_VEC. */
25763
25764 static void
25765 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
25766 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
25767 {
25768 CORE_ADDR baseaddr;
25769
25770 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25771
25772 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
25773 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
25774 addr_vec.append_data (MAYBE_SWAP (cu_index));
25775 }
25776
25777 /* Worker function for traversing an addrmap to build the address table. */
25778
25779 static int
25780 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
25781 {
25782 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
25783 struct partial_symtab *pst = (struct partial_symtab *) obj;
25784
25785 if (data->previous_valid)
25786 add_address_entry (data->objfile, data->addr_vec,
25787 data->previous_cu_start, start_addr,
25788 data->previous_cu_index);
25789
25790 data->previous_cu_start = start_addr;
25791 if (pst != NULL)
25792 {
25793 const auto it = data->cu_index_htab.find (pst);
25794 gdb_assert (it != data->cu_index_htab.cend ());
25795 data->previous_cu_index = it->second;
25796 data->previous_valid = 1;
25797 }
25798 else
25799 data->previous_valid = 0;
25800
25801 return 0;
25802 }
25803
25804 /* Write OBJFILE's address map to ADDR_VEC.
25805 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
25806 in the index file. */
25807
25808 static void
25809 write_address_map (struct objfile *objfile, data_buf &addr_vec,
25810 psym_index_map &cu_index_htab)
25811 {
25812 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
25813
25814 /* When writing the address table, we have to cope with the fact that
25815 the addrmap iterator only provides the start of a region; we have to
25816 wait until the next invocation to get the start of the next region. */
25817
25818 addrmap_index_data.objfile = objfile;
25819 addrmap_index_data.previous_valid = 0;
25820
25821 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
25822 &addrmap_index_data);
25823
25824 /* It's highly unlikely the last entry (end address = 0xff...ff)
25825 is valid, but we should still handle it.
25826 The end address is recorded as the start of the next region, but that
25827 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
25828 anyway. */
25829 if (addrmap_index_data.previous_valid)
25830 add_address_entry (objfile, addr_vec,
25831 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
25832 addrmap_index_data.previous_cu_index);
25833 }
25834
25835 /* Return the symbol kind of PSYM. */
25836
25837 static gdb_index_symbol_kind
25838 symbol_kind (struct partial_symbol *psym)
25839 {
25840 domain_enum domain = PSYMBOL_DOMAIN (psym);
25841 enum address_class aclass = PSYMBOL_CLASS (psym);
25842
25843 switch (domain)
25844 {
25845 case VAR_DOMAIN:
25846 switch (aclass)
25847 {
25848 case LOC_BLOCK:
25849 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
25850 case LOC_TYPEDEF:
25851 return GDB_INDEX_SYMBOL_KIND_TYPE;
25852 case LOC_COMPUTED:
25853 case LOC_CONST_BYTES:
25854 case LOC_OPTIMIZED_OUT:
25855 case LOC_STATIC:
25856 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25857 case LOC_CONST:
25858 /* Note: It's currently impossible to recognize psyms as enum values
25859 short of reading the type info. For now punt. */
25860 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25861 default:
25862 /* There are other LOC_FOO values that one might want to classify
25863 as variables, but dwarf2read.c doesn't currently use them. */
25864 return GDB_INDEX_SYMBOL_KIND_OTHER;
25865 }
25866 case STRUCT_DOMAIN:
25867 return GDB_INDEX_SYMBOL_KIND_TYPE;
25868 default:
25869 return GDB_INDEX_SYMBOL_KIND_OTHER;
25870 }
25871 }
25872
25873 /* Add a list of partial symbols to SYMTAB. */
25874
25875 static void
25876 write_psymbols (struct mapped_symtab *symtab,
25877 std::unordered_set<partial_symbol *> &psyms_seen,
25878 struct partial_symbol **psymp,
25879 int count,
25880 offset_type cu_index,
25881 int is_static)
25882 {
25883 for (; count-- > 0; ++psymp)
25884 {
25885 struct partial_symbol *psym = *psymp;
25886
25887 if (SYMBOL_LANGUAGE (psym) == language_ada)
25888 error (_("Ada is not currently supported by the index"));
25889
25890 /* Only add a given psymbol once. */
25891 if (psyms_seen.insert (psym).second)
25892 {
25893 gdb_index_symbol_kind kind = symbol_kind (psym);
25894
25895 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
25896 is_static, kind, cu_index);
25897 }
25898 }
25899 }
25900
25901 /* A helper struct used when iterating over debug_types. */
25902 struct signatured_type_index_data
25903 {
25904 signatured_type_index_data (data_buf &types_list_,
25905 std::unordered_set<partial_symbol *> &psyms_seen_)
25906 : types_list (types_list_), psyms_seen (psyms_seen_)
25907 {}
25908
25909 struct objfile *objfile;
25910 struct mapped_symtab *symtab;
25911 data_buf &types_list;
25912 std::unordered_set<partial_symbol *> &psyms_seen;
25913 int cu_index;
25914 };
25915
25916 /* A helper function that writes a single signatured_type to an
25917 obstack. */
25918
25919 static int
25920 write_one_signatured_type (void **slot, void *d)
25921 {
25922 struct signatured_type_index_data *info
25923 = (struct signatured_type_index_data *) d;
25924 struct signatured_type *entry = (struct signatured_type *) *slot;
25925 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
25926
25927 write_psymbols (info->symtab,
25928 info->psyms_seen,
25929 &info->objfile->global_psymbols[psymtab->globals_offset],
25930 psymtab->n_global_syms, info->cu_index,
25931 0);
25932 write_psymbols (info->symtab,
25933 info->psyms_seen,
25934 &info->objfile->static_psymbols[psymtab->statics_offset],
25935 psymtab->n_static_syms, info->cu_index,
25936 1);
25937
25938 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
25939 to_underlying (entry->per_cu.sect_off));
25940 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
25941 to_underlying (entry->type_offset_in_tu));
25942 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
25943
25944 ++info->cu_index;
25945
25946 return 1;
25947 }
25948
25949 /* Recurse into all "included" dependencies and count their symbols as
25950 if they appeared in this psymtab. */
25951
25952 static void
25953 recursively_count_psymbols (struct partial_symtab *psymtab,
25954 size_t &psyms_seen)
25955 {
25956 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
25957 if (psymtab->dependencies[i]->user != NULL)
25958 recursively_count_psymbols (psymtab->dependencies[i],
25959 psyms_seen);
25960
25961 psyms_seen += psymtab->n_global_syms;
25962 psyms_seen += psymtab->n_static_syms;
25963 }
25964
25965 /* Recurse into all "included" dependencies and write their symbols as
25966 if they appeared in this psymtab. */
25967
25968 static void
25969 recursively_write_psymbols (struct objfile *objfile,
25970 struct partial_symtab *psymtab,
25971 struct mapped_symtab *symtab,
25972 std::unordered_set<partial_symbol *> &psyms_seen,
25973 offset_type cu_index)
25974 {
25975 int i;
25976
25977 for (i = 0; i < psymtab->number_of_dependencies; ++i)
25978 if (psymtab->dependencies[i]->user != NULL)
25979 recursively_write_psymbols (objfile, psymtab->dependencies[i],
25980 symtab, psyms_seen, cu_index);
25981
25982 write_psymbols (symtab,
25983 psyms_seen,
25984 &objfile->global_psymbols[psymtab->globals_offset],
25985 psymtab->n_global_syms, cu_index,
25986 0);
25987 write_psymbols (symtab,
25988 psyms_seen,
25989 &objfile->static_psymbols[psymtab->statics_offset],
25990 psymtab->n_static_syms, cu_index,
25991 1);
25992 }
25993
25994 /* DWARF-5 .debug_names builder. */
25995 class debug_names
25996 {
25997 public:
25998 debug_names (bool is_dwarf64, bfd_endian dwarf5_byte_order)
25999 : m_dwarf5_byte_order (dwarf5_byte_order),
26000 m_dwarf32 (dwarf5_byte_order),
26001 m_dwarf64 (dwarf5_byte_order),
26002 m_dwarf (is_dwarf64
26003 ? static_cast<dwarf &> (m_dwarf64)
26004 : static_cast<dwarf &> (m_dwarf32)),
26005 m_name_table_string_offs (m_dwarf.name_table_string_offs),
26006 m_name_table_entry_offs (m_dwarf.name_table_entry_offs)
26007 {}
26008
26009 /* Insert one symbol. */
26010 void insert (const partial_symbol *psym, int cu_index, bool is_static)
26011 {
26012 const int dwarf_tag = psymbol_tag (psym);
26013 if (dwarf_tag == 0)
26014 return;
26015 const char *const name = SYMBOL_SEARCH_NAME (psym);
26016 const auto insertpair
26017 = m_name_to_value_set.emplace (c_str_view (name),
26018 std::set<symbol_value> ());
26019 std::set<symbol_value> &value_set = insertpair.first->second;
26020 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static));
26021 }
26022
26023 /* Build all the tables. All symbols must be already inserted.
26024 This function does not call file_write, caller has to do it
26025 afterwards. */
26026 void build ()
26027 {
26028 /* Verify the build method has not be called twice. */
26029 gdb_assert (m_abbrev_table.empty ());
26030 const size_t name_count = m_name_to_value_set.size ();
26031 m_bucket_table.resize
26032 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26033 m_hash_table.reserve (name_count);
26034 m_name_table_string_offs.reserve (name_count);
26035 m_name_table_entry_offs.reserve (name_count);
26036
26037 /* Map each hash of symbol to its name and value. */
26038 struct hash_it_pair
26039 {
26040 uint32_t hash;
26041 decltype (m_name_to_value_set)::const_iterator it;
26042 };
26043 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26044 bucket_hash.resize (m_bucket_table.size ());
26045 for (decltype (m_name_to_value_set)::const_iterator it
26046 = m_name_to_value_set.cbegin ();
26047 it != m_name_to_value_set.cend ();
26048 ++it)
26049 {
26050 const char *const name = it->first.c_str ();
26051 const uint32_t hash = dwarf5_djb_hash (name);
26052 hash_it_pair hashitpair;
26053 hashitpair.hash = hash;
26054 hashitpair.it = it;
26055 auto &slot = bucket_hash[hash % bucket_hash.size()];
26056 slot.push_front (std::move (hashitpair));
26057 }
26058 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26059 {
26060 const std::forward_list<hash_it_pair> &hashitlist
26061 = bucket_hash[bucket_ix];
26062 if (hashitlist.empty ())
26063 continue;
26064 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26065 /* The hashes array is indexed starting at 1. */
26066 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26067 sizeof (bucket_slot), m_dwarf5_byte_order,
26068 m_hash_table.size () + 1);
26069 for (const hash_it_pair &hashitpair : hashitlist)
26070 {
26071 m_hash_table.push_back (0);
26072 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26073 (&m_hash_table.back ()),
26074 sizeof (m_hash_table.back ()),
26075 m_dwarf5_byte_order, hashitpair.hash);
26076 const c_str_view &name = hashitpair.it->first;
26077 const std::set<symbol_value> &value_set = hashitpair.it->second;
26078 m_name_table_string_offs.push_back_reorder
26079 (m_debugstrlookup.lookup (name.c_str ()));
26080 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26081 gdb_assert (!value_set.empty ());
26082 for (const symbol_value &value : value_set)
26083 {
26084 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
26085 value.is_static)];
26086 if (idx == 0)
26087 {
26088 idx = m_idx_next++;
26089 m_abbrev_table.append_unsigned_leb128 (idx);
26090 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
26091 m_abbrev_table.append_unsigned_leb128 (DW_IDX_compile_unit);
26092 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26093 m_abbrev_table.append_unsigned_leb128 (value.is_static
26094 ? DW_IDX_GNU_internal
26095 : DW_IDX_GNU_external);
26096 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26097
26098 /* Terminate attributes list. */
26099 m_abbrev_table.append_unsigned_leb128 (0);
26100 m_abbrev_table.append_unsigned_leb128 (0);
26101 }
26102
26103 m_entry_pool.append_unsigned_leb128 (idx);
26104 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26105 }
26106
26107 /* Terminate the list of CUs. */
26108 m_entry_pool.append_unsigned_leb128 (0);
26109 }
26110 }
26111 gdb_assert (m_hash_table.size () == name_count);
26112
26113 /* Terminate tags list. */
26114 m_abbrev_table.append_unsigned_leb128 (0);
26115 }
26116
26117 /* Return .debug_names bucket count. This must be called only after
26118 calling the build method. */
26119 uint32_t bucket_count () const
26120 {
26121 /* Verify the build method has been already called. */
26122 gdb_assert (!m_abbrev_table.empty ());
26123 const uint32_t retval = m_bucket_table.size ();
26124
26125 /* Check for overflow. */
26126 gdb_assert (retval == m_bucket_table.size ());
26127 return retval;
26128 }
26129
26130 /* Return .debug_names names count. This must be called only after
26131 calling the build method. */
26132 uint32_t name_count () const
26133 {
26134 /* Verify the build method has been already called. */
26135 gdb_assert (!m_abbrev_table.empty ());
26136 const uint32_t retval = m_hash_table.size ();
26137
26138 /* Check for overflow. */
26139 gdb_assert (retval == m_hash_table.size ());
26140 return retval;
26141 }
26142
26143 /* Return number of bytes of .debug_names abbreviation table. This
26144 must be called only after calling the build method. */
26145 uint32_t abbrev_table_bytes () const
26146 {
26147 gdb_assert (!m_abbrev_table.empty ());
26148 return m_abbrev_table.size ();
26149 }
26150
26151 /* Recurse into all "included" dependencies and store their symbols
26152 as if they appeared in this psymtab. */
26153 void recursively_write_psymbols
26154 (struct objfile *objfile,
26155 struct partial_symtab *psymtab,
26156 std::unordered_set<partial_symbol *> &psyms_seen,
26157 int cu_index)
26158 {
26159 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26160 if (psymtab->dependencies[i]->user != NULL)
26161 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26162 psyms_seen, cu_index);
26163
26164 write_psymbols (psyms_seen,
26165 &objfile->global_psymbols[psymtab->globals_offset],
26166 psymtab->n_global_syms, cu_index, false);
26167 write_psymbols (psyms_seen,
26168 &objfile->static_psymbols[psymtab->statics_offset],
26169 psymtab->n_static_syms, cu_index, true);
26170 }
26171
26172 /* Return number of bytes the .debug_names section will have. This
26173 must be called only after calling the build method. */
26174 size_t bytes () const
26175 {
26176 /* Verify the build method has been already called. */
26177 gdb_assert (!m_abbrev_table.empty ());
26178 size_t expected_bytes = 0;
26179 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26180 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26181 expected_bytes += m_name_table_string_offs.bytes ();
26182 expected_bytes += m_name_table_entry_offs.bytes ();
26183 expected_bytes += m_abbrev_table.size ();
26184 expected_bytes += m_entry_pool.size ();
26185 return expected_bytes;
26186 }
26187
26188 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26189 FILE_STR. This must be called only after calling the build
26190 method. */
26191 void file_write (FILE *file_names, FILE *file_str) const
26192 {
26193 /* Verify the build method has been already called. */
26194 gdb_assert (!m_abbrev_table.empty ());
26195 ::file_write (file_names, m_bucket_table);
26196 ::file_write (file_names, m_hash_table);
26197 m_name_table_string_offs.file_write (file_names);
26198 m_name_table_entry_offs.file_write (file_names);
26199 m_abbrev_table.file_write (file_names);
26200 m_entry_pool.file_write (file_names);
26201 m_debugstrlookup.file_write (file_str);
26202 }
26203
26204 private:
26205
26206 /* Storage for symbol names mapping them to their .debug_str section
26207 offsets. */
26208 class debug_str_lookup
26209 {
26210 public:
26211
26212 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26213 All .debug_str section strings are automatically stored. */
26214 debug_str_lookup ()
26215 : m_abfd (dwarf2_per_objfile->objfile->obfd)
26216 {
26217 dwarf2_read_section (dwarf2_per_objfile->objfile,
26218 &dwarf2_per_objfile->str);
26219 if (dwarf2_per_objfile->str.buffer == NULL)
26220 return;
26221 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26222 data < (dwarf2_per_objfile->str.buffer
26223 + dwarf2_per_objfile->str.size);)
26224 {
26225 const char *const s = reinterpret_cast<const char *> (data);
26226 const auto insertpair
26227 = m_str_table.emplace (c_str_view (s),
26228 data - dwarf2_per_objfile->str.buffer);
26229 if (!insertpair.second)
26230 complaint (&symfile_complaints,
26231 _("Duplicate string \"%s\" in "
26232 ".debug_str section [in module %s]"),
26233 s, bfd_get_filename (m_abfd));
26234 data += strlen (s) + 1;
26235 }
26236 }
26237
26238 /* Return offset of symbol name S in the .debug_str section. Add
26239 such symbol to the section's end if it does not exist there
26240 yet. */
26241 size_t lookup (const char *s)
26242 {
26243 const auto it = m_str_table.find (c_str_view (s));
26244 if (it != m_str_table.end ())
26245 return it->second;
26246 const size_t offset = (dwarf2_per_objfile->str.size
26247 + m_str_add_buf.size ());
26248 m_str_table.emplace (c_str_view (s), offset);
26249 m_str_add_buf.append_cstr0 (s);
26250 return offset;
26251 }
26252
26253 /* Append the end of the .debug_str section to FILE. */
26254 void file_write (FILE *file) const
26255 {
26256 m_str_add_buf.file_write (file);
26257 }
26258
26259 private:
26260 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26261 bfd *const m_abfd;
26262
26263 /* Data to add at the end of .debug_str for new needed symbol names. */
26264 data_buf m_str_add_buf;
26265 };
26266
26267 /* Container to map used DWARF tags to their .debug_names abbreviation
26268 tags. */
26269 class index_key
26270 {
26271 public:
26272 index_key (int dwarf_tag_, bool is_static_)
26273 : dwarf_tag (dwarf_tag_), is_static (is_static_)
26274 {
26275 }
26276
26277 bool
26278 operator== (const index_key &other) const
26279 {
26280 return dwarf_tag == other.dwarf_tag && is_static == other.is_static;
26281 }
26282
26283 const int dwarf_tag;
26284 const bool is_static;
26285 };
26286
26287 /* Provide std::unordered_map::hasher for index_key. */
26288 class index_key_hasher
26289 {
26290 public:
26291 size_t
26292 operator () (const index_key &key) const
26293 {
26294 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26295 }
26296 };
26297
26298 /* Parameters of one symbol entry. */
26299 class symbol_value
26300 {
26301 public:
26302 const int dwarf_tag, cu_index;
26303 const bool is_static;
26304
26305 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_)
26306 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_)
26307 {}
26308
26309 bool
26310 operator< (const symbol_value &other) const
26311 {
26312 #define X(n) \
26313 do \
26314 { \
26315 if (n < other.n) \
26316 return true; \
26317 if (n > other.n) \
26318 return false; \
26319 } \
26320 while (0)
26321 X (dwarf_tag);
26322 X (is_static);
26323 X (cu_index);
26324 #undef X
26325 return false;
26326 }
26327 };
26328
26329 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26330 output. */
26331 class offset_vec
26332 {
26333 protected:
26334 const bfd_endian dwarf5_byte_order;
26335 public:
26336 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26337 : dwarf5_byte_order (dwarf5_byte_order_)
26338 {}
26339
26340 /* Call std::vector::reserve for NELEM elements. */
26341 virtual void reserve (size_t nelem) = 0;
26342
26343 /* Call std::vector::push_back with store_unsigned_integer byte
26344 reordering for ELEM. */
26345 virtual void push_back_reorder (size_t elem) = 0;
26346
26347 /* Return expected output size in bytes. */
26348 virtual size_t bytes () const = 0;
26349
26350 /* Write name table to FILE. */
26351 virtual void file_write (FILE *file) const = 0;
26352 };
26353
26354 /* Template to unify DWARF-32 and DWARF-64 output. */
26355 template<typename OffsetSize>
26356 class offset_vec_tmpl : public offset_vec
26357 {
26358 public:
26359 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26360 : offset_vec (dwarf5_byte_order_)
26361 {}
26362
26363 /* Implement offset_vec::reserve. */
26364 void reserve (size_t nelem) override
26365 {
26366 m_vec.reserve (nelem);
26367 }
26368
26369 /* Implement offset_vec::push_back_reorder. */
26370 void push_back_reorder (size_t elem) override
26371 {
26372 m_vec.push_back (elem);
26373 /* Check for overflow. */
26374 gdb_assert (m_vec.back () == elem);
26375 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26376 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26377 }
26378
26379 /* Implement offset_vec::bytes. */
26380 size_t bytes () const override
26381 {
26382 return m_vec.size () * sizeof (m_vec[0]);
26383 }
26384
26385 /* Implement offset_vec::file_write. */
26386 void file_write (FILE *file) const override
26387 {
26388 ::file_write (file, m_vec);
26389 }
26390
26391 private:
26392 std::vector<OffsetSize> m_vec;
26393 };
26394
26395 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26396 respecting name table width. */
26397 class dwarf
26398 {
26399 public:
26400 offset_vec &name_table_string_offs, &name_table_entry_offs;
26401
26402 dwarf (offset_vec &name_table_string_offs_,
26403 offset_vec &name_table_entry_offs_)
26404 : name_table_string_offs (name_table_string_offs_),
26405 name_table_entry_offs (name_table_entry_offs_)
26406 {
26407 }
26408 };
26409
26410 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26411 respecting name table width. */
26412 template<typename OffsetSize>
26413 class dwarf_tmpl : public dwarf
26414 {
26415 public:
26416 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26417 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26418 m_name_table_string_offs (dwarf5_byte_order_),
26419 m_name_table_entry_offs (dwarf5_byte_order_)
26420 {}
26421
26422 private:
26423 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26424 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26425 };
26426
26427 /* Try to reconstruct original DWARF tag for given partial_symbol.
26428 This function is not DWARF-5 compliant but it is sufficient for
26429 GDB as a DWARF-5 index consumer. */
26430 static int psymbol_tag (const struct partial_symbol *psym)
26431 {
26432 domain_enum domain = PSYMBOL_DOMAIN (psym);
26433 enum address_class aclass = PSYMBOL_CLASS (psym);
26434
26435 switch (domain)
26436 {
26437 case VAR_DOMAIN:
26438 switch (aclass)
26439 {
26440 case LOC_BLOCK:
26441 return DW_TAG_subprogram;
26442 case LOC_TYPEDEF:
26443 return DW_TAG_typedef;
26444 case LOC_COMPUTED:
26445 case LOC_CONST_BYTES:
26446 case LOC_OPTIMIZED_OUT:
26447 case LOC_STATIC:
26448 return DW_TAG_variable;
26449 case LOC_CONST:
26450 /* Note: It's currently impossible to recognize psyms as enum values
26451 short of reading the type info. For now punt. */
26452 return DW_TAG_variable;
26453 default:
26454 /* There are other LOC_FOO values that one might want to classify
26455 as variables, but dwarf2read.c doesn't currently use them. */
26456 return DW_TAG_variable;
26457 }
26458 case STRUCT_DOMAIN:
26459 return DW_TAG_structure_type;
26460 default:
26461 return 0;
26462 }
26463 }
26464
26465 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26466 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
26467 struct partial_symbol **psymp, int count, int cu_index,
26468 bool is_static)
26469 {
26470 for (; count-- > 0; ++psymp)
26471 {
26472 struct partial_symbol *psym = *psymp;
26473
26474 if (SYMBOL_LANGUAGE (psym) == language_ada)
26475 error (_("Ada is not currently supported by the index"));
26476
26477 /* Only add a given psymbol once. */
26478 if (psyms_seen.insert (psym).second)
26479 insert (psym, cu_index, is_static);
26480 }
26481 }
26482
26483 /* Store value of each symbol. */
26484 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
26485 m_name_to_value_set;
26486
26487 /* Tables of DWARF-5 .debug_names. They are in object file byte
26488 order. */
26489 std::vector<uint32_t> m_bucket_table;
26490 std::vector<uint32_t> m_hash_table;
26491
26492 const bfd_endian m_dwarf5_byte_order;
26493 dwarf_tmpl<uint32_t> m_dwarf32;
26494 dwarf_tmpl<uint64_t> m_dwarf64;
26495 dwarf &m_dwarf;
26496 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
26497 debug_str_lookup m_debugstrlookup;
26498
26499 /* Map each used .debug_names abbreviation tag parameter to its
26500 index value. */
26501 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
26502
26503 /* Next unused .debug_names abbreviation tag for
26504 m_indexkey_to_idx. */
26505 int m_idx_next = 1;
26506
26507 /* .debug_names abbreviation table. */
26508 data_buf m_abbrev_table;
26509
26510 /* .debug_names entry pool. */
26511 data_buf m_entry_pool;
26512 };
26513
26514 /* Return iff any of the needed offsets does not fit into 32-bit
26515 .debug_names section. */
26516
26517 static bool
26518 check_dwarf64_offsets ()
26519 {
26520 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26521 {
26522 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
26523
26524 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26525 return true;
26526 }
26527 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
26528 {
26529 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
26530 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
26531
26532 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26533 return true;
26534 }
26535 return false;
26536 }
26537
26538 /* The psyms_seen set is potentially going to be largish (~40k
26539 elements when indexing a -g3 build of GDB itself). Estimate the
26540 number of elements in order to avoid too many rehashes, which
26541 require rebuilding buckets and thus many trips to
26542 malloc/free. */
26543
26544 static size_t
26545 psyms_seen_size ()
26546 {
26547 size_t psyms_count = 0;
26548 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26549 {
26550 struct dwarf2_per_cu_data *per_cu
26551 = dwarf2_per_objfile->all_comp_units[i];
26552 struct partial_symtab *psymtab = per_cu->v.psymtab;
26553
26554 if (psymtab != NULL && psymtab->user == NULL)
26555 recursively_count_psymbols (psymtab, psyms_count);
26556 }
26557 /* Generating an index for gdb itself shows a ratio of
26558 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
26559 return psyms_count / 4;
26560 }
26561
26562 /* Write new .gdb_index section for OBJFILE into OUT_FILE.
26563 Return how many bytes were expected to be written into OUT_FILE. */
26564
26565 static size_t
26566 write_gdbindex (struct objfile *objfile, FILE *out_file)
26567 {
26568 mapped_symtab symtab;
26569 data_buf cu_list;
26570
26571 /* While we're scanning CU's create a table that maps a psymtab pointer
26572 (which is what addrmap records) to its index (which is what is recorded
26573 in the index file). This will later be needed to write the address
26574 table. */
26575 psym_index_map cu_index_htab;
26576 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
26577
26578 /* The CU list is already sorted, so we don't need to do additional
26579 work here. Also, the debug_types entries do not appear in
26580 all_comp_units, but only in their own hash table. */
26581
26582 std::unordered_set<partial_symbol *> psyms_seen (psyms_seen_size ());
26583 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26584 {
26585 struct dwarf2_per_cu_data *per_cu
26586 = dwarf2_per_objfile->all_comp_units[i];
26587 struct partial_symtab *psymtab = per_cu->v.psymtab;
26588
26589 /* CU of a shared file from 'dwz -m' may be unused by this main file.
26590 It may be referenced from a local scope but in such case it does not
26591 need to be present in .gdb_index. */
26592 if (psymtab == NULL)
26593 continue;
26594
26595 if (psymtab->user == NULL)
26596 recursively_write_psymbols (objfile, psymtab, &symtab,
26597 psyms_seen, i);
26598
26599 const auto insertpair = cu_index_htab.emplace (psymtab, i);
26600 gdb_assert (insertpair.second);
26601
26602 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
26603 to_underlying (per_cu->sect_off));
26604 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
26605 }
26606
26607 /* Dump the address map. */
26608 data_buf addr_vec;
26609 write_address_map (objfile, addr_vec, cu_index_htab);
26610
26611 /* Write out the .debug_type entries, if any. */
26612 data_buf types_cu_list;
26613 if (dwarf2_per_objfile->signatured_types)
26614 {
26615 signatured_type_index_data sig_data (types_cu_list,
26616 psyms_seen);
26617
26618 sig_data.objfile = objfile;
26619 sig_data.symtab = &symtab;
26620 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
26621 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26622 write_one_signatured_type, &sig_data);
26623 }
26624
26625 /* Now that we've processed all symbols we can shrink their cu_indices
26626 lists. */
26627 uniquify_cu_indices (&symtab);
26628
26629 data_buf symtab_vec, constant_pool;
26630 write_hash_table (&symtab, symtab_vec, constant_pool);
26631
26632 data_buf contents;
26633 const offset_type size_of_contents = 6 * sizeof (offset_type);
26634 offset_type total_len = size_of_contents;
26635
26636 /* The version number. */
26637 contents.append_data (MAYBE_SWAP (8));
26638
26639 /* The offset of the CU list from the start of the file. */
26640 contents.append_data (MAYBE_SWAP (total_len));
26641 total_len += cu_list.size ();
26642
26643 /* The offset of the types CU list from the start of the file. */
26644 contents.append_data (MAYBE_SWAP (total_len));
26645 total_len += types_cu_list.size ();
26646
26647 /* The offset of the address table from the start of the file. */
26648 contents.append_data (MAYBE_SWAP (total_len));
26649 total_len += addr_vec.size ();
26650
26651 /* The offset of the symbol table from the start of the file. */
26652 contents.append_data (MAYBE_SWAP (total_len));
26653 total_len += symtab_vec.size ();
26654
26655 /* The offset of the constant pool from the start of the file. */
26656 contents.append_data (MAYBE_SWAP (total_len));
26657 total_len += constant_pool.size ();
26658
26659 gdb_assert (contents.size () == size_of_contents);
26660
26661 contents.file_write (out_file);
26662 cu_list.file_write (out_file);
26663 types_cu_list.file_write (out_file);
26664 addr_vec.file_write (out_file);
26665 symtab_vec.file_write (out_file);
26666 constant_pool.file_write (out_file);
26667
26668 return total_len;
26669 }
26670
26671 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
26672 static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
26673
26674 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
26675 needed addition to .debug_str section to OUT_FILE_STR. Return how
26676 many bytes were expected to be written into OUT_FILE. */
26677
26678 static size_t
26679 write_debug_names (struct objfile *objfile, FILE *out_file, FILE *out_file_str)
26680 {
26681 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets ();
26682 const int dwarf5_offset_size = dwarf5_is_dwarf64 ? 8 : 4;
26683 const enum bfd_endian dwarf5_byte_order
26684 = gdbarch_byte_order (get_objfile_arch (objfile));
26685
26686 /* The CU list is already sorted, so we don't need to do additional
26687 work here. Also, the debug_types entries do not appear in
26688 all_comp_units, but only in their own hash table. */
26689 data_buf cu_list;
26690 debug_names nametable (dwarf5_is_dwarf64, dwarf5_byte_order);
26691 std::unordered_set<partial_symbol *> psyms_seen (psyms_seen_size ());
26692 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26693 {
26694 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
26695 partial_symtab *psymtab = per_cu->v.psymtab;
26696
26697 /* CU of a shared file from 'dwz -m' may be unused by this main
26698 file. It may be referenced from a local scope but in such
26699 case it does not need to be present in .debug_names. */
26700 if (psymtab == NULL)
26701 continue;
26702
26703 if (psymtab->user == NULL)
26704 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
26705
26706 cu_list.append_uint (dwarf5_offset_size, dwarf5_byte_order,
26707 to_underlying (per_cu->sect_off));
26708 }
26709 nametable.build ();
26710
26711 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
26712
26713 data_buf types_cu_list;
26714 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
26715 {
26716 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
26717 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
26718
26719 types_cu_list.append_uint (dwarf5_offset_size, dwarf5_byte_order,
26720 to_underlying (per_cu.sect_off));
26721 }
26722
26723 const offset_type bytes_of_header
26724 = ((dwarf5_is_dwarf64 ? 12 : 4)
26725 + 2 + 2 + 7 * 4
26726 + sizeof (dwarf5_gdb_augmentation));
26727 size_t expected_bytes = 0;
26728 expected_bytes += bytes_of_header;
26729 expected_bytes += cu_list.size ();
26730 expected_bytes += types_cu_list.size ();
26731 expected_bytes += nametable.bytes ();
26732 data_buf header;
26733
26734 if (!dwarf5_is_dwarf64)
26735 {
26736 const uint64_t size64 = expected_bytes - 4;
26737 gdb_assert (size64 < 0xfffffff0);
26738 header.append_uint (4, dwarf5_byte_order, size64);
26739 }
26740 else
26741 {
26742 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
26743 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
26744 }
26745
26746 /* The version number. */
26747 header.append_uint (2, dwarf5_byte_order, 5);
26748
26749 /* Padding. */
26750 header.append_uint (2, dwarf5_byte_order, 0);
26751
26752 /* comp_unit_count - The number of CUs in the CU list. */
26753 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
26754
26755 /* local_type_unit_count - The number of TUs in the local TU
26756 list. */
26757 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
26758
26759 /* foreign_type_unit_count - The number of TUs in the foreign TU
26760 list. */
26761 header.append_uint (4, dwarf5_byte_order, 0);
26762
26763 /* bucket_count - The number of hash buckets in the hash lookup
26764 table. */
26765 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
26766
26767 /* name_count - The number of unique names in the index. */
26768 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
26769
26770 /* abbrev_table_size - The size in bytes of the abbreviations
26771 table. */
26772 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
26773
26774 /* augmentation_string_size - The size in bytes of the augmentation
26775 string. This value is rounded up to a multiple of 4. */
26776 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
26777 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
26778 header.append_data (dwarf5_gdb_augmentation);
26779
26780 gdb_assert (header.size () == bytes_of_header);
26781
26782 header.file_write (out_file);
26783 cu_list.file_write (out_file);
26784 types_cu_list.file_write (out_file);
26785 nametable.file_write (out_file, out_file_str);
26786
26787 return expected_bytes;
26788 }
26789
26790 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
26791 position is at the end of the file. */
26792
26793 static void
26794 assert_file_size (FILE *file, const char *filename, size_t expected_size)
26795 {
26796 const auto file_size = ftell (file);
26797 if (file_size == -1)
26798 error (_("Can't get `%s' size"), filename);
26799 gdb_assert (file_size == expected_size);
26800 }
26801
26802 /* An index variant. */
26803 enum dw_index_kind
26804 {
26805 /* GDB's own .gdb_index format. */
26806 GDB_INDEX,
26807
26808 /* DWARF5 .debug_names. */
26809 DEBUG_NAMES,
26810 };
26811
26812 /* Create an index file for OBJFILE in the directory DIR. */
26813
26814 static void
26815 write_psymtabs_to_index (struct objfile *objfile, const char *dir,
26816 dw_index_kind index_kind)
26817 {
26818 if (dwarf2_per_objfile->using_index)
26819 error (_("Cannot use an index to create the index"));
26820
26821 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
26822 error (_("Cannot make an index when the file has multiple .debug_types sections"));
26823
26824 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
26825 return;
26826
26827 struct stat st;
26828 if (stat (objfile_name (objfile), &st) < 0)
26829 perror_with_name (objfile_name (objfile));
26830
26831 std::string filename (std::string (dir) + SLASH_STRING
26832 + lbasename (objfile_name (objfile))
26833 + (index_kind == dw_index_kind::DEBUG_NAMES
26834 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
26835
26836 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
26837 if (!out_file)
26838 error (_("Can't open `%s' for writing"), filename.c_str ());
26839
26840 /* Order matters here; we want FILE to be closed before FILENAME is
26841 unlinked, because on MS-Windows one cannot delete a file that is
26842 still open. (Don't call anything here that might throw until
26843 file_closer is created.) */
26844 gdb::unlinker unlink_file (filename.c_str ());
26845 gdb_file_up close_out_file (out_file);
26846
26847 if (index_kind == dw_index_kind::DEBUG_NAMES)
26848 {
26849 std::string filename_str (std::string (dir) + SLASH_STRING
26850 + lbasename (objfile_name (objfile))
26851 + DEBUG_STR_SUFFIX);
26852 FILE *out_file_str
26853 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
26854 if (!out_file_str)
26855 error (_("Can't open `%s' for writing"), filename_str.c_str ());
26856 gdb::unlinker unlink_file_str (filename_str.c_str ());
26857 gdb_file_up close_out_file_str (out_file_str);
26858
26859 const size_t total_len
26860 = write_debug_names (objfile, out_file, out_file_str);
26861 assert_file_size (out_file, filename.c_str (), total_len);
26862
26863 /* We want to keep the file .debug_str file too. */
26864 unlink_file_str.keep ();
26865 }
26866 else
26867 {
26868 const size_t total_len
26869 = write_gdbindex (objfile, out_file);
26870 assert_file_size (out_file, filename.c_str (), total_len);
26871 }
26872
26873 /* We want to keep the file. */
26874 unlink_file.keep ();
26875 }
26876
26877 /* Implementation of the `save gdb-index' command.
26878
26879 Note that the .gdb_index file format used by this command is
26880 documented in the GDB manual. Any changes here must be documented
26881 there. */
26882
26883 static void
26884 save_gdb_index_command (const char *arg, int from_tty)
26885 {
26886 struct objfile *objfile;
26887 const char dwarf5space[] = "-dwarf-5 ";
26888 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
26889
26890 if (!arg)
26891 arg = "";
26892
26893 arg = skip_spaces (arg);
26894 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
26895 {
26896 index_kind = dw_index_kind::DEBUG_NAMES;
26897 arg += strlen (dwarf5space);
26898 arg = skip_spaces (arg);
26899 }
26900
26901 if (!*arg)
26902 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
26903
26904 ALL_OBJFILES (objfile)
26905 {
26906 struct stat st;
26907
26908 /* If the objfile does not correspond to an actual file, skip it. */
26909 if (stat (objfile_name (objfile), &st) < 0)
26910 continue;
26911
26912 dwarf2_per_objfile
26913 = (struct dwarf2_per_objfile *) objfile_data (objfile,
26914 dwarf2_objfile_data_key);
26915 if (dwarf2_per_objfile)
26916 {
26917
26918 TRY
26919 {
26920 write_psymtabs_to_index (objfile, arg, index_kind);
26921 }
26922 CATCH (except, RETURN_MASK_ERROR)
26923 {
26924 exception_fprintf (gdb_stderr, except,
26925 _("Error while writing index for `%s': "),
26926 objfile_name (objfile));
26927 }
26928 END_CATCH
26929 }
26930 }
26931 }
26932
26933 \f
26934
26935 int dwarf_always_disassemble;
26936
26937 static void
26938 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26939 struct cmd_list_element *c, const char *value)
26940 {
26941 fprintf_filtered (file,
26942 _("Whether to always disassemble "
26943 "DWARF expressions is %s.\n"),
26944 value);
26945 }
26946
26947 static void
26948 show_check_physname (struct ui_file *file, int from_tty,
26949 struct cmd_list_element *c, const char *value)
26950 {
26951 fprintf_filtered (file,
26952 _("Whether to check \"physname\" is %s.\n"),
26953 value);
26954 }
26955
26956 void
26957 _initialize_dwarf2_read (void)
26958 {
26959 struct cmd_list_element *c;
26960
26961 dwarf2_objfile_data_key
26962 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
26963
26964 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26965 Set DWARF specific variables.\n\
26966 Configure DWARF variables such as the cache size"),
26967 &set_dwarf_cmdlist, "maintenance set dwarf ",
26968 0/*allow-unknown*/, &maintenance_set_cmdlist);
26969
26970 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
26971 Show DWARF specific variables\n\
26972 Show DWARF variables such as the cache size"),
26973 &show_dwarf_cmdlist, "maintenance show dwarf ",
26974 0/*allow-unknown*/, &maintenance_show_cmdlist);
26975
26976 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26977 &dwarf_max_cache_age, _("\
26978 Set the upper bound on the age of cached DWARF compilation units."), _("\
26979 Show the upper bound on the age of cached DWARF compilation units."), _("\
26980 A higher limit means that cached compilation units will be stored\n\
26981 in memory longer, and more total memory will be used. Zero disables\n\
26982 caching, which can slow down startup."),
26983 NULL,
26984 show_dwarf_max_cache_age,
26985 &set_dwarf_cmdlist,
26986 &show_dwarf_cmdlist);
26987
26988 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26989 &dwarf_always_disassemble, _("\
26990 Set whether `info address' always disassembles DWARF expressions."), _("\
26991 Show whether `info address' always disassembles DWARF expressions."), _("\
26992 When enabled, DWARF expressions are always printed in an assembly-like\n\
26993 syntax. When disabled, expressions will be printed in a more\n\
26994 conversational style, when possible."),
26995 NULL,
26996 show_dwarf_always_disassemble,
26997 &set_dwarf_cmdlist,
26998 &show_dwarf_cmdlist);
26999
27000 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27001 Set debugging of the DWARF reader."), _("\
27002 Show debugging of the DWARF reader."), _("\
27003 When enabled (non-zero), debugging messages are printed during DWARF\n\
27004 reading and symtab expansion. A value of 1 (one) provides basic\n\
27005 information. A value greater than 1 provides more verbose information."),
27006 NULL,
27007 NULL,
27008 &setdebuglist, &showdebuglist);
27009
27010 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27011 Set debugging of the DWARF DIE reader."), _("\
27012 Show debugging of the DWARF DIE reader."), _("\
27013 When enabled (non-zero), DIEs are dumped after they are read in.\n\
27014 The value is the maximum depth to print."),
27015 NULL,
27016 NULL,
27017 &setdebuglist, &showdebuglist);
27018
27019 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27020 Set debugging of the dwarf line reader."), _("\
27021 Show debugging of the dwarf line reader."), _("\
27022 When enabled (non-zero), line number entries are dumped as they are read in.\n\
27023 A value of 1 (one) provides basic information.\n\
27024 A value greater than 1 provides more verbose information."),
27025 NULL,
27026 NULL,
27027 &setdebuglist, &showdebuglist);
27028
27029 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27030 Set cross-checking of \"physname\" code against demangler."), _("\
27031 Show cross-checking of \"physname\" code against demangler."), _("\
27032 When enabled, GDB's internal \"physname\" code is checked against\n\
27033 the demangler."),
27034 NULL, show_check_physname,
27035 &setdebuglist, &showdebuglist);
27036
27037 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27038 no_class, &use_deprecated_index_sections, _("\
27039 Set whether to use deprecated gdb_index sections."), _("\
27040 Show whether to use deprecated gdb_index sections."), _("\
27041 When enabled, deprecated .gdb_index sections are used anyway.\n\
27042 Normally they are ignored either because of a missing feature or\n\
27043 performance issue.\n\
27044 Warning: This option must be enabled before gdb reads the file."),
27045 NULL,
27046 NULL,
27047 &setlist, &showlist);
27048
27049 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
27050 _("\
27051 Save a gdb-index file.\n\
27052 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27053 \n\
27054 No options create one file with .gdb-index extension for pre-DWARF-5\n\
27055 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27056 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
27057 &save_cmdlist);
27058 set_cmd_completer (c, filename_completer);
27059
27060 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27061 &dwarf2_locexpr_funcs);
27062 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27063 &dwarf2_loclist_funcs);
27064
27065 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27066 &dwarf2_block_frame_base_locexpr_funcs);
27067 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27068 &dwarf2_block_frame_base_loclist_funcs);
27069
27070 #if GDB_SELF_TEST
27071 selftests::register_test ("dw2_expand_symtabs_matching",
27072 selftests::dw2_expand_symtabs_matching::run_test);
27073 #endif
27074 }