Make abbrev_table::abbrevs private
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2018 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "bfd.h"
33 #include "elf-bfd.h"
34 #include "symtab.h"
35 #include "gdbtypes.h"
36 #include "objfiles.h"
37 #include "dwarf2.h"
38 #include "buildsym.h"
39 #include "demangle.h"
40 #include "gdb-demangle.h"
41 #include "expression.h"
42 #include "filenames.h" /* for DOSish file names */
43 #include "macrotab.h"
44 #include "language.h"
45 #include "complaints.h"
46 #include "bcache.h"
47 #include "dwarf2expr.h"
48 #include "dwarf2loc.h"
49 #include "cp-support.h"
50 #include "hashtab.h"
51 #include "command.h"
52 #include "gdbcmd.h"
53 #include "block.h"
54 #include "addrmap.h"
55 #include "typeprint.h"
56 #include "psympriv.h"
57 #include <sys/stat.h>
58 #include "completer.h"
59 #include "vec.h"
60 #include "c-lang.h"
61 #include "go-lang.h"
62 #include "valprint.h"
63 #include "gdbcore.h" /* for gnutarget */
64 #include "gdb/gdb-index.h"
65 #include <ctype.h>
66 #include "gdb_bfd.h"
67 #include "f-lang.h"
68 #include "source.h"
69 #include "filestuff.h"
70 #include "build-id.h"
71 #include "namespace.h"
72 #include "common/gdb_unlinker.h"
73 #include "common/function-view.h"
74 #include "common/gdb_optional.h"
75 #include "common/underlying.h"
76 #include "common/byte-vector.h"
77 #include "common/hash_enum.h"
78 #include "filename-seen-cache.h"
79 #include "producer.h"
80 #include <fcntl.h>
81 #include <sys/types.h>
82 #include <algorithm>
83 #include <unordered_set>
84 #include <unordered_map>
85 #include "selftest.h"
86 #include <cmath>
87 #include <set>
88 #include <forward_list>
89
90 /* When == 1, print basic high level tracing messages.
91 When > 1, be more verbose.
92 This is in contrast to the low level DIE reading of dwarf_die_debug. */
93 static unsigned int dwarf_read_debug = 0;
94
95 /* When non-zero, dump DIEs after they are read in. */
96 static unsigned int dwarf_die_debug = 0;
97
98 /* When non-zero, dump line number entries as they are read in. */
99 static unsigned int dwarf_line_debug = 0;
100
101 /* When non-zero, cross-check physname against demangler. */
102 static int check_physname = 0;
103
104 /* When non-zero, do not reject deprecated .gdb_index sections. */
105 static int use_deprecated_index_sections = 0;
106
107 static const struct objfile_data *dwarf2_objfile_data_key;
108
109 /* The "aclass" indices for various kinds of computed DWARF symbols. */
110
111 static int dwarf2_locexpr_index;
112 static int dwarf2_loclist_index;
113 static int dwarf2_locexpr_block_index;
114 static int dwarf2_loclist_block_index;
115
116 /* A descriptor for dwarf sections.
117
118 S.ASECTION, SIZE are typically initialized when the objfile is first
119 scanned. BUFFER, READIN are filled in later when the section is read.
120 If the section contained compressed data then SIZE is updated to record
121 the uncompressed size of the section.
122
123 DWP file format V2 introduces a wrinkle that is easiest to handle by
124 creating the concept of virtual sections contained within a real section.
125 In DWP V2 the sections of the input DWO files are concatenated together
126 into one section, but section offsets are kept relative to the original
127 input section.
128 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
129 the real section this "virtual" section is contained in, and BUFFER,SIZE
130 describe the virtual section. */
131
132 struct dwarf2_section_info
133 {
134 union
135 {
136 /* If this is a real section, the bfd section. */
137 asection *section;
138 /* If this is a virtual section, pointer to the containing ("real")
139 section. */
140 struct dwarf2_section_info *containing_section;
141 } s;
142 /* Pointer to section data, only valid if readin. */
143 const gdb_byte *buffer;
144 /* The size of the section, real or virtual. */
145 bfd_size_type size;
146 /* If this is a virtual section, the offset in the real section.
147 Only valid if is_virtual. */
148 bfd_size_type virtual_offset;
149 /* True if we have tried to read this section. */
150 char readin;
151 /* True if this is a virtual section, False otherwise.
152 This specifies which of s.section and s.containing_section to use. */
153 char is_virtual;
154 };
155
156 typedef struct dwarf2_section_info dwarf2_section_info_def;
157 DEF_VEC_O (dwarf2_section_info_def);
158
159 /* All offsets in the index are of this type. It must be
160 architecture-independent. */
161 typedef uint32_t offset_type;
162
163 DEF_VEC_I (offset_type);
164
165 /* Ensure only legit values are used. */
166 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
167 do { \
168 gdb_assert ((unsigned int) (value) <= 1); \
169 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
170 } while (0)
171
172 /* Ensure only legit values are used. */
173 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
174 do { \
175 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
176 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
177 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
178 } while (0)
179
180 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
181 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
182 do { \
183 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
184 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
185 } while (0)
186
187 #if WORDS_BIGENDIAN
188
189 /* Convert VALUE between big- and little-endian. */
190
191 static offset_type
192 byte_swap (offset_type value)
193 {
194 offset_type result;
195
196 result = (value & 0xff) << 24;
197 result |= (value & 0xff00) << 8;
198 result |= (value & 0xff0000) >> 8;
199 result |= (value & 0xff000000) >> 24;
200 return result;
201 }
202
203 #define MAYBE_SWAP(V) byte_swap (V)
204
205 #else
206 #define MAYBE_SWAP(V) static_cast<offset_type> (V)
207 #endif /* WORDS_BIGENDIAN */
208
209 /* An index into a (C++) symbol name component in a symbol name as
210 recorded in the mapped_index's symbol table. For each C++ symbol
211 in the symbol table, we record one entry for the start of each
212 component in the symbol in a table of name components, and then
213 sort the table, in order to be able to binary search symbol names,
214 ignoring leading namespaces, both completion and regular look up.
215 For example, for symbol "A::B::C", we'll have an entry that points
216 to "A::B::C", another that points to "B::C", and another for "C".
217 Note that function symbols in GDB index have no parameter
218 information, just the function/method names. You can convert a
219 name_component to a "const char *" using the
220 'mapped_index::symbol_name_at(offset_type)' method. */
221
222 struct name_component
223 {
224 /* Offset in the symbol name where the component starts. Stored as
225 a (32-bit) offset instead of a pointer to save memory and improve
226 locality on 64-bit architectures. */
227 offset_type name_offset;
228
229 /* The symbol's index in the symbol and constant pool tables of a
230 mapped_index. */
231 offset_type idx;
232 };
233
234 /* Base class containing bits shared by both .gdb_index and
235 .debug_name indexes. */
236
237 struct mapped_index_base
238 {
239 /* The name_component table (a sorted vector). See name_component's
240 description above. */
241 std::vector<name_component> name_components;
242
243 /* How NAME_COMPONENTS is sorted. */
244 enum case_sensitivity name_components_casing;
245
246 /* Return the number of names in the symbol table. */
247 virtual size_t symbol_name_count () const = 0;
248
249 /* Get the name of the symbol at IDX in the symbol table. */
250 virtual const char *symbol_name_at (offset_type idx) const = 0;
251
252 /* Return whether the name at IDX in the symbol table should be
253 ignored. */
254 virtual bool symbol_name_slot_invalid (offset_type idx) const
255 {
256 return false;
257 }
258
259 /* Build the symbol name component sorted vector, if we haven't
260 yet. */
261 void build_name_components ();
262
263 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
264 possible matches for LN_NO_PARAMS in the name component
265 vector. */
266 std::pair<std::vector<name_component>::const_iterator,
267 std::vector<name_component>::const_iterator>
268 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
269
270 /* Prevent deleting/destroying via a base class pointer. */
271 protected:
272 ~mapped_index_base() = default;
273 };
274
275 /* A description of the mapped index. The file format is described in
276 a comment by the code that writes the index. */
277 struct mapped_index final : public mapped_index_base
278 {
279 /* A slot/bucket in the symbol table hash. */
280 struct symbol_table_slot
281 {
282 const offset_type name;
283 const offset_type vec;
284 };
285
286 /* Index data format version. */
287 int version;
288
289 /* The total length of the buffer. */
290 off_t total_size;
291
292 /* The address table data. */
293 gdb::array_view<const gdb_byte> address_table;
294
295 /* The symbol table, implemented as a hash table. */
296 gdb::array_view<symbol_table_slot> symbol_table;
297
298 /* A pointer to the constant pool. */
299 const char *constant_pool;
300
301 bool symbol_name_slot_invalid (offset_type idx) const override
302 {
303 const auto &bucket = this->symbol_table[idx];
304 return bucket.name == 0 && bucket.vec;
305 }
306
307 /* Convenience method to get at the name of the symbol at IDX in the
308 symbol table. */
309 const char *symbol_name_at (offset_type idx) const override
310 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
311
312 size_t symbol_name_count () const override
313 { return this->symbol_table.size (); }
314 };
315
316 /* A description of the mapped .debug_names.
317 Uninitialized map has CU_COUNT 0. */
318 struct mapped_debug_names final : public mapped_index_base
319 {
320 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
321 : dwarf2_per_objfile (dwarf2_per_objfile_)
322 {}
323
324 struct dwarf2_per_objfile *dwarf2_per_objfile;
325 bfd_endian dwarf5_byte_order;
326 bool dwarf5_is_dwarf64;
327 bool augmentation_is_gdb;
328 uint8_t offset_size;
329 uint32_t cu_count = 0;
330 uint32_t tu_count, bucket_count, name_count;
331 const gdb_byte *cu_table_reordered, *tu_table_reordered;
332 const uint32_t *bucket_table_reordered, *hash_table_reordered;
333 const gdb_byte *name_table_string_offs_reordered;
334 const gdb_byte *name_table_entry_offs_reordered;
335 const gdb_byte *entry_pool;
336
337 struct index_val
338 {
339 ULONGEST dwarf_tag;
340 struct attr
341 {
342 /* Attribute name DW_IDX_*. */
343 ULONGEST dw_idx;
344
345 /* Attribute form DW_FORM_*. */
346 ULONGEST form;
347
348 /* Value if FORM is DW_FORM_implicit_const. */
349 LONGEST implicit_const;
350 };
351 std::vector<attr> attr_vec;
352 };
353
354 std::unordered_map<ULONGEST, index_val> abbrev_map;
355
356 const char *namei_to_name (uint32_t namei) const;
357
358 /* Implementation of the mapped_index_base virtual interface, for
359 the name_components cache. */
360
361 const char *symbol_name_at (offset_type idx) const override
362 { return namei_to_name (idx); }
363
364 size_t symbol_name_count () const override
365 { return this->name_count; }
366 };
367
368 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
369 DEF_VEC_P (dwarf2_per_cu_ptr);
370
371 struct tu_stats
372 {
373 int nr_uniq_abbrev_tables;
374 int nr_symtabs;
375 int nr_symtab_sharers;
376 int nr_stmt_less_type_units;
377 int nr_all_type_units_reallocs;
378 };
379
380 /* Collection of data recorded per objfile.
381 This hangs off of dwarf2_objfile_data_key. */
382
383 struct dwarf2_per_objfile
384 {
385 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
386 dwarf2 section names, or is NULL if the standard ELF names are
387 used. */
388 dwarf2_per_objfile (struct objfile *objfile,
389 const dwarf2_debug_sections *names);
390
391 ~dwarf2_per_objfile ();
392
393 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
394
395 /* Free all cached compilation units. */
396 void free_cached_comp_units ();
397 private:
398 /* This function is mapped across the sections and remembers the
399 offset and size of each of the debugging sections we are
400 interested in. */
401 void locate_sections (bfd *abfd, asection *sectp,
402 const dwarf2_debug_sections &names);
403
404 public:
405 dwarf2_section_info info {};
406 dwarf2_section_info abbrev {};
407 dwarf2_section_info line {};
408 dwarf2_section_info loc {};
409 dwarf2_section_info loclists {};
410 dwarf2_section_info macinfo {};
411 dwarf2_section_info macro {};
412 dwarf2_section_info str {};
413 dwarf2_section_info line_str {};
414 dwarf2_section_info ranges {};
415 dwarf2_section_info rnglists {};
416 dwarf2_section_info addr {};
417 dwarf2_section_info frame {};
418 dwarf2_section_info eh_frame {};
419 dwarf2_section_info gdb_index {};
420 dwarf2_section_info debug_names {};
421 dwarf2_section_info debug_aranges {};
422
423 VEC (dwarf2_section_info_def) *types = NULL;
424
425 /* Back link. */
426 struct objfile *objfile = NULL;
427
428 /* Table of all the compilation units. This is used to locate
429 the target compilation unit of a particular reference. */
430 struct dwarf2_per_cu_data **all_comp_units = NULL;
431
432 /* The number of compilation units in ALL_COMP_UNITS. */
433 int n_comp_units = 0;
434
435 /* The number of .debug_types-related CUs. */
436 int n_type_units = 0;
437
438 /* The number of elements allocated in all_type_units.
439 If there are skeleton-less TUs, we add them to all_type_units lazily. */
440 int n_allocated_type_units = 0;
441
442 /* The .debug_types-related CUs (TUs).
443 This is stored in malloc space because we may realloc it. */
444 struct signatured_type **all_type_units = NULL;
445
446 /* Table of struct type_unit_group objects.
447 The hash key is the DW_AT_stmt_list value. */
448 htab_t type_unit_groups {};
449
450 /* A table mapping .debug_types signatures to its signatured_type entry.
451 This is NULL if the .debug_types section hasn't been read in yet. */
452 htab_t signatured_types {};
453
454 /* Type unit statistics, to see how well the scaling improvements
455 are doing. */
456 struct tu_stats tu_stats {};
457
458 /* A chain of compilation units that are currently read in, so that
459 they can be freed later. */
460 dwarf2_per_cu_data *read_in_chain = NULL;
461
462 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
463 This is NULL if the table hasn't been allocated yet. */
464 htab_t dwo_files {};
465
466 /* True if we've checked for whether there is a DWP file. */
467 bool dwp_checked = false;
468
469 /* The DWP file if there is one, or NULL. */
470 struct dwp_file *dwp_file = NULL;
471
472 /* The shared '.dwz' file, if one exists. This is used when the
473 original data was compressed using 'dwz -m'. */
474 struct dwz_file *dwz_file = NULL;
475
476 /* A flag indicating whether this objfile has a section loaded at a
477 VMA of 0. */
478 bool has_section_at_zero = false;
479
480 /* True if we are using the mapped index,
481 or we are faking it for OBJF_READNOW's sake. */
482 bool using_index = false;
483
484 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
485 mapped_index *index_table = NULL;
486
487 /* The mapped index, or NULL if .debug_names is missing or not being used. */
488 std::unique_ptr<mapped_debug_names> debug_names_table;
489
490 /* When using index_table, this keeps track of all quick_file_names entries.
491 TUs typically share line table entries with a CU, so we maintain a
492 separate table of all line table entries to support the sharing.
493 Note that while there can be way more TUs than CUs, we've already
494 sorted all the TUs into "type unit groups", grouped by their
495 DW_AT_stmt_list value. Therefore the only sharing done here is with a
496 CU and its associated TU group if there is one. */
497 htab_t quick_file_names_table {};
498
499 /* Set during partial symbol reading, to prevent queueing of full
500 symbols. */
501 bool reading_partial_symbols = false;
502
503 /* Table mapping type DIEs to their struct type *.
504 This is NULL if not allocated yet.
505 The mapping is done via (CU/TU + DIE offset) -> type. */
506 htab_t die_type_hash {};
507
508 /* The CUs we recently read. */
509 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
510
511 /* Table containing line_header indexed by offset and offset_in_dwz. */
512 htab_t line_header_hash {};
513
514 /* Table containing all filenames. This is an optional because the
515 table is lazily constructed on first access. */
516 gdb::optional<filename_seen_cache> filenames_cache;
517 };
518
519 /* Get the dwarf2_per_objfile associated to OBJFILE. */
520
521 struct dwarf2_per_objfile *
522 get_dwarf2_per_objfile (struct objfile *objfile)
523 {
524 return ((struct dwarf2_per_objfile *)
525 objfile_data (objfile, dwarf2_objfile_data_key));
526 }
527
528 /* Set the dwarf2_per_objfile associated to OBJFILE. */
529
530 void
531 set_dwarf2_per_objfile (struct objfile *objfile,
532 struct dwarf2_per_objfile *dwarf2_per_objfile)
533 {
534 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
535 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
536 }
537
538 /* Default names of the debugging sections. */
539
540 /* Note that if the debugging section has been compressed, it might
541 have a name like .zdebug_info. */
542
543 static const struct dwarf2_debug_sections dwarf2_elf_names =
544 {
545 { ".debug_info", ".zdebug_info" },
546 { ".debug_abbrev", ".zdebug_abbrev" },
547 { ".debug_line", ".zdebug_line" },
548 { ".debug_loc", ".zdebug_loc" },
549 { ".debug_loclists", ".zdebug_loclists" },
550 { ".debug_macinfo", ".zdebug_macinfo" },
551 { ".debug_macro", ".zdebug_macro" },
552 { ".debug_str", ".zdebug_str" },
553 { ".debug_line_str", ".zdebug_line_str" },
554 { ".debug_ranges", ".zdebug_ranges" },
555 { ".debug_rnglists", ".zdebug_rnglists" },
556 { ".debug_types", ".zdebug_types" },
557 { ".debug_addr", ".zdebug_addr" },
558 { ".debug_frame", ".zdebug_frame" },
559 { ".eh_frame", NULL },
560 { ".gdb_index", ".zgdb_index" },
561 { ".debug_names", ".zdebug_names" },
562 { ".debug_aranges", ".zdebug_aranges" },
563 23
564 };
565
566 /* List of DWO/DWP sections. */
567
568 static const struct dwop_section_names
569 {
570 struct dwarf2_section_names abbrev_dwo;
571 struct dwarf2_section_names info_dwo;
572 struct dwarf2_section_names line_dwo;
573 struct dwarf2_section_names loc_dwo;
574 struct dwarf2_section_names loclists_dwo;
575 struct dwarf2_section_names macinfo_dwo;
576 struct dwarf2_section_names macro_dwo;
577 struct dwarf2_section_names str_dwo;
578 struct dwarf2_section_names str_offsets_dwo;
579 struct dwarf2_section_names types_dwo;
580 struct dwarf2_section_names cu_index;
581 struct dwarf2_section_names tu_index;
582 }
583 dwop_section_names =
584 {
585 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
586 { ".debug_info.dwo", ".zdebug_info.dwo" },
587 { ".debug_line.dwo", ".zdebug_line.dwo" },
588 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
589 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
590 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
591 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
592 { ".debug_str.dwo", ".zdebug_str.dwo" },
593 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
594 { ".debug_types.dwo", ".zdebug_types.dwo" },
595 { ".debug_cu_index", ".zdebug_cu_index" },
596 { ".debug_tu_index", ".zdebug_tu_index" },
597 };
598
599 /* local data types */
600
601 /* The data in a compilation unit header, after target2host
602 translation, looks like this. */
603 struct comp_unit_head
604 {
605 unsigned int length;
606 short version;
607 unsigned char addr_size;
608 unsigned char signed_addr_p;
609 sect_offset abbrev_sect_off;
610
611 /* Size of file offsets; either 4 or 8. */
612 unsigned int offset_size;
613
614 /* Size of the length field; either 4 or 12. */
615 unsigned int initial_length_size;
616
617 enum dwarf_unit_type unit_type;
618
619 /* Offset to the first byte of this compilation unit header in the
620 .debug_info section, for resolving relative reference dies. */
621 sect_offset sect_off;
622
623 /* Offset to first die in this cu from the start of the cu.
624 This will be the first byte following the compilation unit header. */
625 cu_offset first_die_cu_offset;
626
627 /* 64-bit signature of this type unit - it is valid only for
628 UNIT_TYPE DW_UT_type. */
629 ULONGEST signature;
630
631 /* For types, offset in the type's DIE of the type defined by this TU. */
632 cu_offset type_cu_offset_in_tu;
633 };
634
635 /* Type used for delaying computation of method physnames.
636 See comments for compute_delayed_physnames. */
637 struct delayed_method_info
638 {
639 /* The type to which the method is attached, i.e., its parent class. */
640 struct type *type;
641
642 /* The index of the method in the type's function fieldlists. */
643 int fnfield_index;
644
645 /* The index of the method in the fieldlist. */
646 int index;
647
648 /* The name of the DIE. */
649 const char *name;
650
651 /* The DIE associated with this method. */
652 struct die_info *die;
653 };
654
655 /* Internal state when decoding a particular compilation unit. */
656 struct dwarf2_cu
657 {
658 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
659 ~dwarf2_cu ();
660
661 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
662
663 /* The header of the compilation unit. */
664 struct comp_unit_head header {};
665
666 /* Base address of this compilation unit. */
667 CORE_ADDR base_address = 0;
668
669 /* Non-zero if base_address has been set. */
670 int base_known = 0;
671
672 /* The language we are debugging. */
673 enum language language = language_unknown;
674 const struct language_defn *language_defn = nullptr;
675
676 const char *producer = nullptr;
677
678 /* The generic symbol table building routines have separate lists for
679 file scope symbols and all all other scopes (local scopes). So
680 we need to select the right one to pass to add_symbol_to_list().
681 We do it by keeping a pointer to the correct list in list_in_scope.
682
683 FIXME: The original dwarf code just treated the file scope as the
684 first local scope, and all other local scopes as nested local
685 scopes, and worked fine. Check to see if we really need to
686 distinguish these in buildsym.c. */
687 struct pending **list_in_scope = nullptr;
688
689 /* Hash table holding all the loaded partial DIEs
690 with partial_die->offset.SECT_OFF as hash. */
691 htab_t partial_dies = nullptr;
692
693 /* Storage for things with the same lifetime as this read-in compilation
694 unit, including partial DIEs. */
695 auto_obstack comp_unit_obstack;
696
697 /* When multiple dwarf2_cu structures are living in memory, this field
698 chains them all together, so that they can be released efficiently.
699 We will probably also want a generation counter so that most-recently-used
700 compilation units are cached... */
701 struct dwarf2_per_cu_data *read_in_chain = nullptr;
702
703 /* Backlink to our per_cu entry. */
704 struct dwarf2_per_cu_data *per_cu;
705
706 /* How many compilation units ago was this CU last referenced? */
707 int last_used = 0;
708
709 /* A hash table of DIE cu_offset for following references with
710 die_info->offset.sect_off as hash. */
711 htab_t die_hash = nullptr;
712
713 /* Full DIEs if read in. */
714 struct die_info *dies = nullptr;
715
716 /* A set of pointers to dwarf2_per_cu_data objects for compilation
717 units referenced by this one. Only set during full symbol processing;
718 partial symbol tables do not have dependencies. */
719 htab_t dependencies = nullptr;
720
721 /* Header data from the line table, during full symbol processing. */
722 struct line_header *line_header = nullptr;
723 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
724 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
725 this is the DW_TAG_compile_unit die for this CU. We'll hold on
726 to the line header as long as this DIE is being processed. See
727 process_die_scope. */
728 die_info *line_header_die_owner = nullptr;
729
730 /* A list of methods which need to have physnames computed
731 after all type information has been read. */
732 std::vector<delayed_method_info> method_list;
733
734 /* To be copied to symtab->call_site_htab. */
735 htab_t call_site_htab = nullptr;
736
737 /* Non-NULL if this CU came from a DWO file.
738 There is an invariant here that is important to remember:
739 Except for attributes copied from the top level DIE in the "main"
740 (or "stub") file in preparation for reading the DWO file
741 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
742 Either there isn't a DWO file (in which case this is NULL and the point
743 is moot), or there is and either we're not going to read it (in which
744 case this is NULL) or there is and we are reading it (in which case this
745 is non-NULL). */
746 struct dwo_unit *dwo_unit = nullptr;
747
748 /* The DW_AT_addr_base attribute if present, zero otherwise
749 (zero is a valid value though).
750 Note this value comes from the Fission stub CU/TU's DIE. */
751 ULONGEST addr_base = 0;
752
753 /* The DW_AT_ranges_base attribute if present, zero otherwise
754 (zero is a valid value though).
755 Note this value comes from the Fission stub CU/TU's DIE.
756 Also note that the value is zero in the non-DWO case so this value can
757 be used without needing to know whether DWO files are in use or not.
758 N.B. This does not apply to DW_AT_ranges appearing in
759 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
760 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
761 DW_AT_ranges_base *would* have to be applied, and we'd have to care
762 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
763 ULONGEST ranges_base = 0;
764
765 /* Mark used when releasing cached dies. */
766 unsigned int mark : 1;
767
768 /* This CU references .debug_loc. See the symtab->locations_valid field.
769 This test is imperfect as there may exist optimized debug code not using
770 any location list and still facing inlining issues if handled as
771 unoptimized code. For a future better test see GCC PR other/32998. */
772 unsigned int has_loclist : 1;
773
774 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
775 if all the producer_is_* fields are valid. This information is cached
776 because profiling CU expansion showed excessive time spent in
777 producer_is_gxx_lt_4_6. */
778 unsigned int checked_producer : 1;
779 unsigned int producer_is_gxx_lt_4_6 : 1;
780 unsigned int producer_is_gcc_lt_4_3 : 1;
781 unsigned int producer_is_icc_lt_14 : 1;
782
783 /* When set, the file that we're processing is known to have
784 debugging info for C++ namespaces. GCC 3.3.x did not produce
785 this information, but later versions do. */
786
787 unsigned int processing_has_namespace_info : 1;
788 };
789
790 /* Persistent data held for a compilation unit, even when not
791 processing it. We put a pointer to this structure in the
792 read_symtab_private field of the psymtab. */
793
794 struct dwarf2_per_cu_data
795 {
796 /* The start offset and length of this compilation unit.
797 NOTE: Unlike comp_unit_head.length, this length includes
798 initial_length_size.
799 If the DIE refers to a DWO file, this is always of the original die,
800 not the DWO file. */
801 sect_offset sect_off;
802 unsigned int length;
803
804 /* DWARF standard version this data has been read from (such as 4 or 5). */
805 short dwarf_version;
806
807 /* Flag indicating this compilation unit will be read in before
808 any of the current compilation units are processed. */
809 unsigned int queued : 1;
810
811 /* This flag will be set when reading partial DIEs if we need to load
812 absolutely all DIEs for this compilation unit, instead of just the ones
813 we think are interesting. It gets set if we look for a DIE in the
814 hash table and don't find it. */
815 unsigned int load_all_dies : 1;
816
817 /* Non-zero if this CU is from .debug_types.
818 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
819 this is non-zero. */
820 unsigned int is_debug_types : 1;
821
822 /* Non-zero if this CU is from the .dwz file. */
823 unsigned int is_dwz : 1;
824
825 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
826 This flag is only valid if is_debug_types is true.
827 We can't read a CU directly from a DWO file: There are required
828 attributes in the stub. */
829 unsigned int reading_dwo_directly : 1;
830
831 /* Non-zero if the TU has been read.
832 This is used to assist the "Stay in DWO Optimization" for Fission:
833 When reading a DWO, it's faster to read TUs from the DWO instead of
834 fetching them from random other DWOs (due to comdat folding).
835 If the TU has already been read, the optimization is unnecessary
836 (and unwise - we don't want to change where gdb thinks the TU lives
837 "midflight").
838 This flag is only valid if is_debug_types is true. */
839 unsigned int tu_read : 1;
840
841 /* The section this CU/TU lives in.
842 If the DIE refers to a DWO file, this is always the original die,
843 not the DWO file. */
844 struct dwarf2_section_info *section;
845
846 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
847 of the CU cache it gets reset to NULL again. This is left as NULL for
848 dummy CUs (a CU header, but nothing else). */
849 struct dwarf2_cu *cu;
850
851 /* The corresponding dwarf2_per_objfile. */
852 struct dwarf2_per_objfile *dwarf2_per_objfile;
853
854 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
855 is active. Otherwise, the 'psymtab' field is active. */
856 union
857 {
858 /* The partial symbol table associated with this compilation unit,
859 or NULL for unread partial units. */
860 struct partial_symtab *psymtab;
861
862 /* Data needed by the "quick" functions. */
863 struct dwarf2_per_cu_quick_data *quick;
864 } v;
865
866 /* The CUs we import using DW_TAG_imported_unit. This is filled in
867 while reading psymtabs, used to compute the psymtab dependencies,
868 and then cleared. Then it is filled in again while reading full
869 symbols, and only deleted when the objfile is destroyed.
870
871 This is also used to work around a difference between the way gold
872 generates .gdb_index version <=7 and the way gdb does. Arguably this
873 is a gold bug. For symbols coming from TUs, gold records in the index
874 the CU that includes the TU instead of the TU itself. This breaks
875 dw2_lookup_symbol: It assumes that if the index says symbol X lives
876 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
877 will find X. Alas TUs live in their own symtab, so after expanding CU Y
878 we need to look in TU Z to find X. Fortunately, this is akin to
879 DW_TAG_imported_unit, so we just use the same mechanism: For
880 .gdb_index version <=7 this also records the TUs that the CU referred
881 to. Concurrently with this change gdb was modified to emit version 8
882 indices so we only pay a price for gold generated indices.
883 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
884 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
885 };
886
887 /* Entry in the signatured_types hash table. */
888
889 struct signatured_type
890 {
891 /* The "per_cu" object of this type.
892 This struct is used iff per_cu.is_debug_types.
893 N.B.: This is the first member so that it's easy to convert pointers
894 between them. */
895 struct dwarf2_per_cu_data per_cu;
896
897 /* The type's signature. */
898 ULONGEST signature;
899
900 /* Offset in the TU of the type's DIE, as read from the TU header.
901 If this TU is a DWO stub and the definition lives in a DWO file
902 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
903 cu_offset type_offset_in_tu;
904
905 /* Offset in the section of the type's DIE.
906 If the definition lives in a DWO file, this is the offset in the
907 .debug_types.dwo section.
908 The value is zero until the actual value is known.
909 Zero is otherwise not a valid section offset. */
910 sect_offset type_offset_in_section;
911
912 /* Type units are grouped by their DW_AT_stmt_list entry so that they
913 can share them. This points to the containing symtab. */
914 struct type_unit_group *type_unit_group;
915
916 /* The type.
917 The first time we encounter this type we fully read it in and install it
918 in the symbol tables. Subsequent times we only need the type. */
919 struct type *type;
920
921 /* Containing DWO unit.
922 This field is valid iff per_cu.reading_dwo_directly. */
923 struct dwo_unit *dwo_unit;
924 };
925
926 typedef struct signatured_type *sig_type_ptr;
927 DEF_VEC_P (sig_type_ptr);
928
929 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
930 This includes type_unit_group and quick_file_names. */
931
932 struct stmt_list_hash
933 {
934 /* The DWO unit this table is from or NULL if there is none. */
935 struct dwo_unit *dwo_unit;
936
937 /* Offset in .debug_line or .debug_line.dwo. */
938 sect_offset line_sect_off;
939 };
940
941 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
942 an object of this type. */
943
944 struct type_unit_group
945 {
946 /* dwarf2read.c's main "handle" on a TU symtab.
947 To simplify things we create an artificial CU that "includes" all the
948 type units using this stmt_list so that the rest of the code still has
949 a "per_cu" handle on the symtab.
950 This PER_CU is recognized by having no section. */
951 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
952 struct dwarf2_per_cu_data per_cu;
953
954 /* The TUs that share this DW_AT_stmt_list entry.
955 This is added to while parsing type units to build partial symtabs,
956 and is deleted afterwards and not used again. */
957 VEC (sig_type_ptr) *tus;
958
959 /* The compunit symtab.
960 Type units in a group needn't all be defined in the same source file,
961 so we create an essentially anonymous symtab as the compunit symtab. */
962 struct compunit_symtab *compunit_symtab;
963
964 /* The data used to construct the hash key. */
965 struct stmt_list_hash hash;
966
967 /* The number of symtabs from the line header.
968 The value here must match line_header.num_file_names. */
969 unsigned int num_symtabs;
970
971 /* The symbol tables for this TU (obtained from the files listed in
972 DW_AT_stmt_list).
973 WARNING: The order of entries here must match the order of entries
974 in the line header. After the first TU using this type_unit_group, the
975 line header for the subsequent TUs is recreated from this. This is done
976 because we need to use the same symtabs for each TU using the same
977 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
978 there's no guarantee the line header doesn't have duplicate entries. */
979 struct symtab **symtabs;
980 };
981
982 /* These sections are what may appear in a (real or virtual) DWO file. */
983
984 struct dwo_sections
985 {
986 struct dwarf2_section_info abbrev;
987 struct dwarf2_section_info line;
988 struct dwarf2_section_info loc;
989 struct dwarf2_section_info loclists;
990 struct dwarf2_section_info macinfo;
991 struct dwarf2_section_info macro;
992 struct dwarf2_section_info str;
993 struct dwarf2_section_info str_offsets;
994 /* In the case of a virtual DWO file, these two are unused. */
995 struct dwarf2_section_info info;
996 VEC (dwarf2_section_info_def) *types;
997 };
998
999 /* CUs/TUs in DWP/DWO files. */
1000
1001 struct dwo_unit
1002 {
1003 /* Backlink to the containing struct dwo_file. */
1004 struct dwo_file *dwo_file;
1005
1006 /* The "id" that distinguishes this CU/TU.
1007 .debug_info calls this "dwo_id", .debug_types calls this "signature".
1008 Since signatures came first, we stick with it for consistency. */
1009 ULONGEST signature;
1010
1011 /* The section this CU/TU lives in, in the DWO file. */
1012 struct dwarf2_section_info *section;
1013
1014 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
1015 sect_offset sect_off;
1016 unsigned int length;
1017
1018 /* For types, offset in the type's DIE of the type defined by this TU. */
1019 cu_offset type_offset_in_tu;
1020 };
1021
1022 /* include/dwarf2.h defines the DWP section codes.
1023 It defines a max value but it doesn't define a min value, which we
1024 use for error checking, so provide one. */
1025
1026 enum dwp_v2_section_ids
1027 {
1028 DW_SECT_MIN = 1
1029 };
1030
1031 /* Data for one DWO file.
1032
1033 This includes virtual DWO files (a virtual DWO file is a DWO file as it
1034 appears in a DWP file). DWP files don't really have DWO files per se -
1035 comdat folding of types "loses" the DWO file they came from, and from
1036 a high level view DWP files appear to contain a mass of random types.
1037 However, to maintain consistency with the non-DWP case we pretend DWP
1038 files contain virtual DWO files, and we assign each TU with one virtual
1039 DWO file (generally based on the line and abbrev section offsets -
1040 a heuristic that seems to work in practice). */
1041
1042 struct dwo_file
1043 {
1044 /* The DW_AT_GNU_dwo_name attribute.
1045 For virtual DWO files the name is constructed from the section offsets
1046 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
1047 from related CU+TUs. */
1048 const char *dwo_name;
1049
1050 /* The DW_AT_comp_dir attribute. */
1051 const char *comp_dir;
1052
1053 /* The bfd, when the file is open. Otherwise this is NULL.
1054 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
1055 bfd *dbfd;
1056
1057 /* The sections that make up this DWO file.
1058 Remember that for virtual DWO files in DWP V2, these are virtual
1059 sections (for lack of a better name). */
1060 struct dwo_sections sections;
1061
1062 /* The CUs in the file.
1063 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
1064 an extension to handle LLVM's Link Time Optimization output (where
1065 multiple source files may be compiled into a single object/dwo pair). */
1066 htab_t cus;
1067
1068 /* Table of TUs in the file.
1069 Each element is a struct dwo_unit. */
1070 htab_t tus;
1071 };
1072
1073 /* These sections are what may appear in a DWP file. */
1074
1075 struct dwp_sections
1076 {
1077 /* These are used by both DWP version 1 and 2. */
1078 struct dwarf2_section_info str;
1079 struct dwarf2_section_info cu_index;
1080 struct dwarf2_section_info tu_index;
1081
1082 /* These are only used by DWP version 2 files.
1083 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
1084 sections are referenced by section number, and are not recorded here.
1085 In DWP version 2 there is at most one copy of all these sections, each
1086 section being (effectively) comprised of the concatenation of all of the
1087 individual sections that exist in the version 1 format.
1088 To keep the code simple we treat each of these concatenated pieces as a
1089 section itself (a virtual section?). */
1090 struct dwarf2_section_info abbrev;
1091 struct dwarf2_section_info info;
1092 struct dwarf2_section_info line;
1093 struct dwarf2_section_info loc;
1094 struct dwarf2_section_info macinfo;
1095 struct dwarf2_section_info macro;
1096 struct dwarf2_section_info str_offsets;
1097 struct dwarf2_section_info types;
1098 };
1099
1100 /* These sections are what may appear in a virtual DWO file in DWP version 1.
1101 A virtual DWO file is a DWO file as it appears in a DWP file. */
1102
1103 struct virtual_v1_dwo_sections
1104 {
1105 struct dwarf2_section_info abbrev;
1106 struct dwarf2_section_info line;
1107 struct dwarf2_section_info loc;
1108 struct dwarf2_section_info macinfo;
1109 struct dwarf2_section_info macro;
1110 struct dwarf2_section_info str_offsets;
1111 /* Each DWP hash table entry records one CU or one TU.
1112 That is recorded here, and copied to dwo_unit.section. */
1113 struct dwarf2_section_info info_or_types;
1114 };
1115
1116 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1117 In version 2, the sections of the DWO files are concatenated together
1118 and stored in one section of that name. Thus each ELF section contains
1119 several "virtual" sections. */
1120
1121 struct virtual_v2_dwo_sections
1122 {
1123 bfd_size_type abbrev_offset;
1124 bfd_size_type abbrev_size;
1125
1126 bfd_size_type line_offset;
1127 bfd_size_type line_size;
1128
1129 bfd_size_type loc_offset;
1130 bfd_size_type loc_size;
1131
1132 bfd_size_type macinfo_offset;
1133 bfd_size_type macinfo_size;
1134
1135 bfd_size_type macro_offset;
1136 bfd_size_type macro_size;
1137
1138 bfd_size_type str_offsets_offset;
1139 bfd_size_type str_offsets_size;
1140
1141 /* Each DWP hash table entry records one CU or one TU.
1142 That is recorded here, and copied to dwo_unit.section. */
1143 bfd_size_type info_or_types_offset;
1144 bfd_size_type info_or_types_size;
1145 };
1146
1147 /* Contents of DWP hash tables. */
1148
1149 struct dwp_hash_table
1150 {
1151 uint32_t version, nr_columns;
1152 uint32_t nr_units, nr_slots;
1153 const gdb_byte *hash_table, *unit_table;
1154 union
1155 {
1156 struct
1157 {
1158 const gdb_byte *indices;
1159 } v1;
1160 struct
1161 {
1162 /* This is indexed by column number and gives the id of the section
1163 in that column. */
1164 #define MAX_NR_V2_DWO_SECTIONS \
1165 (1 /* .debug_info or .debug_types */ \
1166 + 1 /* .debug_abbrev */ \
1167 + 1 /* .debug_line */ \
1168 + 1 /* .debug_loc */ \
1169 + 1 /* .debug_str_offsets */ \
1170 + 1 /* .debug_macro or .debug_macinfo */)
1171 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1172 const gdb_byte *offsets;
1173 const gdb_byte *sizes;
1174 } v2;
1175 } section_pool;
1176 };
1177
1178 /* Data for one DWP file. */
1179
1180 struct dwp_file
1181 {
1182 /* Name of the file. */
1183 const char *name;
1184
1185 /* File format version. */
1186 int version;
1187
1188 /* The bfd. */
1189 bfd *dbfd;
1190
1191 /* Section info for this file. */
1192 struct dwp_sections sections;
1193
1194 /* Table of CUs in the file. */
1195 const struct dwp_hash_table *cus;
1196
1197 /* Table of TUs in the file. */
1198 const struct dwp_hash_table *tus;
1199
1200 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1201 htab_t loaded_cus;
1202 htab_t loaded_tus;
1203
1204 /* Table to map ELF section numbers to their sections.
1205 This is only needed for the DWP V1 file format. */
1206 unsigned int num_sections;
1207 asection **elf_sections;
1208 };
1209
1210 /* This represents a '.dwz' file. */
1211
1212 struct dwz_file
1213 {
1214 /* A dwz file can only contain a few sections. */
1215 struct dwarf2_section_info abbrev;
1216 struct dwarf2_section_info info;
1217 struct dwarf2_section_info str;
1218 struct dwarf2_section_info line;
1219 struct dwarf2_section_info macro;
1220 struct dwarf2_section_info gdb_index;
1221 struct dwarf2_section_info debug_names;
1222
1223 /* The dwz's BFD. */
1224 bfd *dwz_bfd;
1225 };
1226
1227 /* Struct used to pass misc. parameters to read_die_and_children, et
1228 al. which are used for both .debug_info and .debug_types dies.
1229 All parameters here are unchanging for the life of the call. This
1230 struct exists to abstract away the constant parameters of die reading. */
1231
1232 struct die_reader_specs
1233 {
1234 /* The bfd of die_section. */
1235 bfd* abfd;
1236
1237 /* The CU of the DIE we are parsing. */
1238 struct dwarf2_cu *cu;
1239
1240 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1241 struct dwo_file *dwo_file;
1242
1243 /* The section the die comes from.
1244 This is either .debug_info or .debug_types, or the .dwo variants. */
1245 struct dwarf2_section_info *die_section;
1246
1247 /* die_section->buffer. */
1248 const gdb_byte *buffer;
1249
1250 /* The end of the buffer. */
1251 const gdb_byte *buffer_end;
1252
1253 /* The value of the DW_AT_comp_dir attribute. */
1254 const char *comp_dir;
1255
1256 /* The abbreviation table to use when reading the DIEs. */
1257 struct abbrev_table *abbrev_table;
1258 };
1259
1260 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1261 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1262 const gdb_byte *info_ptr,
1263 struct die_info *comp_unit_die,
1264 int has_children,
1265 void *data);
1266
1267 /* A 1-based directory index. This is a strong typedef to prevent
1268 accidentally using a directory index as a 0-based index into an
1269 array/vector. */
1270 enum class dir_index : unsigned int {};
1271
1272 /* Likewise, a 1-based file name index. */
1273 enum class file_name_index : unsigned int {};
1274
1275 struct file_entry
1276 {
1277 file_entry () = default;
1278
1279 file_entry (const char *name_, dir_index d_index_,
1280 unsigned int mod_time_, unsigned int length_)
1281 : name (name_),
1282 d_index (d_index_),
1283 mod_time (mod_time_),
1284 length (length_)
1285 {}
1286
1287 /* Return the include directory at D_INDEX stored in LH. Returns
1288 NULL if D_INDEX is out of bounds. */
1289 const char *include_dir (const line_header *lh) const;
1290
1291 /* The file name. Note this is an observing pointer. The memory is
1292 owned by debug_line_buffer. */
1293 const char *name {};
1294
1295 /* The directory index (1-based). */
1296 dir_index d_index {};
1297
1298 unsigned int mod_time {};
1299
1300 unsigned int length {};
1301
1302 /* True if referenced by the Line Number Program. */
1303 bool included_p {};
1304
1305 /* The associated symbol table, if any. */
1306 struct symtab *symtab {};
1307 };
1308
1309 /* The line number information for a compilation unit (found in the
1310 .debug_line section) begins with a "statement program header",
1311 which contains the following information. */
1312 struct line_header
1313 {
1314 line_header ()
1315 : offset_in_dwz {}
1316 {}
1317
1318 /* Add an entry to the include directory table. */
1319 void add_include_dir (const char *include_dir);
1320
1321 /* Add an entry to the file name table. */
1322 void add_file_name (const char *name, dir_index d_index,
1323 unsigned int mod_time, unsigned int length);
1324
1325 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1326 is out of bounds. */
1327 const char *include_dir_at (dir_index index) const
1328 {
1329 /* Convert directory index number (1-based) to vector index
1330 (0-based). */
1331 size_t vec_index = to_underlying (index) - 1;
1332
1333 if (vec_index >= include_dirs.size ())
1334 return NULL;
1335 return include_dirs[vec_index];
1336 }
1337
1338 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1339 is out of bounds. */
1340 file_entry *file_name_at (file_name_index index)
1341 {
1342 /* Convert file name index number (1-based) to vector index
1343 (0-based). */
1344 size_t vec_index = to_underlying (index) - 1;
1345
1346 if (vec_index >= file_names.size ())
1347 return NULL;
1348 return &file_names[vec_index];
1349 }
1350
1351 /* Const version of the above. */
1352 const file_entry *file_name_at (unsigned int index) const
1353 {
1354 if (index >= file_names.size ())
1355 return NULL;
1356 return &file_names[index];
1357 }
1358
1359 /* Offset of line number information in .debug_line section. */
1360 sect_offset sect_off {};
1361
1362 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1363 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1364
1365 unsigned int total_length {};
1366 unsigned short version {};
1367 unsigned int header_length {};
1368 unsigned char minimum_instruction_length {};
1369 unsigned char maximum_ops_per_instruction {};
1370 unsigned char default_is_stmt {};
1371 int line_base {};
1372 unsigned char line_range {};
1373 unsigned char opcode_base {};
1374
1375 /* standard_opcode_lengths[i] is the number of operands for the
1376 standard opcode whose value is i. This means that
1377 standard_opcode_lengths[0] is unused, and the last meaningful
1378 element is standard_opcode_lengths[opcode_base - 1]. */
1379 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1380
1381 /* The include_directories table. Note these are observing
1382 pointers. The memory is owned by debug_line_buffer. */
1383 std::vector<const char *> include_dirs;
1384
1385 /* The file_names table. */
1386 std::vector<file_entry> file_names;
1387
1388 /* The start and end of the statement program following this
1389 header. These point into dwarf2_per_objfile->line_buffer. */
1390 const gdb_byte *statement_program_start {}, *statement_program_end {};
1391 };
1392
1393 typedef std::unique_ptr<line_header> line_header_up;
1394
1395 const char *
1396 file_entry::include_dir (const line_header *lh) const
1397 {
1398 return lh->include_dir_at (d_index);
1399 }
1400
1401 /* When we construct a partial symbol table entry we only
1402 need this much information. */
1403 struct partial_die_info
1404 {
1405 /* Offset of this DIE. */
1406 sect_offset sect_off;
1407
1408 /* DWARF-2 tag for this DIE. */
1409 ENUM_BITFIELD(dwarf_tag) tag : 16;
1410
1411 /* Assorted flags describing the data found in this DIE. */
1412 unsigned int has_children : 1;
1413 unsigned int is_external : 1;
1414 unsigned int is_declaration : 1;
1415 unsigned int has_type : 1;
1416 unsigned int has_specification : 1;
1417 unsigned int has_pc_info : 1;
1418 unsigned int may_be_inlined : 1;
1419
1420 /* This DIE has been marked DW_AT_main_subprogram. */
1421 unsigned int main_subprogram : 1;
1422
1423 /* Flag set if the SCOPE field of this structure has been
1424 computed. */
1425 unsigned int scope_set : 1;
1426
1427 /* Flag set if the DIE has a byte_size attribute. */
1428 unsigned int has_byte_size : 1;
1429
1430 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1431 unsigned int has_const_value : 1;
1432
1433 /* Flag set if any of the DIE's children are template arguments. */
1434 unsigned int has_template_arguments : 1;
1435
1436 /* Flag set if fixup_partial_die has been called on this die. */
1437 unsigned int fixup_called : 1;
1438
1439 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1440 unsigned int is_dwz : 1;
1441
1442 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1443 unsigned int spec_is_dwz : 1;
1444
1445 /* The name of this DIE. Normally the value of DW_AT_name, but
1446 sometimes a default name for unnamed DIEs. */
1447 const char *name;
1448
1449 /* The linkage name, if present. */
1450 const char *linkage_name;
1451
1452 /* The scope to prepend to our children. This is generally
1453 allocated on the comp_unit_obstack, so will disappear
1454 when this compilation unit leaves the cache. */
1455 const char *scope;
1456
1457 /* Some data associated with the partial DIE. The tag determines
1458 which field is live. */
1459 union
1460 {
1461 /* The location description associated with this DIE, if any. */
1462 struct dwarf_block *locdesc;
1463 /* The offset of an import, for DW_TAG_imported_unit. */
1464 sect_offset sect_off;
1465 } d;
1466
1467 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1468 CORE_ADDR lowpc;
1469 CORE_ADDR highpc;
1470
1471 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1472 DW_AT_sibling, if any. */
1473 /* NOTE: This member isn't strictly necessary, read_partial_die could
1474 return DW_AT_sibling values to its caller load_partial_dies. */
1475 const gdb_byte *sibling;
1476
1477 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1478 DW_AT_specification (or DW_AT_abstract_origin or
1479 DW_AT_extension). */
1480 sect_offset spec_offset;
1481
1482 /* Pointers to this DIE's parent, first child, and next sibling,
1483 if any. */
1484 struct partial_die_info *die_parent, *die_child, *die_sibling;
1485 };
1486
1487 /* This data structure holds the information of an abbrev. */
1488 struct abbrev_info
1489 {
1490 unsigned int number; /* number identifying abbrev */
1491 enum dwarf_tag tag; /* dwarf tag */
1492 unsigned short has_children; /* boolean */
1493 unsigned short num_attrs; /* number of attributes */
1494 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1495 struct abbrev_info *next; /* next in chain */
1496 };
1497
1498 struct attr_abbrev
1499 {
1500 ENUM_BITFIELD(dwarf_attribute) name : 16;
1501 ENUM_BITFIELD(dwarf_form) form : 16;
1502
1503 /* It is valid only if FORM is DW_FORM_implicit_const. */
1504 LONGEST implicit_const;
1505 };
1506
1507 /* Size of abbrev_table.abbrev_hash_table. */
1508 #define ABBREV_HASH_SIZE 121
1509
1510 /* Top level data structure to contain an abbreviation table. */
1511
1512 struct abbrev_table
1513 {
1514 explicit abbrev_table (sect_offset off)
1515 : sect_off (off)
1516 {
1517 m_abbrevs =
1518 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1519 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1520 }
1521
1522 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1523
1524 /* Allocate space for a struct abbrev_info object in
1525 ABBREV_TABLE. */
1526 struct abbrev_info *alloc_abbrev ();
1527
1528 /* Add an abbreviation to the table. */
1529 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1530
1531 /* Look up an abbrev in the table.
1532 Returns NULL if the abbrev is not found. */
1533
1534 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1535
1536
1537 /* Where the abbrev table came from.
1538 This is used as a sanity check when the table is used. */
1539 const sect_offset sect_off;
1540
1541 /* Storage for the abbrev table. */
1542 auto_obstack abbrev_obstack;
1543
1544 private:
1545
1546 /* Hash table of abbrevs.
1547 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1548 It could be statically allocated, but the previous code didn't so we
1549 don't either. */
1550 struct abbrev_info **m_abbrevs;
1551 };
1552
1553 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1554
1555 /* Attributes have a name and a value. */
1556 struct attribute
1557 {
1558 ENUM_BITFIELD(dwarf_attribute) name : 16;
1559 ENUM_BITFIELD(dwarf_form) form : 15;
1560
1561 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1562 field should be in u.str (existing only for DW_STRING) but it is kept
1563 here for better struct attribute alignment. */
1564 unsigned int string_is_canonical : 1;
1565
1566 union
1567 {
1568 const char *str;
1569 struct dwarf_block *blk;
1570 ULONGEST unsnd;
1571 LONGEST snd;
1572 CORE_ADDR addr;
1573 ULONGEST signature;
1574 }
1575 u;
1576 };
1577
1578 /* This data structure holds a complete die structure. */
1579 struct die_info
1580 {
1581 /* DWARF-2 tag for this DIE. */
1582 ENUM_BITFIELD(dwarf_tag) tag : 16;
1583
1584 /* Number of attributes */
1585 unsigned char num_attrs;
1586
1587 /* True if we're presently building the full type name for the
1588 type derived from this DIE. */
1589 unsigned char building_fullname : 1;
1590
1591 /* True if this die is in process. PR 16581. */
1592 unsigned char in_process : 1;
1593
1594 /* Abbrev number */
1595 unsigned int abbrev;
1596
1597 /* Offset in .debug_info or .debug_types section. */
1598 sect_offset sect_off;
1599
1600 /* The dies in a compilation unit form an n-ary tree. PARENT
1601 points to this die's parent; CHILD points to the first child of
1602 this node; and all the children of a given node are chained
1603 together via their SIBLING fields. */
1604 struct die_info *child; /* Its first child, if any. */
1605 struct die_info *sibling; /* Its next sibling, if any. */
1606 struct die_info *parent; /* Its parent, if any. */
1607
1608 /* An array of attributes, with NUM_ATTRS elements. There may be
1609 zero, but it's not common and zero-sized arrays are not
1610 sufficiently portable C. */
1611 struct attribute attrs[1];
1612 };
1613
1614 /* Get at parts of an attribute structure. */
1615
1616 #define DW_STRING(attr) ((attr)->u.str)
1617 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1618 #define DW_UNSND(attr) ((attr)->u.unsnd)
1619 #define DW_BLOCK(attr) ((attr)->u.blk)
1620 #define DW_SND(attr) ((attr)->u.snd)
1621 #define DW_ADDR(attr) ((attr)->u.addr)
1622 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1623
1624 /* Blocks are a bunch of untyped bytes. */
1625 struct dwarf_block
1626 {
1627 size_t size;
1628
1629 /* Valid only if SIZE is not zero. */
1630 const gdb_byte *data;
1631 };
1632
1633 #ifndef ATTR_ALLOC_CHUNK
1634 #define ATTR_ALLOC_CHUNK 4
1635 #endif
1636
1637 /* Allocate fields for structs, unions and enums in this size. */
1638 #ifndef DW_FIELD_ALLOC_CHUNK
1639 #define DW_FIELD_ALLOC_CHUNK 4
1640 #endif
1641
1642 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1643 but this would require a corresponding change in unpack_field_as_long
1644 and friends. */
1645 static int bits_per_byte = 8;
1646
1647 struct nextfield
1648 {
1649 struct nextfield *next;
1650 int accessibility;
1651 int virtuality;
1652 struct field field;
1653 };
1654
1655 struct nextfnfield
1656 {
1657 struct nextfnfield *next;
1658 struct fn_field fnfield;
1659 };
1660
1661 struct fnfieldlist
1662 {
1663 const char *name;
1664 int length;
1665 struct nextfnfield *head;
1666 };
1667
1668 struct decl_field_list
1669 {
1670 struct decl_field field;
1671 struct decl_field_list *next;
1672 };
1673
1674 /* The routines that read and process dies for a C struct or C++ class
1675 pass lists of data member fields and lists of member function fields
1676 in an instance of a field_info structure, as defined below. */
1677 struct field_info
1678 {
1679 /* List of data member and baseclasses fields. */
1680 struct nextfield *fields, *baseclasses;
1681
1682 /* Number of fields (including baseclasses). */
1683 int nfields;
1684
1685 /* Number of baseclasses. */
1686 int nbaseclasses;
1687
1688 /* Set if the accesibility of one of the fields is not public. */
1689 int non_public_fields;
1690
1691 /* Member function fieldlist array, contains name of possibly overloaded
1692 member function, number of overloaded member functions and a pointer
1693 to the head of the member function field chain. */
1694 struct fnfieldlist *fnfieldlists;
1695
1696 /* Number of entries in the fnfieldlists array. */
1697 int nfnfields;
1698
1699 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1700 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1701 struct decl_field_list *typedef_field_list;
1702 unsigned typedef_field_list_count;
1703
1704 /* Nested types defined by this class and the number of elements in this
1705 list. */
1706 struct decl_field_list *nested_types_list;
1707 unsigned nested_types_list_count;
1708 };
1709
1710 /* One item on the queue of compilation units to read in full symbols
1711 for. */
1712 struct dwarf2_queue_item
1713 {
1714 struct dwarf2_per_cu_data *per_cu;
1715 enum language pretend_language;
1716 struct dwarf2_queue_item *next;
1717 };
1718
1719 /* The current queue. */
1720 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1721
1722 /* Loaded secondary compilation units are kept in memory until they
1723 have not been referenced for the processing of this many
1724 compilation units. Set this to zero to disable caching. Cache
1725 sizes of up to at least twenty will improve startup time for
1726 typical inter-CU-reference binaries, at an obvious memory cost. */
1727 static int dwarf_max_cache_age = 5;
1728 static void
1729 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1730 struct cmd_list_element *c, const char *value)
1731 {
1732 fprintf_filtered (file, _("The upper bound on the age of cached "
1733 "DWARF compilation units is %s.\n"),
1734 value);
1735 }
1736 \f
1737 /* local function prototypes */
1738
1739 static const char *get_section_name (const struct dwarf2_section_info *);
1740
1741 static const char *get_section_file_name (const struct dwarf2_section_info *);
1742
1743 static void dwarf2_find_base_address (struct die_info *die,
1744 struct dwarf2_cu *cu);
1745
1746 static struct partial_symtab *create_partial_symtab
1747 (struct dwarf2_per_cu_data *per_cu, const char *name);
1748
1749 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1750 const gdb_byte *info_ptr,
1751 struct die_info *type_unit_die,
1752 int has_children, void *data);
1753
1754 static void dwarf2_build_psymtabs_hard
1755 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1756
1757 static void scan_partial_symbols (struct partial_die_info *,
1758 CORE_ADDR *, CORE_ADDR *,
1759 int, struct dwarf2_cu *);
1760
1761 static void add_partial_symbol (struct partial_die_info *,
1762 struct dwarf2_cu *);
1763
1764 static void add_partial_namespace (struct partial_die_info *pdi,
1765 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1766 int set_addrmap, struct dwarf2_cu *cu);
1767
1768 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1769 CORE_ADDR *highpc, int set_addrmap,
1770 struct dwarf2_cu *cu);
1771
1772 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1773 struct dwarf2_cu *cu);
1774
1775 static void add_partial_subprogram (struct partial_die_info *pdi,
1776 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1777 int need_pc, struct dwarf2_cu *cu);
1778
1779 static void dwarf2_read_symtab (struct partial_symtab *,
1780 struct objfile *);
1781
1782 static void psymtab_to_symtab_1 (struct partial_symtab *);
1783
1784 static abbrev_table_up abbrev_table_read_table
1785 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1786 sect_offset);
1787
1788 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1789
1790 static struct partial_die_info *load_partial_dies
1791 (const struct die_reader_specs *, const gdb_byte *, int);
1792
1793 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1794 struct partial_die_info *,
1795 struct abbrev_info *,
1796 unsigned int,
1797 const gdb_byte *);
1798
1799 static struct partial_die_info *find_partial_die (sect_offset, int,
1800 struct dwarf2_cu *);
1801
1802 static void fixup_partial_die (struct partial_die_info *,
1803 struct dwarf2_cu *);
1804
1805 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1806 struct attribute *, struct attr_abbrev *,
1807 const gdb_byte *);
1808
1809 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1810
1811 static int read_1_signed_byte (bfd *, const gdb_byte *);
1812
1813 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1814
1815 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1816
1817 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1818
1819 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1820 unsigned int *);
1821
1822 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1823
1824 static LONGEST read_checked_initial_length_and_offset
1825 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1826 unsigned int *, unsigned int *);
1827
1828 static LONGEST read_offset (bfd *, const gdb_byte *,
1829 const struct comp_unit_head *,
1830 unsigned int *);
1831
1832 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1833
1834 static sect_offset read_abbrev_offset
1835 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1836 struct dwarf2_section_info *, sect_offset);
1837
1838 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1839
1840 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1841
1842 static const char *read_indirect_string
1843 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1844 const struct comp_unit_head *, unsigned int *);
1845
1846 static const char *read_indirect_line_string
1847 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1848 const struct comp_unit_head *, unsigned int *);
1849
1850 static const char *read_indirect_string_at_offset
1851 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1852 LONGEST str_offset);
1853
1854 static const char *read_indirect_string_from_dwz
1855 (struct objfile *objfile, struct dwz_file *, LONGEST);
1856
1857 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1858
1859 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1860 const gdb_byte *,
1861 unsigned int *);
1862
1863 static const char *read_str_index (const struct die_reader_specs *reader,
1864 ULONGEST str_index);
1865
1866 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1867
1868 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1869 struct dwarf2_cu *);
1870
1871 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1872 unsigned int);
1873
1874 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1875 struct dwarf2_cu *cu);
1876
1877 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1878 struct dwarf2_cu *cu);
1879
1880 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1881
1882 static struct die_info *die_specification (struct die_info *die,
1883 struct dwarf2_cu **);
1884
1885 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1886 struct dwarf2_cu *cu);
1887
1888 static void dwarf_decode_lines (struct line_header *, const char *,
1889 struct dwarf2_cu *, struct partial_symtab *,
1890 CORE_ADDR, int decode_mapping);
1891
1892 static void dwarf2_start_subfile (const char *, const char *);
1893
1894 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1895 const char *, const char *,
1896 CORE_ADDR);
1897
1898 static struct symbol *new_symbol (struct die_info *, struct type *,
1899 struct dwarf2_cu *, struct symbol * = NULL);
1900
1901 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1902 struct dwarf2_cu *);
1903
1904 static void dwarf2_const_value_attr (const struct attribute *attr,
1905 struct type *type,
1906 const char *name,
1907 struct obstack *obstack,
1908 struct dwarf2_cu *cu, LONGEST *value,
1909 const gdb_byte **bytes,
1910 struct dwarf2_locexpr_baton **baton);
1911
1912 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1913
1914 static int need_gnat_info (struct dwarf2_cu *);
1915
1916 static struct type *die_descriptive_type (struct die_info *,
1917 struct dwarf2_cu *);
1918
1919 static void set_descriptive_type (struct type *, struct die_info *,
1920 struct dwarf2_cu *);
1921
1922 static struct type *die_containing_type (struct die_info *,
1923 struct dwarf2_cu *);
1924
1925 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1926 struct dwarf2_cu *);
1927
1928 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1929
1930 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1931
1932 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1933
1934 static char *typename_concat (struct obstack *obs, const char *prefix,
1935 const char *suffix, int physname,
1936 struct dwarf2_cu *cu);
1937
1938 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1939
1940 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1941
1942 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1943
1944 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1945
1946 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1947
1948 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1949
1950 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1951 struct dwarf2_cu *, struct partial_symtab *);
1952
1953 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1954 values. Keep the items ordered with increasing constraints compliance. */
1955 enum pc_bounds_kind
1956 {
1957 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1958 PC_BOUNDS_NOT_PRESENT,
1959
1960 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1961 were present but they do not form a valid range of PC addresses. */
1962 PC_BOUNDS_INVALID,
1963
1964 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1965 PC_BOUNDS_RANGES,
1966
1967 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1968 PC_BOUNDS_HIGH_LOW,
1969 };
1970
1971 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1972 CORE_ADDR *, CORE_ADDR *,
1973 struct dwarf2_cu *,
1974 struct partial_symtab *);
1975
1976 static void get_scope_pc_bounds (struct die_info *,
1977 CORE_ADDR *, CORE_ADDR *,
1978 struct dwarf2_cu *);
1979
1980 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1981 CORE_ADDR, struct dwarf2_cu *);
1982
1983 static void dwarf2_add_field (struct field_info *, struct die_info *,
1984 struct dwarf2_cu *);
1985
1986 static void dwarf2_attach_fields_to_type (struct field_info *,
1987 struct type *, struct dwarf2_cu *);
1988
1989 static void dwarf2_add_member_fn (struct field_info *,
1990 struct die_info *, struct type *,
1991 struct dwarf2_cu *);
1992
1993 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1994 struct type *,
1995 struct dwarf2_cu *);
1996
1997 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1998
1999 static void read_common_block (struct die_info *, struct dwarf2_cu *);
2000
2001 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
2002
2003 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
2004
2005 static struct using_direct **using_directives (enum language);
2006
2007 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
2008
2009 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
2010
2011 static struct type *read_module_type (struct die_info *die,
2012 struct dwarf2_cu *cu);
2013
2014 static const char *namespace_name (struct die_info *die,
2015 int *is_anonymous, struct dwarf2_cu *);
2016
2017 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
2018
2019 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
2020
2021 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
2022 struct dwarf2_cu *);
2023
2024 static struct die_info *read_die_and_siblings_1
2025 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
2026 struct die_info *);
2027
2028 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
2029 const gdb_byte *info_ptr,
2030 const gdb_byte **new_info_ptr,
2031 struct die_info *parent);
2032
2033 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
2034 struct die_info **, const gdb_byte *,
2035 int *, int);
2036
2037 static const gdb_byte *read_full_die (const struct die_reader_specs *,
2038 struct die_info **, const gdb_byte *,
2039 int *);
2040
2041 static void process_die (struct die_info *, struct dwarf2_cu *);
2042
2043 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
2044 struct obstack *);
2045
2046 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
2047
2048 static const char *dwarf2_full_name (const char *name,
2049 struct die_info *die,
2050 struct dwarf2_cu *cu);
2051
2052 static const char *dwarf2_physname (const char *name, struct die_info *die,
2053 struct dwarf2_cu *cu);
2054
2055 static struct die_info *dwarf2_extension (struct die_info *die,
2056 struct dwarf2_cu **);
2057
2058 static const char *dwarf_tag_name (unsigned int);
2059
2060 static const char *dwarf_attr_name (unsigned int);
2061
2062 static const char *dwarf_form_name (unsigned int);
2063
2064 static const char *dwarf_bool_name (unsigned int);
2065
2066 static const char *dwarf_type_encoding_name (unsigned int);
2067
2068 static struct die_info *sibling_die (struct die_info *);
2069
2070 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
2071
2072 static void dump_die_for_error (struct die_info *);
2073
2074 static void dump_die_1 (struct ui_file *, int level, int max_level,
2075 struct die_info *);
2076
2077 /*static*/ void dump_die (struct die_info *, int max_level);
2078
2079 static void store_in_ref_table (struct die_info *,
2080 struct dwarf2_cu *);
2081
2082 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
2083
2084 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
2085
2086 static struct die_info *follow_die_ref_or_sig (struct die_info *,
2087 const struct attribute *,
2088 struct dwarf2_cu **);
2089
2090 static struct die_info *follow_die_ref (struct die_info *,
2091 const struct attribute *,
2092 struct dwarf2_cu **);
2093
2094 static struct die_info *follow_die_sig (struct die_info *,
2095 const struct attribute *,
2096 struct dwarf2_cu **);
2097
2098 static struct type *get_signatured_type (struct die_info *, ULONGEST,
2099 struct dwarf2_cu *);
2100
2101 static struct type *get_DW_AT_signature_type (struct die_info *,
2102 const struct attribute *,
2103 struct dwarf2_cu *);
2104
2105 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
2106
2107 static void read_signatured_type (struct signatured_type *);
2108
2109 static int attr_to_dynamic_prop (const struct attribute *attr,
2110 struct die_info *die, struct dwarf2_cu *cu,
2111 struct dynamic_prop *prop);
2112
2113 /* memory allocation interface */
2114
2115 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
2116
2117 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
2118
2119 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2120
2121 static int attr_form_is_block (const struct attribute *);
2122
2123 static int attr_form_is_section_offset (const struct attribute *);
2124
2125 static int attr_form_is_constant (const struct attribute *);
2126
2127 static int attr_form_is_ref (const struct attribute *);
2128
2129 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2130 struct dwarf2_loclist_baton *baton,
2131 const struct attribute *attr);
2132
2133 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
2134 struct symbol *sym,
2135 struct dwarf2_cu *cu,
2136 int is_block);
2137
2138 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2139 const gdb_byte *info_ptr,
2140 struct abbrev_info *abbrev);
2141
2142 static hashval_t partial_die_hash (const void *item);
2143
2144 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2145
2146 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
2147 (sect_offset sect_off, unsigned int offset_in_dwz,
2148 struct dwarf2_per_objfile *dwarf2_per_objfile);
2149
2150 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
2151 struct die_info *comp_unit_die,
2152 enum language pretend_language);
2153
2154 static void free_cached_comp_units (void *);
2155
2156 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2157
2158 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
2159
2160 static struct type *set_die_type (struct die_info *, struct type *,
2161 struct dwarf2_cu *);
2162
2163 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2164
2165 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2166
2167 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2168 enum language);
2169
2170 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2171 enum language);
2172
2173 static void process_full_type_unit (struct dwarf2_per_cu_data *,
2174 enum language);
2175
2176 static void dwarf2_add_dependence (struct dwarf2_cu *,
2177 struct dwarf2_per_cu_data *);
2178
2179 static void dwarf2_mark (struct dwarf2_cu *);
2180
2181 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2182
2183 static struct type *get_die_type_at_offset (sect_offset,
2184 struct dwarf2_per_cu_data *);
2185
2186 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
2187
2188 static void dwarf2_release_queue (void *dummy);
2189
2190 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2191 enum language pretend_language);
2192
2193 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
2194
2195 /* The return type of find_file_and_directory. Note, the enclosed
2196 string pointers are only valid while this object is valid. */
2197
2198 struct file_and_directory
2199 {
2200 /* The filename. This is never NULL. */
2201 const char *name;
2202
2203 /* The compilation directory. NULL if not known. If we needed to
2204 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2205 points directly to the DW_AT_comp_dir string attribute owned by
2206 the obstack that owns the DIE. */
2207 const char *comp_dir;
2208
2209 /* If we needed to build a new string for comp_dir, this is what
2210 owns the storage. */
2211 std::string comp_dir_storage;
2212 };
2213
2214 static file_and_directory find_file_and_directory (struct die_info *die,
2215 struct dwarf2_cu *cu);
2216
2217 static char *file_full_name (int file, struct line_header *lh,
2218 const char *comp_dir);
2219
2220 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2221 enum class rcuh_kind { COMPILE, TYPE };
2222
2223 static const gdb_byte *read_and_check_comp_unit_head
2224 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2225 struct comp_unit_head *header,
2226 struct dwarf2_section_info *section,
2227 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2228 rcuh_kind section_kind);
2229
2230 static void init_cutu_and_read_dies
2231 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2232 int use_existing_cu, int keep,
2233 die_reader_func_ftype *die_reader_func, void *data);
2234
2235 static void init_cutu_and_read_dies_simple
2236 (struct dwarf2_per_cu_data *this_cu,
2237 die_reader_func_ftype *die_reader_func, void *data);
2238
2239 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2240
2241 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2242
2243 static struct dwo_unit *lookup_dwo_unit_in_dwp
2244 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2245 struct dwp_file *dwp_file, const char *comp_dir,
2246 ULONGEST signature, int is_debug_types);
2247
2248 static struct dwp_file *get_dwp_file
2249 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2250
2251 static struct dwo_unit *lookup_dwo_comp_unit
2252 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2253
2254 static struct dwo_unit *lookup_dwo_type_unit
2255 (struct signatured_type *, const char *, const char *);
2256
2257 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2258
2259 static void free_dwo_file_cleanup (void *);
2260
2261 struct free_dwo_file_cleanup_data
2262 {
2263 struct dwo_file *dwo_file;
2264 struct dwarf2_per_objfile *dwarf2_per_objfile;
2265 };
2266
2267 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2268
2269 static void check_producer (struct dwarf2_cu *cu);
2270
2271 static void free_line_header_voidp (void *arg);
2272 \f
2273 /* Various complaints about symbol reading that don't abort the process. */
2274
2275 static void
2276 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2277 {
2278 complaint (&symfile_complaints,
2279 _("statement list doesn't fit in .debug_line section"));
2280 }
2281
2282 static void
2283 dwarf2_debug_line_missing_file_complaint (void)
2284 {
2285 complaint (&symfile_complaints,
2286 _(".debug_line section has line data without a file"));
2287 }
2288
2289 static void
2290 dwarf2_debug_line_missing_end_sequence_complaint (void)
2291 {
2292 complaint (&symfile_complaints,
2293 _(".debug_line section has line "
2294 "program sequence without an end"));
2295 }
2296
2297 static void
2298 dwarf2_complex_location_expr_complaint (void)
2299 {
2300 complaint (&symfile_complaints, _("location expression too complex"));
2301 }
2302
2303 static void
2304 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2305 int arg3)
2306 {
2307 complaint (&symfile_complaints,
2308 _("const value length mismatch for '%s', got %d, expected %d"),
2309 arg1, arg2, arg3);
2310 }
2311
2312 static void
2313 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2314 {
2315 complaint (&symfile_complaints,
2316 _("debug info runs off end of %s section"
2317 " [in module %s]"),
2318 get_section_name (section),
2319 get_section_file_name (section));
2320 }
2321
2322 static void
2323 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2324 {
2325 complaint (&symfile_complaints,
2326 _("macro debug info contains a "
2327 "malformed macro definition:\n`%s'"),
2328 arg1);
2329 }
2330
2331 static void
2332 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2333 {
2334 complaint (&symfile_complaints,
2335 _("invalid attribute class or form for '%s' in '%s'"),
2336 arg1, arg2);
2337 }
2338
2339 /* Hash function for line_header_hash. */
2340
2341 static hashval_t
2342 line_header_hash (const struct line_header *ofs)
2343 {
2344 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2345 }
2346
2347 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2348
2349 static hashval_t
2350 line_header_hash_voidp (const void *item)
2351 {
2352 const struct line_header *ofs = (const struct line_header *) item;
2353
2354 return line_header_hash (ofs);
2355 }
2356
2357 /* Equality function for line_header_hash. */
2358
2359 static int
2360 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2361 {
2362 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2363 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2364
2365 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2366 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2367 }
2368
2369 \f
2370
2371 /* Read the given attribute value as an address, taking the attribute's
2372 form into account. */
2373
2374 static CORE_ADDR
2375 attr_value_as_address (struct attribute *attr)
2376 {
2377 CORE_ADDR addr;
2378
2379 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2380 {
2381 /* Aside from a few clearly defined exceptions, attributes that
2382 contain an address must always be in DW_FORM_addr form.
2383 Unfortunately, some compilers happen to be violating this
2384 requirement by encoding addresses using other forms, such
2385 as DW_FORM_data4 for example. For those broken compilers,
2386 we try to do our best, without any guarantee of success,
2387 to interpret the address correctly. It would also be nice
2388 to generate a complaint, but that would require us to maintain
2389 a list of legitimate cases where a non-address form is allowed,
2390 as well as update callers to pass in at least the CU's DWARF
2391 version. This is more overhead than what we're willing to
2392 expand for a pretty rare case. */
2393 addr = DW_UNSND (attr);
2394 }
2395 else
2396 addr = DW_ADDR (attr);
2397
2398 return addr;
2399 }
2400
2401 /* The suffix for an index file. */
2402 #define INDEX4_SUFFIX ".gdb-index"
2403 #define INDEX5_SUFFIX ".debug_names"
2404 #define DEBUG_STR_SUFFIX ".debug_str"
2405
2406 /* See declaration. */
2407
2408 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2409 const dwarf2_debug_sections *names)
2410 : objfile (objfile_)
2411 {
2412 if (names == NULL)
2413 names = &dwarf2_elf_names;
2414
2415 bfd *obfd = objfile->obfd;
2416
2417 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2418 locate_sections (obfd, sec, *names);
2419 }
2420
2421 dwarf2_per_objfile::~dwarf2_per_objfile ()
2422 {
2423 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2424 free_cached_comp_units ();
2425
2426 if (quick_file_names_table)
2427 htab_delete (quick_file_names_table);
2428
2429 if (line_header_hash)
2430 htab_delete (line_header_hash);
2431
2432 /* Everything else should be on the objfile obstack. */
2433 }
2434
2435 /* See declaration. */
2436
2437 void
2438 dwarf2_per_objfile::free_cached_comp_units ()
2439 {
2440 dwarf2_per_cu_data *per_cu = read_in_chain;
2441 dwarf2_per_cu_data **last_chain = &read_in_chain;
2442 while (per_cu != NULL)
2443 {
2444 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2445
2446 delete per_cu->cu;
2447 *last_chain = next_cu;
2448 per_cu = next_cu;
2449 }
2450 }
2451
2452 /* Try to locate the sections we need for DWARF 2 debugging
2453 information and return true if we have enough to do something.
2454 NAMES points to the dwarf2 section names, or is NULL if the standard
2455 ELF names are used. */
2456
2457 int
2458 dwarf2_has_info (struct objfile *objfile,
2459 const struct dwarf2_debug_sections *names)
2460 {
2461 if (objfile->flags & OBJF_READNEVER)
2462 return 0;
2463
2464 struct dwarf2_per_objfile *dwarf2_per_objfile
2465 = get_dwarf2_per_objfile (objfile);
2466
2467 if (dwarf2_per_objfile == NULL)
2468 {
2469 /* Initialize per-objfile state. */
2470 struct dwarf2_per_objfile *data
2471 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
2472
2473 dwarf2_per_objfile = new (data) struct dwarf2_per_objfile (objfile, names);
2474 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
2475 }
2476 return (!dwarf2_per_objfile->info.is_virtual
2477 && dwarf2_per_objfile->info.s.section != NULL
2478 && !dwarf2_per_objfile->abbrev.is_virtual
2479 && dwarf2_per_objfile->abbrev.s.section != NULL);
2480 }
2481
2482 /* Return the containing section of virtual section SECTION. */
2483
2484 static struct dwarf2_section_info *
2485 get_containing_section (const struct dwarf2_section_info *section)
2486 {
2487 gdb_assert (section->is_virtual);
2488 return section->s.containing_section;
2489 }
2490
2491 /* Return the bfd owner of SECTION. */
2492
2493 static struct bfd *
2494 get_section_bfd_owner (const struct dwarf2_section_info *section)
2495 {
2496 if (section->is_virtual)
2497 {
2498 section = get_containing_section (section);
2499 gdb_assert (!section->is_virtual);
2500 }
2501 return section->s.section->owner;
2502 }
2503
2504 /* Return the bfd section of SECTION.
2505 Returns NULL if the section is not present. */
2506
2507 static asection *
2508 get_section_bfd_section (const struct dwarf2_section_info *section)
2509 {
2510 if (section->is_virtual)
2511 {
2512 section = get_containing_section (section);
2513 gdb_assert (!section->is_virtual);
2514 }
2515 return section->s.section;
2516 }
2517
2518 /* Return the name of SECTION. */
2519
2520 static const char *
2521 get_section_name (const struct dwarf2_section_info *section)
2522 {
2523 asection *sectp = get_section_bfd_section (section);
2524
2525 gdb_assert (sectp != NULL);
2526 return bfd_section_name (get_section_bfd_owner (section), sectp);
2527 }
2528
2529 /* Return the name of the file SECTION is in. */
2530
2531 static const char *
2532 get_section_file_name (const struct dwarf2_section_info *section)
2533 {
2534 bfd *abfd = get_section_bfd_owner (section);
2535
2536 return bfd_get_filename (abfd);
2537 }
2538
2539 /* Return the id of SECTION.
2540 Returns 0 if SECTION doesn't exist. */
2541
2542 static int
2543 get_section_id (const struct dwarf2_section_info *section)
2544 {
2545 asection *sectp = get_section_bfd_section (section);
2546
2547 if (sectp == NULL)
2548 return 0;
2549 return sectp->id;
2550 }
2551
2552 /* Return the flags of SECTION.
2553 SECTION (or containing section if this is a virtual section) must exist. */
2554
2555 static int
2556 get_section_flags (const struct dwarf2_section_info *section)
2557 {
2558 asection *sectp = get_section_bfd_section (section);
2559
2560 gdb_assert (sectp != NULL);
2561 return bfd_get_section_flags (sectp->owner, sectp);
2562 }
2563
2564 /* When loading sections, we look either for uncompressed section or for
2565 compressed section names. */
2566
2567 static int
2568 section_is_p (const char *section_name,
2569 const struct dwarf2_section_names *names)
2570 {
2571 if (names->normal != NULL
2572 && strcmp (section_name, names->normal) == 0)
2573 return 1;
2574 if (names->compressed != NULL
2575 && strcmp (section_name, names->compressed) == 0)
2576 return 1;
2577 return 0;
2578 }
2579
2580 /* See declaration. */
2581
2582 void
2583 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2584 const dwarf2_debug_sections &names)
2585 {
2586 flagword aflag = bfd_get_section_flags (abfd, sectp);
2587
2588 if ((aflag & SEC_HAS_CONTENTS) == 0)
2589 {
2590 }
2591 else if (section_is_p (sectp->name, &names.info))
2592 {
2593 this->info.s.section = sectp;
2594 this->info.size = bfd_get_section_size (sectp);
2595 }
2596 else if (section_is_p (sectp->name, &names.abbrev))
2597 {
2598 this->abbrev.s.section = sectp;
2599 this->abbrev.size = bfd_get_section_size (sectp);
2600 }
2601 else if (section_is_p (sectp->name, &names.line))
2602 {
2603 this->line.s.section = sectp;
2604 this->line.size = bfd_get_section_size (sectp);
2605 }
2606 else if (section_is_p (sectp->name, &names.loc))
2607 {
2608 this->loc.s.section = sectp;
2609 this->loc.size = bfd_get_section_size (sectp);
2610 }
2611 else if (section_is_p (sectp->name, &names.loclists))
2612 {
2613 this->loclists.s.section = sectp;
2614 this->loclists.size = bfd_get_section_size (sectp);
2615 }
2616 else if (section_is_p (sectp->name, &names.macinfo))
2617 {
2618 this->macinfo.s.section = sectp;
2619 this->macinfo.size = bfd_get_section_size (sectp);
2620 }
2621 else if (section_is_p (sectp->name, &names.macro))
2622 {
2623 this->macro.s.section = sectp;
2624 this->macro.size = bfd_get_section_size (sectp);
2625 }
2626 else if (section_is_p (sectp->name, &names.str))
2627 {
2628 this->str.s.section = sectp;
2629 this->str.size = bfd_get_section_size (sectp);
2630 }
2631 else if (section_is_p (sectp->name, &names.line_str))
2632 {
2633 this->line_str.s.section = sectp;
2634 this->line_str.size = bfd_get_section_size (sectp);
2635 }
2636 else if (section_is_p (sectp->name, &names.addr))
2637 {
2638 this->addr.s.section = sectp;
2639 this->addr.size = bfd_get_section_size (sectp);
2640 }
2641 else if (section_is_p (sectp->name, &names.frame))
2642 {
2643 this->frame.s.section = sectp;
2644 this->frame.size = bfd_get_section_size (sectp);
2645 }
2646 else if (section_is_p (sectp->name, &names.eh_frame))
2647 {
2648 this->eh_frame.s.section = sectp;
2649 this->eh_frame.size = bfd_get_section_size (sectp);
2650 }
2651 else if (section_is_p (sectp->name, &names.ranges))
2652 {
2653 this->ranges.s.section = sectp;
2654 this->ranges.size = bfd_get_section_size (sectp);
2655 }
2656 else if (section_is_p (sectp->name, &names.rnglists))
2657 {
2658 this->rnglists.s.section = sectp;
2659 this->rnglists.size = bfd_get_section_size (sectp);
2660 }
2661 else if (section_is_p (sectp->name, &names.types))
2662 {
2663 struct dwarf2_section_info type_section;
2664
2665 memset (&type_section, 0, sizeof (type_section));
2666 type_section.s.section = sectp;
2667 type_section.size = bfd_get_section_size (sectp);
2668
2669 VEC_safe_push (dwarf2_section_info_def, this->types,
2670 &type_section);
2671 }
2672 else if (section_is_p (sectp->name, &names.gdb_index))
2673 {
2674 this->gdb_index.s.section = sectp;
2675 this->gdb_index.size = bfd_get_section_size (sectp);
2676 }
2677 else if (section_is_p (sectp->name, &names.debug_names))
2678 {
2679 this->debug_names.s.section = sectp;
2680 this->debug_names.size = bfd_get_section_size (sectp);
2681 }
2682 else if (section_is_p (sectp->name, &names.debug_aranges))
2683 {
2684 this->debug_aranges.s.section = sectp;
2685 this->debug_aranges.size = bfd_get_section_size (sectp);
2686 }
2687
2688 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2689 && bfd_section_vma (abfd, sectp) == 0)
2690 this->has_section_at_zero = true;
2691 }
2692
2693 /* A helper function that decides whether a section is empty,
2694 or not present. */
2695
2696 static int
2697 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2698 {
2699 if (section->is_virtual)
2700 return section->size == 0;
2701 return section->s.section == NULL || section->size == 0;
2702 }
2703
2704 /* Read the contents of the section INFO.
2705 OBJFILE is the main object file, but not necessarily the file where
2706 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2707 of the DWO file.
2708 If the section is compressed, uncompress it before returning. */
2709
2710 static void
2711 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2712 {
2713 asection *sectp;
2714 bfd *abfd;
2715 gdb_byte *buf, *retbuf;
2716
2717 if (info->readin)
2718 return;
2719 info->buffer = NULL;
2720 info->readin = 1;
2721
2722 if (dwarf2_section_empty_p (info))
2723 return;
2724
2725 sectp = get_section_bfd_section (info);
2726
2727 /* If this is a virtual section we need to read in the real one first. */
2728 if (info->is_virtual)
2729 {
2730 struct dwarf2_section_info *containing_section =
2731 get_containing_section (info);
2732
2733 gdb_assert (sectp != NULL);
2734 if ((sectp->flags & SEC_RELOC) != 0)
2735 {
2736 error (_("Dwarf Error: DWP format V2 with relocations is not"
2737 " supported in section %s [in module %s]"),
2738 get_section_name (info), get_section_file_name (info));
2739 }
2740 dwarf2_read_section (objfile, containing_section);
2741 /* Other code should have already caught virtual sections that don't
2742 fit. */
2743 gdb_assert (info->virtual_offset + info->size
2744 <= containing_section->size);
2745 /* If the real section is empty or there was a problem reading the
2746 section we shouldn't get here. */
2747 gdb_assert (containing_section->buffer != NULL);
2748 info->buffer = containing_section->buffer + info->virtual_offset;
2749 return;
2750 }
2751
2752 /* If the section has relocations, we must read it ourselves.
2753 Otherwise we attach it to the BFD. */
2754 if ((sectp->flags & SEC_RELOC) == 0)
2755 {
2756 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2757 return;
2758 }
2759
2760 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2761 info->buffer = buf;
2762
2763 /* When debugging .o files, we may need to apply relocations; see
2764 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2765 We never compress sections in .o files, so we only need to
2766 try this when the section is not compressed. */
2767 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2768 if (retbuf != NULL)
2769 {
2770 info->buffer = retbuf;
2771 return;
2772 }
2773
2774 abfd = get_section_bfd_owner (info);
2775 gdb_assert (abfd != NULL);
2776
2777 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2778 || bfd_bread (buf, info->size, abfd) != info->size)
2779 {
2780 error (_("Dwarf Error: Can't read DWARF data"
2781 " in section %s [in module %s]"),
2782 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2783 }
2784 }
2785
2786 /* A helper function that returns the size of a section in a safe way.
2787 If you are positive that the section has been read before using the
2788 size, then it is safe to refer to the dwarf2_section_info object's
2789 "size" field directly. In other cases, you must call this
2790 function, because for compressed sections the size field is not set
2791 correctly until the section has been read. */
2792
2793 static bfd_size_type
2794 dwarf2_section_size (struct objfile *objfile,
2795 struct dwarf2_section_info *info)
2796 {
2797 if (!info->readin)
2798 dwarf2_read_section (objfile, info);
2799 return info->size;
2800 }
2801
2802 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2803 SECTION_NAME. */
2804
2805 void
2806 dwarf2_get_section_info (struct objfile *objfile,
2807 enum dwarf2_section_enum sect,
2808 asection **sectp, const gdb_byte **bufp,
2809 bfd_size_type *sizep)
2810 {
2811 struct dwarf2_per_objfile *data
2812 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2813 dwarf2_objfile_data_key);
2814 struct dwarf2_section_info *info;
2815
2816 /* We may see an objfile without any DWARF, in which case we just
2817 return nothing. */
2818 if (data == NULL)
2819 {
2820 *sectp = NULL;
2821 *bufp = NULL;
2822 *sizep = 0;
2823 return;
2824 }
2825 switch (sect)
2826 {
2827 case DWARF2_DEBUG_FRAME:
2828 info = &data->frame;
2829 break;
2830 case DWARF2_EH_FRAME:
2831 info = &data->eh_frame;
2832 break;
2833 default:
2834 gdb_assert_not_reached ("unexpected section");
2835 }
2836
2837 dwarf2_read_section (objfile, info);
2838
2839 *sectp = get_section_bfd_section (info);
2840 *bufp = info->buffer;
2841 *sizep = info->size;
2842 }
2843
2844 /* A helper function to find the sections for a .dwz file. */
2845
2846 static void
2847 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2848 {
2849 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2850
2851 /* Note that we only support the standard ELF names, because .dwz
2852 is ELF-only (at the time of writing). */
2853 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2854 {
2855 dwz_file->abbrev.s.section = sectp;
2856 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2857 }
2858 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2859 {
2860 dwz_file->info.s.section = sectp;
2861 dwz_file->info.size = bfd_get_section_size (sectp);
2862 }
2863 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2864 {
2865 dwz_file->str.s.section = sectp;
2866 dwz_file->str.size = bfd_get_section_size (sectp);
2867 }
2868 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2869 {
2870 dwz_file->line.s.section = sectp;
2871 dwz_file->line.size = bfd_get_section_size (sectp);
2872 }
2873 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2874 {
2875 dwz_file->macro.s.section = sectp;
2876 dwz_file->macro.size = bfd_get_section_size (sectp);
2877 }
2878 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2879 {
2880 dwz_file->gdb_index.s.section = sectp;
2881 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2882 }
2883 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2884 {
2885 dwz_file->debug_names.s.section = sectp;
2886 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2887 }
2888 }
2889
2890 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2891 there is no .gnu_debugaltlink section in the file. Error if there
2892 is such a section but the file cannot be found. */
2893
2894 static struct dwz_file *
2895 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2896 {
2897 const char *filename;
2898 struct dwz_file *result;
2899 bfd_size_type buildid_len_arg;
2900 size_t buildid_len;
2901 bfd_byte *buildid;
2902
2903 if (dwarf2_per_objfile->dwz_file != NULL)
2904 return dwarf2_per_objfile->dwz_file;
2905
2906 bfd_set_error (bfd_error_no_error);
2907 gdb::unique_xmalloc_ptr<char> data
2908 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2909 &buildid_len_arg, &buildid));
2910 if (data == NULL)
2911 {
2912 if (bfd_get_error () == bfd_error_no_error)
2913 return NULL;
2914 error (_("could not read '.gnu_debugaltlink' section: %s"),
2915 bfd_errmsg (bfd_get_error ()));
2916 }
2917
2918 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2919
2920 buildid_len = (size_t) buildid_len_arg;
2921
2922 filename = data.get ();
2923
2924 std::string abs_storage;
2925 if (!IS_ABSOLUTE_PATH (filename))
2926 {
2927 gdb::unique_xmalloc_ptr<char> abs
2928 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2929
2930 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2931 filename = abs_storage.c_str ();
2932 }
2933
2934 /* First try the file name given in the section. If that doesn't
2935 work, try to use the build-id instead. */
2936 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2937 if (dwz_bfd != NULL)
2938 {
2939 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2940 dwz_bfd.release ();
2941 }
2942
2943 if (dwz_bfd == NULL)
2944 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2945
2946 if (dwz_bfd == NULL)
2947 error (_("could not find '.gnu_debugaltlink' file for %s"),
2948 objfile_name (dwarf2_per_objfile->objfile));
2949
2950 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2951 struct dwz_file);
2952 result->dwz_bfd = dwz_bfd.release ();
2953
2954 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2955
2956 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2957 dwarf2_per_objfile->dwz_file = result;
2958 return result;
2959 }
2960 \f
2961 /* DWARF quick_symbols_functions support. */
2962
2963 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2964 unique line tables, so we maintain a separate table of all .debug_line
2965 derived entries to support the sharing.
2966 All the quick functions need is the list of file names. We discard the
2967 line_header when we're done and don't need to record it here. */
2968 struct quick_file_names
2969 {
2970 /* The data used to construct the hash key. */
2971 struct stmt_list_hash hash;
2972
2973 /* The number of entries in file_names, real_names. */
2974 unsigned int num_file_names;
2975
2976 /* The file names from the line table, after being run through
2977 file_full_name. */
2978 const char **file_names;
2979
2980 /* The file names from the line table after being run through
2981 gdb_realpath. These are computed lazily. */
2982 const char **real_names;
2983 };
2984
2985 /* When using the index (and thus not using psymtabs), each CU has an
2986 object of this type. This is used to hold information needed by
2987 the various "quick" methods. */
2988 struct dwarf2_per_cu_quick_data
2989 {
2990 /* The file table. This can be NULL if there was no file table
2991 or it's currently not read in.
2992 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2993 struct quick_file_names *file_names;
2994
2995 /* The corresponding symbol table. This is NULL if symbols for this
2996 CU have not yet been read. */
2997 struct compunit_symtab *compunit_symtab;
2998
2999 /* A temporary mark bit used when iterating over all CUs in
3000 expand_symtabs_matching. */
3001 unsigned int mark : 1;
3002
3003 /* True if we've tried to read the file table and found there isn't one.
3004 There will be no point in trying to read it again next time. */
3005 unsigned int no_file_data : 1;
3006 };
3007
3008 /* Utility hash function for a stmt_list_hash. */
3009
3010 static hashval_t
3011 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
3012 {
3013 hashval_t v = 0;
3014
3015 if (stmt_list_hash->dwo_unit != NULL)
3016 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
3017 v += to_underlying (stmt_list_hash->line_sect_off);
3018 return v;
3019 }
3020
3021 /* Utility equality function for a stmt_list_hash. */
3022
3023 static int
3024 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
3025 const struct stmt_list_hash *rhs)
3026 {
3027 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
3028 return 0;
3029 if (lhs->dwo_unit != NULL
3030 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
3031 return 0;
3032
3033 return lhs->line_sect_off == rhs->line_sect_off;
3034 }
3035
3036 /* Hash function for a quick_file_names. */
3037
3038 static hashval_t
3039 hash_file_name_entry (const void *e)
3040 {
3041 const struct quick_file_names *file_data
3042 = (const struct quick_file_names *) e;
3043
3044 return hash_stmt_list_entry (&file_data->hash);
3045 }
3046
3047 /* Equality function for a quick_file_names. */
3048
3049 static int
3050 eq_file_name_entry (const void *a, const void *b)
3051 {
3052 const struct quick_file_names *ea = (const struct quick_file_names *) a;
3053 const struct quick_file_names *eb = (const struct quick_file_names *) b;
3054
3055 return eq_stmt_list_entry (&ea->hash, &eb->hash);
3056 }
3057
3058 /* Delete function for a quick_file_names. */
3059
3060 static void
3061 delete_file_name_entry (void *e)
3062 {
3063 struct quick_file_names *file_data = (struct quick_file_names *) e;
3064 int i;
3065
3066 for (i = 0; i < file_data->num_file_names; ++i)
3067 {
3068 xfree ((void*) file_data->file_names[i]);
3069 if (file_data->real_names)
3070 xfree ((void*) file_data->real_names[i]);
3071 }
3072
3073 /* The space for the struct itself lives on objfile_obstack,
3074 so we don't free it here. */
3075 }
3076
3077 /* Create a quick_file_names hash table. */
3078
3079 static htab_t
3080 create_quick_file_names_table (unsigned int nr_initial_entries)
3081 {
3082 return htab_create_alloc (nr_initial_entries,
3083 hash_file_name_entry, eq_file_name_entry,
3084 delete_file_name_entry, xcalloc, xfree);
3085 }
3086
3087 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3088 have to be created afterwards. You should call age_cached_comp_units after
3089 processing PER_CU->CU. dw2_setup must have been already called. */
3090
3091 static void
3092 load_cu (struct dwarf2_per_cu_data *per_cu)
3093 {
3094 if (per_cu->is_debug_types)
3095 load_full_type_unit (per_cu);
3096 else
3097 load_full_comp_unit (per_cu, language_minimal);
3098
3099 if (per_cu->cu == NULL)
3100 return; /* Dummy CU. */
3101
3102 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
3103 }
3104
3105 /* Read in the symbols for PER_CU. */
3106
3107 static void
3108 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3109 {
3110 struct cleanup *back_to;
3111 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3112
3113 /* Skip type_unit_groups, reading the type units they contain
3114 is handled elsewhere. */
3115 if (IS_TYPE_UNIT_GROUP (per_cu))
3116 return;
3117
3118 back_to = make_cleanup (dwarf2_release_queue, NULL);
3119
3120 if (dwarf2_per_objfile->using_index
3121 ? per_cu->v.quick->compunit_symtab == NULL
3122 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3123 {
3124 queue_comp_unit (per_cu, language_minimal);
3125 load_cu (per_cu);
3126
3127 /* If we just loaded a CU from a DWO, and we're working with an index
3128 that may badly handle TUs, load all the TUs in that DWO as well.
3129 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3130 if (!per_cu->is_debug_types
3131 && per_cu->cu != NULL
3132 && per_cu->cu->dwo_unit != NULL
3133 && dwarf2_per_objfile->index_table != NULL
3134 && dwarf2_per_objfile->index_table->version <= 7
3135 /* DWP files aren't supported yet. */
3136 && get_dwp_file (dwarf2_per_objfile) == NULL)
3137 queue_and_load_all_dwo_tus (per_cu);
3138 }
3139
3140 process_queue (dwarf2_per_objfile);
3141
3142 /* Age the cache, releasing compilation units that have not
3143 been used recently. */
3144 age_cached_comp_units (dwarf2_per_objfile);
3145
3146 do_cleanups (back_to);
3147 }
3148
3149 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3150 the objfile from which this CU came. Returns the resulting symbol
3151 table. */
3152
3153 static struct compunit_symtab *
3154 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3155 {
3156 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3157
3158 gdb_assert (dwarf2_per_objfile->using_index);
3159 if (!per_cu->v.quick->compunit_symtab)
3160 {
3161 struct cleanup *back_to = make_cleanup (free_cached_comp_units,
3162 dwarf2_per_objfile);
3163 scoped_restore decrementer = increment_reading_symtab ();
3164 dw2_do_instantiate_symtab (per_cu);
3165 process_cu_includes (dwarf2_per_objfile);
3166 do_cleanups (back_to);
3167 }
3168
3169 return per_cu->v.quick->compunit_symtab;
3170 }
3171
3172 /* Return the CU/TU given its index.
3173
3174 This is intended for loops like:
3175
3176 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3177 + dwarf2_per_objfile->n_type_units); ++i)
3178 {
3179 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3180
3181 ...;
3182 }
3183 */
3184
3185 static struct dwarf2_per_cu_data *
3186 dw2_get_cutu (struct dwarf2_per_objfile *dwarf2_per_objfile,
3187 int index)
3188 {
3189 if (index >= dwarf2_per_objfile->n_comp_units)
3190 {
3191 index -= dwarf2_per_objfile->n_comp_units;
3192 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3193 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
3194 }
3195
3196 return dwarf2_per_objfile->all_comp_units[index];
3197 }
3198
3199 /* Return the CU given its index.
3200 This differs from dw2_get_cutu in that it's for when you know INDEX
3201 refers to a CU. */
3202
3203 static struct dwarf2_per_cu_data *
3204 dw2_get_cu (struct dwarf2_per_objfile *dwarf2_per_objfile, int index)
3205 {
3206 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
3207
3208 return dwarf2_per_objfile->all_comp_units[index];
3209 }
3210
3211 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3212 objfile_obstack, and constructed with the specified field
3213 values. */
3214
3215 static dwarf2_per_cu_data *
3216 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3217 struct dwarf2_section_info *section,
3218 int is_dwz,
3219 sect_offset sect_off, ULONGEST length)
3220 {
3221 struct objfile *objfile = dwarf2_per_objfile->objfile;
3222 dwarf2_per_cu_data *the_cu
3223 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3224 struct dwarf2_per_cu_data);
3225 the_cu->sect_off = sect_off;
3226 the_cu->length = length;
3227 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
3228 the_cu->section = section;
3229 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3230 struct dwarf2_per_cu_quick_data);
3231 the_cu->is_dwz = is_dwz;
3232 return the_cu;
3233 }
3234
3235 /* A helper for create_cus_from_index that handles a given list of
3236 CUs. */
3237
3238 static void
3239 create_cus_from_index_list (struct objfile *objfile,
3240 const gdb_byte *cu_list, offset_type n_elements,
3241 struct dwarf2_section_info *section,
3242 int is_dwz,
3243 int base_offset)
3244 {
3245 offset_type i;
3246 struct dwarf2_per_objfile *dwarf2_per_objfile
3247 = get_dwarf2_per_objfile (objfile);
3248
3249 for (i = 0; i < n_elements; i += 2)
3250 {
3251 gdb_static_assert (sizeof (ULONGEST) >= 8);
3252
3253 sect_offset sect_off
3254 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3255 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3256 cu_list += 2 * 8;
3257
3258 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
3259 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3260 sect_off, length);
3261 }
3262 }
3263
3264 /* Read the CU list from the mapped index, and use it to create all
3265 the CU objects for this objfile. */
3266
3267 static void
3268 create_cus_from_index (struct objfile *objfile,
3269 const gdb_byte *cu_list, offset_type cu_list_elements,
3270 const gdb_byte *dwz_list, offset_type dwz_elements)
3271 {
3272 struct dwz_file *dwz;
3273 struct dwarf2_per_objfile *dwarf2_per_objfile
3274 = get_dwarf2_per_objfile (objfile);
3275
3276 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3277 dwarf2_per_objfile->all_comp_units =
3278 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3279 dwarf2_per_objfile->n_comp_units);
3280
3281 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3282 &dwarf2_per_objfile->info, 0, 0);
3283
3284 if (dwz_elements == 0)
3285 return;
3286
3287 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3288 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3289 cu_list_elements / 2);
3290 }
3291
3292 /* Create the signatured type hash table from the index. */
3293
3294 static void
3295 create_signatured_type_table_from_index (struct objfile *objfile,
3296 struct dwarf2_section_info *section,
3297 const gdb_byte *bytes,
3298 offset_type elements)
3299 {
3300 offset_type i;
3301 htab_t sig_types_hash;
3302 struct dwarf2_per_objfile *dwarf2_per_objfile
3303 = get_dwarf2_per_objfile (objfile);
3304
3305 dwarf2_per_objfile->n_type_units
3306 = dwarf2_per_objfile->n_allocated_type_units
3307 = elements / 3;
3308 dwarf2_per_objfile->all_type_units =
3309 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3310
3311 sig_types_hash = allocate_signatured_type_table (objfile);
3312
3313 for (i = 0; i < elements; i += 3)
3314 {
3315 struct signatured_type *sig_type;
3316 ULONGEST signature;
3317 void **slot;
3318 cu_offset type_offset_in_tu;
3319
3320 gdb_static_assert (sizeof (ULONGEST) >= 8);
3321 sect_offset sect_off
3322 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3323 type_offset_in_tu
3324 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3325 BFD_ENDIAN_LITTLE);
3326 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3327 bytes += 3 * 8;
3328
3329 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3330 struct signatured_type);
3331 sig_type->signature = signature;
3332 sig_type->type_offset_in_tu = type_offset_in_tu;
3333 sig_type->per_cu.is_debug_types = 1;
3334 sig_type->per_cu.section = section;
3335 sig_type->per_cu.sect_off = sect_off;
3336 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3337 sig_type->per_cu.v.quick
3338 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3339 struct dwarf2_per_cu_quick_data);
3340
3341 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3342 *slot = sig_type;
3343
3344 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3345 }
3346
3347 dwarf2_per_objfile->signatured_types = sig_types_hash;
3348 }
3349
3350 /* Create the signatured type hash table from .debug_names. */
3351
3352 static void
3353 create_signatured_type_table_from_debug_names
3354 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3355 const mapped_debug_names &map,
3356 struct dwarf2_section_info *section,
3357 struct dwarf2_section_info *abbrev_section)
3358 {
3359 struct objfile *objfile = dwarf2_per_objfile->objfile;
3360
3361 dwarf2_read_section (objfile, section);
3362 dwarf2_read_section (objfile, abbrev_section);
3363
3364 dwarf2_per_objfile->n_type_units
3365 = dwarf2_per_objfile->n_allocated_type_units
3366 = map.tu_count;
3367 dwarf2_per_objfile->all_type_units
3368 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3369
3370 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3371
3372 for (uint32_t i = 0; i < map.tu_count; ++i)
3373 {
3374 struct signatured_type *sig_type;
3375 ULONGEST signature;
3376 void **slot;
3377 cu_offset type_offset_in_tu;
3378
3379 sect_offset sect_off
3380 = (sect_offset) (extract_unsigned_integer
3381 (map.tu_table_reordered + i * map.offset_size,
3382 map.offset_size,
3383 map.dwarf5_byte_order));
3384
3385 comp_unit_head cu_header;
3386 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3387 abbrev_section,
3388 section->buffer + to_underlying (sect_off),
3389 rcuh_kind::TYPE);
3390
3391 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3392 struct signatured_type);
3393 sig_type->signature = cu_header.signature;
3394 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3395 sig_type->per_cu.is_debug_types = 1;
3396 sig_type->per_cu.section = section;
3397 sig_type->per_cu.sect_off = sect_off;
3398 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3399 sig_type->per_cu.v.quick
3400 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3401 struct dwarf2_per_cu_quick_data);
3402
3403 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3404 *slot = sig_type;
3405
3406 dwarf2_per_objfile->all_type_units[i] = sig_type;
3407 }
3408
3409 dwarf2_per_objfile->signatured_types = sig_types_hash;
3410 }
3411
3412 /* Read the address map data from the mapped index, and use it to
3413 populate the objfile's psymtabs_addrmap. */
3414
3415 static void
3416 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3417 struct mapped_index *index)
3418 {
3419 struct objfile *objfile = dwarf2_per_objfile->objfile;
3420 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3421 const gdb_byte *iter, *end;
3422 struct addrmap *mutable_map;
3423 CORE_ADDR baseaddr;
3424
3425 auto_obstack temp_obstack;
3426
3427 mutable_map = addrmap_create_mutable (&temp_obstack);
3428
3429 iter = index->address_table.data ();
3430 end = iter + index->address_table.size ();
3431
3432 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3433
3434 while (iter < end)
3435 {
3436 ULONGEST hi, lo, cu_index;
3437 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3438 iter += 8;
3439 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3440 iter += 8;
3441 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3442 iter += 4;
3443
3444 if (lo > hi)
3445 {
3446 complaint (&symfile_complaints,
3447 _(".gdb_index address table has invalid range (%s - %s)"),
3448 hex_string (lo), hex_string (hi));
3449 continue;
3450 }
3451
3452 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3453 {
3454 complaint (&symfile_complaints,
3455 _(".gdb_index address table has invalid CU number %u"),
3456 (unsigned) cu_index);
3457 continue;
3458 }
3459
3460 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3461 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3462 addrmap_set_empty (mutable_map, lo, hi - 1,
3463 dw2_get_cutu (dwarf2_per_objfile, cu_index));
3464 }
3465
3466 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3467 &objfile->objfile_obstack);
3468 }
3469
3470 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3471 populate the objfile's psymtabs_addrmap. */
3472
3473 static void
3474 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3475 struct dwarf2_section_info *section)
3476 {
3477 struct objfile *objfile = dwarf2_per_objfile->objfile;
3478 bfd *abfd = objfile->obfd;
3479 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3480 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3481 SECT_OFF_TEXT (objfile));
3482
3483 auto_obstack temp_obstack;
3484 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3485
3486 std::unordered_map<sect_offset,
3487 dwarf2_per_cu_data *,
3488 gdb::hash_enum<sect_offset>>
3489 debug_info_offset_to_per_cu;
3490 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3491 {
3492 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, cui);
3493 const auto insertpair
3494 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3495 if (!insertpair.second)
3496 {
3497 warning (_("Section .debug_aranges in %s has duplicate "
3498 "debug_info_offset %u, ignoring .debug_aranges."),
3499 objfile_name (objfile), to_underlying (per_cu->sect_off));
3500 return;
3501 }
3502 }
3503
3504 dwarf2_read_section (objfile, section);
3505
3506 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3507
3508 const gdb_byte *addr = section->buffer;
3509
3510 while (addr < section->buffer + section->size)
3511 {
3512 const gdb_byte *const entry_addr = addr;
3513 unsigned int bytes_read;
3514
3515 const LONGEST entry_length = read_initial_length (abfd, addr,
3516 &bytes_read);
3517 addr += bytes_read;
3518
3519 const gdb_byte *const entry_end = addr + entry_length;
3520 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3521 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3522 if (addr + entry_length > section->buffer + section->size)
3523 {
3524 warning (_("Section .debug_aranges in %s entry at offset %zu "
3525 "length %s exceeds section length %s, "
3526 "ignoring .debug_aranges."),
3527 objfile_name (objfile), entry_addr - section->buffer,
3528 plongest (bytes_read + entry_length),
3529 pulongest (section->size));
3530 return;
3531 }
3532
3533 /* The version number. */
3534 const uint16_t version = read_2_bytes (abfd, addr);
3535 addr += 2;
3536 if (version != 2)
3537 {
3538 warning (_("Section .debug_aranges in %s entry at offset %zu "
3539 "has unsupported version %d, ignoring .debug_aranges."),
3540 objfile_name (objfile), entry_addr - section->buffer,
3541 version);
3542 return;
3543 }
3544
3545 const uint64_t debug_info_offset
3546 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3547 addr += offset_size;
3548 const auto per_cu_it
3549 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3550 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3551 {
3552 warning (_("Section .debug_aranges in %s entry at offset %zu "
3553 "debug_info_offset %s does not exists, "
3554 "ignoring .debug_aranges."),
3555 objfile_name (objfile), entry_addr - section->buffer,
3556 pulongest (debug_info_offset));
3557 return;
3558 }
3559 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3560
3561 const uint8_t address_size = *addr++;
3562 if (address_size < 1 || address_size > 8)
3563 {
3564 warning (_("Section .debug_aranges in %s entry at offset %zu "
3565 "address_size %u is invalid, ignoring .debug_aranges."),
3566 objfile_name (objfile), entry_addr - section->buffer,
3567 address_size);
3568 return;
3569 }
3570
3571 const uint8_t segment_selector_size = *addr++;
3572 if (segment_selector_size != 0)
3573 {
3574 warning (_("Section .debug_aranges in %s entry at offset %zu "
3575 "segment_selector_size %u is not supported, "
3576 "ignoring .debug_aranges."),
3577 objfile_name (objfile), entry_addr - section->buffer,
3578 segment_selector_size);
3579 return;
3580 }
3581
3582 /* Must pad to an alignment boundary that is twice the address
3583 size. It is undocumented by the DWARF standard but GCC does
3584 use it. */
3585 for (size_t padding = ((-(addr - section->buffer))
3586 & (2 * address_size - 1));
3587 padding > 0; padding--)
3588 if (*addr++ != 0)
3589 {
3590 warning (_("Section .debug_aranges in %s entry at offset %zu "
3591 "padding is not zero, ignoring .debug_aranges."),
3592 objfile_name (objfile), entry_addr - section->buffer);
3593 return;
3594 }
3595
3596 for (;;)
3597 {
3598 if (addr + 2 * address_size > entry_end)
3599 {
3600 warning (_("Section .debug_aranges in %s entry at offset %zu "
3601 "address list is not properly terminated, "
3602 "ignoring .debug_aranges."),
3603 objfile_name (objfile), entry_addr - section->buffer);
3604 return;
3605 }
3606 ULONGEST start = extract_unsigned_integer (addr, address_size,
3607 dwarf5_byte_order);
3608 addr += address_size;
3609 ULONGEST length = extract_unsigned_integer (addr, address_size,
3610 dwarf5_byte_order);
3611 addr += address_size;
3612 if (start == 0 && length == 0)
3613 break;
3614 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3615 {
3616 /* Symbol was eliminated due to a COMDAT group. */
3617 continue;
3618 }
3619 ULONGEST end = start + length;
3620 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3621 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3622 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3623 }
3624 }
3625
3626 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3627 &objfile->objfile_obstack);
3628 }
3629
3630 /* The hash function for strings in the mapped index. This is the same as
3631 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3632 implementation. This is necessary because the hash function is tied to the
3633 format of the mapped index file. The hash values do not have to match with
3634 SYMBOL_HASH_NEXT.
3635
3636 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3637
3638 static hashval_t
3639 mapped_index_string_hash (int index_version, const void *p)
3640 {
3641 const unsigned char *str = (const unsigned char *) p;
3642 hashval_t r = 0;
3643 unsigned char c;
3644
3645 while ((c = *str++) != 0)
3646 {
3647 if (index_version >= 5)
3648 c = tolower (c);
3649 r = r * 67 + c - 113;
3650 }
3651
3652 return r;
3653 }
3654
3655 /* Find a slot in the mapped index INDEX for the object named NAME.
3656 If NAME is found, set *VEC_OUT to point to the CU vector in the
3657 constant pool and return true. If NAME cannot be found, return
3658 false. */
3659
3660 static bool
3661 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3662 offset_type **vec_out)
3663 {
3664 offset_type hash;
3665 offset_type slot, step;
3666 int (*cmp) (const char *, const char *);
3667
3668 gdb::unique_xmalloc_ptr<char> without_params;
3669 if (current_language->la_language == language_cplus
3670 || current_language->la_language == language_fortran
3671 || current_language->la_language == language_d)
3672 {
3673 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3674 not contain any. */
3675
3676 if (strchr (name, '(') != NULL)
3677 {
3678 without_params = cp_remove_params (name);
3679
3680 if (without_params != NULL)
3681 name = without_params.get ();
3682 }
3683 }
3684
3685 /* Index version 4 did not support case insensitive searches. But the
3686 indices for case insensitive languages are built in lowercase, therefore
3687 simulate our NAME being searched is also lowercased. */
3688 hash = mapped_index_string_hash ((index->version == 4
3689 && case_sensitivity == case_sensitive_off
3690 ? 5 : index->version),
3691 name);
3692
3693 slot = hash & (index->symbol_table.size () - 1);
3694 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3695 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3696
3697 for (;;)
3698 {
3699 const char *str;
3700
3701 const auto &bucket = index->symbol_table[slot];
3702 if (bucket.name == 0 && bucket.vec == 0)
3703 return false;
3704
3705 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3706 if (!cmp (name, str))
3707 {
3708 *vec_out = (offset_type *) (index->constant_pool
3709 + MAYBE_SWAP (bucket.vec));
3710 return true;
3711 }
3712
3713 slot = (slot + step) & (index->symbol_table.size () - 1);
3714 }
3715 }
3716
3717 /* A helper function that reads the .gdb_index from SECTION and fills
3718 in MAP. FILENAME is the name of the file containing the section;
3719 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3720 ok to use deprecated sections.
3721
3722 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3723 out parameters that are filled in with information about the CU and
3724 TU lists in the section.
3725
3726 Returns 1 if all went well, 0 otherwise. */
3727
3728 static int
3729 read_index_from_section (struct objfile *objfile,
3730 const char *filename,
3731 int deprecated_ok,
3732 struct dwarf2_section_info *section,
3733 struct mapped_index *map,
3734 const gdb_byte **cu_list,
3735 offset_type *cu_list_elements,
3736 const gdb_byte **types_list,
3737 offset_type *types_list_elements)
3738 {
3739 const gdb_byte *addr;
3740 offset_type version;
3741 offset_type *metadata;
3742 int i;
3743
3744 if (dwarf2_section_empty_p (section))
3745 return 0;
3746
3747 /* Older elfutils strip versions could keep the section in the main
3748 executable while splitting it for the separate debug info file. */
3749 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3750 return 0;
3751
3752 dwarf2_read_section (objfile, section);
3753
3754 addr = section->buffer;
3755 /* Version check. */
3756 version = MAYBE_SWAP (*(offset_type *) addr);
3757 /* Versions earlier than 3 emitted every copy of a psymbol. This
3758 causes the index to behave very poorly for certain requests. Version 3
3759 contained incomplete addrmap. So, it seems better to just ignore such
3760 indices. */
3761 if (version < 4)
3762 {
3763 static int warning_printed = 0;
3764 if (!warning_printed)
3765 {
3766 warning (_("Skipping obsolete .gdb_index section in %s."),
3767 filename);
3768 warning_printed = 1;
3769 }
3770 return 0;
3771 }
3772 /* Index version 4 uses a different hash function than index version
3773 5 and later.
3774
3775 Versions earlier than 6 did not emit psymbols for inlined
3776 functions. Using these files will cause GDB not to be able to
3777 set breakpoints on inlined functions by name, so we ignore these
3778 indices unless the user has done
3779 "set use-deprecated-index-sections on". */
3780 if (version < 6 && !deprecated_ok)
3781 {
3782 static int warning_printed = 0;
3783 if (!warning_printed)
3784 {
3785 warning (_("\
3786 Skipping deprecated .gdb_index section in %s.\n\
3787 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3788 to use the section anyway."),
3789 filename);
3790 warning_printed = 1;
3791 }
3792 return 0;
3793 }
3794 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3795 of the TU (for symbols coming from TUs),
3796 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3797 Plus gold-generated indices can have duplicate entries for global symbols,
3798 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3799 These are just performance bugs, and we can't distinguish gdb-generated
3800 indices from gold-generated ones, so issue no warning here. */
3801
3802 /* Indexes with higher version than the one supported by GDB may be no
3803 longer backward compatible. */
3804 if (version > 8)
3805 return 0;
3806
3807 map->version = version;
3808 map->total_size = section->size;
3809
3810 metadata = (offset_type *) (addr + sizeof (offset_type));
3811
3812 i = 0;
3813 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3814 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3815 / 8);
3816 ++i;
3817
3818 *types_list = addr + MAYBE_SWAP (metadata[i]);
3819 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3820 - MAYBE_SWAP (metadata[i]))
3821 / 8);
3822 ++i;
3823
3824 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3825 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3826 map->address_table
3827 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3828 ++i;
3829
3830 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3831 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3832 map->symbol_table
3833 = gdb::array_view<mapped_index::symbol_table_slot>
3834 ((mapped_index::symbol_table_slot *) symbol_table,
3835 (mapped_index::symbol_table_slot *) symbol_table_end);
3836
3837 ++i;
3838 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3839
3840 return 1;
3841 }
3842
3843 /* Read .gdb_index. If everything went ok, initialize the "quick"
3844 elements of all the CUs and return 1. Otherwise, return 0. */
3845
3846 static int
3847 dwarf2_read_index (struct objfile *objfile)
3848 {
3849 struct mapped_index local_map, *map;
3850 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3851 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3852 struct dwz_file *dwz;
3853 struct dwarf2_per_objfile *dwarf2_per_objfile
3854 = get_dwarf2_per_objfile (objfile);
3855
3856 if (!read_index_from_section (objfile, objfile_name (objfile),
3857 use_deprecated_index_sections,
3858 &dwarf2_per_objfile->gdb_index, &local_map,
3859 &cu_list, &cu_list_elements,
3860 &types_list, &types_list_elements))
3861 return 0;
3862
3863 /* Don't use the index if it's empty. */
3864 if (local_map.symbol_table.empty ())
3865 return 0;
3866
3867 /* If there is a .dwz file, read it so we can get its CU list as
3868 well. */
3869 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3870 if (dwz != NULL)
3871 {
3872 struct mapped_index dwz_map;
3873 const gdb_byte *dwz_types_ignore;
3874 offset_type dwz_types_elements_ignore;
3875
3876 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3877 1,
3878 &dwz->gdb_index, &dwz_map,
3879 &dwz_list, &dwz_list_elements,
3880 &dwz_types_ignore,
3881 &dwz_types_elements_ignore))
3882 {
3883 warning (_("could not read '.gdb_index' section from %s; skipping"),
3884 bfd_get_filename (dwz->dwz_bfd));
3885 return 0;
3886 }
3887 }
3888
3889 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3890 dwz_list_elements);
3891
3892 if (types_list_elements)
3893 {
3894 struct dwarf2_section_info *section;
3895
3896 /* We can only handle a single .debug_types when we have an
3897 index. */
3898 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3899 return 0;
3900
3901 section = VEC_index (dwarf2_section_info_def,
3902 dwarf2_per_objfile->types, 0);
3903
3904 create_signatured_type_table_from_index (objfile, section, types_list,
3905 types_list_elements);
3906 }
3907
3908 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
3909
3910 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3911 map = new (map) mapped_index ();
3912 *map = local_map;
3913
3914 dwarf2_per_objfile->index_table = map;
3915 dwarf2_per_objfile->using_index = 1;
3916 dwarf2_per_objfile->quick_file_names_table =
3917 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3918
3919 return 1;
3920 }
3921
3922 /* die_reader_func for dw2_get_file_names. */
3923
3924 static void
3925 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3926 const gdb_byte *info_ptr,
3927 struct die_info *comp_unit_die,
3928 int has_children,
3929 void *data)
3930 {
3931 struct dwarf2_cu *cu = reader->cu;
3932 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3933 struct dwarf2_per_objfile *dwarf2_per_objfile
3934 = cu->per_cu->dwarf2_per_objfile;
3935 struct objfile *objfile = dwarf2_per_objfile->objfile;
3936 struct dwarf2_per_cu_data *lh_cu;
3937 struct attribute *attr;
3938 int i;
3939 void **slot;
3940 struct quick_file_names *qfn;
3941
3942 gdb_assert (! this_cu->is_debug_types);
3943
3944 /* Our callers never want to match partial units -- instead they
3945 will match the enclosing full CU. */
3946 if (comp_unit_die->tag == DW_TAG_partial_unit)
3947 {
3948 this_cu->v.quick->no_file_data = 1;
3949 return;
3950 }
3951
3952 lh_cu = this_cu;
3953 slot = NULL;
3954
3955 line_header_up lh;
3956 sect_offset line_offset {};
3957
3958 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3959 if (attr)
3960 {
3961 struct quick_file_names find_entry;
3962
3963 line_offset = (sect_offset) DW_UNSND (attr);
3964
3965 /* We may have already read in this line header (TU line header sharing).
3966 If we have we're done. */
3967 find_entry.hash.dwo_unit = cu->dwo_unit;
3968 find_entry.hash.line_sect_off = line_offset;
3969 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3970 &find_entry, INSERT);
3971 if (*slot != NULL)
3972 {
3973 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3974 return;
3975 }
3976
3977 lh = dwarf_decode_line_header (line_offset, cu);
3978 }
3979 if (lh == NULL)
3980 {
3981 lh_cu->v.quick->no_file_data = 1;
3982 return;
3983 }
3984
3985 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3986 qfn->hash.dwo_unit = cu->dwo_unit;
3987 qfn->hash.line_sect_off = line_offset;
3988 gdb_assert (slot != NULL);
3989 *slot = qfn;
3990
3991 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3992
3993 qfn->num_file_names = lh->file_names.size ();
3994 qfn->file_names =
3995 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3996 for (i = 0; i < lh->file_names.size (); ++i)
3997 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3998 qfn->real_names = NULL;
3999
4000 lh_cu->v.quick->file_names = qfn;
4001 }
4002
4003 /* A helper for the "quick" functions which attempts to read the line
4004 table for THIS_CU. */
4005
4006 static struct quick_file_names *
4007 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
4008 {
4009 /* This should never be called for TUs. */
4010 gdb_assert (! this_cu->is_debug_types);
4011 /* Nor type unit groups. */
4012 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
4013
4014 if (this_cu->v.quick->file_names != NULL)
4015 return this_cu->v.quick->file_names;
4016 /* If we know there is no line data, no point in looking again. */
4017 if (this_cu->v.quick->no_file_data)
4018 return NULL;
4019
4020 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
4021
4022 if (this_cu->v.quick->no_file_data)
4023 return NULL;
4024 return this_cu->v.quick->file_names;
4025 }
4026
4027 /* A helper for the "quick" functions which computes and caches the
4028 real path for a given file name from the line table. */
4029
4030 static const char *
4031 dw2_get_real_path (struct objfile *objfile,
4032 struct quick_file_names *qfn, int index)
4033 {
4034 if (qfn->real_names == NULL)
4035 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
4036 qfn->num_file_names, const char *);
4037
4038 if (qfn->real_names[index] == NULL)
4039 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
4040
4041 return qfn->real_names[index];
4042 }
4043
4044 static struct symtab *
4045 dw2_find_last_source_symtab (struct objfile *objfile)
4046 {
4047 struct dwarf2_per_objfile *dwarf2_per_objfile
4048 = get_dwarf2_per_objfile (objfile);
4049 int index = dwarf2_per_objfile->n_comp_units - 1;
4050 dwarf2_per_cu_data *dwarf_cu = dw2_get_cutu (dwarf2_per_objfile, index);
4051 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
4052
4053 if (cust == NULL)
4054 return NULL;
4055
4056 return compunit_primary_filetab (cust);
4057 }
4058
4059 /* Traversal function for dw2_forget_cached_source_info. */
4060
4061 static int
4062 dw2_free_cached_file_names (void **slot, void *info)
4063 {
4064 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
4065
4066 if (file_data->real_names)
4067 {
4068 int i;
4069
4070 for (i = 0; i < file_data->num_file_names; ++i)
4071 {
4072 xfree ((void*) file_data->real_names[i]);
4073 file_data->real_names[i] = NULL;
4074 }
4075 }
4076
4077 return 1;
4078 }
4079
4080 static void
4081 dw2_forget_cached_source_info (struct objfile *objfile)
4082 {
4083 struct dwarf2_per_objfile *dwarf2_per_objfile
4084 = get_dwarf2_per_objfile (objfile);
4085
4086 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
4087 dw2_free_cached_file_names, NULL);
4088 }
4089
4090 /* Helper function for dw2_map_symtabs_matching_filename that expands
4091 the symtabs and calls the iterator. */
4092
4093 static int
4094 dw2_map_expand_apply (struct objfile *objfile,
4095 struct dwarf2_per_cu_data *per_cu,
4096 const char *name, const char *real_path,
4097 gdb::function_view<bool (symtab *)> callback)
4098 {
4099 struct compunit_symtab *last_made = objfile->compunit_symtabs;
4100
4101 /* Don't visit already-expanded CUs. */
4102 if (per_cu->v.quick->compunit_symtab)
4103 return 0;
4104
4105 /* This may expand more than one symtab, and we want to iterate over
4106 all of them. */
4107 dw2_instantiate_symtab (per_cu);
4108
4109 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4110 last_made, callback);
4111 }
4112
4113 /* Implementation of the map_symtabs_matching_filename method. */
4114
4115 static bool
4116 dw2_map_symtabs_matching_filename
4117 (struct objfile *objfile, const char *name, const char *real_path,
4118 gdb::function_view<bool (symtab *)> callback)
4119 {
4120 int i;
4121 const char *name_basename = lbasename (name);
4122 struct dwarf2_per_objfile *dwarf2_per_objfile
4123 = get_dwarf2_per_objfile (objfile);
4124
4125 /* The rule is CUs specify all the files, including those used by
4126 any TU, so there's no need to scan TUs here. */
4127
4128 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4129 {
4130 int j;
4131 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
4132 struct quick_file_names *file_data;
4133
4134 /* We only need to look at symtabs not already expanded. */
4135 if (per_cu->v.quick->compunit_symtab)
4136 continue;
4137
4138 file_data = dw2_get_file_names (per_cu);
4139 if (file_data == NULL)
4140 continue;
4141
4142 for (j = 0; j < file_data->num_file_names; ++j)
4143 {
4144 const char *this_name = file_data->file_names[j];
4145 const char *this_real_name;
4146
4147 if (compare_filenames_for_search (this_name, name))
4148 {
4149 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4150 callback))
4151 return true;
4152 continue;
4153 }
4154
4155 /* Before we invoke realpath, which can get expensive when many
4156 files are involved, do a quick comparison of the basenames. */
4157 if (! basenames_may_differ
4158 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4159 continue;
4160
4161 this_real_name = dw2_get_real_path (objfile, file_data, j);
4162 if (compare_filenames_for_search (this_real_name, name))
4163 {
4164 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4165 callback))
4166 return true;
4167 continue;
4168 }
4169
4170 if (real_path != NULL)
4171 {
4172 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4173 gdb_assert (IS_ABSOLUTE_PATH (name));
4174 if (this_real_name != NULL
4175 && FILENAME_CMP (real_path, this_real_name) == 0)
4176 {
4177 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4178 callback))
4179 return true;
4180 continue;
4181 }
4182 }
4183 }
4184 }
4185
4186 return false;
4187 }
4188
4189 /* Struct used to manage iterating over all CUs looking for a symbol. */
4190
4191 struct dw2_symtab_iterator
4192 {
4193 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
4194 struct dwarf2_per_objfile *dwarf2_per_objfile;
4195 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4196 int want_specific_block;
4197 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4198 Unused if !WANT_SPECIFIC_BLOCK. */
4199 int block_index;
4200 /* The kind of symbol we're looking for. */
4201 domain_enum domain;
4202 /* The list of CUs from the index entry of the symbol,
4203 or NULL if not found. */
4204 offset_type *vec;
4205 /* The next element in VEC to look at. */
4206 int next;
4207 /* The number of elements in VEC, or zero if there is no match. */
4208 int length;
4209 /* Have we seen a global version of the symbol?
4210 If so we can ignore all further global instances.
4211 This is to work around gold/15646, inefficient gold-generated
4212 indices. */
4213 int global_seen;
4214 };
4215
4216 /* Initialize the index symtab iterator ITER.
4217 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4218 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
4219
4220 static void
4221 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
4222 struct dwarf2_per_objfile *dwarf2_per_objfile,
4223 int want_specific_block,
4224 int block_index,
4225 domain_enum domain,
4226 const char *name)
4227 {
4228 iter->dwarf2_per_objfile = dwarf2_per_objfile;
4229 iter->want_specific_block = want_specific_block;
4230 iter->block_index = block_index;
4231 iter->domain = domain;
4232 iter->next = 0;
4233 iter->global_seen = 0;
4234
4235 mapped_index *index = dwarf2_per_objfile->index_table;
4236
4237 /* index is NULL if OBJF_READNOW. */
4238 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
4239 iter->length = MAYBE_SWAP (*iter->vec);
4240 else
4241 {
4242 iter->vec = NULL;
4243 iter->length = 0;
4244 }
4245 }
4246
4247 /* Return the next matching CU or NULL if there are no more. */
4248
4249 static struct dwarf2_per_cu_data *
4250 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4251 {
4252 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4253
4254 for ( ; iter->next < iter->length; ++iter->next)
4255 {
4256 offset_type cu_index_and_attrs =
4257 MAYBE_SWAP (iter->vec[iter->next + 1]);
4258 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4259 struct dwarf2_per_cu_data *per_cu;
4260 int want_static = iter->block_index != GLOBAL_BLOCK;
4261 /* This value is only valid for index versions >= 7. */
4262 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4263 gdb_index_symbol_kind symbol_kind =
4264 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4265 /* Only check the symbol attributes if they're present.
4266 Indices prior to version 7 don't record them,
4267 and indices >= 7 may elide them for certain symbols
4268 (gold does this). */
4269 int attrs_valid =
4270 (dwarf2_per_objfile->index_table->version >= 7
4271 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4272
4273 /* Don't crash on bad data. */
4274 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4275 + dwarf2_per_objfile->n_type_units))
4276 {
4277 complaint (&symfile_complaints,
4278 _(".gdb_index entry has bad CU index"
4279 " [in module %s]"),
4280 objfile_name (dwarf2_per_objfile->objfile));
4281 continue;
4282 }
4283
4284 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
4285
4286 /* Skip if already read in. */
4287 if (per_cu->v.quick->compunit_symtab)
4288 continue;
4289
4290 /* Check static vs global. */
4291 if (attrs_valid)
4292 {
4293 if (iter->want_specific_block
4294 && want_static != is_static)
4295 continue;
4296 /* Work around gold/15646. */
4297 if (!is_static && iter->global_seen)
4298 continue;
4299 if (!is_static)
4300 iter->global_seen = 1;
4301 }
4302
4303 /* Only check the symbol's kind if it has one. */
4304 if (attrs_valid)
4305 {
4306 switch (iter->domain)
4307 {
4308 case VAR_DOMAIN:
4309 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4310 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4311 /* Some types are also in VAR_DOMAIN. */
4312 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4313 continue;
4314 break;
4315 case STRUCT_DOMAIN:
4316 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4317 continue;
4318 break;
4319 case LABEL_DOMAIN:
4320 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4321 continue;
4322 break;
4323 default:
4324 break;
4325 }
4326 }
4327
4328 ++iter->next;
4329 return per_cu;
4330 }
4331
4332 return NULL;
4333 }
4334
4335 static struct compunit_symtab *
4336 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4337 const char *name, domain_enum domain)
4338 {
4339 struct compunit_symtab *stab_best = NULL;
4340 struct dwarf2_per_objfile *dwarf2_per_objfile
4341 = get_dwarf2_per_objfile (objfile);
4342
4343 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4344
4345 struct dw2_symtab_iterator iter;
4346 struct dwarf2_per_cu_data *per_cu;
4347
4348 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4349
4350 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4351 {
4352 struct symbol *sym, *with_opaque = NULL;
4353 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4354 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4355 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4356
4357 sym = block_find_symbol (block, name, domain,
4358 block_find_non_opaque_type_preferred,
4359 &with_opaque);
4360
4361 /* Some caution must be observed with overloaded functions
4362 and methods, since the index will not contain any overload
4363 information (but NAME might contain it). */
4364
4365 if (sym != NULL
4366 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4367 return stab;
4368 if (with_opaque != NULL
4369 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4370 stab_best = stab;
4371
4372 /* Keep looking through other CUs. */
4373 }
4374
4375 return stab_best;
4376 }
4377
4378 static void
4379 dw2_print_stats (struct objfile *objfile)
4380 {
4381 struct dwarf2_per_objfile *dwarf2_per_objfile
4382 = get_dwarf2_per_objfile (objfile);
4383 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4384 int count = 0;
4385
4386 for (int i = 0; i < total; ++i)
4387 {
4388 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4389
4390 if (!per_cu->v.quick->compunit_symtab)
4391 ++count;
4392 }
4393 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4394 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4395 }
4396
4397 /* This dumps minimal information about the index.
4398 It is called via "mt print objfiles".
4399 One use is to verify .gdb_index has been loaded by the
4400 gdb.dwarf2/gdb-index.exp testcase. */
4401
4402 static void
4403 dw2_dump (struct objfile *objfile)
4404 {
4405 struct dwarf2_per_objfile *dwarf2_per_objfile
4406 = get_dwarf2_per_objfile (objfile);
4407
4408 gdb_assert (dwarf2_per_objfile->using_index);
4409 printf_filtered (".gdb_index:");
4410 if (dwarf2_per_objfile->index_table != NULL)
4411 {
4412 printf_filtered (" version %d\n",
4413 dwarf2_per_objfile->index_table->version);
4414 }
4415 else
4416 printf_filtered (" faked for \"readnow\"\n");
4417 printf_filtered ("\n");
4418 }
4419
4420 static void
4421 dw2_relocate (struct objfile *objfile,
4422 const struct section_offsets *new_offsets,
4423 const struct section_offsets *delta)
4424 {
4425 /* There's nothing to relocate here. */
4426 }
4427
4428 static void
4429 dw2_expand_symtabs_for_function (struct objfile *objfile,
4430 const char *func_name)
4431 {
4432 struct dwarf2_per_objfile *dwarf2_per_objfile
4433 = get_dwarf2_per_objfile (objfile);
4434
4435 struct dw2_symtab_iterator iter;
4436 struct dwarf2_per_cu_data *per_cu;
4437
4438 /* Note: It doesn't matter what we pass for block_index here. */
4439 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4440 func_name);
4441
4442 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4443 dw2_instantiate_symtab (per_cu);
4444
4445 }
4446
4447 static void
4448 dw2_expand_all_symtabs (struct objfile *objfile)
4449 {
4450 struct dwarf2_per_objfile *dwarf2_per_objfile
4451 = get_dwarf2_per_objfile (objfile);
4452 int total_units = (dwarf2_per_objfile->n_comp_units
4453 + dwarf2_per_objfile->n_type_units);
4454
4455 for (int i = 0; i < total_units; ++i)
4456 {
4457 struct dwarf2_per_cu_data *per_cu
4458 = dw2_get_cutu (dwarf2_per_objfile, i);
4459
4460 dw2_instantiate_symtab (per_cu);
4461 }
4462 }
4463
4464 static void
4465 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4466 const char *fullname)
4467 {
4468 struct dwarf2_per_objfile *dwarf2_per_objfile
4469 = get_dwarf2_per_objfile (objfile);
4470
4471 /* We don't need to consider type units here.
4472 This is only called for examining code, e.g. expand_line_sal.
4473 There can be an order of magnitude (or more) more type units
4474 than comp units, and we avoid them if we can. */
4475
4476 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4477 {
4478 int j;
4479 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4480 struct quick_file_names *file_data;
4481
4482 /* We only need to look at symtabs not already expanded. */
4483 if (per_cu->v.quick->compunit_symtab)
4484 continue;
4485
4486 file_data = dw2_get_file_names (per_cu);
4487 if (file_data == NULL)
4488 continue;
4489
4490 for (j = 0; j < file_data->num_file_names; ++j)
4491 {
4492 const char *this_fullname = file_data->file_names[j];
4493
4494 if (filename_cmp (this_fullname, fullname) == 0)
4495 {
4496 dw2_instantiate_symtab (per_cu);
4497 break;
4498 }
4499 }
4500 }
4501 }
4502
4503 static void
4504 dw2_map_matching_symbols (struct objfile *objfile,
4505 const char * name, domain_enum domain,
4506 int global,
4507 int (*callback) (struct block *,
4508 struct symbol *, void *),
4509 void *data, symbol_name_match_type match,
4510 symbol_compare_ftype *ordered_compare)
4511 {
4512 /* Currently unimplemented; used for Ada. The function can be called if the
4513 current language is Ada for a non-Ada objfile using GNU index. As Ada
4514 does not look for non-Ada symbols this function should just return. */
4515 }
4516
4517 /* Symbol name matcher for .gdb_index names.
4518
4519 Symbol names in .gdb_index have a few particularities:
4520
4521 - There's no indication of which is the language of each symbol.
4522
4523 Since each language has its own symbol name matching algorithm,
4524 and we don't know which language is the right one, we must match
4525 each symbol against all languages. This would be a potential
4526 performance problem if it were not mitigated by the
4527 mapped_index::name_components lookup table, which significantly
4528 reduces the number of times we need to call into this matcher,
4529 making it a non-issue.
4530
4531 - Symbol names in the index have no overload (parameter)
4532 information. I.e., in C++, "foo(int)" and "foo(long)" both
4533 appear as "foo" in the index, for example.
4534
4535 This means that the lookup names passed to the symbol name
4536 matcher functions must have no parameter information either
4537 because (e.g.) symbol search name "foo" does not match
4538 lookup-name "foo(int)" [while swapping search name for lookup
4539 name would match].
4540 */
4541 class gdb_index_symbol_name_matcher
4542 {
4543 public:
4544 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4545 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4546
4547 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4548 Returns true if any matcher matches. */
4549 bool matches (const char *symbol_name);
4550
4551 private:
4552 /* A reference to the lookup name we're matching against. */
4553 const lookup_name_info &m_lookup_name;
4554
4555 /* A vector holding all the different symbol name matchers, for all
4556 languages. */
4557 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4558 };
4559
4560 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4561 (const lookup_name_info &lookup_name)
4562 : m_lookup_name (lookup_name)
4563 {
4564 /* Prepare the vector of comparison functions upfront, to avoid
4565 doing the same work for each symbol. Care is taken to avoid
4566 matching with the same matcher more than once if/when multiple
4567 languages use the same matcher function. */
4568 auto &matchers = m_symbol_name_matcher_funcs;
4569 matchers.reserve (nr_languages);
4570
4571 matchers.push_back (default_symbol_name_matcher);
4572
4573 for (int i = 0; i < nr_languages; i++)
4574 {
4575 const language_defn *lang = language_def ((enum language) i);
4576 symbol_name_matcher_ftype *name_matcher
4577 = get_symbol_name_matcher (lang, m_lookup_name);
4578
4579 /* Don't insert the same comparison routine more than once.
4580 Note that we do this linear walk instead of a seemingly
4581 cheaper sorted insert, or use a std::set or something like
4582 that, because relative order of function addresses is not
4583 stable. This is not a problem in practice because the number
4584 of supported languages is low, and the cost here is tiny
4585 compared to the number of searches we'll do afterwards using
4586 this object. */
4587 if (name_matcher != default_symbol_name_matcher
4588 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4589 == matchers.end ()))
4590 matchers.push_back (name_matcher);
4591 }
4592 }
4593
4594 bool
4595 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4596 {
4597 for (auto matches_name : m_symbol_name_matcher_funcs)
4598 if (matches_name (symbol_name, m_lookup_name, NULL))
4599 return true;
4600
4601 return false;
4602 }
4603
4604 /* Starting from a search name, return the string that finds the upper
4605 bound of all strings that start with SEARCH_NAME in a sorted name
4606 list. Returns the empty string to indicate that the upper bound is
4607 the end of the list. */
4608
4609 static std::string
4610 make_sort_after_prefix_name (const char *search_name)
4611 {
4612 /* When looking to complete "func", we find the upper bound of all
4613 symbols that start with "func" by looking for where we'd insert
4614 the closest string that would follow "func" in lexicographical
4615 order. Usually, that's "func"-with-last-character-incremented,
4616 i.e. "fund". Mind non-ASCII characters, though. Usually those
4617 will be UTF-8 multi-byte sequences, but we can't be certain.
4618 Especially mind the 0xff character, which is a valid character in
4619 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4620 rule out compilers allowing it in identifiers. Note that
4621 conveniently, strcmp/strcasecmp are specified to compare
4622 characters interpreted as unsigned char. So what we do is treat
4623 the whole string as a base 256 number composed of a sequence of
4624 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4625 to 0, and carries 1 to the following more-significant position.
4626 If the very first character in SEARCH_NAME ends up incremented
4627 and carries/overflows, then the upper bound is the end of the
4628 list. The string after the empty string is also the empty
4629 string.
4630
4631 Some examples of this operation:
4632
4633 SEARCH_NAME => "+1" RESULT
4634
4635 "abc" => "abd"
4636 "ab\xff" => "ac"
4637 "\xff" "a" "\xff" => "\xff" "b"
4638 "\xff" => ""
4639 "\xff\xff" => ""
4640 "" => ""
4641
4642 Then, with these symbols for example:
4643
4644 func
4645 func1
4646 fund
4647
4648 completing "func" looks for symbols between "func" and
4649 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4650 which finds "func" and "func1", but not "fund".
4651
4652 And with:
4653
4654 funcÿ (Latin1 'ÿ' [0xff])
4655 funcÿ1
4656 fund
4657
4658 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4659 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4660
4661 And with:
4662
4663 ÿÿ (Latin1 'ÿ' [0xff])
4664 ÿÿ1
4665
4666 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4667 the end of the list.
4668 */
4669 std::string after = search_name;
4670 while (!after.empty () && (unsigned char) after.back () == 0xff)
4671 after.pop_back ();
4672 if (!after.empty ())
4673 after.back () = (unsigned char) after.back () + 1;
4674 return after;
4675 }
4676
4677 /* See declaration. */
4678
4679 std::pair<std::vector<name_component>::const_iterator,
4680 std::vector<name_component>::const_iterator>
4681 mapped_index_base::find_name_components_bounds
4682 (const lookup_name_info &lookup_name_without_params) const
4683 {
4684 auto *name_cmp
4685 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4686
4687 const char *cplus
4688 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4689
4690 /* Comparison function object for lower_bound that matches against a
4691 given symbol name. */
4692 auto lookup_compare_lower = [&] (const name_component &elem,
4693 const char *name)
4694 {
4695 const char *elem_qualified = this->symbol_name_at (elem.idx);
4696 const char *elem_name = elem_qualified + elem.name_offset;
4697 return name_cmp (elem_name, name) < 0;
4698 };
4699
4700 /* Comparison function object for upper_bound that matches against a
4701 given symbol name. */
4702 auto lookup_compare_upper = [&] (const char *name,
4703 const name_component &elem)
4704 {
4705 const char *elem_qualified = this->symbol_name_at (elem.idx);
4706 const char *elem_name = elem_qualified + elem.name_offset;
4707 return name_cmp (name, elem_name) < 0;
4708 };
4709
4710 auto begin = this->name_components.begin ();
4711 auto end = this->name_components.end ();
4712
4713 /* Find the lower bound. */
4714 auto lower = [&] ()
4715 {
4716 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4717 return begin;
4718 else
4719 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4720 } ();
4721
4722 /* Find the upper bound. */
4723 auto upper = [&] ()
4724 {
4725 if (lookup_name_without_params.completion_mode ())
4726 {
4727 /* In completion mode, we want UPPER to point past all
4728 symbols names that have the same prefix. I.e., with
4729 these symbols, and completing "func":
4730
4731 function << lower bound
4732 function1
4733 other_function << upper bound
4734
4735 We find the upper bound by looking for the insertion
4736 point of "func"-with-last-character-incremented,
4737 i.e. "fund". */
4738 std::string after = make_sort_after_prefix_name (cplus);
4739 if (after.empty ())
4740 return end;
4741 return std::lower_bound (lower, end, after.c_str (),
4742 lookup_compare_lower);
4743 }
4744 else
4745 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4746 } ();
4747
4748 return {lower, upper};
4749 }
4750
4751 /* See declaration. */
4752
4753 void
4754 mapped_index_base::build_name_components ()
4755 {
4756 if (!this->name_components.empty ())
4757 return;
4758
4759 this->name_components_casing = case_sensitivity;
4760 auto *name_cmp
4761 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4762
4763 /* The code below only knows how to break apart components of C++
4764 symbol names (and other languages that use '::' as
4765 namespace/module separator). If we add support for wild matching
4766 to some language that uses some other operator (E.g., Ada, Go and
4767 D use '.'), then we'll need to try splitting the symbol name
4768 according to that language too. Note that Ada does support wild
4769 matching, but doesn't currently support .gdb_index. */
4770 auto count = this->symbol_name_count ();
4771 for (offset_type idx = 0; idx < count; idx++)
4772 {
4773 if (this->symbol_name_slot_invalid (idx))
4774 continue;
4775
4776 const char *name = this->symbol_name_at (idx);
4777
4778 /* Add each name component to the name component table. */
4779 unsigned int previous_len = 0;
4780 for (unsigned int current_len = cp_find_first_component (name);
4781 name[current_len] != '\0';
4782 current_len += cp_find_first_component (name + current_len))
4783 {
4784 gdb_assert (name[current_len] == ':');
4785 this->name_components.push_back ({previous_len, idx});
4786 /* Skip the '::'. */
4787 current_len += 2;
4788 previous_len = current_len;
4789 }
4790 this->name_components.push_back ({previous_len, idx});
4791 }
4792
4793 /* Sort name_components elements by name. */
4794 auto name_comp_compare = [&] (const name_component &left,
4795 const name_component &right)
4796 {
4797 const char *left_qualified = this->symbol_name_at (left.idx);
4798 const char *right_qualified = this->symbol_name_at (right.idx);
4799
4800 const char *left_name = left_qualified + left.name_offset;
4801 const char *right_name = right_qualified + right.name_offset;
4802
4803 return name_cmp (left_name, right_name) < 0;
4804 };
4805
4806 std::sort (this->name_components.begin (),
4807 this->name_components.end (),
4808 name_comp_compare);
4809 }
4810
4811 /* Helper for dw2_expand_symtabs_matching that works with a
4812 mapped_index_base instead of the containing objfile. This is split
4813 to a separate function in order to be able to unit test the
4814 name_components matching using a mock mapped_index_base. For each
4815 symbol name that matches, calls MATCH_CALLBACK, passing it the
4816 symbol's index in the mapped_index_base symbol table. */
4817
4818 static void
4819 dw2_expand_symtabs_matching_symbol
4820 (mapped_index_base &index,
4821 const lookup_name_info &lookup_name_in,
4822 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4823 enum search_domain kind,
4824 gdb::function_view<void (offset_type)> match_callback)
4825 {
4826 lookup_name_info lookup_name_without_params
4827 = lookup_name_in.make_ignore_params ();
4828 gdb_index_symbol_name_matcher lookup_name_matcher
4829 (lookup_name_without_params);
4830
4831 /* Build the symbol name component sorted vector, if we haven't
4832 yet. */
4833 index.build_name_components ();
4834
4835 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4836
4837 /* Now for each symbol name in range, check to see if we have a name
4838 match, and if so, call the MATCH_CALLBACK callback. */
4839
4840 /* The same symbol may appear more than once in the range though.
4841 E.g., if we're looking for symbols that complete "w", and we have
4842 a symbol named "w1::w2", we'll find the two name components for
4843 that same symbol in the range. To be sure we only call the
4844 callback once per symbol, we first collect the symbol name
4845 indexes that matched in a temporary vector and ignore
4846 duplicates. */
4847 std::vector<offset_type> matches;
4848 matches.reserve (std::distance (bounds.first, bounds.second));
4849
4850 for (; bounds.first != bounds.second; ++bounds.first)
4851 {
4852 const char *qualified = index.symbol_name_at (bounds.first->idx);
4853
4854 if (!lookup_name_matcher.matches (qualified)
4855 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4856 continue;
4857
4858 matches.push_back (bounds.first->idx);
4859 }
4860
4861 std::sort (matches.begin (), matches.end ());
4862
4863 /* Finally call the callback, once per match. */
4864 ULONGEST prev = -1;
4865 for (offset_type idx : matches)
4866 {
4867 if (prev != idx)
4868 {
4869 match_callback (idx);
4870 prev = idx;
4871 }
4872 }
4873
4874 /* Above we use a type wider than idx's for 'prev', since 0 and
4875 (offset_type)-1 are both possible values. */
4876 static_assert (sizeof (prev) > sizeof (offset_type), "");
4877 }
4878
4879 #if GDB_SELF_TEST
4880
4881 namespace selftests { namespace dw2_expand_symtabs_matching {
4882
4883 /* A mock .gdb_index/.debug_names-like name index table, enough to
4884 exercise dw2_expand_symtabs_matching_symbol, which works with the
4885 mapped_index_base interface. Builds an index from the symbol list
4886 passed as parameter to the constructor. */
4887 class mock_mapped_index : public mapped_index_base
4888 {
4889 public:
4890 mock_mapped_index (gdb::array_view<const char *> symbols)
4891 : m_symbol_table (symbols)
4892 {}
4893
4894 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4895
4896 /* Return the number of names in the symbol table. */
4897 virtual size_t symbol_name_count () const
4898 {
4899 return m_symbol_table.size ();
4900 }
4901
4902 /* Get the name of the symbol at IDX in the symbol table. */
4903 virtual const char *symbol_name_at (offset_type idx) const
4904 {
4905 return m_symbol_table[idx];
4906 }
4907
4908 private:
4909 gdb::array_view<const char *> m_symbol_table;
4910 };
4911
4912 /* Convenience function that converts a NULL pointer to a "<null>"
4913 string, to pass to print routines. */
4914
4915 static const char *
4916 string_or_null (const char *str)
4917 {
4918 return str != NULL ? str : "<null>";
4919 }
4920
4921 /* Check if a lookup_name_info built from
4922 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4923 index. EXPECTED_LIST is the list of expected matches, in expected
4924 matching order. If no match expected, then an empty list is
4925 specified. Returns true on success. On failure prints a warning
4926 indicating the file:line that failed, and returns false. */
4927
4928 static bool
4929 check_match (const char *file, int line,
4930 mock_mapped_index &mock_index,
4931 const char *name, symbol_name_match_type match_type,
4932 bool completion_mode,
4933 std::initializer_list<const char *> expected_list)
4934 {
4935 lookup_name_info lookup_name (name, match_type, completion_mode);
4936
4937 bool matched = true;
4938
4939 auto mismatch = [&] (const char *expected_str,
4940 const char *got)
4941 {
4942 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4943 "expected=\"%s\", got=\"%s\"\n"),
4944 file, line,
4945 (match_type == symbol_name_match_type::FULL
4946 ? "FULL" : "WILD"),
4947 name, string_or_null (expected_str), string_or_null (got));
4948 matched = false;
4949 };
4950
4951 auto expected_it = expected_list.begin ();
4952 auto expected_end = expected_list.end ();
4953
4954 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4955 NULL, ALL_DOMAIN,
4956 [&] (offset_type idx)
4957 {
4958 const char *matched_name = mock_index.symbol_name_at (idx);
4959 const char *expected_str
4960 = expected_it == expected_end ? NULL : *expected_it++;
4961
4962 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4963 mismatch (expected_str, matched_name);
4964 });
4965
4966 const char *expected_str
4967 = expected_it == expected_end ? NULL : *expected_it++;
4968 if (expected_str != NULL)
4969 mismatch (expected_str, NULL);
4970
4971 return matched;
4972 }
4973
4974 /* The symbols added to the mock mapped_index for testing (in
4975 canonical form). */
4976 static const char *test_symbols[] = {
4977 "function",
4978 "std::bar",
4979 "std::zfunction",
4980 "std::zfunction2",
4981 "w1::w2",
4982 "ns::foo<char*>",
4983 "ns::foo<int>",
4984 "ns::foo<long>",
4985 "ns2::tmpl<int>::foo2",
4986 "(anonymous namespace)::A::B::C",
4987
4988 /* These are used to check that the increment-last-char in the
4989 matching algorithm for completion doesn't match "t1_fund" when
4990 completing "t1_func". */
4991 "t1_func",
4992 "t1_func1",
4993 "t1_fund",
4994 "t1_fund1",
4995
4996 /* A UTF-8 name with multi-byte sequences to make sure that
4997 cp-name-parser understands this as a single identifier ("função"
4998 is "function" in PT). */
4999 u8"u8função",
5000
5001 /* \377 (0xff) is Latin1 'ÿ'. */
5002 "yfunc\377",
5003
5004 /* \377 (0xff) is Latin1 'ÿ'. */
5005 "\377",
5006 "\377\377123",
5007
5008 /* A name with all sorts of complications. Starts with "z" to make
5009 it easier for the completion tests below. */
5010 #define Z_SYM_NAME \
5011 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
5012 "::tuple<(anonymous namespace)::ui*, " \
5013 "std::default_delete<(anonymous namespace)::ui>, void>"
5014
5015 Z_SYM_NAME
5016 };
5017
5018 /* Returns true if the mapped_index_base::find_name_component_bounds
5019 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
5020 in completion mode. */
5021
5022 static bool
5023 check_find_bounds_finds (mapped_index_base &index,
5024 const char *search_name,
5025 gdb::array_view<const char *> expected_syms)
5026 {
5027 lookup_name_info lookup_name (search_name,
5028 symbol_name_match_type::FULL, true);
5029
5030 auto bounds = index.find_name_components_bounds (lookup_name);
5031
5032 size_t distance = std::distance (bounds.first, bounds.second);
5033 if (distance != expected_syms.size ())
5034 return false;
5035
5036 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
5037 {
5038 auto nc_elem = bounds.first + exp_elem;
5039 const char *qualified = index.symbol_name_at (nc_elem->idx);
5040 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
5041 return false;
5042 }
5043
5044 return true;
5045 }
5046
5047 /* Test the lower-level mapped_index::find_name_component_bounds
5048 method. */
5049
5050 static void
5051 test_mapped_index_find_name_component_bounds ()
5052 {
5053 mock_mapped_index mock_index (test_symbols);
5054
5055 mock_index.build_name_components ();
5056
5057 /* Test the lower-level mapped_index::find_name_component_bounds
5058 method in completion mode. */
5059 {
5060 static const char *expected_syms[] = {
5061 "t1_func",
5062 "t1_func1",
5063 };
5064
5065 SELF_CHECK (check_find_bounds_finds (mock_index,
5066 "t1_func", expected_syms));
5067 }
5068
5069 /* Check that the increment-last-char in the name matching algorithm
5070 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5071 {
5072 static const char *expected_syms1[] = {
5073 "\377",
5074 "\377\377123",
5075 };
5076 SELF_CHECK (check_find_bounds_finds (mock_index,
5077 "\377", expected_syms1));
5078
5079 static const char *expected_syms2[] = {
5080 "\377\377123",
5081 };
5082 SELF_CHECK (check_find_bounds_finds (mock_index,
5083 "\377\377", expected_syms2));
5084 }
5085 }
5086
5087 /* Test dw2_expand_symtabs_matching_symbol. */
5088
5089 static void
5090 test_dw2_expand_symtabs_matching_symbol ()
5091 {
5092 mock_mapped_index mock_index (test_symbols);
5093
5094 /* We let all tests run until the end even if some fails, for debug
5095 convenience. */
5096 bool any_mismatch = false;
5097
5098 /* Create the expected symbols list (an initializer_list). Needed
5099 because lists have commas, and we need to pass them to CHECK,
5100 which is a macro. */
5101 #define EXPECT(...) { __VA_ARGS__ }
5102
5103 /* Wrapper for check_match that passes down the current
5104 __FILE__/__LINE__. */
5105 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5106 any_mismatch |= !check_match (__FILE__, __LINE__, \
5107 mock_index, \
5108 NAME, MATCH_TYPE, COMPLETION_MODE, \
5109 EXPECTED_LIST)
5110
5111 /* Identity checks. */
5112 for (const char *sym : test_symbols)
5113 {
5114 /* Should be able to match all existing symbols. */
5115 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5116 EXPECT (sym));
5117
5118 /* Should be able to match all existing symbols with
5119 parameters. */
5120 std::string with_params = std::string (sym) + "(int)";
5121 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5122 EXPECT (sym));
5123
5124 /* Should be able to match all existing symbols with
5125 parameters and qualifiers. */
5126 with_params = std::string (sym) + " ( int ) const";
5127 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5128 EXPECT (sym));
5129
5130 /* This should really find sym, but cp-name-parser.y doesn't
5131 know about lvalue/rvalue qualifiers yet. */
5132 with_params = std::string (sym) + " ( int ) &&";
5133 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5134 {});
5135 }
5136
5137 /* Check that the name matching algorithm for completion doesn't get
5138 confused with Latin1 'ÿ' / 0xff. */
5139 {
5140 static const char str[] = "\377";
5141 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5142 EXPECT ("\377", "\377\377123"));
5143 }
5144
5145 /* Check that the increment-last-char in the matching algorithm for
5146 completion doesn't match "t1_fund" when completing "t1_func". */
5147 {
5148 static const char str[] = "t1_func";
5149 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5150 EXPECT ("t1_func", "t1_func1"));
5151 }
5152
5153 /* Check that completion mode works at each prefix of the expected
5154 symbol name. */
5155 {
5156 static const char str[] = "function(int)";
5157 size_t len = strlen (str);
5158 std::string lookup;
5159
5160 for (size_t i = 1; i < len; i++)
5161 {
5162 lookup.assign (str, i);
5163 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5164 EXPECT ("function"));
5165 }
5166 }
5167
5168 /* While "w" is a prefix of both components, the match function
5169 should still only be called once. */
5170 {
5171 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5172 EXPECT ("w1::w2"));
5173 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5174 EXPECT ("w1::w2"));
5175 }
5176
5177 /* Same, with a "complicated" symbol. */
5178 {
5179 static const char str[] = Z_SYM_NAME;
5180 size_t len = strlen (str);
5181 std::string lookup;
5182
5183 for (size_t i = 1; i < len; i++)
5184 {
5185 lookup.assign (str, i);
5186 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5187 EXPECT (Z_SYM_NAME));
5188 }
5189 }
5190
5191 /* In FULL mode, an incomplete symbol doesn't match. */
5192 {
5193 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5194 {});
5195 }
5196
5197 /* A complete symbol with parameters matches any overload, since the
5198 index has no overload info. */
5199 {
5200 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5201 EXPECT ("std::zfunction", "std::zfunction2"));
5202 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5203 EXPECT ("std::zfunction", "std::zfunction2"));
5204 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5205 EXPECT ("std::zfunction", "std::zfunction2"));
5206 }
5207
5208 /* Check that whitespace is ignored appropriately. A symbol with a
5209 template argument list. */
5210 {
5211 static const char expected[] = "ns::foo<int>";
5212 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5213 EXPECT (expected));
5214 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5215 EXPECT (expected));
5216 }
5217
5218 /* Check that whitespace is ignored appropriately. A symbol with a
5219 template argument list that includes a pointer. */
5220 {
5221 static const char expected[] = "ns::foo<char*>";
5222 /* Try both completion and non-completion modes. */
5223 static const bool completion_mode[2] = {false, true};
5224 for (size_t i = 0; i < 2; i++)
5225 {
5226 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5227 completion_mode[i], EXPECT (expected));
5228 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5229 completion_mode[i], EXPECT (expected));
5230
5231 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5232 completion_mode[i], EXPECT (expected));
5233 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5234 completion_mode[i], EXPECT (expected));
5235 }
5236 }
5237
5238 {
5239 /* Check method qualifiers are ignored. */
5240 static const char expected[] = "ns::foo<char*>";
5241 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5242 symbol_name_match_type::FULL, true, EXPECT (expected));
5243 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5244 symbol_name_match_type::FULL, true, EXPECT (expected));
5245 CHECK_MATCH ("foo < char * > ( int ) const",
5246 symbol_name_match_type::WILD, true, EXPECT (expected));
5247 CHECK_MATCH ("foo < char * > ( int ) &&",
5248 symbol_name_match_type::WILD, true, EXPECT (expected));
5249 }
5250
5251 /* Test lookup names that don't match anything. */
5252 {
5253 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5254 {});
5255
5256 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5257 {});
5258 }
5259
5260 /* Some wild matching tests, exercising "(anonymous namespace)",
5261 which should not be confused with a parameter list. */
5262 {
5263 static const char *syms[] = {
5264 "A::B::C",
5265 "B::C",
5266 "C",
5267 "A :: B :: C ( int )",
5268 "B :: C ( int )",
5269 "C ( int )",
5270 };
5271
5272 for (const char *s : syms)
5273 {
5274 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5275 EXPECT ("(anonymous namespace)::A::B::C"));
5276 }
5277 }
5278
5279 {
5280 static const char expected[] = "ns2::tmpl<int>::foo2";
5281 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5282 EXPECT (expected));
5283 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5284 EXPECT (expected));
5285 }
5286
5287 SELF_CHECK (!any_mismatch);
5288
5289 #undef EXPECT
5290 #undef CHECK_MATCH
5291 }
5292
5293 static void
5294 run_test ()
5295 {
5296 test_mapped_index_find_name_component_bounds ();
5297 test_dw2_expand_symtabs_matching_symbol ();
5298 }
5299
5300 }} // namespace selftests::dw2_expand_symtabs_matching
5301
5302 #endif /* GDB_SELF_TEST */
5303
5304 /* If FILE_MATCHER is NULL or if PER_CU has
5305 dwarf2_per_cu_quick_data::MARK set (see
5306 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5307 EXPANSION_NOTIFY on it. */
5308
5309 static void
5310 dw2_expand_symtabs_matching_one
5311 (struct dwarf2_per_cu_data *per_cu,
5312 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5313 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5314 {
5315 if (file_matcher == NULL || per_cu->v.quick->mark)
5316 {
5317 bool symtab_was_null
5318 = (per_cu->v.quick->compunit_symtab == NULL);
5319
5320 dw2_instantiate_symtab (per_cu);
5321
5322 if (expansion_notify != NULL
5323 && symtab_was_null
5324 && per_cu->v.quick->compunit_symtab != NULL)
5325 expansion_notify (per_cu->v.quick->compunit_symtab);
5326 }
5327 }
5328
5329 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5330 matched, to expand corresponding CUs that were marked. IDX is the
5331 index of the symbol name that matched. */
5332
5333 static void
5334 dw2_expand_marked_cus
5335 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5336 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5337 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5338 search_domain kind)
5339 {
5340 offset_type *vec, vec_len, vec_idx;
5341 bool global_seen = false;
5342 mapped_index &index = *dwarf2_per_objfile->index_table;
5343
5344 vec = (offset_type *) (index.constant_pool
5345 + MAYBE_SWAP (index.symbol_table[idx].vec));
5346 vec_len = MAYBE_SWAP (vec[0]);
5347 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5348 {
5349 struct dwarf2_per_cu_data *per_cu;
5350 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5351 /* This value is only valid for index versions >= 7. */
5352 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5353 gdb_index_symbol_kind symbol_kind =
5354 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5355 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5356 /* Only check the symbol attributes if they're present.
5357 Indices prior to version 7 don't record them,
5358 and indices >= 7 may elide them for certain symbols
5359 (gold does this). */
5360 int attrs_valid =
5361 (index.version >= 7
5362 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5363
5364 /* Work around gold/15646. */
5365 if (attrs_valid)
5366 {
5367 if (!is_static && global_seen)
5368 continue;
5369 if (!is_static)
5370 global_seen = true;
5371 }
5372
5373 /* Only check the symbol's kind if it has one. */
5374 if (attrs_valid)
5375 {
5376 switch (kind)
5377 {
5378 case VARIABLES_DOMAIN:
5379 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5380 continue;
5381 break;
5382 case FUNCTIONS_DOMAIN:
5383 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5384 continue;
5385 break;
5386 case TYPES_DOMAIN:
5387 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5388 continue;
5389 break;
5390 default:
5391 break;
5392 }
5393 }
5394
5395 /* Don't crash on bad data. */
5396 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5397 + dwarf2_per_objfile->n_type_units))
5398 {
5399 complaint (&symfile_complaints,
5400 _(".gdb_index entry has bad CU index"
5401 " [in module %s]"),
5402 objfile_name (dwarf2_per_objfile->objfile));
5403 continue;
5404 }
5405
5406 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
5407 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5408 expansion_notify);
5409 }
5410 }
5411
5412 /* If FILE_MATCHER is non-NULL, set all the
5413 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5414 that match FILE_MATCHER. */
5415
5416 static void
5417 dw_expand_symtabs_matching_file_matcher
5418 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5419 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5420 {
5421 if (file_matcher == NULL)
5422 return;
5423
5424 objfile *const objfile = dwarf2_per_objfile->objfile;
5425
5426 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5427 htab_eq_pointer,
5428 NULL, xcalloc, xfree));
5429 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5430 htab_eq_pointer,
5431 NULL, xcalloc, xfree));
5432
5433 /* The rule is CUs specify all the files, including those used by
5434 any TU, so there's no need to scan TUs here. */
5435
5436 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5437 {
5438 int j;
5439 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5440 struct quick_file_names *file_data;
5441 void **slot;
5442
5443 QUIT;
5444
5445 per_cu->v.quick->mark = 0;
5446
5447 /* We only need to look at symtabs not already expanded. */
5448 if (per_cu->v.quick->compunit_symtab)
5449 continue;
5450
5451 file_data = dw2_get_file_names (per_cu);
5452 if (file_data == NULL)
5453 continue;
5454
5455 if (htab_find (visited_not_found.get (), file_data) != NULL)
5456 continue;
5457 else if (htab_find (visited_found.get (), file_data) != NULL)
5458 {
5459 per_cu->v.quick->mark = 1;
5460 continue;
5461 }
5462
5463 for (j = 0; j < file_data->num_file_names; ++j)
5464 {
5465 const char *this_real_name;
5466
5467 if (file_matcher (file_data->file_names[j], false))
5468 {
5469 per_cu->v.quick->mark = 1;
5470 break;
5471 }
5472
5473 /* Before we invoke realpath, which can get expensive when many
5474 files are involved, do a quick comparison of the basenames. */
5475 if (!basenames_may_differ
5476 && !file_matcher (lbasename (file_data->file_names[j]),
5477 true))
5478 continue;
5479
5480 this_real_name = dw2_get_real_path (objfile, file_data, j);
5481 if (file_matcher (this_real_name, false))
5482 {
5483 per_cu->v.quick->mark = 1;
5484 break;
5485 }
5486 }
5487
5488 slot = htab_find_slot (per_cu->v.quick->mark
5489 ? visited_found.get ()
5490 : visited_not_found.get (),
5491 file_data, INSERT);
5492 *slot = file_data;
5493 }
5494 }
5495
5496 static void
5497 dw2_expand_symtabs_matching
5498 (struct objfile *objfile,
5499 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5500 const lookup_name_info &lookup_name,
5501 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5502 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5503 enum search_domain kind)
5504 {
5505 struct dwarf2_per_objfile *dwarf2_per_objfile
5506 = get_dwarf2_per_objfile (objfile);
5507
5508 /* index_table is NULL if OBJF_READNOW. */
5509 if (!dwarf2_per_objfile->index_table)
5510 return;
5511
5512 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5513
5514 mapped_index &index = *dwarf2_per_objfile->index_table;
5515
5516 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5517 symbol_matcher,
5518 kind, [&] (offset_type idx)
5519 {
5520 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5521 expansion_notify, kind);
5522 });
5523 }
5524
5525 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5526 symtab. */
5527
5528 static struct compunit_symtab *
5529 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5530 CORE_ADDR pc)
5531 {
5532 int i;
5533
5534 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5535 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5536 return cust;
5537
5538 if (cust->includes == NULL)
5539 return NULL;
5540
5541 for (i = 0; cust->includes[i]; ++i)
5542 {
5543 struct compunit_symtab *s = cust->includes[i];
5544
5545 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5546 if (s != NULL)
5547 return s;
5548 }
5549
5550 return NULL;
5551 }
5552
5553 static struct compunit_symtab *
5554 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5555 struct bound_minimal_symbol msymbol,
5556 CORE_ADDR pc,
5557 struct obj_section *section,
5558 int warn_if_readin)
5559 {
5560 struct dwarf2_per_cu_data *data;
5561 struct compunit_symtab *result;
5562
5563 if (!objfile->psymtabs_addrmap)
5564 return NULL;
5565
5566 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5567 pc);
5568 if (!data)
5569 return NULL;
5570
5571 if (warn_if_readin && data->v.quick->compunit_symtab)
5572 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5573 paddress (get_objfile_arch (objfile), pc));
5574
5575 result
5576 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5577 pc);
5578 gdb_assert (result != NULL);
5579 return result;
5580 }
5581
5582 static void
5583 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5584 void *data, int need_fullname)
5585 {
5586 struct dwarf2_per_objfile *dwarf2_per_objfile
5587 = get_dwarf2_per_objfile (objfile);
5588
5589 if (!dwarf2_per_objfile->filenames_cache)
5590 {
5591 dwarf2_per_objfile->filenames_cache.emplace ();
5592
5593 htab_up visited (htab_create_alloc (10,
5594 htab_hash_pointer, htab_eq_pointer,
5595 NULL, xcalloc, xfree));
5596
5597 /* The rule is CUs specify all the files, including those used
5598 by any TU, so there's no need to scan TUs here. We can
5599 ignore file names coming from already-expanded CUs. */
5600
5601 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5602 {
5603 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
5604
5605 if (per_cu->v.quick->compunit_symtab)
5606 {
5607 void **slot = htab_find_slot (visited.get (),
5608 per_cu->v.quick->file_names,
5609 INSERT);
5610
5611 *slot = per_cu->v.quick->file_names;
5612 }
5613 }
5614
5615 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5616 {
5617 dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5618 struct quick_file_names *file_data;
5619 void **slot;
5620
5621 /* We only need to look at symtabs not already expanded. */
5622 if (per_cu->v.quick->compunit_symtab)
5623 continue;
5624
5625 file_data = dw2_get_file_names (per_cu);
5626 if (file_data == NULL)
5627 continue;
5628
5629 slot = htab_find_slot (visited.get (), file_data, INSERT);
5630 if (*slot)
5631 {
5632 /* Already visited. */
5633 continue;
5634 }
5635 *slot = file_data;
5636
5637 for (int j = 0; j < file_data->num_file_names; ++j)
5638 {
5639 const char *filename = file_data->file_names[j];
5640 dwarf2_per_objfile->filenames_cache->seen (filename);
5641 }
5642 }
5643 }
5644
5645 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5646 {
5647 gdb::unique_xmalloc_ptr<char> this_real_name;
5648
5649 if (need_fullname)
5650 this_real_name = gdb_realpath (filename);
5651 (*fun) (filename, this_real_name.get (), data);
5652 });
5653 }
5654
5655 static int
5656 dw2_has_symbols (struct objfile *objfile)
5657 {
5658 return 1;
5659 }
5660
5661 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5662 {
5663 dw2_has_symbols,
5664 dw2_find_last_source_symtab,
5665 dw2_forget_cached_source_info,
5666 dw2_map_symtabs_matching_filename,
5667 dw2_lookup_symbol,
5668 dw2_print_stats,
5669 dw2_dump,
5670 dw2_relocate,
5671 dw2_expand_symtabs_for_function,
5672 dw2_expand_all_symtabs,
5673 dw2_expand_symtabs_with_fullname,
5674 dw2_map_matching_symbols,
5675 dw2_expand_symtabs_matching,
5676 dw2_find_pc_sect_compunit_symtab,
5677 NULL,
5678 dw2_map_symbol_filenames
5679 };
5680
5681 /* DWARF-5 debug_names reader. */
5682
5683 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5684 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5685
5686 /* A helper function that reads the .debug_names section in SECTION
5687 and fills in MAP. FILENAME is the name of the file containing the
5688 section; it is used for error reporting.
5689
5690 Returns true if all went well, false otherwise. */
5691
5692 static bool
5693 read_debug_names_from_section (struct objfile *objfile,
5694 const char *filename,
5695 struct dwarf2_section_info *section,
5696 mapped_debug_names &map)
5697 {
5698 if (dwarf2_section_empty_p (section))
5699 return false;
5700
5701 /* Older elfutils strip versions could keep the section in the main
5702 executable while splitting it for the separate debug info file. */
5703 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5704 return false;
5705
5706 dwarf2_read_section (objfile, section);
5707
5708 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5709
5710 const gdb_byte *addr = section->buffer;
5711
5712 bfd *const abfd = get_section_bfd_owner (section);
5713
5714 unsigned int bytes_read;
5715 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5716 addr += bytes_read;
5717
5718 map.dwarf5_is_dwarf64 = bytes_read != 4;
5719 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5720 if (bytes_read + length != section->size)
5721 {
5722 /* There may be multiple per-CU indices. */
5723 warning (_("Section .debug_names in %s length %s does not match "
5724 "section length %s, ignoring .debug_names."),
5725 filename, plongest (bytes_read + length),
5726 pulongest (section->size));
5727 return false;
5728 }
5729
5730 /* The version number. */
5731 uint16_t version = read_2_bytes (abfd, addr);
5732 addr += 2;
5733 if (version != 5)
5734 {
5735 warning (_("Section .debug_names in %s has unsupported version %d, "
5736 "ignoring .debug_names."),
5737 filename, version);
5738 return false;
5739 }
5740
5741 /* Padding. */
5742 uint16_t padding = read_2_bytes (abfd, addr);
5743 addr += 2;
5744 if (padding != 0)
5745 {
5746 warning (_("Section .debug_names in %s has unsupported padding %d, "
5747 "ignoring .debug_names."),
5748 filename, padding);
5749 return false;
5750 }
5751
5752 /* comp_unit_count - The number of CUs in the CU list. */
5753 map.cu_count = read_4_bytes (abfd, addr);
5754 addr += 4;
5755
5756 /* local_type_unit_count - The number of TUs in the local TU
5757 list. */
5758 map.tu_count = read_4_bytes (abfd, addr);
5759 addr += 4;
5760
5761 /* foreign_type_unit_count - The number of TUs in the foreign TU
5762 list. */
5763 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5764 addr += 4;
5765 if (foreign_tu_count != 0)
5766 {
5767 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5768 "ignoring .debug_names."),
5769 filename, static_cast<unsigned long> (foreign_tu_count));
5770 return false;
5771 }
5772
5773 /* bucket_count - The number of hash buckets in the hash lookup
5774 table. */
5775 map.bucket_count = read_4_bytes (abfd, addr);
5776 addr += 4;
5777
5778 /* name_count - The number of unique names in the index. */
5779 map.name_count = read_4_bytes (abfd, addr);
5780 addr += 4;
5781
5782 /* abbrev_table_size - The size in bytes of the abbreviations
5783 table. */
5784 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5785 addr += 4;
5786
5787 /* augmentation_string_size - The size in bytes of the augmentation
5788 string. This value is rounded up to a multiple of 4. */
5789 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5790 addr += 4;
5791 map.augmentation_is_gdb = ((augmentation_string_size
5792 == sizeof (dwarf5_augmentation))
5793 && memcmp (addr, dwarf5_augmentation,
5794 sizeof (dwarf5_augmentation)) == 0);
5795 augmentation_string_size += (-augmentation_string_size) & 3;
5796 addr += augmentation_string_size;
5797
5798 /* List of CUs */
5799 map.cu_table_reordered = addr;
5800 addr += map.cu_count * map.offset_size;
5801
5802 /* List of Local TUs */
5803 map.tu_table_reordered = addr;
5804 addr += map.tu_count * map.offset_size;
5805
5806 /* Hash Lookup Table */
5807 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5808 addr += map.bucket_count * 4;
5809 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5810 addr += map.name_count * 4;
5811
5812 /* Name Table */
5813 map.name_table_string_offs_reordered = addr;
5814 addr += map.name_count * map.offset_size;
5815 map.name_table_entry_offs_reordered = addr;
5816 addr += map.name_count * map.offset_size;
5817
5818 const gdb_byte *abbrev_table_start = addr;
5819 for (;;)
5820 {
5821 unsigned int bytes_read;
5822 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5823 addr += bytes_read;
5824 if (index_num == 0)
5825 break;
5826
5827 const auto insertpair
5828 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5829 if (!insertpair.second)
5830 {
5831 warning (_("Section .debug_names in %s has duplicate index %s, "
5832 "ignoring .debug_names."),
5833 filename, pulongest (index_num));
5834 return false;
5835 }
5836 mapped_debug_names::index_val &indexval = insertpair.first->second;
5837 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5838 addr += bytes_read;
5839
5840 for (;;)
5841 {
5842 mapped_debug_names::index_val::attr attr;
5843 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5844 addr += bytes_read;
5845 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5846 addr += bytes_read;
5847 if (attr.form == DW_FORM_implicit_const)
5848 {
5849 attr.implicit_const = read_signed_leb128 (abfd, addr,
5850 &bytes_read);
5851 addr += bytes_read;
5852 }
5853 if (attr.dw_idx == 0 && attr.form == 0)
5854 break;
5855 indexval.attr_vec.push_back (std::move (attr));
5856 }
5857 }
5858 if (addr != abbrev_table_start + abbrev_table_size)
5859 {
5860 warning (_("Section .debug_names in %s has abbreviation_table "
5861 "of size %zu vs. written as %u, ignoring .debug_names."),
5862 filename, addr - abbrev_table_start, abbrev_table_size);
5863 return false;
5864 }
5865 map.entry_pool = addr;
5866
5867 return true;
5868 }
5869
5870 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5871 list. */
5872
5873 static void
5874 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5875 const mapped_debug_names &map,
5876 dwarf2_section_info &section,
5877 bool is_dwz, int base_offset)
5878 {
5879 sect_offset sect_off_prev;
5880 for (uint32_t i = 0; i <= map.cu_count; ++i)
5881 {
5882 sect_offset sect_off_next;
5883 if (i < map.cu_count)
5884 {
5885 sect_off_next
5886 = (sect_offset) (extract_unsigned_integer
5887 (map.cu_table_reordered + i * map.offset_size,
5888 map.offset_size,
5889 map.dwarf5_byte_order));
5890 }
5891 else
5892 sect_off_next = (sect_offset) section.size;
5893 if (i >= 1)
5894 {
5895 const ULONGEST length = sect_off_next - sect_off_prev;
5896 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
5897 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5898 sect_off_prev, length);
5899 }
5900 sect_off_prev = sect_off_next;
5901 }
5902 }
5903
5904 /* Read the CU list from the mapped index, and use it to create all
5905 the CU objects for this dwarf2_per_objfile. */
5906
5907 static void
5908 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5909 const mapped_debug_names &map,
5910 const mapped_debug_names &dwz_map)
5911 {
5912 struct objfile *objfile = dwarf2_per_objfile->objfile;
5913
5914 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
5915 dwarf2_per_objfile->all_comp_units
5916 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
5917 dwarf2_per_objfile->n_comp_units);
5918
5919 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5920 dwarf2_per_objfile->info,
5921 false /* is_dwz */,
5922 0 /* base_offset */);
5923
5924 if (dwz_map.cu_count == 0)
5925 return;
5926
5927 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5928 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5929 true /* is_dwz */,
5930 map.cu_count /* base_offset */);
5931 }
5932
5933 /* Read .debug_names. If everything went ok, initialize the "quick"
5934 elements of all the CUs and return true. Otherwise, return false. */
5935
5936 static bool
5937 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5938 {
5939 mapped_debug_names local_map (dwarf2_per_objfile);
5940 mapped_debug_names dwz_map (dwarf2_per_objfile);
5941 struct objfile *objfile = dwarf2_per_objfile->objfile;
5942
5943 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5944 &dwarf2_per_objfile->debug_names,
5945 local_map))
5946 return false;
5947
5948 /* Don't use the index if it's empty. */
5949 if (local_map.name_count == 0)
5950 return false;
5951
5952 /* If there is a .dwz file, read it so we can get its CU list as
5953 well. */
5954 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5955 if (dwz != NULL)
5956 {
5957 if (!read_debug_names_from_section (objfile,
5958 bfd_get_filename (dwz->dwz_bfd),
5959 &dwz->debug_names, dwz_map))
5960 {
5961 warning (_("could not read '.debug_names' section from %s; skipping"),
5962 bfd_get_filename (dwz->dwz_bfd));
5963 return false;
5964 }
5965 }
5966
5967 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
5968
5969 if (local_map.tu_count != 0)
5970 {
5971 /* We can only handle a single .debug_types when we have an
5972 index. */
5973 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5974 return false;
5975
5976 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5977 dwarf2_per_objfile->types, 0);
5978
5979 create_signatured_type_table_from_debug_names
5980 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
5981 }
5982
5983 create_addrmap_from_aranges (dwarf2_per_objfile,
5984 &dwarf2_per_objfile->debug_aranges);
5985
5986 dwarf2_per_objfile->debug_names_table.reset
5987 (new mapped_debug_names (dwarf2_per_objfile));
5988 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
5989 dwarf2_per_objfile->using_index = 1;
5990 dwarf2_per_objfile->quick_file_names_table =
5991 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
5992
5993 return true;
5994 }
5995
5996 /* Symbol name hashing function as specified by DWARF-5. */
5997
5998 static uint32_t
5999 dwarf5_djb_hash (const char *str_)
6000 {
6001 const unsigned char *str = (const unsigned char *) str_;
6002
6003 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
6004 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
6005
6006 uint32_t hash = 5381;
6007 while (int c = *str++)
6008 hash = hash * 33 + tolower (c);
6009 return hash;
6010 }
6011
6012 /* Type used to manage iterating over all CUs looking for a symbol for
6013 .debug_names. */
6014
6015 class dw2_debug_names_iterator
6016 {
6017 public:
6018 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
6019 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
6020 dw2_debug_names_iterator (const mapped_debug_names &map,
6021 bool want_specific_block,
6022 block_enum block_index, domain_enum domain,
6023 const char *name)
6024 : m_map (map), m_want_specific_block (want_specific_block),
6025 m_block_index (block_index), m_domain (domain),
6026 m_addr (find_vec_in_debug_names (map, name))
6027 {}
6028
6029 dw2_debug_names_iterator (const mapped_debug_names &map,
6030 search_domain search, uint32_t namei)
6031 : m_map (map),
6032 m_search (search),
6033 m_addr (find_vec_in_debug_names (map, namei))
6034 {}
6035
6036 /* Return the next matching CU or NULL if there are no more. */
6037 dwarf2_per_cu_data *next ();
6038
6039 private:
6040 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6041 const char *name);
6042 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6043 uint32_t namei);
6044
6045 /* The internalized form of .debug_names. */
6046 const mapped_debug_names &m_map;
6047
6048 /* If true, only look for symbols that match BLOCK_INDEX. */
6049 const bool m_want_specific_block = false;
6050
6051 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
6052 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
6053 value. */
6054 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
6055
6056 /* The kind of symbol we're looking for. */
6057 const domain_enum m_domain = UNDEF_DOMAIN;
6058 const search_domain m_search = ALL_DOMAIN;
6059
6060 /* The list of CUs from the index entry of the symbol, or NULL if
6061 not found. */
6062 const gdb_byte *m_addr;
6063 };
6064
6065 const char *
6066 mapped_debug_names::namei_to_name (uint32_t namei) const
6067 {
6068 const ULONGEST namei_string_offs
6069 = extract_unsigned_integer ((name_table_string_offs_reordered
6070 + namei * offset_size),
6071 offset_size,
6072 dwarf5_byte_order);
6073 return read_indirect_string_at_offset
6074 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
6075 }
6076
6077 /* Find a slot in .debug_names for the object named NAME. If NAME is
6078 found, return pointer to its pool data. If NAME cannot be found,
6079 return NULL. */
6080
6081 const gdb_byte *
6082 dw2_debug_names_iterator::find_vec_in_debug_names
6083 (const mapped_debug_names &map, const char *name)
6084 {
6085 int (*cmp) (const char *, const char *);
6086
6087 if (current_language->la_language == language_cplus
6088 || current_language->la_language == language_fortran
6089 || current_language->la_language == language_d)
6090 {
6091 /* NAME is already canonical. Drop any qualifiers as
6092 .debug_names does not contain any. */
6093
6094 if (strchr (name, '(') != NULL)
6095 {
6096 gdb::unique_xmalloc_ptr<char> without_params
6097 = cp_remove_params (name);
6098
6099 if (without_params != NULL)
6100 {
6101 name = without_params.get();
6102 }
6103 }
6104 }
6105
6106 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6107
6108 const uint32_t full_hash = dwarf5_djb_hash (name);
6109 uint32_t namei
6110 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6111 (map.bucket_table_reordered
6112 + (full_hash % map.bucket_count)), 4,
6113 map.dwarf5_byte_order);
6114 if (namei == 0)
6115 return NULL;
6116 --namei;
6117 if (namei >= map.name_count)
6118 {
6119 complaint (&symfile_complaints,
6120 _("Wrong .debug_names with name index %u but name_count=%u "
6121 "[in module %s]"),
6122 namei, map.name_count,
6123 objfile_name (map.dwarf2_per_objfile->objfile));
6124 return NULL;
6125 }
6126
6127 for (;;)
6128 {
6129 const uint32_t namei_full_hash
6130 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6131 (map.hash_table_reordered + namei), 4,
6132 map.dwarf5_byte_order);
6133 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6134 return NULL;
6135
6136 if (full_hash == namei_full_hash)
6137 {
6138 const char *const namei_string = map.namei_to_name (namei);
6139
6140 #if 0 /* An expensive sanity check. */
6141 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6142 {
6143 complaint (&symfile_complaints,
6144 _("Wrong .debug_names hash for string at index %u "
6145 "[in module %s]"),
6146 namei, objfile_name (dwarf2_per_objfile->objfile));
6147 return NULL;
6148 }
6149 #endif
6150
6151 if (cmp (namei_string, name) == 0)
6152 {
6153 const ULONGEST namei_entry_offs
6154 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6155 + namei * map.offset_size),
6156 map.offset_size, map.dwarf5_byte_order);
6157 return map.entry_pool + namei_entry_offs;
6158 }
6159 }
6160
6161 ++namei;
6162 if (namei >= map.name_count)
6163 return NULL;
6164 }
6165 }
6166
6167 const gdb_byte *
6168 dw2_debug_names_iterator::find_vec_in_debug_names
6169 (const mapped_debug_names &map, uint32_t namei)
6170 {
6171 if (namei >= map.name_count)
6172 {
6173 complaint (&symfile_complaints,
6174 _("Wrong .debug_names with name index %u but name_count=%u "
6175 "[in module %s]"),
6176 namei, map.name_count,
6177 objfile_name (map.dwarf2_per_objfile->objfile));
6178 return NULL;
6179 }
6180
6181 const ULONGEST namei_entry_offs
6182 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6183 + namei * map.offset_size),
6184 map.offset_size, map.dwarf5_byte_order);
6185 return map.entry_pool + namei_entry_offs;
6186 }
6187
6188 /* See dw2_debug_names_iterator. */
6189
6190 dwarf2_per_cu_data *
6191 dw2_debug_names_iterator::next ()
6192 {
6193 if (m_addr == NULL)
6194 return NULL;
6195
6196 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
6197 struct objfile *objfile = dwarf2_per_objfile->objfile;
6198 bfd *const abfd = objfile->obfd;
6199
6200 again:
6201
6202 unsigned int bytes_read;
6203 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6204 m_addr += bytes_read;
6205 if (abbrev == 0)
6206 return NULL;
6207
6208 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6209 if (indexval_it == m_map.abbrev_map.cend ())
6210 {
6211 complaint (&symfile_complaints,
6212 _("Wrong .debug_names undefined abbrev code %s "
6213 "[in module %s]"),
6214 pulongest (abbrev), objfile_name (objfile));
6215 return NULL;
6216 }
6217 const mapped_debug_names::index_val &indexval = indexval_it->second;
6218 bool have_is_static = false;
6219 bool is_static;
6220 dwarf2_per_cu_data *per_cu = NULL;
6221 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6222 {
6223 ULONGEST ull;
6224 switch (attr.form)
6225 {
6226 case DW_FORM_implicit_const:
6227 ull = attr.implicit_const;
6228 break;
6229 case DW_FORM_flag_present:
6230 ull = 1;
6231 break;
6232 case DW_FORM_udata:
6233 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6234 m_addr += bytes_read;
6235 break;
6236 default:
6237 complaint (&symfile_complaints,
6238 _("Unsupported .debug_names form %s [in module %s]"),
6239 dwarf_form_name (attr.form),
6240 objfile_name (objfile));
6241 return NULL;
6242 }
6243 switch (attr.dw_idx)
6244 {
6245 case DW_IDX_compile_unit:
6246 /* Don't crash on bad data. */
6247 if (ull >= dwarf2_per_objfile->n_comp_units)
6248 {
6249 complaint (&symfile_complaints,
6250 _(".debug_names entry has bad CU index %s"
6251 " [in module %s]"),
6252 pulongest (ull),
6253 objfile_name (dwarf2_per_objfile->objfile));
6254 continue;
6255 }
6256 per_cu = dw2_get_cutu (dwarf2_per_objfile, ull);
6257 break;
6258 case DW_IDX_type_unit:
6259 /* Don't crash on bad data. */
6260 if (ull >= dwarf2_per_objfile->n_type_units)
6261 {
6262 complaint (&symfile_complaints,
6263 _(".debug_names entry has bad TU index %s"
6264 " [in module %s]"),
6265 pulongest (ull),
6266 objfile_name (dwarf2_per_objfile->objfile));
6267 continue;
6268 }
6269 per_cu = dw2_get_cutu (dwarf2_per_objfile,
6270 dwarf2_per_objfile->n_comp_units + ull);
6271 break;
6272 case DW_IDX_GNU_internal:
6273 if (!m_map.augmentation_is_gdb)
6274 break;
6275 have_is_static = true;
6276 is_static = true;
6277 break;
6278 case DW_IDX_GNU_external:
6279 if (!m_map.augmentation_is_gdb)
6280 break;
6281 have_is_static = true;
6282 is_static = false;
6283 break;
6284 }
6285 }
6286
6287 /* Skip if already read in. */
6288 if (per_cu->v.quick->compunit_symtab)
6289 goto again;
6290
6291 /* Check static vs global. */
6292 if (have_is_static)
6293 {
6294 const bool want_static = m_block_index != GLOBAL_BLOCK;
6295 if (m_want_specific_block && want_static != is_static)
6296 goto again;
6297 }
6298
6299 /* Match dw2_symtab_iter_next, symbol_kind
6300 and debug_names::psymbol_tag. */
6301 switch (m_domain)
6302 {
6303 case VAR_DOMAIN:
6304 switch (indexval.dwarf_tag)
6305 {
6306 case DW_TAG_variable:
6307 case DW_TAG_subprogram:
6308 /* Some types are also in VAR_DOMAIN. */
6309 case DW_TAG_typedef:
6310 case DW_TAG_structure_type:
6311 break;
6312 default:
6313 goto again;
6314 }
6315 break;
6316 case STRUCT_DOMAIN:
6317 switch (indexval.dwarf_tag)
6318 {
6319 case DW_TAG_typedef:
6320 case DW_TAG_structure_type:
6321 break;
6322 default:
6323 goto again;
6324 }
6325 break;
6326 case LABEL_DOMAIN:
6327 switch (indexval.dwarf_tag)
6328 {
6329 case 0:
6330 case DW_TAG_variable:
6331 break;
6332 default:
6333 goto again;
6334 }
6335 break;
6336 default:
6337 break;
6338 }
6339
6340 /* Match dw2_expand_symtabs_matching, symbol_kind and
6341 debug_names::psymbol_tag. */
6342 switch (m_search)
6343 {
6344 case VARIABLES_DOMAIN:
6345 switch (indexval.dwarf_tag)
6346 {
6347 case DW_TAG_variable:
6348 break;
6349 default:
6350 goto again;
6351 }
6352 break;
6353 case FUNCTIONS_DOMAIN:
6354 switch (indexval.dwarf_tag)
6355 {
6356 case DW_TAG_subprogram:
6357 break;
6358 default:
6359 goto again;
6360 }
6361 break;
6362 case TYPES_DOMAIN:
6363 switch (indexval.dwarf_tag)
6364 {
6365 case DW_TAG_typedef:
6366 case DW_TAG_structure_type:
6367 break;
6368 default:
6369 goto again;
6370 }
6371 break;
6372 default:
6373 break;
6374 }
6375
6376 return per_cu;
6377 }
6378
6379 static struct compunit_symtab *
6380 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6381 const char *name, domain_enum domain)
6382 {
6383 const block_enum block_index = static_cast<block_enum> (block_index_int);
6384 struct dwarf2_per_objfile *dwarf2_per_objfile
6385 = get_dwarf2_per_objfile (objfile);
6386
6387 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6388 if (!mapp)
6389 {
6390 /* index is NULL if OBJF_READNOW. */
6391 return NULL;
6392 }
6393 const auto &map = *mapp;
6394
6395 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6396 block_index, domain, name);
6397
6398 struct compunit_symtab *stab_best = NULL;
6399 struct dwarf2_per_cu_data *per_cu;
6400 while ((per_cu = iter.next ()) != NULL)
6401 {
6402 struct symbol *sym, *with_opaque = NULL;
6403 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6404 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6405 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6406
6407 sym = block_find_symbol (block, name, domain,
6408 block_find_non_opaque_type_preferred,
6409 &with_opaque);
6410
6411 /* Some caution must be observed with overloaded functions and
6412 methods, since the index will not contain any overload
6413 information (but NAME might contain it). */
6414
6415 if (sym != NULL
6416 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6417 return stab;
6418 if (with_opaque != NULL
6419 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6420 stab_best = stab;
6421
6422 /* Keep looking through other CUs. */
6423 }
6424
6425 return stab_best;
6426 }
6427
6428 /* This dumps minimal information about .debug_names. It is called
6429 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6430 uses this to verify that .debug_names has been loaded. */
6431
6432 static void
6433 dw2_debug_names_dump (struct objfile *objfile)
6434 {
6435 struct dwarf2_per_objfile *dwarf2_per_objfile
6436 = get_dwarf2_per_objfile (objfile);
6437
6438 gdb_assert (dwarf2_per_objfile->using_index);
6439 printf_filtered (".debug_names:");
6440 if (dwarf2_per_objfile->debug_names_table)
6441 printf_filtered (" exists\n");
6442 else
6443 printf_filtered (" faked for \"readnow\"\n");
6444 printf_filtered ("\n");
6445 }
6446
6447 static void
6448 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6449 const char *func_name)
6450 {
6451 struct dwarf2_per_objfile *dwarf2_per_objfile
6452 = get_dwarf2_per_objfile (objfile);
6453
6454 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6455 if (dwarf2_per_objfile->debug_names_table)
6456 {
6457 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6458
6459 /* Note: It doesn't matter what we pass for block_index here. */
6460 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6461 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6462
6463 struct dwarf2_per_cu_data *per_cu;
6464 while ((per_cu = iter.next ()) != NULL)
6465 dw2_instantiate_symtab (per_cu);
6466 }
6467 }
6468
6469 static void
6470 dw2_debug_names_expand_symtabs_matching
6471 (struct objfile *objfile,
6472 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6473 const lookup_name_info &lookup_name,
6474 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6475 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6476 enum search_domain kind)
6477 {
6478 struct dwarf2_per_objfile *dwarf2_per_objfile
6479 = get_dwarf2_per_objfile (objfile);
6480
6481 /* debug_names_table is NULL if OBJF_READNOW. */
6482 if (!dwarf2_per_objfile->debug_names_table)
6483 return;
6484
6485 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6486
6487 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6488
6489 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6490 symbol_matcher,
6491 kind, [&] (offset_type namei)
6492 {
6493 /* The name was matched, now expand corresponding CUs that were
6494 marked. */
6495 dw2_debug_names_iterator iter (map, kind, namei);
6496
6497 struct dwarf2_per_cu_data *per_cu;
6498 while ((per_cu = iter.next ()) != NULL)
6499 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6500 expansion_notify);
6501 });
6502 }
6503
6504 const struct quick_symbol_functions dwarf2_debug_names_functions =
6505 {
6506 dw2_has_symbols,
6507 dw2_find_last_source_symtab,
6508 dw2_forget_cached_source_info,
6509 dw2_map_symtabs_matching_filename,
6510 dw2_debug_names_lookup_symbol,
6511 dw2_print_stats,
6512 dw2_debug_names_dump,
6513 dw2_relocate,
6514 dw2_debug_names_expand_symtabs_for_function,
6515 dw2_expand_all_symtabs,
6516 dw2_expand_symtabs_with_fullname,
6517 dw2_map_matching_symbols,
6518 dw2_debug_names_expand_symtabs_matching,
6519 dw2_find_pc_sect_compunit_symtab,
6520 NULL,
6521 dw2_map_symbol_filenames
6522 };
6523
6524 /* See symfile.h. */
6525
6526 bool
6527 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6528 {
6529 struct dwarf2_per_objfile *dwarf2_per_objfile
6530 = get_dwarf2_per_objfile (objfile);
6531
6532 /* If we're about to read full symbols, don't bother with the
6533 indices. In this case we also don't care if some other debug
6534 format is making psymtabs, because they are all about to be
6535 expanded anyway. */
6536 if ((objfile->flags & OBJF_READNOW))
6537 {
6538 int i;
6539
6540 dwarf2_per_objfile->using_index = 1;
6541 create_all_comp_units (dwarf2_per_objfile);
6542 create_all_type_units (dwarf2_per_objfile);
6543 dwarf2_per_objfile->quick_file_names_table =
6544 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6545
6546 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
6547 + dwarf2_per_objfile->n_type_units); ++i)
6548 {
6549 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
6550
6551 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6552 struct dwarf2_per_cu_quick_data);
6553 }
6554
6555 /* Return 1 so that gdb sees the "quick" functions. However,
6556 these functions will be no-ops because we will have expanded
6557 all symtabs. */
6558 *index_kind = dw_index_kind::GDB_INDEX;
6559 return true;
6560 }
6561
6562 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6563 {
6564 *index_kind = dw_index_kind::DEBUG_NAMES;
6565 return true;
6566 }
6567
6568 if (dwarf2_read_index (objfile))
6569 {
6570 *index_kind = dw_index_kind::GDB_INDEX;
6571 return true;
6572 }
6573
6574 return false;
6575 }
6576
6577 \f
6578
6579 /* Build a partial symbol table. */
6580
6581 void
6582 dwarf2_build_psymtabs (struct objfile *objfile)
6583 {
6584 struct dwarf2_per_objfile *dwarf2_per_objfile
6585 = get_dwarf2_per_objfile (objfile);
6586
6587 if (objfile->global_psymbols.capacity () == 0
6588 && objfile->static_psymbols.capacity () == 0)
6589 init_psymbol_list (objfile, 1024);
6590
6591 TRY
6592 {
6593 /* This isn't really ideal: all the data we allocate on the
6594 objfile's obstack is still uselessly kept around. However,
6595 freeing it seems unsafe. */
6596 psymtab_discarder psymtabs (objfile);
6597 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6598 psymtabs.keep ();
6599 }
6600 CATCH (except, RETURN_MASK_ERROR)
6601 {
6602 exception_print (gdb_stderr, except);
6603 }
6604 END_CATCH
6605 }
6606
6607 /* Return the total length of the CU described by HEADER. */
6608
6609 static unsigned int
6610 get_cu_length (const struct comp_unit_head *header)
6611 {
6612 return header->initial_length_size + header->length;
6613 }
6614
6615 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6616
6617 static inline bool
6618 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6619 {
6620 sect_offset bottom = cu_header->sect_off;
6621 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6622
6623 return sect_off >= bottom && sect_off < top;
6624 }
6625
6626 /* Find the base address of the compilation unit for range lists and
6627 location lists. It will normally be specified by DW_AT_low_pc.
6628 In DWARF-3 draft 4, the base address could be overridden by
6629 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6630 compilation units with discontinuous ranges. */
6631
6632 static void
6633 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6634 {
6635 struct attribute *attr;
6636
6637 cu->base_known = 0;
6638 cu->base_address = 0;
6639
6640 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6641 if (attr)
6642 {
6643 cu->base_address = attr_value_as_address (attr);
6644 cu->base_known = 1;
6645 }
6646 else
6647 {
6648 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6649 if (attr)
6650 {
6651 cu->base_address = attr_value_as_address (attr);
6652 cu->base_known = 1;
6653 }
6654 }
6655 }
6656
6657 /* Read in the comp unit header information from the debug_info at info_ptr.
6658 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6659 NOTE: This leaves members offset, first_die_offset to be filled in
6660 by the caller. */
6661
6662 static const gdb_byte *
6663 read_comp_unit_head (struct comp_unit_head *cu_header,
6664 const gdb_byte *info_ptr,
6665 struct dwarf2_section_info *section,
6666 rcuh_kind section_kind)
6667 {
6668 int signed_addr;
6669 unsigned int bytes_read;
6670 const char *filename = get_section_file_name (section);
6671 bfd *abfd = get_section_bfd_owner (section);
6672
6673 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6674 cu_header->initial_length_size = bytes_read;
6675 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6676 info_ptr += bytes_read;
6677 cu_header->version = read_2_bytes (abfd, info_ptr);
6678 info_ptr += 2;
6679 if (cu_header->version < 5)
6680 switch (section_kind)
6681 {
6682 case rcuh_kind::COMPILE:
6683 cu_header->unit_type = DW_UT_compile;
6684 break;
6685 case rcuh_kind::TYPE:
6686 cu_header->unit_type = DW_UT_type;
6687 break;
6688 default:
6689 internal_error (__FILE__, __LINE__,
6690 _("read_comp_unit_head: invalid section_kind"));
6691 }
6692 else
6693 {
6694 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6695 (read_1_byte (abfd, info_ptr));
6696 info_ptr += 1;
6697 switch (cu_header->unit_type)
6698 {
6699 case DW_UT_compile:
6700 if (section_kind != rcuh_kind::COMPILE)
6701 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6702 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6703 filename);
6704 break;
6705 case DW_UT_type:
6706 section_kind = rcuh_kind::TYPE;
6707 break;
6708 default:
6709 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6710 "(is %d, should be %d or %d) [in module %s]"),
6711 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6712 }
6713
6714 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6715 info_ptr += 1;
6716 }
6717 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6718 cu_header,
6719 &bytes_read);
6720 info_ptr += bytes_read;
6721 if (cu_header->version < 5)
6722 {
6723 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6724 info_ptr += 1;
6725 }
6726 signed_addr = bfd_get_sign_extend_vma (abfd);
6727 if (signed_addr < 0)
6728 internal_error (__FILE__, __LINE__,
6729 _("read_comp_unit_head: dwarf from non elf file"));
6730 cu_header->signed_addr_p = signed_addr;
6731
6732 if (section_kind == rcuh_kind::TYPE)
6733 {
6734 LONGEST type_offset;
6735
6736 cu_header->signature = read_8_bytes (abfd, info_ptr);
6737 info_ptr += 8;
6738
6739 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6740 info_ptr += bytes_read;
6741 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6742 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6743 error (_("Dwarf Error: Too big type_offset in compilation unit "
6744 "header (is %s) [in module %s]"), plongest (type_offset),
6745 filename);
6746 }
6747
6748 return info_ptr;
6749 }
6750
6751 /* Helper function that returns the proper abbrev section for
6752 THIS_CU. */
6753
6754 static struct dwarf2_section_info *
6755 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6756 {
6757 struct dwarf2_section_info *abbrev;
6758 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6759
6760 if (this_cu->is_dwz)
6761 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6762 else
6763 abbrev = &dwarf2_per_objfile->abbrev;
6764
6765 return abbrev;
6766 }
6767
6768 /* Subroutine of read_and_check_comp_unit_head and
6769 read_and_check_type_unit_head to simplify them.
6770 Perform various error checking on the header. */
6771
6772 static void
6773 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6774 struct comp_unit_head *header,
6775 struct dwarf2_section_info *section,
6776 struct dwarf2_section_info *abbrev_section)
6777 {
6778 const char *filename = get_section_file_name (section);
6779
6780 if (header->version < 2 || header->version > 5)
6781 error (_("Dwarf Error: wrong version in compilation unit header "
6782 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6783 filename);
6784
6785 if (to_underlying (header->abbrev_sect_off)
6786 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6787 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
6788 "(offset 0x%x + 6) [in module %s]"),
6789 to_underlying (header->abbrev_sect_off),
6790 to_underlying (header->sect_off),
6791 filename);
6792
6793 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6794 avoid potential 32-bit overflow. */
6795 if (((ULONGEST) header->sect_off + get_cu_length (header))
6796 > section->size)
6797 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6798 "(offset 0x%x + 0) [in module %s]"),
6799 header->length, to_underlying (header->sect_off),
6800 filename);
6801 }
6802
6803 /* Read in a CU/TU header and perform some basic error checking.
6804 The contents of the header are stored in HEADER.
6805 The result is a pointer to the start of the first DIE. */
6806
6807 static const gdb_byte *
6808 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6809 struct comp_unit_head *header,
6810 struct dwarf2_section_info *section,
6811 struct dwarf2_section_info *abbrev_section,
6812 const gdb_byte *info_ptr,
6813 rcuh_kind section_kind)
6814 {
6815 const gdb_byte *beg_of_comp_unit = info_ptr;
6816
6817 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6818
6819 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6820
6821 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6822
6823 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6824 abbrev_section);
6825
6826 return info_ptr;
6827 }
6828
6829 /* Fetch the abbreviation table offset from a comp or type unit header. */
6830
6831 static sect_offset
6832 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6833 struct dwarf2_section_info *section,
6834 sect_offset sect_off)
6835 {
6836 bfd *abfd = get_section_bfd_owner (section);
6837 const gdb_byte *info_ptr;
6838 unsigned int initial_length_size, offset_size;
6839 uint16_t version;
6840
6841 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6842 info_ptr = section->buffer + to_underlying (sect_off);
6843 read_initial_length (abfd, info_ptr, &initial_length_size);
6844 offset_size = initial_length_size == 4 ? 4 : 8;
6845 info_ptr += initial_length_size;
6846
6847 version = read_2_bytes (abfd, info_ptr);
6848 info_ptr += 2;
6849 if (version >= 5)
6850 {
6851 /* Skip unit type and address size. */
6852 info_ptr += 2;
6853 }
6854
6855 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6856 }
6857
6858 /* Allocate a new partial symtab for file named NAME and mark this new
6859 partial symtab as being an include of PST. */
6860
6861 static void
6862 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6863 struct objfile *objfile)
6864 {
6865 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6866
6867 if (!IS_ABSOLUTE_PATH (subpst->filename))
6868 {
6869 /* It shares objfile->objfile_obstack. */
6870 subpst->dirname = pst->dirname;
6871 }
6872
6873 subpst->textlow = 0;
6874 subpst->texthigh = 0;
6875
6876 subpst->dependencies
6877 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6878 subpst->dependencies[0] = pst;
6879 subpst->number_of_dependencies = 1;
6880
6881 subpst->globals_offset = 0;
6882 subpst->n_global_syms = 0;
6883 subpst->statics_offset = 0;
6884 subpst->n_static_syms = 0;
6885 subpst->compunit_symtab = NULL;
6886 subpst->read_symtab = pst->read_symtab;
6887 subpst->readin = 0;
6888
6889 /* No private part is necessary for include psymtabs. This property
6890 can be used to differentiate between such include psymtabs and
6891 the regular ones. */
6892 subpst->read_symtab_private = NULL;
6893 }
6894
6895 /* Read the Line Number Program data and extract the list of files
6896 included by the source file represented by PST. Build an include
6897 partial symtab for each of these included files. */
6898
6899 static void
6900 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6901 struct die_info *die,
6902 struct partial_symtab *pst)
6903 {
6904 line_header_up lh;
6905 struct attribute *attr;
6906
6907 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6908 if (attr)
6909 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6910 if (lh == NULL)
6911 return; /* No linetable, so no includes. */
6912
6913 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
6914 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
6915 }
6916
6917 static hashval_t
6918 hash_signatured_type (const void *item)
6919 {
6920 const struct signatured_type *sig_type
6921 = (const struct signatured_type *) item;
6922
6923 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6924 return sig_type->signature;
6925 }
6926
6927 static int
6928 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6929 {
6930 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6931 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6932
6933 return lhs->signature == rhs->signature;
6934 }
6935
6936 /* Allocate a hash table for signatured types. */
6937
6938 static htab_t
6939 allocate_signatured_type_table (struct objfile *objfile)
6940 {
6941 return htab_create_alloc_ex (41,
6942 hash_signatured_type,
6943 eq_signatured_type,
6944 NULL,
6945 &objfile->objfile_obstack,
6946 hashtab_obstack_allocate,
6947 dummy_obstack_deallocate);
6948 }
6949
6950 /* A helper function to add a signatured type CU to a table. */
6951
6952 static int
6953 add_signatured_type_cu_to_table (void **slot, void *datum)
6954 {
6955 struct signatured_type *sigt = (struct signatured_type *) *slot;
6956 struct signatured_type ***datap = (struct signatured_type ***) datum;
6957
6958 **datap = sigt;
6959 ++*datap;
6960
6961 return 1;
6962 }
6963
6964 /* A helper for create_debug_types_hash_table. Read types from SECTION
6965 and fill them into TYPES_HTAB. It will process only type units,
6966 therefore DW_UT_type. */
6967
6968 static void
6969 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6970 struct dwo_file *dwo_file,
6971 dwarf2_section_info *section, htab_t &types_htab,
6972 rcuh_kind section_kind)
6973 {
6974 struct objfile *objfile = dwarf2_per_objfile->objfile;
6975 struct dwarf2_section_info *abbrev_section;
6976 bfd *abfd;
6977 const gdb_byte *info_ptr, *end_ptr;
6978
6979 abbrev_section = (dwo_file != NULL
6980 ? &dwo_file->sections.abbrev
6981 : &dwarf2_per_objfile->abbrev);
6982
6983 if (dwarf_read_debug)
6984 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6985 get_section_name (section),
6986 get_section_file_name (abbrev_section));
6987
6988 dwarf2_read_section (objfile, section);
6989 info_ptr = section->buffer;
6990
6991 if (info_ptr == NULL)
6992 return;
6993
6994 /* We can't set abfd until now because the section may be empty or
6995 not present, in which case the bfd is unknown. */
6996 abfd = get_section_bfd_owner (section);
6997
6998 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6999 because we don't need to read any dies: the signature is in the
7000 header. */
7001
7002 end_ptr = info_ptr + section->size;
7003 while (info_ptr < end_ptr)
7004 {
7005 struct signatured_type *sig_type;
7006 struct dwo_unit *dwo_tu;
7007 void **slot;
7008 const gdb_byte *ptr = info_ptr;
7009 struct comp_unit_head header;
7010 unsigned int length;
7011
7012 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
7013
7014 /* Initialize it due to a false compiler warning. */
7015 header.signature = -1;
7016 header.type_cu_offset_in_tu = (cu_offset) -1;
7017
7018 /* We need to read the type's signature in order to build the hash
7019 table, but we don't need anything else just yet. */
7020
7021 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
7022 abbrev_section, ptr, section_kind);
7023
7024 length = get_cu_length (&header);
7025
7026 /* Skip dummy type units. */
7027 if (ptr >= info_ptr + length
7028 || peek_abbrev_code (abfd, ptr) == 0
7029 || header.unit_type != DW_UT_type)
7030 {
7031 info_ptr += length;
7032 continue;
7033 }
7034
7035 if (types_htab == NULL)
7036 {
7037 if (dwo_file)
7038 types_htab = allocate_dwo_unit_table (objfile);
7039 else
7040 types_htab = allocate_signatured_type_table (objfile);
7041 }
7042
7043 if (dwo_file)
7044 {
7045 sig_type = NULL;
7046 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7047 struct dwo_unit);
7048 dwo_tu->dwo_file = dwo_file;
7049 dwo_tu->signature = header.signature;
7050 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
7051 dwo_tu->section = section;
7052 dwo_tu->sect_off = sect_off;
7053 dwo_tu->length = length;
7054 }
7055 else
7056 {
7057 /* N.B.: type_offset is not usable if this type uses a DWO file.
7058 The real type_offset is in the DWO file. */
7059 dwo_tu = NULL;
7060 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7061 struct signatured_type);
7062 sig_type->signature = header.signature;
7063 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
7064 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7065 sig_type->per_cu.is_debug_types = 1;
7066 sig_type->per_cu.section = section;
7067 sig_type->per_cu.sect_off = sect_off;
7068 sig_type->per_cu.length = length;
7069 }
7070
7071 slot = htab_find_slot (types_htab,
7072 dwo_file ? (void*) dwo_tu : (void *) sig_type,
7073 INSERT);
7074 gdb_assert (slot != NULL);
7075 if (*slot != NULL)
7076 {
7077 sect_offset dup_sect_off;
7078
7079 if (dwo_file)
7080 {
7081 const struct dwo_unit *dup_tu
7082 = (const struct dwo_unit *) *slot;
7083
7084 dup_sect_off = dup_tu->sect_off;
7085 }
7086 else
7087 {
7088 const struct signatured_type *dup_tu
7089 = (const struct signatured_type *) *slot;
7090
7091 dup_sect_off = dup_tu->per_cu.sect_off;
7092 }
7093
7094 complaint (&symfile_complaints,
7095 _("debug type entry at offset 0x%x is duplicate to"
7096 " the entry at offset 0x%x, signature %s"),
7097 to_underlying (sect_off), to_underlying (dup_sect_off),
7098 hex_string (header.signature));
7099 }
7100 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
7101
7102 if (dwarf_read_debug > 1)
7103 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
7104 to_underlying (sect_off),
7105 hex_string (header.signature));
7106
7107 info_ptr += length;
7108 }
7109 }
7110
7111 /* Create the hash table of all entries in the .debug_types
7112 (or .debug_types.dwo) section(s).
7113 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7114 otherwise it is NULL.
7115
7116 The result is a pointer to the hash table or NULL if there are no types.
7117
7118 Note: This function processes DWO files only, not DWP files. */
7119
7120 static void
7121 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7122 struct dwo_file *dwo_file,
7123 VEC (dwarf2_section_info_def) *types,
7124 htab_t &types_htab)
7125 {
7126 int ix;
7127 struct dwarf2_section_info *section;
7128
7129 if (VEC_empty (dwarf2_section_info_def, types))
7130 return;
7131
7132 for (ix = 0;
7133 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7134 ++ix)
7135 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
7136 types_htab, rcuh_kind::TYPE);
7137 }
7138
7139 /* Create the hash table of all entries in the .debug_types section,
7140 and initialize all_type_units.
7141 The result is zero if there is an error (e.g. missing .debug_types section),
7142 otherwise non-zero. */
7143
7144 static int
7145 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7146 {
7147 htab_t types_htab = NULL;
7148 struct signatured_type **iter;
7149
7150 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
7151 &dwarf2_per_objfile->info, types_htab,
7152 rcuh_kind::COMPILE);
7153 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
7154 dwarf2_per_objfile->types, types_htab);
7155 if (types_htab == NULL)
7156 {
7157 dwarf2_per_objfile->signatured_types = NULL;
7158 return 0;
7159 }
7160
7161 dwarf2_per_objfile->signatured_types = types_htab;
7162
7163 dwarf2_per_objfile->n_type_units
7164 = dwarf2_per_objfile->n_allocated_type_units
7165 = htab_elements (types_htab);
7166 dwarf2_per_objfile->all_type_units =
7167 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
7168 iter = &dwarf2_per_objfile->all_type_units[0];
7169 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7170 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7171 == dwarf2_per_objfile->n_type_units);
7172
7173 return 1;
7174 }
7175
7176 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7177 If SLOT is non-NULL, it is the entry to use in the hash table.
7178 Otherwise we find one. */
7179
7180 static struct signatured_type *
7181 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
7182 void **slot)
7183 {
7184 struct objfile *objfile = dwarf2_per_objfile->objfile;
7185 int n_type_units = dwarf2_per_objfile->n_type_units;
7186 struct signatured_type *sig_type;
7187
7188 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7189 ++n_type_units;
7190 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7191 {
7192 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7193 dwarf2_per_objfile->n_allocated_type_units = 1;
7194 dwarf2_per_objfile->n_allocated_type_units *= 2;
7195 dwarf2_per_objfile->all_type_units
7196 = XRESIZEVEC (struct signatured_type *,
7197 dwarf2_per_objfile->all_type_units,
7198 dwarf2_per_objfile->n_allocated_type_units);
7199 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7200 }
7201 dwarf2_per_objfile->n_type_units = n_type_units;
7202
7203 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7204 struct signatured_type);
7205 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7206 sig_type->signature = sig;
7207 sig_type->per_cu.is_debug_types = 1;
7208 if (dwarf2_per_objfile->using_index)
7209 {
7210 sig_type->per_cu.v.quick =
7211 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7212 struct dwarf2_per_cu_quick_data);
7213 }
7214
7215 if (slot == NULL)
7216 {
7217 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7218 sig_type, INSERT);
7219 }
7220 gdb_assert (*slot == NULL);
7221 *slot = sig_type;
7222 /* The rest of sig_type must be filled in by the caller. */
7223 return sig_type;
7224 }
7225
7226 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7227 Fill in SIG_ENTRY with DWO_ENTRY. */
7228
7229 static void
7230 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
7231 struct signatured_type *sig_entry,
7232 struct dwo_unit *dwo_entry)
7233 {
7234 /* Make sure we're not clobbering something we don't expect to. */
7235 gdb_assert (! sig_entry->per_cu.queued);
7236 gdb_assert (sig_entry->per_cu.cu == NULL);
7237 if (dwarf2_per_objfile->using_index)
7238 {
7239 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7240 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7241 }
7242 else
7243 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7244 gdb_assert (sig_entry->signature == dwo_entry->signature);
7245 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7246 gdb_assert (sig_entry->type_unit_group == NULL);
7247 gdb_assert (sig_entry->dwo_unit == NULL);
7248
7249 sig_entry->per_cu.section = dwo_entry->section;
7250 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7251 sig_entry->per_cu.length = dwo_entry->length;
7252 sig_entry->per_cu.reading_dwo_directly = 1;
7253 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7254 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7255 sig_entry->dwo_unit = dwo_entry;
7256 }
7257
7258 /* Subroutine of lookup_signatured_type.
7259 If we haven't read the TU yet, create the signatured_type data structure
7260 for a TU to be read in directly from a DWO file, bypassing the stub.
7261 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7262 using .gdb_index, then when reading a CU we want to stay in the DWO file
7263 containing that CU. Otherwise we could end up reading several other DWO
7264 files (due to comdat folding) to process the transitive closure of all the
7265 mentioned TUs, and that can be slow. The current DWO file will have every
7266 type signature that it needs.
7267 We only do this for .gdb_index because in the psymtab case we already have
7268 to read all the DWOs to build the type unit groups. */
7269
7270 static struct signatured_type *
7271 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7272 {
7273 struct dwarf2_per_objfile *dwarf2_per_objfile
7274 = cu->per_cu->dwarf2_per_objfile;
7275 struct objfile *objfile = dwarf2_per_objfile->objfile;
7276 struct dwo_file *dwo_file;
7277 struct dwo_unit find_dwo_entry, *dwo_entry;
7278 struct signatured_type find_sig_entry, *sig_entry;
7279 void **slot;
7280
7281 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7282
7283 /* If TU skeletons have been removed then we may not have read in any
7284 TUs yet. */
7285 if (dwarf2_per_objfile->signatured_types == NULL)
7286 {
7287 dwarf2_per_objfile->signatured_types
7288 = allocate_signatured_type_table (objfile);
7289 }
7290
7291 /* We only ever need to read in one copy of a signatured type.
7292 Use the global signatured_types array to do our own comdat-folding
7293 of types. If this is the first time we're reading this TU, and
7294 the TU has an entry in .gdb_index, replace the recorded data from
7295 .gdb_index with this TU. */
7296
7297 find_sig_entry.signature = sig;
7298 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7299 &find_sig_entry, INSERT);
7300 sig_entry = (struct signatured_type *) *slot;
7301
7302 /* We can get here with the TU already read, *or* in the process of being
7303 read. Don't reassign the global entry to point to this DWO if that's
7304 the case. Also note that if the TU is already being read, it may not
7305 have come from a DWO, the program may be a mix of Fission-compiled
7306 code and non-Fission-compiled code. */
7307
7308 /* Have we already tried to read this TU?
7309 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7310 needn't exist in the global table yet). */
7311 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7312 return sig_entry;
7313
7314 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7315 dwo_unit of the TU itself. */
7316 dwo_file = cu->dwo_unit->dwo_file;
7317
7318 /* Ok, this is the first time we're reading this TU. */
7319 if (dwo_file->tus == NULL)
7320 return NULL;
7321 find_dwo_entry.signature = sig;
7322 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7323 if (dwo_entry == NULL)
7324 return NULL;
7325
7326 /* If the global table doesn't have an entry for this TU, add one. */
7327 if (sig_entry == NULL)
7328 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7329
7330 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7331 sig_entry->per_cu.tu_read = 1;
7332 return sig_entry;
7333 }
7334
7335 /* Subroutine of lookup_signatured_type.
7336 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7337 then try the DWP file. If the TU stub (skeleton) has been removed then
7338 it won't be in .gdb_index. */
7339
7340 static struct signatured_type *
7341 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7342 {
7343 struct dwarf2_per_objfile *dwarf2_per_objfile
7344 = cu->per_cu->dwarf2_per_objfile;
7345 struct objfile *objfile = dwarf2_per_objfile->objfile;
7346 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7347 struct dwo_unit *dwo_entry;
7348 struct signatured_type find_sig_entry, *sig_entry;
7349 void **slot;
7350
7351 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7352 gdb_assert (dwp_file != NULL);
7353
7354 /* If TU skeletons have been removed then we may not have read in any
7355 TUs yet. */
7356 if (dwarf2_per_objfile->signatured_types == NULL)
7357 {
7358 dwarf2_per_objfile->signatured_types
7359 = allocate_signatured_type_table (objfile);
7360 }
7361
7362 find_sig_entry.signature = sig;
7363 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7364 &find_sig_entry, INSERT);
7365 sig_entry = (struct signatured_type *) *slot;
7366
7367 /* Have we already tried to read this TU?
7368 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7369 needn't exist in the global table yet). */
7370 if (sig_entry != NULL)
7371 return sig_entry;
7372
7373 if (dwp_file->tus == NULL)
7374 return NULL;
7375 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7376 sig, 1 /* is_debug_types */);
7377 if (dwo_entry == NULL)
7378 return NULL;
7379
7380 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7381 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7382
7383 return sig_entry;
7384 }
7385
7386 /* Lookup a signature based type for DW_FORM_ref_sig8.
7387 Returns NULL if signature SIG is not present in the table.
7388 It is up to the caller to complain about this. */
7389
7390 static struct signatured_type *
7391 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7392 {
7393 struct dwarf2_per_objfile *dwarf2_per_objfile
7394 = cu->per_cu->dwarf2_per_objfile;
7395
7396 if (cu->dwo_unit
7397 && dwarf2_per_objfile->using_index)
7398 {
7399 /* We're in a DWO/DWP file, and we're using .gdb_index.
7400 These cases require special processing. */
7401 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7402 return lookup_dwo_signatured_type (cu, sig);
7403 else
7404 return lookup_dwp_signatured_type (cu, sig);
7405 }
7406 else
7407 {
7408 struct signatured_type find_entry, *entry;
7409
7410 if (dwarf2_per_objfile->signatured_types == NULL)
7411 return NULL;
7412 find_entry.signature = sig;
7413 entry = ((struct signatured_type *)
7414 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7415 return entry;
7416 }
7417 }
7418 \f
7419 /* Low level DIE reading support. */
7420
7421 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7422
7423 static void
7424 init_cu_die_reader (struct die_reader_specs *reader,
7425 struct dwarf2_cu *cu,
7426 struct dwarf2_section_info *section,
7427 struct dwo_file *dwo_file,
7428 struct abbrev_table *abbrev_table)
7429 {
7430 gdb_assert (section->readin && section->buffer != NULL);
7431 reader->abfd = get_section_bfd_owner (section);
7432 reader->cu = cu;
7433 reader->dwo_file = dwo_file;
7434 reader->die_section = section;
7435 reader->buffer = section->buffer;
7436 reader->buffer_end = section->buffer + section->size;
7437 reader->comp_dir = NULL;
7438 reader->abbrev_table = abbrev_table;
7439 }
7440
7441 /* Subroutine of init_cutu_and_read_dies to simplify it.
7442 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7443 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7444 already.
7445
7446 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7447 from it to the DIE in the DWO. If NULL we are skipping the stub.
7448 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7449 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7450 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7451 STUB_COMP_DIR may be non-NULL.
7452 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7453 are filled in with the info of the DIE from the DWO file.
7454 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7455 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7456 kept around for at least as long as *RESULT_READER.
7457
7458 The result is non-zero if a valid (non-dummy) DIE was found. */
7459
7460 static int
7461 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7462 struct dwo_unit *dwo_unit,
7463 struct die_info *stub_comp_unit_die,
7464 const char *stub_comp_dir,
7465 struct die_reader_specs *result_reader,
7466 const gdb_byte **result_info_ptr,
7467 struct die_info **result_comp_unit_die,
7468 int *result_has_children,
7469 abbrev_table_up *result_dwo_abbrev_table)
7470 {
7471 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7472 struct objfile *objfile = dwarf2_per_objfile->objfile;
7473 struct dwarf2_cu *cu = this_cu->cu;
7474 bfd *abfd;
7475 const gdb_byte *begin_info_ptr, *info_ptr;
7476 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7477 int i,num_extra_attrs;
7478 struct dwarf2_section_info *dwo_abbrev_section;
7479 struct attribute *attr;
7480 struct die_info *comp_unit_die;
7481
7482 /* At most one of these may be provided. */
7483 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7484
7485 /* These attributes aren't processed until later:
7486 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7487 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7488 referenced later. However, these attributes are found in the stub
7489 which we won't have later. In order to not impose this complication
7490 on the rest of the code, we read them here and copy them to the
7491 DWO CU/TU die. */
7492
7493 stmt_list = NULL;
7494 low_pc = NULL;
7495 high_pc = NULL;
7496 ranges = NULL;
7497 comp_dir = NULL;
7498
7499 if (stub_comp_unit_die != NULL)
7500 {
7501 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7502 DWO file. */
7503 if (! this_cu->is_debug_types)
7504 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7505 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7506 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7507 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7508 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7509
7510 /* There should be a DW_AT_addr_base attribute here (if needed).
7511 We need the value before we can process DW_FORM_GNU_addr_index. */
7512 cu->addr_base = 0;
7513 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7514 if (attr)
7515 cu->addr_base = DW_UNSND (attr);
7516
7517 /* There should be a DW_AT_ranges_base attribute here (if needed).
7518 We need the value before we can process DW_AT_ranges. */
7519 cu->ranges_base = 0;
7520 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7521 if (attr)
7522 cu->ranges_base = DW_UNSND (attr);
7523 }
7524 else if (stub_comp_dir != NULL)
7525 {
7526 /* Reconstruct the comp_dir attribute to simplify the code below. */
7527 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7528 comp_dir->name = DW_AT_comp_dir;
7529 comp_dir->form = DW_FORM_string;
7530 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7531 DW_STRING (comp_dir) = stub_comp_dir;
7532 }
7533
7534 /* Set up for reading the DWO CU/TU. */
7535 cu->dwo_unit = dwo_unit;
7536 dwarf2_section_info *section = dwo_unit->section;
7537 dwarf2_read_section (objfile, section);
7538 abfd = get_section_bfd_owner (section);
7539 begin_info_ptr = info_ptr = (section->buffer
7540 + to_underlying (dwo_unit->sect_off));
7541 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7542
7543 if (this_cu->is_debug_types)
7544 {
7545 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7546
7547 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7548 &cu->header, section,
7549 dwo_abbrev_section,
7550 info_ptr, rcuh_kind::TYPE);
7551 /* This is not an assert because it can be caused by bad debug info. */
7552 if (sig_type->signature != cu->header.signature)
7553 {
7554 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7555 " TU at offset 0x%x [in module %s]"),
7556 hex_string (sig_type->signature),
7557 hex_string (cu->header.signature),
7558 to_underlying (dwo_unit->sect_off),
7559 bfd_get_filename (abfd));
7560 }
7561 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7562 /* For DWOs coming from DWP files, we don't know the CU length
7563 nor the type's offset in the TU until now. */
7564 dwo_unit->length = get_cu_length (&cu->header);
7565 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7566
7567 /* Establish the type offset that can be used to lookup the type.
7568 For DWO files, we don't know it until now. */
7569 sig_type->type_offset_in_section
7570 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7571 }
7572 else
7573 {
7574 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7575 &cu->header, section,
7576 dwo_abbrev_section,
7577 info_ptr, rcuh_kind::COMPILE);
7578 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7579 /* For DWOs coming from DWP files, we don't know the CU length
7580 until now. */
7581 dwo_unit->length = get_cu_length (&cu->header);
7582 }
7583
7584 *result_dwo_abbrev_table
7585 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7586 cu->header.abbrev_sect_off);
7587 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7588 result_dwo_abbrev_table->get ());
7589
7590 /* Read in the die, but leave space to copy over the attributes
7591 from the stub. This has the benefit of simplifying the rest of
7592 the code - all the work to maintain the illusion of a single
7593 DW_TAG_{compile,type}_unit DIE is done here. */
7594 num_extra_attrs = ((stmt_list != NULL)
7595 + (low_pc != NULL)
7596 + (high_pc != NULL)
7597 + (ranges != NULL)
7598 + (comp_dir != NULL));
7599 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7600 result_has_children, num_extra_attrs);
7601
7602 /* Copy over the attributes from the stub to the DIE we just read in. */
7603 comp_unit_die = *result_comp_unit_die;
7604 i = comp_unit_die->num_attrs;
7605 if (stmt_list != NULL)
7606 comp_unit_die->attrs[i++] = *stmt_list;
7607 if (low_pc != NULL)
7608 comp_unit_die->attrs[i++] = *low_pc;
7609 if (high_pc != NULL)
7610 comp_unit_die->attrs[i++] = *high_pc;
7611 if (ranges != NULL)
7612 comp_unit_die->attrs[i++] = *ranges;
7613 if (comp_dir != NULL)
7614 comp_unit_die->attrs[i++] = *comp_dir;
7615 comp_unit_die->num_attrs += num_extra_attrs;
7616
7617 if (dwarf_die_debug)
7618 {
7619 fprintf_unfiltered (gdb_stdlog,
7620 "Read die from %s@0x%x of %s:\n",
7621 get_section_name (section),
7622 (unsigned) (begin_info_ptr - section->buffer),
7623 bfd_get_filename (abfd));
7624 dump_die (comp_unit_die, dwarf_die_debug);
7625 }
7626
7627 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7628 TUs by skipping the stub and going directly to the entry in the DWO file.
7629 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7630 to get it via circuitous means. Blech. */
7631 if (comp_dir != NULL)
7632 result_reader->comp_dir = DW_STRING (comp_dir);
7633
7634 /* Skip dummy compilation units. */
7635 if (info_ptr >= begin_info_ptr + dwo_unit->length
7636 || peek_abbrev_code (abfd, info_ptr) == 0)
7637 return 0;
7638
7639 *result_info_ptr = info_ptr;
7640 return 1;
7641 }
7642
7643 /* Subroutine of init_cutu_and_read_dies to simplify it.
7644 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7645 Returns NULL if the specified DWO unit cannot be found. */
7646
7647 static struct dwo_unit *
7648 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7649 struct die_info *comp_unit_die)
7650 {
7651 struct dwarf2_cu *cu = this_cu->cu;
7652 ULONGEST signature;
7653 struct dwo_unit *dwo_unit;
7654 const char *comp_dir, *dwo_name;
7655
7656 gdb_assert (cu != NULL);
7657
7658 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7659 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7660 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7661
7662 if (this_cu->is_debug_types)
7663 {
7664 struct signatured_type *sig_type;
7665
7666 /* Since this_cu is the first member of struct signatured_type,
7667 we can go from a pointer to one to a pointer to the other. */
7668 sig_type = (struct signatured_type *) this_cu;
7669 signature = sig_type->signature;
7670 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7671 }
7672 else
7673 {
7674 struct attribute *attr;
7675
7676 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7677 if (! attr)
7678 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7679 " [in module %s]"),
7680 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7681 signature = DW_UNSND (attr);
7682 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7683 signature);
7684 }
7685
7686 return dwo_unit;
7687 }
7688
7689 /* Subroutine of init_cutu_and_read_dies to simplify it.
7690 See it for a description of the parameters.
7691 Read a TU directly from a DWO file, bypassing the stub. */
7692
7693 static void
7694 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7695 int use_existing_cu, int keep,
7696 die_reader_func_ftype *die_reader_func,
7697 void *data)
7698 {
7699 std::unique_ptr<dwarf2_cu> new_cu;
7700 struct signatured_type *sig_type;
7701 struct die_reader_specs reader;
7702 const gdb_byte *info_ptr;
7703 struct die_info *comp_unit_die;
7704 int has_children;
7705 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7706
7707 /* Verify we can do the following downcast, and that we have the
7708 data we need. */
7709 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7710 sig_type = (struct signatured_type *) this_cu;
7711 gdb_assert (sig_type->dwo_unit != NULL);
7712
7713 if (use_existing_cu && this_cu->cu != NULL)
7714 {
7715 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7716 /* There's no need to do the rereading_dwo_cu handling that
7717 init_cutu_and_read_dies does since we don't read the stub. */
7718 }
7719 else
7720 {
7721 /* If !use_existing_cu, this_cu->cu must be NULL. */
7722 gdb_assert (this_cu->cu == NULL);
7723 new_cu.reset (new dwarf2_cu (this_cu));
7724 }
7725
7726 /* A future optimization, if needed, would be to use an existing
7727 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7728 could share abbrev tables. */
7729
7730 /* The abbreviation table used by READER, this must live at least as long as
7731 READER. */
7732 abbrev_table_up dwo_abbrev_table;
7733
7734 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7735 NULL /* stub_comp_unit_die */,
7736 sig_type->dwo_unit->dwo_file->comp_dir,
7737 &reader, &info_ptr,
7738 &comp_unit_die, &has_children,
7739 &dwo_abbrev_table) == 0)
7740 {
7741 /* Dummy die. */
7742 return;
7743 }
7744
7745 /* All the "real" work is done here. */
7746 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7747
7748 /* This duplicates the code in init_cutu_and_read_dies,
7749 but the alternative is making the latter more complex.
7750 This function is only for the special case of using DWO files directly:
7751 no point in overly complicating the general case just to handle this. */
7752 if (new_cu != NULL && keep)
7753 {
7754 /* Link this CU into read_in_chain. */
7755 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7756 dwarf2_per_objfile->read_in_chain = this_cu;
7757 /* The chain owns it now. */
7758 new_cu.release ();
7759 }
7760 }
7761
7762 /* Initialize a CU (or TU) and read its DIEs.
7763 If the CU defers to a DWO file, read the DWO file as well.
7764
7765 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7766 Otherwise the table specified in the comp unit header is read in and used.
7767 This is an optimization for when we already have the abbrev table.
7768
7769 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7770 Otherwise, a new CU is allocated with xmalloc.
7771
7772 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7773 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7774
7775 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7776 linker) then DIE_READER_FUNC will not get called. */
7777
7778 static void
7779 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7780 struct abbrev_table *abbrev_table,
7781 int use_existing_cu, int keep,
7782 die_reader_func_ftype *die_reader_func,
7783 void *data)
7784 {
7785 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7786 struct objfile *objfile = dwarf2_per_objfile->objfile;
7787 struct dwarf2_section_info *section = this_cu->section;
7788 bfd *abfd = get_section_bfd_owner (section);
7789 struct dwarf2_cu *cu;
7790 const gdb_byte *begin_info_ptr, *info_ptr;
7791 struct die_reader_specs reader;
7792 struct die_info *comp_unit_die;
7793 int has_children;
7794 struct attribute *attr;
7795 struct signatured_type *sig_type = NULL;
7796 struct dwarf2_section_info *abbrev_section;
7797 /* Non-zero if CU currently points to a DWO file and we need to
7798 reread it. When this happens we need to reread the skeleton die
7799 before we can reread the DWO file (this only applies to CUs, not TUs). */
7800 int rereading_dwo_cu = 0;
7801
7802 if (dwarf_die_debug)
7803 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
7804 this_cu->is_debug_types ? "type" : "comp",
7805 to_underlying (this_cu->sect_off));
7806
7807 if (use_existing_cu)
7808 gdb_assert (keep);
7809
7810 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7811 file (instead of going through the stub), short-circuit all of this. */
7812 if (this_cu->reading_dwo_directly)
7813 {
7814 /* Narrow down the scope of possibilities to have to understand. */
7815 gdb_assert (this_cu->is_debug_types);
7816 gdb_assert (abbrev_table == NULL);
7817 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7818 die_reader_func, data);
7819 return;
7820 }
7821
7822 /* This is cheap if the section is already read in. */
7823 dwarf2_read_section (objfile, section);
7824
7825 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7826
7827 abbrev_section = get_abbrev_section_for_cu (this_cu);
7828
7829 std::unique_ptr<dwarf2_cu> new_cu;
7830 if (use_existing_cu && this_cu->cu != NULL)
7831 {
7832 cu = this_cu->cu;
7833 /* If this CU is from a DWO file we need to start over, we need to
7834 refetch the attributes from the skeleton CU.
7835 This could be optimized by retrieving those attributes from when we
7836 were here the first time: the previous comp_unit_die was stored in
7837 comp_unit_obstack. But there's no data yet that we need this
7838 optimization. */
7839 if (cu->dwo_unit != NULL)
7840 rereading_dwo_cu = 1;
7841 }
7842 else
7843 {
7844 /* If !use_existing_cu, this_cu->cu must be NULL. */
7845 gdb_assert (this_cu->cu == NULL);
7846 new_cu.reset (new dwarf2_cu (this_cu));
7847 cu = new_cu.get ();
7848 }
7849
7850 /* Get the header. */
7851 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7852 {
7853 /* We already have the header, there's no need to read it in again. */
7854 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7855 }
7856 else
7857 {
7858 if (this_cu->is_debug_types)
7859 {
7860 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7861 &cu->header, section,
7862 abbrev_section, info_ptr,
7863 rcuh_kind::TYPE);
7864
7865 /* Since per_cu is the first member of struct signatured_type,
7866 we can go from a pointer to one to a pointer to the other. */
7867 sig_type = (struct signatured_type *) this_cu;
7868 gdb_assert (sig_type->signature == cu->header.signature);
7869 gdb_assert (sig_type->type_offset_in_tu
7870 == cu->header.type_cu_offset_in_tu);
7871 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7872
7873 /* LENGTH has not been set yet for type units if we're
7874 using .gdb_index. */
7875 this_cu->length = get_cu_length (&cu->header);
7876
7877 /* Establish the type offset that can be used to lookup the type. */
7878 sig_type->type_offset_in_section =
7879 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7880
7881 this_cu->dwarf_version = cu->header.version;
7882 }
7883 else
7884 {
7885 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7886 &cu->header, section,
7887 abbrev_section,
7888 info_ptr,
7889 rcuh_kind::COMPILE);
7890
7891 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7892 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7893 this_cu->dwarf_version = cu->header.version;
7894 }
7895 }
7896
7897 /* Skip dummy compilation units. */
7898 if (info_ptr >= begin_info_ptr + this_cu->length
7899 || peek_abbrev_code (abfd, info_ptr) == 0)
7900 return;
7901
7902 /* If we don't have them yet, read the abbrevs for this compilation unit.
7903 And if we need to read them now, make sure they're freed when we're
7904 done (own the table through ABBREV_TABLE_HOLDER). */
7905 abbrev_table_up abbrev_table_holder;
7906 if (abbrev_table != NULL)
7907 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7908 else
7909 {
7910 abbrev_table_holder
7911 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7912 cu->header.abbrev_sect_off);
7913 abbrev_table = abbrev_table_holder.get ();
7914 }
7915
7916 /* Read the top level CU/TU die. */
7917 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7918 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7919
7920 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7921 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7922 table from the DWO file and pass the ownership over to us. It will be
7923 referenced from READER, so we must make sure to free it after we're done
7924 with READER.
7925
7926 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7927 DWO CU, that this test will fail (the attribute will not be present). */
7928 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7929 abbrev_table_up dwo_abbrev_table;
7930 if (attr)
7931 {
7932 struct dwo_unit *dwo_unit;
7933 struct die_info *dwo_comp_unit_die;
7934
7935 if (has_children)
7936 {
7937 complaint (&symfile_complaints,
7938 _("compilation unit with DW_AT_GNU_dwo_name"
7939 " has children (offset 0x%x) [in module %s]"),
7940 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
7941 }
7942 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7943 if (dwo_unit != NULL)
7944 {
7945 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7946 comp_unit_die, NULL,
7947 &reader, &info_ptr,
7948 &dwo_comp_unit_die, &has_children,
7949 &dwo_abbrev_table) == 0)
7950 {
7951 /* Dummy die. */
7952 return;
7953 }
7954 comp_unit_die = dwo_comp_unit_die;
7955 }
7956 else
7957 {
7958 /* Yikes, we couldn't find the rest of the DIE, we only have
7959 the stub. A complaint has already been logged. There's
7960 not much more we can do except pass on the stub DIE to
7961 die_reader_func. We don't want to throw an error on bad
7962 debug info. */
7963 }
7964 }
7965
7966 /* All of the above is setup for this call. Yikes. */
7967 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7968
7969 /* Done, clean up. */
7970 if (new_cu != NULL && keep)
7971 {
7972 /* Link this CU into read_in_chain. */
7973 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7974 dwarf2_per_objfile->read_in_chain = this_cu;
7975 /* The chain owns it now. */
7976 new_cu.release ();
7977 }
7978 }
7979
7980 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7981 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7982 to have already done the lookup to find the DWO file).
7983
7984 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7985 THIS_CU->is_debug_types, but nothing else.
7986
7987 We fill in THIS_CU->length.
7988
7989 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7990 linker) then DIE_READER_FUNC will not get called.
7991
7992 THIS_CU->cu is always freed when done.
7993 This is done in order to not leave THIS_CU->cu in a state where we have
7994 to care whether it refers to the "main" CU or the DWO CU. */
7995
7996 static void
7997 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7998 struct dwo_file *dwo_file,
7999 die_reader_func_ftype *die_reader_func,
8000 void *data)
8001 {
8002 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
8003 struct objfile *objfile = dwarf2_per_objfile->objfile;
8004 struct dwarf2_section_info *section = this_cu->section;
8005 bfd *abfd = get_section_bfd_owner (section);
8006 struct dwarf2_section_info *abbrev_section;
8007 const gdb_byte *begin_info_ptr, *info_ptr;
8008 struct die_reader_specs reader;
8009 struct die_info *comp_unit_die;
8010 int has_children;
8011
8012 if (dwarf_die_debug)
8013 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
8014 this_cu->is_debug_types ? "type" : "comp",
8015 to_underlying (this_cu->sect_off));
8016
8017 gdb_assert (this_cu->cu == NULL);
8018
8019 abbrev_section = (dwo_file != NULL
8020 ? &dwo_file->sections.abbrev
8021 : get_abbrev_section_for_cu (this_cu));
8022
8023 /* This is cheap if the section is already read in. */
8024 dwarf2_read_section (objfile, section);
8025
8026 struct dwarf2_cu cu (this_cu);
8027
8028 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
8029 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
8030 &cu.header, section,
8031 abbrev_section, info_ptr,
8032 (this_cu->is_debug_types
8033 ? rcuh_kind::TYPE
8034 : rcuh_kind::COMPILE));
8035
8036 this_cu->length = get_cu_length (&cu.header);
8037
8038 /* Skip dummy compilation units. */
8039 if (info_ptr >= begin_info_ptr + this_cu->length
8040 || peek_abbrev_code (abfd, info_ptr) == 0)
8041 return;
8042
8043 abbrev_table_up abbrev_table
8044 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8045 cu.header.abbrev_sect_off);
8046
8047 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
8048 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8049
8050 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8051 }
8052
8053 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
8054 does not lookup the specified DWO file.
8055 This cannot be used to read DWO files.
8056
8057 THIS_CU->cu is always freed when done.
8058 This is done in order to not leave THIS_CU->cu in a state where we have
8059 to care whether it refers to the "main" CU or the DWO CU.
8060 We can revisit this if the data shows there's a performance issue. */
8061
8062 static void
8063 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8064 die_reader_func_ftype *die_reader_func,
8065 void *data)
8066 {
8067 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
8068 }
8069 \f
8070 /* Type Unit Groups.
8071
8072 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8073 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8074 so that all types coming from the same compilation (.o file) are grouped
8075 together. A future step could be to put the types in the same symtab as
8076 the CU the types ultimately came from. */
8077
8078 static hashval_t
8079 hash_type_unit_group (const void *item)
8080 {
8081 const struct type_unit_group *tu_group
8082 = (const struct type_unit_group *) item;
8083
8084 return hash_stmt_list_entry (&tu_group->hash);
8085 }
8086
8087 static int
8088 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
8089 {
8090 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8091 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
8092
8093 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
8094 }
8095
8096 /* Allocate a hash table for type unit groups. */
8097
8098 static htab_t
8099 allocate_type_unit_groups_table (struct objfile *objfile)
8100 {
8101 return htab_create_alloc_ex (3,
8102 hash_type_unit_group,
8103 eq_type_unit_group,
8104 NULL,
8105 &objfile->objfile_obstack,
8106 hashtab_obstack_allocate,
8107 dummy_obstack_deallocate);
8108 }
8109
8110 /* Type units that don't have DW_AT_stmt_list are grouped into their own
8111 partial symtabs. We combine several TUs per psymtab to not let the size
8112 of any one psymtab grow too big. */
8113 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8114 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
8115
8116 /* Helper routine for get_type_unit_group.
8117 Create the type_unit_group object used to hold one or more TUs. */
8118
8119 static struct type_unit_group *
8120 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
8121 {
8122 struct dwarf2_per_objfile *dwarf2_per_objfile
8123 = cu->per_cu->dwarf2_per_objfile;
8124 struct objfile *objfile = dwarf2_per_objfile->objfile;
8125 struct dwarf2_per_cu_data *per_cu;
8126 struct type_unit_group *tu_group;
8127
8128 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8129 struct type_unit_group);
8130 per_cu = &tu_group->per_cu;
8131 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8132
8133 if (dwarf2_per_objfile->using_index)
8134 {
8135 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8136 struct dwarf2_per_cu_quick_data);
8137 }
8138 else
8139 {
8140 unsigned int line_offset = to_underlying (line_offset_struct);
8141 struct partial_symtab *pst;
8142 char *name;
8143
8144 /* Give the symtab a useful name for debug purposes. */
8145 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8146 name = xstrprintf ("<type_units_%d>",
8147 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8148 else
8149 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8150
8151 pst = create_partial_symtab (per_cu, name);
8152 pst->anonymous = 1;
8153
8154 xfree (name);
8155 }
8156
8157 tu_group->hash.dwo_unit = cu->dwo_unit;
8158 tu_group->hash.line_sect_off = line_offset_struct;
8159
8160 return tu_group;
8161 }
8162
8163 /* Look up the type_unit_group for type unit CU, and create it if necessary.
8164 STMT_LIST is a DW_AT_stmt_list attribute. */
8165
8166 static struct type_unit_group *
8167 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
8168 {
8169 struct dwarf2_per_objfile *dwarf2_per_objfile
8170 = cu->per_cu->dwarf2_per_objfile;
8171 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8172 struct type_unit_group *tu_group;
8173 void **slot;
8174 unsigned int line_offset;
8175 struct type_unit_group type_unit_group_for_lookup;
8176
8177 if (dwarf2_per_objfile->type_unit_groups == NULL)
8178 {
8179 dwarf2_per_objfile->type_unit_groups =
8180 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
8181 }
8182
8183 /* Do we need to create a new group, or can we use an existing one? */
8184
8185 if (stmt_list)
8186 {
8187 line_offset = DW_UNSND (stmt_list);
8188 ++tu_stats->nr_symtab_sharers;
8189 }
8190 else
8191 {
8192 /* Ugh, no stmt_list. Rare, but we have to handle it.
8193 We can do various things here like create one group per TU or
8194 spread them over multiple groups to split up the expansion work.
8195 To avoid worst case scenarios (too many groups or too large groups)
8196 we, umm, group them in bunches. */
8197 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8198 | (tu_stats->nr_stmt_less_type_units
8199 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8200 ++tu_stats->nr_stmt_less_type_units;
8201 }
8202
8203 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
8204 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
8205 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8206 &type_unit_group_for_lookup, INSERT);
8207 if (*slot != NULL)
8208 {
8209 tu_group = (struct type_unit_group *) *slot;
8210 gdb_assert (tu_group != NULL);
8211 }
8212 else
8213 {
8214 sect_offset line_offset_struct = (sect_offset) line_offset;
8215 tu_group = create_type_unit_group (cu, line_offset_struct);
8216 *slot = tu_group;
8217 ++tu_stats->nr_symtabs;
8218 }
8219
8220 return tu_group;
8221 }
8222 \f
8223 /* Partial symbol tables. */
8224
8225 /* Create a psymtab named NAME and assign it to PER_CU.
8226
8227 The caller must fill in the following details:
8228 dirname, textlow, texthigh. */
8229
8230 static struct partial_symtab *
8231 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8232 {
8233 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8234 struct partial_symtab *pst;
8235
8236 pst = start_psymtab_common (objfile, name, 0,
8237 objfile->global_psymbols,
8238 objfile->static_psymbols);
8239
8240 pst->psymtabs_addrmap_supported = 1;
8241
8242 /* This is the glue that links PST into GDB's symbol API. */
8243 pst->read_symtab_private = per_cu;
8244 pst->read_symtab = dwarf2_read_symtab;
8245 per_cu->v.psymtab = pst;
8246
8247 return pst;
8248 }
8249
8250 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8251 type. */
8252
8253 struct process_psymtab_comp_unit_data
8254 {
8255 /* True if we are reading a DW_TAG_partial_unit. */
8256
8257 int want_partial_unit;
8258
8259 /* The "pretend" language that is used if the CU doesn't declare a
8260 language. */
8261
8262 enum language pretend_language;
8263 };
8264
8265 /* die_reader_func for process_psymtab_comp_unit. */
8266
8267 static void
8268 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8269 const gdb_byte *info_ptr,
8270 struct die_info *comp_unit_die,
8271 int has_children,
8272 void *data)
8273 {
8274 struct dwarf2_cu *cu = reader->cu;
8275 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8276 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8277 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8278 CORE_ADDR baseaddr;
8279 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8280 struct partial_symtab *pst;
8281 enum pc_bounds_kind cu_bounds_kind;
8282 const char *filename;
8283 struct process_psymtab_comp_unit_data *info
8284 = (struct process_psymtab_comp_unit_data *) data;
8285
8286 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8287 return;
8288
8289 gdb_assert (! per_cu->is_debug_types);
8290
8291 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8292
8293 cu->list_in_scope = &file_symbols;
8294
8295 /* Allocate a new partial symbol table structure. */
8296 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8297 if (filename == NULL)
8298 filename = "";
8299
8300 pst = create_partial_symtab (per_cu, filename);
8301
8302 /* This must be done before calling dwarf2_build_include_psymtabs. */
8303 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8304
8305 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8306
8307 dwarf2_find_base_address (comp_unit_die, cu);
8308
8309 /* Possibly set the default values of LOWPC and HIGHPC from
8310 `DW_AT_ranges'. */
8311 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8312 &best_highpc, cu, pst);
8313 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8314 /* Store the contiguous range if it is not empty; it can be empty for
8315 CUs with no code. */
8316 addrmap_set_empty (objfile->psymtabs_addrmap,
8317 gdbarch_adjust_dwarf2_addr (gdbarch,
8318 best_lowpc + baseaddr),
8319 gdbarch_adjust_dwarf2_addr (gdbarch,
8320 best_highpc + baseaddr) - 1,
8321 pst);
8322
8323 /* Check if comp unit has_children.
8324 If so, read the rest of the partial symbols from this comp unit.
8325 If not, there's no more debug_info for this comp unit. */
8326 if (has_children)
8327 {
8328 struct partial_die_info *first_die;
8329 CORE_ADDR lowpc, highpc;
8330
8331 lowpc = ((CORE_ADDR) -1);
8332 highpc = ((CORE_ADDR) 0);
8333
8334 first_die = load_partial_dies (reader, info_ptr, 1);
8335
8336 scan_partial_symbols (first_die, &lowpc, &highpc,
8337 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8338
8339 /* If we didn't find a lowpc, set it to highpc to avoid
8340 complaints from `maint check'. */
8341 if (lowpc == ((CORE_ADDR) -1))
8342 lowpc = highpc;
8343
8344 /* If the compilation unit didn't have an explicit address range,
8345 then use the information extracted from its child dies. */
8346 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8347 {
8348 best_lowpc = lowpc;
8349 best_highpc = highpc;
8350 }
8351 }
8352 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8353 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
8354
8355 end_psymtab_common (objfile, pst);
8356
8357 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8358 {
8359 int i;
8360 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8361 struct dwarf2_per_cu_data *iter;
8362
8363 /* Fill in 'dependencies' here; we fill in 'users' in a
8364 post-pass. */
8365 pst->number_of_dependencies = len;
8366 pst->dependencies =
8367 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8368 for (i = 0;
8369 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8370 i, iter);
8371 ++i)
8372 pst->dependencies[i] = iter->v.psymtab;
8373
8374 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8375 }
8376
8377 /* Get the list of files included in the current compilation unit,
8378 and build a psymtab for each of them. */
8379 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8380
8381 if (dwarf_read_debug)
8382 {
8383 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8384
8385 fprintf_unfiltered (gdb_stdlog,
8386 "Psymtab for %s unit @0x%x: %s - %s"
8387 ", %d global, %d static syms\n",
8388 per_cu->is_debug_types ? "type" : "comp",
8389 to_underlying (per_cu->sect_off),
8390 paddress (gdbarch, pst->textlow),
8391 paddress (gdbarch, pst->texthigh),
8392 pst->n_global_syms, pst->n_static_syms);
8393 }
8394 }
8395
8396 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8397 Process compilation unit THIS_CU for a psymtab. */
8398
8399 static void
8400 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8401 int want_partial_unit,
8402 enum language pretend_language)
8403 {
8404 /* If this compilation unit was already read in, free the
8405 cached copy in order to read it in again. This is
8406 necessary because we skipped some symbols when we first
8407 read in the compilation unit (see load_partial_dies).
8408 This problem could be avoided, but the benefit is unclear. */
8409 if (this_cu->cu != NULL)
8410 free_one_cached_comp_unit (this_cu);
8411
8412 if (this_cu->is_debug_types)
8413 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8414 NULL);
8415 else
8416 {
8417 process_psymtab_comp_unit_data info;
8418 info.want_partial_unit = want_partial_unit;
8419 info.pretend_language = pretend_language;
8420 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8421 process_psymtab_comp_unit_reader, &info);
8422 }
8423
8424 /* Age out any secondary CUs. */
8425 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8426 }
8427
8428 /* Reader function for build_type_psymtabs. */
8429
8430 static void
8431 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8432 const gdb_byte *info_ptr,
8433 struct die_info *type_unit_die,
8434 int has_children,
8435 void *data)
8436 {
8437 struct dwarf2_per_objfile *dwarf2_per_objfile
8438 = reader->cu->per_cu->dwarf2_per_objfile;
8439 struct objfile *objfile = dwarf2_per_objfile->objfile;
8440 struct dwarf2_cu *cu = reader->cu;
8441 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8442 struct signatured_type *sig_type;
8443 struct type_unit_group *tu_group;
8444 struct attribute *attr;
8445 struct partial_die_info *first_die;
8446 CORE_ADDR lowpc, highpc;
8447 struct partial_symtab *pst;
8448
8449 gdb_assert (data == NULL);
8450 gdb_assert (per_cu->is_debug_types);
8451 sig_type = (struct signatured_type *) per_cu;
8452
8453 if (! has_children)
8454 return;
8455
8456 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8457 tu_group = get_type_unit_group (cu, attr);
8458
8459 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8460
8461 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8462 cu->list_in_scope = &file_symbols;
8463 pst = create_partial_symtab (per_cu, "");
8464 pst->anonymous = 1;
8465
8466 first_die = load_partial_dies (reader, info_ptr, 1);
8467
8468 lowpc = (CORE_ADDR) -1;
8469 highpc = (CORE_ADDR) 0;
8470 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8471
8472 end_psymtab_common (objfile, pst);
8473 }
8474
8475 /* Struct used to sort TUs by their abbreviation table offset. */
8476
8477 struct tu_abbrev_offset
8478 {
8479 struct signatured_type *sig_type;
8480 sect_offset abbrev_offset;
8481 };
8482
8483 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
8484
8485 static int
8486 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
8487 {
8488 const struct tu_abbrev_offset * const *a
8489 = (const struct tu_abbrev_offset * const*) ap;
8490 const struct tu_abbrev_offset * const *b
8491 = (const struct tu_abbrev_offset * const*) bp;
8492 sect_offset aoff = (*a)->abbrev_offset;
8493 sect_offset boff = (*b)->abbrev_offset;
8494
8495 return (aoff > boff) - (aoff < boff);
8496 }
8497
8498 /* Efficiently read all the type units.
8499 This does the bulk of the work for build_type_psymtabs.
8500
8501 The efficiency is because we sort TUs by the abbrev table they use and
8502 only read each abbrev table once. In one program there are 200K TUs
8503 sharing 8K abbrev tables.
8504
8505 The main purpose of this function is to support building the
8506 dwarf2_per_objfile->type_unit_groups table.
8507 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8508 can collapse the search space by grouping them by stmt_list.
8509 The savings can be significant, in the same program from above the 200K TUs
8510 share 8K stmt_list tables.
8511
8512 FUNC is expected to call get_type_unit_group, which will create the
8513 struct type_unit_group if necessary and add it to
8514 dwarf2_per_objfile->type_unit_groups. */
8515
8516 static void
8517 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8518 {
8519 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8520 struct cleanup *cleanups;
8521 abbrev_table_up abbrev_table;
8522 sect_offset abbrev_offset;
8523 struct tu_abbrev_offset *sorted_by_abbrev;
8524 int i;
8525
8526 /* It's up to the caller to not call us multiple times. */
8527 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8528
8529 if (dwarf2_per_objfile->n_type_units == 0)
8530 return;
8531
8532 /* TUs typically share abbrev tables, and there can be way more TUs than
8533 abbrev tables. Sort by abbrev table to reduce the number of times we
8534 read each abbrev table in.
8535 Alternatives are to punt or to maintain a cache of abbrev tables.
8536 This is simpler and efficient enough for now.
8537
8538 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8539 symtab to use). Typically TUs with the same abbrev offset have the same
8540 stmt_list value too so in practice this should work well.
8541
8542 The basic algorithm here is:
8543
8544 sort TUs by abbrev table
8545 for each TU with same abbrev table:
8546 read abbrev table if first user
8547 read TU top level DIE
8548 [IWBN if DWO skeletons had DW_AT_stmt_list]
8549 call FUNC */
8550
8551 if (dwarf_read_debug)
8552 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8553
8554 /* Sort in a separate table to maintain the order of all_type_units
8555 for .gdb_index: TU indices directly index all_type_units. */
8556 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
8557 dwarf2_per_objfile->n_type_units);
8558 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8559 {
8560 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8561
8562 sorted_by_abbrev[i].sig_type = sig_type;
8563 sorted_by_abbrev[i].abbrev_offset =
8564 read_abbrev_offset (dwarf2_per_objfile,
8565 sig_type->per_cu.section,
8566 sig_type->per_cu.sect_off);
8567 }
8568 cleanups = make_cleanup (xfree, sorted_by_abbrev);
8569 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
8570 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
8571
8572 abbrev_offset = (sect_offset) ~(unsigned) 0;
8573
8574 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8575 {
8576 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8577
8578 /* Switch to the next abbrev table if necessary. */
8579 if (abbrev_table == NULL
8580 || tu->abbrev_offset != abbrev_offset)
8581 {
8582 abbrev_offset = tu->abbrev_offset;
8583 abbrev_table =
8584 abbrev_table_read_table (dwarf2_per_objfile,
8585 &dwarf2_per_objfile->abbrev,
8586 abbrev_offset);
8587 ++tu_stats->nr_uniq_abbrev_tables;
8588 }
8589
8590 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table.get (),
8591 0, 0, build_type_psymtabs_reader, NULL);
8592 }
8593
8594 do_cleanups (cleanups);
8595 }
8596
8597 /* Print collected type unit statistics. */
8598
8599 static void
8600 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8601 {
8602 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8603
8604 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8605 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8606 dwarf2_per_objfile->n_type_units);
8607 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8608 tu_stats->nr_uniq_abbrev_tables);
8609 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8610 tu_stats->nr_symtabs);
8611 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8612 tu_stats->nr_symtab_sharers);
8613 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8614 tu_stats->nr_stmt_less_type_units);
8615 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8616 tu_stats->nr_all_type_units_reallocs);
8617 }
8618
8619 /* Traversal function for build_type_psymtabs. */
8620
8621 static int
8622 build_type_psymtab_dependencies (void **slot, void *info)
8623 {
8624 struct dwarf2_per_objfile *dwarf2_per_objfile
8625 = (struct dwarf2_per_objfile *) info;
8626 struct objfile *objfile = dwarf2_per_objfile->objfile;
8627 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8628 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8629 struct partial_symtab *pst = per_cu->v.psymtab;
8630 int len = VEC_length (sig_type_ptr, tu_group->tus);
8631 struct signatured_type *iter;
8632 int i;
8633
8634 gdb_assert (len > 0);
8635 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8636
8637 pst->number_of_dependencies = len;
8638 pst->dependencies =
8639 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8640 for (i = 0;
8641 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8642 ++i)
8643 {
8644 gdb_assert (iter->per_cu.is_debug_types);
8645 pst->dependencies[i] = iter->per_cu.v.psymtab;
8646 iter->type_unit_group = tu_group;
8647 }
8648
8649 VEC_free (sig_type_ptr, tu_group->tus);
8650
8651 return 1;
8652 }
8653
8654 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8655 Build partial symbol tables for the .debug_types comp-units. */
8656
8657 static void
8658 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8659 {
8660 if (! create_all_type_units (dwarf2_per_objfile))
8661 return;
8662
8663 build_type_psymtabs_1 (dwarf2_per_objfile);
8664 }
8665
8666 /* Traversal function for process_skeletonless_type_unit.
8667 Read a TU in a DWO file and build partial symbols for it. */
8668
8669 static int
8670 process_skeletonless_type_unit (void **slot, void *info)
8671 {
8672 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8673 struct dwarf2_per_objfile *dwarf2_per_objfile
8674 = (struct dwarf2_per_objfile *) info;
8675 struct signatured_type find_entry, *entry;
8676
8677 /* If this TU doesn't exist in the global table, add it and read it in. */
8678
8679 if (dwarf2_per_objfile->signatured_types == NULL)
8680 {
8681 dwarf2_per_objfile->signatured_types
8682 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8683 }
8684
8685 find_entry.signature = dwo_unit->signature;
8686 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8687 INSERT);
8688 /* If we've already seen this type there's nothing to do. What's happening
8689 is we're doing our own version of comdat-folding here. */
8690 if (*slot != NULL)
8691 return 1;
8692
8693 /* This does the job that create_all_type_units would have done for
8694 this TU. */
8695 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8696 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8697 *slot = entry;
8698
8699 /* This does the job that build_type_psymtabs_1 would have done. */
8700 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8701 build_type_psymtabs_reader, NULL);
8702
8703 return 1;
8704 }
8705
8706 /* Traversal function for process_skeletonless_type_units. */
8707
8708 static int
8709 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8710 {
8711 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8712
8713 if (dwo_file->tus != NULL)
8714 {
8715 htab_traverse_noresize (dwo_file->tus,
8716 process_skeletonless_type_unit, info);
8717 }
8718
8719 return 1;
8720 }
8721
8722 /* Scan all TUs of DWO files, verifying we've processed them.
8723 This is needed in case a TU was emitted without its skeleton.
8724 Note: This can't be done until we know what all the DWO files are. */
8725
8726 static void
8727 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8728 {
8729 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8730 if (get_dwp_file (dwarf2_per_objfile) == NULL
8731 && dwarf2_per_objfile->dwo_files != NULL)
8732 {
8733 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8734 process_dwo_file_for_skeletonless_type_units,
8735 dwarf2_per_objfile);
8736 }
8737 }
8738
8739 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8740
8741 static void
8742 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8743 {
8744 int i;
8745
8746 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8747 {
8748 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8749 struct partial_symtab *pst = per_cu->v.psymtab;
8750 int j;
8751
8752 if (pst == NULL)
8753 continue;
8754
8755 for (j = 0; j < pst->number_of_dependencies; ++j)
8756 {
8757 /* Set the 'user' field only if it is not already set. */
8758 if (pst->dependencies[j]->user == NULL)
8759 pst->dependencies[j]->user = pst;
8760 }
8761 }
8762 }
8763
8764 /* Build the partial symbol table by doing a quick pass through the
8765 .debug_info and .debug_abbrev sections. */
8766
8767 static void
8768 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8769 {
8770 struct cleanup *back_to;
8771 int i;
8772 struct objfile *objfile = dwarf2_per_objfile->objfile;
8773
8774 if (dwarf_read_debug)
8775 {
8776 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8777 objfile_name (objfile));
8778 }
8779
8780 dwarf2_per_objfile->reading_partial_symbols = 1;
8781
8782 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8783
8784 /* Any cached compilation units will be linked by the per-objfile
8785 read_in_chain. Make sure to free them when we're done. */
8786 back_to = make_cleanup (free_cached_comp_units, dwarf2_per_objfile);
8787
8788 build_type_psymtabs (dwarf2_per_objfile);
8789
8790 create_all_comp_units (dwarf2_per_objfile);
8791
8792 /* Create a temporary address map on a temporary obstack. We later
8793 copy this to the final obstack. */
8794 auto_obstack temp_obstack;
8795
8796 scoped_restore save_psymtabs_addrmap
8797 = make_scoped_restore (&objfile->psymtabs_addrmap,
8798 addrmap_create_mutable (&temp_obstack));
8799
8800 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8801 {
8802 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8803
8804 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8805 }
8806
8807 /* This has to wait until we read the CUs, we need the list of DWOs. */
8808 process_skeletonless_type_units (dwarf2_per_objfile);
8809
8810 /* Now that all TUs have been processed we can fill in the dependencies. */
8811 if (dwarf2_per_objfile->type_unit_groups != NULL)
8812 {
8813 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8814 build_type_psymtab_dependencies, dwarf2_per_objfile);
8815 }
8816
8817 if (dwarf_read_debug)
8818 print_tu_stats (dwarf2_per_objfile);
8819
8820 set_partial_user (dwarf2_per_objfile);
8821
8822 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8823 &objfile->objfile_obstack);
8824 /* At this point we want to keep the address map. */
8825 save_psymtabs_addrmap.release ();
8826
8827 do_cleanups (back_to);
8828
8829 if (dwarf_read_debug)
8830 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8831 objfile_name (objfile));
8832 }
8833
8834 /* die_reader_func for load_partial_comp_unit. */
8835
8836 static void
8837 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8838 const gdb_byte *info_ptr,
8839 struct die_info *comp_unit_die,
8840 int has_children,
8841 void *data)
8842 {
8843 struct dwarf2_cu *cu = reader->cu;
8844
8845 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8846
8847 /* Check if comp unit has_children.
8848 If so, read the rest of the partial symbols from this comp unit.
8849 If not, there's no more debug_info for this comp unit. */
8850 if (has_children)
8851 load_partial_dies (reader, info_ptr, 0);
8852 }
8853
8854 /* Load the partial DIEs for a secondary CU into memory.
8855 This is also used when rereading a primary CU with load_all_dies. */
8856
8857 static void
8858 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8859 {
8860 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8861 load_partial_comp_unit_reader, NULL);
8862 }
8863
8864 static void
8865 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8866 struct dwarf2_section_info *section,
8867 struct dwarf2_section_info *abbrev_section,
8868 unsigned int is_dwz,
8869 int *n_allocated,
8870 int *n_comp_units,
8871 struct dwarf2_per_cu_data ***all_comp_units)
8872 {
8873 const gdb_byte *info_ptr;
8874 struct objfile *objfile = dwarf2_per_objfile->objfile;
8875
8876 if (dwarf_read_debug)
8877 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8878 get_section_name (section),
8879 get_section_file_name (section));
8880
8881 dwarf2_read_section (objfile, section);
8882
8883 info_ptr = section->buffer;
8884
8885 while (info_ptr < section->buffer + section->size)
8886 {
8887 struct dwarf2_per_cu_data *this_cu;
8888
8889 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8890
8891 comp_unit_head cu_header;
8892 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8893 abbrev_section, info_ptr,
8894 rcuh_kind::COMPILE);
8895
8896 /* Save the compilation unit for later lookup. */
8897 if (cu_header.unit_type != DW_UT_type)
8898 {
8899 this_cu = XOBNEW (&objfile->objfile_obstack,
8900 struct dwarf2_per_cu_data);
8901 memset (this_cu, 0, sizeof (*this_cu));
8902 }
8903 else
8904 {
8905 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8906 struct signatured_type);
8907 memset (sig_type, 0, sizeof (*sig_type));
8908 sig_type->signature = cu_header.signature;
8909 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8910 this_cu = &sig_type->per_cu;
8911 }
8912 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8913 this_cu->sect_off = sect_off;
8914 this_cu->length = cu_header.length + cu_header.initial_length_size;
8915 this_cu->is_dwz = is_dwz;
8916 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8917 this_cu->section = section;
8918
8919 if (*n_comp_units == *n_allocated)
8920 {
8921 *n_allocated *= 2;
8922 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
8923 *all_comp_units, *n_allocated);
8924 }
8925 (*all_comp_units)[*n_comp_units] = this_cu;
8926 ++*n_comp_units;
8927
8928 info_ptr = info_ptr + this_cu->length;
8929 }
8930 }
8931
8932 /* Create a list of all compilation units in OBJFILE.
8933 This is only done for -readnow and building partial symtabs. */
8934
8935 static void
8936 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8937 {
8938 int n_allocated;
8939 int n_comp_units;
8940 struct dwarf2_per_cu_data **all_comp_units;
8941 struct dwz_file *dwz;
8942 struct objfile *objfile = dwarf2_per_objfile->objfile;
8943
8944 n_comp_units = 0;
8945 n_allocated = 10;
8946 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
8947
8948 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8949 &dwarf2_per_objfile->abbrev, 0,
8950 &n_allocated, &n_comp_units, &all_comp_units);
8951
8952 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8953 if (dwz != NULL)
8954 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8955 1, &n_allocated, &n_comp_units,
8956 &all_comp_units);
8957
8958 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
8959 struct dwarf2_per_cu_data *,
8960 n_comp_units);
8961 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
8962 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
8963 xfree (all_comp_units);
8964 dwarf2_per_objfile->n_comp_units = n_comp_units;
8965 }
8966
8967 /* Process all loaded DIEs for compilation unit CU, starting at
8968 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8969 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8970 DW_AT_ranges). See the comments of add_partial_subprogram on how
8971 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8972
8973 static void
8974 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8975 CORE_ADDR *highpc, int set_addrmap,
8976 struct dwarf2_cu *cu)
8977 {
8978 struct partial_die_info *pdi;
8979
8980 /* Now, march along the PDI's, descending into ones which have
8981 interesting children but skipping the children of the other ones,
8982 until we reach the end of the compilation unit. */
8983
8984 pdi = first_die;
8985
8986 while (pdi != NULL)
8987 {
8988 fixup_partial_die (pdi, cu);
8989
8990 /* Anonymous namespaces or modules have no name but have interesting
8991 children, so we need to look at them. Ditto for anonymous
8992 enums. */
8993
8994 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8995 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8996 || pdi->tag == DW_TAG_imported_unit
8997 || pdi->tag == DW_TAG_inlined_subroutine)
8998 {
8999 switch (pdi->tag)
9000 {
9001 case DW_TAG_subprogram:
9002 case DW_TAG_inlined_subroutine:
9003 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9004 break;
9005 case DW_TAG_constant:
9006 case DW_TAG_variable:
9007 case DW_TAG_typedef:
9008 case DW_TAG_union_type:
9009 if (!pdi->is_declaration)
9010 {
9011 add_partial_symbol (pdi, cu);
9012 }
9013 break;
9014 case DW_TAG_class_type:
9015 case DW_TAG_interface_type:
9016 case DW_TAG_structure_type:
9017 if (!pdi->is_declaration)
9018 {
9019 add_partial_symbol (pdi, cu);
9020 }
9021 if (cu->language == language_rust && pdi->has_children)
9022 scan_partial_symbols (pdi->die_child, lowpc, highpc,
9023 set_addrmap, cu);
9024 break;
9025 case DW_TAG_enumeration_type:
9026 if (!pdi->is_declaration)
9027 add_partial_enumeration (pdi, cu);
9028 break;
9029 case DW_TAG_base_type:
9030 case DW_TAG_subrange_type:
9031 /* File scope base type definitions are added to the partial
9032 symbol table. */
9033 add_partial_symbol (pdi, cu);
9034 break;
9035 case DW_TAG_namespace:
9036 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
9037 break;
9038 case DW_TAG_module:
9039 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
9040 break;
9041 case DW_TAG_imported_unit:
9042 {
9043 struct dwarf2_per_cu_data *per_cu;
9044
9045 /* For now we don't handle imported units in type units. */
9046 if (cu->per_cu->is_debug_types)
9047 {
9048 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9049 " supported in type units [in module %s]"),
9050 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9051 }
9052
9053 per_cu = dwarf2_find_containing_comp_unit
9054 (pdi->d.sect_off, pdi->is_dwz,
9055 cu->per_cu->dwarf2_per_objfile);
9056
9057 /* Go read the partial unit, if needed. */
9058 if (per_cu->v.psymtab == NULL)
9059 process_psymtab_comp_unit (per_cu, 1, cu->language);
9060
9061 VEC_safe_push (dwarf2_per_cu_ptr,
9062 cu->per_cu->imported_symtabs, per_cu);
9063 }
9064 break;
9065 case DW_TAG_imported_declaration:
9066 add_partial_symbol (pdi, cu);
9067 break;
9068 default:
9069 break;
9070 }
9071 }
9072
9073 /* If the die has a sibling, skip to the sibling. */
9074
9075 pdi = pdi->die_sibling;
9076 }
9077 }
9078
9079 /* Functions used to compute the fully scoped name of a partial DIE.
9080
9081 Normally, this is simple. For C++, the parent DIE's fully scoped
9082 name is concatenated with "::" and the partial DIE's name.
9083 Enumerators are an exception; they use the scope of their parent
9084 enumeration type, i.e. the name of the enumeration type is not
9085 prepended to the enumerator.
9086
9087 There are two complexities. One is DW_AT_specification; in this
9088 case "parent" means the parent of the target of the specification,
9089 instead of the direct parent of the DIE. The other is compilers
9090 which do not emit DW_TAG_namespace; in this case we try to guess
9091 the fully qualified name of structure types from their members'
9092 linkage names. This must be done using the DIE's children rather
9093 than the children of any DW_AT_specification target. We only need
9094 to do this for structures at the top level, i.e. if the target of
9095 any DW_AT_specification (if any; otherwise the DIE itself) does not
9096 have a parent. */
9097
9098 /* Compute the scope prefix associated with PDI's parent, in
9099 compilation unit CU. The result will be allocated on CU's
9100 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9101 field. NULL is returned if no prefix is necessary. */
9102 static const char *
9103 partial_die_parent_scope (struct partial_die_info *pdi,
9104 struct dwarf2_cu *cu)
9105 {
9106 const char *grandparent_scope;
9107 struct partial_die_info *parent, *real_pdi;
9108
9109 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9110 then this means the parent of the specification DIE. */
9111
9112 real_pdi = pdi;
9113 while (real_pdi->has_specification)
9114 real_pdi = find_partial_die (real_pdi->spec_offset,
9115 real_pdi->spec_is_dwz, cu);
9116
9117 parent = real_pdi->die_parent;
9118 if (parent == NULL)
9119 return NULL;
9120
9121 if (parent->scope_set)
9122 return parent->scope;
9123
9124 fixup_partial_die (parent, cu);
9125
9126 grandparent_scope = partial_die_parent_scope (parent, cu);
9127
9128 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9129 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9130 Work around this problem here. */
9131 if (cu->language == language_cplus
9132 && parent->tag == DW_TAG_namespace
9133 && strcmp (parent->name, "::") == 0
9134 && grandparent_scope == NULL)
9135 {
9136 parent->scope = NULL;
9137 parent->scope_set = 1;
9138 return NULL;
9139 }
9140
9141 if (pdi->tag == DW_TAG_enumerator)
9142 /* Enumerators should not get the name of the enumeration as a prefix. */
9143 parent->scope = grandparent_scope;
9144 else if (parent->tag == DW_TAG_namespace
9145 || parent->tag == DW_TAG_module
9146 || parent->tag == DW_TAG_structure_type
9147 || parent->tag == DW_TAG_class_type
9148 || parent->tag == DW_TAG_interface_type
9149 || parent->tag == DW_TAG_union_type
9150 || parent->tag == DW_TAG_enumeration_type)
9151 {
9152 if (grandparent_scope == NULL)
9153 parent->scope = parent->name;
9154 else
9155 parent->scope = typename_concat (&cu->comp_unit_obstack,
9156 grandparent_scope,
9157 parent->name, 0, cu);
9158 }
9159 else
9160 {
9161 /* FIXME drow/2004-04-01: What should we be doing with
9162 function-local names? For partial symbols, we should probably be
9163 ignoring them. */
9164 complaint (&symfile_complaints,
9165 _("unhandled containing DIE tag %d for DIE at %d"),
9166 parent->tag, to_underlying (pdi->sect_off));
9167 parent->scope = grandparent_scope;
9168 }
9169
9170 parent->scope_set = 1;
9171 return parent->scope;
9172 }
9173
9174 /* Return the fully scoped name associated with PDI, from compilation unit
9175 CU. The result will be allocated with malloc. */
9176
9177 static char *
9178 partial_die_full_name (struct partial_die_info *pdi,
9179 struct dwarf2_cu *cu)
9180 {
9181 const char *parent_scope;
9182
9183 /* If this is a template instantiation, we can not work out the
9184 template arguments from partial DIEs. So, unfortunately, we have
9185 to go through the full DIEs. At least any work we do building
9186 types here will be reused if full symbols are loaded later. */
9187 if (pdi->has_template_arguments)
9188 {
9189 fixup_partial_die (pdi, cu);
9190
9191 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9192 {
9193 struct die_info *die;
9194 struct attribute attr;
9195 struct dwarf2_cu *ref_cu = cu;
9196
9197 /* DW_FORM_ref_addr is using section offset. */
9198 attr.name = (enum dwarf_attribute) 0;
9199 attr.form = DW_FORM_ref_addr;
9200 attr.u.unsnd = to_underlying (pdi->sect_off);
9201 die = follow_die_ref (NULL, &attr, &ref_cu);
9202
9203 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9204 }
9205 }
9206
9207 parent_scope = partial_die_parent_scope (pdi, cu);
9208 if (parent_scope == NULL)
9209 return NULL;
9210 else
9211 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
9212 }
9213
9214 static void
9215 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
9216 {
9217 struct dwarf2_per_objfile *dwarf2_per_objfile
9218 = cu->per_cu->dwarf2_per_objfile;
9219 struct objfile *objfile = dwarf2_per_objfile->objfile;
9220 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9221 CORE_ADDR addr = 0;
9222 const char *actual_name = NULL;
9223 CORE_ADDR baseaddr;
9224 char *built_actual_name;
9225
9226 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9227
9228 built_actual_name = partial_die_full_name (pdi, cu);
9229 if (built_actual_name != NULL)
9230 actual_name = built_actual_name;
9231
9232 if (actual_name == NULL)
9233 actual_name = pdi->name;
9234
9235 switch (pdi->tag)
9236 {
9237 case DW_TAG_inlined_subroutine:
9238 case DW_TAG_subprogram:
9239 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
9240 if (pdi->is_external || cu->language == language_ada)
9241 {
9242 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9243 of the global scope. But in Ada, we want to be able to access
9244 nested procedures globally. So all Ada subprograms are stored
9245 in the global scope. */
9246 add_psymbol_to_list (actual_name, strlen (actual_name),
9247 built_actual_name != NULL,
9248 VAR_DOMAIN, LOC_BLOCK,
9249 &objfile->global_psymbols,
9250 addr, cu->language, objfile);
9251 }
9252 else
9253 {
9254 add_psymbol_to_list (actual_name, strlen (actual_name),
9255 built_actual_name != NULL,
9256 VAR_DOMAIN, LOC_BLOCK,
9257 &objfile->static_psymbols,
9258 addr, cu->language, objfile);
9259 }
9260
9261 if (pdi->main_subprogram && actual_name != NULL)
9262 set_objfile_main_name (objfile, actual_name, cu->language);
9263 break;
9264 case DW_TAG_constant:
9265 {
9266 std::vector<partial_symbol *> *list;
9267
9268 if (pdi->is_external)
9269 list = &objfile->global_psymbols;
9270 else
9271 list = &objfile->static_psymbols;
9272 add_psymbol_to_list (actual_name, strlen (actual_name),
9273 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9274 list, 0, cu->language, objfile);
9275 }
9276 break;
9277 case DW_TAG_variable:
9278 if (pdi->d.locdesc)
9279 addr = decode_locdesc (pdi->d.locdesc, cu);
9280
9281 if (pdi->d.locdesc
9282 && addr == 0
9283 && !dwarf2_per_objfile->has_section_at_zero)
9284 {
9285 /* A global or static variable may also have been stripped
9286 out by the linker if unused, in which case its address
9287 will be nullified; do not add such variables into partial
9288 symbol table then. */
9289 }
9290 else if (pdi->is_external)
9291 {
9292 /* Global Variable.
9293 Don't enter into the minimal symbol tables as there is
9294 a minimal symbol table entry from the ELF symbols already.
9295 Enter into partial symbol table if it has a location
9296 descriptor or a type.
9297 If the location descriptor is missing, new_symbol will create
9298 a LOC_UNRESOLVED symbol, the address of the variable will then
9299 be determined from the minimal symbol table whenever the variable
9300 is referenced.
9301 The address for the partial symbol table entry is not
9302 used by GDB, but it comes in handy for debugging partial symbol
9303 table building. */
9304
9305 if (pdi->d.locdesc || pdi->has_type)
9306 add_psymbol_to_list (actual_name, strlen (actual_name),
9307 built_actual_name != NULL,
9308 VAR_DOMAIN, LOC_STATIC,
9309 &objfile->global_psymbols,
9310 addr + baseaddr,
9311 cu->language, objfile);
9312 }
9313 else
9314 {
9315 int has_loc = pdi->d.locdesc != NULL;
9316
9317 /* Static Variable. Skip symbols whose value we cannot know (those
9318 without location descriptors or constant values). */
9319 if (!has_loc && !pdi->has_const_value)
9320 {
9321 xfree (built_actual_name);
9322 return;
9323 }
9324
9325 add_psymbol_to_list (actual_name, strlen (actual_name),
9326 built_actual_name != NULL,
9327 VAR_DOMAIN, LOC_STATIC,
9328 &objfile->static_psymbols,
9329 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
9330 cu->language, objfile);
9331 }
9332 break;
9333 case DW_TAG_typedef:
9334 case DW_TAG_base_type:
9335 case DW_TAG_subrange_type:
9336 add_psymbol_to_list (actual_name, strlen (actual_name),
9337 built_actual_name != NULL,
9338 VAR_DOMAIN, LOC_TYPEDEF,
9339 &objfile->static_psymbols,
9340 0, cu->language, objfile);
9341 break;
9342 case DW_TAG_imported_declaration:
9343 case DW_TAG_namespace:
9344 add_psymbol_to_list (actual_name, strlen (actual_name),
9345 built_actual_name != NULL,
9346 VAR_DOMAIN, LOC_TYPEDEF,
9347 &objfile->global_psymbols,
9348 0, cu->language, objfile);
9349 break;
9350 case DW_TAG_module:
9351 add_psymbol_to_list (actual_name, strlen (actual_name),
9352 built_actual_name != NULL,
9353 MODULE_DOMAIN, LOC_TYPEDEF,
9354 &objfile->global_psymbols,
9355 0, cu->language, objfile);
9356 break;
9357 case DW_TAG_class_type:
9358 case DW_TAG_interface_type:
9359 case DW_TAG_structure_type:
9360 case DW_TAG_union_type:
9361 case DW_TAG_enumeration_type:
9362 /* Skip external references. The DWARF standard says in the section
9363 about "Structure, Union, and Class Type Entries": "An incomplete
9364 structure, union or class type is represented by a structure,
9365 union or class entry that does not have a byte size attribute
9366 and that has a DW_AT_declaration attribute." */
9367 if (!pdi->has_byte_size && pdi->is_declaration)
9368 {
9369 xfree (built_actual_name);
9370 return;
9371 }
9372
9373 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9374 static vs. global. */
9375 add_psymbol_to_list (actual_name, strlen (actual_name),
9376 built_actual_name != NULL,
9377 STRUCT_DOMAIN, LOC_TYPEDEF,
9378 cu->language == language_cplus
9379 ? &objfile->global_psymbols
9380 : &objfile->static_psymbols,
9381 0, cu->language, objfile);
9382
9383 break;
9384 case DW_TAG_enumerator:
9385 add_psymbol_to_list (actual_name, strlen (actual_name),
9386 built_actual_name != NULL,
9387 VAR_DOMAIN, LOC_CONST,
9388 cu->language == language_cplus
9389 ? &objfile->global_psymbols
9390 : &objfile->static_psymbols,
9391 0, cu->language, objfile);
9392 break;
9393 default:
9394 break;
9395 }
9396
9397 xfree (built_actual_name);
9398 }
9399
9400 /* Read a partial die corresponding to a namespace; also, add a symbol
9401 corresponding to that namespace to the symbol table. NAMESPACE is
9402 the name of the enclosing namespace. */
9403
9404 static void
9405 add_partial_namespace (struct partial_die_info *pdi,
9406 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9407 int set_addrmap, struct dwarf2_cu *cu)
9408 {
9409 /* Add a symbol for the namespace. */
9410
9411 add_partial_symbol (pdi, cu);
9412
9413 /* Now scan partial symbols in that namespace. */
9414
9415 if (pdi->has_children)
9416 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9417 }
9418
9419 /* Read a partial die corresponding to a Fortran module. */
9420
9421 static void
9422 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9423 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9424 {
9425 /* Add a symbol for the namespace. */
9426
9427 add_partial_symbol (pdi, cu);
9428
9429 /* Now scan partial symbols in that module. */
9430
9431 if (pdi->has_children)
9432 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9433 }
9434
9435 /* Read a partial die corresponding to a subprogram or an inlined
9436 subprogram and create a partial symbol for that subprogram.
9437 When the CU language allows it, this routine also defines a partial
9438 symbol for each nested subprogram that this subprogram contains.
9439 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9440 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9441
9442 PDI may also be a lexical block, in which case we simply search
9443 recursively for subprograms defined inside that lexical block.
9444 Again, this is only performed when the CU language allows this
9445 type of definitions. */
9446
9447 static void
9448 add_partial_subprogram (struct partial_die_info *pdi,
9449 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9450 int set_addrmap, struct dwarf2_cu *cu)
9451 {
9452 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9453 {
9454 if (pdi->has_pc_info)
9455 {
9456 if (pdi->lowpc < *lowpc)
9457 *lowpc = pdi->lowpc;
9458 if (pdi->highpc > *highpc)
9459 *highpc = pdi->highpc;
9460 if (set_addrmap)
9461 {
9462 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9463 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9464 CORE_ADDR baseaddr;
9465 CORE_ADDR highpc;
9466 CORE_ADDR lowpc;
9467
9468 baseaddr = ANOFFSET (objfile->section_offsets,
9469 SECT_OFF_TEXT (objfile));
9470 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9471 pdi->lowpc + baseaddr);
9472 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9473 pdi->highpc + baseaddr);
9474 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9475 cu->per_cu->v.psymtab);
9476 }
9477 }
9478
9479 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9480 {
9481 if (!pdi->is_declaration)
9482 /* Ignore subprogram DIEs that do not have a name, they are
9483 illegal. Do not emit a complaint at this point, we will
9484 do so when we convert this psymtab into a symtab. */
9485 if (pdi->name)
9486 add_partial_symbol (pdi, cu);
9487 }
9488 }
9489
9490 if (! pdi->has_children)
9491 return;
9492
9493 if (cu->language == language_ada)
9494 {
9495 pdi = pdi->die_child;
9496 while (pdi != NULL)
9497 {
9498 fixup_partial_die (pdi, cu);
9499 if (pdi->tag == DW_TAG_subprogram
9500 || pdi->tag == DW_TAG_inlined_subroutine
9501 || pdi->tag == DW_TAG_lexical_block)
9502 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9503 pdi = pdi->die_sibling;
9504 }
9505 }
9506 }
9507
9508 /* Read a partial die corresponding to an enumeration type. */
9509
9510 static void
9511 add_partial_enumeration (struct partial_die_info *enum_pdi,
9512 struct dwarf2_cu *cu)
9513 {
9514 struct partial_die_info *pdi;
9515
9516 if (enum_pdi->name != NULL)
9517 add_partial_symbol (enum_pdi, cu);
9518
9519 pdi = enum_pdi->die_child;
9520 while (pdi)
9521 {
9522 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9523 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9524 else
9525 add_partial_symbol (pdi, cu);
9526 pdi = pdi->die_sibling;
9527 }
9528 }
9529
9530 /* Return the initial uleb128 in the die at INFO_PTR. */
9531
9532 static unsigned int
9533 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9534 {
9535 unsigned int bytes_read;
9536
9537 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9538 }
9539
9540 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9541 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9542
9543 Return the corresponding abbrev, or NULL if the number is zero (indicating
9544 an empty DIE). In either case *BYTES_READ will be set to the length of
9545 the initial number. */
9546
9547 static struct abbrev_info *
9548 peek_die_abbrev (const die_reader_specs &reader,
9549 const gdb_byte *info_ptr, unsigned int *bytes_read)
9550 {
9551 dwarf2_cu *cu = reader.cu;
9552 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9553 unsigned int abbrev_number
9554 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9555
9556 if (abbrev_number == 0)
9557 return NULL;
9558
9559 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9560 if (!abbrev)
9561 {
9562 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9563 " at offset 0x%x [in module %s]"),
9564 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9565 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
9566 }
9567
9568 return abbrev;
9569 }
9570
9571 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9572 Returns a pointer to the end of a series of DIEs, terminated by an empty
9573 DIE. Any children of the skipped DIEs will also be skipped. */
9574
9575 static const gdb_byte *
9576 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9577 {
9578 while (1)
9579 {
9580 unsigned int bytes_read;
9581 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9582
9583 if (abbrev == NULL)
9584 return info_ptr + bytes_read;
9585 else
9586 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9587 }
9588 }
9589
9590 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9591 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9592 abbrev corresponding to that skipped uleb128 should be passed in
9593 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9594 children. */
9595
9596 static const gdb_byte *
9597 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9598 struct abbrev_info *abbrev)
9599 {
9600 unsigned int bytes_read;
9601 struct attribute attr;
9602 bfd *abfd = reader->abfd;
9603 struct dwarf2_cu *cu = reader->cu;
9604 const gdb_byte *buffer = reader->buffer;
9605 const gdb_byte *buffer_end = reader->buffer_end;
9606 unsigned int form, i;
9607
9608 for (i = 0; i < abbrev->num_attrs; i++)
9609 {
9610 /* The only abbrev we care about is DW_AT_sibling. */
9611 if (abbrev->attrs[i].name == DW_AT_sibling)
9612 {
9613 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9614 if (attr.form == DW_FORM_ref_addr)
9615 complaint (&symfile_complaints,
9616 _("ignoring absolute DW_AT_sibling"));
9617 else
9618 {
9619 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9620 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9621
9622 if (sibling_ptr < info_ptr)
9623 complaint (&symfile_complaints,
9624 _("DW_AT_sibling points backwards"));
9625 else if (sibling_ptr > reader->buffer_end)
9626 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9627 else
9628 return sibling_ptr;
9629 }
9630 }
9631
9632 /* If it isn't DW_AT_sibling, skip this attribute. */
9633 form = abbrev->attrs[i].form;
9634 skip_attribute:
9635 switch (form)
9636 {
9637 case DW_FORM_ref_addr:
9638 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9639 and later it is offset sized. */
9640 if (cu->header.version == 2)
9641 info_ptr += cu->header.addr_size;
9642 else
9643 info_ptr += cu->header.offset_size;
9644 break;
9645 case DW_FORM_GNU_ref_alt:
9646 info_ptr += cu->header.offset_size;
9647 break;
9648 case DW_FORM_addr:
9649 info_ptr += cu->header.addr_size;
9650 break;
9651 case DW_FORM_data1:
9652 case DW_FORM_ref1:
9653 case DW_FORM_flag:
9654 info_ptr += 1;
9655 break;
9656 case DW_FORM_flag_present:
9657 case DW_FORM_implicit_const:
9658 break;
9659 case DW_FORM_data2:
9660 case DW_FORM_ref2:
9661 info_ptr += 2;
9662 break;
9663 case DW_FORM_data4:
9664 case DW_FORM_ref4:
9665 info_ptr += 4;
9666 break;
9667 case DW_FORM_data8:
9668 case DW_FORM_ref8:
9669 case DW_FORM_ref_sig8:
9670 info_ptr += 8;
9671 break;
9672 case DW_FORM_data16:
9673 info_ptr += 16;
9674 break;
9675 case DW_FORM_string:
9676 read_direct_string (abfd, info_ptr, &bytes_read);
9677 info_ptr += bytes_read;
9678 break;
9679 case DW_FORM_sec_offset:
9680 case DW_FORM_strp:
9681 case DW_FORM_GNU_strp_alt:
9682 info_ptr += cu->header.offset_size;
9683 break;
9684 case DW_FORM_exprloc:
9685 case DW_FORM_block:
9686 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9687 info_ptr += bytes_read;
9688 break;
9689 case DW_FORM_block1:
9690 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9691 break;
9692 case DW_FORM_block2:
9693 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9694 break;
9695 case DW_FORM_block4:
9696 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9697 break;
9698 case DW_FORM_sdata:
9699 case DW_FORM_udata:
9700 case DW_FORM_ref_udata:
9701 case DW_FORM_GNU_addr_index:
9702 case DW_FORM_GNU_str_index:
9703 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9704 break;
9705 case DW_FORM_indirect:
9706 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9707 info_ptr += bytes_read;
9708 /* We need to continue parsing from here, so just go back to
9709 the top. */
9710 goto skip_attribute;
9711
9712 default:
9713 error (_("Dwarf Error: Cannot handle %s "
9714 "in DWARF reader [in module %s]"),
9715 dwarf_form_name (form),
9716 bfd_get_filename (abfd));
9717 }
9718 }
9719
9720 if (abbrev->has_children)
9721 return skip_children (reader, info_ptr);
9722 else
9723 return info_ptr;
9724 }
9725
9726 /* Locate ORIG_PDI's sibling.
9727 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9728
9729 static const gdb_byte *
9730 locate_pdi_sibling (const struct die_reader_specs *reader,
9731 struct partial_die_info *orig_pdi,
9732 const gdb_byte *info_ptr)
9733 {
9734 /* Do we know the sibling already? */
9735
9736 if (orig_pdi->sibling)
9737 return orig_pdi->sibling;
9738
9739 /* Are there any children to deal with? */
9740
9741 if (!orig_pdi->has_children)
9742 return info_ptr;
9743
9744 /* Skip the children the long way. */
9745
9746 return skip_children (reader, info_ptr);
9747 }
9748
9749 /* Expand this partial symbol table into a full symbol table. SELF is
9750 not NULL. */
9751
9752 static void
9753 dwarf2_read_symtab (struct partial_symtab *self,
9754 struct objfile *objfile)
9755 {
9756 struct dwarf2_per_objfile *dwarf2_per_objfile
9757 = get_dwarf2_per_objfile (objfile);
9758
9759 if (self->readin)
9760 {
9761 warning (_("bug: psymtab for %s is already read in."),
9762 self->filename);
9763 }
9764 else
9765 {
9766 if (info_verbose)
9767 {
9768 printf_filtered (_("Reading in symbols for %s..."),
9769 self->filename);
9770 gdb_flush (gdb_stdout);
9771 }
9772
9773 /* If this psymtab is constructed from a debug-only objfile, the
9774 has_section_at_zero flag will not necessarily be correct. We
9775 can get the correct value for this flag by looking at the data
9776 associated with the (presumably stripped) associated objfile. */
9777 if (objfile->separate_debug_objfile_backlink)
9778 {
9779 struct dwarf2_per_objfile *dpo_backlink
9780 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9781
9782 dwarf2_per_objfile->has_section_at_zero
9783 = dpo_backlink->has_section_at_zero;
9784 }
9785
9786 dwarf2_per_objfile->reading_partial_symbols = 0;
9787
9788 psymtab_to_symtab_1 (self);
9789
9790 /* Finish up the debug error message. */
9791 if (info_verbose)
9792 printf_filtered (_("done.\n"));
9793 }
9794
9795 process_cu_includes (dwarf2_per_objfile);
9796 }
9797 \f
9798 /* Reading in full CUs. */
9799
9800 /* Add PER_CU to the queue. */
9801
9802 static void
9803 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9804 enum language pretend_language)
9805 {
9806 struct dwarf2_queue_item *item;
9807
9808 per_cu->queued = 1;
9809 item = XNEW (struct dwarf2_queue_item);
9810 item->per_cu = per_cu;
9811 item->pretend_language = pretend_language;
9812 item->next = NULL;
9813
9814 if (dwarf2_queue == NULL)
9815 dwarf2_queue = item;
9816 else
9817 dwarf2_queue_tail->next = item;
9818
9819 dwarf2_queue_tail = item;
9820 }
9821
9822 /* If PER_CU is not yet queued, add it to the queue.
9823 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9824 dependency.
9825 The result is non-zero if PER_CU was queued, otherwise the result is zero
9826 meaning either PER_CU is already queued or it is already loaded.
9827
9828 N.B. There is an invariant here that if a CU is queued then it is loaded.
9829 The caller is required to load PER_CU if we return non-zero. */
9830
9831 static int
9832 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9833 struct dwarf2_per_cu_data *per_cu,
9834 enum language pretend_language)
9835 {
9836 /* We may arrive here during partial symbol reading, if we need full
9837 DIEs to process an unusual case (e.g. template arguments). Do
9838 not queue PER_CU, just tell our caller to load its DIEs. */
9839 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9840 {
9841 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9842 return 1;
9843 return 0;
9844 }
9845
9846 /* Mark the dependence relation so that we don't flush PER_CU
9847 too early. */
9848 if (dependent_cu != NULL)
9849 dwarf2_add_dependence (dependent_cu, per_cu);
9850
9851 /* If it's already on the queue, we have nothing to do. */
9852 if (per_cu->queued)
9853 return 0;
9854
9855 /* If the compilation unit is already loaded, just mark it as
9856 used. */
9857 if (per_cu->cu != NULL)
9858 {
9859 per_cu->cu->last_used = 0;
9860 return 0;
9861 }
9862
9863 /* Add it to the queue. */
9864 queue_comp_unit (per_cu, pretend_language);
9865
9866 return 1;
9867 }
9868
9869 /* Process the queue. */
9870
9871 static void
9872 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9873 {
9874 struct dwarf2_queue_item *item, *next_item;
9875
9876 if (dwarf_read_debug)
9877 {
9878 fprintf_unfiltered (gdb_stdlog,
9879 "Expanding one or more symtabs of objfile %s ...\n",
9880 objfile_name (dwarf2_per_objfile->objfile));
9881 }
9882
9883 /* The queue starts out with one item, but following a DIE reference
9884 may load a new CU, adding it to the end of the queue. */
9885 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9886 {
9887 if ((dwarf2_per_objfile->using_index
9888 ? !item->per_cu->v.quick->compunit_symtab
9889 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9890 /* Skip dummy CUs. */
9891 && item->per_cu->cu != NULL)
9892 {
9893 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9894 unsigned int debug_print_threshold;
9895 char buf[100];
9896
9897 if (per_cu->is_debug_types)
9898 {
9899 struct signatured_type *sig_type =
9900 (struct signatured_type *) per_cu;
9901
9902 sprintf (buf, "TU %s at offset 0x%x",
9903 hex_string (sig_type->signature),
9904 to_underlying (per_cu->sect_off));
9905 /* There can be 100s of TUs.
9906 Only print them in verbose mode. */
9907 debug_print_threshold = 2;
9908 }
9909 else
9910 {
9911 sprintf (buf, "CU at offset 0x%x",
9912 to_underlying (per_cu->sect_off));
9913 debug_print_threshold = 1;
9914 }
9915
9916 if (dwarf_read_debug >= debug_print_threshold)
9917 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9918
9919 if (per_cu->is_debug_types)
9920 process_full_type_unit (per_cu, item->pretend_language);
9921 else
9922 process_full_comp_unit (per_cu, item->pretend_language);
9923
9924 if (dwarf_read_debug >= debug_print_threshold)
9925 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9926 }
9927
9928 item->per_cu->queued = 0;
9929 next_item = item->next;
9930 xfree (item);
9931 }
9932
9933 dwarf2_queue_tail = NULL;
9934
9935 if (dwarf_read_debug)
9936 {
9937 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9938 objfile_name (dwarf2_per_objfile->objfile));
9939 }
9940 }
9941
9942 /* Free all allocated queue entries. This function only releases anything if
9943 an error was thrown; if the queue was processed then it would have been
9944 freed as we went along. */
9945
9946 static void
9947 dwarf2_release_queue (void *dummy)
9948 {
9949 struct dwarf2_queue_item *item, *last;
9950
9951 item = dwarf2_queue;
9952 while (item)
9953 {
9954 /* Anything still marked queued is likely to be in an
9955 inconsistent state, so discard it. */
9956 if (item->per_cu->queued)
9957 {
9958 if (item->per_cu->cu != NULL)
9959 free_one_cached_comp_unit (item->per_cu);
9960 item->per_cu->queued = 0;
9961 }
9962
9963 last = item;
9964 item = item->next;
9965 xfree (last);
9966 }
9967
9968 dwarf2_queue = dwarf2_queue_tail = NULL;
9969 }
9970
9971 /* Read in full symbols for PST, and anything it depends on. */
9972
9973 static void
9974 psymtab_to_symtab_1 (struct partial_symtab *pst)
9975 {
9976 struct dwarf2_per_cu_data *per_cu;
9977 int i;
9978
9979 if (pst->readin)
9980 return;
9981
9982 for (i = 0; i < pst->number_of_dependencies; i++)
9983 if (!pst->dependencies[i]->readin
9984 && pst->dependencies[i]->user == NULL)
9985 {
9986 /* Inform about additional files that need to be read in. */
9987 if (info_verbose)
9988 {
9989 /* FIXME: i18n: Need to make this a single string. */
9990 fputs_filtered (" ", gdb_stdout);
9991 wrap_here ("");
9992 fputs_filtered ("and ", gdb_stdout);
9993 wrap_here ("");
9994 printf_filtered ("%s...", pst->dependencies[i]->filename);
9995 wrap_here (""); /* Flush output. */
9996 gdb_flush (gdb_stdout);
9997 }
9998 psymtab_to_symtab_1 (pst->dependencies[i]);
9999 }
10000
10001 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10002
10003 if (per_cu == NULL)
10004 {
10005 /* It's an include file, no symbols to read for it.
10006 Everything is in the parent symtab. */
10007 pst->readin = 1;
10008 return;
10009 }
10010
10011 dw2_do_instantiate_symtab (per_cu);
10012 }
10013
10014 /* Trivial hash function for die_info: the hash value of a DIE
10015 is its offset in .debug_info for this objfile. */
10016
10017 static hashval_t
10018 die_hash (const void *item)
10019 {
10020 const struct die_info *die = (const struct die_info *) item;
10021
10022 return to_underlying (die->sect_off);
10023 }
10024
10025 /* Trivial comparison function for die_info structures: two DIEs
10026 are equal if they have the same offset. */
10027
10028 static int
10029 die_eq (const void *item_lhs, const void *item_rhs)
10030 {
10031 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
10032 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
10033
10034 return die_lhs->sect_off == die_rhs->sect_off;
10035 }
10036
10037 /* die_reader_func for load_full_comp_unit.
10038 This is identical to read_signatured_type_reader,
10039 but is kept separate for now. */
10040
10041 static void
10042 load_full_comp_unit_reader (const struct die_reader_specs *reader,
10043 const gdb_byte *info_ptr,
10044 struct die_info *comp_unit_die,
10045 int has_children,
10046 void *data)
10047 {
10048 struct dwarf2_cu *cu = reader->cu;
10049 enum language *language_ptr = (enum language *) data;
10050
10051 gdb_assert (cu->die_hash == NULL);
10052 cu->die_hash =
10053 htab_create_alloc_ex (cu->header.length / 12,
10054 die_hash,
10055 die_eq,
10056 NULL,
10057 &cu->comp_unit_obstack,
10058 hashtab_obstack_allocate,
10059 dummy_obstack_deallocate);
10060
10061 if (has_children)
10062 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10063 &info_ptr, comp_unit_die);
10064 cu->dies = comp_unit_die;
10065 /* comp_unit_die is not stored in die_hash, no need. */
10066
10067 /* We try not to read any attributes in this function, because not
10068 all CUs needed for references have been loaded yet, and symbol
10069 table processing isn't initialized. But we have to set the CU language,
10070 or we won't be able to build types correctly.
10071 Similarly, if we do not read the producer, we can not apply
10072 producer-specific interpretation. */
10073 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
10074 }
10075
10076 /* Load the DIEs associated with PER_CU into memory. */
10077
10078 static void
10079 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10080 enum language pretend_language)
10081 {
10082 gdb_assert (! this_cu->is_debug_types);
10083
10084 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10085 load_full_comp_unit_reader, &pretend_language);
10086 }
10087
10088 /* Add a DIE to the delayed physname list. */
10089
10090 static void
10091 add_to_method_list (struct type *type, int fnfield_index, int index,
10092 const char *name, struct die_info *die,
10093 struct dwarf2_cu *cu)
10094 {
10095 struct delayed_method_info mi;
10096 mi.type = type;
10097 mi.fnfield_index = fnfield_index;
10098 mi.index = index;
10099 mi.name = name;
10100 mi.die = die;
10101 cu->method_list.push_back (mi);
10102 }
10103
10104 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10105 "const" / "volatile". If so, decrements LEN by the length of the
10106 modifier and return true. Otherwise return false. */
10107
10108 template<size_t N>
10109 static bool
10110 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10111 {
10112 size_t mod_len = sizeof (mod) - 1;
10113 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10114 {
10115 len -= mod_len;
10116 return true;
10117 }
10118 return false;
10119 }
10120
10121 /* Compute the physnames of any methods on the CU's method list.
10122
10123 The computation of method physnames is delayed in order to avoid the
10124 (bad) condition that one of the method's formal parameters is of an as yet
10125 incomplete type. */
10126
10127 static void
10128 compute_delayed_physnames (struct dwarf2_cu *cu)
10129 {
10130 /* Only C++ delays computing physnames. */
10131 if (cu->method_list.empty ())
10132 return;
10133 gdb_assert (cu->language == language_cplus);
10134
10135 for (struct delayed_method_info &mi : cu->method_list)
10136 {
10137 const char *physname;
10138 struct fn_fieldlist *fn_flp
10139 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
10140 physname = dwarf2_physname (mi.name, mi.die, cu);
10141 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
10142 = physname ? physname : "";
10143
10144 /* Since there's no tag to indicate whether a method is a
10145 const/volatile overload, extract that information out of the
10146 demangled name. */
10147 if (physname != NULL)
10148 {
10149 size_t len = strlen (physname);
10150
10151 while (1)
10152 {
10153 if (physname[len] == ')') /* shortcut */
10154 break;
10155 else if (check_modifier (physname, len, " const"))
10156 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
10157 else if (check_modifier (physname, len, " volatile"))
10158 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
10159 else
10160 break;
10161 }
10162 }
10163 }
10164
10165 /* The list is no longer needed. */
10166 cu->method_list.clear ();
10167 }
10168
10169 /* Go objects should be embedded in a DW_TAG_module DIE,
10170 and it's not clear if/how imported objects will appear.
10171 To keep Go support simple until that's worked out,
10172 go back through what we've read and create something usable.
10173 We could do this while processing each DIE, and feels kinda cleaner,
10174 but that way is more invasive.
10175 This is to, for example, allow the user to type "p var" or "b main"
10176 without having to specify the package name, and allow lookups
10177 of module.object to work in contexts that use the expression
10178 parser. */
10179
10180 static void
10181 fixup_go_packaging (struct dwarf2_cu *cu)
10182 {
10183 char *package_name = NULL;
10184 struct pending *list;
10185 int i;
10186
10187 for (list = global_symbols; list != NULL; list = list->next)
10188 {
10189 for (i = 0; i < list->nsyms; ++i)
10190 {
10191 struct symbol *sym = list->symbol[i];
10192
10193 if (SYMBOL_LANGUAGE (sym) == language_go
10194 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10195 {
10196 char *this_package_name = go_symbol_package_name (sym);
10197
10198 if (this_package_name == NULL)
10199 continue;
10200 if (package_name == NULL)
10201 package_name = this_package_name;
10202 else
10203 {
10204 struct objfile *objfile
10205 = cu->per_cu->dwarf2_per_objfile->objfile;
10206 if (strcmp (package_name, this_package_name) != 0)
10207 complaint (&symfile_complaints,
10208 _("Symtab %s has objects from two different Go packages: %s and %s"),
10209 (symbol_symtab (sym) != NULL
10210 ? symtab_to_filename_for_display
10211 (symbol_symtab (sym))
10212 : objfile_name (objfile)),
10213 this_package_name, package_name);
10214 xfree (this_package_name);
10215 }
10216 }
10217 }
10218 }
10219
10220 if (package_name != NULL)
10221 {
10222 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10223 const char *saved_package_name
10224 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10225 package_name,
10226 strlen (package_name));
10227 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10228 saved_package_name);
10229 struct symbol *sym;
10230
10231 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10232
10233 sym = allocate_symbol (objfile);
10234 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
10235 SYMBOL_SET_NAMES (sym, saved_package_name,
10236 strlen (saved_package_name), 0, objfile);
10237 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10238 e.g., "main" finds the "main" module and not C's main(). */
10239 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10240 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
10241 SYMBOL_TYPE (sym) = type;
10242
10243 add_symbol_to_list (sym, &global_symbols);
10244
10245 xfree (package_name);
10246 }
10247 }
10248
10249 /* Return the symtab for PER_CU. This works properly regardless of
10250 whether we're using the index or psymtabs. */
10251
10252 static struct compunit_symtab *
10253 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10254 {
10255 return (per_cu->dwarf2_per_objfile->using_index
10256 ? per_cu->v.quick->compunit_symtab
10257 : per_cu->v.psymtab->compunit_symtab);
10258 }
10259
10260 /* A helper function for computing the list of all symbol tables
10261 included by PER_CU. */
10262
10263 static void
10264 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10265 htab_t all_children, htab_t all_type_symtabs,
10266 struct dwarf2_per_cu_data *per_cu,
10267 struct compunit_symtab *immediate_parent)
10268 {
10269 void **slot;
10270 int ix;
10271 struct compunit_symtab *cust;
10272 struct dwarf2_per_cu_data *iter;
10273
10274 slot = htab_find_slot (all_children, per_cu, INSERT);
10275 if (*slot != NULL)
10276 {
10277 /* This inclusion and its children have been processed. */
10278 return;
10279 }
10280
10281 *slot = per_cu;
10282 /* Only add a CU if it has a symbol table. */
10283 cust = get_compunit_symtab (per_cu);
10284 if (cust != NULL)
10285 {
10286 /* If this is a type unit only add its symbol table if we haven't
10287 seen it yet (type unit per_cu's can share symtabs). */
10288 if (per_cu->is_debug_types)
10289 {
10290 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10291 if (*slot == NULL)
10292 {
10293 *slot = cust;
10294 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10295 if (cust->user == NULL)
10296 cust->user = immediate_parent;
10297 }
10298 }
10299 else
10300 {
10301 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10302 if (cust->user == NULL)
10303 cust->user = immediate_parent;
10304 }
10305 }
10306
10307 for (ix = 0;
10308 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10309 ++ix)
10310 {
10311 recursively_compute_inclusions (result, all_children,
10312 all_type_symtabs, iter, cust);
10313 }
10314 }
10315
10316 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10317 PER_CU. */
10318
10319 static void
10320 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10321 {
10322 gdb_assert (! per_cu->is_debug_types);
10323
10324 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10325 {
10326 int ix, len;
10327 struct dwarf2_per_cu_data *per_cu_iter;
10328 struct compunit_symtab *compunit_symtab_iter;
10329 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10330 htab_t all_children, all_type_symtabs;
10331 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10332
10333 /* If we don't have a symtab, we can just skip this case. */
10334 if (cust == NULL)
10335 return;
10336
10337 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10338 NULL, xcalloc, xfree);
10339 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10340 NULL, xcalloc, xfree);
10341
10342 for (ix = 0;
10343 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10344 ix, per_cu_iter);
10345 ++ix)
10346 {
10347 recursively_compute_inclusions (&result_symtabs, all_children,
10348 all_type_symtabs, per_cu_iter,
10349 cust);
10350 }
10351
10352 /* Now we have a transitive closure of all the included symtabs. */
10353 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10354 cust->includes
10355 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10356 struct compunit_symtab *, len + 1);
10357 for (ix = 0;
10358 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10359 compunit_symtab_iter);
10360 ++ix)
10361 cust->includes[ix] = compunit_symtab_iter;
10362 cust->includes[len] = NULL;
10363
10364 VEC_free (compunit_symtab_ptr, result_symtabs);
10365 htab_delete (all_children);
10366 htab_delete (all_type_symtabs);
10367 }
10368 }
10369
10370 /* Compute the 'includes' field for the symtabs of all the CUs we just
10371 read. */
10372
10373 static void
10374 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10375 {
10376 int ix;
10377 struct dwarf2_per_cu_data *iter;
10378
10379 for (ix = 0;
10380 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10381 ix, iter);
10382 ++ix)
10383 {
10384 if (! iter->is_debug_types)
10385 compute_compunit_symtab_includes (iter);
10386 }
10387
10388 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10389 }
10390
10391 /* Generate full symbol information for PER_CU, whose DIEs have
10392 already been loaded into memory. */
10393
10394 static void
10395 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10396 enum language pretend_language)
10397 {
10398 struct dwarf2_cu *cu = per_cu->cu;
10399 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10400 struct objfile *objfile = dwarf2_per_objfile->objfile;
10401 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10402 CORE_ADDR lowpc, highpc;
10403 struct compunit_symtab *cust;
10404 CORE_ADDR baseaddr;
10405 struct block *static_block;
10406 CORE_ADDR addr;
10407
10408 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10409
10410 buildsym_init ();
10411 scoped_free_pendings free_pending;
10412
10413 /* Clear the list here in case something was left over. */
10414 cu->method_list.clear ();
10415
10416 cu->list_in_scope = &file_symbols;
10417
10418 cu->language = pretend_language;
10419 cu->language_defn = language_def (cu->language);
10420
10421 /* Do line number decoding in read_file_scope () */
10422 process_die (cu->dies, cu);
10423
10424 /* For now fudge the Go package. */
10425 if (cu->language == language_go)
10426 fixup_go_packaging (cu);
10427
10428 /* Now that we have processed all the DIEs in the CU, all the types
10429 should be complete, and it should now be safe to compute all of the
10430 physnames. */
10431 compute_delayed_physnames (cu);
10432
10433 /* Some compilers don't define a DW_AT_high_pc attribute for the
10434 compilation unit. If the DW_AT_high_pc is missing, synthesize
10435 it, by scanning the DIE's below the compilation unit. */
10436 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10437
10438 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10439 static_block = end_symtab_get_static_block (addr, 0, 1);
10440
10441 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10442 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10443 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10444 addrmap to help ensure it has an accurate map of pc values belonging to
10445 this comp unit. */
10446 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10447
10448 cust = end_symtab_from_static_block (static_block,
10449 SECT_OFF_TEXT (objfile), 0);
10450
10451 if (cust != NULL)
10452 {
10453 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10454
10455 /* Set symtab language to language from DW_AT_language. If the
10456 compilation is from a C file generated by language preprocessors, do
10457 not set the language if it was already deduced by start_subfile. */
10458 if (!(cu->language == language_c
10459 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10460 COMPUNIT_FILETABS (cust)->language = cu->language;
10461
10462 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10463 produce DW_AT_location with location lists but it can be possibly
10464 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10465 there were bugs in prologue debug info, fixed later in GCC-4.5
10466 by "unwind info for epilogues" patch (which is not directly related).
10467
10468 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10469 needed, it would be wrong due to missing DW_AT_producer there.
10470
10471 Still one can confuse GDB by using non-standard GCC compilation
10472 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10473 */
10474 if (cu->has_loclist && gcc_4_minor >= 5)
10475 cust->locations_valid = 1;
10476
10477 if (gcc_4_minor >= 5)
10478 cust->epilogue_unwind_valid = 1;
10479
10480 cust->call_site_htab = cu->call_site_htab;
10481 }
10482
10483 if (dwarf2_per_objfile->using_index)
10484 per_cu->v.quick->compunit_symtab = cust;
10485 else
10486 {
10487 struct partial_symtab *pst = per_cu->v.psymtab;
10488 pst->compunit_symtab = cust;
10489 pst->readin = 1;
10490 }
10491
10492 /* Push it for inclusion processing later. */
10493 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10494 }
10495
10496 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10497 already been loaded into memory. */
10498
10499 static void
10500 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10501 enum language pretend_language)
10502 {
10503 struct dwarf2_cu *cu = per_cu->cu;
10504 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10505 struct objfile *objfile = dwarf2_per_objfile->objfile;
10506 struct compunit_symtab *cust;
10507 struct signatured_type *sig_type;
10508
10509 gdb_assert (per_cu->is_debug_types);
10510 sig_type = (struct signatured_type *) per_cu;
10511
10512 buildsym_init ();
10513 scoped_free_pendings free_pending;
10514
10515 /* Clear the list here in case something was left over. */
10516 cu->method_list.clear ();
10517
10518 cu->list_in_scope = &file_symbols;
10519
10520 cu->language = pretend_language;
10521 cu->language_defn = language_def (cu->language);
10522
10523 /* The symbol tables are set up in read_type_unit_scope. */
10524 process_die (cu->dies, cu);
10525
10526 /* For now fudge the Go package. */
10527 if (cu->language == language_go)
10528 fixup_go_packaging (cu);
10529
10530 /* Now that we have processed all the DIEs in the CU, all the types
10531 should be complete, and it should now be safe to compute all of the
10532 physnames. */
10533 compute_delayed_physnames (cu);
10534
10535 /* TUs share symbol tables.
10536 If this is the first TU to use this symtab, complete the construction
10537 of it with end_expandable_symtab. Otherwise, complete the addition of
10538 this TU's symbols to the existing symtab. */
10539 if (sig_type->type_unit_group->compunit_symtab == NULL)
10540 {
10541 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10542 sig_type->type_unit_group->compunit_symtab = cust;
10543
10544 if (cust != NULL)
10545 {
10546 /* Set symtab language to language from DW_AT_language. If the
10547 compilation is from a C file generated by language preprocessors,
10548 do not set the language if it was already deduced by
10549 start_subfile. */
10550 if (!(cu->language == language_c
10551 && COMPUNIT_FILETABS (cust)->language != language_c))
10552 COMPUNIT_FILETABS (cust)->language = cu->language;
10553 }
10554 }
10555 else
10556 {
10557 augment_type_symtab ();
10558 cust = sig_type->type_unit_group->compunit_symtab;
10559 }
10560
10561 if (dwarf2_per_objfile->using_index)
10562 per_cu->v.quick->compunit_symtab = cust;
10563 else
10564 {
10565 struct partial_symtab *pst = per_cu->v.psymtab;
10566 pst->compunit_symtab = cust;
10567 pst->readin = 1;
10568 }
10569 }
10570
10571 /* Process an imported unit DIE. */
10572
10573 static void
10574 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10575 {
10576 struct attribute *attr;
10577
10578 /* For now we don't handle imported units in type units. */
10579 if (cu->per_cu->is_debug_types)
10580 {
10581 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10582 " supported in type units [in module %s]"),
10583 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10584 }
10585
10586 attr = dwarf2_attr (die, DW_AT_import, cu);
10587 if (attr != NULL)
10588 {
10589 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10590 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10591 dwarf2_per_cu_data *per_cu
10592 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10593 cu->per_cu->dwarf2_per_objfile);
10594
10595 /* If necessary, add it to the queue and load its DIEs. */
10596 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10597 load_full_comp_unit (per_cu, cu->language);
10598
10599 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10600 per_cu);
10601 }
10602 }
10603
10604 /* RAII object that represents a process_die scope: i.e.,
10605 starts/finishes processing a DIE. */
10606 class process_die_scope
10607 {
10608 public:
10609 process_die_scope (die_info *die, dwarf2_cu *cu)
10610 : m_die (die), m_cu (cu)
10611 {
10612 /* We should only be processing DIEs not already in process. */
10613 gdb_assert (!m_die->in_process);
10614 m_die->in_process = true;
10615 }
10616
10617 ~process_die_scope ()
10618 {
10619 m_die->in_process = false;
10620
10621 /* If we're done processing the DIE for the CU that owns the line
10622 header, we don't need the line header anymore. */
10623 if (m_cu->line_header_die_owner == m_die)
10624 {
10625 delete m_cu->line_header;
10626 m_cu->line_header = NULL;
10627 m_cu->line_header_die_owner = NULL;
10628 }
10629 }
10630
10631 private:
10632 die_info *m_die;
10633 dwarf2_cu *m_cu;
10634 };
10635
10636 /* Process a die and its children. */
10637
10638 static void
10639 process_die (struct die_info *die, struct dwarf2_cu *cu)
10640 {
10641 process_die_scope scope (die, cu);
10642
10643 switch (die->tag)
10644 {
10645 case DW_TAG_padding:
10646 break;
10647 case DW_TAG_compile_unit:
10648 case DW_TAG_partial_unit:
10649 read_file_scope (die, cu);
10650 break;
10651 case DW_TAG_type_unit:
10652 read_type_unit_scope (die, cu);
10653 break;
10654 case DW_TAG_subprogram:
10655 case DW_TAG_inlined_subroutine:
10656 read_func_scope (die, cu);
10657 break;
10658 case DW_TAG_lexical_block:
10659 case DW_TAG_try_block:
10660 case DW_TAG_catch_block:
10661 read_lexical_block_scope (die, cu);
10662 break;
10663 case DW_TAG_call_site:
10664 case DW_TAG_GNU_call_site:
10665 read_call_site_scope (die, cu);
10666 break;
10667 case DW_TAG_class_type:
10668 case DW_TAG_interface_type:
10669 case DW_TAG_structure_type:
10670 case DW_TAG_union_type:
10671 process_structure_scope (die, cu);
10672 break;
10673 case DW_TAG_enumeration_type:
10674 process_enumeration_scope (die, cu);
10675 break;
10676
10677 /* These dies have a type, but processing them does not create
10678 a symbol or recurse to process the children. Therefore we can
10679 read them on-demand through read_type_die. */
10680 case DW_TAG_subroutine_type:
10681 case DW_TAG_set_type:
10682 case DW_TAG_array_type:
10683 case DW_TAG_pointer_type:
10684 case DW_TAG_ptr_to_member_type:
10685 case DW_TAG_reference_type:
10686 case DW_TAG_rvalue_reference_type:
10687 case DW_TAG_string_type:
10688 break;
10689
10690 case DW_TAG_base_type:
10691 case DW_TAG_subrange_type:
10692 case DW_TAG_typedef:
10693 /* Add a typedef symbol for the type definition, if it has a
10694 DW_AT_name. */
10695 new_symbol (die, read_type_die (die, cu), cu);
10696 break;
10697 case DW_TAG_common_block:
10698 read_common_block (die, cu);
10699 break;
10700 case DW_TAG_common_inclusion:
10701 break;
10702 case DW_TAG_namespace:
10703 cu->processing_has_namespace_info = 1;
10704 read_namespace (die, cu);
10705 break;
10706 case DW_TAG_module:
10707 cu->processing_has_namespace_info = 1;
10708 read_module (die, cu);
10709 break;
10710 case DW_TAG_imported_declaration:
10711 cu->processing_has_namespace_info = 1;
10712 if (read_namespace_alias (die, cu))
10713 break;
10714 /* The declaration is not a global namespace alias: fall through. */
10715 case DW_TAG_imported_module:
10716 cu->processing_has_namespace_info = 1;
10717 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10718 || cu->language != language_fortran))
10719 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10720 dwarf_tag_name (die->tag));
10721 read_import_statement (die, cu);
10722 break;
10723
10724 case DW_TAG_imported_unit:
10725 process_imported_unit_die (die, cu);
10726 break;
10727
10728 case DW_TAG_variable:
10729 read_variable (die, cu);
10730 break;
10731
10732 default:
10733 new_symbol (die, NULL, cu);
10734 break;
10735 }
10736 }
10737 \f
10738 /* DWARF name computation. */
10739
10740 /* A helper function for dwarf2_compute_name which determines whether DIE
10741 needs to have the name of the scope prepended to the name listed in the
10742 die. */
10743
10744 static int
10745 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10746 {
10747 struct attribute *attr;
10748
10749 switch (die->tag)
10750 {
10751 case DW_TAG_namespace:
10752 case DW_TAG_typedef:
10753 case DW_TAG_class_type:
10754 case DW_TAG_interface_type:
10755 case DW_TAG_structure_type:
10756 case DW_TAG_union_type:
10757 case DW_TAG_enumeration_type:
10758 case DW_TAG_enumerator:
10759 case DW_TAG_subprogram:
10760 case DW_TAG_inlined_subroutine:
10761 case DW_TAG_member:
10762 case DW_TAG_imported_declaration:
10763 return 1;
10764
10765 case DW_TAG_variable:
10766 case DW_TAG_constant:
10767 /* We only need to prefix "globally" visible variables. These include
10768 any variable marked with DW_AT_external or any variable that
10769 lives in a namespace. [Variables in anonymous namespaces
10770 require prefixing, but they are not DW_AT_external.] */
10771
10772 if (dwarf2_attr (die, DW_AT_specification, cu))
10773 {
10774 struct dwarf2_cu *spec_cu = cu;
10775
10776 return die_needs_namespace (die_specification (die, &spec_cu),
10777 spec_cu);
10778 }
10779
10780 attr = dwarf2_attr (die, DW_AT_external, cu);
10781 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10782 && die->parent->tag != DW_TAG_module)
10783 return 0;
10784 /* A variable in a lexical block of some kind does not need a
10785 namespace, even though in C++ such variables may be external
10786 and have a mangled name. */
10787 if (die->parent->tag == DW_TAG_lexical_block
10788 || die->parent->tag == DW_TAG_try_block
10789 || die->parent->tag == DW_TAG_catch_block
10790 || die->parent->tag == DW_TAG_subprogram)
10791 return 0;
10792 return 1;
10793
10794 default:
10795 return 0;
10796 }
10797 }
10798
10799 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10800 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10801 defined for the given DIE. */
10802
10803 static struct attribute *
10804 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10805 {
10806 struct attribute *attr;
10807
10808 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10809 if (attr == NULL)
10810 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10811
10812 return attr;
10813 }
10814
10815 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10816 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10817 defined for the given DIE. */
10818
10819 static const char *
10820 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10821 {
10822 const char *linkage_name;
10823
10824 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10825 if (linkage_name == NULL)
10826 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10827
10828 return linkage_name;
10829 }
10830
10831 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10832 compute the physname for the object, which include a method's:
10833 - formal parameters (C++),
10834 - receiver type (Go),
10835
10836 The term "physname" is a bit confusing.
10837 For C++, for example, it is the demangled name.
10838 For Go, for example, it's the mangled name.
10839
10840 For Ada, return the DIE's linkage name rather than the fully qualified
10841 name. PHYSNAME is ignored..
10842
10843 The result is allocated on the objfile_obstack and canonicalized. */
10844
10845 static const char *
10846 dwarf2_compute_name (const char *name,
10847 struct die_info *die, struct dwarf2_cu *cu,
10848 int physname)
10849 {
10850 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10851
10852 if (name == NULL)
10853 name = dwarf2_name (die, cu);
10854
10855 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10856 but otherwise compute it by typename_concat inside GDB.
10857 FIXME: Actually this is not really true, or at least not always true.
10858 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10859 Fortran names because there is no mangling standard. So new_symbol
10860 will set the demangled name to the result of dwarf2_full_name, and it is
10861 the demangled name that GDB uses if it exists. */
10862 if (cu->language == language_ada
10863 || (cu->language == language_fortran && physname))
10864 {
10865 /* For Ada unit, we prefer the linkage name over the name, as
10866 the former contains the exported name, which the user expects
10867 to be able to reference. Ideally, we want the user to be able
10868 to reference this entity using either natural or linkage name,
10869 but we haven't started looking at this enhancement yet. */
10870 const char *linkage_name = dw2_linkage_name (die, cu);
10871
10872 if (linkage_name != NULL)
10873 return linkage_name;
10874 }
10875
10876 /* These are the only languages we know how to qualify names in. */
10877 if (name != NULL
10878 && (cu->language == language_cplus
10879 || cu->language == language_fortran || cu->language == language_d
10880 || cu->language == language_rust))
10881 {
10882 if (die_needs_namespace (die, cu))
10883 {
10884 const char *prefix;
10885 const char *canonical_name = NULL;
10886
10887 string_file buf;
10888
10889 prefix = determine_prefix (die, cu);
10890 if (*prefix != '\0')
10891 {
10892 char *prefixed_name = typename_concat (NULL, prefix, name,
10893 physname, cu);
10894
10895 buf.puts (prefixed_name);
10896 xfree (prefixed_name);
10897 }
10898 else
10899 buf.puts (name);
10900
10901 /* Template parameters may be specified in the DIE's DW_AT_name, or
10902 as children with DW_TAG_template_type_param or
10903 DW_TAG_value_type_param. If the latter, add them to the name
10904 here. If the name already has template parameters, then
10905 skip this step; some versions of GCC emit both, and
10906 it is more efficient to use the pre-computed name.
10907
10908 Something to keep in mind about this process: it is very
10909 unlikely, or in some cases downright impossible, to produce
10910 something that will match the mangled name of a function.
10911 If the definition of the function has the same debug info,
10912 we should be able to match up with it anyway. But fallbacks
10913 using the minimal symbol, for instance to find a method
10914 implemented in a stripped copy of libstdc++, will not work.
10915 If we do not have debug info for the definition, we will have to
10916 match them up some other way.
10917
10918 When we do name matching there is a related problem with function
10919 templates; two instantiated function templates are allowed to
10920 differ only by their return types, which we do not add here. */
10921
10922 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10923 {
10924 struct attribute *attr;
10925 struct die_info *child;
10926 int first = 1;
10927
10928 die->building_fullname = 1;
10929
10930 for (child = die->child; child != NULL; child = child->sibling)
10931 {
10932 struct type *type;
10933 LONGEST value;
10934 const gdb_byte *bytes;
10935 struct dwarf2_locexpr_baton *baton;
10936 struct value *v;
10937
10938 if (child->tag != DW_TAG_template_type_param
10939 && child->tag != DW_TAG_template_value_param)
10940 continue;
10941
10942 if (first)
10943 {
10944 buf.puts ("<");
10945 first = 0;
10946 }
10947 else
10948 buf.puts (", ");
10949
10950 attr = dwarf2_attr (child, DW_AT_type, cu);
10951 if (attr == NULL)
10952 {
10953 complaint (&symfile_complaints,
10954 _("template parameter missing DW_AT_type"));
10955 buf.puts ("UNKNOWN_TYPE");
10956 continue;
10957 }
10958 type = die_type (child, cu);
10959
10960 if (child->tag == DW_TAG_template_type_param)
10961 {
10962 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
10963 continue;
10964 }
10965
10966 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10967 if (attr == NULL)
10968 {
10969 complaint (&symfile_complaints,
10970 _("template parameter missing "
10971 "DW_AT_const_value"));
10972 buf.puts ("UNKNOWN_VALUE");
10973 continue;
10974 }
10975
10976 dwarf2_const_value_attr (attr, type, name,
10977 &cu->comp_unit_obstack, cu,
10978 &value, &bytes, &baton);
10979
10980 if (TYPE_NOSIGN (type))
10981 /* GDB prints characters as NUMBER 'CHAR'. If that's
10982 changed, this can use value_print instead. */
10983 c_printchar (value, type, &buf);
10984 else
10985 {
10986 struct value_print_options opts;
10987
10988 if (baton != NULL)
10989 v = dwarf2_evaluate_loc_desc (type, NULL,
10990 baton->data,
10991 baton->size,
10992 baton->per_cu);
10993 else if (bytes != NULL)
10994 {
10995 v = allocate_value (type);
10996 memcpy (value_contents_writeable (v), bytes,
10997 TYPE_LENGTH (type));
10998 }
10999 else
11000 v = value_from_longest (type, value);
11001
11002 /* Specify decimal so that we do not depend on
11003 the radix. */
11004 get_formatted_print_options (&opts, 'd');
11005 opts.raw = 1;
11006 value_print (v, &buf, &opts);
11007 release_value (v);
11008 value_free (v);
11009 }
11010 }
11011
11012 die->building_fullname = 0;
11013
11014 if (!first)
11015 {
11016 /* Close the argument list, with a space if necessary
11017 (nested templates). */
11018 if (!buf.empty () && buf.string ().back () == '>')
11019 buf.puts (" >");
11020 else
11021 buf.puts (">");
11022 }
11023 }
11024
11025 /* For C++ methods, append formal parameter type
11026 information, if PHYSNAME. */
11027
11028 if (physname && die->tag == DW_TAG_subprogram
11029 && cu->language == language_cplus)
11030 {
11031 struct type *type = read_type_die (die, cu);
11032
11033 c_type_print_args (type, &buf, 1, cu->language,
11034 &type_print_raw_options);
11035
11036 if (cu->language == language_cplus)
11037 {
11038 /* Assume that an artificial first parameter is
11039 "this", but do not crash if it is not. RealView
11040 marks unnamed (and thus unused) parameters as
11041 artificial; there is no way to differentiate
11042 the two cases. */
11043 if (TYPE_NFIELDS (type) > 0
11044 && TYPE_FIELD_ARTIFICIAL (type, 0)
11045 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11046 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11047 0))))
11048 buf.puts (" const");
11049 }
11050 }
11051
11052 const std::string &intermediate_name = buf.string ();
11053
11054 if (cu->language == language_cplus)
11055 canonical_name
11056 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11057 &objfile->per_bfd->storage_obstack);
11058
11059 /* If we only computed INTERMEDIATE_NAME, or if
11060 INTERMEDIATE_NAME is already canonical, then we need to
11061 copy it to the appropriate obstack. */
11062 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11063 name = ((const char *)
11064 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11065 intermediate_name.c_str (),
11066 intermediate_name.length ()));
11067 else
11068 name = canonical_name;
11069 }
11070 }
11071
11072 return name;
11073 }
11074
11075 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11076 If scope qualifiers are appropriate they will be added. The result
11077 will be allocated on the storage_obstack, or NULL if the DIE does
11078 not have a name. NAME may either be from a previous call to
11079 dwarf2_name or NULL.
11080
11081 The output string will be canonicalized (if C++). */
11082
11083 static const char *
11084 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11085 {
11086 return dwarf2_compute_name (name, die, cu, 0);
11087 }
11088
11089 /* Construct a physname for the given DIE in CU. NAME may either be
11090 from a previous call to dwarf2_name or NULL. The result will be
11091 allocated on the objfile_objstack or NULL if the DIE does not have a
11092 name.
11093
11094 The output string will be canonicalized (if C++). */
11095
11096 static const char *
11097 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11098 {
11099 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11100 const char *retval, *mangled = NULL, *canon = NULL;
11101 int need_copy = 1;
11102
11103 /* In this case dwarf2_compute_name is just a shortcut not building anything
11104 on its own. */
11105 if (!die_needs_namespace (die, cu))
11106 return dwarf2_compute_name (name, die, cu, 1);
11107
11108 mangled = dw2_linkage_name (die, cu);
11109
11110 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11111 See https://github.com/rust-lang/rust/issues/32925. */
11112 if (cu->language == language_rust && mangled != NULL
11113 && strchr (mangled, '{') != NULL)
11114 mangled = NULL;
11115
11116 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11117 has computed. */
11118 gdb::unique_xmalloc_ptr<char> demangled;
11119 if (mangled != NULL)
11120 {
11121 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
11122 type. It is easier for GDB users to search for such functions as
11123 `name(params)' than `long name(params)'. In such case the minimal
11124 symbol names do not match the full symbol names but for template
11125 functions there is never a need to look up their definition from their
11126 declaration so the only disadvantage remains the minimal symbol
11127 variant `long name(params)' does not have the proper inferior type.
11128 */
11129
11130 if (cu->language == language_go)
11131 {
11132 /* This is a lie, but we already lie to the caller new_symbol.
11133 new_symbol assumes we return the mangled name.
11134 This just undoes that lie until things are cleaned up. */
11135 }
11136 else
11137 {
11138 demangled.reset (gdb_demangle (mangled,
11139 (DMGL_PARAMS | DMGL_ANSI
11140 | DMGL_RET_DROP)));
11141 }
11142 if (demangled)
11143 canon = demangled.get ();
11144 else
11145 {
11146 canon = mangled;
11147 need_copy = 0;
11148 }
11149 }
11150
11151 if (canon == NULL || check_physname)
11152 {
11153 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11154
11155 if (canon != NULL && strcmp (physname, canon) != 0)
11156 {
11157 /* It may not mean a bug in GDB. The compiler could also
11158 compute DW_AT_linkage_name incorrectly. But in such case
11159 GDB would need to be bug-to-bug compatible. */
11160
11161 complaint (&symfile_complaints,
11162 _("Computed physname <%s> does not match demangled <%s> "
11163 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
11164 physname, canon, mangled, to_underlying (die->sect_off),
11165 objfile_name (objfile));
11166
11167 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11168 is available here - over computed PHYSNAME. It is safer
11169 against both buggy GDB and buggy compilers. */
11170
11171 retval = canon;
11172 }
11173 else
11174 {
11175 retval = physname;
11176 need_copy = 0;
11177 }
11178 }
11179 else
11180 retval = canon;
11181
11182 if (need_copy)
11183 retval = ((const char *)
11184 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11185 retval, strlen (retval)));
11186
11187 return retval;
11188 }
11189
11190 /* Inspect DIE in CU for a namespace alias. If one exists, record
11191 a new symbol for it.
11192
11193 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11194
11195 static int
11196 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11197 {
11198 struct attribute *attr;
11199
11200 /* If the die does not have a name, this is not a namespace
11201 alias. */
11202 attr = dwarf2_attr (die, DW_AT_name, cu);
11203 if (attr != NULL)
11204 {
11205 int num;
11206 struct die_info *d = die;
11207 struct dwarf2_cu *imported_cu = cu;
11208
11209 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11210 keep inspecting DIEs until we hit the underlying import. */
11211 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11212 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11213 {
11214 attr = dwarf2_attr (d, DW_AT_import, cu);
11215 if (attr == NULL)
11216 break;
11217
11218 d = follow_die_ref (d, attr, &imported_cu);
11219 if (d->tag != DW_TAG_imported_declaration)
11220 break;
11221 }
11222
11223 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11224 {
11225 complaint (&symfile_complaints,
11226 _("DIE at 0x%x has too many recursively imported "
11227 "declarations"), to_underlying (d->sect_off));
11228 return 0;
11229 }
11230
11231 if (attr != NULL)
11232 {
11233 struct type *type;
11234 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11235
11236 type = get_die_type_at_offset (sect_off, cu->per_cu);
11237 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11238 {
11239 /* This declaration is a global namespace alias. Add
11240 a symbol for it whose type is the aliased namespace. */
11241 new_symbol (die, type, cu);
11242 return 1;
11243 }
11244 }
11245 }
11246
11247 return 0;
11248 }
11249
11250 /* Return the using directives repository (global or local?) to use in the
11251 current context for LANGUAGE.
11252
11253 For Ada, imported declarations can materialize renamings, which *may* be
11254 global. However it is impossible (for now?) in DWARF to distinguish
11255 "external" imported declarations and "static" ones. As all imported
11256 declarations seem to be static in all other languages, make them all CU-wide
11257 global only in Ada. */
11258
11259 static struct using_direct **
11260 using_directives (enum language language)
11261 {
11262 if (language == language_ada && context_stack_depth == 0)
11263 return &global_using_directives;
11264 else
11265 return &local_using_directives;
11266 }
11267
11268 /* Read the import statement specified by the given die and record it. */
11269
11270 static void
11271 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11272 {
11273 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11274 struct attribute *import_attr;
11275 struct die_info *imported_die, *child_die;
11276 struct dwarf2_cu *imported_cu;
11277 const char *imported_name;
11278 const char *imported_name_prefix;
11279 const char *canonical_name;
11280 const char *import_alias;
11281 const char *imported_declaration = NULL;
11282 const char *import_prefix;
11283 std::vector<const char *> excludes;
11284
11285 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11286 if (import_attr == NULL)
11287 {
11288 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11289 dwarf_tag_name (die->tag));
11290 return;
11291 }
11292
11293 imported_cu = cu;
11294 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11295 imported_name = dwarf2_name (imported_die, imported_cu);
11296 if (imported_name == NULL)
11297 {
11298 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11299
11300 The import in the following code:
11301 namespace A
11302 {
11303 typedef int B;
11304 }
11305
11306 int main ()
11307 {
11308 using A::B;
11309 B b;
11310 return b;
11311 }
11312
11313 ...
11314 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11315 <52> DW_AT_decl_file : 1
11316 <53> DW_AT_decl_line : 6
11317 <54> DW_AT_import : <0x75>
11318 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11319 <59> DW_AT_name : B
11320 <5b> DW_AT_decl_file : 1
11321 <5c> DW_AT_decl_line : 2
11322 <5d> DW_AT_type : <0x6e>
11323 ...
11324 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11325 <76> DW_AT_byte_size : 4
11326 <77> DW_AT_encoding : 5 (signed)
11327
11328 imports the wrong die ( 0x75 instead of 0x58 ).
11329 This case will be ignored until the gcc bug is fixed. */
11330 return;
11331 }
11332
11333 /* Figure out the local name after import. */
11334 import_alias = dwarf2_name (die, cu);
11335
11336 /* Figure out where the statement is being imported to. */
11337 import_prefix = determine_prefix (die, cu);
11338
11339 /* Figure out what the scope of the imported die is and prepend it
11340 to the name of the imported die. */
11341 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11342
11343 if (imported_die->tag != DW_TAG_namespace
11344 && imported_die->tag != DW_TAG_module)
11345 {
11346 imported_declaration = imported_name;
11347 canonical_name = imported_name_prefix;
11348 }
11349 else if (strlen (imported_name_prefix) > 0)
11350 canonical_name = obconcat (&objfile->objfile_obstack,
11351 imported_name_prefix,
11352 (cu->language == language_d ? "." : "::"),
11353 imported_name, (char *) NULL);
11354 else
11355 canonical_name = imported_name;
11356
11357 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11358 for (child_die = die->child; child_die && child_die->tag;
11359 child_die = sibling_die (child_die))
11360 {
11361 /* DWARF-4: A Fortran use statement with a “rename list” may be
11362 represented by an imported module entry with an import attribute
11363 referring to the module and owned entries corresponding to those
11364 entities that are renamed as part of being imported. */
11365
11366 if (child_die->tag != DW_TAG_imported_declaration)
11367 {
11368 complaint (&symfile_complaints,
11369 _("child DW_TAG_imported_declaration expected "
11370 "- DIE at 0x%x [in module %s]"),
11371 to_underlying (child_die->sect_off), objfile_name (objfile));
11372 continue;
11373 }
11374
11375 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11376 if (import_attr == NULL)
11377 {
11378 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11379 dwarf_tag_name (child_die->tag));
11380 continue;
11381 }
11382
11383 imported_cu = cu;
11384 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11385 &imported_cu);
11386 imported_name = dwarf2_name (imported_die, imported_cu);
11387 if (imported_name == NULL)
11388 {
11389 complaint (&symfile_complaints,
11390 _("child DW_TAG_imported_declaration has unknown "
11391 "imported name - DIE at 0x%x [in module %s]"),
11392 to_underlying (child_die->sect_off), objfile_name (objfile));
11393 continue;
11394 }
11395
11396 excludes.push_back (imported_name);
11397
11398 process_die (child_die, cu);
11399 }
11400
11401 add_using_directive (using_directives (cu->language),
11402 import_prefix,
11403 canonical_name,
11404 import_alias,
11405 imported_declaration,
11406 excludes,
11407 0,
11408 &objfile->objfile_obstack);
11409 }
11410
11411 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11412 types, but gives them a size of zero. Starting with version 14,
11413 ICC is compatible with GCC. */
11414
11415 static int
11416 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11417 {
11418 if (!cu->checked_producer)
11419 check_producer (cu);
11420
11421 return cu->producer_is_icc_lt_14;
11422 }
11423
11424 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11425 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11426 this, it was first present in GCC release 4.3.0. */
11427
11428 static int
11429 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11430 {
11431 if (!cu->checked_producer)
11432 check_producer (cu);
11433
11434 return cu->producer_is_gcc_lt_4_3;
11435 }
11436
11437 static file_and_directory
11438 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11439 {
11440 file_and_directory res;
11441
11442 /* Find the filename. Do not use dwarf2_name here, since the filename
11443 is not a source language identifier. */
11444 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11445 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11446
11447 if (res.comp_dir == NULL
11448 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11449 && IS_ABSOLUTE_PATH (res.name))
11450 {
11451 res.comp_dir_storage = ldirname (res.name);
11452 if (!res.comp_dir_storage.empty ())
11453 res.comp_dir = res.comp_dir_storage.c_str ();
11454 }
11455 if (res.comp_dir != NULL)
11456 {
11457 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11458 directory, get rid of it. */
11459 const char *cp = strchr (res.comp_dir, ':');
11460
11461 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11462 res.comp_dir = cp + 1;
11463 }
11464
11465 if (res.name == NULL)
11466 res.name = "<unknown>";
11467
11468 return res;
11469 }
11470
11471 /* Handle DW_AT_stmt_list for a compilation unit.
11472 DIE is the DW_TAG_compile_unit die for CU.
11473 COMP_DIR is the compilation directory. LOWPC is passed to
11474 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11475
11476 static void
11477 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11478 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11479 {
11480 struct dwarf2_per_objfile *dwarf2_per_objfile
11481 = cu->per_cu->dwarf2_per_objfile;
11482 struct objfile *objfile = dwarf2_per_objfile->objfile;
11483 struct attribute *attr;
11484 struct line_header line_header_local;
11485 hashval_t line_header_local_hash;
11486 void **slot;
11487 int decode_mapping;
11488
11489 gdb_assert (! cu->per_cu->is_debug_types);
11490
11491 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11492 if (attr == NULL)
11493 return;
11494
11495 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11496
11497 /* The line header hash table is only created if needed (it exists to
11498 prevent redundant reading of the line table for partial_units).
11499 If we're given a partial_unit, we'll need it. If we're given a
11500 compile_unit, then use the line header hash table if it's already
11501 created, but don't create one just yet. */
11502
11503 if (dwarf2_per_objfile->line_header_hash == NULL
11504 && die->tag == DW_TAG_partial_unit)
11505 {
11506 dwarf2_per_objfile->line_header_hash
11507 = htab_create_alloc_ex (127, line_header_hash_voidp,
11508 line_header_eq_voidp,
11509 free_line_header_voidp,
11510 &objfile->objfile_obstack,
11511 hashtab_obstack_allocate,
11512 dummy_obstack_deallocate);
11513 }
11514
11515 line_header_local.sect_off = line_offset;
11516 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11517 line_header_local_hash = line_header_hash (&line_header_local);
11518 if (dwarf2_per_objfile->line_header_hash != NULL)
11519 {
11520 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11521 &line_header_local,
11522 line_header_local_hash, NO_INSERT);
11523
11524 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11525 is not present in *SLOT (since if there is something in *SLOT then
11526 it will be for a partial_unit). */
11527 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11528 {
11529 gdb_assert (*slot != NULL);
11530 cu->line_header = (struct line_header *) *slot;
11531 return;
11532 }
11533 }
11534
11535 /* dwarf_decode_line_header does not yet provide sufficient information.
11536 We always have to call also dwarf_decode_lines for it. */
11537 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11538 if (lh == NULL)
11539 return;
11540
11541 cu->line_header = lh.release ();
11542 cu->line_header_die_owner = die;
11543
11544 if (dwarf2_per_objfile->line_header_hash == NULL)
11545 slot = NULL;
11546 else
11547 {
11548 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11549 &line_header_local,
11550 line_header_local_hash, INSERT);
11551 gdb_assert (slot != NULL);
11552 }
11553 if (slot != NULL && *slot == NULL)
11554 {
11555 /* This newly decoded line number information unit will be owned
11556 by line_header_hash hash table. */
11557 *slot = cu->line_header;
11558 cu->line_header_die_owner = NULL;
11559 }
11560 else
11561 {
11562 /* We cannot free any current entry in (*slot) as that struct line_header
11563 may be already used by multiple CUs. Create only temporary decoded
11564 line_header for this CU - it may happen at most once for each line
11565 number information unit. And if we're not using line_header_hash
11566 then this is what we want as well. */
11567 gdb_assert (die->tag != DW_TAG_partial_unit);
11568 }
11569 decode_mapping = (die->tag != DW_TAG_partial_unit);
11570 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11571 decode_mapping);
11572
11573 }
11574
11575 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11576
11577 static void
11578 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11579 {
11580 struct dwarf2_per_objfile *dwarf2_per_objfile
11581 = cu->per_cu->dwarf2_per_objfile;
11582 struct objfile *objfile = dwarf2_per_objfile->objfile;
11583 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11584 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11585 CORE_ADDR highpc = ((CORE_ADDR) 0);
11586 struct attribute *attr;
11587 struct die_info *child_die;
11588 CORE_ADDR baseaddr;
11589
11590 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11591
11592 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11593
11594 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11595 from finish_block. */
11596 if (lowpc == ((CORE_ADDR) -1))
11597 lowpc = highpc;
11598 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11599
11600 file_and_directory fnd = find_file_and_directory (die, cu);
11601
11602 prepare_one_comp_unit (cu, die, cu->language);
11603
11604 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11605 standardised yet. As a workaround for the language detection we fall
11606 back to the DW_AT_producer string. */
11607 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11608 cu->language = language_opencl;
11609
11610 /* Similar hack for Go. */
11611 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11612 set_cu_language (DW_LANG_Go, cu);
11613
11614 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11615
11616 /* Decode line number information if present. We do this before
11617 processing child DIEs, so that the line header table is available
11618 for DW_AT_decl_file. */
11619 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11620
11621 /* Process all dies in compilation unit. */
11622 if (die->child != NULL)
11623 {
11624 child_die = die->child;
11625 while (child_die && child_die->tag)
11626 {
11627 process_die (child_die, cu);
11628 child_die = sibling_die (child_die);
11629 }
11630 }
11631
11632 /* Decode macro information, if present. Dwarf 2 macro information
11633 refers to information in the line number info statement program
11634 header, so we can only read it if we've read the header
11635 successfully. */
11636 attr = dwarf2_attr (die, DW_AT_macros, cu);
11637 if (attr == NULL)
11638 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11639 if (attr && cu->line_header)
11640 {
11641 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11642 complaint (&symfile_complaints,
11643 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11644
11645 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11646 }
11647 else
11648 {
11649 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11650 if (attr && cu->line_header)
11651 {
11652 unsigned int macro_offset = DW_UNSND (attr);
11653
11654 dwarf_decode_macros (cu, macro_offset, 0);
11655 }
11656 }
11657 }
11658
11659 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11660 Create the set of symtabs used by this TU, or if this TU is sharing
11661 symtabs with another TU and the symtabs have already been created
11662 then restore those symtabs in the line header.
11663 We don't need the pc/line-number mapping for type units. */
11664
11665 static void
11666 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
11667 {
11668 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11669 struct type_unit_group *tu_group;
11670 int first_time;
11671 struct attribute *attr;
11672 unsigned int i;
11673 struct signatured_type *sig_type;
11674
11675 gdb_assert (per_cu->is_debug_types);
11676 sig_type = (struct signatured_type *) per_cu;
11677
11678 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11679
11680 /* If we're using .gdb_index (includes -readnow) then
11681 per_cu->type_unit_group may not have been set up yet. */
11682 if (sig_type->type_unit_group == NULL)
11683 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11684 tu_group = sig_type->type_unit_group;
11685
11686 /* If we've already processed this stmt_list there's no real need to
11687 do it again, we could fake it and just recreate the part we need
11688 (file name,index -> symtab mapping). If data shows this optimization
11689 is useful we can do it then. */
11690 first_time = tu_group->compunit_symtab == NULL;
11691
11692 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11693 debug info. */
11694 line_header_up lh;
11695 if (attr != NULL)
11696 {
11697 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11698 lh = dwarf_decode_line_header (line_offset, cu);
11699 }
11700 if (lh == NULL)
11701 {
11702 if (first_time)
11703 dwarf2_start_symtab (cu, "", NULL, 0);
11704 else
11705 {
11706 gdb_assert (tu_group->symtabs == NULL);
11707 restart_symtab (tu_group->compunit_symtab, "", 0);
11708 }
11709 return;
11710 }
11711
11712 cu->line_header = lh.release ();
11713 cu->line_header_die_owner = die;
11714
11715 if (first_time)
11716 {
11717 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
11718
11719 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11720 still initializing it, and our caller (a few levels up)
11721 process_full_type_unit still needs to know if this is the first
11722 time. */
11723
11724 tu_group->num_symtabs = cu->line_header->file_names.size ();
11725 tu_group->symtabs = XNEWVEC (struct symtab *,
11726 cu->line_header->file_names.size ());
11727
11728 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11729 {
11730 file_entry &fe = cu->line_header->file_names[i];
11731
11732 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
11733
11734 if (current_subfile->symtab == NULL)
11735 {
11736 /* NOTE: start_subfile will recognize when it's been
11737 passed a file it has already seen. So we can't
11738 assume there's a simple mapping from
11739 cu->line_header->file_names to subfiles, plus
11740 cu->line_header->file_names may contain dups. */
11741 current_subfile->symtab
11742 = allocate_symtab (cust, current_subfile->name);
11743 }
11744
11745 fe.symtab = current_subfile->symtab;
11746 tu_group->symtabs[i] = fe.symtab;
11747 }
11748 }
11749 else
11750 {
11751 restart_symtab (tu_group->compunit_symtab, "", 0);
11752
11753 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11754 {
11755 file_entry &fe = cu->line_header->file_names[i];
11756
11757 fe.symtab = tu_group->symtabs[i];
11758 }
11759 }
11760
11761 /* The main symtab is allocated last. Type units don't have DW_AT_name
11762 so they don't have a "real" (so to speak) symtab anyway.
11763 There is later code that will assign the main symtab to all symbols
11764 that don't have one. We need to handle the case of a symbol with a
11765 missing symtab (DW_AT_decl_file) anyway. */
11766 }
11767
11768 /* Process DW_TAG_type_unit.
11769 For TUs we want to skip the first top level sibling if it's not the
11770 actual type being defined by this TU. In this case the first top
11771 level sibling is there to provide context only. */
11772
11773 static void
11774 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11775 {
11776 struct die_info *child_die;
11777
11778 prepare_one_comp_unit (cu, die, language_minimal);
11779
11780 /* Initialize (or reinitialize) the machinery for building symtabs.
11781 We do this before processing child DIEs, so that the line header table
11782 is available for DW_AT_decl_file. */
11783 setup_type_unit_groups (die, cu);
11784
11785 if (die->child != NULL)
11786 {
11787 child_die = die->child;
11788 while (child_die && child_die->tag)
11789 {
11790 process_die (child_die, cu);
11791 child_die = sibling_die (child_die);
11792 }
11793 }
11794 }
11795 \f
11796 /* DWO/DWP files.
11797
11798 http://gcc.gnu.org/wiki/DebugFission
11799 http://gcc.gnu.org/wiki/DebugFissionDWP
11800
11801 To simplify handling of both DWO files ("object" files with the DWARF info)
11802 and DWP files (a file with the DWOs packaged up into one file), we treat
11803 DWP files as having a collection of virtual DWO files. */
11804
11805 static hashval_t
11806 hash_dwo_file (const void *item)
11807 {
11808 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11809 hashval_t hash;
11810
11811 hash = htab_hash_string (dwo_file->dwo_name);
11812 if (dwo_file->comp_dir != NULL)
11813 hash += htab_hash_string (dwo_file->comp_dir);
11814 return hash;
11815 }
11816
11817 static int
11818 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11819 {
11820 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11821 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11822
11823 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11824 return 0;
11825 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11826 return lhs->comp_dir == rhs->comp_dir;
11827 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11828 }
11829
11830 /* Allocate a hash table for DWO files. */
11831
11832 static htab_t
11833 allocate_dwo_file_hash_table (struct objfile *objfile)
11834 {
11835 return htab_create_alloc_ex (41,
11836 hash_dwo_file,
11837 eq_dwo_file,
11838 NULL,
11839 &objfile->objfile_obstack,
11840 hashtab_obstack_allocate,
11841 dummy_obstack_deallocate);
11842 }
11843
11844 /* Lookup DWO file DWO_NAME. */
11845
11846 static void **
11847 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11848 const char *dwo_name,
11849 const char *comp_dir)
11850 {
11851 struct dwo_file find_entry;
11852 void **slot;
11853
11854 if (dwarf2_per_objfile->dwo_files == NULL)
11855 dwarf2_per_objfile->dwo_files
11856 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11857
11858 memset (&find_entry, 0, sizeof (find_entry));
11859 find_entry.dwo_name = dwo_name;
11860 find_entry.comp_dir = comp_dir;
11861 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11862
11863 return slot;
11864 }
11865
11866 static hashval_t
11867 hash_dwo_unit (const void *item)
11868 {
11869 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11870
11871 /* This drops the top 32 bits of the id, but is ok for a hash. */
11872 return dwo_unit->signature;
11873 }
11874
11875 static int
11876 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11877 {
11878 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11879 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11880
11881 /* The signature is assumed to be unique within the DWO file.
11882 So while object file CU dwo_id's always have the value zero,
11883 that's OK, assuming each object file DWO file has only one CU,
11884 and that's the rule for now. */
11885 return lhs->signature == rhs->signature;
11886 }
11887
11888 /* Allocate a hash table for DWO CUs,TUs.
11889 There is one of these tables for each of CUs,TUs for each DWO file. */
11890
11891 static htab_t
11892 allocate_dwo_unit_table (struct objfile *objfile)
11893 {
11894 /* Start out with a pretty small number.
11895 Generally DWO files contain only one CU and maybe some TUs. */
11896 return htab_create_alloc_ex (3,
11897 hash_dwo_unit,
11898 eq_dwo_unit,
11899 NULL,
11900 &objfile->objfile_obstack,
11901 hashtab_obstack_allocate,
11902 dummy_obstack_deallocate);
11903 }
11904
11905 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11906
11907 struct create_dwo_cu_data
11908 {
11909 struct dwo_file *dwo_file;
11910 struct dwo_unit dwo_unit;
11911 };
11912
11913 /* die_reader_func for create_dwo_cu. */
11914
11915 static void
11916 create_dwo_cu_reader (const struct die_reader_specs *reader,
11917 const gdb_byte *info_ptr,
11918 struct die_info *comp_unit_die,
11919 int has_children,
11920 void *datap)
11921 {
11922 struct dwarf2_cu *cu = reader->cu;
11923 sect_offset sect_off = cu->per_cu->sect_off;
11924 struct dwarf2_section_info *section = cu->per_cu->section;
11925 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11926 struct dwo_file *dwo_file = data->dwo_file;
11927 struct dwo_unit *dwo_unit = &data->dwo_unit;
11928 struct attribute *attr;
11929
11930 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11931 if (attr == NULL)
11932 {
11933 complaint (&symfile_complaints,
11934 _("Dwarf Error: debug entry at offset 0x%x is missing"
11935 " its dwo_id [in module %s]"),
11936 to_underlying (sect_off), dwo_file->dwo_name);
11937 return;
11938 }
11939
11940 dwo_unit->dwo_file = dwo_file;
11941 dwo_unit->signature = DW_UNSND (attr);
11942 dwo_unit->section = section;
11943 dwo_unit->sect_off = sect_off;
11944 dwo_unit->length = cu->per_cu->length;
11945
11946 if (dwarf_read_debug)
11947 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
11948 to_underlying (sect_off),
11949 hex_string (dwo_unit->signature));
11950 }
11951
11952 /* Create the dwo_units for the CUs in a DWO_FILE.
11953 Note: This function processes DWO files only, not DWP files. */
11954
11955 static void
11956 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11957 struct dwo_file &dwo_file, dwarf2_section_info &section,
11958 htab_t &cus_htab)
11959 {
11960 struct objfile *objfile = dwarf2_per_objfile->objfile;
11961 const gdb_byte *info_ptr, *end_ptr;
11962
11963 dwarf2_read_section (objfile, &section);
11964 info_ptr = section.buffer;
11965
11966 if (info_ptr == NULL)
11967 return;
11968
11969 if (dwarf_read_debug)
11970 {
11971 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11972 get_section_name (&section),
11973 get_section_file_name (&section));
11974 }
11975
11976 end_ptr = info_ptr + section.size;
11977 while (info_ptr < end_ptr)
11978 {
11979 struct dwarf2_per_cu_data per_cu;
11980 struct create_dwo_cu_data create_dwo_cu_data;
11981 struct dwo_unit *dwo_unit;
11982 void **slot;
11983 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11984
11985 memset (&create_dwo_cu_data.dwo_unit, 0,
11986 sizeof (create_dwo_cu_data.dwo_unit));
11987 memset (&per_cu, 0, sizeof (per_cu));
11988 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11989 per_cu.is_debug_types = 0;
11990 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11991 per_cu.section = &section;
11992 create_dwo_cu_data.dwo_file = &dwo_file;
11993
11994 init_cutu_and_read_dies_no_follow (
11995 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11996 info_ptr += per_cu.length;
11997
11998 // If the unit could not be parsed, skip it.
11999 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12000 continue;
12001
12002 if (cus_htab == NULL)
12003 cus_htab = allocate_dwo_unit_table (objfile);
12004
12005 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12006 *dwo_unit = create_dwo_cu_data.dwo_unit;
12007 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12008 gdb_assert (slot != NULL);
12009 if (*slot != NULL)
12010 {
12011 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12012 sect_offset dup_sect_off = dup_cu->sect_off;
12013
12014 complaint (&symfile_complaints,
12015 _("debug cu entry at offset 0x%x is duplicate to"
12016 " the entry at offset 0x%x, signature %s"),
12017 to_underlying (sect_off), to_underlying (dup_sect_off),
12018 hex_string (dwo_unit->signature));
12019 }
12020 *slot = (void *)dwo_unit;
12021 }
12022 }
12023
12024 /* DWP file .debug_{cu,tu}_index section format:
12025 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12026
12027 DWP Version 1:
12028
12029 Both index sections have the same format, and serve to map a 64-bit
12030 signature to a set of section numbers. Each section begins with a header,
12031 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12032 indexes, and a pool of 32-bit section numbers. The index sections will be
12033 aligned at 8-byte boundaries in the file.
12034
12035 The index section header consists of:
12036
12037 V, 32 bit version number
12038 -, 32 bits unused
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 begins at offset 16 in the section, and consists of an array
12045 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12046 order of the application binary). Unused slots in the hash table are 0.
12047 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12048
12049 The parallel table begins immediately after the hash table
12050 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12051 array of 32-bit indexes (using the byte order of the application binary),
12052 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12053 table contains a 32-bit index into the pool of section numbers. For unused
12054 hash table slots, the corresponding entry in the parallel table will be 0.
12055
12056 The pool of section numbers begins immediately following the hash table
12057 (at offset 16 + 12 * M from the beginning of the section). The pool of
12058 section numbers consists of an array of 32-bit words (using the byte order
12059 of the application binary). Each item in the array is indexed starting
12060 from 0. The hash table entry provides the index of the first section
12061 number in the set. Additional section numbers in the set follow, and the
12062 set is terminated by a 0 entry (section number 0 is not used in ELF).
12063
12064 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12065 section must be the first entry in the set, and the .debug_abbrev.dwo must
12066 be the second entry. Other members of the set may follow in any order.
12067
12068 ---
12069
12070 DWP Version 2:
12071
12072 DWP Version 2 combines all the .debug_info, etc. sections into one,
12073 and the entries in the index tables are now offsets into these sections.
12074 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12075 section.
12076
12077 Index Section Contents:
12078 Header
12079 Hash Table of Signatures dwp_hash_table.hash_table
12080 Parallel Table of Indices dwp_hash_table.unit_table
12081 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12082 Table of Section Sizes dwp_hash_table.v2.sizes
12083
12084 The index section header consists of:
12085
12086 V, 32 bit version number
12087 L, 32 bit number of columns in the table of section offsets
12088 N, 32 bit number of compilation units or type units in the index
12089 M, 32 bit number of slots in the hash table
12090
12091 Numbers are recorded using the byte order of the application binary.
12092
12093 The hash table has the same format as version 1.
12094 The parallel table of indices has the same format as version 1,
12095 except that the entries are origin-1 indices into the table of sections
12096 offsets and the table of section sizes.
12097
12098 The table of offsets begins immediately following the parallel table
12099 (at offset 16 + 12 * M from the beginning of the section). The table is
12100 a two-dimensional array of 32-bit words (using the byte order of the
12101 application binary), with L columns and N+1 rows, in row-major order.
12102 Each row in the array is indexed starting from 0. The first row provides
12103 a key to the remaining rows: each column in this row provides an identifier
12104 for a debug section, and the offsets in the same column of subsequent rows
12105 refer to that section. The section identifiers are:
12106
12107 DW_SECT_INFO 1 .debug_info.dwo
12108 DW_SECT_TYPES 2 .debug_types.dwo
12109 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12110 DW_SECT_LINE 4 .debug_line.dwo
12111 DW_SECT_LOC 5 .debug_loc.dwo
12112 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12113 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12114 DW_SECT_MACRO 8 .debug_macro.dwo
12115
12116 The offsets provided by the CU and TU index sections are the base offsets
12117 for the contributions made by each CU or TU to the corresponding section
12118 in the package file. Each CU and TU header contains an abbrev_offset
12119 field, used to find the abbreviations table for that CU or TU within the
12120 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12121 be interpreted as relative to the base offset given in the index section.
12122 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12123 should be interpreted as relative to the base offset for .debug_line.dwo,
12124 and offsets into other debug sections obtained from DWARF attributes should
12125 also be interpreted as relative to the corresponding base offset.
12126
12127 The table of sizes begins immediately following the table of offsets.
12128 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12129 with L columns and N rows, in row-major order. Each row in the array is
12130 indexed starting from 1 (row 0 is shared by the two tables).
12131
12132 ---
12133
12134 Hash table lookup is handled the same in version 1 and 2:
12135
12136 We assume that N and M will not exceed 2^32 - 1.
12137 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12138
12139 Given a 64-bit compilation unit signature or a type signature S, an entry
12140 in the hash table is located as follows:
12141
12142 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12143 the low-order k bits all set to 1.
12144
12145 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12146
12147 3) If the hash table entry at index H matches the signature, use that
12148 entry. If the hash table entry at index H is unused (all zeroes),
12149 terminate the search: the signature is not present in the table.
12150
12151 4) Let H = (H + H') modulo M. Repeat at Step 3.
12152
12153 Because M > N and H' and M are relatively prime, the search is guaranteed
12154 to stop at an unused slot or find the match. */
12155
12156 /* Create a hash table to map DWO IDs to their CU/TU entry in
12157 .debug_{info,types}.dwo in DWP_FILE.
12158 Returns NULL if there isn't one.
12159 Note: This function processes DWP files only, not DWO files. */
12160
12161 static struct dwp_hash_table *
12162 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12163 struct dwp_file *dwp_file, int is_debug_types)
12164 {
12165 struct objfile *objfile = dwarf2_per_objfile->objfile;
12166 bfd *dbfd = dwp_file->dbfd;
12167 const gdb_byte *index_ptr, *index_end;
12168 struct dwarf2_section_info *index;
12169 uint32_t version, nr_columns, nr_units, nr_slots;
12170 struct dwp_hash_table *htab;
12171
12172 if (is_debug_types)
12173 index = &dwp_file->sections.tu_index;
12174 else
12175 index = &dwp_file->sections.cu_index;
12176
12177 if (dwarf2_section_empty_p (index))
12178 return NULL;
12179 dwarf2_read_section (objfile, index);
12180
12181 index_ptr = index->buffer;
12182 index_end = index_ptr + index->size;
12183
12184 version = read_4_bytes (dbfd, index_ptr);
12185 index_ptr += 4;
12186 if (version == 2)
12187 nr_columns = read_4_bytes (dbfd, index_ptr);
12188 else
12189 nr_columns = 0;
12190 index_ptr += 4;
12191 nr_units = read_4_bytes (dbfd, index_ptr);
12192 index_ptr += 4;
12193 nr_slots = read_4_bytes (dbfd, index_ptr);
12194 index_ptr += 4;
12195
12196 if (version != 1 && version != 2)
12197 {
12198 error (_("Dwarf Error: unsupported DWP file version (%s)"
12199 " [in module %s]"),
12200 pulongest (version), dwp_file->name);
12201 }
12202 if (nr_slots != (nr_slots & -nr_slots))
12203 {
12204 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12205 " is not power of 2 [in module %s]"),
12206 pulongest (nr_slots), dwp_file->name);
12207 }
12208
12209 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12210 htab->version = version;
12211 htab->nr_columns = nr_columns;
12212 htab->nr_units = nr_units;
12213 htab->nr_slots = nr_slots;
12214 htab->hash_table = index_ptr;
12215 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12216
12217 /* Exit early if the table is empty. */
12218 if (nr_slots == 0 || nr_units == 0
12219 || (version == 2 && nr_columns == 0))
12220 {
12221 /* All must be zero. */
12222 if (nr_slots != 0 || nr_units != 0
12223 || (version == 2 && nr_columns != 0))
12224 {
12225 complaint (&symfile_complaints,
12226 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12227 " all zero [in modules %s]"),
12228 dwp_file->name);
12229 }
12230 return htab;
12231 }
12232
12233 if (version == 1)
12234 {
12235 htab->section_pool.v1.indices =
12236 htab->unit_table + sizeof (uint32_t) * nr_slots;
12237 /* It's harder to decide whether the section is too small in v1.
12238 V1 is deprecated anyway so we punt. */
12239 }
12240 else
12241 {
12242 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12243 int *ids = htab->section_pool.v2.section_ids;
12244 /* Reverse map for error checking. */
12245 int ids_seen[DW_SECT_MAX + 1];
12246 int i;
12247
12248 if (nr_columns < 2)
12249 {
12250 error (_("Dwarf Error: bad DWP hash table, too few columns"
12251 " in section table [in module %s]"),
12252 dwp_file->name);
12253 }
12254 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12255 {
12256 error (_("Dwarf Error: bad DWP hash table, too many columns"
12257 " in section table [in module %s]"),
12258 dwp_file->name);
12259 }
12260 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12261 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12262 for (i = 0; i < nr_columns; ++i)
12263 {
12264 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12265
12266 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12267 {
12268 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12269 " in section table [in module %s]"),
12270 id, dwp_file->name);
12271 }
12272 if (ids_seen[id] != -1)
12273 {
12274 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12275 " id %d in section table [in module %s]"),
12276 id, dwp_file->name);
12277 }
12278 ids_seen[id] = i;
12279 ids[i] = id;
12280 }
12281 /* Must have exactly one info or types section. */
12282 if (((ids_seen[DW_SECT_INFO] != -1)
12283 + (ids_seen[DW_SECT_TYPES] != -1))
12284 != 1)
12285 {
12286 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12287 " DWO info/types section [in module %s]"),
12288 dwp_file->name);
12289 }
12290 /* Must have an abbrev section. */
12291 if (ids_seen[DW_SECT_ABBREV] == -1)
12292 {
12293 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12294 " section [in module %s]"),
12295 dwp_file->name);
12296 }
12297 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12298 htab->section_pool.v2.sizes =
12299 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12300 * nr_units * nr_columns);
12301 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12302 * nr_units * nr_columns))
12303 > index_end)
12304 {
12305 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12306 " [in module %s]"),
12307 dwp_file->name);
12308 }
12309 }
12310
12311 return htab;
12312 }
12313
12314 /* Update SECTIONS with the data from SECTP.
12315
12316 This function is like the other "locate" section routines that are
12317 passed to bfd_map_over_sections, but in this context the sections to
12318 read comes from the DWP V1 hash table, not the full ELF section table.
12319
12320 The result is non-zero for success, or zero if an error was found. */
12321
12322 static int
12323 locate_v1_virtual_dwo_sections (asection *sectp,
12324 struct virtual_v1_dwo_sections *sections)
12325 {
12326 const struct dwop_section_names *names = &dwop_section_names;
12327
12328 if (section_is_p (sectp->name, &names->abbrev_dwo))
12329 {
12330 /* There can be only one. */
12331 if (sections->abbrev.s.section != NULL)
12332 return 0;
12333 sections->abbrev.s.section = sectp;
12334 sections->abbrev.size = bfd_get_section_size (sectp);
12335 }
12336 else if (section_is_p (sectp->name, &names->info_dwo)
12337 || section_is_p (sectp->name, &names->types_dwo))
12338 {
12339 /* There can be only one. */
12340 if (sections->info_or_types.s.section != NULL)
12341 return 0;
12342 sections->info_or_types.s.section = sectp;
12343 sections->info_or_types.size = bfd_get_section_size (sectp);
12344 }
12345 else if (section_is_p (sectp->name, &names->line_dwo))
12346 {
12347 /* There can be only one. */
12348 if (sections->line.s.section != NULL)
12349 return 0;
12350 sections->line.s.section = sectp;
12351 sections->line.size = bfd_get_section_size (sectp);
12352 }
12353 else if (section_is_p (sectp->name, &names->loc_dwo))
12354 {
12355 /* There can be only one. */
12356 if (sections->loc.s.section != NULL)
12357 return 0;
12358 sections->loc.s.section = sectp;
12359 sections->loc.size = bfd_get_section_size (sectp);
12360 }
12361 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12362 {
12363 /* There can be only one. */
12364 if (sections->macinfo.s.section != NULL)
12365 return 0;
12366 sections->macinfo.s.section = sectp;
12367 sections->macinfo.size = bfd_get_section_size (sectp);
12368 }
12369 else if (section_is_p (sectp->name, &names->macro_dwo))
12370 {
12371 /* There can be only one. */
12372 if (sections->macro.s.section != NULL)
12373 return 0;
12374 sections->macro.s.section = sectp;
12375 sections->macro.size = bfd_get_section_size (sectp);
12376 }
12377 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12378 {
12379 /* There can be only one. */
12380 if (sections->str_offsets.s.section != NULL)
12381 return 0;
12382 sections->str_offsets.s.section = sectp;
12383 sections->str_offsets.size = bfd_get_section_size (sectp);
12384 }
12385 else
12386 {
12387 /* No other kind of section is valid. */
12388 return 0;
12389 }
12390
12391 return 1;
12392 }
12393
12394 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12395 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12396 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12397 This is for DWP version 1 files. */
12398
12399 static struct dwo_unit *
12400 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12401 struct dwp_file *dwp_file,
12402 uint32_t unit_index,
12403 const char *comp_dir,
12404 ULONGEST signature, int is_debug_types)
12405 {
12406 struct objfile *objfile = dwarf2_per_objfile->objfile;
12407 const struct dwp_hash_table *dwp_htab =
12408 is_debug_types ? dwp_file->tus : dwp_file->cus;
12409 bfd *dbfd = dwp_file->dbfd;
12410 const char *kind = is_debug_types ? "TU" : "CU";
12411 struct dwo_file *dwo_file;
12412 struct dwo_unit *dwo_unit;
12413 struct virtual_v1_dwo_sections sections;
12414 void **dwo_file_slot;
12415 int i;
12416
12417 gdb_assert (dwp_file->version == 1);
12418
12419 if (dwarf_read_debug)
12420 {
12421 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12422 kind,
12423 pulongest (unit_index), hex_string (signature),
12424 dwp_file->name);
12425 }
12426
12427 /* Fetch the sections of this DWO unit.
12428 Put a limit on the number of sections we look for so that bad data
12429 doesn't cause us to loop forever. */
12430
12431 #define MAX_NR_V1_DWO_SECTIONS \
12432 (1 /* .debug_info or .debug_types */ \
12433 + 1 /* .debug_abbrev */ \
12434 + 1 /* .debug_line */ \
12435 + 1 /* .debug_loc */ \
12436 + 1 /* .debug_str_offsets */ \
12437 + 1 /* .debug_macro or .debug_macinfo */ \
12438 + 1 /* trailing zero */)
12439
12440 memset (&sections, 0, sizeof (sections));
12441
12442 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12443 {
12444 asection *sectp;
12445 uint32_t section_nr =
12446 read_4_bytes (dbfd,
12447 dwp_htab->section_pool.v1.indices
12448 + (unit_index + i) * sizeof (uint32_t));
12449
12450 if (section_nr == 0)
12451 break;
12452 if (section_nr >= dwp_file->num_sections)
12453 {
12454 error (_("Dwarf Error: bad DWP hash table, section number too large"
12455 " [in module %s]"),
12456 dwp_file->name);
12457 }
12458
12459 sectp = dwp_file->elf_sections[section_nr];
12460 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12461 {
12462 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12463 " [in module %s]"),
12464 dwp_file->name);
12465 }
12466 }
12467
12468 if (i < 2
12469 || dwarf2_section_empty_p (&sections.info_or_types)
12470 || dwarf2_section_empty_p (&sections.abbrev))
12471 {
12472 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12473 " [in module %s]"),
12474 dwp_file->name);
12475 }
12476 if (i == MAX_NR_V1_DWO_SECTIONS)
12477 {
12478 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12479 " [in module %s]"),
12480 dwp_file->name);
12481 }
12482
12483 /* It's easier for the rest of the code if we fake a struct dwo_file and
12484 have dwo_unit "live" in that. At least for now.
12485
12486 The DWP file can be made up of a random collection of CUs and TUs.
12487 However, for each CU + set of TUs that came from the same original DWO
12488 file, we can combine them back into a virtual DWO file to save space
12489 (fewer struct dwo_file objects to allocate). Remember that for really
12490 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12491
12492 std::string virtual_dwo_name =
12493 string_printf ("virtual-dwo/%d-%d-%d-%d",
12494 get_section_id (&sections.abbrev),
12495 get_section_id (&sections.line),
12496 get_section_id (&sections.loc),
12497 get_section_id (&sections.str_offsets));
12498 /* Can we use an existing virtual DWO file? */
12499 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12500 virtual_dwo_name.c_str (),
12501 comp_dir);
12502 /* Create one if necessary. */
12503 if (*dwo_file_slot == NULL)
12504 {
12505 if (dwarf_read_debug)
12506 {
12507 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12508 virtual_dwo_name.c_str ());
12509 }
12510 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12511 dwo_file->dwo_name
12512 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12513 virtual_dwo_name.c_str (),
12514 virtual_dwo_name.size ());
12515 dwo_file->comp_dir = comp_dir;
12516 dwo_file->sections.abbrev = sections.abbrev;
12517 dwo_file->sections.line = sections.line;
12518 dwo_file->sections.loc = sections.loc;
12519 dwo_file->sections.macinfo = sections.macinfo;
12520 dwo_file->sections.macro = sections.macro;
12521 dwo_file->sections.str_offsets = sections.str_offsets;
12522 /* The "str" section is global to the entire DWP file. */
12523 dwo_file->sections.str = dwp_file->sections.str;
12524 /* The info or types section is assigned below to dwo_unit,
12525 there's no need to record it in dwo_file.
12526 Also, we can't simply record type sections in dwo_file because
12527 we record a pointer into the vector in dwo_unit. As we collect more
12528 types we'll grow the vector and eventually have to reallocate space
12529 for it, invalidating all copies of pointers into the previous
12530 contents. */
12531 *dwo_file_slot = dwo_file;
12532 }
12533 else
12534 {
12535 if (dwarf_read_debug)
12536 {
12537 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12538 virtual_dwo_name.c_str ());
12539 }
12540 dwo_file = (struct dwo_file *) *dwo_file_slot;
12541 }
12542
12543 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12544 dwo_unit->dwo_file = dwo_file;
12545 dwo_unit->signature = signature;
12546 dwo_unit->section =
12547 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12548 *dwo_unit->section = sections.info_or_types;
12549 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12550
12551 return dwo_unit;
12552 }
12553
12554 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12555 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12556 piece within that section used by a TU/CU, return a virtual section
12557 of just that piece. */
12558
12559 static struct dwarf2_section_info
12560 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12561 struct dwarf2_section_info *section,
12562 bfd_size_type offset, bfd_size_type size)
12563 {
12564 struct dwarf2_section_info result;
12565 asection *sectp;
12566
12567 gdb_assert (section != NULL);
12568 gdb_assert (!section->is_virtual);
12569
12570 memset (&result, 0, sizeof (result));
12571 result.s.containing_section = section;
12572 result.is_virtual = 1;
12573
12574 if (size == 0)
12575 return result;
12576
12577 sectp = get_section_bfd_section (section);
12578
12579 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12580 bounds of the real section. This is a pretty-rare event, so just
12581 flag an error (easier) instead of a warning and trying to cope. */
12582 if (sectp == NULL
12583 || offset + size > bfd_get_section_size (sectp))
12584 {
12585 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12586 " in section %s [in module %s]"),
12587 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12588 objfile_name (dwarf2_per_objfile->objfile));
12589 }
12590
12591 result.virtual_offset = offset;
12592 result.size = size;
12593 return result;
12594 }
12595
12596 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12597 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12598 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12599 This is for DWP version 2 files. */
12600
12601 static struct dwo_unit *
12602 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12603 struct dwp_file *dwp_file,
12604 uint32_t unit_index,
12605 const char *comp_dir,
12606 ULONGEST signature, int is_debug_types)
12607 {
12608 struct objfile *objfile = dwarf2_per_objfile->objfile;
12609 const struct dwp_hash_table *dwp_htab =
12610 is_debug_types ? dwp_file->tus : dwp_file->cus;
12611 bfd *dbfd = dwp_file->dbfd;
12612 const char *kind = is_debug_types ? "TU" : "CU";
12613 struct dwo_file *dwo_file;
12614 struct dwo_unit *dwo_unit;
12615 struct virtual_v2_dwo_sections sections;
12616 void **dwo_file_slot;
12617 int i;
12618
12619 gdb_assert (dwp_file->version == 2);
12620
12621 if (dwarf_read_debug)
12622 {
12623 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12624 kind,
12625 pulongest (unit_index), hex_string (signature),
12626 dwp_file->name);
12627 }
12628
12629 /* Fetch the section offsets of this DWO unit. */
12630
12631 memset (&sections, 0, sizeof (sections));
12632
12633 for (i = 0; i < dwp_htab->nr_columns; ++i)
12634 {
12635 uint32_t offset = read_4_bytes (dbfd,
12636 dwp_htab->section_pool.v2.offsets
12637 + (((unit_index - 1) * dwp_htab->nr_columns
12638 + i)
12639 * sizeof (uint32_t)));
12640 uint32_t size = read_4_bytes (dbfd,
12641 dwp_htab->section_pool.v2.sizes
12642 + (((unit_index - 1) * dwp_htab->nr_columns
12643 + i)
12644 * sizeof (uint32_t)));
12645
12646 switch (dwp_htab->section_pool.v2.section_ids[i])
12647 {
12648 case DW_SECT_INFO:
12649 case DW_SECT_TYPES:
12650 sections.info_or_types_offset = offset;
12651 sections.info_or_types_size = size;
12652 break;
12653 case DW_SECT_ABBREV:
12654 sections.abbrev_offset = offset;
12655 sections.abbrev_size = size;
12656 break;
12657 case DW_SECT_LINE:
12658 sections.line_offset = offset;
12659 sections.line_size = size;
12660 break;
12661 case DW_SECT_LOC:
12662 sections.loc_offset = offset;
12663 sections.loc_size = size;
12664 break;
12665 case DW_SECT_STR_OFFSETS:
12666 sections.str_offsets_offset = offset;
12667 sections.str_offsets_size = size;
12668 break;
12669 case DW_SECT_MACINFO:
12670 sections.macinfo_offset = offset;
12671 sections.macinfo_size = size;
12672 break;
12673 case DW_SECT_MACRO:
12674 sections.macro_offset = offset;
12675 sections.macro_size = size;
12676 break;
12677 }
12678 }
12679
12680 /* It's easier for the rest of the code if we fake a struct dwo_file and
12681 have dwo_unit "live" in that. At least for now.
12682
12683 The DWP file can be made up of a random collection of CUs and TUs.
12684 However, for each CU + set of TUs that came from the same original DWO
12685 file, we can combine them back into a virtual DWO file to save space
12686 (fewer struct dwo_file objects to allocate). Remember that for really
12687 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12688
12689 std::string virtual_dwo_name =
12690 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12691 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12692 (long) (sections.line_size ? sections.line_offset : 0),
12693 (long) (sections.loc_size ? sections.loc_offset : 0),
12694 (long) (sections.str_offsets_size
12695 ? sections.str_offsets_offset : 0));
12696 /* Can we use an existing virtual DWO file? */
12697 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12698 virtual_dwo_name.c_str (),
12699 comp_dir);
12700 /* Create one if necessary. */
12701 if (*dwo_file_slot == NULL)
12702 {
12703 if (dwarf_read_debug)
12704 {
12705 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12706 virtual_dwo_name.c_str ());
12707 }
12708 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12709 dwo_file->dwo_name
12710 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12711 virtual_dwo_name.c_str (),
12712 virtual_dwo_name.size ());
12713 dwo_file->comp_dir = comp_dir;
12714 dwo_file->sections.abbrev =
12715 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12716 sections.abbrev_offset, sections.abbrev_size);
12717 dwo_file->sections.line =
12718 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12719 sections.line_offset, sections.line_size);
12720 dwo_file->sections.loc =
12721 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12722 sections.loc_offset, sections.loc_size);
12723 dwo_file->sections.macinfo =
12724 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12725 sections.macinfo_offset, sections.macinfo_size);
12726 dwo_file->sections.macro =
12727 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12728 sections.macro_offset, sections.macro_size);
12729 dwo_file->sections.str_offsets =
12730 create_dwp_v2_section (dwarf2_per_objfile,
12731 &dwp_file->sections.str_offsets,
12732 sections.str_offsets_offset,
12733 sections.str_offsets_size);
12734 /* The "str" section is global to the entire DWP file. */
12735 dwo_file->sections.str = dwp_file->sections.str;
12736 /* The info or types section is assigned below to dwo_unit,
12737 there's no need to record it in dwo_file.
12738 Also, we can't simply record type sections in dwo_file because
12739 we record a pointer into the vector in dwo_unit. As we collect more
12740 types we'll grow the vector and eventually have to reallocate space
12741 for it, invalidating all copies of pointers into the previous
12742 contents. */
12743 *dwo_file_slot = dwo_file;
12744 }
12745 else
12746 {
12747 if (dwarf_read_debug)
12748 {
12749 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12750 virtual_dwo_name.c_str ());
12751 }
12752 dwo_file = (struct dwo_file *) *dwo_file_slot;
12753 }
12754
12755 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12756 dwo_unit->dwo_file = dwo_file;
12757 dwo_unit->signature = signature;
12758 dwo_unit->section =
12759 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12760 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12761 is_debug_types
12762 ? &dwp_file->sections.types
12763 : &dwp_file->sections.info,
12764 sections.info_or_types_offset,
12765 sections.info_or_types_size);
12766 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12767
12768 return dwo_unit;
12769 }
12770
12771 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12772 Returns NULL if the signature isn't found. */
12773
12774 static struct dwo_unit *
12775 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12776 struct dwp_file *dwp_file, const char *comp_dir,
12777 ULONGEST signature, int is_debug_types)
12778 {
12779 const struct dwp_hash_table *dwp_htab =
12780 is_debug_types ? dwp_file->tus : dwp_file->cus;
12781 bfd *dbfd = dwp_file->dbfd;
12782 uint32_t mask = dwp_htab->nr_slots - 1;
12783 uint32_t hash = signature & mask;
12784 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12785 unsigned int i;
12786 void **slot;
12787 struct dwo_unit find_dwo_cu;
12788
12789 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12790 find_dwo_cu.signature = signature;
12791 slot = htab_find_slot (is_debug_types
12792 ? dwp_file->loaded_tus
12793 : dwp_file->loaded_cus,
12794 &find_dwo_cu, INSERT);
12795
12796 if (*slot != NULL)
12797 return (struct dwo_unit *) *slot;
12798
12799 /* Use a for loop so that we don't loop forever on bad debug info. */
12800 for (i = 0; i < dwp_htab->nr_slots; ++i)
12801 {
12802 ULONGEST signature_in_table;
12803
12804 signature_in_table =
12805 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12806 if (signature_in_table == signature)
12807 {
12808 uint32_t unit_index =
12809 read_4_bytes (dbfd,
12810 dwp_htab->unit_table + hash * sizeof (uint32_t));
12811
12812 if (dwp_file->version == 1)
12813 {
12814 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12815 dwp_file, unit_index,
12816 comp_dir, signature,
12817 is_debug_types);
12818 }
12819 else
12820 {
12821 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12822 dwp_file, unit_index,
12823 comp_dir, signature,
12824 is_debug_types);
12825 }
12826 return (struct dwo_unit *) *slot;
12827 }
12828 if (signature_in_table == 0)
12829 return NULL;
12830 hash = (hash + hash2) & mask;
12831 }
12832
12833 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12834 " [in module %s]"),
12835 dwp_file->name);
12836 }
12837
12838 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12839 Open the file specified by FILE_NAME and hand it off to BFD for
12840 preliminary analysis. Return a newly initialized bfd *, which
12841 includes a canonicalized copy of FILE_NAME.
12842 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12843 SEARCH_CWD is true if the current directory is to be searched.
12844 It will be searched before debug-file-directory.
12845 If successful, the file is added to the bfd include table of the
12846 objfile's bfd (see gdb_bfd_record_inclusion).
12847 If unable to find/open the file, return NULL.
12848 NOTE: This function is derived from symfile_bfd_open. */
12849
12850 static gdb_bfd_ref_ptr
12851 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12852 const char *file_name, int is_dwp, int search_cwd)
12853 {
12854 int desc, flags;
12855 char *absolute_name;
12856 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12857 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12858 to debug_file_directory. */
12859 char *search_path;
12860 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12861
12862 if (search_cwd)
12863 {
12864 if (*debug_file_directory != '\0')
12865 search_path = concat (".", dirname_separator_string,
12866 debug_file_directory, (char *) NULL);
12867 else
12868 search_path = xstrdup (".");
12869 }
12870 else
12871 search_path = xstrdup (debug_file_directory);
12872
12873 flags = OPF_RETURN_REALPATH;
12874 if (is_dwp)
12875 flags |= OPF_SEARCH_IN_PATH;
12876 desc = openp (search_path, flags, file_name,
12877 O_RDONLY | O_BINARY, &absolute_name);
12878 xfree (search_path);
12879 if (desc < 0)
12880 return NULL;
12881
12882 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
12883 xfree (absolute_name);
12884 if (sym_bfd == NULL)
12885 return NULL;
12886 bfd_set_cacheable (sym_bfd.get (), 1);
12887
12888 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12889 return NULL;
12890
12891 /* Success. Record the bfd as having been included by the objfile's bfd.
12892 This is important because things like demangled_names_hash lives in the
12893 objfile's per_bfd space and may have references to things like symbol
12894 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12895 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12896
12897 return sym_bfd;
12898 }
12899
12900 /* Try to open DWO file FILE_NAME.
12901 COMP_DIR is the DW_AT_comp_dir attribute.
12902 The result is the bfd handle of the file.
12903 If there is a problem finding or opening the file, return NULL.
12904 Upon success, the canonicalized path of the file is stored in the bfd,
12905 same as symfile_bfd_open. */
12906
12907 static gdb_bfd_ref_ptr
12908 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12909 const char *file_name, const char *comp_dir)
12910 {
12911 if (IS_ABSOLUTE_PATH (file_name))
12912 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12913 0 /*is_dwp*/, 0 /*search_cwd*/);
12914
12915 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12916
12917 if (comp_dir != NULL)
12918 {
12919 char *path_to_try = concat (comp_dir, SLASH_STRING,
12920 file_name, (char *) NULL);
12921
12922 /* NOTE: If comp_dir is a relative path, this will also try the
12923 search path, which seems useful. */
12924 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12925 path_to_try,
12926 0 /*is_dwp*/,
12927 1 /*search_cwd*/));
12928 xfree (path_to_try);
12929 if (abfd != NULL)
12930 return abfd;
12931 }
12932
12933 /* That didn't work, try debug-file-directory, which, despite its name,
12934 is a list of paths. */
12935
12936 if (*debug_file_directory == '\0')
12937 return NULL;
12938
12939 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12940 0 /*is_dwp*/, 1 /*search_cwd*/);
12941 }
12942
12943 /* This function is mapped across the sections and remembers the offset and
12944 size of each of the DWO debugging sections we are interested in. */
12945
12946 static void
12947 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12948 {
12949 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12950 const struct dwop_section_names *names = &dwop_section_names;
12951
12952 if (section_is_p (sectp->name, &names->abbrev_dwo))
12953 {
12954 dwo_sections->abbrev.s.section = sectp;
12955 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12956 }
12957 else if (section_is_p (sectp->name, &names->info_dwo))
12958 {
12959 dwo_sections->info.s.section = sectp;
12960 dwo_sections->info.size = bfd_get_section_size (sectp);
12961 }
12962 else if (section_is_p (sectp->name, &names->line_dwo))
12963 {
12964 dwo_sections->line.s.section = sectp;
12965 dwo_sections->line.size = bfd_get_section_size (sectp);
12966 }
12967 else if (section_is_p (sectp->name, &names->loc_dwo))
12968 {
12969 dwo_sections->loc.s.section = sectp;
12970 dwo_sections->loc.size = bfd_get_section_size (sectp);
12971 }
12972 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12973 {
12974 dwo_sections->macinfo.s.section = sectp;
12975 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12976 }
12977 else if (section_is_p (sectp->name, &names->macro_dwo))
12978 {
12979 dwo_sections->macro.s.section = sectp;
12980 dwo_sections->macro.size = bfd_get_section_size (sectp);
12981 }
12982 else if (section_is_p (sectp->name, &names->str_dwo))
12983 {
12984 dwo_sections->str.s.section = sectp;
12985 dwo_sections->str.size = bfd_get_section_size (sectp);
12986 }
12987 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12988 {
12989 dwo_sections->str_offsets.s.section = sectp;
12990 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12991 }
12992 else if (section_is_p (sectp->name, &names->types_dwo))
12993 {
12994 struct dwarf2_section_info type_section;
12995
12996 memset (&type_section, 0, sizeof (type_section));
12997 type_section.s.section = sectp;
12998 type_section.size = bfd_get_section_size (sectp);
12999 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
13000 &type_section);
13001 }
13002 }
13003
13004 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13005 by PER_CU. This is for the non-DWP case.
13006 The result is NULL if DWO_NAME can't be found. */
13007
13008 static struct dwo_file *
13009 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13010 const char *dwo_name, const char *comp_dir)
13011 {
13012 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13013 struct objfile *objfile = dwarf2_per_objfile->objfile;
13014 struct dwo_file *dwo_file;
13015 struct cleanup *cleanups;
13016
13017 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
13018 if (dbfd == NULL)
13019 {
13020 if (dwarf_read_debug)
13021 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13022 return NULL;
13023 }
13024 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
13025 dwo_file->dwo_name = dwo_name;
13026 dwo_file->comp_dir = comp_dir;
13027 dwo_file->dbfd = dbfd.release ();
13028
13029 free_dwo_file_cleanup_data *cleanup_data = XNEW (free_dwo_file_cleanup_data);
13030 cleanup_data->dwo_file = dwo_file;
13031 cleanup_data->dwarf2_per_objfile = dwarf2_per_objfile;
13032
13033 cleanups = make_cleanup (free_dwo_file_cleanup, cleanup_data);
13034
13035 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13036 &dwo_file->sections);
13037
13038 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13039 dwo_file->cus);
13040
13041 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file,
13042 dwo_file->sections.types, dwo_file->tus);
13043
13044 discard_cleanups (cleanups);
13045
13046 if (dwarf_read_debug)
13047 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13048
13049 return dwo_file;
13050 }
13051
13052 /* This function is mapped across the sections and remembers the offset and
13053 size of each of the DWP debugging sections common to version 1 and 2 that
13054 we are interested in. */
13055
13056 static void
13057 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13058 void *dwp_file_ptr)
13059 {
13060 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13061 const struct dwop_section_names *names = &dwop_section_names;
13062 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13063
13064 /* Record the ELF section number for later lookup: this is what the
13065 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13066 gdb_assert (elf_section_nr < dwp_file->num_sections);
13067 dwp_file->elf_sections[elf_section_nr] = sectp;
13068
13069 /* Look for specific sections that we need. */
13070 if (section_is_p (sectp->name, &names->str_dwo))
13071 {
13072 dwp_file->sections.str.s.section = sectp;
13073 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13074 }
13075 else if (section_is_p (sectp->name, &names->cu_index))
13076 {
13077 dwp_file->sections.cu_index.s.section = sectp;
13078 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13079 }
13080 else if (section_is_p (sectp->name, &names->tu_index))
13081 {
13082 dwp_file->sections.tu_index.s.section = sectp;
13083 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13084 }
13085 }
13086
13087 /* This function is mapped across the sections and remembers the offset and
13088 size of each of the DWP version 2 debugging sections that we are interested
13089 in. This is split into a separate function because we don't know if we
13090 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13091
13092 static void
13093 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13094 {
13095 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13096 const struct dwop_section_names *names = &dwop_section_names;
13097 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13098
13099 /* Record the ELF section number for later lookup: this is what the
13100 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13101 gdb_assert (elf_section_nr < dwp_file->num_sections);
13102 dwp_file->elf_sections[elf_section_nr] = sectp;
13103
13104 /* Look for specific sections that we need. */
13105 if (section_is_p (sectp->name, &names->abbrev_dwo))
13106 {
13107 dwp_file->sections.abbrev.s.section = sectp;
13108 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13109 }
13110 else if (section_is_p (sectp->name, &names->info_dwo))
13111 {
13112 dwp_file->sections.info.s.section = sectp;
13113 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13114 }
13115 else if (section_is_p (sectp->name, &names->line_dwo))
13116 {
13117 dwp_file->sections.line.s.section = sectp;
13118 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13119 }
13120 else if (section_is_p (sectp->name, &names->loc_dwo))
13121 {
13122 dwp_file->sections.loc.s.section = sectp;
13123 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13124 }
13125 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13126 {
13127 dwp_file->sections.macinfo.s.section = sectp;
13128 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13129 }
13130 else if (section_is_p (sectp->name, &names->macro_dwo))
13131 {
13132 dwp_file->sections.macro.s.section = sectp;
13133 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13134 }
13135 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13136 {
13137 dwp_file->sections.str_offsets.s.section = sectp;
13138 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13139 }
13140 else if (section_is_p (sectp->name, &names->types_dwo))
13141 {
13142 dwp_file->sections.types.s.section = sectp;
13143 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13144 }
13145 }
13146
13147 /* Hash function for dwp_file loaded CUs/TUs. */
13148
13149 static hashval_t
13150 hash_dwp_loaded_cutus (const void *item)
13151 {
13152 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13153
13154 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13155 return dwo_unit->signature;
13156 }
13157
13158 /* Equality function for dwp_file loaded CUs/TUs. */
13159
13160 static int
13161 eq_dwp_loaded_cutus (const void *a, const void *b)
13162 {
13163 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13164 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13165
13166 return dua->signature == dub->signature;
13167 }
13168
13169 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13170
13171 static htab_t
13172 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13173 {
13174 return htab_create_alloc_ex (3,
13175 hash_dwp_loaded_cutus,
13176 eq_dwp_loaded_cutus,
13177 NULL,
13178 &objfile->objfile_obstack,
13179 hashtab_obstack_allocate,
13180 dummy_obstack_deallocate);
13181 }
13182
13183 /* Try to open DWP file FILE_NAME.
13184 The result is the bfd handle of the file.
13185 If there is a problem finding or opening the file, return NULL.
13186 Upon success, the canonicalized path of the file is stored in the bfd,
13187 same as symfile_bfd_open. */
13188
13189 static gdb_bfd_ref_ptr
13190 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13191 const char *file_name)
13192 {
13193 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13194 1 /*is_dwp*/,
13195 1 /*search_cwd*/));
13196 if (abfd != NULL)
13197 return abfd;
13198
13199 /* Work around upstream bug 15652.
13200 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13201 [Whether that's a "bug" is debatable, but it is getting in our way.]
13202 We have no real idea where the dwp file is, because gdb's realpath-ing
13203 of the executable's path may have discarded the needed info.
13204 [IWBN if the dwp file name was recorded in the executable, akin to
13205 .gnu_debuglink, but that doesn't exist yet.]
13206 Strip the directory from FILE_NAME and search again. */
13207 if (*debug_file_directory != '\0')
13208 {
13209 /* Don't implicitly search the current directory here.
13210 If the user wants to search "." to handle this case,
13211 it must be added to debug-file-directory. */
13212 return try_open_dwop_file (dwarf2_per_objfile,
13213 lbasename (file_name), 1 /*is_dwp*/,
13214 0 /*search_cwd*/);
13215 }
13216
13217 return NULL;
13218 }
13219
13220 /* Initialize the use of the DWP file for the current objfile.
13221 By convention the name of the DWP file is ${objfile}.dwp.
13222 The result is NULL if it can't be found. */
13223
13224 static struct dwp_file *
13225 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13226 {
13227 struct objfile *objfile = dwarf2_per_objfile->objfile;
13228 struct dwp_file *dwp_file;
13229
13230 /* Try to find first .dwp for the binary file before any symbolic links
13231 resolving. */
13232
13233 /* If the objfile is a debug file, find the name of the real binary
13234 file and get the name of dwp file from there. */
13235 std::string dwp_name;
13236 if (objfile->separate_debug_objfile_backlink != NULL)
13237 {
13238 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13239 const char *backlink_basename = lbasename (backlink->original_name);
13240
13241 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13242 }
13243 else
13244 dwp_name = objfile->original_name;
13245
13246 dwp_name += ".dwp";
13247
13248 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13249 if (dbfd == NULL
13250 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13251 {
13252 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13253 dwp_name = objfile_name (objfile);
13254 dwp_name += ".dwp";
13255 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13256 }
13257
13258 if (dbfd == NULL)
13259 {
13260 if (dwarf_read_debug)
13261 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13262 return NULL;
13263 }
13264 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
13265 dwp_file->name = bfd_get_filename (dbfd.get ());
13266 dwp_file->dbfd = dbfd.release ();
13267
13268 /* +1: section 0 is unused */
13269 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13270 dwp_file->elf_sections =
13271 OBSTACK_CALLOC (&objfile->objfile_obstack,
13272 dwp_file->num_sections, asection *);
13273
13274 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13275 dwp_file);
13276
13277 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
13278
13279 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
13280
13281 /* The DWP file version is stored in the hash table. Oh well. */
13282 if (dwp_file->cus && dwp_file->tus
13283 && dwp_file->cus->version != dwp_file->tus->version)
13284 {
13285 /* Technically speaking, we should try to limp along, but this is
13286 pretty bizarre. We use pulongest here because that's the established
13287 portability solution (e.g, we cannot use %u for uint32_t). */
13288 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13289 " TU version %s [in DWP file %s]"),
13290 pulongest (dwp_file->cus->version),
13291 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13292 }
13293
13294 if (dwp_file->cus)
13295 dwp_file->version = dwp_file->cus->version;
13296 else if (dwp_file->tus)
13297 dwp_file->version = dwp_file->tus->version;
13298 else
13299 dwp_file->version = 2;
13300
13301 if (dwp_file->version == 2)
13302 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13303 dwp_file);
13304
13305 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13306 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13307
13308 if (dwarf_read_debug)
13309 {
13310 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13311 fprintf_unfiltered (gdb_stdlog,
13312 " %s CUs, %s TUs\n",
13313 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13314 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13315 }
13316
13317 return dwp_file;
13318 }
13319
13320 /* Wrapper around open_and_init_dwp_file, only open it once. */
13321
13322 static struct dwp_file *
13323 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13324 {
13325 if (! dwarf2_per_objfile->dwp_checked)
13326 {
13327 dwarf2_per_objfile->dwp_file
13328 = open_and_init_dwp_file (dwarf2_per_objfile);
13329 dwarf2_per_objfile->dwp_checked = 1;
13330 }
13331 return dwarf2_per_objfile->dwp_file;
13332 }
13333
13334 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13335 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13336 or in the DWP file for the objfile, referenced by THIS_UNIT.
13337 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13338 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13339
13340 This is called, for example, when wanting to read a variable with a
13341 complex location. Therefore we don't want to do file i/o for every call.
13342 Therefore we don't want to look for a DWO file on every call.
13343 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13344 then we check if we've already seen DWO_NAME, and only THEN do we check
13345 for a DWO file.
13346
13347 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13348 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13349
13350 static struct dwo_unit *
13351 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13352 const char *dwo_name, const char *comp_dir,
13353 ULONGEST signature, int is_debug_types)
13354 {
13355 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13356 struct objfile *objfile = dwarf2_per_objfile->objfile;
13357 const char *kind = is_debug_types ? "TU" : "CU";
13358 void **dwo_file_slot;
13359 struct dwo_file *dwo_file;
13360 struct dwp_file *dwp_file;
13361
13362 /* First see if there's a DWP file.
13363 If we have a DWP file but didn't find the DWO inside it, don't
13364 look for the original DWO file. It makes gdb behave differently
13365 depending on whether one is debugging in the build tree. */
13366
13367 dwp_file = get_dwp_file (dwarf2_per_objfile);
13368 if (dwp_file != NULL)
13369 {
13370 const struct dwp_hash_table *dwp_htab =
13371 is_debug_types ? dwp_file->tus : dwp_file->cus;
13372
13373 if (dwp_htab != NULL)
13374 {
13375 struct dwo_unit *dwo_cutu =
13376 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13377 signature, is_debug_types);
13378
13379 if (dwo_cutu != NULL)
13380 {
13381 if (dwarf_read_debug)
13382 {
13383 fprintf_unfiltered (gdb_stdlog,
13384 "Virtual DWO %s %s found: @%s\n",
13385 kind, hex_string (signature),
13386 host_address_to_string (dwo_cutu));
13387 }
13388 return dwo_cutu;
13389 }
13390 }
13391 }
13392 else
13393 {
13394 /* No DWP file, look for the DWO file. */
13395
13396 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13397 dwo_name, comp_dir);
13398 if (*dwo_file_slot == NULL)
13399 {
13400 /* Read in the file and build a table of the CUs/TUs it contains. */
13401 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13402 }
13403 /* NOTE: This will be NULL if unable to open the file. */
13404 dwo_file = (struct dwo_file *) *dwo_file_slot;
13405
13406 if (dwo_file != NULL)
13407 {
13408 struct dwo_unit *dwo_cutu = NULL;
13409
13410 if (is_debug_types && dwo_file->tus)
13411 {
13412 struct dwo_unit find_dwo_cutu;
13413
13414 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13415 find_dwo_cutu.signature = signature;
13416 dwo_cutu
13417 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13418 }
13419 else if (!is_debug_types && dwo_file->cus)
13420 {
13421 struct dwo_unit find_dwo_cutu;
13422
13423 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13424 find_dwo_cutu.signature = signature;
13425 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13426 &find_dwo_cutu);
13427 }
13428
13429 if (dwo_cutu != NULL)
13430 {
13431 if (dwarf_read_debug)
13432 {
13433 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13434 kind, dwo_name, hex_string (signature),
13435 host_address_to_string (dwo_cutu));
13436 }
13437 return dwo_cutu;
13438 }
13439 }
13440 }
13441
13442 /* We didn't find it. This could mean a dwo_id mismatch, or
13443 someone deleted the DWO/DWP file, or the search path isn't set up
13444 correctly to find the file. */
13445
13446 if (dwarf_read_debug)
13447 {
13448 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13449 kind, dwo_name, hex_string (signature));
13450 }
13451
13452 /* This is a warning and not a complaint because it can be caused by
13453 pilot error (e.g., user accidentally deleting the DWO). */
13454 {
13455 /* Print the name of the DWP file if we looked there, helps the user
13456 better diagnose the problem. */
13457 std::string dwp_text;
13458
13459 if (dwp_file != NULL)
13460 dwp_text = string_printf (" [in DWP file %s]",
13461 lbasename (dwp_file->name));
13462
13463 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
13464 " [in module %s]"),
13465 kind, dwo_name, hex_string (signature),
13466 dwp_text.c_str (),
13467 this_unit->is_debug_types ? "TU" : "CU",
13468 to_underlying (this_unit->sect_off), objfile_name (objfile));
13469 }
13470 return NULL;
13471 }
13472
13473 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13474 See lookup_dwo_cutu_unit for details. */
13475
13476 static struct dwo_unit *
13477 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13478 const char *dwo_name, const char *comp_dir,
13479 ULONGEST signature)
13480 {
13481 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13482 }
13483
13484 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13485 See lookup_dwo_cutu_unit for details. */
13486
13487 static struct dwo_unit *
13488 lookup_dwo_type_unit (struct signatured_type *this_tu,
13489 const char *dwo_name, const char *comp_dir)
13490 {
13491 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13492 }
13493
13494 /* Traversal function for queue_and_load_all_dwo_tus. */
13495
13496 static int
13497 queue_and_load_dwo_tu (void **slot, void *info)
13498 {
13499 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13500 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13501 ULONGEST signature = dwo_unit->signature;
13502 struct signatured_type *sig_type =
13503 lookup_dwo_signatured_type (per_cu->cu, signature);
13504
13505 if (sig_type != NULL)
13506 {
13507 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13508
13509 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13510 a real dependency of PER_CU on SIG_TYPE. That is detected later
13511 while processing PER_CU. */
13512 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13513 load_full_type_unit (sig_cu);
13514 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13515 }
13516
13517 return 1;
13518 }
13519
13520 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13521 The DWO may have the only definition of the type, though it may not be
13522 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13523 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13524
13525 static void
13526 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13527 {
13528 struct dwo_unit *dwo_unit;
13529 struct dwo_file *dwo_file;
13530
13531 gdb_assert (!per_cu->is_debug_types);
13532 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13533 gdb_assert (per_cu->cu != NULL);
13534
13535 dwo_unit = per_cu->cu->dwo_unit;
13536 gdb_assert (dwo_unit != NULL);
13537
13538 dwo_file = dwo_unit->dwo_file;
13539 if (dwo_file->tus != NULL)
13540 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13541 }
13542
13543 /* Free all resources associated with DWO_FILE.
13544 Close the DWO file and munmap the sections.
13545 All memory should be on the objfile obstack. */
13546
13547 static void
13548 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
13549 {
13550
13551 /* Note: dbfd is NULL for virtual DWO files. */
13552 gdb_bfd_unref (dwo_file->dbfd);
13553
13554 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13555 }
13556
13557 /* Wrapper for free_dwo_file for use in cleanups. */
13558
13559 static void
13560 free_dwo_file_cleanup (void *arg)
13561 {
13562 struct free_dwo_file_cleanup_data *data
13563 = (struct free_dwo_file_cleanup_data *) arg;
13564 struct objfile *objfile = data->dwarf2_per_objfile->objfile;
13565
13566 free_dwo_file (data->dwo_file, objfile);
13567
13568 xfree (data);
13569 }
13570
13571 /* Traversal function for free_dwo_files. */
13572
13573 static int
13574 free_dwo_file_from_slot (void **slot, void *info)
13575 {
13576 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13577 struct objfile *objfile = (struct objfile *) info;
13578
13579 free_dwo_file (dwo_file, objfile);
13580
13581 return 1;
13582 }
13583
13584 /* Free all resources associated with DWO_FILES. */
13585
13586 static void
13587 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13588 {
13589 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13590 }
13591 \f
13592 /* Read in various DIEs. */
13593
13594 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13595 Inherit only the children of the DW_AT_abstract_origin DIE not being
13596 already referenced by DW_AT_abstract_origin from the children of the
13597 current DIE. */
13598
13599 static void
13600 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13601 {
13602 struct die_info *child_die;
13603 sect_offset *offsetp;
13604 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13605 struct die_info *origin_die;
13606 /* Iterator of the ORIGIN_DIE children. */
13607 struct die_info *origin_child_die;
13608 struct attribute *attr;
13609 struct dwarf2_cu *origin_cu;
13610 struct pending **origin_previous_list_in_scope;
13611
13612 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13613 if (!attr)
13614 return;
13615
13616 /* Note that following die references may follow to a die in a
13617 different cu. */
13618
13619 origin_cu = cu;
13620 origin_die = follow_die_ref (die, attr, &origin_cu);
13621
13622 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13623 symbols in. */
13624 origin_previous_list_in_scope = origin_cu->list_in_scope;
13625 origin_cu->list_in_scope = cu->list_in_scope;
13626
13627 if (die->tag != origin_die->tag
13628 && !(die->tag == DW_TAG_inlined_subroutine
13629 && origin_die->tag == DW_TAG_subprogram))
13630 complaint (&symfile_complaints,
13631 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
13632 to_underlying (die->sect_off),
13633 to_underlying (origin_die->sect_off));
13634
13635 std::vector<sect_offset> offsets;
13636
13637 for (child_die = die->child;
13638 child_die && child_die->tag;
13639 child_die = sibling_die (child_die))
13640 {
13641 struct die_info *child_origin_die;
13642 struct dwarf2_cu *child_origin_cu;
13643
13644 /* We are trying to process concrete instance entries:
13645 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13646 it's not relevant to our analysis here. i.e. detecting DIEs that are
13647 present in the abstract instance but not referenced in the concrete
13648 one. */
13649 if (child_die->tag == DW_TAG_call_site
13650 || child_die->tag == DW_TAG_GNU_call_site)
13651 continue;
13652
13653 /* For each CHILD_DIE, find the corresponding child of
13654 ORIGIN_DIE. If there is more than one layer of
13655 DW_AT_abstract_origin, follow them all; there shouldn't be,
13656 but GCC versions at least through 4.4 generate this (GCC PR
13657 40573). */
13658 child_origin_die = child_die;
13659 child_origin_cu = cu;
13660 while (1)
13661 {
13662 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13663 child_origin_cu);
13664 if (attr == NULL)
13665 break;
13666 child_origin_die = follow_die_ref (child_origin_die, attr,
13667 &child_origin_cu);
13668 }
13669
13670 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13671 counterpart may exist. */
13672 if (child_origin_die != child_die)
13673 {
13674 if (child_die->tag != child_origin_die->tag
13675 && !(child_die->tag == DW_TAG_inlined_subroutine
13676 && child_origin_die->tag == DW_TAG_subprogram))
13677 complaint (&symfile_complaints,
13678 _("Child DIE 0x%x and its abstract origin 0x%x have "
13679 "different tags"),
13680 to_underlying (child_die->sect_off),
13681 to_underlying (child_origin_die->sect_off));
13682 if (child_origin_die->parent != origin_die)
13683 complaint (&symfile_complaints,
13684 _("Child DIE 0x%x and its abstract origin 0x%x have "
13685 "different parents"),
13686 to_underlying (child_die->sect_off),
13687 to_underlying (child_origin_die->sect_off));
13688 else
13689 offsets.push_back (child_origin_die->sect_off);
13690 }
13691 }
13692 std::sort (offsets.begin (), offsets.end ());
13693 sect_offset *offsets_end = offsets.data () + offsets.size ();
13694 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13695 if (offsetp[-1] == *offsetp)
13696 complaint (&symfile_complaints,
13697 _("Multiple children of DIE 0x%x refer "
13698 "to DIE 0x%x as their abstract origin"),
13699 to_underlying (die->sect_off), to_underlying (*offsetp));
13700
13701 offsetp = offsets.data ();
13702 origin_child_die = origin_die->child;
13703 while (origin_child_die && origin_child_die->tag)
13704 {
13705 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13706 while (offsetp < offsets_end
13707 && *offsetp < origin_child_die->sect_off)
13708 offsetp++;
13709 if (offsetp >= offsets_end
13710 || *offsetp > origin_child_die->sect_off)
13711 {
13712 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13713 Check whether we're already processing ORIGIN_CHILD_DIE.
13714 This can happen with mutually referenced abstract_origins.
13715 PR 16581. */
13716 if (!origin_child_die->in_process)
13717 process_die (origin_child_die, origin_cu);
13718 }
13719 origin_child_die = sibling_die (origin_child_die);
13720 }
13721 origin_cu->list_in_scope = origin_previous_list_in_scope;
13722 }
13723
13724 static void
13725 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13726 {
13727 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13728 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13729 struct context_stack *newobj;
13730 CORE_ADDR lowpc;
13731 CORE_ADDR highpc;
13732 struct die_info *child_die;
13733 struct attribute *attr, *call_line, *call_file;
13734 const char *name;
13735 CORE_ADDR baseaddr;
13736 struct block *block;
13737 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13738 std::vector<struct symbol *> template_args;
13739 struct template_symbol *templ_func = NULL;
13740
13741 if (inlined_func)
13742 {
13743 /* If we do not have call site information, we can't show the
13744 caller of this inlined function. That's too confusing, so
13745 only use the scope for local variables. */
13746 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13747 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13748 if (call_line == NULL || call_file == NULL)
13749 {
13750 read_lexical_block_scope (die, cu);
13751 return;
13752 }
13753 }
13754
13755 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13756
13757 name = dwarf2_name (die, cu);
13758
13759 /* Ignore functions with missing or empty names. These are actually
13760 illegal according to the DWARF standard. */
13761 if (name == NULL)
13762 {
13763 complaint (&symfile_complaints,
13764 _("missing name for subprogram DIE at %d"),
13765 to_underlying (die->sect_off));
13766 return;
13767 }
13768
13769 /* Ignore functions with missing or invalid low and high pc attributes. */
13770 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13771 <= PC_BOUNDS_INVALID)
13772 {
13773 attr = dwarf2_attr (die, DW_AT_external, cu);
13774 if (!attr || !DW_UNSND (attr))
13775 complaint (&symfile_complaints,
13776 _("cannot get low and high bounds "
13777 "for subprogram DIE at %d"),
13778 to_underlying (die->sect_off));
13779 return;
13780 }
13781
13782 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13783 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13784
13785 /* If we have any template arguments, then we must allocate a
13786 different sort of symbol. */
13787 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13788 {
13789 if (child_die->tag == DW_TAG_template_type_param
13790 || child_die->tag == DW_TAG_template_value_param)
13791 {
13792 templ_func = allocate_template_symbol (objfile);
13793 templ_func->subclass = SYMBOL_TEMPLATE;
13794 break;
13795 }
13796 }
13797
13798 newobj = push_context (0, lowpc);
13799 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13800 (struct symbol *) templ_func);
13801
13802 /* If there is a location expression for DW_AT_frame_base, record
13803 it. */
13804 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13805 if (attr)
13806 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13807
13808 /* If there is a location for the static link, record it. */
13809 newobj->static_link = NULL;
13810 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13811 if (attr)
13812 {
13813 newobj->static_link
13814 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13815 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13816 }
13817
13818 cu->list_in_scope = &local_symbols;
13819
13820 if (die->child != NULL)
13821 {
13822 child_die = die->child;
13823 while (child_die && child_die->tag)
13824 {
13825 if (child_die->tag == DW_TAG_template_type_param
13826 || child_die->tag == DW_TAG_template_value_param)
13827 {
13828 struct symbol *arg = new_symbol (child_die, NULL, cu);
13829
13830 if (arg != NULL)
13831 template_args.push_back (arg);
13832 }
13833 else
13834 process_die (child_die, cu);
13835 child_die = sibling_die (child_die);
13836 }
13837 }
13838
13839 inherit_abstract_dies (die, cu);
13840
13841 /* If we have a DW_AT_specification, we might need to import using
13842 directives from the context of the specification DIE. See the
13843 comment in determine_prefix. */
13844 if (cu->language == language_cplus
13845 && dwarf2_attr (die, DW_AT_specification, cu))
13846 {
13847 struct dwarf2_cu *spec_cu = cu;
13848 struct die_info *spec_die = die_specification (die, &spec_cu);
13849
13850 while (spec_die)
13851 {
13852 child_die = spec_die->child;
13853 while (child_die && child_die->tag)
13854 {
13855 if (child_die->tag == DW_TAG_imported_module)
13856 process_die (child_die, spec_cu);
13857 child_die = sibling_die (child_die);
13858 }
13859
13860 /* In some cases, GCC generates specification DIEs that
13861 themselves contain DW_AT_specification attributes. */
13862 spec_die = die_specification (spec_die, &spec_cu);
13863 }
13864 }
13865
13866 newobj = pop_context ();
13867 /* Make a block for the local symbols within. */
13868 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
13869 newobj->static_link, lowpc, highpc);
13870
13871 /* For C++, set the block's scope. */
13872 if ((cu->language == language_cplus
13873 || cu->language == language_fortran
13874 || cu->language == language_d
13875 || cu->language == language_rust)
13876 && cu->processing_has_namespace_info)
13877 block_set_scope (block, determine_prefix (die, cu),
13878 &objfile->objfile_obstack);
13879
13880 /* If we have address ranges, record them. */
13881 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13882
13883 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
13884
13885 /* Attach template arguments to function. */
13886 if (!template_args.empty ())
13887 {
13888 gdb_assert (templ_func != NULL);
13889
13890 templ_func->n_template_arguments = template_args.size ();
13891 templ_func->template_arguments
13892 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13893 templ_func->n_template_arguments);
13894 memcpy (templ_func->template_arguments,
13895 template_args.data (),
13896 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13897 }
13898
13899 /* In C++, we can have functions nested inside functions (e.g., when
13900 a function declares a class that has methods). This means that
13901 when we finish processing a function scope, we may need to go
13902 back to building a containing block's symbol lists. */
13903 local_symbols = newobj->locals;
13904 local_using_directives = newobj->local_using_directives;
13905
13906 /* If we've finished processing a top-level function, subsequent
13907 symbols go in the file symbol list. */
13908 if (outermost_context_p ())
13909 cu->list_in_scope = &file_symbols;
13910 }
13911
13912 /* Process all the DIES contained within a lexical block scope. Start
13913 a new scope, process the dies, and then close the scope. */
13914
13915 static void
13916 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13917 {
13918 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13919 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13920 struct context_stack *newobj;
13921 CORE_ADDR lowpc, highpc;
13922 struct die_info *child_die;
13923 CORE_ADDR baseaddr;
13924
13925 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13926
13927 /* Ignore blocks with missing or invalid low and high pc attributes. */
13928 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13929 as multiple lexical blocks? Handling children in a sane way would
13930 be nasty. Might be easier to properly extend generic blocks to
13931 describe ranges. */
13932 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13933 {
13934 case PC_BOUNDS_NOT_PRESENT:
13935 /* DW_TAG_lexical_block has no attributes, process its children as if
13936 there was no wrapping by that DW_TAG_lexical_block.
13937 GCC does no longer produces such DWARF since GCC r224161. */
13938 for (child_die = die->child;
13939 child_die != NULL && child_die->tag;
13940 child_die = sibling_die (child_die))
13941 process_die (child_die, cu);
13942 return;
13943 case PC_BOUNDS_INVALID:
13944 return;
13945 }
13946 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13947 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13948
13949 push_context (0, lowpc);
13950 if (die->child != NULL)
13951 {
13952 child_die = die->child;
13953 while (child_die && child_die->tag)
13954 {
13955 process_die (child_die, cu);
13956 child_die = sibling_die (child_die);
13957 }
13958 }
13959 inherit_abstract_dies (die, cu);
13960 newobj = pop_context ();
13961
13962 if (local_symbols != NULL || local_using_directives != NULL)
13963 {
13964 struct block *block
13965 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
13966 newobj->start_addr, highpc);
13967
13968 /* Note that recording ranges after traversing children, as we
13969 do here, means that recording a parent's ranges entails
13970 walking across all its children's ranges as they appear in
13971 the address map, which is quadratic behavior.
13972
13973 It would be nicer to record the parent's ranges before
13974 traversing its children, simply overriding whatever you find
13975 there. But since we don't even decide whether to create a
13976 block until after we've traversed its children, that's hard
13977 to do. */
13978 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13979 }
13980 local_symbols = newobj->locals;
13981 local_using_directives = newobj->local_using_directives;
13982 }
13983
13984 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13985
13986 static void
13987 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13988 {
13989 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13990 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13991 CORE_ADDR pc, baseaddr;
13992 struct attribute *attr;
13993 struct call_site *call_site, call_site_local;
13994 void **slot;
13995 int nparams;
13996 struct die_info *child_die;
13997
13998 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13999
14000 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
14001 if (attr == NULL)
14002 {
14003 /* This was a pre-DWARF-5 GNU extension alias
14004 for DW_AT_call_return_pc. */
14005 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14006 }
14007 if (!attr)
14008 {
14009 complaint (&symfile_complaints,
14010 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
14011 "DIE 0x%x [in module %s]"),
14012 to_underlying (die->sect_off), objfile_name (objfile));
14013 return;
14014 }
14015 pc = attr_value_as_address (attr) + baseaddr;
14016 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
14017
14018 if (cu->call_site_htab == NULL)
14019 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14020 NULL, &objfile->objfile_obstack,
14021 hashtab_obstack_allocate, NULL);
14022 call_site_local.pc = pc;
14023 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14024 if (*slot != NULL)
14025 {
14026 complaint (&symfile_complaints,
14027 _("Duplicate PC %s for DW_TAG_call_site "
14028 "DIE 0x%x [in module %s]"),
14029 paddress (gdbarch, pc), to_underlying (die->sect_off),
14030 objfile_name (objfile));
14031 return;
14032 }
14033
14034 /* Count parameters at the caller. */
14035
14036 nparams = 0;
14037 for (child_die = die->child; child_die && child_die->tag;
14038 child_die = sibling_die (child_die))
14039 {
14040 if (child_die->tag != DW_TAG_call_site_parameter
14041 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14042 {
14043 complaint (&symfile_complaints,
14044 _("Tag %d is not DW_TAG_call_site_parameter in "
14045 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14046 child_die->tag, to_underlying (child_die->sect_off),
14047 objfile_name (objfile));
14048 continue;
14049 }
14050
14051 nparams++;
14052 }
14053
14054 call_site
14055 = ((struct call_site *)
14056 obstack_alloc (&objfile->objfile_obstack,
14057 sizeof (*call_site)
14058 + (sizeof (*call_site->parameter) * (nparams - 1))));
14059 *slot = call_site;
14060 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14061 call_site->pc = pc;
14062
14063 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14064 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14065 {
14066 struct die_info *func_die;
14067
14068 /* Skip also over DW_TAG_inlined_subroutine. */
14069 for (func_die = die->parent;
14070 func_die && func_die->tag != DW_TAG_subprogram
14071 && func_die->tag != DW_TAG_subroutine_type;
14072 func_die = func_die->parent);
14073
14074 /* DW_AT_call_all_calls is a superset
14075 of DW_AT_call_all_tail_calls. */
14076 if (func_die
14077 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14078 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14079 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14080 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14081 {
14082 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14083 not complete. But keep CALL_SITE for look ups via call_site_htab,
14084 both the initial caller containing the real return address PC and
14085 the final callee containing the current PC of a chain of tail
14086 calls do not need to have the tail call list complete. But any
14087 function candidate for a virtual tail call frame searched via
14088 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14089 determined unambiguously. */
14090 }
14091 else
14092 {
14093 struct type *func_type = NULL;
14094
14095 if (func_die)
14096 func_type = get_die_type (func_die, cu);
14097 if (func_type != NULL)
14098 {
14099 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14100
14101 /* Enlist this call site to the function. */
14102 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14103 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14104 }
14105 else
14106 complaint (&symfile_complaints,
14107 _("Cannot find function owning DW_TAG_call_site "
14108 "DIE 0x%x [in module %s]"),
14109 to_underlying (die->sect_off), objfile_name (objfile));
14110 }
14111 }
14112
14113 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14114 if (attr == NULL)
14115 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14116 if (attr == NULL)
14117 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14118 if (attr == NULL)
14119 {
14120 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14121 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14122 }
14123 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14124 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14125 /* Keep NULL DWARF_BLOCK. */;
14126 else if (attr_form_is_block (attr))
14127 {
14128 struct dwarf2_locexpr_baton *dlbaton;
14129
14130 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14131 dlbaton->data = DW_BLOCK (attr)->data;
14132 dlbaton->size = DW_BLOCK (attr)->size;
14133 dlbaton->per_cu = cu->per_cu;
14134
14135 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14136 }
14137 else if (attr_form_is_ref (attr))
14138 {
14139 struct dwarf2_cu *target_cu = cu;
14140 struct die_info *target_die;
14141
14142 target_die = follow_die_ref (die, attr, &target_cu);
14143 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14144 if (die_is_declaration (target_die, target_cu))
14145 {
14146 const char *target_physname;
14147
14148 /* Prefer the mangled name; otherwise compute the demangled one. */
14149 target_physname = dw2_linkage_name (target_die, target_cu);
14150 if (target_physname == NULL)
14151 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14152 if (target_physname == NULL)
14153 complaint (&symfile_complaints,
14154 _("DW_AT_call_target target DIE has invalid "
14155 "physname, for referencing DIE 0x%x [in module %s]"),
14156 to_underlying (die->sect_off), objfile_name (objfile));
14157 else
14158 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14159 }
14160 else
14161 {
14162 CORE_ADDR lowpc;
14163
14164 /* DW_AT_entry_pc should be preferred. */
14165 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14166 <= PC_BOUNDS_INVALID)
14167 complaint (&symfile_complaints,
14168 _("DW_AT_call_target target DIE has invalid "
14169 "low pc, for referencing DIE 0x%x [in module %s]"),
14170 to_underlying (die->sect_off), objfile_name (objfile));
14171 else
14172 {
14173 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14174 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14175 }
14176 }
14177 }
14178 else
14179 complaint (&symfile_complaints,
14180 _("DW_TAG_call_site DW_AT_call_target is neither "
14181 "block nor reference, for DIE 0x%x [in module %s]"),
14182 to_underlying (die->sect_off), objfile_name (objfile));
14183
14184 call_site->per_cu = cu->per_cu;
14185
14186 for (child_die = die->child;
14187 child_die && child_die->tag;
14188 child_die = sibling_die (child_die))
14189 {
14190 struct call_site_parameter *parameter;
14191 struct attribute *loc, *origin;
14192
14193 if (child_die->tag != DW_TAG_call_site_parameter
14194 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14195 {
14196 /* Already printed the complaint above. */
14197 continue;
14198 }
14199
14200 gdb_assert (call_site->parameter_count < nparams);
14201 parameter = &call_site->parameter[call_site->parameter_count];
14202
14203 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14204 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14205 register is contained in DW_AT_call_value. */
14206
14207 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14208 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14209 if (origin == NULL)
14210 {
14211 /* This was a pre-DWARF-5 GNU extension alias
14212 for DW_AT_call_parameter. */
14213 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14214 }
14215 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14216 {
14217 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14218
14219 sect_offset sect_off
14220 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14221 if (!offset_in_cu_p (&cu->header, sect_off))
14222 {
14223 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14224 binding can be done only inside one CU. Such referenced DIE
14225 therefore cannot be even moved to DW_TAG_partial_unit. */
14226 complaint (&symfile_complaints,
14227 _("DW_AT_call_parameter offset is not in CU for "
14228 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14229 to_underlying (child_die->sect_off),
14230 objfile_name (objfile));
14231 continue;
14232 }
14233 parameter->u.param_cu_off
14234 = (cu_offset) (sect_off - cu->header.sect_off);
14235 }
14236 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14237 {
14238 complaint (&symfile_complaints,
14239 _("No DW_FORM_block* DW_AT_location for "
14240 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14241 to_underlying (child_die->sect_off), objfile_name (objfile));
14242 continue;
14243 }
14244 else
14245 {
14246 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14247 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14248 if (parameter->u.dwarf_reg != -1)
14249 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14250 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14251 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14252 &parameter->u.fb_offset))
14253 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14254 else
14255 {
14256 complaint (&symfile_complaints,
14257 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14258 "for DW_FORM_block* DW_AT_location is supported for "
14259 "DW_TAG_call_site child DIE 0x%x "
14260 "[in module %s]"),
14261 to_underlying (child_die->sect_off),
14262 objfile_name (objfile));
14263 continue;
14264 }
14265 }
14266
14267 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14268 if (attr == NULL)
14269 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14270 if (!attr_form_is_block (attr))
14271 {
14272 complaint (&symfile_complaints,
14273 _("No DW_FORM_block* DW_AT_call_value for "
14274 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14275 to_underlying (child_die->sect_off),
14276 objfile_name (objfile));
14277 continue;
14278 }
14279 parameter->value = DW_BLOCK (attr)->data;
14280 parameter->value_size = DW_BLOCK (attr)->size;
14281
14282 /* Parameters are not pre-cleared by memset above. */
14283 parameter->data_value = NULL;
14284 parameter->data_value_size = 0;
14285 call_site->parameter_count++;
14286
14287 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14288 if (attr == NULL)
14289 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14290 if (attr)
14291 {
14292 if (!attr_form_is_block (attr))
14293 complaint (&symfile_complaints,
14294 _("No DW_FORM_block* DW_AT_call_data_value for "
14295 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
14296 to_underlying (child_die->sect_off),
14297 objfile_name (objfile));
14298 else
14299 {
14300 parameter->data_value = DW_BLOCK (attr)->data;
14301 parameter->data_value_size = DW_BLOCK (attr)->size;
14302 }
14303 }
14304 }
14305 }
14306
14307 /* Helper function for read_variable. If DIE represents a virtual
14308 table, then return the type of the concrete object that is
14309 associated with the virtual table. Otherwise, return NULL. */
14310
14311 static struct type *
14312 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14313 {
14314 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14315 if (attr == NULL)
14316 return NULL;
14317
14318 /* Find the type DIE. */
14319 struct die_info *type_die = NULL;
14320 struct dwarf2_cu *type_cu = cu;
14321
14322 if (attr_form_is_ref (attr))
14323 type_die = follow_die_ref (die, attr, &type_cu);
14324 if (type_die == NULL)
14325 return NULL;
14326
14327 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14328 return NULL;
14329 return die_containing_type (type_die, type_cu);
14330 }
14331
14332 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14333
14334 static void
14335 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14336 {
14337 struct rust_vtable_symbol *storage = NULL;
14338
14339 if (cu->language == language_rust)
14340 {
14341 struct type *containing_type = rust_containing_type (die, cu);
14342
14343 if (containing_type != NULL)
14344 {
14345 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14346
14347 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14348 struct rust_vtable_symbol);
14349 initialize_objfile_symbol (storage);
14350 storage->concrete_type = containing_type;
14351 storage->subclass = SYMBOL_RUST_VTABLE;
14352 }
14353 }
14354
14355 new_symbol (die, NULL, cu, storage);
14356 }
14357
14358 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14359 reading .debug_rnglists.
14360 Callback's type should be:
14361 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14362 Return true if the attributes are present and valid, otherwise,
14363 return false. */
14364
14365 template <typename Callback>
14366 static bool
14367 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14368 Callback &&callback)
14369 {
14370 struct dwarf2_per_objfile *dwarf2_per_objfile
14371 = cu->per_cu->dwarf2_per_objfile;
14372 struct objfile *objfile = dwarf2_per_objfile->objfile;
14373 bfd *obfd = objfile->obfd;
14374 /* Base address selection entry. */
14375 CORE_ADDR base;
14376 int found_base;
14377 const gdb_byte *buffer;
14378 CORE_ADDR baseaddr;
14379 bool overflow = false;
14380
14381 found_base = cu->base_known;
14382 base = cu->base_address;
14383
14384 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14385 if (offset >= dwarf2_per_objfile->rnglists.size)
14386 {
14387 complaint (&symfile_complaints,
14388 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14389 offset);
14390 return false;
14391 }
14392 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14393
14394 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14395
14396 while (1)
14397 {
14398 /* Initialize it due to a false compiler warning. */
14399 CORE_ADDR range_beginning = 0, range_end = 0;
14400 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14401 + dwarf2_per_objfile->rnglists.size);
14402 unsigned int bytes_read;
14403
14404 if (buffer == buf_end)
14405 {
14406 overflow = true;
14407 break;
14408 }
14409 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14410 switch (rlet)
14411 {
14412 case DW_RLE_end_of_list:
14413 break;
14414 case DW_RLE_base_address:
14415 if (buffer + cu->header.addr_size > buf_end)
14416 {
14417 overflow = true;
14418 break;
14419 }
14420 base = read_address (obfd, buffer, cu, &bytes_read);
14421 found_base = 1;
14422 buffer += bytes_read;
14423 break;
14424 case DW_RLE_start_length:
14425 if (buffer + cu->header.addr_size > buf_end)
14426 {
14427 overflow = true;
14428 break;
14429 }
14430 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14431 buffer += bytes_read;
14432 range_end = (range_beginning
14433 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14434 buffer += bytes_read;
14435 if (buffer > buf_end)
14436 {
14437 overflow = true;
14438 break;
14439 }
14440 break;
14441 case DW_RLE_offset_pair:
14442 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14443 buffer += bytes_read;
14444 if (buffer > buf_end)
14445 {
14446 overflow = true;
14447 break;
14448 }
14449 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14450 buffer += bytes_read;
14451 if (buffer > buf_end)
14452 {
14453 overflow = true;
14454 break;
14455 }
14456 break;
14457 case DW_RLE_start_end:
14458 if (buffer + 2 * cu->header.addr_size > buf_end)
14459 {
14460 overflow = true;
14461 break;
14462 }
14463 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14464 buffer += bytes_read;
14465 range_end = read_address (obfd, buffer, cu, &bytes_read);
14466 buffer += bytes_read;
14467 break;
14468 default:
14469 complaint (&symfile_complaints,
14470 _("Invalid .debug_rnglists data (no base address)"));
14471 return false;
14472 }
14473 if (rlet == DW_RLE_end_of_list || overflow)
14474 break;
14475 if (rlet == DW_RLE_base_address)
14476 continue;
14477
14478 if (!found_base)
14479 {
14480 /* We have no valid base address for the ranges
14481 data. */
14482 complaint (&symfile_complaints,
14483 _("Invalid .debug_rnglists data (no base address)"));
14484 return false;
14485 }
14486
14487 if (range_beginning > range_end)
14488 {
14489 /* Inverted range entries are invalid. */
14490 complaint (&symfile_complaints,
14491 _("Invalid .debug_rnglists data (inverted range)"));
14492 return false;
14493 }
14494
14495 /* Empty range entries have no effect. */
14496 if (range_beginning == range_end)
14497 continue;
14498
14499 range_beginning += base;
14500 range_end += base;
14501
14502 /* A not-uncommon case of bad debug info.
14503 Don't pollute the addrmap with bad data. */
14504 if (range_beginning + baseaddr == 0
14505 && !dwarf2_per_objfile->has_section_at_zero)
14506 {
14507 complaint (&symfile_complaints,
14508 _(".debug_rnglists entry has start address of zero"
14509 " [in module %s]"), objfile_name (objfile));
14510 continue;
14511 }
14512
14513 callback (range_beginning, range_end);
14514 }
14515
14516 if (overflow)
14517 {
14518 complaint (&symfile_complaints,
14519 _("Offset %d is not terminated "
14520 "for DW_AT_ranges attribute"),
14521 offset);
14522 return false;
14523 }
14524
14525 return true;
14526 }
14527
14528 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14529 Callback's type should be:
14530 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14531 Return 1 if the attributes are present and valid, otherwise, return 0. */
14532
14533 template <typename Callback>
14534 static int
14535 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14536 Callback &&callback)
14537 {
14538 struct dwarf2_per_objfile *dwarf2_per_objfile
14539 = cu->per_cu->dwarf2_per_objfile;
14540 struct objfile *objfile = dwarf2_per_objfile->objfile;
14541 struct comp_unit_head *cu_header = &cu->header;
14542 bfd *obfd = objfile->obfd;
14543 unsigned int addr_size = cu_header->addr_size;
14544 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14545 /* Base address selection entry. */
14546 CORE_ADDR base;
14547 int found_base;
14548 unsigned int dummy;
14549 const gdb_byte *buffer;
14550 CORE_ADDR baseaddr;
14551
14552 if (cu_header->version >= 5)
14553 return dwarf2_rnglists_process (offset, cu, callback);
14554
14555 found_base = cu->base_known;
14556 base = cu->base_address;
14557
14558 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14559 if (offset >= dwarf2_per_objfile->ranges.size)
14560 {
14561 complaint (&symfile_complaints,
14562 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14563 offset);
14564 return 0;
14565 }
14566 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14567
14568 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14569
14570 while (1)
14571 {
14572 CORE_ADDR range_beginning, range_end;
14573
14574 range_beginning = read_address (obfd, buffer, cu, &dummy);
14575 buffer += addr_size;
14576 range_end = read_address (obfd, buffer, cu, &dummy);
14577 buffer += addr_size;
14578 offset += 2 * addr_size;
14579
14580 /* An end of list marker is a pair of zero addresses. */
14581 if (range_beginning == 0 && range_end == 0)
14582 /* Found the end of list entry. */
14583 break;
14584
14585 /* Each base address selection entry is a pair of 2 values.
14586 The first is the largest possible address, the second is
14587 the base address. Check for a base address here. */
14588 if ((range_beginning & mask) == mask)
14589 {
14590 /* If we found the largest possible address, then we already
14591 have the base address in range_end. */
14592 base = range_end;
14593 found_base = 1;
14594 continue;
14595 }
14596
14597 if (!found_base)
14598 {
14599 /* We have no valid base address for the ranges
14600 data. */
14601 complaint (&symfile_complaints,
14602 _("Invalid .debug_ranges data (no base address)"));
14603 return 0;
14604 }
14605
14606 if (range_beginning > range_end)
14607 {
14608 /* Inverted range entries are invalid. */
14609 complaint (&symfile_complaints,
14610 _("Invalid .debug_ranges data (inverted range)"));
14611 return 0;
14612 }
14613
14614 /* Empty range entries have no effect. */
14615 if (range_beginning == range_end)
14616 continue;
14617
14618 range_beginning += base;
14619 range_end += base;
14620
14621 /* A not-uncommon case of bad debug info.
14622 Don't pollute the addrmap with bad data. */
14623 if (range_beginning + baseaddr == 0
14624 && !dwarf2_per_objfile->has_section_at_zero)
14625 {
14626 complaint (&symfile_complaints,
14627 _(".debug_ranges entry has start address of zero"
14628 " [in module %s]"), objfile_name (objfile));
14629 continue;
14630 }
14631
14632 callback (range_beginning, range_end);
14633 }
14634
14635 return 1;
14636 }
14637
14638 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14639 Return 1 if the attributes are present and valid, otherwise, return 0.
14640 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14641
14642 static int
14643 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14644 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14645 struct partial_symtab *ranges_pst)
14646 {
14647 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14648 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14649 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14650 SECT_OFF_TEXT (objfile));
14651 int low_set = 0;
14652 CORE_ADDR low = 0;
14653 CORE_ADDR high = 0;
14654 int retval;
14655
14656 retval = dwarf2_ranges_process (offset, cu,
14657 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14658 {
14659 if (ranges_pst != NULL)
14660 {
14661 CORE_ADDR lowpc;
14662 CORE_ADDR highpc;
14663
14664 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14665 range_beginning + baseaddr);
14666 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14667 range_end + baseaddr);
14668 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14669 ranges_pst);
14670 }
14671
14672 /* FIXME: This is recording everything as a low-high
14673 segment of consecutive addresses. We should have a
14674 data structure for discontiguous block ranges
14675 instead. */
14676 if (! low_set)
14677 {
14678 low = range_beginning;
14679 high = range_end;
14680 low_set = 1;
14681 }
14682 else
14683 {
14684 if (range_beginning < low)
14685 low = range_beginning;
14686 if (range_end > high)
14687 high = range_end;
14688 }
14689 });
14690 if (!retval)
14691 return 0;
14692
14693 if (! low_set)
14694 /* If the first entry is an end-of-list marker, the range
14695 describes an empty scope, i.e. no instructions. */
14696 return 0;
14697
14698 if (low_return)
14699 *low_return = low;
14700 if (high_return)
14701 *high_return = high;
14702 return 1;
14703 }
14704
14705 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14706 definition for the return value. *LOWPC and *HIGHPC are set iff
14707 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14708
14709 static enum pc_bounds_kind
14710 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14711 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14712 struct partial_symtab *pst)
14713 {
14714 struct dwarf2_per_objfile *dwarf2_per_objfile
14715 = cu->per_cu->dwarf2_per_objfile;
14716 struct attribute *attr;
14717 struct attribute *attr_high;
14718 CORE_ADDR low = 0;
14719 CORE_ADDR high = 0;
14720 enum pc_bounds_kind ret;
14721
14722 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14723 if (attr_high)
14724 {
14725 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14726 if (attr)
14727 {
14728 low = attr_value_as_address (attr);
14729 high = attr_value_as_address (attr_high);
14730 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14731 high += low;
14732 }
14733 else
14734 /* Found high w/o low attribute. */
14735 return PC_BOUNDS_INVALID;
14736
14737 /* Found consecutive range of addresses. */
14738 ret = PC_BOUNDS_HIGH_LOW;
14739 }
14740 else
14741 {
14742 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14743 if (attr != NULL)
14744 {
14745 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14746 We take advantage of the fact that DW_AT_ranges does not appear
14747 in DW_TAG_compile_unit of DWO files. */
14748 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14749 unsigned int ranges_offset = (DW_UNSND (attr)
14750 + (need_ranges_base
14751 ? cu->ranges_base
14752 : 0));
14753
14754 /* Value of the DW_AT_ranges attribute is the offset in the
14755 .debug_ranges section. */
14756 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14757 return PC_BOUNDS_INVALID;
14758 /* Found discontinuous range of addresses. */
14759 ret = PC_BOUNDS_RANGES;
14760 }
14761 else
14762 return PC_BOUNDS_NOT_PRESENT;
14763 }
14764
14765 /* read_partial_die has also the strict LOW < HIGH requirement. */
14766 if (high <= low)
14767 return PC_BOUNDS_INVALID;
14768
14769 /* When using the GNU linker, .gnu.linkonce. sections are used to
14770 eliminate duplicate copies of functions and vtables and such.
14771 The linker will arbitrarily choose one and discard the others.
14772 The AT_*_pc values for such functions refer to local labels in
14773 these sections. If the section from that file was discarded, the
14774 labels are not in the output, so the relocs get a value of 0.
14775 If this is a discarded function, mark the pc bounds as invalid,
14776 so that GDB will ignore it. */
14777 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14778 return PC_BOUNDS_INVALID;
14779
14780 *lowpc = low;
14781 if (highpc)
14782 *highpc = high;
14783 return ret;
14784 }
14785
14786 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14787 its low and high PC addresses. Do nothing if these addresses could not
14788 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14789 and HIGHPC to the high address if greater than HIGHPC. */
14790
14791 static void
14792 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14793 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14794 struct dwarf2_cu *cu)
14795 {
14796 CORE_ADDR low, high;
14797 struct die_info *child = die->child;
14798
14799 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14800 {
14801 *lowpc = std::min (*lowpc, low);
14802 *highpc = std::max (*highpc, high);
14803 }
14804
14805 /* If the language does not allow nested subprograms (either inside
14806 subprograms or lexical blocks), we're done. */
14807 if (cu->language != language_ada)
14808 return;
14809
14810 /* Check all the children of the given DIE. If it contains nested
14811 subprograms, then check their pc bounds. Likewise, we need to
14812 check lexical blocks as well, as they may also contain subprogram
14813 definitions. */
14814 while (child && child->tag)
14815 {
14816 if (child->tag == DW_TAG_subprogram
14817 || child->tag == DW_TAG_lexical_block)
14818 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14819 child = sibling_die (child);
14820 }
14821 }
14822
14823 /* Get the low and high pc's represented by the scope DIE, and store
14824 them in *LOWPC and *HIGHPC. If the correct values can't be
14825 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14826
14827 static void
14828 get_scope_pc_bounds (struct die_info *die,
14829 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14830 struct dwarf2_cu *cu)
14831 {
14832 CORE_ADDR best_low = (CORE_ADDR) -1;
14833 CORE_ADDR best_high = (CORE_ADDR) 0;
14834 CORE_ADDR current_low, current_high;
14835
14836 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14837 >= PC_BOUNDS_RANGES)
14838 {
14839 best_low = current_low;
14840 best_high = current_high;
14841 }
14842 else
14843 {
14844 struct die_info *child = die->child;
14845
14846 while (child && child->tag)
14847 {
14848 switch (child->tag) {
14849 case DW_TAG_subprogram:
14850 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14851 break;
14852 case DW_TAG_namespace:
14853 case DW_TAG_module:
14854 /* FIXME: carlton/2004-01-16: Should we do this for
14855 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14856 that current GCC's always emit the DIEs corresponding
14857 to definitions of methods of classes as children of a
14858 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14859 the DIEs giving the declarations, which could be
14860 anywhere). But I don't see any reason why the
14861 standards says that they have to be there. */
14862 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14863
14864 if (current_low != ((CORE_ADDR) -1))
14865 {
14866 best_low = std::min (best_low, current_low);
14867 best_high = std::max (best_high, current_high);
14868 }
14869 break;
14870 default:
14871 /* Ignore. */
14872 break;
14873 }
14874
14875 child = sibling_die (child);
14876 }
14877 }
14878
14879 *lowpc = best_low;
14880 *highpc = best_high;
14881 }
14882
14883 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14884 in DIE. */
14885
14886 static void
14887 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14888 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14889 {
14890 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14891 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14892 struct attribute *attr;
14893 struct attribute *attr_high;
14894
14895 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14896 if (attr_high)
14897 {
14898 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14899 if (attr)
14900 {
14901 CORE_ADDR low = attr_value_as_address (attr);
14902 CORE_ADDR high = attr_value_as_address (attr_high);
14903
14904 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14905 high += low;
14906
14907 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14908 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14909 record_block_range (block, low, high - 1);
14910 }
14911 }
14912
14913 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14914 if (attr)
14915 {
14916 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14917 We take advantage of the fact that DW_AT_ranges does not appear
14918 in DW_TAG_compile_unit of DWO files. */
14919 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14920
14921 /* The value of the DW_AT_ranges attribute is the offset of the
14922 address range list in the .debug_ranges section. */
14923 unsigned long offset = (DW_UNSND (attr)
14924 + (need_ranges_base ? cu->ranges_base : 0));
14925 const gdb_byte *buffer;
14926
14927 /* For some target architectures, but not others, the
14928 read_address function sign-extends the addresses it returns.
14929 To recognize base address selection entries, we need a
14930 mask. */
14931 unsigned int addr_size = cu->header.addr_size;
14932 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14933
14934 /* The base address, to which the next pair is relative. Note
14935 that this 'base' is a DWARF concept: most entries in a range
14936 list are relative, to reduce the number of relocs against the
14937 debugging information. This is separate from this function's
14938 'baseaddr' argument, which GDB uses to relocate debugging
14939 information from a shared library based on the address at
14940 which the library was loaded. */
14941 CORE_ADDR base = cu->base_address;
14942 int base_known = cu->base_known;
14943
14944 dwarf2_ranges_process (offset, cu,
14945 [&] (CORE_ADDR start, CORE_ADDR end)
14946 {
14947 start += baseaddr;
14948 end += baseaddr;
14949 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14950 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14951 record_block_range (block, start, end - 1);
14952 });
14953 }
14954 }
14955
14956 /* Check whether the producer field indicates either of GCC < 4.6, or the
14957 Intel C/C++ compiler, and cache the result in CU. */
14958
14959 static void
14960 check_producer (struct dwarf2_cu *cu)
14961 {
14962 int major, minor;
14963
14964 if (cu->producer == NULL)
14965 {
14966 /* For unknown compilers expect their behavior is DWARF version
14967 compliant.
14968
14969 GCC started to support .debug_types sections by -gdwarf-4 since
14970 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14971 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14972 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14973 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14974 }
14975 else if (producer_is_gcc (cu->producer, &major, &minor))
14976 {
14977 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14978 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14979 }
14980 else if (producer_is_icc (cu->producer, &major, &minor))
14981 cu->producer_is_icc_lt_14 = major < 14;
14982 else
14983 {
14984 /* For other non-GCC compilers, expect their behavior is DWARF version
14985 compliant. */
14986 }
14987
14988 cu->checked_producer = 1;
14989 }
14990
14991 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14992 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14993 during 4.6.0 experimental. */
14994
14995 static int
14996 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14997 {
14998 if (!cu->checked_producer)
14999 check_producer (cu);
15000
15001 return cu->producer_is_gxx_lt_4_6;
15002 }
15003
15004 /* Return the default accessibility type if it is not overriden by
15005 DW_AT_accessibility. */
15006
15007 static enum dwarf_access_attribute
15008 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15009 {
15010 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15011 {
15012 /* The default DWARF 2 accessibility for members is public, the default
15013 accessibility for inheritance is private. */
15014
15015 if (die->tag != DW_TAG_inheritance)
15016 return DW_ACCESS_public;
15017 else
15018 return DW_ACCESS_private;
15019 }
15020 else
15021 {
15022 /* DWARF 3+ defines the default accessibility a different way. The same
15023 rules apply now for DW_TAG_inheritance as for the members and it only
15024 depends on the container kind. */
15025
15026 if (die->parent->tag == DW_TAG_class_type)
15027 return DW_ACCESS_private;
15028 else
15029 return DW_ACCESS_public;
15030 }
15031 }
15032
15033 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15034 offset. If the attribute was not found return 0, otherwise return
15035 1. If it was found but could not properly be handled, set *OFFSET
15036 to 0. */
15037
15038 static int
15039 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15040 LONGEST *offset)
15041 {
15042 struct attribute *attr;
15043
15044 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15045 if (attr != NULL)
15046 {
15047 *offset = 0;
15048
15049 /* Note that we do not check for a section offset first here.
15050 This is because DW_AT_data_member_location is new in DWARF 4,
15051 so if we see it, we can assume that a constant form is really
15052 a constant and not a section offset. */
15053 if (attr_form_is_constant (attr))
15054 *offset = dwarf2_get_attr_constant_value (attr, 0);
15055 else if (attr_form_is_section_offset (attr))
15056 dwarf2_complex_location_expr_complaint ();
15057 else if (attr_form_is_block (attr))
15058 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15059 else
15060 dwarf2_complex_location_expr_complaint ();
15061
15062 return 1;
15063 }
15064
15065 return 0;
15066 }
15067
15068 /* Add an aggregate field to the field list. */
15069
15070 static void
15071 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15072 struct dwarf2_cu *cu)
15073 {
15074 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15075 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15076 struct nextfield *new_field;
15077 struct attribute *attr;
15078 struct field *fp;
15079 const char *fieldname = "";
15080
15081 /* Allocate a new field list entry and link it in. */
15082 new_field = XNEW (struct nextfield);
15083 make_cleanup (xfree, new_field);
15084 memset (new_field, 0, sizeof (struct nextfield));
15085
15086 if (die->tag == DW_TAG_inheritance)
15087 {
15088 new_field->next = fip->baseclasses;
15089 fip->baseclasses = new_field;
15090 }
15091 else
15092 {
15093 new_field->next = fip->fields;
15094 fip->fields = new_field;
15095 }
15096 fip->nfields++;
15097
15098 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15099 if (attr)
15100 new_field->accessibility = DW_UNSND (attr);
15101 else
15102 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15103 if (new_field->accessibility != DW_ACCESS_public)
15104 fip->non_public_fields = 1;
15105
15106 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15107 if (attr)
15108 new_field->virtuality = DW_UNSND (attr);
15109 else
15110 new_field->virtuality = DW_VIRTUALITY_none;
15111
15112 fp = &new_field->field;
15113
15114 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15115 {
15116 LONGEST offset;
15117
15118 /* Data member other than a C++ static data member. */
15119
15120 /* Get type of field. */
15121 fp->type = die_type (die, cu);
15122
15123 SET_FIELD_BITPOS (*fp, 0);
15124
15125 /* Get bit size of field (zero if none). */
15126 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15127 if (attr)
15128 {
15129 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15130 }
15131 else
15132 {
15133 FIELD_BITSIZE (*fp) = 0;
15134 }
15135
15136 /* Get bit offset of field. */
15137 if (handle_data_member_location (die, cu, &offset))
15138 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15139 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15140 if (attr)
15141 {
15142 if (gdbarch_bits_big_endian (gdbarch))
15143 {
15144 /* For big endian bits, the DW_AT_bit_offset gives the
15145 additional bit offset from the MSB of the containing
15146 anonymous object to the MSB of the field. We don't
15147 have to do anything special since we don't need to
15148 know the size of the anonymous object. */
15149 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15150 }
15151 else
15152 {
15153 /* For little endian bits, compute the bit offset to the
15154 MSB of the anonymous object, subtract off the number of
15155 bits from the MSB of the field to the MSB of the
15156 object, and then subtract off the number of bits of
15157 the field itself. The result is the bit offset of
15158 the LSB of the field. */
15159 int anonymous_size;
15160 int bit_offset = DW_UNSND (attr);
15161
15162 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15163 if (attr)
15164 {
15165 /* The size of the anonymous object containing
15166 the bit field is explicit, so use the
15167 indicated size (in bytes). */
15168 anonymous_size = DW_UNSND (attr);
15169 }
15170 else
15171 {
15172 /* The size of the anonymous object containing
15173 the bit field must be inferred from the type
15174 attribute of the data member containing the
15175 bit field. */
15176 anonymous_size = TYPE_LENGTH (fp->type);
15177 }
15178 SET_FIELD_BITPOS (*fp,
15179 (FIELD_BITPOS (*fp)
15180 + anonymous_size * bits_per_byte
15181 - bit_offset - FIELD_BITSIZE (*fp)));
15182 }
15183 }
15184 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15185 if (attr != NULL)
15186 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15187 + dwarf2_get_attr_constant_value (attr, 0)));
15188
15189 /* Get name of field. */
15190 fieldname = dwarf2_name (die, cu);
15191 if (fieldname == NULL)
15192 fieldname = "";
15193
15194 /* The name is already allocated along with this objfile, so we don't
15195 need to duplicate it for the type. */
15196 fp->name = fieldname;
15197
15198 /* Change accessibility for artificial fields (e.g. virtual table
15199 pointer or virtual base class pointer) to private. */
15200 if (dwarf2_attr (die, DW_AT_artificial, cu))
15201 {
15202 FIELD_ARTIFICIAL (*fp) = 1;
15203 new_field->accessibility = DW_ACCESS_private;
15204 fip->non_public_fields = 1;
15205 }
15206 }
15207 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15208 {
15209 /* C++ static member. */
15210
15211 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15212 is a declaration, but all versions of G++ as of this writing
15213 (so through at least 3.2.1) incorrectly generate
15214 DW_TAG_variable tags. */
15215
15216 const char *physname;
15217
15218 /* Get name of field. */
15219 fieldname = dwarf2_name (die, cu);
15220 if (fieldname == NULL)
15221 return;
15222
15223 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15224 if (attr
15225 /* Only create a symbol if this is an external value.
15226 new_symbol checks this and puts the value in the global symbol
15227 table, which we want. If it is not external, new_symbol
15228 will try to put the value in cu->list_in_scope which is wrong. */
15229 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15230 {
15231 /* A static const member, not much different than an enum as far as
15232 we're concerned, except that we can support more types. */
15233 new_symbol (die, NULL, cu);
15234 }
15235
15236 /* Get physical name. */
15237 physname = dwarf2_physname (fieldname, die, cu);
15238
15239 /* The name is already allocated along with this objfile, so we don't
15240 need to duplicate it for the type. */
15241 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15242 FIELD_TYPE (*fp) = die_type (die, cu);
15243 FIELD_NAME (*fp) = fieldname;
15244 }
15245 else if (die->tag == DW_TAG_inheritance)
15246 {
15247 LONGEST offset;
15248
15249 /* C++ base class field. */
15250 if (handle_data_member_location (die, cu, &offset))
15251 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15252 FIELD_BITSIZE (*fp) = 0;
15253 FIELD_TYPE (*fp) = die_type (die, cu);
15254 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15255 fip->nbaseclasses++;
15256 }
15257 }
15258
15259 /* Can the type given by DIE define another type? */
15260
15261 static bool
15262 type_can_define_types (const struct die_info *die)
15263 {
15264 switch (die->tag)
15265 {
15266 case DW_TAG_typedef:
15267 case DW_TAG_class_type:
15268 case DW_TAG_structure_type:
15269 case DW_TAG_union_type:
15270 case DW_TAG_enumeration_type:
15271 return true;
15272
15273 default:
15274 return false;
15275 }
15276 }
15277
15278 /* Add a type definition defined in the scope of the FIP's class. */
15279
15280 static void
15281 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15282 struct dwarf2_cu *cu)
15283 {
15284 struct decl_field_list *new_field;
15285 struct decl_field *fp;
15286
15287 /* Allocate a new field list entry and link it in. */
15288 new_field = XCNEW (struct decl_field_list);
15289 make_cleanup (xfree, new_field);
15290
15291 gdb_assert (type_can_define_types (die));
15292
15293 fp = &new_field->field;
15294
15295 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15296 fp->name = dwarf2_name (die, cu);
15297 fp->type = read_type_die (die, cu);
15298
15299 /* Save accessibility. */
15300 enum dwarf_access_attribute accessibility;
15301 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15302 if (attr != NULL)
15303 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15304 else
15305 accessibility = dwarf2_default_access_attribute (die, cu);
15306 switch (accessibility)
15307 {
15308 case DW_ACCESS_public:
15309 /* The assumed value if neither private nor protected. */
15310 break;
15311 case DW_ACCESS_private:
15312 fp->is_private = 1;
15313 break;
15314 case DW_ACCESS_protected:
15315 fp->is_protected = 1;
15316 break;
15317 default:
15318 complaint (&symfile_complaints,
15319 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15320 }
15321
15322 if (die->tag == DW_TAG_typedef)
15323 {
15324 new_field->next = fip->typedef_field_list;
15325 fip->typedef_field_list = new_field;
15326 fip->typedef_field_list_count++;
15327 }
15328 else
15329 {
15330 new_field->next = fip->nested_types_list;
15331 fip->nested_types_list = new_field;
15332 fip->nested_types_list_count++;
15333 }
15334 }
15335
15336 /* Create the vector of fields, and attach it to the type. */
15337
15338 static void
15339 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15340 struct dwarf2_cu *cu)
15341 {
15342 int nfields = fip->nfields;
15343
15344 /* Record the field count, allocate space for the array of fields,
15345 and create blank accessibility bitfields if necessary. */
15346 TYPE_NFIELDS (type) = nfields;
15347 TYPE_FIELDS (type) = (struct field *)
15348 TYPE_ALLOC (type, sizeof (struct field) * nfields);
15349 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
15350
15351 if (fip->non_public_fields && cu->language != language_ada)
15352 {
15353 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15354
15355 TYPE_FIELD_PRIVATE_BITS (type) =
15356 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15357 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15358
15359 TYPE_FIELD_PROTECTED_BITS (type) =
15360 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15361 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15362
15363 TYPE_FIELD_IGNORE_BITS (type) =
15364 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15365 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15366 }
15367
15368 /* If the type has baseclasses, allocate and clear a bit vector for
15369 TYPE_FIELD_VIRTUAL_BITS. */
15370 if (fip->nbaseclasses && cu->language != language_ada)
15371 {
15372 int num_bytes = B_BYTES (fip->nbaseclasses);
15373 unsigned char *pointer;
15374
15375 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15376 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15377 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15378 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
15379 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
15380 }
15381
15382 /* Copy the saved-up fields into the field vector. Start from the head of
15383 the list, adding to the tail of the field array, so that they end up in
15384 the same order in the array in which they were added to the list. */
15385 while (nfields-- > 0)
15386 {
15387 struct nextfield *fieldp;
15388
15389 if (fip->fields)
15390 {
15391 fieldp = fip->fields;
15392 fip->fields = fieldp->next;
15393 }
15394 else
15395 {
15396 fieldp = fip->baseclasses;
15397 fip->baseclasses = fieldp->next;
15398 }
15399
15400 TYPE_FIELD (type, nfields) = fieldp->field;
15401 switch (fieldp->accessibility)
15402 {
15403 case DW_ACCESS_private:
15404 if (cu->language != language_ada)
15405 SET_TYPE_FIELD_PRIVATE (type, nfields);
15406 break;
15407
15408 case DW_ACCESS_protected:
15409 if (cu->language != language_ada)
15410 SET_TYPE_FIELD_PROTECTED (type, nfields);
15411 break;
15412
15413 case DW_ACCESS_public:
15414 break;
15415
15416 default:
15417 /* Unknown accessibility. Complain and treat it as public. */
15418 {
15419 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15420 fieldp->accessibility);
15421 }
15422 break;
15423 }
15424 if (nfields < fip->nbaseclasses)
15425 {
15426 switch (fieldp->virtuality)
15427 {
15428 case DW_VIRTUALITY_virtual:
15429 case DW_VIRTUALITY_pure_virtual:
15430 if (cu->language == language_ada)
15431 error (_("unexpected virtuality in component of Ada type"));
15432 SET_TYPE_FIELD_VIRTUAL (type, nfields);
15433 break;
15434 }
15435 }
15436 }
15437 }
15438
15439 /* Return true if this member function is a constructor, false
15440 otherwise. */
15441
15442 static int
15443 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15444 {
15445 const char *fieldname;
15446 const char *type_name;
15447 int len;
15448
15449 if (die->parent == NULL)
15450 return 0;
15451
15452 if (die->parent->tag != DW_TAG_structure_type
15453 && die->parent->tag != DW_TAG_union_type
15454 && die->parent->tag != DW_TAG_class_type)
15455 return 0;
15456
15457 fieldname = dwarf2_name (die, cu);
15458 type_name = dwarf2_name (die->parent, cu);
15459 if (fieldname == NULL || type_name == NULL)
15460 return 0;
15461
15462 len = strlen (fieldname);
15463 return (strncmp (fieldname, type_name, len) == 0
15464 && (type_name[len] == '\0' || type_name[len] == '<'));
15465 }
15466
15467 /* Add a member function to the proper fieldlist. */
15468
15469 static void
15470 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15471 struct type *type, struct dwarf2_cu *cu)
15472 {
15473 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15474 struct attribute *attr;
15475 struct fnfieldlist *flp;
15476 int i;
15477 struct fn_field *fnp;
15478 const char *fieldname;
15479 struct nextfnfield *new_fnfield;
15480 struct type *this_type;
15481 enum dwarf_access_attribute accessibility;
15482
15483 if (cu->language == language_ada)
15484 error (_("unexpected member function in Ada type"));
15485
15486 /* Get name of member function. */
15487 fieldname = dwarf2_name (die, cu);
15488 if (fieldname == NULL)
15489 return;
15490
15491 /* Look up member function name in fieldlist. */
15492 for (i = 0; i < fip->nfnfields; i++)
15493 {
15494 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15495 break;
15496 }
15497
15498 /* Create new list element if necessary. */
15499 if (i < fip->nfnfields)
15500 flp = &fip->fnfieldlists[i];
15501 else
15502 {
15503 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
15504 {
15505 fip->fnfieldlists = (struct fnfieldlist *)
15506 xrealloc (fip->fnfieldlists,
15507 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
15508 * sizeof (struct fnfieldlist));
15509 if (fip->nfnfields == 0)
15510 make_cleanup (free_current_contents, &fip->fnfieldlists);
15511 }
15512 flp = &fip->fnfieldlists[fip->nfnfields];
15513 flp->name = fieldname;
15514 flp->length = 0;
15515 flp->head = NULL;
15516 i = fip->nfnfields++;
15517 }
15518
15519 /* Create a new member function field and chain it to the field list
15520 entry. */
15521 new_fnfield = XNEW (struct nextfnfield);
15522 make_cleanup (xfree, new_fnfield);
15523 memset (new_fnfield, 0, sizeof (struct nextfnfield));
15524 new_fnfield->next = flp->head;
15525 flp->head = new_fnfield;
15526 flp->length++;
15527
15528 /* Fill in the member function field info. */
15529 fnp = &new_fnfield->fnfield;
15530
15531 /* Delay processing of the physname until later. */
15532 if (cu->language == language_cplus)
15533 {
15534 add_to_method_list (type, i, flp->length - 1, fieldname,
15535 die, cu);
15536 }
15537 else
15538 {
15539 const char *physname = dwarf2_physname (fieldname, die, cu);
15540 fnp->physname = physname ? physname : "";
15541 }
15542
15543 fnp->type = alloc_type (objfile);
15544 this_type = read_type_die (die, cu);
15545 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15546 {
15547 int nparams = TYPE_NFIELDS (this_type);
15548
15549 /* TYPE is the domain of this method, and THIS_TYPE is the type
15550 of the method itself (TYPE_CODE_METHOD). */
15551 smash_to_method_type (fnp->type, type,
15552 TYPE_TARGET_TYPE (this_type),
15553 TYPE_FIELDS (this_type),
15554 TYPE_NFIELDS (this_type),
15555 TYPE_VARARGS (this_type));
15556
15557 /* Handle static member functions.
15558 Dwarf2 has no clean way to discern C++ static and non-static
15559 member functions. G++ helps GDB by marking the first
15560 parameter for non-static member functions (which is the this
15561 pointer) as artificial. We obtain this information from
15562 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15563 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15564 fnp->voffset = VOFFSET_STATIC;
15565 }
15566 else
15567 complaint (&symfile_complaints, _("member function type missing for '%s'"),
15568 dwarf2_full_name (fieldname, die, cu));
15569
15570 /* Get fcontext from DW_AT_containing_type if present. */
15571 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15572 fnp->fcontext = die_containing_type (die, cu);
15573
15574 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15575 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15576
15577 /* Get accessibility. */
15578 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15579 if (attr)
15580 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15581 else
15582 accessibility = dwarf2_default_access_attribute (die, cu);
15583 switch (accessibility)
15584 {
15585 case DW_ACCESS_private:
15586 fnp->is_private = 1;
15587 break;
15588 case DW_ACCESS_protected:
15589 fnp->is_protected = 1;
15590 break;
15591 }
15592
15593 /* Check for artificial methods. */
15594 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15595 if (attr && DW_UNSND (attr) != 0)
15596 fnp->is_artificial = 1;
15597
15598 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15599
15600 /* Get index in virtual function table if it is a virtual member
15601 function. For older versions of GCC, this is an offset in the
15602 appropriate virtual table, as specified by DW_AT_containing_type.
15603 For everyone else, it is an expression to be evaluated relative
15604 to the object address. */
15605
15606 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15607 if (attr)
15608 {
15609 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15610 {
15611 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15612 {
15613 /* Old-style GCC. */
15614 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15615 }
15616 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15617 || (DW_BLOCK (attr)->size > 1
15618 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15619 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15620 {
15621 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15622 if ((fnp->voffset % cu->header.addr_size) != 0)
15623 dwarf2_complex_location_expr_complaint ();
15624 else
15625 fnp->voffset /= cu->header.addr_size;
15626 fnp->voffset += 2;
15627 }
15628 else
15629 dwarf2_complex_location_expr_complaint ();
15630
15631 if (!fnp->fcontext)
15632 {
15633 /* If there is no `this' field and no DW_AT_containing_type,
15634 we cannot actually find a base class context for the
15635 vtable! */
15636 if (TYPE_NFIELDS (this_type) == 0
15637 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15638 {
15639 complaint (&symfile_complaints,
15640 _("cannot determine context for virtual member "
15641 "function \"%s\" (offset %d)"),
15642 fieldname, to_underlying (die->sect_off));
15643 }
15644 else
15645 {
15646 fnp->fcontext
15647 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15648 }
15649 }
15650 }
15651 else if (attr_form_is_section_offset (attr))
15652 {
15653 dwarf2_complex_location_expr_complaint ();
15654 }
15655 else
15656 {
15657 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15658 fieldname);
15659 }
15660 }
15661 else
15662 {
15663 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15664 if (attr && DW_UNSND (attr))
15665 {
15666 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15667 complaint (&symfile_complaints,
15668 _("Member function \"%s\" (offset %d) is virtual "
15669 "but the vtable offset is not specified"),
15670 fieldname, to_underlying (die->sect_off));
15671 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15672 TYPE_CPLUS_DYNAMIC (type) = 1;
15673 }
15674 }
15675 }
15676
15677 /* Create the vector of member function fields, and attach it to the type. */
15678
15679 static void
15680 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15681 struct dwarf2_cu *cu)
15682 {
15683 struct fnfieldlist *flp;
15684 int i;
15685
15686 if (cu->language == language_ada)
15687 error (_("unexpected member functions in Ada type"));
15688
15689 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15690 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15691 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
15692
15693 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
15694 {
15695 struct nextfnfield *nfp = flp->head;
15696 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15697 int k;
15698
15699 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
15700 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
15701 fn_flp->fn_fields = (struct fn_field *)
15702 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
15703 for (k = flp->length; (k--, nfp); nfp = nfp->next)
15704 fn_flp->fn_fields[k] = nfp->fnfield;
15705 }
15706
15707 TYPE_NFN_FIELDS (type) = fip->nfnfields;
15708 }
15709
15710 /* Returns non-zero if NAME is the name of a vtable member in CU's
15711 language, zero otherwise. */
15712 static int
15713 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15714 {
15715 static const char vptr[] = "_vptr";
15716
15717 /* Look for the C++ form of the vtable. */
15718 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15719 return 1;
15720
15721 return 0;
15722 }
15723
15724 /* GCC outputs unnamed structures that are really pointers to member
15725 functions, with the ABI-specified layout. If TYPE describes
15726 such a structure, smash it into a member function type.
15727
15728 GCC shouldn't do this; it should just output pointer to member DIEs.
15729 This is GCC PR debug/28767. */
15730
15731 static void
15732 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15733 {
15734 struct type *pfn_type, *self_type, *new_type;
15735
15736 /* Check for a structure with no name and two children. */
15737 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15738 return;
15739
15740 /* Check for __pfn and __delta members. */
15741 if (TYPE_FIELD_NAME (type, 0) == NULL
15742 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15743 || TYPE_FIELD_NAME (type, 1) == NULL
15744 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15745 return;
15746
15747 /* Find the type of the method. */
15748 pfn_type = TYPE_FIELD_TYPE (type, 0);
15749 if (pfn_type == NULL
15750 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15751 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15752 return;
15753
15754 /* Look for the "this" argument. */
15755 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15756 if (TYPE_NFIELDS (pfn_type) == 0
15757 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15758 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15759 return;
15760
15761 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15762 new_type = alloc_type (objfile);
15763 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15764 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15765 TYPE_VARARGS (pfn_type));
15766 smash_to_methodptr_type (type, new_type);
15767 }
15768
15769
15770 /* Called when we find the DIE that starts a structure or union scope
15771 (definition) to create a type for the structure or union. Fill in
15772 the type's name and general properties; the members will not be
15773 processed until process_structure_scope. A symbol table entry for
15774 the type will also not be done until process_structure_scope (assuming
15775 the type has a name).
15776
15777 NOTE: we need to call these functions regardless of whether or not the
15778 DIE has a DW_AT_name attribute, since it might be an anonymous
15779 structure or union. This gets the type entered into our set of
15780 user defined types. */
15781
15782 static struct type *
15783 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15784 {
15785 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15786 struct type *type;
15787 struct attribute *attr;
15788 const char *name;
15789
15790 /* If the definition of this type lives in .debug_types, read that type.
15791 Don't follow DW_AT_specification though, that will take us back up
15792 the chain and we want to go down. */
15793 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15794 if (attr)
15795 {
15796 type = get_DW_AT_signature_type (die, attr, cu);
15797
15798 /* The type's CU may not be the same as CU.
15799 Ensure TYPE is recorded with CU in die_type_hash. */
15800 return set_die_type (die, type, cu);
15801 }
15802
15803 type = alloc_type (objfile);
15804 INIT_CPLUS_SPECIFIC (type);
15805
15806 name = dwarf2_name (die, cu);
15807 if (name != NULL)
15808 {
15809 if (cu->language == language_cplus
15810 || cu->language == language_d
15811 || cu->language == language_rust)
15812 {
15813 const char *full_name = dwarf2_full_name (name, die, cu);
15814
15815 /* dwarf2_full_name might have already finished building the DIE's
15816 type. If so, there is no need to continue. */
15817 if (get_die_type (die, cu) != NULL)
15818 return get_die_type (die, cu);
15819
15820 TYPE_TAG_NAME (type) = full_name;
15821 if (die->tag == DW_TAG_structure_type
15822 || die->tag == DW_TAG_class_type)
15823 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15824 }
15825 else
15826 {
15827 /* The name is already allocated along with this objfile, so
15828 we don't need to duplicate it for the type. */
15829 TYPE_TAG_NAME (type) = name;
15830 if (die->tag == DW_TAG_class_type)
15831 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15832 }
15833 }
15834
15835 if (die->tag == DW_TAG_structure_type)
15836 {
15837 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15838 }
15839 else if (die->tag == DW_TAG_union_type)
15840 {
15841 TYPE_CODE (type) = TYPE_CODE_UNION;
15842 }
15843 else
15844 {
15845 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15846 }
15847
15848 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15849 TYPE_DECLARED_CLASS (type) = 1;
15850
15851 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15852 if (attr)
15853 {
15854 if (attr_form_is_constant (attr))
15855 TYPE_LENGTH (type) = DW_UNSND (attr);
15856 else
15857 {
15858 /* For the moment, dynamic type sizes are not supported
15859 by GDB's struct type. The actual size is determined
15860 on-demand when resolving the type of a given object,
15861 so set the type's length to zero for now. Otherwise,
15862 we record an expression as the length, and that expression
15863 could lead to a very large value, which could eventually
15864 lead to us trying to allocate that much memory when creating
15865 a value of that type. */
15866 TYPE_LENGTH (type) = 0;
15867 }
15868 }
15869 else
15870 {
15871 TYPE_LENGTH (type) = 0;
15872 }
15873
15874 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15875 {
15876 /* ICC<14 does not output the required DW_AT_declaration on
15877 incomplete types, but gives them a size of zero. */
15878 TYPE_STUB (type) = 1;
15879 }
15880 else
15881 TYPE_STUB_SUPPORTED (type) = 1;
15882
15883 if (die_is_declaration (die, cu))
15884 TYPE_STUB (type) = 1;
15885 else if (attr == NULL && die->child == NULL
15886 && producer_is_realview (cu->producer))
15887 /* RealView does not output the required DW_AT_declaration
15888 on incomplete types. */
15889 TYPE_STUB (type) = 1;
15890
15891 /* We need to add the type field to the die immediately so we don't
15892 infinitely recurse when dealing with pointers to the structure
15893 type within the structure itself. */
15894 set_die_type (die, type, cu);
15895
15896 /* set_die_type should be already done. */
15897 set_descriptive_type (type, die, cu);
15898
15899 return type;
15900 }
15901
15902 /* Finish creating a structure or union type, including filling in
15903 its members and creating a symbol for it. */
15904
15905 static void
15906 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15907 {
15908 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15909 struct die_info *child_die;
15910 struct type *type;
15911
15912 type = get_die_type (die, cu);
15913 if (type == NULL)
15914 type = read_structure_type (die, cu);
15915
15916 if (die->child != NULL && ! die_is_declaration (die, cu))
15917 {
15918 struct field_info fi;
15919 std::vector<struct symbol *> template_args;
15920 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
15921
15922 memset (&fi, 0, sizeof (struct field_info));
15923
15924 child_die = die->child;
15925
15926 while (child_die && child_die->tag)
15927 {
15928 if (child_die->tag == DW_TAG_member
15929 || child_die->tag == DW_TAG_variable)
15930 {
15931 /* NOTE: carlton/2002-11-05: A C++ static data member
15932 should be a DW_TAG_member that is a declaration, but
15933 all versions of G++ as of this writing (so through at
15934 least 3.2.1) incorrectly generate DW_TAG_variable
15935 tags for them instead. */
15936 dwarf2_add_field (&fi, child_die, cu);
15937 }
15938 else if (child_die->tag == DW_TAG_subprogram)
15939 {
15940 /* Rust doesn't have member functions in the C++ sense.
15941 However, it does emit ordinary functions as children
15942 of a struct DIE. */
15943 if (cu->language == language_rust)
15944 read_func_scope (child_die, cu);
15945 else
15946 {
15947 /* C++ member function. */
15948 dwarf2_add_member_fn (&fi, child_die, type, cu);
15949 }
15950 }
15951 else if (child_die->tag == DW_TAG_inheritance)
15952 {
15953 /* C++ base class field. */
15954 dwarf2_add_field (&fi, child_die, cu);
15955 }
15956 else if (type_can_define_types (child_die))
15957 dwarf2_add_type_defn (&fi, child_die, cu);
15958 else if (child_die->tag == DW_TAG_template_type_param
15959 || child_die->tag == DW_TAG_template_value_param)
15960 {
15961 struct symbol *arg = new_symbol (child_die, NULL, cu);
15962
15963 if (arg != NULL)
15964 template_args.push_back (arg);
15965 }
15966
15967 child_die = sibling_die (child_die);
15968 }
15969
15970 /* Attach template arguments to type. */
15971 if (!template_args.empty ())
15972 {
15973 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15974 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15975 TYPE_TEMPLATE_ARGUMENTS (type)
15976 = XOBNEWVEC (&objfile->objfile_obstack,
15977 struct symbol *,
15978 TYPE_N_TEMPLATE_ARGUMENTS (type));
15979 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15980 template_args.data (),
15981 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15982 * sizeof (struct symbol *)));
15983 }
15984
15985 /* Attach fields and member functions to the type. */
15986 if (fi.nfields)
15987 dwarf2_attach_fields_to_type (&fi, type, cu);
15988 if (fi.nfnfields)
15989 {
15990 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15991
15992 /* Get the type which refers to the base class (possibly this
15993 class itself) which contains the vtable pointer for the current
15994 class from the DW_AT_containing_type attribute. This use of
15995 DW_AT_containing_type is a GNU extension. */
15996
15997 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15998 {
15999 struct type *t = die_containing_type (die, cu);
16000
16001 set_type_vptr_basetype (type, t);
16002 if (type == t)
16003 {
16004 int i;
16005
16006 /* Our own class provides vtbl ptr. */
16007 for (i = TYPE_NFIELDS (t) - 1;
16008 i >= TYPE_N_BASECLASSES (t);
16009 --i)
16010 {
16011 const char *fieldname = TYPE_FIELD_NAME (t, i);
16012
16013 if (is_vtable_name (fieldname, cu))
16014 {
16015 set_type_vptr_fieldno (type, i);
16016 break;
16017 }
16018 }
16019
16020 /* Complain if virtual function table field not found. */
16021 if (i < TYPE_N_BASECLASSES (t))
16022 complaint (&symfile_complaints,
16023 _("virtual function table pointer "
16024 "not found when defining class '%s'"),
16025 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16026 "");
16027 }
16028 else
16029 {
16030 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16031 }
16032 }
16033 else if (cu->producer
16034 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16035 {
16036 /* The IBM XLC compiler does not provide direct indication
16037 of the containing type, but the vtable pointer is
16038 always named __vfp. */
16039
16040 int i;
16041
16042 for (i = TYPE_NFIELDS (type) - 1;
16043 i >= TYPE_N_BASECLASSES (type);
16044 --i)
16045 {
16046 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16047 {
16048 set_type_vptr_fieldno (type, i);
16049 set_type_vptr_basetype (type, type);
16050 break;
16051 }
16052 }
16053 }
16054 }
16055
16056 /* Copy fi.typedef_field_list linked list elements content into the
16057 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16058 if (fi.typedef_field_list)
16059 {
16060 int i = fi.typedef_field_list_count;
16061
16062 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16063 TYPE_TYPEDEF_FIELD_ARRAY (type)
16064 = ((struct decl_field *)
16065 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
16066 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
16067
16068 /* Reverse the list order to keep the debug info elements order. */
16069 while (--i >= 0)
16070 {
16071 struct decl_field *dest, *src;
16072
16073 dest = &TYPE_TYPEDEF_FIELD (type, i);
16074 src = &fi.typedef_field_list->field;
16075 fi.typedef_field_list = fi.typedef_field_list->next;
16076 *dest = *src;
16077 }
16078 }
16079
16080 /* Copy fi.nested_types_list linked list elements content into the
16081 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16082 if (fi.nested_types_list != NULL && cu->language != language_ada)
16083 {
16084 int i = fi.nested_types_list_count;
16085
16086 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16087 TYPE_NESTED_TYPES_ARRAY (type)
16088 = ((struct decl_field *)
16089 TYPE_ALLOC (type, sizeof (struct decl_field) * i));
16090 TYPE_NESTED_TYPES_COUNT (type) = i;
16091
16092 /* Reverse the list order to keep the debug info elements order. */
16093 while (--i >= 0)
16094 {
16095 struct decl_field *dest, *src;
16096
16097 dest = &TYPE_NESTED_TYPES_FIELD (type, i);
16098 src = &fi.nested_types_list->field;
16099 fi.nested_types_list = fi.nested_types_list->next;
16100 *dest = *src;
16101 }
16102 }
16103
16104 do_cleanups (back_to);
16105 }
16106
16107 quirk_gcc_member_function_pointer (type, objfile);
16108
16109 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16110 snapshots) has been known to create a die giving a declaration
16111 for a class that has, as a child, a die giving a definition for a
16112 nested class. So we have to process our children even if the
16113 current die is a declaration. Normally, of course, a declaration
16114 won't have any children at all. */
16115
16116 child_die = die->child;
16117
16118 while (child_die != NULL && child_die->tag)
16119 {
16120 if (child_die->tag == DW_TAG_member
16121 || child_die->tag == DW_TAG_variable
16122 || child_die->tag == DW_TAG_inheritance
16123 || child_die->tag == DW_TAG_template_value_param
16124 || child_die->tag == DW_TAG_template_type_param)
16125 {
16126 /* Do nothing. */
16127 }
16128 else
16129 process_die (child_die, cu);
16130
16131 child_die = sibling_die (child_die);
16132 }
16133
16134 /* Do not consider external references. According to the DWARF standard,
16135 these DIEs are identified by the fact that they have no byte_size
16136 attribute, and a declaration attribute. */
16137 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16138 || !die_is_declaration (die, cu))
16139 new_symbol (die, type, cu);
16140 }
16141
16142 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16143 update TYPE using some information only available in DIE's children. */
16144
16145 static void
16146 update_enumeration_type_from_children (struct die_info *die,
16147 struct type *type,
16148 struct dwarf2_cu *cu)
16149 {
16150 struct die_info *child_die;
16151 int unsigned_enum = 1;
16152 int flag_enum = 1;
16153 ULONGEST mask = 0;
16154
16155 auto_obstack obstack;
16156
16157 for (child_die = die->child;
16158 child_die != NULL && child_die->tag;
16159 child_die = sibling_die (child_die))
16160 {
16161 struct attribute *attr;
16162 LONGEST value;
16163 const gdb_byte *bytes;
16164 struct dwarf2_locexpr_baton *baton;
16165 const char *name;
16166
16167 if (child_die->tag != DW_TAG_enumerator)
16168 continue;
16169
16170 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16171 if (attr == NULL)
16172 continue;
16173
16174 name = dwarf2_name (child_die, cu);
16175 if (name == NULL)
16176 name = "<anonymous enumerator>";
16177
16178 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16179 &value, &bytes, &baton);
16180 if (value < 0)
16181 {
16182 unsigned_enum = 0;
16183 flag_enum = 0;
16184 }
16185 else if ((mask & value) != 0)
16186 flag_enum = 0;
16187 else
16188 mask |= value;
16189
16190 /* If we already know that the enum type is neither unsigned, nor
16191 a flag type, no need to look at the rest of the enumerates. */
16192 if (!unsigned_enum && !flag_enum)
16193 break;
16194 }
16195
16196 if (unsigned_enum)
16197 TYPE_UNSIGNED (type) = 1;
16198 if (flag_enum)
16199 TYPE_FLAG_ENUM (type) = 1;
16200 }
16201
16202 /* Given a DW_AT_enumeration_type die, set its type. We do not
16203 complete the type's fields yet, or create any symbols. */
16204
16205 static struct type *
16206 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16207 {
16208 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16209 struct type *type;
16210 struct attribute *attr;
16211 const char *name;
16212
16213 /* If the definition of this type lives in .debug_types, read that type.
16214 Don't follow DW_AT_specification though, that will take us back up
16215 the chain and we want to go down. */
16216 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16217 if (attr)
16218 {
16219 type = get_DW_AT_signature_type (die, attr, cu);
16220
16221 /* The type's CU may not be the same as CU.
16222 Ensure TYPE is recorded with CU in die_type_hash. */
16223 return set_die_type (die, type, cu);
16224 }
16225
16226 type = alloc_type (objfile);
16227
16228 TYPE_CODE (type) = TYPE_CODE_ENUM;
16229 name = dwarf2_full_name (NULL, die, cu);
16230 if (name != NULL)
16231 TYPE_TAG_NAME (type) = name;
16232
16233 attr = dwarf2_attr (die, DW_AT_type, cu);
16234 if (attr != NULL)
16235 {
16236 struct type *underlying_type = die_type (die, cu);
16237
16238 TYPE_TARGET_TYPE (type) = underlying_type;
16239 }
16240
16241 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16242 if (attr)
16243 {
16244 TYPE_LENGTH (type) = DW_UNSND (attr);
16245 }
16246 else
16247 {
16248 TYPE_LENGTH (type) = 0;
16249 }
16250
16251 /* The enumeration DIE can be incomplete. In Ada, any type can be
16252 declared as private in the package spec, and then defined only
16253 inside the package body. Such types are known as Taft Amendment
16254 Types. When another package uses such a type, an incomplete DIE
16255 may be generated by the compiler. */
16256 if (die_is_declaration (die, cu))
16257 TYPE_STUB (type) = 1;
16258
16259 /* Finish the creation of this type by using the enum's children.
16260 We must call this even when the underlying type has been provided
16261 so that we can determine if we're looking at a "flag" enum. */
16262 update_enumeration_type_from_children (die, type, cu);
16263
16264 /* If this type has an underlying type that is not a stub, then we
16265 may use its attributes. We always use the "unsigned" attribute
16266 in this situation, because ordinarily we guess whether the type
16267 is unsigned -- but the guess can be wrong and the underlying type
16268 can tell us the reality. However, we defer to a local size
16269 attribute if one exists, because this lets the compiler override
16270 the underlying type if needed. */
16271 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16272 {
16273 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16274 if (TYPE_LENGTH (type) == 0)
16275 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16276 }
16277
16278 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16279
16280 return set_die_type (die, type, cu);
16281 }
16282
16283 /* Given a pointer to a die which begins an enumeration, process all
16284 the dies that define the members of the enumeration, and create the
16285 symbol for the enumeration type.
16286
16287 NOTE: We reverse the order of the element list. */
16288
16289 static void
16290 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16291 {
16292 struct type *this_type;
16293
16294 this_type = get_die_type (die, cu);
16295 if (this_type == NULL)
16296 this_type = read_enumeration_type (die, cu);
16297
16298 if (die->child != NULL)
16299 {
16300 struct die_info *child_die;
16301 struct symbol *sym;
16302 struct field *fields = NULL;
16303 int num_fields = 0;
16304 const char *name;
16305
16306 child_die = die->child;
16307 while (child_die && child_die->tag)
16308 {
16309 if (child_die->tag != DW_TAG_enumerator)
16310 {
16311 process_die (child_die, cu);
16312 }
16313 else
16314 {
16315 name = dwarf2_name (child_die, cu);
16316 if (name)
16317 {
16318 sym = new_symbol (child_die, this_type, cu);
16319
16320 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16321 {
16322 fields = (struct field *)
16323 xrealloc (fields,
16324 (num_fields + DW_FIELD_ALLOC_CHUNK)
16325 * sizeof (struct field));
16326 }
16327
16328 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16329 FIELD_TYPE (fields[num_fields]) = NULL;
16330 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16331 FIELD_BITSIZE (fields[num_fields]) = 0;
16332
16333 num_fields++;
16334 }
16335 }
16336
16337 child_die = sibling_die (child_die);
16338 }
16339
16340 if (num_fields)
16341 {
16342 TYPE_NFIELDS (this_type) = num_fields;
16343 TYPE_FIELDS (this_type) = (struct field *)
16344 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16345 memcpy (TYPE_FIELDS (this_type), fields,
16346 sizeof (struct field) * num_fields);
16347 xfree (fields);
16348 }
16349 }
16350
16351 /* If we are reading an enum from a .debug_types unit, and the enum
16352 is a declaration, and the enum is not the signatured type in the
16353 unit, then we do not want to add a symbol for it. Adding a
16354 symbol would in some cases obscure the true definition of the
16355 enum, giving users an incomplete type when the definition is
16356 actually available. Note that we do not want to do this for all
16357 enums which are just declarations, because C++0x allows forward
16358 enum declarations. */
16359 if (cu->per_cu->is_debug_types
16360 && die_is_declaration (die, cu))
16361 {
16362 struct signatured_type *sig_type;
16363
16364 sig_type = (struct signatured_type *) cu->per_cu;
16365 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16366 if (sig_type->type_offset_in_section != die->sect_off)
16367 return;
16368 }
16369
16370 new_symbol (die, this_type, cu);
16371 }
16372
16373 /* Extract all information from a DW_TAG_array_type DIE and put it in
16374 the DIE's type field. For now, this only handles one dimensional
16375 arrays. */
16376
16377 static struct type *
16378 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16379 {
16380 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16381 struct die_info *child_die;
16382 struct type *type;
16383 struct type *element_type, *range_type, *index_type;
16384 struct attribute *attr;
16385 const char *name;
16386 struct dynamic_prop *byte_stride_prop = NULL;
16387 unsigned int bit_stride = 0;
16388
16389 element_type = die_type (die, cu);
16390
16391 /* The die_type call above may have already set the type for this DIE. */
16392 type = get_die_type (die, cu);
16393 if (type)
16394 return type;
16395
16396 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16397 if (attr != NULL)
16398 {
16399 int stride_ok;
16400
16401 byte_stride_prop
16402 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16403 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16404 if (!stride_ok)
16405 {
16406 complaint (&symfile_complaints,
16407 _("unable to read array DW_AT_byte_stride "
16408 " - DIE at 0x%x [in module %s]"),
16409 to_underlying (die->sect_off),
16410 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16411 /* Ignore this attribute. We will likely not be able to print
16412 arrays of this type correctly, but there is little we can do
16413 to help if we cannot read the attribute's value. */
16414 byte_stride_prop = NULL;
16415 }
16416 }
16417
16418 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16419 if (attr != NULL)
16420 bit_stride = DW_UNSND (attr);
16421
16422 /* Irix 6.2 native cc creates array types without children for
16423 arrays with unspecified length. */
16424 if (die->child == NULL)
16425 {
16426 index_type = objfile_type (objfile)->builtin_int;
16427 range_type = create_static_range_type (NULL, index_type, 0, -1);
16428 type = create_array_type_with_stride (NULL, element_type, range_type,
16429 byte_stride_prop, bit_stride);
16430 return set_die_type (die, type, cu);
16431 }
16432
16433 std::vector<struct type *> range_types;
16434 child_die = die->child;
16435 while (child_die && child_die->tag)
16436 {
16437 if (child_die->tag == DW_TAG_subrange_type)
16438 {
16439 struct type *child_type = read_type_die (child_die, cu);
16440
16441 if (child_type != NULL)
16442 {
16443 /* The range type was succesfully read. Save it for the
16444 array type creation. */
16445 range_types.push_back (child_type);
16446 }
16447 }
16448 child_die = sibling_die (child_die);
16449 }
16450
16451 /* Dwarf2 dimensions are output from left to right, create the
16452 necessary array types in backwards order. */
16453
16454 type = element_type;
16455
16456 if (read_array_order (die, cu) == DW_ORD_col_major)
16457 {
16458 int i = 0;
16459
16460 while (i < range_types.size ())
16461 type = create_array_type_with_stride (NULL, type, range_types[i++],
16462 byte_stride_prop, bit_stride);
16463 }
16464 else
16465 {
16466 size_t ndim = range_types.size ();
16467 while (ndim-- > 0)
16468 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16469 byte_stride_prop, bit_stride);
16470 }
16471
16472 /* Understand Dwarf2 support for vector types (like they occur on
16473 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16474 array type. This is not part of the Dwarf2/3 standard yet, but a
16475 custom vendor extension. The main difference between a regular
16476 array and the vector variant is that vectors are passed by value
16477 to functions. */
16478 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16479 if (attr)
16480 make_vector_type (type);
16481
16482 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16483 implementation may choose to implement triple vectors using this
16484 attribute. */
16485 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16486 if (attr)
16487 {
16488 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16489 TYPE_LENGTH (type) = DW_UNSND (attr);
16490 else
16491 complaint (&symfile_complaints,
16492 _("DW_AT_byte_size for array type smaller "
16493 "than the total size of elements"));
16494 }
16495
16496 name = dwarf2_name (die, cu);
16497 if (name)
16498 TYPE_NAME (type) = name;
16499
16500 /* Install the type in the die. */
16501 set_die_type (die, type, cu);
16502
16503 /* set_die_type should be already done. */
16504 set_descriptive_type (type, die, cu);
16505
16506 return type;
16507 }
16508
16509 static enum dwarf_array_dim_ordering
16510 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16511 {
16512 struct attribute *attr;
16513
16514 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16515
16516 if (attr)
16517 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16518
16519 /* GNU F77 is a special case, as at 08/2004 array type info is the
16520 opposite order to the dwarf2 specification, but data is still
16521 laid out as per normal fortran.
16522
16523 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16524 version checking. */
16525
16526 if (cu->language == language_fortran
16527 && cu->producer && strstr (cu->producer, "GNU F77"))
16528 {
16529 return DW_ORD_row_major;
16530 }
16531
16532 switch (cu->language_defn->la_array_ordering)
16533 {
16534 case array_column_major:
16535 return DW_ORD_col_major;
16536 case array_row_major:
16537 default:
16538 return DW_ORD_row_major;
16539 };
16540 }
16541
16542 /* Extract all information from a DW_TAG_set_type DIE and put it in
16543 the DIE's type field. */
16544
16545 static struct type *
16546 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16547 {
16548 struct type *domain_type, *set_type;
16549 struct attribute *attr;
16550
16551 domain_type = die_type (die, cu);
16552
16553 /* The die_type call above may have already set the type for this DIE. */
16554 set_type = get_die_type (die, cu);
16555 if (set_type)
16556 return set_type;
16557
16558 set_type = create_set_type (NULL, domain_type);
16559
16560 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16561 if (attr)
16562 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16563
16564 return set_die_type (die, set_type, cu);
16565 }
16566
16567 /* A helper for read_common_block that creates a locexpr baton.
16568 SYM is the symbol which we are marking as computed.
16569 COMMON_DIE is the DIE for the common block.
16570 COMMON_LOC is the location expression attribute for the common
16571 block itself.
16572 MEMBER_LOC is the location expression attribute for the particular
16573 member of the common block that we are processing.
16574 CU is the CU from which the above come. */
16575
16576 static void
16577 mark_common_block_symbol_computed (struct symbol *sym,
16578 struct die_info *common_die,
16579 struct attribute *common_loc,
16580 struct attribute *member_loc,
16581 struct dwarf2_cu *cu)
16582 {
16583 struct dwarf2_per_objfile *dwarf2_per_objfile
16584 = cu->per_cu->dwarf2_per_objfile;
16585 struct objfile *objfile = dwarf2_per_objfile->objfile;
16586 struct dwarf2_locexpr_baton *baton;
16587 gdb_byte *ptr;
16588 unsigned int cu_off;
16589 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16590 LONGEST offset = 0;
16591
16592 gdb_assert (common_loc && member_loc);
16593 gdb_assert (attr_form_is_block (common_loc));
16594 gdb_assert (attr_form_is_block (member_loc)
16595 || attr_form_is_constant (member_loc));
16596
16597 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16598 baton->per_cu = cu->per_cu;
16599 gdb_assert (baton->per_cu);
16600
16601 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16602
16603 if (attr_form_is_constant (member_loc))
16604 {
16605 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16606 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16607 }
16608 else
16609 baton->size += DW_BLOCK (member_loc)->size;
16610
16611 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16612 baton->data = ptr;
16613
16614 *ptr++ = DW_OP_call4;
16615 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16616 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16617 ptr += 4;
16618
16619 if (attr_form_is_constant (member_loc))
16620 {
16621 *ptr++ = DW_OP_addr;
16622 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16623 ptr += cu->header.addr_size;
16624 }
16625 else
16626 {
16627 /* We have to copy the data here, because DW_OP_call4 will only
16628 use a DW_AT_location attribute. */
16629 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16630 ptr += DW_BLOCK (member_loc)->size;
16631 }
16632
16633 *ptr++ = DW_OP_plus;
16634 gdb_assert (ptr - baton->data == baton->size);
16635
16636 SYMBOL_LOCATION_BATON (sym) = baton;
16637 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16638 }
16639
16640 /* Create appropriate locally-scoped variables for all the
16641 DW_TAG_common_block entries. Also create a struct common_block
16642 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16643 is used to sepate the common blocks name namespace from regular
16644 variable names. */
16645
16646 static void
16647 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16648 {
16649 struct attribute *attr;
16650
16651 attr = dwarf2_attr (die, DW_AT_location, cu);
16652 if (attr)
16653 {
16654 /* Support the .debug_loc offsets. */
16655 if (attr_form_is_block (attr))
16656 {
16657 /* Ok. */
16658 }
16659 else if (attr_form_is_section_offset (attr))
16660 {
16661 dwarf2_complex_location_expr_complaint ();
16662 attr = NULL;
16663 }
16664 else
16665 {
16666 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16667 "common block member");
16668 attr = NULL;
16669 }
16670 }
16671
16672 if (die->child != NULL)
16673 {
16674 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16675 struct die_info *child_die;
16676 size_t n_entries = 0, size;
16677 struct common_block *common_block;
16678 struct symbol *sym;
16679
16680 for (child_die = die->child;
16681 child_die && child_die->tag;
16682 child_die = sibling_die (child_die))
16683 ++n_entries;
16684
16685 size = (sizeof (struct common_block)
16686 + (n_entries - 1) * sizeof (struct symbol *));
16687 common_block
16688 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16689 size);
16690 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16691 common_block->n_entries = 0;
16692
16693 for (child_die = die->child;
16694 child_die && child_die->tag;
16695 child_die = sibling_die (child_die))
16696 {
16697 /* Create the symbol in the DW_TAG_common_block block in the current
16698 symbol scope. */
16699 sym = new_symbol (child_die, NULL, cu);
16700 if (sym != NULL)
16701 {
16702 struct attribute *member_loc;
16703
16704 common_block->contents[common_block->n_entries++] = sym;
16705
16706 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16707 cu);
16708 if (member_loc)
16709 {
16710 /* GDB has handled this for a long time, but it is
16711 not specified by DWARF. It seems to have been
16712 emitted by gfortran at least as recently as:
16713 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16714 complaint (&symfile_complaints,
16715 _("Variable in common block has "
16716 "DW_AT_data_member_location "
16717 "- DIE at 0x%x [in module %s]"),
16718 to_underlying (child_die->sect_off),
16719 objfile_name (objfile));
16720
16721 if (attr_form_is_section_offset (member_loc))
16722 dwarf2_complex_location_expr_complaint ();
16723 else if (attr_form_is_constant (member_loc)
16724 || attr_form_is_block (member_loc))
16725 {
16726 if (attr)
16727 mark_common_block_symbol_computed (sym, die, attr,
16728 member_loc, cu);
16729 }
16730 else
16731 dwarf2_complex_location_expr_complaint ();
16732 }
16733 }
16734 }
16735
16736 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16737 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16738 }
16739 }
16740
16741 /* Create a type for a C++ namespace. */
16742
16743 static struct type *
16744 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16745 {
16746 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16747 const char *previous_prefix, *name;
16748 int is_anonymous;
16749 struct type *type;
16750
16751 /* For extensions, reuse the type of the original namespace. */
16752 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16753 {
16754 struct die_info *ext_die;
16755 struct dwarf2_cu *ext_cu = cu;
16756
16757 ext_die = dwarf2_extension (die, &ext_cu);
16758 type = read_type_die (ext_die, ext_cu);
16759
16760 /* EXT_CU may not be the same as CU.
16761 Ensure TYPE is recorded with CU in die_type_hash. */
16762 return set_die_type (die, type, cu);
16763 }
16764
16765 name = namespace_name (die, &is_anonymous, cu);
16766
16767 /* Now build the name of the current namespace. */
16768
16769 previous_prefix = determine_prefix (die, cu);
16770 if (previous_prefix[0] != '\0')
16771 name = typename_concat (&objfile->objfile_obstack,
16772 previous_prefix, name, 0, cu);
16773
16774 /* Create the type. */
16775 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16776 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16777
16778 return set_die_type (die, type, cu);
16779 }
16780
16781 /* Read a namespace scope. */
16782
16783 static void
16784 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16785 {
16786 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16787 int is_anonymous;
16788
16789 /* Add a symbol associated to this if we haven't seen the namespace
16790 before. Also, add a using directive if it's an anonymous
16791 namespace. */
16792
16793 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16794 {
16795 struct type *type;
16796
16797 type = read_type_die (die, cu);
16798 new_symbol (die, type, cu);
16799
16800 namespace_name (die, &is_anonymous, cu);
16801 if (is_anonymous)
16802 {
16803 const char *previous_prefix = determine_prefix (die, cu);
16804
16805 std::vector<const char *> excludes;
16806 add_using_directive (using_directives (cu->language),
16807 previous_prefix, TYPE_NAME (type), NULL,
16808 NULL, excludes, 0, &objfile->objfile_obstack);
16809 }
16810 }
16811
16812 if (die->child != NULL)
16813 {
16814 struct die_info *child_die = die->child;
16815
16816 while (child_die && child_die->tag)
16817 {
16818 process_die (child_die, cu);
16819 child_die = sibling_die (child_die);
16820 }
16821 }
16822 }
16823
16824 /* Read a Fortran module as type. This DIE can be only a declaration used for
16825 imported module. Still we need that type as local Fortran "use ... only"
16826 declaration imports depend on the created type in determine_prefix. */
16827
16828 static struct type *
16829 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16830 {
16831 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16832 const char *module_name;
16833 struct type *type;
16834
16835 module_name = dwarf2_name (die, cu);
16836 if (!module_name)
16837 complaint (&symfile_complaints,
16838 _("DW_TAG_module has no name, offset 0x%x"),
16839 to_underlying (die->sect_off));
16840 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16841
16842 /* determine_prefix uses TYPE_TAG_NAME. */
16843 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16844
16845 return set_die_type (die, type, cu);
16846 }
16847
16848 /* Read a Fortran module. */
16849
16850 static void
16851 read_module (struct die_info *die, struct dwarf2_cu *cu)
16852 {
16853 struct die_info *child_die = die->child;
16854 struct type *type;
16855
16856 type = read_type_die (die, cu);
16857 new_symbol (die, type, cu);
16858
16859 while (child_die && child_die->tag)
16860 {
16861 process_die (child_die, cu);
16862 child_die = sibling_die (child_die);
16863 }
16864 }
16865
16866 /* Return the name of the namespace represented by DIE. Set
16867 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16868 namespace. */
16869
16870 static const char *
16871 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16872 {
16873 struct die_info *current_die;
16874 const char *name = NULL;
16875
16876 /* Loop through the extensions until we find a name. */
16877
16878 for (current_die = die;
16879 current_die != NULL;
16880 current_die = dwarf2_extension (die, &cu))
16881 {
16882 /* We don't use dwarf2_name here so that we can detect the absence
16883 of a name -> anonymous namespace. */
16884 name = dwarf2_string_attr (die, DW_AT_name, cu);
16885
16886 if (name != NULL)
16887 break;
16888 }
16889
16890 /* Is it an anonymous namespace? */
16891
16892 *is_anonymous = (name == NULL);
16893 if (*is_anonymous)
16894 name = CP_ANONYMOUS_NAMESPACE_STR;
16895
16896 return name;
16897 }
16898
16899 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16900 the user defined type vector. */
16901
16902 static struct type *
16903 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16904 {
16905 struct gdbarch *gdbarch
16906 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16907 struct comp_unit_head *cu_header = &cu->header;
16908 struct type *type;
16909 struct attribute *attr_byte_size;
16910 struct attribute *attr_address_class;
16911 int byte_size, addr_class;
16912 struct type *target_type;
16913
16914 target_type = die_type (die, cu);
16915
16916 /* The die_type call above may have already set the type for this DIE. */
16917 type = get_die_type (die, cu);
16918 if (type)
16919 return type;
16920
16921 type = lookup_pointer_type (target_type);
16922
16923 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16924 if (attr_byte_size)
16925 byte_size = DW_UNSND (attr_byte_size);
16926 else
16927 byte_size = cu_header->addr_size;
16928
16929 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16930 if (attr_address_class)
16931 addr_class = DW_UNSND (attr_address_class);
16932 else
16933 addr_class = DW_ADDR_none;
16934
16935 /* If the pointer size or address class is different than the
16936 default, create a type variant marked as such and set the
16937 length accordingly. */
16938 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
16939 {
16940 if (gdbarch_address_class_type_flags_p (gdbarch))
16941 {
16942 int type_flags;
16943
16944 type_flags = gdbarch_address_class_type_flags
16945 (gdbarch, byte_size, addr_class);
16946 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16947 == 0);
16948 type = make_type_with_address_space (type, type_flags);
16949 }
16950 else if (TYPE_LENGTH (type) != byte_size)
16951 {
16952 complaint (&symfile_complaints,
16953 _("invalid pointer size %d"), byte_size);
16954 }
16955 else
16956 {
16957 /* Should we also complain about unhandled address classes? */
16958 }
16959 }
16960
16961 TYPE_LENGTH (type) = byte_size;
16962 return set_die_type (die, type, cu);
16963 }
16964
16965 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16966 the user defined type vector. */
16967
16968 static struct type *
16969 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16970 {
16971 struct type *type;
16972 struct type *to_type;
16973 struct type *domain;
16974
16975 to_type = die_type (die, cu);
16976 domain = die_containing_type (die, cu);
16977
16978 /* The calls above may have already set the type for this DIE. */
16979 type = get_die_type (die, cu);
16980 if (type)
16981 return type;
16982
16983 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16984 type = lookup_methodptr_type (to_type);
16985 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16986 {
16987 struct type *new_type
16988 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16989
16990 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16991 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16992 TYPE_VARARGS (to_type));
16993 type = lookup_methodptr_type (new_type);
16994 }
16995 else
16996 type = lookup_memberptr_type (to_type, domain);
16997
16998 return set_die_type (die, type, cu);
16999 }
17000
17001 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17002 the user defined type vector. */
17003
17004 static struct type *
17005 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17006 enum type_code refcode)
17007 {
17008 struct comp_unit_head *cu_header = &cu->header;
17009 struct type *type, *target_type;
17010 struct attribute *attr;
17011
17012 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17013
17014 target_type = die_type (die, cu);
17015
17016 /* The die_type call above may have already set the type for this DIE. */
17017 type = get_die_type (die, cu);
17018 if (type)
17019 return type;
17020
17021 type = lookup_reference_type (target_type, refcode);
17022 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17023 if (attr)
17024 {
17025 TYPE_LENGTH (type) = DW_UNSND (attr);
17026 }
17027 else
17028 {
17029 TYPE_LENGTH (type) = cu_header->addr_size;
17030 }
17031 return set_die_type (die, type, cu);
17032 }
17033
17034 /* Add the given cv-qualifiers to the element type of the array. GCC
17035 outputs DWARF type qualifiers that apply to an array, not the
17036 element type. But GDB relies on the array element type to carry
17037 the cv-qualifiers. This mimics section 6.7.3 of the C99
17038 specification. */
17039
17040 static struct type *
17041 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17042 struct type *base_type, int cnst, int voltl)
17043 {
17044 struct type *el_type, *inner_array;
17045
17046 base_type = copy_type (base_type);
17047 inner_array = base_type;
17048
17049 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17050 {
17051 TYPE_TARGET_TYPE (inner_array) =
17052 copy_type (TYPE_TARGET_TYPE (inner_array));
17053 inner_array = TYPE_TARGET_TYPE (inner_array);
17054 }
17055
17056 el_type = TYPE_TARGET_TYPE (inner_array);
17057 cnst |= TYPE_CONST (el_type);
17058 voltl |= TYPE_VOLATILE (el_type);
17059 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17060
17061 return set_die_type (die, base_type, cu);
17062 }
17063
17064 static struct type *
17065 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17066 {
17067 struct type *base_type, *cv_type;
17068
17069 base_type = die_type (die, cu);
17070
17071 /* The die_type call above may have already set the type for this DIE. */
17072 cv_type = get_die_type (die, cu);
17073 if (cv_type)
17074 return cv_type;
17075
17076 /* In case the const qualifier is applied to an array type, the element type
17077 is so qualified, not the array type (section 6.7.3 of C99). */
17078 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17079 return add_array_cv_type (die, cu, base_type, 1, 0);
17080
17081 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17082 return set_die_type (die, cv_type, cu);
17083 }
17084
17085 static struct type *
17086 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17087 {
17088 struct type *base_type, *cv_type;
17089
17090 base_type = die_type (die, cu);
17091
17092 /* The die_type call above may have already set the type for this DIE. */
17093 cv_type = get_die_type (die, cu);
17094 if (cv_type)
17095 return cv_type;
17096
17097 /* In case the volatile qualifier is applied to an array type, the
17098 element type is so qualified, not the array type (section 6.7.3
17099 of C99). */
17100 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17101 return add_array_cv_type (die, cu, base_type, 0, 1);
17102
17103 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17104 return set_die_type (die, cv_type, cu);
17105 }
17106
17107 /* Handle DW_TAG_restrict_type. */
17108
17109 static struct type *
17110 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17111 {
17112 struct type *base_type, *cv_type;
17113
17114 base_type = die_type (die, cu);
17115
17116 /* The die_type call above may have already set the type for this DIE. */
17117 cv_type = get_die_type (die, cu);
17118 if (cv_type)
17119 return cv_type;
17120
17121 cv_type = make_restrict_type (base_type);
17122 return set_die_type (die, cv_type, cu);
17123 }
17124
17125 /* Handle DW_TAG_atomic_type. */
17126
17127 static struct type *
17128 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17129 {
17130 struct type *base_type, *cv_type;
17131
17132 base_type = die_type (die, cu);
17133
17134 /* The die_type call above may have already set the type for this DIE. */
17135 cv_type = get_die_type (die, cu);
17136 if (cv_type)
17137 return cv_type;
17138
17139 cv_type = make_atomic_type (base_type);
17140 return set_die_type (die, cv_type, cu);
17141 }
17142
17143 /* Extract all information from a DW_TAG_string_type DIE and add to
17144 the user defined type vector. It isn't really a user defined type,
17145 but it behaves like one, with other DIE's using an AT_user_def_type
17146 attribute to reference it. */
17147
17148 static struct type *
17149 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17150 {
17151 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17152 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17153 struct type *type, *range_type, *index_type, *char_type;
17154 struct attribute *attr;
17155 unsigned int length;
17156
17157 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17158 if (attr)
17159 {
17160 length = DW_UNSND (attr);
17161 }
17162 else
17163 {
17164 /* Check for the DW_AT_byte_size attribute. */
17165 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17166 if (attr)
17167 {
17168 length = DW_UNSND (attr);
17169 }
17170 else
17171 {
17172 length = 1;
17173 }
17174 }
17175
17176 index_type = objfile_type (objfile)->builtin_int;
17177 range_type = create_static_range_type (NULL, index_type, 1, length);
17178 char_type = language_string_char_type (cu->language_defn, gdbarch);
17179 type = create_string_type (NULL, char_type, range_type);
17180
17181 return set_die_type (die, type, cu);
17182 }
17183
17184 /* Assuming that DIE corresponds to a function, returns nonzero
17185 if the function is prototyped. */
17186
17187 static int
17188 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17189 {
17190 struct attribute *attr;
17191
17192 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17193 if (attr && (DW_UNSND (attr) != 0))
17194 return 1;
17195
17196 /* The DWARF standard implies that the DW_AT_prototyped attribute
17197 is only meaninful for C, but the concept also extends to other
17198 languages that allow unprototyped functions (Eg: Objective C).
17199 For all other languages, assume that functions are always
17200 prototyped. */
17201 if (cu->language != language_c
17202 && cu->language != language_objc
17203 && cu->language != language_opencl)
17204 return 1;
17205
17206 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17207 prototyped and unprototyped functions; default to prototyped,
17208 since that is more common in modern code (and RealView warns
17209 about unprototyped functions). */
17210 if (producer_is_realview (cu->producer))
17211 return 1;
17212
17213 return 0;
17214 }
17215
17216 /* Handle DIES due to C code like:
17217
17218 struct foo
17219 {
17220 int (*funcp)(int a, long l);
17221 int b;
17222 };
17223
17224 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17225
17226 static struct type *
17227 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17228 {
17229 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17230 struct type *type; /* Type that this function returns. */
17231 struct type *ftype; /* Function that returns above type. */
17232 struct attribute *attr;
17233
17234 type = die_type (die, cu);
17235
17236 /* The die_type call above may have already set the type for this DIE. */
17237 ftype = get_die_type (die, cu);
17238 if (ftype)
17239 return ftype;
17240
17241 ftype = lookup_function_type (type);
17242
17243 if (prototyped_function_p (die, cu))
17244 TYPE_PROTOTYPED (ftype) = 1;
17245
17246 /* Store the calling convention in the type if it's available in
17247 the subroutine die. Otherwise set the calling convention to
17248 the default value DW_CC_normal. */
17249 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17250 if (attr)
17251 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17252 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17253 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17254 else
17255 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17256
17257 /* Record whether the function returns normally to its caller or not
17258 if the DWARF producer set that information. */
17259 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17260 if (attr && (DW_UNSND (attr) != 0))
17261 TYPE_NO_RETURN (ftype) = 1;
17262
17263 /* We need to add the subroutine type to the die immediately so
17264 we don't infinitely recurse when dealing with parameters
17265 declared as the same subroutine type. */
17266 set_die_type (die, ftype, cu);
17267
17268 if (die->child != NULL)
17269 {
17270 struct type *void_type = objfile_type (objfile)->builtin_void;
17271 struct die_info *child_die;
17272 int nparams, iparams;
17273
17274 /* Count the number of parameters.
17275 FIXME: GDB currently ignores vararg functions, but knows about
17276 vararg member functions. */
17277 nparams = 0;
17278 child_die = die->child;
17279 while (child_die && child_die->tag)
17280 {
17281 if (child_die->tag == DW_TAG_formal_parameter)
17282 nparams++;
17283 else if (child_die->tag == DW_TAG_unspecified_parameters)
17284 TYPE_VARARGS (ftype) = 1;
17285 child_die = sibling_die (child_die);
17286 }
17287
17288 /* Allocate storage for parameters and fill them in. */
17289 TYPE_NFIELDS (ftype) = nparams;
17290 TYPE_FIELDS (ftype) = (struct field *)
17291 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17292
17293 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17294 even if we error out during the parameters reading below. */
17295 for (iparams = 0; iparams < nparams; iparams++)
17296 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17297
17298 iparams = 0;
17299 child_die = die->child;
17300 while (child_die && child_die->tag)
17301 {
17302 if (child_die->tag == DW_TAG_formal_parameter)
17303 {
17304 struct type *arg_type;
17305
17306 /* DWARF version 2 has no clean way to discern C++
17307 static and non-static member functions. G++ helps
17308 GDB by marking the first parameter for non-static
17309 member functions (which is the this pointer) as
17310 artificial. We pass this information to
17311 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17312
17313 DWARF version 3 added DW_AT_object_pointer, which GCC
17314 4.5 does not yet generate. */
17315 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17316 if (attr)
17317 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17318 else
17319 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17320 arg_type = die_type (child_die, cu);
17321
17322 /* RealView does not mark THIS as const, which the testsuite
17323 expects. GCC marks THIS as const in method definitions,
17324 but not in the class specifications (GCC PR 43053). */
17325 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17326 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17327 {
17328 int is_this = 0;
17329 struct dwarf2_cu *arg_cu = cu;
17330 const char *name = dwarf2_name (child_die, cu);
17331
17332 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17333 if (attr)
17334 {
17335 /* If the compiler emits this, use it. */
17336 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17337 is_this = 1;
17338 }
17339 else if (name && strcmp (name, "this") == 0)
17340 /* Function definitions will have the argument names. */
17341 is_this = 1;
17342 else if (name == NULL && iparams == 0)
17343 /* Declarations may not have the names, so like
17344 elsewhere in GDB, assume an artificial first
17345 argument is "this". */
17346 is_this = 1;
17347
17348 if (is_this)
17349 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17350 arg_type, 0);
17351 }
17352
17353 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17354 iparams++;
17355 }
17356 child_die = sibling_die (child_die);
17357 }
17358 }
17359
17360 return ftype;
17361 }
17362
17363 static struct type *
17364 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17365 {
17366 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17367 const char *name = NULL;
17368 struct type *this_type, *target_type;
17369
17370 name = dwarf2_full_name (NULL, die, cu);
17371 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17372 TYPE_TARGET_STUB (this_type) = 1;
17373 set_die_type (die, this_type, cu);
17374 target_type = die_type (die, cu);
17375 if (target_type != this_type)
17376 TYPE_TARGET_TYPE (this_type) = target_type;
17377 else
17378 {
17379 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17380 spec and cause infinite loops in GDB. */
17381 complaint (&symfile_complaints,
17382 _("Self-referential DW_TAG_typedef "
17383 "- DIE at 0x%x [in module %s]"),
17384 to_underlying (die->sect_off), objfile_name (objfile));
17385 TYPE_TARGET_TYPE (this_type) = NULL;
17386 }
17387 return this_type;
17388 }
17389
17390 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17391 (which may be different from NAME) to the architecture back-end to allow
17392 it to guess the correct format if necessary. */
17393
17394 static struct type *
17395 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17396 const char *name_hint)
17397 {
17398 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17399 const struct floatformat **format;
17400 struct type *type;
17401
17402 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17403 if (format)
17404 type = init_float_type (objfile, bits, name, format);
17405 else
17406 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17407
17408 return type;
17409 }
17410
17411 /* Find a representation of a given base type and install
17412 it in the TYPE field of the die. */
17413
17414 static struct type *
17415 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17416 {
17417 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17418 struct type *type;
17419 struct attribute *attr;
17420 int encoding = 0, bits = 0;
17421 const char *name;
17422
17423 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17424 if (attr)
17425 {
17426 encoding = DW_UNSND (attr);
17427 }
17428 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17429 if (attr)
17430 {
17431 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17432 }
17433 name = dwarf2_name (die, cu);
17434 if (!name)
17435 {
17436 complaint (&symfile_complaints,
17437 _("DW_AT_name missing from DW_TAG_base_type"));
17438 }
17439
17440 switch (encoding)
17441 {
17442 case DW_ATE_address:
17443 /* Turn DW_ATE_address into a void * pointer. */
17444 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17445 type = init_pointer_type (objfile, bits, name, type);
17446 break;
17447 case DW_ATE_boolean:
17448 type = init_boolean_type (objfile, bits, 1, name);
17449 break;
17450 case DW_ATE_complex_float:
17451 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17452 type = init_complex_type (objfile, name, type);
17453 break;
17454 case DW_ATE_decimal_float:
17455 type = init_decfloat_type (objfile, bits, name);
17456 break;
17457 case DW_ATE_float:
17458 type = dwarf2_init_float_type (objfile, bits, name, name);
17459 break;
17460 case DW_ATE_signed:
17461 type = init_integer_type (objfile, bits, 0, name);
17462 break;
17463 case DW_ATE_unsigned:
17464 if (cu->language == language_fortran
17465 && name
17466 && startswith (name, "character("))
17467 type = init_character_type (objfile, bits, 1, name);
17468 else
17469 type = init_integer_type (objfile, bits, 1, name);
17470 break;
17471 case DW_ATE_signed_char:
17472 if (cu->language == language_ada || cu->language == language_m2
17473 || cu->language == language_pascal
17474 || cu->language == language_fortran)
17475 type = init_character_type (objfile, bits, 0, name);
17476 else
17477 type = init_integer_type (objfile, bits, 0, name);
17478 break;
17479 case DW_ATE_unsigned_char:
17480 if (cu->language == language_ada || cu->language == language_m2
17481 || cu->language == language_pascal
17482 || cu->language == language_fortran
17483 || cu->language == language_rust)
17484 type = init_character_type (objfile, bits, 1, name);
17485 else
17486 type = init_integer_type (objfile, bits, 1, name);
17487 break;
17488 case DW_ATE_UTF:
17489 {
17490 gdbarch *arch = get_objfile_arch (objfile);
17491
17492 if (bits == 16)
17493 type = builtin_type (arch)->builtin_char16;
17494 else if (bits == 32)
17495 type = builtin_type (arch)->builtin_char32;
17496 else
17497 {
17498 complaint (&symfile_complaints,
17499 _("unsupported DW_ATE_UTF bit size: '%d'"),
17500 bits);
17501 type = init_integer_type (objfile, bits, 1, name);
17502 }
17503 return set_die_type (die, type, cu);
17504 }
17505 break;
17506
17507 default:
17508 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17509 dwarf_type_encoding_name (encoding));
17510 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17511 break;
17512 }
17513
17514 if (name && strcmp (name, "char") == 0)
17515 TYPE_NOSIGN (type) = 1;
17516
17517 return set_die_type (die, type, cu);
17518 }
17519
17520 /* Parse dwarf attribute if it's a block, reference or constant and put the
17521 resulting value of the attribute into struct bound_prop.
17522 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17523
17524 static int
17525 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17526 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17527 {
17528 struct dwarf2_property_baton *baton;
17529 struct obstack *obstack
17530 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17531
17532 if (attr == NULL || prop == NULL)
17533 return 0;
17534
17535 if (attr_form_is_block (attr))
17536 {
17537 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17538 baton->referenced_type = NULL;
17539 baton->locexpr.per_cu = cu->per_cu;
17540 baton->locexpr.size = DW_BLOCK (attr)->size;
17541 baton->locexpr.data = DW_BLOCK (attr)->data;
17542 prop->data.baton = baton;
17543 prop->kind = PROP_LOCEXPR;
17544 gdb_assert (prop->data.baton != NULL);
17545 }
17546 else if (attr_form_is_ref (attr))
17547 {
17548 struct dwarf2_cu *target_cu = cu;
17549 struct die_info *target_die;
17550 struct attribute *target_attr;
17551
17552 target_die = follow_die_ref (die, attr, &target_cu);
17553 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17554 if (target_attr == NULL)
17555 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17556 target_cu);
17557 if (target_attr == NULL)
17558 return 0;
17559
17560 switch (target_attr->name)
17561 {
17562 case DW_AT_location:
17563 if (attr_form_is_section_offset (target_attr))
17564 {
17565 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17566 baton->referenced_type = die_type (target_die, target_cu);
17567 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17568 prop->data.baton = baton;
17569 prop->kind = PROP_LOCLIST;
17570 gdb_assert (prop->data.baton != NULL);
17571 }
17572 else if (attr_form_is_block (target_attr))
17573 {
17574 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17575 baton->referenced_type = die_type (target_die, target_cu);
17576 baton->locexpr.per_cu = cu->per_cu;
17577 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17578 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17579 prop->data.baton = baton;
17580 prop->kind = PROP_LOCEXPR;
17581 gdb_assert (prop->data.baton != NULL);
17582 }
17583 else
17584 {
17585 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17586 "dynamic property");
17587 return 0;
17588 }
17589 break;
17590 case DW_AT_data_member_location:
17591 {
17592 LONGEST offset;
17593
17594 if (!handle_data_member_location (target_die, target_cu,
17595 &offset))
17596 return 0;
17597
17598 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17599 baton->referenced_type = read_type_die (target_die->parent,
17600 target_cu);
17601 baton->offset_info.offset = offset;
17602 baton->offset_info.type = die_type (target_die, target_cu);
17603 prop->data.baton = baton;
17604 prop->kind = PROP_ADDR_OFFSET;
17605 break;
17606 }
17607 }
17608 }
17609 else if (attr_form_is_constant (attr))
17610 {
17611 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17612 prop->kind = PROP_CONST;
17613 }
17614 else
17615 {
17616 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17617 dwarf2_name (die, cu));
17618 return 0;
17619 }
17620
17621 return 1;
17622 }
17623
17624 /* Read the given DW_AT_subrange DIE. */
17625
17626 static struct type *
17627 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17628 {
17629 struct type *base_type, *orig_base_type;
17630 struct type *range_type;
17631 struct attribute *attr;
17632 struct dynamic_prop low, high;
17633 int low_default_is_valid;
17634 int high_bound_is_count = 0;
17635 const char *name;
17636 LONGEST negative_mask;
17637
17638 orig_base_type = die_type (die, cu);
17639 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17640 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17641 creating the range type, but we use the result of check_typedef
17642 when examining properties of the type. */
17643 base_type = check_typedef (orig_base_type);
17644
17645 /* The die_type call above may have already set the type for this DIE. */
17646 range_type = get_die_type (die, cu);
17647 if (range_type)
17648 return range_type;
17649
17650 low.kind = PROP_CONST;
17651 high.kind = PROP_CONST;
17652 high.data.const_val = 0;
17653
17654 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17655 omitting DW_AT_lower_bound. */
17656 switch (cu->language)
17657 {
17658 case language_c:
17659 case language_cplus:
17660 low.data.const_val = 0;
17661 low_default_is_valid = 1;
17662 break;
17663 case language_fortran:
17664 low.data.const_val = 1;
17665 low_default_is_valid = 1;
17666 break;
17667 case language_d:
17668 case language_objc:
17669 case language_rust:
17670 low.data.const_val = 0;
17671 low_default_is_valid = (cu->header.version >= 4);
17672 break;
17673 case language_ada:
17674 case language_m2:
17675 case language_pascal:
17676 low.data.const_val = 1;
17677 low_default_is_valid = (cu->header.version >= 4);
17678 break;
17679 default:
17680 low.data.const_val = 0;
17681 low_default_is_valid = 0;
17682 break;
17683 }
17684
17685 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17686 if (attr)
17687 attr_to_dynamic_prop (attr, die, cu, &low);
17688 else if (!low_default_is_valid)
17689 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17690 "- DIE at 0x%x [in module %s]"),
17691 to_underlying (die->sect_off),
17692 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17693
17694 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
17695 if (!attr_to_dynamic_prop (attr, die, cu, &high))
17696 {
17697 attr = dwarf2_attr (die, DW_AT_count, cu);
17698 if (attr_to_dynamic_prop (attr, die, cu, &high))
17699 {
17700 /* If bounds are constant do the final calculation here. */
17701 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17702 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17703 else
17704 high_bound_is_count = 1;
17705 }
17706 }
17707
17708 /* Dwarf-2 specifications explicitly allows to create subrange types
17709 without specifying a base type.
17710 In that case, the base type must be set to the type of
17711 the lower bound, upper bound or count, in that order, if any of these
17712 three attributes references an object that has a type.
17713 If no base type is found, the Dwarf-2 specifications say that
17714 a signed integer type of size equal to the size of an address should
17715 be used.
17716 For the following C code: `extern char gdb_int [];'
17717 GCC produces an empty range DIE.
17718 FIXME: muller/2010-05-28: Possible references to object for low bound,
17719 high bound or count are not yet handled by this code. */
17720 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17721 {
17722 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17723 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17724 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17725 struct type *int_type = objfile_type (objfile)->builtin_int;
17726
17727 /* Test "int", "long int", and "long long int" objfile types,
17728 and select the first one having a size above or equal to the
17729 architecture address size. */
17730 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17731 base_type = int_type;
17732 else
17733 {
17734 int_type = objfile_type (objfile)->builtin_long;
17735 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17736 base_type = int_type;
17737 else
17738 {
17739 int_type = objfile_type (objfile)->builtin_long_long;
17740 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17741 base_type = int_type;
17742 }
17743 }
17744 }
17745
17746 /* Normally, the DWARF producers are expected to use a signed
17747 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17748 But this is unfortunately not always the case, as witnessed
17749 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17750 is used instead. To work around that ambiguity, we treat
17751 the bounds as signed, and thus sign-extend their values, when
17752 the base type is signed. */
17753 negative_mask =
17754 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17755 if (low.kind == PROP_CONST
17756 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17757 low.data.const_val |= negative_mask;
17758 if (high.kind == PROP_CONST
17759 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17760 high.data.const_val |= negative_mask;
17761
17762 range_type = create_range_type (NULL, orig_base_type, &low, &high);
17763
17764 if (high_bound_is_count)
17765 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17766
17767 /* Ada expects an empty array on no boundary attributes. */
17768 if (attr == NULL && cu->language != language_ada)
17769 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17770
17771 name = dwarf2_name (die, cu);
17772 if (name)
17773 TYPE_NAME (range_type) = name;
17774
17775 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17776 if (attr)
17777 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17778
17779 set_die_type (die, range_type, cu);
17780
17781 /* set_die_type should be already done. */
17782 set_descriptive_type (range_type, die, cu);
17783
17784 return range_type;
17785 }
17786
17787 static struct type *
17788 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17789 {
17790 struct type *type;
17791
17792 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17793 NULL);
17794 TYPE_NAME (type) = dwarf2_name (die, cu);
17795
17796 /* In Ada, an unspecified type is typically used when the description
17797 of the type is defered to a different unit. When encountering
17798 such a type, we treat it as a stub, and try to resolve it later on,
17799 when needed. */
17800 if (cu->language == language_ada)
17801 TYPE_STUB (type) = 1;
17802
17803 return set_die_type (die, type, cu);
17804 }
17805
17806 /* Read a single die and all its descendents. Set the die's sibling
17807 field to NULL; set other fields in the die correctly, and set all
17808 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17809 location of the info_ptr after reading all of those dies. PARENT
17810 is the parent of the die in question. */
17811
17812 static struct die_info *
17813 read_die_and_children (const struct die_reader_specs *reader,
17814 const gdb_byte *info_ptr,
17815 const gdb_byte **new_info_ptr,
17816 struct die_info *parent)
17817 {
17818 struct die_info *die;
17819 const gdb_byte *cur_ptr;
17820 int has_children;
17821
17822 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17823 if (die == NULL)
17824 {
17825 *new_info_ptr = cur_ptr;
17826 return NULL;
17827 }
17828 store_in_ref_table (die, reader->cu);
17829
17830 if (has_children)
17831 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17832 else
17833 {
17834 die->child = NULL;
17835 *new_info_ptr = cur_ptr;
17836 }
17837
17838 die->sibling = NULL;
17839 die->parent = parent;
17840 return die;
17841 }
17842
17843 /* Read a die, all of its descendents, and all of its siblings; set
17844 all of the fields of all of the dies correctly. Arguments are as
17845 in read_die_and_children. */
17846
17847 static struct die_info *
17848 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17849 const gdb_byte *info_ptr,
17850 const gdb_byte **new_info_ptr,
17851 struct die_info *parent)
17852 {
17853 struct die_info *first_die, *last_sibling;
17854 const gdb_byte *cur_ptr;
17855
17856 cur_ptr = info_ptr;
17857 first_die = last_sibling = NULL;
17858
17859 while (1)
17860 {
17861 struct die_info *die
17862 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17863
17864 if (die == NULL)
17865 {
17866 *new_info_ptr = cur_ptr;
17867 return first_die;
17868 }
17869
17870 if (!first_die)
17871 first_die = die;
17872 else
17873 last_sibling->sibling = die;
17874
17875 last_sibling = die;
17876 }
17877 }
17878
17879 /* Read a die, all of its descendents, and all of its siblings; set
17880 all of the fields of all of the dies correctly. Arguments are as
17881 in read_die_and_children.
17882 This the main entry point for reading a DIE and all its children. */
17883
17884 static struct die_info *
17885 read_die_and_siblings (const struct die_reader_specs *reader,
17886 const gdb_byte *info_ptr,
17887 const gdb_byte **new_info_ptr,
17888 struct die_info *parent)
17889 {
17890 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17891 new_info_ptr, parent);
17892
17893 if (dwarf_die_debug)
17894 {
17895 fprintf_unfiltered (gdb_stdlog,
17896 "Read die from %s@0x%x of %s:\n",
17897 get_section_name (reader->die_section),
17898 (unsigned) (info_ptr - reader->die_section->buffer),
17899 bfd_get_filename (reader->abfd));
17900 dump_die (die, dwarf_die_debug);
17901 }
17902
17903 return die;
17904 }
17905
17906 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17907 attributes.
17908 The caller is responsible for filling in the extra attributes
17909 and updating (*DIEP)->num_attrs.
17910 Set DIEP to point to a newly allocated die with its information,
17911 except for its child, sibling, and parent fields.
17912 Set HAS_CHILDREN to tell whether the die has children or not. */
17913
17914 static const gdb_byte *
17915 read_full_die_1 (const struct die_reader_specs *reader,
17916 struct die_info **diep, const gdb_byte *info_ptr,
17917 int *has_children, int num_extra_attrs)
17918 {
17919 unsigned int abbrev_number, bytes_read, i;
17920 struct abbrev_info *abbrev;
17921 struct die_info *die;
17922 struct dwarf2_cu *cu = reader->cu;
17923 bfd *abfd = reader->abfd;
17924
17925 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17926 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17927 info_ptr += bytes_read;
17928 if (!abbrev_number)
17929 {
17930 *diep = NULL;
17931 *has_children = 0;
17932 return info_ptr;
17933 }
17934
17935 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17936 if (!abbrev)
17937 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17938 abbrev_number,
17939 bfd_get_filename (abfd));
17940
17941 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17942 die->sect_off = sect_off;
17943 die->tag = abbrev->tag;
17944 die->abbrev = abbrev_number;
17945
17946 /* Make the result usable.
17947 The caller needs to update num_attrs after adding the extra
17948 attributes. */
17949 die->num_attrs = abbrev->num_attrs;
17950
17951 for (i = 0; i < abbrev->num_attrs; ++i)
17952 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17953 info_ptr);
17954
17955 *diep = die;
17956 *has_children = abbrev->has_children;
17957 return info_ptr;
17958 }
17959
17960 /* Read a die and all its attributes.
17961 Set DIEP to point to a newly allocated die with its information,
17962 except for its child, sibling, and parent fields.
17963 Set HAS_CHILDREN to tell whether the die has children or not. */
17964
17965 static const gdb_byte *
17966 read_full_die (const struct die_reader_specs *reader,
17967 struct die_info **diep, const gdb_byte *info_ptr,
17968 int *has_children)
17969 {
17970 const gdb_byte *result;
17971
17972 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17973
17974 if (dwarf_die_debug)
17975 {
17976 fprintf_unfiltered (gdb_stdlog,
17977 "Read die from %s@0x%x of %s:\n",
17978 get_section_name (reader->die_section),
17979 (unsigned) (info_ptr - reader->die_section->buffer),
17980 bfd_get_filename (reader->abfd));
17981 dump_die (*diep, dwarf_die_debug);
17982 }
17983
17984 return result;
17985 }
17986 \f
17987 /* Abbreviation tables.
17988
17989 In DWARF version 2, the description of the debugging information is
17990 stored in a separate .debug_abbrev section. Before we read any
17991 dies from a section we read in all abbreviations and install them
17992 in a hash table. */
17993
17994 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17995
17996 struct abbrev_info *
17997 abbrev_table::alloc_abbrev ()
17998 {
17999 struct abbrev_info *abbrev;
18000
18001 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18002 memset (abbrev, 0, sizeof (struct abbrev_info));
18003
18004 return abbrev;
18005 }
18006
18007 /* Add an abbreviation to the table. */
18008
18009 void
18010 abbrev_table::add_abbrev (unsigned int abbrev_number,
18011 struct abbrev_info *abbrev)
18012 {
18013 unsigned int hash_number;
18014
18015 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18016 abbrev->next = m_abbrevs[hash_number];
18017 m_abbrevs[hash_number] = abbrev;
18018 }
18019
18020 /* Look up an abbrev in the table.
18021 Returns NULL if the abbrev is not found. */
18022
18023 struct abbrev_info *
18024 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18025 {
18026 unsigned int hash_number;
18027 struct abbrev_info *abbrev;
18028
18029 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18030 abbrev = m_abbrevs[hash_number];
18031
18032 while (abbrev)
18033 {
18034 if (abbrev->number == abbrev_number)
18035 return abbrev;
18036 abbrev = abbrev->next;
18037 }
18038 return NULL;
18039 }
18040
18041 /* Read in an abbrev table. */
18042
18043 static abbrev_table_up
18044 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18045 struct dwarf2_section_info *section,
18046 sect_offset sect_off)
18047 {
18048 struct objfile *objfile = dwarf2_per_objfile->objfile;
18049 bfd *abfd = get_section_bfd_owner (section);
18050 const gdb_byte *abbrev_ptr;
18051 struct abbrev_info *cur_abbrev;
18052 unsigned int abbrev_number, bytes_read, abbrev_name;
18053 unsigned int abbrev_form;
18054 struct attr_abbrev *cur_attrs;
18055 unsigned int allocated_attrs;
18056
18057 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18058
18059 dwarf2_read_section (objfile, section);
18060 abbrev_ptr = section->buffer + to_underlying (sect_off);
18061 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18062 abbrev_ptr += bytes_read;
18063
18064 allocated_attrs = ATTR_ALLOC_CHUNK;
18065 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18066
18067 /* Loop until we reach an abbrev number of 0. */
18068 while (abbrev_number)
18069 {
18070 cur_abbrev = abbrev_table->alloc_abbrev ();
18071
18072 /* read in abbrev header */
18073 cur_abbrev->number = abbrev_number;
18074 cur_abbrev->tag
18075 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18076 abbrev_ptr += bytes_read;
18077 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18078 abbrev_ptr += 1;
18079
18080 /* now read in declarations */
18081 for (;;)
18082 {
18083 LONGEST implicit_const;
18084
18085 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18086 abbrev_ptr += bytes_read;
18087 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18088 abbrev_ptr += bytes_read;
18089 if (abbrev_form == DW_FORM_implicit_const)
18090 {
18091 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18092 &bytes_read);
18093 abbrev_ptr += bytes_read;
18094 }
18095 else
18096 {
18097 /* Initialize it due to a false compiler warning. */
18098 implicit_const = -1;
18099 }
18100
18101 if (abbrev_name == 0)
18102 break;
18103
18104 if (cur_abbrev->num_attrs == allocated_attrs)
18105 {
18106 allocated_attrs += ATTR_ALLOC_CHUNK;
18107 cur_attrs
18108 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18109 }
18110
18111 cur_attrs[cur_abbrev->num_attrs].name
18112 = (enum dwarf_attribute) abbrev_name;
18113 cur_attrs[cur_abbrev->num_attrs].form
18114 = (enum dwarf_form) abbrev_form;
18115 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18116 ++cur_abbrev->num_attrs;
18117 }
18118
18119 cur_abbrev->attrs =
18120 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18121 cur_abbrev->num_attrs);
18122 memcpy (cur_abbrev->attrs, cur_attrs,
18123 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18124
18125 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18126
18127 /* Get next abbreviation.
18128 Under Irix6 the abbreviations for a compilation unit are not
18129 always properly terminated with an abbrev number of 0.
18130 Exit loop if we encounter an abbreviation which we have
18131 already read (which means we are about to read the abbreviations
18132 for the next compile unit) or if the end of the abbreviation
18133 table is reached. */
18134 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18135 break;
18136 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18137 abbrev_ptr += bytes_read;
18138 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18139 break;
18140 }
18141
18142 xfree (cur_attrs);
18143 return abbrev_table;
18144 }
18145
18146 /* Returns nonzero if TAG represents a type that we might generate a partial
18147 symbol for. */
18148
18149 static int
18150 is_type_tag_for_partial (int tag)
18151 {
18152 switch (tag)
18153 {
18154 #if 0
18155 /* Some types that would be reasonable to generate partial symbols for,
18156 that we don't at present. */
18157 case DW_TAG_array_type:
18158 case DW_TAG_file_type:
18159 case DW_TAG_ptr_to_member_type:
18160 case DW_TAG_set_type:
18161 case DW_TAG_string_type:
18162 case DW_TAG_subroutine_type:
18163 #endif
18164 case DW_TAG_base_type:
18165 case DW_TAG_class_type:
18166 case DW_TAG_interface_type:
18167 case DW_TAG_enumeration_type:
18168 case DW_TAG_structure_type:
18169 case DW_TAG_subrange_type:
18170 case DW_TAG_typedef:
18171 case DW_TAG_union_type:
18172 return 1;
18173 default:
18174 return 0;
18175 }
18176 }
18177
18178 /* Load all DIEs that are interesting for partial symbols into memory. */
18179
18180 static struct partial_die_info *
18181 load_partial_dies (const struct die_reader_specs *reader,
18182 const gdb_byte *info_ptr, int building_psymtab)
18183 {
18184 struct dwarf2_cu *cu = reader->cu;
18185 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18186 struct partial_die_info *part_die;
18187 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18188 unsigned int bytes_read;
18189 unsigned int load_all = 0;
18190 int nesting_level = 1;
18191
18192 parent_die = NULL;
18193 last_die = NULL;
18194
18195 gdb_assert (cu->per_cu != NULL);
18196 if (cu->per_cu->load_all_dies)
18197 load_all = 1;
18198
18199 cu->partial_dies
18200 = htab_create_alloc_ex (cu->header.length / 12,
18201 partial_die_hash,
18202 partial_die_eq,
18203 NULL,
18204 &cu->comp_unit_obstack,
18205 hashtab_obstack_allocate,
18206 dummy_obstack_deallocate);
18207
18208 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
18209
18210 while (1)
18211 {
18212 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18213
18214 /* A NULL abbrev means the end of a series of children. */
18215 if (abbrev == NULL)
18216 {
18217 if (--nesting_level == 0)
18218 {
18219 /* PART_DIE was probably the last thing allocated on the
18220 comp_unit_obstack, so we could call obstack_free
18221 here. We don't do that because the waste is small,
18222 and will be cleaned up when we're done with this
18223 compilation unit. This way, we're also more robust
18224 against other users of the comp_unit_obstack. */
18225 return first_die;
18226 }
18227 info_ptr += bytes_read;
18228 last_die = parent_die;
18229 parent_die = parent_die->die_parent;
18230 continue;
18231 }
18232
18233 /* Check for template arguments. We never save these; if
18234 they're seen, we just mark the parent, and go on our way. */
18235 if (parent_die != NULL
18236 && cu->language == language_cplus
18237 && (abbrev->tag == DW_TAG_template_type_param
18238 || abbrev->tag == DW_TAG_template_value_param))
18239 {
18240 parent_die->has_template_arguments = 1;
18241
18242 if (!load_all)
18243 {
18244 /* We don't need a partial DIE for the template argument. */
18245 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18246 continue;
18247 }
18248 }
18249
18250 /* We only recurse into c++ subprograms looking for template arguments.
18251 Skip their other children. */
18252 if (!load_all
18253 && cu->language == language_cplus
18254 && parent_die != NULL
18255 && parent_die->tag == DW_TAG_subprogram)
18256 {
18257 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18258 continue;
18259 }
18260
18261 /* Check whether this DIE is interesting enough to save. Normally
18262 we would not be interested in members here, but there may be
18263 later variables referencing them via DW_AT_specification (for
18264 static members). */
18265 if (!load_all
18266 && !is_type_tag_for_partial (abbrev->tag)
18267 && abbrev->tag != DW_TAG_constant
18268 && abbrev->tag != DW_TAG_enumerator
18269 && abbrev->tag != DW_TAG_subprogram
18270 && abbrev->tag != DW_TAG_inlined_subroutine
18271 && abbrev->tag != DW_TAG_lexical_block
18272 && abbrev->tag != DW_TAG_variable
18273 && abbrev->tag != DW_TAG_namespace
18274 && abbrev->tag != DW_TAG_module
18275 && abbrev->tag != DW_TAG_member
18276 && abbrev->tag != DW_TAG_imported_unit
18277 && abbrev->tag != DW_TAG_imported_declaration)
18278 {
18279 /* Otherwise we skip to the next sibling, if any. */
18280 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18281 continue;
18282 }
18283
18284 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
18285 info_ptr);
18286
18287 /* This two-pass algorithm for processing partial symbols has a
18288 high cost in cache pressure. Thus, handle some simple cases
18289 here which cover the majority of C partial symbols. DIEs
18290 which neither have specification tags in them, nor could have
18291 specification tags elsewhere pointing at them, can simply be
18292 processed and discarded.
18293
18294 This segment is also optional; scan_partial_symbols and
18295 add_partial_symbol will handle these DIEs if we chain
18296 them in normally. When compilers which do not emit large
18297 quantities of duplicate debug information are more common,
18298 this code can probably be removed. */
18299
18300 /* Any complete simple types at the top level (pretty much all
18301 of them, for a language without namespaces), can be processed
18302 directly. */
18303 if (parent_die == NULL
18304 && part_die->has_specification == 0
18305 && part_die->is_declaration == 0
18306 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
18307 || part_die->tag == DW_TAG_base_type
18308 || part_die->tag == DW_TAG_subrange_type))
18309 {
18310 if (building_psymtab && part_die->name != NULL)
18311 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
18312 VAR_DOMAIN, LOC_TYPEDEF,
18313 &objfile->static_psymbols,
18314 0, cu->language, objfile);
18315 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
18316 continue;
18317 }
18318
18319 /* The exception for DW_TAG_typedef with has_children above is
18320 a workaround of GCC PR debug/47510. In the case of this complaint
18321 type_name_no_tag_or_error will error on such types later.
18322
18323 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18324 it could not find the child DIEs referenced later, this is checked
18325 above. In correct DWARF DW_TAG_typedef should have no children. */
18326
18327 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
18328 complaint (&symfile_complaints,
18329 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18330 "- DIE at 0x%x [in module %s]"),
18331 to_underlying (part_die->sect_off), objfile_name (objfile));
18332
18333 /* If we're at the second level, and we're an enumerator, and
18334 our parent has no specification (meaning possibly lives in a
18335 namespace elsewhere), then we can add the partial symbol now
18336 instead of queueing it. */
18337 if (part_die->tag == DW_TAG_enumerator
18338 && parent_die != NULL
18339 && parent_die->die_parent == NULL
18340 && parent_die->tag == DW_TAG_enumeration_type
18341 && parent_die->has_specification == 0)
18342 {
18343 if (part_die->name == NULL)
18344 complaint (&symfile_complaints,
18345 _("malformed enumerator DIE ignored"));
18346 else if (building_psymtab)
18347 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
18348 VAR_DOMAIN, LOC_CONST,
18349 cu->language == language_cplus
18350 ? &objfile->global_psymbols
18351 : &objfile->static_psymbols,
18352 0, cu->language, objfile);
18353
18354 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
18355 continue;
18356 }
18357
18358 /* We'll save this DIE so link it in. */
18359 part_die->die_parent = parent_die;
18360 part_die->die_sibling = NULL;
18361 part_die->die_child = NULL;
18362
18363 if (last_die && last_die == parent_die)
18364 last_die->die_child = part_die;
18365 else if (last_die)
18366 last_die->die_sibling = part_die;
18367
18368 last_die = part_die;
18369
18370 if (first_die == NULL)
18371 first_die = part_die;
18372
18373 /* Maybe add the DIE to the hash table. Not all DIEs that we
18374 find interesting need to be in the hash table, because we
18375 also have the parent/sibling/child chains; only those that we
18376 might refer to by offset later during partial symbol reading.
18377
18378 For now this means things that might have be the target of a
18379 DW_AT_specification, DW_AT_abstract_origin, or
18380 DW_AT_extension. DW_AT_extension will refer only to
18381 namespaces; DW_AT_abstract_origin refers to functions (and
18382 many things under the function DIE, but we do not recurse
18383 into function DIEs during partial symbol reading) and
18384 possibly variables as well; DW_AT_specification refers to
18385 declarations. Declarations ought to have the DW_AT_declaration
18386 flag. It happens that GCC forgets to put it in sometimes, but
18387 only for functions, not for types.
18388
18389 Adding more things than necessary to the hash table is harmless
18390 except for the performance cost. Adding too few will result in
18391 wasted time in find_partial_die, when we reread the compilation
18392 unit with load_all_dies set. */
18393
18394 if (load_all
18395 || abbrev->tag == DW_TAG_constant
18396 || abbrev->tag == DW_TAG_subprogram
18397 || abbrev->tag == DW_TAG_variable
18398 || abbrev->tag == DW_TAG_namespace
18399 || part_die->is_declaration)
18400 {
18401 void **slot;
18402
18403 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18404 to_underlying (part_die->sect_off),
18405 INSERT);
18406 *slot = part_die;
18407 }
18408
18409 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
18410
18411 /* For some DIEs we want to follow their children (if any). For C
18412 we have no reason to follow the children of structures; for other
18413 languages we have to, so that we can get at method physnames
18414 to infer fully qualified class names, for DW_AT_specification,
18415 and for C++ template arguments. For C++, we also look one level
18416 inside functions to find template arguments (if the name of the
18417 function does not already contain the template arguments).
18418
18419 For Ada, we need to scan the children of subprograms and lexical
18420 blocks as well because Ada allows the definition of nested
18421 entities that could be interesting for the debugger, such as
18422 nested subprograms for instance. */
18423 if (last_die->has_children
18424 && (load_all
18425 || last_die->tag == DW_TAG_namespace
18426 || last_die->tag == DW_TAG_module
18427 || last_die->tag == DW_TAG_enumeration_type
18428 || (cu->language == language_cplus
18429 && last_die->tag == DW_TAG_subprogram
18430 && (last_die->name == NULL
18431 || strchr (last_die->name, '<') == NULL))
18432 || (cu->language != language_c
18433 && (last_die->tag == DW_TAG_class_type
18434 || last_die->tag == DW_TAG_interface_type
18435 || last_die->tag == DW_TAG_structure_type
18436 || last_die->tag == DW_TAG_union_type))
18437 || (cu->language == language_ada
18438 && (last_die->tag == DW_TAG_subprogram
18439 || last_die->tag == DW_TAG_lexical_block))))
18440 {
18441 nesting_level++;
18442 parent_die = last_die;
18443 continue;
18444 }
18445
18446 /* Otherwise we skip to the next sibling, if any. */
18447 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18448
18449 /* Back to the top, do it again. */
18450 }
18451 }
18452
18453 /* Read a minimal amount of information into the minimal die structure. */
18454
18455 static const gdb_byte *
18456 read_partial_die (const struct die_reader_specs *reader,
18457 struct partial_die_info *part_die,
18458 struct abbrev_info *abbrev, unsigned int abbrev_len,
18459 const gdb_byte *info_ptr)
18460 {
18461 struct dwarf2_cu *cu = reader->cu;
18462 struct dwarf2_per_objfile *dwarf2_per_objfile
18463 = cu->per_cu->dwarf2_per_objfile;
18464 struct objfile *objfile = dwarf2_per_objfile->objfile;
18465 const gdb_byte *buffer = reader->buffer;
18466 unsigned int i;
18467 struct attribute attr;
18468 int has_low_pc_attr = 0;
18469 int has_high_pc_attr = 0;
18470 int high_pc_relative = 0;
18471
18472 memset (part_die, 0, sizeof (struct partial_die_info));
18473
18474 part_die->sect_off = (sect_offset) (info_ptr - buffer);
18475
18476 info_ptr += abbrev_len;
18477
18478 if (abbrev == NULL)
18479 return info_ptr;
18480
18481 part_die->tag = abbrev->tag;
18482 part_die->has_children = abbrev->has_children;
18483
18484 for (i = 0; i < abbrev->num_attrs; ++i)
18485 {
18486 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
18487
18488 /* Store the data if it is of an attribute we want to keep in a
18489 partial symbol table. */
18490 switch (attr.name)
18491 {
18492 case DW_AT_name:
18493 switch (part_die->tag)
18494 {
18495 case DW_TAG_compile_unit:
18496 case DW_TAG_partial_unit:
18497 case DW_TAG_type_unit:
18498 /* Compilation units have a DW_AT_name that is a filename, not
18499 a source language identifier. */
18500 case DW_TAG_enumeration_type:
18501 case DW_TAG_enumerator:
18502 /* These tags always have simple identifiers already; no need
18503 to canonicalize them. */
18504 part_die->name = DW_STRING (&attr);
18505 break;
18506 default:
18507 part_die->name
18508 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18509 &objfile->per_bfd->storage_obstack);
18510 break;
18511 }
18512 break;
18513 case DW_AT_linkage_name:
18514 case DW_AT_MIPS_linkage_name:
18515 /* Note that both forms of linkage name might appear. We
18516 assume they will be the same, and we only store the last
18517 one we see. */
18518 if (cu->language == language_ada)
18519 part_die->name = DW_STRING (&attr);
18520 part_die->linkage_name = DW_STRING (&attr);
18521 break;
18522 case DW_AT_low_pc:
18523 has_low_pc_attr = 1;
18524 part_die->lowpc = attr_value_as_address (&attr);
18525 break;
18526 case DW_AT_high_pc:
18527 has_high_pc_attr = 1;
18528 part_die->highpc = attr_value_as_address (&attr);
18529 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18530 high_pc_relative = 1;
18531 break;
18532 case DW_AT_location:
18533 /* Support the .debug_loc offsets. */
18534 if (attr_form_is_block (&attr))
18535 {
18536 part_die->d.locdesc = DW_BLOCK (&attr);
18537 }
18538 else if (attr_form_is_section_offset (&attr))
18539 {
18540 dwarf2_complex_location_expr_complaint ();
18541 }
18542 else
18543 {
18544 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18545 "partial symbol information");
18546 }
18547 break;
18548 case DW_AT_external:
18549 part_die->is_external = DW_UNSND (&attr);
18550 break;
18551 case DW_AT_declaration:
18552 part_die->is_declaration = DW_UNSND (&attr);
18553 break;
18554 case DW_AT_type:
18555 part_die->has_type = 1;
18556 break;
18557 case DW_AT_abstract_origin:
18558 case DW_AT_specification:
18559 case DW_AT_extension:
18560 part_die->has_specification = 1;
18561 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
18562 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18563 || cu->per_cu->is_dwz);
18564 break;
18565 case DW_AT_sibling:
18566 /* Ignore absolute siblings, they might point outside of
18567 the current compile unit. */
18568 if (attr.form == DW_FORM_ref_addr)
18569 complaint (&symfile_complaints,
18570 _("ignoring absolute DW_AT_sibling"));
18571 else
18572 {
18573 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18574 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18575
18576 if (sibling_ptr < info_ptr)
18577 complaint (&symfile_complaints,
18578 _("DW_AT_sibling points backwards"));
18579 else if (sibling_ptr > reader->buffer_end)
18580 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18581 else
18582 part_die->sibling = sibling_ptr;
18583 }
18584 break;
18585 case DW_AT_byte_size:
18586 part_die->has_byte_size = 1;
18587 break;
18588 case DW_AT_const_value:
18589 part_die->has_const_value = 1;
18590 break;
18591 case DW_AT_calling_convention:
18592 /* DWARF doesn't provide a way to identify a program's source-level
18593 entry point. DW_AT_calling_convention attributes are only meant
18594 to describe functions' calling conventions.
18595
18596 However, because it's a necessary piece of information in
18597 Fortran, and before DWARF 4 DW_CC_program was the only
18598 piece of debugging information whose definition refers to
18599 a 'main program' at all, several compilers marked Fortran
18600 main programs with DW_CC_program --- even when those
18601 functions use the standard calling conventions.
18602
18603 Although DWARF now specifies a way to provide this
18604 information, we support this practice for backward
18605 compatibility. */
18606 if (DW_UNSND (&attr) == DW_CC_program
18607 && cu->language == language_fortran)
18608 part_die->main_subprogram = 1;
18609 break;
18610 case DW_AT_inline:
18611 if (DW_UNSND (&attr) == DW_INL_inlined
18612 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18613 part_die->may_be_inlined = 1;
18614 break;
18615
18616 case DW_AT_import:
18617 if (part_die->tag == DW_TAG_imported_unit)
18618 {
18619 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
18620 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18621 || cu->per_cu->is_dwz);
18622 }
18623 break;
18624
18625 case DW_AT_main_subprogram:
18626 part_die->main_subprogram = DW_UNSND (&attr);
18627 break;
18628
18629 default:
18630 break;
18631 }
18632 }
18633
18634 if (high_pc_relative)
18635 part_die->highpc += part_die->lowpc;
18636
18637 if (has_low_pc_attr && has_high_pc_attr)
18638 {
18639 /* When using the GNU linker, .gnu.linkonce. sections are used to
18640 eliminate duplicate copies of functions and vtables and such.
18641 The linker will arbitrarily choose one and discard the others.
18642 The AT_*_pc values for such functions refer to local labels in
18643 these sections. If the section from that file was discarded, the
18644 labels are not in the output, so the relocs get a value of 0.
18645 If this is a discarded function, mark the pc bounds as invalid,
18646 so that GDB will ignore it. */
18647 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18648 {
18649 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18650
18651 complaint (&symfile_complaints,
18652 _("DW_AT_low_pc %s is zero "
18653 "for DIE at 0x%x [in module %s]"),
18654 paddress (gdbarch, part_die->lowpc),
18655 to_underlying (part_die->sect_off), objfile_name (objfile));
18656 }
18657 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18658 else if (part_die->lowpc >= part_die->highpc)
18659 {
18660 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18661
18662 complaint (&symfile_complaints,
18663 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18664 "for DIE at 0x%x [in module %s]"),
18665 paddress (gdbarch, part_die->lowpc),
18666 paddress (gdbarch, part_die->highpc),
18667 to_underlying (part_die->sect_off),
18668 objfile_name (objfile));
18669 }
18670 else
18671 part_die->has_pc_info = 1;
18672 }
18673
18674 return info_ptr;
18675 }
18676
18677 /* Find a cached partial DIE at OFFSET in CU. */
18678
18679 static struct partial_die_info *
18680 find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
18681 {
18682 struct partial_die_info *lookup_die = NULL;
18683 struct partial_die_info part_die;
18684
18685 part_die.sect_off = sect_off;
18686 lookup_die = ((struct partial_die_info *)
18687 htab_find_with_hash (cu->partial_dies, &part_die,
18688 to_underlying (sect_off)));
18689
18690 return lookup_die;
18691 }
18692
18693 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18694 except in the case of .debug_types DIEs which do not reference
18695 outside their CU (they do however referencing other types via
18696 DW_FORM_ref_sig8). */
18697
18698 static struct partial_die_info *
18699 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18700 {
18701 struct dwarf2_per_objfile *dwarf2_per_objfile
18702 = cu->per_cu->dwarf2_per_objfile;
18703 struct objfile *objfile = dwarf2_per_objfile->objfile;
18704 struct dwarf2_per_cu_data *per_cu = NULL;
18705 struct partial_die_info *pd = NULL;
18706
18707 if (offset_in_dwz == cu->per_cu->is_dwz
18708 && offset_in_cu_p (&cu->header, sect_off))
18709 {
18710 pd = find_partial_die_in_comp_unit (sect_off, cu);
18711 if (pd != NULL)
18712 return pd;
18713 /* We missed recording what we needed.
18714 Load all dies and try again. */
18715 per_cu = cu->per_cu;
18716 }
18717 else
18718 {
18719 /* TUs don't reference other CUs/TUs (except via type signatures). */
18720 if (cu->per_cu->is_debug_types)
18721 {
18722 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
18723 " external reference to offset 0x%x [in module %s].\n"),
18724 to_underlying (cu->header.sect_off), to_underlying (sect_off),
18725 bfd_get_filename (objfile->obfd));
18726 }
18727 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18728 dwarf2_per_objfile);
18729
18730 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18731 load_partial_comp_unit (per_cu);
18732
18733 per_cu->cu->last_used = 0;
18734 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
18735 }
18736
18737 /* If we didn't find it, and not all dies have been loaded,
18738 load them all and try again. */
18739
18740 if (pd == NULL && per_cu->load_all_dies == 0)
18741 {
18742 per_cu->load_all_dies = 1;
18743
18744 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18745 THIS_CU->cu may already be in use. So we can't just free it and
18746 replace its DIEs with the ones we read in. Instead, we leave those
18747 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18748 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18749 set. */
18750 load_partial_comp_unit (per_cu);
18751
18752 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
18753 }
18754
18755 if (pd == NULL)
18756 internal_error (__FILE__, __LINE__,
18757 _("could not find partial DIE 0x%x "
18758 "in cache [from module %s]\n"),
18759 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
18760 return pd;
18761 }
18762
18763 /* See if we can figure out if the class lives in a namespace. We do
18764 this by looking for a member function; its demangled name will
18765 contain namespace info, if there is any. */
18766
18767 static void
18768 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18769 struct dwarf2_cu *cu)
18770 {
18771 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18772 what template types look like, because the demangler
18773 frequently doesn't give the same name as the debug info. We
18774 could fix this by only using the demangled name to get the
18775 prefix (but see comment in read_structure_type). */
18776
18777 struct partial_die_info *real_pdi;
18778 struct partial_die_info *child_pdi;
18779
18780 /* If this DIE (this DIE's specification, if any) has a parent, then
18781 we should not do this. We'll prepend the parent's fully qualified
18782 name when we create the partial symbol. */
18783
18784 real_pdi = struct_pdi;
18785 while (real_pdi->has_specification)
18786 real_pdi = find_partial_die (real_pdi->spec_offset,
18787 real_pdi->spec_is_dwz, cu);
18788
18789 if (real_pdi->die_parent != NULL)
18790 return;
18791
18792 for (child_pdi = struct_pdi->die_child;
18793 child_pdi != NULL;
18794 child_pdi = child_pdi->die_sibling)
18795 {
18796 if (child_pdi->tag == DW_TAG_subprogram
18797 && child_pdi->linkage_name != NULL)
18798 {
18799 char *actual_class_name
18800 = language_class_name_from_physname (cu->language_defn,
18801 child_pdi->linkage_name);
18802 if (actual_class_name != NULL)
18803 {
18804 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18805 struct_pdi->name
18806 = ((const char *)
18807 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18808 actual_class_name,
18809 strlen (actual_class_name)));
18810 xfree (actual_class_name);
18811 }
18812 break;
18813 }
18814 }
18815 }
18816
18817 /* Adjust PART_DIE before generating a symbol for it. This function
18818 may set the is_external flag or change the DIE's name. */
18819
18820 static void
18821 fixup_partial_die (struct partial_die_info *part_die,
18822 struct dwarf2_cu *cu)
18823 {
18824 /* Once we've fixed up a die, there's no point in doing so again.
18825 This also avoids a memory leak if we were to call
18826 guess_partial_die_structure_name multiple times. */
18827 if (part_die->fixup_called)
18828 return;
18829
18830 /* If we found a reference attribute and the DIE has no name, try
18831 to find a name in the referred to DIE. */
18832
18833 if (part_die->name == NULL && part_die->has_specification)
18834 {
18835 struct partial_die_info *spec_die;
18836
18837 spec_die = find_partial_die (part_die->spec_offset,
18838 part_die->spec_is_dwz, cu);
18839
18840 fixup_partial_die (spec_die, cu);
18841
18842 if (spec_die->name)
18843 {
18844 part_die->name = spec_die->name;
18845
18846 /* Copy DW_AT_external attribute if it is set. */
18847 if (spec_die->is_external)
18848 part_die->is_external = spec_die->is_external;
18849 }
18850 }
18851
18852 /* Set default names for some unnamed DIEs. */
18853
18854 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
18855 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
18856
18857 /* If there is no parent die to provide a namespace, and there are
18858 children, see if we can determine the namespace from their linkage
18859 name. */
18860 if (cu->language == language_cplus
18861 && !VEC_empty (dwarf2_section_info_def,
18862 cu->per_cu->dwarf2_per_objfile->types)
18863 && part_die->die_parent == NULL
18864 && part_die->has_children
18865 && (part_die->tag == DW_TAG_class_type
18866 || part_die->tag == DW_TAG_structure_type
18867 || part_die->tag == DW_TAG_union_type))
18868 guess_partial_die_structure_name (part_die, cu);
18869
18870 /* GCC might emit a nameless struct or union that has a linkage
18871 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18872 if (part_die->name == NULL
18873 && (part_die->tag == DW_TAG_class_type
18874 || part_die->tag == DW_TAG_interface_type
18875 || part_die->tag == DW_TAG_structure_type
18876 || part_die->tag == DW_TAG_union_type)
18877 && part_die->linkage_name != NULL)
18878 {
18879 char *demangled;
18880
18881 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
18882 if (demangled)
18883 {
18884 const char *base;
18885
18886 /* Strip any leading namespaces/classes, keep only the base name.
18887 DW_AT_name for named DIEs does not contain the prefixes. */
18888 base = strrchr (demangled, ':');
18889 if (base && base > demangled && base[-1] == ':')
18890 base++;
18891 else
18892 base = demangled;
18893
18894 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18895 part_die->name
18896 = ((const char *)
18897 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18898 base, strlen (base)));
18899 xfree (demangled);
18900 }
18901 }
18902
18903 part_die->fixup_called = 1;
18904 }
18905
18906 /* Read an attribute value described by an attribute form. */
18907
18908 static const gdb_byte *
18909 read_attribute_value (const struct die_reader_specs *reader,
18910 struct attribute *attr, unsigned form,
18911 LONGEST implicit_const, const gdb_byte *info_ptr)
18912 {
18913 struct dwarf2_cu *cu = reader->cu;
18914 struct dwarf2_per_objfile *dwarf2_per_objfile
18915 = cu->per_cu->dwarf2_per_objfile;
18916 struct objfile *objfile = dwarf2_per_objfile->objfile;
18917 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18918 bfd *abfd = reader->abfd;
18919 struct comp_unit_head *cu_header = &cu->header;
18920 unsigned int bytes_read;
18921 struct dwarf_block *blk;
18922
18923 attr->form = (enum dwarf_form) form;
18924 switch (form)
18925 {
18926 case DW_FORM_ref_addr:
18927 if (cu->header.version == 2)
18928 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18929 else
18930 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18931 &cu->header, &bytes_read);
18932 info_ptr += bytes_read;
18933 break;
18934 case DW_FORM_GNU_ref_alt:
18935 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18936 info_ptr += bytes_read;
18937 break;
18938 case DW_FORM_addr:
18939 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18940 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18941 info_ptr += bytes_read;
18942 break;
18943 case DW_FORM_block2:
18944 blk = dwarf_alloc_block (cu);
18945 blk->size = read_2_bytes (abfd, info_ptr);
18946 info_ptr += 2;
18947 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18948 info_ptr += blk->size;
18949 DW_BLOCK (attr) = blk;
18950 break;
18951 case DW_FORM_block4:
18952 blk = dwarf_alloc_block (cu);
18953 blk->size = read_4_bytes (abfd, info_ptr);
18954 info_ptr += 4;
18955 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18956 info_ptr += blk->size;
18957 DW_BLOCK (attr) = blk;
18958 break;
18959 case DW_FORM_data2:
18960 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18961 info_ptr += 2;
18962 break;
18963 case DW_FORM_data4:
18964 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18965 info_ptr += 4;
18966 break;
18967 case DW_FORM_data8:
18968 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18969 info_ptr += 8;
18970 break;
18971 case DW_FORM_data16:
18972 blk = dwarf_alloc_block (cu);
18973 blk->size = 16;
18974 blk->data = read_n_bytes (abfd, info_ptr, 16);
18975 info_ptr += 16;
18976 DW_BLOCK (attr) = blk;
18977 break;
18978 case DW_FORM_sec_offset:
18979 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18980 info_ptr += bytes_read;
18981 break;
18982 case DW_FORM_string:
18983 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18984 DW_STRING_IS_CANONICAL (attr) = 0;
18985 info_ptr += bytes_read;
18986 break;
18987 case DW_FORM_strp:
18988 if (!cu->per_cu->is_dwz)
18989 {
18990 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18991 abfd, info_ptr, cu_header,
18992 &bytes_read);
18993 DW_STRING_IS_CANONICAL (attr) = 0;
18994 info_ptr += bytes_read;
18995 break;
18996 }
18997 /* FALLTHROUGH */
18998 case DW_FORM_line_strp:
18999 if (!cu->per_cu->is_dwz)
19000 {
19001 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19002 abfd, info_ptr,
19003 cu_header, &bytes_read);
19004 DW_STRING_IS_CANONICAL (attr) = 0;
19005 info_ptr += bytes_read;
19006 break;
19007 }
19008 /* FALLTHROUGH */
19009 case DW_FORM_GNU_strp_alt:
19010 {
19011 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19012 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19013 &bytes_read);
19014
19015 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19016 dwz, str_offset);
19017 DW_STRING_IS_CANONICAL (attr) = 0;
19018 info_ptr += bytes_read;
19019 }
19020 break;
19021 case DW_FORM_exprloc:
19022 case DW_FORM_block:
19023 blk = dwarf_alloc_block (cu);
19024 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19025 info_ptr += bytes_read;
19026 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19027 info_ptr += blk->size;
19028 DW_BLOCK (attr) = blk;
19029 break;
19030 case DW_FORM_block1:
19031 blk = dwarf_alloc_block (cu);
19032 blk->size = read_1_byte (abfd, info_ptr);
19033 info_ptr += 1;
19034 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19035 info_ptr += blk->size;
19036 DW_BLOCK (attr) = blk;
19037 break;
19038 case DW_FORM_data1:
19039 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19040 info_ptr += 1;
19041 break;
19042 case DW_FORM_flag:
19043 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19044 info_ptr += 1;
19045 break;
19046 case DW_FORM_flag_present:
19047 DW_UNSND (attr) = 1;
19048 break;
19049 case DW_FORM_sdata:
19050 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19051 info_ptr += bytes_read;
19052 break;
19053 case DW_FORM_udata:
19054 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19055 info_ptr += bytes_read;
19056 break;
19057 case DW_FORM_ref1:
19058 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19059 + read_1_byte (abfd, info_ptr));
19060 info_ptr += 1;
19061 break;
19062 case DW_FORM_ref2:
19063 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19064 + read_2_bytes (abfd, info_ptr));
19065 info_ptr += 2;
19066 break;
19067 case DW_FORM_ref4:
19068 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19069 + read_4_bytes (abfd, info_ptr));
19070 info_ptr += 4;
19071 break;
19072 case DW_FORM_ref8:
19073 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19074 + read_8_bytes (abfd, info_ptr));
19075 info_ptr += 8;
19076 break;
19077 case DW_FORM_ref_sig8:
19078 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19079 info_ptr += 8;
19080 break;
19081 case DW_FORM_ref_udata:
19082 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19083 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19084 info_ptr += bytes_read;
19085 break;
19086 case DW_FORM_indirect:
19087 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19088 info_ptr += bytes_read;
19089 if (form == DW_FORM_implicit_const)
19090 {
19091 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19092 info_ptr += bytes_read;
19093 }
19094 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19095 info_ptr);
19096 break;
19097 case DW_FORM_implicit_const:
19098 DW_SND (attr) = implicit_const;
19099 break;
19100 case DW_FORM_GNU_addr_index:
19101 if (reader->dwo_file == NULL)
19102 {
19103 /* For now flag a hard error.
19104 Later we can turn this into a complaint. */
19105 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19106 dwarf_form_name (form),
19107 bfd_get_filename (abfd));
19108 }
19109 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19110 info_ptr += bytes_read;
19111 break;
19112 case DW_FORM_GNU_str_index:
19113 if (reader->dwo_file == NULL)
19114 {
19115 /* For now flag a hard error.
19116 Later we can turn this into a complaint if warranted. */
19117 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19118 dwarf_form_name (form),
19119 bfd_get_filename (abfd));
19120 }
19121 {
19122 ULONGEST str_index =
19123 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19124
19125 DW_STRING (attr) = read_str_index (reader, str_index);
19126 DW_STRING_IS_CANONICAL (attr) = 0;
19127 info_ptr += bytes_read;
19128 }
19129 break;
19130 default:
19131 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19132 dwarf_form_name (form),
19133 bfd_get_filename (abfd));
19134 }
19135
19136 /* Super hack. */
19137 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19138 attr->form = DW_FORM_GNU_ref_alt;
19139
19140 /* We have seen instances where the compiler tried to emit a byte
19141 size attribute of -1 which ended up being encoded as an unsigned
19142 0xffffffff. Although 0xffffffff is technically a valid size value,
19143 an object of this size seems pretty unlikely so we can relatively
19144 safely treat these cases as if the size attribute was invalid and
19145 treat them as zero by default. */
19146 if (attr->name == DW_AT_byte_size
19147 && form == DW_FORM_data4
19148 && DW_UNSND (attr) >= 0xffffffff)
19149 {
19150 complaint
19151 (&symfile_complaints,
19152 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19153 hex_string (DW_UNSND (attr)));
19154 DW_UNSND (attr) = 0;
19155 }
19156
19157 return info_ptr;
19158 }
19159
19160 /* Read an attribute described by an abbreviated attribute. */
19161
19162 static const gdb_byte *
19163 read_attribute (const struct die_reader_specs *reader,
19164 struct attribute *attr, struct attr_abbrev *abbrev,
19165 const gdb_byte *info_ptr)
19166 {
19167 attr->name = abbrev->name;
19168 return read_attribute_value (reader, attr, abbrev->form,
19169 abbrev->implicit_const, info_ptr);
19170 }
19171
19172 /* Read dwarf information from a buffer. */
19173
19174 static unsigned int
19175 read_1_byte (bfd *abfd, const gdb_byte *buf)
19176 {
19177 return bfd_get_8 (abfd, buf);
19178 }
19179
19180 static int
19181 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19182 {
19183 return bfd_get_signed_8 (abfd, buf);
19184 }
19185
19186 static unsigned int
19187 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19188 {
19189 return bfd_get_16 (abfd, buf);
19190 }
19191
19192 static int
19193 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19194 {
19195 return bfd_get_signed_16 (abfd, buf);
19196 }
19197
19198 static unsigned int
19199 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19200 {
19201 return bfd_get_32 (abfd, buf);
19202 }
19203
19204 static int
19205 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19206 {
19207 return bfd_get_signed_32 (abfd, buf);
19208 }
19209
19210 static ULONGEST
19211 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19212 {
19213 return bfd_get_64 (abfd, buf);
19214 }
19215
19216 static CORE_ADDR
19217 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19218 unsigned int *bytes_read)
19219 {
19220 struct comp_unit_head *cu_header = &cu->header;
19221 CORE_ADDR retval = 0;
19222
19223 if (cu_header->signed_addr_p)
19224 {
19225 switch (cu_header->addr_size)
19226 {
19227 case 2:
19228 retval = bfd_get_signed_16 (abfd, buf);
19229 break;
19230 case 4:
19231 retval = bfd_get_signed_32 (abfd, buf);
19232 break;
19233 case 8:
19234 retval = bfd_get_signed_64 (abfd, buf);
19235 break;
19236 default:
19237 internal_error (__FILE__, __LINE__,
19238 _("read_address: bad switch, signed [in module %s]"),
19239 bfd_get_filename (abfd));
19240 }
19241 }
19242 else
19243 {
19244 switch (cu_header->addr_size)
19245 {
19246 case 2:
19247 retval = bfd_get_16 (abfd, buf);
19248 break;
19249 case 4:
19250 retval = bfd_get_32 (abfd, buf);
19251 break;
19252 case 8:
19253 retval = bfd_get_64 (abfd, buf);
19254 break;
19255 default:
19256 internal_error (__FILE__, __LINE__,
19257 _("read_address: bad switch, "
19258 "unsigned [in module %s]"),
19259 bfd_get_filename (abfd));
19260 }
19261 }
19262
19263 *bytes_read = cu_header->addr_size;
19264 return retval;
19265 }
19266
19267 /* Read the initial length from a section. The (draft) DWARF 3
19268 specification allows the initial length to take up either 4 bytes
19269 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19270 bytes describe the length and all offsets will be 8 bytes in length
19271 instead of 4.
19272
19273 An older, non-standard 64-bit format is also handled by this
19274 function. The older format in question stores the initial length
19275 as an 8-byte quantity without an escape value. Lengths greater
19276 than 2^32 aren't very common which means that the initial 4 bytes
19277 is almost always zero. Since a length value of zero doesn't make
19278 sense for the 32-bit format, this initial zero can be considered to
19279 be an escape value which indicates the presence of the older 64-bit
19280 format. As written, the code can't detect (old format) lengths
19281 greater than 4GB. If it becomes necessary to handle lengths
19282 somewhat larger than 4GB, we could allow other small values (such
19283 as the non-sensical values of 1, 2, and 3) to also be used as
19284 escape values indicating the presence of the old format.
19285
19286 The value returned via bytes_read should be used to increment the
19287 relevant pointer after calling read_initial_length().
19288
19289 [ Note: read_initial_length() and read_offset() are based on the
19290 document entitled "DWARF Debugging Information Format", revision
19291 3, draft 8, dated November 19, 2001. This document was obtained
19292 from:
19293
19294 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19295
19296 This document is only a draft and is subject to change. (So beware.)
19297
19298 Details regarding the older, non-standard 64-bit format were
19299 determined empirically by examining 64-bit ELF files produced by
19300 the SGI toolchain on an IRIX 6.5 machine.
19301
19302 - Kevin, July 16, 2002
19303 ] */
19304
19305 static LONGEST
19306 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19307 {
19308 LONGEST length = bfd_get_32 (abfd, buf);
19309
19310 if (length == 0xffffffff)
19311 {
19312 length = bfd_get_64 (abfd, buf + 4);
19313 *bytes_read = 12;
19314 }
19315 else if (length == 0)
19316 {
19317 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19318 length = bfd_get_64 (abfd, buf);
19319 *bytes_read = 8;
19320 }
19321 else
19322 {
19323 *bytes_read = 4;
19324 }
19325
19326 return length;
19327 }
19328
19329 /* Cover function for read_initial_length.
19330 Returns the length of the object at BUF, and stores the size of the
19331 initial length in *BYTES_READ and stores the size that offsets will be in
19332 *OFFSET_SIZE.
19333 If the initial length size is not equivalent to that specified in
19334 CU_HEADER then issue a complaint.
19335 This is useful when reading non-comp-unit headers. */
19336
19337 static LONGEST
19338 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19339 const struct comp_unit_head *cu_header,
19340 unsigned int *bytes_read,
19341 unsigned int *offset_size)
19342 {
19343 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19344
19345 gdb_assert (cu_header->initial_length_size == 4
19346 || cu_header->initial_length_size == 8
19347 || cu_header->initial_length_size == 12);
19348
19349 if (cu_header->initial_length_size != *bytes_read)
19350 complaint (&symfile_complaints,
19351 _("intermixed 32-bit and 64-bit DWARF sections"));
19352
19353 *offset_size = (*bytes_read == 4) ? 4 : 8;
19354 return length;
19355 }
19356
19357 /* Read an offset from the data stream. The size of the offset is
19358 given by cu_header->offset_size. */
19359
19360 static LONGEST
19361 read_offset (bfd *abfd, const gdb_byte *buf,
19362 const struct comp_unit_head *cu_header,
19363 unsigned int *bytes_read)
19364 {
19365 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19366
19367 *bytes_read = cu_header->offset_size;
19368 return offset;
19369 }
19370
19371 /* Read an offset from the data stream. */
19372
19373 static LONGEST
19374 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19375 {
19376 LONGEST retval = 0;
19377
19378 switch (offset_size)
19379 {
19380 case 4:
19381 retval = bfd_get_32 (abfd, buf);
19382 break;
19383 case 8:
19384 retval = bfd_get_64 (abfd, buf);
19385 break;
19386 default:
19387 internal_error (__FILE__, __LINE__,
19388 _("read_offset_1: bad switch [in module %s]"),
19389 bfd_get_filename (abfd));
19390 }
19391
19392 return retval;
19393 }
19394
19395 static const gdb_byte *
19396 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19397 {
19398 /* If the size of a host char is 8 bits, we can return a pointer
19399 to the buffer, otherwise we have to copy the data to a buffer
19400 allocated on the temporary obstack. */
19401 gdb_assert (HOST_CHAR_BIT == 8);
19402 return buf;
19403 }
19404
19405 static const char *
19406 read_direct_string (bfd *abfd, const gdb_byte *buf,
19407 unsigned int *bytes_read_ptr)
19408 {
19409 /* If the size of a host char is 8 bits, we can return a pointer
19410 to the string, otherwise we have to copy the string to a buffer
19411 allocated on the temporary obstack. */
19412 gdb_assert (HOST_CHAR_BIT == 8);
19413 if (*buf == '\0')
19414 {
19415 *bytes_read_ptr = 1;
19416 return NULL;
19417 }
19418 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19419 return (const char *) buf;
19420 }
19421
19422 /* Return pointer to string at section SECT offset STR_OFFSET with error
19423 reporting strings FORM_NAME and SECT_NAME. */
19424
19425 static const char *
19426 read_indirect_string_at_offset_from (struct objfile *objfile,
19427 bfd *abfd, LONGEST str_offset,
19428 struct dwarf2_section_info *sect,
19429 const char *form_name,
19430 const char *sect_name)
19431 {
19432 dwarf2_read_section (objfile, sect);
19433 if (sect->buffer == NULL)
19434 error (_("%s used without %s section [in module %s]"),
19435 form_name, sect_name, bfd_get_filename (abfd));
19436 if (str_offset >= sect->size)
19437 error (_("%s pointing outside of %s section [in module %s]"),
19438 form_name, sect_name, bfd_get_filename (abfd));
19439 gdb_assert (HOST_CHAR_BIT == 8);
19440 if (sect->buffer[str_offset] == '\0')
19441 return NULL;
19442 return (const char *) (sect->buffer + str_offset);
19443 }
19444
19445 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19446
19447 static const char *
19448 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19449 bfd *abfd, LONGEST str_offset)
19450 {
19451 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19452 abfd, str_offset,
19453 &dwarf2_per_objfile->str,
19454 "DW_FORM_strp", ".debug_str");
19455 }
19456
19457 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19458
19459 static const char *
19460 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19461 bfd *abfd, LONGEST str_offset)
19462 {
19463 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19464 abfd, str_offset,
19465 &dwarf2_per_objfile->line_str,
19466 "DW_FORM_line_strp",
19467 ".debug_line_str");
19468 }
19469
19470 /* Read a string at offset STR_OFFSET in the .debug_str section from
19471 the .dwz file DWZ. Throw an error if the offset is too large. If
19472 the string consists of a single NUL byte, return NULL; otherwise
19473 return a pointer to the string. */
19474
19475 static const char *
19476 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19477 LONGEST str_offset)
19478 {
19479 dwarf2_read_section (objfile, &dwz->str);
19480
19481 if (dwz->str.buffer == NULL)
19482 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19483 "section [in module %s]"),
19484 bfd_get_filename (dwz->dwz_bfd));
19485 if (str_offset >= dwz->str.size)
19486 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19487 ".debug_str section [in module %s]"),
19488 bfd_get_filename (dwz->dwz_bfd));
19489 gdb_assert (HOST_CHAR_BIT == 8);
19490 if (dwz->str.buffer[str_offset] == '\0')
19491 return NULL;
19492 return (const char *) (dwz->str.buffer + str_offset);
19493 }
19494
19495 /* Return pointer to string at .debug_str offset as read from BUF.
19496 BUF is assumed to be in a compilation unit described by CU_HEADER.
19497 Return *BYTES_READ_PTR count of bytes read from BUF. */
19498
19499 static const char *
19500 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19501 const gdb_byte *buf,
19502 const struct comp_unit_head *cu_header,
19503 unsigned int *bytes_read_ptr)
19504 {
19505 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19506
19507 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19508 }
19509
19510 /* Return pointer to string at .debug_line_str offset as read from BUF.
19511 BUF is assumed to be in a compilation unit described by CU_HEADER.
19512 Return *BYTES_READ_PTR count of bytes read from BUF. */
19513
19514 static const char *
19515 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19516 bfd *abfd, const gdb_byte *buf,
19517 const struct comp_unit_head *cu_header,
19518 unsigned int *bytes_read_ptr)
19519 {
19520 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19521
19522 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19523 str_offset);
19524 }
19525
19526 ULONGEST
19527 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19528 unsigned int *bytes_read_ptr)
19529 {
19530 ULONGEST result;
19531 unsigned int num_read;
19532 int shift;
19533 unsigned char byte;
19534
19535 result = 0;
19536 shift = 0;
19537 num_read = 0;
19538 while (1)
19539 {
19540 byte = bfd_get_8 (abfd, buf);
19541 buf++;
19542 num_read++;
19543 result |= ((ULONGEST) (byte & 127) << shift);
19544 if ((byte & 128) == 0)
19545 {
19546 break;
19547 }
19548 shift += 7;
19549 }
19550 *bytes_read_ptr = num_read;
19551 return result;
19552 }
19553
19554 static LONGEST
19555 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19556 unsigned int *bytes_read_ptr)
19557 {
19558 LONGEST result;
19559 int shift, num_read;
19560 unsigned char byte;
19561
19562 result = 0;
19563 shift = 0;
19564 num_read = 0;
19565 while (1)
19566 {
19567 byte = bfd_get_8 (abfd, buf);
19568 buf++;
19569 num_read++;
19570 result |= ((LONGEST) (byte & 127) << shift);
19571 shift += 7;
19572 if ((byte & 128) == 0)
19573 {
19574 break;
19575 }
19576 }
19577 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19578 result |= -(((LONGEST) 1) << shift);
19579 *bytes_read_ptr = num_read;
19580 return result;
19581 }
19582
19583 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19584 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19585 ADDR_SIZE is the size of addresses from the CU header. */
19586
19587 static CORE_ADDR
19588 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19589 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19590 {
19591 struct objfile *objfile = dwarf2_per_objfile->objfile;
19592 bfd *abfd = objfile->obfd;
19593 const gdb_byte *info_ptr;
19594
19595 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19596 if (dwarf2_per_objfile->addr.buffer == NULL)
19597 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19598 objfile_name (objfile));
19599 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19600 error (_("DW_FORM_addr_index pointing outside of "
19601 ".debug_addr section [in module %s]"),
19602 objfile_name (objfile));
19603 info_ptr = (dwarf2_per_objfile->addr.buffer
19604 + addr_base + addr_index * addr_size);
19605 if (addr_size == 4)
19606 return bfd_get_32 (abfd, info_ptr);
19607 else
19608 return bfd_get_64 (abfd, info_ptr);
19609 }
19610
19611 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19612
19613 static CORE_ADDR
19614 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19615 {
19616 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19617 cu->addr_base, cu->header.addr_size);
19618 }
19619
19620 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19621
19622 static CORE_ADDR
19623 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19624 unsigned int *bytes_read)
19625 {
19626 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19627 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19628
19629 return read_addr_index (cu, addr_index);
19630 }
19631
19632 /* Data structure to pass results from dwarf2_read_addr_index_reader
19633 back to dwarf2_read_addr_index. */
19634
19635 struct dwarf2_read_addr_index_data
19636 {
19637 ULONGEST addr_base;
19638 int addr_size;
19639 };
19640
19641 /* die_reader_func for dwarf2_read_addr_index. */
19642
19643 static void
19644 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19645 const gdb_byte *info_ptr,
19646 struct die_info *comp_unit_die,
19647 int has_children,
19648 void *data)
19649 {
19650 struct dwarf2_cu *cu = reader->cu;
19651 struct dwarf2_read_addr_index_data *aidata =
19652 (struct dwarf2_read_addr_index_data *) data;
19653
19654 aidata->addr_base = cu->addr_base;
19655 aidata->addr_size = cu->header.addr_size;
19656 }
19657
19658 /* Given an index in .debug_addr, fetch the value.
19659 NOTE: This can be called during dwarf expression evaluation,
19660 long after the debug information has been read, and thus per_cu->cu
19661 may no longer exist. */
19662
19663 CORE_ADDR
19664 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19665 unsigned int addr_index)
19666 {
19667 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19668 struct objfile *objfile = dwarf2_per_objfile->objfile;
19669 struct dwarf2_cu *cu = per_cu->cu;
19670 ULONGEST addr_base;
19671 int addr_size;
19672
19673 /* We need addr_base and addr_size.
19674 If we don't have PER_CU->cu, we have to get it.
19675 Nasty, but the alternative is storing the needed info in PER_CU,
19676 which at this point doesn't seem justified: it's not clear how frequently
19677 it would get used and it would increase the size of every PER_CU.
19678 Entry points like dwarf2_per_cu_addr_size do a similar thing
19679 so we're not in uncharted territory here.
19680 Alas we need to be a bit more complicated as addr_base is contained
19681 in the DIE.
19682
19683 We don't need to read the entire CU(/TU).
19684 We just need the header and top level die.
19685
19686 IWBN to use the aging mechanism to let us lazily later discard the CU.
19687 For now we skip this optimization. */
19688
19689 if (cu != NULL)
19690 {
19691 addr_base = cu->addr_base;
19692 addr_size = cu->header.addr_size;
19693 }
19694 else
19695 {
19696 struct dwarf2_read_addr_index_data aidata;
19697
19698 /* Note: We can't use init_cutu_and_read_dies_simple here,
19699 we need addr_base. */
19700 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19701 dwarf2_read_addr_index_reader, &aidata);
19702 addr_base = aidata.addr_base;
19703 addr_size = aidata.addr_size;
19704 }
19705
19706 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19707 addr_size);
19708 }
19709
19710 /* Given a DW_FORM_GNU_str_index, fetch the string.
19711 This is only used by the Fission support. */
19712
19713 static const char *
19714 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19715 {
19716 struct dwarf2_cu *cu = reader->cu;
19717 struct dwarf2_per_objfile *dwarf2_per_objfile
19718 = cu->per_cu->dwarf2_per_objfile;
19719 struct objfile *objfile = dwarf2_per_objfile->objfile;
19720 const char *objf_name = objfile_name (objfile);
19721 bfd *abfd = objfile->obfd;
19722 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19723 struct dwarf2_section_info *str_offsets_section =
19724 &reader->dwo_file->sections.str_offsets;
19725 const gdb_byte *info_ptr;
19726 ULONGEST str_offset;
19727 static const char form_name[] = "DW_FORM_GNU_str_index";
19728
19729 dwarf2_read_section (objfile, str_section);
19730 dwarf2_read_section (objfile, str_offsets_section);
19731 if (str_section->buffer == NULL)
19732 error (_("%s used without .debug_str.dwo section"
19733 " in CU at offset 0x%x [in module %s]"),
19734 form_name, to_underlying (cu->header.sect_off), objf_name);
19735 if (str_offsets_section->buffer == NULL)
19736 error (_("%s used without .debug_str_offsets.dwo section"
19737 " in CU at offset 0x%x [in module %s]"),
19738 form_name, to_underlying (cu->header.sect_off), objf_name);
19739 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19740 error (_("%s pointing outside of .debug_str_offsets.dwo"
19741 " section in CU at offset 0x%x [in module %s]"),
19742 form_name, to_underlying (cu->header.sect_off), objf_name);
19743 info_ptr = (str_offsets_section->buffer
19744 + str_index * cu->header.offset_size);
19745 if (cu->header.offset_size == 4)
19746 str_offset = bfd_get_32 (abfd, info_ptr);
19747 else
19748 str_offset = bfd_get_64 (abfd, info_ptr);
19749 if (str_offset >= str_section->size)
19750 error (_("Offset from %s pointing outside of"
19751 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
19752 form_name, to_underlying (cu->header.sect_off), objf_name);
19753 return (const char *) (str_section->buffer + str_offset);
19754 }
19755
19756 /* Return the length of an LEB128 number in BUF. */
19757
19758 static int
19759 leb128_size (const gdb_byte *buf)
19760 {
19761 const gdb_byte *begin = buf;
19762 gdb_byte byte;
19763
19764 while (1)
19765 {
19766 byte = *buf++;
19767 if ((byte & 128) == 0)
19768 return buf - begin;
19769 }
19770 }
19771
19772 static void
19773 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19774 {
19775 switch (lang)
19776 {
19777 case DW_LANG_C89:
19778 case DW_LANG_C99:
19779 case DW_LANG_C11:
19780 case DW_LANG_C:
19781 case DW_LANG_UPC:
19782 cu->language = language_c;
19783 break;
19784 case DW_LANG_Java:
19785 case DW_LANG_C_plus_plus:
19786 case DW_LANG_C_plus_plus_11:
19787 case DW_LANG_C_plus_plus_14:
19788 cu->language = language_cplus;
19789 break;
19790 case DW_LANG_D:
19791 cu->language = language_d;
19792 break;
19793 case DW_LANG_Fortran77:
19794 case DW_LANG_Fortran90:
19795 case DW_LANG_Fortran95:
19796 case DW_LANG_Fortran03:
19797 case DW_LANG_Fortran08:
19798 cu->language = language_fortran;
19799 break;
19800 case DW_LANG_Go:
19801 cu->language = language_go;
19802 break;
19803 case DW_LANG_Mips_Assembler:
19804 cu->language = language_asm;
19805 break;
19806 case DW_LANG_Ada83:
19807 case DW_LANG_Ada95:
19808 cu->language = language_ada;
19809 break;
19810 case DW_LANG_Modula2:
19811 cu->language = language_m2;
19812 break;
19813 case DW_LANG_Pascal83:
19814 cu->language = language_pascal;
19815 break;
19816 case DW_LANG_ObjC:
19817 cu->language = language_objc;
19818 break;
19819 case DW_LANG_Rust:
19820 case DW_LANG_Rust_old:
19821 cu->language = language_rust;
19822 break;
19823 case DW_LANG_Cobol74:
19824 case DW_LANG_Cobol85:
19825 default:
19826 cu->language = language_minimal;
19827 break;
19828 }
19829 cu->language_defn = language_def (cu->language);
19830 }
19831
19832 /* Return the named attribute or NULL if not there. */
19833
19834 static struct attribute *
19835 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19836 {
19837 for (;;)
19838 {
19839 unsigned int i;
19840 struct attribute *spec = NULL;
19841
19842 for (i = 0; i < die->num_attrs; ++i)
19843 {
19844 if (die->attrs[i].name == name)
19845 return &die->attrs[i];
19846 if (die->attrs[i].name == DW_AT_specification
19847 || die->attrs[i].name == DW_AT_abstract_origin)
19848 spec = &die->attrs[i];
19849 }
19850
19851 if (!spec)
19852 break;
19853
19854 die = follow_die_ref (die, spec, &cu);
19855 }
19856
19857 return NULL;
19858 }
19859
19860 /* Return the named attribute or NULL if not there,
19861 but do not follow DW_AT_specification, etc.
19862 This is for use in contexts where we're reading .debug_types dies.
19863 Following DW_AT_specification, DW_AT_abstract_origin will take us
19864 back up the chain, and we want to go down. */
19865
19866 static struct attribute *
19867 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19868 {
19869 unsigned int i;
19870
19871 for (i = 0; i < die->num_attrs; ++i)
19872 if (die->attrs[i].name == name)
19873 return &die->attrs[i];
19874
19875 return NULL;
19876 }
19877
19878 /* Return the string associated with a string-typed attribute, or NULL if it
19879 is either not found or is of an incorrect type. */
19880
19881 static const char *
19882 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19883 {
19884 struct attribute *attr;
19885 const char *str = NULL;
19886
19887 attr = dwarf2_attr (die, name, cu);
19888
19889 if (attr != NULL)
19890 {
19891 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19892 || attr->form == DW_FORM_string
19893 || attr->form == DW_FORM_GNU_str_index
19894 || attr->form == DW_FORM_GNU_strp_alt)
19895 str = DW_STRING (attr);
19896 else
19897 complaint (&symfile_complaints,
19898 _("string type expected for attribute %s for "
19899 "DIE at 0x%x in module %s"),
19900 dwarf_attr_name (name), to_underlying (die->sect_off),
19901 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19902 }
19903
19904 return str;
19905 }
19906
19907 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19908 and holds a non-zero value. This function should only be used for
19909 DW_FORM_flag or DW_FORM_flag_present attributes. */
19910
19911 static int
19912 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19913 {
19914 struct attribute *attr = dwarf2_attr (die, name, cu);
19915
19916 return (attr && DW_UNSND (attr));
19917 }
19918
19919 static int
19920 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19921 {
19922 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19923 which value is non-zero. However, we have to be careful with
19924 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19925 (via dwarf2_flag_true_p) follows this attribute. So we may
19926 end up accidently finding a declaration attribute that belongs
19927 to a different DIE referenced by the specification attribute,
19928 even though the given DIE does not have a declaration attribute. */
19929 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19930 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19931 }
19932
19933 /* Return the die giving the specification for DIE, if there is
19934 one. *SPEC_CU is the CU containing DIE on input, and the CU
19935 containing the return value on output. If there is no
19936 specification, but there is an abstract origin, that is
19937 returned. */
19938
19939 static struct die_info *
19940 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19941 {
19942 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19943 *spec_cu);
19944
19945 if (spec_attr == NULL)
19946 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19947
19948 if (spec_attr == NULL)
19949 return NULL;
19950 else
19951 return follow_die_ref (die, spec_attr, spec_cu);
19952 }
19953
19954 /* Stub for free_line_header to match void * callback types. */
19955
19956 static void
19957 free_line_header_voidp (void *arg)
19958 {
19959 struct line_header *lh = (struct line_header *) arg;
19960
19961 delete lh;
19962 }
19963
19964 void
19965 line_header::add_include_dir (const char *include_dir)
19966 {
19967 if (dwarf_line_debug >= 2)
19968 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19969 include_dirs.size () + 1, include_dir);
19970
19971 include_dirs.push_back (include_dir);
19972 }
19973
19974 void
19975 line_header::add_file_name (const char *name,
19976 dir_index d_index,
19977 unsigned int mod_time,
19978 unsigned int length)
19979 {
19980 if (dwarf_line_debug >= 2)
19981 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
19982 (unsigned) file_names.size () + 1, name);
19983
19984 file_names.emplace_back (name, d_index, mod_time, length);
19985 }
19986
19987 /* A convenience function to find the proper .debug_line section for a CU. */
19988
19989 static struct dwarf2_section_info *
19990 get_debug_line_section (struct dwarf2_cu *cu)
19991 {
19992 struct dwarf2_section_info *section;
19993 struct dwarf2_per_objfile *dwarf2_per_objfile
19994 = cu->per_cu->dwarf2_per_objfile;
19995
19996 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19997 DWO file. */
19998 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19999 section = &cu->dwo_unit->dwo_file->sections.line;
20000 else if (cu->per_cu->is_dwz)
20001 {
20002 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20003
20004 section = &dwz->line;
20005 }
20006 else
20007 section = &dwarf2_per_objfile->line;
20008
20009 return section;
20010 }
20011
20012 /* Read directory or file name entry format, starting with byte of
20013 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20014 entries count and the entries themselves in the described entry
20015 format. */
20016
20017 static void
20018 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20019 bfd *abfd, const gdb_byte **bufp,
20020 struct line_header *lh,
20021 const struct comp_unit_head *cu_header,
20022 void (*callback) (struct line_header *lh,
20023 const char *name,
20024 dir_index d_index,
20025 unsigned int mod_time,
20026 unsigned int length))
20027 {
20028 gdb_byte format_count, formati;
20029 ULONGEST data_count, datai;
20030 const gdb_byte *buf = *bufp;
20031 const gdb_byte *format_header_data;
20032 unsigned int bytes_read;
20033
20034 format_count = read_1_byte (abfd, buf);
20035 buf += 1;
20036 format_header_data = buf;
20037 for (formati = 0; formati < format_count; formati++)
20038 {
20039 read_unsigned_leb128 (abfd, buf, &bytes_read);
20040 buf += bytes_read;
20041 read_unsigned_leb128 (abfd, buf, &bytes_read);
20042 buf += bytes_read;
20043 }
20044
20045 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20046 buf += bytes_read;
20047 for (datai = 0; datai < data_count; datai++)
20048 {
20049 const gdb_byte *format = format_header_data;
20050 struct file_entry fe;
20051
20052 for (formati = 0; formati < format_count; formati++)
20053 {
20054 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20055 format += bytes_read;
20056
20057 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20058 format += bytes_read;
20059
20060 gdb::optional<const char *> string;
20061 gdb::optional<unsigned int> uint;
20062
20063 switch (form)
20064 {
20065 case DW_FORM_string:
20066 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20067 buf += bytes_read;
20068 break;
20069
20070 case DW_FORM_line_strp:
20071 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20072 abfd, buf,
20073 cu_header,
20074 &bytes_read));
20075 buf += bytes_read;
20076 break;
20077
20078 case DW_FORM_data1:
20079 uint.emplace (read_1_byte (abfd, buf));
20080 buf += 1;
20081 break;
20082
20083 case DW_FORM_data2:
20084 uint.emplace (read_2_bytes (abfd, buf));
20085 buf += 2;
20086 break;
20087
20088 case DW_FORM_data4:
20089 uint.emplace (read_4_bytes (abfd, buf));
20090 buf += 4;
20091 break;
20092
20093 case DW_FORM_data8:
20094 uint.emplace (read_8_bytes (abfd, buf));
20095 buf += 8;
20096 break;
20097
20098 case DW_FORM_udata:
20099 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20100 buf += bytes_read;
20101 break;
20102
20103 case DW_FORM_block:
20104 /* It is valid only for DW_LNCT_timestamp which is ignored by
20105 current GDB. */
20106 break;
20107 }
20108
20109 switch (content_type)
20110 {
20111 case DW_LNCT_path:
20112 if (string.has_value ())
20113 fe.name = *string;
20114 break;
20115 case DW_LNCT_directory_index:
20116 if (uint.has_value ())
20117 fe.d_index = (dir_index) *uint;
20118 break;
20119 case DW_LNCT_timestamp:
20120 if (uint.has_value ())
20121 fe.mod_time = *uint;
20122 break;
20123 case DW_LNCT_size:
20124 if (uint.has_value ())
20125 fe.length = *uint;
20126 break;
20127 case DW_LNCT_MD5:
20128 break;
20129 default:
20130 complaint (&symfile_complaints,
20131 _("Unknown format content type %s"),
20132 pulongest (content_type));
20133 }
20134 }
20135
20136 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20137 }
20138
20139 *bufp = buf;
20140 }
20141
20142 /* Read the statement program header starting at OFFSET in
20143 .debug_line, or .debug_line.dwo. Return a pointer
20144 to a struct line_header, allocated using xmalloc.
20145 Returns NULL if there is a problem reading the header, e.g., if it
20146 has a version we don't understand.
20147
20148 NOTE: the strings in the include directory and file name tables of
20149 the returned object point into the dwarf line section buffer,
20150 and must not be freed. */
20151
20152 static line_header_up
20153 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20154 {
20155 const gdb_byte *line_ptr;
20156 unsigned int bytes_read, offset_size;
20157 int i;
20158 const char *cur_dir, *cur_file;
20159 struct dwarf2_section_info *section;
20160 bfd *abfd;
20161 struct dwarf2_per_objfile *dwarf2_per_objfile
20162 = cu->per_cu->dwarf2_per_objfile;
20163
20164 section = get_debug_line_section (cu);
20165 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20166 if (section->buffer == NULL)
20167 {
20168 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20169 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20170 else
20171 complaint (&symfile_complaints, _("missing .debug_line section"));
20172 return 0;
20173 }
20174
20175 /* We can't do this until we know the section is non-empty.
20176 Only then do we know we have such a section. */
20177 abfd = get_section_bfd_owner (section);
20178
20179 /* Make sure that at least there's room for the total_length field.
20180 That could be 12 bytes long, but we're just going to fudge that. */
20181 if (to_underlying (sect_off) + 4 >= section->size)
20182 {
20183 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20184 return 0;
20185 }
20186
20187 line_header_up lh (new line_header ());
20188
20189 lh->sect_off = sect_off;
20190 lh->offset_in_dwz = cu->per_cu->is_dwz;
20191
20192 line_ptr = section->buffer + to_underlying (sect_off);
20193
20194 /* Read in the header. */
20195 lh->total_length =
20196 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20197 &bytes_read, &offset_size);
20198 line_ptr += bytes_read;
20199 if (line_ptr + lh->total_length > (section->buffer + section->size))
20200 {
20201 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20202 return 0;
20203 }
20204 lh->statement_program_end = line_ptr + lh->total_length;
20205 lh->version = read_2_bytes (abfd, line_ptr);
20206 line_ptr += 2;
20207 if (lh->version > 5)
20208 {
20209 /* This is a version we don't understand. The format could have
20210 changed in ways we don't handle properly so just punt. */
20211 complaint (&symfile_complaints,
20212 _("unsupported version in .debug_line section"));
20213 return NULL;
20214 }
20215 if (lh->version >= 5)
20216 {
20217 gdb_byte segment_selector_size;
20218
20219 /* Skip address size. */
20220 read_1_byte (abfd, line_ptr);
20221 line_ptr += 1;
20222
20223 segment_selector_size = read_1_byte (abfd, line_ptr);
20224 line_ptr += 1;
20225 if (segment_selector_size != 0)
20226 {
20227 complaint (&symfile_complaints,
20228 _("unsupported segment selector size %u "
20229 "in .debug_line section"),
20230 segment_selector_size);
20231 return NULL;
20232 }
20233 }
20234 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20235 line_ptr += offset_size;
20236 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20237 line_ptr += 1;
20238 if (lh->version >= 4)
20239 {
20240 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20241 line_ptr += 1;
20242 }
20243 else
20244 lh->maximum_ops_per_instruction = 1;
20245
20246 if (lh->maximum_ops_per_instruction == 0)
20247 {
20248 lh->maximum_ops_per_instruction = 1;
20249 complaint (&symfile_complaints,
20250 _("invalid maximum_ops_per_instruction "
20251 "in `.debug_line' section"));
20252 }
20253
20254 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20255 line_ptr += 1;
20256 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20257 line_ptr += 1;
20258 lh->line_range = read_1_byte (abfd, line_ptr);
20259 line_ptr += 1;
20260 lh->opcode_base = read_1_byte (abfd, line_ptr);
20261 line_ptr += 1;
20262 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20263
20264 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20265 for (i = 1; i < lh->opcode_base; ++i)
20266 {
20267 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20268 line_ptr += 1;
20269 }
20270
20271 if (lh->version >= 5)
20272 {
20273 /* Read directory table. */
20274 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20275 &cu->header,
20276 [] (struct line_header *lh, const char *name,
20277 dir_index d_index, unsigned int mod_time,
20278 unsigned int length)
20279 {
20280 lh->add_include_dir (name);
20281 });
20282
20283 /* Read file name table. */
20284 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20285 &cu->header,
20286 [] (struct line_header *lh, const char *name,
20287 dir_index d_index, unsigned int mod_time,
20288 unsigned int length)
20289 {
20290 lh->add_file_name (name, d_index, mod_time, length);
20291 });
20292 }
20293 else
20294 {
20295 /* Read directory table. */
20296 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20297 {
20298 line_ptr += bytes_read;
20299 lh->add_include_dir (cur_dir);
20300 }
20301 line_ptr += bytes_read;
20302
20303 /* Read file name table. */
20304 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20305 {
20306 unsigned int mod_time, length;
20307 dir_index d_index;
20308
20309 line_ptr += bytes_read;
20310 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20311 line_ptr += bytes_read;
20312 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20313 line_ptr += bytes_read;
20314 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20315 line_ptr += bytes_read;
20316
20317 lh->add_file_name (cur_file, d_index, mod_time, length);
20318 }
20319 line_ptr += bytes_read;
20320 }
20321 lh->statement_program_start = line_ptr;
20322
20323 if (line_ptr > (section->buffer + section->size))
20324 complaint (&symfile_complaints,
20325 _("line number info header doesn't "
20326 "fit in `.debug_line' section"));
20327
20328 return lh;
20329 }
20330
20331 /* Subroutine of dwarf_decode_lines to simplify it.
20332 Return the file name of the psymtab for included file FILE_INDEX
20333 in line header LH of PST.
20334 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20335 If space for the result is malloc'd, *NAME_HOLDER will be set.
20336 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20337
20338 static const char *
20339 psymtab_include_file_name (const struct line_header *lh, int file_index,
20340 const struct partial_symtab *pst,
20341 const char *comp_dir,
20342 gdb::unique_xmalloc_ptr<char> *name_holder)
20343 {
20344 const file_entry &fe = lh->file_names[file_index];
20345 const char *include_name = fe.name;
20346 const char *include_name_to_compare = include_name;
20347 const char *pst_filename;
20348 int file_is_pst;
20349
20350 const char *dir_name = fe.include_dir (lh);
20351
20352 gdb::unique_xmalloc_ptr<char> hold_compare;
20353 if (!IS_ABSOLUTE_PATH (include_name)
20354 && (dir_name != NULL || comp_dir != NULL))
20355 {
20356 /* Avoid creating a duplicate psymtab for PST.
20357 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20358 Before we do the comparison, however, we need to account
20359 for DIR_NAME and COMP_DIR.
20360 First prepend dir_name (if non-NULL). If we still don't
20361 have an absolute path prepend comp_dir (if non-NULL).
20362 However, the directory we record in the include-file's
20363 psymtab does not contain COMP_DIR (to match the
20364 corresponding symtab(s)).
20365
20366 Example:
20367
20368 bash$ cd /tmp
20369 bash$ gcc -g ./hello.c
20370 include_name = "hello.c"
20371 dir_name = "."
20372 DW_AT_comp_dir = comp_dir = "/tmp"
20373 DW_AT_name = "./hello.c"
20374
20375 */
20376
20377 if (dir_name != NULL)
20378 {
20379 name_holder->reset (concat (dir_name, SLASH_STRING,
20380 include_name, (char *) NULL));
20381 include_name = name_holder->get ();
20382 include_name_to_compare = include_name;
20383 }
20384 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20385 {
20386 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20387 include_name, (char *) NULL));
20388 include_name_to_compare = hold_compare.get ();
20389 }
20390 }
20391
20392 pst_filename = pst->filename;
20393 gdb::unique_xmalloc_ptr<char> copied_name;
20394 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20395 {
20396 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20397 pst_filename, (char *) NULL));
20398 pst_filename = copied_name.get ();
20399 }
20400
20401 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20402
20403 if (file_is_pst)
20404 return NULL;
20405 return include_name;
20406 }
20407
20408 /* State machine to track the state of the line number program. */
20409
20410 class lnp_state_machine
20411 {
20412 public:
20413 /* Initialize a machine state for the start of a line number
20414 program. */
20415 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20416
20417 file_entry *current_file ()
20418 {
20419 /* lh->file_names is 0-based, but the file name numbers in the
20420 statement program are 1-based. */
20421 return m_line_header->file_name_at (m_file);
20422 }
20423
20424 /* Record the line in the state machine. END_SEQUENCE is true if
20425 we're processing the end of a sequence. */
20426 void record_line (bool end_sequence);
20427
20428 /* Check address and if invalid nop-out the rest of the lines in this
20429 sequence. */
20430 void check_line_address (struct dwarf2_cu *cu,
20431 const gdb_byte *line_ptr,
20432 CORE_ADDR lowpc, CORE_ADDR address);
20433
20434 void handle_set_discriminator (unsigned int discriminator)
20435 {
20436 m_discriminator = discriminator;
20437 m_line_has_non_zero_discriminator |= discriminator != 0;
20438 }
20439
20440 /* Handle DW_LNE_set_address. */
20441 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20442 {
20443 m_op_index = 0;
20444 address += baseaddr;
20445 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20446 }
20447
20448 /* Handle DW_LNS_advance_pc. */
20449 void handle_advance_pc (CORE_ADDR adjust);
20450
20451 /* Handle a special opcode. */
20452 void handle_special_opcode (unsigned char op_code);
20453
20454 /* Handle DW_LNS_advance_line. */
20455 void handle_advance_line (int line_delta)
20456 {
20457 advance_line (line_delta);
20458 }
20459
20460 /* Handle DW_LNS_set_file. */
20461 void handle_set_file (file_name_index file);
20462
20463 /* Handle DW_LNS_negate_stmt. */
20464 void handle_negate_stmt ()
20465 {
20466 m_is_stmt = !m_is_stmt;
20467 }
20468
20469 /* Handle DW_LNS_const_add_pc. */
20470 void handle_const_add_pc ();
20471
20472 /* Handle DW_LNS_fixed_advance_pc. */
20473 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20474 {
20475 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20476 m_op_index = 0;
20477 }
20478
20479 /* Handle DW_LNS_copy. */
20480 void handle_copy ()
20481 {
20482 record_line (false);
20483 m_discriminator = 0;
20484 }
20485
20486 /* Handle DW_LNE_end_sequence. */
20487 void handle_end_sequence ()
20488 {
20489 m_record_line_callback = ::record_line;
20490 }
20491
20492 private:
20493 /* Advance the line by LINE_DELTA. */
20494 void advance_line (int line_delta)
20495 {
20496 m_line += line_delta;
20497
20498 if (line_delta != 0)
20499 m_line_has_non_zero_discriminator = m_discriminator != 0;
20500 }
20501
20502 gdbarch *m_gdbarch;
20503
20504 /* True if we're recording lines.
20505 Otherwise we're building partial symtabs and are just interested in
20506 finding include files mentioned by the line number program. */
20507 bool m_record_lines_p;
20508
20509 /* The line number header. */
20510 line_header *m_line_header;
20511
20512 /* These are part of the standard DWARF line number state machine,
20513 and initialized according to the DWARF spec. */
20514
20515 unsigned char m_op_index = 0;
20516 /* The line table index (1-based) of the current file. */
20517 file_name_index m_file = (file_name_index) 1;
20518 unsigned int m_line = 1;
20519
20520 /* These are initialized in the constructor. */
20521
20522 CORE_ADDR m_address;
20523 bool m_is_stmt;
20524 unsigned int m_discriminator;
20525
20526 /* Additional bits of state we need to track. */
20527
20528 /* The last file that we called dwarf2_start_subfile for.
20529 This is only used for TLLs. */
20530 unsigned int m_last_file = 0;
20531 /* The last file a line number was recorded for. */
20532 struct subfile *m_last_subfile = NULL;
20533
20534 /* The function to call to record a line. */
20535 record_line_ftype *m_record_line_callback = NULL;
20536
20537 /* The last line number that was recorded, used to coalesce
20538 consecutive entries for the same line. This can happen, for
20539 example, when discriminators are present. PR 17276. */
20540 unsigned int m_last_line = 0;
20541 bool m_line_has_non_zero_discriminator = false;
20542 };
20543
20544 void
20545 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20546 {
20547 CORE_ADDR addr_adj = (((m_op_index + adjust)
20548 / m_line_header->maximum_ops_per_instruction)
20549 * m_line_header->minimum_instruction_length);
20550 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20551 m_op_index = ((m_op_index + adjust)
20552 % m_line_header->maximum_ops_per_instruction);
20553 }
20554
20555 void
20556 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20557 {
20558 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20559 CORE_ADDR addr_adj = (((m_op_index
20560 + (adj_opcode / m_line_header->line_range))
20561 / m_line_header->maximum_ops_per_instruction)
20562 * m_line_header->minimum_instruction_length);
20563 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20564 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20565 % m_line_header->maximum_ops_per_instruction);
20566
20567 int line_delta = (m_line_header->line_base
20568 + (adj_opcode % m_line_header->line_range));
20569 advance_line (line_delta);
20570 record_line (false);
20571 m_discriminator = 0;
20572 }
20573
20574 void
20575 lnp_state_machine::handle_set_file (file_name_index file)
20576 {
20577 m_file = file;
20578
20579 const file_entry *fe = current_file ();
20580 if (fe == NULL)
20581 dwarf2_debug_line_missing_file_complaint ();
20582 else if (m_record_lines_p)
20583 {
20584 const char *dir = fe->include_dir (m_line_header);
20585
20586 m_last_subfile = current_subfile;
20587 m_line_has_non_zero_discriminator = m_discriminator != 0;
20588 dwarf2_start_subfile (fe->name, dir);
20589 }
20590 }
20591
20592 void
20593 lnp_state_machine::handle_const_add_pc ()
20594 {
20595 CORE_ADDR adjust
20596 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20597
20598 CORE_ADDR addr_adj
20599 = (((m_op_index + adjust)
20600 / m_line_header->maximum_ops_per_instruction)
20601 * m_line_header->minimum_instruction_length);
20602
20603 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20604 m_op_index = ((m_op_index + adjust)
20605 % m_line_header->maximum_ops_per_instruction);
20606 }
20607
20608 /* Ignore this record_line request. */
20609
20610 static void
20611 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20612 {
20613 return;
20614 }
20615
20616 /* Return non-zero if we should add LINE to the line number table.
20617 LINE is the line to add, LAST_LINE is the last line that was added,
20618 LAST_SUBFILE is the subfile for LAST_LINE.
20619 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20620 had a non-zero discriminator.
20621
20622 We have to be careful in the presence of discriminators.
20623 E.g., for this line:
20624
20625 for (i = 0; i < 100000; i++);
20626
20627 clang can emit four line number entries for that one line,
20628 each with a different discriminator.
20629 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20630
20631 However, we want gdb to coalesce all four entries into one.
20632 Otherwise the user could stepi into the middle of the line and
20633 gdb would get confused about whether the pc really was in the
20634 middle of the line.
20635
20636 Things are further complicated by the fact that two consecutive
20637 line number entries for the same line is a heuristic used by gcc
20638 to denote the end of the prologue. So we can't just discard duplicate
20639 entries, we have to be selective about it. The heuristic we use is
20640 that we only collapse consecutive entries for the same line if at least
20641 one of those entries has a non-zero discriminator. PR 17276.
20642
20643 Note: Addresses in the line number state machine can never go backwards
20644 within one sequence, thus this coalescing is ok. */
20645
20646 static int
20647 dwarf_record_line_p (unsigned int line, unsigned int last_line,
20648 int line_has_non_zero_discriminator,
20649 struct subfile *last_subfile)
20650 {
20651 if (current_subfile != last_subfile)
20652 return 1;
20653 if (line != last_line)
20654 return 1;
20655 /* Same line for the same file that we've seen already.
20656 As a last check, for pr 17276, only record the line if the line
20657 has never had a non-zero discriminator. */
20658 if (!line_has_non_zero_discriminator)
20659 return 1;
20660 return 0;
20661 }
20662
20663 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20664 in the line table of subfile SUBFILE. */
20665
20666 static void
20667 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20668 unsigned int line, CORE_ADDR address,
20669 record_line_ftype p_record_line)
20670 {
20671 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20672
20673 if (dwarf_line_debug)
20674 {
20675 fprintf_unfiltered (gdb_stdlog,
20676 "Recording line %u, file %s, address %s\n",
20677 line, lbasename (subfile->name),
20678 paddress (gdbarch, address));
20679 }
20680
20681 (*p_record_line) (subfile, line, addr);
20682 }
20683
20684 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20685 Mark the end of a set of line number records.
20686 The arguments are the same as for dwarf_record_line_1.
20687 If SUBFILE is NULL the request is ignored. */
20688
20689 static void
20690 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20691 CORE_ADDR address, record_line_ftype p_record_line)
20692 {
20693 if (subfile == NULL)
20694 return;
20695
20696 if (dwarf_line_debug)
20697 {
20698 fprintf_unfiltered (gdb_stdlog,
20699 "Finishing current line, file %s, address %s\n",
20700 lbasename (subfile->name),
20701 paddress (gdbarch, address));
20702 }
20703
20704 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20705 }
20706
20707 void
20708 lnp_state_machine::record_line (bool end_sequence)
20709 {
20710 if (dwarf_line_debug)
20711 {
20712 fprintf_unfiltered (gdb_stdlog,
20713 "Processing actual line %u: file %u,"
20714 " address %s, is_stmt %u, discrim %u\n",
20715 m_line, to_underlying (m_file),
20716 paddress (m_gdbarch, m_address),
20717 m_is_stmt, m_discriminator);
20718 }
20719
20720 file_entry *fe = current_file ();
20721
20722 if (fe == NULL)
20723 dwarf2_debug_line_missing_file_complaint ();
20724 /* For now we ignore lines not starting on an instruction boundary.
20725 But not when processing end_sequence for compatibility with the
20726 previous version of the code. */
20727 else if (m_op_index == 0 || end_sequence)
20728 {
20729 fe->included_p = 1;
20730 if (m_record_lines_p && m_is_stmt)
20731 {
20732 if (m_last_subfile != current_subfile || end_sequence)
20733 {
20734 dwarf_finish_line (m_gdbarch, m_last_subfile,
20735 m_address, m_record_line_callback);
20736 }
20737
20738 if (!end_sequence)
20739 {
20740 if (dwarf_record_line_p (m_line, m_last_line,
20741 m_line_has_non_zero_discriminator,
20742 m_last_subfile))
20743 {
20744 dwarf_record_line_1 (m_gdbarch, current_subfile,
20745 m_line, m_address,
20746 m_record_line_callback);
20747 }
20748 m_last_subfile = current_subfile;
20749 m_last_line = m_line;
20750 }
20751 }
20752 }
20753 }
20754
20755 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20756 bool record_lines_p)
20757 {
20758 m_gdbarch = arch;
20759 m_record_lines_p = record_lines_p;
20760 m_line_header = lh;
20761
20762 m_record_line_callback = ::record_line;
20763
20764 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20765 was a line entry for it so that the backend has a chance to adjust it
20766 and also record it in case it needs it. This is currently used by MIPS
20767 code, cf. `mips_adjust_dwarf2_line'. */
20768 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20769 m_is_stmt = lh->default_is_stmt;
20770 m_discriminator = 0;
20771 }
20772
20773 void
20774 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20775 const gdb_byte *line_ptr,
20776 CORE_ADDR lowpc, CORE_ADDR address)
20777 {
20778 /* If address < lowpc then it's not a usable value, it's outside the
20779 pc range of the CU. However, we restrict the test to only address
20780 values of zero to preserve GDB's previous behaviour which is to
20781 handle the specific case of a function being GC'd by the linker. */
20782
20783 if (address == 0 && address < lowpc)
20784 {
20785 /* This line table is for a function which has been
20786 GCd by the linker. Ignore it. PR gdb/12528 */
20787
20788 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20789 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20790
20791 complaint (&symfile_complaints,
20792 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20793 line_offset, objfile_name (objfile));
20794 m_record_line_callback = noop_record_line;
20795 /* Note: record_line_callback is left as noop_record_line until
20796 we see DW_LNE_end_sequence. */
20797 }
20798 }
20799
20800 /* Subroutine of dwarf_decode_lines to simplify it.
20801 Process the line number information in LH.
20802 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20803 program in order to set included_p for every referenced header. */
20804
20805 static void
20806 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20807 const int decode_for_pst_p, CORE_ADDR lowpc)
20808 {
20809 const gdb_byte *line_ptr, *extended_end;
20810 const gdb_byte *line_end;
20811 unsigned int bytes_read, extended_len;
20812 unsigned char op_code, extended_op;
20813 CORE_ADDR baseaddr;
20814 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20815 bfd *abfd = objfile->obfd;
20816 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20817 /* True if we're recording line info (as opposed to building partial
20818 symtabs and just interested in finding include files mentioned by
20819 the line number program). */
20820 bool record_lines_p = !decode_for_pst_p;
20821
20822 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20823
20824 line_ptr = lh->statement_program_start;
20825 line_end = lh->statement_program_end;
20826
20827 /* Read the statement sequences until there's nothing left. */
20828 while (line_ptr < line_end)
20829 {
20830 /* The DWARF line number program state machine. Reset the state
20831 machine at the start of each sequence. */
20832 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20833 bool end_sequence = false;
20834
20835 if (record_lines_p)
20836 {
20837 /* Start a subfile for the current file of the state
20838 machine. */
20839 const file_entry *fe = state_machine.current_file ();
20840
20841 if (fe != NULL)
20842 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
20843 }
20844
20845 /* Decode the table. */
20846 while (line_ptr < line_end && !end_sequence)
20847 {
20848 op_code = read_1_byte (abfd, line_ptr);
20849 line_ptr += 1;
20850
20851 if (op_code >= lh->opcode_base)
20852 {
20853 /* Special opcode. */
20854 state_machine.handle_special_opcode (op_code);
20855 }
20856 else switch (op_code)
20857 {
20858 case DW_LNS_extended_op:
20859 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20860 &bytes_read);
20861 line_ptr += bytes_read;
20862 extended_end = line_ptr + extended_len;
20863 extended_op = read_1_byte (abfd, line_ptr);
20864 line_ptr += 1;
20865 switch (extended_op)
20866 {
20867 case DW_LNE_end_sequence:
20868 state_machine.handle_end_sequence ();
20869 end_sequence = true;
20870 break;
20871 case DW_LNE_set_address:
20872 {
20873 CORE_ADDR address
20874 = read_address (abfd, line_ptr, cu, &bytes_read);
20875 line_ptr += bytes_read;
20876
20877 state_machine.check_line_address (cu, line_ptr,
20878 lowpc, address);
20879 state_machine.handle_set_address (baseaddr, address);
20880 }
20881 break;
20882 case DW_LNE_define_file:
20883 {
20884 const char *cur_file;
20885 unsigned int mod_time, length;
20886 dir_index dindex;
20887
20888 cur_file = read_direct_string (abfd, line_ptr,
20889 &bytes_read);
20890 line_ptr += bytes_read;
20891 dindex = (dir_index)
20892 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20893 line_ptr += bytes_read;
20894 mod_time =
20895 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20896 line_ptr += bytes_read;
20897 length =
20898 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20899 line_ptr += bytes_read;
20900 lh->add_file_name (cur_file, dindex, mod_time, length);
20901 }
20902 break;
20903 case DW_LNE_set_discriminator:
20904 {
20905 /* The discriminator is not interesting to the
20906 debugger; just ignore it. We still need to
20907 check its value though:
20908 if there are consecutive entries for the same
20909 (non-prologue) line we want to coalesce them.
20910 PR 17276. */
20911 unsigned int discr
20912 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20913 line_ptr += bytes_read;
20914
20915 state_machine.handle_set_discriminator (discr);
20916 }
20917 break;
20918 default:
20919 complaint (&symfile_complaints,
20920 _("mangled .debug_line section"));
20921 return;
20922 }
20923 /* Make sure that we parsed the extended op correctly. If e.g.
20924 we expected a different address size than the producer used,
20925 we may have read the wrong number of bytes. */
20926 if (line_ptr != extended_end)
20927 {
20928 complaint (&symfile_complaints,
20929 _("mangled .debug_line section"));
20930 return;
20931 }
20932 break;
20933 case DW_LNS_copy:
20934 state_machine.handle_copy ();
20935 break;
20936 case DW_LNS_advance_pc:
20937 {
20938 CORE_ADDR adjust
20939 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20940 line_ptr += bytes_read;
20941
20942 state_machine.handle_advance_pc (adjust);
20943 }
20944 break;
20945 case DW_LNS_advance_line:
20946 {
20947 int line_delta
20948 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20949 line_ptr += bytes_read;
20950
20951 state_machine.handle_advance_line (line_delta);
20952 }
20953 break;
20954 case DW_LNS_set_file:
20955 {
20956 file_name_index file
20957 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20958 &bytes_read);
20959 line_ptr += bytes_read;
20960
20961 state_machine.handle_set_file (file);
20962 }
20963 break;
20964 case DW_LNS_set_column:
20965 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20966 line_ptr += bytes_read;
20967 break;
20968 case DW_LNS_negate_stmt:
20969 state_machine.handle_negate_stmt ();
20970 break;
20971 case DW_LNS_set_basic_block:
20972 break;
20973 /* Add to the address register of the state machine the
20974 address increment value corresponding to special opcode
20975 255. I.e., this value is scaled by the minimum
20976 instruction length since special opcode 255 would have
20977 scaled the increment. */
20978 case DW_LNS_const_add_pc:
20979 state_machine.handle_const_add_pc ();
20980 break;
20981 case DW_LNS_fixed_advance_pc:
20982 {
20983 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20984 line_ptr += 2;
20985
20986 state_machine.handle_fixed_advance_pc (addr_adj);
20987 }
20988 break;
20989 default:
20990 {
20991 /* Unknown standard opcode, ignore it. */
20992 int i;
20993
20994 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20995 {
20996 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20997 line_ptr += bytes_read;
20998 }
20999 }
21000 }
21001 }
21002
21003 if (!end_sequence)
21004 dwarf2_debug_line_missing_end_sequence_complaint ();
21005
21006 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21007 in which case we still finish recording the last line). */
21008 state_machine.record_line (true);
21009 }
21010 }
21011
21012 /* Decode the Line Number Program (LNP) for the given line_header
21013 structure and CU. The actual information extracted and the type
21014 of structures created from the LNP depends on the value of PST.
21015
21016 1. If PST is NULL, then this procedure uses the data from the program
21017 to create all necessary symbol tables, and their linetables.
21018
21019 2. If PST is not NULL, this procedure reads the program to determine
21020 the list of files included by the unit represented by PST, and
21021 builds all the associated partial symbol tables.
21022
21023 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21024 It is used for relative paths in the line table.
21025 NOTE: When processing partial symtabs (pst != NULL),
21026 comp_dir == pst->dirname.
21027
21028 NOTE: It is important that psymtabs have the same file name (via strcmp)
21029 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21030 symtab we don't use it in the name of the psymtabs we create.
21031 E.g. expand_line_sal requires this when finding psymtabs to expand.
21032 A good testcase for this is mb-inline.exp.
21033
21034 LOWPC is the lowest address in CU (or 0 if not known).
21035
21036 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21037 for its PC<->lines mapping information. Otherwise only the filename
21038 table is read in. */
21039
21040 static void
21041 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21042 struct dwarf2_cu *cu, struct partial_symtab *pst,
21043 CORE_ADDR lowpc, int decode_mapping)
21044 {
21045 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21046 const int decode_for_pst_p = (pst != NULL);
21047
21048 if (decode_mapping)
21049 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21050
21051 if (decode_for_pst_p)
21052 {
21053 int file_index;
21054
21055 /* Now that we're done scanning the Line Header Program, we can
21056 create the psymtab of each included file. */
21057 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21058 if (lh->file_names[file_index].included_p == 1)
21059 {
21060 gdb::unique_xmalloc_ptr<char> name_holder;
21061 const char *include_name =
21062 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21063 &name_holder);
21064 if (include_name != NULL)
21065 dwarf2_create_include_psymtab (include_name, pst, objfile);
21066 }
21067 }
21068 else
21069 {
21070 /* Make sure a symtab is created for every file, even files
21071 which contain only variables (i.e. no code with associated
21072 line numbers). */
21073 struct compunit_symtab *cust = buildsym_compunit_symtab ();
21074 int i;
21075
21076 for (i = 0; i < lh->file_names.size (); i++)
21077 {
21078 file_entry &fe = lh->file_names[i];
21079
21080 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
21081
21082 if (current_subfile->symtab == NULL)
21083 {
21084 current_subfile->symtab
21085 = allocate_symtab (cust, current_subfile->name);
21086 }
21087 fe.symtab = current_subfile->symtab;
21088 }
21089 }
21090 }
21091
21092 /* Start a subfile for DWARF. FILENAME is the name of the file and
21093 DIRNAME the name of the source directory which contains FILENAME
21094 or NULL if not known.
21095 This routine tries to keep line numbers from identical absolute and
21096 relative file names in a common subfile.
21097
21098 Using the `list' example from the GDB testsuite, which resides in
21099 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21100 of /srcdir/list0.c yields the following debugging information for list0.c:
21101
21102 DW_AT_name: /srcdir/list0.c
21103 DW_AT_comp_dir: /compdir
21104 files.files[0].name: list0.h
21105 files.files[0].dir: /srcdir
21106 files.files[1].name: list0.c
21107 files.files[1].dir: /srcdir
21108
21109 The line number information for list0.c has to end up in a single
21110 subfile, so that `break /srcdir/list0.c:1' works as expected.
21111 start_subfile will ensure that this happens provided that we pass the
21112 concatenation of files.files[1].dir and files.files[1].name as the
21113 subfile's name. */
21114
21115 static void
21116 dwarf2_start_subfile (const char *filename, const char *dirname)
21117 {
21118 char *copy = NULL;
21119
21120 /* In order not to lose the line information directory,
21121 we concatenate it to the filename when it makes sense.
21122 Note that the Dwarf3 standard says (speaking of filenames in line
21123 information): ``The directory index is ignored for file names
21124 that represent full path names''. Thus ignoring dirname in the
21125 `else' branch below isn't an issue. */
21126
21127 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21128 {
21129 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21130 filename = copy;
21131 }
21132
21133 start_subfile (filename);
21134
21135 if (copy != NULL)
21136 xfree (copy);
21137 }
21138
21139 /* Start a symtab for DWARF.
21140 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21141
21142 static struct compunit_symtab *
21143 dwarf2_start_symtab (struct dwarf2_cu *cu,
21144 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21145 {
21146 struct compunit_symtab *cust
21147 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21148 low_pc, cu->language);
21149
21150 record_debugformat ("DWARF 2");
21151 record_producer (cu->producer);
21152
21153 /* We assume that we're processing GCC output. */
21154 processing_gcc_compilation = 2;
21155
21156 cu->processing_has_namespace_info = 0;
21157
21158 return cust;
21159 }
21160
21161 static void
21162 var_decode_location (struct attribute *attr, struct symbol *sym,
21163 struct dwarf2_cu *cu)
21164 {
21165 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21166 struct comp_unit_head *cu_header = &cu->header;
21167
21168 /* NOTE drow/2003-01-30: There used to be a comment and some special
21169 code here to turn a symbol with DW_AT_external and a
21170 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21171 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21172 with some versions of binutils) where shared libraries could have
21173 relocations against symbols in their debug information - the
21174 minimal symbol would have the right address, but the debug info
21175 would not. It's no longer necessary, because we will explicitly
21176 apply relocations when we read in the debug information now. */
21177
21178 /* A DW_AT_location attribute with no contents indicates that a
21179 variable has been optimized away. */
21180 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21181 {
21182 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21183 return;
21184 }
21185
21186 /* Handle one degenerate form of location expression specially, to
21187 preserve GDB's previous behavior when section offsets are
21188 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21189 then mark this symbol as LOC_STATIC. */
21190
21191 if (attr_form_is_block (attr)
21192 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21193 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21194 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21195 && (DW_BLOCK (attr)->size
21196 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21197 {
21198 unsigned int dummy;
21199
21200 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21201 SYMBOL_VALUE_ADDRESS (sym) =
21202 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21203 else
21204 SYMBOL_VALUE_ADDRESS (sym) =
21205 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21206 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21207 fixup_symbol_section (sym, objfile);
21208 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21209 SYMBOL_SECTION (sym));
21210 return;
21211 }
21212
21213 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21214 expression evaluator, and use LOC_COMPUTED only when necessary
21215 (i.e. when the value of a register or memory location is
21216 referenced, or a thread-local block, etc.). Then again, it might
21217 not be worthwhile. I'm assuming that it isn't unless performance
21218 or memory numbers show me otherwise. */
21219
21220 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21221
21222 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21223 cu->has_loclist = 1;
21224 }
21225
21226 /* Given a pointer to a DWARF information entry, figure out if we need
21227 to make a symbol table entry for it, and if so, create a new entry
21228 and return a pointer to it.
21229 If TYPE is NULL, determine symbol type from the die, otherwise
21230 used the passed type.
21231 If SPACE is not NULL, use it to hold the new symbol. If it is
21232 NULL, allocate a new symbol on the objfile's obstack. */
21233
21234 static struct symbol *
21235 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21236 struct symbol *space)
21237 {
21238 struct dwarf2_per_objfile *dwarf2_per_objfile
21239 = cu->per_cu->dwarf2_per_objfile;
21240 struct objfile *objfile = dwarf2_per_objfile->objfile;
21241 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21242 struct symbol *sym = NULL;
21243 const char *name;
21244 struct attribute *attr = NULL;
21245 struct attribute *attr2 = NULL;
21246 CORE_ADDR baseaddr;
21247 struct pending **list_to_add = NULL;
21248
21249 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21250
21251 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21252
21253 name = dwarf2_name (die, cu);
21254 if (name)
21255 {
21256 const char *linkagename;
21257 int suppress_add = 0;
21258
21259 if (space)
21260 sym = space;
21261 else
21262 sym = allocate_symbol (objfile);
21263 OBJSTAT (objfile, n_syms++);
21264
21265 /* Cache this symbol's name and the name's demangled form (if any). */
21266 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21267 linkagename = dwarf2_physname (name, die, cu);
21268 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21269
21270 /* Fortran does not have mangling standard and the mangling does differ
21271 between gfortran, iFort etc. */
21272 if (cu->language == language_fortran
21273 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21274 symbol_set_demangled_name (&(sym->ginfo),
21275 dwarf2_full_name (name, die, cu),
21276 NULL);
21277
21278 /* Default assumptions.
21279 Use the passed type or decode it from the die. */
21280 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21281 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21282 if (type != NULL)
21283 SYMBOL_TYPE (sym) = type;
21284 else
21285 SYMBOL_TYPE (sym) = die_type (die, cu);
21286 attr = dwarf2_attr (die,
21287 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21288 cu);
21289 if (attr)
21290 {
21291 SYMBOL_LINE (sym) = DW_UNSND (attr);
21292 }
21293
21294 attr = dwarf2_attr (die,
21295 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21296 cu);
21297 if (attr)
21298 {
21299 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21300 struct file_entry *fe;
21301
21302 if (cu->line_header != NULL)
21303 fe = cu->line_header->file_name_at (file_index);
21304 else
21305 fe = NULL;
21306
21307 if (fe == NULL)
21308 complaint (&symfile_complaints,
21309 _("file index out of range"));
21310 else
21311 symbol_set_symtab (sym, fe->symtab);
21312 }
21313
21314 switch (die->tag)
21315 {
21316 case DW_TAG_label:
21317 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21318 if (attr)
21319 {
21320 CORE_ADDR addr;
21321
21322 addr = attr_value_as_address (attr);
21323 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21324 SYMBOL_VALUE_ADDRESS (sym) = addr;
21325 }
21326 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21327 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21328 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21329 add_symbol_to_list (sym, cu->list_in_scope);
21330 break;
21331 case DW_TAG_subprogram:
21332 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21333 finish_block. */
21334 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21335 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21336 if ((attr2 && (DW_UNSND (attr2) != 0))
21337 || cu->language == language_ada)
21338 {
21339 /* Subprograms marked external are stored as a global symbol.
21340 Ada subprograms, whether marked external or not, are always
21341 stored as a global symbol, because we want to be able to
21342 access them globally. For instance, we want to be able
21343 to break on a nested subprogram without having to
21344 specify the context. */
21345 list_to_add = &global_symbols;
21346 }
21347 else
21348 {
21349 list_to_add = cu->list_in_scope;
21350 }
21351 break;
21352 case DW_TAG_inlined_subroutine:
21353 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21354 finish_block. */
21355 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21356 SYMBOL_INLINED (sym) = 1;
21357 list_to_add = cu->list_in_scope;
21358 break;
21359 case DW_TAG_template_value_param:
21360 suppress_add = 1;
21361 /* Fall through. */
21362 case DW_TAG_constant:
21363 case DW_TAG_variable:
21364 case DW_TAG_member:
21365 /* Compilation with minimal debug info may result in
21366 variables with missing type entries. Change the
21367 misleading `void' type to something sensible. */
21368 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21369 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21370
21371 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21372 /* In the case of DW_TAG_member, we should only be called for
21373 static const members. */
21374 if (die->tag == DW_TAG_member)
21375 {
21376 /* dwarf2_add_field uses die_is_declaration,
21377 so we do the same. */
21378 gdb_assert (die_is_declaration (die, cu));
21379 gdb_assert (attr);
21380 }
21381 if (attr)
21382 {
21383 dwarf2_const_value (attr, sym, cu);
21384 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21385 if (!suppress_add)
21386 {
21387 if (attr2 && (DW_UNSND (attr2) != 0))
21388 list_to_add = &global_symbols;
21389 else
21390 list_to_add = cu->list_in_scope;
21391 }
21392 break;
21393 }
21394 attr = dwarf2_attr (die, DW_AT_location, cu);
21395 if (attr)
21396 {
21397 var_decode_location (attr, sym, cu);
21398 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21399
21400 /* Fortran explicitly imports any global symbols to the local
21401 scope by DW_TAG_common_block. */
21402 if (cu->language == language_fortran && die->parent
21403 && die->parent->tag == DW_TAG_common_block)
21404 attr2 = NULL;
21405
21406 if (SYMBOL_CLASS (sym) == LOC_STATIC
21407 && SYMBOL_VALUE_ADDRESS (sym) == 0
21408 && !dwarf2_per_objfile->has_section_at_zero)
21409 {
21410 /* When a static variable is eliminated by the linker,
21411 the corresponding debug information is not stripped
21412 out, but the variable address is set to null;
21413 do not add such variables into symbol table. */
21414 }
21415 else if (attr2 && (DW_UNSND (attr2) != 0))
21416 {
21417 /* Workaround gfortran PR debug/40040 - it uses
21418 DW_AT_location for variables in -fPIC libraries which may
21419 get overriden by other libraries/executable and get
21420 a different address. Resolve it by the minimal symbol
21421 which may come from inferior's executable using copy
21422 relocation. Make this workaround only for gfortran as for
21423 other compilers GDB cannot guess the minimal symbol
21424 Fortran mangling kind. */
21425 if (cu->language == language_fortran && die->parent
21426 && die->parent->tag == DW_TAG_module
21427 && cu->producer
21428 && startswith (cu->producer, "GNU Fortran"))
21429 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21430
21431 /* A variable with DW_AT_external is never static,
21432 but it may be block-scoped. */
21433 list_to_add = (cu->list_in_scope == &file_symbols
21434 ? &global_symbols : cu->list_in_scope);
21435 }
21436 else
21437 list_to_add = cu->list_in_scope;
21438 }
21439 else
21440 {
21441 /* We do not know the address of this symbol.
21442 If it is an external symbol and we have type information
21443 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21444 The address of the variable will then be determined from
21445 the minimal symbol table whenever the variable is
21446 referenced. */
21447 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21448
21449 /* Fortran explicitly imports any global symbols to the local
21450 scope by DW_TAG_common_block. */
21451 if (cu->language == language_fortran && die->parent
21452 && die->parent->tag == DW_TAG_common_block)
21453 {
21454 /* SYMBOL_CLASS doesn't matter here because
21455 read_common_block is going to reset it. */
21456 if (!suppress_add)
21457 list_to_add = cu->list_in_scope;
21458 }
21459 else if (attr2 && (DW_UNSND (attr2) != 0)
21460 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21461 {
21462 /* A variable with DW_AT_external is never static, but it
21463 may be block-scoped. */
21464 list_to_add = (cu->list_in_scope == &file_symbols
21465 ? &global_symbols : cu->list_in_scope);
21466
21467 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21468 }
21469 else if (!die_is_declaration (die, cu))
21470 {
21471 /* Use the default LOC_OPTIMIZED_OUT class. */
21472 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21473 if (!suppress_add)
21474 list_to_add = cu->list_in_scope;
21475 }
21476 }
21477 break;
21478 case DW_TAG_formal_parameter:
21479 /* If we are inside a function, mark this as an argument. If
21480 not, we might be looking at an argument to an inlined function
21481 when we do not have enough information to show inlined frames;
21482 pretend it's a local variable in that case so that the user can
21483 still see it. */
21484 if (context_stack_depth > 0
21485 && context_stack[context_stack_depth - 1].name != NULL)
21486 SYMBOL_IS_ARGUMENT (sym) = 1;
21487 attr = dwarf2_attr (die, DW_AT_location, cu);
21488 if (attr)
21489 {
21490 var_decode_location (attr, sym, cu);
21491 }
21492 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21493 if (attr)
21494 {
21495 dwarf2_const_value (attr, sym, cu);
21496 }
21497
21498 list_to_add = cu->list_in_scope;
21499 break;
21500 case DW_TAG_unspecified_parameters:
21501 /* From varargs functions; gdb doesn't seem to have any
21502 interest in this information, so just ignore it for now.
21503 (FIXME?) */
21504 break;
21505 case DW_TAG_template_type_param:
21506 suppress_add = 1;
21507 /* Fall through. */
21508 case DW_TAG_class_type:
21509 case DW_TAG_interface_type:
21510 case DW_TAG_structure_type:
21511 case DW_TAG_union_type:
21512 case DW_TAG_set_type:
21513 case DW_TAG_enumeration_type:
21514 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21515 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21516
21517 {
21518 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21519 really ever be static objects: otherwise, if you try
21520 to, say, break of a class's method and you're in a file
21521 which doesn't mention that class, it won't work unless
21522 the check for all static symbols in lookup_symbol_aux
21523 saves you. See the OtherFileClass tests in
21524 gdb.c++/namespace.exp. */
21525
21526 if (!suppress_add)
21527 {
21528 list_to_add = (cu->list_in_scope == &file_symbols
21529 && cu->language == language_cplus
21530 ? &global_symbols : cu->list_in_scope);
21531
21532 /* The semantics of C++ state that "struct foo {
21533 ... }" also defines a typedef for "foo". */
21534 if (cu->language == language_cplus
21535 || cu->language == language_ada
21536 || cu->language == language_d
21537 || cu->language == language_rust)
21538 {
21539 /* The symbol's name is already allocated along
21540 with this objfile, so we don't need to
21541 duplicate it for the type. */
21542 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21543 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21544 }
21545 }
21546 }
21547 break;
21548 case DW_TAG_typedef:
21549 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21550 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21551 list_to_add = cu->list_in_scope;
21552 break;
21553 case DW_TAG_base_type:
21554 case DW_TAG_subrange_type:
21555 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21556 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21557 list_to_add = cu->list_in_scope;
21558 break;
21559 case DW_TAG_enumerator:
21560 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21561 if (attr)
21562 {
21563 dwarf2_const_value (attr, sym, cu);
21564 }
21565 {
21566 /* NOTE: carlton/2003-11-10: See comment above in the
21567 DW_TAG_class_type, etc. block. */
21568
21569 list_to_add = (cu->list_in_scope == &file_symbols
21570 && cu->language == language_cplus
21571 ? &global_symbols : cu->list_in_scope);
21572 }
21573 break;
21574 case DW_TAG_imported_declaration:
21575 case DW_TAG_namespace:
21576 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21577 list_to_add = &global_symbols;
21578 break;
21579 case DW_TAG_module:
21580 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21581 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21582 list_to_add = &global_symbols;
21583 break;
21584 case DW_TAG_common_block:
21585 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21586 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21587 add_symbol_to_list (sym, cu->list_in_scope);
21588 break;
21589 default:
21590 /* Not a tag we recognize. Hopefully we aren't processing
21591 trash data, but since we must specifically ignore things
21592 we don't recognize, there is nothing else we should do at
21593 this point. */
21594 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
21595 dwarf_tag_name (die->tag));
21596 break;
21597 }
21598
21599 if (suppress_add)
21600 {
21601 sym->hash_next = objfile->template_symbols;
21602 objfile->template_symbols = sym;
21603 list_to_add = NULL;
21604 }
21605
21606 if (list_to_add != NULL)
21607 add_symbol_to_list (sym, list_to_add);
21608
21609 /* For the benefit of old versions of GCC, check for anonymous
21610 namespaces based on the demangled name. */
21611 if (!cu->processing_has_namespace_info
21612 && cu->language == language_cplus)
21613 cp_scan_for_anonymous_namespaces (sym, objfile);
21614 }
21615 return (sym);
21616 }
21617
21618 /* Given an attr with a DW_FORM_dataN value in host byte order,
21619 zero-extend it as appropriate for the symbol's type. The DWARF
21620 standard (v4) is not entirely clear about the meaning of using
21621 DW_FORM_dataN for a constant with a signed type, where the type is
21622 wider than the data. The conclusion of a discussion on the DWARF
21623 list was that this is unspecified. We choose to always zero-extend
21624 because that is the interpretation long in use by GCC. */
21625
21626 static gdb_byte *
21627 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21628 struct dwarf2_cu *cu, LONGEST *value, int bits)
21629 {
21630 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21631 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21632 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21633 LONGEST l = DW_UNSND (attr);
21634
21635 if (bits < sizeof (*value) * 8)
21636 {
21637 l &= ((LONGEST) 1 << bits) - 1;
21638 *value = l;
21639 }
21640 else if (bits == sizeof (*value) * 8)
21641 *value = l;
21642 else
21643 {
21644 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21645 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21646 return bytes;
21647 }
21648
21649 return NULL;
21650 }
21651
21652 /* Read a constant value from an attribute. Either set *VALUE, or if
21653 the value does not fit in *VALUE, set *BYTES - either already
21654 allocated on the objfile obstack, or newly allocated on OBSTACK,
21655 or, set *BATON, if we translated the constant to a location
21656 expression. */
21657
21658 static void
21659 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21660 const char *name, struct obstack *obstack,
21661 struct dwarf2_cu *cu,
21662 LONGEST *value, const gdb_byte **bytes,
21663 struct dwarf2_locexpr_baton **baton)
21664 {
21665 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21666 struct comp_unit_head *cu_header = &cu->header;
21667 struct dwarf_block *blk;
21668 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21669 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21670
21671 *value = 0;
21672 *bytes = NULL;
21673 *baton = NULL;
21674
21675 switch (attr->form)
21676 {
21677 case DW_FORM_addr:
21678 case DW_FORM_GNU_addr_index:
21679 {
21680 gdb_byte *data;
21681
21682 if (TYPE_LENGTH (type) != cu_header->addr_size)
21683 dwarf2_const_value_length_mismatch_complaint (name,
21684 cu_header->addr_size,
21685 TYPE_LENGTH (type));
21686 /* Symbols of this form are reasonably rare, so we just
21687 piggyback on the existing location code rather than writing
21688 a new implementation of symbol_computed_ops. */
21689 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21690 (*baton)->per_cu = cu->per_cu;
21691 gdb_assert ((*baton)->per_cu);
21692
21693 (*baton)->size = 2 + cu_header->addr_size;
21694 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21695 (*baton)->data = data;
21696
21697 data[0] = DW_OP_addr;
21698 store_unsigned_integer (&data[1], cu_header->addr_size,
21699 byte_order, DW_ADDR (attr));
21700 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21701 }
21702 break;
21703 case DW_FORM_string:
21704 case DW_FORM_strp:
21705 case DW_FORM_GNU_str_index:
21706 case DW_FORM_GNU_strp_alt:
21707 /* DW_STRING is already allocated on the objfile obstack, point
21708 directly to it. */
21709 *bytes = (const gdb_byte *) DW_STRING (attr);
21710 break;
21711 case DW_FORM_block1:
21712 case DW_FORM_block2:
21713 case DW_FORM_block4:
21714 case DW_FORM_block:
21715 case DW_FORM_exprloc:
21716 case DW_FORM_data16:
21717 blk = DW_BLOCK (attr);
21718 if (TYPE_LENGTH (type) != blk->size)
21719 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21720 TYPE_LENGTH (type));
21721 *bytes = blk->data;
21722 break;
21723
21724 /* The DW_AT_const_value attributes are supposed to carry the
21725 symbol's value "represented as it would be on the target
21726 architecture." By the time we get here, it's already been
21727 converted to host endianness, so we just need to sign- or
21728 zero-extend it as appropriate. */
21729 case DW_FORM_data1:
21730 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21731 break;
21732 case DW_FORM_data2:
21733 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21734 break;
21735 case DW_FORM_data4:
21736 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21737 break;
21738 case DW_FORM_data8:
21739 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21740 break;
21741
21742 case DW_FORM_sdata:
21743 case DW_FORM_implicit_const:
21744 *value = DW_SND (attr);
21745 break;
21746
21747 case DW_FORM_udata:
21748 *value = DW_UNSND (attr);
21749 break;
21750
21751 default:
21752 complaint (&symfile_complaints,
21753 _("unsupported const value attribute form: '%s'"),
21754 dwarf_form_name (attr->form));
21755 *value = 0;
21756 break;
21757 }
21758 }
21759
21760
21761 /* Copy constant value from an attribute to a symbol. */
21762
21763 static void
21764 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21765 struct dwarf2_cu *cu)
21766 {
21767 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21768 LONGEST value;
21769 const gdb_byte *bytes;
21770 struct dwarf2_locexpr_baton *baton;
21771
21772 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21773 SYMBOL_PRINT_NAME (sym),
21774 &objfile->objfile_obstack, cu,
21775 &value, &bytes, &baton);
21776
21777 if (baton != NULL)
21778 {
21779 SYMBOL_LOCATION_BATON (sym) = baton;
21780 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21781 }
21782 else if (bytes != NULL)
21783 {
21784 SYMBOL_VALUE_BYTES (sym) = bytes;
21785 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21786 }
21787 else
21788 {
21789 SYMBOL_VALUE (sym) = value;
21790 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21791 }
21792 }
21793
21794 /* Return the type of the die in question using its DW_AT_type attribute. */
21795
21796 static struct type *
21797 die_type (struct die_info *die, struct dwarf2_cu *cu)
21798 {
21799 struct attribute *type_attr;
21800
21801 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21802 if (!type_attr)
21803 {
21804 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21805 /* A missing DW_AT_type represents a void type. */
21806 return objfile_type (objfile)->builtin_void;
21807 }
21808
21809 return lookup_die_type (die, type_attr, cu);
21810 }
21811
21812 /* True iff CU's producer generates GNAT Ada auxiliary information
21813 that allows to find parallel types through that information instead
21814 of having to do expensive parallel lookups by type name. */
21815
21816 static int
21817 need_gnat_info (struct dwarf2_cu *cu)
21818 {
21819 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
21820 of GNAT produces this auxiliary information, without any indication
21821 that it is produced. Part of enhancing the FSF version of GNAT
21822 to produce that information will be to put in place an indicator
21823 that we can use in order to determine whether the descriptive type
21824 info is available or not. One suggestion that has been made is
21825 to use a new attribute, attached to the CU die. For now, assume
21826 that the descriptive type info is not available. */
21827 return 0;
21828 }
21829
21830 /* Return the auxiliary type of the die in question using its
21831 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21832 attribute is not present. */
21833
21834 static struct type *
21835 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21836 {
21837 struct attribute *type_attr;
21838
21839 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21840 if (!type_attr)
21841 return NULL;
21842
21843 return lookup_die_type (die, type_attr, cu);
21844 }
21845
21846 /* If DIE has a descriptive_type attribute, then set the TYPE's
21847 descriptive type accordingly. */
21848
21849 static void
21850 set_descriptive_type (struct type *type, struct die_info *die,
21851 struct dwarf2_cu *cu)
21852 {
21853 struct type *descriptive_type = die_descriptive_type (die, cu);
21854
21855 if (descriptive_type)
21856 {
21857 ALLOCATE_GNAT_AUX_TYPE (type);
21858 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21859 }
21860 }
21861
21862 /* Return the containing type of the die in question using its
21863 DW_AT_containing_type attribute. */
21864
21865 static struct type *
21866 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21867 {
21868 struct attribute *type_attr;
21869 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21870
21871 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21872 if (!type_attr)
21873 error (_("Dwarf Error: Problem turning containing type into gdb type "
21874 "[in module %s]"), objfile_name (objfile));
21875
21876 return lookup_die_type (die, type_attr, cu);
21877 }
21878
21879 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21880
21881 static struct type *
21882 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21883 {
21884 struct dwarf2_per_objfile *dwarf2_per_objfile
21885 = cu->per_cu->dwarf2_per_objfile;
21886 struct objfile *objfile = dwarf2_per_objfile->objfile;
21887 char *message, *saved;
21888
21889 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
21890 objfile_name (objfile),
21891 to_underlying (cu->header.sect_off),
21892 to_underlying (die->sect_off));
21893 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21894 message, strlen (message));
21895 xfree (message);
21896
21897 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21898 }
21899
21900 /* Look up the type of DIE in CU using its type attribute ATTR.
21901 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21902 DW_AT_containing_type.
21903 If there is no type substitute an error marker. */
21904
21905 static struct type *
21906 lookup_die_type (struct die_info *die, const struct attribute *attr,
21907 struct dwarf2_cu *cu)
21908 {
21909 struct dwarf2_per_objfile *dwarf2_per_objfile
21910 = cu->per_cu->dwarf2_per_objfile;
21911 struct objfile *objfile = dwarf2_per_objfile->objfile;
21912 struct type *this_type;
21913
21914 gdb_assert (attr->name == DW_AT_type
21915 || attr->name == DW_AT_GNAT_descriptive_type
21916 || attr->name == DW_AT_containing_type);
21917
21918 /* First see if we have it cached. */
21919
21920 if (attr->form == DW_FORM_GNU_ref_alt)
21921 {
21922 struct dwarf2_per_cu_data *per_cu;
21923 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21924
21925 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21926 dwarf2_per_objfile);
21927 this_type = get_die_type_at_offset (sect_off, per_cu);
21928 }
21929 else if (attr_form_is_ref (attr))
21930 {
21931 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21932
21933 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21934 }
21935 else if (attr->form == DW_FORM_ref_sig8)
21936 {
21937 ULONGEST signature = DW_SIGNATURE (attr);
21938
21939 return get_signatured_type (die, signature, cu);
21940 }
21941 else
21942 {
21943 complaint (&symfile_complaints,
21944 _("Dwarf Error: Bad type attribute %s in DIE"
21945 " at 0x%x [in module %s]"),
21946 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
21947 objfile_name (objfile));
21948 return build_error_marker_type (cu, die);
21949 }
21950
21951 /* If not cached we need to read it in. */
21952
21953 if (this_type == NULL)
21954 {
21955 struct die_info *type_die = NULL;
21956 struct dwarf2_cu *type_cu = cu;
21957
21958 if (attr_form_is_ref (attr))
21959 type_die = follow_die_ref (die, attr, &type_cu);
21960 if (type_die == NULL)
21961 return build_error_marker_type (cu, die);
21962 /* If we find the type now, it's probably because the type came
21963 from an inter-CU reference and the type's CU got expanded before
21964 ours. */
21965 this_type = read_type_die (type_die, type_cu);
21966 }
21967
21968 /* If we still don't have a type use an error marker. */
21969
21970 if (this_type == NULL)
21971 return build_error_marker_type (cu, die);
21972
21973 return this_type;
21974 }
21975
21976 /* Return the type in DIE, CU.
21977 Returns NULL for invalid types.
21978
21979 This first does a lookup in die_type_hash,
21980 and only reads the die in if necessary.
21981
21982 NOTE: This can be called when reading in partial or full symbols. */
21983
21984 static struct type *
21985 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21986 {
21987 struct type *this_type;
21988
21989 this_type = get_die_type (die, cu);
21990 if (this_type)
21991 return this_type;
21992
21993 return read_type_die_1 (die, cu);
21994 }
21995
21996 /* Read the type in DIE, CU.
21997 Returns NULL for invalid types. */
21998
21999 static struct type *
22000 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22001 {
22002 struct type *this_type = NULL;
22003
22004 switch (die->tag)
22005 {
22006 case DW_TAG_class_type:
22007 case DW_TAG_interface_type:
22008 case DW_TAG_structure_type:
22009 case DW_TAG_union_type:
22010 this_type = read_structure_type (die, cu);
22011 break;
22012 case DW_TAG_enumeration_type:
22013 this_type = read_enumeration_type (die, cu);
22014 break;
22015 case DW_TAG_subprogram:
22016 case DW_TAG_subroutine_type:
22017 case DW_TAG_inlined_subroutine:
22018 this_type = read_subroutine_type (die, cu);
22019 break;
22020 case DW_TAG_array_type:
22021 this_type = read_array_type (die, cu);
22022 break;
22023 case DW_TAG_set_type:
22024 this_type = read_set_type (die, cu);
22025 break;
22026 case DW_TAG_pointer_type:
22027 this_type = read_tag_pointer_type (die, cu);
22028 break;
22029 case DW_TAG_ptr_to_member_type:
22030 this_type = read_tag_ptr_to_member_type (die, cu);
22031 break;
22032 case DW_TAG_reference_type:
22033 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22034 break;
22035 case DW_TAG_rvalue_reference_type:
22036 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22037 break;
22038 case DW_TAG_const_type:
22039 this_type = read_tag_const_type (die, cu);
22040 break;
22041 case DW_TAG_volatile_type:
22042 this_type = read_tag_volatile_type (die, cu);
22043 break;
22044 case DW_TAG_restrict_type:
22045 this_type = read_tag_restrict_type (die, cu);
22046 break;
22047 case DW_TAG_string_type:
22048 this_type = read_tag_string_type (die, cu);
22049 break;
22050 case DW_TAG_typedef:
22051 this_type = read_typedef (die, cu);
22052 break;
22053 case DW_TAG_subrange_type:
22054 this_type = read_subrange_type (die, cu);
22055 break;
22056 case DW_TAG_base_type:
22057 this_type = read_base_type (die, cu);
22058 break;
22059 case DW_TAG_unspecified_type:
22060 this_type = read_unspecified_type (die, cu);
22061 break;
22062 case DW_TAG_namespace:
22063 this_type = read_namespace_type (die, cu);
22064 break;
22065 case DW_TAG_module:
22066 this_type = read_module_type (die, cu);
22067 break;
22068 case DW_TAG_atomic_type:
22069 this_type = read_tag_atomic_type (die, cu);
22070 break;
22071 default:
22072 complaint (&symfile_complaints,
22073 _("unexpected tag in read_type_die: '%s'"),
22074 dwarf_tag_name (die->tag));
22075 break;
22076 }
22077
22078 return this_type;
22079 }
22080
22081 /* See if we can figure out if the class lives in a namespace. We do
22082 this by looking for a member function; its demangled name will
22083 contain namespace info, if there is any.
22084 Return the computed name or NULL.
22085 Space for the result is allocated on the objfile's obstack.
22086 This is the full-die version of guess_partial_die_structure_name.
22087 In this case we know DIE has no useful parent. */
22088
22089 static char *
22090 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22091 {
22092 struct die_info *spec_die;
22093 struct dwarf2_cu *spec_cu;
22094 struct die_info *child;
22095 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22096
22097 spec_cu = cu;
22098 spec_die = die_specification (die, &spec_cu);
22099 if (spec_die != NULL)
22100 {
22101 die = spec_die;
22102 cu = spec_cu;
22103 }
22104
22105 for (child = die->child;
22106 child != NULL;
22107 child = child->sibling)
22108 {
22109 if (child->tag == DW_TAG_subprogram)
22110 {
22111 const char *linkage_name = dw2_linkage_name (child, cu);
22112
22113 if (linkage_name != NULL)
22114 {
22115 char *actual_name
22116 = language_class_name_from_physname (cu->language_defn,
22117 linkage_name);
22118 char *name = NULL;
22119
22120 if (actual_name != NULL)
22121 {
22122 const char *die_name = dwarf2_name (die, cu);
22123
22124 if (die_name != NULL
22125 && strcmp (die_name, actual_name) != 0)
22126 {
22127 /* Strip off the class name from the full name.
22128 We want the prefix. */
22129 int die_name_len = strlen (die_name);
22130 int actual_name_len = strlen (actual_name);
22131
22132 /* Test for '::' as a sanity check. */
22133 if (actual_name_len > die_name_len + 2
22134 && actual_name[actual_name_len
22135 - die_name_len - 1] == ':')
22136 name = (char *) obstack_copy0 (
22137 &objfile->per_bfd->storage_obstack,
22138 actual_name, actual_name_len - die_name_len - 2);
22139 }
22140 }
22141 xfree (actual_name);
22142 return name;
22143 }
22144 }
22145 }
22146
22147 return NULL;
22148 }
22149
22150 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22151 prefix part in such case. See
22152 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22153
22154 static const char *
22155 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22156 {
22157 struct attribute *attr;
22158 const char *base;
22159
22160 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22161 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22162 return NULL;
22163
22164 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22165 return NULL;
22166
22167 attr = dw2_linkage_name_attr (die, cu);
22168 if (attr == NULL || DW_STRING (attr) == NULL)
22169 return NULL;
22170
22171 /* dwarf2_name had to be already called. */
22172 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22173
22174 /* Strip the base name, keep any leading namespaces/classes. */
22175 base = strrchr (DW_STRING (attr), ':');
22176 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22177 return "";
22178
22179 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22180 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22181 DW_STRING (attr),
22182 &base[-1] - DW_STRING (attr));
22183 }
22184
22185 /* Return the name of the namespace/class that DIE is defined within,
22186 or "" if we can't tell. The caller should not xfree the result.
22187
22188 For example, if we're within the method foo() in the following
22189 code:
22190
22191 namespace N {
22192 class C {
22193 void foo () {
22194 }
22195 };
22196 }
22197
22198 then determine_prefix on foo's die will return "N::C". */
22199
22200 static const char *
22201 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22202 {
22203 struct dwarf2_per_objfile *dwarf2_per_objfile
22204 = cu->per_cu->dwarf2_per_objfile;
22205 struct die_info *parent, *spec_die;
22206 struct dwarf2_cu *spec_cu;
22207 struct type *parent_type;
22208 const char *retval;
22209
22210 if (cu->language != language_cplus
22211 && cu->language != language_fortran && cu->language != language_d
22212 && cu->language != language_rust)
22213 return "";
22214
22215 retval = anonymous_struct_prefix (die, cu);
22216 if (retval)
22217 return retval;
22218
22219 /* We have to be careful in the presence of DW_AT_specification.
22220 For example, with GCC 3.4, given the code
22221
22222 namespace N {
22223 void foo() {
22224 // Definition of N::foo.
22225 }
22226 }
22227
22228 then we'll have a tree of DIEs like this:
22229
22230 1: DW_TAG_compile_unit
22231 2: DW_TAG_namespace // N
22232 3: DW_TAG_subprogram // declaration of N::foo
22233 4: DW_TAG_subprogram // definition of N::foo
22234 DW_AT_specification // refers to die #3
22235
22236 Thus, when processing die #4, we have to pretend that we're in
22237 the context of its DW_AT_specification, namely the contex of die
22238 #3. */
22239 spec_cu = cu;
22240 spec_die = die_specification (die, &spec_cu);
22241 if (spec_die == NULL)
22242 parent = die->parent;
22243 else
22244 {
22245 parent = spec_die->parent;
22246 cu = spec_cu;
22247 }
22248
22249 if (parent == NULL)
22250 return "";
22251 else if (parent->building_fullname)
22252 {
22253 const char *name;
22254 const char *parent_name;
22255
22256 /* It has been seen on RealView 2.2 built binaries,
22257 DW_TAG_template_type_param types actually _defined_ as
22258 children of the parent class:
22259
22260 enum E {};
22261 template class <class Enum> Class{};
22262 Class<enum E> class_e;
22263
22264 1: DW_TAG_class_type (Class)
22265 2: DW_TAG_enumeration_type (E)
22266 3: DW_TAG_enumerator (enum1:0)
22267 3: DW_TAG_enumerator (enum2:1)
22268 ...
22269 2: DW_TAG_template_type_param
22270 DW_AT_type DW_FORM_ref_udata (E)
22271
22272 Besides being broken debug info, it can put GDB into an
22273 infinite loop. Consider:
22274
22275 When we're building the full name for Class<E>, we'll start
22276 at Class, and go look over its template type parameters,
22277 finding E. We'll then try to build the full name of E, and
22278 reach here. We're now trying to build the full name of E,
22279 and look over the parent DIE for containing scope. In the
22280 broken case, if we followed the parent DIE of E, we'd again
22281 find Class, and once again go look at its template type
22282 arguments, etc., etc. Simply don't consider such parent die
22283 as source-level parent of this die (it can't be, the language
22284 doesn't allow it), and break the loop here. */
22285 name = dwarf2_name (die, cu);
22286 parent_name = dwarf2_name (parent, cu);
22287 complaint (&symfile_complaints,
22288 _("template param type '%s' defined within parent '%s'"),
22289 name ? name : "<unknown>",
22290 parent_name ? parent_name : "<unknown>");
22291 return "";
22292 }
22293 else
22294 switch (parent->tag)
22295 {
22296 case DW_TAG_namespace:
22297 parent_type = read_type_die (parent, cu);
22298 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22299 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22300 Work around this problem here. */
22301 if (cu->language == language_cplus
22302 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22303 return "";
22304 /* We give a name to even anonymous namespaces. */
22305 return TYPE_TAG_NAME (parent_type);
22306 case DW_TAG_class_type:
22307 case DW_TAG_interface_type:
22308 case DW_TAG_structure_type:
22309 case DW_TAG_union_type:
22310 case DW_TAG_module:
22311 parent_type = read_type_die (parent, cu);
22312 if (TYPE_TAG_NAME (parent_type) != NULL)
22313 return TYPE_TAG_NAME (parent_type);
22314 else
22315 /* An anonymous structure is only allowed non-static data
22316 members; no typedefs, no member functions, et cetera.
22317 So it does not need a prefix. */
22318 return "";
22319 case DW_TAG_compile_unit:
22320 case DW_TAG_partial_unit:
22321 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22322 if (cu->language == language_cplus
22323 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22324 && die->child != NULL
22325 && (die->tag == DW_TAG_class_type
22326 || die->tag == DW_TAG_structure_type
22327 || die->tag == DW_TAG_union_type))
22328 {
22329 char *name = guess_full_die_structure_name (die, cu);
22330 if (name != NULL)
22331 return name;
22332 }
22333 return "";
22334 case DW_TAG_enumeration_type:
22335 parent_type = read_type_die (parent, cu);
22336 if (TYPE_DECLARED_CLASS (parent_type))
22337 {
22338 if (TYPE_TAG_NAME (parent_type) != NULL)
22339 return TYPE_TAG_NAME (parent_type);
22340 return "";
22341 }
22342 /* Fall through. */
22343 default:
22344 return determine_prefix (parent, cu);
22345 }
22346 }
22347
22348 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22349 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22350 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22351 an obconcat, otherwise allocate storage for the result. The CU argument is
22352 used to determine the language and hence, the appropriate separator. */
22353
22354 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22355
22356 static char *
22357 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22358 int physname, struct dwarf2_cu *cu)
22359 {
22360 const char *lead = "";
22361 const char *sep;
22362
22363 if (suffix == NULL || suffix[0] == '\0'
22364 || prefix == NULL || prefix[0] == '\0')
22365 sep = "";
22366 else if (cu->language == language_d)
22367 {
22368 /* For D, the 'main' function could be defined in any module, but it
22369 should never be prefixed. */
22370 if (strcmp (suffix, "D main") == 0)
22371 {
22372 prefix = "";
22373 sep = "";
22374 }
22375 else
22376 sep = ".";
22377 }
22378 else if (cu->language == language_fortran && physname)
22379 {
22380 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22381 DW_AT_MIPS_linkage_name is preferred and used instead. */
22382
22383 lead = "__";
22384 sep = "_MOD_";
22385 }
22386 else
22387 sep = "::";
22388
22389 if (prefix == NULL)
22390 prefix = "";
22391 if (suffix == NULL)
22392 suffix = "";
22393
22394 if (obs == NULL)
22395 {
22396 char *retval
22397 = ((char *)
22398 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22399
22400 strcpy (retval, lead);
22401 strcat (retval, prefix);
22402 strcat (retval, sep);
22403 strcat (retval, suffix);
22404 return retval;
22405 }
22406 else
22407 {
22408 /* We have an obstack. */
22409 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22410 }
22411 }
22412
22413 /* Return sibling of die, NULL if no sibling. */
22414
22415 static struct die_info *
22416 sibling_die (struct die_info *die)
22417 {
22418 return die->sibling;
22419 }
22420
22421 /* Get name of a die, return NULL if not found. */
22422
22423 static const char *
22424 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22425 struct obstack *obstack)
22426 {
22427 if (name && cu->language == language_cplus)
22428 {
22429 std::string canon_name = cp_canonicalize_string (name);
22430
22431 if (!canon_name.empty ())
22432 {
22433 if (canon_name != name)
22434 name = (const char *) obstack_copy0 (obstack,
22435 canon_name.c_str (),
22436 canon_name.length ());
22437 }
22438 }
22439
22440 return name;
22441 }
22442
22443 /* Get name of a die, return NULL if not found.
22444 Anonymous namespaces are converted to their magic string. */
22445
22446 static const char *
22447 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22448 {
22449 struct attribute *attr;
22450 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22451
22452 attr = dwarf2_attr (die, DW_AT_name, cu);
22453 if ((!attr || !DW_STRING (attr))
22454 && die->tag != DW_TAG_namespace
22455 && die->tag != DW_TAG_class_type
22456 && die->tag != DW_TAG_interface_type
22457 && die->tag != DW_TAG_structure_type
22458 && die->tag != DW_TAG_union_type)
22459 return NULL;
22460
22461 switch (die->tag)
22462 {
22463 case DW_TAG_compile_unit:
22464 case DW_TAG_partial_unit:
22465 /* Compilation units have a DW_AT_name that is a filename, not
22466 a source language identifier. */
22467 case DW_TAG_enumeration_type:
22468 case DW_TAG_enumerator:
22469 /* These tags always have simple identifiers already; no need
22470 to canonicalize them. */
22471 return DW_STRING (attr);
22472
22473 case DW_TAG_namespace:
22474 if (attr != NULL && DW_STRING (attr) != NULL)
22475 return DW_STRING (attr);
22476 return CP_ANONYMOUS_NAMESPACE_STR;
22477
22478 case DW_TAG_class_type:
22479 case DW_TAG_interface_type:
22480 case DW_TAG_structure_type:
22481 case DW_TAG_union_type:
22482 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22483 structures or unions. These were of the form "._%d" in GCC 4.1,
22484 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22485 and GCC 4.4. We work around this problem by ignoring these. */
22486 if (attr && DW_STRING (attr)
22487 && (startswith (DW_STRING (attr), "._")
22488 || startswith (DW_STRING (attr), "<anonymous")))
22489 return NULL;
22490
22491 /* GCC might emit a nameless typedef that has a linkage name. See
22492 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22493 if (!attr || DW_STRING (attr) == NULL)
22494 {
22495 char *demangled = NULL;
22496
22497 attr = dw2_linkage_name_attr (die, cu);
22498 if (attr == NULL || DW_STRING (attr) == NULL)
22499 return NULL;
22500
22501 /* Avoid demangling DW_STRING (attr) the second time on a second
22502 call for the same DIE. */
22503 if (!DW_STRING_IS_CANONICAL (attr))
22504 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22505
22506 if (demangled)
22507 {
22508 const char *base;
22509
22510 /* FIXME: we already did this for the partial symbol... */
22511 DW_STRING (attr)
22512 = ((const char *)
22513 obstack_copy0 (&objfile->per_bfd->storage_obstack,
22514 demangled, strlen (demangled)));
22515 DW_STRING_IS_CANONICAL (attr) = 1;
22516 xfree (demangled);
22517
22518 /* Strip any leading namespaces/classes, keep only the base name.
22519 DW_AT_name for named DIEs does not contain the prefixes. */
22520 base = strrchr (DW_STRING (attr), ':');
22521 if (base && base > DW_STRING (attr) && base[-1] == ':')
22522 return &base[1];
22523 else
22524 return DW_STRING (attr);
22525 }
22526 }
22527 break;
22528
22529 default:
22530 break;
22531 }
22532
22533 if (!DW_STRING_IS_CANONICAL (attr))
22534 {
22535 DW_STRING (attr)
22536 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22537 &objfile->per_bfd->storage_obstack);
22538 DW_STRING_IS_CANONICAL (attr) = 1;
22539 }
22540 return DW_STRING (attr);
22541 }
22542
22543 /* Return the die that this die in an extension of, or NULL if there
22544 is none. *EXT_CU is the CU containing DIE on input, and the CU
22545 containing the return value on output. */
22546
22547 static struct die_info *
22548 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22549 {
22550 struct attribute *attr;
22551
22552 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22553 if (attr == NULL)
22554 return NULL;
22555
22556 return follow_die_ref (die, attr, ext_cu);
22557 }
22558
22559 /* Convert a DIE tag into its string name. */
22560
22561 static const char *
22562 dwarf_tag_name (unsigned tag)
22563 {
22564 const char *name = get_DW_TAG_name (tag);
22565
22566 if (name == NULL)
22567 return "DW_TAG_<unknown>";
22568
22569 return name;
22570 }
22571
22572 /* Convert a DWARF attribute code into its string name. */
22573
22574 static const char *
22575 dwarf_attr_name (unsigned attr)
22576 {
22577 const char *name;
22578
22579 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22580 if (attr == DW_AT_MIPS_fde)
22581 return "DW_AT_MIPS_fde";
22582 #else
22583 if (attr == DW_AT_HP_block_index)
22584 return "DW_AT_HP_block_index";
22585 #endif
22586
22587 name = get_DW_AT_name (attr);
22588
22589 if (name == NULL)
22590 return "DW_AT_<unknown>";
22591
22592 return name;
22593 }
22594
22595 /* Convert a DWARF value form code into its string name. */
22596
22597 static const char *
22598 dwarf_form_name (unsigned form)
22599 {
22600 const char *name = get_DW_FORM_name (form);
22601
22602 if (name == NULL)
22603 return "DW_FORM_<unknown>";
22604
22605 return name;
22606 }
22607
22608 static const char *
22609 dwarf_bool_name (unsigned mybool)
22610 {
22611 if (mybool)
22612 return "TRUE";
22613 else
22614 return "FALSE";
22615 }
22616
22617 /* Convert a DWARF type code into its string name. */
22618
22619 static const char *
22620 dwarf_type_encoding_name (unsigned enc)
22621 {
22622 const char *name = get_DW_ATE_name (enc);
22623
22624 if (name == NULL)
22625 return "DW_ATE_<unknown>";
22626
22627 return name;
22628 }
22629
22630 static void
22631 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22632 {
22633 unsigned int i;
22634
22635 print_spaces (indent, f);
22636 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
22637 dwarf_tag_name (die->tag), die->abbrev,
22638 to_underlying (die->sect_off));
22639
22640 if (die->parent != NULL)
22641 {
22642 print_spaces (indent, f);
22643 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
22644 to_underlying (die->parent->sect_off));
22645 }
22646
22647 print_spaces (indent, f);
22648 fprintf_unfiltered (f, " has children: %s\n",
22649 dwarf_bool_name (die->child != NULL));
22650
22651 print_spaces (indent, f);
22652 fprintf_unfiltered (f, " attributes:\n");
22653
22654 for (i = 0; i < die->num_attrs; ++i)
22655 {
22656 print_spaces (indent, f);
22657 fprintf_unfiltered (f, " %s (%s) ",
22658 dwarf_attr_name (die->attrs[i].name),
22659 dwarf_form_name (die->attrs[i].form));
22660
22661 switch (die->attrs[i].form)
22662 {
22663 case DW_FORM_addr:
22664 case DW_FORM_GNU_addr_index:
22665 fprintf_unfiltered (f, "address: ");
22666 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22667 break;
22668 case DW_FORM_block2:
22669 case DW_FORM_block4:
22670 case DW_FORM_block:
22671 case DW_FORM_block1:
22672 fprintf_unfiltered (f, "block: size %s",
22673 pulongest (DW_BLOCK (&die->attrs[i])->size));
22674 break;
22675 case DW_FORM_exprloc:
22676 fprintf_unfiltered (f, "expression: size %s",
22677 pulongest (DW_BLOCK (&die->attrs[i])->size));
22678 break;
22679 case DW_FORM_data16:
22680 fprintf_unfiltered (f, "constant of 16 bytes");
22681 break;
22682 case DW_FORM_ref_addr:
22683 fprintf_unfiltered (f, "ref address: ");
22684 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22685 break;
22686 case DW_FORM_GNU_ref_alt:
22687 fprintf_unfiltered (f, "alt ref address: ");
22688 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22689 break;
22690 case DW_FORM_ref1:
22691 case DW_FORM_ref2:
22692 case DW_FORM_ref4:
22693 case DW_FORM_ref8:
22694 case DW_FORM_ref_udata:
22695 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22696 (long) (DW_UNSND (&die->attrs[i])));
22697 break;
22698 case DW_FORM_data1:
22699 case DW_FORM_data2:
22700 case DW_FORM_data4:
22701 case DW_FORM_data8:
22702 case DW_FORM_udata:
22703 case DW_FORM_sdata:
22704 fprintf_unfiltered (f, "constant: %s",
22705 pulongest (DW_UNSND (&die->attrs[i])));
22706 break;
22707 case DW_FORM_sec_offset:
22708 fprintf_unfiltered (f, "section offset: %s",
22709 pulongest (DW_UNSND (&die->attrs[i])));
22710 break;
22711 case DW_FORM_ref_sig8:
22712 fprintf_unfiltered (f, "signature: %s",
22713 hex_string (DW_SIGNATURE (&die->attrs[i])));
22714 break;
22715 case DW_FORM_string:
22716 case DW_FORM_strp:
22717 case DW_FORM_line_strp:
22718 case DW_FORM_GNU_str_index:
22719 case DW_FORM_GNU_strp_alt:
22720 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22721 DW_STRING (&die->attrs[i])
22722 ? DW_STRING (&die->attrs[i]) : "",
22723 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22724 break;
22725 case DW_FORM_flag:
22726 if (DW_UNSND (&die->attrs[i]))
22727 fprintf_unfiltered (f, "flag: TRUE");
22728 else
22729 fprintf_unfiltered (f, "flag: FALSE");
22730 break;
22731 case DW_FORM_flag_present:
22732 fprintf_unfiltered (f, "flag: TRUE");
22733 break;
22734 case DW_FORM_indirect:
22735 /* The reader will have reduced the indirect form to
22736 the "base form" so this form should not occur. */
22737 fprintf_unfiltered (f,
22738 "unexpected attribute form: DW_FORM_indirect");
22739 break;
22740 case DW_FORM_implicit_const:
22741 fprintf_unfiltered (f, "constant: %s",
22742 plongest (DW_SND (&die->attrs[i])));
22743 break;
22744 default:
22745 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22746 die->attrs[i].form);
22747 break;
22748 }
22749 fprintf_unfiltered (f, "\n");
22750 }
22751 }
22752
22753 static void
22754 dump_die_for_error (struct die_info *die)
22755 {
22756 dump_die_shallow (gdb_stderr, 0, die);
22757 }
22758
22759 static void
22760 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22761 {
22762 int indent = level * 4;
22763
22764 gdb_assert (die != NULL);
22765
22766 if (level >= max_level)
22767 return;
22768
22769 dump_die_shallow (f, indent, die);
22770
22771 if (die->child != NULL)
22772 {
22773 print_spaces (indent, f);
22774 fprintf_unfiltered (f, " Children:");
22775 if (level + 1 < max_level)
22776 {
22777 fprintf_unfiltered (f, "\n");
22778 dump_die_1 (f, level + 1, max_level, die->child);
22779 }
22780 else
22781 {
22782 fprintf_unfiltered (f,
22783 " [not printed, max nesting level reached]\n");
22784 }
22785 }
22786
22787 if (die->sibling != NULL && level > 0)
22788 {
22789 dump_die_1 (f, level, max_level, die->sibling);
22790 }
22791 }
22792
22793 /* This is called from the pdie macro in gdbinit.in.
22794 It's not static so gcc will keep a copy callable from gdb. */
22795
22796 void
22797 dump_die (struct die_info *die, int max_level)
22798 {
22799 dump_die_1 (gdb_stdlog, 0, max_level, die);
22800 }
22801
22802 static void
22803 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22804 {
22805 void **slot;
22806
22807 slot = htab_find_slot_with_hash (cu->die_hash, die,
22808 to_underlying (die->sect_off),
22809 INSERT);
22810
22811 *slot = die;
22812 }
22813
22814 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22815 required kind. */
22816
22817 static sect_offset
22818 dwarf2_get_ref_die_offset (const struct attribute *attr)
22819 {
22820 if (attr_form_is_ref (attr))
22821 return (sect_offset) DW_UNSND (attr);
22822
22823 complaint (&symfile_complaints,
22824 _("unsupported die ref attribute form: '%s'"),
22825 dwarf_form_name (attr->form));
22826 return {};
22827 }
22828
22829 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22830 * the value held by the attribute is not constant. */
22831
22832 static LONGEST
22833 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22834 {
22835 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22836 return DW_SND (attr);
22837 else if (attr->form == DW_FORM_udata
22838 || attr->form == DW_FORM_data1
22839 || attr->form == DW_FORM_data2
22840 || attr->form == DW_FORM_data4
22841 || attr->form == DW_FORM_data8)
22842 return DW_UNSND (attr);
22843 else
22844 {
22845 /* For DW_FORM_data16 see attr_form_is_constant. */
22846 complaint (&symfile_complaints,
22847 _("Attribute value is not a constant (%s)"),
22848 dwarf_form_name (attr->form));
22849 return default_value;
22850 }
22851 }
22852
22853 /* Follow reference or signature attribute ATTR of SRC_DIE.
22854 On entry *REF_CU is the CU of SRC_DIE.
22855 On exit *REF_CU is the CU of the result. */
22856
22857 static struct die_info *
22858 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22859 struct dwarf2_cu **ref_cu)
22860 {
22861 struct die_info *die;
22862
22863 if (attr_form_is_ref (attr))
22864 die = follow_die_ref (src_die, attr, ref_cu);
22865 else if (attr->form == DW_FORM_ref_sig8)
22866 die = follow_die_sig (src_die, attr, ref_cu);
22867 else
22868 {
22869 dump_die_for_error (src_die);
22870 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22871 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22872 }
22873
22874 return die;
22875 }
22876
22877 /* Follow reference OFFSET.
22878 On entry *REF_CU is the CU of the source die referencing OFFSET.
22879 On exit *REF_CU is the CU of the result.
22880 Returns NULL if OFFSET is invalid. */
22881
22882 static struct die_info *
22883 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22884 struct dwarf2_cu **ref_cu)
22885 {
22886 struct die_info temp_die;
22887 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22888 struct dwarf2_per_objfile *dwarf2_per_objfile
22889 = cu->per_cu->dwarf2_per_objfile;
22890 struct objfile *objfile = dwarf2_per_objfile->objfile;
22891
22892 gdb_assert (cu->per_cu != NULL);
22893
22894 target_cu = cu;
22895
22896 if (cu->per_cu->is_debug_types)
22897 {
22898 /* .debug_types CUs cannot reference anything outside their CU.
22899 If they need to, they have to reference a signatured type via
22900 DW_FORM_ref_sig8. */
22901 if (!offset_in_cu_p (&cu->header, sect_off))
22902 return NULL;
22903 }
22904 else if (offset_in_dwz != cu->per_cu->is_dwz
22905 || !offset_in_cu_p (&cu->header, sect_off))
22906 {
22907 struct dwarf2_per_cu_data *per_cu;
22908
22909 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22910 dwarf2_per_objfile);
22911
22912 /* If necessary, add it to the queue and load its DIEs. */
22913 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22914 load_full_comp_unit (per_cu, cu->language);
22915
22916 target_cu = per_cu->cu;
22917 }
22918 else if (cu->dies == NULL)
22919 {
22920 /* We're loading full DIEs during partial symbol reading. */
22921 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22922 load_full_comp_unit (cu->per_cu, language_minimal);
22923 }
22924
22925 *ref_cu = target_cu;
22926 temp_die.sect_off = sect_off;
22927 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22928 &temp_die,
22929 to_underlying (sect_off));
22930 }
22931
22932 /* Follow reference attribute ATTR of SRC_DIE.
22933 On entry *REF_CU is the CU of SRC_DIE.
22934 On exit *REF_CU is the CU of the result. */
22935
22936 static struct die_info *
22937 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22938 struct dwarf2_cu **ref_cu)
22939 {
22940 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22941 struct dwarf2_cu *cu = *ref_cu;
22942 struct die_info *die;
22943
22944 die = follow_die_offset (sect_off,
22945 (attr->form == DW_FORM_GNU_ref_alt
22946 || cu->per_cu->is_dwz),
22947 ref_cu);
22948 if (!die)
22949 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
22950 "at 0x%x [in module %s]"),
22951 to_underlying (sect_off), to_underlying (src_die->sect_off),
22952 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22953
22954 return die;
22955 }
22956
22957 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22958 Returned value is intended for DW_OP_call*. Returned
22959 dwarf2_locexpr_baton->data has lifetime of
22960 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22961
22962 struct dwarf2_locexpr_baton
22963 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22964 struct dwarf2_per_cu_data *per_cu,
22965 CORE_ADDR (*get_frame_pc) (void *baton),
22966 void *baton)
22967 {
22968 struct dwarf2_cu *cu;
22969 struct die_info *die;
22970 struct attribute *attr;
22971 struct dwarf2_locexpr_baton retval;
22972 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22973 struct dwarf2_per_objfile *dwarf2_per_objfile
22974 = get_dwarf2_per_objfile (objfile);
22975
22976 if (per_cu->cu == NULL)
22977 load_cu (per_cu);
22978 cu = per_cu->cu;
22979 if (cu == NULL)
22980 {
22981 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22982 Instead just throw an error, not much else we can do. */
22983 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
22984 to_underlying (sect_off), objfile_name (objfile));
22985 }
22986
22987 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22988 if (!die)
22989 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
22990 to_underlying (sect_off), objfile_name (objfile));
22991
22992 attr = dwarf2_attr (die, DW_AT_location, cu);
22993 if (!attr)
22994 {
22995 /* DWARF: "If there is no such attribute, then there is no effect.".
22996 DATA is ignored if SIZE is 0. */
22997
22998 retval.data = NULL;
22999 retval.size = 0;
23000 }
23001 else if (attr_form_is_section_offset (attr))
23002 {
23003 struct dwarf2_loclist_baton loclist_baton;
23004 CORE_ADDR pc = (*get_frame_pc) (baton);
23005 size_t size;
23006
23007 fill_in_loclist_baton (cu, &loclist_baton, attr);
23008
23009 retval.data = dwarf2_find_location_expression (&loclist_baton,
23010 &size, pc);
23011 retval.size = size;
23012 }
23013 else
23014 {
23015 if (!attr_form_is_block (attr))
23016 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
23017 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23018 to_underlying (sect_off), objfile_name (objfile));
23019
23020 retval.data = DW_BLOCK (attr)->data;
23021 retval.size = DW_BLOCK (attr)->size;
23022 }
23023 retval.per_cu = cu->per_cu;
23024
23025 age_cached_comp_units (dwarf2_per_objfile);
23026
23027 return retval;
23028 }
23029
23030 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23031 offset. */
23032
23033 struct dwarf2_locexpr_baton
23034 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23035 struct dwarf2_per_cu_data *per_cu,
23036 CORE_ADDR (*get_frame_pc) (void *baton),
23037 void *baton)
23038 {
23039 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23040
23041 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23042 }
23043
23044 /* Write a constant of a given type as target-ordered bytes into
23045 OBSTACK. */
23046
23047 static const gdb_byte *
23048 write_constant_as_bytes (struct obstack *obstack,
23049 enum bfd_endian byte_order,
23050 struct type *type,
23051 ULONGEST value,
23052 LONGEST *len)
23053 {
23054 gdb_byte *result;
23055
23056 *len = TYPE_LENGTH (type);
23057 result = (gdb_byte *) obstack_alloc (obstack, *len);
23058 store_unsigned_integer (result, *len, byte_order, value);
23059
23060 return result;
23061 }
23062
23063 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23064 pointer to the constant bytes and set LEN to the length of the
23065 data. If memory is needed, allocate it on OBSTACK. If the DIE
23066 does not have a DW_AT_const_value, return NULL. */
23067
23068 const gdb_byte *
23069 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23070 struct dwarf2_per_cu_data *per_cu,
23071 struct obstack *obstack,
23072 LONGEST *len)
23073 {
23074 struct dwarf2_cu *cu;
23075 struct die_info *die;
23076 struct attribute *attr;
23077 const gdb_byte *result = NULL;
23078 struct type *type;
23079 LONGEST value;
23080 enum bfd_endian byte_order;
23081 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23082
23083 if (per_cu->cu == NULL)
23084 load_cu (per_cu);
23085 cu = per_cu->cu;
23086 if (cu == NULL)
23087 {
23088 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23089 Instead just throw an error, not much else we can do. */
23090 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
23091 to_underlying (sect_off), objfile_name (objfile));
23092 }
23093
23094 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23095 if (!die)
23096 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
23097 to_underlying (sect_off), objfile_name (objfile));
23098
23099
23100 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23101 if (attr == NULL)
23102 return NULL;
23103
23104 byte_order = (bfd_big_endian (objfile->obfd)
23105 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23106
23107 switch (attr->form)
23108 {
23109 case DW_FORM_addr:
23110 case DW_FORM_GNU_addr_index:
23111 {
23112 gdb_byte *tem;
23113
23114 *len = cu->header.addr_size;
23115 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23116 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23117 result = tem;
23118 }
23119 break;
23120 case DW_FORM_string:
23121 case DW_FORM_strp:
23122 case DW_FORM_GNU_str_index:
23123 case DW_FORM_GNU_strp_alt:
23124 /* DW_STRING is already allocated on the objfile obstack, point
23125 directly to it. */
23126 result = (const gdb_byte *) DW_STRING (attr);
23127 *len = strlen (DW_STRING (attr));
23128 break;
23129 case DW_FORM_block1:
23130 case DW_FORM_block2:
23131 case DW_FORM_block4:
23132 case DW_FORM_block:
23133 case DW_FORM_exprloc:
23134 case DW_FORM_data16:
23135 result = DW_BLOCK (attr)->data;
23136 *len = DW_BLOCK (attr)->size;
23137 break;
23138
23139 /* The DW_AT_const_value attributes are supposed to carry the
23140 symbol's value "represented as it would be on the target
23141 architecture." By the time we get here, it's already been
23142 converted to host endianness, so we just need to sign- or
23143 zero-extend it as appropriate. */
23144 case DW_FORM_data1:
23145 type = die_type (die, cu);
23146 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23147 if (result == NULL)
23148 result = write_constant_as_bytes (obstack, byte_order,
23149 type, value, len);
23150 break;
23151 case DW_FORM_data2:
23152 type = die_type (die, cu);
23153 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23154 if (result == NULL)
23155 result = write_constant_as_bytes (obstack, byte_order,
23156 type, value, len);
23157 break;
23158 case DW_FORM_data4:
23159 type = die_type (die, cu);
23160 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23161 if (result == NULL)
23162 result = write_constant_as_bytes (obstack, byte_order,
23163 type, value, len);
23164 break;
23165 case DW_FORM_data8:
23166 type = die_type (die, cu);
23167 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23168 if (result == NULL)
23169 result = write_constant_as_bytes (obstack, byte_order,
23170 type, value, len);
23171 break;
23172
23173 case DW_FORM_sdata:
23174 case DW_FORM_implicit_const:
23175 type = die_type (die, cu);
23176 result = write_constant_as_bytes (obstack, byte_order,
23177 type, DW_SND (attr), len);
23178 break;
23179
23180 case DW_FORM_udata:
23181 type = die_type (die, cu);
23182 result = write_constant_as_bytes (obstack, byte_order,
23183 type, DW_UNSND (attr), len);
23184 break;
23185
23186 default:
23187 complaint (&symfile_complaints,
23188 _("unsupported const value attribute form: '%s'"),
23189 dwarf_form_name (attr->form));
23190 break;
23191 }
23192
23193 return result;
23194 }
23195
23196 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23197 valid type for this die is found. */
23198
23199 struct type *
23200 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23201 struct dwarf2_per_cu_data *per_cu)
23202 {
23203 struct dwarf2_cu *cu;
23204 struct die_info *die;
23205
23206 if (per_cu->cu == NULL)
23207 load_cu (per_cu);
23208 cu = per_cu->cu;
23209 if (!cu)
23210 return NULL;
23211
23212 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23213 if (!die)
23214 return NULL;
23215
23216 return die_type (die, cu);
23217 }
23218
23219 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23220 PER_CU. */
23221
23222 struct type *
23223 dwarf2_get_die_type (cu_offset die_offset,
23224 struct dwarf2_per_cu_data *per_cu)
23225 {
23226 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23227 return get_die_type_at_offset (die_offset_sect, per_cu);
23228 }
23229
23230 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23231 On entry *REF_CU is the CU of SRC_DIE.
23232 On exit *REF_CU is the CU of the result.
23233 Returns NULL if the referenced DIE isn't found. */
23234
23235 static struct die_info *
23236 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23237 struct dwarf2_cu **ref_cu)
23238 {
23239 struct die_info temp_die;
23240 struct dwarf2_cu *sig_cu;
23241 struct die_info *die;
23242
23243 /* While it might be nice to assert sig_type->type == NULL here,
23244 we can get here for DW_AT_imported_declaration where we need
23245 the DIE not the type. */
23246
23247 /* If necessary, add it to the queue and load its DIEs. */
23248
23249 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23250 read_signatured_type (sig_type);
23251
23252 sig_cu = sig_type->per_cu.cu;
23253 gdb_assert (sig_cu != NULL);
23254 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23255 temp_die.sect_off = sig_type->type_offset_in_section;
23256 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23257 to_underlying (temp_die.sect_off));
23258 if (die)
23259 {
23260 struct dwarf2_per_objfile *dwarf2_per_objfile
23261 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23262
23263 /* For .gdb_index version 7 keep track of included TUs.
23264 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23265 if (dwarf2_per_objfile->index_table != NULL
23266 && dwarf2_per_objfile->index_table->version <= 7)
23267 {
23268 VEC_safe_push (dwarf2_per_cu_ptr,
23269 (*ref_cu)->per_cu->imported_symtabs,
23270 sig_cu->per_cu);
23271 }
23272
23273 *ref_cu = sig_cu;
23274 return die;
23275 }
23276
23277 return NULL;
23278 }
23279
23280 /* Follow signatured type referenced by ATTR in SRC_DIE.
23281 On entry *REF_CU is the CU of SRC_DIE.
23282 On exit *REF_CU is the CU of the result.
23283 The result is the DIE of the type.
23284 If the referenced type cannot be found an error is thrown. */
23285
23286 static struct die_info *
23287 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23288 struct dwarf2_cu **ref_cu)
23289 {
23290 ULONGEST signature = DW_SIGNATURE (attr);
23291 struct signatured_type *sig_type;
23292 struct die_info *die;
23293
23294 gdb_assert (attr->form == DW_FORM_ref_sig8);
23295
23296 sig_type = lookup_signatured_type (*ref_cu, signature);
23297 /* sig_type will be NULL if the signatured type is missing from
23298 the debug info. */
23299 if (sig_type == NULL)
23300 {
23301 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23302 " from DIE at 0x%x [in module %s]"),
23303 hex_string (signature), to_underlying (src_die->sect_off),
23304 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23305 }
23306
23307 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23308 if (die == NULL)
23309 {
23310 dump_die_for_error (src_die);
23311 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23312 " from DIE at 0x%x [in module %s]"),
23313 hex_string (signature), to_underlying (src_die->sect_off),
23314 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23315 }
23316
23317 return die;
23318 }
23319
23320 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23321 reading in and processing the type unit if necessary. */
23322
23323 static struct type *
23324 get_signatured_type (struct die_info *die, ULONGEST signature,
23325 struct dwarf2_cu *cu)
23326 {
23327 struct dwarf2_per_objfile *dwarf2_per_objfile
23328 = cu->per_cu->dwarf2_per_objfile;
23329 struct signatured_type *sig_type;
23330 struct dwarf2_cu *type_cu;
23331 struct die_info *type_die;
23332 struct type *type;
23333
23334 sig_type = lookup_signatured_type (cu, signature);
23335 /* sig_type will be NULL if the signatured type is missing from
23336 the debug info. */
23337 if (sig_type == NULL)
23338 {
23339 complaint (&symfile_complaints,
23340 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23341 " from DIE at 0x%x [in module %s]"),
23342 hex_string (signature), to_underlying (die->sect_off),
23343 objfile_name (dwarf2_per_objfile->objfile));
23344 return build_error_marker_type (cu, die);
23345 }
23346
23347 /* If we already know the type we're done. */
23348 if (sig_type->type != NULL)
23349 return sig_type->type;
23350
23351 type_cu = cu;
23352 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23353 if (type_die != NULL)
23354 {
23355 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23356 is created. This is important, for example, because for c++ classes
23357 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23358 type = read_type_die (type_die, type_cu);
23359 if (type == NULL)
23360 {
23361 complaint (&symfile_complaints,
23362 _("Dwarf Error: Cannot build signatured type %s"
23363 " referenced from DIE at 0x%x [in module %s]"),
23364 hex_string (signature), to_underlying (die->sect_off),
23365 objfile_name (dwarf2_per_objfile->objfile));
23366 type = build_error_marker_type (cu, die);
23367 }
23368 }
23369 else
23370 {
23371 complaint (&symfile_complaints,
23372 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23373 " from DIE at 0x%x [in module %s]"),
23374 hex_string (signature), to_underlying (die->sect_off),
23375 objfile_name (dwarf2_per_objfile->objfile));
23376 type = build_error_marker_type (cu, die);
23377 }
23378 sig_type->type = type;
23379
23380 return type;
23381 }
23382
23383 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23384 reading in and processing the type unit if necessary. */
23385
23386 static struct type *
23387 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23388 struct dwarf2_cu *cu) /* ARI: editCase function */
23389 {
23390 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23391 if (attr_form_is_ref (attr))
23392 {
23393 struct dwarf2_cu *type_cu = cu;
23394 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23395
23396 return read_type_die (type_die, type_cu);
23397 }
23398 else if (attr->form == DW_FORM_ref_sig8)
23399 {
23400 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23401 }
23402 else
23403 {
23404 struct dwarf2_per_objfile *dwarf2_per_objfile
23405 = cu->per_cu->dwarf2_per_objfile;
23406
23407 complaint (&symfile_complaints,
23408 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23409 " at 0x%x [in module %s]"),
23410 dwarf_form_name (attr->form), to_underlying (die->sect_off),
23411 objfile_name (dwarf2_per_objfile->objfile));
23412 return build_error_marker_type (cu, die);
23413 }
23414 }
23415
23416 /* Load the DIEs associated with type unit PER_CU into memory. */
23417
23418 static void
23419 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23420 {
23421 struct signatured_type *sig_type;
23422
23423 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23424 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23425
23426 /* We have the per_cu, but we need the signatured_type.
23427 Fortunately this is an easy translation. */
23428 gdb_assert (per_cu->is_debug_types);
23429 sig_type = (struct signatured_type *) per_cu;
23430
23431 gdb_assert (per_cu->cu == NULL);
23432
23433 read_signatured_type (sig_type);
23434
23435 gdb_assert (per_cu->cu != NULL);
23436 }
23437
23438 /* die_reader_func for read_signatured_type.
23439 This is identical to load_full_comp_unit_reader,
23440 but is kept separate for now. */
23441
23442 static void
23443 read_signatured_type_reader (const struct die_reader_specs *reader,
23444 const gdb_byte *info_ptr,
23445 struct die_info *comp_unit_die,
23446 int has_children,
23447 void *data)
23448 {
23449 struct dwarf2_cu *cu = reader->cu;
23450
23451 gdb_assert (cu->die_hash == NULL);
23452 cu->die_hash =
23453 htab_create_alloc_ex (cu->header.length / 12,
23454 die_hash,
23455 die_eq,
23456 NULL,
23457 &cu->comp_unit_obstack,
23458 hashtab_obstack_allocate,
23459 dummy_obstack_deallocate);
23460
23461 if (has_children)
23462 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23463 &info_ptr, comp_unit_die);
23464 cu->dies = comp_unit_die;
23465 /* comp_unit_die is not stored in die_hash, no need. */
23466
23467 /* We try not to read any attributes in this function, because not
23468 all CUs needed for references have been loaded yet, and symbol
23469 table processing isn't initialized. But we have to set the CU language,
23470 or we won't be able to build types correctly.
23471 Similarly, if we do not read the producer, we can not apply
23472 producer-specific interpretation. */
23473 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23474 }
23475
23476 /* Read in a signatured type and build its CU and DIEs.
23477 If the type is a stub for the real type in a DWO file,
23478 read in the real type from the DWO file as well. */
23479
23480 static void
23481 read_signatured_type (struct signatured_type *sig_type)
23482 {
23483 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23484
23485 gdb_assert (per_cu->is_debug_types);
23486 gdb_assert (per_cu->cu == NULL);
23487
23488 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23489 read_signatured_type_reader, NULL);
23490 sig_type->per_cu.tu_read = 1;
23491 }
23492
23493 /* Decode simple location descriptions.
23494 Given a pointer to a dwarf block that defines a location, compute
23495 the location and return the value.
23496
23497 NOTE drow/2003-11-18: This function is called in two situations
23498 now: for the address of static or global variables (partial symbols
23499 only) and for offsets into structures which are expected to be
23500 (more or less) constant. The partial symbol case should go away,
23501 and only the constant case should remain. That will let this
23502 function complain more accurately. A few special modes are allowed
23503 without complaint for global variables (for instance, global
23504 register values and thread-local values).
23505
23506 A location description containing no operations indicates that the
23507 object is optimized out. The return value is 0 for that case.
23508 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23509 callers will only want a very basic result and this can become a
23510 complaint.
23511
23512 Note that stack[0] is unused except as a default error return. */
23513
23514 static CORE_ADDR
23515 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23516 {
23517 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23518 size_t i;
23519 size_t size = blk->size;
23520 const gdb_byte *data = blk->data;
23521 CORE_ADDR stack[64];
23522 int stacki;
23523 unsigned int bytes_read, unsnd;
23524 gdb_byte op;
23525
23526 i = 0;
23527 stacki = 0;
23528 stack[stacki] = 0;
23529 stack[++stacki] = 0;
23530
23531 while (i < size)
23532 {
23533 op = data[i++];
23534 switch (op)
23535 {
23536 case DW_OP_lit0:
23537 case DW_OP_lit1:
23538 case DW_OP_lit2:
23539 case DW_OP_lit3:
23540 case DW_OP_lit4:
23541 case DW_OP_lit5:
23542 case DW_OP_lit6:
23543 case DW_OP_lit7:
23544 case DW_OP_lit8:
23545 case DW_OP_lit9:
23546 case DW_OP_lit10:
23547 case DW_OP_lit11:
23548 case DW_OP_lit12:
23549 case DW_OP_lit13:
23550 case DW_OP_lit14:
23551 case DW_OP_lit15:
23552 case DW_OP_lit16:
23553 case DW_OP_lit17:
23554 case DW_OP_lit18:
23555 case DW_OP_lit19:
23556 case DW_OP_lit20:
23557 case DW_OP_lit21:
23558 case DW_OP_lit22:
23559 case DW_OP_lit23:
23560 case DW_OP_lit24:
23561 case DW_OP_lit25:
23562 case DW_OP_lit26:
23563 case DW_OP_lit27:
23564 case DW_OP_lit28:
23565 case DW_OP_lit29:
23566 case DW_OP_lit30:
23567 case DW_OP_lit31:
23568 stack[++stacki] = op - DW_OP_lit0;
23569 break;
23570
23571 case DW_OP_reg0:
23572 case DW_OP_reg1:
23573 case DW_OP_reg2:
23574 case DW_OP_reg3:
23575 case DW_OP_reg4:
23576 case DW_OP_reg5:
23577 case DW_OP_reg6:
23578 case DW_OP_reg7:
23579 case DW_OP_reg8:
23580 case DW_OP_reg9:
23581 case DW_OP_reg10:
23582 case DW_OP_reg11:
23583 case DW_OP_reg12:
23584 case DW_OP_reg13:
23585 case DW_OP_reg14:
23586 case DW_OP_reg15:
23587 case DW_OP_reg16:
23588 case DW_OP_reg17:
23589 case DW_OP_reg18:
23590 case DW_OP_reg19:
23591 case DW_OP_reg20:
23592 case DW_OP_reg21:
23593 case DW_OP_reg22:
23594 case DW_OP_reg23:
23595 case DW_OP_reg24:
23596 case DW_OP_reg25:
23597 case DW_OP_reg26:
23598 case DW_OP_reg27:
23599 case DW_OP_reg28:
23600 case DW_OP_reg29:
23601 case DW_OP_reg30:
23602 case DW_OP_reg31:
23603 stack[++stacki] = op - DW_OP_reg0;
23604 if (i < size)
23605 dwarf2_complex_location_expr_complaint ();
23606 break;
23607
23608 case DW_OP_regx:
23609 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23610 i += bytes_read;
23611 stack[++stacki] = unsnd;
23612 if (i < size)
23613 dwarf2_complex_location_expr_complaint ();
23614 break;
23615
23616 case DW_OP_addr:
23617 stack[++stacki] = read_address (objfile->obfd, &data[i],
23618 cu, &bytes_read);
23619 i += bytes_read;
23620 break;
23621
23622 case DW_OP_const1u:
23623 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23624 i += 1;
23625 break;
23626
23627 case DW_OP_const1s:
23628 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23629 i += 1;
23630 break;
23631
23632 case DW_OP_const2u:
23633 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23634 i += 2;
23635 break;
23636
23637 case DW_OP_const2s:
23638 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23639 i += 2;
23640 break;
23641
23642 case DW_OP_const4u:
23643 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23644 i += 4;
23645 break;
23646
23647 case DW_OP_const4s:
23648 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23649 i += 4;
23650 break;
23651
23652 case DW_OP_const8u:
23653 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23654 i += 8;
23655 break;
23656
23657 case DW_OP_constu:
23658 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23659 &bytes_read);
23660 i += bytes_read;
23661 break;
23662
23663 case DW_OP_consts:
23664 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23665 i += bytes_read;
23666 break;
23667
23668 case DW_OP_dup:
23669 stack[stacki + 1] = stack[stacki];
23670 stacki++;
23671 break;
23672
23673 case DW_OP_plus:
23674 stack[stacki - 1] += stack[stacki];
23675 stacki--;
23676 break;
23677
23678 case DW_OP_plus_uconst:
23679 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23680 &bytes_read);
23681 i += bytes_read;
23682 break;
23683
23684 case DW_OP_minus:
23685 stack[stacki - 1] -= stack[stacki];
23686 stacki--;
23687 break;
23688
23689 case DW_OP_deref:
23690 /* If we're not the last op, then we definitely can't encode
23691 this using GDB's address_class enum. This is valid for partial
23692 global symbols, although the variable's address will be bogus
23693 in the psymtab. */
23694 if (i < size)
23695 dwarf2_complex_location_expr_complaint ();
23696 break;
23697
23698 case DW_OP_GNU_push_tls_address:
23699 case DW_OP_form_tls_address:
23700 /* The top of the stack has the offset from the beginning
23701 of the thread control block at which the variable is located. */
23702 /* Nothing should follow this operator, so the top of stack would
23703 be returned. */
23704 /* This is valid for partial global symbols, but the variable's
23705 address will be bogus in the psymtab. Make it always at least
23706 non-zero to not look as a variable garbage collected by linker
23707 which have DW_OP_addr 0. */
23708 if (i < size)
23709 dwarf2_complex_location_expr_complaint ();
23710 stack[stacki]++;
23711 break;
23712
23713 case DW_OP_GNU_uninit:
23714 break;
23715
23716 case DW_OP_GNU_addr_index:
23717 case DW_OP_GNU_const_index:
23718 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23719 &bytes_read);
23720 i += bytes_read;
23721 break;
23722
23723 default:
23724 {
23725 const char *name = get_DW_OP_name (op);
23726
23727 if (name)
23728 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23729 name);
23730 else
23731 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23732 op);
23733 }
23734
23735 return (stack[stacki]);
23736 }
23737
23738 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23739 outside of the allocated space. Also enforce minimum>0. */
23740 if (stacki >= ARRAY_SIZE (stack) - 1)
23741 {
23742 complaint (&symfile_complaints,
23743 _("location description stack overflow"));
23744 return 0;
23745 }
23746
23747 if (stacki <= 0)
23748 {
23749 complaint (&symfile_complaints,
23750 _("location description stack underflow"));
23751 return 0;
23752 }
23753 }
23754 return (stack[stacki]);
23755 }
23756
23757 /* memory allocation interface */
23758
23759 static struct dwarf_block *
23760 dwarf_alloc_block (struct dwarf2_cu *cu)
23761 {
23762 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23763 }
23764
23765 static struct die_info *
23766 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23767 {
23768 struct die_info *die;
23769 size_t size = sizeof (struct die_info);
23770
23771 if (num_attrs > 1)
23772 size += (num_attrs - 1) * sizeof (struct attribute);
23773
23774 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23775 memset (die, 0, sizeof (struct die_info));
23776 return (die);
23777 }
23778
23779 \f
23780 /* Macro support. */
23781
23782 /* Return file name relative to the compilation directory of file number I in
23783 *LH's file name table. The result is allocated using xmalloc; the caller is
23784 responsible for freeing it. */
23785
23786 static char *
23787 file_file_name (int file, struct line_header *lh)
23788 {
23789 /* Is the file number a valid index into the line header's file name
23790 table? Remember that file numbers start with one, not zero. */
23791 if (1 <= file && file <= lh->file_names.size ())
23792 {
23793 const file_entry &fe = lh->file_names[file - 1];
23794
23795 if (!IS_ABSOLUTE_PATH (fe.name))
23796 {
23797 const char *dir = fe.include_dir (lh);
23798 if (dir != NULL)
23799 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23800 }
23801 return xstrdup (fe.name);
23802 }
23803 else
23804 {
23805 /* The compiler produced a bogus file number. We can at least
23806 record the macro definitions made in the file, even if we
23807 won't be able to find the file by name. */
23808 char fake_name[80];
23809
23810 xsnprintf (fake_name, sizeof (fake_name),
23811 "<bad macro file number %d>", file);
23812
23813 complaint (&symfile_complaints,
23814 _("bad file number in macro information (%d)"),
23815 file);
23816
23817 return xstrdup (fake_name);
23818 }
23819 }
23820
23821 /* Return the full name of file number I in *LH's file name table.
23822 Use COMP_DIR as the name of the current directory of the
23823 compilation. The result is allocated using xmalloc; the caller is
23824 responsible for freeing it. */
23825 static char *
23826 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23827 {
23828 /* Is the file number a valid index into the line header's file name
23829 table? Remember that file numbers start with one, not zero. */
23830 if (1 <= file && file <= lh->file_names.size ())
23831 {
23832 char *relative = file_file_name (file, lh);
23833
23834 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23835 return relative;
23836 return reconcat (relative, comp_dir, SLASH_STRING,
23837 relative, (char *) NULL);
23838 }
23839 else
23840 return file_file_name (file, lh);
23841 }
23842
23843
23844 static struct macro_source_file *
23845 macro_start_file (int file, int line,
23846 struct macro_source_file *current_file,
23847 struct line_header *lh)
23848 {
23849 /* File name relative to the compilation directory of this source file. */
23850 char *file_name = file_file_name (file, lh);
23851
23852 if (! current_file)
23853 {
23854 /* Note: We don't create a macro table for this compilation unit
23855 at all until we actually get a filename. */
23856 struct macro_table *macro_table = get_macro_table ();
23857
23858 /* If we have no current file, then this must be the start_file
23859 directive for the compilation unit's main source file. */
23860 current_file = macro_set_main (macro_table, file_name);
23861 macro_define_special (macro_table);
23862 }
23863 else
23864 current_file = macro_include (current_file, line, file_name);
23865
23866 xfree (file_name);
23867
23868 return current_file;
23869 }
23870
23871 static const char *
23872 consume_improper_spaces (const char *p, const char *body)
23873 {
23874 if (*p == ' ')
23875 {
23876 complaint (&symfile_complaints,
23877 _("macro definition contains spaces "
23878 "in formal argument list:\n`%s'"),
23879 body);
23880
23881 while (*p == ' ')
23882 p++;
23883 }
23884
23885 return p;
23886 }
23887
23888
23889 static void
23890 parse_macro_definition (struct macro_source_file *file, int line,
23891 const char *body)
23892 {
23893 const char *p;
23894
23895 /* The body string takes one of two forms. For object-like macro
23896 definitions, it should be:
23897
23898 <macro name> " " <definition>
23899
23900 For function-like macro definitions, it should be:
23901
23902 <macro name> "() " <definition>
23903 or
23904 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23905
23906 Spaces may appear only where explicitly indicated, and in the
23907 <definition>.
23908
23909 The Dwarf 2 spec says that an object-like macro's name is always
23910 followed by a space, but versions of GCC around March 2002 omit
23911 the space when the macro's definition is the empty string.
23912
23913 The Dwarf 2 spec says that there should be no spaces between the
23914 formal arguments in a function-like macro's formal argument list,
23915 but versions of GCC around March 2002 include spaces after the
23916 commas. */
23917
23918
23919 /* Find the extent of the macro name. The macro name is terminated
23920 by either a space or null character (for an object-like macro) or
23921 an opening paren (for a function-like macro). */
23922 for (p = body; *p; p++)
23923 if (*p == ' ' || *p == '(')
23924 break;
23925
23926 if (*p == ' ' || *p == '\0')
23927 {
23928 /* It's an object-like macro. */
23929 int name_len = p - body;
23930 char *name = savestring (body, name_len);
23931 const char *replacement;
23932
23933 if (*p == ' ')
23934 replacement = body + name_len + 1;
23935 else
23936 {
23937 dwarf2_macro_malformed_definition_complaint (body);
23938 replacement = body + name_len;
23939 }
23940
23941 macro_define_object (file, line, name, replacement);
23942
23943 xfree (name);
23944 }
23945 else if (*p == '(')
23946 {
23947 /* It's a function-like macro. */
23948 char *name = savestring (body, p - body);
23949 int argc = 0;
23950 int argv_size = 1;
23951 char **argv = XNEWVEC (char *, argv_size);
23952
23953 p++;
23954
23955 p = consume_improper_spaces (p, body);
23956
23957 /* Parse the formal argument list. */
23958 while (*p && *p != ')')
23959 {
23960 /* Find the extent of the current argument name. */
23961 const char *arg_start = p;
23962
23963 while (*p && *p != ',' && *p != ')' && *p != ' ')
23964 p++;
23965
23966 if (! *p || p == arg_start)
23967 dwarf2_macro_malformed_definition_complaint (body);
23968 else
23969 {
23970 /* Make sure argv has room for the new argument. */
23971 if (argc >= argv_size)
23972 {
23973 argv_size *= 2;
23974 argv = XRESIZEVEC (char *, argv, argv_size);
23975 }
23976
23977 argv[argc++] = savestring (arg_start, p - arg_start);
23978 }
23979
23980 p = consume_improper_spaces (p, body);
23981
23982 /* Consume the comma, if present. */
23983 if (*p == ',')
23984 {
23985 p++;
23986
23987 p = consume_improper_spaces (p, body);
23988 }
23989 }
23990
23991 if (*p == ')')
23992 {
23993 p++;
23994
23995 if (*p == ' ')
23996 /* Perfectly formed definition, no complaints. */
23997 macro_define_function (file, line, name,
23998 argc, (const char **) argv,
23999 p + 1);
24000 else if (*p == '\0')
24001 {
24002 /* Complain, but do define it. */
24003 dwarf2_macro_malformed_definition_complaint (body);
24004 macro_define_function (file, line, name,
24005 argc, (const char **) argv,
24006 p);
24007 }
24008 else
24009 /* Just complain. */
24010 dwarf2_macro_malformed_definition_complaint (body);
24011 }
24012 else
24013 /* Just complain. */
24014 dwarf2_macro_malformed_definition_complaint (body);
24015
24016 xfree (name);
24017 {
24018 int i;
24019
24020 for (i = 0; i < argc; i++)
24021 xfree (argv[i]);
24022 }
24023 xfree (argv);
24024 }
24025 else
24026 dwarf2_macro_malformed_definition_complaint (body);
24027 }
24028
24029 /* Skip some bytes from BYTES according to the form given in FORM.
24030 Returns the new pointer. */
24031
24032 static const gdb_byte *
24033 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24034 enum dwarf_form form,
24035 unsigned int offset_size,
24036 struct dwarf2_section_info *section)
24037 {
24038 unsigned int bytes_read;
24039
24040 switch (form)
24041 {
24042 case DW_FORM_data1:
24043 case DW_FORM_flag:
24044 ++bytes;
24045 break;
24046
24047 case DW_FORM_data2:
24048 bytes += 2;
24049 break;
24050
24051 case DW_FORM_data4:
24052 bytes += 4;
24053 break;
24054
24055 case DW_FORM_data8:
24056 bytes += 8;
24057 break;
24058
24059 case DW_FORM_data16:
24060 bytes += 16;
24061 break;
24062
24063 case DW_FORM_string:
24064 read_direct_string (abfd, bytes, &bytes_read);
24065 bytes += bytes_read;
24066 break;
24067
24068 case DW_FORM_sec_offset:
24069 case DW_FORM_strp:
24070 case DW_FORM_GNU_strp_alt:
24071 bytes += offset_size;
24072 break;
24073
24074 case DW_FORM_block:
24075 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24076 bytes += bytes_read;
24077 break;
24078
24079 case DW_FORM_block1:
24080 bytes += 1 + read_1_byte (abfd, bytes);
24081 break;
24082 case DW_FORM_block2:
24083 bytes += 2 + read_2_bytes (abfd, bytes);
24084 break;
24085 case DW_FORM_block4:
24086 bytes += 4 + read_4_bytes (abfd, bytes);
24087 break;
24088
24089 case DW_FORM_sdata:
24090 case DW_FORM_udata:
24091 case DW_FORM_GNU_addr_index:
24092 case DW_FORM_GNU_str_index:
24093 bytes = gdb_skip_leb128 (bytes, buffer_end);
24094 if (bytes == NULL)
24095 {
24096 dwarf2_section_buffer_overflow_complaint (section);
24097 return NULL;
24098 }
24099 break;
24100
24101 case DW_FORM_implicit_const:
24102 break;
24103
24104 default:
24105 {
24106 complaint (&symfile_complaints,
24107 _("invalid form 0x%x in `%s'"),
24108 form, get_section_name (section));
24109 return NULL;
24110 }
24111 }
24112
24113 return bytes;
24114 }
24115
24116 /* A helper for dwarf_decode_macros that handles skipping an unknown
24117 opcode. Returns an updated pointer to the macro data buffer; or,
24118 on error, issues a complaint and returns NULL. */
24119
24120 static const gdb_byte *
24121 skip_unknown_opcode (unsigned int opcode,
24122 const gdb_byte **opcode_definitions,
24123 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24124 bfd *abfd,
24125 unsigned int offset_size,
24126 struct dwarf2_section_info *section)
24127 {
24128 unsigned int bytes_read, i;
24129 unsigned long arg;
24130 const gdb_byte *defn;
24131
24132 if (opcode_definitions[opcode] == NULL)
24133 {
24134 complaint (&symfile_complaints,
24135 _("unrecognized DW_MACFINO opcode 0x%x"),
24136 opcode);
24137 return NULL;
24138 }
24139
24140 defn = opcode_definitions[opcode];
24141 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24142 defn += bytes_read;
24143
24144 for (i = 0; i < arg; ++i)
24145 {
24146 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24147 (enum dwarf_form) defn[i], offset_size,
24148 section);
24149 if (mac_ptr == NULL)
24150 {
24151 /* skip_form_bytes already issued the complaint. */
24152 return NULL;
24153 }
24154 }
24155
24156 return mac_ptr;
24157 }
24158
24159 /* A helper function which parses the header of a macro section.
24160 If the macro section is the extended (for now called "GNU") type,
24161 then this updates *OFFSET_SIZE. Returns a pointer to just after
24162 the header, or issues a complaint and returns NULL on error. */
24163
24164 static const gdb_byte *
24165 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24166 bfd *abfd,
24167 const gdb_byte *mac_ptr,
24168 unsigned int *offset_size,
24169 int section_is_gnu)
24170 {
24171 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24172
24173 if (section_is_gnu)
24174 {
24175 unsigned int version, flags;
24176
24177 version = read_2_bytes (abfd, mac_ptr);
24178 if (version != 4 && version != 5)
24179 {
24180 complaint (&symfile_complaints,
24181 _("unrecognized version `%d' in .debug_macro section"),
24182 version);
24183 return NULL;
24184 }
24185 mac_ptr += 2;
24186
24187 flags = read_1_byte (abfd, mac_ptr);
24188 ++mac_ptr;
24189 *offset_size = (flags & 1) ? 8 : 4;
24190
24191 if ((flags & 2) != 0)
24192 /* We don't need the line table offset. */
24193 mac_ptr += *offset_size;
24194
24195 /* Vendor opcode descriptions. */
24196 if ((flags & 4) != 0)
24197 {
24198 unsigned int i, count;
24199
24200 count = read_1_byte (abfd, mac_ptr);
24201 ++mac_ptr;
24202 for (i = 0; i < count; ++i)
24203 {
24204 unsigned int opcode, bytes_read;
24205 unsigned long arg;
24206
24207 opcode = read_1_byte (abfd, mac_ptr);
24208 ++mac_ptr;
24209 opcode_definitions[opcode] = mac_ptr;
24210 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24211 mac_ptr += bytes_read;
24212 mac_ptr += arg;
24213 }
24214 }
24215 }
24216
24217 return mac_ptr;
24218 }
24219
24220 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24221 including DW_MACRO_import. */
24222
24223 static void
24224 dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24225 bfd *abfd,
24226 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24227 struct macro_source_file *current_file,
24228 struct line_header *lh,
24229 struct dwarf2_section_info *section,
24230 int section_is_gnu, int section_is_dwz,
24231 unsigned int offset_size,
24232 htab_t include_hash)
24233 {
24234 struct objfile *objfile = dwarf2_per_objfile->objfile;
24235 enum dwarf_macro_record_type macinfo_type;
24236 int at_commandline;
24237 const gdb_byte *opcode_definitions[256];
24238
24239 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24240 &offset_size, section_is_gnu);
24241 if (mac_ptr == NULL)
24242 {
24243 /* We already issued a complaint. */
24244 return;
24245 }
24246
24247 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24248 GDB is still reading the definitions from command line. First
24249 DW_MACINFO_start_file will need to be ignored as it was already executed
24250 to create CURRENT_FILE for the main source holding also the command line
24251 definitions. On first met DW_MACINFO_start_file this flag is reset to
24252 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24253
24254 at_commandline = 1;
24255
24256 do
24257 {
24258 /* Do we at least have room for a macinfo type byte? */
24259 if (mac_ptr >= mac_end)
24260 {
24261 dwarf2_section_buffer_overflow_complaint (section);
24262 break;
24263 }
24264
24265 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24266 mac_ptr++;
24267
24268 /* Note that we rely on the fact that the corresponding GNU and
24269 DWARF constants are the same. */
24270 DIAGNOSTIC_PUSH
24271 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24272 switch (macinfo_type)
24273 {
24274 /* A zero macinfo type indicates the end of the macro
24275 information. */
24276 case 0:
24277 break;
24278
24279 case DW_MACRO_define:
24280 case DW_MACRO_undef:
24281 case DW_MACRO_define_strp:
24282 case DW_MACRO_undef_strp:
24283 case DW_MACRO_define_sup:
24284 case DW_MACRO_undef_sup:
24285 {
24286 unsigned int bytes_read;
24287 int line;
24288 const char *body;
24289 int is_define;
24290
24291 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24292 mac_ptr += bytes_read;
24293
24294 if (macinfo_type == DW_MACRO_define
24295 || macinfo_type == DW_MACRO_undef)
24296 {
24297 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24298 mac_ptr += bytes_read;
24299 }
24300 else
24301 {
24302 LONGEST str_offset;
24303
24304 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24305 mac_ptr += offset_size;
24306
24307 if (macinfo_type == DW_MACRO_define_sup
24308 || macinfo_type == DW_MACRO_undef_sup
24309 || section_is_dwz)
24310 {
24311 struct dwz_file *dwz
24312 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24313
24314 body = read_indirect_string_from_dwz (objfile,
24315 dwz, str_offset);
24316 }
24317 else
24318 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24319 abfd, str_offset);
24320 }
24321
24322 is_define = (macinfo_type == DW_MACRO_define
24323 || macinfo_type == DW_MACRO_define_strp
24324 || macinfo_type == DW_MACRO_define_sup);
24325 if (! current_file)
24326 {
24327 /* DWARF violation as no main source is present. */
24328 complaint (&symfile_complaints,
24329 _("debug info with no main source gives macro %s "
24330 "on line %d: %s"),
24331 is_define ? _("definition") : _("undefinition"),
24332 line, body);
24333 break;
24334 }
24335 if ((line == 0 && !at_commandline)
24336 || (line != 0 && at_commandline))
24337 complaint (&symfile_complaints,
24338 _("debug info gives %s macro %s with %s line %d: %s"),
24339 at_commandline ? _("command-line") : _("in-file"),
24340 is_define ? _("definition") : _("undefinition"),
24341 line == 0 ? _("zero") : _("non-zero"), line, body);
24342
24343 if (is_define)
24344 parse_macro_definition (current_file, line, body);
24345 else
24346 {
24347 gdb_assert (macinfo_type == DW_MACRO_undef
24348 || macinfo_type == DW_MACRO_undef_strp
24349 || macinfo_type == DW_MACRO_undef_sup);
24350 macro_undef (current_file, line, body);
24351 }
24352 }
24353 break;
24354
24355 case DW_MACRO_start_file:
24356 {
24357 unsigned int bytes_read;
24358 int line, file;
24359
24360 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24361 mac_ptr += bytes_read;
24362 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24363 mac_ptr += bytes_read;
24364
24365 if ((line == 0 && !at_commandline)
24366 || (line != 0 && at_commandline))
24367 complaint (&symfile_complaints,
24368 _("debug info gives source %d included "
24369 "from %s at %s line %d"),
24370 file, at_commandline ? _("command-line") : _("file"),
24371 line == 0 ? _("zero") : _("non-zero"), line);
24372
24373 if (at_commandline)
24374 {
24375 /* This DW_MACRO_start_file was executed in the
24376 pass one. */
24377 at_commandline = 0;
24378 }
24379 else
24380 current_file = macro_start_file (file, line, current_file, lh);
24381 }
24382 break;
24383
24384 case DW_MACRO_end_file:
24385 if (! current_file)
24386 complaint (&symfile_complaints,
24387 _("macro debug info has an unmatched "
24388 "`close_file' directive"));
24389 else
24390 {
24391 current_file = current_file->included_by;
24392 if (! current_file)
24393 {
24394 enum dwarf_macro_record_type next_type;
24395
24396 /* GCC circa March 2002 doesn't produce the zero
24397 type byte marking the end of the compilation
24398 unit. Complain if it's not there, but exit no
24399 matter what. */
24400
24401 /* Do we at least have room for a macinfo type byte? */
24402 if (mac_ptr >= mac_end)
24403 {
24404 dwarf2_section_buffer_overflow_complaint (section);
24405 return;
24406 }
24407
24408 /* We don't increment mac_ptr here, so this is just
24409 a look-ahead. */
24410 next_type
24411 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24412 mac_ptr);
24413 if (next_type != 0)
24414 complaint (&symfile_complaints,
24415 _("no terminating 0-type entry for "
24416 "macros in `.debug_macinfo' section"));
24417
24418 return;
24419 }
24420 }
24421 break;
24422
24423 case DW_MACRO_import:
24424 case DW_MACRO_import_sup:
24425 {
24426 LONGEST offset;
24427 void **slot;
24428 bfd *include_bfd = abfd;
24429 struct dwarf2_section_info *include_section = section;
24430 const gdb_byte *include_mac_end = mac_end;
24431 int is_dwz = section_is_dwz;
24432 const gdb_byte *new_mac_ptr;
24433
24434 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24435 mac_ptr += offset_size;
24436
24437 if (macinfo_type == DW_MACRO_import_sup)
24438 {
24439 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24440
24441 dwarf2_read_section (objfile, &dwz->macro);
24442
24443 include_section = &dwz->macro;
24444 include_bfd = get_section_bfd_owner (include_section);
24445 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24446 is_dwz = 1;
24447 }
24448
24449 new_mac_ptr = include_section->buffer + offset;
24450 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24451
24452 if (*slot != NULL)
24453 {
24454 /* This has actually happened; see
24455 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24456 complaint (&symfile_complaints,
24457 _("recursive DW_MACRO_import in "
24458 ".debug_macro section"));
24459 }
24460 else
24461 {
24462 *slot = (void *) new_mac_ptr;
24463
24464 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24465 include_bfd, new_mac_ptr,
24466 include_mac_end, current_file, lh,
24467 section, section_is_gnu, is_dwz,
24468 offset_size, include_hash);
24469
24470 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24471 }
24472 }
24473 break;
24474
24475 case DW_MACINFO_vendor_ext:
24476 if (!section_is_gnu)
24477 {
24478 unsigned int bytes_read;
24479
24480 /* This reads the constant, but since we don't recognize
24481 any vendor extensions, we ignore it. */
24482 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24483 mac_ptr += bytes_read;
24484 read_direct_string (abfd, mac_ptr, &bytes_read);
24485 mac_ptr += bytes_read;
24486
24487 /* We don't recognize any vendor extensions. */
24488 break;
24489 }
24490 /* FALLTHROUGH */
24491
24492 default:
24493 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24494 mac_ptr, mac_end, abfd, offset_size,
24495 section);
24496 if (mac_ptr == NULL)
24497 return;
24498 break;
24499 }
24500 DIAGNOSTIC_POP
24501 } while (macinfo_type != 0);
24502 }
24503
24504 static void
24505 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24506 int section_is_gnu)
24507 {
24508 struct dwarf2_per_objfile *dwarf2_per_objfile
24509 = cu->per_cu->dwarf2_per_objfile;
24510 struct objfile *objfile = dwarf2_per_objfile->objfile;
24511 struct line_header *lh = cu->line_header;
24512 bfd *abfd;
24513 const gdb_byte *mac_ptr, *mac_end;
24514 struct macro_source_file *current_file = 0;
24515 enum dwarf_macro_record_type macinfo_type;
24516 unsigned int offset_size = cu->header.offset_size;
24517 const gdb_byte *opcode_definitions[256];
24518 void **slot;
24519 struct dwarf2_section_info *section;
24520 const char *section_name;
24521
24522 if (cu->dwo_unit != NULL)
24523 {
24524 if (section_is_gnu)
24525 {
24526 section = &cu->dwo_unit->dwo_file->sections.macro;
24527 section_name = ".debug_macro.dwo";
24528 }
24529 else
24530 {
24531 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24532 section_name = ".debug_macinfo.dwo";
24533 }
24534 }
24535 else
24536 {
24537 if (section_is_gnu)
24538 {
24539 section = &dwarf2_per_objfile->macro;
24540 section_name = ".debug_macro";
24541 }
24542 else
24543 {
24544 section = &dwarf2_per_objfile->macinfo;
24545 section_name = ".debug_macinfo";
24546 }
24547 }
24548
24549 dwarf2_read_section (objfile, section);
24550 if (section->buffer == NULL)
24551 {
24552 complaint (&symfile_complaints, _("missing %s section"), section_name);
24553 return;
24554 }
24555 abfd = get_section_bfd_owner (section);
24556
24557 /* First pass: Find the name of the base filename.
24558 This filename is needed in order to process all macros whose definition
24559 (or undefinition) comes from the command line. These macros are defined
24560 before the first DW_MACINFO_start_file entry, and yet still need to be
24561 associated to the base file.
24562
24563 To determine the base file name, we scan the macro definitions until we
24564 reach the first DW_MACINFO_start_file entry. We then initialize
24565 CURRENT_FILE accordingly so that any macro definition found before the
24566 first DW_MACINFO_start_file can still be associated to the base file. */
24567
24568 mac_ptr = section->buffer + offset;
24569 mac_end = section->buffer + section->size;
24570
24571 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24572 &offset_size, section_is_gnu);
24573 if (mac_ptr == NULL)
24574 {
24575 /* We already issued a complaint. */
24576 return;
24577 }
24578
24579 do
24580 {
24581 /* Do we at least have room for a macinfo type byte? */
24582 if (mac_ptr >= mac_end)
24583 {
24584 /* Complaint is printed during the second pass as GDB will probably
24585 stop the first pass earlier upon finding
24586 DW_MACINFO_start_file. */
24587 break;
24588 }
24589
24590 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24591 mac_ptr++;
24592
24593 /* Note that we rely on the fact that the corresponding GNU and
24594 DWARF constants are the same. */
24595 DIAGNOSTIC_PUSH
24596 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24597 switch (macinfo_type)
24598 {
24599 /* A zero macinfo type indicates the end of the macro
24600 information. */
24601 case 0:
24602 break;
24603
24604 case DW_MACRO_define:
24605 case DW_MACRO_undef:
24606 /* Only skip the data by MAC_PTR. */
24607 {
24608 unsigned int bytes_read;
24609
24610 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24611 mac_ptr += bytes_read;
24612 read_direct_string (abfd, mac_ptr, &bytes_read);
24613 mac_ptr += bytes_read;
24614 }
24615 break;
24616
24617 case DW_MACRO_start_file:
24618 {
24619 unsigned int bytes_read;
24620 int line, file;
24621
24622 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24623 mac_ptr += bytes_read;
24624 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24625 mac_ptr += bytes_read;
24626
24627 current_file = macro_start_file (file, line, current_file, lh);
24628 }
24629 break;
24630
24631 case DW_MACRO_end_file:
24632 /* No data to skip by MAC_PTR. */
24633 break;
24634
24635 case DW_MACRO_define_strp:
24636 case DW_MACRO_undef_strp:
24637 case DW_MACRO_define_sup:
24638 case DW_MACRO_undef_sup:
24639 {
24640 unsigned int bytes_read;
24641
24642 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24643 mac_ptr += bytes_read;
24644 mac_ptr += offset_size;
24645 }
24646 break;
24647
24648 case DW_MACRO_import:
24649 case DW_MACRO_import_sup:
24650 /* Note that, according to the spec, a transparent include
24651 chain cannot call DW_MACRO_start_file. So, we can just
24652 skip this opcode. */
24653 mac_ptr += offset_size;
24654 break;
24655
24656 case DW_MACINFO_vendor_ext:
24657 /* Only skip the data by MAC_PTR. */
24658 if (!section_is_gnu)
24659 {
24660 unsigned int bytes_read;
24661
24662 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24663 mac_ptr += bytes_read;
24664 read_direct_string (abfd, mac_ptr, &bytes_read);
24665 mac_ptr += bytes_read;
24666 }
24667 /* FALLTHROUGH */
24668
24669 default:
24670 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24671 mac_ptr, mac_end, abfd, offset_size,
24672 section);
24673 if (mac_ptr == NULL)
24674 return;
24675 break;
24676 }
24677 DIAGNOSTIC_POP
24678 } while (macinfo_type != 0 && current_file == NULL);
24679
24680 /* Second pass: Process all entries.
24681
24682 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24683 command-line macro definitions/undefinitions. This flag is unset when we
24684 reach the first DW_MACINFO_start_file entry. */
24685
24686 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24687 htab_eq_pointer,
24688 NULL, xcalloc, xfree));
24689 mac_ptr = section->buffer + offset;
24690 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24691 *slot = (void *) mac_ptr;
24692 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24693 abfd, mac_ptr, mac_end,
24694 current_file, lh, section,
24695 section_is_gnu, 0, offset_size,
24696 include_hash.get ());
24697 }
24698
24699 /* Check if the attribute's form is a DW_FORM_block*
24700 if so return true else false. */
24701
24702 static int
24703 attr_form_is_block (const struct attribute *attr)
24704 {
24705 return (attr == NULL ? 0 :
24706 attr->form == DW_FORM_block1
24707 || attr->form == DW_FORM_block2
24708 || attr->form == DW_FORM_block4
24709 || attr->form == DW_FORM_block
24710 || attr->form == DW_FORM_exprloc);
24711 }
24712
24713 /* Return non-zero if ATTR's value is a section offset --- classes
24714 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24715 You may use DW_UNSND (attr) to retrieve such offsets.
24716
24717 Section 7.5.4, "Attribute Encodings", explains that no attribute
24718 may have a value that belongs to more than one of these classes; it
24719 would be ambiguous if we did, because we use the same forms for all
24720 of them. */
24721
24722 static int
24723 attr_form_is_section_offset (const struct attribute *attr)
24724 {
24725 return (attr->form == DW_FORM_data4
24726 || attr->form == DW_FORM_data8
24727 || attr->form == DW_FORM_sec_offset);
24728 }
24729
24730 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24731 zero otherwise. When this function returns true, you can apply
24732 dwarf2_get_attr_constant_value to it.
24733
24734 However, note that for some attributes you must check
24735 attr_form_is_section_offset before using this test. DW_FORM_data4
24736 and DW_FORM_data8 are members of both the constant class, and of
24737 the classes that contain offsets into other debug sections
24738 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24739 that, if an attribute's can be either a constant or one of the
24740 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
24741 taken as section offsets, not constants.
24742
24743 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24744 cannot handle that. */
24745
24746 static int
24747 attr_form_is_constant (const struct attribute *attr)
24748 {
24749 switch (attr->form)
24750 {
24751 case DW_FORM_sdata:
24752 case DW_FORM_udata:
24753 case DW_FORM_data1:
24754 case DW_FORM_data2:
24755 case DW_FORM_data4:
24756 case DW_FORM_data8:
24757 case DW_FORM_implicit_const:
24758 return 1;
24759 default:
24760 return 0;
24761 }
24762 }
24763
24764
24765 /* DW_ADDR is always stored already as sect_offset; despite for the forms
24766 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24767
24768 static int
24769 attr_form_is_ref (const struct attribute *attr)
24770 {
24771 switch (attr->form)
24772 {
24773 case DW_FORM_ref_addr:
24774 case DW_FORM_ref1:
24775 case DW_FORM_ref2:
24776 case DW_FORM_ref4:
24777 case DW_FORM_ref8:
24778 case DW_FORM_ref_udata:
24779 case DW_FORM_GNU_ref_alt:
24780 return 1;
24781 default:
24782 return 0;
24783 }
24784 }
24785
24786 /* Return the .debug_loc section to use for CU.
24787 For DWO files use .debug_loc.dwo. */
24788
24789 static struct dwarf2_section_info *
24790 cu_debug_loc_section (struct dwarf2_cu *cu)
24791 {
24792 struct dwarf2_per_objfile *dwarf2_per_objfile
24793 = cu->per_cu->dwarf2_per_objfile;
24794
24795 if (cu->dwo_unit)
24796 {
24797 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24798
24799 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24800 }
24801 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24802 : &dwarf2_per_objfile->loc);
24803 }
24804
24805 /* A helper function that fills in a dwarf2_loclist_baton. */
24806
24807 static void
24808 fill_in_loclist_baton (struct dwarf2_cu *cu,
24809 struct dwarf2_loclist_baton *baton,
24810 const struct attribute *attr)
24811 {
24812 struct dwarf2_per_objfile *dwarf2_per_objfile
24813 = cu->per_cu->dwarf2_per_objfile;
24814 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24815
24816 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
24817
24818 baton->per_cu = cu->per_cu;
24819 gdb_assert (baton->per_cu);
24820 /* We don't know how long the location list is, but make sure we
24821 don't run off the edge of the section. */
24822 baton->size = section->size - DW_UNSND (attr);
24823 baton->data = section->buffer + DW_UNSND (attr);
24824 baton->base_address = cu->base_address;
24825 baton->from_dwo = cu->dwo_unit != NULL;
24826 }
24827
24828 static void
24829 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24830 struct dwarf2_cu *cu, int is_block)
24831 {
24832 struct dwarf2_per_objfile *dwarf2_per_objfile
24833 = cu->per_cu->dwarf2_per_objfile;
24834 struct objfile *objfile = dwarf2_per_objfile->objfile;
24835 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24836
24837 if (attr_form_is_section_offset (attr)
24838 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24839 the section. If so, fall through to the complaint in the
24840 other branch. */
24841 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24842 {
24843 struct dwarf2_loclist_baton *baton;
24844
24845 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24846
24847 fill_in_loclist_baton (cu, baton, attr);
24848
24849 if (cu->base_known == 0)
24850 complaint (&symfile_complaints,
24851 _("Location list used without "
24852 "specifying the CU base address."));
24853
24854 SYMBOL_ACLASS_INDEX (sym) = (is_block
24855 ? dwarf2_loclist_block_index
24856 : dwarf2_loclist_index);
24857 SYMBOL_LOCATION_BATON (sym) = baton;
24858 }
24859 else
24860 {
24861 struct dwarf2_locexpr_baton *baton;
24862
24863 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24864 baton->per_cu = cu->per_cu;
24865 gdb_assert (baton->per_cu);
24866
24867 if (attr_form_is_block (attr))
24868 {
24869 /* Note that we're just copying the block's data pointer
24870 here, not the actual data. We're still pointing into the
24871 info_buffer for SYM's objfile; right now we never release
24872 that buffer, but when we do clean up properly this may
24873 need to change. */
24874 baton->size = DW_BLOCK (attr)->size;
24875 baton->data = DW_BLOCK (attr)->data;
24876 }
24877 else
24878 {
24879 dwarf2_invalid_attrib_class_complaint ("location description",
24880 SYMBOL_NATURAL_NAME (sym));
24881 baton->size = 0;
24882 }
24883
24884 SYMBOL_ACLASS_INDEX (sym) = (is_block
24885 ? dwarf2_locexpr_block_index
24886 : dwarf2_locexpr_index);
24887 SYMBOL_LOCATION_BATON (sym) = baton;
24888 }
24889 }
24890
24891 /* Return the OBJFILE associated with the compilation unit CU. If CU
24892 came from a separate debuginfo file, then the master objfile is
24893 returned. */
24894
24895 struct objfile *
24896 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24897 {
24898 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24899
24900 /* Return the master objfile, so that we can report and look up the
24901 correct file containing this variable. */
24902 if (objfile->separate_debug_objfile_backlink)
24903 objfile = objfile->separate_debug_objfile_backlink;
24904
24905 return objfile;
24906 }
24907
24908 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24909 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24910 CU_HEADERP first. */
24911
24912 static const struct comp_unit_head *
24913 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24914 struct dwarf2_per_cu_data *per_cu)
24915 {
24916 const gdb_byte *info_ptr;
24917
24918 if (per_cu->cu)
24919 return &per_cu->cu->header;
24920
24921 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24922
24923 memset (cu_headerp, 0, sizeof (*cu_headerp));
24924 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24925 rcuh_kind::COMPILE);
24926
24927 return cu_headerp;
24928 }
24929
24930 /* Return the address size given in the compilation unit header for CU. */
24931
24932 int
24933 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24934 {
24935 struct comp_unit_head cu_header_local;
24936 const struct comp_unit_head *cu_headerp;
24937
24938 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24939
24940 return cu_headerp->addr_size;
24941 }
24942
24943 /* Return the offset size given in the compilation unit header for CU. */
24944
24945 int
24946 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24947 {
24948 struct comp_unit_head cu_header_local;
24949 const struct comp_unit_head *cu_headerp;
24950
24951 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24952
24953 return cu_headerp->offset_size;
24954 }
24955
24956 /* See its dwarf2loc.h declaration. */
24957
24958 int
24959 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24960 {
24961 struct comp_unit_head cu_header_local;
24962 const struct comp_unit_head *cu_headerp;
24963
24964 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24965
24966 if (cu_headerp->version == 2)
24967 return cu_headerp->addr_size;
24968 else
24969 return cu_headerp->offset_size;
24970 }
24971
24972 /* Return the text offset of the CU. The returned offset comes from
24973 this CU's objfile. If this objfile came from a separate debuginfo
24974 file, then the offset may be different from the corresponding
24975 offset in the parent objfile. */
24976
24977 CORE_ADDR
24978 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24979 {
24980 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24981
24982 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24983 }
24984
24985 /* Return DWARF version number of PER_CU. */
24986
24987 short
24988 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24989 {
24990 return per_cu->dwarf_version;
24991 }
24992
24993 /* Locate the .debug_info compilation unit from CU's objfile which contains
24994 the DIE at OFFSET. Raises an error on failure. */
24995
24996 static struct dwarf2_per_cu_data *
24997 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24998 unsigned int offset_in_dwz,
24999 struct dwarf2_per_objfile *dwarf2_per_objfile)
25000 {
25001 struct dwarf2_per_cu_data *this_cu;
25002 int low, high;
25003 const sect_offset *cu_off;
25004
25005 low = 0;
25006 high = dwarf2_per_objfile->n_comp_units - 1;
25007 while (high > low)
25008 {
25009 struct dwarf2_per_cu_data *mid_cu;
25010 int mid = low + (high - low) / 2;
25011
25012 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25013 cu_off = &mid_cu->sect_off;
25014 if (mid_cu->is_dwz > offset_in_dwz
25015 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
25016 high = mid;
25017 else
25018 low = mid + 1;
25019 }
25020 gdb_assert (low == high);
25021 this_cu = dwarf2_per_objfile->all_comp_units[low];
25022 cu_off = &this_cu->sect_off;
25023 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
25024 {
25025 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25026 error (_("Dwarf Error: could not find partial DIE containing "
25027 "offset 0x%x [in module %s]"),
25028 to_underlying (sect_off),
25029 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25030
25031 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25032 <= sect_off);
25033 return dwarf2_per_objfile->all_comp_units[low-1];
25034 }
25035 else
25036 {
25037 this_cu = dwarf2_per_objfile->all_comp_units[low];
25038 if (low == dwarf2_per_objfile->n_comp_units - 1
25039 && sect_off >= this_cu->sect_off + this_cu->length)
25040 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
25041 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25042 return this_cu;
25043 }
25044 }
25045
25046 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25047
25048 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25049 : per_cu (per_cu_),
25050 mark (0),
25051 has_loclist (0),
25052 checked_producer (0),
25053 producer_is_gxx_lt_4_6 (0),
25054 producer_is_gcc_lt_4_3 (0),
25055 producer_is_icc_lt_14 (0),
25056 processing_has_namespace_info (0)
25057 {
25058 per_cu->cu = this;
25059 }
25060
25061 /* Destroy a dwarf2_cu. */
25062
25063 dwarf2_cu::~dwarf2_cu ()
25064 {
25065 per_cu->cu = NULL;
25066 }
25067
25068 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25069
25070 static void
25071 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25072 enum language pretend_language)
25073 {
25074 struct attribute *attr;
25075
25076 /* Set the language we're debugging. */
25077 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25078 if (attr)
25079 set_cu_language (DW_UNSND (attr), cu);
25080 else
25081 {
25082 cu->language = pretend_language;
25083 cu->language_defn = language_def (cu->language);
25084 }
25085
25086 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25087 }
25088
25089 /* Free all cached compilation units. */
25090
25091 static void
25092 free_cached_comp_units (void *data)
25093 {
25094 struct dwarf2_per_objfile *dwarf2_per_objfile
25095 = (struct dwarf2_per_objfile *) data;
25096
25097 dwarf2_per_objfile->free_cached_comp_units ();
25098 }
25099
25100 /* Increase the age counter on each cached compilation unit, and free
25101 any that are too old. */
25102
25103 static void
25104 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25105 {
25106 struct dwarf2_per_cu_data *per_cu, **last_chain;
25107
25108 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25109 per_cu = dwarf2_per_objfile->read_in_chain;
25110 while (per_cu != NULL)
25111 {
25112 per_cu->cu->last_used ++;
25113 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25114 dwarf2_mark (per_cu->cu);
25115 per_cu = per_cu->cu->read_in_chain;
25116 }
25117
25118 per_cu = dwarf2_per_objfile->read_in_chain;
25119 last_chain = &dwarf2_per_objfile->read_in_chain;
25120 while (per_cu != NULL)
25121 {
25122 struct dwarf2_per_cu_data *next_cu;
25123
25124 next_cu = per_cu->cu->read_in_chain;
25125
25126 if (!per_cu->cu->mark)
25127 {
25128 delete per_cu->cu;
25129 *last_chain = next_cu;
25130 }
25131 else
25132 last_chain = &per_cu->cu->read_in_chain;
25133
25134 per_cu = next_cu;
25135 }
25136 }
25137
25138 /* Remove a single compilation unit from the cache. */
25139
25140 static void
25141 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25142 {
25143 struct dwarf2_per_cu_data *per_cu, **last_chain;
25144 struct dwarf2_per_objfile *dwarf2_per_objfile
25145 = target_per_cu->dwarf2_per_objfile;
25146
25147 per_cu = dwarf2_per_objfile->read_in_chain;
25148 last_chain = &dwarf2_per_objfile->read_in_chain;
25149 while (per_cu != NULL)
25150 {
25151 struct dwarf2_per_cu_data *next_cu;
25152
25153 next_cu = per_cu->cu->read_in_chain;
25154
25155 if (per_cu == target_per_cu)
25156 {
25157 delete per_cu->cu;
25158 per_cu->cu = NULL;
25159 *last_chain = next_cu;
25160 break;
25161 }
25162 else
25163 last_chain = &per_cu->cu->read_in_chain;
25164
25165 per_cu = next_cu;
25166 }
25167 }
25168
25169 /* Release all extra memory associated with OBJFILE. */
25170
25171 void
25172 dwarf2_free_objfile (struct objfile *objfile)
25173 {
25174 struct dwarf2_per_objfile *dwarf2_per_objfile
25175 = get_dwarf2_per_objfile (objfile);
25176
25177 if (dwarf2_per_objfile == NULL)
25178 return;
25179
25180 dwarf2_per_objfile->~dwarf2_per_objfile ();
25181 }
25182
25183 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25184 We store these in a hash table separate from the DIEs, and preserve them
25185 when the DIEs are flushed out of cache.
25186
25187 The CU "per_cu" pointer is needed because offset alone is not enough to
25188 uniquely identify the type. A file may have multiple .debug_types sections,
25189 or the type may come from a DWO file. Furthermore, while it's more logical
25190 to use per_cu->section+offset, with Fission the section with the data is in
25191 the DWO file but we don't know that section at the point we need it.
25192 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25193 because we can enter the lookup routine, get_die_type_at_offset, from
25194 outside this file, and thus won't necessarily have PER_CU->cu.
25195 Fortunately, PER_CU is stable for the life of the objfile. */
25196
25197 struct dwarf2_per_cu_offset_and_type
25198 {
25199 const struct dwarf2_per_cu_data *per_cu;
25200 sect_offset sect_off;
25201 struct type *type;
25202 };
25203
25204 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25205
25206 static hashval_t
25207 per_cu_offset_and_type_hash (const void *item)
25208 {
25209 const struct dwarf2_per_cu_offset_and_type *ofs
25210 = (const struct dwarf2_per_cu_offset_and_type *) item;
25211
25212 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25213 }
25214
25215 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25216
25217 static int
25218 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25219 {
25220 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25221 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25222 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25223 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25224
25225 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25226 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25227 }
25228
25229 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25230 table if necessary. For convenience, return TYPE.
25231
25232 The DIEs reading must have careful ordering to:
25233 * Not cause infite loops trying to read in DIEs as a prerequisite for
25234 reading current DIE.
25235 * Not trying to dereference contents of still incompletely read in types
25236 while reading in other DIEs.
25237 * Enable referencing still incompletely read in types just by a pointer to
25238 the type without accessing its fields.
25239
25240 Therefore caller should follow these rules:
25241 * Try to fetch any prerequisite types we may need to build this DIE type
25242 before building the type and calling set_die_type.
25243 * After building type call set_die_type for current DIE as soon as
25244 possible before fetching more types to complete the current type.
25245 * Make the type as complete as possible before fetching more types. */
25246
25247 static struct type *
25248 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25249 {
25250 struct dwarf2_per_objfile *dwarf2_per_objfile
25251 = cu->per_cu->dwarf2_per_objfile;
25252 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25253 struct objfile *objfile = dwarf2_per_objfile->objfile;
25254 struct attribute *attr;
25255 struct dynamic_prop prop;
25256
25257 /* For Ada types, make sure that the gnat-specific data is always
25258 initialized (if not already set). There are a few types where
25259 we should not be doing so, because the type-specific area is
25260 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25261 where the type-specific area is used to store the floatformat).
25262 But this is not a problem, because the gnat-specific information
25263 is actually not needed for these types. */
25264 if (need_gnat_info (cu)
25265 && TYPE_CODE (type) != TYPE_CODE_FUNC
25266 && TYPE_CODE (type) != TYPE_CODE_FLT
25267 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25268 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25269 && TYPE_CODE (type) != TYPE_CODE_METHOD
25270 && !HAVE_GNAT_AUX_INFO (type))
25271 INIT_GNAT_SPECIFIC (type);
25272
25273 /* Read DW_AT_allocated and set in type. */
25274 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25275 if (attr_form_is_block (attr))
25276 {
25277 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25278 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25279 }
25280 else if (attr != NULL)
25281 {
25282 complaint (&symfile_complaints,
25283 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
25284 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25285 to_underlying (die->sect_off));
25286 }
25287
25288 /* Read DW_AT_associated and set in type. */
25289 attr = dwarf2_attr (die, DW_AT_associated, cu);
25290 if (attr_form_is_block (attr))
25291 {
25292 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25293 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25294 }
25295 else if (attr != NULL)
25296 {
25297 complaint (&symfile_complaints,
25298 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
25299 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25300 to_underlying (die->sect_off));
25301 }
25302
25303 /* Read DW_AT_data_location and set in type. */
25304 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25305 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25306 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25307
25308 if (dwarf2_per_objfile->die_type_hash == NULL)
25309 {
25310 dwarf2_per_objfile->die_type_hash =
25311 htab_create_alloc_ex (127,
25312 per_cu_offset_and_type_hash,
25313 per_cu_offset_and_type_eq,
25314 NULL,
25315 &objfile->objfile_obstack,
25316 hashtab_obstack_allocate,
25317 dummy_obstack_deallocate);
25318 }
25319
25320 ofs.per_cu = cu->per_cu;
25321 ofs.sect_off = die->sect_off;
25322 ofs.type = type;
25323 slot = (struct dwarf2_per_cu_offset_and_type **)
25324 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25325 if (*slot)
25326 complaint (&symfile_complaints,
25327 _("A problem internal to GDB: DIE 0x%x has type already set"),
25328 to_underlying (die->sect_off));
25329 *slot = XOBNEW (&objfile->objfile_obstack,
25330 struct dwarf2_per_cu_offset_and_type);
25331 **slot = ofs;
25332 return type;
25333 }
25334
25335 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25336 or return NULL if the die does not have a saved type. */
25337
25338 static struct type *
25339 get_die_type_at_offset (sect_offset sect_off,
25340 struct dwarf2_per_cu_data *per_cu)
25341 {
25342 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25343 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25344
25345 if (dwarf2_per_objfile->die_type_hash == NULL)
25346 return NULL;
25347
25348 ofs.per_cu = per_cu;
25349 ofs.sect_off = sect_off;
25350 slot = ((struct dwarf2_per_cu_offset_and_type *)
25351 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25352 if (slot)
25353 return slot->type;
25354 else
25355 return NULL;
25356 }
25357
25358 /* Look up the type for DIE in CU in die_type_hash,
25359 or return NULL if DIE does not have a saved type. */
25360
25361 static struct type *
25362 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25363 {
25364 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25365 }
25366
25367 /* Add a dependence relationship from CU to REF_PER_CU. */
25368
25369 static void
25370 dwarf2_add_dependence (struct dwarf2_cu *cu,
25371 struct dwarf2_per_cu_data *ref_per_cu)
25372 {
25373 void **slot;
25374
25375 if (cu->dependencies == NULL)
25376 cu->dependencies
25377 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25378 NULL, &cu->comp_unit_obstack,
25379 hashtab_obstack_allocate,
25380 dummy_obstack_deallocate);
25381
25382 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25383 if (*slot == NULL)
25384 *slot = ref_per_cu;
25385 }
25386
25387 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25388 Set the mark field in every compilation unit in the
25389 cache that we must keep because we are keeping CU. */
25390
25391 static int
25392 dwarf2_mark_helper (void **slot, void *data)
25393 {
25394 struct dwarf2_per_cu_data *per_cu;
25395
25396 per_cu = (struct dwarf2_per_cu_data *) *slot;
25397
25398 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25399 reading of the chain. As such dependencies remain valid it is not much
25400 useful to track and undo them during QUIT cleanups. */
25401 if (per_cu->cu == NULL)
25402 return 1;
25403
25404 if (per_cu->cu->mark)
25405 return 1;
25406 per_cu->cu->mark = 1;
25407
25408 if (per_cu->cu->dependencies != NULL)
25409 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25410
25411 return 1;
25412 }
25413
25414 /* Set the mark field in CU and in every other compilation unit in the
25415 cache that we must keep because we are keeping CU. */
25416
25417 static void
25418 dwarf2_mark (struct dwarf2_cu *cu)
25419 {
25420 if (cu->mark)
25421 return;
25422 cu->mark = 1;
25423 if (cu->dependencies != NULL)
25424 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25425 }
25426
25427 static void
25428 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25429 {
25430 while (per_cu)
25431 {
25432 per_cu->cu->mark = 0;
25433 per_cu = per_cu->cu->read_in_chain;
25434 }
25435 }
25436
25437 /* Trivial hash function for partial_die_info: the hash value of a DIE
25438 is its offset in .debug_info for this objfile. */
25439
25440 static hashval_t
25441 partial_die_hash (const void *item)
25442 {
25443 const struct partial_die_info *part_die
25444 = (const struct partial_die_info *) item;
25445
25446 return to_underlying (part_die->sect_off);
25447 }
25448
25449 /* Trivial comparison function for partial_die_info structures: two DIEs
25450 are equal if they have the same offset. */
25451
25452 static int
25453 partial_die_eq (const void *item_lhs, const void *item_rhs)
25454 {
25455 const struct partial_die_info *part_die_lhs
25456 = (const struct partial_die_info *) item_lhs;
25457 const struct partial_die_info *part_die_rhs
25458 = (const struct partial_die_info *) item_rhs;
25459
25460 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25461 }
25462
25463 static struct cmd_list_element *set_dwarf_cmdlist;
25464 static struct cmd_list_element *show_dwarf_cmdlist;
25465
25466 static void
25467 set_dwarf_cmd (const char *args, int from_tty)
25468 {
25469 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25470 gdb_stdout);
25471 }
25472
25473 static void
25474 show_dwarf_cmd (const char *args, int from_tty)
25475 {
25476 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25477 }
25478
25479 /* Free data associated with OBJFILE, if necessary. */
25480
25481 static void
25482 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
25483 {
25484 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
25485 int ix;
25486
25487 for (ix = 0; ix < data->n_comp_units; ++ix)
25488 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
25489
25490 for (ix = 0; ix < data->n_type_units; ++ix)
25491 VEC_free (dwarf2_per_cu_ptr,
25492 data->all_type_units[ix]->per_cu.imported_symtabs);
25493 xfree (data->all_type_units);
25494
25495 VEC_free (dwarf2_section_info_def, data->types);
25496
25497 if (data->dwo_files)
25498 free_dwo_files (data->dwo_files, objfile);
25499 if (data->dwp_file)
25500 gdb_bfd_unref (data->dwp_file->dbfd);
25501
25502 if (data->dwz_file && data->dwz_file->dwz_bfd)
25503 gdb_bfd_unref (data->dwz_file->dwz_bfd);
25504
25505 if (data->index_table != NULL)
25506 data->index_table->~mapped_index ();
25507 }
25508
25509 \f
25510 /* The "save gdb-index" command. */
25511
25512 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25513 error checking. */
25514
25515 static void
25516 file_write (FILE *file, const void *data, size_t size)
25517 {
25518 if (fwrite (data, 1, size, file) != size)
25519 error (_("couldn't data write to file"));
25520 }
25521
25522 /* Write the contents of VEC to FILE, with error checking. */
25523
25524 template<typename Elem, typename Alloc>
25525 static void
25526 file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
25527 {
25528 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
25529 }
25530
25531 /* In-memory buffer to prepare data to be written later to a file. */
25532 class data_buf
25533 {
25534 public:
25535 /* Copy DATA to the end of the buffer. */
25536 template<typename T>
25537 void append_data (const T &data)
25538 {
25539 std::copy (reinterpret_cast<const gdb_byte *> (&data),
25540 reinterpret_cast<const gdb_byte *> (&data + 1),
25541 grow (sizeof (data)));
25542 }
25543
25544 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
25545 terminating zero is appended too. */
25546 void append_cstr0 (const char *cstr)
25547 {
25548 const size_t size = strlen (cstr) + 1;
25549 std::copy (cstr, cstr + size, grow (size));
25550 }
25551
25552 /* Store INPUT as ULEB128 to the end of buffer. */
25553 void append_unsigned_leb128 (ULONGEST input)
25554 {
25555 for (;;)
25556 {
25557 gdb_byte output = input & 0x7f;
25558 input >>= 7;
25559 if (input)
25560 output |= 0x80;
25561 append_data (output);
25562 if (input == 0)
25563 break;
25564 }
25565 }
25566
25567 /* Accept a host-format integer in VAL and append it to the buffer
25568 as a target-format integer which is LEN bytes long. */
25569 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
25570 {
25571 ::store_unsigned_integer (grow (len), len, byte_order, val);
25572 }
25573
25574 /* Return the size of the buffer. */
25575 size_t size () const
25576 {
25577 return m_vec.size ();
25578 }
25579
25580 /* Return true iff the buffer is empty. */
25581 bool empty () const
25582 {
25583 return m_vec.empty ();
25584 }
25585
25586 /* Write the buffer to FILE. */
25587 void file_write (FILE *file) const
25588 {
25589 ::file_write (file, m_vec);
25590 }
25591
25592 private:
25593 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
25594 the start of the new block. */
25595 gdb_byte *grow (size_t size)
25596 {
25597 m_vec.resize (m_vec.size () + size);
25598 return &*m_vec.end () - size;
25599 }
25600
25601 gdb::byte_vector m_vec;
25602 };
25603
25604 /* An entry in the symbol table. */
25605 struct symtab_index_entry
25606 {
25607 /* The name of the symbol. */
25608 const char *name;
25609 /* The offset of the name in the constant pool. */
25610 offset_type index_offset;
25611 /* A sorted vector of the indices of all the CUs that hold an object
25612 of this name. */
25613 std::vector<offset_type> cu_indices;
25614 };
25615
25616 /* The symbol table. This is a power-of-2-sized hash table. */
25617 struct mapped_symtab
25618 {
25619 mapped_symtab ()
25620 {
25621 data.resize (1024);
25622 }
25623
25624 offset_type n_elements = 0;
25625 std::vector<symtab_index_entry> data;
25626 };
25627
25628 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
25629 the slot.
25630
25631 Function is used only during write_hash_table so no index format backward
25632 compatibility is needed. */
25633
25634 static symtab_index_entry &
25635 find_slot (struct mapped_symtab *symtab, const char *name)
25636 {
25637 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
25638
25639 index = hash & (symtab->data.size () - 1);
25640 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
25641
25642 for (;;)
25643 {
25644 if (symtab->data[index].name == NULL
25645 || strcmp (name, symtab->data[index].name) == 0)
25646 return symtab->data[index];
25647 index = (index + step) & (symtab->data.size () - 1);
25648 }
25649 }
25650
25651 /* Expand SYMTAB's hash table. */
25652
25653 static void
25654 hash_expand (struct mapped_symtab *symtab)
25655 {
25656 auto old_entries = std::move (symtab->data);
25657
25658 symtab->data.clear ();
25659 symtab->data.resize (old_entries.size () * 2);
25660
25661 for (auto &it : old_entries)
25662 if (it.name != NULL)
25663 {
25664 auto &ref = find_slot (symtab, it.name);
25665 ref = std::move (it);
25666 }
25667 }
25668
25669 /* Add an entry to SYMTAB. NAME is the name of the symbol.
25670 CU_INDEX is the index of the CU in which the symbol appears.
25671 IS_STATIC is one if the symbol is static, otherwise zero (global). */
25672
25673 static void
25674 add_index_entry (struct mapped_symtab *symtab, const char *name,
25675 int is_static, gdb_index_symbol_kind kind,
25676 offset_type cu_index)
25677 {
25678 offset_type cu_index_and_attrs;
25679
25680 ++symtab->n_elements;
25681 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
25682 hash_expand (symtab);
25683
25684 symtab_index_entry &slot = find_slot (symtab, name);
25685 if (slot.name == NULL)
25686 {
25687 slot.name = name;
25688 /* index_offset is set later. */
25689 }
25690
25691 cu_index_and_attrs = 0;
25692 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
25693 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
25694 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
25695
25696 /* We don't want to record an index value twice as we want to avoid the
25697 duplication.
25698 We process all global symbols and then all static symbols
25699 (which would allow us to avoid the duplication by only having to check
25700 the last entry pushed), but a symbol could have multiple kinds in one CU.
25701 To keep things simple we don't worry about the duplication here and
25702 sort and uniqufy the list after we've processed all symbols. */
25703 slot.cu_indices.push_back (cu_index_and_attrs);
25704 }
25705
25706 /* Sort and remove duplicates of all symbols' cu_indices lists. */
25707
25708 static void
25709 uniquify_cu_indices (struct mapped_symtab *symtab)
25710 {
25711 for (auto &entry : symtab->data)
25712 {
25713 if (entry.name != NULL && !entry.cu_indices.empty ())
25714 {
25715 auto &cu_indices = entry.cu_indices;
25716 std::sort (cu_indices.begin (), cu_indices.end ());
25717 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
25718 cu_indices.erase (from, cu_indices.end ());
25719 }
25720 }
25721 }
25722
25723 /* A form of 'const char *' suitable for container keys. Only the
25724 pointer is stored. The strings themselves are compared, not the
25725 pointers. */
25726 class c_str_view
25727 {
25728 public:
25729 c_str_view (const char *cstr)
25730 : m_cstr (cstr)
25731 {}
25732
25733 bool operator== (const c_str_view &other) const
25734 {
25735 return strcmp (m_cstr, other.m_cstr) == 0;
25736 }
25737
25738 /* Return the underlying C string. Note, the returned string is
25739 only a reference with lifetime of this object. */
25740 const char *c_str () const
25741 {
25742 return m_cstr;
25743 }
25744
25745 private:
25746 friend class c_str_view_hasher;
25747 const char *const m_cstr;
25748 };
25749
25750 /* A std::unordered_map::hasher for c_str_view that uses the right
25751 hash function for strings in a mapped index. */
25752 class c_str_view_hasher
25753 {
25754 public:
25755 size_t operator () (const c_str_view &x) const
25756 {
25757 return mapped_index_string_hash (INT_MAX, x.m_cstr);
25758 }
25759 };
25760
25761 /* A std::unordered_map::hasher for std::vector<>. */
25762 template<typename T>
25763 class vector_hasher
25764 {
25765 public:
25766 size_t operator () (const std::vector<T> &key) const
25767 {
25768 return iterative_hash (key.data (),
25769 sizeof (key.front ()) * key.size (), 0);
25770 }
25771 };
25772
25773 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
25774 constant pool entries going into the data buffer CPOOL. */
25775
25776 static void
25777 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
25778 {
25779 {
25780 /* Elements are sorted vectors of the indices of all the CUs that
25781 hold an object of this name. */
25782 std::unordered_map<std::vector<offset_type>, offset_type,
25783 vector_hasher<offset_type>>
25784 symbol_hash_table;
25785
25786 /* We add all the index vectors to the constant pool first, to
25787 ensure alignment is ok. */
25788 for (symtab_index_entry &entry : symtab->data)
25789 {
25790 if (entry.name == NULL)
25791 continue;
25792 gdb_assert (entry.index_offset == 0);
25793
25794 /* Finding before inserting is faster than always trying to
25795 insert, because inserting always allocates a node, does the
25796 lookup, and then destroys the new node if another node
25797 already had the same key. C++17 try_emplace will avoid
25798 this. */
25799 const auto found
25800 = symbol_hash_table.find (entry.cu_indices);
25801 if (found != symbol_hash_table.end ())
25802 {
25803 entry.index_offset = found->second;
25804 continue;
25805 }
25806
25807 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
25808 entry.index_offset = cpool.size ();
25809 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
25810 for (const auto index : entry.cu_indices)
25811 cpool.append_data (MAYBE_SWAP (index));
25812 }
25813 }
25814
25815 /* Now write out the hash table. */
25816 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
25817 for (const auto &entry : symtab->data)
25818 {
25819 offset_type str_off, vec_off;
25820
25821 if (entry.name != NULL)
25822 {
25823 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
25824 if (insertpair.second)
25825 cpool.append_cstr0 (entry.name);
25826 str_off = insertpair.first->second;
25827 vec_off = entry.index_offset;
25828 }
25829 else
25830 {
25831 /* While 0 is a valid constant pool index, it is not valid
25832 to have 0 for both offsets. */
25833 str_off = 0;
25834 vec_off = 0;
25835 }
25836
25837 output.append_data (MAYBE_SWAP (str_off));
25838 output.append_data (MAYBE_SWAP (vec_off));
25839 }
25840 }
25841
25842 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
25843
25844 /* Helper struct for building the address table. */
25845 struct addrmap_index_data
25846 {
25847 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
25848 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
25849 {}
25850
25851 struct objfile *objfile;
25852 data_buf &addr_vec;
25853 psym_index_map &cu_index_htab;
25854
25855 /* Non-zero if the previous_* fields are valid.
25856 We can't write an entry until we see the next entry (since it is only then
25857 that we know the end of the entry). */
25858 int previous_valid;
25859 /* Index of the CU in the table of all CUs in the index file. */
25860 unsigned int previous_cu_index;
25861 /* Start address of the CU. */
25862 CORE_ADDR previous_cu_start;
25863 };
25864
25865 /* Write an address entry to ADDR_VEC. */
25866
25867 static void
25868 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
25869 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
25870 {
25871 CORE_ADDR baseaddr;
25872
25873 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25874
25875 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
25876 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
25877 addr_vec.append_data (MAYBE_SWAP (cu_index));
25878 }
25879
25880 /* Worker function for traversing an addrmap to build the address table. */
25881
25882 static int
25883 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
25884 {
25885 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
25886 struct partial_symtab *pst = (struct partial_symtab *) obj;
25887
25888 if (data->previous_valid)
25889 add_address_entry (data->objfile, data->addr_vec,
25890 data->previous_cu_start, start_addr,
25891 data->previous_cu_index);
25892
25893 data->previous_cu_start = start_addr;
25894 if (pst != NULL)
25895 {
25896 const auto it = data->cu_index_htab.find (pst);
25897 gdb_assert (it != data->cu_index_htab.cend ());
25898 data->previous_cu_index = it->second;
25899 data->previous_valid = 1;
25900 }
25901 else
25902 data->previous_valid = 0;
25903
25904 return 0;
25905 }
25906
25907 /* Write OBJFILE's address map to ADDR_VEC.
25908 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
25909 in the index file. */
25910
25911 static void
25912 write_address_map (struct objfile *objfile, data_buf &addr_vec,
25913 psym_index_map &cu_index_htab)
25914 {
25915 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
25916
25917 /* When writing the address table, we have to cope with the fact that
25918 the addrmap iterator only provides the start of a region; we have to
25919 wait until the next invocation to get the start of the next region. */
25920
25921 addrmap_index_data.objfile = objfile;
25922 addrmap_index_data.previous_valid = 0;
25923
25924 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
25925 &addrmap_index_data);
25926
25927 /* It's highly unlikely the last entry (end address = 0xff...ff)
25928 is valid, but we should still handle it.
25929 The end address is recorded as the start of the next region, but that
25930 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
25931 anyway. */
25932 if (addrmap_index_data.previous_valid)
25933 add_address_entry (objfile, addr_vec,
25934 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
25935 addrmap_index_data.previous_cu_index);
25936 }
25937
25938 /* Return the symbol kind of PSYM. */
25939
25940 static gdb_index_symbol_kind
25941 symbol_kind (struct partial_symbol *psym)
25942 {
25943 domain_enum domain = PSYMBOL_DOMAIN (psym);
25944 enum address_class aclass = PSYMBOL_CLASS (psym);
25945
25946 switch (domain)
25947 {
25948 case VAR_DOMAIN:
25949 switch (aclass)
25950 {
25951 case LOC_BLOCK:
25952 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
25953 case LOC_TYPEDEF:
25954 return GDB_INDEX_SYMBOL_KIND_TYPE;
25955 case LOC_COMPUTED:
25956 case LOC_CONST_BYTES:
25957 case LOC_OPTIMIZED_OUT:
25958 case LOC_STATIC:
25959 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25960 case LOC_CONST:
25961 /* Note: It's currently impossible to recognize psyms as enum values
25962 short of reading the type info. For now punt. */
25963 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
25964 default:
25965 /* There are other LOC_FOO values that one might want to classify
25966 as variables, but dwarf2read.c doesn't currently use them. */
25967 return GDB_INDEX_SYMBOL_KIND_OTHER;
25968 }
25969 case STRUCT_DOMAIN:
25970 return GDB_INDEX_SYMBOL_KIND_TYPE;
25971 default:
25972 return GDB_INDEX_SYMBOL_KIND_OTHER;
25973 }
25974 }
25975
25976 /* Add a list of partial symbols to SYMTAB. */
25977
25978 static void
25979 write_psymbols (struct mapped_symtab *symtab,
25980 std::unordered_set<partial_symbol *> &psyms_seen,
25981 struct partial_symbol **psymp,
25982 int count,
25983 offset_type cu_index,
25984 int is_static)
25985 {
25986 for (; count-- > 0; ++psymp)
25987 {
25988 struct partial_symbol *psym = *psymp;
25989
25990 if (SYMBOL_LANGUAGE (psym) == language_ada)
25991 error (_("Ada is not currently supported by the index"));
25992
25993 /* Only add a given psymbol once. */
25994 if (psyms_seen.insert (psym).second)
25995 {
25996 gdb_index_symbol_kind kind = symbol_kind (psym);
25997
25998 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
25999 is_static, kind, cu_index);
26000 }
26001 }
26002 }
26003
26004 /* A helper struct used when iterating over debug_types. */
26005 struct signatured_type_index_data
26006 {
26007 signatured_type_index_data (data_buf &types_list_,
26008 std::unordered_set<partial_symbol *> &psyms_seen_)
26009 : types_list (types_list_), psyms_seen (psyms_seen_)
26010 {}
26011
26012 struct objfile *objfile;
26013 struct mapped_symtab *symtab;
26014 data_buf &types_list;
26015 std::unordered_set<partial_symbol *> &psyms_seen;
26016 int cu_index;
26017 };
26018
26019 /* A helper function that writes a single signatured_type to an
26020 obstack. */
26021
26022 static int
26023 write_one_signatured_type (void **slot, void *d)
26024 {
26025 struct signatured_type_index_data *info
26026 = (struct signatured_type_index_data *) d;
26027 struct signatured_type *entry = (struct signatured_type *) *slot;
26028 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26029
26030 write_psymbols (info->symtab,
26031 info->psyms_seen,
26032 &info->objfile->global_psymbols[psymtab->globals_offset],
26033 psymtab->n_global_syms, info->cu_index,
26034 0);
26035 write_psymbols (info->symtab,
26036 info->psyms_seen,
26037 &info->objfile->static_psymbols[psymtab->statics_offset],
26038 psymtab->n_static_syms, info->cu_index,
26039 1);
26040
26041 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26042 to_underlying (entry->per_cu.sect_off));
26043 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26044 to_underlying (entry->type_offset_in_tu));
26045 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
26046
26047 ++info->cu_index;
26048
26049 return 1;
26050 }
26051
26052 /* Recurse into all "included" dependencies and count their symbols as
26053 if they appeared in this psymtab. */
26054
26055 static void
26056 recursively_count_psymbols (struct partial_symtab *psymtab,
26057 size_t &psyms_seen)
26058 {
26059 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26060 if (psymtab->dependencies[i]->user != NULL)
26061 recursively_count_psymbols (psymtab->dependencies[i],
26062 psyms_seen);
26063
26064 psyms_seen += psymtab->n_global_syms;
26065 psyms_seen += psymtab->n_static_syms;
26066 }
26067
26068 /* Recurse into all "included" dependencies and write their symbols as
26069 if they appeared in this psymtab. */
26070
26071 static void
26072 recursively_write_psymbols (struct objfile *objfile,
26073 struct partial_symtab *psymtab,
26074 struct mapped_symtab *symtab,
26075 std::unordered_set<partial_symbol *> &psyms_seen,
26076 offset_type cu_index)
26077 {
26078 int i;
26079
26080 for (i = 0; i < psymtab->number_of_dependencies; ++i)
26081 if (psymtab->dependencies[i]->user != NULL)
26082 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26083 symtab, psyms_seen, cu_index);
26084
26085 write_psymbols (symtab,
26086 psyms_seen,
26087 &objfile->global_psymbols[psymtab->globals_offset],
26088 psymtab->n_global_syms, cu_index,
26089 0);
26090 write_psymbols (symtab,
26091 psyms_seen,
26092 &objfile->static_psymbols[psymtab->statics_offset],
26093 psymtab->n_static_syms, cu_index,
26094 1);
26095 }
26096
26097 /* DWARF-5 .debug_names builder. */
26098 class debug_names
26099 {
26100 public:
26101 debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile, bool is_dwarf64,
26102 bfd_endian dwarf5_byte_order)
26103 : m_dwarf5_byte_order (dwarf5_byte_order),
26104 m_dwarf32 (dwarf5_byte_order),
26105 m_dwarf64 (dwarf5_byte_order),
26106 m_dwarf (is_dwarf64
26107 ? static_cast<dwarf &> (m_dwarf64)
26108 : static_cast<dwarf &> (m_dwarf32)),
26109 m_name_table_string_offs (m_dwarf.name_table_string_offs),
26110 m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
26111 m_debugstrlookup (dwarf2_per_objfile)
26112 {}
26113
26114 int dwarf5_offset_size () const
26115 {
26116 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
26117 return dwarf5_is_dwarf64 ? 8 : 4;
26118 }
26119
26120 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
26121 enum class unit_kind { cu, tu };
26122
26123 /* Insert one symbol. */
26124 void insert (const partial_symbol *psym, int cu_index, bool is_static,
26125 unit_kind kind)
26126 {
26127 const int dwarf_tag = psymbol_tag (psym);
26128 if (dwarf_tag == 0)
26129 return;
26130 const char *const name = SYMBOL_SEARCH_NAME (psym);
26131 const auto insertpair
26132 = m_name_to_value_set.emplace (c_str_view (name),
26133 std::set<symbol_value> ());
26134 std::set<symbol_value> &value_set = insertpair.first->second;
26135 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
26136 }
26137
26138 /* Build all the tables. All symbols must be already inserted.
26139 This function does not call file_write, caller has to do it
26140 afterwards. */
26141 void build ()
26142 {
26143 /* Verify the build method has not be called twice. */
26144 gdb_assert (m_abbrev_table.empty ());
26145 const size_t name_count = m_name_to_value_set.size ();
26146 m_bucket_table.resize
26147 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26148 m_hash_table.reserve (name_count);
26149 m_name_table_string_offs.reserve (name_count);
26150 m_name_table_entry_offs.reserve (name_count);
26151
26152 /* Map each hash of symbol to its name and value. */
26153 struct hash_it_pair
26154 {
26155 uint32_t hash;
26156 decltype (m_name_to_value_set)::const_iterator it;
26157 };
26158 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26159 bucket_hash.resize (m_bucket_table.size ());
26160 for (decltype (m_name_to_value_set)::const_iterator it
26161 = m_name_to_value_set.cbegin ();
26162 it != m_name_to_value_set.cend ();
26163 ++it)
26164 {
26165 const char *const name = it->first.c_str ();
26166 const uint32_t hash = dwarf5_djb_hash (name);
26167 hash_it_pair hashitpair;
26168 hashitpair.hash = hash;
26169 hashitpair.it = it;
26170 auto &slot = bucket_hash[hash % bucket_hash.size()];
26171 slot.push_front (std::move (hashitpair));
26172 }
26173 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26174 {
26175 const std::forward_list<hash_it_pair> &hashitlist
26176 = bucket_hash[bucket_ix];
26177 if (hashitlist.empty ())
26178 continue;
26179 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26180 /* The hashes array is indexed starting at 1. */
26181 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26182 sizeof (bucket_slot), m_dwarf5_byte_order,
26183 m_hash_table.size () + 1);
26184 for (const hash_it_pair &hashitpair : hashitlist)
26185 {
26186 m_hash_table.push_back (0);
26187 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26188 (&m_hash_table.back ()),
26189 sizeof (m_hash_table.back ()),
26190 m_dwarf5_byte_order, hashitpair.hash);
26191 const c_str_view &name = hashitpair.it->first;
26192 const std::set<symbol_value> &value_set = hashitpair.it->second;
26193 m_name_table_string_offs.push_back_reorder
26194 (m_debugstrlookup.lookup (name.c_str ()));
26195 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26196 gdb_assert (!value_set.empty ());
26197 for (const symbol_value &value : value_set)
26198 {
26199 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
26200 value.is_static,
26201 value.kind)];
26202 if (idx == 0)
26203 {
26204 idx = m_idx_next++;
26205 m_abbrev_table.append_unsigned_leb128 (idx);
26206 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
26207 m_abbrev_table.append_unsigned_leb128
26208 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
26209 : DW_IDX_type_unit);
26210 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26211 m_abbrev_table.append_unsigned_leb128 (value.is_static
26212 ? DW_IDX_GNU_internal
26213 : DW_IDX_GNU_external);
26214 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26215
26216 /* Terminate attributes list. */
26217 m_abbrev_table.append_unsigned_leb128 (0);
26218 m_abbrev_table.append_unsigned_leb128 (0);
26219 }
26220
26221 m_entry_pool.append_unsigned_leb128 (idx);
26222 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26223 }
26224
26225 /* Terminate the list of CUs. */
26226 m_entry_pool.append_unsigned_leb128 (0);
26227 }
26228 }
26229 gdb_assert (m_hash_table.size () == name_count);
26230
26231 /* Terminate tags list. */
26232 m_abbrev_table.append_unsigned_leb128 (0);
26233 }
26234
26235 /* Return .debug_names bucket count. This must be called only after
26236 calling the build method. */
26237 uint32_t bucket_count () const
26238 {
26239 /* Verify the build method has been already called. */
26240 gdb_assert (!m_abbrev_table.empty ());
26241 const uint32_t retval = m_bucket_table.size ();
26242
26243 /* Check for overflow. */
26244 gdb_assert (retval == m_bucket_table.size ());
26245 return retval;
26246 }
26247
26248 /* Return .debug_names names count. This must be called only after
26249 calling the build method. */
26250 uint32_t name_count () const
26251 {
26252 /* Verify the build method has been already called. */
26253 gdb_assert (!m_abbrev_table.empty ());
26254 const uint32_t retval = m_hash_table.size ();
26255
26256 /* Check for overflow. */
26257 gdb_assert (retval == m_hash_table.size ());
26258 return retval;
26259 }
26260
26261 /* Return number of bytes of .debug_names abbreviation table. This
26262 must be called only after calling the build method. */
26263 uint32_t abbrev_table_bytes () const
26264 {
26265 gdb_assert (!m_abbrev_table.empty ());
26266 return m_abbrev_table.size ();
26267 }
26268
26269 /* Recurse into all "included" dependencies and store their symbols
26270 as if they appeared in this psymtab. */
26271 void recursively_write_psymbols
26272 (struct objfile *objfile,
26273 struct partial_symtab *psymtab,
26274 std::unordered_set<partial_symbol *> &psyms_seen,
26275 int cu_index)
26276 {
26277 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26278 if (psymtab->dependencies[i]->user != NULL)
26279 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26280 psyms_seen, cu_index);
26281
26282 write_psymbols (psyms_seen,
26283 &objfile->global_psymbols[psymtab->globals_offset],
26284 psymtab->n_global_syms, cu_index, false, unit_kind::cu);
26285 write_psymbols (psyms_seen,
26286 &objfile->static_psymbols[psymtab->statics_offset],
26287 psymtab->n_static_syms, cu_index, true, unit_kind::cu);
26288 }
26289
26290 /* Return number of bytes the .debug_names section will have. This
26291 must be called only after calling the build method. */
26292 size_t bytes () const
26293 {
26294 /* Verify the build method has been already called. */
26295 gdb_assert (!m_abbrev_table.empty ());
26296 size_t expected_bytes = 0;
26297 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26298 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26299 expected_bytes += m_name_table_string_offs.bytes ();
26300 expected_bytes += m_name_table_entry_offs.bytes ();
26301 expected_bytes += m_abbrev_table.size ();
26302 expected_bytes += m_entry_pool.size ();
26303 return expected_bytes;
26304 }
26305
26306 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26307 FILE_STR. This must be called only after calling the build
26308 method. */
26309 void file_write (FILE *file_names, FILE *file_str) const
26310 {
26311 /* Verify the build method has been already called. */
26312 gdb_assert (!m_abbrev_table.empty ());
26313 ::file_write (file_names, m_bucket_table);
26314 ::file_write (file_names, m_hash_table);
26315 m_name_table_string_offs.file_write (file_names);
26316 m_name_table_entry_offs.file_write (file_names);
26317 m_abbrev_table.file_write (file_names);
26318 m_entry_pool.file_write (file_names);
26319 m_debugstrlookup.file_write (file_str);
26320 }
26321
26322 /* A helper user data for write_one_signatured_type. */
26323 class write_one_signatured_type_data
26324 {
26325 public:
26326 write_one_signatured_type_data (debug_names &nametable_,
26327 signatured_type_index_data &&info_)
26328 : nametable (nametable_), info (std::move (info_))
26329 {}
26330 debug_names &nametable;
26331 struct signatured_type_index_data info;
26332 };
26333
26334 /* A helper function to pass write_one_signatured_type to
26335 htab_traverse_noresize. */
26336 static int
26337 write_one_signatured_type (void **slot, void *d)
26338 {
26339 write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
26340 struct signatured_type_index_data *info = &data->info;
26341 struct signatured_type *entry = (struct signatured_type *) *slot;
26342
26343 data->nametable.write_one_signatured_type (entry, info);
26344
26345 return 1;
26346 }
26347
26348 private:
26349
26350 /* Storage for symbol names mapping them to their .debug_str section
26351 offsets. */
26352 class debug_str_lookup
26353 {
26354 public:
26355
26356 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26357 All .debug_str section strings are automatically stored. */
26358 debug_str_lookup (struct dwarf2_per_objfile *dwarf2_per_objfile)
26359 : m_abfd (dwarf2_per_objfile->objfile->obfd),
26360 m_dwarf2_per_objfile (dwarf2_per_objfile)
26361 {
26362 dwarf2_read_section (dwarf2_per_objfile->objfile,
26363 &dwarf2_per_objfile->str);
26364 if (dwarf2_per_objfile->str.buffer == NULL)
26365 return;
26366 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26367 data < (dwarf2_per_objfile->str.buffer
26368 + dwarf2_per_objfile->str.size);)
26369 {
26370 const char *const s = reinterpret_cast<const char *> (data);
26371 const auto insertpair
26372 = m_str_table.emplace (c_str_view (s),
26373 data - dwarf2_per_objfile->str.buffer);
26374 if (!insertpair.second)
26375 complaint (&symfile_complaints,
26376 _("Duplicate string \"%s\" in "
26377 ".debug_str section [in module %s]"),
26378 s, bfd_get_filename (m_abfd));
26379 data += strlen (s) + 1;
26380 }
26381 }
26382
26383 /* Return offset of symbol name S in the .debug_str section. Add
26384 such symbol to the section's end if it does not exist there
26385 yet. */
26386 size_t lookup (const char *s)
26387 {
26388 const auto it = m_str_table.find (c_str_view (s));
26389 if (it != m_str_table.end ())
26390 return it->second;
26391 const size_t offset = (m_dwarf2_per_objfile->str.size
26392 + m_str_add_buf.size ());
26393 m_str_table.emplace (c_str_view (s), offset);
26394 m_str_add_buf.append_cstr0 (s);
26395 return offset;
26396 }
26397
26398 /* Append the end of the .debug_str section to FILE. */
26399 void file_write (FILE *file) const
26400 {
26401 m_str_add_buf.file_write (file);
26402 }
26403
26404 private:
26405 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26406 bfd *const m_abfd;
26407 struct dwarf2_per_objfile *m_dwarf2_per_objfile;
26408
26409 /* Data to add at the end of .debug_str for new needed symbol names. */
26410 data_buf m_str_add_buf;
26411 };
26412
26413 /* Container to map used DWARF tags to their .debug_names abbreviation
26414 tags. */
26415 class index_key
26416 {
26417 public:
26418 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
26419 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
26420 {
26421 }
26422
26423 bool
26424 operator== (const index_key &other) const
26425 {
26426 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
26427 && kind == other.kind);
26428 }
26429
26430 const int dwarf_tag;
26431 const bool is_static;
26432 const unit_kind kind;
26433 };
26434
26435 /* Provide std::unordered_map::hasher for index_key. */
26436 class index_key_hasher
26437 {
26438 public:
26439 size_t
26440 operator () (const index_key &key) const
26441 {
26442 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26443 }
26444 };
26445
26446 /* Parameters of one symbol entry. */
26447 class symbol_value
26448 {
26449 public:
26450 const int dwarf_tag, cu_index;
26451 const bool is_static;
26452 const unit_kind kind;
26453
26454 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
26455 unit_kind kind_)
26456 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
26457 kind (kind_)
26458 {}
26459
26460 bool
26461 operator< (const symbol_value &other) const
26462 {
26463 #define X(n) \
26464 do \
26465 { \
26466 if (n < other.n) \
26467 return true; \
26468 if (n > other.n) \
26469 return false; \
26470 } \
26471 while (0)
26472 X (dwarf_tag);
26473 X (is_static);
26474 X (kind);
26475 X (cu_index);
26476 #undef X
26477 return false;
26478 }
26479 };
26480
26481 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26482 output. */
26483 class offset_vec
26484 {
26485 protected:
26486 const bfd_endian dwarf5_byte_order;
26487 public:
26488 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26489 : dwarf5_byte_order (dwarf5_byte_order_)
26490 {}
26491
26492 /* Call std::vector::reserve for NELEM elements. */
26493 virtual void reserve (size_t nelem) = 0;
26494
26495 /* Call std::vector::push_back with store_unsigned_integer byte
26496 reordering for ELEM. */
26497 virtual void push_back_reorder (size_t elem) = 0;
26498
26499 /* Return expected output size in bytes. */
26500 virtual size_t bytes () const = 0;
26501
26502 /* Write name table to FILE. */
26503 virtual void file_write (FILE *file) const = 0;
26504 };
26505
26506 /* Template to unify DWARF-32 and DWARF-64 output. */
26507 template<typename OffsetSize>
26508 class offset_vec_tmpl : public offset_vec
26509 {
26510 public:
26511 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26512 : offset_vec (dwarf5_byte_order_)
26513 {}
26514
26515 /* Implement offset_vec::reserve. */
26516 void reserve (size_t nelem) override
26517 {
26518 m_vec.reserve (nelem);
26519 }
26520
26521 /* Implement offset_vec::push_back_reorder. */
26522 void push_back_reorder (size_t elem) override
26523 {
26524 m_vec.push_back (elem);
26525 /* Check for overflow. */
26526 gdb_assert (m_vec.back () == elem);
26527 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
26528 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
26529 }
26530
26531 /* Implement offset_vec::bytes. */
26532 size_t bytes () const override
26533 {
26534 return m_vec.size () * sizeof (m_vec[0]);
26535 }
26536
26537 /* Implement offset_vec::file_write. */
26538 void file_write (FILE *file) const override
26539 {
26540 ::file_write (file, m_vec);
26541 }
26542
26543 private:
26544 std::vector<OffsetSize> m_vec;
26545 };
26546
26547 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
26548 respecting name table width. */
26549 class dwarf
26550 {
26551 public:
26552 offset_vec &name_table_string_offs, &name_table_entry_offs;
26553
26554 dwarf (offset_vec &name_table_string_offs_,
26555 offset_vec &name_table_entry_offs_)
26556 : name_table_string_offs (name_table_string_offs_),
26557 name_table_entry_offs (name_table_entry_offs_)
26558 {
26559 }
26560 };
26561
26562 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
26563 respecting name table width. */
26564 template<typename OffsetSize>
26565 class dwarf_tmpl : public dwarf
26566 {
26567 public:
26568 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
26569 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
26570 m_name_table_string_offs (dwarf5_byte_order_),
26571 m_name_table_entry_offs (dwarf5_byte_order_)
26572 {}
26573
26574 private:
26575 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
26576 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
26577 };
26578
26579 /* Try to reconstruct original DWARF tag for given partial_symbol.
26580 This function is not DWARF-5 compliant but it is sufficient for
26581 GDB as a DWARF-5 index consumer. */
26582 static int psymbol_tag (const struct partial_symbol *psym)
26583 {
26584 domain_enum domain = PSYMBOL_DOMAIN (psym);
26585 enum address_class aclass = PSYMBOL_CLASS (psym);
26586
26587 switch (domain)
26588 {
26589 case VAR_DOMAIN:
26590 switch (aclass)
26591 {
26592 case LOC_BLOCK:
26593 return DW_TAG_subprogram;
26594 case LOC_TYPEDEF:
26595 return DW_TAG_typedef;
26596 case LOC_COMPUTED:
26597 case LOC_CONST_BYTES:
26598 case LOC_OPTIMIZED_OUT:
26599 case LOC_STATIC:
26600 return DW_TAG_variable;
26601 case LOC_CONST:
26602 /* Note: It's currently impossible to recognize psyms as enum values
26603 short of reading the type info. For now punt. */
26604 return DW_TAG_variable;
26605 default:
26606 /* There are other LOC_FOO values that one might want to classify
26607 as variables, but dwarf2read.c doesn't currently use them. */
26608 return DW_TAG_variable;
26609 }
26610 case STRUCT_DOMAIN:
26611 return DW_TAG_structure_type;
26612 default:
26613 return 0;
26614 }
26615 }
26616
26617 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
26618 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
26619 struct partial_symbol **psymp, int count, int cu_index,
26620 bool is_static, unit_kind kind)
26621 {
26622 for (; count-- > 0; ++psymp)
26623 {
26624 struct partial_symbol *psym = *psymp;
26625
26626 if (SYMBOL_LANGUAGE (psym) == language_ada)
26627 error (_("Ada is not currently supported by the index"));
26628
26629 /* Only add a given psymbol once. */
26630 if (psyms_seen.insert (psym).second)
26631 insert (psym, cu_index, is_static, kind);
26632 }
26633 }
26634
26635 /* A helper function that writes a single signatured_type
26636 to a debug_names. */
26637 void
26638 write_one_signatured_type (struct signatured_type *entry,
26639 struct signatured_type_index_data *info)
26640 {
26641 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26642
26643 write_psymbols (info->psyms_seen,
26644 &info->objfile->global_psymbols[psymtab->globals_offset],
26645 psymtab->n_global_syms, info->cu_index, false,
26646 unit_kind::tu);
26647 write_psymbols (info->psyms_seen,
26648 &info->objfile->static_psymbols[psymtab->statics_offset],
26649 psymtab->n_static_syms, info->cu_index, true,
26650 unit_kind::tu);
26651
26652 info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
26653 to_underlying (entry->per_cu.sect_off));
26654
26655 ++info->cu_index;
26656 }
26657
26658 /* Store value of each symbol. */
26659 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
26660 m_name_to_value_set;
26661
26662 /* Tables of DWARF-5 .debug_names. They are in object file byte
26663 order. */
26664 std::vector<uint32_t> m_bucket_table;
26665 std::vector<uint32_t> m_hash_table;
26666
26667 const bfd_endian m_dwarf5_byte_order;
26668 dwarf_tmpl<uint32_t> m_dwarf32;
26669 dwarf_tmpl<uint64_t> m_dwarf64;
26670 dwarf &m_dwarf;
26671 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
26672 debug_str_lookup m_debugstrlookup;
26673
26674 /* Map each used .debug_names abbreviation tag parameter to its
26675 index value. */
26676 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
26677
26678 /* Next unused .debug_names abbreviation tag for
26679 m_indexkey_to_idx. */
26680 int m_idx_next = 1;
26681
26682 /* .debug_names abbreviation table. */
26683 data_buf m_abbrev_table;
26684
26685 /* .debug_names entry pool. */
26686 data_buf m_entry_pool;
26687 };
26688
26689 /* Return iff any of the needed offsets does not fit into 32-bit
26690 .debug_names section. */
26691
26692 static bool
26693 check_dwarf64_offsets (struct dwarf2_per_objfile *dwarf2_per_objfile)
26694 {
26695 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26696 {
26697 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
26698
26699 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26700 return true;
26701 }
26702 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
26703 {
26704 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
26705 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
26706
26707 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
26708 return true;
26709 }
26710 return false;
26711 }
26712
26713 /* The psyms_seen set is potentially going to be largish (~40k
26714 elements when indexing a -g3 build of GDB itself). Estimate the
26715 number of elements in order to avoid too many rehashes, which
26716 require rebuilding buckets and thus many trips to
26717 malloc/free. */
26718
26719 static size_t
26720 psyms_seen_size (struct dwarf2_per_objfile *dwarf2_per_objfile)
26721 {
26722 size_t psyms_count = 0;
26723 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26724 {
26725 struct dwarf2_per_cu_data *per_cu
26726 = dwarf2_per_objfile->all_comp_units[i];
26727 struct partial_symtab *psymtab = per_cu->v.psymtab;
26728
26729 if (psymtab != NULL && psymtab->user == NULL)
26730 recursively_count_psymbols (psymtab, psyms_count);
26731 }
26732 /* Generating an index for gdb itself shows a ratio of
26733 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
26734 return psyms_count / 4;
26735 }
26736
26737 /* Write new .gdb_index section for OBJFILE into OUT_FILE.
26738 Return how many bytes were expected to be written into OUT_FILE. */
26739
26740 static size_t
26741 write_gdbindex (struct dwarf2_per_objfile *dwarf2_per_objfile, FILE *out_file)
26742 {
26743 struct objfile *objfile = dwarf2_per_objfile->objfile;
26744 mapped_symtab symtab;
26745 data_buf cu_list;
26746
26747 /* While we're scanning CU's create a table that maps a psymtab pointer
26748 (which is what addrmap records) to its index (which is what is recorded
26749 in the index file). This will later be needed to write the address
26750 table. */
26751 psym_index_map cu_index_htab;
26752 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
26753
26754 /* The CU list is already sorted, so we don't need to do additional
26755 work here. Also, the debug_types entries do not appear in
26756 all_comp_units, but only in their own hash table. */
26757
26758 std::unordered_set<partial_symbol *> psyms_seen
26759 (psyms_seen_size (dwarf2_per_objfile));
26760 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26761 {
26762 struct dwarf2_per_cu_data *per_cu
26763 = dwarf2_per_objfile->all_comp_units[i];
26764 struct partial_symtab *psymtab = per_cu->v.psymtab;
26765
26766 /* CU of a shared file from 'dwz -m' may be unused by this main file.
26767 It may be referenced from a local scope but in such case it does not
26768 need to be present in .gdb_index. */
26769 if (psymtab == NULL)
26770 continue;
26771
26772 if (psymtab->user == NULL)
26773 recursively_write_psymbols (objfile, psymtab, &symtab,
26774 psyms_seen, i);
26775
26776 const auto insertpair = cu_index_htab.emplace (psymtab, i);
26777 gdb_assert (insertpair.second);
26778
26779 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
26780 to_underlying (per_cu->sect_off));
26781 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
26782 }
26783
26784 /* Dump the address map. */
26785 data_buf addr_vec;
26786 write_address_map (objfile, addr_vec, cu_index_htab);
26787
26788 /* Write out the .debug_type entries, if any. */
26789 data_buf types_cu_list;
26790 if (dwarf2_per_objfile->signatured_types)
26791 {
26792 signatured_type_index_data sig_data (types_cu_list,
26793 psyms_seen);
26794
26795 sig_data.objfile = objfile;
26796 sig_data.symtab = &symtab;
26797 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
26798 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26799 write_one_signatured_type, &sig_data);
26800 }
26801
26802 /* Now that we've processed all symbols we can shrink their cu_indices
26803 lists. */
26804 uniquify_cu_indices (&symtab);
26805
26806 data_buf symtab_vec, constant_pool;
26807 write_hash_table (&symtab, symtab_vec, constant_pool);
26808
26809 data_buf contents;
26810 const offset_type size_of_contents = 6 * sizeof (offset_type);
26811 offset_type total_len = size_of_contents;
26812
26813 /* The version number. */
26814 contents.append_data (MAYBE_SWAP (8));
26815
26816 /* The offset of the CU list from the start of the file. */
26817 contents.append_data (MAYBE_SWAP (total_len));
26818 total_len += cu_list.size ();
26819
26820 /* The offset of the types CU list from the start of the file. */
26821 contents.append_data (MAYBE_SWAP (total_len));
26822 total_len += types_cu_list.size ();
26823
26824 /* The offset of the address table from the start of the file. */
26825 contents.append_data (MAYBE_SWAP (total_len));
26826 total_len += addr_vec.size ();
26827
26828 /* The offset of the symbol table from the start of the file. */
26829 contents.append_data (MAYBE_SWAP (total_len));
26830 total_len += symtab_vec.size ();
26831
26832 /* The offset of the constant pool from the start of the file. */
26833 contents.append_data (MAYBE_SWAP (total_len));
26834 total_len += constant_pool.size ();
26835
26836 gdb_assert (contents.size () == size_of_contents);
26837
26838 contents.file_write (out_file);
26839 cu_list.file_write (out_file);
26840 types_cu_list.file_write (out_file);
26841 addr_vec.file_write (out_file);
26842 symtab_vec.file_write (out_file);
26843 constant_pool.file_write (out_file);
26844
26845 return total_len;
26846 }
26847
26848 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
26849 static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
26850
26851 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
26852 needed addition to .debug_str section to OUT_FILE_STR. Return how
26853 many bytes were expected to be written into OUT_FILE. */
26854
26855 static size_t
26856 write_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
26857 FILE *out_file, FILE *out_file_str)
26858 {
26859 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (dwarf2_per_objfile);
26860 struct objfile *objfile = dwarf2_per_objfile->objfile;
26861 const enum bfd_endian dwarf5_byte_order
26862 = gdbarch_byte_order (get_objfile_arch (objfile));
26863
26864 /* The CU list is already sorted, so we don't need to do additional
26865 work here. Also, the debug_types entries do not appear in
26866 all_comp_units, but only in their own hash table. */
26867 data_buf cu_list;
26868 debug_names nametable (dwarf2_per_objfile, dwarf5_is_dwarf64,
26869 dwarf5_byte_order);
26870 std::unordered_set<partial_symbol *>
26871 psyms_seen (psyms_seen_size (dwarf2_per_objfile));
26872 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
26873 {
26874 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
26875 partial_symtab *psymtab = per_cu->v.psymtab;
26876
26877 /* CU of a shared file from 'dwz -m' may be unused by this main
26878 file. It may be referenced from a local scope but in such
26879 case it does not need to be present in .debug_names. */
26880 if (psymtab == NULL)
26881 continue;
26882
26883 if (psymtab->user == NULL)
26884 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
26885
26886 cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
26887 to_underlying (per_cu->sect_off));
26888 }
26889
26890 /* Write out the .debug_type entries, if any. */
26891 data_buf types_cu_list;
26892 if (dwarf2_per_objfile->signatured_types)
26893 {
26894 debug_names::write_one_signatured_type_data sig_data (nametable,
26895 signatured_type_index_data (types_cu_list, psyms_seen));
26896
26897 sig_data.info.objfile = objfile;
26898 /* It is used only for gdb_index. */
26899 sig_data.info.symtab = nullptr;
26900 sig_data.info.cu_index = 0;
26901 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
26902 debug_names::write_one_signatured_type,
26903 &sig_data);
26904 }
26905
26906 nametable.build ();
26907
26908 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
26909
26910 const offset_type bytes_of_header
26911 = ((dwarf5_is_dwarf64 ? 12 : 4)
26912 + 2 + 2 + 7 * 4
26913 + sizeof (dwarf5_gdb_augmentation));
26914 size_t expected_bytes = 0;
26915 expected_bytes += bytes_of_header;
26916 expected_bytes += cu_list.size ();
26917 expected_bytes += types_cu_list.size ();
26918 expected_bytes += nametable.bytes ();
26919 data_buf header;
26920
26921 if (!dwarf5_is_dwarf64)
26922 {
26923 const uint64_t size64 = expected_bytes - 4;
26924 gdb_assert (size64 < 0xfffffff0);
26925 header.append_uint (4, dwarf5_byte_order, size64);
26926 }
26927 else
26928 {
26929 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
26930 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
26931 }
26932
26933 /* The version number. */
26934 header.append_uint (2, dwarf5_byte_order, 5);
26935
26936 /* Padding. */
26937 header.append_uint (2, dwarf5_byte_order, 0);
26938
26939 /* comp_unit_count - The number of CUs in the CU list. */
26940 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
26941
26942 /* local_type_unit_count - The number of TUs in the local TU
26943 list. */
26944 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
26945
26946 /* foreign_type_unit_count - The number of TUs in the foreign TU
26947 list. */
26948 header.append_uint (4, dwarf5_byte_order, 0);
26949
26950 /* bucket_count - The number of hash buckets in the hash lookup
26951 table. */
26952 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
26953
26954 /* name_count - The number of unique names in the index. */
26955 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
26956
26957 /* abbrev_table_size - The size in bytes of the abbreviations
26958 table. */
26959 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
26960
26961 /* augmentation_string_size - The size in bytes of the augmentation
26962 string. This value is rounded up to a multiple of 4. */
26963 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
26964 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
26965 header.append_data (dwarf5_gdb_augmentation);
26966
26967 gdb_assert (header.size () == bytes_of_header);
26968
26969 header.file_write (out_file);
26970 cu_list.file_write (out_file);
26971 types_cu_list.file_write (out_file);
26972 nametable.file_write (out_file, out_file_str);
26973
26974 return expected_bytes;
26975 }
26976
26977 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
26978 position is at the end of the file. */
26979
26980 static void
26981 assert_file_size (FILE *file, const char *filename, size_t expected_size)
26982 {
26983 const auto file_size = ftell (file);
26984 if (file_size == -1)
26985 error (_("Can't get `%s' size"), filename);
26986 gdb_assert (file_size == expected_size);
26987 }
26988
26989 /* Create an index file for OBJFILE in the directory DIR. */
26990
26991 static void
26992 write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
26993 const char *dir,
26994 dw_index_kind index_kind)
26995 {
26996 struct objfile *objfile = dwarf2_per_objfile->objfile;
26997
26998 if (dwarf2_per_objfile->using_index)
26999 error (_("Cannot use an index to create the index"));
27000
27001 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
27002 error (_("Cannot make an index when the file has multiple .debug_types sections"));
27003
27004 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
27005 return;
27006
27007 struct stat st;
27008 if (stat (objfile_name (objfile), &st) < 0)
27009 perror_with_name (objfile_name (objfile));
27010
27011 std::string filename (std::string (dir) + SLASH_STRING
27012 + lbasename (objfile_name (objfile))
27013 + (index_kind == dw_index_kind::DEBUG_NAMES
27014 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
27015
27016 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
27017 if (!out_file)
27018 error (_("Can't open `%s' for writing"), filename.c_str ());
27019
27020 /* Order matters here; we want FILE to be closed before FILENAME is
27021 unlinked, because on MS-Windows one cannot delete a file that is
27022 still open. (Don't call anything here that might throw until
27023 file_closer is created.) */
27024 gdb::unlinker unlink_file (filename.c_str ());
27025 gdb_file_up close_out_file (out_file);
27026
27027 if (index_kind == dw_index_kind::DEBUG_NAMES)
27028 {
27029 std::string filename_str (std::string (dir) + SLASH_STRING
27030 + lbasename (objfile_name (objfile))
27031 + DEBUG_STR_SUFFIX);
27032 FILE *out_file_str
27033 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
27034 if (!out_file_str)
27035 error (_("Can't open `%s' for writing"), filename_str.c_str ());
27036 gdb::unlinker unlink_file_str (filename_str.c_str ());
27037 gdb_file_up close_out_file_str (out_file_str);
27038
27039 const size_t total_len
27040 = write_debug_names (dwarf2_per_objfile, out_file, out_file_str);
27041 assert_file_size (out_file, filename.c_str (), total_len);
27042
27043 /* We want to keep the file .debug_str file too. */
27044 unlink_file_str.keep ();
27045 }
27046 else
27047 {
27048 const size_t total_len
27049 = write_gdbindex (dwarf2_per_objfile, out_file);
27050 assert_file_size (out_file, filename.c_str (), total_len);
27051 }
27052
27053 /* We want to keep the file. */
27054 unlink_file.keep ();
27055 }
27056
27057 /* Implementation of the `save gdb-index' command.
27058
27059 Note that the .gdb_index file format used by this command is
27060 documented in the GDB manual. Any changes here must be documented
27061 there. */
27062
27063 static void
27064 save_gdb_index_command (const char *arg, int from_tty)
27065 {
27066 struct objfile *objfile;
27067 const char dwarf5space[] = "-dwarf-5 ";
27068 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
27069
27070 if (!arg)
27071 arg = "";
27072
27073 arg = skip_spaces (arg);
27074 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
27075 {
27076 index_kind = dw_index_kind::DEBUG_NAMES;
27077 arg += strlen (dwarf5space);
27078 arg = skip_spaces (arg);
27079 }
27080
27081 if (!*arg)
27082 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
27083
27084 ALL_OBJFILES (objfile)
27085 {
27086 struct stat st;
27087
27088 /* If the objfile does not correspond to an actual file, skip it. */
27089 if (stat (objfile_name (objfile), &st) < 0)
27090 continue;
27091
27092 struct dwarf2_per_objfile *dwarf2_per_objfile
27093 = get_dwarf2_per_objfile (objfile);
27094
27095 if (dwarf2_per_objfile != NULL)
27096 {
27097 TRY
27098 {
27099 write_psymtabs_to_index (dwarf2_per_objfile, arg, index_kind);
27100 }
27101 CATCH (except, RETURN_MASK_ERROR)
27102 {
27103 exception_fprintf (gdb_stderr, except,
27104 _("Error while writing index for `%s': "),
27105 objfile_name (objfile));
27106 }
27107 END_CATCH
27108 }
27109
27110 }
27111 }
27112
27113 \f
27114
27115 int dwarf_always_disassemble;
27116
27117 static void
27118 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
27119 struct cmd_list_element *c, const char *value)
27120 {
27121 fprintf_filtered (file,
27122 _("Whether to always disassemble "
27123 "DWARF expressions is %s.\n"),
27124 value);
27125 }
27126
27127 static void
27128 show_check_physname (struct ui_file *file, int from_tty,
27129 struct cmd_list_element *c, const char *value)
27130 {
27131 fprintf_filtered (file,
27132 _("Whether to check \"physname\" is %s.\n"),
27133 value);
27134 }
27135
27136 void
27137 _initialize_dwarf2_read (void)
27138 {
27139 struct cmd_list_element *c;
27140
27141 dwarf2_objfile_data_key
27142 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
27143
27144 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
27145 Set DWARF specific variables.\n\
27146 Configure DWARF variables such as the cache size"),
27147 &set_dwarf_cmdlist, "maintenance set dwarf ",
27148 0/*allow-unknown*/, &maintenance_set_cmdlist);
27149
27150 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
27151 Show DWARF specific variables\n\
27152 Show DWARF variables such as the cache size"),
27153 &show_dwarf_cmdlist, "maintenance show dwarf ",
27154 0/*allow-unknown*/, &maintenance_show_cmdlist);
27155
27156 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
27157 &dwarf_max_cache_age, _("\
27158 Set the upper bound on the age of cached DWARF compilation units."), _("\
27159 Show the upper bound on the age of cached DWARF compilation units."), _("\
27160 A higher limit means that cached compilation units will be stored\n\
27161 in memory longer, and more total memory will be used. Zero disables\n\
27162 caching, which can slow down startup."),
27163 NULL,
27164 show_dwarf_max_cache_age,
27165 &set_dwarf_cmdlist,
27166 &show_dwarf_cmdlist);
27167
27168 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
27169 &dwarf_always_disassemble, _("\
27170 Set whether `info address' always disassembles DWARF expressions."), _("\
27171 Show whether `info address' always disassembles DWARF expressions."), _("\
27172 When enabled, DWARF expressions are always printed in an assembly-like\n\
27173 syntax. When disabled, expressions will be printed in a more\n\
27174 conversational style, when possible."),
27175 NULL,
27176 show_dwarf_always_disassemble,
27177 &set_dwarf_cmdlist,
27178 &show_dwarf_cmdlist);
27179
27180 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27181 Set debugging of the DWARF reader."), _("\
27182 Show debugging of the DWARF reader."), _("\
27183 When enabled (non-zero), debugging messages are printed during DWARF\n\
27184 reading and symtab expansion. A value of 1 (one) provides basic\n\
27185 information. A value greater than 1 provides more verbose information."),
27186 NULL,
27187 NULL,
27188 &setdebuglist, &showdebuglist);
27189
27190 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27191 Set debugging of the DWARF DIE reader."), _("\
27192 Show debugging of the DWARF DIE reader."), _("\
27193 When enabled (non-zero), DIEs are dumped after they are read in.\n\
27194 The value is the maximum depth to print."),
27195 NULL,
27196 NULL,
27197 &setdebuglist, &showdebuglist);
27198
27199 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27200 Set debugging of the dwarf line reader."), _("\
27201 Show debugging of the dwarf line reader."), _("\
27202 When enabled (non-zero), line number entries are dumped as they are read in.\n\
27203 A value of 1 (one) provides basic information.\n\
27204 A value greater than 1 provides more verbose information."),
27205 NULL,
27206 NULL,
27207 &setdebuglist, &showdebuglist);
27208
27209 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27210 Set cross-checking of \"physname\" code against demangler."), _("\
27211 Show cross-checking of \"physname\" code against demangler."), _("\
27212 When enabled, GDB's internal \"physname\" code is checked against\n\
27213 the demangler."),
27214 NULL, show_check_physname,
27215 &setdebuglist, &showdebuglist);
27216
27217 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27218 no_class, &use_deprecated_index_sections, _("\
27219 Set whether to use deprecated gdb_index sections."), _("\
27220 Show whether to use deprecated gdb_index sections."), _("\
27221 When enabled, deprecated .gdb_index sections are used anyway.\n\
27222 Normally they are ignored either because of a missing feature or\n\
27223 performance issue.\n\
27224 Warning: This option must be enabled before gdb reads the file."),
27225 NULL,
27226 NULL,
27227 &setlist, &showlist);
27228
27229 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
27230 _("\
27231 Save a gdb-index file.\n\
27232 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27233 \n\
27234 No options create one file with .gdb-index extension for pre-DWARF-5\n\
27235 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27236 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
27237 &save_cmdlist);
27238 set_cmd_completer (c, filename_completer);
27239
27240 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27241 &dwarf2_locexpr_funcs);
27242 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27243 &dwarf2_loclist_funcs);
27244
27245 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27246 &dwarf2_block_frame_base_locexpr_funcs);
27247 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27248 &dwarf2_block_frame_base_loclist_funcs);
27249
27250 #if GDB_SELF_TEST
27251 selftests::register_test ("dw2_expand_symtabs_matching",
27252 selftests::dw2_expand_symtabs_matching::run_test);
27253 #endif
27254 }
This page took 1.172101 seconds and 4 git commands to generate.